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