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