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