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