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