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