xref: /linux/drivers/net/wireless/ath/ath12k/mac.c (revision db87bd2ad1f736c2f7ab231f9b40c885934f6b2c)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2025 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 
7 #include <net/mac80211.h>
8 #include <net/cfg80211.h>
9 #include <linux/etherdevice.h>
10 
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 
25 #define CHAN2G(_channel, _freq, _flags) { \
26 	.band                   = NL80211_BAND_2GHZ, \
27 	.hw_value               = (_channel), \
28 	.center_freq            = (_freq), \
29 	.flags                  = (_flags), \
30 	.max_antenna_gain       = 0, \
31 	.max_power              = 30, \
32 }
33 
34 #define CHAN5G(_channel, _freq, _flags) { \
35 	.band                   = NL80211_BAND_5GHZ, \
36 	.hw_value               = (_channel), \
37 	.center_freq            = (_freq), \
38 	.flags                  = (_flags), \
39 	.max_antenna_gain       = 0, \
40 	.max_power              = 30, \
41 }
42 
43 #define CHAN6G(_channel, _freq, _flags) { \
44 	.band                   = NL80211_BAND_6GHZ, \
45 	.hw_value               = (_channel), \
46 	.center_freq            = (_freq), \
47 	.flags                  = (_flags), \
48 	.max_antenna_gain       = 0, \
49 	.max_power              = 30, \
50 }
51 
52 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
53 	CHAN2G(1, 2412, 0),
54 	CHAN2G(2, 2417, 0),
55 	CHAN2G(3, 2422, 0),
56 	CHAN2G(4, 2427, 0),
57 	CHAN2G(5, 2432, 0),
58 	CHAN2G(6, 2437, 0),
59 	CHAN2G(7, 2442, 0),
60 	CHAN2G(8, 2447, 0),
61 	CHAN2G(9, 2452, 0),
62 	CHAN2G(10, 2457, 0),
63 	CHAN2G(11, 2462, 0),
64 	CHAN2G(12, 2467, 0),
65 	CHAN2G(13, 2472, 0),
66 	CHAN2G(14, 2484, 0),
67 };
68 
69 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
70 	CHAN5G(36, 5180, 0),
71 	CHAN5G(40, 5200, 0),
72 	CHAN5G(44, 5220, 0),
73 	CHAN5G(48, 5240, 0),
74 	CHAN5G(52, 5260, 0),
75 	CHAN5G(56, 5280, 0),
76 	CHAN5G(60, 5300, 0),
77 	CHAN5G(64, 5320, 0),
78 	CHAN5G(100, 5500, 0),
79 	CHAN5G(104, 5520, 0),
80 	CHAN5G(108, 5540, 0),
81 	CHAN5G(112, 5560, 0),
82 	CHAN5G(116, 5580, 0),
83 	CHAN5G(120, 5600, 0),
84 	CHAN5G(124, 5620, 0),
85 	CHAN5G(128, 5640, 0),
86 	CHAN5G(132, 5660, 0),
87 	CHAN5G(136, 5680, 0),
88 	CHAN5G(140, 5700, 0),
89 	CHAN5G(144, 5720, 0),
90 	CHAN5G(149, 5745, 0),
91 	CHAN5G(153, 5765, 0),
92 	CHAN5G(157, 5785, 0),
93 	CHAN5G(161, 5805, 0),
94 	CHAN5G(165, 5825, 0),
95 	CHAN5G(169, 5845, 0),
96 	CHAN5G(173, 5865, 0),
97 };
98 
99 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
100 	/* Operating Class 136 */
101 	CHAN6G(2, 5935, 0),
102 
103 	/* Operating Classes 131-135 */
104 	CHAN6G(1, 5955, 0),
105 	CHAN6G(5, 5975, 0),
106 	CHAN6G(9, 5995, 0),
107 	CHAN6G(13, 6015, 0),
108 	CHAN6G(17, 6035, 0),
109 	CHAN6G(21, 6055, 0),
110 	CHAN6G(25, 6075, 0),
111 	CHAN6G(29, 6095, 0),
112 	CHAN6G(33, 6115, 0),
113 	CHAN6G(37, 6135, 0),
114 	CHAN6G(41, 6155, 0),
115 	CHAN6G(45, 6175, 0),
116 	CHAN6G(49, 6195, 0),
117 	CHAN6G(53, 6215, 0),
118 	CHAN6G(57, 6235, 0),
119 	CHAN6G(61, 6255, 0),
120 	CHAN6G(65, 6275, 0),
121 	CHAN6G(69, 6295, 0),
122 	CHAN6G(73, 6315, 0),
123 	CHAN6G(77, 6335, 0),
124 	CHAN6G(81, 6355, 0),
125 	CHAN6G(85, 6375, 0),
126 	CHAN6G(89, 6395, 0),
127 	CHAN6G(93, 6415, 0),
128 	CHAN6G(97, 6435, 0),
129 	CHAN6G(101, 6455, 0),
130 	CHAN6G(105, 6475, 0),
131 	CHAN6G(109, 6495, 0),
132 	CHAN6G(113, 6515, 0),
133 	CHAN6G(117, 6535, 0),
134 	CHAN6G(121, 6555, 0),
135 	CHAN6G(125, 6575, 0),
136 	CHAN6G(129, 6595, 0),
137 	CHAN6G(133, 6615, 0),
138 	CHAN6G(137, 6635, 0),
139 	CHAN6G(141, 6655, 0),
140 	CHAN6G(145, 6675, 0),
141 	CHAN6G(149, 6695, 0),
142 	CHAN6G(153, 6715, 0),
143 	CHAN6G(157, 6735, 0),
144 	CHAN6G(161, 6755, 0),
145 	CHAN6G(165, 6775, 0),
146 	CHAN6G(169, 6795, 0),
147 	CHAN6G(173, 6815, 0),
148 	CHAN6G(177, 6835, 0),
149 	CHAN6G(181, 6855, 0),
150 	CHAN6G(185, 6875, 0),
151 	CHAN6G(189, 6895, 0),
152 	CHAN6G(193, 6915, 0),
153 	CHAN6G(197, 6935, 0),
154 	CHAN6G(201, 6955, 0),
155 	CHAN6G(205, 6975, 0),
156 	CHAN6G(209, 6995, 0),
157 	CHAN6G(213, 7015, 0),
158 	CHAN6G(217, 7035, 0),
159 	CHAN6G(221, 7055, 0),
160 	CHAN6G(225, 7075, 0),
161 	CHAN6G(229, 7095, 0),
162 	CHAN6G(233, 7115, 0),
163 };
164 
165 static struct ieee80211_rate ath12k_legacy_rates[] = {
166 	{ .bitrate = 10,
167 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
168 	{ .bitrate = 20,
169 	  .hw_value = ATH12K_HW_RATE_CCK_LP_2M,
170 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_2M,
171 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
172 	{ .bitrate = 55,
173 	  .hw_value = ATH12K_HW_RATE_CCK_LP_5_5M,
174 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_5_5M,
175 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
176 	{ .bitrate = 110,
177 	  .hw_value = ATH12K_HW_RATE_CCK_LP_11M,
178 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_11M,
179 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
180 
181 	{ .bitrate = 60, .hw_value = ATH12K_HW_RATE_OFDM_6M },
182 	{ .bitrate = 90, .hw_value = ATH12K_HW_RATE_OFDM_9M },
183 	{ .bitrate = 120, .hw_value = ATH12K_HW_RATE_OFDM_12M },
184 	{ .bitrate = 180, .hw_value = ATH12K_HW_RATE_OFDM_18M },
185 	{ .bitrate = 240, .hw_value = ATH12K_HW_RATE_OFDM_24M },
186 	{ .bitrate = 360, .hw_value = ATH12K_HW_RATE_OFDM_36M },
187 	{ .bitrate = 480, .hw_value = ATH12K_HW_RATE_OFDM_48M },
188 	{ .bitrate = 540, .hw_value = ATH12K_HW_RATE_OFDM_54M },
189 };
190 
191 static const int
192 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
193 	[NL80211_BAND_2GHZ] = {
194 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
195 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
196 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
197 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
198 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
199 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
200 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
201 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
202 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
203 	},
204 	[NL80211_BAND_5GHZ] = {
205 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
206 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
207 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
208 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
209 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
210 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
211 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
212 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
213 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
214 	},
215 	[NL80211_BAND_6GHZ] = {
216 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
217 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
218 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
219 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
220 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
221 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
222 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
223 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
224 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
225 	},
226 
227 };
228 
229 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
230 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
231 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
232 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE |
233 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_START_USER_INFO,
234 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
235 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
236 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
237 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
238 			     HTT_RX_FP_CTRL_FILTER_FLASG3
239 };
240 
241 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
242 #define ath12k_g_rates ath12k_legacy_rates
243 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
244 #define ath12k_a_rates (ath12k_legacy_rates + 4)
245 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
246 
247 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
248 
249 static const u32 ath12k_smps_map[] = {
250 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
251 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
252 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
253 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
254 };
255 
256 static int ath12k_start_vdev_delay(struct ath12k *ar,
257 				   struct ath12k_link_vif *arvif);
258 static void ath12k_mac_stop(struct ath12k *ar);
259 static int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif);
260 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif);
261 
ath12k_mac_phymode_str(enum wmi_phy_mode mode)262 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
263 {
264 	switch (mode) {
265 	case MODE_11A:
266 		return "11a";
267 	case MODE_11G:
268 		return "11g";
269 	case MODE_11B:
270 		return "11b";
271 	case MODE_11GONLY:
272 		return "11gonly";
273 	case MODE_11NA_HT20:
274 		return "11na-ht20";
275 	case MODE_11NG_HT20:
276 		return "11ng-ht20";
277 	case MODE_11NA_HT40:
278 		return "11na-ht40";
279 	case MODE_11NG_HT40:
280 		return "11ng-ht40";
281 	case MODE_11AC_VHT20:
282 		return "11ac-vht20";
283 	case MODE_11AC_VHT40:
284 		return "11ac-vht40";
285 	case MODE_11AC_VHT80:
286 		return "11ac-vht80";
287 	case MODE_11AC_VHT160:
288 		return "11ac-vht160";
289 	case MODE_11AC_VHT80_80:
290 		return "11ac-vht80+80";
291 	case MODE_11AC_VHT20_2G:
292 		return "11ac-vht20-2g";
293 	case MODE_11AC_VHT40_2G:
294 		return "11ac-vht40-2g";
295 	case MODE_11AC_VHT80_2G:
296 		return "11ac-vht80-2g";
297 	case MODE_11AX_HE20:
298 		return "11ax-he20";
299 	case MODE_11AX_HE40:
300 		return "11ax-he40";
301 	case MODE_11AX_HE80:
302 		return "11ax-he80";
303 	case MODE_11AX_HE80_80:
304 		return "11ax-he80+80";
305 	case MODE_11AX_HE160:
306 		return "11ax-he160";
307 	case MODE_11AX_HE20_2G:
308 		return "11ax-he20-2g";
309 	case MODE_11AX_HE40_2G:
310 		return "11ax-he40-2g";
311 	case MODE_11AX_HE80_2G:
312 		return "11ax-he80-2g";
313 	case MODE_11BE_EHT20:
314 		return "11be-eht20";
315 	case MODE_11BE_EHT40:
316 		return "11be-eht40";
317 	case MODE_11BE_EHT80:
318 		return "11be-eht80";
319 	case MODE_11BE_EHT80_80:
320 		return "11be-eht80+80";
321 	case MODE_11BE_EHT160:
322 		return "11be-eht160";
323 	case MODE_11BE_EHT160_160:
324 		return "11be-eht160+160";
325 	case MODE_11BE_EHT320:
326 		return "11be-eht320";
327 	case MODE_11BE_EHT20_2G:
328 		return "11be-eht20-2g";
329 	case MODE_11BE_EHT40_2G:
330 		return "11be-eht40-2g";
331 	case MODE_UNKNOWN:
332 		/* skip */
333 		break;
334 
335 		/* no default handler to allow compiler to check that the
336 		 * enum is fully handled
337 		 */
338 	}
339 
340 	return "<unknown>";
341 }
342 
ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)343 u16 ath12k_mac_he_convert_tones_to_ru_tones(u16 tones)
344 {
345 	switch (tones) {
346 	case 26:
347 		return RU_26;
348 	case 52:
349 		return RU_52;
350 	case 106:
351 		return RU_106;
352 	case 242:
353 		return RU_242;
354 	case 484:
355 		return RU_484;
356 	case 996:
357 		return RU_996;
358 	case (996 * 2):
359 		return RU_2X996;
360 	default:
361 		return RU_26;
362 	}
363 }
364 
ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)365 enum nl80211_eht_gi ath12k_mac_eht_gi_to_nl80211_eht_gi(u8 sgi)
366 {
367 	switch (sgi) {
368 	case RX_MSDU_START_SGI_0_8_US:
369 		return NL80211_RATE_INFO_EHT_GI_0_8;
370 	case RX_MSDU_START_SGI_1_6_US:
371 		return NL80211_RATE_INFO_EHT_GI_1_6;
372 	case RX_MSDU_START_SGI_3_2_US:
373 		return NL80211_RATE_INFO_EHT_GI_3_2;
374 	default:
375 		return NL80211_RATE_INFO_EHT_GI_0_8;
376 	}
377 }
378 
ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)379 enum nl80211_eht_ru_alloc ath12k_mac_eht_ru_tones_to_nl80211_eht_ru_alloc(u16 ru_tones)
380 {
381 	switch (ru_tones) {
382 	case 26:
383 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
384 	case 52:
385 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52;
386 	case (52 + 26):
387 		return NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
388 	case 106:
389 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106;
390 	case (106 + 26):
391 		return NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
392 	case 242:
393 		return NL80211_RATE_INFO_EHT_RU_ALLOC_242;
394 	case 484:
395 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484;
396 	case (484 + 242):
397 		return NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
398 	case 996:
399 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996;
400 	case (996 + 484):
401 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
402 	case (996 + 484 + 242):
403 		return NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
404 	case (2 * 996):
405 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
406 	case (2 * 996 + 484):
407 		return NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
408 	case (3 * 996):
409 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
410 	case (3 * 996 + 484):
411 		return NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
412 	case (4 * 996):
413 		return NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
414 	default:
415 		return NL80211_RATE_INFO_EHT_RU_ALLOC_26;
416 	}
417 }
418 
419 enum rate_info_bw
ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)420 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
421 {
422 	u8 ret = RATE_INFO_BW_20;
423 
424 	switch (bw) {
425 	case ATH12K_BW_20:
426 		ret = RATE_INFO_BW_20;
427 		break;
428 	case ATH12K_BW_40:
429 		ret = RATE_INFO_BW_40;
430 		break;
431 	case ATH12K_BW_80:
432 		ret = RATE_INFO_BW_80;
433 		break;
434 	case ATH12K_BW_160:
435 		ret = RATE_INFO_BW_160;
436 		break;
437 	case ATH12K_BW_320:
438 		ret = RATE_INFO_BW_320;
439 		break;
440 	}
441 
442 	return ret;
443 }
444 
ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)445 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
446 {
447 	switch (bw) {
448 	case RATE_INFO_BW_20:
449 		return ATH12K_BW_20;
450 	case RATE_INFO_BW_40:
451 		return ATH12K_BW_40;
452 	case RATE_INFO_BW_80:
453 		return ATH12K_BW_80;
454 	case RATE_INFO_BW_160:
455 		return ATH12K_BW_160;
456 	case RATE_INFO_BW_320:
457 		return ATH12K_BW_320;
458 	default:
459 		return ATH12K_BW_20;
460 	}
461 }
462 
ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc,u8 preamble,u8 * rateidx,u16 * rate)463 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
464 					  u16 *rate)
465 {
466 	/* As default, it is OFDM rates */
467 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
468 	int max_rates_idx = ath12k_g_rates_size;
469 
470 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
471 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
472 		i = 0;
473 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
474 	}
475 
476 	while (i < max_rates_idx) {
477 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
478 			*rateidx = i;
479 			*rate = ath12k_legacy_rates[i].bitrate;
480 			return 0;
481 		}
482 		i++;
483 	}
484 
485 	return -EINVAL;
486 }
487 
ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band * sband,u32 bitrate)488 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
489 			     u32 bitrate)
490 {
491 	int i;
492 
493 	for (i = 0; i < sband->n_bitrates; i++)
494 		if (sband->bitrates[i].bitrate == bitrate)
495 			return i;
496 
497 	return 0;
498 }
499 
500 static u32
ath12k_mac_max_ht_nss(const u8 * ht_mcs_mask)501 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
502 {
503 	int nss;
504 
505 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
506 		if (ht_mcs_mask[nss])
507 			return nss + 1;
508 
509 	return 1;
510 }
511 
512 static u32
ath12k_mac_max_vht_nss(const u16 * vht_mcs_mask)513 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
514 {
515 	int nss;
516 
517 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
518 		if (vht_mcs_mask[nss])
519 			return nss + 1;
520 
521 	return 1;
522 }
523 
524 static u32
ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])525 ath12k_mac_max_he_nss(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
526 {
527 	int nss;
528 
529 	for (nss = NL80211_HE_NSS_MAX - 1; nss >= 0; nss--)
530 		if (he_mcs_mask[nss])
531 			return nss + 1;
532 
533 	return 1;
534 }
535 
ath12k_parse_mpdudensity(u8 mpdudensity)536 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
537 {
538 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
539  *   0 for no restriction
540  *   1 for 1/4 us
541  *   2 for 1/2 us
542  *   3 for 1 us
543  *   4 for 2 us
544  *   5 for 4 us
545  *   6 for 8 us
546  *   7 for 16 us
547  */
548 	switch (mpdudensity) {
549 	case 0:
550 		return 0;
551 	case 1:
552 	case 2:
553 	case 3:
554 	/* Our lower layer calculations limit our precision to
555 	 * 1 microsecond
556 	 */
557 		return 1;
558 	case 4:
559 		return 2;
560 	case 5:
561 		return 4;
562 	case 6:
563 		return 8;
564 	case 7:
565 		return 16;
566 	default:
567 		return 0;
568 	}
569 }
570 
ath12k_mac_vif_link_chan(struct ieee80211_vif * vif,u8 link_id,struct cfg80211_chan_def * def)571 static int ath12k_mac_vif_link_chan(struct ieee80211_vif *vif, u8 link_id,
572 				    struct cfg80211_chan_def *def)
573 {
574 	struct ieee80211_bss_conf *link_conf;
575 	struct ieee80211_chanctx_conf *conf;
576 
577 	rcu_read_lock();
578 	link_conf = rcu_dereference(vif->link_conf[link_id]);
579 
580 	if (!link_conf) {
581 		rcu_read_unlock();
582 		return -ENOLINK;
583 	}
584 
585 	conf = rcu_dereference(link_conf->chanctx_conf);
586 	if (!conf) {
587 		rcu_read_unlock();
588 		return -ENOENT;
589 	}
590 	*def = conf->def;
591 	rcu_read_unlock();
592 
593 	return 0;
594 }
595 
596 static struct ath12k_link_vif *
ath12k_mac_get_tx_arvif(struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * link_conf)597 ath12k_mac_get_tx_arvif(struct ath12k_link_vif *arvif,
598 			struct ieee80211_bss_conf *link_conf)
599 {
600 	struct ieee80211_bss_conf *tx_bss_conf;
601 	struct ath12k *ar = arvif->ar;
602 	struct ath12k_vif *tx_ahvif;
603 
604 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
605 
606 	tx_bss_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
607 					link_conf->tx_bss_conf);
608 	if (tx_bss_conf) {
609 		tx_ahvif = ath12k_vif_to_ahvif(tx_bss_conf->vif);
610 		return wiphy_dereference(tx_ahvif->ah->hw->wiphy,
611 					 tx_ahvif->link[tx_bss_conf->link_id]);
612 	}
613 
614 	return NULL;
615 }
616 
ath12k_mac_get_tx_bssid(struct ath12k_link_vif * arvif)617 static const u8 *ath12k_mac_get_tx_bssid(struct ath12k_link_vif *arvif)
618 {
619 	struct ieee80211_bss_conf *link_conf;
620 	struct ath12k_link_vif *tx_arvif;
621 	struct ath12k *ar = arvif->ar;
622 
623 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
624 
625 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
626 	if (!link_conf) {
627 		ath12k_warn(ar->ab,
628 			    "unable to access bss link conf for link %u required to retrieve transmitting link conf\n",
629 			    arvif->link_id);
630 		return NULL;
631 	}
632 	if (link_conf->vif->type == NL80211_IFTYPE_STATION) {
633 		if (link_conf->nontransmitted)
634 			return link_conf->transmitter_bssid;
635 	} else {
636 		tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
637 		if (tx_arvif)
638 			return tx_arvif->bssid;
639 	}
640 
641 	return NULL;
642 }
643 
644 struct ieee80211_bss_conf *
ath12k_mac_get_link_bss_conf(struct ath12k_link_vif * arvif)645 ath12k_mac_get_link_bss_conf(struct ath12k_link_vif *arvif)
646 {
647 	struct ieee80211_vif *vif = arvif->ahvif->vif;
648 	struct ieee80211_bss_conf *link_conf;
649 	struct ath12k *ar = arvif->ar;
650 
651 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
652 
653 	if (arvif->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
654 		return NULL;
655 
656 	link_conf = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
657 				      vif->link_conf[arvif->link_id]);
658 
659 	return link_conf;
660 }
661 
ath12k_mac_get_link_sta(struct ath12k_link_sta * arsta)662 static struct ieee80211_link_sta *ath12k_mac_get_link_sta(struct ath12k_link_sta *arsta)
663 {
664 	struct ath12k_sta *ahsta = arsta->ahsta;
665 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
666 	struct ieee80211_link_sta *link_sta;
667 
668 	lockdep_assert_wiphy(ahsta->ahvif->ah->hw->wiphy);
669 
670 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
671 		return NULL;
672 
673 	link_sta = wiphy_dereference(ahsta->ahvif->ah->hw->wiphy,
674 				     sta->link[arsta->link_id]);
675 
676 	return link_sta;
677 }
678 
ath12k_mac_bitrate_is_cck(int bitrate)679 static bool ath12k_mac_bitrate_is_cck(int bitrate)
680 {
681 	switch (bitrate) {
682 	case 10:
683 	case 20:
684 	case 55:
685 	case 110:
686 		return true;
687 	}
688 
689 	return false;
690 }
691 
ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band * sband,u8 hw_rate,bool cck)692 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
693 			     u8 hw_rate, bool cck)
694 {
695 	const struct ieee80211_rate *rate;
696 	int i;
697 
698 	for (i = 0; i < sband->n_bitrates; i++) {
699 		rate = &sband->bitrates[i];
700 
701 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
702 			continue;
703 
704 		if (rate->hw_value == hw_rate)
705 			return i;
706 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
707 			 rate->hw_value_short == hw_rate)
708 			return i;
709 	}
710 
711 	return 0;
712 }
713 
ath12k_mac_bitrate_to_rate(int bitrate)714 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
715 {
716 	return DIV_ROUND_UP(bitrate, 5) |
717 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
718 }
719 
ath12k_get_arvif_iter(void * data,u8 * mac,struct ieee80211_vif * vif)720 static void ath12k_get_arvif_iter(void *data, u8 *mac,
721 				  struct ieee80211_vif *vif)
722 {
723 	struct ath12k_vif_iter *arvif_iter = data;
724 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
725 	unsigned long links_map = ahvif->links_map;
726 	struct ath12k_link_vif *arvif;
727 	u8 link_id;
728 
729 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
730 		arvif = rcu_dereference(ahvif->link[link_id]);
731 
732 		if (WARN_ON(!arvif))
733 			continue;
734 
735 		if (!arvif->is_created)
736 			continue;
737 
738 		if (arvif->vdev_id == arvif_iter->vdev_id &&
739 		    arvif->ar == arvif_iter->ar) {
740 			arvif_iter->arvif = arvif;
741 			break;
742 		}
743 	}
744 }
745 
ath12k_mac_get_arvif(struct ath12k * ar,u32 vdev_id)746 struct ath12k_link_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
747 {
748 	struct ath12k_vif_iter arvif_iter = {};
749 	u32 flags;
750 
751 	/* To use the arvif returned, caller must have held rcu read lock.
752 	 */
753 	WARN_ON(!rcu_read_lock_any_held());
754 	arvif_iter.vdev_id = vdev_id;
755 	arvif_iter.ar = ar;
756 
757 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
758 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
759 						   flags,
760 						   ath12k_get_arvif_iter,
761 						   &arvif_iter);
762 	if (!arvif_iter.arvif) {
763 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
764 		return NULL;
765 	}
766 
767 	return arvif_iter.arvif;
768 }
769 
ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base * ab,u32 vdev_id)770 struct ath12k_link_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
771 							u32 vdev_id)
772 {
773 	int i;
774 	struct ath12k_pdev *pdev;
775 	struct ath12k_link_vif *arvif;
776 
777 	for (i = 0; i < ab->num_radios; i++) {
778 		pdev = rcu_dereference(ab->pdevs_active[i]);
779 		if (pdev && pdev->ar &&
780 		    (pdev->ar->allocated_vdev_map & (1LL << vdev_id))) {
781 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
782 			if (arvif)
783 				return arvif;
784 		}
785 	}
786 
787 	return NULL;
788 }
789 
ath12k_mac_get_ar_by_vdev_id(struct ath12k_base * ab,u32 vdev_id)790 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
791 {
792 	int i;
793 	struct ath12k_pdev *pdev;
794 
795 	for (i = 0; i < ab->num_radios; i++) {
796 		pdev = rcu_dereference(ab->pdevs_active[i]);
797 		if (pdev && pdev->ar) {
798 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
799 				return pdev->ar;
800 		}
801 	}
802 
803 	return NULL;
804 }
805 
ath12k_mac_get_ar_by_pdev_id(struct ath12k_base * ab,u32 pdev_id)806 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
807 {
808 	int i;
809 	struct ath12k_pdev *pdev;
810 
811 	if (ab->hw_params->single_pdev_only) {
812 		pdev = rcu_dereference(ab->pdevs_active[0]);
813 		return pdev ? pdev->ar : NULL;
814 	}
815 
816 	if (WARN_ON(pdev_id > ab->num_radios))
817 		return NULL;
818 
819 	for (i = 0; i < ab->num_radios; i++) {
820 		if (ab->fw_mode == ATH12K_FIRMWARE_MODE_FTM)
821 			pdev = &ab->pdevs[i];
822 		else
823 			pdev = rcu_dereference(ab->pdevs_active[i]);
824 
825 		if (pdev && pdev->pdev_id == pdev_id)
826 			return (pdev->ar ? pdev->ar : NULL);
827 	}
828 
829 	return NULL;
830 }
831 
ath12k_mac_is_ml_arvif(struct ath12k_link_vif * arvif)832 static bool ath12k_mac_is_ml_arvif(struct ath12k_link_vif *arvif)
833 {
834 	struct ath12k_vif *ahvif = arvif->ahvif;
835 
836 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
837 
838 	if (ahvif->vif->valid_links & BIT(arvif->link_id))
839 		return true;
840 
841 	return false;
842 }
843 
ath12k_mac_get_ar_by_chan(struct ieee80211_hw * hw,struct ieee80211_channel * channel)844 static struct ath12k *ath12k_mac_get_ar_by_chan(struct ieee80211_hw *hw,
845 						struct ieee80211_channel *channel)
846 {
847 	struct ath12k_hw *ah = hw->priv;
848 	struct ath12k *ar;
849 	int i;
850 
851 	ar = ah->radio;
852 
853 	if (ah->num_radio == 1)
854 		return ar;
855 
856 	for_each_ar(ah, ar, i) {
857 		if (channel->center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
858 		    channel->center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
859 			return ar;
860 	}
861 	return NULL;
862 }
863 
ath12k_get_ar_by_ctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)864 static struct ath12k *ath12k_get_ar_by_ctx(struct ieee80211_hw *hw,
865 					   struct ieee80211_chanctx_conf *ctx)
866 {
867 	if (!ctx)
868 		return NULL;
869 
870 	return ath12k_mac_get_ar_by_chan(hw, ctx->def.chan);
871 }
872 
ath12k_get_ar_by_vif(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 link_id)873 struct ath12k *ath12k_get_ar_by_vif(struct ieee80211_hw *hw,
874 				    struct ieee80211_vif *vif,
875 				    u8 link_id)
876 {
877 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
878 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
879 	struct ath12k_link_vif *arvif;
880 
881 	lockdep_assert_wiphy(hw->wiphy);
882 
883 	/* If there is one pdev within ah, then we return
884 	 * ar directly.
885 	 */
886 	if (ah->num_radio == 1)
887 		return ah->radio;
888 
889 	if (!(ahvif->links_map & BIT(link_id)))
890 		return NULL;
891 
892 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
893 	if (arvif && arvif->is_created)
894 		return arvif->ar;
895 
896 	return NULL;
897 }
898 
ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf,void * data)899 void ath12k_mac_get_any_chanctx_conf_iter(struct ieee80211_hw *hw,
900 					  struct ieee80211_chanctx_conf *conf,
901 					  void *data)
902 {
903 	struct ath12k_mac_get_any_chanctx_conf_arg *arg = data;
904 	struct ath12k *ctx_ar = ath12k_get_ar_by_ctx(hw, conf);
905 
906 	if (ctx_ar == arg->ar)
907 		arg->chanctx_conf = conf;
908 }
909 
ath12k_mac_get_vif_up(struct ath12k * ar)910 static struct ath12k_link_vif *ath12k_mac_get_vif_up(struct ath12k *ar)
911 {
912 	struct ath12k_link_vif *arvif;
913 
914 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
915 
916 	list_for_each_entry(arvif, &ar->arvifs, list) {
917 		if (arvif->is_up)
918 			return arvif;
919 	}
920 
921 	return NULL;
922 }
923 
ath12k_mac_band_match(enum nl80211_band band1,enum WMI_HOST_WLAN_BAND band2)924 static bool ath12k_mac_band_match(enum nl80211_band band1, enum WMI_HOST_WLAN_BAND band2)
925 {
926 	switch (band1) {
927 	case NL80211_BAND_2GHZ:
928 		if (band2 & WMI_HOST_WLAN_2GHZ_CAP)
929 			return true;
930 		break;
931 	case NL80211_BAND_5GHZ:
932 	case NL80211_BAND_6GHZ:
933 		if (band2 & WMI_HOST_WLAN_5GHZ_CAP)
934 			return true;
935 		break;
936 	default:
937 		return false;
938 	}
939 
940 	return false;
941 }
942 
ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif * arvif)943 static u8 ath12k_mac_get_target_pdev_id_from_vif(struct ath12k_link_vif *arvif)
944 {
945 	struct ath12k *ar = arvif->ar;
946 	struct ath12k_base *ab = ar->ab;
947 	struct ieee80211_vif *vif = arvif->ahvif->vif;
948 	struct cfg80211_chan_def def;
949 	enum nl80211_band band;
950 	u8 pdev_id = ab->fw_pdev[0].pdev_id;
951 	int i;
952 
953 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
954 		return pdev_id;
955 
956 	band = def.chan->band;
957 
958 	for (i = 0; i < ab->fw_pdev_count; i++) {
959 		if (ath12k_mac_band_match(band, ab->fw_pdev[i].supported_bands))
960 			return ab->fw_pdev[i].pdev_id;
961 	}
962 
963 	return pdev_id;
964 }
965 
ath12k_mac_get_target_pdev_id(struct ath12k * ar)966 u8 ath12k_mac_get_target_pdev_id(struct ath12k *ar)
967 {
968 	struct ath12k_link_vif *arvif;
969 	struct ath12k_base *ab = ar->ab;
970 
971 	if (!ab->hw_params->single_pdev_only)
972 		return ar->pdev->pdev_id;
973 
974 	arvif = ath12k_mac_get_vif_up(ar);
975 
976 	/* fw_pdev array has pdev ids derived from phy capability
977 	 * service ready event (pdev_and_hw_link_ids).
978 	 * If no vif is active, return default first index.
979 	 */
980 	if (!arvif)
981 		return ar->ab->fw_pdev[0].pdev_id;
982 
983 	/* If active vif is found, return the pdev id matching chandef band */
984 	return ath12k_mac_get_target_pdev_id_from_vif(arvif);
985 }
986 
ath12k_pdev_caps_update(struct ath12k * ar)987 static void ath12k_pdev_caps_update(struct ath12k *ar)
988 {
989 	struct ath12k_base *ab = ar->ab;
990 
991 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
992 
993 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
994 	 * But since the received value in svcrdy is same as hw_max_tx_power,
995 	 * we can set ar->min_tx_power to 0 currently until
996 	 * this is fixed in firmware
997 	 */
998 	ar->min_tx_power = 0;
999 
1000 	ar->txpower_limit_2g = ar->max_tx_power;
1001 	ar->txpower_limit_5g = ar->max_tx_power;
1002 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
1003 }
1004 
ath12k_mac_txpower_recalc(struct ath12k * ar)1005 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
1006 {
1007 	struct ath12k_pdev *pdev = ar->pdev;
1008 	struct ath12k_link_vif *arvif;
1009 	int ret, txpower = -1;
1010 	u32 param;
1011 
1012 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1013 
1014 	list_for_each_entry(arvif, &ar->arvifs, list) {
1015 		if (arvif->txpower <= 0)
1016 			continue;
1017 
1018 		if (txpower == -1)
1019 			txpower = arvif->txpower;
1020 		else
1021 			txpower = min(txpower, arvif->txpower);
1022 	}
1023 
1024 	if (txpower == -1)
1025 		return 0;
1026 
1027 	/* txpwr is set as 2 units per dBm in FW*/
1028 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
1029 			ar->max_tx_power) * 2;
1030 
1031 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
1032 		   txpower / 2);
1033 
1034 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) &&
1035 	    ar->txpower_limit_2g != txpower) {
1036 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
1037 		ret = ath12k_wmi_pdev_set_param(ar, param,
1038 						txpower, ar->pdev->pdev_id);
1039 		if (ret)
1040 			goto fail;
1041 		ar->txpower_limit_2g = txpower;
1042 	}
1043 
1044 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) &&
1045 	    ar->txpower_limit_5g != txpower) {
1046 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
1047 		ret = ath12k_wmi_pdev_set_param(ar, param,
1048 						txpower, ar->pdev->pdev_id);
1049 		if (ret)
1050 			goto fail;
1051 		ar->txpower_limit_5g = txpower;
1052 	}
1053 
1054 	return 0;
1055 
1056 fail:
1057 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
1058 		    txpower / 2, param, ret);
1059 	return ret;
1060 }
1061 
ath12k_recalc_rtscts_prot(struct ath12k_link_vif * arvif)1062 static int ath12k_recalc_rtscts_prot(struct ath12k_link_vif *arvif)
1063 {
1064 	struct ath12k *ar = arvif->ar;
1065 	u32 vdev_param, rts_cts;
1066 	int ret;
1067 
1068 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1069 
1070 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
1071 
1072 	/* Enable RTS/CTS protection for sw retries (when legacy stations
1073 	 * are in BSS) or by default only for second rate series.
1074 	 * TODO: Check if we need to enable CTS 2 Self in any case
1075 	 */
1076 	rts_cts = WMI_USE_RTS_CTS;
1077 
1078 	if (arvif->num_legacy_stations > 0)
1079 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
1080 	else
1081 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
1082 
1083 	/* Need not send duplicate param value to firmware */
1084 	if (arvif->rtscts_prot_mode == rts_cts)
1085 		return 0;
1086 
1087 	arvif->rtscts_prot_mode = rts_cts;
1088 
1089 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1090 		   arvif->vdev_id, rts_cts);
1091 
1092 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1093 					    vdev_param, rts_cts);
1094 	if (ret)
1095 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1096 			    arvif->vdev_id, ret);
1097 
1098 	return ret;
1099 }
1100 
ath12k_mac_set_kickout(struct ath12k_link_vif * arvif)1101 static int ath12k_mac_set_kickout(struct ath12k_link_vif *arvif)
1102 {
1103 	struct ath12k *ar = arvif->ar;
1104 	u32 param;
1105 	int ret;
1106 
1107 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
1108 					ATH12K_KICKOUT_THRESHOLD,
1109 					ar->pdev->pdev_id);
1110 	if (ret) {
1111 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
1112 			    arvif->vdev_id, ret);
1113 		return ret;
1114 	}
1115 
1116 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
1117 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1118 					    ATH12K_KEEPALIVE_MIN_IDLE);
1119 	if (ret) {
1120 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
1121 			    arvif->vdev_id, ret);
1122 		return ret;
1123 	}
1124 
1125 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
1126 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1127 					    ATH12K_KEEPALIVE_MAX_IDLE);
1128 	if (ret) {
1129 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
1130 			    arvif->vdev_id, ret);
1131 		return ret;
1132 	}
1133 
1134 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
1135 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
1136 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
1137 	if (ret) {
1138 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
1139 			    arvif->vdev_id, ret);
1140 		return ret;
1141 	}
1142 
1143 	return 0;
1144 }
1145 
ath12k_mac_peer_cleanup_all(struct ath12k * ar)1146 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
1147 {
1148 	struct ath12k_peer *peer, *tmp;
1149 	struct ath12k_base *ab = ar->ab;
1150 
1151 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1152 
1153 	spin_lock_bh(&ab->base_lock);
1154 	list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
1155 		/* Skip Rx TID cleanup for self peer */
1156 		if (peer->sta)
1157 			ath12k_dp_rx_peer_tid_cleanup(ar, peer);
1158 
1159 		list_del(&peer->list);
1160 		kfree(peer);
1161 	}
1162 	spin_unlock_bh(&ab->base_lock);
1163 
1164 	ar->num_peers = 0;
1165 	ar->num_stations = 0;
1166 }
1167 
ath12k_mac_vdev_setup_sync(struct ath12k * ar)1168 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
1169 {
1170 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1171 
1172 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
1173 		return -ESHUTDOWN;
1174 
1175 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
1176 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
1177 
1178 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
1179 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
1180 		return -ETIMEDOUT;
1181 
1182 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
1183 }
1184 
ath12k_monitor_vdev_up(struct ath12k * ar,int vdev_id)1185 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
1186 {
1187 	struct ath12k_wmi_vdev_up_params params = {};
1188 	int ret;
1189 
1190 	params.vdev_id = vdev_id;
1191 	params.bssid = ar->mac_addr;
1192 	ret = ath12k_wmi_vdev_up(ar, &params);
1193 	if (ret) {
1194 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1195 			    vdev_id, ret);
1196 		return ret;
1197 	}
1198 
1199 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1200 		   vdev_id);
1201 	return 0;
1202 }
1203 
ath12k_mac_monitor_vdev_start(struct ath12k * ar,int vdev_id,struct cfg80211_chan_def * chandef)1204 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
1205 					 struct cfg80211_chan_def *chandef)
1206 {
1207 	struct ieee80211_channel *channel;
1208 	struct wmi_vdev_start_req_arg arg = {};
1209 	struct ath12k_wmi_vdev_up_params params = {};
1210 	int ret;
1211 
1212 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1213 
1214 	channel = chandef->chan;
1215 	arg.vdev_id = vdev_id;
1216 	arg.freq = channel->center_freq;
1217 	arg.band_center_freq1 = chandef->center_freq1;
1218 	arg.band_center_freq2 = chandef->center_freq2;
1219 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
1220 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
1221 
1222 	arg.min_power = 0;
1223 	arg.max_power = channel->max_power;
1224 	arg.max_reg_power = channel->max_reg_power;
1225 	arg.max_antenna_gain = channel->max_antenna_gain;
1226 
1227 	arg.pref_tx_streams = ar->num_tx_chains;
1228 	arg.pref_rx_streams = ar->num_rx_chains;
1229 	arg.punct_bitmap = 0xFFFFFFFF;
1230 
1231 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
1232 
1233 	reinit_completion(&ar->vdev_setup_done);
1234 	reinit_completion(&ar->vdev_delete_done);
1235 
1236 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
1237 	if (ret) {
1238 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
1239 			    vdev_id, ret);
1240 		return ret;
1241 	}
1242 
1243 	ret = ath12k_mac_vdev_setup_sync(ar);
1244 	if (ret) {
1245 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
1246 			    vdev_id, ret);
1247 		return ret;
1248 	}
1249 
1250 	params.vdev_id = vdev_id;
1251 	params.bssid = ar->mac_addr;
1252 	ret = ath12k_wmi_vdev_up(ar, &params);
1253 	if (ret) {
1254 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
1255 			    vdev_id, ret);
1256 		goto vdev_stop;
1257 	}
1258 
1259 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
1260 		   vdev_id);
1261 	return 0;
1262 
1263 vdev_stop:
1264 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
1265 	if (ret)
1266 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
1267 			    vdev_id, ret);
1268 	return ret;
1269 }
1270 
ath12k_mac_monitor_vdev_stop(struct ath12k * ar)1271 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
1272 {
1273 	int ret;
1274 
1275 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1276 
1277 	reinit_completion(&ar->vdev_setup_done);
1278 
1279 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1280 	if (ret)
1281 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
1282 			    ar->monitor_vdev_id, ret);
1283 
1284 	ret = ath12k_mac_vdev_setup_sync(ar);
1285 	if (ret)
1286 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
1287 			    ar->monitor_vdev_id, ret);
1288 
1289 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1290 	if (ret)
1291 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
1292 			    ar->monitor_vdev_id, ret);
1293 
1294 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
1295 		   ar->monitor_vdev_id);
1296 	return ret;
1297 }
1298 
ath12k_mac_monitor_vdev_delete(struct ath12k * ar)1299 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
1300 {
1301 	int ret;
1302 	unsigned long time_left;
1303 
1304 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1305 
1306 	if (!ar->monitor_vdev_created)
1307 		return 0;
1308 
1309 	reinit_completion(&ar->vdev_delete_done);
1310 
1311 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1312 	if (ret) {
1313 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
1314 			    ar->monitor_vdev_id, ret);
1315 		return ret;
1316 	}
1317 
1318 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1319 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1320 	if (time_left == 0) {
1321 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1322 	} else {
1323 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1324 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1325 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1326 			   ar->monitor_vdev_id);
1327 		ar->num_created_vdevs--;
1328 		ar->monitor_vdev_id = -1;
1329 		ar->monitor_vdev_created = false;
1330 	}
1331 
1332 	return ret;
1333 }
1334 
ath12k_mac_monitor_start(struct ath12k * ar)1335 static int ath12k_mac_monitor_start(struct ath12k *ar)
1336 {
1337 	struct ath12k_mac_get_any_chanctx_conf_arg arg;
1338 	int ret;
1339 
1340 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1341 
1342 	if (ar->monitor_started)
1343 		return 0;
1344 
1345 	arg.ar = ar;
1346 	arg.chanctx_conf = NULL;
1347 	ieee80211_iter_chan_contexts_atomic(ath12k_ar_to_hw(ar),
1348 					    ath12k_mac_get_any_chanctx_conf_iter,
1349 					    &arg);
1350 	if (!arg.chanctx_conf)
1351 		return 0;
1352 
1353 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id,
1354 					    &arg.chanctx_conf->def);
1355 	if (ret) {
1356 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1357 		return ret;
1358 	}
1359 
1360 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1361 	if (ret) {
1362 		ath12k_warn(ar->ab, "fail to set monitor filter: %d\n", ret);
1363 		return ret;
1364 	}
1365 
1366 	ar->monitor_started = true;
1367 	ar->num_started_vdevs++;
1368 
1369 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started\n");
1370 
1371 	return 0;
1372 }
1373 
ath12k_mac_monitor_stop(struct ath12k * ar)1374 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1375 {
1376 	int ret;
1377 
1378 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1379 
1380 	if (!ar->monitor_started)
1381 		return 0;
1382 
1383 	ret = ath12k_mac_monitor_vdev_stop(ar);
1384 	if (ret) {
1385 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1386 		return ret;
1387 	}
1388 
1389 	ar->monitor_started = false;
1390 	ar->num_started_vdevs--;
1391 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1392 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1393 	return ret;
1394 }
1395 
ath12k_mac_vdev_stop(struct ath12k_link_vif * arvif)1396 int ath12k_mac_vdev_stop(struct ath12k_link_vif *arvif)
1397 {
1398 	struct ath12k_vif *ahvif = arvif->ahvif;
1399 	struct ath12k *ar = arvif->ar;
1400 	int ret;
1401 
1402 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
1403 
1404 	reinit_completion(&ar->vdev_setup_done);
1405 
1406 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
1407 	if (ret) {
1408 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
1409 			    arvif->vdev_id, ret);
1410 		goto err;
1411 	}
1412 
1413 	ret = ath12k_mac_vdev_setup_sync(ar);
1414 	if (ret) {
1415 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
1416 			    arvif->vdev_id, ret);
1417 		goto err;
1418 	}
1419 
1420 	WARN_ON(ar->num_started_vdevs == 0);
1421 
1422 	ar->num_started_vdevs--;
1423 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
1424 		   ahvif->vif->addr, arvif->vdev_id);
1425 
1426 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags)) {
1427 		clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
1428 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
1429 			   arvif->vdev_id);
1430 	}
1431 
1432 	return 0;
1433 err:
1434 	return ret;
1435 }
1436 
ath12k_mac_op_config(struct ieee80211_hw * hw,int radio_idx,u32 changed)1437 static int ath12k_mac_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
1438 {
1439 	return 0;
1440 }
1441 
ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif * arvif,struct sk_buff * bcn)1442 static int ath12k_mac_setup_bcn_p2p_ie(struct ath12k_link_vif *arvif,
1443 				       struct sk_buff *bcn)
1444 {
1445 	struct ath12k *ar = arvif->ar;
1446 	struct ieee80211_mgmt *mgmt;
1447 	const u8 *p2p_ie;
1448 	int ret;
1449 
1450 	mgmt = (void *)bcn->data;
1451 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1452 					 mgmt->u.beacon.variable,
1453 					 bcn->len - (mgmt->u.beacon.variable -
1454 						     bcn->data));
1455 	if (!p2p_ie) {
1456 		ath12k_warn(ar->ab, "no P2P ie found in beacon\n");
1457 		return -ENOENT;
1458 	}
1459 
1460 	ret = ath12k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1461 	if (ret) {
1462 		ath12k_warn(ar->ab, "failed to submit P2P GO bcn ie for vdev %i: %d\n",
1463 			    arvif->vdev_id, ret);
1464 		return ret;
1465 	}
1466 
1467 	return 0;
1468 }
1469 
ath12k_mac_remove_vendor_ie(struct sk_buff * skb,unsigned int oui,u8 oui_type,size_t ie_offset)1470 static int ath12k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1471 				       u8 oui_type, size_t ie_offset)
1472 {
1473 	const u8 *next, *end;
1474 	size_t len;
1475 	u8 *ie;
1476 
1477 	if (WARN_ON(skb->len < ie_offset))
1478 		return -EINVAL;
1479 
1480 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1481 					   skb->data + ie_offset,
1482 					   skb->len - ie_offset);
1483 	if (!ie)
1484 		return -ENOENT;
1485 
1486 	len = ie[1] + 2;
1487 	end = skb->data + skb->len;
1488 	next = ie + len;
1489 
1490 	if (WARN_ON(next > end))
1491 		return -EINVAL;
1492 
1493 	memmove(ie, next, end - next);
1494 	skb_trim(skb, skb->len - len);
1495 
1496 	return 0;
1497 }
1498 
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)1499 static void ath12k_mac_set_arvif_ies(struct ath12k_link_vif *arvif,
1500 				     struct ath12k_link_vif *tx_arvif,
1501 				     struct sk_buff *bcn,
1502 				     u8 bssid_index, bool *nontx_profile_found)
1503 {
1504 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)bcn->data;
1505 	const struct element *elem, *nontx, *index, *nie, *ext_cap_ie;
1506 	const u8 *start, *tail;
1507 	u16 rem_len;
1508 	u8 i;
1509 
1510 	start = bcn->data + ieee80211_get_hdrlen_from_skb(bcn) + sizeof(mgmt->u.beacon);
1511 	tail = skb_tail_pointer(bcn);
1512 	rem_len = tail - start;
1513 
1514 	arvif->rsnie_present = false;
1515 	arvif->wpaie_present = false;
1516 
1517 	if (cfg80211_find_ie(WLAN_EID_RSN, start, rem_len))
1518 		arvif->rsnie_present = true;
1519 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT, WLAN_OUI_TYPE_MICROSOFT_WPA,
1520 				    start, rem_len))
1521 		arvif->wpaie_present = true;
1522 
1523 	ext_cap_ie = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, start, rem_len);
1524 	if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1525 	    (ext_cap_ie->data[10] & WLAN_EXT_CAPA11_BCN_PROTECT))
1526 		tx_arvif->beacon_prot = true;
1527 
1528 	/* Return from here for the transmitted profile */
1529 	if (!bssid_index)
1530 		return;
1531 
1532 	/* Initial rsnie_present for the nontransmitted profile is set to be same as that
1533 	 * of the transmitted profile. It will be changed if security configurations are
1534 	 * different.
1535 	 */
1536 	*nontx_profile_found = false;
1537 	for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, rem_len) {
1538 		/* Fixed minimum MBSSID element length with at least one
1539 		 * nontransmitted BSSID profile is 12 bytes as given below;
1540 		 * 1 (max BSSID indicator) +
1541 		 * 2 (Nontransmitted BSSID profile: Subelement ID + length) +
1542 		 * 4 (Nontransmitted BSSID Capabilities: tag + length + info)
1543 		 * 2 (Nontransmitted BSSID SSID: tag + length)
1544 		 * 3 (Nontransmitted BSSID Index: tag + length + BSSID index
1545 		 */
1546 		if (elem->datalen < 12 || elem->data[0] < 1)
1547 			continue; /* Max BSSID indicator must be >=1 */
1548 
1549 		for_each_element(nontx, elem->data + 1, elem->datalen - 1) {
1550 			start = nontx->data;
1551 
1552 			if (nontx->id != 0 || nontx->datalen < 4)
1553 				continue; /* Invalid nontransmitted profile */
1554 
1555 			if (nontx->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
1556 			    nontx->data[1] != 2) {
1557 				continue; /* Missing nontransmitted BSS capabilities */
1558 			}
1559 
1560 			if (nontx->data[4] != WLAN_EID_SSID)
1561 				continue; /* Missing SSID for nontransmitted BSS */
1562 
1563 			index = cfg80211_find_elem(WLAN_EID_MULTI_BSSID_IDX,
1564 						   start, nontx->datalen);
1565 			if (!index || index->datalen < 1 || index->data[0] == 0)
1566 				continue; /* Invalid MBSSID Index element */
1567 
1568 			if (index->data[0] == bssid_index) {
1569 				*nontx_profile_found = true;
1570 
1571 				/* Check if nontx BSS has beacon protection enabled */
1572 				if (!tx_arvif->beacon_prot) {
1573 					ext_cap_ie =
1574 					    cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY,
1575 							       nontx->data,
1576 							       nontx->datalen);
1577 					if (ext_cap_ie && ext_cap_ie->datalen >= 11 &&
1578 					    (ext_cap_ie->data[10] &
1579 					     WLAN_EXT_CAPA11_BCN_PROTECT))
1580 						tx_arvif->beacon_prot = true;
1581 				}
1582 
1583 				if (cfg80211_find_ie(WLAN_EID_RSN,
1584 						     nontx->data,
1585 						     nontx->datalen)) {
1586 					arvif->rsnie_present = true;
1587 					return;
1588 				} else if (!arvif->rsnie_present) {
1589 					return; /* Both tx and nontx BSS are open */
1590 				}
1591 
1592 				nie = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
1593 							     nontx->data,
1594 							     nontx->datalen);
1595 				if (!nie || nie->datalen < 2)
1596 					return; /* Invalid non-inheritance element */
1597 
1598 				for (i = 1; i < nie->datalen - 1; i++) {
1599 					if (nie->data[i] == WLAN_EID_RSN) {
1600 						arvif->rsnie_present = false;
1601 						break;
1602 					}
1603 				}
1604 
1605 				return;
1606 			}
1607 		}
1608 	}
1609 }
1610 
ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif * arvif,struct ath12k_link_vif * tx_arvif,u8 bssid_index)1611 static int ath12k_mac_setup_bcn_tmpl_ema(struct ath12k_link_vif *arvif,
1612 					 struct ath12k_link_vif *tx_arvif,
1613 					 u8 bssid_index)
1614 {
1615 	struct ath12k_wmi_bcn_tmpl_ema_arg ema_args;
1616 	struct ieee80211_ema_beacons *beacons;
1617 	bool nontx_profile_found = false;
1618 	int ret = 0;
1619 	u8 i;
1620 
1621 	beacons = ieee80211_beacon_get_template_ema_list(ath12k_ar_to_hw(tx_arvif->ar),
1622 							 tx_arvif->ahvif->vif,
1623 							 tx_arvif->link_id);
1624 	if (!beacons || !beacons->cnt) {
1625 		ath12k_warn(arvif->ar->ab,
1626 			    "failed to get ema beacon templates from mac80211\n");
1627 		return -EPERM;
1628 	}
1629 
1630 	if (tx_arvif == arvif)
1631 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[0].skb, 0, NULL);
1632 
1633 	for (i = 0; i < beacons->cnt; i++) {
1634 		if (tx_arvif != arvif && !nontx_profile_found)
1635 			ath12k_mac_set_arvif_ies(arvif, tx_arvif, beacons->bcn[i].skb,
1636 						 bssid_index,
1637 						 &nontx_profile_found);
1638 
1639 		ema_args.bcn_cnt = beacons->cnt;
1640 		ema_args.bcn_index = i;
1641 		ret = ath12k_wmi_bcn_tmpl(tx_arvif, &beacons->bcn[i].offs,
1642 					  beacons->bcn[i].skb, &ema_args);
1643 		if (ret) {
1644 			ath12k_warn(tx_arvif->ar->ab,
1645 				    "failed to set ema beacon template id %i error %d\n",
1646 				    i, ret);
1647 			break;
1648 		}
1649 	}
1650 
1651 	if (tx_arvif != arvif && !nontx_profile_found)
1652 		ath12k_warn(arvif->ar->ab,
1653 			    "nontransmitted bssid index %u not found in beacon template\n",
1654 			    bssid_index);
1655 
1656 	ieee80211_beacon_free_ema_list(beacons);
1657 	return ret;
1658 }
1659 
ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif * arvif)1660 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_link_vif *arvif)
1661 {
1662 	struct ath12k_vif *ahvif = arvif->ahvif;
1663 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
1664 	struct ieee80211_bss_conf *link_conf;
1665 	struct ath12k_link_vif *tx_arvif;
1666 	struct ath12k *ar = arvif->ar;
1667 	struct ath12k_base *ab = ar->ab;
1668 	struct ieee80211_mutable_offsets offs = {};
1669 	bool nontx_profile_found = false;
1670 	struct sk_buff *bcn;
1671 	int ret;
1672 
1673 	if (ahvif->vdev_type != WMI_VDEV_TYPE_AP)
1674 		return 0;
1675 
1676 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
1677 	if (!link_conf) {
1678 		ath12k_warn(ar->ab, "unable to access bss link conf to set bcn tmpl for vif %pM link %u\n",
1679 			    vif->addr, arvif->link_id);
1680 		return -ENOLINK;
1681 	}
1682 
1683 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
1684 	if (tx_arvif) {
1685 		if (tx_arvif != arvif && arvif->is_up)
1686 			return 0;
1687 
1688 		if (link_conf->ema_ap)
1689 			return ath12k_mac_setup_bcn_tmpl_ema(arvif, tx_arvif,
1690 							     link_conf->bssid_index);
1691 	} else {
1692 		tx_arvif = arvif;
1693 	}
1694 
1695 	bcn = ieee80211_beacon_get_template(ath12k_ar_to_hw(tx_arvif->ar),
1696 					    tx_arvif->ahvif->vif,
1697 					    &offs, tx_arvif->link_id);
1698 	if (!bcn) {
1699 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1700 		return -EPERM;
1701 	}
1702 
1703 	if (tx_arvif == arvif) {
1704 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn, 0, NULL);
1705 	} else {
1706 		ath12k_mac_set_arvif_ies(arvif, tx_arvif, bcn,
1707 					 link_conf->bssid_index,
1708 					 &nontx_profile_found);
1709 		if (!nontx_profile_found)
1710 			ath12k_warn(ab,
1711 				    "nontransmitted profile not found in beacon template\n");
1712 	}
1713 
1714 	if (ahvif->vif->type == NL80211_IFTYPE_AP && ahvif->vif->p2p) {
1715 		ret = ath12k_mac_setup_bcn_p2p_ie(arvif, bcn);
1716 		if (ret) {
1717 			ath12k_warn(ab, "failed to setup P2P GO bcn ie: %d\n",
1718 				    ret);
1719 			goto free_bcn_skb;
1720 		}
1721 
1722 		/* P2P IE is inserted by firmware automatically (as
1723 		 * configured above) so remove it from the base beacon
1724 		 * template to avoid duplicate P2P IEs in beacon frames.
1725 		 */
1726 		ret = ath12k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA,
1727 						  WLAN_OUI_TYPE_WFA_P2P,
1728 						  offsetof(struct ieee80211_mgmt,
1729 							   u.beacon.variable));
1730 		if (ret) {
1731 			ath12k_warn(ab, "failed to remove P2P vendor ie: %d\n",
1732 				    ret);
1733 			goto free_bcn_skb;
1734 		}
1735 	}
1736 
1737 	ret = ath12k_wmi_bcn_tmpl(arvif, &offs, bcn, NULL);
1738 
1739 	if (ret)
1740 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1741 			    ret);
1742 
1743 free_bcn_skb:
1744 	kfree_skb(bcn);
1745 	return ret;
1746 }
1747 
ath12k_control_beaconing(struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info)1748 static void ath12k_control_beaconing(struct ath12k_link_vif *arvif,
1749 				     struct ieee80211_bss_conf *info)
1750 {
1751 	struct ath12k_wmi_vdev_up_params params = {};
1752 	struct ath12k_vif *ahvif = arvif->ahvif;
1753 	struct ath12k *ar = arvif->ar;
1754 	int ret;
1755 
1756 	lockdep_assert_wiphy(ath12k_ar_to_hw(arvif->ar)->wiphy);
1757 
1758 	if (!info->enable_beacon) {
1759 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1760 		if (ret)
1761 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1762 				    arvif->vdev_id, ret);
1763 
1764 		arvif->is_up = false;
1765 		return;
1766 	}
1767 
1768 	/* Install the beacon template to the FW */
1769 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1770 	if (ret) {
1771 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1772 			    ret);
1773 		return;
1774 	}
1775 
1776 	ahvif->aid = 0;
1777 
1778 	ether_addr_copy(arvif->bssid, info->addr);
1779 
1780 	params.vdev_id = arvif->vdev_id;
1781 	params.aid = ahvif->aid;
1782 	params.bssid = arvif->bssid;
1783 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
1784 	if (params.tx_bssid) {
1785 		params.nontx_profile_idx = info->bssid_index;
1786 		params.nontx_profile_cnt = 1 << info->bssid_indicator;
1787 	}
1788 	ret = ath12k_wmi_vdev_up(arvif->ar, &params);
1789 	if (ret) {
1790 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1791 			    arvif->vdev_id, ret);
1792 		return;
1793 	}
1794 
1795 	arvif->is_up = true;
1796 
1797 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1798 }
1799 
ath12k_mac_handle_beacon_iter(void * data,u8 * mac,struct ieee80211_vif * vif)1800 static void ath12k_mac_handle_beacon_iter(void *data, u8 *mac,
1801 					  struct ieee80211_vif *vif)
1802 {
1803 	struct sk_buff *skb = data;
1804 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
1805 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1806 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1807 
1808 	if (vif->type != NL80211_IFTYPE_STATION || !arvif->is_created)
1809 		return;
1810 
1811 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1812 		return;
1813 
1814 	cancel_delayed_work(&arvif->connection_loss_work);
1815 }
1816 
ath12k_mac_handle_beacon(struct ath12k * ar,struct sk_buff * skb)1817 void ath12k_mac_handle_beacon(struct ath12k *ar, struct sk_buff *skb)
1818 {
1819 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1820 						   IEEE80211_IFACE_ITER_NORMAL,
1821 						   ath12k_mac_handle_beacon_iter,
1822 						   skb);
1823 }
1824 
ath12k_mac_handle_beacon_miss_iter(void * data,u8 * mac,struct ieee80211_vif * vif)1825 static void ath12k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1826 					       struct ieee80211_vif *vif)
1827 {
1828 	u32 *vdev_id = data;
1829 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
1830 	struct ath12k_link_vif *arvif = &ahvif->deflink;
1831 	struct ieee80211_hw *hw;
1832 
1833 	if (!arvif->is_created || arvif->vdev_id != *vdev_id)
1834 		return;
1835 
1836 	if (!arvif->is_up)
1837 		return;
1838 
1839 	ieee80211_beacon_loss(vif);
1840 	hw = ath12k_ar_to_hw(arvif->ar);
1841 
1842 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
1843 	 * (done by mac80211) succeeds but beacons do not resume then it
1844 	 * doesn't make sense to continue operation. Queue connection loss work
1845 	 * which can be cancelled when beacon is received.
1846 	 */
1847 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1848 				     ATH12K_CONNECTION_LOSS_HZ);
1849 }
1850 
ath12k_mac_handle_beacon_miss(struct ath12k * ar,u32 vdev_id)1851 void ath12k_mac_handle_beacon_miss(struct ath12k *ar, u32 vdev_id)
1852 {
1853 	ieee80211_iterate_active_interfaces_atomic(ath12k_ar_to_hw(ar),
1854 						   IEEE80211_IFACE_ITER_NORMAL,
1855 						   ath12k_mac_handle_beacon_miss_iter,
1856 						   &vdev_id);
1857 }
1858 
ath12k_mac_vif_sta_connection_loss_work(struct work_struct * work)1859 static void ath12k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1860 {
1861 	struct ath12k_link_vif *arvif = container_of(work, struct ath12k_link_vif,
1862 						     connection_loss_work.work);
1863 	struct ieee80211_vif *vif = arvif->ahvif->vif;
1864 
1865 	if (!arvif->is_up)
1866 		return;
1867 
1868 	ieee80211_connection_loss(vif);
1869 }
1870 
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)1871 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
1872 				      struct ath12k_link_vif *arvif,
1873 				      struct ath12k_link_sta *arsta,
1874 				      struct ath12k_wmi_peer_assoc_arg *arg)
1875 {
1876 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1877 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
1878 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1879 	struct ieee80211_bss_conf *bss_conf;
1880 	u32 aid;
1881 
1882 	lockdep_assert_wiphy(hw->wiphy);
1883 
1884 	if (vif->type == NL80211_IFTYPE_STATION)
1885 		aid = vif->cfg.aid;
1886 	else
1887 		aid = sta->aid;
1888 
1889 	ether_addr_copy(arg->peer_mac, arsta->addr);
1890 	arg->vdev_id = arvif->vdev_id;
1891 	arg->peer_associd = aid;
1892 	arg->auth_flag = true;
1893 	/* TODO: STA WAR in ath10k for listen interval required? */
1894 	arg->peer_listen_intval = hw->conf.listen_interval;
1895 	arg->peer_nss = 1;
1896 
1897 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
1898 	if (!bss_conf) {
1899 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc for vif %pM link %u\n",
1900 			    vif->addr, arvif->link_id);
1901 		return;
1902 	}
1903 
1904 	arg->peer_caps = bss_conf->assoc_capability;
1905 }
1906 
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)1907 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
1908 				       struct ath12k_link_vif *arvif,
1909 				       struct ath12k_link_sta *arsta,
1910 				       struct ath12k_wmi_peer_assoc_arg *arg)
1911 {
1912 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1913 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
1914 	struct ieee80211_bss_conf *info;
1915 	struct cfg80211_chan_def def;
1916 	struct cfg80211_bss *bss;
1917 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1918 	const u8 *rsnie = NULL;
1919 	const u8 *wpaie = NULL;
1920 
1921 	lockdep_assert_wiphy(hw->wiphy);
1922 
1923 	info = ath12k_mac_get_link_bss_conf(arvif);
1924 	if (!info) {
1925 		ath12k_warn(ar->ab, "unable to access bss link conf for peer assoc crypto for vif %pM link %u\n",
1926 			    vif->addr, arvif->link_id);
1927 		return;
1928 	}
1929 
1930 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
1931 		return;
1932 
1933 	bss = cfg80211_get_bss(hw->wiphy, def.chan, info->bssid, NULL, 0,
1934 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1935 
1936 	if (arvif->rsnie_present || arvif->wpaie_present) {
1937 		arg->need_ptk_4_way = true;
1938 		if (arvif->wpaie_present)
1939 			arg->need_gtk_2_way = true;
1940 	} else if (bss) {
1941 		const struct cfg80211_bss_ies *ies;
1942 
1943 		rcu_read_lock();
1944 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1945 
1946 		ies = rcu_dereference(bss->ies);
1947 
1948 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1949 						WLAN_OUI_TYPE_MICROSOFT_WPA,
1950 						ies->data,
1951 						ies->len);
1952 		rcu_read_unlock();
1953 		cfg80211_put_bss(hw->wiphy, bss);
1954 	}
1955 
1956 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
1957 	if (rsnie || wpaie) {
1958 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
1959 			   "%s: rsn ie found\n", __func__);
1960 		arg->need_ptk_4_way = true;
1961 	}
1962 
1963 	if (wpaie) {
1964 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
1965 			   "%s: wpa ie found\n", __func__);
1966 		arg->need_gtk_2_way = true;
1967 	}
1968 
1969 	if (sta->mfp) {
1970 		/* TODO: Need to check if FW supports PMF? */
1971 		arg->is_pmf_enabled = true;
1972 	}
1973 
1974 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
1975 }
1976 
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)1977 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
1978 				      struct ath12k_link_vif *arvif,
1979 				      struct ath12k_link_sta *arsta,
1980 				      struct ath12k_wmi_peer_assoc_arg *arg)
1981 {
1982 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
1983 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
1984 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1985 	struct ieee80211_link_sta *link_sta;
1986 	struct cfg80211_chan_def def;
1987 	const struct ieee80211_supported_band *sband;
1988 	const struct ieee80211_rate *rates;
1989 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
1990 	enum nl80211_band band;
1991 	u32 ratemask;
1992 	u8 rate;
1993 	int i;
1994 
1995 	lockdep_assert_wiphy(hw->wiphy);
1996 
1997 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
1998 		return;
1999 
2000 	link_sta = ath12k_mac_get_link_sta(arsta);
2001 	if (!link_sta) {
2002 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc rates for sta %pM link %u\n",
2003 			    sta->addr, arsta->link_id);
2004 		return;
2005 	}
2006 
2007 	band = def.chan->band;
2008 	sband = hw->wiphy->bands[band];
2009 	ratemask = link_sta->supp_rates[band];
2010 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2011 	rates = sband->bitrates;
2012 
2013 	rateset->num_rates = 0;
2014 
2015 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2016 		if (!(ratemask & 1))
2017 			continue;
2018 
2019 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
2020 		rateset->rates[rateset->num_rates] = rate;
2021 		rateset->num_rates++;
2022 	}
2023 }
2024 
2025 static bool
ath12k_peer_assoc_h_ht_masked(const u8 * ht_mcs_mask)2026 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
2027 {
2028 	int nss;
2029 
2030 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2031 		if (ht_mcs_mask[nss])
2032 			return false;
2033 
2034 	return true;
2035 }
2036 
2037 static bool
ath12k_peer_assoc_h_vht_masked(const u16 * vht_mcs_mask)2038 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
2039 {
2040 	int nss;
2041 
2042 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2043 		if (vht_mcs_mask[nss])
2044 			return false;
2045 
2046 	return true;
2047 }
2048 
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)2049 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
2050 				   struct ath12k_link_vif *arvif,
2051 				   struct ath12k_link_sta *arsta,
2052 				   struct ath12k_wmi_peer_assoc_arg *arg)
2053 {
2054 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2055 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2056 	const struct ieee80211_sta_ht_cap *ht_cap;
2057 	struct ieee80211_link_sta *link_sta;
2058 	struct cfg80211_chan_def def;
2059 	enum nl80211_band band;
2060 	const u8 *ht_mcs_mask;
2061 	int i, n;
2062 	u8 max_nss;
2063 	u32 stbc;
2064 
2065 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2066 
2067 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2068 		return;
2069 
2070 	link_sta = ath12k_mac_get_link_sta(arsta);
2071 	if (!link_sta) {
2072 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc ht for sta %pM link %u\n",
2073 			    sta->addr, arsta->link_id);
2074 		return;
2075 	}
2076 
2077 	ht_cap = &link_sta->ht_cap;
2078 	if (!ht_cap->ht_supported)
2079 		return;
2080 
2081 	band = def.chan->band;
2082 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2083 
2084 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
2085 		return;
2086 
2087 	arg->ht_flag = true;
2088 
2089 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2090 				    ht_cap->ampdu_factor)) - 1;
2091 
2092 	arg->peer_mpdu_density =
2093 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
2094 
2095 	arg->peer_ht_caps = ht_cap->cap;
2096 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
2097 
2098 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2099 		arg->ldpc_flag = true;
2100 
2101 	if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2102 		arg->bw_40 = true;
2103 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
2104 	}
2105 
2106 	/* As firmware handles these two flags (IEEE80211_HT_CAP_SGI_20
2107 	 * and IEEE80211_HT_CAP_SGI_40) for enabling SGI, reset both
2108 	 * flags if guard interval is to force Long GI
2109 	 */
2110 	if (arvif->bitrate_mask.control[band].gi == NL80211_TXRATE_FORCE_LGI) {
2111 		arg->peer_ht_caps &= ~(IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40);
2112 	} else {
2113 		/* Enable SGI flag if either SGI_20 or SGI_40 is supported */
2114 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40))
2115 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
2116 	}
2117 
2118 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2119 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
2120 		arg->stbc_flag = true;
2121 	}
2122 
2123 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2124 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2125 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2126 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
2127 		arg->peer_rate_caps |= stbc;
2128 		arg->stbc_flag = true;
2129 	}
2130 
2131 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2132 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
2133 	else if (ht_cap->mcs.rx_mask[1])
2134 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
2135 
2136 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2137 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2138 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2139 			max_nss = (i / 8) + 1;
2140 			arg->peer_ht_rates.rates[n++] = i;
2141 		}
2142 
2143 	/* This is a workaround for HT-enabled STAs which break the spec
2144 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2145 	 *
2146 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2147 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2148 	 *
2149 	 * Firmware asserts if such situation occurs.
2150 	 */
2151 	if (n == 0) {
2152 		arg->peer_ht_rates.num_rates = 8;
2153 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2154 			arg->peer_ht_rates.rates[i] = i;
2155 	} else {
2156 		arg->peer_ht_rates.num_rates = n;
2157 		arg->peer_nss = min(link_sta->rx_nss, max_nss);
2158 	}
2159 
2160 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2161 		   arg->peer_mac,
2162 		   arg->peer_ht_rates.num_rates,
2163 		   arg->peer_nss);
2164 }
2165 
ath12k_mac_get_max_vht_mcs_map(u16 mcs_map,int nss)2166 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
2167 {
2168 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2169 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
2170 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
2171 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
2172 	}
2173 	return 0;
2174 }
2175 
2176 static u16
ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])2177 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2178 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2179 {
2180 	int idx_limit;
2181 	int nss;
2182 	u16 mcs_map;
2183 	u16 mcs;
2184 
2185 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2186 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2187 			  vht_mcs_limit[nss];
2188 
2189 		if (mcs_map)
2190 			idx_limit = fls(mcs_map) - 1;
2191 		else
2192 			idx_limit = -1;
2193 
2194 		switch (idx_limit) {
2195 		case 0:
2196 		case 1:
2197 		case 2:
2198 		case 3:
2199 		case 4:
2200 		case 5:
2201 		case 6:
2202 		case 7:
2203 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2204 			break;
2205 		case 8:
2206 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2207 			break;
2208 		case 9:
2209 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2210 			break;
2211 		default:
2212 			WARN_ON(1);
2213 			fallthrough;
2214 		case -1:
2215 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2216 			break;
2217 		}
2218 
2219 		tx_mcs_set &= ~(0x3 << (nss * 2));
2220 		tx_mcs_set |= mcs << (nss * 2);
2221 	}
2222 
2223 	return tx_mcs_set;
2224 }
2225 
ath12k_get_nss_160mhz(struct ath12k * ar,u8 max_nss)2226 static u8 ath12k_get_nss_160mhz(struct ath12k *ar,
2227 				u8 max_nss)
2228 {
2229 	u8 nss_ratio_info = ar->pdev->cap.nss_ratio_info;
2230 	u8 max_sup_nss = 0;
2231 
2232 	switch (nss_ratio_info) {
2233 	case WMI_NSS_RATIO_1BY2_NSS:
2234 		max_sup_nss = max_nss >> 1;
2235 		break;
2236 	case WMI_NSS_RATIO_3BY4_NSS:
2237 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_3BY4_NSS not supported\n");
2238 		break;
2239 	case WMI_NSS_RATIO_1_NSS:
2240 		max_sup_nss = max_nss;
2241 		break;
2242 	case WMI_NSS_RATIO_2_NSS:
2243 		ath12k_warn(ar->ab, "WMI_NSS_RATIO_2_NSS not supported\n");
2244 		break;
2245 	default:
2246 		ath12k_warn(ar->ab, "invalid nss ratio received from fw: %d\n",
2247 			    nss_ratio_info);
2248 		break;
2249 	}
2250 
2251 	return max_sup_nss;
2252 }
2253 
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)2254 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
2255 				    struct ath12k_link_vif *arvif,
2256 				    struct ath12k_link_sta *arsta,
2257 				    struct ath12k_wmi_peer_assoc_arg *arg)
2258 {
2259 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2260 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2261 	const struct ieee80211_sta_vht_cap *vht_cap;
2262 	struct ieee80211_link_sta *link_sta;
2263 	struct cfg80211_chan_def def;
2264 	enum nl80211_band band;
2265 	u16 *vht_mcs_mask;
2266 	u16 tx_mcs_map;
2267 	u8 ampdu_factor;
2268 	u8 max_nss, vht_mcs;
2269 	int i, vht_nss, nss_idx;
2270 	bool user_rate_valid = true;
2271 	u32 rx_nss, tx_nss, nss_160;
2272 
2273 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2274 
2275 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2276 		return;
2277 
2278 	link_sta = ath12k_mac_get_link_sta(arsta);
2279 	if (!link_sta) {
2280 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc vht for sta %pM link %u\n",
2281 			    sta->addr, arsta->link_id);
2282 		return;
2283 	}
2284 
2285 	vht_cap = &link_sta->vht_cap;
2286 	if (!vht_cap->vht_supported)
2287 		return;
2288 
2289 	band = def.chan->band;
2290 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2291 
2292 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
2293 		return;
2294 
2295 	arg->vht_flag = true;
2296 
2297 	/* TODO: similar flags required? */
2298 	arg->vht_capable = true;
2299 
2300 	if (def.chan->band == NL80211_BAND_2GHZ)
2301 		arg->vht_ng_flag = true;
2302 
2303 	arg->peer_vht_caps = vht_cap->cap;
2304 
2305 	ampdu_factor = (vht_cap->cap &
2306 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2307 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2308 
2309 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2310 	 * zero in VHT IE. Using it would result in degraded throughput.
2311 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2312 	 * it if VHT max_mpdu is smaller.
2313 	 */
2314 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2315 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2316 					ampdu_factor)) - 1);
2317 
2318 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2319 		arg->bw_80 = true;
2320 
2321 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2322 		arg->bw_160 = true;
2323 
2324 	vht_nss =  ath12k_mac_max_vht_nss(vht_mcs_mask);
2325 
2326 	if (vht_nss > link_sta->rx_nss) {
2327 		user_rate_valid = false;
2328 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2329 			if (vht_mcs_mask[nss_idx]) {
2330 				user_rate_valid = true;
2331 				break;
2332 			}
2333 		}
2334 	}
2335 
2336 	if (!user_rate_valid) {
2337 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2338 			   "Setting vht range MCS value to peer supported nss:%d for peer %pM\n",
2339 			   link_sta->rx_nss, arsta->addr);
2340 		vht_mcs_mask[link_sta->rx_nss - 1] = vht_mcs_mask[vht_nss - 1];
2341 	}
2342 
2343 	/* Calculate peer NSS capability from VHT capabilities if STA
2344 	 * supports VHT.
2345 	 */
2346 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2347 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2348 			  (2 * i) & 3;
2349 
2350 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
2351 		    vht_mcs_mask[i])
2352 			max_nss = i + 1;
2353 	}
2354 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2355 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2356 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2357 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2358 
2359 	tx_mcs_map = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
2360 	arg->tx_mcs_set = ath12k_peer_assoc_h_vht_limit(tx_mcs_map, vht_mcs_mask);
2361 
2362 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
2363 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
2364 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
2365 	 */
2366 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
2367 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
2368 
2369 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
2370 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
2371 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
2372 
2373 	/* TODO:  Check */
2374 	arg->tx_max_mcs_nss = 0xFF;
2375 
2376 	if (arg->peer_phymode == MODE_11AC_VHT160) {
2377 		tx_nss = ath12k_get_nss_160mhz(ar, max_nss);
2378 		rx_nss = min(arg->peer_nss, tx_nss);
2379 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2380 
2381 		if (!rx_nss) {
2382 			ath12k_warn(ar->ab, "invalid max_nss\n");
2383 			return;
2384 		}
2385 
2386 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2387 		arg->peer_bw_rxnss_override |= nss_160;
2388 	}
2389 
2390 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2391 		   "mac vht peer %pM max_mpdu %d flags 0x%x nss_override 0x%x\n",
2392 		   arsta->addr, arg->peer_max_mpdu, arg->peer_flags,
2393 		   arg->peer_bw_rxnss_override);
2394 }
2395 
ath12k_mac_get_max_he_mcs_map(u16 mcs_map,int nss)2396 static int ath12k_mac_get_max_he_mcs_map(u16 mcs_map, int nss)
2397 {
2398 	switch ((mcs_map >> (2 * nss)) & 0x3) {
2399 	case IEEE80211_HE_MCS_SUPPORT_0_7: return BIT(8) - 1;
2400 	case IEEE80211_HE_MCS_SUPPORT_0_9: return BIT(10) - 1;
2401 	case IEEE80211_HE_MCS_SUPPORT_0_11: return BIT(12) - 1;
2402 	}
2403 	return 0;
2404 }
2405 
ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,const u16 * he_mcs_limit)2406 static u16 ath12k_peer_assoc_h_he_limit(u16 tx_mcs_set,
2407 					const u16 *he_mcs_limit)
2408 {
2409 	int idx_limit;
2410 	int nss;
2411 	u16 mcs_map;
2412 	u16 mcs;
2413 
2414 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
2415 		mcs_map = ath12k_mac_get_max_he_mcs_map(tx_mcs_set, nss) &
2416 			he_mcs_limit[nss];
2417 
2418 		if (mcs_map)
2419 			idx_limit = fls(mcs_map) - 1;
2420 		else
2421 			idx_limit = -1;
2422 
2423 		switch (idx_limit) {
2424 		case 0 ... 7:
2425 			mcs = IEEE80211_HE_MCS_SUPPORT_0_7;
2426 			break;
2427 		case 8:
2428 		case 9:
2429 			mcs = IEEE80211_HE_MCS_SUPPORT_0_9;
2430 			break;
2431 		case 10:
2432 		case 11:
2433 			mcs = IEEE80211_HE_MCS_SUPPORT_0_11;
2434 			break;
2435 		default:
2436 			WARN_ON(1);
2437 			fallthrough;
2438 		case -1:
2439 			mcs = IEEE80211_HE_MCS_NOT_SUPPORTED;
2440 			break;
2441 		}
2442 
2443 		tx_mcs_set &= ~(0x3 << (nss * 2));
2444 		tx_mcs_set |= mcs << (nss * 2);
2445 	}
2446 
2447 	return tx_mcs_set;
2448 }
2449 
2450 static bool
ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])2451 ath12k_peer_assoc_h_he_masked(const u16 he_mcs_mask[NL80211_HE_NSS_MAX])
2452 {
2453 	int nss;
2454 
2455 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++)
2456 		if (he_mcs_mask[nss])
2457 			return false;
2458 
2459 	return true;
2460 }
2461 
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)2462 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
2463 				   struct ath12k_link_vif *arvif,
2464 				   struct ath12k_link_sta *arsta,
2465 				   struct ath12k_wmi_peer_assoc_arg *arg)
2466 {
2467 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2468 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2469 	const struct ieee80211_sta_he_cap *he_cap;
2470 	struct ieee80211_bss_conf *link_conf;
2471 	struct ieee80211_link_sta *link_sta;
2472 	struct cfg80211_chan_def def;
2473 	int i;
2474 	u8 ampdu_factor, max_nss;
2475 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2476 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
2477 	u16 mcs_160_map, mcs_80_map;
2478 	u8 link_id = arvif->link_id;
2479 	bool support_160;
2480 	enum nl80211_band band;
2481 	u16 *he_mcs_mask;
2482 	u8 he_mcs;
2483 	u16 he_tx_mcs = 0, v = 0;
2484 	int he_nss, nss_idx;
2485 	bool user_rate_valid = true;
2486 	u32 rx_nss, tx_nss, nss_160;
2487 
2488 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
2489 		return;
2490 
2491 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
2492 	if (!link_conf) {
2493 		ath12k_warn(ar->ab, "unable to access bss link conf in peer assoc he for vif %pM link %u",
2494 			    vif->addr, link_id);
2495 		return;
2496 	}
2497 
2498 	link_sta = ath12k_mac_get_link_sta(arsta);
2499 	if (!link_sta) {
2500 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2501 			    sta->addr, arsta->link_id);
2502 		return;
2503 	}
2504 
2505 	he_cap = &link_sta->he_cap;
2506 	if (!he_cap->has_he)
2507 		return;
2508 
2509 	band = def.chan->band;
2510 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
2511 
2512 	if (ath12k_peer_assoc_h_he_masked(he_mcs_mask))
2513 		return;
2514 
2515 	arg->he_flag = true;
2516 
2517 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
2518 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
2519 
2520 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
2521 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2522 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2523 
2524 	if (support_160) {
2525 		for (i = 7; i >= 0; i--) {
2526 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
2527 
2528 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2529 				rx_mcs_160 = i + 1;
2530 				break;
2531 			}
2532 		}
2533 	}
2534 
2535 	for (i = 7; i >= 0; i--) {
2536 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
2537 
2538 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
2539 			rx_mcs_80 = i + 1;
2540 			break;
2541 		}
2542 	}
2543 
2544 	if (support_160)
2545 		max_nss = min(rx_mcs_80, rx_mcs_160);
2546 	else
2547 		max_nss = rx_mcs_80;
2548 
2549 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2550 
2551 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
2552 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
2553 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
2554 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
2555 	arg->peer_he_ops = link_conf->he_oper.params;
2556 
2557 	/* the top most byte is used to indicate BSS color info */
2558 	arg->peer_he_ops &= 0xffffff;
2559 
2560 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
2561 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
2562 	 * as calculated while parsing VHT caps(if VHT caps is present)
2563 	 * or HT caps (if VHT caps is not present).
2564 	 *
2565 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
2566 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
2567 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
2568 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
2569 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
2570 	 * length.
2571 	 */
2572 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2573 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
2574 
2575 	if (ampdu_factor) {
2576 		if (link_sta->vht_cap.vht_supported)
2577 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
2578 						    ampdu_factor)) - 1;
2579 		else if (link_sta->ht_cap.ht_supported)
2580 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
2581 						    ampdu_factor)) - 1;
2582 	}
2583 
2584 	if (he_cap->he_cap_elem.phy_cap_info[6] &
2585 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2586 		int bit = 7;
2587 		int nss, ru;
2588 
2589 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
2590 					  IEEE80211_PPE_THRES_NSS_MASK;
2591 		arg->peer_ppet.ru_bit_mask =
2592 			(he_cap->ppe_thres[0] &
2593 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2594 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2595 
2596 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
2597 			for (ru = 0; ru < 4; ru++) {
2598 				u32 val = 0;
2599 				int i;
2600 
2601 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
2602 					continue;
2603 				for (i = 0; i < 6; i++) {
2604 					val >>= 1;
2605 					val |= ((he_cap->ppe_thres[bit / 8] >>
2606 						 (bit % 8)) & 0x1) << 5;
2607 					bit++;
2608 				}
2609 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
2610 								val << (ru * 6);
2611 			}
2612 		}
2613 	}
2614 
2615 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
2616 		arg->twt_responder = true;
2617 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
2618 		arg->twt_requester = true;
2619 
2620 	he_nss = ath12k_mac_max_he_nss(he_mcs_mask);
2621 
2622 	if (he_nss > link_sta->rx_nss) {
2623 		user_rate_valid = false;
2624 		for (nss_idx = link_sta->rx_nss - 1; nss_idx >= 0; nss_idx--) {
2625 			if (he_mcs_mask[nss_idx]) {
2626 				user_rate_valid = true;
2627 				break;
2628 			}
2629 		}
2630 	}
2631 
2632 	if (!user_rate_valid) {
2633 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2634 			   "Setting he range MCS value to peer supported nss:%d for peer %pM\n",
2635 			   link_sta->rx_nss, arsta->addr);
2636 		he_mcs_mask[link_sta->rx_nss - 1] = he_mcs_mask[he_nss - 1];
2637 	}
2638 
2639 	switch (link_sta->bandwidth) {
2640 	case IEEE80211_STA_RX_BW_160:
2641 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
2642 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2643 
2644 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2645 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
2646 
2647 		arg->peer_he_mcs_count++;
2648 		if (!he_tx_mcs)
2649 			he_tx_mcs = v;
2650 		fallthrough;
2651 
2652 	default:
2653 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
2654 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2655 
2656 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
2657 		v = ath12k_peer_assoc_h_he_limit(v, he_mcs_mask);
2658 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
2659 
2660 		arg->peer_he_mcs_count++;
2661 		if (!he_tx_mcs)
2662 			he_tx_mcs = v;
2663 		break;
2664 	}
2665 
2666 	/* Calculate peer NSS capability from HE capabilities if STA
2667 	 * supports HE.
2668 	 */
2669 	for (i = 0, max_nss = 0, he_mcs = 0; i < NL80211_HE_NSS_MAX; i++) {
2670 		he_mcs = he_tx_mcs >> (2 * i) & 3;
2671 
2672 		/* In case of fixed rates, MCS Range in he_tx_mcs might have
2673 		 * unsupported range, with he_mcs_mask set, so check either of them
2674 		 * to find nss.
2675 		 */
2676 		if (he_mcs != IEEE80211_HE_MCS_NOT_SUPPORTED ||
2677 		    he_mcs_mask[i])
2678 			max_nss = i + 1;
2679 	}
2680 
2681 	max_nss = min(max_nss, ar->num_tx_chains);
2682 	arg->peer_nss = min(link_sta->rx_nss, max_nss);
2683 
2684 	if (arg->peer_phymode == MODE_11AX_HE160) {
2685 		tx_nss = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
2686 		rx_nss = min(arg->peer_nss, tx_nss);
2687 
2688 		arg->peer_nss = min(link_sta->rx_nss, ar->num_rx_chains);
2689 		arg->peer_bw_rxnss_override = ATH12K_BW_NSS_MAP_ENABLE;
2690 
2691 		if (!rx_nss) {
2692 			ath12k_warn(ar->ab, "invalid max_nss\n");
2693 			return;
2694 		}
2695 
2696 		nss_160 = u32_encode_bits(rx_nss - 1, ATH12K_PEER_RX_NSS_160MHZ);
2697 		arg->peer_bw_rxnss_override |= nss_160;
2698 	}
2699 
2700 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2701 		   "mac he peer %pM nss %d mcs cnt %d nss_override 0x%x\n",
2702 		   arsta->addr, arg->peer_nss,
2703 		   arg->peer_he_mcs_count,
2704 		   arg->peer_bw_rxnss_override);
2705 }
2706 
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)2707 static void ath12k_peer_assoc_h_he_6ghz(struct ath12k *ar,
2708 					struct ath12k_link_vif *arvif,
2709 					struct ath12k_link_sta *arsta,
2710 					struct ath12k_wmi_peer_assoc_arg *arg)
2711 {
2712 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
2713 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2714 	const struct ieee80211_sta_he_cap *he_cap;
2715 	struct ieee80211_link_sta *link_sta;
2716 	struct cfg80211_chan_def def;
2717 	enum nl80211_band band;
2718 	u8 ampdu_factor, mpdu_density;
2719 
2720 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
2721 		return;
2722 
2723 	band = def.chan->band;
2724 
2725 	link_sta = ath12k_mac_get_link_sta(arsta);
2726 	if (!link_sta) {
2727 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he 6ghz for sta %pM link %u\n",
2728 			    sta->addr, arsta->link_id);
2729 		return;
2730 	}
2731 
2732 	he_cap = &link_sta->he_cap;
2733 
2734 	if (!arg->he_flag || band != NL80211_BAND_6GHZ || !link_sta->he_6ghz_capa.capa)
2735 		return;
2736 
2737 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2738 		arg->bw_40 = true;
2739 
2740 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2741 		arg->bw_80 = true;
2742 
2743 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160)
2744 		arg->bw_160 = true;
2745 
2746 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2747 		arg->bw_320 = true;
2748 
2749 	arg->peer_he_caps_6ghz = le16_to_cpu(link_sta->he_6ghz_capa.capa);
2750 
2751 	mpdu_density = u32_get_bits(arg->peer_he_caps_6ghz,
2752 				    IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
2753 	arg->peer_mpdu_density = ath12k_parse_mpdudensity(mpdu_density);
2754 
2755 	/* From IEEE Std 802.11ax-2021 - Section 10.12.2: An HE STA shall be capable of
2756 	 * receiving A-MPDU where the A-MPDU pre-EOF padding length is up to the value
2757 	 * indicated by the Maximum A-MPDU Length Exponent Extension field in the HE
2758 	 * Capabilities element and the Maximum A-MPDU Length Exponent field in HE 6 GHz
2759 	 * Band Capabilities element in the 6 GHz band.
2760 	 *
2761 	 * Here, we are extracting the Max A-MPDU Exponent Extension from HE caps and
2762 	 * factor is the Maximum A-MPDU Length Exponent from HE 6 GHZ Band capability.
2763 	 */
2764 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
2765 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK) +
2766 			u32_get_bits(arg->peer_he_caps_6ghz,
2767 				     IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
2768 
2769 	arg->peer_max_mpdu = (1u << (IEEE80211_HE_6GHZ_MAX_AMPDU_FACTOR +
2770 				     ampdu_factor)) - 1;
2771 }
2772 
ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap * ht_cap,const struct ieee80211_he_6ghz_capa * he_6ghz_capa,int * smps)2773 static int ath12k_get_smps_from_capa(const struct ieee80211_sta_ht_cap *ht_cap,
2774 				     const struct ieee80211_he_6ghz_capa *he_6ghz_capa,
2775 				     int *smps)
2776 {
2777 	if (ht_cap->ht_supported)
2778 		*smps = u16_get_bits(ht_cap->cap, IEEE80211_HT_CAP_SM_PS);
2779 	else
2780 		*smps = le16_get_bits(he_6ghz_capa->capa,
2781 				      IEEE80211_HE_6GHZ_CAP_SM_PS);
2782 
2783 	if (*smps >= ARRAY_SIZE(ath12k_smps_map))
2784 		return -EINVAL;
2785 
2786 	return 0;
2787 }
2788 
ath12k_peer_assoc_h_smps(struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)2789 static void ath12k_peer_assoc_h_smps(struct ath12k_link_sta *arsta,
2790 				     struct ath12k_wmi_peer_assoc_arg *arg)
2791 {
2792 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2793 	const struct ieee80211_he_6ghz_capa *he_6ghz_capa;
2794 	struct ath12k_link_vif *arvif = arsta->arvif;
2795 	const struct ieee80211_sta_ht_cap *ht_cap;
2796 	struct ieee80211_link_sta *link_sta;
2797 	struct ath12k *ar = arvif->ar;
2798 	int smps;
2799 
2800 	link_sta = ath12k_mac_get_link_sta(arsta);
2801 	if (!link_sta) {
2802 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
2803 			    sta->addr, arsta->link_id);
2804 		return;
2805 	}
2806 
2807 	he_6ghz_capa = &link_sta->he_6ghz_capa;
2808 	ht_cap = &link_sta->ht_cap;
2809 
2810 	if (!ht_cap->ht_supported && !he_6ghz_capa->capa)
2811 		return;
2812 
2813 	if (ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps))
2814 		return;
2815 
2816 	switch (smps) {
2817 	case WLAN_HT_CAP_SM_PS_STATIC:
2818 		arg->static_mimops_flag = true;
2819 		break;
2820 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
2821 		arg->dynamic_mimops_flag = true;
2822 		break;
2823 	case WLAN_HT_CAP_SM_PS_DISABLED:
2824 		arg->spatial_mux_flag = true;
2825 		break;
2826 	default:
2827 		break;
2828 	}
2829 }
2830 
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)2831 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
2832 				    struct ath12k_link_vif *arvif,
2833 				    struct ath12k_link_sta *arsta,
2834 				    struct ath12k_wmi_peer_assoc_arg *arg)
2835 {
2836 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2837 
2838 	switch (arvif->ahvif->vdev_type) {
2839 	case WMI_VDEV_TYPE_AP:
2840 		if (sta->wme) {
2841 			/* TODO: Check WME vs QoS */
2842 			arg->is_wme_set = true;
2843 			arg->qos_flag = true;
2844 		}
2845 
2846 		if (sta->wme && sta->uapsd_queues) {
2847 			/* TODO: Check WME vs QoS */
2848 			arg->is_wme_set = true;
2849 			arg->apsd_flag = true;
2850 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
2851 		}
2852 		break;
2853 	case WMI_VDEV_TYPE_STA:
2854 		if (sta->wme) {
2855 			arg->is_wme_set = true;
2856 			arg->qos_flag = true;
2857 		}
2858 		break;
2859 	default:
2860 		break;
2861 	}
2862 
2863 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
2864 		   arsta->addr, arg->qos_flag);
2865 }
2866 
ath12k_peer_assoc_qos_ap(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)2867 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
2868 				    struct ath12k_link_vif *arvif,
2869 				    struct ath12k_link_sta *arsta)
2870 {
2871 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
2872 	struct ath12k_wmi_ap_ps_arg arg;
2873 	u32 max_sp;
2874 	u32 uapsd;
2875 	int ret;
2876 
2877 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
2878 
2879 	arg.vdev_id = arvif->vdev_id;
2880 
2881 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2882 		   sta->uapsd_queues, sta->max_sp);
2883 
2884 	uapsd = 0;
2885 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2886 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2887 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2888 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2889 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2890 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2891 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2892 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2893 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2894 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2895 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2896 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2897 
2898 	max_sp = 0;
2899 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2900 		max_sp = sta->max_sp;
2901 
2902 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
2903 	arg.value = uapsd;
2904 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
2905 	if (ret)
2906 		goto err;
2907 
2908 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
2909 	arg.value = max_sp;
2910 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
2911 	if (ret)
2912 		goto err;
2913 
2914 	/* TODO: revisit during testing */
2915 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
2916 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2917 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
2918 	if (ret)
2919 		goto err;
2920 
2921 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
2922 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
2923 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, arsta->addr, &arg);
2924 	if (ret)
2925 		goto err;
2926 
2927 	return 0;
2928 
2929 err:
2930 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
2931 		    arg.param, arvif->vdev_id, ret);
2932 	return ret;
2933 }
2934 
ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta * sta)2935 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_link_sta *sta)
2936 {
2937 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
2938 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
2939 }
2940 
ath12k_mac_get_phymode_vht(struct ath12k * ar,struct ieee80211_link_sta * link_sta)2941 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
2942 						    struct ieee80211_link_sta *link_sta)
2943 {
2944 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2945 		if (link_sta->vht_cap.cap & (IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
2946 		    IEEE80211_VHT_CAP_EXT_NSS_BW_MASK))
2947 			return MODE_11AC_VHT160;
2948 
2949 		/* Allow STA to connect even if it does not explicitly advertise 160 MHz
2950 		 * support
2951 		 */
2952 		return MODE_11AC_VHT160;
2953 	}
2954 
2955 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2956 		return MODE_11AC_VHT80;
2957 
2958 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2959 		return MODE_11AC_VHT40;
2960 
2961 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
2962 		return MODE_11AC_VHT20;
2963 
2964 	return MODE_UNKNOWN;
2965 }
2966 
ath12k_mac_get_phymode_he(struct ath12k * ar,struct ieee80211_link_sta * link_sta)2967 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
2968 						   struct ieee80211_link_sta *link_sta)
2969 {
2970 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2971 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
2972 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
2973 			return MODE_11AX_HE160;
2974 
2975 		return MODE_UNKNOWN;
2976 	}
2977 
2978 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
2979 		return MODE_11AX_HE80;
2980 
2981 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
2982 		return MODE_11AX_HE40;
2983 
2984 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
2985 		return MODE_11AX_HE20;
2986 
2987 	return MODE_UNKNOWN;
2988 }
2989 
ath12k_mac_get_phymode_eht(struct ath12k * ar,struct ieee80211_link_sta * link_sta)2990 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
2991 						    struct ieee80211_link_sta *link_sta)
2992 {
2993 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_320)
2994 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[0] &
2995 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
2996 			return MODE_11BE_EHT320;
2997 
2998 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2999 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[0] &
3000 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
3001 			return MODE_11BE_EHT160;
3002 
3003 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
3004 			    link_sta->he_cap.he_cap_elem.phy_cap_info[0]);
3005 
3006 		return MODE_UNKNOWN;
3007 	}
3008 
3009 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3010 		return MODE_11BE_EHT80;
3011 
3012 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3013 		return MODE_11BE_EHT40;
3014 
3015 	if (link_sta->bandwidth == IEEE80211_STA_RX_BW_20)
3016 		return MODE_11BE_EHT20;
3017 
3018 	return MODE_UNKNOWN;
3019 }
3020 
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)3021 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
3022 					struct ath12k_link_vif *arvif,
3023 					struct ath12k_link_sta *arsta,
3024 					struct ath12k_wmi_peer_assoc_arg *arg)
3025 {
3026 	struct ieee80211_link_sta *link_sta;
3027 	struct cfg80211_chan_def def;
3028 	enum nl80211_band band;
3029 	const u8 *ht_mcs_mask;
3030 	const u16 *vht_mcs_mask;
3031 	const u16 *he_mcs_mask;
3032 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
3033 
3034 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3035 
3036 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3037 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3038 
3039 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
3040 		return;
3041 
3042 	band = def.chan->band;
3043 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3044 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3045 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
3046 
3047 	link_sta = ath12k_mac_get_link_sta(arsta);
3048 	if (!link_sta) {
3049 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
3050 			    sta->addr, arsta->link_id);
3051 		return;
3052 	}
3053 
3054 	switch (band) {
3055 	case NL80211_BAND_2GHZ:
3056 		if (link_sta->eht_cap.has_eht) {
3057 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3058 				phymode = MODE_11BE_EHT40_2G;
3059 			else
3060 				phymode = MODE_11BE_EHT20_2G;
3061 		} else if (link_sta->he_cap.has_he &&
3062 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3063 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_80)
3064 				phymode = MODE_11AX_HE80_2G;
3065 			else if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3066 				phymode = MODE_11AX_HE40_2G;
3067 			else
3068 				phymode = MODE_11AX_HE20_2G;
3069 		} else if (link_sta->vht_cap.vht_supported &&
3070 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3071 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3072 				phymode = MODE_11AC_VHT40;
3073 			else
3074 				phymode = MODE_11AC_VHT20;
3075 		} else if (link_sta->ht_cap.ht_supported &&
3076 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3077 			if (link_sta->bandwidth == IEEE80211_STA_RX_BW_40)
3078 				phymode = MODE_11NG_HT40;
3079 			else
3080 				phymode = MODE_11NG_HT20;
3081 		} else if (ath12k_mac_sta_has_ofdm_only(link_sta)) {
3082 			phymode = MODE_11G;
3083 		} else {
3084 			phymode = MODE_11B;
3085 		}
3086 		break;
3087 	case NL80211_BAND_5GHZ:
3088 	case NL80211_BAND_6GHZ:
3089 		/* Check EHT first */
3090 		if (link_sta->eht_cap.has_eht) {
3091 			phymode = ath12k_mac_get_phymode_eht(ar, link_sta);
3092 		} else if (link_sta->he_cap.has_he &&
3093 			   !ath12k_peer_assoc_h_he_masked(he_mcs_mask)) {
3094 			phymode = ath12k_mac_get_phymode_he(ar, link_sta);
3095 		} else if (link_sta->vht_cap.vht_supported &&
3096 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
3097 			phymode = ath12k_mac_get_phymode_vht(ar, link_sta);
3098 		} else if (link_sta->ht_cap.ht_supported &&
3099 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
3100 			if (link_sta->bandwidth >= IEEE80211_STA_RX_BW_40)
3101 				phymode = MODE_11NA_HT40;
3102 			else
3103 				phymode = MODE_11NA_HT20;
3104 		} else {
3105 			phymode = MODE_11A;
3106 		}
3107 		break;
3108 	default:
3109 		break;
3110 	}
3111 
3112 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
3113 		   arsta->addr, ath12k_mac_phymode_str(phymode));
3114 
3115 	arg->peer_phymode = phymode;
3116 	WARN_ON(phymode == MODE_UNKNOWN);
3117 }
3118 
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)3119 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
3120 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
3121 				   u32 *rx_mcs, u32 *tx_mcs)
3122 {
3123 	*rx_mcs = 0;
3124 	u32p_replace_bits(rx_mcs,
3125 			  u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX),
3126 			  WMI_EHT_MCS_NSS_0_7);
3127 	u32p_replace_bits(rx_mcs,
3128 			  u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX),
3129 			  WMI_EHT_MCS_NSS_8_9);
3130 	u32p_replace_bits(rx_mcs,
3131 			  u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX),
3132 			  WMI_EHT_MCS_NSS_10_11);
3133 	u32p_replace_bits(rx_mcs,
3134 			  u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX),
3135 			  WMI_EHT_MCS_NSS_12_13);
3136 
3137 	*tx_mcs = 0;
3138 	u32p_replace_bits(tx_mcs,
3139 			  u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX),
3140 			  WMI_EHT_MCS_NSS_0_7);
3141 	u32p_replace_bits(tx_mcs,
3142 			  u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX),
3143 			  WMI_EHT_MCS_NSS_8_9);
3144 	u32p_replace_bits(tx_mcs,
3145 			  u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX),
3146 			  WMI_EHT_MCS_NSS_10_11);
3147 	u32p_replace_bits(tx_mcs,
3148 			  u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX),
3149 			  WMI_EHT_MCS_NSS_12_13);
3150 }
3151 
ath12k_mac_set_eht_ppe_threshold(const u8 * ppe_thres,struct ath12k_wmi_ppe_threshold_arg * ppet)3152 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
3153 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
3154 {
3155 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
3156 	u8 nss, ru, i;
3157 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
3158 
3159 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
3160 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
3161 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3162 
3163 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
3164 		for (ru = 0;
3165 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
3166 		     ru++) {
3167 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
3168 				continue;
3169 
3170 			val = 0;
3171 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
3172 				val |= (((ppe_thres[bit_pos / 8] >>
3173 					  (bit_pos % 8)) & 0x1) << i);
3174 				bit_pos++;
3175 			}
3176 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
3177 					(val << (ru * ppet_bit_len_per_ru));
3178 		}
3179 	}
3180 }
3181 
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)3182 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
3183 				    struct ath12k_link_vif *arvif,
3184 				    struct ath12k_link_sta *arsta,
3185 				    struct ath12k_wmi_peer_assoc_arg *arg)
3186 {
3187 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3188 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
3189 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
3190 	const struct ieee80211_sta_eht_cap *eht_cap;
3191 	const struct ieee80211_sta_he_cap *he_cap;
3192 	struct ieee80211_link_sta *link_sta;
3193 	struct ieee80211_bss_conf *link_conf;
3194 	u32 *rx_mcs, *tx_mcs;
3195 
3196 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3197 
3198 	link_sta = ath12k_mac_get_link_sta(arsta);
3199 	if (!link_sta) {
3200 		ath12k_warn(ar->ab, "unable to access link sta in peer assoc eht for sta %pM link %u\n",
3201 			    sta->addr, arsta->link_id);
3202 		return;
3203 	}
3204 
3205 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3206 	if (!link_conf) {
3207 		ath12k_warn(ar->ab, "unable to access link_conf in peer assoc eht set\n");
3208 		return;
3209 	}
3210 
3211 	eht_cap = &link_sta->eht_cap;
3212 	he_cap = &link_sta->he_cap;
3213 	if (!he_cap->has_he || !eht_cap->has_eht)
3214 		return;
3215 
3216 	arg->eht_flag = true;
3217 
3218 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
3219 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
3220 	    eht_cap->eht_ppe_thres[0] != 0)
3221 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
3222 						 &arg->peer_eht_ppet);
3223 
3224 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
3225 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
3226 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
3227 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
3228 
3229 	rx_mcs = arg->peer_eht_rx_mcs_set;
3230 	tx_mcs = arg->peer_eht_tx_mcs_set;
3231 
3232 	switch (link_sta->bandwidth) {
3233 	case IEEE80211_STA_RX_BW_320:
3234 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
3235 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3236 				       bw->rx_tx_mcs9_max_nss,
3237 				       bw->rx_tx_mcs11_max_nss,
3238 				       bw->rx_tx_mcs13_max_nss,
3239 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
3240 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320]);
3241 		arg->peer_eht_mcs_count++;
3242 		fallthrough;
3243 	case IEEE80211_STA_RX_BW_160:
3244 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
3245 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3246 				       bw->rx_tx_mcs9_max_nss,
3247 				       bw->rx_tx_mcs11_max_nss,
3248 				       bw->rx_tx_mcs13_max_nss,
3249 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
3250 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160]);
3251 		arg->peer_eht_mcs_count++;
3252 		fallthrough;
3253 	default:
3254 		if ((he_cap->he_cap_elem.phy_cap_info[0] &
3255 		     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
3256 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
3257 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
3258 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0) {
3259 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
3260 
3261 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
3262 					       bw_20->rx_tx_mcs9_max_nss,
3263 					       bw_20->rx_tx_mcs11_max_nss,
3264 					       bw_20->rx_tx_mcs13_max_nss,
3265 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3266 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80]);
3267 		} else {
3268 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
3269 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
3270 					       bw->rx_tx_mcs9_max_nss,
3271 					       bw->rx_tx_mcs11_max_nss,
3272 					       bw->rx_tx_mcs13_max_nss,
3273 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
3274 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80]);
3275 		}
3276 
3277 		arg->peer_eht_mcs_count++;
3278 		break;
3279 	}
3280 
3281 	arg->punct_bitmap = ~arvif->punct_bitmap;
3282 	arg->eht_disable_mcs15 = link_conf->eht_disable_mcs15;
3283 }
3284 
ath12k_peer_assoc_h_mlo(struct ath12k_link_sta * arsta,struct ath12k_wmi_peer_assoc_arg * arg)3285 static void ath12k_peer_assoc_h_mlo(struct ath12k_link_sta *arsta,
3286 				    struct ath12k_wmi_peer_assoc_arg *arg)
3287 {
3288 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
3289 	struct peer_assoc_mlo_params *ml = &arg->ml;
3290 	struct ath12k_sta *ahsta = arsta->ahsta;
3291 	struct ath12k_link_sta *arsta_p;
3292 	struct ath12k_link_vif *arvif;
3293 	unsigned long links;
3294 	u8 link_id;
3295 	int i;
3296 
3297 	if (!sta->mlo || ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID)
3298 		return;
3299 
3300 	ml->enabled = true;
3301 	ml->assoc_link = arsta->is_assoc_link;
3302 
3303 	/* For now considering the primary umac based on assoc link */
3304 	ml->primary_umac = arsta->is_assoc_link;
3305 	ml->peer_id_valid = true;
3306 	ml->logical_link_idx_valid = true;
3307 
3308 	ether_addr_copy(ml->mld_addr, sta->addr);
3309 	ml->logical_link_idx = arsta->link_idx;
3310 	ml->ml_peer_id = ahsta->ml_peer_id;
3311 	ml->ieee_link_id = arsta->link_id;
3312 	ml->num_partner_links = 0;
3313 	ml->eml_cap = sta->eml_cap;
3314 	links = ahsta->links_map;
3315 
3316 	rcu_read_lock();
3317 
3318 	i = 0;
3319 
3320 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
3321 		if (i >= ATH12K_WMI_MLO_MAX_LINKS)
3322 			break;
3323 
3324 		arsta_p = rcu_dereference(ahsta->link[link_id]);
3325 		arvif = rcu_dereference(ahsta->ahvif->link[link_id]);
3326 
3327 		if (arsta_p == arsta)
3328 			continue;
3329 
3330 		if (!arvif->is_started)
3331 			continue;
3332 
3333 		ml->partner_info[i].vdev_id = arvif->vdev_id;
3334 		ml->partner_info[i].hw_link_id = arvif->ar->pdev->hw_link_id;
3335 		ml->partner_info[i].assoc_link = arsta_p->is_assoc_link;
3336 		ml->partner_info[i].primary_umac = arsta_p->is_assoc_link;
3337 		ml->partner_info[i].logical_link_idx_valid = true;
3338 		ml->partner_info[i].logical_link_idx = arsta_p->link_idx;
3339 		ml->num_partner_links++;
3340 
3341 		i++;
3342 	}
3343 
3344 	rcu_read_unlock();
3345 }
3346 
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)3347 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
3348 				      struct ath12k_link_vif *arvif,
3349 				      struct ath12k_link_sta *arsta,
3350 				      struct ath12k_wmi_peer_assoc_arg *arg,
3351 				      bool reassoc)
3352 {
3353 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3354 
3355 	memset(arg, 0, sizeof(*arg));
3356 
3357 	reinit_completion(&ar->peer_assoc_done);
3358 
3359 	arg->peer_new_assoc = !reassoc;
3360 	ath12k_peer_assoc_h_basic(ar, arvif, arsta, arg);
3361 	ath12k_peer_assoc_h_crypto(ar, arvif, arsta, arg);
3362 	ath12k_peer_assoc_h_rates(ar, arvif, arsta, arg);
3363 	ath12k_peer_assoc_h_ht(ar, arvif, arsta, arg);
3364 	ath12k_peer_assoc_h_vht(ar, arvif, arsta, arg);
3365 	ath12k_peer_assoc_h_he(ar, arvif, arsta, arg);
3366 	ath12k_peer_assoc_h_he_6ghz(ar, arvif, arsta, arg);
3367 	ath12k_peer_assoc_h_eht(ar, arvif, arsta, arg);
3368 	ath12k_peer_assoc_h_qos(ar, arvif, arsta, arg);
3369 	ath12k_peer_assoc_h_phymode(ar, arvif, arsta, arg);
3370 	ath12k_peer_assoc_h_smps(arsta, arg);
3371 	ath12k_peer_assoc_h_mlo(arsta, arg);
3372 
3373 	arsta->peer_nss = arg->peer_nss;
3374 	/* TODO: amsdu_disable req? */
3375 }
3376 
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)3377 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_link_vif *arvif,
3378 				  const u8 *addr,
3379 				  const struct ieee80211_sta_ht_cap *ht_cap,
3380 				  const struct ieee80211_he_6ghz_capa *he_6ghz_capa)
3381 {
3382 	int smps, ret = 0;
3383 
3384 	if (!ht_cap->ht_supported && !he_6ghz_capa)
3385 		return 0;
3386 
3387 	ret = ath12k_get_smps_from_capa(ht_cap, he_6ghz_capa, &smps);
3388 	if (ret < 0)
3389 		return ret;
3390 
3391 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
3392 					 WMI_PEER_MIMO_PS_STATE,
3393 					 ath12k_smps_map[smps]);
3394 }
3395 
ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif * arvif)3396 static int ath12k_mac_set_he_txbf_conf(struct ath12k_link_vif *arvif)
3397 {
3398 	struct ath12k_vif *ahvif = arvif->ahvif;
3399 	struct ath12k *ar = arvif->ar;
3400 	u32 param = WMI_VDEV_PARAM_SET_HEMU_MODE;
3401 	u32 value = 0;
3402 	int ret;
3403 	struct ieee80211_bss_conf *link_conf;
3404 
3405 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3406 	if (!link_conf) {
3407 		ath12k_warn(ar->ab, "unable to access bss link conf in txbf conf\n");
3408 		return -EINVAL;
3409 	}
3410 
3411 	if (!link_conf->he_support)
3412 		return 0;
3413 
3414 	if (link_conf->he_su_beamformer) {
3415 		value |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3416 		if (link_conf->he_mu_beamformer &&
3417 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3418 			value |= u32_encode_bits(HE_MU_BFER_ENABLE, HE_MODE_MU_TX_BFER);
3419 	}
3420 
3421 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3422 		value |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3423 			 u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3424 
3425 		if (link_conf->he_full_ul_mumimo)
3426 			value |= u32_encode_bits(HE_UL_MUMIMO_ENABLE, HE_MODE_UL_MUMIMO);
3427 
3428 		if (link_conf->he_su_beamformee)
3429 			value |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3430 	}
3431 
3432 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3433 	if (ret) {
3434 		ath12k_warn(ar->ab, "failed to set vdev %d HE MU mode: %d\n",
3435 			    arvif->vdev_id, ret);
3436 		return ret;
3437 	}
3438 
3439 	param = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
3440 	value =	u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
3441 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
3442 				HE_TRIG_NONTRIG_SOUNDING_MODE);
3443 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3444 					    param, value);
3445 	if (ret) {
3446 		ath12k_warn(ar->ab, "failed to set vdev %d sounding mode: %d\n",
3447 			    arvif->vdev_id, ret);
3448 		return ret;
3449 	}
3450 
3451 	return 0;
3452 }
3453 
ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_sta_he_cap * he_cap,int * hemode)3454 static int ath12k_mac_vif_recalc_sta_he_txbf(struct ath12k *ar,
3455 					     struct ath12k_link_vif *arvif,
3456 					     struct ieee80211_sta_he_cap *he_cap,
3457 					     int *hemode)
3458 {
3459 	struct ieee80211_vif *vif = arvif->ahvif->vif;
3460 	struct ieee80211_he_cap_elem he_cap_elem = {};
3461 	struct ieee80211_sta_he_cap *cap_band;
3462 	struct cfg80211_chan_def def;
3463 	u8 link_id = arvif->link_id;
3464 	struct ieee80211_bss_conf *link_conf;
3465 
3466 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3467 	if (!link_conf) {
3468 		ath12k_warn(ar->ab, "unable to access bss link conf in recalc txbf conf\n");
3469 		return -EINVAL;
3470 	}
3471 
3472 	if (!link_conf->he_support)
3473 		return 0;
3474 
3475 	if (vif->type != NL80211_IFTYPE_STATION)
3476 		return -EINVAL;
3477 
3478 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, link_id, &def)))
3479 		return -EINVAL;
3480 
3481 	if (def.chan->band == NL80211_BAND_2GHZ)
3482 		cap_band = &ar->mac.iftype[NL80211_BAND_2GHZ][vif->type].he_cap;
3483 	else
3484 		cap_band = &ar->mac.iftype[NL80211_BAND_5GHZ][vif->type].he_cap;
3485 
3486 	memcpy(&he_cap_elem, &cap_band->he_cap_elem, sizeof(he_cap_elem));
3487 
3488 	*hemode = 0;
3489 	if (HECAP_PHY_SUBFME_GET(he_cap_elem.phy_cap_info)) {
3490 		if (HECAP_PHY_SUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3491 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3492 		if (HECAP_PHY_MUBFMR_GET(he_cap->he_cap_elem.phy_cap_info))
3493 			*hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
3494 	}
3495 
3496 	if (vif->type != NL80211_IFTYPE_MESH_POINT) {
3497 		*hemode |= u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
3498 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
3499 
3500 		if (HECAP_PHY_ULMUMIMO_GET(he_cap_elem.phy_cap_info))
3501 			if (HECAP_PHY_ULMUMIMO_GET(he_cap->he_cap_elem.phy_cap_info))
3502 				*hemode |= u32_encode_bits(HE_UL_MUMIMO_ENABLE,
3503 							  HE_MODE_UL_MUMIMO);
3504 
3505 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFEE))
3506 			*hemode |= u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE);
3507 
3508 		if (u32_get_bits(*hemode, HE_MODE_MU_TX_BFER))
3509 			*hemode |= u32_encode_bits(HE_SU_BFER_ENABLE, HE_MODE_SU_TX_BFER);
3510 	}
3511 
3512 	return 0;
3513 }
3514 
ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif * arvif)3515 static int ath12k_mac_set_eht_txbf_conf(struct ath12k_link_vif *arvif)
3516 {
3517 	struct ath12k_vif *ahvif = arvif->ahvif;
3518 	struct ath12k *ar = arvif->ar;
3519 	u32 param = WMI_VDEV_PARAM_SET_EHT_MU_MODE;
3520 	u32 value = 0;
3521 	int ret;
3522 	struct ieee80211_bss_conf *link_conf;
3523 
3524 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
3525 	if (!link_conf) {
3526 		ath12k_warn(ar->ab, "unable to access bss link conf in eht txbf conf\n");
3527 		return -ENOENT;
3528 	}
3529 
3530 	if (!link_conf->eht_support)
3531 		return 0;
3532 
3533 	if (link_conf->eht_su_beamformer) {
3534 		value |= u32_encode_bits(EHT_SU_BFER_ENABLE, EHT_MODE_SU_TX_BFER);
3535 		if (link_conf->eht_mu_beamformer &&
3536 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
3537 			value |= u32_encode_bits(EHT_MU_BFER_ENABLE,
3538 						 EHT_MODE_MU_TX_BFER) |
3539 				 u32_encode_bits(EHT_DL_MUOFDMA_ENABLE,
3540 						 EHT_MODE_DL_OFDMA_MUMIMO) |
3541 				 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE,
3542 						 EHT_MODE_UL_OFDMA_MUMIMO);
3543 	}
3544 
3545 	if (ahvif->vif->type != NL80211_IFTYPE_MESH_POINT) {
3546 		value |= u32_encode_bits(EHT_DL_MUOFDMA_ENABLE, EHT_MODE_DL_OFDMA) |
3547 			 u32_encode_bits(EHT_UL_MUOFDMA_ENABLE, EHT_MODE_UL_OFDMA);
3548 
3549 		if (link_conf->eht_80mhz_full_bw_ul_mumimo)
3550 			value |= u32_encode_bits(EHT_UL_MUMIMO_ENABLE, EHT_MODE_MUMIMO);
3551 
3552 		if (link_conf->eht_su_beamformee)
3553 			value |= u32_encode_bits(EHT_SU_BFEE_ENABLE,
3554 						 EHT_MODE_SU_TX_BFEE);
3555 	}
3556 
3557 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param, value);
3558 	if (ret) {
3559 		ath12k_warn(ar->ab, "failed to set vdev %d EHT MU mode: %d\n",
3560 			    arvif->vdev_id, ret);
3561 		return ret;
3562 	}
3563 
3564 	return 0;
3565 }
3566 
ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k * ar,struct ieee80211_link_sta * link_sta)3567 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3568 					      struct ieee80211_link_sta *link_sta)
3569 {
3570 	u32 bw;
3571 
3572 	switch (link_sta->bandwidth) {
3573 	case IEEE80211_STA_RX_BW_20:
3574 		bw = WMI_PEER_CHWIDTH_20MHZ;
3575 		break;
3576 	case IEEE80211_STA_RX_BW_40:
3577 		bw = WMI_PEER_CHWIDTH_40MHZ;
3578 		break;
3579 	case IEEE80211_STA_RX_BW_80:
3580 		bw = WMI_PEER_CHWIDTH_80MHZ;
3581 		break;
3582 	case IEEE80211_STA_RX_BW_160:
3583 		bw = WMI_PEER_CHWIDTH_160MHZ;
3584 		break;
3585 	case IEEE80211_STA_RX_BW_320:
3586 		bw = WMI_PEER_CHWIDTH_320MHZ;
3587 		break;
3588 	default:
3589 		ath12k_warn(ar->ab, "Invalid bandwidth %d for link station %pM\n",
3590 			    link_sta->bandwidth, link_sta->addr);
3591 		bw = WMI_PEER_CHWIDTH_20MHZ;
3592 		break;
3593 	}
3594 
3595 	return bw;
3596 }
3597 
ath12k_bss_assoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * bss_conf)3598 static void ath12k_bss_assoc(struct ath12k *ar,
3599 			     struct ath12k_link_vif *arvif,
3600 			     struct ieee80211_bss_conf *bss_conf)
3601 {
3602 	struct ath12k_vif *ahvif = arvif->ahvif;
3603 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
3604 	struct ath12k_wmi_vdev_up_params params = {};
3605 	struct ieee80211_link_sta *link_sta;
3606 	u8 link_id = bss_conf->link_id;
3607 	struct ath12k_link_sta *arsta;
3608 	struct ieee80211_sta *ap_sta;
3609 	struct ath12k_sta *ahsta;
3610 	struct ath12k_peer *peer;
3611 	bool is_auth = false;
3612 	u32 hemode = 0;
3613 	int ret;
3614 
3615 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3616 
3617 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
3618 					kzalloc(sizeof(*peer_arg), GFP_KERNEL);
3619 	if (!peer_arg)
3620 		return;
3621 
3622 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3623 		   "mac vdev %i link id %u assoc bssid %pM aid %d\n",
3624 		   arvif->vdev_id, link_id, arvif->bssid, ahvif->aid);
3625 
3626 	rcu_read_lock();
3627 
3628 	/* During ML connection, cfg.ap_addr has the MLD address. For
3629 	 * non-ML connection, it has the BSSID.
3630 	 */
3631 	ap_sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
3632 	if (!ap_sta) {
3633 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
3634 			    vif->cfg.ap_addr, arvif->vdev_id);
3635 		rcu_read_unlock();
3636 		return;
3637 	}
3638 
3639 	ahsta = ath12k_sta_to_ahsta(ap_sta);
3640 
3641 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
3642 				  ahsta->link[link_id]);
3643 	if (WARN_ON(!arsta)) {
3644 		rcu_read_unlock();
3645 		return;
3646 	}
3647 
3648 	link_sta = ath12k_mac_get_link_sta(arsta);
3649 	if (WARN_ON(!link_sta)) {
3650 		rcu_read_unlock();
3651 		return;
3652 	}
3653 
3654 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, false);
3655 
3656 	/* link_sta->he_cap must be protected by rcu_read_lock */
3657 	ret = ath12k_mac_vif_recalc_sta_he_txbf(ar, arvif, &link_sta->he_cap, &hemode);
3658 	if (ret) {
3659 		ath12k_warn(ar->ab, "failed to recalc he txbf for vdev %i on bss %pM: %d\n",
3660 			    arvif->vdev_id, bss_conf->bssid, ret);
3661 		rcu_read_unlock();
3662 		return;
3663 	}
3664 
3665 	rcu_read_unlock();
3666 
3667 	/* keep this before ath12k_wmi_send_peer_assoc_cmd() */
3668 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3669 					    WMI_VDEV_PARAM_SET_HEMU_MODE, hemode);
3670 	if (ret) {
3671 		ath12k_warn(ar->ab, "failed to submit vdev param txbf 0x%x: %d\n",
3672 			    hemode, ret);
3673 		return;
3674 	}
3675 
3676 	peer_arg->is_assoc = true;
3677 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
3678 	if (ret) {
3679 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
3680 			    bss_conf->bssid, arvif->vdev_id, ret);
3681 		return;
3682 	}
3683 
3684 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3685 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3686 			    bss_conf->bssid, arvif->vdev_id);
3687 		return;
3688 	}
3689 
3690 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
3691 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
3692 	if (ret) {
3693 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3694 			    arvif->vdev_id, ret);
3695 		return;
3696 	}
3697 
3698 	WARN_ON(arvif->is_up);
3699 
3700 	ahvif->aid = vif->cfg.aid;
3701 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3702 
3703 	params.vdev_id = arvif->vdev_id;
3704 	params.aid = ahvif->aid;
3705 	params.bssid = arvif->bssid;
3706 	params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
3707 	if (params.tx_bssid) {
3708 		params.nontx_profile_idx = bss_conf->bssid_index;
3709 		params.nontx_profile_cnt = 1 << bss_conf->bssid_indicator;
3710 	}
3711 	ret = ath12k_wmi_vdev_up(ar, &params);
3712 	if (ret) {
3713 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
3714 			    arvif->vdev_id, ret);
3715 		return;
3716 	}
3717 
3718 	arvif->is_up = true;
3719 	arvif->rekey_data.enable_offload = false;
3720 
3721 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3722 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3723 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3724 
3725 	spin_lock_bh(&ar->ab->base_lock);
3726 
3727 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
3728 	if (peer && peer->is_authorized)
3729 		is_auth = true;
3730 
3731 	spin_unlock_bh(&ar->ab->base_lock);
3732 
3733 	/* Authorize BSS Peer */
3734 	if (is_auth) {
3735 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
3736 						arvif->vdev_id,
3737 						WMI_PEER_AUTHORIZE,
3738 						1);
3739 		if (ret)
3740 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
3741 	}
3742 
3743 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
3744 					   &bss_conf->he_obss_pd);
3745 	if (ret)
3746 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
3747 			    arvif->vdev_id, ret);
3748 
3749 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
3750 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
3751 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
3752 		ath12k_mac_11d_scan_stop_all(ar->ab);
3753 }
3754 
ath12k_bss_disassoc(struct ath12k * ar,struct ath12k_link_vif * arvif)3755 static void ath12k_bss_disassoc(struct ath12k *ar,
3756 				struct ath12k_link_vif *arvif)
3757 {
3758 	int ret;
3759 
3760 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
3761 
3762 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3763 		   arvif->vdev_id, arvif->bssid);
3764 
3765 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
3766 	if (ret)
3767 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
3768 			    arvif->vdev_id, ret);
3769 
3770 	arvif->is_up = false;
3771 
3772 	memset(&arvif->rekey_data, 0, sizeof(arvif->rekey_data));
3773 
3774 	cancel_delayed_work(&arvif->connection_loss_work);
3775 }
3776 
ath12k_mac_get_rate_hw_value(int bitrate)3777 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
3778 {
3779 	u32 preamble;
3780 	u16 hw_value;
3781 	int rate;
3782 	size_t i;
3783 
3784 	if (ath12k_mac_bitrate_is_cck(bitrate))
3785 		preamble = WMI_RATE_PREAMBLE_CCK;
3786 	else
3787 		preamble = WMI_RATE_PREAMBLE_OFDM;
3788 
3789 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
3790 		if (ath12k_legacy_rates[i].bitrate != bitrate)
3791 			continue;
3792 
3793 		hw_value = ath12k_legacy_rates[i].hw_value;
3794 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
3795 
3796 		return rate;
3797 	}
3798 
3799 	return -EINVAL;
3800 }
3801 
ath12k_recalculate_mgmt_rate(struct ath12k * ar,struct ath12k_link_vif * arvif,struct cfg80211_chan_def * def)3802 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
3803 					 struct ath12k_link_vif *arvif,
3804 					 struct cfg80211_chan_def *def)
3805 {
3806 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
3807 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
3808 	const struct ieee80211_supported_band *sband;
3809 	struct ieee80211_bss_conf *bss_conf;
3810 	u8 basic_rate_idx;
3811 	int hw_rate_code;
3812 	u32 vdev_param;
3813 	u16 bitrate;
3814 	int ret;
3815 
3816 	lockdep_assert_wiphy(hw->wiphy);
3817 
3818 	bss_conf = ath12k_mac_get_link_bss_conf(arvif);
3819 	if (!bss_conf) {
3820 		ath12k_warn(ar->ab, "unable to access bss link conf in mgmt rate calc for vif %pM link %u\n",
3821 			    vif->addr, arvif->link_id);
3822 		return;
3823 	}
3824 
3825 	sband = hw->wiphy->bands[def->chan->band];
3826 	if (bss_conf->basic_rates)
3827 		basic_rate_idx = __ffs(bss_conf->basic_rates);
3828 	else
3829 		basic_rate_idx = 0;
3830 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
3831 
3832 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
3833 	if (hw_rate_code < 0) {
3834 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
3835 		return;
3836 	}
3837 
3838 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
3839 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
3840 					    hw_rate_code);
3841 	if (ret)
3842 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
3843 
3844 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
3845 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
3846 					    hw_rate_code);
3847 	if (ret)
3848 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
3849 }
3850 
ath12k_mac_init_arvif(struct ath12k_vif * ahvif,struct ath12k_link_vif * arvif,int link_id)3851 static void ath12k_mac_init_arvif(struct ath12k_vif *ahvif,
3852 				  struct ath12k_link_vif *arvif, int link_id)
3853 {
3854 	struct ath12k_hw *ah = ahvif->ah;
3855 	u8 _link_id;
3856 	int i;
3857 
3858 	lockdep_assert_wiphy(ah->hw->wiphy);
3859 
3860 	if (WARN_ON(!arvif))
3861 		return;
3862 
3863 	if (WARN_ON(link_id >= ATH12K_NUM_MAX_LINKS))
3864 		return;
3865 
3866 	if (link_id < 0)
3867 		_link_id = 0;
3868 	else
3869 		_link_id = link_id;
3870 
3871 	arvif->ahvif = ahvif;
3872 	arvif->link_id = _link_id;
3873 
3874 	/* Protects the datapath stats update on a per link basis */
3875 	spin_lock_init(&arvif->link_stats_lock);
3876 
3877 	INIT_LIST_HEAD(&arvif->list);
3878 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
3879 			  ath12k_mac_vif_sta_connection_loss_work);
3880 
3881 	arvif->num_stations = 0;
3882 
3883 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
3884 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
3885 		arvif->bitrate_mask.control[i].gi = NL80211_TXRATE_DEFAULT_GI;
3886 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
3887 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
3888 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
3889 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
3890 		memset(arvif->bitrate_mask.control[i].he_mcs, 0xff,
3891 		       sizeof(arvif->bitrate_mask.control[i].he_mcs));
3892 	}
3893 
3894 	/* Handle MLO related assignments */
3895 	if (link_id >= 0) {
3896 		rcu_assign_pointer(ahvif->link[arvif->link_id], arvif);
3897 		ahvif->links_map |= BIT(_link_id);
3898 	}
3899 
3900 	ath12k_generic_dbg(ATH12K_DBG_MAC,
3901 			   "mac init link arvif (link_id %d%s) for vif %pM. links_map 0x%x",
3902 			   _link_id, (link_id < 0) ? " deflink" : "", ahvif->vif->addr,
3903 			   ahvif->links_map);
3904 }
3905 
ath12k_mac_remove_link_interface(struct ieee80211_hw * hw,struct ath12k_link_vif * arvif)3906 static void ath12k_mac_remove_link_interface(struct ieee80211_hw *hw,
3907 					     struct ath12k_link_vif *arvif)
3908 {
3909 	struct ath12k_vif *ahvif = arvif->ahvif;
3910 	struct ath12k_hw *ah = hw->priv;
3911 	struct ath12k *ar = arvif->ar;
3912 	int ret;
3913 
3914 	lockdep_assert_wiphy(ah->hw->wiphy);
3915 
3916 	cancel_delayed_work_sync(&arvif->connection_loss_work);
3917 
3918 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac remove link interface (vdev %d link id %d)",
3919 		   arvif->vdev_id, arvif->link_id);
3920 
3921 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map) &&
3922 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
3923 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
3924 		ath12k_mac_11d_scan_stop(ar);
3925 
3926 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
3927 		ret = ath12k_peer_delete(ar, arvif->vdev_id, arvif->bssid);
3928 		if (ret)
3929 			ath12k_warn(ar->ab, "failed to submit AP self-peer removal on vdev %d link id %d: %d",
3930 				    arvif->vdev_id, arvif->link_id, ret);
3931 	}
3932 	ath12k_mac_vdev_delete(ar, arvif);
3933 }
3934 
ath12k_mac_assign_link_vif(struct ath12k_hw * ah,struct ieee80211_vif * vif,u8 link_id)3935 static struct ath12k_link_vif *ath12k_mac_assign_link_vif(struct ath12k_hw *ah,
3936 							  struct ieee80211_vif *vif,
3937 							  u8 link_id)
3938 {
3939 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
3940 	struct ath12k_link_vif *arvif;
3941 
3942 	lockdep_assert_wiphy(ah->hw->wiphy);
3943 
3944 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
3945 	if (arvif)
3946 		return arvif;
3947 
3948 	/* If this is the first link arvif being created for an ML VIF
3949 	 * use the preallocated deflink memory except for scan arvifs
3950 	 */
3951 	if (!ahvif->links_map && link_id < ATH12K_FIRST_SCAN_LINK) {
3952 		arvif = &ahvif->deflink;
3953 
3954 		if (vif->type == NL80211_IFTYPE_STATION)
3955 			arvif->is_sta_assoc_link = true;
3956 	} else {
3957 		arvif = kzalloc(sizeof(*arvif), GFP_KERNEL);
3958 		if (!arvif)
3959 			return NULL;
3960 	}
3961 
3962 	ath12k_mac_init_arvif(ahvif, arvif, link_id);
3963 
3964 	return arvif;
3965 }
3966 
ath12k_mac_unassign_link_vif(struct ath12k_link_vif * arvif)3967 static void ath12k_mac_unassign_link_vif(struct ath12k_link_vif *arvif)
3968 {
3969 	struct ath12k_vif *ahvif = arvif->ahvif;
3970 	struct ath12k_hw *ah = ahvif->ah;
3971 
3972 	lockdep_assert_wiphy(ah->hw->wiphy);
3973 
3974 	rcu_assign_pointer(ahvif->link[arvif->link_id], NULL);
3975 	synchronize_rcu();
3976 	ahvif->links_map &= ~BIT(arvif->link_id);
3977 
3978 	if (arvif != &ahvif->deflink)
3979 		kfree(arvif);
3980 	else
3981 		memset(arvif, 0, sizeof(*arvif));
3982 }
3983 
3984 static 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])3985 ath12k_mac_op_change_vif_links(struct ieee80211_hw *hw,
3986 			       struct ieee80211_vif *vif,
3987 			       u16 old_links, u16 new_links,
3988 			       struct ieee80211_bss_conf *ol[IEEE80211_MLD_MAX_NUM_LINKS])
3989 {
3990 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
3991 	unsigned long to_remove = old_links & ~new_links;
3992 	unsigned long to_add = ~old_links & new_links;
3993 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
3994 	struct ath12k_link_vif *arvif;
3995 	u8 link_id;
3996 
3997 	lockdep_assert_wiphy(hw->wiphy);
3998 
3999 	ath12k_generic_dbg(ATH12K_DBG_MAC,
4000 			   "mac vif link changed for MLD %pM old_links 0x%x new_links 0x%x\n",
4001 			   vif->addr, old_links, new_links);
4002 
4003 	for_each_set_bit(link_id, &to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
4004 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4005 		/* mac80211 wants to add link but driver already has the
4006 		 * link. This should not happen ideally.
4007 		 */
4008 		if (WARN_ON(arvif))
4009 			return -EINVAL;
4010 
4011 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
4012 		if (WARN_ON(!arvif))
4013 			return -EINVAL;
4014 	}
4015 
4016 	for_each_set_bit(link_id, &to_remove, IEEE80211_MLD_MAX_NUM_LINKS) {
4017 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4018 		if (WARN_ON(!arvif))
4019 			return -EINVAL;
4020 
4021 		if (!arvif->is_created)
4022 			continue;
4023 
4024 		if (WARN_ON(!arvif->ar))
4025 			return -EINVAL;
4026 
4027 		ath12k_mac_remove_link_interface(hw, arvif);
4028 		ath12k_mac_unassign_link_vif(arvif);
4029 	}
4030 
4031 	return 0;
4032 }
4033 
ath12k_mac_fils_discovery(struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info)4034 static int ath12k_mac_fils_discovery(struct ath12k_link_vif *arvif,
4035 				     struct ieee80211_bss_conf *info)
4036 {
4037 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
4038 	struct ath12k *ar = arvif->ar;
4039 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4040 	struct sk_buff *tmpl;
4041 	int ret;
4042 	u32 interval;
4043 	bool unsol_bcast_probe_resp_enabled = false;
4044 
4045 	if (info->fils_discovery.max_interval) {
4046 		interval = info->fils_discovery.max_interval;
4047 
4048 		tmpl = ieee80211_get_fils_discovery_tmpl(hw, vif);
4049 		if (tmpl)
4050 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
4051 							     tmpl);
4052 	} else if (info->unsol_bcast_probe_resp_interval) {
4053 		unsol_bcast_probe_resp_enabled = 1;
4054 		interval = info->unsol_bcast_probe_resp_interval;
4055 
4056 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(hw, vif);
4057 		if (tmpl)
4058 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
4059 							 tmpl);
4060 	} else { /* Disable */
4061 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
4062 	}
4063 
4064 	if (!tmpl) {
4065 		ath12k_warn(ar->ab,
4066 			    "mac vdev %i failed to retrieve %s template\n",
4067 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
4068 			    "unsolicited broadcast probe response" :
4069 			    "FILS discovery"));
4070 		return -EPERM;
4071 	}
4072 	kfree_skb(tmpl);
4073 
4074 	if (!ret)
4075 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
4076 						unsol_bcast_probe_resp_enabled);
4077 
4078 	return ret;
4079 }
4080 
ath12k_mac_vif_setup_ps(struct ath12k_link_vif * arvif)4081 static void ath12k_mac_vif_setup_ps(struct ath12k_link_vif *arvif)
4082 {
4083 	struct ath12k *ar = arvif->ar;
4084 	struct ieee80211_vif *vif = arvif->ahvif->vif;
4085 	struct ieee80211_conf *conf = &ath12k_ar_to_hw(ar)->conf;
4086 	enum wmi_sta_powersave_param param;
4087 	struct ieee80211_bss_conf *info;
4088 	enum wmi_sta_ps_mode psmode;
4089 	int ret;
4090 	int timeout;
4091 	bool enable_ps;
4092 
4093 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4094 
4095 	if (vif->type != NL80211_IFTYPE_STATION)
4096 		return;
4097 
4098 	enable_ps = arvif->ahvif->ps;
4099 	if (enable_ps) {
4100 		psmode = WMI_STA_PS_MODE_ENABLED;
4101 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
4102 
4103 		timeout = conf->dynamic_ps_timeout;
4104 		if (timeout == 0) {
4105 			info = ath12k_mac_get_link_bss_conf(arvif);
4106 			if (!info) {
4107 				ath12k_warn(ar->ab, "unable to access bss link conf in setup ps for vif %pM link %u\n",
4108 					    vif->addr, arvif->link_id);
4109 				return;
4110 			}
4111 
4112 			/* firmware doesn't like 0 */
4113 			timeout = ieee80211_tu_to_usec(info->beacon_int) / 1000;
4114 		}
4115 
4116 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
4117 						  timeout);
4118 		if (ret) {
4119 			ath12k_warn(ar->ab, "failed to set inactivity time for vdev %d: %i\n",
4120 				    arvif->vdev_id, ret);
4121 			return;
4122 		}
4123 	} else {
4124 		psmode = WMI_STA_PS_MODE_DISABLED;
4125 	}
4126 
4127 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d psmode %s\n",
4128 		   arvif->vdev_id, psmode ? "enable" : "disable");
4129 
4130 	ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, psmode);
4131 	if (ret)
4132 		ath12k_warn(ar->ab, "failed to set sta power save mode %d for vdev %d: %d\n",
4133 			    psmode, arvif->vdev_id, ret);
4134 }
4135 
ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u64 changed)4136 static void ath12k_mac_op_vif_cfg_changed(struct ieee80211_hw *hw,
4137 					  struct ieee80211_vif *vif,
4138 					  u64 changed)
4139 {
4140 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4141 	unsigned long links = ahvif->links_map;
4142 	struct ieee80211_vif_cfg *vif_cfg;
4143 	struct ieee80211_bss_conf *info;
4144 	struct ath12k_link_vif *arvif;
4145 	struct ieee80211_sta *sta;
4146 	struct ath12k_sta *ahsta;
4147 	struct ath12k *ar;
4148 	u8 link_id;
4149 
4150 	lockdep_assert_wiphy(hw->wiphy);
4151 
4152 	if (changed & BSS_CHANGED_SSID && vif->type == NL80211_IFTYPE_AP) {
4153 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4154 		if (vif->cfg.ssid_len)
4155 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4156 	}
4157 
4158 	if (changed & BSS_CHANGED_ASSOC) {
4159 		if (vif->cfg.assoc) {
4160 			/* only in station mode we can get here, so it's safe
4161 			 * to use ap_addr
4162 			 */
4163 			rcu_read_lock();
4164 			sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
4165 			if (!sta) {
4166 				rcu_read_unlock();
4167 				WARN_ONCE(1, "failed to find sta with addr %pM\n",
4168 					  vif->cfg.ap_addr);
4169 				return;
4170 			}
4171 
4172 			ahsta = ath12k_sta_to_ahsta(sta);
4173 			arvif = wiphy_dereference(hw->wiphy,
4174 						  ahvif->link[ahsta->assoc_link_id]);
4175 			rcu_read_unlock();
4176 
4177 			ar = arvif->ar;
4178 			/* there is no reason for which an assoc link's
4179 			 * bss info does not exist
4180 			 */
4181 			info = ath12k_mac_get_link_bss_conf(arvif);
4182 			ath12k_bss_assoc(ar, arvif, info);
4183 
4184 			/* exclude assoc link as it is done above */
4185 			links &= ~BIT(ahsta->assoc_link_id);
4186 		}
4187 
4188 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4189 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4190 			if (!arvif || !arvif->ar)
4191 				continue;
4192 
4193 			ar = arvif->ar;
4194 
4195 			if (vif->cfg.assoc) {
4196 				info = ath12k_mac_get_link_bss_conf(arvif);
4197 				if (!info)
4198 					continue;
4199 
4200 				ath12k_bss_assoc(ar, arvif, info);
4201 			} else {
4202 				ath12k_bss_disassoc(ar, arvif);
4203 			}
4204 		}
4205 	}
4206 
4207 	if (changed & BSS_CHANGED_PS) {
4208 		links = ahvif->links_map;
4209 		vif_cfg = &vif->cfg;
4210 
4211 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
4212 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4213 			if (!arvif || !arvif->ar)
4214 				continue;
4215 
4216 			ar = arvif->ar;
4217 
4218 			if (ar->ab->hw_params->supports_sta_ps) {
4219 				ahvif->ps = vif_cfg->ps;
4220 				ath12k_mac_vif_setup_ps(arvif);
4221 			}
4222 		}
4223 	}
4224 }
4225 
ath12k_mac_supports_tpc(struct ath12k * ar,struct ath12k_vif * ahvif,const struct cfg80211_chan_def * chandef)4226 static bool ath12k_mac_supports_tpc(struct ath12k *ar, struct ath12k_vif *ahvif,
4227 				    const struct cfg80211_chan_def *chandef)
4228 {
4229 	return ath12k_wmi_supports_6ghz_cc_ext(ar) &&
4230 		test_bit(WMI_TLV_SERVICE_EXT_TPC_REG_SUPPORT, ar->ab->wmi_ab.svc_map) &&
4231 		(ahvif->vdev_type == WMI_VDEV_TYPE_STA  ||
4232 		 ahvif->vdev_type == WMI_VDEV_TYPE_AP) &&
4233 		ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
4234 		chandef->chan &&
4235 		chandef->chan->band == NL80211_BAND_6GHZ;
4236 }
4237 
ath12k_mac_bss_info_changed(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_bss_conf * info,u64 changed)4238 static void ath12k_mac_bss_info_changed(struct ath12k *ar,
4239 					struct ath12k_link_vif *arvif,
4240 					struct ieee80211_bss_conf *info,
4241 					u64 changed)
4242 {
4243 	struct ath12k_vif *ahvif = arvif->ahvif;
4244 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
4245 	struct cfg80211_chan_def def;
4246 	u32 param_id, param_value;
4247 	enum nl80211_band band;
4248 	u32 vdev_param;
4249 	int mcast_rate;
4250 	u32 preamble;
4251 	u16 hw_value;
4252 	u16 bitrate;
4253 	int ret;
4254 	u8 rateidx;
4255 	u32 rate;
4256 
4257 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4258 
4259 	if (changed & BSS_CHANGED_BEACON_INT) {
4260 		arvif->beacon_interval = info->beacon_int;
4261 
4262 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
4263 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4264 						    param_id,
4265 						    arvif->beacon_interval);
4266 		if (ret)
4267 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
4268 				    arvif->vdev_id);
4269 		else
4270 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4271 				   "Beacon interval: %d set for VDEV: %d\n",
4272 				   arvif->beacon_interval, arvif->vdev_id);
4273 	}
4274 
4275 	if (changed & BSS_CHANGED_BEACON) {
4276 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
4277 		param_value = WMI_BEACON_BURST_MODE;
4278 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
4279 						param_value, ar->pdev->pdev_id);
4280 		if (ret)
4281 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
4282 				    arvif->vdev_id);
4283 		else
4284 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4285 				   "Set burst beacon mode for VDEV: %d\n",
4286 				   arvif->vdev_id);
4287 
4288 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
4289 		if (ret)
4290 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
4291 				    ret);
4292 	}
4293 
4294 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4295 		arvif->dtim_period = info->dtim_period;
4296 
4297 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
4298 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4299 						    param_id,
4300 						    arvif->dtim_period);
4301 
4302 		if (ret)
4303 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
4304 				    arvif->vdev_id, ret);
4305 		else
4306 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4307 				   "DTIM period: %d set for VDEV: %d\n",
4308 				   arvif->dtim_period, arvif->vdev_id);
4309 	}
4310 
4311 	if (changed & BSS_CHANGED_SSID &&
4312 	    vif->type == NL80211_IFTYPE_AP) {
4313 		ahvif->u.ap.ssid_len = vif->cfg.ssid_len;
4314 		if (vif->cfg.ssid_len)
4315 			memcpy(ahvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
4316 		ahvif->u.ap.hidden_ssid = info->hidden_ssid;
4317 	}
4318 
4319 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4320 		ether_addr_copy(arvif->bssid, info->bssid);
4321 
4322 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4323 		if (info->enable_beacon) {
4324 			ret = ath12k_mac_set_he_txbf_conf(arvif);
4325 			if (ret)
4326 				ath12k_warn(ar->ab,
4327 					    "failed to set HE TXBF config for vdev: %d\n",
4328 					    arvif->vdev_id);
4329 
4330 			ret = ath12k_mac_set_eht_txbf_conf(arvif);
4331 			if (ret)
4332 				ath12k_warn(ar->ab,
4333 					    "failed to set EHT TXBF config for vdev: %d\n",
4334 					    arvif->vdev_id);
4335 		}
4336 		ath12k_control_beaconing(arvif, info);
4337 
4338 		if (arvif->is_up && info->he_support &&
4339 		    info->he_oper.params) {
4340 			/* TODO: Extend to support 1024 BA Bitmap size */
4341 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4342 							    WMI_VDEV_PARAM_BA_MODE,
4343 							    WMI_BA_MODE_BUFFER_SIZE_256);
4344 			if (ret)
4345 				ath12k_warn(ar->ab,
4346 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
4347 					    arvif->vdev_id);
4348 
4349 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
4350 			param_value = info->he_oper.params;
4351 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4352 							    param_id, param_value);
4353 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4354 				   "he oper param: %x set for VDEV: %d\n",
4355 				   param_value, arvif->vdev_id);
4356 
4357 			if (ret)
4358 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
4359 					    param_value, arvif->vdev_id, ret);
4360 		}
4361 	}
4362 
4363 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4364 		u32 cts_prot;
4365 
4366 		cts_prot = !!(info->use_cts_prot);
4367 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
4368 
4369 		if (arvif->is_started) {
4370 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4371 							    param_id, cts_prot);
4372 			if (ret)
4373 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
4374 					    arvif->vdev_id);
4375 			else
4376 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
4377 					   cts_prot, arvif->vdev_id);
4378 		} else {
4379 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
4380 		}
4381 	}
4382 
4383 	if (changed & BSS_CHANGED_ERP_SLOT) {
4384 		u32 slottime;
4385 
4386 		if (info->use_short_slot)
4387 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4388 
4389 		else
4390 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4391 
4392 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
4393 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4394 						    param_id, slottime);
4395 		if (ret)
4396 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
4397 				    arvif->vdev_id);
4398 		else
4399 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4400 				   "Set slottime: %d for VDEV: %d\n",
4401 				   slottime, arvif->vdev_id);
4402 	}
4403 
4404 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4405 		u32 preamble;
4406 
4407 		if (info->use_short_preamble)
4408 			preamble = WMI_VDEV_PREAMBLE_SHORT;
4409 		else
4410 			preamble = WMI_VDEV_PREAMBLE_LONG;
4411 
4412 		param_id = WMI_VDEV_PARAM_PREAMBLE;
4413 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4414 						    param_id, preamble);
4415 		if (ret)
4416 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
4417 				    arvif->vdev_id);
4418 		else
4419 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4420 				   "Set preamble: %d for VDEV: %d\n",
4421 				   preamble, arvif->vdev_id);
4422 	}
4423 
4424 	if (changed & BSS_CHANGED_ASSOC) {
4425 		if (vif->cfg.assoc)
4426 			ath12k_bss_assoc(ar, arvif, info);
4427 		else
4428 			ath12k_bss_disassoc(ar, arvif);
4429 	}
4430 
4431 	if (changed & BSS_CHANGED_TXPOWER) {
4432 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4433 			   arvif->vdev_id, info->txpower);
4434 
4435 		arvif->txpower = info->txpower;
4436 		ath12k_mac_txpower_recalc(ar);
4437 	}
4438 
4439 	if (changed & BSS_CHANGED_MCAST_RATE &&
4440 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
4441 		band = def.chan->band;
4442 		mcast_rate = info->mcast_rate[band];
4443 
4444 		if (mcast_rate > 0) {
4445 			rateidx = mcast_rate - 1;
4446 		} else {
4447 			if (info->basic_rates)
4448 				rateidx = __ffs(info->basic_rates);
4449 			else
4450 				rateidx = 0;
4451 		}
4452 
4453 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP)
4454 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
4455 
4456 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
4457 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
4458 
4459 		if (ath12k_mac_bitrate_is_cck(bitrate))
4460 			preamble = WMI_RATE_PREAMBLE_CCK;
4461 		else
4462 			preamble = WMI_RATE_PREAMBLE_OFDM;
4463 
4464 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
4465 
4466 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
4467 			   "mac vdev %d mcast_rate %x\n",
4468 			   arvif->vdev_id, rate);
4469 
4470 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
4471 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4472 						    vdev_param, rate);
4473 		if (ret)
4474 			ath12k_warn(ar->ab,
4475 				    "failed to set mcast rate on vdev %i: %d\n",
4476 				    arvif->vdev_id,  ret);
4477 
4478 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
4479 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4480 						    vdev_param, rate);
4481 		if (ret)
4482 			ath12k_warn(ar->ab,
4483 				    "failed to set bcast rate on vdev %i: %d\n",
4484 				    arvif->vdev_id,  ret);
4485 	}
4486 
4487 	if (changed & BSS_CHANGED_BASIC_RATES &&
4488 	    !ath12k_mac_vif_link_chan(vif, arvif->link_id, &def))
4489 		ath12k_recalculate_mgmt_rate(ar, arvif, &def);
4490 
4491 	if (changed & BSS_CHANGED_TWT) {
4492 		if (info->twt_requester || info->twt_responder)
4493 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
4494 		else
4495 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
4496 	}
4497 
4498 	if (changed & BSS_CHANGED_HE_OBSS_PD)
4499 		ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
4500 					     &info->he_obss_pd);
4501 
4502 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
4503 		if (vif->type == NL80211_IFTYPE_AP) {
4504 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4505 							    arvif->vdev_id,
4506 							    info->he_bss_color.color,
4507 							    ATH12K_BSS_COLOR_AP_PERIODS,
4508 							    info->he_bss_color.enabled);
4509 			if (ret)
4510 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
4511 					    arvif->vdev_id,  ret);
4512 		} else if (vif->type == NL80211_IFTYPE_STATION) {
4513 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
4514 									  arvif->vdev_id,
4515 									  1);
4516 			if (ret)
4517 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
4518 					    arvif->vdev_id,  ret);
4519 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
4520 							    arvif->vdev_id,
4521 							    0,
4522 							    ATH12K_BSS_COLOR_STA_PERIODS,
4523 							    1);
4524 			if (ret)
4525 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
4526 					    arvif->vdev_id,  ret);
4527 		}
4528 	}
4529 
4530 	ath12k_mac_fils_discovery(arvif, info);
4531 }
4532 
ath12k_ahvif_get_link_cache(struct ath12k_vif * ahvif,u8 link_id)4533 static struct ath12k_vif_cache *ath12k_ahvif_get_link_cache(struct ath12k_vif *ahvif,
4534 							    u8 link_id)
4535 {
4536 	if (!ahvif->cache[link_id]) {
4537 		ahvif->cache[link_id] = kzalloc(sizeof(*ahvif->cache[0]), GFP_KERNEL);
4538 		if (ahvif->cache[link_id])
4539 			INIT_LIST_HEAD(&ahvif->cache[link_id]->key_conf.list);
4540 	}
4541 
4542 	return ahvif->cache[link_id];
4543 }
4544 
ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache * cache)4545 static void ath12k_ahvif_put_link_key_cache(struct ath12k_vif_cache *cache)
4546 {
4547 	struct ath12k_key_conf *key_conf, *tmp;
4548 
4549 	if (!cache || list_empty(&cache->key_conf.list))
4550 		return;
4551 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
4552 		list_del(&key_conf->list);
4553 		kfree(key_conf);
4554 	}
4555 }
4556 
ath12k_ahvif_put_link_cache(struct ath12k_vif * ahvif,u8 link_id)4557 static void ath12k_ahvif_put_link_cache(struct ath12k_vif *ahvif, u8 link_id)
4558 {
4559 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
4560 		return;
4561 
4562 	ath12k_ahvif_put_link_key_cache(ahvif->cache[link_id]);
4563 	kfree(ahvif->cache[link_id]);
4564 	ahvif->cache[link_id] = NULL;
4565 }
4566 
ath12k_mac_op_link_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)4567 static void ath12k_mac_op_link_info_changed(struct ieee80211_hw *hw,
4568 					    struct ieee80211_vif *vif,
4569 					    struct ieee80211_bss_conf *info,
4570 					    u64 changed)
4571 {
4572 	struct ath12k *ar;
4573 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4574 	struct ath12k_vif_cache *cache;
4575 	struct ath12k_link_vif *arvif;
4576 	u8 link_id = info->link_id;
4577 
4578 	lockdep_assert_wiphy(hw->wiphy);
4579 
4580 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
4581 
4582 	/* if the vdev is not created on a certain radio,
4583 	 * cache the info to be updated later on vdev creation
4584 	 */
4585 
4586 	if (!arvif || !arvif->is_created) {
4587 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
4588 		if (!cache)
4589 			return;
4590 
4591 		cache->bss_conf_changed |= changed;
4592 
4593 		return;
4594 	}
4595 
4596 	ar = arvif->ar;
4597 
4598 	ath12k_mac_bss_info_changed(ar, arvif, info, changed);
4599 }
4600 
4601 static struct ath12k*
ath12k_mac_select_scan_device(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 center_freq)4602 ath12k_mac_select_scan_device(struct ieee80211_hw *hw,
4603 			      struct ieee80211_vif *vif,
4604 			      u32 center_freq)
4605 {
4606 	struct ath12k_hw *ah = hw->priv;
4607 	enum nl80211_band band;
4608 	struct ath12k *ar;
4609 	int i;
4610 
4611 	if (ah->num_radio == 1)
4612 		return ah->radio;
4613 
4614 	/* Currently mac80211 supports splitting scan requests into
4615 	 * multiple scan requests per band.
4616 	 * Loop through first channel and determine the scan radio
4617 	 * TODO: There could be 5 GHz low/high channels in that case
4618 	 * split the hw request and perform multiple scans
4619 	 */
4620 
4621 	if (center_freq < ATH12K_MIN_5GHZ_FREQ)
4622 		band = NL80211_BAND_2GHZ;
4623 	else if (center_freq < ATH12K_MIN_6GHZ_FREQ)
4624 		band = NL80211_BAND_5GHZ;
4625 	else
4626 		band = NL80211_BAND_6GHZ;
4627 
4628 	for_each_ar(ah, ar, i) {
4629 		if (ar->mac.sbands[band].channels &&
4630 		    center_freq >= KHZ_TO_MHZ(ar->freq_range.start_freq) &&
4631 		    center_freq <= KHZ_TO_MHZ(ar->freq_range.end_freq))
4632 			return ar;
4633 	}
4634 
4635 	return NULL;
4636 }
4637 
__ath12k_mac_scan_finish(struct ath12k * ar)4638 void __ath12k_mac_scan_finish(struct ath12k *ar)
4639 {
4640 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
4641 
4642 	lockdep_assert_held(&ar->data_lock);
4643 
4644 	switch (ar->scan.state) {
4645 	case ATH12K_SCAN_IDLE:
4646 		break;
4647 	case ATH12K_SCAN_RUNNING:
4648 	case ATH12K_SCAN_ABORTING:
4649 		if (ar->scan.is_roc && ar->scan.roc_notify)
4650 			ieee80211_remain_on_channel_expired(hw);
4651 		fallthrough;
4652 	case ATH12K_SCAN_STARTING:
4653 		cancel_delayed_work(&ar->scan.timeout);
4654 		complete_all(&ar->scan.completed);
4655 		wiphy_work_queue(ar->ah->hw->wiphy, &ar->scan.vdev_clean_wk);
4656 		break;
4657 	}
4658 }
4659 
ath12k_mac_scan_finish(struct ath12k * ar)4660 void ath12k_mac_scan_finish(struct ath12k *ar)
4661 {
4662 	spin_lock_bh(&ar->data_lock);
4663 	__ath12k_mac_scan_finish(ar);
4664 	spin_unlock_bh(&ar->data_lock);
4665 }
4666 
ath12k_scan_stop(struct ath12k * ar)4667 static int ath12k_scan_stop(struct ath12k *ar)
4668 {
4669 	struct ath12k_wmi_scan_cancel_arg arg = {
4670 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
4671 		.scan_id = ATH12K_SCAN_ID,
4672 	};
4673 	int ret;
4674 
4675 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4676 
4677 	/* TODO: Fill other STOP Params */
4678 	arg.pdev_id = ar->pdev->pdev_id;
4679 
4680 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
4681 	if (ret) {
4682 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
4683 		goto out;
4684 	}
4685 
4686 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
4687 	if (ret == 0) {
4688 		ath12k_warn(ar->ab,
4689 			    "failed to receive scan abort comple: timed out\n");
4690 		ret = -ETIMEDOUT;
4691 	} else if (ret > 0) {
4692 		ret = 0;
4693 	}
4694 
4695 out:
4696 	/* Scan state should be updated in scan completion worker but in
4697 	 * case firmware fails to deliver the event (for whatever reason)
4698 	 * it is desired to clean up scan state anyway. Firmware may have
4699 	 * just dropped the scan completion event delivery due to transport
4700 	 * pipe being overflown with data and/or it can recover on its own
4701 	 * before next scan request is submitted.
4702 	 */
4703 	spin_lock_bh(&ar->data_lock);
4704 	if (ret)
4705 		__ath12k_mac_scan_finish(ar);
4706 	spin_unlock_bh(&ar->data_lock);
4707 
4708 	return ret;
4709 }
4710 
ath12k_scan_abort(struct ath12k * ar)4711 static void ath12k_scan_abort(struct ath12k *ar)
4712 {
4713 	int ret;
4714 
4715 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4716 
4717 	spin_lock_bh(&ar->data_lock);
4718 
4719 	switch (ar->scan.state) {
4720 	case ATH12K_SCAN_IDLE:
4721 		/* This can happen if timeout worker kicked in and called
4722 		 * abortion while scan completion was being processed.
4723 		 */
4724 		break;
4725 	case ATH12K_SCAN_STARTING:
4726 	case ATH12K_SCAN_ABORTING:
4727 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
4728 			    ar->scan.state);
4729 		break;
4730 	case ATH12K_SCAN_RUNNING:
4731 		ar->scan.state = ATH12K_SCAN_ABORTING;
4732 		spin_unlock_bh(&ar->data_lock);
4733 
4734 		ret = ath12k_scan_stop(ar);
4735 		if (ret)
4736 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
4737 
4738 		spin_lock_bh(&ar->data_lock);
4739 		break;
4740 	}
4741 
4742 	spin_unlock_bh(&ar->data_lock);
4743 }
4744 
ath12k_scan_timeout_work(struct work_struct * work)4745 static void ath12k_scan_timeout_work(struct work_struct *work)
4746 {
4747 	struct ath12k *ar = container_of(work, struct ath12k,
4748 					 scan.timeout.work);
4749 
4750 	wiphy_lock(ath12k_ar_to_hw(ar)->wiphy);
4751 	ath12k_scan_abort(ar);
4752 	wiphy_unlock(ath12k_ar_to_hw(ar)->wiphy);
4753 }
4754 
ath12k_mac_scan_send_complete(struct ath12k * ar,struct cfg80211_scan_info * info)4755 static void ath12k_mac_scan_send_complete(struct ath12k *ar,
4756 					  struct cfg80211_scan_info *info)
4757 {
4758 	struct ath12k_hw *ah = ar->ah;
4759 	struct ath12k *partner_ar;
4760 	int i;
4761 
4762 	lockdep_assert_wiphy(ah->hw->wiphy);
4763 
4764 	for_each_ar(ah, partner_ar, i)
4765 		if (partner_ar != ar &&
4766 		    partner_ar->scan.state == ATH12K_SCAN_RUNNING)
4767 			return;
4768 
4769 	ieee80211_scan_completed(ah->hw, info);
4770 }
4771 
ath12k_scan_vdev_clean_work(struct wiphy * wiphy,struct wiphy_work * work)4772 static void ath12k_scan_vdev_clean_work(struct wiphy *wiphy, struct wiphy_work *work)
4773 {
4774 	struct ath12k *ar = container_of(work, struct ath12k,
4775 					 scan.vdev_clean_wk);
4776 	struct ath12k_hw *ah = ar->ah;
4777 	struct ath12k_link_vif *arvif;
4778 
4779 	lockdep_assert_wiphy(wiphy);
4780 
4781 	arvif = ar->scan.arvif;
4782 
4783 	/* The scan vdev has already been deleted. This can occur when a
4784 	 * new scan request is made on the same vif with a different
4785 	 * frequency, causing the scan arvif to move from one radio to
4786 	 * another. Or, scan was abrupted and via remove interface, the
4787 	 * arvif is already deleted. Alternatively, if the scan vdev is not
4788 	 * being used as an actual vdev, then do not delete it.
4789 	 */
4790 	if (!arvif || arvif->is_started)
4791 		goto work_complete;
4792 
4793 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac clean scan vdev (link id %u)",
4794 		   arvif->link_id);
4795 
4796 	ath12k_mac_remove_link_interface(ah->hw, arvif);
4797 	ath12k_mac_unassign_link_vif(arvif);
4798 
4799 work_complete:
4800 	spin_lock_bh(&ar->data_lock);
4801 	ar->scan.arvif = NULL;
4802 	if (!ar->scan.is_roc) {
4803 		struct cfg80211_scan_info info = {
4804 			.aborted = ((ar->scan.state ==
4805 				    ATH12K_SCAN_ABORTING) ||
4806 				    (ar->scan.state ==
4807 				    ATH12K_SCAN_STARTING)),
4808 		};
4809 
4810 		ath12k_mac_scan_send_complete(ar, &info);
4811 	}
4812 
4813 	ar->scan.state = ATH12K_SCAN_IDLE;
4814 	ar->scan_channel = NULL;
4815 	ar->scan.roc_freq = 0;
4816 	spin_unlock_bh(&ar->data_lock);
4817 }
4818 
ath12k_start_scan(struct ath12k * ar,struct ath12k_wmi_scan_req_arg * arg)4819 static int ath12k_start_scan(struct ath12k *ar,
4820 			     struct ath12k_wmi_scan_req_arg *arg)
4821 {
4822 	int ret;
4823 
4824 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
4825 
4826 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
4827 	if (ret)
4828 		return ret;
4829 
4830 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
4831 	if (ret == 0) {
4832 		ret = ath12k_scan_stop(ar);
4833 		if (ret)
4834 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
4835 
4836 		return -ETIMEDOUT;
4837 	}
4838 
4839 	/* If we failed to start the scan, return error code at
4840 	 * this point.  This is probably due to some issue in the
4841 	 * firmware, but no need to wedge the driver due to that...
4842 	 */
4843 	spin_lock_bh(&ar->data_lock);
4844 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
4845 		spin_unlock_bh(&ar->data_lock);
4846 		return -EINVAL;
4847 	}
4848 	spin_unlock_bh(&ar->data_lock);
4849 
4850 	return 0;
4851 }
4852 
ath12k_mac_get_fw_stats(struct ath12k * ar,struct ath12k_fw_stats_req_params * param)4853 int ath12k_mac_get_fw_stats(struct ath12k *ar,
4854 			    struct ath12k_fw_stats_req_params *param)
4855 {
4856 	struct ath12k_base *ab = ar->ab;
4857 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
4858 	unsigned long time_left;
4859 	int ret;
4860 
4861 	guard(mutex)(&ah->hw_mutex);
4862 
4863 	if (ah->state != ATH12K_HW_STATE_ON)
4864 		return -ENETDOWN;
4865 
4866 	ath12k_fw_stats_reset(ar);
4867 
4868 	reinit_completion(&ar->fw_stats_complete);
4869 	reinit_completion(&ar->fw_stats_done);
4870 
4871 	ret = ath12k_wmi_send_stats_request_cmd(ar, param->stats_id,
4872 						param->vdev_id, param->pdev_id);
4873 	if (ret) {
4874 		ath12k_warn(ab, "failed to request fw stats: %d\n", ret);
4875 		return ret;
4876 	}
4877 
4878 	ath12k_dbg(ab, ATH12K_DBG_WMI,
4879 		   "get fw stat pdev id %d vdev id %d stats id 0x%x\n",
4880 		   param->pdev_id, param->vdev_id, param->stats_id);
4881 
4882 	time_left = wait_for_completion_timeout(&ar->fw_stats_complete, 1 * HZ);
4883 	if (!time_left) {
4884 		ath12k_warn(ab, "time out while waiting for get fw stats\n");
4885 		return -ETIMEDOUT;
4886 	}
4887 
4888 	/* Firmware sends WMI_UPDATE_STATS_EVENTID back-to-back
4889 	 * when stats data buffer limit is reached. fw_stats_complete
4890 	 * is completed once host receives first event from firmware, but
4891 	 * still there could be more events following. Below is to wait
4892 	 * until firmware completes sending all the events.
4893 	 */
4894 	time_left = wait_for_completion_timeout(&ar->fw_stats_done, 3 * HZ);
4895 	if (!time_left) {
4896 		ath12k_warn(ab, "time out while waiting for fw stats done\n");
4897 		return -ETIMEDOUT;
4898 	}
4899 
4900 	return 0;
4901 }
4902 
ath12k_mac_op_get_txpower(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,int * dbm)4903 static int ath12k_mac_op_get_txpower(struct ieee80211_hw *hw,
4904 				     struct ieee80211_vif *vif,
4905 				     unsigned int link_id,
4906 				     int *dbm)
4907 {
4908 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
4909 	struct ath12k_fw_stats_req_params params = {};
4910 	struct ath12k_fw_stats_pdev *pdev;
4911 	struct ath12k_hw *ah = hw->priv;
4912 	struct ath12k_link_vif *arvif;
4913 	struct ath12k_base *ab;
4914 	struct ath12k *ar;
4915 	int ret;
4916 
4917 	/* Final Tx power is minimum of Target Power, CTL power, Regulatory
4918 	 * Power, PSD EIRP Power. We just know the Regulatory power from the
4919 	 * regulatory rules obtained. FW knows all these power and sets the min
4920 	 * of these. Hence, we request the FW pdev stats in which FW reports
4921 	 * the minimum of all vdev's channel Tx power.
4922 	 */
4923 	lockdep_assert_wiphy(hw->wiphy);
4924 
4925 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4926 	if (!arvif || !arvif->ar)
4927 		return -EINVAL;
4928 
4929 	ar = arvif->ar;
4930 	ab = ar->ab;
4931 	if (ah->state != ATH12K_HW_STATE_ON)
4932 		goto err_fallback;
4933 
4934 	if (test_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags))
4935 		return -EAGAIN;
4936 
4937 	/* Limit the requests to Firmware for fetching the tx power */
4938 	if (ar->chan_tx_pwr != ATH12K_PDEV_TX_POWER_INVALID &&
4939 	    time_before(jiffies,
4940 			msecs_to_jiffies(ATH12K_PDEV_TX_POWER_REFRESH_TIME_MSECS) +
4941 					 ar->last_tx_power_update))
4942 		goto send_tx_power;
4943 
4944 	params.pdev_id = ar->pdev->pdev_id;
4945 	params.vdev_id = arvif->vdev_id;
4946 	params.stats_id = WMI_REQUEST_PDEV_STAT;
4947 	ret = ath12k_mac_get_fw_stats(ar, &params);
4948 	if (ret) {
4949 		ath12k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
4950 		goto err_fallback;
4951 	}
4952 
4953 	spin_lock_bh(&ar->data_lock);
4954 	pdev = list_first_entry_or_null(&ar->fw_stats.pdevs,
4955 					struct ath12k_fw_stats_pdev, list);
4956 	if (!pdev) {
4957 		spin_unlock_bh(&ar->data_lock);
4958 		goto err_fallback;
4959 	}
4960 
4961 	/* tx power reported by firmware is in units of 0.5 dBm */
4962 	ar->chan_tx_pwr = pdev->chan_tx_power / 2;
4963 	spin_unlock_bh(&ar->data_lock);
4964 	ar->last_tx_power_update = jiffies;
4965 
4966 send_tx_power:
4967 	*dbm = ar->chan_tx_pwr;
4968 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower fetched from firmware %d dBm\n",
4969 		   *dbm);
4970 	return 0;
4971 
4972 err_fallback:
4973 	/* We didn't get txpower from FW. Hence, relying on vif->bss_conf.txpower */
4974 	*dbm = vif->bss_conf.txpower;
4975 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower from firmware NaN, reported %d dBm\n",
4976 		   *dbm);
4977 	return 0;
4978 }
4979 
4980 static u8
ath12k_mac_find_link_id_by_ar(struct ath12k_vif * ahvif,struct ath12k * ar)4981 ath12k_mac_find_link_id_by_ar(struct ath12k_vif *ahvif, struct ath12k *ar)
4982 {
4983 	struct ath12k_link_vif *arvif;
4984 	struct ath12k_hw *ah = ahvif->ah;
4985 	unsigned long links = ahvif->links_map;
4986 	unsigned long scan_links_map;
4987 	u8 link_id;
4988 
4989 	lockdep_assert_wiphy(ah->hw->wiphy);
4990 
4991 	for_each_set_bit(link_id, &links, ATH12K_NUM_MAX_LINKS) {
4992 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
4993 
4994 		if (!arvif || !arvif->is_created)
4995 			continue;
4996 
4997 		if (ar == arvif->ar)
4998 			return link_id;
4999 	}
5000 
5001 	/* input ar is not assigned to any of the links of ML VIF, use next
5002 	 * available scan link for scan vdev creation. There are cases where
5003 	 * single scan req needs to be split in driver and initiate separate
5004 	 * scan requests to firmware based on device.
5005 	 */
5006 
5007 	 /* Unset all non-scan links (0-14) of scan_links_map so that ffs() will
5008 	  * choose an available link among scan links (i.e link id >= 15)
5009 	  */
5010 	scan_links_map = ~ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
5011 	if (scan_links_map)
5012 		return __ffs(scan_links_map);
5013 
5014 	return ATH12K_FIRST_SCAN_LINK;
5015 }
5016 
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)5017 static int ath12k_mac_initiate_hw_scan(struct ieee80211_hw *hw,
5018 				       struct ieee80211_vif *vif,
5019 				       struct ieee80211_scan_request *hw_req,
5020 				       int n_channels,
5021 				       struct ieee80211_channel **chan_list,
5022 				       struct ath12k *ar)
5023 {
5024 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5025 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5026 	struct ath12k_link_vif *arvif;
5027 	struct cfg80211_scan_request *req = &hw_req->req;
5028 	struct ath12k_wmi_scan_req_arg *arg = NULL;
5029 	u8 link_id;
5030 	int ret;
5031 	int i;
5032 	bool create = true;
5033 
5034 	lockdep_assert_wiphy(hw->wiphy);
5035 
5036 	arvif = &ahvif->deflink;
5037 
5038 	/* check if any of the links of ML VIF is already started on
5039 	 * radio(ar) corresponding to given scan frequency and use it,
5040 	 * if not use scan link (link id >= 15) for scan purpose.
5041 	 */
5042 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
5043 	/* All scan links are occupied. ideally this shouldn't happen as
5044 	 * mac80211 won't schedule scan for same band until ongoing scan is
5045 	 * completed, don't try to exceed max links just in case if it happens.
5046 	 */
5047 	if (link_id >= ATH12K_NUM_MAX_LINKS)
5048 		return -EBUSY;
5049 
5050 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5051 
5052 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac link ID %d selected for scan",
5053 		   arvif->link_id);
5054 
5055 	/* If the vif is already assigned to a specific vdev of an ar,
5056 	 * check whether its already started, vdev which is started
5057 	 * are not allowed to switch to a new radio.
5058 	 * If the vdev is not started, but was earlier created on a
5059 	 * different ar, delete that vdev and create a new one. We don't
5060 	 * delete at the scan stop as an optimization to avoid redundant
5061 	 * delete-create vdev's for the same ar, in case the request is
5062 	 * always on the same band for the vif
5063 	 */
5064 	if (arvif->is_created) {
5065 		if (WARN_ON(!arvif->ar))
5066 			return -EINVAL;
5067 
5068 		if (ar != arvif->ar && arvif->is_started)
5069 			return -EINVAL;
5070 
5071 		if (ar != arvif->ar) {
5072 			ath12k_mac_remove_link_interface(hw, arvif);
5073 			ath12k_mac_unassign_link_vif(arvif);
5074 		} else {
5075 			create = false;
5076 		}
5077 	}
5078 
5079 	if (create) {
5080 		/* Previous arvif would've been cleared in radio switch block
5081 		 * above, assign arvif again for create.
5082 		 */
5083 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
5084 
5085 		ret = ath12k_mac_vdev_create(ar, arvif);
5086 		if (ret) {
5087 			ath12k_warn(ar->ab, "unable to create scan vdev %d\n", ret);
5088 			return -EINVAL;
5089 		}
5090 	}
5091 
5092 	spin_lock_bh(&ar->data_lock);
5093 	switch (ar->scan.state) {
5094 	case ATH12K_SCAN_IDLE:
5095 		reinit_completion(&ar->scan.started);
5096 		reinit_completion(&ar->scan.completed);
5097 		ar->scan.state = ATH12K_SCAN_STARTING;
5098 		ar->scan.is_roc = false;
5099 		ar->scan.arvif = arvif;
5100 		ret = 0;
5101 		break;
5102 	case ATH12K_SCAN_STARTING:
5103 	case ATH12K_SCAN_RUNNING:
5104 	case ATH12K_SCAN_ABORTING:
5105 		ret = -EBUSY;
5106 		break;
5107 	}
5108 	spin_unlock_bh(&ar->data_lock);
5109 
5110 	if (ret)
5111 		goto exit;
5112 
5113 	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
5114 	if (!arg) {
5115 		ret = -ENOMEM;
5116 		goto exit;
5117 	}
5118 
5119 	ath12k_wmi_start_scan_init(ar, arg);
5120 	arg->vdev_id = arvif->vdev_id;
5121 	arg->scan_id = ATH12K_SCAN_ID;
5122 
5123 	if (req->ie_len) {
5124 		arg->extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
5125 		if (!arg->extraie.ptr) {
5126 			ret = -ENOMEM;
5127 			goto exit;
5128 		}
5129 		arg->extraie.len = req->ie_len;
5130 	}
5131 
5132 	if (req->n_ssids) {
5133 		arg->num_ssids = req->n_ssids;
5134 		for (i = 0; i < arg->num_ssids; i++)
5135 			arg->ssid[i] = req->ssids[i];
5136 	} else {
5137 		arg->scan_f_passive = 1;
5138 	}
5139 
5140 	if (n_channels) {
5141 		arg->num_chan = n_channels;
5142 		arg->chan_list = kcalloc(arg->num_chan, sizeof(*arg->chan_list),
5143 					 GFP_KERNEL);
5144 		if (!arg->chan_list) {
5145 			ret = -ENOMEM;
5146 			goto exit;
5147 		}
5148 
5149 		for (i = 0; i < arg->num_chan; i++)
5150 			arg->chan_list[i] = chan_list[i]->center_freq;
5151 	}
5152 
5153 	ret = ath12k_start_scan(ar, arg);
5154 	if (ret) {
5155 		if (ret == -EBUSY)
5156 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5157 				   "scan engine is busy 11d state %d\n", ar->state_11d);
5158 		else
5159 			ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
5160 
5161 		spin_lock_bh(&ar->data_lock);
5162 		ar->scan.state = ATH12K_SCAN_IDLE;
5163 		spin_unlock_bh(&ar->data_lock);
5164 		goto exit;
5165 	}
5166 
5167 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac scan started");
5168 
5169 	/* Add a margin to account for event/command processing */
5170 	ieee80211_queue_delayed_work(ath12k_ar_to_hw(ar), &ar->scan.timeout,
5171 				     msecs_to_jiffies(arg->max_scan_time +
5172 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
5173 
5174 exit:
5175 	if (arg) {
5176 		kfree(arg->chan_list);
5177 		kfree(arg->extraie.ptr);
5178 		kfree(arg);
5179 	}
5180 
5181 	if (ar->state_11d == ATH12K_11D_PREPARING &&
5182 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5183 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE)
5184 		ath12k_mac_11d_scan_start(ar, arvif->vdev_id);
5185 
5186 	return ret;
5187 }
5188 
ath12k_mac_op_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)5189 static int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
5190 				 struct ieee80211_vif *vif,
5191 				 struct ieee80211_scan_request *hw_req)
5192 {
5193 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5194 	struct ieee80211_channel **chan_list, *chan;
5195 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
5196 	unsigned long links_map, link_id;
5197 	struct ath12k_link_vif *arvif;
5198 	struct ath12k *ar, *scan_ar;
5199 	int i, j, ret = 0;
5200 
5201 	lockdep_assert_wiphy(hw->wiphy);
5202 
5203 	chan_list = kcalloc(hw_req->req.n_channels, sizeof(*chan_list), GFP_KERNEL);
5204 	if (!chan_list)
5205 		return -ENOMEM;
5206 
5207 	/* There could be channels that belong to multiple underlying radio
5208 	 * in same scan request as mac80211 sees it as single band. In that
5209 	 * case split the hw_req based on frequency range and schedule scans to
5210 	 * corresponding radio.
5211 	 */
5212 	for_each_ar(ah, ar, i) {
5213 		int n_chans = 0;
5214 
5215 		for (j = 0; j < hw_req->req.n_channels; j++) {
5216 			chan = hw_req->req.channels[j];
5217 			scan_ar = ath12k_mac_select_scan_device(hw, vif,
5218 								chan->center_freq);
5219 			if (!scan_ar) {
5220 				ath12k_hw_warn(ah, "unable to select scan device for freq %d\n",
5221 					       chan->center_freq);
5222 				ret = -EINVAL;
5223 				goto abort;
5224 			}
5225 			if (ar != scan_ar)
5226 				continue;
5227 
5228 			chan_list[n_chans++] = chan;
5229 		}
5230 		if (n_chans) {
5231 			ret = ath12k_mac_initiate_hw_scan(hw, vif, hw_req, n_chans,
5232 							  chan_list, ar);
5233 			if (ret)
5234 				goto abort;
5235 		}
5236 	}
5237 abort:
5238 	/* If any of the parallel scans initiated fails, abort all and
5239 	 * remove the scan interfaces created. Return complete scan
5240 	 * failure as mac80211 assumes this as single scan request.
5241 	 */
5242 	if (ret) {
5243 		ath12k_hw_warn(ah, "Scan failed %d , cleanup all scan vdevs\n", ret);
5244 		links_map = ahvif->links_map;
5245 		for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5246 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5247 			if (!arvif)
5248 				continue;
5249 
5250 			ar = arvif->ar;
5251 			if (ar->scan.arvif == arvif) {
5252 				wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
5253 				spin_lock_bh(&ar->data_lock);
5254 				ar->scan.arvif = NULL;
5255 				ar->scan.state = ATH12K_SCAN_IDLE;
5256 				ar->scan_channel = NULL;
5257 				ar->scan.roc_freq = 0;
5258 				spin_unlock_bh(&ar->data_lock);
5259 			}
5260 			if (link_id >= ATH12K_FIRST_SCAN_LINK) {
5261 				ath12k_mac_remove_link_interface(hw, arvif);
5262 				ath12k_mac_unassign_link_vif(arvif);
5263 			}
5264 		}
5265 	}
5266 	kfree(chan_list);
5267 	return ret;
5268 }
5269 
ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5270 static void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
5271 					 struct ieee80211_vif *vif)
5272 {
5273 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5274 	unsigned long link_id, links_map = ahvif->links_map;
5275 	struct ath12k_link_vif *arvif;
5276 	struct ath12k *ar;
5277 
5278 	lockdep_assert_wiphy(hw->wiphy);
5279 
5280 	for_each_set_bit(link_id, &links_map, ATH12K_NUM_MAX_LINKS) {
5281 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5282 		if (!arvif || arvif->is_started)
5283 			continue;
5284 
5285 		ar = arvif->ar;
5286 
5287 		ath12k_scan_abort(ar);
5288 
5289 		cancel_delayed_work_sync(&ar->scan.timeout);
5290 	}
5291 }
5292 
ath12k_install_key(struct ath12k_link_vif * arvif,struct ieee80211_key_conf * key,enum set_key_cmd cmd,const u8 * macaddr,u32 flags)5293 static int ath12k_install_key(struct ath12k_link_vif *arvif,
5294 			      struct ieee80211_key_conf *key,
5295 			      enum set_key_cmd cmd,
5296 			      const u8 *macaddr, u32 flags)
5297 {
5298 	int ret;
5299 	struct ath12k *ar = arvif->ar;
5300 	struct wmi_vdev_install_key_arg arg = {
5301 		.vdev_id = arvif->vdev_id,
5302 		.key_idx = key->keyidx,
5303 		.key_len = key->keylen,
5304 		.key_data = key->key,
5305 		.key_flags = flags,
5306 		.ieee80211_key_cipher = key->cipher,
5307 		.macaddr = macaddr,
5308 	};
5309 	struct ath12k_vif *ahvif = arvif->ahvif;
5310 
5311 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5312 
5313 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
5314 		return 0;
5315 
5316 	if (cmd == DISABLE_KEY) {
5317 		/* TODO: Check if FW expects  value other than NONE for del */
5318 		/* arg.key_cipher = WMI_CIPHER_NONE; */
5319 		arg.key_len = 0;
5320 		arg.key_data = NULL;
5321 		goto check_order;
5322 	}
5323 
5324 	switch (key->cipher) {
5325 	case WLAN_CIPHER_SUITE_CCMP:
5326 	case WLAN_CIPHER_SUITE_CCMP_256:
5327 		arg.key_cipher = WMI_CIPHER_AES_CCM;
5328 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5329 		break;
5330 	case WLAN_CIPHER_SUITE_TKIP:
5331 		arg.key_cipher = WMI_CIPHER_TKIP;
5332 		arg.key_txmic_len = 8;
5333 		arg.key_rxmic_len = 8;
5334 		break;
5335 	case WLAN_CIPHER_SUITE_GCMP:
5336 	case WLAN_CIPHER_SUITE_GCMP_256:
5337 		arg.key_cipher = WMI_CIPHER_AES_GCM;
5338 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5339 		break;
5340 	case WLAN_CIPHER_SUITE_AES_CMAC:
5341 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5342 		break;
5343 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5344 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5345 		arg.key_cipher = WMI_CIPHER_AES_GMAC;
5346 		break;
5347 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5348 		arg.key_cipher = WMI_CIPHER_AES_CMAC;
5349 		break;
5350 	default:
5351 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
5352 		return -EOPNOTSUPP;
5353 	}
5354 
5355 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
5356 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
5357 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
5358 
5359 check_order:
5360 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5361 	    arg.key_flags == WMI_KEY_GROUP) {
5362 		if (cmd == SET_KEY) {
5363 			if (arvif->pairwise_key_done) {
5364 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5365 					   "vdev %u pairwise key done, go install group key\n",
5366 					   arg.vdev_id);
5367 				goto install;
5368 			} else {
5369 				/* WCN7850 firmware requires pairwise key to be installed
5370 				 * before group key. In case group key comes first, cache
5371 				 * it and return. Will revisit it once pairwise key gets
5372 				 * installed.
5373 				 */
5374 				arvif->group_key = arg;
5375 				arvif->group_key_valid = true;
5376 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5377 					   "vdev %u group key before pairwise key, cache and skip\n",
5378 					   arg.vdev_id);
5379 
5380 				ret = 0;
5381 				goto out;
5382 			}
5383 		} else {
5384 			arvif->group_key_valid = false;
5385 		}
5386 	}
5387 
5388 install:
5389 	reinit_completion(&ar->install_key_done);
5390 
5391 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
5392 	if (ret)
5393 		return ret;
5394 
5395 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
5396 		return -ETIMEDOUT;
5397 
5398 	if (ether_addr_equal(arg.macaddr, arvif->bssid))
5399 		ahvif->key_cipher = arg.ieee80211_key_cipher;
5400 
5401 	if (ar->install_key_status) {
5402 		ret = -EINVAL;
5403 		goto out;
5404 	}
5405 
5406 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
5407 	    arg.key_flags == WMI_KEY_PAIRWISE) {
5408 		if (cmd == SET_KEY) {
5409 			arvif->pairwise_key_done = true;
5410 			if (arvif->group_key_valid) {
5411 				/* Install cached GTK */
5412 				arvif->group_key_valid = false;
5413 				arg = arvif->group_key;
5414 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5415 					   "vdev %u pairwise key done, group key ready, go install\n",
5416 					   arg.vdev_id);
5417 				goto install;
5418 			}
5419 		} else {
5420 			arvif->pairwise_key_done = false;
5421 		}
5422 	}
5423 
5424 out:
5425 	if (ret) {
5426 		/* In case of failure userspace may not do DISABLE_KEY
5427 		 * but triggers re-connection directly, so manually reset
5428 		 * status here.
5429 		 */
5430 		arvif->group_key_valid = false;
5431 		arvif->pairwise_key_done = false;
5432 	}
5433 
5434 	return ret;
5435 }
5436 
ath12k_clear_peer_keys(struct ath12k_link_vif * arvif,const u8 * addr)5437 static int ath12k_clear_peer_keys(struct ath12k_link_vif *arvif,
5438 				  const u8 *addr)
5439 {
5440 	struct ath12k *ar = arvif->ar;
5441 	struct ath12k_base *ab = ar->ab;
5442 	struct ath12k_peer *peer;
5443 	int first_errno = 0;
5444 	int ret;
5445 	int i;
5446 	u32 flags = 0;
5447 
5448 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5449 
5450 	spin_lock_bh(&ab->base_lock);
5451 	peer = ath12k_peer_find(ab, arvif->vdev_id, addr);
5452 	spin_unlock_bh(&ab->base_lock);
5453 
5454 	if (!peer)
5455 		return -ENOENT;
5456 
5457 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
5458 		if (!peer->keys[i])
5459 			continue;
5460 
5461 		/* key flags are not required to delete the key */
5462 		ret = ath12k_install_key(arvif, peer->keys[i],
5463 					 DISABLE_KEY, addr, flags);
5464 		if (ret < 0 && first_errno == 0)
5465 			first_errno = ret;
5466 
5467 		if (ret < 0)
5468 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
5469 				    i, ret);
5470 
5471 		spin_lock_bh(&ab->base_lock);
5472 		peer->keys[i] = NULL;
5473 		spin_unlock_bh(&ab->base_lock);
5474 	}
5475 
5476 	return first_errno;
5477 }
5478 
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)5479 static int ath12k_mac_set_key(struct ath12k *ar, enum set_key_cmd cmd,
5480 			      struct ath12k_link_vif *arvif,
5481 			      struct ath12k_link_sta *arsta,
5482 			      struct ieee80211_key_conf *key)
5483 {
5484 	struct ieee80211_sta *sta = NULL;
5485 	struct ath12k_base *ab = ar->ab;
5486 	struct ath12k_peer *peer;
5487 	struct ath12k_sta *ahsta;
5488 	const u8 *peer_addr;
5489 	int ret;
5490 	u32 flags = 0;
5491 
5492 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5493 
5494 	if (arsta)
5495 		sta = ath12k_ahsta_to_sta(arsta->ahsta);
5496 
5497 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ab->dev_flags))
5498 		return 1;
5499 
5500 	if (sta)
5501 		peer_addr = arsta->addr;
5502 	else
5503 		peer_addr = arvif->bssid;
5504 
5505 	key->hw_key_idx = key->keyidx;
5506 
5507 	/* the peer should not disappear in mid-way (unless FW goes awry) since
5508 	 * we already hold wiphy lock. we just make sure its there now.
5509 	 */
5510 	spin_lock_bh(&ab->base_lock);
5511 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
5512 	spin_unlock_bh(&ab->base_lock);
5513 
5514 	if (!peer) {
5515 		if (cmd == SET_KEY) {
5516 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
5517 				    peer_addr);
5518 			return -EOPNOTSUPP;
5519 		}
5520 
5521 		/* if the peer doesn't exist there is no key to disable
5522 		 * anymore
5523 		 */
5524 		return 0;
5525 	}
5526 
5527 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5528 		flags = WMI_KEY_PAIRWISE;
5529 	else
5530 		flags = WMI_KEY_GROUP;
5531 
5532 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
5533 	if (ret) {
5534 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
5535 		return ret;
5536 	}
5537 
5538 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
5539 	if (ret) {
5540 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
5541 		return ret;
5542 	}
5543 
5544 	spin_lock_bh(&ab->base_lock);
5545 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
5546 	if (peer && cmd == SET_KEY) {
5547 		peer->keys[key->keyidx] = key;
5548 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
5549 			peer->ucast_keyidx = key->keyidx;
5550 			peer->sec_type = ath12k_dp_tx_get_encrypt_type(key->cipher);
5551 		} else {
5552 			peer->mcast_keyidx = key->keyidx;
5553 			peer->sec_type_grp = ath12k_dp_tx_get_encrypt_type(key->cipher);
5554 		}
5555 	} else if (peer && cmd == DISABLE_KEY) {
5556 		peer->keys[key->keyidx] = NULL;
5557 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5558 			peer->ucast_keyidx = 0;
5559 		else
5560 			peer->mcast_keyidx = 0;
5561 	} else if (!peer)
5562 		/* impossible unless FW goes crazy */
5563 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
5564 
5565 	if (sta) {
5566 		ahsta = ath12k_sta_to_ahsta(sta);
5567 
5568 		switch (key->cipher) {
5569 		case WLAN_CIPHER_SUITE_TKIP:
5570 		case WLAN_CIPHER_SUITE_CCMP:
5571 		case WLAN_CIPHER_SUITE_CCMP_256:
5572 		case WLAN_CIPHER_SUITE_GCMP:
5573 		case WLAN_CIPHER_SUITE_GCMP_256:
5574 			if (cmd == SET_KEY)
5575 				ahsta->pn_type = HAL_PN_TYPE_WPA;
5576 			else
5577 				ahsta->pn_type = HAL_PN_TYPE_NONE;
5578 			break;
5579 		default:
5580 			ahsta->pn_type = HAL_PN_TYPE_NONE;
5581 			break;
5582 		}
5583 	}
5584 
5585 	spin_unlock_bh(&ab->base_lock);
5586 
5587 	return 0;
5588 }
5589 
ath12k_mac_update_key_cache(struct ath12k_vif_cache * cache,enum set_key_cmd cmd,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)5590 static int ath12k_mac_update_key_cache(struct ath12k_vif_cache *cache,
5591 				       enum set_key_cmd cmd,
5592 				       struct ieee80211_sta *sta,
5593 				       struct ieee80211_key_conf *key)
5594 {
5595 	struct ath12k_key_conf *key_conf, *tmp;
5596 
5597 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
5598 		if (key_conf->key != key)
5599 			continue;
5600 
5601 		/* If SET key entry is already present in cache, nothing to do,
5602 		 * just return
5603 		 */
5604 		if (cmd == SET_KEY)
5605 			return 0;
5606 
5607 		/* DEL key for an old SET key which driver hasn't flushed yet.
5608 		 */
5609 		list_del(&key_conf->list);
5610 		kfree(key_conf);
5611 	}
5612 
5613 	if (cmd == SET_KEY) {
5614 		key_conf = kzalloc(sizeof(*key_conf), GFP_KERNEL);
5615 
5616 		if (!key_conf)
5617 			return -ENOMEM;
5618 
5619 		key_conf->cmd = cmd;
5620 		key_conf->sta = sta;
5621 		key_conf->key = key;
5622 		list_add_tail(&key_conf->list,
5623 			      &cache->key_conf.list);
5624 	}
5625 
5626 	return 0;
5627 }
5628 
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)5629 static int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5630 				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5631 				 struct ieee80211_key_conf *key)
5632 {
5633 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
5634 	struct ath12k_link_vif *arvif;
5635 	struct ath12k_link_sta *arsta = NULL;
5636 	struct ath12k_vif_cache *cache;
5637 	struct ath12k_sta *ahsta;
5638 	unsigned long links;
5639 	u8 link_id;
5640 	int ret;
5641 
5642 	lockdep_assert_wiphy(hw->wiphy);
5643 
5644 	/* IGTK needs to be done in host software */
5645 	if (key->keyidx == 4 || key->keyidx == 5)
5646 		return 1;
5647 
5648 	if (key->keyidx > WMI_MAX_KEY_INDEX)
5649 		return -ENOSPC;
5650 
5651 	if (sta) {
5652 		ahsta = ath12k_sta_to_ahsta(sta);
5653 
5654 		/* For an ML STA Pairwise key is same for all associated link Stations,
5655 		 * hence do set key for all link STAs which are active.
5656 		 */
5657 		if (sta->mlo) {
5658 			links = ahsta->links_map;
5659 			for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
5660 				arvif = wiphy_dereference(hw->wiphy,
5661 							  ahvif->link[link_id]);
5662 				arsta = wiphy_dereference(hw->wiphy,
5663 							  ahsta->link[link_id]);
5664 
5665 				if (WARN_ON(!arvif || !arsta))
5666 					/* arvif and arsta are expected to be valid when
5667 					 * STA is present.
5668 					 */
5669 					continue;
5670 
5671 				ret = ath12k_mac_set_key(arvif->ar, cmd, arvif,
5672 							 arsta, key);
5673 				if (ret)
5674 					break;
5675 			}
5676 
5677 			return 0;
5678 		}
5679 
5680 		arsta = &ahsta->deflink;
5681 		arvif = arsta->arvif;
5682 		if (WARN_ON(!arvif))
5683 			return -EINVAL;
5684 
5685 		ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, arsta, key);
5686 		if (ret)
5687 			return ret;
5688 
5689 		return 0;
5690 	}
5691 
5692 	if (key->link_id >= 0 && key->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
5693 		link_id = key->link_id;
5694 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
5695 	} else {
5696 		link_id = 0;
5697 		arvif = &ahvif->deflink;
5698 	}
5699 
5700 	if (!arvif || !arvif->is_created) {
5701 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
5702 		if (!cache)
5703 			return -ENOSPC;
5704 
5705 		ret = ath12k_mac_update_key_cache(cache, cmd, sta, key);
5706 		if (ret)
5707 			return ret;
5708 
5709 		return 0;
5710 	}
5711 
5712 	ret = ath12k_mac_set_key(arvif->ar, cmd, arvif, NULL, key);
5713 	if (ret)
5714 		return ret;
5715 
5716 	return 0;
5717 }
5718 
5719 static int
ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)5720 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
5721 				      enum nl80211_band band,
5722 				      const struct cfg80211_bitrate_mask *mask)
5723 {
5724 	int num_rates = 0;
5725 	int i;
5726 
5727 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
5728 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
5729 
5730 	return num_rates;
5731 }
5732 
5733 static int
ath12k_mac_bitrate_mask_num_he_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)5734 ath12k_mac_bitrate_mask_num_he_rates(struct ath12k *ar,
5735 				     enum nl80211_band band,
5736 				     const struct cfg80211_bitrate_mask *mask)
5737 {
5738 	int num_rates = 0;
5739 	int i;
5740 
5741 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++)
5742 		num_rates += hweight16(mask->control[band].he_mcs[i]);
5743 
5744 	return num_rates;
5745 }
5746 
5747 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)5748 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_link_vif *arvif,
5749 				   struct ath12k_link_sta *arsta,
5750 				   const struct cfg80211_bitrate_mask *mask,
5751 				   enum nl80211_band band)
5752 {
5753 	struct ath12k *ar = arvif->ar;
5754 	u8 vht_rate, nss;
5755 	u32 rate_code;
5756 	int ret, i;
5757 
5758 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5759 
5760 	nss = 0;
5761 
5762 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
5763 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
5764 			nss = i + 1;
5765 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
5766 		}
5767 	}
5768 
5769 	if (!nss) {
5770 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
5771 			    arsta->addr);
5772 		return -EINVAL;
5773 	}
5774 
5775 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5776 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
5777 		   arsta->addr);
5778 
5779 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
5780 					WMI_RATE_PREAMBLE_VHT);
5781 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
5782 					arvif->vdev_id,
5783 					WMI_PEER_PARAM_FIXED_RATE,
5784 					rate_code);
5785 	if (ret)
5786 		ath12k_warn(ar->ab,
5787 			    "failed to update STA %pM Fixed Rate %d: %d\n",
5788 			     arsta->addr, rate_code, ret);
5789 
5790 	return ret;
5791 }
5792 
5793 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)5794 ath12k_mac_set_peer_he_fixed_rate(struct ath12k_link_vif *arvif,
5795 				  struct ath12k_link_sta *arsta,
5796 				  const struct cfg80211_bitrate_mask *mask,
5797 				  enum nl80211_band band)
5798 {
5799 	struct ath12k *ar = arvif->ar;
5800 	u8 he_rate, nss;
5801 	u32 rate_code;
5802 	int ret, i;
5803 	struct ath12k_sta *ahsta = arsta->ahsta;
5804 	struct ieee80211_sta *sta;
5805 
5806 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5807 
5808 	sta = ath12k_ahsta_to_sta(ahsta);
5809 	nss = 0;
5810 
5811 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
5812 		if (hweight16(mask->control[band].he_mcs[i]) == 1) {
5813 			nss = i + 1;
5814 			he_rate = ffs(mask->control[band].he_mcs[i]) - 1;
5815 		}
5816 	}
5817 
5818 	if (!nss) {
5819 		ath12k_warn(ar->ab, "No single HE Fixed rate found to set for %pM",
5820 			    arsta->addr);
5821 		return -EINVAL;
5822 	}
5823 
5824 	/* Avoid updating invalid nss as fixed rate*/
5825 	if (nss > sta->deflink.rx_nss)
5826 		return -EINVAL;
5827 
5828 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5829 		   "Setting Fixed HE Rate for peer %pM. Device will not switch to any other selected rates",
5830 		   arsta->addr);
5831 
5832 	rate_code = ATH12K_HW_RATE_CODE(he_rate, nss - 1,
5833 					WMI_RATE_PREAMBLE_HE);
5834 
5835 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
5836 					arvif->vdev_id,
5837 					WMI_PEER_PARAM_FIXED_RATE,
5838 					rate_code);
5839 	if (ret)
5840 		ath12k_warn(ar->ab,
5841 			    "failed to update STA %pM Fixed Rate %d: %d\n",
5842 			    arsta->addr, rate_code, ret);
5843 
5844 	return ret;
5845 }
5846 
ath12k_mac_station_assoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta,bool reassoc)5847 static int ath12k_mac_station_assoc(struct ath12k *ar,
5848 				    struct ath12k_link_vif *arvif,
5849 				    struct ath12k_link_sta *arsta,
5850 				    bool reassoc)
5851 {
5852 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
5853 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
5854 	struct ieee80211_link_sta *link_sta;
5855 	int ret;
5856 	struct cfg80211_chan_def def;
5857 	enum nl80211_band band;
5858 	struct cfg80211_bitrate_mask *mask;
5859 	u8 num_vht_rates, num_he_rates;
5860 	u8 link_id = arvif->link_id;
5861 
5862 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5863 
5864 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
5865 		return -EPERM;
5866 
5867 	if (WARN_ON(!rcu_access_pointer(sta->link[link_id])))
5868 		return -EINVAL;
5869 
5870 	band = def.chan->band;
5871 	mask = &arvif->bitrate_mask;
5872 
5873 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
5874 		kzalloc(sizeof(*peer_arg), GFP_KERNEL);
5875 	if (!peer_arg)
5876 		return -ENOMEM;
5877 
5878 	ath12k_peer_assoc_prepare(ar, arvif, arsta, peer_arg, reassoc);
5879 
5880 	if (peer_arg->peer_nss < 1) {
5881 		ath12k_warn(ar->ab,
5882 			    "invalid peer NSS %d\n", peer_arg->peer_nss);
5883 		return -EINVAL;
5884 	}
5885 
5886 	peer_arg->is_assoc = true;
5887 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
5888 	if (ret) {
5889 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
5890 			    arsta->addr, arvif->vdev_id, ret);
5891 		return ret;
5892 	}
5893 
5894 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
5895 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
5896 			    arsta->addr, arvif->vdev_id);
5897 		return -ETIMEDOUT;
5898 	}
5899 
5900 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
5901 	num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
5902 
5903 	/* If single VHT/HE rate is configured (by set_bitrate_mask()),
5904 	 * peer_assoc will disable VHT/HE. This is now enabled by a peer specific
5905 	 * fixed param.
5906 	 * Note that all other rates and NSS will be disabled for this peer.
5907 	 */
5908 	link_sta = ath12k_mac_get_link_sta(arsta);
5909 	if (!link_sta) {
5910 		ath12k_warn(ar->ab, "unable to access link sta in station assoc\n");
5911 		return -EINVAL;
5912 	}
5913 
5914 	spin_lock_bh(&ar->data_lock);
5915 	arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
5916 	arsta->bw_prev = link_sta->bandwidth;
5917 	spin_unlock_bh(&ar->data_lock);
5918 
5919 	if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
5920 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask, band);
5921 	} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
5922 		ret = ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
5923 		if (ret)
5924 			return ret;
5925 	}
5926 
5927 	/* Re-assoc is run only to update supported rates for given station. It
5928 	 * doesn't make much sense to reconfigure the peer completely.
5929 	 */
5930 	if (reassoc)
5931 		return 0;
5932 
5933 	ret = ath12k_setup_peer_smps(ar, arvif, arsta->addr,
5934 				     &link_sta->ht_cap, &link_sta->he_6ghz_capa);
5935 	if (ret) {
5936 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
5937 			    arvif->vdev_id, ret);
5938 		return ret;
5939 	}
5940 
5941 	if (!sta->wme) {
5942 		arvif->num_legacy_stations++;
5943 		ret = ath12k_recalc_rtscts_prot(arvif);
5944 		if (ret)
5945 			return ret;
5946 	}
5947 
5948 	if (sta->wme && sta->uapsd_queues) {
5949 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, arsta);
5950 		if (ret) {
5951 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
5952 				    arsta->addr, arvif->vdev_id, ret);
5953 			return ret;
5954 		}
5955 	}
5956 
5957 	return 0;
5958 }
5959 
ath12k_mac_station_disassoc(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)5960 static int ath12k_mac_station_disassoc(struct ath12k *ar,
5961 				       struct ath12k_link_vif *arvif,
5962 				       struct ath12k_link_sta *arsta)
5963 {
5964 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
5965 
5966 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
5967 
5968 	if (!sta->wme) {
5969 		arvif->num_legacy_stations--;
5970 		return ath12k_recalc_rtscts_prot(arvif);
5971 	}
5972 
5973 	return 0;
5974 }
5975 
ath12k_sta_rc_update_wk(struct wiphy * wiphy,struct wiphy_work * wk)5976 static void ath12k_sta_rc_update_wk(struct wiphy *wiphy, struct wiphy_work *wk)
5977 {
5978 	struct ieee80211_link_sta *link_sta;
5979 	struct ath12k *ar;
5980 	struct ath12k_link_vif *arvif;
5981 	struct ieee80211_sta *sta;
5982 	struct cfg80211_chan_def def;
5983 	enum nl80211_band band;
5984 	const u8 *ht_mcs_mask;
5985 	const u16 *vht_mcs_mask;
5986 	const u16 *he_mcs_mask;
5987 	u32 changed, bw, nss, mac_nss, smps, bw_prev;
5988 	int err, num_vht_rates, num_he_rates;
5989 	const struct cfg80211_bitrate_mask *mask;
5990 	enum wmi_phy_mode peer_phymode;
5991 	struct ath12k_link_sta *arsta;
5992 	struct ieee80211_vif *vif;
5993 
5994 	lockdep_assert_wiphy(wiphy);
5995 
5996 	arsta = container_of(wk, struct ath12k_link_sta, update_wk);
5997 	sta = ath12k_ahsta_to_sta(arsta->ahsta);
5998 	arvif = arsta->arvif;
5999 	vif = ath12k_ahvif_to_vif(arvif->ahvif);
6000 	ar = arvif->ar;
6001 
6002 	if (WARN_ON(ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)))
6003 		return;
6004 
6005 	band = def.chan->band;
6006 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6007 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6008 	he_mcs_mask = arvif->bitrate_mask.control[band].he_mcs;
6009 
6010 	spin_lock_bh(&ar->data_lock);
6011 
6012 	changed = arsta->changed;
6013 	arsta->changed = 0;
6014 
6015 	bw = arsta->bw;
6016 	bw_prev = arsta->bw_prev;
6017 	nss = arsta->nss;
6018 	smps = arsta->smps;
6019 
6020 	spin_unlock_bh(&ar->data_lock);
6021 
6022 	nss = max_t(u32, 1, nss);
6023 	mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
6024 		       ath12k_mac_max_vht_nss(vht_mcs_mask),
6025 		       ath12k_mac_max_he_nss(he_mcs_mask));
6026 	nss = min(nss, mac_nss);
6027 
6028 	struct ath12k_wmi_peer_assoc_arg *peer_arg __free(kfree) =
6029 					kzalloc(sizeof(*peer_arg), GFP_KERNEL);
6030 	if (!peer_arg)
6031 		return;
6032 
6033 	if (changed & IEEE80211_RC_BW_CHANGED) {
6034 		ath12k_peer_assoc_h_phymode(ar, arvif, arsta, peer_arg);
6035 		peer_phymode = peer_arg->peer_phymode;
6036 
6037 		if (bw > bw_prev) {
6038 			/* Phymode shows maximum supported channel width, if we
6039 			 * upgrade bandwidth then due to sanity check of firmware,
6040 			 * we have to send WMI_PEER_PHYMODE followed by
6041 			 * WMI_PEER_CHWIDTH
6042 			 */
6043 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
6044 				   arsta->addr, bw, bw_prev);
6045 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6046 							arvif->vdev_id, WMI_PEER_PHYMODE,
6047 							peer_phymode);
6048 			if (err) {
6049 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6050 					    arsta->addr, peer_phymode, err);
6051 				return;
6052 			}
6053 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6054 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6055 							bw);
6056 			if (err)
6057 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
6058 					    arsta->addr, bw, err);
6059 		} else {
6060 			/* When we downgrade bandwidth this will conflict with phymode
6061 			 * and cause to trigger firmware crash. In this case we send
6062 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
6063 			 */
6064 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
6065 				   arsta->addr, bw, bw_prev);
6066 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6067 							arvif->vdev_id, WMI_PEER_CHWIDTH,
6068 							bw);
6069 			if (err) {
6070 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
6071 					    arsta->addr, bw, err);
6072 				return;
6073 			}
6074 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6075 							arvif->vdev_id, WMI_PEER_PHYMODE,
6076 							peer_phymode);
6077 			if (err)
6078 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
6079 					    arsta->addr, peer_phymode, err);
6080 		}
6081 	}
6082 
6083 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6084 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
6085 			   arsta->addr, nss);
6086 
6087 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6088 						WMI_PEER_NSS, nss);
6089 		if (err)
6090 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
6091 				    arsta->addr, nss, err);
6092 	}
6093 
6094 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6095 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
6096 			   arsta->addr, smps);
6097 
6098 		err = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
6099 						WMI_PEER_MIMO_PS_STATE, smps);
6100 		if (err)
6101 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
6102 				    arsta->addr, smps, err);
6103 	}
6104 
6105 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6106 		mask = &arvif->bitrate_mask;
6107 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6108 								      mask);
6109 		num_he_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band,
6110 								    mask);
6111 
6112 		/* Peer_assoc_prepare will reject vht rates in
6113 		 * bitrate_mask if its not available in range format and
6114 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
6115 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
6116 		 * But, Single rate in VHT mask can be set as per-peer
6117 		 * fixed rate. But even if any HT rates are configured in
6118 		 * the bitrate mask, device will not switch to those rates
6119 		 * when per-peer Fixed rate is set.
6120 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
6121 		 * across HT/VHT and for multiple VHT MCS support.
6122 		 */
6123 		link_sta = ath12k_mac_get_link_sta(arsta);
6124 		if (!link_sta) {
6125 			ath12k_warn(ar->ab, "unable to access link sta in peer assoc he for sta %pM link %u\n",
6126 				    sta->addr, arsta->link_id);
6127 			return;
6128 		}
6129 
6130 		if (link_sta->vht_cap.vht_supported && num_vht_rates == 1) {
6131 			ath12k_mac_set_peer_vht_fixed_rate(arvif, arsta, mask,
6132 							   band);
6133 		} else if (link_sta->he_cap.has_he && num_he_rates == 1) {
6134 			ath12k_mac_set_peer_he_fixed_rate(arvif, arsta, mask, band);
6135 		} else {
6136 			/* If the peer is non-VHT/HE or no fixed VHT/HE rate
6137 			 * is provided in the new bitrate mask we set the
6138 			 * other rates using peer_assoc command. Also clear
6139 			 * the peer fixed rate settings as it has higher proprity
6140 			 * than peer assoc
6141 			 */
6142 			err = ath12k_wmi_set_peer_param(ar, arsta->addr,
6143 							arvif->vdev_id,
6144 							WMI_PEER_PARAM_FIXED_RATE,
6145 							WMI_FIXED_RATE_NONE);
6146 			if (err)
6147 				ath12k_warn(ar->ab,
6148 					    "failed to disable peer fixed rate for STA %pM ret %d\n",
6149 					    arsta->addr, err);
6150 
6151 			ath12k_peer_assoc_prepare(ar, arvif, arsta,
6152 						  peer_arg, true);
6153 
6154 			peer_arg->is_assoc = false;
6155 			err = ath12k_wmi_send_peer_assoc_cmd(ar, peer_arg);
6156 			if (err)
6157 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
6158 					    arsta->addr, arvif->vdev_id, err);
6159 
6160 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
6161 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
6162 					    arsta->addr, arvif->vdev_id);
6163 		}
6164 	}
6165 }
6166 
ath12k_mac_free_unassign_link_sta(struct ath12k_hw * ah,struct ath12k_sta * ahsta,u8 link_id)6167 static void ath12k_mac_free_unassign_link_sta(struct ath12k_hw *ah,
6168 					      struct ath12k_sta *ahsta,
6169 					      u8 link_id)
6170 {
6171 	struct ath12k_link_sta *arsta;
6172 
6173 	lockdep_assert_wiphy(ah->hw->wiphy);
6174 
6175 	if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS))
6176 		return;
6177 
6178 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6179 	if (WARN_ON(!arsta))
6180 		return;
6181 
6182 	ahsta->links_map &= ~BIT(link_id);
6183 	rcu_assign_pointer(ahsta->link[link_id], NULL);
6184 	synchronize_rcu();
6185 
6186 	if (arsta == &ahsta->deflink) {
6187 		arsta->link_id = ATH12K_INVALID_LINK_ID;
6188 		arsta->ahsta = NULL;
6189 		arsta->arvif = NULL;
6190 		return;
6191 	}
6192 
6193 	kfree(arsta);
6194 }
6195 
ath12k_mac_inc_num_stations(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6196 static int ath12k_mac_inc_num_stations(struct ath12k_link_vif *arvif,
6197 				       struct ath12k_link_sta *arsta)
6198 {
6199 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6200 	struct ath12k *ar = arvif->ar;
6201 
6202 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6203 
6204 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6205 		return 0;
6206 
6207 	if (ar->num_stations >= ar->max_num_stations)
6208 		return -ENOBUFS;
6209 
6210 	ar->num_stations++;
6211 	arvif->num_stations++;
6212 
6213 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6214 		   "mac station %pM connected to vdev %u num_stations %u\n",
6215 		   arsta->addr, arvif->vdev_id, arvif->num_stations);
6216 
6217 	return 0;
6218 }
6219 
ath12k_mac_dec_num_stations(struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6220 static void ath12k_mac_dec_num_stations(struct ath12k_link_vif *arvif,
6221 					struct ath12k_link_sta *arsta)
6222 {
6223 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6224 	struct ath12k *ar = arvif->ar;
6225 
6226 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6227 
6228 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6229 		return;
6230 
6231 	ar->num_stations--;
6232 
6233 	if (arvif->num_stations) {
6234 		arvif->num_stations--;
6235 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6236 			   "mac station %pM disconnected from vdev %u num_stations %u\n",
6237 			   arsta->addr, arvif->vdev_id, arvif->num_stations);
6238 	} else {
6239 		ath12k_warn(ar->ab,
6240 			    "mac station %pM disconnect for vdev %u without any connected station\n",
6241 			    arsta->addr, arvif->vdev_id);
6242 	}
6243 }
6244 
ath12k_mac_station_post_remove(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6245 static void ath12k_mac_station_post_remove(struct ath12k *ar,
6246 					   struct ath12k_link_vif *arvif,
6247 					   struct ath12k_link_sta *arsta)
6248 {
6249 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6250 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6251 	struct ath12k_peer *peer;
6252 
6253 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6254 
6255 	ath12k_mac_dec_num_stations(arvif, arsta);
6256 
6257 	spin_lock_bh(&ar->ab->base_lock);
6258 
6259 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
6260 	if (peer && peer->sta == sta) {
6261 		ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
6262 			    vif->addr, arvif->vdev_id);
6263 		peer->sta = NULL;
6264 		list_del(&peer->list);
6265 		kfree(peer);
6266 		ar->num_peers--;
6267 	}
6268 
6269 	spin_unlock_bh(&ar->ab->base_lock);
6270 
6271 	kfree(arsta->rx_stats);
6272 	arsta->rx_stats = NULL;
6273 }
6274 
ath12k_mac_station_unauthorize(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6275 static int ath12k_mac_station_unauthorize(struct ath12k *ar,
6276 					  struct ath12k_link_vif *arvif,
6277 					  struct ath12k_link_sta *arsta)
6278 {
6279 	struct ath12k_peer *peer;
6280 	int ret;
6281 
6282 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6283 
6284 	spin_lock_bh(&ar->ab->base_lock);
6285 
6286 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
6287 	if (peer)
6288 		peer->is_authorized = false;
6289 
6290 	spin_unlock_bh(&ar->ab->base_lock);
6291 
6292 	/* Driver must clear the keys during the state change from
6293 	 * IEEE80211_STA_AUTHORIZED to IEEE80211_STA_ASSOC, since after
6294 	 * returning from here, mac80211 is going to delete the keys
6295 	 * in __sta_info_destroy_part2(). This will ensure that the driver does
6296 	 * not retain stale key references after mac80211 deletes the keys.
6297 	 */
6298 	ret = ath12k_clear_peer_keys(arvif, arsta->addr);
6299 	if (ret) {
6300 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
6301 			    arvif->vdev_id, ret);
6302 		return ret;
6303 	}
6304 
6305 	return 0;
6306 }
6307 
ath12k_mac_station_authorize(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6308 static int ath12k_mac_station_authorize(struct ath12k *ar,
6309 					struct ath12k_link_vif *arvif,
6310 					struct ath12k_link_sta *arsta)
6311 {
6312 	struct ath12k_peer *peer;
6313 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6314 	int ret;
6315 
6316 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6317 
6318 	spin_lock_bh(&ar->ab->base_lock);
6319 
6320 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
6321 	if (peer)
6322 		peer->is_authorized = true;
6323 
6324 	spin_unlock_bh(&ar->ab->base_lock);
6325 
6326 	if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
6327 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6328 						arvif->vdev_id,
6329 						WMI_PEER_AUTHORIZE,
6330 						1);
6331 		if (ret) {
6332 			ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
6333 				    arsta->addr, arvif->vdev_id, ret);
6334 			return ret;
6335 		}
6336 	}
6337 
6338 	return 0;
6339 }
6340 
ath12k_mac_station_remove(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6341 static int ath12k_mac_station_remove(struct ath12k *ar,
6342 				     struct ath12k_link_vif *arvif,
6343 				     struct ath12k_link_sta *arsta)
6344 {
6345 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6346 	struct ath12k_vif *ahvif = arvif->ahvif;
6347 	int ret = 0;
6348 
6349 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6350 
6351 	wiphy_work_cancel(ar->ah->hw->wiphy, &arsta->update_wk);
6352 
6353 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6354 		ath12k_bss_disassoc(ar, arvif);
6355 		ret = ath12k_mac_vdev_stop(arvif);
6356 		if (ret)
6357 			ath12k_warn(ar->ab, "failed to stop vdev %i: %d\n",
6358 				    arvif->vdev_id, ret);
6359 	}
6360 
6361 	if (sta->mlo)
6362 		return ret;
6363 
6364 	ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
6365 
6366 	ret = ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6367 	if (ret)
6368 		ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
6369 			    arsta->addr, arvif->vdev_id);
6370 	else
6371 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
6372 			   arsta->addr, arvif->vdev_id);
6373 
6374 	ath12k_mac_station_post_remove(ar, arvif, arsta);
6375 
6376 	if (sta->valid_links)
6377 		ath12k_mac_free_unassign_link_sta(ahvif->ah,
6378 						  arsta->ahsta, arsta->link_id);
6379 
6380 	return ret;
6381 }
6382 
ath12k_mac_station_add(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ath12k_link_sta * arsta)6383 static int ath12k_mac_station_add(struct ath12k *ar,
6384 				  struct ath12k_link_vif *arvif,
6385 				  struct ath12k_link_sta *arsta)
6386 {
6387 	struct ath12k_base *ab = ar->ab;
6388 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6389 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(arsta->ahsta);
6390 	struct ath12k_wmi_peer_create_arg peer_param = {};
6391 	int ret;
6392 
6393 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
6394 
6395 	ret = ath12k_mac_inc_num_stations(arvif, arsta);
6396 	if (ret) {
6397 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
6398 			    ar->max_num_stations);
6399 		goto exit;
6400 	}
6401 
6402 	if (ath12k_debugfs_is_extd_rx_stats_enabled(ar) && !arsta->rx_stats) {
6403 		arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
6404 		if (!arsta->rx_stats) {
6405 			ret = -ENOMEM;
6406 			goto dec_num_station;
6407 		}
6408 	}
6409 
6410 	peer_param.vdev_id = arvif->vdev_id;
6411 	peer_param.peer_addr = arsta->addr;
6412 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
6413 	peer_param.ml_enabled = sta->mlo;
6414 
6415 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
6416 	if (ret) {
6417 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
6418 			    arsta->addr, arvif->vdev_id);
6419 		goto free_peer;
6420 	}
6421 
6422 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
6423 		   arsta->addr, arvif->vdev_id);
6424 
6425 	if (ieee80211_vif_is_mesh(vif)) {
6426 		ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
6427 						arvif->vdev_id,
6428 						WMI_PEER_USE_4ADDR, 1);
6429 		if (ret) {
6430 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
6431 				    arsta->addr, ret);
6432 			goto free_peer;
6433 		}
6434 	}
6435 
6436 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, arsta->addr);
6437 	if (ret) {
6438 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
6439 			    arsta->addr, arvif->vdev_id, ret);
6440 		goto free_peer;
6441 	}
6442 
6443 	if (ab->hw_params->vdev_start_delay &&
6444 	    !arvif->is_started &&
6445 	    arvif->ahvif->vdev_type != WMI_VDEV_TYPE_AP) {
6446 		ret = ath12k_start_vdev_delay(ar, arvif);
6447 		if (ret) {
6448 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
6449 			goto free_peer;
6450 		}
6451 	}
6452 
6453 	ewma_avg_rssi_init(&arsta->avg_rssi);
6454 	return 0;
6455 
6456 free_peer:
6457 	ath12k_peer_delete(ar, arvif->vdev_id, arsta->addr);
6458 	kfree(arsta->rx_stats);
6459 	arsta->rx_stats = NULL;
6460 dec_num_station:
6461 	ath12k_mac_dec_num_stations(arvif, arsta);
6462 exit:
6463 	return ret;
6464 }
6465 
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)6466 static int ath12k_mac_assign_link_sta(struct ath12k_hw *ah,
6467 				      struct ath12k_sta *ahsta,
6468 				      struct ath12k_link_sta *arsta,
6469 				      struct ath12k_vif *ahvif,
6470 				      u8 link_id)
6471 {
6472 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
6473 	struct ieee80211_link_sta *link_sta;
6474 	struct ath12k_link_vif *arvif;
6475 
6476 	lockdep_assert_wiphy(ah->hw->wiphy);
6477 
6478 	if (!arsta || link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
6479 		return -EINVAL;
6480 
6481 	arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
6482 	if (!arvif)
6483 		return -EINVAL;
6484 
6485 	memset(arsta, 0, sizeof(*arsta));
6486 
6487 	link_sta = wiphy_dereference(ah->hw->wiphy, sta->link[link_id]);
6488 	if (!link_sta)
6489 		return -EINVAL;
6490 
6491 	ether_addr_copy(arsta->addr, link_sta->addr);
6492 
6493 	/* logical index of the link sta in order of creation */
6494 	arsta->link_idx = ahsta->num_peer++;
6495 
6496 	arsta->link_id = link_id;
6497 	ahsta->links_map |= BIT(arsta->link_id);
6498 	arsta->arvif = arvif;
6499 	arsta->ahsta = ahsta;
6500 	ahsta->ahvif = ahvif;
6501 
6502 	wiphy_work_init(&arsta->update_wk, ath12k_sta_rc_update_wk);
6503 
6504 	rcu_assign_pointer(ahsta->link[link_id], arsta);
6505 
6506 	return 0;
6507 }
6508 
ath12k_mac_ml_station_remove(struct ath12k_vif * ahvif,struct ath12k_sta * ahsta)6509 static void ath12k_mac_ml_station_remove(struct ath12k_vif *ahvif,
6510 					 struct ath12k_sta *ahsta)
6511 {
6512 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
6513 	struct ath12k_hw *ah = ahvif->ah;
6514 	struct ath12k_link_vif *arvif;
6515 	struct ath12k_link_sta *arsta;
6516 	unsigned long links;
6517 	struct ath12k *ar;
6518 	u8 link_id;
6519 
6520 	lockdep_assert_wiphy(ah->hw->wiphy);
6521 
6522 	ath12k_peer_mlo_link_peers_delete(ahvif, ahsta);
6523 
6524 	/* validate link station removal and clear arsta links */
6525 	links = ahsta->links_map;
6526 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6527 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
6528 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
6529 		if (!arvif || !arsta)
6530 			continue;
6531 
6532 		ar = arvif->ar;
6533 
6534 		ath12k_mac_station_post_remove(ar, arvif, arsta);
6535 
6536 		ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
6537 	}
6538 
6539 	ath12k_peer_ml_delete(ah, sta);
6540 }
6541 
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)6542 static int ath12k_mac_handle_link_sta_state(struct ieee80211_hw *hw,
6543 					    struct ath12k_link_vif *arvif,
6544 					    struct ath12k_link_sta *arsta,
6545 					    enum ieee80211_sta_state old_state,
6546 					    enum ieee80211_sta_state new_state)
6547 {
6548 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
6549 	struct ieee80211_bss_conf *link_conf;
6550 	struct ath12k *ar = arvif->ar;
6551 	struct ath12k_reg_info *reg_info;
6552 	struct ath12k_base *ab = ar->ab;
6553 	int ret = 0;
6554 
6555 	lockdep_assert_wiphy(hw->wiphy);
6556 
6557 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac handle link %u sta %pM state %d -> %d\n",
6558 		   arsta->link_id, arsta->addr, old_state, new_state);
6559 
6560 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST: Remove the station
6561 	 * from driver
6562 	 */
6563 	if ((old_state == IEEE80211_STA_NONE &&
6564 	     new_state == IEEE80211_STA_NOTEXIST)) {
6565 		ret = ath12k_mac_station_remove(ar, arvif, arsta);
6566 		if (ret) {
6567 			ath12k_warn(ab, "Failed to remove station: %pM for VDEV: %d\n",
6568 				    arsta->addr, arvif->vdev_id);
6569 			goto exit;
6570 		}
6571 	}
6572 
6573 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE: Add new station to driver */
6574 	if (old_state == IEEE80211_STA_NOTEXIST &&
6575 	    new_state == IEEE80211_STA_NONE) {
6576 		ret = ath12k_mac_station_add(ar, arvif, arsta);
6577 		if (ret)
6578 			ath12k_warn(ab, "Failed to add station: %pM for VDEV: %d\n",
6579 				    arsta->addr, arvif->vdev_id);
6580 
6581 	/* IEEE80211_STA_AUTH -> IEEE80211_STA_ASSOC: Send station assoc command for
6582 	 * peer associated to AP/Mesh/ADHOC vif type.
6583 	 */
6584 	} else if (old_state == IEEE80211_STA_AUTH &&
6585 		   new_state == IEEE80211_STA_ASSOC &&
6586 		   (vif->type == NL80211_IFTYPE_AP ||
6587 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
6588 		    vif->type == NL80211_IFTYPE_ADHOC)) {
6589 		ret = ath12k_mac_station_assoc(ar, arvif, arsta, false);
6590 		if (ret)
6591 			ath12k_warn(ab, "Failed to associate station: %pM\n",
6592 				    arsta->addr);
6593 
6594 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTHORIZED: set peer status as
6595 	 * authorized
6596 	 */
6597 	} else if (old_state == IEEE80211_STA_ASSOC &&
6598 		   new_state == IEEE80211_STA_AUTHORIZED) {
6599 		ret = ath12k_mac_station_authorize(ar, arvif, arsta);
6600 		if (ret) {
6601 			ath12k_warn(ab, "Failed to authorize station: %pM\n",
6602 				    arsta->addr);
6603 			goto exit;
6604 		}
6605 
6606 		if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
6607 		    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_STA) {
6608 			link_conf = ath12k_mac_get_link_bss_conf(arvif);
6609 			reg_info = ab->reg_info[ar->pdev_idx];
6610 			ath12k_dbg(ab, ATH12K_DBG_MAC, "connection done, update reg rules\n");
6611 			ath12k_hw_to_ah(hw)->regd_updated = false;
6612 			ath12k_reg_handle_chan_list(ab, reg_info, arvif->ahvif->vdev_type,
6613 						    link_conf->power_type);
6614 		}
6615 
6616 	/* IEEE80211_STA_AUTHORIZED -> IEEE80211_STA_ASSOC: station may be in removal,
6617 	 * deauthorize it.
6618 	 */
6619 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
6620 		   new_state == IEEE80211_STA_ASSOC) {
6621 		ath12k_mac_station_unauthorize(ar, arvif, arsta);
6622 
6623 	/* IEEE80211_STA_ASSOC -> IEEE80211_STA_AUTH: disassoc peer connected to
6624 	 * AP/mesh/ADHOC vif type.
6625 	 */
6626 	} else if (old_state == IEEE80211_STA_ASSOC &&
6627 		   new_state == IEEE80211_STA_AUTH &&
6628 		   (vif->type == NL80211_IFTYPE_AP ||
6629 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
6630 		    vif->type == NL80211_IFTYPE_ADHOC)) {
6631 		ret = ath12k_mac_station_disassoc(ar, arvif, arsta);
6632 		if (ret)
6633 			ath12k_warn(ab, "Failed to disassociate station: %pM\n",
6634 				    arsta->addr);
6635 	}
6636 
6637 exit:
6638 	return ret;
6639 }
6640 
ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg * freq_range,u32 freq,u8 mac_id)6641 static bool ath12k_mac_is_freq_on_mac(struct ath12k_hw_mode_freq_range_arg *freq_range,
6642 				      u32 freq, u8 mac_id)
6643 {
6644 	return (freq >= freq_range[mac_id].low_2ghz_freq &&
6645 		freq <= freq_range[mac_id].high_2ghz_freq) ||
6646 	       (freq >= freq_range[mac_id].low_5ghz_freq &&
6647 		freq <= freq_range[mac_id].high_5ghz_freq);
6648 }
6649 
6650 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)6651 ath12k_mac_2_freq_same_mac_in_freq_range(struct ath12k_base *ab,
6652 					 struct ath12k_hw_mode_freq_range_arg *freq_range,
6653 					 u32 freq_link1, u32 freq_link2)
6654 {
6655 	u8 i;
6656 
6657 	for (i = 0; i < MAX_RADIOS; i++) {
6658 		if (ath12k_mac_is_freq_on_mac(freq_range, freq_link1, i) &&
6659 		    ath12k_mac_is_freq_on_mac(freq_range, freq_link2, i))
6660 			return true;
6661 	}
6662 
6663 	return false;
6664 }
6665 
ath12k_mac_is_hw_dbs_capable(struct ath12k_base * ab)6666 static bool ath12k_mac_is_hw_dbs_capable(struct ath12k_base *ab)
6667 {
6668 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
6669 			ab->wmi_ab.svc_map) &&
6670 	       ab->wmi_ab.hw_mode_info.support_dbs;
6671 }
6672 
ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)6673 static bool ath12k_mac_2_freq_same_mac_in_dbs(struct ath12k_base *ab,
6674 					      u32 freq_link1, u32 freq_link2)
6675 {
6676 	struct ath12k_hw_mode_freq_range_arg *freq_range;
6677 
6678 	if (!ath12k_mac_is_hw_dbs_capable(ab))
6679 		return true;
6680 
6681 	freq_range = ab->wmi_ab.hw_mode_info.freq_range_caps[ATH12K_HW_MODE_DBS];
6682 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, freq_range,
6683 							freq_link1, freq_link2);
6684 }
6685 
ath12k_mac_is_hw_sbs_capable(struct ath12k_base * ab)6686 static bool ath12k_mac_is_hw_sbs_capable(struct ath12k_base *ab)
6687 {
6688 	return test_bit(WMI_TLV_SERVICE_DUAL_BAND_SIMULTANEOUS_SUPPORT,
6689 			ab->wmi_ab.svc_map) &&
6690 	       ab->wmi_ab.hw_mode_info.support_sbs;
6691 }
6692 
ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)6693 static bool ath12k_mac_2_freq_same_mac_in_sbs(struct ath12k_base *ab,
6694 					      u32 freq_link1, u32 freq_link2)
6695 {
6696 	struct ath12k_hw_mode_info *info = &ab->wmi_ab.hw_mode_info;
6697 	struct ath12k_hw_mode_freq_range_arg *sbs_uppr_share;
6698 	struct ath12k_hw_mode_freq_range_arg *sbs_low_share;
6699 	struct ath12k_hw_mode_freq_range_arg *sbs_range;
6700 
6701 	if (!ath12k_mac_is_hw_sbs_capable(ab))
6702 		return true;
6703 
6704 	if (ab->wmi_ab.sbs_lower_band_end_freq) {
6705 		sbs_uppr_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_UPPER_SHARE];
6706 		sbs_low_share = info->freq_range_caps[ATH12K_HW_MODE_SBS_LOWER_SHARE];
6707 
6708 		return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_low_share,
6709 								freq_link1, freq_link2) ||
6710 		       ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_uppr_share,
6711 								freq_link1, freq_link2);
6712 	}
6713 
6714 	sbs_range = info->freq_range_caps[ATH12K_HW_MODE_SBS];
6715 	return ath12k_mac_2_freq_same_mac_in_freq_range(ab, sbs_range,
6716 							freq_link1, freq_link2);
6717 }
6718 
ath12k_mac_freqs_on_same_mac(struct ath12k_base * ab,u32 freq_link1,u32 freq_link2)6719 static bool ath12k_mac_freqs_on_same_mac(struct ath12k_base *ab,
6720 					 u32 freq_link1, u32 freq_link2)
6721 {
6722 	return ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_link1, freq_link2) &&
6723 	       ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_link1, freq_link2);
6724 }
6725 
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)6726 static int ath12k_mac_mlo_sta_set_link_active(struct ath12k_base *ab,
6727 					      enum wmi_mlo_link_force_reason reason,
6728 					      enum wmi_mlo_link_force_mode mode,
6729 					      u8 *mlo_vdev_id_lst,
6730 					      u8 num_mlo_vdev,
6731 					      u8 *mlo_inactive_vdev_lst,
6732 					      u8 num_mlo_inactive_vdev)
6733 {
6734 	struct wmi_mlo_link_set_active_arg param = {};
6735 	u32 entry_idx, entry_offset, vdev_idx;
6736 	u8 vdev_id;
6737 
6738 	param.reason = reason;
6739 	param.force_mode = mode;
6740 
6741 	for (vdev_idx = 0; vdev_idx < num_mlo_vdev; vdev_idx++) {
6742 		vdev_id = mlo_vdev_id_lst[vdev_idx];
6743 		entry_idx = vdev_id / 32;
6744 		entry_offset = vdev_id % 32;
6745 		if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
6746 			ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
6747 				    entry_idx, num_mlo_vdev, vdev_id);
6748 			return -EINVAL;
6749 		}
6750 		param.vdev_bitmap[entry_idx] |= 1 << entry_offset;
6751 		/* update entry number if entry index changed */
6752 		if (param.num_vdev_bitmap < entry_idx + 1)
6753 			param.num_vdev_bitmap = entry_idx + 1;
6754 	}
6755 
6756 	ath12k_dbg(ab, ATH12K_DBG_MAC,
6757 		   "num_vdev_bitmap %d vdev_bitmap[0] = 0x%x, vdev_bitmap[1] = 0x%x",
6758 		   param.num_vdev_bitmap, param.vdev_bitmap[0], param.vdev_bitmap[1]);
6759 
6760 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_INACTIVE) {
6761 		for (vdev_idx = 0; vdev_idx < num_mlo_inactive_vdev; vdev_idx++) {
6762 			vdev_id = mlo_inactive_vdev_lst[vdev_idx];
6763 			entry_idx = vdev_id / 32;
6764 			entry_offset = vdev_id % 32;
6765 			if (entry_idx >= WMI_MLO_LINK_NUM_SZ) {
6766 				ath12k_warn(ab, "Invalid entry_idx %d num_mlo_vdev %d vdev %d",
6767 					    entry_idx, num_mlo_inactive_vdev, vdev_id);
6768 				return -EINVAL;
6769 			}
6770 			param.inactive_vdev_bitmap[entry_idx] |= 1 << entry_offset;
6771 			/* update entry number if entry index changed */
6772 			if (param.num_inactive_vdev_bitmap < entry_idx + 1)
6773 				param.num_inactive_vdev_bitmap = entry_idx + 1;
6774 		}
6775 
6776 		ath12k_dbg(ab, ATH12K_DBG_MAC,
6777 			   "num_vdev_bitmap %d inactive_vdev_bitmap[0] = 0x%x, inactive_vdev_bitmap[1] = 0x%x",
6778 			   param.num_inactive_vdev_bitmap,
6779 			   param.inactive_vdev_bitmap[0],
6780 			   param.inactive_vdev_bitmap[1]);
6781 	}
6782 
6783 	if (mode == WMI_MLO_LINK_FORCE_MODE_ACTIVE_LINK_NUM ||
6784 	    mode == WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM) {
6785 		param.num_link_entry = 1;
6786 		param.link_num[0].num_of_link = num_mlo_vdev - 1;
6787 	}
6788 
6789 	return ath12k_wmi_send_mlo_link_set_active_cmd(ab, &param);
6790 }
6791 
ath12k_mac_mlo_sta_update_link_active(struct ath12k_base * ab,struct ieee80211_hw * hw,struct ath12k_vif * ahvif)6792 static int ath12k_mac_mlo_sta_update_link_active(struct ath12k_base *ab,
6793 						 struct ieee80211_hw *hw,
6794 						 struct ath12k_vif *ahvif)
6795 {
6796 	u8 mlo_vdev_id_lst[IEEE80211_MLD_MAX_NUM_LINKS] = {};
6797 	u32 mlo_freq_list[IEEE80211_MLD_MAX_NUM_LINKS] = {};
6798 	unsigned long links = ahvif->links_map;
6799 	enum wmi_mlo_link_force_reason reason;
6800 	struct ieee80211_chanctx_conf *conf;
6801 	enum wmi_mlo_link_force_mode mode;
6802 	struct ieee80211_bss_conf *info;
6803 	struct ath12k_link_vif *arvif;
6804 	u8 num_mlo_vdev = 0;
6805 	u8 link_id;
6806 
6807 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
6808 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
6809 		/* make sure vdev is created on this device */
6810 		if (!arvif || !arvif->is_created || arvif->ar->ab != ab)
6811 			continue;
6812 
6813 		info = ath12k_mac_get_link_bss_conf(arvif);
6814 		conf = wiphy_dereference(hw->wiphy, info->chanctx_conf);
6815 		mlo_freq_list[num_mlo_vdev] = conf->def.chan->center_freq;
6816 
6817 		mlo_vdev_id_lst[num_mlo_vdev] = arvif->vdev_id;
6818 		num_mlo_vdev++;
6819 	}
6820 
6821 	/* It is not allowed to activate more links than a single device
6822 	 * supported. Something goes wrong if we reach here.
6823 	 */
6824 	if (num_mlo_vdev > ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
6825 		WARN_ON_ONCE(1);
6826 		return -EINVAL;
6827 	}
6828 
6829 	/* if 2 links are established and both link channels fall on the
6830 	 * same hardware MAC, send command to firmware to deactivate one
6831 	 * of them.
6832 	 */
6833 	if (num_mlo_vdev == 2 &&
6834 	    ath12k_mac_freqs_on_same_mac(ab, mlo_freq_list[0],
6835 					 mlo_freq_list[1])) {
6836 		mode = WMI_MLO_LINK_FORCE_MODE_INACTIVE_LINK_NUM;
6837 		reason = WMI_MLO_LINK_FORCE_REASON_NEW_CONNECT;
6838 		return ath12k_mac_mlo_sta_set_link_active(ab, reason, mode,
6839 							  mlo_vdev_id_lst, num_mlo_vdev,
6840 							  NULL, 0);
6841 	}
6842 
6843 	return 0;
6844 }
6845 
ath12k_mac_are_sbs_chan(struct ath12k_base * ab,u32 freq_1,u32 freq_2)6846 static bool ath12k_mac_are_sbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
6847 {
6848 	if (!ath12k_mac_is_hw_sbs_capable(ab))
6849 		return false;
6850 
6851 	if (ath12k_is_2ghz_channel_freq(freq_1) ||
6852 	    ath12k_is_2ghz_channel_freq(freq_2))
6853 		return false;
6854 
6855 	return !ath12k_mac_2_freq_same_mac_in_sbs(ab, freq_1, freq_2);
6856 }
6857 
ath12k_mac_are_dbs_chan(struct ath12k_base * ab,u32 freq_1,u32 freq_2)6858 static bool ath12k_mac_are_dbs_chan(struct ath12k_base *ab, u32 freq_1, u32 freq_2)
6859 {
6860 	if (!ath12k_mac_is_hw_dbs_capable(ab))
6861 		return false;
6862 
6863 	return !ath12k_mac_2_freq_same_mac_in_dbs(ab, freq_1, freq_2);
6864 }
6865 
ath12k_mac_select_links(struct ath12k_base * ab,struct ieee80211_vif * vif,struct ieee80211_hw * hw,u16 * selected_links)6866 static int ath12k_mac_select_links(struct ath12k_base *ab,
6867 				   struct ieee80211_vif *vif,
6868 				   struct ieee80211_hw *hw,
6869 				   u16 *selected_links)
6870 {
6871 	unsigned long useful_links = ieee80211_vif_usable_links(vif);
6872 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6873 	u8 num_useful_links = hweight_long(useful_links);
6874 	struct ieee80211_chanctx_conf *chanctx;
6875 	struct ath12k_link_vif *assoc_arvif;
6876 	u32 assoc_link_freq, partner_freq;
6877 	u16 sbs_links = 0, dbs_links = 0;
6878 	struct ieee80211_bss_conf *info;
6879 	struct ieee80211_channel *chan;
6880 	struct ieee80211_sta *sta;
6881 	struct ath12k_sta *ahsta;
6882 	u8 link_id;
6883 
6884 	/* activate all useful links if less than max supported */
6885 	if (num_useful_links <= ATH12K_NUM_MAX_ACTIVE_LINKS_PER_DEVICE) {
6886 		*selected_links = useful_links;
6887 		return 0;
6888 	}
6889 
6890 	/* only in station mode we can get here, so it's safe
6891 	 * to use ap_addr
6892 	 */
6893 	rcu_read_lock();
6894 	sta = ieee80211_find_sta(vif, vif->cfg.ap_addr);
6895 	if (!sta) {
6896 		rcu_read_unlock();
6897 		ath12k_warn(ab, "failed to find sta with addr %pM\n", vif->cfg.ap_addr);
6898 		return -EINVAL;
6899 	}
6900 
6901 	ahsta = ath12k_sta_to_ahsta(sta);
6902 	assoc_arvif = wiphy_dereference(hw->wiphy, ahvif->link[ahsta->assoc_link_id]);
6903 	info = ath12k_mac_get_link_bss_conf(assoc_arvif);
6904 	chanctx = rcu_dereference(info->chanctx_conf);
6905 	assoc_link_freq = chanctx->def.chan->center_freq;
6906 	rcu_read_unlock();
6907 	ath12k_dbg(ab, ATH12K_DBG_MAC, "assoc link %u freq %u\n",
6908 		   assoc_arvif->link_id, assoc_link_freq);
6909 
6910 	/* assoc link is already activated and has to be kept active,
6911 	 * only need to select a partner link from others.
6912 	 */
6913 	useful_links &= ~BIT(assoc_arvif->link_id);
6914 	for_each_set_bit(link_id, &useful_links, IEEE80211_MLD_MAX_NUM_LINKS) {
6915 		info = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
6916 		if (!info) {
6917 			ath12k_warn(ab, "failed to get link info for link: %u\n",
6918 				    link_id);
6919 			return -ENOLINK;
6920 		}
6921 
6922 		chan = info->chanreq.oper.chan;
6923 		if (!chan) {
6924 			ath12k_warn(ab, "failed to get chan for link: %u\n", link_id);
6925 			return -EINVAL;
6926 		}
6927 
6928 		partner_freq = chan->center_freq;
6929 		if (ath12k_mac_are_sbs_chan(ab, assoc_link_freq, partner_freq)) {
6930 			sbs_links |= BIT(link_id);
6931 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new SBS link %u freq %u\n",
6932 				   link_id, partner_freq);
6933 			continue;
6934 		}
6935 
6936 		if (ath12k_mac_are_dbs_chan(ab, assoc_link_freq, partner_freq)) {
6937 			dbs_links |= BIT(link_id);
6938 			ath12k_dbg(ab, ATH12K_DBG_MAC, "new DBS link %u freq %u\n",
6939 				   link_id, partner_freq);
6940 			continue;
6941 		}
6942 
6943 		ath12k_dbg(ab, ATH12K_DBG_MAC, "non DBS/SBS link %u freq %u\n",
6944 			   link_id, partner_freq);
6945 	}
6946 
6947 	/* choose the first candidate no matter how many is in the list */
6948 	if (sbs_links)
6949 		link_id = __ffs(sbs_links);
6950 	else if (dbs_links)
6951 		link_id = __ffs(dbs_links);
6952 	else
6953 		link_id = ffs(useful_links) - 1;
6954 
6955 	ath12k_dbg(ab, ATH12K_DBG_MAC, "select partner link %u\n", link_id);
6956 
6957 	*selected_links = BIT(assoc_arvif->link_id) | BIT(link_id);
6958 
6959 	return 0;
6960 }
6961 
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)6962 static int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
6963 				   struct ieee80211_vif *vif,
6964 				   struct ieee80211_sta *sta,
6965 				   enum ieee80211_sta_state old_state,
6966 				   enum ieee80211_sta_state new_state)
6967 {
6968 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
6969 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
6970 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
6971 	struct ath12k_base *prev_ab = NULL, *ab;
6972 	struct ath12k_link_vif *arvif;
6973 	struct ath12k_link_sta *arsta;
6974 	unsigned long valid_links;
6975 	u16 selected_links = 0;
6976 	u8 link_id = 0, i;
6977 	struct ath12k *ar;
6978 	int ret;
6979 
6980 	lockdep_assert_wiphy(hw->wiphy);
6981 
6982 	if (ieee80211_vif_is_mld(vif) && sta->valid_links) {
6983 		WARN_ON(!sta->mlo && hweight16(sta->valid_links) != 1);
6984 		link_id = ffs(sta->valid_links) - 1;
6985 	}
6986 
6987 	/* IEEE80211_STA_NOTEXIST -> IEEE80211_STA_NONE:
6988 	 * New station add received. If this is a ML station then
6989 	 * ahsta->links_map will be zero and sta->valid_links will be 1.
6990 	 * Assign default link to the first link sta.
6991 	 */
6992 	if (old_state == IEEE80211_STA_NOTEXIST &&
6993 	    new_state == IEEE80211_STA_NONE) {
6994 		memset(ahsta, 0, sizeof(*ahsta));
6995 
6996 		arsta = &ahsta->deflink;
6997 
6998 		/* ML sta */
6999 		if (sta->mlo && !ahsta->links_map &&
7000 		    (hweight16(sta->valid_links) == 1)) {
7001 			ret = ath12k_peer_ml_create(ah, sta);
7002 			if (ret) {
7003 				ath12k_hw_warn(ah, "unable to create ML peer for sta %pM",
7004 					       sta->addr);
7005 				goto exit;
7006 			}
7007 		}
7008 
7009 		ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif,
7010 						 link_id);
7011 		if (ret) {
7012 			ath12k_hw_warn(ah, "unable assign link %d for sta %pM",
7013 				       link_id, sta->addr);
7014 			goto exit;
7015 		}
7016 
7017 		/* above arsta will get memset, hence do this after assign
7018 		 * link sta
7019 		 */
7020 		if (sta->mlo) {
7021 			/* For station mode, arvif->is_sta_assoc_link has been set when
7022 			 * vdev starts. Make sure the arvif/arsta pair have same setting
7023 			 */
7024 			if (vif->type == NL80211_IFTYPE_STATION &&
7025 			    !arsta->arvif->is_sta_assoc_link) {
7026 				ath12k_hw_warn(ah, "failed to verify assoc link setting with link id %u\n",
7027 					       link_id);
7028 				ret = -EINVAL;
7029 				goto exit;
7030 			}
7031 
7032 			arsta->is_assoc_link = true;
7033 			ahsta->assoc_link_id = link_id;
7034 		}
7035 	}
7036 
7037 	/* In the ML station scenario, activate all partner links once the
7038 	 * client is transitioning to the associated state.
7039 	 *
7040 	 * FIXME: Ideally, this activation should occur when the client
7041 	 * transitions to the authorized state. However, there are some
7042 	 * issues with handling this in the firmware. Until the firmware
7043 	 * can manage it properly, activate the links when the client is
7044 	 * about to move to the associated state.
7045 	 */
7046 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7047 	    old_state == IEEE80211_STA_AUTH && new_state == IEEE80211_STA_ASSOC) {
7048 		/* TODO: for now only do link selection for single device
7049 		 * MLO case. Other cases would be handled in the future.
7050 		 */
7051 		ab = ah->radio[0].ab;
7052 		if (ab->ag->num_devices == 1) {
7053 			ret = ath12k_mac_select_links(ab, vif, hw, &selected_links);
7054 			if (ret) {
7055 				ath12k_warn(ab,
7056 					    "failed to get selected links: %d\n", ret);
7057 				goto exit;
7058 			}
7059 		} else {
7060 			selected_links = ieee80211_vif_usable_links(vif);
7061 		}
7062 
7063 		ieee80211_set_active_links(vif, selected_links);
7064 	}
7065 
7066 	/* Handle all the other state transitions in generic way */
7067 	valid_links = ahsta->links_map;
7068 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7069 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7070 		arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7071 		/* some assumptions went wrong! */
7072 		if (WARN_ON(!arvif || !arsta))
7073 			continue;
7074 
7075 		/* vdev might be in deleted */
7076 		if (WARN_ON(!arvif->ar))
7077 			continue;
7078 
7079 		ret = ath12k_mac_handle_link_sta_state(hw, arvif, arsta,
7080 						       old_state, new_state);
7081 		if (ret) {
7082 			ath12k_hw_warn(ah, "unable to move link sta %d of sta %pM from state %d to %d",
7083 				       link_id, arsta->addr, old_state, new_state);
7084 			goto exit;
7085 		}
7086 	}
7087 
7088 	if (ieee80211_vif_is_mld(vif) && vif->type == NL80211_IFTYPE_STATION &&
7089 	    old_state == IEEE80211_STA_ASSOC && new_state == IEEE80211_STA_AUTHORIZED) {
7090 		for_each_ar(ah, ar, i) {
7091 			ab = ar->ab;
7092 			if (prev_ab == ab)
7093 				continue;
7094 
7095 			ret = ath12k_mac_mlo_sta_update_link_active(ab, hw, ahvif);
7096 			if (ret) {
7097 				ath12k_warn(ab,
7098 					    "failed to update link active state on connect %d\n",
7099 					    ret);
7100 				goto exit;
7101 			}
7102 
7103 			prev_ab = ab;
7104 		}
7105 	}
7106 	/* IEEE80211_STA_NONE -> IEEE80211_STA_NOTEXIST:
7107 	 * Remove the station from driver (handle ML sta here since that
7108 	 * needs special handling. Normal sta will be handled in generic
7109 	 * handler below
7110 	 */
7111 	if (old_state == IEEE80211_STA_NONE &&
7112 	    new_state == IEEE80211_STA_NOTEXIST && sta->mlo)
7113 		ath12k_mac_ml_station_remove(ahvif, ahsta);
7114 
7115 	ret = 0;
7116 
7117 exit:
7118 	/* update the state if everything went well */
7119 	if (!ret)
7120 		ahsta->state = new_state;
7121 
7122 	return ret;
7123 }
7124 
ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)7125 static int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
7126 				       struct ieee80211_vif *vif,
7127 				       struct ieee80211_sta *sta)
7128 {
7129 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7130 	struct ath12k *ar;
7131 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7132 	struct ath12k_link_vif *arvif;
7133 	struct ath12k_link_sta *arsta;
7134 	u8 link_id;
7135 	int ret;
7136 	s16 txpwr;
7137 
7138 	lockdep_assert_wiphy(hw->wiphy);
7139 
7140 	/* TODO: use link id from mac80211 once that's implemented */
7141 	link_id = 0;
7142 
7143 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7144 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_id]);
7145 
7146 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
7147 		txpwr = 0;
7148 	} else {
7149 		txpwr = sta->deflink.txpwr.power;
7150 		if (!txpwr) {
7151 			ret = -EINVAL;
7152 			goto out;
7153 		}
7154 	}
7155 
7156 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL) {
7157 		ret = -EINVAL;
7158 		goto out;
7159 	}
7160 
7161 	ar = arvif->ar;
7162 
7163 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr, arvif->vdev_id,
7164 					WMI_PEER_USE_FIXED_PWR, txpwr);
7165 	if (ret) {
7166 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
7167 			    ret);
7168 		goto out;
7169 	}
7170 
7171 out:
7172 	return ret;
7173 }
7174 
ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,u32 changed)7175 static void ath12k_mac_op_link_sta_rc_update(struct ieee80211_hw *hw,
7176 					     struct ieee80211_vif *vif,
7177 					     struct ieee80211_link_sta *link_sta,
7178 					     u32 changed)
7179 {
7180 	struct ieee80211_sta *sta = link_sta->sta;
7181 	struct ath12k *ar;
7182 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7183 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7184 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
7185 	struct ath12k_link_sta *arsta;
7186 	struct ath12k_link_vif *arvif;
7187 	struct ath12k_peer *peer;
7188 	u32 bw, smps;
7189 
7190 	rcu_read_lock();
7191 	arvif = rcu_dereference(ahvif->link[link_sta->link_id]);
7192 	if (!arvif) {
7193 		ath12k_hw_warn(ah, "mac sta rc update failed to fetch link vif on link id %u for peer %pM\n",
7194 			       link_sta->link_id, sta->addr);
7195 		rcu_read_unlock();
7196 		return;
7197 	}
7198 
7199 	ar = arvif->ar;
7200 
7201 	arsta = rcu_dereference(ahsta->link[link_sta->link_id]);
7202 	if (!arsta) {
7203 		rcu_read_unlock();
7204 		ath12k_warn(ar->ab, "mac sta rc update failed to fetch link sta on link id %u for peer %pM\n",
7205 			    link_sta->link_id, sta->addr);
7206 		return;
7207 	}
7208 	spin_lock_bh(&ar->ab->base_lock);
7209 
7210 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arsta->addr);
7211 	if (!peer) {
7212 		spin_unlock_bh(&ar->ab->base_lock);
7213 		rcu_read_unlock();
7214 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
7215 			    arsta->addr, arvif->vdev_id);
7216 		return;
7217 	}
7218 
7219 	spin_unlock_bh(&ar->ab->base_lock);
7220 
7221 	if (arsta->link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
7222 		rcu_read_unlock();
7223 		return;
7224 	}
7225 
7226 	link_sta = rcu_dereference(sta->link[arsta->link_id]);
7227 	if (!link_sta) {
7228 		rcu_read_unlock();
7229 		ath12k_warn(ar->ab, "unable to access link sta in rc update for sta %pM link %u\n",
7230 			    sta->addr, arsta->link_id);
7231 		return;
7232 	}
7233 
7234 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
7235 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
7236 		   arsta->addr, changed, link_sta->bandwidth, link_sta->rx_nss,
7237 		   link_sta->smps_mode);
7238 
7239 	spin_lock_bh(&ar->data_lock);
7240 
7241 	if (changed & IEEE80211_RC_BW_CHANGED) {
7242 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, link_sta);
7243 		arsta->bw_prev = arsta->bw;
7244 		arsta->bw = bw;
7245 	}
7246 
7247 	if (changed & IEEE80211_RC_NSS_CHANGED)
7248 		arsta->nss = link_sta->rx_nss;
7249 
7250 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
7251 		smps = WMI_PEER_SMPS_PS_NONE;
7252 
7253 		switch (link_sta->smps_mode) {
7254 		case IEEE80211_SMPS_AUTOMATIC:
7255 		case IEEE80211_SMPS_OFF:
7256 			smps = WMI_PEER_SMPS_PS_NONE;
7257 			break;
7258 		case IEEE80211_SMPS_STATIC:
7259 			smps = WMI_PEER_SMPS_STATIC;
7260 			break;
7261 		case IEEE80211_SMPS_DYNAMIC:
7262 			smps = WMI_PEER_SMPS_DYNAMIC;
7263 			break;
7264 		default:
7265 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM link %u\n",
7266 				    link_sta->smps_mode, arsta->addr, link_sta->link_id);
7267 			smps = WMI_PEER_SMPS_PS_NONE;
7268 			break;
7269 		}
7270 
7271 		arsta->smps = smps;
7272 	}
7273 
7274 	arsta->changed |= changed;
7275 
7276 	spin_unlock_bh(&ar->data_lock);
7277 
7278 	wiphy_work_queue(hw->wiphy, &arsta->update_wk);
7279 
7280 	rcu_read_unlock();
7281 }
7282 
ath12k_mac_alloc_assign_link_sta(struct ath12k_hw * ah,struct ath12k_sta * ahsta,struct ath12k_vif * ahvif,u8 link_id)7283 static struct ath12k_link_sta *ath12k_mac_alloc_assign_link_sta(struct ath12k_hw *ah,
7284 								struct ath12k_sta *ahsta,
7285 								struct ath12k_vif *ahvif,
7286 								u8 link_id)
7287 {
7288 	struct ath12k_link_sta *arsta;
7289 	int ret;
7290 
7291 	lockdep_assert_wiphy(ah->hw->wiphy);
7292 
7293 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7294 		return NULL;
7295 
7296 	arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
7297 	if (arsta)
7298 		return NULL;
7299 
7300 	arsta = kmalloc(sizeof(*arsta), GFP_KERNEL);
7301 	if (!arsta)
7302 		return NULL;
7303 
7304 	ret = ath12k_mac_assign_link_sta(ah, ahsta, arsta, ahvif, link_id);
7305 	if (ret) {
7306 		kfree(arsta);
7307 		return NULL;
7308 	}
7309 
7310 	return arsta;
7311 }
7312 
ath12k_mac_op_change_sta_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 old_links,u16 new_links)7313 static int ath12k_mac_op_change_sta_links(struct ieee80211_hw *hw,
7314 					  struct ieee80211_vif *vif,
7315 					  struct ieee80211_sta *sta,
7316 					  u16 old_links, u16 new_links)
7317 {
7318 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7319 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
7320 	struct ath12k_hw *ah = hw->priv;
7321 	struct ath12k_link_vif *arvif;
7322 	struct ath12k_link_sta *arsta;
7323 	unsigned long valid_links;
7324 	struct ath12k *ar;
7325 	u8 link_id;
7326 	int ret;
7327 
7328 	lockdep_assert_wiphy(hw->wiphy);
7329 
7330 	if (!sta->valid_links)
7331 		return -EINVAL;
7332 
7333 	/* Firmware does not support removal of one of link stas. All sta
7334 	 * would be removed during ML STA delete in sta_state(), hence link
7335 	 * sta removal is not handled here.
7336 	 */
7337 	if (new_links < old_links)
7338 		return 0;
7339 
7340 	if (ahsta->ml_peer_id == ATH12K_MLO_PEER_ID_INVALID) {
7341 		ath12k_hw_warn(ah, "unable to add link for ml sta %pM", sta->addr);
7342 		return -EINVAL;
7343 	}
7344 
7345 	/* this op is expected only after initial sta insertion with default link */
7346 	if (WARN_ON(ahsta->links_map == 0))
7347 		return -EINVAL;
7348 
7349 	valid_links = new_links;
7350 	for_each_set_bit(link_id, &valid_links, IEEE80211_MLD_MAX_NUM_LINKS) {
7351 		if (ahsta->links_map & BIT(link_id))
7352 			continue;
7353 
7354 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7355 		arsta = ath12k_mac_alloc_assign_link_sta(ah, ahsta, ahvif, link_id);
7356 
7357 		if (!arvif || !arsta) {
7358 			ath12k_hw_warn(ah, "Failed to alloc/assign link sta");
7359 			continue;
7360 		}
7361 
7362 		ar = arvif->ar;
7363 		if (!ar)
7364 			continue;
7365 
7366 		ret = ath12k_mac_station_add(ar, arvif, arsta);
7367 		if (ret) {
7368 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
7369 				    arsta->addr, arvif->vdev_id);
7370 			ath12k_mac_free_unassign_link_sta(ah, ahsta, link_id);
7371 			return ret;
7372 		}
7373 	}
7374 
7375 	return 0;
7376 }
7377 
ath12k_mac_op_can_activate_links(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 active_links)7378 static bool ath12k_mac_op_can_activate_links(struct ieee80211_hw *hw,
7379 					     struct ieee80211_vif *vif,
7380 					     u16 active_links)
7381 {
7382 	/* TODO: Handle recovery case */
7383 
7384 	return true;
7385 }
7386 
ath12k_conf_tx_uapsd(struct ath12k_link_vif * arvif,u16 ac,bool enable)7387 static int ath12k_conf_tx_uapsd(struct ath12k_link_vif *arvif,
7388 				u16 ac, bool enable)
7389 {
7390 	struct ath12k *ar = arvif->ar;
7391 	struct ath12k_vif *ahvif = arvif->ahvif;
7392 	u32 value;
7393 	int ret;
7394 
7395 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA)
7396 		return 0;
7397 
7398 	switch (ac) {
7399 	case IEEE80211_AC_VO:
7400 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
7401 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
7402 		break;
7403 	case IEEE80211_AC_VI:
7404 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
7405 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
7406 		break;
7407 	case IEEE80211_AC_BE:
7408 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
7409 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
7410 		break;
7411 	case IEEE80211_AC_BK:
7412 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
7413 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
7414 		break;
7415 	}
7416 
7417 	if (enable)
7418 		ahvif->u.sta.uapsd |= value;
7419 	else
7420 		ahvif->u.sta.uapsd &= ~value;
7421 
7422 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7423 					  WMI_STA_PS_PARAM_UAPSD,
7424 					  ahvif->u.sta.uapsd);
7425 	if (ret) {
7426 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
7427 		goto exit;
7428 	}
7429 
7430 	if (ahvif->u.sta.uapsd)
7431 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
7432 	else
7433 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
7434 
7435 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7436 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
7437 					  value);
7438 	if (ret)
7439 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
7440 
7441 exit:
7442 	return ret;
7443 }
7444 
ath12k_mac_conf_tx(struct ath12k_link_vif * arvif,u16 ac,const struct ieee80211_tx_queue_params * params)7445 static int ath12k_mac_conf_tx(struct ath12k_link_vif *arvif, u16 ac,
7446 			      const struct ieee80211_tx_queue_params *params)
7447 {
7448 	struct wmi_wmm_params_arg *p = NULL;
7449 	struct ath12k *ar = arvif->ar;
7450 	struct ath12k_base *ab = ar->ab;
7451 	int ret;
7452 
7453 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
7454 
7455 	switch (ac) {
7456 	case IEEE80211_AC_VO:
7457 		p = &arvif->wmm_params.ac_vo;
7458 		break;
7459 	case IEEE80211_AC_VI:
7460 		p = &arvif->wmm_params.ac_vi;
7461 		break;
7462 	case IEEE80211_AC_BE:
7463 		p = &arvif->wmm_params.ac_be;
7464 		break;
7465 	case IEEE80211_AC_BK:
7466 		p = &arvif->wmm_params.ac_bk;
7467 		break;
7468 	}
7469 
7470 	if (WARN_ON(!p)) {
7471 		ret = -EINVAL;
7472 		goto exit;
7473 	}
7474 
7475 	p->cwmin = params->cw_min;
7476 	p->cwmax = params->cw_max;
7477 	p->aifs = params->aifs;
7478 	p->txop = params->txop;
7479 
7480 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
7481 					     &arvif->wmm_params);
7482 	if (ret) {
7483 		ath12k_warn(ab, "pdev idx %d failed to set wmm params: %d\n",
7484 			    ar->pdev_idx, ret);
7485 		goto exit;
7486 	}
7487 
7488 	ret = ath12k_conf_tx_uapsd(arvif, ac, params->uapsd);
7489 	if (ret)
7490 		ath12k_warn(ab, "pdev idx %d failed to set sta uapsd: %d\n",
7491 			    ar->pdev_idx, ret);
7492 
7493 exit:
7494 	return ret;
7495 }
7496 
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)7497 static int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
7498 				 struct ieee80211_vif *vif,
7499 				 unsigned int link_id, u16 ac,
7500 				 const struct ieee80211_tx_queue_params *params)
7501 {
7502 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
7503 	struct ath12k_link_vif *arvif;
7504 	struct ath12k_vif_cache *cache;
7505 	int ret;
7506 
7507 	lockdep_assert_wiphy(hw->wiphy);
7508 
7509 	if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS)
7510 		return -EINVAL;
7511 
7512 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
7513 	if (!arvif || !arvif->is_created) {
7514 		cache = ath12k_ahvif_get_link_cache(ahvif, link_id);
7515 		if (!cache)
7516 			return -ENOSPC;
7517 
7518 		cache->tx_conf.changed = true;
7519 		cache->tx_conf.ac = ac;
7520 		cache->tx_conf.tx_queue_params = *params;
7521 
7522 		return 0;
7523 	}
7524 
7525 	ret = ath12k_mac_conf_tx(arvif, ac, params);
7526 
7527 	return ret;
7528 }
7529 
7530 static struct ieee80211_sta_ht_cap
ath12k_create_ht_cap(struct ath12k * ar,u32 ar_ht_cap,u32 rate_cap_rx_chainmask)7531 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
7532 {
7533 	int i;
7534 	struct ieee80211_sta_ht_cap ht_cap = {};
7535 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
7536 
7537 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
7538 		return ht_cap;
7539 
7540 	ht_cap.ht_supported = 1;
7541 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
7542 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
7543 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
7544 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
7545 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
7546 
7547 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
7548 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
7549 
7550 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
7551 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
7552 
7553 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
7554 		u32 smps;
7555 
7556 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
7557 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
7558 
7559 		ht_cap.cap |= smps;
7560 	}
7561 
7562 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
7563 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
7564 
7565 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
7566 		u32 stbc;
7567 
7568 		stbc   = ar_ht_cap;
7569 		stbc  &= WMI_HT_CAP_RX_STBC;
7570 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
7571 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
7572 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
7573 
7574 		ht_cap.cap |= stbc;
7575 	}
7576 
7577 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
7578 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
7579 
7580 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
7581 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
7582 
7583 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
7584 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
7585 
7586 	for (i = 0; i < ar->num_rx_chains; i++) {
7587 		if (rate_cap_rx_chainmask & BIT(i))
7588 			ht_cap.mcs.rx_mask[i] = 0xFF;
7589 	}
7590 
7591 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
7592 
7593 	return ht_cap;
7594 }
7595 
ath12k_mac_set_txbf_conf(struct ath12k_link_vif * arvif)7596 static int ath12k_mac_set_txbf_conf(struct ath12k_link_vif *arvif)
7597 {
7598 	u32 value = 0;
7599 	struct ath12k *ar = arvif->ar;
7600 	struct ath12k_vif *ahvif = arvif->ahvif;
7601 	int nsts;
7602 	int sound_dim;
7603 	u32 vht_cap = ar->pdev->cap.vht_cap;
7604 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
7605 
7606 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
7607 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
7608 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
7609 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
7610 	}
7611 
7612 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
7613 		sound_dim = vht_cap &
7614 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
7615 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
7616 		if (sound_dim > (ar->num_tx_chains - 1))
7617 			sound_dim = ar->num_tx_chains - 1;
7618 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
7619 	}
7620 
7621 	if (!value)
7622 		return 0;
7623 
7624 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
7625 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
7626 
7627 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
7628 		    ahvif->vdev_type == WMI_VDEV_TYPE_AP)
7629 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
7630 	}
7631 
7632 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
7633 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
7634 
7635 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
7636 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
7637 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
7638 	}
7639 
7640 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
7641 					     vdev_param, value);
7642 }
7643 
ath12k_set_vht_txbf_cap(struct ath12k * ar,u32 * vht_cap)7644 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
7645 {
7646 	bool subfer, subfee;
7647 	int sound_dim = 0;
7648 
7649 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
7650 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
7651 
7652 	if (ar->num_tx_chains < 2) {
7653 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
7654 		subfer = false;
7655 	}
7656 
7657 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
7658 	if (!subfer)
7659 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
7660 
7661 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
7662 	if (!subfee)
7663 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
7664 
7665 	sound_dim = u32_get_bits(*vht_cap,
7666 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
7667 	*vht_cap = u32_replace_bits(*vht_cap, 0,
7668 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
7669 
7670 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
7671 
7672 	/* Enable Sounding Dimension Field only if SU BF is enabled */
7673 	if (subfer) {
7674 		if (sound_dim > (ar->num_tx_chains - 1))
7675 			sound_dim = ar->num_tx_chains - 1;
7676 
7677 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
7678 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
7679 	}
7680 
7681 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
7682 	if (!subfee)
7683 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
7684 }
7685 
7686 static struct ieee80211_sta_vht_cap
ath12k_create_vht_cap(struct ath12k * ar,u32 rate_cap_tx_chainmask,u32 rate_cap_rx_chainmask)7687 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
7688 		      u32 rate_cap_rx_chainmask)
7689 {
7690 	struct ieee80211_sta_vht_cap vht_cap = {};
7691 	u16 txmcs_map, rxmcs_map;
7692 	int i;
7693 
7694 	vht_cap.vht_supported = 1;
7695 	vht_cap.cap = ar->pdev->cap.vht_cap;
7696 
7697 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
7698 
7699 	/* 80P80 is not supported */
7700 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
7701 
7702 	rxmcs_map = 0;
7703 	txmcs_map = 0;
7704 	for (i = 0; i < 8; i++) {
7705 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
7706 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
7707 		else
7708 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
7709 
7710 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
7711 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
7712 		else
7713 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
7714 	}
7715 
7716 	if (rate_cap_tx_chainmask <= 1)
7717 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
7718 
7719 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
7720 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
7721 
7722 	/* Check if the HW supports 1:1 NSS ratio and reset
7723 	 * EXT NSS BW Support field to 0 to indicate 1:1 ratio
7724 	 */
7725 	if (ar->pdev->cap.nss_ratio_info == WMI_NSS_RATIO_1_NSS)
7726 		vht_cap.cap &= ~IEEE80211_VHT_CAP_EXT_NSS_BW_MASK;
7727 
7728 	return vht_cap;
7729 }
7730 
ath12k_mac_setup_ht_vht_cap(struct ath12k * ar,struct ath12k_pdev_cap * cap,u32 * ht_cap_info)7731 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
7732 					struct ath12k_pdev_cap *cap,
7733 					u32 *ht_cap_info)
7734 {
7735 	struct ieee80211_supported_band *band;
7736 	u32 rate_cap_tx_chainmask;
7737 	u32 rate_cap_rx_chainmask;
7738 	u32 ht_cap;
7739 
7740 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
7741 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
7742 
7743 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
7744 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
7745 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
7746 		if (ht_cap_info)
7747 			*ht_cap_info = ht_cap;
7748 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
7749 						    rate_cap_rx_chainmask);
7750 	}
7751 
7752 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
7753 	    (ar->ab->hw_params->single_pdev_only ||
7754 	     !ar->supports_6ghz)) {
7755 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
7756 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
7757 		if (ht_cap_info)
7758 			*ht_cap_info = ht_cap;
7759 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
7760 						    rate_cap_rx_chainmask);
7761 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
7762 						      rate_cap_rx_chainmask);
7763 	}
7764 }
7765 
ath12k_check_chain_mask(struct ath12k * ar,u32 ant,bool is_tx_ant)7766 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
7767 {
7768 	/* TODO: Check the request chainmask against the supported
7769 	 * chainmask table which is advertised in extented_service_ready event
7770 	 */
7771 
7772 	return 0;
7773 }
7774 
ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg * fw_ppet,u8 * he_ppet)7775 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
7776 				  u8 *he_ppet)
7777 {
7778 	int nss, ru;
7779 	u8 bit = 7;
7780 
7781 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
7782 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
7783 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
7784 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
7785 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
7786 		for (ru = 0; ru < 4; ru++) {
7787 			u8 val;
7788 			int i;
7789 
7790 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
7791 				continue;
7792 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
7793 			       0x3f;
7794 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
7795 			for (i = 5; i >= 0; i--) {
7796 				he_ppet[bit / 8] |=
7797 					((val >> i) & 0x1) << ((bit % 8));
7798 				bit++;
7799 			}
7800 		}
7801 	}
7802 }
7803 
7804 static void
ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem * he_cap_elem)7805 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
7806 {
7807 	u8 m;
7808 
7809 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
7810 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
7811 	he_cap_elem->mac_cap_info[0] &= ~m;
7812 
7813 	m = IEEE80211_HE_MAC_CAP2_TRS |
7814 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
7815 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
7816 	he_cap_elem->mac_cap_info[2] &= ~m;
7817 
7818 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
7819 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
7820 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
7821 	he_cap_elem->mac_cap_info[3] &= ~m;
7822 
7823 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
7824 	    IEEE80211_HE_MAC_CAP4_BQR;
7825 	he_cap_elem->mac_cap_info[4] &= ~m;
7826 
7827 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
7828 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
7829 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
7830 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
7831 	he_cap_elem->mac_cap_info[5] &= ~m;
7832 
7833 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
7834 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
7835 	he_cap_elem->phy_cap_info[2] &= ~m;
7836 
7837 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
7838 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
7839 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
7840 	he_cap_elem->phy_cap_info[3] &= ~m;
7841 
7842 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
7843 	he_cap_elem->phy_cap_info[4] &= ~m;
7844 
7845 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
7846 	he_cap_elem->phy_cap_info[5] &= ~m;
7847 
7848 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
7849 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
7850 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
7851 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
7852 	he_cap_elem->phy_cap_info[6] &= ~m;
7853 
7854 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
7855 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
7856 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
7857 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
7858 	he_cap_elem->phy_cap_info[7] &= ~m;
7859 
7860 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
7861 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
7862 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
7863 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
7864 	he_cap_elem->phy_cap_info[8] &= ~m;
7865 
7866 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
7867 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
7868 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
7869 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
7870 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
7871 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
7872 	he_cap_elem->phy_cap_info[9] &= ~m;
7873 }
7874 
ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap * pcap,struct ath12k_band_cap * bcap)7875 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
7876 					   struct ath12k_band_cap *bcap)
7877 {
7878 	u8 val;
7879 
7880 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
7881 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
7882 		bcap->he_6ghz_capa |=
7883 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
7884 					IEEE80211_HE_6GHZ_CAP_SM_PS);
7885 	else
7886 		bcap->he_6ghz_capa |=
7887 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
7888 					IEEE80211_HE_6GHZ_CAP_SM_PS);
7889 	val = u32_get_bits(pcap->vht_cap,
7890 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
7891 	bcap->he_6ghz_capa |=
7892 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
7893 	val = u32_get_bits(pcap->vht_cap,
7894 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
7895 	bcap->he_6ghz_capa |=
7896 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
7897 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
7898 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
7899 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
7900 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
7901 
7902 	return cpu_to_le16(bcap->he_6ghz_capa);
7903 }
7904 
ath12k_mac_set_hemcsmap(struct ath12k * ar,struct ath12k_pdev_cap * cap,struct ieee80211_sta_he_cap * he_cap)7905 static void ath12k_mac_set_hemcsmap(struct ath12k *ar,
7906 				    struct ath12k_pdev_cap *cap,
7907 				    struct ieee80211_sta_he_cap *he_cap)
7908 {
7909 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
7910 	u8 maxtxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_tx_chains);
7911 	u8 maxrxnss_160 = ath12k_get_nss_160mhz(ar, ar->num_rx_chains);
7912 	u16 txmcs_map_160 = 0, rxmcs_map_160 = 0;
7913 	u16 txmcs_map = 0, rxmcs_map = 0;
7914 	u32 i;
7915 
7916 	for (i = 0; i < 8; i++) {
7917 		if (i < ar->num_tx_chains &&
7918 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
7919 			txmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
7920 		else
7921 			txmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
7922 
7923 		if (i < ar->num_rx_chains &&
7924 		    (ar->cfg_rx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
7925 			rxmcs_map |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
7926 		else
7927 			rxmcs_map |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
7928 
7929 		if (i < maxtxnss_160 &&
7930 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
7931 			txmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
7932 		else
7933 			txmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
7934 
7935 		if (i < maxrxnss_160 &&
7936 		    (ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift) & BIT(i))
7937 			rxmcs_map_160 |= IEEE80211_HE_MCS_SUPPORT_0_11 << (i * 2);
7938 		else
7939 			rxmcs_map_160 |= IEEE80211_HE_MCS_NOT_SUPPORTED << (i * 2);
7940 	}
7941 
7942 	mcs_nss->rx_mcs_80 = cpu_to_le16(rxmcs_map & 0xffff);
7943 	mcs_nss->tx_mcs_80 = cpu_to_le16(txmcs_map & 0xffff);
7944 	mcs_nss->rx_mcs_160 = cpu_to_le16(rxmcs_map_160 & 0xffff);
7945 	mcs_nss->tx_mcs_160 = cpu_to_le16(txmcs_map_160 & 0xffff);
7946 }
7947 
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)7948 static void ath12k_mac_copy_he_cap(struct ath12k *ar,
7949 				   struct ath12k_band_cap *band_cap,
7950 				   int iftype, u8 num_tx_chains,
7951 				   struct ieee80211_sta_he_cap *he_cap)
7952 {
7953 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
7954 
7955 	he_cap->has_he = true;
7956 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
7957 	       sizeof(he_cap_elem->mac_cap_info));
7958 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
7959 	       sizeof(he_cap_elem->phy_cap_info));
7960 
7961 	he_cap_elem->mac_cap_info[1] &=
7962 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
7963 	he_cap_elem->phy_cap_info[0] &=
7964 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
7965 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
7966 		IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
7967 	/* 80PLUS80 is not supported */
7968 	he_cap_elem->phy_cap_info[0] &=
7969 		~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G;
7970 	he_cap_elem->phy_cap_info[5] &=
7971 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
7972 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
7973 
7974 	switch (iftype) {
7975 	case NL80211_IFTYPE_AP:
7976 		he_cap_elem->mac_cap_info[2] &=
7977 			~IEEE80211_HE_MAC_CAP2_BCAST_TWT;
7978 		he_cap_elem->phy_cap_info[3] &=
7979 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
7980 		he_cap_elem->phy_cap_info[9] |=
7981 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
7982 		break;
7983 	case NL80211_IFTYPE_STATION:
7984 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
7985 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
7986 		he_cap_elem->phy_cap_info[9] |=
7987 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
7988 		break;
7989 	case NL80211_IFTYPE_MESH_POINT:
7990 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
7991 		break;
7992 	}
7993 
7994 	ath12k_mac_set_hemcsmap(ar, &ar->pdev->cap, he_cap);
7995 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
7996 	if (he_cap_elem->phy_cap_info[6] &
7997 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
7998 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
7999 }
8000 
8001 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)8002 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
8003 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
8004 			    const struct ieee80211_he_cap_elem *he_cap,
8005 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
8006 {
8007 	if ((he_cap->phy_cap_info[0] &
8008 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8009 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
8010 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
8011 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
8012 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
8013 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
8014 
8015 	if (he_cap->phy_cap_info[0] &
8016 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
8017 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
8018 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
8019 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8020 
8021 	if (he_cap->phy_cap_info[0] &
8022 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
8023 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
8024 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8025 
8026 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
8027 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
8028 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
8029 }
8030 
ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg * fw_ppet,struct ieee80211_sta_eht_cap * cap)8031 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
8032 					   struct ieee80211_sta_eht_cap *cap)
8033 {
8034 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
8035 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
8036 
8037 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
8038 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
8039 
8040 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
8041 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8042 
8043 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
8044 		for (ru = 0;
8045 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
8046 		     ru++) {
8047 			u32 val = 0;
8048 
8049 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
8050 				continue;
8051 
8052 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
8053 						(ru * ppet_bit_len_per_ru),
8054 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
8055 
8056 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
8057 				cap->eht_ppe_thres[bit / 8] |=
8058 					(((val >> i) & 0x1) << ((bit % 8)));
8059 				bit++;
8060 			}
8061 		}
8062 	}
8063 }
8064 
8065 static void
ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed * eht_cap_elem)8066 ath12k_mac_filter_eht_cap_mesh(struct ieee80211_eht_cap_elem_fixed
8067 			       *eht_cap_elem)
8068 {
8069 	u8 m;
8070 
8071 	m = IEEE80211_EHT_MAC_CAP0_EPCS_PRIO_ACCESS;
8072 	eht_cap_elem->mac_cap_info[0] &= ~m;
8073 
8074 	m = IEEE80211_EHT_PHY_CAP0_PARTIAL_BW_UL_MU_MIMO;
8075 	eht_cap_elem->phy_cap_info[0] &= ~m;
8076 
8077 	m = IEEE80211_EHT_PHY_CAP3_NG_16_MU_FEEDBACK |
8078 	    IEEE80211_EHT_PHY_CAP3_CODEBOOK_7_5_MU_FDBK |
8079 	    IEEE80211_EHT_PHY_CAP3_TRIG_MU_BF_PART_BW_FDBK |
8080 	    IEEE80211_EHT_PHY_CAP3_TRIG_CQI_FDBK;
8081 	eht_cap_elem->phy_cap_info[3] &= ~m;
8082 
8083 	m = IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO |
8084 	    IEEE80211_EHT_PHY_CAP4_PSR_SR_SUPP |
8085 	    IEEE80211_EHT_PHY_CAP4_POWER_BOOST_FACT_SUPP |
8086 	    IEEE80211_EHT_PHY_CAP4_EHT_MU_PPDU_4_EHT_LTF_08_GI;
8087 	eht_cap_elem->phy_cap_info[4] &= ~m;
8088 
8089 	m = IEEE80211_EHT_PHY_CAP5_NON_TRIG_CQI_FEEDBACK |
8090 	    IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP |
8091 	    IEEE80211_EHT_PHY_CAP5_RX_LESS_242_TONE_RU_SUPP |
8092 	    IEEE80211_EHT_PHY_CAP5_MAX_NUM_SUPP_EHT_LTF_MASK;
8093 	eht_cap_elem->phy_cap_info[5] &= ~m;
8094 
8095 	m = IEEE80211_EHT_PHY_CAP6_MAX_NUM_SUPP_EHT_LTF_MASK;
8096 	eht_cap_elem->phy_cap_info[6] &= ~m;
8097 
8098 	m = IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8099 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8100 	    IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ |
8101 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8102 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8103 	    IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ;
8104 	eht_cap_elem->phy_cap_info[7] &= ~m;
8105 }
8106 
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)8107 static void ath12k_mac_copy_eht_cap(struct ath12k *ar,
8108 				    struct ath12k_band_cap *band_cap,
8109 				    struct ieee80211_he_cap_elem *he_cap_elem,
8110 				    int iftype,
8111 				    struct ieee80211_sta_eht_cap *eht_cap)
8112 {
8113 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
8114 
8115 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
8116 
8117 	if (!(test_bit(WMI_TLV_SERVICE_11BE, ar->ab->wmi_ab.svc_map)) ||
8118 	    ath12k_acpi_get_disable_11be(ar->ab))
8119 		return;
8120 
8121 	eht_cap->has_eht = true;
8122 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
8123 	       sizeof(eht_cap_elem->mac_cap_info));
8124 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
8125 	       sizeof(eht_cap_elem->phy_cap_info));
8126 
8127 	switch (iftype) {
8128 	case NL80211_IFTYPE_AP:
8129 		eht_cap_elem->phy_cap_info[0] &=
8130 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
8131 		eht_cap_elem->phy_cap_info[4] &=
8132 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
8133 		eht_cap_elem->phy_cap_info[5] &=
8134 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
8135 		break;
8136 	case NL80211_IFTYPE_STATION:
8137 		eht_cap_elem->phy_cap_info[7] &=
8138 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
8139 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
8140 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
8141 		eht_cap_elem->phy_cap_info[7] &=
8142 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
8143 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
8144 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
8145 		break;
8146 	case NL80211_IFTYPE_MESH_POINT:
8147 		ath12k_mac_filter_eht_cap_mesh(eht_cap_elem);
8148 		break;
8149 	default:
8150 		break;
8151 	}
8152 
8153 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
8154 				    he_cap_elem, eht_cap_elem);
8155 
8156 	if (eht_cap_elem->phy_cap_info[5] &
8157 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
8158 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
8159 }
8160 
ath12k_mac_copy_sband_iftype_data(struct ath12k * ar,struct ath12k_pdev_cap * cap,struct ieee80211_sband_iftype_data * data,int band)8161 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
8162 					     struct ath12k_pdev_cap *cap,
8163 					     struct ieee80211_sband_iftype_data *data,
8164 					     int band)
8165 {
8166 	struct ath12k_band_cap *band_cap = &cap->band[band];
8167 	int i, idx = 0;
8168 
8169 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
8170 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
8171 
8172 		switch (i) {
8173 		case NL80211_IFTYPE_STATION:
8174 		case NL80211_IFTYPE_AP:
8175 		case NL80211_IFTYPE_MESH_POINT:
8176 			break;
8177 
8178 		default:
8179 			continue;
8180 		}
8181 
8182 		data[idx].types_mask = BIT(i);
8183 
8184 		ath12k_mac_copy_he_cap(ar, band_cap, i, ar->num_tx_chains, he_cap);
8185 		if (band == NL80211_BAND_6GHZ) {
8186 			data[idx].he_6ghz_capa.capa =
8187 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
8188 		}
8189 		ath12k_mac_copy_eht_cap(ar, band_cap, &he_cap->he_cap_elem, i,
8190 					&data[idx].eht_cap);
8191 		idx++;
8192 	}
8193 
8194 	return idx;
8195 }
8196 
ath12k_mac_setup_sband_iftype_data(struct ath12k * ar,struct ath12k_pdev_cap * cap)8197 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
8198 					       struct ath12k_pdev_cap *cap)
8199 {
8200 	struct ieee80211_supported_band *sband;
8201 	enum nl80211_band band;
8202 	int count;
8203 
8204 	if (cap->supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
8205 		band = NL80211_BAND_2GHZ;
8206 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8207 							  ar->mac.iftype[band],
8208 							  band);
8209 		sband = &ar->mac.sbands[band];
8210 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8211 						 count);
8212 	}
8213 
8214 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
8215 		band = NL80211_BAND_5GHZ;
8216 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8217 							  ar->mac.iftype[band],
8218 							  band);
8219 		sband = &ar->mac.sbands[band];
8220 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8221 						 count);
8222 	}
8223 
8224 	if (cap->supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
8225 	    ar->supports_6ghz) {
8226 		band = NL80211_BAND_6GHZ;
8227 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
8228 							  ar->mac.iftype[band],
8229 							  band);
8230 		sband = &ar->mac.sbands[band];
8231 		_ieee80211_set_sband_iftype_data(sband, ar->mac.iftype[band],
8232 						 count);
8233 	}
8234 }
8235 
__ath12k_set_antenna(struct ath12k * ar,u32 tx_ant,u32 rx_ant)8236 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
8237 {
8238 	struct ath12k_hw *ah = ath12k_ar_to_ah(ar);
8239 	int ret;
8240 
8241 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8242 
8243 	if (ath12k_check_chain_mask(ar, tx_ant, true))
8244 		return -EINVAL;
8245 
8246 	if (ath12k_check_chain_mask(ar, rx_ant, false))
8247 		return -EINVAL;
8248 
8249 	/* Since we advertised the max cap of all radios combined during wiphy
8250 	 * registration, ensure we don't set the antenna config higher than the
8251 	 * limits
8252 	 */
8253 	tx_ant = min_t(u32, tx_ant, ar->pdev->cap.tx_chain_mask);
8254 	rx_ant = min_t(u32, rx_ant, ar->pdev->cap.rx_chain_mask);
8255 
8256 	ar->cfg_tx_chainmask = tx_ant;
8257 	ar->cfg_rx_chainmask = rx_ant;
8258 
8259 	if (ah->state != ATH12K_HW_STATE_ON &&
8260 	    ah->state != ATH12K_HW_STATE_RESTARTED)
8261 		return 0;
8262 
8263 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
8264 					tx_ant, ar->pdev->pdev_id);
8265 	if (ret) {
8266 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
8267 			    ret, tx_ant);
8268 		return ret;
8269 	}
8270 
8271 	ar->num_tx_chains = hweight32(tx_ant);
8272 
8273 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
8274 					rx_ant, ar->pdev->pdev_id);
8275 	if (ret) {
8276 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
8277 			    ret, rx_ant);
8278 		return ret;
8279 	}
8280 
8281 	ar->num_rx_chains = hweight32(rx_ant);
8282 
8283 	/* Reload HT/VHT/HE capability */
8284 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
8285 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
8286 
8287 	return 0;
8288 }
8289 
ath12k_mgmt_over_wmi_tx_drop(struct ath12k * ar,struct sk_buff * skb)8290 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
8291 {
8292 	int num_mgmt;
8293 
8294 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8295 
8296 	ieee80211_free_txskb(ath12k_ar_to_hw(ar), skb);
8297 
8298 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
8299 
8300 	if (num_mgmt < 0)
8301 		WARN_ON_ONCE(1);
8302 
8303 	if (!num_mgmt)
8304 		wake_up(&ar->txmgmt_empty_waitq);
8305 }
8306 
ath12k_mac_tx_mgmt_pending_free(int buf_id,void * skb,void * ctx)8307 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
8308 {
8309 	struct sk_buff *msdu = skb;
8310 	struct ieee80211_tx_info *info;
8311 	struct ath12k *ar = ctx;
8312 	struct ath12k_base *ab = ar->ab;
8313 
8314 	spin_lock_bh(&ar->txmgmt_idr_lock);
8315 	idr_remove(&ar->txmgmt_idr, buf_id);
8316 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8317 	dma_unmap_single(ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
8318 			 DMA_TO_DEVICE);
8319 
8320 	info = IEEE80211_SKB_CB(msdu);
8321 	memset(&info->status, 0, sizeof(info->status));
8322 
8323 	ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8324 
8325 	return 0;
8326 }
8327 
ath12k_mac_vif_txmgmt_idr_remove(int buf_id,void * skb,void * ctx)8328 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
8329 {
8330 	struct ieee80211_vif *vif = ctx;
8331 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
8332 	struct sk_buff *msdu = skb;
8333 	struct ath12k *ar = skb_cb->ar;
8334 	struct ath12k_base *ab = ar->ab;
8335 
8336 	if (skb_cb->vif == vif) {
8337 		spin_lock_bh(&ar->txmgmt_idr_lock);
8338 		idr_remove(&ar->txmgmt_idr, buf_id);
8339 		spin_unlock_bh(&ar->txmgmt_idr_lock);
8340 		dma_unmap_single(ab->dev, skb_cb->paddr, msdu->len,
8341 				 DMA_TO_DEVICE);
8342 	}
8343 
8344 	return 0;
8345 }
8346 
ath12k_mac_mgmt_tx_wmi(struct ath12k * ar,struct ath12k_link_vif * arvif,struct sk_buff * skb)8347 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_link_vif *arvif,
8348 				  struct sk_buff *skb)
8349 {
8350 	struct ath12k_base *ab = ar->ab;
8351 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8352 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
8353 	struct ieee80211_tx_info *info;
8354 	enum hal_encrypt_type enctype;
8355 	unsigned int mic_len;
8356 	dma_addr_t paddr;
8357 	int buf_id;
8358 	int ret;
8359 
8360 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
8361 
8362 	skb_cb->ar = ar;
8363 	spin_lock_bh(&ar->txmgmt_idr_lock);
8364 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
8365 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
8366 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8367 	if (buf_id < 0)
8368 		return -ENOSPC;
8369 
8370 	info = IEEE80211_SKB_CB(skb);
8371 	if ((skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
8372 	    !(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
8373 		if ((ieee80211_is_action(hdr->frame_control) ||
8374 		     ieee80211_is_deauth(hdr->frame_control) ||
8375 		     ieee80211_is_disassoc(hdr->frame_control)) &&
8376 		     ieee80211_has_protected(hdr->frame_control)) {
8377 			enctype = ath12k_dp_tx_get_encrypt_type(skb_cb->cipher);
8378 			mic_len = ath12k_dp_rx_crypto_mic_len(ar, enctype);
8379 			skb_put(skb, mic_len);
8380 		}
8381 	}
8382 
8383 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
8384 	if (dma_mapping_error(ab->dev, paddr)) {
8385 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
8386 		ret = -EIO;
8387 		goto err_free_idr;
8388 	}
8389 
8390 	skb_cb->paddr = paddr;
8391 
8392 	ret = ath12k_wmi_mgmt_send(arvif, buf_id, skb);
8393 	if (ret) {
8394 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
8395 		goto err_unmap_buf;
8396 	}
8397 
8398 	return 0;
8399 
8400 err_unmap_buf:
8401 	dma_unmap_single(ab->dev, skb_cb->paddr,
8402 			 skb->len, DMA_TO_DEVICE);
8403 err_free_idr:
8404 	spin_lock_bh(&ar->txmgmt_idr_lock);
8405 	idr_remove(&ar->txmgmt_idr, buf_id);
8406 	spin_unlock_bh(&ar->txmgmt_idr_lock);
8407 
8408 	return ret;
8409 }
8410 
ath12k_mgmt_over_wmi_tx_purge(struct ath12k * ar)8411 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
8412 {
8413 	struct sk_buff *skb;
8414 
8415 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
8416 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8417 }
8418 
ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif * arvif,struct sk_buff * skb)8419 static int ath12k_mac_mgmt_action_frame_fill_elem_data(struct ath12k_link_vif *arvif,
8420 						       struct sk_buff *skb)
8421 {
8422 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8423 	u8 category, *buf, iv_len, action_code, dialog_token;
8424 	struct ieee80211_bss_conf *link_conf;
8425 	struct ieee80211_chanctx_conf *conf;
8426 	int cur_tx_power, max_tx_power;
8427 	struct ath12k *ar = arvif->ar;
8428 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
8429 	struct wiphy *wiphy = hw->wiphy;
8430 	struct ath12k_skb_cb *skb_cb;
8431 	struct ieee80211_mgmt *mgmt;
8432 	unsigned int remaining_len;
8433 	bool has_protected;
8434 
8435 	lockdep_assert_wiphy(wiphy);
8436 
8437 	/* make sure category field is present */
8438 	if (skb->len < IEEE80211_MIN_ACTION_SIZE)
8439 		return -EINVAL;
8440 
8441 	remaining_len = skb->len - IEEE80211_MIN_ACTION_SIZE;
8442 	has_protected = ieee80211_has_protected(hdr->frame_control);
8443 
8444 	/* In case of SW crypto and hdr protected (PMF), packet will already be encrypted,
8445 	 * we can't put in data in this case
8446 	 */
8447 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
8448 	    has_protected)
8449 		return 0;
8450 
8451 	mgmt = (struct ieee80211_mgmt *)hdr;
8452 	buf = (u8 *)&mgmt->u.action;
8453 
8454 	/* FCTL_PROTECTED frame might have extra space added for HDR_LEN. Offset that
8455 	 * many bytes if it is there
8456 	 */
8457 	if (has_protected) {
8458 		skb_cb = ATH12K_SKB_CB(skb);
8459 
8460 		switch (skb_cb->cipher) {
8461 		/* Cipher suite having flag %IEEE80211_KEY_FLAG_GENERATE_IV_MGMT set in
8462 		 * key needs to be processed. See ath12k_install_key()
8463 		 */
8464 		case WLAN_CIPHER_SUITE_CCMP:
8465 		case WLAN_CIPHER_SUITE_CCMP_256:
8466 		case WLAN_CIPHER_SUITE_GCMP:
8467 		case WLAN_CIPHER_SUITE_GCMP_256:
8468 			iv_len = IEEE80211_CCMP_HDR_LEN;
8469 			break;
8470 		case WLAN_CIPHER_SUITE_TKIP:
8471 			iv_len = 0;
8472 			break;
8473 		default:
8474 			return -EINVAL;
8475 		}
8476 
8477 		if (remaining_len < iv_len)
8478 			return -EINVAL;
8479 
8480 		buf += iv_len;
8481 		remaining_len -= iv_len;
8482 	}
8483 
8484 	category = *buf++;
8485 	/* category code is already taken care in %IEEE80211_MIN_ACTION_SIZE hence
8486 	 * no need to adjust remaining_len
8487 	 */
8488 
8489 	switch (category) {
8490 	case WLAN_CATEGORY_RADIO_MEASUREMENT:
8491 		/* need action code and dialog token */
8492 		if (remaining_len < 2)
8493 			return -EINVAL;
8494 
8495 		/* Packet Format:
8496 		 *	Action Code | Dialog Token | Variable Len (based on Action Code)
8497 		 */
8498 		action_code = *buf++;
8499 		dialog_token = *buf++;
8500 		remaining_len -= 2;
8501 
8502 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
8503 		if (!link_conf) {
8504 			ath12k_warn(ar->ab,
8505 				    "failed to get bss link conf for vdev %d in RM handling\n",
8506 				    arvif->vdev_id);
8507 			return -EINVAL;
8508 		}
8509 
8510 		conf = wiphy_dereference(wiphy, link_conf->chanctx_conf);
8511 		if (!conf)
8512 			return -ENOENT;
8513 
8514 		cur_tx_power = link_conf->txpower;
8515 		max_tx_power = min(conf->def.chan->max_reg_power,
8516 				   (int)ar->max_tx_power / 2);
8517 
8518 		ath12k_mac_op_get_txpower(hw, arvif->ahvif->vif, arvif->link_id,
8519 					  &cur_tx_power);
8520 
8521 		switch (action_code) {
8522 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST:
8523 			/* need variable fields to be present in len */
8524 			if (remaining_len < 2)
8525 				return -EINVAL;
8526 
8527 			/* Variable length format as defined in IEEE 802.11-2024,
8528 			 * Figure 9-1187-Link Measurement Request frame Action field
8529 			 * format.
8530 			 *	Transmit Power | Max Tx Power
8531 			 * We fill both of these.
8532 			 */
8533 			*buf++ = cur_tx_power;
8534 			*buf = max_tx_power;
8535 
8536 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8537 				   "RRM: Link Measurement Req dialog_token %u cur_tx_power %d max_tx_power %d\n",
8538 				   dialog_token, cur_tx_power, max_tx_power);
8539 			break;
8540 		case WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT:
8541 			/* need variable fields to be present in len */
8542 			if (remaining_len < 3)
8543 				return -EINVAL;
8544 
8545 			/* Variable length format as defined in IEEE 802.11-2024,
8546 			 * Figure 9-1188-Link Measurement Report frame Action field format
8547 			 *	TPC Report | Variable Fields
8548 			 *
8549 			 * TPC Report Format:
8550 			 *	Element ID | Len | Tx Power | Link Margin
8551 			 *
8552 			 * We fill Tx power in the TPC Report (2nd index)
8553 			 */
8554 			buf[2] = cur_tx_power;
8555 
8556 			/* TODO: At present, Link margin data is not present so can't
8557 			 * really fill it now. Once it is available, it can be added
8558 			 * here
8559 			 */
8560 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8561 				   "RRM: Link Measurement Report dialog_token %u cur_tx_power %d\n",
8562 				   dialog_token, cur_tx_power);
8563 			break;
8564 		default:
8565 			return -EINVAL;
8566 		}
8567 		break;
8568 	default:
8569 		/* nothing to fill */
8570 		return 0;
8571 	}
8572 
8573 	return 0;
8574 }
8575 
ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif * arvif,struct sk_buff * skb)8576 static int ath12k_mac_mgmt_frame_fill_elem_data(struct ath12k_link_vif *arvif,
8577 						struct sk_buff *skb)
8578 {
8579 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8580 
8581 	if (!ieee80211_is_action(hdr->frame_control))
8582 		return 0;
8583 
8584 	return ath12k_mac_mgmt_action_frame_fill_elem_data(arvif, skb);
8585 }
8586 
ath12k_mgmt_over_wmi_tx_work(struct wiphy * wiphy,struct wiphy_work * work)8587 static void ath12k_mgmt_over_wmi_tx_work(struct wiphy *wiphy, struct wiphy_work *work)
8588 {
8589 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
8590 	struct ath12k_hw *ah = ar->ah;
8591 	struct ath12k_skb_cb *skb_cb;
8592 	struct ath12k_vif *ahvif;
8593 	struct ath12k_link_vif *arvif;
8594 	struct sk_buff *skb;
8595 	int ret;
8596 
8597 	lockdep_assert_wiphy(wiphy);
8598 
8599 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
8600 		skb_cb = ATH12K_SKB_CB(skb);
8601 		if (!skb_cb->vif) {
8602 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
8603 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8604 			continue;
8605 		}
8606 
8607 		ahvif = ath12k_vif_to_ahvif(skb_cb->vif);
8608 		if (!(ahvif->links_map & BIT(skb_cb->link_id))) {
8609 			ath12k_warn(ar->ab,
8610 				    "invalid linkid %u in mgmt over wmi tx with linkmap 0x%x\n",
8611 				    skb_cb->link_id, ahvif->links_map);
8612 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8613 			continue;
8614 		}
8615 
8616 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[skb_cb->link_id]);
8617 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id)) {
8618 			/* Fill in the data which is required to be filled by the driver
8619 			 * For example: Max Tx power in Link Measurement Request/Report
8620 			 */
8621 			ret = ath12k_mac_mgmt_frame_fill_elem_data(arvif, skb);
8622 			if (ret) {
8623 				/* If we couldn't fill the data due to any reason,
8624 				 * let's not discard transmitting the packet.
8625 				 * For example: Software crypto and PMF case
8626 				 */
8627 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8628 					   "Failed to fill the required data for the mgmt packet err %d\n",
8629 					   ret);
8630 			}
8631 
8632 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
8633 			if (ret) {
8634 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
8635 					    arvif->vdev_id, ret);
8636 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8637 			}
8638 		} else {
8639 			ath12k_warn(ar->ab,
8640 				    "dropping mgmt frame for vdev %d link %u is_started %d\n",
8641 				    arvif->vdev_id,
8642 				    skb_cb->link_id,
8643 				    arvif->is_started);
8644 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
8645 		}
8646 	}
8647 }
8648 
ath12k_mac_mgmt_tx(struct ath12k * ar,struct sk_buff * skb,bool is_prb_rsp)8649 static int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
8650 			      bool is_prb_rsp)
8651 {
8652 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
8653 
8654 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
8655 		return -ESHUTDOWN;
8656 
8657 	/* Drop probe response packets when the pending management tx
8658 	 * count has reached a certain threshold, so as to prioritize
8659 	 * other mgmt packets like auth and assoc to be sent on time
8660 	 * for establishing successful connections.
8661 	 */
8662 	if (is_prb_rsp &&
8663 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
8664 		ath12k_warn(ar->ab,
8665 			    "dropping probe response as pending queue is almost full\n");
8666 		return -ENOSPC;
8667 	}
8668 
8669 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
8670 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
8671 		return -ENOSPC;
8672 	}
8673 
8674 	skb_queue_tail(q, skb);
8675 	atomic_inc(&ar->num_pending_mgmt_tx);
8676 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
8677 
8678 	return 0;
8679 }
8680 
ath12k_mac_add_p2p_noa_ie(struct ath12k * ar,struct ieee80211_vif * vif,struct sk_buff * skb,bool is_prb_rsp)8681 static void ath12k_mac_add_p2p_noa_ie(struct ath12k *ar,
8682 				      struct ieee80211_vif *vif,
8683 				      struct sk_buff *skb,
8684 				      bool is_prb_rsp)
8685 {
8686 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8687 
8688 	if (likely(!is_prb_rsp))
8689 		return;
8690 
8691 	spin_lock_bh(&ar->data_lock);
8692 
8693 	if (ahvif->u.ap.noa_data &&
8694 	    !pskb_expand_head(skb, 0, ahvif->u.ap.noa_len,
8695 			      GFP_ATOMIC))
8696 		skb_put_data(skb, ahvif->u.ap.noa_data,
8697 			     ahvif->u.ap.noa_len);
8698 
8699 	spin_unlock_bh(&ar->data_lock);
8700 }
8701 
8702 /* 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)8703 static void ath12k_mlo_mcast_update_tx_link_address(struct ieee80211_vif *vif,
8704 						    u8 link_id, struct sk_buff *skb,
8705 						    u32 info_flags)
8706 {
8707 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8708 	struct ieee80211_bss_conf *bss_conf;
8709 
8710 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
8711 		return;
8712 
8713 	bss_conf = rcu_dereference(vif->link_conf[link_id]);
8714 	if (bss_conf)
8715 		ether_addr_copy(hdr->addr2, bss_conf->addr);
8716 }
8717 
8718 /* 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)8719 static u8 ath12k_mac_get_tx_link(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
8720 				 u8 link, struct sk_buff *skb, u32 info_flags)
8721 {
8722 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8723 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8724 	struct ieee80211_link_sta *link_sta;
8725 	struct ieee80211_bss_conf *bss_conf;
8726 	struct ath12k_sta *ahsta;
8727 
8728 	/* Use the link id passed or the default vif link */
8729 	if (!sta) {
8730 		if (link != IEEE80211_LINK_UNSPECIFIED)
8731 			return link;
8732 
8733 		return ahvif->deflink.link_id;
8734 	}
8735 
8736 	ahsta = ath12k_sta_to_ahsta(sta);
8737 
8738 	/* Below translation ensures we pass proper A2 & A3 for non ML clients.
8739 	 * Also it assumes for now support only for MLO AP in this path
8740 	 */
8741 	if (!sta->mlo) {
8742 		link = ahsta->deflink.link_id;
8743 
8744 		if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
8745 			return link;
8746 
8747 		bss_conf = rcu_dereference(vif->link_conf[link]);
8748 		if (bss_conf) {
8749 			ether_addr_copy(hdr->addr2, bss_conf->addr);
8750 			if (!ieee80211_has_tods(hdr->frame_control) &&
8751 			    !ieee80211_has_fromds(hdr->frame_control))
8752 				ether_addr_copy(hdr->addr3, bss_conf->addr);
8753 		}
8754 
8755 		return link;
8756 	}
8757 
8758 	/* enqueue eth enacap & data frames on primary link, FW does link
8759 	 * selection and address translation.
8760 	 */
8761 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP ||
8762 	    ieee80211_is_data(hdr->frame_control))
8763 		return ahsta->assoc_link_id;
8764 
8765 	/* 802.11 frame cases */
8766 	if (link == IEEE80211_LINK_UNSPECIFIED)
8767 		link = ahsta->deflink.link_id;
8768 
8769 	if (!ieee80211_is_mgmt(hdr->frame_control))
8770 		return link;
8771 
8772 	/* Perform address conversion for ML STA Tx */
8773 	bss_conf = rcu_dereference(vif->link_conf[link]);
8774 	link_sta = rcu_dereference(sta->link[link]);
8775 
8776 	if (bss_conf && link_sta) {
8777 		ether_addr_copy(hdr->addr1, link_sta->addr);
8778 		ether_addr_copy(hdr->addr2, bss_conf->addr);
8779 
8780 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
8781 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
8782 		else if (vif->type == NL80211_IFTYPE_AP)
8783 			ether_addr_copy(hdr->addr3, bss_conf->addr);
8784 
8785 		return link;
8786 	}
8787 
8788 	if (bss_conf) {
8789 		/* In certain cases where a ML sta associated and added subset of
8790 		 * links on which the ML AP is active, but now sends some frame
8791 		 * (ex. Probe request) on a different link which is active in our
8792 		 * MLD but was not added during previous association, we can
8793 		 * still honor the Tx to that ML STA via the requested link.
8794 		 * The control would reach here in such case only when that link
8795 		 * address is same as the MLD address or in worst case clients
8796 		 * used MLD address at TA wrongly which would have helped
8797 		 * identify the ML sta object and pass it here.
8798 		 * If the link address of that STA is different from MLD address,
8799 		 * then the sta object would be NULL and control won't reach
8800 		 * here but return at the start of the function itself with !sta
8801 		 * check. Also this would not need any translation at hdr->addr1
8802 		 * from MLD to link address since the RA is the MLD address
8803 		 * (same as that link address ideally) already.
8804 		 */
8805 		ether_addr_copy(hdr->addr2, bss_conf->addr);
8806 
8807 		if (vif->type == NL80211_IFTYPE_STATION && bss_conf->bssid)
8808 			ether_addr_copy(hdr->addr3, bss_conf->bssid);
8809 		else if (vif->type == NL80211_IFTYPE_AP)
8810 			ether_addr_copy(hdr->addr3, bss_conf->addr);
8811 	}
8812 
8813 	return link;
8814 }
8815 
8816 /* Note: called under rcu_read_lock() */
ath12k_mac_op_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)8817 static void ath12k_mac_op_tx(struct ieee80211_hw *hw,
8818 			     struct ieee80211_tx_control *control,
8819 			     struct sk_buff *skb)
8820 {
8821 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
8822 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
8823 	struct ieee80211_vif *vif = info->control.vif;
8824 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
8825 	struct ath12k_link_vif *arvif = &ahvif->deflink;
8826 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
8827 	struct ieee80211_key_conf *key = info->control.hw_key;
8828 	struct ieee80211_sta *sta = control->sta;
8829 	struct ath12k_link_vif *tmp_arvif;
8830 	u32 info_flags = info->flags;
8831 	struct sk_buff *msdu_copied;
8832 	struct ath12k *ar, *tmp_ar;
8833 	struct ath12k_peer *peer;
8834 	unsigned long links_map;
8835 	bool is_mcast = false;
8836 	bool is_dvlan = false;
8837 	struct ethhdr *eth;
8838 	bool is_prb_rsp;
8839 	u16 mcbc_gsn;
8840 	u8 link_id;
8841 	int ret;
8842 
8843 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
8844 		ieee80211_free_txskb(hw, skb);
8845 		return;
8846 	}
8847 
8848 	link_id = u32_get_bits(info->control.flags, IEEE80211_TX_CTRL_MLO_LINK);
8849 	memset(skb_cb, 0, sizeof(*skb_cb));
8850 	skb_cb->vif = vif;
8851 
8852 	if (key) {
8853 		skb_cb->cipher = key->cipher;
8854 		skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
8855 	}
8856 
8857 	/* handle only for MLO case, use deflink for non MLO case */
8858 	if (ieee80211_vif_is_mld(vif)) {
8859 		link_id = ath12k_mac_get_tx_link(sta, vif, link_id, skb, info_flags);
8860 		if (link_id >= IEEE80211_MLD_MAX_NUM_LINKS) {
8861 			ieee80211_free_txskb(hw, skb);
8862 			return;
8863 		}
8864 	} else {
8865 		link_id = 0;
8866 	}
8867 
8868 	arvif = rcu_dereference(ahvif->link[link_id]);
8869 	if (!arvif || !arvif->ar) {
8870 		ath12k_warn(ahvif->ah, "failed to find arvif link id %u for frame transmission",
8871 			    link_id);
8872 		ieee80211_free_txskb(hw, skb);
8873 		return;
8874 	}
8875 
8876 	ar = arvif->ar;
8877 	skb_cb->link_id = link_id;
8878 	is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
8879 
8880 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
8881 		eth = (struct ethhdr *)skb->data;
8882 		is_mcast = is_multicast_ether_addr(eth->h_dest);
8883 
8884 		skb_cb->flags |= ATH12K_SKB_HW_80211_ENCAP;
8885 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
8886 		if (sta && sta->mlo)
8887 			skb_cb->flags |= ATH12K_SKB_MLO_STA;
8888 
8889 		ret = ath12k_mac_mgmt_tx(ar, skb, is_prb_rsp);
8890 		if (ret) {
8891 			ath12k_warn(ar->ab, "failed to queue management frame %d\n",
8892 				    ret);
8893 			ieee80211_free_txskb(hw, skb);
8894 		}
8895 		return;
8896 	}
8897 
8898 	if (!(info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP))
8899 		is_mcast = is_multicast_ether_addr(hdr->addr1);
8900 
8901 	/* This is case only for P2P_GO */
8902 	if (vif->type == NL80211_IFTYPE_AP && vif->p2p)
8903 		ath12k_mac_add_p2p_noa_ie(ar, vif, skb, is_prb_rsp);
8904 
8905 	/* Checking if it is a DVLAN frame */
8906 	if (!test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags) &&
8907 	    !(skb_cb->flags & ATH12K_SKB_HW_80211_ENCAP) &&
8908 	    !(skb_cb->flags & ATH12K_SKB_CIPHER_SET) &&
8909 	    ieee80211_has_protected(hdr->frame_control))
8910 		is_dvlan = true;
8911 
8912 	if (!vif->valid_links || !is_mcast || is_dvlan ||
8913 	    (skb_cb->flags & ATH12K_SKB_HW_80211_ENCAP) ||
8914 	    test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags)) {
8915 		ret = ath12k_dp_tx(ar, arvif, skb, false, 0, is_mcast);
8916 		if (unlikely(ret)) {
8917 			ath12k_warn(ar->ab, "failed to transmit frame %d\n", ret);
8918 			ieee80211_free_txskb(ar->ah->hw, skb);
8919 			return;
8920 		}
8921 	} else {
8922 		mcbc_gsn = atomic_inc_return(&ahvif->mcbc_gsn) & 0xfff;
8923 
8924 		links_map = ahvif->links_map;
8925 		for_each_set_bit(link_id, &links_map,
8926 				 IEEE80211_MLD_MAX_NUM_LINKS) {
8927 			tmp_arvif = rcu_dereference(ahvif->link[link_id]);
8928 			if (!tmp_arvif || !tmp_arvif->is_up)
8929 				continue;
8930 
8931 			tmp_ar = tmp_arvif->ar;
8932 			msdu_copied = skb_copy(skb, GFP_ATOMIC);
8933 			if (!msdu_copied) {
8934 				ath12k_err(ar->ab,
8935 					   "skb copy failure link_id 0x%X vdevid 0x%X\n",
8936 					   link_id, tmp_arvif->vdev_id);
8937 				continue;
8938 			}
8939 
8940 			ath12k_mlo_mcast_update_tx_link_address(vif, link_id,
8941 								msdu_copied,
8942 								info_flags);
8943 
8944 			skb_cb = ATH12K_SKB_CB(msdu_copied);
8945 			skb_cb->link_id = link_id;
8946 
8947 			/* For open mode, skip peer find logic */
8948 			if (unlikely(!ahvif->key_cipher))
8949 				goto skip_peer_find;
8950 
8951 			spin_lock_bh(&tmp_ar->ab->base_lock);
8952 			peer = ath12k_peer_find_by_addr(tmp_ar->ab, tmp_arvif->bssid);
8953 			if (!peer) {
8954 				spin_unlock_bh(&tmp_ar->ab->base_lock);
8955 				ath12k_warn(tmp_ar->ab,
8956 					    "failed to find peer for vdev_id 0x%X addr %pM link_map 0x%X\n",
8957 					    tmp_arvif->vdev_id, tmp_arvif->bssid,
8958 					    ahvif->links_map);
8959 				dev_kfree_skb_any(msdu_copied);
8960 				continue;
8961 			}
8962 
8963 			key = peer->keys[peer->mcast_keyidx];
8964 			if (key) {
8965 				skb_cb->cipher = key->cipher;
8966 				skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
8967 
8968 				hdr = (struct ieee80211_hdr *)msdu_copied->data;
8969 				if (!ieee80211_has_protected(hdr->frame_control))
8970 					hdr->frame_control |=
8971 						cpu_to_le16(IEEE80211_FCTL_PROTECTED);
8972 			}
8973 			spin_unlock_bh(&tmp_ar->ab->base_lock);
8974 
8975 skip_peer_find:
8976 			ret = ath12k_dp_tx(tmp_ar, tmp_arvif,
8977 					   msdu_copied, true, mcbc_gsn, is_mcast);
8978 			if (unlikely(ret)) {
8979 				if (ret == -ENOMEM) {
8980 					/* Drops are expected during heavy multicast
8981 					 * frame flood. Print with debug log
8982 					 * level to avoid lot of console prints
8983 					 */
8984 					ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
8985 						   "failed to transmit frame %d\n",
8986 						   ret);
8987 				} else {
8988 					ath12k_warn(ar->ab,
8989 						    "failed to transmit frame %d\n",
8990 						    ret);
8991 				}
8992 
8993 				dev_kfree_skb_any(msdu_copied);
8994 			}
8995 		}
8996 		ieee80211_free_txskb(ar->ah->hw, skb);
8997 	}
8998 }
8999 
ath12k_mac_drain_tx(struct ath12k * ar)9000 void ath12k_mac_drain_tx(struct ath12k *ar)
9001 {
9002 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9003 
9004 	/* make sure rcu-protected mac80211 tx path itself is drained */
9005 	synchronize_net();
9006 
9007 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->wmi_mgmt_tx_work);
9008 	ath12k_mgmt_over_wmi_tx_purge(ar);
9009 }
9010 
ath12k_mac_config_mon_status_default(struct ath12k * ar,bool enable)9011 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
9012 {
9013 	struct htt_rx_ring_tlv_filter tlv_filter = {};
9014 	struct ath12k_base *ab = ar->ab;
9015 	u32 ring_id, i;
9016 	int ret = 0;
9017 
9018 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9019 
9020 	if (!ab->hw_params->rxdma1_enable)
9021 		return ret;
9022 
9023 	if (enable) {
9024 		tlv_filter = ath12k_mac_mon_status_filter_default;
9025 
9026 		if (ath12k_debugfs_rx_filter(ar))
9027 			tlv_filter.rx_filter = ath12k_debugfs_rx_filter(ar);
9028 	} else {
9029 		tlv_filter.rxmon_disable = true;
9030 	}
9031 
9032 	for (i = 0; i < ab->hw_params->num_rxdma_per_pdev; i++) {
9033 		ring_id = ar->dp.rxdma_mon_dst_ring[i].ring_id;
9034 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id,
9035 						       ar->dp.mac_id + i,
9036 						       HAL_RXDMA_MONITOR_DST,
9037 						       DP_RXDMA_REFILL_RING_SIZE,
9038 						       &tlv_filter);
9039 		if (ret) {
9040 			ath12k_err(ab,
9041 				   "failed to setup filter for monitor buf %d\n",
9042 				   ret);
9043 		}
9044 	}
9045 
9046 	return ret;
9047 }
9048 
ath12k_mac_start(struct ath12k * ar)9049 static int ath12k_mac_start(struct ath12k *ar)
9050 {
9051 	struct ath12k_hw *ah = ar->ah;
9052 	struct ath12k_base *ab = ar->ab;
9053 	struct ath12k_pdev *pdev = ar->pdev;
9054 	int ret;
9055 
9056 	lockdep_assert_held(&ah->hw_mutex);
9057 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9058 
9059 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
9060 					1, pdev->pdev_id);
9061 
9062 	if (ret) {
9063 		ath12k_err(ab, "failed to enable PMF QOS: %d\n", ret);
9064 		goto err;
9065 	}
9066 
9067 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
9068 					pdev->pdev_id);
9069 	if (ret) {
9070 		ath12k_err(ab, "failed to enable dynamic bw: %d\n", ret);
9071 		goto err;
9072 	}
9073 
9074 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
9075 					0, pdev->pdev_id);
9076 	if (ret) {
9077 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
9078 			   ret);
9079 		goto err;
9080 	}
9081 
9082 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
9083 	if (ret) {
9084 		ath12k_err(ab, "failed to offload radar detection: %d\n",
9085 			   ret);
9086 		goto err;
9087 	}
9088 
9089 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
9090 						  HTT_PPDU_STATS_TAG_DEFAULT);
9091 	if (ret) {
9092 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
9093 		goto err;
9094 	}
9095 
9096 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
9097 					1, pdev->pdev_id);
9098 
9099 	if (ret) {
9100 		ath12k_err(ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
9101 		goto err;
9102 	}
9103 
9104 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
9105 
9106 	/* TODO: Do we need to enable ANI? */
9107 
9108 	ret = ath12k_reg_update_chan_list(ar, false);
9109 
9110 	/* The ar state alone can be turned off for non supported country
9111 	 * without returning the error value. As we need to update the channel
9112 	 * for the next ar.
9113 	 */
9114 	if (ret) {
9115 		if (ret == -EINVAL)
9116 			ret = 0;
9117 		goto err;
9118 	}
9119 
9120 	ar->num_started_vdevs = 0;
9121 	ar->num_created_vdevs = 0;
9122 	ar->num_peers = 0;
9123 	ar->allocated_vdev_map = 0;
9124 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
9125 
9126 	/* Configure monitor status ring with default rx_filter to get rx status
9127 	 * such as rssi, rx_duration.
9128 	 */
9129 	ret = ath12k_mac_config_mon_status_default(ar, true);
9130 	if (ret && (ret != -EOPNOTSUPP)) {
9131 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
9132 			   ret);
9133 		goto err;
9134 	}
9135 
9136 	if (ret == -EOPNOTSUPP)
9137 		ath12k_dbg(ab, ATH12K_DBG_MAC,
9138 			   "monitor status config is not yet supported");
9139 
9140 	/* Configure the hash seed for hash based reo dest ring selection */
9141 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
9142 
9143 	/* allow device to enter IMPS */
9144 	if (ab->hw_params->idle_ps) {
9145 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
9146 						1, pdev->pdev_id);
9147 		if (ret) {
9148 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
9149 			goto err;
9150 		}
9151 	}
9152 
9153 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
9154 			   &ab->pdevs[ar->pdev_idx]);
9155 
9156 	return 0;
9157 err:
9158 
9159 	return ret;
9160 }
9161 
ath12k_drain_tx(struct ath12k_hw * ah)9162 static void ath12k_drain_tx(struct ath12k_hw *ah)
9163 {
9164 	struct ath12k *ar;
9165 	int i;
9166 
9167 	lockdep_assert_wiphy(ah->hw->wiphy);
9168 
9169 	for_each_ar(ah, ar, i)
9170 		ath12k_mac_drain_tx(ar);
9171 }
9172 
ath12k_mac_op_start(struct ieee80211_hw * hw)9173 static int ath12k_mac_op_start(struct ieee80211_hw *hw)
9174 {
9175 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9176 	struct ath12k *ar;
9177 	int ret, i;
9178 
9179 	if (ath12k_ftm_mode)
9180 		return -EPERM;
9181 
9182 	lockdep_assert_wiphy(hw->wiphy);
9183 
9184 	ath12k_drain_tx(ah);
9185 
9186 	guard(mutex)(&ah->hw_mutex);
9187 
9188 	switch (ah->state) {
9189 	case ATH12K_HW_STATE_OFF:
9190 		ah->state = ATH12K_HW_STATE_ON;
9191 		break;
9192 	case ATH12K_HW_STATE_RESTARTING:
9193 		ah->state = ATH12K_HW_STATE_RESTARTED;
9194 		break;
9195 	case ATH12K_HW_STATE_RESTARTED:
9196 	case ATH12K_HW_STATE_WEDGED:
9197 	case ATH12K_HW_STATE_ON:
9198 	case ATH12K_HW_STATE_TM:
9199 		ah->state = ATH12K_HW_STATE_OFF;
9200 
9201 		WARN_ON(1);
9202 		return -EINVAL;
9203 	}
9204 
9205 	for_each_ar(ah, ar, i) {
9206 		ret = ath12k_mac_start(ar);
9207 		if (ret) {
9208 			ah->state = ATH12K_HW_STATE_OFF;
9209 
9210 			ath12k_err(ar->ab, "fail to start mac operations in pdev idx %d ret %d\n",
9211 				   ar->pdev_idx, ret);
9212 			goto fail_start;
9213 		}
9214 	}
9215 
9216 	return 0;
9217 
9218 fail_start:
9219 	for (; i > 0; i--) {
9220 		ar = ath12k_ah_to_ar(ah, i - 1);
9221 		ath12k_mac_stop(ar);
9222 	}
9223 
9224 	return ret;
9225 }
9226 
ath12k_mac_rfkill_config(struct ath12k * ar)9227 int ath12k_mac_rfkill_config(struct ath12k *ar)
9228 {
9229 	struct ath12k_base *ab = ar->ab;
9230 	u32 param;
9231 	int ret;
9232 
9233 	if (ab->hw_params->rfkill_pin == 0)
9234 		return -EOPNOTSUPP;
9235 
9236 	ath12k_dbg(ab, ATH12K_DBG_MAC,
9237 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
9238 		   ab->hw_params->rfkill_pin, ab->hw_params->rfkill_cfg,
9239 		   ab->hw_params->rfkill_on_level);
9240 
9241 	param = u32_encode_bits(ab->hw_params->rfkill_on_level,
9242 				WMI_RFKILL_CFG_RADIO_LEVEL) |
9243 		u32_encode_bits(ab->hw_params->rfkill_pin,
9244 				WMI_RFKILL_CFG_GPIO_PIN_NUM) |
9245 		u32_encode_bits(ab->hw_params->rfkill_cfg,
9246 				WMI_RFKILL_CFG_PIN_AS_GPIO);
9247 
9248 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_HW_RFKILL_CONFIG,
9249 					param, ar->pdev->pdev_id);
9250 	if (ret) {
9251 		ath12k_warn(ab,
9252 			    "failed to set rfkill config 0x%x: %d\n",
9253 			    param, ret);
9254 		return ret;
9255 	}
9256 
9257 	return 0;
9258 }
9259 
ath12k_mac_rfkill_enable_radio(struct ath12k * ar,bool enable)9260 int ath12k_mac_rfkill_enable_radio(struct ath12k *ar, bool enable)
9261 {
9262 	enum wmi_rfkill_enable_radio param;
9263 	int ret;
9264 
9265 	if (enable)
9266 		param = WMI_RFKILL_ENABLE_RADIO_ON;
9267 	else
9268 		param = WMI_RFKILL_ENABLE_RADIO_OFF;
9269 
9270 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac %d rfkill enable %d",
9271 		   ar->pdev_idx, param);
9272 
9273 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RFKILL_ENABLE,
9274 					param, ar->pdev->pdev_id);
9275 	if (ret) {
9276 		ath12k_warn(ar->ab, "failed to set rfkill enable param %d: %d\n",
9277 			    param, ret);
9278 		return ret;
9279 	}
9280 
9281 	return 0;
9282 }
9283 
ath12k_mac_stop(struct ath12k * ar)9284 static void ath12k_mac_stop(struct ath12k *ar)
9285 {
9286 	struct ath12k_hw *ah = ar->ah;
9287 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
9288 	struct ath12k_wmi_scan_chan_list_arg *arg;
9289 	int ret;
9290 
9291 	lockdep_assert_held(&ah->hw_mutex);
9292 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9293 
9294 	ret = ath12k_mac_config_mon_status_default(ar, false);
9295 	if (ret && (ret != -EOPNOTSUPP))
9296 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
9297 			   ret);
9298 
9299 	clear_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
9300 
9301 	cancel_delayed_work_sync(&ar->scan.timeout);
9302 	wiphy_work_cancel(ath12k_ar_to_hw(ar)->wiphy, &ar->scan.vdev_clean_wk);
9303 	cancel_work_sync(&ar->regd_channel_update_work);
9304 	cancel_work_sync(&ar->regd_update_work);
9305 	cancel_work_sync(&ar->ab->rfkill_work);
9306 	cancel_work_sync(&ar->ab->update_11d_work);
9307 	ar->state_11d = ATH12K_11D_IDLE;
9308 	complete(&ar->completed_11d_scan);
9309 
9310 	spin_lock_bh(&ar->data_lock);
9311 
9312 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
9313 		list_del(&ppdu_stats->list);
9314 		kfree(ppdu_stats);
9315 	}
9316 
9317 	while ((arg = list_first_entry_or_null(&ar->regd_channel_update_queue,
9318 					       struct ath12k_wmi_scan_chan_list_arg,
9319 					       list))) {
9320 		list_del(&arg->list);
9321 		kfree(arg);
9322 	}
9323 	spin_unlock_bh(&ar->data_lock);
9324 
9325 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
9326 
9327 	synchronize_rcu();
9328 
9329 	atomic_set(&ar->num_pending_mgmt_tx, 0);
9330 }
9331 
ath12k_mac_op_stop(struct ieee80211_hw * hw,bool suspend)9332 static void ath12k_mac_op_stop(struct ieee80211_hw *hw, bool suspend)
9333 {
9334 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
9335 	struct ath12k *ar;
9336 	int i;
9337 
9338 	lockdep_assert_wiphy(hw->wiphy);
9339 
9340 	ath12k_drain_tx(ah);
9341 
9342 	mutex_lock(&ah->hw_mutex);
9343 
9344 	ah->state = ATH12K_HW_STATE_OFF;
9345 
9346 	for_each_ar(ah, ar, i)
9347 		ath12k_mac_stop(ar);
9348 
9349 	mutex_unlock(&ah->hw_mutex);
9350 }
9351 
9352 static u8
ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif * arvif)9353 ath12k_mac_get_vdev_stats_id(struct ath12k_link_vif *arvif)
9354 {
9355 	struct ath12k_base *ab = arvif->ar->ab;
9356 	u8 vdev_stats_id = 0;
9357 
9358 	do {
9359 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
9360 			vdev_stats_id++;
9361 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
9362 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
9363 				break;
9364 			}
9365 		} else {
9366 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
9367 			break;
9368 		}
9369 	} while (vdev_stats_id);
9370 
9371 	arvif->vdev_stats_id = vdev_stats_id;
9372 	return vdev_stats_id;
9373 }
9374 
ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif * arvif,u32 * flags,u32 * tx_vdev_id)9375 static int ath12k_mac_setup_vdev_params_mbssid(struct ath12k_link_vif *arvif,
9376 					       u32 *flags, u32 *tx_vdev_id)
9377 {
9378 	struct ath12k_vif *ahvif = arvif->ahvif;
9379 	struct ieee80211_bss_conf *link_conf;
9380 	struct ath12k *ar = arvif->ar;
9381 	struct ath12k_link_vif *tx_arvif;
9382 
9383 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
9384 	if (!link_conf) {
9385 		ath12k_warn(ar->ab, "unable to access bss link conf in set mbssid params for vif %pM link %u\n",
9386 			    ahvif->vif->addr, arvif->link_id);
9387 		return -ENOLINK;
9388 	}
9389 
9390 	tx_arvif = ath12k_mac_get_tx_arvif(arvif, link_conf);
9391 	if (!tx_arvif)
9392 		return 0;
9393 
9394 	if (link_conf->nontransmitted) {
9395 		if (ath12k_ar_to_hw(ar)->wiphy !=
9396 		    ath12k_ar_to_hw(tx_arvif->ar)->wiphy)
9397 			return -EINVAL;
9398 
9399 		*flags = WMI_VDEV_MBSSID_FLAGS_NON_TRANSMIT_AP;
9400 		*tx_vdev_id = tx_arvif->vdev_id;
9401 	} else if (tx_arvif == arvif) {
9402 		*flags = WMI_VDEV_MBSSID_FLAGS_TRANSMIT_AP;
9403 	} else {
9404 		return -EINVAL;
9405 	}
9406 
9407 	if (link_conf->ema_ap)
9408 		*flags |= WMI_VDEV_MBSSID_FLAGS_EMA_MODE;
9409 
9410 	return 0;
9411 }
9412 
ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif * arvif,struct ath12k_wmi_vdev_create_arg * arg)9413 static int ath12k_mac_setup_vdev_create_arg(struct ath12k_link_vif *arvif,
9414 					    struct ath12k_wmi_vdev_create_arg *arg)
9415 {
9416 	struct ath12k *ar = arvif->ar;
9417 	struct ath12k_pdev *pdev = ar->pdev;
9418 	struct ath12k_vif *ahvif = arvif->ahvif;
9419 	int ret;
9420 
9421 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9422 
9423 	arg->if_id = arvif->vdev_id;
9424 	arg->type = ahvif->vdev_type;
9425 	arg->subtype = ahvif->vdev_subtype;
9426 	arg->pdev_id = pdev->pdev_id;
9427 
9428 	arg->mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
9429 	arg->mbssid_tx_vdev_id = 0;
9430 	if (!test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
9431 		      ar->ab->wmi_ab.svc_map)) {
9432 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
9433 							  &arg->mbssid_flags,
9434 							  &arg->mbssid_tx_vdev_id);
9435 		if (ret)
9436 			return ret;
9437 	}
9438 
9439 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
9440 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
9441 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
9442 	}
9443 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
9444 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
9445 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
9446 	}
9447 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5GHZ_CAP &&
9448 	    ar->supports_6ghz) {
9449 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
9450 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
9451 	}
9452 
9453 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
9454 
9455 	if (ath12k_mac_is_ml_arvif(arvif)) {
9456 		if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS) {
9457 			ath12k_warn(ar->ab, "too many MLO links during setting up vdev: %d",
9458 				    ahvif->vif->valid_links);
9459 			return -EINVAL;
9460 		}
9461 
9462 		ether_addr_copy(arg->mld_addr, ahvif->vif->addr);
9463 	}
9464 
9465 	return 0;
9466 }
9467 
ath12k_mac_update_vif_offload(struct ath12k_link_vif * arvif)9468 static void ath12k_mac_update_vif_offload(struct ath12k_link_vif *arvif)
9469 {
9470 	struct ath12k_vif *ahvif = arvif->ahvif;
9471 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
9472 	struct ath12k *ar = arvif->ar;
9473 	struct ath12k_base *ab = ar->ab;
9474 	u32 param_id, param_value;
9475 	int ret;
9476 
9477 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
9478 	if (vif->type != NL80211_IFTYPE_STATION &&
9479 	    vif->type != NL80211_IFTYPE_AP)
9480 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
9481 					IEEE80211_OFFLOAD_DECAP_ENABLED);
9482 
9483 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
9484 		ahvif->tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
9485 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
9486 		ahvif->tx_encap_type = ATH12K_HW_TXRX_RAW;
9487 	else
9488 		ahvif->tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
9489 
9490 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9491 					    param_id, ahvif->tx_encap_type);
9492 	if (ret) {
9493 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
9494 			    arvif->vdev_id, ret);
9495 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
9496 	}
9497 
9498 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
9499 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
9500 		param_value = ATH12K_HW_TXRX_ETHERNET;
9501 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
9502 		param_value = ATH12K_HW_TXRX_RAW;
9503 	else
9504 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
9505 
9506 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9507 					    param_id, param_value);
9508 	if (ret) {
9509 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
9510 			    arvif->vdev_id, ret);
9511 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
9512 	}
9513 }
9514 
ath12k_mac_op_update_vif_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif)9515 static void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
9516 					     struct ieee80211_vif *vif)
9517 {
9518 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
9519 	struct ath12k_link_vif *arvif;
9520 	unsigned long links;
9521 	int link_id;
9522 
9523 	lockdep_assert_wiphy(hw->wiphy);
9524 
9525 	if (vif->valid_links) {
9526 		links = vif->valid_links;
9527 		for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
9528 			arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
9529 			if (!(arvif && arvif->ar))
9530 				continue;
9531 
9532 			ath12k_mac_update_vif_offload(arvif);
9533 		}
9534 
9535 		return;
9536 	}
9537 
9538 	ath12k_mac_update_vif_offload(&ahvif->deflink);
9539 }
9540 
ath12k_mac_vif_ap_active_any(struct ath12k_base * ab)9541 static bool ath12k_mac_vif_ap_active_any(struct ath12k_base *ab)
9542 {
9543 	struct ath12k *ar;
9544 	struct ath12k_pdev *pdev;
9545 	struct ath12k_link_vif *arvif;
9546 	int i;
9547 
9548 	for (i = 0; i < ab->num_radios; i++) {
9549 		pdev = &ab->pdevs[i];
9550 		ar = pdev->ar;
9551 		list_for_each_entry(arvif, &ar->arvifs, list) {
9552 			if (arvif->is_up &&
9553 			    arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP)
9554 				return true;
9555 		}
9556 	}
9557 	return false;
9558 }
9559 
ath12k_mac_11d_scan_start(struct ath12k * ar,u32 vdev_id)9560 void ath12k_mac_11d_scan_start(struct ath12k *ar, u32 vdev_id)
9561 {
9562 	struct wmi_11d_scan_start_arg arg;
9563 	int ret;
9564 
9565 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9566 
9567 	if (ar->regdom_set_by_user)
9568 		goto fin;
9569 
9570 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID)
9571 		goto fin;
9572 
9573 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
9574 		goto fin;
9575 
9576 	if (ath12k_mac_vif_ap_active_any(ar->ab))
9577 		goto fin;
9578 
9579 	arg.vdev_id = vdev_id;
9580 	arg.start_interval_msec = 0;
9581 	arg.scan_period_msec = ATH12K_SCAN_11D_INTERVAL;
9582 
9583 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
9584 		   "mac start 11d scan for vdev %d\n", vdev_id);
9585 
9586 	ret = ath12k_wmi_send_11d_scan_start_cmd(ar, &arg);
9587 	if (ret) {
9588 		ath12k_warn(ar->ab, "failed to start 11d scan vdev %d ret: %d\n",
9589 			    vdev_id, ret);
9590 	} else {
9591 		ar->vdev_id_11d_scan = vdev_id;
9592 		if (ar->state_11d == ATH12K_11D_PREPARING)
9593 			ar->state_11d = ATH12K_11D_RUNNING;
9594 	}
9595 
9596 fin:
9597 	if (ar->state_11d == ATH12K_11D_PREPARING) {
9598 		ar->state_11d = ATH12K_11D_IDLE;
9599 		complete(&ar->completed_11d_scan);
9600 	}
9601 }
9602 
ath12k_mac_11d_scan_stop(struct ath12k * ar)9603 void ath12k_mac_11d_scan_stop(struct ath12k *ar)
9604 {
9605 	int ret;
9606 	u32 vdev_id;
9607 
9608 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9609 
9610 	if (!test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ar->ab->wmi_ab.svc_map))
9611 		return;
9612 
9613 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac stop 11d for vdev %d\n",
9614 		   ar->vdev_id_11d_scan);
9615 
9616 	if (ar->state_11d == ATH12K_11D_PREPARING) {
9617 		ar->state_11d = ATH12K_11D_IDLE;
9618 		complete(&ar->completed_11d_scan);
9619 	}
9620 
9621 	if (ar->vdev_id_11d_scan != ATH12K_11D_INVALID_VDEV_ID) {
9622 		vdev_id = ar->vdev_id_11d_scan;
9623 
9624 		ret = ath12k_wmi_send_11d_scan_stop_cmd(ar, vdev_id);
9625 		if (ret) {
9626 			ath12k_warn(ar->ab,
9627 				    "failed to stopt 11d scan vdev %d ret: %d\n",
9628 				    vdev_id, ret);
9629 		} else {
9630 			ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
9631 			ar->state_11d = ATH12K_11D_IDLE;
9632 			complete(&ar->completed_11d_scan);
9633 		}
9634 	}
9635 }
9636 
ath12k_mac_11d_scan_stop_all(struct ath12k_base * ab)9637 void ath12k_mac_11d_scan_stop_all(struct ath12k_base *ab)
9638 {
9639 	struct ath12k *ar;
9640 	struct ath12k_pdev *pdev;
9641 	int i;
9642 
9643 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac stop soc 11d scan\n");
9644 
9645 	for (i = 0; i < ab->num_radios; i++) {
9646 		pdev = &ab->pdevs[i];
9647 		ar = pdev->ar;
9648 
9649 		ath12k_mac_11d_scan_stop(ar);
9650 	}
9651 }
9652 
ath12k_mac_determine_vdev_type(struct ieee80211_vif * vif,struct ath12k_vif * ahvif)9653 static void ath12k_mac_determine_vdev_type(struct ieee80211_vif *vif,
9654 					   struct ath12k_vif *ahvif)
9655 {
9656 	ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
9657 
9658 	switch (vif->type) {
9659 	case NL80211_IFTYPE_UNSPECIFIED:
9660 	case NL80211_IFTYPE_STATION:
9661 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
9662 
9663 		if (vif->p2p)
9664 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
9665 
9666 		break;
9667 	case NL80211_IFTYPE_MESH_POINT:
9668 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
9669 		fallthrough;
9670 	case NL80211_IFTYPE_AP:
9671 		ahvif->vdev_type = WMI_VDEV_TYPE_AP;
9672 
9673 		if (vif->p2p)
9674 			ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
9675 
9676 		break;
9677 	case NL80211_IFTYPE_MONITOR:
9678 		ahvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
9679 		break;
9680 	case NL80211_IFTYPE_P2P_DEVICE:
9681 		ahvif->vdev_type = WMI_VDEV_TYPE_STA;
9682 		ahvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
9683 		break;
9684 	default:
9685 		WARN_ON(1);
9686 		break;
9687 	}
9688 }
9689 
ath12k_mac_vdev_create(struct ath12k * ar,struct ath12k_link_vif * arvif)9690 int ath12k_mac_vdev_create(struct ath12k *ar, struct ath12k_link_vif *arvif)
9691 {
9692 	struct ath12k_hw *ah = ar->ah;
9693 	struct ath12k_base *ab = ar->ab;
9694 	struct ieee80211_hw *hw = ah->hw;
9695 	struct ath12k_vif *ahvif = arvif->ahvif;
9696 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
9697 	struct ath12k_wmi_vdev_create_arg vdev_arg = {};
9698 	struct ath12k_wmi_peer_create_arg peer_param = {};
9699 	struct ieee80211_bss_conf *link_conf = NULL;
9700 	u32 param_id, param_value;
9701 	u16 nss;
9702 	int i;
9703 	int ret, vdev_id;
9704 	u8 link_id;
9705 
9706 	lockdep_assert_wiphy(hw->wiphy);
9707 
9708 	/* In NO_VIRTUAL_MONITOR, its necessary to restrict only one monitor
9709 	 * interface in each radio
9710 	 */
9711 	if (vif->type == NL80211_IFTYPE_MONITOR && ar->monitor_vdev_created)
9712 		return -EINVAL;
9713 
9714 	link_id = arvif->link_id;
9715 
9716 	if (link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
9717 		link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
9718 		if (!link_conf) {
9719 			ath12k_warn(ar->ab, "unable to access bss link conf in vdev create for vif %pM link %u\n",
9720 				    vif->addr, arvif->link_id);
9721 			return -ENOLINK;
9722 		}
9723 	}
9724 
9725 	if (link_conf)
9726 		memcpy(arvif->bssid, link_conf->addr, ETH_ALEN);
9727 	else
9728 		memcpy(arvif->bssid, vif->addr, ETH_ALEN);
9729 
9730 	arvif->ar = ar;
9731 	vdev_id = __ffs64(ab->free_vdev_map);
9732 	arvif->vdev_id = vdev_id;
9733 	if (vif->type == NL80211_IFTYPE_MONITOR)
9734 		ar->monitor_vdev_id = vdev_id;
9735 
9736 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev create id %d type %d subtype %d map %llx\n",
9737 		   arvif->vdev_id, ahvif->vdev_type, ahvif->vdev_subtype,
9738 		   ab->free_vdev_map);
9739 
9740 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
9741 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
9742 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
9743 
9744 	ret = ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
9745 	if (ret) {
9746 		ath12k_warn(ab, "failed to create vdev parameters %d: %d\n",
9747 			    arvif->vdev_id, ret);
9748 		goto err;
9749 	}
9750 
9751 	ret = ath12k_wmi_vdev_create(ar, arvif->bssid, &vdev_arg);
9752 	if (ret) {
9753 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
9754 			    arvif->vdev_id, ret);
9755 		return ret;
9756 	}
9757 
9758 	ar->num_created_vdevs++;
9759 	arvif->is_created = true;
9760 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
9761 		   vif->addr, arvif->vdev_id);
9762 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
9763 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
9764 
9765 	spin_lock_bh(&ar->data_lock);
9766 	list_add(&arvif->list, &ar->arvifs);
9767 	spin_unlock_bh(&ar->data_lock);
9768 
9769 	ath12k_mac_update_vif_offload(arvif);
9770 
9771 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
9772 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9773 					    WMI_VDEV_PARAM_NSS, nss);
9774 	if (ret) {
9775 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
9776 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
9777 		goto err_vdev_del;
9778 	}
9779 
9780 	switch (ahvif->vdev_type) {
9781 	case WMI_VDEV_TYPE_AP:
9782 		peer_param.vdev_id = arvif->vdev_id;
9783 		peer_param.peer_addr = arvif->bssid;
9784 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
9785 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
9786 		if (ret) {
9787 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
9788 				    arvif->vdev_id, ret);
9789 			goto err_vdev_del;
9790 		}
9791 
9792 		ret = ath12k_mac_set_kickout(arvif);
9793 		if (ret) {
9794 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
9795 				    arvif->vdev_id, ret);
9796 			goto err_peer_del;
9797 		}
9798 		ath12k_mac_11d_scan_stop_all(ar->ab);
9799 		break;
9800 	case WMI_VDEV_TYPE_STA:
9801 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
9802 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
9803 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
9804 						  param_id, param_value);
9805 		if (ret) {
9806 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
9807 				    arvif->vdev_id, ret);
9808 			goto err_peer_del;
9809 		}
9810 
9811 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
9812 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
9813 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
9814 						  param_id, param_value);
9815 		if (ret) {
9816 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
9817 				    arvif->vdev_id, ret);
9818 			goto err_peer_del;
9819 		}
9820 
9821 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
9822 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
9823 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
9824 						  param_id, param_value);
9825 		if (ret) {
9826 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
9827 				    arvif->vdev_id, ret);
9828 			goto err_peer_del;
9829 		}
9830 
9831 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
9832 		if (ret) {
9833 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
9834 				    arvif->vdev_id, ret);
9835 			goto err_peer_del;
9836 		}
9837 
9838 		if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
9839 		    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
9840 		    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
9841 			reinit_completion(&ar->completed_11d_scan);
9842 			ar->state_11d = ATH12K_11D_PREPARING;
9843 		}
9844 		break;
9845 	case WMI_VDEV_TYPE_MONITOR:
9846 		ar->monitor_vdev_created = true;
9847 		break;
9848 	default:
9849 		break;
9850 	}
9851 
9852 	if (link_conf)
9853 		arvif->txpower = link_conf->txpower;
9854 	else
9855 		arvif->txpower = NL80211_TX_POWER_AUTOMATIC;
9856 
9857 	ret = ath12k_mac_txpower_recalc(ar);
9858 	if (ret)
9859 		goto err_peer_del;
9860 
9861 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
9862 	param_value = hw->wiphy->rts_threshold;
9863 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
9864 					    param_id, param_value);
9865 	if (ret) {
9866 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
9867 			    arvif->vdev_id, ret);
9868 	}
9869 
9870 	ath12k_dp_vdev_tx_attach(ar, arvif);
9871 
9872 	return ret;
9873 
9874 err_peer_del:
9875 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
9876 		reinit_completion(&ar->peer_delete_done);
9877 
9878 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arvif->bssid,
9879 						      arvif->vdev_id);
9880 		if (ret) {
9881 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
9882 				    arvif->vdev_id, arvif->bssid);
9883 			goto err;
9884 		}
9885 
9886 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
9887 						       arvif->bssid);
9888 		if (ret)
9889 			goto err_vdev_del;
9890 
9891 		ar->num_peers--;
9892 	}
9893 
9894 err_vdev_del:
9895 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
9896 		ar->monitor_vdev_id = -1;
9897 		ar->monitor_vdev_created = false;
9898 	}
9899 
9900 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
9901 	ar->num_created_vdevs--;
9902 	arvif->is_created = false;
9903 	arvif->ar = NULL;
9904 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
9905 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
9906 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
9907 	spin_lock_bh(&ar->data_lock);
9908 	list_del(&arvif->list);
9909 	spin_unlock_bh(&ar->data_lock);
9910 
9911 err:
9912 	arvif->ar = NULL;
9913 	return ret;
9914 }
9915 
ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif * arvif)9916 static void ath12k_mac_vif_flush_key_cache(struct ath12k_link_vif *arvif)
9917 {
9918 	struct ath12k_key_conf *key_conf, *tmp;
9919 	struct ath12k_vif *ahvif = arvif->ahvif;
9920 	struct ath12k_hw *ah = ahvif->ah;
9921 	struct ath12k_sta *ahsta;
9922 	struct ath12k_link_sta *arsta;
9923 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
9924 	int ret;
9925 
9926 	lockdep_assert_wiphy(ah->hw->wiphy);
9927 
9928 	list_for_each_entry_safe(key_conf, tmp, &cache->key_conf.list, list) {
9929 		arsta = NULL;
9930 		if (key_conf->sta) {
9931 			ahsta = ath12k_sta_to_ahsta(key_conf->sta);
9932 			arsta = wiphy_dereference(ah->hw->wiphy,
9933 						  ahsta->link[arvif->link_id]);
9934 			if (!arsta)
9935 				goto free_cache;
9936 		}
9937 
9938 		ret = ath12k_mac_set_key(arvif->ar, key_conf->cmd,
9939 					 arvif, arsta,
9940 					 key_conf->key);
9941 		if (ret)
9942 			ath12k_warn(arvif->ar->ab, "unable to apply set key param to vdev %d ret %d\n",
9943 				    arvif->vdev_id, ret);
9944 free_cache:
9945 		list_del(&key_conf->list);
9946 		kfree(key_conf);
9947 	}
9948 }
9949 
ath12k_mac_vif_cache_flush(struct ath12k * ar,struct ath12k_link_vif * arvif)9950 static void ath12k_mac_vif_cache_flush(struct ath12k *ar, struct ath12k_link_vif *arvif)
9951 {
9952 	struct ath12k_vif *ahvif = arvif->ahvif;
9953 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
9954 	struct ath12k_vif_cache *cache = ahvif->cache[arvif->link_id];
9955 	struct ath12k_base *ab = ar->ab;
9956 	struct ieee80211_bss_conf *link_conf;
9957 
9958 	int ret;
9959 
9960 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
9961 
9962 	if (!cache)
9963 		return;
9964 
9965 	if (cache->tx_conf.changed) {
9966 		ret = ath12k_mac_conf_tx(arvif, cache->tx_conf.ac,
9967 					 &cache->tx_conf.tx_queue_params);
9968 		if (ret)
9969 			ath12k_warn(ab,
9970 				    "unable to apply tx config parameters to vdev %d\n",
9971 				    ret);
9972 	}
9973 
9974 	if (cache->bss_conf_changed) {
9975 		link_conf = ath12k_mac_get_link_bss_conf(arvif);
9976 		if (!link_conf) {
9977 			ath12k_warn(ar->ab, "unable to access bss link conf in cache flush for vif %pM link %u\n",
9978 				    vif->addr, arvif->link_id);
9979 			return;
9980 		}
9981 		ath12k_mac_bss_info_changed(ar, arvif, link_conf,
9982 					    cache->bss_conf_changed);
9983 	}
9984 
9985 	if (!list_empty(&cache->key_conf.list))
9986 		ath12k_mac_vif_flush_key_cache(arvif);
9987 
9988 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
9989 }
9990 
ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw * hw,struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)9991 static struct ath12k *ath12k_mac_assign_vif_to_vdev(struct ieee80211_hw *hw,
9992 						    struct ath12k_link_vif *arvif,
9993 						    struct ieee80211_chanctx_conf *ctx)
9994 {
9995 	struct ath12k_vif *ahvif = arvif->ahvif;
9996 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
9997 	struct ath12k_link_vif *scan_arvif;
9998 	struct ath12k_hw *ah = hw->priv;
9999 	struct ath12k *ar;
10000 	struct ath12k_base *ab;
10001 	u8 link_id = arvif->link_id, scan_link_id;
10002 	unsigned long scan_link_map;
10003 	int ret;
10004 
10005 	lockdep_assert_wiphy(hw->wiphy);
10006 
10007 	if (ah->num_radio == 1)
10008 		ar = ah->radio;
10009 	else if (ctx)
10010 		ar = ath12k_get_ar_by_ctx(hw, ctx);
10011 	else
10012 		return NULL;
10013 
10014 	if (!ar)
10015 		return NULL;
10016 
10017 	/* cleanup the scan vdev if we are done scan on that ar
10018 	 * and now we want to create for actual usage.
10019 	 */
10020 	if (ieee80211_vif_is_mld(vif)) {
10021 		scan_link_map = ahvif->links_map & ATH12K_SCAN_LINKS_MASK;
10022 		for_each_set_bit(scan_link_id, &scan_link_map, ATH12K_NUM_MAX_LINKS) {
10023 			scan_arvif = wiphy_dereference(hw->wiphy,
10024 						       ahvif->link[scan_link_id]);
10025 			if (scan_arvif && scan_arvif->ar == ar) {
10026 				ar->scan.arvif = NULL;
10027 				ath12k_mac_remove_link_interface(hw, scan_arvif);
10028 				ath12k_mac_unassign_link_vif(scan_arvif);
10029 				break;
10030 			}
10031 		}
10032 	}
10033 
10034 	if (arvif->ar) {
10035 		/* This is not expected really */
10036 		if (WARN_ON(!arvif->is_created)) {
10037 			arvif->ar = NULL;
10038 			return NULL;
10039 		}
10040 
10041 		if (ah->num_radio == 1)
10042 			return arvif->ar;
10043 
10044 		/* This can happen as scan vdev gets created during multiple scans
10045 		 * across different radios before a vdev is brought up in
10046 		 * a certain radio.
10047 		 */
10048 		if (ar != arvif->ar) {
10049 			if (WARN_ON(arvif->is_started))
10050 				return NULL;
10051 
10052 			ath12k_mac_remove_link_interface(hw, arvif);
10053 			ath12k_mac_unassign_link_vif(arvif);
10054 		}
10055 	}
10056 
10057 	ab = ar->ab;
10058 
10059 	/* Assign arvif again here since previous radio switch block
10060 	 * would've unassigned and cleared it.
10061 	 */
10062 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
10063 	if (vif->type == NL80211_IFTYPE_AP &&
10064 	    ar->num_peers > (ar->max_num_peers - 1)) {
10065 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
10066 		goto unlock;
10067 	}
10068 
10069 	if (arvif->is_created)
10070 		goto flush;
10071 
10072 	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS(ab) - 1)) {
10073 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
10074 			    TARGET_NUM_VDEVS(ab));
10075 		goto unlock;
10076 	}
10077 
10078 	ret = ath12k_mac_vdev_create(ar, arvif);
10079 	if (ret) {
10080 		ath12k_warn(ab, "failed to create vdev %pM ret %d", vif->addr, ret);
10081 		goto unlock;
10082 	}
10083 
10084 flush:
10085 	/* If the vdev is created during channel assign and not during
10086 	 * add_interface(), Apply any parameters for the vdev which were received
10087 	 * after add_interface, corresponding to this vif.
10088 	 */
10089 	ath12k_mac_vif_cache_flush(ar, arvif);
10090 unlock:
10091 	return arvif->ar;
10092 }
10093 
ath12k_mac_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10094 static int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
10095 				       struct ieee80211_vif *vif)
10096 {
10097 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10098 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10099 	struct ath12k_reg_info *reg_info;
10100 	struct ath12k_link_vif *arvif;
10101 	struct ath12k_base *ab;
10102 	struct ath12k *ar;
10103 	int i;
10104 
10105 	lockdep_assert_wiphy(hw->wiphy);
10106 
10107 	memset(ahvif, 0, sizeof(*ahvif));
10108 
10109 	ahvif->ah = ah;
10110 	ahvif->vif = vif;
10111 	arvif = &ahvif->deflink;
10112 
10113 	ath12k_mac_init_arvif(ahvif, arvif, -1);
10114 
10115 	/* Allocate Default Queue now and reassign during actual vdev create */
10116 	vif->cab_queue = ATH12K_HW_DEFAULT_QUEUE;
10117 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
10118 		vif->hw_queue[i] = ATH12K_HW_DEFAULT_QUEUE;
10119 
10120 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
10121 
10122 	ath12k_mac_determine_vdev_type(vif, ahvif);
10123 
10124 	for_each_ar(ah, ar, i) {
10125 		if (!ath12k_wmi_supports_6ghz_cc_ext(ar))
10126 			continue;
10127 
10128 		ab = ar->ab;
10129 		reg_info = ab->reg_info[ar->pdev_idx];
10130 		ath12k_dbg(ab, ATH12K_DBG_MAC, "interface added to change reg rules\n");
10131 		ah->regd_updated = false;
10132 		ath12k_reg_handle_chan_list(ab, reg_info, ahvif->vdev_type,
10133 					    IEEE80211_REG_UNSET_AP);
10134 		break;
10135 	}
10136 
10137 	/* Defer vdev creation until assign_chanctx or hw_scan is initiated as driver
10138 	 * will not know if this interface is an ML vif at this point.
10139 	 */
10140 	return 0;
10141 }
10142 
ath12k_mac_vif_unref(struct ath12k_dp * dp,struct ieee80211_vif * vif)10143 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
10144 {
10145 	struct ath12k_tx_desc_info *tx_desc_info;
10146 	struct ath12k_skb_cb *skb_cb;
10147 	struct sk_buff *skb;
10148 	int i;
10149 
10150 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
10151 		spin_lock_bh(&dp->tx_desc_lock[i]);
10152 
10153 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
10154 				    list) {
10155 			skb = tx_desc_info->skb;
10156 			if (!skb)
10157 				continue;
10158 
10159 			skb_cb = ATH12K_SKB_CB(skb);
10160 			if (skb_cb->vif == vif)
10161 				skb_cb->vif = NULL;
10162 		}
10163 
10164 		spin_unlock_bh(&dp->tx_desc_lock[i]);
10165 	}
10166 }
10167 
ath12k_mac_vdev_delete(struct ath12k * ar,struct ath12k_link_vif * arvif)10168 static int ath12k_mac_vdev_delete(struct ath12k *ar, struct ath12k_link_vif *arvif)
10169 {
10170 	struct ath12k_vif *ahvif = arvif->ahvif;
10171 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(ahvif);
10172 	struct ath12k_base *ab = ar->ab;
10173 	unsigned long time_left;
10174 	int ret;
10175 
10176 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10177 
10178 	reinit_completion(&ar->vdev_delete_done);
10179 
10180 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
10181 	if (ret) {
10182 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
10183 			    arvif->vdev_id, ret);
10184 		goto err_vdev_del;
10185 	}
10186 
10187 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
10188 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
10189 	if (time_left == 0) {
10190 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
10191 		goto err_vdev_del;
10192 	}
10193 
10194 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
10195 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
10196 	ar->num_created_vdevs--;
10197 
10198 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
10199 		ar->monitor_vdev_id = -1;
10200 		ar->monitor_vdev_created = false;
10201 	}
10202 
10203 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
10204 		   vif->addr, arvif->vdev_id);
10205 
10206 err_vdev_del:
10207 	spin_lock_bh(&ar->data_lock);
10208 	list_del(&arvif->list);
10209 	spin_unlock_bh(&ar->data_lock);
10210 
10211 	ath12k_peer_cleanup(ar, arvif->vdev_id);
10212 	ath12k_ahvif_put_link_cache(ahvif, arvif->link_id);
10213 
10214 	idr_for_each(&ar->txmgmt_idr,
10215 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
10216 
10217 	ath12k_mac_vif_unref(&ab->dp, vif);
10218 	ath12k_dp_tx_put_bank_profile(&ab->dp, arvif->bank_id);
10219 
10220 	/* Recalc txpower for remaining vdev */
10221 	ath12k_mac_txpower_recalc(ar);
10222 
10223 	/* TODO: recal traffic pause state based on the available vdevs */
10224 	arvif->is_created = false;
10225 	arvif->ar = NULL;
10226 
10227 	return ret;
10228 }
10229 
ath12k_mac_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)10230 static void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
10231 					   struct ieee80211_vif *vif)
10232 {
10233 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10234 	struct ath12k_link_vif *arvif;
10235 	struct ath12k *ar;
10236 	u8 link_id;
10237 
10238 	lockdep_assert_wiphy(hw->wiphy);
10239 
10240 	for (link_id = 0; link_id < ATH12K_NUM_MAX_LINKS; link_id++) {
10241 		/* if we cached some config but never received assign chanctx,
10242 		 * free the allocated cache.
10243 		 */
10244 		ath12k_ahvif_put_link_cache(ahvif, link_id);
10245 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
10246 		if (!arvif || !arvif->is_created)
10247 			continue;
10248 
10249 		ar = arvif->ar;
10250 
10251 		/* Scan abortion is in progress since before this, cancel_hw_scan()
10252 		 * is expected to be executed. Since link is anyways going to be removed
10253 		 * now, just cancel the worker and send the scan aborted to user space
10254 		 */
10255 		if (ar->scan.arvif == arvif) {
10256 			wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
10257 
10258 			spin_lock_bh(&ar->data_lock);
10259 			ar->scan.arvif = NULL;
10260 			if (!ar->scan.is_roc) {
10261 				struct cfg80211_scan_info info = {
10262 					.aborted = true,
10263 				};
10264 
10265 				ath12k_mac_scan_send_complete(ar, &info);
10266 			}
10267 
10268 			ar->scan.state = ATH12K_SCAN_IDLE;
10269 			ar->scan_channel = NULL;
10270 			ar->scan.roc_freq = 0;
10271 			spin_unlock_bh(&ar->data_lock);
10272 		}
10273 
10274 		ath12k_mac_remove_link_interface(hw, arvif);
10275 		ath12k_mac_unassign_link_vif(arvif);
10276 	}
10277 }
10278 
10279 /* FIXME: Has to be verified. */
10280 #define SUPPORTED_FILTERS			\
10281 	(FIF_ALLMULTI |				\
10282 	FIF_CONTROL |				\
10283 	FIF_PSPOLL |				\
10284 	FIF_OTHER_BSS |				\
10285 	FIF_BCN_PRBRESP_PROMISC |		\
10286 	FIF_PROBE_REQ |				\
10287 	FIF_FCSFAIL)
10288 
ath12k_mac_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)10289 static void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
10290 					   unsigned int changed_flags,
10291 					   unsigned int *total_flags,
10292 					   u64 multicast)
10293 {
10294 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10295 	struct ath12k *ar;
10296 
10297 	lockdep_assert_wiphy(hw->wiphy);
10298 
10299 	ar = ath12k_ah_to_ar(ah, 0);
10300 
10301 	*total_flags &= SUPPORTED_FILTERS;
10302 	ar->filter_flags = *total_flags;
10303 }
10304 
ath12k_mac_op_get_antenna(struct ieee80211_hw * hw,int radio_idx,u32 * tx_ant,u32 * rx_ant)10305 static int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
10306 				     u32 *tx_ant, u32 *rx_ant)
10307 {
10308 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10309 	int antennas_rx = 0, antennas_tx = 0;
10310 	struct ath12k *ar;
10311 	int i;
10312 
10313 	lockdep_assert_wiphy(hw->wiphy);
10314 
10315 	for_each_ar(ah, ar, i) {
10316 		antennas_rx = max_t(u32, antennas_rx, ar->cfg_rx_chainmask);
10317 		antennas_tx = max_t(u32, antennas_tx, ar->cfg_tx_chainmask);
10318 	}
10319 
10320 	*tx_ant = antennas_tx;
10321 	*rx_ant = antennas_rx;
10322 
10323 	return 0;
10324 }
10325 
ath12k_mac_op_set_antenna(struct ieee80211_hw * hw,int radio_idx,u32 tx_ant,u32 rx_ant)10326 static int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, int radio_idx,
10327 				     u32 tx_ant, u32 rx_ant)
10328 {
10329 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
10330 	struct ath12k *ar;
10331 	int ret = 0;
10332 	int i;
10333 
10334 	lockdep_assert_wiphy(hw->wiphy);
10335 
10336 	for_each_ar(ah, ar, i) {
10337 		ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
10338 		if (ret)
10339 			break;
10340 	}
10341 
10342 	return ret;
10343 }
10344 
ath12k_mac_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params,u8 link_id)10345 static int ath12k_mac_ampdu_action(struct ieee80211_hw *hw,
10346 				   struct ieee80211_vif *vif,
10347 				   struct ieee80211_ampdu_params *params,
10348 				   u8 link_id)
10349 {
10350 	struct ath12k *ar;
10351 	int ret = -EINVAL;
10352 
10353 	lockdep_assert_wiphy(hw->wiphy);
10354 
10355 	ar = ath12k_get_ar_by_vif(hw, vif, link_id);
10356 	if (!ar)
10357 		return -EINVAL;
10358 
10359 	switch (params->action) {
10360 	case IEEE80211_AMPDU_RX_START:
10361 		ret = ath12k_dp_rx_ampdu_start(ar, params, link_id);
10362 		break;
10363 	case IEEE80211_AMPDU_RX_STOP:
10364 		ret = ath12k_dp_rx_ampdu_stop(ar, params, link_id);
10365 		break;
10366 	case IEEE80211_AMPDU_TX_START:
10367 	case IEEE80211_AMPDU_TX_STOP_CONT:
10368 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
10369 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
10370 	case IEEE80211_AMPDU_TX_OPERATIONAL:
10371 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
10372 		 * Tx aggregation requests.
10373 		 */
10374 		ret = -EOPNOTSUPP;
10375 		break;
10376 	}
10377 
10378 	if (ret)
10379 		ath12k_warn(ar->ab, "unable to perform ampdu action %d for vif %pM link %u ret %d\n",
10380 			    params->action, vif->addr, link_id, ret);
10381 
10382 	return ret;
10383 }
10384 
ath12k_mac_op_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)10385 static int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
10386 				      struct ieee80211_vif *vif,
10387 				      struct ieee80211_ampdu_params *params)
10388 {
10389 	struct ieee80211_sta *sta = params->sta;
10390 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
10391 	unsigned long links_map = ahsta->links_map;
10392 	int ret = -EINVAL;
10393 	u8 link_id;
10394 
10395 	lockdep_assert_wiphy(hw->wiphy);
10396 
10397 	if (WARN_ON(!links_map))
10398 		return ret;
10399 
10400 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10401 		ret = ath12k_mac_ampdu_action(hw, vif, params, link_id);
10402 		if (ret)
10403 			return ret;
10404 	}
10405 
10406 	return 0;
10407 }
10408 
ath12k_mac_op_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)10409 static int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
10410 				     struct ieee80211_chanctx_conf *ctx)
10411 {
10412 	struct ath12k *ar;
10413 	struct ath12k_base *ab;
10414 
10415 	lockdep_assert_wiphy(hw->wiphy);
10416 
10417 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10418 	if (!ar)
10419 		return -EINVAL;
10420 
10421 	ab = ar->ab;
10422 
10423 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10424 		   "mac chanctx add freq %u width %d ptr %p\n",
10425 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10426 
10427 	spin_lock_bh(&ar->data_lock);
10428 	/* TODO: In case of multiple channel context, populate rx_channel from
10429 	 * Rx PPDU desc information.
10430 	 */
10431 	ar->rx_channel = ctx->def.chan;
10432 	spin_unlock_bh(&ar->data_lock);
10433 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10434 
10435 	return 0;
10436 }
10437 
ath12k_mac_op_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)10438 static void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
10439 					 struct ieee80211_chanctx_conf *ctx)
10440 {
10441 	struct ath12k *ar;
10442 	struct ath12k_base *ab;
10443 
10444 	lockdep_assert_wiphy(hw->wiphy);
10445 
10446 	ar = ath12k_get_ar_by_ctx(hw, ctx);
10447 	if (!ar)
10448 		return;
10449 
10450 	ab = ar->ab;
10451 
10452 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10453 		   "mac chanctx remove freq %u width %d ptr %p\n",
10454 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
10455 
10456 	spin_lock_bh(&ar->data_lock);
10457 	/* TODO: In case of there is one more channel context left, populate
10458 	 * rx_channel with the channel of that remaining channel context.
10459 	 */
10460 	ar->rx_channel = NULL;
10461 	spin_unlock_bh(&ar->data_lock);
10462 	ar->chan_tx_pwr = ATH12K_PDEV_TX_POWER_INVALID;
10463 }
10464 
10465 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)10466 ath12k_mac_check_down_grade_phy_mode(struct ath12k *ar,
10467 				     enum wmi_phy_mode mode,
10468 				     enum nl80211_band band,
10469 				     enum nl80211_iftype type)
10470 {
10471 	struct ieee80211_sta_eht_cap *eht_cap = NULL;
10472 	enum wmi_phy_mode down_mode;
10473 	int n = ar->mac.sbands[band].n_iftype_data;
10474 	int i;
10475 	struct ieee80211_sband_iftype_data *data;
10476 
10477 	if (mode < MODE_11BE_EHT20)
10478 		return mode;
10479 
10480 	data = ar->mac.iftype[band];
10481 	for (i = 0; i < n; i++) {
10482 		if (data[i].types_mask & BIT(type)) {
10483 			eht_cap = &data[i].eht_cap;
10484 			break;
10485 		}
10486 	}
10487 
10488 	if (eht_cap && eht_cap->has_eht)
10489 		return mode;
10490 
10491 	switch (mode) {
10492 	case MODE_11BE_EHT20:
10493 		down_mode = MODE_11AX_HE20;
10494 		break;
10495 	case MODE_11BE_EHT40:
10496 		down_mode = MODE_11AX_HE40;
10497 		break;
10498 	case MODE_11BE_EHT80:
10499 		down_mode = MODE_11AX_HE80;
10500 		break;
10501 	case MODE_11BE_EHT80_80:
10502 		down_mode = MODE_11AX_HE80_80;
10503 		break;
10504 	case MODE_11BE_EHT160:
10505 	case MODE_11BE_EHT160_160:
10506 	case MODE_11BE_EHT320:
10507 		down_mode = MODE_11AX_HE160;
10508 		break;
10509 	case MODE_11BE_EHT20_2G:
10510 		down_mode = MODE_11AX_HE20_2G;
10511 		break;
10512 	case MODE_11BE_EHT40_2G:
10513 		down_mode = MODE_11AX_HE40_2G;
10514 		break;
10515 	default:
10516 		down_mode = mode;
10517 		break;
10518 	}
10519 
10520 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10521 		   "mac vdev start phymode %s downgrade to %s\n",
10522 		   ath12k_mac_phymode_str(mode),
10523 		   ath12k_mac_phymode_str(down_mode));
10524 
10525 	return down_mode;
10526 }
10527 
10528 static void
ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif * arvif,struct wmi_ml_arg * ml_arg)10529 ath12k_mac_mlo_get_vdev_args(struct ath12k_link_vif *arvif,
10530 			     struct wmi_ml_arg *ml_arg)
10531 {
10532 	struct ath12k_vif *ahvif = arvif->ahvif;
10533 	struct wmi_ml_partner_info *partner_info;
10534 	struct ieee80211_bss_conf *link_conf;
10535 	struct ath12k_link_vif *arvif_p;
10536 	unsigned long links;
10537 	u8 link_id;
10538 
10539 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
10540 
10541 	if (!ath12k_mac_is_ml_arvif(arvif))
10542 		return;
10543 
10544 	if (hweight16(ahvif->vif->valid_links) > ATH12K_WMI_MLO_MAX_LINKS)
10545 		return;
10546 
10547 	ml_arg->enabled = true;
10548 
10549 	/* Driver always add a new link via VDEV START, FW takes
10550 	 * care of internally adding this link to existing
10551 	 * link vdevs which are advertised as partners below
10552 	 */
10553 	ml_arg->link_add = true;
10554 
10555 	ml_arg->assoc_link = arvif->is_sta_assoc_link;
10556 
10557 	partner_info = ml_arg->partner_info;
10558 
10559 	links = ahvif->links_map;
10560 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
10561 		arvif_p = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
10562 
10563 		if (WARN_ON(!arvif_p))
10564 			continue;
10565 
10566 		if (arvif == arvif_p)
10567 			continue;
10568 
10569 		if (!arvif_p->is_created)
10570 			continue;
10571 
10572 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
10573 					      ahvif->vif->link_conf[arvif_p->link_id]);
10574 
10575 		if (!link_conf)
10576 			continue;
10577 
10578 		partner_info->vdev_id = arvif_p->vdev_id;
10579 		partner_info->hw_link_id = arvif_p->ar->pdev->hw_link_id;
10580 		ether_addr_copy(partner_info->addr, link_conf->addr);
10581 		ml_arg->num_partner_links++;
10582 		partner_info++;
10583 	}
10584 }
10585 
10586 static int
ath12k_mac_vdev_start_restart(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx,bool restart)10587 ath12k_mac_vdev_start_restart(struct ath12k_link_vif *arvif,
10588 			      struct ieee80211_chanctx_conf *ctx,
10589 			      bool restart)
10590 {
10591 	struct ath12k *ar = arvif->ar;
10592 	struct ath12k_base *ab = ar->ab;
10593 	struct wmi_vdev_start_req_arg arg = {};
10594 	const struct cfg80211_chan_def *chandef = &ctx->def;
10595 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
10596 	struct ath12k_vif *ahvif = arvif->ahvif;
10597 	struct ieee80211_bss_conf *link_conf;
10598 	unsigned int dfs_cac_time;
10599 	int ret;
10600 
10601 	lockdep_assert_wiphy(hw->wiphy);
10602 
10603 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
10604 	if (!link_conf) {
10605 		ath12k_warn(ar->ab, "unable to access bss link conf in vdev start for vif %pM link %u\n",
10606 			    ahvif->vif->addr, arvif->link_id);
10607 		return -ENOLINK;
10608 	}
10609 
10610 	reinit_completion(&ar->vdev_setup_done);
10611 
10612 	arg.vdev_id = arvif->vdev_id;
10613 	arg.dtim_period = arvif->dtim_period;
10614 	arg.bcn_intval = arvif->beacon_interval;
10615 	arg.punct_bitmap = ~arvif->punct_bitmap;
10616 
10617 	arg.freq = chandef->chan->center_freq;
10618 	arg.band_center_freq1 = chandef->center_freq1;
10619 	arg.band_center_freq2 = chandef->center_freq2;
10620 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
10621 
10622 	arg.mode = ath12k_mac_check_down_grade_phy_mode(ar, arg.mode,
10623 							chandef->chan->band,
10624 							ahvif->vif->type);
10625 	arg.min_power = 0;
10626 	arg.max_power = chandef->chan->max_power;
10627 	arg.max_reg_power = chandef->chan->max_reg_power;
10628 	arg.max_antenna_gain = chandef->chan->max_antenna_gain;
10629 
10630 	arg.pref_tx_streams = ar->num_tx_chains;
10631 	arg.pref_rx_streams = ar->num_rx_chains;
10632 
10633 	arg.mbssid_flags = WMI_VDEV_MBSSID_FLAGS_NON_MBSSID_AP;
10634 	arg.mbssid_tx_vdev_id = 0;
10635 	if (test_bit(WMI_TLV_SERVICE_MBSS_PARAM_IN_VDEV_START_SUPPORT,
10636 		     ar->ab->wmi_ab.svc_map)) {
10637 		ret = ath12k_mac_setup_vdev_params_mbssid(arvif,
10638 							  &arg.mbssid_flags,
10639 							  &arg.mbssid_tx_vdev_id);
10640 		if (ret)
10641 			return ret;
10642 	}
10643 
10644 	if (ahvif->vdev_type == WMI_VDEV_TYPE_AP) {
10645 		arg.ssid = ahvif->u.ap.ssid;
10646 		arg.ssid_len = ahvif->u.ap.ssid_len;
10647 		arg.hidden_ssid = ahvif->u.ap.hidden_ssid;
10648 
10649 		/* For now allow DFS for AP mode */
10650 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
10651 
10652 		arg.freq2_radar = ctx->radar_enabled;
10653 
10654 		arg.passive = arg.chan_radar;
10655 
10656 		spin_lock_bh(&ab->base_lock);
10657 		arg.regdomain = ar->ab->dfs_region;
10658 		spin_unlock_bh(&ab->base_lock);
10659 
10660 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
10661 	}
10662 
10663 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
10664 
10665 	if (!restart)
10666 		ath12k_mac_mlo_get_vdev_args(arvif, &arg.ml);
10667 
10668 	ath12k_dbg(ab, ATH12K_DBG_MAC,
10669 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
10670 		   arg.vdev_id, arg.freq,
10671 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
10672 
10673 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
10674 	if (ret) {
10675 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
10676 			    restart ? "restart" : "start", arg.vdev_id);
10677 		return ret;
10678 	}
10679 
10680 	ret = ath12k_mac_vdev_setup_sync(ar);
10681 	if (ret) {
10682 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
10683 			    arg.vdev_id, restart ? "restart" : "start", ret);
10684 		return ret;
10685 	}
10686 
10687 	/* TODO: For now we only set TPC power here. However when
10688 	 * channel changes, say CSA, it should be updated again.
10689 	 */
10690 	if (ath12k_mac_supports_tpc(ar, ahvif, chandef)) {
10691 		ath12k_mac_fill_reg_tpc_info(ar, arvif, ctx);
10692 		ath12k_wmi_send_vdev_set_tpc_power(ar, arvif->vdev_id,
10693 						   &arvif->reg_tpc_info);
10694 	}
10695 
10696 	ar->num_started_vdevs++;
10697 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
10698 		   ahvif->vif->addr, arvif->vdev_id);
10699 
10700 	/* Enable CAC Running Flag in the driver by checking all sub-channel's DFS
10701 	 * state as NL80211_DFS_USABLE which indicates CAC needs to be
10702 	 * done before channel usage. This flag is used to drop rx packets.
10703 	 * during CAC.
10704 	 */
10705 	/* TODO: Set the flag for other interface types as required */
10706 	if (arvif->ahvif->vdev_type == WMI_VDEV_TYPE_AP && ctx->radar_enabled &&
10707 	    cfg80211_chandef_dfs_usable(hw->wiphy, chandef)) {
10708 		set_bit(ATH12K_FLAG_CAC_RUNNING, &ar->dev_flags);
10709 		dfs_cac_time = cfg80211_chandef_dfs_cac_time(hw->wiphy, chandef);
10710 
10711 		ath12k_dbg(ab, ATH12K_DBG_MAC,
10712 			   "CAC started dfs_cac_time %u center_freq %d center_freq1 %d for vdev %d\n",
10713 			   dfs_cac_time, arg.freq, arg.band_center_freq1, arg.vdev_id);
10714 	}
10715 
10716 	ret = ath12k_mac_set_txbf_conf(arvif);
10717 	if (ret)
10718 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
10719 			    arvif->vdev_id, ret);
10720 
10721 	return 0;
10722 }
10723 
ath12k_mac_vdev_start(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)10724 static int ath12k_mac_vdev_start(struct ath12k_link_vif *arvif,
10725 				 struct ieee80211_chanctx_conf *ctx)
10726 {
10727 	return ath12k_mac_vdev_start_restart(arvif, ctx, false);
10728 }
10729 
ath12k_mac_vdev_restart(struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)10730 static int ath12k_mac_vdev_restart(struct ath12k_link_vif *arvif,
10731 				   struct ieee80211_chanctx_conf *ctx)
10732 {
10733 	return ath12k_mac_vdev_start_restart(arvif, ctx, true);
10734 }
10735 
10736 struct ath12k_mac_change_chanctx_arg {
10737 	struct ieee80211_chanctx_conf *ctx;
10738 	struct ieee80211_vif_chanctx_switch *vifs;
10739 	int n_vifs;
10740 	int next_vif;
10741 	struct ath12k *ar;
10742 };
10743 
10744 static void
ath12k_mac_change_chanctx_cnt_iter(void * data,u8 * mac,struct ieee80211_vif * vif)10745 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
10746 				   struct ieee80211_vif *vif)
10747 {
10748 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10749 	struct ath12k_mac_change_chanctx_arg *arg = data;
10750 	struct ieee80211_bss_conf *link_conf;
10751 	struct ath12k_link_vif *arvif;
10752 	unsigned long links_map;
10753 	u8 link_id;
10754 
10755 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
10756 
10757 	links_map = ahvif->links_map;
10758 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10759 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
10760 		if (WARN_ON(!arvif))
10761 			continue;
10762 
10763 		if (!arvif->is_created || arvif->ar != arg->ar)
10764 			continue;
10765 
10766 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
10767 					      vif->link_conf[link_id]);
10768 		if (WARN_ON(!link_conf))
10769 			continue;
10770 
10771 		if (rcu_access_pointer(link_conf->chanctx_conf) != arg->ctx)
10772 			continue;
10773 
10774 		arg->n_vifs++;
10775 	}
10776 }
10777 
10778 static void
ath12k_mac_change_chanctx_fill_iter(void * data,u8 * mac,struct ieee80211_vif * vif)10779 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
10780 				    struct ieee80211_vif *vif)
10781 {
10782 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
10783 	struct ath12k_mac_change_chanctx_arg *arg = data;
10784 	struct ieee80211_bss_conf *link_conf;
10785 	struct ieee80211_chanctx_conf *ctx;
10786 	struct ath12k_link_vif *arvif;
10787 	unsigned long links_map;
10788 	u8 link_id;
10789 
10790 	lockdep_assert_wiphy(ahvif->ah->hw->wiphy);
10791 
10792 	links_map = ahvif->links_map;
10793 	for_each_set_bit(link_id, &links_map, IEEE80211_MLD_MAX_NUM_LINKS) {
10794 		arvif = wiphy_dereference(ahvif->ah->hw->wiphy, ahvif->link[link_id]);
10795 		if (WARN_ON(!arvif))
10796 			continue;
10797 
10798 		if (!arvif->is_created || arvif->ar != arg->ar)
10799 			continue;
10800 
10801 		link_conf = wiphy_dereference(ahvif->ah->hw->wiphy,
10802 					      vif->link_conf[arvif->link_id]);
10803 		if (WARN_ON(!link_conf))
10804 			continue;
10805 
10806 		ctx = rcu_access_pointer(link_conf->chanctx_conf);
10807 		if (ctx != arg->ctx)
10808 			continue;
10809 
10810 		if (WARN_ON(arg->next_vif == arg->n_vifs))
10811 			return;
10812 
10813 		arg->vifs[arg->next_vif].vif = vif;
10814 		arg->vifs[arg->next_vif].old_ctx = ctx;
10815 		arg->vifs[arg->next_vif].new_ctx = ctx;
10816 		arg->vifs[arg->next_vif].link_conf = link_conf;
10817 		arg->next_vif++;
10818 	}
10819 }
10820 
ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)10821 static u32 ath12k_mac_nlwidth_to_wmiwidth(enum nl80211_chan_width width)
10822 {
10823 	switch (width) {
10824 	case NL80211_CHAN_WIDTH_20:
10825 		return WMI_CHAN_WIDTH_20;
10826 	case NL80211_CHAN_WIDTH_40:
10827 		return WMI_CHAN_WIDTH_40;
10828 	case NL80211_CHAN_WIDTH_80:
10829 		return WMI_CHAN_WIDTH_80;
10830 	case NL80211_CHAN_WIDTH_160:
10831 		return WMI_CHAN_WIDTH_160;
10832 	case NL80211_CHAN_WIDTH_80P80:
10833 		return WMI_CHAN_WIDTH_80P80;
10834 	case NL80211_CHAN_WIDTH_5:
10835 		return WMI_CHAN_WIDTH_5;
10836 	case NL80211_CHAN_WIDTH_10:
10837 		return WMI_CHAN_WIDTH_10;
10838 	case NL80211_CHAN_WIDTH_320:
10839 		return WMI_CHAN_WIDTH_320;
10840 	default:
10841 		WARN_ON(1);
10842 		return WMI_CHAN_WIDTH_20;
10843 	}
10844 }
10845 
ath12k_mac_update_peer_puncturing_width(struct ath12k * ar,struct ath12k_link_vif * arvif,struct cfg80211_chan_def def)10846 static int ath12k_mac_update_peer_puncturing_width(struct ath12k *ar,
10847 						   struct ath12k_link_vif *arvif,
10848 						   struct cfg80211_chan_def def)
10849 {
10850 	u32 param_id, param_value;
10851 	int ret;
10852 
10853 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
10854 		return 0;
10855 
10856 	param_id = WMI_PEER_CHWIDTH_PUNCTURE_20MHZ_BITMAP;
10857 	param_value = ath12k_mac_nlwidth_to_wmiwidth(def.width) |
10858 		u32_encode_bits((~def.punctured),
10859 				WMI_PEER_PUNCTURE_BITMAP);
10860 
10861 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
10862 		   "punctured bitmap %02x width %d vdev %d\n",
10863 		   def.punctured, def.width, arvif->vdev_id);
10864 
10865 	ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
10866 					arvif->vdev_id, param_id,
10867 					param_value);
10868 
10869 	return ret;
10870 }
10871 
10872 static void
ath12k_mac_update_vif_chan(struct ath12k * ar,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs)10873 ath12k_mac_update_vif_chan(struct ath12k *ar,
10874 			   struct ieee80211_vif_chanctx_switch *vifs,
10875 			   int n_vifs)
10876 {
10877 	struct ath12k_wmi_vdev_up_params params = {};
10878 	struct ath12k_link_vif *arvif;
10879 	struct ieee80211_bss_conf *link_conf;
10880 	struct ath12k_base *ab = ar->ab;
10881 	struct ieee80211_vif *vif;
10882 	struct ath12k_vif *ahvif;
10883 	u8 link_id;
10884 	int ret;
10885 	int i;
10886 	bool monitor_vif = false;
10887 
10888 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10889 
10890 	for (i = 0; i < n_vifs; i++) {
10891 		vif = vifs[i].vif;
10892 		ahvif = ath12k_vif_to_ahvif(vif);
10893 		link_conf = vifs[i].link_conf;
10894 		link_id = link_conf->link_id;
10895 		arvif = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
10896 					  ahvif->link[link_id]);
10897 
10898 		if (vif->type == NL80211_IFTYPE_MONITOR) {
10899 			monitor_vif = true;
10900 			continue;
10901 		}
10902 
10903 		ath12k_dbg(ab, ATH12K_DBG_MAC,
10904 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
10905 			   arvif->vdev_id,
10906 			   vifs[i].old_ctx->def.chan->center_freq,
10907 			   vifs[i].new_ctx->def.chan->center_freq,
10908 			   vifs[i].old_ctx->def.width,
10909 			   vifs[i].new_ctx->def.width);
10910 
10911 		if (WARN_ON(!arvif->is_started))
10912 			continue;
10913 
10914 		arvif->punct_bitmap = vifs[i].new_ctx->def.punctured;
10915 
10916 		/* Firmware expect vdev_restart only if vdev is up.
10917 		 * If vdev is down then it expect vdev_stop->vdev_start.
10918 		 */
10919 		if (arvif->is_up) {
10920 			ret = ath12k_mac_vdev_restart(arvif, vifs[i].new_ctx);
10921 			if (ret) {
10922 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
10923 					    arvif->vdev_id, ret);
10924 				continue;
10925 			}
10926 		} else {
10927 			ret = ath12k_mac_vdev_stop(arvif);
10928 			if (ret) {
10929 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
10930 					    arvif->vdev_id, ret);
10931 				continue;
10932 			}
10933 
10934 			ret = ath12k_mac_vdev_start(arvif, vifs[i].new_ctx);
10935 			if (ret)
10936 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
10937 					    arvif->vdev_id, ret);
10938 			continue;
10939 		}
10940 
10941 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
10942 		if (ret)
10943 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
10944 				    ret);
10945 
10946 		memset(&params, 0, sizeof(params));
10947 		params.vdev_id = arvif->vdev_id;
10948 		params.aid = ahvif->aid;
10949 		params.bssid = arvif->bssid;
10950 		params.tx_bssid = ath12k_mac_get_tx_bssid(arvif);
10951 		if (params.tx_bssid) {
10952 			params.nontx_profile_idx = link_conf->bssid_index;
10953 			params.nontx_profile_cnt = 1 << link_conf->bssid_indicator;
10954 		}
10955 		ret = ath12k_wmi_vdev_up(arvif->ar, &params);
10956 		if (ret) {
10957 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
10958 				    arvif->vdev_id, ret);
10959 			continue;
10960 		}
10961 
10962 		ret = ath12k_mac_update_peer_puncturing_width(arvif->ar, arvif,
10963 							      vifs[i].new_ctx->def);
10964 		if (ret) {
10965 			ath12k_warn(ar->ab,
10966 				    "failed to update puncturing bitmap %02x and width %d: %d\n",
10967 				    vifs[i].new_ctx->def.punctured,
10968 				    vifs[i].new_ctx->def.width, ret);
10969 			continue;
10970 		}
10971 	}
10972 
10973 	/* Restart the internal monitor vdev on new channel */
10974 	if (!monitor_vif && ar->monitor_vdev_created) {
10975 		if (!ath12k_mac_monitor_stop(ar))
10976 			ath12k_mac_monitor_start(ar);
10977 	}
10978 }
10979 
10980 static void
ath12k_mac_update_active_vif_chan(struct ath12k * ar,struct ieee80211_chanctx_conf * ctx)10981 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
10982 				  struct ieee80211_chanctx_conf *ctx)
10983 {
10984 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx, .ar = ar };
10985 	struct ieee80211_hw *hw = ath12k_ar_to_hw(ar);
10986 
10987 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
10988 
10989 	ieee80211_iterate_active_interfaces_atomic(hw,
10990 						   IEEE80211_IFACE_ITER_NORMAL,
10991 						   ath12k_mac_change_chanctx_cnt_iter,
10992 						   &arg);
10993 	if (arg.n_vifs == 0)
10994 		return;
10995 
10996 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
10997 	if (!arg.vifs)
10998 		return;
10999 
11000 	ieee80211_iterate_active_interfaces_atomic(hw,
11001 						   IEEE80211_IFACE_ITER_NORMAL,
11002 						   ath12k_mac_change_chanctx_fill_iter,
11003 						   &arg);
11004 
11005 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
11006 
11007 	kfree(arg.vifs);
11008 }
11009 
ath12k_mac_op_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)11010 static void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
11011 					 struct ieee80211_chanctx_conf *ctx,
11012 					 u32 changed)
11013 {
11014 	struct ath12k *ar;
11015 	struct ath12k_base *ab;
11016 
11017 	lockdep_assert_wiphy(hw->wiphy);
11018 
11019 	ar = ath12k_get_ar_by_ctx(hw, ctx);
11020 	if (!ar)
11021 		return;
11022 
11023 	ab = ar->ab;
11024 
11025 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11026 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
11027 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
11028 
11029 	/* This shouldn't really happen because channel switching should use
11030 	 * switch_vif_chanctx().
11031 	 */
11032 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
11033 		return;
11034 
11035 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH ||
11036 	    changed & IEEE80211_CHANCTX_CHANGE_RADAR ||
11037 	    changed & IEEE80211_CHANCTX_CHANGE_PUNCTURING)
11038 		ath12k_mac_update_active_vif_chan(ar, ctx);
11039 
11040 	/* TODO: Recalc radar detection */
11041 }
11042 
ath12k_start_vdev_delay(struct ath12k * ar,struct ath12k_link_vif * arvif)11043 static int ath12k_start_vdev_delay(struct ath12k *ar,
11044 				   struct ath12k_link_vif *arvif)
11045 {
11046 	struct ath12k_base *ab = ar->ab;
11047 	struct ath12k_vif *ahvif = arvif->ahvif;
11048 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
11049 	struct ieee80211_chanctx_conf *chanctx;
11050 	struct ieee80211_bss_conf *link_conf;
11051 	int ret;
11052 
11053 	if (WARN_ON(arvif->is_started))
11054 		return -EBUSY;
11055 
11056 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
11057 	if (!link_conf) {
11058 		ath12k_warn(ab, "failed to get link conf for vdev %u\n", arvif->vdev_id);
11059 		return -EINVAL;
11060 	}
11061 
11062 	chanctx	= wiphy_dereference(ath12k_ar_to_hw(arvif->ar)->wiphy,
11063 				    link_conf->chanctx_conf);
11064 	ret = ath12k_mac_vdev_start(arvif, chanctx);
11065 	if (ret) {
11066 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11067 			    arvif->vdev_id, vif->addr,
11068 			    chanctx->def.chan->center_freq, ret);
11069 		return ret;
11070 	}
11071 
11072 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11073 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
11074 		if (ret) {
11075 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
11076 			return ret;
11077 		}
11078 	}
11079 
11080 	arvif->is_started = true;
11081 
11082 	/* TODO: Setup ps and cts/rts protection */
11083 	return 0;
11084 }
11085 
ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def * chan_def)11086 static u8 ath12k_mac_get_num_pwr_levels(struct cfg80211_chan_def *chan_def)
11087 {
11088 	if (chan_def->chan->flags & IEEE80211_CHAN_PSD) {
11089 		switch (chan_def->width) {
11090 		case NL80211_CHAN_WIDTH_20:
11091 			return 1;
11092 		case NL80211_CHAN_WIDTH_40:
11093 			return 2;
11094 		case NL80211_CHAN_WIDTH_80:
11095 			return 4;
11096 		case NL80211_CHAN_WIDTH_160:
11097 			return 8;
11098 		case NL80211_CHAN_WIDTH_320:
11099 			return 16;
11100 		default:
11101 			return 1;
11102 		}
11103 	} else {
11104 		switch (chan_def->width) {
11105 		case NL80211_CHAN_WIDTH_20:
11106 			return 1;
11107 		case NL80211_CHAN_WIDTH_40:
11108 			return 2;
11109 		case NL80211_CHAN_WIDTH_80:
11110 			return 3;
11111 		case NL80211_CHAN_WIDTH_160:
11112 			return 4;
11113 		case NL80211_CHAN_WIDTH_320:
11114 			return 5;
11115 		default:
11116 			return 1;
11117 		}
11118 	}
11119 }
11120 
ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def * chan_def)11121 static u16 ath12k_mac_get_6ghz_start_frequency(struct cfg80211_chan_def *chan_def)
11122 {
11123 	u16 diff_seq;
11124 
11125 	/* It is to get the lowest channel number's center frequency of the chan.
11126 	 * For example,
11127 	 * bandwidth=40 MHz, center frequency is 5965, lowest channel is 1
11128 	 * with center frequency 5955, its diff is 5965 - 5955 = 10.
11129 	 * bandwidth=80 MHz, center frequency is 5985, lowest channel is 1
11130 	 * with center frequency 5955, its diff is 5985 - 5955 = 30.
11131 	 * bandwidth=160 MHz, center frequency is 6025, lowest channel is 1
11132 	 * with center frequency 5955, its diff is 6025 - 5955 = 70.
11133 	 * bandwidth=320 MHz, center frequency is 6105, lowest channel is 1
11134 	 * with center frequency 5955, its diff is 6105 - 5955 = 70.
11135 	 */
11136 	switch (chan_def->width) {
11137 	case NL80211_CHAN_WIDTH_320:
11138 		diff_seq = 150;
11139 		break;
11140 	case NL80211_CHAN_WIDTH_160:
11141 		diff_seq = 70;
11142 		break;
11143 	case NL80211_CHAN_WIDTH_80:
11144 		diff_seq = 30;
11145 		break;
11146 	case NL80211_CHAN_WIDTH_40:
11147 		diff_seq = 10;
11148 		break;
11149 	default:
11150 		diff_seq = 0;
11151 	}
11152 
11153 	return chan_def->center_freq1 - diff_seq;
11154 }
11155 
ath12k_mac_get_seg_freq(struct cfg80211_chan_def * chan_def,u16 start_seq,u8 seq)11156 static u16 ath12k_mac_get_seg_freq(struct cfg80211_chan_def *chan_def,
11157 				   u16 start_seq, u8 seq)
11158 {
11159 	u16 seg_seq;
11160 
11161 	/* It is to get the center frequency of the specific bandwidth.
11162 	 * start_seq means the lowest channel number's center frequency.
11163 	 * seq 0/1/2/3 means 20 MHz/40 MHz/80 MHz/160 MHz.
11164 	 * For example,
11165 	 * lowest channel is 1, its center frequency 5955,
11166 	 * center frequency is 5955 when bandwidth=20 MHz, its diff is 5955 - 5955 = 0.
11167 	 * lowest channel is 1, its center frequency 5955,
11168 	 * center frequency is 5965 when bandwidth=40 MHz, its diff is 5965 - 5955 = 10.
11169 	 * lowest channel is 1, its center frequency 5955,
11170 	 * center frequency is 5985 when bandwidth=80 MHz, its diff is 5985 - 5955 = 30.
11171 	 * lowest channel is 1, its center frequency 5955,
11172 	 * center frequency is 6025 when bandwidth=160 MHz, its diff is 6025 - 5955 = 70.
11173 	 */
11174 	seg_seq = 10 * (BIT(seq) - 1);
11175 	return seg_seq + start_seq;
11176 }
11177 
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)11178 static void ath12k_mac_get_psd_channel(struct ath12k *ar,
11179 				       u16 step_freq,
11180 				       u16 *start_freq,
11181 				       u16 *center_freq,
11182 				       u8 i,
11183 				       struct ieee80211_channel **temp_chan,
11184 				       s8 *tx_power)
11185 {
11186 	/* It is to get the center frequency for each 20 MHz.
11187 	 * For example, if the chan is 160 MHz and center frequency is 6025,
11188 	 * then it include 8 channels, they are 1/5/9/13/17/21/25/29,
11189 	 * channel number 1's center frequency is 5955, it is parameter start_freq.
11190 	 * parameter i is the step of the 8 channels. i is 0~7 for the 8 channels.
11191 	 * the channel 1/5/9/13/17/21/25/29 maps i=0/1/2/3/4/5/6/7,
11192 	 * and maps its center frequency is 5955/5975/5995/6015/6035/6055/6075/6095,
11193 	 * the gap is 20 for each channel, parameter step_freq means the gap.
11194 	 * after get the center frequency of each channel, it is easy to find the
11195 	 * struct ieee80211_channel of it and get the max_reg_power.
11196 	 */
11197 	*center_freq = *start_freq + i * step_freq;
11198 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11199 	*tx_power = (*temp_chan)->max_reg_power;
11200 }
11201 
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)11202 static void ath12k_mac_get_eirp_power(struct ath12k *ar,
11203 				      u16 *start_freq,
11204 				      u16 *center_freq,
11205 				      u8 i,
11206 				      struct ieee80211_channel **temp_chan,
11207 				      struct cfg80211_chan_def *def,
11208 				      s8 *tx_power)
11209 {
11210 	/* It is to get the center frequency for 20 MHz/40 MHz/80 MHz/
11211 	 * 160 MHz bandwidth, and then plus 10 to the center frequency,
11212 	 * it is the center frequency of a channel number.
11213 	 * For example, when configured channel number is 1.
11214 	 * center frequency is 5965 when bandwidth=40 MHz, after plus 10, it is 5975,
11215 	 * then it is channel number 5.
11216 	 * center frequency is 5985 when bandwidth=80 MHz, after plus 10, it is 5995,
11217 	 * then it is channel number 9.
11218 	 * center frequency is 6025 when bandwidth=160 MHz, after plus 10, it is 6035,
11219 	 * then it is channel number 17.
11220 	 * after get the center frequency of each channel, it is easy to find the
11221 	 * struct ieee80211_channel of it and get the max_reg_power.
11222 	 */
11223 	*center_freq = ath12k_mac_get_seg_freq(def, *start_freq, i);
11224 
11225 	/* For the 20 MHz, its center frequency is same with same channel */
11226 	if (i != 0)
11227 		*center_freq += 10;
11228 
11229 	*temp_chan = ieee80211_get_channel(ar->ah->hw->wiphy, *center_freq);
11230 	*tx_power = (*temp_chan)->max_reg_power;
11231 }
11232 
ath12k_mac_fill_reg_tpc_info(struct ath12k * ar,struct ath12k_link_vif * arvif,struct ieee80211_chanctx_conf * ctx)11233 void ath12k_mac_fill_reg_tpc_info(struct ath12k *ar,
11234 				  struct ath12k_link_vif *arvif,
11235 				  struct ieee80211_chanctx_conf *ctx)
11236 {
11237 	struct ath12k_base *ab = ar->ab;
11238 	struct ath12k_reg_tpc_power_info *reg_tpc_info = &arvif->reg_tpc_info;
11239 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11240 	struct ieee80211_channel *chan, *temp_chan;
11241 	u8 pwr_lvl_idx, num_pwr_levels, pwr_reduction;
11242 	bool is_psd_power = false, is_tpe_present = false;
11243 	s8 max_tx_power[ATH12K_NUM_PWR_LEVELS],
11244 		psd_power, tx_power, eirp_power;
11245 	struct ath12k_vif *ahvif = arvif->ahvif;
11246 	u16 start_freq, center_freq;
11247 	u8 reg_6ghz_power_mode;
11248 
11249 	chan = ctx->def.chan;
11250 	start_freq = ath12k_mac_get_6ghz_start_frequency(&ctx->def);
11251 	pwr_reduction = bss_conf->pwr_reduction;
11252 
11253 	if (arvif->reg_tpc_info.num_pwr_levels) {
11254 		is_tpe_present = true;
11255 		num_pwr_levels = arvif->reg_tpc_info.num_pwr_levels;
11256 	} else {
11257 		num_pwr_levels = ath12k_mac_get_num_pwr_levels(&ctx->def);
11258 	}
11259 
11260 	for (pwr_lvl_idx = 0; pwr_lvl_idx < num_pwr_levels; pwr_lvl_idx++) {
11261 		/* STA received TPE IE*/
11262 		if (is_tpe_present) {
11263 			/* local power is PSD power*/
11264 			if (chan->flags & IEEE80211_CHAN_PSD) {
11265 				/* Connecting AP is psd power */
11266 				if (reg_tpc_info->is_psd_power) {
11267 					is_psd_power = true;
11268 					ath12k_mac_get_psd_channel(ar, 20,
11269 								   &start_freq,
11270 								   &center_freq,
11271 								   pwr_lvl_idx,
11272 								   &temp_chan,
11273 								   &tx_power);
11274 					psd_power = temp_chan->psd;
11275 					eirp_power = tx_power;
11276 					max_tx_power[pwr_lvl_idx] =
11277 						min_t(s8,
11278 						      psd_power,
11279 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11280 				/* Connecting AP is not psd power */
11281 				} else {
11282 					ath12k_mac_get_eirp_power(ar,
11283 								  &start_freq,
11284 								  &center_freq,
11285 								  pwr_lvl_idx,
11286 								  &temp_chan,
11287 								  &ctx->def,
11288 								  &tx_power);
11289 					psd_power = temp_chan->psd;
11290 					/* convert psd power to EIRP power based
11291 					 * on channel width
11292 					 */
11293 					tx_power =
11294 						min_t(s8, tx_power,
11295 						      psd_power + 13 + pwr_lvl_idx * 3);
11296 					max_tx_power[pwr_lvl_idx] =
11297 						min_t(s8,
11298 						      tx_power,
11299 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11300 				}
11301 			/* local power is not PSD power */
11302 			} else {
11303 				/* Connecting AP is psd power */
11304 				if (reg_tpc_info->is_psd_power) {
11305 					is_psd_power = true;
11306 					ath12k_mac_get_psd_channel(ar, 20,
11307 								   &start_freq,
11308 								   &center_freq,
11309 								   pwr_lvl_idx,
11310 								   &temp_chan,
11311 								   &tx_power);
11312 					eirp_power = tx_power;
11313 					max_tx_power[pwr_lvl_idx] =
11314 						reg_tpc_info->tpe[pwr_lvl_idx];
11315 				/* Connecting AP is not psd power */
11316 				} else {
11317 					ath12k_mac_get_eirp_power(ar,
11318 								  &start_freq,
11319 								  &center_freq,
11320 								  pwr_lvl_idx,
11321 								  &temp_chan,
11322 								  &ctx->def,
11323 								  &tx_power);
11324 					max_tx_power[pwr_lvl_idx] =
11325 						min_t(s8,
11326 						      tx_power,
11327 						      reg_tpc_info->tpe[pwr_lvl_idx]);
11328 				}
11329 			}
11330 		/* STA not received TPE IE */
11331 		} else {
11332 			/* local power is PSD power*/
11333 			if (chan->flags & IEEE80211_CHAN_PSD) {
11334 				is_psd_power = true;
11335 				ath12k_mac_get_psd_channel(ar, 20,
11336 							   &start_freq,
11337 							   &center_freq,
11338 							   pwr_lvl_idx,
11339 							   &temp_chan,
11340 							   &tx_power);
11341 				psd_power = temp_chan->psd;
11342 				eirp_power = tx_power;
11343 				max_tx_power[pwr_lvl_idx] = psd_power;
11344 			} else {
11345 				ath12k_mac_get_eirp_power(ar,
11346 							  &start_freq,
11347 							  &center_freq,
11348 							  pwr_lvl_idx,
11349 							  &temp_chan,
11350 							  &ctx->def,
11351 							  &tx_power);
11352 				max_tx_power[pwr_lvl_idx] = tx_power;
11353 			}
11354 		}
11355 
11356 		if (is_psd_power) {
11357 			/* If AP local power constraint is present */
11358 			if (pwr_reduction)
11359 				eirp_power = eirp_power - pwr_reduction;
11360 
11361 			/* If firmware updated max tx power is non zero, then take
11362 			 * the min of firmware updated ap tx power
11363 			 * and max power derived from above mentioned parameters.
11364 			 */
11365 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11366 				   "eirp power : %d firmware report power : %d\n",
11367 				   eirp_power, ar->max_allowed_tx_power);
11368 			/* Firmware reports lower max_allowed_tx_power during vdev
11369 			 * start response. In case of 6 GHz, firmware is not aware
11370 			 * of EIRP power unless driver sets EIRP power through WMI
11371 			 * TPC command. So radio which does not support idle power
11372 			 * save can set maximum calculated EIRP power directly to
11373 			 * firmware through TPC command without min comparison with
11374 			 * vdev start response's max_allowed_tx_power.
11375 			 */
11376 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11377 				eirp_power = min_t(s8,
11378 						   eirp_power,
11379 						   ar->max_allowed_tx_power);
11380 		} else {
11381 			/* If AP local power constraint is present */
11382 			if (pwr_reduction)
11383 				max_tx_power[pwr_lvl_idx] =
11384 					max_tx_power[pwr_lvl_idx] - pwr_reduction;
11385 			/* If firmware updated max tx power is non zero, then take
11386 			 * the min of firmware updated ap tx power
11387 			 * and max power derived from above mentioned parameters.
11388 			 */
11389 			if (ar->max_allowed_tx_power && ab->hw_params->idle_ps)
11390 				max_tx_power[pwr_lvl_idx] =
11391 					min_t(s8,
11392 					      max_tx_power[pwr_lvl_idx],
11393 					      ar->max_allowed_tx_power);
11394 		}
11395 		reg_tpc_info->chan_power_info[pwr_lvl_idx].chan_cfreq = center_freq;
11396 		reg_tpc_info->chan_power_info[pwr_lvl_idx].tx_power =
11397 			max_tx_power[pwr_lvl_idx];
11398 	}
11399 
11400 	reg_tpc_info->num_pwr_levels = num_pwr_levels;
11401 	reg_tpc_info->is_psd_power = is_psd_power;
11402 	reg_tpc_info->eirp_power = eirp_power;
11403 	if (ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11404 		reg_6ghz_power_mode = bss_conf->power_type;
11405 	else
11406 		/* For now, LPI is the only supported AP power mode */
11407 		reg_6ghz_power_mode = IEEE80211_REG_LPI_AP;
11408 
11409 	reg_tpc_info->ap_power_type =
11410 		ath12k_reg_ap_pwr_convert(reg_6ghz_power_mode);
11411 }
11412 
ath12k_mac_parse_tx_pwr_env(struct ath12k * ar,struct ath12k_link_vif * arvif)11413 static void ath12k_mac_parse_tx_pwr_env(struct ath12k *ar,
11414 					struct ath12k_link_vif *arvif)
11415 {
11416 	struct ieee80211_bss_conf *bss_conf = ath12k_mac_get_link_bss_conf(arvif);
11417 	struct ath12k_reg_tpc_power_info *tpc_info = &arvif->reg_tpc_info;
11418 	struct ieee80211_parsed_tpe_eirp *local_non_psd, *reg_non_psd;
11419 	struct ieee80211_parsed_tpe_psd *local_psd, *reg_psd;
11420 	struct ieee80211_parsed_tpe *tpe = &bss_conf->tpe;
11421 	enum wmi_reg_6g_client_type client_type;
11422 	struct ath12k_reg_info *reg_info;
11423 	struct ath12k_base *ab = ar->ab;
11424 	bool psd_valid, non_psd_valid;
11425 	int i;
11426 
11427 	reg_info = ab->reg_info[ar->pdev_idx];
11428 	client_type = reg_info->client_type;
11429 
11430 	local_psd = &tpe->psd_local[client_type];
11431 	reg_psd = &tpe->psd_reg_client[client_type];
11432 	local_non_psd = &tpe->max_local[client_type];
11433 	reg_non_psd = &tpe->max_reg_client[client_type];
11434 
11435 	psd_valid = local_psd->valid | reg_psd->valid;
11436 	non_psd_valid = local_non_psd->valid | reg_non_psd->valid;
11437 
11438 	if (!psd_valid && !non_psd_valid) {
11439 		ath12k_warn(ab,
11440 			    "no transmit power envelope match client power type %d\n",
11441 			    client_type);
11442 		return;
11443 	}
11444 
11445 	if (psd_valid) {
11446 		tpc_info->is_psd_power = true;
11447 
11448 		tpc_info->num_pwr_levels = max(local_psd->count,
11449 					       reg_psd->count);
11450 		if (tpc_info->num_pwr_levels > ATH12K_NUM_PWR_LEVELS)
11451 			tpc_info->num_pwr_levels = ATH12K_NUM_PWR_LEVELS;
11452 
11453 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
11454 			tpc_info->tpe[i] = min(local_psd->power[i],
11455 					       reg_psd->power[i]) / 2;
11456 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11457 				   "TPE PSD power[%d] : %d\n",
11458 				   i, tpc_info->tpe[i]);
11459 		}
11460 	} else {
11461 		tpc_info->is_psd_power = false;
11462 		tpc_info->eirp_power = 0;
11463 
11464 		tpc_info->num_pwr_levels = max(local_non_psd->count,
11465 					       reg_non_psd->count);
11466 		if (tpc_info->num_pwr_levels > ATH12K_NUM_PWR_LEVELS)
11467 			tpc_info->num_pwr_levels = ATH12K_NUM_PWR_LEVELS;
11468 
11469 		for (i = 0; i < tpc_info->num_pwr_levels; i++) {
11470 			tpc_info->tpe[i] = min(local_non_psd->power[i],
11471 					       reg_non_psd->power[i]) / 2;
11472 			ath12k_dbg(ab, ATH12K_DBG_MAC,
11473 				   "non PSD power[%d] : %d\n",
11474 				   i, tpc_info->tpe[i]);
11475 		}
11476 	}
11477 }
11478 
11479 static 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)11480 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
11481 				 struct ieee80211_vif *vif,
11482 				 struct ieee80211_bss_conf *link_conf,
11483 				 struct ieee80211_chanctx_conf *ctx)
11484 {
11485 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11486 	struct ath12k *ar;
11487 	struct ath12k_base *ab;
11488 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11489 	u8 link_id = link_conf->link_id;
11490 	struct ath12k_link_vif *arvif;
11491 	int ret;
11492 
11493 	lockdep_assert_wiphy(hw->wiphy);
11494 
11495 	/* For multi radio wiphy, the vdev was not created during add_interface
11496 	 * create now since we have a channel ctx now to assign to a specific ar/fw
11497 	 */
11498 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
11499 	if (!arvif) {
11500 		WARN_ON(1);
11501 		return -ENOMEM;
11502 	}
11503 
11504 	ar = ath12k_mac_assign_vif_to_vdev(hw, arvif, ctx);
11505 	if (!ar) {
11506 		ath12k_hw_warn(ah, "failed to assign chanctx for vif %pM link id %u link vif is already started",
11507 			       vif->addr, link_id);
11508 		return -EINVAL;
11509 	}
11510 
11511 	ab = ar->ab;
11512 
11513 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11514 		   "mac chanctx assign ptr %p vdev_id %i\n",
11515 		   ctx, arvif->vdev_id);
11516 
11517 	if (ath12k_wmi_supports_6ghz_cc_ext(ar) &&
11518 	    ctx->def.chan->band == NL80211_BAND_6GHZ &&
11519 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA)
11520 		ath12k_mac_parse_tx_pwr_env(ar, arvif);
11521 
11522 	arvif->punct_bitmap = ctx->def.punctured;
11523 
11524 	/* for some targets bss peer must be created before vdev_start */
11525 	if (ab->hw_params->vdev_start_delay &&
11526 	    ahvif->vdev_type != WMI_VDEV_TYPE_AP &&
11527 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
11528 	    !ath12k_peer_exist_by_vdev_id(ab, arvif->vdev_id)) {
11529 		ret = 0;
11530 		goto out;
11531 	}
11532 
11533 	if (WARN_ON(arvif->is_started)) {
11534 		ret = -EBUSY;
11535 		goto out;
11536 	}
11537 
11538 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11539 		ret = ath12k_mac_monitor_start(ar);
11540 		if (ret) {
11541 			ath12k_mac_monitor_vdev_delete(ar);
11542 			goto out;
11543 		}
11544 
11545 		arvif->is_started = true;
11546 		goto out;
11547 	}
11548 
11549 	ret = ath12k_mac_vdev_start(arvif, ctx);
11550 	if (ret) {
11551 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
11552 			    arvif->vdev_id, vif->addr,
11553 			    ctx->def.chan->center_freq, ret);
11554 		goto out;
11555 	}
11556 
11557 	arvif->is_started = true;
11558 
11559 	/* TODO: Setup ps and cts/rts protection */
11560 
11561 out:
11562 	return ret;
11563 }
11564 
11565 static 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)11566 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
11567 				   struct ieee80211_vif *vif,
11568 				   struct ieee80211_bss_conf *link_conf,
11569 				   struct ieee80211_chanctx_conf *ctx)
11570 {
11571 	struct ath12k *ar;
11572 	struct ath12k_base *ab;
11573 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
11574 	struct ath12k_link_vif *arvif;
11575 	u8 link_id = link_conf->link_id;
11576 	int ret;
11577 
11578 	lockdep_assert_wiphy(hw->wiphy);
11579 
11580 	arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
11581 
11582 	/* The vif is expected to be attached to an ar's VDEV.
11583 	 * We leave the vif/vdev in this function as is
11584 	 * and not delete the vdev symmetric to assign_vif_chanctx()
11585 	 * the VDEV will be deleted and unassigned either during
11586 	 * remove_interface() or when there is a change in channel
11587 	 * that moves the vif to a new ar
11588 	 */
11589 	if (!arvif || !arvif->is_created)
11590 		return;
11591 
11592 	ar = arvif->ar;
11593 	ab = ar->ab;
11594 
11595 	ath12k_dbg(ab, ATH12K_DBG_MAC,
11596 		   "mac chanctx unassign ptr %p vdev_id %i\n",
11597 		   ctx, arvif->vdev_id);
11598 
11599 	WARN_ON(!arvif->is_started);
11600 
11601 	if (ahvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
11602 		ret = ath12k_mac_monitor_stop(ar);
11603 		if (ret)
11604 			return;
11605 
11606 		arvif->is_started = false;
11607 	}
11608 
11609 	if (ahvif->vdev_type != WMI_VDEV_TYPE_STA &&
11610 	    ahvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
11611 		ath12k_bss_disassoc(ar, arvif);
11612 		ret = ath12k_mac_vdev_stop(arvif);
11613 		if (ret)
11614 			ath12k_warn(ab, "failed to stop vdev %i: %d\n",
11615 				    arvif->vdev_id, ret);
11616 	}
11617 	arvif->is_started = false;
11618 
11619 	if (test_bit(WMI_TLV_SERVICE_11D_OFFLOAD, ab->wmi_ab.svc_map) &&
11620 	    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
11621 	    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE &&
11622 	    ar->state_11d != ATH12K_11D_PREPARING) {
11623 		reinit_completion(&ar->completed_11d_scan);
11624 		ar->state_11d = ATH12K_11D_PREPARING;
11625 	}
11626 
11627 	if (ar->scan.arvif == arvif && ar->scan.state == ATH12K_SCAN_RUNNING) {
11628 		ath12k_scan_abort(ar);
11629 		ar->scan.arvif = NULL;
11630 	}
11631 }
11632 
11633 static 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)11634 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
11635 				 struct ieee80211_vif_chanctx_switch *vifs,
11636 				 int n_vifs,
11637 				 enum ieee80211_chanctx_switch_mode mode)
11638 {
11639 	struct ath12k *ar;
11640 
11641 	lockdep_assert_wiphy(hw->wiphy);
11642 
11643 	ar = ath12k_get_ar_by_ctx(hw, vifs->old_ctx);
11644 	if (!ar)
11645 		return -EINVAL;
11646 
11647 	/* Switching channels across radio is not allowed */
11648 	if (ar != ath12k_get_ar_by_ctx(hw, vifs->new_ctx))
11649 		return -EINVAL;
11650 
11651 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
11652 		   "mac chanctx switch n_vifs %d mode %d\n",
11653 		   n_vifs, mode);
11654 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
11655 
11656 	return 0;
11657 }
11658 
11659 static int
ath12k_set_vdev_param_to_all_vifs(struct ath12k * ar,int param,u32 value)11660 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
11661 {
11662 	struct ath12k_link_vif *arvif;
11663 	int ret = 0;
11664 
11665 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11666 
11667 	list_for_each_entry(arvif, &ar->arvifs, list) {
11668 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
11669 			   param, arvif->vdev_id, value);
11670 
11671 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
11672 						    param, value);
11673 		if (ret) {
11674 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
11675 				    param, arvif->vdev_id, ret);
11676 			break;
11677 		}
11678 	}
11679 
11680 	return ret;
11681 }
11682 
11683 /* mac80211 stores device specific RTS/Fragmentation threshold value,
11684  * this is set interface specific to firmware from ath12k driver
11685  */
ath12k_mac_op_set_rts_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)11686 static int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw,
11687 					   int radio_idx, u32 value)
11688 {
11689 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11690 	struct ath12k *ar;
11691 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD, ret = 0, i;
11692 
11693 	lockdep_assert_wiphy(hw->wiphy);
11694 
11695 	/* Currently we set the rts threshold value to all the vifs across
11696 	 * all radios of the single wiphy.
11697 	 * TODO Once support for vif specific RTS threshold in mac80211 is
11698 	 * available, ath12k can make use of it.
11699 	 */
11700 	for_each_ar(ah, ar, i) {
11701 		ret = ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
11702 		if (ret) {
11703 			ath12k_warn(ar->ab, "failed to set RTS config for all vdevs of pdev %d",
11704 				    ar->pdev->pdev_id);
11705 			break;
11706 		}
11707 	}
11708 
11709 	return ret;
11710 }
11711 
ath12k_mac_op_set_frag_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)11712 static int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
11713 					    int radio_idx, u32 value)
11714 {
11715 	/* Even though there's a WMI vdev param for fragmentation threshold no
11716 	 * known firmware actually implements it. Moreover it is not possible to
11717 	 * rely frame fragmentation to mac80211 because firmware clears the
11718 	 * "more fragments" bit in frame control making it impossible for remote
11719 	 * devices to reassemble frames.
11720 	 *
11721 	 * Hence implement a dummy callback just to say fragmentation isn't
11722 	 * supported. This effectively prevents mac80211 from doing frame
11723 	 * fragmentation in software.
11724 	 */
11725 
11726 	lockdep_assert_wiphy(hw->wiphy);
11727 
11728 	return -EOPNOTSUPP;
11729 }
11730 
ath12k_mac_flush(struct ath12k * ar)11731 static int ath12k_mac_flush(struct ath12k *ar)
11732 {
11733 	long time_left;
11734 	int ret = 0;
11735 
11736 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
11737 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
11738 				       ATH12K_FLUSH_TIMEOUT);
11739 	if (time_left == 0) {
11740 		ath12k_warn(ar->ab,
11741 			    "failed to flush transmit queue, data pkts pending %d\n",
11742 			    atomic_read(&ar->dp.num_tx_pending));
11743 		ret = -ETIMEDOUT;
11744 	}
11745 
11746 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
11747 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
11748 				       ATH12K_FLUSH_TIMEOUT);
11749 	if (time_left == 0) {
11750 		ath12k_warn(ar->ab,
11751 			    "failed to flush mgmt transmit queue, mgmt pkts pending %d\n",
11752 			    atomic_read(&ar->num_pending_mgmt_tx));
11753 		ret = -ETIMEDOUT;
11754 	}
11755 
11756 	return ret;
11757 }
11758 
ath12k_mac_wait_tx_complete(struct ath12k * ar)11759 int ath12k_mac_wait_tx_complete(struct ath12k *ar)
11760 {
11761 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11762 
11763 	ath12k_mac_drain_tx(ar);
11764 	return ath12k_mac_flush(ar);
11765 }
11766 
ath12k_mac_op_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)11767 static void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
11768 				u32 queues, bool drop)
11769 {
11770 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
11771 	struct ath12k_link_vif *arvif;
11772 	struct ath12k_vif *ahvif;
11773 	unsigned long links;
11774 	struct ath12k *ar;
11775 	u8 link_id;
11776 	int i;
11777 
11778 	lockdep_assert_wiphy(hw->wiphy);
11779 
11780 	if (drop)
11781 		return;
11782 
11783 	/* vif can be NULL when flush() is considered for hw */
11784 	if (!vif) {
11785 		for_each_ar(ah, ar, i)
11786 			ath12k_mac_flush(ar);
11787 		return;
11788 	}
11789 
11790 	for_each_ar(ah, ar, i)
11791 		wiphy_work_flush(hw->wiphy, &ar->wmi_mgmt_tx_work);
11792 
11793 	ahvif = ath12k_vif_to_ahvif(vif);
11794 	links = ahvif->links_map;
11795 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
11796 		arvif = wiphy_dereference(hw->wiphy, ahvif->link[link_id]);
11797 		if (!(arvif && arvif->ar))
11798 			continue;
11799 
11800 		ath12k_mac_flush(arvif->ar);
11801 	}
11802 }
11803 
11804 static int
ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)11805 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
11806 				     enum nl80211_band band,
11807 				     const struct cfg80211_bitrate_mask *mask)
11808 {
11809 	int num_rates = 0;
11810 	int i;
11811 
11812 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
11813 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
11814 
11815 	return num_rates;
11816 }
11817 
11818 static bool
ath12k_mac_has_single_legacy_rate(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)11819 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
11820 				  enum nl80211_band band,
11821 				  const struct cfg80211_bitrate_mask *mask)
11822 {
11823 	int num_rates = 0;
11824 
11825 	num_rates = hweight32(mask->control[band].legacy);
11826 
11827 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
11828 		return false;
11829 
11830 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
11831 		return false;
11832 
11833 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask))
11834 		return false;
11835 
11836 	return num_rates == 1;
11837 }
11838 
11839 static __le16
ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap * he_cap)11840 ath12k_mac_get_tx_mcs_map(const struct ieee80211_sta_he_cap *he_cap)
11841 {
11842 	if (he_cap->he_cap_elem.phy_cap_info[0] &
11843 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
11844 		return he_cap->he_mcs_nss_supp.tx_mcs_160;
11845 
11846 	return he_cap->he_mcs_nss_supp.tx_mcs_80;
11847 }
11848 
11849 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)11850 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
11851 				       struct ieee80211_vif *vif,
11852 				       enum nl80211_band band,
11853 				       const struct cfg80211_bitrate_mask *mask,
11854 				       int *nss)
11855 {
11856 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
11857 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
11858 	const struct ieee80211_sta_he_cap *he_cap;
11859 	u16 he_mcs_map = 0;
11860 	u8 ht_nss_mask = 0;
11861 	u8 vht_nss_mask = 0;
11862 	u8 he_nss_mask = 0;
11863 	int i;
11864 
11865 	/* No need to consider legacy here. Basic rates are always present
11866 	 * in bitrate mask
11867 	 */
11868 
11869 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
11870 		if (mask->control[band].ht_mcs[i] == 0)
11871 			continue;
11872 		else if (mask->control[band].ht_mcs[i] ==
11873 			 sband->ht_cap.mcs.rx_mask[i])
11874 			ht_nss_mask |= BIT(i);
11875 		else
11876 			return false;
11877 	}
11878 
11879 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
11880 		if (mask->control[band].vht_mcs[i] == 0)
11881 			continue;
11882 		else if (mask->control[band].vht_mcs[i] ==
11883 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
11884 			vht_nss_mask |= BIT(i);
11885 		else
11886 			return false;
11887 	}
11888 
11889 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
11890 	if (!he_cap)
11891 		return false;
11892 
11893 	he_mcs_map = le16_to_cpu(ath12k_mac_get_tx_mcs_map(he_cap));
11894 
11895 	for (i = 0; i < ARRAY_SIZE(mask->control[band].he_mcs); i++) {
11896 		if (mask->control[band].he_mcs[i] == 0)
11897 			continue;
11898 
11899 		if (mask->control[band].he_mcs[i] ==
11900 		    ath12k_mac_get_max_he_mcs_map(he_mcs_map, i))
11901 			he_nss_mask |= BIT(i);
11902 		else
11903 			return false;
11904 	}
11905 
11906 	if (ht_nss_mask != vht_nss_mask || ht_nss_mask != he_nss_mask)
11907 		return false;
11908 
11909 	if (ht_nss_mask == 0)
11910 		return false;
11911 
11912 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
11913 		return false;
11914 
11915 	*nss = fls(ht_nss_mask);
11916 
11917 	return true;
11918 }
11919 
11920 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)11921 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
11922 				  enum nl80211_band band,
11923 				  const struct cfg80211_bitrate_mask *mask,
11924 				  u32 *rate, u8 *nss)
11925 {
11926 	int rate_idx;
11927 	u16 bitrate;
11928 	u8 preamble;
11929 	u8 hw_rate;
11930 
11931 	if (hweight32(mask->control[band].legacy) != 1)
11932 		return -EINVAL;
11933 
11934 	rate_idx = ffs(mask->control[band].legacy) - 1;
11935 
11936 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
11937 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
11938 
11939 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
11940 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
11941 
11942 	if (ath12k_mac_bitrate_is_cck(bitrate))
11943 		preamble = WMI_RATE_PREAMBLE_CCK;
11944 	else
11945 		preamble = WMI_RATE_PREAMBLE_OFDM;
11946 
11947 	*nss = 1;
11948 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
11949 
11950 	return 0;
11951 }
11952 
11953 static int
ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif * arvif,u8 he_gi,u8 he_ltf)11954 ath12k_mac_set_fixed_rate_gi_ltf(struct ath12k_link_vif *arvif, u8 he_gi, u8 he_ltf)
11955 {
11956 	struct ath12k *ar = arvif->ar;
11957 	int ret;
11958 
11959 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
11960 
11961 	/* 0.8 = 0, 1.6 = 2 and 3.2 = 3. */
11962 	if (he_gi && he_gi != 0xFF)
11963 		he_gi += 1;
11964 
11965 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
11966 					    WMI_VDEV_PARAM_SGI, he_gi);
11967 	if (ret) {
11968 		ath12k_warn(ar->ab, "failed to set HE GI:%d, error:%d\n",
11969 			    he_gi, ret);
11970 		return ret;
11971 	}
11972 	/* start from 1 */
11973 	if (he_ltf != 0xFF)
11974 		he_ltf += 1;
11975 
11976 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
11977 					    WMI_VDEV_PARAM_HE_LTF, he_ltf);
11978 	if (ret) {
11979 		ath12k_warn(ar->ab, "failed to set HE LTF:%d, error:%d\n",
11980 			    he_ltf, ret);
11981 		return ret;
11982 	}
11983 	return 0;
11984 }
11985 
11986 static int
ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif * arvif,u16 he_gi,u8 he_ltf)11987 ath12k_mac_set_auto_rate_gi_ltf(struct ath12k_link_vif *arvif, u16 he_gi, u8 he_ltf)
11988 {
11989 	struct ath12k *ar = arvif->ar;
11990 	int ret;
11991 	u32 he_ar_gi_ltf;
11992 
11993 	if (he_gi != 0xFF) {
11994 		switch (he_gi) {
11995 		case NL80211_RATE_INFO_HE_GI_0_8:
11996 			he_gi = WMI_AUTORATE_800NS_GI;
11997 			break;
11998 		case NL80211_RATE_INFO_HE_GI_1_6:
11999 			he_gi = WMI_AUTORATE_1600NS_GI;
12000 			break;
12001 		case NL80211_RATE_INFO_HE_GI_3_2:
12002 			he_gi = WMI_AUTORATE_3200NS_GI;
12003 			break;
12004 		default:
12005 			ath12k_warn(ar->ab, "Invalid GI\n");
12006 			return -EINVAL;
12007 		}
12008 	}
12009 
12010 	if (he_ltf != 0xFF) {
12011 		switch (he_ltf) {
12012 		case NL80211_RATE_INFO_HE_1XLTF:
12013 			he_ltf = WMI_HE_AUTORATE_LTF_1X;
12014 			break;
12015 		case NL80211_RATE_INFO_HE_2XLTF:
12016 			he_ltf = WMI_HE_AUTORATE_LTF_2X;
12017 			break;
12018 		case NL80211_RATE_INFO_HE_4XLTF:
12019 			he_ltf = WMI_HE_AUTORATE_LTF_4X;
12020 			break;
12021 		default:
12022 			ath12k_warn(ar->ab, "Invalid LTF\n");
12023 			return -EINVAL;
12024 		}
12025 	}
12026 
12027 	he_ar_gi_ltf = he_gi | he_ltf;
12028 
12029 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12030 					    WMI_VDEV_PARAM_AUTORATE_MISC_CFG,
12031 					    he_ar_gi_ltf);
12032 	if (ret) {
12033 		ath12k_warn(ar->ab,
12034 			    "failed to set HE autorate GI:%u, LTF:%u params, error:%d\n",
12035 			    he_gi, he_ltf, ret);
12036 		return ret;
12037 	}
12038 
12039 	return 0;
12040 }
12041 
ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)12042 static u32 ath12k_mac_nlgi_to_wmigi(enum nl80211_txrate_gi gi)
12043 {
12044 	switch (gi) {
12045 	case NL80211_TXRATE_DEFAULT_GI:
12046 		return WMI_GI_400_NS;
12047 	case NL80211_TXRATE_FORCE_LGI:
12048 		return WMI_GI_800_NS;
12049 	default:
12050 		return WMI_GI_400_NS;
12051 	}
12052 }
12053 
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)12054 static int ath12k_mac_set_rate_params(struct ath12k_link_vif *arvif,
12055 				      u32 rate, u8 nss, u8 sgi, u8 ldpc,
12056 				      u8 he_gi, u8 he_ltf, bool he_fixed_rate)
12057 {
12058 	struct ieee80211_bss_conf *link_conf;
12059 	struct ath12k *ar = arvif->ar;
12060 	u32 vdev_param;
12061 	u32 param_value;
12062 	int ret;
12063 	bool he_support;
12064 
12065 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12066 
12067 	link_conf = ath12k_mac_get_link_bss_conf(arvif);
12068 	if (!link_conf)
12069 		return -EINVAL;
12070 
12071 	he_support = link_conf->he_support;
12072 
12073 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12074 		   "mac set rate params vdev %i rate 0x%02x nss 0x%02x sgi 0x%02x ldpc 0x%02x\n",
12075 		   arvif->vdev_id, rate, nss, sgi, ldpc);
12076 
12077 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12078 		   "he_gi 0x%02x he_ltf 0x%02x he_fixed_rate %d\n", he_gi,
12079 		   he_ltf, he_fixed_rate);
12080 
12081 	if (!he_support) {
12082 		vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
12083 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12084 						    vdev_param, rate);
12085 		if (ret) {
12086 			ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
12087 				    rate, ret);
12088 			return ret;
12089 		}
12090 	}
12091 
12092 	vdev_param = WMI_VDEV_PARAM_NSS;
12093 
12094 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12095 					    vdev_param, nss);
12096 	if (ret) {
12097 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
12098 			    nss, ret);
12099 		return ret;
12100 	}
12101 
12102 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12103 					    WMI_VDEV_PARAM_LDPC, ldpc);
12104 	if (ret) {
12105 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
12106 			    ldpc, ret);
12107 		return ret;
12108 	}
12109 
12110 	if (he_support) {
12111 		if (he_fixed_rate)
12112 			ret = ath12k_mac_set_fixed_rate_gi_ltf(arvif, he_gi, he_ltf);
12113 		else
12114 			ret = ath12k_mac_set_auto_rate_gi_ltf(arvif, he_gi, he_ltf);
12115 		if (ret)
12116 			return ret;
12117 	} else {
12118 		vdev_param = WMI_VDEV_PARAM_SGI;
12119 		param_value = ath12k_mac_nlgi_to_wmigi(sgi);
12120 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
12121 						    vdev_param, param_value);
12122 		if (ret) {
12123 			ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
12124 				    sgi, ret);
12125 			return ret;
12126 		}
12127 	}
12128 
12129 	return 0;
12130 }
12131 
12132 static bool
ath12k_mac_vht_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12133 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
12134 				 enum nl80211_band band,
12135 				 const struct cfg80211_bitrate_mask *mask)
12136 {
12137 	int i;
12138 	u16 vht_mcs;
12139 
12140 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
12141 		vht_mcs = mask->control[band].vht_mcs[i];
12142 
12143 		switch (vht_mcs) {
12144 		case 0:
12145 		case BIT(8) - 1:
12146 		case BIT(9) - 1:
12147 		case BIT(10) - 1:
12148 			break;
12149 		default:
12150 			return false;
12151 		}
12152 	}
12153 
12154 	return true;
12155 }
12156 
12157 static bool
ath12k_mac_he_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)12158 ath12k_mac_he_mcs_range_present(struct ath12k *ar,
12159 				enum nl80211_band band,
12160 				const struct cfg80211_bitrate_mask *mask)
12161 {
12162 	int i;
12163 	u16 he_mcs;
12164 
12165 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
12166 		he_mcs = mask->control[band].he_mcs[i];
12167 
12168 		switch (he_mcs) {
12169 		case 0:
12170 		case BIT(8) - 1:
12171 		case BIT(10) - 1:
12172 		case BIT(12) - 1:
12173 			break;
12174 		default:
12175 			return false;
12176 		}
12177 	}
12178 
12179 	return true;
12180 }
12181 
ath12k_mac_set_bitrate_mask_iter(void * data,struct ieee80211_sta * sta)12182 static void ath12k_mac_set_bitrate_mask_iter(void *data,
12183 					     struct ieee80211_sta *sta)
12184 {
12185 	struct ath12k_link_vif *arvif = data;
12186 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12187 	struct ath12k_link_sta *arsta;
12188 	struct ath12k *ar = arvif->ar;
12189 
12190 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12191 
12192 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12193 				  ahsta->link[arvif->link_id]);
12194 	if (!arsta || arsta->arvif != arvif)
12195 		return;
12196 
12197 	spin_lock_bh(&ar->data_lock);
12198 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
12199 	spin_unlock_bh(&ar->data_lock);
12200 
12201 	wiphy_work_queue(ath12k_ar_to_hw(ar)->wiphy, &arsta->update_wk);
12202 }
12203 
ath12k_mac_disable_peer_fixed_rate(void * data,struct ieee80211_sta * sta)12204 static void ath12k_mac_disable_peer_fixed_rate(void *data,
12205 					       struct ieee80211_sta *sta)
12206 {
12207 	struct ath12k_link_vif *arvif = data;
12208 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12209 	struct ath12k_link_sta *arsta;
12210 	struct ath12k *ar = arvif->ar;
12211 	int ret;
12212 
12213 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12214 
12215 	arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
12216 				  ahsta->link[arvif->link_id]);
12217 
12218 	if (!arsta || arsta->arvif != arvif)
12219 		return;
12220 
12221 	ret = ath12k_wmi_set_peer_param(ar, arsta->addr,
12222 					arvif->vdev_id,
12223 					WMI_PEER_PARAM_FIXED_RATE,
12224 					WMI_FIXED_RATE_NONE);
12225 	if (ret)
12226 		ath12k_warn(ar->ab,
12227 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
12228 			    arsta->addr, ret);
12229 }
12230 
12231 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)12232 ath12k_mac_validate_fixed_rate_settings(struct ath12k *ar, enum nl80211_band band,
12233 					const struct cfg80211_bitrate_mask *mask,
12234 					unsigned int link_id)
12235 {
12236 	bool he_fixed_rate = false, vht_fixed_rate = false;
12237 	const u16 *vht_mcs_mask, *he_mcs_mask;
12238 	struct ieee80211_link_sta *link_sta;
12239 	struct ath12k_peer *peer, *tmp;
12240 	u8 vht_nss, he_nss;
12241 	int ret = true;
12242 
12243 	vht_mcs_mask = mask->control[band].vht_mcs;
12244 	he_mcs_mask = mask->control[band].he_mcs;
12245 
12246 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask) == 1)
12247 		vht_fixed_rate = true;
12248 
12249 	if (ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask) == 1)
12250 		he_fixed_rate = true;
12251 
12252 	if (!vht_fixed_rate && !he_fixed_rate)
12253 		return true;
12254 
12255 	vht_nss = ath12k_mac_max_vht_nss(vht_mcs_mask);
12256 	he_nss =  ath12k_mac_max_he_nss(he_mcs_mask);
12257 
12258 	rcu_read_lock();
12259 	spin_lock_bh(&ar->ab->base_lock);
12260 	list_for_each_entry_safe(peer, tmp, &ar->ab->peers, list) {
12261 		if (peer->sta) {
12262 			link_sta = rcu_dereference(peer->sta->link[link_id]);
12263 			if (!link_sta) {
12264 				ret = false;
12265 				goto exit;
12266 			}
12267 
12268 			if (vht_fixed_rate && (!link_sta->vht_cap.vht_supported ||
12269 					       link_sta->rx_nss < vht_nss)) {
12270 				ret = false;
12271 				goto exit;
12272 			}
12273 			if (he_fixed_rate && (!link_sta->he_cap.has_he ||
12274 					      link_sta->rx_nss < he_nss)) {
12275 				ret = false;
12276 				goto exit;
12277 			}
12278 		}
12279 	}
12280 exit:
12281 	spin_unlock_bh(&ar->ab->base_lock);
12282 	rcu_read_unlock();
12283 	return ret;
12284 }
12285 
12286 static int
ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)12287 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
12288 			       struct ieee80211_vif *vif,
12289 			       const struct cfg80211_bitrate_mask *mask)
12290 {
12291 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12292 	struct ath12k_link_vif *arvif;
12293 	struct cfg80211_chan_def def;
12294 	struct ath12k *ar;
12295 	enum nl80211_band band;
12296 	const u8 *ht_mcs_mask;
12297 	const u16 *vht_mcs_mask;
12298 	const u16 *he_mcs_mask;
12299 	u8 he_ltf = 0;
12300 	u8 he_gi = 0;
12301 	u32 rate;
12302 	u8 nss, mac_nss;
12303 	u8 sgi;
12304 	u8 ldpc;
12305 	int single_nss;
12306 	int ret;
12307 	int num_rates;
12308 	bool he_fixed_rate = false;
12309 
12310 	lockdep_assert_wiphy(hw->wiphy);
12311 
12312 	arvif = &ahvif->deflink;
12313 
12314 	ar = arvif->ar;
12315 	if (ath12k_mac_vif_link_chan(vif, arvif->link_id, &def)) {
12316 		ret = -EPERM;
12317 		goto out;
12318 	}
12319 
12320 	band = def.chan->band;
12321 	ht_mcs_mask = mask->control[band].ht_mcs;
12322 	vht_mcs_mask = mask->control[band].vht_mcs;
12323 	he_mcs_mask = mask->control[band].he_mcs;
12324 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
12325 
12326 	sgi = mask->control[band].gi;
12327 	if (sgi == NL80211_TXRATE_FORCE_SGI) {
12328 		ret = -EINVAL;
12329 		goto out;
12330 	}
12331 
12332 	he_gi = mask->control[band].he_gi;
12333 	he_ltf = mask->control[band].he_ltf;
12334 
12335 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
12336 	 * requires passing at least one of used basic rates along with them.
12337 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
12338 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
12339 	 * suitable for setting single HT/VHT rates.
12340 	 * But, there could be a single basic rate passed from userspace which
12341 	 * can be done through the FIXED_RATE param.
12342 	 */
12343 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
12344 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
12345 							&nss);
12346 		if (ret) {
12347 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
12348 				    arvif->vdev_id, ret);
12349 			goto out;
12350 		}
12351 
12352 		ieee80211_iterate_stations_mtx(hw,
12353 					       ath12k_mac_disable_peer_fixed_rate,
12354 					       arvif);
12355 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, vif, band, mask,
12356 							  &single_nss)) {
12357 		rate = WMI_FIXED_RATE_NONE;
12358 		nss = single_nss;
12359 		arvif->bitrate_mask = *mask;
12360 
12361 		ieee80211_iterate_stations_atomic(hw,
12362 						  ath12k_mac_set_bitrate_mask_iter,
12363 						  arvif);
12364 	} else {
12365 		rate = WMI_FIXED_RATE_NONE;
12366 
12367 		if (!ath12k_mac_validate_fixed_rate_settings(ar, band,
12368 							     mask, arvif->link_id))
12369 			ath12k_warn(ar->ab,
12370 				    "failed to update fixed rate settings due to mcs/nss incompatibility\n");
12371 
12372 		mac_nss = max3(ath12k_mac_max_ht_nss(ht_mcs_mask),
12373 			       ath12k_mac_max_vht_nss(vht_mcs_mask),
12374 			       ath12k_mac_max_he_nss(he_mcs_mask));
12375 		nss = min_t(u32, ar->num_tx_chains, mac_nss);
12376 
12377 		/* If multiple rates across different preambles are given
12378 		 * we can reconfigure this info with all peers using PEER_ASSOC
12379 		 * command with the below exception cases.
12380 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
12381 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
12382 		 * mandates passing basic rates along with HT/VHT rates, FW
12383 		 * doesn't allow switching from VHT to Legacy. Hence instead of
12384 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
12385 		 * we could set this VHT rate as peer fixed rate param, which
12386 		 * will override FIXED rate and FW rate control algorithm.
12387 		 * If single VHT rate is passed along with HT rates, we select
12388 		 * the VHT rate as fixed rate for vht peers.
12389 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
12390 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
12391 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
12392 		 * RATEMASK_CMDID can cover all use cases of setting rates
12393 		 * across multiple preambles and rates within same type.
12394 		 * But requires more validation of the command at this point.
12395 		 */
12396 
12397 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
12398 								  mask);
12399 
12400 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
12401 		    num_rates > 1) {
12402 			/* TODO: Handle multiple VHT MCS values setting using
12403 			 * RATEMASK CMD
12404 			 */
12405 			ath12k_warn(ar->ab,
12406 				    "Setting more than one MCS Value in bitrate mask not supported\n");
12407 			ret = -EINVAL;
12408 			goto out;
12409 		}
12410 
12411 		num_rates = ath12k_mac_bitrate_mask_num_he_rates(ar, band, mask);
12412 		if (num_rates == 1)
12413 			he_fixed_rate = true;
12414 
12415 		if (!ath12k_mac_he_mcs_range_present(ar, band, mask) &&
12416 		    num_rates > 1) {
12417 			ath12k_warn(ar->ab,
12418 				    "Setting more than one HE MCS Value in bitrate mask not supported\n");
12419 			ret = -EINVAL;
12420 			goto out;
12421 		}
12422 		ieee80211_iterate_stations_mtx(hw,
12423 					       ath12k_mac_disable_peer_fixed_rate,
12424 					       arvif);
12425 
12426 		arvif->bitrate_mask = *mask;
12427 		ieee80211_iterate_stations_mtx(hw,
12428 					       ath12k_mac_set_bitrate_mask_iter,
12429 					       arvif);
12430 	}
12431 
12432 	ret = ath12k_mac_set_rate_params(arvif, rate, nss, sgi, ldpc, he_gi,
12433 					 he_ltf, he_fixed_rate);
12434 	if (ret) {
12435 		ath12k_warn(ar->ab, "failed to set rate params on vdev %i: %d\n",
12436 			    arvif->vdev_id, ret);
12437 	}
12438 
12439 out:
12440 	return ret;
12441 }
12442 
12443 static void
ath12k_mac_op_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)12444 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
12445 				enum ieee80211_reconfig_type reconfig_type)
12446 {
12447 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12448 	struct ath12k *ar;
12449 	struct ath12k_base *ab;
12450 	struct ath12k_vif *ahvif;
12451 	struct ath12k_link_vif *arvif;
12452 	int recovery_count, i;
12453 
12454 	lockdep_assert_wiphy(hw->wiphy);
12455 
12456 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
12457 		return;
12458 
12459 	guard(mutex)(&ah->hw_mutex);
12460 
12461 	if (ah->state != ATH12K_HW_STATE_RESTARTED)
12462 		return;
12463 
12464 	ah->state = ATH12K_HW_STATE_ON;
12465 	ieee80211_wake_queues(hw);
12466 
12467 	for_each_ar(ah, ar, i) {
12468 		ab = ar->ab;
12469 
12470 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
12471 			    ar->pdev->pdev_id);
12472 
12473 		if (ar->ab->hw_params->current_cc_support &&
12474 		    ar->alpha2[0] != 0 && ar->alpha2[1] != 0) {
12475 			struct wmi_set_current_country_arg arg = {};
12476 
12477 			memcpy(&arg.alpha2, ar->alpha2, 2);
12478 			reinit_completion(&ar->regd_update_completed);
12479 			ath12k_wmi_send_set_current_country_cmd(ar, &arg);
12480 		}
12481 
12482 		if (ab->is_reset) {
12483 			recovery_count = atomic_inc_return(&ab->recovery_count);
12484 
12485 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
12486 				   recovery_count);
12487 
12488 			/* When there are multiple radios in an SOC,
12489 			 * the recovery has to be done for each radio
12490 			 */
12491 			if (recovery_count == ab->num_radios) {
12492 				atomic_dec(&ab->reset_count);
12493 				complete(&ab->reset_complete);
12494 				ab->is_reset = false;
12495 				atomic_set(&ab->fail_cont_count, 0);
12496 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
12497 			}
12498 		}
12499 
12500 		list_for_each_entry(arvif, &ar->arvifs, list) {
12501 			ahvif = arvif->ahvif;
12502 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
12503 				   "reconfig cipher %d up %d vdev type %d\n",
12504 				   ahvif->key_cipher,
12505 				   arvif->is_up,
12506 				   ahvif->vdev_type);
12507 
12508 			/* After trigger disconnect, then upper layer will
12509 			 * trigger connect again, then the PN number of
12510 			 * upper layer will be reset to keep up with AP
12511 			 * side, hence PN number mismatch will not happen.
12512 			 */
12513 			if (arvif->is_up &&
12514 			    ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12515 			    ahvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
12516 				ieee80211_hw_restart_disconnect(ahvif->vif);
12517 
12518 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
12519 					   "restart disconnect\n");
12520 			}
12521 		}
12522 	}
12523 }
12524 
12525 static void
ath12k_mac_update_bss_chan_survey(struct ath12k * ar,struct ieee80211_channel * channel)12526 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
12527 				  struct ieee80211_channel *channel)
12528 {
12529 	int ret;
12530 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
12531 
12532 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
12533 
12534 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
12535 	    ar->rx_channel != channel)
12536 		return;
12537 
12538 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
12539 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
12540 			   "ignoring bss chan info req while scanning..\n");
12541 		return;
12542 	}
12543 
12544 	reinit_completion(&ar->bss_survey_done);
12545 
12546 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
12547 	if (ret) {
12548 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
12549 		return;
12550 	}
12551 
12552 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
12553 	if (ret == 0)
12554 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
12555 }
12556 
ath12k_mac_op_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)12557 static int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
12558 				    struct survey_info *survey)
12559 {
12560 	struct ath12k *ar;
12561 	struct ieee80211_supported_band *sband;
12562 	struct survey_info *ar_survey;
12563 
12564 	lockdep_assert_wiphy(hw->wiphy);
12565 
12566 	if (idx >= ATH12K_NUM_CHANS)
12567 		return -ENOENT;
12568 
12569 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
12570 	if (sband && idx >= sband->n_channels) {
12571 		idx -= sband->n_channels;
12572 		sband = NULL;
12573 	}
12574 
12575 	if (!sband)
12576 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
12577 	if (sband && idx >= sband->n_channels) {
12578 		idx -= sband->n_channels;
12579 		sband = NULL;
12580 	}
12581 
12582 	if (!sband)
12583 		sband = hw->wiphy->bands[NL80211_BAND_6GHZ];
12584 
12585 	if (!sband || idx >= sband->n_channels)
12586 		return -ENOENT;
12587 
12588 	ar = ath12k_mac_get_ar_by_chan(hw, &sband->channels[idx]);
12589 	if (!ar) {
12590 		if (sband->channels[idx].flags & IEEE80211_CHAN_DISABLED) {
12591 			memset(survey, 0, sizeof(*survey));
12592 			return 0;
12593 		}
12594 		return -ENOENT;
12595 	}
12596 
12597 	ar_survey = &ar->survey[idx];
12598 
12599 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
12600 
12601 	spin_lock_bh(&ar->data_lock);
12602 	memcpy(survey, ar_survey, sizeof(*survey));
12603 	spin_unlock_bh(&ar->data_lock);
12604 
12605 	survey->channel = &sband->channels[idx];
12606 
12607 	if (ar->rx_channel == survey->channel)
12608 		survey->filled |= SURVEY_INFO_IN_USE;
12609 
12610 	return 0;
12611 }
12612 
ath12k_mac_op_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)12613 static void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
12614 					 struct ieee80211_vif *vif,
12615 					 struct ieee80211_sta *sta,
12616 					 struct station_info *sinfo)
12617 {
12618 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(sta);
12619 	struct ath12k_fw_stats_req_params params = {};
12620 	struct ath12k_link_sta *arsta;
12621 	s8 signal, noise_floor;
12622 	struct ath12k *ar;
12623 	bool db2dbm;
12624 
12625 	lockdep_assert_wiphy(hw->wiphy);
12626 
12627 	arsta = &ahsta->deflink;
12628 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
12629 	if (!ar)
12630 		return;
12631 
12632 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
12633 			  ar->ab->wmi_ab.svc_map);
12634 
12635 	sinfo->rx_duration = arsta->rx_duration;
12636 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
12637 
12638 	sinfo->tx_duration = arsta->tx_duration;
12639 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
12640 
12641 	if (arsta->txrate.legacy || arsta->txrate.nss) {
12642 		if (arsta->txrate.legacy) {
12643 			sinfo->txrate.legacy = arsta->txrate.legacy;
12644 		} else {
12645 			sinfo->txrate.mcs = arsta->txrate.mcs;
12646 			sinfo->txrate.nss = arsta->txrate.nss;
12647 			sinfo->txrate.bw = arsta->txrate.bw;
12648 			sinfo->txrate.he_gi = arsta->txrate.he_gi;
12649 			sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
12650 			sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
12651 			sinfo->txrate.eht_gi = arsta->txrate.eht_gi;
12652 			sinfo->txrate.eht_ru_alloc = arsta->txrate.eht_ru_alloc;
12653 		}
12654 		sinfo->txrate.flags = arsta->txrate.flags;
12655 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
12656 	}
12657 
12658 	/* TODO: Use real NF instead of default one. */
12659 	signal = arsta->rssi_comb;
12660 
12661 	params.pdev_id = ar->pdev->pdev_id;
12662 	params.vdev_id = 0;
12663 	params.stats_id = WMI_REQUEST_VDEV_STAT;
12664 
12665 	if (!signal &&
12666 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12667 	    !(ath12k_mac_get_fw_stats(ar, &params)))
12668 		signal = arsta->rssi_beacon;
12669 
12670 	spin_lock_bh(&ar->data_lock);
12671 	noise_floor = ath12k_pdev_get_noise_floor(ar);
12672 	spin_unlock_bh(&ar->data_lock);
12673 
12674 	if (signal) {
12675 		sinfo->signal = db2dbm ? signal : signal + noise_floor;
12676 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
12677 	}
12678 
12679 	sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi);
12680 
12681 	if (!db2dbm)
12682 		sinfo->signal_avg += noise_floor;
12683 
12684 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
12685 
12686 	sinfo->tx_retries = arsta->tx_retry_count;
12687 	sinfo->tx_failed = arsta->tx_retry_failed;
12688 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
12689 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
12690 }
12691 
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)12692 static void ath12k_mac_op_link_sta_statistics(struct ieee80211_hw *hw,
12693 					      struct ieee80211_vif *vif,
12694 					      struct ieee80211_link_sta *link_sta,
12695 					      struct link_station_info *link_sinfo)
12696 {
12697 	struct ath12k_sta *ahsta = ath12k_sta_to_ahsta(link_sta->sta);
12698 	struct ath12k_fw_stats_req_params params = {};
12699 	struct ath12k_link_sta *arsta;
12700 	struct ath12k *ar;
12701 	s8 signal;
12702 	bool db2dbm;
12703 
12704 	lockdep_assert_wiphy(hw->wiphy);
12705 
12706 	arsta = wiphy_dereference(hw->wiphy, ahsta->link[link_sta->link_id]);
12707 
12708 	if (!arsta)
12709 		return;
12710 
12711 	ar = ath12k_get_ar_by_vif(hw, vif, arsta->link_id);
12712 	if (!ar)
12713 		return;
12714 
12715 	db2dbm = test_bit(WMI_TLV_SERVICE_HW_DB2DBM_CONVERSION_SUPPORT,
12716 			  ar->ab->wmi_ab.svc_map);
12717 
12718 	link_sinfo->rx_duration = arsta->rx_duration;
12719 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
12720 
12721 	link_sinfo->tx_duration = arsta->tx_duration;
12722 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
12723 
12724 	if (arsta->txrate.legacy || arsta->txrate.nss) {
12725 		if (arsta->txrate.legacy) {
12726 			link_sinfo->txrate.legacy = arsta->txrate.legacy;
12727 		} else {
12728 			link_sinfo->txrate.mcs = arsta->txrate.mcs;
12729 			link_sinfo->txrate.nss = arsta->txrate.nss;
12730 			link_sinfo->txrate.bw = arsta->txrate.bw;
12731 			link_sinfo->txrate.he_gi = arsta->txrate.he_gi;
12732 			link_sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
12733 			link_sinfo->txrate.he_ru_alloc =
12734 				arsta->txrate.he_ru_alloc;
12735 			link_sinfo->txrate.eht_gi = arsta->txrate.eht_gi;
12736 			link_sinfo->txrate.eht_ru_alloc =
12737 				arsta->txrate.eht_ru_alloc;
12738 		}
12739 		link_sinfo->txrate.flags = arsta->txrate.flags;
12740 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
12741 	}
12742 
12743 	/* TODO: Use real NF instead of default one. */
12744 	signal = arsta->rssi_comb;
12745 
12746 	params.pdev_id = ar->pdev->pdev_id;
12747 	params.vdev_id = 0;
12748 	params.stats_id = WMI_REQUEST_VDEV_STAT;
12749 
12750 	if (!signal &&
12751 	    ahsta->ahvif->vdev_type == WMI_VDEV_TYPE_STA &&
12752 	    !(ath12k_mac_get_fw_stats(ar, &params)))
12753 		signal = arsta->rssi_beacon;
12754 
12755 	if (signal) {
12756 		link_sinfo->signal =
12757 			db2dbm ? signal : signal + ATH12K_DEFAULT_NOISE_FLOOR;
12758 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
12759 	}
12760 
12761 	link_sinfo->signal_avg = ewma_avg_rssi_read(&arsta->avg_rssi);
12762 
12763 	if (!db2dbm)
12764 		link_sinfo->signal_avg += ATH12K_DEFAULT_NOISE_FLOOR;
12765 
12766 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
12767 
12768 	link_sinfo->tx_retries = arsta->tx_retry_count;
12769 	link_sinfo->tx_failed = arsta->tx_retry_failed;
12770 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
12771 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
12772 }
12773 
ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif)12774 static int ath12k_mac_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
12775 						  struct ieee80211_vif *vif)
12776 {
12777 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12778 	struct ath12k *ar;
12779 
12780 	ar = ath12k_ah_to_ar(ah, 0);
12781 
12782 	lockdep_assert_wiphy(hw->wiphy);
12783 
12784 	spin_lock_bh(&ar->data_lock);
12785 	ar->scan.roc_notify = false;
12786 	spin_unlock_bh(&ar->data_lock);
12787 
12788 	ath12k_scan_abort(ar);
12789 
12790 	cancel_delayed_work_sync(&ar->scan.timeout);
12791 	wiphy_work_cancel(hw->wiphy, &ar->scan.vdev_clean_wk);
12792 
12793 	return 0;
12794 }
12795 
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)12796 static int ath12k_mac_op_remain_on_channel(struct ieee80211_hw *hw,
12797 					   struct ieee80211_vif *vif,
12798 					   struct ieee80211_channel *chan,
12799 					   int duration,
12800 					   enum ieee80211_roc_type type)
12801 {
12802 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12803 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12804 	struct ath12k_link_vif *arvif;
12805 	struct ath12k *ar;
12806 	u32 scan_time_msec;
12807 	bool create = true;
12808 	u8 link_id;
12809 	int ret;
12810 
12811 	lockdep_assert_wiphy(hw->wiphy);
12812 
12813 	ar = ath12k_mac_select_scan_device(hw, vif, chan->center_freq);
12814 	if (!ar)
12815 		return -EINVAL;
12816 
12817 	/* check if any of the links of ML VIF is already started on
12818 	 * radio(ar) corresponding to given scan frequency and use it,
12819 	 * if not use deflink(link 0) for scan purpose.
12820 	 */
12821 
12822 	link_id = ath12k_mac_find_link_id_by_ar(ahvif, ar);
12823 	arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12824 	/* If the vif is already assigned to a specific vdev of an ar,
12825 	 * check whether its already started, vdev which is started
12826 	 * are not allowed to switch to a new radio.
12827 	 * If the vdev is not started, but was earlier created on a
12828 	 * different ar, delete that vdev and create a new one. We don't
12829 	 * delete at the scan stop as an optimization to avoid redundant
12830 	 * delete-create vdev's for the same ar, in case the request is
12831 	 * always on the same band for the vif
12832 	 */
12833 	if (arvif->is_created) {
12834 		if (WARN_ON(!arvif->ar))
12835 			return -EINVAL;
12836 
12837 		if (ar != arvif->ar && arvif->is_started)
12838 			return -EBUSY;
12839 
12840 		if (ar != arvif->ar) {
12841 			ath12k_mac_remove_link_interface(hw, arvif);
12842 			ath12k_mac_unassign_link_vif(arvif);
12843 		} else {
12844 			create = false;
12845 		}
12846 	}
12847 
12848 	if (create) {
12849 		arvif = ath12k_mac_assign_link_vif(ah, vif, link_id);
12850 
12851 		ret = ath12k_mac_vdev_create(ar, arvif);
12852 		if (ret) {
12853 			ath12k_warn(ar->ab, "unable to create scan vdev for roc: %d\n",
12854 				    ret);
12855 			return ret;
12856 		}
12857 	}
12858 
12859 	spin_lock_bh(&ar->data_lock);
12860 
12861 	switch (ar->scan.state) {
12862 	case ATH12K_SCAN_IDLE:
12863 		reinit_completion(&ar->scan.started);
12864 		reinit_completion(&ar->scan.completed);
12865 		reinit_completion(&ar->scan.on_channel);
12866 		ar->scan.state = ATH12K_SCAN_STARTING;
12867 		ar->scan.is_roc = true;
12868 		ar->scan.arvif = arvif;
12869 		ar->scan.roc_freq = chan->center_freq;
12870 		ar->scan.roc_notify = true;
12871 		ret = 0;
12872 		break;
12873 	case ATH12K_SCAN_STARTING:
12874 	case ATH12K_SCAN_RUNNING:
12875 	case ATH12K_SCAN_ABORTING:
12876 		ret = -EBUSY;
12877 		break;
12878 	}
12879 
12880 	spin_unlock_bh(&ar->data_lock);
12881 
12882 	if (ret)
12883 		return ret;
12884 
12885 	scan_time_msec = hw->wiphy->max_remain_on_channel_duration * 2;
12886 
12887 	struct ath12k_wmi_scan_req_arg *arg __free(kfree) =
12888 					kzalloc(sizeof(*arg), GFP_KERNEL);
12889 	if (!arg)
12890 		return -ENOMEM;
12891 
12892 	ath12k_wmi_start_scan_init(ar, arg);
12893 	arg->num_chan = 1;
12894 
12895 	u32 *chan_list __free(kfree) = kcalloc(arg->num_chan, sizeof(*chan_list),
12896 					       GFP_KERNEL);
12897 	if (!chan_list)
12898 		return -ENOMEM;
12899 
12900 	arg->chan_list = chan_list;
12901 	arg->vdev_id = arvif->vdev_id;
12902 	arg->scan_id = ATH12K_SCAN_ID;
12903 	arg->chan_list[0] = chan->center_freq;
12904 	arg->dwell_time_active = scan_time_msec;
12905 	arg->dwell_time_passive = scan_time_msec;
12906 	arg->max_scan_time = scan_time_msec;
12907 	arg->scan_f_passive = 1;
12908 	arg->burst_duration = duration;
12909 
12910 	ret = ath12k_start_scan(ar, arg);
12911 	if (ret) {
12912 		ath12k_warn(ar->ab, "failed to start roc scan: %d\n", ret);
12913 
12914 		spin_lock_bh(&ar->data_lock);
12915 		ar->scan.state = ATH12K_SCAN_IDLE;
12916 		spin_unlock_bh(&ar->data_lock);
12917 		return ret;
12918 	}
12919 
12920 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
12921 	if (ret == 0) {
12922 		ath12k_warn(ar->ab, "failed to switch to channel for roc scan\n");
12923 		ret = ath12k_scan_stop(ar);
12924 		if (ret)
12925 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
12926 		return -ETIMEDOUT;
12927 	}
12928 
12929 	ieee80211_queue_delayed_work(hw, &ar->scan.timeout,
12930 				     msecs_to_jiffies(duration));
12931 
12932 	return 0;
12933 }
12934 
ath12k_mac_op_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)12935 static void ath12k_mac_op_set_rekey_data(struct ieee80211_hw *hw,
12936 					 struct ieee80211_vif *vif,
12937 					 struct cfg80211_gtk_rekey_data *data)
12938 {
12939 	struct ath12k_vif *ahvif = ath12k_vif_to_ahvif(vif);
12940 	struct ath12k_rekey_data *rekey_data;
12941 	struct ath12k_hw *ah = ath12k_hw_to_ah(hw);
12942 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
12943 	struct ath12k_link_vif *arvif;
12944 
12945 	lockdep_assert_wiphy(hw->wiphy);
12946 
12947 	arvif = &ahvif->deflink;
12948 	rekey_data = &arvif->rekey_data;
12949 
12950 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set rekey data vdev %d\n",
12951 		   arvif->vdev_id);
12952 
12953 	memcpy(rekey_data->kck, data->kck, NL80211_KCK_LEN);
12954 	memcpy(rekey_data->kek, data->kek, NL80211_KEK_LEN);
12955 
12956 	/* The supplicant works on big-endian, the firmware expects it on
12957 	 * little endian.
12958 	 */
12959 	rekey_data->replay_ctr = get_unaligned_be64(data->replay_ctr);
12960 
12961 	arvif->rekey_data.enable_offload = true;
12962 
12963 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kck", NULL,
12964 			rekey_data->kck, NL80211_KCK_LEN);
12965 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "kek", NULL,
12966 			rekey_data->kck, NL80211_KEK_LEN);
12967 	ath12k_dbg_dump(ar->ab, ATH12K_DBG_MAC, "replay ctr", NULL,
12968 			&rekey_data->replay_ctr, sizeof(rekey_data->replay_ctr));
12969 }
12970 
12971 static const struct ieee80211_ops ath12k_ops = {
12972 	.tx				= ath12k_mac_op_tx,
12973 	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
12974 	.start                          = ath12k_mac_op_start,
12975 	.stop                           = ath12k_mac_op_stop,
12976 	.reconfig_complete              = ath12k_mac_op_reconfig_complete,
12977 	.add_interface                  = ath12k_mac_op_add_interface,
12978 	.remove_interface		= ath12k_mac_op_remove_interface,
12979 	.update_vif_offload		= ath12k_mac_op_update_vif_offload,
12980 	.config                         = ath12k_mac_op_config,
12981 	.link_info_changed              = ath12k_mac_op_link_info_changed,
12982 	.vif_cfg_changed		= ath12k_mac_op_vif_cfg_changed,
12983 	.change_vif_links               = ath12k_mac_op_change_vif_links,
12984 	.configure_filter		= ath12k_mac_op_configure_filter,
12985 	.hw_scan                        = ath12k_mac_op_hw_scan,
12986 	.cancel_hw_scan                 = ath12k_mac_op_cancel_hw_scan,
12987 	.set_key                        = ath12k_mac_op_set_key,
12988 	.set_rekey_data	                = ath12k_mac_op_set_rekey_data,
12989 	.sta_state                      = ath12k_mac_op_sta_state,
12990 	.sta_set_txpwr			= ath12k_mac_op_sta_set_txpwr,
12991 	.link_sta_rc_update		= ath12k_mac_op_link_sta_rc_update,
12992 	.conf_tx                        = ath12k_mac_op_conf_tx,
12993 	.set_antenna			= ath12k_mac_op_set_antenna,
12994 	.get_antenna			= ath12k_mac_op_get_antenna,
12995 	.ampdu_action			= ath12k_mac_op_ampdu_action,
12996 	.add_chanctx			= ath12k_mac_op_add_chanctx,
12997 	.remove_chanctx			= ath12k_mac_op_remove_chanctx,
12998 	.change_chanctx			= ath12k_mac_op_change_chanctx,
12999 	.assign_vif_chanctx		= ath12k_mac_op_assign_vif_chanctx,
13000 	.unassign_vif_chanctx		= ath12k_mac_op_unassign_vif_chanctx,
13001 	.switch_vif_chanctx		= ath12k_mac_op_switch_vif_chanctx,
13002 	.get_txpower			= ath12k_mac_op_get_txpower,
13003 	.set_rts_threshold		= ath12k_mac_op_set_rts_threshold,
13004 	.set_frag_threshold		= ath12k_mac_op_set_frag_threshold,
13005 	.set_bitrate_mask		= ath12k_mac_op_set_bitrate_mask,
13006 	.get_survey			= ath12k_mac_op_get_survey,
13007 	.flush				= ath12k_mac_op_flush,
13008 	.sta_statistics			= ath12k_mac_op_sta_statistics,
13009 	.link_sta_statistics		= ath12k_mac_op_link_sta_statistics,
13010 	.remain_on_channel              = ath12k_mac_op_remain_on_channel,
13011 	.cancel_remain_on_channel       = ath12k_mac_op_cancel_remain_on_channel,
13012 	.change_sta_links               = ath12k_mac_op_change_sta_links,
13013 	.can_activate_links             = ath12k_mac_op_can_activate_links,
13014 #ifdef CONFIG_PM
13015 	.suspend			= ath12k_wow_op_suspend,
13016 	.resume				= ath12k_wow_op_resume,
13017 	.set_wakeup			= ath12k_wow_op_set_wakeup,
13018 #endif
13019 #ifdef CONFIG_ATH12K_DEBUGFS
13020 	.vif_add_debugfs                = ath12k_debugfs_op_vif_add,
13021 #endif
13022 	CFG80211_TESTMODE_CMD(ath12k_tm_cmd)
13023 #ifdef CONFIG_ATH12K_DEBUGFS
13024 	.link_sta_add_debugfs           = ath12k_debugfs_link_sta_op_add,
13025 #endif
13026 };
13027 
ath12k_mac_update_freq_range(struct ath12k * ar,u32 freq_low,u32 freq_high)13028 void ath12k_mac_update_freq_range(struct ath12k *ar,
13029 				  u32 freq_low, u32 freq_high)
13030 {
13031 	if (!(freq_low && freq_high))
13032 		return;
13033 
13034 	if (ar->freq_range.start_freq || ar->freq_range.end_freq) {
13035 		ar->freq_range.start_freq = min(ar->freq_range.start_freq,
13036 						MHZ_TO_KHZ(freq_low));
13037 		ar->freq_range.end_freq = max(ar->freq_range.end_freq,
13038 					      MHZ_TO_KHZ(freq_high));
13039 	} else {
13040 		ar->freq_range.start_freq = MHZ_TO_KHZ(freq_low);
13041 		ar->freq_range.end_freq = MHZ_TO_KHZ(freq_high);
13042 	}
13043 
13044 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
13045 		   "mac pdev %u freq limit updated. New range %u->%u MHz\n",
13046 		   ar->pdev->pdev_id, KHZ_TO_MHZ(ar->freq_range.start_freq),
13047 		   KHZ_TO_MHZ(ar->freq_range.end_freq));
13048 }
13049 
ath12k_mac_update_ch_list(struct ath12k * ar,struct ieee80211_supported_band * band,u32 freq_low,u32 freq_high)13050 static void ath12k_mac_update_ch_list(struct ath12k *ar,
13051 				      struct ieee80211_supported_band *band,
13052 				      u32 freq_low, u32 freq_high)
13053 {
13054 	int i;
13055 
13056 	if (!(freq_low && freq_high))
13057 		return;
13058 
13059 	for (i = 0; i < band->n_channels; i++) {
13060 		if (band->channels[i].center_freq < freq_low ||
13061 		    band->channels[i].center_freq > freq_high)
13062 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
13063 	}
13064 }
13065 
ath12k_get_phy_id(struct ath12k * ar,u32 band)13066 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
13067 {
13068 	struct ath12k_pdev *pdev = ar->pdev;
13069 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
13070 
13071 	if (band == WMI_HOST_WLAN_2GHZ_CAP)
13072 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
13073 
13074 	if (band == WMI_HOST_WLAN_5GHZ_CAP)
13075 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
13076 
13077 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
13078 
13079 	return 0;
13080 }
13081 
ath12k_mac_update_band(struct ath12k * ar,struct ieee80211_supported_band * orig_band,struct ieee80211_supported_band * new_band)13082 static int ath12k_mac_update_band(struct ath12k *ar,
13083 				  struct ieee80211_supported_band *orig_band,
13084 				  struct ieee80211_supported_band *new_band)
13085 {
13086 	int i;
13087 
13088 	if (!orig_band || !new_band)
13089 		return -EINVAL;
13090 
13091 	if (orig_band->band != new_band->band)
13092 		return -EINVAL;
13093 
13094 	for (i = 0; i < new_band->n_channels; i++) {
13095 		if (new_band->channels[i].flags & IEEE80211_CHAN_DISABLED)
13096 			continue;
13097 		/* An enabled channel in new_band should not be already enabled
13098 		 * in the orig_band
13099 		 */
13100 		if (WARN_ON(!(orig_band->channels[i].flags &
13101 			      IEEE80211_CHAN_DISABLED)))
13102 			return -EINVAL;
13103 		orig_band->channels[i].flags &= ~IEEE80211_CHAN_DISABLED;
13104 	}
13105 	return 0;
13106 }
13107 
ath12k_mac_setup_channels_rates(struct ath12k * ar,u32 supported_bands,struct ieee80211_supported_band * bands[])13108 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
13109 					   u32 supported_bands,
13110 					   struct ieee80211_supported_band *bands[])
13111 {
13112 	struct ieee80211_supported_band *band;
13113 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
13114 	struct ath12k_base *ab = ar->ab;
13115 	u32 phy_id, freq_low, freq_high;
13116 	struct ath12k_hw *ah = ar->ah;
13117 	void *channels;
13118 	int ret;
13119 
13120 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
13121 		      ARRAY_SIZE(ath12k_5ghz_channels) +
13122 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
13123 		     ATH12K_NUM_CHANS);
13124 
13125 	reg_cap = &ab->hal_reg_cap[ar->pdev_idx];
13126 
13127 	if (supported_bands & WMI_HOST_WLAN_2GHZ_CAP) {
13128 		channels = kmemdup(ath12k_2ghz_channels,
13129 				   sizeof(ath12k_2ghz_channels),
13130 				   GFP_KERNEL);
13131 		if (!channels)
13132 			return -ENOMEM;
13133 
13134 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
13135 		band->band = NL80211_BAND_2GHZ;
13136 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
13137 		band->channels = channels;
13138 		band->n_bitrates = ath12k_g_rates_size;
13139 		band->bitrates = ath12k_g_rates;
13140 
13141 		if (ab->hw_params->single_pdev_only) {
13142 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2GHZ_CAP);
13143 			reg_cap = &ab->hal_reg_cap[phy_id];
13144 		}
13145 
13146 		freq_low = max(reg_cap->low_2ghz_chan,
13147 			       ab->reg_freq_2ghz.start_freq);
13148 		freq_high = min(reg_cap->high_2ghz_chan,
13149 				ab->reg_freq_2ghz.end_freq);
13150 
13151 		ath12k_mac_update_ch_list(ar, band,
13152 					  reg_cap->low_2ghz_chan,
13153 					  reg_cap->high_2ghz_chan);
13154 
13155 		ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13156 
13157 		if (!bands[NL80211_BAND_2GHZ]) {
13158 			bands[NL80211_BAND_2GHZ] = band;
13159 		} else {
13160 			/* Split mac in same band under same wiphy */
13161 			ret = ath12k_mac_update_band(ar, bands[NL80211_BAND_2GHZ], band);
13162 			if (ret) {
13163 				kfree(channels);
13164 				band->channels = NULL;
13165 				return ret;
13166 			}
13167 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 2 GHz split mac with start freq %d end freq %d",
13168 				   ar->pdev->pdev_id,
13169 				   KHZ_TO_MHZ(ar->freq_range.start_freq),
13170 				   KHZ_TO_MHZ(ar->freq_range.end_freq));
13171 		}
13172 	}
13173 
13174 	if (supported_bands & WMI_HOST_WLAN_5GHZ_CAP) {
13175 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6GHZ_FREQ) {
13176 			channels = kmemdup(ath12k_6ghz_channels,
13177 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
13178 			if (!channels) {
13179 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13180 				return -ENOMEM;
13181 			}
13182 
13183 			ar->supports_6ghz = true;
13184 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
13185 			band->band = NL80211_BAND_6GHZ;
13186 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
13187 			band->channels = channels;
13188 			band->n_bitrates = ath12k_a_rates_size;
13189 			band->bitrates = ath12k_a_rates;
13190 
13191 			freq_low = max(reg_cap->low_5ghz_chan,
13192 				       ab->reg_freq_6ghz.start_freq);
13193 			freq_high = min(reg_cap->high_5ghz_chan,
13194 					ab->reg_freq_6ghz.end_freq);
13195 
13196 			ath12k_mac_update_ch_list(ar, band,
13197 						  reg_cap->low_5ghz_chan,
13198 						  reg_cap->high_5ghz_chan);
13199 
13200 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13201 			ah->use_6ghz_regd = true;
13202 
13203 			if (!bands[NL80211_BAND_6GHZ]) {
13204 				bands[NL80211_BAND_6GHZ] = band;
13205 			} else {
13206 				/* Split mac in same band under same wiphy */
13207 				ret = ath12k_mac_update_band(ar,
13208 							     bands[NL80211_BAND_6GHZ],
13209 							     band);
13210 				if (ret) {
13211 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13212 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13213 					kfree(channels);
13214 					band->channels = NULL;
13215 					return ret;
13216 				}
13217 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 6 GHz split mac with start freq %d end freq %d",
13218 					   ar->pdev->pdev_id,
13219 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13220 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13221 			}
13222 		}
13223 
13224 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6GHZ_FREQ) {
13225 			channels = kmemdup(ath12k_5ghz_channels,
13226 					   sizeof(ath12k_5ghz_channels),
13227 					   GFP_KERNEL);
13228 			if (!channels) {
13229 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13230 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
13231 				return -ENOMEM;
13232 			}
13233 
13234 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
13235 			band->band = NL80211_BAND_5GHZ;
13236 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
13237 			band->channels = channels;
13238 			band->n_bitrates = ath12k_a_rates_size;
13239 			band->bitrates = ath12k_a_rates;
13240 
13241 			if (ab->hw_params->single_pdev_only) {
13242 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5GHZ_CAP);
13243 				reg_cap = &ab->hal_reg_cap[phy_id];
13244 			}
13245 
13246 			freq_low = max(reg_cap->low_5ghz_chan,
13247 				       ab->reg_freq_5ghz.start_freq);
13248 			freq_high = min(reg_cap->high_5ghz_chan,
13249 					ab->reg_freq_5ghz.end_freq);
13250 
13251 			ath12k_mac_update_ch_list(ar, band,
13252 						  reg_cap->low_5ghz_chan,
13253 						  reg_cap->high_5ghz_chan);
13254 
13255 			ath12k_mac_update_freq_range(ar, freq_low, freq_high);
13256 
13257 			if (!bands[NL80211_BAND_5GHZ]) {
13258 				bands[NL80211_BAND_5GHZ] = band;
13259 			} else {
13260 				/* Split mac in same band under same wiphy */
13261 				ret = ath12k_mac_update_band(ar,
13262 							     bands[NL80211_BAND_5GHZ],
13263 							     band);
13264 				if (ret) {
13265 					kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13266 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13267 					kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
13268 					ar->mac.sbands[NL80211_BAND_2GHZ].channels = NULL;
13269 					kfree(channels);
13270 					band->channels = NULL;
13271 					return ret;
13272 				}
13273 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac pdev %u identified as 5 GHz split mac with start freq %d end freq %d",
13274 					   ar->pdev->pdev_id,
13275 					   KHZ_TO_MHZ(ar->freq_range.start_freq),
13276 					   KHZ_TO_MHZ(ar->freq_range.end_freq));
13277 			}
13278 		}
13279 	}
13280 
13281 	return 0;
13282 }
13283 
ath12k_mac_get_ifmodes(struct ath12k_hw * ah)13284 static u16 ath12k_mac_get_ifmodes(struct ath12k_hw *ah)
13285 {
13286 	struct ath12k *ar;
13287 	int i;
13288 	u16 interface_modes = U16_MAX;
13289 
13290 	for_each_ar(ah, ar, i)
13291 		interface_modes &= ar->ab->hw_params->interface_modes;
13292 
13293 	return interface_modes == U16_MAX ? 0 : interface_modes;
13294 }
13295 
ath12k_mac_is_iface_mode_enable(struct ath12k_hw * ah,enum nl80211_iftype type)13296 static bool ath12k_mac_is_iface_mode_enable(struct ath12k_hw *ah,
13297 					    enum nl80211_iftype type)
13298 {
13299 	struct ath12k *ar;
13300 	int i;
13301 	u16 interface_modes, mode = 0;
13302 	bool is_enable = false;
13303 
13304 	if (type == NL80211_IFTYPE_MESH_POINT) {
13305 		if (IS_ENABLED(CONFIG_MAC80211_MESH))
13306 			mode = BIT(type);
13307 	} else {
13308 		mode = BIT(type);
13309 	}
13310 
13311 	for_each_ar(ah, ar, i) {
13312 		interface_modes = ar->ab->hw_params->interface_modes;
13313 		if (interface_modes & mode) {
13314 			is_enable = true;
13315 			break;
13316 		}
13317 	}
13318 
13319 	return is_enable;
13320 }
13321 
13322 static int
ath12k_mac_setup_radio_iface_comb(struct ath12k * ar,struct ieee80211_iface_combination * comb)13323 ath12k_mac_setup_radio_iface_comb(struct ath12k *ar,
13324 				  struct ieee80211_iface_combination *comb)
13325 {
13326 	u16 interface_modes = ar->ab->hw_params->interface_modes;
13327 	struct ieee80211_iface_limit *limits;
13328 	int n_limits, max_interfaces;
13329 	bool ap, mesh, p2p;
13330 
13331 	ap = interface_modes & BIT(NL80211_IFTYPE_AP);
13332 	p2p = interface_modes & BIT(NL80211_IFTYPE_P2P_DEVICE);
13333 
13334 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
13335 	       (interface_modes & BIT(NL80211_IFTYPE_MESH_POINT));
13336 
13337 	if ((ap || mesh) && !p2p) {
13338 		n_limits = 2;
13339 		max_interfaces = 16;
13340 	} else if (p2p) {
13341 		n_limits = 3;
13342 		if (ap || mesh)
13343 			max_interfaces = 16;
13344 		else
13345 			max_interfaces = 3;
13346 	} else {
13347 		n_limits = 1;
13348 		max_interfaces = 1;
13349 	}
13350 
13351 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
13352 	if (!limits)
13353 		return -ENOMEM;
13354 
13355 	limits[0].max = 1;
13356 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
13357 
13358 	if (ap || mesh || p2p)
13359 		limits[1].max = max_interfaces;
13360 
13361 	if (ap)
13362 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
13363 
13364 	if (mesh)
13365 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
13366 
13367 	if (p2p) {
13368 		limits[1].types |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
13369 					BIT(NL80211_IFTYPE_P2P_GO);
13370 		limits[2].max = 1;
13371 		limits[2].types |= BIT(NL80211_IFTYPE_P2P_DEVICE);
13372 	}
13373 
13374 	comb[0].limits = limits;
13375 	comb[0].n_limits = n_limits;
13376 	comb[0].max_interfaces = max_interfaces;
13377 	comb[0].beacon_int_infra_match = true;
13378 	comb[0].beacon_int_min_gcd = 100;
13379 
13380 	comb[0].num_different_channels = 1;
13381 	comb[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
13382 				      BIT(NL80211_CHAN_WIDTH_20) |
13383 				      BIT(NL80211_CHAN_WIDTH_40) |
13384 				      BIT(NL80211_CHAN_WIDTH_80) |
13385 				      BIT(NL80211_CHAN_WIDTH_160);
13386 
13387 	return 0;
13388 }
13389 
13390 static int
ath12k_mac_setup_global_iface_comb(struct ath12k_hw * ah,struct wiphy_radio * radio,u8 n_radio,struct ieee80211_iface_combination * comb)13391 ath12k_mac_setup_global_iface_comb(struct ath12k_hw *ah,
13392 				   struct wiphy_radio *radio,
13393 				   u8 n_radio,
13394 				   struct ieee80211_iface_combination *comb)
13395 {
13396 	const struct ieee80211_iface_combination *iter_comb;
13397 	struct ieee80211_iface_limit *limits;
13398 	int i, j, n_limits;
13399 	bool ap, mesh, p2p;
13400 
13401 	if (!n_radio)
13402 		return 0;
13403 
13404 	ap = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_AP);
13405 	p2p = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_P2P_DEVICE);
13406 	mesh = ath12k_mac_is_iface_mode_enable(ah, NL80211_IFTYPE_MESH_POINT);
13407 
13408 	if ((ap || mesh) && !p2p)
13409 		n_limits = 2;
13410 	else if (p2p)
13411 		n_limits = 3;
13412 	else
13413 		n_limits = 1;
13414 
13415 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
13416 	if (!limits)
13417 		return -ENOMEM;
13418 
13419 	for (i = 0; i < n_radio; i++) {
13420 		iter_comb = radio[i].iface_combinations;
13421 		for (j = 0; j < iter_comb->n_limits && j < n_limits; j++) {
13422 			limits[j].types |= iter_comb->limits[j].types;
13423 			limits[j].max += iter_comb->limits[j].max;
13424 		}
13425 
13426 		comb->max_interfaces += iter_comb->max_interfaces;
13427 		comb->num_different_channels += iter_comb->num_different_channels;
13428 		comb->radar_detect_widths |= iter_comb->radar_detect_widths;
13429 	}
13430 
13431 	comb->limits = limits;
13432 	comb->n_limits = n_limits;
13433 	comb->beacon_int_infra_match = true;
13434 	comb->beacon_int_min_gcd = 100;
13435 
13436 	return 0;
13437 }
13438 
13439 static
ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination * iface_comb)13440 void ath12k_mac_cleanup_iface_comb(const struct ieee80211_iface_combination *iface_comb)
13441 {
13442 	kfree(iface_comb[0].limits);
13443 	kfree(iface_comb);
13444 }
13445 
ath12k_mac_cleanup_iface_combinations(struct ath12k_hw * ah)13446 static void ath12k_mac_cleanup_iface_combinations(struct ath12k_hw *ah)
13447 {
13448 	struct wiphy *wiphy = ah->hw->wiphy;
13449 	const struct wiphy_radio *radio;
13450 	int i;
13451 
13452 	if (wiphy->n_radio > 0) {
13453 		radio = wiphy->radio;
13454 		for (i = 0; i < wiphy->n_radio; i++)
13455 			ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
13456 
13457 		kfree(wiphy->radio);
13458 	}
13459 
13460 	ath12k_mac_cleanup_iface_comb(wiphy->iface_combinations);
13461 }
13462 
ath12k_mac_setup_iface_combinations(struct ath12k_hw * ah)13463 static int ath12k_mac_setup_iface_combinations(struct ath12k_hw *ah)
13464 {
13465 	struct ieee80211_iface_combination *combinations, *comb;
13466 	struct wiphy *wiphy = ah->hw->wiphy;
13467 	struct wiphy_radio *radio;
13468 	int n_combinations = 1;
13469 	struct ath12k *ar;
13470 	int i, ret;
13471 
13472 	if (ah->num_radio == 1) {
13473 		ar = &ah->radio[0];
13474 
13475 		if (ar->ab->hw_params->single_pdev_only)
13476 			n_combinations = 2;
13477 
13478 		combinations = kcalloc(n_combinations, sizeof(*combinations),
13479 				       GFP_KERNEL);
13480 		if (!combinations)
13481 			return -ENOMEM;
13482 
13483 		ret = ath12k_mac_setup_radio_iface_comb(ar, combinations);
13484 		if (ret) {
13485 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for one radio: %d",
13486 				       ret);
13487 			goto err_free_combinations;
13488 		}
13489 
13490 		if (ar->ab->hw_params->single_pdev_only) {
13491 			comb = combinations + 1;
13492 			memcpy(comb, combinations, sizeof(*comb));
13493 			comb->num_different_channels = 2;
13494 			comb->radar_detect_widths = 0;
13495 		}
13496 
13497 		goto out;
13498 	}
13499 
13500 	combinations = kcalloc(n_combinations, sizeof(*combinations), GFP_KERNEL);
13501 	if (!combinations)
13502 		return -ENOMEM;
13503 
13504 	/* there are multiple radios */
13505 
13506 	radio = kcalloc(ah->num_radio, sizeof(*radio), GFP_KERNEL);
13507 	if (!radio) {
13508 		ret = -ENOMEM;
13509 		goto err_free_combinations;
13510 	}
13511 
13512 	for_each_ar(ah, ar, i) {
13513 		comb = kzalloc(sizeof(*comb), GFP_KERNEL);
13514 		if (!comb) {
13515 			ret = -ENOMEM;
13516 			goto err_free_radios;
13517 		}
13518 
13519 		ret = ath12k_mac_setup_radio_iface_comb(ar, comb);
13520 		if (ret) {
13521 			ath12k_hw_warn(ah, "failed to setup radio interface combinations for radio %d: %d",
13522 				       i, ret);
13523 			kfree(comb);
13524 			goto err_free_radios;
13525 		}
13526 
13527 		radio[i].freq_range = &ar->freq_range;
13528 		radio[i].n_freq_range = 1;
13529 
13530 		radio[i].iface_combinations = comb;
13531 		radio[i].n_iface_combinations = 1;
13532 	}
13533 
13534 	ret = ath12k_mac_setup_global_iface_comb(ah, radio, ah->num_radio, combinations);
13535 	if (ret) {
13536 		ath12k_hw_warn(ah, "failed to setup global interface combinations: %d",
13537 			       ret);
13538 		goto err_free_all_radios;
13539 	}
13540 
13541 	wiphy->radio = radio;
13542 	wiphy->n_radio = ah->num_radio;
13543 
13544 out:
13545 	wiphy->iface_combinations = combinations;
13546 	wiphy->n_iface_combinations = n_combinations;
13547 
13548 	return 0;
13549 
13550 err_free_all_radios:
13551 	i = ah->num_radio;
13552 
13553 err_free_radios:
13554 	while (i--)
13555 		ath12k_mac_cleanup_iface_comb(radio[i].iface_combinations);
13556 
13557 	kfree(radio);
13558 
13559 err_free_combinations:
13560 	kfree(combinations);
13561 
13562 	return ret;
13563 }
13564 
13565 static const u8 ath12k_if_types_ext_capa[] = {
13566 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
13567 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
13568 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
13569 };
13570 
13571 static const u8 ath12k_if_types_ext_capa_sta[] = {
13572 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
13573 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
13574 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
13575 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
13576 };
13577 
13578 static const u8 ath12k_if_types_ext_capa_ap[] = {
13579 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
13580 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
13581 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
13582 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
13583 	[10] = WLAN_EXT_CAPA11_EMA_SUPPORT,
13584 };
13585 
13586 static struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
13587 	{
13588 		.extended_capabilities = ath12k_if_types_ext_capa,
13589 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
13590 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
13591 	}, {
13592 		.iftype = NL80211_IFTYPE_STATION,
13593 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
13594 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
13595 		.extended_capabilities_len =
13596 				sizeof(ath12k_if_types_ext_capa_sta),
13597 	}, {
13598 		.iftype = NL80211_IFTYPE_AP,
13599 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
13600 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
13601 		.extended_capabilities_len =
13602 				sizeof(ath12k_if_types_ext_capa_ap),
13603 		.eml_capabilities = 0,
13604 		.mld_capa_and_ops = 0,
13605 	},
13606 };
13607 
ath12k_mac_cleanup_unregister(struct ath12k * ar)13608 static void ath12k_mac_cleanup_unregister(struct ath12k *ar)
13609 {
13610 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
13611 	idr_destroy(&ar->txmgmt_idr);
13612 
13613 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
13614 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
13615 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
13616 }
13617 
ath12k_mac_hw_unregister(struct ath12k_hw * ah)13618 static void ath12k_mac_hw_unregister(struct ath12k_hw *ah)
13619 {
13620 	struct ieee80211_hw *hw = ah->hw;
13621 	struct ath12k *ar;
13622 	int i;
13623 
13624 	for_each_ar(ah, ar, i) {
13625 		cancel_work_sync(&ar->regd_channel_update_work);
13626 		cancel_work_sync(&ar->regd_update_work);
13627 		ath12k_debugfs_unregister(ar);
13628 		ath12k_fw_stats_reset(ar);
13629 	}
13630 
13631 	ieee80211_unregister_hw(hw);
13632 
13633 	for_each_ar(ah, ar, i)
13634 		ath12k_mac_cleanup_unregister(ar);
13635 
13636 	ath12k_mac_cleanup_iface_combinations(ah);
13637 
13638 	SET_IEEE80211_DEV(hw, NULL);
13639 }
13640 
ath12k_mac_setup_register(struct ath12k * ar,u32 * ht_cap,struct ieee80211_supported_band * bands[])13641 static int ath12k_mac_setup_register(struct ath12k *ar,
13642 				     u32 *ht_cap,
13643 				     struct ieee80211_supported_band *bands[])
13644 {
13645 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
13646 	int ret;
13647 
13648 	init_waitqueue_head(&ar->txmgmt_empty_waitq);
13649 	idr_init(&ar->txmgmt_idr);
13650 	spin_lock_init(&ar->txmgmt_idr_lock);
13651 
13652 	ath12k_pdev_caps_update(ar);
13653 
13654 	ret = ath12k_mac_setup_channels_rates(ar,
13655 					      cap->supported_bands,
13656 					      bands);
13657 	if (ret)
13658 		return ret;
13659 
13660 	ath12k_mac_setup_ht_vht_cap(ar, cap, ht_cap);
13661 	ath12k_mac_setup_sband_iftype_data(ar, cap);
13662 
13663 	ar->max_num_stations = ath12k_core_get_max_station_per_radio(ar->ab);
13664 	ar->max_num_peers = ath12k_core_get_max_peers_per_radio(ar->ab);
13665 
13666 	ar->rssi_info.min_nf_dbm = ATH12K_DEFAULT_NOISE_FLOOR;
13667 	ar->rssi_info.temp_offset = 0;
13668 	ar->rssi_info.noise_floor = ar->rssi_info.min_nf_dbm + ar->rssi_info.temp_offset;
13669 
13670 	return 0;
13671 }
13672 
ath12k_mac_hw_register(struct ath12k_hw * ah)13673 static int ath12k_mac_hw_register(struct ath12k_hw *ah)
13674 {
13675 	struct ieee80211_hw *hw = ah->hw;
13676 	struct wiphy *wiphy = hw->wiphy;
13677 	struct ath12k *ar = ath12k_ah_to_ar(ah, 0);
13678 	struct ath12k_base *ab = ar->ab;
13679 	struct ath12k_pdev *pdev;
13680 	struct ath12k_pdev_cap *cap;
13681 	static const u32 cipher_suites[] = {
13682 		WLAN_CIPHER_SUITE_TKIP,
13683 		WLAN_CIPHER_SUITE_CCMP,
13684 		WLAN_CIPHER_SUITE_AES_CMAC,
13685 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
13686 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
13687 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
13688 		WLAN_CIPHER_SUITE_GCMP,
13689 		WLAN_CIPHER_SUITE_GCMP_256,
13690 		WLAN_CIPHER_SUITE_CCMP_256,
13691 	};
13692 	int ret, i, j;
13693 	u32 ht_cap = U32_MAX, antennas_rx = 0, antennas_tx = 0;
13694 	bool is_6ghz = false, is_raw_mode = false, is_monitor_disable = false;
13695 	u8 *mac_addr = NULL;
13696 	u8 mbssid_max_interfaces = 0;
13697 
13698 	wiphy->max_ap_assoc_sta = 0;
13699 
13700 	for_each_ar(ah, ar, i) {
13701 		u32 ht_cap_info = 0;
13702 
13703 		pdev = ar->pdev;
13704 		if (ar->ab->pdevs_macaddr_valid) {
13705 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
13706 		} else {
13707 			ether_addr_copy(ar->mac_addr, ar->ab->mac_addr);
13708 			ar->mac_addr[4] += ar->pdev_idx;
13709 		}
13710 
13711 		ret = ath12k_mac_setup_register(ar, &ht_cap_info, hw->wiphy->bands);
13712 		if (ret)
13713 			goto err_cleanup_unregister;
13714 
13715 		/* 6 GHz does not support HT Cap, hence do not consider it */
13716 		if (!ar->supports_6ghz)
13717 			ht_cap &= ht_cap_info;
13718 
13719 		wiphy->max_ap_assoc_sta += ar->max_num_stations;
13720 
13721 		/* Advertise the max antenna support of all radios, driver can handle
13722 		 * per pdev specific antenna setting based on pdev cap when antenna
13723 		 * changes are made
13724 		 */
13725 		cap = &pdev->cap;
13726 
13727 		antennas_rx = max_t(u32, antennas_rx, cap->rx_chain_mask);
13728 		antennas_tx = max_t(u32, antennas_tx, cap->tx_chain_mask);
13729 
13730 		if (ar->supports_6ghz)
13731 			is_6ghz = true;
13732 
13733 		if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
13734 			is_raw_mode = true;
13735 
13736 		if (!ar->ab->hw_params->supports_monitor)
13737 			is_monitor_disable = true;
13738 
13739 		if (i == 0)
13740 			mac_addr = ar->mac_addr;
13741 		else
13742 			mac_addr = ab->mac_addr;
13743 
13744 		mbssid_max_interfaces += TARGET_NUM_VDEVS(ar->ab);
13745 	}
13746 
13747 	wiphy->available_antennas_rx = antennas_rx;
13748 	wiphy->available_antennas_tx = antennas_tx;
13749 
13750 	SET_IEEE80211_PERM_ADDR(hw, mac_addr);
13751 	SET_IEEE80211_DEV(hw, ab->dev);
13752 
13753 	ret = ath12k_mac_setup_iface_combinations(ah);
13754 	if (ret) {
13755 		ath12k_err(ab, "failed to setup interface combinations: %d\n", ret);
13756 		goto err_complete_cleanup_unregister;
13757 	}
13758 
13759 	wiphy->interface_modes = ath12k_mac_get_ifmodes(ah);
13760 
13761 	if (ah->num_radio == 1 &&
13762 	    wiphy->bands[NL80211_BAND_2GHZ] &&
13763 	    wiphy->bands[NL80211_BAND_5GHZ] &&
13764 	    wiphy->bands[NL80211_BAND_6GHZ])
13765 		ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
13766 
13767 	ieee80211_hw_set(hw, SIGNAL_DBM);
13768 	ieee80211_hw_set(hw, SUPPORTS_PS);
13769 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
13770 	ieee80211_hw_set(hw, MFP_CAPABLE);
13771 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
13772 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
13773 	ieee80211_hw_set(hw, AP_LINK_PS);
13774 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
13775 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
13776 	ieee80211_hw_set(hw, SUPPORTS_PER_STA_GTK);
13777 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
13778 	ieee80211_hw_set(hw, QUEUE_CONTROL);
13779 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
13780 	ieee80211_hw_set(hw, REPORTS_LOW_ACK);
13781 	ieee80211_hw_set(hw, NO_VIRTUAL_MONITOR);
13782 
13783 	if (test_bit(WMI_TLV_SERVICE_ETH_OFFLOAD, ar->wmi->wmi_ab->svc_map)) {
13784 		ieee80211_hw_set(hw, SUPPORTS_TX_ENCAP_OFFLOAD);
13785 		ieee80211_hw_set(hw, SUPPORTS_RX_DECAP_OFFLOAD);
13786 	}
13787 
13788 	if (cap->nss_ratio_enabled)
13789 		ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
13790 
13791 	if ((ht_cap & WMI_HT_CAP_ENABLED) || is_6ghz) {
13792 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
13793 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
13794 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
13795 		ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
13796 		ieee80211_hw_set(hw, USES_RSS);
13797 	}
13798 
13799 	wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
13800 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
13801 
13802 	/* TODO: Check if HT capability advertised from firmware is different
13803 	 * for each band for a dual band capable radio. It will be tricky to
13804 	 * handle it when the ht capability different for each band.
13805 	 */
13806 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS ||
13807 	    (is_6ghz && ab->hw_params->supports_dynamic_smps_6ghz))
13808 		wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
13809 
13810 	wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
13811 	wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
13812 
13813 	hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
13814 
13815 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
13816 	wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
13817 	wiphy->max_remain_on_channel_duration = 5000;
13818 
13819 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
13820 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
13821 				   NL80211_FEATURE_AP_SCAN;
13822 
13823 	wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
13824 
13825 	/* MLO is not yet supported so disable Wireless Extensions for now
13826 	 * to make sure ath12k users don't use it. This flag can be removed
13827 	 * once WIPHY_FLAG_SUPPORTS_MLO is enabled.
13828 	 */
13829 	wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
13830 
13831 	/* Copy over MLO related capabilities received from
13832 	 * WMI_SERVICE_READY_EXT2_EVENT if single_chip_mlo_supp is set.
13833 	 */
13834 	if (ab->ag->mlo_capable) {
13835 		ath12k_iftypes_ext_capa[2].eml_capabilities = cap->eml_cap;
13836 		ath12k_iftypes_ext_capa[2].mld_capa_and_ops = cap->mld_cap;
13837 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
13838 
13839 		ieee80211_hw_set(hw, MLO_MCAST_MULTI_LINK_TX);
13840 	}
13841 
13842 	hw->queues = ATH12K_HW_MAX_QUEUES;
13843 	wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
13844 	hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
13845 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT;
13846 
13847 	hw->vif_data_size = sizeof(struct ath12k_vif);
13848 	hw->sta_data_size = sizeof(struct ath12k_sta);
13849 	hw->extra_tx_headroom = ab->hw_params->iova_mask;
13850 
13851 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
13852 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
13853 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
13854 
13855 	wiphy->cipher_suites = cipher_suites;
13856 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
13857 
13858 	wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
13859 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(ath12k_iftypes_ext_capa);
13860 
13861 	wiphy->mbssid_max_interfaces = mbssid_max_interfaces;
13862 	wiphy->ema_max_profile_periodicity = TARGET_EMA_MAX_PROFILE_PERIOD;
13863 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
13864 
13865 	if (is_6ghz) {
13866 		wiphy_ext_feature_set(wiphy,
13867 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
13868 		wiphy_ext_feature_set(wiphy,
13869 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
13870 	}
13871 
13872 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PUNCT);
13873 	if (test_bit(WMI_TLV_SERVICE_BEACON_PROTECTION_SUPPORT, ab->wmi_ab.svc_map))
13874 		wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_BEACON_PROTECTION);
13875 
13876 	ath12k_reg_init(hw);
13877 
13878 	if (!is_raw_mode) {
13879 		hw->netdev_features = NETIF_F_HW_CSUM;
13880 		ieee80211_hw_set(hw, SW_CRYPTO_CONTROL);
13881 		ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
13882 	}
13883 
13884 	if (test_bit(WMI_TLV_SERVICE_NLO, ar->wmi->wmi_ab->svc_map)) {
13885 		wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
13886 		wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
13887 		wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
13888 		wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
13889 		wiphy->max_sched_scan_plan_interval =
13890 					WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
13891 		wiphy->max_sched_scan_plan_iterations =
13892 					WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
13893 		wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
13894 	}
13895 
13896 	ret = ath12k_wow_init(ar);
13897 	if (ret) {
13898 		ath12k_warn(ar->ab, "failed to init wow: %d\n", ret);
13899 		goto err_cleanup_if_combs;
13900 	}
13901 
13902 	/* Boot-time regulatory updates have already been processed.
13903 	 * Mark them as complete now, because after registration,
13904 	 * cfg80211 will notify us again if there are any pending hints.
13905 	 * We need to wait for those hints to be processed, so it's
13906 	 * important to mark the boot-time updates as complete before
13907 	 * proceeding with registration.
13908 	 */
13909 	for_each_ar(ah, ar, i)
13910 		complete_all(&ar->regd_update_completed);
13911 
13912 	ret = ieee80211_register_hw(hw);
13913 	if (ret) {
13914 		ath12k_err(ab, "ieee80211 registration failed: %d\n", ret);
13915 		goto err_cleanup_if_combs;
13916 	}
13917 
13918 	if (is_monitor_disable)
13919 		/* There's a race between calling ieee80211_register_hw()
13920 		 * and here where the monitor mode is enabled for a little
13921 		 * while. But that time is so short and in practise it make
13922 		 * a difference in real life.
13923 		 */
13924 		wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
13925 
13926 	for_each_ar(ah, ar, i) {
13927 		/* Apply the regd received during initialization */
13928 		ret = ath12k_regd_update(ar, true);
13929 		if (ret) {
13930 			ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
13931 			goto err_unregister_hw;
13932 		}
13933 
13934 		if (ar->ab->hw_params->current_cc_support && ab->new_alpha2[0]) {
13935 			struct wmi_set_current_country_arg current_cc = {};
13936 
13937 			memcpy(&current_cc.alpha2, ab->new_alpha2, 2);
13938 			memcpy(&ar->alpha2, ab->new_alpha2, 2);
13939 
13940 			reinit_completion(&ar->regd_update_completed);
13941 
13942 			ret = ath12k_wmi_send_set_current_country_cmd(ar, &current_cc);
13943 			if (ret)
13944 				ath12k_warn(ar->ab,
13945 					    "failed set cc code for mac register: %d\n",
13946 					    ret);
13947 		}
13948 
13949 		ath12k_fw_stats_init(ar);
13950 		ath12k_debugfs_register(ar);
13951 	}
13952 
13953 	return 0;
13954 
13955 err_unregister_hw:
13956 	for_each_ar(ah, ar, i)
13957 		ath12k_debugfs_unregister(ar);
13958 
13959 	ieee80211_unregister_hw(hw);
13960 
13961 err_cleanup_if_combs:
13962 	ath12k_mac_cleanup_iface_combinations(ah);
13963 
13964 err_complete_cleanup_unregister:
13965 	i = ah->num_radio;
13966 
13967 err_cleanup_unregister:
13968 	for (j = 0; j < i; j++) {
13969 		ar = ath12k_ah_to_ar(ah, j);
13970 		ath12k_mac_cleanup_unregister(ar);
13971 	}
13972 
13973 	SET_IEEE80211_DEV(hw, NULL);
13974 
13975 	return ret;
13976 }
13977 
ath12k_mac_setup(struct ath12k * ar)13978 static void ath12k_mac_setup(struct ath12k *ar)
13979 {
13980 	struct ath12k_base *ab = ar->ab;
13981 	struct ath12k_pdev *pdev = ar->pdev;
13982 	u8 pdev_idx = ar->pdev_idx;
13983 
13984 	ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, pdev_idx);
13985 
13986 	ar->wmi = &ab->wmi_ab.wmi[pdev_idx];
13987 	/* FIXME: wmi[0] is already initialized during attach,
13988 	 * Should we do this again?
13989 	 */
13990 	ath12k_wmi_pdev_attach(ab, pdev_idx);
13991 
13992 	ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
13993 	ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
13994 	ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
13995 	ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
13996 	ar->scan.arvif = NULL;
13997 	ar->vdev_id_11d_scan = ATH12K_11D_INVALID_VDEV_ID;
13998 
13999 	spin_lock_init(&ar->data_lock);
14000 	INIT_LIST_HEAD(&ar->arvifs);
14001 	INIT_LIST_HEAD(&ar->ppdu_stats_info);
14002 
14003 	init_completion(&ar->vdev_setup_done);
14004 	init_completion(&ar->vdev_delete_done);
14005 	init_completion(&ar->peer_assoc_done);
14006 	init_completion(&ar->peer_delete_done);
14007 	init_completion(&ar->install_key_done);
14008 	init_completion(&ar->bss_survey_done);
14009 	init_completion(&ar->scan.started);
14010 	init_completion(&ar->scan.completed);
14011 	init_completion(&ar->scan.on_channel);
14012 	init_completion(&ar->mlo_setup_done);
14013 	init_completion(&ar->completed_11d_scan);
14014 	init_completion(&ar->regd_update_completed);
14015 
14016 	INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
14017 	wiphy_work_init(&ar->scan.vdev_clean_wk, ath12k_scan_vdev_clean_work);
14018 	INIT_WORK(&ar->regd_channel_update_work, ath12k_regd_update_chan_list_work);
14019 	INIT_LIST_HEAD(&ar->regd_channel_update_queue);
14020 	INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
14021 
14022 	wiphy_work_init(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
14023 	skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
14024 
14025 	ar->monitor_vdev_id = -1;
14026 	ar->monitor_vdev_created = false;
14027 	ar->monitor_started = false;
14028 }
14029 
__ath12k_mac_mlo_setup(struct ath12k * ar)14030 static int __ath12k_mac_mlo_setup(struct ath12k *ar)
14031 {
14032 	u8 num_link = 0, partner_link_id[ATH12K_GROUP_MAX_RADIO] = {};
14033 	struct ath12k_base *partner_ab, *ab = ar->ab;
14034 	struct ath12k_hw_group *ag = ab->ag;
14035 	struct wmi_mlo_setup_arg mlo = {};
14036 	struct ath12k_pdev *pdev;
14037 	unsigned long time_left;
14038 	int i, j, ret;
14039 
14040 	lockdep_assert_held(&ag->mutex);
14041 
14042 	reinit_completion(&ar->mlo_setup_done);
14043 
14044 	for (i = 0; i < ag->num_devices; i++) {
14045 		partner_ab = ag->ab[i];
14046 
14047 		for (j = 0; j < partner_ab->num_radios; j++) {
14048 			pdev = &partner_ab->pdevs[j];
14049 
14050 			/* Avoid the self link */
14051 			if (ar == pdev->ar)
14052 				continue;
14053 
14054 			partner_link_id[num_link] = pdev->hw_link_id;
14055 			num_link++;
14056 
14057 			ath12k_dbg(ab, ATH12K_DBG_MAC, "device %d pdev %d hw_link_id %d num_link %d\n",
14058 				   i, j, pdev->hw_link_id, num_link);
14059 		}
14060 	}
14061 
14062 	if (num_link == 0)
14063 		return 0;
14064 
14065 	mlo.group_id = cpu_to_le32(ag->id);
14066 	mlo.partner_link_id = partner_link_id;
14067 	mlo.num_partner_links = num_link;
14068 	ar->mlo_setup_status = 0;
14069 
14070 	ath12k_dbg(ab, ATH12K_DBG_MAC, "group id %d num_link %d\n", ag->id, num_link);
14071 
14072 	ret = ath12k_wmi_mlo_setup(ar, &mlo);
14073 	if (ret) {
14074 		ath12k_err(ab, "failed to send  setup MLO WMI command for pdev %d: %d\n",
14075 			   ar->pdev_idx, ret);
14076 		return ret;
14077 	}
14078 
14079 	time_left = wait_for_completion_timeout(&ar->mlo_setup_done,
14080 						WMI_MLO_CMD_TIMEOUT_HZ);
14081 
14082 	if (!time_left || ar->mlo_setup_status)
14083 		return ar->mlo_setup_status ? : -ETIMEDOUT;
14084 
14085 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo setup done for pdev %d\n", ar->pdev_idx);
14086 
14087 	return 0;
14088 }
14089 
__ath12k_mac_mlo_teardown(struct ath12k * ar)14090 static int __ath12k_mac_mlo_teardown(struct ath12k *ar)
14091 {
14092 	struct ath12k_base *ab = ar->ab;
14093 	int ret;
14094 	u8 num_link;
14095 
14096 	if (test_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags))
14097 		return 0;
14098 
14099 	num_link = ath12k_get_num_partner_link(ar);
14100 
14101 	if (num_link == 0)
14102 		return 0;
14103 
14104 	ret = ath12k_wmi_mlo_teardown(ar);
14105 	if (ret) {
14106 		ath12k_warn(ab, "failed to send MLO teardown WMI command for pdev %d: %d\n",
14107 			    ar->pdev_idx, ret);
14108 		return ret;
14109 	}
14110 
14111 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mlo teardown for pdev %d\n", ar->pdev_idx);
14112 
14113 	return 0;
14114 }
14115 
ath12k_mac_mlo_setup(struct ath12k_hw_group * ag)14116 int ath12k_mac_mlo_setup(struct ath12k_hw_group *ag)
14117 {
14118 	struct ath12k_hw *ah;
14119 	struct ath12k *ar;
14120 	int ret;
14121 	int i, j;
14122 
14123 	for (i = 0; i < ag->num_hw; i++) {
14124 		ah = ag->ah[i];
14125 		if (!ah)
14126 			continue;
14127 
14128 		for_each_ar(ah, ar, j) {
14129 			ar = &ah->radio[j];
14130 			ret = __ath12k_mac_mlo_setup(ar);
14131 			if (ret) {
14132 				ath12k_err(ar->ab, "failed to setup MLO: %d\n", ret);
14133 				goto err_setup;
14134 			}
14135 		}
14136 	}
14137 
14138 	return 0;
14139 
14140 err_setup:
14141 	for (i = i - 1; i >= 0; i--) {
14142 		ah = ag->ah[i];
14143 		if (!ah)
14144 			continue;
14145 
14146 		for (j = j - 1; j >= 0; j--) {
14147 			ar = &ah->radio[j];
14148 			if (!ar)
14149 				continue;
14150 
14151 			__ath12k_mac_mlo_teardown(ar);
14152 		}
14153 	}
14154 
14155 	return ret;
14156 }
14157 
ath12k_mac_mlo_teardown(struct ath12k_hw_group * ag)14158 void ath12k_mac_mlo_teardown(struct ath12k_hw_group *ag)
14159 {
14160 	struct ath12k_hw *ah;
14161 	struct ath12k *ar;
14162 	int ret, i, j;
14163 
14164 	for (i = 0; i < ag->num_hw; i++) {
14165 		ah = ag->ah[i];
14166 		if (!ah)
14167 			continue;
14168 
14169 		for_each_ar(ah, ar, j) {
14170 			ar = &ah->radio[j];
14171 			ret = __ath12k_mac_mlo_teardown(ar);
14172 			if (ret) {
14173 				ath12k_err(ar->ab, "failed to teardown MLO: %d\n", ret);
14174 				break;
14175 			}
14176 		}
14177 	}
14178 }
14179 
ath12k_mac_register(struct ath12k_hw_group * ag)14180 int ath12k_mac_register(struct ath12k_hw_group *ag)
14181 {
14182 	struct ath12k_hw *ah;
14183 	int i;
14184 	int ret;
14185 
14186 	for (i = 0; i < ag->num_hw; i++) {
14187 		ah = ath12k_ag_to_ah(ag, i);
14188 
14189 		ret = ath12k_mac_hw_register(ah);
14190 		if (ret)
14191 			goto err;
14192 	}
14193 
14194 	return 0;
14195 
14196 err:
14197 	for (i = i - 1; i >= 0; i--) {
14198 		ah = ath12k_ag_to_ah(ag, i);
14199 		if (!ah)
14200 			continue;
14201 
14202 		ath12k_mac_hw_unregister(ah);
14203 	}
14204 
14205 	return ret;
14206 }
14207 
ath12k_mac_unregister(struct ath12k_hw_group * ag)14208 void ath12k_mac_unregister(struct ath12k_hw_group *ag)
14209 {
14210 	struct ath12k_hw *ah;
14211 	int i;
14212 
14213 	for (i = ag->num_hw - 1; i >= 0; i--) {
14214 		ah = ath12k_ag_to_ah(ag, i);
14215 		if (!ah)
14216 			continue;
14217 
14218 		ath12k_mac_hw_unregister(ah);
14219 	}
14220 }
14221 
ath12k_mac_hw_destroy(struct ath12k_hw * ah)14222 static void ath12k_mac_hw_destroy(struct ath12k_hw *ah)
14223 {
14224 	ieee80211_free_hw(ah->hw);
14225 }
14226 
ath12k_mac_hw_allocate(struct ath12k_hw_group * ag,struct ath12k_pdev_map * pdev_map,u8 num_pdev_map)14227 static struct ath12k_hw *ath12k_mac_hw_allocate(struct ath12k_hw_group *ag,
14228 						struct ath12k_pdev_map *pdev_map,
14229 						u8 num_pdev_map)
14230 {
14231 	struct ieee80211_hw *hw;
14232 	struct ath12k *ar;
14233 	struct ath12k_base *ab;
14234 	struct ath12k_pdev *pdev;
14235 	struct ath12k_hw *ah;
14236 	int i;
14237 	u8 pdev_idx;
14238 
14239 	hw = ieee80211_alloc_hw(struct_size(ah, radio, num_pdev_map),
14240 				&ath12k_ops);
14241 	if (!hw)
14242 		return NULL;
14243 
14244 	ah = ath12k_hw_to_ah(hw);
14245 	ah->hw = hw;
14246 	ah->num_radio = num_pdev_map;
14247 
14248 	mutex_init(&ah->hw_mutex);
14249 	INIT_LIST_HEAD(&ah->ml_peers);
14250 
14251 	for (i = 0; i < num_pdev_map; i++) {
14252 		ab = pdev_map[i].ab;
14253 		pdev_idx = pdev_map[i].pdev_idx;
14254 		pdev = &ab->pdevs[pdev_idx];
14255 
14256 		ar = ath12k_ah_to_ar(ah, i);
14257 		ar->ah = ah;
14258 		ar->ab = ab;
14259 		ar->hw_link_id = pdev->hw_link_id;
14260 		ar->pdev = pdev;
14261 		ar->pdev_idx = pdev_idx;
14262 		pdev->ar = ar;
14263 
14264 		ag->hw_links[ar->hw_link_id].device_id = ab->device_id;
14265 		ag->hw_links[ar->hw_link_id].pdev_idx = pdev_idx;
14266 
14267 		ath12k_mac_setup(ar);
14268 		ath12k_dp_pdev_pre_alloc(ar);
14269 	}
14270 
14271 	return ah;
14272 }
14273 
ath12k_mac_destroy(struct ath12k_hw_group * ag)14274 void ath12k_mac_destroy(struct ath12k_hw_group *ag)
14275 {
14276 	struct ath12k_pdev *pdev;
14277 	struct ath12k_base *ab = ag->ab[0];
14278 	int i, j;
14279 	struct ath12k_hw *ah;
14280 
14281 	for (i = 0; i < ag->num_devices; i++) {
14282 		ab = ag->ab[i];
14283 		if (!ab)
14284 			continue;
14285 
14286 		for (j = 0; j < ab->num_radios; j++) {
14287 			pdev = &ab->pdevs[j];
14288 			if (!pdev->ar)
14289 				continue;
14290 			pdev->ar = NULL;
14291 		}
14292 	}
14293 
14294 	for (i = 0; i < ag->num_hw; i++) {
14295 		ah = ath12k_ag_to_ah(ag, i);
14296 		if (!ah)
14297 			continue;
14298 
14299 		ath12k_mac_hw_destroy(ah);
14300 		ath12k_ag_set_ah(ag, i, NULL);
14301 	}
14302 }
14303 
ath12k_mac_set_device_defaults(struct ath12k_base * ab)14304 static void ath12k_mac_set_device_defaults(struct ath12k_base *ab)
14305 {
14306 	int total_vdev;
14307 
14308 	/* Initialize channel counters frequency value in hertz */
14309 	ab->cc_freq_hz = 320000;
14310 	total_vdev = ab->num_radios * TARGET_NUM_VDEVS(ab);
14311 	ab->free_vdev_map = (1LL << total_vdev) - 1;
14312 }
14313 
ath12k_mac_allocate(struct ath12k_hw_group * ag)14314 int ath12k_mac_allocate(struct ath12k_hw_group *ag)
14315 {
14316 	struct ath12k_pdev_map pdev_map[ATH12K_GROUP_MAX_RADIO];
14317 	int mac_id, device_id, total_radio, num_hw;
14318 	struct ath12k_base *ab;
14319 	struct ath12k_hw *ah;
14320 	int ret, i, j;
14321 	u8 radio_per_hw;
14322 
14323 	total_radio = 0;
14324 	for (i = 0; i < ag->num_devices; i++) {
14325 		ab = ag->ab[i];
14326 		if (!ab)
14327 			continue;
14328 
14329 		ath12k_debugfs_pdev_create(ab);
14330 		ath12k_mac_set_device_defaults(ab);
14331 		total_radio += ab->num_radios;
14332 	}
14333 
14334 	if (!total_radio)
14335 		return -EINVAL;
14336 
14337 	if (WARN_ON(total_radio > ATH12K_GROUP_MAX_RADIO))
14338 		return -ENOSPC;
14339 
14340 	/* All pdev get combined and register as single wiphy based on
14341 	 * hardware group which participate in multi-link operation else
14342 	 * each pdev get register separately.
14343 	 */
14344 	if (ag->mlo_capable)
14345 		radio_per_hw = total_radio;
14346 	else
14347 		radio_per_hw = 1;
14348 
14349 	num_hw = total_radio / radio_per_hw;
14350 
14351 	ag->num_hw = 0;
14352 	device_id = 0;
14353 	mac_id = 0;
14354 	for (i = 0; i < num_hw; i++) {
14355 		for (j = 0; j < radio_per_hw; j++) {
14356 			if (device_id >= ag->num_devices || !ag->ab[device_id]) {
14357 				ret = -ENOSPC;
14358 				goto err;
14359 			}
14360 
14361 			ab = ag->ab[device_id];
14362 			pdev_map[j].ab = ab;
14363 			pdev_map[j].pdev_idx = mac_id;
14364 			mac_id++;
14365 
14366 			/* If mac_id falls beyond the current device MACs then
14367 			 * move to next device
14368 			 */
14369 			if (mac_id >= ab->num_radios) {
14370 				mac_id = 0;
14371 				device_id++;
14372 			}
14373 		}
14374 
14375 		ab = pdev_map->ab;
14376 
14377 		ah = ath12k_mac_hw_allocate(ag, pdev_map, radio_per_hw);
14378 		if (!ah) {
14379 			ath12k_warn(ab, "failed to allocate mac80211 hw device for hw_idx %d\n",
14380 				    i);
14381 			ret = -ENOMEM;
14382 			goto err;
14383 		}
14384 
14385 		ah->dev = ab->dev;
14386 
14387 		ag->ah[i] = ah;
14388 		ag->num_hw++;
14389 	}
14390 
14391 	return 0;
14392 
14393 err:
14394 	for (i = i - 1; i >= 0; i--) {
14395 		ah = ath12k_ag_to_ah(ag, i);
14396 		if (!ah)
14397 			continue;
14398 
14399 		ath12k_mac_hw_destroy(ah);
14400 		ath12k_ag_set_ah(ag, i, NULL);
14401 	}
14402 
14403 	return ret;
14404 }
14405 
ath12k_mac_vif_set_keepalive(struct ath12k_link_vif * arvif,enum wmi_sta_keepalive_method method,u32 interval)14406 int ath12k_mac_vif_set_keepalive(struct ath12k_link_vif *arvif,
14407 				 enum wmi_sta_keepalive_method method,
14408 				 u32 interval)
14409 {
14410 	struct wmi_sta_keepalive_arg arg = {};
14411 	struct ath12k *ar = arvif->ar;
14412 	int ret;
14413 
14414 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
14415 
14416 	if (arvif->ahvif->vdev_type != WMI_VDEV_TYPE_STA)
14417 		return 0;
14418 
14419 	if (!test_bit(WMI_TLV_SERVICE_STA_KEEP_ALIVE, ar->ab->wmi_ab.svc_map))
14420 		return 0;
14421 
14422 	arg.vdev_id = arvif->vdev_id;
14423 	arg.enabled = 1;
14424 	arg.method = method;
14425 	arg.interval = interval;
14426 
14427 	ret = ath12k_wmi_sta_keepalive(ar, &arg);
14428 	if (ret) {
14429 		ath12k_warn(ar->ab, "failed to set keepalive on vdev %i: %d\n",
14430 			    arvif->vdev_id, ret);
14431 		return ret;
14432 	}
14433 
14434 	return 0;
14435 }
14436