xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision a0b0f6c7d7f29f1ade9ec59699d02e3b153ee8e4) !
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 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
55 	CHAN2G(1, 2412, 0),
56 	CHAN2G(2, 2417, 0),
57 	CHAN2G(3, 2422, 0),
58 	CHAN2G(4, 2427, 0),
59 	CHAN2G(5, 2432, 0),
60 	CHAN2G(6, 2437, 0),
61 	CHAN2G(7, 2442, 0),
62 	CHAN2G(8, 2447, 0),
63 	CHAN2G(9, 2452, 0),
64 	CHAN2G(10, 2457, 0),
65 	CHAN2G(11, 2462, 0),
66 	CHAN2G(12, 2467, 0),
67 	CHAN2G(13, 2472, 0),
68 	CHAN2G(14, 2484, 0),
69 };
70 
71 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
72 	CHAN5G(36, 5180, 0),
73 	CHAN5G(40, 5200, 0),
74 	CHAN5G(44, 5220, 0),
75 	CHAN5G(48, 5240, 0),
76 	CHAN5G(52, 5260, 0),
77 	CHAN5G(56, 5280, 0),
78 	CHAN5G(60, 5300, 0),
79 	CHAN5G(64, 5320, 0),
80 	CHAN5G(100, 5500, 0),
81 	CHAN5G(104, 5520, 0),
82 	CHAN5G(108, 5540, 0),
83 	CHAN5G(112, 5560, 0),
84 	CHAN5G(116, 5580, 0),
85 	CHAN5G(120, 5600, 0),
86 	CHAN5G(124, 5620, 0),
87 	CHAN5G(128, 5640, 0),
88 	CHAN5G(132, 5660, 0),
89 	CHAN5G(136, 5680, 0),
90 	CHAN5G(140, 5700, 0),
91 	CHAN5G(144, 5720, 0),
92 	CHAN5G(149, 5745, 0),
93 	CHAN5G(153, 5765, 0),
94 	CHAN5G(157, 5785, 0),
95 	CHAN5G(161, 5805, 0),
96 	CHAN5G(165, 5825, 0),
97 	CHAN5G(169, 5845, 0),
98 	CHAN5G(173, 5865, 0),
99 };
100 
101 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
102 	/* Operating Class 136 */
103 	CHAN6G(2, 5935, 0),
104 
105 	/* Operating Classes 131-135 */
106 	CHAN6G(1, 5955, 0),
107 	CHAN6G(5, 5975, 0),
108 	CHAN6G(9, 5995, 0),
109 	CHAN6G(13, 6015, 0),
110 	CHAN6G(17, 6035, 0),
111 	CHAN6G(21, 6055, 0),
112 	CHAN6G(25, 6075, 0),
113 	CHAN6G(29, 6095, 0),
114 	CHAN6G(33, 6115, 0),
115 	CHAN6G(37, 6135, 0),
116 	CHAN6G(41, 6155, 0),
117 	CHAN6G(45, 6175, 0),
118 	CHAN6G(49, 6195, 0),
119 	CHAN6G(53, 6215, 0),
120 	CHAN6G(57, 6235, 0),
121 	CHAN6G(61, 6255, 0),
122 	CHAN6G(65, 6275, 0),
123 	CHAN6G(69, 6295, 0),
124 	CHAN6G(73, 6315, 0),
125 	CHAN6G(77, 6335, 0),
126 	CHAN6G(81, 6355, 0),
127 	CHAN6G(85, 6375, 0),
128 	CHAN6G(89, 6395, 0),
129 	CHAN6G(93, 6415, 0),
130 	CHAN6G(97, 6435, 0),
131 	CHAN6G(101, 6455, 0),
132 	CHAN6G(105, 6475, 0),
133 	CHAN6G(109, 6495, 0),
134 	CHAN6G(113, 6515, 0),
135 	CHAN6G(117, 6535, 0),
136 	CHAN6G(121, 6555, 0),
137 	CHAN6G(125, 6575, 0),
138 	CHAN6G(129, 6595, 0),
139 	CHAN6G(133, 6615, 0),
140 	CHAN6G(137, 6635, 0),
141 	CHAN6G(141, 6655, 0),
142 	CHAN6G(145, 6675, 0),
143 	CHAN6G(149, 6695, 0),
144 	CHAN6G(153, 6715, 0),
145 	CHAN6G(157, 6735, 0),
146 	CHAN6G(161, 6755, 0),
147 	CHAN6G(165, 6775, 0),
148 	CHAN6G(169, 6795, 0),
149 	CHAN6G(173, 6815, 0),
150 	CHAN6G(177, 6835, 0),
151 	CHAN6G(181, 6855, 0),
152 	CHAN6G(185, 6875, 0),
153 	CHAN6G(189, 6895, 0),
154 	CHAN6G(193, 6915, 0),
155 	CHAN6G(197, 6935, 0),
156 	CHAN6G(201, 6955, 0),
157 	CHAN6G(205, 6975, 0),
158 	CHAN6G(209, 6995, 0),
159 	CHAN6G(213, 7015, 0),
160 	CHAN6G(217, 7035, 0),
161 	CHAN6G(221, 7055, 0),
162 	CHAN6G(225, 7075, 0),
163 	CHAN6G(229, 7095, 0),
164 	CHAN6G(233, 7115, 0),
165 };
166 
167 #define ATH12K_MAC_RATE_A_M(bps, code) \
168 	{ .bitrate = (bps), .hw_value = (code),\
169 	  .flags = IEEE80211_RATE_MANDATORY_A }
170 
171 #define ATH12K_MAC_RATE_B(bps, code, code_short) \
172 	{ .bitrate = (bps), .hw_value = (code), .hw_value_short = (code_short),\
173 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE }
174 
175 static struct ieee80211_rate ath12k_legacy_rates[] = {
176 	{ .bitrate = 10,
177 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
178 	ATH12K_MAC_RATE_B(20, ATH12K_HW_RATE_CCK_LP_2M,
179 			  ATH12K_HW_RATE_CCK_SP_2M),
180 	ATH12K_MAC_RATE_B(55, ATH12K_HW_RATE_CCK_LP_5_5M,
181 			  ATH12K_HW_RATE_CCK_SP_5_5M),
182 	ATH12K_MAC_RATE_B(110, ATH12K_HW_RATE_CCK_LP_11M,
183 			  ATH12K_HW_RATE_CCK_SP_11M),
184 	ATH12K_MAC_RATE_A_M(60, ATH12K_HW_RATE_OFDM_6M),
185 	ATH12K_MAC_RATE_A_M(90, ATH12K_HW_RATE_OFDM_9M),
186 	ATH12K_MAC_RATE_A_M(120, ATH12K_HW_RATE_OFDM_12M),
187 	ATH12K_MAC_RATE_A_M(180, ATH12K_HW_RATE_OFDM_18M),
188 	ATH12K_MAC_RATE_A_M(240, ATH12K_HW_RATE_OFDM_24M),
189 	ATH12K_MAC_RATE_A_M(360, ATH12K_HW_RATE_OFDM_36M),
190 	ATH12K_MAC_RATE_A_M(480, ATH12K_HW_RATE_OFDM_48M),
191 	ATH12K_MAC_RATE_A_M(540, ATH12K_HW_RATE_OFDM_54M),
192 };
193 
194 static const int
195 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
196 	[NL80211_BAND_2GHZ] = {
197 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
198 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
199 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
200 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
201 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
202 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
203 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
204 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
205 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
206 	},
207 	[NL80211_BAND_5GHZ] = {
208 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
209 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
210 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
211 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
212 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
213 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
214 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
215 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
216 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
217 	},
218 	[NL80211_BAND_6GHZ] = {
219 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
220 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
221 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
222 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
223 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
224 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
225 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
226 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
227 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
228 	},
229 
230 };
231 
232 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
233 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
234 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
235 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE |
236 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_START_USER_INFO,
237 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
238 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
239 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
240 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
241 			     HTT_RX_FP_CTRL_FILTER_FLASG3
242 };
243 
244 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
245 #define ath12k_g_rates ath12k_legacy_rates
246 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
247 #define ath12k_a_rates (ath12k_legacy_rates + 4)
248 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
249 
250 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
251 
252 static const u32 ath12k_smps_map[] = {
253 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
254 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
255 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
256 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
257 };
258 
259 static int ath12k_start_vdev_delay(struct ath12k *ar,
260 				   struct ath12k_link_vif *arvif);
261 static void ath12k_mac_stop(struct ath12k *ar);
262 static int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif);
263 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif);
264 
ath12k_mac_phymode_str(enum wmi_phy_mode mode)265 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
266 {
267 	switch (mode) {
268 	case MODE_11A:
269 		return "11a";
270 	case MODE_11G:
271 		return "11g";
272 	case MODE_11B:
273 		return "11b";
274 	case MODE_11GONLY:
275 		return "11gonly";
276 	case MODE_11NA_HT20:
277 		return "11na-ht20";
278 	case MODE_11NG_HT20:
279 		return "11ng-ht20";
280 	case MODE_11NA_HT40:
281 		return "11na-ht40";
282 	case MODE_11NG_HT40:
283 		return "11ng-ht40";
284 	case MODE_11AC_VHT20:
285 		return "11ac-vht20";
286 	case MODE_11AC_VHT40:
287 		return "11ac-vht40";
288 	case MODE_11AC_VHT80:
289 		return "11ac-vht80";
290 	case MODE_11AC_VHT160:
291 		return "11ac-vht160";
292 	case MODE_11AC_VHT80_80:
293 		return "11ac-vht80+80";
294 	case MODE_11AC_VHT20_2G:
295 		return "11ac-vht20-2g";
296 	case MODE_11AC_VHT40_2G:
297 		return "11ac-vht40-2g";
298 	case MODE_11AC_VHT80_2G:
299 		return "11ac-vht80-2g";
300 	case MODE_11AX_HE20:
301 		return "11ax-he20";
302 	case MODE_11AX_HE40:
303 		return "11ax-he40";
304 	case MODE_11AX_HE80:
305 		return "11ax-he80";
306 	case MODE_11AX_HE80_80:
307 		return "11ax-he80+80";
308 	case MODE_11AX_HE160:
309 		return "11ax-he160";
310 	case MODE_11AX_HE20_2G:
311 		return "11ax-he20-2g";
312 	case MODE_11AX_HE40_2G:
313 		return "11ax-he40-2g";
314 	case MODE_11AX_HE80_2G:
315 		return "11ax-he80-2g";
316 	case MODE_11BE_EHT20:
317 		return "11be-eht20";
318 	case MODE_11BE_EHT40:
319 		return "11be-eht40";
320 	case MODE_11BE_EHT80:
321 		return "11be-eht80";
322 	case MODE_11BE_EHT80_80:
323 		return "11be-eht80+80";
324 	case MODE_11BE_EHT160:
325 		return "11be-eht160";
326 	case MODE_11BE_EHT160_160:
327 		return "11be-eht160+160";
328 	case MODE_11BE_EHT320:
329 		return "11be-eht320";
330 	case MODE_11BE_EHT20_2G:
331 		return "11be-eht20-2g";
332 	case MODE_11BE_EHT40_2G:
333 		return "11be-eht40-2g";
334 	case MODE_UNKNOWN:
335 		/* skip */
336 		break;
337 
338 		/* no default handler to allow compiler to check that the
339 		 * enum is fully handled
340 		 */
341 	}
342 
343 	return "<unknown>";
344 }
345 
ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)346 u16 ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)
347 {
348 	switch (tones) {
349 	case 26:
350 		return RU_26;
351 	case 52:
352 		return RU_52;
353 	case 106:
354 		return RU_106;
355 	case 242:
356 		return RU_242;
357 	case 484:
358 		return RU_484;
359 	case 996:
360 		return RU_996;
361 	case (996 * 2):
362 		return RU_2X996;
363 	default:
364 		return RU_26;
365 	}
366 }
367 EXPORT_SYMBOL(ath12k_mac_he_convert_tones_to_ru_tones);
368 
ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)369 enum nl80211_eht_gi ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)
370 {
371 	switch (sgi) {
372 	case RX_MSDU_START_SGI_0_8_US:
373 		return NL80211_RATE_INFO_EHT_GI_0_8;
374 	case RX_MSDU_START_SGI_1_6_US:
375 		return NL80211_RATE_INFO_EHT_GI_1_6;
376 	case RX_MSDU_START_SGI_3_2_US:
377 		return NL80211_RATE_INFO_EHT_GI_3_2;
378 	default:
379 		return NL80211_RATE_INFO_EHT_GI_0_8;
380 	}
381 }
382 EXPORT_SYMBOL(ath12k_mac_eht_gi_to_nl80211_eht_gi);
383 
ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)384 enum nl80211_eht_ru_alloc ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)
385 {
386 	switch (ru_tones) {
387 	case 26:
388 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
389 	case 52:
390 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52;
391 	case (52 + 26):
392 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
393 	case 106:
394 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106;
395 	case (106 + 26):
396 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
397 	case 242:
398 		return NL80211_RATE_INFO_EHT_RU_ALLOC_242;
399 	case 484:
400 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484;
401 	case (484 + 242):
402 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
403 	case 996:
404 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996;
405 	case (996 + 484):
406 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
407 	case (996 + 484 + 242):
408 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
409 	case (2 * 996):
410 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
411 	case (2 * 996 + 484):
412 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
413 	case (3 * 996):
414 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
415 	case (3 * 996 + 484):
416 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
417 	case (4 * 996):
418 		return NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
419 	default:
420 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
421 	}
422 }
423 EXPORT_SYMBOL(ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc);
424 
425 enum rate_info_bw
ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)426 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
427 {
428 	u8 ret = RATE_INFO_BW_20;
429 
430 	switch (bw) {
431 	case ATH12K_BW_20:
432 		ret = RATE_INFO_BW_20;
433 		break;
434 	case ATH12K_BW_40:
435 		ret = RATE_INFO_BW_40;
436 		break;
437 	case ATH12K_BW_80:
438 		ret = RATE_INFO_BW_80;
439 		break;
440 	case ATH12K_BW_160:
441 		ret = RATE_INFO_BW_160;
442 		break;
443 	case ATH12K_BW_320:
444 		ret = RATE_INFO_BW_320;
445 		break;
446 	}
447 
448 	return ret;
449 }
450 EXPORT_SYMBOL(ath12k_mac_bw_to_mac80211_bw);
451 
ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)452 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
453 {
454 	switch (bw) {
455 	case RATE_INFO_BW_20:
456 		return ATH12K_BW_20;
457 	case RATE_INFO_BW_40:
458 		return ATH12K_BW_40;
459 	case RATE_INFO_BW_80:
460 		return ATH12K_BW_80;
461 	case RATE_INFO_BW_160:
462 		return ATH12K_BW_160;
463 	case RATE_INFO_BW_320:
464 		return ATH12K_BW_320;
465 	default:
466 		return ATH12K_BW_20;
467 	}
468 }
469 
ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc,u8 preamble,u8 * rateidx,u16 * rate)470 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
471 					  u16 *rate)
472 {
473 	/* As default, it is OFDM rates */
474 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
475 	int max_rates_idx = ath12k_g_rates_size;
476 
477 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
478 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
479 		i = 0;
480 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
481 	}
482 
483 	while (i < max_rates_idx) {
484 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
485 			*rateidx = i;
486 			*rate = ath12k_legacy_rates[i].bitrate;
487 			return 0;
488 		}
489 		i++;
490 	}
491 
492 	return -EINVAL;
493 }
494 EXPORT_SYMBOL(ath12k_mac_hw_ratecode_to_legacy_rate);
495 
ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band * sband,u32 bitrate)496 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
497 			     u32 bitrate)
498 {
499 	int i;
500 
501 	for (i = 0; i < sband->n_bitrates; i++)
502 		if (sband->bitrates[i].bitrate == bitrate)
503 			return i;
504 
505 	return 0;
506 }
507 
508 static u32
ath12k_mac_max_ht_nss(const u8 * ht_mcs_mask)509 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
510 {
511 	int nss;
512 
513 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
514 		if (ht_mcs_mask[nss])
515 			return nss + 1;
516 
517 	return 1;
518 }
519 
520 static u32
ath12k_mac_max_vht_nss(const u16 * vht_mcs_mask)521 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
522 {
523 	int nss;
524 
525 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
526 		if (vht_mcs_mask[nss])
527 			return nss + 1;
528 
529 	return 1;
530 }
531 
532 static u32
ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])533 ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
534 {
535 	int nss;
536 
537 	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
538 		if (he_mcs_mask[nss])
539 			return nss + 1;
540 
541 	return 1;
542 }
543 
544 static u32
ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])545 ath12k_mac_max_eht_nss(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
546 {
547 	int nss;
548 
549 	for (nss = NL80211_EHT_NSS_MAX - 1; nss >= 0; nss--)
550 		if (eht_mcs_mask[nss])
551 			return nss + 1;
552 
553 	return 1;
554 }
555 
556 static u32
ath12k_mac_max_eht_mcs_nss(const u8 * eht_mcs,int eht_mcs_set_size)557 ath12k_mac_max_eht_mcs_nss(const u8 *eht_mcs, int eht_mcs_set_size)
558 {
559 	int i;
560 	u8 nss = 0;
561 
562 	for (i = 0; i < eht_mcs_set_size; i++)
563 		nss = max(nss, u8_get_bits(eht_mcs[i], IEEE80211_EHT_MCS_NSS_RX));
564 
565 	return nss;
566 }
567 
ath12k_parse_mpdudensity(u8 mpdudensity)568 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
569 {
570 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
571  *   0 for no restriction
572  *   1 for 1/4 us
573  *   2 for 1/2 us
574  *   3 for 1 us
575  *   4 for 2 us
576  *   5 for 4 us
577  *   6 for 8 us
578  *   7 for 16 us
579  */
580 	switch (mpdudensity) {
581 	case 0:
582 		return 0;
583 	case 1:
584 	case 2:
585 	case 3:
586 	/* Our lower layer calculations limit our precision to
587 	 * 1 microsecond
588 	 */
589 		return 1;
590 	case 4:
591 		return 2;
592 	case 5:
593 		return 4;
594 	case 6:
595 		return 8;
596 	case 7:
597 		return 16;
598 	default:
599 		return 0;
600 	}
601 }
602 
ath12k_mac_vif_link_chan(struct ieee80211_vif * vif,u8 link_id,struct cfg80211_chan_def * def)603 static int ath12k_mac_vif_link_chan(struct ieee80211_vif *vif, u8 link_id,
604 				    struct cfg80211_chan_def *def)
605 {
606 	struct ieee80211_bss_conf *link_conf;
607 	struct ieee80211_chanctx_conf *conf;
608 
609 	rcu_read_lock();
610 	link_conf = rcu_dereference(vif->link_conf[link_id]);
611 
612 	if (!link_conf) {
613 		rcu_read_unlock();
614 		return -ENOLINK;
615 	}
616 
617 	conf = rcu_dereference(link_conf->chanctx_conf);
618 	if (!conf) {
619 		rcu_read_unlock();
620 		return -ENOENT;
621 	}
622 	*def = conf->def;
623 	rcu_read_unlock();
624 
625 	return 0;
626 }
627 
628 static struct ath12k_link_vif *
ath12k_mac_get_tx_arvif(struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * link_conf)629 ath12k_mac_get_tx_arvif(struct ath12k_link_vif *arvif,
630 			struct ieee80211_bss_conf *link_conf)
631 {
632 	struct ieee80211_bss_conf *tx_bss_conf;
633 	struct ath12k *ar = arvif->ar;
634 	struct ath12k_vif *tx_ahvif;
635 
636 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
637 
638 	tx_bss_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
639 					link_conf->tx_bss_conf);
640 	if (tx_bss_conf) {
641 		tx_ahvif = ath12k_vif_to_ahvif(tx_bss_conf->vif);
642 		return wiphy_dereference(tx_ahvif->ah->hw->wiphy,
643 					 tx_ahvif->link[tx_bss_conf->link_id]);
644 	}
645 
646 	return NULL;
647 }
648 
ath12k_mac_get_tx_bssid(struct ath12k_link_vif * arvif)649 static const u8 *ath12k_mac_get_tx_bssid(struct ath12k_link_vif *arvif)
650 {
651 	struct ieee80211_bss_conf *link_conf;
652 	struct ath12k_link_vif *tx_arvif;
653 	struct ath12k *ar = arvif->ar;
654 
655 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
656 
657 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
658 	if (!link_conf) {
659 		ath12k_warn(ar->ab,
660 			    "unable to access bss link conf for link %u required to retrieve transmitting link conf\n",
661 			    arvif->link_id);
662 		return NULL;
663 	}
664 	if (link_conf->vif->type == NL80211_IFTYPE_STATION) {
665 		if (link_conf->nontransmitted)
666 			return link_conf->transmitter_bssid;
667 	} else {
668 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
669 		if (tx_arvif)
670 			return tx_arvif->bssid;
671 	}
672 
673 	return NULL;
674 }
675 
676 struct ieee80211_bss_conf *
ath12k_mac_get_link_bss_conf(struct ath12k_link_vif * arvif)677 ath12k_mac_get_link_bss_conf(struct ath12k_link_vif *arvif)
678 {
679 	struct ieee80211_vif *vif = arvif->ahvif->vif;
680 	struct ieee80211_bss_conf *link_conf;
681 	struct ath12k *ar = arvif->ar;
682 
683 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
684 
685 	if (arvif->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
686 		return NULL;
687 
688 	link_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
689 				      vif->link_conf[arvif->link_id]);
690 
691 	return link_conf;
692 }
693 
ath12k_mac_get_link_sta(struct ath12k_link_sta * arsta)694 static struct ieee80211_link_sta *ath12k_mac_get_link_sta(struct ath12k_link_sta *arsta)
695 {
696 	struct ath12k_sta *ahsta = arsta->ahsta;
697 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
698 	struct ieee80211_link_sta *link_sta;
699 
700 	lockdep_assert_wiphy(ahsta->ahvif->ah->hw->wiphy);
701 
702 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
703 		return NULL;
704 
705 	link_sta = wiphy_dereference(ahsta->ahvif->ah->hw->wiphy,
706 				     sta->link[arsta->link_id]);
707 
708 	return link_sta;
709 }
710 
ath12k_mac_bitrate_is_cck(int bitrate)711 static bool ath12k_mac_bitrate_is_cck(int bitrate)
712 {
713 	switch (bitrate) {
714 	case 10:
715 	case 20:
716 	case 55:
717 	case 110:
718 		return true;
719 	}
720 
721 	return false;
722 }
723 
ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band * sband,u8 hw_rate,bool cck)724 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
725 			     u8 hw_rate, bool cck)
726 {
727 	const struct ieee80211_rate *rate;
728 	int i;
729 
730 	for (i = 0; i < sband->n_bitrates; i++) {
731 		rate = &sband->bitrates[i];
732 
733 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
734 			continue;
735 
736 		/* To handle 802.11a PPDU type */
737 		if ((!cck) && (rate->hw_value == hw_rate) &&
738 		    (rate->flags & IEEE80211_RATE_MANDATORY_A))
739 			return i;
740 		/* To handle 802.11b short PPDU type */
741 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
742 			 rate->hw_value_short == hw_rate)
743 			return i;
744 		/* To handle 802.11b long PPDU type */
745 		else if (rate->hw_value == hw_rate)
746 			return i;
747 	}
748 
749 	return 0;
750 }
751 
ath12k_mac_bitrate_to_rate(int bitrate)752 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
753 {
754 	return DIV_ROUND_UP(bitrate, 5) |
755 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
756 }
757 
ath12k_get_arvif_iter(void * data,u8 * mac,struct ieee80211_vif * vif)758 static void ath12k_get_arvif_iter(void *data, u8 *mac,
759 				  struct ieee80211_vif *vif)
760 {
761 	struct ath12k_vif_iter *arvif_iter = data;
762 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
763 	unsigned long links_map = ahvif->links_map;
764 	struct ath12k_link_vif *arvif;
765 	u8 link_id;
766 
767 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
768 		arvif = rcu_dereference(ahvif->link[link_id]);
769 
770 		if (WARN_ON(!arvif))
771 			continue;
772 
773 		if (!arvif->is_created)
774 			continue;
775 
776 		if (arvif->vdev_id == arvif_iter->vdev_id &&
777 		    arvif->ar == arvif_iter->ar) {
778 			arvif_iter->arvif = arvif;
779 			break;
780 		}
781 	}
782 }
783 
ath12k_mac_get_arvif(struct ath12k * ar,u32 vdev_id)784 struct ath12k_link_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
785 {
786 	struct ath12k_vif_iter arvif_iter = {};
787 	u32 flags;
788 
789 	/* To use the arvif returned, caller must have held rcu read lock.
790 	 */
791 	WARN_ON(!rcu_read_lock_any_held());
792 	arvif_iter.vdev_id = vdev_id;
793 	arvif_iter.ar = ar;
794 
795 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
796 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
797 						   flags,
798 						   ath12k_get_arvif_iter,
799 						   &arvif_iter);
800 	if (!arvif_iter.arvif) {
801 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
802 		return NULL;
803 	}
804 
805 	return arvif_iter.arvif;
806 }
807 
ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base * ab,u32 vdev_id)808 struct ath12k_link_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
809 							u32 vdev_id)
810 {
811 	int i;
812 	struct ath12k_pdev *pdev;
813 	struct ath12k_link_vif *arvif;
814 
815 	for (i = 0; i < ab->num_radios; i++) {
816 		pdev = rcu_dereference(ab->pdevs_active[i]);
817 		if (pdev && pdev->ar &&
818 		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
819 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
820 			if (arvif)
821 				return arvif;
822 		}
823 	}
824 
825 	return NULL;
826 }
827 
ath12k_mac_get_ar_by_vdev_id(struct ath12k_base * ab,u32 vdev_id)828 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
829 {
830 	int i;
831 	struct ath12k_pdev *pdev;
832 
833 	for (i = 0; i < ab->num_radios; i++) {
834 		pdev = rcu_dereference(ab->pdevs_active[i]);
835 		if (pdev && pdev->ar) {
836 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
837 				return pdev->ar;
838 		}
839 	}
840 
841 	return NULL;
842 }
843 
ath12k_mac_get_ar_by_pdev_id(struct ath12k_base * ab,u32 pdev_id)844 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
845 {
846 	int i;
847 	struct ath12k_pdev *pdev;
848 
849 	if (ab->hw_params->single_pdev_only) {
850 		pdev = rcu_dereference(ab->pdevs_active[0]);
851 		return pdev ? pdev->ar : NULL;
852 	}
853 
854 	if (WARN_ON(pdev_id > ab->num_radios))
855 		return NULL;
856 
857 	for (i = 0; i < ab->num_radios; i++) {
858 		if (ab->fw_mode == ATH12K_FIRMWARE_MODE_FTM)
859 			pdev = &ab->pdevs[i];
860 		else
861 			pdev = rcu_dereference(ab->pdevs_active[i]);
862 
863 		if (pdev && pdev->pdev_id == pdev_id)
864 			return (pdev->ar ? pdev->ar : NULL);
865 	}
866 
867 	return NULL;
868 }
869 
ath12k_mac_is_ml_arvif(struct ath12k_link_vif * arvif)870 static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
871 {
872 	struct ath12k_vif *ahvif = arvif->ahvif;
873 
874 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
875 
876 	if (ahvif->vif->valid_links & BIT(arvif->link_id))
877 		return true;
878 
879 	return false;
880 }
881 
ath12k_mac_get_ar_by_chan(struct ieee80211_hw * hw,struct ieee80211_channel * channel)882 static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
883 						struct ieee80211_channel *channel)
884 {
885 	struct ath12k_hw *ah = hw->priv;
886 	struct ath12k *ar;
887 	int i;
888 
889 	ar = ah->radio;
890 
891 	if (ah->num_radio == 1)
892 		return ar;
893 
894 	for_each_ar(ah, ar, i) {
895 		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
896 		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
897 			return ar;
898 	}
899 	return NULL;
900 }
901 
ath12k_get_ar_by_ctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)902 static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
903 					   struct ieee80211_chanctx_conf *ctx)
904 {
905 	if (!ctx)
906 		return NULL;
907 
908 	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
909 }
910 
ath12k_get_ar_by_vif(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 link_id)911 struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
912 				    struct ieee80211_vif *vif,
913 				    u8 link_id)
914 {
915 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
916 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
917 	struct ath12k_link_vif *arvif;
918 
919 	lockdep_assert_wiphy(hw->wiphy);
920 
921 	/* If there is one pdev within ah, then we return
922 	 * ar directly.
923 	 */
924 	if (ah->num_radio == 1)
925 		return ah->radio;
926 
927 	if (!(ahvif->links_map & BIT(link_id)))
928 		return NULL;
929 
930 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
931 	if (arvif && arvif->is_created)
932 		return arvif->ar;
933 
934 	return NULL;
935 }
936 
ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf,void * data)937 void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
938 					  struct ieee80211_chanctx_conf *conf,
939 					  void *data)
940 {
941 	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
942 	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
943 
944 	if (ctx_ar == arg->ar)
945 		arg->chanctx_conf = conf;
946 }
947 
ath12k_mac_get_vif_up(struct ath12k * ar)948 static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
949 {
950 	struct ath12k_link_vif *arvif;
951 
952 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
953 
954 	list_for_each_entry(arvif, &ar->arvifs, list) {
955 		if (arvif->is_up)
956 			return arvif;
957 	}
958 
959 	return NULL;
960 }
961 
ath12k_mac_band_match(enum nl80211_band band1,enum WMI_HOST_WLAN_BAND band2)962 static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
963 {
964 	switch (band1) {
965 	case NL80211_BAND_2GHZ:
966 		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
967 			return true;
968 		break;
969 	case NL80211_BAND_5GHZ:
970 	case NL80211_BAND_6GHZ:
971 		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
972 			return true;
973 		break;
974 	default:
975 		return false;
976 	}
977 
978 	return false;
979 }
980 
ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif * arvif)981 static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
982 {
983 	struct ath12k *ar = arvif->ar;
984 	struct ath12k_base *ab = ar->ab;
985 	struct ieee80211_vif *vif = arvif->ahvif->vif;
986 	struct cfg80211_chan_def def;
987 	enum nl80211_band band;
988 	u8 pdev_id = ab->fw_pdev[0].pdev_id;
989 	int i;
990 
991 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
992 		return pdev_id;
993 
994 	band = def.chan->band;
995 
996 	for (i = 0; i < ab->fw_pdev_count; i++) {
997 		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
998 			return ab->fw_pdev[i].pdev_id;
999 	}
1000 
1001 	return pdev_id;
1002 }
1003 
ath12k_mac_get_target_pdev_id(struct ath12k * ar)1004 u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
1005 {
1006 	struct ath12k_link_vif *arvif;
1007 	struct ath12k_base *ab = ar->ab;
1008 
1009 	if (!ab->hw_params->single_pdev_only)
1010 		return ar->pdev->pdev_id;
1011 
1012 	arvif = ath12k_mac_get_vif_up(ar);
1013 
1014 	/* fw_pdev array has pdev ids derived from phy capability
1015 	 * service ready event (pdev_and_hw_link_ids).
1016 	 * If no vif is active, return default first index.
1017 	 */
1018 	if (!arvif)
1019 		return ar->ab->fw_pdev[0].pdev_id;
1020 
1021 	/* If active vif is found, return the pdev id matching chandef band */
1022 	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
1023 }
1024 
ath12k_pdev_caps_update(struct ath12k * ar)1025 static void ath12k_pdev_caps_update(struct ath12k *ar)
1026 {
1027 	struct ath12k_base *ab = ar->ab;
1028 
1029 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
1030 
1031 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
1032 	 * But since the received value in svcrdy is same as hw_max_tx_power,
1033 	 * we can set ar->min_tx_power to 0 currently until
1034 	 * this is fixed in firmware
1035 	 */
1036 	ar->min_tx_power = 0;
1037 
1038 	ar->txpower_limit_2g = ar->max_tx_power;
1039 	ar->txpower_limit_5g = ar->max_tx_power;
1040 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
1041 }
1042 
ath12k_mac_txpower_recalc(struct ath12k * ar)1043 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
1044 {
1045 	struct ath12k_pdev *pdev = ar->pdev;
1046 	struct ath12k_link_vif *arvif;
1047 	int ret, txpower = -1;
1048 	u32 param;
1049 
1050 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1051 
1052 	list_for_each_entry(arvif, &ar->arvifs, list) {
1053 		if (arvif->txpower <= 0)
1054 			continue;
1055 
1056 		if (txpower == -1)
1057 			txpower = arvif->txpower;
1058 		else
1059 			txpower = min(txpower, arvif->txpower);
1060 	}
1061 
1062 	if (txpower == -1)
1063 		return 0;
1064 
1065 	/* txpwr is set as 2 units per dBm in FW*/
1066 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
1067 			ar->max_tx_power) * 2;
1068 
1069 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
1070 		   txpower / 2);
1071 
1072 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
1073 	    ar->txpower_limit_2g != txpower) {
1074 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
1075 		ret = ath12k_wmi_pdev_set_param(ar, param,
1076 						txpower, ar->pdev->pdev_id);
1077 		if (ret)
1078 			goto fail;
1079 		ar->txpower_limit_2g = txpower;
1080 	}
1081 
1082 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
1083 	    ar->txpower_limit_5g != txpower) {
1084 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
1085 		ret = ath12k_wmi_pdev_set_param(ar, param,
1086 						txpower, ar->pdev->pdev_id);
1087 		if (ret)
1088 			goto fail;
1089 		ar->txpower_limit_5g = txpower;
1090 	}
1091 
1092 	return 0;
1093 
1094 fail:
1095 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
1096 		    txpower / 2, param, ret);
1097 	return ret;
1098 }
1099 
ath12k_recalc_rtscts_prot(struct ath12k_link_vif * arvif)1100 static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
1101 {
1102 	struct ath12k *ar = arvif->ar;
1103 	u32 vdev_param, rts_cts;
1104 	int ret;
1105 
1106 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1107 
1108 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
1109 
1110 	/* Enable RTS/CTS protection for sw retries (when legacy stations
1111 	 * are in BSS) or by default only for second rate series.
1112 	 * TODO: Check if we need to enable CTS 2 Self in any case
1113 	 */
1114 	rts_cts = WMI_USE_RTS_CTS;
1115 
1116 	if (arvif->num_legacy_stations > 0)
1117 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
1118 	else
1119 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
1120 
1121 	/* Need not send duplicate param value to firmware */
1122 	if (arvif->rtscts_prot_mode == rts_cts)
1123 		return 0;
1124 
1125 	arvif->rtscts_prot_mode = rts_cts;
1126 
1127 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1128 		   arvif->vdev_id, rts_cts);
1129 
1130 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1131 					    vdev_param, rts_cts);
1132 	if (ret)
1133 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1134 			    arvif->vdev_id, ret);
1135 
1136 	return ret;
1137 }
1138 
ath12k_mac_set_kickout(struct ath12k_link_vif * arvif)1139 static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
1140 {
1141 	struct ath12k *ar = arvif->ar;
1142 	u32 param;
1143 	int ret;
1144 
1145 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
1146 					ATH12K_KICKOUT_THRESHOLD,
1147 					ar->pdev->pdev_id);
1148 	if (ret) {
1149 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
1150 			    arvif->vdev_id, ret);
1151 		return ret;
1152 	}
1153 
1154 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
1155 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1156 					    ATH12K_KEEPALIVE_MIN_IDLE);
1157 	if (ret) {
1158 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
1159 			    arvif->vdev_id, ret);
1160 		return ret;
1161 	}
1162 
1163 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
1164 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1165 					    ATH12K_KEEPALIVE_MAX_IDLE);
1166 	if (ret) {
1167 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
1168 			    arvif->vdev_id, ret);
1169 		return ret;
1170 	}
1171 
1172 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
1173 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1174 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
1175 	if (ret) {
1176 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
1177 			    arvif->vdev_id, ret);
1178 		return ret;
1179 	}
1180 
1181 	return 0;
1182 }
1183 
ath12k_mac_link_sta_rhash_cleanup(void * data,struct ieee80211_sta * sta)1184 static void ath12k_mac_link_sta_rhash_cleanup(void *data, struct ieee80211_sta *sta)
1185 {
1186 	u8 link_id;
1187 	unsigned long links_map;
1188 	struct ath12k_sta *ahsta;
1189 	struct ath12k *ar = data;
1190 	struct ath12k_link_sta *arsta;
1191 	struct ath12k_link_vif *arvif;
1192 	struct ath12k_base *ab = ar->ab;
1193 
1194 	ahsta = ath12k_sta_to_ahsta(sta);
1195 	links_map = ahsta->links_map;
1196 
1197 	rcu_read_lock();
1198 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
1199 		arsta = rcu_dereference(ahsta->link[link_id]);
1200 		if (!arsta)
1201 			continue;
1202 		arvif = arsta->arvif;
1203 		if (!(arvif->ar == ar))
1204 			continue;
1205 
1206 		spin_lock_bh(&ab->base_lock);
1207 		ath12k_link_sta_rhash_delete(ab, arsta);
1208 		spin_unlock_bh(&ab->base_lock);
1209 	}
1210 	rcu_read_unlock();
1211 }
1212 
ath12k_mac_peer_cleanup_all(struct ath12k * ar)1213 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
1214 {
1215 	struct ath12k_dp_link_peer *peer, *tmp;
1216 	struct ath12k_base *ab = ar->ab;
1217 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
1218 	struct ath12k_link_vif *arvif, *tmp_vif;
1219 	struct ath12k_dp_hw *dp_hw = &ar->ah->dp_hw;
1220 	struct ath12k_dp_peer *dp_peer = NULL;
1221 	u16 peerid_index;
1222 	struct list_head peers;
1223 
1224 	INIT_LIST_HEAD(&peers);
1225 
1226 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1227 
1228 	spin_lock_bh(&dp->dp_lock);
1229 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
1230 		/* Skip Rx TID cleanup for self peer */
1231 		if (peer->sta && peer->dp_peer)
1232 			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
1233 
1234 		/* cleanup dp peer */
1235 		spin_lock_bh(&dp_hw->peer_lock);
1236 		dp_peer = peer->dp_peer;
1237 		peerid_index = ath12k_dp_peer_get_peerid_index(dp, peer->peer_id);
1238 		rcu_assign_pointer(dp_peer->link_peers[peer->link_id], NULL);
1239 		rcu_assign_pointer(dp_hw->dp_peers[peerid_index], NULL);
1240 		spin_unlock_bh(&dp_hw->peer_lock);
1241 
1242 		ath12k_dp_link_peer_rhash_delete(dp, peer);
1243 
1244 		list_move(&peer->list, &peers);
1245 	}
1246 	spin_unlock_bh(&dp->dp_lock);
1247 
1248 	synchronize_rcu();
1249 
1250 	list_for_each_entry_safe(peer, tmp, &peers, list) {
1251 		ath12k_dp_link_peer_free(peer);
1252 	}
1253 
1254 	ar->num_peers = 0;
1255 	ar->num_stations = 0;
1256 
1257 	/* Cleanup rhash table maintained for arsta by iterating over sta */
1258 	ieee80211_iterate_stations_mtx(ar->ah->hw, ath12k_mac_link_sta_rhash_cleanup,
1259 				       ar);
1260 
1261 	/* Delete all the self dp_peers on asserted radio */
1262 	list_for_each_entry_safe_reverse(arvif, tmp_vif, &ar->arvifs, list) {
1263 		if ((arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
1264 		    (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)) {
1265 			ath12k_dp_peer_delete(dp_hw, arvif->bssid, NULL);
1266 			arvif->num_stations = 0;
1267 		}
1268 	}
1269 }
1270 
ath12k_mac_dp_peer_cleanup(struct ath12k_hw * ah)1271 void ath12k_mac_dp_peer_cleanup(struct ath12k_hw *ah)
1272 {
1273 	struct list_head peers;
1274 	struct ath12k_dp_peer *dp_peer, *tmp;
1275 	struct ath12k_dp_hw *dp_hw = &ah->dp_hw;
1276 
1277 	INIT_LIST_HEAD(&peers);
1278 
1279 	spin_lock_bh(&dp_hw->peer_lock);
1280 	list_for_each_entry_safe(dp_peer, tmp, &dp_hw->dp_peers_list, list) {
1281 		if (dp_peer->is_mlo) {
1282 			rcu_assign_pointer(dp_hw->dp_peers[dp_peer->peer_id], NULL);
1283 			clear_bit(dp_peer->peer_id, ah->free_ml_peer_id_map);
1284 		}
1285 
1286 		list_move(&dp_peer->list, &peers);
1287 	}
1288 
1289 	spin_unlock_bh(&dp_hw->peer_lock);
1290 
1291 	synchronize_rcu();
1292 
1293 	list_for_each_entry_safe(dp_peer, tmp, &peers, list) {
1294 		list_del(&dp_peer->list);
1295 		kfree(dp_peer);
1296 	}
1297 }
1298 
ath12k_mac_vdev_setup_sync(struct ath12k * ar)1299 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1300 {
1301 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1302 
1303 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1304 		return -ESHUTDOWN;
1305 
1306 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1307 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1308 
1309 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1310 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1311 		return -ETIMEDOUT;
1312 
1313 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1314 }
1315 
ath12k_monitor_vdev_up(struct ath12k * ar,int vdev_id)1316 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1317 {
1318 	struct ath12k_wmi_vdev_up_params params = {};
1319 	int ret;
1320 
1321 	params.vdev_id = vdev_id;
1322 	params.bssid = ar->mac_addr;
1323 	ret = ath12k_wmi_vdev_up(ar, &params);
1324 	if (ret) {
1325 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1326 			    vdev_id, ret);
1327 		return ret;
1328 	}
1329 
1330 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1331 		   vdev_id);
1332 	return 0;
1333 }
1334 
ath12k_mac_monitor_vdev_start(struct ath12k * ar,int vdev_id,struct cfg80211_chan_def * chandef)1335 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1336 					 struct cfg80211_chan_def *chandef)
1337 {
1338 	struct ieee80211_channel *channel;
1339 	struct wmi_vdev_start_req_arg arg = {};
1340 	struct ath12k_wmi_vdev_up_params params = {};
1341 	int ret;
1342 
1343 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1344 
1345 	channel = chandef->chan;
1346 	arg.vdev_id = vdev_id;
1347 	arg.freq = channel->center_freq;
1348 	arg.band_center_freq1 = chandef->center_freq1;
1349 	arg.band_center_freq2 = chandef->center_freq2;
1350 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1351 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1352 
1353 	arg.min_power = 0;
1354 	arg.max_power = channel->max_power;
1355 	arg.max_reg_power = channel->max_reg_power;
1356 	arg.max_antenna_gain = channel->max_antenna_gain;
1357 
1358 	arg.pref_tx_streams = ar->num_tx_chains;
1359 	arg.pref_rx_streams = ar->num_rx_chains;
1360 	arg.punct_bitmap = 0xFFFFFFFF;
1361 
1362 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1363 
1364 	reinit_completion(&ar->vdev_setup_done);
1365 	reinit_completion(&ar->vdev_delete_done);
1366 
1367 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1368 	if (ret) {
1369 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1370 			    vdev_id, ret);
1371 		return ret;
1372 	}
1373 
1374 	ret = ath12k_mac_vdev_setup_sync(ar);
1375 	if (ret) {
1376 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1377 			    vdev_id, ret);
1378 		return ret;
1379 	}
1380 
1381 	params.vdev_id = vdev_id;
1382 	params.bssid = ar->mac_addr;
1383 	ret = ath12k_wmi_vdev_up(ar, &params);
1384 	if (ret) {
1385 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1386 			    vdev_id, ret);
1387 		goto vdev_stop;
1388 	}
1389 
1390 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1391 		   vdev_id);
1392 	return 0;
1393 
1394 vdev_stop:
1395 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1396 	if (ret)
1397 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1398 			    vdev_id, ret);
1399 	return ret;
1400 }
1401 
ath12k_mac_monitor_vdev_stop(struct ath12k * ar)1402 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1403 {
1404 	int ret;
1405 
1406 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1407 
1408 	reinit_completion(&ar->vdev_setup_done);
1409 
1410 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1411 	if (ret)
1412 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1413 			    ar->monitor_vdev_id, ret);
1414 
1415 	ret = ath12k_mac_vdev_setup_sync(ar);
1416 	if (ret)
1417 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1418 			    ar->monitor_vdev_id, ret);
1419 
1420 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1421 	if (ret)
1422 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1423 			    ar->monitor_vdev_id, ret);
1424 
1425 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1426 		   ar->monitor_vdev_id);
1427 	return ret;
1428 }
1429 
ath12k_mac_monitor_vdev_delete(struct ath12k * ar)1430 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1431 {
1432 	int ret;
1433 	unsigned long time_left;
1434 
1435 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1436 
1437 	if (!ar->monitor_vdev_created)
1438 		return 0;
1439 
1440 	reinit_completion(&ar->vdev_delete_done);
1441 
1442 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1443 	if (ret) {
1444 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1445 			    ar->monitor_vdev_id, ret);
1446 		return ret;
1447 	}
1448 
1449 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1450 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1451 	if (time_left == 0) {
1452 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1453 	} else {
1454 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1455 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1456 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1457 			   ar->monitor_vdev_id);
1458 		ar->num_created_vdevs--;
1459 		ar->monitor_vdev_id = -1;
1460 		ar->monitor_vdev_created = false;
1461 	}
1462 
1463 	return ret;
1464 }
1465 
ath12k_mac_monitor_start(struct ath12k * ar)1466 static int ath12k_mac_monitor_start(struct ath12k *ar)
1467 {
1468 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1469 	int ret;
1470 
1471 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1472 
1473 	if (ar->monitor_started)
1474 		return 0;
1475 
1476 	arg.ar = ar;
1477 	arg.chanctx_conf = NULL;
1478 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1479 					    ath12k_mac_get_any_chanctx_conf_iter,
1480 					    &arg);
1481 	if (!arg.chanctx_conf)
1482 		return 0;
1483 
1484 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1485 					    &arg.chanctx_conf->def);
1486 	if (ret) {
1487 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1488 		return ret;
1489 	}
1490 
1491 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1492 	if (ret) {
1493 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1494 		return ret;
1495 	}
1496 
1497 	ar->monitor_started = true;
1498 	ar->num_started_vdevs++;
1499 
1500 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1501 
1502 	return 0;
1503 }
1504 
ath12k_mac_monitor_stop(struct ath12k * ar)1505 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1506 {
1507 	int ret;
1508 
1509 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1510 
1511 	if (!ar->monitor_started)
1512 		return 0;
1513 
1514 	ret = ath12k_mac_monitor_vdev_stop(ar);
1515 	if (ret) {
1516 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1517 		return ret;
1518 	}
1519 
1520 	ar->monitor_started = false;
1521 	ar->num_started_vdevs--;
1522 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1523 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1524 	return ret;
1525 }
1526 
ath12k_mac_vdev_stop(struct ath12k_link_vif * arvif)1527 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1528 {
1529 	struct ath12k_vif *ahvif = arvif->ahvif;
1530 	struct ath12k *ar = arvif->ar;
1531 	int ret;
1532 
1533 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1534 
1535 	reinit_completion(&ar->vdev_setup_done);
1536 
1537 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1538 	if (ret) {
1539 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1540 			    arvif->vdev_id, ret);
1541 		goto err;
1542 	}
1543 
1544 	ret = ath12k_mac_vdev_setup_sync(ar);
1545 	if (ret) {
1546 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1547 			    arvif->vdev_id, ret);
1548 		goto err;
1549 	}
1550 
1551 	WARN_ON(ar->num_started_vdevs == 0);
1552 
1553 	ar->num_started_vdevs--;
1554 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1555 		   ahvif->vif->addr, arvif->vdev_id);
1556 
1557 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1558 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1559 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1560 			   arvif->vdev_id);
1561 	}
1562 
1563 	return 0;
1564 err:
1565 	return ret;
1566 }
1567 
ath12k_mac_op_config(struct ieee80211_hw * hw,int radio_idx,u32 changed)1568 int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1569 {
1570 	return 0;
1571 }
1572 EXPORT_SYMBOL(ath12k_mac_op_config);
1573 
ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif * arvif,struct sk_buff * bcn)1574 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1575 				       struct sk_buff *bcn)
1576 {
1577 	struct ath12k *ar = arvif->ar;
1578 	struct ieee80211_mgmt *mgmt;
1579 	const u8 *p2p_ie;
1580 	int ret;
1581 
1582 	mgmt = (void *)bcn->data;
1583 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1584 					 mgmt->u.beacon.variable,
1585 					 bcn->len - (mgmt->u.beacon.variable -
1586 						     bcn->data));
1587 	if (!p2p_ie) {
1588 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1589 		return -ENOENT;
1590 	}
1591 
1592 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1593 	if (ret) {
1594 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1595 			    arvif->vdev_id, ret);
1596 		return ret;
1597 	}
1598 
1599 	return 0;
1600 }
1601 
ath12k_mac_remove_vendor_ie(struct sk_buff * skb,unsigned int oui,u8 oui_type,size_t ie_offset)1602 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1603 				       u8 oui_type, size_t ie_offset)
1604 {
1605 	const u8 *next, *end;
1606 	size_t len;
1607 	u8 *ie;
1608 
1609 	if (WARN_ON(skb->len < ie_offset))
1610 		return -EINVAL;
1611 
1612 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1613 					   skb->data + ie_offset,
1614 					   skb->len - ie_offset);
1615 	if (!ie)
1616 		return -ENOENT;
1617 
1618 	len = ie[1] + 2;
1619 	end = skb->data + skb->len;
1620 	next = ie + len;
1621 
1622 	if (WARN_ON(next > end))
1623 		return -EINVAL;
1624 
1625 	memmove(ie, next, end - next);
1626 	skb_trim(skb, skb->len - len);
1627 
1628 	return 0;
1629 }
1630 
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)1631 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1632 				     struct ath12k_link_vif *tx_arvif,
1633 				     struct sk_buff *bcn,
1634 				     u8 bssid_index, bool *nontx_profile_found)
1635 {
1636 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1637 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1638 	const u8 *start, *tail;
1639 	u16 rem_len;
1640 	u8 i;
1641 
1642 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1643 	tail = skb_tail_pointer(bcn);
1644 	rem_len = tail - start;
1645 
1646 	arvif->rsnie_present = false;
1647 	arvif->wpaie_present = false;
1648 
1649 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1650 		arvif->rsnie_present = true;
1651 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1652 				    start, rem_len))
1653 		arvif->wpaie_present = true;
1654 
1655 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1656 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1657 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1658 		tx_arvif->beacon_prot = true;
1659 
1660 	/* Return from here for the transmitted profile */
1661 	if (!bssid_index)
1662 		return;
1663 
1664 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1665 	 * of the transmitted profile. It will be changed if security configurations are
1666 	 * different.
1667 	 */
1668 	*nontx_profile_found = false;
1669 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1670 		/* Fixed minimum MBSSID element length with at least one
1671 		 * nontransmitted BSSID profile is 12 bytes as given below;
1672 		 * 1 (max BSSID indicator) +
1673 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1674 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1675 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1676 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1677 		 */
1678 		if (elem->datalen < 12 || elem->data[0] < 1)
1679 			continue; /* Max BSSID indicator must be >=1 */
1680 
1681 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1682 			start = nontx->data;
1683 
1684 			if (nontx->id != 0 || nontx->datalen < 4)
1685 				continue; /* Invalid nontransmitted profile */
1686 
1687 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1688 			    nontx->data[1] != 2) {
1689 				continue; /* Missing nontransmitted BSS capabilities */
1690 			}
1691 
1692 			if (nontx->data[4] != WLAN_EID_SSID)
1693 				continue; /* Missing SSID for nontransmitted BSS */
1694 
1695 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1696 						   start, nontx->datalen);
1697 			if (!index || index->datalen < 1 || index->data[0] == 0)
1698 				continue; /* Invalid MBSSID Index element */
1699 
1700 			if (index->data[0] == bssid_index) {
1701 				*nontx_profile_found = true;
1702 
1703 				/* Check if nontx BSS has beacon protection enabled */
1704 				if (!tx_arvif->beacon_prot) {
1705 					ext_cap_ie =
1706 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1707 							       nontx->data,
1708 							       nontx->datalen);
1709 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1710 					    (ext_cap_ie->data[10] &
1711 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1712 						tx_arvif->beacon_prot = true;
1713 				}
1714 
1715 				if (cfg80211_find_ie(WLAN_EID_RSN,
1716 						     nontx->data,
1717 						     nontx->datalen)) {
1718 					arvif->rsnie_present = true;
1719 					return;
1720 				} else if (!arvif->rsnie_present) {
1721 					return; /* Both tx and nontx BSS are open */
1722 				}
1723 
1724 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1725 							     nontx->data,
1726 							     nontx->datalen);
1727 				if (!nie || nie->datalen < 2)
1728 					return; /* Invalid non-inheritance element */
1729 
1730 				for (i = 1; i < nie->datalen - 1; i++) {
1731 					if (nie->data[i] == WLAN_EID_RSN) {
1732 						arvif->rsnie_present = false;
1733 						break;
1734 					}
1735 				}
1736 
1737 				return;
1738 			}
1739 		}
1740 	}
1741 }
1742 
ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif * arvif,struct ath12k_link_vif * tx_arvif,u8 bssid_index)1743 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1744 					 struct ath12k_link_vif *tx_arvif,
1745 					 u8 bssid_index)
1746 {
1747 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1748 	struct ieee80211_ema_beacons *beacons;
1749 	bool nontx_profile_found = false;
1750 	int ret = 0;
1751 	u8 i;
1752 
1753 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1754 							 tx_arvif->ahvif->vif,
1755 							 tx_arvif->link_id);
1756 	if (!beacons || !beacons->cnt) {
1757 		ath12k_warn(arvif->ar->ab,
1758 			    "failed to get ema beacon templates from mac80211\n");
1759 		return -EPERM;
1760 	}
1761 
1762 	if (tx_arvif == arvif)
1763 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1764 
1765 	for (i = 0; i < beacons->cnt; i++) {
1766 		if (tx_arvif != arvif && !nontx_profile_found)
1767 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1768 						 bssid_index,
1769 						 &nontx_profile_found);
1770 
1771 		ema_args.bcn_cnt = beacons->cnt;
1772 		ema_args.bcn_index = i;
1773 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1774 					  beacons->bcn[i].skb, &ema_args);
1775 		if (ret) {
1776 			ath12k_warn(tx_arvif->ar->ab,
1777 				    "failed to set ema beacon template id %i error %d\n",
1778 				    i, ret);
1779 			break;
1780 		}
1781 	}
1782 
1783 	if (tx_arvif != arvif && !nontx_profile_found)
1784 		ath12k_warn(arvif->ar->ab,
1785 			    "nontransmitted bssid index %u not found in beacon template\n",
1786 			    bssid_index);
1787 
1788 	ieee80211_beacon_free_ema_list(beacons);
1789 	return ret;
1790 }
1791 
ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif * arvif)1792 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1793 {
1794 	struct ath12k_vif *ahvif = arvif->ahvif;
1795 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1796 	struct ieee80211_bss_conf *link_conf;
1797 	struct ath12k_link_vif *tx_arvif;
1798 	struct ath12k *ar = arvif->ar;
1799 	struct ath12k_base *ab = ar->ab;
1800 	struct ieee80211_mutable_offsets offs = {};
1801 	bool nontx_profile_found = false;
1802 	struct sk_buff *bcn;
1803 	int ret;
1804 
1805 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1806 		return 0;
1807 
1808 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1809 	if (!link_conf) {
1810 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1811 			    vif->addr, arvif->link_id);
1812 		return -ENOLINK;
1813 	}
1814 
1815 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1816 	if (tx_arvif) {
1817 		if (tx_arvif != arvif && arvif->is_up)
1818 			return 0;
1819 
1820 		if (link_conf->ema_ap)
1821 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1822 							     link_conf->bssid_index);
1823 	} else {
1824 		tx_arvif = arvif;
1825 	}
1826 
1827 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1828 					    tx_arvif->ahvif->vif,
1829 					    &offs, tx_arvif->link_id);
1830 	if (!bcn) {
1831 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1832 		return -EPERM;
1833 	}
1834 
1835 	if (tx_arvif == arvif) {
1836 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1837 	} else {
1838 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1839 					 link_conf->bssid_index,
1840 					 &nontx_profile_found);
1841 		if (!nontx_profile_found)
1842 			ath12k_warn(ab,
1843 				    "nontransmitted profile not found in beacon template\n");
1844 	}
1845 
1846 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1847 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1848 		if (ret) {
1849 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1850 				    ret);
1851 			goto free_bcn_skb;
1852 		}
1853 
1854 		/* P2P IE is inserted by firmware automatically (as
1855 		 * configured above) so remove it from the base beacon
1856 		 * template to avoid duplicate P2P IEs in beacon frames.
1857 		 */
1858 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1859 						  WLAN_OUI_TYPE_WFA_P2P,
1860 						  offsetof(struct ieee80211_mgmt,
1861 							   u.beacon.variable));
1862 		if (ret) {
1863 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1864 				    ret);
1865 			goto free_bcn_skb;
1866 		}
1867 	}
1868 
1869 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1870 
1871 	if (ret)
1872 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1873 			    ret);
1874 
1875 free_bcn_skb:
1876 	kfree_skb(bcn);
1877 	return ret;
1878 }
1879 
ath12k_control_beaconing(struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info)1880 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1881 				     struct ieee80211_bss_conf *info)
1882 {
1883 	struct ath12k_wmi_vdev_up_params params = {};
1884 	struct ath12k_vif *ahvif = arvif->ahvif;
1885 	struct ath12k *ar = arvif->ar;
1886 	int ret;
1887 
1888 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1889 
1890 	if (!info->enable_beacon) {
1891 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1892 		if (ret)
1893 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1894 				    arvif->vdev_id, ret);
1895 
1896 		arvif->is_up = false;
1897 		return;
1898 	}
1899 
1900 	/* Install the beacon template to the FW */
1901 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1902 	if (ret) {
1903 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1904 			    ret);
1905 		return;
1906 	}
1907 
1908 	ahvif->aid = 0;
1909 
1910 	ether_addr_copy(arvif->bssid, info->addr);
1911 
1912 	params.vdev_id = arvif->vdev_id;
1913 	params.aid = ahvif->aid;
1914 	params.bssid = arvif->bssid;
1915 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1916 	if (params.tx_bssid) {
1917 		params.nontx_profile_idx = info->bssid_index;
1918 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1919 	}
1920 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1921 	if (ret) {
1922 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1923 			    arvif->vdev_id, ret);
1924 		return;
1925 	}
1926 
1927 	arvif->is_up = true;
1928 
1929 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1930 }
1931 
ath12k_mac_handle_beacon_iter(void * data,u8 * mac,struct ieee80211_vif * vif)1932 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1933 					  struct ieee80211_vif *vif)
1934 {
1935 	struct sk_buff *skb = data;
1936 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1937 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1938 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1939 
1940 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1941 		return;
1942 
1943 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1944 		return;
1945 
1946 	cancel_delayed_work(&arvif->connection_loss_work);
1947 }
1948 
ath12k_mac_handle_beacon(struct ath12k * ar,struct sk_buff * skb)1949 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1950 {
1951 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1952 						   IEEE80211_IFACE_ITER_NORMAL,
1953 						   ath12k_mac_handle_beacon_iter,
1954 						   skb);
1955 }
1956 
ath12k_mac_handle_beacon_miss(struct ath12k * ar,struct ath12k_link_vif * arvif)1957 void ath12k_mac_handle_beacon_miss(struct ath12k *ar,
1958 				   struct ath12k_link_vif *arvif)
1959 {
1960 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1961 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1962 
1963 	if (!(arvif->is_created && arvif->is_up))
1964 		return;
1965 
1966 	ieee80211_beacon_loss(vif);
1967 
1968 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1969 	 * (done by mac80211) succeeds but beacons do not resume then it
1970 	 * doesn't make sense to continue operation. Queue connection loss work
1971 	 * which can be cancelled when beacon is received.
1972 	 */
1973 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1974 				     ATH12K_CONNECTION_LOSS_HZ);
1975 }
1976 
ath12k_mac_vif_sta_connection_loss_work(struct work_struct * work)1977 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1978 {
1979 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1980 						     connection_loss_work.work);
1981 	struct ieee80211_vif *vif = arvif->ahvif->vif;
1982 
1983 	if (!arvif->is_up)
1984 		return;
1985 
1986 	ieee80211_connection_loss(vif);
1987 }
1988 
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)1989 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
1990 				      struct ath12k_link_vif *arvif,
1991 				      struct ath12k_link_sta *arsta,
1992 				      struct ath12k_wmi_peer_assoc_arg *arg)
1993 {
1994 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1995 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
1996 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1997 	struct ieee80211_bss_conf *bss_conf;
1998 	u32 aid;
1999 
2000 	lockdep_assert_wiphy(hw->wiphy);
2001 
2002 	if (vif->type == NL80211_IFTYPE_STATION)
2003 		aid = vif->cfg.aid;
2004 	else
2005 		aid = sta->aid;
2006 
2007 	ether_addr_copy(arg->peer_mac, arsta->addr);
2008 	arg->vdev_id = arvif->vdev_id;
2009 	arg->peer_associd = aid;
2010 	arg->auth_flag = true;
2011 	/* TODO: STA WAR in ath10k for listen interval required? */
2012 	arg->peer_listen_intval = hw->conf.listen_interval;
2013 	arg->peer_nss = 1;
2014 
2015 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
2016 	if (!bss_conf) {
2017 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
2018 			    vif->addr, arvif->link_id);
2019 		return;
2020 	}
2021 
2022 	arg->peer_caps = bss_conf->assoc_capability;
2023 }
2024 
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)2025 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
2026 				       struct ath12k_link_vif *arvif,
2027 				       struct ath12k_link_sta *arsta,
2028 				       struct ath12k_wmi_peer_assoc_arg *arg)
2029 {
2030 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2031 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2032 	struct ieee80211_bss_conf *info;
2033 	struct cfg80211_chan_def def;
2034 	struct cfg80211_bss *bss;
2035 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2036 	const u8 *rsnie = NULL;
2037 	const u8 *wpaie = NULL;
2038 
2039 	lockdep_assert_wiphy(hw->wiphy);
2040 
2041 	info = ath12k_mac_get_link_bss_conf(arvif);
2042 	if (!info) {
2043 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
2044 			    vif->addr, arvif->link_id);
2045 		return;
2046 	}
2047 
2048 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2049 		return;
2050 
2051 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
2052 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2053 
2054 	if (arvif->rsnie_present || arvif->wpaie_present) {
2055 		arg->need_ptk_4_way = true;
2056 		if (arvif->wpaie_present)
2057 			arg->need_gtk_2_way = true;
2058 	} else if (bss) {
2059 		const struct cfg80211_bss_ies *ies;
2060 
2061 		rcu_read_lock();
2062 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2063 
2064 		ies = rcu_dereference(bss->ies);
2065 
2066 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2067 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2068 						ies->data,
2069 						ies->len);
2070 		rcu_read_unlock();
2071 		cfg80211_put_bss(hw->wiphy, bss);
2072 	}
2073 
2074 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2075 	if (rsnie || wpaie) {
2076 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2077 			   "%s: rsn ie found\n", __func__);
2078 		arg->need_ptk_4_way = true;
2079 	}
2080 
2081 	if (wpaie) {
2082 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
2083 			   "%s: wpa ie found\n", __func__);
2084 		arg->need_gtk_2_way = true;
2085 	}
2086 
2087 	if (sta->mfp) {
2088 		/* TODO: Need to check if FW supports PMF? */
2089 		arg->is_pmf_enabled = true;
2090 	}
2091 
2092 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
2093 }
2094 
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)2095 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
2096 				      struct ath12k_link_vif *arvif,
2097 				      struct ath12k_link_sta *arsta,
2098 				      struct ath12k_wmi_peer_assoc_arg *arg)
2099 {
2100 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2101 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2102 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2103 	struct ieee80211_link_sta *link_sta;
2104 	struct cfg80211_chan_def def;
2105 	const struct ieee80211_supported_band *sband;
2106 	const struct ieee80211_rate *rates;
2107 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
2108 	enum nl80211_band band;
2109 	u32 ratemask;
2110 	u8 rate;
2111 	int i;
2112 
2113 	lockdep_assert_wiphy(hw->wiphy);
2114 
2115 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2116 		return;
2117 
2118 	link_sta = ath12k_mac_get_link_sta(arsta);
2119 	if (!link_sta) {
2120 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2121 			    sta->addr, arsta->link_id);
2122 		return;
2123 	}
2124 
2125 	band = def.chan->band;
2126 	sband = hw->wiphy->bands[band];
2127 	ratemask = link_sta->supp_rates[band];
2128 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2129 	rates = sband->bitrates;
2130 
2131 	rateset->num_rates = 0;
2132 
2133 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2134 		if (!(ratemask & 1))
2135 			continue;
2136 
2137 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2138 		rateset->rates[rateset->num_rates] = rate;
2139 		rateset->num_rates++;
2140 	}
2141 }
2142 
2143 static bool
ath12k_peer_assoc_h_ht_masked(const u8 * ht_mcs_mask)2144 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2145 {
2146 	int nss;
2147 
2148 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2149 		if (ht_mcs_mask[nss])
2150 			return false;
2151 
2152 	return true;
2153 }
2154 
2155 static bool
ath12k_peer_assoc_h_vht_masked(const u16 * vht_mcs_mask)2156 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2157 {
2158 	int nss;
2159 
2160 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2161 		if (vht_mcs_mask[nss])
2162 			return false;
2163 
2164 	return true;
2165 }
2166 
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)2167 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2168 				   struct ath12k_link_vif *arvif,
2169 				   struct ath12k_link_sta *arsta,
2170 				   struct ath12k_wmi_peer_assoc_arg *arg)
2171 {
2172 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2173 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2174 	const struct ieee80211_sta_ht_cap *ht_cap;
2175 	struct ieee80211_link_sta *link_sta;
2176 	struct cfg80211_chan_def def;
2177 	enum nl80211_band band;
2178 	const u8 *ht_mcs_mask;
2179 	int i, n;
2180 	u8 max_nss;
2181 	u32 stbc;
2182 
2183 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2184 
2185 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2186 		return;
2187 
2188 	link_sta = ath12k_mac_get_link_sta(arsta);
2189 	if (!link_sta) {
2190 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2191 			    sta->addr, arsta->link_id);
2192 		return;
2193 	}
2194 
2195 	ht_cap = &link_sta->ht_cap;
2196 	if (!ht_cap->ht_supported)
2197 		return;
2198 
2199 	band = def.chan->band;
2200 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2201 
2202 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2203 		return;
2204 
2205 	arg->ht_flag = true;
2206 
2207 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2208 				    ht_cap->ampdu_factor)) - 1;
2209 
2210 	arg->peer_mpdu_density =
2211 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2212 
2213 	arg->peer_ht_caps = ht_cap->cap;
2214 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2215 
2216 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2217 		arg->ldpc_flag = true;
2218 
2219 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2220 		arg->bw_40 = true;
2221 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2222 	}
2223 
2224 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2225 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2226 	 * flags if guard interval is to force Long GI
2227 	 */
2228 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2229 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2230 	} else {
2231 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2232 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2233 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2234 	}
2235 
2236 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2237 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2238 		arg->stbc_flag = true;
2239 	}
2240 
2241 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2242 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2243 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2244 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2245 		arg->peer_rate_caps |= stbc;
2246 		arg->stbc_flag = true;
2247 	}
2248 
2249 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2250 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2251 	else if (ht_cap->mcs.rx_mask[1])
2252 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2253 
2254 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2255 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2256 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2257 			max_nss = (i / 8) + 1;
2258 			arg->peer_ht_rates.rates[n++] = i;
2259 		}
2260 
2261 	/* This is a workaround for HT-enabled STAs which break the spec
2262 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2263 	 *
2264 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2265 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2266 	 *
2267 	 * Firmware asserts if such situation occurs.
2268 	 */
2269 	if (n == 0) {
2270 		arg->peer_ht_rates.num_rates = 8;
2271 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2272 			arg->peer_ht_rates.rates[i] = i;
2273 	} else {
2274 		arg->peer_ht_rates.num_rates = n;
2275 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2276 	}
2277 
2278 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2279 		   arg->peer_mac,
2280 		   arg->peer_ht_rates.num_rates,
2281 		   arg->peer_nss);
2282 }
2283 
ath12k_mac_get_max_vht_mcs_map(u16 mcs_map,int nss)2284 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2285 {
2286 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2287 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2288 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2289 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2290 	}
2291 	return 0;
2292 }
2293 
2294 static u16
ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])2295 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2296 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2297 {
2298 	int idx_limit;
2299 	int nss;
2300 	u16 mcs_map;
2301 	u16 mcs;
2302 
2303 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2304 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2305 			  vht_mcs_limit[nss];
2306 
2307 		if (mcs_map)
2308 			idx_limit = fls(mcs_map) - 1;
2309 		else
2310 			idx_limit = -1;
2311 
2312 		switch (idx_limit) {
2313 		case 0:
2314 		case 1:
2315 		case 2:
2316 		case 3:
2317 		case 4:
2318 		case 5:
2319 		case 6:
2320 		case 7:
2321 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2322 			break;
2323 		case 8:
2324 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2325 			break;
2326 		case 9:
2327 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2328 			break;
2329 		default:
2330 			WARN_ON(1);
2331 			fallthrough;
2332 		case -1:
2333 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2334 			break;
2335 		}
2336 
2337 		tx_mcs_set &= ~(0x3 << (nss * 2));
2338 		tx_mcs_set |= mcs << (nss * 2);
2339 	}
2340 
2341 	return tx_mcs_set;
2342 }
2343 
ath12k_get_nss_160mhz(struct ath12k * ar,u8 max_nss)2344 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2345 				u8 max_nss)
2346 {
2347 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2348 	u8 max_sup_nss = 0;
2349 
2350 	switch (nss_ratio_info) {
2351 	case WMI_NSS_RATIO_1BY2_NSS:
2352 		max_sup_nss = max_nss >> 1;
2353 		break;
2354 	case WMI_NSS_RATIO_3BY4_NSS:
2355 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2356 		break;
2357 	case WMI_NSS_RATIO_1_NSS:
2358 		max_sup_nss = max_nss;
2359 		break;
2360 	case WMI_NSS_RATIO_2_NSS:
2361 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2362 		break;
2363 	default:
2364 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2365 			    nss_ratio_info);
2366 		break;
2367 	}
2368 
2369 	return max_sup_nss;
2370 }
2371 
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)2372 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2373 				    struct ath12k_link_vif *arvif,
2374 				    struct ath12k_link_sta *arsta,
2375 				    struct ath12k_wmi_peer_assoc_arg *arg)
2376 {
2377 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2378 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2379 	const struct ieee80211_sta_vht_cap *vht_cap;
2380 	struct ieee80211_link_sta *link_sta;
2381 	struct cfg80211_chan_def def;
2382 	enum nl80211_band band;
2383 	u16 *vht_mcs_mask;
2384 	u8 ampdu_factor;
2385 	u8 max_nss, vht_mcs;
2386 	int i, vht_nss, nss_idx;
2387 	bool user_rate_valid = true;
2388 	u32 rx_nss, tx_nss, nss_160;
2389 
2390 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2391 
2392 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2393 		return;
2394 
2395 	link_sta = ath12k_mac_get_link_sta(arsta);
2396 	if (!link_sta) {
2397 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2398 			    sta->addr, arsta->link_id);
2399 		return;
2400 	}
2401 
2402 	vht_cap = &link_sta->vht_cap;
2403 	if (!vht_cap->vht_supported)
2404 		return;
2405 
2406 	band = def.chan->band;
2407 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2408 
2409 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2410 		return;
2411 
2412 	arg->vht_flag = true;
2413 
2414 	/* TODO: similar flags required? */
2415 	arg->vht_capable = true;
2416 
2417 	if (def.chan->band == NL80211_BAND_2GHZ)
2418 		arg->vht_ng_flag = true;
2419 
2420 	arg->peer_vht_caps = vht_cap->cap;
2421 
2422 	ampdu_factor = (vht_cap->cap &
2423 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2424 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2425 
2426 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2427 	 * zero in VHT IE. Using it would result in degraded throughput.
2428 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2429 	 * it if VHT max_mpdu is smaller.
2430 	 */
2431 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2432 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2433 					ampdu_factor)) - 1);
2434 
2435 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2436 		arg->bw_80 = true;
2437 
2438 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2439 		arg->bw_160 = true;
2440 
2441 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2442 
2443 	if (vht_nss > link_sta->rx_nss) {
2444 		user_rate_valid = false;
2445 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2446 			if (vht_mcs_mask[nss_idx]) {
2447 				user_rate_valid = true;
2448 				break;
2449 			}
2450 		}
2451 	}
2452 
2453 	if (!user_rate_valid) {
2454 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2455 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2456 			   link_sta->rx_nss, arsta->addr);
2457 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2458 	}
2459 
2460 	/* Calculate peer NSS capability from VHT capabilities if STA
2461 	 * supports VHT.
2462 	 */
2463 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2464 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2465 			  (2 * i) & 3;
2466 
2467 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2468 		    vht_mcs_mask[i])
2469 			max_nss = i + 1;
2470 	}
2471 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2472 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2473 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2474 	arg->rx_mcs_set = ath12k_peer_assoc_h_vht_limit(arg->rx_mcs_set, vht_mcs_mask);
2475 
2476 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2477 	arg->tx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2478 
2479 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2480 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2481 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2482 	 */
2483 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2484 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2485 
2486 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2487 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2488 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2489 
2490 	/* TODO:  Check */
2491 	arg->tx_max_mcs_nss = 0xFF;
2492 
2493 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2494 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2495 		rx_nss = min(arg->peer_nss, tx_nss);
2496 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2497 
2498 		if (!rx_nss) {
2499 			ath12k_warn(ar->ab, "invalid max_nss\n");
2500 			return;
2501 		}
2502 
2503 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2504 		arg->peer_bw_rxnss_override |= nss_160;
2505 	}
2506 
2507 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2508 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2509 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2510 		   arg->peer_bw_rxnss_override);
2511 }
2512 
ath12k_mac_get_max_he_mcs_map(u16 mcs_map,int nss)2513 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2514 {
2515 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2516 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2517 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2518 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2519 	}
2520 	return 0;
2521 }
2522 
ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,const u16 * he_mcs_limit)2523 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2524 					const u16 *he_mcs_limit)
2525 {
2526 	int idx_limit;
2527 	int nss;
2528 	u16 mcs_map;
2529 	u16 mcs;
2530 
2531 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2532 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2533 			he_mcs_limit[nss];
2534 
2535 		if (mcs_map)
2536 			idx_limit = fls(mcs_map) - 1;
2537 		else
2538 			idx_limit = -1;
2539 
2540 		switch (idx_limit) {
2541 		case 0 ... 7:
2542 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2543 			break;
2544 		case 8:
2545 		case 9:
2546 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2547 			break;
2548 		case 10:
2549 		case 11:
2550 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2551 			break;
2552 		default:
2553 			WARN_ON(1);
2554 			fallthrough;
2555 		case -1:
2556 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2557 			break;
2558 		}
2559 
2560 		tx_mcs_set &= ~(0x3 << (nss * 2));
2561 		tx_mcs_set |= mcs << (nss * 2);
2562 	}
2563 
2564 	return tx_mcs_set;
2565 }
2566 
2567 static bool
ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])2568 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2569 {
2570 	int nss;
2571 
2572 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2573 		if (he_mcs_mask[nss])
2574 			return false;
2575 
2576 	return true;
2577 }
2578 
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)2579 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2580 				   struct ath12k_link_vif *arvif,
2581 				   struct ath12k_link_sta *arsta,
2582 				   struct ath12k_wmi_peer_assoc_arg *arg)
2583 {
2584 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2585 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2586 	const struct ieee80211_sta_he_cap *he_cap;
2587 	struct ieee80211_bss_conf *link_conf;
2588 	struct ieee80211_link_sta *link_sta;
2589 	struct cfg80211_chan_def def;
2590 	int i;
2591 	u8 ampdu_factor, max_nss;
2592 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2593 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2594 	u16 mcs_160_map, mcs_80_map;
2595 	u8 link_id = arvif->link_id;
2596 	bool support_160;
2597 	enum nl80211_band band;
2598 	u16 *he_mcs_mask;
2599 	u8 he_mcs;
2600 	u16 he_tx_mcs = 0, v = 0;
2601 	int he_nss, nss_idx;
2602 	bool user_rate_valid = true;
2603 	u32 rx_nss, tx_nss, nss_160;
2604 
2605 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2606 		return;
2607 
2608 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2609 	if (!link_conf) {
2610 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2611 			    vif->addr, link_id);
2612 		return;
2613 	}
2614 
2615 	link_sta = ath12k_mac_get_link_sta(arsta);
2616 	if (!link_sta) {
2617 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2618 			    sta->addr, arsta->link_id);
2619 		return;
2620 	}
2621 
2622 	he_cap = &link_sta->he_cap;
2623 	if (!he_cap->has_he)
2624 		return;
2625 
2626 	band = def.chan->band;
2627 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2628 
2629 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2630 		return;
2631 
2632 	arg->he_flag = true;
2633 
2634 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2635 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2636 
2637 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2638 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2639 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2640 
2641 	if (support_160) {
2642 		for (i = 7; i >= 0; i--) {
2643 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2644 
2645 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2646 				rx_mcs_160 = i + 1;
2647 				break;
2648 			}
2649 		}
2650 	}
2651 
2652 	for (i = 7; i >= 0; i--) {
2653 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2654 
2655 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2656 			rx_mcs_80 = i + 1;
2657 			break;
2658 		}
2659 	}
2660 
2661 	if (support_160)
2662 		max_nss = min(rx_mcs_80, rx_mcs_160);
2663 	else
2664 		max_nss = rx_mcs_80;
2665 
2666 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2667 
2668 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2669 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2670 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2671 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2672 	arg->peer_he_ops = link_conf->he_oper.params;
2673 
2674 	/* the top most byte is used to indicate BSS color info */
2675 	arg->peer_he_ops &= 0xffffff;
2676 
2677 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2678 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2679 	 * as calculated while parsing VHT caps(if VHT caps is present)
2680 	 * or HT caps (if VHT caps is not present).
2681 	 *
2682 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2683 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2684 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2685 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2686 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2687 	 * length.
2688 	 */
2689 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2690 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2691 
2692 	if (ampdu_factor) {
2693 		if (link_sta->vht_cap.vht_supported)
2694 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2695 						    ampdu_factor)) - 1;
2696 		else if (link_sta->ht_cap.ht_supported)
2697 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2698 						    ampdu_factor)) - 1;
2699 	}
2700 
2701 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2702 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2703 		int bit = 7;
2704 		int nss, ru;
2705 
2706 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2707 					  IEEE80211_PPE_THRES_NSS_MASK;
2708 		arg->peer_ppet.ru_bit_mask =
2709 			(he_cap->ppe_thres[0] &
2710 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2711 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2712 
2713 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2714 			for (ru = 0; ru < 4; ru++) {
2715 				u32 val = 0;
2716 				int i;
2717 
2718 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2719 					continue;
2720 				for (i = 0; i < 6; i++) {
2721 					val >>= 1;
2722 					val |= ((he_cap->ppe_thres[bit / 8] >>
2723 						 (bit % 8)) & 0x1) << 5;
2724 					bit++;
2725 				}
2726 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2727 								val << (ru * 6);
2728 			}
2729 		}
2730 	}
2731 
2732 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2733 		arg->twt_responder = true;
2734 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2735 		arg->twt_requester = true;
2736 
2737 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2738 
2739 	if (he_nss > link_sta->rx_nss) {
2740 		user_rate_valid = false;
2741 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2742 			if (he_mcs_mask[nss_idx]) {
2743 				user_rate_valid = true;
2744 				break;
2745 			}
2746 		}
2747 	}
2748 
2749 	if (!user_rate_valid) {
2750 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2751 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2752 			   link_sta->rx_nss, arsta->addr);
2753 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2754 	}
2755 
2756 	switch (link_sta->bandwidth) {
2757 	case IEEE80211_STA_RX_BW_160:
2758 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2759 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2760 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2761 
2762 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
2763 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2764 
2765 		arg->peer_he_mcs_count++;
2766 		if (!he_tx_mcs)
2767 			he_tx_mcs = v;
2768 		fallthrough;
2769 
2770 	default:
2771 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2772 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2773 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2774 
2775 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2776 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2777 
2778 		arg->peer_he_mcs_count++;
2779 		if (!he_tx_mcs)
2780 			he_tx_mcs = v;
2781 		break;
2782 	}
2783 
2784 	/* Calculate peer NSS capability from HE capabilities if STA
2785 	 * supports HE.
2786 	 */
2787 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2788 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2789 
2790 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2791 		 * unsupported range, with he_mcs_mask set, so check either of them
2792 		 * to find nss.
2793 		 */
2794 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2795 		    he_mcs_mask[i])
2796 			max_nss = i + 1;
2797 	}
2798 
2799 	max_nss = min(max_nss, ar->num_tx_chains);
2800 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2801 
2802 	if (arg->peer_phymode == MODE_11AX_HE160) {
2803 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2804 		rx_nss = min(arg->peer_nss, tx_nss);
2805 
2806 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2807 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2808 
2809 		if (!rx_nss) {
2810 			ath12k_warn(ar->ab, "invalid max_nss\n");
2811 			return;
2812 		}
2813 
2814 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2815 		arg->peer_bw_rxnss_override |= nss_160;
2816 	}
2817 
2818 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2819 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2820 		   arsta->addr, arg->peer_nss,
2821 		   arg->peer_he_mcs_count,
2822 		   arg->peer_bw_rxnss_override);
2823 }
2824 
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)2825 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2826 					struct ath12k_link_vif *arvif,
2827 					struct ath12k_link_sta *arsta,
2828 					struct ath12k_wmi_peer_assoc_arg *arg)
2829 {
2830 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2831 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2832 	const struct ieee80211_sta_he_cap *he_cap;
2833 	struct ieee80211_link_sta *link_sta;
2834 	struct cfg80211_chan_def def;
2835 	enum nl80211_band band;
2836 	u8 ampdu_factor, mpdu_density;
2837 
2838 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2839 		return;
2840 
2841 	band = def.chan->band;
2842 
2843 	link_sta = ath12k_mac_get_link_sta(arsta);
2844 	if (!link_sta) {
2845 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2846 			    sta->addr, arsta->link_id);
2847 		return;
2848 	}
2849 
2850 	he_cap = &link_sta->he_cap;
2851 
2852 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2853 		return;
2854 
2855 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2856 		arg->bw_40 = true;
2857 
2858 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2859 		arg->bw_80 = true;
2860 
2861 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2862 		arg->bw_160 = true;
2863 
2864 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2865 		arg->bw_320 = true;
2866 
2867 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2868 
2869 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2870 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2871 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2872 
2873 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2874 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2875 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2876 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2877 	 * Band Capabilities element in the 6 GHz band.
2878 	 *
2879 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2880 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2881 	 */
2882 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2883 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2884 			u32_get_bits(arg->peer_he_caps_6ghz,
2885 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2886 
2887 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2888 				     ampdu_factor)) - 1;
2889 }
2890 
ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap * ht_cap,const struct ieee80211_he_6ghz_capa * he_6ghz_capa,int * smps)2891 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2892 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2893 				     int *smps)
2894 {
2895 	if (ht_cap->ht_supported)
2896 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2897 	else
2898 		*smps = le16_get_bits(he_6ghz_capa->capa,
2899 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2900 
2901 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2902 		return -EINVAL;
2903 
2904 	return 0;
2905 }
2906 
ath12k_peer_assoc_h_smps(struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2907 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2908 				     struct ath12k_wmi_peer_assoc_arg *arg)
2909 {
2910 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2911 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2912 	struct ath12k_link_vif *arvif = arsta->arvif;
2913 	const struct ieee80211_sta_ht_cap *ht_cap;
2914 	struct ieee80211_link_sta *link_sta;
2915 	struct ath12k *ar = arvif->ar;
2916 	int smps;
2917 
2918 	link_sta = ath12k_mac_get_link_sta(arsta);
2919 	if (!link_sta) {
2920 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2921 			    sta->addr, arsta->link_id);
2922 		return;
2923 	}
2924 
2925 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2926 	ht_cap = &link_sta->ht_cap;
2927 
2928 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2929 		return;
2930 
2931 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2932 		return;
2933 
2934 	switch (smps) {
2935 	case WLAN_HT_CAP_SM_PS_STATIC:
2936 		arg->static_mimops_flag = true;
2937 		break;
2938 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2939 		arg->dynamic_mimops_flag = true;
2940 		break;
2941 	case WLAN_HT_CAP_SM_PS_DISABLED:
2942 		arg->spatial_mux_flag = true;
2943 		break;
2944 	default:
2945 		break;
2946 	}
2947 }
2948 
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)2949 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2950 				    struct ath12k_link_vif *arvif,
2951 				    struct ath12k_link_sta *arsta,
2952 				    struct ath12k_wmi_peer_assoc_arg *arg)
2953 {
2954 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2955 
2956 	switch (arvif->ahvif->vdev_type) {
2957 	case WMI_VDEV_TYPE_AP:
2958 		if (sta->wme) {
2959 			/* TODO: Check WME vs QoS */
2960 			arg->is_wme_set = true;
2961 			arg->qos_flag = true;
2962 		}
2963 
2964 		if (sta->wme && sta->uapsd_queues) {
2965 			/* TODO: Check WME vs QoS */
2966 			arg->is_wme_set = true;
2967 			arg->apsd_flag = true;
2968 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2969 		}
2970 		break;
2971 	case WMI_VDEV_TYPE_STA:
2972 		if (sta->wme) {
2973 			arg->is_wme_set = true;
2974 			arg->qos_flag = true;
2975 		}
2976 		break;
2977 	default:
2978 		break;
2979 	}
2980 
2981 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
2982 		   arsta->addr, arg->qos_flag);
2983 }
2984 
ath12k_peer_assoc_qos_ap(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)2985 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
2986 				    struct ath12k_link_vif *arvif,
2987 				    struct ath12k_link_sta *arsta)
2988 {
2989 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2990 	struct ath12k_wmi_ap_ps_arg arg;
2991 	u32 max_sp;
2992 	u32 uapsd;
2993 	int ret;
2994 
2995 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2996 
2997 	arg.vdev_id = arvif->vdev_id;
2998 
2999 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
3000 		   sta->uapsd_queues, sta->max_sp);
3001 
3002 	uapsd = 0;
3003 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
3004 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
3005 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
3006 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
3007 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
3008 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
3009 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
3010 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
3011 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
3012 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
3013 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
3014 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
3015 
3016 	max_sp = 0;
3017 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
3018 		max_sp = sta->max_sp;
3019 
3020 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
3021 	arg.value = uapsd;
3022 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3023 	if (ret)
3024 		goto err;
3025 
3026 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
3027 	arg.value = max_sp;
3028 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3029 	if (ret)
3030 		goto err;
3031 
3032 	/* TODO: revisit during testing */
3033 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
3034 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3035 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3036 	if (ret)
3037 		goto err;
3038 
3039 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
3040 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
3041 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
3042 	if (ret)
3043 		goto err;
3044 
3045 	return 0;
3046 
3047 err:
3048 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
3049 		    arg.param, arvif->vdev_id, ret);
3050 	return ret;
3051 }
3052 
ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta * sta)3053 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
3054 {
3055 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
3056 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
3057 }
3058 
ath12k_mac_get_phymode_vht(struct ath12k * ar,struct ieee80211_link_sta * link_sta)3059 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
3060 						    struct ieee80211_link_sta *link_sta)
3061 {
3062 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3063 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
3064 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
3065 			return MODE_11AC_VHT160;
3066 
3067 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
3068 		 * support
3069 		 */
3070 		return MODE_11AC_VHT160;
3071 	}
3072 
3073 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3074 		return MODE_11AC_VHT80;
3075 
3076 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3077 		return MODE_11AC_VHT40;
3078 
3079 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3080 		return MODE_11AC_VHT20;
3081 
3082 	return MODE_UNKNOWN;
3083 }
3084 
ath12k_mac_get_phymode_he(struct ath12k * ar,struct ieee80211_link_sta * link_sta)3085 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
3086 						   struct ieee80211_link_sta *link_sta)
3087 {
3088 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3089 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3090 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3091 			return MODE_11AX_HE160;
3092 
3093 		return MODE_UNKNOWN;
3094 	}
3095 
3096 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3097 		return MODE_11AX_HE80;
3098 
3099 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3100 		return MODE_11AX_HE40;
3101 
3102 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3103 		return MODE_11AX_HE20;
3104 
3105 	return MODE_UNKNOWN;
3106 }
3107 
ath12k_mac_get_phymode_eht(struct ath12k * ar,struct ieee80211_link_sta * link_sta)3108 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
3109 						    struct ieee80211_link_sta *link_sta)
3110 {
3111 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
3112 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
3113 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3114 			return MODE_11BE_EHT320;
3115 
3116 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
3117 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3118 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3119 			return MODE_11BE_EHT160;
3120 
3121 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3122 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3123 
3124 		return MODE_UNKNOWN;
3125 	}
3126 
3127 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3128 		return MODE_11BE_EHT80;
3129 
3130 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3131 		return MODE_11BE_EHT40;
3132 
3133 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3134 		return MODE_11BE_EHT20;
3135 
3136 	return MODE_UNKNOWN;
3137 }
3138 
3139 static bool
ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])3140 ath12k_peer_assoc_h_eht_masked(const u16 eht_mcs_mask[NL80211_EHT_NSS_MAX])
3141 {
3142 	int nss;
3143 
3144 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++)
3145 		if (eht_mcs_mask[nss])
3146 			return false;
3147 
3148 	return true;
3149 }
3150 
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)3151 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3152 					struct ath12k_link_vif *arvif,
3153 					struct ath12k_link_sta *arsta,
3154 					struct ath12k_wmi_peer_assoc_arg *arg)
3155 {
3156 	struct ieee80211_link_sta *link_sta;
3157 	struct cfg80211_chan_def def;
3158 	enum nl80211_band band;
3159 	const u8 *ht_mcs_mask;
3160 	const u16 *vht_mcs_mask;
3161 	const u16 *he_mcs_mask;
3162 	const u16 *eht_mcs_mask;
3163 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3164 
3165 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3166 
3167 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3168 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3169 
3170 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3171 		return;
3172 
3173 	band = def.chan->band;
3174 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3175 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3176 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3177 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3178 
3179 	link_sta = ath12k_mac_get_link_sta(arsta);
3180 	if (!link_sta) {
3181 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3182 			    sta->addr, arsta->link_id);
3183 		return;
3184 	}
3185 
3186 	switch (band) {
3187 	case NL80211_BAND_2GHZ:
3188 		if (link_sta->eht_cap.has_eht &&
3189 		    !ath12k_peer_assoc_h_eht_masked(eht_mcs_mask)) {
3190 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3191 				phymode = MODE_11BE_EHT40_2G;
3192 			else
3193 				phymode = MODE_11BE_EHT20_2G;
3194 		} else if (link_sta->he_cap.has_he &&
3195 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3196 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3197 				phymode = MODE_11AX_HE80_2G;
3198 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3199 				phymode = MODE_11AX_HE40_2G;
3200 			else
3201 				phymode = MODE_11AX_HE20_2G;
3202 		} else if (link_sta->vht_cap.vht_supported &&
3203 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3204 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3205 				phymode = MODE_11AC_VHT40;
3206 			else
3207 				phymode = MODE_11AC_VHT20;
3208 		} else if (link_sta->ht_cap.ht_supported &&
3209 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3210 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3211 				phymode = MODE_11NG_HT40;
3212 			else
3213 				phymode = MODE_11NG_HT20;
3214 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3215 			phymode = MODE_11G;
3216 		} else {
3217 			phymode = MODE_11B;
3218 		}
3219 		break;
3220 	case NL80211_BAND_5GHZ:
3221 	case NL80211_BAND_6GHZ:
3222 		/* Check EHT first */
3223 		if (link_sta->eht_cap.has_eht) {
3224 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3225 		} else if (link_sta->he_cap.has_he &&
3226 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3227 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3228 		} else if (link_sta->vht_cap.vht_supported &&
3229 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3230 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3231 		} else if (link_sta->ht_cap.ht_supported &&
3232 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3233 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3234 				phymode = MODE_11NA_HT40;
3235 			else
3236 				phymode = MODE_11NA_HT20;
3237 		} else {
3238 			phymode = MODE_11A;
3239 		}
3240 		break;
3241 	default:
3242 		break;
3243 	}
3244 
3245 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3246 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3247 
3248 	arg->peer_phymode = phymode;
3249 	WARN_ON(phymode == MODE_UNKNOWN);
3250 }
3251 
3252 #define ATH12K_EHT_MCS_7_ENABLED	0x00FF
3253 #define ATH12K_EHT_MCS_9_ENABLED	0x0300
3254 #define ATH12K_EHT_MCS_11_ENABLED	0x0C00
3255 #define ATH12K_EHT_MCS_13_ENABLED	0x3000
3256 
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])3257 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3258 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3259 				   u32 *rx_mcs, u32 *tx_mcs,
3260 				   const u16 eht_mcs_limit[NL80211_EHT_NSS_MAX])
3261 {
3262 	int nss;
3263 	u8 mcs_7 = 0, mcs_9 = 0, mcs_11 = 0, mcs_13 = 0;
3264 	u8 peer_mcs_7, peer_mcs_9, peer_mcs_11, peer_mcs_13;
3265 
3266 	for (nss = 0; nss < NL80211_EHT_NSS_MAX; nss++) {
3267 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_7_ENABLED)
3268 			mcs_7++;
3269 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_9_ENABLED)
3270 			mcs_9++;
3271 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_11_ENABLED)
3272 			mcs_11++;
3273 		if (eht_mcs_limit[nss] & ATH12K_EHT_MCS_13_ENABLED)
3274 			mcs_13++;
3275 	}
3276 
3277 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX);
3278 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX);
3279 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX);
3280 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX);
3281 
3282 	*rx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3283 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3284 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3285 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3286 
3287 	peer_mcs_7 = u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX);
3288 	peer_mcs_9 = u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX);
3289 	peer_mcs_11 = u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX);
3290 	peer_mcs_13 = u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX);
3291 
3292 	*tx_mcs = u32_encode_bits(min(peer_mcs_7, mcs_7), WMI_EHT_MCS_NSS_0_7) |
3293 		  u32_encode_bits(min(peer_mcs_9, mcs_9), WMI_EHT_MCS_NSS_8_9) |
3294 		  u32_encode_bits(min(peer_mcs_11, mcs_11), WMI_EHT_MCS_NSS_10_11) |
3295 		  u32_encode_bits(min(peer_mcs_13, mcs_13), WMI_EHT_MCS_NSS_12_13);
3296 }
3297 
ath12k_mac_set_eht_ppe_threshold(const u8 * ppe_thres,struct ath12k_wmi_ppe_threshold_arg * ppet)3298 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3299 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3300 {
3301 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3302 	u8 nss, ru, i;
3303 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3304 
3305 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3306 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3307 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3308 
3309 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3310 		for (ru = 0;
3311 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3312 		     ru++) {
3313 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3314 				continue;
3315 
3316 			val = 0;
3317 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3318 				val |= (((ppe_thres[bit_pos / 8] >>
3319 					  (bit_pos % 8)) & 0x1) << i);
3320 				bit_pos++;
3321 			}
3322 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3323 					(val << (ru * ppet_bit_len_per_ru));
3324 		}
3325 	}
3326 }
3327 
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)3328 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3329 				    struct ath12k_link_vif *arvif,
3330 				    struct ath12k_link_sta *arsta,
3331 				    struct ath12k_wmi_peer_assoc_arg *arg)
3332 {
3333 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3334 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3335 	const struct ieee80211_eht_mcs_nss_supp *own_eht_mcs_nss_supp;
3336 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3337 	const struct ieee80211_sta_eht_cap *eht_cap, *own_eht_cap;
3338 	const struct ieee80211_sband_iftype_data *iftd;
3339 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3340 	const struct ieee80211_sta_he_cap *he_cap;
3341 	struct ieee80211_link_sta *link_sta;
3342 	struct ieee80211_bss_conf *link_conf;
3343 	struct cfg80211_chan_def def;
3344 	bool user_rate_valid = true;
3345 	enum nl80211_band band;
3346 	int eht_nss, nss_idx;
3347 	u32 *rx_mcs, *tx_mcs;
3348 	u16 *eht_mcs_mask;
3349 	u8 max_nss = 0;
3350 
3351 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3352 
3353 	link_sta = ath12k_mac_get_link_sta(arsta);
3354 	if (!link_sta) {
3355 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3356 			    sta->addr, arsta->link_id);
3357 		return;
3358 	}
3359 
3360 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3361 	if (!link_conf) {
3362 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3363 		return;
3364 	}
3365 
3366 	eht_cap = &link_sta->eht_cap;
3367 	he_cap = &link_sta->he_cap;
3368 	if (!he_cap->has_he || !eht_cap->has_eht)
3369 		return;
3370 
3371 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3372 		return;
3373 
3374 	band = def.chan->band;
3375 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
3376 
3377 	iftd = ieee80211_get_sband_iftype_data(&ar->mac.sbands[band], vif->type);
3378 	if (!iftd) {
3379 		ath12k_warn(ar->ab,
3380 			    "unable to access iftype_data in struct ieee80211_supported_band\n");
3381 		return;
3382 	}
3383 
3384 	own_eht_cap = &iftd->eht_cap;
3385 	own_eht_mcs_nss_supp = &own_eht_cap->eht_mcs_nss_supp;
3386 
3387 	arg->eht_flag = true;
3388 
3389 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3390 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3391 	    eht_cap->eht_ppe_thres[0] != 0)
3392 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3393 						 &arg->peer_eht_ppet);
3394 
3395 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3396 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3397 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3398 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3399 
3400 	rx_mcs = arg->peer_eht_rx_mcs_set;
3401 	tx_mcs = arg->peer_eht_tx_mcs_set;
3402 
3403 	eht_nss = ath12k_mac_max_eht_mcs_nss((void *)own_eht_mcs_nss_supp,
3404 					     sizeof(*own_eht_mcs_nss_supp));
3405 	if (eht_nss > link_sta->rx_nss) {
3406 		user_rate_valid = false;
3407 		for (nss_idx = (link_sta->rx_nss - 1); nss_idx >= 0; nss_idx--) {
3408 			if (eht_mcs_mask[nss_idx]) {
3409 				user_rate_valid = true;
3410 				break;
3411 			}
3412 		}
3413 	}
3414 
3415 	if (!user_rate_valid) {
3416 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3417 			   "Setting eht range MCS value to peer supported nss %d for peer %pM\n",
3418 			   link_sta->rx_nss, arsta->addr);
3419 		eht_mcs_mask[link_sta->rx_nss - 1] = eht_mcs_mask[eht_nss - 1];
3420 	}
3421 
3422 	bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3423 	bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3424 
3425 	switch (link_sta->bandwidth) {
3426 	case IEEE80211_STA_RX_BW_320:
3427 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3428 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3429 				       bw->rx_tx_mcs9_max_nss,
3430 				       bw->rx_tx_mcs11_max_nss,
3431 				       bw->rx_tx_mcs13_max_nss,
3432 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3433 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3434 				       eht_mcs_mask);
3435 		arg->peer_eht_mcs_count++;
3436 		fallthrough;
3437 	case IEEE80211_STA_RX_BW_160:
3438 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3439 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3440 				       bw->rx_tx_mcs9_max_nss,
3441 				       bw->rx_tx_mcs11_max_nss,
3442 				       bw->rx_tx_mcs13_max_nss,
3443 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3444 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3445 				       eht_mcs_mask);
3446 		arg->peer_eht_mcs_count++;
3447 		fallthrough;
3448 	default:
3449 		if (!(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3450 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3451 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3452 
3453 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3454 					       bw_20->rx_tx_mcs9_max_nss,
3455 					       bw_20->rx_tx_mcs11_max_nss,
3456 					       bw_20->rx_tx_mcs13_max_nss,
3457 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3458 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3459 					       eht_mcs_mask);
3460 		} else {
3461 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3462 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3463 					       bw->rx_tx_mcs9_max_nss,
3464 					       bw->rx_tx_mcs11_max_nss,
3465 					       bw->rx_tx_mcs13_max_nss,
3466 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3467 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3468 					       eht_mcs_mask);
3469 		}
3470 
3471 		arg->peer_eht_mcs_count++;
3472 		break;
3473 	}
3474 
3475 	arg->punct_bitmap = ~arvif->punct_bitmap;
3476 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3477 
3478 	if (!(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3479 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3480 		if (bw_20->rx_tx_mcs13_max_nss)
3481 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3482 							   IEEE80211_EHT_MCS_NSS_RX));
3483 		if (bw_20->rx_tx_mcs11_max_nss)
3484 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3485 							   IEEE80211_EHT_MCS_NSS_RX));
3486 		if (bw_20->rx_tx_mcs9_max_nss)
3487 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3488 							   IEEE80211_EHT_MCS_NSS_RX));
3489 		if (bw_20->rx_tx_mcs7_max_nss)
3490 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3491 							   IEEE80211_EHT_MCS_NSS_RX));
3492 	} else {
3493 		if (bw->rx_tx_mcs13_max_nss)
3494 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3495 							   IEEE80211_EHT_MCS_NSS_RX));
3496 		if (bw->rx_tx_mcs11_max_nss)
3497 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3498 							   IEEE80211_EHT_MCS_NSS_RX));
3499 		if (bw->rx_tx_mcs9_max_nss)
3500 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3501 							   IEEE80211_EHT_MCS_NSS_RX));
3502 	}
3503 
3504 	max_nss = min(max_nss, (uint8_t)eht_nss);
3505 
3506 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3507 
3508 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3509 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3510 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3511 		   arg->punct_bitmap);
3512 }
3513 
ath12k_peer_assoc_h_mlo(struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)3514 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3515 				    struct ath12k_wmi_peer_assoc_arg *arg)
3516 {
3517 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3518 	struct peer_assoc_mlo_params *ml = &arg->ml;
3519 	struct ath12k_sta *ahsta = arsta->ahsta;
3520 	struct ath12k_link_sta *arsta_p;
3521 	struct ath12k_link_vif *arvif;
3522 	unsigned long links;
3523 	u8 link_id;
3524 	int i;
3525 
3526 	if (!sta->mlo || ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3527 		return;
3528 
3529 	ml->enabled = true;
3530 	ml->assoc_link = arsta->is_assoc_link;
3531 
3532 	/* For now considering the primary umac based on assoc link */
3533 	ml->primary_umac = arsta->is_assoc_link;
3534 	ml->peer_id_valid = true;
3535 	ml->logical_link_idx_valid = true;
3536 
3537 	ether_addr_copy(ml->mld_addr, sta->addr);
3538 	ml->logical_link_idx = arsta->link_idx;
3539 	ml->ml_peer_id = ahsta->ml_peer_id;
3540 	ml->ieee_link_id = arsta->link_id;
3541 	ml->num_partner_links = 0;
3542 	ml->eml_cap = sta->eml_cap;
3543 	links = ahsta->links_map;
3544 
3545 	rcu_read_lock();
3546 
3547 	i = 0;
3548 
3549 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3550 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3551 			break;
3552 
3553 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3554 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3555 
3556 		if (arsta_p == arsta)
3557 			continue;
3558 
3559 		if (!arvif->is_started)
3560 			continue;
3561 
3562 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3563 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3564 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3565 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3566 		ml->partner_info[i].logical_link_idx_valid = true;
3567 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3568 		ml->num_partner_links++;
3569 
3570 		i++;
3571 	}
3572 
3573 	rcu_read_unlock();
3574 }
3575 
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)3576 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3577 				      struct ath12k_link_vif *arvif,
3578 				      struct ath12k_link_sta *arsta,
3579 				      struct ath12k_wmi_peer_assoc_arg *arg,
3580 				      bool reassoc)
3581 {
3582 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3583 
3584 	memset(arg, 0, sizeof(*arg));
3585 
3586 	reinit_completion(&ar->peer_assoc_done);
3587 
3588 	arg->peer_new_assoc = !reassoc;
3589 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3590 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3591 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3592 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3593 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3594 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3595 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3596 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3597 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3598 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3599 	ath12k_peer_assoc_h_smps(arsta, arg);
3600 	ath12k_peer_assoc_h_mlo(arsta, arg);
3601 
3602 	arsta->peer_nss = arg->peer_nss;
3603 	/* TODO: amsdu_disable req? */
3604 }
3605 
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)3606 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3607 				  const u8 *addr,
3608 				  const struct ieee80211_sta_ht_cap *ht_cap,
3609 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3610 {
3611 	int smps, ret = 0;
3612 
3613 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3614 		return 0;
3615 
3616 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3617 	if (ret < 0)
3618 		return ret;
3619 
3620 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3621 					 WMI_PEER_MIMO_PS_STATE,
3622 					 ath12k_smps_map[smps]);
3623 }
3624 
ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif * arvif)3625 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3626 {
3627 	struct ath12k_vif *ahvif = arvif->ahvif;
3628 	struct ath12k *ar = arvif->ar;
3629 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3630 	u32 value = 0;
3631 	int ret;
3632 	struct ieee80211_bss_conf *link_conf;
3633 
3634 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3635 	if (!link_conf) {
3636 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3637 		return -EINVAL;
3638 	}
3639 
3640 	if (!link_conf->he_support)
3641 		return 0;
3642 
3643 	if (link_conf->he_su_beamformer) {
3644 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3645 		if (link_conf->he_mu_beamformer &&
3646 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3647 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3648 	}
3649 
3650 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3651 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3652 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3653 
3654 		if (link_conf->he_full_ul_mumimo)
3655 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3656 
3657 		if (link_conf->he_su_beamformee)
3658 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3659 	}
3660 
3661 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3662 	if (ret) {
3663 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3664 			    arvif->vdev_id, ret);
3665 		return ret;
3666 	}
3667 
3668 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3669 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3670 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3671 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3672 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3673 					    param, value);
3674 	if (ret) {
3675 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3676 			    arvif->vdev_id, ret);
3677 		return ret;
3678 	}
3679 
3680 	return 0;
3681 }
3682 
ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_sta_he_cap * he_cap,int * hemode)3683 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3684 					     struct ath12k_link_vif *arvif,
3685 					     struct ieee80211_sta_he_cap *he_cap,
3686 					     int *hemode)
3687 {
3688 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3689 	struct ieee80211_he_cap_elem he_cap_elem = {};
3690 	struct ieee80211_sta_he_cap *cap_band;
3691 	struct cfg80211_chan_def def;
3692 	u8 link_id = arvif->link_id;
3693 	struct ieee80211_bss_conf *link_conf;
3694 
3695 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3696 	if (!link_conf) {
3697 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3698 		return -EINVAL;
3699 	}
3700 
3701 	if (!link_conf->he_support)
3702 		return 0;
3703 
3704 	if (vif->type != NL80211_IFTYPE_STATION)
3705 		return -EINVAL;
3706 
3707 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3708 		return -EINVAL;
3709 
3710 	if (def.chan->band == NL80211_BAND_2GHZ)
3711 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3712 	else
3713 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3714 
3715 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3716 
3717 	*hemode = 0;
3718 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3719 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3720 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3721 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3722 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3723 	}
3724 
3725 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3726 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3727 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3728 
3729 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3730 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3731 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3732 							  HE_MODE_UL_MUMIMO);
3733 
3734 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3735 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3736 
3737 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3738 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3739 	}
3740 
3741 	return 0;
3742 }
3743 
ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif * arvif)3744 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3745 {
3746 	struct ath12k_vif *ahvif = arvif->ahvif;
3747 	struct ath12k *ar = arvif->ar;
3748 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3749 	u32 value = 0;
3750 	int ret;
3751 	struct ieee80211_bss_conf *link_conf;
3752 
3753 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3754 	if (!link_conf) {
3755 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3756 		return -ENOENT;
3757 	}
3758 
3759 	if (!link_conf->eht_support)
3760 		return 0;
3761 
3762 	if (link_conf->eht_su_beamformer) {
3763 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3764 		if (link_conf->eht_mu_beamformer &&
3765 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3766 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3767 						 EHT_MODE_MU_TX_BFER) |
3768 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3769 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3770 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3771 						 EHT_MODE_UL_OFDMA_MUMIMO);
3772 	}
3773 
3774 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3775 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3776 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3777 
3778 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3779 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3780 
3781 		if (link_conf->eht_su_beamformee)
3782 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3783 						 EHT_MODE_SU_TX_BFEE);
3784 	}
3785 
3786 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3787 	if (ret) {
3788 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3789 			    arvif->vdev_id, ret);
3790 		return ret;
3791 	}
3792 
3793 	return 0;
3794 }
3795 
ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k * ar,struct ieee80211_link_sta * link_sta)3796 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3797 					      struct ieee80211_link_sta *link_sta)
3798 {
3799 	u32 bw;
3800 
3801 	switch (link_sta->bandwidth) {
3802 	case IEEE80211_STA_RX_BW_20:
3803 		bw = WMI_PEER_CHWIDTH_20MHZ;
3804 		break;
3805 	case IEEE80211_STA_RX_BW_40:
3806 		bw = WMI_PEER_CHWIDTH_40MHZ;
3807 		break;
3808 	case IEEE80211_STA_RX_BW_80:
3809 		bw = WMI_PEER_CHWIDTH_80MHZ;
3810 		break;
3811 	case IEEE80211_STA_RX_BW_160:
3812 		bw = WMI_PEER_CHWIDTH_160MHZ;
3813 		break;
3814 	case IEEE80211_STA_RX_BW_320:
3815 		bw = WMI_PEER_CHWIDTH_320MHZ;
3816 		break;
3817 	default:
3818 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3819 			    link_sta->bandwidth, link_sta->addr);
3820 		bw = WMI_PEER_CHWIDTH_20MHZ;
3821 		break;
3822 	}
3823 
3824 	return bw;
3825 }
3826 
ath12k_bss_assoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * bss_conf)3827 static void ath12k_bss_assoc(struct ath12k *ar,
3828 			     struct ath12k_link_vif *arvif,
3829 			     struct ieee80211_bss_conf *bss_conf)
3830 {
3831 	struct ath12k_vif *ahvif = arvif->ahvif;
3832 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3833 	struct ath12k_wmi_vdev_up_params params = {};
3834 	struct ieee80211_link_sta *link_sta;
3835 	u8 link_id = bss_conf->link_id;
3836 	struct ath12k_link_sta *arsta;
3837 	struct ieee80211_sta *ap_sta;
3838 	struct ath12k_sta *ahsta;
3839 	struct ath12k_dp_link_peer *peer;
3840 	bool is_auth = false;
3841 	u32 hemode = 0;
3842 	int ret;
3843 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
3844 
3845 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3846 
3847 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3848 					kzalloc_obj(*peer_arg);
3849 	if (!peer_arg)
3850 		return;
3851 
3852 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3853 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3854 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3855 
3856 	rcu_read_lock();
3857 
3858 	/* During ML connection, cfg.ap_addr has the MLD address. For
3859 	 * non-ML connection, it has the BSSID.
3860 	 */
3861 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3862 	if (!ap_sta) {
3863 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3864 			    vif->cfg.ap_addr, arvif->vdev_id);
3865 		rcu_read_unlock();
3866 		return;
3867 	}
3868 
3869 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3870 
3871 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3872 				  ahsta->link[link_id]);
3873 	if (WARN_ON(!arsta)) {
3874 		rcu_read_unlock();
3875 		return;
3876 	}
3877 
3878 	link_sta = ath12k_mac_get_link_sta(arsta);
3879 	if (WARN_ON(!link_sta)) {
3880 		rcu_read_unlock();
3881 		return;
3882 	}
3883 
3884 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3885 
3886 	/* link_sta->he_cap must be protected by rcu_read_lock */
3887 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3888 	if (ret) {
3889 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3890 			    arvif->vdev_id, bss_conf->bssid, ret);
3891 		rcu_read_unlock();
3892 		return;
3893 	}
3894 
3895 	rcu_read_unlock();
3896 
3897 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3898 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3899 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3900 	if (ret) {
3901 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3902 			    hemode, ret);
3903 		return;
3904 	}
3905 
3906 	peer_arg->is_assoc = true;
3907 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3908 	if (ret) {
3909 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3910 			    bss_conf->bssid, arvif->vdev_id, ret);
3911 		return;
3912 	}
3913 
3914 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3915 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3916 			    bss_conf->bssid, arvif->vdev_id);
3917 		return;
3918 	}
3919 
3920 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3921 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3922 	if (ret) {
3923 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3924 			    arvif->vdev_id, ret);
3925 		return;
3926 	}
3927 
3928 	WARN_ON(arvif->is_up);
3929 
3930 	ahvif->aid = vif->cfg.aid;
3931 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3932 
3933 	params.vdev_id = arvif->vdev_id;
3934 	params.aid = ahvif->aid;
3935 	params.bssid = arvif->bssid;
3936 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
3937 	if (params.tx_bssid) {
3938 		params.nontx_profile_idx = bss_conf->bssid_index;
3939 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
3940 	}
3941 	ret = ath12k_wmi_vdev_up(ar, &params);
3942 	if (ret) {
3943 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3944 			    arvif->vdev_id, ret);
3945 		return;
3946 	}
3947 
3948 	arvif->is_up = true;
3949 	arvif->rekey_data.enable_offload = false;
3950 
3951 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3952 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3953 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3954 
3955 	spin_lock_bh(&dp->dp_lock);
3956 
3957 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
3958 							 arvif->bssid);
3959 	if (peer && peer->is_authorized)
3960 		is_auth = true;
3961 
3962 	spin_unlock_bh(&dp->dp_lock);
3963 
3964 	/* Authorize BSS Peer */
3965 	if (is_auth) {
3966 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
3967 						arvif->vdev_id,
3968 						WMI_PEER_AUTHORIZE,
3969 						1);
3970 		if (ret)
3971 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3972 	}
3973 
3974 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3975 					   &bss_conf->he_obss_pd);
3976 	if (ret)
3977 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3978 			    arvif->vdev_id, ret);
3979 
3980 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
3981 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
3982 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
3983 		ath12k_mac_11d_scan_stop_all(ar->ab);
3984 }
3985 
ath12k_bss_disassoc(struct ath12k * ar,struct ath12k_link_vif * arvif)3986 static void ath12k_bss_disassoc(struct ath12k *ar,
3987 				struct ath12k_link_vif *arvif)
3988 {
3989 	int ret;
3990 
3991 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3992 
3993 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3994 		   arvif->vdev_id, arvif->bssid);
3995 
3996 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
3997 	if (ret)
3998 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
3999 			    arvif->vdev_id, ret);
4000 
4001 	arvif->is_up = false;
4002 
4003 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
4004 
4005 	cancel_delayed_work(&arvif->connection_loss_work);
4006 }
4007 
ath12k_mac_get_rate_hw_value(int bitrate)4008 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
4009 {
4010 	u32 preamble;
4011 	u16 hw_value;
4012 	int rate;
4013 	size_t i;
4014 
4015 	if (ath12k_mac_bitrate_is_cck(bitrate))
4016 		preamble = WMI_RATE_PREAMBLE_CCK;
4017 	else
4018 		preamble = WMI_RATE_PREAMBLE_OFDM;
4019 
4020 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
4021 		if (ath12k_legacy_rates[i].bitrate != bitrate)
4022 			continue;
4023 
4024 		hw_value = ath12k_legacy_rates[i].hw_value;
4025 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4026 
4027 		return rate;
4028 	}
4029 
4030 	return -EINVAL;
4031 }
4032 
ath12k_recalculate_mgmt_rate(struct ath12k * ar,struct ath12k_link_vif * arvif,struct cfg80211_chan_def * def)4033 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
4034 					 struct ath12k_link_vif *arvif,
4035 					 struct cfg80211_chan_def *def)
4036 {
4037 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4038 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4039 	const struct ieee80211_supported_band *sband;
4040 	struct ieee80211_bss_conf *bss_conf;
4041 	u8 basic_rate_idx;
4042 	int hw_rate_code;
4043 	u32 vdev_param;
4044 	u16 bitrate;
4045 	int ret;
4046 
4047 	lockdep_assert_wiphy(hw->wiphy);
4048 
4049 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
4050 	if (!bss_conf) {
4051 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
4052 			    vif->addr, arvif->link_id);
4053 		return;
4054 	}
4055 
4056 	sband = hw->wiphy->bands[def->chan->band];
4057 	if (bss_conf->basic_rates)
4058 		basic_rate_idx = __ffs(bss_conf->basic_rates);
4059 	else
4060 		basic_rate_idx = 0;
4061 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
4062 
4063 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
4064 	if (hw_rate_code < 0) {
4065 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
4066 		return;
4067 	}
4068 
4069 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
4070 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4071 					    hw_rate_code);
4072 	if (ret)
4073 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
4074 
4075 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
4076 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4077 					    hw_rate_code);
4078 	if (ret)
4079 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
4080 }
4081 
ath12k_mac_bcn_tx_event(struct ath12k_link_vif * arvif)4082 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
4083 {
4084 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4085 	struct ieee80211_bss_conf *link_conf;
4086 
4087 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
4088 	if (!link_conf) {
4089 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
4090 			    arvif->vdev_id);
4091 		return;
4092 	}
4093 
4094 	if (link_conf->color_change_active) {
4095 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
4096 			ieee80211_color_change_finish(vif, arvif->link_id);
4097 			return;
4098 		}
4099 
4100 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
4101 		ath12k_mac_setup_bcn_tmpl(arvif);
4102 	}
4103 }
4104 
ath12k_mac_bcn_tx_work(struct wiphy * wiphy,struct wiphy_work * work)4105 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
4106 {
4107 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
4108 						     bcn_tx_work);
4109 
4110 	lockdep_assert_wiphy(wiphy);
4111 	ath12k_mac_bcn_tx_event(arvif);
4112 }
4113 
ath12k_mac_init_arvif(struct ath12k_vif * ahvif,struct ath12k_link_vif * arvif,int link_id)4114 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4115 				  struct ath12k_link_vif *arvif, int link_id)
4116 {
4117 	struct ath12k_hw *ah = ahvif->ah;
4118 	u8 _link_id;
4119 	int i;
4120 
4121 	lockdep_assert_wiphy(ah->hw->wiphy);
4122 
4123 	if (WARN_ON(!arvif))
4124 		return;
4125 
4126 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4127 		return;
4128 
4129 	if (link_id < 0)
4130 		_link_id = 0;
4131 	else
4132 		_link_id = link_id;
4133 
4134 	arvif->ahvif = ahvif;
4135 	arvif->link_id = _link_id;
4136 
4137 	/* Protects the datapath stats update on a per link basis */
4138 	spin_lock_init(&arvif->link_stats_lock);
4139 
4140 	INIT_LIST_HEAD(&arvif->list);
4141 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4142 			  ath12k_mac_vif_sta_connection_loss_work);
4143 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4144 
4145 	arvif->num_stations = 0;
4146 
4147 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4148 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4149 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4150 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4151 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4152 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4153 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4154 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4155 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4156 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4157 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4158 	}
4159 
4160 	/* Handle MLO related assignments */
4161 	if (link_id >= 0) {
4162 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4163 		ahvif->links_map |= BIT(_link_id);
4164 	}
4165 
4166 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4167 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4168 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4169 			   ahvif->links_map);
4170 }
4171 
ath12k_mac_remove_link_interface(struct ieee80211_hw * hw,struct ath12k_link_vif * arvif)4172 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4173 					     struct ath12k_link_vif *arvif)
4174 {
4175 	struct ath12k_vif *ahvif = arvif->ahvif;
4176 	struct ath12k_hw *ah = hw->priv;
4177 	struct ath12k *ar = arvif->ar;
4178 	int ret;
4179 
4180 	lockdep_assert_wiphy(ah->hw->wiphy);
4181 
4182 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4183 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4184 
4185 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4186 		   arvif->vdev_id, arvif->link_id);
4187 
4188 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4189 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4190 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4191 		ath12k_mac_11d_scan_stop(ar);
4192 
4193 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4194 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4195 		if (ret)
4196 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4197 				    arvif->vdev_id, arvif->link_id, ret);
4198 
4199 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)
4200 			ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
4201 	}
4202 	ath12k_mac_vdev_delete(ar, arvif);
4203 }
4204 
ath12k_mac_assign_link_vif(struct ath12k_hw * ah,struct ieee80211_vif * vif,u8 link_id)4205 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4206 							  struct ieee80211_vif *vif,
4207 							  u8 link_id)
4208 {
4209 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4210 	struct ath12k_link_vif *arvif;
4211 
4212 	lockdep_assert_wiphy(ah->hw->wiphy);
4213 
4214 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4215 	if (arvif)
4216 		return arvif;
4217 
4218 	/* If this is the first link arvif being created for an ML VIF
4219 	 * use the preallocated deflink memory except for scan arvifs
4220 	 */
4221 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4222 		arvif = &ahvif->deflink;
4223 
4224 		if (vif->type == NL80211_IFTYPE_STATION)
4225 			arvif->is_sta_assoc_link = true;
4226 	} else {
4227 		arvif = kzalloc_obj(*arvif);
4228 		if (!arvif)
4229 			return NULL;
4230 	}
4231 
4232 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4233 
4234 	return arvif;
4235 }
4236 
ath12k_mac_unassign_link_vif(struct ath12k_link_vif * arvif)4237 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4238 {
4239 	struct ath12k_vif *ahvif = arvif->ahvif;
4240 	struct ath12k_hw *ah = ahvif->ah;
4241 
4242 	lockdep_assert_wiphy(ah->hw->wiphy);
4243 
4244 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4245 	synchronize_rcu();
4246 	ahvif->links_map &= ~BIT(arvif->link_id);
4247 
4248 	if (arvif != &ahvif->deflink)
4249 		kfree(arvif);
4250 	else
4251 		memset(arvif, 0, sizeof(*arvif));
4252 }
4253 
4254 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])4255 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4256 			       struct ieee80211_vif *vif,
4257 			       u16 old_links, u16 new_links,
4258 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4259 {
4260 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4261 	unsigned long to_remove = old_links & ~new_links;
4262 	unsigned long to_add = ~old_links & new_links;
4263 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4264 	struct ath12k_link_vif *arvif;
4265 	u8 link_id;
4266 
4267 	lockdep_assert_wiphy(hw->wiphy);
4268 
4269 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4270 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4271 			   vif->addr, old_links, new_links);
4272 
4273 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4274 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4275 		/* mac80211 wants to add link but driver already has the
4276 		 * link. This should not happen ideally.
4277 		 */
4278 		if (WARN_ON(arvif))
4279 			return -EINVAL;
4280 
4281 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4282 		if (WARN_ON(!arvif))
4283 			return -EINVAL;
4284 	}
4285 
4286 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4287 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4288 		if (WARN_ON(!arvif))
4289 			return -EINVAL;
4290 
4291 		if (!arvif->is_created) {
4292 			ath12k_mac_unassign_link_vif(arvif);
4293 			continue;
4294 		}
4295 
4296 		if (WARN_ON(!arvif->ar))
4297 			return -EINVAL;
4298 
4299 		ath12k_mac_remove_link_interface(hw, arvif);
4300 		ath12k_mac_unassign_link_vif(arvif);
4301 	}
4302 
4303 	return 0;
4304 }
4305 EXPORT_SYMBOL(ath12k_mac_op_change_vif_links);
4306 
ath12k_mac_fils_discovery(struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info)4307 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4308 				     struct ieee80211_bss_conf *info)
4309 {
4310 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4311 	struct ath12k *ar = arvif->ar;
4312 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4313 	struct sk_buff *tmpl;
4314 	int ret;
4315 	u32 interval;
4316 	bool unsol_bcast_probe_resp_enabled = false;
4317 
4318 	if (info->fils_discovery.max_interval) {
4319 		interval = info->fils_discovery.max_interval;
4320 
4321 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif,
4322 							 info->link_id);
4323 		if (tmpl)
4324 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4325 							     tmpl);
4326 	} else if (info->unsol_bcast_probe_resp_interval) {
4327 		unsol_bcast_probe_resp_enabled = 1;
4328 		interval = info->unsol_bcast_probe_resp_interval;
4329 
4330 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif,
4331 								 info->link_id);
4332 		if (tmpl)
4333 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4334 							 tmpl);
4335 	} else { /* Disable */
4336 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4337 	}
4338 
4339 	if (!tmpl) {
4340 		ath12k_warn(ar->ab,
4341 			    "mac vdev %i failed to retrieve %s template\n",
4342 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4343 			    "unsolicited broadcast probe response" :
4344 			    "FILS discovery"));
4345 		return -EPERM;
4346 	}
4347 	kfree_skb(tmpl);
4348 
4349 	if (!ret)
4350 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4351 						unsol_bcast_probe_resp_enabled);
4352 
4353 	return ret;
4354 }
4355 
ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 changed)4356 void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4357 				   struct ieee80211_vif *vif,
4358 				   u64 changed)
4359 {
4360 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4361 	unsigned long links = ahvif->links_map;
4362 	struct ieee80211_bss_conf *info;
4363 	struct ath12k_link_vif *arvif;
4364 	struct ieee80211_sta *sta;
4365 	struct ath12k_sta *ahsta;
4366 	struct ath12k *ar;
4367 	u8 link_id;
4368 
4369 	lockdep_assert_wiphy(hw->wiphy);
4370 
4371 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4372 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4373 		if (vif->cfg.ssid_len)
4374 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4375 	}
4376 
4377 	if (changed & BSS_CHANGED_ASSOC) {
4378 		if (vif->cfg.assoc) {
4379 			/* only in station mode we can get here, so it's safe
4380 			 * to use ap_addr
4381 			 */
4382 			rcu_read_lock();
4383 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4384 			if (!sta) {
4385 				rcu_read_unlock();
4386 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4387 					  vif->cfg.ap_addr);
4388 				return;
4389 			}
4390 
4391 			ahsta = ath12k_sta_to_ahsta(sta);
4392 			arvif = wiphy_dereference(hw->wiphy,
4393 						  ahvif->link[ahsta->assoc_link_id]);
4394 			rcu_read_unlock();
4395 
4396 			ar = arvif->ar;
4397 			/* there is no reason for which an assoc link's
4398 			 * bss info does not exist
4399 			 */
4400 			info = ath12k_mac_get_link_bss_conf(arvif);
4401 			ath12k_bss_assoc(ar, arvif, info);
4402 
4403 			/* exclude assoc link as it is done above */
4404 			links &= ~BIT(ahsta->assoc_link_id);
4405 		}
4406 
4407 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4408 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4409 			if (!arvif || !arvif->ar)
4410 				continue;
4411 
4412 			ar = arvif->ar;
4413 
4414 			if (vif->cfg.assoc) {
4415 				info = ath12k_mac_get_link_bss_conf(arvif);
4416 				if (!info)
4417 					continue;
4418 
4419 				ath12k_bss_assoc(ar, arvif, info);
4420 			} else {
4421 				ath12k_bss_disassoc(ar, arvif);
4422 			}
4423 		}
4424 	}
4425 }
4426 EXPORT_SYMBOL(ath12k_mac_op_vif_cfg_changed);
4427 
ath12k_mac_vif_setup_ps(struct ath12k_link_vif * arvif)4428 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4429 {
4430 	struct ath12k *ar = arvif->ar;
4431 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4432 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4433 	enum wmi_sta_powersave_param param;
4434 	struct ieee80211_bss_conf *info;
4435 	enum wmi_sta_ps_mode psmode;
4436 	int ret;
4437 	int timeout;
4438 	bool enable_ps;
4439 
4440 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4441 
4442 	if (vif->type != NL80211_IFTYPE_STATION)
4443 		return;
4444 
4445 	enable_ps = arvif->ahvif->ps;
4446 	if (enable_ps) {
4447 		psmode = WMI_STA_PS_MODE_ENABLED;
4448 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4449 
4450 		timeout = conf->dynamic_ps_timeout;
4451 		if (timeout == 0) {
4452 			info = ath12k_mac_get_link_bss_conf(arvif);
4453 			if (!info) {
4454 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4455 					    vif->addr, arvif->link_id);
4456 				return;
4457 			}
4458 
4459 			/* firmware doesn't like 0 */
4460 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4461 		}
4462 
4463 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4464 						  timeout);
4465 		if (ret) {
4466 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4467 				    arvif->vdev_id, ret);
4468 			return;
4469 		}
4470 	} else {
4471 		psmode = WMI_STA_PS_MODE_DISABLED;
4472 	}
4473 
4474 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4475 		   arvif->vdev_id, psmode ? "enable" : "disable");
4476 
4477 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4478 	if (ret)
4479 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4480 			    psmode, arvif->vdev_id, ret);
4481 }
4482 
ath12k_mac_supports_tpc(struct ath12k * ar,struct ath12k_vif * ahvif,const struct cfg80211_chan_def * chandef)4483 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4484 				    const struct cfg80211_chan_def *chandef)
4485 {
4486 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4487 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4488 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4489 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4490 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4491 		chandef->chan &&
4492 		chandef->chan->band == NL80211_BAND_6GHZ;
4493 }
4494 
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)4495 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4496 				      struct ath12k_link_vif *arvif,
4497 				      struct ath12k_link_vif *tx_arvif,
4498 				      struct ieee80211_bss_conf *info, u16 aid)
4499 {
4500 	struct ath12k_wmi_vdev_up_params params = {
4501 		.vdev_id = arvif->vdev_id,
4502 		.aid = aid,
4503 		.bssid = arvif->bssid
4504 	};
4505 	int ret;
4506 
4507 	if (tx_arvif) {
4508 		params.tx_bssid = tx_arvif->bssid;
4509 		params.nontx_profile_idx = info->bssid_index;
4510 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4511 	}
4512 
4513 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4514 	if (ret)
4515 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4516 			    arvif->vdev_id, ret);
4517 }
4518 
ath12k_mac_config_obss_pd(struct ath12k_link_vif * arvif,const struct ieee80211_he_obss_pd * he_obss_pd)4519 static int ath12k_mac_config_obss_pd(struct ath12k_link_vif *arvif,
4520 				     const struct ieee80211_he_obss_pd *he_obss_pd)
4521 {
4522 	struct ath12k_wmi_obss_pd_arg obss_pd_arg = {};
4523 	u32 srg_bitmap[2], non_srg_bitmap[2];
4524 	struct ath12k *ar = arvif->ar;
4525 	u32 param_id, pdev_id;
4526 	u32 param_val;
4527 	int ret;
4528 
4529 	if (ar->ab->hw_params->single_pdev_only)
4530 		pdev_id = ath12k_mac_get_target_pdev_id_from_vif(arvif);
4531 	else
4532 		pdev_id = ar->pdev->pdev_id;
4533 
4534 	/* Set and enable SRG/non-SRG OBSS PD threshold */
4535 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
4536 	if (ar->monitor_started || !he_obss_pd->enable) {
4537 		ret = ath12k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
4538 		if (ret)
4539 			ath12k_warn(ar->ab,
4540 				    "failed to set OBSS PD threshold for pdev %u: %d\n",
4541 				    pdev_id, ret);
4542 		return ret;
4543 	}
4544 
4545 	/*
4546 	 * This service flag indicates firmware support for SRG/SRP-based
4547 	 * spatial reuse. It also specifies whether OBSS PD threshold values
4548 	 * should be interpreted as dB (offset) or dBm (absolute) units.
4549 	 */
4550 	obss_pd_arg.srp_support = test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
4551 					   ar->ab->wmi_ab.svc_map);
4552 
4553 	if (!(he_obss_pd->sr_ctrl &
4554 	      IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)) {
4555 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4556 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4557 						 he_obss_pd->non_srg_max_offset;
4558 		else
4559 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
4560 
4561 		if (!obss_pd_arg.srp_support)
4562 			obss_pd_arg.non_srg_th -= ATH12K_DEFAULT_NOISE_FLOOR;
4563 
4564 		obss_pd_arg.non_srg_enabled = true;
4565 	}
4566 
4567 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
4568 		obss_pd_arg.srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4569 				     he_obss_pd->max_offset;
4570 		obss_pd_arg.srg_enabled = true;
4571 	}
4572 
4573 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4574 		   "pdev %u OBSS PD sr_ctrl 0x%x srg_th %d dBm non_srg_th %d dBm\n",
4575 		   pdev_id, he_obss_pd->sr_ctrl,
4576 		   obss_pd_arg.srg_th, obss_pd_arg.non_srg_th);
4577 
4578 	param_val = ath12k_wmi_build_obss_pd(&obss_pd_arg);
4579 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4580 	if (ret) {
4581 		ath12k_warn(ar->ab,
4582 			    "failed to set OBSS PD threshold for pdev %u: %d\n",
4583 			    pdev_id, ret);
4584 		return ret;
4585 	}
4586 
4587 	/* Enable OBSS PD for all access category */
4588 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
4589 	param_val = 0xf;
4590 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4591 	if (ret) {
4592 		ath12k_warn(ar->ab,
4593 			    "failed to set OBSS PD per ac for pdev %u: %d\n",
4594 			    pdev_id, ret);
4595 		return ret;
4596 	}
4597 
4598 	/* Set SR prohibit */
4599 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
4600 	param_val = !!(he_obss_pd->sr_ctrl &
4601 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
4602 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4603 	if (ret) {
4604 		ath12k_warn(ar->ab, "failed to set SR prohibit for pdev %u: %d\n",
4605 			    pdev_id, ret);
4606 		return ret;
4607 	}
4608 
4609 	if (!obss_pd_arg.srp_support)
4610 		return 0;
4611 
4612 	memcpy(srg_bitmap, he_obss_pd->bss_color_bitmap, sizeof(srg_bitmap));
4613 	/* Set SRG BSS color bitmap */
4614 	ret = ath12k_wmi_pdev_set_srg_bss_color_bitmap(ar, pdev_id, srg_bitmap);
4615 	if (ret) {
4616 		ath12k_warn(ar->ab,
4617 			    "failed to set SRG bss color bitmap for pdev %u: %d\n",
4618 			    pdev_id, ret);
4619 		return ret;
4620 	}
4621 
4622 	/* Enable BSS colors for SRG */
4623 	ret = ath12k_wmi_pdev_srg_obss_color_enable_bitmap(ar, pdev_id, srg_bitmap);
4624 	if (ret) {
4625 		ath12k_warn(ar->ab,
4626 			    "failed to enable SRG bss color bitmap pdev %u: %d\n",
4627 			    pdev_id, ret);
4628 		return ret;
4629 	}
4630 
4631 	memcpy(srg_bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(srg_bitmap));
4632 	/* Set SRG partial bssid bitmap */
4633 	ret = ath12k_wmi_pdev_set_srg_partial_bssid_bitmap(ar, pdev_id, srg_bitmap);
4634 	if (ret) {
4635 		ath12k_warn(ar->ab,
4636 			    "failed to set SRG partial bssid bitmap for pdev %u: %d\n",
4637 			    pdev_id, ret);
4638 		return ret;
4639 	}
4640 
4641 	/* Enable partial bssid mask for SRG */
4642 	ret = ath12k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, pdev_id, srg_bitmap);
4643 	if (ret) {
4644 		ath12k_warn(ar->ab,
4645 			    "failed to enable SRG bssid bitmap pdev %u: %d\n",
4646 			    pdev_id, ret);
4647 		return ret;
4648 	}
4649 
4650 	/*
4651 	 * No explicit non-SRG bitmap from mac80211; enable all colors/bssids
4652 	 * as non-SRG candidates. Actual SRG members are filtered by SRG bitmaps.
4653 	 */
4654 	memset(non_srg_bitmap, 0xff, sizeof(non_srg_bitmap));
4655 
4656 	/* Enable BSS colors for non-SRG */
4657 	ret = ath12k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, pdev_id,
4658 							       non_srg_bitmap);
4659 	if (ret) {
4660 		ath12k_warn(ar->ab,
4661 			    "failed to enable non SRG color bitmap pdev %u: %d\n",
4662 			    pdev_id, ret);
4663 		return ret;
4664 	}
4665 
4666 	/* Enable partial bssid mask for non-SRG */
4667 	ret = ath12k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, pdev_id,
4668 							       non_srg_bitmap);
4669 	if (ret) {
4670 		ath12k_warn(ar->ab,
4671 			    "failed to enable non SRG bssid bitmap pdev %u: %d\n",
4672 			    pdev_id, ret);
4673 		return ret;
4674 	}
4675 
4676 	return 0;
4677 }
4678 
ath12k_mac_bss_info_changed(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info,u64 changed)4679 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4680 					struct ath12k_link_vif *arvif,
4681 					struct ieee80211_bss_conf *info,
4682 					u64 changed)
4683 {
4684 	struct ath12k_vif *ahvif = arvif->ahvif;
4685 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4686 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4687 	struct ath12k_link_vif *tx_arvif;
4688 	struct cfg80211_chan_def def;
4689 	u32 param_id, param_value;
4690 	enum nl80211_band band;
4691 	u32 vdev_param;
4692 	int mcast_rate;
4693 	u32 preamble;
4694 	u16 hw_value;
4695 	u16 bitrate;
4696 	u8 rateidx;
4697 	u32 rate;
4698 	int ret;
4699 
4700 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4701 
4702 	if (changed & BSS_CHANGED_BEACON_INT) {
4703 		arvif->beacon_interval = info->beacon_int;
4704 
4705 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4706 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4707 						    param_id,
4708 						    arvif->beacon_interval);
4709 		if (ret)
4710 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4711 				    arvif->vdev_id);
4712 		else
4713 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4714 				   "Beacon interval: %d set for VDEV: %d\n",
4715 				   arvif->beacon_interval, arvif->vdev_id);
4716 	}
4717 
4718 	if (changed & BSS_CHANGED_BEACON) {
4719 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4720 		param_value = WMI_BEACON_BURST_MODE;
4721 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4722 						param_value, ar->pdev->pdev_id);
4723 		if (ret)
4724 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4725 				    arvif->vdev_id);
4726 		else
4727 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4728 				   "Set burst beacon mode for VDEV: %d\n",
4729 				   arvif->vdev_id);
4730 
4731 		/* In MBSSID case, need to install transmitting VIF's template first */
4732 
4733 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4734 		if (ret)
4735 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4736 				    ret);
4737 
4738 		if (!arvif->is_csa_in_progress)
4739 			goto skip_vdev_up;
4740 
4741 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4742 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4743 			/* skip non tx vif's */
4744 			goto skip_vdev_up;
4745 
4746 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4747 
4748 		arvif->is_csa_in_progress = false;
4749 
4750 		if (tx_arvif && arvif == tx_arvif) {
4751 			struct ath12k_link_vif *arvif_itr;
4752 
4753 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4754 				if (!arvif_itr->is_csa_in_progress)
4755 					continue;
4756 
4757 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4758 							  info, ahvif->aid);
4759 				arvif_itr->is_csa_in_progress = false;
4760 			}
4761 		}
4762 	}
4763 
4764 skip_vdev_up:
4765 
4766 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4767 		arvif->dtim_period = info->dtim_period;
4768 
4769 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4770 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4771 						    param_id,
4772 						    arvif->dtim_period);
4773 
4774 		if (ret)
4775 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4776 				    arvif->vdev_id, ret);
4777 		else
4778 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4779 				   "DTIM period: %d set for VDEV: %d\n",
4780 				   arvif->dtim_period, arvif->vdev_id);
4781 	}
4782 
4783 	if (changed & BSS_CHANGED_SSID &&
4784 	    vif->type == NL80211_IFTYPE_AP) {
4785 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4786 		if (vif->cfg.ssid_len)
4787 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4788 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4789 	}
4790 
4791 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4792 		ether_addr_copy(arvif->bssid, info->bssid);
4793 
4794 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4795 		if (info->enable_beacon) {
4796 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4797 			if (ret)
4798 				ath12k_warn(ar->ab,
4799 					    "failed to set HE TXBF config for vdev: %d\n",
4800 					    arvif->vdev_id);
4801 
4802 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4803 			if (ret)
4804 				ath12k_warn(ar->ab,
4805 					    "failed to set EHT TXBF config for vdev: %d\n",
4806 					    arvif->vdev_id);
4807 		}
4808 		ath12k_control_beaconing(arvif, info);
4809 
4810 		if (arvif->is_up && info->he_support &&
4811 		    info->he_oper.params) {
4812 			/* TODO: Extend to support 1024 BA Bitmap size */
4813 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4814 							    WMI_VDEV_PARAM_BA_MODE,
4815 							    WMI_BA_MODE_BUFFER_SIZE_256);
4816 			if (ret)
4817 				ath12k_warn(ar->ab,
4818 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4819 					    arvif->vdev_id);
4820 
4821 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4822 			param_value = info->he_oper.params;
4823 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4824 							    param_id, param_value);
4825 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4826 				   "he oper param: %x set for VDEV: %d\n",
4827 				   param_value, arvif->vdev_id);
4828 
4829 			if (ret)
4830 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4831 					    param_value, arvif->vdev_id, ret);
4832 		}
4833 	}
4834 
4835 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4836 		u32 cts_prot;
4837 
4838 		cts_prot = !!(info->use_cts_prot);
4839 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4840 
4841 		if (arvif->is_started) {
4842 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4843 							    param_id, cts_prot);
4844 			if (ret)
4845 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4846 					    arvif->vdev_id);
4847 			else
4848 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4849 					   cts_prot, arvif->vdev_id);
4850 		} else {
4851 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4852 		}
4853 	}
4854 
4855 	if (changed & BSS_CHANGED_ERP_SLOT) {
4856 		u32 slottime;
4857 
4858 		if (info->use_short_slot)
4859 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4860 
4861 		else
4862 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4863 
4864 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4865 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4866 						    param_id, slottime);
4867 		if (ret)
4868 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4869 				    arvif->vdev_id);
4870 		else
4871 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4872 				   "Set slottime: %d for VDEV: %d\n",
4873 				   slottime, arvif->vdev_id);
4874 	}
4875 
4876 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4877 		u32 preamble;
4878 
4879 		if (info->use_short_preamble)
4880 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4881 		else
4882 			preamble = WMI_VDEV_PREAMBLE_LONG;
4883 
4884 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4885 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4886 						    param_id, preamble);
4887 		if (ret)
4888 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4889 				    arvif->vdev_id);
4890 		else
4891 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4892 				   "Set preamble: %d for VDEV: %d\n",
4893 				   preamble, arvif->vdev_id);
4894 	}
4895 
4896 	if (changed & BSS_CHANGED_ASSOC) {
4897 		if (vif->cfg.assoc)
4898 			ath12k_bss_assoc(ar, arvif, info);
4899 		else
4900 			ath12k_bss_disassoc(ar, arvif);
4901 	}
4902 
4903 	if (changed & BSS_CHANGED_TXPOWER) {
4904 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4905 			   arvif->vdev_id, info->txpower);
4906 
4907 		arvif->txpower = info->txpower;
4908 		ath12k_mac_txpower_recalc(ar);
4909 	}
4910 
4911 	if (changed & BSS_CHANGED_MCAST_RATE &&
4912 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4913 		band = def.chan->band;
4914 		mcast_rate = info->mcast_rate[band];
4915 
4916 		if (mcast_rate > 0) {
4917 			rateidx = mcast_rate - 1;
4918 		} else {
4919 			if (info->basic_rates)
4920 				rateidx = __ffs(info->basic_rates);
4921 			else
4922 				rateidx = 0;
4923 		}
4924 
4925 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
4926 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
4927 
4928 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
4929 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
4930 
4931 		if (ath12k_mac_bitrate_is_cck(bitrate))
4932 			preamble = WMI_RATE_PREAMBLE_CCK;
4933 		else
4934 			preamble = WMI_RATE_PREAMBLE_OFDM;
4935 
4936 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4937 
4938 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4939 			   "mac vdev %d mcast_rate %x\n",
4940 			   arvif->vdev_id, rate);
4941 
4942 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
4943 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4944 						    vdev_param, rate);
4945 		if (ret)
4946 			ath12k_warn(ar->ab,
4947 				    "failed to set mcast rate on vdev %i: %d\n",
4948 				    arvif->vdev_id,  ret);
4949 
4950 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
4951 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4952 						    vdev_param, rate);
4953 		if (ret)
4954 			ath12k_warn(ar->ab,
4955 				    "failed to set bcast rate on vdev %i: %d\n",
4956 				    arvif->vdev_id,  ret);
4957 	}
4958 
4959 	if (changed & BSS_CHANGED_BASIC_RATES &&
4960 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
4961 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
4962 
4963 	if (changed & BSS_CHANGED_TWT) {
4964 		if (info->twt_requester || info->twt_responder)
4965 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
4966 		else
4967 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
4968 	}
4969 
4970 	if (changed & BSS_CHANGED_HE_OBSS_PD) {
4971 		if (vif->type == NL80211_IFTYPE_AP)
4972 			ath12k_mac_config_obss_pd(arvif, &info->he_obss_pd);
4973 		else
4974 			ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4975 						     &info->he_obss_pd);
4976 	}
4977 
4978 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
4979 		if (vif->type == NL80211_IFTYPE_AP) {
4980 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4981 							    arvif->vdev_id,
4982 							    info->he_bss_color.color,
4983 							    ATH12K_BSS_COLOR_AP_PERIODS,
4984 							    info->he_bss_color.enabled);
4985 			if (ret)
4986 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
4987 					    arvif->vdev_id,  ret);
4988 
4989 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
4990 			if (info->he_bss_color.enabled)
4991 				param_value = info->he_bss_color.color <<
4992 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
4993 			else
4994 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
4995 
4996 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4997 							    param_id,
4998 							    param_value);
4999 			if (ret)
5000 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
5001 					    arvif->vdev_id,  ret);
5002 			else
5003 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
5004 					   param_value, arvif->vdev_id);
5005 		} else if (vif->type == NL80211_IFTYPE_STATION) {
5006 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
5007 									  arvif->vdev_id,
5008 									  1);
5009 			if (ret)
5010 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
5011 					    arvif->vdev_id,  ret);
5012 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5013 							    arvif->vdev_id,
5014 							    0,
5015 							    ATH12K_BSS_COLOR_STA_PERIODS,
5016 							    1);
5017 			if (ret)
5018 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
5019 					    arvif->vdev_id,  ret);
5020 		}
5021 	}
5022 
5023 	ath12k_mac_fils_discovery(arvif, info);
5024 
5025 	if (changed & BSS_CHANGED_PS &&
5026 	    ar->ab->hw_params->supports_sta_ps) {
5027 		ahvif->ps = vif_cfg->ps;
5028 		ath12k_mac_vif_setup_ps(arvif);
5029 	}
5030 }
5031 
ath12k_ahvif_get_link_cache(struct ath12k_vif * ahvif,u8 link_id)5032 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
5033 							    u8 link_id)
5034 {
5035 	if (!ahvif->cache[link_id]) {
5036 		ahvif->cache[link_id] = kzalloc_obj(*ahvif->cache[0]);
5037 		if (ahvif->cache[link_id])
5038 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5039 	}
5040 
5041 	return ahvif->cache[link_id];
5042 }
5043 
ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache * cache)5044 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5045 {
5046 	struct ath12k_key_conf *key_conf, *tmp;
5047 
5048 	if (!cache || list_empty(&cache->key_conf.list))
5049 		return;
5050 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5051 		list_del(&key_conf->list);
5052 		kfree(key_conf);
5053 	}
5054 }
5055 
ath12k_ahvif_put_link_cache(struct ath12k_vif * ahvif,u8 link_id)5056 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5057 {
5058 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5059 		return;
5060 
5061 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5062 	kfree(ahvif->cache[link_id]);
5063 	ahvif->cache[link_id] = NULL;
5064 }
5065 
ath12k_mac_op_link_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)5066 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5067 				     struct ieee80211_vif *vif,
5068 				     struct ieee80211_bss_conf *info,
5069 				     u64 changed)
5070 {
5071 	struct ath12k *ar;
5072 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5073 	struct ath12k_vif_cache *cache;
5074 	struct ath12k_link_vif *arvif;
5075 	u8 link_id = info->link_id;
5076 
5077 	lockdep_assert_wiphy(hw->wiphy);
5078 
5079 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5080 
5081 	/* if the vdev is not created on a certain radio,
5082 	 * cache the info to be updated later on vdev creation
5083 	 */
5084 
5085 	if (!arvif || !arvif->is_created) {
5086 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5087 		if (!cache)
5088 			return;
5089 
5090 		cache->bss_conf_changed |= changed;
5091 
5092 		return;
5093 	}
5094 
5095 	ar = arvif->ar;
5096 
5097 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5098 }
5099 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5100 
5101 static struct ath12k*
ath12k_mac_select_scan_device(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 center_freq)5102 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5103 			      struct ieee80211_vif *vif,
5104 			      u32 center_freq)
5105 {
5106 	struct ath12k_hw *ah = hw->priv;
5107 	enum nl80211_band band;
5108 	struct ath12k *ar;
5109 	int i;
5110 
5111 	if (ah->num_radio == 1)
5112 		return ah->radio;
5113 
5114 	/* Currently mac80211 supports splitting scan requests into
5115 	 * multiple scan requests per band.
5116 	 * Loop through first channel and determine the scan radio
5117 	 * TODO: There could be 5 GHz low/high channels in that case
5118 	 * split the hw request and perform multiple scans
5119 	 */
5120 
5121 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5122 		band = NL80211_BAND_2GHZ;
5123 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5124 		band = NL80211_BAND_5GHZ;
5125 	else
5126 		band = NL80211_BAND_6GHZ;
5127 
5128 	for_each_ar(ah, ar, i) {
5129 		if (ar->mac.sbands[band].channels &&
5130 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5131 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5132 			return ar;
5133 	}
5134 
5135 	return NULL;
5136 }
5137 
__ath12k_mac_scan_finish(struct ath12k * ar)5138 void __ath12k_mac_scan_finish(struct ath12k *ar)
5139 {
5140 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5141 
5142 	lockdep_assert_held(&ar->data_lock);
5143 
5144 	switch (ar->scan.state) {
5145 	case ATH12K_SCAN_IDLE:
5146 		break;
5147 	case ATH12K_SCAN_RUNNING:
5148 	case ATH12K_SCAN_ABORTING:
5149 		if (ar->scan.is_roc && ar->scan.roc_notify)
5150 			ieee80211_remain_on_channel_expired(hw);
5151 		fallthrough;
5152 	case ATH12K_SCAN_STARTING:
5153 		cancel_delayed_work(&ar->scan.timeout);
5154 		complete_all(&ar->scan.completed);
5155 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5156 		break;
5157 	}
5158 }
5159 
ath12k_mac_scan_finish(struct ath12k * ar)5160 void ath12k_mac_scan_finish(struct ath12k *ar)
5161 {
5162 	spin_lock_bh(&ar->data_lock);
5163 	__ath12k_mac_scan_finish(ar);
5164 	spin_unlock_bh(&ar->data_lock);
5165 }
5166 
ath12k_scan_stop(struct ath12k * ar)5167 static int ath12k_scan_stop(struct ath12k *ar)
5168 {
5169 	struct ath12k_wmi_scan_cancel_arg arg = {
5170 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5171 		.scan_id = ATH12K_SCAN_ID,
5172 	};
5173 	int ret;
5174 
5175 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5176 
5177 	/* TODO: Fill other STOP Params */
5178 	arg.pdev_id = ar->pdev->pdev_id;
5179 
5180 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5181 	if (ret) {
5182 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5183 		goto out;
5184 	}
5185 
5186 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5187 	if (ret == 0) {
5188 		ath12k_warn(ar->ab,
5189 			    "failed to receive scan abort comple: timed out\n");
5190 		ret = -ETIMEDOUT;
5191 	} else if (ret > 0) {
5192 		ret = 0;
5193 	}
5194 
5195 out:
5196 	/* Scan state should be updated in scan completion worker but in
5197 	 * case firmware fails to deliver the event (for whatever reason)
5198 	 * it is desired to clean up scan state anyway. Firmware may have
5199 	 * just dropped the scan completion event delivery due to transport
5200 	 * pipe being overflown with data and/or it can recover on its own
5201 	 * before next scan request is submitted.
5202 	 */
5203 	spin_lock_bh(&ar->data_lock);
5204 	if (ret)
5205 		__ath12k_mac_scan_finish(ar);
5206 	spin_unlock_bh(&ar->data_lock);
5207 
5208 	return ret;
5209 }
5210 
ath12k_scan_abort(struct ath12k * ar)5211 static void ath12k_scan_abort(struct ath12k *ar)
5212 {
5213 	int ret;
5214 
5215 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5216 
5217 	spin_lock_bh(&ar->data_lock);
5218 
5219 	switch (ar->scan.state) {
5220 	case ATH12K_SCAN_IDLE:
5221 		/* This can happen if timeout worker kicked in and called
5222 		 * abortion while scan completion was being processed.
5223 		 */
5224 		break;
5225 	case ATH12K_SCAN_STARTING:
5226 	case ATH12K_SCAN_ABORTING:
5227 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5228 			    ar->scan.state);
5229 		break;
5230 	case ATH12K_SCAN_RUNNING:
5231 		ar->scan.state = ATH12K_SCAN_ABORTING;
5232 		spin_unlock_bh(&ar->data_lock);
5233 
5234 		ret = ath12k_scan_stop(ar);
5235 		if (ret)
5236 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5237 
5238 		spin_lock_bh(&ar->data_lock);
5239 		break;
5240 	}
5241 
5242 	spin_unlock_bh(&ar->data_lock);
5243 }
5244 
ath12k_scan_timeout_work(struct work_struct * work)5245 static void ath12k_scan_timeout_work(struct work_struct *work)
5246 {
5247 	struct ath12k *ar = container_of(work, struct ath12k,
5248 					 scan.timeout.work);
5249 
5250 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5251 	ath12k_scan_abort(ar);
5252 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5253 }
5254 
ath12k_mac_scan_send_complete(struct ath12k * ar,struct cfg80211_scan_info * info)5255 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5256 					  struct cfg80211_scan_info *info)
5257 {
5258 	struct ath12k_hw *ah = ar->ah;
5259 	struct ath12k *partner_ar;
5260 	int i;
5261 
5262 	lockdep_assert_wiphy(ah->hw->wiphy);
5263 
5264 	for_each_ar(ah, partner_ar, i)
5265 		if (partner_ar != ar &&
5266 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5267 			return;
5268 
5269 	ieee80211_scan_completed(ah->hw, info);
5270 }
5271 
ath12k_scan_vdev_clean_work(struct wiphy * wiphy,struct wiphy_work * work)5272 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5273 {
5274 	struct ath12k *ar = container_of(work, struct ath12k,
5275 					 scan.vdev_clean_wk);
5276 	struct ath12k_hw *ah = ar->ah;
5277 	struct ath12k_link_vif *arvif;
5278 
5279 	lockdep_assert_wiphy(wiphy);
5280 
5281 	arvif = ar->scan.arvif;
5282 
5283 	/* The scan vdev has already been deleted. This can occur when a
5284 	 * new scan request is made on the same vif with a different
5285 	 * frequency, causing the scan arvif to move from one radio to
5286 	 * another. Or, scan was abrupted and via remove interface, the
5287 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5288 	 * being used as an actual vdev, then do not delete it.
5289 	 */
5290 	if (!arvif || arvif->is_started)
5291 		goto work_complete;
5292 
5293 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5294 		   arvif->link_id);
5295 
5296 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5297 	ath12k_mac_unassign_link_vif(arvif);
5298 
5299 work_complete:
5300 	spin_lock_bh(&ar->data_lock);
5301 	ar->scan.arvif = NULL;
5302 	if (!ar->scan.is_roc) {
5303 		struct cfg80211_scan_info info = {
5304 			.aborted = ((ar->scan.state ==
5305 				    ATH12K_SCAN_ABORTING) ||
5306 				    (ar->scan.state ==
5307 				    ATH12K_SCAN_STARTING)),
5308 		};
5309 
5310 		ath12k_mac_scan_send_complete(ar, &info);
5311 	}
5312 
5313 	ar->scan.state = ATH12K_SCAN_IDLE;
5314 	ar->scan_channel = NULL;
5315 	ar->scan.roc_freq = 0;
5316 	spin_unlock_bh(&ar->data_lock);
5317 }
5318 
ath12k_start_scan(struct ath12k * ar,struct ath12k_wmi_scan_req_arg * arg)5319 static int ath12k_start_scan(struct ath12k *ar,
5320 			     struct ath12k_wmi_scan_req_arg *arg)
5321 {
5322 	int ret;
5323 
5324 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5325 
5326 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5327 	if (ret)
5328 		return ret;
5329 
5330 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5331 	if (ret == 0) {
5332 		ret = ath12k_scan_stop(ar);
5333 		if (ret)
5334 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5335 
5336 		return -ETIMEDOUT;
5337 	}
5338 
5339 	/* If we failed to start the scan, return error code at
5340 	 * this point.  This is probably due to some issue in the
5341 	 * firmware, but no need to wedge the driver due to that...
5342 	 */
5343 	spin_lock_bh(&ar->data_lock);
5344 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5345 		spin_unlock_bh(&ar->data_lock);
5346 		return -EINVAL;
5347 	}
5348 	spin_unlock_bh(&ar->data_lock);
5349 
5350 	return 0;
5351 }
5352 
ath12k_mac_get_fw_stats(struct ath12k * ar,struct ath12k_fw_stats_req_params * param)5353 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5354 			    struct ath12k_fw_stats_req_params *param)
5355 {
5356 	struct ath12k_base *ab = ar->ab;
5357 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5358 	unsigned long time_left;
5359 	int ret;
5360 
5361 	guard(mutex)(&ah->hw_mutex);
5362 
5363 	if (ah->state != ATH12K_HW_STATE_ON)
5364 		return -ENETDOWN;
5365 
5366 	reinit_completion(&ar->fw_stats_complete);
5367 	reinit_completion(&ar->fw_stats_done);
5368 
5369 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5370 						param->vdev_id, param->pdev_id);
5371 	if (ret) {
5372 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5373 		return ret;
5374 	}
5375 
5376 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5377 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5378 		   param->pdev_id, param->vdev_id, param->stats_id);
5379 
5380 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5381 	if (!time_left) {
5382 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5383 		return -ETIMEDOUT;
5384 	}
5385 
5386 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5387 	 * when stats data buffer limit is reached. fw_stats_complete
5388 	 * is completed once host receives first event from firmware, but
5389 	 * still there could be more events following. Below is to wait
5390 	 * until firmware completes sending all the events.
5391 	 */
5392 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5393 	if (!time_left) {
5394 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5395 		return -ETIMEDOUT;
5396 	}
5397 
5398 	return 0;
5399 }
5400 
ath12k_mac_op_get_txpower(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,int * dbm)5401 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5402 			      struct ieee80211_vif *vif,
5403 			      unsigned int link_id,
5404 			      int *dbm)
5405 {
5406 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5407 	struct ath12k_fw_stats_req_params params = {};
5408 	struct ath12k_fw_stats_pdev *pdev;
5409 	struct ath12k_hw *ah = hw->priv;
5410 	struct ath12k_link_vif *arvif;
5411 	struct ath12k_base *ab;
5412 	struct ath12k *ar;
5413 	int ret;
5414 
5415 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5416 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5417 	 * regulatory rules obtained. FW knows all these power and sets the min
5418 	 * of these. Hence, we request the FW pdev stats in which FW reports
5419 	 * the minimum of all vdev's channel Tx power.
5420 	 */
5421 	lockdep_assert_wiphy(hw->wiphy);
5422 
5423 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5424 	if (!arvif || !arvif->ar)
5425 		return -EINVAL;
5426 
5427 	ar = arvif->ar;
5428 	ab = ar->ab;
5429 	if (ah->state != ATH12K_HW_STATE_ON)
5430 		goto err_fallback;
5431 
5432 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5433 		return -EAGAIN;
5434 
5435 	/* Limit the requests to Firmware for fetching the tx power */
5436 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5437 	    time_before(jiffies,
5438 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5439 					 ar->last_tx_power_update))
5440 		goto send_tx_power;
5441 
5442 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
5443 	params.vdev_id = arvif->vdev_id;
5444 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5445 	ret = ath12k_mac_get_fw_stats(ar, &params);
5446 	if (ret) {
5447 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5448 		goto err_fallback;
5449 	}
5450 
5451 	spin_lock_bh(&ar->data_lock);
5452 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5453 					struct ath12k_fw_stats_pdev, list);
5454 	if (!pdev) {
5455 		spin_unlock_bh(&ar->data_lock);
5456 		goto err_fallback;
5457 	}
5458 
5459 	/* tx power reported by firmware is in units of 0.5 dBm */
5460 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5461 	spin_unlock_bh(&ar->data_lock);
5462 	ar->last_tx_power_update = jiffies;
5463 	ath12k_fw_stats_reset(ar);
5464 
5465 send_tx_power:
5466 	*dbm = ar->chan_tx_pwr;
5467 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5468 		   *dbm);
5469 	return 0;
5470 
5471 err_fallback:
5472 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5473 	*dbm = vif->bss_conf.txpower;
5474 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5475 		   *dbm);
5476 	return 0;
5477 }
5478 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5479 
5480 static u8
ath12k_mac_find_link_id_by_ar(struct ath12k_vif * ahvif,struct ath12k * ar)5481 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5482 {
5483 	struct ath12k_link_vif *arvif;
5484 	struct ath12k_hw *ah = ahvif->ah;
5485 	unsigned long links = ahvif->links_map;
5486 	unsigned long scan_links_map;
5487 	u8 link_id;
5488 
5489 	lockdep_assert_wiphy(ah->hw->wiphy);
5490 
5491 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5492 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5493 
5494 		if (!arvif || !arvif->is_created)
5495 			continue;
5496 
5497 		if (ar == arvif->ar)
5498 			return link_id;
5499 	}
5500 
5501 	/* input ar is not assigned to any of the links of ML VIF, use next
5502 	 * available scan link for scan vdev creation. There are cases where
5503 	 * single scan req needs to be split in driver and initiate separate
5504 	 * scan requests to firmware based on device.
5505 	 */
5506 
5507 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5508 	  * choose an available link among scan links (i.e link id >= 15)
5509 	  */
5510 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5511 	if (scan_links_map)
5512 		return __ffs(scan_links_map);
5513 
5514 	return ATH12K_FIRST_SCAN_LINK;
5515 }
5516 
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)5517 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5518 				       struct ieee80211_vif *vif,
5519 				       struct ieee80211_scan_request *hw_req,
5520 				       int n_channels,
5521 				       struct ieee80211_channel **chan_list,
5522 				       struct ath12k *ar)
5523 {
5524 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5525 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5526 	struct ath12k_link_vif *arvif;
5527 	struct cfg80211_scan_request *req = &hw_req->req;
5528 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5529 	u8 link_id;
5530 	int ret;
5531 	int i;
5532 	bool create = true;
5533 
5534 	lockdep_assert_wiphy(hw->wiphy);
5535 
5536 	arvif = &ahvif->deflink;
5537 
5538 	/* check if any of the links of ML VIF is already started on
5539 	 * radio(ar) corresponding to given scan frequency and use it,
5540 	 * if not use scan link (link id >= 15) for scan purpose.
5541 	 */
5542 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5543 	/* All scan links are occupied. ideally this shouldn't happen as
5544 	 * mac80211 won't schedule scan for same band until ongoing scan is
5545 	 * completed, don't try to exceed max links just in case if it happens.
5546 	 */
5547 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5548 		return -EBUSY;
5549 
5550 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5551 
5552 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5553 		   arvif->link_id);
5554 
5555 	/* If the vif is already assigned to a specific vdev of an ar,
5556 	 * check whether its already started, vdev which is started
5557 	 * are not allowed to switch to a new radio.
5558 	 * If the vdev is not started, but was earlier created on a
5559 	 * different ar, delete that vdev and create a new one. We don't
5560 	 * delete at the scan stop as an optimization to avoid redundant
5561 	 * delete-create vdev's for the same ar, in case the request is
5562 	 * always on the same band for the vif
5563 	 */
5564 	if (arvif->is_created) {
5565 		if (WARN_ON(!arvif->ar))
5566 			return -EINVAL;
5567 
5568 		if (ar != arvif->ar && arvif->is_started)
5569 			return -EINVAL;
5570 
5571 		if (ar != arvif->ar) {
5572 			ath12k_mac_remove_link_interface(hw, arvif);
5573 			ath12k_mac_unassign_link_vif(arvif);
5574 		} else {
5575 			create = false;
5576 		}
5577 	}
5578 
5579 	if (create) {
5580 		/* Previous arvif would've been cleared in radio switch block
5581 		 * above, assign arvif again for create.
5582 		 */
5583 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5584 
5585 		ret = ath12k_mac_vdev_create(ar, arvif);
5586 		if (ret) {
5587 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5588 			ath12k_mac_unassign_link_vif(arvif);
5589 			return ret;
5590 		}
5591 	}
5592 
5593 	spin_lock_bh(&ar->data_lock);
5594 	switch (ar->scan.state) {
5595 	case ATH12K_SCAN_IDLE:
5596 		reinit_completion(&ar->scan.started);
5597 		reinit_completion(&ar->scan.completed);
5598 		ar->scan.state = ATH12K_SCAN_STARTING;
5599 		ar->scan.is_roc = false;
5600 		ar->scan.arvif = arvif;
5601 		ret = 0;
5602 		break;
5603 	case ATH12K_SCAN_STARTING:
5604 	case ATH12K_SCAN_RUNNING:
5605 	case ATH12K_SCAN_ABORTING:
5606 		ret = -EBUSY;
5607 		break;
5608 	}
5609 	spin_unlock_bh(&ar->data_lock);
5610 
5611 	if (ret)
5612 		goto exit;
5613 
5614 	arg = kzalloc_obj(*arg);
5615 	if (!arg) {
5616 		ret = -ENOMEM;
5617 		goto exit;
5618 	}
5619 
5620 	ath12k_wmi_start_scan_init(ar, arg);
5621 	arg->vdev_id = arvif->vdev_id;
5622 	arg->scan_id = ATH12K_SCAN_ID;
5623 
5624 	if (req->ie_len) {
5625 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5626 		if (!arg->extraie.ptr) {
5627 			ret = -ENOMEM;
5628 			goto exit;
5629 		}
5630 		arg->extraie.len = req->ie_len;
5631 	}
5632 
5633 	if (req->n_ssids) {
5634 		arg->num_ssids = req->n_ssids;
5635 		for (i = 0; i < arg->num_ssids; i++)
5636 			arg->ssid[i] = req->ssids[i];
5637 	} else {
5638 		arg->scan_f_passive = 1;
5639 	}
5640 
5641 	if (n_channels) {
5642 		arg->num_chan = n_channels;
5643 		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
5644 					 GFP_KERNEL);
5645 		if (!arg->chan_list) {
5646 			ret = -ENOMEM;
5647 			goto exit;
5648 		}
5649 
5650 		for (i = 0; i < arg->num_chan; i++)
5651 			arg->chan_list[i] = chan_list[i]->center_freq;
5652 	}
5653 
5654 	ret = ath12k_start_scan(ar, arg);
5655 	if (ret) {
5656 		if (ret == -EBUSY)
5657 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5658 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5659 		else
5660 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5661 
5662 		spin_lock_bh(&ar->data_lock);
5663 		ar->scan.state = ATH12K_SCAN_IDLE;
5664 		spin_unlock_bh(&ar->data_lock);
5665 		goto exit;
5666 	}
5667 
5668 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5669 
5670 	/* Add a margin to account for event/command processing */
5671 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5672 				     msecs_to_jiffies(arg->max_scan_time +
5673 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5674 
5675 exit:
5676 	if (arg) {
5677 		kfree(arg->chan_list);
5678 		kfree(arg->extraie.ptr);
5679 		kfree(arg);
5680 	}
5681 
5682 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5683 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5684 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5685 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5686 
5687 	return ret;
5688 }
5689 
ath12k_mac_op_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)5690 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5691 			  struct ieee80211_vif *vif,
5692 			  struct ieee80211_scan_request *hw_req)
5693 {
5694 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5695 	struct ieee80211_channel **chan_list, *chan;
5696 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5697 	unsigned long links_map, link_id;
5698 	struct ath12k_link_vif *arvif;
5699 	struct ath12k *ar, *scan_ar;
5700 	int i, j, ret = 0;
5701 
5702 	lockdep_assert_wiphy(hw->wiphy);
5703 
5704 	chan_list = kzalloc_objs(*chan_list, hw_req->req.n_channels);
5705 	if (!chan_list)
5706 		return -ENOMEM;
5707 
5708 	/* There could be channels that belong to multiple underlying radio
5709 	 * in same scan request as mac80211 sees it as single band. In that
5710 	 * case split the hw_req based on frequency range and schedule scans to
5711 	 * corresponding radio.
5712 	 */
5713 	for_each_ar(ah, ar, i) {
5714 		int n_chans = 0;
5715 
5716 		for (j = 0; j < hw_req->req.n_channels; j++) {
5717 			chan = hw_req->req.channels[j];
5718 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5719 								chan->center_freq);
5720 			if (!scan_ar) {
5721 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5722 					       chan->center_freq);
5723 				ret = -EINVAL;
5724 				goto abort;
5725 			}
5726 			if (ar != scan_ar)
5727 				continue;
5728 
5729 			chan_list[n_chans++] = chan;
5730 		}
5731 		if (n_chans) {
5732 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5733 							  chan_list, ar);
5734 			if (ret)
5735 				goto abort;
5736 		}
5737 	}
5738 abort:
5739 	/* If any of the parallel scans initiated fails, abort all and
5740 	 * remove the scan interfaces created. Return complete scan
5741 	 * failure as mac80211 assumes this as single scan request.
5742 	 */
5743 	if (ret) {
5744 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5745 		links_map = ahvif->links_map;
5746 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5747 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5748 			if (!arvif)
5749 				continue;
5750 
5751 			ar = arvif->ar;
5752 			if (ar->scan.arvif == arvif) {
5753 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5754 				spin_lock_bh(&ar->data_lock);
5755 				ar->scan.arvif = NULL;
5756 				ar->scan.state = ATH12K_SCAN_IDLE;
5757 				ar->scan_channel = NULL;
5758 				ar->scan.roc_freq = 0;
5759 				spin_unlock_bh(&ar->data_lock);
5760 			}
5761 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5762 				ath12k_mac_remove_link_interface(hw, arvif);
5763 				ath12k_mac_unassign_link_vif(arvif);
5764 			}
5765 		}
5766 	}
5767 	kfree(chan_list);
5768 	return ret;
5769 }
5770 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5771 
ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5772 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5773 				  struct ieee80211_vif *vif)
5774 {
5775 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5776 	unsigned long link_id, links_map = ahvif->links_map;
5777 	struct ath12k_link_vif *arvif;
5778 	struct ath12k *ar;
5779 
5780 	lockdep_assert_wiphy(hw->wiphy);
5781 
5782 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5783 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5784 		if (!arvif || !arvif->is_created ||
5785 		    arvif->ar->scan.arvif != arvif)
5786 			continue;
5787 
5788 		ar = arvif->ar;
5789 
5790 		ath12k_scan_abort(ar);
5791 
5792 		cancel_delayed_work_sync(&ar->scan.timeout);
5793 	}
5794 }
5795 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5796 
ath12k_install_key(struct ath12k_link_vif * arvif,struct ieee80211_key_conf * key,enum set_key_cmd cmd,const u8 * macaddr,u32 flags)5797 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5798 			      struct ieee80211_key_conf *key,
5799 			      enum set_key_cmd cmd,
5800 			      const u8 *macaddr, u32 flags)
5801 {
5802 	int ret;
5803 	struct ath12k *ar = arvif->ar;
5804 	struct wmi_vdev_install_key_arg arg = {
5805 		.vdev_id = arvif->vdev_id,
5806 		.key_idx = key->keyidx,
5807 		.key_len = key->keylen,
5808 		.key_data = key->key,
5809 		.key_flags = flags,
5810 		.ieee80211_key_cipher = key->cipher,
5811 		.macaddr = macaddr,
5812 	};
5813 	struct ath12k_vif *ahvif = arvif->ahvif;
5814 
5815 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5816 
5817 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5818 		return 0;
5819 
5820 	if (cmd == DISABLE_KEY) {
5821 		/* TODO: Check if FW expects  value other than NONE for del */
5822 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5823 		arg.key_len = 0;
5824 		arg.key_data = NULL;
5825 		goto check_order;
5826 	}
5827 
5828 	switch (key->cipher) {
5829 	case WLAN_CIPHER_SUITE_CCMP:
5830 	case WLAN_CIPHER_SUITE_CCMP_256:
5831 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5832 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5833 		break;
5834 	case WLAN_CIPHER_SUITE_TKIP:
5835 		arg.key_cipher = WMI_CIPHER_TKIP;
5836 		arg.key_txmic_len = 8;
5837 		arg.key_rxmic_len = 8;
5838 		break;
5839 	case WLAN_CIPHER_SUITE_GCMP:
5840 	case WLAN_CIPHER_SUITE_GCMP_256:
5841 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5842 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5843 		break;
5844 	case WLAN_CIPHER_SUITE_AES_CMAC:
5845 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5846 		break;
5847 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5848 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5849 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5850 		break;
5851 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5852 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5853 		break;
5854 	default:
5855 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5856 		return -EOPNOTSUPP;
5857 	}
5858 
5859 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5860 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5861 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5862 
5863 check_order:
5864 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5865 	    arg.key_flags == WMI_KEY_GROUP) {
5866 		if (cmd == SET_KEY) {
5867 			if (arvif->pairwise_key_done) {
5868 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5869 					   "vdev %u pairwise key done, go install group key\n",
5870 					   arg.vdev_id);
5871 				goto install;
5872 			} else {
5873 				/* WCN7850 firmware requires pairwise key to be installed
5874 				 * before group key. In case group key comes first, cache
5875 				 * it and return. Will revisit it once pairwise key gets
5876 				 * installed.
5877 				 */
5878 				arvif->group_key = arg;
5879 				arvif->group_key_valid = true;
5880 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5881 					   "vdev %u group key before pairwise key, cache and skip\n",
5882 					   arg.vdev_id);
5883 
5884 				ret = 0;
5885 				goto out;
5886 			}
5887 		} else {
5888 			arvif->group_key_valid = false;
5889 		}
5890 	}
5891 
5892 install:
5893 	reinit_completion(&ar->install_key_done);
5894 
5895 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5896 	if (ret)
5897 		return ret;
5898 
5899 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5900 		return -ETIMEDOUT;
5901 
5902 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5903 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5904 
5905 	if (ar->install_key_status) {
5906 		ret = -EINVAL;
5907 		goto out;
5908 	}
5909 
5910 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5911 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5912 		if (cmd == SET_KEY) {
5913 			arvif->pairwise_key_done = true;
5914 			if (arvif->group_key_valid) {
5915 				/* Install cached GTK */
5916 				arvif->group_key_valid = false;
5917 				arg = arvif->group_key;
5918 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5919 					   "vdev %u pairwise key done, group key ready, go install\n",
5920 					   arg.vdev_id);
5921 				goto install;
5922 			}
5923 		} else {
5924 			arvif->pairwise_key_done = false;
5925 		}
5926 	}
5927 
5928 out:
5929 	if (ret) {
5930 		/* In case of failure userspace may not do DISABLE_KEY
5931 		 * but triggers re-connection directly, so manually reset
5932 		 * status here.
5933 		 */
5934 		arvif->group_key_valid = false;
5935 		arvif->pairwise_key_done = false;
5936 	}
5937 
5938 	return ret;
5939 }
5940 
ath12k_clear_peer_keys(struct ath12k_link_vif * arvif,const u8 * addr)5941 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5942 				  const u8 *addr)
5943 {
5944 	struct ath12k *ar = arvif->ar;
5945 	struct ath12k_base *ab = ar->ab;
5946 	struct ath12k_dp_link_peer *peer;
5947 	int first_errno = 0;
5948 	int ret;
5949 	int i, len;
5950 	u32 flags = 0;
5951 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5952 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
5953 
5954 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5955 
5956 	spin_lock_bh(&dp->dp_lock);
5957 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
5958 	if (!peer || !peer->dp_peer) {
5959 		spin_unlock_bh(&dp->dp_lock);
5960 		return -ENOENT;
5961 	}
5962 
5963 	len = ARRAY_SIZE(peer->dp_peer->keys);
5964 	for (i = 0; i < len; i++) {
5965 		if (!peer->dp_peer->keys[i])
5966 			continue;
5967 
5968 		keys[i] = peer->dp_peer->keys[i];
5969 		peer->dp_peer->keys[i] = NULL;
5970 	}
5971 
5972 	spin_unlock_bh(&dp->dp_lock);
5973 
5974 	for (i = 0; i < len; i++) {
5975 		if (!keys[i])
5976 			continue;
5977 
5978 		/* key flags are not required to delete the key */
5979 		ret = ath12k_install_key(arvif, keys[i],
5980 					 DISABLE_KEY, addr, flags);
5981 		if (ret < 0 && first_errno == 0)
5982 			first_errno = ret;
5983 
5984 		if (ret < 0)
5985 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5986 				    i, ret);
5987 	}
5988 
5989 	return first_errno;
5990 }
5991 
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)5992 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5993 			      struct ath12k_link_vif *arvif,
5994 			      struct ath12k_link_sta *arsta,
5995 			      struct ieee80211_key_conf *key)
5996 {
5997 	struct ieee80211_sta *sta = NULL;
5998 	struct ath12k_base *ab = ar->ab;
5999 	struct ath12k_dp_link_peer *peer;
6000 	struct ath12k_sta *ahsta;
6001 	const u8 *peer_addr;
6002 	int ret;
6003 	u32 flags = 0;
6004 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6005 
6006 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6007 
6008 	if (arsta)
6009 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6010 
6011 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6012 		return 1;
6013 
6014 	if (sta)
6015 		peer_addr = arsta->addr;
6016 	else
6017 		peer_addr = arvif->bssid;
6018 
6019 	key->hw_key_idx = key->keyidx;
6020 
6021 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6022 	 * we already hold wiphy lock. we just make sure its there now.
6023 	 */
6024 	spin_lock_bh(&dp->dp_lock);
6025 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6026 							 peer_addr);
6027 	if (!peer || !peer->dp_peer) {
6028 		spin_unlock_bh(&dp->dp_lock);
6029 
6030 		if (cmd == SET_KEY) {
6031 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6032 				    peer_addr);
6033 			return -EOPNOTSUPP;
6034 		}
6035 
6036 		/* if the peer doesn't exist there is no key to disable
6037 		 * anymore
6038 		 */
6039 		return 0;
6040 	}
6041 
6042 	spin_unlock_bh(&dp->dp_lock);
6043 
6044 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6045 		flags = WMI_KEY_PAIRWISE;
6046 	else
6047 		flags = WMI_KEY_GROUP;
6048 
6049 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6050 	if (ret) {
6051 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6052 		return ret;
6053 	}
6054 
6055 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6056 	if (ret) {
6057 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6058 		return ret;
6059 	}
6060 
6061 	spin_lock_bh(&dp->dp_lock);
6062 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6063 							 peer_addr);
6064 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6065 		peer->dp_peer->keys[key->keyidx] = key;
6066 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6067 			peer->dp_peer->ucast_keyidx = key->keyidx;
6068 			peer->dp_peer->sec_type =
6069 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6070 		} else {
6071 			peer->dp_peer->mcast_keyidx = key->keyidx;
6072 			peer->dp_peer->sec_type_grp =
6073 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6074 		}
6075 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6076 		peer->dp_peer->keys[key->keyidx] = NULL;
6077 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6078 			peer->dp_peer->ucast_keyidx = 0;
6079 		else
6080 			peer->dp_peer->mcast_keyidx = 0;
6081 	} else if (!peer)
6082 		/* impossible unless FW goes crazy */
6083 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6084 
6085 	if (sta) {
6086 		ahsta = ath12k_sta_to_ahsta(sta);
6087 
6088 		switch (key->cipher) {
6089 		case WLAN_CIPHER_SUITE_TKIP:
6090 		case WLAN_CIPHER_SUITE_CCMP:
6091 		case WLAN_CIPHER_SUITE_CCMP_256:
6092 		case WLAN_CIPHER_SUITE_GCMP:
6093 		case WLAN_CIPHER_SUITE_GCMP_256:
6094 			if (cmd == SET_KEY)
6095 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6096 			else
6097 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6098 			break;
6099 		default:
6100 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6101 			break;
6102 		}
6103 	}
6104 
6105 	spin_unlock_bh(&dp->dp_lock);
6106 
6107 	return 0;
6108 }
6109 
ath12k_mac_update_key_cache(struct ath12k_vif_cache * cache,enum set_key_cmd cmd,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)6110 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6111 				       enum set_key_cmd cmd,
6112 				       struct ieee80211_sta *sta,
6113 				       struct ieee80211_key_conf *key)
6114 {
6115 	struct ath12k_key_conf *key_conf, *tmp;
6116 
6117 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6118 		if (key_conf->key != key)
6119 			continue;
6120 
6121 		/* If SET key entry is already present in cache, nothing to do,
6122 		 * just return
6123 		 */
6124 		if (cmd == SET_KEY)
6125 			return 0;
6126 
6127 		/* DEL key for an old SET key which driver hasn't flushed yet.
6128 		 */
6129 		list_del(&key_conf->list);
6130 		kfree(key_conf);
6131 	}
6132 
6133 	if (cmd == SET_KEY) {
6134 		key_conf = kzalloc_obj(*key_conf);
6135 
6136 		if (!key_conf)
6137 			return -ENOMEM;
6138 
6139 		key_conf->cmd = cmd;
6140 		key_conf->sta = sta;
6141 		key_conf->key = key;
6142 		list_add_tail(&key_conf->list,
6143 			      &cache->key_conf.list);
6144 	}
6145 
6146 	return 0;
6147 }
6148 
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)6149 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6150 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6151 			  struct ieee80211_key_conf *key)
6152 {
6153 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6154 	struct ath12k_link_vif *arvif;
6155 	struct ath12k_link_sta *arsta = NULL;
6156 	struct ath12k_vif_cache *cache;
6157 	struct ath12k_sta *ahsta;
6158 	unsigned long links;
6159 	u8 link_id;
6160 	int ret;
6161 
6162 	lockdep_assert_wiphy(hw->wiphy);
6163 
6164 	/* IGTK needs to be done in host software */
6165 	if (key->keyidx == 4 || key->keyidx == 5)
6166 		return 1;
6167 
6168 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6169 		return -ENOSPC;
6170 
6171 	if (sta) {
6172 		ahsta = ath12k_sta_to_ahsta(sta);
6173 
6174 		/* For an ML STA Pairwise key is same for all associated link Stations,
6175 		 * hence do set key for all link STAs which are active.
6176 		 */
6177 		if (sta->mlo) {
6178 			links = ahsta->links_map;
6179 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6180 				arvif = wiphy_dereference(hw->wiphy,
6181 							  ahvif->link[link_id]);
6182 				arsta = wiphy_dereference(hw->wiphy,
6183 							  ahsta->link[link_id]);
6184 
6185 				if (WARN_ON(!arvif || !arsta))
6186 					/* arvif and arsta are expected to be valid when
6187 					 * STA is present.
6188 					 */
6189 					continue;
6190 
6191 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6192 							 arsta, key);
6193 				if (ret)
6194 					break;
6195 			}
6196 
6197 			return 0;
6198 		}
6199 
6200 		arsta = &ahsta->deflink;
6201 		arvif = arsta->arvif;
6202 		if (WARN_ON(!arvif))
6203 			return -EINVAL;
6204 
6205 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6206 		if (ret)
6207 			return ret;
6208 
6209 		return 0;
6210 	}
6211 
6212 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6213 		link_id = key->link_id;
6214 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6215 	} else {
6216 		link_id = 0;
6217 		arvif = &ahvif->deflink;
6218 	}
6219 
6220 	if (!arvif || !arvif->is_created) {
6221 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6222 		if (!cache)
6223 			return -ENOSPC;
6224 
6225 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6226 		if (ret)
6227 			return ret;
6228 
6229 		return 0;
6230 	}
6231 
6232 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6233 	if (ret)
6234 		return ret;
6235 
6236 	return 0;
6237 }
6238 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6239 
6240 static int
ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6241 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6242 				      enum nl80211_band band,
6243 				      const struct cfg80211_bitrate_mask *mask)
6244 {
6245 	int num_rates = 0;
6246 	int i;
6247 
6248 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6249 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6250 
6251 	return num_rates;
6252 }
6253 
6254 static int
ath12k_mac_bitrate_mask_num_he_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6255 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6256 				     enum nl80211_band band,
6257 				     const struct cfg80211_bitrate_mask *mask)
6258 {
6259 	int num_rates = 0;
6260 	int i;
6261 
6262 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6263 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6264 
6265 	return num_rates;
6266 }
6267 
6268 static int
ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6269 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6270 				      enum nl80211_band band,
6271 				      const struct cfg80211_bitrate_mask *mask)
6272 {
6273 	int num_rates = 0;
6274 	int i;
6275 
6276 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6277 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6278 
6279 	return num_rates;
6280 }
6281 
6282 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)6283 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6284 				   struct ath12k_link_sta *arsta,
6285 				   const struct cfg80211_bitrate_mask *mask,
6286 				   enum nl80211_band band)
6287 {
6288 	struct ath12k *ar = arvif->ar;
6289 	u8 vht_rate, nss;
6290 	u32 rate_code;
6291 	int ret, i;
6292 
6293 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6294 
6295 	nss = 0;
6296 
6297 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6298 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6299 			nss = i + 1;
6300 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6301 		}
6302 	}
6303 
6304 	if (!nss) {
6305 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6306 			    arsta->addr);
6307 		return -EINVAL;
6308 	}
6309 
6310 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6311 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6312 		   arsta->addr);
6313 
6314 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6315 					WMI_RATE_PREAMBLE_VHT);
6316 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6317 					arvif->vdev_id,
6318 					WMI_PEER_PARAM_FIXED_RATE,
6319 					rate_code);
6320 	if (ret)
6321 		ath12k_warn(ar->ab,
6322 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6323 			     arsta->addr, rate_code, ret);
6324 
6325 	return ret;
6326 }
6327 
6328 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)6329 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6330 				  struct ath12k_link_sta *arsta,
6331 				  const struct cfg80211_bitrate_mask *mask,
6332 				  enum nl80211_band band)
6333 {
6334 	struct ath12k *ar = arvif->ar;
6335 	u8 he_rate, nss;
6336 	u32 rate_code;
6337 	int ret, i;
6338 	struct ath12k_sta *ahsta = arsta->ahsta;
6339 	struct ieee80211_sta *sta;
6340 
6341 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6342 
6343 	sta = ath12k_ahsta_to_sta(ahsta);
6344 	nss = 0;
6345 
6346 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6347 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6348 			nss = i + 1;
6349 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6350 		}
6351 	}
6352 
6353 	if (!nss) {
6354 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6355 			    arsta->addr);
6356 		return -EINVAL;
6357 	}
6358 
6359 	/* Avoid updating invalid nss as fixed rate*/
6360 	if (nss > sta->deflink.rx_nss)
6361 		return -EINVAL;
6362 
6363 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6364 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6365 		   arsta->addr);
6366 
6367 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6368 					WMI_RATE_PREAMBLE_HE);
6369 
6370 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6371 					arvif->vdev_id,
6372 					WMI_PEER_PARAM_FIXED_RATE,
6373 					rate_code);
6374 	if (ret)
6375 		ath12k_warn(ar->ab,
6376 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6377 			    arsta->addr, rate_code, ret);
6378 
6379 	return ret;
6380 }
6381 
6382 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)6383 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6384 				   struct ath12k_link_sta *arsta,
6385 				   const struct cfg80211_bitrate_mask *mask,
6386 				   enum nl80211_band band)
6387 {
6388 	struct ath12k_sta *ahsta = arsta->ahsta;
6389 	struct ath12k *ar = arvif->ar;
6390 	struct ieee80211_sta *sta;
6391 	struct ieee80211_link_sta *link_sta;
6392 	u8 eht_rate, nss = 0;
6393 	u32 rate_code;
6394 	int ret, i;
6395 
6396 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6397 
6398 	sta = ath12k_ahsta_to_sta(ahsta);
6399 
6400 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6401 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6402 			nss = i + 1;
6403 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6404 		}
6405 	}
6406 
6407 	if (!nss) {
6408 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6409 			    arsta->addr);
6410 		return -EINVAL;
6411 	}
6412 
6413 	/* Avoid updating invalid nss as fixed rate*/
6414 	link_sta = ath12k_mac_get_link_sta(arsta);
6415 	if (!link_sta || nss > link_sta->rx_nss) {
6416 		ath12k_warn(ar->ab,
6417 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6418 			    sta->addr, arsta->link_id, nss);
6419 		return -EINVAL;
6420 	}
6421 
6422 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6423 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6424 		   arsta->addr);
6425 
6426 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6427 					WMI_RATE_PREAMBLE_EHT);
6428 
6429 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6430 					arvif->vdev_id,
6431 					WMI_PEER_PARAM_FIXED_RATE,
6432 					rate_code);
6433 	if (ret)
6434 		ath12k_warn(ar->ab,
6435 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6436 			    arsta->addr, rate_code, ret);
6437 
6438 	return ret;
6439 }
6440 
ath12k_mac_station_assoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,bool reassoc)6441 static int ath12k_mac_station_assoc(struct ath12k *ar,
6442 				    struct ath12k_link_vif *arvif,
6443 				    struct ath12k_link_sta *arsta,
6444 				    bool reassoc)
6445 {
6446 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6447 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6448 	struct ieee80211_link_sta *link_sta;
6449 	int ret;
6450 	struct cfg80211_chan_def def;
6451 	enum nl80211_band band;
6452 	struct cfg80211_bitrate_mask *mask;
6453 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6454 	u8 link_id = arvif->link_id;
6455 
6456 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6457 
6458 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6459 		return -EPERM;
6460 
6461 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6462 		return -EINVAL;
6463 
6464 	band = def.chan->band;
6465 	mask = &arvif->bitrate_mask;
6466 
6467 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6468 		kzalloc_obj(*peer_arg);
6469 	if (!peer_arg)
6470 		return -ENOMEM;
6471 
6472 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6473 
6474 	if (peer_arg->peer_nss < 1) {
6475 		ath12k_warn(ar->ab,
6476 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6477 		return -EINVAL;
6478 	}
6479 
6480 	peer_arg->is_assoc = true;
6481 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6482 	if (ret) {
6483 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6484 			    arsta->addr, arvif->vdev_id, ret);
6485 		return ret;
6486 	}
6487 
6488 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
6489 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6490 			    arsta->addr, arvif->vdev_id);
6491 		return -ETIMEDOUT;
6492 	}
6493 
6494 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6495 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6496 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6497 
6498 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6499 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6500 	 * specific fixed param.
6501 	 * Note that all other rates and NSS will be disabled for this peer.
6502 	 */
6503 	link_sta = ath12k_mac_get_link_sta(arsta);
6504 	if (!link_sta) {
6505 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6506 		return -EINVAL;
6507 	}
6508 
6509 	spin_lock_bh(&ar->data_lock);
6510 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6511 	arsta->bw_prev = link_sta->bandwidth;
6512 	spin_unlock_bh(&ar->data_lock);
6513 
6514 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6515 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6516 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6517 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6518 		if (ret)
6519 			return ret;
6520 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6521 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6522 		if (ret)
6523 			return ret;
6524 	}
6525 
6526 	/* Re-assoc is run only to update supported rates for given station. It
6527 	 * doesn't make much sense to reconfigure the peer completely.
6528 	 */
6529 	if (reassoc)
6530 		return 0;
6531 
6532 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6533 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6534 	if (ret) {
6535 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6536 			    arvif->vdev_id, ret);
6537 		return ret;
6538 	}
6539 
6540 	if (!sta->wme) {
6541 		arvif->num_legacy_stations++;
6542 		ret = ath12k_recalc_rtscts_prot(arvif);
6543 		if (ret)
6544 			return ret;
6545 	}
6546 
6547 	if (sta->wme && sta->uapsd_queues) {
6548 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6549 		if (ret) {
6550 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6551 				    arsta->addr, arvif->vdev_id, ret);
6552 			return ret;
6553 		}
6554 	}
6555 
6556 	return 0;
6557 }
6558 
ath12k_mac_station_disassoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6559 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6560 				       struct ath12k_link_vif *arvif,
6561 				       struct ath12k_link_sta *arsta)
6562 {
6563 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6564 
6565 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6566 
6567 	if (!sta->wme) {
6568 		arvif->num_legacy_stations--;
6569 		return ath12k_recalc_rtscts_prot(arvif);
6570 	}
6571 
6572 	return 0;
6573 }
6574 
ath12k_sta_rc_update_wk(struct wiphy * wiphy,struct wiphy_work * wk)6575 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6576 {
6577 	struct ieee80211_link_sta *link_sta;
6578 	struct ath12k *ar;
6579 	struct ath12k_link_vif *arvif;
6580 	struct ieee80211_sta *sta;
6581 	struct cfg80211_chan_def def;
6582 	enum nl80211_band band;
6583 	const u8 *ht_mcs_mask;
6584 	const u16 *vht_mcs_mask;
6585 	const u16 *he_mcs_mask;
6586 	const u16 *eht_mcs_mask;
6587 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6588 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6589 	const struct cfg80211_bitrate_mask *mask;
6590 	enum wmi_phy_mode peer_phymode;
6591 	struct ath12k_link_sta *arsta;
6592 	struct ieee80211_vif *vif;
6593 
6594 	lockdep_assert_wiphy(wiphy);
6595 
6596 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6597 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6598 	arvif = arsta->arvif;
6599 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6600 	ar = arvif->ar;
6601 
6602 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6603 		return;
6604 
6605 	band = def.chan->band;
6606 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6607 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6608 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6609 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6610 
6611 	spin_lock_bh(&ar->data_lock);
6612 
6613 	changed = arsta->changed;
6614 	arsta->changed = 0;
6615 
6616 	bw = arsta->bw;
6617 	bw_prev = arsta->bw_prev;
6618 	nss = arsta->nss;
6619 	smps = arsta->smps;
6620 
6621 	spin_unlock_bh(&ar->data_lock);
6622 
6623 	nss = max_t(u32, 1, nss);
6624 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6625 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6626 		       ath12k_mac_max_he_nss(he_mcs_mask));
6627 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6628 	nss = min(nss, mac_nss);
6629 
6630 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6631 					kzalloc_obj(*peer_arg);
6632 	if (!peer_arg)
6633 		return;
6634 
6635 	if (changed & IEEE80211_RC_BW_CHANGED) {
6636 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6637 		peer_phymode = peer_arg->peer_phymode;
6638 
6639 		if (bw > bw_prev) {
6640 			/* Phymode shows maximum supported channel width, if we
6641 			 * upgrade bandwidth then due to sanity check of firmware,
6642 			 * we have to send WMI_PEER_PHYMODE followed by
6643 			 * WMI_PEER_CHWIDTH
6644 			 */
6645 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6646 				   arsta->addr, bw, bw_prev);
6647 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6648 							arvif->vdev_id, WMI_PEER_PHYMODE,
6649 							peer_phymode);
6650 			if (err) {
6651 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6652 					    arsta->addr, peer_phymode, err);
6653 				return;
6654 			}
6655 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6656 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6657 							bw);
6658 			if (err)
6659 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6660 					    arsta->addr, bw, err);
6661 		} else {
6662 			/* When we downgrade bandwidth this will conflict with phymode
6663 			 * and cause to trigger firmware crash. In this case we send
6664 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6665 			 */
6666 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6667 				   arsta->addr, bw, bw_prev);
6668 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6669 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6670 							bw);
6671 			if (err) {
6672 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6673 					    arsta->addr, bw, err);
6674 				return;
6675 			}
6676 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6677 							arvif->vdev_id, WMI_PEER_PHYMODE,
6678 							peer_phymode);
6679 			if (err)
6680 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6681 					    arsta->addr, peer_phymode, err);
6682 		}
6683 	}
6684 
6685 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6686 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6687 			   arsta->addr, nss);
6688 
6689 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6690 						WMI_PEER_NSS, nss);
6691 		if (err)
6692 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6693 				    arsta->addr, nss, err);
6694 	}
6695 
6696 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6697 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6698 			   arsta->addr, smps);
6699 
6700 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6701 						WMI_PEER_MIMO_PS_STATE, smps);
6702 		if (err)
6703 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6704 				    arsta->addr, smps, err);
6705 	}
6706 
6707 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6708 		mask = &arvif->bitrate_mask;
6709 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6710 								      mask);
6711 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6712 								    mask);
6713 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6714 								      mask);
6715 
6716 		/* Peer_assoc_prepare will reject vht rates in
6717 		 * bitrate_mask if its not available in range format and
6718 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6719 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6720 		 * But, Single rate in VHT mask can be set as per-peer
6721 		 * fixed rate. But even if any HT rates are configured in
6722 		 * the bitrate mask, device will not switch to those rates
6723 		 * when per-peer Fixed rate is set.
6724 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6725 		 * across HT/VHT and for multiple VHT MCS support.
6726 		 */
6727 		link_sta = ath12k_mac_get_link_sta(arsta);
6728 		if (!link_sta) {
6729 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6730 				    sta->addr, arsta->link_id);
6731 			return;
6732 		}
6733 
6734 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6735 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6736 							   band);
6737 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6738 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6739 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6740 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6741 								 mask, band);
6742 			if (err) {
6743 				ath12k_warn(ar->ab,
6744 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6745 					    arsta->addr, err);
6746 				return;
6747 			}
6748 		} else {
6749 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6750 			 * rate is provided in the new bitrate mask we set the
6751 			 * other rates using peer_assoc command. Also clear
6752 			 * the peer fixed rate settings as it has higher proprity
6753 			 * than peer assoc
6754 			 */
6755 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6756 							arvif->vdev_id,
6757 							WMI_PEER_PARAM_FIXED_RATE,
6758 							WMI_FIXED_RATE_NONE);
6759 			if (err)
6760 				ath12k_warn(ar->ab,
6761 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6762 					    arsta->addr, err);
6763 
6764 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6765 						  peer_arg, true);
6766 
6767 			peer_arg->is_assoc = false;
6768 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6769 			if (err)
6770 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6771 					    arsta->addr, arvif->vdev_id, err);
6772 
6773 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6774 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6775 					    arsta->addr, arvif->vdev_id);
6776 		}
6777 	}
6778 }
6779 
ath12k_mac_free_unassign_link_sta(struct ath12k_hw * ah,struct ath12k_sta * ahsta,u8 link_id)6780 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6781 					      struct ath12k_sta *ahsta,
6782 					      u8 link_id)
6783 {
6784 	struct ath12k_link_sta *arsta;
6785 
6786 	lockdep_assert_wiphy(ah->hw->wiphy);
6787 
6788 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6789 		return;
6790 
6791 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6792 	if (WARN_ON(!arsta))
6793 		return;
6794 
6795 	ahsta->links_map &= ~BIT(link_id);
6796 	ahsta->free_logical_link_idx_map |= BIT(arsta->link_idx);
6797 
6798 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6799 	synchronize_rcu();
6800 
6801 	if (arsta == &ahsta->deflink) {
6802 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6803 		arsta->ahsta = NULL;
6804 		arsta->arvif = NULL;
6805 		return;
6806 	}
6807 
6808 	kfree(arsta);
6809 }
6810 
ath12k_mac_inc_num_stations(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6811 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6812 				       struct ath12k_link_sta *arsta)
6813 {
6814 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6815 	struct ath12k *ar = arvif->ar;
6816 
6817 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6818 
6819 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6820 		return 0;
6821 
6822 	if (ar->num_stations >= ar->max_num_stations)
6823 		return -ENOBUFS;
6824 
6825 	ar->num_stations++;
6826 	arvif->num_stations++;
6827 
6828 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6829 		   "mac station %pM connected to vdev %u num_stations %u\n",
6830 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6831 
6832 	return 0;
6833 }
6834 
ath12k_mac_dec_num_stations(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6835 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6836 					struct ath12k_link_sta *arsta)
6837 {
6838 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6839 	struct ath12k *ar = arvif->ar;
6840 
6841 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6842 
6843 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6844 		return;
6845 
6846 	ar->num_stations--;
6847 
6848 	if (arvif->num_stations) {
6849 		arvif->num_stations--;
6850 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6851 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6852 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6853 	} else {
6854 		ath12k_warn(ar->ab,
6855 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6856 			    arsta->addr, arvif->vdev_id);
6857 	}
6858 }
6859 
ath12k_mac_station_post_remove(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6860 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6861 					   struct ath12k_link_vif *arvif,
6862 					   struct ath12k_link_sta *arsta)
6863 {
6864 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6865 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6866 	struct ath12k_dp_link_peer *peer;
6867 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6868 
6869 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6870 
6871 	ath12k_mac_dec_num_stations(arvif, arsta);
6872 
6873 	spin_lock_bh(&dp->dp_lock);
6874 
6875 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6876 							 arsta->addr);
6877 	if (peer && peer->sta == sta) {
6878 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6879 			    vif->addr, arvif->vdev_id);
6880 		peer->sta = NULL;
6881 
6882 		ath12k_dp_link_peer_free(peer);
6883 		ar->num_peers--;
6884 	}
6885 
6886 	spin_unlock_bh(&dp->dp_lock);
6887 }
6888 
ath12k_mac_station_unauthorize(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6889 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6890 					  struct ath12k_link_vif *arvif,
6891 					  struct ath12k_link_sta *arsta)
6892 {
6893 	struct ath12k_dp_link_peer *peer;
6894 	int ret;
6895 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6896 
6897 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6898 
6899 	spin_lock_bh(&dp->dp_lock);
6900 
6901 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6902 							 arsta->addr);
6903 	if (peer)
6904 		peer->is_authorized = false;
6905 
6906 	spin_unlock_bh(&dp->dp_lock);
6907 
6908 	/* Driver must clear the keys during the state change from
6909 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6910 	 * returning from here, mac80211 is going to delete the keys
6911 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6912 	 * not retain stale key references after mac80211 deletes the keys.
6913 	 */
6914 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6915 	if (ret) {
6916 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6917 			    arvif->vdev_id, ret);
6918 		return ret;
6919 	}
6920 
6921 	return 0;
6922 }
6923 
ath12k_mac_station_authorize(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6924 static int ath12k_mac_station_authorize(struct ath12k *ar,
6925 					struct ath12k_link_vif *arvif,
6926 					struct ath12k_link_sta *arsta)
6927 {
6928 	struct ath12k_dp_link_peer *peer;
6929 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6930 	int ret;
6931 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6932 
6933 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6934 
6935 	spin_lock_bh(&dp->dp_lock);
6936 
6937 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6938 							 arsta->addr);
6939 	if (peer)
6940 		peer->is_authorized = true;
6941 
6942 	spin_unlock_bh(&dp->dp_lock);
6943 
6944 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
6945 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6946 						arvif->vdev_id,
6947 						WMI_PEER_AUTHORIZE,
6948 						1);
6949 		if (ret) {
6950 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
6951 				    arsta->addr, arvif->vdev_id, ret);
6952 			return ret;
6953 		}
6954 	}
6955 
6956 	return 0;
6957 }
6958 
ath12k_mac_station_remove(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6959 static int ath12k_mac_station_remove(struct ath12k *ar,
6960 				     struct ath12k_link_vif *arvif,
6961 				     struct ath12k_link_sta *arsta)
6962 {
6963 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6964 	struct ath12k_vif *ahvif = arvif->ahvif;
6965 	int ret = 0;
6966 	struct ath12k_link_sta *temp_arsta;
6967 
6968 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6969 
6970 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
6971 
6972 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6973 		ath12k_bss_disassoc(ar, arvif);
6974 		ret = ath12k_mac_vdev_stop(arvif);
6975 		if (ret)
6976 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
6977 				    arvif->vdev_id, ret);
6978 	}
6979 
6980 	if (sta->mlo)
6981 		return ret;
6982 
6983 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
6984 
6985 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6986 	if (ret)
6987 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
6988 			    arsta->addr, arvif->vdev_id);
6989 	else
6990 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
6991 			   arsta->addr, arvif->vdev_id);
6992 
6993 	ath12k_mac_station_post_remove(ar, arvif, arsta);
6994 
6995 	spin_lock_bh(&ar->ab->base_lock);
6996 
6997 	/* To handle roaming and split phy scenario */
6998 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
6999 	if (temp_arsta && temp_arsta->arvif->ar == ar)
7000 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7001 
7002 	spin_unlock_bh(&ar->ab->base_lock);
7003 
7004 	if (sta->valid_links)
7005 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
7006 						  arsta->ahsta, arsta->link_id);
7007 
7008 	return ret;
7009 }
7010 
ath12k_mac_station_add(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)7011 static int ath12k_mac_station_add(struct ath12k *ar,
7012 				  struct ath12k_link_vif *arvif,
7013 				  struct ath12k_link_sta *arsta)
7014 {
7015 	struct ath12k_base *ab = ar->ab;
7016 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7017 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7018 	struct ath12k_wmi_peer_create_arg peer_param = {};
7019 	int ret;
7020 	struct ath12k_link_sta *temp_arsta;
7021 
7022 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7023 
7024 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7025 	if (ret) {
7026 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7027 			    ar->max_num_stations);
7028 		goto exit;
7029 	}
7030 
7031 	spin_lock_bh(&ab->base_lock);
7032 
7033 	/*
7034 	 * In case of Split PHY and roaming scenario, pdev idx
7035 	 * might differ but both the pdev will share same rhash
7036 	 * table. In that case update the rhash table if link_sta is
7037 	 * already present
7038 	 */
7039 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7040 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7041 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7042 
7043 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7044 	spin_unlock_bh(&ab->base_lock);
7045 	if (ret) {
7046 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7047 			    arsta->addr, ret);
7048 		goto dec_num_station;
7049 	}
7050 
7051 	peer_param.vdev_id = arvif->vdev_id;
7052 	peer_param.peer_addr = arsta->addr;
7053 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7054 	peer_param.ml_enabled = sta->mlo;
7055 
7056 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7057 	if (ret) {
7058 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7059 			    arsta->addr, arvif->vdev_id);
7060 		goto free_peer;
7061 	}
7062 
7063 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7064 		   arsta->addr, arvif->vdev_id);
7065 
7066 	if (ieee80211_vif_is_mesh(vif)) {
7067 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7068 						arvif->vdev_id,
7069 						WMI_PEER_USE_4ADDR, 1);
7070 		if (ret) {
7071 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7072 				    arsta->addr, ret);
7073 			goto free_peer;
7074 		}
7075 	}
7076 
7077 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7078 	if (ret) {
7079 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7080 			    arsta->addr, arvif->vdev_id, ret);
7081 		goto free_peer;
7082 	}
7083 
7084 	if (ab->hw_params->vdev_start_delay &&
7085 	    !arvif->is_started &&
7086 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7087 		ret = ath12k_start_vdev_delay(ar, arvif);
7088 		if (ret) {
7089 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7090 			goto free_peer;
7091 		}
7092 	}
7093 
7094 	return 0;
7095 
7096 free_peer:
7097 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7098 	spin_lock_bh(&ab->base_lock);
7099 	ath12k_link_sta_rhash_delete(ab, arsta);
7100 	spin_unlock_bh(&ab->base_lock);
7101 dec_num_station:
7102 	ath12k_mac_dec_num_stations(arvif, arsta);
7103 exit:
7104 	return ret;
7105 }
7106 
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)7107 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7108 				      struct ath12k_sta *ahsta,
7109 				      struct ath12k_link_sta *arsta,
7110 				      struct ath12k_vif *ahvif,
7111 				      u8 link_id)
7112 {
7113 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7114 	struct ieee80211_link_sta *link_sta;
7115 	struct ath12k_link_vif *arvif;
7116 	int link_idx;
7117 
7118 	lockdep_assert_wiphy(ah->hw->wiphy);
7119 
7120 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7121 		return -EINVAL;
7122 
7123 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7124 	if (!arvif)
7125 		return -EINVAL;
7126 
7127 	memset(arsta, 0, sizeof(*arsta));
7128 
7129 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7130 	if (!link_sta)
7131 		return -EINVAL;
7132 
7133 	ether_addr_copy(arsta->addr, link_sta->addr);
7134 
7135 	if (!ahsta->free_logical_link_idx_map)
7136 		return -ENOSPC;
7137 
7138 	/*
7139 	 * Allocate a logical link index by selecting the first available bit
7140 	 * from the free logical index map
7141 	 */
7142 	link_idx = __ffs(ahsta->free_logical_link_idx_map);
7143 	ahsta->free_logical_link_idx_map &= ~BIT(link_idx);
7144 	arsta->link_idx = link_idx;
7145 
7146 	arsta->link_id = link_id;
7147 	ahsta->links_map |= BIT(arsta->link_id);
7148 	arsta->arvif = arvif;
7149 	arsta->ahsta = ahsta;
7150 	ahsta->ahvif = ahvif;
7151 
7152 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7153 
7154 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7155 
7156 	return 0;
7157 }
7158 
ath12k_mac_ml_station_remove(struct ath12k_vif * ahvif,struct ath12k_sta * ahsta)7159 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7160 					 struct ath12k_sta *ahsta)
7161 {
7162 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7163 	struct ath12k_hw *ah = ahvif->ah;
7164 	struct ath12k_link_vif *arvif;
7165 	struct ath12k_link_sta *arsta;
7166 	unsigned long links;
7167 	struct ath12k *ar;
7168 	u8 link_id;
7169 
7170 	lockdep_assert_wiphy(ah->hw->wiphy);
7171 
7172 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7173 
7174 	/* validate link station removal and clear arsta links */
7175 	links = ahsta->links_map;
7176 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7177 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7178 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7179 		if (!arvif || !arsta)
7180 			continue;
7181 
7182 		ar = arvif->ar;
7183 
7184 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7185 
7186 		spin_lock_bh(&ar->ab->base_lock);
7187 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7188 		spin_unlock_bh(&ar->ab->base_lock);
7189 
7190 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7191 	}
7192 
7193 	if (sta->mlo) {
7194 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7195 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7196 	}
7197 }
7198 
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)7199 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7200 					    struct ath12k_link_vif *arvif,
7201 					    struct ath12k_link_sta *arsta,
7202 					    enum ieee80211_sta_state old_state,
7203 					    enum ieee80211_sta_state new_state)
7204 {
7205 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7206 	struct ieee80211_bss_conf *link_conf;
7207 	struct ath12k *ar = arvif->ar;
7208 	struct ath12k_reg_info *reg_info;
7209 	struct ath12k_base *ab = ar->ab;
7210 	int ret = 0;
7211 
7212 	lockdep_assert_wiphy(hw->wiphy);
7213 
7214 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7215 		   arsta->link_id, arsta->addr, old_state, new_state);
7216 
7217 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7218 	 * from driver
7219 	 */
7220 	if ((old_state == IEEE80211_STA_NONE &&
7221 	     new_state == IEEE80211_STA_NOTEXIST)) {
7222 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7223 		if (ret) {
7224 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7225 				    arsta->addr, arvif->vdev_id);
7226 			goto exit;
7227 		}
7228 	}
7229 
7230 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7231 	if (old_state == IEEE80211_STA_NOTEXIST &&
7232 	    new_state == IEEE80211_STA_NONE) {
7233 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7234 		if (ret)
7235 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7236 				    arsta->addr, arvif->vdev_id);
7237 
7238 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7239 	 * peer associated to AP/Mesh/ADHOC vif type.
7240 	 */
7241 	} else if (old_state == IEEE80211_STA_AUTH &&
7242 		   new_state == IEEE80211_STA_ASSOC &&
7243 		   (vif->type == NL80211_IFTYPE_AP ||
7244 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7245 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7246 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7247 		if (ret)
7248 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7249 				    arsta->addr);
7250 
7251 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7252 	 * authorized
7253 	 */
7254 	} else if (old_state == IEEE80211_STA_ASSOC &&
7255 		   new_state == IEEE80211_STA_AUTHORIZED) {
7256 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7257 		if (ret) {
7258 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7259 				    arsta->addr);
7260 			goto exit;
7261 		}
7262 
7263 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7264 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7265 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7266 			reg_info = ab->reg_info[ar->pdev_idx];
7267 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7268 			ath12k_hw_to_ah(hw)->regd_updated = false;
7269 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7270 						    link_conf->power_type);
7271 		}
7272 
7273 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7274 	 * deauthorize it.
7275 	 */
7276 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7277 		   new_state == IEEE80211_STA_ASSOC) {
7278 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7279 
7280 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7281 	 * AP/mesh/ADHOC vif type.
7282 	 */
7283 	} else if (old_state == IEEE80211_STA_ASSOC &&
7284 		   new_state == IEEE80211_STA_AUTH &&
7285 		   (vif->type == NL80211_IFTYPE_AP ||
7286 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7287 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7288 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7289 		if (ret)
7290 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7291 				    arsta->addr);
7292 	}
7293 
7294 exit:
7295 	return ret;
7296 }
7297 
ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg * freq_range,u32 freq,u8 mac_id)7298 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7299 				      u32 freq, u8 mac_id)
7300 {
7301 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7302 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7303 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7304 		freq <= freq_range[mac_id].high_5ghz_freq);
7305 }
7306 
7307 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)7308 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7309 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7310 					 u32 freq_link1, u32 freq_link2)
7311 {
7312 	u8 i;
7313 
7314 	for (i = 0; i < MAX_RADIOS; i++) {
7315 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7316 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7317 			return true;
7318 	}
7319 
7320 	return false;
7321 }
7322 
ath12k_mac_is_hw_dbs_capable(struct ath12k_base * ab)7323 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7324 {
7325 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7326 			ab->wmi_ab.svc_map) &&
7327 	       ab->wmi_ab.hw_mode_info.support_dbs;
7328 }
7329 
ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)7330 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7331 					      u32 freq_link1, u32 freq_link2)
7332 {
7333 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7334 
7335 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7336 		return true;
7337 
7338 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7339 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7340 							freq_link1, freq_link2);
7341 }
7342 
ath12k_mac_is_hw_sbs_capable(struct ath12k_base * ab)7343 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7344 {
7345 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7346 			ab->wmi_ab.svc_map) &&
7347 	       ab->wmi_ab.hw_mode_info.support_sbs;
7348 }
7349 
ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)7350 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7351 					      u32 freq_link1, u32 freq_link2)
7352 {
7353 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7354 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7355 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7356 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7357 
7358 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7359 		return true;
7360 
7361 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7362 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7363 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7364 
7365 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7366 								freq_link1, freq_link2) ||
7367 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7368 								freq_link1, freq_link2);
7369 	}
7370 
7371 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7372 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7373 							freq_link1, freq_link2);
7374 }
7375 
ath12k_mac_freqs_on_same_mac(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)7376 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7377 					 u32 freq_link1, u32 freq_link2)
7378 {
7379 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7380 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7381 }
7382 
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)7383 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7384 					      enum wmi_mlo_link_force_reason reason,
7385 					      enum wmi_mlo_link_force_mode mode,
7386 					      u8 *mlo_vdev_id_lst,
7387 					      u8 num_mlo_vdev,
7388 					      u8 *mlo_inactive_vdev_lst,
7389 					      u8 num_mlo_inactive_vdev)
7390 {
7391 	struct wmi_mlo_link_set_active_arg param = {};
7392 	u32 entry_idx, entry_offset, vdev_idx;
7393 	u8 vdev_id;
7394 
7395 	param.reason = reason;
7396 	param.force_mode = mode;
7397 
7398 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7399 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7400 		entry_idx = vdev_id / 32;
7401 		entry_offset = vdev_id % 32;
7402 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7403 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7404 				    entry_idx, num_mlo_vdev, vdev_id);
7405 			return -EINVAL;
7406 		}
7407 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7408 		/* update entry number if entry index changed */
7409 		if (param.num_vdev_bitmap < entry_idx + 1)
7410 			param.num_vdev_bitmap = entry_idx + 1;
7411 	}
7412 
7413 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7414 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7415 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7416 
7417 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7418 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7419 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7420 			entry_idx = vdev_id / 32;
7421 			entry_offset = vdev_id % 32;
7422 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7423 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7424 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7425 				return -EINVAL;
7426 			}
7427 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7428 			/* update entry number if entry index changed */
7429 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7430 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7431 		}
7432 
7433 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7434 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7435 			   param.num_inactive_vdev_bitmap,
7436 			   param.inactive_vdev_bitmap[0],
7437 			   param.inactive_vdev_bitmap[1]);
7438 	}
7439 
7440 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7441 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7442 		param.num_link_entry = 1;
7443 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7444 	}
7445 
7446 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7447 }
7448 
ath12k_mac_mlo_sta_update_link_active(struct ath12k_base * ab,struct ieee80211_hw * hw,struct ath12k_vif * ahvif)7449 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7450 						 struct ieee80211_hw *hw,
7451 						 struct ath12k_vif *ahvif)
7452 {
7453 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7454 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7455 	unsigned long links = ahvif->links_map;
7456 	enum wmi_mlo_link_force_reason reason;
7457 	struct ieee80211_chanctx_conf *conf;
7458 	enum wmi_mlo_link_force_mode mode;
7459 	struct ieee80211_bss_conf *info;
7460 	struct ath12k_link_vif *arvif;
7461 	u8 num_mlo_vdev = 0;
7462 	u8 link_id;
7463 
7464 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7465 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7466 		/* make sure vdev is created on this device */
7467 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7468 			continue;
7469 
7470 		info = ath12k_mac_get_link_bss_conf(arvif);
7471 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7472 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7473 
7474 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7475 		num_mlo_vdev++;
7476 	}
7477 
7478 	/* It is not allowed to activate more links than a single device
7479 	 * supported. Something goes wrong if we reach here.
7480 	 */
7481 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7482 		WARN_ON_ONCE(1);
7483 		return -EINVAL;
7484 	}
7485 
7486 	/* if 2 links are established and both link channels fall on the
7487 	 * same hardware MAC, send command to firmware to deactivate one
7488 	 * of them.
7489 	 */
7490 	if (num_mlo_vdev == 2 &&
7491 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7492 					 mlo_freq_list[1])) {
7493 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7494 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7495 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7496 							  mlo_vdev_id_lst, num_mlo_vdev,
7497 							  NULL, 0);
7498 	}
7499 
7500 	return 0;
7501 }
7502 
ath12k_mac_are_sbs_chan(struct ath12k_base * ab,u32 freq_1,u32 freq_2)7503 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7504 {
7505 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7506 		return false;
7507 
7508 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7509 	    ath12k_is_2ghz_channel_freq(freq_2))
7510 		return false;
7511 
7512 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7513 }
7514 
ath12k_mac_are_dbs_chan(struct ath12k_base * ab,u32 freq_1,u32 freq_2)7515 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7516 {
7517 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7518 		return false;
7519 
7520 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7521 }
7522 
ath12k_mac_select_links(struct ath12k_base * ab,struct ieee80211_vif * vif,struct ieee80211_hw * hw,u16 * selected_links)7523 static int ath12k_mac_select_links(struct ath12k_base *ab,
7524 				   struct ieee80211_vif *vif,
7525 				   struct ieee80211_hw *hw,
7526 				   u16 *selected_links)
7527 {
7528 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7529 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7530 	u8 num_useful_links = hweight_long(useful_links);
7531 	struct ieee80211_chanctx_conf *chanctx;
7532 	struct ath12k_link_vif *assoc_arvif;
7533 	u32 assoc_link_freq, partner_freq;
7534 	u16 sbs_links = 0, dbs_links = 0;
7535 	struct ieee80211_bss_conf *info;
7536 	struct ieee80211_channel *chan;
7537 	struct ieee80211_sta *sta;
7538 	struct ath12k_sta *ahsta;
7539 	u8 link_id;
7540 
7541 	/* activate all useful links if less than max supported */
7542 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7543 		*selected_links = useful_links;
7544 		return 0;
7545 	}
7546 
7547 	/* only in station mode we can get here, so it's safe
7548 	 * to use ap_addr
7549 	 */
7550 	rcu_read_lock();
7551 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7552 	if (!sta) {
7553 		rcu_read_unlock();
7554 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7555 		return -EINVAL;
7556 	}
7557 
7558 	ahsta = ath12k_sta_to_ahsta(sta);
7559 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7560 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7561 	chanctx = rcu_dereference(info->chanctx_conf);
7562 	assoc_link_freq = chanctx->def.chan->center_freq;
7563 	rcu_read_unlock();
7564 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7565 		   assoc_arvif->link_id, assoc_link_freq);
7566 
7567 	/* assoc link is already activated and has to be kept active,
7568 	 * only need to select a partner link from others.
7569 	 */
7570 	useful_links &= ~BIT(assoc_arvif->link_id);
7571 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7572 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7573 		if (!info) {
7574 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7575 				    link_id);
7576 			return -ENOLINK;
7577 		}
7578 
7579 		chan = info->chanreq.oper.chan;
7580 		if (!chan) {
7581 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7582 			return -EINVAL;
7583 		}
7584 
7585 		partner_freq = chan->center_freq;
7586 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7587 			sbs_links |= BIT(link_id);
7588 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7589 				   link_id, partner_freq);
7590 			continue;
7591 		}
7592 
7593 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7594 			dbs_links |= BIT(link_id);
7595 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7596 				   link_id, partner_freq);
7597 			continue;
7598 		}
7599 
7600 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7601 			   link_id, partner_freq);
7602 	}
7603 
7604 	/* choose the first candidate no matter how many is in the list */
7605 	if (sbs_links)
7606 		link_id = __ffs(sbs_links);
7607 	else if (dbs_links)
7608 		link_id = __ffs(dbs_links);
7609 	else
7610 		link_id = ffs(useful_links) - 1;
7611 
7612 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7613 
7614 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7615 
7616 	return 0;
7617 }
7618 
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)7619 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7620 			    struct ieee80211_vif *vif,
7621 			    struct ieee80211_sta *sta,
7622 			    enum ieee80211_sta_state old_state,
7623 			    enum ieee80211_sta_state new_state)
7624 {
7625 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7626 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7627 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7628 	struct ath12k_base *prev_ab = NULL, *ab;
7629 	struct ath12k_link_vif *arvif;
7630 	struct ath12k_link_sta *arsta;
7631 	unsigned long valid_links;
7632 	u16 selected_links = 0;
7633 	u8 link_id = 0, i;
7634 	struct ath12k *ar;
7635 	int ret = -EINVAL;
7636 	struct ath12k_dp_peer_create_params dp_params = {};
7637 
7638 	lockdep_assert_wiphy(hw->wiphy);
7639 
7640 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7641 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7642 		link_id = ffs(sta->valid_links) - 1;
7643 	}
7644 
7645 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7646 	 * New station add received. If this is a ML station then
7647 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7648 	 * Assign default link to the first link sta.
7649 	 */
7650 	if (old_state == IEEE80211_STA_NOTEXIST &&
7651 	    new_state == IEEE80211_STA_NONE) {
7652 		memset(ahsta, 0, sizeof(*ahsta));
7653 		ahsta->free_logical_link_idx_map = U16_MAX;
7654 
7655 		arsta = &ahsta->deflink;
7656 
7657 		/* ML sta */
7658 		if (sta->mlo && !ahsta->links_map &&
7659 		    (hweight16(sta->valid_links) == 1)) {
7660 			ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7661 			if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7662 				ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7663 					       sta->addr);
7664 				goto exit;
7665 			}
7666 
7667 			dp_params.is_mlo = true;
7668 			dp_params.peer_id = ahsta->ml_peer_id | ATH12K_PEER_ML_ID_VALID;
7669 		}
7670 
7671 		dp_params.sta = sta;
7672 
7673 		if (vif->type == NL80211_IFTYPE_AP)
7674 			dp_params.ucast_ra_only = true;
7675 
7676 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7677 		if (ret) {
7678 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7679 				       sta->addr, ret);
7680 
7681 			goto ml_peer_id_clear;
7682 		}
7683 
7684 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7685 						 link_id);
7686 		if (ret) {
7687 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7688 				       link_id, sta->addr);
7689 			goto peer_delete;
7690 		}
7691 
7692 		/* above arsta will get memset, hence do this after assign
7693 		 * link sta
7694 		 */
7695 		if (sta->mlo) {
7696 			/* For station mode, arvif->is_sta_assoc_link has been set when
7697 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7698 			 */
7699 			if (vif->type == NL80211_IFTYPE_STATION &&
7700 			    !arsta->arvif->is_sta_assoc_link) {
7701 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7702 					       link_id);
7703 				ret = -EINVAL;
7704 				goto exit;
7705 			}
7706 
7707 			arsta->is_assoc_link = true;
7708 			ahsta->assoc_link_id = link_id;
7709 		}
7710 	}
7711 
7712 	/* In the ML station scenario, activate all partner links once the
7713 	 * client is transitioning to the associated state.
7714 	 *
7715 	 * FIXME: Ideally, this activation should occur when the client
7716 	 * transitions to the authorized state. However, there are some
7717 	 * issues with handling this in the firmware. Until the firmware
7718 	 * can manage it properly, activate the links when the client is
7719 	 * about to move to the associated state.
7720 	 */
7721 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7722 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7723 		/* TODO: for now only do link selection for single device
7724 		 * MLO case. Other cases would be handled in the future.
7725 		 */
7726 		ab = ah->radio[0].ab;
7727 		if (ab->ag->num_devices == 1) {
7728 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7729 			if (ret) {
7730 				ath12k_warn(ab,
7731 					    "failed to get selected links: %d\n", ret);
7732 				goto exit;
7733 			}
7734 		} else {
7735 			selected_links = ieee80211_vif_usable_links(vif);
7736 		}
7737 
7738 		ieee80211_set_active_links(vif, selected_links);
7739 	}
7740 
7741 	/* Handle all the other state transitions in generic way */
7742 	valid_links = ahsta->links_map;
7743 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7744 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7745 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7746 		/* some assumptions went wrong! */
7747 		if (WARN_ON(!arvif || !arsta))
7748 			continue;
7749 
7750 		/* vdev might be in deleted */
7751 		if (WARN_ON(!arvif->ar))
7752 			continue;
7753 
7754 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7755 						       old_state, new_state);
7756 		if (ret) {
7757 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7758 				       link_id, arsta->addr, old_state, new_state);
7759 
7760 			if (old_state == IEEE80211_STA_NOTEXIST &&
7761 			    new_state == IEEE80211_STA_NONE)
7762 				goto peer_delete;
7763 			else
7764 				goto exit;
7765 		}
7766 	}
7767 
7768 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7769 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7770 		for_each_ar(ah, ar, i) {
7771 			ab = ar->ab;
7772 			if (prev_ab == ab)
7773 				continue;
7774 
7775 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7776 			if (ret) {
7777 				ath12k_warn(ab,
7778 					    "failed to update link active state on connect %d\n",
7779 					    ret);
7780 				goto exit;
7781 			}
7782 
7783 			prev_ab = ab;
7784 		}
7785 	}
7786 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7787 	 * Remove the station from driver (handle ML sta here since that
7788 	 * needs special handling. Normal sta will be handled in generic
7789 	 * handler below
7790 	 */
7791 	if (old_state == IEEE80211_STA_NONE &&
7792 	    new_state == IEEE80211_STA_NOTEXIST) {
7793 		if (sta->mlo)
7794 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7795 
7796 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7797 	}
7798 
7799 	ret = 0;
7800 	goto exit;
7801 
7802 peer_delete:
7803 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7804 ml_peer_id_clear:
7805 	if (sta->mlo) {
7806 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7807 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7808 	}
7809 exit:
7810 	/* update the state if everything went well */
7811 	if (!ret)
7812 		ahsta->state = new_state;
7813 
7814 	return ret;
7815 }
7816 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7817 
ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)7818 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7819 				struct ieee80211_vif *vif,
7820 				struct ieee80211_sta *sta)
7821 {
7822 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7823 	struct ath12k *ar;
7824 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7825 	struct ath12k_link_vif *arvif;
7826 	struct ath12k_link_sta *arsta;
7827 	u8 link_id;
7828 	int ret;
7829 	s16 txpwr;
7830 
7831 	lockdep_assert_wiphy(hw->wiphy);
7832 
7833 	/* TODO: use link id from mac80211 once that's implemented */
7834 	link_id = 0;
7835 
7836 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7837 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7838 
7839 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7840 		txpwr = 0;
7841 	} else {
7842 		txpwr = sta->deflink.txpwr.power;
7843 		if (!txpwr) {
7844 			ret = -EINVAL;
7845 			goto out;
7846 		}
7847 	}
7848 
7849 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7850 		ret = -EINVAL;
7851 		goto out;
7852 	}
7853 
7854 	ar = arvif->ar;
7855 
7856 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7857 					WMI_PEER_USE_FIXED_PWR, txpwr);
7858 	if (ret) {
7859 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7860 			    ret);
7861 		goto out;
7862 	}
7863 
7864 out:
7865 	return ret;
7866 }
7867 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
7868 
ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,u32 changed)7869 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7870 				      struct ieee80211_vif *vif,
7871 				      struct ieee80211_link_sta *link_sta,
7872 				      u32 changed)
7873 {
7874 	struct ieee80211_sta *sta = link_sta->sta;
7875 	struct ath12k *ar;
7876 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7877 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7878 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7879 	struct ath12k_link_sta *arsta;
7880 	struct ath12k_link_vif *arvif;
7881 	struct ath12k_dp_link_peer *peer;
7882 	u32 bw, smps;
7883 	struct ath12k_dp *dp;
7884 
7885 	rcu_read_lock();
7886 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7887 	if (!arvif) {
7888 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7889 			       link_sta->link_id, sta->addr);
7890 		rcu_read_unlock();
7891 		return;
7892 	}
7893 
7894 	ar = arvif->ar;
7895 	dp = ath12k_ab_to_dp(ar->ab);
7896 
7897 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7898 	if (!arsta) {
7899 		rcu_read_unlock();
7900 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7901 			    link_sta->link_id, sta->addr);
7902 		return;
7903 	}
7904 	spin_lock_bh(&dp->dp_lock);
7905 
7906 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7907 							 arsta->addr);
7908 	if (!peer) {
7909 		spin_unlock_bh(&dp->dp_lock);
7910 		rcu_read_unlock();
7911 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
7912 			    arsta->addr, arvif->vdev_id);
7913 		return;
7914 	}
7915 
7916 	spin_unlock_bh(&dp->dp_lock);
7917 
7918 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
7919 		rcu_read_unlock();
7920 		return;
7921 	}
7922 
7923 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
7924 	if (!link_sta) {
7925 		rcu_read_unlock();
7926 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
7927 			    sta->addr, arsta->link_id);
7928 		return;
7929 	}
7930 
7931 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
7932 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
7933 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
7934 		   link_sta->smps_mode);
7935 
7936 	spin_lock_bh(&ar->data_lock);
7937 
7938 	if (changed & IEEE80211_RC_BW_CHANGED) {
7939 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
7940 		arsta->bw_prev = arsta->bw;
7941 		arsta->bw = bw;
7942 	}
7943 
7944 	if (changed & IEEE80211_RC_NSS_CHANGED)
7945 		arsta->nss = link_sta->rx_nss;
7946 
7947 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
7948 		smps = WMI_PEER_SMPS_PS_NONE;
7949 
7950 		switch (link_sta->smps_mode) {
7951 		case IEEE80211_SMPS_AUTOMATIC:
7952 		case IEEE80211_SMPS_OFF:
7953 			smps = WMI_PEER_SMPS_PS_NONE;
7954 			break;
7955 		case IEEE80211_SMPS_STATIC:
7956 			smps = WMI_PEER_SMPS_STATIC;
7957 			break;
7958 		case IEEE80211_SMPS_DYNAMIC:
7959 			smps = WMI_PEER_SMPS_DYNAMIC;
7960 			break;
7961 		default:
7962 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
7963 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
7964 			smps = WMI_PEER_SMPS_PS_NONE;
7965 			break;
7966 		}
7967 
7968 		arsta->smps = smps;
7969 	}
7970 
7971 	arsta->changed |= changed;
7972 
7973 	spin_unlock_bh(&ar->data_lock);
7974 
7975 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
7976 
7977 	rcu_read_unlock();
7978 }
7979 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
7980 
ath12k_mac_alloc_assign_link_sta(struct ath12k_hw * ah,struct ath12k_sta * ahsta,struct ath12k_vif * ahvif,u8 link_id)7981 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
7982 								struct ath12k_sta *ahsta,
7983 								struct ath12k_vif *ahvif,
7984 								u8 link_id)
7985 {
7986 	struct ath12k_link_sta *arsta;
7987 	int ret;
7988 
7989 	lockdep_assert_wiphy(ah->hw->wiphy);
7990 
7991 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7992 		return NULL;
7993 
7994 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7995 	if (arsta)
7996 		return NULL;
7997 
7998 	arsta = kmalloc_obj(*arsta);
7999 	if (!arsta)
8000 		return NULL;
8001 
8002 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
8003 	if (ret) {
8004 		kfree(arsta);
8005 		return NULL;
8006 	}
8007 
8008 	return arsta;
8009 }
8010 
ath12k_mac_op_change_sta_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 old_links,u16 new_links)8011 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
8012 				   struct ieee80211_vif *vif,
8013 				   struct ieee80211_sta *sta,
8014 				   u16 old_links, u16 new_links)
8015 {
8016 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8017 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8018 	struct ath12k_hw *ah = hw->priv;
8019 	struct ath12k_link_vif *arvif;
8020 	struct ath12k_link_sta *arsta;
8021 	unsigned long valid_links;
8022 	struct ath12k *ar;
8023 	u8 link_id;
8024 	int ret;
8025 
8026 	lockdep_assert_wiphy(hw->wiphy);
8027 
8028 	if (!sta->valid_links)
8029 		return -EINVAL;
8030 
8031 	/* Firmware does not support removal of one of link stas. All sta
8032 	 * would be removed during ML STA delete in sta_state(), hence link
8033 	 * sta removal is not handled here.
8034 	 */
8035 	if (new_links < old_links)
8036 		return 0;
8037 
8038 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8039 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8040 		return -EINVAL;
8041 	}
8042 
8043 	/* this op is expected only after initial sta insertion with default link */
8044 	if (WARN_ON(ahsta->links_map == 0))
8045 		return -EINVAL;
8046 
8047 	valid_links = new_links;
8048 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8049 		if (ahsta->links_map & BIT(link_id))
8050 			continue;
8051 
8052 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8053 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8054 
8055 		if (!arvif || !arsta) {
8056 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8057 			continue;
8058 		}
8059 
8060 		ar = arvif->ar;
8061 		if (!ar)
8062 			continue;
8063 
8064 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8065 		if (ret) {
8066 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8067 				    arsta->addr, arvif->vdev_id);
8068 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8069 			return ret;
8070 		}
8071 	}
8072 
8073 	return 0;
8074 }
8075 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8076 
ath12k_mac_op_can_activate_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 active_links)8077 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8078 				      struct ieee80211_vif *vif,
8079 				      u16 active_links)
8080 {
8081 	/* TODO: Handle recovery case */
8082 
8083 	return true;
8084 }
8085 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8086 
ath12k_conf_tx_uapsd(struct ath12k_link_vif * arvif,u16 ac,bool enable)8087 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8088 				u16 ac, bool enable)
8089 {
8090 	struct ath12k *ar = arvif->ar;
8091 	struct ath12k_vif *ahvif = arvif->ahvif;
8092 	u32 value;
8093 	int ret;
8094 
8095 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8096 		return 0;
8097 
8098 	switch (ac) {
8099 	case IEEE80211_AC_VO:
8100 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8101 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8102 		break;
8103 	case IEEE80211_AC_VI:
8104 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8105 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8106 		break;
8107 	case IEEE80211_AC_BE:
8108 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8109 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8110 		break;
8111 	case IEEE80211_AC_BK:
8112 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8113 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8114 		break;
8115 	}
8116 
8117 	if (enable)
8118 		ahvif->u.sta.uapsd |= value;
8119 	else
8120 		ahvif->u.sta.uapsd &= ~value;
8121 
8122 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8123 					  WMI_STA_PS_PARAM_UAPSD,
8124 					  ahvif->u.sta.uapsd);
8125 	if (ret) {
8126 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8127 		goto exit;
8128 	}
8129 
8130 	if (ahvif->u.sta.uapsd)
8131 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8132 	else
8133 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8134 
8135 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8136 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8137 					  value);
8138 	if (ret)
8139 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8140 
8141 exit:
8142 	return ret;
8143 }
8144 
ath12k_mac_conf_tx(struct ath12k_link_vif * arvif,u16 ac,const struct ieee80211_tx_queue_params * params)8145 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8146 			      const struct ieee80211_tx_queue_params *params)
8147 {
8148 	struct wmi_wmm_params_arg *p = NULL;
8149 	struct ath12k *ar = arvif->ar;
8150 	struct ath12k_base *ab = ar->ab;
8151 	int ret;
8152 
8153 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8154 
8155 	switch (ac) {
8156 	case IEEE80211_AC_VO:
8157 		p = &arvif->wmm_params.ac_vo;
8158 		break;
8159 	case IEEE80211_AC_VI:
8160 		p = &arvif->wmm_params.ac_vi;
8161 		break;
8162 	case IEEE80211_AC_BE:
8163 		p = &arvif->wmm_params.ac_be;
8164 		break;
8165 	case IEEE80211_AC_BK:
8166 		p = &arvif->wmm_params.ac_bk;
8167 		break;
8168 	}
8169 
8170 	if (WARN_ON(!p)) {
8171 		ret = -EINVAL;
8172 		goto exit;
8173 	}
8174 
8175 	p->cwmin = params->cw_min;
8176 	p->cwmax = params->cw_max;
8177 	p->aifs = params->aifs;
8178 	p->txop = params->txop;
8179 
8180 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8181 					     &arvif->wmm_params);
8182 	if (ret) {
8183 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8184 			    ar->pdev_idx, ret);
8185 		goto exit;
8186 	}
8187 
8188 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8189 	if (ret)
8190 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8191 			    ar->pdev_idx, ret);
8192 
8193 exit:
8194 	return ret;
8195 }
8196 
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)8197 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8198 			  struct ieee80211_vif *vif,
8199 			  unsigned int link_id, u16 ac,
8200 			  const struct ieee80211_tx_queue_params *params)
8201 {
8202 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8203 	struct ath12k_link_vif *arvif;
8204 	struct ath12k_vif_cache *cache;
8205 	int ret;
8206 
8207 	lockdep_assert_wiphy(hw->wiphy);
8208 
8209 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8210 		return -EINVAL;
8211 
8212 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8213 	if (!arvif || !arvif->is_created) {
8214 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8215 		if (!cache)
8216 			return -ENOSPC;
8217 
8218 		cache->tx_conf.changed = true;
8219 		cache->tx_conf.ac = ac;
8220 		cache->tx_conf.tx_queue_params = *params;
8221 
8222 		return 0;
8223 	}
8224 
8225 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8226 
8227 	return ret;
8228 }
8229 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8230 
8231 static struct ieee80211_sta_ht_cap
ath12k_create_ht_cap(struct ath12k * ar,u32 ar_ht_cap,u32 rate_cap_rx_chainmask)8232 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8233 {
8234 	int i;
8235 	struct ieee80211_sta_ht_cap ht_cap = {};
8236 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8237 
8238 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8239 		return ht_cap;
8240 
8241 	ht_cap.ht_supported = 1;
8242 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8243 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8244 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8245 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8246 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8247 
8248 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8249 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8250 
8251 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8252 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8253 
8254 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8255 		u32 smps;
8256 
8257 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8258 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8259 
8260 		ht_cap.cap |= smps;
8261 	}
8262 
8263 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8264 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8265 
8266 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8267 		u32 stbc;
8268 
8269 		stbc   = ar_ht_cap;
8270 		stbc  &= WMI_HT_CAP_RX_STBC;
8271 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8272 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8273 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8274 
8275 		ht_cap.cap |= stbc;
8276 	}
8277 
8278 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8279 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8280 
8281 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8282 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8283 
8284 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8285 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8286 
8287 	for (i = 0; i < ar->num_rx_chains; i++) {
8288 		if (rate_cap_rx_chainmask & BIT(i))
8289 			ht_cap.mcs.rx_mask[i] = 0xFF;
8290 	}
8291 
8292 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8293 
8294 	return ht_cap;
8295 }
8296 
ath12k_mac_set_txbf_conf(struct ath12k_link_vif * arvif)8297 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8298 {
8299 	u32 value = 0;
8300 	struct ath12k *ar = arvif->ar;
8301 	struct ath12k_vif *ahvif = arvif->ahvif;
8302 	int nsts;
8303 	int sound_dim;
8304 	u32 vht_cap = ar->pdev->cap.vht_cap;
8305 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8306 
8307 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8308 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8309 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8310 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8311 	}
8312 
8313 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8314 		sound_dim = vht_cap &
8315 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8316 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8317 		if (sound_dim > (ar->num_tx_chains - 1))
8318 			sound_dim = ar->num_tx_chains - 1;
8319 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8320 	}
8321 
8322 	if (!value)
8323 		return 0;
8324 
8325 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8326 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8327 
8328 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8329 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8330 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8331 	}
8332 
8333 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8334 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8335 
8336 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8337 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8338 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8339 	}
8340 
8341 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8342 					     vdev_param, value);
8343 }
8344 
ath12k_set_vht_txbf_cap(struct ath12k * ar,u32 * vht_cap)8345 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8346 {
8347 	bool subfer, subfee;
8348 	int sound_dim = 0;
8349 
8350 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8351 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8352 
8353 	if (ar->num_tx_chains < 2) {
8354 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8355 		subfer = false;
8356 	}
8357 
8358 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8359 	if (!subfer)
8360 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8361 
8362 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8363 	if (!subfee)
8364 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8365 
8366 	sound_dim = u32_get_bits(*vht_cap,
8367 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8368 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8369 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8370 
8371 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8372 
8373 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8374 	if (subfer) {
8375 		if (sound_dim > (ar->num_tx_chains - 1))
8376 			sound_dim = ar->num_tx_chains - 1;
8377 
8378 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8379 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8380 	}
8381 
8382 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8383 	if (!subfee)
8384 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8385 }
8386 
8387 static struct ieee80211_sta_vht_cap
ath12k_create_vht_cap(struct ath12k * ar,u32 rate_cap_tx_chainmask,u32 rate_cap_rx_chainmask)8388 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8389 		      u32 rate_cap_rx_chainmask)
8390 {
8391 	struct ieee80211_sta_vht_cap vht_cap = {};
8392 	u16 txmcs_map, rxmcs_map;
8393 	int i;
8394 
8395 	vht_cap.vht_supported = 1;
8396 	vht_cap.cap = ar->pdev->cap.vht_cap;
8397 
8398 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8399 
8400 	/* 80P80 is not supported */
8401 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8402 
8403 	rxmcs_map = 0;
8404 	txmcs_map = 0;
8405 	for (i = 0; i < 8; i++) {
8406 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8407 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8408 		else
8409 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8410 
8411 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8412 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8413 		else
8414 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8415 	}
8416 
8417 	if (rate_cap_tx_chainmask <= 1)
8418 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8419 
8420 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8421 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8422 
8423 	/* Check if the HW supports 1:1 NSS ratio and reset
8424 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8425 	 */
8426 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8427 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8428 
8429 	return vht_cap;
8430 }
8431 
ath12k_mac_setup_ht_vht_cap(struct ath12k * ar,struct ath12k_pdev_cap * cap,u32 * ht_cap_info)8432 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8433 					struct ath12k_pdev_cap *cap,
8434 					u32 *ht_cap_info)
8435 {
8436 	struct ieee80211_supported_band *band;
8437 	u32 rate_cap_tx_chainmask;
8438 	u32 rate_cap_rx_chainmask;
8439 	u32 ht_cap;
8440 
8441 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8442 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8443 
8444 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8445 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8446 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8447 		if (ht_cap_info)
8448 			*ht_cap_info = ht_cap;
8449 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8450 						    rate_cap_rx_chainmask);
8451 	}
8452 
8453 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8454 	    (ar->ab->hw_params->single_pdev_only ||
8455 	     !ar->supports_6ghz)) {
8456 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8457 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8458 		if (ht_cap_info)
8459 			*ht_cap_info = ht_cap;
8460 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8461 						    rate_cap_rx_chainmask);
8462 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8463 						      rate_cap_rx_chainmask);
8464 	}
8465 }
8466 
ath12k_check_chain_mask(struct ath12k * ar,u32 ant,bool is_tx_ant)8467 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8468 {
8469 	/* TODO: Check the request chainmask against the supported
8470 	 * chainmask table which is advertised in extented_service_ready event
8471 	 */
8472 
8473 	return 0;
8474 }
8475 
ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg * fw_ppet,u8 * he_ppet)8476 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8477 				  u8 *he_ppet)
8478 {
8479 	int nss, ru;
8480 	u8 bit = 7;
8481 
8482 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8483 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8484 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8485 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8486 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8487 		for (ru = 0; ru < 4; ru++) {
8488 			u8 val;
8489 			int i;
8490 
8491 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8492 				continue;
8493 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8494 			       0x3f;
8495 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8496 			for (i = 5; i >= 0; i--) {
8497 				he_ppet[bit / 8] |=
8498 					((val >> i) & 0x1) << ((bit % 8));
8499 				bit++;
8500 			}
8501 		}
8502 	}
8503 }
8504 
8505 static void
ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem * he_cap_elem)8506 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8507 {
8508 	u8 m;
8509 
8510 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8511 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8512 	he_cap_elem->mac_cap_info[0] &= ~m;
8513 
8514 	m = IEEE80211_HE_MAC_CAP2_TRS |
8515 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8516 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8517 	he_cap_elem->mac_cap_info[2] &= ~m;
8518 
8519 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8520 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8521 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8522 	he_cap_elem->mac_cap_info[3] &= ~m;
8523 
8524 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8525 	    IEEE80211_HE_MAC_CAP4_BQR;
8526 	he_cap_elem->mac_cap_info[4] &= ~m;
8527 
8528 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8529 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8530 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8531 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8532 	he_cap_elem->mac_cap_info[5] &= ~m;
8533 
8534 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8535 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8536 	he_cap_elem->phy_cap_info[2] &= ~m;
8537 
8538 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8539 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8540 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8541 	he_cap_elem->phy_cap_info[3] &= ~m;
8542 
8543 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8544 	he_cap_elem->phy_cap_info[4] &= ~m;
8545 
8546 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8547 	he_cap_elem->phy_cap_info[5] &= ~m;
8548 
8549 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8550 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8551 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8552 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8553 	he_cap_elem->phy_cap_info[6] &= ~m;
8554 
8555 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8556 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8557 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8558 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8559 	he_cap_elem->phy_cap_info[7] &= ~m;
8560 
8561 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8562 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8563 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8564 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8565 	he_cap_elem->phy_cap_info[8] &= ~m;
8566 
8567 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8568 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8569 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8570 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8571 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8572 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8573 	he_cap_elem->phy_cap_info[9] &= ~m;
8574 }
8575 
ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap * pcap,struct ath12k_band_cap * bcap)8576 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8577 					   struct ath12k_band_cap *bcap)
8578 {
8579 	u8 val;
8580 
8581 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8582 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8583 		bcap->he_6ghz_capa |=
8584 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8585 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8586 	else
8587 		bcap->he_6ghz_capa |=
8588 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8589 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8590 	val = u32_get_bits(pcap->vht_cap,
8591 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8592 	bcap->he_6ghz_capa |=
8593 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8594 	val = u32_get_bits(pcap->vht_cap,
8595 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8596 	bcap->he_6ghz_capa |=
8597 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8598 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8599 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8600 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8601 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8602 
8603 	return cpu_to_le16(bcap->he_6ghz_capa);
8604 }
8605 
ath12k_mac_set_hemcsmap(struct ath12k * ar,struct ath12k_pdev_cap * cap,struct ieee80211_sta_he_cap * he_cap)8606 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8607 				    struct ath12k_pdev_cap *cap,
8608 				    struct ieee80211_sta_he_cap *he_cap)
8609 {
8610 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8611 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8612 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8613 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8614 	u16 txmcs_map = 0, rxmcs_map = 0;
8615 	u32 i;
8616 
8617 	for (i = 0; i < 8; i++) {
8618 		if (i < ar->num_tx_chains &&
8619 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8620 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8621 		else
8622 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8623 
8624 		if (i < ar->num_rx_chains &&
8625 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8626 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8627 		else
8628 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8629 
8630 		if (i < maxtxnss_160 &&
8631 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8632 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8633 		else
8634 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8635 
8636 		if (i < maxrxnss_160 &&
8637 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8638 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8639 		else
8640 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8641 	}
8642 
8643 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8644 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8645 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8646 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8647 }
8648 
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)8649 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8650 				   struct ath12k_band_cap *band_cap,
8651 				   int iftype, u8 num_tx_chains,
8652 				   struct ieee80211_sta_he_cap *he_cap)
8653 {
8654 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8655 
8656 	he_cap->has_he = true;
8657 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8658 	       sizeof(he_cap_elem->mac_cap_info));
8659 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8660 	       sizeof(he_cap_elem->phy_cap_info));
8661 
8662 	he_cap_elem->mac_cap_info[1] &=
8663 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8664 	he_cap_elem->phy_cap_info[0] &=
8665 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8666 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8667 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8668 	/* 80PLUS80 is not supported */
8669 	he_cap_elem->phy_cap_info[0] &=
8670 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8671 	he_cap_elem->phy_cap_info[5] &=
8672 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8673 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8674 
8675 	switch (iftype) {
8676 	case NL80211_IFTYPE_AP:
8677 		he_cap_elem->mac_cap_info[2] &=
8678 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8679 		he_cap_elem->phy_cap_info[3] &=
8680 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8681 		he_cap_elem->phy_cap_info[9] |=
8682 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8683 		break;
8684 	case NL80211_IFTYPE_STATION:
8685 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8686 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8687 		he_cap_elem->phy_cap_info[9] |=
8688 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8689 		break;
8690 	case NL80211_IFTYPE_MESH_POINT:
8691 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8692 		break;
8693 	}
8694 
8695 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8696 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8697 	if (he_cap_elem->phy_cap_info[6] &
8698 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8699 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8700 }
8701 
8702 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)8703 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8704 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8705 			    const struct ieee80211_he_cap_elem *he_cap,
8706 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8707 {
8708 	if ((he_cap->phy_cap_info[0] &
8709 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8710 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8711 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8712 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8713 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8714 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8715 
8716 	if (he_cap->phy_cap_info[0] &
8717 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8718 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8719 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8720 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8721 
8722 	if (he_cap->phy_cap_info[0] &
8723 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8724 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8725 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8726 
8727 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8728 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8729 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8730 }
8731 
ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg * fw_ppet,struct ieee80211_sta_eht_cap * cap)8732 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8733 					   struct ieee80211_sta_eht_cap *cap)
8734 {
8735 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8736 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8737 
8738 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8739 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8740 
8741 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8742 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8743 
8744 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8745 		for (ru = 0;
8746 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8747 		     ru++) {
8748 			u32 val = 0;
8749 
8750 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8751 				continue;
8752 
8753 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8754 						(ru * ppet_bit_len_per_ru),
8755 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8756 
8757 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8758 				cap->eht_ppe_thres[bit / 8] |=
8759 					(((val >> i) & 0x1) << ((bit % 8)));
8760 				bit++;
8761 			}
8762 		}
8763 	}
8764 }
8765 
8766 static void
ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed * eht_cap_elem)8767 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8768 			       *eht_cap_elem)
8769 {
8770 	u8 m;
8771 
8772 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8773 	eht_cap_elem->mac_cap_info[0] &= ~m;
8774 
8775 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8776 	eht_cap_elem->phy_cap_info[0] &= ~m;
8777 
8778 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8779 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8780 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8781 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8782 	eht_cap_elem->phy_cap_info[3] &= ~m;
8783 
8784 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8785 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8786 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8787 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8788 	eht_cap_elem->phy_cap_info[4] &= ~m;
8789 
8790 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8791 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8792 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8793 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8794 	eht_cap_elem->phy_cap_info[5] &= ~m;
8795 
8796 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8797 	eht_cap_elem->phy_cap_info[6] &= ~m;
8798 
8799 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8800 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8801 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8802 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8803 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8804 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8805 	eht_cap_elem->phy_cap_info[7] &= ~m;
8806 }
8807 
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)8808 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8809 				    struct ath12k_band_cap *band_cap,
8810 				    struct ieee80211_he_cap_elem *he_cap_elem,
8811 				    int iftype,
8812 				    struct ieee80211_sta_eht_cap *eht_cap)
8813 {
8814 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8815 
8816 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8817 
8818 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8819 	    ath12k_acpi_get_disable_11be(ar->ab))
8820 		return;
8821 
8822 	eht_cap->has_eht = true;
8823 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8824 	       sizeof(eht_cap_elem->mac_cap_info));
8825 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8826 	       sizeof(eht_cap_elem->phy_cap_info));
8827 
8828 	switch (iftype) {
8829 	case NL80211_IFTYPE_AP:
8830 		eht_cap_elem->phy_cap_info[0] &=
8831 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8832 		eht_cap_elem->phy_cap_info[4] &=
8833 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8834 		eht_cap_elem->phy_cap_info[5] &=
8835 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8836 		break;
8837 	case NL80211_IFTYPE_STATION:
8838 		eht_cap_elem->phy_cap_info[7] &=
8839 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8840 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8841 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8842 		eht_cap_elem->phy_cap_info[7] &=
8843 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8844 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8845 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8846 		break;
8847 	case NL80211_IFTYPE_MESH_POINT:
8848 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8849 		break;
8850 	default:
8851 		break;
8852 	}
8853 
8854 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8855 				    he_cap_elem, eht_cap_elem);
8856 
8857 	if (eht_cap_elem->phy_cap_info[5] &
8858 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8859 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8860 }
8861 
ath12k_mac_copy_sband_iftype_data(struct ath12k * ar,struct ath12k_pdev_cap * cap,struct ieee80211_sband_iftype_data * data,int band)8862 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8863 					     struct ath12k_pdev_cap *cap,
8864 					     struct ieee80211_sband_iftype_data *data,
8865 					     int band)
8866 {
8867 	struct ath12k_band_cap *band_cap = &cap->band[band];
8868 	int i, idx = 0;
8869 
8870 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8871 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8872 
8873 		switch (i) {
8874 		case NL80211_IFTYPE_STATION:
8875 		case NL80211_IFTYPE_AP:
8876 		case NL80211_IFTYPE_MESH_POINT:
8877 			break;
8878 
8879 		default:
8880 			continue;
8881 		}
8882 
8883 		data[idx].types_mask = BIT(i);
8884 
8885 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8886 		if (band == NL80211_BAND_6GHZ) {
8887 			data[idx].he_6ghz_capa.capa =
8888 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8889 		}
8890 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8891 					&data[idx].eht_cap);
8892 		idx++;
8893 	}
8894 
8895 	return idx;
8896 }
8897 
ath12k_mac_setup_sband_iftype_data(struct ath12k * ar,struct ath12k_pdev_cap * cap)8898 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
8899 					       struct ath12k_pdev_cap *cap)
8900 {
8901 	struct ieee80211_supported_band *sband;
8902 	enum nl80211_band band;
8903 	int count;
8904 
8905 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8906 		band = NL80211_BAND_2GHZ;
8907 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8908 							  ar->mac.iftype[band],
8909 							  band);
8910 		sband = &ar->mac.sbands[band];
8911 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8912 						 count);
8913 	}
8914 
8915 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
8916 		band = NL80211_BAND_5GHZ;
8917 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8918 							  ar->mac.iftype[band],
8919 							  band);
8920 		sband = &ar->mac.sbands[band];
8921 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8922 						 count);
8923 	}
8924 
8925 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8926 	    ar->supports_6ghz) {
8927 		band = NL80211_BAND_6GHZ;
8928 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8929 							  ar->mac.iftype[band],
8930 							  band);
8931 		sband = &ar->mac.sbands[band];
8932 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8933 						 count);
8934 	}
8935 }
8936 
__ath12k_set_antenna(struct ath12k * ar,u32 tx_ant,u32 rx_ant)8937 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
8938 {
8939 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
8940 	int ret;
8941 
8942 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8943 
8944 	if (ath12k_check_chain_mask(ar, tx_ant, true))
8945 		return -EINVAL;
8946 
8947 	if (ath12k_check_chain_mask(ar, rx_ant, false))
8948 		return -EINVAL;
8949 
8950 	/* Since we advertised the max cap of all radios combined during wiphy
8951 	 * registration, ensure we don't set the antenna config higher than the
8952 	 * limits
8953 	 */
8954 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
8955 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
8956 
8957 	ar->cfg_tx_chainmask = tx_ant;
8958 	ar->cfg_rx_chainmask = rx_ant;
8959 
8960 	if (ah->state != ATH12K_HW_STATE_ON &&
8961 	    ah->state != ATH12K_HW_STATE_RESTARTED)
8962 		return 0;
8963 
8964 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
8965 					tx_ant, ar->pdev->pdev_id);
8966 	if (ret) {
8967 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
8968 			    ret, tx_ant);
8969 		return ret;
8970 	}
8971 
8972 	ar->num_tx_chains = hweight32(tx_ant);
8973 
8974 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
8975 					rx_ant, ar->pdev->pdev_id);
8976 	if (ret) {
8977 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
8978 			    ret, rx_ant);
8979 		return ret;
8980 	}
8981 
8982 	ar->num_rx_chains = hweight32(rx_ant);
8983 
8984 	/* Reload HT/VHT/HE capability */
8985 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
8986 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
8987 
8988 	return 0;
8989 }
8990 
ath12k_mgmt_over_wmi_tx_drop(struct ath12k * ar,struct sk_buff * skb)8991 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
8992 {
8993 	int num_mgmt;
8994 
8995 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8996 
8997 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
8998 
8999 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
9000 
9001 	if (num_mgmt < 0)
9002 		WARN_ON_ONCE(1);
9003 
9004 	if (!num_mgmt)
9005 		wake_up(&ar->txmgmt_empty_waitq);
9006 }
9007 
ath12k_mac_tx_mgmt_free(struct ath12k * ar,int buf_id)9008 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
9009 {
9010 	struct sk_buff *msdu;
9011 	struct ieee80211_tx_info *info;
9012 
9013 	spin_lock_bh(&ar->txmgmt_idr_lock);
9014 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
9015 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9016 
9017 	if (!msdu)
9018 		return;
9019 
9020 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9021 			 DMA_TO_DEVICE);
9022 
9023 	info = IEEE80211_SKB_CB(msdu);
9024 	memset(&info->status, 0, sizeof(info->status));
9025 
9026 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9027 }
9028 
ath12k_mac_tx_mgmt_pending_free(int buf_id,void * skb,void * ctx)9029 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
9030 {
9031 	struct ath12k *ar = ctx;
9032 
9033 	ath12k_mac_tx_mgmt_free(ar, buf_id);
9034 
9035 	return 0;
9036 }
9037 
ath12k_mac_vif_txmgmt_idr_remove(int buf_id,void * skb,void * ctx)9038 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
9039 {
9040 	struct ieee80211_vif *vif = ctx;
9041 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9042 	struct ath12k *ar = skb_cb->ar;
9043 
9044 	if (skb_cb->vif == vif)
9045 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9046 
9047 	return 0;
9048 }
9049 
ath12k_mac_mgmt_tx_wmi(struct ath12k * ar,struct ath12k_link_vif * arvif,struct sk_buff * skb)9050 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9051 				  struct sk_buff *skb)
9052 {
9053 	struct ath12k_base *ab = ar->ab;
9054 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9055 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9056 	struct ieee80211_tx_info *info;
9057 	enum hal_encrypt_type enctype;
9058 	unsigned int mic_len;
9059 	dma_addr_t paddr;
9060 	int buf_id;
9061 	int ret;
9062 
9063 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9064 
9065 	skb_cb->ar = ar;
9066 	spin_lock_bh(&ar->txmgmt_idr_lock);
9067 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9068 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9069 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9070 	if (buf_id < 0)
9071 		return -ENOSPC;
9072 
9073 	info = IEEE80211_SKB_CB(skb);
9074 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9075 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9076 		if ((ieee80211_is_action(hdr->frame_control) ||
9077 		     ieee80211_is_deauth(hdr->frame_control) ||
9078 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9079 		     ieee80211_has_protected(hdr->frame_control)) {
9080 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9081 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9082 			skb_put(skb, mic_len);
9083 		}
9084 	}
9085 
9086 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9087 	if (dma_mapping_error(ab->dev, paddr)) {
9088 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9089 		ret = -EIO;
9090 		goto err_free_idr;
9091 	}
9092 
9093 	skb_cb->paddr = paddr;
9094 
9095 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9096 	if (ret) {
9097 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9098 		goto err_unmap_buf;
9099 	}
9100 
9101 	return 0;
9102 
9103 err_unmap_buf:
9104 	dma_unmap_single(ab->dev, skb_cb->paddr,
9105 			 skb->len, DMA_TO_DEVICE);
9106 err_free_idr:
9107 	spin_lock_bh(&ar->txmgmt_idr_lock);
9108 	idr_remove(&ar->txmgmt_idr, buf_id);
9109 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9110 
9111 	return ret;
9112 }
9113 
ath12k_mgmt_over_wmi_tx_purge(struct ath12k * ar)9114 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9115 {
9116 	struct sk_buff *skb;
9117 
9118 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9119 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9120 }
9121 
ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif * arvif,struct sk_buff * skb)9122 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9123 						       struct sk_buff *skb)
9124 {
9125 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9126 	u8 category, *buf, iv_len, action_code, dialog_token;
9127 	struct ieee80211_bss_conf *link_conf;
9128 	struct ieee80211_chanctx_conf *conf;
9129 	int cur_tx_power, max_tx_power;
9130 	struct ath12k *ar = arvif->ar;
9131 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9132 	struct wiphy *wiphy = hw->wiphy;
9133 	struct ath12k_skb_cb *skb_cb;
9134 	struct ieee80211_mgmt *mgmt;
9135 	unsigned int remaining_len;
9136 	bool has_protected;
9137 
9138 	lockdep_assert_wiphy(wiphy);
9139 
9140 	/* make sure category field is present */
9141 	if (skb->len < IEEE80211_MIN_ACTION_SIZE(category))
9142 		return -EINVAL;
9143 
9144 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE(category);
9145 	has_protected = ieee80211_has_protected(hdr->frame_control);
9146 
9147 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9148 	 * we can't put in data in this case
9149 	 */
9150 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9151 	    has_protected)
9152 		return 0;
9153 
9154 	mgmt = (struct ieee80211_mgmt *)hdr;
9155 	buf = (u8 *)&mgmt->u.action;
9156 
9157 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9158 	 * many bytes if it is there
9159 	 */
9160 	if (has_protected) {
9161 		skb_cb = ATH12K_SKB_CB(skb);
9162 
9163 		switch (skb_cb->cipher) {
9164 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9165 		 * key needs to be processed. See ath12k_install_key()
9166 		 */
9167 		case WLAN_CIPHER_SUITE_CCMP:
9168 		case WLAN_CIPHER_SUITE_CCMP_256:
9169 		case WLAN_CIPHER_SUITE_GCMP:
9170 		case WLAN_CIPHER_SUITE_GCMP_256:
9171 			iv_len = IEEE80211_CCMP_HDR_LEN;
9172 			break;
9173 		case WLAN_CIPHER_SUITE_TKIP:
9174 			iv_len = 0;
9175 			break;
9176 		default:
9177 			return -EINVAL;
9178 		}
9179 
9180 		if (remaining_len < iv_len)
9181 			return -EINVAL;
9182 
9183 		buf += iv_len;
9184 		remaining_len -= iv_len;
9185 	}
9186 
9187 	category = *buf++;
9188 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9189 	 * no need to adjust remaining_len
9190 	 */
9191 
9192 	switch (category) {
9193 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9194 		/* need action code and dialog token */
9195 		if (remaining_len < 2)
9196 			return -EINVAL;
9197 
9198 		/* Packet Format:
9199 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9200 		 */
9201 		action_code = *buf++;
9202 		dialog_token = *buf++;
9203 		remaining_len -= 2;
9204 
9205 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9206 		if (!link_conf) {
9207 			ath12k_warn(ar->ab,
9208 				    "failed to get bss link conf for vdev %d in RM handling\n",
9209 				    arvif->vdev_id);
9210 			return -EINVAL;
9211 		}
9212 
9213 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9214 		if (!conf)
9215 			return -ENOENT;
9216 
9217 		cur_tx_power = link_conf->txpower;
9218 		max_tx_power = min(conf->def.chan->max_reg_power,
9219 				   (int)ar->max_tx_power / 2);
9220 
9221 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9222 					  &cur_tx_power);
9223 
9224 		switch (action_code) {
9225 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9226 			/* need variable fields to be present in len */
9227 			if (remaining_len < 2)
9228 				return -EINVAL;
9229 
9230 			/* Variable length format as defined in IEEE 802.11-2024,
9231 			 * Figure 9-1187-Link Measurement Request frame Action field
9232 			 * format.
9233 			 *	Transmit Power | Max Tx Power
9234 			 * We fill both of these.
9235 			 */
9236 			*buf++ = cur_tx_power;
9237 			*buf = max_tx_power;
9238 
9239 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9240 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9241 				   dialog_token, cur_tx_power, max_tx_power);
9242 			break;
9243 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9244 			/* need variable fields to be present in len */
9245 			if (remaining_len < 3)
9246 				return -EINVAL;
9247 
9248 			/* Variable length format as defined in IEEE 802.11-2024,
9249 			 * Figure 9-1188-Link Measurement Report frame Action field format
9250 			 *	TPC Report | Variable Fields
9251 			 *
9252 			 * TPC Report Format:
9253 			 *	Element ID | Len | Tx Power | Link Margin
9254 			 *
9255 			 * We fill Tx power in the TPC Report (2nd index)
9256 			 */
9257 			buf[2] = cur_tx_power;
9258 
9259 			/* TODO: At present, Link margin data is not present so can't
9260 			 * really fill it now. Once it is available, it can be added
9261 			 * here
9262 			 */
9263 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9264 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9265 				   dialog_token, cur_tx_power);
9266 			break;
9267 		default:
9268 			return -EINVAL;
9269 		}
9270 		break;
9271 	default:
9272 		/* nothing to fill */
9273 		return 0;
9274 	}
9275 
9276 	return 0;
9277 }
9278 
ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif * arvif,struct sk_buff * skb)9279 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9280 						struct sk_buff *skb)
9281 {
9282 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9283 
9284 	if (!ieee80211_is_action(hdr->frame_control))
9285 		return 0;
9286 
9287 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9288 }
9289 
ath12k_mgmt_over_wmi_tx_work(struct wiphy * wiphy,struct wiphy_work * work)9290 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9291 {
9292 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9293 	struct ath12k_hw *ah = ar->ah;
9294 	struct ath12k_skb_cb *skb_cb;
9295 	struct ath12k_vif *ahvif;
9296 	struct ath12k_link_vif *arvif;
9297 	struct sk_buff *skb;
9298 	int ret;
9299 
9300 	lockdep_assert_wiphy(wiphy);
9301 
9302 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9303 		skb_cb = ATH12K_SKB_CB(skb);
9304 		if (!skb_cb->vif) {
9305 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9306 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9307 			continue;
9308 		}
9309 
9310 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9311 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9312 			ath12k_warn(ar->ab,
9313 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9314 				    skb_cb->link_id, ahvif->links_map);
9315 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9316 			continue;
9317 		}
9318 
9319 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9320 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9321 			/* Fill in the data which is required to be filled by the driver
9322 			 * For example: Max Tx power in Link Measurement Request/Report
9323 			 */
9324 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9325 			if (ret) {
9326 				/* If we couldn't fill the data due to any reason,
9327 				 * let's not discard transmitting the packet.
9328 				 * For example: Software crypto and PMF case
9329 				 */
9330 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9331 					   "Failed to fill the required data for the mgmt packet err %d\n",
9332 					   ret);
9333 			}
9334 
9335 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9336 			if (ret) {
9337 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9338 					    arvif->vdev_id, ret);
9339 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9340 			}
9341 		} else {
9342 			ath12k_warn(ar->ab,
9343 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9344 				    arvif->vdev_id,
9345 				    skb_cb->link_id,
9346 				    arvif->is_started);
9347 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9348 		}
9349 	}
9350 }
9351 
ath12k_mac_mgmt_tx(struct ath12k * ar,struct sk_buff * skb,bool is_prb_rsp)9352 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9353 		       bool is_prb_rsp)
9354 {
9355 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9356 
9357 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9358 		return -ESHUTDOWN;
9359 
9360 	/* Drop probe response packets when the pending management tx
9361 	 * count has reached a certain threshold, so as to prioritize
9362 	 * other mgmt packets like auth and assoc to be sent on time
9363 	 * for establishing successful connections.
9364 	 */
9365 	if (is_prb_rsp &&
9366 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9367 		ath12k_warn(ar->ab,
9368 			    "dropping probe response as pending queue is almost full\n");
9369 		return -ENOSPC;
9370 	}
9371 
9372 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9373 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9374 		return -ENOSPC;
9375 	}
9376 
9377 	skb_queue_tail(q, skb);
9378 	atomic_inc(&ar->num_pending_mgmt_tx);
9379 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9380 
9381 	return 0;
9382 }
9383 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9384 
ath12k_mac_add_p2p_noa_ie(struct ath12k * ar,struct ieee80211_vif * vif,struct sk_buff * skb,bool is_prb_rsp)9385 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9386 			       struct ieee80211_vif *vif,
9387 			       struct sk_buff *skb,
9388 			       bool is_prb_rsp)
9389 {
9390 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9391 
9392 	if (likely(!is_prb_rsp))
9393 		return;
9394 
9395 	spin_lock_bh(&ar->data_lock);
9396 
9397 	if (ahvif->u.ap.noa_data &&
9398 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9399 			      GFP_ATOMIC))
9400 		skb_put_data(skb, ahvif->u.ap.noa_data,
9401 			     ahvif->u.ap.noa_len);
9402 
9403 	spin_unlock_bh(&ar->data_lock);
9404 }
9405 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9406 
9407 /* 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)9408 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9409 					     u8 link_id, struct sk_buff *skb,
9410 					     u32 info_flags)
9411 {
9412 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9413 	struct ieee80211_bss_conf *bss_conf;
9414 
9415 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9416 		return;
9417 
9418 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9419 	if (bss_conf)
9420 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9421 }
9422 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9423 
9424 /* 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)9425 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9426 			  u8 link, struct sk_buff *skb, u32 info_flags)
9427 {
9428 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9429 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9430 	struct ieee80211_link_sta *link_sta;
9431 	struct ieee80211_bss_conf *bss_conf;
9432 	struct ath12k_sta *ahsta;
9433 
9434 	/* Use the link id passed or the default vif link */
9435 	if (!sta) {
9436 		if (link != IEEE80211_LINK_UNSPECIFIED)
9437 			return link;
9438 
9439 		return ahvif->deflink.link_id;
9440 	}
9441 
9442 	ahsta = ath12k_sta_to_ahsta(sta);
9443 
9444 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9445 	 * Also it assumes for now support only for MLO AP in this path
9446 	 */
9447 	if (!sta->mlo) {
9448 		link = ahsta->deflink.link_id;
9449 
9450 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9451 			return link;
9452 
9453 		bss_conf = rcu_dereference(vif->link_conf[link]);
9454 		if (bss_conf) {
9455 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9456 			if (!ieee80211_has_tods(hdr->frame_control) &&
9457 			    !ieee80211_has_fromds(hdr->frame_control))
9458 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9459 		}
9460 
9461 		return link;
9462 	}
9463 
9464 	/* enqueue eth enacap & data frames on primary link, FW does link
9465 	 * selection and address translation.
9466 	 */
9467 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9468 	    ieee80211_is_data(hdr->frame_control))
9469 		return ahsta->assoc_link_id;
9470 
9471 	/* 802.11 frame cases */
9472 	if (link == IEEE80211_LINK_UNSPECIFIED)
9473 		link = ahsta->deflink.link_id;
9474 
9475 	if (!ieee80211_is_mgmt(hdr->frame_control))
9476 		return link;
9477 
9478 	/* Perform address conversion for ML STA Tx */
9479 	bss_conf = rcu_dereference(vif->link_conf[link]);
9480 	link_sta = rcu_dereference(sta->link[link]);
9481 
9482 	if (bss_conf && link_sta) {
9483 		ether_addr_copy(hdr->addr1, link_sta->addr);
9484 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9485 
9486 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9487 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9488 		else if (vif->type == NL80211_IFTYPE_AP)
9489 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9490 
9491 		return link;
9492 	}
9493 
9494 	if (bss_conf) {
9495 		/* In certain cases where a ML sta associated and added subset of
9496 		 * links on which the ML AP is active, but now sends some frame
9497 		 * (ex. Probe request) on a different link which is active in our
9498 		 * MLD but was not added during previous association, we can
9499 		 * still honor the Tx to that ML STA via the requested link.
9500 		 * The control would reach here in such case only when that link
9501 		 * address is same as the MLD address or in worst case clients
9502 		 * used MLD address at TA wrongly which would have helped
9503 		 * identify the ML sta object and pass it here.
9504 		 * If the link address of that STA is different from MLD address,
9505 		 * then the sta object would be NULL and control won't reach
9506 		 * here but return at the start of the function itself with !sta
9507 		 * check. Also this would not need any translation at hdr->addr1
9508 		 * from MLD to link address since the RA is the MLD address
9509 		 * (same as that link address ideally) already.
9510 		 */
9511 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9512 
9513 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9514 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9515 		else if (vif->type == NL80211_IFTYPE_AP)
9516 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9517 	}
9518 
9519 	return link;
9520 }
9521 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9522 
ath12k_mac_drain_tx(struct ath12k * ar)9523 void ath12k_mac_drain_tx(struct ath12k *ar)
9524 {
9525 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9526 
9527 	/* make sure rcu-protected mac80211 tx path itself is drained */
9528 	synchronize_net();
9529 
9530 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9531 	ath12k_mgmt_over_wmi_tx_purge(ar);
9532 }
9533 
ath12k_mac_config_mon_status_default(struct ath12k * ar,bool enable)9534 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9535 {
9536 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9537 	struct ath12k_base *ab = ar->ab;
9538 	u32 ring_id, i;
9539 	int ret = 0;
9540 
9541 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9542 
9543 	if (!ab->hw_params->rxdma1_enable)
9544 		return ret;
9545 
9546 	if (enable) {
9547 		tlv_filter = ath12k_mac_mon_status_filter_default;
9548 
9549 		if (ath12k_debugfs_rx_filter(ar))
9550 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9551 	} else {
9552 		tlv_filter.rxmon_disable = true;
9553 	}
9554 
9555 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9556 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9557 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9558 						       ar->dp.mac_id + i,
9559 						       HAL_RXDMA_MONITOR_DST,
9560 						       DP_RXDMA_REFILL_RING_SIZE,
9561 						       &tlv_filter);
9562 		if (ret) {
9563 			ath12k_err(ab,
9564 				   "failed to setup filter for monitor buf %d\n",
9565 				   ret);
9566 		}
9567 	}
9568 
9569 	return ret;
9570 }
9571 
ath12k_mac_start(struct ath12k * ar)9572 static int ath12k_mac_start(struct ath12k *ar)
9573 {
9574 	struct ath12k_hw *ah = ar->ah;
9575 	struct ath12k_base *ab = ar->ab;
9576 	struct ath12k_pdev *pdev = ar->pdev;
9577 	int ret;
9578 
9579 	lockdep_assert_held(&ah->hw_mutex);
9580 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9581 
9582 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9583 					1, pdev->pdev_id);
9584 
9585 	if (ret) {
9586 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9587 		goto err;
9588 	}
9589 
9590 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9591 					pdev->pdev_id);
9592 	if (ret) {
9593 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9594 		goto err;
9595 	}
9596 
9597 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9598 					0, pdev->pdev_id);
9599 	if (ret) {
9600 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9601 			   ret);
9602 		goto err;
9603 	}
9604 
9605 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9606 	if (ret) {
9607 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9608 			   ret);
9609 		goto err;
9610 	}
9611 
9612 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9613 						  HTT_PPDU_STATS_TAG_DEFAULT);
9614 	if (ret) {
9615 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9616 		goto err;
9617 	}
9618 
9619 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9620 					1, pdev->pdev_id);
9621 
9622 	if (ret) {
9623 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9624 		goto err;
9625 	}
9626 
9627 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9628 
9629 	/* TODO: Do we need to enable ANI? */
9630 
9631 	ret = ath12k_reg_update_chan_list(ar, false);
9632 
9633 	/* The ar state alone can be turned off for non supported country
9634 	 * without returning the error value. As we need to update the channel
9635 	 * for the next ar.
9636 	 */
9637 	if (ret) {
9638 		if (ret == -EINVAL)
9639 			ret = 0;
9640 		goto err;
9641 	}
9642 
9643 	ar->num_started_vdevs = 0;
9644 	ar->num_created_vdevs = 0;
9645 	ar->num_peers = 0;
9646 	ar->allocated_vdev_map = 0;
9647 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9648 
9649 	/* Configure monitor status ring with default rx_filter to get rx status
9650 	 * such as rssi, rx_duration.
9651 	 */
9652 	ret = ath12k_mac_config_mon_status_default(ar, true);
9653 	if (ret && (ret != -EOPNOTSUPP)) {
9654 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9655 			   ret);
9656 		goto err;
9657 	}
9658 
9659 	if (ret == -EOPNOTSUPP)
9660 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9661 			   "monitor status config is not yet supported");
9662 
9663 	/* Configure the hash seed for hash based reo dest ring selection */
9664 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9665 
9666 	/* allow device to enter IMPS */
9667 	if (ab->hw_params->idle_ps) {
9668 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9669 						1, pdev->pdev_id);
9670 		if (ret) {
9671 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9672 			goto err;
9673 		}
9674 	}
9675 
9676 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9677 			   &ab->pdevs[ar->pdev_idx]);
9678 
9679 	return 0;
9680 err:
9681 
9682 	return ret;
9683 }
9684 
ath12k_drain_tx(struct ath12k_hw * ah)9685 static void ath12k_drain_tx(struct ath12k_hw *ah)
9686 {
9687 	struct ath12k *ar;
9688 	int i;
9689 
9690 	lockdep_assert_wiphy(ah->hw->wiphy);
9691 
9692 	for_each_ar(ah, ar, i)
9693 		ath12k_mac_drain_tx(ar);
9694 }
9695 
ath12k_mac_op_start(struct ieee80211_hw * hw)9696 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9697 {
9698 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9699 	struct ath12k *ar;
9700 	int ret, i;
9701 
9702 	if (ath12k_ftm_mode)
9703 		return -EPERM;
9704 
9705 	lockdep_assert_wiphy(hw->wiphy);
9706 
9707 	ath12k_drain_tx(ah);
9708 
9709 	guard(mutex)(&ah->hw_mutex);
9710 
9711 	switch (ah->state) {
9712 	case ATH12K_HW_STATE_OFF:
9713 		ah->state = ATH12K_HW_STATE_ON;
9714 		break;
9715 	case ATH12K_HW_STATE_RESTARTING:
9716 		ah->state = ATH12K_HW_STATE_RESTARTED;
9717 		break;
9718 	case ATH12K_HW_STATE_RESTARTED:
9719 	case ATH12K_HW_STATE_WEDGED:
9720 	case ATH12K_HW_STATE_ON:
9721 	case ATH12K_HW_STATE_TM:
9722 		ah->state = ATH12K_HW_STATE_OFF;
9723 
9724 		WARN_ON(1);
9725 		return -EINVAL;
9726 	}
9727 
9728 	for_each_ar(ah, ar, i) {
9729 		ret = ath12k_mac_start(ar);
9730 		if (ret) {
9731 			ah->state = ATH12K_HW_STATE_OFF;
9732 
9733 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9734 				   ar->pdev_idx, ret);
9735 			goto fail_start;
9736 		}
9737 	}
9738 
9739 	return 0;
9740 
9741 fail_start:
9742 	for (; i > 0; i--) {
9743 		ar = ath12k_ah_to_ar(ah, i - 1);
9744 		ath12k_mac_stop(ar);
9745 	}
9746 
9747 	return ret;
9748 }
9749 EXPORT_SYMBOL(ath12k_mac_op_start);
9750 
ath12k_mac_rfkill_config(struct ath12k * ar)9751 int ath12k_mac_rfkill_config(struct ath12k *ar)
9752 {
9753 	struct ath12k_base *ab = ar->ab;
9754 	u32 param;
9755 	int ret;
9756 
9757 	if (ab->hw_params->rfkill_pin == 0)
9758 		return -EOPNOTSUPP;
9759 
9760 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9761 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9762 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9763 		   ab->hw_params->rfkill_on_level);
9764 
9765 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9766 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9767 		u32_encode_bits(ab->hw_params->rfkill_pin,
9768 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9769 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9770 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9771 
9772 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9773 					param, ar->pdev->pdev_id);
9774 	if (ret) {
9775 		ath12k_warn(ab,
9776 			    "failed to set rfkill config 0x%x: %d\n",
9777 			    param, ret);
9778 		return ret;
9779 	}
9780 
9781 	return 0;
9782 }
9783 
ath12k_mac_rfkill_enable_radio(struct ath12k * ar,bool enable)9784 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9785 {
9786 	enum wmi_rfkill_enable_radio param;
9787 	int ret;
9788 
9789 	if (enable)
9790 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9791 	else
9792 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9793 
9794 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9795 		   ar->pdev_idx, param);
9796 
9797 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9798 					param, ar->pdev->pdev_id);
9799 	if (ret) {
9800 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9801 			    param, ret);
9802 		return ret;
9803 	}
9804 
9805 	return 0;
9806 }
9807 
ath12k_mac_stop(struct ath12k * ar)9808 static void ath12k_mac_stop(struct ath12k *ar)
9809 {
9810 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
9811 	struct ath12k_hw *ah = ar->ah;
9812 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9813 	struct ath12k_wmi_scan_chan_list_arg *arg;
9814 	int ret;
9815 
9816 	lockdep_assert_held(&ah->hw_mutex);
9817 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9818 
9819 	ret = ath12k_mac_config_mon_status_default(ar, false);
9820 	if (ret && (ret != -EOPNOTSUPP))
9821 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9822 			   ret);
9823 
9824 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9825 
9826 	cancel_delayed_work_sync(&ar->scan.timeout);
9827 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9828 	cancel_work_sync(&ar->regd_channel_update_work);
9829 	cancel_work_sync(&ar->regd_update_work);
9830 	cancel_work_sync(&ar->ab->rfkill_work);
9831 	cancel_work_sync(&ar->ab->update_11d_work);
9832 	ar->state_11d = ATH12K_11D_IDLE;
9833 	complete(&ar->completed_11d_scan);
9834 
9835 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
9836 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
9837 		list_del(&ppdu_stats->list);
9838 		kfree(ppdu_stats);
9839 	}
9840 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
9841 
9842 	spin_lock_bh(&ar->data_lock);
9843 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9844 					       struct ath12k_wmi_scan_chan_list_arg,
9845 					       list))) {
9846 		list_del(&arg->list);
9847 		kfree(arg);
9848 	}
9849 	spin_unlock_bh(&ar->data_lock);
9850 
9851 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9852 
9853 	synchronize_rcu();
9854 
9855 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9856 }
9857 
ath12k_mac_op_stop(struct ieee80211_hw * hw,bool suspend)9858 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9859 {
9860 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9861 	struct ath12k *ar;
9862 	int i;
9863 
9864 	lockdep_assert_wiphy(hw->wiphy);
9865 
9866 	ath12k_drain_tx(ah);
9867 
9868 	mutex_lock(&ah->hw_mutex);
9869 
9870 	ah->state = ATH12K_HW_STATE_OFF;
9871 
9872 	for_each_ar(ah, ar, i)
9873 		ath12k_mac_stop(ar);
9874 
9875 	mutex_unlock(&ah->hw_mutex);
9876 }
9877 EXPORT_SYMBOL(ath12k_mac_op_stop);
9878 
9879 static u8
ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif * arvif)9880 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9881 {
9882 	struct ath12k_base *ab = arvif->ar->ab;
9883 	u8 vdev_stats_id = 0;
9884 
9885 	do {
9886 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
9887 			vdev_stats_id++;
9888 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
9889 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
9890 				break;
9891 			}
9892 		} else {
9893 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
9894 			break;
9895 		}
9896 	} while (vdev_stats_id);
9897 
9898 	arvif->vdev_stats_id = vdev_stats_id;
9899 	return vdev_stats_id;
9900 }
9901 
ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif * arvif,u32 * flags,u32 * tx_vdev_id)9902 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
9903 					       u32 *flags, u32 *tx_vdev_id)
9904 {
9905 	struct ath12k_vif *ahvif = arvif->ahvif;
9906 	struct ieee80211_bss_conf *link_conf;
9907 	struct ath12k *ar = arvif->ar;
9908 	struct ath12k_link_vif *tx_arvif;
9909 
9910 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
9911 	if (!link_conf) {
9912 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
9913 			    ahvif->vif->addr, arvif->link_id);
9914 		return -ENOLINK;
9915 	}
9916 
9917 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
9918 	if (!tx_arvif)
9919 		return 0;
9920 
9921 	if (link_conf->nontransmitted) {
9922 		if (ath12k_ar_to_hw(ar)->wiphy !=
9923 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
9924 			return -EINVAL;
9925 
9926 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
9927 		*tx_vdev_id = tx_arvif->vdev_id;
9928 	} else if (tx_arvif == arvif) {
9929 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
9930 	} else {
9931 		return -EINVAL;
9932 	}
9933 
9934 	if (link_conf->ema_ap)
9935 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
9936 
9937 	return 0;
9938 }
9939 
ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif * arvif,struct ath12k_wmi_vdev_create_arg * arg)9940 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
9941 					    struct ath12k_wmi_vdev_create_arg *arg)
9942 {
9943 	struct ath12k *ar = arvif->ar;
9944 	struct ath12k_pdev *pdev = ar->pdev;
9945 	struct ath12k_vif *ahvif = arvif->ahvif;
9946 	int ret;
9947 
9948 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9949 
9950 	arg->if_id = arvif->vdev_id;
9951 	arg->type = ahvif->vdev_type;
9952 	arg->subtype = ahvif->vdev_subtype;
9953 	arg->pdev_id = pdev->pdev_id;
9954 
9955 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
9956 	arg->mbssid_tx_vdev_id = 0;
9957 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
9958 		      ar->ab->wmi_ab.svc_map)) {
9959 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
9960 							  &arg->mbssid_flags,
9961 							  &arg->mbssid_tx_vdev_id);
9962 		if (ret)
9963 			return ret;
9964 	}
9965 
9966 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9967 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
9968 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
9969 	}
9970 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9971 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
9972 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
9973 	}
9974 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9975 	    ar->supports_6ghz) {
9976 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
9977 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
9978 	}
9979 
9980 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
9981 
9982 	if (ath12k_mac_is_ml_arvif(arvif)) {
9983 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
9984 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
9985 				    ahvif->vif->valid_links);
9986 			return -EINVAL;
9987 		}
9988 
9989 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
9990 	}
9991 
9992 	return 0;
9993 }
9994 
ath12k_mac_update_vif_offload(struct ath12k_link_vif * arvif)9995 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
9996 {
9997 	struct ath12k_vif *ahvif = arvif->ahvif;
9998 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
9999 	struct ath12k *ar = arvif->ar;
10000 	struct ath12k_base *ab = ar->ab;
10001 	u32 param_id, param_value;
10002 	int ret;
10003 
10004 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
10005 	if (vif->type != NL80211_IFTYPE_STATION &&
10006 	    vif->type != NL80211_IFTYPE_AP)
10007 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
10008 					IEEE80211_OFFLOAD_DECAP_ENABLED);
10009 
10010 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
10011 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
10012 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10013 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
10014 	else
10015 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
10016 
10017 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10018 					    param_id, ahvif->dp_vif.tx_encap_type);
10019 	if (ret) {
10020 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10021 			    arvif->vdev_id, ret);
10022 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10023 	}
10024 
10025 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10026 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10027 		param_value = ATH12K_HW_TXRX_ETHERNET;
10028 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10029 		param_value = ATH12K_HW_TXRX_RAW;
10030 	else
10031 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10032 
10033 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10034 					    param_id, param_value);
10035 	if (ret) {
10036 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10037 			    arvif->vdev_id, ret);
10038 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10039 	}
10040 }
10041 
ath12k_mac_op_update_vif_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10042 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10043 				      struct ieee80211_vif *vif)
10044 {
10045 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10046 	struct ath12k_link_vif *arvif;
10047 	unsigned long links;
10048 	int link_id;
10049 
10050 	lockdep_assert_wiphy(hw->wiphy);
10051 
10052 	if (vif->valid_links) {
10053 		links = vif->valid_links;
10054 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10055 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10056 			if (!(arvif && arvif->ar))
10057 				continue;
10058 
10059 			ath12k_mac_update_vif_offload(arvif);
10060 		}
10061 
10062 		return;
10063 	}
10064 
10065 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10066 }
10067 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10068 
ath12k_mac_vif_ap_active_any(struct ath12k_base * ab)10069 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10070 {
10071 	struct ath12k *ar;
10072 	struct ath12k_pdev *pdev;
10073 	struct ath12k_link_vif *arvif;
10074 	int i;
10075 
10076 	for (i = 0; i < ab->num_radios; i++) {
10077 		pdev = &ab->pdevs[i];
10078 		ar = pdev->ar;
10079 		list_for_each_entry(arvif, &ar->arvifs, list) {
10080 			if (arvif->is_up &&
10081 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10082 				return true;
10083 		}
10084 	}
10085 	return false;
10086 }
10087 
ath12k_mac_11d_scan_start(struct ath12k * ar,u32 vdev_id)10088 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10089 {
10090 	struct wmi_11d_scan_start_arg arg;
10091 	int ret;
10092 
10093 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10094 
10095 	if (ar->regdom_set_by_user)
10096 		goto fin;
10097 
10098 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10099 		goto fin;
10100 
10101 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10102 		goto fin;
10103 
10104 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10105 		goto fin;
10106 
10107 	arg.vdev_id = vdev_id;
10108 	arg.start_interval_msec = 0;
10109 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10110 
10111 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10112 		   "mac start 11d scan for vdev %d\n", vdev_id);
10113 
10114 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10115 	if (ret) {
10116 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10117 			    vdev_id, ret);
10118 	} else {
10119 		ar->vdev_id_11d_scan = vdev_id;
10120 		if (ar->state_11d == ATH12K_11D_PREPARING)
10121 			ar->state_11d = ATH12K_11D_RUNNING;
10122 	}
10123 
10124 fin:
10125 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10126 		ar->state_11d = ATH12K_11D_IDLE;
10127 		complete(&ar->completed_11d_scan);
10128 	}
10129 }
10130 
ath12k_mac_11d_scan_stop(struct ath12k * ar)10131 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10132 {
10133 	int ret;
10134 	u32 vdev_id;
10135 
10136 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10137 
10138 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10139 		return;
10140 
10141 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10142 		   ar->vdev_id_11d_scan);
10143 
10144 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10145 		ar->state_11d = ATH12K_11D_IDLE;
10146 		complete(&ar->completed_11d_scan);
10147 	}
10148 
10149 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10150 		vdev_id = ar->vdev_id_11d_scan;
10151 
10152 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10153 		if (ret) {
10154 			ath12k_warn(ar->ab,
10155 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10156 				    vdev_id, ret);
10157 		} else {
10158 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10159 			ar->state_11d = ATH12K_11D_IDLE;
10160 			complete(&ar->completed_11d_scan);
10161 		}
10162 	}
10163 }
10164 
ath12k_mac_11d_scan_stop_all(struct ath12k_base * ab)10165 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10166 {
10167 	struct ath12k *ar;
10168 	struct ath12k_pdev *pdev;
10169 	int i;
10170 
10171 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10172 
10173 	for (i = 0; i < ab->num_radios; i++) {
10174 		pdev = &ab->pdevs[i];
10175 		ar = pdev->ar;
10176 
10177 		ath12k_mac_11d_scan_stop(ar);
10178 	}
10179 }
10180 
ath12k_mac_determine_vdev_type(struct ieee80211_vif * vif,struct ath12k_vif * ahvif)10181 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10182 					   struct ath12k_vif *ahvif)
10183 {
10184 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10185 
10186 	switch (vif->type) {
10187 	case NL80211_IFTYPE_UNSPECIFIED:
10188 	case NL80211_IFTYPE_STATION:
10189 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10190 
10191 		if (vif->p2p)
10192 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10193 
10194 		break;
10195 	case NL80211_IFTYPE_MESH_POINT:
10196 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10197 		fallthrough;
10198 	case NL80211_IFTYPE_AP:
10199 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10200 
10201 		if (vif->p2p)
10202 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10203 
10204 		break;
10205 	case NL80211_IFTYPE_MONITOR:
10206 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10207 		break;
10208 	case NL80211_IFTYPE_P2P_DEVICE:
10209 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10210 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10211 		break;
10212 	default:
10213 		WARN_ON(1);
10214 		break;
10215 	}
10216 }
10217 
ath12k_mac_vdev_create(struct ath12k * ar,struct ath12k_link_vif * arvif)10218 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10219 {
10220 	struct ath12k_hw *ah = ar->ah;
10221 	struct ath12k_base *ab = ar->ab;
10222 	struct ieee80211_hw *hw = ah->hw;
10223 	struct ath12k_vif *ahvif = arvif->ahvif;
10224 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10225 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10226 	struct ath12k_wmi_peer_create_arg peer_param = {};
10227 	struct ieee80211_bss_conf *link_conf = NULL;
10228 	u32 param_id, param_value;
10229 	u16 nss;
10230 	int i;
10231 	int ret, vdev_id;
10232 	u8 link_id;
10233 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10234 	struct ath12k_dp_peer_create_params params = {};
10235 	bool dp_peer_created = false;
10236 
10237 	lockdep_assert_wiphy(hw->wiphy);
10238 
10239 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10240 	 * interface in each radio
10241 	 */
10242 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10243 		return -EINVAL;
10244 
10245 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10246 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10247 			    TARGET_NUM_VDEVS(ab));
10248 		return -ENOSPC;
10249 	}
10250 
10251 	link_id = arvif->link_id;
10252 
10253 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10254 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10255 		if (!link_conf) {
10256 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10257 				    vif->addr, arvif->link_id);
10258 			return -ENOLINK;
10259 		}
10260 	}
10261 
10262 	if (link_conf)
10263 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10264 	else
10265 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10266 
10267 	arvif->ar = ar;
10268 	vdev_id = __ffs64(ab->free_vdev_map);
10269 	arvif->vdev_id = vdev_id;
10270 	if (vif->type == NL80211_IFTYPE_MONITOR)
10271 		ar->monitor_vdev_id = vdev_id;
10272 
10273 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10274 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10275 		   ab->free_vdev_map);
10276 
10277 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10278 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10279 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10280 
10281 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10282 	if (ret) {
10283 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10284 			    arvif->vdev_id, ret);
10285 		goto err;
10286 	}
10287 
10288 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10289 	if (ret) {
10290 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10291 			    arvif->vdev_id, ret);
10292 		return ret;
10293 	}
10294 
10295 	ar->num_created_vdevs++;
10296 	arvif->is_created = true;
10297 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10298 		   vif->addr, arvif->vdev_id);
10299 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10300 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10301 
10302 	spin_lock_bh(&ar->data_lock);
10303 	list_add(&arvif->list, &ar->arvifs);
10304 	spin_unlock_bh(&ar->data_lock);
10305 
10306 	ath12k_mac_update_vif_offload(arvif);
10307 
10308 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10309 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10310 					    WMI_VDEV_PARAM_NSS, nss);
10311 	if (ret) {
10312 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10313 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10314 		goto err_vdev_del;
10315 	}
10316 
10317 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10318 
10319 	dp_link_vif->vdev_id = arvif->vdev_id;
10320 	dp_link_vif->lmac_id = ar->lmac_id;
10321 	dp_link_vif->pdev_idx = ar->pdev_idx;
10322 
10323 	switch (ahvif->vdev_type) {
10324 	case WMI_VDEV_TYPE_AP:
10325 		params.ucast_ra_only = true;
10326 
10327 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10328 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10329 			if (ret) {
10330 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10331 					    arvif->vdev_id, ret);
10332 				goto err_vdev_del;
10333 			}
10334 			dp_peer_created = true;
10335 		}
10336 
10337 		peer_param.vdev_id = arvif->vdev_id;
10338 		peer_param.peer_addr = arvif->bssid;
10339 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10340 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10341 		if (ret) {
10342 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10343 				    arvif->vdev_id, ret);
10344 			goto err_dp_peer_del;
10345 		}
10346 
10347 		ret = ath12k_mac_set_kickout(arvif);
10348 		if (ret) {
10349 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10350 				    arvif->vdev_id, ret);
10351 			goto err_peer_del;
10352 		}
10353 		ath12k_mac_11d_scan_stop_all(ar->ab);
10354 		break;
10355 	case WMI_VDEV_TYPE_STA:
10356 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10357 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10358 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10359 						  param_id, param_value);
10360 		if (ret) {
10361 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10362 				    arvif->vdev_id, ret);
10363 			goto err_peer_del;
10364 		}
10365 
10366 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10367 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10368 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10369 						  param_id, param_value);
10370 		if (ret) {
10371 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10372 				    arvif->vdev_id, ret);
10373 			goto err_peer_del;
10374 		}
10375 
10376 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10377 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10378 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10379 						  param_id, param_value);
10380 		if (ret) {
10381 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10382 				    arvif->vdev_id, ret);
10383 			goto err_peer_del;
10384 		}
10385 
10386 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10387 		if (ret) {
10388 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10389 				    arvif->vdev_id, ret);
10390 			goto err_peer_del;
10391 		}
10392 
10393 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10394 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10395 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10396 			reinit_completion(&ar->completed_11d_scan);
10397 			ar->state_11d = ATH12K_11D_PREPARING;
10398 		}
10399 		break;
10400 	case WMI_VDEV_TYPE_MONITOR:
10401 		ar->monitor_vdev_created = true;
10402 		break;
10403 	default:
10404 		break;
10405 	}
10406 
10407 	if (link_conf)
10408 		arvif->txpower = link_conf->txpower;
10409 	else
10410 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10411 
10412 	ret = ath12k_mac_txpower_recalc(ar);
10413 	if (ret)
10414 		goto err_peer_del;
10415 
10416 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10417 	param_value = hw->wiphy->rts_threshold;
10418 	ar->rts_threshold = param_value;
10419 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10420 					    param_id, param_value);
10421 	if (ret) {
10422 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10423 			    arvif->vdev_id, ret);
10424 	}
10425 
10426 	ath12k_dp_vdev_tx_attach(ar, arvif);
10427 
10428 	return ret;
10429 
10430 err_peer_del:
10431 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10432 		reinit_completion(&ar->peer_delete_done);
10433 
10434 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10435 						      arvif->vdev_id);
10436 		if (ret) {
10437 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10438 				    arvif->vdev_id, arvif->bssid);
10439 			goto err;
10440 		}
10441 
10442 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10443 						       arvif->bssid);
10444 		if (ret)
10445 			goto err_vdev_del;
10446 
10447 		ar->num_peers--;
10448 	}
10449 
10450 err_dp_peer_del:
10451 	if (dp_peer_created)
10452 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10453 
10454 err_vdev_del:
10455 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10456 		ar->monitor_vdev_id = -1;
10457 		ar->monitor_vdev_created = false;
10458 	}
10459 
10460 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10461 	ar->num_created_vdevs--;
10462 	arvif->is_created = false;
10463 	arvif->ar = NULL;
10464 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10465 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10466 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10467 	spin_lock_bh(&ar->data_lock);
10468 	list_del(&arvif->list);
10469 	spin_unlock_bh(&ar->data_lock);
10470 
10471 err:
10472 	arvif->ar = NULL;
10473 	return ret;
10474 }
10475 
ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif * arvif)10476 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10477 {
10478 	struct ath12k_key_conf *key_conf, *tmp;
10479 	struct ath12k_vif *ahvif = arvif->ahvif;
10480 	struct ath12k_hw *ah = ahvif->ah;
10481 	struct ath12k_sta *ahsta;
10482 	struct ath12k_link_sta *arsta;
10483 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10484 	int ret;
10485 
10486 	lockdep_assert_wiphy(ah->hw->wiphy);
10487 
10488 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10489 		arsta = NULL;
10490 		if (key_conf->sta) {
10491 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10492 			arsta = wiphy_dereference(ah->hw->wiphy,
10493 						  ahsta->link[arvif->link_id]);
10494 			if (!arsta)
10495 				goto free_cache;
10496 		}
10497 
10498 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10499 					 arvif, arsta,
10500 					 key_conf->key);
10501 		if (ret)
10502 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10503 				    arvif->vdev_id, ret);
10504 free_cache:
10505 		list_del(&key_conf->list);
10506 		kfree(key_conf);
10507 	}
10508 }
10509 
ath12k_mac_vif_cache_flush(struct ath12k * ar,struct ath12k_link_vif * arvif)10510 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10511 {
10512 	struct ath12k_vif *ahvif = arvif->ahvif;
10513 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10514 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10515 	struct ath12k_base *ab = ar->ab;
10516 	struct ieee80211_bss_conf *link_conf;
10517 
10518 	int ret;
10519 
10520 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10521 
10522 	if (!cache)
10523 		return;
10524 
10525 	if (cache->tx_conf.changed) {
10526 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10527 					 &cache->tx_conf.tx_queue_params);
10528 		if (ret)
10529 			ath12k_warn(ab,
10530 				    "unable to apply tx config parameters to vdev %d\n",
10531 				    ret);
10532 	}
10533 
10534 	if (cache->bss_conf_changed) {
10535 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10536 		if (!link_conf) {
10537 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10538 				    vif->addr, arvif->link_id);
10539 			return;
10540 		}
10541 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10542 					    cache->bss_conf_changed);
10543 	}
10544 
10545 	if (!list_empty(&cache->key_conf.list))
10546 		ath12k_mac_vif_flush_key_cache(arvif);
10547 
10548 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10549 }
10550 
ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw * hw,struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)10551 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10552 						    struct ath12k_link_vif *arvif,
10553 						    struct ieee80211_chanctx_conf *ctx)
10554 {
10555 	struct ath12k_vif *ahvif = arvif->ahvif;
10556 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10557 	struct ath12k_link_vif *scan_arvif;
10558 	struct ath12k_hw *ah = hw->priv;
10559 	struct ath12k *ar;
10560 	struct ath12k_base *ab;
10561 	u8 link_id = arvif->link_id, scan_link_id;
10562 	unsigned long scan_link_map;
10563 	int ret;
10564 
10565 	lockdep_assert_wiphy(hw->wiphy);
10566 
10567 	if (ah->num_radio == 1)
10568 		ar = ah->radio;
10569 	else if (ctx)
10570 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10571 	else
10572 		return NULL;
10573 
10574 	if (!ar)
10575 		return NULL;
10576 
10577 	/* cleanup the scan vdev if we are done scan on that ar
10578 	 * and now we want to create for actual usage.
10579 	 */
10580 	if (ieee80211_vif_is_mld(vif)) {
10581 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10582 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10583 			scan_arvif = wiphy_dereference(hw->wiphy,
10584 						       ahvif->link[scan_link_id]);
10585 			if (scan_arvif && scan_arvif->ar == ar) {
10586 				ar->scan.arvif = NULL;
10587 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10588 				ath12k_mac_unassign_link_vif(scan_arvif);
10589 				break;
10590 			}
10591 		}
10592 	}
10593 
10594 	if (arvif->ar) {
10595 		/* This is not expected really */
10596 		if (WARN_ON(!arvif->is_created)) {
10597 			arvif->ar = NULL;
10598 			return NULL;
10599 		}
10600 
10601 		if (ah->num_radio == 1)
10602 			return arvif->ar;
10603 
10604 		/* This can happen as scan vdev gets created during multiple scans
10605 		 * across different radios before a vdev is brought up in
10606 		 * a certain radio.
10607 		 */
10608 		if (ar != arvif->ar) {
10609 			if (WARN_ON(arvif->is_started))
10610 				return NULL;
10611 
10612 			ath12k_mac_remove_link_interface(hw, arvif);
10613 			ath12k_mac_unassign_link_vif(arvif);
10614 		}
10615 	}
10616 
10617 	ab = ar->ab;
10618 
10619 	/* Assign arvif again here since previous radio switch block
10620 	 * would've unassigned and cleared it.
10621 	 */
10622 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10623 	if (vif->type == NL80211_IFTYPE_AP &&
10624 	    ar->num_peers > (ar->max_num_peers - 1)) {
10625 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10626 		goto unlock;
10627 	}
10628 
10629 	if (arvif->is_created)
10630 		goto flush;
10631 
10632 	ret = ath12k_mac_vdev_create(ar, arvif);
10633 	if (ret) {
10634 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10635 		goto unlock;
10636 	}
10637 
10638 flush:
10639 	/* If the vdev is created during channel assign and not during
10640 	 * add_interface(), Apply any parameters for the vdev which were received
10641 	 * after add_interface, corresponding to this vif.
10642 	 */
10643 	ath12k_mac_vif_cache_flush(ar, arvif);
10644 unlock:
10645 	return arvif->ar;
10646 }
10647 
ath12k_mac_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10648 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10649 				struct ieee80211_vif *vif)
10650 {
10651 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10652 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10653 	struct ath12k_reg_info *reg_info;
10654 	struct ath12k_link_vif *arvif;
10655 	struct ath12k_base *ab;
10656 	struct ath12k *ar;
10657 	int i;
10658 
10659 	lockdep_assert_wiphy(hw->wiphy);
10660 
10661 	memset(ahvif, 0, sizeof(*ahvif));
10662 
10663 	ahvif->ah = ah;
10664 	ahvif->vif = vif;
10665 	arvif = &ahvif->deflink;
10666 
10667 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10668 
10669 	/* Allocate Default Queue now and reassign during actual vdev create */
10670 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10671 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10672 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10673 
10674 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10675 
10676 	ath12k_mac_determine_vdev_type(vif, ahvif);
10677 
10678 	for_each_ar(ah, ar, i) {
10679 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10680 			continue;
10681 
10682 		ab = ar->ab;
10683 		reg_info = ab->reg_info[ar->pdev_idx];
10684 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10685 		ah->regd_updated = false;
10686 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10687 					    IEEE80211_REG_UNSET_AP);
10688 		break;
10689 	}
10690 
10691 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10692 	 * will not know if this interface is an ML vif at this point.
10693 	 */
10694 	return 0;
10695 }
10696 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10697 
ath12k_mac_vif_unref(struct ath12k_dp * dp,struct ieee80211_vif * vif)10698 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10699 {
10700 	struct ath12k_tx_desc_info *tx_desc_info;
10701 	struct ath12k_skb_cb *skb_cb;
10702 	struct sk_buff *skb;
10703 	int i;
10704 
10705 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10706 		spin_lock_bh(&dp->tx_desc_lock[i]);
10707 
10708 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10709 				    list) {
10710 			skb = tx_desc_info->skb;
10711 			if (!skb)
10712 				continue;
10713 
10714 			skb_cb = ATH12K_SKB_CB(skb);
10715 			if (skb_cb->vif == vif)
10716 				skb_cb->vif = NULL;
10717 		}
10718 
10719 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10720 	}
10721 }
10722 
ath12k_mac_vdev_delete(struct ath12k * ar,struct ath12k_link_vif * arvif)10723 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10724 {
10725 	struct ath12k_vif *ahvif = arvif->ahvif;
10726 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10727 	struct ath12k_dp_link_vif *dp_link_vif;
10728 	struct ath12k_base *ab = ar->ab;
10729 	unsigned long time_left;
10730 	int ret;
10731 
10732 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10733 
10734 	reinit_completion(&ar->vdev_delete_done);
10735 
10736 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10737 	if (ret) {
10738 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10739 			    arvif->vdev_id, ret);
10740 		goto err_vdev_del;
10741 	}
10742 
10743 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10744 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10745 	if (time_left == 0) {
10746 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10747 		goto err_vdev_del;
10748 	}
10749 
10750 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10751 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10752 	ar->num_created_vdevs--;
10753 
10754 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10755 		ar->monitor_vdev_id = -1;
10756 		ar->monitor_vdev_created = false;
10757 	}
10758 
10759 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10760 		   vif->addr, arvif->vdev_id);
10761 
10762 err_vdev_del:
10763 	spin_lock_bh(&ar->data_lock);
10764 	list_del(&arvif->list);
10765 	spin_unlock_bh(&ar->data_lock);
10766 
10767 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10768 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10769 
10770 	idr_for_each(&ar->txmgmt_idr,
10771 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10772 
10773 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10774 
10775 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10776 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
10777 
10778 	/* Recalc txpower for remaining vdev */
10779 	ath12k_mac_txpower_recalc(ar);
10780 
10781 	/* TODO: recal traffic pause state based on the available vdevs */
10782 	arvif->is_created = false;
10783 	arvif->ar = NULL;
10784 
10785 	return ret;
10786 }
10787 
ath12k_mac_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10788 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10789 				    struct ieee80211_vif *vif)
10790 {
10791 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10792 	struct ath12k_link_vif *arvif;
10793 	struct ath12k *ar;
10794 	u8 link_id;
10795 
10796 	lockdep_assert_wiphy(hw->wiphy);
10797 
10798 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10799 		/* if we cached some config but never received assign chanctx,
10800 		 * free the allocated cache.
10801 		 */
10802 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10803 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10804 		if (!arvif || !arvif->is_created)
10805 			continue;
10806 
10807 		ar = arvif->ar;
10808 
10809 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10810 		 * is expected to be executed. Since link is anyways going to be removed
10811 		 * now, just cancel the worker and send the scan aborted to user space
10812 		 */
10813 		if (ar->scan.arvif == arvif) {
10814 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10815 
10816 			spin_lock_bh(&ar->data_lock);
10817 			ar->scan.arvif = NULL;
10818 			if (!ar->scan.is_roc) {
10819 				struct cfg80211_scan_info info = {
10820 					.aborted = true,
10821 				};
10822 
10823 				ath12k_mac_scan_send_complete(ar, &info);
10824 			}
10825 
10826 			ar->scan.state = ATH12K_SCAN_IDLE;
10827 			ar->scan_channel = NULL;
10828 			ar->scan.roc_freq = 0;
10829 			spin_unlock_bh(&ar->data_lock);
10830 		}
10831 
10832 		ath12k_mac_remove_link_interface(hw, arvif);
10833 		ath12k_mac_unassign_link_vif(arvif);
10834 	}
10835 }
10836 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
10837 
10838 /* FIXME: Has to be verified. */
10839 #define SUPPORTED_FILTERS			\
10840 	(FIF_ALLMULTI |				\
10841 	FIF_CONTROL |				\
10842 	FIF_PSPOLL |				\
10843 	FIF_OTHER_BSS |				\
10844 	FIF_BCN_PRBRESP_PROMISC |		\
10845 	FIF_PROBE_REQ |				\
10846 	FIF_FCSFAIL)
10847 
ath12k_mac_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)10848 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10849 				    unsigned int changed_flags,
10850 				    unsigned int *total_flags,
10851 				    u64 multicast)
10852 {
10853 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10854 	struct ath12k *ar;
10855 
10856 	lockdep_assert_wiphy(hw->wiphy);
10857 
10858 	ar = ath12k_ah_to_ar(ah, 0);
10859 
10860 	*total_flags &= SUPPORTED_FILTERS;
10861 	ar->filter_flags = *total_flags;
10862 }
10863 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
10864 
ath12k_mac_op_get_antenna(struct ieee80211_hw * hw,int radio_idx,u32 * tx_ant,u32 * rx_ant)10865 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
10866 			      u32 *tx_ant, u32 *rx_ant)
10867 {
10868 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10869 	int antennas_rx = 0, antennas_tx = 0;
10870 	struct ath12k *ar;
10871 	int i;
10872 
10873 	lockdep_assert_wiphy(hw->wiphy);
10874 
10875 	for_each_ar(ah, ar, i) {
10876 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
10877 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
10878 	}
10879 
10880 	*tx_ant = antennas_tx;
10881 	*rx_ant = antennas_rx;
10882 
10883 	return 0;
10884 }
10885 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
10886 
ath12k_mac_op_set_antenna(struct ieee80211_hw * hw,int radio_idx,u32 tx_ant,u32 rx_ant)10887 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
10888 			      u32 tx_ant, u32 rx_ant)
10889 {
10890 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10891 	struct ath12k *ar;
10892 	int ret = 0;
10893 	int i;
10894 
10895 	lockdep_assert_wiphy(hw->wiphy);
10896 
10897 	for_each_ar(ah, ar, i) {
10898 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
10899 		if (ret)
10900 			break;
10901 	}
10902 
10903 	return ret;
10904 }
10905 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
10906 
ath12k_mac_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params,u8 link_id)10907 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
10908 				   struct ieee80211_vif *vif,
10909 				   struct ieee80211_ampdu_params *params,
10910 				   u8 link_id)
10911 {
10912 	struct ath12k *ar;
10913 	int ret = -EINVAL;
10914 
10915 	lockdep_assert_wiphy(hw->wiphy);
10916 
10917 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
10918 	if (!ar)
10919 		return -EINVAL;
10920 
10921 	switch (params->action) {
10922 	case IEEE80211_AMPDU_RX_START:
10923 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
10924 		break;
10925 	case IEEE80211_AMPDU_RX_STOP:
10926 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
10927 		break;
10928 	case IEEE80211_AMPDU_TX_START:
10929 	case IEEE80211_AMPDU_TX_STOP_CONT:
10930 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
10931 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
10932 	case IEEE80211_AMPDU_TX_OPERATIONAL:
10933 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
10934 		 * Tx aggregation requests.
10935 		 */
10936 		ret = -EOPNOTSUPP;
10937 		break;
10938 	}
10939 
10940 	if (ret)
10941 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
10942 			    params->action, vif->addr, link_id, ret);
10943 
10944 	return ret;
10945 }
10946 
ath12k_mac_op_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)10947 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
10948 			       struct ieee80211_vif *vif,
10949 			       struct ieee80211_ampdu_params *params)
10950 {
10951 	struct ieee80211_sta *sta = params->sta;
10952 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
10953 	unsigned long links_map = ahsta->links_map;
10954 	int ret = -EINVAL;
10955 	u8 link_id;
10956 
10957 	lockdep_assert_wiphy(hw->wiphy);
10958 
10959 	if (WARN_ON(!links_map))
10960 		return ret;
10961 
10962 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10963 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
10964 		if (ret)
10965 			return ret;
10966 	}
10967 
10968 	return 0;
10969 }
10970 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
10971 
ath12k_mac_op_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)10972 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
10973 			      struct ieee80211_chanctx_conf *ctx)
10974 {
10975 	struct ath12k *ar;
10976 	struct ath12k_base *ab;
10977 
10978 	lockdep_assert_wiphy(hw->wiphy);
10979 
10980 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10981 	if (!ar)
10982 		return -EINVAL;
10983 
10984 	ab = ar->ab;
10985 
10986 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10987 		   "mac chanctx add freq %u width %d ptr %p\n",
10988 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10989 
10990 	spin_lock_bh(&ar->data_lock);
10991 	/* TODO: In case of multiple channel context, populate rx_channel from
10992 	 * Rx PPDU desc information.
10993 	 */
10994 	ar->rx_channel = ctx->def.chan;
10995 	spin_unlock_bh(&ar->data_lock);
10996 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10997 
10998 	return 0;
10999 }
11000 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
11001 
ath12k_mac_op_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)11002 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
11003 				  struct ieee80211_chanctx_conf *ctx)
11004 {
11005 	struct ath12k *ar;
11006 	struct ath12k_base *ab;
11007 
11008 	lockdep_assert_wiphy(hw->wiphy);
11009 
11010 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11011 	if (!ar)
11012 		return;
11013 
11014 	ab = ar->ab;
11015 
11016 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11017 		   "mac chanctx remove freq %u width %d ptr %p\n",
11018 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11019 
11020 	spin_lock_bh(&ar->data_lock);
11021 	/* TODO: In case of there is one more channel context left, populate
11022 	 * rx_channel with the channel of that remaining channel context.
11023 	 */
11024 	ar->rx_channel = NULL;
11025 	spin_unlock_bh(&ar->data_lock);
11026 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11027 }
11028 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11029 
11030 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)11031 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11032 				     enum wmi_phy_mode mode,
11033 				     enum nl80211_band band,
11034 				     enum nl80211_iftype type)
11035 {
11036 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11037 	enum wmi_phy_mode down_mode;
11038 	int n = ar->mac.sbands[band].n_iftype_data;
11039 	int i;
11040 	struct ieee80211_sband_iftype_data *data;
11041 
11042 	if (mode < MODE_11BE_EHT20)
11043 		return mode;
11044 
11045 	data = ar->mac.iftype[band];
11046 	for (i = 0; i < n; i++) {
11047 		if (data[i].types_mask & BIT(type)) {
11048 			eht_cap = &data[i].eht_cap;
11049 			break;
11050 		}
11051 	}
11052 
11053 	if (eht_cap && eht_cap->has_eht)
11054 		return mode;
11055 
11056 	switch (mode) {
11057 	case MODE_11BE_EHT20:
11058 		down_mode = MODE_11AX_HE20;
11059 		break;
11060 	case MODE_11BE_EHT40:
11061 		down_mode = MODE_11AX_HE40;
11062 		break;
11063 	case MODE_11BE_EHT80:
11064 		down_mode = MODE_11AX_HE80;
11065 		break;
11066 	case MODE_11BE_EHT80_80:
11067 		down_mode = MODE_11AX_HE80_80;
11068 		break;
11069 	case MODE_11BE_EHT160:
11070 	case MODE_11BE_EHT160_160:
11071 	case MODE_11BE_EHT320:
11072 		down_mode = MODE_11AX_HE160;
11073 		break;
11074 	case MODE_11BE_EHT20_2G:
11075 		down_mode = MODE_11AX_HE20_2G;
11076 		break;
11077 	case MODE_11BE_EHT40_2G:
11078 		down_mode = MODE_11AX_HE40_2G;
11079 		break;
11080 	default:
11081 		down_mode = mode;
11082 		break;
11083 	}
11084 
11085 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11086 		   "mac vdev start phymode %s downgrade to %s\n",
11087 		   ath12k_mac_phymode_str(mode),
11088 		   ath12k_mac_phymode_str(down_mode));
11089 
11090 	return down_mode;
11091 }
11092 
11093 static void
ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif * arvif,struct wmi_ml_arg * ml_arg)11094 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11095 			     struct wmi_ml_arg *ml_arg)
11096 {
11097 	struct ath12k_vif *ahvif = arvif->ahvif;
11098 	struct wmi_ml_partner_info *partner_info;
11099 	struct ieee80211_bss_conf *link_conf;
11100 	struct ath12k_link_vif *arvif_p;
11101 	unsigned long links;
11102 	u8 link_id;
11103 
11104 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11105 
11106 	if (!ath12k_mac_is_ml_arvif(arvif))
11107 		return;
11108 
11109 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11110 		return;
11111 
11112 	ml_arg->enabled = true;
11113 
11114 	/* Driver always add a new link via VDEV START, FW takes
11115 	 * care of internally adding this link to existing
11116 	 * link vdevs which are advertised as partners below
11117 	 */
11118 	ml_arg->link_add = true;
11119 
11120 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11121 
11122 	partner_info = ml_arg->partner_info;
11123 
11124 	links = ahvif->links_map;
11125 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11126 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11127 
11128 		if (WARN_ON(!arvif_p))
11129 			continue;
11130 
11131 		if (arvif == arvif_p)
11132 			continue;
11133 
11134 		if (!arvif_p->is_started)
11135 			continue;
11136 
11137 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11138 					      ahvif->vif->link_conf[arvif_p->link_id]);
11139 
11140 		if (!link_conf)
11141 			continue;
11142 
11143 		partner_info->vdev_id = arvif_p->vdev_id;
11144 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11145 		ether_addr_copy(partner_info->addr, link_conf->addr);
11146 		ml_arg->num_partner_links++;
11147 		partner_info++;
11148 	}
11149 }
11150 
11151 static int
ath12k_mac_vdev_start_restart(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx,bool restart)11152 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11153 			      struct ieee80211_chanctx_conf *ctx,
11154 			      bool restart)
11155 {
11156 	struct ath12k *ar = arvif->ar;
11157 	struct ath12k_base *ab = ar->ab;
11158 	struct wmi_vdev_start_req_arg arg = {};
11159 	const struct cfg80211_chan_def *chandef = &ctx->def;
11160 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11161 	struct ath12k_vif *ahvif = arvif->ahvif;
11162 	struct ieee80211_bss_conf *link_conf;
11163 	unsigned int dfs_cac_time;
11164 	int ret;
11165 
11166 	lockdep_assert_wiphy(hw->wiphy);
11167 
11168 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11169 	if (!link_conf) {
11170 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11171 			    ahvif->vif->addr, arvif->link_id);
11172 		return -ENOLINK;
11173 	}
11174 
11175 	reinit_completion(&ar->vdev_setup_done);
11176 
11177 	arg.vdev_id = arvif->vdev_id;
11178 	arg.dtim_period = arvif->dtim_period;
11179 	arg.bcn_intval = arvif->beacon_interval;
11180 	arg.punct_bitmap = ~arvif->punct_bitmap;
11181 
11182 	arg.freq = chandef->chan->center_freq;
11183 	arg.band_center_freq1 = chandef->center_freq1;
11184 	arg.band_center_freq2 = chandef->center_freq2;
11185 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11186 
11187 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11188 							chandef->chan->band,
11189 							ahvif->vif->type);
11190 	arg.min_power = 0;
11191 	arg.max_power = chandef->chan->max_power;
11192 	arg.max_reg_power = chandef->chan->max_reg_power;
11193 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11194 
11195 	arg.pref_tx_streams = ar->num_tx_chains;
11196 	arg.pref_rx_streams = ar->num_rx_chains;
11197 
11198 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11199 	arg.mbssid_tx_vdev_id = 0;
11200 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11201 		     ar->ab->wmi_ab.svc_map)) {
11202 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11203 							  &arg.mbssid_flags,
11204 							  &arg.mbssid_tx_vdev_id);
11205 		if (ret)
11206 			return ret;
11207 	}
11208 
11209 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11210 		arg.ssid = ahvif->u.ap.ssid;
11211 		arg.ssid_len = ahvif->u.ap.ssid_len;
11212 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11213 
11214 		/* For now allow DFS for AP mode */
11215 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11216 
11217 		arg.freq2_radar = ctx->radar_enabled;
11218 
11219 		arg.passive = arg.chan_radar;
11220 
11221 		spin_lock_bh(&ab->base_lock);
11222 		arg.regdomain = ar->ab->dfs_region;
11223 		spin_unlock_bh(&ab->base_lock);
11224 
11225 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11226 	}
11227 
11228 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11229 
11230 	if (!restart)
11231 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11232 
11233 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11234 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11235 		   arg.vdev_id, arg.freq,
11236 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11237 
11238 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11239 	if (ret) {
11240 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11241 			    restart ? "restart" : "start", arg.vdev_id);
11242 		return ret;
11243 	}
11244 
11245 	ret = ath12k_mac_vdev_setup_sync(ar);
11246 	if (ret) {
11247 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11248 			    arg.vdev_id, restart ? "restart" : "start", ret);
11249 		return ret;
11250 	}
11251 
11252 	/* TODO: For now we only set TPC power here. However when
11253 	 * channel changes, say CSA, it should be updated again.
11254 	 */
11255 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11256 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11257 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11258 						   &arvif->reg_tpc_info);
11259 	}
11260 
11261 	ar->num_started_vdevs++;
11262 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11263 		   ahvif->vif->addr, arvif->vdev_id);
11264 
11265 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11266 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11267 	 * done before channel usage. This flag is used to drop rx packets.
11268 	 * during CAC.
11269 	 */
11270 	/* TODO: Set the flag for other interface types as required */
11271 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11272 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11273 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11274 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11275 
11276 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11277 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11278 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11279 	}
11280 
11281 	ret = ath12k_mac_set_txbf_conf(arvif);
11282 	if (ret)
11283 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11284 			    arvif->vdev_id, ret);
11285 
11286 	return 0;
11287 }
11288 
ath12k_mac_vdev_start(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)11289 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11290 				 struct ieee80211_chanctx_conf *ctx)
11291 {
11292 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11293 }
11294 
ath12k_mac_vdev_restart(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)11295 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11296 				   struct ieee80211_chanctx_conf *ctx)
11297 {
11298 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11299 }
11300 
11301 struct ath12k_mac_change_chanctx_arg {
11302 	struct ieee80211_chanctx_conf *ctx;
11303 	struct ieee80211_vif_chanctx_switch *vifs;
11304 	int n_vifs;
11305 	int next_vif;
11306 	struct ath12k *ar;
11307 };
11308 
11309 static void
ath12k_mac_change_chanctx_cnt_iter(void * data,u8 * mac,struct ieee80211_vif * vif)11310 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11311 				   struct ieee80211_vif *vif)
11312 {
11313 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11314 	struct ath12k_mac_change_chanctx_arg *arg = data;
11315 	struct ieee80211_bss_conf *link_conf;
11316 	struct ath12k_link_vif *arvif;
11317 	unsigned long links_map;
11318 	u8 link_id;
11319 
11320 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11321 
11322 	links_map = ahvif->links_map;
11323 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11324 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11325 		if (WARN_ON(!arvif))
11326 			continue;
11327 
11328 		if (!arvif->is_created || arvif->ar != arg->ar)
11329 			continue;
11330 
11331 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11332 					      vif->link_conf[link_id]);
11333 		if (WARN_ON(!link_conf))
11334 			continue;
11335 
11336 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11337 			continue;
11338 
11339 		arg->n_vifs++;
11340 	}
11341 }
11342 
11343 static void
ath12k_mac_change_chanctx_fill_iter(void * data,u8 * mac,struct ieee80211_vif * vif)11344 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11345 				    struct ieee80211_vif *vif)
11346 {
11347 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11348 	struct ath12k_mac_change_chanctx_arg *arg = data;
11349 	struct ieee80211_bss_conf *link_conf;
11350 	struct ieee80211_chanctx_conf *ctx;
11351 	struct ath12k_link_vif *arvif;
11352 	unsigned long links_map;
11353 	u8 link_id;
11354 
11355 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11356 
11357 	links_map = ahvif->links_map;
11358 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11359 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11360 		if (WARN_ON(!arvif))
11361 			continue;
11362 
11363 		if (!arvif->is_created || arvif->ar != arg->ar)
11364 			continue;
11365 
11366 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11367 					      vif->link_conf[arvif->link_id]);
11368 		if (WARN_ON(!link_conf))
11369 			continue;
11370 
11371 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11372 		if (ctx != arg->ctx)
11373 			continue;
11374 
11375 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11376 			return;
11377 
11378 		arg->vifs[arg->next_vif].vif = vif;
11379 		arg->vifs[arg->next_vif].old_ctx = ctx;
11380 		arg->vifs[arg->next_vif].new_ctx = ctx;
11381 		arg->vifs[arg->next_vif].link_conf = link_conf;
11382 		arg->next_vif++;
11383 	}
11384 }
11385 
ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)11386 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11387 {
11388 	switch (width) {
11389 	case NL80211_CHAN_WIDTH_20:
11390 		return WMI_CHAN_WIDTH_20;
11391 	case NL80211_CHAN_WIDTH_40:
11392 		return WMI_CHAN_WIDTH_40;
11393 	case NL80211_CHAN_WIDTH_80:
11394 		return WMI_CHAN_WIDTH_80;
11395 	case NL80211_CHAN_WIDTH_160:
11396 		return WMI_CHAN_WIDTH_160;
11397 	case NL80211_CHAN_WIDTH_80P80:
11398 		return WMI_CHAN_WIDTH_80P80;
11399 	case NL80211_CHAN_WIDTH_5:
11400 		return WMI_CHAN_WIDTH_5;
11401 	case NL80211_CHAN_WIDTH_10:
11402 		return WMI_CHAN_WIDTH_10;
11403 	case NL80211_CHAN_WIDTH_320:
11404 		return WMI_CHAN_WIDTH_320;
11405 	default:
11406 		WARN_ON(1);
11407 		return WMI_CHAN_WIDTH_20;
11408 	}
11409 }
11410 
ath12k_mac_update_peer_puncturing_width(struct ath12k * ar,struct ath12k_link_vif * arvif,struct cfg80211_chan_def def)11411 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11412 						   struct ath12k_link_vif *arvif,
11413 						   struct cfg80211_chan_def def)
11414 {
11415 	u32 param_id, param_value;
11416 	int ret;
11417 
11418 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11419 		return 0;
11420 
11421 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11422 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11423 		u32_encode_bits((~def.punctured),
11424 				WMI_PEER_PUNCTURE_BITMAP);
11425 
11426 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11427 		   "punctured bitmap %02x width %d vdev %d\n",
11428 		   def.punctured, def.width, arvif->vdev_id);
11429 
11430 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11431 					arvif->vdev_id, param_id,
11432 					param_value);
11433 
11434 	return ret;
11435 }
11436 
11437 static void
ath12k_mac_update_vif_chan(struct ath12k * ar,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs)11438 ath12k_mac_update_vif_chan(struct ath12k *ar,
11439 			   struct ieee80211_vif_chanctx_switch *vifs,
11440 			   int n_vifs)
11441 {
11442 	struct ath12k_wmi_vdev_up_params params = {};
11443 	struct ieee80211_bss_conf *link_conf;
11444 	struct ath12k_base *ab = ar->ab;
11445 	struct ath12k_link_vif *arvif;
11446 	struct ieee80211_vif *vif;
11447 	struct ath12k_vif *ahvif;
11448 	u8 link_id;
11449 	int ret;
11450 	int i;
11451 	bool monitor_vif = false;
11452 
11453 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11454 
11455 	for (i = 0; i < n_vifs; i++) {
11456 		vif = vifs[i].vif;
11457 		ahvif = ath12k_vif_to_ahvif(vif);
11458 		link_conf = vifs[i].link_conf;
11459 		link_id = link_conf->link_id;
11460 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11461 					  ahvif->link[link_id]);
11462 
11463 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11464 			monitor_vif = true;
11465 			continue;
11466 		}
11467 
11468 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11469 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11470 			   arvif->vdev_id,
11471 			   vifs[i].old_ctx->def.chan->center_freq,
11472 			   vifs[i].new_ctx->def.chan->center_freq,
11473 			   vifs[i].old_ctx->def.width,
11474 			   vifs[i].new_ctx->def.width);
11475 
11476 		if (WARN_ON(!arvif->is_started))
11477 			continue;
11478 
11479 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11480 
11481 		/* Firmware expect vdev_restart only if vdev is up.
11482 		 * If vdev is down then it expect vdev_stop->vdev_start.
11483 		 */
11484 		if (arvif->is_up) {
11485 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11486 			if (ret) {
11487 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11488 					    arvif->vdev_id, ret);
11489 				continue;
11490 			}
11491 		} else {
11492 			ret = ath12k_mac_vdev_stop(arvif);
11493 			if (ret) {
11494 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11495 					    arvif->vdev_id, ret);
11496 				continue;
11497 			}
11498 
11499 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11500 			if (ret)
11501 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11502 					    arvif->vdev_id, ret);
11503 			continue;
11504 		}
11505 
11506 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11507 							      vifs[i].new_ctx->def);
11508 		if (ret) {
11509 			ath12k_warn(ar->ab,
11510 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11511 				    vifs[i].new_ctx->def.punctured,
11512 				    vifs[i].new_ctx->def.width, ret);
11513 			continue;
11514 		}
11515 
11516 		/* Defer VDEV bring-up during CSA to avoid installing stale
11517 		 * beacon templates. The beacon content is updated only
11518 		 * after CSA finalize, so we mark CSA in progress and skip
11519 		 * VDEV_UP for now. It will be handled later in
11520 		 * bss_info_changed().
11521 		 */
11522 		if (link_conf->csa_active &&
11523 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11524 			arvif->is_csa_in_progress = true;
11525 			continue;
11526 		}
11527 
11528 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11529 		if (ret)
11530 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11531 				    ret);
11532 
11533 		memset(&params, 0, sizeof(params));
11534 		params.vdev_id = arvif->vdev_id;
11535 		params.aid = ahvif->aid;
11536 		params.bssid = arvif->bssid;
11537 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11538 		if (params.tx_bssid) {
11539 			params.nontx_profile_idx = link_conf->bssid_index;
11540 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11541 		}
11542 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11543 		if (ret) {
11544 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11545 				    arvif->vdev_id, ret);
11546 			continue;
11547 		}
11548 	}
11549 
11550 	/* Restart the internal monitor vdev on new channel */
11551 	if (!monitor_vif && ar->monitor_vdev_created) {
11552 		if (!ath12k_mac_monitor_stop(ar))
11553 			ath12k_mac_monitor_start(ar);
11554 	}
11555 }
11556 
11557 static void
ath12k_mac_update_active_vif_chan(struct ath12k * ar,struct ieee80211_chanctx_conf * ctx)11558 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11559 				  struct ieee80211_chanctx_conf *ctx)
11560 {
11561 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11562 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11563 
11564 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11565 
11566 	ieee80211_iterate_active_interfaces_atomic(hw,
11567 						   IEEE80211_IFACE_ITER_NORMAL,
11568 						   ath12k_mac_change_chanctx_cnt_iter,
11569 						   &arg);
11570 	if (arg.n_vifs == 0)
11571 		return;
11572 
11573 	arg.vifs = kzalloc_objs(arg.vifs[0], arg.n_vifs);
11574 	if (!arg.vifs)
11575 		return;
11576 
11577 	ieee80211_iterate_active_interfaces_atomic(hw,
11578 						   IEEE80211_IFACE_ITER_NORMAL,
11579 						   ath12k_mac_change_chanctx_fill_iter,
11580 						   &arg);
11581 
11582 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11583 
11584 	kfree(arg.vifs);
11585 }
11586 
ath12k_mac_op_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)11587 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11588 				  struct ieee80211_chanctx_conf *ctx,
11589 				  u32 changed)
11590 {
11591 	struct ath12k *ar;
11592 	struct ath12k_base *ab;
11593 
11594 	lockdep_assert_wiphy(hw->wiphy);
11595 
11596 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11597 	if (!ar)
11598 		return;
11599 
11600 	ab = ar->ab;
11601 
11602 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11603 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11604 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11605 
11606 	/* This shouldn't really happen because channel switching should use
11607 	 * switch_vif_chanctx().
11608 	 */
11609 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11610 		return;
11611 
11612 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11613 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11614 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11615 		ath12k_mac_update_active_vif_chan(ar, ctx);
11616 
11617 	/* TODO: Recalc radar detection */
11618 }
11619 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11620 
ath12k_start_vdev_delay(struct ath12k * ar,struct ath12k_link_vif * arvif)11621 static int ath12k_start_vdev_delay(struct ath12k *ar,
11622 				   struct ath12k_link_vif *arvif)
11623 {
11624 	struct ath12k_base *ab = ar->ab;
11625 	struct ath12k_vif *ahvif = arvif->ahvif;
11626 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11627 	struct ieee80211_chanctx_conf *chanctx;
11628 	struct ieee80211_bss_conf *link_conf;
11629 	int ret;
11630 
11631 	if (WARN_ON(arvif->is_started))
11632 		return -EBUSY;
11633 
11634 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11635 	if (!link_conf) {
11636 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11637 		return -EINVAL;
11638 	}
11639 
11640 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11641 				    link_conf->chanctx_conf);
11642 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11643 	if (ret) {
11644 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11645 			    arvif->vdev_id, vif->addr,
11646 			    chanctx->def.chan->center_freq, ret);
11647 		return ret;
11648 	}
11649 
11650 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11651 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11652 		if (ret) {
11653 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11654 			return ret;
11655 		}
11656 	}
11657 
11658 	arvif->is_started = true;
11659 
11660 	/* TODO: Setup ps and cts/rts protection */
11661 	return 0;
11662 }
11663 
ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def * chan_def)11664 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11665 {
11666 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11667 		switch (chan_def->width) {
11668 		case NL80211_CHAN_WIDTH_20:
11669 			return 1;
11670 		case NL80211_CHAN_WIDTH_40:
11671 			return 2;
11672 		case NL80211_CHAN_WIDTH_80:
11673 			return 4;
11674 		case NL80211_CHAN_WIDTH_160:
11675 			return 8;
11676 		case NL80211_CHAN_WIDTH_320:
11677 			return 16;
11678 		default:
11679 			return 1;
11680 		}
11681 	} else {
11682 		switch (chan_def->width) {
11683 		case NL80211_CHAN_WIDTH_20:
11684 			return 1;
11685 		case NL80211_CHAN_WIDTH_40:
11686 			return 2;
11687 		case NL80211_CHAN_WIDTH_80:
11688 			return 3;
11689 		case NL80211_CHAN_WIDTH_160:
11690 			return 4;
11691 		case NL80211_CHAN_WIDTH_320:
11692 			return 5;
11693 		default:
11694 			return 1;
11695 		}
11696 	}
11697 }
11698 
ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def * chan_def)11699 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11700 {
11701 	u16 diff_seq;
11702 
11703 	/* It is to get the lowest channel number's center frequency of the chan.
11704 	 * For example,
11705 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11706 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11707 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11708 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11709 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11710 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11711 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11712 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11713 	 */
11714 	switch (chan_def->width) {
11715 	case NL80211_CHAN_WIDTH_320:
11716 		diff_seq = 150;
11717 		break;
11718 	case NL80211_CHAN_WIDTH_160:
11719 		diff_seq = 70;
11720 		break;
11721 	case NL80211_CHAN_WIDTH_80:
11722 		diff_seq = 30;
11723 		break;
11724 	case NL80211_CHAN_WIDTH_40:
11725 		diff_seq = 10;
11726 		break;
11727 	default:
11728 		diff_seq = 0;
11729 	}
11730 
11731 	return chan_def->center_freq1 - diff_seq;
11732 }
11733 
ath12k_mac_get_seg_freq(struct cfg80211_chan_def * chan_def,u16 start_seq,u8 seq)11734 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11735 				   u16 start_seq, u8 seq)
11736 {
11737 	u16 seg_seq;
11738 
11739 	/* It is to get the center frequency of the specific bandwidth.
11740 	 * start_seq means the lowest channel number's center frequency.
11741 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11742 	 * For example,
11743 	 * lowest channel is 1, its center frequency 5955,
11744 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11745 	 * lowest channel is 1, its center frequency 5955,
11746 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11747 	 * lowest channel is 1, its center frequency 5955,
11748 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11749 	 * lowest channel is 1, its center frequency 5955,
11750 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11751 	 */
11752 	seg_seq = 10 * (BIT(seq) - 1);
11753 	return seg_seq + start_seq;
11754 }
11755 
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)11756 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11757 				       u16 step_freq,
11758 				       u16 *start_freq,
11759 				       u16 *center_freq,
11760 				       u8 i,
11761 				       struct ieee80211_channel **temp_chan,
11762 				       s8 *tx_power)
11763 {
11764 	/* It is to get the center frequency for each 20 MHz.
11765 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11766 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11767 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11768 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11769 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11770 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11771 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11772 	 * after get the center frequency of each channel, it is easy to find the
11773 	 * struct ieee80211_channel of it and get the max_reg_power.
11774 	 */
11775 	*center_freq = *start_freq + i * step_freq;
11776 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11777 	*tx_power = (*temp_chan)->max_reg_power;
11778 }
11779 
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)11780 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11781 				      u16 *start_freq,
11782 				      u16 *center_freq,
11783 				      u8 i,
11784 				      struct ieee80211_channel **temp_chan,
11785 				      struct cfg80211_chan_def *def,
11786 				      s8 *tx_power)
11787 {
11788 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11789 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11790 	 * it is the center frequency of a channel number.
11791 	 * For example, when configured channel number is 1.
11792 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11793 	 * then it is channel number 5.
11794 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11795 	 * then it is channel number 9.
11796 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11797 	 * then it is channel number 17.
11798 	 * after get the center frequency of each channel, it is easy to find the
11799 	 * struct ieee80211_channel of it and get the max_reg_power.
11800 	 */
11801 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11802 
11803 	/* For the 20 MHz, its center frequency is same with same channel */
11804 	if (i != 0)
11805 		*center_freq += 10;
11806 
11807 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11808 	*tx_power = (*temp_chan)->max_reg_power;
11809 }
11810 
ath12k_mac_fill_reg_tpc_info(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)11811 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11812 				  struct ath12k_link_vif *arvif,
11813 				  struct ieee80211_chanctx_conf *ctx)
11814 {
11815 	struct ath12k_base *ab = ar->ab;
11816 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11817 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11818 	struct ieee80211_channel *chan, *temp_chan;
11819 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11820 	bool is_psd_power = false, is_tpe_present = false;
11821 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
11822 	s8 eirp_power = 0;
11823 	struct ath12k_vif *ahvif = arvif->ahvif;
11824 	u16 start_freq, center_freq;
11825 	u8 reg_6ghz_power_mode;
11826 
11827 	chan = ctx->def.chan;
11828 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
11829 	pwr_reduction = bss_conf->pwr_reduction;
11830 
11831 	if (arvif->reg_tpc_info.num_pwr_levels) {
11832 		is_tpe_present = true;
11833 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
11834 	} else {
11835 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
11836 	}
11837 
11838 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
11839 		/* STA received TPE IE*/
11840 		if (is_tpe_present) {
11841 			/* local power is PSD power*/
11842 			if (chan->flags & IEEE80211_CHAN_PSD) {
11843 				/* Connecting AP is psd power */
11844 				if (reg_tpc_info->is_psd_power) {
11845 					is_psd_power = true;
11846 					ath12k_mac_get_psd_channel(ar, 20,
11847 								   &start_freq,
11848 								   &center_freq,
11849 								   pwr_lvl_idx,
11850 								   &temp_chan,
11851 								   &tx_power);
11852 					psd_power = temp_chan->psd;
11853 					eirp_power = tx_power;
11854 					max_tx_power[pwr_lvl_idx] =
11855 						min_t(s8,
11856 						      psd_power,
11857 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11858 				/* Connecting AP is not psd power */
11859 				} else {
11860 					ath12k_mac_get_eirp_power(ar,
11861 								  &start_freq,
11862 								  &center_freq,
11863 								  pwr_lvl_idx,
11864 								  &temp_chan,
11865 								  &ctx->def,
11866 								  &tx_power);
11867 					psd_power = temp_chan->psd;
11868 					/* convert psd power to EIRP power based
11869 					 * on channel width
11870 					 */
11871 					tx_power =
11872 						min_t(s8, tx_power,
11873 						      psd_power + 13 + pwr_lvl_idx * 3);
11874 					max_tx_power[pwr_lvl_idx] =
11875 						min_t(s8,
11876 						      tx_power,
11877 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11878 				}
11879 			/* local power is not PSD power */
11880 			} else {
11881 				/* Connecting AP is psd power */
11882 				if (reg_tpc_info->is_psd_power) {
11883 					is_psd_power = true;
11884 					ath12k_mac_get_psd_channel(ar, 20,
11885 								   &start_freq,
11886 								   &center_freq,
11887 								   pwr_lvl_idx,
11888 								   &temp_chan,
11889 								   &tx_power);
11890 					eirp_power = tx_power;
11891 					max_tx_power[pwr_lvl_idx] =
11892 						reg_tpc_info->tpe[pwr_lvl_idx];
11893 				/* Connecting AP is not psd power */
11894 				} else {
11895 					ath12k_mac_get_eirp_power(ar,
11896 								  &start_freq,
11897 								  &center_freq,
11898 								  pwr_lvl_idx,
11899 								  &temp_chan,
11900 								  &ctx->def,
11901 								  &tx_power);
11902 					max_tx_power[pwr_lvl_idx] =
11903 						min_t(s8,
11904 						      tx_power,
11905 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11906 				}
11907 			}
11908 		/* STA not received TPE IE */
11909 		} else {
11910 			/* local power is PSD power*/
11911 			if (chan->flags & IEEE80211_CHAN_PSD) {
11912 				is_psd_power = true;
11913 				ath12k_mac_get_psd_channel(ar, 20,
11914 							   &start_freq,
11915 							   &center_freq,
11916 							   pwr_lvl_idx,
11917 							   &temp_chan,
11918 							   &tx_power);
11919 				psd_power = temp_chan->psd;
11920 				eirp_power = tx_power;
11921 				max_tx_power[pwr_lvl_idx] = psd_power;
11922 			} else {
11923 				ath12k_mac_get_eirp_power(ar,
11924 							  &start_freq,
11925 							  &center_freq,
11926 							  pwr_lvl_idx,
11927 							  &temp_chan,
11928 							  &ctx->def,
11929 							  &tx_power);
11930 				max_tx_power[pwr_lvl_idx] = tx_power;
11931 			}
11932 		}
11933 
11934 		if (is_psd_power) {
11935 			/* If AP local power constraint is present */
11936 			if (pwr_reduction)
11937 				eirp_power = eirp_power - pwr_reduction;
11938 
11939 			/* If firmware updated max tx power is non zero, then take
11940 			 * the min of firmware updated ap tx power
11941 			 * and max power derived from above mentioned parameters.
11942 			 */
11943 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11944 				   "eirp power : %d firmware report power : %d\n",
11945 				   eirp_power, ar->max_allowed_tx_power);
11946 			/* Firmware reports lower max_allowed_tx_power during vdev
11947 			 * start response. In case of 6 GHz, firmware is not aware
11948 			 * of EIRP power unless driver sets EIRP power through WMI
11949 			 * TPC command. So radio which does not support idle power
11950 			 * save can set maximum calculated EIRP power directly to
11951 			 * firmware through TPC command without min comparison with
11952 			 * vdev start response's max_allowed_tx_power.
11953 			 */
11954 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11955 				eirp_power = min_t(s8,
11956 						   eirp_power,
11957 						   ar->max_allowed_tx_power);
11958 		} else {
11959 			/* If AP local power constraint is present */
11960 			if (pwr_reduction)
11961 				max_tx_power[pwr_lvl_idx] =
11962 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
11963 			/* If firmware updated max tx power is non zero, then take
11964 			 * the min of firmware updated ap tx power
11965 			 * and max power derived from above mentioned parameters.
11966 			 */
11967 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11968 				max_tx_power[pwr_lvl_idx] =
11969 					min_t(s8,
11970 					      max_tx_power[pwr_lvl_idx],
11971 					      ar->max_allowed_tx_power);
11972 		}
11973 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
11974 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
11975 			max_tx_power[pwr_lvl_idx];
11976 	}
11977 
11978 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
11979 	reg_tpc_info->is_psd_power = is_psd_power;
11980 	reg_tpc_info->eirp_power = eirp_power;
11981 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11982 		reg_6ghz_power_mode = bss_conf->power_type;
11983 	else
11984 		/* For now, LPI is the only supported AP power mode */
11985 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
11986 
11987 	reg_tpc_info->ap_power_type =
11988 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
11989 }
11990 
ath12k_mac_parse_tx_pwr_env(struct ath12k * ar,struct ath12k_link_vif * arvif)11991 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
11992 					struct ath12k_link_vif *arvif)
11993 {
11994 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11995 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
11996 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
11997 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
11998 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
11999 	enum wmi_reg_6g_client_type client_type;
12000 	struct ath12k_reg_info *reg_info;
12001 	struct ath12k_base *ab = ar->ab;
12002 	bool psd_valid, non_psd_valid;
12003 	int i;
12004 
12005 	reg_info = ab->reg_info[ar->pdev_idx];
12006 	client_type = reg_info->client_type;
12007 
12008 	local_psd = &tpe->psd_local[client_type];
12009 	reg_psd = &tpe->psd_reg_client[client_type];
12010 	local_non_psd = &tpe->max_local[client_type];
12011 	reg_non_psd = &tpe->max_reg_client[client_type];
12012 
12013 	psd_valid = local_psd->valid | reg_psd->valid;
12014 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
12015 
12016 	if (!psd_valid && !non_psd_valid) {
12017 		ath12k_warn(ab,
12018 			    "no transmit power envelope match client power type %d\n",
12019 			    client_type);
12020 		return;
12021 	}
12022 
12023 	if (psd_valid) {
12024 		tpc_info->is_psd_power = true;
12025 
12026 		tpc_info->num_pwr_levels = max(local_psd->count,
12027 					       reg_psd->count);
12028 		tpc_info->num_pwr_levels =
12029 				min3(tpc_info->num_pwr_levels,
12030 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12031 				     ATH12K_NUM_PWR_LEVELS);
12032 
12033 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12034 			tpc_info->tpe[i] = min(local_psd->power[i],
12035 					       reg_psd->power[i]) / 2;
12036 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12037 				   "TPE PSD power[%d] : %d\n",
12038 				   i, tpc_info->tpe[i]);
12039 		}
12040 	} else {
12041 		tpc_info->is_psd_power = false;
12042 		tpc_info->eirp_power = 0;
12043 
12044 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12045 					       reg_non_psd->count);
12046 		tpc_info->num_pwr_levels =
12047 				min3(tpc_info->num_pwr_levels,
12048 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12049 				     ATH12K_NUM_PWR_LEVELS);
12050 
12051 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12052 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12053 					       reg_non_psd->power[i]) / 2;
12054 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12055 				   "non PSD power[%d] : %d\n",
12056 				   i, tpc_info->tpe[i]);
12057 		}
12058 	}
12059 }
12060 
12061 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)12062 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12063 				 struct ieee80211_vif *vif,
12064 				 struct ieee80211_bss_conf *link_conf,
12065 				 struct ieee80211_chanctx_conf *ctx)
12066 {
12067 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12068 	struct ath12k *ar;
12069 	struct ath12k_base *ab;
12070 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12071 	u8 link_id = link_conf->link_id;
12072 	struct ath12k_link_vif *arvif;
12073 	int ret;
12074 
12075 	lockdep_assert_wiphy(hw->wiphy);
12076 
12077 	/* For multi radio wiphy, the vdev was not created during add_interface
12078 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12079 	 */
12080 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12081 	if (!arvif) {
12082 		WARN_ON(1);
12083 		return -ENOMEM;
12084 	}
12085 
12086 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12087 	if (!ar) {
12088 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12089 			       vif->addr, link_id);
12090 		return -EINVAL;
12091 	}
12092 
12093 	ab = ar->ab;
12094 
12095 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12096 		   "mac chanctx assign ptr %p vdev_id %i\n",
12097 		   ctx, arvif->vdev_id);
12098 
12099 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12100 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12101 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12102 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12103 
12104 	arvif->punct_bitmap = ctx->def.punctured;
12105 
12106 	/* for some targets bss peer must be created before vdev_start */
12107 	if (ab->hw_params->vdev_start_delay &&
12108 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12109 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12110 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12111 		ret = 0;
12112 		goto out;
12113 	}
12114 
12115 	if (WARN_ON(arvif->is_started)) {
12116 		ret = -EBUSY;
12117 		goto out;
12118 	}
12119 
12120 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12121 		ret = ath12k_mac_monitor_start(ar);
12122 		if (ret) {
12123 			ath12k_mac_monitor_vdev_delete(ar);
12124 			goto out;
12125 		}
12126 
12127 		arvif->is_started = true;
12128 		goto out;
12129 	}
12130 
12131 	ret = ath12k_mac_vdev_start(arvif, ctx);
12132 	if (ret) {
12133 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12134 			    arvif->vdev_id, vif->addr,
12135 			    ctx->def.chan->center_freq, ret);
12136 		goto out;
12137 	}
12138 
12139 	arvif->is_started = true;
12140 
12141 	/* TODO: Setup ps and cts/rts protection */
12142 
12143 out:
12144 	return ret;
12145 }
12146 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12147 
12148 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)12149 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12150 				   struct ieee80211_vif *vif,
12151 				   struct ieee80211_bss_conf *link_conf,
12152 				   struct ieee80211_chanctx_conf *ctx)
12153 {
12154 	struct ath12k *ar;
12155 	struct ath12k_base *ab;
12156 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12157 	struct ath12k_link_vif *arvif;
12158 	u8 link_id = link_conf->link_id;
12159 	int ret;
12160 
12161 	lockdep_assert_wiphy(hw->wiphy);
12162 
12163 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12164 
12165 	/* The vif is expected to be attached to an ar's VDEV.
12166 	 * We leave the vif/vdev in this function as is
12167 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12168 	 * the VDEV will be deleted and unassigned either during
12169 	 * remove_interface() or when there is a change in channel
12170 	 * that moves the vif to a new ar
12171 	 */
12172 	if (!arvif || !arvif->is_created)
12173 		return;
12174 
12175 	ar = arvif->ar;
12176 	ab = ar->ab;
12177 
12178 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12179 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12180 		   ctx, arvif->vdev_id);
12181 
12182 	WARN_ON(!arvif->is_started);
12183 
12184 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12185 		ret = ath12k_mac_monitor_stop(ar);
12186 		if (ret)
12187 			return;
12188 
12189 		arvif->is_started = false;
12190 	}
12191 
12192 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12193 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12194 		ath12k_bss_disassoc(ar, arvif);
12195 		ret = ath12k_mac_vdev_stop(arvif);
12196 		if (ret)
12197 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12198 				    arvif->vdev_id, ret);
12199 	}
12200 	arvif->is_started = false;
12201 
12202 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12203 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12204 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12205 	    ar->state_11d != ATH12K_11D_PREPARING) {
12206 		reinit_completion(&ar->completed_11d_scan);
12207 		ar->state_11d = ATH12K_11D_PREPARING;
12208 	}
12209 
12210 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12211 		ath12k_scan_abort(ar);
12212 		ar->scan.arvif = NULL;
12213 	}
12214 }
12215 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12216 
12217 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)12218 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12219 				 struct ieee80211_vif_chanctx_switch *vifs,
12220 				 int n_vifs,
12221 				 enum ieee80211_chanctx_switch_mode mode)
12222 {
12223 	struct ath12k *ar;
12224 
12225 	lockdep_assert_wiphy(hw->wiphy);
12226 
12227 	ar = ath12k_get_ar_by_ctx(hw, vifs->old_ctx);
12228 	if (!ar)
12229 		return -EINVAL;
12230 
12231 	/* Switching channels across radio is not allowed */
12232 	if (ar != ath12k_get_ar_by_ctx(hw, vifs->new_ctx))
12233 		return -EINVAL;
12234 
12235 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12236 		   "mac chanctx switch n_vifs %d mode %d\n",
12237 		   n_vifs, mode);
12238 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
12239 
12240 	return 0;
12241 }
12242 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12243 
12244 static int
ath12k_set_vdev_param_to_all_vifs(struct ath12k * ar,int param,u32 value)12245 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12246 {
12247 	struct ath12k_link_vif *arvif;
12248 	int ret = 0;
12249 
12250 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12251 
12252 	list_for_each_entry(arvif, &ar->arvifs, list) {
12253 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12254 			   param, arvif->vdev_id, value);
12255 
12256 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12257 						    param, value);
12258 		if (ret) {
12259 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12260 				    param, arvif->vdev_id, ret);
12261 			break;
12262 		}
12263 	}
12264 
12265 	return ret;
12266 }
12267 
12268 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12269  * this is set interface specific to firmware from ath12k driver
12270  */
ath12k_mac_op_set_rts_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)12271 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12272 				    int radio_idx, u32 value)
12273 {
12274 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12275 	struct wiphy *wiphy = hw->wiphy;
12276 	struct ath12k *ar;
12277 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12278 	int ret = 0, ret_err, i;
12279 
12280 	lockdep_assert_wiphy(hw->wiphy);
12281 
12282 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12283 		return -EINVAL;
12284 
12285 	if (radio_idx != -1) {
12286 		/* Update RTS threshold in specified radio */
12287 		ar = ath12k_ah_to_ar(ah, radio_idx);
12288 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12289 		if (ret) {
12290 			ath12k_warn(ar->ab,
12291 				    "failed to set RTS config for all vdevs of pdev %d",
12292 				    ar->pdev->pdev_id);
12293 			return ret;
12294 		}
12295 
12296 		ar->rts_threshold = value;
12297 		return 0;
12298 	}
12299 
12300 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12301 	for_each_ar(ah, ar, i) {
12302 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12303 		if (ret) {
12304 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12305 				    ar->pdev->pdev_id);
12306 			break;
12307 		}
12308 	}
12309 	if (!ret) {
12310 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12311 		 * the RTS threshold value for all radios
12312 		 */
12313 		for_each_ar(ah, ar, i)
12314 			ar->rts_threshold = value;
12315 		return 0;
12316 	}
12317 
12318 	/* RTS threshold config failed, revert to the previous RTS threshold */
12319 	for (i = i - 1; i >= 0; i--) {
12320 		ar = ath12k_ah_to_ar(ah, i);
12321 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12322 							    ar->rts_threshold);
12323 		if (ret_err)
12324 			ath12k_warn(ar->ab,
12325 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12326 				    ar->pdev->pdev_id);
12327 	}
12328 
12329 	return ret;
12330 }
12331 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12332 
ath12k_mac_op_set_frag_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)12333 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12334 				     int radio_idx, u32 value)
12335 {
12336 	/* Even though there's a WMI vdev param for fragmentation threshold no
12337 	 * known firmware actually implements it. Moreover it is not possible to
12338 	 * rely frame fragmentation to mac80211 because firmware clears the
12339 	 * "more fragments" bit in frame control making it impossible for remote
12340 	 * devices to reassemble frames.
12341 	 *
12342 	 * Hence implement a dummy callback just to say fragmentation isn't
12343 	 * supported. This effectively prevents mac80211 from doing frame
12344 	 * fragmentation in software.
12345 	 */
12346 
12347 	lockdep_assert_wiphy(hw->wiphy);
12348 
12349 	return -EOPNOTSUPP;
12350 }
12351 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12352 
ath12k_mac_flush(struct ath12k * ar)12353 static int ath12k_mac_flush(struct ath12k *ar)
12354 {
12355 	long time_left;
12356 	int ret = 0;
12357 
12358 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12359 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12360 				       ATH12K_FLUSH_TIMEOUT);
12361 	if (time_left == 0) {
12362 		ath12k_warn(ar->ab,
12363 			    "failed to flush transmit queue, data pkts pending %d\n",
12364 			    atomic_read(&ar->dp.num_tx_pending));
12365 		ret = -ETIMEDOUT;
12366 	}
12367 
12368 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12369 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12370 				       ATH12K_FLUSH_TIMEOUT);
12371 	if (time_left == 0) {
12372 		ath12k_warn(ar->ab,
12373 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12374 			    atomic_read(&ar->num_pending_mgmt_tx));
12375 		ret = -ETIMEDOUT;
12376 	}
12377 
12378 	return ret;
12379 }
12380 
ath12k_mac_wait_tx_complete(struct ath12k * ar)12381 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12382 {
12383 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12384 
12385 	ath12k_mac_drain_tx(ar);
12386 	return ath12k_mac_flush(ar);
12387 }
12388 
ath12k_mac_op_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)12389 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12390 			 u32 queues, bool drop)
12391 {
12392 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12393 	struct ath12k_link_vif *arvif;
12394 	struct ath12k_vif *ahvif;
12395 	unsigned long links;
12396 	struct ath12k *ar;
12397 	u8 link_id;
12398 	int i;
12399 
12400 	lockdep_assert_wiphy(hw->wiphy);
12401 
12402 	if (drop)
12403 		return;
12404 
12405 	for_each_ar(ah, ar, i)
12406 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12407 
12408 	/* vif can be NULL when flush() is considered for hw */
12409 	if (!vif) {
12410 		for_each_ar(ah, ar, i)
12411 			ath12k_mac_flush(ar);
12412 		return;
12413 	}
12414 
12415 	ahvif = ath12k_vif_to_ahvif(vif);
12416 	links = ahvif->links_map;
12417 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12418 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12419 		if (!(arvif && arvif->ar))
12420 			continue;
12421 
12422 		ath12k_mac_flush(arvif->ar);
12423 	}
12424 }
12425 EXPORT_SYMBOL(ath12k_mac_op_flush);
12426 
12427 static int
ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12428 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12429 				     enum nl80211_band band,
12430 				     const struct cfg80211_bitrate_mask *mask)
12431 {
12432 	int num_rates = 0;
12433 	int i;
12434 
12435 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12436 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12437 
12438 	return num_rates;
12439 }
12440 
12441 static bool
ath12k_mac_has_single_legacy_rate(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12442 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12443 				  enum nl80211_band band,
12444 				  const struct cfg80211_bitrate_mask *mask)
12445 {
12446 	int num_rates = 0;
12447 
12448 	num_rates = hweight32(mask->control[band].legacy);
12449 
12450 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12451 		return false;
12452 
12453 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12454 		return false;
12455 
12456 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12457 		return false;
12458 
12459 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12460 		return false;
12461 
12462 	return num_rates == 1;
12463 }
12464 
12465 static __le16
ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap * he_cap)12466 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12467 {
12468 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12469 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12470 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12471 
12472 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12473 }
12474 
12475 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)12476 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12477 				       struct ieee80211_vif *vif,
12478 				       enum nl80211_band band,
12479 				       const struct cfg80211_bitrate_mask *mask,
12480 				       int *nss)
12481 {
12482 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12483 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12484 	const struct ieee80211_sband_iftype_data *data;
12485 	const struct ieee80211_sta_he_cap *he_cap;
12486 	u16 he_mcs_map = 0;
12487 	u16 eht_mcs_map = 0;
12488 	u8 ht_nss_mask = 0;
12489 	u8 vht_nss_mask = 0;
12490 	u8 he_nss_mask = 0;
12491 	u8 eht_nss_mask = 0;
12492 	u8 mcs_nss_len;
12493 	int i;
12494 
12495 	/* No need to consider legacy here. Basic rates are always present
12496 	 * in bitrate mask
12497 	 */
12498 
12499 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12500 		if (mask->control[band].ht_mcs[i] == 0)
12501 			continue;
12502 		else if (mask->control[band].ht_mcs[i] ==
12503 			 sband->ht_cap.mcs.rx_mask[i])
12504 			ht_nss_mask |= BIT(i);
12505 		else
12506 			return false;
12507 	}
12508 
12509 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12510 		if (mask->control[band].vht_mcs[i] == 0)
12511 			continue;
12512 		else if (mask->control[band].vht_mcs[i] ==
12513 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12514 			vht_nss_mask |= BIT(i);
12515 		else
12516 			return false;
12517 	}
12518 
12519 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12520 	if (!he_cap)
12521 		return false;
12522 
12523 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12524 
12525 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12526 		if (mask->control[band].he_mcs[i] == 0)
12527 			continue;
12528 
12529 		if (mask->control[band].he_mcs[i] ==
12530 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12531 			he_nss_mask |= BIT(i);
12532 		else
12533 			return false;
12534 	}
12535 
12536 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12537 
12538 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12539 						 &data->eht_cap.eht_cap_elem,
12540 						 false);
12541 	if (mcs_nss_len == 4) {
12542 		/* 20 MHz only STA case */
12543 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12544 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12545 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12546 			eht_mcs_map = 0x1fff;
12547 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12548 			eht_mcs_map = 0x07ff;
12549 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12550 			eht_mcs_map = 0x01ff;
12551 		else
12552 			eht_mcs_map = 0x007f;
12553 	} else {
12554 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12555 
12556 		switch (mcs_nss_len) {
12557 		case 9:
12558 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12559 			break;
12560 		case 6:
12561 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12562 			break;
12563 		case 3:
12564 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12565 			break;
12566 		default:
12567 			return false;
12568 		}
12569 
12570 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12571 			eht_mcs_map = 0x1fff;
12572 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12573 			eht_mcs_map = 0x7ff;
12574 		else
12575 			eht_mcs_map = 0x1ff;
12576 	}
12577 
12578 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12579 		if (mask->control[band].eht_mcs[i] == 0)
12580 			continue;
12581 
12582 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12583 			eht_nss_mask |= BIT(i);
12584 		else
12585 			return false;
12586 	}
12587 
12588 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12589 	    ht_nss_mask != eht_nss_mask)
12590 		return false;
12591 
12592 	if (ht_nss_mask == 0)
12593 		return false;
12594 
12595 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12596 		return false;
12597 
12598 	*nss = fls(ht_nss_mask);
12599 
12600 	return true;
12601 }
12602 
12603 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)12604 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12605 				  enum nl80211_band band,
12606 				  const struct cfg80211_bitrate_mask *mask,
12607 				  u32 *rate, u8 *nss)
12608 {
12609 	int rate_idx;
12610 	u16 bitrate;
12611 	u8 preamble;
12612 	u8 hw_rate;
12613 
12614 	if (hweight32(mask->control[band].legacy) != 1)
12615 		return -EINVAL;
12616 
12617 	rate_idx = ffs(mask->control[band].legacy) - 1;
12618 
12619 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12620 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12621 
12622 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12623 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12624 
12625 	if (ath12k_mac_bitrate_is_cck(bitrate))
12626 		preamble = WMI_RATE_PREAMBLE_CCK;
12627 	else
12628 		preamble = WMI_RATE_PREAMBLE_OFDM;
12629 
12630 	*nss = 1;
12631 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12632 
12633 	return 0;
12634 }
12635 
12636 static int
ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif * arvif,u8 gi,u8 ltf,u32 param)12637 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12638 				 u32 param)
12639 {
12640 	struct ath12k *ar = arvif->ar;
12641 	int ret;
12642 
12643 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12644 
12645 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12646 	if (gi && gi != 0xFF)
12647 		gi += 1;
12648 
12649 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12650 					    WMI_VDEV_PARAM_SGI, gi);
12651 	if (ret) {
12652 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12653 			    gi, ret);
12654 		return ret;
12655 	}
12656 
12657 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12658 		/* HE values start from 1 */
12659 		if (ltf != 0xFF)
12660 			ltf += 1;
12661 	} else {
12662 		/* EHT values start from 5 */
12663 		if (ltf != 0xFF)
12664 			ltf += 4;
12665 	}
12666 
12667 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12668 					    param, ltf);
12669 	if (ret) {
12670 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12671 			    ltf, ret);
12672 		return ret;
12673 	}
12674 	return 0;
12675 }
12676 
12677 static int
ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif * arvif,u16 gi,u8 ltf)12678 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12679 {
12680 	struct ath12k *ar = arvif->ar;
12681 	int ret;
12682 	u32 ar_gi_ltf;
12683 
12684 	if (gi != 0xFF) {
12685 		switch (gi) {
12686 		case ATH12K_RATE_INFO_GI_0_8:
12687 			gi = WMI_AUTORATE_800NS_GI;
12688 			break;
12689 		case ATH12K_RATE_INFO_GI_1_6:
12690 			gi = WMI_AUTORATE_1600NS_GI;
12691 			break;
12692 		case ATH12K_RATE_INFO_GI_3_2:
12693 			gi = WMI_AUTORATE_3200NS_GI;
12694 			break;
12695 		default:
12696 			ath12k_warn(ar->ab, "Invalid GI\n");
12697 			return -EINVAL;
12698 		}
12699 	}
12700 
12701 	if (ltf != 0xFF) {
12702 		switch (ltf) {
12703 		case ATH12K_RATE_INFO_1XLTF:
12704 			ltf = WMI_AUTORATE_LTF_1X;
12705 			break;
12706 		case ATH12K_RATE_INFO_2XLTF:
12707 			ltf = WMI_AUTORATE_LTF_2X;
12708 			break;
12709 		case ATH12K_RATE_INFO_4XLTF:
12710 			ltf = WMI_AUTORATE_LTF_4X;
12711 			break;
12712 		default:
12713 			ath12k_warn(ar->ab, "Invalid LTF\n");
12714 			return -EINVAL;
12715 		}
12716 	}
12717 
12718 	ar_gi_ltf = gi | ltf;
12719 
12720 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12721 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12722 					    ar_gi_ltf);
12723 	if (ret) {
12724 		ath12k_warn(ar->ab,
12725 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12726 			    gi, ltf, ret);
12727 		return ret;
12728 	}
12729 
12730 	return 0;
12731 }
12732 
ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)12733 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12734 {
12735 	switch (gi) {
12736 	case NL80211_TXRATE_DEFAULT_GI:
12737 		return WMI_GI_400_NS;
12738 	case NL80211_TXRATE_FORCE_LGI:
12739 		return WMI_GI_800_NS;
12740 	default:
12741 		return WMI_GI_400_NS;
12742 	}
12743 }
12744 
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)12745 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12746 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12747 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12748 				      u8 eht_gi, u8 eht_ltf,
12749 				      bool eht_fixed_rate)
12750 {
12751 	struct ieee80211_bss_conf *link_conf;
12752 	struct ath12k *ar = arvif->ar;
12753 	bool he_support, eht_support, gi_ltf_set = false;
12754 	u32 vdev_param;
12755 	u32 param_value;
12756 	int ret;
12757 
12758 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12759 
12760 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12761 	if (!link_conf)
12762 		return -EINVAL;
12763 
12764 	he_support = link_conf->he_support;
12765 	eht_support = link_conf->eht_support;
12766 
12767 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12768 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
12769 		   arvif->vdev_id, rate, nss, sgi, ldpc);
12770 
12771 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12772 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
12773 		   he_ltf, he_fixed_rate);
12774 
12775 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12776 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
12777 		   eht_gi, eht_ltf, eht_fixed_rate);
12778 
12779 	if (!he_support && !eht_support) {
12780 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
12781 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12782 						    vdev_param, rate);
12783 		if (ret) {
12784 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
12785 				    rate, ret);
12786 			return ret;
12787 		}
12788 	}
12789 
12790 	vdev_param = WMI_VDEV_PARAM_NSS;
12791 
12792 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12793 					    vdev_param, nss);
12794 	if (ret) {
12795 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
12796 			    nss, ret);
12797 		return ret;
12798 	}
12799 
12800 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12801 					    WMI_VDEV_PARAM_LDPC, ldpc);
12802 	if (ret) {
12803 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
12804 			    ldpc, ret);
12805 		return ret;
12806 	}
12807 
12808 	if (eht_support) {
12809 		if (eht_fixed_rate)
12810 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
12811 							       WMI_VDEV_PARAM_EHT_LTF);
12812 		else
12813 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
12814 
12815 		if (ret) {
12816 			ath12k_warn(ar->ab,
12817 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
12818 				    eht_gi, eht_ltf, ret);
12819 			return ret;
12820 		}
12821 		gi_ltf_set = true;
12822 	}
12823 
12824 	if (he_support) {
12825 		if (he_fixed_rate)
12826 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
12827 							       WMI_VDEV_PARAM_HE_LTF);
12828 		else
12829 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
12830 		if (ret)
12831 			return ret;
12832 		gi_ltf_set = true;
12833 	}
12834 
12835 	if (!gi_ltf_set) {
12836 		vdev_param = WMI_VDEV_PARAM_SGI;
12837 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
12838 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12839 						    vdev_param, param_value);
12840 		if (ret) {
12841 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
12842 				    sgi, ret);
12843 			return ret;
12844 		}
12845 	}
12846 
12847 	return 0;
12848 }
12849 
12850 static bool
ath12k_mac_vht_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12851 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
12852 				 enum nl80211_band band,
12853 				 const struct cfg80211_bitrate_mask *mask)
12854 {
12855 	int i;
12856 	u16 vht_mcs;
12857 
12858 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
12859 		vht_mcs = mask->control[band].vht_mcs[i];
12860 
12861 		switch (vht_mcs) {
12862 		case 0:
12863 		case BIT(8) - 1:
12864 		case BIT(9) - 1:
12865 		case BIT(10) - 1:
12866 			break;
12867 		default:
12868 			return false;
12869 		}
12870 	}
12871 
12872 	return true;
12873 }
12874 
12875 static bool
ath12k_mac_he_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12876 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
12877 				enum nl80211_band band,
12878 				const struct cfg80211_bitrate_mask *mask)
12879 {
12880 	int i;
12881 	u16 he_mcs;
12882 
12883 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
12884 		he_mcs = mask->control[band].he_mcs[i];
12885 
12886 		switch (he_mcs) {
12887 		case 0:
12888 		case BIT(8) - 1:
12889 		case BIT(10) - 1:
12890 		case BIT(12) - 1:
12891 			break;
12892 		default:
12893 			return false;
12894 		}
12895 	}
12896 
12897 	return true;
12898 }
12899 
12900 static bool
ath12k_mac_eht_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12901 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
12902 				 enum nl80211_band band,
12903 				 const struct cfg80211_bitrate_mask *mask)
12904 {
12905 	u16 eht_mcs;
12906 	int i;
12907 
12908 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
12909 		eht_mcs = mask->control[band].eht_mcs[i];
12910 
12911 		switch (eht_mcs) {
12912 		case 0:
12913 		case BIT(8) - 1:
12914 		case BIT(10) - 1:
12915 		case BIT(12) - 1:
12916 		case BIT(14) - 1:
12917 			break;
12918 		case BIT(15) - 1:
12919 		case BIT(16) - 1:
12920 		case BIT(16) - BIT(14) - 1:
12921 			if (i != 0)
12922 				return false;
12923 			break;
12924 		default:
12925 			return false;
12926 		}
12927 	}
12928 
12929 	return true;
12930 }
12931 
ath12k_mac_set_bitrate_mask_iter(void * data,struct ieee80211_sta * sta)12932 static void ath12k_mac_set_bitrate_mask_iter(void *data,
12933 					     struct ieee80211_sta *sta)
12934 {
12935 	struct ath12k_link_vif *arvif = data;
12936 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12937 	struct ath12k_link_sta *arsta;
12938 	struct ath12k *ar = arvif->ar;
12939 
12940 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12941 
12942 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12943 				  ahsta->link[arvif->link_id]);
12944 	if (!arsta || arsta->arvif != arvif)
12945 		return;
12946 
12947 	spin_lock_bh(&ar->data_lock);
12948 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
12949 	spin_unlock_bh(&ar->data_lock);
12950 
12951 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
12952 }
12953 
ath12k_mac_disable_peer_fixed_rate(void * data,struct ieee80211_sta * sta)12954 static void ath12k_mac_disable_peer_fixed_rate(void *data,
12955 					       struct ieee80211_sta *sta)
12956 {
12957 	struct ath12k_link_vif *arvif = data;
12958 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12959 	struct ath12k_link_sta *arsta;
12960 	struct ath12k *ar = arvif->ar;
12961 	int ret;
12962 
12963 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12964 
12965 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12966 				  ahsta->link[arvif->link_id]);
12967 
12968 	if (!arsta || arsta->arvif != arvif)
12969 		return;
12970 
12971 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
12972 					arvif->vdev_id,
12973 					WMI_PEER_PARAM_FIXED_RATE,
12974 					WMI_FIXED_RATE_NONE);
12975 	if (ret)
12976 		ath12k_warn(ar->ab,
12977 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
12978 			    arsta->addr, ret);
12979 }
12980 
12981 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)12982 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
12983 					const struct cfg80211_bitrate_mask *mask,
12984 					unsigned int link_id)
12985 {
12986 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
12987 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
12988 	struct ieee80211_link_sta *link_sta;
12989 	struct ath12k_dp_link_peer *peer, *tmp;
12990 	u8 vht_nss, he_nss, eht_nss;
12991 	int ret = true;
12992 	struct ath12k_base *ab = ar->ab;
12993 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
12994 
12995 	vht_mcs_mask = mask->control[band].vht_mcs;
12996 	he_mcs_mask = mask->control[band].he_mcs;
12997 	eht_mcs_mask = mask->control[band].eht_mcs;
12998 
12999 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
13000 		vht_fixed_rate = true;
13001 
13002 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
13003 		he_fixed_rate = true;
13004 
13005 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
13006 		eht_fixed_rate = true;
13007 
13008 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
13009 		return true;
13010 
13011 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
13012 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
13013 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
13014 
13015 	rcu_read_lock();
13016 	spin_lock_bh(&dp->dp_lock);
13017 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
13018 		if (peer->sta) {
13019 			link_sta = rcu_dereference(peer->sta->link[link_id]);
13020 			if (!link_sta) {
13021 				ret = false;
13022 				goto exit;
13023 			}
13024 
13025 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13026 					       link_sta->rx_nss < vht_nss)) {
13027 				ret = false;
13028 				goto exit;
13029 			}
13030 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13031 					      link_sta->rx_nss < he_nss)) {
13032 				ret = false;
13033 				goto exit;
13034 			}
13035 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13036 					       link_sta->rx_nss < eht_nss)) {
13037 				ret = false;
13038 				goto exit;
13039 			}
13040 		}
13041 	}
13042 exit:
13043 	spin_unlock_bh(&dp->dp_lock);
13044 	rcu_read_unlock();
13045 	return ret;
13046 }
13047 
13048 int
ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)13049 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13050 			       struct ieee80211_vif *vif,
13051 			       const struct cfg80211_bitrate_mask *mask)
13052 {
13053 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13054 	struct ath12k_link_vif *arvif;
13055 	struct cfg80211_chan_def def;
13056 	struct ath12k *ar;
13057 	enum nl80211_band band;
13058 	const u8 *ht_mcs_mask;
13059 	const u16 *vht_mcs_mask;
13060 	const u16 *he_mcs_mask;
13061 	const u16 *eht_mcs_mask;
13062 	u8 he_ltf = 0;
13063 	u8 he_gi = 0;
13064 	u8 eht_ltf = 0, eht_gi = 0;
13065 	u32 rate;
13066 	u8 nss, mac_nss;
13067 	u8 sgi;
13068 	u8 ldpc;
13069 	int single_nss;
13070 	int ret;
13071 	int num_rates;
13072 	bool he_fixed_rate = false;
13073 	bool eht_fixed_rate = false;
13074 
13075 	lockdep_assert_wiphy(hw->wiphy);
13076 
13077 	arvif = &ahvif->deflink;
13078 
13079 	ar = arvif->ar;
13080 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13081 		ret = -EPERM;
13082 		goto out;
13083 	}
13084 
13085 	band = def.chan->band;
13086 	ht_mcs_mask = mask->control[band].ht_mcs;
13087 	vht_mcs_mask = mask->control[band].vht_mcs;
13088 	he_mcs_mask = mask->control[band].he_mcs;
13089 	eht_mcs_mask = mask->control[band].eht_mcs;
13090 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13091 
13092 	sgi = mask->control[band].gi;
13093 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13094 		ret = -EINVAL;
13095 		goto out;
13096 	}
13097 
13098 	he_gi = mask->control[band].he_gi;
13099 	he_ltf = mask->control[band].he_ltf;
13100 
13101 	eht_gi = mask->control[band].eht_gi;
13102 	eht_ltf = mask->control[band].eht_ltf;
13103 
13104 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13105 	 * requires passing at least one of used basic rates along with them.
13106 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13107 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13108 	 * suitable for setting single HT/VHT rates.
13109 	 * But, there could be a single basic rate passed from userspace which
13110 	 * can be done through the FIXED_RATE param.
13111 	 */
13112 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13113 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13114 							&nss);
13115 		if (ret) {
13116 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13117 				    arvif->vdev_id, ret);
13118 			goto out;
13119 		}
13120 
13121 		ieee80211_iterate_stations_mtx(hw,
13122 					       ath12k_mac_disable_peer_fixed_rate,
13123 					       arvif);
13124 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13125 							  &single_nss)) {
13126 		rate = WMI_FIXED_RATE_NONE;
13127 		nss = single_nss;
13128 		arvif->bitrate_mask = *mask;
13129 
13130 		ieee80211_iterate_stations_atomic(hw,
13131 						  ath12k_mac_set_bitrate_mask_iter,
13132 						  arvif);
13133 	} else {
13134 		rate = WMI_FIXED_RATE_NONE;
13135 
13136 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13137 							     mask, arvif->link_id))
13138 			ath12k_warn(ar->ab,
13139 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13140 
13141 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13142 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13143 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13144 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13145 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13146 
13147 		/* If multiple rates across different preambles are given
13148 		 * we can reconfigure this info with all peers using PEER_ASSOC
13149 		 * command with the below exception cases.
13150 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13151 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13152 		 * mandates passing basic rates along with HT/VHT rates, FW
13153 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13154 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13155 		 * we could set this VHT rate as peer fixed rate param, which
13156 		 * will override FIXED rate and FW rate control algorithm.
13157 		 * If single VHT rate is passed along with HT rates, we select
13158 		 * the VHT rate as fixed rate for vht peers.
13159 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13160 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13161 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13162 		 * RATEMASK_CMDID can cover all use cases of setting rates
13163 		 * across multiple preambles and rates within same type.
13164 		 * But requires more validation of the command at this point.
13165 		 */
13166 
13167 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13168 								  mask);
13169 
13170 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13171 		    num_rates > 1) {
13172 			/* TODO: Handle multiple VHT MCS values setting using
13173 			 * RATEMASK CMD
13174 			 */
13175 			ath12k_warn(ar->ab,
13176 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13177 			ret = -EINVAL;
13178 			goto out;
13179 		}
13180 
13181 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13182 		if (num_rates == 1)
13183 			he_fixed_rate = true;
13184 
13185 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13186 		    num_rates > 1) {
13187 			ath12k_warn(ar->ab,
13188 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13189 			ret = -EINVAL;
13190 			goto out;
13191 		}
13192 
13193 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13194 								  mask);
13195 		if (num_rates == 1)
13196 			eht_fixed_rate = true;
13197 
13198 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13199 		    num_rates > 1) {
13200 			ath12k_warn(ar->ab,
13201 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13202 			ret = -EINVAL;
13203 			goto out;
13204 		}
13205 
13206 		ieee80211_iterate_stations_mtx(hw,
13207 					       ath12k_mac_disable_peer_fixed_rate,
13208 					       arvif);
13209 
13210 		arvif->bitrate_mask = *mask;
13211 		ieee80211_iterate_stations_mtx(hw,
13212 					       ath12k_mac_set_bitrate_mask_iter,
13213 					       arvif);
13214 	}
13215 
13216 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13217 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13218 					 eht_fixed_rate);
13219 	if (ret) {
13220 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13221 			    arvif->vdev_id, ret);
13222 	}
13223 
13224 out:
13225 	return ret;
13226 }
13227 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13228 
13229 void
ath12k_mac_op_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)13230 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13231 				enum ieee80211_reconfig_type reconfig_type)
13232 {
13233 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13234 	struct ath12k *ar;
13235 	struct ath12k_base *ab;
13236 	struct ath12k_vif *ahvif;
13237 	struct ath12k_link_vif *arvif;
13238 	int recovery_count, i;
13239 
13240 	lockdep_assert_wiphy(hw->wiphy);
13241 
13242 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13243 		return;
13244 
13245 	guard(mutex)(&ah->hw_mutex);
13246 
13247 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13248 		return;
13249 
13250 	ah->state = ATH12K_HW_STATE_ON;
13251 	ieee80211_wake_queues(hw);
13252 
13253 	for_each_ar(ah, ar, i) {
13254 		ab = ar->ab;
13255 
13256 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13257 			    ar->pdev->pdev_id);
13258 
13259 		if (ar->ab->hw_params->current_cc_support &&
13260 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13261 			struct wmi_set_current_country_arg arg = {};
13262 
13263 			memcpy(&arg.alpha2, ar->alpha2, 2);
13264 			reinit_completion(&ar->regd_update_completed);
13265 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13266 		}
13267 
13268 		if (ab->is_reset) {
13269 			recovery_count = atomic_inc_return(&ab->recovery_count);
13270 
13271 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13272 				   recovery_count);
13273 
13274 			/* When there are multiple radios in an SOC,
13275 			 * the recovery has to be done for each radio
13276 			 */
13277 			if (recovery_count == ab->num_radios) {
13278 				atomic_dec(&ab->reset_count);
13279 				complete(&ab->reset_complete);
13280 				ab->is_reset = false;
13281 				atomic_set(&ab->fail_cont_count, 0);
13282 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13283 			}
13284 		}
13285 
13286 		list_for_each_entry(arvif, &ar->arvifs, list) {
13287 			ahvif = arvif->ahvif;
13288 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13289 				   "reconfig cipher %d up %d vdev type %d\n",
13290 				   ahvif->dp_vif.key_cipher,
13291 				   arvif->is_up,
13292 				   ahvif->vdev_type);
13293 
13294 			/* After trigger disconnect, then upper layer will
13295 			 * trigger connect again, then the PN number of
13296 			 * upper layer will be reset to keep up with AP
13297 			 * side, hence PN number mismatch will not happen.
13298 			 */
13299 			if (arvif->is_up &&
13300 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13301 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13302 				ieee80211_hw_restart_disconnect(ahvif->vif);
13303 
13304 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13305 					   "restart disconnect\n");
13306 			}
13307 		}
13308 	}
13309 }
13310 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13311 
13312 static void
ath12k_mac_update_bss_chan_survey(struct ath12k * ar,struct ieee80211_channel * channel)13313 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13314 				  struct ieee80211_channel *channel)
13315 {
13316 	int ret;
13317 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13318 
13319 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13320 
13321 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13322 	    ar->rx_channel != channel)
13323 		return;
13324 
13325 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13326 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13327 			   "ignoring bss chan info req while scanning..\n");
13328 		return;
13329 	}
13330 
13331 	reinit_completion(&ar->bss_survey_done);
13332 
13333 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13334 	if (ret) {
13335 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13336 		return;
13337 	}
13338 
13339 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13340 	if (ret == 0)
13341 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13342 }
13343 
ath12k_mac_op_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)13344 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13345 			     struct survey_info *survey)
13346 {
13347 	struct ath12k *ar;
13348 	struct ieee80211_supported_band *sband;
13349 	struct survey_info *ar_survey;
13350 
13351 	lockdep_assert_wiphy(hw->wiphy);
13352 
13353 	if (idx >= ATH12K_NUM_CHANS)
13354 		return -ENOENT;
13355 
13356 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13357 	if (sband && idx >= sband->n_channels) {
13358 		idx -= sband->n_channels;
13359 		sband = NULL;
13360 	}
13361 
13362 	if (!sband)
13363 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13364 	if (sband && idx >= sband->n_channels) {
13365 		idx -= sband->n_channels;
13366 		sband = NULL;
13367 	}
13368 
13369 	if (!sband)
13370 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13371 
13372 	if (!sband || idx >= sband->n_channels)
13373 		return -ENOENT;
13374 
13375 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13376 	if (!ar) {
13377 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13378 			memset(survey, 0, sizeof(*survey));
13379 			return 0;
13380 		}
13381 		return -ENOENT;
13382 	}
13383 
13384 	ar_survey = &ar->survey[idx];
13385 
13386 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13387 
13388 	spin_lock_bh(&ar->data_lock);
13389 	memcpy(survey, ar_survey, sizeof(*survey));
13390 	spin_unlock_bh(&ar->data_lock);
13391 
13392 	survey->channel = &sband->channels[idx];
13393 
13394 	if (ar->rx_channel == survey->channel)
13395 		survey->filled |= SURVEY_INFO_IN_USE;
13396 
13397 	return 0;
13398 }
13399 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13400 
ath12k_mac_put_chain_rssi(struct station_info * sinfo,struct ath12k_link_sta * arsta)13401 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13402 				      struct ath12k_link_sta *arsta)
13403 {
13404 	s8 rssi;
13405 	int i;
13406 
13407 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13408 		sinfo->chains &= ~BIT(i);
13409 		rssi = arsta->chain_signal[i];
13410 
13411 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13412 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13413 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13414 		    rssi != 0) {
13415 			sinfo->chain_signal[i] = rssi;
13416 			sinfo->chains |= BIT(i);
13417 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13418 		}
13419 	}
13420 }
13421 
ath12k_mac_op_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)13422 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13423 				  struct ieee80211_vif *vif,
13424 				  struct ieee80211_sta *sta,
13425 				  struct station_info *sinfo)
13426 {
13427 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13428 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13429 	struct ath12k_fw_stats_req_params params = {};
13430 	struct ath12k_dp_link_peer *peer;
13431 	struct ath12k_link_sta *arsta;
13432 	s8 signal, noise_floor;
13433 	struct ath12k_dp *dp;
13434 	struct ath12k *ar;
13435 	bool db2dbm;
13436 
13437 	lockdep_assert_wiphy(hw->wiphy);
13438 
13439 	arsta = &ahsta->deflink;
13440 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13441 	if (!ar)
13442 		return;
13443 
13444 	dp = ath12k_ab_to_dp(ar->ab);
13445 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13446 
13447 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13448 			  ar->ab->wmi_ab.svc_map);
13449 
13450 	sinfo->rx_duration = rate_info.rx_duration;
13451 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13452 
13453 	sinfo->tx_duration = rate_info.tx_duration;
13454 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13455 
13456 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13457 		if (rate_info.txrate.legacy) {
13458 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13459 		} else {
13460 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13461 			sinfo->txrate.nss = rate_info.txrate.nss;
13462 			sinfo->txrate.bw = rate_info.txrate.bw;
13463 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13464 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13465 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13466 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13467 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13468 		}
13469 		sinfo->txrate.flags = rate_info.txrate.flags;
13470 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13471 	}
13472 
13473 	/* TODO: Use real NF instead of default one. */
13474 	signal = rate_info.rssi_comb;
13475 
13476 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13477 	params.vdev_id = 0;
13478 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13479 
13480 	if (!signal &&
13481 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13482 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13483 		signal = arsta->rssi_beacon;
13484 		ath12k_fw_stats_reset(ar);
13485 	}
13486 
13487 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13488 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13489 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13490 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13491 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13492 		ath12k_fw_stats_reset(ar);
13493 	}
13494 
13495 	spin_lock_bh(&ar->data_lock);
13496 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13497 	spin_unlock_bh(&ar->data_lock);
13498 
13499 	if (signal) {
13500 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13501 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13502 	}
13503 
13504 	sinfo->signal_avg = rate_info.signal_avg;
13505 
13506 	if (!db2dbm)
13507 		sinfo->signal_avg += noise_floor;
13508 
13509 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13510 
13511 	spin_lock_bh(&dp->dp_lock);
13512 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13513 	if (!peer) {
13514 		spin_unlock_bh(&dp->dp_lock);
13515 		return;
13516 	}
13517 
13518 	sinfo->tx_retries = peer->tx_retry_count;
13519 	sinfo->tx_failed = peer->tx_retry_failed;
13520 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13521 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13522 
13523 	spin_unlock_bh(&dp->dp_lock);
13524 }
13525 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13526 
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)13527 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13528 				       struct ieee80211_vif *vif,
13529 				       struct ieee80211_link_sta *link_sta,
13530 				       struct link_station_info *link_sinfo)
13531 {
13532 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13533 	struct ath12k_fw_stats_req_params params = {};
13534 	struct ath12k_dp_link_peer *peer;
13535 	struct ath12k_link_sta *arsta;
13536 	struct ath12k *ar;
13537 	s8 signal;
13538 	bool db2dbm;
13539 
13540 	lockdep_assert_wiphy(hw->wiphy);
13541 
13542 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13543 
13544 	if (!arsta)
13545 		return;
13546 
13547 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13548 	if (!ar)
13549 		return;
13550 
13551 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13552 			  ar->ab->wmi_ab.svc_map);
13553 
13554 	spin_lock_bh(&ar->ab->dp->dp_lock);
13555 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13556 	if (!peer) {
13557 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13558 		return;
13559 	}
13560 
13561 	link_sinfo->rx_duration = peer->rx_duration;
13562 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13563 
13564 	link_sinfo->tx_duration = peer->tx_duration;
13565 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13566 
13567 	if (peer->txrate.legacy || peer->txrate.nss) {
13568 		if (peer->txrate.legacy) {
13569 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13570 		} else {
13571 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13572 			link_sinfo->txrate.nss = peer->txrate.nss;
13573 			link_sinfo->txrate.bw = peer->txrate.bw;
13574 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13575 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13576 			link_sinfo->txrate.he_ru_alloc =
13577 				peer->txrate.he_ru_alloc;
13578 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13579 			link_sinfo->txrate.eht_ru_alloc =
13580 				peer->txrate.eht_ru_alloc;
13581 		}
13582 		link_sinfo->txrate.flags = peer->txrate.flags;
13583 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13584 	}
13585 
13586 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13587 
13588 	if (!db2dbm)
13589 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13590 
13591 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13592 
13593 	link_sinfo->tx_retries = peer->tx_retry_count;
13594 	link_sinfo->tx_failed = peer->tx_retry_failed;
13595 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13596 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13597 
13598 	/* TODO: Use real NF instead of default one. */
13599 	signal = peer->rssi_comb;
13600 
13601 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13602 
13603 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13604 		params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13605 		params.vdev_id = 0;
13606 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13607 
13608 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13609 			signal = arsta->rssi_beacon;
13610 			ath12k_fw_stats_reset(ar);
13611 		}
13612 	}
13613 
13614 	if (signal) {
13615 		link_sinfo->signal =
13616 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13617 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13618 	}
13619 }
13620 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13621 
ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif)13622 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13623 					   struct ieee80211_vif *vif)
13624 {
13625 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13626 	struct ath12k *ar;
13627 
13628 	ar = ath12k_ah_to_ar(ah, 0);
13629 
13630 	lockdep_assert_wiphy(hw->wiphy);
13631 
13632 	spin_lock_bh(&ar->data_lock);
13633 	ar->scan.roc_notify = false;
13634 	spin_unlock_bh(&ar->data_lock);
13635 
13636 	ath12k_scan_abort(ar);
13637 
13638 	cancel_delayed_work_sync(&ar->scan.timeout);
13639 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13640 
13641 	return 0;
13642 }
13643 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13644 
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)13645 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13646 				    struct ieee80211_vif *vif,
13647 				    struct ieee80211_channel *chan,
13648 				    int duration,
13649 				    enum ieee80211_roc_type type)
13650 {
13651 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13652 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13653 	struct ath12k_link_vif *arvif;
13654 	struct ath12k *ar;
13655 	u32 scan_time_msec;
13656 	bool create = true;
13657 	u8 link_id;
13658 	int ret;
13659 
13660 	lockdep_assert_wiphy(hw->wiphy);
13661 
13662 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13663 	if (!ar)
13664 		return -EINVAL;
13665 
13666 	/* check if any of the links of ML VIF is already started on
13667 	 * radio(ar) corresponding to given scan frequency and use it,
13668 	 * if not use deflink(link 0) for scan purpose.
13669 	 */
13670 
13671 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13672 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13673 	/* If the vif is already assigned to a specific vdev of an ar,
13674 	 * check whether its already started, vdev which is started
13675 	 * are not allowed to switch to a new radio.
13676 	 * If the vdev is not started, but was earlier created on a
13677 	 * different ar, delete that vdev and create a new one. We don't
13678 	 * delete at the scan stop as an optimization to avoid redundant
13679 	 * delete-create vdev's for the same ar, in case the request is
13680 	 * always on the same band for the vif
13681 	 */
13682 	if (arvif->is_created) {
13683 		if (WARN_ON(!arvif->ar))
13684 			return -EINVAL;
13685 
13686 		if (ar != arvif->ar && arvif->is_started)
13687 			return -EBUSY;
13688 
13689 		if (ar != arvif->ar) {
13690 			ath12k_mac_remove_link_interface(hw, arvif);
13691 			ath12k_mac_unassign_link_vif(arvif);
13692 		} else {
13693 			create = false;
13694 		}
13695 	}
13696 
13697 	if (create) {
13698 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13699 
13700 		ret = ath12k_mac_vdev_create(ar, arvif);
13701 		if (ret) {
13702 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13703 				    ret);
13704 			ath12k_mac_unassign_link_vif(arvif);
13705 			return ret;
13706 		}
13707 	}
13708 
13709 	spin_lock_bh(&ar->data_lock);
13710 
13711 	switch (ar->scan.state) {
13712 	case ATH12K_SCAN_IDLE:
13713 		reinit_completion(&ar->scan.started);
13714 		reinit_completion(&ar->scan.completed);
13715 		reinit_completion(&ar->scan.on_channel);
13716 		ar->scan.state = ATH12K_SCAN_STARTING;
13717 		ar->scan.is_roc = true;
13718 		ar->scan.arvif = arvif;
13719 		ar->scan.roc_freq = chan->center_freq;
13720 		ar->scan.roc_notify = true;
13721 		ret = 0;
13722 		break;
13723 	case ATH12K_SCAN_STARTING:
13724 	case ATH12K_SCAN_RUNNING:
13725 	case ATH12K_SCAN_ABORTING:
13726 		ret = -EBUSY;
13727 		break;
13728 	}
13729 
13730 	spin_unlock_bh(&ar->data_lock);
13731 
13732 	if (ret)
13733 		return ret;
13734 
13735 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13736 
13737 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13738 					kzalloc_obj(*arg);
13739 	if (!arg)
13740 		return -ENOMEM;
13741 
13742 	ath12k_wmi_start_scan_init(ar, arg);
13743 	arg->num_chan = 1;
13744 
13745 	u32 *chan_list __free(kfree) = kcalloc(arg->num_chan, sizeof(*chan_list),
13746 					       GFP_KERNEL);
13747 	if (!chan_list)
13748 		return -ENOMEM;
13749 
13750 	arg->chan_list = chan_list;
13751 	arg->vdev_id = arvif->vdev_id;
13752 	arg->scan_id = ATH12K_SCAN_ID;
13753 	arg->chan_list[0] = chan->center_freq;
13754 	arg->dwell_time_active = scan_time_msec;
13755 	arg->dwell_time_passive = scan_time_msec;
13756 	arg->max_scan_time = scan_time_msec;
13757 	arg->scan_f_passive = 1;
13758 	arg->burst_duration = duration;
13759 
13760 	ret = ath12k_start_scan(ar, arg);
13761 	if (ret) {
13762 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13763 
13764 		spin_lock_bh(&ar->data_lock);
13765 		ar->scan.state = ATH12K_SCAN_IDLE;
13766 		spin_unlock_bh(&ar->data_lock);
13767 		return ret;
13768 	}
13769 
13770 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
13771 	if (ret == 0) {
13772 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
13773 		ret = ath12k_scan_stop(ar);
13774 		if (ret)
13775 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
13776 		return -ETIMEDOUT;
13777 	}
13778 
13779 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
13780 				     msecs_to_jiffies(duration));
13781 
13782 	return 0;
13783 }
13784 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
13785 
ath12k_mac_op_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)13786 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
13787 				  struct ieee80211_vif *vif,
13788 				  struct cfg80211_gtk_rekey_data *data)
13789 {
13790 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13791 	struct ath12k_rekey_data *rekey_data;
13792 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13793 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
13794 	struct ath12k_link_vif *arvif;
13795 
13796 	lockdep_assert_wiphy(hw->wiphy);
13797 
13798 	arvif = &ahvif->deflink;
13799 	rekey_data = &arvif->rekey_data;
13800 
13801 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
13802 		   arvif->vdev_id);
13803 
13804 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
13805 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
13806 
13807 	/* The supplicant works on big-endian, the firmware expects it on
13808 	 * little endian.
13809 	 */
13810 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
13811 
13812 	arvif->rekey_data.enable_offload = true;
13813 
13814 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
13815 			rekey_data->kck, NL80211_KCK_LEN);
13816 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
13817 			rekey_data->kck, NL80211_KEK_LEN);
13818 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
13819 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
13820 }
13821 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
13822 
ath12k_mac_update_freq_range(struct ath12k * ar,u32 freq_low,u32 freq_high)13823 void ath12k_mac_update_freq_range(struct ath12k *ar,
13824 				  u32 freq_low, u32 freq_high)
13825 {
13826 	if (!(freq_low && freq_high))
13827 		return;
13828 
13829 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
13830 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
13831 						MHZ_TO_KHZ(freq_low));
13832 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
13833 					      MHZ_TO_KHZ(freq_high));
13834 	} else {
13835 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
13836 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
13837 	}
13838 
13839 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13840 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
13841 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
13842 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
13843 }
13844 
ath12k_mac_update_ch_list(struct ath12k * ar,struct ieee80211_supported_band * band,u32 freq_low,u32 freq_high)13845 static void ath12k_mac_update_ch_list(struct ath12k *ar,
13846 				      struct ieee80211_supported_band *band,
13847 				      u32 freq_low, u32 freq_high)
13848 {
13849 	int i;
13850 
13851 	if (!(freq_low && freq_high))
13852 		return;
13853 
13854 	for (i = 0; i < band->n_channels; i++) {
13855 		if (band->channels[i].center_freq < freq_low ||
13856 		    band->channels[i].center_freq > freq_high)
13857 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13858 	}
13859 }
13860 
ath12k_get_phy_id(struct ath12k * ar,u32 band)13861 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
13862 {
13863 	struct ath12k_pdev *pdev = ar->pdev;
13864 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
13865 
13866 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
13867 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
13868 
13869 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
13870 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
13871 
13872 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
13873 
13874 	return 0;
13875 }
13876 
ath12k_mac_update_band(struct ath12k * ar,struct ieee80211_supported_band * orig_band,struct ieee80211_supported_band * new_band)13877 static int ath12k_mac_update_band(struct ath12k *ar,
13878 				  struct ieee80211_supported_band *orig_band,
13879 				  struct ieee80211_supported_band *new_band)
13880 {
13881 	int i;
13882 
13883 	if (!orig_band || !new_band)
13884 		return -EINVAL;
13885 
13886 	if (orig_band->band != new_band->band)
13887 		return -EINVAL;
13888 
13889 	for (i = 0; i < new_band->n_channels; i++) {
13890 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
13891 			continue;
13892 		/* An enabled channel in new_band should not be already enabled
13893 		 * in the orig_band
13894 		 */
13895 		if (WARN_ON(!(orig_band->channels[i].flags &
13896 			      IEEE80211_CHAN_DISABLED)))
13897 			return -EINVAL;
13898 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
13899 	}
13900 	return 0;
13901 }
13902 
ath12k_mac_setup_channels_rates(struct ath12k * ar,u32 supported_bands,struct ieee80211_supported_band * bands[])13903 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
13904 					   u32 supported_bands,
13905 					   struct ieee80211_supported_band *bands[])
13906 {
13907 	struct ieee80211_supported_band *band;
13908 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
13909 	struct ath12k_base *ab = ar->ab;
13910 	u32 phy_id, freq_low, freq_high;
13911 	struct ath12k_hw *ah = ar->ah;
13912 	void *channels;
13913 	int ret;
13914 
13915 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
13916 		      ARRAY_SIZE(ath12k_5ghz_channels) +
13917 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
13918 		     ATH12K_NUM_CHANS);
13919 
13920 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
13921 
13922 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
13923 		channels = kmemdup(ath12k_2ghz_channels,
13924 				   sizeof(ath12k_2ghz_channels),
13925 				   GFP_KERNEL);
13926 		if (!channels)
13927 			return -ENOMEM;
13928 
13929 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
13930 		band->band = NL80211_BAND_2GHZ;
13931 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
13932 		band->channels = channels;
13933 		band->n_bitrates = ath12k_g_rates_size;
13934 		band->bitrates = ath12k_g_rates;
13935 
13936 		if (ab->hw_params->single_pdev_only) {
13937 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
13938 			reg_cap = &ab->hal_reg_cap[phy_id];
13939 		}
13940 
13941 		freq_low = max(reg_cap->low_2ghz_chan,
13942 			       ab->reg_freq_2ghz.start_freq);
13943 		freq_high = min(reg_cap->high_2ghz_chan,
13944 				ab->reg_freq_2ghz.end_freq);
13945 
13946 		ath12k_mac_update_ch_list(ar, band,
13947 					  reg_cap->low_2ghz_chan,
13948 					  reg_cap->high_2ghz_chan);
13949 
13950 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13951 
13952 		if (!bands[NL80211_BAND_2GHZ]) {
13953 			bands[NL80211_BAND_2GHZ] = band;
13954 		} else {
13955 			/* Split mac in same band under same wiphy */
13956 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
13957 			if (ret) {
13958 				kfree(channels);
13959 				band->channels = NULL;
13960 				return ret;
13961 			}
13962 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
13963 				   ar->pdev->pdev_id,
13964 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
13965 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
13966 		}
13967 	}
13968 
13969 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
13970 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
13971 			channels = kmemdup(ath12k_6ghz_channels,
13972 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
13973 			if (!channels) {
13974 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13975 				return -ENOMEM;
13976 			}
13977 
13978 			ar->supports_6ghz = true;
13979 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
13980 			band->band = NL80211_BAND_6GHZ;
13981 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
13982 			band->channels = channels;
13983 			band->n_bitrates = ath12k_a_rates_size;
13984 			band->bitrates = ath12k_a_rates;
13985 
13986 			freq_low = max(reg_cap->low_5ghz_chan,
13987 				       ab->reg_freq_6ghz.start_freq);
13988 			freq_high = min(reg_cap->high_5ghz_chan,
13989 					ab->reg_freq_6ghz.end_freq);
13990 
13991 			ath12k_mac_update_ch_list(ar, band,
13992 						  reg_cap->low_5ghz_chan,
13993 						  reg_cap->high_5ghz_chan);
13994 
13995 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13996 			ah->use_6ghz_regd = true;
13997 
13998 			if (!bands[NL80211_BAND_6GHZ]) {
13999 				bands[NL80211_BAND_6GHZ] = band;
14000 			} else {
14001 				/* Split mac in same band under same wiphy */
14002 				ret = ath12k_mac_update_band(ar,
14003 							     bands[NL80211_BAND_6GHZ],
14004 							     band);
14005 				if (ret) {
14006 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14007 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14008 					kfree(channels);
14009 					band->channels = NULL;
14010 					return ret;
14011 				}
14012 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
14013 					   ar->pdev->pdev_id,
14014 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14015 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14016 			}
14017 		}
14018 
14019 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
14020 			channels = kmemdup(ath12k_5ghz_channels,
14021 					   sizeof(ath12k_5ghz_channels),
14022 					   GFP_KERNEL);
14023 			if (!channels) {
14024 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14025 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14026 				return -ENOMEM;
14027 			}
14028 
14029 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14030 			band->band = NL80211_BAND_5GHZ;
14031 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14032 			band->channels = channels;
14033 			band->n_bitrates = ath12k_a_rates_size;
14034 			band->bitrates = ath12k_a_rates;
14035 
14036 			if (ab->hw_params->single_pdev_only) {
14037 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14038 				reg_cap = &ab->hal_reg_cap[phy_id];
14039 			}
14040 
14041 			freq_low = max(reg_cap->low_5ghz_chan,
14042 				       ab->reg_freq_5ghz.start_freq);
14043 			freq_high = min(reg_cap->high_5ghz_chan,
14044 					ab->reg_freq_5ghz.end_freq);
14045 
14046 			ath12k_mac_update_ch_list(ar, band,
14047 						  reg_cap->low_5ghz_chan,
14048 						  reg_cap->high_5ghz_chan);
14049 
14050 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14051 
14052 			if (!bands[NL80211_BAND_5GHZ]) {
14053 				bands[NL80211_BAND_5GHZ] = band;
14054 			} else {
14055 				/* Split mac in same band under same wiphy */
14056 				ret = ath12k_mac_update_band(ar,
14057 							     bands[NL80211_BAND_5GHZ],
14058 							     band);
14059 				if (ret) {
14060 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14061 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14062 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14063 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14064 					kfree(channels);
14065 					band->channels = NULL;
14066 					return ret;
14067 				}
14068 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14069 					   ar->pdev->pdev_id,
14070 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14071 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14072 			}
14073 		}
14074 	}
14075 
14076 	return 0;
14077 }
14078 
ath12k_mac_get_ifmodes(struct ath12k_hw * ah)14079 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14080 {
14081 	struct ath12k *ar;
14082 	int i;
14083 	u16 interface_modes = U16_MAX;
14084 
14085 	for_each_ar(ah, ar, i)
14086 		interface_modes &= ar->ab->hw_params->interface_modes;
14087 
14088 	return interface_modes == U16_MAX ? 0 : interface_modes;
14089 }
14090 
ath12k_mac_is_iface_mode_enable(struct ath12k_hw * ah,enum nl80211_iftype type)14091 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14092 					    enum nl80211_iftype type)
14093 {
14094 	struct ath12k *ar;
14095 	int i;
14096 	u16 interface_modes, mode = 0;
14097 	bool is_enable = false;
14098 
14099 	if (type == NL80211_IFTYPE_MESH_POINT) {
14100 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14101 			mode = BIT(type);
14102 	} else {
14103 		mode = BIT(type);
14104 	}
14105 
14106 	for_each_ar(ah, ar, i) {
14107 		interface_modes = ar->ab->hw_params->interface_modes;
14108 		if (interface_modes & mode) {
14109 			is_enable = true;
14110 			break;
14111 		}
14112 	}
14113 
14114 	return is_enable;
14115 }
14116 
14117 static int
ath12k_mac_setup_radio_iface_comb(struct ath12k * ar,struct ieee80211_iface_combination * comb)14118 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14119 				  struct ieee80211_iface_combination *comb)
14120 {
14121 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14122 	struct ieee80211_iface_limit *limits;
14123 	int n_limits, max_interfaces;
14124 	bool ap, mesh, p2p;
14125 
14126 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14127 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14128 
14129 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14130 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14131 
14132 	if ((ap || mesh) && !p2p) {
14133 		n_limits = 2;
14134 		max_interfaces = 16;
14135 	} else if (p2p) {
14136 		n_limits = 3;
14137 		if (ap || mesh)
14138 			max_interfaces = 16;
14139 		else
14140 			max_interfaces = 3;
14141 	} else {
14142 		n_limits = 1;
14143 		max_interfaces = 1;
14144 	}
14145 
14146 	limits = kzalloc_objs(*limits, n_limits);
14147 	if (!limits)
14148 		return -ENOMEM;
14149 
14150 	limits[0].max = 1;
14151 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14152 
14153 	if (ap || mesh || p2p)
14154 		limits[1].max = max_interfaces;
14155 
14156 	if (ap)
14157 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14158 
14159 	if (mesh)
14160 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14161 
14162 	if (p2p) {
14163 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14164 					BIT(NL80211_IFTYPE_P2P_GO);
14165 		limits[2].max = 1;
14166 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14167 	}
14168 
14169 	comb[0].limits = limits;
14170 	comb[0].n_limits = n_limits;
14171 	comb[0].max_interfaces = max_interfaces;
14172 	comb[0].beacon_int_infra_match = true;
14173 	comb[0].beacon_int_min_gcd = 100;
14174 
14175 	comb[0].num_different_channels = 1;
14176 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14177 				      BIT(NL80211_CHAN_WIDTH_20) |
14178 				      BIT(NL80211_CHAN_WIDTH_40) |
14179 				      BIT(NL80211_CHAN_WIDTH_80) |
14180 				      BIT(NL80211_CHAN_WIDTH_160);
14181 
14182 	return 0;
14183 }
14184 
14185 static int
ath12k_mac_setup_global_iface_comb(struct ath12k_hw * ah,struct wiphy_radio * radio,u8 n_radio,struct ieee80211_iface_combination * comb)14186 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14187 				   struct wiphy_radio *radio,
14188 				   u8 n_radio,
14189 				   struct ieee80211_iface_combination *comb)
14190 {
14191 	const struct ieee80211_iface_combination *iter_comb;
14192 	struct ieee80211_iface_limit *limits;
14193 	int i, j, n_limits;
14194 	bool ap, mesh, p2p;
14195 
14196 	if (!n_radio)
14197 		return 0;
14198 
14199 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14200 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14201 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14202 
14203 	if ((ap || mesh) && !p2p)
14204 		n_limits = 2;
14205 	else if (p2p)
14206 		n_limits = 3;
14207 	else
14208 		n_limits = 1;
14209 
14210 	limits = kzalloc_objs(*limits, n_limits);
14211 	if (!limits)
14212 		return -ENOMEM;
14213 
14214 	for (i = 0; i < n_radio; i++) {
14215 		iter_comb = radio[i].iface_combinations;
14216 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14217 			limits[j].types |= iter_comb->limits[j].types;
14218 			limits[j].max += iter_comb->limits[j].max;
14219 		}
14220 
14221 		comb->max_interfaces += iter_comb->max_interfaces;
14222 		comb->num_different_channels += iter_comb->num_different_channels;
14223 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14224 	}
14225 
14226 	comb->limits = limits;
14227 	comb->n_limits = n_limits;
14228 	comb->beacon_int_infra_match = true;
14229 	comb->beacon_int_min_gcd = 100;
14230 
14231 	return 0;
14232 }
14233 
14234 static
ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination * iface_comb)14235 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14236 {
14237 	kfree(iface_comb[0].limits);
14238 	kfree(iface_comb);
14239 }
14240 
ath12k_mac_cleanup_iface_combinations(struct ath12k_hw * ah)14241 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14242 {
14243 	struct wiphy *wiphy = ah->hw->wiphy;
14244 	const struct wiphy_radio *radio;
14245 	int i;
14246 
14247 	if (wiphy->n_radio > 0) {
14248 		radio = wiphy->radio;
14249 		for (i = 0; i < wiphy->n_radio; i++)
14250 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14251 
14252 		kfree(wiphy->radio);
14253 	}
14254 
14255 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14256 }
14257 
ath12k_mac_setup_iface_combinations(struct ath12k_hw * ah)14258 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14259 {
14260 	struct ieee80211_iface_combination *combinations, *comb;
14261 	struct wiphy *wiphy = ah->hw->wiphy;
14262 	struct wiphy_radio *radio;
14263 	int n_combinations = 1;
14264 	struct ath12k *ar;
14265 	int i, ret;
14266 
14267 	if (ah->num_radio == 1) {
14268 		ar = &ah->radio[0];
14269 
14270 		if (ar->ab->hw_params->single_pdev_only)
14271 			n_combinations = 2;
14272 
14273 		combinations = kzalloc_objs(*combinations, n_combinations);
14274 		if (!combinations)
14275 			return -ENOMEM;
14276 
14277 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14278 		if (ret) {
14279 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14280 				       ret);
14281 			goto err_free_combinations;
14282 		}
14283 
14284 		if (ar->ab->hw_params->single_pdev_only) {
14285 			comb = combinations + 1;
14286 			memcpy(comb, combinations, sizeof(*comb));
14287 			comb->num_different_channels = 2;
14288 			comb->radar_detect_widths = 0;
14289 		}
14290 
14291 		goto out;
14292 	}
14293 
14294 	combinations = kzalloc_objs(*combinations, n_combinations);
14295 	if (!combinations)
14296 		return -ENOMEM;
14297 
14298 	/* there are multiple radios */
14299 
14300 	radio = kzalloc_objs(*radio, ah->num_radio);
14301 	if (!radio) {
14302 		ret = -ENOMEM;
14303 		goto err_free_combinations;
14304 	}
14305 
14306 	for_each_ar(ah, ar, i) {
14307 		comb = kzalloc_obj(*comb);
14308 		if (!comb) {
14309 			ret = -ENOMEM;
14310 			goto err_free_radios;
14311 		}
14312 
14313 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14314 		if (ret) {
14315 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14316 				       i, ret);
14317 			kfree(comb);
14318 			goto err_free_radios;
14319 		}
14320 
14321 		radio[i].freq_range = &ar->freq_range;
14322 		radio[i].n_freq_range = 1;
14323 
14324 		radio[i].iface_combinations = comb;
14325 		radio[i].n_iface_combinations = 1;
14326 	}
14327 
14328 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14329 	if (ret) {
14330 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14331 			       ret);
14332 		goto err_free_all_radios;
14333 	}
14334 
14335 	wiphy->radio = radio;
14336 	wiphy->n_radio = ah->num_radio;
14337 
14338 out:
14339 	wiphy->iface_combinations = combinations;
14340 	wiphy->n_iface_combinations = n_combinations;
14341 
14342 	return 0;
14343 
14344 err_free_all_radios:
14345 	i = ah->num_radio;
14346 
14347 err_free_radios:
14348 	while (i--)
14349 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14350 
14351 	kfree(radio);
14352 
14353 err_free_combinations:
14354 	kfree(combinations);
14355 
14356 	return ret;
14357 }
14358 
14359 static const u8 ath12k_if_types_ext_capa[] = {
14360 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14361 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14362 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14363 };
14364 
14365 static const u8 ath12k_if_types_ext_capa_sta[] = {
14366 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14367 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14368 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14369 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14370 };
14371 
14372 static const u8 ath12k_if_types_ext_capa_ap[] = {
14373 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14374 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14375 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14376 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14377 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14378 };
14379 
14380 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14381 	{
14382 		.extended_capabilities = ath12k_if_types_ext_capa,
14383 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14384 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14385 	}, {
14386 		.iftype = NL80211_IFTYPE_STATION,
14387 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14388 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14389 		.extended_capabilities_len =
14390 				sizeof(ath12k_if_types_ext_capa_sta),
14391 	}, {
14392 		.iftype = NL80211_IFTYPE_AP,
14393 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14394 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14395 		.extended_capabilities_len =
14396 				sizeof(ath12k_if_types_ext_capa_ap),
14397 		.eml_capabilities = 0,
14398 		.mld_capa_and_ops = 0,
14399 	},
14400 };
14401 
ath12k_mac_cleanup_unregister(struct ath12k * ar)14402 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14403 {
14404 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14405 	idr_destroy(&ar->txmgmt_idr);
14406 
14407 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14408 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14409 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14410 }
14411 
ath12k_mac_hw_unregister(struct ath12k_hw * ah)14412 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14413 {
14414 	struct ieee80211_hw *hw = ah->hw;
14415 	struct ath12k *ar;
14416 	int i;
14417 
14418 	for_each_ar(ah, ar, i) {
14419 		cancel_work_sync(&ar->regd_channel_update_work);
14420 		cancel_work_sync(&ar->regd_update_work);
14421 		ath12k_debugfs_unregister(ar);
14422 		ath12k_fw_stats_reset(ar);
14423 	}
14424 
14425 	ieee80211_unregister_hw(hw);
14426 
14427 	for_each_ar(ah, ar, i)
14428 		ath12k_mac_cleanup_unregister(ar);
14429 
14430 	ath12k_mac_cleanup_iface_combinations(ah);
14431 
14432 	SET_IEEE80211_DEV(hw, NULL);
14433 }
14434 
ath12k_mac_setup_register(struct ath12k * ar,u32 * ht_cap,struct ieee80211_supported_band * bands[])14435 static int ath12k_mac_setup_register(struct ath12k *ar,
14436 				     u32 *ht_cap,
14437 				     struct ieee80211_supported_band *bands[])
14438 {
14439 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14440 	int ret;
14441 
14442 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14443 	idr_init(&ar->txmgmt_idr);
14444 	spin_lock_init(&ar->txmgmt_idr_lock);
14445 
14446 	ath12k_pdev_caps_update(ar);
14447 
14448 	ret = ath12k_mac_setup_channels_rates(ar,
14449 					      cap->supported_bands,
14450 					      bands);
14451 	if (ret)
14452 		return ret;
14453 
14454 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14455 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14456 
14457 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14458 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14459 
14460 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14461 	ar->rssi_info.temp_offset = 0;
14462 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14463 
14464 	return 0;
14465 }
14466 
ath12k_mac_hw_register(struct ath12k_hw * ah)14467 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14468 {
14469 	struct ieee80211_hw *hw = ah->hw;
14470 	struct wiphy *wiphy = hw->wiphy;
14471 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14472 	struct ath12k_base *ab = ar->ab;
14473 	struct ath12k_pdev *pdev;
14474 	struct ath12k_pdev_cap *cap;
14475 	static const u32 cipher_suites[] = {
14476 		WLAN_CIPHER_SUITE_TKIP,
14477 		WLAN_CIPHER_SUITE_CCMP,
14478 		WLAN_CIPHER_SUITE_AES_CMAC,
14479 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14480 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14481 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14482 		WLAN_CIPHER_SUITE_GCMP,
14483 		WLAN_CIPHER_SUITE_GCMP_256,
14484 		WLAN_CIPHER_SUITE_CCMP_256,
14485 	};
14486 	int ret, i, j;
14487 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14488 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14489 	u8 *mac_addr = NULL;
14490 	u8 mbssid_max_interfaces = 0;
14491 
14492 	wiphy->max_ap_assoc_sta = 0;
14493 
14494 	for_each_ar(ah, ar, i) {
14495 		u32 ht_cap_info = 0;
14496 
14497 		pdev = ar->pdev;
14498 		if (ar->ab->pdevs_macaddr_valid) {
14499 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14500 		} else {
14501 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14502 			ar->mac_addr[4] += ar->pdev_idx;
14503 		}
14504 
14505 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14506 		if (ret)
14507 			goto err_cleanup_unregister;
14508 
14509 		/* 6 GHz does not support HT Cap, hence do not consider it */
14510 		if (!ar->supports_6ghz)
14511 			ht_cap &= ht_cap_info;
14512 
14513 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14514 
14515 		/* Advertise the max antenna support of all radios, driver can handle
14516 		 * per pdev specific antenna setting based on pdev cap when antenna
14517 		 * changes are made
14518 		 */
14519 		cap = &pdev->cap;
14520 
14521 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14522 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14523 
14524 		if (ar->supports_6ghz)
14525 			is_6ghz = true;
14526 
14527 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14528 			is_raw_mode = true;
14529 
14530 		if (!ar->ab->hw_params->supports_monitor)
14531 			is_monitor_disable = true;
14532 
14533 		if (i == 0)
14534 			mac_addr = ar->mac_addr;
14535 		else
14536 			mac_addr = ab->mac_addr;
14537 
14538 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14539 	}
14540 
14541 	wiphy->available_antennas_rx = antennas_rx;
14542 	wiphy->available_antennas_tx = antennas_tx;
14543 
14544 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14545 	SET_IEEE80211_DEV(hw, ab->dev);
14546 
14547 	ret = ath12k_mac_setup_iface_combinations(ah);
14548 	if (ret) {
14549 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14550 		goto err_complete_cleanup_unregister;
14551 	}
14552 
14553 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14554 
14555 	if (ah->num_radio == 1 &&
14556 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14557 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14558 	    wiphy->bands[NL80211_BAND_6GHZ])
14559 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14560 
14561 	ieee80211_hw_set(hw, SIGNAL_DBM);
14562 	ieee80211_hw_set(hw, SUPPORTS_PS);
14563 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14564 	ieee80211_hw_set(hw, MFP_CAPABLE);
14565 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14566 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14567 	ieee80211_hw_set(hw, AP_LINK_PS);
14568 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14569 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14570 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14571 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14572 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14573 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14574 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14575 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14576 
14577 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14578 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14579 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14580 	}
14581 
14582 	if (cap->nss_ratio_enabled)
14583 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14584 
14585 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14586 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14587 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14588 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14589 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14590 		ieee80211_hw_set(hw, USES_RSS);
14591 	}
14592 
14593 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14594 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14595 
14596 	/* TODO: Check if HT capability advertised from firmware is different
14597 	 * for each band for a dual band capable radio. It will be tricky to
14598 	 * handle it when the ht capability different for each band.
14599 	 */
14600 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14601 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14602 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14603 
14604 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14605 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14606 
14607 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14608 
14609 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14610 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14611 	wiphy->max_remain_on_channel_duration = 5000;
14612 
14613 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14614 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14615 				   NL80211_FEATURE_AP_SCAN;
14616 
14617 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14618 
14619 	/* MLO is not yet supported so disable Wireless Extensions for now
14620 	 * to make sure ath12k users don't use it. This flag can be removed
14621 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14622 	 */
14623 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14624 
14625 	/* Copy over MLO related capabilities received from
14626 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14627 	 */
14628 	if (ab->ag->mlo_capable) {
14629 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14630 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14631 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14632 
14633 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14634 	}
14635 
14636 	hw->queues = ATH12K_HW_MAX_QUEUES;
14637 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14638 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14639 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14640 
14641 	hw->vif_data_size = sizeof(struct ath12k_vif);
14642 	hw->sta_data_size = sizeof(struct ath12k_sta);
14643 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14644 
14645 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14646 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14647 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14648 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14649 		     ab->wmi_ab.svc_map)) {
14650 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14651 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14652 	}
14653 
14654 	wiphy->cipher_suites = cipher_suites;
14655 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14656 
14657 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14658 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14659 
14660 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14661 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14662 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14663 
14664 	if (is_6ghz) {
14665 		wiphy_ext_feature_set(wiphy,
14666 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14667 		wiphy_ext_feature_set(wiphy,
14668 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14669 	}
14670 
14671 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14672 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14673 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14674 
14675 	ath12k_reg_init(hw);
14676 
14677 	if (!is_raw_mode) {
14678 		hw->netdev_features = NETIF_F_HW_CSUM;
14679 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14680 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14681 	}
14682 
14683 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14684 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14685 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14686 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14687 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14688 		wiphy->max_sched_scan_plan_interval =
14689 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14690 		wiphy->max_sched_scan_plan_iterations =
14691 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14692 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14693 	}
14694 
14695 	ret = ath12k_wow_init(ar);
14696 	if (ret) {
14697 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14698 		goto err_cleanup_if_combs;
14699 	}
14700 
14701 	/* Boot-time regulatory updates have already been processed.
14702 	 * Mark them as complete now, because after registration,
14703 	 * cfg80211 will notify us again if there are any pending hints.
14704 	 * We need to wait for those hints to be processed, so it's
14705 	 * important to mark the boot-time updates as complete before
14706 	 * proceeding with registration.
14707 	 */
14708 	for_each_ar(ah, ar, i)
14709 		complete_all(&ar->regd_update_completed);
14710 
14711 	ret = ieee80211_register_hw(hw);
14712 	if (ret) {
14713 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14714 		goto err_cleanup_if_combs;
14715 	}
14716 
14717 	if (is_monitor_disable)
14718 		/* There's a race between calling ieee80211_register_hw()
14719 		 * and here where the monitor mode is enabled for a little
14720 		 * while. But that time is so short and in practice it doesn't make
14721 		 * a difference in real life.
14722 		 */
14723 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14724 
14725 	for_each_ar(ah, ar, i) {
14726 		/* Apply the regd received during initialization */
14727 		ret = ath12k_regd_update(ar, true);
14728 		if (ret) {
14729 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14730 			goto err_unregister_hw;
14731 		}
14732 
14733 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14734 			struct wmi_set_current_country_arg current_cc = {};
14735 
14736 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14737 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14738 
14739 			reinit_completion(&ar->regd_update_completed);
14740 
14741 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
14742 			if (ret)
14743 				ath12k_warn(ar->ab,
14744 					    "failed set cc code for mac register: %d\n",
14745 					    ret);
14746 		}
14747 
14748 		ath12k_fw_stats_init(ar);
14749 		ath12k_debugfs_register(ar);
14750 	}
14751 
14752 	return 0;
14753 
14754 err_unregister_hw:
14755 	for_each_ar(ah, ar, i)
14756 		ath12k_debugfs_unregister(ar);
14757 
14758 	ieee80211_unregister_hw(hw);
14759 
14760 err_cleanup_if_combs:
14761 	ath12k_mac_cleanup_iface_combinations(ah);
14762 
14763 err_complete_cleanup_unregister:
14764 	i = ah->num_radio;
14765 
14766 err_cleanup_unregister:
14767 	for (j = 0; j < i; j++) {
14768 		ar = ath12k_ah_to_ar(ah, j);
14769 		ath12k_mac_cleanup_unregister(ar);
14770 	}
14771 
14772 	SET_IEEE80211_DEV(hw, NULL);
14773 
14774 	return ret;
14775 }
14776 
ath12k_mac_setup(struct ath12k * ar)14777 static void ath12k_mac_setup(struct ath12k *ar)
14778 {
14779 	struct ath12k_base *ab = ar->ab;
14780 	struct ath12k_pdev *pdev = ar->pdev;
14781 	u8 pdev_idx = ar->pdev_idx;
14782 
14783 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
14784 
14785 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
14786 	/* FIXME: wmi[0] is already initialized during attach,
14787 	 * Should we do this again?
14788 	 */
14789 	ath12k_wmi_pdev_attach(ab, pdev_idx);
14790 
14791 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
14792 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
14793 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
14794 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
14795 	ar->scan.arvif = NULL;
14796 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
14797 
14798 	spin_lock_init(&ar->data_lock);
14799 	spin_lock_init(&ar->dp.ppdu_list_lock);
14800 	INIT_LIST_HEAD(&ar->arvifs);
14801 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
14802 
14803 	init_completion(&ar->vdev_setup_done);
14804 	init_completion(&ar->vdev_delete_done);
14805 	init_completion(&ar->peer_assoc_done);
14806 	init_completion(&ar->peer_delete_done);
14807 	init_completion(&ar->install_key_done);
14808 	init_completion(&ar->bss_survey_done);
14809 	init_completion(&ar->scan.started);
14810 	init_completion(&ar->scan.completed);
14811 	init_completion(&ar->scan.on_channel);
14812 	init_completion(&ar->mlo_setup_done);
14813 	init_completion(&ar->completed_11d_scan);
14814 	init_completion(&ar->regd_update_completed);
14815 	init_completion(&ar->thermal.wmi_sync);
14816 
14817 	ar->thermal.temperature = 0;
14818 	ar->thermal.hwmon_dev = NULL;
14819 
14820 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
14821 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
14822 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
14823 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
14824 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
14825 
14826 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
14827 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
14828 
14829 	ar->monitor_vdev_id = -1;
14830 	ar->monitor_vdev_created = false;
14831 	ar->monitor_started = false;
14832 }
14833 
__ath12k_mac_mlo_setup(struct ath12k * ar)14834 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
14835 {
14836 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
14837 	struct ath12k_base *partner_ab, *ab = ar->ab;
14838 	struct ath12k_hw_group *ag = ab->ag;
14839 	struct wmi_mlo_setup_arg mlo = {};
14840 	struct ath12k_pdev *pdev;
14841 	unsigned long time_left;
14842 	int i, j, ret;
14843 
14844 	lockdep_assert_held(&ag->mutex);
14845 
14846 	reinit_completion(&ar->mlo_setup_done);
14847 
14848 	for (i = 0; i < ag->num_devices; i++) {
14849 		partner_ab = ag->ab[i];
14850 
14851 		for (j = 0; j < partner_ab->num_radios; j++) {
14852 			pdev = &partner_ab->pdevs[j];
14853 
14854 			/* Avoid the self link */
14855 			if (ar == pdev->ar)
14856 				continue;
14857 
14858 			partner_link_id[num_link] = pdev->hw_link_id;
14859 			num_link++;
14860 
14861 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
14862 				   i, j, pdev->hw_link_id, num_link);
14863 		}
14864 	}
14865 
14866 	if (num_link == 0)
14867 		return 0;
14868 
14869 	mlo.group_id = cpu_to_le32(ag->id);
14870 	mlo.partner_link_id = partner_link_id;
14871 	mlo.num_partner_links = num_link;
14872 	ar->mlo_setup_status = 0;
14873 
14874 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
14875 
14876 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
14877 	if (ret) {
14878 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
14879 			   ar->pdev_idx, ret);
14880 		return ret;
14881 	}
14882 
14883 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
14884 						WMI_MLO_CMD_TIMEOUT_HZ);
14885 
14886 	if (!time_left || ar->mlo_setup_status)
14887 		return ar->mlo_setup_status ? : -ETIMEDOUT;
14888 
14889 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
14890 
14891 	return 0;
14892 }
14893 
__ath12k_mac_mlo_teardown(struct ath12k * ar)14894 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
14895 {
14896 	struct ath12k_base *ab = ar->ab;
14897 	int ret;
14898 	u8 num_link;
14899 
14900 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
14901 		return 0;
14902 
14903 	num_link = ath12k_get_num_partner_link(ar);
14904 
14905 	if (num_link == 0)
14906 		return 0;
14907 
14908 	ret = ath12k_wmi_mlo_teardown(ar);
14909 	if (ret) {
14910 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
14911 			    ar->pdev_idx, ret);
14912 		return ret;
14913 	}
14914 
14915 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
14916 
14917 	return 0;
14918 }
14919 
ath12k_mac_mlo_setup(struct ath12k_hw_group * ag)14920 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
14921 {
14922 	struct ath12k_hw *ah;
14923 	struct ath12k *ar;
14924 	int ret;
14925 	int i, j;
14926 
14927 	for (i = 0; i < ag->num_hw; i++) {
14928 		ah = ag->ah[i];
14929 		if (!ah)
14930 			continue;
14931 
14932 		for_each_ar(ah, ar, j) {
14933 			ar = &ah->radio[j];
14934 			ret = __ath12k_mac_mlo_setup(ar);
14935 			if (ret) {
14936 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
14937 				goto err_setup;
14938 			}
14939 		}
14940 	}
14941 
14942 	return 0;
14943 
14944 err_setup:
14945 	for (i = i - 1; i >= 0; i--) {
14946 		ah = ag->ah[i];
14947 		if (!ah)
14948 			continue;
14949 
14950 		for (j = j - 1; j >= 0; j--) {
14951 			ar = &ah->radio[j];
14952 			if (!ar)
14953 				continue;
14954 
14955 			__ath12k_mac_mlo_teardown(ar);
14956 		}
14957 	}
14958 
14959 	return ret;
14960 }
14961 
ath12k_mac_mlo_teardown(struct ath12k_hw_group * ag)14962 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
14963 {
14964 	struct ath12k_hw *ah;
14965 	struct ath12k *ar;
14966 	int ret, i, j;
14967 
14968 	for (i = 0; i < ag->num_hw; i++) {
14969 		ah = ag->ah[i];
14970 		if (!ah)
14971 			continue;
14972 
14973 		for_each_ar(ah, ar, j) {
14974 			ar = &ah->radio[j];
14975 			ret = __ath12k_mac_mlo_teardown(ar);
14976 			if (ret) {
14977 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
14978 				break;
14979 			}
14980 		}
14981 	}
14982 }
14983 
ath12k_mac_register(struct ath12k_hw_group * ag)14984 int ath12k_mac_register(struct ath12k_hw_group *ag)
14985 {
14986 	struct ath12k_hw *ah;
14987 	int i;
14988 	int ret;
14989 
14990 	for (i = 0; i < ag->num_hw; i++) {
14991 		ah = ath12k_ag_to_ah(ag, i);
14992 
14993 		ret = ath12k_mac_hw_register(ah);
14994 		if (ret)
14995 			goto err;
14996 	}
14997 
14998 	return 0;
14999 
15000 err:
15001 	for (i = i - 1; i >= 0; i--) {
15002 		ah = ath12k_ag_to_ah(ag, i);
15003 		if (!ah)
15004 			continue;
15005 
15006 		ath12k_mac_hw_unregister(ah);
15007 	}
15008 
15009 	return ret;
15010 }
15011 
ath12k_mac_unregister(struct ath12k_hw_group * ag)15012 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
15013 {
15014 	struct ath12k_hw *ah;
15015 	int i;
15016 
15017 	for (i = ag->num_hw - 1; i >= 0; i--) {
15018 		ah = ath12k_ag_to_ah(ag, i);
15019 		if (!ah)
15020 			continue;
15021 
15022 		ath12k_mac_hw_unregister(ah);
15023 	}
15024 }
15025 
ath12k_mac_hw_destroy(struct ath12k_hw * ah)15026 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15027 {
15028 	ieee80211_free_hw(ah->hw);
15029 }
15030 
ath12k_mac_hw_allocate(struct ath12k_hw_group * ag,struct ath12k_pdev_map * pdev_map,u8 num_pdev_map)15031 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15032 						struct ath12k_pdev_map *pdev_map,
15033 						u8 num_pdev_map)
15034 {
15035 	struct ieee80211_hw *hw;
15036 	struct ath12k *ar;
15037 	struct ath12k_base *ab;
15038 	struct ath12k_pdev *pdev;
15039 	struct ath12k_hw *ah;
15040 	int i;
15041 	u8 pdev_idx;
15042 
15043 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15044 				pdev_map->ab->ath12k_ops);
15045 	if (!hw)
15046 		return NULL;
15047 
15048 	ah = ath12k_hw_to_ah(hw);
15049 	ah->hw = hw;
15050 	ah->num_radio = num_pdev_map;
15051 
15052 	mutex_init(&ah->hw_mutex);
15053 
15054 	spin_lock_init(&ah->dp_hw.peer_lock);
15055 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15056 
15057 	for (i = 0; i < num_pdev_map; i++) {
15058 		ab = pdev_map[i].ab;
15059 		pdev_idx = pdev_map[i].pdev_idx;
15060 		pdev = &ab->pdevs[pdev_idx];
15061 
15062 		ar = ath12k_ah_to_ar(ah, i);
15063 		ar->ah = ah;
15064 		ar->ab = ab;
15065 		ar->hw_link_id = pdev->hw_link_id;
15066 		ar->pdev = pdev;
15067 		ar->pdev_idx = pdev_idx;
15068 		ar->radio_idx = i;
15069 		pdev->ar = ar;
15070 
15071 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15072 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15073 
15074 		ath12k_mac_setup(ar);
15075 		ath12k_dp_pdev_pre_alloc(ar);
15076 	}
15077 
15078 	return ah;
15079 }
15080 
ath12k_mac_destroy(struct ath12k_hw_group * ag)15081 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15082 {
15083 	struct ath12k_pdev *pdev;
15084 	struct ath12k_base *ab = ag->ab[0];
15085 	int i, j;
15086 	struct ath12k_hw *ah;
15087 
15088 	for (i = 0; i < ag->num_devices; i++) {
15089 		ab = ag->ab[i];
15090 		if (!ab)
15091 			continue;
15092 
15093 		for (j = 0; j < ab->num_radios; j++) {
15094 			pdev = &ab->pdevs[j];
15095 			if (!pdev->ar)
15096 				continue;
15097 			pdev->ar = NULL;
15098 		}
15099 	}
15100 
15101 	for (i = 0; i < ag->num_hw; i++) {
15102 		ah = ath12k_ag_to_ah(ag, i);
15103 		if (!ah)
15104 			continue;
15105 
15106 		ath12k_mac_hw_destroy(ah);
15107 		ath12k_ag_set_ah(ag, i, NULL);
15108 	}
15109 }
15110 
ath12k_mac_set_device_defaults(struct ath12k_base * ab)15111 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15112 {
15113 	int total_vdev;
15114 
15115 	/* Initialize channel counters frequency value in hertz */
15116 	ab->cc_freq_hz = 320000;
15117 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15118 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15119 }
15120 
ath12k_mac_allocate(struct ath12k_hw_group * ag)15121 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15122 {
15123 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15124 	int mac_id, device_id, total_radio, num_hw;
15125 	struct ath12k_base *ab;
15126 	struct ath12k_hw *ah;
15127 	int ret, i, j;
15128 	u8 radio_per_hw;
15129 
15130 	total_radio = 0;
15131 	for (i = 0; i < ag->num_devices; i++) {
15132 		ab = ag->ab[i];
15133 		if (!ab)
15134 			continue;
15135 
15136 		ath12k_mac_set_device_defaults(ab);
15137 		total_radio += ab->num_radios;
15138 	}
15139 
15140 	if (!total_radio)
15141 		return -EINVAL;
15142 
15143 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15144 		return -ENOSPC;
15145 
15146 	/* All pdev get combined and register as single wiphy based on
15147 	 * hardware group which participate in multi-link operation else
15148 	 * each pdev get register separately.
15149 	 */
15150 	if (ag->mlo_capable)
15151 		radio_per_hw = total_radio;
15152 	else
15153 		radio_per_hw = 1;
15154 
15155 	num_hw = total_radio / radio_per_hw;
15156 
15157 	ag->num_hw = 0;
15158 	device_id = 0;
15159 	mac_id = 0;
15160 	for (i = 0; i < num_hw; i++) {
15161 		for (j = 0; j < radio_per_hw; j++) {
15162 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15163 				ret = -ENOSPC;
15164 				goto err;
15165 			}
15166 
15167 			ab = ag->ab[device_id];
15168 			pdev_map[j].ab = ab;
15169 			pdev_map[j].pdev_idx = mac_id;
15170 			mac_id++;
15171 
15172 			/* If mac_id falls beyond the current device MACs then
15173 			 * move to next device
15174 			 */
15175 			if (mac_id >= ab->num_radios) {
15176 				mac_id = 0;
15177 				device_id++;
15178 			}
15179 		}
15180 
15181 		ab = pdev_map->ab;
15182 
15183 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15184 		if (!ah) {
15185 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15186 				    i);
15187 			ret = -ENOMEM;
15188 			goto err;
15189 		}
15190 
15191 		ah->dev = ab->dev;
15192 
15193 		ag->ah[i] = ah;
15194 		ag->num_hw++;
15195 	}
15196 
15197 	return 0;
15198 
15199 err:
15200 	for (i = i - 1; i >= 0; i--) {
15201 		ah = ath12k_ag_to_ah(ag, i);
15202 		if (!ah)
15203 			continue;
15204 
15205 		ath12k_mac_hw_destroy(ah);
15206 		ath12k_ag_set_ah(ag, i, NULL);
15207 	}
15208 
15209 	return ret;
15210 }
15211 
ath12k_mac_vif_set_keepalive(struct ath12k_link_vif * arvif,enum wmi_sta_keepalive_method method,u32 interval)15212 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15213 				 enum wmi_sta_keepalive_method method,
15214 				 u32 interval)
15215 {
15216 	struct wmi_sta_keepalive_arg arg = {};
15217 	struct ath12k *ar = arvif->ar;
15218 	int ret;
15219 
15220 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15221 
15222 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15223 		return 0;
15224 
15225 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15226 		return 0;
15227 
15228 	arg.vdev_id = arvif->vdev_id;
15229 	arg.enabled = 1;
15230 	arg.method = method;
15231 	arg.interval = interval;
15232 
15233 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15234 	if (ret) {
15235 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15236 			    arvif->vdev_id, ret);
15237 		return ret;
15238 	}
15239 
15240 	return 0;
15241 }
15242