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