xref: /freebsd/sys/contrib/dev/athk/ath10k/mac.c (revision 9954217599ce02fbf1772388e24e0b89663f4425)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2005-2011 Atheros Communications Inc.
4  * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
5  * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
6  * Copyright (c) 2021-2024 Qualcomm Innovation Center, Inc. All rights reserved.
7  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
8  */
9 
10 #include "mac.h"
11 
12 #include <linux/export.h>
13 #include <net/cfg80211.h>
14 #include <net/mac80211.h>
15 #include <linux/etherdevice.h>
16 #include <linux/acpi.h>
17 #if defined(__linux__) || (defined(__FreeBSD__) && defined(CONFIG_OF))
18 #include <linux/of.h>
19 #endif
20 #include <linux/bitfield.h>
21 #include <linux/random.h>
22 
23 #include "hif.h"
24 #include "core.h"
25 #include "debug.h"
26 #include "wmi.h"
27 #include "htt.h"
28 #include "txrx.h"
29 #include "testmode.h"
30 #include "wmi-tlv.h"
31 #include "wmi-ops.h"
32 #include "wow.h"
33 #include "leds.h"
34 
35 /*********/
36 /* Rates */
37 /*********/
38 
39 static struct ieee80211_rate ath10k_rates[] = {
40 	{ .bitrate = 10,
41 	  .hw_value = ATH10K_HW_RATE_CCK_LP_1M },
42 	{ .bitrate = 20,
43 	  .hw_value = ATH10K_HW_RATE_CCK_LP_2M,
44 	  .hw_value_short = ATH10K_HW_RATE_CCK_SP_2M,
45 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
46 	{ .bitrate = 55,
47 	  .hw_value = ATH10K_HW_RATE_CCK_LP_5_5M,
48 	  .hw_value_short = ATH10K_HW_RATE_CCK_SP_5_5M,
49 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
50 	{ .bitrate = 110,
51 	  .hw_value = ATH10K_HW_RATE_CCK_LP_11M,
52 	  .hw_value_short = ATH10K_HW_RATE_CCK_SP_11M,
53 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
54 
55 	{ .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
56 	{ .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
57 	{ .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
58 	{ .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
59 	{ .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
60 	{ .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
61 	{ .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
62 	{ .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
63 };
64 
65 static struct ieee80211_rate ath10k_rates_rev2[] = {
66 	{ .bitrate = 10,
67 	  .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_1M },
68 	{ .bitrate = 20,
69 	  .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_2M,
70 	  .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_2M,
71 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
72 	{ .bitrate = 55,
73 	  .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_5_5M,
74 	  .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_5_5M,
75 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
76 	{ .bitrate = 110,
77 	  .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_11M,
78 	  .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_11M,
79 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
80 
81 	{ .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
82 	{ .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
83 	{ .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
84 	{ .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
85 	{ .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
86 	{ .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
87 	{ .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
88 	{ .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
89 };
90 
91 static const struct cfg80211_sar_freq_ranges ath10k_sar_freq_ranges[] = {
92 	{.start_freq = 2402, .end_freq = 2494 },
93 	{.start_freq = 5170, .end_freq = 5875 },
94 };
95 
96 static const struct cfg80211_sar_capa ath10k_sar_capa = {
97 	.type = NL80211_SAR_TYPE_POWER,
98 	.num_freq_ranges = (ARRAY_SIZE(ath10k_sar_freq_ranges)),
99 	.freq_ranges = &ath10k_sar_freq_ranges[0],
100 };
101 
102 #define ATH10K_MAC_FIRST_OFDM_RATE_IDX 4
103 
104 #define ath10k_a_rates (ath10k_rates + ATH10K_MAC_FIRST_OFDM_RATE_IDX)
105 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - \
106 			     ATH10K_MAC_FIRST_OFDM_RATE_IDX)
107 #define ath10k_g_rates (ath10k_rates + 0)
108 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
109 
110 #define ath10k_g_rates_rev2 (ath10k_rates_rev2 + 0)
111 #define ath10k_g_rates_rev2_size (ARRAY_SIZE(ath10k_rates_rev2))
112 
113 #define ath10k_wmi_legacy_rates ath10k_rates
114 
ath10k_mac_bitrate_is_cck(int bitrate)115 static bool ath10k_mac_bitrate_is_cck(int bitrate)
116 {
117 	switch (bitrate) {
118 	case 10:
119 	case 20:
120 	case 55:
121 	case 110:
122 		return true;
123 	}
124 
125 	return false;
126 }
127 
ath10k_mac_bitrate_to_rate(int bitrate)128 static u8 ath10k_mac_bitrate_to_rate(int bitrate)
129 {
130 	return DIV_ROUND_UP(bitrate, 5) |
131 	       (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
132 }
133 
ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band * sband,u8 hw_rate,bool cck)134 u8 ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
135 			     u8 hw_rate, bool cck)
136 {
137 	const struct ieee80211_rate *rate;
138 	int i;
139 
140 	for (i = 0; i < sband->n_bitrates; i++) {
141 		rate = &sband->bitrates[i];
142 
143 		if (ath10k_mac_bitrate_is_cck(rate->bitrate) != cck)
144 			continue;
145 
146 		if (rate->hw_value == hw_rate)
147 			return i;
148 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
149 			 rate->hw_value_short == hw_rate)
150 			return i;
151 	}
152 
153 	return 0;
154 }
155 
ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band * sband,u32 bitrate)156 u8 ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
157 			     u32 bitrate)
158 {
159 	int i;
160 
161 	for (i = 0; i < sband->n_bitrates; i++)
162 		if (sband->bitrates[i].bitrate == bitrate)
163 			return i;
164 
165 	return 0;
166 }
167 
ath10k_mac_get_rate_hw_value(int bitrate)168 static int ath10k_mac_get_rate_hw_value(int bitrate)
169 {
170 	int i;
171 	u8 hw_value_prefix = 0;
172 
173 	if (ath10k_mac_bitrate_is_cck(bitrate))
174 		hw_value_prefix = WMI_RATE_PREAMBLE_CCK << 6;
175 
176 	for (i = 0; i < ARRAY_SIZE(ath10k_rates); i++) {
177 		if (ath10k_rates[i].bitrate == bitrate)
178 			return hw_value_prefix | ath10k_rates[i].hw_value;
179 	}
180 
181 	return -EINVAL;
182 }
183 
ath10k_mac_get_max_vht_mcs_map(u16 mcs_map,int nss)184 static int ath10k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
185 {
186 	switch ((mcs_map >> (2 * nss)) & 0x3) {
187 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
188 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
189 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
190 	}
191 	return 0;
192 }
193 
194 static u32
ath10k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])195 ath10k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
196 {
197 	int nss;
198 
199 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
200 		if (ht_mcs_mask[nss])
201 			return nss + 1;
202 
203 	return 1;
204 }
205 
206 static u32
ath10k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])207 ath10k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
208 {
209 	int nss;
210 
211 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
212 		if (vht_mcs_mask[nss])
213 			return nss + 1;
214 
215 	return 1;
216 }
217 
ath10k_mac_ext_resource_config(struct ath10k * ar,u32 val)218 int ath10k_mac_ext_resource_config(struct ath10k *ar, u32 val)
219 {
220 	enum wmi_host_platform_type platform_type;
221 	int ret;
222 
223 	if (test_bit(WMI_SERVICE_TX_MODE_DYNAMIC, ar->wmi.svc_map))
224 		platform_type = WMI_HOST_PLATFORM_LOW_PERF;
225 	else
226 		platform_type = WMI_HOST_PLATFORM_HIGH_PERF;
227 
228 	ret = ath10k_wmi_ext_resource_config(ar, platform_type, val);
229 
230 	if (ret && ret != -EOPNOTSUPP) {
231 		ath10k_warn(ar, "failed to configure ext resource: %d\n", ret);
232 		return ret;
233 	}
234 
235 	return 0;
236 }
237 
238 /**********/
239 /* Crypto */
240 /**********/
241 
ath10k_send_key(struct ath10k_vif * arvif,struct ieee80211_key_conf * key,enum set_key_cmd cmd,const u8 * macaddr,u32 flags)242 static int ath10k_send_key(struct ath10k_vif *arvif,
243 			   struct ieee80211_key_conf *key,
244 			   enum set_key_cmd cmd,
245 			   const u8 *macaddr, u32 flags)
246 {
247 	struct ath10k *ar = arvif->ar;
248 	struct wmi_vdev_install_key_arg arg = {
249 		.vdev_id = arvif->vdev_id,
250 		.key_idx = key->keyidx,
251 		.key_len = key->keylen,
252 		.key_data = key->key,
253 		.key_flags = flags,
254 		.macaddr = macaddr,
255 	};
256 
257 	lockdep_assert_held(&arvif->ar->conf_mutex);
258 
259 	switch (key->cipher) {
260 	case WLAN_CIPHER_SUITE_CCMP:
261 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_CCM];
262 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
263 		break;
264 	case WLAN_CIPHER_SUITE_TKIP:
265 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_TKIP];
266 		arg.key_txmic_len = 8;
267 		arg.key_rxmic_len = 8;
268 		break;
269 	case WLAN_CIPHER_SUITE_WEP40:
270 	case WLAN_CIPHER_SUITE_WEP104:
271 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_WEP];
272 		break;
273 	case WLAN_CIPHER_SUITE_CCMP_256:
274 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_CCM];
275 		break;
276 	case WLAN_CIPHER_SUITE_GCMP:
277 	case WLAN_CIPHER_SUITE_GCMP_256:
278 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_GCM];
279 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
280 		break;
281 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
282 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
283 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
284 	case WLAN_CIPHER_SUITE_AES_CMAC:
285 		WARN_ON(1);
286 		return -EINVAL;
287 	default:
288 		ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
289 		return -EOPNOTSUPP;
290 	}
291 
292 	if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
293 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
294 
295 	if (cmd == DISABLE_KEY) {
296 		if (flags & WMI_KEY_GROUP) {
297 			/* Not all hardware handles group-key deletion operation
298 			 * correctly. Replace the key with a junk value to invalidate it.
299 			 */
300 			get_random_bytes(key->key, key->keylen);
301 		} else {
302 			arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_NONE];
303 			arg.key_data = NULL;
304 		}
305 	}
306 
307 	return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
308 }
309 
ath10k_install_key(struct ath10k_vif * arvif,struct ieee80211_key_conf * key,enum set_key_cmd cmd,const u8 * macaddr,u32 flags)310 static int ath10k_install_key(struct ath10k_vif *arvif,
311 			      struct ieee80211_key_conf *key,
312 			      enum set_key_cmd cmd,
313 			      const u8 *macaddr, u32 flags)
314 {
315 	struct ath10k *ar = arvif->ar;
316 	int ret;
317 	unsigned long time_left;
318 
319 	lockdep_assert_held(&ar->conf_mutex);
320 
321 	reinit_completion(&ar->install_key_done);
322 
323 	if (arvif->nohwcrypt)
324 		return 1;
325 
326 	ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
327 	if (ret)
328 		return ret;
329 
330 	time_left = wait_for_completion_timeout(&ar->install_key_done, 3 * HZ);
331 	if (time_left == 0)
332 		return -ETIMEDOUT;
333 
334 	return 0;
335 }
336 
ath10k_install_peer_wep_keys(struct ath10k_vif * arvif,const u8 * addr)337 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
338 					const u8 *addr)
339 {
340 	struct ath10k *ar = arvif->ar;
341 	struct ath10k_peer *peer;
342 	int ret;
343 	int i;
344 	u32 flags;
345 
346 	lockdep_assert_held(&ar->conf_mutex);
347 
348 	if (WARN_ON(arvif->vif->type != NL80211_IFTYPE_AP &&
349 		    arvif->vif->type != NL80211_IFTYPE_ADHOC &&
350 		    arvif->vif->type != NL80211_IFTYPE_MESH_POINT))
351 		return -EINVAL;
352 
353 	spin_lock_bh(&ar->data_lock);
354 	peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
355 	spin_unlock_bh(&ar->data_lock);
356 
357 	if (!peer)
358 		return -ENOENT;
359 
360 	for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
361 		if (arvif->wep_keys[i] == NULL)
362 			continue;
363 
364 		switch (arvif->vif->type) {
365 		case NL80211_IFTYPE_AP:
366 			flags = WMI_KEY_PAIRWISE;
367 
368 			if (arvif->def_wep_key_idx == i)
369 				flags |= WMI_KEY_TX_USAGE;
370 
371 			ret = ath10k_install_key(arvif, arvif->wep_keys[i],
372 						 SET_KEY, addr, flags);
373 			if (ret < 0)
374 				return ret;
375 			break;
376 		case NL80211_IFTYPE_ADHOC:
377 			ret = ath10k_install_key(arvif, arvif->wep_keys[i],
378 						 SET_KEY, addr,
379 						 WMI_KEY_PAIRWISE);
380 			if (ret < 0)
381 				return ret;
382 
383 			ret = ath10k_install_key(arvif, arvif->wep_keys[i],
384 						 SET_KEY, addr, WMI_KEY_GROUP);
385 			if (ret < 0)
386 				return ret;
387 			break;
388 		default:
389 			WARN_ON(1);
390 			return -EINVAL;
391 		}
392 
393 		spin_lock_bh(&ar->data_lock);
394 		peer->keys[i] = arvif->wep_keys[i];
395 		spin_unlock_bh(&ar->data_lock);
396 	}
397 
398 	/* In some cases (notably with static WEP IBSS with multiple keys)
399 	 * multicast Tx becomes broken. Both pairwise and groupwise keys are
400 	 * installed already. Using WMI_KEY_TX_USAGE in different combinations
401 	 * didn't seem help. Using def_keyid vdev parameter seems to be
402 	 * effective so use that.
403 	 *
404 	 * FIXME: Revisit. Perhaps this can be done in a less hacky way.
405 	 */
406 	if (arvif->vif->type != NL80211_IFTYPE_ADHOC)
407 		return 0;
408 
409 	if (arvif->def_wep_key_idx == -1)
410 		return 0;
411 
412 	ret = ath10k_wmi_vdev_set_param(arvif->ar,
413 					arvif->vdev_id,
414 					arvif->ar->wmi.vdev_param->def_keyid,
415 					arvif->def_wep_key_idx);
416 	if (ret) {
417 		ath10k_warn(ar, "failed to re-set def wpa key idxon vdev %i: %d\n",
418 			    arvif->vdev_id, ret);
419 		return ret;
420 	}
421 
422 	return 0;
423 }
424 
ath10k_clear_peer_keys(struct ath10k_vif * arvif,const u8 * addr)425 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
426 				  const u8 *addr)
427 {
428 	struct ath10k *ar = arvif->ar;
429 	struct ath10k_peer *peer;
430 	int first_errno = 0;
431 	int ret;
432 	int i;
433 	u32 flags = 0;
434 
435 	lockdep_assert_held(&ar->conf_mutex);
436 
437 	spin_lock_bh(&ar->data_lock);
438 	peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
439 	spin_unlock_bh(&ar->data_lock);
440 
441 	if (!peer)
442 		return -ENOENT;
443 
444 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
445 		if (peer->keys[i] == NULL)
446 			continue;
447 
448 		/* key flags are not required to delete the key */
449 		ret = ath10k_install_key(arvif, peer->keys[i],
450 					 DISABLE_KEY, addr, flags);
451 		if (ret < 0 && first_errno == 0)
452 			first_errno = ret;
453 
454 		if (ret < 0)
455 			ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
456 				    i, ret);
457 
458 		spin_lock_bh(&ar->data_lock);
459 		peer->keys[i] = NULL;
460 		spin_unlock_bh(&ar->data_lock);
461 	}
462 
463 	return first_errno;
464 }
465 
ath10k_mac_is_peer_wep_key_set(struct ath10k * ar,const u8 * addr,u8 keyidx)466 bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
467 				    u8 keyidx)
468 {
469 	struct ath10k_peer *peer;
470 	int i;
471 
472 	lockdep_assert_held(&ar->data_lock);
473 
474 	/* We don't know which vdev this peer belongs to,
475 	 * since WMI doesn't give us that information.
476 	 *
477 	 * FIXME: multi-bss needs to be handled.
478 	 */
479 	peer = ath10k_peer_find(ar, 0, addr);
480 	if (!peer)
481 		return false;
482 
483 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
484 		if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
485 			return true;
486 	}
487 
488 	return false;
489 }
490 
ath10k_clear_vdev_key(struct ath10k_vif * arvif,struct ieee80211_key_conf * key)491 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
492 				 struct ieee80211_key_conf *key)
493 {
494 	struct ath10k *ar = arvif->ar;
495 	struct ath10k_peer *peer;
496 	u8 addr[ETH_ALEN];
497 	int first_errno = 0;
498 	int ret;
499 	int i;
500 	u32 flags = 0;
501 
502 	lockdep_assert_held(&ar->conf_mutex);
503 
504 	for (;;) {
505 		/* since ath10k_install_key we can't hold data_lock all the
506 		 * time, so we try to remove the keys incrementally
507 		 */
508 		spin_lock_bh(&ar->data_lock);
509 		i = 0;
510 		list_for_each_entry(peer, &ar->peers, list) {
511 			for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
512 				if (peer->keys[i] == key) {
513 					ether_addr_copy(addr, peer->addr);
514 					peer->keys[i] = NULL;
515 					break;
516 				}
517 			}
518 
519 			if (i < ARRAY_SIZE(peer->keys))
520 				break;
521 		}
522 		spin_unlock_bh(&ar->data_lock);
523 
524 		if (i == ARRAY_SIZE(peer->keys))
525 			break;
526 		/* key flags are not required to delete the key */
527 		ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
528 		if (ret < 0 && first_errno == 0)
529 			first_errno = ret;
530 
531 		if (ret)
532 			ath10k_warn(ar, "failed to remove key for %pM: %d\n",
533 				    addr, ret);
534 	}
535 
536 	return first_errno;
537 }
538 
ath10k_mac_vif_update_wep_key(struct ath10k_vif * arvif,struct ieee80211_key_conf * key)539 static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
540 					 struct ieee80211_key_conf *key)
541 {
542 	struct ath10k *ar = arvif->ar;
543 	struct ath10k_peer *peer;
544 	int ret;
545 
546 	lockdep_assert_held(&ar->conf_mutex);
547 
548 	list_for_each_entry(peer, &ar->peers, list) {
549 		if (ether_addr_equal(peer->addr, arvif->vif->addr))
550 			continue;
551 
552 		if (ether_addr_equal(peer->addr, arvif->bssid))
553 			continue;
554 
555 		if (peer->keys[key->keyidx] == key)
556 			continue;
557 
558 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
559 			   arvif->vdev_id, key->keyidx);
560 
561 		ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
562 		if (ret) {
563 			ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
564 				    arvif->vdev_id, peer->addr, ret);
565 			return ret;
566 		}
567 	}
568 
569 	return 0;
570 }
571 
572 /*********************/
573 /* General utilities */
574 /*********************/
575 
576 static inline enum wmi_phy_mode
chan_to_phymode(const struct cfg80211_chan_def * chandef)577 chan_to_phymode(const struct cfg80211_chan_def *chandef)
578 {
579 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
580 
581 	switch (chandef->chan->band) {
582 	case NL80211_BAND_2GHZ:
583 		switch (chandef->width) {
584 		case NL80211_CHAN_WIDTH_20_NOHT:
585 			if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
586 				phymode = MODE_11B;
587 			else
588 				phymode = MODE_11G;
589 			break;
590 		case NL80211_CHAN_WIDTH_20:
591 			phymode = MODE_11NG_HT20;
592 			break;
593 		case NL80211_CHAN_WIDTH_40:
594 			phymode = MODE_11NG_HT40;
595 			break;
596 		default:
597 			phymode = MODE_UNKNOWN;
598 			break;
599 		}
600 		break;
601 	case NL80211_BAND_5GHZ:
602 		switch (chandef->width) {
603 		case NL80211_CHAN_WIDTH_20_NOHT:
604 			phymode = MODE_11A;
605 			break;
606 		case NL80211_CHAN_WIDTH_20:
607 			phymode = MODE_11NA_HT20;
608 			break;
609 		case NL80211_CHAN_WIDTH_40:
610 			phymode = MODE_11NA_HT40;
611 			break;
612 		case NL80211_CHAN_WIDTH_80:
613 			phymode = MODE_11AC_VHT80;
614 			break;
615 		case NL80211_CHAN_WIDTH_160:
616 			phymode = MODE_11AC_VHT160;
617 			break;
618 		case NL80211_CHAN_WIDTH_80P80:
619 			phymode = MODE_11AC_VHT80_80;
620 			break;
621 		default:
622 			phymode = MODE_UNKNOWN;
623 			break;
624 		}
625 		break;
626 	default:
627 		break;
628 	}
629 
630 	WARN_ON(phymode == MODE_UNKNOWN);
631 	return phymode;
632 }
633 
ath10k_parse_mpdudensity(u8 mpdudensity)634 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
635 {
636 /*
637  * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
638  *   0 for no restriction
639  *   1 for 1/4 us
640  *   2 for 1/2 us
641  *   3 for 1 us
642  *   4 for 2 us
643  *   5 for 4 us
644  *   6 for 8 us
645  *   7 for 16 us
646  */
647 	switch (mpdudensity) {
648 	case 0:
649 		return 0;
650 	case 1:
651 	case 2:
652 	case 3:
653 	/* Our lower layer calculations limit our precision to
654 	 * 1 microsecond
655 	 */
656 		return 1;
657 	case 4:
658 		return 2;
659 	case 5:
660 		return 4;
661 	case 6:
662 		return 8;
663 	case 7:
664 		return 16;
665 	default:
666 		return 0;
667 	}
668 }
669 
ath10k_mac_vif_chan(struct ieee80211_vif * vif,struct cfg80211_chan_def * def)670 int ath10k_mac_vif_chan(struct ieee80211_vif *vif,
671 			struct cfg80211_chan_def *def)
672 {
673 	struct ieee80211_chanctx_conf *conf;
674 
675 	rcu_read_lock();
676 	conf = rcu_dereference(vif->bss_conf.chanctx_conf);
677 	if (!conf) {
678 		rcu_read_unlock();
679 		return -ENOENT;
680 	}
681 
682 	*def = conf->def;
683 	rcu_read_unlock();
684 
685 	return 0;
686 }
687 
ath10k_mac_num_chanctxs_iter(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf,void * data)688 static void ath10k_mac_num_chanctxs_iter(struct ieee80211_hw *hw,
689 					 struct ieee80211_chanctx_conf *conf,
690 					 void *data)
691 {
692 	int *num = data;
693 
694 	(*num)++;
695 }
696 
ath10k_mac_num_chanctxs(struct ath10k * ar)697 static int ath10k_mac_num_chanctxs(struct ath10k *ar)
698 {
699 	int num = 0;
700 
701 	ieee80211_iter_chan_contexts_atomic(ar->hw,
702 					    ath10k_mac_num_chanctxs_iter,
703 					    &num);
704 
705 	return num;
706 }
707 
708 static void
ath10k_mac_get_any_chandef_iter(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf,void * data)709 ath10k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
710 				struct ieee80211_chanctx_conf *conf,
711 				void *data)
712 {
713 	struct cfg80211_chan_def **def = data;
714 
715 	*def = &conf->def;
716 }
717 
ath10k_wait_for_peer_delete_done(struct ath10k * ar,u32 vdev_id,const u8 * addr)718 static void ath10k_wait_for_peer_delete_done(struct ath10k *ar, u32 vdev_id,
719 					     const u8 *addr)
720 {
721 	unsigned long time_left;
722 	int ret;
723 
724 	if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
725 		ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
726 		if (ret) {
727 			ath10k_warn(ar, "failed wait for peer deleted");
728 			return;
729 		}
730 
731 		time_left = wait_for_completion_timeout(&ar->peer_delete_done,
732 							5 * HZ);
733 		if (!time_left)
734 			ath10k_warn(ar, "Timeout in receiving peer delete response\n");
735 	}
736 }
737 
ath10k_peer_create(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 vdev_id,const u8 * addr,enum wmi_peer_type peer_type)738 static int ath10k_peer_create(struct ath10k *ar,
739 			      struct ieee80211_vif *vif,
740 			      struct ieee80211_sta *sta,
741 			      u32 vdev_id,
742 			      const u8 *addr,
743 			      enum wmi_peer_type peer_type)
744 {
745 	struct ath10k_peer *peer;
746 	int ret;
747 
748 	lockdep_assert_held(&ar->conf_mutex);
749 
750 	/* Each vdev consumes a peer entry as well. */
751 	if (ar->num_peers + list_count_nodes(&ar->arvifs) >= ar->max_num_peers)
752 		return -ENOBUFS;
753 
754 	ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
755 	if (ret) {
756 		ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
757 			    addr, vdev_id, ret);
758 		return ret;
759 	}
760 
761 	ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
762 	if (ret) {
763 		ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
764 			    addr, vdev_id, ret);
765 		return ret;
766 	}
767 
768 	spin_lock_bh(&ar->data_lock);
769 
770 	peer = ath10k_peer_find(ar, vdev_id, addr);
771 	if (!peer) {
772 		spin_unlock_bh(&ar->data_lock);
773 		ath10k_warn(ar, "failed to find peer %pM on vdev %i after creation\n",
774 			    addr, vdev_id);
775 		ath10k_wait_for_peer_delete_done(ar, vdev_id, addr);
776 		return -ENOENT;
777 	}
778 
779 	peer->vif = vif;
780 	peer->sta = sta;
781 
782 	spin_unlock_bh(&ar->data_lock);
783 
784 	ar->num_peers++;
785 
786 	return 0;
787 }
788 
ath10k_mac_set_kickout(struct ath10k_vif * arvif)789 static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
790 {
791 	struct ath10k *ar = arvif->ar;
792 	u32 param;
793 	int ret;
794 
795 	param = ar->wmi.pdev_param->sta_kickout_th;
796 	ret = ath10k_wmi_pdev_set_param(ar, param,
797 					ATH10K_KICKOUT_THRESHOLD);
798 	if (ret) {
799 		ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
800 			    arvif->vdev_id, ret);
801 		return ret;
802 	}
803 
804 	param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
805 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
806 					ATH10K_KEEPALIVE_MIN_IDLE);
807 	if (ret) {
808 		ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
809 			    arvif->vdev_id, ret);
810 		return ret;
811 	}
812 
813 	param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
814 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
815 					ATH10K_KEEPALIVE_MAX_IDLE);
816 	if (ret) {
817 		ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
818 			    arvif->vdev_id, ret);
819 		return ret;
820 	}
821 
822 	param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
823 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
824 					ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
825 	if (ret) {
826 		ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
827 			    arvif->vdev_id, ret);
828 		return ret;
829 	}
830 
831 	return 0;
832 }
833 
ath10k_mac_set_rts(struct ath10k_vif * arvif,u32 value)834 static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
835 {
836 	struct ath10k *ar = arvif->ar;
837 	u32 vdev_param;
838 
839 	vdev_param = ar->wmi.vdev_param->rts_threshold;
840 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
841 }
842 
ath10k_peer_delete(struct ath10k * ar,u32 vdev_id,const u8 * addr)843 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
844 {
845 	int ret;
846 
847 	lockdep_assert_held(&ar->conf_mutex);
848 
849 	ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
850 	if (ret)
851 		return ret;
852 
853 	ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
854 	if (ret)
855 		return ret;
856 
857 	if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
858 		unsigned long time_left;
859 
860 		time_left = wait_for_completion_timeout
861 			    (&ar->peer_delete_done, 5 * HZ);
862 
863 		if (!time_left) {
864 			ath10k_warn(ar, "Timeout in receiving peer delete response\n");
865 			return -ETIMEDOUT;
866 		}
867 	}
868 
869 	ar->num_peers--;
870 
871 	return 0;
872 }
873 
ath10k_peer_map_cleanup(struct ath10k * ar,struct ath10k_peer * peer)874 static void ath10k_peer_map_cleanup(struct ath10k *ar, struct ath10k_peer *peer)
875 {
876 	int peer_id, i;
877 
878 	lockdep_assert_held(&ar->conf_mutex);
879 
880 	for_each_set_bit(peer_id, peer->peer_ids,
881 			 ATH10K_MAX_NUM_PEER_IDS) {
882 		ar->peer_map[peer_id] = NULL;
883 	}
884 
885 	/* Double check that peer is properly un-referenced from
886 	 * the peer_map
887 	 */
888 	for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
889 		if (ar->peer_map[i] == peer) {
890 			ath10k_warn(ar, "removing stale peer_map entry for %pM (ptr %p idx %d)\n",
891 				    peer->addr, peer, i);
892 			ar->peer_map[i] = NULL;
893 		}
894 	}
895 
896 	list_del(&peer->list);
897 	kfree(peer);
898 	ar->num_peers--;
899 }
900 
ath10k_peer_cleanup(struct ath10k * ar,u32 vdev_id)901 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
902 {
903 	struct ath10k_peer *peer, *tmp;
904 
905 	lockdep_assert_held(&ar->conf_mutex);
906 
907 	spin_lock_bh(&ar->data_lock);
908 	list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
909 		if (peer->vdev_id != vdev_id)
910 			continue;
911 
912 		ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
913 			    peer->addr, vdev_id);
914 
915 		ath10k_peer_map_cleanup(ar, peer);
916 	}
917 	spin_unlock_bh(&ar->data_lock);
918 }
919 
ath10k_peer_cleanup_all(struct ath10k * ar)920 static void ath10k_peer_cleanup_all(struct ath10k *ar)
921 {
922 	struct ath10k_peer *peer, *tmp;
923 	int i;
924 
925 	lockdep_assert_held(&ar->conf_mutex);
926 
927 	spin_lock_bh(&ar->data_lock);
928 	list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
929 		list_del(&peer->list);
930 		kfree(peer);
931 	}
932 
933 	for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++)
934 		ar->peer_map[i] = NULL;
935 
936 	spin_unlock_bh(&ar->data_lock);
937 
938 	ar->num_peers = 0;
939 	ar->num_stations = 0;
940 }
941 
ath10k_mac_tdls_peer_update(struct ath10k * ar,u32 vdev_id,struct ieee80211_sta * sta,enum wmi_tdls_peer_state state)942 static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
943 				       struct ieee80211_sta *sta,
944 				       enum wmi_tdls_peer_state state)
945 {
946 	int ret;
947 	struct wmi_tdls_peer_update_cmd_arg arg = {};
948 	struct wmi_tdls_peer_capab_arg cap = {};
949 	struct wmi_channel_arg chan_arg = {};
950 
951 	lockdep_assert_held(&ar->conf_mutex);
952 
953 	arg.vdev_id = vdev_id;
954 	arg.peer_state = state;
955 	ether_addr_copy(arg.addr, sta->addr);
956 
957 	cap.peer_max_sp = sta->max_sp;
958 	cap.peer_uapsd_queues = sta->uapsd_queues;
959 
960 	if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
961 	    !sta->tdls_initiator)
962 		cap.is_peer_responder = 1;
963 
964 	ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
965 	if (ret) {
966 		ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
967 			    arg.addr, vdev_id, ret);
968 		return ret;
969 	}
970 
971 	return 0;
972 }
973 
974 /************************/
975 /* Interface management */
976 /************************/
977 
ath10k_mac_vif_beacon_free(struct ath10k_vif * arvif)978 void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
979 {
980 	struct ath10k *ar = arvif->ar;
981 
982 	lockdep_assert_held(&ar->data_lock);
983 
984 	if (!arvif->beacon)
985 		return;
986 
987 	if (!arvif->beacon_buf)
988 		dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
989 				 arvif->beacon->len, DMA_TO_DEVICE);
990 
991 	if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
992 		    arvif->beacon_state != ATH10K_BEACON_SENT))
993 		return;
994 
995 	dev_kfree_skb_any(arvif->beacon);
996 
997 	arvif->beacon = NULL;
998 	arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
999 }
1000 
ath10k_mac_vif_beacon_cleanup(struct ath10k_vif * arvif)1001 static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
1002 {
1003 	struct ath10k *ar = arvif->ar;
1004 
1005 	lockdep_assert_held(&ar->data_lock);
1006 
1007 	ath10k_mac_vif_beacon_free(arvif);
1008 
1009 	if (arvif->beacon_buf) {
1010 		if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
1011 			kfree(arvif->beacon_buf);
1012 		else
1013 			dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
1014 					  arvif->beacon_buf,
1015 					  arvif->beacon_paddr);
1016 		arvif->beacon_buf = NULL;
1017 	}
1018 }
1019 
ath10k_vdev_setup_sync(struct ath10k * ar)1020 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
1021 {
1022 	unsigned long time_left;
1023 
1024 	lockdep_assert_held(&ar->conf_mutex);
1025 
1026 	if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
1027 		return -ESHUTDOWN;
1028 
1029 	time_left = wait_for_completion_timeout(&ar->vdev_setup_done,
1030 						ATH10K_VDEV_SETUP_TIMEOUT_HZ);
1031 	if (time_left == 0)
1032 		return -ETIMEDOUT;
1033 
1034 	return ar->last_wmi_vdev_start_status;
1035 }
1036 
ath10k_vdev_delete_sync(struct ath10k * ar)1037 static inline int ath10k_vdev_delete_sync(struct ath10k *ar)
1038 {
1039 	unsigned long time_left;
1040 
1041 	lockdep_assert_held(&ar->conf_mutex);
1042 
1043 	if (!test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map))
1044 		return 0;
1045 
1046 	if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
1047 		return -ESHUTDOWN;
1048 
1049 	time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
1050 						ATH10K_VDEV_DELETE_TIMEOUT_HZ);
1051 	if (time_left == 0)
1052 		return -ETIMEDOUT;
1053 
1054 	return 0;
1055 }
1056 
ath10k_monitor_vdev_start(struct ath10k * ar,int vdev_id)1057 static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
1058 {
1059 	struct cfg80211_chan_def *chandef = NULL;
1060 	struct ieee80211_channel *channel = NULL;
1061 	struct wmi_vdev_start_request_arg arg = {};
1062 	int ret = 0;
1063 
1064 	lockdep_assert_held(&ar->conf_mutex);
1065 
1066 	ieee80211_iter_chan_contexts_atomic(ar->hw,
1067 					    ath10k_mac_get_any_chandef_iter,
1068 					    &chandef);
1069 	if (WARN_ON_ONCE(!chandef))
1070 		return -ENOENT;
1071 
1072 	channel = chandef->chan;
1073 
1074 	arg.vdev_id = vdev_id;
1075 	arg.channel.freq = channel->center_freq;
1076 	arg.channel.band_center_freq1 = chandef->center_freq1;
1077 	arg.channel.band_center_freq2 = chandef->center_freq2;
1078 
1079 	/* TODO setup this dynamically, what in case we
1080 	 * don't have any vifs?
1081 	 */
1082 	arg.channel.mode = chan_to_phymode(chandef);
1083 	arg.channel.chan_radar =
1084 			!!(channel->flags & IEEE80211_CHAN_RADAR);
1085 
1086 	arg.channel.min_power = 0;
1087 	arg.channel.max_power = channel->max_power * 2;
1088 	arg.channel.max_reg_power = channel->max_reg_power * 2;
1089 	arg.channel.max_antenna_gain = channel->max_antenna_gain;
1090 
1091 	reinit_completion(&ar->vdev_setup_done);
1092 	reinit_completion(&ar->vdev_delete_done);
1093 
1094 	ret = ath10k_wmi_vdev_start(ar, &arg);
1095 	if (ret) {
1096 		ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
1097 			    vdev_id, ret);
1098 		return ret;
1099 	}
1100 
1101 	ret = ath10k_vdev_setup_sync(ar);
1102 	if (ret) {
1103 		ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
1104 			    vdev_id, ret);
1105 		return ret;
1106 	}
1107 
1108 	ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
1109 	if (ret) {
1110 		ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
1111 			    vdev_id, ret);
1112 		goto vdev_stop;
1113 	}
1114 
1115 	ar->monitor_vdev_id = vdev_id;
1116 
1117 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
1118 		   ar->monitor_vdev_id);
1119 	return 0;
1120 
1121 vdev_stop:
1122 	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1123 	if (ret)
1124 		ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
1125 			    ar->monitor_vdev_id, ret);
1126 
1127 	return ret;
1128 }
1129 
ath10k_monitor_vdev_stop(struct ath10k * ar)1130 static int ath10k_monitor_vdev_stop(struct ath10k *ar)
1131 {
1132 	int ret = 0;
1133 
1134 	lockdep_assert_held(&ar->conf_mutex);
1135 
1136 	ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1137 	if (ret)
1138 		ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
1139 			    ar->monitor_vdev_id, ret);
1140 
1141 	reinit_completion(&ar->vdev_setup_done);
1142 	reinit_completion(&ar->vdev_delete_done);
1143 
1144 	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1145 	if (ret)
1146 		ath10k_warn(ar, "failed to request monitor vdev %i stop: %d\n",
1147 			    ar->monitor_vdev_id, ret);
1148 
1149 	ret = ath10k_vdev_setup_sync(ar);
1150 	if (ret)
1151 		ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
1152 			    ar->monitor_vdev_id, ret);
1153 
1154 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
1155 		   ar->monitor_vdev_id);
1156 	return ret;
1157 }
1158 
ath10k_monitor_vdev_create(struct ath10k * ar)1159 static int ath10k_monitor_vdev_create(struct ath10k *ar)
1160 {
1161 	int bit, ret = 0;
1162 
1163 	lockdep_assert_held(&ar->conf_mutex);
1164 
1165 	if (ar->free_vdev_map == 0) {
1166 		ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
1167 		return -ENOMEM;
1168 	}
1169 
1170 	bit = __ffs64(ar->free_vdev_map);
1171 
1172 	ar->monitor_vdev_id = bit;
1173 
1174 	ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
1175 				     WMI_VDEV_TYPE_MONITOR,
1176 				     0, ar->mac_addr);
1177 	if (ret) {
1178 		ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
1179 			    ar->monitor_vdev_id, ret);
1180 		return ret;
1181 	}
1182 
1183 	ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1184 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
1185 		   ar->monitor_vdev_id);
1186 
1187 	return 0;
1188 }
1189 
ath10k_monitor_vdev_delete(struct ath10k * ar)1190 static int ath10k_monitor_vdev_delete(struct ath10k *ar)
1191 {
1192 	int ret = 0;
1193 
1194 	lockdep_assert_held(&ar->conf_mutex);
1195 
1196 	ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1197 	if (ret) {
1198 		ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
1199 			    ar->monitor_vdev_id, ret);
1200 		return ret;
1201 	}
1202 
1203 	ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
1204 
1205 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
1206 		   ar->monitor_vdev_id);
1207 	return ret;
1208 }
1209 
ath10k_monitor_start(struct ath10k * ar)1210 static int ath10k_monitor_start(struct ath10k *ar)
1211 {
1212 	int ret;
1213 
1214 	lockdep_assert_held(&ar->conf_mutex);
1215 
1216 	ret = ath10k_monitor_vdev_create(ar);
1217 	if (ret) {
1218 		ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
1219 		return ret;
1220 	}
1221 
1222 	ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
1223 	if (ret) {
1224 		ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
1225 		ath10k_monitor_vdev_delete(ar);
1226 		return ret;
1227 	}
1228 
1229 	ar->monitor_started = true;
1230 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
1231 
1232 	return 0;
1233 }
1234 
ath10k_monitor_stop(struct ath10k * ar)1235 static int ath10k_monitor_stop(struct ath10k *ar)
1236 {
1237 	int ret;
1238 
1239 	lockdep_assert_held(&ar->conf_mutex);
1240 
1241 	ret = ath10k_monitor_vdev_stop(ar);
1242 	if (ret) {
1243 		ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
1244 		return ret;
1245 	}
1246 
1247 	ret = ath10k_monitor_vdev_delete(ar);
1248 	if (ret) {
1249 		ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
1250 		return ret;
1251 	}
1252 
1253 	ar->monitor_started = false;
1254 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
1255 
1256 	return 0;
1257 }
1258 
ath10k_mac_monitor_vdev_is_needed(struct ath10k * ar)1259 static bool ath10k_mac_monitor_vdev_is_needed(struct ath10k *ar)
1260 {
1261 	int num_ctx;
1262 
1263 	/* At least one chanctx is required to derive a channel to start
1264 	 * monitor vdev on.
1265 	 */
1266 	num_ctx = ath10k_mac_num_chanctxs(ar);
1267 	if (num_ctx == 0)
1268 		return false;
1269 
1270 	/* If there's already an existing special monitor interface then don't
1271 	 * bother creating another monitor vdev.
1272 	 */
1273 	if (ar->monitor_arvif)
1274 		return false;
1275 
1276 	return ar->monitor ||
1277 	       (!test_bit(ATH10K_FW_FEATURE_ALLOWS_MESH_BCAST,
1278 			  ar->running_fw->fw_file.fw_features) &&
1279 		(ar->filter_flags & (FIF_OTHER_BSS | FIF_MCAST_ACTION))) ||
1280 	       test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1281 }
1282 
ath10k_mac_monitor_vdev_is_allowed(struct ath10k * ar)1283 static bool ath10k_mac_monitor_vdev_is_allowed(struct ath10k *ar)
1284 {
1285 	int num_ctx;
1286 
1287 	num_ctx = ath10k_mac_num_chanctxs(ar);
1288 
1289 	/* FIXME: Current interface combinations and cfg80211/mac80211 code
1290 	 * shouldn't allow this but make sure to prevent handling the following
1291 	 * case anyway since multi-channel DFS hasn't been tested at all.
1292 	 */
1293 	if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags) && num_ctx > 1)
1294 		return false;
1295 
1296 	return true;
1297 }
1298 
ath10k_monitor_recalc(struct ath10k * ar)1299 static int ath10k_monitor_recalc(struct ath10k *ar)
1300 {
1301 	bool needed;
1302 	bool allowed;
1303 	int ret;
1304 
1305 	lockdep_assert_held(&ar->conf_mutex);
1306 
1307 	needed = ath10k_mac_monitor_vdev_is_needed(ar);
1308 	allowed = ath10k_mac_monitor_vdev_is_allowed(ar);
1309 
1310 	ath10k_dbg(ar, ATH10K_DBG_MAC,
1311 		   "mac monitor recalc started? %d needed? %d allowed? %d\n",
1312 		   ar->monitor_started, needed, allowed);
1313 
1314 	if (WARN_ON(needed && !allowed)) {
1315 		if (ar->monitor_started) {
1316 			ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopping disallowed monitor\n");
1317 
1318 			ret = ath10k_monitor_stop(ar);
1319 			if (ret)
1320 				ath10k_warn(ar, "failed to stop disallowed monitor: %d\n",
1321 					    ret);
1322 				/* not serious */
1323 		}
1324 
1325 		return -EPERM;
1326 	}
1327 
1328 	if (needed == ar->monitor_started)
1329 		return 0;
1330 
1331 	if (needed)
1332 		return ath10k_monitor_start(ar);
1333 	else
1334 		return ath10k_monitor_stop(ar);
1335 }
1336 
ath10k_mac_can_set_cts_prot(struct ath10k_vif * arvif)1337 static bool ath10k_mac_can_set_cts_prot(struct ath10k_vif *arvif)
1338 {
1339 	struct ath10k *ar = arvif->ar;
1340 
1341 	lockdep_assert_held(&ar->conf_mutex);
1342 
1343 	if (!arvif->is_started) {
1344 		ath10k_dbg(ar, ATH10K_DBG_MAC, "defer cts setup, vdev is not ready yet\n");
1345 		return false;
1346 	}
1347 
1348 	return true;
1349 }
1350 
ath10k_mac_set_cts_prot(struct ath10k_vif * arvif)1351 static int ath10k_mac_set_cts_prot(struct ath10k_vif *arvif)
1352 {
1353 	struct ath10k *ar = arvif->ar;
1354 	u32 vdev_param;
1355 
1356 	lockdep_assert_held(&ar->conf_mutex);
1357 
1358 	vdev_param = ar->wmi.vdev_param->protection_mode;
1359 
1360 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_protection %d\n",
1361 		   arvif->vdev_id, arvif->use_cts_prot);
1362 
1363 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1364 					 arvif->use_cts_prot ? 1 : 0);
1365 }
1366 
ath10k_recalc_rtscts_prot(struct ath10k_vif * arvif)1367 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
1368 {
1369 	struct ath10k *ar = arvif->ar;
1370 	u32 vdev_param, rts_cts = 0;
1371 
1372 	lockdep_assert_held(&ar->conf_mutex);
1373 
1374 	vdev_param = ar->wmi.vdev_param->enable_rtscts;
1375 
1376 	rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
1377 
1378 	if (arvif->num_legacy_stations > 0)
1379 		rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
1380 			      WMI_RTSCTS_PROFILE);
1381 	else
1382 		rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
1383 			      WMI_RTSCTS_PROFILE);
1384 
1385 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1386 		   arvif->vdev_id, rts_cts);
1387 
1388 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1389 					 rts_cts);
1390 }
1391 
ath10k_start_cac(struct ath10k * ar)1392 static int ath10k_start_cac(struct ath10k *ar)
1393 {
1394 	int ret;
1395 
1396 	lockdep_assert_held(&ar->conf_mutex);
1397 
1398 	set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1399 
1400 	ret = ath10k_monitor_recalc(ar);
1401 	if (ret) {
1402 		ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
1403 		clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1404 		return ret;
1405 	}
1406 
1407 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
1408 		   ar->monitor_vdev_id);
1409 
1410 	return 0;
1411 }
1412 
ath10k_stop_cac(struct ath10k * ar)1413 static int ath10k_stop_cac(struct ath10k *ar)
1414 {
1415 	lockdep_assert_held(&ar->conf_mutex);
1416 
1417 	/* CAC is not running - do nothing */
1418 	if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
1419 		return 0;
1420 
1421 	clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1422 	ath10k_monitor_stop(ar);
1423 
1424 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
1425 
1426 	return 0;
1427 }
1428 
ath10k_mac_has_radar_iter(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf,void * data)1429 static void ath10k_mac_has_radar_iter(struct ieee80211_hw *hw,
1430 				      struct ieee80211_chanctx_conf *conf,
1431 				      void *data)
1432 {
1433 	bool *ret = data;
1434 
1435 	if (!*ret && conf->radar_enabled)
1436 		*ret = true;
1437 }
1438 
ath10k_mac_has_radar_enabled(struct ath10k * ar)1439 static bool ath10k_mac_has_radar_enabled(struct ath10k *ar)
1440 {
1441 	bool has_radar = false;
1442 
1443 	ieee80211_iter_chan_contexts_atomic(ar->hw,
1444 					    ath10k_mac_has_radar_iter,
1445 					    &has_radar);
1446 
1447 	return has_radar;
1448 }
1449 
ath10k_recalc_radar_detection(struct ath10k * ar)1450 static void ath10k_recalc_radar_detection(struct ath10k *ar)
1451 {
1452 	int ret;
1453 
1454 	lockdep_assert_held(&ar->conf_mutex);
1455 
1456 	ath10k_stop_cac(ar);
1457 
1458 	if (!ath10k_mac_has_radar_enabled(ar))
1459 		return;
1460 
1461 	if (ar->num_started_vdevs > 0)
1462 		return;
1463 
1464 	ret = ath10k_start_cac(ar);
1465 	if (ret) {
1466 		/*
1467 		 * Not possible to start CAC on current channel so starting
1468 		 * radiation is not allowed, make this channel DFS_UNAVAILABLE
1469 		 * by indicating that radar was detected.
1470 		 */
1471 		ath10k_warn(ar, "failed to start CAC: %d\n", ret);
1472 		ieee80211_radar_detected(ar->hw, NULL);
1473 	}
1474 }
1475 
ath10k_vdev_stop(struct ath10k_vif * arvif)1476 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
1477 {
1478 	struct ath10k *ar = arvif->ar;
1479 	int ret;
1480 
1481 	lockdep_assert_held(&ar->conf_mutex);
1482 
1483 	reinit_completion(&ar->vdev_setup_done);
1484 	reinit_completion(&ar->vdev_delete_done);
1485 
1486 	ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
1487 	if (ret) {
1488 		ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
1489 			    arvif->vdev_id, ret);
1490 		return ret;
1491 	}
1492 
1493 	ret = ath10k_vdev_setup_sync(ar);
1494 	if (ret) {
1495 		ath10k_warn(ar, "failed to synchronize setup for vdev %i: %d\n",
1496 			    arvif->vdev_id, ret);
1497 		return ret;
1498 	}
1499 
1500 	WARN_ON(ar->num_started_vdevs == 0);
1501 
1502 	if (ar->num_started_vdevs != 0) {
1503 		ar->num_started_vdevs--;
1504 		ath10k_recalc_radar_detection(ar);
1505 	}
1506 
1507 	return ret;
1508 }
1509 
ath10k_vdev_start_restart(struct ath10k_vif * arvif,const struct cfg80211_chan_def * chandef,bool restart)1510 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif,
1511 				     const struct cfg80211_chan_def *chandef,
1512 				     bool restart)
1513 {
1514 	struct ath10k *ar = arvif->ar;
1515 	struct wmi_vdev_start_request_arg arg = {};
1516 	int ret = 0;
1517 
1518 	lockdep_assert_held(&ar->conf_mutex);
1519 
1520 	reinit_completion(&ar->vdev_setup_done);
1521 	reinit_completion(&ar->vdev_delete_done);
1522 
1523 	arg.vdev_id = arvif->vdev_id;
1524 	arg.dtim_period = arvif->dtim_period;
1525 	arg.bcn_intval = arvif->beacon_interval;
1526 
1527 	arg.channel.freq = chandef->chan->center_freq;
1528 	arg.channel.band_center_freq1 = chandef->center_freq1;
1529 	arg.channel.band_center_freq2 = chandef->center_freq2;
1530 	arg.channel.mode = chan_to_phymode(chandef);
1531 
1532 	arg.channel.min_power = 0;
1533 	arg.channel.max_power = chandef->chan->max_power * 2;
1534 	arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1535 	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain;
1536 
1537 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1538 		arg.ssid = arvif->u.ap.ssid;
1539 		arg.ssid_len = arvif->u.ap.ssid_len;
1540 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1541 
1542 		/* For now allow DFS for AP mode */
1543 		arg.channel.chan_radar =
1544 			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1545 	} else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1546 		arg.ssid = arvif->vif->cfg.ssid;
1547 		arg.ssid_len = arvif->vif->cfg.ssid_len;
1548 	}
1549 
1550 	ath10k_dbg(ar, ATH10K_DBG_MAC,
1551 		   "mac vdev %d start center_freq %d phymode %s\n",
1552 		   arg.vdev_id, arg.channel.freq,
1553 		   ath10k_wmi_phymode_str(arg.channel.mode));
1554 
1555 	if (restart)
1556 		ret = ath10k_wmi_vdev_restart(ar, &arg);
1557 	else
1558 		ret = ath10k_wmi_vdev_start(ar, &arg);
1559 
1560 	if (ret) {
1561 		ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
1562 			    arg.vdev_id, ret);
1563 		return ret;
1564 	}
1565 
1566 	ret = ath10k_vdev_setup_sync(ar);
1567 	if (ret) {
1568 		ath10k_warn(ar,
1569 			    "failed to synchronize setup for vdev %i restart %d: %d\n",
1570 			    arg.vdev_id, restart, ret);
1571 		return ret;
1572 	}
1573 
1574 	ar->num_started_vdevs++;
1575 	ath10k_recalc_radar_detection(ar);
1576 
1577 	return ret;
1578 }
1579 
ath10k_vdev_start(struct ath10k_vif * arvif,const struct cfg80211_chan_def * def)1580 static int ath10k_vdev_start(struct ath10k_vif *arvif,
1581 			     const struct cfg80211_chan_def *def)
1582 {
1583 	return ath10k_vdev_start_restart(arvif, def, false);
1584 }
1585 
ath10k_vdev_restart(struct ath10k_vif * arvif,const struct cfg80211_chan_def * def)1586 static int ath10k_vdev_restart(struct ath10k_vif *arvif,
1587 			       const struct cfg80211_chan_def *def)
1588 {
1589 	return ath10k_vdev_start_restart(arvif, def, true);
1590 }
1591 
ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif * arvif,struct sk_buff * bcn)1592 static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1593 				       struct sk_buff *bcn)
1594 {
1595 	struct ath10k *ar = arvif->ar;
1596 	struct ieee80211_mgmt *mgmt;
1597 	const u8 *p2p_ie;
1598 	int ret;
1599 
1600 	if (arvif->vif->type != NL80211_IFTYPE_AP || !arvif->vif->p2p)
1601 		return 0;
1602 
1603 	mgmt = (void *)bcn->data;
1604 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1605 					 mgmt->u.beacon.variable,
1606 					 bcn->len - (mgmt->u.beacon.variable -
1607 						     bcn->data));
1608 	if (!p2p_ie)
1609 		return -ENOENT;
1610 
1611 	ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1612 	if (ret) {
1613 		ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1614 			    arvif->vdev_id, ret);
1615 		return ret;
1616 	}
1617 
1618 	return 0;
1619 }
1620 
ath10k_mac_remove_vendor_ie(struct sk_buff * skb,unsigned int oui,u8 oui_type,size_t ie_offset)1621 static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1622 				       u8 oui_type, size_t ie_offset)
1623 {
1624 	size_t len;
1625 	const u8 *next;
1626 	const u8 *end;
1627 	u8 *ie;
1628 
1629 	if (WARN_ON(skb->len < ie_offset))
1630 		return -EINVAL;
1631 
1632 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1633 					   skb->data + ie_offset,
1634 					   skb->len - ie_offset);
1635 	if (!ie)
1636 		return -ENOENT;
1637 
1638 	len = ie[1] + 2;
1639 	end = skb->data + skb->len;
1640 	next = ie + len;
1641 
1642 	if (WARN_ON(next > end))
1643 		return -EINVAL;
1644 
1645 	memmove(ie, next, end - next);
1646 	skb_trim(skb, skb->len - len);
1647 
1648 	return 0;
1649 }
1650 
ath10k_mac_setup_bcn_tmpl(struct ath10k_vif * arvif)1651 static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1652 {
1653 	struct ath10k *ar = arvif->ar;
1654 	struct ieee80211_hw *hw = ar->hw;
1655 	struct ieee80211_vif *vif = arvif->vif;
1656 	struct ieee80211_mutable_offsets offs = {};
1657 	struct sk_buff *bcn;
1658 	int ret;
1659 
1660 	if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1661 		return 0;
1662 
1663 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1664 	    arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1665 		return 0;
1666 
1667 	bcn = ieee80211_beacon_get_template(hw, vif, &offs, 0);
1668 	if (!bcn) {
1669 		ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1670 		return -EPERM;
1671 	}
1672 
1673 	ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1674 	if (ret) {
1675 		ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1676 		kfree_skb(bcn);
1677 		return ret;
1678 	}
1679 
1680 	/* P2P IE is inserted by firmware automatically (as configured above)
1681 	 * so remove it from the base beacon template to avoid duplicate P2P
1682 	 * IEs in beacon frames.
1683 	 */
1684 	ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1685 				    offsetof(struct ieee80211_mgmt,
1686 					     u.beacon.variable));
1687 
1688 	ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1689 				  0, NULL, 0);
1690 	kfree_skb(bcn);
1691 
1692 	if (ret) {
1693 		ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1694 			    ret);
1695 		return ret;
1696 	}
1697 
1698 	return 0;
1699 }
1700 
ath10k_mac_setup_prb_tmpl(struct ath10k_vif * arvif)1701 static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1702 {
1703 	struct ath10k *ar = arvif->ar;
1704 	struct ieee80211_hw *hw = ar->hw;
1705 	struct ieee80211_vif *vif = arvif->vif;
1706 	struct sk_buff *prb;
1707 	int ret;
1708 
1709 	if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1710 		return 0;
1711 
1712 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1713 		return 0;
1714 
1715 	 /* For mesh, probe response and beacon share the same template */
1716 	if (ieee80211_vif_is_mesh(vif))
1717 		return 0;
1718 
1719 	prb = ieee80211_proberesp_get(hw, vif);
1720 	if (!prb) {
1721 		ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1722 		return -EPERM;
1723 	}
1724 
1725 	ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1726 	kfree_skb(prb);
1727 
1728 	if (ret) {
1729 		ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1730 			    ret);
1731 		return ret;
1732 	}
1733 
1734 	return 0;
1735 }
1736 
ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif * arvif)1737 static int ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif *arvif)
1738 {
1739 	struct ath10k *ar = arvif->ar;
1740 	struct cfg80211_chan_def def;
1741 	int ret;
1742 
1743 	/* When originally vdev is started during assign_vif_chanctx() some
1744 	 * information is missing, notably SSID. Firmware revisions with beacon
1745 	 * offloading require the SSID to be provided during vdev (re)start to
1746 	 * handle hidden SSID properly.
1747 	 *
1748 	 * Vdev restart must be done after vdev has been both started and
1749 	 * upped. Otherwise some firmware revisions (at least 10.2) fail to
1750 	 * deliver vdev restart response event causing timeouts during vdev
1751 	 * syncing in ath10k.
1752 	 *
1753 	 * Note: The vdev down/up and template reinstallation could be skipped
1754 	 * since only wmi-tlv firmware are known to have beacon offload and
1755 	 * wmi-tlv doesn't seem to misbehave like 10.2 wrt vdev restart
1756 	 * response delivery. It's probably more robust to keep it as is.
1757 	 */
1758 	if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1759 		return 0;
1760 
1761 	if (WARN_ON(!arvif->is_started))
1762 		return -EINVAL;
1763 
1764 	if (WARN_ON(!arvif->is_up))
1765 		return -EINVAL;
1766 
1767 	if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
1768 		return -EINVAL;
1769 
1770 	ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1771 	if (ret) {
1772 		ath10k_warn(ar, "failed to bring down ap vdev %i: %d\n",
1773 			    arvif->vdev_id, ret);
1774 		return ret;
1775 	}
1776 
1777 	/* Vdev down reset beacon & presp templates. Reinstall them. Otherwise
1778 	 * firmware will crash upon vdev up.
1779 	 */
1780 
1781 	ret = ath10k_mac_setup_bcn_tmpl(arvif);
1782 	if (ret) {
1783 		ath10k_warn(ar, "failed to update beacon template: %d\n", ret);
1784 		return ret;
1785 	}
1786 
1787 	ret = ath10k_mac_setup_prb_tmpl(arvif);
1788 	if (ret) {
1789 		ath10k_warn(ar, "failed to update presp template: %d\n", ret);
1790 		return ret;
1791 	}
1792 
1793 	ret = ath10k_vdev_restart(arvif, &def);
1794 	if (ret) {
1795 		ath10k_warn(ar, "failed to restart ap vdev %i: %d\n",
1796 			    arvif->vdev_id, ret);
1797 		return ret;
1798 	}
1799 
1800 	ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1801 				 arvif->bssid);
1802 	if (ret) {
1803 		ath10k_warn(ar, "failed to bring up ap vdev %i: %d\n",
1804 			    arvif->vdev_id, ret);
1805 		return ret;
1806 	}
1807 
1808 	return 0;
1809 }
1810 
ath10k_control_beaconing(struct ath10k_vif * arvif,struct ieee80211_bss_conf * info)1811 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1812 				     struct ieee80211_bss_conf *info)
1813 {
1814 	struct ath10k *ar = arvif->ar;
1815 	int ret = 0;
1816 
1817 	lockdep_assert_held(&arvif->ar->conf_mutex);
1818 
1819 	if (!info->enable_beacon) {
1820 		ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1821 		if (ret)
1822 			ath10k_warn(ar, "failed to down vdev_id %i: %d\n",
1823 				    arvif->vdev_id, ret);
1824 
1825 		arvif->is_up = false;
1826 
1827 		spin_lock_bh(&arvif->ar->data_lock);
1828 		ath10k_mac_vif_beacon_free(arvif);
1829 		spin_unlock_bh(&arvif->ar->data_lock);
1830 
1831 		return;
1832 	}
1833 
1834 	arvif->tx_seq_no = 0x1000;
1835 
1836 	arvif->aid = 0;
1837 	ether_addr_copy(arvif->bssid, info->bssid);
1838 
1839 	ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1840 				 arvif->bssid);
1841 	if (ret) {
1842 		ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1843 			    arvif->vdev_id, ret);
1844 		return;
1845 	}
1846 
1847 	arvif->is_up = true;
1848 
1849 	ret = ath10k_mac_vif_fix_hidden_ssid(arvif);
1850 	if (ret) {
1851 		ath10k_warn(ar, "failed to fix hidden ssid for vdev %i, expect trouble: %d\n",
1852 			    arvif->vdev_id, ret);
1853 		return;
1854 	}
1855 
1856 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1857 }
1858 
ath10k_control_ibss(struct ath10k_vif * arvif,struct ieee80211_vif * vif)1859 static void ath10k_control_ibss(struct ath10k_vif *arvif,
1860 				struct ieee80211_vif *vif)
1861 {
1862 	struct ath10k *ar = arvif->ar;
1863 	u32 vdev_param;
1864 	int ret = 0;
1865 
1866 	lockdep_assert_held(&arvif->ar->conf_mutex);
1867 
1868 	if (!vif->cfg.ibss_joined) {
1869 		if (is_zero_ether_addr(arvif->bssid))
1870 			return;
1871 
1872 		eth_zero_addr(arvif->bssid);
1873 
1874 		return;
1875 	}
1876 
1877 	vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1878 	ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1879 					ATH10K_DEFAULT_ATIM);
1880 	if (ret)
1881 		ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1882 			    arvif->vdev_id, ret);
1883 }
1884 
ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif * arvif)1885 static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1886 {
1887 	struct ath10k *ar = arvif->ar;
1888 	u32 param;
1889 	u32 value;
1890 	int ret;
1891 
1892 	lockdep_assert_held(&arvif->ar->conf_mutex);
1893 
1894 	if (arvif->u.sta.uapsd)
1895 		value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1896 	else
1897 		value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1898 
1899 	param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1900 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1901 	if (ret) {
1902 		ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1903 			    value, arvif->vdev_id, ret);
1904 		return ret;
1905 	}
1906 
1907 	return 0;
1908 }
1909 
ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif * arvif)1910 static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1911 {
1912 	struct ath10k *ar = arvif->ar;
1913 	u32 param;
1914 	u32 value;
1915 	int ret;
1916 
1917 	lockdep_assert_held(&arvif->ar->conf_mutex);
1918 
1919 	if (arvif->u.sta.uapsd)
1920 		value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1921 	else
1922 		value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1923 
1924 	param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1925 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1926 					  param, value);
1927 	if (ret) {
1928 		ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1929 			    value, arvif->vdev_id, ret);
1930 		return ret;
1931 	}
1932 
1933 	return 0;
1934 }
1935 
ath10k_mac_num_vifs_started(struct ath10k * ar)1936 static int ath10k_mac_num_vifs_started(struct ath10k *ar)
1937 {
1938 	struct ath10k_vif *arvif;
1939 	int num = 0;
1940 
1941 	lockdep_assert_held(&ar->conf_mutex);
1942 
1943 	list_for_each_entry(arvif, &ar->arvifs, list)
1944 		if (arvif->is_started)
1945 			num++;
1946 
1947 	return num;
1948 }
1949 
ath10k_mac_vif_setup_ps(struct ath10k_vif * arvif)1950 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1951 {
1952 	struct ath10k *ar = arvif->ar;
1953 	struct ieee80211_vif *vif = arvif->vif;
1954 	struct ieee80211_conf *conf = &ar->hw->conf;
1955 	enum wmi_sta_powersave_param param;
1956 	enum wmi_sta_ps_mode psmode;
1957 	int ret;
1958 	int ps_timeout;
1959 	bool enable_ps;
1960 
1961 	lockdep_assert_held(&arvif->ar->conf_mutex);
1962 
1963 	if (arvif->vif->type != NL80211_IFTYPE_STATION)
1964 		return 0;
1965 
1966 	enable_ps = arvif->ps;
1967 
1968 	if (enable_ps && ath10k_mac_num_vifs_started(ar) > 1 &&
1969 	    !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1970 		      ar->running_fw->fw_file.fw_features)) {
1971 		ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1972 			    arvif->vdev_id);
1973 		enable_ps = false;
1974 	}
1975 
1976 	if (!arvif->is_started) {
1977 		/* mac80211 can update vif powersave state while disconnected.
1978 		 * Firmware doesn't behave nicely and consumes more power than
1979 		 * necessary if PS is disabled on a non-started vdev. Hence
1980 		 * force-enable PS for non-running vdevs.
1981 		 */
1982 		psmode = WMI_STA_PS_MODE_ENABLED;
1983 	} else if (enable_ps) {
1984 		psmode = WMI_STA_PS_MODE_ENABLED;
1985 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1986 
1987 		ps_timeout = conf->dynamic_ps_timeout;
1988 		if (ps_timeout == 0) {
1989 			/* Firmware doesn't like 0 */
1990 			ps_timeout = ieee80211_tu_to_usec(
1991 				vif->bss_conf.beacon_int) / 1000;
1992 		}
1993 
1994 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1995 						  ps_timeout);
1996 		if (ret) {
1997 			ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1998 				    arvif->vdev_id, ret);
1999 			return ret;
2000 		}
2001 	} else {
2002 		psmode = WMI_STA_PS_MODE_DISABLED;
2003 	}
2004 
2005 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
2006 		   arvif->vdev_id, psmode ? "enable" : "disable");
2007 
2008 	ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
2009 	if (ret) {
2010 		ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
2011 			    psmode, arvif->vdev_id, ret);
2012 		return ret;
2013 	}
2014 
2015 	return 0;
2016 }
2017 
ath10k_mac_vif_disable_keepalive(struct ath10k_vif * arvif)2018 static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
2019 {
2020 	struct ath10k *ar = arvif->ar;
2021 	struct wmi_sta_keepalive_arg arg = {};
2022 	int ret;
2023 
2024 	lockdep_assert_held(&arvif->ar->conf_mutex);
2025 
2026 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
2027 		return 0;
2028 
2029 	if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
2030 		return 0;
2031 
2032 	/* Some firmware revisions have a bug and ignore the `enabled` field.
2033 	 * Instead use the interval to disable the keepalive.
2034 	 */
2035 	arg.vdev_id = arvif->vdev_id;
2036 	arg.enabled = 1;
2037 	arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
2038 	arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
2039 
2040 	ret = ath10k_wmi_sta_keepalive(ar, &arg);
2041 	if (ret) {
2042 		ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
2043 			    arvif->vdev_id, ret);
2044 		return ret;
2045 	}
2046 
2047 	return 0;
2048 }
2049 
ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif * arvif)2050 static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
2051 {
2052 	struct ath10k *ar = arvif->ar;
2053 	struct ieee80211_vif *vif = arvif->vif;
2054 	int ret;
2055 
2056 	lockdep_assert_held(&arvif->ar->conf_mutex);
2057 
2058 	if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
2059 		return;
2060 
2061 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
2062 		return;
2063 
2064 	if (!vif->bss_conf.csa_active)
2065 		return;
2066 
2067 	if (!arvif->is_up)
2068 		return;
2069 
2070 	if (!ieee80211_beacon_cntdwn_is_complete(vif, 0)) {
2071 		ieee80211_beacon_update_cntdwn(vif, 0);
2072 
2073 		ret = ath10k_mac_setup_bcn_tmpl(arvif);
2074 		if (ret)
2075 			ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
2076 				    ret);
2077 
2078 		ret = ath10k_mac_setup_prb_tmpl(arvif);
2079 		if (ret)
2080 			ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
2081 				    ret);
2082 	} else {
2083 		ieee80211_csa_finish(vif, 0);
2084 	}
2085 }
2086 
ath10k_mac_vif_ap_csa_work(struct work_struct * work)2087 static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
2088 {
2089 	struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2090 						ap_csa_work);
2091 	struct ath10k *ar = arvif->ar;
2092 
2093 	mutex_lock(&ar->conf_mutex);
2094 	ath10k_mac_vif_ap_csa_count_down(arvif);
2095 	mutex_unlock(&ar->conf_mutex);
2096 }
2097 
ath10k_mac_handle_beacon_iter(void * data,u8 * mac,struct ieee80211_vif * vif)2098 static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
2099 					  struct ieee80211_vif *vif)
2100 {
2101 	struct sk_buff *skb = data;
2102 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
2103 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2104 
2105 	if (vif->type != NL80211_IFTYPE_STATION)
2106 		return;
2107 
2108 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
2109 		return;
2110 
2111 	cancel_delayed_work(&arvif->connection_loss_work);
2112 }
2113 
ath10k_mac_handle_beacon(struct ath10k * ar,struct sk_buff * skb)2114 void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
2115 {
2116 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
2117 						   ATH10K_ITER_NORMAL_FLAGS,
2118 						   ath10k_mac_handle_beacon_iter,
2119 						   skb);
2120 }
2121 
ath10k_mac_handle_beacon_miss_iter(void * data,u8 * mac,struct ieee80211_vif * vif)2122 static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
2123 					       struct ieee80211_vif *vif)
2124 {
2125 	u32 *vdev_id = data;
2126 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2127 	struct ath10k *ar = arvif->ar;
2128 	struct ieee80211_hw *hw = ar->hw;
2129 
2130 	if (arvif->vdev_id != *vdev_id)
2131 		return;
2132 
2133 	if (!arvif->is_up)
2134 		return;
2135 
2136 	ieee80211_beacon_loss(vif);
2137 
2138 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
2139 	 * (done by mac80211) succeeds but beacons do not resume then it
2140 	 * doesn't make sense to continue operation. Queue connection loss work
2141 	 * which can be cancelled when beacon is received.
2142 	 */
2143 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
2144 				     ATH10K_CONNECTION_LOSS_HZ);
2145 }
2146 
ath10k_mac_handle_beacon_miss(struct ath10k * ar,u32 vdev_id)2147 void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
2148 {
2149 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
2150 						   ATH10K_ITER_NORMAL_FLAGS,
2151 						   ath10k_mac_handle_beacon_miss_iter,
2152 						   &vdev_id);
2153 }
2154 
ath10k_mac_vif_sta_connection_loss_work(struct work_struct * work)2155 static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
2156 {
2157 	struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2158 						connection_loss_work.work);
2159 	struct ieee80211_vif *vif = arvif->vif;
2160 
2161 	if (!arvif->is_up)
2162 		return;
2163 
2164 	ieee80211_connection_loss(vif);
2165 }
2166 
2167 /**********************/
2168 /* Station management */
2169 /**********************/
2170 
ath10k_peer_assoc_h_listen_intval(struct ath10k * ar,struct ieee80211_vif * vif)2171 static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
2172 					     struct ieee80211_vif *vif)
2173 {
2174 	/* Some firmware revisions have unstable STA powersave when listen
2175 	 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
2176 	 * generate NullFunc frames properly even if buffered frames have been
2177 	 * indicated in Beacon TIM. Firmware would seldom wake up to pull
2178 	 * buffered frames. Often pinging the device from AP would simply fail.
2179 	 *
2180 	 * As a workaround set it to 1.
2181 	 */
2182 	if (vif->type == NL80211_IFTYPE_STATION)
2183 		return 1;
2184 
2185 	return ar->hw->conf.listen_interval;
2186 }
2187 
ath10k_peer_assoc_h_basic(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct wmi_peer_assoc_complete_arg * arg)2188 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
2189 				      struct ieee80211_vif *vif,
2190 				      struct ieee80211_sta *sta,
2191 				      struct wmi_peer_assoc_complete_arg *arg)
2192 {
2193 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2194 	u32 aid;
2195 
2196 	lockdep_assert_held(&ar->conf_mutex);
2197 
2198 	if (vif->type == NL80211_IFTYPE_STATION)
2199 		aid = vif->cfg.aid;
2200 	else
2201 		aid = sta->aid;
2202 
2203 	ether_addr_copy(arg->addr, sta->addr);
2204 	arg->vdev_id = arvif->vdev_id;
2205 	arg->peer_aid = aid;
2206 	arg->peer_flags |= arvif->ar->wmi.peer_flags->auth;
2207 	arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
2208 	arg->peer_num_spatial_streams = 1;
2209 	arg->peer_caps = vif->bss_conf.assoc_capability;
2210 }
2211 
ath10k_peer_assoc_h_crypto(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct wmi_peer_assoc_complete_arg * arg)2212 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
2213 				       struct ieee80211_vif *vif,
2214 				       struct ieee80211_sta *sta,
2215 				       struct wmi_peer_assoc_complete_arg *arg)
2216 {
2217 	struct ieee80211_bss_conf *info = &vif->bss_conf;
2218 	struct cfg80211_chan_def def;
2219 	struct cfg80211_bss *bss;
2220 	const u8 *rsnie = NULL;
2221 	const u8 *wpaie = NULL;
2222 
2223 	lockdep_assert_held(&ar->conf_mutex);
2224 
2225 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2226 		return;
2227 
2228 	bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid,
2229 			       vif->cfg.ssid_len ? vif->cfg.ssid : NULL,
2230 			       vif->cfg.ssid_len,
2231 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2232 	if (bss) {
2233 		const struct cfg80211_bss_ies *ies;
2234 
2235 		rcu_read_lock();
2236 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2237 
2238 		ies = rcu_dereference(bss->ies);
2239 
2240 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2241 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2242 						ies->data,
2243 						ies->len);
2244 		rcu_read_unlock();
2245 		cfg80211_put_bss(ar->hw->wiphy, bss);
2246 	}
2247 
2248 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2249 	if (rsnie || wpaie) {
2250 		ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
2251 		arg->peer_flags |= ar->wmi.peer_flags->need_ptk_4_way;
2252 	}
2253 
2254 	if (wpaie) {
2255 		ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
2256 		arg->peer_flags |= ar->wmi.peer_flags->need_gtk_2_way;
2257 	}
2258 
2259 	if (sta->mfp &&
2260 	    test_bit(ATH10K_FW_FEATURE_MFP_SUPPORT,
2261 		     ar->running_fw->fw_file.fw_features)) {
2262 		arg->peer_flags |= ar->wmi.peer_flags->pmf;
2263 	}
2264 }
2265 
ath10k_peer_assoc_h_rates(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct wmi_peer_assoc_complete_arg * arg)2266 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
2267 				      struct ieee80211_vif *vif,
2268 				      struct ieee80211_sta *sta,
2269 				      struct wmi_peer_assoc_complete_arg *arg)
2270 {
2271 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2272 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2273 	struct cfg80211_chan_def def;
2274 	const struct ieee80211_supported_band *sband;
2275 	const struct ieee80211_rate *rates;
2276 	enum nl80211_band band;
2277 	u32 ratemask;
2278 	u8 rate;
2279 	int i;
2280 
2281 	lockdep_assert_held(&ar->conf_mutex);
2282 
2283 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2284 		return;
2285 
2286 	band = def.chan->band;
2287 	sband = ar->hw->wiphy->bands[band];
2288 	ratemask = sta->deflink.supp_rates[band];
2289 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2290 	rates = sband->bitrates;
2291 
2292 	rateset->num_rates = 0;
2293 
2294 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2295 		if (!(ratemask & 1))
2296 			continue;
2297 
2298 		rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
2299 		rateset->rates[rateset->num_rates] = rate;
2300 		rateset->num_rates++;
2301 	}
2302 }
2303 
2304 static bool
ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])2305 ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
2306 {
2307 	int nss;
2308 
2309 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2310 		if (ht_mcs_mask[nss])
2311 			return false;
2312 
2313 	return true;
2314 }
2315 
2316 static bool
ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])2317 ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
2318 {
2319 	int nss;
2320 
2321 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2322 		if (vht_mcs_mask[nss])
2323 			return false;
2324 
2325 	return true;
2326 }
2327 
ath10k_peer_assoc_h_ht(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct wmi_peer_assoc_complete_arg * arg)2328 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
2329 				   struct ieee80211_vif *vif,
2330 				   struct ieee80211_sta *sta,
2331 				   struct wmi_peer_assoc_complete_arg *arg)
2332 {
2333 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
2334 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2335 	struct cfg80211_chan_def def;
2336 	enum nl80211_band band;
2337 	const u8 *ht_mcs_mask;
2338 	const u16 *vht_mcs_mask;
2339 	int i, n;
2340 	u8 max_nss;
2341 	u32 stbc;
2342 
2343 	lockdep_assert_held(&ar->conf_mutex);
2344 
2345 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2346 		return;
2347 
2348 	if (!ht_cap->ht_supported)
2349 		return;
2350 
2351 	band = def.chan->band;
2352 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2353 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2354 
2355 	if (ath10k_peer_assoc_h_ht_masked(ht_mcs_mask) &&
2356 	    ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2357 		return;
2358 
2359 	arg->peer_flags |= ar->wmi.peer_flags->ht;
2360 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2361 				    ht_cap->ampdu_factor)) - 1;
2362 
2363 	arg->peer_mpdu_density =
2364 		ath10k_parse_mpdudensity(ht_cap->ampdu_density);
2365 
2366 	arg->peer_ht_caps = ht_cap->cap;
2367 	arg->peer_rate_caps |= WMI_RC_HT_FLAG;
2368 
2369 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2370 		arg->peer_flags |= ar->wmi.peer_flags->ldbc;
2371 
2372 	if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40) {
2373 		arg->peer_flags |= ar->wmi.peer_flags->bw40;
2374 		arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
2375 	}
2376 
2377 	if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
2378 		if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
2379 			arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2380 
2381 		if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
2382 			arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2383 	}
2384 
2385 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2386 		arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
2387 		arg->peer_flags |= ar->wmi.peer_flags->stbc;
2388 	}
2389 
2390 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2391 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2392 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2393 		stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
2394 		arg->peer_rate_caps |= stbc;
2395 		arg->peer_flags |= ar->wmi.peer_flags->stbc;
2396 	}
2397 
2398 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2399 		arg->peer_rate_caps |= WMI_RC_TS_FLAG;
2400 	else if (ht_cap->mcs.rx_mask[1])
2401 		arg->peer_rate_caps |= WMI_RC_DS_FLAG;
2402 
2403 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2404 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2405 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2406 			max_nss = (i / 8) + 1;
2407 			arg->peer_ht_rates.rates[n++] = i;
2408 		}
2409 
2410 	/*
2411 	 * This is a workaround for HT-enabled STAs which break the spec
2412 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2413 	 *
2414 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2415 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2416 	 *
2417 	 * Firmware asserts if such situation occurs.
2418 	 */
2419 	if (n == 0) {
2420 		arg->peer_ht_rates.num_rates = 8;
2421 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2422 			arg->peer_ht_rates.rates[i] = i;
2423 	} else {
2424 		arg->peer_ht_rates.num_rates = n;
2425 		arg->peer_num_spatial_streams = min(sta->deflink.rx_nss,
2426 						    max_nss);
2427 	}
2428 
2429 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2430 		   arg->addr,
2431 		   arg->peer_ht_rates.num_rates,
2432 		   arg->peer_num_spatial_streams);
2433 }
2434 
ath10k_peer_assoc_qos_ap(struct ath10k * ar,struct ath10k_vif * arvif,struct ieee80211_sta * sta)2435 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
2436 				    struct ath10k_vif *arvif,
2437 				    struct ieee80211_sta *sta)
2438 {
2439 	u32 uapsd = 0;
2440 	u32 max_sp = 0;
2441 	int ret = 0;
2442 
2443 	lockdep_assert_held(&ar->conf_mutex);
2444 
2445 	if (sta->wme && sta->uapsd_queues) {
2446 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2447 			   sta->uapsd_queues, sta->max_sp);
2448 
2449 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2450 			uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2451 				 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2452 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2453 			uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2454 				 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2455 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2456 			uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2457 				 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2458 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2459 			uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2460 				 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2461 
2462 		if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2463 			max_sp = sta->max_sp;
2464 
2465 		ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2466 						 sta->addr,
2467 						 WMI_AP_PS_PEER_PARAM_UAPSD,
2468 						 uapsd);
2469 		if (ret) {
2470 			ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
2471 				    arvif->vdev_id, ret);
2472 			return ret;
2473 		}
2474 
2475 		ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2476 						 sta->addr,
2477 						 WMI_AP_PS_PEER_PARAM_MAX_SP,
2478 						 max_sp);
2479 		if (ret) {
2480 			ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
2481 				    arvif->vdev_id, ret);
2482 			return ret;
2483 		}
2484 
2485 		/* TODO setup this based on STA listen interval and
2486 		 * beacon interval. Currently we don't know
2487 		 * sta->listen_interval - mac80211 patch required.
2488 		 * Currently use 10 seconds
2489 		 */
2490 		ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
2491 						 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
2492 						 10);
2493 		if (ret) {
2494 			ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
2495 				    arvif->vdev_id, ret);
2496 			return ret;
2497 		}
2498 	}
2499 
2500 	return 0;
2501 }
2502 
2503 static u16
ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])2504 ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2505 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2506 {
2507 	int idx_limit;
2508 	int nss;
2509 	u16 mcs_map;
2510 	u16 mcs;
2511 
2512 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2513 		mcs_map = ath10k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2514 			  vht_mcs_limit[nss];
2515 
2516 		if (mcs_map)
2517 			idx_limit = fls(mcs_map) - 1;
2518 		else
2519 			idx_limit = -1;
2520 
2521 		switch (idx_limit) {
2522 		case 0:
2523 		case 1:
2524 		case 2:
2525 		case 3:
2526 		case 4:
2527 		case 5:
2528 		case 6:
2529 		default:
2530 			/* see ath10k_mac_can_set_bitrate_mask() */
2531 			WARN_ON(1);
2532 			fallthrough;
2533 		case -1:
2534 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2535 			break;
2536 		case 7:
2537 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2538 			break;
2539 		case 8:
2540 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2541 			break;
2542 		case 9:
2543 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2544 			break;
2545 		}
2546 
2547 		tx_mcs_set &= ~(0x3 << (nss * 2));
2548 		tx_mcs_set |= mcs << (nss * 2);
2549 	}
2550 
2551 	return tx_mcs_set;
2552 }
2553 
get_160mhz_nss_from_maxrate(int rate)2554 static u32 get_160mhz_nss_from_maxrate(int rate)
2555 {
2556 	u32 nss;
2557 
2558 	switch (rate) {
2559 	case 780:
2560 		nss = 1;
2561 		break;
2562 	case 1560:
2563 		nss = 2;
2564 		break;
2565 	case 2106:
2566 		nss = 3; /* not support MCS9 from spec*/
2567 		break;
2568 	case 3120:
2569 		nss = 4;
2570 		break;
2571 	default:
2572 		 nss = 1;
2573 	}
2574 
2575 	return nss;
2576 }
2577 
ath10k_peer_assoc_h_vht(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct wmi_peer_assoc_complete_arg * arg)2578 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
2579 				    struct ieee80211_vif *vif,
2580 				    struct ieee80211_sta *sta,
2581 				    struct wmi_peer_assoc_complete_arg *arg)
2582 {
2583 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
2584 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2585 	struct ath10k_hw_params *hw = &ar->hw_params;
2586 	struct cfg80211_chan_def def;
2587 	enum nl80211_band band;
2588 	const u16 *vht_mcs_mask;
2589 	u8 ampdu_factor;
2590 	u8 max_nss, vht_mcs;
2591 	int i;
2592 
2593 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2594 		return;
2595 
2596 	if (!vht_cap->vht_supported)
2597 		return;
2598 
2599 	band = def.chan->band;
2600 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2601 
2602 	if (ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2603 		return;
2604 
2605 	arg->peer_flags |= ar->wmi.peer_flags->vht;
2606 
2607 	if (def.chan->band == NL80211_BAND_2GHZ)
2608 		arg->peer_flags |= ar->wmi.peer_flags->vht_2g;
2609 
2610 	arg->peer_vht_caps = vht_cap->cap;
2611 
2612 	ampdu_factor = (vht_cap->cap &
2613 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2614 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2615 
2616 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2617 	 * zero in VHT IE. Using it would result in degraded throughput.
2618 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2619 	 * it if VHT max_mpdu is smaller.
2620 	 */
2621 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2622 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2623 					ampdu_factor)) - 1);
2624 
2625 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2626 		arg->peer_flags |= ar->wmi.peer_flags->bw80;
2627 
2628 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160)
2629 		arg->peer_flags |= ar->wmi.peer_flags->bw160;
2630 
2631 	/* Calculate peer NSS capability from VHT capabilities if STA
2632 	 * supports VHT.
2633 	 */
2634 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2635 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2636 			  (2 * i) & 3;
2637 
2638 		if ((vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) &&
2639 		    vht_mcs_mask[i])
2640 			max_nss = i + 1;
2641 	}
2642 	arg->peer_num_spatial_streams = min(sta->deflink.rx_nss, max_nss);
2643 	arg->peer_vht_rates.rx_max_rate =
2644 		__le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2645 	arg->peer_vht_rates.rx_mcs_set =
2646 		__le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2647 	arg->peer_vht_rates.tx_max_rate =
2648 		__le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2649 	arg->peer_vht_rates.tx_mcs_set = ath10k_peer_assoc_h_vht_limit(
2650 		__le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
2651 
2652 	/* Configure bandwidth-NSS mapping to FW
2653 	 * for the chip's tx chains setting on 160Mhz bw
2654 	 */
2655 	if (arg->peer_phymode == MODE_11AC_VHT160 ||
2656 	    arg->peer_phymode == MODE_11AC_VHT80_80) {
2657 		u32 rx_nss;
2658 		u32 max_rate;
2659 
2660 		max_rate = arg->peer_vht_rates.rx_max_rate;
2661 		rx_nss = get_160mhz_nss_from_maxrate(max_rate);
2662 
2663 		if (rx_nss == 0)
2664 			rx_nss = arg->peer_num_spatial_streams;
2665 		else
2666 			rx_nss = min(arg->peer_num_spatial_streams, rx_nss);
2667 
2668 		max_rate = hw->vht160_mcs_tx_highest;
2669 		rx_nss = min(rx_nss, get_160mhz_nss_from_maxrate(max_rate));
2670 
2671 		arg->peer_bw_rxnss_override =
2672 			FIELD_PREP(WMI_PEER_NSS_MAP_ENABLE, 1) |
2673 			FIELD_PREP(WMI_PEER_NSS_160MHZ_MASK, (rx_nss - 1));
2674 
2675 		if (arg->peer_phymode == MODE_11AC_VHT80_80) {
2676 			arg->peer_bw_rxnss_override |=
2677 			FIELD_PREP(WMI_PEER_NSS_80_80MHZ_MASK, (rx_nss - 1));
2678 		}
2679 	}
2680 	ath10k_dbg(ar, ATH10K_DBG_MAC,
2681 		   "mac vht peer %pM max_mpdu %d flags 0x%x peer_rx_nss_override 0x%x\n",
2682 		   sta->addr, arg->peer_max_mpdu,
2683 		   arg->peer_flags, arg->peer_bw_rxnss_override);
2684 }
2685 
ath10k_peer_assoc_h_qos(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct wmi_peer_assoc_complete_arg * arg)2686 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
2687 				    struct ieee80211_vif *vif,
2688 				    struct ieee80211_sta *sta,
2689 				    struct wmi_peer_assoc_complete_arg *arg)
2690 {
2691 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2692 
2693 	switch (arvif->vdev_type) {
2694 	case WMI_VDEV_TYPE_AP:
2695 		if (sta->wme)
2696 			arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2697 
2698 		if (sta->wme && sta->uapsd_queues) {
2699 			arg->peer_flags |= arvif->ar->wmi.peer_flags->apsd;
2700 			arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
2701 		}
2702 		break;
2703 	case WMI_VDEV_TYPE_STA:
2704 		if (sta->wme)
2705 			arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2706 		break;
2707 	case WMI_VDEV_TYPE_IBSS:
2708 		if (sta->wme)
2709 			arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2710 		break;
2711 	default:
2712 		break;
2713 	}
2714 
2715 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
2716 		   sta->addr, !!(arg->peer_flags &
2717 		   arvif->ar->wmi.peer_flags->qos));
2718 }
2719 
ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta * sta)2720 static bool ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2721 {
2722 	return sta->deflink.supp_rates[NL80211_BAND_2GHZ] >>
2723 	       ATH10K_MAC_FIRST_OFDM_RATE_IDX;
2724 }
2725 
ath10k_mac_get_phymode_vht(struct ath10k * ar,struct ieee80211_sta * sta)2726 static enum wmi_phy_mode ath10k_mac_get_phymode_vht(struct ath10k *ar,
2727 						    struct ieee80211_sta *sta)
2728 {
2729 	struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
2730 
2731 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_160) {
2732 		switch (vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
2733 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
2734 			return MODE_11AC_VHT160;
2735 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
2736 			return MODE_11AC_VHT80_80;
2737 		default:
2738 			/* not sure if this is a valid case? */
2739 			return MODE_11AC_VHT160;
2740 		}
2741 	}
2742 
2743 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_80)
2744 		return MODE_11AC_VHT80;
2745 
2746 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2747 		return MODE_11AC_VHT40;
2748 
2749 	if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20)
2750 		return MODE_11AC_VHT20;
2751 
2752 	return MODE_UNKNOWN;
2753 }
2754 
ath10k_peer_assoc_h_phymode(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct wmi_peer_assoc_complete_arg * arg)2755 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
2756 					struct ieee80211_vif *vif,
2757 					struct ieee80211_sta *sta,
2758 					struct wmi_peer_assoc_complete_arg *arg)
2759 {
2760 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2761 	struct cfg80211_chan_def def;
2762 	enum nl80211_band band;
2763 	const u8 *ht_mcs_mask;
2764 	const u16 *vht_mcs_mask;
2765 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
2766 
2767 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2768 		return;
2769 
2770 	band = def.chan->band;
2771 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2772 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2773 
2774 	switch (band) {
2775 	case NL80211_BAND_2GHZ:
2776 		if (sta->deflink.vht_cap.vht_supported &&
2777 		    !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2778 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2779 				phymode = MODE_11AC_VHT40;
2780 			else
2781 				phymode = MODE_11AC_VHT20;
2782 		} else if (sta->deflink.ht_cap.ht_supported &&
2783 			   !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2784 			if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_40)
2785 				phymode = MODE_11NG_HT40;
2786 			else
2787 				phymode = MODE_11NG_HT20;
2788 		} else if (ath10k_mac_sta_has_ofdm_only(sta)) {
2789 			phymode = MODE_11G;
2790 		} else {
2791 			phymode = MODE_11B;
2792 		}
2793 
2794 		break;
2795 	case NL80211_BAND_5GHZ:
2796 		/*
2797 		 * Check VHT first.
2798 		 */
2799 		if (sta->deflink.vht_cap.vht_supported &&
2800 		    !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2801 			phymode = ath10k_mac_get_phymode_vht(ar, sta);
2802 		} else if (sta->deflink.ht_cap.ht_supported &&
2803 			   !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2804 			if (sta->deflink.bandwidth >= IEEE80211_STA_RX_BW_40)
2805 				phymode = MODE_11NA_HT40;
2806 			else
2807 				phymode = MODE_11NA_HT20;
2808 		} else {
2809 			phymode = MODE_11A;
2810 		}
2811 
2812 		break;
2813 	default:
2814 		break;
2815 	}
2816 
2817 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
2818 		   sta->addr, ath10k_wmi_phymode_str(phymode));
2819 
2820 	arg->peer_phymode = phymode;
2821 	WARN_ON(phymode == MODE_UNKNOWN);
2822 }
2823 
ath10k_peer_assoc_prepare(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct wmi_peer_assoc_complete_arg * arg)2824 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
2825 				     struct ieee80211_vif *vif,
2826 				     struct ieee80211_sta *sta,
2827 				     struct wmi_peer_assoc_complete_arg *arg)
2828 {
2829 	lockdep_assert_held(&ar->conf_mutex);
2830 
2831 	memset(arg, 0, sizeof(*arg));
2832 
2833 	ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
2834 	ath10k_peer_assoc_h_crypto(ar, vif, sta, arg);
2835 	ath10k_peer_assoc_h_rates(ar, vif, sta, arg);
2836 	ath10k_peer_assoc_h_ht(ar, vif, sta, arg);
2837 	ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
2838 	ath10k_peer_assoc_h_vht(ar, vif, sta, arg);
2839 	ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
2840 
2841 	return 0;
2842 }
2843 
2844 static const u32 ath10k_smps_map[] = {
2845 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2846 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2847 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2848 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2849 };
2850 
ath10k_setup_peer_smps(struct ath10k * ar,struct ath10k_vif * arvif,const u8 * addr,const struct ieee80211_sta_ht_cap * ht_cap)2851 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2852 				  const u8 *addr,
2853 				  const struct ieee80211_sta_ht_cap *ht_cap)
2854 {
2855 	int smps;
2856 
2857 	if (!ht_cap->ht_supported)
2858 		return 0;
2859 
2860 	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2861 	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2862 
2863 	if (smps >= ARRAY_SIZE(ath10k_smps_map))
2864 		return -EINVAL;
2865 
2866 	return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2867 					 ar->wmi.peer_param->smps_state,
2868 					 ath10k_smps_map[smps]);
2869 }
2870 
ath10k_mac_vif_recalc_txbf(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta_vht_cap vht_cap)2871 static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2872 				      struct ieee80211_vif *vif,
2873 				      struct ieee80211_sta_vht_cap vht_cap)
2874 {
2875 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2876 	int ret;
2877 	u32 param;
2878 	u32 value;
2879 
2880 	if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_AFTER_ASSOC)
2881 		return 0;
2882 
2883 	if (!(ar->vht_cap_info &
2884 	      (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2885 	       IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2886 	       IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2887 	       IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2888 		return 0;
2889 
2890 	param = ar->wmi.vdev_param->txbf;
2891 	value = 0;
2892 
2893 	if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2894 		return 0;
2895 
2896 	/* The following logic is correct. If a remote STA advertises support
2897 	 * for being a beamformer then we should enable us being a beamformee.
2898 	 */
2899 
2900 	if (ar->vht_cap_info &
2901 	    (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2902 	     IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2903 		if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2904 			value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2905 
2906 		if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2907 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2908 	}
2909 
2910 	if (ar->vht_cap_info &
2911 	    (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2912 	     IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2913 		if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2914 			value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2915 
2916 		if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2917 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2918 	}
2919 
2920 	if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2921 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2922 
2923 	if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2924 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2925 
2926 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2927 	if (ret) {
2928 		ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2929 			    value, ret);
2930 		return ret;
2931 	}
2932 
2933 	return 0;
2934 }
2935 
ath10k_mac_is_connected(struct ath10k * ar)2936 static bool ath10k_mac_is_connected(struct ath10k *ar)
2937 {
2938 	struct ath10k_vif *arvif;
2939 
2940 	list_for_each_entry(arvif, &ar->arvifs, list) {
2941 		if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_STA)
2942 			return true;
2943 	}
2944 
2945 	return false;
2946 }
2947 
ath10k_mac_txpower_setup(struct ath10k * ar,int txpower)2948 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2949 {
2950 	int ret;
2951 	u32 param;
2952 	int tx_power_2g, tx_power_5g;
2953 	bool connected;
2954 
2955 	lockdep_assert_held(&ar->conf_mutex);
2956 
2957 	/* ath10k internally uses unit of 0.5 dBm so multiply by 2 */
2958 	tx_power_2g = txpower * 2;
2959 	tx_power_5g = txpower * 2;
2960 
2961 	connected = ath10k_mac_is_connected(ar);
2962 
2963 	if (connected && ar->tx_power_2g_limit)
2964 		if (tx_power_2g > ar->tx_power_2g_limit)
2965 			tx_power_2g = ar->tx_power_2g_limit;
2966 
2967 	if (connected && ar->tx_power_5g_limit)
2968 		if (tx_power_5g > ar->tx_power_5g_limit)
2969 			tx_power_5g = ar->tx_power_5g_limit;
2970 
2971 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower 2g: %d, 5g: %d\n",
2972 		   tx_power_2g, tx_power_5g);
2973 
2974 	param = ar->wmi.pdev_param->txpower_limit2g;
2975 	ret = ath10k_wmi_pdev_set_param(ar, param, tx_power_2g);
2976 	if (ret) {
2977 		ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2978 			    tx_power_2g, ret);
2979 		return ret;
2980 	}
2981 
2982 	param = ar->wmi.pdev_param->txpower_limit5g;
2983 	ret = ath10k_wmi_pdev_set_param(ar, param, tx_power_5g);
2984 	if (ret) {
2985 		ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2986 			    tx_power_5g, ret);
2987 		return ret;
2988 	}
2989 
2990 	return 0;
2991 }
2992 
ath10k_mac_txpower_recalc(struct ath10k * ar)2993 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2994 {
2995 	struct ath10k_vif *arvif;
2996 	int ret, txpower = -1;
2997 
2998 	lockdep_assert_held(&ar->conf_mutex);
2999 
3000 	list_for_each_entry(arvif, &ar->arvifs, list) {
3001 		/* txpower not initialized yet? */
3002 		if (arvif->txpower == INT_MIN)
3003 			continue;
3004 
3005 		if (txpower == -1)
3006 			txpower = arvif->txpower;
3007 		else
3008 			txpower = min(txpower, arvif->txpower);
3009 	}
3010 
3011 	if (txpower == -1)
3012 		return 0;
3013 
3014 	ret = ath10k_mac_txpower_setup(ar, txpower);
3015 	if (ret) {
3016 		ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3017 			    txpower, ret);
3018 		return ret;
3019 	}
3020 
3021 	return 0;
3022 }
3023 
ath10k_mac_set_sar_power(struct ath10k * ar)3024 static int ath10k_mac_set_sar_power(struct ath10k *ar)
3025 {
3026 	if (!ar->hw_params.dynamic_sar_support)
3027 		return -EOPNOTSUPP;
3028 
3029 	if (!ath10k_mac_is_connected(ar))
3030 		return 0;
3031 
3032 	/* if connected, then arvif->txpower must be valid */
3033 	return ath10k_mac_txpower_recalc(ar);
3034 }
3035 
ath10k_mac_set_sar_specs(struct ieee80211_hw * hw,const struct cfg80211_sar_specs * sar)3036 static int ath10k_mac_set_sar_specs(struct ieee80211_hw *hw,
3037 				    const struct cfg80211_sar_specs *sar)
3038 {
3039 	const struct cfg80211_sar_sub_specs *sub_specs;
3040 	struct ath10k *ar = hw->priv;
3041 	u32 i;
3042 	int ret;
3043 
3044 	mutex_lock(&ar->conf_mutex);
3045 
3046 	if (!ar->hw_params.dynamic_sar_support) {
3047 		ret = -EOPNOTSUPP;
3048 		goto err;
3049 	}
3050 
3051 	if (!sar || sar->type != NL80211_SAR_TYPE_POWER ||
3052 	    sar->num_sub_specs == 0) {
3053 		ret = -EINVAL;
3054 		goto err;
3055 	}
3056 
3057 	sub_specs = sar->sub_specs;
3058 
3059 	/* 0dbm is not a practical value for ath10k, so use 0
3060 	 * as no SAR limitation on it.
3061 	 */
3062 	ar->tx_power_2g_limit = 0;
3063 	ar->tx_power_5g_limit = 0;
3064 
3065 	/* note the power is in 0.25dbm unit, while ath10k uses
3066 	 * 0.5dbm unit.
3067 	 */
3068 	for (i = 0; i < sar->num_sub_specs; i++) {
3069 		if (sub_specs->freq_range_index == 0)
3070 			ar->tx_power_2g_limit = sub_specs->power / 2;
3071 		else if (sub_specs->freq_range_index == 1)
3072 			ar->tx_power_5g_limit = sub_specs->power / 2;
3073 
3074 		sub_specs++;
3075 	}
3076 
3077 	ret = ath10k_mac_set_sar_power(ar);
3078 	if (ret) {
3079 		ath10k_warn(ar, "failed to set sar power: %d", ret);
3080 		goto err;
3081 	}
3082 
3083 err:
3084 	mutex_unlock(&ar->conf_mutex);
3085 	return ret;
3086 }
3087 
3088 /* can be called only in mac80211 callbacks due to `key_count` usage */
ath10k_bss_assoc(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf)3089 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
3090 			     struct ieee80211_vif *vif,
3091 			     struct ieee80211_bss_conf *bss_conf)
3092 {
3093 	struct ath10k *ar = hw->priv;
3094 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3095 	struct ieee80211_sta_ht_cap ht_cap;
3096 	struct ieee80211_sta_vht_cap vht_cap;
3097 	struct wmi_peer_assoc_complete_arg peer_arg;
3098 	struct ieee80211_sta *ap_sta;
3099 	int ret;
3100 
3101 	lockdep_assert_held(&ar->conf_mutex);
3102 
3103 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
3104 		   arvif->vdev_id, arvif->bssid, arvif->aid);
3105 
3106 	rcu_read_lock();
3107 
3108 	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
3109 	if (!ap_sta) {
3110 		ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
3111 			    bss_conf->bssid, arvif->vdev_id);
3112 		rcu_read_unlock();
3113 		return;
3114 	}
3115 
3116 	/* ap_sta must be accessed only within rcu section which must be left
3117 	 * before calling ath10k_setup_peer_smps() which might sleep.
3118 	 */
3119 	ht_cap = ap_sta->deflink.ht_cap;
3120 	vht_cap = ap_sta->deflink.vht_cap;
3121 
3122 	ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
3123 	if (ret) {
3124 		ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
3125 			    bss_conf->bssid, arvif->vdev_id, ret);
3126 		rcu_read_unlock();
3127 		return;
3128 	}
3129 
3130 	rcu_read_unlock();
3131 
3132 	ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
3133 	if (ret) {
3134 		ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
3135 			    bss_conf->bssid, arvif->vdev_id, ret);
3136 		return;
3137 	}
3138 
3139 	ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
3140 	if (ret) {
3141 		ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
3142 			    arvif->vdev_id, ret);
3143 		return;
3144 	}
3145 
3146 	ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
3147 	if (ret) {
3148 		ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
3149 			    arvif->vdev_id, bss_conf->bssid, ret);
3150 		return;
3151 	}
3152 
3153 	ath10k_dbg(ar, ATH10K_DBG_MAC,
3154 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
3155 		   arvif->vdev_id, bss_conf->bssid, vif->cfg.aid);
3156 
3157 	WARN_ON(arvif->is_up);
3158 
3159 	arvif->aid = vif->cfg.aid;
3160 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
3161 
3162 	ret = ath10k_wmi_pdev_set_param(ar,
3163 					ar->wmi.pdev_param->peer_stats_info_enable, 1);
3164 	if (ret)
3165 		ath10k_warn(ar, "failed to enable peer stats info: %d\n", ret);
3166 
3167 	ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
3168 	if (ret) {
3169 		ath10k_warn(ar, "failed to set vdev %d up: %d\n",
3170 			    arvif->vdev_id, ret);
3171 		return;
3172 	}
3173 
3174 	arvif->is_up = true;
3175 
3176 	ath10k_mac_set_sar_power(ar);
3177 
3178 	/* Workaround: Some firmware revisions (tested with qca6174
3179 	 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
3180 	 * poked with peer param command.
3181 	 */
3182 	ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
3183 					ar->wmi.peer_param->dummy_var, 1);
3184 	if (ret) {
3185 		ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
3186 			    arvif->bssid, arvif->vdev_id, ret);
3187 		return;
3188 	}
3189 }
3190 
ath10k_bss_disassoc(struct ieee80211_hw * hw,struct ieee80211_vif * vif)3191 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
3192 				struct ieee80211_vif *vif)
3193 {
3194 	struct ath10k *ar = hw->priv;
3195 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3196 	struct ieee80211_sta_vht_cap vht_cap = {};
3197 	int ret;
3198 
3199 	lockdep_assert_held(&ar->conf_mutex);
3200 
3201 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3202 		   arvif->vdev_id, arvif->bssid);
3203 
3204 	ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
3205 	if (ret)
3206 		ath10k_warn(ar, "failed to down vdev %i: %d\n",
3207 			    arvif->vdev_id, ret);
3208 
3209 	arvif->def_wep_key_idx = -1;
3210 
3211 	ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
3212 	if (ret) {
3213 		ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
3214 			    arvif->vdev_id, ret);
3215 		return;
3216 	}
3217 
3218 	arvif->is_up = false;
3219 
3220 	ath10k_mac_txpower_recalc(ar);
3221 
3222 	cancel_delayed_work_sync(&arvif->connection_loss_work);
3223 }
3224 
ath10k_new_peer_tid_config(struct ath10k * ar,struct ieee80211_sta * sta,struct ath10k_vif * arvif)3225 static int ath10k_new_peer_tid_config(struct ath10k *ar,
3226 				      struct ieee80211_sta *sta,
3227 				      struct ath10k_vif *arvif)
3228 {
3229 	struct wmi_per_peer_per_tid_cfg_arg arg = {};
3230 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
3231 	bool config_apply;
3232 	int ret, i;
3233 
3234 	for (i = 0; i < ATH10K_TID_MAX; i++) {
3235 		config_apply = false;
3236 		if (arvif->retry_long[i] || arvif->ampdu[i] ||
3237 		    arvif->rate_ctrl[i] || arvif->rtscts[i]) {
3238 			config_apply = true;
3239 			arg.tid = i;
3240 			arg.vdev_id = arvif->vdev_id;
3241 			arg.retry_count = arvif->retry_long[i];
3242 			arg.aggr_control = arvif->ampdu[i];
3243 			arg.rate_ctrl = arvif->rate_ctrl[i];
3244 			arg.rcode_flags = arvif->rate_code[i];
3245 
3246 			if (arvif->rtscts[i])
3247 				arg.ext_tid_cfg_bitmap =
3248 					WMI_EXT_TID_RTS_CTS_CONFIG;
3249 			else
3250 				arg.ext_tid_cfg_bitmap = 0;
3251 
3252 			arg.rtscts_ctrl = arvif->rtscts[i];
3253 		}
3254 
3255 		if (arvif->noack[i]) {
3256 			arg.ack_policy = arvif->noack[i];
3257 			arg.rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_DEFAULT_LOWEST_RATE;
3258 			arg.aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
3259 			config_apply = true;
3260 		}
3261 
3262 		/* Assign default value(-1) to newly connected station.
3263 		 * This is to identify station specific tid configuration not
3264 		 * configured for the station.
3265 		 */
3266 		arsta->retry_long[i] = -1;
3267 		arsta->noack[i] = -1;
3268 		arsta->ampdu[i] = -1;
3269 
3270 		if (!config_apply)
3271 			continue;
3272 
3273 		ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
3274 
3275 		ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
3276 		if (ret) {
3277 			ath10k_warn(ar, "failed to set per tid retry/aggr config for sta %pM: %d\n",
3278 				    sta->addr, ret);
3279 			return ret;
3280 		}
3281 
3282 		memset(&arg, 0, sizeof(arg));
3283 	}
3284 
3285 	return 0;
3286 }
3287 
ath10k_station_assoc(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool reassoc)3288 static int ath10k_station_assoc(struct ath10k *ar,
3289 				struct ieee80211_vif *vif,
3290 				struct ieee80211_sta *sta,
3291 				bool reassoc)
3292 {
3293 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3294 	struct wmi_peer_assoc_complete_arg peer_arg;
3295 	int ret = 0;
3296 
3297 	lockdep_assert_held(&ar->conf_mutex);
3298 
3299 	ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
3300 	if (ret) {
3301 		ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
3302 			    sta->addr, arvif->vdev_id, ret);
3303 		return ret;
3304 	}
3305 
3306 	ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
3307 	if (ret) {
3308 		ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3309 			    sta->addr, arvif->vdev_id, ret);
3310 		return ret;
3311 	}
3312 
3313 	/* Re-assoc is run only to update supported rates for given station. It
3314 	 * doesn't make much sense to reconfigure the peer completely.
3315 	 */
3316 	if (!reassoc) {
3317 		ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
3318 					     &sta->deflink.ht_cap);
3319 		if (ret) {
3320 			ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
3321 				    arvif->vdev_id, ret);
3322 			return ret;
3323 		}
3324 
3325 		ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
3326 		if (ret) {
3327 			ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
3328 				    sta->addr, arvif->vdev_id, ret);
3329 			return ret;
3330 		}
3331 
3332 		if (!sta->wme) {
3333 			arvif->num_legacy_stations++;
3334 			ret  = ath10k_recalc_rtscts_prot(arvif);
3335 			if (ret) {
3336 				ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3337 					    arvif->vdev_id, ret);
3338 				return ret;
3339 			}
3340 		}
3341 
3342 		/* Plumb cached keys only for static WEP */
3343 		if ((arvif->def_wep_key_idx != -1) && (!sta->tdls)) {
3344 			ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
3345 			if (ret) {
3346 				ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
3347 					    arvif->vdev_id, ret);
3348 				return ret;
3349 			}
3350 		}
3351 	}
3352 
3353 	if (!test_bit(WMI_SERVICE_PEER_TID_CONFIGS_SUPPORT, ar->wmi.svc_map))
3354 		return ret;
3355 
3356 	return ath10k_new_peer_tid_config(ar, sta, arvif);
3357 }
3358 
ath10k_station_disassoc(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3359 static int ath10k_station_disassoc(struct ath10k *ar,
3360 				   struct ieee80211_vif *vif,
3361 				   struct ieee80211_sta *sta)
3362 {
3363 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3364 	int ret = 0;
3365 
3366 	lockdep_assert_held(&ar->conf_mutex);
3367 
3368 	if (!sta->wme) {
3369 		arvif->num_legacy_stations--;
3370 		ret = ath10k_recalc_rtscts_prot(arvif);
3371 		if (ret) {
3372 			ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3373 				    arvif->vdev_id, ret);
3374 			return ret;
3375 		}
3376 	}
3377 
3378 	ret = ath10k_clear_peer_keys(arvif, sta->addr);
3379 	if (ret) {
3380 		ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
3381 			    arvif->vdev_id, ret);
3382 		return ret;
3383 	}
3384 
3385 	return ret;
3386 }
3387 
3388 /**************/
3389 /* Regulatory */
3390 /**************/
3391 
ath10k_update_channel_list(struct ath10k * ar)3392 static int ath10k_update_channel_list(struct ath10k *ar)
3393 {
3394 	struct ieee80211_hw *hw = ar->hw;
3395 	struct ieee80211_supported_band **bands;
3396 	enum nl80211_band band;
3397 	struct ieee80211_channel *channel;
3398 	struct wmi_scan_chan_list_arg arg = {};
3399 	struct wmi_channel_arg *ch;
3400 	bool passive;
3401 	int len;
3402 	int ret;
3403 	int i;
3404 
3405 	lockdep_assert_held(&ar->conf_mutex);
3406 
3407 	bands = hw->wiphy->bands;
3408 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3409 		if (!bands[band])
3410 			continue;
3411 
3412 		for (i = 0; i < bands[band]->n_channels; i++) {
3413 			if (bands[band]->channels[i].flags &
3414 			    IEEE80211_CHAN_DISABLED)
3415 				continue;
3416 
3417 			arg.n_channels++;
3418 		}
3419 	}
3420 
3421 	len = sizeof(struct wmi_channel_arg) * arg.n_channels;
3422 	arg.channels = kzalloc(len, GFP_KERNEL);
3423 	if (!arg.channels)
3424 		return -ENOMEM;
3425 
3426 	ch = arg.channels;
3427 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3428 		if (!bands[band])
3429 			continue;
3430 
3431 		for (i = 0; i < bands[band]->n_channels; i++) {
3432 			channel = &bands[band]->channels[i];
3433 
3434 			if (channel->flags & IEEE80211_CHAN_DISABLED)
3435 				continue;
3436 
3437 			ch->allow_ht = true;
3438 
3439 			/* FIXME: when should we really allow VHT? */
3440 			ch->allow_vht = true;
3441 
3442 			ch->allow_ibss =
3443 				!(channel->flags & IEEE80211_CHAN_NO_IR);
3444 
3445 			ch->ht40plus =
3446 				!(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
3447 
3448 			ch->chan_radar =
3449 				!!(channel->flags & IEEE80211_CHAN_RADAR);
3450 
3451 			passive = channel->flags & IEEE80211_CHAN_NO_IR;
3452 			ch->passive = passive;
3453 
3454 			/* the firmware is ignoring the "radar" flag of the
3455 			 * channel and is scanning actively using Probe Requests
3456 			 * on "Radar detection"/DFS channels which are not
3457 			 * marked as "available"
3458 			 */
3459 			ch->passive |= ch->chan_radar;
3460 
3461 			ch->freq = channel->center_freq;
3462 			ch->band_center_freq1 = channel->center_freq;
3463 			ch->min_power = 0;
3464 			ch->max_power = channel->max_power * 2;
3465 			ch->max_reg_power = channel->max_reg_power * 2;
3466 			ch->max_antenna_gain = channel->max_antenna_gain;
3467 			ch->reg_class_id = 0; /* FIXME */
3468 
3469 			/* FIXME: why use only legacy modes, why not any
3470 			 * HT/VHT modes? Would that even make any
3471 			 * difference?
3472 			 */
3473 			if (channel->band == NL80211_BAND_2GHZ)
3474 				ch->mode = MODE_11G;
3475 			else
3476 				ch->mode = MODE_11A;
3477 
3478 			if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
3479 				continue;
3480 
3481 			ath10k_dbg(ar, ATH10K_DBG_WMI,
3482 				   "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
3483 				    ch - arg.channels, arg.n_channels,
3484 				   ch->freq, ch->max_power, ch->max_reg_power,
3485 				   ch->max_antenna_gain, ch->mode);
3486 
3487 			ch++;
3488 		}
3489 	}
3490 
3491 	ret = ath10k_wmi_scan_chan_list(ar, &arg);
3492 	kfree(arg.channels);
3493 
3494 	return ret;
3495 }
3496 
3497 static enum wmi_dfs_region
ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)3498 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
3499 {
3500 	switch (dfs_region) {
3501 	case NL80211_DFS_UNSET:
3502 		return WMI_UNINIT_DFS_DOMAIN;
3503 	case NL80211_DFS_FCC:
3504 		return WMI_FCC_DFS_DOMAIN;
3505 	case NL80211_DFS_ETSI:
3506 		return WMI_ETSI_DFS_DOMAIN;
3507 	case NL80211_DFS_JP:
3508 		return WMI_MKK4_DFS_DOMAIN;
3509 	}
3510 	return WMI_UNINIT_DFS_DOMAIN;
3511 }
3512 
ath10k_regd_update(struct ath10k * ar)3513 static void ath10k_regd_update(struct ath10k *ar)
3514 {
3515 	struct reg_dmn_pair_mapping *regpair;
3516 	int ret;
3517 	enum wmi_dfs_region wmi_dfs_reg;
3518 	enum nl80211_dfs_regions nl_dfs_reg;
3519 
3520 	lockdep_assert_held(&ar->conf_mutex);
3521 
3522 	ret = ath10k_update_channel_list(ar);
3523 	if (ret)
3524 		ath10k_warn(ar, "failed to update channel list: %d\n", ret);
3525 
3526 	regpair = ar->ath_common.regulatory.regpair;
3527 
3528 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
3529 		nl_dfs_reg = ar->dfs_detector->region;
3530 		wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
3531 	} else {
3532 		wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
3533 	}
3534 
3535 	/* Target allows setting up per-band regdomain but ath_common provides
3536 	 * a combined one only
3537 	 */
3538 	ret = ath10k_wmi_pdev_set_regdomain(ar,
3539 					    regpair->reg_domain,
3540 					    regpair->reg_domain, /* 2ghz */
3541 					    regpair->reg_domain, /* 5ghz */
3542 					    regpair->reg_2ghz_ctl,
3543 					    regpair->reg_5ghz_ctl,
3544 					    wmi_dfs_reg);
3545 	if (ret)
3546 		ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
3547 }
3548 
ath10k_mac_update_channel_list(struct ath10k * ar,struct ieee80211_supported_band * band)3549 static void ath10k_mac_update_channel_list(struct ath10k *ar,
3550 					   struct ieee80211_supported_band *band)
3551 {
3552 	int i;
3553 
3554 	if (ar->low_5ghz_chan && ar->high_5ghz_chan) {
3555 		for (i = 0; i < band->n_channels; i++) {
3556 			if (band->channels[i].center_freq < ar->low_5ghz_chan ||
3557 			    band->channels[i].center_freq > ar->high_5ghz_chan)
3558 				band->channels[i].flags |=
3559 					IEEE80211_CHAN_DISABLED;
3560 		}
3561 	}
3562 }
3563 
ath10k_reg_notifier(struct wiphy * wiphy,struct regulatory_request * request)3564 static void ath10k_reg_notifier(struct wiphy *wiphy,
3565 				struct regulatory_request *request)
3566 {
3567 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
3568 	struct ath10k *ar = hw->priv;
3569 	bool result;
3570 
3571 	ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
3572 
3573 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
3574 		ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
3575 			   request->dfs_region);
3576 		result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
3577 							  request->dfs_region);
3578 		if (!result)
3579 			ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
3580 				    request->dfs_region);
3581 	}
3582 
3583 	mutex_lock(&ar->conf_mutex);
3584 	if (ar->state == ATH10K_STATE_ON)
3585 		ath10k_regd_update(ar);
3586 	mutex_unlock(&ar->conf_mutex);
3587 
3588 	if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
3589 		ath10k_mac_update_channel_list(ar,
3590 					       ar->hw->wiphy->bands[NL80211_BAND_5GHZ]);
3591 }
3592 
ath10k_stop_radar_confirmation(struct ath10k * ar)3593 static void ath10k_stop_radar_confirmation(struct ath10k *ar)
3594 {
3595 	spin_lock_bh(&ar->data_lock);
3596 	ar->radar_conf_state = ATH10K_RADAR_CONFIRMATION_STOPPED;
3597 	spin_unlock_bh(&ar->data_lock);
3598 
3599 	cancel_work_sync(&ar->radar_confirmation_work);
3600 }
3601 
3602 /***************/
3603 /* TX handlers */
3604 /***************/
3605 
3606 enum ath10k_mac_tx_path {
3607 	ATH10K_MAC_TX_HTT,
3608 	ATH10K_MAC_TX_HTT_MGMT,
3609 	ATH10K_MAC_TX_WMI_MGMT,
3610 	ATH10K_MAC_TX_UNKNOWN,
3611 };
3612 
ath10k_mac_tx_lock(struct ath10k * ar,int reason)3613 void ath10k_mac_tx_lock(struct ath10k *ar, int reason)
3614 {
3615 	lockdep_assert_held(&ar->htt.tx_lock);
3616 
3617 	WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3618 	ar->tx_paused |= BIT(reason);
3619 	ieee80211_stop_queues(ar->hw);
3620 }
3621 
ath10k_mac_tx_unlock_iter(void * data,u8 * mac,struct ieee80211_vif * vif)3622 static void ath10k_mac_tx_unlock_iter(void *data, u8 *mac,
3623 				      struct ieee80211_vif *vif)
3624 {
3625 	struct ath10k *ar = data;
3626 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3627 
3628 	if (arvif->tx_paused)
3629 		return;
3630 
3631 	ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3632 }
3633 
ath10k_mac_tx_unlock(struct ath10k * ar,int reason)3634 void ath10k_mac_tx_unlock(struct ath10k *ar, int reason)
3635 {
3636 	lockdep_assert_held(&ar->htt.tx_lock);
3637 
3638 	WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3639 	ar->tx_paused &= ~BIT(reason);
3640 
3641 	if (ar->tx_paused)
3642 		return;
3643 
3644 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
3645 						   ATH10K_ITER_RESUME_FLAGS,
3646 						   ath10k_mac_tx_unlock_iter,
3647 						   ar);
3648 
3649 	ieee80211_wake_queue(ar->hw, ar->hw->offchannel_tx_hw_queue);
3650 }
3651 
ath10k_mac_vif_tx_lock(struct ath10k_vif * arvif,int reason)3652 void ath10k_mac_vif_tx_lock(struct ath10k_vif *arvif, int reason)
3653 {
3654 	struct ath10k *ar = arvif->ar;
3655 
3656 	lockdep_assert_held(&ar->htt.tx_lock);
3657 
3658 	WARN_ON(reason >= BITS_PER_LONG);
3659 	arvif->tx_paused |= BIT(reason);
3660 	ieee80211_stop_queue(ar->hw, arvif->vdev_id);
3661 }
3662 
ath10k_mac_vif_tx_unlock(struct ath10k_vif * arvif,int reason)3663 void ath10k_mac_vif_tx_unlock(struct ath10k_vif *arvif, int reason)
3664 {
3665 	struct ath10k *ar = arvif->ar;
3666 
3667 	lockdep_assert_held(&ar->htt.tx_lock);
3668 
3669 	WARN_ON(reason >= BITS_PER_LONG);
3670 	arvif->tx_paused &= ~BIT(reason);
3671 
3672 	if (ar->tx_paused)
3673 		return;
3674 
3675 	if (arvif->tx_paused)
3676 		return;
3677 
3678 	ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3679 }
3680 
ath10k_mac_vif_handle_tx_pause(struct ath10k_vif * arvif,enum wmi_tlv_tx_pause_id pause_id,enum wmi_tlv_tx_pause_action action)3681 static void ath10k_mac_vif_handle_tx_pause(struct ath10k_vif *arvif,
3682 					   enum wmi_tlv_tx_pause_id pause_id,
3683 					   enum wmi_tlv_tx_pause_action action)
3684 {
3685 	struct ath10k *ar = arvif->ar;
3686 
3687 	lockdep_assert_held(&ar->htt.tx_lock);
3688 
3689 	switch (action) {
3690 	case WMI_TLV_TX_PAUSE_ACTION_STOP:
3691 		ath10k_mac_vif_tx_lock(arvif, pause_id);
3692 		break;
3693 	case WMI_TLV_TX_PAUSE_ACTION_WAKE:
3694 		ath10k_mac_vif_tx_unlock(arvif, pause_id);
3695 		break;
3696 	default:
3697 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
3698 			   "received unknown tx pause action %d on vdev %i, ignoring\n",
3699 			    action, arvif->vdev_id);
3700 		break;
3701 	}
3702 }
3703 
3704 struct ath10k_mac_tx_pause {
3705 	u32 vdev_id;
3706 	enum wmi_tlv_tx_pause_id pause_id;
3707 	enum wmi_tlv_tx_pause_action action;
3708 };
3709 
ath10k_mac_handle_tx_pause_iter(void * data,u8 * mac,struct ieee80211_vif * vif)3710 static void ath10k_mac_handle_tx_pause_iter(void *data, u8 *mac,
3711 					    struct ieee80211_vif *vif)
3712 {
3713 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3714 	struct ath10k_mac_tx_pause *arg = data;
3715 
3716 	if (arvif->vdev_id != arg->vdev_id)
3717 		return;
3718 
3719 	ath10k_mac_vif_handle_tx_pause(arvif, arg->pause_id, arg->action);
3720 }
3721 
ath10k_mac_handle_tx_pause_vdev(struct ath10k * ar,u32 vdev_id,enum wmi_tlv_tx_pause_id pause_id,enum wmi_tlv_tx_pause_action action)3722 void ath10k_mac_handle_tx_pause_vdev(struct ath10k *ar, u32 vdev_id,
3723 				     enum wmi_tlv_tx_pause_id pause_id,
3724 				     enum wmi_tlv_tx_pause_action action)
3725 {
3726 	struct ath10k_mac_tx_pause arg = {
3727 		.vdev_id = vdev_id,
3728 		.pause_id = pause_id,
3729 		.action = action,
3730 	};
3731 
3732 	spin_lock_bh(&ar->htt.tx_lock);
3733 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
3734 						   ATH10K_ITER_RESUME_FLAGS,
3735 						   ath10k_mac_handle_tx_pause_iter,
3736 						   &arg);
3737 	spin_unlock_bh(&ar->htt.tx_lock);
3738 }
3739 
3740 static enum ath10k_hw_txrx_mode
ath10k_mac_tx_h_get_txmode(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct sk_buff * skb)3741 ath10k_mac_tx_h_get_txmode(struct ath10k *ar,
3742 			   struct ieee80211_vif *vif,
3743 			   struct ieee80211_sta *sta,
3744 			   struct sk_buff *skb)
3745 {
3746 	const struct ieee80211_hdr *hdr = (void *)skb->data;
3747 	const struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
3748 	__le16 fc = hdr->frame_control;
3749 
3750 	if (IEEE80211_SKB_CB(skb)->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
3751 		return ATH10K_HW_TXRX_ETHERNET;
3752 
3753 	if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
3754 		return ATH10K_HW_TXRX_RAW;
3755 
3756 	if (ieee80211_is_mgmt(fc))
3757 		return ATH10K_HW_TXRX_MGMT;
3758 
3759 	/* Workaround:
3760 	 *
3761 	 * NullFunc frames are mostly used to ping if a client or AP are still
3762 	 * reachable and responsive. This implies tx status reports must be
3763 	 * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
3764 	 * come to a conclusion that the other end disappeared and tear down
3765 	 * BSS connection or it can never disconnect from BSS/client (which is
3766 	 * the case).
3767 	 *
3768 	 * Firmware with HTT older than 3.0 delivers incorrect tx status for
3769 	 * NullFunc frames to driver. However there's a HTT Mgmt Tx command
3770 	 * which seems to deliver correct tx reports for NullFunc frames. The
3771 	 * downside of using it is it ignores client powersave state so it can
3772 	 * end up disconnecting sleeping clients in AP mode. It should fix STA
3773 	 * mode though because AP don't sleep.
3774 	 */
3775 	if (ar->htt.target_version_major < 3 &&
3776 	    (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
3777 	    !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3778 		      ar->running_fw->fw_file.fw_features))
3779 		return ATH10K_HW_TXRX_MGMT;
3780 
3781 	/* Workaround:
3782 	 *
3783 	 * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
3784 	 * NativeWifi txmode - it selects AP key instead of peer key. It seems
3785 	 * to work with Ethernet txmode so use it.
3786 	 *
3787 	 * FIXME: Check if raw mode works with TDLS.
3788 	 */
3789 	if (ieee80211_is_data_present(fc) && sta && sta->tdls)
3790 		return ATH10K_HW_TXRX_ETHERNET;
3791 
3792 	if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags) ||
3793 	    skb_cb->flags & ATH10K_SKB_F_RAW_TX)
3794 		return ATH10K_HW_TXRX_RAW;
3795 
3796 	return ATH10K_HW_TXRX_NATIVE_WIFI;
3797 }
3798 
ath10k_tx_h_use_hwcrypto(struct ieee80211_vif * vif,struct sk_buff * skb)3799 static bool ath10k_tx_h_use_hwcrypto(struct ieee80211_vif *vif,
3800 				     struct sk_buff *skb)
3801 {
3802 	const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3803 	const struct ieee80211_hdr *hdr = (void *)skb->data;
3804 	const u32 mask = IEEE80211_TX_INTFL_DONT_ENCRYPT |
3805 			 IEEE80211_TX_CTL_INJECTED;
3806 
3807 	if (!ieee80211_has_protected(hdr->frame_control))
3808 		return false;
3809 
3810 	if ((info->flags & mask) == mask)
3811 		return false;
3812 
3813 	if (vif)
3814 		return !((struct ath10k_vif *)vif->drv_priv)->nohwcrypt;
3815 
3816 	return true;
3817 }
3818 
3819 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
3820  * Control in the header.
3821  */
ath10k_tx_h_nwifi(struct ieee80211_hw * hw,struct sk_buff * skb)3822 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
3823 {
3824 	struct ieee80211_hdr *hdr = (void *)skb->data;
3825 	struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3826 	u8 *qos_ctl;
3827 
3828 	if (!ieee80211_is_data_qos(hdr->frame_control))
3829 		return;
3830 
3831 	qos_ctl = ieee80211_get_qos_ctl(hdr);
3832 	memmove(skb->data + IEEE80211_QOS_CTL_LEN,
3833 #if defined(__linux__)
3834 		skb->data, (void *)qos_ctl - (void *)skb->data);
3835 #elif defined(__FreeBSD__)
3836 		skb->data, qos_ctl - skb->data);
3837 #endif
3838 	skb_pull(skb, IEEE80211_QOS_CTL_LEN);
3839 
3840 	/* Some firmware revisions don't handle sending QoS NullFunc well.
3841 	 * These frames are mainly used for CQM purposes so it doesn't really
3842 	 * matter whether QoS NullFunc or NullFunc are sent.
3843 	 */
3844 	hdr = (void *)skb->data;
3845 	if (ieee80211_is_qos_nullfunc(hdr->frame_control))
3846 		cb->flags &= ~ATH10K_SKB_F_QOS;
3847 
3848 	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3849 }
3850 
ath10k_tx_h_8023(struct sk_buff * skb)3851 static void ath10k_tx_h_8023(struct sk_buff *skb)
3852 {
3853 	struct ieee80211_hdr *hdr;
3854 	struct rfc1042_hdr *rfc1042;
3855 	struct ethhdr *eth;
3856 	size_t hdrlen;
3857 	u8 da[ETH_ALEN];
3858 	u8 sa[ETH_ALEN];
3859 	__be16 type;
3860 
3861 	hdr = (void *)skb->data;
3862 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
3863 #if defined(__linux__)
3864 	rfc1042 = (void *)skb->data + hdrlen;
3865 #elif defined(__FreeBSD__)
3866 	rfc1042 = (void *)(skb->data + hdrlen);
3867 #endif
3868 
3869 	ether_addr_copy(da, ieee80211_get_DA(hdr));
3870 	ether_addr_copy(sa, ieee80211_get_SA(hdr));
3871 	type = rfc1042->snap_type;
3872 
3873 	skb_pull(skb, hdrlen + sizeof(*rfc1042));
3874 	skb_push(skb, sizeof(*eth));
3875 
3876 	eth = (void *)skb->data;
3877 	ether_addr_copy(eth->h_dest, da);
3878 	ether_addr_copy(eth->h_source, sa);
3879 	eth->h_proto = type;
3880 }
3881 
ath10k_tx_h_add_p2p_noa_ie(struct ath10k * ar,struct ieee80211_vif * vif,struct sk_buff * skb)3882 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
3883 				       struct ieee80211_vif *vif,
3884 				       struct sk_buff *skb)
3885 {
3886 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3887 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3888 
3889 	/* This is case only for P2P_GO */
3890 	if (vif->type != NL80211_IFTYPE_AP || !vif->p2p)
3891 		return;
3892 
3893 	if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
3894 		spin_lock_bh(&ar->data_lock);
3895 		if (arvif->u.ap.noa_data)
3896 			if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
3897 					      GFP_ATOMIC))
3898 				skb_put_data(skb, arvif->u.ap.noa_data,
3899 					     arvif->u.ap.noa_len);
3900 		spin_unlock_bh(&ar->data_lock);
3901 	}
3902 }
3903 
ath10k_mac_tx_h_fill_cb(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_txq * txq,struct ieee80211_sta * sta,struct sk_buff * skb,u16 airtime)3904 static void ath10k_mac_tx_h_fill_cb(struct ath10k *ar,
3905 				    struct ieee80211_vif *vif,
3906 				    struct ieee80211_txq *txq,
3907 				    struct ieee80211_sta *sta,
3908 				    struct sk_buff *skb, u16 airtime)
3909 {
3910 	struct ieee80211_hdr *hdr = (void *)skb->data;
3911 	struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3912 	const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3913 	bool is_data = ieee80211_is_data(hdr->frame_control) ||
3914 			ieee80211_is_data_qos(hdr->frame_control);
3915 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3916 	struct ath10k_sta *arsta;
3917 	u8 tid, *qos_ctl;
3918 	bool noack = false;
3919 
3920 	cb->flags = 0;
3921 
3922 	if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
3923 		cb->flags |= ATH10K_SKB_F_QOS;	/* Assume data frames are QoS */
3924 		goto finish_cb_fill;
3925 	}
3926 
3927 	if (!ath10k_tx_h_use_hwcrypto(vif, skb))
3928 		cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3929 
3930 	if (ieee80211_is_mgmt(hdr->frame_control))
3931 		cb->flags |= ATH10K_SKB_F_MGMT;
3932 
3933 	if (ieee80211_is_data_qos(hdr->frame_control)) {
3934 		cb->flags |= ATH10K_SKB_F_QOS;
3935 		qos_ctl = ieee80211_get_qos_ctl(hdr);
3936 		tid = (*qos_ctl) & IEEE80211_QOS_CTL_TID_MASK;
3937 
3938 		if (arvif->noack[tid] == WMI_PEER_TID_CONFIG_NOACK)
3939 			noack = true;
3940 
3941 		if (sta) {
3942 			arsta = (struct ath10k_sta *)sta->drv_priv;
3943 
3944 			if (arsta->noack[tid] == WMI_PEER_TID_CONFIG_NOACK)
3945 				noack = true;
3946 
3947 			if (arsta->noack[tid] == WMI_PEER_TID_CONFIG_ACK)
3948 				noack = false;
3949 		}
3950 
3951 		if (noack)
3952 			cb->flags |= ATH10K_SKB_F_NOACK_TID;
3953 	}
3954 
3955 	/* Data frames encrypted in software will be posted to firmware
3956 	 * with tx encap mode set to RAW. Ex: Multicast traffic generated
3957 	 * for a specific VLAN group will always be encrypted in software.
3958 	 */
3959 	if (is_data && ieee80211_has_protected(hdr->frame_control) &&
3960 	    !info->control.hw_key) {
3961 		cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3962 		cb->flags |= ATH10K_SKB_F_RAW_TX;
3963 	}
3964 
3965 finish_cb_fill:
3966 	cb->vif = vif;
3967 	cb->txq = txq;
3968 	cb->airtime_est = airtime;
3969 	if (sta) {
3970 		arsta = (struct ath10k_sta *)sta->drv_priv;
3971 		spin_lock_bh(&ar->data_lock);
3972 		cb->ucast_cipher = arsta->ucast_cipher;
3973 		spin_unlock_bh(&ar->data_lock);
3974 	}
3975 }
3976 
ath10k_mac_tx_frm_has_freq(struct ath10k * ar)3977 bool ath10k_mac_tx_frm_has_freq(struct ath10k *ar)
3978 {
3979 	/* FIXME: Not really sure since when the behaviour changed. At some
3980 	 * point new firmware stopped requiring creation of peer entries for
3981 	 * offchannel tx (and actually creating them causes issues with wmi-htc
3982 	 * tx credit replenishment and reliability). Assuming it's at least 3.4
3983 	 * because that's when the `freq` was introduced to TX_FRM HTT command.
3984 	 */
3985 	return (ar->htt.target_version_major >= 3 &&
3986 		ar->htt.target_version_minor >= 4 &&
3987 		ar->running_fw->fw_file.htt_op_version == ATH10K_FW_HTT_OP_VERSION_TLV);
3988 }
3989 
ath10k_mac_tx_wmi_mgmt(struct ath10k * ar,struct sk_buff * skb)3990 static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
3991 {
3992 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
3993 
3994 	if (skb_queue_len_lockless(q) >= ATH10K_MAX_NUM_MGMT_PENDING) {
3995 		ath10k_warn(ar, "wmi mgmt tx queue is full\n");
3996 		return -ENOSPC;
3997 	}
3998 
3999 	skb_queue_tail(q, skb);
4000 	ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
4001 
4002 	return 0;
4003 }
4004 
4005 static enum ath10k_mac_tx_path
ath10k_mac_tx_h_get_txpath(struct ath10k * ar,struct sk_buff * skb,enum ath10k_hw_txrx_mode txmode)4006 ath10k_mac_tx_h_get_txpath(struct ath10k *ar,
4007 			   struct sk_buff *skb,
4008 			   enum ath10k_hw_txrx_mode txmode)
4009 {
4010 	switch (txmode) {
4011 	case ATH10K_HW_TXRX_RAW:
4012 	case ATH10K_HW_TXRX_NATIVE_WIFI:
4013 	case ATH10K_HW_TXRX_ETHERNET:
4014 		return ATH10K_MAC_TX_HTT;
4015 	case ATH10K_HW_TXRX_MGMT:
4016 		if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
4017 			     ar->running_fw->fw_file.fw_features) ||
4018 			     test_bit(WMI_SERVICE_MGMT_TX_WMI,
4019 				      ar->wmi.svc_map))
4020 			return ATH10K_MAC_TX_WMI_MGMT;
4021 		else if (ar->htt.target_version_major >= 3)
4022 			return ATH10K_MAC_TX_HTT;
4023 		else
4024 			return ATH10K_MAC_TX_HTT_MGMT;
4025 	}
4026 
4027 	return ATH10K_MAC_TX_UNKNOWN;
4028 }
4029 
ath10k_mac_tx_submit(struct ath10k * ar,enum ath10k_hw_txrx_mode txmode,enum ath10k_mac_tx_path txpath,struct sk_buff * skb)4030 static int ath10k_mac_tx_submit(struct ath10k *ar,
4031 				enum ath10k_hw_txrx_mode txmode,
4032 				enum ath10k_mac_tx_path txpath,
4033 				struct sk_buff *skb)
4034 {
4035 	struct ath10k_htt *htt = &ar->htt;
4036 	int ret = -EINVAL;
4037 
4038 	switch (txpath) {
4039 	case ATH10K_MAC_TX_HTT:
4040 		ret = ath10k_htt_tx(htt, txmode, skb);
4041 		break;
4042 	case ATH10K_MAC_TX_HTT_MGMT:
4043 		ret = ath10k_htt_mgmt_tx(htt, skb);
4044 		break;
4045 	case ATH10K_MAC_TX_WMI_MGMT:
4046 		ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
4047 		break;
4048 	case ATH10K_MAC_TX_UNKNOWN:
4049 		WARN_ON_ONCE(1);
4050 		ret = -EINVAL;
4051 		break;
4052 	}
4053 
4054 	if (ret) {
4055 		ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
4056 			    ret);
4057 		ieee80211_free_txskb(ar->hw, skb);
4058 	}
4059 
4060 	return ret;
4061 }
4062 
4063 /* This function consumes the sk_buff regardless of return value as far as
4064  * caller is concerned so no freeing is necessary afterwards.
4065  */
ath10k_mac_tx(struct ath10k * ar,struct ieee80211_vif * vif,enum ath10k_hw_txrx_mode txmode,enum ath10k_mac_tx_path txpath,struct sk_buff * skb,bool noque_offchan)4066 static int ath10k_mac_tx(struct ath10k *ar,
4067 			 struct ieee80211_vif *vif,
4068 			 enum ath10k_hw_txrx_mode txmode,
4069 			 enum ath10k_mac_tx_path txpath,
4070 			 struct sk_buff *skb, bool noque_offchan)
4071 {
4072 	struct ieee80211_hw *hw = ar->hw;
4073 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4074 	const struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
4075 	int ret;
4076 
4077 	/* We should disable CCK RATE due to P2P */
4078 	if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
4079 		ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
4080 
4081 	switch (txmode) {
4082 	case ATH10K_HW_TXRX_MGMT:
4083 	case ATH10K_HW_TXRX_NATIVE_WIFI:
4084 		ath10k_tx_h_nwifi(hw, skb);
4085 		ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
4086 		ath10k_tx_h_seq_no(vif, skb);
4087 		break;
4088 	case ATH10K_HW_TXRX_ETHERNET:
4089 		/* Convert 802.11->802.3 header only if the frame was earlier
4090 		 * encapsulated to 802.11 by mac80211. Otherwise pass it as is.
4091 		 */
4092 		if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP))
4093 			ath10k_tx_h_8023(skb);
4094 		break;
4095 	case ATH10K_HW_TXRX_RAW:
4096 		if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags) &&
4097 		    !(skb_cb->flags & ATH10K_SKB_F_RAW_TX)) {
4098 			WARN_ON_ONCE(1);
4099 			ieee80211_free_txskb(hw, skb);
4100 			return -EOPNOTSUPP;
4101 		}
4102 	}
4103 
4104 	if (!noque_offchan && info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
4105 		if (!ath10k_mac_tx_frm_has_freq(ar)) {
4106 			ath10k_dbg(ar, ATH10K_DBG_MAC, "mac queued offchannel skb %p len %d\n",
4107 				   skb, skb->len);
4108 
4109 			skb_queue_tail(&ar->offchan_tx_queue, skb);
4110 			ieee80211_queue_work(hw, &ar->offchan_tx_work);
4111 			return 0;
4112 		}
4113 	}
4114 
4115 	ret = ath10k_mac_tx_submit(ar, txmode, txpath, skb);
4116 	if (ret) {
4117 		ath10k_warn(ar, "failed to submit frame: %d\n", ret);
4118 		return ret;
4119 	}
4120 
4121 	return 0;
4122 }
4123 
ath10k_offchan_tx_purge(struct ath10k * ar)4124 void ath10k_offchan_tx_purge(struct ath10k *ar)
4125 {
4126 	struct sk_buff *skb;
4127 
4128 	for (;;) {
4129 		skb = skb_dequeue(&ar->offchan_tx_queue);
4130 		if (!skb)
4131 			break;
4132 
4133 		ieee80211_free_txskb(ar->hw, skb);
4134 	}
4135 }
4136 
ath10k_offchan_tx_work(struct work_struct * work)4137 void ath10k_offchan_tx_work(struct work_struct *work)
4138 {
4139 	struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
4140 	struct ath10k_peer *peer;
4141 	struct ath10k_vif *arvif;
4142 	enum ath10k_hw_txrx_mode txmode;
4143 	enum ath10k_mac_tx_path txpath;
4144 	struct ieee80211_hdr *hdr;
4145 	struct ieee80211_vif *vif;
4146 	struct ieee80211_sta *sta;
4147 	struct sk_buff *skb;
4148 	const u8 *peer_addr;
4149 	int vdev_id;
4150 	int ret;
4151 	unsigned long time_left;
4152 	bool tmp_peer_created = false;
4153 
4154 	/* FW requirement: We must create a peer before FW will send out
4155 	 * an offchannel frame. Otherwise the frame will be stuck and
4156 	 * never transmitted. We delete the peer upon tx completion.
4157 	 * It is unlikely that a peer for offchannel tx will already be
4158 	 * present. However it may be in some rare cases so account for that.
4159 	 * Otherwise we might remove a legitimate peer and break stuff.
4160 	 */
4161 
4162 	for (;;) {
4163 		skb = skb_dequeue(&ar->offchan_tx_queue);
4164 		if (!skb)
4165 			break;
4166 
4167 		mutex_lock(&ar->conf_mutex);
4168 
4169 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p len %d\n",
4170 			   skb, skb->len);
4171 
4172 		hdr = (struct ieee80211_hdr *)skb->data;
4173 		peer_addr = ieee80211_get_DA(hdr);
4174 
4175 		spin_lock_bh(&ar->data_lock);
4176 		vdev_id = ar->scan.vdev_id;
4177 		peer = ath10k_peer_find(ar, vdev_id, peer_addr);
4178 		spin_unlock_bh(&ar->data_lock);
4179 
4180 		if (peer) {
4181 			ath10k_warn(ar, "peer %pM on vdev %d already present\n",
4182 				    peer_addr, vdev_id);
4183 		} else {
4184 			ret = ath10k_peer_create(ar, NULL, NULL, vdev_id,
4185 						 peer_addr,
4186 						 WMI_PEER_TYPE_DEFAULT);
4187 			if (ret)
4188 				ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
4189 					    peer_addr, vdev_id, ret);
4190 			tmp_peer_created = (ret == 0);
4191 		}
4192 
4193 		spin_lock_bh(&ar->data_lock);
4194 		reinit_completion(&ar->offchan_tx_completed);
4195 		ar->offchan_tx_skb = skb;
4196 		spin_unlock_bh(&ar->data_lock);
4197 
4198 		/* It's safe to access vif and sta - conf_mutex guarantees that
4199 		 * sta_state() and remove_interface() are locked exclusively
4200 		 * out wrt to this offchannel worker.
4201 		 */
4202 		arvif = ath10k_get_arvif(ar, vdev_id);
4203 		if (arvif) {
4204 			vif = arvif->vif;
4205 			sta = ieee80211_find_sta(vif, peer_addr);
4206 		} else {
4207 			vif = NULL;
4208 			sta = NULL;
4209 		}
4210 
4211 		txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4212 		txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4213 
4214 		ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, true);
4215 		if (ret) {
4216 			ath10k_warn(ar, "failed to transmit offchannel frame: %d\n",
4217 				    ret);
4218 			/* not serious */
4219 		}
4220 
4221 		time_left =
4222 		wait_for_completion_timeout(&ar->offchan_tx_completed, 3 * HZ);
4223 		if (time_left == 0)
4224 			ath10k_warn(ar, "timed out waiting for offchannel skb %p, len: %d\n",
4225 				    skb, skb->len);
4226 
4227 		if (!peer && tmp_peer_created) {
4228 			ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
4229 			if (ret)
4230 				ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
4231 					    peer_addr, vdev_id, ret);
4232 		}
4233 
4234 		mutex_unlock(&ar->conf_mutex);
4235 	}
4236 }
4237 
ath10k_mgmt_over_wmi_tx_purge(struct ath10k * ar)4238 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
4239 {
4240 	struct sk_buff *skb;
4241 
4242 	for (;;) {
4243 		skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
4244 		if (!skb)
4245 			break;
4246 
4247 		ieee80211_free_txskb(ar->hw, skb);
4248 	}
4249 }
4250 
ath10k_mgmt_over_wmi_tx_work(struct work_struct * work)4251 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
4252 {
4253 	struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
4254 	struct sk_buff *skb;
4255 	dma_addr_t paddr;
4256 	int ret;
4257 
4258 	for (;;) {
4259 		skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
4260 		if (!skb)
4261 			break;
4262 
4263 		if (test_bit(ATH10K_FW_FEATURE_MGMT_TX_BY_REF,
4264 			     ar->running_fw->fw_file.fw_features)) {
4265 			paddr = dma_map_single(ar->dev, skb->data,
4266 					       skb->len, DMA_TO_DEVICE);
4267 			if (dma_mapping_error(ar->dev, paddr)) {
4268 				ieee80211_free_txskb(ar->hw, skb);
4269 				continue;
4270 			}
4271 			ret = ath10k_wmi_mgmt_tx_send(ar, skb, paddr);
4272 			if (ret) {
4273 				ath10k_warn(ar, "failed to transmit management frame by ref via WMI: %d\n",
4274 					    ret);
4275 				/* remove this msdu from idr tracking */
4276 				ath10k_wmi_cleanup_mgmt_tx_send(ar, skb);
4277 
4278 				dma_unmap_single(ar->dev, paddr, skb->len,
4279 						 DMA_TO_DEVICE);
4280 				ieee80211_free_txskb(ar->hw, skb);
4281 			}
4282 		} else {
4283 			ret = ath10k_wmi_mgmt_tx(ar, skb);
4284 			if (ret) {
4285 				ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
4286 					    ret);
4287 				ieee80211_free_txskb(ar->hw, skb);
4288 			}
4289 		}
4290 	}
4291 }
4292 
ath10k_mac_txq_init(struct ieee80211_txq * txq)4293 static void ath10k_mac_txq_init(struct ieee80211_txq *txq)
4294 {
4295 	struct ath10k_txq *artxq;
4296 
4297 	if (!txq)
4298 		return;
4299 
4300 	artxq = (void *)txq->drv_priv;
4301 	INIT_LIST_HEAD(&artxq->list);
4302 }
4303 
ath10k_mac_txq_unref(struct ath10k * ar,struct ieee80211_txq * txq)4304 static void ath10k_mac_txq_unref(struct ath10k *ar, struct ieee80211_txq *txq)
4305 {
4306 	struct ath10k_skb_cb *cb;
4307 	struct sk_buff *msdu;
4308 	int msdu_id;
4309 
4310 	if (!txq)
4311 		return;
4312 
4313 	spin_lock_bh(&ar->htt.tx_lock);
4314 	idr_for_each_entry(&ar->htt.pending_tx, msdu, msdu_id) {
4315 		cb = ATH10K_SKB_CB(msdu);
4316 		if (cb->txq == txq)
4317 			cb->txq = NULL;
4318 	}
4319 	spin_unlock_bh(&ar->htt.tx_lock);
4320 }
4321 
ath10k_mac_txq_lookup(struct ath10k * ar,u16 peer_id,u8 tid)4322 struct ieee80211_txq *ath10k_mac_txq_lookup(struct ath10k *ar,
4323 					    u16 peer_id,
4324 					    u8 tid)
4325 {
4326 	struct ath10k_peer *peer;
4327 
4328 	lockdep_assert_held(&ar->data_lock);
4329 
4330 	peer = ar->peer_map[peer_id];
4331 	if (!peer)
4332 		return NULL;
4333 
4334 	if (peer->removed)
4335 		return NULL;
4336 
4337 	if (peer->sta)
4338 		return peer->sta->txq[tid];
4339 	else if (peer->vif)
4340 		return peer->vif->txq;
4341 	else
4342 		return NULL;
4343 }
4344 
ath10k_mac_tx_can_push(struct ieee80211_hw * hw,struct ieee80211_txq * txq)4345 static bool ath10k_mac_tx_can_push(struct ieee80211_hw *hw,
4346 				   struct ieee80211_txq *txq)
4347 {
4348 	struct ath10k *ar = hw->priv;
4349 	struct ath10k_txq *artxq = (void *)txq->drv_priv;
4350 
4351 	/* No need to get locks */
4352 	if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH)
4353 		return true;
4354 
4355 	if (ar->htt.num_pending_tx < ar->htt.tx_q_state.num_push_allowed)
4356 		return true;
4357 
4358 	if (artxq->num_fw_queued < artxq->num_push_allowed)
4359 		return true;
4360 
4361 	return false;
4362 }
4363 
4364 /* Return estimated airtime in microsecond, which is calculated using last
4365  * reported TX rate. This is just a rough estimation because host driver has no
4366  * knowledge of the actual transmit rate, retries or aggregation. If actual
4367  * airtime can be reported by firmware, then delta between estimated and actual
4368  * airtime can be adjusted from deficit.
4369  */
4370 #define IEEE80211_ATF_OVERHEAD		100	/* IFS + some slot time */
4371 #define IEEE80211_ATF_OVERHEAD_IFS	16	/* IFS only */
ath10k_mac_update_airtime(struct ath10k * ar,struct ieee80211_txq * txq,struct sk_buff * skb)4372 static u16 ath10k_mac_update_airtime(struct ath10k *ar,
4373 				     struct ieee80211_txq *txq,
4374 				     struct sk_buff *skb)
4375 {
4376 	struct ath10k_sta *arsta;
4377 	u32 pktlen;
4378 	u16 airtime = 0;
4379 
4380 	if (!txq || !txq->sta)
4381 		return airtime;
4382 
4383 	if (test_bit(WMI_SERVICE_REPORT_AIRTIME, ar->wmi.svc_map))
4384 		return airtime;
4385 
4386 	spin_lock_bh(&ar->data_lock);
4387 	arsta = (struct ath10k_sta *)txq->sta->drv_priv;
4388 
4389 	pktlen = skb->len + 38; /* Assume MAC header 30, SNAP 8 for most case */
4390 	if (arsta->last_tx_bitrate) {
4391 		/* airtime in us, last_tx_bitrate in 100kbps */
4392 		airtime = (pktlen * 8 * (1000 / 100))
4393 				/ arsta->last_tx_bitrate;
4394 		/* overhead for media access time and IFS */
4395 		airtime += IEEE80211_ATF_OVERHEAD_IFS;
4396 	} else {
4397 		/* This is mostly for throttle excessive BC/MC frames, and the
4398 		 * airtime/rate doesn't need be exact. Airtime of BC/MC frames
4399 		 * in 2G get some discount, which helps prevent very low rate
4400 		 * frames from being blocked for too long.
4401 		 */
4402 		airtime = (pktlen * 8 * (1000 / 100)) / 60; /* 6M */
4403 		airtime += IEEE80211_ATF_OVERHEAD;
4404 	}
4405 	spin_unlock_bh(&ar->data_lock);
4406 
4407 	return airtime;
4408 }
4409 
ath10k_mac_tx_push_txq(struct ieee80211_hw * hw,struct ieee80211_txq * txq)4410 int ath10k_mac_tx_push_txq(struct ieee80211_hw *hw,
4411 			   struct ieee80211_txq *txq)
4412 {
4413 	struct ath10k *ar = hw->priv;
4414 	struct ath10k_htt *htt = &ar->htt;
4415 	struct ath10k_txq *artxq = (void *)txq->drv_priv;
4416 	struct ieee80211_vif *vif = txq->vif;
4417 	struct ieee80211_sta *sta = txq->sta;
4418 	enum ath10k_hw_txrx_mode txmode;
4419 	enum ath10k_mac_tx_path txpath;
4420 	struct sk_buff *skb;
4421 	struct ieee80211_hdr *hdr;
4422 	size_t skb_len;
4423 	bool is_mgmt, is_presp;
4424 	int ret;
4425 	u16 airtime;
4426 
4427 	spin_lock_bh(&ar->htt.tx_lock);
4428 	ret = ath10k_htt_tx_inc_pending(htt);
4429 	spin_unlock_bh(&ar->htt.tx_lock);
4430 
4431 	if (ret)
4432 		return ret;
4433 
4434 	skb = ieee80211_tx_dequeue_ni(hw, txq);
4435 	if (!skb) {
4436 		spin_lock_bh(&ar->htt.tx_lock);
4437 		ath10k_htt_tx_dec_pending(htt);
4438 		spin_unlock_bh(&ar->htt.tx_lock);
4439 
4440 		return -ENOENT;
4441 	}
4442 
4443 	airtime = ath10k_mac_update_airtime(ar, txq, skb);
4444 	ath10k_mac_tx_h_fill_cb(ar, vif, txq, sta, skb, airtime);
4445 
4446 	skb_len = skb->len;
4447 	txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4448 	txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4449 	is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
4450 
4451 	if (is_mgmt) {
4452 		hdr = (struct ieee80211_hdr *)skb->data;
4453 		is_presp = ieee80211_is_probe_resp(hdr->frame_control);
4454 
4455 		spin_lock_bh(&ar->htt.tx_lock);
4456 		ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
4457 
4458 		if (ret) {
4459 			ath10k_htt_tx_dec_pending(htt);
4460 			spin_unlock_bh(&ar->htt.tx_lock);
4461 			return ret;
4462 		}
4463 		spin_unlock_bh(&ar->htt.tx_lock);
4464 	}
4465 
4466 	ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, false);
4467 	if (unlikely(ret)) {
4468 		ath10k_warn(ar, "failed to push frame: %d\n", ret);
4469 
4470 		spin_lock_bh(&ar->htt.tx_lock);
4471 		ath10k_htt_tx_dec_pending(htt);
4472 		if (is_mgmt)
4473 			ath10k_htt_tx_mgmt_dec_pending(htt);
4474 		spin_unlock_bh(&ar->htt.tx_lock);
4475 
4476 		return ret;
4477 	}
4478 
4479 	spin_lock_bh(&ar->htt.tx_lock);
4480 	artxq->num_fw_queued++;
4481 	spin_unlock_bh(&ar->htt.tx_lock);
4482 
4483 	return skb_len;
4484 }
4485 
ath10k_mac_schedule_txq(struct ieee80211_hw * hw,u32 ac)4486 static int ath10k_mac_schedule_txq(struct ieee80211_hw *hw, u32 ac)
4487 {
4488 	struct ieee80211_txq *txq;
4489 	int ret = 0;
4490 
4491 	ieee80211_txq_schedule_start(hw, ac);
4492 	while ((txq = ieee80211_next_txq(hw, ac))) {
4493 		while (ath10k_mac_tx_can_push(hw, txq)) {
4494 			ret = ath10k_mac_tx_push_txq(hw, txq);
4495 			if (ret < 0)
4496 				break;
4497 		}
4498 		ieee80211_return_txq(hw, txq, false);
4499 		ath10k_htt_tx_txq_update(hw, txq);
4500 		if (ret == -EBUSY)
4501 			break;
4502 	}
4503 	ieee80211_txq_schedule_end(hw, ac);
4504 
4505 	return ret;
4506 }
4507 
ath10k_mac_tx_push_pending(struct ath10k * ar)4508 void ath10k_mac_tx_push_pending(struct ath10k *ar)
4509 {
4510 	struct ieee80211_hw *hw = ar->hw;
4511 	u32 ac;
4512 
4513 	if (ar->htt.tx_q_state.mode != HTT_TX_MODE_SWITCH_PUSH)
4514 		return;
4515 
4516 	if (ar->htt.num_pending_tx >= (ar->htt.max_num_pending_tx / 2))
4517 		return;
4518 
4519 	rcu_read_lock();
4520 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4521 		if (ath10k_mac_schedule_txq(hw, ac) == -EBUSY)
4522 			break;
4523 	}
4524 	rcu_read_unlock();
4525 }
4526 EXPORT_SYMBOL(ath10k_mac_tx_push_pending);
4527 
4528 /************/
4529 /* Scanning */
4530 /************/
4531 
__ath10k_scan_finish(struct ath10k * ar)4532 void __ath10k_scan_finish(struct ath10k *ar)
4533 {
4534 	lockdep_assert_held(&ar->data_lock);
4535 
4536 	switch (ar->scan.state) {
4537 	case ATH10K_SCAN_IDLE:
4538 		break;
4539 	case ATH10K_SCAN_RUNNING:
4540 	case ATH10K_SCAN_ABORTING:
4541 		if (ar->scan.is_roc && ar->scan.roc_notify)
4542 			ieee80211_remain_on_channel_expired(ar->hw);
4543 		fallthrough;
4544 	case ATH10K_SCAN_STARTING:
4545 		if (!ar->scan.is_roc) {
4546 			struct cfg80211_scan_info info = {
4547 				.aborted = ((ar->scan.state ==
4548 					    ATH10K_SCAN_ABORTING) ||
4549 					    (ar->scan.state ==
4550 					    ATH10K_SCAN_STARTING)),
4551 			};
4552 
4553 			ieee80211_scan_completed(ar->hw, &info);
4554 		}
4555 
4556 		ar->scan.state = ATH10K_SCAN_IDLE;
4557 		ar->scan_channel = NULL;
4558 		ar->scan.roc_freq = 0;
4559 		ath10k_offchan_tx_purge(ar);
4560 		cancel_delayed_work(&ar->scan.timeout);
4561 		complete(&ar->scan.completed);
4562 		break;
4563 	}
4564 }
4565 
ath10k_scan_finish(struct ath10k * ar)4566 void ath10k_scan_finish(struct ath10k *ar)
4567 {
4568 	spin_lock_bh(&ar->data_lock);
4569 	__ath10k_scan_finish(ar);
4570 	spin_unlock_bh(&ar->data_lock);
4571 }
4572 
ath10k_scan_stop(struct ath10k * ar)4573 static int ath10k_scan_stop(struct ath10k *ar)
4574 {
4575 	struct wmi_stop_scan_arg arg = {
4576 		.req_id = 1, /* FIXME */
4577 		.req_type = WMI_SCAN_STOP_ONE,
4578 		.u.scan_id = ATH10K_SCAN_ID,
4579 	};
4580 	int ret;
4581 
4582 	lockdep_assert_held(&ar->conf_mutex);
4583 
4584 	ret = ath10k_wmi_stop_scan(ar, &arg);
4585 	if (ret) {
4586 		ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
4587 		goto out;
4588 	}
4589 
4590 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
4591 	if (ret == 0) {
4592 		ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
4593 		ret = -ETIMEDOUT;
4594 	} else if (ret > 0) {
4595 		ret = 0;
4596 	}
4597 
4598 out:
4599 	/* Scan state should be updated upon scan completion but in case
4600 	 * firmware fails to deliver the event (for whatever reason) it is
4601 	 * desired to clean up scan state anyway. Firmware may have just
4602 	 * dropped the scan completion event delivery due to transport pipe
4603 	 * being overflown with data and/or it can recover on its own before
4604 	 * next scan request is submitted.
4605 	 */
4606 	spin_lock_bh(&ar->data_lock);
4607 	if (ar->scan.state != ATH10K_SCAN_IDLE)
4608 		__ath10k_scan_finish(ar);
4609 	spin_unlock_bh(&ar->data_lock);
4610 
4611 	return ret;
4612 }
4613 
ath10k_scan_abort(struct ath10k * ar)4614 static void ath10k_scan_abort(struct ath10k *ar)
4615 {
4616 	int ret;
4617 
4618 	lockdep_assert_held(&ar->conf_mutex);
4619 
4620 	spin_lock_bh(&ar->data_lock);
4621 
4622 	switch (ar->scan.state) {
4623 	case ATH10K_SCAN_IDLE:
4624 		/* This can happen if timeout worker kicked in and called
4625 		 * abortion while scan completion was being processed.
4626 		 */
4627 		break;
4628 	case ATH10K_SCAN_STARTING:
4629 	case ATH10K_SCAN_ABORTING:
4630 		ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
4631 			    ath10k_scan_state_str(ar->scan.state),
4632 			    ar->scan.state);
4633 		break;
4634 	case ATH10K_SCAN_RUNNING:
4635 		ar->scan.state = ATH10K_SCAN_ABORTING;
4636 		spin_unlock_bh(&ar->data_lock);
4637 
4638 		ret = ath10k_scan_stop(ar);
4639 		if (ret)
4640 			ath10k_warn(ar, "failed to abort scan: %d\n", ret);
4641 
4642 		spin_lock_bh(&ar->data_lock);
4643 		break;
4644 	}
4645 
4646 	spin_unlock_bh(&ar->data_lock);
4647 }
4648 
ath10k_scan_timeout_work(struct work_struct * work)4649 void ath10k_scan_timeout_work(struct work_struct *work)
4650 {
4651 	struct ath10k *ar = container_of(work, struct ath10k,
4652 					 scan.timeout.work);
4653 
4654 	mutex_lock(&ar->conf_mutex);
4655 	ath10k_scan_abort(ar);
4656 	mutex_unlock(&ar->conf_mutex);
4657 }
4658 
ath10k_start_scan(struct ath10k * ar,const struct wmi_start_scan_arg * arg)4659 static int ath10k_start_scan(struct ath10k *ar,
4660 			     const struct wmi_start_scan_arg *arg)
4661 {
4662 	int ret;
4663 
4664 	lockdep_assert_held(&ar->conf_mutex);
4665 
4666 	ret = ath10k_wmi_start_scan(ar, arg);
4667 	if (ret)
4668 		return ret;
4669 
4670 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
4671 	if (ret == 0) {
4672 		ret = ath10k_scan_stop(ar);
4673 		if (ret)
4674 			ath10k_warn(ar, "failed to stop scan: %d\n", ret);
4675 
4676 		return -ETIMEDOUT;
4677 	}
4678 
4679 	/* If we failed to start the scan, return error code at
4680 	 * this point.  This is probably due to some issue in the
4681 	 * firmware, but no need to wedge the driver due to that...
4682 	 */
4683 	spin_lock_bh(&ar->data_lock);
4684 	if (ar->scan.state == ATH10K_SCAN_IDLE) {
4685 		spin_unlock_bh(&ar->data_lock);
4686 		return -EINVAL;
4687 	}
4688 	spin_unlock_bh(&ar->data_lock);
4689 
4690 	return 0;
4691 }
4692 
4693 /**********************/
4694 /* mac80211 callbacks */
4695 /**********************/
4696 
ath10k_mac_op_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)4697 static void ath10k_mac_op_tx(struct ieee80211_hw *hw,
4698 			     struct ieee80211_tx_control *control,
4699 			     struct sk_buff *skb)
4700 {
4701 	struct ath10k *ar = hw->priv;
4702 	struct ath10k_htt *htt = &ar->htt;
4703 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4704 	struct ieee80211_vif *vif = info->control.vif;
4705 	struct ieee80211_sta *sta = control->sta;
4706 	struct ieee80211_txq *txq = NULL;
4707 	enum ath10k_hw_txrx_mode txmode;
4708 	enum ath10k_mac_tx_path txpath;
4709 	bool is_htt;
4710 	bool is_mgmt;
4711 	int ret;
4712 	u16 airtime;
4713 
4714 	airtime = ath10k_mac_update_airtime(ar, txq, skb);
4715 	ath10k_mac_tx_h_fill_cb(ar, vif, txq, sta, skb, airtime);
4716 
4717 	txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4718 	txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4719 	is_htt = (txpath == ATH10K_MAC_TX_HTT ||
4720 		  txpath == ATH10K_MAC_TX_HTT_MGMT);
4721 	is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
4722 
4723 	if (is_htt) {
4724 		bool is_presp = false;
4725 
4726 		spin_lock_bh(&ar->htt.tx_lock);
4727 		if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)) {
4728 			struct ieee80211_hdr *hdr = (void *)skb->data;
4729 
4730 			is_presp = ieee80211_is_probe_resp(hdr->frame_control);
4731 		}
4732 
4733 		ret = ath10k_htt_tx_inc_pending(htt);
4734 		if (ret) {
4735 			ath10k_warn(ar, "failed to increase tx pending count: %d, dropping\n",
4736 				    ret);
4737 			spin_unlock_bh(&ar->htt.tx_lock);
4738 			ieee80211_free_txskb(ar->hw, skb);
4739 			return;
4740 		}
4741 
4742 		ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
4743 		if (ret) {
4744 			ath10k_dbg(ar, ATH10K_DBG_MAC, "failed to increase tx mgmt pending count: %d, dropping\n",
4745 				   ret);
4746 			ath10k_htt_tx_dec_pending(htt);
4747 			spin_unlock_bh(&ar->htt.tx_lock);
4748 			ieee80211_free_txskb(ar->hw, skb);
4749 			return;
4750 		}
4751 		spin_unlock_bh(&ar->htt.tx_lock);
4752 	}
4753 
4754 	ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, false);
4755 	if (ret) {
4756 		ath10k_warn(ar, "failed to transmit frame: %d\n", ret);
4757 		if (is_htt) {
4758 			spin_lock_bh(&ar->htt.tx_lock);
4759 			ath10k_htt_tx_dec_pending(htt);
4760 			if (is_mgmt)
4761 				ath10k_htt_tx_mgmt_dec_pending(htt);
4762 			spin_unlock_bh(&ar->htt.tx_lock);
4763 		}
4764 		return;
4765 	}
4766 }
4767 
ath10k_mac_op_wake_tx_queue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)4768 static void ath10k_mac_op_wake_tx_queue(struct ieee80211_hw *hw,
4769 					struct ieee80211_txq *txq)
4770 {
4771 	struct ath10k *ar = hw->priv;
4772 	int ret;
4773 	u8 ac = txq->ac;
4774 
4775 	ath10k_htt_tx_txq_update(hw, txq);
4776 	if (ar->htt.tx_q_state.mode != HTT_TX_MODE_SWITCH_PUSH)
4777 		return;
4778 
4779 	spin_lock_bh(&ar->queue_lock[ac]);
4780 
4781 	ieee80211_txq_schedule_start(hw, ac);
4782 	txq = ieee80211_next_txq(hw, ac);
4783 	if (!txq)
4784 		goto out;
4785 
4786 	while (ath10k_mac_tx_can_push(hw, txq)) {
4787 		ret = ath10k_mac_tx_push_txq(hw, txq);
4788 		if (ret < 0)
4789 			break;
4790 	}
4791 	ieee80211_return_txq(hw, txq, false);
4792 	ath10k_htt_tx_txq_update(hw, txq);
4793 out:
4794 	ieee80211_txq_schedule_end(hw, ac);
4795 	spin_unlock_bh(&ar->queue_lock[ac]);
4796 }
4797 
4798 /* Must not be called with conf_mutex held as workers can use that also. */
ath10k_drain_tx(struct ath10k * ar)4799 void ath10k_drain_tx(struct ath10k *ar)
4800 {
4801 	lockdep_assert_not_held(&ar->conf_mutex);
4802 
4803 	/* make sure rcu-protected mac80211 tx path itself is drained */
4804 	synchronize_net();
4805 
4806 	ath10k_offchan_tx_purge(ar);
4807 	ath10k_mgmt_over_wmi_tx_purge(ar);
4808 
4809 	cancel_work_sync(&ar->offchan_tx_work);
4810 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
4811 }
4812 
ath10k_halt(struct ath10k * ar)4813 void ath10k_halt(struct ath10k *ar)
4814 {
4815 	struct ath10k_vif *arvif;
4816 
4817 	lockdep_assert_held(&ar->conf_mutex);
4818 
4819 	clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
4820 	ar->filter_flags = 0;
4821 	ar->monitor = false;
4822 	ar->monitor_arvif = NULL;
4823 
4824 	if (ar->monitor_started)
4825 		ath10k_monitor_stop(ar);
4826 
4827 	ar->monitor_started = false;
4828 	ar->tx_paused = 0;
4829 
4830 	ath10k_scan_finish(ar);
4831 	ath10k_peer_cleanup_all(ar);
4832 	ath10k_stop_radar_confirmation(ar);
4833 	ath10k_core_stop(ar);
4834 	ath10k_hif_power_down(ar);
4835 
4836 	spin_lock_bh(&ar->data_lock);
4837 	list_for_each_entry(arvif, &ar->arvifs, list)
4838 		ath10k_mac_vif_beacon_cleanup(arvif);
4839 	spin_unlock_bh(&ar->data_lock);
4840 }
4841 
ath10k_get_antenna(struct ieee80211_hw * hw,int radio_idx,u32 * tx_ant,u32 * rx_ant)4842 static int ath10k_get_antenna(struct ieee80211_hw *hw, int radio_idx,
4843 			      u32 *tx_ant, u32 *rx_ant)
4844 {
4845 	struct ath10k *ar = hw->priv;
4846 
4847 	mutex_lock(&ar->conf_mutex);
4848 
4849 	*tx_ant = ar->cfg_tx_chainmask;
4850 	*rx_ant = ar->cfg_rx_chainmask;
4851 
4852 	mutex_unlock(&ar->conf_mutex);
4853 
4854 	return 0;
4855 }
4856 
ath10k_check_chain_mask(struct ath10k * ar,u32 cm,const char * dbg)4857 static bool ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
4858 {
4859 	/* It is not clear that allowing gaps in chainmask
4860 	 * is helpful.  Probably it will not do what user
4861 	 * is hoping for, so warn in that case.
4862 	 */
4863 	if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
4864 		return true;
4865 
4866 	ath10k_warn(ar, "mac %s antenna chainmask is invalid: 0x%x.  Suggested values: 15, 7, 3, 1 or 0.\n",
4867 		    dbg, cm);
4868 	return false;
4869 }
4870 
ath10k_mac_get_vht_cap_bf_sts(struct ath10k * ar)4871 static int ath10k_mac_get_vht_cap_bf_sts(struct ath10k *ar)
4872 {
4873 	int nsts = ar->vht_cap_info;
4874 
4875 	nsts &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4876 	nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4877 
4878 	/* If firmware does not deliver to host number of space-time
4879 	 * streams supported, assume it support up to 4 BF STS and return
4880 	 * the value for VHT CAP: nsts-1)
4881 	 */
4882 	if (nsts == 0)
4883 		return 3;
4884 
4885 	return nsts;
4886 }
4887 
ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k * ar)4888 static int ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k *ar)
4889 {
4890 	int sound_dim = ar->vht_cap_info;
4891 
4892 	sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4893 	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4894 
4895 	/* If the sounding dimension is not advertised by the firmware,
4896 	 * let's use a default value of 1
4897 	 */
4898 	if (sound_dim == 0)
4899 		return 1;
4900 
4901 	return sound_dim;
4902 }
4903 
ath10k_create_vht_cap(struct ath10k * ar)4904 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4905 {
4906 	struct ieee80211_sta_vht_cap vht_cap = {};
4907 	struct ath10k_hw_params *hw = &ar->hw_params;
4908 	u16 mcs_map;
4909 	u32 val;
4910 	int i;
4911 
4912 	vht_cap.vht_supported = 1;
4913 	vht_cap.cap = ar->vht_cap_info;
4914 
4915 	if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4916 				IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
4917 		val = ath10k_mac_get_vht_cap_bf_sts(ar);
4918 		val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4919 		val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4920 
4921 		vht_cap.cap |= val;
4922 	}
4923 
4924 	if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4925 				IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
4926 		val = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
4927 		val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4928 		val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4929 
4930 		vht_cap.cap |= val;
4931 	}
4932 
4933 	mcs_map = 0;
4934 	for (i = 0; i < 8; i++) {
4935 		if ((i < ar->num_rf_chains) && (ar->cfg_tx_chainmask & BIT(i)))
4936 			mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4937 		else
4938 			mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4939 	}
4940 
4941 	if (ar->cfg_tx_chainmask <= 1)
4942 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
4943 
4944 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4945 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4946 
4947 	/* If we are supporting 160Mhz or 80+80, then the NIC may be able to do
4948 	 * a restricted NSS for 160 or 80+80 vs what it can do for 80Mhz.  Give
4949 	 * user-space a clue if that is the case.
4950 	 */
4951 	if ((vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) &&
4952 	    (hw->vht160_mcs_rx_highest != 0 ||
4953 	     hw->vht160_mcs_tx_highest != 0)) {
4954 		vht_cap.vht_mcs.rx_highest = cpu_to_le16(hw->vht160_mcs_rx_highest);
4955 		vht_cap.vht_mcs.tx_highest = cpu_to_le16(hw->vht160_mcs_tx_highest);
4956 	}
4957 
4958 	return vht_cap;
4959 }
4960 
ath10k_get_ht_cap(struct ath10k * ar)4961 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4962 {
4963 	int i;
4964 	struct ieee80211_sta_ht_cap ht_cap = {};
4965 
4966 	if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4967 		return ht_cap;
4968 
4969 	ht_cap.ht_supported = 1;
4970 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4971 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4972 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4973 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4974 	ht_cap.cap |=
4975 		WLAN_HT_CAP_SM_PS_DISABLED << IEEE80211_HT_CAP_SM_PS_SHIFT;
4976 
4977 	if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4978 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4979 
4980 	if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4981 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4982 
4983 	if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4984 		u32 smps;
4985 
4986 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
4987 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4988 
4989 		ht_cap.cap |= smps;
4990 	}
4991 
4992 	if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC && (ar->cfg_tx_chainmask > 1))
4993 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4994 
4995 	if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4996 		u32 stbc;
4997 
4998 		stbc   = ar->ht_cap_info;
4999 		stbc  &= WMI_HT_CAP_RX_STBC;
5000 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5001 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5002 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
5003 
5004 		ht_cap.cap |= stbc;
5005 	}
5006 
5007 	if (ar->ht_cap_info & WMI_HT_CAP_LDPC || (ar->ht_cap_info &
5008 	    WMI_HT_CAP_RX_LDPC && (ar->ht_cap_info & WMI_HT_CAP_TX_LDPC)))
5009 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5010 
5011 	if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5012 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5013 
5014 	/* max AMSDU is implicitly taken from vht_cap_info */
5015 	if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5016 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5017 
5018 	for (i = 0; i < ar->num_rf_chains; i++) {
5019 		if (ar->cfg_rx_chainmask & BIT(i))
5020 			ht_cap.mcs.rx_mask[i] = 0xFF;
5021 	}
5022 
5023 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5024 
5025 	return ht_cap;
5026 }
5027 
ath10k_mac_setup_ht_vht_cap(struct ath10k * ar)5028 static void ath10k_mac_setup_ht_vht_cap(struct ath10k *ar)
5029 {
5030 	struct ieee80211_supported_band *band;
5031 	struct ieee80211_sta_vht_cap vht_cap;
5032 	struct ieee80211_sta_ht_cap ht_cap;
5033 
5034 	ht_cap = ath10k_get_ht_cap(ar);
5035 	vht_cap = ath10k_create_vht_cap(ar);
5036 
5037 	if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5038 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5039 		band->ht_cap = ht_cap;
5040 	}
5041 	if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5042 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5043 		band->ht_cap = ht_cap;
5044 		band->vht_cap = vht_cap;
5045 	}
5046 }
5047 
__ath10k_set_antenna(struct ath10k * ar,u32 tx_ant,u32 rx_ant)5048 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
5049 {
5050 	int ret;
5051 	bool is_valid_tx_chain_mask, is_valid_rx_chain_mask;
5052 
5053 	lockdep_assert_held(&ar->conf_mutex);
5054 
5055 	is_valid_tx_chain_mask = ath10k_check_chain_mask(ar, tx_ant, "tx");
5056 	is_valid_rx_chain_mask = ath10k_check_chain_mask(ar, rx_ant, "rx");
5057 
5058 	if (!is_valid_tx_chain_mask || !is_valid_rx_chain_mask)
5059 		return -EINVAL;
5060 
5061 	ar->cfg_tx_chainmask = tx_ant;
5062 	ar->cfg_rx_chainmask = rx_ant;
5063 
5064 	if ((ar->state != ATH10K_STATE_ON) &&
5065 	    (ar->state != ATH10K_STATE_RESTARTED))
5066 		return 0;
5067 
5068 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
5069 					tx_ant);
5070 	if (ret) {
5071 		ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
5072 			    ret, tx_ant);
5073 		return ret;
5074 	}
5075 
5076 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
5077 					rx_ant);
5078 	if (ret) {
5079 		ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
5080 			    ret, rx_ant);
5081 		return ret;
5082 	}
5083 
5084 	/* Reload HT/VHT capability */
5085 	ath10k_mac_setup_ht_vht_cap(ar);
5086 
5087 	return 0;
5088 }
5089 
ath10k_set_antenna(struct ieee80211_hw * hw,int radio_idx,u32 tx_ant,u32 rx_ant)5090 static int ath10k_set_antenna(struct ieee80211_hw *hw, int radio_idx,
5091 			      u32 tx_ant, u32 rx_ant)
5092 {
5093 	struct ath10k *ar = hw->priv;
5094 	int ret;
5095 
5096 	mutex_lock(&ar->conf_mutex);
5097 	ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
5098 	mutex_unlock(&ar->conf_mutex);
5099 	return ret;
5100 }
5101 
__ath10k_fetch_bb_timing_dt(struct ath10k * ar,struct wmi_bb_timing_cfg_arg * bb_timing)5102 static int __ath10k_fetch_bb_timing_dt(struct ath10k *ar,
5103 				       struct wmi_bb_timing_cfg_arg *bb_timing)
5104 {
5105 #if defined(__linux__) || (defined(__FreeBSD__) && defined(CONFIG_OF))
5106 	struct device_node *node;
5107 	const char *fem_name;
5108 	int ret;
5109 
5110 	node = ar->dev->of_node;
5111 	if (!node)
5112 		return -ENOENT;
5113 
5114 	ret = of_property_read_string_index(node, "ext-fem-name", 0, &fem_name);
5115 	if (ret)
5116 		return -ENOENT;
5117 
5118 	/*
5119 	 * If external Front End module used in hardware, then default base band timing
5120 	 * parameter cannot be used since they were fine tuned for reference hardware,
5121 	 * so choosing different value suitable for that external FEM.
5122 	 */
5123 	if (!strcmp("microsemi-lx5586", fem_name)) {
5124 		bb_timing->bb_tx_timing = 0x00;
5125 		bb_timing->bb_xpa_timing = 0x0101;
5126 	} else {
5127 		return -ENOENT;
5128 	}
5129 
5130 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot bb_tx_timing 0x%x bb_xpa_timing 0x%x\n",
5131 		   bb_timing->bb_tx_timing, bb_timing->bb_xpa_timing);
5132 	return 0;
5133 #else
5134 	return -EINVAL;
5135 #endif
5136 }
5137 
ath10k_mac_rfkill_config(struct ath10k * ar)5138 static int ath10k_mac_rfkill_config(struct ath10k *ar)
5139 {
5140 	u32 param;
5141 	int ret;
5142 
5143 	if (ar->hw_values->rfkill_pin == 0) {
5144 		ath10k_warn(ar, "ath10k does not support hardware rfkill with this device\n");
5145 		return -EOPNOTSUPP;
5146 	}
5147 
5148 	ath10k_dbg(ar, ATH10K_DBG_MAC,
5149 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
5150 		   ar->hw_values->rfkill_pin, ar->hw_values->rfkill_cfg,
5151 		   ar->hw_values->rfkill_on_level);
5152 
5153 	param = FIELD_PREP(WMI_TLV_RFKILL_CFG_RADIO_LEVEL,
5154 			   ar->hw_values->rfkill_on_level) |
5155 		FIELD_PREP(WMI_TLV_RFKILL_CFG_GPIO_PIN_NUM,
5156 			   ar->hw_values->rfkill_pin) |
5157 		FIELD_PREP(WMI_TLV_RFKILL_CFG_PIN_AS_GPIO,
5158 			   ar->hw_values->rfkill_cfg);
5159 
5160 	ret = ath10k_wmi_pdev_set_param(ar,
5161 					ar->wmi.pdev_param->rfkill_config,
5162 					param);
5163 	if (ret) {
5164 		ath10k_warn(ar,
5165 			    "failed to set rfkill config 0x%x: %d\n",
5166 			    param, ret);
5167 		return ret;
5168 	}
5169 	return 0;
5170 }
5171 
ath10k_mac_rfkill_enable_radio(struct ath10k * ar,bool enable)5172 int ath10k_mac_rfkill_enable_radio(struct ath10k *ar, bool enable)
5173 {
5174 	enum wmi_tlv_rfkill_enable_radio param;
5175 	int ret;
5176 
5177 	if (enable)
5178 		param = WMI_TLV_RFKILL_ENABLE_RADIO_ON;
5179 	else
5180 		param = WMI_TLV_RFKILL_ENABLE_RADIO_OFF;
5181 
5182 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac rfkill enable %d", param);
5183 
5184 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rfkill_enable,
5185 					param);
5186 	if (ret) {
5187 		ath10k_warn(ar, "failed to set rfkill enable param %d: %d\n",
5188 			    param, ret);
5189 		return ret;
5190 	}
5191 
5192 	return 0;
5193 }
5194 
ath10k_start(struct ieee80211_hw * hw)5195 static int ath10k_start(struct ieee80211_hw *hw)
5196 {
5197 	struct ath10k *ar = hw->priv;
5198 	u32 param;
5199 	int ret = 0;
5200 	struct wmi_bb_timing_cfg_arg bb_timing = {};
5201 
5202 	/*
5203 	 * This makes sense only when restarting hw. It is harmless to call
5204 	 * unconditionally. This is necessary to make sure no HTT/WMI tx
5205 	 * commands will be submitted while restarting.
5206 	 */
5207 	ath10k_drain_tx(ar);
5208 
5209 	mutex_lock(&ar->conf_mutex);
5210 
5211 	switch (ar->state) {
5212 	case ATH10K_STATE_OFF:
5213 		ar->state = ATH10K_STATE_ON;
5214 		break;
5215 	case ATH10K_STATE_RESTARTING:
5216 		ar->state = ATH10K_STATE_RESTARTED;
5217 		break;
5218 	case ATH10K_STATE_ON:
5219 	case ATH10K_STATE_RESTARTED:
5220 	case ATH10K_STATE_WEDGED:
5221 		WARN_ON(1);
5222 		ret = -EINVAL;
5223 		goto err;
5224 	case ATH10K_STATE_UTF:
5225 		ret = -EBUSY;
5226 		goto err;
5227 	}
5228 
5229 	spin_lock_bh(&ar->data_lock);
5230 
5231 	if (ar->hw_rfkill_on) {
5232 		ar->hw_rfkill_on = false;
5233 		spin_unlock_bh(&ar->data_lock);
5234 		goto err;
5235 	}
5236 
5237 	spin_unlock_bh(&ar->data_lock);
5238 
5239 	ret = ath10k_hif_power_up(ar, ATH10K_FIRMWARE_MODE_NORMAL);
5240 	if (ret) {
5241 		ath10k_err(ar, "Could not init hif: %d\n", ret);
5242 		goto err_off;
5243 	}
5244 
5245 	ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL,
5246 				&ar->normal_mode_fw);
5247 	if (ret) {
5248 		ath10k_err(ar, "Could not init core: %d\n", ret);
5249 		goto err_power_down;
5250 	}
5251 
5252 	if (ar->sys_cap_info & WMI_TLV_SYS_CAP_INFO_RFKILL) {
5253 		ret = ath10k_mac_rfkill_config(ar);
5254 		if (ret && ret != -EOPNOTSUPP) {
5255 			ath10k_warn(ar, "failed to configure rfkill: %d", ret);
5256 			goto err_core_stop;
5257 		}
5258 	}
5259 
5260 	param = ar->wmi.pdev_param->pmf_qos;
5261 	ret = ath10k_wmi_pdev_set_param(ar, param, 1);
5262 	if (ret) {
5263 		ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
5264 		goto err_core_stop;
5265 	}
5266 
5267 	param = ar->wmi.pdev_param->dynamic_bw;
5268 	ret = ath10k_wmi_pdev_set_param(ar, param, 1);
5269 	if (ret) {
5270 		ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
5271 		goto err_core_stop;
5272 	}
5273 
5274 	if (test_bit(WMI_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi.svc_map)) {
5275 		ret = ath10k_wmi_scan_prob_req_oui(ar, ar->mac_addr);
5276 		if (ret) {
5277 			ath10k_err(ar, "failed to set prob req oui: %i\n", ret);
5278 			goto err_core_stop;
5279 		}
5280 	}
5281 
5282 	if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
5283 		ret = ath10k_wmi_adaptive_qcs(ar, true);
5284 		if (ret) {
5285 			ath10k_warn(ar, "failed to enable adaptive qcs: %d\n",
5286 				    ret);
5287 			goto err_core_stop;
5288 		}
5289 	}
5290 
5291 	if (test_bit(WMI_SERVICE_BURST, ar->wmi.svc_map)) {
5292 		param = ar->wmi.pdev_param->burst_enable;
5293 		ret = ath10k_wmi_pdev_set_param(ar, param, 0);
5294 		if (ret) {
5295 			ath10k_warn(ar, "failed to disable burst: %d\n", ret);
5296 			goto err_core_stop;
5297 		}
5298 	}
5299 
5300 	param = ar->wmi.pdev_param->idle_ps_config;
5301 	ret = ath10k_wmi_pdev_set_param(ar, param, 1);
5302 	if (ret && ret != -EOPNOTSUPP) {
5303 		ath10k_warn(ar, "failed to enable idle_ps_config: %d\n", ret);
5304 		goto err_core_stop;
5305 	}
5306 
5307 	__ath10k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
5308 
5309 	/*
5310 	 * By default FW set ARP frames ac to voice (6). In that case ARP
5311 	 * exchange is not working properly for UAPSD enabled AP. ARP requests
5312 	 * which arrives with access category 0 are processed by network stack
5313 	 * and send back with access category 0, but FW changes access category
5314 	 * to 6. Set ARP frames access category to best effort (0) solves
5315 	 * this problem.
5316 	 */
5317 
5318 	param = ar->wmi.pdev_param->arp_ac_override;
5319 	ret = ath10k_wmi_pdev_set_param(ar, param, 0);
5320 	if (ret) {
5321 		ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
5322 			    ret);
5323 		goto err_core_stop;
5324 	}
5325 
5326 	if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA,
5327 		     ar->running_fw->fw_file.fw_features)) {
5328 		ret = ath10k_wmi_pdev_enable_adaptive_cca(ar, 1,
5329 							  WMI_CCA_DETECT_LEVEL_AUTO,
5330 							  WMI_CCA_DETECT_MARGIN_AUTO);
5331 		if (ret) {
5332 			ath10k_warn(ar, "failed to enable adaptive cca: %d\n",
5333 				    ret);
5334 			goto err_core_stop;
5335 		}
5336 	}
5337 
5338 	param = ar->wmi.pdev_param->ani_enable;
5339 	ret = ath10k_wmi_pdev_set_param(ar, param, 1);
5340 	if (ret) {
5341 		ath10k_warn(ar, "failed to enable ani by default: %d\n",
5342 			    ret);
5343 		goto err_core_stop;
5344 	}
5345 
5346 	ar->ani_enabled = true;
5347 
5348 	if (ath10k_peer_stats_enabled(ar)) {
5349 		param = ar->wmi.pdev_param->peer_stats_update_period;
5350 		ret = ath10k_wmi_pdev_set_param(ar, param,
5351 						PEER_DEFAULT_STATS_UPDATE_PERIOD);
5352 		if (ret) {
5353 			ath10k_warn(ar,
5354 				    "failed to set peer stats period : %d\n",
5355 				    ret);
5356 			goto err_core_stop;
5357 		}
5358 	}
5359 
5360 	param = ar->wmi.pdev_param->enable_btcoex;
5361 	if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map) &&
5362 	    test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM,
5363 		     ar->running_fw->fw_file.fw_features) &&
5364 	    ar->coex_support) {
5365 		ret = ath10k_wmi_pdev_set_param(ar, param, 0);
5366 		if (ret) {
5367 			ath10k_warn(ar,
5368 				    "failed to set btcoex param: %d\n", ret);
5369 			goto err_core_stop;
5370 		}
5371 		clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
5372 	}
5373 
5374 	if (test_bit(WMI_SERVICE_BB_TIMING_CONFIG_SUPPORT, ar->wmi.svc_map)) {
5375 		ret = __ath10k_fetch_bb_timing_dt(ar, &bb_timing);
5376 		if (!ret) {
5377 			ret = ath10k_wmi_pdev_bb_timing(ar, &bb_timing);
5378 			if (ret) {
5379 				ath10k_warn(ar,
5380 					    "failed to set bb timings: %d\n",
5381 					    ret);
5382 				goto err_core_stop;
5383 			}
5384 		}
5385 	}
5386 
5387 	ar->num_started_vdevs = 0;
5388 	ath10k_regd_update(ar);
5389 
5390 	ath10k_spectral_start(ar);
5391 	ath10k_thermal_set_throttling(ar);
5392 
5393 	ar->radar_conf_state = ATH10K_RADAR_CONFIRMATION_IDLE;
5394 
5395 	mutex_unlock(&ar->conf_mutex);
5396 	return 0;
5397 
5398 err_core_stop:
5399 	ath10k_core_stop(ar);
5400 
5401 err_power_down:
5402 	ath10k_hif_power_down(ar);
5403 
5404 err_off:
5405 	ar->state = ATH10K_STATE_OFF;
5406 
5407 err:
5408 	mutex_unlock(&ar->conf_mutex);
5409 	return ret;
5410 }
5411 
ath10k_stop(struct ieee80211_hw * hw,bool suspend)5412 static void ath10k_stop(struct ieee80211_hw *hw, bool suspend)
5413 {
5414 	struct ath10k *ar = hw->priv;
5415 	u32 opt;
5416 
5417 	ath10k_drain_tx(ar);
5418 
5419 	mutex_lock(&ar->conf_mutex);
5420 	if (ar->state != ATH10K_STATE_OFF) {
5421 		if (!ar->hw_rfkill_on) {
5422 			/* If the current driver state is RESTARTING but not yet
5423 			 * fully RESTARTED because of incoming suspend event,
5424 			 * then ath10k_halt() is already called via
5425 			 * ath10k_core_restart() and should not be called here.
5426 			 */
5427 			if (ar->state != ATH10K_STATE_RESTARTING) {
5428 				ath10k_halt(ar);
5429 			} else {
5430 				/* Suspending here, because when in RESTARTING
5431 				 * state, ath10k_core_stop() skips
5432 				 * ath10k_wait_for_suspend().
5433 				 */
5434 				opt = WMI_PDEV_SUSPEND_AND_DISABLE_INTR;
5435 				ath10k_wait_for_suspend(ar, opt);
5436 			}
5437 		}
5438 		ar->state = ATH10K_STATE_OFF;
5439 	}
5440 	mutex_unlock(&ar->conf_mutex);
5441 
5442 	cancel_work_sync(&ar->set_coverage_class_work);
5443 	cancel_delayed_work_sync(&ar->scan.timeout);
5444 	cancel_work_sync(&ar->restart_work);
5445 }
5446 
ath10k_config_ps(struct ath10k * ar)5447 static int ath10k_config_ps(struct ath10k *ar)
5448 {
5449 	struct ath10k_vif *arvif;
5450 	int ret = 0;
5451 
5452 	lockdep_assert_held(&ar->conf_mutex);
5453 
5454 	list_for_each_entry(arvif, &ar->arvifs, list) {
5455 		ret = ath10k_mac_vif_setup_ps(arvif);
5456 		if (ret) {
5457 			ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
5458 			break;
5459 		}
5460 	}
5461 
5462 	return ret;
5463 }
5464 
ath10k_config(struct ieee80211_hw * hw,int radio_idx,u32 changed)5465 static int ath10k_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
5466 {
5467 	struct ath10k *ar = hw->priv;
5468 	struct ieee80211_conf *conf = &hw->conf;
5469 	int ret = 0;
5470 
5471 	mutex_lock(&ar->conf_mutex);
5472 
5473 	if (changed & IEEE80211_CONF_CHANGE_PS)
5474 		ath10k_config_ps(ar);
5475 
5476 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
5477 		ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
5478 		ret = ath10k_monitor_recalc(ar);
5479 		if (ret)
5480 			ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5481 	}
5482 
5483 	mutex_unlock(&ar->conf_mutex);
5484 	return ret;
5485 }
5486 
get_nss_from_chainmask(u16 chain_mask)5487 static u32 get_nss_from_chainmask(u16 chain_mask)
5488 {
5489 	if ((chain_mask & 0xf) == 0xf)
5490 		return 4;
5491 	else if ((chain_mask & 0x7) == 0x7)
5492 		return 3;
5493 	else if ((chain_mask & 0x3) == 0x3)
5494 		return 2;
5495 	return 1;
5496 }
5497 
ath10k_mac_set_txbf_conf(struct ath10k_vif * arvif)5498 static int ath10k_mac_set_txbf_conf(struct ath10k_vif *arvif)
5499 {
5500 	u32 value = 0;
5501 	struct ath10k *ar = arvif->ar;
5502 	int nsts;
5503 	int sound_dim;
5504 
5505 	if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_BEFORE_ASSOC)
5506 		return 0;
5507 
5508 	nsts = ath10k_mac_get_vht_cap_bf_sts(ar);
5509 	if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
5510 				IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE))
5511 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
5512 
5513 	sound_dim = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
5514 	if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
5515 				IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))
5516 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
5517 
5518 	if (!value)
5519 		return 0;
5520 
5521 	if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
5522 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
5523 
5524 	if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
5525 		value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFER |
5526 			  WMI_VDEV_PARAM_TXBF_SU_TX_BFER);
5527 
5528 	if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
5529 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
5530 
5531 	if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
5532 		value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFEE |
5533 			  WMI_VDEV_PARAM_TXBF_SU_TX_BFEE);
5534 
5535 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5536 					 ar->wmi.vdev_param->txbf, value);
5537 }
5538 
ath10k_update_vif_offload(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5539 static void ath10k_update_vif_offload(struct ieee80211_hw *hw,
5540 				      struct ieee80211_vif *vif)
5541 {
5542 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
5543 	struct ath10k *ar = hw->priv;
5544 	u32 vdev_param;
5545 	int ret;
5546 
5547 	if (ath10k_frame_mode != ATH10K_HW_TXRX_ETHERNET ||
5548 	    ar->wmi.vdev_param->tx_encap_type == WMI_VDEV_PARAM_UNSUPPORTED ||
5549 	     (vif->type != NL80211_IFTYPE_STATION &&
5550 	      vif->type != NL80211_IFTYPE_AP))
5551 		vif->offload_flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
5552 
5553 	vdev_param = ar->wmi.vdev_param->tx_encap_type;
5554 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5555 					ATH10K_HW_TXRX_NATIVE_WIFI);
5556 	/* 10.X firmware does not support this VDEV parameter. Do not warn */
5557 	if (ret && ret != -EOPNOTSUPP) {
5558 		ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
5559 			    arvif->vdev_id, ret);
5560 	}
5561 }
5562 
5563 /*
5564  * TODO:
5565  * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
5566  * because we will send mgmt frames without CCK. This requirement
5567  * for P2P_FIND/GO_NEG should be handled by checking CCK flag
5568  * in the TX packet.
5569  */
ath10k_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5570 static int ath10k_add_interface(struct ieee80211_hw *hw,
5571 				struct ieee80211_vif *vif)
5572 {
5573 	struct ath10k *ar = hw->priv;
5574 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
5575 	struct ath10k_peer *peer;
5576 	enum wmi_sta_powersave_param param;
5577 	int ret = 0;
5578 	u32 value;
5579 	int bit;
5580 	int i;
5581 	u32 vdev_param;
5582 
5583 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
5584 
5585 	mutex_lock(&ar->conf_mutex);
5586 
5587 	memset(arvif, 0, sizeof(*arvif));
5588 	ath10k_mac_txq_init(vif->txq);
5589 
5590 	arvif->ar = ar;
5591 	arvif->vif = vif;
5592 
5593 	INIT_LIST_HEAD(&arvif->list);
5594 	INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
5595 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
5596 			  ath10k_mac_vif_sta_connection_loss_work);
5597 
5598 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
5599 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
5600 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
5601 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
5602 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
5603 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
5604 	}
5605 
5606 	if (ar->num_peers >= ar->max_num_peers) {
5607 		ath10k_warn(ar, "refusing vdev creation due to insufficient peer entry resources in firmware\n");
5608 		ret = -ENOBUFS;
5609 		goto err;
5610 	}
5611 
5612 	if (ar->free_vdev_map == 0) {
5613 		ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
5614 		ret = -EBUSY;
5615 		goto err;
5616 	}
5617 	bit = __ffs64(ar->free_vdev_map);
5618 
5619 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
5620 		   bit, ar->free_vdev_map);
5621 
5622 	arvif->vdev_id = bit;
5623 	arvif->vdev_subtype =
5624 		ath10k_wmi_get_vdev_subtype(ar, WMI_VDEV_SUBTYPE_NONE);
5625 
5626 	switch (vif->type) {
5627 	case NL80211_IFTYPE_P2P_DEVICE:
5628 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
5629 		arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5630 					(ar, WMI_VDEV_SUBTYPE_P2P_DEVICE);
5631 		break;
5632 	case NL80211_IFTYPE_UNSPECIFIED:
5633 	case NL80211_IFTYPE_STATION:
5634 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
5635 		if (vif->p2p)
5636 			arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5637 					(ar, WMI_VDEV_SUBTYPE_P2P_CLIENT);
5638 		break;
5639 	case NL80211_IFTYPE_ADHOC:
5640 		arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
5641 		break;
5642 	case NL80211_IFTYPE_MESH_POINT:
5643 		if (test_bit(WMI_SERVICE_MESH_11S, ar->wmi.svc_map)) {
5644 			arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5645 						(ar, WMI_VDEV_SUBTYPE_MESH_11S);
5646 		} else if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
5647 			ret = -EINVAL;
5648 			ath10k_warn(ar, "must load driver with rawmode=1 to add mesh interfaces\n");
5649 			goto err;
5650 		}
5651 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
5652 		break;
5653 	case NL80211_IFTYPE_AP:
5654 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
5655 
5656 		if (vif->p2p)
5657 			arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5658 						(ar, WMI_VDEV_SUBTYPE_P2P_GO);
5659 		break;
5660 	case NL80211_IFTYPE_MONITOR:
5661 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
5662 		break;
5663 	default:
5664 		WARN_ON(1);
5665 		break;
5666 	}
5667 
5668 	/* Using vdev_id as queue number will make it very easy to do per-vif
5669 	 * tx queue locking. This shouldn't wrap due to interface combinations
5670 	 * but do a modulo for correctness sake and prevent using offchannel tx
5671 	 * queues for regular vif tx.
5672 	 */
5673 	vif->cab_queue = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
5674 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
5675 		vif->hw_queue[i] = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
5676 
5677 	/* Some firmware revisions don't wait for beacon tx completion before
5678 	 * sending another SWBA event. This could lead to hardware using old
5679 	 * (freed) beacon data in some cases, e.g. tx credit starvation
5680 	 * combined with missed TBTT. This is very rare.
5681 	 *
5682 	 * On non-IOMMU-enabled hosts this could be a possible security issue
5683 	 * because hw could beacon some random data on the air.  On
5684 	 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
5685 	 * device would crash.
5686 	 *
5687 	 * Since there are no beacon tx completions (implicit nor explicit)
5688 	 * propagated to host the only workaround for this is to allocate a
5689 	 * DMA-coherent buffer for a lifetime of a vif and use it for all
5690 	 * beacon tx commands. Worst case for this approach is some beacons may
5691 	 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
5692 	 */
5693 	if (vif->type == NL80211_IFTYPE_ADHOC ||
5694 	    vif->type == NL80211_IFTYPE_MESH_POINT ||
5695 	    vif->type == NL80211_IFTYPE_AP) {
5696 		if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL) {
5697 			arvif->beacon_buf = kmalloc(IEEE80211_MAX_FRAME_LEN,
5698 						    GFP_KERNEL);
5699 
5700 			/* Using a kernel pointer in place of a dma_addr_t
5701 			 * token can lead to undefined behavior if that
5702 			 * makes it into cache management functions. Use a
5703 			 * known-invalid address token instead, which
5704 			 * avoids the warning and makes it easier to catch
5705 			 * bugs if it does end up getting used.
5706 			 */
5707 			arvif->beacon_paddr = DMA_MAPPING_ERROR;
5708 		} else {
5709 			arvif->beacon_buf =
5710 				dma_alloc_coherent(ar->dev,
5711 						   IEEE80211_MAX_FRAME_LEN,
5712 						   &arvif->beacon_paddr,
5713 						   GFP_ATOMIC);
5714 		}
5715 		if (!arvif->beacon_buf) {
5716 			ret = -ENOMEM;
5717 			ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
5718 				    ret);
5719 			goto err;
5720 		}
5721 	}
5722 	if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
5723 		arvif->nohwcrypt = true;
5724 
5725 	if (arvif->nohwcrypt &&
5726 	    !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
5727 		ret = -EINVAL;
5728 		ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
5729 		goto err;
5730 	}
5731 
5732 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
5733 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
5734 		   arvif->beacon_buf ? "single-buf" : "per-skb");
5735 
5736 	ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
5737 				     arvif->vdev_subtype, vif->addr);
5738 	if (ret) {
5739 		ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
5740 			    arvif->vdev_id, ret);
5741 		goto err;
5742 	}
5743 
5744 	if (test_bit(WMI_SERVICE_VDEV_DISABLE_4_ADDR_SRC_LRN_SUPPORT,
5745 		     ar->wmi.svc_map)) {
5746 		vdev_param = ar->wmi.vdev_param->disable_4addr_src_lrn;
5747 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5748 						WMI_VDEV_DISABLE_4_ADDR_SRC_LRN);
5749 		if (ret && ret != -EOPNOTSUPP) {
5750 			ath10k_warn(ar, "failed to disable 4addr src lrn vdev %i: %d\n",
5751 				    arvif->vdev_id, ret);
5752 		}
5753 	}
5754 
5755 	ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
5756 	spin_lock_bh(&ar->data_lock);
5757 	list_add(&arvif->list, &ar->arvifs);
5758 	spin_unlock_bh(&ar->data_lock);
5759 
5760 	/* It makes no sense to have firmware do keepalives. mac80211 already
5761 	 * takes care of this with idle connection polling.
5762 	 */
5763 	ret = ath10k_mac_vif_disable_keepalive(arvif);
5764 	if (ret) {
5765 		ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
5766 			    arvif->vdev_id, ret);
5767 		goto err_vdev_delete;
5768 	}
5769 
5770 	arvif->def_wep_key_idx = -1;
5771 
5772 	ath10k_update_vif_offload(hw, vif);
5773 
5774 	/* Configuring number of spatial stream for monitor interface is causing
5775 	 * target assert in qca9888 and qca6174.
5776 	 */
5777 	if (ar->cfg_tx_chainmask && (vif->type != NL80211_IFTYPE_MONITOR)) {
5778 		u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5779 
5780 		vdev_param = ar->wmi.vdev_param->nss;
5781 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5782 						nss);
5783 		if (ret) {
5784 			ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
5785 				    arvif->vdev_id, ar->cfg_tx_chainmask, nss,
5786 				    ret);
5787 			goto err_vdev_delete;
5788 		}
5789 	}
5790 
5791 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5792 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5793 		ret = ath10k_peer_create(ar, vif, NULL, arvif->vdev_id,
5794 					 vif->addr, WMI_PEER_TYPE_DEFAULT);
5795 		if (ret) {
5796 			ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
5797 				    arvif->vdev_id, ret);
5798 			goto err_vdev_delete;
5799 		}
5800 
5801 		spin_lock_bh(&ar->data_lock);
5802 
5803 		peer = ath10k_peer_find(ar, arvif->vdev_id, vif->addr);
5804 		if (!peer) {
5805 			ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
5806 				    vif->addr, arvif->vdev_id);
5807 			spin_unlock_bh(&ar->data_lock);
5808 			ret = -ENOENT;
5809 			goto err_peer_delete;
5810 		}
5811 
5812 		arvif->peer_id = find_first_bit(peer->peer_ids,
5813 						ATH10K_MAX_NUM_PEER_IDS);
5814 
5815 		spin_unlock_bh(&ar->data_lock);
5816 	} else {
5817 		arvif->peer_id = HTT_INVALID_PEERID;
5818 	}
5819 
5820 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5821 		ret = ath10k_mac_set_kickout(arvif);
5822 		if (ret) {
5823 			ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
5824 				    arvif->vdev_id, ret);
5825 			goto err_peer_delete;
5826 		}
5827 	}
5828 
5829 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
5830 		param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5831 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5832 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5833 						  param, value);
5834 		if (ret) {
5835 			ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
5836 				    arvif->vdev_id, ret);
5837 			goto err_peer_delete;
5838 		}
5839 
5840 		ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
5841 		if (ret) {
5842 			ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
5843 				    arvif->vdev_id, ret);
5844 			goto err_peer_delete;
5845 		}
5846 
5847 		ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
5848 		if (ret) {
5849 			ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
5850 				    arvif->vdev_id, ret);
5851 			goto err_peer_delete;
5852 		}
5853 	}
5854 
5855 	ret = ath10k_mac_set_txbf_conf(arvif);
5856 	if (ret) {
5857 		ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
5858 			    arvif->vdev_id, ret);
5859 		goto err_peer_delete;
5860 	}
5861 
5862 	ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
5863 	if (ret) {
5864 		ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
5865 			    arvif->vdev_id, ret);
5866 		goto err_peer_delete;
5867 	}
5868 
5869 	arvif->txpower = vif->bss_conf.txpower;
5870 	ret = ath10k_mac_txpower_recalc(ar);
5871 	if (ret) {
5872 		ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5873 		goto err_peer_delete;
5874 	}
5875 
5876 	if (test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map)) {
5877 		vdev_param = ar->wmi.vdev_param->rtt_responder_role;
5878 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5879 						arvif->ftm_responder);
5880 
5881 		/* It is harmless to not set FTM role. Do not warn */
5882 		if (ret && ret != -EOPNOTSUPP)
5883 			ath10k_warn(ar, "failed to set vdev %i FTM Responder: %d\n",
5884 				    arvif->vdev_id, ret);
5885 	}
5886 
5887 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5888 		ar->monitor_arvif = arvif;
5889 		ret = ath10k_monitor_recalc(ar);
5890 		if (ret) {
5891 			ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5892 			goto err_peer_delete;
5893 		}
5894 	}
5895 
5896 	spin_lock_bh(&ar->htt.tx_lock);
5897 	if (!ar->tx_paused)
5898 		ieee80211_wake_queue(ar->hw, arvif->vdev_id);
5899 	spin_unlock_bh(&ar->htt.tx_lock);
5900 
5901 	mutex_unlock(&ar->conf_mutex);
5902 	return 0;
5903 
5904 err_peer_delete:
5905 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5906 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5907 		ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
5908 		ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5909 						 vif->addr);
5910 	}
5911 
5912 err_vdev_delete:
5913 	ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5914 	ar->free_vdev_map |= 1LL << arvif->vdev_id;
5915 	spin_lock_bh(&ar->data_lock);
5916 	list_del(&arvif->list);
5917 	spin_unlock_bh(&ar->data_lock);
5918 
5919 err:
5920 	if (arvif->beacon_buf) {
5921 		if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
5922 			kfree(arvif->beacon_buf);
5923 		else
5924 			dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
5925 					  arvif->beacon_buf,
5926 					  arvif->beacon_paddr);
5927 		arvif->beacon_buf = NULL;
5928 	}
5929 
5930 	mutex_unlock(&ar->conf_mutex);
5931 
5932 	return ret;
5933 }
5934 
ath10k_mac_vif_tx_unlock_all(struct ath10k_vif * arvif)5935 static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
5936 {
5937 	int i;
5938 
5939 	for (i = 0; i < BITS_PER_LONG; i++)
5940 		ath10k_mac_vif_tx_unlock(arvif, i);
5941 }
5942 
ath10k_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5943 static void ath10k_remove_interface(struct ieee80211_hw *hw,
5944 				    struct ieee80211_vif *vif)
5945 {
5946 	struct ath10k *ar = hw->priv;
5947 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
5948 	struct ath10k_peer *peer;
5949 	int ret;
5950 	int i;
5951 
5952 	cancel_work_sync(&arvif->ap_csa_work);
5953 	cancel_delayed_work_sync(&arvif->connection_loss_work);
5954 
5955 	mutex_lock(&ar->conf_mutex);
5956 
5957 	ret = ath10k_spectral_vif_stop(arvif);
5958 	if (ret)
5959 		ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
5960 			    arvif->vdev_id, ret);
5961 
5962 	ar->free_vdev_map |= 1LL << arvif->vdev_id;
5963 	spin_lock_bh(&ar->data_lock);
5964 	list_del(&arvif->list);
5965 	spin_unlock_bh(&ar->data_lock);
5966 
5967 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5968 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5969 		ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
5970 					     vif->addr);
5971 		if (ret)
5972 			ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
5973 				    arvif->vdev_id, ret);
5974 
5975 		ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5976 						 vif->addr);
5977 		kfree(arvif->u.ap.noa_data);
5978 	}
5979 
5980 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
5981 		   arvif->vdev_id);
5982 
5983 	ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5984 	if (ret)
5985 		ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
5986 			    arvif->vdev_id, ret);
5987 
5988 	ret = ath10k_vdev_delete_sync(ar);
5989 	if (ret) {
5990 		ath10k_warn(ar, "Error in receiving vdev delete response: %d\n", ret);
5991 		goto out;
5992 	}
5993 
5994 	/* Some firmware revisions don't notify host about self-peer removal
5995 	 * until after associated vdev is deleted.
5996 	 */
5997 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5998 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5999 		ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
6000 						   vif->addr);
6001 		if (ret)
6002 			ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
6003 				    arvif->vdev_id, ret);
6004 
6005 		spin_lock_bh(&ar->data_lock);
6006 		ar->num_peers--;
6007 		spin_unlock_bh(&ar->data_lock);
6008 	}
6009 
6010 	spin_lock_bh(&ar->data_lock);
6011 	for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
6012 		peer = ar->peer_map[i];
6013 		if (!peer)
6014 			continue;
6015 
6016 		if (peer->vif == vif) {
6017 			ath10k_warn(ar, "found vif peer %pM entry on vdev %i after it was supposedly removed\n",
6018 				    vif->addr, arvif->vdev_id);
6019 			peer->vif = NULL;
6020 		}
6021 	}
6022 
6023 	/* Clean this up late, less opportunity for firmware to access
6024 	 * DMA memory we have deleted.
6025 	 */
6026 	ath10k_mac_vif_beacon_cleanup(arvif);
6027 	spin_unlock_bh(&ar->data_lock);
6028 
6029 	ath10k_peer_cleanup(ar, arvif->vdev_id);
6030 	ath10k_mac_txq_unref(ar, vif->txq);
6031 
6032 	if (vif->type == NL80211_IFTYPE_MONITOR) {
6033 		ar->monitor_arvif = NULL;
6034 		ret = ath10k_monitor_recalc(ar);
6035 		if (ret)
6036 			ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
6037 	}
6038 
6039 	ret = ath10k_mac_txpower_recalc(ar);
6040 	if (ret)
6041 		ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
6042 
6043 	spin_lock_bh(&ar->htt.tx_lock);
6044 	ath10k_mac_vif_tx_unlock_all(arvif);
6045 	spin_unlock_bh(&ar->htt.tx_lock);
6046 
6047 	ath10k_mac_txq_unref(ar, vif->txq);
6048 
6049 out:
6050 	mutex_unlock(&ar->conf_mutex);
6051 }
6052 
6053 /*
6054  * FIXME: Has to be verified.
6055  */
6056 #define SUPPORTED_FILTERS			\
6057 	(FIF_ALLMULTI |				\
6058 	FIF_CONTROL |				\
6059 	FIF_PSPOLL |				\
6060 	FIF_OTHER_BSS |				\
6061 	FIF_BCN_PRBRESP_PROMISC |		\
6062 	FIF_PROBE_REQ |				\
6063 	FIF_FCSFAIL)
6064 
ath10k_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)6065 static void ath10k_configure_filter(struct ieee80211_hw *hw,
6066 				    unsigned int changed_flags,
6067 				    unsigned int *total_flags,
6068 				    u64 multicast)
6069 {
6070 	struct ath10k *ar = hw->priv;
6071 	int ret;
6072 	unsigned int supported = SUPPORTED_FILTERS;
6073 
6074 	mutex_lock(&ar->conf_mutex);
6075 
6076 	if (ar->hw_params.mcast_frame_registration)
6077 		supported |= FIF_MCAST_ACTION;
6078 
6079 	*total_flags &= supported;
6080 
6081 	ar->filter_flags = *total_flags;
6082 
6083 	ret = ath10k_monitor_recalc(ar);
6084 	if (ret)
6085 		ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
6086 
6087 	mutex_unlock(&ar->conf_mutex);
6088 }
6089 
ath10k_recalculate_mgmt_rate(struct ath10k * ar,struct ieee80211_vif * vif,struct cfg80211_chan_def * def)6090 static void ath10k_recalculate_mgmt_rate(struct ath10k *ar,
6091 					 struct ieee80211_vif *vif,
6092 					 struct cfg80211_chan_def *def)
6093 {
6094 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6095 	const struct ieee80211_supported_band *sband;
6096 	u8 basic_rate_idx;
6097 	int hw_rate_code;
6098 	u32 vdev_param;
6099 	u16 bitrate;
6100 	int ret;
6101 
6102 	lockdep_assert_held(&ar->conf_mutex);
6103 
6104 	sband = ar->hw->wiphy->bands[def->chan->band];
6105 	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
6106 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
6107 
6108 	hw_rate_code = ath10k_mac_get_rate_hw_value(bitrate);
6109 	if (hw_rate_code < 0) {
6110 		ath10k_warn(ar, "bitrate not supported %d\n", bitrate);
6111 		return;
6112 	}
6113 
6114 	vdev_param = ar->wmi.vdev_param->mgmt_rate;
6115 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6116 					hw_rate_code);
6117 	if (ret)
6118 		ath10k_warn(ar, "failed to set mgmt tx rate %d\n", ret);
6119 }
6120 
ath10k_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)6121 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
6122 				    struct ieee80211_vif *vif,
6123 				    struct ieee80211_bss_conf *info,
6124 				    u64 changed)
6125 {
6126 	struct ath10k *ar = hw->priv;
6127 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6128 	struct cfg80211_chan_def def;
6129 	u32 vdev_param, pdev_param, slottime, preamble;
6130 	u16 bitrate, hw_value;
6131 	u8 rate, rateidx;
6132 	int ret = 0, mcast_rate;
6133 	enum nl80211_band band;
6134 
6135 	mutex_lock(&ar->conf_mutex);
6136 
6137 	if (changed & BSS_CHANGED_IBSS)
6138 		ath10k_control_ibss(arvif, vif);
6139 
6140 	if (changed & BSS_CHANGED_BEACON_INT) {
6141 		arvif->beacon_interval = info->beacon_int;
6142 		vdev_param = ar->wmi.vdev_param->beacon_interval;
6143 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6144 						arvif->beacon_interval);
6145 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6146 			   "mac vdev %d beacon_interval %d\n",
6147 			   arvif->vdev_id, arvif->beacon_interval);
6148 
6149 		if (ret)
6150 			ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
6151 				    arvif->vdev_id, ret);
6152 	}
6153 
6154 	if (changed & BSS_CHANGED_BEACON) {
6155 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6156 			   "vdev %d set beacon tx mode to staggered\n",
6157 			   arvif->vdev_id);
6158 
6159 		pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
6160 		ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
6161 						WMI_BEACON_STAGGERED_MODE);
6162 		if (ret)
6163 			ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
6164 				    arvif->vdev_id, ret);
6165 
6166 		ret = ath10k_mac_setup_bcn_tmpl(arvif);
6167 		if (ret)
6168 			ath10k_warn(ar, "failed to update beacon template: %d\n",
6169 				    ret);
6170 
6171 		if (ieee80211_vif_is_mesh(vif)) {
6172 			/* mesh doesn't use SSID but firmware needs it */
6173 			arvif->u.ap.ssid_len = 4;
6174 			memcpy(arvif->u.ap.ssid, "mesh", arvif->u.ap.ssid_len);
6175 		}
6176 	}
6177 
6178 	if (changed & BSS_CHANGED_AP_PROBE_RESP) {
6179 		ret = ath10k_mac_setup_prb_tmpl(arvif);
6180 		if (ret)
6181 			ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
6182 				    arvif->vdev_id, ret);
6183 	}
6184 
6185 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
6186 		arvif->dtim_period = info->dtim_period;
6187 
6188 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6189 			   "mac vdev %d dtim_period %d\n",
6190 			   arvif->vdev_id, arvif->dtim_period);
6191 
6192 		vdev_param = ar->wmi.vdev_param->dtim_period;
6193 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6194 						arvif->dtim_period);
6195 		if (ret)
6196 			ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
6197 				    arvif->vdev_id, ret);
6198 	}
6199 
6200 	if (changed & BSS_CHANGED_SSID &&
6201 	    vif->type == NL80211_IFTYPE_AP) {
6202 		arvif->u.ap.ssid_len = vif->cfg.ssid_len;
6203 		if (vif->cfg.ssid_len)
6204 			memcpy(arvif->u.ap.ssid, vif->cfg.ssid,
6205 			       vif->cfg.ssid_len);
6206 		arvif->u.ap.hidden_ssid = info->hidden_ssid;
6207 	}
6208 
6209 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
6210 		ether_addr_copy(arvif->bssid, info->bssid);
6211 
6212 	if (changed & BSS_CHANGED_FTM_RESPONDER &&
6213 	    arvif->ftm_responder != info->ftm_responder &&
6214 	    test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map)) {
6215 		arvif->ftm_responder = info->ftm_responder;
6216 
6217 		vdev_param = ar->wmi.vdev_param->rtt_responder_role;
6218 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6219 						arvif->ftm_responder);
6220 
6221 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6222 			   "mac vdev %d ftm_responder %d:ret %d\n",
6223 			   arvif->vdev_id, arvif->ftm_responder, ret);
6224 	}
6225 
6226 	if (changed & BSS_CHANGED_BEACON_ENABLED)
6227 		ath10k_control_beaconing(arvif, info);
6228 
6229 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
6230 		arvif->use_cts_prot = info->use_cts_prot;
6231 
6232 		ret = ath10k_recalc_rtscts_prot(arvif);
6233 		if (ret)
6234 			ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
6235 				    arvif->vdev_id, ret);
6236 
6237 		if (ath10k_mac_can_set_cts_prot(arvif)) {
6238 			ret = ath10k_mac_set_cts_prot(arvif);
6239 			if (ret)
6240 				ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
6241 					    arvif->vdev_id, ret);
6242 		}
6243 	}
6244 
6245 	if (changed & BSS_CHANGED_ERP_SLOT) {
6246 		if (info->use_short_slot)
6247 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
6248 
6249 		else
6250 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
6251 
6252 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
6253 			   arvif->vdev_id, slottime);
6254 
6255 		vdev_param = ar->wmi.vdev_param->slot_time;
6256 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6257 						slottime);
6258 		if (ret)
6259 			ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
6260 				    arvif->vdev_id, ret);
6261 	}
6262 
6263 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
6264 		if (info->use_short_preamble)
6265 			preamble = WMI_VDEV_PREAMBLE_SHORT;
6266 		else
6267 			preamble = WMI_VDEV_PREAMBLE_LONG;
6268 
6269 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6270 			   "mac vdev %d preamble %dn",
6271 			   arvif->vdev_id, preamble);
6272 
6273 		vdev_param = ar->wmi.vdev_param->preamble;
6274 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6275 						preamble);
6276 		if (ret)
6277 			ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
6278 				    arvif->vdev_id, ret);
6279 	}
6280 
6281 	if (changed & BSS_CHANGED_ASSOC) {
6282 		if (vif->cfg.assoc) {
6283 			/* Workaround: Make sure monitor vdev is not running
6284 			 * when associating to prevent some firmware revisions
6285 			 * (e.g. 10.1 and 10.2) from crashing.
6286 			 */
6287 			if (ar->monitor_started)
6288 				ath10k_monitor_stop(ar);
6289 			ath10k_bss_assoc(hw, vif, info);
6290 			ath10k_monitor_recalc(ar);
6291 		} else {
6292 			ath10k_bss_disassoc(hw, vif);
6293 		}
6294 	}
6295 
6296 	if (changed & BSS_CHANGED_TXPOWER) {
6297 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
6298 			   arvif->vdev_id, info->txpower);
6299 
6300 		arvif->txpower = info->txpower;
6301 		ret = ath10k_mac_txpower_recalc(ar);
6302 		if (ret)
6303 			ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
6304 	}
6305 
6306 	if (changed & BSS_CHANGED_PS) {
6307 		arvif->ps = vif->cfg.ps;
6308 
6309 		ret = ath10k_config_ps(ar);
6310 		if (ret)
6311 			ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
6312 				    arvif->vdev_id, ret);
6313 	}
6314 
6315 	if (changed & BSS_CHANGED_MCAST_RATE &&
6316 	    !ath10k_mac_vif_chan(arvif->vif, &def)) {
6317 		band = def.chan->band;
6318 		mcast_rate = vif->bss_conf.mcast_rate[band];
6319 		if (mcast_rate > 0)
6320 			rateidx = mcast_rate - 1;
6321 		else
6322 			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
6323 
6324 		if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
6325 			rateidx += ATH10K_MAC_FIRST_OFDM_RATE_IDX;
6326 
6327 		bitrate = ath10k_wmi_legacy_rates[rateidx].bitrate;
6328 		hw_value = ath10k_wmi_legacy_rates[rateidx].hw_value;
6329 		if (ath10k_mac_bitrate_is_cck(bitrate))
6330 			preamble = WMI_RATE_PREAMBLE_CCK;
6331 		else
6332 			preamble = WMI_RATE_PREAMBLE_OFDM;
6333 
6334 		rate = ATH10K_HW_RATECODE(hw_value, 0, preamble);
6335 
6336 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6337 			   "mac vdev %d mcast_rate %x\n",
6338 			   arvif->vdev_id, rate);
6339 
6340 		vdev_param = ar->wmi.vdev_param->mcast_data_rate;
6341 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
6342 						vdev_param, rate);
6343 		if (ret)
6344 			ath10k_warn(ar,
6345 				    "failed to set mcast rate on vdev %i: %d\n",
6346 				    arvif->vdev_id,  ret);
6347 
6348 		vdev_param = ar->wmi.vdev_param->bcast_data_rate;
6349 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
6350 						vdev_param, rate);
6351 		if (ret)
6352 			ath10k_warn(ar,
6353 				    "failed to set bcast rate on vdev %i: %d\n",
6354 				    arvif->vdev_id,  ret);
6355 	}
6356 
6357 	if (changed & BSS_CHANGED_BASIC_RATES &&
6358 	    !ath10k_mac_vif_chan(arvif->vif, &def))
6359 		ath10k_recalculate_mgmt_rate(ar, vif, &def);
6360 
6361 	mutex_unlock(&ar->conf_mutex);
6362 }
6363 
ath10k_mac_op_set_coverage_class(struct ieee80211_hw * hw,int radio_idx,s16 value)6364 static void ath10k_mac_op_set_coverage_class(struct ieee80211_hw *hw, int radio_idx,
6365 					     s16 value)
6366 {
6367 	struct ath10k *ar = hw->priv;
6368 
6369 	/* This function should never be called if setting the coverage class
6370 	 * is not supported on this hardware.
6371 	 */
6372 	if (!ar->hw_params.hw_ops->set_coverage_class) {
6373 		WARN_ON_ONCE(1);
6374 		return;
6375 	}
6376 	ar->hw_params.hw_ops->set_coverage_class(ar, -1, value);
6377 }
6378 
6379 struct ath10k_mac_tdls_iter_data {
6380 	u32 num_tdls_stations;
6381 	struct ieee80211_vif *curr_vif;
6382 };
6383 
ath10k_mac_tdls_vif_stations_count_iter(void * data,struct ieee80211_sta * sta)6384 static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
6385 						    struct ieee80211_sta *sta)
6386 {
6387 	struct ath10k_mac_tdls_iter_data *iter_data = data;
6388 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6389 	struct ieee80211_vif *sta_vif = arsta->arvif->vif;
6390 
6391 	if (sta->tdls && sta_vif == iter_data->curr_vif)
6392 		iter_data->num_tdls_stations++;
6393 }
6394 
ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw * hw,struct ieee80211_vif * vif)6395 static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
6396 					      struct ieee80211_vif *vif)
6397 {
6398 	struct ath10k_mac_tdls_iter_data data = {};
6399 
6400 	data.curr_vif = vif;
6401 
6402 	ieee80211_iterate_stations_atomic(hw,
6403 					  ath10k_mac_tdls_vif_stations_count_iter,
6404 					  &data);
6405 	return data.num_tdls_stations;
6406 }
6407 
ath10k_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)6408 static int ath10k_hw_scan(struct ieee80211_hw *hw,
6409 			  struct ieee80211_vif *vif,
6410 			  struct ieee80211_scan_request *hw_req)
6411 {
6412 	struct ath10k *ar = hw->priv;
6413 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6414 	struct cfg80211_scan_request *req = &hw_req->req;
6415 	struct wmi_start_scan_arg *arg = NULL;
6416 	int ret = 0;
6417 	int i;
6418 	u32 scan_timeout;
6419 
6420 	mutex_lock(&ar->conf_mutex);
6421 
6422 	if (ath10k_mac_tdls_vif_stations_count(hw, vif) > 0) {
6423 		ret = -EBUSY;
6424 		goto exit;
6425 	}
6426 
6427 	spin_lock_bh(&ar->data_lock);
6428 	switch (ar->scan.state) {
6429 	case ATH10K_SCAN_IDLE:
6430 		reinit_completion(&ar->scan.started);
6431 		reinit_completion(&ar->scan.completed);
6432 		ar->scan.state = ATH10K_SCAN_STARTING;
6433 		ar->scan.is_roc = false;
6434 		ar->scan.vdev_id = arvif->vdev_id;
6435 		ret = 0;
6436 		break;
6437 	case ATH10K_SCAN_STARTING:
6438 	case ATH10K_SCAN_RUNNING:
6439 	case ATH10K_SCAN_ABORTING:
6440 		ret = -EBUSY;
6441 		break;
6442 	}
6443 	spin_unlock_bh(&ar->data_lock);
6444 
6445 	if (ret)
6446 		goto exit;
6447 
6448 	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
6449 	if (!arg) {
6450 		ret = -ENOMEM;
6451 		goto exit;
6452 	}
6453 
6454 	ath10k_wmi_start_scan_init(ar, arg);
6455 	arg->vdev_id = arvif->vdev_id;
6456 	arg->scan_id = ATH10K_SCAN_ID;
6457 
6458 	if (req->ie_len) {
6459 		arg->ie_len = req->ie_len;
6460 		memcpy(arg->ie, req->ie, arg->ie_len);
6461 	}
6462 
6463 	if (req->n_ssids) {
6464 		arg->n_ssids = req->n_ssids;
6465 		for (i = 0; i < arg->n_ssids; i++) {
6466 			arg->ssids[i].len  = req->ssids[i].ssid_len;
6467 			arg->ssids[i].ssid = req->ssids[i].ssid;
6468 		}
6469 	} else {
6470 		arg->scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
6471 	}
6472 
6473 	if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6474 		arg->scan_ctrl_flags |=  WMI_SCAN_ADD_SPOOFED_MAC_IN_PROBE_REQ;
6475 		ether_addr_copy(arg->mac_addr.addr, req->mac_addr);
6476 		ether_addr_copy(arg->mac_mask.addr, req->mac_addr_mask);
6477 	}
6478 
6479 	if (req->n_channels) {
6480 		arg->n_channels = req->n_channels;
6481 		for (i = 0; i < arg->n_channels; i++)
6482 			arg->channels[i] = req->channels[i]->center_freq;
6483 	}
6484 
6485 	/* if duration is set, default dwell times will be overwritten */
6486 	if (req->duration) {
6487 		arg->dwell_time_active = req->duration;
6488 		arg->dwell_time_passive = req->duration;
6489 		arg->burst_duration_ms = req->duration;
6490 
6491 		scan_timeout = min_t(u32, arg->max_rest_time *
6492 				(arg->n_channels - 1) + (req->duration +
6493 				ATH10K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD) *
6494 				arg->n_channels, arg->max_scan_time);
6495 	} else {
6496 		scan_timeout = arg->max_scan_time;
6497 	}
6498 
6499 	/* Add a 200ms margin to account for event/command processing */
6500 	scan_timeout += 200;
6501 
6502 	ret = ath10k_start_scan(ar, arg);
6503 	if (ret) {
6504 		ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
6505 		spin_lock_bh(&ar->data_lock);
6506 		ar->scan.state = ATH10K_SCAN_IDLE;
6507 		spin_unlock_bh(&ar->data_lock);
6508 	}
6509 
6510 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
6511 				     msecs_to_jiffies(scan_timeout));
6512 
6513 exit:
6514 	kfree(arg);
6515 
6516 	mutex_unlock(&ar->conf_mutex);
6517 	return ret;
6518 }
6519 
ath10k_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)6520 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
6521 				  struct ieee80211_vif *vif)
6522 {
6523 	struct ath10k *ar = hw->priv;
6524 
6525 	mutex_lock(&ar->conf_mutex);
6526 	ath10k_scan_abort(ar);
6527 	mutex_unlock(&ar->conf_mutex);
6528 
6529 	cancel_delayed_work_sync(&ar->scan.timeout);
6530 }
6531 
ath10k_set_key_h_def_keyidx(struct ath10k * ar,struct ath10k_vif * arvif,enum set_key_cmd cmd,struct ieee80211_key_conf * key)6532 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
6533 					struct ath10k_vif *arvif,
6534 					enum set_key_cmd cmd,
6535 					struct ieee80211_key_conf *key)
6536 {
6537 	u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
6538 	int ret;
6539 
6540 	/* 10.1 firmware branch requires default key index to be set to group
6541 	 * key index after installing it. Otherwise FW/HW Txes corrupted
6542 	 * frames with multi-vif APs. This is not required for main firmware
6543 	 * branch (e.g. 636).
6544 	 *
6545 	 * This is also needed for 636 fw for IBSS-RSN to work more reliably.
6546 	 *
6547 	 * FIXME: It remains unknown if this is required for multi-vif STA
6548 	 * interfaces on 10.1.
6549 	 */
6550 
6551 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6552 	    arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
6553 		return;
6554 
6555 	if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
6556 		return;
6557 
6558 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
6559 		return;
6560 
6561 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6562 		return;
6563 
6564 	if (cmd != SET_KEY)
6565 		return;
6566 
6567 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6568 					key->keyidx);
6569 	if (ret)
6570 		ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
6571 			    arvif->vdev_id, ret);
6572 }
6573 
ath10k_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)6574 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6575 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6576 			  struct ieee80211_key_conf *key)
6577 {
6578 	struct ath10k *ar = hw->priv;
6579 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6580 	struct ath10k_sta *arsta;
6581 	struct ath10k_peer *peer;
6582 	const u8 *peer_addr;
6583 	bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
6584 		      key->cipher == WLAN_CIPHER_SUITE_WEP104;
6585 	int ret = 0;
6586 	int ret2;
6587 	u32 flags = 0;
6588 	u32 flags2;
6589 
6590 	/* this one needs to be done in software */
6591 	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
6592 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
6593 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
6594 	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
6595 		return 1;
6596 
6597 	if (arvif->nohwcrypt)
6598 		return 1;
6599 
6600 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6601 		return -ENOSPC;
6602 
6603 	mutex_lock(&ar->conf_mutex);
6604 
6605 	if (sta) {
6606 		arsta = (struct ath10k_sta *)sta->drv_priv;
6607 		peer_addr = sta->addr;
6608 		spin_lock_bh(&ar->data_lock);
6609 		arsta->ucast_cipher = key->cipher;
6610 		spin_unlock_bh(&ar->data_lock);
6611 	} else if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
6612 		peer_addr = vif->bss_conf.bssid;
6613 	} else {
6614 		peer_addr = vif->addr;
6615 	}
6616 
6617 	key->hw_key_idx = key->keyidx;
6618 
6619 	if (is_wep) {
6620 		if (cmd == SET_KEY)
6621 			arvif->wep_keys[key->keyidx] = key;
6622 		else
6623 			arvif->wep_keys[key->keyidx] = NULL;
6624 	}
6625 
6626 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6627 	 * we already hold conf_mutex. we just make sure its there now.
6628 	 */
6629 	spin_lock_bh(&ar->data_lock);
6630 	peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
6631 	spin_unlock_bh(&ar->data_lock);
6632 
6633 	if (!peer) {
6634 		if (cmd == SET_KEY) {
6635 			ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
6636 				    peer_addr);
6637 			ret = -EOPNOTSUPP;
6638 			goto exit;
6639 		} else {
6640 			/* if the peer doesn't exist there is no key to disable anymore */
6641 			goto exit;
6642 		}
6643 	}
6644 
6645 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6646 		flags |= WMI_KEY_PAIRWISE;
6647 	else
6648 		flags |= WMI_KEY_GROUP;
6649 
6650 	if (is_wep) {
6651 		if (cmd == DISABLE_KEY)
6652 			ath10k_clear_vdev_key(arvif, key);
6653 
6654 		/* When WEP keys are uploaded it's possible that there are
6655 		 * stations associated already (e.g. when merging) without any
6656 		 * keys. Static WEP needs an explicit per-peer key upload.
6657 		 */
6658 		if (vif->type == NL80211_IFTYPE_ADHOC &&
6659 		    cmd == SET_KEY)
6660 			ath10k_mac_vif_update_wep_key(arvif, key);
6661 
6662 		/* 802.1x never sets the def_wep_key_idx so each set_key()
6663 		 * call changes default tx key.
6664 		 *
6665 		 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
6666 		 * after first set_key().
6667 		 */
6668 		if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
6669 			flags |= WMI_KEY_TX_USAGE;
6670 	}
6671 
6672 	ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
6673 	if (ret) {
6674 		WARN_ON(ret > 0);
6675 		ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
6676 			    arvif->vdev_id, peer_addr, ret);
6677 		goto exit;
6678 	}
6679 
6680 	/* mac80211 sets static WEP keys as groupwise while firmware requires
6681 	 * them to be installed twice as both pairwise and groupwise.
6682 	 */
6683 	if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
6684 		flags2 = flags;
6685 		flags2 &= ~WMI_KEY_GROUP;
6686 		flags2 |= WMI_KEY_PAIRWISE;
6687 
6688 		ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
6689 		if (ret) {
6690 			WARN_ON(ret > 0);
6691 			ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
6692 				    arvif->vdev_id, peer_addr, ret);
6693 			ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
6694 						  peer_addr, flags);
6695 			if (ret2) {
6696 				WARN_ON(ret2 > 0);
6697 				ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
6698 					    arvif->vdev_id, peer_addr, ret2);
6699 			}
6700 			goto exit;
6701 		}
6702 	}
6703 
6704 	ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
6705 
6706 	spin_lock_bh(&ar->data_lock);
6707 	peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
6708 	if (peer && cmd == SET_KEY)
6709 		peer->keys[key->keyidx] = key;
6710 	else if (peer && cmd == DISABLE_KEY)
6711 		peer->keys[key->keyidx] = NULL;
6712 	else if (peer == NULL)
6713 		/* impossible unless FW goes crazy */
6714 		ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
6715 	spin_unlock_bh(&ar->data_lock);
6716 
6717 	if (sta && sta->tdls)
6718 		ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6719 					  ar->wmi.peer_param->authorize, 1);
6720 	else if (sta && cmd == SET_KEY && (key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
6721 		ath10k_wmi_peer_set_param(ar, arvif->vdev_id, peer_addr,
6722 					  ar->wmi.peer_param->authorize, 1);
6723 
6724 exit:
6725 	mutex_unlock(&ar->conf_mutex);
6726 	return ret;
6727 }
6728 
ath10k_set_default_unicast_key(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int keyidx)6729 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
6730 					   struct ieee80211_vif *vif,
6731 					   int keyidx)
6732 {
6733 	struct ath10k *ar = hw->priv;
6734 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6735 	int ret;
6736 
6737 	mutex_lock(&arvif->ar->conf_mutex);
6738 
6739 	if (arvif->ar->state != ATH10K_STATE_ON)
6740 		goto unlock;
6741 
6742 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
6743 		   arvif->vdev_id, keyidx);
6744 
6745 	ret = ath10k_wmi_vdev_set_param(arvif->ar,
6746 					arvif->vdev_id,
6747 					arvif->ar->wmi.vdev_param->def_keyid,
6748 					keyidx);
6749 
6750 	if (ret) {
6751 		ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
6752 			    arvif->vdev_id,
6753 			    ret);
6754 		goto unlock;
6755 	}
6756 
6757 	arvif->def_wep_key_idx = keyidx;
6758 
6759 unlock:
6760 	mutex_unlock(&arvif->ar->conf_mutex);
6761 }
6762 
ath10k_sta_rc_update_wk(struct work_struct * wk)6763 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
6764 {
6765 	struct ath10k *ar;
6766 	struct ath10k_vif *arvif;
6767 	struct ath10k_sta *arsta;
6768 	struct ieee80211_sta *sta;
6769 	struct cfg80211_chan_def def;
6770 	enum nl80211_band band;
6771 	const u8 *ht_mcs_mask;
6772 	const u16 *vht_mcs_mask;
6773 	u32 changed, bw, nss, smps;
6774 	int err;
6775 
6776 	arsta = container_of(wk, struct ath10k_sta, update_wk);
6777 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
6778 	arvif = arsta->arvif;
6779 	ar = arvif->ar;
6780 
6781 	if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
6782 		return;
6783 
6784 	band = def.chan->band;
6785 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6786 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6787 
6788 	spin_lock_bh(&ar->data_lock);
6789 
6790 	changed = arsta->changed;
6791 	arsta->changed = 0;
6792 
6793 	bw = arsta->bw;
6794 	nss = arsta->nss;
6795 	smps = arsta->smps;
6796 
6797 	spin_unlock_bh(&ar->data_lock);
6798 
6799 	mutex_lock(&ar->conf_mutex);
6800 
6801 	nss = max_t(u32, 1, nss);
6802 	nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
6803 			   ath10k_mac_max_vht_nss(vht_mcs_mask)));
6804 
6805 	if (changed & IEEE80211_RC_BW_CHANGED) {
6806 		enum wmi_phy_mode mode;
6807 
6808 		mode = chan_to_phymode(&def);
6809 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM peer bw %d phymode %d\n",
6810 			   sta->addr, bw, mode);
6811 
6812 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6813 						ar->wmi.peer_param->phymode, mode);
6814 		if (err) {
6815 			ath10k_warn(ar, "failed to update STA %pM peer phymode %d: %d\n",
6816 				    sta->addr, mode, err);
6817 			goto exit;
6818 		}
6819 
6820 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6821 						ar->wmi.peer_param->chan_width, bw);
6822 		if (err)
6823 			ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
6824 				    sta->addr, bw, err);
6825 	}
6826 
6827 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6828 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM nss %d\n",
6829 			   sta->addr, nss);
6830 
6831 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6832 						ar->wmi.peer_param->nss, nss);
6833 		if (err)
6834 			ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
6835 				    sta->addr, nss, err);
6836 	}
6837 
6838 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6839 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM smps %d\n",
6840 			   sta->addr, smps);
6841 
6842 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6843 						ar->wmi.peer_param->smps_state, smps);
6844 		if (err)
6845 			ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
6846 				    sta->addr, smps, err);
6847 	}
6848 
6849 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6850 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM supp rates\n",
6851 			   sta->addr);
6852 
6853 		err = ath10k_station_assoc(ar, arvif->vif, sta, true);
6854 		if (err)
6855 			ath10k_warn(ar, "failed to reassociate station: %pM\n",
6856 				    sta->addr);
6857 	}
6858 
6859 exit:
6860 	mutex_unlock(&ar->conf_mutex);
6861 }
6862 
ath10k_mac_inc_num_stations(struct ath10k_vif * arvif,struct ieee80211_sta * sta)6863 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
6864 				       struct ieee80211_sta *sta)
6865 {
6866 	struct ath10k *ar = arvif->ar;
6867 
6868 	lockdep_assert_held(&ar->conf_mutex);
6869 
6870 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6871 		return 0;
6872 
6873 	if (ar->num_stations >= ar->max_num_stations)
6874 		return -ENOBUFS;
6875 
6876 	ar->num_stations++;
6877 
6878 	return 0;
6879 }
6880 
ath10k_mac_dec_num_stations(struct ath10k_vif * arvif,struct ieee80211_sta * sta)6881 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
6882 					struct ieee80211_sta *sta)
6883 {
6884 	struct ath10k *ar = arvif->ar;
6885 
6886 	lockdep_assert_held(&ar->conf_mutex);
6887 
6888 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6889 		return;
6890 
6891 	ar->num_stations--;
6892 }
6893 
ath10k_sta_set_txpwr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)6894 static int ath10k_sta_set_txpwr(struct ieee80211_hw *hw,
6895 				struct ieee80211_vif *vif,
6896 				struct ieee80211_sta *sta)
6897 {
6898 	struct ath10k *ar = hw->priv;
6899 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6900 	int ret = 0;
6901 	s16 txpwr;
6902 
6903 	if (sta->deflink.txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
6904 		txpwr = 0;
6905 	} else {
6906 		txpwr = sta->deflink.txpwr.power;
6907 		if (!txpwr)
6908 			return -EINVAL;
6909 	}
6910 
6911 	if (txpwr > ATH10K_TX_POWER_MAX_VAL || txpwr < ATH10K_TX_POWER_MIN_VAL)
6912 		return -EINVAL;
6913 
6914 	mutex_lock(&ar->conf_mutex);
6915 
6916 	ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6917 					ar->wmi.peer_param->use_fixed_power, txpwr);
6918 	if (ret) {
6919 		ath10k_warn(ar, "failed to set tx power for station ret: %d\n",
6920 			    ret);
6921 		goto out;
6922 	}
6923 
6924 out:
6925 	mutex_unlock(&ar->conf_mutex);
6926 	return ret;
6927 }
6928 
6929 struct ath10k_mac_iter_tid_conf_data {
6930 	struct ieee80211_vif *curr_vif;
6931 	struct ath10k *ar;
6932 	bool reset_config;
6933 };
6934 
6935 static bool
ath10k_mac_bitrate_mask_has_single_rate(struct ath10k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask,int * vht_num_rates)6936 ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
6937 					enum nl80211_band band,
6938 					const struct cfg80211_bitrate_mask *mask,
6939 					int *vht_num_rates)
6940 {
6941 	int num_rates = 0;
6942 	int i, tmp;
6943 
6944 	num_rates += hweight32(mask->control[band].legacy);
6945 
6946 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6947 		num_rates += hweight8(mask->control[band].ht_mcs[i]);
6948 
6949 	*vht_num_rates = 0;
6950 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6951 		tmp = hweight16(mask->control[band].vht_mcs[i]);
6952 		num_rates += tmp;
6953 		*vht_num_rates += tmp;
6954 	}
6955 
6956 	return num_rates == 1;
6957 }
6958 
6959 static int
ath10k_mac_bitrate_mask_get_single_rate(struct ath10k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask,u8 * rate,u8 * nss,bool vht_only)6960 ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
6961 					enum nl80211_band band,
6962 					const struct cfg80211_bitrate_mask *mask,
6963 					u8 *rate, u8 *nss, bool vht_only)
6964 {
6965 	int rate_idx;
6966 	int i;
6967 	u16 bitrate;
6968 	u8 preamble;
6969 	u8 hw_rate;
6970 
6971 	if (vht_only)
6972 		goto next;
6973 
6974 	if (hweight32(mask->control[band].legacy) == 1) {
6975 		rate_idx = ffs(mask->control[band].legacy) - 1;
6976 
6977 		if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
6978 			rate_idx += ATH10K_MAC_FIRST_OFDM_RATE_IDX;
6979 
6980 		hw_rate = ath10k_wmi_legacy_rates[rate_idx].hw_value;
6981 		bitrate = ath10k_wmi_legacy_rates[rate_idx].bitrate;
6982 
6983 		if (ath10k_mac_bitrate_is_cck(bitrate))
6984 			preamble = WMI_RATE_PREAMBLE_CCK;
6985 		else
6986 			preamble = WMI_RATE_PREAMBLE_OFDM;
6987 
6988 		*nss = 1;
6989 		*rate = preamble << 6 |
6990 			(*nss - 1) << 4 |
6991 			hw_rate << 0;
6992 
6993 		return 0;
6994 	}
6995 
6996 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6997 		if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
6998 			*nss = i + 1;
6999 			*rate = WMI_RATE_PREAMBLE_HT << 6 |
7000 				(*nss - 1) << 4 |
7001 				(ffs(mask->control[band].ht_mcs[i]) - 1);
7002 
7003 			return 0;
7004 		}
7005 	}
7006 
7007 next:
7008 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
7009 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
7010 			*nss = i + 1;
7011 			*rate = WMI_RATE_PREAMBLE_VHT << 6 |
7012 				(*nss - 1) << 4 |
7013 				(ffs(mask->control[band].vht_mcs[i]) - 1);
7014 
7015 			return 0;
7016 		}
7017 	}
7018 
7019 	return -EINVAL;
7020 }
7021 
ath10k_mac_validate_rate_mask(struct ath10k * ar,struct ieee80211_sta * sta,u32 rate_ctrl_flag,u8 nss)7022 static int ath10k_mac_validate_rate_mask(struct ath10k *ar,
7023 					 struct ieee80211_sta *sta,
7024 					 u32 rate_ctrl_flag, u8 nss)
7025 {
7026 	struct ieee80211_sta_ht_cap *ht_cap = &sta->deflink.ht_cap;
7027 	struct ieee80211_sta_vht_cap *vht_cap = &sta->deflink.vht_cap;
7028 
7029 	if (nss > sta->deflink.rx_nss) {
7030 		ath10k_warn(ar, "Invalid nss field, configured %u limit %u\n",
7031 			    nss, sta->deflink.rx_nss);
7032 		return -EINVAL;
7033 	}
7034 
7035 	if (ATH10K_HW_PREAMBLE(rate_ctrl_flag) == WMI_RATE_PREAMBLE_VHT) {
7036 		if (!vht_cap->vht_supported) {
7037 			ath10k_warn(ar, "Invalid VHT rate for sta %pM\n",
7038 				    sta->addr);
7039 			return -EINVAL;
7040 		}
7041 	} else if (ATH10K_HW_PREAMBLE(rate_ctrl_flag) == WMI_RATE_PREAMBLE_HT) {
7042 		if (!ht_cap->ht_supported || vht_cap->vht_supported) {
7043 			ath10k_warn(ar, "Invalid HT rate for sta %pM\n",
7044 				    sta->addr);
7045 			return -EINVAL;
7046 		}
7047 	} else {
7048 		if (ht_cap->ht_supported || vht_cap->vht_supported)
7049 			return -EINVAL;
7050 	}
7051 
7052 	return 0;
7053 }
7054 
7055 static int
ath10k_mac_tid_bitrate_config(struct ath10k * ar,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 * rate_ctrl_flag,u8 * rate_ctrl,enum nl80211_tx_rate_setting txrate_type,const struct cfg80211_bitrate_mask * mask)7056 ath10k_mac_tid_bitrate_config(struct ath10k *ar,
7057 			      struct ieee80211_vif *vif,
7058 			      struct ieee80211_sta *sta,
7059 			      u32 *rate_ctrl_flag, u8 *rate_ctrl,
7060 			      enum nl80211_tx_rate_setting txrate_type,
7061 			      const struct cfg80211_bitrate_mask *mask)
7062 {
7063 	struct cfg80211_chan_def def;
7064 	enum nl80211_band band;
7065 	u8 nss, rate;
7066 	int vht_num_rates, ret;
7067 
7068 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
7069 		return -EINVAL;
7070 
7071 	if (txrate_type == NL80211_TX_RATE_AUTOMATIC) {
7072 		*rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_AUTO;
7073 		*rate_ctrl_flag = 0;
7074 		return 0;
7075 	}
7076 
7077 	band = def.chan->band;
7078 
7079 	if (!ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask,
7080 						     &vht_num_rates)) {
7081 		return -EINVAL;
7082 	}
7083 
7084 	ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
7085 						      &rate, &nss, false);
7086 	if (ret) {
7087 		ath10k_warn(ar, "failed to get single rate: %d\n",
7088 			    ret);
7089 		return ret;
7090 	}
7091 
7092 	*rate_ctrl_flag = rate;
7093 
7094 	if (sta && ath10k_mac_validate_rate_mask(ar, sta, *rate_ctrl_flag, nss))
7095 		return -EINVAL;
7096 
7097 	if (txrate_type == NL80211_TX_RATE_FIXED)
7098 		*rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_FIXED_RATE;
7099 	else if (txrate_type == NL80211_TX_RATE_LIMITED &&
7100 		 (test_bit(WMI_SERVICE_EXT_PEER_TID_CONFIGS_SUPPORT,
7101 			   ar->wmi.svc_map)))
7102 		*rate_ctrl = WMI_PEER_TID_CONFIG_RATE_UPPER_CAP;
7103 	else
7104 		return -EOPNOTSUPP;
7105 
7106 	return 0;
7107 }
7108 
ath10k_mac_set_tid_config(struct ath10k * ar,struct ieee80211_sta * sta,struct ieee80211_vif * vif,u32 changed,struct wmi_per_peer_per_tid_cfg_arg * arg)7109 static int ath10k_mac_set_tid_config(struct ath10k *ar, struct ieee80211_sta *sta,
7110 				     struct ieee80211_vif *vif, u32 changed,
7111 				     struct wmi_per_peer_per_tid_cfg_arg *arg)
7112 {
7113 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7114 	struct ath10k_sta *arsta;
7115 	int ret;
7116 
7117 	if (sta) {
7118 		if (!sta->wme)
7119 			return -EOPNOTSUPP;
7120 
7121 		arsta = (struct ath10k_sta *)sta->drv_priv;
7122 
7123 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7124 			if ((arsta->retry_long[arg->tid] > 0 ||
7125 			     arsta->rate_code[arg->tid] > 0 ||
7126 			     arsta->ampdu[arg->tid] ==
7127 					WMI_TID_CONFIG_AGGR_CONTROL_ENABLE) &&
7128 			     arg->ack_policy == WMI_PEER_TID_CONFIG_NOACK) {
7129 				changed &= ~BIT(NL80211_TID_CONFIG_ATTR_NOACK);
7130 				arg->ack_policy = 0;
7131 				arg->aggr_control = 0;
7132 				arg->rate_ctrl = 0;
7133 				arg->rcode_flags = 0;
7134 			}
7135 		}
7136 
7137 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7138 			if (arsta->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK ||
7139 			    arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7140 				arg->aggr_control = 0;
7141 				changed &= ~BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
7142 			}
7143 		}
7144 
7145 		if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7146 		    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7147 			if (arsta->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK ||
7148 			    arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7149 				arg->rate_ctrl = 0;
7150 				arg->rcode_flags = 0;
7151 			}
7152 		}
7153 
7154 		ether_addr_copy(arg->peer_macaddr.addr, sta->addr);
7155 
7156 		ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, arg);
7157 		if (ret)
7158 			return ret;
7159 
7160 		/* Store the configured parameters in success case */
7161 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7162 			arsta->noack[arg->tid] = arg->ack_policy;
7163 			arg->ack_policy = 0;
7164 			arg->aggr_control = 0;
7165 			arg->rate_ctrl = 0;
7166 			arg->rcode_flags = 0;
7167 		}
7168 
7169 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7170 			arsta->retry_long[arg->tid] = arg->retry_count;
7171 			arg->retry_count = 0;
7172 		}
7173 
7174 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7175 			arsta->ampdu[arg->tid] = arg->aggr_control;
7176 			arg->aggr_control = 0;
7177 		}
7178 
7179 		if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7180 		    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7181 			arsta->rate_ctrl[arg->tid] = arg->rate_ctrl;
7182 			arg->rate_ctrl = 0;
7183 			arg->rcode_flags = 0;
7184 		}
7185 
7186 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7187 			arsta->rtscts[arg->tid] = arg->rtscts_ctrl;
7188 			arg->ext_tid_cfg_bitmap = 0;
7189 		}
7190 	} else {
7191 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7192 			if ((arvif->retry_long[arg->tid] ||
7193 			     arvif->rate_code[arg->tid] ||
7194 			     arvif->ampdu[arg->tid] ==
7195 					WMI_TID_CONFIG_AGGR_CONTROL_ENABLE) &&
7196 			     arg->ack_policy == WMI_PEER_TID_CONFIG_NOACK) {
7197 				changed &= ~BIT(NL80211_TID_CONFIG_ATTR_NOACK);
7198 			} else {
7199 				arvif->noack[arg->tid] = arg->ack_policy;
7200 				arvif->ampdu[arg->tid] = arg->aggr_control;
7201 				arvif->rate_ctrl[arg->tid] = arg->rate_ctrl;
7202 			}
7203 		}
7204 
7205 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7206 			if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK)
7207 				changed &= ~BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
7208 			else
7209 				arvif->retry_long[arg->tid] = arg->retry_count;
7210 		}
7211 
7212 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7213 			if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK)
7214 				changed &= ~BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
7215 			else
7216 				arvif->ampdu[arg->tid] = arg->aggr_control;
7217 		}
7218 
7219 		if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7220 		    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7221 			if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7222 				changed &= ~(BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7223 					     BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE));
7224 			} else {
7225 				arvif->rate_ctrl[arg->tid] = arg->rate_ctrl;
7226 				arvif->rate_code[arg->tid] = arg->rcode_flags;
7227 			}
7228 		}
7229 
7230 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7231 			arvif->rtscts[arg->tid] = arg->rtscts_ctrl;
7232 			arg->ext_tid_cfg_bitmap = 0;
7233 		}
7234 
7235 		if (changed)
7236 			arvif->tid_conf_changed[arg->tid] |= changed;
7237 	}
7238 
7239 	return 0;
7240 }
7241 
7242 static int
ath10k_mac_parse_tid_config(struct ath10k * ar,struct ieee80211_sta * sta,struct ieee80211_vif * vif,struct cfg80211_tid_cfg * tid_conf,struct wmi_per_peer_per_tid_cfg_arg * arg)7243 ath10k_mac_parse_tid_config(struct ath10k *ar,
7244 			    struct ieee80211_sta *sta,
7245 			    struct ieee80211_vif *vif,
7246 			    struct cfg80211_tid_cfg *tid_conf,
7247 			    struct wmi_per_peer_per_tid_cfg_arg *arg)
7248 {
7249 	u32 changed = tid_conf->mask;
7250 	int ret = 0, i = 0;
7251 
7252 	if (!changed)
7253 		return -EINVAL;
7254 
7255 	while (i < ATH10K_TID_MAX) {
7256 		if (!(tid_conf->tids & BIT(i))) {
7257 			i++;
7258 			continue;
7259 		}
7260 
7261 		arg->tid = i;
7262 
7263 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7264 			if (tid_conf->noack == NL80211_TID_CONFIG_ENABLE) {
7265 				arg->ack_policy = WMI_PEER_TID_CONFIG_NOACK;
7266 				arg->rate_ctrl =
7267 				WMI_TID_CONFIG_RATE_CONTROL_DEFAULT_LOWEST_RATE;
7268 				arg->aggr_control =
7269 					WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
7270 			} else {
7271 				arg->ack_policy =
7272 					WMI_PEER_TID_CONFIG_ACK;
7273 				arg->rate_ctrl =
7274 					WMI_TID_CONFIG_RATE_CONTROL_AUTO;
7275 				arg->aggr_control =
7276 					WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7277 			}
7278 		}
7279 
7280 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG))
7281 			arg->retry_count = tid_conf->retry_long;
7282 
7283 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7284 			if (tid_conf->noack == NL80211_TID_CONFIG_ENABLE)
7285 				arg->aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7286 			else
7287 				arg->aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
7288 		}
7289 
7290 		if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7291 		    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7292 			ret = ath10k_mac_tid_bitrate_config(ar, vif, sta,
7293 							    &arg->rcode_flags,
7294 							    &arg->rate_ctrl,
7295 							    tid_conf->txrate_type,
7296 							&tid_conf->txrate_mask);
7297 			if (ret) {
7298 				ath10k_warn(ar, "failed to configure bitrate mask %d\n",
7299 					    ret);
7300 				arg->rcode_flags = 0;
7301 				arg->rate_ctrl = 0;
7302 			}
7303 		}
7304 
7305 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7306 			if (tid_conf->rtscts)
7307 				arg->rtscts_ctrl = tid_conf->rtscts;
7308 
7309 			arg->ext_tid_cfg_bitmap = WMI_EXT_TID_RTS_CTS_CONFIG;
7310 		}
7311 
7312 		ret = ath10k_mac_set_tid_config(ar, sta, vif, changed, arg);
7313 		if (ret)
7314 			return ret;
7315 		i++;
7316 	}
7317 
7318 	return ret;
7319 }
7320 
ath10k_mac_reset_tid_config(struct ath10k * ar,struct ieee80211_sta * sta,struct ath10k_vif * arvif,u8 tids)7321 static int ath10k_mac_reset_tid_config(struct ath10k *ar,
7322 				       struct ieee80211_sta *sta,
7323 				       struct ath10k_vif *arvif,
7324 				       u8 tids)
7325 {
7326 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7327 	struct wmi_per_peer_per_tid_cfg_arg arg;
7328 	int ret = 0, i = 0;
7329 
7330 	arg.vdev_id = arvif->vdev_id;
7331 	while (i < ATH10K_TID_MAX) {
7332 		if (!(tids & BIT(i))) {
7333 			i++;
7334 			continue;
7335 		}
7336 
7337 		arg.tid = i;
7338 		arg.ack_policy = WMI_PEER_TID_CONFIG_ACK;
7339 		arg.retry_count = ATH10K_MAX_RETRY_COUNT;
7340 		arg.rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_AUTO;
7341 		arg.aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7342 		arg.rtscts_ctrl = WMI_TID_CONFIG_RTSCTS_CONTROL_ENABLE;
7343 		arg.ext_tid_cfg_bitmap = WMI_EXT_TID_RTS_CTS_CONFIG;
7344 
7345 		ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
7346 
7347 		ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
7348 		if (ret)
7349 			return ret;
7350 
7351 		if (!arvif->tids_rst) {
7352 			arsta->retry_long[i] = -1;
7353 			arsta->noack[i] = -1;
7354 			arsta->ampdu[i] = -1;
7355 			arsta->rate_code[i] = -1;
7356 			arsta->rate_ctrl[i] = 0;
7357 			arsta->rtscts[i] = -1;
7358 		} else {
7359 			arvif->retry_long[i] = 0;
7360 			arvif->noack[i] = 0;
7361 			arvif->ampdu[i] = 0;
7362 			arvif->rate_code[i] = 0;
7363 			arvif->rate_ctrl[i] = 0;
7364 			arvif->rtscts[i] = 0;
7365 		}
7366 
7367 		i++;
7368 	}
7369 
7370 	return ret;
7371 }
7372 
ath10k_sta_tid_cfg_wk(struct work_struct * wk)7373 static void ath10k_sta_tid_cfg_wk(struct work_struct *wk)
7374 {
7375 	struct wmi_per_peer_per_tid_cfg_arg arg = {};
7376 	struct ieee80211_sta *sta;
7377 	struct ath10k_sta *arsta;
7378 	struct ath10k_vif *arvif;
7379 	struct ath10k *ar;
7380 	bool config_apply;
7381 	int ret, i;
7382 	u32 changed;
7383 	u8 nss;
7384 
7385 	arsta = container_of(wk, struct ath10k_sta, tid_config_wk);
7386 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
7387 	arvif = arsta->arvif;
7388 	ar = arvif->ar;
7389 
7390 	mutex_lock(&ar->conf_mutex);
7391 
7392 	if (arvif->tids_rst) {
7393 		ret = ath10k_mac_reset_tid_config(ar, sta, arvif,
7394 						  arvif->tids_rst);
7395 		goto exit;
7396 	}
7397 
7398 	ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
7399 
7400 	for (i = 0; i < ATH10K_TID_MAX; i++) {
7401 		config_apply = false;
7402 		changed = arvif->tid_conf_changed[i];
7403 
7404 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7405 			if (arsta->noack[i] != -1) {
7406 				arg.ack_policy  = 0;
7407 			} else {
7408 				config_apply = true;
7409 				arg.ack_policy = arvif->noack[i];
7410 				arg.aggr_control = arvif->ampdu[i];
7411 				arg.rate_ctrl = arvif->rate_ctrl[i];
7412 			}
7413 		}
7414 
7415 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7416 			if (arsta->retry_long[i] != -1 ||
7417 			    arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7418 			    arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7419 				arg.retry_count = 0;
7420 			} else {
7421 				arg.retry_count = arvif->retry_long[i];
7422 				config_apply = true;
7423 			}
7424 		}
7425 
7426 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7427 			if (arsta->ampdu[i] != -1 ||
7428 			    arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7429 			    arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7430 				arg.aggr_control = 0;
7431 			} else {
7432 				arg.aggr_control = arvif->ampdu[i];
7433 				config_apply = true;
7434 			}
7435 		}
7436 
7437 		if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7438 		    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7439 			nss = ATH10K_HW_NSS(arvif->rate_code[i]);
7440 			ret = ath10k_mac_validate_rate_mask(ar, sta,
7441 							    arvif->rate_code[i],
7442 							    nss);
7443 			if (ret &&
7444 			    arvif->rate_ctrl[i] > WMI_TID_CONFIG_RATE_CONTROL_AUTO) {
7445 				arg.rate_ctrl = 0;
7446 				arg.rcode_flags = 0;
7447 			}
7448 
7449 			if (arsta->rate_ctrl[i] >
7450 			    WMI_TID_CONFIG_RATE_CONTROL_AUTO ||
7451 			    arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7452 			    arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7453 				arg.rate_ctrl = 0;
7454 				arg.rcode_flags = 0;
7455 			} else {
7456 				arg.rate_ctrl = arvif->rate_ctrl[i];
7457 				arg.rcode_flags = arvif->rate_code[i];
7458 				config_apply = true;
7459 			}
7460 		}
7461 
7462 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7463 			if (arsta->rtscts[i]) {
7464 				arg.rtscts_ctrl = 0;
7465 				arg.ext_tid_cfg_bitmap = 0;
7466 			} else {
7467 				arg.rtscts_ctrl = arvif->rtscts[i] - 1;
7468 				arg.ext_tid_cfg_bitmap =
7469 					WMI_EXT_TID_RTS_CTS_CONFIG;
7470 				config_apply = true;
7471 			}
7472 		}
7473 
7474 		arg.tid = i;
7475 
7476 		if (config_apply) {
7477 			ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
7478 			if (ret)
7479 				ath10k_warn(ar, "failed to set per tid config for sta %pM: %d\n",
7480 					    sta->addr, ret);
7481 		}
7482 
7483 		arg.ack_policy  = 0;
7484 		arg.retry_count  = 0;
7485 		arg.aggr_control  = 0;
7486 		arg.rate_ctrl = 0;
7487 		arg.rcode_flags = 0;
7488 	}
7489 
7490 exit:
7491 	mutex_unlock(&ar->conf_mutex);
7492 }
7493 
ath10k_mac_vif_stations_tid_conf(void * data,struct ieee80211_sta * sta)7494 static void ath10k_mac_vif_stations_tid_conf(void *data,
7495 					     struct ieee80211_sta *sta)
7496 {
7497 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7498 	struct ath10k_mac_iter_tid_conf_data *iter_data = data;
7499 	struct ieee80211_vif *sta_vif = arsta->arvif->vif;
7500 
7501 	if (sta_vif != iter_data->curr_vif || !sta->wme)
7502 		return;
7503 
7504 	ieee80211_queue_work(iter_data->ar->hw, &arsta->tid_config_wk);
7505 }
7506 
ath10k_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)7507 static int ath10k_sta_state(struct ieee80211_hw *hw,
7508 			    struct ieee80211_vif *vif,
7509 			    struct ieee80211_sta *sta,
7510 			    enum ieee80211_sta_state old_state,
7511 			    enum ieee80211_sta_state new_state)
7512 {
7513 	struct ath10k *ar = hw->priv;
7514 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7515 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7516 	struct ath10k_peer *peer;
7517 	int ret = 0;
7518 	int i;
7519 
7520 	if (old_state == IEEE80211_STA_NOTEXIST &&
7521 	    new_state == IEEE80211_STA_NONE) {
7522 		memset(arsta, 0, sizeof(*arsta));
7523 		arsta->arvif = arvif;
7524 		arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED;
7525 		INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
7526 		INIT_WORK(&arsta->tid_config_wk, ath10k_sta_tid_cfg_wk);
7527 
7528 		for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
7529 			ath10k_mac_txq_init(sta->txq[i]);
7530 	}
7531 
7532 	/* cancel must be done outside the mutex to avoid deadlock */
7533 	if ((old_state == IEEE80211_STA_NONE &&
7534 	     new_state == IEEE80211_STA_NOTEXIST)) {
7535 		cancel_work_sync(&arsta->update_wk);
7536 		cancel_work_sync(&arsta->tid_config_wk);
7537 	}
7538 
7539 	mutex_lock(&ar->conf_mutex);
7540 
7541 	if (old_state == IEEE80211_STA_NOTEXIST &&
7542 	    new_state == IEEE80211_STA_NONE) {
7543 		/*
7544 		 * New station addition.
7545 		 */
7546 		enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
7547 		u32 num_tdls_stations;
7548 
7549 		ath10k_dbg(ar, ATH10K_DBG_STA,
7550 			   "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
7551 			   arvif->vdev_id, sta->addr,
7552 			   ar->num_stations + 1, ar->max_num_stations,
7553 			   ar->num_peers + 1, ar->max_num_peers);
7554 
7555 		num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
7556 
7557 		if (sta->tdls) {
7558 			if (num_tdls_stations >= ar->max_num_tdls_vdevs) {
7559 				ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
7560 					    arvif->vdev_id,
7561 					    ar->max_num_tdls_vdevs);
7562 				ret = -ELNRNG;
7563 				goto exit;
7564 			}
7565 			peer_type = WMI_PEER_TYPE_TDLS;
7566 		}
7567 
7568 		ret = ath10k_mac_inc_num_stations(arvif, sta);
7569 		if (ret) {
7570 			ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
7571 				    ar->max_num_stations);
7572 			goto exit;
7573 		}
7574 
7575 		if (ath10k_debug_is_extd_tx_stats_enabled(ar)) {
7576 			arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats),
7577 						  GFP_KERNEL);
7578 			if (!arsta->tx_stats) {
7579 				ath10k_mac_dec_num_stations(arvif, sta);
7580 				ret = -ENOMEM;
7581 				goto exit;
7582 			}
7583 		}
7584 
7585 		ret = ath10k_peer_create(ar, vif, sta, arvif->vdev_id,
7586 					 sta->addr, peer_type);
7587 		if (ret) {
7588 			ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
7589 				    sta->addr, arvif->vdev_id, ret);
7590 			ath10k_mac_dec_num_stations(arvif, sta);
7591 			kfree(arsta->tx_stats);
7592 			goto exit;
7593 		}
7594 
7595 		spin_lock_bh(&ar->data_lock);
7596 
7597 		peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
7598 		if (!peer) {
7599 			ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
7600 				    vif->addr, arvif->vdev_id);
7601 			spin_unlock_bh(&ar->data_lock);
7602 			ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7603 			ath10k_mac_dec_num_stations(arvif, sta);
7604 			kfree(arsta->tx_stats);
7605 			ret = -ENOENT;
7606 			goto exit;
7607 		}
7608 
7609 		arsta->peer_id = find_first_bit(peer->peer_ids,
7610 						ATH10K_MAX_NUM_PEER_IDS);
7611 
7612 		spin_unlock_bh(&ar->data_lock);
7613 
7614 		if (!sta->tdls)
7615 			goto exit;
7616 
7617 		ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7618 						      WMI_TDLS_ENABLE_ACTIVE);
7619 		if (ret) {
7620 			ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
7621 				    arvif->vdev_id, ret);
7622 			ath10k_peer_delete(ar, arvif->vdev_id,
7623 					   sta->addr);
7624 			ath10k_mac_dec_num_stations(arvif, sta);
7625 			kfree(arsta->tx_stats);
7626 			goto exit;
7627 		}
7628 
7629 		ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
7630 						  WMI_TDLS_PEER_STATE_PEERING);
7631 		if (ret) {
7632 			ath10k_warn(ar,
7633 				    "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
7634 				    sta->addr, arvif->vdev_id, ret);
7635 			ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7636 			ath10k_mac_dec_num_stations(arvif, sta);
7637 			kfree(arsta->tx_stats);
7638 
7639 			if (num_tdls_stations != 0)
7640 				goto exit;
7641 			ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7642 							WMI_TDLS_DISABLE);
7643 		}
7644 	} else if ((old_state == IEEE80211_STA_NONE &&
7645 		    new_state == IEEE80211_STA_NOTEXIST)) {
7646 		/*
7647 		 * Existing station deletion.
7648 		 */
7649 		ath10k_dbg(ar, ATH10K_DBG_STA,
7650 			   "mac vdev %d peer delete %pM sta %p (sta gone)\n",
7651 			   arvif->vdev_id, sta->addr, sta);
7652 
7653 		if (sta->tdls) {
7654 			ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id,
7655 							  sta,
7656 							  WMI_TDLS_PEER_STATE_TEARDOWN);
7657 			if (ret)
7658 				ath10k_warn(ar, "failed to update tdls peer state for %pM state %d: %i\n",
7659 					    sta->addr,
7660 					    WMI_TDLS_PEER_STATE_TEARDOWN, ret);
7661 		}
7662 
7663 		ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7664 		if (ret)
7665 			ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
7666 				    sta->addr, arvif->vdev_id, ret);
7667 
7668 		ath10k_mac_dec_num_stations(arvif, sta);
7669 
7670 		spin_lock_bh(&ar->data_lock);
7671 		for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
7672 			peer = ar->peer_map[i];
7673 			if (!peer)
7674 				continue;
7675 
7676 			if (peer->sta == sta) {
7677 				ath10k_warn(ar, "found sta peer %pM (ptr %p id %d) entry on vdev %i after it was supposedly removed\n",
7678 					    sta->addr, peer, i, arvif->vdev_id);
7679 				peer->sta = NULL;
7680 
7681 				/* Clean up the peer object as well since we
7682 				 * must have failed to do this above.
7683 				 */
7684 				ath10k_peer_map_cleanup(ar, peer);
7685 			}
7686 		}
7687 		spin_unlock_bh(&ar->data_lock);
7688 
7689 		if (ath10k_debug_is_extd_tx_stats_enabled(ar)) {
7690 			kfree(arsta->tx_stats);
7691 			arsta->tx_stats = NULL;
7692 		}
7693 
7694 		for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
7695 			ath10k_mac_txq_unref(ar, sta->txq[i]);
7696 
7697 		if (!sta->tdls)
7698 			goto exit;
7699 
7700 		if (ath10k_mac_tdls_vif_stations_count(hw, vif))
7701 			goto exit;
7702 
7703 		/* This was the last tdls peer in current vif */
7704 		ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7705 						      WMI_TDLS_DISABLE);
7706 		if (ret) {
7707 			ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
7708 				    arvif->vdev_id, ret);
7709 		}
7710 	} else if (old_state == IEEE80211_STA_AUTH &&
7711 		   new_state == IEEE80211_STA_ASSOC &&
7712 		   (vif->type == NL80211_IFTYPE_AP ||
7713 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7714 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7715 		/*
7716 		 * New association.
7717 		 */
7718 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac sta %pM associated\n",
7719 			   sta->addr);
7720 
7721 		ret = ath10k_station_assoc(ar, vif, sta, false);
7722 		if (ret)
7723 			ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
7724 				    sta->addr, arvif->vdev_id, ret);
7725 	} else if (old_state == IEEE80211_STA_ASSOC &&
7726 		   new_state == IEEE80211_STA_AUTHORIZED &&
7727 		   sta->tdls) {
7728 		/*
7729 		 * Tdls station authorized.
7730 		 */
7731 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac tdls sta %pM authorized\n",
7732 			   sta->addr);
7733 
7734 		ret = ath10k_station_assoc(ar, vif, sta, false);
7735 		if (ret) {
7736 			ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
7737 				    sta->addr, arvif->vdev_id, ret);
7738 			goto exit;
7739 		}
7740 
7741 		ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
7742 						  WMI_TDLS_PEER_STATE_CONNECTED);
7743 		if (ret)
7744 			ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
7745 				    sta->addr, arvif->vdev_id, ret);
7746 	} else if (old_state == IEEE80211_STA_ASSOC &&
7747 		    new_state == IEEE80211_STA_AUTH &&
7748 		    (vif->type == NL80211_IFTYPE_AP ||
7749 		     vif->type == NL80211_IFTYPE_MESH_POINT ||
7750 		     vif->type == NL80211_IFTYPE_ADHOC)) {
7751 		/*
7752 		 * Disassociation.
7753 		 */
7754 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac sta %pM disassociated\n",
7755 			   sta->addr);
7756 
7757 		ret = ath10k_station_disassoc(ar, vif, sta);
7758 		if (ret)
7759 			ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
7760 				    sta->addr, arvif->vdev_id, ret);
7761 	}
7762 exit:
7763 	mutex_unlock(&ar->conf_mutex);
7764 	return ret;
7765 }
7766 
ath10k_conf_tx_uapsd(struct ath10k * ar,struct ieee80211_vif * vif,u16 ac,bool enable)7767 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
7768 				u16 ac, bool enable)
7769 {
7770 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7771 	struct wmi_sta_uapsd_auto_trig_arg arg = {};
7772 	u32 prio = 0, acc = 0;
7773 	u32 value = 0;
7774 	int ret = 0;
7775 
7776 	lockdep_assert_held(&ar->conf_mutex);
7777 
7778 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
7779 		return 0;
7780 
7781 	switch (ac) {
7782 	case IEEE80211_AC_VO:
7783 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
7784 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
7785 		prio = 7;
7786 		acc = 3;
7787 		break;
7788 	case IEEE80211_AC_VI:
7789 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
7790 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
7791 		prio = 5;
7792 		acc = 2;
7793 		break;
7794 	case IEEE80211_AC_BE:
7795 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
7796 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
7797 		prio = 2;
7798 		acc = 1;
7799 		break;
7800 	case IEEE80211_AC_BK:
7801 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
7802 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
7803 		prio = 0;
7804 		acc = 0;
7805 		break;
7806 	}
7807 
7808 	if (enable)
7809 		arvif->u.sta.uapsd |= value;
7810 	else
7811 		arvif->u.sta.uapsd &= ~value;
7812 
7813 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7814 					  WMI_STA_PS_PARAM_UAPSD,
7815 					  arvif->u.sta.uapsd);
7816 	if (ret) {
7817 		ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
7818 		goto exit;
7819 	}
7820 
7821 	if (arvif->u.sta.uapsd)
7822 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
7823 	else
7824 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
7825 
7826 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7827 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
7828 					  value);
7829 	if (ret)
7830 		ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
7831 
7832 	ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
7833 	if (ret) {
7834 		ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
7835 			    arvif->vdev_id, ret);
7836 		return ret;
7837 	}
7838 
7839 	ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
7840 	if (ret) {
7841 		ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
7842 			    arvif->vdev_id, ret);
7843 		return ret;
7844 	}
7845 
7846 	if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
7847 	    test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
7848 		/* Only userspace can make an educated decision when to send
7849 		 * trigger frame. The following effectively disables u-UAPSD
7850 		 * autotrigger in firmware (which is enabled by default
7851 		 * provided the autotrigger service is available).
7852 		 */
7853 
7854 		arg.wmm_ac = acc;
7855 		arg.user_priority = prio;
7856 		arg.service_interval = 0;
7857 		arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
7858 		arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
7859 
7860 		ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
7861 						arvif->bssid, &arg, 1);
7862 		if (ret) {
7863 			ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
7864 				    ret);
7865 			return ret;
7866 		}
7867 	}
7868 
7869 exit:
7870 	return ret;
7871 }
7872 
ath10k_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 ac,const struct ieee80211_tx_queue_params * params)7873 static int ath10k_conf_tx(struct ieee80211_hw *hw,
7874 			  struct ieee80211_vif *vif,
7875 			  unsigned int link_id, u16 ac,
7876 			  const struct ieee80211_tx_queue_params *params)
7877 {
7878 	struct ath10k *ar = hw->priv;
7879 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7880 	struct wmi_wmm_params_arg *p = NULL;
7881 	int ret;
7882 
7883 	mutex_lock(&ar->conf_mutex);
7884 
7885 	switch (ac) {
7886 	case IEEE80211_AC_VO:
7887 		p = &arvif->wmm_params.ac_vo;
7888 		break;
7889 	case IEEE80211_AC_VI:
7890 		p = &arvif->wmm_params.ac_vi;
7891 		break;
7892 	case IEEE80211_AC_BE:
7893 		p = &arvif->wmm_params.ac_be;
7894 		break;
7895 	case IEEE80211_AC_BK:
7896 		p = &arvif->wmm_params.ac_bk;
7897 		break;
7898 	}
7899 
7900 	if (WARN_ON(!p)) {
7901 		ret = -EINVAL;
7902 		goto exit;
7903 	}
7904 
7905 	p->cwmin = params->cw_min;
7906 	p->cwmax = params->cw_max;
7907 	p->aifs = params->aifs;
7908 
7909 	/*
7910 	 * The channel time duration programmed in the HW is in absolute
7911 	 * microseconds, while mac80211 gives the txop in units of
7912 	 * 32 microseconds.
7913 	 */
7914 	p->txop = params->txop * 32;
7915 
7916 	if (ar->wmi.ops->gen_vdev_wmm_conf) {
7917 		ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
7918 					       &arvif->wmm_params);
7919 		if (ret) {
7920 			ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
7921 				    arvif->vdev_id, ret);
7922 			goto exit;
7923 		}
7924 	} else {
7925 		/* This won't work well with multi-interface cases but it's
7926 		 * better than nothing.
7927 		 */
7928 		ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
7929 		if (ret) {
7930 			ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
7931 			goto exit;
7932 		}
7933 	}
7934 
7935 	ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
7936 	if (ret)
7937 		ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
7938 
7939 exit:
7940 	mutex_unlock(&ar->conf_mutex);
7941 	return ret;
7942 }
7943 
ath10k_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * chan,int duration,enum ieee80211_roc_type type)7944 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
7945 				    struct ieee80211_vif *vif,
7946 				    struct ieee80211_channel *chan,
7947 				    int duration,
7948 				    enum ieee80211_roc_type type)
7949 {
7950 	struct ath10k *ar = hw->priv;
7951 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7952 	struct wmi_start_scan_arg *arg = NULL;
7953 	int ret = 0;
7954 	u32 scan_time_msec;
7955 
7956 	mutex_lock(&ar->conf_mutex);
7957 
7958 	if (ath10k_mac_tdls_vif_stations_count(hw, vif) > 0) {
7959 		ret = -EBUSY;
7960 		goto exit;
7961 	}
7962 
7963 	spin_lock_bh(&ar->data_lock);
7964 	switch (ar->scan.state) {
7965 	case ATH10K_SCAN_IDLE:
7966 		reinit_completion(&ar->scan.started);
7967 		reinit_completion(&ar->scan.completed);
7968 		reinit_completion(&ar->scan.on_channel);
7969 		ar->scan.state = ATH10K_SCAN_STARTING;
7970 		ar->scan.is_roc = true;
7971 		ar->scan.vdev_id = arvif->vdev_id;
7972 		ar->scan.roc_freq = chan->center_freq;
7973 		ar->scan.roc_notify = true;
7974 		ret = 0;
7975 		break;
7976 	case ATH10K_SCAN_STARTING:
7977 	case ATH10K_SCAN_RUNNING:
7978 	case ATH10K_SCAN_ABORTING:
7979 		ret = -EBUSY;
7980 		break;
7981 	}
7982 	spin_unlock_bh(&ar->data_lock);
7983 
7984 	if (ret)
7985 		goto exit;
7986 
7987 	scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
7988 
7989 	arg = kzalloc(sizeof(*arg), GFP_KERNEL);
7990 	if (!arg) {
7991 		ret = -ENOMEM;
7992 		goto exit;
7993 	}
7994 
7995 	ath10k_wmi_start_scan_init(ar, arg);
7996 	arg->vdev_id = arvif->vdev_id;
7997 	arg->scan_id = ATH10K_SCAN_ID;
7998 	arg->n_channels = 1;
7999 	arg->channels[0] = chan->center_freq;
8000 	arg->dwell_time_active = scan_time_msec;
8001 	arg->dwell_time_passive = scan_time_msec;
8002 	arg->max_scan_time = scan_time_msec;
8003 	arg->scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
8004 	arg->scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
8005 	arg->burst_duration_ms = duration;
8006 
8007 	ret = ath10k_start_scan(ar, arg);
8008 	if (ret) {
8009 		ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
8010 		spin_lock_bh(&ar->data_lock);
8011 		ar->scan.state = ATH10K_SCAN_IDLE;
8012 		spin_unlock_bh(&ar->data_lock);
8013 		goto exit;
8014 	}
8015 
8016 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
8017 	if (ret == 0) {
8018 		ath10k_warn(ar, "failed to switch to channel for roc scan\n");
8019 
8020 		ret = ath10k_scan_stop(ar);
8021 		if (ret)
8022 			ath10k_warn(ar, "failed to stop scan: %d\n", ret);
8023 
8024 		ret = -ETIMEDOUT;
8025 		goto exit;
8026 	}
8027 
8028 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
8029 				     msecs_to_jiffies(duration));
8030 
8031 	ret = 0;
8032 exit:
8033 	kfree(arg);
8034 
8035 	mutex_unlock(&ar->conf_mutex);
8036 	return ret;
8037 }
8038 
ath10k_cancel_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif)8039 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw,
8040 					   struct ieee80211_vif *vif)
8041 {
8042 	struct ath10k *ar = hw->priv;
8043 
8044 	mutex_lock(&ar->conf_mutex);
8045 
8046 	spin_lock_bh(&ar->data_lock);
8047 	ar->scan.roc_notify = false;
8048 	spin_unlock_bh(&ar->data_lock);
8049 
8050 	ath10k_scan_abort(ar);
8051 
8052 	mutex_unlock(&ar->conf_mutex);
8053 
8054 	cancel_delayed_work_sync(&ar->scan.timeout);
8055 
8056 	return 0;
8057 }
8058 
8059 /*
8060  * Both RTS and Fragmentation threshold are interface-specific
8061  * in ath10k, but device-specific in mac80211.
8062  */
8063 
ath10k_set_rts_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)8064 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
8065 				    u32 value)
8066 {
8067 	struct ath10k *ar = hw->priv;
8068 	struct ath10k_vif *arvif;
8069 	int ret = 0;
8070 
8071 	mutex_lock(&ar->conf_mutex);
8072 	list_for_each_entry(arvif, &ar->arvifs, list) {
8073 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
8074 			   arvif->vdev_id, value);
8075 
8076 		ret = ath10k_mac_set_rts(arvif, value);
8077 		if (ret) {
8078 			ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
8079 				    arvif->vdev_id, ret);
8080 			break;
8081 		}
8082 	}
8083 	mutex_unlock(&ar->conf_mutex);
8084 
8085 	return ret;
8086 }
8087 
ath10k_mac_op_set_frag_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)8088 static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw,
8089 					    int radio_idx, u32 value)
8090 {
8091 	/* Even though there's a WMI enum for fragmentation threshold no known
8092 	 * firmware actually implements it. Moreover it is not possible to rely
8093 	 * frame fragmentation to mac80211 because firmware clears the "more
8094 	 * fragments" bit in frame control making it impossible for remote
8095 	 * devices to reassemble frames.
8096 	 *
8097 	 * Hence implement a dummy callback just to say fragmentation isn't
8098 	 * supported. This effectively prevents mac80211 from doing frame
8099 	 * fragmentation in software.
8100 	 */
8101 	return -EOPNOTSUPP;
8102 }
8103 
ath10k_mac_wait_tx_complete(struct ath10k * ar)8104 void ath10k_mac_wait_tx_complete(struct ath10k *ar)
8105 {
8106 	bool skip;
8107 	long time_left;
8108 
8109 	/* mac80211 doesn't care if we really xmit queued frames or not
8110 	 * we'll collect those frames either way if we stop/delete vdevs
8111 	 */
8112 
8113 	if (ar->state == ATH10K_STATE_WEDGED)
8114 		return;
8115 
8116 	time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
8117 			bool empty;
8118 
8119 			spin_lock_bh(&ar->htt.tx_lock);
8120 			empty = (ar->htt.num_pending_tx == 0);
8121 			spin_unlock_bh(&ar->htt.tx_lock);
8122 
8123 			skip = (ar->state == ATH10K_STATE_WEDGED) ||
8124 			       test_bit(ATH10K_FLAG_CRASH_FLUSH,
8125 					&ar->dev_flags);
8126 
8127 			(empty || skip);
8128 		}), ATH10K_FLUSH_TIMEOUT_HZ);
8129 
8130 	if (time_left == 0 || skip)
8131 		ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
8132 			    skip, ar->state, time_left);
8133 }
8134 
ath10k_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)8135 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
8136 			 u32 queues, bool drop)
8137 {
8138 	struct ath10k *ar = hw->priv;
8139 	struct ath10k_vif *arvif;
8140 	u32 bitmap;
8141 
8142 	if (drop) {
8143 		if (vif && vif->type == NL80211_IFTYPE_STATION) {
8144 			bitmap = ~(1 << WMI_MGMT_TID);
8145 			list_for_each_entry(arvif, &ar->arvifs, list) {
8146 				if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
8147 					ath10k_wmi_peer_flush(ar, arvif->vdev_id,
8148 							      arvif->bssid, bitmap);
8149 			}
8150 			ath10k_htt_flush_tx(&ar->htt);
8151 		}
8152 		return;
8153 	}
8154 
8155 	mutex_lock(&ar->conf_mutex);
8156 	ath10k_mac_wait_tx_complete(ar);
8157 	mutex_unlock(&ar->conf_mutex);
8158 }
8159 
8160 /* TODO: Implement this function properly
8161  * For now it is needed to reply to Probe Requests in IBSS mode.
8162  * Probably we need this information from FW.
8163  */
ath10k_tx_last_beacon(struct ieee80211_hw * hw)8164 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
8165 {
8166 	return 1;
8167 }
8168 
ath10k_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)8169 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
8170 				     enum ieee80211_reconfig_type reconfig_type)
8171 {
8172 	struct ath10k *ar = hw->priv;
8173 	struct ath10k_vif *arvif;
8174 
8175 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
8176 		return;
8177 
8178 	mutex_lock(&ar->conf_mutex);
8179 
8180 	/* If device failed to restart it will be in a different state, e.g.
8181 	 * ATH10K_STATE_WEDGED
8182 	 */
8183 	if (ar->state == ATH10K_STATE_RESTARTED) {
8184 		ath10k_info(ar, "device successfully recovered\n");
8185 		ar->state = ATH10K_STATE_ON;
8186 		ieee80211_wake_queues(ar->hw);
8187 
8188 		/* Clear recovery state. */
8189 		complete(&ar->driver_recovery);
8190 		atomic_set(&ar->fail_cont_count, 0);
8191 		atomic_set(&ar->pending_recovery, 0);
8192 
8193 		if (ar->hw_params.hw_restart_disconnect) {
8194 			list_for_each_entry(arvif, &ar->arvifs, list) {
8195 				if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_STA)
8196 					ieee80211_hw_restart_disconnect(arvif->vif);
8197 				}
8198 		}
8199 	}
8200 
8201 	mutex_unlock(&ar->conf_mutex);
8202 }
8203 
8204 static void
ath10k_mac_update_bss_chan_survey(struct ath10k * ar,struct ieee80211_channel * channel)8205 ath10k_mac_update_bss_chan_survey(struct ath10k *ar,
8206 				  struct ieee80211_channel *channel)
8207 {
8208 	int ret;
8209 	enum wmi_bss_survey_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
8210 
8211 	lockdep_assert_held(&ar->conf_mutex);
8212 
8213 	if (!test_bit(WMI_SERVICE_BSS_CHANNEL_INFO_64, ar->wmi.svc_map) ||
8214 	    (ar->rx_channel != channel))
8215 		return;
8216 
8217 	if (ar->scan.state != ATH10K_SCAN_IDLE) {
8218 		ath10k_dbg(ar, ATH10K_DBG_MAC, "ignoring bss chan info request while scanning..\n");
8219 		return;
8220 	}
8221 
8222 	reinit_completion(&ar->bss_survey_done);
8223 
8224 	ret = ath10k_wmi_pdev_bss_chan_info_request(ar, type);
8225 	if (ret) {
8226 		ath10k_warn(ar, "failed to send pdev bss chan info request\n");
8227 		return;
8228 	}
8229 
8230 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
8231 	if (!ret) {
8232 		ath10k_warn(ar, "bss channel survey timed out\n");
8233 		return;
8234 	}
8235 }
8236 
ath10k_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)8237 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
8238 			     struct survey_info *survey)
8239 {
8240 	struct ath10k *ar = hw->priv;
8241 	struct ieee80211_supported_band *sband;
8242 	struct survey_info *ar_survey = &ar->survey[idx];
8243 	int ret = 0;
8244 
8245 	mutex_lock(&ar->conf_mutex);
8246 
8247 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
8248 	if (sband && idx >= sband->n_channels) {
8249 		idx -= sband->n_channels;
8250 		sband = NULL;
8251 	}
8252 
8253 	if (!sband)
8254 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
8255 
8256 	if (!sband || idx >= sband->n_channels) {
8257 		ret = -ENOENT;
8258 		goto exit;
8259 	}
8260 
8261 	ath10k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
8262 
8263 	spin_lock_bh(&ar->data_lock);
8264 	memcpy(survey, ar_survey, sizeof(*survey));
8265 	spin_unlock_bh(&ar->data_lock);
8266 
8267 	survey->channel = &sband->channels[idx];
8268 
8269 	if (ar->rx_channel == survey->channel)
8270 		survey->filled |= SURVEY_INFO_IN_USE;
8271 
8272 exit:
8273 	mutex_unlock(&ar->conf_mutex);
8274 	return ret;
8275 }
8276 
8277 static bool
ath10k_mac_bitrate_mask_get_single_nss(struct ath10k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask,int * nss)8278 ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
8279 				       enum nl80211_band band,
8280 				       const struct cfg80211_bitrate_mask *mask,
8281 				       int *nss)
8282 {
8283 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
8284 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
8285 	u8 ht_nss_mask = 0;
8286 	u8 vht_nss_mask = 0;
8287 	int i;
8288 
8289 	if (mask->control[band].legacy)
8290 		return false;
8291 
8292 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
8293 		if (mask->control[band].ht_mcs[i] == 0)
8294 			continue;
8295 		else if (mask->control[band].ht_mcs[i] ==
8296 			 sband->ht_cap.mcs.rx_mask[i])
8297 			ht_nss_mask |= BIT(i);
8298 		else
8299 			return false;
8300 	}
8301 
8302 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
8303 		if (mask->control[band].vht_mcs[i] == 0)
8304 			continue;
8305 		else if (mask->control[band].vht_mcs[i] ==
8306 			 ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
8307 			vht_nss_mask |= BIT(i);
8308 		else
8309 			return false;
8310 	}
8311 
8312 	if (ht_nss_mask != vht_nss_mask)
8313 		return false;
8314 
8315 	if (ht_nss_mask == 0)
8316 		return false;
8317 
8318 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
8319 		return false;
8320 
8321 	*nss = fls(ht_nss_mask);
8322 
8323 	return true;
8324 }
8325 
ath10k_mac_set_fixed_rate_params(struct ath10k_vif * arvif,u8 rate,u8 nss,u8 sgi,u8 ldpc)8326 static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
8327 					    u8 rate, u8 nss, u8 sgi, u8 ldpc)
8328 {
8329 	struct ath10k *ar = arvif->ar;
8330 	u32 vdev_param;
8331 	int ret;
8332 
8333 	lockdep_assert_held(&ar->conf_mutex);
8334 
8335 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02x nss %u sgi %u\n",
8336 		   arvif->vdev_id, rate, nss, sgi);
8337 
8338 	vdev_param = ar->wmi.vdev_param->fixed_rate;
8339 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
8340 	if (ret) {
8341 		ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
8342 			    rate, ret);
8343 		return ret;
8344 	}
8345 
8346 	vdev_param = ar->wmi.vdev_param->nss;
8347 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
8348 	if (ret) {
8349 		ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
8350 		return ret;
8351 	}
8352 
8353 	vdev_param = ar->wmi.vdev_param->sgi;
8354 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
8355 	if (ret) {
8356 		ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
8357 		return ret;
8358 	}
8359 
8360 	vdev_param = ar->wmi.vdev_param->ldpc;
8361 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
8362 	if (ret) {
8363 		ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
8364 		return ret;
8365 	}
8366 
8367 	return 0;
8368 }
8369 
8370 static bool
ath10k_mac_can_set_bitrate_mask(struct ath10k * ar,enum nl80211_band band,const struct cfg80211_bitrate_mask * mask,bool allow_pfr)8371 ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
8372 				enum nl80211_band band,
8373 				const struct cfg80211_bitrate_mask *mask,
8374 				bool allow_pfr)
8375 {
8376 	int i;
8377 	u16 vht_mcs;
8378 
8379 	/* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
8380 	 * to express all VHT MCS rate masks. Effectively only the following
8381 	 * ranges can be used: none, 0-7, 0-8 and 0-9.
8382 	 */
8383 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
8384 		vht_mcs = mask->control[band].vht_mcs[i];
8385 
8386 		switch (vht_mcs) {
8387 		case 0:
8388 		case BIT(8) - 1:
8389 		case BIT(9) - 1:
8390 		case BIT(10) - 1:
8391 			break;
8392 		default:
8393 			if (!allow_pfr)
8394 				ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
8395 			return false;
8396 		}
8397 	}
8398 
8399 	return true;
8400 }
8401 
ath10k_mac_set_vht_bitrate_mask_fixup(struct ath10k * ar,struct ath10k_vif * arvif,struct ieee80211_sta * sta)8402 static bool ath10k_mac_set_vht_bitrate_mask_fixup(struct ath10k *ar,
8403 						  struct ath10k_vif *arvif,
8404 						  struct ieee80211_sta *sta)
8405 {
8406 	int err;
8407 	u8 rate = arvif->vht_pfr;
8408 
8409 	/* skip non vht and multiple rate peers */
8410 	if (!sta->deflink.vht_cap.vht_supported || arvif->vht_num_rates != 1)
8411 		return false;
8412 
8413 	err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
8414 					WMI_PEER_PARAM_FIXED_RATE, rate);
8415 	if (err)
8416 		ath10k_warn(ar, "failed to enable STA %pM peer fixed rate: %d\n",
8417 			    sta->addr, err);
8418 
8419 	return true;
8420 }
8421 
ath10k_mac_set_bitrate_mask_iter(void * data,struct ieee80211_sta * sta)8422 static void ath10k_mac_set_bitrate_mask_iter(void *data,
8423 					     struct ieee80211_sta *sta)
8424 {
8425 	struct ath10k_vif *arvif = data;
8426 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8427 	struct ath10k *ar = arvif->ar;
8428 
8429 	if (arsta->arvif != arvif)
8430 		return;
8431 
8432 	if (ath10k_mac_set_vht_bitrate_mask_fixup(ar, arvif, sta))
8433 		return;
8434 
8435 	spin_lock_bh(&ar->data_lock);
8436 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
8437 	spin_unlock_bh(&ar->data_lock);
8438 
8439 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
8440 }
8441 
ath10k_mac_clr_bitrate_mask_iter(void * data,struct ieee80211_sta * sta)8442 static void ath10k_mac_clr_bitrate_mask_iter(void *data,
8443 					     struct ieee80211_sta *sta)
8444 {
8445 	struct ath10k_vif *arvif = data;
8446 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8447 	struct ath10k *ar = arvif->ar;
8448 	int err;
8449 
8450 	/* clear vht peers only */
8451 	if (arsta->arvif != arvif || !sta->deflink.vht_cap.vht_supported)
8452 		return;
8453 
8454 	err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
8455 					WMI_PEER_PARAM_FIXED_RATE,
8456 					WMI_FIXED_RATE_NONE);
8457 	if (err)
8458 		ath10k_warn(ar, "failed to clear STA %pM peer fixed rate: %d\n",
8459 			    sta->addr, err);
8460 }
8461 
ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)8462 static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
8463 					  struct ieee80211_vif *vif,
8464 					  const struct cfg80211_bitrate_mask *mask)
8465 {
8466 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8467 	struct cfg80211_chan_def def;
8468 	struct ath10k *ar = arvif->ar;
8469 	enum nl80211_band band;
8470 	const u8 *ht_mcs_mask;
8471 	const u16 *vht_mcs_mask;
8472 	u8 rate;
8473 	u8 nss;
8474 	u8 sgi;
8475 	u8 ldpc;
8476 	int single_nss;
8477 	int ret;
8478 	int vht_num_rates, allow_pfr;
8479 	u8 vht_pfr;
8480 	bool update_bitrate_mask = true;
8481 
8482 	if (ath10k_mac_vif_chan(vif, &def))
8483 		return -EPERM;
8484 
8485 	band = def.chan->band;
8486 	ht_mcs_mask = mask->control[band].ht_mcs;
8487 	vht_mcs_mask = mask->control[band].vht_mcs;
8488 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
8489 
8490 	sgi = mask->control[band].gi;
8491 	if (sgi == NL80211_TXRATE_FORCE_LGI)
8492 		return -EINVAL;
8493 
8494 	allow_pfr = test_bit(ATH10K_FW_FEATURE_PEER_FIXED_RATE,
8495 			     ar->normal_mode_fw.fw_file.fw_features);
8496 	if (allow_pfr) {
8497 		mutex_lock(&ar->conf_mutex);
8498 		ieee80211_iterate_stations_atomic(ar->hw,
8499 						  ath10k_mac_clr_bitrate_mask_iter,
8500 						  arvif);
8501 		mutex_unlock(&ar->conf_mutex);
8502 	}
8503 
8504 	if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask,
8505 						    &vht_num_rates)) {
8506 		ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
8507 							      &rate, &nss,
8508 							      false);
8509 		if (ret) {
8510 			ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
8511 				    arvif->vdev_id, ret);
8512 			return ret;
8513 		}
8514 	} else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
8515 							  &single_nss)) {
8516 		rate = WMI_FIXED_RATE_NONE;
8517 		nss = single_nss;
8518 	} else {
8519 		rate = WMI_FIXED_RATE_NONE;
8520 		nss = min(ar->num_rf_chains,
8521 			  max(ath10k_mac_max_ht_nss(ht_mcs_mask),
8522 			      ath10k_mac_max_vht_nss(vht_mcs_mask)));
8523 
8524 #if defined(__FreeBSD__)
8525 		vht_pfr = 0;
8526 #endif
8527 		if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask,
8528 						     allow_pfr)) {
8529 			u8 vht_nss;
8530 
8531 			if (!allow_pfr || vht_num_rates != 1)
8532 				return -EINVAL;
8533 
8534 			/* Reach here, firmware supports peer fixed rate and has
8535 			 * single vht rate, and don't update vif birate_mask, as
8536 			 * the rate only for specific peer.
8537 			 */
8538 			ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
8539 								&vht_pfr,
8540 								&vht_nss,
8541 								true);
8542 			update_bitrate_mask = false;
8543 #if defined(__linux__)
8544 		} else {
8545 			vht_pfr = 0;
8546 #endif
8547 		}
8548 
8549 		mutex_lock(&ar->conf_mutex);
8550 
8551 		if (update_bitrate_mask)
8552 			arvif->bitrate_mask = *mask;
8553 		arvif->vht_num_rates = vht_num_rates;
8554 		arvif->vht_pfr = vht_pfr;
8555 		ieee80211_iterate_stations_atomic(ar->hw,
8556 						  ath10k_mac_set_bitrate_mask_iter,
8557 						  arvif);
8558 
8559 		mutex_unlock(&ar->conf_mutex);
8560 	}
8561 
8562 	mutex_lock(&ar->conf_mutex);
8563 
8564 	ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
8565 	if (ret) {
8566 		ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
8567 			    arvif->vdev_id, ret);
8568 		goto exit;
8569 	}
8570 
8571 exit:
8572 	mutex_unlock(&ar->conf_mutex);
8573 
8574 	return ret;
8575 }
8576 
ath10k_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,u32 changed)8577 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
8578 				 struct ieee80211_vif *vif,
8579 				 struct ieee80211_link_sta *link_sta,
8580 				 u32 changed)
8581 {
8582 	struct ieee80211_sta *sta = link_sta->sta;
8583 	struct ath10k *ar = hw->priv;
8584 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8585 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8586 	struct ath10k_peer *peer;
8587 	u32 bw, smps;
8588 
8589 	spin_lock_bh(&ar->data_lock);
8590 
8591 	peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
8592 	if (!peer) {
8593 		spin_unlock_bh(&ar->data_lock);
8594 		ath10k_warn(ar, "mac sta rc update failed to find peer %pM on vdev %i\n",
8595 			    sta->addr, arvif->vdev_id);
8596 		return;
8597 	}
8598 
8599 	ath10k_dbg(ar, ATH10K_DBG_STA,
8600 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
8601 		   sta->addr, changed, sta->deflink.bandwidth,
8602 		   sta->deflink.rx_nss,
8603 		   sta->deflink.smps_mode);
8604 
8605 	if (changed & IEEE80211_RC_BW_CHANGED) {
8606 		bw = WMI_PEER_CHWIDTH_20MHZ;
8607 
8608 		switch (sta->deflink.bandwidth) {
8609 		case IEEE80211_STA_RX_BW_20:
8610 			bw = WMI_PEER_CHWIDTH_20MHZ;
8611 			break;
8612 		case IEEE80211_STA_RX_BW_40:
8613 			bw = WMI_PEER_CHWIDTH_40MHZ;
8614 			break;
8615 		case IEEE80211_STA_RX_BW_80:
8616 			bw = WMI_PEER_CHWIDTH_80MHZ;
8617 			break;
8618 		case IEEE80211_STA_RX_BW_160:
8619 			bw = WMI_PEER_CHWIDTH_160MHZ;
8620 			break;
8621 		default:
8622 			ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
8623 				    sta->deflink.bandwidth, sta->addr);
8624 			bw = WMI_PEER_CHWIDTH_20MHZ;
8625 			break;
8626 		}
8627 
8628 		arsta->bw = bw;
8629 	}
8630 
8631 	if (changed & IEEE80211_RC_NSS_CHANGED)
8632 		arsta->nss = sta->deflink.rx_nss;
8633 
8634 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
8635 		smps = WMI_PEER_SMPS_PS_NONE;
8636 
8637 		switch (sta->deflink.smps_mode) {
8638 		case IEEE80211_SMPS_AUTOMATIC:
8639 		case IEEE80211_SMPS_OFF:
8640 			smps = WMI_PEER_SMPS_PS_NONE;
8641 			break;
8642 		case IEEE80211_SMPS_STATIC:
8643 			smps = WMI_PEER_SMPS_STATIC;
8644 			break;
8645 		case IEEE80211_SMPS_DYNAMIC:
8646 			smps = WMI_PEER_SMPS_DYNAMIC;
8647 			break;
8648 		case IEEE80211_SMPS_NUM_MODES:
8649 			ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
8650 				    sta->deflink.smps_mode, sta->addr);
8651 			smps = WMI_PEER_SMPS_PS_NONE;
8652 			break;
8653 		}
8654 
8655 		arsta->smps = smps;
8656 	}
8657 
8658 	arsta->changed |= changed;
8659 
8660 	spin_unlock_bh(&ar->data_lock);
8661 
8662 	ieee80211_queue_work(hw, &arsta->update_wk);
8663 }
8664 
ath10k_offset_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,s64 tsf_offset)8665 static void ath10k_offset_tsf(struct ieee80211_hw *hw,
8666 			      struct ieee80211_vif *vif, s64 tsf_offset)
8667 {
8668 	struct ath10k *ar = hw->priv;
8669 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8670 	u32 offset, vdev_param;
8671 	int ret;
8672 
8673 	if (tsf_offset < 0) {
8674 		vdev_param = ar->wmi.vdev_param->dec_tsf;
8675 		offset = -tsf_offset;
8676 	} else {
8677 		vdev_param = ar->wmi.vdev_param->inc_tsf;
8678 		offset = tsf_offset;
8679 	}
8680 
8681 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
8682 					vdev_param, offset);
8683 
8684 	if (ret && ret != -EOPNOTSUPP)
8685 		ath10k_warn(ar, "failed to set tsf offset %d cmd %d: %d\n",
8686 			    offset, vdev_param, ret);
8687 }
8688 
ath10k_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)8689 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
8690 			       struct ieee80211_vif *vif,
8691 			       struct ieee80211_ampdu_params *params)
8692 {
8693 	struct ath10k *ar = hw->priv;
8694 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8695 	struct ieee80211_sta *sta = params->sta;
8696 	enum ieee80211_ampdu_mlme_action action = params->action;
8697 	u16 tid = params->tid;
8698 
8699 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %u action %d\n",
8700 		   arvif->vdev_id, sta->addr, tid, action);
8701 
8702 	switch (action) {
8703 	case IEEE80211_AMPDU_RX_START:
8704 	case IEEE80211_AMPDU_RX_STOP:
8705 		/* HTT AddBa/DelBa events trigger mac80211 Rx BA session
8706 		 * creation/removal. Do we need to verify this?
8707 		 */
8708 		return 0;
8709 	case IEEE80211_AMPDU_TX_START:
8710 	case IEEE80211_AMPDU_TX_STOP_CONT:
8711 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
8712 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
8713 	case IEEE80211_AMPDU_TX_OPERATIONAL:
8714 		/* Firmware offloads Tx aggregation entirely so deny mac80211
8715 		 * Tx aggregation requests.
8716 		 */
8717 #if defined(__FreeBSD__)
8718 	default:
8719 #endif
8720 		return -EOPNOTSUPP;
8721 	}
8722 
8723 	return -EINVAL;
8724 }
8725 
8726 static void
ath10k_mac_update_rx_channel(struct ath10k * ar,struct ieee80211_chanctx_conf * ctx,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs)8727 ath10k_mac_update_rx_channel(struct ath10k *ar,
8728 			     struct ieee80211_chanctx_conf *ctx,
8729 			     struct ieee80211_vif_chanctx_switch *vifs,
8730 			     int n_vifs)
8731 {
8732 	struct cfg80211_chan_def *def = NULL;
8733 
8734 	/* Both locks are required because ar->rx_channel is modified. This
8735 	 * allows readers to hold either lock.
8736 	 */
8737 	lockdep_assert_held(&ar->conf_mutex);
8738 	lockdep_assert_held(&ar->data_lock);
8739 
8740 	WARN_ON(ctx && vifs);
8741 	WARN_ON(vifs && !n_vifs);
8742 
8743 	/* FIXME: Sort of an optimization and a workaround. Peers and vifs are
8744 	 * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
8745 	 * ppdu on Rx may reduce performance on low-end systems. It should be
8746 	 * possible to make tables/hashmaps to speed the lookup up (be vary of
8747 	 * cpu data cache lines though regarding sizes) but to keep the initial
8748 	 * implementation simple and less intrusive fallback to the slow lookup
8749 	 * only for multi-channel cases. Single-channel cases will remain to
8750 	 * use the old channel derival and thus performance should not be
8751 	 * affected much.
8752 	 */
8753 	rcu_read_lock();
8754 	if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
8755 		ieee80211_iter_chan_contexts_atomic(ar->hw,
8756 						    ath10k_mac_get_any_chandef_iter,
8757 						    &def);
8758 
8759 		if (vifs)
8760 			def = &vifs[0].new_ctx->def;
8761 
8762 		ar->rx_channel = def->chan;
8763 	} else if ((ctx && ath10k_mac_num_chanctxs(ar) == 0) ||
8764 		   (ctx && (ar->state == ATH10K_STATE_RESTARTED))) {
8765 		/* During driver restart due to firmware assert, since mac80211
8766 		 * already has valid channel context for given radio, channel
8767 		 * context iteration return num_chanctx > 0. So fix rx_channel
8768 		 * when restart is in progress.
8769 		 */
8770 		ar->rx_channel = ctx->def.chan;
8771 	} else {
8772 		ar->rx_channel = NULL;
8773 	}
8774 	rcu_read_unlock();
8775 }
8776 
8777 static void
ath10k_mac_update_vif_chan(struct ath10k * ar,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs)8778 ath10k_mac_update_vif_chan(struct ath10k *ar,
8779 			   struct ieee80211_vif_chanctx_switch *vifs,
8780 			   int n_vifs)
8781 {
8782 	struct ath10k_vif *arvif;
8783 	int ret;
8784 	int i;
8785 
8786 	lockdep_assert_held(&ar->conf_mutex);
8787 
8788 	/* First stop monitor interface. Some FW versions crash if there's a
8789 	 * lone monitor interface.
8790 	 */
8791 	if (ar->monitor_started)
8792 		ath10k_monitor_stop(ar);
8793 
8794 	for (i = 0; i < n_vifs; i++) {
8795 		arvif = (void *)vifs[i].vif->drv_priv;
8796 
8797 		ath10k_dbg(ar, ATH10K_DBG_MAC,
8798 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
8799 			   arvif->vdev_id,
8800 			   vifs[i].old_ctx->def.chan->center_freq,
8801 			   vifs[i].new_ctx->def.chan->center_freq,
8802 			   vifs[i].old_ctx->def.width,
8803 			   vifs[i].new_ctx->def.width);
8804 
8805 		if (WARN_ON(!arvif->is_started))
8806 			continue;
8807 
8808 		if (WARN_ON(!arvif->is_up))
8809 			continue;
8810 
8811 		ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
8812 		if (ret) {
8813 			ath10k_warn(ar, "failed to down vdev %d: %d\n",
8814 				    arvif->vdev_id, ret);
8815 			continue;
8816 		}
8817 	}
8818 
8819 	/* All relevant vdevs are downed and associated channel resources
8820 	 * should be available for the channel switch now.
8821 	 */
8822 
8823 	spin_lock_bh(&ar->data_lock);
8824 	ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
8825 	spin_unlock_bh(&ar->data_lock);
8826 
8827 	for (i = 0; i < n_vifs; i++) {
8828 		arvif = (void *)vifs[i].vif->drv_priv;
8829 
8830 		if (WARN_ON(!arvif->is_started))
8831 			continue;
8832 
8833 		if (WARN_ON(!arvif->is_up))
8834 			continue;
8835 
8836 		ret = ath10k_mac_setup_bcn_tmpl(arvif);
8837 		if (ret)
8838 			ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
8839 				    ret);
8840 
8841 		ret = ath10k_mac_setup_prb_tmpl(arvif);
8842 		if (ret)
8843 			ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
8844 				    ret);
8845 
8846 		ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
8847 		if (ret) {
8848 			ath10k_warn(ar, "failed to restart vdev %d: %d\n",
8849 				    arvif->vdev_id, ret);
8850 			continue;
8851 		}
8852 
8853 		ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
8854 					 arvif->bssid);
8855 		if (ret) {
8856 			ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
8857 				    arvif->vdev_id, ret);
8858 			continue;
8859 		}
8860 	}
8861 
8862 	ath10k_monitor_recalc(ar);
8863 }
8864 
8865 static int
ath10k_mac_op_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)8866 ath10k_mac_op_add_chanctx(struct ieee80211_hw *hw,
8867 			  struct ieee80211_chanctx_conf *ctx)
8868 {
8869 	struct ath10k *ar = hw->priv;
8870 
8871 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8872 		   "mac chanctx add freq %u width %d ptr %p\n",
8873 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
8874 
8875 	mutex_lock(&ar->conf_mutex);
8876 
8877 	spin_lock_bh(&ar->data_lock);
8878 	ath10k_mac_update_rx_channel(ar, ctx, NULL, 0);
8879 	spin_unlock_bh(&ar->data_lock);
8880 
8881 	ath10k_recalc_radar_detection(ar);
8882 	ath10k_monitor_recalc(ar);
8883 
8884 	mutex_unlock(&ar->conf_mutex);
8885 
8886 	return 0;
8887 }
8888 
8889 static void
ath10k_mac_op_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)8890 ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
8891 			     struct ieee80211_chanctx_conf *ctx)
8892 {
8893 	struct ath10k *ar = hw->priv;
8894 
8895 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8896 		   "mac chanctx remove freq %u width %d ptr %p\n",
8897 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
8898 
8899 	mutex_lock(&ar->conf_mutex);
8900 
8901 	spin_lock_bh(&ar->data_lock);
8902 	ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
8903 	spin_unlock_bh(&ar->data_lock);
8904 
8905 	ath10k_recalc_radar_detection(ar);
8906 	ath10k_monitor_recalc(ar);
8907 
8908 	mutex_unlock(&ar->conf_mutex);
8909 }
8910 
8911 struct ath10k_mac_change_chanctx_arg {
8912 	struct ieee80211_chanctx_conf *ctx;
8913 	struct ieee80211_vif_chanctx_switch *vifs;
8914 	int n_vifs;
8915 	int next_vif;
8916 };
8917 
8918 static void
ath10k_mac_change_chanctx_cnt_iter(void * data,u8 * mac,struct ieee80211_vif * vif)8919 ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
8920 				   struct ieee80211_vif *vif)
8921 {
8922 	struct ath10k_mac_change_chanctx_arg *arg = data;
8923 
8924 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) != arg->ctx)
8925 		return;
8926 
8927 	arg->n_vifs++;
8928 }
8929 
8930 static void
ath10k_mac_change_chanctx_fill_iter(void * data,u8 * mac,struct ieee80211_vif * vif)8931 ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
8932 				    struct ieee80211_vif *vif)
8933 {
8934 	struct ath10k_mac_change_chanctx_arg *arg = data;
8935 	struct ieee80211_chanctx_conf *ctx;
8936 
8937 	ctx = rcu_access_pointer(vif->bss_conf.chanctx_conf);
8938 	if (ctx != arg->ctx)
8939 		return;
8940 
8941 	if (WARN_ON(arg->next_vif == arg->n_vifs))
8942 		return;
8943 
8944 	arg->vifs[arg->next_vif].vif = vif;
8945 	arg->vifs[arg->next_vif].old_ctx = ctx;
8946 	arg->vifs[arg->next_vif].new_ctx = ctx;
8947 	arg->next_vif++;
8948 }
8949 
8950 static void
ath10k_mac_op_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)8951 ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
8952 			     struct ieee80211_chanctx_conf *ctx,
8953 			     u32 changed)
8954 {
8955 	struct ath10k *ar = hw->priv;
8956 	struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
8957 
8958 	mutex_lock(&ar->conf_mutex);
8959 
8960 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8961 		   "mac chanctx change freq %u width %d ptr %p changed %x\n",
8962 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
8963 
8964 	/* This shouldn't really happen because channel switching should use
8965 	 * switch_vif_chanctx().
8966 	 */
8967 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
8968 		goto unlock;
8969 
8970 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
8971 		ieee80211_iterate_active_interfaces_atomic(
8972 					hw,
8973 					ATH10K_ITER_NORMAL_FLAGS,
8974 					ath10k_mac_change_chanctx_cnt_iter,
8975 					&arg);
8976 		if (arg.n_vifs == 0)
8977 			goto radar;
8978 
8979 		arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
8980 				   GFP_KERNEL);
8981 		if (!arg.vifs)
8982 			goto radar;
8983 
8984 		ieee80211_iterate_active_interfaces_atomic(
8985 					hw,
8986 					ATH10K_ITER_NORMAL_FLAGS,
8987 					ath10k_mac_change_chanctx_fill_iter,
8988 					&arg);
8989 		ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
8990 		kfree(arg.vifs);
8991 	}
8992 
8993 radar:
8994 	ath10k_recalc_radar_detection(ar);
8995 
8996 	/* FIXME: How to configure Rx chains properly? */
8997 
8998 	/* No other actions are actually necessary. Firmware maintains channel
8999 	 * definitions per vdev internally and there's no host-side channel
9000 	 * context abstraction to configure, e.g. channel width.
9001 	 */
9002 
9003 unlock:
9004 	mutex_unlock(&ar->conf_mutex);
9005 }
9006 
9007 static int
ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)9008 ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
9009 				 struct ieee80211_vif *vif,
9010 				 struct ieee80211_bss_conf *link_conf,
9011 				 struct ieee80211_chanctx_conf *ctx)
9012 {
9013 	struct ath10k *ar = hw->priv;
9014 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
9015 	int ret;
9016 
9017 	mutex_lock(&ar->conf_mutex);
9018 
9019 	ath10k_dbg(ar, ATH10K_DBG_MAC,
9020 		   "mac chanctx assign ptr %p vdev_id %i\n",
9021 		   ctx, arvif->vdev_id);
9022 
9023 	if (WARN_ON(arvif->is_started)) {
9024 		mutex_unlock(&ar->conf_mutex);
9025 		return -EBUSY;
9026 	}
9027 
9028 	ret = ath10k_vdev_start(arvif, &ctx->def);
9029 	if (ret) {
9030 		ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
9031 			    arvif->vdev_id, vif->addr,
9032 			    ctx->def.chan->center_freq, ret);
9033 		goto err;
9034 	}
9035 
9036 	arvif->is_started = true;
9037 
9038 	ret = ath10k_mac_vif_setup_ps(arvif);
9039 	if (ret) {
9040 		ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
9041 			    arvif->vdev_id, ret);
9042 		goto err_stop;
9043 	}
9044 
9045 	if (vif->type == NL80211_IFTYPE_MONITOR) {
9046 		ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
9047 		if (ret) {
9048 			ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
9049 				    arvif->vdev_id, ret);
9050 			goto err_stop;
9051 		}
9052 
9053 		arvif->is_up = true;
9054 	}
9055 
9056 	if (ath10k_mac_can_set_cts_prot(arvif)) {
9057 		ret = ath10k_mac_set_cts_prot(arvif);
9058 		if (ret)
9059 			ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
9060 				    arvif->vdev_id, ret);
9061 	}
9062 
9063 	if (ath10k_peer_stats_enabled(ar) &&
9064 	    ar->hw_params.tx_stats_over_pktlog) {
9065 		ar->pktlog_filter |= ATH10K_PKTLOG_PEER_STATS;
9066 		ret = ath10k_wmi_pdev_pktlog_enable(ar,
9067 						    ar->pktlog_filter);
9068 		if (ret) {
9069 			ath10k_warn(ar, "failed to enable pktlog %d\n", ret);
9070 			goto err_stop;
9071 		}
9072 	}
9073 
9074 	mutex_unlock(&ar->conf_mutex);
9075 	return 0;
9076 
9077 err_stop:
9078 	ath10k_vdev_stop(arvif);
9079 	arvif->is_started = false;
9080 	ath10k_mac_vif_setup_ps(arvif);
9081 
9082 err:
9083 	mutex_unlock(&ar->conf_mutex);
9084 	return ret;
9085 }
9086 
9087 static void
ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)9088 ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
9089 				   struct ieee80211_vif *vif,
9090 				   struct ieee80211_bss_conf *link_conf,
9091 				   struct ieee80211_chanctx_conf *ctx)
9092 {
9093 	struct ath10k *ar = hw->priv;
9094 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
9095 	int ret;
9096 
9097 	mutex_lock(&ar->conf_mutex);
9098 
9099 	ath10k_dbg(ar, ATH10K_DBG_MAC,
9100 		   "mac chanctx unassign ptr %p vdev_id %i\n",
9101 		   ctx, arvif->vdev_id);
9102 
9103 	WARN_ON(!arvif->is_started);
9104 
9105 	if (vif->type == NL80211_IFTYPE_MONITOR) {
9106 		WARN_ON(!arvif->is_up);
9107 
9108 		ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
9109 		if (ret)
9110 			ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
9111 				    arvif->vdev_id, ret);
9112 
9113 		arvif->is_up = false;
9114 	}
9115 
9116 	ret = ath10k_vdev_stop(arvif);
9117 	if (ret)
9118 		ath10k_warn(ar, "failed to stop vdev %i: %d\n",
9119 			    arvif->vdev_id, ret);
9120 
9121 	arvif->is_started = false;
9122 
9123 	mutex_unlock(&ar->conf_mutex);
9124 }
9125 
9126 static int
ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs,enum ieee80211_chanctx_switch_mode mode)9127 ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
9128 				 struct ieee80211_vif_chanctx_switch *vifs,
9129 				 int n_vifs,
9130 				 enum ieee80211_chanctx_switch_mode mode)
9131 {
9132 	struct ath10k *ar = hw->priv;
9133 
9134 	mutex_lock(&ar->conf_mutex);
9135 
9136 	ath10k_dbg(ar, ATH10K_DBG_MAC,
9137 		   "mac chanctx switch n_vifs %d mode %d\n",
9138 		   n_vifs, mode);
9139 	ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
9140 
9141 	mutex_unlock(&ar->conf_mutex);
9142 	return 0;
9143 }
9144 
ath10k_mac_op_sta_pre_rcu_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)9145 static void ath10k_mac_op_sta_pre_rcu_remove(struct ieee80211_hw *hw,
9146 					     struct ieee80211_vif *vif,
9147 					     struct ieee80211_sta *sta)
9148 {
9149 	struct ath10k *ar;
9150 	struct ath10k_peer *peer;
9151 
9152 	ar = hw->priv;
9153 
9154 	list_for_each_entry(peer, &ar->peers, list)
9155 		if (peer->sta == sta)
9156 			peer->removed = true;
9157 }
9158 
9159 /* HT MCS parameters with Nss = 1 */
9160 static const struct ath10k_index_ht_data_rate_type supported_ht_mcs_rate_nss1[] = {
9161 	/* MCS  L20   L40   S20  S40 */
9162 	{0,  { 65,  135,  72,  150} },
9163 	{1,  { 130, 270,  144, 300} },
9164 	{2,  { 195, 405,  217, 450} },
9165 	{3,  { 260, 540,  289, 600} },
9166 	{4,  { 390, 810,  433, 900} },
9167 	{5,  { 520, 1080, 578, 1200} },
9168 	{6,  { 585, 1215, 650, 1350} },
9169 	{7,  { 650, 1350, 722, 1500} }
9170 };
9171 
9172 /* HT MCS parameters with Nss = 2 */
9173 static const struct ath10k_index_ht_data_rate_type supported_ht_mcs_rate_nss2[] = {
9174 	/* MCS  L20    L40   S20   S40 */
9175 	{0,  {130,  270,  144,  300} },
9176 	{1,  {260,  540,  289,  600} },
9177 	{2,  {390,  810,  433,  900} },
9178 	{3,  {520,  1080, 578,  1200} },
9179 	{4,  {780,  1620, 867,  1800} },
9180 	{5,  {1040, 2160, 1156, 2400} },
9181 	{6,  {1170, 2430, 1300, 2700} },
9182 	{7,  {1300, 2700, 1444, 3000} }
9183 };
9184 
9185 /* MCS parameters with Nss = 1 */
9186 static const struct ath10k_index_vht_data_rate_type supported_vht_mcs_rate_nss1[] = {
9187 	/* MCS  L80    S80     L40   S40    L20   S20 */
9188 	{0,  {293,  325},  {135,  150},  {65,   72} },
9189 	{1,  {585,  650},  {270,  300},  {130,  144} },
9190 	{2,  {878,  975},  {405,  450},  {195,  217} },
9191 	{3,  {1170, 1300}, {540,  600},  {260,  289} },
9192 	{4,  {1755, 1950}, {810,  900},  {390,  433} },
9193 	{5,  {2340, 2600}, {1080, 1200}, {520,  578} },
9194 	{6,  {2633, 2925}, {1215, 1350}, {585,  650} },
9195 	{7,  {2925, 3250}, {1350, 1500}, {650,  722} },
9196 	{8,  {3510, 3900}, {1620, 1800}, {780,  867} },
9197 	{9,  {3900, 4333}, {1800, 2000}, {865,  960} }
9198 };
9199 
9200 /*MCS parameters with Nss = 2 */
9201 static const struct ath10k_index_vht_data_rate_type supported_vht_mcs_rate_nss2[] = {
9202 	/* MCS  L80    S80     L40   S40    L20   S20 */
9203 	{0,  {585,  650},  {270,  300},  {130,  144} },
9204 	{1,  {1170, 1300}, {540,  600},  {260,  289} },
9205 	{2,  {1755, 1950}, {810,  900},  {390,  433} },
9206 	{3,  {2340, 2600}, {1080, 1200}, {520,  578} },
9207 	{4,  {3510, 3900}, {1620, 1800}, {780,  867} },
9208 	{5,  {4680, 5200}, {2160, 2400}, {1040, 1156} },
9209 	{6,  {5265, 5850}, {2430, 2700}, {1170, 1300} },
9210 	{7,  {5850, 6500}, {2700, 3000}, {1300, 1444} },
9211 	{8,  {7020, 7800}, {3240, 3600}, {1560, 1733} },
9212 	{9,  {7800, 8667}, {3600, 4000}, {1730, 1920} }
9213 };
9214 
ath10k_mac_get_rate_flags_ht(struct ath10k * ar,u32 rate,u8 nss,u8 mcs,u8 * flags,u8 * bw)9215 static void ath10k_mac_get_rate_flags_ht(struct ath10k *ar, u32 rate, u8 nss, u8 mcs,
9216 					 u8 *flags, u8 *bw)
9217 {
9218 #if defined(__linux__)
9219 	struct ath10k_index_ht_data_rate_type *mcs_rate;
9220 #elif defined(__FreeBSD__)
9221 	const struct ath10k_index_ht_data_rate_type *mcs_rate;
9222 #endif
9223 	u8 index;
9224 	size_t len_nss1 = ARRAY_SIZE(supported_ht_mcs_rate_nss1);
9225 	size_t len_nss2 = ARRAY_SIZE(supported_ht_mcs_rate_nss2);
9226 
9227 	if (mcs >= (len_nss1 + len_nss2)) {
9228 		ath10k_warn(ar, "not supported mcs %d in current rate table", mcs);
9229 		return;
9230 	}
9231 
9232 #if defined(__linux__)
9233 	mcs_rate = (struct ath10k_index_ht_data_rate_type *)
9234 #elif defined(__FreeBSD__)
9235 	mcs_rate = (const struct ath10k_index_ht_data_rate_type *)
9236 #endif
9237 		   ((nss == 1) ? &supported_ht_mcs_rate_nss1 :
9238 		   &supported_ht_mcs_rate_nss2);
9239 
9240 	if (mcs >= len_nss1)
9241 		index = mcs - len_nss1;
9242 	else
9243 		index = mcs;
9244 
9245 	if (rate == mcs_rate[index].supported_rate[0]) {
9246 		*bw = RATE_INFO_BW_20;
9247 	} else if (rate == mcs_rate[index].supported_rate[1]) {
9248 		*bw |= RATE_INFO_BW_40;
9249 	} else if (rate == mcs_rate[index].supported_rate[2]) {
9250 		*bw |= RATE_INFO_BW_20;
9251 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
9252 	} else if (rate == mcs_rate[index].supported_rate[3]) {
9253 		*bw |= RATE_INFO_BW_40;
9254 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
9255 	} else {
9256 		ath10k_warn(ar, "invalid ht params rate %d 100kbps nss %d mcs %d",
9257 			    rate, nss, mcs);
9258 	}
9259 }
9260 
ath10k_mac_get_rate_flags_vht(struct ath10k * ar,u32 rate,u8 nss,u8 mcs,u8 * flags,u8 * bw)9261 static void ath10k_mac_get_rate_flags_vht(struct ath10k *ar, u32 rate, u8 nss, u8 mcs,
9262 					  u8 *flags, u8 *bw)
9263 {
9264 #if defined(__linux__)
9265 	struct ath10k_index_vht_data_rate_type *mcs_rate;
9266 
9267 	mcs_rate = (struct ath10k_index_vht_data_rate_type *)
9268 #elif defined(__FreeBSD__)
9269 	const struct ath10k_index_vht_data_rate_type *mcs_rate;
9270 
9271 	mcs_rate = (const struct ath10k_index_vht_data_rate_type *)
9272 #endif
9273 		   ((nss == 1) ? &supported_vht_mcs_rate_nss1 :
9274 		   &supported_vht_mcs_rate_nss2);
9275 
9276 	if (rate == mcs_rate[mcs].supported_VHT80_rate[0]) {
9277 		*bw = RATE_INFO_BW_80;
9278 	} else if (rate == mcs_rate[mcs].supported_VHT80_rate[1]) {
9279 		*bw = RATE_INFO_BW_80;
9280 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
9281 	} else if (rate == mcs_rate[mcs].supported_VHT40_rate[0]) {
9282 		*bw = RATE_INFO_BW_40;
9283 	} else if (rate == mcs_rate[mcs].supported_VHT40_rate[1]) {
9284 		*bw = RATE_INFO_BW_40;
9285 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
9286 	} else if (rate == mcs_rate[mcs].supported_VHT20_rate[0]) {
9287 		*bw = RATE_INFO_BW_20;
9288 	} else if (rate == mcs_rate[mcs].supported_VHT20_rate[1]) {
9289 		*bw = RATE_INFO_BW_20;
9290 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
9291 	} else {
9292 		ath10k_warn(ar, "invalid vht params rate %d 100kbps nss %d mcs %d",
9293 			    rate, nss, mcs);
9294 	}
9295 }
9296 
ath10k_mac_get_rate_flags(struct ath10k * ar,u32 rate,enum ath10k_phy_mode mode,u8 nss,u8 mcs,u8 * flags,u8 * bw)9297 static void ath10k_mac_get_rate_flags(struct ath10k *ar, u32 rate,
9298 				      enum ath10k_phy_mode mode, u8 nss, u8 mcs,
9299 				      u8 *flags, u8 *bw)
9300 {
9301 	if (mode == ATH10K_PHY_MODE_HT) {
9302 		*flags = RATE_INFO_FLAGS_MCS;
9303 		ath10k_mac_get_rate_flags_ht(ar, rate, nss, mcs, flags, bw);
9304 	} else if (mode == ATH10K_PHY_MODE_VHT) {
9305 		*flags = RATE_INFO_FLAGS_VHT_MCS;
9306 		ath10k_mac_get_rate_flags_vht(ar, rate, nss, mcs, flags, bw);
9307 	}
9308 }
9309 
ath10k_mac_parse_bitrate(struct ath10k * ar,u32 rate_code,u32 bitrate_kbps,struct rate_info * rate)9310 static void ath10k_mac_parse_bitrate(struct ath10k *ar, u32 rate_code,
9311 				     u32 bitrate_kbps, struct rate_info *rate)
9312 {
9313 	enum ath10k_phy_mode mode = ATH10K_PHY_MODE_LEGACY;
9314 	enum wmi_rate_preamble preamble = WMI_TLV_GET_HW_RC_PREAM_V1(rate_code);
9315 	u8 nss = WMI_TLV_GET_HW_RC_NSS_V1(rate_code) + 1;
9316 	u8 mcs = WMI_TLV_GET_HW_RC_RATE_V1(rate_code);
9317 	u8 flags = 0, bw = 0;
9318 
9319 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac parse rate code 0x%x bitrate %d kbps\n",
9320 		   rate_code, bitrate_kbps);
9321 
9322 	if (preamble == WMI_RATE_PREAMBLE_HT)
9323 		mode = ATH10K_PHY_MODE_HT;
9324 	else if (preamble == WMI_RATE_PREAMBLE_VHT)
9325 		mode = ATH10K_PHY_MODE_VHT;
9326 
9327 	ath10k_mac_get_rate_flags(ar, bitrate_kbps / 100, mode, nss, mcs, &flags, &bw);
9328 
9329 	ath10k_dbg(ar, ATH10K_DBG_MAC,
9330 		   "mac parse bitrate preamble %d mode %d nss %d mcs %d flags %x bw %d\n",
9331 		   preamble, mode, nss, mcs, flags, bw);
9332 
9333 	rate->flags = flags;
9334 	rate->bw = bw;
9335 	rate->legacy = bitrate_kbps / 100;
9336 	rate->nss = nss;
9337 	rate->mcs = mcs;
9338 }
9339 
ath10k_mac_sta_get_peer_stats_info(struct ath10k * ar,struct ieee80211_sta * sta,struct station_info * sinfo)9340 static void ath10k_mac_sta_get_peer_stats_info(struct ath10k *ar,
9341 					       struct ieee80211_sta *sta,
9342 					       struct station_info *sinfo)
9343 {
9344 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
9345 	struct ath10k_peer *peer;
9346 	unsigned long time_left;
9347 	int ret;
9348 
9349 	if (!(ar->hw_params.supports_peer_stats_info &&
9350 	      arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA))
9351 		return;
9352 
9353 	spin_lock_bh(&ar->data_lock);
9354 	peer = ath10k_peer_find(ar, arsta->arvif->vdev_id, sta->addr);
9355 	spin_unlock_bh(&ar->data_lock);
9356 	if (!peer)
9357 		return;
9358 
9359 	reinit_completion(&ar->peer_stats_info_complete);
9360 
9361 	ret = ath10k_wmi_request_peer_stats_info(ar,
9362 						 arsta->arvif->vdev_id,
9363 						 WMI_REQUEST_ONE_PEER_STATS_INFO,
9364 						 arsta->arvif->bssid,
9365 						 0);
9366 	if (ret && ret != -EOPNOTSUPP) {
9367 		ath10k_warn(ar, "could not request peer stats info: %d\n", ret);
9368 		return;
9369 	}
9370 
9371 	time_left = wait_for_completion_timeout(&ar->peer_stats_info_complete, 3 * HZ);
9372 	if (time_left == 0) {
9373 		ath10k_warn(ar, "timed out waiting peer stats info\n");
9374 		return;
9375 	}
9376 
9377 	if (arsta->rx_rate_code != 0 && arsta->rx_bitrate_kbps != 0) {
9378 		ath10k_mac_parse_bitrate(ar, arsta->rx_rate_code,
9379 					 arsta->rx_bitrate_kbps,
9380 					 &sinfo->rxrate);
9381 
9382 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
9383 		arsta->rx_rate_code = 0;
9384 		arsta->rx_bitrate_kbps = 0;
9385 	}
9386 
9387 	if (arsta->tx_rate_code != 0 && arsta->tx_bitrate_kbps != 0) {
9388 		ath10k_mac_parse_bitrate(ar, arsta->tx_rate_code,
9389 					 arsta->tx_bitrate_kbps,
9390 					 &sinfo->txrate);
9391 
9392 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
9393 		arsta->tx_rate_code = 0;
9394 		arsta->tx_bitrate_kbps = 0;
9395 	}
9396 }
9397 
ath10k_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)9398 static void ath10k_sta_statistics(struct ieee80211_hw *hw,
9399 				  struct ieee80211_vif *vif,
9400 				  struct ieee80211_sta *sta,
9401 				  struct station_info *sinfo)
9402 {
9403 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
9404 	struct ath10k *ar = arsta->arvif->ar;
9405 
9406 	if (!ath10k_peer_stats_enabled(ar))
9407 		return;
9408 
9409 	mutex_lock(&ar->conf_mutex);
9410 	ath10k_debug_fw_stats_request(ar);
9411 	mutex_unlock(&ar->conf_mutex);
9412 
9413 	sinfo->rx_duration = arsta->rx_duration;
9414 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
9415 
9416 	if (arsta->txrate.legacy || arsta->txrate.nss) {
9417 		if (arsta->txrate.legacy) {
9418 			sinfo->txrate.legacy = arsta->txrate.legacy;
9419 		} else {
9420 			sinfo->txrate.mcs = arsta->txrate.mcs;
9421 			sinfo->txrate.nss = arsta->txrate.nss;
9422 			sinfo->txrate.bw = arsta->txrate.bw;
9423 		}
9424 		sinfo->txrate.flags = arsta->txrate.flags;
9425 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
9426 	}
9427 
9428 	if (ar->htt.disable_tx_comp) {
9429 		sinfo->tx_failed = arsta->tx_failed;
9430 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
9431 	}
9432 
9433 	sinfo->tx_retries = arsta->tx_retries;
9434 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
9435 
9436 	ath10k_mac_sta_get_peer_stats_info(ar, sta, sinfo);
9437 }
9438 
ath10k_mac_op_set_tid_config(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct cfg80211_tid_config * tid_config)9439 static int ath10k_mac_op_set_tid_config(struct ieee80211_hw *hw,
9440 					struct ieee80211_vif *vif,
9441 					struct ieee80211_sta *sta,
9442 					struct cfg80211_tid_config *tid_config)
9443 {
9444 	struct ath10k *ar = hw->priv;
9445 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
9446 	struct ath10k_mac_iter_tid_conf_data data = {};
9447 	struct wmi_per_peer_per_tid_cfg_arg arg = {};
9448 	int ret, i;
9449 
9450 	mutex_lock(&ar->conf_mutex);
9451 	arg.vdev_id = arvif->vdev_id;
9452 
9453 	arvif->tids_rst = 0;
9454 	memset(arvif->tid_conf_changed, 0, sizeof(arvif->tid_conf_changed));
9455 
9456 	for (i = 0; i < tid_config->n_tid_conf; i++) {
9457 		ret = ath10k_mac_parse_tid_config(ar, sta, vif,
9458 						  &tid_config->tid_conf[i],
9459 						  &arg);
9460 		if (ret)
9461 			goto exit;
9462 	}
9463 
9464 	ret = 0;
9465 
9466 	if (sta)
9467 		goto exit;
9468 
9469 	arvif->tids_rst = 0;
9470 	data.curr_vif = vif;
9471 	data.ar = ar;
9472 
9473 	ieee80211_iterate_stations_atomic(hw, ath10k_mac_vif_stations_tid_conf,
9474 					  &data);
9475 
9476 exit:
9477 	mutex_unlock(&ar->conf_mutex);
9478 	return ret;
9479 }
9480 
ath10k_mac_op_reset_tid_config(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u8 tids)9481 static int ath10k_mac_op_reset_tid_config(struct ieee80211_hw *hw,
9482 					  struct ieee80211_vif *vif,
9483 					  struct ieee80211_sta *sta,
9484 					  u8 tids)
9485 {
9486 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
9487 	struct ath10k_mac_iter_tid_conf_data data = {};
9488 	struct ath10k *ar = hw->priv;
9489 	int ret = 0;
9490 
9491 	mutex_lock(&ar->conf_mutex);
9492 
9493 	if (sta) {
9494 		arvif->tids_rst = 0;
9495 		ret = ath10k_mac_reset_tid_config(ar, sta, arvif, tids);
9496 		goto exit;
9497 	}
9498 
9499 	arvif->tids_rst = tids;
9500 	data.curr_vif = vif;
9501 	data.ar = ar;
9502 	ieee80211_iterate_stations_atomic(hw, ath10k_mac_vif_stations_tid_conf,
9503 					  &data);
9504 
9505 exit:
9506 	mutex_unlock(&ar->conf_mutex);
9507 	return ret;
9508 }
9509 
9510 static const struct ieee80211_ops ath10k_ops = {
9511 	.tx				= ath10k_mac_op_tx,
9512 	.wake_tx_queue			= ath10k_mac_op_wake_tx_queue,
9513 	.start				= ath10k_start,
9514 	.stop				= ath10k_stop,
9515 	.config				= ath10k_config,
9516 	.add_interface			= ath10k_add_interface,
9517 	.update_vif_offload		= ath10k_update_vif_offload,
9518 	.remove_interface		= ath10k_remove_interface,
9519 	.configure_filter		= ath10k_configure_filter,
9520 	.bss_info_changed		= ath10k_bss_info_changed,
9521 	.set_coverage_class		= ath10k_mac_op_set_coverage_class,
9522 	.hw_scan			= ath10k_hw_scan,
9523 	.cancel_hw_scan			= ath10k_cancel_hw_scan,
9524 	.set_key			= ath10k_set_key,
9525 	.set_default_unicast_key        = ath10k_set_default_unicast_key,
9526 	.sta_state			= ath10k_sta_state,
9527 	.sta_set_txpwr			= ath10k_sta_set_txpwr,
9528 	.conf_tx			= ath10k_conf_tx,
9529 	.remain_on_channel		= ath10k_remain_on_channel,
9530 	.cancel_remain_on_channel	= ath10k_cancel_remain_on_channel,
9531 	.set_rts_threshold		= ath10k_set_rts_threshold,
9532 	.set_frag_threshold		= ath10k_mac_op_set_frag_threshold,
9533 	.flush				= ath10k_flush,
9534 	.tx_last_beacon			= ath10k_tx_last_beacon,
9535 	.set_antenna			= ath10k_set_antenna,
9536 	.get_antenna			= ath10k_get_antenna,
9537 	.reconfig_complete		= ath10k_reconfig_complete,
9538 	.get_survey			= ath10k_get_survey,
9539 	.set_bitrate_mask		= ath10k_mac_op_set_bitrate_mask,
9540 	.link_sta_rc_update		= ath10k_sta_rc_update,
9541 	.offset_tsf			= ath10k_offset_tsf,
9542 	.ampdu_action			= ath10k_ampdu_action,
9543 	.get_et_sset_count		= ath10k_debug_get_et_sset_count,
9544 	.get_et_stats			= ath10k_debug_get_et_stats,
9545 	.get_et_strings			= ath10k_debug_get_et_strings,
9546 	.add_chanctx			= ath10k_mac_op_add_chanctx,
9547 	.remove_chanctx			= ath10k_mac_op_remove_chanctx,
9548 	.change_chanctx			= ath10k_mac_op_change_chanctx,
9549 	.assign_vif_chanctx		= ath10k_mac_op_assign_vif_chanctx,
9550 	.unassign_vif_chanctx		= ath10k_mac_op_unassign_vif_chanctx,
9551 	.switch_vif_chanctx		= ath10k_mac_op_switch_vif_chanctx,
9552 	.sta_pre_rcu_remove		= ath10k_mac_op_sta_pre_rcu_remove,
9553 	.sta_statistics			= ath10k_sta_statistics,
9554 	.set_tid_config			= ath10k_mac_op_set_tid_config,
9555 	.reset_tid_config		= ath10k_mac_op_reset_tid_config,
9556 
9557 	CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
9558 
9559 #ifdef CONFIG_PM
9560 	.suspend			= ath10k_wow_op_suspend,
9561 	.resume				= ath10k_wow_op_resume,
9562 	.set_wakeup			= ath10k_wow_op_set_wakeup,
9563 #endif
9564 #ifdef CONFIG_MAC80211_DEBUGFS
9565 	.sta_add_debugfs		= ath10k_sta_add_debugfs,
9566 #endif
9567 	.set_sar_specs			= ath10k_mac_set_sar_specs,
9568 };
9569 
9570 #define CHAN2G(_channel, _freq, _flags) { \
9571 	.band			= NL80211_BAND_2GHZ, \
9572 	.hw_value		= (_channel), \
9573 	.center_freq		= (_freq), \
9574 	.flags			= (_flags), \
9575 	.max_antenna_gain	= 0, \
9576 	.max_power		= 30, \
9577 }
9578 
9579 #define CHAN5G(_channel, _freq, _flags) { \
9580 	.band			= NL80211_BAND_5GHZ, \
9581 	.hw_value		= (_channel), \
9582 	.center_freq		= (_freq), \
9583 	.flags			= (_flags), \
9584 	.max_antenna_gain	= 0, \
9585 	.max_power		= 30, \
9586 }
9587 
9588 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
9589 	CHAN2G(1, 2412, 0),
9590 	CHAN2G(2, 2417, 0),
9591 	CHAN2G(3, 2422, 0),
9592 	CHAN2G(4, 2427, 0),
9593 	CHAN2G(5, 2432, 0),
9594 	CHAN2G(6, 2437, 0),
9595 	CHAN2G(7, 2442, 0),
9596 	CHAN2G(8, 2447, 0),
9597 	CHAN2G(9, 2452, 0),
9598 	CHAN2G(10, 2457, 0),
9599 	CHAN2G(11, 2462, 0),
9600 	CHAN2G(12, 2467, 0),
9601 	CHAN2G(13, 2472, 0),
9602 	CHAN2G(14, 2484, 0),
9603 };
9604 
9605 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
9606 	CHAN5G(36, 5180, 0),
9607 	CHAN5G(40, 5200, 0),
9608 	CHAN5G(44, 5220, 0),
9609 	CHAN5G(48, 5240, 0),
9610 	CHAN5G(52, 5260, 0),
9611 	CHAN5G(56, 5280, 0),
9612 	CHAN5G(60, 5300, 0),
9613 	CHAN5G(64, 5320, 0),
9614 	CHAN5G(100, 5500, 0),
9615 	CHAN5G(104, 5520, 0),
9616 	CHAN5G(108, 5540, 0),
9617 	CHAN5G(112, 5560, 0),
9618 	CHAN5G(116, 5580, 0),
9619 	CHAN5G(120, 5600, 0),
9620 	CHAN5G(124, 5620, 0),
9621 	CHAN5G(128, 5640, 0),
9622 	CHAN5G(132, 5660, 0),
9623 	CHAN5G(136, 5680, 0),
9624 	CHAN5G(140, 5700, 0),
9625 	CHAN5G(144, 5720, 0),
9626 	CHAN5G(149, 5745, 0),
9627 	CHAN5G(153, 5765, 0),
9628 	CHAN5G(157, 5785, 0),
9629 	CHAN5G(161, 5805, 0),
9630 	CHAN5G(165, 5825, 0),
9631 	CHAN5G(169, 5845, 0),
9632 	CHAN5G(173, 5865, 0),
9633 	/* If you add more, you may need to change ATH10K_MAX_5G_CHAN */
9634 	/* And you will definitely need to change ATH10K_NUM_CHANS in core.h */
9635 };
9636 
ath10k_mac_create(size_t priv_size)9637 struct ath10k *ath10k_mac_create(size_t priv_size)
9638 {
9639 	struct ieee80211_hw *hw;
9640 	struct ieee80211_ops *ops;
9641 	struct ath10k *ar;
9642 
9643 	ops = kmemdup(&ath10k_ops, sizeof(ath10k_ops), GFP_KERNEL);
9644 	if (!ops)
9645 		return NULL;
9646 
9647 	hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, ops);
9648 	if (!hw) {
9649 		kfree(ops);
9650 		return NULL;
9651 	}
9652 
9653 	ar = hw->priv;
9654 	ar->hw = hw;
9655 	ar->ops = ops;
9656 
9657 	return ar;
9658 }
9659 
ath10k_mac_destroy(struct ath10k * ar)9660 void ath10k_mac_destroy(struct ath10k *ar)
9661 {
9662 	struct ieee80211_ops *ops = ar->ops;
9663 
9664 	ieee80211_free_hw(ar->hw);
9665 	kfree(ops);
9666 }
9667 
9668 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
9669 	{
9670 		.max	= 8,
9671 		.types	= BIT(NL80211_IFTYPE_STATION)
9672 			| BIT(NL80211_IFTYPE_P2P_CLIENT)
9673 	},
9674 	{
9675 		.max	= 3,
9676 		.types	= BIT(NL80211_IFTYPE_P2P_GO)
9677 	},
9678 	{
9679 		.max	= 1,
9680 		.types	= BIT(NL80211_IFTYPE_P2P_DEVICE)
9681 	},
9682 	{
9683 		.max	= 7,
9684 		.types	= BIT(NL80211_IFTYPE_AP)
9685 #ifdef CONFIG_MAC80211_MESH
9686 			| BIT(NL80211_IFTYPE_MESH_POINT)
9687 #endif
9688 	},
9689 };
9690 
9691 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
9692 	{
9693 		.max	= 8,
9694 		.types	= BIT(NL80211_IFTYPE_AP)
9695 #ifdef CONFIG_MAC80211_MESH
9696 			| BIT(NL80211_IFTYPE_MESH_POINT)
9697 #endif
9698 	},
9699 	{
9700 		.max	= 1,
9701 		.types	= BIT(NL80211_IFTYPE_STATION)
9702 	},
9703 };
9704 
9705 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
9706 	{
9707 		.limits = ath10k_if_limits,
9708 		.n_limits = ARRAY_SIZE(ath10k_if_limits),
9709 		.max_interfaces = 8,
9710 		.num_different_channels = 1,
9711 		.beacon_int_infra_match = true,
9712 	},
9713 };
9714 
9715 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
9716 	{
9717 		.limits = ath10k_10x_if_limits,
9718 		.n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
9719 		.max_interfaces = 8,
9720 		.num_different_channels = 1,
9721 		.beacon_int_infra_match = true,
9722 		.beacon_int_min_gcd = 1,
9723 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
9724 		.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9725 					BIT(NL80211_CHAN_WIDTH_20) |
9726 					BIT(NL80211_CHAN_WIDTH_40) |
9727 					BIT(NL80211_CHAN_WIDTH_80),
9728 #endif
9729 	},
9730 };
9731 
9732 static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
9733 	{
9734 		.max = 2,
9735 		.types = BIT(NL80211_IFTYPE_STATION),
9736 	},
9737 	{
9738 		.max = 2,
9739 		.types = BIT(NL80211_IFTYPE_AP) |
9740 #ifdef CONFIG_MAC80211_MESH
9741 			 BIT(NL80211_IFTYPE_MESH_POINT) |
9742 #endif
9743 			 BIT(NL80211_IFTYPE_P2P_CLIENT) |
9744 			 BIT(NL80211_IFTYPE_P2P_GO),
9745 	},
9746 	{
9747 		.max = 1,
9748 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
9749 	},
9750 };
9751 
9752 static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
9753 	{
9754 		.max = 2,
9755 		.types = BIT(NL80211_IFTYPE_STATION),
9756 	},
9757 	{
9758 		.max = 2,
9759 		.types = BIT(NL80211_IFTYPE_P2P_CLIENT),
9760 	},
9761 	{
9762 		.max = 1,
9763 		.types = BIT(NL80211_IFTYPE_AP) |
9764 #ifdef CONFIG_MAC80211_MESH
9765 			 BIT(NL80211_IFTYPE_MESH_POINT) |
9766 #endif
9767 			 BIT(NL80211_IFTYPE_P2P_GO),
9768 	},
9769 	{
9770 		.max = 1,
9771 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
9772 	},
9773 };
9774 
9775 static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
9776 	{
9777 		.max = 1,
9778 		.types = BIT(NL80211_IFTYPE_STATION),
9779 	},
9780 	{
9781 		.max = 1,
9782 		.types = BIT(NL80211_IFTYPE_ADHOC),
9783 	},
9784 };
9785 
9786 /* FIXME: This is not thoroughly tested. These combinations may over- or
9787  * underestimate hw/fw capabilities.
9788  */
9789 static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
9790 	{
9791 		.limits = ath10k_tlv_if_limit,
9792 		.num_different_channels = 1,
9793 		.max_interfaces = 4,
9794 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
9795 	},
9796 	{
9797 		.limits = ath10k_tlv_if_limit_ibss,
9798 		.num_different_channels = 1,
9799 		.max_interfaces = 2,
9800 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
9801 	},
9802 };
9803 
9804 static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
9805 	{
9806 		.limits = ath10k_tlv_if_limit,
9807 		.num_different_channels = 1,
9808 		.max_interfaces = 4,
9809 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
9810 	},
9811 	{
9812 		.limits = ath10k_tlv_qcs_if_limit,
9813 		.num_different_channels = 2,
9814 		.max_interfaces = 4,
9815 		.n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
9816 	},
9817 	{
9818 		.limits = ath10k_tlv_if_limit_ibss,
9819 		.num_different_channels = 1,
9820 		.max_interfaces = 2,
9821 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
9822 	},
9823 };
9824 
9825 static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
9826 	{
9827 		.max = 1,
9828 		.types = BIT(NL80211_IFTYPE_STATION),
9829 	},
9830 	{
9831 		.max	= 16,
9832 		.types	= BIT(NL80211_IFTYPE_AP)
9833 #ifdef CONFIG_MAC80211_MESH
9834 			| BIT(NL80211_IFTYPE_MESH_POINT)
9835 #endif
9836 	},
9837 };
9838 
9839 static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
9840 	{
9841 		.limits = ath10k_10_4_if_limits,
9842 		.n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
9843 		.max_interfaces = 16,
9844 		.num_different_channels = 1,
9845 		.beacon_int_infra_match = true,
9846 		.beacon_int_min_gcd = 1,
9847 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
9848 		.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9849 					BIT(NL80211_CHAN_WIDTH_20) |
9850 					BIT(NL80211_CHAN_WIDTH_40) |
9851 					BIT(NL80211_CHAN_WIDTH_80) |
9852 					BIT(NL80211_CHAN_WIDTH_80P80) |
9853 					BIT(NL80211_CHAN_WIDTH_160),
9854 #endif
9855 	},
9856 };
9857 
9858 static const struct
9859 ieee80211_iface_combination ath10k_10_4_bcn_int_if_comb[] = {
9860 	{
9861 		.limits = ath10k_10_4_if_limits,
9862 		.n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
9863 		.max_interfaces = 16,
9864 		.num_different_channels = 1,
9865 		.beacon_int_infra_match = true,
9866 		.beacon_int_min_gcd = 100,
9867 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
9868 		.radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9869 					BIT(NL80211_CHAN_WIDTH_20) |
9870 					BIT(NL80211_CHAN_WIDTH_40) |
9871 					BIT(NL80211_CHAN_WIDTH_80) |
9872 					BIT(NL80211_CHAN_WIDTH_80P80) |
9873 					BIT(NL80211_CHAN_WIDTH_160),
9874 #endif
9875 	},
9876 };
9877 
ath10k_get_arvif_iter(void * data,u8 * mac,struct ieee80211_vif * vif)9878 static void ath10k_get_arvif_iter(void *data, u8 *mac,
9879 				  struct ieee80211_vif *vif)
9880 {
9881 	struct ath10k_vif_iter *arvif_iter = data;
9882 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
9883 
9884 	if (arvif->vdev_id == arvif_iter->vdev_id)
9885 		arvif_iter->arvif = arvif;
9886 }
9887 
ath10k_get_arvif(struct ath10k * ar,u32 vdev_id)9888 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
9889 {
9890 	struct ath10k_vif_iter arvif_iter;
9891 
9892 	memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
9893 	arvif_iter.vdev_id = vdev_id;
9894 
9895 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
9896 						   ATH10K_ITER_RESUME_FLAGS,
9897 						   ath10k_get_arvif_iter,
9898 						   &arvif_iter);
9899 	if (!arvif_iter.arvif) {
9900 		ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
9901 		return NULL;
9902 	}
9903 
9904 	return arvif_iter.arvif;
9905 }
9906 
9907 #define WRD_METHOD "WRDD"
9908 #define WRDD_WIFI  (0x07)
9909 
ath10k_mac_wrdd_get_mcc(struct ath10k * ar,union acpi_object * wrdd)9910 static u32 ath10k_mac_wrdd_get_mcc(struct ath10k *ar, union acpi_object *wrdd)
9911 {
9912 	union acpi_object *mcc_pkg;
9913 	union acpi_object *domain_type;
9914 	union acpi_object *mcc_value;
9915 	u32 i;
9916 
9917 #if defined(__linux__)
9918 	if (wrdd->type != ACPI_TYPE_PACKAGE ||
9919 	    wrdd->package.count < 2 ||
9920 	    wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
9921 	    wrdd->package.elements[0].integer.value != 0) {
9922 #elif defined(__FreeBSD__)
9923 	if (wrdd->Type != ACPI_TYPE_PACKAGE ||
9924 	    wrdd->Package.Count < 2 ||
9925 	    wrdd->Package.Elements[0].Type != ACPI_TYPE_INTEGER ||
9926 	    wrdd->Package.Elements[0].Integer.Value != 0) {
9927 #endif
9928 		ath10k_warn(ar, "ignoring malformed/unsupported wrdd structure\n");
9929 		return 0;
9930 	}
9931 
9932 #if defined(__linux__)
9933 	for (i = 1; i < wrdd->package.count; ++i) {
9934 		mcc_pkg = &wrdd->package.elements[i];
9935 
9936 		if (mcc_pkg->type != ACPI_TYPE_PACKAGE)
9937 			continue;
9938 		if (mcc_pkg->package.count < 2)
9939 			continue;
9940 		if (mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
9941 		    mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER)
9942 			continue;
9943 
9944 		domain_type = &mcc_pkg->package.elements[0];
9945 		if (domain_type->integer.value != WRDD_WIFI)
9946 			continue;
9947 
9948 		mcc_value = &mcc_pkg->package.elements[1];
9949 		return mcc_value->integer.value;
9950 #elif defined(__FreeBSD__)
9951 	for (i = 1; i < wrdd->Package.Count; ++i) {
9952 		mcc_pkg = &wrdd->Package.Elements[i];
9953 
9954 		if (mcc_pkg->Type != ACPI_TYPE_PACKAGE)
9955 			continue;
9956 		if (mcc_pkg->Package.Count < 2)
9957 			continue;
9958 		if (mcc_pkg->Package.Elements[0].Type != ACPI_TYPE_INTEGER ||
9959 		    mcc_pkg->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
9960 			continue;
9961 
9962 		domain_type = &mcc_pkg->Package.Elements[0];
9963 		if (domain_type->Integer.Value != WRDD_WIFI)
9964 			continue;
9965 
9966 		mcc_value = &mcc_pkg->Package.Elements[1];
9967 		return mcc_value->Integer.Value;
9968 #endif
9969 	}
9970 	return 0;
9971 }
9972 
9973 static int ath10k_mac_get_wrdd_regulatory(struct ath10k *ar, u16 *rd)
9974 {
9975 	acpi_handle root_handle;
9976 	acpi_handle handle;
9977 	struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
9978 	acpi_status status;
9979 	u32 alpha2_code;
9980 	char alpha2[3];
9981 
9982 	root_handle = ACPI_HANDLE(ar->dev);
9983 	if (!root_handle)
9984 		return -EOPNOTSUPP;
9985 
9986 	status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
9987 	if (ACPI_FAILURE(status)) {
9988 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
9989 			   "failed to get wrd method %d\n", status);
9990 		return -EIO;
9991 	}
9992 
9993 	status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
9994 	if (ACPI_FAILURE(status)) {
9995 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
9996 			   "failed to call wrdc %d\n", status);
9997 		return -EIO;
9998 	}
9999 
10000 #if defined(__linux__)
10001 	alpha2_code = ath10k_mac_wrdd_get_mcc(ar, wrdd.pointer);
10002 	kfree(wrdd.pointer);
10003 #elif defined(__FreeBSD__)
10004 	alpha2_code = ath10k_mac_wrdd_get_mcc(ar, wrdd.Pointer);
10005 	kfree(wrdd.Pointer);
10006 #endif
10007 	if (!alpha2_code)
10008 		return -EIO;
10009 
10010 	alpha2[0] = (alpha2_code >> 8) & 0xff;
10011 	alpha2[1] = (alpha2_code >> 0) & 0xff;
10012 	alpha2[2] = '\0';
10013 
10014 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
10015 		   "regulatory hint from WRDD (alpha2-code): %s\n", alpha2);
10016 
10017 	*rd = ath_regd_find_country_by_name(alpha2);
10018 	if (*rd == 0xffff)
10019 		return -EIO;
10020 
10021 	*rd |= COUNTRY_ERD_FLAG;
10022 	return 0;
10023 }
10024 
10025 static int ath10k_mac_init_rd(struct ath10k *ar)
10026 {
10027 	int ret;
10028 	u16 rd;
10029 
10030 	ret = ath10k_mac_get_wrdd_regulatory(ar, &rd);
10031 	if (ret) {
10032 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
10033 			   "fallback to eeprom programmed regulatory settings\n");
10034 		rd = ar->hw_eeprom_rd;
10035 	}
10036 
10037 	ar->ath_common.regulatory.current_rd = rd;
10038 	return 0;
10039 }
10040 
10041 int ath10k_mac_register(struct ath10k *ar)
10042 {
10043 	static const u32 cipher_suites[] = {
10044 		WLAN_CIPHER_SUITE_WEP40,
10045 		WLAN_CIPHER_SUITE_WEP104,
10046 		WLAN_CIPHER_SUITE_TKIP,
10047 		WLAN_CIPHER_SUITE_CCMP,
10048 
10049 		/* Do not add hardware supported ciphers before this line.
10050 		 * Allow software encryption for all chips. Don't forget to
10051 		 * update n_cipher_suites below.
10052 		 */
10053 		WLAN_CIPHER_SUITE_AES_CMAC,
10054 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
10055 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
10056 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
10057 
10058 		/* Only QCA99x0 and QCA4019 variants support GCMP-128, GCMP-256
10059 		 * and CCMP-256 in hardware.
10060 		 */
10061 		WLAN_CIPHER_SUITE_GCMP,
10062 		WLAN_CIPHER_SUITE_GCMP_256,
10063 		WLAN_CIPHER_SUITE_CCMP_256,
10064 	};
10065 	struct ieee80211_supported_band *band;
10066 	void *channels;
10067 	int ret;
10068 
10069 	if (!is_valid_ether_addr(ar->mac_addr)) {
10070 		ath10k_warn(ar, "invalid MAC address; choosing random\n");
10071 		eth_random_addr(ar->mac_addr);
10072 	}
10073 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
10074 
10075 	SET_IEEE80211_DEV(ar->hw, ar->dev);
10076 
10077 	BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
10078 		      ARRAY_SIZE(ath10k_5ghz_channels)) !=
10079 		     ATH10K_NUM_CHANS);
10080 
10081 	if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
10082 		channels = kmemdup(ath10k_2ghz_channels,
10083 				   sizeof(ath10k_2ghz_channels),
10084 				   GFP_KERNEL);
10085 		if (!channels) {
10086 			ret = -ENOMEM;
10087 			goto err_free;
10088 		}
10089 
10090 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
10091 		band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
10092 		band->channels = channels;
10093 
10094 		if (ar->hw_params.cck_rate_map_rev2) {
10095 			band->n_bitrates = ath10k_g_rates_rev2_size;
10096 			band->bitrates = ath10k_g_rates_rev2;
10097 		} else {
10098 			band->n_bitrates = ath10k_g_rates_size;
10099 			band->bitrates = ath10k_g_rates;
10100 		}
10101 
10102 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
10103 	}
10104 
10105 	if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
10106 		channels = kmemdup(ath10k_5ghz_channels,
10107 				   sizeof(ath10k_5ghz_channels),
10108 				   GFP_KERNEL);
10109 		if (!channels) {
10110 			ret = -ENOMEM;
10111 			goto err_free;
10112 		}
10113 
10114 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
10115 		band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
10116 		band->channels = channels;
10117 		band->n_bitrates = ath10k_a_rates_size;
10118 		band->bitrates = ath10k_a_rates;
10119 		ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
10120 	}
10121 
10122 	wiphy_read_of_freq_limits(ar->hw->wiphy);
10123 	ath10k_mac_setup_ht_vht_cap(ar);
10124 
10125 	ar->hw->wiphy->interface_modes =
10126 		BIT(NL80211_IFTYPE_STATION) |
10127 		BIT(NL80211_IFTYPE_AP) |
10128 		BIT(NL80211_IFTYPE_MESH_POINT);
10129 
10130 	ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
10131 	ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
10132 
10133 	if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->normal_mode_fw.fw_file.fw_features))
10134 		ar->hw->wiphy->interface_modes |=
10135 			BIT(NL80211_IFTYPE_P2P_DEVICE) |
10136 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
10137 			BIT(NL80211_IFTYPE_P2P_GO);
10138 
10139 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
10140 
10141 	if (!test_bit(ATH10K_FW_FEATURE_NO_PS,
10142 		      ar->running_fw->fw_file.fw_features)) {
10143 		ieee80211_hw_set(ar->hw, SUPPORTS_PS);
10144 		ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
10145 	}
10146 
10147 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
10148 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
10149 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
10150 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
10151 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
10152 	ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
10153 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
10154 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
10155 	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
10156 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
10157 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
10158 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
10159 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
10160 
10161 	if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
10162 		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
10163 
10164 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
10165 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
10166 
10167 	if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
10168 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
10169 
10170 	if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
10171 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
10172 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
10173 	}
10174 
10175 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
10176 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
10177 
10178 	if (test_bit(WMI_SERVICE_NLO, ar->wmi.svc_map)) {
10179 		ar->hw->wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
10180 		ar->hw->wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
10181 		ar->hw->wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
10182 		ar->hw->wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
10183 		ar->hw->wiphy->max_sched_scan_plan_interval =
10184 			WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
10185 		ar->hw->wiphy->max_sched_scan_plan_iterations =
10186 			WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
10187 		ar->hw->wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
10188 	}
10189 
10190 	ar->hw->vif_data_size = sizeof(struct ath10k_vif);
10191 	ar->hw->sta_data_size = sizeof(struct ath10k_sta);
10192 	ar->hw->txq_data_size = sizeof(struct ath10k_txq);
10193 
10194 	ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
10195 
10196 	if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
10197 		ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
10198 
10199 		/* Firmware delivers WPS/P2P Probe Requests frames to driver so
10200 		 * that userspace (e.g. wpa_supplicant/hostapd) can generate
10201 		 * correct Probe Responses. This is more of a hack advert..
10202 		 */
10203 		ar->hw->wiphy->probe_resp_offload |=
10204 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
10205 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
10206 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
10207 	}
10208 
10209 	if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map) ||
10210 	    test_bit(WMI_SERVICE_TDLS_EXPLICIT_MODE_ONLY, ar->wmi.svc_map)) {
10211 		ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
10212 		if (test_bit(WMI_SERVICE_TDLS_WIDER_BANDWIDTH, ar->wmi.svc_map))
10213 			ieee80211_hw_set(ar->hw, TDLS_WIDER_BW);
10214 	}
10215 
10216 	if (test_bit(WMI_SERVICE_TDLS_UAPSD_BUFFER_STA, ar->wmi.svc_map))
10217 		ieee80211_hw_set(ar->hw, SUPPORTS_TDLS_BUFFER_STA);
10218 
10219 	if (ath10k_frame_mode == ATH10K_HW_TXRX_ETHERNET) {
10220 		if (ar->wmi.vdev_param->tx_encap_type !=
10221 		    WMI_VDEV_PARAM_UNSUPPORTED)
10222 			ieee80211_hw_set(ar->hw, SUPPORTS_TX_ENCAP_OFFLOAD);
10223 	}
10224 
10225 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
10226 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
10227 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
10228 
10229 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
10230 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
10231 				   NL80211_FEATURE_AP_SCAN;
10232 
10233 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
10234 
10235 	ret = ath10k_wow_init(ar);
10236 	if (ret) {
10237 		ath10k_warn(ar, "failed to init wow: %d\n", ret);
10238 		goto err_free;
10239 	}
10240 
10241 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
10242 	wiphy_ext_feature_set(ar->hw->wiphy,
10243 			      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
10244 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_AQL);
10245 
10246 	if (ar->hw_params.mcast_frame_registration)
10247 		wiphy_ext_feature_set(ar->hw->wiphy,
10248 				      NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
10249 
10250 	if (test_bit(WMI_SERVICE_TX_DATA_ACK_RSSI, ar->wmi.svc_map) ||
10251 	    test_bit(WMI_SERVICE_HTT_MGMT_TX_COMP_VALID_FLAGS, ar->wmi.svc_map))
10252 		wiphy_ext_feature_set(ar->hw->wiphy,
10253 				      NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
10254 
10255 	if (ath10k_peer_stats_enabled(ar) ||
10256 	    test_bit(WMI_SERVICE_REPORT_AIRTIME, ar->wmi.svc_map))
10257 		wiphy_ext_feature_set(ar->hw->wiphy,
10258 				      NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
10259 
10260 	if (test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map))
10261 		wiphy_ext_feature_set(ar->hw->wiphy,
10262 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
10263 
10264 	if (test_bit(WMI_SERVICE_TX_PWR_PER_PEER, ar->wmi.svc_map))
10265 		wiphy_ext_feature_set(ar->hw->wiphy,
10266 				      NL80211_EXT_FEATURE_STA_TX_PWR);
10267 
10268 	if (test_bit(WMI_SERVICE_PEER_TID_CONFIGS_SUPPORT, ar->wmi.svc_map)) {
10269 		ar->hw->wiphy->tid_config_support.vif |=
10270 				BIT(NL80211_TID_CONFIG_ATTR_NOACK) |
10271 				BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT) |
10272 				BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG) |
10273 				BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL) |
10274 				BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
10275 				BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
10276 
10277 		if (test_bit(WMI_SERVICE_EXT_PEER_TID_CONFIGS_SUPPORT,
10278 			     ar->wmi.svc_map)) {
10279 			ar->hw->wiphy->tid_config_support.vif |=
10280 				BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
10281 		}
10282 
10283 		ar->hw->wiphy->tid_config_support.peer =
10284 				ar->hw->wiphy->tid_config_support.vif;
10285 		ar->hw->wiphy->max_data_retry_count = ATH10K_MAX_RETRY_COUNT;
10286 	} else {
10287 		ar->ops->set_tid_config = NULL;
10288 	}
10289 	/*
10290 	 * on LL hardware queues are managed entirely by the FW
10291 	 * so we only advertise to mac we can do the queues thing
10292 	 */
10293 	ar->hw->queues = IEEE80211_MAX_QUEUES;
10294 
10295 	/* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
10296 	 * something that vdev_ids can't reach so that we don't stop the queue
10297 	 * accidentally.
10298 	 */
10299 	ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
10300 
10301 	switch (ar->running_fw->fw_file.wmi_op_version) {
10302 	case ATH10K_FW_WMI_OP_VERSION_MAIN:
10303 		ar->hw->wiphy->iface_combinations = ath10k_if_comb;
10304 		ar->hw->wiphy->n_iface_combinations =
10305 			ARRAY_SIZE(ath10k_if_comb);
10306 		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
10307 		break;
10308 	case ATH10K_FW_WMI_OP_VERSION_TLV:
10309 		if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
10310 			ar->hw->wiphy->iface_combinations =
10311 				ath10k_tlv_qcs_if_comb;
10312 			ar->hw->wiphy->n_iface_combinations =
10313 				ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
10314 		} else {
10315 			ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
10316 			ar->hw->wiphy->n_iface_combinations =
10317 				ARRAY_SIZE(ath10k_tlv_if_comb);
10318 		}
10319 		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
10320 		break;
10321 	case ATH10K_FW_WMI_OP_VERSION_10_1:
10322 	case ATH10K_FW_WMI_OP_VERSION_10_2:
10323 	case ATH10K_FW_WMI_OP_VERSION_10_2_4:
10324 		ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
10325 		ar->hw->wiphy->n_iface_combinations =
10326 			ARRAY_SIZE(ath10k_10x_if_comb);
10327 		break;
10328 	case ATH10K_FW_WMI_OP_VERSION_10_4:
10329 		ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
10330 		ar->hw->wiphy->n_iface_combinations =
10331 			ARRAY_SIZE(ath10k_10_4_if_comb);
10332 		if (test_bit(WMI_SERVICE_VDEV_DIFFERENT_BEACON_INTERVAL_SUPPORT,
10333 			     ar->wmi.svc_map)) {
10334 			ar->hw->wiphy->iface_combinations =
10335 				ath10k_10_4_bcn_int_if_comb;
10336 			ar->hw->wiphy->n_iface_combinations =
10337 				ARRAY_SIZE(ath10k_10_4_bcn_int_if_comb);
10338 		}
10339 		break;
10340 	case ATH10K_FW_WMI_OP_VERSION_UNSET:
10341 	case ATH10K_FW_WMI_OP_VERSION_MAX:
10342 		WARN_ON(1);
10343 		ret = -EINVAL;
10344 		goto err_free;
10345 	}
10346 
10347 	if (ar->hw_params.dynamic_sar_support)
10348 		ar->hw->wiphy->sar_capa = &ath10k_sar_capa;
10349 
10350 	if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
10351 		ar->hw->netdev_features = NETIF_F_HW_CSUM;
10352 
10353 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) {
10354 		/* Init ath dfs pattern detector */
10355 		ar->ath_common.debug_mask = ATH_DBG_DFS;
10356 		ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
10357 							     NL80211_DFS_UNSET);
10358 
10359 		if (!ar->dfs_detector)
10360 			ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
10361 	}
10362 
10363 	ret = ath10k_mac_init_rd(ar);
10364 	if (ret) {
10365 		ath10k_err(ar, "failed to derive regdom: %d\n", ret);
10366 		goto err_dfs_detector_exit;
10367 	}
10368 
10369 	/* Disable set_coverage_class for chipsets that do not support it. */
10370 	if (!ar->hw_params.hw_ops->set_coverage_class)
10371 		ar->ops->set_coverage_class = NULL;
10372 
10373 	ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
10374 			    ath10k_reg_notifier);
10375 	if (ret) {
10376 		ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
10377 		goto err_dfs_detector_exit;
10378 	}
10379 
10380 	if (test_bit(WMI_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi.svc_map)) {
10381 		ar->hw->wiphy->features |=
10382 			NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
10383 	}
10384 
10385 	ar->hw->wiphy->cipher_suites = cipher_suites;
10386 
10387 	/* QCA988x and QCA6174 family chips do not support CCMP-256, GCMP-128
10388 	 * and GCMP-256 ciphers in hardware. Fetch number of ciphers supported
10389 	 * from chip specific hw_param table.
10390 	 */
10391 	if (!ar->hw_params.n_cipher_suites ||
10392 	    ar->hw_params.n_cipher_suites > ARRAY_SIZE(cipher_suites)) {
10393 		ath10k_err(ar, "invalid hw_params.n_cipher_suites %d\n",
10394 			   ar->hw_params.n_cipher_suites);
10395 		ar->hw_params.n_cipher_suites = 8;
10396 	}
10397 	ar->hw->wiphy->n_cipher_suites = ar->hw_params.n_cipher_suites;
10398 
10399 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
10400 
10401 	ar->hw->weight_multiplier = ATH10K_AIRTIME_WEIGHT_MULTIPLIER;
10402 
10403 	ret = ieee80211_register_hw(ar->hw);
10404 	if (ret) {
10405 		ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
10406 		goto err_dfs_detector_exit;
10407 	}
10408 
10409 	if (test_bit(WMI_SERVICE_PER_PACKET_SW_ENCRYPT, ar->wmi.svc_map)) {
10410 		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
10411 		ar->hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_AP_VLAN);
10412 	}
10413 
10414 	if (!ath_is_world_regd(&ar->ath_common.reg_world_copy) &&
10415 	    !ath_is_world_regd(&ar->ath_common.regulatory)) {
10416 		ret = regulatory_hint(ar->hw->wiphy,
10417 				      ar->ath_common.regulatory.alpha2);
10418 		if (ret)
10419 			goto err_unregister;
10420 	}
10421 
10422 	return 0;
10423 
10424 err_unregister:
10425 	ieee80211_unregister_hw(ar->hw);
10426 
10427 err_dfs_detector_exit:
10428 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
10429 		ar->dfs_detector->exit(ar->dfs_detector);
10430 
10431 err_free:
10432 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
10433 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
10434 
10435 	SET_IEEE80211_DEV(ar->hw, NULL);
10436 	return ret;
10437 }
10438 
10439 void ath10k_mac_unregister(struct ath10k *ar)
10440 {
10441 	ieee80211_unregister_hw(ar->hw);
10442 
10443 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
10444 		ar->dfs_detector->exit(ar->dfs_detector);
10445 
10446 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
10447 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
10448 
10449 	SET_IEEE80211_DEV(ar->hw, NULL);
10450 }
10451