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