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