• 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 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
3423 	}
3424 
3425 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
3426 		sound_dim = vht_cap &
3427 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3428 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3429 		if (sound_dim > (ar->num_tx_chains - 1))
3430 			sound_dim = ar->num_tx_chains - 1;
3431 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
3432 	}
3433 
3434 	if (!value)
3435 		return 0;
3436 
3437 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
3438 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
3439 
3440 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
3441 		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
3442 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
3443 	}
3444 
3445 	/* TODO: SUBFEE not validated in HK, disable here until validated? */
3446 
3447 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
3448 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
3449 
3450 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
3451 		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
3452 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
3453 	}
3454 
3455 	return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3456 					     vdev_param, value);
3457 }
3458 
ath11k_set_vht_txbf_cap(struct ath11k * ar,u32 * vht_cap)3459 static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
3460 {
3461 	bool subfer, subfee;
3462 	int sound_dim = 0;
3463 
3464 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
3465 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
3466 
3467 	if (ar->num_tx_chains < 2) {
3468 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
3469 		subfer = false;
3470 	}
3471 
3472 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
3473 	if (!subfer)
3474 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
3475 
3476 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
3477 	if (!subfee)
3478 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
3479 
3480 	sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
3481 	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3482 	*vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3483 
3484 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
3485 
3486 	/* Enable Sounding Dimension Field only if SU BF is enabled */
3487 	if (subfer) {
3488 		if (sound_dim > (ar->num_tx_chains - 1))
3489 			sound_dim = ar->num_tx_chains - 1;
3490 
3491 		sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3492 		sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3493 		*vht_cap |= sound_dim;
3494 	}
3495 
3496 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
3497 	if (!subfee)
3498 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
3499 }
3500 
3501 static struct ieee80211_sta_vht_cap
ath11k_create_vht_cap(struct ath11k * ar,u32 rate_cap_tx_chainmask,u32 rate_cap_rx_chainmask)3502 ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
3503 		      u32 rate_cap_rx_chainmask)
3504 {
3505 	struct ieee80211_sta_vht_cap vht_cap = {0};
3506 	u16 txmcs_map, rxmcs_map;
3507 	int i;
3508 
3509 	vht_cap.vht_supported = 1;
3510 	vht_cap.cap = ar->pdev->cap.vht_cap;
3511 
3512 	ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
3513 
3514 	/* TODO: Enable back VHT160 mode once association issues are fixed */
3515 	/* Disabling VHT160 and VHT80+80 modes */
3516 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
3517 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
3518 
3519 	rxmcs_map = 0;
3520 	txmcs_map = 0;
3521 	for (i = 0; i < 8; i++) {
3522 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
3523 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3524 		else
3525 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3526 
3527 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
3528 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3529 		else
3530 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3531 	}
3532 
3533 	if (rate_cap_tx_chainmask <= 1)
3534 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
3535 
3536 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
3537 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
3538 
3539 	return vht_cap;
3540 }
3541 
ath11k_mac_setup_ht_vht_cap(struct ath11k * ar,struct ath11k_pdev_cap * cap,u32 * ht_cap_info)3542 static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
3543 					struct ath11k_pdev_cap *cap,
3544 					u32 *ht_cap_info)
3545 {
3546 	struct ieee80211_supported_band *band;
3547 	u32 rate_cap_tx_chainmask;
3548 	u32 rate_cap_rx_chainmask;
3549 	u32 ht_cap;
3550 
3551 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
3552 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
3553 
3554 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
3555 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
3556 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
3557 		if (ht_cap_info)
3558 			*ht_cap_info = ht_cap;
3559 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3560 						    rate_cap_rx_chainmask);
3561 	}
3562 
3563 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP && !ar->supports_6ghz) {
3564 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
3565 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
3566 		if (ht_cap_info)
3567 			*ht_cap_info = ht_cap;
3568 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3569 						    rate_cap_rx_chainmask);
3570 		band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
3571 						      rate_cap_rx_chainmask);
3572 	}
3573 }
3574 
ath11k_check_chain_mask(struct ath11k * ar,u32 ant,bool is_tx_ant)3575 static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
3576 {
3577 	/* TODO: Check the request chainmask against the supported
3578 	 * chainmask table which is advertised in extented_service_ready event
3579 	 */
3580 
3581 	return 0;
3582 }
3583 
ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold * fw_ppet,u8 * he_ppet)3584 static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
3585 				  u8 *he_ppet)
3586 {
3587 	int nss, ru;
3588 	u8 bit = 7;
3589 
3590 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
3591 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
3592 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
3593 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
3594 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
3595 		for (ru = 0; ru < 4; ru++) {
3596 			u8 val;
3597 			int i;
3598 
3599 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
3600 				continue;
3601 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
3602 			       0x3f;
3603 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
3604 			for (i = 5; i >= 0; i--) {
3605 				he_ppet[bit / 8] |=
3606 					((val >> i) & 0x1) << ((bit % 8));
3607 				bit++;
3608 			}
3609 		}
3610 	}
3611 }
3612 
3613 static void
ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem * he_cap_elem)3614 ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
3615 {
3616 	u8 m;
3617 
3618 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
3619 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
3620 	he_cap_elem->mac_cap_info[0] &= ~m;
3621 
3622 	m = IEEE80211_HE_MAC_CAP2_TRS |
3623 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3624 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3625 	he_cap_elem->mac_cap_info[2] &= ~m;
3626 
3627 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
3628 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3629 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3630 	he_cap_elem->mac_cap_info[3] &= ~m;
3631 
3632 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
3633 	    IEEE80211_HE_MAC_CAP4_BQR;
3634 	he_cap_elem->mac_cap_info[4] &= ~m;
3635 
3636 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECVITE_TRANSMISSION |
3637 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
3638 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
3639 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
3640 	he_cap_elem->mac_cap_info[5] &= ~m;
3641 
3642 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
3643 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
3644 	he_cap_elem->phy_cap_info[2] &= ~m;
3645 
3646 	m = IEEE80211_HE_PHY_CAP3_RX_HE_MU_PPDU_FROM_NON_AP_STA |
3647 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
3648 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
3649 	he_cap_elem->phy_cap_info[3] &= ~m;
3650 
3651 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
3652 	he_cap_elem->phy_cap_info[4] &= ~m;
3653 
3654 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
3655 	he_cap_elem->phy_cap_info[5] &= ~m;
3656 
3657 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
3658 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMER_FB |
3659 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
3660 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
3661 	he_cap_elem->phy_cap_info[6] &= ~m;
3662 
3663 	m = IEEE80211_HE_PHY_CAP7_SRP_BASED_SR |
3664 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_AR |
3665 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
3666 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
3667 	he_cap_elem->phy_cap_info[7] &= ~m;
3668 
3669 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
3670 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
3671 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
3672 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
3673 	he_cap_elem->phy_cap_info[8] &= ~m;
3674 
3675 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
3676 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
3677 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
3678 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
3679 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
3680 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
3681 	he_cap_elem->phy_cap_info[9] &= ~m;
3682 }
3683 
ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap * pcap,struct ath11k_band_cap * bcap)3684 static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
3685 					   struct ath11k_band_cap *bcap)
3686 {
3687 	u8 val;
3688 
3689 	bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
3690 	if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3691 		bcap->he_6ghz_capa |=
3692 			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
3693 				   WLAN_HT_CAP_SM_PS_DYNAMIC);
3694 	else
3695 		bcap->he_6ghz_capa |=
3696 			FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
3697 				   WLAN_HT_CAP_SM_PS_DISABLED);
3698 	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
3699 			pcap->vht_cap);
3700 	bcap->he_6ghz_capa |=
3701 		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
3702 	val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
3703 	bcap->he_6ghz_capa |=
3704 		FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
3705 	if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
3706 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
3707 	if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
3708 		bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
3709 
3710 	return cpu_to_le16(bcap->he_6ghz_capa);
3711 }
3712 
ath11k_mac_copy_he_cap(struct ath11k * ar,struct ath11k_pdev_cap * cap,struct ieee80211_sband_iftype_data * data,int band)3713 static int ath11k_mac_copy_he_cap(struct ath11k *ar,
3714 				  struct ath11k_pdev_cap *cap,
3715 				  struct ieee80211_sband_iftype_data *data,
3716 				  int band)
3717 {
3718 	int i, idx = 0;
3719 
3720 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
3721 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
3722 		struct ath11k_band_cap *band_cap = &cap->band[band];
3723 		struct ieee80211_he_cap_elem *he_cap_elem =
3724 				&he_cap->he_cap_elem;
3725 
3726 		switch (i) {
3727 		case NL80211_IFTYPE_STATION:
3728 		case NL80211_IFTYPE_AP:
3729 		case NL80211_IFTYPE_MESH_POINT:
3730 			break;
3731 
3732 		default:
3733 			continue;
3734 		}
3735 
3736 		data[idx].types_mask = BIT(i);
3737 		he_cap->has_he = true;
3738 		memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
3739 		       sizeof(he_cap_elem->mac_cap_info));
3740 		memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
3741 		       sizeof(he_cap_elem->phy_cap_info));
3742 
3743 		he_cap_elem->mac_cap_info[1] &=
3744 			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
3745 		he_cap_elem->phy_cap_info[4] &=
3746 			~IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_MASK;
3747 		he_cap_elem->phy_cap_info[4] &=
3748 			~IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_MASK;
3749 		he_cap_elem->phy_cap_info[4] |= (ar->num_tx_chains - 1) << 2;
3750 
3751 		he_cap_elem->phy_cap_info[5] &=
3752 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
3753 		he_cap_elem->phy_cap_info[5] &=
3754 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK;
3755 		he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
3756 
3757 		switch (i) {
3758 		case NL80211_IFTYPE_AP:
3759 			he_cap_elem->phy_cap_info[3] &=
3760 				~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
3761 			he_cap_elem->phy_cap_info[9] |=
3762 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
3763 			break;
3764 		case NL80211_IFTYPE_STATION:
3765 			he_cap_elem->mac_cap_info[0] &=
3766 				~IEEE80211_HE_MAC_CAP0_TWT_RES;
3767 			he_cap_elem->mac_cap_info[0] |=
3768 				IEEE80211_HE_MAC_CAP0_TWT_REQ;
3769 			he_cap_elem->phy_cap_info[9] |=
3770 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
3771 			break;
3772 		case NL80211_IFTYPE_MESH_POINT:
3773 			ath11k_mac_filter_he_cap_mesh(he_cap_elem);
3774 			break;
3775 		}
3776 
3777 		he_cap->he_mcs_nss_supp.rx_mcs_80 =
3778 			cpu_to_le16(band_cap->he_mcs & 0xffff);
3779 		he_cap->he_mcs_nss_supp.tx_mcs_80 =
3780 			cpu_to_le16(band_cap->he_mcs & 0xffff);
3781 		he_cap->he_mcs_nss_supp.rx_mcs_160 =
3782 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3783 		he_cap->he_mcs_nss_supp.tx_mcs_160 =
3784 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3785 		he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
3786 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3787 		he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
3788 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3789 
3790 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
3791 		if (he_cap_elem->phy_cap_info[6] &
3792 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
3793 			ath11k_gen_ppe_thresh(&band_cap->he_ppet,
3794 					      he_cap->ppe_thres);
3795 
3796 		if (band == NL80211_BAND_6GHZ) {
3797 			data[idx].he_6ghz_capa.capa =
3798 				ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
3799 		}
3800 		idx++;
3801 	}
3802 
3803 	return idx;
3804 }
3805 
ath11k_mac_setup_he_cap(struct ath11k * ar,struct ath11k_pdev_cap * cap)3806 static void ath11k_mac_setup_he_cap(struct ath11k *ar,
3807 				    struct ath11k_pdev_cap *cap)
3808 {
3809 	struct ieee80211_supported_band *band;
3810 	int count;
3811 
3812 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
3813 		count = ath11k_mac_copy_he_cap(ar, cap,
3814 					       ar->mac.iftype[NL80211_BAND_2GHZ],
3815 					       NL80211_BAND_2GHZ);
3816 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
3817 		band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
3818 		band->n_iftype_data = count;
3819 	}
3820 
3821 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
3822 		count = ath11k_mac_copy_he_cap(ar, cap,
3823 					       ar->mac.iftype[NL80211_BAND_5GHZ],
3824 					       NL80211_BAND_5GHZ);
3825 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
3826 		band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
3827 		band->n_iftype_data = count;
3828 	}
3829 
3830 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP &&
3831 	    ar->supports_6ghz) {
3832 		count = ath11k_mac_copy_he_cap(ar, cap,
3833 					       ar->mac.iftype[NL80211_BAND_6GHZ],
3834 					       NL80211_BAND_6GHZ);
3835 		band = &ar->mac.sbands[NL80211_BAND_6GHZ];
3836 		band->iftype_data = ar->mac.iftype[NL80211_BAND_6GHZ];
3837 		band->n_iftype_data = count;
3838 	}
3839 }
3840 
__ath11k_set_antenna(struct ath11k * ar,u32 tx_ant,u32 rx_ant)3841 static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
3842 {
3843 	int ret;
3844 
3845 	lockdep_assert_held(&ar->conf_mutex);
3846 
3847 	if (ath11k_check_chain_mask(ar, tx_ant, true))
3848 		return -EINVAL;
3849 
3850 	if (ath11k_check_chain_mask(ar, rx_ant, false))
3851 		return -EINVAL;
3852 
3853 	ar->cfg_tx_chainmask = tx_ant;
3854 	ar->cfg_rx_chainmask = rx_ant;
3855 
3856 	if (ar->state != ATH11K_STATE_ON &&
3857 	    ar->state != ATH11K_STATE_RESTARTED)
3858 		return 0;
3859 
3860 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
3861 					tx_ant, ar->pdev->pdev_id);
3862 	if (ret) {
3863 		ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
3864 			    ret, tx_ant);
3865 		return ret;
3866 	}
3867 
3868 	ar->num_tx_chains = get_num_chains(tx_ant);
3869 
3870 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
3871 					rx_ant, ar->pdev->pdev_id);
3872 	if (ret) {
3873 		ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
3874 			    ret, rx_ant);
3875 		return ret;
3876 	}
3877 
3878 	ar->num_rx_chains = get_num_chains(rx_ant);
3879 
3880 	/* Reload HT/VHT/HE capability */
3881 	ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
3882 	ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
3883 
3884 	return 0;
3885 }
3886 
ath11k_mac_tx_mgmt_free(struct ath11k * ar,int buf_id)3887 static void ath11k_mac_tx_mgmt_free(struct ath11k *ar, int buf_id)
3888 {
3889 	struct sk_buff *msdu;
3890 	struct ieee80211_tx_info *info;
3891 
3892 	spin_lock_bh(&ar->txmgmt_idr_lock);
3893 	msdu = idr_remove(&ar->txmgmt_idr, buf_id);
3894 	spin_unlock_bh(&ar->txmgmt_idr_lock);
3895 
3896 	if (!msdu)
3897 		return;
3898 
3899 	dma_unmap_single(ar->ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
3900 			 DMA_TO_DEVICE);
3901 
3902 	info = IEEE80211_SKB_CB(msdu);
3903 	memset(&info->status, 0, sizeof(info->status));
3904 
3905 	ieee80211_free_txskb(ar->hw, msdu);
3906 }
3907 
ath11k_mac_tx_mgmt_pending_free(int buf_id,void * skb,void * ctx)3908 int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
3909 {
3910 	struct ath11k *ar = ctx;
3911 
3912 	ath11k_mac_tx_mgmt_free(ar, buf_id);
3913 
3914 	return 0;
3915 }
3916 
ath11k_mac_vif_txmgmt_idr_remove(int buf_id,void * skb,void * ctx)3917 static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
3918 {
3919 	struct ieee80211_vif *vif = ctx;
3920 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
3921 	struct ath11k *ar = skb_cb->ar;
3922 
3923 	if (skb_cb->vif == vif)
3924 		ath11k_mac_tx_mgmt_free(ar, buf_id);
3925 
3926 	return 0;
3927 }
3928 
ath11k_mac_mgmt_tx_wmi(struct ath11k * ar,struct ath11k_vif * arvif,struct sk_buff * skb)3929 static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
3930 				  struct sk_buff *skb)
3931 {
3932 	struct ath11k_base *ab = ar->ab;
3933 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3934 	struct ieee80211_tx_info *info;
3935 	dma_addr_t paddr;
3936 	int buf_id;
3937 	int ret;
3938 
3939 	ATH11K_SKB_CB(skb)->ar = ar;
3940 
3941 	spin_lock_bh(&ar->txmgmt_idr_lock);
3942 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
3943 			   ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
3944 	spin_unlock_bh(&ar->txmgmt_idr_lock);
3945 	if (buf_id < 0)
3946 		return -ENOSPC;
3947 
3948 	info = IEEE80211_SKB_CB(skb);
3949 	if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
3950 		if ((ieee80211_is_action(hdr->frame_control) ||
3951 		     ieee80211_is_deauth(hdr->frame_control) ||
3952 		     ieee80211_is_disassoc(hdr->frame_control)) &&
3953 		     ieee80211_has_protected(hdr->frame_control)) {
3954 			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
3955 		}
3956 	}
3957 
3958 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
3959 	if (dma_mapping_error(ab->dev, paddr)) {
3960 		ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
3961 		ret = -EIO;
3962 		goto err_free_idr;
3963 	}
3964 
3965 	ATH11K_SKB_CB(skb)->paddr = paddr;
3966 
3967 	ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
3968 	if (ret) {
3969 		ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
3970 		goto err_unmap_buf;
3971 	}
3972 
3973 	return 0;
3974 
3975 err_unmap_buf:
3976 	dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
3977 			 skb->len, DMA_TO_DEVICE);
3978 err_free_idr:
3979 	spin_lock_bh(&ar->txmgmt_idr_lock);
3980 	idr_remove(&ar->txmgmt_idr, buf_id);
3981 	spin_unlock_bh(&ar->txmgmt_idr_lock);
3982 
3983 	return ret;
3984 }
3985 
ath11k_mgmt_over_wmi_tx_purge(struct ath11k * ar)3986 static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
3987 {
3988 	struct sk_buff *skb;
3989 
3990 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
3991 		ieee80211_free_txskb(ar->hw, skb);
3992 }
3993 
ath11k_mgmt_over_wmi_tx_work(struct work_struct * work)3994 static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
3995 {
3996 	struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
3997 	struct ath11k_skb_cb *skb_cb;
3998 	struct ath11k_vif *arvif;
3999 	struct sk_buff *skb;
4000 	int ret;
4001 
4002 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
4003 		skb_cb = ATH11K_SKB_CB(skb);
4004 		if (!skb_cb->vif) {
4005 			ath11k_warn(ar->ab, "no vif found for mgmt frame\n");
4006 			ieee80211_free_txskb(ar->hw, skb);
4007 			continue;
4008 		}
4009 
4010 		arvif = ath11k_vif_to_arvif(skb_cb->vif);
4011 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id) &&
4012 		    arvif->is_started) {
4013 			ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
4014 			if (ret) {
4015 				ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
4016 					    arvif->vdev_id, ret);
4017 				ieee80211_free_txskb(ar->hw, skb);
4018 			} else {
4019 				atomic_inc(&ar->num_pending_mgmt_tx);
4020 			}
4021 		} else {
4022 			ath11k_warn(ar->ab,
4023 				    "dropping mgmt frame for vdev %d, is_started %d\n",
4024 				    arvif->vdev_id,
4025 				    arvif->is_started);
4026 			ieee80211_free_txskb(ar->hw, skb);
4027 		}
4028 	}
4029 }
4030 
ath11k_mac_mgmt_tx(struct ath11k * ar,struct sk_buff * skb,bool is_prb_rsp)4031 static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
4032 			      bool is_prb_rsp)
4033 {
4034 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
4035 
4036 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4037 		return -ESHUTDOWN;
4038 
4039 	/* Drop probe response packets when the pending management tx
4040 	 * count has reached a certain threshold, so as to prioritize
4041 	 * other mgmt packets like auth and assoc to be sent on time
4042 	 * for establishing successful connections.
4043 	 */
4044 	if (is_prb_rsp &&
4045 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
4046 		ath11k_warn(ar->ab,
4047 			    "dropping probe response as pending queue is almost full\n");
4048 		return -ENOSPC;
4049 	}
4050 
4051 	if (skb_queue_len(q) == ATH11K_TX_MGMT_NUM_PENDING_MAX) {
4052 		ath11k_warn(ar->ab, "mgmt tx queue is full\n");
4053 		return -ENOSPC;
4054 	}
4055 
4056 	skb_queue_tail(q, skb);
4057 	ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
4058 
4059 	return 0;
4060 }
4061 
ath11k_mac_op_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)4062 static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
4063 			     struct ieee80211_tx_control *control,
4064 			     struct sk_buff *skb)
4065 {
4066 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
4067 	struct ath11k *ar = hw->priv;
4068 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4069 	struct ieee80211_vif *vif = info->control.vif;
4070 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4071 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
4072 	struct ieee80211_key_conf *key = info->control.hw_key;
4073 	u32 info_flags = info->flags;
4074 	bool is_prb_rsp;
4075 	int ret;
4076 
4077 	memset(skb_cb, 0, sizeof(*skb_cb));
4078 	skb_cb->vif = vif;
4079 
4080 	if (key) {
4081 		skb_cb->cipher = key->cipher;
4082 		skb_cb->flags |= ATH11K_SKB_CIPHER_SET;
4083 	}
4084 
4085 	if (info_flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4086 		skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
4087 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
4088 		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
4089 		ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
4090 		if (ret) {
4091 			ath11k_warn(ar->ab, "failed to queue management frame %d\n",
4092 				    ret);
4093 			ieee80211_free_txskb(ar->hw, skb);
4094 		}
4095 		return;
4096 	}
4097 
4098 	ret = ath11k_dp_tx(ar, arvif, skb);
4099 	if (ret) {
4100 		ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
4101 		ieee80211_free_txskb(ar->hw, skb);
4102 	}
4103 }
4104 
ath11k_mac_drain_tx(struct ath11k * ar)4105 void ath11k_mac_drain_tx(struct ath11k *ar)
4106 {
4107 	/* make sure rcu-protected mac80211 tx path itself is drained */
4108 	synchronize_net();
4109 
4110 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
4111 	ath11k_mgmt_over_wmi_tx_purge(ar);
4112 }
4113 
ath11k_mac_config_mon_status_default(struct ath11k * ar,bool enable)4114 static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
4115 {
4116 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
4117 	struct ath11k_base *ab = ar->ab;
4118 	int i, ret = 0;
4119 	u32 ring_id;
4120 
4121 	if (enable) {
4122 		tlv_filter = ath11k_mac_mon_status_filter_default;
4123 		if (ath11k_debugfs_rx_filter(ar))
4124 			tlv_filter.rx_filter = ath11k_debugfs_rx_filter(ar);
4125 	}
4126 
4127 	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
4128 		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
4129 		ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id,
4130 						       ar->dp.mac_id + i,
4131 						       HAL_RXDMA_MONITOR_STATUS,
4132 						       DP_RX_BUFFER_SIZE,
4133 						       &tlv_filter);
4134 	}
4135 
4136 	return ret;
4137 }
4138 
ath11k_mac_op_start(struct ieee80211_hw * hw)4139 static int ath11k_mac_op_start(struct ieee80211_hw *hw)
4140 {
4141 	struct ath11k *ar = hw->priv;
4142 	struct ath11k_base *ab = ar->ab;
4143 	struct ath11k_pdev *pdev = ar->pdev;
4144 	int ret;
4145 
4146 	ath11k_mac_drain_tx(ar);
4147 	mutex_lock(&ar->conf_mutex);
4148 
4149 	switch (ar->state) {
4150 	case ATH11K_STATE_OFF:
4151 		ar->state = ATH11K_STATE_ON;
4152 		break;
4153 	case ATH11K_STATE_RESTARTING:
4154 		ar->state = ATH11K_STATE_RESTARTED;
4155 		break;
4156 	case ATH11K_STATE_RESTARTED:
4157 	case ATH11K_STATE_WEDGED:
4158 	case ATH11K_STATE_ON:
4159 		WARN_ON(1);
4160 		ret = -EINVAL;
4161 		goto err;
4162 	}
4163 
4164 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
4165 					1, pdev->pdev_id);
4166 
4167 	if (ret) {
4168 		ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
4169 		goto err;
4170 	}
4171 
4172 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
4173 					pdev->pdev_id);
4174 	if (ret) {
4175 		ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
4176 		goto err;
4177 	}
4178 
4179 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
4180 					0, pdev->pdev_id);
4181 	if (ret) {
4182 		ath11k_err(ab, "failed to set ac override for ARP: %d\n",
4183 			   ret);
4184 		goto err;
4185 	}
4186 
4187 	ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
4188 	if (ret) {
4189 		ath11k_err(ab, "failed to offload radar detection: %d\n",
4190 			   ret);
4191 		goto err;
4192 	}
4193 
4194 	ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
4195 						  HTT_PPDU_STATS_TAG_DEFAULT);
4196 	if (ret) {
4197 		ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
4198 		goto err;
4199 	}
4200 
4201 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
4202 					1, pdev->pdev_id);
4203 
4204 	if (ret) {
4205 		ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
4206 		goto err;
4207 	}
4208 
4209 	__ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
4210 
4211 	/* TODO: Do we need to enable ANI? */
4212 
4213 	ath11k_reg_update_chan_list(ar);
4214 
4215 	ar->num_started_vdevs = 0;
4216 	ar->num_created_vdevs = 0;
4217 	ar->num_peers = 0;
4218 	ar->allocated_vdev_map = 0;
4219 
4220 	/* Configure monitor status ring with default rx_filter to get rx status
4221 	 * such as rssi, rx_duration.
4222 	 */
4223 	ret = ath11k_mac_config_mon_status_default(ar, true);
4224 	if (ret) {
4225 		ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
4226 			   ret);
4227 		goto err;
4228 	}
4229 
4230 	/* Configure the hash seed for hash based reo dest ring selection */
4231 	ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
4232 
4233 	/* allow device to enter IMPS */
4234 	if (ab->hw_params.idle_ps) {
4235 		ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_IDLE_PS_CONFIG,
4236 						1, pdev->pdev_id);
4237 		if (ret) {
4238 			ath11k_err(ab, "failed to enable idle ps: %d\n", ret);
4239 			goto err;
4240 		}
4241 	}
4242 
4243 	mutex_unlock(&ar->conf_mutex);
4244 
4245 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
4246 			   &ab->pdevs[ar->pdev_idx]);
4247 
4248 	return 0;
4249 
4250 err:
4251 	ar->state = ATH11K_STATE_OFF;
4252 	mutex_unlock(&ar->conf_mutex);
4253 
4254 	return ret;
4255 }
4256 
ath11k_mac_op_stop(struct ieee80211_hw * hw)4257 static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
4258 {
4259 	struct ath11k *ar = hw->priv;
4260 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
4261 	int ret;
4262 
4263 	ath11k_mac_drain_tx(ar);
4264 
4265 	mutex_lock(&ar->conf_mutex);
4266 	ret = ath11k_mac_config_mon_status_default(ar, false);
4267 	if (ret)
4268 		ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
4269 			   ret);
4270 
4271 	clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
4272 	ar->state = ATH11K_STATE_OFF;
4273 	mutex_unlock(&ar->conf_mutex);
4274 
4275 	cancel_delayed_work_sync(&ar->scan.timeout);
4276 	cancel_work_sync(&ar->regd_update_work);
4277 
4278 	spin_lock_bh(&ar->data_lock);
4279 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
4280 		list_del(&ppdu_stats->list);
4281 		kfree(ppdu_stats);
4282 	}
4283 	spin_unlock_bh(&ar->data_lock);
4284 
4285 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
4286 
4287 	synchronize_rcu();
4288 
4289 	atomic_set(&ar->num_pending_mgmt_tx, 0);
4290 }
4291 
4292 static void
ath11k_mac_setup_vdev_create_params(struct ath11k_vif * arvif,struct vdev_create_params * params)4293 ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
4294 				    struct vdev_create_params *params)
4295 {
4296 	struct ath11k *ar = arvif->ar;
4297 	struct ath11k_pdev *pdev = ar->pdev;
4298 
4299 	params->if_id = arvif->vdev_id;
4300 	params->type = arvif->vdev_type;
4301 	params->subtype = arvif->vdev_subtype;
4302 	params->pdev_id = pdev->pdev_id;
4303 
4304 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
4305 		params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
4306 		params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
4307 	}
4308 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
4309 		params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
4310 		params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
4311 	}
4312 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP &&
4313 	    ar->supports_6ghz) {
4314 		params->chains[NL80211_BAND_6GHZ].tx = ar->num_tx_chains;
4315 		params->chains[NL80211_BAND_6GHZ].rx = ar->num_rx_chains;
4316 	}
4317 }
4318 
4319 static u32
ath11k_mac_prepare_he_mode(struct ath11k_pdev * pdev,u32 viftype)4320 ath11k_mac_prepare_he_mode(struct ath11k_pdev *pdev, u32 viftype)
4321 {
4322 	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
4323 	struct ath11k_band_cap *cap_band = NULL;
4324 	u32 *hecap_phy_ptr = NULL;
4325 	u32 hemode = 0;
4326 
4327 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP)
4328 		cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
4329 	else
4330 		cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
4331 
4332 	hecap_phy_ptr = &cap_band->he_cap_phy_info[0];
4333 
4334 	hemode = FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE) |
4335 		 FIELD_PREP(HE_MODE_SU_TX_BFER, HECAP_PHY_SUBFMR_GET(hecap_phy_ptr)) |
4336 		 FIELD_PREP(HE_MODE_UL_MUMIMO, HECAP_PHY_ULMUMIMO_GET(hecap_phy_ptr));
4337 
4338 	/* TODO WDS and other modes */
4339 	if (viftype == NL80211_IFTYPE_AP) {
4340 		hemode |= FIELD_PREP(HE_MODE_MU_TX_BFER,
4341 			  HECAP_PHY_MUBFMR_GET(hecap_phy_ptr)) |
4342 			  FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
4343 			  FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
4344 	} else {
4345 		hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
4346 	}
4347 
4348 	return hemode;
4349 }
4350 
ath11k_set_he_mu_sounding_mode(struct ath11k * ar,struct ath11k_vif * arvif)4351 static int ath11k_set_he_mu_sounding_mode(struct ath11k *ar,
4352 					  struct ath11k_vif *arvif)
4353 {
4354 	u32 param_id, param_value;
4355 	struct ath11k_base *ab = ar->ab;
4356 	int ret = 0;
4357 
4358 	param_id = WMI_VDEV_PARAM_SET_HEMU_MODE;
4359 	param_value = ath11k_mac_prepare_he_mode(ar->pdev, arvif->vif->type);
4360 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4361 					    param_id, param_value);
4362 	if (ret) {
4363 		ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d param_value %x\n",
4364 			    arvif->vdev_id, ret, param_value);
4365 		return ret;
4366 	}
4367 	param_id = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
4368 	param_value =
4369 		FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
4370 		FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
4371 			   HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
4372 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4373 					    param_id, param_value);
4374 	if (ret) {
4375 		ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d\n",
4376 			    arvif->vdev_id, ret);
4377 		return ret;
4378 	}
4379 	return ret;
4380 }
4381 
ath11k_mac_op_update_vif_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif)4382 static void ath11k_mac_op_update_vif_offload(struct ieee80211_hw *hw,
4383 					     struct ieee80211_vif *vif)
4384 {
4385 	struct ath11k *ar = hw->priv;
4386 	struct ath11k_base *ab = ar->ab;
4387 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4388 	u32 param_id, param_value;
4389 	int ret;
4390 
4391 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
4392 	if (ath11k_frame_mode != ATH11K_HW_TXRX_ETHERNET ||
4393 	    (vif->type != NL80211_IFTYPE_STATION &&
4394 	     vif->type != NL80211_IFTYPE_AP))
4395 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
4396 
4397 	if (vif->offload_flags & IEEE80211_OFFLOAD_ENCAP_ENABLED)
4398 		param_value = ATH11K_HW_TXRX_ETHERNET;
4399 	else if (test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags))
4400 		param_value = ATH11K_HW_TXRX_RAW;
4401 	else
4402 		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
4403 
4404 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4405 					    param_id, param_value);
4406 	if (ret) {
4407 		ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
4408 			    arvif->vdev_id, ret);
4409 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
4410 	}
4411 }
4412 
ath11k_mac_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)4413 static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
4414 				       struct ieee80211_vif *vif)
4415 {
4416 	struct ath11k *ar = hw->priv;
4417 	struct ath11k_base *ab = ar->ab;
4418 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4419 	struct vdev_create_params vdev_param = {0};
4420 	struct peer_create_params peer_param;
4421 	u32 param_id, param_value;
4422 	u16 nss;
4423 	int i;
4424 	int ret;
4425 	int bit;
4426 
4427 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
4428 
4429 	mutex_lock(&ar->conf_mutex);
4430 
4431 	if (vif->type == NL80211_IFTYPE_AP &&
4432 	    ar->num_peers > (ar->max_num_peers - 1)) {
4433 		ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
4434 		ret = -ENOBUFS;
4435 		goto err;
4436 	}
4437 
4438 	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS - 1)) {
4439 		ath11k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
4440 			    TARGET_NUM_VDEVS);
4441 		ret = -EBUSY;
4442 		goto err;
4443 	}
4444 
4445 	memset(arvif, 0, sizeof(*arvif));
4446 
4447 	arvif->ar = ar;
4448 	arvif->vif = vif;
4449 
4450 	INIT_LIST_HEAD(&arvif->list);
4451 
4452 	/* Should we initialize any worker to handle connection loss indication
4453 	 * from firmware in sta mode?
4454 	 */
4455 
4456 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4457 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4458 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4459 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4460 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4461 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4462 	}
4463 
4464 	bit = __ffs64(ab->free_vdev_map);
4465 
4466 	arvif->vdev_id = bit;
4467 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
4468 
4469 	switch (vif->type) {
4470 	case NL80211_IFTYPE_UNSPECIFIED:
4471 	case NL80211_IFTYPE_STATION:
4472 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
4473 		break;
4474 	case NL80211_IFTYPE_MESH_POINT:
4475 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
4476 		fallthrough;
4477 	case NL80211_IFTYPE_AP:
4478 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
4479 		break;
4480 	case NL80211_IFTYPE_MONITOR:
4481 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
4482 		break;
4483 	default:
4484 		WARN_ON(1);
4485 		break;
4486 	}
4487 
4488 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
4489 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
4490 		   ab->free_vdev_map);
4491 
4492 	vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
4493 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
4494 		vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
4495 
4496 	ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
4497 
4498 	ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
4499 	if (ret) {
4500 		ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
4501 			    arvif->vdev_id, ret);
4502 		goto err;
4503 	}
4504 
4505 	ar->num_created_vdevs++;
4506 	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
4507 		   vif->addr, arvif->vdev_id);
4508 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
4509 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
4510 
4511 	spin_lock_bh(&ar->data_lock);
4512 	list_add(&arvif->list, &ar->arvifs);
4513 	spin_unlock_bh(&ar->data_lock);
4514 
4515 	ath11k_mac_op_update_vif_offload(hw, vif);
4516 
4517 	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
4518 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4519 					    WMI_VDEV_PARAM_NSS, nss);
4520 	if (ret) {
4521 		ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
4522 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
4523 		goto err_vdev_del;
4524 	}
4525 
4526 	switch (arvif->vdev_type) {
4527 	case WMI_VDEV_TYPE_AP:
4528 		peer_param.vdev_id = arvif->vdev_id;
4529 		peer_param.peer_addr = vif->addr;
4530 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4531 		ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
4532 		if (ret) {
4533 			ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
4534 				    arvif->vdev_id, ret);
4535 			goto err_vdev_del;
4536 		}
4537 
4538 		ret = ath11k_mac_set_kickout(arvif);
4539 		if (ret) {
4540 			ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
4541 				    arvif->vdev_id, ret);
4542 			goto err_peer_del;
4543 		}
4544 		break;
4545 	case WMI_VDEV_TYPE_STA:
4546 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
4547 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4548 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4549 						  param_id, param_value);
4550 		if (ret) {
4551 			ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
4552 				    arvif->vdev_id, ret);
4553 			goto err_peer_del;
4554 		}
4555 
4556 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
4557 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
4558 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4559 						  param_id, param_value);
4560 		if (ret) {
4561 			ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
4562 				    arvif->vdev_id, ret);
4563 			goto err_peer_del;
4564 		}
4565 
4566 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
4567 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
4568 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4569 						  param_id, param_value);
4570 		if (ret) {
4571 			ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
4572 				    arvif->vdev_id, ret);
4573 			goto err_peer_del;
4574 		}
4575 
4576 		ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
4577 		if (ret) {
4578 			ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
4579 				    arvif->vdev_id, ret);
4580 			goto err_peer_del;
4581 		}
4582 		break;
4583 	default:
4584 		break;
4585 	}
4586 
4587 	arvif->txpower = vif->bss_conf.txpower;
4588 	ret = ath11k_mac_txpower_recalc(ar);
4589 	if (ret)
4590 		goto err_peer_del;
4591 
4592 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
4593 	param_value = ar->hw->wiphy->rts_threshold;
4594 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4595 					    param_id, param_value);
4596 	if (ret) {
4597 		ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
4598 			    arvif->vdev_id, ret);
4599 	}
4600 
4601 	ath11k_dp_vdev_tx_attach(ar, arvif);
4602 
4603 	mutex_unlock(&ar->conf_mutex);
4604 
4605 	return 0;
4606 
4607 err_peer_del:
4608 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4609 		reinit_completion(&ar->peer_delete_done);
4610 
4611 		ret = ath11k_wmi_send_peer_delete_cmd(ar, vif->addr,
4612 						      arvif->vdev_id);
4613 		if (ret) {
4614 			ath11k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
4615 				    arvif->vdev_id, vif->addr);
4616 			goto err;
4617 		}
4618 
4619 		ret = ath11k_wait_for_peer_delete_done(ar, arvif->vdev_id,
4620 						       vif->addr);
4621 		if (ret)
4622 			goto err;
4623 
4624 		ar->num_peers--;
4625 	}
4626 
4627 err_vdev_del:
4628 	ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
4629 	ar->num_created_vdevs--;
4630 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
4631 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
4632 	spin_lock_bh(&ar->data_lock);
4633 	list_del(&arvif->list);
4634 	spin_unlock_bh(&ar->data_lock);
4635 
4636 err:
4637 	mutex_unlock(&ar->conf_mutex);
4638 
4639 	return ret;
4640 }
4641 
ath11k_mac_vif_unref(int buf_id,void * skb,void * ctx)4642 static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
4643 {
4644 	struct ieee80211_vif *vif = (struct ieee80211_vif *)ctx;
4645 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
4646 
4647 	if (skb_cb->vif == vif)
4648 		skb_cb->vif = NULL;
4649 
4650 	return 0;
4651 }
4652 
ath11k_mac_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)4653 static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
4654 					   struct ieee80211_vif *vif)
4655 {
4656 	struct ath11k *ar = hw->priv;
4657 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4658 	struct ath11k_base *ab = ar->ab;
4659 	int ret;
4660 	int i;
4661 
4662 	mutex_lock(&ar->conf_mutex);
4663 
4664 	ath11k_dbg(ab, ATH11K_DBG_MAC, "mac remove interface (vdev %d)\n",
4665 		   arvif->vdev_id);
4666 
4667 	spin_lock_bh(&ar->data_lock);
4668 	list_del(&arvif->list);
4669 	spin_unlock_bh(&ar->data_lock);
4670 
4671 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4672 		ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
4673 		if (ret)
4674 			ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
4675 				    arvif->vdev_id, ret);
4676 	}
4677 
4678 	ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
4679 	if (ret)
4680 		ath11k_warn(ab, "failed to delete WMI vdev %d: %d\n",
4681 			    arvif->vdev_id, ret);
4682 
4683 	ar->num_created_vdevs--;
4684 	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
4685 		   vif->addr, arvif->vdev_id);
4686 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
4687 	ab->free_vdev_map |= 1LL << (arvif->vdev_id);
4688 
4689 	ath11k_peer_cleanup(ar, arvif->vdev_id);
4690 
4691 	idr_for_each(&ar->txmgmt_idr,
4692 		     ath11k_mac_vif_txmgmt_idr_remove, vif);
4693 
4694 	for (i = 0; i < DP_TCL_NUM_RING_MAX; i++) {
4695 		spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
4696 		idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
4697 			     ath11k_mac_vif_unref, vif);
4698 		spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
4699 	}
4700 
4701 	/* Recalc txpower for remaining vdev */
4702 	ath11k_mac_txpower_recalc(ar);
4703 	clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4704 
4705 	/* TODO: recal traffic pause state based on the available vdevs */
4706 
4707 	mutex_unlock(&ar->conf_mutex);
4708 }
4709 
4710 /* FIXME: Has to be verified. */
4711 #define SUPPORTED_FILTERS			\
4712 	(FIF_ALLMULTI |				\
4713 	FIF_CONTROL |				\
4714 	FIF_PSPOLL |				\
4715 	FIF_OTHER_BSS |				\
4716 	FIF_BCN_PRBRESP_PROMISC |		\
4717 	FIF_PROBE_REQ |				\
4718 	FIF_FCSFAIL)
4719 
ath11k_mac_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)4720 static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
4721 					   unsigned int changed_flags,
4722 					   unsigned int *total_flags,
4723 					   u64 multicast)
4724 {
4725 	struct ath11k *ar = hw->priv;
4726 	bool reset_flag = false;
4727 	int ret = 0;
4728 
4729 	mutex_lock(&ar->conf_mutex);
4730 
4731 	changed_flags &= SUPPORTED_FILTERS;
4732 	*total_flags &= SUPPORTED_FILTERS;
4733 	ar->filter_flags = *total_flags;
4734 
4735 	/* For monitor mode */
4736 	reset_flag = !(ar->filter_flags & FIF_BCN_PRBRESP_PROMISC);
4737 
4738 	ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, reset_flag);
4739 	if (!ret) {
4740 		if (!reset_flag)
4741 			set_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4742 		else
4743 			clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4744 	} else {
4745 		ath11k_warn(ar->ab,
4746 			    "fail to set monitor filter: %d\n", ret);
4747 	}
4748 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
4749 		   "changed_flags:0x%x, total_flags:0x%x, reset_flag:%d\n",
4750 		   changed_flags, *total_flags, reset_flag);
4751 
4752 	mutex_unlock(&ar->conf_mutex);
4753 }
4754 
ath11k_mac_op_get_antenna(struct ieee80211_hw * hw,u32 * tx_ant,u32 * rx_ant)4755 static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
4756 {
4757 	struct ath11k *ar = hw->priv;
4758 
4759 	mutex_lock(&ar->conf_mutex);
4760 
4761 	*tx_ant = ar->cfg_tx_chainmask;
4762 	*rx_ant = ar->cfg_rx_chainmask;
4763 
4764 	mutex_unlock(&ar->conf_mutex);
4765 
4766 	return 0;
4767 }
4768 
ath11k_mac_op_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)4769 static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
4770 {
4771 	struct ath11k *ar = hw->priv;
4772 	int ret;
4773 
4774 	mutex_lock(&ar->conf_mutex);
4775 	ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
4776 	mutex_unlock(&ar->conf_mutex);
4777 
4778 	return ret;
4779 }
4780 
ath11k_mac_op_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)4781 static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
4782 				      struct ieee80211_vif *vif,
4783 				      struct ieee80211_ampdu_params *params)
4784 {
4785 	struct ath11k *ar = hw->priv;
4786 	int ret = -EINVAL;
4787 
4788 	mutex_lock(&ar->conf_mutex);
4789 
4790 	switch (params->action) {
4791 	case IEEE80211_AMPDU_RX_START:
4792 		ret = ath11k_dp_rx_ampdu_start(ar, params);
4793 		break;
4794 	case IEEE80211_AMPDU_RX_STOP:
4795 		ret = ath11k_dp_rx_ampdu_stop(ar, params);
4796 		break;
4797 	case IEEE80211_AMPDU_TX_START:
4798 	case IEEE80211_AMPDU_TX_STOP_CONT:
4799 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
4800 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4801 	case IEEE80211_AMPDU_TX_OPERATIONAL:
4802 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
4803 		 * Tx aggregation requests.
4804 		 */
4805 		ret = -EOPNOTSUPP;
4806 		break;
4807 	}
4808 
4809 	mutex_unlock(&ar->conf_mutex);
4810 
4811 	return ret;
4812 }
4813 
ath11k_mac_op_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)4814 static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
4815 				     struct ieee80211_chanctx_conf *ctx)
4816 {
4817 	struct ath11k *ar = hw->priv;
4818 	struct ath11k_base *ab = ar->ab;
4819 
4820 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4821 		   "mac chanctx add freq %hu width %d ptr %pK\n",
4822 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
4823 
4824 	mutex_lock(&ar->conf_mutex);
4825 
4826 	spin_lock_bh(&ar->data_lock);
4827 	/* TODO: In case of multiple channel context, populate rx_channel from
4828 	 * Rx PPDU desc information.
4829 	 */
4830 	ar->rx_channel = ctx->def.chan;
4831 	spin_unlock_bh(&ar->data_lock);
4832 
4833 	mutex_unlock(&ar->conf_mutex);
4834 
4835 	return 0;
4836 }
4837 
ath11k_mac_op_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)4838 static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
4839 					 struct ieee80211_chanctx_conf *ctx)
4840 {
4841 	struct ath11k *ar = hw->priv;
4842 	struct ath11k_base *ab = ar->ab;
4843 
4844 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4845 		   "mac chanctx remove freq %hu width %d ptr %pK\n",
4846 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
4847 
4848 	mutex_lock(&ar->conf_mutex);
4849 
4850 	spin_lock_bh(&ar->data_lock);
4851 	/* TODO: In case of there is one more channel context left, populate
4852 	 * rx_channel with the channel of that remaining channel context.
4853 	 */
4854 	ar->rx_channel = NULL;
4855 	spin_unlock_bh(&ar->data_lock);
4856 
4857 	mutex_unlock(&ar->conf_mutex);
4858 }
4859 
ath11k_mac_vdev_setup_sync(struct ath11k * ar)4860 static inline int ath11k_mac_vdev_setup_sync(struct ath11k *ar)
4861 {
4862 	lockdep_assert_held(&ar->conf_mutex);
4863 
4864 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4865 		return -ESHUTDOWN;
4866 
4867 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
4868 					 ATH11K_VDEV_SETUP_TIMEOUT_HZ))
4869 		return -ETIMEDOUT;
4870 
4871 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
4872 }
4873 
4874 static int
ath11k_mac_vdev_start_restart(struct ath11k_vif * arvif,const struct cfg80211_chan_def * chandef,bool restart)4875 ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
4876 			      const struct cfg80211_chan_def *chandef,
4877 			      bool restart)
4878 {
4879 	struct ath11k *ar = arvif->ar;
4880 	struct ath11k_base *ab = ar->ab;
4881 	struct wmi_vdev_start_req_arg arg = {};
4882 	int he_support = arvif->vif->bss_conf.he_support;
4883 	int ret = 0;
4884 
4885 	lockdep_assert_held(&ar->conf_mutex);
4886 
4887 	reinit_completion(&ar->vdev_setup_done);
4888 
4889 	arg.vdev_id = arvif->vdev_id;
4890 	arg.dtim_period = arvif->dtim_period;
4891 	arg.bcn_intval = arvif->beacon_interval;
4892 
4893 	arg.channel.freq = chandef->chan->center_freq;
4894 	arg.channel.band_center_freq1 = chandef->center_freq1;
4895 	arg.channel.band_center_freq2 = chandef->center_freq2;
4896 	arg.channel.mode =
4897 		ath11k_phymodes[chandef->chan->band][chandef->width];
4898 
4899 	arg.channel.min_power = 0;
4900 	arg.channel.max_power = chandef->chan->max_power * 2;
4901 	arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
4902 	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
4903 
4904 	arg.pref_tx_streams = ar->num_tx_chains;
4905 	arg.pref_rx_streams = ar->num_rx_chains;
4906 
4907 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4908 		arg.ssid = arvif->u.ap.ssid;
4909 		arg.ssid_len = arvif->u.ap.ssid_len;
4910 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
4911 
4912 		/* For now allow DFS for AP mode */
4913 		arg.channel.chan_radar =
4914 			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
4915 
4916 		arg.channel.passive = arg.channel.chan_radar;
4917 
4918 		spin_lock_bh(&ab->base_lock);
4919 		arg.regdomain = ar->ab->dfs_region;
4920 		spin_unlock_bh(&ab->base_lock);
4921 
4922 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
4923 		if (he_support) {
4924 			ret = ath11k_set_he_mu_sounding_mode(ar, arvif);
4925 			if (ret) {
4926 				ath11k_warn(ar->ab, "failed to set he mode vdev %i\n",
4927 					    arg.vdev_id);
4928 				return ret;
4929 			}
4930 		}
4931 	}
4932 
4933 	arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
4934 
4935 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4936 		   "mac vdev %d start center_freq %d phymode %s\n",
4937 		   arg.vdev_id, arg.channel.freq,
4938 		   ath11k_wmi_phymode_str(arg.channel.mode));
4939 
4940 	ret = ath11k_wmi_vdev_start(ar, &arg, restart);
4941 	if (ret) {
4942 		ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
4943 			    restart ? "restart" : "start", arg.vdev_id);
4944 		return ret;
4945 	}
4946 
4947 	ret = ath11k_mac_vdev_setup_sync(ar);
4948 	if (ret) {
4949 		ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
4950 			    arg.vdev_id, restart ? "restart" : "start", ret);
4951 		return ret;
4952 	}
4953 
4954 	ar->num_started_vdevs++;
4955 	ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
4956 		   arvif->vif->addr, arvif->vdev_id);
4957 
4958 	/* Enable CAC Flag in the driver by checking the channel DFS cac time,
4959 	 * i.e dfs_cac_ms value which will be valid only for radar channels
4960 	 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
4961 	 * done before channel usage. This flags is used to drop rx packets.
4962 	 * during CAC.
4963 	 */
4964 	/* TODO Set the flag for other interface types as required */
4965 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
4966 	    chandef->chan->dfs_cac_ms &&
4967 	    chandef->chan->dfs_state == NL80211_DFS_USABLE) {
4968 		set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
4969 		ath11k_dbg(ab, ATH11K_DBG_MAC,
4970 			   "CAC Started in chan_freq %d for vdev %d\n",
4971 			   arg.channel.freq, arg.vdev_id);
4972 	}
4973 
4974 	ret = ath11k_mac_set_txbf_conf(arvif);
4975 	if (ret)
4976 		ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
4977 			    arvif->vdev_id, ret);
4978 
4979 	return 0;
4980 }
4981 
ath11k_mac_vdev_stop(struct ath11k_vif * arvif)4982 static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
4983 {
4984 	struct ath11k *ar = arvif->ar;
4985 	int ret;
4986 
4987 	lockdep_assert_held(&ar->conf_mutex);
4988 
4989 	reinit_completion(&ar->vdev_setup_done);
4990 
4991 	spin_lock_bh(&ar->data_lock);
4992 
4993 	ar->vdev_stop_status.stop_in_progress = true;
4994 	ar->vdev_stop_status.vdev_id = arvif->vdev_id;
4995 
4996 	spin_unlock_bh(&ar->data_lock);
4997 
4998 	ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
4999 	if (ret) {
5000 		ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
5001 			    arvif->vdev_id, ret);
5002 		goto err;
5003 	}
5004 
5005 	ret = ath11k_mac_vdev_setup_sync(ar);
5006 	if (ret) {
5007 		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
5008 			    arvif->vdev_id, ret);
5009 		goto err;
5010 	}
5011 
5012 	WARN_ON(ar->num_started_vdevs == 0);
5013 
5014 	ar->num_started_vdevs--;
5015 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
5016 		   arvif->vif->addr, arvif->vdev_id);
5017 
5018 	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
5019 		clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
5020 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
5021 			   arvif->vdev_id);
5022 	}
5023 
5024 	return 0;
5025 err:
5026 	spin_lock_bh(&ar->data_lock);
5027 	ar->vdev_stop_status.stop_in_progress = false;
5028 	spin_unlock_bh(&ar->data_lock);
5029 
5030 	return ret;
5031 }
5032 
ath11k_mac_vdev_start(struct ath11k_vif * arvif,const struct cfg80211_chan_def * chandef)5033 static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
5034 				 const struct cfg80211_chan_def *chandef)
5035 {
5036 	return ath11k_mac_vdev_start_restart(arvif, chandef, false);
5037 }
5038 
ath11k_mac_vdev_restart(struct ath11k_vif * arvif,const struct cfg80211_chan_def * chandef)5039 static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
5040 				   const struct cfg80211_chan_def *chandef)
5041 {
5042 	return ath11k_mac_vdev_start_restart(arvif, chandef, true);
5043 }
5044 
5045 struct ath11k_mac_change_chanctx_arg {
5046 	struct ieee80211_chanctx_conf *ctx;
5047 	struct ieee80211_vif_chanctx_switch *vifs;
5048 	int n_vifs;
5049 	int next_vif;
5050 };
5051 
5052 static void
ath11k_mac_change_chanctx_cnt_iter(void * data,u8 * mac,struct ieee80211_vif * vif)5053 ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
5054 				   struct ieee80211_vif *vif)
5055 {
5056 	struct ath11k_mac_change_chanctx_arg *arg = data;
5057 
5058 	if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
5059 		return;
5060 
5061 	arg->n_vifs++;
5062 }
5063 
5064 static void
ath11k_mac_change_chanctx_fill_iter(void * data,u8 * mac,struct ieee80211_vif * vif)5065 ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
5066 				    struct ieee80211_vif *vif)
5067 {
5068 	struct ath11k_mac_change_chanctx_arg *arg = data;
5069 	struct ieee80211_chanctx_conf *ctx;
5070 
5071 	ctx = rcu_access_pointer(vif->chanctx_conf);
5072 	if (ctx != arg->ctx)
5073 		return;
5074 
5075 	if (WARN_ON(arg->next_vif == arg->n_vifs))
5076 		return;
5077 
5078 	arg->vifs[arg->next_vif].vif = vif;
5079 	arg->vifs[arg->next_vif].old_ctx = ctx;
5080 	arg->vifs[arg->next_vif].new_ctx = ctx;
5081 	arg->next_vif++;
5082 }
5083 
5084 static void
ath11k_mac_update_vif_chan(struct ath11k * ar,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs)5085 ath11k_mac_update_vif_chan(struct ath11k *ar,
5086 			   struct ieee80211_vif_chanctx_switch *vifs,
5087 			   int n_vifs)
5088 {
5089 	struct ath11k_base *ab = ar->ab;
5090 	struct ath11k_vif *arvif;
5091 	int ret;
5092 	int i;
5093 
5094 	lockdep_assert_held(&ar->conf_mutex);
5095 
5096 	for (i = 0; i < n_vifs; i++) {
5097 		arvif = (void *)vifs[i].vif->drv_priv;
5098 
5099 		ath11k_dbg(ab, ATH11K_DBG_MAC,
5100 			   "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
5101 			   arvif->vdev_id,
5102 			   vifs[i].old_ctx->def.chan->center_freq,
5103 			   vifs[i].new_ctx->def.chan->center_freq,
5104 			   vifs[i].old_ctx->def.width,
5105 			   vifs[i].new_ctx->def.width);
5106 
5107 		if (WARN_ON(!arvif->is_started))
5108 			continue;
5109 
5110 		if (WARN_ON(!arvif->is_up))
5111 			continue;
5112 
5113 		ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
5114 		if (ret) {
5115 			ath11k_warn(ab, "failed to down vdev %d: %d\n",
5116 				    arvif->vdev_id, ret);
5117 			continue;
5118 		}
5119 	}
5120 
5121 	/* All relevant vdevs are downed and associated channel resources
5122 	 * should be available for the channel switch now.
5123 	 */
5124 
5125 	/* TODO: Update ar->rx_channel */
5126 
5127 	for (i = 0; i < n_vifs; i++) {
5128 		arvif = (void *)vifs[i].vif->drv_priv;
5129 
5130 		if (WARN_ON(!arvif->is_started))
5131 			continue;
5132 
5133 		if (WARN_ON(!arvif->is_up))
5134 			continue;
5135 
5136 		ret = ath11k_mac_vdev_restart(arvif, &vifs[i].new_ctx->def);
5137 		if (ret) {
5138 			ath11k_warn(ab, "failed to restart vdev %d: %d\n",
5139 				    arvif->vdev_id, ret);
5140 			continue;
5141 		}
5142 
5143 		ret = ath11k_mac_setup_bcn_tmpl(arvif);
5144 		if (ret)
5145 			ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
5146 				    ret);
5147 
5148 		ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
5149 					 arvif->bssid);
5150 		if (ret) {
5151 			ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
5152 				    arvif->vdev_id, ret);
5153 			continue;
5154 		}
5155 	}
5156 }
5157 
5158 static void
ath11k_mac_update_active_vif_chan(struct ath11k * ar,struct ieee80211_chanctx_conf * ctx)5159 ath11k_mac_update_active_vif_chan(struct ath11k *ar,
5160 				  struct ieee80211_chanctx_conf *ctx)
5161 {
5162 	struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
5163 
5164 	lockdep_assert_held(&ar->conf_mutex);
5165 
5166 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
5167 						   IEEE80211_IFACE_ITER_NORMAL,
5168 						   ath11k_mac_change_chanctx_cnt_iter,
5169 						   &arg);
5170 	if (arg.n_vifs == 0)
5171 		return;
5172 
5173 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
5174 	if (!arg.vifs)
5175 		return;
5176 
5177 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
5178 						   IEEE80211_IFACE_ITER_NORMAL,
5179 						   ath11k_mac_change_chanctx_fill_iter,
5180 						   &arg);
5181 
5182 	ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
5183 
5184 	kfree(arg.vifs);
5185 }
5186 
ath11k_mac_op_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)5187 static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
5188 					 struct ieee80211_chanctx_conf *ctx,
5189 					 u32 changed)
5190 {
5191 	struct ath11k *ar = hw->priv;
5192 	struct ath11k_base *ab = ar->ab;
5193 
5194 	mutex_lock(&ar->conf_mutex);
5195 
5196 	ath11k_dbg(ab, ATH11K_DBG_MAC,
5197 		   "mac chanctx change freq %hu width %d ptr %pK changed %x\n",
5198 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
5199 
5200 	/* This shouldn't really happen because channel switching should use
5201 	 * switch_vif_chanctx().
5202 	 */
5203 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
5204 		goto unlock;
5205 
5206 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH)
5207 		ath11k_mac_update_active_vif_chan(ar, ctx);
5208 
5209 	/* TODO: Recalc radar detection */
5210 
5211 unlock:
5212 	mutex_unlock(&ar->conf_mutex);
5213 }
5214 
ath11k_start_vdev_delay(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5215 static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
5216 				   struct ieee80211_vif *vif)
5217 {
5218 	struct ath11k *ar = hw->priv;
5219 	struct ath11k_base *ab = ar->ab;
5220 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5221 	int ret;
5222 
5223 	if (WARN_ON(arvif->is_started))
5224 		return -EBUSY;
5225 
5226 	ret = ath11k_mac_vdev_start(arvif, &arvif->chanctx.def);
5227 	if (ret) {
5228 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
5229 			    arvif->vdev_id, vif->addr,
5230 			    arvif->chanctx.def.chan->center_freq, ret);
5231 		return ret;
5232 	}
5233 
5234 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5235 		ret = ath11k_monitor_vdev_up(ar, arvif->vdev_id);
5236 		if (ret) {
5237 			ath11k_warn(ab, "failed put monitor up: %d\n", ret);
5238 			return ret;
5239 		}
5240 	}
5241 
5242 	arvif->is_started = true;
5243 
5244 	/* TODO: Setup ps and cts/rts protection */
5245 	return 0;
5246 }
5247 
5248 static int
ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_chanctx_conf * ctx)5249 ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
5250 				 struct ieee80211_vif *vif,
5251 				 struct ieee80211_chanctx_conf *ctx)
5252 {
5253 	struct ath11k *ar = hw->priv;
5254 	struct ath11k_base *ab = ar->ab;
5255 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5256 	int ret;
5257 	struct peer_create_params param;
5258 
5259 	mutex_lock(&ar->conf_mutex);
5260 
5261 	ath11k_dbg(ab, ATH11K_DBG_MAC,
5262 		   "mac chanctx assign ptr %pK vdev_id %i\n",
5263 		   ctx, arvif->vdev_id);
5264 
5265 	/* for QCA6390 bss peer must be created before vdev_start */
5266 	if (ab->hw_params.vdev_start_delay &&
5267 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
5268 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR &&
5269 	    !ath11k_peer_find_by_vdev_id(ab, arvif->vdev_id)) {
5270 		memcpy(&arvif->chanctx, ctx, sizeof(*ctx));
5271 		ret = 0;
5272 		goto out;
5273 	}
5274 
5275 	if (WARN_ON(arvif->is_started)) {
5276 		ret = -EBUSY;
5277 		goto out;
5278 	}
5279 
5280 	if (ab->hw_params.vdev_start_delay &&
5281 	    arvif->vdev_type != WMI_VDEV_TYPE_AP &&
5282 	    arvif->vdev_type != WMI_VDEV_TYPE_MONITOR) {
5283 		param.vdev_id = arvif->vdev_id;
5284 		param.peer_type = WMI_PEER_TYPE_DEFAULT;
5285 		param.peer_addr = ar->mac_addr;
5286 
5287 		ret = ath11k_peer_create(ar, arvif, NULL, &param);
5288 		if (ret) {
5289 			ath11k_warn(ab, "failed to create peer after vdev start delay: %d",
5290 				    ret);
5291 			goto out;
5292 		}
5293 	}
5294 
5295 	ret = ath11k_mac_vdev_start(arvif, &ctx->def);
5296 	if (ret) {
5297 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
5298 			    arvif->vdev_id, vif->addr,
5299 			    ctx->def.chan->center_freq, ret);
5300 		goto out;
5301 	}
5302 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
5303 		ret = ath11k_monitor_vdev_up(ar, arvif->vdev_id);
5304 		if (ret)
5305 			goto out;
5306 	}
5307 
5308 	arvif->is_started = true;
5309 
5310 	/* TODO: Setup ps and cts/rts protection */
5311 
5312 	ret = 0;
5313 
5314 out:
5315 	mutex_unlock(&ar->conf_mutex);
5316 
5317 	return ret;
5318 }
5319 
5320 static void
ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_chanctx_conf * ctx)5321 ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
5322 				   struct ieee80211_vif *vif,
5323 				   struct ieee80211_chanctx_conf *ctx)
5324 {
5325 	struct ath11k *ar = hw->priv;
5326 	struct ath11k_base *ab = ar->ab;
5327 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5328 	int ret;
5329 
5330 	mutex_lock(&ar->conf_mutex);
5331 
5332 	ath11k_dbg(ab, ATH11K_DBG_MAC,
5333 		   "mac chanctx unassign ptr %pK vdev_id %i\n",
5334 		   ctx, arvif->vdev_id);
5335 
5336 	WARN_ON(!arvif->is_started);
5337 
5338 	if (ab->hw_params.vdev_start_delay &&
5339 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR &&
5340 	    ath11k_peer_find_by_addr(ab, ar->mac_addr))
5341 		ath11k_peer_delete(ar, arvif->vdev_id, ar->mac_addr);
5342 
5343 	ret = ath11k_mac_vdev_stop(arvif);
5344 	if (ret)
5345 		ath11k_warn(ab, "failed to stop vdev %i: %d\n",
5346 			    arvif->vdev_id, ret);
5347 
5348 	arvif->is_started = false;
5349 
5350 	if (ab->hw_params.vdev_start_delay &&
5351 	    arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
5352 		ath11k_wmi_vdev_down(ar, arvif->vdev_id);
5353 
5354 	mutex_unlock(&ar->conf_mutex);
5355 }
5356 
5357 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)5358 ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
5359 				 struct ieee80211_vif_chanctx_switch *vifs,
5360 				 int n_vifs,
5361 				 enum ieee80211_chanctx_switch_mode mode)
5362 {
5363 	struct ath11k *ar = hw->priv;
5364 
5365 	mutex_lock(&ar->conf_mutex);
5366 
5367 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5368 		   "mac chanctx switch n_vifs %d mode %d\n",
5369 		   n_vifs, mode);
5370 	ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
5371 
5372 	mutex_unlock(&ar->conf_mutex);
5373 
5374 	return 0;
5375 }
5376 
5377 static int
ath11k_set_vdev_param_to_all_vifs(struct ath11k * ar,int param,u32 value)5378 ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
5379 {
5380 	struct ath11k_vif *arvif;
5381 	int ret = 0;
5382 
5383 	mutex_lock(&ar->conf_mutex);
5384 	list_for_each_entry(arvif, &ar->arvifs, list) {
5385 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
5386 			   param, arvif->vdev_id, value);
5387 
5388 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5389 						    param, value);
5390 		if (ret) {
5391 			ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
5392 				    param, arvif->vdev_id, ret);
5393 			break;
5394 		}
5395 	}
5396 	mutex_unlock(&ar->conf_mutex);
5397 	return ret;
5398 }
5399 
5400 /* mac80211 stores device specific RTS/Fragmentation threshold value,
5401  * this is set interface specific to firmware from ath11k driver
5402  */
ath11k_mac_op_set_rts_threshold(struct ieee80211_hw * hw,u32 value)5403 static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5404 {
5405 	struct ath11k *ar = hw->priv;
5406 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
5407 
5408 	return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
5409 }
5410 
ath11k_mac_op_set_frag_threshold(struct ieee80211_hw * hw,u32 value)5411 static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
5412 {
5413 	/* Even though there's a WMI vdev param for fragmentation threshold no
5414 	 * known firmware actually implements it. Moreover it is not possible to
5415 	 * rely frame fragmentation to mac80211 because firmware clears the
5416 	 * "more fragments" bit in frame control making it impossible for remote
5417 	 * devices to reassemble frames.
5418 	 *
5419 	 * Hence implement a dummy callback just to say fragmentation isn't
5420 	 * supported. This effectively prevents mac80211 from doing frame
5421 	 * fragmentation in software.
5422 	 */
5423 	return -EOPNOTSUPP;
5424 }
5425 
ath11k_mac_op_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)5426 static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5427 				u32 queues, bool drop)
5428 {
5429 	struct ath11k *ar = hw->priv;
5430 	long time_left;
5431 
5432 	if (drop)
5433 		return;
5434 
5435 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
5436 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
5437 				       ATH11K_FLUSH_TIMEOUT);
5438 	if (time_left == 0)
5439 		ath11k_warn(ar->ab, "failed to flush transmit queue %ld\n", time_left);
5440 }
5441 
5442 static int
ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)5443 ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
5444 				     enum nl80211_band band,
5445 				     const struct cfg80211_bitrate_mask *mask)
5446 {
5447 	int num_rates = 0;
5448 	int i;
5449 
5450 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
5451 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
5452 
5453 	return num_rates;
5454 }
5455 
5456 static bool
ath11k_mac_has_single_legacy_rate(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)5457 ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
5458 				  enum nl80211_band band,
5459 				  const struct cfg80211_bitrate_mask *mask)
5460 {
5461 	int num_rates = 0;
5462 
5463 	num_rates = hweight32(mask->control[band].legacy);
5464 
5465 	if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
5466 		return false;
5467 
5468 	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
5469 		return false;
5470 
5471 	return num_rates == 1;
5472 }
5473 
5474 static bool
ath11k_mac_bitrate_mask_get_single_nss(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask,int * nss)5475 ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
5476 				       enum nl80211_band band,
5477 				       const struct cfg80211_bitrate_mask *mask,
5478 				       int *nss)
5479 {
5480 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
5481 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5482 	u8 ht_nss_mask = 0;
5483 	u8 vht_nss_mask = 0;
5484 	int i;
5485 
5486 	/* No need to consider legacy here. Basic rates are always present
5487 	 * in bitrate mask
5488 	 */
5489 
5490 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
5491 		if (mask->control[band].ht_mcs[i] == 0)
5492 			continue;
5493 		else if (mask->control[band].ht_mcs[i] ==
5494 			 sband->ht_cap.mcs.rx_mask[i])
5495 			ht_nss_mask |= BIT(i);
5496 		else
5497 			return false;
5498 	}
5499 
5500 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
5501 		if (mask->control[band].vht_mcs[i] == 0)
5502 			continue;
5503 		else if (mask->control[band].vht_mcs[i] ==
5504 			 ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
5505 			vht_nss_mask |= BIT(i);
5506 		else
5507 			return false;
5508 	}
5509 
5510 	if (ht_nss_mask != vht_nss_mask)
5511 		return false;
5512 
5513 	if (ht_nss_mask == 0)
5514 		return false;
5515 
5516 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
5517 		return false;
5518 
5519 	*nss = fls(ht_nss_mask);
5520 
5521 	return true;
5522 }
5523 
5524 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)5525 ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
5526 				  enum nl80211_band band,
5527 				  const struct cfg80211_bitrate_mask *mask,
5528 				  u32 *rate, u8 *nss)
5529 {
5530 	int rate_idx;
5531 	u16 bitrate;
5532 	u8 preamble;
5533 	u8 hw_rate;
5534 
5535 	if (hweight32(mask->control[band].legacy) != 1)
5536 		return -EINVAL;
5537 
5538 	rate_idx = ffs(mask->control[band].legacy) - 1;
5539 
5540 	if (band == NL80211_BAND_5GHZ || band == NL80211_BAND_6GHZ)
5541 		rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
5542 
5543 	hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
5544 	bitrate = ath11k_legacy_rates[rate_idx].bitrate;
5545 
5546 	if (ath11k_mac_bitrate_is_cck(bitrate))
5547 		preamble = WMI_RATE_PREAMBLE_CCK;
5548 	else
5549 		preamble = WMI_RATE_PREAMBLE_OFDM;
5550 
5551 	*nss = 1;
5552 	*rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
5553 
5554 	return 0;
5555 }
5556 
ath11k_mac_set_fixed_rate_params(struct ath11k_vif * arvif,u32 rate,u8 nss,u8 sgi,u8 ldpc)5557 static int ath11k_mac_set_fixed_rate_params(struct ath11k_vif *arvif,
5558 					    u32 rate, u8 nss, u8 sgi, u8 ldpc)
5559 {
5560 	struct ath11k *ar = arvif->ar;
5561 	u32 vdev_param;
5562 	int ret;
5563 
5564 	lockdep_assert_held(&ar->conf_mutex);
5565 
5566 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
5567 		   arvif->vdev_id, rate, nss, sgi);
5568 
5569 	vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
5570 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5571 					    vdev_param, rate);
5572 	if (ret) {
5573 		ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
5574 			    rate, ret);
5575 		return ret;
5576 	}
5577 
5578 	vdev_param = WMI_VDEV_PARAM_NSS;
5579 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5580 					    vdev_param, nss);
5581 	if (ret) {
5582 		ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
5583 			    nss, ret);
5584 		return ret;
5585 	}
5586 
5587 	vdev_param = WMI_VDEV_PARAM_SGI;
5588 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5589 					    vdev_param, sgi);
5590 	if (ret) {
5591 		ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
5592 			    sgi, ret);
5593 		return ret;
5594 	}
5595 
5596 	vdev_param = WMI_VDEV_PARAM_LDPC;
5597 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5598 					    vdev_param, ldpc);
5599 	if (ret) {
5600 		ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
5601 			    ldpc, ret);
5602 		return ret;
5603 	}
5604 
5605 	return 0;
5606 }
5607 
5608 static bool
ath11k_mac_vht_mcs_range_present(struct ath11k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask)5609 ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
5610 				 enum nl80211_band band,
5611 				 const struct cfg80211_bitrate_mask *mask)
5612 {
5613 	int i;
5614 	u16 vht_mcs;
5615 
5616 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5617 		vht_mcs = mask->control[band].vht_mcs[i];
5618 
5619 		switch (vht_mcs) {
5620 		case 0:
5621 		case BIT(8) - 1:
5622 		case BIT(9) - 1:
5623 		case BIT(10) - 1:
5624 			break;
5625 		default:
5626 			return false;
5627 		}
5628 	}
5629 
5630 	return true;
5631 }
5632 
ath11k_mac_set_bitrate_mask_iter(void * data,struct ieee80211_sta * sta)5633 static void ath11k_mac_set_bitrate_mask_iter(void *data,
5634 					     struct ieee80211_sta *sta)
5635 {
5636 	struct ath11k_vif *arvif = data;
5637 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5638 	struct ath11k *ar = arvif->ar;
5639 
5640 	spin_lock_bh(&ar->data_lock);
5641 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
5642 	spin_unlock_bh(&ar->data_lock);
5643 
5644 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
5645 }
5646 
ath11k_mac_disable_peer_fixed_rate(void * data,struct ieee80211_sta * sta)5647 static void ath11k_mac_disable_peer_fixed_rate(void *data,
5648 					       struct ieee80211_sta *sta)
5649 {
5650 	struct ath11k_vif *arvif = data;
5651 	struct ath11k *ar = arvif->ar;
5652 	int ret;
5653 
5654 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
5655 					arvif->vdev_id,
5656 					WMI_PEER_PARAM_FIXED_RATE,
5657 					WMI_FIXED_RATE_NONE);
5658 	if (ret)
5659 		ath11k_warn(ar->ab,
5660 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
5661 			    sta->addr, ret);
5662 }
5663 
5664 static int
ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)5665 ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
5666 			       struct ieee80211_vif *vif,
5667 			       const struct cfg80211_bitrate_mask *mask)
5668 {
5669 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5670 	struct cfg80211_chan_def def;
5671 	struct ath11k *ar = arvif->ar;
5672 	enum nl80211_band band;
5673 	const u8 *ht_mcs_mask;
5674 	const u16 *vht_mcs_mask;
5675 	u32 rate;
5676 	u8 nss;
5677 	u8 sgi;
5678 	u8 ldpc;
5679 	int single_nss;
5680 	int ret;
5681 	int num_rates;
5682 
5683 	if (ath11k_mac_vif_chan(vif, &def))
5684 		return -EPERM;
5685 
5686 	band = def.chan->band;
5687 	ht_mcs_mask = mask->control[band].ht_mcs;
5688 	vht_mcs_mask = mask->control[band].vht_mcs;
5689 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
5690 
5691 	sgi = mask->control[band].gi;
5692 	if (sgi == NL80211_TXRATE_FORCE_LGI)
5693 		return -EINVAL;
5694 
5695 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
5696 	 * requires passing atleast one of used basic rates along with them.
5697 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
5698 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
5699 	 * suitable for setting single HT/VHT rates.
5700 	 * But, there could be a single basic rate passed from userspace which
5701 	 * can be done through the FIXED_RATE param.
5702 	 */
5703 	if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
5704 		ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
5705 							&nss);
5706 		if (ret) {
5707 			ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
5708 				    arvif->vdev_id, ret);
5709 			return ret;
5710 		}
5711 		ieee80211_iterate_stations_atomic(ar->hw,
5712 						  ath11k_mac_disable_peer_fixed_rate,
5713 						  arvif);
5714 	} else if (ath11k_mac_bitrate_mask_get_single_nss(ar, band, mask,
5715 							  &single_nss)) {
5716 		rate = WMI_FIXED_RATE_NONE;
5717 		nss = single_nss;
5718 	} else {
5719 		rate = WMI_FIXED_RATE_NONE;
5720 		nss = min_t(u32, ar->num_tx_chains,
5721 			    max(ath11k_mac_max_ht_nss(ht_mcs_mask),
5722 				ath11k_mac_max_vht_nss(vht_mcs_mask)));
5723 
5724 		/* If multiple rates across different preambles are given
5725 		 * we can reconfigure this info with all peers using PEER_ASSOC
5726 		 * command with the below exception cases.
5727 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
5728 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
5729 		 * mandates passing basic rates along with HT/VHT rates, FW
5730 		 * doesn't allow switching from VHT to Legacy. Hence instead of
5731 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
5732 		 * we could set this VHT rate as peer fixed rate param, which
5733 		 * will override FIXED rate and FW rate control algorithm.
5734 		 * If single VHT rate is passed along with HT rates, we select
5735 		 * the VHT rate as fixed rate for vht peers.
5736 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
5737 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
5738 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
5739 		 * RATEMASK_CMDID can cover all use cases of setting rates
5740 		 * across multiple preambles and rates within same type.
5741 		 * But requires more validation of the command at this point.
5742 		 */
5743 
5744 		num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
5745 								  mask);
5746 
5747 		if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
5748 		    num_rates > 1) {
5749 			/* TODO: Handle multiple VHT MCS values setting using
5750 			 * RATEMASK CMD
5751 			 */
5752 			ath11k_warn(ar->ab,
5753 				    "Setting more than one MCS Value in bitrate mask not supported\n");
5754 			return -EINVAL;
5755 		}
5756 
5757 		ieee80211_iterate_stations_atomic(ar->hw,
5758 						  ath11k_mac_disable_peer_fixed_rate,
5759 						  arvif);
5760 
5761 		mutex_lock(&ar->conf_mutex);
5762 
5763 		arvif->bitrate_mask = *mask;
5764 		ieee80211_iterate_stations_atomic(ar->hw,
5765 						  ath11k_mac_set_bitrate_mask_iter,
5766 						  arvif);
5767 
5768 		mutex_unlock(&ar->conf_mutex);
5769 	}
5770 
5771 	mutex_lock(&ar->conf_mutex);
5772 
5773 	ret = ath11k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
5774 	if (ret) {
5775 		ath11k_warn(ar->ab, "failed to set fixed rate params on vdev %i: %d\n",
5776 			    arvif->vdev_id, ret);
5777 	}
5778 
5779 	mutex_unlock(&ar->conf_mutex);
5780 
5781 	return ret;
5782 }
5783 
5784 static void
ath11k_mac_op_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)5785 ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
5786 				enum ieee80211_reconfig_type reconfig_type)
5787 {
5788 	struct ath11k *ar = hw->priv;
5789 
5790 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
5791 		return;
5792 
5793 	mutex_lock(&ar->conf_mutex);
5794 
5795 	if (ar->state == ATH11K_STATE_RESTARTED) {
5796 		ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
5797 			    ar->pdev->pdev_id);
5798 		ar->state = ATH11K_STATE_ON;
5799 		ieee80211_wake_queues(ar->hw);
5800 	}
5801 
5802 	mutex_unlock(&ar->conf_mutex);
5803 }
5804 
5805 static void
ath11k_mac_update_bss_chan_survey(struct ath11k * ar,struct ieee80211_channel * channel)5806 ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
5807 				  struct ieee80211_channel *channel)
5808 {
5809 	int ret;
5810 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
5811 
5812 	lockdep_assert_held(&ar->conf_mutex);
5813 
5814 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
5815 	    ar->rx_channel != channel)
5816 		return;
5817 
5818 	if (ar->scan.state != ATH11K_SCAN_IDLE) {
5819 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5820 			   "ignoring bss chan info req while scanning..\n");
5821 		return;
5822 	}
5823 
5824 	reinit_completion(&ar->bss_survey_done);
5825 
5826 	ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
5827 	if (ret) {
5828 		ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
5829 		return;
5830 	}
5831 
5832 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
5833 	if (ret == 0)
5834 		ath11k_warn(ar->ab, "bss channel survey timed out\n");
5835 }
5836 
ath11k_mac_op_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)5837 static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
5838 				    struct survey_info *survey)
5839 {
5840 	struct ath11k *ar = hw->priv;
5841 	struct ieee80211_supported_band *sband;
5842 	struct survey_info *ar_survey;
5843 	int ret = 0;
5844 
5845 	if (idx >= ATH11K_NUM_CHANS)
5846 		return -ENOENT;
5847 
5848 	ar_survey = &ar->survey[idx];
5849 
5850 	mutex_lock(&ar->conf_mutex);
5851 
5852 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
5853 	if (sband && idx >= sband->n_channels) {
5854 		idx -= sband->n_channels;
5855 		sband = NULL;
5856 	}
5857 
5858 	if (!sband)
5859 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
5860 
5861 	if (!sband || idx >= sband->n_channels) {
5862 		ret = -ENOENT;
5863 		goto exit;
5864 	}
5865 
5866 	ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
5867 
5868 	spin_lock_bh(&ar->data_lock);
5869 	memcpy(survey, ar_survey, sizeof(*survey));
5870 	spin_unlock_bh(&ar->data_lock);
5871 
5872 	survey->channel = &sband->channels[idx];
5873 
5874 	if (ar->rx_channel == survey->channel)
5875 		survey->filled |= SURVEY_INFO_IN_USE;
5876 
5877 exit:
5878 	mutex_unlock(&ar->conf_mutex);
5879 	return ret;
5880 }
5881 
ath11k_mac_op_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)5882 static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
5883 					 struct ieee80211_vif *vif,
5884 					 struct ieee80211_sta *sta,
5885 					 struct station_info *sinfo)
5886 {
5887 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5888 
5889 	sinfo->rx_duration = arsta->rx_duration;
5890 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
5891 
5892 	sinfo->tx_duration = arsta->tx_duration;
5893 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
5894 
5895 	if (!arsta->txrate.legacy && !arsta->txrate.nss)
5896 		return;
5897 
5898 	if (arsta->txrate.legacy) {
5899 		sinfo->txrate.legacy = arsta->txrate.legacy;
5900 	} else {
5901 		sinfo->txrate.mcs = arsta->txrate.mcs;
5902 		sinfo->txrate.nss = arsta->txrate.nss;
5903 		sinfo->txrate.bw = arsta->txrate.bw;
5904 		sinfo->txrate.he_gi = arsta->txrate.he_gi;
5905 		sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
5906 		sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
5907 	}
5908 	sinfo->txrate.flags = arsta->txrate.flags;
5909 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5910 
5911 	/* TODO: Use real NF instead of default one. */
5912 	sinfo->signal = arsta->rssi_comb + ATH11K_DEFAULT_NOISE_FLOOR;
5913 }
5914 
5915 static const struct ieee80211_ops ath11k_ops = {
5916 	.tx				= ath11k_mac_op_tx,
5917 	.start                          = ath11k_mac_op_start,
5918 	.stop                           = ath11k_mac_op_stop,
5919 	.reconfig_complete              = ath11k_mac_op_reconfig_complete,
5920 	.add_interface                  = ath11k_mac_op_add_interface,
5921 	.remove_interface		= ath11k_mac_op_remove_interface,
5922 	.update_vif_offload		= ath11k_mac_op_update_vif_offload,
5923 	.config                         = ath11k_mac_op_config,
5924 	.bss_info_changed               = ath11k_mac_op_bss_info_changed,
5925 	.configure_filter		= ath11k_mac_op_configure_filter,
5926 	.hw_scan                        = ath11k_mac_op_hw_scan,
5927 	.cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
5928 	.set_key                        = ath11k_mac_op_set_key,
5929 	.sta_state                      = ath11k_mac_op_sta_state,
5930 	.sta_set_txpwr			= ath11k_mac_op_sta_set_txpwr,
5931 	.sta_rc_update			= ath11k_mac_op_sta_rc_update,
5932 	.conf_tx                        = ath11k_mac_op_conf_tx,
5933 	.set_antenna			= ath11k_mac_op_set_antenna,
5934 	.get_antenna			= ath11k_mac_op_get_antenna,
5935 	.ampdu_action			= ath11k_mac_op_ampdu_action,
5936 	.add_chanctx			= ath11k_mac_op_add_chanctx,
5937 	.remove_chanctx			= ath11k_mac_op_remove_chanctx,
5938 	.change_chanctx			= ath11k_mac_op_change_chanctx,
5939 	.assign_vif_chanctx		= ath11k_mac_op_assign_vif_chanctx,
5940 	.unassign_vif_chanctx		= ath11k_mac_op_unassign_vif_chanctx,
5941 	.switch_vif_chanctx		= ath11k_mac_op_switch_vif_chanctx,
5942 	.set_rts_threshold		= ath11k_mac_op_set_rts_threshold,
5943 	.set_frag_threshold		= ath11k_mac_op_set_frag_threshold,
5944 	.set_bitrate_mask		= ath11k_mac_op_set_bitrate_mask,
5945 	.get_survey			= ath11k_mac_op_get_survey,
5946 	.flush				= ath11k_mac_op_flush,
5947 	.sta_statistics			= ath11k_mac_op_sta_statistics,
5948 	CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
5949 #ifdef CONFIG_ATH11K_DEBUGFS
5950 	.sta_add_debugfs		= ath11k_debugfs_sta_op_add,
5951 #endif
5952 };
5953 
ath11k_mac_update_ch_list(struct ath11k * ar,struct ieee80211_supported_band * band,u32 freq_low,u32 freq_high)5954 static void ath11k_mac_update_ch_list(struct ath11k *ar,
5955 				      struct ieee80211_supported_band *band,
5956 				      u32 freq_low, u32 freq_high)
5957 {
5958 	int i;
5959 
5960 	if (!(freq_low && freq_high))
5961 		return;
5962 
5963 	for (i = 0; i < band->n_channels; i++) {
5964 		if (band->channels[i].center_freq < freq_low ||
5965 		    band->channels[i].center_freq > freq_high)
5966 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
5967 	}
5968 }
5969 
ath11k_get_phy_id(struct ath11k * ar,u32 band)5970 static u32 ath11k_get_phy_id(struct ath11k *ar, u32 band)
5971 {
5972 	struct ath11k_pdev *pdev = ar->pdev;
5973 	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
5974 
5975 	if (band == WMI_HOST_WLAN_2G_CAP)
5976 		return pdev_cap->band[NL80211_BAND_2GHZ].phy_id;
5977 
5978 	if (band == WMI_HOST_WLAN_5G_CAP)
5979 		return pdev_cap->band[NL80211_BAND_5GHZ].phy_id;
5980 
5981 	ath11k_warn(ar->ab, "unsupported phy cap:%d\n", band);
5982 
5983 	return 0;
5984 }
5985 
ath11k_mac_setup_channels_rates(struct ath11k * ar,u32 supported_bands)5986 static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
5987 					   u32 supported_bands)
5988 {
5989 	struct ieee80211_supported_band *band;
5990 	struct ath11k_hal_reg_capabilities_ext *reg_cap;
5991 	void *channels;
5992 	u32 phy_id;
5993 
5994 	BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
5995 		      ARRAY_SIZE(ath11k_5ghz_channels) +
5996 		      ARRAY_SIZE(ath11k_6ghz_channels)) !=
5997 		     ATH11K_NUM_CHANS);
5998 
5999 	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
6000 
6001 	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
6002 		channels = kmemdup(ath11k_2ghz_channels,
6003 				   sizeof(ath11k_2ghz_channels),
6004 				   GFP_KERNEL);
6005 		if (!channels)
6006 			return -ENOMEM;
6007 
6008 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
6009 		band->band = NL80211_BAND_2GHZ;
6010 		band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
6011 		band->channels = channels;
6012 		band->n_bitrates = ath11k_g_rates_size;
6013 		band->bitrates = ath11k_g_rates;
6014 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
6015 
6016 		if (ar->ab->hw_params.single_pdev_only) {
6017 			phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_2G_CAP);
6018 			reg_cap = &ar->ab->hal_reg_cap[phy_id];
6019 		}
6020 		ath11k_mac_update_ch_list(ar, band,
6021 					  reg_cap->low_2ghz_chan,
6022 					  reg_cap->high_2ghz_chan);
6023 	}
6024 
6025 	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
6026 		if (reg_cap->high_5ghz_chan >= ATH11K_MAX_6G_FREQ) {
6027 			channels = kmemdup(ath11k_6ghz_channels,
6028 					   sizeof(ath11k_6ghz_channels), GFP_KERNEL);
6029 			if (!channels) {
6030 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6031 				return -ENOMEM;
6032 			}
6033 
6034 			ar->supports_6ghz = true;
6035 			band = &ar->mac.sbands[NL80211_BAND_6GHZ];
6036 			band->band = NL80211_BAND_6GHZ;
6037 			band->n_channels = ARRAY_SIZE(ath11k_6ghz_channels);
6038 			band->channels = channels;
6039 			band->n_bitrates = ath11k_a_rates_size;
6040 			band->bitrates = ath11k_a_rates;
6041 			ar->hw->wiphy->bands[NL80211_BAND_6GHZ] = band;
6042 			ath11k_mac_update_ch_list(ar, band,
6043 						  reg_cap->low_5ghz_chan,
6044 						  reg_cap->high_5ghz_chan);
6045 		}
6046 
6047 		if (reg_cap->low_5ghz_chan < ATH11K_MIN_6G_FREQ) {
6048 			channels = kmemdup(ath11k_5ghz_channels,
6049 					   sizeof(ath11k_5ghz_channels),
6050 					   GFP_KERNEL);
6051 			if (!channels) {
6052 				kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6053 				kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6054 				return -ENOMEM;
6055 			}
6056 
6057 			band = &ar->mac.sbands[NL80211_BAND_5GHZ];
6058 			band->band = NL80211_BAND_5GHZ;
6059 			band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
6060 			band->channels = channels;
6061 			band->n_bitrates = ath11k_a_rates_size;
6062 			band->bitrates = ath11k_a_rates;
6063 			ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
6064 
6065 			if (ar->ab->hw_params.single_pdev_only) {
6066 				phy_id = ath11k_get_phy_id(ar, WMI_HOST_WLAN_5G_CAP);
6067 				reg_cap = &ar->ab->hal_reg_cap[phy_id];
6068 			}
6069 
6070 			ath11k_mac_update_ch_list(ar, band,
6071 						  reg_cap->low_5ghz_chan,
6072 						  reg_cap->high_5ghz_chan);
6073 		}
6074 	}
6075 
6076 	return 0;
6077 }
6078 
ath11k_mac_setup_iface_combinations(struct ath11k * ar)6079 static int ath11k_mac_setup_iface_combinations(struct ath11k *ar)
6080 {
6081 	struct ath11k_base *ab = ar->ab;
6082 	struct ieee80211_iface_combination *combinations;
6083 	struct ieee80211_iface_limit *limits;
6084 	int n_limits;
6085 
6086 	combinations = kzalloc(sizeof(*combinations), GFP_KERNEL);
6087 	if (!combinations)
6088 		return -ENOMEM;
6089 
6090 	n_limits = 2;
6091 
6092 	limits = kcalloc(n_limits, sizeof(*limits), GFP_KERNEL);
6093 	if (!limits) {
6094 		kfree(combinations);
6095 		return -ENOMEM;
6096 	}
6097 
6098 	limits[0].max = 1;
6099 	limits[0].types |= BIT(NL80211_IFTYPE_STATION);
6100 
6101 	limits[1].max = 16;
6102 	limits[1].types |= BIT(NL80211_IFTYPE_AP);
6103 
6104 	if (IS_ENABLED(CONFIG_MAC80211_MESH) &&
6105 	    ab->hw_params.interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
6106 		limits[1].types |= BIT(NL80211_IFTYPE_MESH_POINT);
6107 
6108 	combinations[0].limits = limits;
6109 	combinations[0].n_limits = n_limits;
6110 	combinations[0].max_interfaces = 16;
6111 	combinations[0].num_different_channels = 1;
6112 	combinations[0].beacon_int_infra_match = true;
6113 	combinations[0].beacon_int_min_gcd = 100;
6114 	combinations[0].radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
6115 						BIT(NL80211_CHAN_WIDTH_20) |
6116 						BIT(NL80211_CHAN_WIDTH_40) |
6117 						BIT(NL80211_CHAN_WIDTH_80);
6118 
6119 	ar->hw->wiphy->iface_combinations = combinations;
6120 	ar->hw->wiphy->n_iface_combinations = 1;
6121 
6122 	return 0;
6123 }
6124 
6125 static const u8 ath11k_if_types_ext_capa[] = {
6126 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6127 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6128 };
6129 
6130 static const u8 ath11k_if_types_ext_capa_sta[] = {
6131 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6132 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6133 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
6134 };
6135 
6136 static const u8 ath11k_if_types_ext_capa_ap[] = {
6137 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
6138 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
6139 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
6140 };
6141 
6142 static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
6143 	{
6144 		.extended_capabilities = ath11k_if_types_ext_capa,
6145 		.extended_capabilities_mask = ath11k_if_types_ext_capa,
6146 		.extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
6147 	}, {
6148 		.iftype = NL80211_IFTYPE_STATION,
6149 		.extended_capabilities = ath11k_if_types_ext_capa_sta,
6150 		.extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
6151 		.extended_capabilities_len =
6152 				sizeof(ath11k_if_types_ext_capa_sta),
6153 	}, {
6154 		.iftype = NL80211_IFTYPE_AP,
6155 		.extended_capabilities = ath11k_if_types_ext_capa_ap,
6156 		.extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
6157 		.extended_capabilities_len =
6158 				sizeof(ath11k_if_types_ext_capa_ap),
6159 	},
6160 };
6161 
__ath11k_mac_unregister(struct ath11k * ar)6162 static void __ath11k_mac_unregister(struct ath11k *ar)
6163 {
6164 	cancel_work_sync(&ar->regd_update_work);
6165 
6166 	ieee80211_unregister_hw(ar->hw);
6167 
6168 	idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
6169 	idr_destroy(&ar->txmgmt_idr);
6170 
6171 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6172 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
6173 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6174 
6175 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
6176 	kfree(ar->hw->wiphy->iface_combinations);
6177 
6178 	SET_IEEE80211_DEV(ar->hw, NULL);
6179 }
6180 
ath11k_mac_unregister(struct ath11k_base * ab)6181 void ath11k_mac_unregister(struct ath11k_base *ab)
6182 {
6183 	struct ath11k *ar;
6184 	struct ath11k_pdev *pdev;
6185 	int i;
6186 
6187 	for (i = 0; i < ab->num_radios; i++) {
6188 		pdev = &ab->pdevs[i];
6189 		ar = pdev->ar;
6190 		if (!ar)
6191 			continue;
6192 
6193 		__ath11k_mac_unregister(ar);
6194 	}
6195 }
6196 
__ath11k_mac_register(struct ath11k * ar)6197 static int __ath11k_mac_register(struct ath11k *ar)
6198 {
6199 	struct ath11k_base *ab = ar->ab;
6200 	struct ath11k_pdev_cap *cap = &ar->pdev->cap;
6201 	static const u32 cipher_suites[] = {
6202 		WLAN_CIPHER_SUITE_TKIP,
6203 		WLAN_CIPHER_SUITE_CCMP,
6204 		WLAN_CIPHER_SUITE_AES_CMAC,
6205 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
6206 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
6207 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
6208 		WLAN_CIPHER_SUITE_GCMP,
6209 		WLAN_CIPHER_SUITE_GCMP_256,
6210 		WLAN_CIPHER_SUITE_CCMP_256,
6211 	};
6212 	int ret;
6213 	u32 ht_cap = 0;
6214 
6215 	ath11k_pdev_caps_update(ar);
6216 
6217 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
6218 
6219 	SET_IEEE80211_DEV(ar->hw, ab->dev);
6220 
6221 	ret = ath11k_mac_setup_channels_rates(ar,
6222 					      cap->supported_bands);
6223 	if (ret)
6224 		goto err;
6225 
6226 	ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
6227 	ath11k_mac_setup_he_cap(ar, cap);
6228 
6229 	ret = ath11k_mac_setup_iface_combinations(ar);
6230 	if (ret) {
6231 		ath11k_err(ar->ab, "failed to setup interface combinations: %d\n", ret);
6232 		goto err_free_channels;
6233 	}
6234 
6235 	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
6236 	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
6237 
6238 	ar->hw->wiphy->interface_modes = ab->hw_params.interface_modes;
6239 
6240 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
6241 	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
6242 	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
6243 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
6244 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
6245 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
6246 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
6247 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
6248 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
6249 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
6250 	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
6251 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
6252 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
6253 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
6254 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
6255 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
6256 	if (ht_cap & WMI_HT_CAP_ENABLED) {
6257 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
6258 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
6259 		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
6260 		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
6261 		ieee80211_hw_set(ar->hw, USES_RSS);
6262 	}
6263 
6264 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
6265 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
6266 
6267 	/* TODO: Check if HT capability advertised from firmware is different
6268 	 * for each band for a dual band capable radio. It will be tricky to
6269 	 * handle it when the ht capability different for each band.
6270 	 */
6271 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS)
6272 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
6273 
6274 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
6275 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
6276 
6277 	ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
6278 
6279 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
6280 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
6281 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
6282 
6283 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
6284 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
6285 				   NL80211_FEATURE_AP_SCAN;
6286 
6287 	ar->max_num_stations = TARGET_NUM_STATIONS;
6288 	ar->max_num_peers = TARGET_NUM_PEERS_PDEV;
6289 
6290 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
6291 
6292 	ar->hw->queues = ATH11K_HW_MAX_QUEUES;
6293 	ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
6294 	ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
6295 	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
6296 
6297 	ar->hw->vif_data_size = sizeof(struct ath11k_vif);
6298 	ar->hw->sta_data_size = sizeof(struct ath11k_sta);
6299 
6300 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
6301 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
6302 
6303 	ar->hw->wiphy->cipher_suites = cipher_suites;
6304 	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
6305 
6306 	ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
6307 	ar->hw->wiphy->num_iftype_ext_capab =
6308 		ARRAY_SIZE(ath11k_iftypes_ext_capa);
6309 
6310 	ath11k_reg_init(ar);
6311 
6312 	if (!test_bit(ATH11K_FLAG_RAW_MODE, &ab->dev_flags)) {
6313 		ar->hw->netdev_features = NETIF_F_HW_CSUM;
6314 		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
6315 		ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
6316 	}
6317 
6318 	ret = ieee80211_register_hw(ar->hw);
6319 	if (ret) {
6320 		ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
6321 		goto err_free_if_combs;
6322 	}
6323 
6324 	if (!ab->hw_params.supports_monitor)
6325 		/* There's a race between calling ieee80211_register_hw()
6326 		 * and here where the monitor mode is enabled for a little
6327 		 * while. But that time is so short and in practise it make
6328 		 * a difference in real life.
6329 		 */
6330 		ar->hw->wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MONITOR);
6331 
6332 	/* Apply the regd received during initialization */
6333 	ret = ath11k_regd_update(ar);
6334 	if (ret) {
6335 		ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
6336 		goto err_unregister_hw;
6337 	}
6338 
6339 	ret = ath11k_debugfs_register(ar);
6340 	if (ret) {
6341 		ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
6342 		goto err_unregister_hw;
6343 	}
6344 
6345 	return 0;
6346 
6347 err_unregister_hw:
6348 	ieee80211_unregister_hw(ar->hw);
6349 
6350 err_free_if_combs:
6351 	kfree(ar->hw->wiphy->iface_combinations[0].limits);
6352 	kfree(ar->hw->wiphy->iface_combinations);
6353 
6354 err_free_channels:
6355 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
6356 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
6357 	kfree(ar->mac.sbands[NL80211_BAND_6GHZ].channels);
6358 
6359 err:
6360 	SET_IEEE80211_DEV(ar->hw, NULL);
6361 	return ret;
6362 }
6363 
ath11k_mac_register(struct ath11k_base * ab)6364 int ath11k_mac_register(struct ath11k_base *ab)
6365 {
6366 	struct ath11k *ar;
6367 	struct ath11k_pdev *pdev;
6368 	int i;
6369 	int ret;
6370 
6371 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
6372 		return 0;
6373 
6374 	for (i = 0; i < ab->num_radios; i++) {
6375 		pdev = &ab->pdevs[i];
6376 		ar = pdev->ar;
6377 		if (ab->pdevs_macaddr_valid) {
6378 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
6379 		} else {
6380 			ether_addr_copy(ar->mac_addr, ab->mac_addr);
6381 			ar->mac_addr[4] += i;
6382 		}
6383 
6384 		ret = __ath11k_mac_register(ar);
6385 		if (ret)
6386 			goto err_cleanup;
6387 
6388 		idr_init(&ar->txmgmt_idr);
6389 		spin_lock_init(&ar->txmgmt_idr_lock);
6390 	}
6391 
6392 	/* Initialize channel counters frequency value in hertz */
6393 	ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
6394 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
6395 
6396 	return 0;
6397 
6398 err_cleanup:
6399 	for (i = i - 1; i >= 0; i--) {
6400 		pdev = &ab->pdevs[i];
6401 		ar = pdev->ar;
6402 		__ath11k_mac_unregister(ar);
6403 	}
6404 
6405 	return ret;
6406 }
6407 
ath11k_mac_allocate(struct ath11k_base * ab)6408 int ath11k_mac_allocate(struct ath11k_base *ab)
6409 {
6410 	struct ieee80211_hw *hw;
6411 	struct ath11k *ar;
6412 	struct ath11k_pdev *pdev;
6413 	int ret;
6414 	int i;
6415 
6416 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
6417 		return 0;
6418 
6419 	for (i = 0; i < ab->num_radios; i++) {
6420 		pdev = &ab->pdevs[i];
6421 		hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
6422 		if (!hw) {
6423 			ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
6424 			ret = -ENOMEM;
6425 			goto err_free_mac;
6426 		}
6427 
6428 		ar = hw->priv;
6429 		ar->hw = hw;
6430 		ar->ab = ab;
6431 		ar->pdev = pdev;
6432 		ar->pdev_idx = i;
6433 		ar->lmac_id = ath11k_hw_get_mac_from_pdev_id(&ab->hw_params, i);
6434 
6435 		ar->wmi = &ab->wmi_ab.wmi[i];
6436 		/* FIXME wmi[0] is already initialized during attach,
6437 		 * Should we do this again?
6438 		 */
6439 		ath11k_wmi_pdev_attach(ab, i);
6440 
6441 		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
6442 		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
6443 		ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
6444 		ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
6445 
6446 		pdev->ar = ar;
6447 		spin_lock_init(&ar->data_lock);
6448 		INIT_LIST_HEAD(&ar->arvifs);
6449 		INIT_LIST_HEAD(&ar->ppdu_stats_info);
6450 		mutex_init(&ar->conf_mutex);
6451 		init_completion(&ar->vdev_setup_done);
6452 		init_completion(&ar->peer_assoc_done);
6453 		init_completion(&ar->peer_delete_done);
6454 		init_completion(&ar->install_key_done);
6455 		init_completion(&ar->bss_survey_done);
6456 		init_completion(&ar->scan.started);
6457 		init_completion(&ar->scan.completed);
6458 		init_completion(&ar->thermal.wmi_sync);
6459 
6460 		INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
6461 		INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
6462 
6463 		INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
6464 		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
6465 		clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
6466 	}
6467 
6468 	return 0;
6469 
6470 err_free_mac:
6471 	ath11k_mac_destroy(ab);
6472 
6473 	return ret;
6474 }
6475 
ath11k_mac_destroy(struct ath11k_base * ab)6476 void ath11k_mac_destroy(struct ath11k_base *ab)
6477 {
6478 	struct ath11k *ar;
6479 	struct ath11k_pdev *pdev;
6480 	int i;
6481 
6482 	for (i = 0; i < ab->num_radios; i++) {
6483 		pdev = &ab->pdevs[i];
6484 		ar = pdev->ar;
6485 		if (!ar)
6486 			continue;
6487 
6488 		ieee80211_free_hw(ar->hw);
6489 		pdev->ar = NULL;
6490 	}
6491 }
6492