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