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