xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision 68993ced0f618e36cf33388f1e50223e5e6e78cc)
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 	lockdep_assert_in_rcu_read_lock();
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 ((vif->type == NL80211_IFTYPE_AP ||
3450 		     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3451 		    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3452 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3453 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3454 
3455 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3456 					       bw_20->rx_tx_mcs9_max_nss,
3457 					       bw_20->rx_tx_mcs11_max_nss,
3458 					       bw_20->rx_tx_mcs13_max_nss,
3459 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3460 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3461 					       eht_mcs_mask);
3462 		} else {
3463 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3464 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3465 					       bw->rx_tx_mcs9_max_nss,
3466 					       bw->rx_tx_mcs11_max_nss,
3467 					       bw->rx_tx_mcs13_max_nss,
3468 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3469 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3470 					       eht_mcs_mask);
3471 		}
3472 
3473 		arg->peer_eht_mcs_count++;
3474 		break;
3475 	}
3476 
3477 	arg->punct_bitmap = ~arvif->punct_bitmap;
3478 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3479 
3480 	if ((vif->type == NL80211_IFTYPE_AP ||
3481 	     vif->type == NL80211_IFTYPE_MESH_POINT) &&
3482 	    !(link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3483 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) {
3484 		if (bw_20->rx_tx_mcs13_max_nss)
3485 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs13_max_nss,
3486 							   IEEE80211_EHT_MCS_NSS_RX));
3487 		if (bw_20->rx_tx_mcs11_max_nss)
3488 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs11_max_nss,
3489 							   IEEE80211_EHT_MCS_NSS_RX));
3490 		if (bw_20->rx_tx_mcs9_max_nss)
3491 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs9_max_nss,
3492 							   IEEE80211_EHT_MCS_NSS_RX));
3493 		if (bw_20->rx_tx_mcs7_max_nss)
3494 			max_nss = max(max_nss, u8_get_bits(bw_20->rx_tx_mcs7_max_nss,
3495 							   IEEE80211_EHT_MCS_NSS_RX));
3496 	} else {
3497 		if (bw->rx_tx_mcs13_max_nss)
3498 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs13_max_nss,
3499 							   IEEE80211_EHT_MCS_NSS_RX));
3500 		if (bw->rx_tx_mcs11_max_nss)
3501 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs11_max_nss,
3502 							   IEEE80211_EHT_MCS_NSS_RX));
3503 		if (bw->rx_tx_mcs9_max_nss)
3504 			max_nss = max(max_nss, u8_get_bits(bw->rx_tx_mcs9_max_nss,
3505 							   IEEE80211_EHT_MCS_NSS_RX));
3506 	}
3507 
3508 	max_nss = min(max_nss, (uint8_t)eht_nss);
3509 
3510 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
3511 
3512 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3513 		   "mac eht peer %pM nss %d mcs cnt %d ru_punct_bitmap 0x%x\n",
3514 		   arsta->addr, arg->peer_nss, arg->peer_eht_mcs_count,
3515 		   arg->punct_bitmap);
3516 }
3517 
ath12k_peer_assoc_h_mlo(struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)3518 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3519 				    struct ath12k_wmi_peer_assoc_arg *arg)
3520 {
3521 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3522 	struct peer_assoc_mlo_params *ml = &arg->ml;
3523 	struct ath12k_sta *ahsta = arsta->ahsta;
3524 	struct ath12k_link_sta *arsta_p;
3525 	struct ath12k_link_vif *arvif;
3526 	unsigned long links;
3527 	u8 link_id;
3528 	int i;
3529 
3530 	if (!sta->mlo || ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3531 		return;
3532 
3533 	ml->enabled = true;
3534 	ml->assoc_link = arsta->is_assoc_link;
3535 
3536 	/* For now considering the primary umac based on assoc link */
3537 	ml->primary_umac = arsta->is_assoc_link;
3538 	ml->peer_id_valid = true;
3539 	ml->logical_link_idx_valid = true;
3540 
3541 	ether_addr_copy(ml->mld_addr, sta->addr);
3542 	ml->logical_link_idx = arsta->link_idx;
3543 	ml->ml_peer_id = ahsta->ml_peer_id;
3544 	ml->ieee_link_id = arsta->link_id;
3545 	ml->num_partner_links = 0;
3546 	ml->eml_cap = sta->eml_cap;
3547 	links = ahsta->links_map;
3548 
3549 	rcu_read_lock();
3550 
3551 	i = 0;
3552 
3553 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3554 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3555 			break;
3556 
3557 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3558 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3559 
3560 		if (arsta_p == arsta)
3561 			continue;
3562 
3563 		if (!arvif->is_started)
3564 			continue;
3565 
3566 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3567 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3568 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3569 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3570 		ml->partner_info[i].logical_link_idx_valid = true;
3571 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3572 		ml->num_partner_links++;
3573 
3574 		i++;
3575 	}
3576 
3577 	rcu_read_unlock();
3578 }
3579 
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)3580 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3581 				      struct ath12k_link_vif *arvif,
3582 				      struct ath12k_link_sta *arsta,
3583 				      struct ath12k_wmi_peer_assoc_arg *arg,
3584 				      bool reassoc)
3585 {
3586 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3587 
3588 	memset(arg, 0, sizeof(*arg));
3589 
3590 	reinit_completion(&ar->peer_assoc_done);
3591 
3592 	arg->peer_new_assoc = !reassoc;
3593 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3594 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3595 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3596 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3597 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3598 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3599 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3600 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3601 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3602 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3603 	ath12k_peer_assoc_h_smps(arsta, arg);
3604 	ath12k_peer_assoc_h_mlo(arsta, arg);
3605 
3606 	arsta->peer_nss = arg->peer_nss;
3607 	/* TODO: amsdu_disable req? */
3608 }
3609 
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)3610 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3611 				  const u8 *addr,
3612 				  const struct ieee80211_sta_ht_cap *ht_cap,
3613 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3614 {
3615 	int smps, ret = 0;
3616 
3617 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3618 		return 0;
3619 
3620 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3621 	if (ret < 0)
3622 		return ret;
3623 
3624 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3625 					 WMI_PEER_MIMO_PS_STATE,
3626 					 ath12k_smps_map[smps]);
3627 }
3628 
ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif * arvif)3629 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3630 {
3631 	struct ath12k_vif *ahvif = arvif->ahvif;
3632 	struct ath12k *ar = arvif->ar;
3633 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3634 	u32 value = 0;
3635 	int ret;
3636 	struct ieee80211_bss_conf *link_conf;
3637 
3638 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3639 	if (!link_conf) {
3640 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3641 		return -EINVAL;
3642 	}
3643 
3644 	if (!link_conf->he_support)
3645 		return 0;
3646 
3647 	if (link_conf->he_su_beamformer) {
3648 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3649 		if (link_conf->he_mu_beamformer &&
3650 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3651 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3652 	}
3653 
3654 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3655 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3656 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3657 
3658 		if (link_conf->he_full_ul_mumimo)
3659 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3660 
3661 		if (link_conf->he_su_beamformee)
3662 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3663 	}
3664 
3665 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3666 	if (ret) {
3667 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3668 			    arvif->vdev_id, ret);
3669 		return ret;
3670 	}
3671 
3672 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3673 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3674 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3675 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3676 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3677 					    param, value);
3678 	if (ret) {
3679 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3680 			    arvif->vdev_id, ret);
3681 		return ret;
3682 	}
3683 
3684 	return 0;
3685 }
3686 
ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_sta_he_cap * he_cap,int * hemode)3687 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3688 					     struct ath12k_link_vif *arvif,
3689 					     struct ieee80211_sta_he_cap *he_cap,
3690 					     int *hemode)
3691 {
3692 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3693 	struct ieee80211_he_cap_elem he_cap_elem = {};
3694 	struct ieee80211_sta_he_cap *cap_band;
3695 	struct cfg80211_chan_def def;
3696 	u8 link_id = arvif->link_id;
3697 	struct ieee80211_bss_conf *link_conf;
3698 
3699 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3700 	if (!link_conf) {
3701 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3702 		return -EINVAL;
3703 	}
3704 
3705 	if (!link_conf->he_support)
3706 		return 0;
3707 
3708 	if (vif->type != NL80211_IFTYPE_STATION)
3709 		return -EINVAL;
3710 
3711 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3712 		return -EINVAL;
3713 
3714 	if (def.chan->band == NL80211_BAND_2GHZ)
3715 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3716 	else
3717 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3718 
3719 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3720 
3721 	*hemode = 0;
3722 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3723 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3724 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3725 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3726 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3727 	}
3728 
3729 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3730 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3731 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3732 
3733 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3734 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3735 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3736 							  HE_MODE_UL_MUMIMO);
3737 
3738 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3739 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3740 
3741 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3742 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3743 	}
3744 
3745 	return 0;
3746 }
3747 
ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif * arvif)3748 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3749 {
3750 	struct ath12k_vif *ahvif = arvif->ahvif;
3751 	struct ath12k *ar = arvif->ar;
3752 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3753 	u32 value = 0;
3754 	int ret;
3755 	struct ieee80211_bss_conf *link_conf;
3756 
3757 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3758 	if (!link_conf) {
3759 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3760 		return -ENOENT;
3761 	}
3762 
3763 	if (!link_conf->eht_support)
3764 		return 0;
3765 
3766 	if (link_conf->eht_su_beamformer) {
3767 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3768 		if (link_conf->eht_mu_beamformer &&
3769 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3770 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3771 						 EHT_MODE_MU_TX_BFER) |
3772 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3773 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3774 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3775 						 EHT_MODE_UL_OFDMA_MUMIMO);
3776 	}
3777 
3778 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3779 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3780 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3781 
3782 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3783 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3784 
3785 		if (link_conf->eht_su_beamformee)
3786 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3787 						 EHT_MODE_SU_TX_BFEE);
3788 	}
3789 
3790 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3791 	if (ret) {
3792 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3793 			    arvif->vdev_id, ret);
3794 		return ret;
3795 	}
3796 
3797 	return 0;
3798 }
3799 
ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k * ar,struct ieee80211_link_sta * link_sta)3800 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3801 					      struct ieee80211_link_sta *link_sta)
3802 {
3803 	u32 bw;
3804 
3805 	switch (link_sta->bandwidth) {
3806 	case IEEE80211_STA_RX_BW_20:
3807 		bw = WMI_PEER_CHWIDTH_20MHZ;
3808 		break;
3809 	case IEEE80211_STA_RX_BW_40:
3810 		bw = WMI_PEER_CHWIDTH_40MHZ;
3811 		break;
3812 	case IEEE80211_STA_RX_BW_80:
3813 		bw = WMI_PEER_CHWIDTH_80MHZ;
3814 		break;
3815 	case IEEE80211_STA_RX_BW_160:
3816 		bw = WMI_PEER_CHWIDTH_160MHZ;
3817 		break;
3818 	case IEEE80211_STA_RX_BW_320:
3819 		bw = WMI_PEER_CHWIDTH_320MHZ;
3820 		break;
3821 	default:
3822 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3823 			    link_sta->bandwidth, link_sta->addr);
3824 		bw = WMI_PEER_CHWIDTH_20MHZ;
3825 		break;
3826 	}
3827 
3828 	return bw;
3829 }
3830 
ath12k_bss_assoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * bss_conf)3831 static void ath12k_bss_assoc(struct ath12k *ar,
3832 			     struct ath12k_link_vif *arvif,
3833 			     struct ieee80211_bss_conf *bss_conf)
3834 {
3835 	struct ath12k_vif *ahvif = arvif->ahvif;
3836 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3837 	struct ath12k_wmi_vdev_up_params params = {};
3838 	struct ieee80211_link_sta *link_sta;
3839 	u8 link_id = bss_conf->link_id;
3840 	struct ath12k_link_sta *arsta;
3841 	struct ieee80211_sta *ap_sta;
3842 	struct ath12k_sta *ahsta;
3843 	struct ath12k_dp_link_peer *peer;
3844 	bool is_auth = false;
3845 	u32 hemode = 0;
3846 	int ret;
3847 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
3848 
3849 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3850 
3851 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3852 					kzalloc_obj(*peer_arg);
3853 	if (!peer_arg)
3854 		return;
3855 
3856 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3857 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3858 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3859 
3860 	rcu_read_lock();
3861 
3862 	/* During ML connection, cfg.ap_addr has the MLD address. For
3863 	 * non-ML connection, it has the BSSID.
3864 	 */
3865 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3866 	if (!ap_sta) {
3867 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3868 			    vif->cfg.ap_addr, arvif->vdev_id);
3869 		rcu_read_unlock();
3870 		return;
3871 	}
3872 
3873 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3874 
3875 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3876 				  ahsta->link[link_id]);
3877 	if (WARN_ON(!arsta)) {
3878 		rcu_read_unlock();
3879 		return;
3880 	}
3881 
3882 	link_sta = ath12k_mac_get_link_sta(arsta);
3883 	if (WARN_ON(!link_sta)) {
3884 		rcu_read_unlock();
3885 		return;
3886 	}
3887 
3888 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3889 
3890 	/* link_sta->he_cap must be protected by rcu_read_lock */
3891 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3892 	if (ret) {
3893 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3894 			    arvif->vdev_id, bss_conf->bssid, ret);
3895 		rcu_read_unlock();
3896 		return;
3897 	}
3898 
3899 	rcu_read_unlock();
3900 
3901 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3902 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3903 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3904 	if (ret) {
3905 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3906 			    hemode, ret);
3907 		return;
3908 	}
3909 
3910 	peer_arg->is_assoc = true;
3911 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3912 	if (ret) {
3913 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3914 			    bss_conf->bssid, arvif->vdev_id, ret);
3915 		return;
3916 	}
3917 
3918 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3919 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3920 			    bss_conf->bssid, arvif->vdev_id);
3921 		return;
3922 	}
3923 
3924 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3925 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3926 	if (ret) {
3927 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3928 			    arvif->vdev_id, ret);
3929 		return;
3930 	}
3931 
3932 	WARN_ON(arvif->is_up);
3933 
3934 	ahvif->aid = vif->cfg.aid;
3935 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3936 
3937 	params.vdev_id = arvif->vdev_id;
3938 	params.aid = ahvif->aid;
3939 	params.bssid = arvif->bssid;
3940 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
3941 	if (params.tx_bssid) {
3942 		params.nontx_profile_idx = bss_conf->bssid_index;
3943 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
3944 	}
3945 	ret = ath12k_wmi_vdev_up(ar, &params);
3946 	if (ret) {
3947 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3948 			    arvif->vdev_id, ret);
3949 		return;
3950 	}
3951 
3952 	arvif->is_up = true;
3953 	arvif->rekey_data.enable_offload = false;
3954 
3955 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3956 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3957 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3958 
3959 	spin_lock_bh(&dp->dp_lock);
3960 
3961 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
3962 							 arvif->bssid);
3963 	if (peer && peer->is_authorized)
3964 		is_auth = true;
3965 
3966 	spin_unlock_bh(&dp->dp_lock);
3967 
3968 	/* Authorize BSS Peer */
3969 	if (is_auth) {
3970 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
3971 						arvif->vdev_id,
3972 						WMI_PEER_AUTHORIZE,
3973 						1);
3974 		if (ret)
3975 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3976 	}
3977 
3978 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3979 					   &bss_conf->he_obss_pd);
3980 	if (ret)
3981 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3982 			    arvif->vdev_id, ret);
3983 
3984 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
3985 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
3986 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
3987 		ath12k_mac_11d_scan_stop_all(ar->ab);
3988 }
3989 
ath12k_bss_disassoc(struct ath12k * ar,struct ath12k_link_vif * arvif)3990 static void ath12k_bss_disassoc(struct ath12k *ar,
3991 				struct ath12k_link_vif *arvif)
3992 {
3993 	int ret;
3994 
3995 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3996 
3997 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3998 		   arvif->vdev_id, arvif->bssid);
3999 
4000 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
4001 	if (ret)
4002 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
4003 			    arvif->vdev_id, ret);
4004 
4005 	arvif->is_up = false;
4006 
4007 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
4008 
4009 	cancel_delayed_work(&arvif->connection_loss_work);
4010 }
4011 
ath12k_mac_get_rate_hw_value(int bitrate)4012 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
4013 {
4014 	u32 preamble;
4015 	u16 hw_value;
4016 	int rate;
4017 	size_t i;
4018 
4019 	if (ath12k_mac_bitrate_is_cck(bitrate))
4020 		preamble = WMI_RATE_PREAMBLE_CCK;
4021 	else
4022 		preamble = WMI_RATE_PREAMBLE_OFDM;
4023 
4024 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
4025 		if (ath12k_legacy_rates[i].bitrate != bitrate)
4026 			continue;
4027 
4028 		hw_value = ath12k_legacy_rates[i].hw_value;
4029 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4030 
4031 		return rate;
4032 	}
4033 
4034 	return -EINVAL;
4035 }
4036 
ath12k_recalculate_mgmt_rate(struct ath12k * ar,struct ath12k_link_vif * arvif,struct cfg80211_chan_def * def)4037 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
4038 					 struct ath12k_link_vif *arvif,
4039 					 struct cfg80211_chan_def *def)
4040 {
4041 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4042 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4043 	const struct ieee80211_supported_band *sband;
4044 	struct ieee80211_bss_conf *bss_conf;
4045 	u8 basic_rate_idx;
4046 	int hw_rate_code;
4047 	u32 vdev_param;
4048 	u16 bitrate;
4049 	int ret;
4050 
4051 	lockdep_assert_wiphy(hw->wiphy);
4052 
4053 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
4054 	if (!bss_conf) {
4055 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
4056 			    vif->addr, arvif->link_id);
4057 		return;
4058 	}
4059 
4060 	sband = hw->wiphy->bands[def->chan->band];
4061 	if (bss_conf->basic_rates)
4062 		basic_rate_idx = __ffs(bss_conf->basic_rates);
4063 	else
4064 		basic_rate_idx = 0;
4065 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
4066 
4067 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
4068 	if (hw_rate_code < 0) {
4069 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
4070 		return;
4071 	}
4072 
4073 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
4074 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4075 					    hw_rate_code);
4076 	if (ret)
4077 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
4078 
4079 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
4080 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
4081 					    hw_rate_code);
4082 	if (ret)
4083 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
4084 }
4085 
ath12k_mac_bcn_tx_event(struct ath12k_link_vif * arvif)4086 static void ath12k_mac_bcn_tx_event(struct ath12k_link_vif *arvif)
4087 {
4088 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4089 	struct ieee80211_bss_conf *link_conf;
4090 
4091 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
4092 	if (!link_conf) {
4093 		ath12k_warn(arvif->ar->ab, "failed to get link conf for vdev %u\n",
4094 			    arvif->vdev_id);
4095 		return;
4096 	}
4097 
4098 	if (link_conf->color_change_active) {
4099 		if (ieee80211_beacon_cntdwn_is_complete(vif, arvif->link_id)) {
4100 			ieee80211_color_change_finish(vif, arvif->link_id);
4101 			return;
4102 		}
4103 
4104 		ieee80211_beacon_update_cntdwn(vif, arvif->link_id);
4105 		ath12k_mac_setup_bcn_tmpl(arvif);
4106 	}
4107 }
4108 
ath12k_mac_bcn_tx_work(struct wiphy * wiphy,struct wiphy_work * work)4109 static void ath12k_mac_bcn_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
4110 {
4111 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
4112 						     bcn_tx_work);
4113 
4114 	lockdep_assert_wiphy(wiphy);
4115 	ath12k_mac_bcn_tx_event(arvif);
4116 }
4117 
ath12k_mac_init_arvif(struct ath12k_vif * ahvif,struct ath12k_link_vif * arvif,int link_id)4118 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
4119 				  struct ath12k_link_vif *arvif, int link_id)
4120 {
4121 	struct ath12k_hw *ah = ahvif->ah;
4122 	u8 _link_id;
4123 	int i;
4124 
4125 	lockdep_assert_wiphy(ah->hw->wiphy);
4126 
4127 	if (WARN_ON(!arvif))
4128 		return;
4129 
4130 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
4131 		return;
4132 
4133 	if (link_id < 0)
4134 		_link_id = 0;
4135 	else
4136 		_link_id = link_id;
4137 
4138 	arvif->ahvif = ahvif;
4139 	arvif->link_id = _link_id;
4140 
4141 	/* Protects the datapath stats update on a per link basis */
4142 	spin_lock_init(&arvif->link_stats_lock);
4143 
4144 	INIT_LIST_HEAD(&arvif->list);
4145 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
4146 			  ath12k_mac_vif_sta_connection_loss_work);
4147 	wiphy_work_init(&arvif->bcn_tx_work, ath12k_mac_bcn_tx_work);
4148 
4149 	arvif->num_stations = 0;
4150 
4151 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4152 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4153 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
4154 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4155 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4156 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4157 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4158 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
4159 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
4160 		memset(arvif->bitrate_mask.control[i].eht_mcs, 0xff,
4161 		       sizeof(arvif->bitrate_mask.control[i].eht_mcs));
4162 	}
4163 
4164 	/* Handle MLO related assignments */
4165 	if (link_id >= 0) {
4166 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
4167 		ahvif->links_map |= BIT(_link_id);
4168 	}
4169 
4170 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4171 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
4172 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
4173 			   ahvif->links_map);
4174 }
4175 
ath12k_mac_remove_link_interface(struct ieee80211_hw * hw,struct ath12k_link_vif * arvif)4176 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
4177 					     struct ath12k_link_vif *arvif)
4178 {
4179 	struct ath12k_vif *ahvif = arvif->ahvif;
4180 	struct ath12k_hw *ah = hw->priv;
4181 	struct ath12k *ar = arvif->ar;
4182 	int ret;
4183 
4184 	lockdep_assert_wiphy(ah->hw->wiphy);
4185 
4186 	cancel_delayed_work_sync(&arvif->connection_loss_work);
4187 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &arvif->bcn_tx_work);
4188 
4189 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
4190 		   arvif->vdev_id, arvif->link_id);
4191 
4192 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
4193 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
4194 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
4195 		ath12k_mac_11d_scan_stop(ar);
4196 
4197 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
4198 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
4199 		if (ret)
4200 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
4201 				    arvif->vdev_id, arvif->link_id, ret);
4202 
4203 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS)
4204 			ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
4205 	}
4206 	ath12k_mac_vdev_delete(ar, arvif);
4207 }
4208 
ath12k_mac_assign_link_vif(struct ath12k_hw * ah,struct ieee80211_vif * vif,u8 link_id)4209 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
4210 							  struct ieee80211_vif *vif,
4211 							  u8 link_id)
4212 {
4213 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4214 	struct ath12k_link_vif *arvif;
4215 
4216 	lockdep_assert_wiphy(ah->hw->wiphy);
4217 
4218 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4219 	if (arvif)
4220 		return arvif;
4221 
4222 	/* If this is the first link arvif being created for an ML VIF
4223 	 * use the preallocated deflink memory except for scan arvifs
4224 	 */
4225 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
4226 		arvif = &ahvif->deflink;
4227 
4228 		if (vif->type == NL80211_IFTYPE_STATION)
4229 			arvif->is_sta_assoc_link = true;
4230 	} else {
4231 		arvif = kzalloc_obj(*arvif);
4232 		if (!arvif)
4233 			return NULL;
4234 	}
4235 
4236 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
4237 
4238 	return arvif;
4239 }
4240 
ath12k_mac_unassign_link_vif(struct ath12k_link_vif * arvif)4241 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
4242 {
4243 	struct ath12k_vif *ahvif = arvif->ahvif;
4244 	struct ath12k_hw *ah = ahvif->ah;
4245 
4246 	lockdep_assert_wiphy(ah->hw->wiphy);
4247 
4248 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
4249 	synchronize_rcu();
4250 	ahvif->links_map &= ~BIT(arvif->link_id);
4251 
4252 	if (arvif != &ahvif->deflink)
4253 		kfree(arvif);
4254 	else
4255 		memset(arvif, 0, sizeof(*arvif));
4256 }
4257 
4258 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])4259 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
4260 			       struct ieee80211_vif *vif,
4261 			       u16 old_links, u16 new_links,
4262 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
4263 {
4264 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4265 	unsigned long to_remove = old_links & ~new_links;
4266 	unsigned long to_add = ~old_links & new_links;
4267 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
4268 	struct ath12k_link_vif *arvif;
4269 	u8 link_id;
4270 
4271 	lockdep_assert_wiphy(hw->wiphy);
4272 
4273 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4274 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4275 			   vif->addr, old_links, new_links);
4276 
4277 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4278 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4279 		/* mac80211 wants to add link but driver already has the
4280 		 * link. This should not happen ideally.
4281 		 */
4282 		if (WARN_ON(arvif))
4283 			return -EINVAL;
4284 
4285 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4286 		if (WARN_ON(!arvif))
4287 			return -EINVAL;
4288 	}
4289 
4290 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4291 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4292 		if (WARN_ON(!arvif))
4293 			return -EINVAL;
4294 
4295 		if (!arvif->is_created) {
4296 			ath12k_mac_unassign_link_vif(arvif);
4297 			continue;
4298 		}
4299 
4300 		if (WARN_ON(!arvif->ar))
4301 			return -EINVAL;
4302 
4303 		ath12k_mac_remove_link_interface(hw, arvif);
4304 		ath12k_mac_unassign_link_vif(arvif);
4305 	}
4306 
4307 	return 0;
4308 }
4309 EXPORT_SYMBOL(ath12k_mac_op_change_vif_links);
4310 
ath12k_mac_fils_discovery(struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info)4311 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4312 				     struct ieee80211_bss_conf *info)
4313 {
4314 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4315 	struct ath12k *ar = arvif->ar;
4316 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4317 	struct sk_buff *tmpl;
4318 	int ret;
4319 	u32 interval;
4320 	bool unsol_bcast_probe_resp_enabled = false;
4321 
4322 	if (info->fils_discovery.max_interval) {
4323 		interval = info->fils_discovery.max_interval;
4324 
4325 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif,
4326 							 info->link_id);
4327 		if (tmpl)
4328 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4329 							     tmpl);
4330 	} else if (info->unsol_bcast_probe_resp_interval) {
4331 		unsol_bcast_probe_resp_enabled = 1;
4332 		interval = info->unsol_bcast_probe_resp_interval;
4333 
4334 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif,
4335 								 info->link_id);
4336 		if (tmpl)
4337 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4338 							 tmpl);
4339 	} else { /* Disable */
4340 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4341 	}
4342 
4343 	if (!tmpl) {
4344 		ath12k_warn(ar->ab,
4345 			    "mac vdev %i failed to retrieve %s template\n",
4346 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4347 			    "unsolicited broadcast probe response" :
4348 			    "FILS discovery"));
4349 		return -EPERM;
4350 	}
4351 	kfree_skb(tmpl);
4352 
4353 	if (!ret)
4354 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4355 						unsol_bcast_probe_resp_enabled);
4356 
4357 	return ret;
4358 }
4359 
ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 changed)4360 void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4361 				   struct ieee80211_vif *vif,
4362 				   u64 changed)
4363 {
4364 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4365 	unsigned long links = ahvif->links_map;
4366 	struct ieee80211_bss_conf *info;
4367 	struct ath12k_link_vif *arvif;
4368 	struct ieee80211_sta *sta;
4369 	struct ath12k_sta *ahsta;
4370 	struct ath12k *ar;
4371 	u8 link_id;
4372 
4373 	lockdep_assert_wiphy(hw->wiphy);
4374 
4375 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4376 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4377 		if (vif->cfg.ssid_len)
4378 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4379 	}
4380 
4381 	if (changed & BSS_CHANGED_ASSOC) {
4382 		if (vif->cfg.assoc) {
4383 			/* only in station mode we can get here, so it's safe
4384 			 * to use ap_addr
4385 			 */
4386 			rcu_read_lock();
4387 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4388 			if (!sta) {
4389 				rcu_read_unlock();
4390 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4391 					  vif->cfg.ap_addr);
4392 				return;
4393 			}
4394 
4395 			ahsta = ath12k_sta_to_ahsta(sta);
4396 			arvif = wiphy_dereference(hw->wiphy,
4397 						  ahvif->link[ahsta->assoc_link_id]);
4398 			rcu_read_unlock();
4399 
4400 			ar = arvif->ar;
4401 			/* there is no reason for which an assoc link's
4402 			 * bss info does not exist
4403 			 */
4404 			info = ath12k_mac_get_link_bss_conf(arvif);
4405 			ath12k_bss_assoc(ar, arvif, info);
4406 
4407 			/* exclude assoc link as it is done above */
4408 			links &= ~BIT(ahsta->assoc_link_id);
4409 		}
4410 
4411 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4412 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4413 			if (!arvif || !arvif->ar)
4414 				continue;
4415 
4416 			ar = arvif->ar;
4417 
4418 			if (vif->cfg.assoc) {
4419 				info = ath12k_mac_get_link_bss_conf(arvif);
4420 				if (!info)
4421 					continue;
4422 
4423 				ath12k_bss_assoc(ar, arvif, info);
4424 			} else {
4425 				ath12k_bss_disassoc(ar, arvif);
4426 			}
4427 		}
4428 	}
4429 }
4430 EXPORT_SYMBOL(ath12k_mac_op_vif_cfg_changed);
4431 
ath12k_mac_vif_setup_ps(struct ath12k_link_vif * arvif)4432 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4433 {
4434 	struct ath12k *ar = arvif->ar;
4435 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4436 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4437 	enum wmi_sta_powersave_param param;
4438 	struct ieee80211_bss_conf *info;
4439 	enum wmi_sta_ps_mode psmode;
4440 	int ret;
4441 	int timeout;
4442 	bool enable_ps;
4443 
4444 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4445 
4446 	if (vif->type != NL80211_IFTYPE_STATION)
4447 		return;
4448 
4449 	enable_ps = arvif->ahvif->ps;
4450 	if (enable_ps) {
4451 		psmode = WMI_STA_PS_MODE_ENABLED;
4452 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4453 
4454 		timeout = conf->dynamic_ps_timeout;
4455 		if (timeout == 0) {
4456 			info = ath12k_mac_get_link_bss_conf(arvif);
4457 			if (!info) {
4458 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4459 					    vif->addr, arvif->link_id);
4460 				return;
4461 			}
4462 
4463 			/* firmware doesn't like 0 */
4464 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4465 		}
4466 
4467 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4468 						  timeout);
4469 		if (ret) {
4470 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4471 				    arvif->vdev_id, ret);
4472 			return;
4473 		}
4474 	} else {
4475 		psmode = WMI_STA_PS_MODE_DISABLED;
4476 	}
4477 
4478 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4479 		   arvif->vdev_id, psmode ? "enable" : "disable");
4480 
4481 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4482 	if (ret)
4483 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4484 			    psmode, arvif->vdev_id, ret);
4485 }
4486 
ath12k_mac_supports_tpc(struct ath12k * ar,struct ath12k_vif * ahvif,const struct cfg80211_chan_def * chandef)4487 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4488 				    const struct cfg80211_chan_def *chandef)
4489 {
4490 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4491 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4492 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4493 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4494 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4495 		chandef->chan &&
4496 		chandef->chan->band == NL80211_BAND_6GHZ;
4497 }
4498 
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)4499 static void ath12k_wmi_vdev_params_up(struct ath12k *ar,
4500 				      struct ath12k_link_vif *arvif,
4501 				      struct ath12k_link_vif *tx_arvif,
4502 				      struct ieee80211_bss_conf *info, u16 aid)
4503 {
4504 	struct ath12k_wmi_vdev_up_params params = {
4505 		.vdev_id = arvif->vdev_id,
4506 		.aid = aid,
4507 		.bssid = arvif->bssid
4508 	};
4509 	int ret;
4510 
4511 	if (tx_arvif) {
4512 		params.tx_bssid = tx_arvif->bssid;
4513 		params.nontx_profile_idx = info->bssid_index;
4514 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
4515 	}
4516 
4517 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
4518 	if (ret)
4519 		ath12k_warn(ar->ab, "failed to bring vdev up %d: %d\n",
4520 			    arvif->vdev_id, ret);
4521 }
4522 
ath12k_mac_config_obss_pd(struct ath12k_link_vif * arvif,const struct ieee80211_he_obss_pd * he_obss_pd)4523 static int ath12k_mac_config_obss_pd(struct ath12k_link_vif *arvif,
4524 				     const struct ieee80211_he_obss_pd *he_obss_pd)
4525 {
4526 	struct ath12k_wmi_obss_pd_arg obss_pd_arg = {};
4527 	u32 srg_bitmap[2], non_srg_bitmap[2];
4528 	struct ath12k *ar = arvif->ar;
4529 	u32 param_id, pdev_id;
4530 	u32 param_val;
4531 	int ret;
4532 
4533 	if (ar->ab->hw_params->single_pdev_only)
4534 		pdev_id = ath12k_mac_get_target_pdev_id_from_vif(arvif);
4535 	else
4536 		pdev_id = ar->pdev->pdev_id;
4537 
4538 	/* Set and enable SRG/non-SRG OBSS PD threshold */
4539 	param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
4540 	if (ar->monitor_started || !he_obss_pd->enable) {
4541 		ret = ath12k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
4542 		if (ret)
4543 			ath12k_warn(ar->ab,
4544 				    "failed to set OBSS PD threshold for pdev %u: %d\n",
4545 				    pdev_id, ret);
4546 		return ret;
4547 	}
4548 
4549 	/*
4550 	 * This service flag indicates firmware support for SRG/SRP-based
4551 	 * spatial reuse. It also specifies whether OBSS PD threshold values
4552 	 * should be interpreted as dB (offset) or dBm (absolute) units.
4553 	 */
4554 	obss_pd_arg.srp_support = test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
4555 					   ar->ab->wmi_ab.svc_map);
4556 
4557 	if (!(he_obss_pd->sr_ctrl &
4558 	      IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED)) {
4559 		if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
4560 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4561 						 he_obss_pd->non_srg_max_offset;
4562 		else
4563 			obss_pd_arg.non_srg_th = ATH12K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
4564 
4565 		if (!obss_pd_arg.srp_support)
4566 			obss_pd_arg.non_srg_th -= ATH12K_DEFAULT_NOISE_FLOOR;
4567 
4568 		obss_pd_arg.non_srg_enabled = true;
4569 	}
4570 
4571 	if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
4572 		obss_pd_arg.srg_th = ATH12K_OBSS_PD_MAX_THRESHOLD +
4573 				     he_obss_pd->max_offset;
4574 		obss_pd_arg.srg_enabled = true;
4575 	}
4576 
4577 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4578 		   "pdev %u OBSS PD sr_ctrl 0x%x srg_th %d dBm non_srg_th %d dBm\n",
4579 		   pdev_id, he_obss_pd->sr_ctrl,
4580 		   obss_pd_arg.srg_th, obss_pd_arg.non_srg_th);
4581 
4582 	param_val = ath12k_wmi_build_obss_pd(&obss_pd_arg);
4583 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4584 	if (ret) {
4585 		ath12k_warn(ar->ab,
4586 			    "failed to set OBSS PD threshold for pdev %u: %d\n",
4587 			    pdev_id, ret);
4588 		return ret;
4589 	}
4590 
4591 	/* Enable OBSS PD for all access category */
4592 	param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
4593 	param_val = 0xf;
4594 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4595 	if (ret) {
4596 		ath12k_warn(ar->ab,
4597 			    "failed to set OBSS PD per ac for pdev %u: %d\n",
4598 			    pdev_id, ret);
4599 		return ret;
4600 	}
4601 
4602 	/* Set SR prohibit */
4603 	param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
4604 	param_val = !!(he_obss_pd->sr_ctrl &
4605 		       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
4606 	ret = ath12k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
4607 	if (ret) {
4608 		ath12k_warn(ar->ab, "failed to set SR prohibit for pdev %u: %d\n",
4609 			    pdev_id, ret);
4610 		return ret;
4611 	}
4612 
4613 	if (!obss_pd_arg.srp_support)
4614 		return 0;
4615 
4616 	memcpy(srg_bitmap, he_obss_pd->bss_color_bitmap, sizeof(srg_bitmap));
4617 	/* Set SRG BSS color bitmap */
4618 	ret = ath12k_wmi_pdev_set_srg_bss_color_bitmap(ar, pdev_id, srg_bitmap);
4619 	if (ret) {
4620 		ath12k_warn(ar->ab,
4621 			    "failed to set SRG bss color bitmap for pdev %u: %d\n",
4622 			    pdev_id, ret);
4623 		return ret;
4624 	}
4625 
4626 	/* Enable BSS colors for SRG */
4627 	ret = ath12k_wmi_pdev_srg_obss_color_enable_bitmap(ar, pdev_id, srg_bitmap);
4628 	if (ret) {
4629 		ath12k_warn(ar->ab,
4630 			    "failed to enable SRG bss color bitmap pdev %u: %d\n",
4631 			    pdev_id, ret);
4632 		return ret;
4633 	}
4634 
4635 	memcpy(srg_bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(srg_bitmap));
4636 	/* Set SRG partial bssid bitmap */
4637 	ret = ath12k_wmi_pdev_set_srg_partial_bssid_bitmap(ar, pdev_id, srg_bitmap);
4638 	if (ret) {
4639 		ath12k_warn(ar->ab,
4640 			    "failed to set SRG partial bssid bitmap for pdev %u: %d\n",
4641 			    pdev_id, ret);
4642 		return ret;
4643 	}
4644 
4645 	/* Enable partial bssid mask for SRG */
4646 	ret = ath12k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, pdev_id, srg_bitmap);
4647 	if (ret) {
4648 		ath12k_warn(ar->ab,
4649 			    "failed to enable SRG bssid bitmap pdev %u: %d\n",
4650 			    pdev_id, ret);
4651 		return ret;
4652 	}
4653 
4654 	/*
4655 	 * No explicit non-SRG bitmap from mac80211; enable all colors/bssids
4656 	 * as non-SRG candidates. Actual SRG members are filtered by SRG bitmaps.
4657 	 */
4658 	memset(non_srg_bitmap, 0xff, sizeof(non_srg_bitmap));
4659 
4660 	/* Enable BSS colors for non-SRG */
4661 	ret = ath12k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, pdev_id,
4662 							       non_srg_bitmap);
4663 	if (ret) {
4664 		ath12k_warn(ar->ab,
4665 			    "failed to enable non SRG color bitmap pdev %u: %d\n",
4666 			    pdev_id, ret);
4667 		return ret;
4668 	}
4669 
4670 	/* Enable partial bssid mask for non-SRG */
4671 	ret = ath12k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, pdev_id,
4672 							       non_srg_bitmap);
4673 	if (ret) {
4674 		ath12k_warn(ar->ab,
4675 			    "failed to enable non SRG bssid bitmap pdev %u: %d\n",
4676 			    pdev_id, ret);
4677 		return ret;
4678 	}
4679 
4680 	return 0;
4681 }
4682 
ath12k_mac_bss_info_changed(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info,u64 changed)4683 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4684 					struct ath12k_link_vif *arvif,
4685 					struct ieee80211_bss_conf *info,
4686 					u64 changed)
4687 {
4688 	struct ath12k_vif *ahvif = arvif->ahvif;
4689 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4690 	struct ieee80211_vif_cfg *vif_cfg = &vif->cfg;
4691 	struct ath12k_link_vif *tx_arvif;
4692 	struct cfg80211_chan_def def;
4693 	u32 param_id, param_value;
4694 	enum nl80211_band band;
4695 	u32 vdev_param;
4696 	int mcast_rate;
4697 	u32 preamble;
4698 	u16 hw_value;
4699 	u16 bitrate;
4700 	u8 rateidx;
4701 	u32 rate;
4702 	int ret;
4703 
4704 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4705 
4706 	if (changed & BSS_CHANGED_BEACON_INT) {
4707 		arvif->beacon_interval = info->beacon_int;
4708 
4709 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4710 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4711 						    param_id,
4712 						    arvif->beacon_interval);
4713 		if (ret)
4714 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4715 				    arvif->vdev_id);
4716 		else
4717 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4718 				   "Beacon interval: %d set for VDEV: %d\n",
4719 				   arvif->beacon_interval, arvif->vdev_id);
4720 	}
4721 
4722 	if (changed & BSS_CHANGED_BEACON) {
4723 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4724 		param_value = WMI_BEACON_BURST_MODE;
4725 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4726 						param_value, ar->pdev->pdev_id);
4727 		if (ret)
4728 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4729 				    arvif->vdev_id);
4730 		else
4731 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4732 				   "Set burst beacon mode for VDEV: %d\n",
4733 				   arvif->vdev_id);
4734 
4735 		/* In MBSSID case, need to install transmitting VIF's template first */
4736 
4737 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4738 		if (ret)
4739 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4740 				    ret);
4741 
4742 		if (!arvif->is_csa_in_progress)
4743 			goto skip_vdev_up;
4744 
4745 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, info);
4746 		if (tx_arvif && arvif != tx_arvif && tx_arvif->is_csa_in_progress)
4747 			/* skip non tx vif's */
4748 			goto skip_vdev_up;
4749 
4750 		ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif, info, ahvif->aid);
4751 
4752 		arvif->is_csa_in_progress = false;
4753 
4754 		if (tx_arvif && arvif == tx_arvif) {
4755 			struct ath12k_link_vif *arvif_itr;
4756 
4757 			list_for_each_entry(arvif_itr, &ar->arvifs, list) {
4758 				if (!arvif_itr->is_csa_in_progress)
4759 					continue;
4760 
4761 				ath12k_wmi_vdev_params_up(ar, arvif, tx_arvif,
4762 							  info, ahvif->aid);
4763 				arvif_itr->is_csa_in_progress = false;
4764 			}
4765 		}
4766 	}
4767 
4768 skip_vdev_up:
4769 
4770 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4771 		arvif->dtim_period = info->dtim_period;
4772 
4773 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4774 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4775 						    param_id,
4776 						    arvif->dtim_period);
4777 
4778 		if (ret)
4779 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4780 				    arvif->vdev_id, ret);
4781 		else
4782 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4783 				   "DTIM period: %d set for VDEV: %d\n",
4784 				   arvif->dtim_period, arvif->vdev_id);
4785 	}
4786 
4787 	if (changed & BSS_CHANGED_SSID &&
4788 	    vif->type == NL80211_IFTYPE_AP) {
4789 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4790 		if (vif->cfg.ssid_len)
4791 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4792 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4793 	}
4794 
4795 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4796 		ether_addr_copy(arvif->bssid, info->bssid);
4797 
4798 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4799 		if (info->enable_beacon) {
4800 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4801 			if (ret)
4802 				ath12k_warn(ar->ab,
4803 					    "failed to set HE TXBF config for vdev: %d\n",
4804 					    arvif->vdev_id);
4805 
4806 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4807 			if (ret)
4808 				ath12k_warn(ar->ab,
4809 					    "failed to set EHT TXBF config for vdev: %d\n",
4810 					    arvif->vdev_id);
4811 		}
4812 		ath12k_control_beaconing(arvif, info);
4813 
4814 		if (arvif->is_up && info->he_support &&
4815 		    info->he_oper.params) {
4816 			/* TODO: Extend to support 1024 BA Bitmap size */
4817 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4818 							    WMI_VDEV_PARAM_BA_MODE,
4819 							    WMI_BA_MODE_BUFFER_SIZE_256);
4820 			if (ret)
4821 				ath12k_warn(ar->ab,
4822 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4823 					    arvif->vdev_id);
4824 
4825 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4826 			param_value = info->he_oper.params;
4827 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4828 							    param_id, param_value);
4829 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4830 				   "he oper param: %x set for VDEV: %d\n",
4831 				   param_value, arvif->vdev_id);
4832 
4833 			if (ret)
4834 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4835 					    param_value, arvif->vdev_id, ret);
4836 		}
4837 	}
4838 
4839 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4840 		u32 cts_prot;
4841 
4842 		cts_prot = !!(info->use_cts_prot);
4843 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4844 
4845 		if (arvif->is_started) {
4846 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4847 							    param_id, cts_prot);
4848 			if (ret)
4849 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4850 					    arvif->vdev_id);
4851 			else
4852 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4853 					   cts_prot, arvif->vdev_id);
4854 		} else {
4855 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4856 		}
4857 	}
4858 
4859 	if (changed & BSS_CHANGED_ERP_SLOT) {
4860 		u32 slottime;
4861 
4862 		if (info->use_short_slot)
4863 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4864 
4865 		else
4866 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4867 
4868 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4869 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4870 						    param_id, slottime);
4871 		if (ret)
4872 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4873 				    arvif->vdev_id);
4874 		else
4875 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4876 				   "Set slottime: %d for VDEV: %d\n",
4877 				   slottime, arvif->vdev_id);
4878 	}
4879 
4880 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4881 		u32 preamble;
4882 
4883 		if (info->use_short_preamble)
4884 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4885 		else
4886 			preamble = WMI_VDEV_PREAMBLE_LONG;
4887 
4888 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4889 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4890 						    param_id, preamble);
4891 		if (ret)
4892 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4893 				    arvif->vdev_id);
4894 		else
4895 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4896 				   "Set preamble: %d for VDEV: %d\n",
4897 				   preamble, arvif->vdev_id);
4898 	}
4899 
4900 	if (changed & BSS_CHANGED_ASSOC) {
4901 		if (vif->cfg.assoc)
4902 			ath12k_bss_assoc(ar, arvif, info);
4903 		else
4904 			ath12k_bss_disassoc(ar, arvif);
4905 	}
4906 
4907 	if (changed & BSS_CHANGED_TXPOWER) {
4908 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4909 			   arvif->vdev_id, info->txpower);
4910 
4911 		arvif->txpower = info->txpower;
4912 		ath12k_mac_txpower_recalc(ar);
4913 	}
4914 
4915 	if (changed & BSS_CHANGED_MCAST_RATE &&
4916 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4917 		band = def.chan->band;
4918 		mcast_rate = info->mcast_rate[band];
4919 
4920 		if (mcast_rate > 0) {
4921 			rateidx = mcast_rate - 1;
4922 		} else {
4923 			if (info->basic_rates)
4924 				rateidx = __ffs(info->basic_rates);
4925 			else
4926 				rateidx = 0;
4927 		}
4928 
4929 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
4930 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
4931 
4932 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
4933 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
4934 
4935 		if (ath12k_mac_bitrate_is_cck(bitrate))
4936 			preamble = WMI_RATE_PREAMBLE_CCK;
4937 		else
4938 			preamble = WMI_RATE_PREAMBLE_OFDM;
4939 
4940 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4941 
4942 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4943 			   "mac vdev %d mcast_rate %x\n",
4944 			   arvif->vdev_id, rate);
4945 
4946 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
4947 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4948 						    vdev_param, rate);
4949 		if (ret)
4950 			ath12k_warn(ar->ab,
4951 				    "failed to set mcast rate on vdev %i: %d\n",
4952 				    arvif->vdev_id,  ret);
4953 
4954 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
4955 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4956 						    vdev_param, rate);
4957 		if (ret)
4958 			ath12k_warn(ar->ab,
4959 				    "failed to set bcast rate on vdev %i: %d\n",
4960 				    arvif->vdev_id,  ret);
4961 	}
4962 
4963 	if (changed & BSS_CHANGED_BASIC_RATES &&
4964 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
4965 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
4966 
4967 	if (changed & BSS_CHANGED_TWT) {
4968 		if (info->twt_requester || info->twt_responder)
4969 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
4970 		else
4971 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
4972 	}
4973 
4974 	if (changed & BSS_CHANGED_HE_OBSS_PD) {
4975 		if (vif->type == NL80211_IFTYPE_AP)
4976 			ath12k_mac_config_obss_pd(arvif, &info->he_obss_pd);
4977 		else
4978 			ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4979 						     &info->he_obss_pd);
4980 	}
4981 
4982 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
4983 		if (vif->type == NL80211_IFTYPE_AP) {
4984 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4985 							    arvif->vdev_id,
4986 							    info->he_bss_color.color,
4987 							    ATH12K_BSS_COLOR_AP_PERIODS,
4988 							    info->he_bss_color.enabled);
4989 			if (ret)
4990 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %u: %d\n",
4991 					    arvif->vdev_id,  ret);
4992 
4993 			param_id = WMI_VDEV_PARAM_BSS_COLOR;
4994 			if (info->he_bss_color.enabled)
4995 				param_value = info->he_bss_color.color <<
4996 					      IEEE80211_HE_OPERATION_BSS_COLOR_OFFSET;
4997 			else
4998 				param_value = IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED;
4999 
5000 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5001 							    param_id,
5002 							    param_value);
5003 			if (ret)
5004 				ath12k_warn(ar->ab, "failed to set bss color param on vdev %u: %d\n",
5005 					    arvif->vdev_id,  ret);
5006 			else
5007 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "bss color param 0x%x set on vdev %u\n",
5008 					   param_value, arvif->vdev_id);
5009 		} else if (vif->type == NL80211_IFTYPE_STATION) {
5010 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
5011 									  arvif->vdev_id,
5012 									  1);
5013 			if (ret)
5014 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
5015 					    arvif->vdev_id,  ret);
5016 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
5017 							    arvif->vdev_id,
5018 							    0,
5019 							    ATH12K_BSS_COLOR_STA_PERIODS,
5020 							    1);
5021 			if (ret)
5022 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
5023 					    arvif->vdev_id,  ret);
5024 		}
5025 	}
5026 
5027 	ath12k_mac_fils_discovery(arvif, info);
5028 
5029 	if (changed & BSS_CHANGED_PS &&
5030 	    ar->ab->hw_params->supports_sta_ps) {
5031 		ahvif->ps = vif_cfg->ps;
5032 		ath12k_mac_vif_setup_ps(arvif);
5033 	}
5034 }
5035 
ath12k_ahvif_get_link_cache(struct ath12k_vif * ahvif,u8 link_id)5036 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
5037 							    u8 link_id)
5038 {
5039 	if (!ahvif->cache[link_id]) {
5040 		ahvif->cache[link_id] = kzalloc_obj(*ahvif->cache[0]);
5041 		if (ahvif->cache[link_id])
5042 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
5043 	}
5044 
5045 	return ahvif->cache[link_id];
5046 }
5047 
ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache * cache)5048 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
5049 {
5050 	struct ath12k_key_conf *key_conf, *tmp;
5051 
5052 	if (!cache || list_empty(&cache->key_conf.list))
5053 		return;
5054 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5055 		list_del(&key_conf->list);
5056 		kfree(key_conf);
5057 	}
5058 }
5059 
ath12k_ahvif_put_link_cache(struct ath12k_vif * ahvif,u8 link_id)5060 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
5061 {
5062 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
5063 		return;
5064 
5065 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
5066 	kfree(ahvif->cache[link_id]);
5067 	ahvif->cache[link_id] = NULL;
5068 }
5069 
ath12k_mac_op_link_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)5070 void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
5071 				     struct ieee80211_vif *vif,
5072 				     struct ieee80211_bss_conf *info,
5073 				     u64 changed)
5074 {
5075 	struct ath12k *ar;
5076 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5077 	struct ath12k_vif_cache *cache;
5078 	struct ath12k_link_vif *arvif;
5079 	u8 link_id = info->link_id;
5080 
5081 	lockdep_assert_wiphy(hw->wiphy);
5082 
5083 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5084 
5085 	/* if the vdev is not created on a certain radio,
5086 	 * cache the info to be updated later on vdev creation
5087 	 */
5088 
5089 	if (!arvif || !arvif->is_created) {
5090 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5091 		if (!cache)
5092 			return;
5093 
5094 		cache->bss_conf_changed |= changed;
5095 
5096 		return;
5097 	}
5098 
5099 	ar = arvif->ar;
5100 
5101 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
5102 }
5103 EXPORT_SYMBOL(ath12k_mac_op_link_info_changed);
5104 
5105 static struct ath12k*
ath12k_mac_select_scan_device(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 center_freq)5106 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
5107 			      struct ieee80211_vif *vif,
5108 			      u32 center_freq)
5109 {
5110 	struct ath12k_hw *ah = hw->priv;
5111 	enum nl80211_band band;
5112 	struct ath12k *ar;
5113 	int i;
5114 
5115 	if (ah->num_radio == 1)
5116 		return ah->radio;
5117 
5118 	/* Currently mac80211 supports splitting scan requests into
5119 	 * multiple scan requests per band.
5120 	 * Loop through first channel and determine the scan radio
5121 	 * TODO: There could be 5 GHz low/high channels in that case
5122 	 * split the hw request and perform multiple scans
5123 	 */
5124 
5125 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
5126 		band = NL80211_BAND_2GHZ;
5127 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
5128 		band = NL80211_BAND_5GHZ;
5129 	else
5130 		band = NL80211_BAND_6GHZ;
5131 
5132 	for_each_ar(ah, ar, i) {
5133 		if (ar->mac.sbands[band].channels &&
5134 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
5135 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
5136 			return ar;
5137 	}
5138 
5139 	return NULL;
5140 }
5141 
__ath12k_mac_scan_finish(struct ath12k * ar)5142 void __ath12k_mac_scan_finish(struct ath12k *ar)
5143 {
5144 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
5145 
5146 	lockdep_assert_held(&ar->data_lock);
5147 
5148 	switch (ar->scan.state) {
5149 	case ATH12K_SCAN_IDLE:
5150 		break;
5151 	case ATH12K_SCAN_RUNNING:
5152 	case ATH12K_SCAN_ABORTING:
5153 		if (ar->scan.is_roc && ar->scan.roc_notify)
5154 			ieee80211_remain_on_channel_expired(hw);
5155 		fallthrough;
5156 	case ATH12K_SCAN_STARTING:
5157 		cancel_delayed_work(&ar->scan.timeout);
5158 		complete_all(&ar->scan.completed);
5159 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
5160 		break;
5161 	}
5162 }
5163 
ath12k_mac_scan_finish(struct ath12k * ar)5164 void ath12k_mac_scan_finish(struct ath12k *ar)
5165 {
5166 	spin_lock_bh(&ar->data_lock);
5167 	__ath12k_mac_scan_finish(ar);
5168 	spin_unlock_bh(&ar->data_lock);
5169 }
5170 
ath12k_scan_stop(struct ath12k * ar)5171 static int ath12k_scan_stop(struct ath12k *ar)
5172 {
5173 	struct ath12k_wmi_scan_cancel_arg arg = {
5174 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
5175 		.scan_id = ATH12K_SCAN_ID,
5176 	};
5177 	int ret;
5178 
5179 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5180 
5181 	/* TODO: Fill other STOP Params */
5182 	arg.pdev_id = ar->pdev->pdev_id;
5183 
5184 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
5185 	if (ret) {
5186 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
5187 		goto out;
5188 	}
5189 
5190 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5191 	if (ret == 0) {
5192 		ath12k_warn(ar->ab,
5193 			    "failed to receive scan abort comple: timed out\n");
5194 		ret = -ETIMEDOUT;
5195 	} else if (ret > 0) {
5196 		ret = 0;
5197 	}
5198 
5199 out:
5200 	/* Scan state should be updated in scan completion worker but in
5201 	 * case firmware fails to deliver the event (for whatever reason)
5202 	 * it is desired to clean up scan state anyway. Firmware may have
5203 	 * just dropped the scan completion event delivery due to transport
5204 	 * pipe being overflown with data and/or it can recover on its own
5205 	 * before next scan request is submitted.
5206 	 */
5207 	spin_lock_bh(&ar->data_lock);
5208 	if (ret)
5209 		__ath12k_mac_scan_finish(ar);
5210 	spin_unlock_bh(&ar->data_lock);
5211 
5212 	return ret;
5213 }
5214 
ath12k_scan_abort(struct ath12k * ar)5215 static void ath12k_scan_abort(struct ath12k *ar)
5216 {
5217 	int ret;
5218 
5219 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5220 
5221 	spin_lock_bh(&ar->data_lock);
5222 
5223 	switch (ar->scan.state) {
5224 	case ATH12K_SCAN_IDLE:
5225 		/* This can happen if timeout worker kicked in and called
5226 		 * abortion while scan completion was being processed.
5227 		 */
5228 		break;
5229 	case ATH12K_SCAN_STARTING:
5230 	case ATH12K_SCAN_ABORTING:
5231 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
5232 			    ar->scan.state);
5233 		break;
5234 	case ATH12K_SCAN_RUNNING:
5235 		ar->scan.state = ATH12K_SCAN_ABORTING;
5236 		spin_unlock_bh(&ar->data_lock);
5237 
5238 		ret = ath12k_scan_stop(ar);
5239 		if (ret)
5240 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
5241 
5242 		spin_lock_bh(&ar->data_lock);
5243 		break;
5244 	}
5245 
5246 	spin_unlock_bh(&ar->data_lock);
5247 }
5248 
ath12k_scan_timeout_work(struct work_struct * work)5249 static void ath12k_scan_timeout_work(struct work_struct *work)
5250 {
5251 	struct ath12k *ar = container_of(work, struct ath12k,
5252 					 scan.timeout.work);
5253 
5254 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
5255 	ath12k_scan_abort(ar);
5256 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
5257 }
5258 
ath12k_mac_scan_send_complete(struct ath12k * ar,struct cfg80211_scan_info * info)5259 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
5260 					  struct cfg80211_scan_info *info)
5261 {
5262 	struct ath12k_hw *ah = ar->ah;
5263 	struct ath12k *partner_ar;
5264 	int i;
5265 
5266 	lockdep_assert_wiphy(ah->hw->wiphy);
5267 
5268 	for_each_ar(ah, partner_ar, i)
5269 		if (partner_ar != ar &&
5270 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
5271 			return;
5272 
5273 	ieee80211_scan_completed(ah->hw, info);
5274 }
5275 
ath12k_scan_vdev_clean_work(struct wiphy * wiphy,struct wiphy_work * work)5276 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
5277 {
5278 	struct ath12k *ar = container_of(work, struct ath12k,
5279 					 scan.vdev_clean_wk);
5280 	struct ath12k_hw *ah = ar->ah;
5281 	struct ath12k_link_vif *arvif;
5282 
5283 	lockdep_assert_wiphy(wiphy);
5284 
5285 	arvif = ar->scan.arvif;
5286 
5287 	/* The scan vdev has already been deleted. This can occur when a
5288 	 * new scan request is made on the same vif with a different
5289 	 * frequency, causing the scan arvif to move from one radio to
5290 	 * another. Or, scan was abrupted and via remove interface, the
5291 	 * arvif is already deleted. Alternatively, if the scan vdev is not
5292 	 * being used as an actual vdev, then do not delete it.
5293 	 */
5294 	if (!arvif || arvif->is_started)
5295 		goto work_complete;
5296 
5297 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
5298 		   arvif->link_id);
5299 
5300 	ath12k_mac_remove_link_interface(ah->hw, arvif);
5301 	ath12k_mac_unassign_link_vif(arvif);
5302 
5303 work_complete:
5304 	spin_lock_bh(&ar->data_lock);
5305 	ar->scan.arvif = NULL;
5306 	if (!ar->scan.is_roc) {
5307 		struct cfg80211_scan_info info = {
5308 			.aborted = ((ar->scan.state ==
5309 				    ATH12K_SCAN_ABORTING) ||
5310 				    (ar->scan.state ==
5311 				    ATH12K_SCAN_STARTING)),
5312 		};
5313 
5314 		ath12k_mac_scan_send_complete(ar, &info);
5315 	}
5316 
5317 	ar->scan.state = ATH12K_SCAN_IDLE;
5318 	ar->scan_channel = NULL;
5319 	ar->scan.roc_freq = 0;
5320 	spin_unlock_bh(&ar->data_lock);
5321 }
5322 
ath12k_start_scan(struct ath12k * ar,struct ath12k_wmi_scan_req_arg * arg)5323 static int ath12k_start_scan(struct ath12k *ar,
5324 			     struct ath12k_wmi_scan_req_arg *arg)
5325 {
5326 	int ret;
5327 
5328 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5329 
5330 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
5331 	if (ret)
5332 		return ret;
5333 
5334 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5335 	if (ret == 0) {
5336 		ret = ath12k_scan_stop(ar);
5337 		if (ret)
5338 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
5339 
5340 		return -ETIMEDOUT;
5341 	}
5342 
5343 	/* If we failed to start the scan, return error code at
5344 	 * this point.  This is probably due to some issue in the
5345 	 * firmware, but no need to wedge the driver due to that...
5346 	 */
5347 	spin_lock_bh(&ar->data_lock);
5348 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
5349 		spin_unlock_bh(&ar->data_lock);
5350 		return -EINVAL;
5351 	}
5352 	spin_unlock_bh(&ar->data_lock);
5353 
5354 	return 0;
5355 }
5356 
ath12k_mac_get_fw_stats(struct ath12k * ar,struct ath12k_fw_stats_req_params * param)5357 int ath12k_mac_get_fw_stats(struct ath12k *ar,
5358 			    struct ath12k_fw_stats_req_params *param)
5359 {
5360 	struct ath12k_base *ab = ar->ab;
5361 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
5362 	unsigned long time_left;
5363 	int ret;
5364 
5365 	guard(mutex)(&ah->hw_mutex);
5366 
5367 	if (ah->state != ATH12K_HW_STATE_ON)
5368 		return -ENETDOWN;
5369 
5370 	reinit_completion(&ar->fw_stats_complete);
5371 	reinit_completion(&ar->fw_stats_done);
5372 
5373 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
5374 						param->vdev_id, param->pdev_id);
5375 	if (ret) {
5376 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
5377 		return ret;
5378 	}
5379 
5380 	ath12k_dbg(ab, ATH12K_DBG_WMI,
5381 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
5382 		   param->pdev_id, param->vdev_id, param->stats_id);
5383 
5384 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
5385 	if (!time_left) {
5386 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
5387 		return -ETIMEDOUT;
5388 	}
5389 
5390 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
5391 	 * when stats data buffer limit is reached. fw_stats_complete
5392 	 * is completed once host receives first event from firmware, but
5393 	 * still there could be more events following. Below is to wait
5394 	 * until firmware completes sending all the events.
5395 	 */
5396 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
5397 	if (!time_left) {
5398 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
5399 		return -ETIMEDOUT;
5400 	}
5401 
5402 	return 0;
5403 }
5404 
ath12k_mac_op_get_txpower(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,int * dbm)5405 int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
5406 			      struct ieee80211_vif *vif,
5407 			      unsigned int link_id,
5408 			      int *dbm)
5409 {
5410 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5411 	struct ath12k_fw_stats_req_params params = {};
5412 	struct ath12k_fw_stats_pdev *pdev;
5413 	struct ath12k_hw *ah = hw->priv;
5414 	struct ath12k_link_vif *arvif;
5415 	struct ath12k_base *ab;
5416 	struct ath12k *ar;
5417 	int ret;
5418 
5419 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
5420 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
5421 	 * regulatory rules obtained. FW knows all these power and sets the min
5422 	 * of these. Hence, we request the FW pdev stats in which FW reports
5423 	 * the minimum of all vdev's channel Tx power.
5424 	 */
5425 	lockdep_assert_wiphy(hw->wiphy);
5426 
5427 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5428 	if (!arvif || !arvif->ar)
5429 		return -EINVAL;
5430 
5431 	ar = arvif->ar;
5432 	ab = ar->ab;
5433 	if (ah->state != ATH12K_HW_STATE_ON)
5434 		goto err_fallback;
5435 
5436 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
5437 		return -EAGAIN;
5438 
5439 	/* Limit the requests to Firmware for fetching the tx power */
5440 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
5441 	    time_before(jiffies,
5442 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
5443 					 ar->last_tx_power_update))
5444 		goto send_tx_power;
5445 
5446 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
5447 	params.vdev_id = arvif->vdev_id;
5448 	params.stats_id = WMI_REQUEST_PDEV_STAT;
5449 	ret = ath12k_mac_get_fw_stats(ar, &params);
5450 	if (ret) {
5451 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
5452 		goto err_fallback;
5453 	}
5454 
5455 	spin_lock_bh(&ar->data_lock);
5456 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
5457 					struct ath12k_fw_stats_pdev, list);
5458 	if (!pdev) {
5459 		spin_unlock_bh(&ar->data_lock);
5460 		goto err_fallback;
5461 	}
5462 
5463 	/* tx power reported by firmware is in units of 0.5 dBm */
5464 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
5465 	spin_unlock_bh(&ar->data_lock);
5466 	ar->last_tx_power_update = jiffies;
5467 	ath12k_fw_stats_reset(ar);
5468 
5469 send_tx_power:
5470 	*dbm = ar->chan_tx_pwr;
5471 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
5472 		   *dbm);
5473 	return 0;
5474 
5475 err_fallback:
5476 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
5477 	*dbm = vif->bss_conf.txpower;
5478 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
5479 		   *dbm);
5480 	return 0;
5481 }
5482 EXPORT_SYMBOL(ath12k_mac_op_get_txpower);
5483 
5484 static u8
ath12k_mac_find_link_id_by_ar(struct ath12k_vif * ahvif,struct ath12k * ar)5485 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
5486 {
5487 	struct ath12k_link_vif *arvif;
5488 	struct ath12k_hw *ah = ahvif->ah;
5489 	unsigned long links = ahvif->links_map;
5490 	unsigned long scan_links_map;
5491 	u8 link_id;
5492 
5493 	lockdep_assert_wiphy(ah->hw->wiphy);
5494 
5495 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
5496 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
5497 
5498 		if (!arvif || !arvif->is_created)
5499 			continue;
5500 
5501 		if (ar == arvif->ar)
5502 			return link_id;
5503 	}
5504 
5505 	/* input ar is not assigned to any of the links of ML VIF, use next
5506 	 * available scan link for scan vdev creation. There are cases where
5507 	 * single scan req needs to be split in driver and initiate separate
5508 	 * scan requests to firmware based on device.
5509 	 */
5510 
5511 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5512 	  * choose an available link among scan links (i.e link id >= 15)
5513 	  */
5514 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5515 	if (scan_links_map)
5516 		return __ffs(scan_links_map);
5517 
5518 	return ATH12K_FIRST_SCAN_LINK;
5519 }
5520 
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)5521 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5522 				       struct ieee80211_vif *vif,
5523 				       struct ieee80211_scan_request *hw_req,
5524 				       int n_channels,
5525 				       struct ieee80211_channel **chan_list,
5526 				       struct ath12k *ar)
5527 {
5528 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5529 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5530 	struct ath12k_link_vif *arvif;
5531 	struct cfg80211_scan_request *req = &hw_req->req;
5532 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5533 	u8 link_id;
5534 	int ret;
5535 	int i;
5536 	bool create = true;
5537 
5538 	lockdep_assert_wiphy(hw->wiphy);
5539 
5540 	arvif = &ahvif->deflink;
5541 
5542 	/* check if any of the links of ML VIF is already started on
5543 	 * radio(ar) corresponding to given scan frequency and use it,
5544 	 * if not use scan link (link id >= 15) for scan purpose.
5545 	 */
5546 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5547 	/* All scan links are occupied. ideally this shouldn't happen as
5548 	 * mac80211 won't schedule scan for same band until ongoing scan is
5549 	 * completed, don't try to exceed max links just in case if it happens.
5550 	 */
5551 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5552 		return -EBUSY;
5553 
5554 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5555 
5556 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5557 		   arvif->link_id);
5558 
5559 	/* If the vif is already assigned to a specific vdev of an ar,
5560 	 * check whether its already started, vdev which is started
5561 	 * are not allowed to switch to a new radio.
5562 	 * If the vdev is not started, but was earlier created on a
5563 	 * different ar, delete that vdev and create a new one. We don't
5564 	 * delete at the scan stop as an optimization to avoid redundant
5565 	 * delete-create vdev's for the same ar, in case the request is
5566 	 * always on the same band for the vif
5567 	 */
5568 	if (arvif->is_created) {
5569 		if (WARN_ON(!arvif->ar))
5570 			return -EINVAL;
5571 
5572 		if (ar != arvif->ar && arvif->is_started)
5573 			return -EINVAL;
5574 
5575 		if (ar != arvif->ar) {
5576 			ath12k_mac_remove_link_interface(hw, arvif);
5577 			ath12k_mac_unassign_link_vif(arvif);
5578 		} else {
5579 			create = false;
5580 		}
5581 	}
5582 
5583 	if (create) {
5584 		/* Previous arvif would've been cleared in radio switch block
5585 		 * above, assign arvif again for create.
5586 		 */
5587 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5588 
5589 		ret = ath12k_mac_vdev_create(ar, arvif);
5590 		if (ret) {
5591 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5592 			ath12k_mac_unassign_link_vif(arvif);
5593 			return ret;
5594 		}
5595 	}
5596 
5597 	spin_lock_bh(&ar->data_lock);
5598 	switch (ar->scan.state) {
5599 	case ATH12K_SCAN_IDLE:
5600 		reinit_completion(&ar->scan.started);
5601 		reinit_completion(&ar->scan.completed);
5602 		ar->scan.state = ATH12K_SCAN_STARTING;
5603 		ar->scan.is_roc = false;
5604 		ar->scan.arvif = arvif;
5605 		ret = 0;
5606 		break;
5607 	case ATH12K_SCAN_STARTING:
5608 	case ATH12K_SCAN_RUNNING:
5609 	case ATH12K_SCAN_ABORTING:
5610 		ret = -EBUSY;
5611 		break;
5612 	}
5613 	spin_unlock_bh(&ar->data_lock);
5614 
5615 	if (ret)
5616 		goto exit;
5617 
5618 	arg = kzalloc_obj(*arg);
5619 	if (!arg) {
5620 		ret = -ENOMEM;
5621 		goto exit;
5622 	}
5623 
5624 	ath12k_wmi_start_scan_init(ar, arg);
5625 	arg->vdev_id = arvif->vdev_id;
5626 	arg->scan_id = ATH12K_SCAN_ID;
5627 
5628 	if (req->ie_len) {
5629 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5630 		if (!arg->extraie.ptr) {
5631 			ret = -ENOMEM;
5632 			goto exit;
5633 		}
5634 		arg->extraie.len = req->ie_len;
5635 	}
5636 
5637 	if (req->n_ssids) {
5638 		arg->num_ssids = req->n_ssids;
5639 		for (i = 0; i < arg->num_ssids; i++)
5640 			arg->ssid[i] = req->ssids[i];
5641 	} else {
5642 		arg->scan_f_passive = 1;
5643 	}
5644 
5645 	if (n_channels) {
5646 		arg->num_chan = n_channels;
5647 		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
5648 					 GFP_KERNEL);
5649 		if (!arg->chan_list) {
5650 			ret = -ENOMEM;
5651 			goto exit;
5652 		}
5653 
5654 		for (i = 0; i < arg->num_chan; i++)
5655 			arg->chan_list[i] = chan_list[i]->center_freq;
5656 	}
5657 
5658 	ret = ath12k_start_scan(ar, arg);
5659 	if (ret) {
5660 		if (ret == -EBUSY)
5661 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5662 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5663 		else
5664 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5665 
5666 		spin_lock_bh(&ar->data_lock);
5667 		ar->scan.state = ATH12K_SCAN_IDLE;
5668 		spin_unlock_bh(&ar->data_lock);
5669 		goto exit;
5670 	}
5671 
5672 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5673 
5674 	/* Add a margin to account for event/command processing */
5675 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5676 				     msecs_to_jiffies(arg->max_scan_time +
5677 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5678 
5679 exit:
5680 	if (arg) {
5681 		kfree(arg->chan_list);
5682 		kfree(arg->extraie.ptr);
5683 		kfree(arg);
5684 	}
5685 
5686 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5687 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5688 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5689 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5690 
5691 	return ret;
5692 }
5693 
ath12k_mac_op_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)5694 int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5695 			  struct ieee80211_vif *vif,
5696 			  struct ieee80211_scan_request *hw_req)
5697 {
5698 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5699 	struct ieee80211_channel **chan_list, *chan;
5700 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5701 	unsigned long links_map, link_id;
5702 	struct ath12k_link_vif *arvif;
5703 	struct ath12k *ar, *scan_ar;
5704 	int i, j, ret = 0;
5705 
5706 	lockdep_assert_wiphy(hw->wiphy);
5707 
5708 	chan_list = kzalloc_objs(*chan_list, hw_req->req.n_channels);
5709 	if (!chan_list)
5710 		return -ENOMEM;
5711 
5712 	/* There could be channels that belong to multiple underlying radio
5713 	 * in same scan request as mac80211 sees it as single band. In that
5714 	 * case split the hw_req based on frequency range and schedule scans to
5715 	 * corresponding radio.
5716 	 */
5717 	for_each_ar(ah, ar, i) {
5718 		int n_chans = 0;
5719 
5720 		for (j = 0; j < hw_req->req.n_channels; j++) {
5721 			chan = hw_req->req.channels[j];
5722 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5723 								chan->center_freq);
5724 			if (!scan_ar) {
5725 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5726 					       chan->center_freq);
5727 				ret = -EINVAL;
5728 				goto abort;
5729 			}
5730 			if (ar != scan_ar)
5731 				continue;
5732 
5733 			chan_list[n_chans++] = chan;
5734 		}
5735 		if (n_chans) {
5736 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5737 							  chan_list, ar);
5738 			if (ret)
5739 				goto abort;
5740 		}
5741 	}
5742 abort:
5743 	/* If any of the parallel scans initiated fails, abort all and
5744 	 * remove the scan interfaces created. Return complete scan
5745 	 * failure as mac80211 assumes this as single scan request.
5746 	 */
5747 	if (ret) {
5748 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5749 		links_map = ahvif->links_map;
5750 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5751 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5752 			if (!arvif)
5753 				continue;
5754 
5755 			ar = arvif->ar;
5756 			if (ar->scan.arvif == arvif) {
5757 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5758 				spin_lock_bh(&ar->data_lock);
5759 				ar->scan.arvif = NULL;
5760 				ar->scan.state = ATH12K_SCAN_IDLE;
5761 				ar->scan_channel = NULL;
5762 				ar->scan.roc_freq = 0;
5763 				spin_unlock_bh(&ar->data_lock);
5764 			}
5765 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5766 				ath12k_mac_remove_link_interface(hw, arvif);
5767 				ath12k_mac_unassign_link_vif(arvif);
5768 			}
5769 		}
5770 	}
5771 	kfree(chan_list);
5772 	return ret;
5773 }
5774 EXPORT_SYMBOL(ath12k_mac_op_hw_scan);
5775 
ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5776 void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5777 				  struct ieee80211_vif *vif)
5778 {
5779 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5780 	unsigned long link_id, links_map = ahvif->links_map;
5781 	struct ath12k_link_vif *arvif;
5782 	struct ath12k *ar;
5783 
5784 	lockdep_assert_wiphy(hw->wiphy);
5785 
5786 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5787 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5788 		if (!arvif || !arvif->is_created ||
5789 		    arvif->ar->scan.arvif != arvif)
5790 			continue;
5791 
5792 		ar = arvif->ar;
5793 
5794 		ath12k_scan_abort(ar);
5795 
5796 		cancel_delayed_work_sync(&ar->scan.timeout);
5797 	}
5798 }
5799 EXPORT_SYMBOL(ath12k_mac_op_cancel_hw_scan);
5800 
ath12k_install_key(struct ath12k_link_vif * arvif,struct ieee80211_key_conf * key,enum set_key_cmd cmd,const u8 * macaddr,u32 flags)5801 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5802 			      struct ieee80211_key_conf *key,
5803 			      enum set_key_cmd cmd,
5804 			      const u8 *macaddr, u32 flags)
5805 {
5806 	int ret;
5807 	struct ath12k *ar = arvif->ar;
5808 	struct wmi_vdev_install_key_arg arg = {
5809 		.vdev_id = arvif->vdev_id,
5810 		.key_idx = key->keyidx,
5811 		.key_len = key->keylen,
5812 		.key_data = key->key,
5813 		.key_flags = flags,
5814 		.ieee80211_key_cipher = key->cipher,
5815 		.macaddr = macaddr,
5816 	};
5817 	struct ath12k_vif *ahvif = arvif->ahvif;
5818 
5819 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5820 
5821 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5822 		return 0;
5823 
5824 	if (cmd == DISABLE_KEY) {
5825 		/* TODO: Check if FW expects  value other than NONE for del */
5826 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5827 		arg.key_len = 0;
5828 		arg.key_data = NULL;
5829 		goto check_order;
5830 	}
5831 
5832 	switch (key->cipher) {
5833 	case WLAN_CIPHER_SUITE_CCMP:
5834 	case WLAN_CIPHER_SUITE_CCMP_256:
5835 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5836 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5837 		break;
5838 	case WLAN_CIPHER_SUITE_TKIP:
5839 		arg.key_cipher = WMI_CIPHER_TKIP;
5840 		arg.key_txmic_len = 8;
5841 		arg.key_rxmic_len = 8;
5842 		break;
5843 	case WLAN_CIPHER_SUITE_GCMP:
5844 	case WLAN_CIPHER_SUITE_GCMP_256:
5845 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5846 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5847 		break;
5848 	case WLAN_CIPHER_SUITE_AES_CMAC:
5849 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5850 		break;
5851 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5852 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5853 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5854 		break;
5855 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5856 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5857 		break;
5858 	default:
5859 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5860 		return -EOPNOTSUPP;
5861 	}
5862 
5863 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5864 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5865 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5866 
5867 check_order:
5868 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5869 	    arg.key_flags == WMI_KEY_GROUP) {
5870 		if (cmd == SET_KEY) {
5871 			if (arvif->pairwise_key_done) {
5872 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5873 					   "vdev %u pairwise key done, go install group key\n",
5874 					   arg.vdev_id);
5875 				goto install;
5876 			} else {
5877 				/* WCN7850 firmware requires pairwise key to be installed
5878 				 * before group key. In case group key comes first, cache
5879 				 * it and return. Will revisit it once pairwise key gets
5880 				 * installed.
5881 				 */
5882 				arvif->group_key = arg;
5883 				arvif->group_key_valid = true;
5884 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5885 					   "vdev %u group key before pairwise key, cache and skip\n",
5886 					   arg.vdev_id);
5887 
5888 				ret = 0;
5889 				goto out;
5890 			}
5891 		} else {
5892 			arvif->group_key_valid = false;
5893 		}
5894 	}
5895 
5896 install:
5897 	reinit_completion(&ar->install_key_done);
5898 
5899 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5900 	if (ret)
5901 		return ret;
5902 
5903 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5904 		return -ETIMEDOUT;
5905 
5906 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5907 		ahvif->dp_vif.key_cipher = arg.ieee80211_key_cipher;
5908 
5909 	if (ar->install_key_status) {
5910 		ret = -EINVAL;
5911 		goto out;
5912 	}
5913 
5914 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5915 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5916 		if (cmd == SET_KEY) {
5917 			arvif->pairwise_key_done = true;
5918 			if (arvif->group_key_valid) {
5919 				/* Install cached GTK */
5920 				arvif->group_key_valid = false;
5921 				arg = arvif->group_key;
5922 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5923 					   "vdev %u pairwise key done, group key ready, go install\n",
5924 					   arg.vdev_id);
5925 				goto install;
5926 			}
5927 		} else {
5928 			arvif->pairwise_key_done = false;
5929 		}
5930 	}
5931 
5932 out:
5933 	if (ret) {
5934 		/* In case of failure userspace may not do DISABLE_KEY
5935 		 * but triggers re-connection directly, so manually reset
5936 		 * status here.
5937 		 */
5938 		arvif->group_key_valid = false;
5939 		arvif->pairwise_key_done = false;
5940 	}
5941 
5942 	return ret;
5943 }
5944 
ath12k_clear_peer_keys(struct ath12k_link_vif * arvif,const u8 * addr)5945 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5946 				  const u8 *addr)
5947 {
5948 	struct ath12k *ar = arvif->ar;
5949 	struct ath12k_base *ab = ar->ab;
5950 	struct ath12k_dp_link_peer *peer;
5951 	int first_errno = 0;
5952 	int ret;
5953 	int i, len;
5954 	u32 flags = 0;
5955 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
5956 	struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1] = {};
5957 
5958 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5959 
5960 	spin_lock_bh(&dp->dp_lock);
5961 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id, addr);
5962 	if (!peer || !peer->dp_peer) {
5963 		spin_unlock_bh(&dp->dp_lock);
5964 		return -ENOENT;
5965 	}
5966 
5967 	len = ARRAY_SIZE(peer->dp_peer->keys);
5968 	for (i = 0; i < len; i++) {
5969 		if (!peer->dp_peer->keys[i])
5970 			continue;
5971 
5972 		keys[i] = peer->dp_peer->keys[i];
5973 		peer->dp_peer->keys[i] = NULL;
5974 	}
5975 
5976 	spin_unlock_bh(&dp->dp_lock);
5977 
5978 	for (i = 0; i < len; i++) {
5979 		if (!keys[i])
5980 			continue;
5981 
5982 		/* key flags are not required to delete the key */
5983 		ret = ath12k_install_key(arvif, keys[i],
5984 					 DISABLE_KEY, addr, flags);
5985 		if (ret < 0 && first_errno == 0)
5986 			first_errno = ret;
5987 
5988 		if (ret < 0)
5989 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5990 				    i, ret);
5991 	}
5992 
5993 	return first_errno;
5994 }
5995 
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)5996 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5997 			      struct ath12k_link_vif *arvif,
5998 			      struct ath12k_link_sta *arsta,
5999 			      struct ieee80211_key_conf *key)
6000 {
6001 	struct ieee80211_sta *sta = NULL;
6002 	struct ath12k_base *ab = ar->ab;
6003 	struct ath12k_dp_link_peer *peer;
6004 	struct ath12k_sta *ahsta;
6005 	const u8 *peer_addr;
6006 	int ret;
6007 	u32 flags = 0;
6008 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
6009 
6010 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6011 
6012 	if (arsta)
6013 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
6014 
6015 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
6016 		return 1;
6017 
6018 	if (sta)
6019 		peer_addr = arsta->addr;
6020 	else
6021 		peer_addr = arvif->bssid;
6022 
6023 	key->hw_key_idx = key->keyidx;
6024 
6025 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6026 	 * we already hold wiphy lock. we just make sure its there now.
6027 	 */
6028 	spin_lock_bh(&dp->dp_lock);
6029 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6030 							 peer_addr);
6031 	if (!peer || !peer->dp_peer) {
6032 		spin_unlock_bh(&dp->dp_lock);
6033 
6034 		if (cmd == SET_KEY) {
6035 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
6036 				    peer_addr);
6037 			return -EOPNOTSUPP;
6038 		}
6039 
6040 		/* if the peer doesn't exist there is no key to disable
6041 		 * anymore
6042 		 */
6043 		return 0;
6044 	}
6045 
6046 	spin_unlock_bh(&dp->dp_lock);
6047 
6048 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6049 		flags = WMI_KEY_PAIRWISE;
6050 	else
6051 		flags = WMI_KEY_GROUP;
6052 
6053 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
6054 	if (ret) {
6055 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
6056 		return ret;
6057 	}
6058 
6059 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
6060 	if (ret) {
6061 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
6062 		return ret;
6063 	}
6064 
6065 	spin_lock_bh(&dp->dp_lock);
6066 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6067 							 peer_addr);
6068 	if (peer && peer->dp_peer && cmd == SET_KEY) {
6069 		peer->dp_peer->keys[key->keyidx] = key;
6070 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
6071 			peer->dp_peer->ucast_keyidx = key->keyidx;
6072 			peer->dp_peer->sec_type =
6073 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6074 		} else {
6075 			peer->dp_peer->mcast_keyidx = key->keyidx;
6076 			peer->dp_peer->sec_type_grp =
6077 					ath12k_dp_tx_get_encrypt_type(key->cipher);
6078 		}
6079 	} else if (peer && peer->dp_peer && cmd == DISABLE_KEY) {
6080 		peer->dp_peer->keys[key->keyidx] = NULL;
6081 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6082 			peer->dp_peer->ucast_keyidx = 0;
6083 		else
6084 			peer->dp_peer->mcast_keyidx = 0;
6085 	} else if (!peer)
6086 		/* impossible unless FW goes crazy */
6087 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
6088 
6089 	if (sta) {
6090 		ahsta = ath12k_sta_to_ahsta(sta);
6091 
6092 		switch (key->cipher) {
6093 		case WLAN_CIPHER_SUITE_TKIP:
6094 		case WLAN_CIPHER_SUITE_CCMP:
6095 		case WLAN_CIPHER_SUITE_CCMP_256:
6096 		case WLAN_CIPHER_SUITE_GCMP:
6097 		case WLAN_CIPHER_SUITE_GCMP_256:
6098 			if (cmd == SET_KEY)
6099 				ahsta->pn_type = HAL_PN_TYPE_WPA;
6100 			else
6101 				ahsta->pn_type = HAL_PN_TYPE_NONE;
6102 			break;
6103 		default:
6104 			ahsta->pn_type = HAL_PN_TYPE_NONE;
6105 			break;
6106 		}
6107 	}
6108 
6109 	spin_unlock_bh(&dp->dp_lock);
6110 
6111 	return 0;
6112 }
6113 
ath12k_mac_update_key_cache(struct ath12k_vif_cache * cache,enum set_key_cmd cmd,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)6114 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
6115 				       enum set_key_cmd cmd,
6116 				       struct ieee80211_sta *sta,
6117 				       struct ieee80211_key_conf *key)
6118 {
6119 	struct ath12k_key_conf *key_conf, *tmp;
6120 
6121 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
6122 		if (key_conf->key != key)
6123 			continue;
6124 
6125 		/* If SET key entry is already present in cache, nothing to do,
6126 		 * just return
6127 		 */
6128 		if (cmd == SET_KEY)
6129 			return 0;
6130 
6131 		/* DEL key for an old SET key which driver hasn't flushed yet.
6132 		 */
6133 		list_del(&key_conf->list);
6134 		kfree(key_conf);
6135 	}
6136 
6137 	if (cmd == SET_KEY) {
6138 		key_conf = kzalloc_obj(*key_conf);
6139 
6140 		if (!key_conf)
6141 			return -ENOMEM;
6142 
6143 		key_conf->cmd = cmd;
6144 		key_conf->sta = sta;
6145 		key_conf->key = key;
6146 		list_add_tail(&key_conf->list,
6147 			      &cache->key_conf.list);
6148 	}
6149 
6150 	return 0;
6151 }
6152 
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)6153 int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6154 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6155 			  struct ieee80211_key_conf *key)
6156 {
6157 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6158 	struct ath12k_link_vif *arvif;
6159 	struct ath12k_link_sta *arsta = NULL;
6160 	struct ath12k_vif_cache *cache;
6161 	struct ath12k_sta *ahsta;
6162 	unsigned long links;
6163 	u8 link_id;
6164 	int ret;
6165 
6166 	lockdep_assert_wiphy(hw->wiphy);
6167 
6168 	/* IGTK needs to be done in host software */
6169 	if (key->keyidx == 4 || key->keyidx == 5)
6170 		return 1;
6171 
6172 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6173 		return -ENOSPC;
6174 
6175 	if (sta) {
6176 		ahsta = ath12k_sta_to_ahsta(sta);
6177 
6178 		/* For an ML STA Pairwise key is same for all associated link Stations,
6179 		 * hence do set key for all link STAs which are active.
6180 		 */
6181 		if (sta->mlo) {
6182 			links = ahsta->links_map;
6183 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6184 				arvif = wiphy_dereference(hw->wiphy,
6185 							  ahvif->link[link_id]);
6186 				arsta = wiphy_dereference(hw->wiphy,
6187 							  ahsta->link[link_id]);
6188 
6189 				if (WARN_ON(!arvif || !arsta))
6190 					/* arvif and arsta are expected to be valid when
6191 					 * STA is present.
6192 					 */
6193 					continue;
6194 
6195 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
6196 							 arsta, key);
6197 				if (ret)
6198 					break;
6199 			}
6200 
6201 			return 0;
6202 		}
6203 
6204 		arsta = &ahsta->deflink;
6205 		arvif = arsta->arvif;
6206 		if (WARN_ON(!arvif))
6207 			return -EINVAL;
6208 
6209 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
6210 		if (ret)
6211 			return ret;
6212 
6213 		return 0;
6214 	}
6215 
6216 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
6217 		link_id = key->link_id;
6218 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6219 	} else {
6220 		link_id = 0;
6221 		arvif = &ahvif->deflink;
6222 	}
6223 
6224 	if (!arvif || !arvif->is_created) {
6225 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
6226 		if (!cache)
6227 			return -ENOSPC;
6228 
6229 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
6230 		if (ret)
6231 			return ret;
6232 
6233 		return 0;
6234 	}
6235 
6236 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
6237 	if (ret)
6238 		return ret;
6239 
6240 	return 0;
6241 }
6242 EXPORT_SYMBOL(ath12k_mac_op_set_key);
6243 
6244 static int
ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6245 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
6246 				      enum nl80211_band band,
6247 				      const struct cfg80211_bitrate_mask *mask)
6248 {
6249 	int num_rates = 0;
6250 	int i;
6251 
6252 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6253 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
6254 
6255 	return num_rates;
6256 }
6257 
6258 static int
ath12k_mac_bitrate_mask_num_he_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6259 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
6260 				     enum nl80211_band band,
6261 				     const struct cfg80211_bitrate_mask *mask)
6262 {
6263 	int num_rates = 0;
6264 	int i;
6265 
6266 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
6267 		num_rates += hweight16(mask->control[band].he_mcs[i]);
6268 
6269 	return num_rates;
6270 }
6271 
6272 static int
ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6273 ath12k_mac_bitrate_mask_num_eht_rates(struct ath12k *ar,
6274 				      enum nl80211_band band,
6275 				      const struct cfg80211_bitrate_mask *mask)
6276 {
6277 	int num_rates = 0;
6278 	int i;
6279 
6280 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++)
6281 		num_rates += hweight16(mask->control[band].eht_mcs[i]);
6282 
6283 	return num_rates;
6284 }
6285 
6286 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)6287 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
6288 				   struct ath12k_link_sta *arsta,
6289 				   const struct cfg80211_bitrate_mask *mask,
6290 				   enum nl80211_band band)
6291 {
6292 	struct ath12k *ar = arvif->ar;
6293 	u8 vht_rate, nss;
6294 	u32 rate_code;
6295 	int ret, i;
6296 
6297 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6298 
6299 	nss = 0;
6300 
6301 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6302 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6303 			nss = i + 1;
6304 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
6305 		}
6306 	}
6307 
6308 	if (!nss) {
6309 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
6310 			    arsta->addr);
6311 		return -EINVAL;
6312 	}
6313 
6314 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6315 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
6316 		   arsta->addr);
6317 
6318 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
6319 					WMI_RATE_PREAMBLE_VHT);
6320 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6321 					arvif->vdev_id,
6322 					WMI_PEER_PARAM_FIXED_RATE,
6323 					rate_code);
6324 	if (ret)
6325 		ath12k_warn(ar->ab,
6326 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6327 			     arsta->addr, rate_code, ret);
6328 
6329 	return ret;
6330 }
6331 
6332 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)6333 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
6334 				  struct ath12k_link_sta *arsta,
6335 				  const struct cfg80211_bitrate_mask *mask,
6336 				  enum nl80211_band band)
6337 {
6338 	struct ath12k *ar = arvif->ar;
6339 	u8 he_rate, nss;
6340 	u32 rate_code;
6341 	int ret, i;
6342 	struct ath12k_sta *ahsta = arsta->ahsta;
6343 	struct ieee80211_sta *sta;
6344 
6345 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6346 
6347 	sta = ath12k_ahsta_to_sta(ahsta);
6348 	nss = 0;
6349 
6350 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
6351 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
6352 			nss = i + 1;
6353 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
6354 		}
6355 	}
6356 
6357 	if (!nss) {
6358 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
6359 			    arsta->addr);
6360 		return -EINVAL;
6361 	}
6362 
6363 	/* Avoid updating invalid nss as fixed rate*/
6364 	if (nss > sta->deflink.rx_nss)
6365 		return -EINVAL;
6366 
6367 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6368 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
6369 		   arsta->addr);
6370 
6371 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
6372 					WMI_RATE_PREAMBLE_HE);
6373 
6374 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6375 					arvif->vdev_id,
6376 					WMI_PEER_PARAM_FIXED_RATE,
6377 					rate_code);
6378 	if (ret)
6379 		ath12k_warn(ar->ab,
6380 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6381 			    arsta->addr, rate_code, ret);
6382 
6383 	return ret;
6384 }
6385 
6386 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)6387 ath12k_mac_set_peer_eht_fixed_rate(struct ath12k_link_vif *arvif,
6388 				   struct ath12k_link_sta *arsta,
6389 				   const struct cfg80211_bitrate_mask *mask,
6390 				   enum nl80211_band band)
6391 {
6392 	struct ath12k_sta *ahsta = arsta->ahsta;
6393 	struct ath12k *ar = arvif->ar;
6394 	struct ieee80211_sta *sta;
6395 	struct ieee80211_link_sta *link_sta;
6396 	u8 eht_rate, nss = 0;
6397 	u32 rate_code;
6398 	int ret, i;
6399 
6400 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6401 
6402 	sta = ath12k_ahsta_to_sta(ahsta);
6403 
6404 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
6405 		if (hweight16(mask->control[band].eht_mcs[i]) == 1) {
6406 			nss = i + 1;
6407 			eht_rate = ffs(mask->control[band].eht_mcs[i]) - 1;
6408 		}
6409 	}
6410 
6411 	if (!nss) {
6412 		ath12k_warn(ar->ab, "No single EHT Fixed rate found to set for %pM\n",
6413 			    arsta->addr);
6414 		return -EINVAL;
6415 	}
6416 
6417 	/* Avoid updating invalid nss as fixed rate*/
6418 	link_sta = ath12k_mac_get_link_sta(arsta);
6419 	if (!link_sta || nss > link_sta->rx_nss) {
6420 		ath12k_warn(ar->ab,
6421 			    "unable to access link sta for sta %pM link %u or fixed nss of %u is not supported by sta\n",
6422 			    sta->addr, arsta->link_id, nss);
6423 		return -EINVAL;
6424 	}
6425 
6426 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6427 		   "Setting Fixed EHT Rate for peer %pM. Device will not switch to any other selected rates\n",
6428 		   arsta->addr);
6429 
6430 	rate_code = ATH12K_HW_RATE_CODE(eht_rate, nss - 1,
6431 					WMI_RATE_PREAMBLE_EHT);
6432 
6433 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6434 					arvif->vdev_id,
6435 					WMI_PEER_PARAM_FIXED_RATE,
6436 					rate_code);
6437 	if (ret)
6438 		ath12k_warn(ar->ab,
6439 			    "failed to update STA %pM Fixed Rate %d: %d\n",
6440 			    arsta->addr, rate_code, ret);
6441 
6442 	return ret;
6443 }
6444 
ath12k_mac_station_assoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,bool reassoc)6445 static int ath12k_mac_station_assoc(struct ath12k *ar,
6446 				    struct ath12k_link_vif *arvif,
6447 				    struct ath12k_link_sta *arsta,
6448 				    bool reassoc)
6449 {
6450 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6451 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6452 	struct ieee80211_link_sta *link_sta;
6453 	int ret;
6454 	struct cfg80211_chan_def def;
6455 	enum nl80211_band band;
6456 	struct cfg80211_bitrate_mask *mask;
6457 	u8 num_vht_rates, num_he_rates, num_eht_rates;
6458 	u8 link_id = arvif->link_id;
6459 
6460 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6461 
6462 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6463 		return -EPERM;
6464 
6465 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
6466 		return -EINVAL;
6467 
6468 	band = def.chan->band;
6469 	mask = &arvif->bitrate_mask;
6470 
6471 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6472 		kzalloc_obj(*peer_arg);
6473 	if (!peer_arg)
6474 		return -ENOMEM;
6475 
6476 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
6477 
6478 	if (peer_arg->peer_nss < 1) {
6479 		ath12k_warn(ar->ab,
6480 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
6481 		return -EINVAL;
6482 	}
6483 
6484 	peer_arg->is_assoc = true;
6485 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6486 	if (ret) {
6487 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6488 			    arsta->addr, arvif->vdev_id, ret);
6489 		return ret;
6490 	}
6491 
6492 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
6493 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6494 			    arsta->addr, arvif->vdev_id);
6495 		return -ETIMEDOUT;
6496 	}
6497 
6498 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
6499 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
6500 	num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask);
6501 
6502 	/* If single VHT/HE/EHT rate is configured (by set_bitrate_mask()),
6503 	 * peer_assoc will disable VHT/HE/EHT. This is now enabled by a peer
6504 	 * specific fixed param.
6505 	 * Note that all other rates and NSS will be disabled for this peer.
6506 	 */
6507 	link_sta = ath12k_mac_get_link_sta(arsta);
6508 	if (!link_sta) {
6509 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
6510 		return -EINVAL;
6511 	}
6512 
6513 	spin_lock_bh(&ar->data_lock);
6514 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
6515 	arsta->bw_prev = link_sta->bandwidth;
6516 	spin_unlock_bh(&ar->data_lock);
6517 
6518 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6519 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
6520 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6521 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6522 		if (ret)
6523 			return ret;
6524 	} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6525 		ret = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta, mask, band);
6526 		if (ret)
6527 			return ret;
6528 	}
6529 
6530 	/* Re-assoc is run only to update supported rates for given station. It
6531 	 * doesn't make much sense to reconfigure the peer completely.
6532 	 */
6533 	if (reassoc)
6534 		return 0;
6535 
6536 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
6537 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
6538 	if (ret) {
6539 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
6540 			    arvif->vdev_id, ret);
6541 		return ret;
6542 	}
6543 
6544 	if (!sta->wme) {
6545 		arvif->num_legacy_stations++;
6546 		ret = ath12k_recalc_rtscts_prot(arvif);
6547 		if (ret)
6548 			return ret;
6549 	}
6550 
6551 	if (sta->wme && sta->uapsd_queues) {
6552 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
6553 		if (ret) {
6554 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
6555 				    arsta->addr, arvif->vdev_id, ret);
6556 			return ret;
6557 		}
6558 	}
6559 
6560 	return 0;
6561 }
6562 
ath12k_mac_station_disassoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6563 static int ath12k_mac_station_disassoc(struct ath12k *ar,
6564 				       struct ath12k_link_vif *arvif,
6565 				       struct ath12k_link_sta *arsta)
6566 {
6567 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6568 
6569 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6570 
6571 	if (!sta->wme) {
6572 		arvif->num_legacy_stations--;
6573 		return ath12k_recalc_rtscts_prot(arvif);
6574 	}
6575 
6576 	return 0;
6577 }
6578 
ath12k_sta_rc_update_wk(struct wiphy * wiphy,struct wiphy_work * wk)6579 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
6580 {
6581 	struct ieee80211_link_sta *link_sta;
6582 	struct ath12k *ar;
6583 	struct ath12k_link_vif *arvif;
6584 	struct ieee80211_sta *sta;
6585 	struct cfg80211_chan_def def;
6586 	enum nl80211_band band;
6587 	const u8 *ht_mcs_mask;
6588 	const u16 *vht_mcs_mask;
6589 	const u16 *he_mcs_mask;
6590 	const u16 *eht_mcs_mask;
6591 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
6592 	int err, num_vht_rates, num_he_rates, num_eht_rates;
6593 	const struct cfg80211_bitrate_mask *mask;
6594 	enum wmi_phy_mode peer_phymode;
6595 	struct ath12k_link_sta *arsta;
6596 	struct ieee80211_vif *vif;
6597 
6598 	lockdep_assert_wiphy(wiphy);
6599 
6600 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
6601 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
6602 	arvif = arsta->arvif;
6603 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6604 	ar = arvif->ar;
6605 
6606 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6607 		return;
6608 
6609 	band = def.chan->band;
6610 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6611 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6612 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6613 	eht_mcs_mask = arvif->bitrate_mask.control[band].eht_mcs;
6614 
6615 	spin_lock_bh(&ar->data_lock);
6616 
6617 	changed = arsta->changed;
6618 	arsta->changed = 0;
6619 
6620 	bw = arsta->bw;
6621 	bw_prev = arsta->bw_prev;
6622 	nss = arsta->nss;
6623 	smps = arsta->smps;
6624 
6625 	spin_unlock_bh(&ar->data_lock);
6626 
6627 	nss = max_t(u32, 1, nss);
6628 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6629 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6630 		       ath12k_mac_max_he_nss(he_mcs_mask));
6631 	mac_nss = max(mac_nss, ath12k_mac_max_eht_nss(eht_mcs_mask));
6632 	nss = min(nss, mac_nss);
6633 
6634 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6635 					kzalloc_obj(*peer_arg);
6636 	if (!peer_arg)
6637 		return;
6638 
6639 	if (changed & IEEE80211_RC_BW_CHANGED) {
6640 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6641 		peer_phymode = peer_arg->peer_phymode;
6642 
6643 		if (bw > bw_prev) {
6644 			/* Phymode shows maximum supported channel width, if we
6645 			 * upgrade bandwidth then due to sanity check of firmware,
6646 			 * we have to send WMI_PEER_PHYMODE followed by
6647 			 * WMI_PEER_CHWIDTH
6648 			 */
6649 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6650 				   arsta->addr, bw, bw_prev);
6651 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6652 							arvif->vdev_id, WMI_PEER_PHYMODE,
6653 							peer_phymode);
6654 			if (err) {
6655 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6656 					    arsta->addr, peer_phymode, err);
6657 				return;
6658 			}
6659 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6660 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6661 							bw);
6662 			if (err)
6663 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6664 					    arsta->addr, bw, err);
6665 		} else {
6666 			/* When we downgrade bandwidth this will conflict with phymode
6667 			 * and cause to trigger firmware crash. In this case we send
6668 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6669 			 */
6670 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6671 				   arsta->addr, bw, bw_prev);
6672 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6673 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6674 							bw);
6675 			if (err) {
6676 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6677 					    arsta->addr, bw, err);
6678 				return;
6679 			}
6680 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6681 							arvif->vdev_id, WMI_PEER_PHYMODE,
6682 							peer_phymode);
6683 			if (err)
6684 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6685 					    arsta->addr, peer_phymode, err);
6686 		}
6687 	}
6688 
6689 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6690 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6691 			   arsta->addr, nss);
6692 
6693 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6694 						WMI_PEER_NSS, nss);
6695 		if (err)
6696 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6697 				    arsta->addr, nss, err);
6698 	}
6699 
6700 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6701 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6702 			   arsta->addr, smps);
6703 
6704 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6705 						WMI_PEER_MIMO_PS_STATE, smps);
6706 		if (err)
6707 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6708 				    arsta->addr, smps, err);
6709 	}
6710 
6711 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6712 		mask = &arvif->bitrate_mask;
6713 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6714 								      mask);
6715 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6716 								    mask);
6717 		num_eht_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
6718 								      mask);
6719 
6720 		/* Peer_assoc_prepare will reject vht rates in
6721 		 * bitrate_mask if its not available in range format and
6722 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6723 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6724 		 * But, Single rate in VHT mask can be set as per-peer
6725 		 * fixed rate. But even if any HT rates are configured in
6726 		 * the bitrate mask, device will not switch to those rates
6727 		 * when per-peer Fixed rate is set.
6728 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6729 		 * across HT/VHT and for multiple VHT MCS support.
6730 		 */
6731 		link_sta = ath12k_mac_get_link_sta(arsta);
6732 		if (!link_sta) {
6733 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6734 				    sta->addr, arsta->link_id);
6735 			return;
6736 		}
6737 
6738 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6739 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6740 							   band);
6741 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6742 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6743 		} else if (link_sta->eht_cap.has_eht && num_eht_rates == 1) {
6744 			err = ath12k_mac_set_peer_eht_fixed_rate(arvif, arsta,
6745 								 mask, band);
6746 			if (err) {
6747 				ath12k_warn(ar->ab,
6748 					    "failed to set peer EHT fixed rate for STA %pM ret %d\n",
6749 					    arsta->addr, err);
6750 				return;
6751 			}
6752 		} else {
6753 			/* If the peer is non-VHT/HE/EHT or no fixed VHT/HE/EHT
6754 			 * rate is provided in the new bitrate mask we set the
6755 			 * other rates using peer_assoc command. Also clear
6756 			 * the peer fixed rate settings as it has higher proprity
6757 			 * than peer assoc
6758 			 */
6759 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6760 							arvif->vdev_id,
6761 							WMI_PEER_PARAM_FIXED_RATE,
6762 							WMI_FIXED_RATE_NONE);
6763 			if (err)
6764 				ath12k_warn(ar->ab,
6765 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6766 					    arsta->addr, err);
6767 
6768 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6769 						  peer_arg, true);
6770 
6771 			peer_arg->is_assoc = false;
6772 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6773 			if (err)
6774 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6775 					    arsta->addr, arvif->vdev_id, err);
6776 
6777 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6778 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6779 					    arsta->addr, arvif->vdev_id);
6780 		}
6781 	}
6782 }
6783 
ath12k_mac_free_unassign_link_sta(struct ath12k_hw * ah,struct ath12k_sta * ahsta,u8 link_id)6784 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6785 					      struct ath12k_sta *ahsta,
6786 					      u8 link_id)
6787 {
6788 	struct ath12k_link_sta *arsta;
6789 
6790 	lockdep_assert_wiphy(ah->hw->wiphy);
6791 
6792 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6793 		return;
6794 
6795 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6796 	if (WARN_ON(!arsta))
6797 		return;
6798 
6799 	ahsta->links_map &= ~BIT(link_id);
6800 	ahsta->free_logical_link_idx_map |= BIT(arsta->link_idx);
6801 
6802 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6803 	synchronize_rcu();
6804 
6805 	if (arsta == &ahsta->deflink) {
6806 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6807 		arsta->ahsta = NULL;
6808 		arsta->arvif = NULL;
6809 		return;
6810 	}
6811 
6812 	kfree(arsta);
6813 }
6814 
ath12k_mac_inc_num_stations(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6815 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6816 				       struct ath12k_link_sta *arsta)
6817 {
6818 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6819 	struct ath12k *ar = arvif->ar;
6820 
6821 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6822 
6823 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6824 		return 0;
6825 
6826 	if (ar->num_stations >= ar->max_num_stations)
6827 		return -ENOBUFS;
6828 
6829 	ar->num_stations++;
6830 	arvif->num_stations++;
6831 
6832 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6833 		   "mac station %pM connected to vdev %u num_stations %u\n",
6834 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6835 
6836 	return 0;
6837 }
6838 
ath12k_mac_dec_num_stations(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6839 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6840 					struct ath12k_link_sta *arsta)
6841 {
6842 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6843 	struct ath12k *ar = arvif->ar;
6844 
6845 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6846 
6847 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6848 		return;
6849 
6850 	ar->num_stations--;
6851 
6852 	if (arvif->num_stations) {
6853 		arvif->num_stations--;
6854 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6855 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6856 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6857 	} else {
6858 		ath12k_warn(ar->ab,
6859 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6860 			    arsta->addr, arvif->vdev_id);
6861 	}
6862 }
6863 
ath12k_mac_station_post_remove(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6864 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6865 					   struct ath12k_link_vif *arvif,
6866 					   struct ath12k_link_sta *arsta)
6867 {
6868 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6869 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6870 	struct ath12k_dp_link_peer *peer;
6871 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6872 
6873 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6874 
6875 	ath12k_mac_dec_num_stations(arvif, arsta);
6876 
6877 	spin_lock_bh(&dp->dp_lock);
6878 
6879 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6880 							 arsta->addr);
6881 	if (peer && peer->sta == sta) {
6882 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6883 			    vif->addr, arvif->vdev_id);
6884 		peer->sta = NULL;
6885 
6886 		ath12k_dp_link_peer_free(peer);
6887 		ar->num_peers--;
6888 	}
6889 
6890 	spin_unlock_bh(&dp->dp_lock);
6891 }
6892 
ath12k_mac_station_unauthorize(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6893 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6894 					  struct ath12k_link_vif *arvif,
6895 					  struct ath12k_link_sta *arsta)
6896 {
6897 	struct ath12k_dp_link_peer *peer;
6898 	int ret;
6899 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6900 
6901 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6902 
6903 	spin_lock_bh(&dp->dp_lock);
6904 
6905 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6906 							 arsta->addr);
6907 	if (peer)
6908 		peer->is_authorized = false;
6909 
6910 	spin_unlock_bh(&dp->dp_lock);
6911 
6912 	/* Driver must clear the keys during the state change from
6913 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6914 	 * returning from here, mac80211 is going to delete the keys
6915 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6916 	 * not retain stale key references after mac80211 deletes the keys.
6917 	 */
6918 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6919 	if (ret) {
6920 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6921 			    arvif->vdev_id, ret);
6922 		return ret;
6923 	}
6924 
6925 	return 0;
6926 }
6927 
ath12k_mac_station_authorize(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6928 static int ath12k_mac_station_authorize(struct ath12k *ar,
6929 					struct ath12k_link_vif *arvif,
6930 					struct ath12k_link_sta *arsta)
6931 {
6932 	struct ath12k_dp_link_peer *peer;
6933 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6934 	int ret;
6935 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
6936 
6937 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6938 
6939 	spin_lock_bh(&dp->dp_lock);
6940 
6941 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
6942 							 arsta->addr);
6943 	if (peer)
6944 		peer->is_authorized = true;
6945 
6946 	spin_unlock_bh(&dp->dp_lock);
6947 
6948 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
6949 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6950 						arvif->vdev_id,
6951 						WMI_PEER_AUTHORIZE,
6952 						1);
6953 		if (ret) {
6954 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
6955 				    arsta->addr, arvif->vdev_id, ret);
6956 			return ret;
6957 		}
6958 	}
6959 
6960 	return 0;
6961 }
6962 
ath12k_mac_station_remove(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6963 static int ath12k_mac_station_remove(struct ath12k *ar,
6964 				     struct ath12k_link_vif *arvif,
6965 				     struct ath12k_link_sta *arsta)
6966 {
6967 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6968 	struct ath12k_vif *ahvif = arvif->ahvif;
6969 	int ret = 0;
6970 	struct ath12k_link_sta *temp_arsta;
6971 
6972 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6973 
6974 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
6975 
6976 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6977 		ath12k_bss_disassoc(ar, arvif);
6978 		ret = ath12k_mac_vdev_stop(arvif);
6979 		if (ret)
6980 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
6981 				    arvif->vdev_id, ret);
6982 	}
6983 
6984 	if (sta->mlo)
6985 		return ret;
6986 
6987 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
6988 
6989 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6990 	if (ret)
6991 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
6992 			    arsta->addr, arvif->vdev_id);
6993 	else
6994 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
6995 			   arsta->addr, arvif->vdev_id);
6996 
6997 	ath12k_mac_station_post_remove(ar, arvif, arsta);
6998 
6999 	spin_lock_bh(&ar->ab->base_lock);
7000 
7001 	/* To handle roaming and split phy scenario */
7002 	temp_arsta = ath12k_link_sta_find_by_addr(ar->ab, arsta->addr);
7003 	if (temp_arsta && temp_arsta->arvif->ar == ar)
7004 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7005 
7006 	spin_unlock_bh(&ar->ab->base_lock);
7007 
7008 	if (sta->valid_links)
7009 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
7010 						  arsta->ahsta, arsta->link_id);
7011 
7012 	return ret;
7013 }
7014 
ath12k_mac_station_add(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)7015 static int ath12k_mac_station_add(struct ath12k *ar,
7016 				  struct ath12k_link_vif *arvif,
7017 				  struct ath12k_link_sta *arsta)
7018 {
7019 	struct ath12k_base *ab = ar->ab;
7020 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7021 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
7022 	struct ath12k_wmi_peer_create_arg peer_param = {};
7023 	int ret;
7024 	struct ath12k_link_sta *temp_arsta;
7025 
7026 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7027 
7028 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
7029 	if (ret) {
7030 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
7031 			    ar->max_num_stations);
7032 		goto exit;
7033 	}
7034 
7035 	spin_lock_bh(&ab->base_lock);
7036 
7037 	/*
7038 	 * In case of Split PHY and roaming scenario, pdev idx
7039 	 * might differ but both the pdev will share same rhash
7040 	 * table. In that case update the rhash table if link_sta is
7041 	 * already present
7042 	 */
7043 	temp_arsta = ath12k_link_sta_find_by_addr(ab, arsta->addr);
7044 	if (temp_arsta && temp_arsta->arvif->ar != ar)
7045 		ath12k_link_sta_rhash_delete(ab, temp_arsta);
7046 
7047 	ret = ath12k_link_sta_rhash_add(ab, arsta);
7048 	spin_unlock_bh(&ab->base_lock);
7049 	if (ret) {
7050 		ath12k_warn(ab, "Failed to add arsta: %pM to hash table, ret: %d",
7051 			    arsta->addr, ret);
7052 		goto dec_num_station;
7053 	}
7054 
7055 	peer_param.vdev_id = arvif->vdev_id;
7056 	peer_param.peer_addr = arsta->addr;
7057 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
7058 	peer_param.ml_enabled = sta->mlo;
7059 
7060 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
7061 	if (ret) {
7062 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
7063 			    arsta->addr, arvif->vdev_id);
7064 		goto free_peer;
7065 	}
7066 
7067 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
7068 		   arsta->addr, arvif->vdev_id);
7069 
7070 	if (ieee80211_vif_is_mesh(vif)) {
7071 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
7072 						arvif->vdev_id,
7073 						WMI_PEER_USE_4ADDR, 1);
7074 		if (ret) {
7075 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
7076 				    arsta->addr, ret);
7077 			goto free_peer;
7078 		}
7079 	}
7080 
7081 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
7082 	if (ret) {
7083 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
7084 			    arsta->addr, arvif->vdev_id, ret);
7085 		goto free_peer;
7086 	}
7087 
7088 	if (ab->hw_params->vdev_start_delay &&
7089 	    !arvif->is_started &&
7090 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
7091 		ret = ath12k_start_vdev_delay(ar, arvif);
7092 		if (ret) {
7093 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
7094 			goto free_peer;
7095 		}
7096 	}
7097 
7098 	return 0;
7099 
7100 free_peer:
7101 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
7102 	spin_lock_bh(&ab->base_lock);
7103 	ath12k_link_sta_rhash_delete(ab, arsta);
7104 	spin_unlock_bh(&ab->base_lock);
7105 dec_num_station:
7106 	ath12k_mac_dec_num_stations(arvif, arsta);
7107 exit:
7108 	return ret;
7109 }
7110 
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)7111 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
7112 				      struct ath12k_sta *ahsta,
7113 				      struct ath12k_link_sta *arsta,
7114 				      struct ath12k_vif *ahvif,
7115 				      u8 link_id)
7116 {
7117 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7118 	struct ieee80211_link_sta *link_sta;
7119 	struct ath12k_link_vif *arvif;
7120 	int link_idx;
7121 
7122 	lockdep_assert_wiphy(ah->hw->wiphy);
7123 
7124 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7125 		return -EINVAL;
7126 
7127 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7128 	if (!arvif)
7129 		return -EINVAL;
7130 
7131 	memset(arsta, 0, sizeof(*arsta));
7132 
7133 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
7134 	if (!link_sta)
7135 		return -EINVAL;
7136 
7137 	ether_addr_copy(arsta->addr, link_sta->addr);
7138 
7139 	if (!ahsta->free_logical_link_idx_map)
7140 		return -ENOSPC;
7141 
7142 	/*
7143 	 * Allocate a logical link index by selecting the first available bit
7144 	 * from the free logical index map
7145 	 */
7146 	link_idx = __ffs(ahsta->free_logical_link_idx_map);
7147 	ahsta->free_logical_link_idx_map &= ~BIT(link_idx);
7148 	arsta->link_idx = link_idx;
7149 
7150 	arsta->link_id = link_id;
7151 	ahsta->links_map |= BIT(arsta->link_id);
7152 	arsta->arvif = arvif;
7153 	arsta->ahsta = ahsta;
7154 	ahsta->ahvif = ahvif;
7155 
7156 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
7157 
7158 	rcu_assign_pointer(ahsta->link[link_id], arsta);
7159 
7160 	return 0;
7161 }
7162 
ath12k_mac_ml_station_remove(struct ath12k_vif * ahvif,struct ath12k_sta * ahsta)7163 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
7164 					 struct ath12k_sta *ahsta)
7165 {
7166 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
7167 	struct ath12k_hw *ah = ahvif->ah;
7168 	struct ath12k_link_vif *arvif;
7169 	struct ath12k_link_sta *arsta;
7170 	unsigned long links;
7171 	struct ath12k *ar;
7172 	u8 link_id;
7173 
7174 	lockdep_assert_wiphy(ah->hw->wiphy);
7175 
7176 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
7177 
7178 	/* validate link station removal and clear arsta links */
7179 	links = ahsta->links_map;
7180 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7181 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
7182 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7183 		if (!arvif || !arsta)
7184 			continue;
7185 
7186 		ar = arvif->ar;
7187 
7188 		ath12k_mac_station_post_remove(ar, arvif, arsta);
7189 
7190 		spin_lock_bh(&ar->ab->base_lock);
7191 		ath12k_link_sta_rhash_delete(ar->ab, arsta);
7192 		spin_unlock_bh(&ar->ab->base_lock);
7193 
7194 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7195 	}
7196 
7197 	if (sta->mlo) {
7198 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7199 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7200 	}
7201 }
7202 
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)7203 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
7204 					    struct ath12k_link_vif *arvif,
7205 					    struct ath12k_link_sta *arsta,
7206 					    enum ieee80211_sta_state old_state,
7207 					    enum ieee80211_sta_state new_state)
7208 {
7209 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
7210 	struct ieee80211_bss_conf *link_conf;
7211 	struct ath12k *ar = arvif->ar;
7212 	struct ath12k_reg_info *reg_info;
7213 	struct ath12k_base *ab = ar->ab;
7214 	int ret = 0;
7215 
7216 	lockdep_assert_wiphy(hw->wiphy);
7217 
7218 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
7219 		   arsta->link_id, arsta->addr, old_state, new_state);
7220 
7221 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
7222 	 * from driver
7223 	 */
7224 	if ((old_state == IEEE80211_STA_NONE &&
7225 	     new_state == IEEE80211_STA_NOTEXIST)) {
7226 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
7227 		if (ret) {
7228 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
7229 				    arsta->addr, arvif->vdev_id);
7230 			goto exit;
7231 		}
7232 	}
7233 
7234 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
7235 	if (old_state == IEEE80211_STA_NOTEXIST &&
7236 	    new_state == IEEE80211_STA_NONE) {
7237 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7238 		if (ret)
7239 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
7240 				    arsta->addr, arvif->vdev_id);
7241 
7242 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
7243 	 * peer associated to AP/Mesh/ADHOC vif type.
7244 	 */
7245 	} else if (old_state == IEEE80211_STA_AUTH &&
7246 		   new_state == IEEE80211_STA_ASSOC &&
7247 		   (vif->type == NL80211_IFTYPE_AP ||
7248 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7249 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7250 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
7251 		if (ret)
7252 			ath12k_warn(ab, "Failed to associate station: %pM\n",
7253 				    arsta->addr);
7254 
7255 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
7256 	 * authorized
7257 	 */
7258 	} else if (old_state == IEEE80211_STA_ASSOC &&
7259 		   new_state == IEEE80211_STA_AUTHORIZED) {
7260 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
7261 		if (ret) {
7262 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
7263 				    arsta->addr);
7264 			goto exit;
7265 		}
7266 
7267 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
7268 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
7269 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
7270 			reg_info = ab->reg_info[ar->pdev_idx];
7271 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
7272 			ath12k_hw_to_ah(hw)->regd_updated = false;
7273 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
7274 						    link_conf->power_type);
7275 		}
7276 
7277 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
7278 	 * deauthorize it.
7279 	 */
7280 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
7281 		   new_state == IEEE80211_STA_ASSOC) {
7282 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
7283 
7284 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
7285 	 * AP/mesh/ADHOC vif type.
7286 	 */
7287 	} else if (old_state == IEEE80211_STA_ASSOC &&
7288 		   new_state == IEEE80211_STA_AUTH &&
7289 		   (vif->type == NL80211_IFTYPE_AP ||
7290 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7291 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7292 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
7293 		if (ret)
7294 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
7295 				    arsta->addr);
7296 	}
7297 
7298 exit:
7299 	return ret;
7300 }
7301 
ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg * freq_range,u32 freq,u8 mac_id)7302 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
7303 				      u32 freq, u8 mac_id)
7304 {
7305 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
7306 		freq <= freq_range[mac_id].high_2ghz_freq) ||
7307 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
7308 		freq <= freq_range[mac_id].high_5ghz_freq);
7309 }
7310 
7311 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)7312 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
7313 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
7314 					 u32 freq_link1, u32 freq_link2)
7315 {
7316 	u8 i;
7317 
7318 	for (i = 0; i < MAX_RADIOS; i++) {
7319 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
7320 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
7321 			return true;
7322 	}
7323 
7324 	return false;
7325 }
7326 
ath12k_mac_is_hw_dbs_capable(struct ath12k_base * ab)7327 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
7328 {
7329 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7330 			ab->wmi_ab.svc_map) &&
7331 	       ab->wmi_ab.hw_mode_info.support_dbs;
7332 }
7333 
ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)7334 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
7335 					      u32 freq_link1, u32 freq_link2)
7336 {
7337 	struct ath12k_hw_mode_freq_range_arg *freq_range;
7338 
7339 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7340 		return true;
7341 
7342 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
7343 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
7344 							freq_link1, freq_link2);
7345 }
7346 
ath12k_mac_is_hw_sbs_capable(struct ath12k_base * ab)7347 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
7348 {
7349 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
7350 			ab->wmi_ab.svc_map) &&
7351 	       ab->wmi_ab.hw_mode_info.support_sbs;
7352 }
7353 
ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)7354 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
7355 					      u32 freq_link1, u32 freq_link2)
7356 {
7357 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
7358 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
7359 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
7360 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
7361 
7362 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7363 		return true;
7364 
7365 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
7366 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
7367 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
7368 
7369 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
7370 								freq_link1, freq_link2) ||
7371 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
7372 								freq_link1, freq_link2);
7373 	}
7374 
7375 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
7376 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
7377 							freq_link1, freq_link2);
7378 }
7379 
ath12k_mac_freqs_on_same_mac(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)7380 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
7381 					 u32 freq_link1, u32 freq_link2)
7382 {
7383 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
7384 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
7385 }
7386 
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)7387 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
7388 					      enum wmi_mlo_link_force_reason reason,
7389 					      enum wmi_mlo_link_force_mode mode,
7390 					      u8 *mlo_vdev_id_lst,
7391 					      u8 num_mlo_vdev,
7392 					      u8 *mlo_inactive_vdev_lst,
7393 					      u8 num_mlo_inactive_vdev)
7394 {
7395 	struct wmi_mlo_link_set_active_arg param = {};
7396 	u32 entry_idx, entry_offset, vdev_idx;
7397 	u8 vdev_id;
7398 
7399 	param.reason = reason;
7400 	param.force_mode = mode;
7401 
7402 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
7403 		vdev_id = mlo_vdev_id_lst[vdev_idx];
7404 		entry_idx = vdev_id / 32;
7405 		entry_offset = vdev_id % 32;
7406 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7407 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7408 				    entry_idx, num_mlo_vdev, vdev_id);
7409 			return -EINVAL;
7410 		}
7411 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
7412 		/* update entry number if entry index changed */
7413 		if (param.num_vdev_bitmap < entry_idx + 1)
7414 			param.num_vdev_bitmap = entry_idx + 1;
7415 	}
7416 
7417 	ath12k_dbg(ab, ATH12K_DBG_MAC,
7418 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
7419 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
7420 
7421 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
7422 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
7423 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
7424 			entry_idx = vdev_id / 32;
7425 			entry_offset = vdev_id % 32;
7426 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
7427 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
7428 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
7429 				return -EINVAL;
7430 			}
7431 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
7432 			/* update entry number if entry index changed */
7433 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
7434 				param.num_inactive_vdev_bitmap = entry_idx + 1;
7435 		}
7436 
7437 		ath12k_dbg(ab, ATH12K_DBG_MAC,
7438 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
7439 			   param.num_inactive_vdev_bitmap,
7440 			   param.inactive_vdev_bitmap[0],
7441 			   param.inactive_vdev_bitmap[1]);
7442 	}
7443 
7444 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
7445 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
7446 		param.num_link_entry = 1;
7447 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
7448 	}
7449 
7450 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
7451 }
7452 
ath12k_mac_mlo_sta_update_link_active(struct ath12k_base * ab,struct ieee80211_hw * hw,struct ath12k_vif * ahvif)7453 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
7454 						 struct ieee80211_hw *hw,
7455 						 struct ath12k_vif *ahvif)
7456 {
7457 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7458 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
7459 	unsigned long links = ahvif->links_map;
7460 	enum wmi_mlo_link_force_reason reason;
7461 	struct ieee80211_chanctx_conf *conf;
7462 	enum wmi_mlo_link_force_mode mode;
7463 	struct ieee80211_bss_conf *info;
7464 	struct ath12k_link_vif *arvif;
7465 	u8 num_mlo_vdev = 0;
7466 	u8 link_id;
7467 
7468 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
7469 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7470 		/* make sure vdev is created on this device */
7471 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
7472 			continue;
7473 
7474 		info = ath12k_mac_get_link_bss_conf(arvif);
7475 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
7476 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
7477 
7478 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
7479 		num_mlo_vdev++;
7480 	}
7481 
7482 	/* It is not allowed to activate more links than a single device
7483 	 * supported. Something goes wrong if we reach here.
7484 	 */
7485 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7486 		WARN_ON_ONCE(1);
7487 		return -EINVAL;
7488 	}
7489 
7490 	/* if 2 links are established and both link channels fall on the
7491 	 * same hardware MAC, send command to firmware to deactivate one
7492 	 * of them.
7493 	 */
7494 	if (num_mlo_vdev == 2 &&
7495 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
7496 					 mlo_freq_list[1])) {
7497 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
7498 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
7499 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
7500 							  mlo_vdev_id_lst, num_mlo_vdev,
7501 							  NULL, 0);
7502 	}
7503 
7504 	return 0;
7505 }
7506 
ath12k_mac_are_sbs_chan(struct ath12k_base * ab,u32 freq_1,u32 freq_2)7507 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7508 {
7509 	if (!ath12k_mac_is_hw_sbs_capable(ab))
7510 		return false;
7511 
7512 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
7513 	    ath12k_is_2ghz_channel_freq(freq_2))
7514 		return false;
7515 
7516 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
7517 }
7518 
ath12k_mac_are_dbs_chan(struct ath12k_base * ab,u32 freq_1,u32 freq_2)7519 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
7520 {
7521 	if (!ath12k_mac_is_hw_dbs_capable(ab))
7522 		return false;
7523 
7524 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
7525 }
7526 
ath12k_mac_select_links(struct ath12k_base * ab,struct ieee80211_vif * vif,struct ieee80211_hw * hw,u16 * selected_links)7527 static int ath12k_mac_select_links(struct ath12k_base *ab,
7528 				   struct ieee80211_vif *vif,
7529 				   struct ieee80211_hw *hw,
7530 				   u16 *selected_links)
7531 {
7532 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
7533 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7534 	u8 num_useful_links = hweight_long(useful_links);
7535 	struct ieee80211_chanctx_conf *chanctx;
7536 	struct ath12k_link_vif *assoc_arvif;
7537 	u32 assoc_link_freq, partner_freq;
7538 	u16 sbs_links = 0, dbs_links = 0;
7539 	struct ieee80211_bss_conf *info;
7540 	struct ieee80211_channel *chan;
7541 	struct ieee80211_sta *sta;
7542 	struct ath12k_sta *ahsta;
7543 	u8 link_id;
7544 
7545 	/* activate all useful links if less than max supported */
7546 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
7547 		*selected_links = useful_links;
7548 		return 0;
7549 	}
7550 
7551 	/* only in station mode we can get here, so it's safe
7552 	 * to use ap_addr
7553 	 */
7554 	rcu_read_lock();
7555 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
7556 	if (!sta) {
7557 		rcu_read_unlock();
7558 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
7559 		return -EINVAL;
7560 	}
7561 
7562 	ahsta = ath12k_sta_to_ahsta(sta);
7563 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
7564 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
7565 	chanctx = rcu_dereference(info->chanctx_conf);
7566 	assoc_link_freq = chanctx->def.chan->center_freq;
7567 	rcu_read_unlock();
7568 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
7569 		   assoc_arvif->link_id, assoc_link_freq);
7570 
7571 	/* assoc link is already activated and has to be kept active,
7572 	 * only need to select a partner link from others.
7573 	 */
7574 	useful_links &= ~BIT(assoc_arvif->link_id);
7575 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7576 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
7577 		if (!info) {
7578 			ath12k_warn(ab, "failed to get link info for link: %u\n",
7579 				    link_id);
7580 			return -ENOLINK;
7581 		}
7582 
7583 		chan = info->chanreq.oper.chan;
7584 		if (!chan) {
7585 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
7586 			return -EINVAL;
7587 		}
7588 
7589 		partner_freq = chan->center_freq;
7590 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
7591 			sbs_links |= BIT(link_id);
7592 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
7593 				   link_id, partner_freq);
7594 			continue;
7595 		}
7596 
7597 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
7598 			dbs_links |= BIT(link_id);
7599 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
7600 				   link_id, partner_freq);
7601 			continue;
7602 		}
7603 
7604 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
7605 			   link_id, partner_freq);
7606 	}
7607 
7608 	/* choose the first candidate no matter how many is in the list */
7609 	if (sbs_links)
7610 		link_id = __ffs(sbs_links);
7611 	else if (dbs_links)
7612 		link_id = __ffs(dbs_links);
7613 	else
7614 		link_id = ffs(useful_links) - 1;
7615 
7616 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
7617 
7618 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
7619 
7620 	return 0;
7621 }
7622 
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)7623 int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
7624 			    struct ieee80211_vif *vif,
7625 			    struct ieee80211_sta *sta,
7626 			    enum ieee80211_sta_state old_state,
7627 			    enum ieee80211_sta_state new_state)
7628 {
7629 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7630 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7631 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7632 	struct ath12k_base *prev_ab = NULL, *ab;
7633 	struct ath12k_link_vif *arvif;
7634 	struct ath12k_link_sta *arsta;
7635 	unsigned long valid_links;
7636 	u16 selected_links = 0;
7637 	u8 link_id = 0, i;
7638 	struct ath12k *ar;
7639 	int ret = -EINVAL;
7640 	struct ath12k_dp_peer_create_params dp_params = {};
7641 
7642 	lockdep_assert_wiphy(hw->wiphy);
7643 
7644 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
7645 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
7646 		link_id = ffs(sta->valid_links) - 1;
7647 	}
7648 
7649 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
7650 	 * New station add received. If this is a ML station then
7651 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
7652 	 * Assign default link to the first link sta.
7653 	 */
7654 	if (old_state == IEEE80211_STA_NOTEXIST &&
7655 	    new_state == IEEE80211_STA_NONE) {
7656 		memset(ahsta, 0, sizeof(*ahsta));
7657 		ahsta->free_logical_link_idx_map = U16_MAX;
7658 
7659 		arsta = &ahsta->deflink;
7660 
7661 		/* ML sta */
7662 		if (sta->mlo && !ahsta->links_map &&
7663 		    (hweight16(sta->valid_links) == 1)) {
7664 			ahsta->ml_peer_id = ath12k_peer_ml_alloc(ah);
7665 			if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7666 				ath12k_hw_warn(ah, "unable to allocate ML peer id for sta %pM",
7667 					       sta->addr);
7668 				goto exit;
7669 			}
7670 
7671 			dp_params.is_mlo = true;
7672 			dp_params.peer_id = ahsta->ml_peer_id | ATH12K_PEER_ML_ID_VALID;
7673 		}
7674 
7675 		dp_params.sta = sta;
7676 
7677 		if (vif->type == NL80211_IFTYPE_AP)
7678 			dp_params.ucast_ra_only = true;
7679 
7680 		ret = ath12k_dp_peer_create(&ah->dp_hw, sta->addr, &dp_params);
7681 		if (ret) {
7682 			ath12k_hw_warn(ah, "unable to create ath12k_dp_peer for sta %pM, ret: %d",
7683 				       sta->addr, ret);
7684 
7685 			goto ml_peer_id_clear;
7686 		}
7687 
7688 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7689 						 link_id);
7690 		if (ret) {
7691 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7692 				       link_id, sta->addr);
7693 			goto peer_delete;
7694 		}
7695 
7696 		/* above arsta will get memset, hence do this after assign
7697 		 * link sta
7698 		 */
7699 		if (sta->mlo) {
7700 			/* For station mode, arvif->is_sta_assoc_link has been set when
7701 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7702 			 */
7703 			if (vif->type == NL80211_IFTYPE_STATION &&
7704 			    !arsta->arvif->is_sta_assoc_link) {
7705 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7706 					       link_id);
7707 				ret = -EINVAL;
7708 				goto exit;
7709 			}
7710 
7711 			arsta->is_assoc_link = true;
7712 			ahsta->assoc_link_id = link_id;
7713 		}
7714 	}
7715 
7716 	/* In the ML station scenario, activate all partner links once the
7717 	 * client is transitioning to the associated state.
7718 	 *
7719 	 * FIXME: Ideally, this activation should occur when the client
7720 	 * transitions to the authorized state. However, there are some
7721 	 * issues with handling this in the firmware. Until the firmware
7722 	 * can manage it properly, activate the links when the client is
7723 	 * about to move to the associated state.
7724 	 */
7725 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7726 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7727 		/* TODO: for now only do link selection for single device
7728 		 * MLO case. Other cases would be handled in the future.
7729 		 */
7730 		ab = ah->radio[0].ab;
7731 		if (ab->ag->num_devices == 1) {
7732 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7733 			if (ret) {
7734 				ath12k_warn(ab,
7735 					    "failed to get selected links: %d\n", ret);
7736 				goto exit;
7737 			}
7738 		} else {
7739 			selected_links = ieee80211_vif_usable_links(vif);
7740 		}
7741 
7742 		ieee80211_set_active_links(vif, selected_links);
7743 	}
7744 
7745 	/* Handle all the other state transitions in generic way */
7746 	valid_links = ahsta->links_map;
7747 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7748 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7749 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7750 		/* some assumptions went wrong! */
7751 		if (WARN_ON(!arvif || !arsta))
7752 			continue;
7753 
7754 		/* vdev might be in deleted */
7755 		if (WARN_ON(!arvif->ar))
7756 			continue;
7757 
7758 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7759 						       old_state, new_state);
7760 		if (ret) {
7761 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7762 				       link_id, arsta->addr, old_state, new_state);
7763 
7764 			if (old_state == IEEE80211_STA_NOTEXIST &&
7765 			    new_state == IEEE80211_STA_NONE)
7766 				goto peer_delete;
7767 			else
7768 				goto exit;
7769 		}
7770 	}
7771 
7772 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7773 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7774 		for_each_ar(ah, ar, i) {
7775 			ab = ar->ab;
7776 			if (prev_ab == ab)
7777 				continue;
7778 
7779 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7780 			if (ret) {
7781 				ath12k_warn(ab,
7782 					    "failed to update link active state on connect %d\n",
7783 					    ret);
7784 				goto exit;
7785 			}
7786 
7787 			prev_ab = ab;
7788 		}
7789 	}
7790 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7791 	 * Remove the station from driver (handle ML sta here since that
7792 	 * needs special handling. Normal sta will be handled in generic
7793 	 * handler below
7794 	 */
7795 	if (old_state == IEEE80211_STA_NONE &&
7796 	    new_state == IEEE80211_STA_NOTEXIST) {
7797 		if (sta->mlo)
7798 			ath12k_mac_ml_station_remove(ahvif, ahsta);
7799 
7800 		ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7801 	}
7802 
7803 	ret = 0;
7804 	goto exit;
7805 
7806 peer_delete:
7807 	ath12k_dp_peer_delete(&ah->dp_hw, sta->addr, sta);
7808 ml_peer_id_clear:
7809 	if (sta->mlo) {
7810 		clear_bit(ahsta->ml_peer_id, ah->free_ml_peer_id_map);
7811 		ahsta->ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
7812 	}
7813 exit:
7814 	/* update the state if everything went well */
7815 	if (!ret)
7816 		ahsta->state = new_state;
7817 
7818 	return ret;
7819 }
7820 EXPORT_SYMBOL(ath12k_mac_op_sta_state);
7821 
ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)7822 int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7823 				struct ieee80211_vif *vif,
7824 				struct ieee80211_sta *sta)
7825 {
7826 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7827 	struct ath12k *ar;
7828 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7829 	struct ath12k_link_vif *arvif;
7830 	struct ath12k_link_sta *arsta;
7831 	u8 link_id;
7832 	int ret;
7833 	s16 txpwr;
7834 
7835 	lockdep_assert_wiphy(hw->wiphy);
7836 
7837 	/* TODO: use link id from mac80211 once that's implemented */
7838 	link_id = 0;
7839 
7840 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7841 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7842 
7843 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7844 		txpwr = 0;
7845 	} else {
7846 		txpwr = sta->deflink.txpwr.power;
7847 		if (!txpwr) {
7848 			ret = -EINVAL;
7849 			goto out;
7850 		}
7851 	}
7852 
7853 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7854 		ret = -EINVAL;
7855 		goto out;
7856 	}
7857 
7858 	ar = arvif->ar;
7859 
7860 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7861 					WMI_PEER_USE_FIXED_PWR, txpwr);
7862 	if (ret) {
7863 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7864 			    ret);
7865 		goto out;
7866 	}
7867 
7868 out:
7869 	return ret;
7870 }
7871 EXPORT_SYMBOL(ath12k_mac_op_sta_set_txpwr);
7872 
ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,u32 changed)7873 void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7874 				      struct ieee80211_vif *vif,
7875 				      struct ieee80211_link_sta *link_sta,
7876 				      u32 changed)
7877 {
7878 	struct ieee80211_sta *sta = link_sta->sta;
7879 	struct ath12k *ar;
7880 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7881 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7882 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7883 	struct ath12k_link_sta *arsta;
7884 	struct ath12k_link_vif *arvif;
7885 	struct ath12k_dp_link_peer *peer;
7886 	u32 bw, smps;
7887 	struct ath12k_dp *dp;
7888 
7889 	rcu_read_lock();
7890 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7891 	if (!arvif) {
7892 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7893 			       link_sta->link_id, sta->addr);
7894 		rcu_read_unlock();
7895 		return;
7896 	}
7897 
7898 	ar = arvif->ar;
7899 	dp = ath12k_ab_to_dp(ar->ab);
7900 
7901 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7902 	if (!arsta) {
7903 		rcu_read_unlock();
7904 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7905 			    link_sta->link_id, sta->addr);
7906 		return;
7907 	}
7908 	spin_lock_bh(&dp->dp_lock);
7909 
7910 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arvif->vdev_id,
7911 							 arsta->addr);
7912 	if (!peer) {
7913 		spin_unlock_bh(&dp->dp_lock);
7914 		rcu_read_unlock();
7915 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
7916 			    arsta->addr, arvif->vdev_id);
7917 		return;
7918 	}
7919 
7920 	spin_unlock_bh(&dp->dp_lock);
7921 
7922 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
7923 		rcu_read_unlock();
7924 		return;
7925 	}
7926 
7927 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
7928 	if (!link_sta) {
7929 		rcu_read_unlock();
7930 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
7931 			    sta->addr, arsta->link_id);
7932 		return;
7933 	}
7934 
7935 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
7936 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
7937 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
7938 		   link_sta->smps_mode);
7939 
7940 	spin_lock_bh(&ar->data_lock);
7941 
7942 	if (changed & IEEE80211_RC_BW_CHANGED) {
7943 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
7944 		arsta->bw_prev = arsta->bw;
7945 		arsta->bw = bw;
7946 	}
7947 
7948 	if (changed & IEEE80211_RC_NSS_CHANGED)
7949 		arsta->nss = link_sta->rx_nss;
7950 
7951 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
7952 		smps = WMI_PEER_SMPS_PS_NONE;
7953 
7954 		switch (link_sta->smps_mode) {
7955 		case IEEE80211_SMPS_AUTOMATIC:
7956 		case IEEE80211_SMPS_OFF:
7957 			smps = WMI_PEER_SMPS_PS_NONE;
7958 			break;
7959 		case IEEE80211_SMPS_STATIC:
7960 			smps = WMI_PEER_SMPS_STATIC;
7961 			break;
7962 		case IEEE80211_SMPS_DYNAMIC:
7963 			smps = WMI_PEER_SMPS_DYNAMIC;
7964 			break;
7965 		default:
7966 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
7967 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
7968 			smps = WMI_PEER_SMPS_PS_NONE;
7969 			break;
7970 		}
7971 
7972 		arsta->smps = smps;
7973 	}
7974 
7975 	arsta->changed |= changed;
7976 
7977 	spin_unlock_bh(&ar->data_lock);
7978 
7979 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
7980 
7981 	rcu_read_unlock();
7982 }
7983 EXPORT_SYMBOL(ath12k_mac_op_link_sta_rc_update);
7984 
ath12k_mac_alloc_assign_link_sta(struct ath12k_hw * ah,struct ath12k_sta * ahsta,struct ath12k_vif * ahvif,u8 link_id)7985 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
7986 								struct ath12k_sta *ahsta,
7987 								struct ath12k_vif *ahvif,
7988 								u8 link_id)
7989 {
7990 	struct ath12k_link_sta *arsta;
7991 	int ret;
7992 
7993 	lockdep_assert_wiphy(ah->hw->wiphy);
7994 
7995 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7996 		return NULL;
7997 
7998 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7999 	if (arsta)
8000 		return NULL;
8001 
8002 	arsta = kmalloc_obj(*arsta);
8003 	if (!arsta)
8004 		return NULL;
8005 
8006 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
8007 	if (ret) {
8008 		kfree(arsta);
8009 		return NULL;
8010 	}
8011 
8012 	return arsta;
8013 }
8014 
ath12k_mac_op_change_sta_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 old_links,u16 new_links)8015 int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
8016 				   struct ieee80211_vif *vif,
8017 				   struct ieee80211_sta *sta,
8018 				   u16 old_links, u16 new_links)
8019 {
8020 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8021 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
8022 	struct ath12k_hw *ah = hw->priv;
8023 	struct ath12k_link_vif *arvif;
8024 	struct ath12k_link_sta *arsta;
8025 	unsigned long valid_links;
8026 	struct ath12k *ar;
8027 	u8 link_id;
8028 	int ret;
8029 
8030 	lockdep_assert_wiphy(hw->wiphy);
8031 
8032 	if (!sta->valid_links)
8033 		return -EINVAL;
8034 
8035 	/* Firmware does not support removal of one of link stas. All sta
8036 	 * would be removed during ML STA delete in sta_state(), hence link
8037 	 * sta removal is not handled here.
8038 	 */
8039 	if (new_links < old_links)
8040 		return 0;
8041 
8042 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
8043 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
8044 		return -EINVAL;
8045 	}
8046 
8047 	/* this op is expected only after initial sta insertion with default link */
8048 	if (WARN_ON(ahsta->links_map == 0))
8049 		return -EINVAL;
8050 
8051 	valid_links = new_links;
8052 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
8053 		if (ahsta->links_map & BIT(link_id))
8054 			continue;
8055 
8056 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8057 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
8058 
8059 		if (!arvif || !arsta) {
8060 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
8061 			continue;
8062 		}
8063 
8064 		ar = arvif->ar;
8065 		if (!ar)
8066 			continue;
8067 
8068 		ret = ath12k_mac_station_add(ar, arvif, arsta);
8069 		if (ret) {
8070 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
8071 				    arsta->addr, arvif->vdev_id);
8072 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
8073 			return ret;
8074 		}
8075 	}
8076 
8077 	return 0;
8078 }
8079 EXPORT_SYMBOL(ath12k_mac_op_change_sta_links);
8080 
ath12k_mac_op_can_activate_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 active_links)8081 bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
8082 				      struct ieee80211_vif *vif,
8083 				      u16 active_links)
8084 {
8085 	/* TODO: Handle recovery case */
8086 
8087 	return true;
8088 }
8089 EXPORT_SYMBOL(ath12k_mac_op_can_activate_links);
8090 
ath12k_conf_tx_uapsd(struct ath12k_link_vif * arvif,u16 ac,bool enable)8091 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
8092 				u16 ac, bool enable)
8093 {
8094 	struct ath12k *ar = arvif->ar;
8095 	struct ath12k_vif *ahvif = arvif->ahvif;
8096 	u32 value;
8097 	int ret;
8098 
8099 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
8100 		return 0;
8101 
8102 	switch (ac) {
8103 	case IEEE80211_AC_VO:
8104 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
8105 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
8106 		break;
8107 	case IEEE80211_AC_VI:
8108 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
8109 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
8110 		break;
8111 	case IEEE80211_AC_BE:
8112 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
8113 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
8114 		break;
8115 	case IEEE80211_AC_BK:
8116 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
8117 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
8118 		break;
8119 	}
8120 
8121 	if (enable)
8122 		ahvif->u.sta.uapsd |= value;
8123 	else
8124 		ahvif->u.sta.uapsd &= ~value;
8125 
8126 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8127 					  WMI_STA_PS_PARAM_UAPSD,
8128 					  ahvif->u.sta.uapsd);
8129 	if (ret) {
8130 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
8131 		goto exit;
8132 	}
8133 
8134 	if (ahvif->u.sta.uapsd)
8135 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
8136 	else
8137 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
8138 
8139 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
8140 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
8141 					  value);
8142 	if (ret)
8143 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
8144 
8145 exit:
8146 	return ret;
8147 }
8148 
ath12k_mac_conf_tx(struct ath12k_link_vif * arvif,u16 ac,const struct ieee80211_tx_queue_params * params)8149 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
8150 			      const struct ieee80211_tx_queue_params *params)
8151 {
8152 	struct wmi_wmm_params_arg *p = NULL;
8153 	struct ath12k *ar = arvif->ar;
8154 	struct ath12k_base *ab = ar->ab;
8155 	int ret;
8156 
8157 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8158 
8159 	switch (ac) {
8160 	case IEEE80211_AC_VO:
8161 		p = &arvif->wmm_params.ac_vo;
8162 		break;
8163 	case IEEE80211_AC_VI:
8164 		p = &arvif->wmm_params.ac_vi;
8165 		break;
8166 	case IEEE80211_AC_BE:
8167 		p = &arvif->wmm_params.ac_be;
8168 		break;
8169 	case IEEE80211_AC_BK:
8170 		p = &arvif->wmm_params.ac_bk;
8171 		break;
8172 	}
8173 
8174 	if (WARN_ON(!p)) {
8175 		ret = -EINVAL;
8176 		goto exit;
8177 	}
8178 
8179 	p->cwmin = params->cw_min;
8180 	p->cwmax = params->cw_max;
8181 	p->aifs = params->aifs;
8182 	p->txop = params->txop;
8183 
8184 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
8185 					     &arvif->wmm_params);
8186 	if (ret) {
8187 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
8188 			    ar->pdev_idx, ret);
8189 		goto exit;
8190 	}
8191 
8192 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
8193 	if (ret)
8194 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
8195 			    ar->pdev_idx, ret);
8196 
8197 exit:
8198 	return ret;
8199 }
8200 
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)8201 int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
8202 			  struct ieee80211_vif *vif,
8203 			  unsigned int link_id, u16 ac,
8204 			  const struct ieee80211_tx_queue_params *params)
8205 {
8206 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8207 	struct ath12k_link_vif *arvif;
8208 	struct ath12k_vif_cache *cache;
8209 	int ret;
8210 
8211 	lockdep_assert_wiphy(hw->wiphy);
8212 
8213 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
8214 		return -EINVAL;
8215 
8216 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
8217 	if (!arvif || !arvif->is_created) {
8218 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
8219 		if (!cache)
8220 			return -ENOSPC;
8221 
8222 		cache->tx_conf.changed = true;
8223 		cache->tx_conf.ac = ac;
8224 		cache->tx_conf.tx_queue_params = *params;
8225 
8226 		return 0;
8227 	}
8228 
8229 	ret = ath12k_mac_conf_tx(arvif, ac, params);
8230 
8231 	return ret;
8232 }
8233 EXPORT_SYMBOL(ath12k_mac_op_conf_tx);
8234 
8235 static struct ieee80211_sta_ht_cap
ath12k_create_ht_cap(struct ath12k * ar,u32 ar_ht_cap,u32 rate_cap_rx_chainmask)8236 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
8237 {
8238 	int i;
8239 	struct ieee80211_sta_ht_cap ht_cap = {};
8240 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
8241 
8242 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
8243 		return ht_cap;
8244 
8245 	ht_cap.ht_supported = 1;
8246 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
8247 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
8248 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
8249 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
8250 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
8251 
8252 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
8253 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
8254 
8255 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
8256 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
8257 
8258 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
8259 		u32 smps;
8260 
8261 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
8262 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
8263 
8264 		ht_cap.cap |= smps;
8265 	}
8266 
8267 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
8268 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
8269 
8270 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
8271 		u32 stbc;
8272 
8273 		stbc   = ar_ht_cap;
8274 		stbc  &= WMI_HT_CAP_RX_STBC;
8275 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
8276 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
8277 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
8278 
8279 		ht_cap.cap |= stbc;
8280 	}
8281 
8282 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
8283 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
8284 
8285 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
8286 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
8287 
8288 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
8289 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
8290 
8291 	for (i = 0; i < ar->num_rx_chains; i++) {
8292 		if (rate_cap_rx_chainmask & BIT(i))
8293 			ht_cap.mcs.rx_mask[i] = 0xFF;
8294 	}
8295 
8296 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
8297 
8298 	return ht_cap;
8299 }
8300 
ath12k_mac_set_txbf_conf(struct ath12k_link_vif * arvif)8301 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
8302 {
8303 	u32 value = 0;
8304 	struct ath12k *ar = arvif->ar;
8305 	struct ath12k_vif *ahvif = arvif->ahvif;
8306 	int nsts;
8307 	int sound_dim;
8308 	u32 vht_cap = ar->pdev->cap.vht_cap;
8309 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
8310 
8311 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
8312 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
8313 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
8314 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
8315 	}
8316 
8317 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
8318 		sound_dim = vht_cap &
8319 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
8320 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
8321 		if (sound_dim > (ar->num_tx_chains - 1))
8322 			sound_dim = ar->num_tx_chains - 1;
8323 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
8324 	}
8325 
8326 	if (!value)
8327 		return 0;
8328 
8329 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
8330 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
8331 
8332 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
8333 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
8334 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
8335 	}
8336 
8337 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
8338 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
8339 
8340 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
8341 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
8342 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
8343 	}
8344 
8345 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
8346 					     vdev_param, value);
8347 }
8348 
ath12k_set_vht_txbf_cap(struct ath12k * ar,u32 * vht_cap)8349 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
8350 {
8351 	bool subfer, subfee;
8352 	int sound_dim = 0;
8353 
8354 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
8355 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
8356 
8357 	if (ar->num_tx_chains < 2) {
8358 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
8359 		subfer = false;
8360 	}
8361 
8362 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
8363 	if (!subfer)
8364 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
8365 
8366 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
8367 	if (!subfee)
8368 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
8369 
8370 	sound_dim = u32_get_bits(*vht_cap,
8371 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8372 	*vht_cap = u32_replace_bits(*vht_cap, 0,
8373 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8374 
8375 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
8376 
8377 	/* Enable Sounding Dimension Field only if SU BF is enabled */
8378 	if (subfer) {
8379 		if (sound_dim > (ar->num_tx_chains - 1))
8380 			sound_dim = ar->num_tx_chains - 1;
8381 
8382 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
8383 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
8384 	}
8385 
8386 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
8387 	if (!subfee)
8388 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
8389 }
8390 
8391 static struct ieee80211_sta_vht_cap
ath12k_create_vht_cap(struct ath12k * ar,u32 rate_cap_tx_chainmask,u32 rate_cap_rx_chainmask)8392 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
8393 		      u32 rate_cap_rx_chainmask)
8394 {
8395 	struct ieee80211_sta_vht_cap vht_cap = {};
8396 	u16 txmcs_map, rxmcs_map;
8397 	int i;
8398 
8399 	vht_cap.vht_supported = 1;
8400 	vht_cap.cap = ar->pdev->cap.vht_cap;
8401 
8402 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
8403 
8404 	/* 80P80 is not supported */
8405 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
8406 
8407 	rxmcs_map = 0;
8408 	txmcs_map = 0;
8409 	for (i = 0; i < 8; i++) {
8410 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
8411 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8412 		else
8413 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8414 
8415 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
8416 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
8417 		else
8418 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
8419 	}
8420 
8421 	if (rate_cap_tx_chainmask <= 1)
8422 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
8423 
8424 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
8425 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
8426 
8427 	/* Check if the HW supports 1:1 NSS ratio and reset
8428 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
8429 	 */
8430 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
8431 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
8432 
8433 	return vht_cap;
8434 }
8435 
ath12k_mac_setup_ht_vht_cap(struct ath12k * ar,struct ath12k_pdev_cap * cap,u32 * ht_cap_info)8436 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
8437 					struct ath12k_pdev_cap *cap,
8438 					u32 *ht_cap_info)
8439 {
8440 	struct ieee80211_supported_band *band;
8441 	u32 rate_cap_tx_chainmask;
8442 	u32 rate_cap_rx_chainmask;
8443 	u32 ht_cap;
8444 
8445 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
8446 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
8447 
8448 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8449 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
8450 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
8451 		if (ht_cap_info)
8452 			*ht_cap_info = ht_cap;
8453 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8454 						    rate_cap_rx_chainmask);
8455 	}
8456 
8457 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8458 	    (ar->ab->hw_params->single_pdev_only ||
8459 	     !ar->supports_6ghz)) {
8460 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
8461 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
8462 		if (ht_cap_info)
8463 			*ht_cap_info = ht_cap;
8464 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
8465 						    rate_cap_rx_chainmask);
8466 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
8467 						      rate_cap_rx_chainmask);
8468 	}
8469 }
8470 
ath12k_check_chain_mask(struct ath12k * ar,u32 ant,bool is_tx_ant)8471 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
8472 {
8473 	/* TODO: Check the request chainmask against the supported
8474 	 * chainmask table which is advertised in extented_service_ready event
8475 	 */
8476 
8477 	return 0;
8478 }
8479 
ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg * fw_ppet,u8 * he_ppet)8480 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8481 				  u8 *he_ppet)
8482 {
8483 	int nss, ru;
8484 	u8 bit = 7;
8485 
8486 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
8487 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
8488 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
8489 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
8490 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8491 		for (ru = 0; ru < 4; ru++) {
8492 			u8 val;
8493 			int i;
8494 
8495 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8496 				continue;
8497 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
8498 			       0x3f;
8499 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
8500 			for (i = 5; i >= 0; i--) {
8501 				he_ppet[bit / 8] |=
8502 					((val >> i) & 0x1) << ((bit % 8));
8503 				bit++;
8504 			}
8505 		}
8506 	}
8507 }
8508 
8509 static void
ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem * he_cap_elem)8510 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
8511 {
8512 	u8 m;
8513 
8514 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
8515 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
8516 	he_cap_elem->mac_cap_info[0] &= ~m;
8517 
8518 	m = IEEE80211_HE_MAC_CAP2_TRS |
8519 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8520 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8521 	he_cap_elem->mac_cap_info[2] &= ~m;
8522 
8523 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
8524 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
8525 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
8526 	he_cap_elem->mac_cap_info[3] &= ~m;
8527 
8528 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
8529 	    IEEE80211_HE_MAC_CAP4_BQR;
8530 	he_cap_elem->mac_cap_info[4] &= ~m;
8531 
8532 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
8533 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
8534 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
8535 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
8536 	he_cap_elem->mac_cap_info[5] &= ~m;
8537 
8538 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
8539 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
8540 	he_cap_elem->phy_cap_info[2] &= ~m;
8541 
8542 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
8543 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
8544 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
8545 	he_cap_elem->phy_cap_info[3] &= ~m;
8546 
8547 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
8548 	he_cap_elem->phy_cap_info[4] &= ~m;
8549 
8550 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
8551 	he_cap_elem->phy_cap_info[5] &= ~m;
8552 
8553 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
8554 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
8555 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
8556 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
8557 	he_cap_elem->phy_cap_info[6] &= ~m;
8558 
8559 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
8560 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
8561 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
8562 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
8563 	he_cap_elem->phy_cap_info[7] &= ~m;
8564 
8565 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
8566 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
8567 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
8568 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
8569 	he_cap_elem->phy_cap_info[8] &= ~m;
8570 
8571 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
8572 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
8573 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
8574 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
8575 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
8576 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
8577 	he_cap_elem->phy_cap_info[9] &= ~m;
8578 }
8579 
ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap * pcap,struct ath12k_band_cap * bcap)8580 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
8581 					   struct ath12k_band_cap *bcap)
8582 {
8583 	u8 val;
8584 
8585 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
8586 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8587 		bcap->he_6ghz_capa |=
8588 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
8589 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8590 	else
8591 		bcap->he_6ghz_capa |=
8592 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
8593 					IEEE80211_HE_6GHZ_CAP_SM_PS);
8594 	val = u32_get_bits(pcap->vht_cap,
8595 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
8596 	bcap->he_6ghz_capa |=
8597 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
8598 	val = u32_get_bits(pcap->vht_cap,
8599 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
8600 	bcap->he_6ghz_capa |=
8601 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
8602 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
8603 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
8604 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
8605 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
8606 
8607 	return cpu_to_le16(bcap->he_6ghz_capa);
8608 }
8609 
ath12k_mac_set_hemcsmap(struct ath12k * ar,struct ath12k_pdev_cap * cap,struct ieee80211_sta_he_cap * he_cap)8610 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
8611 				    struct ath12k_pdev_cap *cap,
8612 				    struct ieee80211_sta_he_cap *he_cap)
8613 {
8614 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
8615 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
8616 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
8617 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
8618 	u16 txmcs_map = 0, rxmcs_map = 0;
8619 	u32 i;
8620 
8621 	for (i = 0; i < 8; i++) {
8622 		if (i < ar->num_tx_chains &&
8623 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8624 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8625 		else
8626 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8627 
8628 		if (i < ar->num_rx_chains &&
8629 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8630 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8631 		else
8632 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8633 
8634 		if (i < maxtxnss_160 &&
8635 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8636 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8637 		else
8638 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8639 
8640 		if (i < maxrxnss_160 &&
8641 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
8642 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
8643 		else
8644 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
8645 	}
8646 
8647 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
8648 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
8649 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
8650 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
8651 }
8652 
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)8653 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
8654 				   struct ath12k_band_cap *band_cap,
8655 				   int iftype, u8 num_tx_chains,
8656 				   struct ieee80211_sta_he_cap *he_cap)
8657 {
8658 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
8659 
8660 	he_cap->has_he = true;
8661 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
8662 	       sizeof(he_cap_elem->mac_cap_info));
8663 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
8664 	       sizeof(he_cap_elem->phy_cap_info));
8665 
8666 	he_cap_elem->mac_cap_info[1] &=
8667 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
8668 	he_cap_elem->phy_cap_info[0] &=
8669 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8670 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8671 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
8672 	/* 80PLUS80 is not supported */
8673 	he_cap_elem->phy_cap_info[0] &=
8674 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
8675 	he_cap_elem->phy_cap_info[5] &=
8676 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
8677 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
8678 
8679 	switch (iftype) {
8680 	case NL80211_IFTYPE_AP:
8681 		he_cap_elem->mac_cap_info[2] &=
8682 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
8683 		he_cap_elem->phy_cap_info[3] &=
8684 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
8685 		he_cap_elem->phy_cap_info[9] |=
8686 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
8687 		break;
8688 	case NL80211_IFTYPE_STATION:
8689 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
8690 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
8691 		he_cap_elem->phy_cap_info[9] |=
8692 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
8693 		break;
8694 	case NL80211_IFTYPE_MESH_POINT:
8695 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
8696 		break;
8697 	}
8698 
8699 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
8700 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
8701 	if (he_cap_elem->phy_cap_info[6] &
8702 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
8703 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
8704 }
8705 
8706 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)8707 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8708 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8709 			    const struct ieee80211_he_cap_elem *he_cap,
8710 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8711 {
8712 	if ((he_cap->phy_cap_info[0] &
8713 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8714 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8715 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8716 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8717 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8718 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8719 
8720 	if (he_cap->phy_cap_info[0] &
8721 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8722 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8723 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8724 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8725 
8726 	if (he_cap->phy_cap_info[0] &
8727 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8728 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8729 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8730 
8731 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8732 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8733 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8734 }
8735 
ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg * fw_ppet,struct ieee80211_sta_eht_cap * cap)8736 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8737 					   struct ieee80211_sta_eht_cap *cap)
8738 {
8739 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8740 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8741 
8742 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8743 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8744 
8745 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8746 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8747 
8748 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8749 		for (ru = 0;
8750 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8751 		     ru++) {
8752 			u32 val = 0;
8753 
8754 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8755 				continue;
8756 
8757 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8758 						(ru * ppet_bit_len_per_ru),
8759 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8760 
8761 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8762 				cap->eht_ppe_thres[bit / 8] |=
8763 					(((val >> i) & 0x1) << ((bit % 8)));
8764 				bit++;
8765 			}
8766 		}
8767 	}
8768 }
8769 
8770 static void
ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed * eht_cap_elem)8771 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8772 			       *eht_cap_elem)
8773 {
8774 	u8 m;
8775 
8776 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8777 	eht_cap_elem->mac_cap_info[0] &= ~m;
8778 
8779 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8780 	eht_cap_elem->phy_cap_info[0] &= ~m;
8781 
8782 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8783 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8784 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8785 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8786 	eht_cap_elem->phy_cap_info[3] &= ~m;
8787 
8788 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8789 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8790 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8791 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8792 	eht_cap_elem->phy_cap_info[4] &= ~m;
8793 
8794 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8795 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8796 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8797 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8798 	eht_cap_elem->phy_cap_info[5] &= ~m;
8799 
8800 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8801 	eht_cap_elem->phy_cap_info[6] &= ~m;
8802 
8803 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8804 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8805 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8806 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8807 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8808 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8809 	eht_cap_elem->phy_cap_info[7] &= ~m;
8810 }
8811 
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)8812 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8813 				    struct ath12k_band_cap *band_cap,
8814 				    struct ieee80211_he_cap_elem *he_cap_elem,
8815 				    int iftype,
8816 				    struct ieee80211_sta_eht_cap *eht_cap)
8817 {
8818 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8819 
8820 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8821 
8822 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8823 	    ath12k_acpi_get_disable_11be(ar->ab))
8824 		return;
8825 
8826 	eht_cap->has_eht = true;
8827 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8828 	       sizeof(eht_cap_elem->mac_cap_info));
8829 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8830 	       sizeof(eht_cap_elem->phy_cap_info));
8831 
8832 	switch (iftype) {
8833 	case NL80211_IFTYPE_AP:
8834 		eht_cap_elem->phy_cap_info[0] &=
8835 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8836 		eht_cap_elem->phy_cap_info[4] &=
8837 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8838 		eht_cap_elem->phy_cap_info[5] &=
8839 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8840 		break;
8841 	case NL80211_IFTYPE_STATION:
8842 		eht_cap_elem->phy_cap_info[7] &=
8843 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8844 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8845 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8846 		eht_cap_elem->phy_cap_info[7] &=
8847 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8848 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8849 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8850 		break;
8851 	case NL80211_IFTYPE_MESH_POINT:
8852 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8853 		break;
8854 	default:
8855 		break;
8856 	}
8857 
8858 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8859 				    he_cap_elem, eht_cap_elem);
8860 
8861 	if (eht_cap_elem->phy_cap_info[5] &
8862 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8863 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8864 }
8865 
ath12k_mac_copy_sband_iftype_data(struct ath12k * ar,struct ath12k_pdev_cap * cap,struct ieee80211_sband_iftype_data * data,int band)8866 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8867 					     struct ath12k_pdev_cap *cap,
8868 					     struct ieee80211_sband_iftype_data *data,
8869 					     int band)
8870 {
8871 	struct ath12k_band_cap *band_cap = &cap->band[band];
8872 	int i, idx = 0;
8873 
8874 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8875 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8876 
8877 		switch (i) {
8878 		case NL80211_IFTYPE_STATION:
8879 		case NL80211_IFTYPE_AP:
8880 		case NL80211_IFTYPE_MESH_POINT:
8881 			break;
8882 
8883 		default:
8884 			continue;
8885 		}
8886 
8887 		data[idx].types_mask = BIT(i);
8888 
8889 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8890 		if (band == NL80211_BAND_6GHZ) {
8891 			data[idx].he_6ghz_capa.capa =
8892 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8893 		}
8894 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8895 					&data[idx].eht_cap);
8896 		idx++;
8897 	}
8898 
8899 	return idx;
8900 }
8901 
ath12k_mac_setup_sband_iftype_data(struct ath12k * ar,struct ath12k_pdev_cap * cap)8902 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
8903 					       struct ath12k_pdev_cap *cap)
8904 {
8905 	struct ieee80211_supported_band *sband;
8906 	enum nl80211_band band;
8907 	int count;
8908 
8909 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8910 		band = NL80211_BAND_2GHZ;
8911 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8912 							  ar->mac.iftype[band],
8913 							  band);
8914 		sband = &ar->mac.sbands[band];
8915 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8916 						 count);
8917 	}
8918 
8919 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
8920 		band = NL80211_BAND_5GHZ;
8921 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8922 							  ar->mac.iftype[band],
8923 							  band);
8924 		sband = &ar->mac.sbands[band];
8925 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8926 						 count);
8927 	}
8928 
8929 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8930 	    ar->supports_6ghz) {
8931 		band = NL80211_BAND_6GHZ;
8932 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8933 							  ar->mac.iftype[band],
8934 							  band);
8935 		sband = &ar->mac.sbands[band];
8936 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8937 						 count);
8938 	}
8939 }
8940 
__ath12k_set_antenna(struct ath12k * ar,u32 tx_ant,u32 rx_ant)8941 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
8942 {
8943 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
8944 	int ret;
8945 
8946 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8947 
8948 	if (ath12k_check_chain_mask(ar, tx_ant, true))
8949 		return -EINVAL;
8950 
8951 	if (ath12k_check_chain_mask(ar, rx_ant, false))
8952 		return -EINVAL;
8953 
8954 	/* Since we advertised the max cap of all radios combined during wiphy
8955 	 * registration, ensure we don't set the antenna config higher than the
8956 	 * limits
8957 	 */
8958 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
8959 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
8960 
8961 	ar->cfg_tx_chainmask = tx_ant;
8962 	ar->cfg_rx_chainmask = rx_ant;
8963 
8964 	if (ah->state != ATH12K_HW_STATE_ON &&
8965 	    ah->state != ATH12K_HW_STATE_RESTARTED)
8966 		return 0;
8967 
8968 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
8969 					tx_ant, ar->pdev->pdev_id);
8970 	if (ret) {
8971 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
8972 			    ret, tx_ant);
8973 		return ret;
8974 	}
8975 
8976 	ar->num_tx_chains = hweight32(tx_ant);
8977 
8978 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
8979 					rx_ant, ar->pdev->pdev_id);
8980 	if (ret) {
8981 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
8982 			    ret, rx_ant);
8983 		return ret;
8984 	}
8985 
8986 	ar->num_rx_chains = hweight32(rx_ant);
8987 
8988 	/* Reload HT/VHT/HE capability */
8989 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
8990 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
8991 
8992 	return 0;
8993 }
8994 
ath12k_mgmt_over_wmi_tx_drop(struct ath12k * ar,struct sk_buff * skb)8995 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
8996 {
8997 	int num_mgmt;
8998 
8999 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9000 
9001 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
9002 
9003 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
9004 
9005 	if (num_mgmt < 0)
9006 		WARN_ON_ONCE(1);
9007 
9008 	if (!num_mgmt)
9009 		wake_up(&ar->txmgmt_empty_waitq);
9010 }
9011 
ath12k_mac_tx_mgmt_free(struct ath12k * ar,int buf_id)9012 static void ath12k_mac_tx_mgmt_free(struct ath12k *ar, int buf_id)
9013 {
9014 	struct sk_buff *msdu;
9015 	struct ieee80211_tx_info *info;
9016 
9017 	spin_lock_bh(&ar->txmgmt_idr_lock);
9018 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
9019 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9020 
9021 	if (!msdu)
9022 		return;
9023 
9024 	dma_unmap_single(ar->ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
9025 			 DMA_TO_DEVICE);
9026 
9027 	info = IEEE80211_SKB_CB(msdu);
9028 	memset(&info->status, 0, sizeof(info->status));
9029 
9030 	ath12k_mgmt_over_wmi_tx_drop(ar, msdu);
9031 }
9032 
ath12k_mac_tx_mgmt_pending_free(int buf_id,void * skb,void * ctx)9033 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
9034 {
9035 	struct ath12k *ar = ctx;
9036 
9037 	ath12k_mac_tx_mgmt_free(ar, buf_id);
9038 
9039 	return 0;
9040 }
9041 
ath12k_mac_vif_txmgmt_idr_remove(int buf_id,void * skb,void * ctx)9042 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
9043 {
9044 	struct ieee80211_vif *vif = ctx;
9045 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9046 	struct ath12k *ar = skb_cb->ar;
9047 
9048 	if (skb_cb->vif == vif)
9049 		ath12k_mac_tx_mgmt_free(ar, buf_id);
9050 
9051 	return 0;
9052 }
9053 
ath12k_mac_mgmt_tx_wmi(struct ath12k * ar,struct ath12k_link_vif * arvif,struct sk_buff * skb)9054 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
9055 				  struct sk_buff *skb)
9056 {
9057 	struct ath12k_base *ab = ar->ab;
9058 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9059 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
9060 	struct ieee80211_tx_info *info;
9061 	enum hal_encrypt_type enctype;
9062 	unsigned int mic_len;
9063 	dma_addr_t paddr;
9064 	int buf_id;
9065 	int ret;
9066 
9067 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9068 
9069 	skb_cb->ar = ar;
9070 	spin_lock_bh(&ar->txmgmt_idr_lock);
9071 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
9072 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
9073 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9074 	if (buf_id < 0)
9075 		return -ENOSPC;
9076 
9077 	info = IEEE80211_SKB_CB(skb);
9078 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
9079 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
9080 		if ((ieee80211_is_action(hdr->frame_control) ||
9081 		     ieee80211_is_deauth(hdr->frame_control) ||
9082 		     ieee80211_is_disassoc(hdr->frame_control)) &&
9083 		     ieee80211_has_protected(hdr->frame_control)) {
9084 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
9085 			mic_len = ath12k_dp_rx_crypto_mic_len(ab->dp, enctype);
9086 			skb_put(skb, mic_len);
9087 		}
9088 	}
9089 
9090 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
9091 	if (dma_mapping_error(ab->dev, paddr)) {
9092 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
9093 		ret = -EIO;
9094 		goto err_free_idr;
9095 	}
9096 
9097 	skb_cb->paddr = paddr;
9098 
9099 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
9100 	if (ret) {
9101 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
9102 		goto err_unmap_buf;
9103 	}
9104 
9105 	return 0;
9106 
9107 err_unmap_buf:
9108 	dma_unmap_single(ab->dev, skb_cb->paddr,
9109 			 skb->len, DMA_TO_DEVICE);
9110 err_free_idr:
9111 	spin_lock_bh(&ar->txmgmt_idr_lock);
9112 	idr_remove(&ar->txmgmt_idr, buf_id);
9113 	spin_unlock_bh(&ar->txmgmt_idr_lock);
9114 
9115 	return ret;
9116 }
9117 
ath12k_mgmt_over_wmi_tx_purge(struct ath12k * ar)9118 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
9119 {
9120 	struct sk_buff *skb;
9121 
9122 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
9123 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9124 }
9125 
ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif * arvif,struct sk_buff * skb)9126 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9127 						       struct sk_buff *skb)
9128 {
9129 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9130 	u8 category, *buf, iv_len, action_code, dialog_token;
9131 	struct ieee80211_bss_conf *link_conf;
9132 	struct ieee80211_chanctx_conf *conf;
9133 	int cur_tx_power, max_tx_power;
9134 	struct ath12k *ar = arvif->ar;
9135 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
9136 	struct wiphy *wiphy = hw->wiphy;
9137 	struct ath12k_skb_cb *skb_cb;
9138 	struct ieee80211_mgmt *mgmt;
9139 	unsigned int remaining_len;
9140 	bool has_protected;
9141 
9142 	lockdep_assert_wiphy(wiphy);
9143 
9144 	/* make sure category field is present */
9145 	if (skb->len < IEEE80211_MIN_ACTION_SIZE(category))
9146 		return -EINVAL;
9147 
9148 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE(category);
9149 	has_protected = ieee80211_has_protected(hdr->frame_control);
9150 
9151 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
9152 	 * we can't put in data in this case
9153 	 */
9154 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
9155 	    has_protected)
9156 		return 0;
9157 
9158 	mgmt = (struct ieee80211_mgmt *)hdr;
9159 	buf = (u8 *)&mgmt->u.action;
9160 
9161 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
9162 	 * many bytes if it is there
9163 	 */
9164 	if (has_protected) {
9165 		skb_cb = ATH12K_SKB_CB(skb);
9166 
9167 		switch (skb_cb->cipher) {
9168 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
9169 		 * key needs to be processed. See ath12k_install_key()
9170 		 */
9171 		case WLAN_CIPHER_SUITE_CCMP:
9172 		case WLAN_CIPHER_SUITE_CCMP_256:
9173 		case WLAN_CIPHER_SUITE_GCMP:
9174 		case WLAN_CIPHER_SUITE_GCMP_256:
9175 			iv_len = IEEE80211_CCMP_HDR_LEN;
9176 			break;
9177 		case WLAN_CIPHER_SUITE_TKIP:
9178 			iv_len = 0;
9179 			break;
9180 		default:
9181 			return -EINVAL;
9182 		}
9183 
9184 		if (remaining_len < iv_len)
9185 			return -EINVAL;
9186 
9187 		buf += iv_len;
9188 		remaining_len -= iv_len;
9189 	}
9190 
9191 	category = *buf++;
9192 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
9193 	 * no need to adjust remaining_len
9194 	 */
9195 
9196 	switch (category) {
9197 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
9198 		/* need action code and dialog token */
9199 		if (remaining_len < 2)
9200 			return -EINVAL;
9201 
9202 		/* Packet Format:
9203 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
9204 		 */
9205 		action_code = *buf++;
9206 		dialog_token = *buf++;
9207 		remaining_len -= 2;
9208 
9209 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9210 		if (!link_conf) {
9211 			ath12k_warn(ar->ab,
9212 				    "failed to get bss link conf for vdev %d in RM handling\n",
9213 				    arvif->vdev_id);
9214 			return -EINVAL;
9215 		}
9216 
9217 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
9218 		if (!conf)
9219 			return -ENOENT;
9220 
9221 		cur_tx_power = link_conf->txpower;
9222 		max_tx_power = min(conf->def.chan->max_reg_power,
9223 				   (int)ar->max_tx_power / 2);
9224 
9225 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
9226 					  &cur_tx_power);
9227 
9228 		switch (action_code) {
9229 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
9230 			/* need variable fields to be present in len */
9231 			if (remaining_len < 2)
9232 				return -EINVAL;
9233 
9234 			/* Variable length format as defined in IEEE 802.11-2024,
9235 			 * Figure 9-1187-Link Measurement Request frame Action field
9236 			 * format.
9237 			 *	Transmit Power | Max Tx Power
9238 			 * We fill both of these.
9239 			 */
9240 			*buf++ = cur_tx_power;
9241 			*buf = max_tx_power;
9242 
9243 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9244 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
9245 				   dialog_token, cur_tx_power, max_tx_power);
9246 			break;
9247 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
9248 			/* need variable fields to be present in len */
9249 			if (remaining_len < 3)
9250 				return -EINVAL;
9251 
9252 			/* Variable length format as defined in IEEE 802.11-2024,
9253 			 * Figure 9-1188-Link Measurement Report frame Action field format
9254 			 *	TPC Report | Variable Fields
9255 			 *
9256 			 * TPC Report Format:
9257 			 *	Element ID | Len | Tx Power | Link Margin
9258 			 *
9259 			 * We fill Tx power in the TPC Report (2nd index)
9260 			 */
9261 			buf[2] = cur_tx_power;
9262 
9263 			/* TODO: At present, Link margin data is not present so can't
9264 			 * really fill it now. Once it is available, it can be added
9265 			 * here
9266 			 */
9267 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9268 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
9269 				   dialog_token, cur_tx_power);
9270 			break;
9271 		default:
9272 			return -EINVAL;
9273 		}
9274 		break;
9275 	default:
9276 		/* nothing to fill */
9277 		return 0;
9278 	}
9279 
9280 	return 0;
9281 }
9282 
ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif * arvif,struct sk_buff * skb)9283 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
9284 						struct sk_buff *skb)
9285 {
9286 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9287 
9288 	if (!ieee80211_is_action(hdr->frame_control))
9289 		return 0;
9290 
9291 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
9292 }
9293 
ath12k_mgmt_over_wmi_tx_work(struct wiphy * wiphy,struct wiphy_work * work)9294 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
9295 {
9296 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
9297 	struct ath12k_hw *ah = ar->ah;
9298 	struct ath12k_skb_cb *skb_cb;
9299 	struct ath12k_vif *ahvif;
9300 	struct ath12k_link_vif *arvif;
9301 	struct sk_buff *skb;
9302 	int ret;
9303 
9304 	lockdep_assert_wiphy(wiphy);
9305 
9306 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
9307 		skb_cb = ATH12K_SKB_CB(skb);
9308 		if (!skb_cb->vif) {
9309 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
9310 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9311 			continue;
9312 		}
9313 
9314 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
9315 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
9316 			ath12k_warn(ar->ab,
9317 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
9318 				    skb_cb->link_id, ahvif->links_map);
9319 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9320 			continue;
9321 		}
9322 
9323 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
9324 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
9325 			/* Fill in the data which is required to be filled by the driver
9326 			 * For example: Max Tx power in Link Measurement Request/Report
9327 			 */
9328 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
9329 			if (ret) {
9330 				/* If we couldn't fill the data due to any reason,
9331 				 * let's not discard transmitting the packet.
9332 				 * For example: Software crypto and PMF case
9333 				 */
9334 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9335 					   "Failed to fill the required data for the mgmt packet err %d\n",
9336 					   ret);
9337 			}
9338 
9339 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
9340 			if (ret) {
9341 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
9342 					    arvif->vdev_id, ret);
9343 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9344 			}
9345 		} else {
9346 			ath12k_warn(ar->ab,
9347 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
9348 				    arvif->vdev_id,
9349 				    skb_cb->link_id,
9350 				    arvif->is_started);
9351 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
9352 		}
9353 	}
9354 }
9355 
ath12k_mac_mgmt_tx(struct ath12k * ar,struct sk_buff * skb,bool is_prb_rsp)9356 int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
9357 		       bool is_prb_rsp)
9358 {
9359 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
9360 
9361 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
9362 		return -ESHUTDOWN;
9363 
9364 	/* Drop probe response packets when the pending management tx
9365 	 * count has reached a certain threshold, so as to prioritize
9366 	 * other mgmt packets like auth and assoc to be sent on time
9367 	 * for establishing successful connections.
9368 	 */
9369 	if (is_prb_rsp &&
9370 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
9371 		ath12k_warn(ar->ab,
9372 			    "dropping probe response as pending queue is almost full\n");
9373 		return -ENOSPC;
9374 	}
9375 
9376 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
9377 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
9378 		return -ENOSPC;
9379 	}
9380 
9381 	skb_queue_tail(q, skb);
9382 	atomic_inc(&ar->num_pending_mgmt_tx);
9383 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9384 
9385 	return 0;
9386 }
9387 EXPORT_SYMBOL(ath12k_mac_mgmt_tx);
9388 
ath12k_mac_add_p2p_noa_ie(struct ath12k * ar,struct ieee80211_vif * vif,struct sk_buff * skb,bool is_prb_rsp)9389 void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
9390 			       struct ieee80211_vif *vif,
9391 			       struct sk_buff *skb,
9392 			       bool is_prb_rsp)
9393 {
9394 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9395 
9396 	if (likely(!is_prb_rsp))
9397 		return;
9398 
9399 	spin_lock_bh(&ar->data_lock);
9400 
9401 	if (ahvif->u.ap.noa_data &&
9402 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
9403 			      GFP_ATOMIC))
9404 		skb_put_data(skb, ahvif->u.ap.noa_data,
9405 			     ahvif->u.ap.noa_len);
9406 
9407 	spin_unlock_bh(&ar->data_lock);
9408 }
9409 EXPORT_SYMBOL(ath12k_mac_add_p2p_noa_ie);
9410 
9411 /* 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)9412 void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
9413 					     u8 link_id, struct sk_buff *skb,
9414 					     u32 info_flags)
9415 {
9416 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9417 	struct ieee80211_bss_conf *bss_conf;
9418 
9419 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9420 		return;
9421 
9422 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
9423 	if (bss_conf)
9424 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9425 }
9426 EXPORT_SYMBOL(ath12k_mlo_mcast_update_tx_link_address);
9427 
9428 /* 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)9429 u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
9430 			  u8 link, struct sk_buff *skb, u32 info_flags)
9431 {
9432 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
9433 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9434 	struct ieee80211_link_sta *link_sta;
9435 	struct ieee80211_bss_conf *bss_conf;
9436 	struct ath12k_sta *ahsta;
9437 
9438 	/* Use the link id passed or the default vif link */
9439 	if (!sta) {
9440 		if (link != IEEE80211_LINK_UNSPECIFIED)
9441 			return link;
9442 
9443 		return ahvif->deflink.link_id;
9444 	}
9445 
9446 	ahsta = ath12k_sta_to_ahsta(sta);
9447 
9448 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
9449 	 * Also it assumes for now support only for MLO AP in this path
9450 	 */
9451 	if (!sta->mlo) {
9452 		link = ahsta->deflink.link_id;
9453 
9454 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
9455 			return link;
9456 
9457 		bss_conf = rcu_dereference(vif->link_conf[link]);
9458 		if (bss_conf) {
9459 			ether_addr_copy(hdr->addr2, bss_conf->addr);
9460 			if (!ieee80211_has_tods(hdr->frame_control) &&
9461 			    !ieee80211_has_fromds(hdr->frame_control))
9462 				ether_addr_copy(hdr->addr3, bss_conf->addr);
9463 		}
9464 
9465 		return link;
9466 	}
9467 
9468 	/* enqueue eth enacap & data frames on primary link, FW does link
9469 	 * selection and address translation.
9470 	 */
9471 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
9472 	    ieee80211_is_data(hdr->frame_control))
9473 		return ahsta->assoc_link_id;
9474 
9475 	/* 802.11 frame cases */
9476 	if (link == IEEE80211_LINK_UNSPECIFIED)
9477 		link = ahsta->deflink.link_id;
9478 
9479 	if (!ieee80211_is_mgmt(hdr->frame_control))
9480 		return link;
9481 
9482 	/* Perform address conversion for ML STA Tx */
9483 	bss_conf = rcu_dereference(vif->link_conf[link]);
9484 	link_sta = rcu_dereference(sta->link[link]);
9485 
9486 	if (bss_conf && link_sta) {
9487 		ether_addr_copy(hdr->addr1, link_sta->addr);
9488 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9489 
9490 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9491 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9492 		else if (vif->type == NL80211_IFTYPE_AP)
9493 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9494 
9495 		return link;
9496 	}
9497 
9498 	if (bss_conf) {
9499 		/* In certain cases where a ML sta associated and added subset of
9500 		 * links on which the ML AP is active, but now sends some frame
9501 		 * (ex. Probe request) on a different link which is active in our
9502 		 * MLD but was not added during previous association, we can
9503 		 * still honor the Tx to that ML STA via the requested link.
9504 		 * The control would reach here in such case only when that link
9505 		 * address is same as the MLD address or in worst case clients
9506 		 * used MLD address at TA wrongly which would have helped
9507 		 * identify the ML sta object and pass it here.
9508 		 * If the link address of that STA is different from MLD address,
9509 		 * then the sta object would be NULL and control won't reach
9510 		 * here but return at the start of the function itself with !sta
9511 		 * check. Also this would not need any translation at hdr->addr1
9512 		 * from MLD to link address since the RA is the MLD address
9513 		 * (same as that link address ideally) already.
9514 		 */
9515 		ether_addr_copy(hdr->addr2, bss_conf->addr);
9516 
9517 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
9518 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
9519 		else if (vif->type == NL80211_IFTYPE_AP)
9520 			ether_addr_copy(hdr->addr3, bss_conf->addr);
9521 	}
9522 
9523 	return link;
9524 }
9525 EXPORT_SYMBOL(ath12k_mac_get_tx_link);
9526 
ath12k_mac_drain_tx(struct ath12k * ar)9527 void ath12k_mac_drain_tx(struct ath12k *ar)
9528 {
9529 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9530 
9531 	/* make sure rcu-protected mac80211 tx path itself is drained */
9532 	synchronize_net();
9533 
9534 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9535 	ath12k_mgmt_over_wmi_tx_purge(ar);
9536 }
9537 
ath12k_mac_config_mon_status_default(struct ath12k * ar,bool enable)9538 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9539 {
9540 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9541 	struct ath12k_base *ab = ar->ab;
9542 	u32 ring_id, i;
9543 	int ret = 0;
9544 
9545 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9546 
9547 	if (!ab->hw_params->rxdma1_enable)
9548 		return ret;
9549 
9550 	if (enable) {
9551 		tlv_filter = ath12k_mac_mon_status_filter_default;
9552 
9553 		if (ath12k_debugfs_rx_filter(ar))
9554 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9555 	} else {
9556 		tlv_filter.rxmon_disable = true;
9557 	}
9558 
9559 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9560 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9561 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9562 						       ar->dp.mac_id + i,
9563 						       HAL_RXDMA_MONITOR_DST,
9564 						       DP_RXDMA_REFILL_RING_SIZE,
9565 						       &tlv_filter);
9566 		if (ret) {
9567 			ath12k_err(ab,
9568 				   "failed to setup filter for monitor buf %d\n",
9569 				   ret);
9570 		}
9571 	}
9572 
9573 	return ret;
9574 }
9575 
ath12k_mac_start(struct ath12k * ar)9576 static int ath12k_mac_start(struct ath12k *ar)
9577 {
9578 	struct ath12k_hw *ah = ar->ah;
9579 	struct ath12k_base *ab = ar->ab;
9580 	struct ath12k_pdev *pdev = ar->pdev;
9581 	int ret;
9582 
9583 	lockdep_assert_held(&ah->hw_mutex);
9584 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9585 
9586 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9587 					1, pdev->pdev_id);
9588 
9589 	if (ret) {
9590 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9591 		goto err;
9592 	}
9593 
9594 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9595 					pdev->pdev_id);
9596 	if (ret) {
9597 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9598 		goto err;
9599 	}
9600 
9601 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9602 					0, pdev->pdev_id);
9603 	if (ret) {
9604 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9605 			   ret);
9606 		goto err;
9607 	}
9608 
9609 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9610 	if (ret) {
9611 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9612 			   ret);
9613 		goto err;
9614 	}
9615 
9616 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9617 						  HTT_PPDU_STATS_TAG_DEFAULT);
9618 	if (ret) {
9619 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9620 		goto err;
9621 	}
9622 
9623 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9624 					1, pdev->pdev_id);
9625 
9626 	if (ret) {
9627 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9628 		goto err;
9629 	}
9630 
9631 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9632 
9633 	/* TODO: Do we need to enable ANI? */
9634 
9635 	ret = ath12k_reg_update_chan_list(ar, false);
9636 
9637 	/* The ar state alone can be turned off for non supported country
9638 	 * without returning the error value. As we need to update the channel
9639 	 * for the next ar.
9640 	 */
9641 	if (ret) {
9642 		if (ret == -EINVAL)
9643 			ret = 0;
9644 		goto err;
9645 	}
9646 
9647 	ar->num_started_vdevs = 0;
9648 	ar->num_created_vdevs = 0;
9649 	ar->num_peers = 0;
9650 	ar->allocated_vdev_map = 0;
9651 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9652 
9653 	/* Configure monitor status ring with default rx_filter to get rx status
9654 	 * such as rssi, rx_duration.
9655 	 */
9656 	ret = ath12k_mac_config_mon_status_default(ar, true);
9657 	if (ret && (ret != -EOPNOTSUPP)) {
9658 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9659 			   ret);
9660 		goto err;
9661 	}
9662 
9663 	if (ret == -EOPNOTSUPP)
9664 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9665 			   "monitor status config is not yet supported");
9666 
9667 	/* Configure the hash seed for hash based reo dest ring selection */
9668 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9669 
9670 	/* allow device to enter IMPS */
9671 	if (ab->hw_params->idle_ps) {
9672 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9673 						1, pdev->pdev_id);
9674 		if (ret) {
9675 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9676 			goto err;
9677 		}
9678 	}
9679 
9680 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9681 			   &ab->pdevs[ar->pdev_idx]);
9682 
9683 	return 0;
9684 err:
9685 
9686 	return ret;
9687 }
9688 
ath12k_drain_tx(struct ath12k_hw * ah)9689 static void ath12k_drain_tx(struct ath12k_hw *ah)
9690 {
9691 	struct ath12k *ar;
9692 	int i;
9693 
9694 	lockdep_assert_wiphy(ah->hw->wiphy);
9695 
9696 	for_each_ar(ah, ar, i)
9697 		ath12k_mac_drain_tx(ar);
9698 }
9699 
ath12k_mac_op_start(struct ieee80211_hw * hw)9700 int ath12k_mac_op_start(struct ieee80211_hw *hw)
9701 {
9702 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9703 	struct ath12k *ar;
9704 	int ret, i;
9705 
9706 	if (ath12k_ftm_mode)
9707 		return -EPERM;
9708 
9709 	lockdep_assert_wiphy(hw->wiphy);
9710 
9711 	ath12k_drain_tx(ah);
9712 
9713 	guard(mutex)(&ah->hw_mutex);
9714 
9715 	switch (ah->state) {
9716 	case ATH12K_HW_STATE_OFF:
9717 		ah->state = ATH12K_HW_STATE_ON;
9718 		break;
9719 	case ATH12K_HW_STATE_RESTARTING:
9720 		ah->state = ATH12K_HW_STATE_RESTARTED;
9721 		break;
9722 	case ATH12K_HW_STATE_RESTARTED:
9723 	case ATH12K_HW_STATE_WEDGED:
9724 	case ATH12K_HW_STATE_ON:
9725 	case ATH12K_HW_STATE_TM:
9726 		ah->state = ATH12K_HW_STATE_OFF;
9727 
9728 		WARN_ON(1);
9729 		return -EINVAL;
9730 	}
9731 
9732 	for_each_ar(ah, ar, i) {
9733 		ret = ath12k_mac_start(ar);
9734 		if (ret) {
9735 			ah->state = ATH12K_HW_STATE_OFF;
9736 
9737 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9738 				   ar->pdev_idx, ret);
9739 			goto fail_start;
9740 		}
9741 	}
9742 
9743 	return 0;
9744 
9745 fail_start:
9746 	for (; i > 0; i--) {
9747 		ar = ath12k_ah_to_ar(ah, i - 1);
9748 		ath12k_mac_stop(ar);
9749 	}
9750 
9751 	return ret;
9752 }
9753 EXPORT_SYMBOL(ath12k_mac_op_start);
9754 
ath12k_mac_rfkill_config(struct ath12k * ar)9755 int ath12k_mac_rfkill_config(struct ath12k *ar)
9756 {
9757 	struct ath12k_base *ab = ar->ab;
9758 	u32 param;
9759 	int ret;
9760 
9761 	if (ab->hw_params->rfkill_pin == 0)
9762 		return -EOPNOTSUPP;
9763 
9764 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9765 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9766 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9767 		   ab->hw_params->rfkill_on_level);
9768 
9769 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9770 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9771 		u32_encode_bits(ab->hw_params->rfkill_pin,
9772 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9773 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9774 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9775 
9776 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9777 					param, ar->pdev->pdev_id);
9778 	if (ret) {
9779 		ath12k_warn(ab,
9780 			    "failed to set rfkill config 0x%x: %d\n",
9781 			    param, ret);
9782 		return ret;
9783 	}
9784 
9785 	return 0;
9786 }
9787 
ath12k_mac_rfkill_enable_radio(struct ath12k * ar,bool enable)9788 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9789 {
9790 	enum wmi_rfkill_enable_radio param;
9791 	int ret;
9792 
9793 	if (enable)
9794 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9795 	else
9796 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9797 
9798 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9799 		   ar->pdev_idx, param);
9800 
9801 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9802 					param, ar->pdev->pdev_id);
9803 	if (ret) {
9804 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9805 			    param, ret);
9806 		return ret;
9807 	}
9808 
9809 	return 0;
9810 }
9811 
ath12k_mac_stop(struct ath12k * ar)9812 static void ath12k_mac_stop(struct ath12k *ar)
9813 {
9814 	struct ath12k_pdev_dp *dp_pdev = &ar->dp;
9815 	struct ath12k_hw *ah = ar->ah;
9816 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9817 	struct ath12k_wmi_scan_chan_list_arg *arg;
9818 	int ret;
9819 
9820 	lockdep_assert_held(&ah->hw_mutex);
9821 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9822 
9823 	ret = ath12k_mac_config_mon_status_default(ar, false);
9824 	if (ret && (ret != -EOPNOTSUPP))
9825 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9826 			   ret);
9827 
9828 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9829 
9830 	cancel_delayed_work_sync(&ar->scan.timeout);
9831 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9832 	cancel_work_sync(&ar->regd_channel_update_work);
9833 	cancel_work_sync(&ar->regd_update_work);
9834 	cancel_work_sync(&ar->ab->rfkill_work);
9835 	cancel_work_sync(&ar->ab->update_11d_work);
9836 	ar->state_11d = ATH12K_11D_IDLE;
9837 	complete(&ar->completed_11d_scan);
9838 
9839 	spin_lock_bh(&dp_pdev->ppdu_list_lock);
9840 	list_for_each_entry_safe(ppdu_stats, tmp, &dp_pdev->ppdu_stats_info, list) {
9841 		list_del(&ppdu_stats->list);
9842 		kfree(ppdu_stats);
9843 	}
9844 	spin_unlock_bh(&dp_pdev->ppdu_list_lock);
9845 
9846 	spin_lock_bh(&ar->data_lock);
9847 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9848 					       struct ath12k_wmi_scan_chan_list_arg,
9849 					       list))) {
9850 		list_del(&arg->list);
9851 		kfree(arg);
9852 	}
9853 	spin_unlock_bh(&ar->data_lock);
9854 
9855 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9856 
9857 	synchronize_rcu();
9858 
9859 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9860 }
9861 
ath12k_mac_op_stop(struct ieee80211_hw * hw,bool suspend)9862 void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9863 {
9864 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9865 	struct ath12k *ar;
9866 	int i;
9867 
9868 	lockdep_assert_wiphy(hw->wiphy);
9869 
9870 	ath12k_drain_tx(ah);
9871 
9872 	mutex_lock(&ah->hw_mutex);
9873 
9874 	ah->state = ATH12K_HW_STATE_OFF;
9875 
9876 	for_each_ar(ah, ar, i)
9877 		ath12k_mac_stop(ar);
9878 
9879 	mutex_unlock(&ah->hw_mutex);
9880 }
9881 EXPORT_SYMBOL(ath12k_mac_op_stop);
9882 
9883 static u8
ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif * arvif)9884 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9885 {
9886 	struct ath12k_base *ab = arvif->ar->ab;
9887 	u8 vdev_stats_id = 0;
9888 
9889 	do {
9890 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
9891 			vdev_stats_id++;
9892 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
9893 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
9894 				break;
9895 			}
9896 		} else {
9897 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
9898 			break;
9899 		}
9900 	} while (vdev_stats_id);
9901 
9902 	arvif->vdev_stats_id = vdev_stats_id;
9903 	return vdev_stats_id;
9904 }
9905 
ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif * arvif,u32 * flags,u32 * tx_vdev_id)9906 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
9907 					       u32 *flags, u32 *tx_vdev_id)
9908 {
9909 	struct ath12k_vif *ahvif = arvif->ahvif;
9910 	struct ieee80211_bss_conf *link_conf;
9911 	struct ath12k *ar = arvif->ar;
9912 	struct ath12k_link_vif *tx_arvif;
9913 
9914 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
9915 	if (!link_conf) {
9916 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
9917 			    ahvif->vif->addr, arvif->link_id);
9918 		return -ENOLINK;
9919 	}
9920 
9921 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
9922 	if (!tx_arvif)
9923 		return 0;
9924 
9925 	if (link_conf->nontransmitted) {
9926 		if (ath12k_ar_to_hw(ar)->wiphy !=
9927 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
9928 			return -EINVAL;
9929 
9930 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
9931 		*tx_vdev_id = tx_arvif->vdev_id;
9932 	} else if (tx_arvif == arvif) {
9933 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
9934 	} else {
9935 		return -EINVAL;
9936 	}
9937 
9938 	if (link_conf->ema_ap)
9939 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
9940 
9941 	return 0;
9942 }
9943 
ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif * arvif,struct ath12k_wmi_vdev_create_arg * arg)9944 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
9945 					    struct ath12k_wmi_vdev_create_arg *arg)
9946 {
9947 	struct ath12k *ar = arvif->ar;
9948 	struct ath12k_pdev *pdev = ar->pdev;
9949 	struct ath12k_vif *ahvif = arvif->ahvif;
9950 	int ret;
9951 
9952 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9953 
9954 	arg->if_id = arvif->vdev_id;
9955 	arg->type = ahvif->vdev_type;
9956 	arg->subtype = ahvif->vdev_subtype;
9957 	arg->pdev_id = pdev->pdev_id;
9958 
9959 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
9960 	arg->mbssid_tx_vdev_id = 0;
9961 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
9962 		      ar->ab->wmi_ab.svc_map)) {
9963 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
9964 							  &arg->mbssid_flags,
9965 							  &arg->mbssid_tx_vdev_id);
9966 		if (ret)
9967 			return ret;
9968 	}
9969 
9970 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9971 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
9972 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
9973 	}
9974 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9975 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
9976 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
9977 	}
9978 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9979 	    ar->supports_6ghz) {
9980 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
9981 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
9982 	}
9983 
9984 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
9985 
9986 	if (ath12k_mac_is_ml_arvif(arvif)) {
9987 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
9988 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
9989 				    ahvif->vif->valid_links);
9990 			return -EINVAL;
9991 		}
9992 
9993 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
9994 	}
9995 
9996 	return 0;
9997 }
9998 
ath12k_mac_update_vif_offload(struct ath12k_link_vif * arvif)9999 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
10000 {
10001 	struct ath12k_vif *ahvif = arvif->ahvif;
10002 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10003 	struct ath12k *ar = arvif->ar;
10004 	struct ath12k_base *ab = ar->ab;
10005 	u32 param_id, param_value;
10006 	int ret;
10007 
10008 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
10009 	if (vif->type != NL80211_IFTYPE_STATION &&
10010 	    vif->type != NL80211_IFTYPE_AP)
10011 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
10012 					IEEE80211_OFFLOAD_DECAP_ENABLED);
10013 
10014 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
10015 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
10016 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10017 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_RAW;
10018 	else
10019 		ahvif->dp_vif.tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
10020 
10021 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10022 					    param_id, ahvif->dp_vif.tx_encap_type);
10023 	if (ret) {
10024 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
10025 			    arvif->vdev_id, ret);
10026 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
10027 	}
10028 
10029 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
10030 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
10031 		param_value = ATH12K_HW_TXRX_ETHERNET;
10032 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
10033 		param_value = ATH12K_HW_TXRX_RAW;
10034 	else
10035 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
10036 
10037 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10038 					    param_id, param_value);
10039 	if (ret) {
10040 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
10041 			    arvif->vdev_id, ret);
10042 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
10043 	}
10044 }
10045 
ath12k_mac_op_update_vif_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10046 void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
10047 				      struct ieee80211_vif *vif)
10048 {
10049 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10050 	struct ath12k_link_vif *arvif;
10051 	unsigned long links;
10052 	int link_id;
10053 
10054 	lockdep_assert_wiphy(hw->wiphy);
10055 
10056 	if (vif->valid_links) {
10057 		links = vif->valid_links;
10058 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10059 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10060 			if (!(arvif && arvif->ar))
10061 				continue;
10062 
10063 			ath12k_mac_update_vif_offload(arvif);
10064 		}
10065 
10066 		return;
10067 	}
10068 
10069 	ath12k_mac_update_vif_offload(&ahvif->deflink);
10070 }
10071 EXPORT_SYMBOL(ath12k_mac_op_update_vif_offload);
10072 
ath12k_mac_vif_ap_active_any(struct ath12k_base * ab)10073 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
10074 {
10075 	struct ath12k *ar;
10076 	struct ath12k_pdev *pdev;
10077 	struct ath12k_link_vif *arvif;
10078 	int i;
10079 
10080 	for (i = 0; i < ab->num_radios; i++) {
10081 		pdev = &ab->pdevs[i];
10082 		ar = pdev->ar;
10083 		list_for_each_entry(arvif, &ar->arvifs, list) {
10084 			if (arvif->is_up &&
10085 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
10086 				return true;
10087 		}
10088 	}
10089 	return false;
10090 }
10091 
ath12k_mac_11d_scan_start(struct ath12k * ar,u32 vdev_id)10092 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
10093 {
10094 	struct wmi_11d_scan_start_arg arg;
10095 	int ret;
10096 
10097 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10098 
10099 	if (ar->regdom_set_by_user)
10100 		goto fin;
10101 
10102 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
10103 		goto fin;
10104 
10105 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10106 		goto fin;
10107 
10108 	if (ath12k_mac_vif_ap_active_any(ar->ab))
10109 		goto fin;
10110 
10111 	arg.vdev_id = vdev_id;
10112 	arg.start_interval_msec = 0;
10113 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
10114 
10115 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10116 		   "mac start 11d scan for vdev %d\n", vdev_id);
10117 
10118 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
10119 	if (ret) {
10120 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
10121 			    vdev_id, ret);
10122 	} else {
10123 		ar->vdev_id_11d_scan = vdev_id;
10124 		if (ar->state_11d == ATH12K_11D_PREPARING)
10125 			ar->state_11d = ATH12K_11D_RUNNING;
10126 	}
10127 
10128 fin:
10129 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10130 		ar->state_11d = ATH12K_11D_IDLE;
10131 		complete(&ar->completed_11d_scan);
10132 	}
10133 }
10134 
ath12k_mac_11d_scan_stop(struct ath12k * ar)10135 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
10136 {
10137 	int ret;
10138 	u32 vdev_id;
10139 
10140 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10141 
10142 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
10143 		return;
10144 
10145 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
10146 		   ar->vdev_id_11d_scan);
10147 
10148 	if (ar->state_11d == ATH12K_11D_PREPARING) {
10149 		ar->state_11d = ATH12K_11D_IDLE;
10150 		complete(&ar->completed_11d_scan);
10151 	}
10152 
10153 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
10154 		vdev_id = ar->vdev_id_11d_scan;
10155 
10156 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
10157 		if (ret) {
10158 			ath12k_warn(ar->ab,
10159 				    "failed to stopt 11d scan vdev %d ret: %d\n",
10160 				    vdev_id, ret);
10161 		} else {
10162 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
10163 			ar->state_11d = ATH12K_11D_IDLE;
10164 			complete(&ar->completed_11d_scan);
10165 		}
10166 	}
10167 }
10168 
ath12k_mac_11d_scan_stop_all(struct ath12k_base * ab)10169 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
10170 {
10171 	struct ath12k *ar;
10172 	struct ath12k_pdev *pdev;
10173 	int i;
10174 
10175 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
10176 
10177 	for (i = 0; i < ab->num_radios; i++) {
10178 		pdev = &ab->pdevs[i];
10179 		ar = pdev->ar;
10180 
10181 		ath12k_mac_11d_scan_stop(ar);
10182 	}
10183 }
10184 
ath12k_mac_determine_vdev_type(struct ieee80211_vif * vif,struct ath12k_vif * ahvif)10185 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
10186 					   struct ath12k_vif *ahvif)
10187 {
10188 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
10189 
10190 	switch (vif->type) {
10191 	case NL80211_IFTYPE_UNSPECIFIED:
10192 	case NL80211_IFTYPE_STATION:
10193 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10194 
10195 		if (vif->p2p)
10196 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
10197 
10198 		break;
10199 	case NL80211_IFTYPE_MESH_POINT:
10200 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
10201 		fallthrough;
10202 	case NL80211_IFTYPE_AP:
10203 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
10204 
10205 		if (vif->p2p)
10206 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
10207 
10208 		break;
10209 	case NL80211_IFTYPE_MONITOR:
10210 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
10211 		break;
10212 	case NL80211_IFTYPE_P2P_DEVICE:
10213 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
10214 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
10215 		break;
10216 	default:
10217 		WARN_ON(1);
10218 		break;
10219 	}
10220 }
10221 
ath12k_mac_vdev_create(struct ath12k * ar,struct ath12k_link_vif * arvif)10222 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
10223 {
10224 	struct ath12k_hw *ah = ar->ah;
10225 	struct ath12k_base *ab = ar->ab;
10226 	struct ieee80211_hw *hw = ah->hw;
10227 	struct ath12k_vif *ahvif = arvif->ahvif;
10228 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10229 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
10230 	struct ath12k_wmi_peer_create_arg peer_param = {};
10231 	struct ieee80211_bss_conf *link_conf = NULL;
10232 	u32 param_id, param_value;
10233 	u16 nss;
10234 	int i;
10235 	int ret, vdev_id;
10236 	u8 link_id;
10237 	struct ath12k_dp_link_vif *dp_link_vif = NULL;
10238 	struct ath12k_dp_peer_create_params params = {};
10239 	bool dp_peer_created = false;
10240 
10241 	lockdep_assert_wiphy(hw->wiphy);
10242 
10243 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
10244 	 * interface in each radio
10245 	 */
10246 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
10247 		return -EINVAL;
10248 
10249 	if (ar->num_created_vdevs >= TARGET_NUM_VDEVS(ab)) {
10250 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10251 			    TARGET_NUM_VDEVS(ab));
10252 		return -ENOSPC;
10253 	}
10254 
10255 	link_id = arvif->link_id;
10256 
10257 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10258 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
10259 		if (!link_conf) {
10260 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
10261 				    vif->addr, arvif->link_id);
10262 			return -ENOLINK;
10263 		}
10264 	}
10265 
10266 	if (link_conf)
10267 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
10268 	else
10269 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
10270 
10271 	arvif->ar = ar;
10272 	vdev_id = __ffs64(ab->free_vdev_map);
10273 	arvif->vdev_id = vdev_id;
10274 	if (vif->type == NL80211_IFTYPE_MONITOR)
10275 		ar->monitor_vdev_id = vdev_id;
10276 
10277 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
10278 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
10279 		   ab->free_vdev_map);
10280 
10281 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
10282 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10283 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
10284 
10285 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
10286 	if (ret) {
10287 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
10288 			    arvif->vdev_id, ret);
10289 		goto err;
10290 	}
10291 
10292 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
10293 	if (ret) {
10294 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
10295 			    arvif->vdev_id, ret);
10296 		return ret;
10297 	}
10298 
10299 	ar->num_created_vdevs++;
10300 	arvif->is_created = true;
10301 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
10302 		   vif->addr, arvif->vdev_id);
10303 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
10304 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
10305 
10306 	spin_lock_bh(&ar->data_lock);
10307 	list_add(&arvif->list, &ar->arvifs);
10308 	spin_unlock_bh(&ar->data_lock);
10309 
10310 	ath12k_mac_update_vif_offload(arvif);
10311 
10312 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
10313 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10314 					    WMI_VDEV_PARAM_NSS, nss);
10315 	if (ret) {
10316 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
10317 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
10318 		goto err_vdev_del;
10319 	}
10320 
10321 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10322 
10323 	dp_link_vif->vdev_id = arvif->vdev_id;
10324 	dp_link_vif->lmac_id = ar->lmac_id;
10325 	dp_link_vif->pdev_idx = ar->pdev_idx;
10326 
10327 	switch (ahvif->vdev_type) {
10328 	case WMI_VDEV_TYPE_AP:
10329 		params.ucast_ra_only = true;
10330 
10331 		if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
10332 			ret = ath12k_dp_peer_create(&ah->dp_hw, arvif->bssid, &params);
10333 			if (ret) {
10334 				ath12k_warn(ab, "failed to vdev %d create dp_peer for AP: %d\n",
10335 					    arvif->vdev_id, ret);
10336 				goto err_vdev_del;
10337 			}
10338 			dp_peer_created = true;
10339 		}
10340 
10341 		peer_param.vdev_id = arvif->vdev_id;
10342 		peer_param.peer_addr = arvif->bssid;
10343 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
10344 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
10345 		if (ret) {
10346 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
10347 				    arvif->vdev_id, ret);
10348 			goto err_dp_peer_del;
10349 		}
10350 
10351 		ret = ath12k_mac_set_kickout(arvif);
10352 		if (ret) {
10353 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
10354 				    arvif->vdev_id, ret);
10355 			goto err_peer_del;
10356 		}
10357 		ath12k_mac_11d_scan_stop_all(ar->ab);
10358 		break;
10359 	case WMI_VDEV_TYPE_STA:
10360 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
10361 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
10362 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10363 						  param_id, param_value);
10364 		if (ret) {
10365 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
10366 				    arvif->vdev_id, ret);
10367 			goto err_peer_del;
10368 		}
10369 
10370 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
10371 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
10372 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10373 						  param_id, param_value);
10374 		if (ret) {
10375 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
10376 				    arvif->vdev_id, ret);
10377 			goto err_peer_del;
10378 		}
10379 
10380 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
10381 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
10382 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
10383 						  param_id, param_value);
10384 		if (ret) {
10385 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
10386 				    arvif->vdev_id, ret);
10387 			goto err_peer_del;
10388 		}
10389 
10390 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
10391 		if (ret) {
10392 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
10393 				    arvif->vdev_id, ret);
10394 			goto err_peer_del;
10395 		}
10396 
10397 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
10398 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
10399 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
10400 			reinit_completion(&ar->completed_11d_scan);
10401 			ar->state_11d = ATH12K_11D_PREPARING;
10402 		}
10403 		break;
10404 	case WMI_VDEV_TYPE_MONITOR:
10405 		ar->monitor_vdev_created = true;
10406 		break;
10407 	default:
10408 		break;
10409 	}
10410 
10411 	if (link_conf)
10412 		arvif->txpower = link_conf->txpower;
10413 	else
10414 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
10415 
10416 	ret = ath12k_mac_txpower_recalc(ar);
10417 	if (ret)
10418 		goto err_peer_del;
10419 
10420 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
10421 	param_value = hw->wiphy->rts_threshold;
10422 	ar->rts_threshold = param_value;
10423 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
10424 					    param_id, param_value);
10425 	if (ret) {
10426 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
10427 			    arvif->vdev_id, ret);
10428 	}
10429 
10430 	ath12k_dp_vdev_tx_attach(ar, arvif);
10431 
10432 	return ret;
10433 
10434 err_peer_del:
10435 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10436 		reinit_completion(&ar->peer_delete_done);
10437 
10438 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
10439 						      arvif->vdev_id);
10440 		if (ret) {
10441 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
10442 				    arvif->vdev_id, arvif->bssid);
10443 			goto err;
10444 		}
10445 
10446 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
10447 						       arvif->bssid);
10448 		if (ret)
10449 			goto err_vdev_del;
10450 
10451 		ar->num_peers--;
10452 	}
10453 
10454 err_dp_peer_del:
10455 	if (dp_peer_created)
10456 		ath12k_dp_peer_delete(&ah->dp_hw, arvif->bssid, NULL);
10457 
10458 err_vdev_del:
10459 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10460 		ar->monitor_vdev_id = -1;
10461 		ar->monitor_vdev_created = false;
10462 	}
10463 
10464 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10465 	ar->num_created_vdevs--;
10466 	arvif->is_created = false;
10467 	arvif->ar = NULL;
10468 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10469 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10470 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
10471 	spin_lock_bh(&ar->data_lock);
10472 	list_del(&arvif->list);
10473 	spin_unlock_bh(&ar->data_lock);
10474 
10475 err:
10476 	arvif->ar = NULL;
10477 	return ret;
10478 }
10479 
ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif * arvif)10480 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
10481 {
10482 	struct ath12k_key_conf *key_conf, *tmp;
10483 	struct ath12k_vif *ahvif = arvif->ahvif;
10484 	struct ath12k_hw *ah = ahvif->ah;
10485 	struct ath12k_sta *ahsta;
10486 	struct ath12k_link_sta *arsta;
10487 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10488 	int ret;
10489 
10490 	lockdep_assert_wiphy(ah->hw->wiphy);
10491 
10492 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
10493 		arsta = NULL;
10494 		if (key_conf->sta) {
10495 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
10496 			arsta = wiphy_dereference(ah->hw->wiphy,
10497 						  ahsta->link[arvif->link_id]);
10498 			if (!arsta)
10499 				goto free_cache;
10500 		}
10501 
10502 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
10503 					 arvif, arsta,
10504 					 key_conf->key);
10505 		if (ret)
10506 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
10507 				    arvif->vdev_id, ret);
10508 free_cache:
10509 		list_del(&key_conf->list);
10510 		kfree(key_conf);
10511 	}
10512 }
10513 
ath12k_mac_vif_cache_flush(struct ath12k * ar,struct ath12k_link_vif * arvif)10514 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
10515 {
10516 	struct ath12k_vif *ahvif = arvif->ahvif;
10517 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10518 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
10519 	struct ath12k_base *ab = ar->ab;
10520 	struct ieee80211_bss_conf *link_conf;
10521 
10522 	int ret;
10523 
10524 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10525 
10526 	if (!cache)
10527 		return;
10528 
10529 	if (cache->tx_conf.changed) {
10530 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
10531 					 &cache->tx_conf.tx_queue_params);
10532 		if (ret)
10533 			ath12k_warn(ab,
10534 				    "unable to apply tx config parameters to vdev %d\n",
10535 				    ret);
10536 	}
10537 
10538 	if (cache->bss_conf_changed) {
10539 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
10540 		if (!link_conf) {
10541 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
10542 				    vif->addr, arvif->link_id);
10543 			return;
10544 		}
10545 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
10546 					    cache->bss_conf_changed);
10547 	}
10548 
10549 	if (!list_empty(&cache->key_conf.list))
10550 		ath12k_mac_vif_flush_key_cache(arvif);
10551 
10552 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10553 }
10554 
ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw * hw,struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)10555 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
10556 						    struct ath12k_link_vif *arvif,
10557 						    struct ieee80211_chanctx_conf *ctx)
10558 {
10559 	struct ath12k_vif *ahvif = arvif->ahvif;
10560 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10561 	struct ath12k_link_vif *scan_arvif;
10562 	struct ath12k_hw *ah = hw->priv;
10563 	struct ath12k *ar;
10564 	struct ath12k_base *ab;
10565 	u8 link_id = arvif->link_id, scan_link_id;
10566 	unsigned long scan_link_map;
10567 	int ret;
10568 
10569 	lockdep_assert_wiphy(hw->wiphy);
10570 
10571 	if (ah->num_radio == 1)
10572 		ar = ah->radio;
10573 	else if (ctx)
10574 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10575 	else
10576 		return NULL;
10577 
10578 	if (!ar)
10579 		return NULL;
10580 
10581 	/* cleanup the scan vdev if we are done scan on that ar
10582 	 * and now we want to create for actual usage.
10583 	 */
10584 	if (ieee80211_vif_is_mld(vif)) {
10585 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10586 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10587 			scan_arvif = wiphy_dereference(hw->wiphy,
10588 						       ahvif->link[scan_link_id]);
10589 			if (scan_arvif && scan_arvif->ar == ar) {
10590 				ar->scan.arvif = NULL;
10591 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10592 				ath12k_mac_unassign_link_vif(scan_arvif);
10593 				break;
10594 			}
10595 		}
10596 	}
10597 
10598 	if (arvif->ar) {
10599 		/* This is not expected really */
10600 		if (WARN_ON(!arvif->is_created)) {
10601 			arvif->ar = NULL;
10602 			return NULL;
10603 		}
10604 
10605 		if (ah->num_radio == 1)
10606 			return arvif->ar;
10607 
10608 		/* This can happen as scan vdev gets created during multiple scans
10609 		 * across different radios before a vdev is brought up in
10610 		 * a certain radio.
10611 		 */
10612 		if (ar != arvif->ar) {
10613 			if (WARN_ON(arvif->is_started))
10614 				return NULL;
10615 
10616 			ath12k_mac_remove_link_interface(hw, arvif);
10617 			ath12k_mac_unassign_link_vif(arvif);
10618 		}
10619 	}
10620 
10621 	ab = ar->ab;
10622 
10623 	/* Assign arvif again here since previous radio switch block
10624 	 * would've unassigned and cleared it.
10625 	 */
10626 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10627 	if (vif->type == NL80211_IFTYPE_AP &&
10628 	    ar->num_peers > (ar->max_num_peers - 1)) {
10629 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10630 		goto unlock;
10631 	}
10632 
10633 	if (arvif->is_created)
10634 		goto flush;
10635 
10636 	ret = ath12k_mac_vdev_create(ar, arvif);
10637 	if (ret) {
10638 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10639 		goto unlock;
10640 	}
10641 
10642 flush:
10643 	/* If the vdev is created during channel assign and not during
10644 	 * add_interface(), Apply any parameters for the vdev which were received
10645 	 * after add_interface, corresponding to this vif.
10646 	 */
10647 	ath12k_mac_vif_cache_flush(ar, arvif);
10648 unlock:
10649 	return arvif->ar;
10650 }
10651 
ath12k_mac_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10652 int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10653 				struct ieee80211_vif *vif)
10654 {
10655 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10656 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10657 	struct ath12k_reg_info *reg_info;
10658 	struct ath12k_link_vif *arvif;
10659 	struct ath12k_base *ab;
10660 	struct ath12k *ar;
10661 	int i;
10662 
10663 	lockdep_assert_wiphy(hw->wiphy);
10664 
10665 	memset(ahvif, 0, sizeof(*ahvif));
10666 
10667 	ahvif->ah = ah;
10668 	ahvif->vif = vif;
10669 	arvif = &ahvif->deflink;
10670 
10671 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10672 
10673 	/* Allocate Default Queue now and reassign during actual vdev create */
10674 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10675 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10676 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10677 
10678 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10679 
10680 	ath12k_mac_determine_vdev_type(vif, ahvif);
10681 
10682 	for_each_ar(ah, ar, i) {
10683 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10684 			continue;
10685 
10686 		ab = ar->ab;
10687 		reg_info = ab->reg_info[ar->pdev_idx];
10688 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10689 		ah->regd_updated = false;
10690 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10691 					    IEEE80211_REG_UNSET_AP);
10692 		break;
10693 	}
10694 
10695 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10696 	 * will not know if this interface is an ML vif at this point.
10697 	 */
10698 	return 0;
10699 }
10700 EXPORT_SYMBOL(ath12k_mac_op_add_interface);
10701 
ath12k_mac_vif_unref(struct ath12k_dp * dp,struct ieee80211_vif * vif)10702 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10703 {
10704 	struct ath12k_tx_desc_info *tx_desc_info;
10705 	struct ath12k_skb_cb *skb_cb;
10706 	struct sk_buff *skb;
10707 	int i;
10708 
10709 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10710 		spin_lock_bh(&dp->tx_desc_lock[i]);
10711 
10712 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10713 				    list) {
10714 			skb = tx_desc_info->skb;
10715 			if (!skb)
10716 				continue;
10717 
10718 			skb_cb = ATH12K_SKB_CB(skb);
10719 			if (skb_cb->vif == vif)
10720 				skb_cb->vif = NULL;
10721 		}
10722 
10723 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10724 	}
10725 }
10726 
ath12k_mac_vdev_delete(struct ath12k * ar,struct ath12k_link_vif * arvif)10727 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10728 {
10729 	struct ath12k_vif *ahvif = arvif->ahvif;
10730 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10731 	struct ath12k_dp_link_vif *dp_link_vif;
10732 	struct ath12k_base *ab = ar->ab;
10733 	unsigned long time_left;
10734 	int ret;
10735 
10736 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10737 
10738 	reinit_completion(&ar->vdev_delete_done);
10739 
10740 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10741 	if (ret) {
10742 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10743 			    arvif->vdev_id, ret);
10744 		goto err_vdev_del;
10745 	}
10746 
10747 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10748 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10749 	if (time_left == 0) {
10750 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10751 		goto err_vdev_del;
10752 	}
10753 
10754 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10755 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10756 	ar->num_created_vdevs--;
10757 
10758 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10759 		ar->monitor_vdev_id = -1;
10760 		ar->monitor_vdev_created = false;
10761 	}
10762 
10763 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10764 		   vif->addr, arvif->vdev_id);
10765 
10766 err_vdev_del:
10767 	spin_lock_bh(&ar->data_lock);
10768 	list_del(&arvif->list);
10769 	spin_unlock_bh(&ar->data_lock);
10770 
10771 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10772 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10773 
10774 	idr_for_each(&ar->txmgmt_idr,
10775 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10776 
10777 	ath12k_mac_vif_unref(ath12k_ab_to_dp(ab), vif);
10778 
10779 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, arvif->link_id);
10780 	ath12k_dp_tx_put_bank_profile(ath12k_ab_to_dp(ab), dp_link_vif->bank_id);
10781 
10782 	/* Recalc txpower for remaining vdev */
10783 	ath12k_mac_txpower_recalc(ar);
10784 
10785 	/* TODO: recal traffic pause state based on the available vdevs */
10786 	arvif->is_created = false;
10787 	arvif->ar = NULL;
10788 
10789 	return ret;
10790 }
10791 
ath12k_mac_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10792 void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10793 				    struct ieee80211_vif *vif)
10794 {
10795 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10796 	struct ath12k_link_vif *arvif;
10797 	struct ath12k *ar;
10798 	u8 link_id;
10799 
10800 	lockdep_assert_wiphy(hw->wiphy);
10801 
10802 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10803 		/* if we cached some config but never received assign chanctx,
10804 		 * free the allocated cache.
10805 		 */
10806 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10807 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10808 		if (!arvif || !arvif->is_created)
10809 			continue;
10810 
10811 		ar = arvif->ar;
10812 
10813 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10814 		 * is expected to be executed. Since link is anyways going to be removed
10815 		 * now, just cancel the worker and send the scan aborted to user space
10816 		 */
10817 		if (ar->scan.arvif == arvif) {
10818 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10819 
10820 			spin_lock_bh(&ar->data_lock);
10821 			ar->scan.arvif = NULL;
10822 			if (!ar->scan.is_roc) {
10823 				struct cfg80211_scan_info info = {
10824 					.aborted = true,
10825 				};
10826 
10827 				ath12k_mac_scan_send_complete(ar, &info);
10828 			}
10829 
10830 			ar->scan.state = ATH12K_SCAN_IDLE;
10831 			ar->scan_channel = NULL;
10832 			ar->scan.roc_freq = 0;
10833 			spin_unlock_bh(&ar->data_lock);
10834 		}
10835 
10836 		ath12k_mac_remove_link_interface(hw, arvif);
10837 		ath12k_mac_unassign_link_vif(arvif);
10838 	}
10839 }
10840 EXPORT_SYMBOL(ath12k_mac_op_remove_interface);
10841 
10842 /* FIXME: Has to be verified. */
10843 #define SUPPORTED_FILTERS			\
10844 	(FIF_ALLMULTI |				\
10845 	FIF_CONTROL |				\
10846 	FIF_PSPOLL |				\
10847 	FIF_OTHER_BSS |				\
10848 	FIF_BCN_PRBRESP_PROMISC |		\
10849 	FIF_PROBE_REQ |				\
10850 	FIF_FCSFAIL)
10851 
ath12k_mac_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)10852 void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10853 				    unsigned int changed_flags,
10854 				    unsigned int *total_flags,
10855 				    u64 multicast)
10856 {
10857 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10858 	struct ath12k *ar;
10859 
10860 	lockdep_assert_wiphy(hw->wiphy);
10861 
10862 	ar = ath12k_ah_to_ar(ah, 0);
10863 
10864 	*total_flags &= SUPPORTED_FILTERS;
10865 	ar->filter_flags = *total_flags;
10866 }
10867 EXPORT_SYMBOL(ath12k_mac_op_configure_filter);
10868 
ath12k_mac_op_get_antenna(struct ieee80211_hw * hw,int radio_idx,u32 * tx_ant,u32 * rx_ant)10869 int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
10870 			      u32 *tx_ant, u32 *rx_ant)
10871 {
10872 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10873 	int antennas_rx = 0, antennas_tx = 0;
10874 	struct ath12k *ar;
10875 	int i;
10876 
10877 	lockdep_assert_wiphy(hw->wiphy);
10878 
10879 	for_each_ar(ah, ar, i) {
10880 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
10881 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
10882 	}
10883 
10884 	*tx_ant = antennas_tx;
10885 	*rx_ant = antennas_rx;
10886 
10887 	return 0;
10888 }
10889 EXPORT_SYMBOL(ath12k_mac_op_get_antenna);
10890 
ath12k_mac_op_set_antenna(struct ieee80211_hw * hw,int radio_idx,u32 tx_ant,u32 rx_ant)10891 int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
10892 			      u32 tx_ant, u32 rx_ant)
10893 {
10894 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10895 	struct ath12k *ar;
10896 	int ret = 0;
10897 	int i;
10898 
10899 	lockdep_assert_wiphy(hw->wiphy);
10900 
10901 	for_each_ar(ah, ar, i) {
10902 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
10903 		if (ret)
10904 			break;
10905 	}
10906 
10907 	return ret;
10908 }
10909 EXPORT_SYMBOL(ath12k_mac_op_set_antenna);
10910 
ath12k_mac_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params,u8 link_id)10911 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
10912 				   struct ieee80211_vif *vif,
10913 				   struct ieee80211_ampdu_params *params,
10914 				   u8 link_id)
10915 {
10916 	struct ath12k *ar;
10917 	int ret = -EINVAL;
10918 
10919 	lockdep_assert_wiphy(hw->wiphy);
10920 
10921 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
10922 	if (!ar)
10923 		return -EINVAL;
10924 
10925 	switch (params->action) {
10926 	case IEEE80211_AMPDU_RX_START:
10927 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
10928 		break;
10929 	case IEEE80211_AMPDU_RX_STOP:
10930 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
10931 		break;
10932 	case IEEE80211_AMPDU_TX_START:
10933 	case IEEE80211_AMPDU_TX_STOP_CONT:
10934 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
10935 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
10936 	case IEEE80211_AMPDU_TX_OPERATIONAL:
10937 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
10938 		 * Tx aggregation requests.
10939 		 */
10940 		ret = -EOPNOTSUPP;
10941 		break;
10942 	}
10943 
10944 	if (ret)
10945 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
10946 			    params->action, vif->addr, link_id, ret);
10947 
10948 	return ret;
10949 }
10950 
ath12k_mac_op_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)10951 int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
10952 			       struct ieee80211_vif *vif,
10953 			       struct ieee80211_ampdu_params *params)
10954 {
10955 	struct ieee80211_sta *sta = params->sta;
10956 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
10957 	unsigned long links_map = ahsta->links_map;
10958 	int ret = -EINVAL;
10959 	u8 link_id;
10960 
10961 	lockdep_assert_wiphy(hw->wiphy);
10962 
10963 	if (WARN_ON(!links_map))
10964 		return ret;
10965 
10966 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10967 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
10968 		if (ret)
10969 			return ret;
10970 	}
10971 
10972 	return 0;
10973 }
10974 EXPORT_SYMBOL(ath12k_mac_op_ampdu_action);
10975 
ath12k_mac_op_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)10976 int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
10977 			      struct ieee80211_chanctx_conf *ctx)
10978 {
10979 	struct ath12k *ar;
10980 	struct ath12k_base *ab;
10981 
10982 	lockdep_assert_wiphy(hw->wiphy);
10983 
10984 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10985 	if (!ar)
10986 		return -EINVAL;
10987 
10988 	ab = ar->ab;
10989 
10990 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10991 		   "mac chanctx add freq %u width %d ptr %p\n",
10992 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10993 
10994 	spin_lock_bh(&ar->data_lock);
10995 	/* TODO: In case of multiple channel context, populate rx_channel from
10996 	 * Rx PPDU desc information.
10997 	 */
10998 	ar->rx_channel = ctx->def.chan;
10999 	spin_unlock_bh(&ar->data_lock);
11000 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11001 
11002 	return 0;
11003 }
11004 EXPORT_SYMBOL(ath12k_mac_op_add_chanctx);
11005 
ath12k_mac_op_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)11006 void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
11007 				  struct ieee80211_chanctx_conf *ctx)
11008 {
11009 	struct ath12k *ar;
11010 	struct ath12k_base *ab;
11011 
11012 	lockdep_assert_wiphy(hw->wiphy);
11013 
11014 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11015 	if (!ar)
11016 		return;
11017 
11018 	ab = ar->ab;
11019 
11020 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11021 		   "mac chanctx remove freq %u width %d ptr %p\n",
11022 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
11023 
11024 	spin_lock_bh(&ar->data_lock);
11025 	/* TODO: In case of there is one more channel context left, populate
11026 	 * rx_channel with the channel of that remaining channel context.
11027 	 */
11028 	ar->rx_channel = NULL;
11029 	spin_unlock_bh(&ar->data_lock);
11030 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
11031 }
11032 EXPORT_SYMBOL(ath12k_mac_op_remove_chanctx);
11033 
11034 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)11035 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
11036 				     enum wmi_phy_mode mode,
11037 				     enum nl80211_band band,
11038 				     enum nl80211_iftype type)
11039 {
11040 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
11041 	enum wmi_phy_mode down_mode;
11042 	int n = ar->mac.sbands[band].n_iftype_data;
11043 	int i;
11044 	struct ieee80211_sband_iftype_data *data;
11045 
11046 	if (mode < MODE_11BE_EHT20)
11047 		return mode;
11048 
11049 	data = ar->mac.iftype[band];
11050 	for (i = 0; i < n; i++) {
11051 		if (data[i].types_mask & BIT(type)) {
11052 			eht_cap = &data[i].eht_cap;
11053 			break;
11054 		}
11055 	}
11056 
11057 	if (eht_cap && eht_cap->has_eht)
11058 		return mode;
11059 
11060 	switch (mode) {
11061 	case MODE_11BE_EHT20:
11062 		down_mode = MODE_11AX_HE20;
11063 		break;
11064 	case MODE_11BE_EHT40:
11065 		down_mode = MODE_11AX_HE40;
11066 		break;
11067 	case MODE_11BE_EHT80:
11068 		down_mode = MODE_11AX_HE80;
11069 		break;
11070 	case MODE_11BE_EHT80_80:
11071 		down_mode = MODE_11AX_HE80_80;
11072 		break;
11073 	case MODE_11BE_EHT160:
11074 	case MODE_11BE_EHT160_160:
11075 	case MODE_11BE_EHT320:
11076 		down_mode = MODE_11AX_HE160;
11077 		break;
11078 	case MODE_11BE_EHT20_2G:
11079 		down_mode = MODE_11AX_HE20_2G;
11080 		break;
11081 	case MODE_11BE_EHT40_2G:
11082 		down_mode = MODE_11AX_HE40_2G;
11083 		break;
11084 	default:
11085 		down_mode = mode;
11086 		break;
11087 	}
11088 
11089 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11090 		   "mac vdev start phymode %s downgrade to %s\n",
11091 		   ath12k_mac_phymode_str(mode),
11092 		   ath12k_mac_phymode_str(down_mode));
11093 
11094 	return down_mode;
11095 }
11096 
11097 static void
ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif * arvif,struct wmi_ml_arg * ml_arg)11098 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
11099 			     struct wmi_ml_arg *ml_arg)
11100 {
11101 	struct ath12k_vif *ahvif = arvif->ahvif;
11102 	struct wmi_ml_partner_info *partner_info;
11103 	struct ieee80211_bss_conf *link_conf;
11104 	struct ath12k_link_vif *arvif_p;
11105 	unsigned long links;
11106 	u8 link_id;
11107 
11108 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11109 
11110 	if (!ath12k_mac_is_ml_arvif(arvif))
11111 		return;
11112 
11113 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
11114 		return;
11115 
11116 	ml_arg->enabled = true;
11117 
11118 	/* Driver always add a new link via VDEV START, FW takes
11119 	 * care of internally adding this link to existing
11120 	 * link vdevs which are advertised as partners below
11121 	 */
11122 	ml_arg->link_add = true;
11123 
11124 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
11125 
11126 	partner_info = ml_arg->partner_info;
11127 
11128 	links = ahvif->links_map;
11129 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11130 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11131 
11132 		if (WARN_ON(!arvif_p))
11133 			continue;
11134 
11135 		if (arvif == arvif_p)
11136 			continue;
11137 
11138 		if (!arvif_p->is_started)
11139 			continue;
11140 
11141 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11142 					      ahvif->vif->link_conf[arvif_p->link_id]);
11143 
11144 		if (!link_conf)
11145 			continue;
11146 
11147 		partner_info->vdev_id = arvif_p->vdev_id;
11148 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
11149 		ether_addr_copy(partner_info->addr, link_conf->addr);
11150 		ml_arg->num_partner_links++;
11151 		partner_info++;
11152 	}
11153 }
11154 
11155 static int
ath12k_mac_vdev_start_restart(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx,bool restart)11156 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
11157 			      struct ieee80211_chanctx_conf *ctx,
11158 			      bool restart)
11159 {
11160 	struct ath12k *ar = arvif->ar;
11161 	struct ath12k_base *ab = ar->ab;
11162 	struct wmi_vdev_start_req_arg arg = {};
11163 	const struct cfg80211_chan_def *chandef = &ctx->def;
11164 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11165 	struct ath12k_vif *ahvif = arvif->ahvif;
11166 	struct ieee80211_bss_conf *link_conf;
11167 	unsigned int dfs_cac_time;
11168 	int ret;
11169 
11170 	lockdep_assert_wiphy(hw->wiphy);
11171 
11172 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11173 	if (!link_conf) {
11174 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
11175 			    ahvif->vif->addr, arvif->link_id);
11176 		return -ENOLINK;
11177 	}
11178 
11179 	reinit_completion(&ar->vdev_setup_done);
11180 
11181 	arg.vdev_id = arvif->vdev_id;
11182 	arg.dtim_period = arvif->dtim_period;
11183 	arg.bcn_intval = arvif->beacon_interval;
11184 	arg.punct_bitmap = ~arvif->punct_bitmap;
11185 
11186 	arg.freq = chandef->chan->center_freq;
11187 	arg.band_center_freq1 = chandef->center_freq1;
11188 	arg.band_center_freq2 = chandef->center_freq2;
11189 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
11190 
11191 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
11192 							chandef->chan->band,
11193 							ahvif->vif->type);
11194 	arg.min_power = 0;
11195 	arg.max_power = chandef->chan->max_power;
11196 	arg.max_reg_power = chandef->chan->max_reg_power;
11197 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
11198 
11199 	arg.pref_tx_streams = ar->num_tx_chains;
11200 	arg.pref_rx_streams = ar->num_rx_chains;
11201 
11202 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
11203 	arg.mbssid_tx_vdev_id = 0;
11204 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
11205 		     ar->ab->wmi_ab.svc_map)) {
11206 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
11207 							  &arg.mbssid_flags,
11208 							  &arg.mbssid_tx_vdev_id);
11209 		if (ret)
11210 			return ret;
11211 	}
11212 
11213 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11214 		arg.ssid = ahvif->u.ap.ssid;
11215 		arg.ssid_len = ahvif->u.ap.ssid_len;
11216 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
11217 
11218 		/* For now allow DFS for AP mode */
11219 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
11220 
11221 		arg.freq2_radar = ctx->radar_enabled;
11222 
11223 		arg.passive = arg.chan_radar;
11224 
11225 		spin_lock_bh(&ab->base_lock);
11226 		arg.regdomain = ar->ab->dfs_region;
11227 		spin_unlock_bh(&ab->base_lock);
11228 
11229 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
11230 	}
11231 
11232 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
11233 
11234 	if (!restart)
11235 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
11236 
11237 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11238 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
11239 		   arg.vdev_id, arg.freq,
11240 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
11241 
11242 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
11243 	if (ret) {
11244 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
11245 			    restart ? "restart" : "start", arg.vdev_id);
11246 		return ret;
11247 	}
11248 
11249 	ret = ath12k_mac_vdev_setup_sync(ar);
11250 	if (ret) {
11251 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
11252 			    arg.vdev_id, restart ? "restart" : "start", ret);
11253 		return ret;
11254 	}
11255 
11256 	/* TODO: For now we only set TPC power here. However when
11257 	 * channel changes, say CSA, it should be updated again.
11258 	 */
11259 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
11260 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
11261 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
11262 						   &arvif->reg_tpc_info);
11263 	}
11264 
11265 	ar->num_started_vdevs++;
11266 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
11267 		   ahvif->vif->addr, arvif->vdev_id);
11268 
11269 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
11270 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
11271 	 * done before channel usage. This flag is used to drop rx packets.
11272 	 * during CAC.
11273 	 */
11274 	/* TODO: Set the flag for other interface types as required */
11275 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
11276 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
11277 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
11278 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
11279 
11280 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11281 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
11282 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
11283 	}
11284 
11285 	ret = ath12k_mac_set_txbf_conf(arvif);
11286 	if (ret)
11287 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
11288 			    arvif->vdev_id, ret);
11289 
11290 	return 0;
11291 }
11292 
ath12k_mac_vdev_start(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)11293 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
11294 				 struct ieee80211_chanctx_conf *ctx)
11295 {
11296 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
11297 }
11298 
ath12k_mac_vdev_restart(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)11299 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
11300 				   struct ieee80211_chanctx_conf *ctx)
11301 {
11302 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
11303 }
11304 
11305 struct ath12k_mac_change_chanctx_arg {
11306 	struct ieee80211_chanctx_conf *ctx;
11307 	struct ieee80211_vif_chanctx_switch *vifs;
11308 	int n_vifs;
11309 	int next_vif;
11310 	struct ath12k *ar;
11311 };
11312 
11313 static void
ath12k_mac_change_chanctx_cnt_iter(void * data,u8 * mac,struct ieee80211_vif * vif)11314 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
11315 				   struct ieee80211_vif *vif)
11316 {
11317 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11318 	struct ath12k_mac_change_chanctx_arg *arg = data;
11319 	struct ieee80211_bss_conf *link_conf;
11320 	struct ath12k_link_vif *arvif;
11321 	unsigned long links_map;
11322 	u8 link_id;
11323 
11324 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11325 
11326 	links_map = ahvif->links_map;
11327 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11328 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11329 		if (WARN_ON(!arvif))
11330 			continue;
11331 
11332 		if (!arvif->is_created || arvif->ar != arg->ar)
11333 			continue;
11334 
11335 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11336 					      vif->link_conf[link_id]);
11337 		if (WARN_ON(!link_conf))
11338 			continue;
11339 
11340 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
11341 			continue;
11342 
11343 		arg->n_vifs++;
11344 	}
11345 }
11346 
11347 static void
ath12k_mac_change_chanctx_fill_iter(void * data,u8 * mac,struct ieee80211_vif * vif)11348 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
11349 				    struct ieee80211_vif *vif)
11350 {
11351 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11352 	struct ath12k_mac_change_chanctx_arg *arg = data;
11353 	struct ieee80211_bss_conf *link_conf;
11354 	struct ieee80211_chanctx_conf *ctx;
11355 	struct ath12k_link_vif *arvif;
11356 	unsigned long links_map;
11357 	u8 link_id;
11358 
11359 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
11360 
11361 	links_map = ahvif->links_map;
11362 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
11363 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
11364 		if (WARN_ON(!arvif))
11365 			continue;
11366 
11367 		if (!arvif->is_created || arvif->ar != arg->ar)
11368 			continue;
11369 
11370 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
11371 					      vif->link_conf[arvif->link_id]);
11372 		if (WARN_ON(!link_conf))
11373 			continue;
11374 
11375 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
11376 		if (ctx != arg->ctx)
11377 			continue;
11378 
11379 		if (WARN_ON(arg->next_vif == arg->n_vifs))
11380 			return;
11381 
11382 		arg->vifs[arg->next_vif].vif = vif;
11383 		arg->vifs[arg->next_vif].old_ctx = ctx;
11384 		arg->vifs[arg->next_vif].new_ctx = ctx;
11385 		arg->vifs[arg->next_vif].link_conf = link_conf;
11386 		arg->next_vif++;
11387 	}
11388 }
11389 
ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)11390 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
11391 {
11392 	switch (width) {
11393 	case NL80211_CHAN_WIDTH_20:
11394 		return WMI_CHAN_WIDTH_20;
11395 	case NL80211_CHAN_WIDTH_40:
11396 		return WMI_CHAN_WIDTH_40;
11397 	case NL80211_CHAN_WIDTH_80:
11398 		return WMI_CHAN_WIDTH_80;
11399 	case NL80211_CHAN_WIDTH_160:
11400 		return WMI_CHAN_WIDTH_160;
11401 	case NL80211_CHAN_WIDTH_80P80:
11402 		return WMI_CHAN_WIDTH_80P80;
11403 	case NL80211_CHAN_WIDTH_5:
11404 		return WMI_CHAN_WIDTH_5;
11405 	case NL80211_CHAN_WIDTH_10:
11406 		return WMI_CHAN_WIDTH_10;
11407 	case NL80211_CHAN_WIDTH_320:
11408 		return WMI_CHAN_WIDTH_320;
11409 	default:
11410 		WARN_ON(1);
11411 		return WMI_CHAN_WIDTH_20;
11412 	}
11413 }
11414 
ath12k_mac_update_peer_puncturing_width(struct ath12k * ar,struct ath12k_link_vif * arvif,struct cfg80211_chan_def def)11415 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
11416 						   struct ath12k_link_vif *arvif,
11417 						   struct cfg80211_chan_def def)
11418 {
11419 	u32 param_id, param_value;
11420 	int ret;
11421 
11422 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
11423 		return 0;
11424 
11425 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
11426 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
11427 		u32_encode_bits((~def.punctured),
11428 				WMI_PEER_PUNCTURE_BITMAP);
11429 
11430 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11431 		   "punctured bitmap %02x width %d vdev %d\n",
11432 		   def.punctured, def.width, arvif->vdev_id);
11433 
11434 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
11435 					arvif->vdev_id, param_id,
11436 					param_value);
11437 
11438 	return ret;
11439 }
11440 
11441 static void
ath12k_mac_update_vif_chan(struct ath12k * ar,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs)11442 ath12k_mac_update_vif_chan(struct ath12k *ar,
11443 			   struct ieee80211_vif_chanctx_switch *vifs,
11444 			   int n_vifs)
11445 {
11446 	struct ath12k_wmi_vdev_up_params params = {};
11447 	struct ieee80211_bss_conf *link_conf;
11448 	struct ath12k_base *ab = ar->ab;
11449 	struct ath12k_link_vif *arvif;
11450 	struct ieee80211_vif *vif;
11451 	struct ath12k_vif *ahvif;
11452 	u8 link_id;
11453 	int ret;
11454 	int i;
11455 	bool monitor_vif = false;
11456 
11457 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11458 
11459 	for (i = 0; i < n_vifs; i++) {
11460 		vif = vifs[i].vif;
11461 		ahvif = ath12k_vif_to_ahvif(vif);
11462 		link_conf = vifs[i].link_conf;
11463 		link_id = link_conf->link_id;
11464 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
11465 					  ahvif->link[link_id]);
11466 
11467 		if (vif->type == NL80211_IFTYPE_MONITOR) {
11468 			monitor_vif = true;
11469 			continue;
11470 		}
11471 
11472 		ath12k_dbg(ab, ATH12K_DBG_MAC,
11473 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
11474 			   arvif->vdev_id,
11475 			   vifs[i].old_ctx->def.chan->center_freq,
11476 			   vifs[i].new_ctx->def.chan->center_freq,
11477 			   vifs[i].old_ctx->def.width,
11478 			   vifs[i].new_ctx->def.width);
11479 
11480 		if (WARN_ON(!arvif->is_started))
11481 			continue;
11482 
11483 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
11484 
11485 		/* Firmware expect vdev_restart only if vdev is up.
11486 		 * If vdev is down then it expect vdev_stop->vdev_start.
11487 		 */
11488 		if (arvif->is_up) {
11489 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
11490 			if (ret) {
11491 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
11492 					    arvif->vdev_id, ret);
11493 				continue;
11494 			}
11495 		} else {
11496 			ret = ath12k_mac_vdev_stop(arvif);
11497 			if (ret) {
11498 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
11499 					    arvif->vdev_id, ret);
11500 				continue;
11501 			}
11502 
11503 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
11504 			if (ret)
11505 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
11506 					    arvif->vdev_id, ret);
11507 			continue;
11508 		}
11509 
11510 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
11511 							      vifs[i].new_ctx->def);
11512 		if (ret) {
11513 			ath12k_warn(ar->ab,
11514 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
11515 				    vifs[i].new_ctx->def.punctured,
11516 				    vifs[i].new_ctx->def.width, ret);
11517 			continue;
11518 		}
11519 
11520 		/* Defer VDEV bring-up during CSA to avoid installing stale
11521 		 * beacon templates. The beacon content is updated only
11522 		 * after CSA finalize, so we mark CSA in progress and skip
11523 		 * VDEV_UP for now. It will be handled later in
11524 		 * bss_info_changed().
11525 		 */
11526 		if (link_conf->csa_active &&
11527 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
11528 			arvif->is_csa_in_progress = true;
11529 			continue;
11530 		}
11531 
11532 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
11533 		if (ret)
11534 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
11535 				    ret);
11536 
11537 		memset(&params, 0, sizeof(params));
11538 		params.vdev_id = arvif->vdev_id;
11539 		params.aid = ahvif->aid;
11540 		params.bssid = arvif->bssid;
11541 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
11542 		if (params.tx_bssid) {
11543 			params.nontx_profile_idx = link_conf->bssid_index;
11544 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
11545 		}
11546 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
11547 		if (ret) {
11548 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
11549 				    arvif->vdev_id, ret);
11550 			continue;
11551 		}
11552 	}
11553 
11554 	/* Restart the internal monitor vdev on new channel */
11555 	if (!monitor_vif && ar->monitor_vdev_created) {
11556 		if (!ath12k_mac_monitor_stop(ar))
11557 			ath12k_mac_monitor_start(ar);
11558 	}
11559 }
11560 
11561 static void
ath12k_mac_update_active_vif_chan(struct ath12k * ar,struct ieee80211_chanctx_conf * ctx)11562 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
11563 				  struct ieee80211_chanctx_conf *ctx)
11564 {
11565 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
11566 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
11567 
11568 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11569 
11570 	ieee80211_iterate_active_interfaces_atomic(hw,
11571 						   IEEE80211_IFACE_ITER_NORMAL,
11572 						   ath12k_mac_change_chanctx_cnt_iter,
11573 						   &arg);
11574 	if (arg.n_vifs == 0)
11575 		return;
11576 
11577 	arg.vifs = kzalloc_objs(arg.vifs[0], arg.n_vifs);
11578 	if (!arg.vifs)
11579 		return;
11580 
11581 	ieee80211_iterate_active_interfaces_atomic(hw,
11582 						   IEEE80211_IFACE_ITER_NORMAL,
11583 						   ath12k_mac_change_chanctx_fill_iter,
11584 						   &arg);
11585 
11586 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11587 
11588 	kfree(arg.vifs);
11589 }
11590 
ath12k_mac_op_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)11591 void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11592 				  struct ieee80211_chanctx_conf *ctx,
11593 				  u32 changed)
11594 {
11595 	struct ath12k *ar;
11596 	struct ath12k_base *ab;
11597 
11598 	lockdep_assert_wiphy(hw->wiphy);
11599 
11600 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11601 	if (!ar)
11602 		return;
11603 
11604 	ab = ar->ab;
11605 
11606 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11607 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11608 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11609 
11610 	/* This shouldn't really happen because channel switching should use
11611 	 * switch_vif_chanctx().
11612 	 */
11613 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11614 		return;
11615 
11616 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11617 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11618 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11619 		ath12k_mac_update_active_vif_chan(ar, ctx);
11620 
11621 	/* TODO: Recalc radar detection */
11622 }
11623 EXPORT_SYMBOL(ath12k_mac_op_change_chanctx);
11624 
ath12k_start_vdev_delay(struct ath12k * ar,struct ath12k_link_vif * arvif)11625 static int ath12k_start_vdev_delay(struct ath12k *ar,
11626 				   struct ath12k_link_vif *arvif)
11627 {
11628 	struct ath12k_base *ab = ar->ab;
11629 	struct ath12k_vif *ahvif = arvif->ahvif;
11630 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11631 	struct ieee80211_chanctx_conf *chanctx;
11632 	struct ieee80211_bss_conf *link_conf;
11633 	int ret;
11634 
11635 	if (WARN_ON(arvif->is_started))
11636 		return -EBUSY;
11637 
11638 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11639 	if (!link_conf) {
11640 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11641 		return -EINVAL;
11642 	}
11643 
11644 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11645 				    link_conf->chanctx_conf);
11646 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11647 	if (ret) {
11648 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11649 			    arvif->vdev_id, vif->addr,
11650 			    chanctx->def.chan->center_freq, ret);
11651 		return ret;
11652 	}
11653 
11654 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11655 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11656 		if (ret) {
11657 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11658 			return ret;
11659 		}
11660 	}
11661 
11662 	arvif->is_started = true;
11663 
11664 	/* TODO: Setup ps and cts/rts protection */
11665 	return 0;
11666 }
11667 
ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def * chan_def)11668 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11669 {
11670 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11671 		switch (chan_def->width) {
11672 		case NL80211_CHAN_WIDTH_20:
11673 			return 1;
11674 		case NL80211_CHAN_WIDTH_40:
11675 			return 2;
11676 		case NL80211_CHAN_WIDTH_80:
11677 			return 4;
11678 		case NL80211_CHAN_WIDTH_160:
11679 			return 8;
11680 		case NL80211_CHAN_WIDTH_320:
11681 			return 16;
11682 		default:
11683 			return 1;
11684 		}
11685 	} else {
11686 		switch (chan_def->width) {
11687 		case NL80211_CHAN_WIDTH_20:
11688 			return 1;
11689 		case NL80211_CHAN_WIDTH_40:
11690 			return 2;
11691 		case NL80211_CHAN_WIDTH_80:
11692 			return 3;
11693 		case NL80211_CHAN_WIDTH_160:
11694 			return 4;
11695 		case NL80211_CHAN_WIDTH_320:
11696 			return 5;
11697 		default:
11698 			return 1;
11699 		}
11700 	}
11701 }
11702 
ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def * chan_def)11703 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11704 {
11705 	u16 diff_seq;
11706 
11707 	/* It is to get the lowest channel number's center frequency of the chan.
11708 	 * For example,
11709 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11710 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11711 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11712 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11713 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11714 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11715 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11716 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11717 	 */
11718 	switch (chan_def->width) {
11719 	case NL80211_CHAN_WIDTH_320:
11720 		diff_seq = 150;
11721 		break;
11722 	case NL80211_CHAN_WIDTH_160:
11723 		diff_seq = 70;
11724 		break;
11725 	case NL80211_CHAN_WIDTH_80:
11726 		diff_seq = 30;
11727 		break;
11728 	case NL80211_CHAN_WIDTH_40:
11729 		diff_seq = 10;
11730 		break;
11731 	default:
11732 		diff_seq = 0;
11733 	}
11734 
11735 	return chan_def->center_freq1 - diff_seq;
11736 }
11737 
ath12k_mac_get_seg_freq(struct cfg80211_chan_def * chan_def,u16 start_seq,u8 seq)11738 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11739 				   u16 start_seq, u8 seq)
11740 {
11741 	u16 seg_seq;
11742 
11743 	/* It is to get the center frequency of the specific bandwidth.
11744 	 * start_seq means the lowest channel number's center frequency.
11745 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11746 	 * For example,
11747 	 * lowest channel is 1, its center frequency 5955,
11748 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11749 	 * lowest channel is 1, its center frequency 5955,
11750 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11751 	 * lowest channel is 1, its center frequency 5955,
11752 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11753 	 * lowest channel is 1, its center frequency 5955,
11754 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11755 	 */
11756 	seg_seq = 10 * (BIT(seq) - 1);
11757 	return seg_seq + start_seq;
11758 }
11759 
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)11760 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11761 				       u16 step_freq,
11762 				       u16 *start_freq,
11763 				       u16 *center_freq,
11764 				       u8 i,
11765 				       struct ieee80211_channel **temp_chan,
11766 				       s8 *tx_power)
11767 {
11768 	/* It is to get the center frequency for each 20 MHz.
11769 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11770 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11771 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11772 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11773 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11774 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11775 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11776 	 * after get the center frequency of each channel, it is easy to find the
11777 	 * struct ieee80211_channel of it and get the max_reg_power.
11778 	 */
11779 	*center_freq = *start_freq + i * step_freq;
11780 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11781 	*tx_power = (*temp_chan)->max_reg_power;
11782 }
11783 
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)11784 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11785 				      u16 *start_freq,
11786 				      u16 *center_freq,
11787 				      u8 i,
11788 				      struct ieee80211_channel **temp_chan,
11789 				      struct cfg80211_chan_def *def,
11790 				      s8 *tx_power)
11791 {
11792 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11793 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11794 	 * it is the center frequency of a channel number.
11795 	 * For example, when configured channel number is 1.
11796 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11797 	 * then it is channel number 5.
11798 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11799 	 * then it is channel number 9.
11800 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11801 	 * then it is channel number 17.
11802 	 * after get the center frequency of each channel, it is easy to find the
11803 	 * struct ieee80211_channel of it and get the max_reg_power.
11804 	 */
11805 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11806 
11807 	/* For the 20 MHz, its center frequency is same with same channel */
11808 	if (i != 0)
11809 		*center_freq += 10;
11810 
11811 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11812 	*tx_power = (*temp_chan)->max_reg_power;
11813 }
11814 
ath12k_mac_fill_reg_tpc_info(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)11815 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11816 				  struct ath12k_link_vif *arvif,
11817 				  struct ieee80211_chanctx_conf *ctx)
11818 {
11819 	struct ath12k_base *ab = ar->ab;
11820 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11821 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11822 	struct ieee80211_channel *chan, *temp_chan;
11823 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11824 	bool is_psd_power = false, is_tpe_present = false;
11825 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS], psd_power, tx_power;
11826 	s8 eirp_power = 0;
11827 	struct ath12k_vif *ahvif = arvif->ahvif;
11828 	u16 start_freq, center_freq;
11829 	u8 reg_6ghz_power_mode;
11830 
11831 	chan = ctx->def.chan;
11832 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
11833 	pwr_reduction = bss_conf->pwr_reduction;
11834 
11835 	if (arvif->reg_tpc_info.num_pwr_levels) {
11836 		is_tpe_present = true;
11837 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
11838 	} else {
11839 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
11840 	}
11841 
11842 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
11843 		/* STA received TPE IE*/
11844 		if (is_tpe_present) {
11845 			/* local power is PSD power*/
11846 			if (chan->flags & IEEE80211_CHAN_PSD) {
11847 				/* Connecting AP is psd power */
11848 				if (reg_tpc_info->is_psd_power) {
11849 					is_psd_power = true;
11850 					ath12k_mac_get_psd_channel(ar, 20,
11851 								   &start_freq,
11852 								   &center_freq,
11853 								   pwr_lvl_idx,
11854 								   &temp_chan,
11855 								   &tx_power);
11856 					psd_power = temp_chan->psd;
11857 					eirp_power = tx_power;
11858 					max_tx_power[pwr_lvl_idx] =
11859 						min_t(s8,
11860 						      psd_power,
11861 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11862 				/* Connecting AP is not psd power */
11863 				} else {
11864 					ath12k_mac_get_eirp_power(ar,
11865 								  &start_freq,
11866 								  &center_freq,
11867 								  pwr_lvl_idx,
11868 								  &temp_chan,
11869 								  &ctx->def,
11870 								  &tx_power);
11871 					psd_power = temp_chan->psd;
11872 					/* convert psd power to EIRP power based
11873 					 * on channel width
11874 					 */
11875 					tx_power =
11876 						min_t(s8, tx_power,
11877 						      psd_power + 13 + pwr_lvl_idx * 3);
11878 					max_tx_power[pwr_lvl_idx] =
11879 						min_t(s8,
11880 						      tx_power,
11881 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11882 				}
11883 			/* local power is not PSD power */
11884 			} else {
11885 				/* Connecting AP is psd power */
11886 				if (reg_tpc_info->is_psd_power) {
11887 					is_psd_power = true;
11888 					ath12k_mac_get_psd_channel(ar, 20,
11889 								   &start_freq,
11890 								   &center_freq,
11891 								   pwr_lvl_idx,
11892 								   &temp_chan,
11893 								   &tx_power);
11894 					eirp_power = tx_power;
11895 					max_tx_power[pwr_lvl_idx] =
11896 						reg_tpc_info->tpe[pwr_lvl_idx];
11897 				/* Connecting AP is not psd power */
11898 				} else {
11899 					ath12k_mac_get_eirp_power(ar,
11900 								  &start_freq,
11901 								  &center_freq,
11902 								  pwr_lvl_idx,
11903 								  &temp_chan,
11904 								  &ctx->def,
11905 								  &tx_power);
11906 					max_tx_power[pwr_lvl_idx] =
11907 						min_t(s8,
11908 						      tx_power,
11909 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11910 				}
11911 			}
11912 		/* STA not received TPE IE */
11913 		} else {
11914 			/* local power is PSD power*/
11915 			if (chan->flags & IEEE80211_CHAN_PSD) {
11916 				is_psd_power = true;
11917 				ath12k_mac_get_psd_channel(ar, 20,
11918 							   &start_freq,
11919 							   &center_freq,
11920 							   pwr_lvl_idx,
11921 							   &temp_chan,
11922 							   &tx_power);
11923 				psd_power = temp_chan->psd;
11924 				eirp_power = tx_power;
11925 				max_tx_power[pwr_lvl_idx] = psd_power;
11926 			} else {
11927 				ath12k_mac_get_eirp_power(ar,
11928 							  &start_freq,
11929 							  &center_freq,
11930 							  pwr_lvl_idx,
11931 							  &temp_chan,
11932 							  &ctx->def,
11933 							  &tx_power);
11934 				max_tx_power[pwr_lvl_idx] = tx_power;
11935 			}
11936 		}
11937 
11938 		if (is_psd_power) {
11939 			/* If AP local power constraint is present */
11940 			if (pwr_reduction)
11941 				eirp_power = eirp_power - pwr_reduction;
11942 
11943 			/* If firmware updated max tx power is non zero, then take
11944 			 * the min of firmware updated ap tx power
11945 			 * and max power derived from above mentioned parameters.
11946 			 */
11947 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11948 				   "eirp power : %d firmware report power : %d\n",
11949 				   eirp_power, ar->max_allowed_tx_power);
11950 			/* Firmware reports lower max_allowed_tx_power during vdev
11951 			 * start response. In case of 6 GHz, firmware is not aware
11952 			 * of EIRP power unless driver sets EIRP power through WMI
11953 			 * TPC command. So radio which does not support idle power
11954 			 * save can set maximum calculated EIRP power directly to
11955 			 * firmware through TPC command without min comparison with
11956 			 * vdev start response's max_allowed_tx_power.
11957 			 */
11958 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11959 				eirp_power = min_t(s8,
11960 						   eirp_power,
11961 						   ar->max_allowed_tx_power);
11962 		} else {
11963 			/* If AP local power constraint is present */
11964 			if (pwr_reduction)
11965 				max_tx_power[pwr_lvl_idx] =
11966 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
11967 			/* If firmware updated max tx power is non zero, then take
11968 			 * the min of firmware updated ap tx power
11969 			 * and max power derived from above mentioned parameters.
11970 			 */
11971 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11972 				max_tx_power[pwr_lvl_idx] =
11973 					min_t(s8,
11974 					      max_tx_power[pwr_lvl_idx],
11975 					      ar->max_allowed_tx_power);
11976 		}
11977 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
11978 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
11979 			max_tx_power[pwr_lvl_idx];
11980 	}
11981 
11982 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
11983 	reg_tpc_info->is_psd_power = is_psd_power;
11984 	reg_tpc_info->eirp_power = eirp_power;
11985 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11986 		reg_6ghz_power_mode = bss_conf->power_type;
11987 	else
11988 		/* For now, LPI is the only supported AP power mode */
11989 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
11990 
11991 	reg_tpc_info->ap_power_type =
11992 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
11993 }
11994 
ath12k_mac_parse_tx_pwr_env(struct ath12k * ar,struct ath12k_link_vif * arvif)11995 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
11996 					struct ath12k_link_vif *arvif)
11997 {
11998 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11999 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
12000 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
12001 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
12002 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
12003 	enum wmi_reg_6g_client_type client_type;
12004 	struct ath12k_reg_info *reg_info;
12005 	struct ath12k_base *ab = ar->ab;
12006 	bool psd_valid, non_psd_valid;
12007 	int i;
12008 
12009 	reg_info = ab->reg_info[ar->pdev_idx];
12010 	client_type = reg_info->client_type;
12011 
12012 	local_psd = &tpe->psd_local[client_type];
12013 	reg_psd = &tpe->psd_reg_client[client_type];
12014 	local_non_psd = &tpe->max_local[client_type];
12015 	reg_non_psd = &tpe->max_reg_client[client_type];
12016 
12017 	psd_valid = local_psd->valid | reg_psd->valid;
12018 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
12019 
12020 	if (!psd_valid && !non_psd_valid) {
12021 		ath12k_warn(ab,
12022 			    "no transmit power envelope match client power type %d\n",
12023 			    client_type);
12024 		return;
12025 	}
12026 
12027 	if (psd_valid) {
12028 		tpc_info->is_psd_power = true;
12029 
12030 		tpc_info->num_pwr_levels = max(local_psd->count,
12031 					       reg_psd->count);
12032 		tpc_info->num_pwr_levels =
12033 				min3(tpc_info->num_pwr_levels,
12034 				     IEEE80211_TPE_PSD_ENTRIES_320MHZ,
12035 				     ATH12K_NUM_PWR_LEVELS);
12036 
12037 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12038 			tpc_info->tpe[i] = min(local_psd->power[i],
12039 					       reg_psd->power[i]) / 2;
12040 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12041 				   "TPE PSD power[%d] : %d\n",
12042 				   i, tpc_info->tpe[i]);
12043 		}
12044 	} else {
12045 		tpc_info->is_psd_power = false;
12046 		tpc_info->eirp_power = 0;
12047 
12048 		tpc_info->num_pwr_levels = max(local_non_psd->count,
12049 					       reg_non_psd->count);
12050 		tpc_info->num_pwr_levels =
12051 				min3(tpc_info->num_pwr_levels,
12052 				     IEEE80211_TPE_EIRP_ENTRIES_320MHZ,
12053 				     ATH12K_NUM_PWR_LEVELS);
12054 
12055 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
12056 			tpc_info->tpe[i] = min(local_non_psd->power[i],
12057 					       reg_non_psd->power[i]) / 2;
12058 			ath12k_dbg(ab, ATH12K_DBG_MAC,
12059 				   "non PSD power[%d] : %d\n",
12060 				   i, tpc_info->tpe[i]);
12061 		}
12062 	}
12063 }
12064 
12065 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)12066 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
12067 				 struct ieee80211_vif *vif,
12068 				 struct ieee80211_bss_conf *link_conf,
12069 				 struct ieee80211_chanctx_conf *ctx)
12070 {
12071 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12072 	struct ath12k *ar;
12073 	struct ath12k_base *ab;
12074 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12075 	u8 link_id = link_conf->link_id;
12076 	struct ath12k_link_vif *arvif;
12077 	int ret;
12078 
12079 	lockdep_assert_wiphy(hw->wiphy);
12080 
12081 	/* For multi radio wiphy, the vdev was not created during add_interface
12082 	 * create now since we have a channel ctx now to assign to a specific ar/fw
12083 	 */
12084 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12085 	if (!arvif) {
12086 		WARN_ON(1);
12087 		return -ENOMEM;
12088 	}
12089 
12090 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
12091 	if (!ar) {
12092 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
12093 			       vif->addr, link_id);
12094 		return -EINVAL;
12095 	}
12096 
12097 	ab = ar->ab;
12098 
12099 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12100 		   "mac chanctx assign ptr %p vdev_id %i\n",
12101 		   ctx, arvif->vdev_id);
12102 
12103 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
12104 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
12105 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
12106 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
12107 
12108 	arvif->punct_bitmap = ctx->def.punctured;
12109 
12110 	/* for some targets bss peer must be created before vdev_start */
12111 	if (ab->hw_params->vdev_start_delay &&
12112 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
12113 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
12114 	    !ath12k_dp_link_peer_exist_by_vdev_id(ath12k_ab_to_dp(ab), arvif->vdev_id)) {
12115 		ret = 0;
12116 		goto out;
12117 	}
12118 
12119 	if (WARN_ON(arvif->is_started)) {
12120 		ret = -EBUSY;
12121 		goto out;
12122 	}
12123 
12124 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12125 		ret = ath12k_mac_monitor_start(ar);
12126 		if (ret) {
12127 			ath12k_mac_monitor_vdev_delete(ar);
12128 			goto out;
12129 		}
12130 
12131 		arvif->is_started = true;
12132 		goto out;
12133 	}
12134 
12135 	ret = ath12k_mac_vdev_start(arvif, ctx);
12136 	if (ret) {
12137 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
12138 			    arvif->vdev_id, vif->addr,
12139 			    ctx->def.chan->center_freq, ret);
12140 		goto out;
12141 	}
12142 
12143 	arvif->is_started = true;
12144 
12145 	/* TODO: Setup ps and cts/rts protection */
12146 
12147 out:
12148 	return ret;
12149 }
12150 EXPORT_SYMBOL(ath12k_mac_op_assign_vif_chanctx);
12151 
12152 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)12153 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
12154 				   struct ieee80211_vif *vif,
12155 				   struct ieee80211_bss_conf *link_conf,
12156 				   struct ieee80211_chanctx_conf *ctx)
12157 {
12158 	struct ath12k *ar;
12159 	struct ath12k_base *ab;
12160 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12161 	struct ath12k_link_vif *arvif;
12162 	u8 link_id = link_conf->link_id;
12163 	int ret;
12164 
12165 	lockdep_assert_wiphy(hw->wiphy);
12166 
12167 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12168 
12169 	/* The vif is expected to be attached to an ar's VDEV.
12170 	 * We leave the vif/vdev in this function as is
12171 	 * and not delete the vdev symmetric to assign_vif_chanctx()
12172 	 * the VDEV will be deleted and unassigned either during
12173 	 * remove_interface() or when there is a change in channel
12174 	 * that moves the vif to a new ar
12175 	 */
12176 	if (!arvif || !arvif->is_created)
12177 		return;
12178 
12179 	ar = arvif->ar;
12180 	ab = ar->ab;
12181 
12182 	ath12k_dbg(ab, ATH12K_DBG_MAC,
12183 		   "mac chanctx unassign ptr %p vdev_id %i\n",
12184 		   ctx, arvif->vdev_id);
12185 
12186 	WARN_ON(!arvif->is_started);
12187 
12188 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
12189 		ret = ath12k_mac_monitor_stop(ar);
12190 		if (ret)
12191 			return;
12192 
12193 		arvif->is_started = false;
12194 	}
12195 
12196 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
12197 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
12198 		ath12k_bss_disassoc(ar, arvif);
12199 		ret = ath12k_mac_vdev_stop(arvif);
12200 		if (ret)
12201 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
12202 				    arvif->vdev_id, ret);
12203 	}
12204 	arvif->is_started = false;
12205 
12206 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
12207 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12208 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
12209 	    ar->state_11d != ATH12K_11D_PREPARING) {
12210 		reinit_completion(&ar->completed_11d_scan);
12211 		ar->state_11d = ATH12K_11D_PREPARING;
12212 	}
12213 
12214 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
12215 		ath12k_scan_abort(ar);
12216 		ar->scan.arvif = NULL;
12217 	}
12218 }
12219 EXPORT_SYMBOL(ath12k_mac_op_unassign_vif_chanctx);
12220 
12221 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)12222 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
12223 				 struct ieee80211_vif_chanctx_switch *vifs,
12224 				 int n_vifs,
12225 				 enum ieee80211_chanctx_switch_mode mode)
12226 {
12227 	struct ath12k *ar;
12228 
12229 	lockdep_assert_wiphy(hw->wiphy);
12230 
12231 	ar = ath12k_get_ar_by_ctx(hw, vifs->old_ctx);
12232 	if (!ar)
12233 		return -EINVAL;
12234 
12235 	/* Switching channels across radio is not allowed */
12236 	if (ar != ath12k_get_ar_by_ctx(hw, vifs->new_ctx))
12237 		return -EINVAL;
12238 
12239 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12240 		   "mac chanctx switch n_vifs %d mode %d\n",
12241 		   n_vifs, mode);
12242 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
12243 
12244 	return 0;
12245 }
12246 EXPORT_SYMBOL(ath12k_mac_op_switch_vif_chanctx);
12247 
12248 static int
ath12k_set_vdev_param_to_all_vifs(struct ath12k * ar,int param,u32 value)12249 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
12250 {
12251 	struct ath12k_link_vif *arvif;
12252 	int ret = 0;
12253 
12254 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12255 
12256 	list_for_each_entry(arvif, &ar->arvifs, list) {
12257 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
12258 			   param, arvif->vdev_id, value);
12259 
12260 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12261 						    param, value);
12262 		if (ret) {
12263 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
12264 				    param, arvif->vdev_id, ret);
12265 			break;
12266 		}
12267 	}
12268 
12269 	return ret;
12270 }
12271 
12272 /* mac80211 stores device specific RTS/Fragmentation threshold value,
12273  * this is set interface specific to firmware from ath12k driver
12274  */
ath12k_mac_op_set_rts_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)12275 int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
12276 				    int radio_idx, u32 value)
12277 {
12278 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12279 	struct wiphy *wiphy = hw->wiphy;
12280 	struct ath12k *ar;
12281 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
12282 	int ret = 0, ret_err, i;
12283 
12284 	lockdep_assert_wiphy(hw->wiphy);
12285 
12286 	if (radio_idx >= wiphy->n_radio || radio_idx < -1)
12287 		return -EINVAL;
12288 
12289 	if (radio_idx != -1) {
12290 		/* Update RTS threshold in specified radio */
12291 		ar = ath12k_ah_to_ar(ah, radio_idx);
12292 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12293 		if (ret) {
12294 			ath12k_warn(ar->ab,
12295 				    "failed to set RTS config for all vdevs of pdev %d",
12296 				    ar->pdev->pdev_id);
12297 			return ret;
12298 		}
12299 
12300 		ar->rts_threshold = value;
12301 		return 0;
12302 	}
12303 
12304 	/* Radio_index passed is -1, so set RTS threshold for all radios. */
12305 	for_each_ar(ah, ar, i) {
12306 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
12307 		if (ret) {
12308 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
12309 				    ar->pdev->pdev_id);
12310 			break;
12311 		}
12312 	}
12313 	if (!ret) {
12314 		/* Setting new RTS threshold for vdevs of all radios passed, so update
12315 		 * the RTS threshold value for all radios
12316 		 */
12317 		for_each_ar(ah, ar, i)
12318 			ar->rts_threshold = value;
12319 		return 0;
12320 	}
12321 
12322 	/* RTS threshold config failed, revert to the previous RTS threshold */
12323 	for (i = i - 1; i >= 0; i--) {
12324 		ar = ath12k_ah_to_ar(ah, i);
12325 		ret_err = ath12k_set_vdev_param_to_all_vifs(ar, param_id,
12326 							    ar->rts_threshold);
12327 		if (ret_err)
12328 			ath12k_warn(ar->ab,
12329 				    "failed to restore RTS threshold for all vdevs of pdev %d",
12330 				    ar->pdev->pdev_id);
12331 	}
12332 
12333 	return ret;
12334 }
12335 EXPORT_SYMBOL(ath12k_mac_op_set_rts_threshold);
12336 
ath12k_mac_op_set_frag_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)12337 int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
12338 				     int radio_idx, u32 value)
12339 {
12340 	/* Even though there's a WMI vdev param for fragmentation threshold no
12341 	 * known firmware actually implements it. Moreover it is not possible to
12342 	 * rely frame fragmentation to mac80211 because firmware clears the
12343 	 * "more fragments" bit in frame control making it impossible for remote
12344 	 * devices to reassemble frames.
12345 	 *
12346 	 * Hence implement a dummy callback just to say fragmentation isn't
12347 	 * supported. This effectively prevents mac80211 from doing frame
12348 	 * fragmentation in software.
12349 	 */
12350 
12351 	lockdep_assert_wiphy(hw->wiphy);
12352 
12353 	return -EOPNOTSUPP;
12354 }
12355 EXPORT_SYMBOL(ath12k_mac_op_set_frag_threshold);
12356 
ath12k_mac_flush(struct ath12k * ar)12357 static int ath12k_mac_flush(struct ath12k *ar)
12358 {
12359 	long time_left;
12360 	int ret = 0;
12361 
12362 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
12363 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
12364 				       ATH12K_FLUSH_TIMEOUT);
12365 	if (time_left == 0) {
12366 		ath12k_warn(ar->ab,
12367 			    "failed to flush transmit queue, data pkts pending %d\n",
12368 			    atomic_read(&ar->dp.num_tx_pending));
12369 		ret = -ETIMEDOUT;
12370 	}
12371 
12372 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
12373 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
12374 				       ATH12K_FLUSH_TIMEOUT);
12375 	if (time_left == 0) {
12376 		ath12k_warn(ar->ab,
12377 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
12378 			    atomic_read(&ar->num_pending_mgmt_tx));
12379 		ret = -ETIMEDOUT;
12380 	}
12381 
12382 	return ret;
12383 }
12384 
ath12k_mac_wait_tx_complete(struct ath12k * ar)12385 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
12386 {
12387 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12388 
12389 	ath12k_mac_drain_tx(ar);
12390 	return ath12k_mac_flush(ar);
12391 }
12392 
ath12k_mac_op_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)12393 void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
12394 			 u32 queues, bool drop)
12395 {
12396 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12397 	struct ath12k_link_vif *arvif;
12398 	struct ath12k_vif *ahvif;
12399 	unsigned long links;
12400 	struct ath12k *ar;
12401 	u8 link_id;
12402 	int i;
12403 
12404 	lockdep_assert_wiphy(hw->wiphy);
12405 
12406 	if (drop)
12407 		return;
12408 
12409 	for_each_ar(ah, ar, i)
12410 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
12411 
12412 	/* vif can be NULL when flush() is considered for hw */
12413 	if (!vif) {
12414 		for_each_ar(ah, ar, i)
12415 			ath12k_mac_flush(ar);
12416 		return;
12417 	}
12418 
12419 	ahvif = ath12k_vif_to_ahvif(vif);
12420 	links = ahvif->links_map;
12421 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
12422 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
12423 		if (!(arvif && arvif->ar))
12424 			continue;
12425 
12426 		ath12k_mac_flush(arvif->ar);
12427 	}
12428 }
12429 EXPORT_SYMBOL(ath12k_mac_op_flush);
12430 
12431 static int
ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12432 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
12433 				     enum nl80211_band band,
12434 				     const struct cfg80211_bitrate_mask *mask)
12435 {
12436 	int num_rates = 0;
12437 	int i;
12438 
12439 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
12440 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
12441 
12442 	return num_rates;
12443 }
12444 
12445 static bool
ath12k_mac_has_single_legacy_rate(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12446 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
12447 				  enum nl80211_band band,
12448 				  const struct cfg80211_bitrate_mask *mask)
12449 {
12450 	int num_rates = 0;
12451 
12452 	num_rates = hweight32(mask->control[band].legacy);
12453 
12454 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
12455 		return false;
12456 
12457 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
12458 		return false;
12459 
12460 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
12461 		return false;
12462 
12463 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask))
12464 		return false;
12465 
12466 	return num_rates == 1;
12467 }
12468 
12469 static __le16
ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap * he_cap)12470 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
12471 {
12472 	if (he_cap->he_cap_elem.phy_cap_info[0] &
12473 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
12474 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
12475 
12476 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
12477 }
12478 
12479 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)12480 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
12481 				       struct ieee80211_vif *vif,
12482 				       enum nl80211_band band,
12483 				       const struct cfg80211_bitrate_mask *mask,
12484 				       int *nss)
12485 {
12486 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
12487 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
12488 	const struct ieee80211_sband_iftype_data *data;
12489 	const struct ieee80211_sta_he_cap *he_cap;
12490 	u16 he_mcs_map = 0;
12491 	u16 eht_mcs_map = 0;
12492 	u8 ht_nss_mask = 0;
12493 	u8 vht_nss_mask = 0;
12494 	u8 he_nss_mask = 0;
12495 	u8 eht_nss_mask = 0;
12496 	u8 mcs_nss_len;
12497 	int i;
12498 
12499 	/* No need to consider legacy here. Basic rates are always present
12500 	 * in bitrate mask
12501 	 */
12502 
12503 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
12504 		if (mask->control[band].ht_mcs[i] == 0)
12505 			continue;
12506 		else if (mask->control[band].ht_mcs[i] ==
12507 			 sband->ht_cap.mcs.rx_mask[i])
12508 			ht_nss_mask |= BIT(i);
12509 		else
12510 			return false;
12511 	}
12512 
12513 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
12514 		if (mask->control[band].vht_mcs[i] == 0)
12515 			continue;
12516 		else if (mask->control[band].vht_mcs[i] ==
12517 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
12518 			vht_nss_mask |= BIT(i);
12519 		else
12520 			return false;
12521 	}
12522 
12523 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
12524 	if (!he_cap)
12525 		return false;
12526 
12527 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
12528 
12529 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
12530 		if (mask->control[band].he_mcs[i] == 0)
12531 			continue;
12532 
12533 		if (mask->control[band].he_mcs[i] ==
12534 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
12535 			he_nss_mask |= BIT(i);
12536 		else
12537 			return false;
12538 	}
12539 
12540 	data = ieee80211_get_sband_iftype_data(sband, vif->type);
12541 
12542 	mcs_nss_len = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem,
12543 						 &data->eht_cap.eht_cap_elem,
12544 						 false);
12545 	if (mcs_nss_len == 4) {
12546 		/* 20 MHz only STA case */
12547 		const struct ieee80211_eht_mcs_nss_supp_20mhz_only *eht_mcs_nss =
12548 			&data->eht_cap.eht_mcs_nss_supp.only_20mhz;
12549 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12550 			eht_mcs_map = 0x1fff;
12551 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12552 			eht_mcs_map = 0x07ff;
12553 		else if (eht_mcs_nss->rx_tx_mcs9_max_nss)
12554 			eht_mcs_map = 0x01ff;
12555 		else
12556 			eht_mcs_map = 0x007f;
12557 	} else {
12558 		const struct ieee80211_eht_mcs_nss_supp_bw *eht_mcs_nss;
12559 
12560 		switch (mcs_nss_len) {
12561 		case 9:
12562 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._320;
12563 			break;
12564 		case 6:
12565 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._160;
12566 			break;
12567 		case 3:
12568 			eht_mcs_nss = &data->eht_cap.eht_mcs_nss_supp.bw._80;
12569 			break;
12570 		default:
12571 			return false;
12572 		}
12573 
12574 		if (eht_mcs_nss->rx_tx_mcs13_max_nss)
12575 			eht_mcs_map = 0x1fff;
12576 		else if (eht_mcs_nss->rx_tx_mcs11_max_nss)
12577 			eht_mcs_map = 0x7ff;
12578 		else
12579 			eht_mcs_map = 0x1ff;
12580 	}
12581 
12582 	for (i = 0; i < ARRAY_SIZE(mask->control[band].eht_mcs); i++) {
12583 		if (mask->control[band].eht_mcs[i] == 0)
12584 			continue;
12585 
12586 		if (mask->control[band].eht_mcs[i] < eht_mcs_map)
12587 			eht_nss_mask |= BIT(i);
12588 		else
12589 			return false;
12590 	}
12591 
12592 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask ||
12593 	    ht_nss_mask != eht_nss_mask)
12594 		return false;
12595 
12596 	if (ht_nss_mask == 0)
12597 		return false;
12598 
12599 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
12600 		return false;
12601 
12602 	*nss = fls(ht_nss_mask);
12603 
12604 	return true;
12605 }
12606 
12607 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)12608 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
12609 				  enum nl80211_band band,
12610 				  const struct cfg80211_bitrate_mask *mask,
12611 				  u32 *rate, u8 *nss)
12612 {
12613 	int rate_idx;
12614 	u16 bitrate;
12615 	u8 preamble;
12616 	u8 hw_rate;
12617 
12618 	if (hweight32(mask->control[band].legacy) != 1)
12619 		return -EINVAL;
12620 
12621 	rate_idx = ffs(mask->control[band].legacy) - 1;
12622 
12623 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
12624 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
12625 
12626 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
12627 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
12628 
12629 	if (ath12k_mac_bitrate_is_cck(bitrate))
12630 		preamble = WMI_RATE_PREAMBLE_CCK;
12631 	else
12632 		preamble = WMI_RATE_PREAMBLE_OFDM;
12633 
12634 	*nss = 1;
12635 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
12636 
12637 	return 0;
12638 }
12639 
12640 static int
ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif * arvif,u8 gi,u8 ltf,u32 param)12641 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 gi, u8 ltf,
12642 				 u32 param)
12643 {
12644 	struct ath12k *ar = arvif->ar;
12645 	int ret;
12646 
12647 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12648 
12649 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
12650 	if (gi && gi != 0xFF)
12651 		gi += 1;
12652 
12653 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12654 					    WMI_VDEV_PARAM_SGI, gi);
12655 	if (ret) {
12656 		ath12k_warn(ar->ab, "failed to set GI:%d, error:%d\n",
12657 			    gi, ret);
12658 		return ret;
12659 	}
12660 
12661 	if (param == WMI_VDEV_PARAM_HE_LTF) {
12662 		/* HE values start from 1 */
12663 		if (ltf != 0xFF)
12664 			ltf += 1;
12665 	} else {
12666 		/* EHT values start from 5 */
12667 		if (ltf != 0xFF)
12668 			ltf += 4;
12669 	}
12670 
12671 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12672 					    param, ltf);
12673 	if (ret) {
12674 		ath12k_warn(ar->ab, "failed to set LTF:%d, error:%d\n",
12675 			    ltf, ret);
12676 		return ret;
12677 	}
12678 	return 0;
12679 }
12680 
12681 static int
ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif * arvif,u16 gi,u8 ltf)12682 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 gi, u8 ltf)
12683 {
12684 	struct ath12k *ar = arvif->ar;
12685 	int ret;
12686 	u32 ar_gi_ltf;
12687 
12688 	if (gi != 0xFF) {
12689 		switch (gi) {
12690 		case ATH12K_RATE_INFO_GI_0_8:
12691 			gi = WMI_AUTORATE_800NS_GI;
12692 			break;
12693 		case ATH12K_RATE_INFO_GI_1_6:
12694 			gi = WMI_AUTORATE_1600NS_GI;
12695 			break;
12696 		case ATH12K_RATE_INFO_GI_3_2:
12697 			gi = WMI_AUTORATE_3200NS_GI;
12698 			break;
12699 		default:
12700 			ath12k_warn(ar->ab, "Invalid GI\n");
12701 			return -EINVAL;
12702 		}
12703 	}
12704 
12705 	if (ltf != 0xFF) {
12706 		switch (ltf) {
12707 		case ATH12K_RATE_INFO_1XLTF:
12708 			ltf = WMI_AUTORATE_LTF_1X;
12709 			break;
12710 		case ATH12K_RATE_INFO_2XLTF:
12711 			ltf = WMI_AUTORATE_LTF_2X;
12712 			break;
12713 		case ATH12K_RATE_INFO_4XLTF:
12714 			ltf = WMI_AUTORATE_LTF_4X;
12715 			break;
12716 		default:
12717 			ath12k_warn(ar->ab, "Invalid LTF\n");
12718 			return -EINVAL;
12719 		}
12720 	}
12721 
12722 	ar_gi_ltf = gi | ltf;
12723 
12724 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12725 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12726 					    ar_gi_ltf);
12727 	if (ret) {
12728 		ath12k_warn(ar->ab,
12729 			    "failed to set autorate GI:%u, LTF:%u params, error:%d\n",
12730 			    gi, ltf, ret);
12731 		return ret;
12732 	}
12733 
12734 	return 0;
12735 }
12736 
ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)12737 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12738 {
12739 	switch (gi) {
12740 	case NL80211_TXRATE_DEFAULT_GI:
12741 		return WMI_GI_400_NS;
12742 	case NL80211_TXRATE_FORCE_LGI:
12743 		return WMI_GI_800_NS;
12744 	default:
12745 		return WMI_GI_400_NS;
12746 	}
12747 }
12748 
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)12749 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12750 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12751 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate,
12752 				      u8 eht_gi, u8 eht_ltf,
12753 				      bool eht_fixed_rate)
12754 {
12755 	struct ieee80211_bss_conf *link_conf;
12756 	struct ath12k *ar = arvif->ar;
12757 	bool he_support, eht_support, gi_ltf_set = false;
12758 	u32 vdev_param;
12759 	u32 param_value;
12760 	int ret;
12761 
12762 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12763 
12764 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12765 	if (!link_conf)
12766 		return -EINVAL;
12767 
12768 	he_support = link_conf->he_support;
12769 	eht_support = link_conf->eht_support;
12770 
12771 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12772 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
12773 		   arvif->vdev_id, rate, nss, sgi, ldpc);
12774 
12775 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12776 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
12777 		   he_ltf, he_fixed_rate);
12778 
12779 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12780 		   "eht_gi 0x%02x eht_ltf 0x%02x eht_fixed_rate %d\n",
12781 		   eht_gi, eht_ltf, eht_fixed_rate);
12782 
12783 	if (!he_support && !eht_support) {
12784 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
12785 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12786 						    vdev_param, rate);
12787 		if (ret) {
12788 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
12789 				    rate, ret);
12790 			return ret;
12791 		}
12792 	}
12793 
12794 	vdev_param = WMI_VDEV_PARAM_NSS;
12795 
12796 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12797 					    vdev_param, nss);
12798 	if (ret) {
12799 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
12800 			    nss, ret);
12801 		return ret;
12802 	}
12803 
12804 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12805 					    WMI_VDEV_PARAM_LDPC, ldpc);
12806 	if (ret) {
12807 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
12808 			    ldpc, ret);
12809 		return ret;
12810 	}
12811 
12812 	if (eht_support) {
12813 		if (eht_fixed_rate)
12814 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, eht_gi, eht_ltf,
12815 							       WMI_VDEV_PARAM_EHT_LTF);
12816 		else
12817 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, eht_gi, eht_ltf);
12818 
12819 		if (ret) {
12820 			ath12k_warn(ar->ab,
12821 				    "failed to set EHT LTF/GI params %d/%d: %d\n",
12822 				    eht_gi, eht_ltf, ret);
12823 			return ret;
12824 		}
12825 		gi_ltf_set = true;
12826 	}
12827 
12828 	if (he_support) {
12829 		if (he_fixed_rate)
12830 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf,
12831 							       WMI_VDEV_PARAM_HE_LTF);
12832 		else
12833 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
12834 		if (ret)
12835 			return ret;
12836 		gi_ltf_set = true;
12837 	}
12838 
12839 	if (!gi_ltf_set) {
12840 		vdev_param = WMI_VDEV_PARAM_SGI;
12841 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
12842 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12843 						    vdev_param, param_value);
12844 		if (ret) {
12845 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
12846 				    sgi, ret);
12847 			return ret;
12848 		}
12849 	}
12850 
12851 	return 0;
12852 }
12853 
12854 static bool
ath12k_mac_vht_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12855 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
12856 				 enum nl80211_band band,
12857 				 const struct cfg80211_bitrate_mask *mask)
12858 {
12859 	int i;
12860 	u16 vht_mcs;
12861 
12862 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
12863 		vht_mcs = mask->control[band].vht_mcs[i];
12864 
12865 		switch (vht_mcs) {
12866 		case 0:
12867 		case BIT(8) - 1:
12868 		case BIT(9) - 1:
12869 		case BIT(10) - 1:
12870 			break;
12871 		default:
12872 			return false;
12873 		}
12874 	}
12875 
12876 	return true;
12877 }
12878 
12879 static bool
ath12k_mac_he_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12880 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
12881 				enum nl80211_band band,
12882 				const struct cfg80211_bitrate_mask *mask)
12883 {
12884 	int i;
12885 	u16 he_mcs;
12886 
12887 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
12888 		he_mcs = mask->control[band].he_mcs[i];
12889 
12890 		switch (he_mcs) {
12891 		case 0:
12892 		case BIT(8) - 1:
12893 		case BIT(10) - 1:
12894 		case BIT(12) - 1:
12895 			break;
12896 		default:
12897 			return false;
12898 		}
12899 	}
12900 
12901 	return true;
12902 }
12903 
12904 static bool
ath12k_mac_eht_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12905 ath12k_mac_eht_mcs_range_present(struct ath12k *ar,
12906 				 enum nl80211_band band,
12907 				 const struct cfg80211_bitrate_mask *mask)
12908 {
12909 	u16 eht_mcs;
12910 	int i;
12911 
12912 	for (i = 0; i < NL80211_EHT_NSS_MAX; i++) {
12913 		eht_mcs = mask->control[band].eht_mcs[i];
12914 
12915 		switch (eht_mcs) {
12916 		case 0:
12917 		case BIT(8) - 1:
12918 		case BIT(10) - 1:
12919 		case BIT(12) - 1:
12920 		case BIT(14) - 1:
12921 			break;
12922 		case BIT(15) - 1:
12923 		case BIT(16) - 1:
12924 		case BIT(16) - BIT(14) - 1:
12925 			if (i != 0)
12926 				return false;
12927 			break;
12928 		default:
12929 			return false;
12930 		}
12931 	}
12932 
12933 	return true;
12934 }
12935 
ath12k_mac_set_bitrate_mask_iter(void * data,struct ieee80211_sta * sta)12936 static void ath12k_mac_set_bitrate_mask_iter(void *data,
12937 					     struct ieee80211_sta *sta)
12938 {
12939 	struct ath12k_link_vif *arvif = data;
12940 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12941 	struct ath12k_link_sta *arsta;
12942 	struct ath12k *ar = arvif->ar;
12943 
12944 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12945 
12946 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12947 				  ahsta->link[arvif->link_id]);
12948 	if (!arsta || arsta->arvif != arvif)
12949 		return;
12950 
12951 	spin_lock_bh(&ar->data_lock);
12952 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
12953 	spin_unlock_bh(&ar->data_lock);
12954 
12955 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
12956 }
12957 
ath12k_mac_disable_peer_fixed_rate(void * data,struct ieee80211_sta * sta)12958 static void ath12k_mac_disable_peer_fixed_rate(void *data,
12959 					       struct ieee80211_sta *sta)
12960 {
12961 	struct ath12k_link_vif *arvif = data;
12962 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12963 	struct ath12k_link_sta *arsta;
12964 	struct ath12k *ar = arvif->ar;
12965 	int ret;
12966 
12967 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12968 
12969 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12970 				  ahsta->link[arvif->link_id]);
12971 
12972 	if (!arsta || arsta->arvif != arvif)
12973 		return;
12974 
12975 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
12976 					arvif->vdev_id,
12977 					WMI_PEER_PARAM_FIXED_RATE,
12978 					WMI_FIXED_RATE_NONE);
12979 	if (ret)
12980 		ath12k_warn(ar->ab,
12981 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
12982 			    arsta->addr, ret);
12983 }
12984 
12985 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)12986 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
12987 					const struct cfg80211_bitrate_mask *mask,
12988 					unsigned int link_id)
12989 {
12990 	bool eht_fixed_rate = false, he_fixed_rate = false, vht_fixed_rate = false;
12991 	const u16 *vht_mcs_mask, *he_mcs_mask, *eht_mcs_mask;
12992 	struct ieee80211_link_sta *link_sta;
12993 	struct ath12k_dp_link_peer *peer, *tmp;
12994 	u8 vht_nss, he_nss, eht_nss;
12995 	int ret = true;
12996 	struct ath12k_base *ab = ar->ab;
12997 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
12998 
12999 	vht_mcs_mask = mask->control[band].vht_mcs;
13000 	he_mcs_mask = mask->control[band].he_mcs;
13001 	eht_mcs_mask = mask->control[band].eht_mcs;
13002 
13003 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
13004 		vht_fixed_rate = true;
13005 
13006 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
13007 		he_fixed_rate = true;
13008 
13009 	if (ath12k_mac_bitrate_mask_num_eht_rates(ar, band, mask) == 1)
13010 		eht_fixed_rate = true;
13011 
13012 	if (!vht_fixed_rate && !he_fixed_rate && !eht_fixed_rate)
13013 		return true;
13014 
13015 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
13016 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
13017 	eht_nss = ath12k_mac_max_eht_nss(eht_mcs_mask);
13018 
13019 	rcu_read_lock();
13020 	spin_lock_bh(&dp->dp_lock);
13021 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
13022 		if (peer->sta) {
13023 			link_sta = rcu_dereference(peer->sta->link[link_id]);
13024 			if (!link_sta) {
13025 				ret = false;
13026 				goto exit;
13027 			}
13028 
13029 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
13030 					       link_sta->rx_nss < vht_nss)) {
13031 				ret = false;
13032 				goto exit;
13033 			}
13034 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
13035 					      link_sta->rx_nss < he_nss)) {
13036 				ret = false;
13037 				goto exit;
13038 			}
13039 			if (eht_fixed_rate && (!link_sta->eht_cap.has_eht ||
13040 					       link_sta->rx_nss < eht_nss)) {
13041 				ret = false;
13042 				goto exit;
13043 			}
13044 		}
13045 	}
13046 exit:
13047 	spin_unlock_bh(&dp->dp_lock);
13048 	rcu_read_unlock();
13049 	return ret;
13050 }
13051 
13052 int
ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)13053 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
13054 			       struct ieee80211_vif *vif,
13055 			       const struct cfg80211_bitrate_mask *mask)
13056 {
13057 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13058 	struct ath12k_link_vif *arvif;
13059 	struct cfg80211_chan_def def;
13060 	struct ath12k *ar;
13061 	enum nl80211_band band;
13062 	const u8 *ht_mcs_mask;
13063 	const u16 *vht_mcs_mask;
13064 	const u16 *he_mcs_mask;
13065 	const u16 *eht_mcs_mask;
13066 	u8 he_ltf = 0;
13067 	u8 he_gi = 0;
13068 	u8 eht_ltf = 0, eht_gi = 0;
13069 	u32 rate;
13070 	u8 nss, mac_nss;
13071 	u8 sgi;
13072 	u8 ldpc;
13073 	int single_nss;
13074 	int ret;
13075 	int num_rates;
13076 	bool he_fixed_rate = false;
13077 	bool eht_fixed_rate = false;
13078 
13079 	lockdep_assert_wiphy(hw->wiphy);
13080 
13081 	arvif = &ahvif->deflink;
13082 
13083 	ar = arvif->ar;
13084 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
13085 		ret = -EPERM;
13086 		goto out;
13087 	}
13088 
13089 	band = def.chan->band;
13090 	ht_mcs_mask = mask->control[band].ht_mcs;
13091 	vht_mcs_mask = mask->control[band].vht_mcs;
13092 	he_mcs_mask = mask->control[band].he_mcs;
13093 	eht_mcs_mask = mask->control[band].eht_mcs;
13094 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
13095 
13096 	sgi = mask->control[band].gi;
13097 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
13098 		ret = -EINVAL;
13099 		goto out;
13100 	}
13101 
13102 	he_gi = mask->control[band].he_gi;
13103 	he_ltf = mask->control[band].he_ltf;
13104 
13105 	eht_gi = mask->control[band].eht_gi;
13106 	eht_ltf = mask->control[band].eht_ltf;
13107 
13108 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
13109 	 * requires passing at least one of used basic rates along with them.
13110 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
13111 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
13112 	 * suitable for setting single HT/VHT rates.
13113 	 * But, there could be a single basic rate passed from userspace which
13114 	 * can be done through the FIXED_RATE param.
13115 	 */
13116 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
13117 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
13118 							&nss);
13119 		if (ret) {
13120 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
13121 				    arvif->vdev_id, ret);
13122 			goto out;
13123 		}
13124 
13125 		ieee80211_iterate_stations_mtx(hw,
13126 					       ath12k_mac_disable_peer_fixed_rate,
13127 					       arvif);
13128 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
13129 							  &single_nss)) {
13130 		rate = WMI_FIXED_RATE_NONE;
13131 		nss = single_nss;
13132 		arvif->bitrate_mask = *mask;
13133 
13134 		ieee80211_iterate_stations_atomic(hw,
13135 						  ath12k_mac_set_bitrate_mask_iter,
13136 						  arvif);
13137 	} else {
13138 		rate = WMI_FIXED_RATE_NONE;
13139 
13140 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
13141 							     mask, arvif->link_id))
13142 			ath12k_warn(ar->ab,
13143 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
13144 
13145 		mac_nss = max(max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
13146 				   ath12k_mac_max_vht_nss(vht_mcs_mask),
13147 				   ath12k_mac_max_he_nss(he_mcs_mask)),
13148 			       ath12k_mac_max_eht_nss(eht_mcs_mask));
13149 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
13150 
13151 		/* If multiple rates across different preambles are given
13152 		 * we can reconfigure this info with all peers using PEER_ASSOC
13153 		 * command with the below exception cases.
13154 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
13155 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
13156 		 * mandates passing basic rates along with HT/VHT rates, FW
13157 		 * doesn't allow switching from VHT to Legacy. Hence instead of
13158 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
13159 		 * we could set this VHT rate as peer fixed rate param, which
13160 		 * will override FIXED rate and FW rate control algorithm.
13161 		 * If single VHT rate is passed along with HT rates, we select
13162 		 * the VHT rate as fixed rate for vht peers.
13163 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
13164 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
13165 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
13166 		 * RATEMASK_CMDID can cover all use cases of setting rates
13167 		 * across multiple preambles and rates within same type.
13168 		 * But requires more validation of the command at this point.
13169 		 */
13170 
13171 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
13172 								  mask);
13173 
13174 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
13175 		    num_rates > 1) {
13176 			/* TODO: Handle multiple VHT MCS values setting using
13177 			 * RATEMASK CMD
13178 			 */
13179 			ath12k_warn(ar->ab,
13180 				    "Setting more than one MCS Value in bitrate mask not supported\n");
13181 			ret = -EINVAL;
13182 			goto out;
13183 		}
13184 
13185 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
13186 		if (num_rates == 1)
13187 			he_fixed_rate = true;
13188 
13189 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
13190 		    num_rates > 1) {
13191 			ath12k_warn(ar->ab,
13192 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
13193 			ret = -EINVAL;
13194 			goto out;
13195 		}
13196 
13197 		num_rates = ath12k_mac_bitrate_mask_num_eht_rates(ar, band,
13198 								  mask);
13199 		if (num_rates == 1)
13200 			eht_fixed_rate = true;
13201 
13202 		if (!ath12k_mac_eht_mcs_range_present(ar, band, mask) &&
13203 		    num_rates > 1) {
13204 			ath12k_warn(ar->ab,
13205 				    "Setting more than one EHT MCS Value in bitrate mask not supported\n");
13206 			ret = -EINVAL;
13207 			goto out;
13208 		}
13209 
13210 		ieee80211_iterate_stations_mtx(hw,
13211 					       ath12k_mac_disable_peer_fixed_rate,
13212 					       arvif);
13213 
13214 		arvif->bitrate_mask = *mask;
13215 		ieee80211_iterate_stations_mtx(hw,
13216 					       ath12k_mac_set_bitrate_mask_iter,
13217 					       arvif);
13218 	}
13219 
13220 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
13221 					 he_ltf, he_fixed_rate, eht_gi, eht_ltf,
13222 					 eht_fixed_rate);
13223 	if (ret) {
13224 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
13225 			    arvif->vdev_id, ret);
13226 	}
13227 
13228 out:
13229 	return ret;
13230 }
13231 EXPORT_SYMBOL(ath12k_mac_op_set_bitrate_mask);
13232 
13233 void
ath12k_mac_op_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)13234 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
13235 				enum ieee80211_reconfig_type reconfig_type)
13236 {
13237 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13238 	struct ath12k *ar;
13239 	struct ath12k_base *ab;
13240 	struct ath12k_vif *ahvif;
13241 	struct ath12k_link_vif *arvif;
13242 	int recovery_count, i;
13243 
13244 	lockdep_assert_wiphy(hw->wiphy);
13245 
13246 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
13247 		return;
13248 
13249 	guard(mutex)(&ah->hw_mutex);
13250 
13251 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
13252 		return;
13253 
13254 	ah->state = ATH12K_HW_STATE_ON;
13255 	ieee80211_wake_queues(hw);
13256 
13257 	for_each_ar(ah, ar, i) {
13258 		ab = ar->ab;
13259 
13260 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
13261 			    ar->pdev->pdev_id);
13262 
13263 		if (ar->ab->hw_params->current_cc_support &&
13264 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
13265 			struct wmi_set_current_country_arg arg = {};
13266 
13267 			memcpy(&arg.alpha2, ar->alpha2, 2);
13268 			reinit_completion(&ar->regd_update_completed);
13269 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
13270 		}
13271 
13272 		if (ab->is_reset) {
13273 			recovery_count = atomic_inc_return(&ab->recovery_count);
13274 
13275 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
13276 				   recovery_count);
13277 
13278 			/* When there are multiple radios in an SOC,
13279 			 * the recovery has to be done for each radio
13280 			 */
13281 			if (recovery_count == ab->num_radios) {
13282 				atomic_dec(&ab->reset_count);
13283 				complete(&ab->reset_complete);
13284 				ab->is_reset = false;
13285 				atomic_set(&ab->fail_cont_count, 0);
13286 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
13287 			}
13288 		}
13289 
13290 		list_for_each_entry(arvif, &ar->arvifs, list) {
13291 			ahvif = arvif->ahvif;
13292 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
13293 				   "reconfig cipher %d up %d vdev type %d\n",
13294 				   ahvif->dp_vif.key_cipher,
13295 				   arvif->is_up,
13296 				   ahvif->vdev_type);
13297 
13298 			/* After trigger disconnect, then upper layer will
13299 			 * trigger connect again, then the PN number of
13300 			 * upper layer will be reset to keep up with AP
13301 			 * side, hence PN number mismatch will not happen.
13302 			 */
13303 			if (arvif->is_up &&
13304 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13305 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
13306 				ieee80211_hw_restart_disconnect(ahvif->vif);
13307 
13308 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
13309 					   "restart disconnect\n");
13310 			}
13311 		}
13312 	}
13313 }
13314 EXPORT_SYMBOL(ath12k_mac_op_reconfig_complete);
13315 
13316 static void
ath12k_mac_update_bss_chan_survey(struct ath12k * ar,struct ieee80211_channel * channel)13317 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
13318 				  struct ieee80211_channel *channel)
13319 {
13320 	int ret;
13321 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
13322 
13323 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
13324 
13325 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
13326 	    ar->rx_channel != channel)
13327 		return;
13328 
13329 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
13330 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13331 			   "ignoring bss chan info req while scanning..\n");
13332 		return;
13333 	}
13334 
13335 	reinit_completion(&ar->bss_survey_done);
13336 
13337 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
13338 	if (ret) {
13339 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
13340 		return;
13341 	}
13342 
13343 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
13344 	if (ret == 0)
13345 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
13346 }
13347 
ath12k_mac_op_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)13348 int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
13349 			     struct survey_info *survey)
13350 {
13351 	struct ath12k *ar;
13352 	struct ieee80211_supported_band *sband;
13353 	struct survey_info *ar_survey;
13354 
13355 	lockdep_assert_wiphy(hw->wiphy);
13356 
13357 	if (idx >= ATH12K_NUM_CHANS)
13358 		return -ENOENT;
13359 
13360 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
13361 	if (sband && idx >= sband->n_channels) {
13362 		idx -= sband->n_channels;
13363 		sband = NULL;
13364 	}
13365 
13366 	if (!sband)
13367 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
13368 	if (sband && idx >= sband->n_channels) {
13369 		idx -= sband->n_channels;
13370 		sband = NULL;
13371 	}
13372 
13373 	if (!sband)
13374 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
13375 
13376 	if (!sband || idx >= sband->n_channels)
13377 		return -ENOENT;
13378 
13379 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
13380 	if (!ar) {
13381 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
13382 			memset(survey, 0, sizeof(*survey));
13383 			return 0;
13384 		}
13385 		return -ENOENT;
13386 	}
13387 
13388 	ar_survey = &ar->survey[idx];
13389 
13390 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
13391 
13392 	spin_lock_bh(&ar->data_lock);
13393 	memcpy(survey, ar_survey, sizeof(*survey));
13394 	spin_unlock_bh(&ar->data_lock);
13395 
13396 	survey->channel = &sband->channels[idx];
13397 
13398 	if (ar->rx_channel == survey->channel)
13399 		survey->filled |= SURVEY_INFO_IN_USE;
13400 
13401 	return 0;
13402 }
13403 EXPORT_SYMBOL(ath12k_mac_op_get_survey);
13404 
ath12k_mac_put_chain_rssi(struct station_info * sinfo,struct ath12k_link_sta * arsta)13405 static void ath12k_mac_put_chain_rssi(struct station_info *sinfo,
13406 				      struct ath12k_link_sta *arsta)
13407 {
13408 	s8 rssi;
13409 	int i;
13410 
13411 	for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
13412 		sinfo->chains &= ~BIT(i);
13413 		rssi = arsta->chain_signal[i];
13414 
13415 		if (rssi != ATH12K_DEFAULT_NOISE_FLOOR &&
13416 		    rssi != ATH12K_INVALID_RSSI_FULL &&
13417 		    rssi != ATH12K_INVALID_RSSI_EMPTY &&
13418 		    rssi != 0) {
13419 			sinfo->chain_signal[i] = rssi;
13420 			sinfo->chains |= BIT(i);
13421 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
13422 		}
13423 	}
13424 }
13425 
ath12k_mac_op_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)13426 void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
13427 				  struct ieee80211_vif *vif,
13428 				  struct ieee80211_sta *sta,
13429 				  struct station_info *sinfo)
13430 {
13431 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
13432 	struct ath12k_dp_link_peer_rate_info rate_info = {};
13433 	struct ath12k_fw_stats_req_params params = {};
13434 	struct ath12k_dp_link_peer *peer;
13435 	struct ath12k_link_sta *arsta;
13436 	s8 signal, noise_floor;
13437 	struct ath12k_dp *dp;
13438 	struct ath12k *ar;
13439 	bool db2dbm;
13440 
13441 	lockdep_assert_wiphy(hw->wiphy);
13442 
13443 	arsta = &ahsta->deflink;
13444 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13445 	if (!ar)
13446 		return;
13447 
13448 	dp = ath12k_ab_to_dp(ar->ab);
13449 	ath12k_dp_link_peer_get_sta_rate_info_stats(dp, arsta->addr, &rate_info);
13450 
13451 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13452 			  ar->ab->wmi_ab.svc_map);
13453 
13454 	sinfo->rx_duration = rate_info.rx_duration;
13455 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13456 
13457 	sinfo->tx_duration = rate_info.tx_duration;
13458 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13459 
13460 	if (rate_info.txrate.legacy || rate_info.txrate.nss) {
13461 		if (rate_info.txrate.legacy) {
13462 			sinfo->txrate.legacy = rate_info.txrate.legacy;
13463 		} else {
13464 			sinfo->txrate.mcs = rate_info.txrate.mcs;
13465 			sinfo->txrate.nss = rate_info.txrate.nss;
13466 			sinfo->txrate.bw = rate_info.txrate.bw;
13467 			sinfo->txrate.he_gi = rate_info.txrate.he_gi;
13468 			sinfo->txrate.he_dcm = rate_info.txrate.he_dcm;
13469 			sinfo->txrate.he_ru_alloc = rate_info.txrate.he_ru_alloc;
13470 			sinfo->txrate.eht_gi = rate_info.txrate.eht_gi;
13471 			sinfo->txrate.eht_ru_alloc = rate_info.txrate.eht_ru_alloc;
13472 		}
13473 		sinfo->txrate.flags = rate_info.txrate.flags;
13474 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13475 	}
13476 
13477 	/* TODO: Use real NF instead of default one. */
13478 	signal = rate_info.rssi_comb;
13479 
13480 	params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13481 	params.vdev_id = 0;
13482 	params.stats_id = WMI_REQUEST_VDEV_STAT;
13483 
13484 	if (!signal &&
13485 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13486 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13487 		signal = arsta->rssi_beacon;
13488 		ath12k_fw_stats_reset(ar);
13489 	}
13490 
13491 	params.stats_id = WMI_REQUEST_RSSI_PER_CHAIN_STAT;
13492 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) &&
13493 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
13494 	    !(ath12k_mac_get_fw_stats(ar, &params))) {
13495 		ath12k_mac_put_chain_rssi(sinfo, arsta);
13496 		ath12k_fw_stats_reset(ar);
13497 	}
13498 
13499 	spin_lock_bh(&ar->data_lock);
13500 	noise_floor = ath12k_pdev_get_noise_floor(ar);
13501 	spin_unlock_bh(&ar->data_lock);
13502 
13503 	if (signal) {
13504 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
13505 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13506 	}
13507 
13508 	sinfo->signal_avg = rate_info.signal_avg;
13509 
13510 	if (!db2dbm)
13511 		sinfo->signal_avg += noise_floor;
13512 
13513 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13514 
13515 	spin_lock_bh(&dp->dp_lock);
13516 	peer = ath12k_dp_link_peer_find_by_addr(dp, arsta->addr);
13517 	if (!peer) {
13518 		spin_unlock_bh(&dp->dp_lock);
13519 		return;
13520 	}
13521 
13522 	sinfo->tx_retries = peer->tx_retry_count;
13523 	sinfo->tx_failed = peer->tx_retry_failed;
13524 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13525 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13526 
13527 	spin_unlock_bh(&dp->dp_lock);
13528 }
13529 EXPORT_SYMBOL(ath12k_mac_op_sta_statistics);
13530 
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)13531 void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
13532 				       struct ieee80211_vif *vif,
13533 				       struct ieee80211_link_sta *link_sta,
13534 				       struct link_station_info *link_sinfo)
13535 {
13536 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
13537 	struct ath12k_fw_stats_req_params params = {};
13538 	struct ath12k_dp_link_peer *peer;
13539 	struct ath12k_link_sta *arsta;
13540 	struct ath12k *ar;
13541 	s8 signal;
13542 	bool db2dbm;
13543 
13544 	lockdep_assert_wiphy(hw->wiphy);
13545 
13546 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
13547 
13548 	if (!arsta)
13549 		return;
13550 
13551 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
13552 	if (!ar)
13553 		return;
13554 
13555 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
13556 			  ar->ab->wmi_ab.svc_map);
13557 
13558 	spin_lock_bh(&ar->ab->dp->dp_lock);
13559 	peer = ath12k_dp_link_peer_find_by_addr(ar->ab->dp, arsta->addr);
13560 	if (!peer) {
13561 		spin_unlock_bh(&ar->ab->dp->dp_lock);
13562 		return;
13563 	}
13564 
13565 	link_sinfo->rx_duration = peer->rx_duration;
13566 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
13567 
13568 	link_sinfo->tx_duration = peer->tx_duration;
13569 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
13570 
13571 	if (peer->txrate.legacy || peer->txrate.nss) {
13572 		if (peer->txrate.legacy) {
13573 			link_sinfo->txrate.legacy = peer->txrate.legacy;
13574 		} else {
13575 			link_sinfo->txrate.mcs = peer->txrate.mcs;
13576 			link_sinfo->txrate.nss = peer->txrate.nss;
13577 			link_sinfo->txrate.bw = peer->txrate.bw;
13578 			link_sinfo->txrate.he_gi = peer->txrate.he_gi;
13579 			link_sinfo->txrate.he_dcm = peer->txrate.he_dcm;
13580 			link_sinfo->txrate.he_ru_alloc =
13581 				peer->txrate.he_ru_alloc;
13582 			link_sinfo->txrate.eht_gi = peer->txrate.eht_gi;
13583 			link_sinfo->txrate.eht_ru_alloc =
13584 				peer->txrate.eht_ru_alloc;
13585 		}
13586 		link_sinfo->txrate.flags = peer->txrate.flags;
13587 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
13588 	}
13589 
13590 	link_sinfo->signal_avg = ewma_avg_rssi_read(&peer->avg_rssi);
13591 
13592 	if (!db2dbm)
13593 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
13594 
13595 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
13596 
13597 	link_sinfo->tx_retries = peer->tx_retry_count;
13598 	link_sinfo->tx_failed = peer->tx_retry_failed;
13599 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
13600 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
13601 
13602 	/* TODO: Use real NF instead of default one. */
13603 	signal = peer->rssi_comb;
13604 
13605 	spin_unlock_bh(&ar->ab->dp->dp_lock);
13606 
13607 	if (!signal && ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
13608 		params.pdev_id = ath12k_mac_get_target_pdev_id(ar);
13609 		params.vdev_id = 0;
13610 		params.stats_id = WMI_REQUEST_VDEV_STAT;
13611 
13612 		if (!ath12k_mac_get_fw_stats(ar, &params)) {
13613 			signal = arsta->rssi_beacon;
13614 			ath12k_fw_stats_reset(ar);
13615 		}
13616 	}
13617 
13618 	if (signal) {
13619 		link_sinfo->signal =
13620 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
13621 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
13622 	}
13623 }
13624 EXPORT_SYMBOL(ath12k_mac_op_link_sta_statistics);
13625 
ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif)13626 int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
13627 					   struct ieee80211_vif *vif)
13628 {
13629 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13630 	struct ath12k *ar;
13631 
13632 	ar = ath12k_ah_to_ar(ah, 0);
13633 
13634 	lockdep_assert_wiphy(hw->wiphy);
13635 
13636 	spin_lock_bh(&ar->data_lock);
13637 	ar->scan.roc_notify = false;
13638 	spin_unlock_bh(&ar->data_lock);
13639 
13640 	ath12k_scan_abort(ar);
13641 
13642 	cancel_delayed_work_sync(&ar->scan.timeout);
13643 	wiphy_work_flush(hw->wiphy, &ar->scan.vdev_clean_wk);
13644 
13645 	return 0;
13646 }
13647 EXPORT_SYMBOL(ath12k_mac_op_cancel_remain_on_channel);
13648 
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)13649 int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
13650 				    struct ieee80211_vif *vif,
13651 				    struct ieee80211_channel *chan,
13652 				    int duration,
13653 				    enum ieee80211_roc_type type)
13654 {
13655 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13656 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13657 	struct ath12k_link_vif *arvif;
13658 	struct ath12k *ar;
13659 	u32 scan_time_msec;
13660 	bool create = true;
13661 	u8 link_id;
13662 	int ret;
13663 
13664 	lockdep_assert_wiphy(hw->wiphy);
13665 
13666 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
13667 	if (!ar)
13668 		return -EINVAL;
13669 
13670 	/* check if any of the links of ML VIF is already started on
13671 	 * radio(ar) corresponding to given scan frequency and use it,
13672 	 * if not use deflink(link 0) for scan purpose.
13673 	 */
13674 
13675 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
13676 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13677 	/* If the vif is already assigned to a specific vdev of an ar,
13678 	 * check whether its already started, vdev which is started
13679 	 * are not allowed to switch to a new radio.
13680 	 * If the vdev is not started, but was earlier created on a
13681 	 * different ar, delete that vdev and create a new one. We don't
13682 	 * delete at the scan stop as an optimization to avoid redundant
13683 	 * delete-create vdev's for the same ar, in case the request is
13684 	 * always on the same band for the vif
13685 	 */
13686 	if (arvif->is_created) {
13687 		if (WARN_ON(!arvif->ar))
13688 			return -EINVAL;
13689 
13690 		if (ar != arvif->ar && arvif->is_started)
13691 			return -EBUSY;
13692 
13693 		if (ar != arvif->ar) {
13694 			ath12k_mac_remove_link_interface(hw, arvif);
13695 			ath12k_mac_unassign_link_vif(arvif);
13696 		} else {
13697 			create = false;
13698 		}
13699 	}
13700 
13701 	if (create) {
13702 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
13703 
13704 		ret = ath12k_mac_vdev_create(ar, arvif);
13705 		if (ret) {
13706 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
13707 				    ret);
13708 			ath12k_mac_unassign_link_vif(arvif);
13709 			return ret;
13710 		}
13711 	}
13712 
13713 	spin_lock_bh(&ar->data_lock);
13714 
13715 	switch (ar->scan.state) {
13716 	case ATH12K_SCAN_IDLE:
13717 		reinit_completion(&ar->scan.started);
13718 		reinit_completion(&ar->scan.completed);
13719 		reinit_completion(&ar->scan.on_channel);
13720 		ar->scan.state = ATH12K_SCAN_STARTING;
13721 		ar->scan.is_roc = true;
13722 		ar->scan.arvif = arvif;
13723 		ar->scan.roc_freq = chan->center_freq;
13724 		ar->scan.roc_notify = true;
13725 		ret = 0;
13726 		break;
13727 	case ATH12K_SCAN_STARTING:
13728 	case ATH12K_SCAN_RUNNING:
13729 	case ATH12K_SCAN_ABORTING:
13730 		ret = -EBUSY;
13731 		break;
13732 	}
13733 
13734 	spin_unlock_bh(&ar->data_lock);
13735 
13736 	if (ret)
13737 		return ret;
13738 
13739 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
13740 
13741 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
13742 					kzalloc_obj(*arg);
13743 	if (!arg)
13744 		return -ENOMEM;
13745 
13746 	ath12k_wmi_start_scan_init(ar, arg);
13747 	arg->num_chan = 1;
13748 
13749 	u32 *chan_list __free(kfree) = kcalloc(arg->num_chan, sizeof(*chan_list),
13750 					       GFP_KERNEL);
13751 	if (!chan_list)
13752 		return -ENOMEM;
13753 
13754 	arg->chan_list = chan_list;
13755 	arg->vdev_id = arvif->vdev_id;
13756 	arg->scan_id = ATH12K_SCAN_ID;
13757 	arg->chan_list[0] = chan->center_freq;
13758 	arg->dwell_time_active = scan_time_msec;
13759 	arg->dwell_time_passive = scan_time_msec;
13760 	arg->max_scan_time = scan_time_msec;
13761 	arg->scan_f_passive = 1;
13762 	arg->burst_duration = duration;
13763 
13764 	ret = ath12k_start_scan(ar, arg);
13765 	if (ret) {
13766 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
13767 
13768 		spin_lock_bh(&ar->data_lock);
13769 		ar->scan.state = ATH12K_SCAN_IDLE;
13770 		spin_unlock_bh(&ar->data_lock);
13771 		return ret;
13772 	}
13773 
13774 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
13775 	if (ret == 0) {
13776 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
13777 		ret = ath12k_scan_stop(ar);
13778 		if (ret)
13779 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
13780 		return -ETIMEDOUT;
13781 	}
13782 
13783 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
13784 				     msecs_to_jiffies(duration));
13785 
13786 	return 0;
13787 }
13788 EXPORT_SYMBOL(ath12k_mac_op_remain_on_channel);
13789 
ath12k_mac_op_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)13790 void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
13791 				  struct ieee80211_vif *vif,
13792 				  struct cfg80211_gtk_rekey_data *data)
13793 {
13794 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
13795 	struct ath12k_rekey_data *rekey_data;
13796 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
13797 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
13798 	struct ath12k_link_vif *arvif;
13799 
13800 	lockdep_assert_wiphy(hw->wiphy);
13801 
13802 	arvif = &ahvif->deflink;
13803 	rekey_data = &arvif->rekey_data;
13804 
13805 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
13806 		   arvif->vdev_id);
13807 
13808 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
13809 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
13810 
13811 	/* The supplicant works on big-endian, the firmware expects it on
13812 	 * little endian.
13813 	 */
13814 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
13815 
13816 	arvif->rekey_data.enable_offload = true;
13817 
13818 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
13819 			rekey_data->kck, NL80211_KCK_LEN);
13820 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
13821 			rekey_data->kck, NL80211_KEK_LEN);
13822 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
13823 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
13824 }
13825 EXPORT_SYMBOL(ath12k_mac_op_set_rekey_data);
13826 
ath12k_mac_update_freq_range(struct ath12k * ar,u32 freq_low,u32 freq_high)13827 void ath12k_mac_update_freq_range(struct ath12k *ar,
13828 				  u32 freq_low, u32 freq_high)
13829 {
13830 	if (!(freq_low && freq_high))
13831 		return;
13832 
13833 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
13834 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
13835 						MHZ_TO_KHZ(freq_low));
13836 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
13837 					      MHZ_TO_KHZ(freq_high));
13838 	} else {
13839 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
13840 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
13841 	}
13842 
13843 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13844 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
13845 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
13846 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
13847 }
13848 
ath12k_mac_update_ch_list(struct ath12k * ar,struct ieee80211_supported_band * band,u32 freq_low,u32 freq_high)13849 static void ath12k_mac_update_ch_list(struct ath12k *ar,
13850 				      struct ieee80211_supported_band *band,
13851 				      u32 freq_low, u32 freq_high)
13852 {
13853 	int i;
13854 
13855 	if (!(freq_low && freq_high))
13856 		return;
13857 
13858 	for (i = 0; i < band->n_channels; i++) {
13859 		if (band->channels[i].center_freq < freq_low ||
13860 		    band->channels[i].center_freq > freq_high)
13861 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13862 	}
13863 }
13864 
ath12k_get_phy_id(struct ath12k * ar,u32 band)13865 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
13866 {
13867 	struct ath12k_pdev *pdev = ar->pdev;
13868 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
13869 
13870 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
13871 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
13872 
13873 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
13874 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
13875 
13876 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
13877 
13878 	return 0;
13879 }
13880 
ath12k_mac_update_band(struct ath12k * ar,struct ieee80211_supported_band * orig_band,struct ieee80211_supported_band * new_band)13881 static int ath12k_mac_update_band(struct ath12k *ar,
13882 				  struct ieee80211_supported_band *orig_band,
13883 				  struct ieee80211_supported_band *new_band)
13884 {
13885 	int i;
13886 
13887 	if (!orig_band || !new_band)
13888 		return -EINVAL;
13889 
13890 	if (orig_band->band != new_band->band)
13891 		return -EINVAL;
13892 
13893 	for (i = 0; i < new_band->n_channels; i++) {
13894 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
13895 			continue;
13896 		/* An enabled channel in new_band should not be already enabled
13897 		 * in the orig_band
13898 		 */
13899 		if (WARN_ON(!(orig_band->channels[i].flags &
13900 			      IEEE80211_CHAN_DISABLED)))
13901 			return -EINVAL;
13902 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
13903 	}
13904 	return 0;
13905 }
13906 
ath12k_mac_setup_channels_rates(struct ath12k * ar,u32 supported_bands,struct ieee80211_supported_band * bands[])13907 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
13908 					   u32 supported_bands,
13909 					   struct ieee80211_supported_band *bands[])
13910 {
13911 	struct ieee80211_supported_band *band;
13912 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
13913 	struct ath12k_base *ab = ar->ab;
13914 	u32 phy_id, freq_low, freq_high;
13915 	struct ath12k_hw *ah = ar->ah;
13916 	void *channels;
13917 	int ret;
13918 
13919 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
13920 		      ARRAY_SIZE(ath12k_5ghz_channels) +
13921 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
13922 		     ATH12K_NUM_CHANS);
13923 
13924 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
13925 
13926 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
13927 		channels = kmemdup(ath12k_2ghz_channels,
13928 				   sizeof(ath12k_2ghz_channels),
13929 				   GFP_KERNEL);
13930 		if (!channels)
13931 			return -ENOMEM;
13932 
13933 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
13934 		band->band = NL80211_BAND_2GHZ;
13935 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
13936 		band->channels = channels;
13937 		band->n_bitrates = ath12k_g_rates_size;
13938 		band->bitrates = ath12k_g_rates;
13939 
13940 		if (ab->hw_params->single_pdev_only) {
13941 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
13942 			reg_cap = &ab->hal_reg_cap[phy_id];
13943 		}
13944 
13945 		freq_low = max(reg_cap->low_2ghz_chan,
13946 			       ab->reg_freq_2ghz.start_freq);
13947 		freq_high = min(reg_cap->high_2ghz_chan,
13948 				ab->reg_freq_2ghz.end_freq);
13949 
13950 		ath12k_mac_update_ch_list(ar, band,
13951 					  reg_cap->low_2ghz_chan,
13952 					  reg_cap->high_2ghz_chan);
13953 
13954 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13955 
13956 		if (!bands[NL80211_BAND_2GHZ]) {
13957 			bands[NL80211_BAND_2GHZ] = band;
13958 		} else {
13959 			/* Split mac in same band under same wiphy */
13960 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
13961 			if (ret) {
13962 				kfree(channels);
13963 				band->channels = NULL;
13964 				return ret;
13965 			}
13966 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
13967 				   ar->pdev->pdev_id,
13968 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
13969 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
13970 		}
13971 	}
13972 
13973 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
13974 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
13975 			channels = kmemdup(ath12k_6ghz_channels,
13976 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
13977 			if (!channels) {
13978 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13979 				return -ENOMEM;
13980 			}
13981 
13982 			ar->supports_6ghz = true;
13983 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
13984 			band->band = NL80211_BAND_6GHZ;
13985 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
13986 			band->channels = channels;
13987 			band->n_bitrates = ath12k_a_rates_size;
13988 			band->bitrates = ath12k_a_rates;
13989 
13990 			freq_low = max(reg_cap->low_5ghz_chan,
13991 				       ab->reg_freq_6ghz.start_freq);
13992 			freq_high = min(reg_cap->high_5ghz_chan,
13993 					ab->reg_freq_6ghz.end_freq);
13994 
13995 			ath12k_mac_update_ch_list(ar, band,
13996 						  reg_cap->low_5ghz_chan,
13997 						  reg_cap->high_5ghz_chan);
13998 
13999 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14000 			ah->use_6ghz_regd = true;
14001 
14002 			if (!bands[NL80211_BAND_6GHZ]) {
14003 				bands[NL80211_BAND_6GHZ] = band;
14004 			} else {
14005 				/* Split mac in same band under same wiphy */
14006 				ret = ath12k_mac_update_band(ar,
14007 							     bands[NL80211_BAND_6GHZ],
14008 							     band);
14009 				if (ret) {
14010 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14011 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14012 					kfree(channels);
14013 					band->channels = NULL;
14014 					return ret;
14015 				}
14016 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
14017 					   ar->pdev->pdev_id,
14018 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14019 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14020 			}
14021 		}
14022 
14023 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
14024 			channels = kmemdup(ath12k_5ghz_channels,
14025 					   sizeof(ath12k_5ghz_channels),
14026 					   GFP_KERNEL);
14027 			if (!channels) {
14028 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14029 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14030 				return -ENOMEM;
14031 			}
14032 
14033 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
14034 			band->band = NL80211_BAND_5GHZ;
14035 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
14036 			band->channels = channels;
14037 			band->n_bitrates = ath12k_a_rates_size;
14038 			band->bitrates = ath12k_a_rates;
14039 
14040 			if (ab->hw_params->single_pdev_only) {
14041 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
14042 				reg_cap = &ab->hal_reg_cap[phy_id];
14043 			}
14044 
14045 			freq_low = max(reg_cap->low_5ghz_chan,
14046 				       ab->reg_freq_5ghz.start_freq);
14047 			freq_high = min(reg_cap->high_5ghz_chan,
14048 					ab->reg_freq_5ghz.end_freq);
14049 
14050 			ath12k_mac_update_ch_list(ar, band,
14051 						  reg_cap->low_5ghz_chan,
14052 						  reg_cap->high_5ghz_chan);
14053 
14054 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
14055 
14056 			if (!bands[NL80211_BAND_5GHZ]) {
14057 				bands[NL80211_BAND_5GHZ] = band;
14058 			} else {
14059 				/* Split mac in same band under same wiphy */
14060 				ret = ath12k_mac_update_band(ar,
14061 							     bands[NL80211_BAND_5GHZ],
14062 							     band);
14063 				if (ret) {
14064 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14065 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14066 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14067 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
14068 					kfree(channels);
14069 					band->channels = NULL;
14070 					return ret;
14071 				}
14072 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
14073 					   ar->pdev->pdev_id,
14074 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
14075 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
14076 			}
14077 		}
14078 	}
14079 
14080 	return 0;
14081 }
14082 
ath12k_mac_get_ifmodes(struct ath12k_hw * ah)14083 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
14084 {
14085 	struct ath12k *ar;
14086 	int i;
14087 	u16 interface_modes = U16_MAX;
14088 
14089 	for_each_ar(ah, ar, i)
14090 		interface_modes &= ar->ab->hw_params->interface_modes;
14091 
14092 	return interface_modes == U16_MAX ? 0 : interface_modes;
14093 }
14094 
ath12k_mac_is_iface_mode_enable(struct ath12k_hw * ah,enum nl80211_iftype type)14095 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
14096 					    enum nl80211_iftype type)
14097 {
14098 	struct ath12k *ar;
14099 	int i;
14100 	u16 interface_modes, mode = 0;
14101 	bool is_enable = false;
14102 
14103 	if (type == NL80211_IFTYPE_MESH_POINT) {
14104 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
14105 			mode = BIT(type);
14106 	} else {
14107 		mode = BIT(type);
14108 	}
14109 
14110 	for_each_ar(ah, ar, i) {
14111 		interface_modes = ar->ab->hw_params->interface_modes;
14112 		if (interface_modes & mode) {
14113 			is_enable = true;
14114 			break;
14115 		}
14116 	}
14117 
14118 	return is_enable;
14119 }
14120 
14121 static int
ath12k_mac_setup_radio_iface_comb(struct ath12k * ar,struct ieee80211_iface_combination * comb)14122 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
14123 				  struct ieee80211_iface_combination *comb)
14124 {
14125 	u16 interface_modes = ar->ab->hw_params->interface_modes;
14126 	struct ieee80211_iface_limit *limits;
14127 	int n_limits, max_interfaces;
14128 	bool ap, mesh, p2p;
14129 
14130 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
14131 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
14132 
14133 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
14134 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
14135 
14136 	if ((ap || mesh) && !p2p) {
14137 		n_limits = 2;
14138 		max_interfaces = 16;
14139 	} else if (p2p) {
14140 		n_limits = 3;
14141 		if (ap || mesh)
14142 			max_interfaces = 16;
14143 		else
14144 			max_interfaces = 3;
14145 	} else {
14146 		n_limits = 1;
14147 		max_interfaces = 1;
14148 	}
14149 
14150 	limits = kzalloc_objs(*limits, n_limits);
14151 	if (!limits)
14152 		return -ENOMEM;
14153 
14154 	limits[0].max = 1;
14155 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
14156 
14157 	if (ap || mesh || p2p)
14158 		limits[1].max = max_interfaces;
14159 
14160 	if (ap)
14161 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
14162 
14163 	if (mesh)
14164 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
14165 
14166 	if (p2p) {
14167 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
14168 					BIT(NL80211_IFTYPE_P2P_GO);
14169 		limits[2].max = 1;
14170 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
14171 	}
14172 
14173 	comb[0].limits = limits;
14174 	comb[0].n_limits = n_limits;
14175 	comb[0].max_interfaces = max_interfaces;
14176 	comb[0].beacon_int_infra_match = true;
14177 	comb[0].beacon_int_min_gcd = 100;
14178 
14179 	comb[0].num_different_channels = 1;
14180 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
14181 				      BIT(NL80211_CHAN_WIDTH_20) |
14182 				      BIT(NL80211_CHAN_WIDTH_40) |
14183 				      BIT(NL80211_CHAN_WIDTH_80) |
14184 				      BIT(NL80211_CHAN_WIDTH_160);
14185 
14186 	return 0;
14187 }
14188 
14189 static int
ath12k_mac_setup_global_iface_comb(struct ath12k_hw * ah,struct wiphy_radio * radio,u8 n_radio,struct ieee80211_iface_combination * comb)14190 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
14191 				   struct wiphy_radio *radio,
14192 				   u8 n_radio,
14193 				   struct ieee80211_iface_combination *comb)
14194 {
14195 	const struct ieee80211_iface_combination *iter_comb;
14196 	struct ieee80211_iface_limit *limits;
14197 	int i, j, n_limits;
14198 	bool ap, mesh, p2p;
14199 
14200 	if (!n_radio)
14201 		return 0;
14202 
14203 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
14204 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
14205 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
14206 
14207 	if ((ap || mesh) && !p2p)
14208 		n_limits = 2;
14209 	else if (p2p)
14210 		n_limits = 3;
14211 	else
14212 		n_limits = 1;
14213 
14214 	limits = kzalloc_objs(*limits, n_limits);
14215 	if (!limits)
14216 		return -ENOMEM;
14217 
14218 	for (i = 0; i < n_radio; i++) {
14219 		iter_comb = radio[i].iface_combinations;
14220 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
14221 			limits[j].types |= iter_comb->limits[j].types;
14222 			limits[j].max += iter_comb->limits[j].max;
14223 		}
14224 
14225 		comb->max_interfaces += iter_comb->max_interfaces;
14226 		comb->num_different_channels += iter_comb->num_different_channels;
14227 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
14228 	}
14229 
14230 	comb->limits = limits;
14231 	comb->n_limits = n_limits;
14232 	comb->beacon_int_infra_match = true;
14233 	comb->beacon_int_min_gcd = 100;
14234 
14235 	return 0;
14236 }
14237 
14238 static
ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination * iface_comb)14239 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
14240 {
14241 	kfree(iface_comb[0].limits);
14242 	kfree(iface_comb);
14243 }
14244 
ath12k_mac_cleanup_iface_combinations(struct ath12k_hw * ah)14245 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
14246 {
14247 	struct wiphy *wiphy = ah->hw->wiphy;
14248 	const struct wiphy_radio *radio;
14249 	int i;
14250 
14251 	if (wiphy->n_radio > 0) {
14252 		radio = wiphy->radio;
14253 		for (i = 0; i < wiphy->n_radio; i++)
14254 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14255 
14256 		kfree(wiphy->radio);
14257 	}
14258 
14259 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
14260 }
14261 
ath12k_mac_setup_iface_combinations(struct ath12k_hw * ah)14262 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
14263 {
14264 	struct ieee80211_iface_combination *combinations, *comb;
14265 	struct wiphy *wiphy = ah->hw->wiphy;
14266 	struct wiphy_radio *radio;
14267 	int n_combinations = 1;
14268 	struct ath12k *ar;
14269 	int i, ret;
14270 
14271 	if (ah->num_radio == 1) {
14272 		ar = &ah->radio[0];
14273 
14274 		if (ar->ab->hw_params->single_pdev_only)
14275 			n_combinations = 2;
14276 
14277 		combinations = kzalloc_objs(*combinations, n_combinations);
14278 		if (!combinations)
14279 			return -ENOMEM;
14280 
14281 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
14282 		if (ret) {
14283 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
14284 				       ret);
14285 			goto err_free_combinations;
14286 		}
14287 
14288 		if (ar->ab->hw_params->single_pdev_only) {
14289 			comb = combinations + 1;
14290 			memcpy(comb, combinations, sizeof(*comb));
14291 			comb->num_different_channels = 2;
14292 			comb->radar_detect_widths = 0;
14293 		}
14294 
14295 		goto out;
14296 	}
14297 
14298 	combinations = kzalloc_objs(*combinations, n_combinations);
14299 	if (!combinations)
14300 		return -ENOMEM;
14301 
14302 	/* there are multiple radios */
14303 
14304 	radio = kzalloc_objs(*radio, ah->num_radio);
14305 	if (!radio) {
14306 		ret = -ENOMEM;
14307 		goto err_free_combinations;
14308 	}
14309 
14310 	for_each_ar(ah, ar, i) {
14311 		comb = kzalloc_obj(*comb);
14312 		if (!comb) {
14313 			ret = -ENOMEM;
14314 			goto err_free_radios;
14315 		}
14316 
14317 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
14318 		if (ret) {
14319 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
14320 				       i, ret);
14321 			kfree(comb);
14322 			goto err_free_radios;
14323 		}
14324 
14325 		radio[i].freq_range = &ar->freq_range;
14326 		radio[i].n_freq_range = 1;
14327 
14328 		radio[i].iface_combinations = comb;
14329 		radio[i].n_iface_combinations = 1;
14330 	}
14331 
14332 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
14333 	if (ret) {
14334 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
14335 			       ret);
14336 		goto err_free_all_radios;
14337 	}
14338 
14339 	wiphy->radio = radio;
14340 	wiphy->n_radio = ah->num_radio;
14341 
14342 out:
14343 	wiphy->iface_combinations = combinations;
14344 	wiphy->n_iface_combinations = n_combinations;
14345 
14346 	return 0;
14347 
14348 err_free_all_radios:
14349 	i = ah->num_radio;
14350 
14351 err_free_radios:
14352 	while (i--)
14353 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
14354 
14355 	kfree(radio);
14356 
14357 err_free_combinations:
14358 	kfree(combinations);
14359 
14360 	return ret;
14361 }
14362 
14363 static const u8 ath12k_if_types_ext_capa[] = {
14364 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14365 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14366 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14367 };
14368 
14369 static const u8 ath12k_if_types_ext_capa_sta[] = {
14370 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14371 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14372 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14373 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
14374 };
14375 
14376 static const u8 ath12k_if_types_ext_capa_ap[] = {
14377 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
14378 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
14379 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
14380 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
14381 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
14382 };
14383 
14384 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
14385 	{
14386 		.extended_capabilities = ath12k_if_types_ext_capa,
14387 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
14388 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
14389 	}, {
14390 		.iftype = NL80211_IFTYPE_STATION,
14391 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
14392 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
14393 		.extended_capabilities_len =
14394 				sizeof(ath12k_if_types_ext_capa_sta),
14395 	}, {
14396 		.iftype = NL80211_IFTYPE_AP,
14397 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
14398 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
14399 		.extended_capabilities_len =
14400 				sizeof(ath12k_if_types_ext_capa_ap),
14401 		.eml_capabilities = 0,
14402 		.mld_capa_and_ops = 0,
14403 	},
14404 };
14405 
ath12k_mac_cleanup_unregister(struct ath12k * ar)14406 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
14407 {
14408 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
14409 	idr_destroy(&ar->txmgmt_idr);
14410 
14411 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
14412 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
14413 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
14414 }
14415 
ath12k_mac_hw_unregister(struct ath12k_hw * ah)14416 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
14417 {
14418 	struct ieee80211_hw *hw = ah->hw;
14419 	struct ath12k *ar;
14420 	int i;
14421 
14422 	for_each_ar(ah, ar, i) {
14423 		cancel_work_sync(&ar->regd_channel_update_work);
14424 		cancel_work_sync(&ar->regd_update_work);
14425 		ath12k_debugfs_unregister(ar);
14426 		ath12k_fw_stats_reset(ar);
14427 	}
14428 
14429 	ieee80211_unregister_hw(hw);
14430 
14431 	for_each_ar(ah, ar, i)
14432 		ath12k_mac_cleanup_unregister(ar);
14433 
14434 	ath12k_mac_cleanup_iface_combinations(ah);
14435 
14436 	SET_IEEE80211_DEV(hw, NULL);
14437 }
14438 
ath12k_mac_setup_register(struct ath12k * ar,u32 * ht_cap,struct ieee80211_supported_band * bands[])14439 static int ath12k_mac_setup_register(struct ath12k *ar,
14440 				     u32 *ht_cap,
14441 				     struct ieee80211_supported_band *bands[])
14442 {
14443 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
14444 	int ret;
14445 
14446 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
14447 	idr_init(&ar->txmgmt_idr);
14448 	spin_lock_init(&ar->txmgmt_idr_lock);
14449 
14450 	ath12k_pdev_caps_update(ar);
14451 
14452 	ret = ath12k_mac_setup_channels_rates(ar,
14453 					      cap->supported_bands,
14454 					      bands);
14455 	if (ret)
14456 		return ret;
14457 
14458 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
14459 	ath12k_mac_setup_sband_iftype_data(ar, cap);
14460 
14461 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
14462 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
14463 
14464 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
14465 	ar->rssi_info.temp_offset = 0;
14466 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
14467 
14468 	return 0;
14469 }
14470 
ath12k_mac_hw_register(struct ath12k_hw * ah)14471 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
14472 {
14473 	struct ieee80211_hw *hw = ah->hw;
14474 	struct wiphy *wiphy = hw->wiphy;
14475 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
14476 	struct ath12k_base *ab = ar->ab;
14477 	struct ath12k_pdev *pdev;
14478 	struct ath12k_pdev_cap *cap;
14479 	static const u32 cipher_suites[] = {
14480 		WLAN_CIPHER_SUITE_TKIP,
14481 		WLAN_CIPHER_SUITE_CCMP,
14482 		WLAN_CIPHER_SUITE_AES_CMAC,
14483 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
14484 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
14485 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
14486 		WLAN_CIPHER_SUITE_GCMP,
14487 		WLAN_CIPHER_SUITE_GCMP_256,
14488 		WLAN_CIPHER_SUITE_CCMP_256,
14489 	};
14490 	int ret, i, j;
14491 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
14492 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
14493 	u8 *mac_addr = NULL;
14494 	u8 mbssid_max_interfaces = 0;
14495 
14496 	wiphy->max_ap_assoc_sta = 0;
14497 
14498 	for_each_ar(ah, ar, i) {
14499 		u32 ht_cap_info = 0;
14500 
14501 		pdev = ar->pdev;
14502 		if (ar->ab->pdevs_macaddr_valid) {
14503 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
14504 		} else {
14505 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
14506 			ar->mac_addr[4] += ar->pdev_idx;
14507 		}
14508 
14509 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
14510 		if (ret)
14511 			goto err_cleanup_unregister;
14512 
14513 		/* 6 GHz does not support HT Cap, hence do not consider it */
14514 		if (!ar->supports_6ghz)
14515 			ht_cap &= ht_cap_info;
14516 
14517 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
14518 
14519 		/* Advertise the max antenna support of all radios, driver can handle
14520 		 * per pdev specific antenna setting based on pdev cap when antenna
14521 		 * changes are made
14522 		 */
14523 		cap = &pdev->cap;
14524 
14525 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
14526 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
14527 
14528 		if (ar->supports_6ghz)
14529 			is_6ghz = true;
14530 
14531 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
14532 			is_raw_mode = true;
14533 
14534 		if (!ar->ab->hw_params->supports_monitor)
14535 			is_monitor_disable = true;
14536 
14537 		if (i == 0)
14538 			mac_addr = ar->mac_addr;
14539 		else
14540 			mac_addr = ab->mac_addr;
14541 
14542 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
14543 	}
14544 
14545 	wiphy->available_antennas_rx = antennas_rx;
14546 	wiphy->available_antennas_tx = antennas_tx;
14547 
14548 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
14549 	SET_IEEE80211_DEV(hw, ab->dev);
14550 
14551 	ret = ath12k_mac_setup_iface_combinations(ah);
14552 	if (ret) {
14553 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
14554 		goto err_complete_cleanup_unregister;
14555 	}
14556 
14557 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
14558 
14559 	if (ah->num_radio == 1 &&
14560 	    wiphy->bands[NL80211_BAND_2GHZ] &&
14561 	    wiphy->bands[NL80211_BAND_5GHZ] &&
14562 	    wiphy->bands[NL80211_BAND_6GHZ])
14563 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
14564 
14565 	ieee80211_hw_set(hw, SIGNAL_DBM);
14566 	ieee80211_hw_set(hw, SUPPORTS_PS);
14567 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
14568 	ieee80211_hw_set(hw, MFP_CAPABLE);
14569 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
14570 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
14571 	ieee80211_hw_set(hw, AP_LINK_PS);
14572 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
14573 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
14574 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
14575 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
14576 	ieee80211_hw_set(hw, QUEUE_CONTROL);
14577 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
14578 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
14579 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
14580 
14581 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
14582 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
14583 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
14584 	}
14585 
14586 	if (cap->nss_ratio_enabled)
14587 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
14588 
14589 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
14590 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
14591 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
14592 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
14593 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
14594 		ieee80211_hw_set(hw, USES_RSS);
14595 	}
14596 
14597 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
14598 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
14599 
14600 	/* TODO: Check if HT capability advertised from firmware is different
14601 	 * for each band for a dual band capable radio. It will be tricky to
14602 	 * handle it when the ht capability different for each band.
14603 	 */
14604 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
14605 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
14606 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
14607 
14608 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
14609 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
14610 
14611 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
14612 
14613 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
14614 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
14615 	wiphy->max_remain_on_channel_duration = 5000;
14616 
14617 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
14618 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
14619 				   NL80211_FEATURE_AP_SCAN;
14620 
14621 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
14622 
14623 	/* MLO is not yet supported so disable Wireless Extensions for now
14624 	 * to make sure ath12k users don't use it. This flag can be removed
14625 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
14626 	 */
14627 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
14628 
14629 	/* Copy over MLO related capabilities received from
14630 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
14631 	 */
14632 	if (ab->ag->mlo_capable) {
14633 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
14634 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
14635 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
14636 
14637 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
14638 	}
14639 
14640 	hw->queues = ATH12K_HW_MAX_QUEUES;
14641 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
14642 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
14643 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
14644 
14645 	hw->vif_data_size = sizeof(struct ath12k_vif);
14646 	hw->sta_data_size = sizeof(struct ath12k_sta);
14647 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
14648 
14649 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
14650 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
14651 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
14652 	if (test_bit(WMI_TLV_SERVICE_BSS_COLOR_OFFLOAD,
14653 		     ab->wmi_ab.svc_map)) {
14654 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_COLOR);
14655 		ieee80211_hw_set(hw, DETECTS_COLOR_COLLISION);
14656 	}
14657 
14658 	wiphy->cipher_suites = cipher_suites;
14659 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
14660 
14661 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
14662 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
14663 
14664 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
14665 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
14666 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
14667 
14668 	if (is_6ghz) {
14669 		wiphy_ext_feature_set(wiphy,
14670 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
14671 		wiphy_ext_feature_set(wiphy,
14672 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
14673 	}
14674 
14675 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
14676 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
14677 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
14678 
14679 	ath12k_reg_init(hw);
14680 
14681 	if (!is_raw_mode) {
14682 		hw->netdev_features = NETIF_F_HW_CSUM;
14683 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
14684 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
14685 	}
14686 
14687 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
14688 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
14689 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
14690 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
14691 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
14692 		wiphy->max_sched_scan_plan_interval =
14693 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
14694 		wiphy->max_sched_scan_plan_iterations =
14695 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
14696 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
14697 	}
14698 
14699 	ret = ath12k_wow_init(ar);
14700 	if (ret) {
14701 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
14702 		goto err_cleanup_if_combs;
14703 	}
14704 
14705 	/* Boot-time regulatory updates have already been processed.
14706 	 * Mark them as complete now, because after registration,
14707 	 * cfg80211 will notify us again if there are any pending hints.
14708 	 * We need to wait for those hints to be processed, so it's
14709 	 * important to mark the boot-time updates as complete before
14710 	 * proceeding with registration.
14711 	 */
14712 	for_each_ar(ah, ar, i)
14713 		complete_all(&ar->regd_update_completed);
14714 
14715 	ret = ieee80211_register_hw(hw);
14716 	if (ret) {
14717 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
14718 		goto err_cleanup_if_combs;
14719 	}
14720 
14721 	if (is_monitor_disable)
14722 		/* There's a race between calling ieee80211_register_hw()
14723 		 * and here where the monitor mode is enabled for a little
14724 		 * while. But that time is so short and in practice it doesn't make
14725 		 * a difference in real life.
14726 		 */
14727 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
14728 
14729 	for_each_ar(ah, ar, i) {
14730 		/* Apply the regd received during initialization */
14731 		ret = ath12k_regd_update(ar, true);
14732 		if (ret) {
14733 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
14734 			goto err_unregister_hw;
14735 		}
14736 
14737 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
14738 			struct wmi_set_current_country_arg current_cc = {};
14739 
14740 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
14741 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
14742 
14743 			reinit_completion(&ar->regd_update_completed);
14744 
14745 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
14746 			if (ret)
14747 				ath12k_warn(ar->ab,
14748 					    "failed set cc code for mac register: %d\n",
14749 					    ret);
14750 		}
14751 
14752 		ath12k_fw_stats_init(ar);
14753 		ath12k_debugfs_register(ar);
14754 	}
14755 
14756 	return 0;
14757 
14758 err_unregister_hw:
14759 	for_each_ar(ah, ar, i)
14760 		ath12k_debugfs_unregister(ar);
14761 
14762 	ieee80211_unregister_hw(hw);
14763 
14764 err_cleanup_if_combs:
14765 	ath12k_mac_cleanup_iface_combinations(ah);
14766 
14767 err_complete_cleanup_unregister:
14768 	i = ah->num_radio;
14769 
14770 err_cleanup_unregister:
14771 	for (j = 0; j < i; j++) {
14772 		ar = ath12k_ah_to_ar(ah, j);
14773 		ath12k_mac_cleanup_unregister(ar);
14774 	}
14775 
14776 	SET_IEEE80211_DEV(hw, NULL);
14777 
14778 	return ret;
14779 }
14780 
ath12k_mac_setup(struct ath12k * ar)14781 static void ath12k_mac_setup(struct ath12k *ar)
14782 {
14783 	struct ath12k_base *ab = ar->ab;
14784 	struct ath12k_pdev *pdev = ar->pdev;
14785 	u8 pdev_idx = ar->pdev_idx;
14786 
14787 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
14788 
14789 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
14790 	/* FIXME: wmi[0] is already initialized during attach,
14791 	 * Should we do this again?
14792 	 */
14793 	ath12k_wmi_pdev_attach(ab, pdev_idx);
14794 
14795 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
14796 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
14797 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
14798 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
14799 	ar->scan.arvif = NULL;
14800 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
14801 
14802 	spin_lock_init(&ar->data_lock);
14803 	spin_lock_init(&ar->dp.ppdu_list_lock);
14804 	INIT_LIST_HEAD(&ar->arvifs);
14805 	INIT_LIST_HEAD(&ar->dp.ppdu_stats_info);
14806 
14807 	init_completion(&ar->vdev_setup_done);
14808 	init_completion(&ar->vdev_delete_done);
14809 	init_completion(&ar->peer_assoc_done);
14810 	init_completion(&ar->peer_delete_done);
14811 	init_completion(&ar->install_key_done);
14812 	init_completion(&ar->bss_survey_done);
14813 	init_completion(&ar->scan.started);
14814 	init_completion(&ar->scan.completed);
14815 	init_completion(&ar->scan.on_channel);
14816 	init_completion(&ar->mlo_setup_done);
14817 	init_completion(&ar->completed_11d_scan);
14818 	init_completion(&ar->regd_update_completed);
14819 	init_completion(&ar->thermal.wmi_sync);
14820 
14821 	ar->thermal.temperature = 0;
14822 	ar->thermal.hwmon_dev = NULL;
14823 
14824 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
14825 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
14826 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
14827 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
14828 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
14829 
14830 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
14831 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
14832 
14833 	ar->monitor_vdev_id = -1;
14834 	ar->monitor_vdev_created = false;
14835 	ar->monitor_started = false;
14836 }
14837 
__ath12k_mac_mlo_setup(struct ath12k * ar)14838 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
14839 {
14840 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
14841 	struct ath12k_base *partner_ab, *ab = ar->ab;
14842 	struct ath12k_hw_group *ag = ab->ag;
14843 	struct wmi_mlo_setup_arg mlo = {};
14844 	struct ath12k_pdev *pdev;
14845 	unsigned long time_left;
14846 	int i, j, ret;
14847 
14848 	lockdep_assert_held(&ag->mutex);
14849 
14850 	reinit_completion(&ar->mlo_setup_done);
14851 
14852 	for (i = 0; i < ag->num_devices; i++) {
14853 		partner_ab = ag->ab[i];
14854 
14855 		for (j = 0; j < partner_ab->num_radios; j++) {
14856 			pdev = &partner_ab->pdevs[j];
14857 
14858 			/* Avoid the self link */
14859 			if (ar == pdev->ar)
14860 				continue;
14861 
14862 			partner_link_id[num_link] = pdev->hw_link_id;
14863 			num_link++;
14864 
14865 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
14866 				   i, j, pdev->hw_link_id, num_link);
14867 		}
14868 	}
14869 
14870 	if (num_link == 0)
14871 		return 0;
14872 
14873 	mlo.group_id = cpu_to_le32(ag->id);
14874 	mlo.partner_link_id = partner_link_id;
14875 	mlo.num_partner_links = num_link;
14876 	ar->mlo_setup_status = 0;
14877 
14878 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
14879 
14880 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
14881 	if (ret) {
14882 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
14883 			   ar->pdev_idx, ret);
14884 		return ret;
14885 	}
14886 
14887 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
14888 						WMI_MLO_CMD_TIMEOUT_HZ);
14889 
14890 	if (!time_left || ar->mlo_setup_status)
14891 		return ar->mlo_setup_status ? : -ETIMEDOUT;
14892 
14893 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
14894 
14895 	return 0;
14896 }
14897 
__ath12k_mac_mlo_teardown(struct ath12k * ar)14898 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
14899 {
14900 	struct ath12k_base *ab = ar->ab;
14901 	int ret;
14902 	u8 num_link;
14903 
14904 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
14905 		return 0;
14906 
14907 	num_link = ath12k_get_num_partner_link(ar);
14908 
14909 	if (num_link == 0)
14910 		return 0;
14911 
14912 	ret = ath12k_wmi_mlo_teardown(ar);
14913 	if (ret) {
14914 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
14915 			    ar->pdev_idx, ret);
14916 		return ret;
14917 	}
14918 
14919 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
14920 
14921 	return 0;
14922 }
14923 
ath12k_mac_mlo_setup(struct ath12k_hw_group * ag)14924 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
14925 {
14926 	struct ath12k_hw *ah;
14927 	struct ath12k *ar;
14928 	int ret;
14929 	int i, j;
14930 
14931 	for (i = 0; i < ag->num_hw; i++) {
14932 		ah = ag->ah[i];
14933 		if (!ah)
14934 			continue;
14935 
14936 		for_each_ar(ah, ar, j) {
14937 			ar = &ah->radio[j];
14938 			ret = __ath12k_mac_mlo_setup(ar);
14939 			if (ret) {
14940 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
14941 				goto err_setup;
14942 			}
14943 		}
14944 	}
14945 
14946 	return 0;
14947 
14948 err_setup:
14949 	for (i = i - 1; i >= 0; i--) {
14950 		ah = ag->ah[i];
14951 		if (!ah)
14952 			continue;
14953 
14954 		for (j = j - 1; j >= 0; j--) {
14955 			ar = &ah->radio[j];
14956 			if (!ar)
14957 				continue;
14958 
14959 			__ath12k_mac_mlo_teardown(ar);
14960 		}
14961 	}
14962 
14963 	return ret;
14964 }
14965 
ath12k_mac_mlo_teardown(struct ath12k_hw_group * ag)14966 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
14967 {
14968 	struct ath12k_hw *ah;
14969 	struct ath12k *ar;
14970 	int ret, i, j;
14971 
14972 	for (i = 0; i < ag->num_hw; i++) {
14973 		ah = ag->ah[i];
14974 		if (!ah)
14975 			continue;
14976 
14977 		for_each_ar(ah, ar, j) {
14978 			ar = &ah->radio[j];
14979 			ret = __ath12k_mac_mlo_teardown(ar);
14980 			if (ret) {
14981 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
14982 				break;
14983 			}
14984 		}
14985 	}
14986 }
14987 
ath12k_mac_register(struct ath12k_hw_group * ag)14988 int ath12k_mac_register(struct ath12k_hw_group *ag)
14989 {
14990 	struct ath12k_hw *ah;
14991 	int i;
14992 	int ret;
14993 
14994 	for (i = 0; i < ag->num_hw; i++) {
14995 		ah = ath12k_ag_to_ah(ag, i);
14996 
14997 		ret = ath12k_mac_hw_register(ah);
14998 		if (ret)
14999 			goto err;
15000 	}
15001 
15002 	return 0;
15003 
15004 err:
15005 	for (i = i - 1; i >= 0; i--) {
15006 		ah = ath12k_ag_to_ah(ag, i);
15007 		if (!ah)
15008 			continue;
15009 
15010 		ath12k_mac_hw_unregister(ah);
15011 	}
15012 
15013 	return ret;
15014 }
15015 
ath12k_mac_unregister(struct ath12k_hw_group * ag)15016 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
15017 {
15018 	struct ath12k_hw *ah;
15019 	int i;
15020 
15021 	for (i = ag->num_hw - 1; i >= 0; i--) {
15022 		ah = ath12k_ag_to_ah(ag, i);
15023 		if (!ah)
15024 			continue;
15025 
15026 		ath12k_mac_hw_unregister(ah);
15027 	}
15028 }
15029 
ath12k_mac_hw_destroy(struct ath12k_hw * ah)15030 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
15031 {
15032 	ieee80211_free_hw(ah->hw);
15033 }
15034 
ath12k_mac_hw_allocate(struct ath12k_hw_group * ag,struct ath12k_pdev_map * pdev_map,u8 num_pdev_map)15035 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
15036 						struct ath12k_pdev_map *pdev_map,
15037 						u8 num_pdev_map)
15038 {
15039 	struct ieee80211_hw *hw;
15040 	struct ath12k *ar;
15041 	struct ath12k_base *ab;
15042 	struct ath12k_pdev *pdev;
15043 	struct ath12k_hw *ah;
15044 	int i;
15045 	u8 pdev_idx;
15046 
15047 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
15048 				pdev_map->ab->ath12k_ops);
15049 	if (!hw)
15050 		return NULL;
15051 
15052 	ah = ath12k_hw_to_ah(hw);
15053 	ah->hw = hw;
15054 	ah->num_radio = num_pdev_map;
15055 
15056 	mutex_init(&ah->hw_mutex);
15057 
15058 	spin_lock_init(&ah->dp_hw.peer_lock);
15059 	INIT_LIST_HEAD(&ah->dp_hw.dp_peers_list);
15060 
15061 	for (i = 0; i < num_pdev_map; i++) {
15062 		ab = pdev_map[i].ab;
15063 		pdev_idx = pdev_map[i].pdev_idx;
15064 		pdev = &ab->pdevs[pdev_idx];
15065 
15066 		ar = ath12k_ah_to_ar(ah, i);
15067 		ar->ah = ah;
15068 		ar->ab = ab;
15069 		ar->hw_link_id = pdev->hw_link_id;
15070 		ar->pdev = pdev;
15071 		ar->pdev_idx = pdev_idx;
15072 		ar->radio_idx = i;
15073 		pdev->ar = ar;
15074 
15075 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
15076 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
15077 
15078 		ath12k_mac_setup(ar);
15079 		ath12k_dp_pdev_pre_alloc(ar);
15080 	}
15081 
15082 	return ah;
15083 }
15084 
ath12k_mac_destroy(struct ath12k_hw_group * ag)15085 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
15086 {
15087 	struct ath12k_pdev *pdev;
15088 	struct ath12k_base *ab = ag->ab[0];
15089 	int i, j;
15090 	struct ath12k_hw *ah;
15091 
15092 	for (i = 0; i < ag->num_devices; i++) {
15093 		ab = ag->ab[i];
15094 		if (!ab)
15095 			continue;
15096 
15097 		for (j = 0; j < ab->num_radios; j++) {
15098 			pdev = &ab->pdevs[j];
15099 			if (!pdev->ar)
15100 				continue;
15101 			pdev->ar = NULL;
15102 		}
15103 	}
15104 
15105 	for (i = 0; i < ag->num_hw; i++) {
15106 		ah = ath12k_ag_to_ah(ag, i);
15107 		if (!ah)
15108 			continue;
15109 
15110 		ath12k_mac_hw_destroy(ah);
15111 		ath12k_ag_set_ah(ag, i, NULL);
15112 	}
15113 }
15114 
ath12k_mac_set_device_defaults(struct ath12k_base * ab)15115 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
15116 {
15117 	int total_vdev;
15118 
15119 	/* Initialize channel counters frequency value in hertz */
15120 	ab->cc_freq_hz = 320000;
15121 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
15122 	ab->free_vdev_map = (1LL << total_vdev) - 1;
15123 }
15124 
ath12k_mac_allocate(struct ath12k_hw_group * ag)15125 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
15126 {
15127 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
15128 	int mac_id, device_id, total_radio, num_hw;
15129 	struct ath12k_base *ab;
15130 	struct ath12k_hw *ah;
15131 	int ret, i, j;
15132 	u8 radio_per_hw;
15133 
15134 	total_radio = 0;
15135 	for (i = 0; i < ag->num_devices; i++) {
15136 		ab = ag->ab[i];
15137 		if (!ab)
15138 			continue;
15139 
15140 		ath12k_mac_set_device_defaults(ab);
15141 		total_radio += ab->num_radios;
15142 	}
15143 
15144 	if (!total_radio)
15145 		return -EINVAL;
15146 
15147 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
15148 		return -ENOSPC;
15149 
15150 	/* All pdev get combined and register as single wiphy based on
15151 	 * hardware group which participate in multi-link operation else
15152 	 * each pdev get register separately.
15153 	 */
15154 	if (ag->mlo_capable)
15155 		radio_per_hw = total_radio;
15156 	else
15157 		radio_per_hw = 1;
15158 
15159 	num_hw = total_radio / radio_per_hw;
15160 
15161 	ag->num_hw = 0;
15162 	device_id = 0;
15163 	mac_id = 0;
15164 	for (i = 0; i < num_hw; i++) {
15165 		for (j = 0; j < radio_per_hw; j++) {
15166 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
15167 				ret = -ENOSPC;
15168 				goto err;
15169 			}
15170 
15171 			ab = ag->ab[device_id];
15172 			pdev_map[j].ab = ab;
15173 			pdev_map[j].pdev_idx = mac_id;
15174 			mac_id++;
15175 
15176 			/* If mac_id falls beyond the current device MACs then
15177 			 * move to next device
15178 			 */
15179 			if (mac_id >= ab->num_radios) {
15180 				mac_id = 0;
15181 				device_id++;
15182 			}
15183 		}
15184 
15185 		ab = pdev_map->ab;
15186 
15187 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
15188 		if (!ah) {
15189 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
15190 				    i);
15191 			ret = -ENOMEM;
15192 			goto err;
15193 		}
15194 
15195 		ah->dev = ab->dev;
15196 
15197 		ag->ah[i] = ah;
15198 		ag->num_hw++;
15199 	}
15200 
15201 	return 0;
15202 
15203 err:
15204 	for (i = i - 1; i >= 0; i--) {
15205 		ah = ath12k_ag_to_ah(ag, i);
15206 		if (!ah)
15207 			continue;
15208 
15209 		ath12k_mac_hw_destroy(ah);
15210 		ath12k_ag_set_ah(ag, i, NULL);
15211 	}
15212 
15213 	return ret;
15214 }
15215 
ath12k_mac_vif_set_keepalive(struct ath12k_link_vif * arvif,enum wmi_sta_keepalive_method method,u32 interval)15216 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
15217 				 enum wmi_sta_keepalive_method method,
15218 				 u32 interval)
15219 {
15220 	struct wmi_sta_keepalive_arg arg = {};
15221 	struct ath12k *ar = arvif->ar;
15222 	int ret;
15223 
15224 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
15225 
15226 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
15227 		return 0;
15228 
15229 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
15230 		return 0;
15231 
15232 	arg.vdev_id = arvif->vdev_id;
15233 	arg.enabled = 1;
15234 	arg.method = method;
15235 	arg.interval = interval;
15236 
15237 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
15238 	if (ret) {
15239 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
15240 			    arvif->vdev_id, ret);
15241 		return ret;
15242 	}
15243 
15244 	return 0;
15245 }
15246