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