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