xref: /openbmc/linux/drivers/net/wireless/ath/ath12k/mac.c (revision 0a9445aa)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 
7 #include <net/mac80211.h>
8 #include <linux/etherdevice.h>
9 #include "mac.h"
10 #include "core.h"
11 #include "debug.h"
12 #include "wmi.h"
13 #include "hw.h"
14 #include "dp_tx.h"
15 #include "dp_rx.h"
16 #include "peer.h"
17 
18 #define CHAN2G(_channel, _freq, _flags) { \
19 	.band                   = NL80211_BAND_2GHZ, \
20 	.hw_value               = (_channel), \
21 	.center_freq            = (_freq), \
22 	.flags                  = (_flags), \
23 	.max_antenna_gain       = 0, \
24 	.max_power              = 30, \
25 }
26 
27 #define CHAN5G(_channel, _freq, _flags) { \
28 	.band                   = NL80211_BAND_5GHZ, \
29 	.hw_value               = (_channel), \
30 	.center_freq            = (_freq), \
31 	.flags                  = (_flags), \
32 	.max_antenna_gain       = 0, \
33 	.max_power              = 30, \
34 }
35 
36 #define CHAN6G(_channel, _freq, _flags) { \
37 	.band                   = NL80211_BAND_6GHZ, \
38 	.hw_value               = (_channel), \
39 	.center_freq            = (_freq), \
40 	.flags                  = (_flags), \
41 	.max_antenna_gain       = 0, \
42 	.max_power              = 30, \
43 }
44 
45 static const struct ieee80211_channel ath12k_2ghz_channels[] = {
46 	CHAN2G(1, 2412, 0),
47 	CHAN2G(2, 2417, 0),
48 	CHAN2G(3, 2422, 0),
49 	CHAN2G(4, 2427, 0),
50 	CHAN2G(5, 2432, 0),
51 	CHAN2G(6, 2437, 0),
52 	CHAN2G(7, 2442, 0),
53 	CHAN2G(8, 2447, 0),
54 	CHAN2G(9, 2452, 0),
55 	CHAN2G(10, 2457, 0),
56 	CHAN2G(11, 2462, 0),
57 	CHAN2G(12, 2467, 0),
58 	CHAN2G(13, 2472, 0),
59 	CHAN2G(14, 2484, 0),
60 };
61 
62 static const struct ieee80211_channel ath12k_5ghz_channels[] = {
63 	CHAN5G(36, 5180, 0),
64 	CHAN5G(40, 5200, 0),
65 	CHAN5G(44, 5220, 0),
66 	CHAN5G(48, 5240, 0),
67 	CHAN5G(52, 5260, 0),
68 	CHAN5G(56, 5280, 0),
69 	CHAN5G(60, 5300, 0),
70 	CHAN5G(64, 5320, 0),
71 	CHAN5G(100, 5500, 0),
72 	CHAN5G(104, 5520, 0),
73 	CHAN5G(108, 5540, 0),
74 	CHAN5G(112, 5560, 0),
75 	CHAN5G(116, 5580, 0),
76 	CHAN5G(120, 5600, 0),
77 	CHAN5G(124, 5620, 0),
78 	CHAN5G(128, 5640, 0),
79 	CHAN5G(132, 5660, 0),
80 	CHAN5G(136, 5680, 0),
81 	CHAN5G(140, 5700, 0),
82 	CHAN5G(144, 5720, 0),
83 	CHAN5G(149, 5745, 0),
84 	CHAN5G(153, 5765, 0),
85 	CHAN5G(157, 5785, 0),
86 	CHAN5G(161, 5805, 0),
87 	CHAN5G(165, 5825, 0),
88 	CHAN5G(169, 5845, 0),
89 	CHAN5G(173, 5865, 0),
90 };
91 
92 static const struct ieee80211_channel ath12k_6ghz_channels[] = {
93 	CHAN6G(1, 5955, 0),
94 	CHAN6G(5, 5975, 0),
95 	CHAN6G(9, 5995, 0),
96 	CHAN6G(13, 6015, 0),
97 	CHAN6G(17, 6035, 0),
98 	CHAN6G(21, 6055, 0),
99 	CHAN6G(25, 6075, 0),
100 	CHAN6G(29, 6095, 0),
101 	CHAN6G(33, 6115, 0),
102 	CHAN6G(37, 6135, 0),
103 	CHAN6G(41, 6155, 0),
104 	CHAN6G(45, 6175, 0),
105 	CHAN6G(49, 6195, 0),
106 	CHAN6G(53, 6215, 0),
107 	CHAN6G(57, 6235, 0),
108 	CHAN6G(61, 6255, 0),
109 	CHAN6G(65, 6275, 0),
110 	CHAN6G(69, 6295, 0),
111 	CHAN6G(73, 6315, 0),
112 	CHAN6G(77, 6335, 0),
113 	CHAN6G(81, 6355, 0),
114 	CHAN6G(85, 6375, 0),
115 	CHAN6G(89, 6395, 0),
116 	CHAN6G(93, 6415, 0),
117 	CHAN6G(97, 6435, 0),
118 	CHAN6G(101, 6455, 0),
119 	CHAN6G(105, 6475, 0),
120 	CHAN6G(109, 6495, 0),
121 	CHAN6G(113, 6515, 0),
122 	CHAN6G(117, 6535, 0),
123 	CHAN6G(121, 6555, 0),
124 	CHAN6G(125, 6575, 0),
125 	CHAN6G(129, 6595, 0),
126 	CHAN6G(133, 6615, 0),
127 	CHAN6G(137, 6635, 0),
128 	CHAN6G(141, 6655, 0),
129 	CHAN6G(145, 6675, 0),
130 	CHAN6G(149, 6695, 0),
131 	CHAN6G(153, 6715, 0),
132 	CHAN6G(157, 6735, 0),
133 	CHAN6G(161, 6755, 0),
134 	CHAN6G(165, 6775, 0),
135 	CHAN6G(169, 6795, 0),
136 	CHAN6G(173, 6815, 0),
137 	CHAN6G(177, 6835, 0),
138 	CHAN6G(181, 6855, 0),
139 	CHAN6G(185, 6875, 0),
140 	CHAN6G(189, 6895, 0),
141 	CHAN6G(193, 6915, 0),
142 	CHAN6G(197, 6935, 0),
143 	CHAN6G(201, 6955, 0),
144 	CHAN6G(205, 6975, 0),
145 	CHAN6G(209, 6995, 0),
146 	CHAN6G(213, 7015, 0),
147 	CHAN6G(217, 7035, 0),
148 	CHAN6G(221, 7055, 0),
149 	CHAN6G(225, 7075, 0),
150 	CHAN6G(229, 7095, 0),
151 	CHAN6G(233, 7115, 0),
152 };
153 
154 static struct ieee80211_rate ath12k_legacy_rates[] = {
155 	{ .bitrate = 10,
156 	  .hw_value = ATH12K_HW_RATE_CCK_LP_1M },
157 	{ .bitrate = 20,
158 	  .hw_value = ATH12K_HW_RATE_CCK_LP_2M,
159 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_2M,
160 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
161 	{ .bitrate = 55,
162 	  .hw_value = ATH12K_HW_RATE_CCK_LP_5_5M,
163 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_5_5M,
164 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
165 	{ .bitrate = 110,
166 	  .hw_value = ATH12K_HW_RATE_CCK_LP_11M,
167 	  .hw_value_short = ATH12K_HW_RATE_CCK_SP_11M,
168 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
169 
170 	{ .bitrate = 60, .hw_value = ATH12K_HW_RATE_OFDM_6M },
171 	{ .bitrate = 90, .hw_value = ATH12K_HW_RATE_OFDM_9M },
172 	{ .bitrate = 120, .hw_value = ATH12K_HW_RATE_OFDM_12M },
173 	{ .bitrate = 180, .hw_value = ATH12K_HW_RATE_OFDM_18M },
174 	{ .bitrate = 240, .hw_value = ATH12K_HW_RATE_OFDM_24M },
175 	{ .bitrate = 360, .hw_value = ATH12K_HW_RATE_OFDM_36M },
176 	{ .bitrate = 480, .hw_value = ATH12K_HW_RATE_OFDM_48M },
177 	{ .bitrate = 540, .hw_value = ATH12K_HW_RATE_OFDM_54M },
178 };
179 
180 static const int
181 ath12k_phymodes[NUM_NL80211_BANDS][ATH12K_CHAN_WIDTH_NUM] = {
182 	[NL80211_BAND_2GHZ] = {
183 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
184 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
185 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20_2G,
186 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20_2G,
187 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40_2G,
188 			[NL80211_CHAN_WIDTH_80] = MODE_UNKNOWN,
189 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
190 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
191 			[NL80211_CHAN_WIDTH_320] = MODE_UNKNOWN,
192 	},
193 	[NL80211_BAND_5GHZ] = {
194 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
195 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
196 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
197 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
198 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
199 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
200 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
201 			[NL80211_CHAN_WIDTH_80P80] = MODE_11BE_EHT80_80,
202 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
203 	},
204 	[NL80211_BAND_6GHZ] = {
205 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
206 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
207 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11BE_EHT20,
208 			[NL80211_CHAN_WIDTH_20] = MODE_11BE_EHT20,
209 			[NL80211_CHAN_WIDTH_40] = MODE_11BE_EHT40,
210 			[NL80211_CHAN_WIDTH_80] = MODE_11BE_EHT80,
211 			[NL80211_CHAN_WIDTH_160] = MODE_11BE_EHT160,
212 			[NL80211_CHAN_WIDTH_80P80] = MODE_11BE_EHT80_80,
213 			[NL80211_CHAN_WIDTH_320] = MODE_11BE_EHT320,
214 	},
215 
216 };
217 
218 const struct htt_rx_ring_tlv_filter ath12k_mac_mon_status_filter_default = {
219 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
220 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
221 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE,
222 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
223 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
224 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
225 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
226 			     HTT_RX_FP_CTRL_FILTER_FLASG3
227 };
228 
229 #define ATH12K_MAC_FIRST_OFDM_RATE_IDX 4
230 #define ath12k_g_rates ath12k_legacy_rates
231 #define ath12k_g_rates_size (ARRAY_SIZE(ath12k_legacy_rates))
232 #define ath12k_a_rates (ath12k_legacy_rates + 4)
233 #define ath12k_a_rates_size (ARRAY_SIZE(ath12k_legacy_rates) - 4)
234 
235 #define ATH12K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
236 
237 static const u32 ath12k_smps_map[] = {
238 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
239 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
240 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
241 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
242 };
243 
244 static int ath12k_start_vdev_delay(struct ieee80211_hw *hw,
245 				   struct ieee80211_vif *vif);
246 
ath12k_mac_phymode_str(enum wmi_phy_mode mode)247 static const char *ath12k_mac_phymode_str(enum wmi_phy_mode mode)
248 {
249 	switch (mode) {
250 	case MODE_11A:
251 		return "11a";
252 	case MODE_11G:
253 		return "11g";
254 	case MODE_11B:
255 		return "11b";
256 	case MODE_11GONLY:
257 		return "11gonly";
258 	case MODE_11NA_HT20:
259 		return "11na-ht20";
260 	case MODE_11NG_HT20:
261 		return "11ng-ht20";
262 	case MODE_11NA_HT40:
263 		return "11na-ht40";
264 	case MODE_11NG_HT40:
265 		return "11ng-ht40";
266 	case MODE_11AC_VHT20:
267 		return "11ac-vht20";
268 	case MODE_11AC_VHT40:
269 		return "11ac-vht40";
270 	case MODE_11AC_VHT80:
271 		return "11ac-vht80";
272 	case MODE_11AC_VHT160:
273 		return "11ac-vht160";
274 	case MODE_11AC_VHT80_80:
275 		return "11ac-vht80+80";
276 	case MODE_11AC_VHT20_2G:
277 		return "11ac-vht20-2g";
278 	case MODE_11AC_VHT40_2G:
279 		return "11ac-vht40-2g";
280 	case MODE_11AC_VHT80_2G:
281 		return "11ac-vht80-2g";
282 	case MODE_11AX_HE20:
283 		return "11ax-he20";
284 	case MODE_11AX_HE40:
285 		return "11ax-he40";
286 	case MODE_11AX_HE80:
287 		return "11ax-he80";
288 	case MODE_11AX_HE80_80:
289 		return "11ax-he80+80";
290 	case MODE_11AX_HE160:
291 		return "11ax-he160";
292 	case MODE_11AX_HE20_2G:
293 		return "11ax-he20-2g";
294 	case MODE_11AX_HE40_2G:
295 		return "11ax-he40-2g";
296 	case MODE_11AX_HE80_2G:
297 		return "11ax-he80-2g";
298 	case MODE_11BE_EHT20:
299 		return "11be-eht20";
300 	case MODE_11BE_EHT40:
301 		return "11be-eht40";
302 	case MODE_11BE_EHT80:
303 		return "11be-eht80";
304 	case MODE_11BE_EHT80_80:
305 		return "11be-eht80+80";
306 	case MODE_11BE_EHT160:
307 		return "11be-eht160";
308 	case MODE_11BE_EHT160_160:
309 		return "11be-eht160+160";
310 	case MODE_11BE_EHT320:
311 		return "11be-eht320";
312 	case MODE_11BE_EHT20_2G:
313 		return "11be-eht20-2g";
314 	case MODE_11BE_EHT40_2G:
315 		return "11be-eht40-2g";
316 	case MODE_UNKNOWN:
317 		/* skip */
318 		break;
319 
320 		/* no default handler to allow compiler to check that the
321 		 * enum is fully handled
322 		 */
323 	}
324 
325 	return "<unknown>";
326 }
327 
328 enum rate_info_bw
ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)329 ath12k_mac_bw_to_mac80211_bw(enum ath12k_supported_bw bw)
330 {
331 	u8 ret = RATE_INFO_BW_20;
332 
333 	switch (bw) {
334 	case ATH12K_BW_20:
335 		ret = RATE_INFO_BW_20;
336 		break;
337 	case ATH12K_BW_40:
338 		ret = RATE_INFO_BW_40;
339 		break;
340 	case ATH12K_BW_80:
341 		ret = RATE_INFO_BW_80;
342 		break;
343 	case ATH12K_BW_160:
344 		ret = RATE_INFO_BW_160;
345 		break;
346 	}
347 
348 	return ret;
349 }
350 
ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)351 enum ath12k_supported_bw ath12k_mac_mac80211_bw_to_ath12k_bw(enum rate_info_bw bw)
352 {
353 	switch (bw) {
354 	case RATE_INFO_BW_20:
355 		return ATH12K_BW_20;
356 	case RATE_INFO_BW_40:
357 		return ATH12K_BW_40;
358 	case RATE_INFO_BW_80:
359 		return ATH12K_BW_80;
360 	case RATE_INFO_BW_160:
361 		return ATH12K_BW_160;
362 	default:
363 		return ATH12K_BW_20;
364 	}
365 }
366 
ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc,u8 preamble,u8 * rateidx,u16 * rate)367 int ath12k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
368 					  u16 *rate)
369 {
370 	/* As default, it is OFDM rates */
371 	int i = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
372 	int max_rates_idx = ath12k_g_rates_size;
373 
374 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
375 		hw_rc &= ~ATH12K_HW_RATECODE_CCK_SHORT_PREAM_MASK;
376 		i = 0;
377 		max_rates_idx = ATH12K_MAC_FIRST_OFDM_RATE_IDX;
378 	}
379 
380 	while (i < max_rates_idx) {
381 		if (hw_rc == ath12k_legacy_rates[i].hw_value) {
382 			*rateidx = i;
383 			*rate = ath12k_legacy_rates[i].bitrate;
384 			return 0;
385 		}
386 		i++;
387 	}
388 
389 	return -EINVAL;
390 }
391 
ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band * sband,u32 bitrate)392 u8 ath12k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
393 			     u32 bitrate)
394 {
395 	int i;
396 
397 	for (i = 0; i < sband->n_bitrates; i++)
398 		if (sband->bitrates[i].bitrate == bitrate)
399 			return i;
400 
401 	return 0;
402 }
403 
404 static u32
ath12k_mac_max_ht_nss(const u8 * ht_mcs_mask)405 ath12k_mac_max_ht_nss(const u8 *ht_mcs_mask)
406 {
407 	int nss;
408 
409 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
410 		if (ht_mcs_mask[nss])
411 			return nss + 1;
412 
413 	return 1;
414 }
415 
416 static u32
ath12k_mac_max_vht_nss(const u16 * vht_mcs_mask)417 ath12k_mac_max_vht_nss(const u16 *vht_mcs_mask)
418 {
419 	int nss;
420 
421 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
422 		if (vht_mcs_mask[nss])
423 			return nss + 1;
424 
425 	return 1;
426 }
427 
ath12k_parse_mpdudensity(u8 mpdudensity)428 static u8 ath12k_parse_mpdudensity(u8 mpdudensity)
429 {
430 /*  From IEEE Std 802.11-2020 defined values for "Minimum MPDU Start Spacing":
431  *   0 for no restriction
432  *   1 for 1/4 us
433  *   2 for 1/2 us
434  *   3 for 1 us
435  *   4 for 2 us
436  *   5 for 4 us
437  *   6 for 8 us
438  *   7 for 16 us
439  */
440 	switch (mpdudensity) {
441 	case 0:
442 		return 0;
443 	case 1:
444 	case 2:
445 	case 3:
446 	/* Our lower layer calculations limit our precision to
447 	 * 1 microsecond
448 	 */
449 		return 1;
450 	case 4:
451 		return 2;
452 	case 5:
453 		return 4;
454 	case 6:
455 		return 8;
456 	case 7:
457 		return 16;
458 	default:
459 		return 0;
460 	}
461 }
462 
ath12k_mac_vif_chan(struct ieee80211_vif * vif,struct cfg80211_chan_def * def)463 static int ath12k_mac_vif_chan(struct ieee80211_vif *vif,
464 			       struct cfg80211_chan_def *def)
465 {
466 	struct ieee80211_chanctx_conf *conf;
467 
468 	rcu_read_lock();
469 	conf = rcu_dereference(vif->bss_conf.chanctx_conf);
470 	if (!conf) {
471 		rcu_read_unlock();
472 		return -ENOENT;
473 	}
474 
475 	*def = conf->def;
476 	rcu_read_unlock();
477 
478 	return 0;
479 }
480 
ath12k_mac_bitrate_is_cck(int bitrate)481 static bool ath12k_mac_bitrate_is_cck(int bitrate)
482 {
483 	switch (bitrate) {
484 	case 10:
485 	case 20:
486 	case 55:
487 	case 110:
488 		return true;
489 	}
490 
491 	return false;
492 }
493 
ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band * sband,u8 hw_rate,bool cck)494 u8 ath12k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
495 			     u8 hw_rate, bool cck)
496 {
497 	const struct ieee80211_rate *rate;
498 	int i;
499 
500 	for (i = 0; i < sband->n_bitrates; i++) {
501 		rate = &sband->bitrates[i];
502 
503 		if (ath12k_mac_bitrate_is_cck(rate->bitrate) != cck)
504 			continue;
505 
506 		if (rate->hw_value == hw_rate)
507 			return i;
508 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
509 			 rate->hw_value_short == hw_rate)
510 			return i;
511 	}
512 
513 	return 0;
514 }
515 
ath12k_mac_bitrate_to_rate(int bitrate)516 static u8 ath12k_mac_bitrate_to_rate(int bitrate)
517 {
518 	return DIV_ROUND_UP(bitrate, 5) |
519 	       (ath12k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
520 }
521 
ath12k_get_arvif_iter(void * data,u8 * mac,struct ieee80211_vif * vif)522 static void ath12k_get_arvif_iter(void *data, u8 *mac,
523 				  struct ieee80211_vif *vif)
524 {
525 	struct ath12k_vif_iter *arvif_iter = data;
526 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
527 
528 	if (arvif->vdev_id == arvif_iter->vdev_id)
529 		arvif_iter->arvif = arvif;
530 }
531 
ath12k_mac_get_arvif(struct ath12k * ar,u32 vdev_id)532 struct ath12k_vif *ath12k_mac_get_arvif(struct ath12k *ar, u32 vdev_id)
533 {
534 	struct ath12k_vif_iter arvif_iter = {};
535 	u32 flags;
536 
537 	arvif_iter.vdev_id = vdev_id;
538 
539 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
540 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
541 						   flags,
542 						   ath12k_get_arvif_iter,
543 						   &arvif_iter);
544 	if (!arvif_iter.arvif) {
545 		ath12k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
546 		return NULL;
547 	}
548 
549 	return arvif_iter.arvif;
550 }
551 
ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base * ab,u32 vdev_id)552 struct ath12k_vif *ath12k_mac_get_arvif_by_vdev_id(struct ath12k_base *ab,
553 						   u32 vdev_id)
554 {
555 	int i;
556 	struct ath12k_pdev *pdev;
557 	struct ath12k_vif *arvif;
558 
559 	for (i = 0; i < ab->num_radios; i++) {
560 		pdev = rcu_dereference(ab->pdevs_active[i]);
561 		if (pdev && pdev->ar) {
562 			arvif = ath12k_mac_get_arvif(pdev->ar, vdev_id);
563 			if (arvif)
564 				return arvif;
565 		}
566 	}
567 
568 	return NULL;
569 }
570 
ath12k_mac_get_ar_by_vdev_id(struct ath12k_base * ab,u32 vdev_id)571 struct ath12k *ath12k_mac_get_ar_by_vdev_id(struct ath12k_base *ab, u32 vdev_id)
572 {
573 	int i;
574 	struct ath12k_pdev *pdev;
575 
576 	for (i = 0; i < ab->num_radios; i++) {
577 		pdev = rcu_dereference(ab->pdevs_active[i]);
578 		if (pdev && pdev->ar) {
579 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
580 				return pdev->ar;
581 		}
582 	}
583 
584 	return NULL;
585 }
586 
ath12k_mac_get_ar_by_pdev_id(struct ath12k_base * ab,u32 pdev_id)587 struct ath12k *ath12k_mac_get_ar_by_pdev_id(struct ath12k_base *ab, u32 pdev_id)
588 {
589 	int i;
590 	struct ath12k_pdev *pdev;
591 
592 	if (ab->hw_params->single_pdev_only) {
593 		pdev = rcu_dereference(ab->pdevs_active[0]);
594 		return pdev ? pdev->ar : NULL;
595 	}
596 
597 	if (WARN_ON(pdev_id > ab->num_radios))
598 		return NULL;
599 
600 	for (i = 0; i < ab->num_radios; i++) {
601 		pdev = rcu_dereference(ab->pdevs_active[i]);
602 
603 		if (pdev && pdev->pdev_id == pdev_id)
604 			return (pdev->ar ? pdev->ar : NULL);
605 	}
606 
607 	return NULL;
608 }
609 
ath12k_pdev_caps_update(struct ath12k * ar)610 static void ath12k_pdev_caps_update(struct ath12k *ar)
611 {
612 	struct ath12k_base *ab = ar->ab;
613 
614 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
615 
616 	/* FIXME: Set min_tx_power to ab->target_caps.hw_min_tx_power.
617 	 * But since the received value in svcrdy is same as hw_max_tx_power,
618 	 * we can set ar->min_tx_power to 0 currently until
619 	 * this is fixed in firmware
620 	 */
621 	ar->min_tx_power = 0;
622 
623 	ar->txpower_limit_2g = ar->max_tx_power;
624 	ar->txpower_limit_5g = ar->max_tx_power;
625 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
626 }
627 
ath12k_mac_txpower_recalc(struct ath12k * ar)628 static int ath12k_mac_txpower_recalc(struct ath12k *ar)
629 {
630 	struct ath12k_pdev *pdev = ar->pdev;
631 	struct ath12k_vif *arvif;
632 	int ret, txpower = -1;
633 	u32 param;
634 
635 	lockdep_assert_held(&ar->conf_mutex);
636 
637 	list_for_each_entry(arvif, &ar->arvifs, list) {
638 		if (arvif->txpower <= 0)
639 			continue;
640 
641 		if (txpower == -1)
642 			txpower = arvif->txpower;
643 		else
644 			txpower = min(txpower, arvif->txpower);
645 	}
646 
647 	if (txpower == -1)
648 		return 0;
649 
650 	/* txpwr is set as 2 units per dBm in FW*/
651 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
652 			ar->max_tx_power) * 2;
653 
654 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "txpower to set in hw %d\n",
655 		   txpower / 2);
656 
657 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
658 	    ar->txpower_limit_2g != txpower) {
659 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
660 		ret = ath12k_wmi_pdev_set_param(ar, param,
661 						txpower, ar->pdev->pdev_id);
662 		if (ret)
663 			goto fail;
664 		ar->txpower_limit_2g = txpower;
665 	}
666 
667 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
668 	    ar->txpower_limit_5g != txpower) {
669 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
670 		ret = ath12k_wmi_pdev_set_param(ar, param,
671 						txpower, ar->pdev->pdev_id);
672 		if (ret)
673 			goto fail;
674 		ar->txpower_limit_5g = txpower;
675 	}
676 
677 	return 0;
678 
679 fail:
680 	ath12k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
681 		    txpower / 2, param, ret);
682 	return ret;
683 }
684 
ath12k_recalc_rtscts_prot(struct ath12k_vif * arvif)685 static int ath12k_recalc_rtscts_prot(struct ath12k_vif *arvif)
686 {
687 	struct ath12k *ar = arvif->ar;
688 	u32 vdev_param, rts_cts;
689 	int ret;
690 
691 	lockdep_assert_held(&ar->conf_mutex);
692 
693 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
694 
695 	/* Enable RTS/CTS protection for sw retries (when legacy stations
696 	 * are in BSS) or by default only for second rate series.
697 	 * TODO: Check if we need to enable CTS 2 Self in any case
698 	 */
699 	rts_cts = WMI_USE_RTS_CTS;
700 
701 	if (arvif->num_legacy_stations > 0)
702 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
703 	else
704 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
705 
706 	/* Need not send duplicate param value to firmware */
707 	if (arvif->rtscts_prot_mode == rts_cts)
708 		return 0;
709 
710 	arvif->rtscts_prot_mode = rts_cts;
711 
712 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
713 		   arvif->vdev_id, rts_cts);
714 
715 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
716 					    vdev_param, rts_cts);
717 	if (ret)
718 		ath12k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
719 			    arvif->vdev_id, ret);
720 
721 	return ret;
722 }
723 
ath12k_mac_set_kickout(struct ath12k_vif * arvif)724 static int ath12k_mac_set_kickout(struct ath12k_vif *arvif)
725 {
726 	struct ath12k *ar = arvif->ar;
727 	u32 param;
728 	int ret;
729 
730 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
731 					ATH12K_KICKOUT_THRESHOLD,
732 					ar->pdev->pdev_id);
733 	if (ret) {
734 		ath12k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
735 			    arvif->vdev_id, ret);
736 		return ret;
737 	}
738 
739 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
740 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
741 					    ATH12K_KEEPALIVE_MIN_IDLE);
742 	if (ret) {
743 		ath12k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
744 			    arvif->vdev_id, ret);
745 		return ret;
746 	}
747 
748 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
749 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
750 					    ATH12K_KEEPALIVE_MAX_IDLE);
751 	if (ret) {
752 		ath12k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
753 			    arvif->vdev_id, ret);
754 		return ret;
755 	}
756 
757 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
758 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
759 					    ATH12K_KEEPALIVE_MAX_UNRESPONSIVE);
760 	if (ret) {
761 		ath12k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
762 			    arvif->vdev_id, ret);
763 		return ret;
764 	}
765 
766 	return 0;
767 }
768 
ath12k_mac_peer_cleanup_all(struct ath12k * ar)769 void ath12k_mac_peer_cleanup_all(struct ath12k *ar)
770 {
771 	struct ath12k_peer *peer, *tmp;
772 	struct ath12k_base *ab = ar->ab;
773 
774 	lockdep_assert_held(&ar->conf_mutex);
775 
776 	spin_lock_bh(&ab->base_lock);
777 	list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
778 		ath12k_dp_rx_peer_tid_cleanup(ar, peer);
779 		list_del(&peer->list);
780 		kfree(peer);
781 	}
782 	spin_unlock_bh(&ab->base_lock);
783 
784 	ar->num_peers = 0;
785 	ar->num_stations = 0;
786 }
787 
ath12k_mac_vdev_setup_sync(struct ath12k * ar)788 static int ath12k_mac_vdev_setup_sync(struct ath12k *ar)
789 {
790 	lockdep_assert_held(&ar->conf_mutex);
791 
792 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
793 		return -ESHUTDOWN;
794 
795 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev setup timeout %d\n",
796 		   ATH12K_VDEV_SETUP_TIMEOUT_HZ);
797 
798 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
799 					 ATH12K_VDEV_SETUP_TIMEOUT_HZ))
800 		return -ETIMEDOUT;
801 
802 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
803 }
804 
ath12k_monitor_vdev_up(struct ath12k * ar,int vdev_id)805 static int ath12k_monitor_vdev_up(struct ath12k *ar, int vdev_id)
806 {
807 	int ret;
808 
809 	ret = ath12k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
810 	if (ret) {
811 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
812 			    vdev_id, ret);
813 		return ret;
814 	}
815 
816 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
817 		   vdev_id);
818 	return 0;
819 }
820 
ath12k_mac_monitor_vdev_start(struct ath12k * ar,int vdev_id,struct cfg80211_chan_def * chandef)821 static int ath12k_mac_monitor_vdev_start(struct ath12k *ar, int vdev_id,
822 					 struct cfg80211_chan_def *chandef)
823 {
824 	struct ieee80211_channel *channel;
825 	struct wmi_vdev_start_req_arg arg = {};
826 	int ret;
827 
828 	lockdep_assert_held(&ar->conf_mutex);
829 
830 	channel = chandef->chan;
831 	arg.vdev_id = vdev_id;
832 	arg.freq = channel->center_freq;
833 	arg.band_center_freq1 = chandef->center_freq1;
834 	arg.band_center_freq2 = chandef->center_freq2;
835 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
836 	arg.chan_radar = !!(channel->flags & IEEE80211_CHAN_RADAR);
837 
838 	arg.min_power = 0;
839 	arg.max_power = channel->max_power;
840 	arg.max_reg_power = channel->max_reg_power;
841 	arg.max_antenna_gain = channel->max_antenna_gain;
842 
843 	arg.pref_tx_streams = ar->num_tx_chains;
844 	arg.pref_rx_streams = ar->num_rx_chains;
845 	arg.punct_bitmap = 0xFFFFFFFF;
846 
847 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
848 
849 	reinit_completion(&ar->vdev_setup_done);
850 	reinit_completion(&ar->vdev_delete_done);
851 
852 	ret = ath12k_wmi_vdev_start(ar, &arg, false);
853 	if (ret) {
854 		ath12k_warn(ar->ab, "failed to request monitor vdev %i start: %d\n",
855 			    vdev_id, ret);
856 		return ret;
857 	}
858 
859 	ret = ath12k_mac_vdev_setup_sync(ar);
860 	if (ret) {
861 		ath12k_warn(ar->ab, "failed to synchronize setup for monitor vdev %i start: %d\n",
862 			    vdev_id, ret);
863 		return ret;
864 	}
865 
866 	ret = ath12k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
867 	if (ret) {
868 		ath12k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
869 			    vdev_id, ret);
870 		goto vdev_stop;
871 	}
872 
873 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i started\n",
874 		   vdev_id);
875 	return 0;
876 
877 vdev_stop:
878 	ret = ath12k_wmi_vdev_stop(ar, vdev_id);
879 	if (ret)
880 		ath12k_warn(ar->ab, "failed to stop monitor vdev %i after start failure: %d\n",
881 			    vdev_id, ret);
882 	return ret;
883 }
884 
ath12k_mac_monitor_vdev_stop(struct ath12k * ar)885 static int ath12k_mac_monitor_vdev_stop(struct ath12k *ar)
886 {
887 	int ret;
888 
889 	lockdep_assert_held(&ar->conf_mutex);
890 
891 	reinit_completion(&ar->vdev_setup_done);
892 
893 	ret = ath12k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
894 	if (ret)
895 		ath12k_warn(ar->ab, "failed to request monitor vdev %i stop: %d\n",
896 			    ar->monitor_vdev_id, ret);
897 
898 	ret = ath12k_mac_vdev_setup_sync(ar);
899 	if (ret)
900 		ath12k_warn(ar->ab, "failed to synchronize monitor vdev %i stop: %d\n",
901 			    ar->monitor_vdev_id, ret);
902 
903 	ret = ath12k_wmi_vdev_down(ar, ar->monitor_vdev_id);
904 	if (ret)
905 		ath12k_warn(ar->ab, "failed to put down monitor vdev %i: %d\n",
906 			    ar->monitor_vdev_id, ret);
907 
908 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %i stopped\n",
909 		   ar->monitor_vdev_id);
910 	return ret;
911 }
912 
ath12k_mac_monitor_vdev_create(struct ath12k * ar)913 static int ath12k_mac_monitor_vdev_create(struct ath12k *ar)
914 {
915 	struct ath12k_pdev *pdev = ar->pdev;
916 	struct ath12k_wmi_vdev_create_arg arg = {};
917 	int bit, ret;
918 	u8 tmp_addr[6];
919 	u16 nss;
920 
921 	lockdep_assert_held(&ar->conf_mutex);
922 
923 	if (ar->monitor_vdev_created)
924 		return 0;
925 
926 	if (ar->ab->free_vdev_map == 0) {
927 		ath12k_warn(ar->ab, "failed to find free vdev id for monitor vdev\n");
928 		return -ENOMEM;
929 	}
930 
931 	bit = __ffs64(ar->ab->free_vdev_map);
932 
933 	ar->monitor_vdev_id = bit;
934 
935 	arg.if_id = ar->monitor_vdev_id;
936 	arg.type = WMI_VDEV_TYPE_MONITOR;
937 	arg.subtype = WMI_VDEV_SUBTYPE_NONE;
938 	arg.pdev_id = pdev->pdev_id;
939 	arg.if_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
940 
941 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
942 		arg.chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
943 		arg.chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
944 	}
945 
946 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
947 		arg.chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
948 		arg.chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
949 	}
950 
951 	ret = ath12k_wmi_vdev_create(ar, tmp_addr, &arg);
952 	if (ret) {
953 		ath12k_warn(ar->ab, "failed to request monitor vdev %i creation: %d\n",
954 			    ar->monitor_vdev_id, ret);
955 		ar->monitor_vdev_id = -1;
956 		return ret;
957 	}
958 
959 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
960 	ret = ath12k_wmi_vdev_set_param_cmd(ar, ar->monitor_vdev_id,
961 					    WMI_VDEV_PARAM_NSS, nss);
962 	if (ret) {
963 		ath12k_warn(ar->ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
964 			    ar->monitor_vdev_id, ar->cfg_tx_chainmask, nss, ret);
965 		return ret;
966 	}
967 
968 	ret = ath12k_mac_txpower_recalc(ar);
969 	if (ret)
970 		return ret;
971 
972 	ar->allocated_vdev_map |= 1LL << ar->monitor_vdev_id;
973 	ar->ab->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
974 	ar->num_created_vdevs++;
975 	ar->monitor_vdev_created = true;
976 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d created\n",
977 		   ar->monitor_vdev_id);
978 
979 	return 0;
980 }
981 
ath12k_mac_monitor_vdev_delete(struct ath12k * ar)982 static int ath12k_mac_monitor_vdev_delete(struct ath12k *ar)
983 {
984 	int ret;
985 	unsigned long time_left;
986 
987 	lockdep_assert_held(&ar->conf_mutex);
988 
989 	if (!ar->monitor_vdev_created)
990 		return 0;
991 
992 	reinit_completion(&ar->vdev_delete_done);
993 
994 	ret = ath12k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
995 	if (ret) {
996 		ath12k_warn(ar->ab, "failed to request wmi monitor vdev %i removal: %d\n",
997 			    ar->monitor_vdev_id, ret);
998 		return ret;
999 	}
1000 
1001 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1002 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
1003 	if (time_left == 0) {
1004 		ath12k_warn(ar->ab, "Timeout in receiving vdev delete response\n");
1005 	} else {
1006 		ar->allocated_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1007 		ar->ab->free_vdev_map |= 1LL << (ar->monitor_vdev_id);
1008 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor vdev %d deleted\n",
1009 			   ar->monitor_vdev_id);
1010 		ar->num_created_vdevs--;
1011 		ar->monitor_vdev_id = -1;
1012 		ar->monitor_vdev_created = false;
1013 	}
1014 
1015 	return ret;
1016 }
1017 
1018 static void
ath12k_mac_get_any_chandef_iter(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf,void * data)1019 ath12k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
1020 				struct ieee80211_chanctx_conf *conf,
1021 				void *data)
1022 {
1023 	struct cfg80211_chan_def **def = data;
1024 
1025 	*def = &conf->def;
1026 }
1027 
ath12k_mac_monitor_start(struct ath12k * ar)1028 static int ath12k_mac_monitor_start(struct ath12k *ar)
1029 {
1030 	struct cfg80211_chan_def *chandef = NULL;
1031 	int ret;
1032 
1033 	lockdep_assert_held(&ar->conf_mutex);
1034 
1035 	if (ar->monitor_started)
1036 		return 0;
1037 
1038 	ieee80211_iter_chan_contexts_atomic(ar->hw,
1039 					    ath12k_mac_get_any_chandef_iter,
1040 					    &chandef);
1041 	if (!chandef)
1042 		return 0;
1043 
1044 	ret = ath12k_mac_monitor_vdev_start(ar, ar->monitor_vdev_id, chandef);
1045 	if (ret) {
1046 		ath12k_warn(ar->ab, "failed to start monitor vdev: %d\n", ret);
1047 		ath12k_mac_monitor_vdev_delete(ar);
1048 		return ret;
1049 	}
1050 
1051 	ar->monitor_started = true;
1052 	ar->num_started_vdevs++;
1053 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, false);
1054 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor started ret %d\n", ret);
1055 
1056 	return ret;
1057 }
1058 
ath12k_mac_monitor_stop(struct ath12k * ar)1059 static int ath12k_mac_monitor_stop(struct ath12k *ar)
1060 {
1061 	int ret;
1062 
1063 	lockdep_assert_held(&ar->conf_mutex);
1064 
1065 	if (!ar->monitor_started)
1066 		return 0;
1067 
1068 	ret = ath12k_mac_monitor_vdev_stop(ar);
1069 	if (ret) {
1070 		ath12k_warn(ar->ab, "failed to stop monitor vdev: %d\n", ret);
1071 		return ret;
1072 	}
1073 
1074 	ar->monitor_started = false;
1075 	ar->num_started_vdevs--;
1076 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, true);
1077 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac monitor stopped ret %d\n", ret);
1078 	return ret;
1079 }
1080 
ath12k_mac_op_config(struct ieee80211_hw * hw,u32 changed)1081 static int ath12k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
1082 {
1083 	struct ath12k *ar = hw->priv;
1084 	struct ieee80211_conf *conf = &hw->conf;
1085 	int ret = 0;
1086 
1087 	mutex_lock(&ar->conf_mutex);
1088 
1089 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1090 		ar->monitor_conf_enabled = conf->flags & IEEE80211_CONF_MONITOR;
1091 		if (ar->monitor_conf_enabled) {
1092 			if (ar->monitor_vdev_created)
1093 				goto exit;
1094 			ret = ath12k_mac_monitor_vdev_create(ar);
1095 			if (ret)
1096 				goto exit;
1097 			ret = ath12k_mac_monitor_start(ar);
1098 			if (ret)
1099 				goto err_mon_del;
1100 		} else {
1101 			if (!ar->monitor_vdev_created)
1102 				goto exit;
1103 			ret = ath12k_mac_monitor_stop(ar);
1104 			if (ret)
1105 				goto exit;
1106 			ath12k_mac_monitor_vdev_delete(ar);
1107 		}
1108 	}
1109 
1110 exit:
1111 	mutex_unlock(&ar->conf_mutex);
1112 	return ret;
1113 
1114 err_mon_del:
1115 	ath12k_mac_monitor_vdev_delete(ar);
1116 	mutex_unlock(&ar->conf_mutex);
1117 	return ret;
1118 }
1119 
ath12k_mac_setup_bcn_tmpl(struct ath12k_vif * arvif)1120 static int ath12k_mac_setup_bcn_tmpl(struct ath12k_vif *arvif)
1121 {
1122 	struct ath12k *ar = arvif->ar;
1123 	struct ath12k_base *ab = ar->ab;
1124 	struct ieee80211_hw *hw = ar->hw;
1125 	struct ieee80211_vif *vif = arvif->vif;
1126 	struct ieee80211_mutable_offsets offs = {};
1127 	struct sk_buff *bcn;
1128 	struct ieee80211_mgmt *mgmt;
1129 	u8 *ies;
1130 	int ret;
1131 
1132 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1133 		return 0;
1134 
1135 	bcn = ieee80211_beacon_get_template(hw, vif, &offs, 0);
1136 	if (!bcn) {
1137 		ath12k_warn(ab, "failed to get beacon template from mac80211\n");
1138 		return -EPERM;
1139 	}
1140 
1141 	ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
1142 	ies += sizeof(mgmt->u.beacon);
1143 
1144 	if (cfg80211_find_ie(WLAN_EID_RSN, ies, (skb_tail_pointer(bcn) - ies)))
1145 		arvif->rsnie_present = true;
1146 
1147 	if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1148 				    WLAN_OUI_TYPE_MICROSOFT_WPA,
1149 				    ies, (skb_tail_pointer(bcn) - ies)))
1150 		arvif->wpaie_present = true;
1151 
1152 	ret = ath12k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn);
1153 
1154 	kfree_skb(bcn);
1155 
1156 	if (ret)
1157 		ath12k_warn(ab, "failed to submit beacon template command: %d\n",
1158 			    ret);
1159 
1160 	return ret;
1161 }
1162 
ath12k_control_beaconing(struct ath12k_vif * arvif,struct ieee80211_bss_conf * info)1163 static void ath12k_control_beaconing(struct ath12k_vif *arvif,
1164 				     struct ieee80211_bss_conf *info)
1165 {
1166 	struct ath12k *ar = arvif->ar;
1167 	int ret;
1168 
1169 	lockdep_assert_held(&arvif->ar->conf_mutex);
1170 
1171 	if (!info->enable_beacon) {
1172 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
1173 		if (ret)
1174 			ath12k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
1175 				    arvif->vdev_id, ret);
1176 
1177 		arvif->is_up = false;
1178 		return;
1179 	}
1180 
1181 	/* Install the beacon template to the FW */
1182 	ret = ath12k_mac_setup_bcn_tmpl(arvif);
1183 	if (ret) {
1184 		ath12k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
1185 			    ret);
1186 		return;
1187 	}
1188 
1189 	arvif->aid = 0;
1190 
1191 	ether_addr_copy(arvif->bssid, info->bssid);
1192 
1193 	ret = ath12k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1194 				 arvif->bssid);
1195 	if (ret) {
1196 		ath12k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
1197 			    arvif->vdev_id, ret);
1198 		return;
1199 	}
1200 
1201 	arvif->is_up = true;
1202 
1203 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1204 }
1205 
ath12k_peer_assoc_h_basic(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg)1206 static void ath12k_peer_assoc_h_basic(struct ath12k *ar,
1207 				      struct ieee80211_vif *vif,
1208 				      struct ieee80211_sta *sta,
1209 				      struct ath12k_wmi_peer_assoc_arg *arg)
1210 {
1211 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
1212 	u32 aid;
1213 
1214 	lockdep_assert_held(&ar->conf_mutex);
1215 
1216 	if (vif->type == NL80211_IFTYPE_STATION)
1217 		aid = vif->cfg.aid;
1218 	else
1219 		aid = sta->aid;
1220 
1221 	ether_addr_copy(arg->peer_mac, sta->addr);
1222 	arg->vdev_id = arvif->vdev_id;
1223 	arg->peer_associd = aid;
1224 	arg->auth_flag = true;
1225 	/* TODO: STA WAR in ath10k for listen interval required? */
1226 	arg->peer_listen_intval = ar->hw->conf.listen_interval;
1227 	arg->peer_nss = 1;
1228 	arg->peer_caps = vif->bss_conf.assoc_capability;
1229 }
1230 
ath12k_peer_assoc_h_crypto(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg)1231 static void ath12k_peer_assoc_h_crypto(struct ath12k *ar,
1232 				       struct ieee80211_vif *vif,
1233 				       struct ieee80211_sta *sta,
1234 				       struct ath12k_wmi_peer_assoc_arg *arg)
1235 {
1236 	struct ieee80211_bss_conf *info = &vif->bss_conf;
1237 	struct cfg80211_chan_def def;
1238 	struct cfg80211_bss *bss;
1239 	struct ath12k_vif *arvif = (struct ath12k_vif *)vif->drv_priv;
1240 	const u8 *rsnie = NULL;
1241 	const u8 *wpaie = NULL;
1242 
1243 	lockdep_assert_held(&ar->conf_mutex);
1244 
1245 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
1246 		return;
1247 
1248 	bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
1249 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1250 
1251 	if (arvif->rsnie_present || arvif->wpaie_present) {
1252 		arg->need_ptk_4_way = true;
1253 		if (arvif->wpaie_present)
1254 			arg->need_gtk_2_way = true;
1255 	} else if (bss) {
1256 		const struct cfg80211_bss_ies *ies;
1257 
1258 		rcu_read_lock();
1259 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1260 
1261 		ies = rcu_dereference(bss->ies);
1262 
1263 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1264 						WLAN_OUI_TYPE_MICROSOFT_WPA,
1265 						ies->data,
1266 						ies->len);
1267 		rcu_read_unlock();
1268 		cfg80211_put_bss(ar->hw->wiphy, bss);
1269 	}
1270 
1271 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
1272 	if (rsnie || wpaie) {
1273 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
1274 			   "%s: rsn ie found\n", __func__);
1275 		arg->need_ptk_4_way = true;
1276 	}
1277 
1278 	if (wpaie) {
1279 		ath12k_dbg(ar->ab, ATH12K_DBG_WMI,
1280 			   "%s: wpa ie found\n", __func__);
1281 		arg->need_gtk_2_way = true;
1282 	}
1283 
1284 	if (sta->mfp) {
1285 		/* TODO: Need to check if FW supports PMF? */
1286 		arg->is_pmf_enabled = true;
1287 	}
1288 
1289 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
1290 }
1291 
ath12k_peer_assoc_h_rates(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg)1292 static void ath12k_peer_assoc_h_rates(struct ath12k *ar,
1293 				      struct ieee80211_vif *vif,
1294 				      struct ieee80211_sta *sta,
1295 				      struct ath12k_wmi_peer_assoc_arg *arg)
1296 {
1297 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
1298 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1299 	struct cfg80211_chan_def def;
1300 	const struct ieee80211_supported_band *sband;
1301 	const struct ieee80211_rate *rates;
1302 	enum nl80211_band band;
1303 	u32 ratemask;
1304 	u8 rate;
1305 	int i;
1306 
1307 	lockdep_assert_held(&ar->conf_mutex);
1308 
1309 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
1310 		return;
1311 
1312 	band = def.chan->band;
1313 	sband = ar->hw->wiphy->bands[band];
1314 	ratemask = sta->deflink.supp_rates[band];
1315 	ratemask &= arvif->bitrate_mask.control[band].legacy;
1316 	rates = sband->bitrates;
1317 
1318 	rateset->num_rates = 0;
1319 
1320 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1321 		if (!(ratemask & 1))
1322 			continue;
1323 
1324 		rate = ath12k_mac_bitrate_to_rate(rates->bitrate);
1325 		rateset->rates[rateset->num_rates] = rate;
1326 		rateset->num_rates++;
1327 	}
1328 }
1329 
1330 static bool
ath12k_peer_assoc_h_ht_masked(const u8 * ht_mcs_mask)1331 ath12k_peer_assoc_h_ht_masked(const u8 *ht_mcs_mask)
1332 {
1333 	int nss;
1334 
1335 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
1336 		if (ht_mcs_mask[nss])
1337 			return false;
1338 
1339 	return true;
1340 }
1341 
1342 static bool
ath12k_peer_assoc_h_vht_masked(const u16 * vht_mcs_mask)1343 ath12k_peer_assoc_h_vht_masked(const u16 *vht_mcs_mask)
1344 {
1345 	int nss;
1346 
1347 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
1348 		if (vht_mcs_mask[nss])
1349 			return false;
1350 
1351 	return true;
1352 }
1353 
ath12k_peer_assoc_h_ht(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg)1354 static void ath12k_peer_assoc_h_ht(struct ath12k *ar,
1355 				   struct ieee80211_vif *vif,
1356 				   struct ieee80211_sta *sta,
1357 				   struct ath12k_wmi_peer_assoc_arg *arg)
1358 {
1359 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
1360 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
1361 	struct cfg80211_chan_def def;
1362 	enum nl80211_band band;
1363 	const u8 *ht_mcs_mask;
1364 	int i, n;
1365 	u8 max_nss;
1366 	u32 stbc;
1367 
1368 	lockdep_assert_held(&ar->conf_mutex);
1369 
1370 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
1371 		return;
1372 
1373 	if (!ht_cap->ht_supported)
1374 		return;
1375 
1376 	band = def.chan->band;
1377 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1378 
1379 	if (ath12k_peer_assoc_h_ht_masked(ht_mcs_mask))
1380 		return;
1381 
1382 	arg->ht_flag = true;
1383 
1384 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1385 				    ht_cap->ampdu_factor)) - 1;
1386 
1387 	arg->peer_mpdu_density =
1388 		ath12k_parse_mpdudensity(ht_cap->ampdu_density);
1389 
1390 	arg->peer_ht_caps = ht_cap->cap;
1391 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
1392 
1393 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1394 		arg->ldpc_flag = true;
1395 
1396 	if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40) {
1397 		arg->bw_40 = true;
1398 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
1399 	}
1400 
1401 	if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
1402 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
1403 		    IEEE80211_HT_CAP_SGI_40))
1404 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
1405 	}
1406 
1407 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1408 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
1409 		arg->stbc_flag = true;
1410 	}
1411 
1412 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1413 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1414 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1415 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
1416 		arg->peer_rate_caps |= stbc;
1417 		arg->stbc_flag = true;
1418 	}
1419 
1420 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1421 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
1422 	else if (ht_cap->mcs.rx_mask[1])
1423 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
1424 
1425 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
1426 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
1427 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
1428 			max_nss = (i / 8) + 1;
1429 			arg->peer_ht_rates.rates[n++] = i;
1430 		}
1431 
1432 	/* This is a workaround for HT-enabled STAs which break the spec
1433 	 * and have no HT capabilities RX mask (no HT RX MCS map).
1434 	 *
1435 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1436 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1437 	 *
1438 	 * Firmware asserts if such situation occurs.
1439 	 */
1440 	if (n == 0) {
1441 		arg->peer_ht_rates.num_rates = 8;
1442 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1443 			arg->peer_ht_rates.rates[i] = i;
1444 	} else {
1445 		arg->peer_ht_rates.num_rates = n;
1446 		arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
1447 	}
1448 
1449 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1450 		   arg->peer_mac,
1451 		   arg->peer_ht_rates.num_rates,
1452 		   arg->peer_nss);
1453 }
1454 
ath12k_mac_get_max_vht_mcs_map(u16 mcs_map,int nss)1455 static int ath12k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
1456 {
1457 	switch ((mcs_map >> (2 * nss)) & 0x3) {
1458 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
1459 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
1460 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
1461 	}
1462 	return 0;
1463 }
1464 
1465 static u16
ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])1466 ath12k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
1467 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
1468 {
1469 	int idx_limit;
1470 	int nss;
1471 	u16 mcs_map;
1472 	u16 mcs;
1473 
1474 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
1475 		mcs_map = ath12k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
1476 			  vht_mcs_limit[nss];
1477 
1478 		if (mcs_map)
1479 			idx_limit = fls(mcs_map) - 1;
1480 		else
1481 			idx_limit = -1;
1482 
1483 		switch (idx_limit) {
1484 		case 0:
1485 		case 1:
1486 		case 2:
1487 		case 3:
1488 		case 4:
1489 		case 5:
1490 		case 6:
1491 		case 7:
1492 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
1493 			break;
1494 		case 8:
1495 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
1496 			break;
1497 		case 9:
1498 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
1499 			break;
1500 		default:
1501 			WARN_ON(1);
1502 			fallthrough;
1503 		case -1:
1504 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
1505 			break;
1506 		}
1507 
1508 		tx_mcs_set &= ~(0x3 << (nss * 2));
1509 		tx_mcs_set |= mcs << (nss * 2);
1510 	}
1511 
1512 	return tx_mcs_set;
1513 }
1514 
ath12k_peer_assoc_h_vht(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg)1515 static void ath12k_peer_assoc_h_vht(struct ath12k *ar,
1516 				    struct ieee80211_vif *vif,
1517 				    struct ieee80211_sta *sta,
1518 				    struct ath12k_wmi_peer_assoc_arg *arg)
1519 {
1520 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
1521 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
1522 	struct cfg80211_chan_def def;
1523 	enum nl80211_band band;
1524 	const u16 *vht_mcs_mask;
1525 	u16 tx_mcs_map;
1526 	u8 ampdu_factor;
1527 	u8 max_nss, vht_mcs;
1528 	int i;
1529 
1530 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
1531 		return;
1532 
1533 	if (!vht_cap->vht_supported)
1534 		return;
1535 
1536 	band = def.chan->band;
1537 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1538 
1539 	if (ath12k_peer_assoc_h_vht_masked(vht_mcs_mask))
1540 		return;
1541 
1542 	arg->vht_flag = true;
1543 
1544 	/* TODO: similar flags required? */
1545 	arg->vht_capable = true;
1546 
1547 	if (def.chan->band == NL80211_BAND_2GHZ)
1548 		arg->vht_ng_flag = true;
1549 
1550 	arg->peer_vht_caps = vht_cap->cap;
1551 
1552 	ampdu_factor = (vht_cap->cap &
1553 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1554 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1555 
1556 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1557 	 * zero in VHT IE. Using it would result in degraded throughput.
1558 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1559 	 * it if VHT max_mpdu is smaller.
1560 	 */
1561 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1562 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1563 					ampdu_factor)) - 1);
1564 
1565 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
1566 		arg->bw_80 = true;
1567 
1568 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
1569 		arg->bw_160 = true;
1570 
1571 	/* Calculate peer NSS capability from VHT capabilities if STA
1572 	 * supports VHT.
1573 	 */
1574 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
1575 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
1576 			  (2 * i) & 3;
1577 
1578 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
1579 		    vht_mcs_mask[i])
1580 			max_nss = i + 1;
1581 	}
1582 	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
1583 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1584 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1585 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1586 
1587 	tx_mcs_map = __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1588 	arg->tx_mcs_set = ath12k_peer_assoc_h_vht_limit(tx_mcs_map, vht_mcs_mask);
1589 
1590 	/* In QCN9274 platform, VHT MCS rate 10 and 11 is enabled by default.
1591 	 * VHT MCS rate 10 and 11 is not supported in 11ac standard.
1592 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
1593 	 */
1594 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
1595 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
1596 
1597 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
1598 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
1599 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1600 
1601 	/* TODO:  Check */
1602 	arg->tx_max_mcs_nss = 0xFF;
1603 
1604 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1605 		   sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1606 
1607 	/* TODO: rxnss_override */
1608 }
1609 
ath12k_peer_assoc_h_he(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg)1610 static void ath12k_peer_assoc_h_he(struct ath12k *ar,
1611 				   struct ieee80211_vif *vif,
1612 				   struct ieee80211_sta *sta,
1613 				   struct ath12k_wmi_peer_assoc_arg *arg)
1614 {
1615 	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
1616 	int i;
1617 	u8 ampdu_factor, max_nss;
1618 	u8 rx_mcs_80 = IEEE80211_HE_MCS_NOT_SUPPORTED;
1619 	u8 rx_mcs_160 = IEEE80211_HE_MCS_NOT_SUPPORTED;
1620 	u16 mcs_160_map, mcs_80_map;
1621 	bool support_160;
1622 	u16 v;
1623 
1624 	if (!he_cap->has_he)
1625 		return;
1626 
1627 	arg->he_flag = true;
1628 
1629 	support_160 = !!(he_cap->he_cap_elem.phy_cap_info[0] &
1630 		  IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G);
1631 
1632 	/* Supported HE-MCS and NSS Set of peer he_cap is intersection with self he_cp */
1633 	mcs_160_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
1634 	mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
1635 
1636 	if (support_160) {
1637 		for (i = 7; i >= 0; i--) {
1638 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
1639 
1640 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
1641 				rx_mcs_160 = i + 1;
1642 				break;
1643 			}
1644 		}
1645 	}
1646 
1647 	for (i = 7; i >= 0; i--) {
1648 		u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
1649 
1650 		if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
1651 			rx_mcs_80 = i + 1;
1652 			break;
1653 		}
1654 	}
1655 
1656 	if (support_160)
1657 		max_nss = min(rx_mcs_80, rx_mcs_160);
1658 	else
1659 		max_nss = rx_mcs_80;
1660 
1661 	arg->peer_nss = min(sta->deflink.rx_nss, max_nss);
1662 
1663 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
1664 	       sizeof(he_cap->he_cap_elem.mac_cap_info));
1665 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
1666 	       sizeof(he_cap->he_cap_elem.phy_cap_info));
1667 	arg->peer_he_ops = vif->bss_conf.he_oper.params;
1668 
1669 	/* the top most byte is used to indicate BSS color info */
1670 	arg->peer_he_ops &= 0xffffff;
1671 
1672 	/* As per section 26.6.1 IEEE Std 802.11ax‐2022, if the Max AMPDU
1673 	 * Exponent Extension in HE cap is zero, use the arg->peer_max_mpdu
1674 	 * as calculated while parsing VHT caps(if VHT caps is present)
1675 	 * or HT caps (if VHT caps is not present).
1676 	 *
1677 	 * For non-zero value of Max AMPDU Exponent Extension in HE MAC caps,
1678 	 * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
1679 	 * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
1680 	 * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
1681 	 * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
1682 	 * length.
1683 	 */
1684 	ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
1685 				   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
1686 
1687 	if (ampdu_factor) {
1688 		if (sta->deflink.vht_cap.vht_supported)
1689 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
1690 						    ampdu_factor)) - 1;
1691 		else if (sta->deflink.ht_cap.ht_supported)
1692 			arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
1693 						    ampdu_factor)) - 1;
1694 	}
1695 
1696 	if (he_cap->he_cap_elem.phy_cap_info[6] &
1697 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
1698 		int bit = 7;
1699 		int nss, ru;
1700 
1701 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
1702 					  IEEE80211_PPE_THRES_NSS_MASK;
1703 		arg->peer_ppet.ru_bit_mask =
1704 			(he_cap->ppe_thres[0] &
1705 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
1706 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
1707 
1708 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
1709 			for (ru = 0; ru < 4; ru++) {
1710 				u32 val = 0;
1711 				int i;
1712 
1713 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
1714 					continue;
1715 				for (i = 0; i < 6; i++) {
1716 					val >>= 1;
1717 					val |= ((he_cap->ppe_thres[bit / 8] >>
1718 						 (bit % 8)) & 0x1) << 5;
1719 					bit++;
1720 				}
1721 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
1722 								val << (ru * 6);
1723 			}
1724 		}
1725 	}
1726 
1727 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
1728 		arg->twt_responder = true;
1729 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
1730 		arg->twt_requester = true;
1731 
1732 	switch (sta->deflink.bandwidth) {
1733 	case IEEE80211_STA_RX_BW_160:
1734 		if (he_cap->he_cap_elem.phy_cap_info[0] &
1735 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
1736 			v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
1737 			arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1738 
1739 			v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
1740 			arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1741 
1742 			arg->peer_he_mcs_count++;
1743 		}
1744 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
1745 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1746 
1747 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
1748 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1749 
1750 		arg->peer_he_mcs_count++;
1751 		fallthrough;
1752 
1753 	default:
1754 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
1755 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1756 
1757 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
1758 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1759 
1760 		arg->peer_he_mcs_count++;
1761 		break;
1762 	}
1763 }
1764 
ath12k_peer_assoc_h_smps(struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg)1765 static void ath12k_peer_assoc_h_smps(struct ieee80211_sta *sta,
1766 				     struct ath12k_wmi_peer_assoc_arg *arg)
1767 {
1768 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
1769 	int smps;
1770 
1771 	if (!ht_cap->ht_supported)
1772 		return;
1773 
1774 	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1775 	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1776 
1777 	switch (smps) {
1778 	case WLAN_HT_CAP_SM_PS_STATIC:
1779 		arg->static_mimops_flag = true;
1780 		break;
1781 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
1782 		arg->dynamic_mimops_flag = true;
1783 		break;
1784 	case WLAN_HT_CAP_SM_PS_DISABLED:
1785 		arg->spatial_mux_flag = true;
1786 		break;
1787 	default:
1788 		break;
1789 	}
1790 }
1791 
ath12k_peer_assoc_h_qos(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg)1792 static void ath12k_peer_assoc_h_qos(struct ath12k *ar,
1793 				    struct ieee80211_vif *vif,
1794 				    struct ieee80211_sta *sta,
1795 				    struct ath12k_wmi_peer_assoc_arg *arg)
1796 {
1797 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
1798 
1799 	switch (arvif->vdev_type) {
1800 	case WMI_VDEV_TYPE_AP:
1801 		if (sta->wme) {
1802 			/* TODO: Check WME vs QoS */
1803 			arg->is_wme_set = true;
1804 			arg->qos_flag = true;
1805 		}
1806 
1807 		if (sta->wme && sta->uapsd_queues) {
1808 			/* TODO: Check WME vs QoS */
1809 			arg->is_wme_set = true;
1810 			arg->apsd_flag = true;
1811 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
1812 		}
1813 		break;
1814 	case WMI_VDEV_TYPE_STA:
1815 		if (sta->wme) {
1816 			arg->is_wme_set = true;
1817 			arg->qos_flag = true;
1818 		}
1819 		break;
1820 	default:
1821 		break;
1822 	}
1823 
1824 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM qos %d\n",
1825 		   sta->addr, arg->qos_flag);
1826 }
1827 
ath12k_peer_assoc_qos_ap(struct ath12k * ar,struct ath12k_vif * arvif,struct ieee80211_sta * sta)1828 static int ath12k_peer_assoc_qos_ap(struct ath12k *ar,
1829 				    struct ath12k_vif *arvif,
1830 				    struct ieee80211_sta *sta)
1831 {
1832 	struct ath12k_wmi_ap_ps_arg arg;
1833 	u32 max_sp;
1834 	u32 uapsd;
1835 	int ret;
1836 
1837 	lockdep_assert_held(&ar->conf_mutex);
1838 
1839 	arg.vdev_id = arvif->vdev_id;
1840 
1841 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1842 		   sta->uapsd_queues, sta->max_sp);
1843 
1844 	uapsd = 0;
1845 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1846 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1847 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1848 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1849 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1850 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1851 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1852 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1853 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1854 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1855 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1856 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1857 
1858 	max_sp = 0;
1859 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1860 		max_sp = sta->max_sp;
1861 
1862 	arg.param = WMI_AP_PS_PEER_PARAM_UAPSD;
1863 	arg.value = uapsd;
1864 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &arg);
1865 	if (ret)
1866 		goto err;
1867 
1868 	arg.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
1869 	arg.value = max_sp;
1870 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &arg);
1871 	if (ret)
1872 		goto err;
1873 
1874 	/* TODO: revisit during testing */
1875 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
1876 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1877 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &arg);
1878 	if (ret)
1879 		goto err;
1880 
1881 	arg.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
1882 	arg.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1883 	ret = ath12k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &arg);
1884 	if (ret)
1885 		goto err;
1886 
1887 	return 0;
1888 
1889 err:
1890 	ath12k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
1891 		    arg.param, arvif->vdev_id, ret);
1892 	return ret;
1893 }
1894 
ath12k_mac_sta_has_ofdm_only(struct ieee80211_sta * sta)1895 static bool ath12k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
1896 {
1897 	return sta->deflink.supp_rates[NL80211_BAND_2GHZ] >>
1898 	       ATH12K_MAC_FIRST_OFDM_RATE_IDX;
1899 }
1900 
ath12k_mac_get_phymode_vht(struct ath12k * ar,struct ieee80211_sta * sta)1901 static enum wmi_phy_mode ath12k_mac_get_phymode_vht(struct ath12k *ar,
1902 						    struct ieee80211_sta *sta)
1903 {
1904 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
1905 		switch (sta->deflink.vht_cap.cap &
1906 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
1907 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
1908 			return MODE_11AC_VHT160;
1909 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
1910 			return MODE_11AC_VHT80_80;
1911 		default:
1912 			/* not sure if this is a valid case? */
1913 			return MODE_11AC_VHT160;
1914 		}
1915 	}
1916 
1917 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
1918 		return MODE_11AC_VHT80;
1919 
1920 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
1921 		return MODE_11AC_VHT40;
1922 
1923 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
1924 		return MODE_11AC_VHT20;
1925 
1926 	return MODE_UNKNOWN;
1927 }
1928 
ath12k_mac_get_phymode_he(struct ath12k * ar,struct ieee80211_sta * sta)1929 static enum wmi_phy_mode ath12k_mac_get_phymode_he(struct ath12k *ar,
1930 						   struct ieee80211_sta *sta)
1931 {
1932 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
1933 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
1934 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
1935 			return MODE_11AX_HE160;
1936 		else if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
1937 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
1938 			return MODE_11AX_HE80_80;
1939 		/* not sure if this is a valid case? */
1940 		return MODE_11AX_HE160;
1941 	}
1942 
1943 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
1944 		return MODE_11AX_HE80;
1945 
1946 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
1947 		return MODE_11AX_HE40;
1948 
1949 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
1950 		return MODE_11AX_HE20;
1951 
1952 	return MODE_UNKNOWN;
1953 }
1954 
ath12k_mac_get_phymode_eht(struct ath12k * ar,struct ieee80211_sta * sta)1955 static enum wmi_phy_mode ath12k_mac_get_phymode_eht(struct ath12k *ar,
1956 						    struct ieee80211_sta *sta)
1957 {
1958 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_320)
1959 		if (sta->deflink.eht_cap.eht_cap_elem.phy_cap_info[0] &
1960 		    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
1961 			return MODE_11BE_EHT320;
1962 
1963 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
1964 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
1965 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
1966 			return MODE_11BE_EHT160;
1967 
1968 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[0] &
1969 			 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
1970 			return MODE_11BE_EHT80_80;
1971 
1972 		ath12k_warn(ar->ab, "invalid EHT PHY capability info for 160 Mhz: %d\n",
1973 			    sta->deflink.he_cap.he_cap_elem.phy_cap_info[0]);
1974 
1975 		return MODE_11BE_EHT160;
1976 	}
1977 
1978 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
1979 		return MODE_11BE_EHT80;
1980 
1981 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
1982 		return MODE_11BE_EHT40;
1983 
1984 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
1985 		return MODE_11BE_EHT20;
1986 
1987 	return MODE_UNKNOWN;
1988 }
1989 
ath12k_peer_assoc_h_phymode(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg)1990 static void ath12k_peer_assoc_h_phymode(struct ath12k *ar,
1991 					struct ieee80211_vif *vif,
1992 					struct ieee80211_sta *sta,
1993 					struct ath12k_wmi_peer_assoc_arg *arg)
1994 {
1995 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
1996 	struct cfg80211_chan_def def;
1997 	enum nl80211_band band;
1998 	const u8 *ht_mcs_mask;
1999 	const u16 *vht_mcs_mask;
2000 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
2001 
2002 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
2003 		return;
2004 
2005 	band = def.chan->band;
2006 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2007 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2008 
2009 	switch (band) {
2010 	case NL80211_BAND_2GHZ:
2011 		if (sta->deflink.eht_cap.has_eht) {
2012 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2013 				phymode = MODE_11BE_EHT40_2G;
2014 			else
2015 				phymode = MODE_11BE_EHT20_2G;
2016 		} else if (sta->deflink.he_cap.has_he) {
2017 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2018 				phymode = MODE_11AX_HE80_2G;
2019 			else if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2020 				phymode = MODE_11AX_HE40_2G;
2021 			else
2022 				phymode = MODE_11AX_HE20_2G;
2023 		} else if (sta->deflink.vht_cap.vht_supported &&
2024 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2025 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2026 				phymode = MODE_11AC_VHT40;
2027 			else
2028 				phymode = MODE_11AC_VHT20;
2029 		} else if (sta->deflink.ht_cap.ht_supported &&
2030 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2031 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2032 				phymode = MODE_11NG_HT40;
2033 			else
2034 				phymode = MODE_11NG_HT20;
2035 		} else if (ath12k_mac_sta_has_ofdm_only(sta)) {
2036 			phymode = MODE_11G;
2037 		} else {
2038 			phymode = MODE_11B;
2039 		}
2040 		break;
2041 	case NL80211_BAND_5GHZ:
2042 	case NL80211_BAND_6GHZ:
2043 		/* Check EHT first */
2044 		if (sta->deflink.eht_cap.has_eht) {
2045 			phymode = ath12k_mac_get_phymode_eht(ar, sta);
2046 		} else if (sta->deflink.he_cap.has_he) {
2047 			phymode = ath12k_mac_get_phymode_he(ar, sta);
2048 		} else if (sta->deflink.vht_cap.vht_supported &&
2049 		    !ath12k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2050 			phymode = ath12k_mac_get_phymode_vht(ar, sta);
2051 		} else if (sta->deflink.ht_cap.ht_supported &&
2052 			   !ath12k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2053 			if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40)
2054 				phymode = MODE_11NA_HT40;
2055 			else
2056 				phymode = MODE_11NA_HT20;
2057 		} else {
2058 			phymode = MODE_11A;
2059 		}
2060 		break;
2061 	default:
2062 		break;
2063 	}
2064 
2065 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac peer %pM phymode %s\n",
2066 		   sta->addr, ath12k_mac_phymode_str(phymode));
2067 
2068 	arg->peer_phymode = phymode;
2069 	WARN_ON(phymode == MODE_UNKNOWN);
2070 }
2071 
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)2072 static void ath12k_mac_set_eht_mcs(u8 rx_tx_mcs7, u8 rx_tx_mcs9,
2073 				   u8 rx_tx_mcs11, u8 rx_tx_mcs13,
2074 				   u32 *rx_mcs, u32 *tx_mcs)
2075 {
2076 	*rx_mcs = 0;
2077 	u32p_replace_bits(rx_mcs,
2078 			  u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_RX),
2079 			  WMI_EHT_MCS_NSS_0_7);
2080 	u32p_replace_bits(rx_mcs,
2081 			  u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_RX),
2082 			  WMI_EHT_MCS_NSS_8_9);
2083 	u32p_replace_bits(rx_mcs,
2084 			  u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_RX),
2085 			  WMI_EHT_MCS_NSS_10_11);
2086 	u32p_replace_bits(rx_mcs,
2087 			  u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_RX),
2088 			  WMI_EHT_MCS_NSS_12_13);
2089 
2090 	*tx_mcs = 0;
2091 	u32p_replace_bits(tx_mcs,
2092 			  u8_get_bits(rx_tx_mcs7, IEEE80211_EHT_MCS_NSS_TX),
2093 			  WMI_EHT_MCS_NSS_0_7);
2094 	u32p_replace_bits(tx_mcs,
2095 			  u8_get_bits(rx_tx_mcs9, IEEE80211_EHT_MCS_NSS_TX),
2096 			  WMI_EHT_MCS_NSS_8_9);
2097 	u32p_replace_bits(tx_mcs,
2098 			  u8_get_bits(rx_tx_mcs11, IEEE80211_EHT_MCS_NSS_TX),
2099 			  WMI_EHT_MCS_NSS_10_11);
2100 	u32p_replace_bits(tx_mcs,
2101 			  u8_get_bits(rx_tx_mcs13, IEEE80211_EHT_MCS_NSS_TX),
2102 			  WMI_EHT_MCS_NSS_12_13);
2103 }
2104 
ath12k_mac_set_eht_ppe_threshold(const u8 * ppe_thres,struct ath12k_wmi_ppe_threshold_arg * ppet)2105 static void ath12k_mac_set_eht_ppe_threshold(const u8 *ppe_thres,
2106 					     struct ath12k_wmi_ppe_threshold_arg *ppet)
2107 {
2108 	u32 bit_pos = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE, val;
2109 	u8 nss, ru, i;
2110 	u8 ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
2111 
2112 	ppet->numss_m1 = u8_get_bits(ppe_thres[0], IEEE80211_EHT_PPE_THRES_NSS_MASK);
2113 	ppet->ru_bit_mask = u16_get_bits(get_unaligned_le16(ppe_thres),
2114 					 IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2115 
2116 	for (nss = 0; nss <= ppet->numss_m1; nss++) {
2117 		for (ru = 0;
2118 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2119 		     ru++) {
2120 			if ((ppet->ru_bit_mask & BIT(ru)) == 0)
2121 				continue;
2122 
2123 			val = 0;
2124 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
2125 				val |= (((ppe_thres[bit_pos / 8] >>
2126 					  (bit_pos % 8)) & 0x1) << i);
2127 				bit_pos++;
2128 			}
2129 			ppet->ppet16_ppet8_ru3_ru0[nss] |=
2130 					(val << (ru * ppet_bit_len_per_ru));
2131 		}
2132 	}
2133 }
2134 
ath12k_peer_assoc_h_eht(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg)2135 static void ath12k_peer_assoc_h_eht(struct ath12k *ar,
2136 				    struct ieee80211_vif *vif,
2137 				    struct ieee80211_sta *sta,
2138 				    struct ath12k_wmi_peer_assoc_arg *arg)
2139 {
2140 	const struct ieee80211_sta_eht_cap *eht_cap = &sta->deflink.eht_cap;
2141 	const struct ieee80211_sta_he_cap *he_cap = &sta->deflink.he_cap;
2142 	const struct ieee80211_eht_mcs_nss_supp_20mhz_only *bw_20;
2143 	const struct ieee80211_eht_mcs_nss_supp_bw *bw;
2144 	struct ath12k_vif *arvif = (struct ath12k_vif *)vif->drv_priv;
2145 	u32 *rx_mcs, *tx_mcs;
2146 
2147 	if (!sta->deflink.he_cap.has_he || !eht_cap->has_eht)
2148 		return;
2149 
2150 	arg->eht_flag = true;
2151 
2152 	if ((eht_cap->eht_cap_elem.phy_cap_info[5] &
2153 	     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) &&
2154 	    eht_cap->eht_ppe_thres[0] != 0)
2155 		ath12k_mac_set_eht_ppe_threshold(eht_cap->eht_ppe_thres,
2156 						 &arg->peer_eht_ppet);
2157 
2158 	memcpy(arg->peer_eht_cap_mac, eht_cap->eht_cap_elem.mac_cap_info,
2159 	       sizeof(eht_cap->eht_cap_elem.mac_cap_info));
2160 	memcpy(arg->peer_eht_cap_phy, eht_cap->eht_cap_elem.phy_cap_info,
2161 	       sizeof(eht_cap->eht_cap_elem.phy_cap_info));
2162 
2163 	rx_mcs = arg->peer_eht_rx_mcs_set;
2164 	tx_mcs = arg->peer_eht_tx_mcs_set;
2165 
2166 	switch (sta->deflink.bandwidth) {
2167 	case IEEE80211_STA_RX_BW_320:
2168 		bw = &eht_cap->eht_mcs_nss_supp.bw._320;
2169 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
2170 				       bw->rx_tx_mcs9_max_nss,
2171 				       bw->rx_tx_mcs11_max_nss,
2172 				       bw->rx_tx_mcs13_max_nss,
2173 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320],
2174 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_320]);
2175 		arg->peer_eht_mcs_count++;
2176 		fallthrough;
2177 	case IEEE80211_STA_RX_BW_160:
2178 		bw = &eht_cap->eht_mcs_nss_supp.bw._160;
2179 		ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
2180 				       bw->rx_tx_mcs9_max_nss,
2181 				       bw->rx_tx_mcs11_max_nss,
2182 				       bw->rx_tx_mcs13_max_nss,
2183 				       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160],
2184 				       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_160]);
2185 		arg->peer_eht_mcs_count++;
2186 		fallthrough;
2187 	default:
2188 		if ((he_cap->he_cap_elem.phy_cap_info[0] &
2189 		     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
2190 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
2191 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
2192 		      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0) {
2193 			bw_20 = &eht_cap->eht_mcs_nss_supp.only_20mhz;
2194 
2195 			ath12k_mac_set_eht_mcs(bw_20->rx_tx_mcs7_max_nss,
2196 					       bw_20->rx_tx_mcs9_max_nss,
2197 					       bw_20->rx_tx_mcs11_max_nss,
2198 					       bw_20->rx_tx_mcs13_max_nss,
2199 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
2200 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80]);
2201 		} else {
2202 			bw = &eht_cap->eht_mcs_nss_supp.bw._80;
2203 			ath12k_mac_set_eht_mcs(bw->rx_tx_mcs9_max_nss,
2204 					       bw->rx_tx_mcs9_max_nss,
2205 					       bw->rx_tx_mcs11_max_nss,
2206 					       bw->rx_tx_mcs13_max_nss,
2207 					       &rx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80],
2208 					       &tx_mcs[WMI_EHTCAP_TXRX_MCS_NSS_IDX_80]);
2209 		}
2210 
2211 		arg->peer_eht_mcs_count++;
2212 		break;
2213 	}
2214 
2215 	arg->punct_bitmap = ~arvif->punct_bitmap;
2216 }
2217 
ath12k_peer_assoc_prepare(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath12k_wmi_peer_assoc_arg * arg,bool reassoc)2218 static void ath12k_peer_assoc_prepare(struct ath12k *ar,
2219 				      struct ieee80211_vif *vif,
2220 				      struct ieee80211_sta *sta,
2221 				      struct ath12k_wmi_peer_assoc_arg *arg,
2222 				      bool reassoc)
2223 {
2224 	lockdep_assert_held(&ar->conf_mutex);
2225 
2226 	memset(arg, 0, sizeof(*arg));
2227 
2228 	reinit_completion(&ar->peer_assoc_done);
2229 
2230 	arg->peer_new_assoc = !reassoc;
2231 	ath12k_peer_assoc_h_basic(ar, vif, sta, arg);
2232 	ath12k_peer_assoc_h_crypto(ar, vif, sta, arg);
2233 	ath12k_peer_assoc_h_rates(ar, vif, sta, arg);
2234 	ath12k_peer_assoc_h_ht(ar, vif, sta, arg);
2235 	ath12k_peer_assoc_h_vht(ar, vif, sta, arg);
2236 	ath12k_peer_assoc_h_he(ar, vif, sta, arg);
2237 	ath12k_peer_assoc_h_eht(ar, vif, sta, arg);
2238 	ath12k_peer_assoc_h_qos(ar, vif, sta, arg);
2239 	ath12k_peer_assoc_h_phymode(ar, vif, sta, arg);
2240 	ath12k_peer_assoc_h_smps(sta, arg);
2241 
2242 	/* TODO: amsdu_disable req? */
2243 }
2244 
ath12k_setup_peer_smps(struct ath12k * ar,struct ath12k_vif * arvif,const u8 * addr,const struct ieee80211_sta_ht_cap * ht_cap)2245 static int ath12k_setup_peer_smps(struct ath12k *ar, struct ath12k_vif *arvif,
2246 				  const u8 *addr,
2247 				  const struct ieee80211_sta_ht_cap *ht_cap)
2248 {
2249 	int smps;
2250 
2251 	if (!ht_cap->ht_supported)
2252 		return 0;
2253 
2254 	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2255 	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2256 
2257 	if (smps >= ARRAY_SIZE(ath12k_smps_map))
2258 		return -EINVAL;
2259 
2260 	return ath12k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
2261 					 WMI_PEER_MIMO_PS_STATE,
2262 					 ath12k_smps_map[smps]);
2263 }
2264 
ath12k_bss_assoc(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf)2265 static void ath12k_bss_assoc(struct ieee80211_hw *hw,
2266 			     struct ieee80211_vif *vif,
2267 			     struct ieee80211_bss_conf *bss_conf)
2268 {
2269 	struct ath12k *ar = hw->priv;
2270 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
2271 	struct ath12k_wmi_peer_assoc_arg peer_arg;
2272 	struct ieee80211_sta *ap_sta;
2273 	struct ath12k_peer *peer;
2274 	bool is_auth = false;
2275 	int ret;
2276 
2277 	lockdep_assert_held(&ar->conf_mutex);
2278 
2279 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2280 		   arvif->vdev_id, arvif->bssid, arvif->aid);
2281 
2282 	rcu_read_lock();
2283 
2284 	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2285 	if (!ap_sta) {
2286 		ath12k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
2287 			    bss_conf->bssid, arvif->vdev_id);
2288 		rcu_read_unlock();
2289 		return;
2290 	}
2291 
2292 	ath12k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
2293 
2294 	rcu_read_unlock();
2295 
2296 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2297 	if (ret) {
2298 		ath12k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
2299 			    bss_conf->bssid, arvif->vdev_id, ret);
2300 		return;
2301 	}
2302 
2303 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
2304 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2305 			    bss_conf->bssid, arvif->vdev_id);
2306 		return;
2307 	}
2308 
2309 	ret = ath12k_setup_peer_smps(ar, arvif, bss_conf->bssid,
2310 				     &ap_sta->deflink.ht_cap);
2311 	if (ret) {
2312 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
2313 			    arvif->vdev_id, ret);
2314 		return;
2315 	}
2316 
2317 	WARN_ON(arvif->is_up);
2318 
2319 	arvif->aid = vif->cfg.aid;
2320 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
2321 
2322 	ret = ath12k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2323 	if (ret) {
2324 		ath12k_warn(ar->ab, "failed to set vdev %d up: %d\n",
2325 			    arvif->vdev_id, ret);
2326 		return;
2327 	}
2328 
2329 	arvif->is_up = true;
2330 
2331 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2332 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
2333 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
2334 
2335 	spin_lock_bh(&ar->ab->base_lock);
2336 
2337 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, arvif->bssid);
2338 	if (peer && peer->is_authorized)
2339 		is_auth = true;
2340 
2341 	spin_unlock_bh(&ar->ab->base_lock);
2342 
2343 	/* Authorize BSS Peer */
2344 	if (is_auth) {
2345 		ret = ath12k_wmi_set_peer_param(ar, arvif->bssid,
2346 						arvif->vdev_id,
2347 						WMI_PEER_AUTHORIZE,
2348 						1);
2349 		if (ret)
2350 			ath12k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
2351 	}
2352 
2353 	ret = ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
2354 					   &bss_conf->he_obss_pd);
2355 	if (ret)
2356 		ath12k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
2357 			    arvif->vdev_id, ret);
2358 }
2359 
ath12k_bss_disassoc(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2360 static void ath12k_bss_disassoc(struct ieee80211_hw *hw,
2361 				struct ieee80211_vif *vif)
2362 {
2363 	struct ath12k *ar = hw->priv;
2364 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
2365 	int ret;
2366 
2367 	lockdep_assert_held(&ar->conf_mutex);
2368 
2369 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2370 		   arvif->vdev_id, arvif->bssid);
2371 
2372 	ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
2373 	if (ret)
2374 		ath12k_warn(ar->ab, "failed to down vdev %i: %d\n",
2375 			    arvif->vdev_id, ret);
2376 
2377 	arvif->is_up = false;
2378 
2379 	/* TODO: cancel connection_loss_work */
2380 }
2381 
ath12k_mac_get_rate_hw_value(int bitrate)2382 static u32 ath12k_mac_get_rate_hw_value(int bitrate)
2383 {
2384 	u32 preamble;
2385 	u16 hw_value;
2386 	int rate;
2387 	size_t i;
2388 
2389 	if (ath12k_mac_bitrate_is_cck(bitrate))
2390 		preamble = WMI_RATE_PREAMBLE_CCK;
2391 	else
2392 		preamble = WMI_RATE_PREAMBLE_OFDM;
2393 
2394 	for (i = 0; i < ARRAY_SIZE(ath12k_legacy_rates); i++) {
2395 		if (ath12k_legacy_rates[i].bitrate != bitrate)
2396 			continue;
2397 
2398 		hw_value = ath12k_legacy_rates[i].hw_value;
2399 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
2400 
2401 		return rate;
2402 	}
2403 
2404 	return -EINVAL;
2405 }
2406 
ath12k_recalculate_mgmt_rate(struct ath12k * ar,struct ieee80211_vif * vif,struct cfg80211_chan_def * def)2407 static void ath12k_recalculate_mgmt_rate(struct ath12k *ar,
2408 					 struct ieee80211_vif *vif,
2409 					 struct cfg80211_chan_def *def)
2410 {
2411 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
2412 	const struct ieee80211_supported_band *sband;
2413 	u8 basic_rate_idx;
2414 	int hw_rate_code;
2415 	u32 vdev_param;
2416 	u16 bitrate;
2417 	int ret;
2418 
2419 	lockdep_assert_held(&ar->conf_mutex);
2420 
2421 	sband = ar->hw->wiphy->bands[def->chan->band];
2422 	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
2423 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
2424 
2425 	hw_rate_code = ath12k_mac_get_rate_hw_value(bitrate);
2426 	if (hw_rate_code < 0) {
2427 		ath12k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
2428 		return;
2429 	}
2430 
2431 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
2432 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
2433 					    hw_rate_code);
2434 	if (ret)
2435 		ath12k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
2436 
2437 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
2438 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
2439 					    hw_rate_code);
2440 	if (ret)
2441 		ath12k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
2442 }
2443 
ath12k_mac_fils_discovery(struct ath12k_vif * arvif,struct ieee80211_bss_conf * info)2444 static int ath12k_mac_fils_discovery(struct ath12k_vif *arvif,
2445 				     struct ieee80211_bss_conf *info)
2446 {
2447 	struct ath12k *ar = arvif->ar;
2448 	struct sk_buff *tmpl;
2449 	int ret;
2450 	u32 interval;
2451 	bool unsol_bcast_probe_resp_enabled = false;
2452 
2453 	if (info->fils_discovery.max_interval) {
2454 		interval = info->fils_discovery.max_interval;
2455 
2456 		tmpl = ieee80211_get_fils_discovery_tmpl(ar->hw, arvif->vif);
2457 		if (tmpl)
2458 			ret = ath12k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
2459 							     tmpl);
2460 	} else if (info->unsol_bcast_probe_resp_interval) {
2461 		unsol_bcast_probe_resp_enabled = 1;
2462 		interval = info->unsol_bcast_probe_resp_interval;
2463 
2464 		tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(ar->hw,
2465 								 arvif->vif);
2466 		if (tmpl)
2467 			ret = ath12k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
2468 							 tmpl);
2469 	} else { /* Disable */
2470 		return ath12k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
2471 	}
2472 
2473 	if (!tmpl) {
2474 		ath12k_warn(ar->ab,
2475 			    "mac vdev %i failed to retrieve %s template\n",
2476 			    arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
2477 			    "unsolicited broadcast probe response" :
2478 			    "FILS discovery"));
2479 		return -EPERM;
2480 	}
2481 	kfree_skb(tmpl);
2482 
2483 	if (!ret)
2484 		ret = ath12k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
2485 						unsol_bcast_probe_resp_enabled);
2486 
2487 	return ret;
2488 }
2489 
ath12k_mac_op_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)2490 static void ath12k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
2491 					   struct ieee80211_vif *vif,
2492 					   struct ieee80211_bss_conf *info,
2493 					   u64 changed)
2494 {
2495 	struct ath12k *ar = hw->priv;
2496 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
2497 	struct cfg80211_chan_def def;
2498 	u32 param_id, param_value;
2499 	enum nl80211_band band;
2500 	u32 vdev_param;
2501 	int mcast_rate;
2502 	u32 preamble;
2503 	u16 hw_value;
2504 	u16 bitrate;
2505 	int ret;
2506 	u8 rateidx;
2507 	u32 rate;
2508 
2509 	mutex_lock(&ar->conf_mutex);
2510 
2511 	if (changed & BSS_CHANGED_BEACON_INT) {
2512 		arvif->beacon_interval = info->beacon_int;
2513 
2514 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
2515 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2516 						    param_id,
2517 						    arvif->beacon_interval);
2518 		if (ret)
2519 			ath12k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
2520 				    arvif->vdev_id);
2521 		else
2522 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2523 				   "Beacon interval: %d set for VDEV: %d\n",
2524 				   arvif->beacon_interval, arvif->vdev_id);
2525 	}
2526 
2527 	if (changed & BSS_CHANGED_BEACON) {
2528 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
2529 		param_value = WMI_BEACON_STAGGERED_MODE;
2530 		ret = ath12k_wmi_pdev_set_param(ar, param_id,
2531 						param_value, ar->pdev->pdev_id);
2532 		if (ret)
2533 			ath12k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
2534 				    arvif->vdev_id);
2535 		else
2536 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2537 				   "Set staggered beacon mode for VDEV: %d\n",
2538 				   arvif->vdev_id);
2539 
2540 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
2541 		if (ret)
2542 			ath12k_warn(ar->ab, "failed to update bcn template: %d\n",
2543 				    ret);
2544 	}
2545 
2546 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
2547 		arvif->dtim_period = info->dtim_period;
2548 
2549 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
2550 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2551 						    param_id,
2552 						    arvif->dtim_period);
2553 
2554 		if (ret)
2555 			ath12k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
2556 				    arvif->vdev_id, ret);
2557 		else
2558 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2559 				   "DTIM period: %d set for VDEV: %d\n",
2560 				   arvif->dtim_period, arvif->vdev_id);
2561 	}
2562 
2563 	if (changed & BSS_CHANGED_SSID &&
2564 	    vif->type == NL80211_IFTYPE_AP) {
2565 		arvif->u.ap.ssid_len = vif->cfg.ssid_len;
2566 		if (vif->cfg.ssid_len)
2567 			memcpy(arvif->u.ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
2568 		arvif->u.ap.hidden_ssid = info->hidden_ssid;
2569 	}
2570 
2571 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
2572 		ether_addr_copy(arvif->bssid, info->bssid);
2573 
2574 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
2575 		ath12k_control_beaconing(arvif, info);
2576 
2577 		if (arvif->is_up && vif->bss_conf.he_support &&
2578 		    vif->bss_conf.he_oper.params) {
2579 			/* TODO: Extend to support 1024 BA Bitmap size */
2580 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2581 							    WMI_VDEV_PARAM_BA_MODE,
2582 							    WMI_BA_MODE_BUFFER_SIZE_256);
2583 			if (ret)
2584 				ath12k_warn(ar->ab,
2585 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
2586 					    arvif->vdev_id);
2587 
2588 			param_id = WMI_VDEV_PARAM_HEOPS_0_31;
2589 			param_value = vif->bss_conf.he_oper.params;
2590 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2591 							    param_id, param_value);
2592 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2593 				   "he oper param: %x set for VDEV: %d\n",
2594 				   param_value, arvif->vdev_id);
2595 
2596 			if (ret)
2597 				ath12k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
2598 					    param_value, arvif->vdev_id, ret);
2599 		}
2600 	}
2601 
2602 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2603 		u32 cts_prot;
2604 
2605 		cts_prot = !!(info->use_cts_prot);
2606 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
2607 
2608 		if (arvif->is_started) {
2609 			ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2610 							    param_id, cts_prot);
2611 			if (ret)
2612 				ath12k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
2613 					    arvif->vdev_id);
2614 			else
2615 				ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
2616 					   cts_prot, arvif->vdev_id);
2617 		} else {
2618 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
2619 		}
2620 	}
2621 
2622 	if (changed & BSS_CHANGED_ERP_SLOT) {
2623 		u32 slottime;
2624 
2625 		if (info->use_short_slot)
2626 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2627 
2628 		else
2629 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2630 
2631 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
2632 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2633 						    param_id, slottime);
2634 		if (ret)
2635 			ath12k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
2636 				    arvif->vdev_id);
2637 		else
2638 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2639 				   "Set slottime: %d for VDEV: %d\n",
2640 				   slottime, arvif->vdev_id);
2641 	}
2642 
2643 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2644 		u32 preamble;
2645 
2646 		if (info->use_short_preamble)
2647 			preamble = WMI_VDEV_PREAMBLE_SHORT;
2648 		else
2649 			preamble = WMI_VDEV_PREAMBLE_LONG;
2650 
2651 		param_id = WMI_VDEV_PARAM_PREAMBLE;
2652 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2653 						    param_id, preamble);
2654 		if (ret)
2655 			ath12k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
2656 				    arvif->vdev_id);
2657 		else
2658 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2659 				   "Set preamble: %d for VDEV: %d\n",
2660 				   preamble, arvif->vdev_id);
2661 	}
2662 
2663 	if (changed & BSS_CHANGED_ASSOC) {
2664 		if (vif->cfg.assoc)
2665 			ath12k_bss_assoc(hw, vif, info);
2666 		else
2667 			ath12k_bss_disassoc(hw, vif);
2668 	}
2669 
2670 	if (changed & BSS_CHANGED_TXPOWER) {
2671 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac vdev_id %i txpower %d\n",
2672 			   arvif->vdev_id, info->txpower);
2673 
2674 		arvif->txpower = info->txpower;
2675 		ath12k_mac_txpower_recalc(ar);
2676 	}
2677 
2678 	if (changed & BSS_CHANGED_MCAST_RATE &&
2679 	    !ath12k_mac_vif_chan(arvif->vif, &def)) {
2680 		band = def.chan->band;
2681 		mcast_rate = vif->bss_conf.mcast_rate[band];
2682 
2683 		if (mcast_rate > 0)
2684 			rateidx = mcast_rate - 1;
2685 		else
2686 			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
2687 
2688 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
2689 			rateidx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
2690 
2691 		bitrate = ath12k_legacy_rates[rateidx].bitrate;
2692 		hw_value = ath12k_legacy_rates[rateidx].hw_value;
2693 
2694 		if (ath12k_mac_bitrate_is_cck(bitrate))
2695 			preamble = WMI_RATE_PREAMBLE_CCK;
2696 		else
2697 			preamble = WMI_RATE_PREAMBLE_OFDM;
2698 
2699 		rate = ATH12K_HW_RATE_CODE(hw_value, 0, preamble);
2700 
2701 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
2702 			   "mac vdev %d mcast_rate %x\n",
2703 			   arvif->vdev_id, rate);
2704 
2705 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
2706 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2707 						    vdev_param, rate);
2708 		if (ret)
2709 			ath12k_warn(ar->ab,
2710 				    "failed to set mcast rate on vdev %i: %d\n",
2711 				    arvif->vdev_id,  ret);
2712 
2713 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
2714 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2715 						    vdev_param, rate);
2716 		if (ret)
2717 			ath12k_warn(ar->ab,
2718 				    "failed to set bcast rate on vdev %i: %d\n",
2719 				    arvif->vdev_id,  ret);
2720 	}
2721 
2722 	if (changed & BSS_CHANGED_BASIC_RATES &&
2723 	    !ath12k_mac_vif_chan(arvif->vif, &def))
2724 		ath12k_recalculate_mgmt_rate(ar, vif, &def);
2725 
2726 	if (changed & BSS_CHANGED_TWT) {
2727 		if (info->twt_requester || info->twt_responder)
2728 			ath12k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
2729 		else
2730 			ath12k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
2731 	}
2732 
2733 	if (changed & BSS_CHANGED_HE_OBSS_PD)
2734 		ath12k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
2735 					     &info->he_obss_pd);
2736 
2737 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
2738 		if (vif->type == NL80211_IFTYPE_AP) {
2739 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
2740 							    arvif->vdev_id,
2741 							    info->he_bss_color.color,
2742 							    ATH12K_BSS_COLOR_AP_PERIODS,
2743 							    info->he_bss_color.enabled);
2744 			if (ret)
2745 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
2746 					    arvif->vdev_id,  ret);
2747 		} else if (vif->type == NL80211_IFTYPE_STATION) {
2748 			ret = ath12k_wmi_send_bss_color_change_enable_cmd(ar,
2749 									  arvif->vdev_id,
2750 									  1);
2751 			if (ret)
2752 				ath12k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
2753 					    arvif->vdev_id,  ret);
2754 			ret = ath12k_wmi_obss_color_cfg_cmd(ar,
2755 							    arvif->vdev_id,
2756 							    0,
2757 							    ATH12K_BSS_COLOR_STA_PERIODS,
2758 							    1);
2759 			if (ret)
2760 				ath12k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
2761 					    arvif->vdev_id,  ret);
2762 		}
2763 	}
2764 
2765 	if (changed & BSS_CHANGED_FILS_DISCOVERY ||
2766 	    changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP)
2767 		ath12k_mac_fils_discovery(arvif, info);
2768 
2769 	if (changed & BSS_CHANGED_EHT_PUNCTURING)
2770 		arvif->punct_bitmap = info->eht_puncturing;
2771 
2772 	mutex_unlock(&ar->conf_mutex);
2773 }
2774 
__ath12k_mac_scan_finish(struct ath12k * ar)2775 void __ath12k_mac_scan_finish(struct ath12k *ar)
2776 {
2777 	lockdep_assert_held(&ar->data_lock);
2778 
2779 	switch (ar->scan.state) {
2780 	case ATH12K_SCAN_IDLE:
2781 		break;
2782 	case ATH12K_SCAN_RUNNING:
2783 	case ATH12K_SCAN_ABORTING:
2784 		if (!ar->scan.is_roc) {
2785 			struct cfg80211_scan_info info = {
2786 				.aborted = (ar->scan.state ==
2787 					    ATH12K_SCAN_ABORTING),
2788 			};
2789 
2790 			ieee80211_scan_completed(ar->hw, &info);
2791 		} else if (ar->scan.roc_notify) {
2792 			ieee80211_remain_on_channel_expired(ar->hw);
2793 		}
2794 		fallthrough;
2795 	case ATH12K_SCAN_STARTING:
2796 		ar->scan.state = ATH12K_SCAN_IDLE;
2797 		ar->scan_channel = NULL;
2798 		ar->scan.roc_freq = 0;
2799 		cancel_delayed_work(&ar->scan.timeout);
2800 		complete(&ar->scan.completed);
2801 		break;
2802 	}
2803 }
2804 
ath12k_mac_scan_finish(struct ath12k * ar)2805 void ath12k_mac_scan_finish(struct ath12k *ar)
2806 {
2807 	spin_lock_bh(&ar->data_lock);
2808 	__ath12k_mac_scan_finish(ar);
2809 	spin_unlock_bh(&ar->data_lock);
2810 }
2811 
ath12k_scan_stop(struct ath12k * ar)2812 static int ath12k_scan_stop(struct ath12k *ar)
2813 {
2814 	struct ath12k_wmi_scan_cancel_arg arg = {
2815 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
2816 		.scan_id = ATH12K_SCAN_ID,
2817 	};
2818 	int ret;
2819 
2820 	lockdep_assert_held(&ar->conf_mutex);
2821 
2822 	/* TODO: Fill other STOP Params */
2823 	arg.pdev_id = ar->pdev->pdev_id;
2824 
2825 	ret = ath12k_wmi_send_scan_stop_cmd(ar, &arg);
2826 	if (ret) {
2827 		ath12k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
2828 		goto out;
2829 	}
2830 
2831 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
2832 	if (ret == 0) {
2833 		ath12k_warn(ar->ab,
2834 			    "failed to receive scan abort comple: timed out\n");
2835 		ret = -ETIMEDOUT;
2836 	} else if (ret > 0) {
2837 		ret = 0;
2838 	}
2839 
2840 out:
2841 	/* Scan state should be updated upon scan completion but in case
2842 	 * firmware fails to deliver the event (for whatever reason) it is
2843 	 * desired to clean up scan state anyway. Firmware may have just
2844 	 * dropped the scan completion event delivery due to transport pipe
2845 	 * being overflown with data and/or it can recover on its own before
2846 	 * next scan request is submitted.
2847 	 */
2848 	spin_lock_bh(&ar->data_lock);
2849 	if (ar->scan.state != ATH12K_SCAN_IDLE)
2850 		__ath12k_mac_scan_finish(ar);
2851 	spin_unlock_bh(&ar->data_lock);
2852 
2853 	return ret;
2854 }
2855 
ath12k_scan_abort(struct ath12k * ar)2856 static void ath12k_scan_abort(struct ath12k *ar)
2857 {
2858 	int ret;
2859 
2860 	lockdep_assert_held(&ar->conf_mutex);
2861 
2862 	spin_lock_bh(&ar->data_lock);
2863 
2864 	switch (ar->scan.state) {
2865 	case ATH12K_SCAN_IDLE:
2866 		/* This can happen if timeout worker kicked in and called
2867 		 * abortion while scan completion was being processed.
2868 		 */
2869 		break;
2870 	case ATH12K_SCAN_STARTING:
2871 	case ATH12K_SCAN_ABORTING:
2872 		ath12k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
2873 			    ar->scan.state);
2874 		break;
2875 	case ATH12K_SCAN_RUNNING:
2876 		ar->scan.state = ATH12K_SCAN_ABORTING;
2877 		spin_unlock_bh(&ar->data_lock);
2878 
2879 		ret = ath12k_scan_stop(ar);
2880 		if (ret)
2881 			ath12k_warn(ar->ab, "failed to abort scan: %d\n", ret);
2882 
2883 		spin_lock_bh(&ar->data_lock);
2884 		break;
2885 	}
2886 
2887 	spin_unlock_bh(&ar->data_lock);
2888 }
2889 
ath12k_scan_timeout_work(struct work_struct * work)2890 static void ath12k_scan_timeout_work(struct work_struct *work)
2891 {
2892 	struct ath12k *ar = container_of(work, struct ath12k,
2893 					 scan.timeout.work);
2894 
2895 	mutex_lock(&ar->conf_mutex);
2896 	ath12k_scan_abort(ar);
2897 	mutex_unlock(&ar->conf_mutex);
2898 }
2899 
ath12k_start_scan(struct ath12k * ar,struct ath12k_wmi_scan_req_arg * arg)2900 static int ath12k_start_scan(struct ath12k *ar,
2901 			     struct ath12k_wmi_scan_req_arg *arg)
2902 {
2903 	int ret;
2904 
2905 	lockdep_assert_held(&ar->conf_mutex);
2906 
2907 	ret = ath12k_wmi_send_scan_start_cmd(ar, arg);
2908 	if (ret)
2909 		return ret;
2910 
2911 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
2912 	if (ret == 0) {
2913 		ret = ath12k_scan_stop(ar);
2914 		if (ret)
2915 			ath12k_warn(ar->ab, "failed to stop scan: %d\n", ret);
2916 
2917 		return -ETIMEDOUT;
2918 	}
2919 
2920 	/* If we failed to start the scan, return error code at
2921 	 * this point.  This is probably due to some issue in the
2922 	 * firmware, but no need to wedge the driver due to that...
2923 	 */
2924 	spin_lock_bh(&ar->data_lock);
2925 	if (ar->scan.state == ATH12K_SCAN_IDLE) {
2926 		spin_unlock_bh(&ar->data_lock);
2927 		return -EINVAL;
2928 	}
2929 	spin_unlock_bh(&ar->data_lock);
2930 
2931 	return 0;
2932 }
2933 
ath12k_mac_op_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)2934 static int ath12k_mac_op_hw_scan(struct ieee80211_hw *hw,
2935 				 struct ieee80211_vif *vif,
2936 				 struct ieee80211_scan_request *hw_req)
2937 {
2938 	struct ath12k *ar = hw->priv;
2939 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
2940 	struct cfg80211_scan_request *req = &hw_req->req;
2941 	struct ath12k_wmi_scan_req_arg arg = {};
2942 	int ret;
2943 	int i;
2944 
2945 	mutex_lock(&ar->conf_mutex);
2946 
2947 	spin_lock_bh(&ar->data_lock);
2948 	switch (ar->scan.state) {
2949 	case ATH12K_SCAN_IDLE:
2950 		reinit_completion(&ar->scan.started);
2951 		reinit_completion(&ar->scan.completed);
2952 		ar->scan.state = ATH12K_SCAN_STARTING;
2953 		ar->scan.is_roc = false;
2954 		ar->scan.vdev_id = arvif->vdev_id;
2955 		ret = 0;
2956 		break;
2957 	case ATH12K_SCAN_STARTING:
2958 	case ATH12K_SCAN_RUNNING:
2959 	case ATH12K_SCAN_ABORTING:
2960 		ret = -EBUSY;
2961 		break;
2962 	}
2963 	spin_unlock_bh(&ar->data_lock);
2964 
2965 	if (ret)
2966 		goto exit;
2967 
2968 	ath12k_wmi_start_scan_init(ar, &arg);
2969 	arg.vdev_id = arvif->vdev_id;
2970 	arg.scan_id = ATH12K_SCAN_ID;
2971 
2972 	if (req->ie_len) {
2973 		arg.extraie.ptr = kmemdup(req->ie, req->ie_len, GFP_KERNEL);
2974 		if (!arg.extraie.ptr) {
2975 			ret = -ENOMEM;
2976 			goto exit;
2977 		}
2978 		arg.extraie.len = req->ie_len;
2979 	}
2980 
2981 	if (req->n_ssids) {
2982 		arg.num_ssids = req->n_ssids;
2983 		for (i = 0; i < arg.num_ssids; i++)
2984 			arg.ssid[i] = req->ssids[i];
2985 	} else {
2986 		arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
2987 	}
2988 
2989 	if (req->n_channels) {
2990 		arg.num_chan = req->n_channels;
2991 		arg.chan_list = kcalloc(arg.num_chan, sizeof(*arg.chan_list),
2992 					GFP_KERNEL);
2993 
2994 		if (!arg.chan_list) {
2995 			ret = -ENOMEM;
2996 			goto exit;
2997 		}
2998 
2999 		for (i = 0; i < arg.num_chan; i++)
3000 			arg.chan_list[i] = req->channels[i]->center_freq;
3001 	}
3002 
3003 	ret = ath12k_start_scan(ar, &arg);
3004 	if (ret) {
3005 		ath12k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
3006 		spin_lock_bh(&ar->data_lock);
3007 		ar->scan.state = ATH12K_SCAN_IDLE;
3008 		spin_unlock_bh(&ar->data_lock);
3009 	}
3010 
3011 	/* Add a margin to account for event/command processing */
3012 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
3013 				     msecs_to_jiffies(arg.max_scan_time +
3014 						      ATH12K_MAC_SCAN_TIMEOUT_MSECS));
3015 
3016 exit:
3017 	kfree(arg.chan_list);
3018 
3019 	if (req->ie_len)
3020 		kfree(arg.extraie.ptr);
3021 
3022 	mutex_unlock(&ar->conf_mutex);
3023 	return ret;
3024 }
3025 
ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)3026 static void ath12k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
3027 					 struct ieee80211_vif *vif)
3028 {
3029 	struct ath12k *ar = hw->priv;
3030 
3031 	mutex_lock(&ar->conf_mutex);
3032 	ath12k_scan_abort(ar);
3033 	mutex_unlock(&ar->conf_mutex);
3034 
3035 	cancel_delayed_work_sync(&ar->scan.timeout);
3036 }
3037 
ath12k_install_key(struct ath12k_vif * arvif,struct ieee80211_key_conf * key,enum set_key_cmd cmd,const u8 * macaddr,u32 flags)3038 static int ath12k_install_key(struct ath12k_vif *arvif,
3039 			      struct ieee80211_key_conf *key,
3040 			      enum set_key_cmd cmd,
3041 			      const u8 *macaddr, u32 flags)
3042 {
3043 	int ret;
3044 	struct ath12k *ar = arvif->ar;
3045 	struct wmi_vdev_install_key_arg arg = {
3046 		.vdev_id = arvif->vdev_id,
3047 		.key_idx = key->keyidx,
3048 		.key_len = key->keylen,
3049 		.key_data = key->key,
3050 		.key_flags = flags,
3051 		.macaddr = macaddr,
3052 	};
3053 
3054 	lockdep_assert_held(&arvif->ar->conf_mutex);
3055 
3056 	reinit_completion(&ar->install_key_done);
3057 
3058 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
3059 		return 0;
3060 
3061 	if (cmd == DISABLE_KEY) {
3062 		/* TODO: Check if FW expects  value other than NONE for del */
3063 		/* arg.key_cipher = WMI_CIPHER_NONE; */
3064 		arg.key_len = 0;
3065 		arg.key_data = NULL;
3066 		goto install;
3067 	}
3068 
3069 	switch (key->cipher) {
3070 	case WLAN_CIPHER_SUITE_CCMP:
3071 		arg.key_cipher = WMI_CIPHER_AES_CCM;
3072 		/* TODO: Re-check if flag is valid */
3073 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
3074 		break;
3075 	case WLAN_CIPHER_SUITE_TKIP:
3076 		arg.key_cipher = WMI_CIPHER_TKIP;
3077 		arg.key_txmic_len = 8;
3078 		arg.key_rxmic_len = 8;
3079 		break;
3080 	case WLAN_CIPHER_SUITE_CCMP_256:
3081 		arg.key_cipher = WMI_CIPHER_AES_CCM;
3082 		break;
3083 	case WLAN_CIPHER_SUITE_GCMP:
3084 	case WLAN_CIPHER_SUITE_GCMP_256:
3085 		arg.key_cipher = WMI_CIPHER_AES_GCM;
3086 		break;
3087 	default:
3088 		ath12k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
3089 		return -EOPNOTSUPP;
3090 	}
3091 
3092 	if (test_bit(ATH12K_FLAG_RAW_MODE, &ar->ab->dev_flags))
3093 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
3094 			      IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
3095 
3096 install:
3097 	ret = ath12k_wmi_vdev_install_key(arvif->ar, &arg);
3098 
3099 	if (ret)
3100 		return ret;
3101 
3102 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
3103 		return -ETIMEDOUT;
3104 
3105 	if (ether_addr_equal(macaddr, arvif->vif->addr))
3106 		arvif->key_cipher = key->cipher;
3107 
3108 	return ar->install_key_status ? -EINVAL : 0;
3109 }
3110 
ath12k_clear_peer_keys(struct ath12k_vif * arvif,const u8 * addr)3111 static int ath12k_clear_peer_keys(struct ath12k_vif *arvif,
3112 				  const u8 *addr)
3113 {
3114 	struct ath12k *ar = arvif->ar;
3115 	struct ath12k_base *ab = ar->ab;
3116 	struct ath12k_peer *peer;
3117 	int first_errno = 0;
3118 	int ret;
3119 	int i;
3120 	u32 flags = 0;
3121 
3122 	lockdep_assert_held(&ar->conf_mutex);
3123 
3124 	spin_lock_bh(&ab->base_lock);
3125 	peer = ath12k_peer_find(ab, arvif->vdev_id, addr);
3126 	spin_unlock_bh(&ab->base_lock);
3127 
3128 	if (!peer)
3129 		return -ENOENT;
3130 
3131 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
3132 		if (!peer->keys[i])
3133 			continue;
3134 
3135 		/* key flags are not required to delete the key */
3136 		ret = ath12k_install_key(arvif, peer->keys[i],
3137 					 DISABLE_KEY, addr, flags);
3138 		if (ret < 0 && first_errno == 0)
3139 			first_errno = ret;
3140 
3141 		if (ret < 0)
3142 			ath12k_warn(ab, "failed to remove peer key %d: %d\n",
3143 				    i, ret);
3144 
3145 		spin_lock_bh(&ab->base_lock);
3146 		peer->keys[i] = NULL;
3147 		spin_unlock_bh(&ab->base_lock);
3148 	}
3149 
3150 	return first_errno;
3151 }
3152 
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)3153 static int ath12k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3154 				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3155 				 struct ieee80211_key_conf *key)
3156 {
3157 	struct ath12k *ar = hw->priv;
3158 	struct ath12k_base *ab = ar->ab;
3159 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3160 	struct ath12k_peer *peer;
3161 	struct ath12k_sta *arsta;
3162 	const u8 *peer_addr;
3163 	int ret = 0;
3164 	u32 flags = 0;
3165 
3166 	/* BIP needs to be done in software */
3167 	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3168 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3169 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
3170 	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
3171 		return 1;
3172 
3173 	if (test_bit(ATH12K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
3174 		return 1;
3175 
3176 	if (key->keyidx > WMI_MAX_KEY_INDEX)
3177 		return -ENOSPC;
3178 
3179 	mutex_lock(&ar->conf_mutex);
3180 
3181 	if (sta)
3182 		peer_addr = sta->addr;
3183 	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3184 		peer_addr = vif->bss_conf.bssid;
3185 	else
3186 		peer_addr = vif->addr;
3187 
3188 	key->hw_key_idx = key->keyidx;
3189 
3190 	/* the peer should not disappear in mid-way (unless FW goes awry) since
3191 	 * we already hold conf_mutex. we just make sure its there now.
3192 	 */
3193 	spin_lock_bh(&ab->base_lock);
3194 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
3195 	spin_unlock_bh(&ab->base_lock);
3196 
3197 	if (!peer) {
3198 		if (cmd == SET_KEY) {
3199 			ath12k_warn(ab, "cannot install key for non-existent peer %pM\n",
3200 				    peer_addr);
3201 			ret = -EOPNOTSUPP;
3202 			goto exit;
3203 		} else {
3204 			/* if the peer doesn't exist there is no key to disable
3205 			 * anymore
3206 			 */
3207 			goto exit;
3208 		}
3209 	}
3210 
3211 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3212 		flags |= WMI_KEY_PAIRWISE;
3213 	else
3214 		flags |= WMI_KEY_GROUP;
3215 
3216 	ret = ath12k_install_key(arvif, key, cmd, peer_addr, flags);
3217 	if (ret) {
3218 		ath12k_warn(ab, "ath12k_install_key failed (%d)\n", ret);
3219 		goto exit;
3220 	}
3221 
3222 	ret = ath12k_dp_rx_peer_pn_replay_config(arvif, peer_addr, cmd, key);
3223 	if (ret) {
3224 		ath12k_warn(ab, "failed to offload PN replay detection %d\n", ret);
3225 		goto exit;
3226 	}
3227 
3228 	spin_lock_bh(&ab->base_lock);
3229 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
3230 	if (peer && cmd == SET_KEY) {
3231 		peer->keys[key->keyidx] = key;
3232 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3233 			peer->ucast_keyidx = key->keyidx;
3234 			peer->sec_type = ath12k_dp_tx_get_encrypt_type(key->cipher);
3235 		} else {
3236 			peer->mcast_keyidx = key->keyidx;
3237 			peer->sec_type_grp = ath12k_dp_tx_get_encrypt_type(key->cipher);
3238 		}
3239 	} else if (peer && cmd == DISABLE_KEY) {
3240 		peer->keys[key->keyidx] = NULL;
3241 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3242 			peer->ucast_keyidx = 0;
3243 		else
3244 			peer->mcast_keyidx = 0;
3245 	} else if (!peer)
3246 		/* impossible unless FW goes crazy */
3247 		ath12k_warn(ab, "peer %pM disappeared!\n", peer_addr);
3248 
3249 	if (sta) {
3250 		arsta = (struct ath12k_sta *)sta->drv_priv;
3251 
3252 		switch (key->cipher) {
3253 		case WLAN_CIPHER_SUITE_TKIP:
3254 		case WLAN_CIPHER_SUITE_CCMP:
3255 		case WLAN_CIPHER_SUITE_CCMP_256:
3256 		case WLAN_CIPHER_SUITE_GCMP:
3257 		case WLAN_CIPHER_SUITE_GCMP_256:
3258 			if (cmd == SET_KEY)
3259 				arsta->pn_type = HAL_PN_TYPE_WPA;
3260 			else
3261 				arsta->pn_type = HAL_PN_TYPE_NONE;
3262 			break;
3263 		default:
3264 			arsta->pn_type = HAL_PN_TYPE_NONE;
3265 			break;
3266 		}
3267 	}
3268 
3269 	spin_unlock_bh(&ab->base_lock);
3270 
3271 exit:
3272 	mutex_unlock(&ar->conf_mutex);
3273 	return ret;
3274 }
3275 
3276 static int
ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)3277 ath12k_mac_bitrate_mask_num_vht_rates(struct ath12k *ar,
3278 				      enum nl80211_band band,
3279 				      const struct cfg80211_bitrate_mask *mask)
3280 {
3281 	int num_rates = 0;
3282 	int i;
3283 
3284 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
3285 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
3286 
3287 	return num_rates;
3288 }
3289 
3290 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)3291 ath12k_mac_set_peer_vht_fixed_rate(struct ath12k_vif *arvif,
3292 				   struct ieee80211_sta *sta,
3293 				   const struct cfg80211_bitrate_mask *mask,
3294 				   enum nl80211_band band)
3295 {
3296 	struct ath12k *ar = arvif->ar;
3297 	u8 vht_rate, nss;
3298 	u32 rate_code;
3299 	int ret, i;
3300 
3301 	lockdep_assert_held(&ar->conf_mutex);
3302 
3303 	nss = 0;
3304 
3305 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
3306 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
3307 			nss = i + 1;
3308 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
3309 		}
3310 	}
3311 
3312 	if (!nss) {
3313 		ath12k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
3314 			    sta->addr);
3315 		return -EINVAL;
3316 	}
3317 
3318 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3319 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
3320 		   sta->addr);
3321 
3322 	rate_code = ATH12K_HW_RATE_CODE(vht_rate, nss - 1,
3323 					WMI_RATE_PREAMBLE_VHT);
3324 	ret = ath12k_wmi_set_peer_param(ar, sta->addr,
3325 					arvif->vdev_id,
3326 					WMI_PEER_PARAM_FIXED_RATE,
3327 					rate_code);
3328 	if (ret)
3329 		ath12k_warn(ar->ab,
3330 			    "failed to update STA %pM Fixed Rate %d: %d\n",
3331 			     sta->addr, rate_code, ret);
3332 
3333 	return ret;
3334 }
3335 
ath12k_station_assoc(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool reassoc)3336 static int ath12k_station_assoc(struct ath12k *ar,
3337 				struct ieee80211_vif *vif,
3338 				struct ieee80211_sta *sta,
3339 				bool reassoc)
3340 {
3341 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3342 	struct ath12k_wmi_peer_assoc_arg peer_arg;
3343 	int ret;
3344 	struct cfg80211_chan_def def;
3345 	enum nl80211_band band;
3346 	struct cfg80211_bitrate_mask *mask;
3347 	u8 num_vht_rates;
3348 
3349 	lockdep_assert_held(&ar->conf_mutex);
3350 
3351 	if (WARN_ON(ath12k_mac_vif_chan(vif, &def)))
3352 		return -EPERM;
3353 
3354 	band = def.chan->band;
3355 	mask = &arvif->bitrate_mask;
3356 
3357 	ath12k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
3358 
3359 	if (peer_arg.peer_nss < 1) {
3360 		ath12k_warn(ar->ab,
3361 			    "invalid peer NSS %d\n", peer_arg.peer_nss);
3362 		return -EINVAL;
3363 	}
3364 	ret = ath12k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
3365 	if (ret) {
3366 		ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3367 			    sta->addr, arvif->vdev_id, ret);
3368 		return ret;
3369 	}
3370 
3371 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
3372 		ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3373 			    sta->addr, arvif->vdev_id);
3374 		return -ETIMEDOUT;
3375 	}
3376 
3377 	num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
3378 
3379 	/* If single VHT rate is configured (by set_bitrate_mask()),
3380 	 * peer_assoc will disable VHT. This is now enabled by a peer specific
3381 	 * fixed param.
3382 	 * Note that all other rates and NSS will be disabled for this peer.
3383 	 */
3384 	if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
3385 		ret = ath12k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
3386 							 band);
3387 		if (ret)
3388 			return ret;
3389 	}
3390 
3391 	/* Re-assoc is run only to update supported rates for given station. It
3392 	 * doesn't make much sense to reconfigure the peer completely.
3393 	 */
3394 	if (reassoc)
3395 		return 0;
3396 
3397 	ret = ath12k_setup_peer_smps(ar, arvif, sta->addr,
3398 				     &sta->deflink.ht_cap);
3399 	if (ret) {
3400 		ath12k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
3401 			    arvif->vdev_id, ret);
3402 		return ret;
3403 	}
3404 
3405 	if (!sta->wme) {
3406 		arvif->num_legacy_stations++;
3407 		ret = ath12k_recalc_rtscts_prot(arvif);
3408 		if (ret)
3409 			return ret;
3410 	}
3411 
3412 	if (sta->wme && sta->uapsd_queues) {
3413 		ret = ath12k_peer_assoc_qos_ap(ar, arvif, sta);
3414 		if (ret) {
3415 			ath12k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
3416 				    sta->addr, arvif->vdev_id, ret);
3417 			return ret;
3418 		}
3419 	}
3420 
3421 	return 0;
3422 }
3423 
ath12k_station_disassoc(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3424 static int ath12k_station_disassoc(struct ath12k *ar,
3425 				   struct ieee80211_vif *vif,
3426 				   struct ieee80211_sta *sta)
3427 {
3428 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
3429 	int ret;
3430 
3431 	lockdep_assert_held(&ar->conf_mutex);
3432 
3433 	if (!sta->wme) {
3434 		arvif->num_legacy_stations--;
3435 		ret = ath12k_recalc_rtscts_prot(arvif);
3436 		if (ret)
3437 			return ret;
3438 	}
3439 
3440 	ret = ath12k_clear_peer_keys(arvif, sta->addr);
3441 	if (ret) {
3442 		ath12k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
3443 			    arvif->vdev_id, ret);
3444 		return ret;
3445 	}
3446 	return 0;
3447 }
3448 
ath12k_sta_rc_update_wk(struct work_struct * wk)3449 static void ath12k_sta_rc_update_wk(struct work_struct *wk)
3450 {
3451 	struct ath12k *ar;
3452 	struct ath12k_vif *arvif;
3453 	struct ath12k_sta *arsta;
3454 	struct ieee80211_sta *sta;
3455 	struct cfg80211_chan_def def;
3456 	enum nl80211_band band;
3457 	const u8 *ht_mcs_mask;
3458 	const u16 *vht_mcs_mask;
3459 	u32 changed, bw, nss, smps, bw_prev;
3460 	int err, num_vht_rates;
3461 	const struct cfg80211_bitrate_mask *mask;
3462 	struct ath12k_wmi_peer_assoc_arg peer_arg;
3463 	enum wmi_phy_mode peer_phymode;
3464 
3465 	arsta = container_of(wk, struct ath12k_sta, update_wk);
3466 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3467 	arvif = arsta->arvif;
3468 	ar = arvif->ar;
3469 
3470 	if (WARN_ON(ath12k_mac_vif_chan(arvif->vif, &def)))
3471 		return;
3472 
3473 	band = def.chan->band;
3474 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3475 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3476 
3477 	spin_lock_bh(&ar->data_lock);
3478 
3479 	changed = arsta->changed;
3480 	arsta->changed = 0;
3481 
3482 	bw = arsta->bw;
3483 	bw_prev = arsta->bw_prev;
3484 	nss = arsta->nss;
3485 	smps = arsta->smps;
3486 
3487 	spin_unlock_bh(&ar->data_lock);
3488 
3489 	mutex_lock(&ar->conf_mutex);
3490 
3491 	nss = max_t(u32, 1, nss);
3492 	nss = min(nss, max(ath12k_mac_max_ht_nss(ht_mcs_mask),
3493 			   ath12k_mac_max_vht_nss(vht_mcs_mask)));
3494 
3495 	if (changed & IEEE80211_RC_BW_CHANGED) {
3496 		ath12k_peer_assoc_h_phymode(ar, arvif->vif, sta, &peer_arg);
3497 		peer_phymode = peer_arg.peer_phymode;
3498 
3499 		if (bw > bw_prev) {
3500 			/* Phymode shows maximum supported channel width, if we
3501 			 * upgrade bandwidth then due to sanity check of firmware,
3502 			 * we have to send WMI_PEER_PHYMODE followed by
3503 			 * WMI_PEER_CHWIDTH
3504 			 */
3505 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth upgrade for sta %pM new %d old %d\n",
3506 				   sta->addr, bw, bw_prev);
3507 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3508 							arvif->vdev_id, WMI_PEER_PHYMODE,
3509 							peer_phymode);
3510 			if (err) {
3511 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
3512 					    sta->addr, peer_phymode, err);
3513 				goto err_rc_bw_changed;
3514 			}
3515 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3516 							arvif->vdev_id, WMI_PEER_CHWIDTH,
3517 							bw);
3518 			if (err)
3519 				ath12k_warn(ar->ab, "failed to update STA %pM to peer bandwidth %d: %d\n",
3520 					    sta->addr, bw, err);
3521 		} else {
3522 			/* When we downgrade bandwidth this will conflict with phymode
3523 			 * and cause to trigger firmware crash. In this case we send
3524 			 * WMI_PEER_CHWIDTH followed by WMI_PEER_PHYMODE
3525 			 */
3526 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac bandwidth downgrade for sta %pM new %d old %d\n",
3527 				   sta->addr, bw, bw_prev);
3528 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3529 							arvif->vdev_id, WMI_PEER_CHWIDTH,
3530 							bw);
3531 			if (err) {
3532 				ath12k_warn(ar->ab, "failed to update STA %pM peer to bandwidth %d: %d\n",
3533 					    sta->addr, bw, err);
3534 				goto err_rc_bw_changed;
3535 			}
3536 			err = ath12k_wmi_set_peer_param(ar, sta->addr,
3537 							arvif->vdev_id, WMI_PEER_PHYMODE,
3538 							peer_phymode);
3539 			if (err)
3540 				ath12k_warn(ar->ab, "failed to update STA %pM to peer phymode %d: %d\n",
3541 					    sta->addr, peer_phymode, err);
3542 		}
3543 	}
3544 
3545 	if (changed & IEEE80211_RC_NSS_CHANGED) {
3546 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM nss %d\n",
3547 			   sta->addr, nss);
3548 
3549 		err = ath12k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3550 						WMI_PEER_NSS, nss);
3551 		if (err)
3552 			ath12k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
3553 				    sta->addr, nss, err);
3554 	}
3555 
3556 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3557 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac update sta %pM smps %d\n",
3558 			   sta->addr, smps);
3559 
3560 		err = ath12k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3561 						WMI_PEER_MIMO_PS_STATE, smps);
3562 		if (err)
3563 			ath12k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
3564 				    sta->addr, smps, err);
3565 	}
3566 
3567 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
3568 		mask = &arvif->bitrate_mask;
3569 		num_vht_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
3570 								      mask);
3571 
3572 		/* Peer_assoc_prepare will reject vht rates in
3573 		 * bitrate_mask if its not available in range format and
3574 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
3575 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
3576 		 * But, Single rate in VHT mask can be set as per-peer
3577 		 * fixed rate. But even if any HT rates are configured in
3578 		 * the bitrate mask, device will not switch to those rates
3579 		 * when per-peer Fixed rate is set.
3580 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
3581 		 * across HT/VHT and for multiple VHT MCS support.
3582 		 */
3583 		if (sta->deflink.vht_cap.vht_supported && num_vht_rates == 1) {
3584 			ath12k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
3585 							   band);
3586 		} else {
3587 			/* If the peer is non-VHT or no fixed VHT rate
3588 			 * is provided in the new bitrate mask we set the
3589 			 * other rates using peer_assoc command.
3590 			 */
3591 			ath12k_peer_assoc_prepare(ar, arvif->vif, sta,
3592 						  &peer_arg, true);
3593 
3594 			err = ath12k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
3595 			if (err)
3596 				ath12k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3597 					    sta->addr, arvif->vdev_id, err);
3598 
3599 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
3600 				ath12k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3601 					    sta->addr, arvif->vdev_id);
3602 		}
3603 	}
3604 err_rc_bw_changed:
3605 	mutex_unlock(&ar->conf_mutex);
3606 }
3607 
ath12k_mac_inc_num_stations(struct ath12k_vif * arvif,struct ieee80211_sta * sta)3608 static int ath12k_mac_inc_num_stations(struct ath12k_vif *arvif,
3609 				       struct ieee80211_sta *sta)
3610 {
3611 	struct ath12k *ar = arvif->ar;
3612 
3613 	lockdep_assert_held(&ar->conf_mutex);
3614 
3615 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
3616 		return 0;
3617 
3618 	if (ar->num_stations >= ar->max_num_stations)
3619 		return -ENOBUFS;
3620 
3621 	ar->num_stations++;
3622 
3623 	return 0;
3624 }
3625 
ath12k_mac_dec_num_stations(struct ath12k_vif * arvif,struct ieee80211_sta * sta)3626 static void ath12k_mac_dec_num_stations(struct ath12k_vif *arvif,
3627 					struct ieee80211_sta *sta)
3628 {
3629 	struct ath12k *ar = arvif->ar;
3630 
3631 	lockdep_assert_held(&ar->conf_mutex);
3632 
3633 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
3634 		return;
3635 
3636 	ar->num_stations--;
3637 }
3638 
ath12k_mac_station_add(struct ath12k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3639 static int ath12k_mac_station_add(struct ath12k *ar,
3640 				  struct ieee80211_vif *vif,
3641 				  struct ieee80211_sta *sta)
3642 {
3643 	struct ath12k_base *ab = ar->ab;
3644 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3645 	struct ath12k_sta *arsta = (struct ath12k_sta *)sta->drv_priv;
3646 	struct ath12k_wmi_peer_create_arg peer_param;
3647 	int ret;
3648 
3649 	lockdep_assert_held(&ar->conf_mutex);
3650 
3651 	ret = ath12k_mac_inc_num_stations(arvif, sta);
3652 	if (ret) {
3653 		ath12k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
3654 			    ar->max_num_stations);
3655 		goto exit;
3656 	}
3657 
3658 	arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
3659 	if (!arsta->rx_stats) {
3660 		ret = -ENOMEM;
3661 		goto dec_num_station;
3662 	}
3663 
3664 	peer_param.vdev_id = arvif->vdev_id;
3665 	peer_param.peer_addr = sta->addr;
3666 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
3667 
3668 	ret = ath12k_peer_create(ar, arvif, sta, &peer_param);
3669 	if (ret) {
3670 		ath12k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
3671 			    sta->addr, arvif->vdev_id);
3672 		goto free_peer;
3673 	}
3674 
3675 	ath12k_dbg(ab, ATH12K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
3676 		   sta->addr, arvif->vdev_id);
3677 
3678 	if (ieee80211_vif_is_mesh(vif)) {
3679 		ret = ath12k_wmi_set_peer_param(ar, sta->addr,
3680 						arvif->vdev_id,
3681 						WMI_PEER_USE_4ADDR, 1);
3682 		if (ret) {
3683 			ath12k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
3684 				    sta->addr, ret);
3685 			goto free_peer;
3686 		}
3687 	}
3688 
3689 	ret = ath12k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
3690 	if (ret) {
3691 		ath12k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
3692 			    sta->addr, arvif->vdev_id, ret);
3693 		goto free_peer;
3694 	}
3695 
3696 	if (ab->hw_params->vdev_start_delay &&
3697 	    !arvif->is_started &&
3698 	    arvif->vdev_type != WMI_VDEV_TYPE_AP) {
3699 		ret = ath12k_start_vdev_delay(ar->hw, vif);
3700 		if (ret) {
3701 			ath12k_warn(ab, "failed to delay vdev start: %d\n", ret);
3702 			goto free_peer;
3703 		}
3704 	}
3705 
3706 	return 0;
3707 
3708 free_peer:
3709 	ath12k_peer_delete(ar, arvif->vdev_id, sta->addr);
3710 dec_num_station:
3711 	ath12k_mac_dec_num_stations(arvif, sta);
3712 exit:
3713 	return ret;
3714 }
3715 
ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k * ar,struct ieee80211_sta * sta)3716 static u32 ath12k_mac_ieee80211_sta_bw_to_wmi(struct ath12k *ar,
3717 					      struct ieee80211_sta *sta)
3718 {
3719 	u32 bw = WMI_PEER_CHWIDTH_20MHZ;
3720 
3721 	switch (sta->deflink.bandwidth) {
3722 	case IEEE80211_STA_RX_BW_20:
3723 		bw = WMI_PEER_CHWIDTH_20MHZ;
3724 		break;
3725 	case IEEE80211_STA_RX_BW_40:
3726 		bw = WMI_PEER_CHWIDTH_40MHZ;
3727 		break;
3728 	case IEEE80211_STA_RX_BW_80:
3729 		bw = WMI_PEER_CHWIDTH_80MHZ;
3730 		break;
3731 	case IEEE80211_STA_RX_BW_160:
3732 		bw = WMI_PEER_CHWIDTH_160MHZ;
3733 		break;
3734 	default:
3735 		ath12k_warn(ar->ab, "Invalid bandwidth %d in rc update for %pM\n",
3736 			    sta->deflink.bandwidth, sta->addr);
3737 		bw = WMI_PEER_CHWIDTH_20MHZ;
3738 		break;
3739 	}
3740 
3741 	return bw;
3742 }
3743 
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)3744 static int ath12k_mac_op_sta_state(struct ieee80211_hw *hw,
3745 				   struct ieee80211_vif *vif,
3746 				   struct ieee80211_sta *sta,
3747 				   enum ieee80211_sta_state old_state,
3748 				   enum ieee80211_sta_state new_state)
3749 {
3750 	struct ath12k *ar = hw->priv;
3751 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3752 	struct ath12k_sta *arsta = (struct ath12k_sta *)sta->drv_priv;
3753 	struct ath12k_peer *peer;
3754 	int ret = 0;
3755 
3756 	/* cancel must be done outside the mutex to avoid deadlock */
3757 	if ((old_state == IEEE80211_STA_NONE &&
3758 	     new_state == IEEE80211_STA_NOTEXIST))
3759 		cancel_work_sync(&arsta->update_wk);
3760 
3761 	mutex_lock(&ar->conf_mutex);
3762 
3763 	if (old_state == IEEE80211_STA_NOTEXIST &&
3764 	    new_state == IEEE80211_STA_NONE) {
3765 		memset(arsta, 0, sizeof(*arsta));
3766 		arsta->arvif = arvif;
3767 		INIT_WORK(&arsta->update_wk, ath12k_sta_rc_update_wk);
3768 
3769 		ret = ath12k_mac_station_add(ar, vif, sta);
3770 		if (ret)
3771 			ath12k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
3772 				    sta->addr, arvif->vdev_id);
3773 	} else if ((old_state == IEEE80211_STA_NONE &&
3774 		    new_state == IEEE80211_STA_NOTEXIST)) {
3775 		ath12k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
3776 
3777 		ret = ath12k_peer_delete(ar, arvif->vdev_id, sta->addr);
3778 		if (ret)
3779 			ath12k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
3780 				    sta->addr, arvif->vdev_id);
3781 		else
3782 			ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
3783 				   sta->addr, arvif->vdev_id);
3784 
3785 		ath12k_mac_dec_num_stations(arvif, sta);
3786 		spin_lock_bh(&ar->ab->base_lock);
3787 		peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3788 		if (peer && peer->sta == sta) {
3789 			ath12k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
3790 				    vif->addr, arvif->vdev_id);
3791 			peer->sta = NULL;
3792 			list_del(&peer->list);
3793 			kfree(peer);
3794 			ar->num_peers--;
3795 		}
3796 		spin_unlock_bh(&ar->ab->base_lock);
3797 
3798 		kfree(arsta->rx_stats);
3799 		arsta->rx_stats = NULL;
3800 	} else if (old_state == IEEE80211_STA_AUTH &&
3801 		   new_state == IEEE80211_STA_ASSOC &&
3802 		   (vif->type == NL80211_IFTYPE_AP ||
3803 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
3804 		    vif->type == NL80211_IFTYPE_ADHOC)) {
3805 		ret = ath12k_station_assoc(ar, vif, sta, false);
3806 		if (ret)
3807 			ath12k_warn(ar->ab, "Failed to associate station: %pM\n",
3808 				    sta->addr);
3809 
3810 		spin_lock_bh(&ar->data_lock);
3811 
3812 		arsta->bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
3813 		arsta->bw_prev = sta->deflink.bandwidth;
3814 
3815 		spin_unlock_bh(&ar->data_lock);
3816 	} else if (old_state == IEEE80211_STA_ASSOC &&
3817 		   new_state == IEEE80211_STA_AUTHORIZED) {
3818 		spin_lock_bh(&ar->ab->base_lock);
3819 
3820 		peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3821 		if (peer)
3822 			peer->is_authorized = true;
3823 
3824 		spin_unlock_bh(&ar->ab->base_lock);
3825 
3826 		if (vif->type == NL80211_IFTYPE_STATION && arvif->is_up) {
3827 			ret = ath12k_wmi_set_peer_param(ar, sta->addr,
3828 							arvif->vdev_id,
3829 							WMI_PEER_AUTHORIZE,
3830 							1);
3831 			if (ret)
3832 				ath12k_warn(ar->ab, "Unable to authorize peer %pM vdev %d: %d\n",
3833 					    sta->addr, arvif->vdev_id, ret);
3834 		}
3835 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3836 		   new_state == IEEE80211_STA_ASSOC) {
3837 		spin_lock_bh(&ar->ab->base_lock);
3838 
3839 		peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3840 		if (peer)
3841 			peer->is_authorized = false;
3842 
3843 		spin_unlock_bh(&ar->ab->base_lock);
3844 	} else if (old_state == IEEE80211_STA_ASSOC &&
3845 		   new_state == IEEE80211_STA_AUTH &&
3846 		   (vif->type == NL80211_IFTYPE_AP ||
3847 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
3848 		    vif->type == NL80211_IFTYPE_ADHOC)) {
3849 		ret = ath12k_station_disassoc(ar, vif, sta);
3850 		if (ret)
3851 			ath12k_warn(ar->ab, "Failed to disassociate station: %pM\n",
3852 				    sta->addr);
3853 	}
3854 
3855 	mutex_unlock(&ar->conf_mutex);
3856 	return ret;
3857 }
3858 
ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3859 static int ath12k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
3860 				       struct ieee80211_vif *vif,
3861 				       struct ieee80211_sta *sta)
3862 {
3863 	struct ath12k *ar = hw->priv;
3864 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
3865 	int ret;
3866 	s16 txpwr;
3867 
3868 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
3869 		txpwr = 0;
3870 	} else {
3871 		txpwr = sta->deflink.txpwr.power;
3872 		if (!txpwr)
3873 			return -EINVAL;
3874 	}
3875 
3876 	if (txpwr > ATH12K_TX_POWER_MAX_VAL || txpwr < ATH12K_TX_POWER_MIN_VAL)
3877 		return -EINVAL;
3878 
3879 	mutex_lock(&ar->conf_mutex);
3880 
3881 	ret = ath12k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3882 					WMI_PEER_USE_FIXED_PWR, txpwr);
3883 	if (ret) {
3884 		ath12k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
3885 			    ret);
3886 		goto out;
3887 	}
3888 
3889 out:
3890 	mutex_unlock(&ar->conf_mutex);
3891 	return ret;
3892 }
3893 
ath12k_mac_op_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 changed)3894 static void ath12k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
3895 					struct ieee80211_vif *vif,
3896 					struct ieee80211_sta *sta,
3897 					u32 changed)
3898 {
3899 	struct ath12k *ar = hw->priv;
3900 	struct ath12k_sta *arsta = (struct ath12k_sta *)sta->drv_priv;
3901 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
3902 	struct ath12k_peer *peer;
3903 	u32 bw, smps;
3904 
3905 	spin_lock_bh(&ar->ab->base_lock);
3906 
3907 	peer = ath12k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3908 	if (!peer) {
3909 		spin_unlock_bh(&ar->ab->base_lock);
3910 		ath12k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
3911 			    sta->addr, arvif->vdev_id);
3912 		return;
3913 	}
3914 
3915 	spin_unlock_bh(&ar->ab->base_lock);
3916 
3917 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
3918 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
3919 		   sta->addr, changed, sta->deflink.bandwidth, sta->deflink.rx_nss,
3920 		   sta->deflink.smps_mode);
3921 
3922 	spin_lock_bh(&ar->data_lock);
3923 
3924 	if (changed & IEEE80211_RC_BW_CHANGED) {
3925 		bw = ath12k_mac_ieee80211_sta_bw_to_wmi(ar, sta);
3926 		arsta->bw_prev = arsta->bw;
3927 		arsta->bw = bw;
3928 	}
3929 
3930 	if (changed & IEEE80211_RC_NSS_CHANGED)
3931 		arsta->nss = sta->deflink.rx_nss;
3932 
3933 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3934 		smps = WMI_PEER_SMPS_PS_NONE;
3935 
3936 		switch (sta->deflink.smps_mode) {
3937 		case IEEE80211_SMPS_AUTOMATIC:
3938 		case IEEE80211_SMPS_OFF:
3939 			smps = WMI_PEER_SMPS_PS_NONE;
3940 			break;
3941 		case IEEE80211_SMPS_STATIC:
3942 			smps = WMI_PEER_SMPS_STATIC;
3943 			break;
3944 		case IEEE80211_SMPS_DYNAMIC:
3945 			smps = WMI_PEER_SMPS_DYNAMIC;
3946 			break;
3947 		default:
3948 			ath12k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
3949 				    sta->deflink.smps_mode, sta->addr);
3950 			smps = WMI_PEER_SMPS_PS_NONE;
3951 			break;
3952 		}
3953 
3954 		arsta->smps = smps;
3955 	}
3956 
3957 	arsta->changed |= changed;
3958 
3959 	spin_unlock_bh(&ar->data_lock);
3960 
3961 	ieee80211_queue_work(hw, &arsta->update_wk);
3962 }
3963 
ath12k_conf_tx_uapsd(struct ath12k * ar,struct ieee80211_vif * vif,u16 ac,bool enable)3964 static int ath12k_conf_tx_uapsd(struct ath12k *ar, struct ieee80211_vif *vif,
3965 				u16 ac, bool enable)
3966 {
3967 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
3968 	u32 value;
3969 	int ret;
3970 
3971 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3972 		return 0;
3973 
3974 	switch (ac) {
3975 	case IEEE80211_AC_VO:
3976 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3977 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3978 		break;
3979 	case IEEE80211_AC_VI:
3980 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3981 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3982 		break;
3983 	case IEEE80211_AC_BE:
3984 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3985 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3986 		break;
3987 	case IEEE80211_AC_BK:
3988 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3989 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3990 		break;
3991 	}
3992 
3993 	if (enable)
3994 		arvif->u.sta.uapsd |= value;
3995 	else
3996 		arvif->u.sta.uapsd &= ~value;
3997 
3998 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3999 					  WMI_STA_PS_PARAM_UAPSD,
4000 					  arvif->u.sta.uapsd);
4001 	if (ret) {
4002 		ath12k_warn(ar->ab, "could not set uapsd params %d\n", ret);
4003 		goto exit;
4004 	}
4005 
4006 	if (arvif->u.sta.uapsd)
4007 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4008 	else
4009 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4010 
4011 	ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4012 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4013 					  value);
4014 	if (ret)
4015 		ath12k_warn(ar->ab, "could not set rx wake param %d\n", ret);
4016 
4017 exit:
4018 	return ret;
4019 }
4020 
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)4021 static int ath12k_mac_op_conf_tx(struct ieee80211_hw *hw,
4022 				 struct ieee80211_vif *vif,
4023 				 unsigned int link_id, u16 ac,
4024 				 const struct ieee80211_tx_queue_params *params)
4025 {
4026 	struct ath12k *ar = hw->priv;
4027 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
4028 	struct wmi_wmm_params_arg *p = NULL;
4029 	int ret;
4030 
4031 	mutex_lock(&ar->conf_mutex);
4032 
4033 	switch (ac) {
4034 	case IEEE80211_AC_VO:
4035 		p = &arvif->wmm_params.ac_vo;
4036 		break;
4037 	case IEEE80211_AC_VI:
4038 		p = &arvif->wmm_params.ac_vi;
4039 		break;
4040 	case IEEE80211_AC_BE:
4041 		p = &arvif->wmm_params.ac_be;
4042 		break;
4043 	case IEEE80211_AC_BK:
4044 		p = &arvif->wmm_params.ac_bk;
4045 		break;
4046 	}
4047 
4048 	if (WARN_ON(!p)) {
4049 		ret = -EINVAL;
4050 		goto exit;
4051 	}
4052 
4053 	p->cwmin = params->cw_min;
4054 	p->cwmax = params->cw_max;
4055 	p->aifs = params->aifs;
4056 	p->txop = params->txop;
4057 
4058 	ret = ath12k_wmi_send_wmm_update_cmd(ar, arvif->vdev_id,
4059 					     &arvif->wmm_params);
4060 	if (ret) {
4061 		ath12k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
4062 		goto exit;
4063 	}
4064 
4065 	ret = ath12k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4066 
4067 	if (ret)
4068 		ath12k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
4069 
4070 exit:
4071 	mutex_unlock(&ar->conf_mutex);
4072 	return ret;
4073 }
4074 
4075 static struct ieee80211_sta_ht_cap
ath12k_create_ht_cap(struct ath12k * ar,u32 ar_ht_cap,u32 rate_cap_rx_chainmask)4076 ath12k_create_ht_cap(struct ath12k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
4077 {
4078 	int i;
4079 	struct ieee80211_sta_ht_cap ht_cap = {0};
4080 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
4081 
4082 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
4083 		return ht_cap;
4084 
4085 	ht_cap.ht_supported = 1;
4086 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4087 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
4088 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4089 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4090 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
4091 
4092 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
4093 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4094 
4095 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
4096 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4097 
4098 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
4099 		u32 smps;
4100 
4101 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
4102 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4103 
4104 		ht_cap.cap |= smps;
4105 	}
4106 
4107 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
4108 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4109 
4110 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
4111 		u32 stbc;
4112 
4113 		stbc   = ar_ht_cap;
4114 		stbc  &= WMI_HT_CAP_RX_STBC;
4115 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4116 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4117 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
4118 
4119 		ht_cap.cap |= stbc;
4120 	}
4121 
4122 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
4123 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4124 
4125 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
4126 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4127 
4128 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4129 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4130 
4131 	for (i = 0; i < ar->num_rx_chains; i++) {
4132 		if (rate_cap_rx_chainmask & BIT(i))
4133 			ht_cap.mcs.rx_mask[i] = 0xFF;
4134 	}
4135 
4136 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4137 
4138 	return ht_cap;
4139 }
4140 
ath12k_mac_set_txbf_conf(struct ath12k_vif * arvif)4141 static int ath12k_mac_set_txbf_conf(struct ath12k_vif *arvif)
4142 {
4143 	u32 value = 0;
4144 	struct ath12k *ar = arvif->ar;
4145 	int nsts;
4146 	int sound_dim;
4147 	u32 vht_cap = ar->pdev->cap.vht_cap;
4148 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
4149 
4150 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
4151 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4152 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4153 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
4154 	}
4155 
4156 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
4157 		sound_dim = vht_cap &
4158 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4159 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4160 		if (sound_dim > (ar->num_tx_chains - 1))
4161 			sound_dim = ar->num_tx_chains - 1;
4162 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
4163 	}
4164 
4165 	if (!value)
4166 		return 0;
4167 
4168 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
4169 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
4170 
4171 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
4172 		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
4173 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
4174 	}
4175 
4176 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
4177 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
4178 
4179 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
4180 		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
4181 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
4182 	}
4183 
4184 	return ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4185 					     vdev_param, value);
4186 }
4187 
ath12k_set_vht_txbf_cap(struct ath12k * ar,u32 * vht_cap)4188 static void ath12k_set_vht_txbf_cap(struct ath12k *ar, u32 *vht_cap)
4189 {
4190 	bool subfer, subfee;
4191 	int sound_dim = 0;
4192 
4193 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
4194 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
4195 
4196 	if (ar->num_tx_chains < 2) {
4197 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
4198 		subfer = false;
4199 	}
4200 
4201 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
4202 	if (!subfer)
4203 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
4204 
4205 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
4206 	if (!subfee)
4207 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
4208 
4209 	sound_dim = u32_get_bits(*vht_cap,
4210 				 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
4211 	*vht_cap = u32_replace_bits(*vht_cap, 0,
4212 				    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
4213 
4214 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
4215 
4216 	/* Enable Sounding Dimension Field only if SU BF is enabled */
4217 	if (subfer) {
4218 		if (sound_dim > (ar->num_tx_chains - 1))
4219 			sound_dim = ar->num_tx_chains - 1;
4220 
4221 		*vht_cap = u32_replace_bits(*vht_cap, sound_dim,
4222 					    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
4223 	}
4224 
4225 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
4226 	if (!subfee)
4227 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
4228 }
4229 
4230 static struct ieee80211_sta_vht_cap
ath12k_create_vht_cap(struct ath12k * ar,u32 rate_cap_tx_chainmask,u32 rate_cap_rx_chainmask)4231 ath12k_create_vht_cap(struct ath12k *ar, u32 rate_cap_tx_chainmask,
4232 		      u32 rate_cap_rx_chainmask)
4233 {
4234 	struct ieee80211_sta_vht_cap vht_cap = {0};
4235 	u16 txmcs_map, rxmcs_map;
4236 	int i;
4237 
4238 	vht_cap.vht_supported = 1;
4239 	vht_cap.cap = ar->pdev->cap.vht_cap;
4240 
4241 	ath12k_set_vht_txbf_cap(ar, &vht_cap.cap);
4242 
4243 	/* TODO: Enable back VHT160 mode once association issues are fixed */
4244 	/* Disabling VHT160 and VHT80+80 modes */
4245 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
4246 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
4247 
4248 	rxmcs_map = 0;
4249 	txmcs_map = 0;
4250 	for (i = 0; i < 8; i++) {
4251 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
4252 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4253 		else
4254 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4255 
4256 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
4257 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4258 		else
4259 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4260 	}
4261 
4262 	if (rate_cap_tx_chainmask <= 1)
4263 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
4264 
4265 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
4266 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
4267 
4268 	return vht_cap;
4269 }
4270 
ath12k_mac_setup_ht_vht_cap(struct ath12k * ar,struct ath12k_pdev_cap * cap,u32 * ht_cap_info)4271 static void ath12k_mac_setup_ht_vht_cap(struct ath12k *ar,
4272 					struct ath12k_pdev_cap *cap,
4273 					u32 *ht_cap_info)
4274 {
4275 	struct ieee80211_supported_band *band;
4276 	u32 rate_cap_tx_chainmask;
4277 	u32 rate_cap_rx_chainmask;
4278 	u32 ht_cap;
4279 
4280 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
4281 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
4282 
4283 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
4284 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
4285 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
4286 		if (ht_cap_info)
4287 			*ht_cap_info = ht_cap;
4288 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
4289 						    rate_cap_rx_chainmask);
4290 	}
4291 
4292 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
4293 	    (ar->ab->hw_params->single_pdev_only ||
4294 	     !ar->supports_6ghz)) {
4295 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
4296 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
4297 		if (ht_cap_info)
4298 			*ht_cap_info = ht_cap;
4299 		band->ht_cap = ath12k_create_ht_cap(ar, ht_cap,
4300 						    rate_cap_rx_chainmask);
4301 		band->vht_cap = ath12k_create_vht_cap(ar, rate_cap_tx_chainmask,
4302 						      rate_cap_rx_chainmask);
4303 	}
4304 }
4305 
ath12k_check_chain_mask(struct ath12k * ar,u32 ant,bool is_tx_ant)4306 static int ath12k_check_chain_mask(struct ath12k *ar, u32 ant, bool is_tx_ant)
4307 {
4308 	/* TODO: Check the request chainmask against the supported
4309 	 * chainmask table which is advertised in extented_service_ready event
4310 	 */
4311 
4312 	return 0;
4313 }
4314 
ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg * fw_ppet,u8 * he_ppet)4315 static void ath12k_gen_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
4316 				  u8 *he_ppet)
4317 {
4318 	int nss, ru;
4319 	u8 bit = 7;
4320 
4321 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
4322 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
4323 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
4324 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
4325 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
4326 		for (ru = 0; ru < 4; ru++) {
4327 			u8 val;
4328 			int i;
4329 
4330 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
4331 				continue;
4332 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
4333 			       0x3f;
4334 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
4335 			for (i = 5; i >= 0; i--) {
4336 				he_ppet[bit / 8] |=
4337 					((val >> i) & 0x1) << ((bit % 8));
4338 				bit++;
4339 			}
4340 		}
4341 	}
4342 }
4343 
4344 static void
ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem * he_cap_elem)4345 ath12k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
4346 {
4347 	u8 m;
4348 
4349 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
4350 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
4351 	he_cap_elem->mac_cap_info[0] &= ~m;
4352 
4353 	m = IEEE80211_HE_MAC_CAP2_TRS |
4354 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
4355 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
4356 	he_cap_elem->mac_cap_info[2] &= ~m;
4357 
4358 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
4359 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
4360 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
4361 	he_cap_elem->mac_cap_info[3] &= ~m;
4362 
4363 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
4364 	    IEEE80211_HE_MAC_CAP4_BQR;
4365 	he_cap_elem->mac_cap_info[4] &= ~m;
4366 
4367 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
4368 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
4369 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
4370 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
4371 	he_cap_elem->mac_cap_info[5] &= ~m;
4372 
4373 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
4374 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
4375 	he_cap_elem->phy_cap_info[2] &= ~m;
4376 
4377 	m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
4378 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
4379 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
4380 	he_cap_elem->phy_cap_info[3] &= ~m;
4381 
4382 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
4383 	he_cap_elem->phy_cap_info[4] &= ~m;
4384 
4385 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
4386 	he_cap_elem->phy_cap_info[5] &= ~m;
4387 
4388 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
4389 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
4390 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
4391 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
4392 	he_cap_elem->phy_cap_info[6] &= ~m;
4393 
4394 	m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
4395 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
4396 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
4397 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
4398 	he_cap_elem->phy_cap_info[7] &= ~m;
4399 
4400 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
4401 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
4402 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
4403 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
4404 	he_cap_elem->phy_cap_info[8] &= ~m;
4405 
4406 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
4407 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
4408 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
4409 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
4410 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
4411 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
4412 	he_cap_elem->phy_cap_info[9] &= ~m;
4413 }
4414 
ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap * pcap,struct ath12k_band_cap * bcap)4415 static __le16 ath12k_mac_setup_he_6ghz_cap(struct ath12k_pdev_cap *pcap,
4416 					   struct ath12k_band_cap *bcap)
4417 {
4418 	u8 val;
4419 
4420 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
4421 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
4422 		bcap->he_6ghz_capa |=
4423 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DYNAMIC,
4424 					IEEE80211_HE_6GHZ_CAP_SM_PS);
4425 	else
4426 		bcap->he_6ghz_capa |=
4427 			u32_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED,
4428 					IEEE80211_HE_6GHZ_CAP_SM_PS);
4429 	val = u32_get_bits(pcap->vht_cap,
4430 			   IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
4431 	bcap->he_6ghz_capa |=
4432 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
4433 	val = u32_get_bits(pcap->vht_cap,
4434 			   IEEE80211_VHT_CAP_MAX_MPDU_MASK);
4435 	bcap->he_6ghz_capa |=
4436 		u32_encode_bits(val, IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN);
4437 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
4438 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
4439 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
4440 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
4441 
4442 	return cpu_to_le16(bcap->he_6ghz_capa);
4443 }
4444 
ath12k_mac_copy_he_cap(struct ath12k_band_cap * band_cap,int iftype,u8 num_tx_chains,struct ieee80211_sta_he_cap * he_cap)4445 static void ath12k_mac_copy_he_cap(struct ath12k_band_cap *band_cap,
4446 				   int iftype, u8 num_tx_chains,
4447 				   struct ieee80211_sta_he_cap *he_cap)
4448 {
4449 	struct ieee80211_he_cap_elem *he_cap_elem = &he_cap->he_cap_elem;
4450 	struct ieee80211_he_mcs_nss_supp *mcs_nss = &he_cap->he_mcs_nss_supp;
4451 
4452 	he_cap->has_he = true;
4453 	memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
4454 	       sizeof(he_cap_elem->mac_cap_info));
4455 	memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
4456 	       sizeof(he_cap_elem->phy_cap_info));
4457 
4458 	he_cap_elem->mac_cap_info[1] &=
4459 		IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
4460 
4461 	he_cap_elem->phy_cap_info[5] &=
4462 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
4463 	he_cap_elem->phy_cap_info[5] &=
4464 		~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK;
4465 	he_cap_elem->phy_cap_info[5] |= num_tx_chains - 1;
4466 
4467 	switch (iftype) {
4468 	case NL80211_IFTYPE_AP:
4469 		he_cap_elem->phy_cap_info[3] &=
4470 			~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
4471 		he_cap_elem->phy_cap_info[9] |=
4472 			IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
4473 		break;
4474 	case NL80211_IFTYPE_STATION:
4475 		he_cap_elem->mac_cap_info[0] &= ~IEEE80211_HE_MAC_CAP0_TWT_RES;
4476 		he_cap_elem->mac_cap_info[0] |= IEEE80211_HE_MAC_CAP0_TWT_REQ;
4477 		he_cap_elem->phy_cap_info[9] |=
4478 			IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
4479 		break;
4480 	case NL80211_IFTYPE_MESH_POINT:
4481 		ath12k_mac_filter_he_cap_mesh(he_cap_elem);
4482 		break;
4483 	}
4484 
4485 	mcs_nss->rx_mcs_80 = cpu_to_le16(band_cap->he_mcs & 0xffff);
4486 	mcs_nss->tx_mcs_80 = cpu_to_le16(band_cap->he_mcs & 0xffff);
4487 	mcs_nss->rx_mcs_160 = cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4488 	mcs_nss->tx_mcs_160 = cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4489 	mcs_nss->rx_mcs_80p80 = cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4490 	mcs_nss->tx_mcs_80p80 = cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4491 
4492 	memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
4493 	if (he_cap_elem->phy_cap_info[6] &
4494 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
4495 		ath12k_gen_ppe_thresh(&band_cap->he_ppet, he_cap->ppe_thres);
4496 }
4497 
4498 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)4499 ath12k_mac_copy_eht_mcs_nss(struct ath12k_band_cap *band_cap,
4500 			    struct ieee80211_eht_mcs_nss_supp *mcs_nss,
4501 			    const struct ieee80211_he_cap_elem *he_cap,
4502 			    const struct ieee80211_eht_cap_elem_fixed *eht_cap)
4503 {
4504 	if ((he_cap->phy_cap_info[0] &
4505 	     (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
4506 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G |
4507 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G |
4508 	      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)) == 0)
4509 		memcpy(&mcs_nss->only_20mhz, &band_cap->eht_mcs_20_only,
4510 		       sizeof(struct ieee80211_eht_mcs_nss_supp_20mhz_only));
4511 
4512 	if (he_cap->phy_cap_info[0] &
4513 	    (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G |
4514 	     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))
4515 		memcpy(&mcs_nss->bw._80, &band_cap->eht_mcs_80,
4516 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
4517 
4518 	if (he_cap->phy_cap_info[0] &
4519 	    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
4520 		memcpy(&mcs_nss->bw._160, &band_cap->eht_mcs_160,
4521 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
4522 
4523 	if (eht_cap->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
4524 		memcpy(&mcs_nss->bw._320, &band_cap->eht_mcs_320,
4525 		       sizeof(struct ieee80211_eht_mcs_nss_supp_bw));
4526 }
4527 
ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg * fw_ppet,struct ieee80211_sta_eht_cap * cap)4528 static void ath12k_mac_copy_eht_ppe_thresh(struct ath12k_wmi_ppe_threshold_arg *fw_ppet,
4529 					   struct ieee80211_sta_eht_cap *cap)
4530 {
4531 	u16 bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
4532 	u8 i, nss, ru, ppet_bit_len_per_ru = IEEE80211_EHT_PPE_THRES_INFO_PPET_SIZE * 2;
4533 
4534 	u8p_replace_bits(&cap->eht_ppe_thres[0], fw_ppet->numss_m1,
4535 			 IEEE80211_EHT_PPE_THRES_NSS_MASK);
4536 
4537 	u16p_replace_bits((u16 *)&cap->eht_ppe_thres[0], fw_ppet->ru_bit_mask,
4538 			  IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
4539 
4540 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
4541 		for (ru = 0;
4542 		     ru < hweight16(IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
4543 		     ru++) {
4544 			u32 val = 0;
4545 
4546 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
4547 				continue;
4548 
4549 			u32p_replace_bits(&val, fw_ppet->ppet16_ppet8_ru3_ru0[nss] >>
4550 						(ru * ppet_bit_len_per_ru),
4551 					  GENMASK(ppet_bit_len_per_ru - 1, 0));
4552 
4553 			for (i = 0; i < ppet_bit_len_per_ru; i++) {
4554 				cap->eht_ppe_thres[bit / 8] |=
4555 					(((val >> i) & 0x1) << ((bit % 8)));
4556 				bit++;
4557 			}
4558 		}
4559 	}
4560 }
4561 
ath12k_mac_copy_eht_cap(struct ath12k_band_cap * band_cap,struct ieee80211_he_cap_elem * he_cap_elem,int iftype,struct ieee80211_sta_eht_cap * eht_cap)4562 static void ath12k_mac_copy_eht_cap(struct ath12k_band_cap *band_cap,
4563 				    struct ieee80211_he_cap_elem *he_cap_elem,
4564 				    int iftype,
4565 				    struct ieee80211_sta_eht_cap *eht_cap)
4566 {
4567 	struct ieee80211_eht_cap_elem_fixed *eht_cap_elem = &eht_cap->eht_cap_elem;
4568 
4569 	memset(eht_cap, 0, sizeof(struct ieee80211_sta_eht_cap));
4570 	eht_cap->has_eht = true;
4571 	memcpy(eht_cap_elem->mac_cap_info, band_cap->eht_cap_mac_info,
4572 	       sizeof(eht_cap_elem->mac_cap_info));
4573 	memcpy(eht_cap_elem->phy_cap_info, band_cap->eht_cap_phy_info,
4574 	       sizeof(eht_cap_elem->phy_cap_info));
4575 
4576 	switch (iftype) {
4577 	case NL80211_IFTYPE_AP:
4578 		eht_cap_elem->phy_cap_info[0] &=
4579 			~IEEE80211_EHT_PHY_CAP0_242_TONE_RU_GT20MHZ;
4580 		eht_cap_elem->phy_cap_info[4] &=
4581 			~IEEE80211_EHT_PHY_CAP4_PART_BW_DL_MU_MIMO;
4582 		eht_cap_elem->phy_cap_info[5] &=
4583 			~IEEE80211_EHT_PHY_CAP5_TX_LESS_242_TONE_RU_SUPP;
4584 		break;
4585 	case NL80211_IFTYPE_STATION:
4586 		eht_cap_elem->phy_cap_info[7] &=
4587 			~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_80MHZ |
4588 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ |
4589 			  IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ);
4590 		eht_cap_elem->phy_cap_info[7] &=
4591 			~(IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_80MHZ |
4592 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ |
4593 			  IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ);
4594 		break;
4595 	default:
4596 		break;
4597 	}
4598 
4599 	ath12k_mac_copy_eht_mcs_nss(band_cap, &eht_cap->eht_mcs_nss_supp,
4600 				    he_cap_elem, eht_cap_elem);
4601 
4602 	if (eht_cap_elem->phy_cap_info[5] &
4603 	    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT)
4604 		ath12k_mac_copy_eht_ppe_thresh(&band_cap->eht_ppet, eht_cap);
4605 }
4606 
ath12k_mac_copy_sband_iftype_data(struct ath12k * ar,struct ath12k_pdev_cap * cap,struct ieee80211_sband_iftype_data * data,int band)4607 static int ath12k_mac_copy_sband_iftype_data(struct ath12k *ar,
4608 					     struct ath12k_pdev_cap *cap,
4609 					     struct ieee80211_sband_iftype_data *data,
4610 					     int band)
4611 {
4612 	struct ath12k_band_cap *band_cap = &cap->band[band];
4613 	int i, idx = 0;
4614 
4615 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
4616 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
4617 
4618 		switch (i) {
4619 		case NL80211_IFTYPE_STATION:
4620 		case NL80211_IFTYPE_AP:
4621 		case NL80211_IFTYPE_MESH_POINT:
4622 			break;
4623 
4624 		default:
4625 			continue;
4626 		}
4627 
4628 		data[idx].types_mask = BIT(i);
4629 
4630 		ath12k_mac_copy_he_cap(band_cap, i, ar->num_tx_chains, he_cap);
4631 		if (band == NL80211_BAND_6GHZ) {
4632 			data[idx].he_6ghz_capa.capa =
4633 				ath12k_mac_setup_he_6ghz_cap(cap, band_cap);
4634 		}
4635 		ath12k_mac_copy_eht_cap(band_cap, &he_cap->he_cap_elem, i,
4636 					&data[idx].eht_cap);
4637 		idx++;
4638 	}
4639 
4640 	return idx;
4641 }
4642 
ath12k_mac_setup_sband_iftype_data(struct ath12k * ar,struct ath12k_pdev_cap * cap)4643 static void ath12k_mac_setup_sband_iftype_data(struct ath12k *ar,
4644 					       struct ath12k_pdev_cap *cap)
4645 {
4646 	struct ieee80211_supported_band *sband;
4647 	enum nl80211_band band;
4648 	int count;
4649 
4650 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
4651 		band = NL80211_BAND_2GHZ;
4652 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
4653 							  ar->mac.iftype[band],
4654 							  band);
4655 		sband = &ar->mac.sbands[band];
4656 		sband->iftype_data = ar->mac.iftype[band];
4657 		sband->n_iftype_data = count;
4658 	}
4659 
4660 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
4661 		band = NL80211_BAND_5GHZ;
4662 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
4663 							  ar->mac.iftype[band],
4664 							  band);
4665 		sband = &ar->mac.sbands[band];
4666 		sband->iftype_data = ar->mac.iftype[band];
4667 		sband->n_iftype_data = count;
4668 	}
4669 
4670 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
4671 	    ar->supports_6ghz) {
4672 		band = NL80211_BAND_6GHZ;
4673 		count = ath12k_mac_copy_sband_iftype_data(ar, cap,
4674 							  ar->mac.iftype[band],
4675 							  band);
4676 		sband = &ar->mac.sbands[band];
4677 		sband->iftype_data = ar->mac.iftype[band];
4678 		sband->n_iftype_data = count;
4679 	}
4680 }
4681 
__ath12k_set_antenna(struct ath12k * ar,u32 tx_ant,u32 rx_ant)4682 static int __ath12k_set_antenna(struct ath12k *ar, u32 tx_ant, u32 rx_ant)
4683 {
4684 	int ret;
4685 
4686 	lockdep_assert_held(&ar->conf_mutex);
4687 
4688 	if (ath12k_check_chain_mask(ar, tx_ant, true))
4689 		return -EINVAL;
4690 
4691 	if (ath12k_check_chain_mask(ar, rx_ant, false))
4692 		return -EINVAL;
4693 
4694 	ar->cfg_tx_chainmask = tx_ant;
4695 	ar->cfg_rx_chainmask = rx_ant;
4696 
4697 	if (ar->state != ATH12K_STATE_ON &&
4698 	    ar->state != ATH12K_STATE_RESTARTED)
4699 		return 0;
4700 
4701 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
4702 					tx_ant, ar->pdev->pdev_id);
4703 	if (ret) {
4704 		ath12k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
4705 			    ret, tx_ant);
4706 		return ret;
4707 	}
4708 
4709 	ar->num_tx_chains = hweight32(tx_ant);
4710 
4711 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
4712 					rx_ant, ar->pdev->pdev_id);
4713 	if (ret) {
4714 		ath12k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
4715 			    ret, rx_ant);
4716 		return ret;
4717 	}
4718 
4719 	ar->num_rx_chains = hweight32(rx_ant);
4720 
4721 	/* Reload HT/VHT/HE capability */
4722 	ath12k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
4723 	ath12k_mac_setup_sband_iftype_data(ar, &ar->pdev->cap);
4724 
4725 	return 0;
4726 }
4727 
ath12k_mgmt_over_wmi_tx_drop(struct ath12k * ar,struct sk_buff * skb)4728 static void ath12k_mgmt_over_wmi_tx_drop(struct ath12k *ar, struct sk_buff *skb)
4729 {
4730 	int num_mgmt;
4731 
4732 	ieee80211_free_txskb(ar->hw, skb);
4733 
4734 	num_mgmt = atomic_dec_if_positive(&ar->num_pending_mgmt_tx);
4735 
4736 	if (num_mgmt < 0)
4737 		WARN_ON_ONCE(1);
4738 
4739 	if (!num_mgmt)
4740 		wake_up(&ar->txmgmt_empty_waitq);
4741 }
4742 
ath12k_mac_tx_mgmt_pending_free(int buf_id,void * skb,void * ctx)4743 int ath12k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
4744 {
4745 	struct sk_buff *msdu = skb;
4746 	struct ieee80211_tx_info *info;
4747 	struct ath12k *ar = ctx;
4748 	struct ath12k_base *ab = ar->ab;
4749 
4750 	spin_lock_bh(&ar->txmgmt_idr_lock);
4751 	idr_remove(&ar->txmgmt_idr, buf_id);
4752 	spin_unlock_bh(&ar->txmgmt_idr_lock);
4753 	dma_unmap_single(ab->dev, ATH12K_SKB_CB(msdu)->paddr, msdu->len,
4754 			 DMA_TO_DEVICE);
4755 
4756 	info = IEEE80211_SKB_CB(msdu);
4757 	memset(&info->status, 0, sizeof(info->status));
4758 
4759 	ath12k_mgmt_over_wmi_tx_drop(ar, skb);
4760 
4761 	return 0;
4762 }
4763 
ath12k_mac_vif_txmgmt_idr_remove(int buf_id,void * skb,void * ctx)4764 static int ath12k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
4765 {
4766 	struct ieee80211_vif *vif = ctx;
4767 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
4768 	struct sk_buff *msdu = skb;
4769 	struct ath12k *ar = skb_cb->ar;
4770 	struct ath12k_base *ab = ar->ab;
4771 
4772 	if (skb_cb->vif == vif) {
4773 		spin_lock_bh(&ar->txmgmt_idr_lock);
4774 		idr_remove(&ar->txmgmt_idr, buf_id);
4775 		spin_unlock_bh(&ar->txmgmt_idr_lock);
4776 		dma_unmap_single(ab->dev, skb_cb->paddr, msdu->len,
4777 				 DMA_TO_DEVICE);
4778 	}
4779 
4780 	return 0;
4781 }
4782 
ath12k_mac_mgmt_tx_wmi(struct ath12k * ar,struct ath12k_vif * arvif,struct sk_buff * skb)4783 static int ath12k_mac_mgmt_tx_wmi(struct ath12k *ar, struct ath12k_vif *arvif,
4784 				  struct sk_buff *skb)
4785 {
4786 	struct ath12k_base *ab = ar->ab;
4787 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4788 	struct ieee80211_tx_info *info;
4789 	dma_addr_t paddr;
4790 	int buf_id;
4791 	int ret;
4792 
4793 	ATH12K_SKB_CB(skb)->ar = ar;
4794 	spin_lock_bh(&ar->txmgmt_idr_lock);
4795 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
4796 			   ATH12K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
4797 	spin_unlock_bh(&ar->txmgmt_idr_lock);
4798 	if (buf_id < 0)
4799 		return -ENOSPC;
4800 
4801 	info = IEEE80211_SKB_CB(skb);
4802 	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
4803 		if ((ieee80211_is_action(hdr->frame_control) ||
4804 		     ieee80211_is_deauth(hdr->frame_control) ||
4805 		     ieee80211_is_disassoc(hdr->frame_control)) &&
4806 		     ieee80211_has_protected(hdr->frame_control)) {
4807 			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
4808 		}
4809 	}
4810 
4811 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
4812 	if (dma_mapping_error(ab->dev, paddr)) {
4813 		ath12k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
4814 		ret = -EIO;
4815 		goto err_free_idr;
4816 	}
4817 
4818 	ATH12K_SKB_CB(skb)->paddr = paddr;
4819 
4820 	ret = ath12k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
4821 	if (ret) {
4822 		ath12k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
4823 		goto err_unmap_buf;
4824 	}
4825 
4826 	return 0;
4827 
4828 err_unmap_buf:
4829 	dma_unmap_single(ab->dev, ATH12K_SKB_CB(skb)->paddr,
4830 			 skb->len, DMA_TO_DEVICE);
4831 err_free_idr:
4832 	spin_lock_bh(&ar->txmgmt_idr_lock);
4833 	idr_remove(&ar->txmgmt_idr, buf_id);
4834 	spin_unlock_bh(&ar->txmgmt_idr_lock);
4835 
4836 	return ret;
4837 }
4838 
ath12k_mgmt_over_wmi_tx_purge(struct ath12k * ar)4839 static void ath12k_mgmt_over_wmi_tx_purge(struct ath12k *ar)
4840 {
4841 	struct sk_buff *skb;
4842 
4843 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
4844 		ath12k_mgmt_over_wmi_tx_drop(ar, skb);
4845 }
4846 
ath12k_mgmt_over_wmi_tx_work(struct work_struct * work)4847 static void ath12k_mgmt_over_wmi_tx_work(struct work_struct *work)
4848 {
4849 	struct ath12k *ar = container_of(work, struct ath12k, wmi_mgmt_tx_work);
4850 	struct ath12k_skb_cb *skb_cb;
4851 	struct ath12k_vif *arvif;
4852 	struct sk_buff *skb;
4853 	int ret;
4854 
4855 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
4856 		skb_cb = ATH12K_SKB_CB(skb);
4857 		if (!skb_cb->vif) {
4858 			ath12k_warn(ar->ab, "no vif found for mgmt frame\n");
4859 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
4860 			continue;
4861 		}
4862 
4863 		arvif = ath12k_vif_to_arvif(skb_cb->vif);
4864 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id) &&
4865 		    arvif->is_started) {
4866 			ret = ath12k_mac_mgmt_tx_wmi(ar, arvif, skb);
4867 			if (ret) {
4868 				ath12k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
4869 					    arvif->vdev_id, ret);
4870 				ath12k_mgmt_over_wmi_tx_drop(ar, skb);
4871 			}
4872 		} else {
4873 			ath12k_warn(ar->ab,
4874 				    "dropping mgmt frame for vdev %d, is_started %d\n",
4875 				    arvif->vdev_id,
4876 				    arvif->is_started);
4877 			ath12k_mgmt_over_wmi_tx_drop(ar, skb);
4878 		}
4879 	}
4880 }
4881 
ath12k_mac_mgmt_tx(struct ath12k * ar,struct sk_buff * skb,bool is_prb_rsp)4882 static int ath12k_mac_mgmt_tx(struct ath12k *ar, struct sk_buff *skb,
4883 			      bool is_prb_rsp)
4884 {
4885 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
4886 
4887 	if (test_bit(ATH12K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4888 		return -ESHUTDOWN;
4889 
4890 	/* Drop probe response packets when the pending management tx
4891 	 * count has reached a certain threshold, so as to prioritize
4892 	 * other mgmt packets like auth and assoc to be sent on time
4893 	 * for establishing successful connections.
4894 	 */
4895 	if (is_prb_rsp &&
4896 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH12K_PRB_RSP_DROP_THRESHOLD) {
4897 		ath12k_warn(ar->ab,
4898 			    "dropping probe response as pending queue is almost full\n");
4899 		return -ENOSPC;
4900 	}
4901 
4902 	if (skb_queue_len_lockless(q) >= ATH12K_TX_MGMT_NUM_PENDING_MAX) {
4903 		ath12k_warn(ar->ab, "mgmt tx queue is full\n");
4904 		return -ENOSPC;
4905 	}
4906 
4907 	skb_queue_tail(q, skb);
4908 	atomic_inc(&ar->num_pending_mgmt_tx);
4909 	ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
4910 
4911 	return 0;
4912 }
4913 
ath12k_mac_op_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)4914 static void ath12k_mac_op_tx(struct ieee80211_hw *hw,
4915 			     struct ieee80211_tx_control *control,
4916 			     struct sk_buff *skb)
4917 {
4918 	struct ath12k_skb_cb *skb_cb = ATH12K_SKB_CB(skb);
4919 	struct ath12k *ar = hw->priv;
4920 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4921 	struct ieee80211_vif *vif = info->control.vif;
4922 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
4923 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4924 	struct ieee80211_key_conf *key = info->control.hw_key;
4925 	u32 info_flags = info->flags;
4926 	bool is_prb_rsp;
4927 	int ret;
4928 
4929 	memset(skb_cb, 0, sizeof(*skb_cb));
4930 	skb_cb->vif = vif;
4931 
4932 	if (key) {
4933 		skb_cb->cipher = key->cipher;
4934 		skb_cb->flags |= ATH12K_SKB_CIPHER_SET;
4935 	}
4936 
4937 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4938 		skb_cb->flags |= ATH12K_SKB_HW_80211_ENCAP;
4939 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
4940 		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
4941 		ret = ath12k_mac_mgmt_tx(ar, skb, is_prb_rsp);
4942 		if (ret) {
4943 			ath12k_warn(ar->ab, "failed to queue management frame %d\n",
4944 				    ret);
4945 			ieee80211_free_txskb(ar->hw, skb);
4946 		}
4947 		return;
4948 	}
4949 
4950 	ret = ath12k_dp_tx(ar, arvif, skb);
4951 	if (ret) {
4952 		ath12k_warn(ar->ab, "failed to transmit frame %d\n", ret);
4953 		ieee80211_free_txskb(ar->hw, skb);
4954 	}
4955 }
4956 
ath12k_mac_drain_tx(struct ath12k * ar)4957 void ath12k_mac_drain_tx(struct ath12k *ar)
4958 {
4959 	/* make sure rcu-protected mac80211 tx path itself is drained */
4960 	synchronize_net();
4961 
4962 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
4963 	ath12k_mgmt_over_wmi_tx_purge(ar);
4964 }
4965 
ath12k_mac_config_mon_status_default(struct ath12k * ar,bool enable)4966 static int ath12k_mac_config_mon_status_default(struct ath12k *ar, bool enable)
4967 {
4968 	return -ENOTSUPP;
4969 	/* TODO: Need to support new monitor mode */
4970 }
4971 
ath12k_mac_wait_reconfigure(struct ath12k_base * ab)4972 static void ath12k_mac_wait_reconfigure(struct ath12k_base *ab)
4973 {
4974 	int recovery_start_count;
4975 
4976 	if (!ab->is_reset)
4977 		return;
4978 
4979 	recovery_start_count = atomic_inc_return(&ab->recovery_start_count);
4980 
4981 	ath12k_dbg(ab, ATH12K_DBG_MAC, "recovery start count %d\n", recovery_start_count);
4982 
4983 	if (recovery_start_count == ab->num_radios) {
4984 		complete(&ab->recovery_start);
4985 		ath12k_dbg(ab, ATH12K_DBG_MAC, "recovery started success\n");
4986 	}
4987 
4988 	ath12k_dbg(ab, ATH12K_DBG_MAC, "waiting reconfigure...\n");
4989 
4990 	wait_for_completion_timeout(&ab->reconfigure_complete,
4991 				    ATH12K_RECONFIGURE_TIMEOUT_HZ);
4992 }
4993 
ath12k_mac_op_start(struct ieee80211_hw * hw)4994 static int ath12k_mac_op_start(struct ieee80211_hw *hw)
4995 {
4996 	struct ath12k *ar = hw->priv;
4997 	struct ath12k_base *ab = ar->ab;
4998 	struct ath12k_pdev *pdev = ar->pdev;
4999 	int ret;
5000 
5001 	ath12k_mac_drain_tx(ar);
5002 	mutex_lock(&ar->conf_mutex);
5003 
5004 	switch (ar->state) {
5005 	case ATH12K_STATE_OFF:
5006 		ar->state = ATH12K_STATE_ON;
5007 		break;
5008 	case ATH12K_STATE_RESTARTING:
5009 		ar->state = ATH12K_STATE_RESTARTED;
5010 		ath12k_mac_wait_reconfigure(ab);
5011 		break;
5012 	case ATH12K_STATE_RESTARTED:
5013 	case ATH12K_STATE_WEDGED:
5014 	case ATH12K_STATE_ON:
5015 		WARN_ON(1);
5016 		ret = -EINVAL;
5017 		goto err;
5018 	}
5019 
5020 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
5021 					1, pdev->pdev_id);
5022 
5023 	if (ret) {
5024 		ath12k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
5025 		goto err;
5026 	}
5027 
5028 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
5029 					pdev->pdev_id);
5030 	if (ret) {
5031 		ath12k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
5032 		goto err;
5033 	}
5034 
5035 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
5036 					0, pdev->pdev_id);
5037 	if (ret) {
5038 		ath12k_err(ab, "failed to set ac override for ARP: %d\n",
5039 			   ret);
5040 		goto err;
5041 	}
5042 
5043 	ret = ath12k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
5044 	if (ret) {
5045 		ath12k_err(ab, "failed to offload radar detection: %d\n",
5046 			   ret);
5047 		goto err;
5048 	}
5049 
5050 	ret = ath12k_dp_tx_htt_h2t_ppdu_stats_req(ar,
5051 						  HTT_PPDU_STATS_TAG_DEFAULT);
5052 	if (ret) {
5053 		ath12k_err(ab, "failed to req ppdu stats: %d\n", ret);
5054 		goto err;
5055 	}
5056 
5057 	ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
5058 					1, pdev->pdev_id);
5059 
5060 	if (ret) {
5061 		ath12k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
5062 		goto err;
5063 	}
5064 
5065 	__ath12k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
5066 
5067 	/* TODO: Do we need to enable ANI? */
5068 
5069 	ath12k_reg_update_chan_list(ar);
5070 
5071 	ar->num_started_vdevs = 0;
5072 	ar->num_created_vdevs = 0;
5073 	ar->num_peers = 0;
5074 	ar->allocated_vdev_map = 0;
5075 
5076 	/* Configure monitor status ring with default rx_filter to get rx status
5077 	 * such as rssi, rx_duration.
5078 	 */
5079 	ret = ath12k_mac_config_mon_status_default(ar, true);
5080 	if (ret && (ret != -ENOTSUPP)) {
5081 		ath12k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
5082 			   ret);
5083 		goto err;
5084 	}
5085 
5086 	if (ret == -ENOTSUPP)
5087 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5088 			   "monitor status config is not yet supported");
5089 
5090 	/* Configure the hash seed for hash based reo dest ring selection */
5091 	ath12k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
5092 
5093 	/* allow device to enter IMPS */
5094 	if (ab->hw_params->idle_ps) {
5095 		ret = ath12k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
5096 						1, pdev->pdev_id);
5097 		if (ret) {
5098 			ath12k_err(ab, "failed to enable idle ps: %d\n", ret);
5099 			goto err;
5100 		}
5101 	}
5102 
5103 	mutex_unlock(&ar->conf_mutex);
5104 
5105 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
5106 			   &ab->pdevs[ar->pdev_idx]);
5107 
5108 	return 0;
5109 
5110 err:
5111 	ar->state = ATH12K_STATE_OFF;
5112 	mutex_unlock(&ar->conf_mutex);
5113 
5114 	return ret;
5115 }
5116 
ath12k_mac_op_stop(struct ieee80211_hw * hw)5117 static void ath12k_mac_op_stop(struct ieee80211_hw *hw)
5118 {
5119 	struct ath12k *ar = hw->priv;
5120 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
5121 	int ret;
5122 
5123 	ath12k_mac_drain_tx(ar);
5124 
5125 	mutex_lock(&ar->conf_mutex);
5126 	ret = ath12k_mac_config_mon_status_default(ar, false);
5127 	if (ret && (ret != -ENOTSUPP))
5128 		ath12k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
5129 			   ret);
5130 
5131 	clear_bit(ATH12K_CAC_RUNNING, &ar->dev_flags);
5132 	ar->state = ATH12K_STATE_OFF;
5133 	mutex_unlock(&ar->conf_mutex);
5134 
5135 	cancel_delayed_work_sync(&ar->scan.timeout);
5136 	cancel_work_sync(&ar->regd_update_work);
5137 
5138 	spin_lock_bh(&ar->data_lock);
5139 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
5140 		list_del(&ppdu_stats->list);
5141 		kfree(ppdu_stats);
5142 	}
5143 	spin_unlock_bh(&ar->data_lock);
5144 
5145 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
5146 
5147 	synchronize_rcu();
5148 
5149 	atomic_set(&ar->num_pending_mgmt_tx, 0);
5150 }
5151 
5152 static u8
ath12k_mac_get_vdev_stats_id(struct ath12k_vif * arvif)5153 ath12k_mac_get_vdev_stats_id(struct ath12k_vif *arvif)
5154 {
5155 	struct ath12k_base *ab = arvif->ar->ab;
5156 	u8 vdev_stats_id = 0;
5157 
5158 	do {
5159 		if (ab->free_vdev_stats_id_map & (1LL << vdev_stats_id)) {
5160 			vdev_stats_id++;
5161 			if (vdev_stats_id >= ATH12K_MAX_VDEV_STATS_ID) {
5162 				vdev_stats_id = ATH12K_INVAL_VDEV_STATS_ID;
5163 				break;
5164 			}
5165 		} else {
5166 			ab->free_vdev_stats_id_map |= (1LL << vdev_stats_id);
5167 			break;
5168 		}
5169 	} while (vdev_stats_id);
5170 
5171 	arvif->vdev_stats_id = vdev_stats_id;
5172 	return vdev_stats_id;
5173 }
5174 
ath12k_mac_setup_vdev_create_arg(struct ath12k_vif * arvif,struct ath12k_wmi_vdev_create_arg * arg)5175 static void ath12k_mac_setup_vdev_create_arg(struct ath12k_vif *arvif,
5176 					     struct ath12k_wmi_vdev_create_arg *arg)
5177 {
5178 	struct ath12k *ar = arvif->ar;
5179 	struct ath12k_pdev *pdev = ar->pdev;
5180 
5181 	arg->if_id = arvif->vdev_id;
5182 	arg->type = arvif->vdev_type;
5183 	arg->subtype = arvif->vdev_subtype;
5184 	arg->pdev_id = pdev->pdev_id;
5185 
5186 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
5187 		arg->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
5188 		arg->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
5189 	}
5190 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
5191 		arg->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
5192 		arg->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
5193 	}
5194 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
5195 	    ar->supports_6ghz) {
5196 		arg->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
5197 		arg->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
5198 	}
5199 
5200 	arg->if_stats_id = ath12k_mac_get_vdev_stats_id(arvif);
5201 }
5202 
5203 static u32
ath12k_mac_prepare_he_mode(struct ath12k_pdev * pdev,u32 viftype)5204 ath12k_mac_prepare_he_mode(struct ath12k_pdev *pdev, u32 viftype)
5205 {
5206 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
5207 	struct ath12k_band_cap *cap_band = NULL;
5208 	u32 *hecap_phy_ptr = NULL;
5209 	u32 hemode;
5210 
5211 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP)
5212 		cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
5213 	else
5214 		cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
5215 
5216 	hecap_phy_ptr = &cap_band->he_cap_phy_info[0];
5217 
5218 	hemode = u32_encode_bits(HE_SU_BFEE_ENABLE, HE_MODE_SU_TX_BFEE) |
5219 		 u32_encode_bits(HECAP_PHY_SUBFMR_GET(hecap_phy_ptr),
5220 				 HE_MODE_SU_TX_BFER) |
5221 		 u32_encode_bits(HECAP_PHY_ULMUMIMO_GET(hecap_phy_ptr),
5222 				 HE_MODE_UL_MUMIMO);
5223 
5224 	/* TODO: WDS and other modes */
5225 	if (viftype == NL80211_IFTYPE_AP) {
5226 		hemode |= u32_encode_bits(HECAP_PHY_MUBFMR_GET(hecap_phy_ptr),
5227 					  HE_MODE_MU_TX_BFER) |
5228 			  u32_encode_bits(HE_DL_MUOFDMA_ENABLE, HE_MODE_DL_OFDMA) |
5229 			  u32_encode_bits(HE_UL_MUOFDMA_ENABLE, HE_MODE_UL_OFDMA);
5230 	} else {
5231 		hemode |= u32_encode_bits(HE_MU_BFEE_ENABLE, HE_MODE_MU_TX_BFEE);
5232 	}
5233 
5234 	return hemode;
5235 }
5236 
ath12k_set_he_mu_sounding_mode(struct ath12k * ar,struct ath12k_vif * arvif)5237 static int ath12k_set_he_mu_sounding_mode(struct ath12k *ar,
5238 					  struct ath12k_vif *arvif)
5239 {
5240 	u32 param_id, param_value;
5241 	struct ath12k_base *ab = ar->ab;
5242 	int ret;
5243 
5244 	param_id = WMI_VDEV_PARAM_SET_HEMU_MODE;
5245 	param_value = ath12k_mac_prepare_he_mode(ar->pdev, arvif->vif->type);
5246 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5247 					    param_id, param_value);
5248 	if (ret) {
5249 		ath12k_warn(ab, "failed to set vdev %d HE MU mode: %d param_value %x\n",
5250 			    arvif->vdev_id, ret, param_value);
5251 		return ret;
5252 	}
5253 	param_id = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
5254 	param_value =
5255 		u32_encode_bits(HE_VHT_SOUNDING_MODE_ENABLE, HE_VHT_SOUNDING_MODE) |
5256 		u32_encode_bits(HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE,
5257 				HE_TRIG_NONTRIG_SOUNDING_MODE);
5258 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5259 					    param_id, param_value);
5260 	if (ret) {
5261 		ath12k_warn(ab, "failed to set vdev %d HE MU mode: %d\n",
5262 			    arvif->vdev_id, ret);
5263 		return ret;
5264 	}
5265 	return ret;
5266 }
5267 
ath12k_mac_op_update_vif_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5268 static void ath12k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
5269 					     struct ieee80211_vif *vif)
5270 {
5271 	struct ath12k *ar = hw->priv;
5272 	struct ath12k_base *ab = ar->ab;
5273 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
5274 	u32 param_id, param_value;
5275 	int ret;
5276 
5277 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
5278 	if (vif->type != NL80211_IFTYPE_STATION &&
5279 	    vif->type != NL80211_IFTYPE_AP)
5280 		vif->offload_flags &= ~(IEEE80211_OFFLOAD_ENCAP_ENABLED |
5281 					IEEE80211_OFFLOAD_DECAP_ENABLED);
5282 
5283 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
5284 		arvif->tx_encap_type = ATH12K_HW_TXRX_ETHERNET;
5285 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
5286 		arvif->tx_encap_type = ATH12K_HW_TXRX_RAW;
5287 	else
5288 		arvif->tx_encap_type = ATH12K_HW_TXRX_NATIVE_WIFI;
5289 
5290 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5291 					    param_id, arvif->tx_encap_type);
5292 	if (ret) {
5293 		ath12k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
5294 			    arvif->vdev_id, ret);
5295 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
5296 	}
5297 
5298 	param_id = WMI_VDEV_PARAM_RX_DECAP_TYPE;
5299 	if (vif->offload_flags & IEEE80211_OFFLOAD_DECAP_ENABLED)
5300 		param_value = ATH12K_HW_TXRX_ETHERNET;
5301 	else if (test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags))
5302 		param_value = ATH12K_HW_TXRX_RAW;
5303 	else
5304 		param_value = ATH12K_HW_TXRX_NATIVE_WIFI;
5305 
5306 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5307 					    param_id, param_value);
5308 	if (ret) {
5309 		ath12k_warn(ab, "failed to set vdev %d rx decap mode: %d\n",
5310 			    arvif->vdev_id, ret);
5311 		vif->offload_flags &= ~IEEE80211_OFFLOAD_DECAP_ENABLED;
5312 	}
5313 }
5314 
ath12k_mac_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5315 static int ath12k_mac_op_add_interface(struct ieee80211_hw *hw,
5316 				       struct ieee80211_vif *vif)
5317 {
5318 	struct ath12k *ar = hw->priv;
5319 	struct ath12k_base *ab = ar->ab;
5320 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
5321 	struct ath12k_wmi_vdev_create_arg vdev_arg = {0};
5322 	struct ath12k_wmi_peer_create_arg peer_param;
5323 	u32 param_id, param_value;
5324 	u16 nss;
5325 	int i;
5326 	int ret;
5327 	int bit;
5328 
5329 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
5330 
5331 	mutex_lock(&ar->conf_mutex);
5332 
5333 	if (vif->type == NL80211_IFTYPE_AP &&
5334 	    ar->num_peers > (ar->max_num_peers - 1)) {
5335 		ath12k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
5336 		ret = -ENOBUFS;
5337 		goto err;
5338 	}
5339 
5340 	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS - 1)) {
5341 		ath12k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
5342 			    TARGET_NUM_VDEVS);
5343 		ret = -EBUSY;
5344 		goto err;
5345 	}
5346 
5347 	memset(arvif, 0, sizeof(*arvif));
5348 
5349 	arvif->ar = ar;
5350 	arvif->vif = vif;
5351 
5352 	INIT_LIST_HEAD(&arvif->list);
5353 
5354 	/* Should we initialize any worker to handle connection loss indication
5355 	 * from firmware in sta mode?
5356 	 */
5357 
5358 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
5359 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
5360 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
5361 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
5362 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
5363 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
5364 	}
5365 
5366 	bit = __ffs64(ab->free_vdev_map);
5367 
5368 	arvif->vdev_id = bit;
5369 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
5370 
5371 	switch (vif->type) {
5372 	case NL80211_IFTYPE_UNSPECIFIED:
5373 	case NL80211_IFTYPE_STATION:
5374 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
5375 		break;
5376 	case NL80211_IFTYPE_MESH_POINT:
5377 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
5378 		fallthrough;
5379 	case NL80211_IFTYPE_AP:
5380 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
5381 		break;
5382 	case NL80211_IFTYPE_MONITOR:
5383 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
5384 		ar->monitor_vdev_id = bit;
5385 		break;
5386 	default:
5387 		WARN_ON(1);
5388 		break;
5389 	}
5390 
5391 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
5392 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
5393 		   ab->free_vdev_map);
5394 
5395 	vif->cab_queue = arvif->vdev_id % (ATH12K_HW_MAX_QUEUES - 1);
5396 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
5397 		vif->hw_queue[i] = i % (ATH12K_HW_MAX_QUEUES - 1);
5398 
5399 	ath12k_mac_setup_vdev_create_arg(arvif, &vdev_arg);
5400 
5401 	ret = ath12k_wmi_vdev_create(ar, vif->addr, &vdev_arg);
5402 	if (ret) {
5403 		ath12k_warn(ab, "failed to create WMI vdev %d: %d\n",
5404 			    arvif->vdev_id, ret);
5405 		goto err;
5406 	}
5407 
5408 	ar->num_created_vdevs++;
5409 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
5410 		   vif->addr, arvif->vdev_id);
5411 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
5412 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
5413 
5414 	spin_lock_bh(&ar->data_lock);
5415 	list_add(&arvif->list, &ar->arvifs);
5416 	spin_unlock_bh(&ar->data_lock);
5417 
5418 	ath12k_mac_op_update_vif_offload(hw, vif);
5419 
5420 	nss = hweight32(ar->cfg_tx_chainmask) ? : 1;
5421 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5422 					    WMI_VDEV_PARAM_NSS, nss);
5423 	if (ret) {
5424 		ath12k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
5425 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
5426 		goto err_vdev_del;
5427 	}
5428 
5429 	switch (arvif->vdev_type) {
5430 	case WMI_VDEV_TYPE_AP:
5431 		peer_param.vdev_id = arvif->vdev_id;
5432 		peer_param.peer_addr = vif->addr;
5433 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
5434 		ret = ath12k_peer_create(ar, arvif, NULL, &peer_param);
5435 		if (ret) {
5436 			ath12k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
5437 				    arvif->vdev_id, ret);
5438 			goto err_vdev_del;
5439 		}
5440 
5441 		ret = ath12k_mac_set_kickout(arvif);
5442 		if (ret) {
5443 			ath12k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
5444 				    arvif->vdev_id, ret);
5445 			goto err_peer_del;
5446 		}
5447 		break;
5448 	case WMI_VDEV_TYPE_STA:
5449 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5450 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5451 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5452 						  param_id, param_value);
5453 		if (ret) {
5454 			ath12k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
5455 				    arvif->vdev_id, ret);
5456 			goto err_peer_del;
5457 		}
5458 
5459 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
5460 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
5461 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5462 						  param_id, param_value);
5463 		if (ret) {
5464 			ath12k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
5465 				    arvif->vdev_id, ret);
5466 			goto err_peer_del;
5467 		}
5468 
5469 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
5470 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
5471 		ret = ath12k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5472 						  param_id, param_value);
5473 		if (ret) {
5474 			ath12k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
5475 				    arvif->vdev_id, ret);
5476 			goto err_peer_del;
5477 		}
5478 
5479 		ret = ath12k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
5480 		if (ret) {
5481 			ath12k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
5482 				    arvif->vdev_id, ret);
5483 			goto err_peer_del;
5484 		}
5485 		break;
5486 	default:
5487 		break;
5488 	}
5489 
5490 	arvif->txpower = vif->bss_conf.txpower;
5491 	ret = ath12k_mac_txpower_recalc(ar);
5492 	if (ret)
5493 		goto err_peer_del;
5494 
5495 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
5496 	param_value = ar->hw->wiphy->rts_threshold;
5497 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5498 					    param_id, param_value);
5499 	if (ret) {
5500 		ath12k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
5501 			    arvif->vdev_id, ret);
5502 	}
5503 
5504 	ath12k_dp_vdev_tx_attach(ar, arvif);
5505 
5506 	if (vif->type != NL80211_IFTYPE_MONITOR && ar->monitor_conf_enabled)
5507 		ath12k_mac_monitor_vdev_create(ar);
5508 
5509 	mutex_unlock(&ar->conf_mutex);
5510 
5511 	return ret;
5512 
5513 err_peer_del:
5514 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5515 		reinit_completion(&ar->peer_delete_done);
5516 
5517 		ret = ath12k_wmi_send_peer_delete_cmd(ar, vif->addr,
5518 						      arvif->vdev_id);
5519 		if (ret) {
5520 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
5521 				    arvif->vdev_id, vif->addr);
5522 			goto err;
5523 		}
5524 
5525 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5526 						       vif->addr);
5527 		if (ret)
5528 			goto err;
5529 
5530 		ar->num_peers--;
5531 	}
5532 
5533 err_vdev_del:
5534 	ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
5535 	ar->num_created_vdevs--;
5536 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
5537 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
5538 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
5539 	spin_lock_bh(&ar->data_lock);
5540 	list_del(&arvif->list);
5541 	spin_unlock_bh(&ar->data_lock);
5542 
5543 err:
5544 	mutex_unlock(&ar->conf_mutex);
5545 
5546 	return ret;
5547 }
5548 
ath12k_mac_vif_unref(struct ath12k_dp * dp,struct ieee80211_vif * vif)5549 static void ath12k_mac_vif_unref(struct ath12k_dp *dp, struct ieee80211_vif *vif)
5550 {
5551 	struct ath12k_tx_desc_info *tx_desc_info;
5552 	struct ath12k_skb_cb *skb_cb;
5553 	struct sk_buff *skb;
5554 	int i;
5555 
5556 	for (i = 0; i < ATH12K_HW_MAX_QUEUES; i++) {
5557 		spin_lock_bh(&dp->tx_desc_lock[i]);
5558 
5559 		list_for_each_entry(tx_desc_info, &dp->tx_desc_used_list[i],
5560 				    list) {
5561 			skb = tx_desc_info->skb;
5562 			if (!skb)
5563 				continue;
5564 
5565 			skb_cb = ATH12K_SKB_CB(skb);
5566 			if (skb_cb->vif == vif)
5567 				skb_cb->vif = NULL;
5568 		}
5569 
5570 		spin_unlock_bh(&dp->tx_desc_lock[i]);
5571 	}
5572 }
5573 
ath12k_mac_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5574 static void ath12k_mac_op_remove_interface(struct ieee80211_hw *hw,
5575 					   struct ieee80211_vif *vif)
5576 {
5577 	struct ath12k *ar = hw->priv;
5578 	struct ath12k_vif *arvif = ath12k_vif_to_arvif(vif);
5579 	struct ath12k_base *ab = ar->ab;
5580 	unsigned long time_left;
5581 	int ret;
5582 
5583 	mutex_lock(&ar->conf_mutex);
5584 
5585 	ath12k_dbg(ab, ATH12K_DBG_MAC, "mac remove interface (vdev %d)\n",
5586 		   arvif->vdev_id);
5587 
5588 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5589 		ret = ath12k_peer_delete(ar, arvif->vdev_id, vif->addr);
5590 		if (ret)
5591 			ath12k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
5592 				    arvif->vdev_id, ret);
5593 	}
5594 
5595 	reinit_completion(&ar->vdev_delete_done);
5596 
5597 	ret = ath12k_wmi_vdev_delete(ar, arvif->vdev_id);
5598 	if (ret) {
5599 		ath12k_warn(ab, "failed to delete WMI vdev %d: %d\n",
5600 			    arvif->vdev_id, ret);
5601 		goto err_vdev_del;
5602 	}
5603 
5604 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
5605 						ATH12K_VDEV_DELETE_TIMEOUT_HZ);
5606 	if (time_left == 0) {
5607 		ath12k_warn(ab, "Timeout in receiving vdev delete response\n");
5608 		goto err_vdev_del;
5609 	}
5610 
5611 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5612 		ar->monitor_vdev_id = -1;
5613 		ar->monitor_vdev_created = false;
5614 	} else if (ar->monitor_vdev_created && !ar->monitor_started) {
5615 		ret = ath12k_mac_monitor_vdev_delete(ar);
5616 	}
5617 
5618 	ab->free_vdev_map |= 1LL << (arvif->vdev_id);
5619 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
5620 	ab->free_vdev_stats_id_map &= ~(1LL << arvif->vdev_stats_id);
5621 	ar->num_created_vdevs--;
5622 
5623 	ath12k_dbg(ab, ATH12K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
5624 		   vif->addr, arvif->vdev_id);
5625 
5626 err_vdev_del:
5627 	spin_lock_bh(&ar->data_lock);
5628 	list_del(&arvif->list);
5629 	spin_unlock_bh(&ar->data_lock);
5630 
5631 	ath12k_peer_cleanup(ar, arvif->vdev_id);
5632 
5633 	idr_for_each(&ar->txmgmt_idr,
5634 		     ath12k_mac_vif_txmgmt_idr_remove, vif);
5635 
5636 	ath12k_mac_vif_unref(&ab->dp, vif);
5637 	ath12k_dp_tx_put_bank_profile(&ab->dp, arvif->bank_id);
5638 
5639 	/* Recalc txpower for remaining vdev */
5640 	ath12k_mac_txpower_recalc(ar);
5641 	clear_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5642 
5643 	/* TODO: recal traffic pause state based on the available vdevs */
5644 
5645 	mutex_unlock(&ar->conf_mutex);
5646 }
5647 
5648 /* FIXME: Has to be verified. */
5649 #define SUPPORTED_FILTERS			\
5650 	(FIF_ALLMULTI |				\
5651 	FIF_CONTROL |				\
5652 	FIF_PSPOLL |				\
5653 	FIF_OTHER_BSS |				\
5654 	FIF_BCN_PRBRESP_PROMISC |		\
5655 	FIF_PROBE_REQ |				\
5656 	FIF_FCSFAIL)
5657 
ath12k_mac_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)5658 static void ath12k_mac_op_configure_filter(struct ieee80211_hw *hw,
5659 					   unsigned int changed_flags,
5660 					   unsigned int *total_flags,
5661 					   u64 multicast)
5662 {
5663 	struct ath12k *ar = hw->priv;
5664 	bool reset_flag;
5665 	int ret;
5666 
5667 	mutex_lock(&ar->conf_mutex);
5668 
5669 	*total_flags &= SUPPORTED_FILTERS;
5670 	ar->filter_flags = *total_flags;
5671 
5672 	/* For monitor mode */
5673 	reset_flag = !(ar->filter_flags & FIF_BCN_PRBRESP_PROMISC);
5674 
5675 	ret = ath12k_dp_tx_htt_monitor_mode_ring_config(ar, reset_flag);
5676 	if (!ret) {
5677 		if (!reset_flag)
5678 			set_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5679 		else
5680 			clear_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
5681 	} else {
5682 		ath12k_warn(ar->ab,
5683 			    "fail to set monitor filter: %d\n", ret);
5684 	}
5685 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
5686 		   "total_flags:0x%x, reset_flag:%d\n",
5687 		   *total_flags, reset_flag);
5688 
5689 	mutex_unlock(&ar->conf_mutex);
5690 }
5691 
ath12k_mac_op_get_antenna(struct ieee80211_hw * hw,u32 * tx_ant,u32 * rx_ant)5692 static int ath12k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
5693 {
5694 	struct ath12k *ar = hw->priv;
5695 
5696 	mutex_lock(&ar->conf_mutex);
5697 
5698 	*tx_ant = ar->cfg_tx_chainmask;
5699 	*rx_ant = ar->cfg_rx_chainmask;
5700 
5701 	mutex_unlock(&ar->conf_mutex);
5702 
5703 	return 0;
5704 }
5705 
ath12k_mac_op_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)5706 static int ath12k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
5707 {
5708 	struct ath12k *ar = hw->priv;
5709 	int ret;
5710 
5711 	mutex_lock(&ar->conf_mutex);
5712 	ret = __ath12k_set_antenna(ar, tx_ant, rx_ant);
5713 	mutex_unlock(&ar->conf_mutex);
5714 
5715 	return ret;
5716 }
5717 
ath12k_mac_op_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)5718 static int ath12k_mac_op_ampdu_action(struct ieee80211_hw *hw,
5719 				      struct ieee80211_vif *vif,
5720 				      struct ieee80211_ampdu_params *params)
5721 {
5722 	struct ath12k *ar = hw->priv;
5723 	int ret = -EINVAL;
5724 
5725 	mutex_lock(&ar->conf_mutex);
5726 
5727 	switch (params->action) {
5728 	case IEEE80211_AMPDU_RX_START:
5729 		ret = ath12k_dp_rx_ampdu_start(ar, params);
5730 		break;
5731 	case IEEE80211_AMPDU_RX_STOP:
5732 		ret = ath12k_dp_rx_ampdu_stop(ar, params);
5733 		break;
5734 	case IEEE80211_AMPDU_TX_START:
5735 	case IEEE80211_AMPDU_TX_STOP_CONT:
5736 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
5737 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5738 	case IEEE80211_AMPDU_TX_OPERATIONAL:
5739 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
5740 		 * Tx aggregation requests.
5741 		 */
5742 		ret = -EOPNOTSUPP;
5743 		break;
5744 	}
5745 
5746 	mutex_unlock(&ar->conf_mutex);
5747 
5748 	return ret;
5749 }
5750 
ath12k_mac_op_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)5751 static int ath12k_mac_op_add_chanctx(struct ieee80211_hw *hw,
5752 				     struct ieee80211_chanctx_conf *ctx)
5753 {
5754 	struct ath12k *ar = hw->priv;
5755 	struct ath12k_base *ab = ar->ab;
5756 
5757 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5758 		   "mac chanctx add freq %u width %d ptr %pK\n",
5759 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
5760 
5761 	mutex_lock(&ar->conf_mutex);
5762 
5763 	spin_lock_bh(&ar->data_lock);
5764 	/* TODO: In case of multiple channel context, populate rx_channel from
5765 	 * Rx PPDU desc information.
5766 	 */
5767 	ar->rx_channel = ctx->def.chan;
5768 	spin_unlock_bh(&ar->data_lock);
5769 
5770 	mutex_unlock(&ar->conf_mutex);
5771 
5772 	return 0;
5773 }
5774 
ath12k_mac_op_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)5775 static void ath12k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
5776 					 struct ieee80211_chanctx_conf *ctx)
5777 {
5778 	struct ath12k *ar = hw->priv;
5779 	struct ath12k_base *ab = ar->ab;
5780 
5781 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5782 		   "mac chanctx remove freq %u width %d ptr %pK\n",
5783 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
5784 
5785 	mutex_lock(&ar->conf_mutex);
5786 
5787 	spin_lock_bh(&ar->data_lock);
5788 	/* TODO: In case of there is one more channel context left, populate
5789 	 * rx_channel with the channel of that remaining channel context.
5790 	 */
5791 	ar->rx_channel = NULL;
5792 	spin_unlock_bh(&ar->data_lock);
5793 
5794 	mutex_unlock(&ar->conf_mutex);
5795 }
5796 
5797 static int
ath12k_mac_vdev_start_restart(struct ath12k_vif * arvif,const struct cfg80211_chan_def * chandef,bool restart)5798 ath12k_mac_vdev_start_restart(struct ath12k_vif *arvif,
5799 			      const struct cfg80211_chan_def *chandef,
5800 			      bool restart)
5801 {
5802 	struct ath12k *ar = arvif->ar;
5803 	struct ath12k_base *ab = ar->ab;
5804 	struct wmi_vdev_start_req_arg arg = {};
5805 	int he_support = arvif->vif->bss_conf.he_support;
5806 	int ret;
5807 
5808 	lockdep_assert_held(&ar->conf_mutex);
5809 
5810 	reinit_completion(&ar->vdev_setup_done);
5811 
5812 	arg.vdev_id = arvif->vdev_id;
5813 	arg.dtim_period = arvif->dtim_period;
5814 	arg.bcn_intval = arvif->beacon_interval;
5815 	arg.punct_bitmap = ~arvif->punct_bitmap;
5816 
5817 	arg.freq = chandef->chan->center_freq;
5818 	arg.band_center_freq1 = chandef->center_freq1;
5819 	arg.band_center_freq2 = chandef->center_freq2;
5820 	arg.mode = ath12k_phymodes[chandef->chan->band][chandef->width];
5821 
5822 	arg.min_power = 0;
5823 	arg.max_power = chandef->chan->max_power * 2;
5824 	arg.max_reg_power = chandef->chan->max_reg_power * 2;
5825 	arg.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
5826 
5827 	arg.pref_tx_streams = ar->num_tx_chains;
5828 	arg.pref_rx_streams = ar->num_rx_chains;
5829 
5830 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5831 		arg.ssid = arvif->u.ap.ssid;
5832 		arg.ssid_len = arvif->u.ap.ssid_len;
5833 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
5834 
5835 		/* For now allow DFS for AP mode */
5836 		arg.chan_radar = !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
5837 
5838 		arg.passive = arg.chan_radar;
5839 
5840 		spin_lock_bh(&ab->base_lock);
5841 		arg.regdomain = ar->ab->dfs_region;
5842 		spin_unlock_bh(&ab->base_lock);
5843 
5844 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
5845 		if (he_support) {
5846 			ret = ath12k_set_he_mu_sounding_mode(ar, arvif);
5847 			if (ret) {
5848 				ath12k_warn(ar->ab, "failed to set he mode vdev %i\n",
5849 					    arg.vdev_id);
5850 				return ret;
5851 			}
5852 		}
5853 	}
5854 
5855 	arg.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
5856 
5857 	ath12k_dbg(ab, ATH12K_DBG_MAC,
5858 		   "mac vdev %d start center_freq %d phymode %s punct_bitmap 0x%x\n",
5859 		   arg.vdev_id, arg.freq,
5860 		   ath12k_mac_phymode_str(arg.mode), arg.punct_bitmap);
5861 
5862 	ret = ath12k_wmi_vdev_start(ar, &arg, restart);
5863 	if (ret) {
5864 		ath12k_warn(ar->ab, "failed to %s WMI vdev %i\n",
5865 			    restart ? "restart" : "start", arg.vdev_id);
5866 		return ret;
5867 	}
5868 
5869 	ret = ath12k_mac_vdev_setup_sync(ar);
5870 	if (ret) {
5871 		ath12k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
5872 			    arg.vdev_id, restart ? "restart" : "start", ret);
5873 		return ret;
5874 	}
5875 
5876 	ar->num_started_vdevs++;
5877 	ath12k_dbg(ab, ATH12K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
5878 		   arvif->vif->addr, arvif->vdev_id);
5879 
5880 	/* Enable CAC Flag in the driver by checking the channel DFS cac time,
5881 	 * i.e dfs_cac_ms value which will be valid only for radar channels
5882 	 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
5883 	 * done before channel usage. This flags is used to drop rx packets.
5884 	 * during CAC.
5885 	 */
5886 	/* TODO: Set the flag for other interface types as required */
5887 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
5888 	    chandef->chan->dfs_cac_ms &&
5889 	    chandef->chan->dfs_state == NL80211_DFS_USABLE) {
5890 		set_bit(ATH12K_CAC_RUNNING, &ar->dev_flags);
5891 		ath12k_dbg(ab, ATH12K_DBG_MAC,
5892 			   "CAC Started in chan_freq %d for vdev %d\n",
5893 			   arg.freq, arg.vdev_id);
5894 	}
5895 
5896 	ret = ath12k_mac_set_txbf_conf(arvif);
5897 	if (ret)
5898 		ath12k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
5899 			    arvif->vdev_id, ret);
5900 
5901 	return 0;
5902 }
5903 
ath12k_mac_vdev_stop(struct ath12k_vif * arvif)5904 static int ath12k_mac_vdev_stop(struct ath12k_vif *arvif)
5905 {
5906 	struct ath12k *ar = arvif->ar;
5907 	int ret;
5908 
5909 	lockdep_assert_held(&ar->conf_mutex);
5910 
5911 	reinit_completion(&ar->vdev_setup_done);
5912 
5913 	ret = ath12k_wmi_vdev_stop(ar, arvif->vdev_id);
5914 	if (ret) {
5915 		ath12k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
5916 			    arvif->vdev_id, ret);
5917 		goto err;
5918 	}
5919 
5920 	ret = ath12k_mac_vdev_setup_sync(ar);
5921 	if (ret) {
5922 		ath12k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
5923 			    arvif->vdev_id, ret);
5924 		goto err;
5925 	}
5926 
5927 	WARN_ON(ar->num_started_vdevs == 0);
5928 
5929 	ar->num_started_vdevs--;
5930 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
5931 		   arvif->vif->addr, arvif->vdev_id);
5932 
5933 	if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
5934 		clear_bit(ATH12K_CAC_RUNNING, &ar->dev_flags);
5935 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "CAC Stopped for vdev %d\n",
5936 			   arvif->vdev_id);
5937 	}
5938 
5939 	return 0;
5940 err:
5941 	return ret;
5942 }
5943 
ath12k_mac_vdev_start(struct ath12k_vif * arvif,const struct cfg80211_chan_def * chandef)5944 static int ath12k_mac_vdev_start(struct ath12k_vif *arvif,
5945 				 const struct cfg80211_chan_def *chandef)
5946 {
5947 	return ath12k_mac_vdev_start_restart(arvif, chandef, false);
5948 }
5949 
ath12k_mac_vdev_restart(struct ath12k_vif * arvif,const struct cfg80211_chan_def * chandef)5950 static int ath12k_mac_vdev_restart(struct ath12k_vif *arvif,
5951 				   const struct cfg80211_chan_def *chandef)
5952 {
5953 	return ath12k_mac_vdev_start_restart(arvif, chandef, true);
5954 }
5955 
5956 struct ath12k_mac_change_chanctx_arg {
5957 	struct ieee80211_chanctx_conf *ctx;
5958 	struct ieee80211_vif_chanctx_switch *vifs;
5959 	int n_vifs;
5960 	int next_vif;
5961 };
5962 
5963 static void
ath12k_mac_change_chanctx_cnt_iter(void * data,u8 * mac,struct ieee80211_vif * vif)5964 ath12k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
5965 				   struct ieee80211_vif *vif)
5966 {
5967 	struct ath12k_mac_change_chanctx_arg *arg = data;
5968 
5969 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) != arg->ctx)
5970 		return;
5971 
5972 	arg->n_vifs++;
5973 }
5974 
5975 static void
ath12k_mac_change_chanctx_fill_iter(void * data,u8 * mac,struct ieee80211_vif * vif)5976 ath12k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
5977 				    struct ieee80211_vif *vif)
5978 {
5979 	struct ath12k_mac_change_chanctx_arg *arg = data;
5980 	struct ieee80211_chanctx_conf *ctx;
5981 
5982 	ctx = rcu_access_pointer(vif->bss_conf.chanctx_conf);
5983 	if (ctx != arg->ctx)
5984 		return;
5985 
5986 	if (WARN_ON(arg->next_vif == arg->n_vifs))
5987 		return;
5988 
5989 	arg->vifs[arg->next_vif].vif = vif;
5990 	arg->vifs[arg->next_vif].old_ctx = ctx;
5991 	arg->vifs[arg->next_vif].new_ctx = ctx;
5992 	arg->next_vif++;
5993 }
5994 
5995 static void
ath12k_mac_update_vif_chan(struct ath12k * ar,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs)5996 ath12k_mac_update_vif_chan(struct ath12k *ar,
5997 			   struct ieee80211_vif_chanctx_switch *vifs,
5998 			   int n_vifs)
5999 {
6000 	struct ath12k_base *ab = ar->ab;
6001 	struct ath12k_vif *arvif;
6002 	int ret;
6003 	int i;
6004 	bool monitor_vif = false;
6005 
6006 	lockdep_assert_held(&ar->conf_mutex);
6007 
6008 	for (i = 0; i < n_vifs; i++) {
6009 		arvif = (void *)vifs[i].vif->drv_priv;
6010 
6011 		if (vifs[i].vif->type == NL80211_IFTYPE_MONITOR)
6012 			monitor_vif = true;
6013 
6014 		ath12k_dbg(ab, ATH12K_DBG_MAC,
6015 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
6016 			   arvif->vdev_id,
6017 			   vifs[i].old_ctx->def.chan->center_freq,
6018 			   vifs[i].new_ctx->def.chan->center_freq,
6019 			   vifs[i].old_ctx->def.width,
6020 			   vifs[i].new_ctx->def.width);
6021 
6022 		if (WARN_ON(!arvif->is_started))
6023 			continue;
6024 
6025 		if (WARN_ON(!arvif->is_up))
6026 			continue;
6027 
6028 		ret = ath12k_wmi_vdev_down(ar, arvif->vdev_id);
6029 		if (ret) {
6030 			ath12k_warn(ab, "failed to down vdev %d: %d\n",
6031 				    arvif->vdev_id, ret);
6032 			continue;
6033 		}
6034 	}
6035 
6036 	/* All relevant vdevs are downed and associated channel resources
6037 	 * should be available for the channel switch now.
6038 	 */
6039 
6040 	/* TODO: Update ar->rx_channel */
6041 
6042 	for (i = 0; i < n_vifs; i++) {
6043 		arvif = (void *)vifs[i].vif->drv_priv;
6044 
6045 		if (WARN_ON(!arvif->is_started))
6046 			continue;
6047 
6048 		/* Firmware expect vdev_restart only if vdev is up.
6049 		 * If vdev is down then it expect vdev_stop->vdev_start.
6050 		 */
6051 		if (arvif->is_up) {
6052 			ret = ath12k_mac_vdev_restart(arvif, &vifs[i].new_ctx->def);
6053 			if (ret) {
6054 				ath12k_warn(ab, "failed to restart vdev %d: %d\n",
6055 					    arvif->vdev_id, ret);
6056 				continue;
6057 			}
6058 		} else {
6059 			ret = ath12k_mac_vdev_stop(arvif);
6060 			if (ret) {
6061 				ath12k_warn(ab, "failed to stop vdev %d: %d\n",
6062 					    arvif->vdev_id, ret);
6063 				continue;
6064 			}
6065 
6066 			ret = ath12k_mac_vdev_start(arvif, &vifs[i].new_ctx->def);
6067 			if (ret)
6068 				ath12k_warn(ab, "failed to start vdev %d: %d\n",
6069 					    arvif->vdev_id, ret);
6070 			continue;
6071 		}
6072 
6073 		ret = ath12k_mac_setup_bcn_tmpl(arvif);
6074 		if (ret)
6075 			ath12k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
6076 				    ret);
6077 
6078 		ret = ath12k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
6079 					 arvif->bssid);
6080 		if (ret) {
6081 			ath12k_warn(ab, "failed to bring vdev up %d: %d\n",
6082 				    arvif->vdev_id, ret);
6083 			continue;
6084 		}
6085 	}
6086 
6087 	/* Restart the internal monitor vdev on new channel */
6088 	if (!monitor_vif && ar->monitor_vdev_created) {
6089 		if (!ath12k_mac_monitor_stop(ar))
6090 			ath12k_mac_monitor_start(ar);
6091 	}
6092 }
6093 
6094 static void
ath12k_mac_update_active_vif_chan(struct ath12k * ar,struct ieee80211_chanctx_conf * ctx)6095 ath12k_mac_update_active_vif_chan(struct ath12k *ar,
6096 				  struct ieee80211_chanctx_conf *ctx)
6097 {
6098 	struct ath12k_mac_change_chanctx_arg arg = { .ctx = ctx };
6099 
6100 	lockdep_assert_held(&ar->conf_mutex);
6101 
6102 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
6103 						   IEEE80211_IFACE_ITER_NORMAL,
6104 						   ath12k_mac_change_chanctx_cnt_iter,
6105 						   &arg);
6106 	if (arg.n_vifs == 0)
6107 		return;
6108 
6109 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
6110 	if (!arg.vifs)
6111 		return;
6112 
6113 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
6114 						   IEEE80211_IFACE_ITER_NORMAL,
6115 						   ath12k_mac_change_chanctx_fill_iter,
6116 						   &arg);
6117 
6118 	ath12k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
6119 
6120 	kfree(arg.vifs);
6121 }
6122 
ath12k_mac_op_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)6123 static void ath12k_mac_op_change_chanctx(struct ieee80211_hw *hw,
6124 					 struct ieee80211_chanctx_conf *ctx,
6125 					 u32 changed)
6126 {
6127 	struct ath12k *ar = hw->priv;
6128 	struct ath12k_base *ab = ar->ab;
6129 
6130 	mutex_lock(&ar->conf_mutex);
6131 
6132 	ath12k_dbg(ab, ATH12K_DBG_MAC,
6133 		   "mac chanctx change freq %u width %d ptr %pK changed %x\n",
6134 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
6135 
6136 	/* This shouldn't really happen because channel switching should use
6137 	 * switch_vif_chanctx().
6138 	 */
6139 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
6140 		goto unlock;
6141 
6142 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH)
6143 		ath12k_mac_update_active_vif_chan(ar, ctx);
6144 
6145 	/* TODO: Recalc radar detection */
6146 
6147 unlock:
6148 	mutex_unlock(&ar->conf_mutex);
6149 }
6150 
ath12k_start_vdev_delay(struct ieee80211_hw * hw,struct ieee80211_vif * vif)6151 static int ath12k_start_vdev_delay(struct ieee80211_hw *hw,
6152 				   struct ieee80211_vif *vif)
6153 {
6154 	struct ath12k *ar = hw->priv;
6155 	struct ath12k_base *ab = ar->ab;
6156 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
6157 	int ret;
6158 
6159 	if (WARN_ON(arvif->is_started))
6160 		return -EBUSY;
6161 
6162 	ret = ath12k_mac_vdev_start(arvif, &arvif->chanctx.def);
6163 	if (ret) {
6164 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
6165 			    arvif->vdev_id, vif->addr,
6166 			    arvif->chanctx.def.chan->center_freq, ret);
6167 		return ret;
6168 	}
6169 
6170 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6171 		ret = ath12k_monitor_vdev_up(ar, arvif->vdev_id);
6172 		if (ret) {
6173 			ath12k_warn(ab, "failed put monitor up: %d\n", ret);
6174 			return ret;
6175 		}
6176 	}
6177 
6178 	arvif->is_started = true;
6179 
6180 	/* TODO: Setup ps and cts/rts protection */
6181 	return 0;
6182 }
6183 
6184 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)6185 ath12k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
6186 				 struct ieee80211_vif *vif,
6187 				 struct ieee80211_bss_conf *link_conf,
6188 				 struct ieee80211_chanctx_conf *ctx)
6189 {
6190 	struct ath12k *ar = hw->priv;
6191 	struct ath12k_base *ab = ar->ab;
6192 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
6193 	int ret;
6194 	struct ath12k_wmi_peer_create_arg param;
6195 
6196 	mutex_lock(&ar->conf_mutex);
6197 
6198 	ath12k_dbg(ab, ATH12K_DBG_MAC,
6199 		   "mac chanctx assign ptr %pK vdev_id %i\n",
6200 		   ctx, arvif->vdev_id);
6201 
6202 	arvif->punct_bitmap = link_conf->eht_puncturing;
6203 
6204 	/* for some targets bss peer must be created before vdev_start */
6205 	if (ab->hw_params->vdev_start_delay &&
6206 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6207 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
6208 	    !ath12k_peer_exist_by_vdev_id(ab, arvif->vdev_id)) {
6209 		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
6210 		ret = 0;
6211 		goto out;
6212 	}
6213 
6214 	if (WARN_ON(arvif->is_started)) {
6215 		ret = -EBUSY;
6216 		goto out;
6217 	}
6218 
6219 	if (ab->hw_params->vdev_start_delay &&
6220 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6221 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
6222 		param.vdev_id = arvif->vdev_id;
6223 		param.peer_type = WMI_PEER_TYPE_DEFAULT;
6224 		param.peer_addr = ar->mac_addr;
6225 
6226 		ret = ath12k_peer_create(ar, arvif, NULL, &param);
6227 		if (ret) {
6228 			ath12k_warn(ab, "failed to create peer after vdev start delay: %d",
6229 				    ret);
6230 			goto out;
6231 		}
6232 	}
6233 
6234 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6235 		ret = ath12k_mac_monitor_start(ar);
6236 		if (ret)
6237 			goto out;
6238 		arvif->is_started = true;
6239 		goto out;
6240 	}
6241 
6242 	ret = ath12k_mac_vdev_start(arvif, &ctx->def);
6243 	if (ret) {
6244 		ath12k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
6245 			    arvif->vdev_id, vif->addr,
6246 			    ctx->def.chan->center_freq, ret);
6247 		goto out;
6248 	}
6249 
6250 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR && ar->monitor_vdev_created)
6251 		ath12k_mac_monitor_start(ar);
6252 
6253 	arvif->is_started = true;
6254 
6255 	/* TODO: Setup ps and cts/rts protection */
6256 
6257 out:
6258 	mutex_unlock(&ar->conf_mutex);
6259 
6260 	return ret;
6261 }
6262 
6263 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)6264 ath12k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
6265 				   struct ieee80211_vif *vif,
6266 				   struct ieee80211_bss_conf *link_conf,
6267 				   struct ieee80211_chanctx_conf *ctx)
6268 {
6269 	struct ath12k *ar = hw->priv;
6270 	struct ath12k_base *ab = ar->ab;
6271 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
6272 	int ret;
6273 
6274 	mutex_lock(&ar->conf_mutex);
6275 
6276 	ath12k_dbg(ab, ATH12K_DBG_MAC,
6277 		   "mac chanctx unassign ptr %pK vdev_id %i\n",
6278 		   ctx, arvif->vdev_id);
6279 
6280 	WARN_ON(!arvif->is_started);
6281 
6282 	if (ab->hw_params->vdev_start_delay &&
6283 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR &&
6284 	    ath12k_peer_find_by_addr(ab, ar->mac_addr))
6285 		ath12k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
6286 
6287 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
6288 		ret = ath12k_mac_monitor_stop(ar);
6289 		if (ret) {
6290 			mutex_unlock(&ar->conf_mutex);
6291 			return;
6292 		}
6293 
6294 		arvif->is_started = false;
6295 	}
6296 
6297 	ret = ath12k_mac_vdev_stop(arvif);
6298 	if (ret)
6299 		ath12k_warn(ab, "failed to stop vdev %i: %d\n",
6300 			    arvif->vdev_id, ret);
6301 
6302 	arvif->is_started = false;
6303 
6304 	if (ab->hw_params->vdev_start_delay &&
6305 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
6306 		ath12k_wmi_vdev_down(ar, arvif->vdev_id);
6307 
6308 	if (arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
6309 	    ar->num_started_vdevs == 1 && ar->monitor_vdev_created)
6310 		ath12k_mac_monitor_stop(ar);
6311 
6312 	mutex_unlock(&ar->conf_mutex);
6313 }
6314 
6315 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)6316 ath12k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
6317 				 struct ieee80211_vif_chanctx_switch *vifs,
6318 				 int n_vifs,
6319 				 enum ieee80211_chanctx_switch_mode mode)
6320 {
6321 	struct ath12k *ar = hw->priv;
6322 
6323 	mutex_lock(&ar->conf_mutex);
6324 
6325 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6326 		   "mac chanctx switch n_vifs %d mode %d\n",
6327 		   n_vifs, mode);
6328 	ath12k_mac_update_vif_chan(ar, vifs, n_vifs);
6329 
6330 	mutex_unlock(&ar->conf_mutex);
6331 
6332 	return 0;
6333 }
6334 
6335 static int
ath12k_set_vdev_param_to_all_vifs(struct ath12k * ar,int param,u32 value)6336 ath12k_set_vdev_param_to_all_vifs(struct ath12k *ar, int param, u32 value)
6337 {
6338 	struct ath12k_vif *arvif;
6339 	int ret = 0;
6340 
6341 	mutex_lock(&ar->conf_mutex);
6342 	list_for_each_entry(arvif, &ar->arvifs, list) {
6343 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
6344 			   param, arvif->vdev_id, value);
6345 
6346 		ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6347 						    param, value);
6348 		if (ret) {
6349 			ath12k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
6350 				    param, arvif->vdev_id, ret);
6351 			break;
6352 		}
6353 	}
6354 	mutex_unlock(&ar->conf_mutex);
6355 	return ret;
6356 }
6357 
6358 /* mac80211 stores device specific RTS/Fragmentation threshold value,
6359  * this is set interface specific to firmware from ath12k driver
6360  */
ath12k_mac_op_set_rts_threshold(struct ieee80211_hw * hw,u32 value)6361 static int ath12k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
6362 {
6363 	struct ath12k *ar = hw->priv;
6364 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
6365 
6366 	return ath12k_set_vdev_param_to_all_vifs(ar, param_id, value);
6367 }
6368 
ath12k_mac_op_set_frag_threshold(struct ieee80211_hw * hw,u32 value)6369 static int ath12k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
6370 {
6371 	/* Even though there's a WMI vdev param for fragmentation threshold no
6372 	 * known firmware actually implements it. Moreover it is not possible to
6373 	 * rely frame fragmentation to mac80211 because firmware clears the
6374 	 * "more fragments" bit in frame control making it impossible for remote
6375 	 * devices to reassemble frames.
6376 	 *
6377 	 * Hence implement a dummy callback just to say fragmentation isn't
6378 	 * supported. This effectively prevents mac80211 from doing frame
6379 	 * fragmentation in software.
6380 	 */
6381 	return -EOPNOTSUPP;
6382 }
6383 
ath12k_mac_op_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)6384 static void ath12k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6385 				u32 queues, bool drop)
6386 {
6387 	struct ath12k *ar = hw->priv;
6388 	long time_left;
6389 
6390 	if (drop)
6391 		return;
6392 
6393 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
6394 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
6395 				       ATH12K_FLUSH_TIMEOUT);
6396 	if (time_left == 0)
6397 		ath12k_warn(ar->ab, "failed to flush transmit queue %ld\n", time_left);
6398 
6399 	time_left = wait_event_timeout(ar->txmgmt_empty_waitq,
6400 				       (atomic_read(&ar->num_pending_mgmt_tx) == 0),
6401 				       ATH12K_FLUSH_TIMEOUT);
6402 	if (time_left == 0)
6403 		ath12k_warn(ar->ab, "failed to flush mgmt transmit queue %ld\n",
6404 			    time_left);
6405 }
6406 
6407 static int
ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6408 ath12k_mac_bitrate_mask_num_ht_rates(struct ath12k *ar,
6409 				     enum nl80211_band band,
6410 				     const struct cfg80211_bitrate_mask *mask)
6411 {
6412 	int num_rates = 0;
6413 	int i;
6414 
6415 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6416 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
6417 
6418 	return num_rates;
6419 }
6420 
6421 static bool
ath12k_mac_has_single_legacy_rate(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6422 ath12k_mac_has_single_legacy_rate(struct ath12k *ar,
6423 				  enum nl80211_band band,
6424 				  const struct cfg80211_bitrate_mask *mask)
6425 {
6426 	int num_rates = 0;
6427 
6428 	num_rates = hweight32(mask->control[band].legacy);
6429 
6430 	if (ath12k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
6431 		return false;
6432 
6433 	if (ath12k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
6434 		return false;
6435 
6436 	return num_rates == 1;
6437 }
6438 
6439 static bool
ath12k_mac_bitrate_mask_get_single_nss(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask,int * nss)6440 ath12k_mac_bitrate_mask_get_single_nss(struct ath12k *ar,
6441 				       enum nl80211_band band,
6442 				       const struct cfg80211_bitrate_mask *mask,
6443 				       int *nss)
6444 {
6445 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6446 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
6447 	u8 ht_nss_mask = 0;
6448 	u8 vht_nss_mask = 0;
6449 	int i;
6450 
6451 	/* No need to consider legacy here. Basic rates are always present
6452 	 * in bitrate mask
6453 	 */
6454 
6455 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6456 		if (mask->control[band].ht_mcs[i] == 0)
6457 			continue;
6458 		else if (mask->control[band].ht_mcs[i] ==
6459 			 sband->ht_cap.mcs.rx_mask[i])
6460 			ht_nss_mask |= BIT(i);
6461 		else
6462 			return false;
6463 	}
6464 
6465 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6466 		if (mask->control[band].vht_mcs[i] == 0)
6467 			continue;
6468 		else if (mask->control[band].vht_mcs[i] ==
6469 			 ath12k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
6470 			vht_nss_mask |= BIT(i);
6471 		else
6472 			return false;
6473 	}
6474 
6475 	if (ht_nss_mask != vht_nss_mask)
6476 		return false;
6477 
6478 	if (ht_nss_mask == 0)
6479 		return false;
6480 
6481 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
6482 		return false;
6483 
6484 	*nss = fls(ht_nss_mask);
6485 
6486 	return true;
6487 }
6488 
6489 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)6490 ath12k_mac_get_single_legacy_rate(struct ath12k *ar,
6491 				  enum nl80211_band band,
6492 				  const struct cfg80211_bitrate_mask *mask,
6493 				  u32 *rate, u8 *nss)
6494 {
6495 	int rate_idx;
6496 	u16 bitrate;
6497 	u8 preamble;
6498 	u8 hw_rate;
6499 
6500 	if (hweight32(mask->control[band].legacy) != 1)
6501 		return -EINVAL;
6502 
6503 	rate_idx = ffs(mask->control[band].legacy) - 1;
6504 
6505 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
6506 		rate_idx += ATH12K_MAC_FIRST_OFDM_RATE_IDX;
6507 
6508 	hw_rate = ath12k_legacy_rates[rate_idx].hw_value;
6509 	bitrate = ath12k_legacy_rates[rate_idx].bitrate;
6510 
6511 	if (ath12k_mac_bitrate_is_cck(bitrate))
6512 		preamble = WMI_RATE_PREAMBLE_CCK;
6513 	else
6514 		preamble = WMI_RATE_PREAMBLE_OFDM;
6515 
6516 	*nss = 1;
6517 	*rate = ATH12K_HW_RATE_CODE(hw_rate, 0, preamble);
6518 
6519 	return 0;
6520 }
6521 
ath12k_mac_set_fixed_rate_params(struct ath12k_vif * arvif,u32 rate,u8 nss,u8 sgi,u8 ldpc)6522 static int ath12k_mac_set_fixed_rate_params(struct ath12k_vif *arvif,
6523 					    u32 rate, u8 nss, u8 sgi, u8 ldpc)
6524 {
6525 	struct ath12k *ar = arvif->ar;
6526 	u32 vdev_param;
6527 	int ret;
6528 
6529 	lockdep_assert_held(&ar->conf_mutex);
6530 
6531 	ath12k_dbg(ar->ab, ATH12K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02x nss %u sgi %u\n",
6532 		   arvif->vdev_id, rate, nss, sgi);
6533 
6534 	vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
6535 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6536 					    vdev_param, rate);
6537 	if (ret) {
6538 		ath12k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
6539 			    rate, ret);
6540 		return ret;
6541 	}
6542 
6543 	vdev_param = WMI_VDEV_PARAM_NSS;
6544 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6545 					    vdev_param, nss);
6546 	if (ret) {
6547 		ath12k_warn(ar->ab, "failed to set nss param %d: %d\n",
6548 			    nss, ret);
6549 		return ret;
6550 	}
6551 
6552 	vdev_param = WMI_VDEV_PARAM_SGI;
6553 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6554 					    vdev_param, sgi);
6555 	if (ret) {
6556 		ath12k_warn(ar->ab, "failed to set sgi param %d: %d\n",
6557 			    sgi, ret);
6558 		return ret;
6559 	}
6560 
6561 	vdev_param = WMI_VDEV_PARAM_LDPC;
6562 	ret = ath12k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
6563 					    vdev_param, ldpc);
6564 	if (ret) {
6565 		ath12k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
6566 			    ldpc, ret);
6567 		return ret;
6568 	}
6569 
6570 	return 0;
6571 }
6572 
6573 static bool
ath12k_mac_vht_mcs_range_present(struct ath12k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)6574 ath12k_mac_vht_mcs_range_present(struct ath12k *ar,
6575 				 enum nl80211_band band,
6576 				 const struct cfg80211_bitrate_mask *mask)
6577 {
6578 	int i;
6579 	u16 vht_mcs;
6580 
6581 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
6582 		vht_mcs = mask->control[band].vht_mcs[i];
6583 
6584 		switch (vht_mcs) {
6585 		case 0:
6586 		case BIT(8) - 1:
6587 		case BIT(9) - 1:
6588 		case BIT(10) - 1:
6589 			break;
6590 		default:
6591 			return false;
6592 		}
6593 	}
6594 
6595 	return true;
6596 }
6597 
ath12k_mac_set_bitrate_mask_iter(void * data,struct ieee80211_sta * sta)6598 static void ath12k_mac_set_bitrate_mask_iter(void *data,
6599 					     struct ieee80211_sta *sta)
6600 {
6601 	struct ath12k_vif *arvif = data;
6602 	struct ath12k_sta *arsta = (struct ath12k_sta *)sta->drv_priv;
6603 	struct ath12k *ar = arvif->ar;
6604 
6605 	spin_lock_bh(&ar->data_lock);
6606 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
6607 	spin_unlock_bh(&ar->data_lock);
6608 
6609 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
6610 }
6611 
ath12k_mac_disable_peer_fixed_rate(void * data,struct ieee80211_sta * sta)6612 static void ath12k_mac_disable_peer_fixed_rate(void *data,
6613 					       struct ieee80211_sta *sta)
6614 {
6615 	struct ath12k_vif *arvif = data;
6616 	struct ath12k *ar = arvif->ar;
6617 	int ret;
6618 
6619 	ret = ath12k_wmi_set_peer_param(ar, sta->addr,
6620 					arvif->vdev_id,
6621 					WMI_PEER_PARAM_FIXED_RATE,
6622 					WMI_FIXED_RATE_NONE);
6623 	if (ret)
6624 		ath12k_warn(ar->ab,
6625 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
6626 			    sta->addr, ret);
6627 }
6628 
6629 static int
ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)6630 ath12k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
6631 			       struct ieee80211_vif *vif,
6632 			       const struct cfg80211_bitrate_mask *mask)
6633 {
6634 	struct ath12k_vif *arvif = (void *)vif->drv_priv;
6635 	struct cfg80211_chan_def def;
6636 	struct ath12k *ar = arvif->ar;
6637 	enum nl80211_band band;
6638 	const u8 *ht_mcs_mask;
6639 	const u16 *vht_mcs_mask;
6640 	u32 rate;
6641 	u8 nss;
6642 	u8 sgi;
6643 	u8 ldpc;
6644 	int single_nss;
6645 	int ret;
6646 	int num_rates;
6647 
6648 	if (ath12k_mac_vif_chan(vif, &def))
6649 		return -EPERM;
6650 
6651 	band = def.chan->band;
6652 	ht_mcs_mask = mask->control[band].ht_mcs;
6653 	vht_mcs_mask = mask->control[band].vht_mcs;
6654 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
6655 
6656 	sgi = mask->control[band].gi;
6657 	if (sgi == NL80211_TXRATE_FORCE_LGI)
6658 		return -EINVAL;
6659 
6660 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
6661 	 * requires passing at least one of used basic rates along with them.
6662 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
6663 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
6664 	 * suitable for setting single HT/VHT rates.
6665 	 * But, there could be a single basic rate passed from userspace which
6666 	 * can be done through the FIXED_RATE param.
6667 	 */
6668 	if (ath12k_mac_has_single_legacy_rate(ar, band, mask)) {
6669 		ret = ath12k_mac_get_single_legacy_rate(ar, band, mask, &rate,
6670 							&nss);
6671 		if (ret) {
6672 			ath12k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
6673 				    arvif->vdev_id, ret);
6674 			return ret;
6675 		}
6676 		ieee80211_iterate_stations_atomic(ar->hw,
6677 						  ath12k_mac_disable_peer_fixed_rate,
6678 						  arvif);
6679 	} else if (ath12k_mac_bitrate_mask_get_single_nss(ar, band, mask,
6680 							  &single_nss)) {
6681 		rate = WMI_FIXED_RATE_NONE;
6682 		nss = single_nss;
6683 	} else {
6684 		rate = WMI_FIXED_RATE_NONE;
6685 		nss = min_t(u32, ar->num_tx_chains,
6686 			    max(ath12k_mac_max_ht_nss(ht_mcs_mask),
6687 				ath12k_mac_max_vht_nss(vht_mcs_mask)));
6688 
6689 		/* If multiple rates across different preambles are given
6690 		 * we can reconfigure this info with all peers using PEER_ASSOC
6691 		 * command with the below exception cases.
6692 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
6693 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
6694 		 * mandates passing basic rates along with HT/VHT rates, FW
6695 		 * doesn't allow switching from VHT to Legacy. Hence instead of
6696 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
6697 		 * we could set this VHT rate as peer fixed rate param, which
6698 		 * will override FIXED rate and FW rate control algorithm.
6699 		 * If single VHT rate is passed along with HT rates, we select
6700 		 * the VHT rate as fixed rate for vht peers.
6701 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
6702 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
6703 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
6704 		 * RATEMASK_CMDID can cover all use cases of setting rates
6705 		 * across multiple preambles and rates within same type.
6706 		 * But requires more validation of the command at this point.
6707 		 */
6708 
6709 		num_rates = ath12k_mac_bitrate_mask_num_vht_rates(ar, band,
6710 								  mask);
6711 
6712 		if (!ath12k_mac_vht_mcs_range_present(ar, band, mask) &&
6713 		    num_rates > 1) {
6714 			/* TODO: Handle multiple VHT MCS values setting using
6715 			 * RATEMASK CMD
6716 			 */
6717 			ath12k_warn(ar->ab,
6718 				    "Setting more than one MCS Value in bitrate mask not supported\n");
6719 			return -EINVAL;
6720 		}
6721 
6722 		ieee80211_iterate_stations_atomic(ar->hw,
6723 						  ath12k_mac_disable_peer_fixed_rate,
6724 						  arvif);
6725 
6726 		mutex_lock(&ar->conf_mutex);
6727 
6728 		arvif->bitrate_mask = *mask;
6729 		ieee80211_iterate_stations_atomic(ar->hw,
6730 						  ath12k_mac_set_bitrate_mask_iter,
6731 						  arvif);
6732 
6733 		mutex_unlock(&ar->conf_mutex);
6734 	}
6735 
6736 	mutex_lock(&ar->conf_mutex);
6737 
6738 	ret = ath12k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
6739 	if (ret) {
6740 		ath12k_warn(ar->ab, "failed to set fixed rate params on vdev %i: %d\n",
6741 			    arvif->vdev_id, ret);
6742 	}
6743 
6744 	mutex_unlock(&ar->conf_mutex);
6745 
6746 	return ret;
6747 }
6748 
6749 static void
ath12k_mac_op_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)6750 ath12k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
6751 				enum ieee80211_reconfig_type reconfig_type)
6752 {
6753 	struct ath12k *ar = hw->priv;
6754 	struct ath12k_base *ab = ar->ab;
6755 	struct ath12k_vif *arvif;
6756 	int recovery_count;
6757 
6758 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
6759 		return;
6760 
6761 	mutex_lock(&ar->conf_mutex);
6762 
6763 	if (ar->state == ATH12K_STATE_RESTARTED) {
6764 		ath12k_warn(ar->ab, "pdev %d successfully recovered\n",
6765 			    ar->pdev->pdev_id);
6766 		ar->state = ATH12K_STATE_ON;
6767 		ieee80211_wake_queues(ar->hw);
6768 
6769 		if (ab->is_reset) {
6770 			recovery_count = atomic_inc_return(&ab->recovery_count);
6771 			ath12k_dbg(ab, ATH12K_DBG_BOOT, "recovery count %d\n",
6772 				   recovery_count);
6773 			/* When there are multiple radios in an SOC,
6774 			 * the recovery has to be done for each radio
6775 			 */
6776 			if (recovery_count == ab->num_radios) {
6777 				atomic_dec(&ab->reset_count);
6778 				complete(&ab->reset_complete);
6779 				ab->is_reset = false;
6780 				atomic_set(&ab->fail_cont_count, 0);
6781 				ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset success\n");
6782 			}
6783 		}
6784 
6785 		list_for_each_entry(arvif, &ar->arvifs, list) {
6786 			ath12k_dbg(ab, ATH12K_DBG_BOOT,
6787 				   "reconfig cipher %d up %d vdev type %d\n",
6788 				   arvif->key_cipher,
6789 				   arvif->is_up,
6790 				   arvif->vdev_type);
6791 			/* After trigger disconnect, then upper layer will
6792 			 * trigger connect again, then the PN number of
6793 			 * upper layer will be reset to keep up with AP
6794 			 * side, hence PN number mismatch will not happen.
6795 			 */
6796 			if (arvif->is_up &&
6797 			    arvif->vdev_type == WMI_VDEV_TYPE_STA &&
6798 			    arvif->vdev_subtype == WMI_VDEV_SUBTYPE_NONE) {
6799 				ieee80211_hw_restart_disconnect(arvif->vif);
6800 				ath12k_dbg(ab, ATH12K_DBG_BOOT,
6801 					   "restart disconnect\n");
6802 			}
6803 		}
6804 	}
6805 
6806 	mutex_unlock(&ar->conf_mutex);
6807 }
6808 
6809 static void
ath12k_mac_update_bss_chan_survey(struct ath12k * ar,struct ieee80211_channel * channel)6810 ath12k_mac_update_bss_chan_survey(struct ath12k *ar,
6811 				  struct ieee80211_channel *channel)
6812 {
6813 	int ret;
6814 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
6815 
6816 	lockdep_assert_held(&ar->conf_mutex);
6817 
6818 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
6819 	    ar->rx_channel != channel)
6820 		return;
6821 
6822 	if (ar->scan.state != ATH12K_SCAN_IDLE) {
6823 		ath12k_dbg(ar->ab, ATH12K_DBG_MAC,
6824 			   "ignoring bss chan info req while scanning..\n");
6825 		return;
6826 	}
6827 
6828 	reinit_completion(&ar->bss_survey_done);
6829 
6830 	ret = ath12k_wmi_pdev_bss_chan_info_request(ar, type);
6831 	if (ret) {
6832 		ath12k_warn(ar->ab, "failed to send pdev bss chan info request\n");
6833 		return;
6834 	}
6835 
6836 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
6837 	if (ret == 0)
6838 		ath12k_warn(ar->ab, "bss channel survey timed out\n");
6839 }
6840 
ath12k_mac_op_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)6841 static int ath12k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
6842 				    struct survey_info *survey)
6843 {
6844 	struct ath12k *ar = hw->priv;
6845 	struct ieee80211_supported_band *sband;
6846 	struct survey_info *ar_survey;
6847 	int ret = 0;
6848 
6849 	if (idx >= ATH12K_NUM_CHANS)
6850 		return -ENOENT;
6851 
6852 	ar_survey = &ar->survey[idx];
6853 
6854 	mutex_lock(&ar->conf_mutex);
6855 
6856 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
6857 	if (sband && idx >= sband->n_channels) {
6858 		idx -= sband->n_channels;
6859 		sband = NULL;
6860 	}
6861 
6862 	if (!sband)
6863 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
6864 
6865 	if (!sband || idx >= sband->n_channels) {
6866 		ret = -ENOENT;
6867 		goto exit;
6868 	}
6869 
6870 	ath12k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
6871 
6872 	spin_lock_bh(&ar->data_lock);
6873 	memcpy(survey, ar_survey, sizeof(*survey));
6874 	spin_unlock_bh(&ar->data_lock);
6875 
6876 	survey->channel = &sband->channels[idx];
6877 
6878 	if (ar->rx_channel == survey->channel)
6879 		survey->filled |= SURVEY_INFO_IN_USE;
6880 
6881 exit:
6882 	mutex_unlock(&ar->conf_mutex);
6883 	return ret;
6884 }
6885 
ath12k_mac_op_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)6886 static void ath12k_mac_op_sta_statistics(struct ieee80211_hw *hw,
6887 					 struct ieee80211_vif *vif,
6888 					 struct ieee80211_sta *sta,
6889 					 struct station_info *sinfo)
6890 {
6891 	struct ath12k_sta *arsta = (struct ath12k_sta *)sta->drv_priv;
6892 
6893 	sinfo->rx_duration = arsta->rx_duration;
6894 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
6895 
6896 	sinfo->tx_duration = arsta->tx_duration;
6897 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
6898 
6899 	if (!arsta->txrate.legacy && !arsta->txrate.nss)
6900 		return;
6901 
6902 	if (arsta->txrate.legacy) {
6903 		sinfo->txrate.legacy = arsta->txrate.legacy;
6904 	} else {
6905 		sinfo->txrate.mcs = arsta->txrate.mcs;
6906 		sinfo->txrate.nss = arsta->txrate.nss;
6907 		sinfo->txrate.bw = arsta->txrate.bw;
6908 		sinfo->txrate.he_gi = arsta->txrate.he_gi;
6909 		sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
6910 		sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
6911 	}
6912 	sinfo->txrate.flags = arsta->txrate.flags;
6913 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6914 
6915 	/* TODO: Use real NF instead of default one. */
6916 	sinfo->signal = arsta->rssi_comb + ATH12K_DEFAULT_NOISE_FLOOR;
6917 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
6918 }
6919 
6920 static const struct ieee80211_ops ath12k_ops = {
6921 	.tx				= ath12k_mac_op_tx,
6922 	.wake_tx_queue			= ieee80211_handle_wake_tx_queue,
6923 	.start                          = ath12k_mac_op_start,
6924 	.stop                           = ath12k_mac_op_stop,
6925 	.reconfig_complete              = ath12k_mac_op_reconfig_complete,
6926 	.add_interface                  = ath12k_mac_op_add_interface,
6927 	.remove_interface		= ath12k_mac_op_remove_interface,
6928 	.update_vif_offload		= ath12k_mac_op_update_vif_offload,
6929 	.config                         = ath12k_mac_op_config,
6930 	.bss_info_changed               = ath12k_mac_op_bss_info_changed,
6931 	.configure_filter		= ath12k_mac_op_configure_filter,
6932 	.hw_scan                        = ath12k_mac_op_hw_scan,
6933 	.cancel_hw_scan                 = ath12k_mac_op_cancel_hw_scan,
6934 	.set_key                        = ath12k_mac_op_set_key,
6935 	.sta_state                      = ath12k_mac_op_sta_state,
6936 	.sta_set_txpwr			= ath12k_mac_op_sta_set_txpwr,
6937 	.sta_rc_update			= ath12k_mac_op_sta_rc_update,
6938 	.conf_tx                        = ath12k_mac_op_conf_tx,
6939 	.set_antenna			= ath12k_mac_op_set_antenna,
6940 	.get_antenna			= ath12k_mac_op_get_antenna,
6941 	.ampdu_action			= ath12k_mac_op_ampdu_action,
6942 	.add_chanctx			= ath12k_mac_op_add_chanctx,
6943 	.remove_chanctx			= ath12k_mac_op_remove_chanctx,
6944 	.change_chanctx			= ath12k_mac_op_change_chanctx,
6945 	.assign_vif_chanctx		= ath12k_mac_op_assign_vif_chanctx,
6946 	.unassign_vif_chanctx		= ath12k_mac_op_unassign_vif_chanctx,
6947 	.switch_vif_chanctx		= ath12k_mac_op_switch_vif_chanctx,
6948 	.set_rts_threshold		= ath12k_mac_op_set_rts_threshold,
6949 	.set_frag_threshold		= ath12k_mac_op_set_frag_threshold,
6950 	.set_bitrate_mask		= ath12k_mac_op_set_bitrate_mask,
6951 	.get_survey			= ath12k_mac_op_get_survey,
6952 	.flush				= ath12k_mac_op_flush,
6953 	.sta_statistics			= ath12k_mac_op_sta_statistics,
6954 };
6955 
ath12k_mac_update_ch_list(struct ath12k * ar,struct ieee80211_supported_band * band,u32 freq_low,u32 freq_high)6956 static void ath12k_mac_update_ch_list(struct ath12k *ar,
6957 				      struct ieee80211_supported_band *band,
6958 				      u32 freq_low, u32 freq_high)
6959 {
6960 	int i;
6961 
6962 	if (!(freq_low && freq_high))
6963 		return;
6964 
6965 	for (i = 0; i < band->n_channels; i++) {
6966 		if (band->channels[i].center_freq < freq_low ||
6967 		    band->channels[i].center_freq > freq_high)
6968 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
6969 	}
6970 }
6971 
ath12k_get_phy_id(struct ath12k * ar,u32 band)6972 static u32 ath12k_get_phy_id(struct ath12k *ar, u32 band)
6973 {
6974 	struct ath12k_pdev *pdev = ar->pdev;
6975 	struct ath12k_pdev_cap *pdev_cap = &pdev->cap;
6976 
6977 	if (band == WMI_HOST_WLAN_2G_CAP)
6978 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
6979 
6980 	if (band == WMI_HOST_WLAN_5G_CAP)
6981 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
6982 
6983 	ath12k_warn(ar->ab, "unsupported phy cap:%d\n", band);
6984 
6985 	return 0;
6986 }
6987 
ath12k_mac_setup_channels_rates(struct ath12k * ar,u32 supported_bands)6988 static int ath12k_mac_setup_channels_rates(struct ath12k *ar,
6989 					   u32 supported_bands)
6990 {
6991 	struct ieee80211_supported_band *band;
6992 	struct ath12k_wmi_hal_reg_capabilities_ext_arg *reg_cap;
6993 	void *channels;
6994 	u32 phy_id;
6995 
6996 	BUILD_BUG_ON((ARRAY_SIZE(ath12k_2ghz_channels) +
6997 		      ARRAY_SIZE(ath12k_5ghz_channels) +
6998 		      ARRAY_SIZE(ath12k_6ghz_channels)) !=
6999 		     ATH12K_NUM_CHANS);
7000 
7001 	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
7002 
7003 	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
7004 		channels = kmemdup(ath12k_2ghz_channels,
7005 				   sizeof(ath12k_2ghz_channels),
7006 				   GFP_KERNEL);
7007 		if (!channels)
7008 			return -ENOMEM;
7009 
7010 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
7011 		band->band = NL80211_BAND_2GHZ;
7012 		band->n_channels = ARRAY_SIZE(ath12k_2ghz_channels);
7013 		band->channels = channels;
7014 		band->n_bitrates = ath12k_g_rates_size;
7015 		band->bitrates = ath12k_g_rates;
7016 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
7017 
7018 		if (ar->ab->hw_params->single_pdev_only) {
7019 			phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
7020 			reg_cap = &ar->ab->hal_reg_cap[phy_id];
7021 		}
7022 		ath12k_mac_update_ch_list(ar, band,
7023 					  reg_cap->low_2ghz_chan,
7024 					  reg_cap->high_2ghz_chan);
7025 	}
7026 
7027 	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
7028 		if (reg_cap->high_5ghz_chan >= ATH12K_MIN_6G_FREQ) {
7029 			channels = kmemdup(ath12k_6ghz_channels,
7030 					   sizeof(ath12k_6ghz_channels), GFP_KERNEL);
7031 			if (!channels) {
7032 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7033 				return -ENOMEM;
7034 			}
7035 
7036 			ar->supports_6ghz = true;
7037 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
7038 			band->band = NL80211_BAND_6GHZ;
7039 			band->n_channels = ARRAY_SIZE(ath12k_6ghz_channels);
7040 			band->channels = channels;
7041 			band->n_bitrates = ath12k_a_rates_size;
7042 			band->bitrates = ath12k_a_rates;
7043 			ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
7044 			ath12k_mac_update_ch_list(ar, band,
7045 						  reg_cap->low_5ghz_chan,
7046 						  reg_cap->high_5ghz_chan);
7047 		}
7048 
7049 		if (reg_cap->low_5ghz_chan < ATH12K_MIN_6G_FREQ) {
7050 			channels = kmemdup(ath12k_5ghz_channels,
7051 					   sizeof(ath12k_5ghz_channels),
7052 					   GFP_KERNEL);
7053 			if (!channels) {
7054 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7055 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
7056 				return -ENOMEM;
7057 			}
7058 
7059 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
7060 			band->band = NL80211_BAND_5GHZ;
7061 			band->n_channels = ARRAY_SIZE(ath12k_5ghz_channels);
7062 			band->channels = channels;
7063 			band->n_bitrates = ath12k_a_rates_size;
7064 			band->bitrates = ath12k_a_rates;
7065 			ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
7066 
7067 			if (ar->ab->hw_params->single_pdev_only) {
7068 				phy_id = ath12k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
7069 				reg_cap = &ar->ab->hal_reg_cap[phy_id];
7070 			}
7071 
7072 			ath12k_mac_update_ch_list(ar, band,
7073 						  reg_cap->low_5ghz_chan,
7074 						  reg_cap->high_5ghz_chan);
7075 		}
7076 	}
7077 
7078 	return 0;
7079 }
7080 
ath12k_mac_setup_iface_combinations(struct ath12k * ar)7081 static int ath12k_mac_setup_iface_combinations(struct ath12k *ar)
7082 {
7083 	struct ath12k_base *ab = ar->ab;
7084 	struct ieee80211_iface_combination *combinations;
7085 	struct ieee80211_iface_limit *limits;
7086 	int n_limits, max_interfaces;
7087 	bool ap, mesh;
7088 
7089 	ap = ab->hw_params->interface_modes & BIT(NL80211_IFTYPE_AP);
7090 
7091 	mesh = IS_ENABLED(CONFIG_MAC80211_MESH) &&
7092 		ab->hw_params->interface_modes & BIT(NL80211_IFTYPE_MESH_POINT);
7093 
7094 	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
7095 	if (!combinations)
7096 		return -ENOMEM;
7097 
7098 	if (ap || mesh) {
7099 		n_limits = 2;
7100 		max_interfaces = 16;
7101 	} else {
7102 		n_limits = 1;
7103 		max_interfaces = 1;
7104 	}
7105 
7106 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
7107 	if (!limits) {
7108 		kfree(combinations);
7109 		return -ENOMEM;
7110 	}
7111 
7112 	limits[0].max = 1;
7113 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
7114 
7115 	if (ap) {
7116 		limits[1].max = max_interfaces;
7117 		limits[1].types |= BIT(NL80211_IFTYPE_AP);
7118 	}
7119 
7120 	if (mesh)
7121 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
7122 
7123 	combinations[0].limits = limits;
7124 	combinations[0].n_limits = n_limits;
7125 	combinations[0].max_interfaces = max_interfaces;
7126 	combinations[0].num_different_channels = 1;
7127 	combinations[0].beacon_int_infra_match = true;
7128 	combinations[0].beacon_int_min_gcd = 100;
7129 	combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
7130 						BIT(NL80211_CHAN_WIDTH_20) |
7131 						BIT(NL80211_CHAN_WIDTH_40) |
7132 						BIT(NL80211_CHAN_WIDTH_80);
7133 
7134 	ar->hw->wiphy->iface_combinations = combinations;
7135 	ar->hw->wiphy->n_iface_combinations = 1;
7136 
7137 	return 0;
7138 }
7139 
7140 static const u8 ath12k_if_types_ext_capa[] = {
7141 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
7142 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
7143 };
7144 
7145 static const u8 ath12k_if_types_ext_capa_sta[] = {
7146 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
7147 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
7148 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
7149 };
7150 
7151 static const u8 ath12k_if_types_ext_capa_ap[] = {
7152 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
7153 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
7154 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
7155 };
7156 
7157 static const struct wiphy_iftype_ext_capab ath12k_iftypes_ext_capa[] = {
7158 	{
7159 		.extended_capabilities = ath12k_if_types_ext_capa,
7160 		.extended_capabilities_mask = ath12k_if_types_ext_capa,
7161 		.extended_capabilities_len = sizeof(ath12k_if_types_ext_capa),
7162 	}, {
7163 		.iftype = NL80211_IFTYPE_STATION,
7164 		.extended_capabilities = ath12k_if_types_ext_capa_sta,
7165 		.extended_capabilities_mask = ath12k_if_types_ext_capa_sta,
7166 		.extended_capabilities_len =
7167 				sizeof(ath12k_if_types_ext_capa_sta),
7168 	}, {
7169 		.iftype = NL80211_IFTYPE_AP,
7170 		.extended_capabilities = ath12k_if_types_ext_capa_ap,
7171 		.extended_capabilities_mask = ath12k_if_types_ext_capa_ap,
7172 		.extended_capabilities_len =
7173 				sizeof(ath12k_if_types_ext_capa_ap),
7174 	},
7175 };
7176 
__ath12k_mac_unregister(struct ath12k * ar)7177 static void __ath12k_mac_unregister(struct ath12k *ar)
7178 {
7179 	cancel_work_sync(&ar->regd_update_work);
7180 
7181 	ieee80211_unregister_hw(ar->hw);
7182 
7183 	idr_for_each(&ar->txmgmt_idr, ath12k_mac_tx_mgmt_pending_free, ar);
7184 	idr_destroy(&ar->txmgmt_idr);
7185 
7186 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7187 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
7188 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
7189 
7190 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
7191 	kfree(ar->hw->wiphy->iface_combinations);
7192 
7193 	SET_IEEE80211_DEV(ar->hw, NULL);
7194 }
7195 
ath12k_mac_unregister(struct ath12k_base * ab)7196 void ath12k_mac_unregister(struct ath12k_base *ab)
7197 {
7198 	struct ath12k *ar;
7199 	struct ath12k_pdev *pdev;
7200 	int i;
7201 
7202 	for (i = 0; i < ab->num_radios; i++) {
7203 		pdev = &ab->pdevs[i];
7204 		ar = pdev->ar;
7205 		if (!ar)
7206 			continue;
7207 
7208 		__ath12k_mac_unregister(ar);
7209 	}
7210 }
7211 
__ath12k_mac_register(struct ath12k * ar)7212 static int __ath12k_mac_register(struct ath12k *ar)
7213 {
7214 	struct ath12k_base *ab = ar->ab;
7215 	struct ath12k_pdev_cap *cap = &ar->pdev->cap;
7216 	static const u32 cipher_suites[] = {
7217 		WLAN_CIPHER_SUITE_TKIP,
7218 		WLAN_CIPHER_SUITE_CCMP,
7219 		WLAN_CIPHER_SUITE_AES_CMAC,
7220 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
7221 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
7222 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
7223 		WLAN_CIPHER_SUITE_GCMP,
7224 		WLAN_CIPHER_SUITE_GCMP_256,
7225 		WLAN_CIPHER_SUITE_CCMP_256,
7226 	};
7227 	int ret;
7228 	u32 ht_cap = 0;
7229 
7230 	ath12k_pdev_caps_update(ar);
7231 
7232 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
7233 
7234 	SET_IEEE80211_DEV(ar->hw, ab->dev);
7235 
7236 	ret = ath12k_mac_setup_channels_rates(ar,
7237 					      cap->supported_bands);
7238 	if (ret)
7239 		goto err;
7240 
7241 	ath12k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
7242 	ath12k_mac_setup_sband_iftype_data(ar, cap);
7243 
7244 	ret = ath12k_mac_setup_iface_combinations(ar);
7245 	if (ret) {
7246 		ath12k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
7247 		goto err_free_channels;
7248 	}
7249 
7250 	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
7251 	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
7252 
7253 	ar->hw->wiphy->interface_modes = ab->hw_params->interface_modes;
7254 
7255 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
7256 	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
7257 	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
7258 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
7259 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
7260 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
7261 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
7262 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
7263 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
7264 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
7265 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
7266 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
7267 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
7268 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
7269 
7270 	if (ht_cap & WMI_HT_CAP_ENABLED) {
7271 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
7272 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
7273 		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
7274 		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
7275 		ieee80211_hw_set(ar->hw, USES_RSS);
7276 	}
7277 
7278 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
7279 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
7280 
7281 	/* TODO: Check if HT capability advertised from firmware is different
7282 	 * for each band for a dual band capable radio. It will be tricky to
7283 	 * handle it when the ht capability different for each band.
7284 	 */
7285 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS)
7286 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
7287 
7288 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
7289 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
7290 
7291 	ar->hw->max_listen_interval = ATH12K_MAX_HW_LISTEN_INTERVAL;
7292 
7293 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
7294 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
7295 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
7296 
7297 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
7298 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
7299 				   NL80211_FEATURE_AP_SCAN;
7300 
7301 	ar->max_num_stations = TARGET_NUM_STATIONS;
7302 	ar->max_num_peers = TARGET_NUM_PEERS_PDEV;
7303 
7304 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
7305 
7306 	ar->hw->queues = ATH12K_HW_MAX_QUEUES;
7307 	ar->hw->wiphy->tx_queue_len = ATH12K_QUEUE_LEN;
7308 	ar->hw->offchannel_tx_hw_queue = ATH12K_HW_MAX_QUEUES - 1;
7309 	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_HE;
7310 
7311 	ar->hw->vif_data_size = sizeof(struct ath12k_vif);
7312 	ar->hw->sta_data_size = sizeof(struct ath12k_sta);
7313 
7314 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
7315 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
7316 
7317 	ar->hw->wiphy->cipher_suites = cipher_suites;
7318 	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
7319 
7320 	ar->hw->wiphy->iftype_ext_capab = ath12k_iftypes_ext_capa;
7321 	ar->hw->wiphy->num_iftype_ext_capab =
7322 		ARRAY_SIZE(ath12k_iftypes_ext_capa);
7323 
7324 	if (ar->supports_6ghz) {
7325 		wiphy_ext_feature_set(ar->hw->wiphy,
7326 				      NL80211_EXT_FEATURE_FILS_DISCOVERY);
7327 		wiphy_ext_feature_set(ar->hw->wiphy,
7328 				      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP);
7329 	}
7330 
7331 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_PUNCT);
7332 
7333 	ath12k_reg_init(ar);
7334 
7335 	if (!test_bit(ATH12K_FLAG_RAW_MODE, &ab->dev_flags)) {
7336 		ar->hw->netdev_features = NETIF_F_HW_CSUM;
7337 		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
7338 		ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
7339 	}
7340 
7341 	ret = ieee80211_register_hw(ar->hw);
7342 	if (ret) {
7343 		ath12k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
7344 		goto err_free_if_combs;
7345 	}
7346 
7347 	if (!ab->hw_params->supports_monitor)
7348 		/* There's a race between calling ieee80211_register_hw()
7349 		 * and here where the monitor mode is enabled for a little
7350 		 * while. But that time is so short and in practise it make
7351 		 * a difference in real life.
7352 		 */
7353 		ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
7354 
7355 	/* Apply the regd received during initialization */
7356 	ret = ath12k_regd_update(ar, true);
7357 	if (ret) {
7358 		ath12k_err(ar->ab, "ath12k regd update failed: %d\n", ret);
7359 		goto err_unregister_hw;
7360 	}
7361 
7362 	return 0;
7363 
7364 err_unregister_hw:
7365 	ieee80211_unregister_hw(ar->hw);
7366 
7367 err_free_if_combs:
7368 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
7369 	kfree(ar->hw->wiphy->iface_combinations);
7370 
7371 err_free_channels:
7372 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7373 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
7374 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
7375 
7376 err:
7377 	SET_IEEE80211_DEV(ar->hw, NULL);
7378 	return ret;
7379 }
7380 
ath12k_mac_register(struct ath12k_base * ab)7381 int ath12k_mac_register(struct ath12k_base *ab)
7382 {
7383 	struct ath12k *ar;
7384 	struct ath12k_pdev *pdev;
7385 	int i;
7386 	int ret;
7387 
7388 	if (test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))
7389 		return 0;
7390 
7391 	for (i = 0; i < ab->num_radios; i++) {
7392 		pdev = &ab->pdevs[i];
7393 		ar = pdev->ar;
7394 		if (ab->pdevs_macaddr_valid) {
7395 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
7396 		} else {
7397 			ether_addr_copy(ar->mac_addr, ab->mac_addr);
7398 			ar->mac_addr[4] += i;
7399 		}
7400 
7401 		ret = __ath12k_mac_register(ar);
7402 		if (ret)
7403 			goto err_cleanup;
7404 
7405 		init_waitqueue_head(&ar->txmgmt_empty_waitq);
7406 		idr_init(&ar->txmgmt_idr);
7407 		spin_lock_init(&ar->txmgmt_idr_lock);
7408 	}
7409 
7410 	/* Initialize channel counters frequency value in hertz */
7411 	ab->cc_freq_hz = 320000;
7412 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
7413 
7414 	return 0;
7415 
7416 err_cleanup:
7417 	for (i = i - 1; i >= 0; i--) {
7418 		pdev = &ab->pdevs[i];
7419 		ar = pdev->ar;
7420 		__ath12k_mac_unregister(ar);
7421 	}
7422 
7423 	return ret;
7424 }
7425 
ath12k_mac_allocate(struct ath12k_base * ab)7426 int ath12k_mac_allocate(struct ath12k_base *ab)
7427 {
7428 	struct ieee80211_hw *hw;
7429 	struct ath12k *ar;
7430 	struct ath12k_pdev *pdev;
7431 	int ret;
7432 	int i;
7433 
7434 	if (test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))
7435 		return 0;
7436 
7437 	for (i = 0; i < ab->num_radios; i++) {
7438 		pdev = &ab->pdevs[i];
7439 		hw = ieee80211_alloc_hw(sizeof(struct ath12k), &ath12k_ops);
7440 		if (!hw) {
7441 			ath12k_warn(ab, "failed to allocate mac80211 hw device\n");
7442 			ret = -ENOMEM;
7443 			goto err_free_mac;
7444 		}
7445 
7446 		ar = hw->priv;
7447 		ar->hw = hw;
7448 		ar->ab = ab;
7449 		ar->pdev = pdev;
7450 		ar->pdev_idx = i;
7451 		ar->lmac_id = ath12k_hw_get_mac_from_pdev_id(ab->hw_params, i);
7452 
7453 		ar->wmi = &ab->wmi_ab.wmi[i];
7454 		/* FIXME: wmi[0] is already initialized during attach,
7455 		 * Should we do this again?
7456 		 */
7457 		ath12k_wmi_pdev_attach(ab, i);
7458 
7459 		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
7460 		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
7461 		ar->num_tx_chains = hweight32(pdev->cap.tx_chain_mask);
7462 		ar->num_rx_chains = hweight32(pdev->cap.rx_chain_mask);
7463 
7464 		pdev->ar = ar;
7465 		spin_lock_init(&ar->data_lock);
7466 		INIT_LIST_HEAD(&ar->arvifs);
7467 		INIT_LIST_HEAD(&ar->ppdu_stats_info);
7468 		mutex_init(&ar->conf_mutex);
7469 		init_completion(&ar->vdev_setup_done);
7470 		init_completion(&ar->vdev_delete_done);
7471 		init_completion(&ar->peer_assoc_done);
7472 		init_completion(&ar->peer_delete_done);
7473 		init_completion(&ar->install_key_done);
7474 		init_completion(&ar->bss_survey_done);
7475 		init_completion(&ar->scan.started);
7476 		init_completion(&ar->scan.completed);
7477 
7478 		INIT_DELAYED_WORK(&ar->scan.timeout, ath12k_scan_timeout_work);
7479 		INIT_WORK(&ar->regd_update_work, ath12k_regd_update_work);
7480 
7481 		INIT_WORK(&ar->wmi_mgmt_tx_work, ath12k_mgmt_over_wmi_tx_work);
7482 		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
7483 		clear_bit(ATH12K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
7484 	}
7485 
7486 	return 0;
7487 
7488 err_free_mac:
7489 	ath12k_mac_destroy(ab);
7490 
7491 	return ret;
7492 }
7493 
ath12k_mac_destroy(struct ath12k_base * ab)7494 void ath12k_mac_destroy(struct ath12k_base *ab)
7495 {
7496 	struct ath12k *ar;
7497 	struct ath12k_pdev *pdev;
7498 	int i;
7499 
7500 	for (i = 0; i < ab->num_radios; i++) {
7501 		pdev = &ab->pdevs[i];
7502 		ar = pdev->ar;
7503 		if (!ar)
7504 			continue;
7505 
7506 		ieee80211_free_hw(ar->hw);
7507 		pdev->ar = NULL;
7508 	}
7509 }
7510