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