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