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