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