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