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