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