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