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