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