xref: /linux/drivers/net/wireless/ath/ath10k/mac.c (revision 65aa371ea52a92dd10826a2ea74bd2c395ee90a8)
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 			arvif->beacon_paddr = (dma_addr_t)arvif->beacon_buf;
5587 		} else {
5588 			arvif->beacon_buf =
5589 				dma_alloc_coherent(ar->dev,
5590 						   IEEE80211_MAX_FRAME_LEN,
5591 						   &arvif->beacon_paddr,
5592 						   GFP_ATOMIC);
5593 		}
5594 		if (!arvif->beacon_buf) {
5595 			ret = -ENOMEM;
5596 			ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
5597 				    ret);
5598 			goto err;
5599 		}
5600 	}
5601 	if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
5602 		arvif->nohwcrypt = true;
5603 
5604 	if (arvif->nohwcrypt &&
5605 	    !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
5606 		ret = -EINVAL;
5607 		ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
5608 		goto err;
5609 	}
5610 
5611 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
5612 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
5613 		   arvif->beacon_buf ? "single-buf" : "per-skb");
5614 
5615 	ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
5616 				     arvif->vdev_subtype, vif->addr);
5617 	if (ret) {
5618 		ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
5619 			    arvif->vdev_id, ret);
5620 		goto err;
5621 	}
5622 
5623 	if (test_bit(WMI_SERVICE_VDEV_DISABLE_4_ADDR_SRC_LRN_SUPPORT,
5624 		     ar->wmi.svc_map)) {
5625 		vdev_param = ar->wmi.vdev_param->disable_4addr_src_lrn;
5626 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5627 						WMI_VDEV_DISABLE_4_ADDR_SRC_LRN);
5628 		if (ret && ret != -EOPNOTSUPP) {
5629 			ath10k_warn(ar, "failed to disable 4addr src lrn vdev %i: %d\n",
5630 				    arvif->vdev_id, ret);
5631 		}
5632 	}
5633 
5634 	ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
5635 	spin_lock_bh(&ar->data_lock);
5636 	list_add(&arvif->list, &ar->arvifs);
5637 	spin_unlock_bh(&ar->data_lock);
5638 
5639 	/* It makes no sense to have firmware do keepalives. mac80211 already
5640 	 * takes care of this with idle connection polling.
5641 	 */
5642 	ret = ath10k_mac_vif_disable_keepalive(arvif);
5643 	if (ret) {
5644 		ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
5645 			    arvif->vdev_id, ret);
5646 		goto err_vdev_delete;
5647 	}
5648 
5649 	arvif->def_wep_key_idx = -1;
5650 
5651 	vdev_param = ar->wmi.vdev_param->tx_encap_type;
5652 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5653 					ATH10K_HW_TXRX_NATIVE_WIFI);
5654 	/* 10.X firmware does not support this VDEV parameter. Do not warn */
5655 	if (ret && ret != -EOPNOTSUPP) {
5656 		ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
5657 			    arvif->vdev_id, ret);
5658 		goto err_vdev_delete;
5659 	}
5660 
5661 	/* Configuring number of spatial stream for monitor interface is causing
5662 	 * target assert in qca9888 and qca6174.
5663 	 */
5664 	if (ar->cfg_tx_chainmask && (vif->type != NL80211_IFTYPE_MONITOR)) {
5665 		u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5666 
5667 		vdev_param = ar->wmi.vdev_param->nss;
5668 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5669 						nss);
5670 		if (ret) {
5671 			ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
5672 				    arvif->vdev_id, ar->cfg_tx_chainmask, nss,
5673 				    ret);
5674 			goto err_vdev_delete;
5675 		}
5676 	}
5677 
5678 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5679 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5680 		ret = ath10k_peer_create(ar, vif, NULL, arvif->vdev_id,
5681 					 vif->addr, WMI_PEER_TYPE_DEFAULT);
5682 		if (ret) {
5683 			ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
5684 				    arvif->vdev_id, ret);
5685 			goto err_vdev_delete;
5686 		}
5687 
5688 		spin_lock_bh(&ar->data_lock);
5689 
5690 		peer = ath10k_peer_find(ar, arvif->vdev_id, vif->addr);
5691 		if (!peer) {
5692 			ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
5693 				    vif->addr, arvif->vdev_id);
5694 			spin_unlock_bh(&ar->data_lock);
5695 			ret = -ENOENT;
5696 			goto err_peer_delete;
5697 		}
5698 
5699 		arvif->peer_id = find_first_bit(peer->peer_ids,
5700 						ATH10K_MAX_NUM_PEER_IDS);
5701 
5702 		spin_unlock_bh(&ar->data_lock);
5703 	} else {
5704 		arvif->peer_id = HTT_INVALID_PEERID;
5705 	}
5706 
5707 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5708 		ret = ath10k_mac_set_kickout(arvif);
5709 		if (ret) {
5710 			ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
5711 				    arvif->vdev_id, ret);
5712 			goto err_peer_delete;
5713 		}
5714 	}
5715 
5716 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
5717 		param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5718 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5719 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5720 						  param, value);
5721 		if (ret) {
5722 			ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
5723 				    arvif->vdev_id, ret);
5724 			goto err_peer_delete;
5725 		}
5726 
5727 		ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
5728 		if (ret) {
5729 			ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
5730 				    arvif->vdev_id, ret);
5731 			goto err_peer_delete;
5732 		}
5733 
5734 		ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
5735 		if (ret) {
5736 			ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
5737 				    arvif->vdev_id, ret);
5738 			goto err_peer_delete;
5739 		}
5740 	}
5741 
5742 	ret = ath10k_mac_set_txbf_conf(arvif);
5743 	if (ret) {
5744 		ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
5745 			    arvif->vdev_id, ret);
5746 		goto err_peer_delete;
5747 	}
5748 
5749 	ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
5750 	if (ret) {
5751 		ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
5752 			    arvif->vdev_id, ret);
5753 		goto err_peer_delete;
5754 	}
5755 
5756 	arvif->txpower = vif->bss_conf.txpower;
5757 	ret = ath10k_mac_txpower_recalc(ar);
5758 	if (ret) {
5759 		ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5760 		goto err_peer_delete;
5761 	}
5762 
5763 	if (test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map)) {
5764 		vdev_param = ar->wmi.vdev_param->rtt_responder_role;
5765 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5766 						arvif->ftm_responder);
5767 
5768 		/* It is harmless to not set FTM role. Do not warn */
5769 		if (ret && ret != -EOPNOTSUPP)
5770 			ath10k_warn(ar, "failed to set vdev %i FTM Responder: %d\n",
5771 				    arvif->vdev_id, ret);
5772 	}
5773 
5774 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5775 		ar->monitor_arvif = arvif;
5776 		ret = ath10k_monitor_recalc(ar);
5777 		if (ret) {
5778 			ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5779 			goto err_peer_delete;
5780 		}
5781 	}
5782 
5783 	spin_lock_bh(&ar->htt.tx_lock);
5784 	if (!ar->tx_paused)
5785 		ieee80211_wake_queue(ar->hw, arvif->vdev_id);
5786 	spin_unlock_bh(&ar->htt.tx_lock);
5787 
5788 	mutex_unlock(&ar->conf_mutex);
5789 	return 0;
5790 
5791 err_peer_delete:
5792 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5793 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5794 		ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
5795 		ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5796 						 vif->addr);
5797 	}
5798 
5799 err_vdev_delete:
5800 	ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5801 	ar->free_vdev_map |= 1LL << arvif->vdev_id;
5802 	spin_lock_bh(&ar->data_lock);
5803 	list_del(&arvif->list);
5804 	spin_unlock_bh(&ar->data_lock);
5805 
5806 err:
5807 	if (arvif->beacon_buf) {
5808 		if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
5809 			kfree(arvif->beacon_buf);
5810 		else
5811 			dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
5812 					  arvif->beacon_buf,
5813 					  arvif->beacon_paddr);
5814 		arvif->beacon_buf = NULL;
5815 	}
5816 
5817 	mutex_unlock(&ar->conf_mutex);
5818 
5819 	return ret;
5820 }
5821 
5822 static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
5823 {
5824 	int i;
5825 
5826 	for (i = 0; i < BITS_PER_LONG; i++)
5827 		ath10k_mac_vif_tx_unlock(arvif, i);
5828 }
5829 
5830 static void ath10k_remove_interface(struct ieee80211_hw *hw,
5831 				    struct ieee80211_vif *vif)
5832 {
5833 	struct ath10k *ar = hw->priv;
5834 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
5835 	struct ath10k_peer *peer;
5836 	unsigned long time_left;
5837 	int ret;
5838 	int i;
5839 
5840 	cancel_work_sync(&arvif->ap_csa_work);
5841 	cancel_delayed_work_sync(&arvif->connection_loss_work);
5842 
5843 	mutex_lock(&ar->conf_mutex);
5844 
5845 	ret = ath10k_spectral_vif_stop(arvif);
5846 	if (ret)
5847 		ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
5848 			    arvif->vdev_id, ret);
5849 
5850 	ar->free_vdev_map |= 1LL << arvif->vdev_id;
5851 	spin_lock_bh(&ar->data_lock);
5852 	list_del(&arvif->list);
5853 	spin_unlock_bh(&ar->data_lock);
5854 
5855 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5856 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5857 		ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
5858 					     vif->addr);
5859 		if (ret)
5860 			ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
5861 				    arvif->vdev_id, ret);
5862 
5863 		ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5864 						 vif->addr);
5865 		kfree(arvif->u.ap.noa_data);
5866 	}
5867 
5868 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
5869 		   arvif->vdev_id);
5870 
5871 	ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5872 	if (ret)
5873 		ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
5874 			    arvif->vdev_id, ret);
5875 
5876 	if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
5877 		time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
5878 							ATH10K_VDEV_DELETE_TIMEOUT_HZ);
5879 		if (time_left == 0) {
5880 			ath10k_warn(ar, "Timeout in receiving vdev delete response\n");
5881 			goto out;
5882 		}
5883 	}
5884 
5885 	/* Some firmware revisions don't notify host about self-peer removal
5886 	 * until after associated vdev is deleted.
5887 	 */
5888 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5889 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5890 		ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
5891 						   vif->addr);
5892 		if (ret)
5893 			ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
5894 				    arvif->vdev_id, ret);
5895 
5896 		spin_lock_bh(&ar->data_lock);
5897 		ar->num_peers--;
5898 		spin_unlock_bh(&ar->data_lock);
5899 	}
5900 
5901 	spin_lock_bh(&ar->data_lock);
5902 	for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
5903 		peer = ar->peer_map[i];
5904 		if (!peer)
5905 			continue;
5906 
5907 		if (peer->vif == vif) {
5908 			ath10k_warn(ar, "found vif peer %pM entry on vdev %i after it was supposedly removed\n",
5909 				    vif->addr, arvif->vdev_id);
5910 			peer->vif = NULL;
5911 		}
5912 	}
5913 
5914 	/* Clean this up late, less opportunity for firmware to access
5915 	 * DMA memory we have deleted.
5916 	 */
5917 	ath10k_mac_vif_beacon_cleanup(arvif);
5918 	spin_unlock_bh(&ar->data_lock);
5919 
5920 	ath10k_peer_cleanup(ar, arvif->vdev_id);
5921 	ath10k_mac_txq_unref(ar, vif->txq);
5922 
5923 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5924 		ar->monitor_arvif = NULL;
5925 		ret = ath10k_monitor_recalc(ar);
5926 		if (ret)
5927 			ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5928 	}
5929 
5930 	ret = ath10k_mac_txpower_recalc(ar);
5931 	if (ret)
5932 		ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5933 
5934 	spin_lock_bh(&ar->htt.tx_lock);
5935 	ath10k_mac_vif_tx_unlock_all(arvif);
5936 	spin_unlock_bh(&ar->htt.tx_lock);
5937 
5938 	ath10k_mac_txq_unref(ar, vif->txq);
5939 
5940 out:
5941 	mutex_unlock(&ar->conf_mutex);
5942 }
5943 
5944 /*
5945  * FIXME: Has to be verified.
5946  */
5947 #define SUPPORTED_FILTERS			\
5948 	(FIF_ALLMULTI |				\
5949 	FIF_CONTROL |				\
5950 	FIF_PSPOLL |				\
5951 	FIF_OTHER_BSS |				\
5952 	FIF_BCN_PRBRESP_PROMISC |		\
5953 	FIF_PROBE_REQ |				\
5954 	FIF_FCSFAIL)
5955 
5956 static void ath10k_configure_filter(struct ieee80211_hw *hw,
5957 				    unsigned int changed_flags,
5958 				    unsigned int *total_flags,
5959 				    u64 multicast)
5960 {
5961 	struct ath10k *ar = hw->priv;
5962 	int ret;
5963 
5964 	mutex_lock(&ar->conf_mutex);
5965 
5966 	changed_flags &= SUPPORTED_FILTERS;
5967 	*total_flags &= SUPPORTED_FILTERS;
5968 	ar->filter_flags = *total_flags;
5969 
5970 	ret = ath10k_monitor_recalc(ar);
5971 	if (ret)
5972 		ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5973 
5974 	mutex_unlock(&ar->conf_mutex);
5975 }
5976 
5977 static void ath10k_recalculate_mgmt_rate(struct ath10k *ar,
5978 					 struct ieee80211_vif *vif,
5979 					 struct cfg80211_chan_def *def)
5980 {
5981 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
5982 	const struct ieee80211_supported_band *sband;
5983 	u8 basic_rate_idx;
5984 	int hw_rate_code;
5985 	u32 vdev_param;
5986 	u16 bitrate;
5987 	int ret;
5988 
5989 	lockdep_assert_held(&ar->conf_mutex);
5990 
5991 	sband = ar->hw->wiphy->bands[def->chan->band];
5992 	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
5993 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
5994 
5995 	hw_rate_code = ath10k_mac_get_rate_hw_value(bitrate);
5996 	if (hw_rate_code < 0) {
5997 		ath10k_warn(ar, "bitrate not supported %d\n", bitrate);
5998 		return;
5999 	}
6000 
6001 	vdev_param = ar->wmi.vdev_param->mgmt_rate;
6002 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6003 					hw_rate_code);
6004 	if (ret)
6005 		ath10k_warn(ar, "failed to set mgmt tx rate %d\n", ret);
6006 }
6007 
6008 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
6009 				    struct ieee80211_vif *vif,
6010 				    struct ieee80211_bss_conf *info,
6011 				    u32 changed)
6012 {
6013 	struct ath10k *ar = hw->priv;
6014 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6015 	struct cfg80211_chan_def def;
6016 	u32 vdev_param, pdev_param, slottime, preamble;
6017 	u16 bitrate, hw_value;
6018 	u8 rate, rateidx;
6019 	int ret = 0, mcast_rate;
6020 	enum nl80211_band band;
6021 
6022 	mutex_lock(&ar->conf_mutex);
6023 
6024 	if (changed & BSS_CHANGED_IBSS)
6025 		ath10k_control_ibss(arvif, info, vif->addr);
6026 
6027 	if (changed & BSS_CHANGED_BEACON_INT) {
6028 		arvif->beacon_interval = info->beacon_int;
6029 		vdev_param = ar->wmi.vdev_param->beacon_interval;
6030 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6031 						arvif->beacon_interval);
6032 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6033 			   "mac vdev %d beacon_interval %d\n",
6034 			   arvif->vdev_id, arvif->beacon_interval);
6035 
6036 		if (ret)
6037 			ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
6038 				    arvif->vdev_id, ret);
6039 	}
6040 
6041 	if (changed & BSS_CHANGED_BEACON) {
6042 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6043 			   "vdev %d set beacon tx mode to staggered\n",
6044 			   arvif->vdev_id);
6045 
6046 		pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
6047 		ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
6048 						WMI_BEACON_STAGGERED_MODE);
6049 		if (ret)
6050 			ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
6051 				    arvif->vdev_id, ret);
6052 
6053 		ret = ath10k_mac_setup_bcn_tmpl(arvif);
6054 		if (ret)
6055 			ath10k_warn(ar, "failed to update beacon template: %d\n",
6056 				    ret);
6057 
6058 		if (ieee80211_vif_is_mesh(vif)) {
6059 			/* mesh doesn't use SSID but firmware needs it */
6060 			strncpy(arvif->u.ap.ssid, "mesh",
6061 				sizeof(arvif->u.ap.ssid));
6062 			arvif->u.ap.ssid_len = 4;
6063 		}
6064 	}
6065 
6066 	if (changed & BSS_CHANGED_AP_PROBE_RESP) {
6067 		ret = ath10k_mac_setup_prb_tmpl(arvif);
6068 		if (ret)
6069 			ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
6070 				    arvif->vdev_id, ret);
6071 	}
6072 
6073 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
6074 		arvif->dtim_period = info->dtim_period;
6075 
6076 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6077 			   "mac vdev %d dtim_period %d\n",
6078 			   arvif->vdev_id, arvif->dtim_period);
6079 
6080 		vdev_param = ar->wmi.vdev_param->dtim_period;
6081 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6082 						arvif->dtim_period);
6083 		if (ret)
6084 			ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
6085 				    arvif->vdev_id, ret);
6086 	}
6087 
6088 	if (changed & BSS_CHANGED_SSID &&
6089 	    vif->type == NL80211_IFTYPE_AP) {
6090 		arvif->u.ap.ssid_len = info->ssid_len;
6091 		if (info->ssid_len)
6092 			memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
6093 		arvif->u.ap.hidden_ssid = info->hidden_ssid;
6094 	}
6095 
6096 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
6097 		ether_addr_copy(arvif->bssid, info->bssid);
6098 
6099 	if (changed & BSS_CHANGED_FTM_RESPONDER &&
6100 	    arvif->ftm_responder != info->ftm_responder &&
6101 	    test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map)) {
6102 		arvif->ftm_responder = info->ftm_responder;
6103 
6104 		vdev_param = ar->wmi.vdev_param->rtt_responder_role;
6105 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6106 						arvif->ftm_responder);
6107 
6108 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6109 			   "mac vdev %d ftm_responder %d:ret %d\n",
6110 			   arvif->vdev_id, arvif->ftm_responder, ret);
6111 	}
6112 
6113 	if (changed & BSS_CHANGED_BEACON_ENABLED)
6114 		ath10k_control_beaconing(arvif, info);
6115 
6116 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
6117 		arvif->use_cts_prot = info->use_cts_prot;
6118 
6119 		ret = ath10k_recalc_rtscts_prot(arvif);
6120 		if (ret)
6121 			ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
6122 				    arvif->vdev_id, ret);
6123 
6124 		if (ath10k_mac_can_set_cts_prot(arvif)) {
6125 			ret = ath10k_mac_set_cts_prot(arvif);
6126 			if (ret)
6127 				ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
6128 					    arvif->vdev_id, ret);
6129 		}
6130 	}
6131 
6132 	if (changed & BSS_CHANGED_ERP_SLOT) {
6133 		if (info->use_short_slot)
6134 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
6135 
6136 		else
6137 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
6138 
6139 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
6140 			   arvif->vdev_id, slottime);
6141 
6142 		vdev_param = ar->wmi.vdev_param->slot_time;
6143 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6144 						slottime);
6145 		if (ret)
6146 			ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
6147 				    arvif->vdev_id, ret);
6148 	}
6149 
6150 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
6151 		if (info->use_short_preamble)
6152 			preamble = WMI_VDEV_PREAMBLE_SHORT;
6153 		else
6154 			preamble = WMI_VDEV_PREAMBLE_LONG;
6155 
6156 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6157 			   "mac vdev %d preamble %dn",
6158 			   arvif->vdev_id, preamble);
6159 
6160 		vdev_param = ar->wmi.vdev_param->preamble;
6161 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6162 						preamble);
6163 		if (ret)
6164 			ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
6165 				    arvif->vdev_id, ret);
6166 	}
6167 
6168 	if (changed & BSS_CHANGED_ASSOC) {
6169 		if (info->assoc) {
6170 			/* Workaround: Make sure monitor vdev is not running
6171 			 * when associating to prevent some firmware revisions
6172 			 * (e.g. 10.1 and 10.2) from crashing.
6173 			 */
6174 			if (ar->monitor_started)
6175 				ath10k_monitor_stop(ar);
6176 			ath10k_bss_assoc(hw, vif, info);
6177 			ath10k_monitor_recalc(ar);
6178 		} else {
6179 			ath10k_bss_disassoc(hw, vif);
6180 		}
6181 	}
6182 
6183 	if (changed & BSS_CHANGED_TXPOWER) {
6184 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
6185 			   arvif->vdev_id, info->txpower);
6186 
6187 		arvif->txpower = info->txpower;
6188 		ret = ath10k_mac_txpower_recalc(ar);
6189 		if (ret)
6190 			ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
6191 	}
6192 
6193 	if (changed & BSS_CHANGED_PS) {
6194 		arvif->ps = vif->bss_conf.ps;
6195 
6196 		ret = ath10k_config_ps(ar);
6197 		if (ret)
6198 			ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
6199 				    arvif->vdev_id, ret);
6200 	}
6201 
6202 	if (changed & BSS_CHANGED_MCAST_RATE &&
6203 	    !ath10k_mac_vif_chan(arvif->vif, &def)) {
6204 		band = def.chan->band;
6205 		mcast_rate = vif->bss_conf.mcast_rate[band];
6206 		if (mcast_rate > 0)
6207 			rateidx = mcast_rate - 1;
6208 		else
6209 			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
6210 
6211 		if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
6212 			rateidx += ATH10K_MAC_FIRST_OFDM_RATE_IDX;
6213 
6214 		bitrate = ath10k_wmi_legacy_rates[rateidx].bitrate;
6215 		hw_value = ath10k_wmi_legacy_rates[rateidx].hw_value;
6216 		if (ath10k_mac_bitrate_is_cck(bitrate))
6217 			preamble = WMI_RATE_PREAMBLE_CCK;
6218 		else
6219 			preamble = WMI_RATE_PREAMBLE_OFDM;
6220 
6221 		rate = ATH10K_HW_RATECODE(hw_value, 0, preamble);
6222 
6223 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6224 			   "mac vdev %d mcast_rate %x\n",
6225 			   arvif->vdev_id, rate);
6226 
6227 		vdev_param = ar->wmi.vdev_param->mcast_data_rate;
6228 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
6229 						vdev_param, rate);
6230 		if (ret)
6231 			ath10k_warn(ar,
6232 				    "failed to set mcast rate on vdev %i: %d\n",
6233 				    arvif->vdev_id,  ret);
6234 
6235 		vdev_param = ar->wmi.vdev_param->bcast_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 bcast rate on vdev %i: %d\n",
6241 				    arvif->vdev_id,  ret);
6242 	}
6243 
6244 	if (changed & BSS_CHANGED_BASIC_RATES &&
6245 	    !ath10k_mac_vif_chan(arvif->vif, &def))
6246 		ath10k_recalculate_mgmt_rate(ar, vif, &def);
6247 
6248 	mutex_unlock(&ar->conf_mutex);
6249 }
6250 
6251 static void ath10k_mac_op_set_coverage_class(struct ieee80211_hw *hw, s16 value)
6252 {
6253 	struct ath10k *ar = hw->priv;
6254 
6255 	/* This function should never be called if setting the coverage class
6256 	 * is not supported on this hardware.
6257 	 */
6258 	if (!ar->hw_params.hw_ops->set_coverage_class) {
6259 		WARN_ON_ONCE(1);
6260 		return;
6261 	}
6262 	ar->hw_params.hw_ops->set_coverage_class(ar, value);
6263 }
6264 
6265 struct ath10k_mac_tdls_iter_data {
6266 	u32 num_tdls_stations;
6267 	struct ieee80211_vif *curr_vif;
6268 };
6269 
6270 static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
6271 						    struct ieee80211_sta *sta)
6272 {
6273 	struct ath10k_mac_tdls_iter_data *iter_data = data;
6274 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6275 	struct ieee80211_vif *sta_vif = arsta->arvif->vif;
6276 
6277 	if (sta->tdls && sta_vif == iter_data->curr_vif)
6278 		iter_data->num_tdls_stations++;
6279 }
6280 
6281 static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
6282 					      struct ieee80211_vif *vif)
6283 {
6284 	struct ath10k_mac_tdls_iter_data data = {};
6285 
6286 	data.curr_vif = vif;
6287 
6288 	ieee80211_iterate_stations_atomic(hw,
6289 					  ath10k_mac_tdls_vif_stations_count_iter,
6290 					  &data);
6291 	return data.num_tdls_stations;
6292 }
6293 
6294 static int ath10k_hw_scan(struct ieee80211_hw *hw,
6295 			  struct ieee80211_vif *vif,
6296 			  struct ieee80211_scan_request *hw_req)
6297 {
6298 	struct ath10k *ar = hw->priv;
6299 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6300 	struct cfg80211_scan_request *req = &hw_req->req;
6301 	struct wmi_start_scan_arg arg;
6302 	int ret = 0;
6303 	int i;
6304 	u32 scan_timeout;
6305 
6306 	mutex_lock(&ar->conf_mutex);
6307 
6308 	if (ath10k_mac_tdls_vif_stations_count(hw, vif) > 0) {
6309 		ret = -EBUSY;
6310 		goto exit;
6311 	}
6312 
6313 	spin_lock_bh(&ar->data_lock);
6314 	switch (ar->scan.state) {
6315 	case ATH10K_SCAN_IDLE:
6316 		reinit_completion(&ar->scan.started);
6317 		reinit_completion(&ar->scan.completed);
6318 		ar->scan.state = ATH10K_SCAN_STARTING;
6319 		ar->scan.is_roc = false;
6320 		ar->scan.vdev_id = arvif->vdev_id;
6321 		ret = 0;
6322 		break;
6323 	case ATH10K_SCAN_STARTING:
6324 	case ATH10K_SCAN_RUNNING:
6325 	case ATH10K_SCAN_ABORTING:
6326 		ret = -EBUSY;
6327 		break;
6328 	}
6329 	spin_unlock_bh(&ar->data_lock);
6330 
6331 	if (ret)
6332 		goto exit;
6333 
6334 	memset(&arg, 0, sizeof(arg));
6335 	ath10k_wmi_start_scan_init(ar, &arg);
6336 	arg.vdev_id = arvif->vdev_id;
6337 	arg.scan_id = ATH10K_SCAN_ID;
6338 
6339 	if (req->ie_len) {
6340 		arg.ie_len = req->ie_len;
6341 		memcpy(arg.ie, req->ie, arg.ie_len);
6342 	}
6343 
6344 	if (req->n_ssids) {
6345 		arg.n_ssids = req->n_ssids;
6346 		for (i = 0; i < arg.n_ssids; i++) {
6347 			arg.ssids[i].len  = req->ssids[i].ssid_len;
6348 			arg.ssids[i].ssid = req->ssids[i].ssid;
6349 		}
6350 	} else {
6351 		arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
6352 	}
6353 
6354 	if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6355 		arg.scan_ctrl_flags |=  WMI_SCAN_ADD_SPOOFED_MAC_IN_PROBE_REQ;
6356 		ether_addr_copy(arg.mac_addr.addr, req->mac_addr);
6357 		ether_addr_copy(arg.mac_mask.addr, req->mac_addr_mask);
6358 	}
6359 
6360 	if (req->n_channels) {
6361 		arg.n_channels = req->n_channels;
6362 		for (i = 0; i < arg.n_channels; i++)
6363 			arg.channels[i] = req->channels[i]->center_freq;
6364 	}
6365 
6366 	/* if duration is set, default dwell times will be overwritten */
6367 	if (req->duration) {
6368 		arg.dwell_time_active = req->duration;
6369 		arg.dwell_time_passive = req->duration;
6370 		arg.burst_duration_ms = req->duration;
6371 
6372 		scan_timeout = min_t(u32, arg.max_rest_time *
6373 				(arg.n_channels - 1) + (req->duration +
6374 				ATH10K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD) *
6375 				arg.n_channels, arg.max_scan_time + 200);
6376 
6377 	} else {
6378 		/* Add a 200ms margin to account for event/command processing */
6379 		scan_timeout = arg.max_scan_time + 200;
6380 	}
6381 
6382 	ret = ath10k_start_scan(ar, &arg);
6383 	if (ret) {
6384 		ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
6385 		spin_lock_bh(&ar->data_lock);
6386 		ar->scan.state = ATH10K_SCAN_IDLE;
6387 		spin_unlock_bh(&ar->data_lock);
6388 	}
6389 
6390 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
6391 				     msecs_to_jiffies(scan_timeout));
6392 
6393 exit:
6394 	mutex_unlock(&ar->conf_mutex);
6395 	return ret;
6396 }
6397 
6398 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
6399 				  struct ieee80211_vif *vif)
6400 {
6401 	struct ath10k *ar = hw->priv;
6402 
6403 	mutex_lock(&ar->conf_mutex);
6404 	ath10k_scan_abort(ar);
6405 	mutex_unlock(&ar->conf_mutex);
6406 
6407 	cancel_delayed_work_sync(&ar->scan.timeout);
6408 }
6409 
6410 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
6411 					struct ath10k_vif *arvif,
6412 					enum set_key_cmd cmd,
6413 					struct ieee80211_key_conf *key)
6414 {
6415 	u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
6416 	int ret;
6417 
6418 	/* 10.1 firmware branch requires default key index to be set to group
6419 	 * key index after installing it. Otherwise FW/HW Txes corrupted
6420 	 * frames with multi-vif APs. This is not required for main firmware
6421 	 * branch (e.g. 636).
6422 	 *
6423 	 * This is also needed for 636 fw for IBSS-RSN to work more reliably.
6424 	 *
6425 	 * FIXME: It remains unknown if this is required for multi-vif STA
6426 	 * interfaces on 10.1.
6427 	 */
6428 
6429 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6430 	    arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
6431 		return;
6432 
6433 	if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
6434 		return;
6435 
6436 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
6437 		return;
6438 
6439 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6440 		return;
6441 
6442 	if (cmd != SET_KEY)
6443 		return;
6444 
6445 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6446 					key->keyidx);
6447 	if (ret)
6448 		ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
6449 			    arvif->vdev_id, ret);
6450 }
6451 
6452 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6453 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6454 			  struct ieee80211_key_conf *key)
6455 {
6456 	struct ath10k *ar = hw->priv;
6457 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6458 	struct ath10k_sta *arsta;
6459 	struct ath10k_peer *peer;
6460 	const u8 *peer_addr;
6461 	bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
6462 		      key->cipher == WLAN_CIPHER_SUITE_WEP104;
6463 	int ret = 0;
6464 	int ret2;
6465 	u32 flags = 0;
6466 	u32 flags2;
6467 
6468 	/* this one needs to be done in software */
6469 	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
6470 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
6471 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
6472 	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
6473 		return 1;
6474 
6475 	if (arvif->nohwcrypt)
6476 		return 1;
6477 
6478 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6479 		return -ENOSPC;
6480 
6481 	mutex_lock(&ar->conf_mutex);
6482 
6483 	if (sta) {
6484 		arsta = (struct ath10k_sta *)sta->drv_priv;
6485 		peer_addr = sta->addr;
6486 		spin_lock_bh(&ar->data_lock);
6487 		arsta->ucast_cipher = key->cipher;
6488 		spin_unlock_bh(&ar->data_lock);
6489 	} else if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
6490 		peer_addr = vif->bss_conf.bssid;
6491 	} else {
6492 		peer_addr = vif->addr;
6493 	}
6494 
6495 	key->hw_key_idx = key->keyidx;
6496 
6497 	if (is_wep) {
6498 		if (cmd == SET_KEY)
6499 			arvif->wep_keys[key->keyidx] = key;
6500 		else
6501 			arvif->wep_keys[key->keyidx] = NULL;
6502 	}
6503 
6504 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6505 	 * we already hold conf_mutex. we just make sure its there now.
6506 	 */
6507 	spin_lock_bh(&ar->data_lock);
6508 	peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
6509 	spin_unlock_bh(&ar->data_lock);
6510 
6511 	if (!peer) {
6512 		if (cmd == SET_KEY) {
6513 			ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
6514 				    peer_addr);
6515 			ret = -EOPNOTSUPP;
6516 			goto exit;
6517 		} else {
6518 			/* if the peer doesn't exist there is no key to disable anymore */
6519 			goto exit;
6520 		}
6521 	}
6522 
6523 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6524 		flags |= WMI_KEY_PAIRWISE;
6525 	else
6526 		flags |= WMI_KEY_GROUP;
6527 
6528 	if (is_wep) {
6529 		if (cmd == DISABLE_KEY)
6530 			ath10k_clear_vdev_key(arvif, key);
6531 
6532 		/* When WEP keys are uploaded it's possible that there are
6533 		 * stations associated already (e.g. when merging) without any
6534 		 * keys. Static WEP needs an explicit per-peer key upload.
6535 		 */
6536 		if (vif->type == NL80211_IFTYPE_ADHOC &&
6537 		    cmd == SET_KEY)
6538 			ath10k_mac_vif_update_wep_key(arvif, key);
6539 
6540 		/* 802.1x never sets the def_wep_key_idx so each set_key()
6541 		 * call changes default tx key.
6542 		 *
6543 		 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
6544 		 * after first set_key().
6545 		 */
6546 		if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
6547 			flags |= WMI_KEY_TX_USAGE;
6548 	}
6549 
6550 	ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
6551 	if (ret) {
6552 		WARN_ON(ret > 0);
6553 		ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
6554 			    arvif->vdev_id, peer_addr, ret);
6555 		goto exit;
6556 	}
6557 
6558 	/* mac80211 sets static WEP keys as groupwise while firmware requires
6559 	 * them to be installed twice as both pairwise and groupwise.
6560 	 */
6561 	if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
6562 		flags2 = flags;
6563 		flags2 &= ~WMI_KEY_GROUP;
6564 		flags2 |= WMI_KEY_PAIRWISE;
6565 
6566 		ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
6567 		if (ret) {
6568 			WARN_ON(ret > 0);
6569 			ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
6570 				    arvif->vdev_id, peer_addr, ret);
6571 			ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
6572 						  peer_addr, flags);
6573 			if (ret2) {
6574 				WARN_ON(ret2 > 0);
6575 				ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
6576 					    arvif->vdev_id, peer_addr, ret2);
6577 			}
6578 			goto exit;
6579 		}
6580 	}
6581 
6582 	ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
6583 
6584 	spin_lock_bh(&ar->data_lock);
6585 	peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
6586 	if (peer && cmd == SET_KEY)
6587 		peer->keys[key->keyidx] = key;
6588 	else if (peer && cmd == DISABLE_KEY)
6589 		peer->keys[key->keyidx] = NULL;
6590 	else if (peer == NULL)
6591 		/* impossible unless FW goes crazy */
6592 		ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
6593 	spin_unlock_bh(&ar->data_lock);
6594 
6595 	if (sta && sta->tdls)
6596 		ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6597 					  ar->wmi.peer_param->authorize, 1);
6598 	else if (sta && cmd == SET_KEY && (key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
6599 		ath10k_wmi_peer_set_param(ar, arvif->vdev_id, peer_addr,
6600 					  ar->wmi.peer_param->authorize, 1);
6601 
6602 exit:
6603 	mutex_unlock(&ar->conf_mutex);
6604 	return ret;
6605 }
6606 
6607 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
6608 					   struct ieee80211_vif *vif,
6609 					   int keyidx)
6610 {
6611 	struct ath10k *ar = hw->priv;
6612 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6613 	int ret;
6614 
6615 	mutex_lock(&arvif->ar->conf_mutex);
6616 
6617 	if (arvif->ar->state != ATH10K_STATE_ON)
6618 		goto unlock;
6619 
6620 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
6621 		   arvif->vdev_id, keyidx);
6622 
6623 	ret = ath10k_wmi_vdev_set_param(arvif->ar,
6624 					arvif->vdev_id,
6625 					arvif->ar->wmi.vdev_param->def_keyid,
6626 					keyidx);
6627 
6628 	if (ret) {
6629 		ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
6630 			    arvif->vdev_id,
6631 			    ret);
6632 		goto unlock;
6633 	}
6634 
6635 	arvif->def_wep_key_idx = keyidx;
6636 
6637 unlock:
6638 	mutex_unlock(&arvif->ar->conf_mutex);
6639 }
6640 
6641 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
6642 {
6643 	struct ath10k *ar;
6644 	struct ath10k_vif *arvif;
6645 	struct ath10k_sta *arsta;
6646 	struct ieee80211_sta *sta;
6647 	struct cfg80211_chan_def def;
6648 	enum nl80211_band band;
6649 	const u8 *ht_mcs_mask;
6650 	const u16 *vht_mcs_mask;
6651 	u32 changed, bw, nss, smps;
6652 	int err;
6653 
6654 	arsta = container_of(wk, struct ath10k_sta, update_wk);
6655 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
6656 	arvif = arsta->arvif;
6657 	ar = arvif->ar;
6658 
6659 	if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
6660 		return;
6661 
6662 	band = def.chan->band;
6663 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6664 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6665 
6666 	spin_lock_bh(&ar->data_lock);
6667 
6668 	changed = arsta->changed;
6669 	arsta->changed = 0;
6670 
6671 	bw = arsta->bw;
6672 	nss = arsta->nss;
6673 	smps = arsta->smps;
6674 
6675 	spin_unlock_bh(&ar->data_lock);
6676 
6677 	mutex_lock(&ar->conf_mutex);
6678 
6679 	nss = max_t(u32, 1, nss);
6680 	nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
6681 			   ath10k_mac_max_vht_nss(vht_mcs_mask)));
6682 
6683 	if (changed & IEEE80211_RC_BW_CHANGED) {
6684 		enum wmi_phy_mode mode;
6685 
6686 		mode = chan_to_phymode(&def);
6687 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM peer bw %d phymode %d\n",
6688 			   sta->addr, bw, mode);
6689 
6690 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6691 						ar->wmi.peer_param->phymode, mode);
6692 		if (err) {
6693 			ath10k_warn(ar, "failed to update STA %pM peer phymode %d: %d\n",
6694 				    sta->addr, mode, err);
6695 			goto exit;
6696 		}
6697 
6698 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6699 						ar->wmi.peer_param->chan_width, bw);
6700 		if (err)
6701 			ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
6702 				    sta->addr, bw, err);
6703 	}
6704 
6705 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6706 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM nss %d\n",
6707 			   sta->addr, nss);
6708 
6709 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6710 						ar->wmi.peer_param->nss, nss);
6711 		if (err)
6712 			ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
6713 				    sta->addr, nss, err);
6714 	}
6715 
6716 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6717 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM smps %d\n",
6718 			   sta->addr, smps);
6719 
6720 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6721 						ar->wmi.peer_param->smps_state, smps);
6722 		if (err)
6723 			ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
6724 				    sta->addr, smps, err);
6725 	}
6726 
6727 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6728 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM supp rates\n",
6729 			   sta->addr);
6730 
6731 		err = ath10k_station_assoc(ar, arvif->vif, sta, true);
6732 		if (err)
6733 			ath10k_warn(ar, "failed to reassociate station: %pM\n",
6734 				    sta->addr);
6735 	}
6736 
6737 exit:
6738 	mutex_unlock(&ar->conf_mutex);
6739 }
6740 
6741 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
6742 				       struct ieee80211_sta *sta)
6743 {
6744 	struct ath10k *ar = arvif->ar;
6745 
6746 	lockdep_assert_held(&ar->conf_mutex);
6747 
6748 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6749 		return 0;
6750 
6751 	if (ar->num_stations >= ar->max_num_stations)
6752 		return -ENOBUFS;
6753 
6754 	ar->num_stations++;
6755 
6756 	return 0;
6757 }
6758 
6759 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
6760 					struct ieee80211_sta *sta)
6761 {
6762 	struct ath10k *ar = arvif->ar;
6763 
6764 	lockdep_assert_held(&ar->conf_mutex);
6765 
6766 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6767 		return;
6768 
6769 	ar->num_stations--;
6770 }
6771 
6772 static int ath10k_sta_set_txpwr(struct ieee80211_hw *hw,
6773 				struct ieee80211_vif *vif,
6774 				struct ieee80211_sta *sta)
6775 {
6776 	struct ath10k *ar = hw->priv;
6777 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6778 	int ret = 0;
6779 	s16 txpwr;
6780 
6781 	if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
6782 		txpwr = 0;
6783 	} else {
6784 		txpwr = sta->txpwr.power;
6785 		if (!txpwr)
6786 			return -EINVAL;
6787 	}
6788 
6789 	if (txpwr > ATH10K_TX_POWER_MAX_VAL || txpwr < ATH10K_TX_POWER_MIN_VAL)
6790 		return -EINVAL;
6791 
6792 	mutex_lock(&ar->conf_mutex);
6793 
6794 	ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6795 					ar->wmi.peer_param->use_fixed_power, txpwr);
6796 	if (ret) {
6797 		ath10k_warn(ar, "failed to set tx power for station ret: %d\n",
6798 			    ret);
6799 		goto out;
6800 	}
6801 
6802 out:
6803 	mutex_unlock(&ar->conf_mutex);
6804 	return ret;
6805 }
6806 
6807 struct ath10k_mac_iter_tid_conf_data {
6808 	struct ieee80211_vif *curr_vif;
6809 	struct ath10k *ar;
6810 	bool reset_config;
6811 };
6812 
6813 static bool
6814 ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
6815 					enum nl80211_band band,
6816 					const struct cfg80211_bitrate_mask *mask,
6817 					int *vht_num_rates)
6818 {
6819 	int num_rates = 0;
6820 	int i, tmp;
6821 
6822 	num_rates += hweight32(mask->control[band].legacy);
6823 
6824 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6825 		num_rates += hweight8(mask->control[band].ht_mcs[i]);
6826 
6827 	*vht_num_rates = 0;
6828 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6829 		tmp = hweight16(mask->control[band].vht_mcs[i]);
6830 		num_rates += tmp;
6831 		*vht_num_rates += tmp;
6832 	}
6833 
6834 	return num_rates == 1;
6835 }
6836 
6837 static int
6838 ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
6839 					enum nl80211_band band,
6840 					const struct cfg80211_bitrate_mask *mask,
6841 					u8 *rate, u8 *nss, bool vht_only)
6842 {
6843 	int rate_idx;
6844 	int i;
6845 	u16 bitrate;
6846 	u8 preamble;
6847 	u8 hw_rate;
6848 
6849 	if (vht_only)
6850 		goto next;
6851 
6852 	if (hweight32(mask->control[band].legacy) == 1) {
6853 		rate_idx = ffs(mask->control[band].legacy) - 1;
6854 
6855 		if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
6856 			rate_idx += ATH10K_MAC_FIRST_OFDM_RATE_IDX;
6857 
6858 		hw_rate = ath10k_wmi_legacy_rates[rate_idx].hw_value;
6859 		bitrate = ath10k_wmi_legacy_rates[rate_idx].bitrate;
6860 
6861 		if (ath10k_mac_bitrate_is_cck(bitrate))
6862 			preamble = WMI_RATE_PREAMBLE_CCK;
6863 		else
6864 			preamble = WMI_RATE_PREAMBLE_OFDM;
6865 
6866 		*nss = 1;
6867 		*rate = preamble << 6 |
6868 			(*nss - 1) << 4 |
6869 			hw_rate << 0;
6870 
6871 		return 0;
6872 	}
6873 
6874 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6875 		if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
6876 			*nss = i + 1;
6877 			*rate = WMI_RATE_PREAMBLE_HT << 6 |
6878 				(*nss - 1) << 4 |
6879 				(ffs(mask->control[band].ht_mcs[i]) - 1);
6880 
6881 			return 0;
6882 		}
6883 	}
6884 
6885 next:
6886 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6887 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6888 			*nss = i + 1;
6889 			*rate = WMI_RATE_PREAMBLE_VHT << 6 |
6890 				(*nss - 1) << 4 |
6891 				(ffs(mask->control[band].vht_mcs[i]) - 1);
6892 
6893 			return 0;
6894 		}
6895 	}
6896 
6897 	return -EINVAL;
6898 }
6899 
6900 static int ath10k_mac_validate_rate_mask(struct ath10k *ar,
6901 					 struct ieee80211_sta *sta,
6902 					 u32 rate_ctrl_flag, u8 nss)
6903 {
6904 	if (nss > sta->rx_nss) {
6905 		ath10k_warn(ar, "Invalid nss field, configured %u limit %u\n",
6906 			    nss, sta->rx_nss);
6907 		return -EINVAL;
6908 	}
6909 
6910 	if (ATH10K_HW_PREAMBLE(rate_ctrl_flag) == WMI_RATE_PREAMBLE_VHT) {
6911 		if (!sta->vht_cap.vht_supported) {
6912 			ath10k_warn(ar, "Invalid VHT rate for sta %pM\n",
6913 				    sta->addr);
6914 			return -EINVAL;
6915 		}
6916 	} else if (ATH10K_HW_PREAMBLE(rate_ctrl_flag) == WMI_RATE_PREAMBLE_HT) {
6917 		if (!sta->ht_cap.ht_supported || sta->vht_cap.vht_supported) {
6918 			ath10k_warn(ar, "Invalid HT rate for sta %pM\n",
6919 				    sta->addr);
6920 			return -EINVAL;
6921 		}
6922 	} else {
6923 		if (sta->ht_cap.ht_supported || sta->vht_cap.vht_supported)
6924 			return -EINVAL;
6925 	}
6926 
6927 	return 0;
6928 }
6929 
6930 static int
6931 ath10k_mac_tid_bitrate_config(struct ath10k *ar,
6932 			      struct ieee80211_vif *vif,
6933 			      struct ieee80211_sta *sta,
6934 			      u32 *rate_ctrl_flag, u8 *rate_ctrl,
6935 			      enum nl80211_tx_rate_setting txrate_type,
6936 			      const struct cfg80211_bitrate_mask *mask)
6937 {
6938 	struct cfg80211_chan_def def;
6939 	enum nl80211_band band;
6940 	u8 nss, rate;
6941 	int vht_num_rates, ret;
6942 
6943 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
6944 		return -EINVAL;
6945 
6946 	if (txrate_type == NL80211_TX_RATE_AUTOMATIC) {
6947 		*rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_AUTO;
6948 		*rate_ctrl_flag = 0;
6949 		return 0;
6950 	}
6951 
6952 	band = def.chan->band;
6953 
6954 	if (!ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask,
6955 						     &vht_num_rates)) {
6956 		return -EINVAL;
6957 	}
6958 
6959 	ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
6960 						      &rate, &nss, false);
6961 	if (ret) {
6962 		ath10k_warn(ar, "failed to get single rate: %d\n",
6963 			    ret);
6964 		return ret;
6965 	}
6966 
6967 	*rate_ctrl_flag = rate;
6968 
6969 	if (sta && ath10k_mac_validate_rate_mask(ar, sta, *rate_ctrl_flag, nss))
6970 		return -EINVAL;
6971 
6972 	if (txrate_type == NL80211_TX_RATE_FIXED)
6973 		*rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_FIXED_RATE;
6974 	else if (txrate_type == NL80211_TX_RATE_LIMITED &&
6975 		 (test_bit(WMI_SERVICE_EXT_PEER_TID_CONFIGS_SUPPORT,
6976 			   ar->wmi.svc_map)))
6977 		*rate_ctrl = WMI_PEER_TID_CONFIG_RATE_UPPER_CAP;
6978 	else
6979 		return -EOPNOTSUPP;
6980 
6981 	return 0;
6982 }
6983 
6984 static int ath10k_mac_set_tid_config(struct ath10k *ar, struct ieee80211_sta *sta,
6985 				     struct ieee80211_vif *vif, u32 changed,
6986 				     struct wmi_per_peer_per_tid_cfg_arg *arg)
6987 {
6988 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6989 	struct ath10k_sta *arsta;
6990 	int ret;
6991 
6992 	if (sta) {
6993 		if (!sta->wme)
6994 			return -ENOTSUPP;
6995 
6996 		arsta = (struct ath10k_sta *)sta->drv_priv;
6997 
6998 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
6999 			if ((arsta->retry_long[arg->tid] > 0 ||
7000 			     arsta->rate_code[arg->tid] > 0 ||
7001 			     arsta->ampdu[arg->tid] ==
7002 					WMI_TID_CONFIG_AGGR_CONTROL_ENABLE) &&
7003 			     arg->ack_policy == WMI_PEER_TID_CONFIG_NOACK) {
7004 				changed &= ~BIT(NL80211_TID_CONFIG_ATTR_NOACK);
7005 				arg->ack_policy = 0;
7006 				arg->aggr_control = 0;
7007 				arg->rate_ctrl = 0;
7008 				arg->rcode_flags = 0;
7009 			}
7010 		}
7011 
7012 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7013 			if (arsta->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK ||
7014 			    arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7015 				arg->aggr_control = 0;
7016 				changed &= ~BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
7017 			}
7018 		}
7019 
7020 		if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7021 		    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7022 			if (arsta->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK ||
7023 			    arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7024 				arg->rate_ctrl = 0;
7025 				arg->rcode_flags = 0;
7026 			}
7027 		}
7028 
7029 		ether_addr_copy(arg->peer_macaddr.addr, sta->addr);
7030 
7031 		ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, arg);
7032 		if (ret)
7033 			return ret;
7034 
7035 		/* Store the configured parameters in success case */
7036 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7037 			arsta->noack[arg->tid] = arg->ack_policy;
7038 			arg->ack_policy = 0;
7039 			arg->aggr_control = 0;
7040 			arg->rate_ctrl = 0;
7041 			arg->rcode_flags = 0;
7042 		}
7043 
7044 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7045 			arsta->retry_long[arg->tid] = arg->retry_count;
7046 			arg->retry_count = 0;
7047 		}
7048 
7049 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7050 			arsta->ampdu[arg->tid] = arg->aggr_control;
7051 			arg->aggr_control = 0;
7052 		}
7053 
7054 		if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7055 		    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7056 			arsta->rate_ctrl[arg->tid] = arg->rate_ctrl;
7057 			arg->rate_ctrl = 0;
7058 			arg->rcode_flags = 0;
7059 		}
7060 
7061 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7062 			arsta->rtscts[arg->tid] = arg->rtscts_ctrl;
7063 			arg->ext_tid_cfg_bitmap = 0;
7064 		}
7065 	} else {
7066 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7067 			if ((arvif->retry_long[arg->tid] ||
7068 			     arvif->rate_code[arg->tid] ||
7069 			     arvif->ampdu[arg->tid] ==
7070 					WMI_TID_CONFIG_AGGR_CONTROL_ENABLE) &&
7071 			     arg->ack_policy == WMI_PEER_TID_CONFIG_NOACK) {
7072 				changed &= ~BIT(NL80211_TID_CONFIG_ATTR_NOACK);
7073 			} else {
7074 				arvif->noack[arg->tid] = arg->ack_policy;
7075 				arvif->ampdu[arg->tid] = arg->aggr_control;
7076 				arvif->rate_ctrl[arg->tid] = arg->rate_ctrl;
7077 			}
7078 		}
7079 
7080 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7081 			if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK)
7082 				changed &= ~BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
7083 			else
7084 				arvif->retry_long[arg->tid] = arg->retry_count;
7085 		}
7086 
7087 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7088 			if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK)
7089 				changed &= ~BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
7090 			else
7091 				arvif->ampdu[arg->tid] = arg->aggr_control;
7092 		}
7093 
7094 		if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7095 		    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7096 			if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7097 				changed &= ~(BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7098 					     BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE));
7099 			} else {
7100 				arvif->rate_ctrl[arg->tid] = arg->rate_ctrl;
7101 				arvif->rate_code[arg->tid] = arg->rcode_flags;
7102 			}
7103 		}
7104 
7105 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7106 			arvif->rtscts[arg->tid] = arg->rtscts_ctrl;
7107 			arg->ext_tid_cfg_bitmap = 0;
7108 		}
7109 
7110 		if (changed)
7111 			arvif->tid_conf_changed[arg->tid] |= changed;
7112 	}
7113 
7114 	return 0;
7115 }
7116 
7117 static int
7118 ath10k_mac_parse_tid_config(struct ath10k *ar,
7119 			    struct ieee80211_sta *sta,
7120 			    struct ieee80211_vif *vif,
7121 			    struct cfg80211_tid_cfg *tid_conf,
7122 			    struct wmi_per_peer_per_tid_cfg_arg *arg)
7123 {
7124 	u32 changed = tid_conf->mask;
7125 	int ret = 0, i = 0;
7126 
7127 	if (!changed)
7128 		return -EINVAL;
7129 
7130 	while (i < ATH10K_TID_MAX) {
7131 		if (!(tid_conf->tids & BIT(i))) {
7132 			i++;
7133 			continue;
7134 		}
7135 
7136 		arg->tid = i;
7137 
7138 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7139 			if (tid_conf->noack == NL80211_TID_CONFIG_ENABLE) {
7140 				arg->ack_policy = WMI_PEER_TID_CONFIG_NOACK;
7141 				arg->rate_ctrl =
7142 				WMI_TID_CONFIG_RATE_CONTROL_DEFAULT_LOWEST_RATE;
7143 				arg->aggr_control =
7144 					WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
7145 			} else {
7146 				arg->ack_policy =
7147 					WMI_PEER_TID_CONFIG_ACK;
7148 				arg->rate_ctrl =
7149 					WMI_TID_CONFIG_RATE_CONTROL_AUTO;
7150 				arg->aggr_control =
7151 					WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7152 			}
7153 		}
7154 
7155 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG))
7156 			arg->retry_count = tid_conf->retry_long;
7157 
7158 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7159 			if (tid_conf->noack == NL80211_TID_CONFIG_ENABLE)
7160 				arg->aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7161 			else
7162 				arg->aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
7163 		}
7164 
7165 		if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7166 		    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7167 			ret = ath10k_mac_tid_bitrate_config(ar, vif, sta,
7168 							    &arg->rcode_flags,
7169 							    &arg->rate_ctrl,
7170 							    tid_conf->txrate_type,
7171 							&tid_conf->txrate_mask);
7172 			if (ret) {
7173 				ath10k_warn(ar, "failed to configure bitrate mask %d\n",
7174 					    ret);
7175 				arg->rcode_flags = 0;
7176 				arg->rate_ctrl = 0;
7177 			}
7178 		}
7179 
7180 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7181 			if (tid_conf->rtscts)
7182 				arg->rtscts_ctrl = tid_conf->rtscts;
7183 
7184 			arg->ext_tid_cfg_bitmap = WMI_EXT_TID_RTS_CTS_CONFIG;
7185 		}
7186 
7187 		ret = ath10k_mac_set_tid_config(ar, sta, vif, changed, arg);
7188 		if (ret)
7189 			return ret;
7190 		i++;
7191 	}
7192 
7193 	return ret;
7194 }
7195 
7196 static int ath10k_mac_reset_tid_config(struct ath10k *ar,
7197 				       struct ieee80211_sta *sta,
7198 				       struct ath10k_vif *arvif,
7199 				       u8 tids)
7200 {
7201 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7202 	struct wmi_per_peer_per_tid_cfg_arg arg;
7203 	int ret = 0, i = 0;
7204 
7205 	arg.vdev_id = arvif->vdev_id;
7206 	while (i < ATH10K_TID_MAX) {
7207 		if (!(tids & BIT(i))) {
7208 			i++;
7209 			continue;
7210 		}
7211 
7212 		arg.tid = i;
7213 		arg.ack_policy = WMI_PEER_TID_CONFIG_ACK;
7214 		arg.retry_count = ATH10K_MAX_RETRY_COUNT;
7215 		arg.rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_AUTO;
7216 		arg.aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7217 		arg.rtscts_ctrl = WMI_TID_CONFIG_RTSCTS_CONTROL_ENABLE;
7218 		arg.ext_tid_cfg_bitmap = WMI_EXT_TID_RTS_CTS_CONFIG;
7219 
7220 		ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
7221 
7222 		ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
7223 		if (ret)
7224 			return ret;
7225 
7226 		if (!arvif->tids_rst) {
7227 			arsta->retry_long[i] = -1;
7228 			arsta->noack[i] = -1;
7229 			arsta->ampdu[i] = -1;
7230 			arsta->rate_code[i] = -1;
7231 			arsta->rate_ctrl[i] = 0;
7232 			arsta->rtscts[i] = -1;
7233 		} else {
7234 			arvif->retry_long[i] = 0;
7235 			arvif->noack[i] = 0;
7236 			arvif->ampdu[i] = 0;
7237 			arvif->rate_code[i] = 0;
7238 			arvif->rate_ctrl[i] = 0;
7239 			arvif->rtscts[i] = 0;
7240 		}
7241 
7242 		i++;
7243 	}
7244 
7245 	return ret;
7246 }
7247 
7248 static void ath10k_sta_tid_cfg_wk(struct work_struct *wk)
7249 {
7250 	struct wmi_per_peer_per_tid_cfg_arg arg = {};
7251 	struct ieee80211_sta *sta;
7252 	struct ath10k_sta *arsta;
7253 	struct ath10k_vif *arvif;
7254 	struct ath10k *ar;
7255 	bool config_apply;
7256 	int ret, i;
7257 	u32 changed;
7258 	u8 nss;
7259 
7260 	arsta = container_of(wk, struct ath10k_sta, tid_config_wk);
7261 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
7262 	arvif = arsta->arvif;
7263 	ar = arvif->ar;
7264 
7265 	mutex_lock(&ar->conf_mutex);
7266 
7267 	if (arvif->tids_rst) {
7268 		ret = ath10k_mac_reset_tid_config(ar, sta, arvif,
7269 						  arvif->tids_rst);
7270 		goto exit;
7271 	}
7272 
7273 	ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
7274 
7275 	for (i = 0; i < ATH10K_TID_MAX; i++) {
7276 		config_apply = false;
7277 		changed = arvif->tid_conf_changed[i];
7278 
7279 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7280 			if (arsta->noack[i] != -1) {
7281 				arg.ack_policy  = 0;
7282 			} else {
7283 				config_apply = true;
7284 				arg.ack_policy = arvif->noack[i];
7285 				arg.aggr_control = arvif->ampdu[i];
7286 				arg.rate_ctrl = arvif->rate_ctrl[i];
7287 			}
7288 		}
7289 
7290 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7291 			if (arsta->retry_long[i] != -1 ||
7292 			    arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7293 			    arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7294 				arg.retry_count = 0;
7295 			} else {
7296 				arg.retry_count = arvif->retry_long[i];
7297 				config_apply = true;
7298 			}
7299 		}
7300 
7301 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7302 			if (arsta->ampdu[i] != -1 ||
7303 			    arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7304 			    arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7305 				arg.aggr_control = 0;
7306 			} else {
7307 				arg.aggr_control = arvif->ampdu[i];
7308 				config_apply = true;
7309 			}
7310 		}
7311 
7312 		if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7313 		    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7314 			nss = ATH10K_HW_NSS(arvif->rate_code[i]);
7315 			ret = ath10k_mac_validate_rate_mask(ar, sta,
7316 							    arvif->rate_code[i],
7317 							    nss);
7318 			if (ret &&
7319 			    arvif->rate_ctrl[i] > WMI_TID_CONFIG_RATE_CONTROL_AUTO) {
7320 				arg.rate_ctrl = 0;
7321 				arg.rcode_flags = 0;
7322 			}
7323 
7324 			if (arsta->rate_ctrl[i] >
7325 			    WMI_TID_CONFIG_RATE_CONTROL_AUTO ||
7326 			    arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7327 			    arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7328 				arg.rate_ctrl = 0;
7329 				arg.rcode_flags = 0;
7330 			} else {
7331 				arg.rate_ctrl = arvif->rate_ctrl[i];
7332 				arg.rcode_flags = arvif->rate_code[i];
7333 				config_apply = true;
7334 			}
7335 		}
7336 
7337 		if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7338 			if (arsta->rtscts[i]) {
7339 				arg.rtscts_ctrl = 0;
7340 				arg.ext_tid_cfg_bitmap = 0;
7341 			} else {
7342 				arg.rtscts_ctrl = arvif->rtscts[i] - 1;
7343 				arg.ext_tid_cfg_bitmap =
7344 					WMI_EXT_TID_RTS_CTS_CONFIG;
7345 				config_apply = true;
7346 			}
7347 		}
7348 
7349 		arg.tid = i;
7350 
7351 		if (config_apply) {
7352 			ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
7353 			if (ret)
7354 				ath10k_warn(ar, "failed to set per tid config for sta %pM: %d\n",
7355 					    sta->addr, ret);
7356 		}
7357 
7358 		arg.ack_policy  = 0;
7359 		arg.retry_count  = 0;
7360 		arg.aggr_control  = 0;
7361 		arg.rate_ctrl = 0;
7362 		arg.rcode_flags = 0;
7363 	}
7364 
7365 exit:
7366 	mutex_unlock(&ar->conf_mutex);
7367 }
7368 
7369 static void ath10k_mac_vif_stations_tid_conf(void *data,
7370 					     struct ieee80211_sta *sta)
7371 {
7372 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7373 	struct ath10k_mac_iter_tid_conf_data *iter_data = data;
7374 	struct ieee80211_vif *sta_vif = arsta->arvif->vif;
7375 
7376 	if (sta_vif != iter_data->curr_vif || !sta->wme)
7377 		return;
7378 
7379 	ieee80211_queue_work(iter_data->ar->hw, &arsta->tid_config_wk);
7380 }
7381 
7382 static int ath10k_sta_state(struct ieee80211_hw *hw,
7383 			    struct ieee80211_vif *vif,
7384 			    struct ieee80211_sta *sta,
7385 			    enum ieee80211_sta_state old_state,
7386 			    enum ieee80211_sta_state new_state)
7387 {
7388 	struct ath10k *ar = hw->priv;
7389 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7390 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7391 	struct ath10k_peer *peer;
7392 	int ret = 0;
7393 	int i;
7394 
7395 	if (old_state == IEEE80211_STA_NOTEXIST &&
7396 	    new_state == IEEE80211_STA_NONE) {
7397 		memset(arsta, 0, sizeof(*arsta));
7398 		arsta->arvif = arvif;
7399 		arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED;
7400 		INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
7401 		INIT_WORK(&arsta->tid_config_wk, ath10k_sta_tid_cfg_wk);
7402 
7403 		for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
7404 			ath10k_mac_txq_init(sta->txq[i]);
7405 	}
7406 
7407 	/* cancel must be done outside the mutex to avoid deadlock */
7408 	if ((old_state == IEEE80211_STA_NONE &&
7409 	     new_state == IEEE80211_STA_NOTEXIST)) {
7410 		cancel_work_sync(&arsta->update_wk);
7411 		cancel_work_sync(&arsta->tid_config_wk);
7412 	}
7413 
7414 	mutex_lock(&ar->conf_mutex);
7415 
7416 	if (old_state == IEEE80211_STA_NOTEXIST &&
7417 	    new_state == IEEE80211_STA_NONE) {
7418 		/*
7419 		 * New station addition.
7420 		 */
7421 		enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
7422 		u32 num_tdls_stations;
7423 
7424 		ath10k_dbg(ar, ATH10K_DBG_STA,
7425 			   "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
7426 			   arvif->vdev_id, sta->addr,
7427 			   ar->num_stations + 1, ar->max_num_stations,
7428 			   ar->num_peers + 1, ar->max_num_peers);
7429 
7430 		num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
7431 
7432 		if (sta->tdls) {
7433 			if (num_tdls_stations >= ar->max_num_tdls_vdevs) {
7434 				ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
7435 					    arvif->vdev_id,
7436 					    ar->max_num_tdls_vdevs);
7437 				ret = -ELNRNG;
7438 				goto exit;
7439 			}
7440 			peer_type = WMI_PEER_TYPE_TDLS;
7441 		}
7442 
7443 		ret = ath10k_mac_inc_num_stations(arvif, sta);
7444 		if (ret) {
7445 			ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
7446 				    ar->max_num_stations);
7447 			goto exit;
7448 		}
7449 
7450 		if (ath10k_debug_is_extd_tx_stats_enabled(ar)) {
7451 			arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats),
7452 						  GFP_KERNEL);
7453 			if (!arsta->tx_stats) {
7454 				ath10k_mac_dec_num_stations(arvif, sta);
7455 				ret = -ENOMEM;
7456 				goto exit;
7457 			}
7458 		}
7459 
7460 		ret = ath10k_peer_create(ar, vif, sta, arvif->vdev_id,
7461 					 sta->addr, peer_type);
7462 		if (ret) {
7463 			ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
7464 				    sta->addr, arvif->vdev_id, ret);
7465 			ath10k_mac_dec_num_stations(arvif, sta);
7466 			kfree(arsta->tx_stats);
7467 			goto exit;
7468 		}
7469 
7470 		spin_lock_bh(&ar->data_lock);
7471 
7472 		peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
7473 		if (!peer) {
7474 			ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
7475 				    vif->addr, arvif->vdev_id);
7476 			spin_unlock_bh(&ar->data_lock);
7477 			ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7478 			ath10k_mac_dec_num_stations(arvif, sta);
7479 			kfree(arsta->tx_stats);
7480 			ret = -ENOENT;
7481 			goto exit;
7482 		}
7483 
7484 		arsta->peer_id = find_first_bit(peer->peer_ids,
7485 						ATH10K_MAX_NUM_PEER_IDS);
7486 
7487 		spin_unlock_bh(&ar->data_lock);
7488 
7489 		if (!sta->tdls)
7490 			goto exit;
7491 
7492 		ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7493 						      WMI_TDLS_ENABLE_ACTIVE);
7494 		if (ret) {
7495 			ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
7496 				    arvif->vdev_id, ret);
7497 			ath10k_peer_delete(ar, arvif->vdev_id,
7498 					   sta->addr);
7499 			ath10k_mac_dec_num_stations(arvif, sta);
7500 			kfree(arsta->tx_stats);
7501 			goto exit;
7502 		}
7503 
7504 		ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
7505 						  WMI_TDLS_PEER_STATE_PEERING);
7506 		if (ret) {
7507 			ath10k_warn(ar,
7508 				    "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
7509 				    sta->addr, arvif->vdev_id, ret);
7510 			ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7511 			ath10k_mac_dec_num_stations(arvif, sta);
7512 			kfree(arsta->tx_stats);
7513 
7514 			if (num_tdls_stations != 0)
7515 				goto exit;
7516 			ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7517 							WMI_TDLS_DISABLE);
7518 		}
7519 	} else if ((old_state == IEEE80211_STA_NONE &&
7520 		    new_state == IEEE80211_STA_NOTEXIST)) {
7521 		/*
7522 		 * Existing station deletion.
7523 		 */
7524 		ath10k_dbg(ar, ATH10K_DBG_STA,
7525 			   "mac vdev %d peer delete %pM sta %pK (sta gone)\n",
7526 			   arvif->vdev_id, sta->addr, sta);
7527 
7528 		if (sta->tdls) {
7529 			ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id,
7530 							  sta,
7531 							  WMI_TDLS_PEER_STATE_TEARDOWN);
7532 			if (ret)
7533 				ath10k_warn(ar, "failed to update tdls peer state for %pM state %d: %i\n",
7534 					    sta->addr,
7535 					    WMI_TDLS_PEER_STATE_TEARDOWN, ret);
7536 		}
7537 
7538 		ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7539 		if (ret)
7540 			ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
7541 				    sta->addr, arvif->vdev_id, ret);
7542 
7543 		ath10k_mac_dec_num_stations(arvif, sta);
7544 
7545 		spin_lock_bh(&ar->data_lock);
7546 		for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
7547 			peer = ar->peer_map[i];
7548 			if (!peer)
7549 				continue;
7550 
7551 			if (peer->sta == sta) {
7552 				ath10k_warn(ar, "found sta peer %pM (ptr %pK id %d) entry on vdev %i after it was supposedly removed\n",
7553 					    sta->addr, peer, i, arvif->vdev_id);
7554 				peer->sta = NULL;
7555 
7556 				/* Clean up the peer object as well since we
7557 				 * must have failed to do this above.
7558 				 */
7559 				list_del(&peer->list);
7560 				ar->peer_map[i] = NULL;
7561 				kfree(peer);
7562 				ar->num_peers--;
7563 			}
7564 		}
7565 		spin_unlock_bh(&ar->data_lock);
7566 
7567 		if (ath10k_debug_is_extd_tx_stats_enabled(ar)) {
7568 			kfree(arsta->tx_stats);
7569 			arsta->tx_stats = NULL;
7570 		}
7571 
7572 		for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
7573 			ath10k_mac_txq_unref(ar, sta->txq[i]);
7574 
7575 		if (!sta->tdls)
7576 			goto exit;
7577 
7578 		if (ath10k_mac_tdls_vif_stations_count(hw, vif))
7579 			goto exit;
7580 
7581 		/* This was the last tdls peer in current vif */
7582 		ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7583 						      WMI_TDLS_DISABLE);
7584 		if (ret) {
7585 			ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
7586 				    arvif->vdev_id, ret);
7587 		}
7588 	} else if (old_state == IEEE80211_STA_AUTH &&
7589 		   new_state == IEEE80211_STA_ASSOC &&
7590 		   (vif->type == NL80211_IFTYPE_AP ||
7591 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
7592 		    vif->type == NL80211_IFTYPE_ADHOC)) {
7593 		/*
7594 		 * New association.
7595 		 */
7596 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac sta %pM associated\n",
7597 			   sta->addr);
7598 
7599 		ret = ath10k_station_assoc(ar, vif, sta, false);
7600 		if (ret)
7601 			ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
7602 				    sta->addr, arvif->vdev_id, ret);
7603 	} else if (old_state == IEEE80211_STA_ASSOC &&
7604 		   new_state == IEEE80211_STA_AUTHORIZED &&
7605 		   sta->tdls) {
7606 		/*
7607 		 * Tdls station authorized.
7608 		 */
7609 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac tdls sta %pM authorized\n",
7610 			   sta->addr);
7611 
7612 		ret = ath10k_station_assoc(ar, vif, sta, false);
7613 		if (ret) {
7614 			ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
7615 				    sta->addr, arvif->vdev_id, ret);
7616 			goto exit;
7617 		}
7618 
7619 		ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
7620 						  WMI_TDLS_PEER_STATE_CONNECTED);
7621 		if (ret)
7622 			ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
7623 				    sta->addr, arvif->vdev_id, ret);
7624 	} else if (old_state == IEEE80211_STA_ASSOC &&
7625 		    new_state == IEEE80211_STA_AUTH &&
7626 		    (vif->type == NL80211_IFTYPE_AP ||
7627 		     vif->type == NL80211_IFTYPE_MESH_POINT ||
7628 		     vif->type == NL80211_IFTYPE_ADHOC)) {
7629 		/*
7630 		 * Disassociation.
7631 		 */
7632 		ath10k_dbg(ar, ATH10K_DBG_STA, "mac sta %pM disassociated\n",
7633 			   sta->addr);
7634 
7635 		ret = ath10k_station_disassoc(ar, vif, sta);
7636 		if (ret)
7637 			ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
7638 				    sta->addr, arvif->vdev_id, ret);
7639 	}
7640 exit:
7641 	mutex_unlock(&ar->conf_mutex);
7642 	return ret;
7643 }
7644 
7645 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
7646 				u16 ac, bool enable)
7647 {
7648 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7649 	struct wmi_sta_uapsd_auto_trig_arg arg = {};
7650 	u32 prio = 0, acc = 0;
7651 	u32 value = 0;
7652 	int ret = 0;
7653 
7654 	lockdep_assert_held(&ar->conf_mutex);
7655 
7656 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
7657 		return 0;
7658 
7659 	switch (ac) {
7660 	case IEEE80211_AC_VO:
7661 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
7662 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
7663 		prio = 7;
7664 		acc = 3;
7665 		break;
7666 	case IEEE80211_AC_VI:
7667 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
7668 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
7669 		prio = 5;
7670 		acc = 2;
7671 		break;
7672 	case IEEE80211_AC_BE:
7673 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
7674 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
7675 		prio = 2;
7676 		acc = 1;
7677 		break;
7678 	case IEEE80211_AC_BK:
7679 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
7680 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
7681 		prio = 0;
7682 		acc = 0;
7683 		break;
7684 	}
7685 
7686 	if (enable)
7687 		arvif->u.sta.uapsd |= value;
7688 	else
7689 		arvif->u.sta.uapsd &= ~value;
7690 
7691 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7692 					  WMI_STA_PS_PARAM_UAPSD,
7693 					  arvif->u.sta.uapsd);
7694 	if (ret) {
7695 		ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
7696 		goto exit;
7697 	}
7698 
7699 	if (arvif->u.sta.uapsd)
7700 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
7701 	else
7702 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
7703 
7704 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7705 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
7706 					  value);
7707 	if (ret)
7708 		ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
7709 
7710 	ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
7711 	if (ret) {
7712 		ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
7713 			    arvif->vdev_id, ret);
7714 		return ret;
7715 	}
7716 
7717 	ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
7718 	if (ret) {
7719 		ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
7720 			    arvif->vdev_id, ret);
7721 		return ret;
7722 	}
7723 
7724 	if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
7725 	    test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
7726 		/* Only userspace can make an educated decision when to send
7727 		 * trigger frame. The following effectively disables u-UAPSD
7728 		 * autotrigger in firmware (which is enabled by default
7729 		 * provided the autotrigger service is available).
7730 		 */
7731 
7732 		arg.wmm_ac = acc;
7733 		arg.user_priority = prio;
7734 		arg.service_interval = 0;
7735 		arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
7736 		arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
7737 
7738 		ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
7739 						arvif->bssid, &arg, 1);
7740 		if (ret) {
7741 			ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
7742 				    ret);
7743 			return ret;
7744 		}
7745 	}
7746 
7747 exit:
7748 	return ret;
7749 }
7750 
7751 static int ath10k_conf_tx(struct ieee80211_hw *hw,
7752 			  struct ieee80211_vif *vif, u16 ac,
7753 			  const struct ieee80211_tx_queue_params *params)
7754 {
7755 	struct ath10k *ar = hw->priv;
7756 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7757 	struct wmi_wmm_params_arg *p = NULL;
7758 	int ret;
7759 
7760 	mutex_lock(&ar->conf_mutex);
7761 
7762 	switch (ac) {
7763 	case IEEE80211_AC_VO:
7764 		p = &arvif->wmm_params.ac_vo;
7765 		break;
7766 	case IEEE80211_AC_VI:
7767 		p = &arvif->wmm_params.ac_vi;
7768 		break;
7769 	case IEEE80211_AC_BE:
7770 		p = &arvif->wmm_params.ac_be;
7771 		break;
7772 	case IEEE80211_AC_BK:
7773 		p = &arvif->wmm_params.ac_bk;
7774 		break;
7775 	}
7776 
7777 	if (WARN_ON(!p)) {
7778 		ret = -EINVAL;
7779 		goto exit;
7780 	}
7781 
7782 	p->cwmin = params->cw_min;
7783 	p->cwmax = params->cw_max;
7784 	p->aifs = params->aifs;
7785 
7786 	/*
7787 	 * The channel time duration programmed in the HW is in absolute
7788 	 * microseconds, while mac80211 gives the txop in units of
7789 	 * 32 microseconds.
7790 	 */
7791 	p->txop = params->txop * 32;
7792 
7793 	if (ar->wmi.ops->gen_vdev_wmm_conf) {
7794 		ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
7795 					       &arvif->wmm_params);
7796 		if (ret) {
7797 			ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
7798 				    arvif->vdev_id, ret);
7799 			goto exit;
7800 		}
7801 	} else {
7802 		/* This won't work well with multi-interface cases but it's
7803 		 * better than nothing.
7804 		 */
7805 		ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
7806 		if (ret) {
7807 			ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
7808 			goto exit;
7809 		}
7810 	}
7811 
7812 	ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
7813 	if (ret)
7814 		ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
7815 
7816 exit:
7817 	mutex_unlock(&ar->conf_mutex);
7818 	return ret;
7819 }
7820 
7821 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
7822 				    struct ieee80211_vif *vif,
7823 				    struct ieee80211_channel *chan,
7824 				    int duration,
7825 				    enum ieee80211_roc_type type)
7826 {
7827 	struct ath10k *ar = hw->priv;
7828 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7829 	struct wmi_start_scan_arg arg;
7830 	int ret = 0;
7831 	u32 scan_time_msec;
7832 
7833 	mutex_lock(&ar->conf_mutex);
7834 
7835 	if (ath10k_mac_tdls_vif_stations_count(hw, vif) > 0) {
7836 		ret = -EBUSY;
7837 		goto exit;
7838 	}
7839 
7840 	spin_lock_bh(&ar->data_lock);
7841 	switch (ar->scan.state) {
7842 	case ATH10K_SCAN_IDLE:
7843 		reinit_completion(&ar->scan.started);
7844 		reinit_completion(&ar->scan.completed);
7845 		reinit_completion(&ar->scan.on_channel);
7846 		ar->scan.state = ATH10K_SCAN_STARTING;
7847 		ar->scan.is_roc = true;
7848 		ar->scan.vdev_id = arvif->vdev_id;
7849 		ar->scan.roc_freq = chan->center_freq;
7850 		ar->scan.roc_notify = true;
7851 		ret = 0;
7852 		break;
7853 	case ATH10K_SCAN_STARTING:
7854 	case ATH10K_SCAN_RUNNING:
7855 	case ATH10K_SCAN_ABORTING:
7856 		ret = -EBUSY;
7857 		break;
7858 	}
7859 	spin_unlock_bh(&ar->data_lock);
7860 
7861 	if (ret)
7862 		goto exit;
7863 
7864 	scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
7865 
7866 	memset(&arg, 0, sizeof(arg));
7867 	ath10k_wmi_start_scan_init(ar, &arg);
7868 	arg.vdev_id = arvif->vdev_id;
7869 	arg.scan_id = ATH10K_SCAN_ID;
7870 	arg.n_channels = 1;
7871 	arg.channels[0] = chan->center_freq;
7872 	arg.dwell_time_active = scan_time_msec;
7873 	arg.dwell_time_passive = scan_time_msec;
7874 	arg.max_scan_time = scan_time_msec;
7875 	arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
7876 	arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
7877 	arg.burst_duration_ms = duration;
7878 
7879 	ret = ath10k_start_scan(ar, &arg);
7880 	if (ret) {
7881 		ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
7882 		spin_lock_bh(&ar->data_lock);
7883 		ar->scan.state = ATH10K_SCAN_IDLE;
7884 		spin_unlock_bh(&ar->data_lock);
7885 		goto exit;
7886 	}
7887 
7888 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
7889 	if (ret == 0) {
7890 		ath10k_warn(ar, "failed to switch to channel for roc scan\n");
7891 
7892 		ret = ath10k_scan_stop(ar);
7893 		if (ret)
7894 			ath10k_warn(ar, "failed to stop scan: %d\n", ret);
7895 
7896 		ret = -ETIMEDOUT;
7897 		goto exit;
7898 	}
7899 
7900 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
7901 				     msecs_to_jiffies(duration));
7902 
7903 	ret = 0;
7904 exit:
7905 	mutex_unlock(&ar->conf_mutex);
7906 	return ret;
7907 }
7908 
7909 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw,
7910 					   struct ieee80211_vif *vif)
7911 {
7912 	struct ath10k *ar = hw->priv;
7913 
7914 	mutex_lock(&ar->conf_mutex);
7915 
7916 	spin_lock_bh(&ar->data_lock);
7917 	ar->scan.roc_notify = false;
7918 	spin_unlock_bh(&ar->data_lock);
7919 
7920 	ath10k_scan_abort(ar);
7921 
7922 	mutex_unlock(&ar->conf_mutex);
7923 
7924 	cancel_delayed_work_sync(&ar->scan.timeout);
7925 
7926 	return 0;
7927 }
7928 
7929 /*
7930  * Both RTS and Fragmentation threshold are interface-specific
7931  * in ath10k, but device-specific in mac80211.
7932  */
7933 
7934 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
7935 {
7936 	struct ath10k *ar = hw->priv;
7937 	struct ath10k_vif *arvif;
7938 	int ret = 0;
7939 
7940 	mutex_lock(&ar->conf_mutex);
7941 	list_for_each_entry(arvif, &ar->arvifs, list) {
7942 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
7943 			   arvif->vdev_id, value);
7944 
7945 		ret = ath10k_mac_set_rts(arvif, value);
7946 		if (ret) {
7947 			ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
7948 				    arvif->vdev_id, ret);
7949 			break;
7950 		}
7951 	}
7952 	mutex_unlock(&ar->conf_mutex);
7953 
7954 	return ret;
7955 }
7956 
7957 static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
7958 {
7959 	/* Even though there's a WMI enum for fragmentation threshold no known
7960 	 * firmware actually implements it. Moreover it is not possible to rely
7961 	 * frame fragmentation to mac80211 because firmware clears the "more
7962 	 * fragments" bit in frame control making it impossible for remote
7963 	 * devices to reassemble frames.
7964 	 *
7965 	 * Hence implement a dummy callback just to say fragmentation isn't
7966 	 * supported. This effectively prevents mac80211 from doing frame
7967 	 * fragmentation in software.
7968 	 */
7969 	return -EOPNOTSUPP;
7970 }
7971 
7972 void ath10k_mac_wait_tx_complete(struct ath10k *ar)
7973 {
7974 	bool skip;
7975 	long time_left;
7976 
7977 	/* mac80211 doesn't care if we really xmit queued frames or not
7978 	 * we'll collect those frames either way if we stop/delete vdevs
7979 	 */
7980 
7981 	if (ar->state == ATH10K_STATE_WEDGED)
7982 		return;
7983 
7984 	time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
7985 			bool empty;
7986 
7987 			spin_lock_bh(&ar->htt.tx_lock);
7988 			empty = (ar->htt.num_pending_tx == 0);
7989 			spin_unlock_bh(&ar->htt.tx_lock);
7990 
7991 			skip = (ar->state == ATH10K_STATE_WEDGED) ||
7992 			       test_bit(ATH10K_FLAG_CRASH_FLUSH,
7993 					&ar->dev_flags);
7994 
7995 			(empty || skip);
7996 		}), ATH10K_FLUSH_TIMEOUT_HZ);
7997 
7998 	if (time_left == 0 || skip)
7999 		ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
8000 			    skip, ar->state, time_left);
8001 }
8002 
8003 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
8004 			 u32 queues, bool drop)
8005 {
8006 	struct ath10k *ar = hw->priv;
8007 	struct ath10k_vif *arvif;
8008 	u32 bitmap;
8009 
8010 	if (drop) {
8011 		if (vif && vif->type == NL80211_IFTYPE_STATION) {
8012 			bitmap = ~(1 << WMI_MGMT_TID);
8013 			list_for_each_entry(arvif, &ar->arvifs, list) {
8014 				if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
8015 					ath10k_wmi_peer_flush(ar, arvif->vdev_id,
8016 							      arvif->bssid, bitmap);
8017 			}
8018 			ath10k_htt_flush_tx(&ar->htt);
8019 		}
8020 		return;
8021 	}
8022 
8023 	mutex_lock(&ar->conf_mutex);
8024 	ath10k_mac_wait_tx_complete(ar);
8025 	mutex_unlock(&ar->conf_mutex);
8026 }
8027 
8028 /* TODO: Implement this function properly
8029  * For now it is needed to reply to Probe Requests in IBSS mode.
8030  * Propably we need this information from FW.
8031  */
8032 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
8033 {
8034 	return 1;
8035 }
8036 
8037 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
8038 				     enum ieee80211_reconfig_type reconfig_type)
8039 {
8040 	struct ath10k *ar = hw->priv;
8041 
8042 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
8043 		return;
8044 
8045 	mutex_lock(&ar->conf_mutex);
8046 
8047 	/* If device failed to restart it will be in a different state, e.g.
8048 	 * ATH10K_STATE_WEDGED
8049 	 */
8050 	if (ar->state == ATH10K_STATE_RESTARTED) {
8051 		ath10k_info(ar, "device successfully recovered\n");
8052 		ar->state = ATH10K_STATE_ON;
8053 		ieee80211_wake_queues(ar->hw);
8054 		clear_bit(ATH10K_FLAG_RESTARTING, &ar->dev_flags);
8055 	}
8056 
8057 	mutex_unlock(&ar->conf_mutex);
8058 }
8059 
8060 static void
8061 ath10k_mac_update_bss_chan_survey(struct ath10k *ar,
8062 				  struct ieee80211_channel *channel)
8063 {
8064 	int ret;
8065 	enum wmi_bss_survey_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
8066 
8067 	lockdep_assert_held(&ar->conf_mutex);
8068 
8069 	if (!test_bit(WMI_SERVICE_BSS_CHANNEL_INFO_64, ar->wmi.svc_map) ||
8070 	    (ar->rx_channel != channel))
8071 		return;
8072 
8073 	if (ar->scan.state != ATH10K_SCAN_IDLE) {
8074 		ath10k_dbg(ar, ATH10K_DBG_MAC, "ignoring bss chan info request while scanning..\n");
8075 		return;
8076 	}
8077 
8078 	reinit_completion(&ar->bss_survey_done);
8079 
8080 	ret = ath10k_wmi_pdev_bss_chan_info_request(ar, type);
8081 	if (ret) {
8082 		ath10k_warn(ar, "failed to send pdev bss chan info request\n");
8083 		return;
8084 	}
8085 
8086 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
8087 	if (!ret) {
8088 		ath10k_warn(ar, "bss channel survey timed out\n");
8089 		return;
8090 	}
8091 }
8092 
8093 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
8094 			     struct survey_info *survey)
8095 {
8096 	struct ath10k *ar = hw->priv;
8097 	struct ieee80211_supported_band *sband;
8098 	struct survey_info *ar_survey = &ar->survey[idx];
8099 	int ret = 0;
8100 
8101 	mutex_lock(&ar->conf_mutex);
8102 
8103 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
8104 	if (sband && idx >= sband->n_channels) {
8105 		idx -= sband->n_channels;
8106 		sband = NULL;
8107 	}
8108 
8109 	if (!sband)
8110 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
8111 
8112 	if (!sband || idx >= sband->n_channels) {
8113 		ret = -ENOENT;
8114 		goto exit;
8115 	}
8116 
8117 	ath10k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
8118 
8119 	spin_lock_bh(&ar->data_lock);
8120 	memcpy(survey, ar_survey, sizeof(*survey));
8121 	spin_unlock_bh(&ar->data_lock);
8122 
8123 	survey->channel = &sband->channels[idx];
8124 
8125 	if (ar->rx_channel == survey->channel)
8126 		survey->filled |= SURVEY_INFO_IN_USE;
8127 
8128 exit:
8129 	mutex_unlock(&ar->conf_mutex);
8130 	return ret;
8131 }
8132 
8133 static bool
8134 ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
8135 				       enum nl80211_band band,
8136 				       const struct cfg80211_bitrate_mask *mask,
8137 				       int *nss)
8138 {
8139 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
8140 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
8141 	u8 ht_nss_mask = 0;
8142 	u8 vht_nss_mask = 0;
8143 	int i;
8144 
8145 	if (mask->control[band].legacy)
8146 		return false;
8147 
8148 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
8149 		if (mask->control[band].ht_mcs[i] == 0)
8150 			continue;
8151 		else if (mask->control[band].ht_mcs[i] ==
8152 			 sband->ht_cap.mcs.rx_mask[i])
8153 			ht_nss_mask |= BIT(i);
8154 		else
8155 			return false;
8156 	}
8157 
8158 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
8159 		if (mask->control[band].vht_mcs[i] == 0)
8160 			continue;
8161 		else if (mask->control[band].vht_mcs[i] ==
8162 			 ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
8163 			vht_nss_mask |= BIT(i);
8164 		else
8165 			return false;
8166 	}
8167 
8168 	if (ht_nss_mask != vht_nss_mask)
8169 		return false;
8170 
8171 	if (ht_nss_mask == 0)
8172 		return false;
8173 
8174 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
8175 		return false;
8176 
8177 	*nss = fls(ht_nss_mask);
8178 
8179 	return true;
8180 }
8181 
8182 static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
8183 					    u8 rate, u8 nss, u8 sgi, u8 ldpc)
8184 {
8185 	struct ath10k *ar = arvif->ar;
8186 	u32 vdev_param;
8187 	int ret;
8188 
8189 	lockdep_assert_held(&ar->conf_mutex);
8190 
8191 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02x nss %u sgi %u\n",
8192 		   arvif->vdev_id, rate, nss, sgi);
8193 
8194 	vdev_param = ar->wmi.vdev_param->fixed_rate;
8195 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
8196 	if (ret) {
8197 		ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
8198 			    rate, ret);
8199 		return ret;
8200 	}
8201 
8202 	vdev_param = ar->wmi.vdev_param->nss;
8203 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
8204 	if (ret) {
8205 		ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
8206 		return ret;
8207 	}
8208 
8209 	vdev_param = ar->wmi.vdev_param->sgi;
8210 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
8211 	if (ret) {
8212 		ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
8213 		return ret;
8214 	}
8215 
8216 	vdev_param = ar->wmi.vdev_param->ldpc;
8217 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
8218 	if (ret) {
8219 		ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
8220 		return ret;
8221 	}
8222 
8223 	return 0;
8224 }
8225 
8226 static bool
8227 ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
8228 				enum nl80211_band band,
8229 				const struct cfg80211_bitrate_mask *mask,
8230 				bool allow_pfr)
8231 {
8232 	int i;
8233 	u16 vht_mcs;
8234 
8235 	/* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
8236 	 * to express all VHT MCS rate masks. Effectively only the following
8237 	 * ranges can be used: none, 0-7, 0-8 and 0-9.
8238 	 */
8239 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
8240 		vht_mcs = mask->control[band].vht_mcs[i];
8241 
8242 		switch (vht_mcs) {
8243 		case 0:
8244 		case BIT(8) - 1:
8245 		case BIT(9) - 1:
8246 		case BIT(10) - 1:
8247 			break;
8248 		default:
8249 			if (!allow_pfr)
8250 				ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
8251 			return false;
8252 		}
8253 	}
8254 
8255 	return true;
8256 }
8257 
8258 static bool ath10k_mac_set_vht_bitrate_mask_fixup(struct ath10k *ar,
8259 						  struct ath10k_vif *arvif,
8260 						  struct ieee80211_sta *sta)
8261 {
8262 	int err;
8263 	u8 rate = arvif->vht_pfr;
8264 
8265 	/* skip non vht and multiple rate peers */
8266 	if (!sta->vht_cap.vht_supported || arvif->vht_num_rates != 1)
8267 		return false;
8268 
8269 	err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
8270 					WMI_PEER_PARAM_FIXED_RATE, rate);
8271 	if (err)
8272 		ath10k_warn(ar, "failed to enable STA %pM peer fixed rate: %d\n",
8273 			    sta->addr, err);
8274 
8275 	return true;
8276 }
8277 
8278 static void ath10k_mac_set_bitrate_mask_iter(void *data,
8279 					     struct ieee80211_sta *sta)
8280 {
8281 	struct ath10k_vif *arvif = data;
8282 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8283 	struct ath10k *ar = arvif->ar;
8284 
8285 	if (arsta->arvif != arvif)
8286 		return;
8287 
8288 	if (ath10k_mac_set_vht_bitrate_mask_fixup(ar, arvif, sta))
8289 		return;
8290 
8291 	spin_lock_bh(&ar->data_lock);
8292 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
8293 	spin_unlock_bh(&ar->data_lock);
8294 
8295 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
8296 }
8297 
8298 static void ath10k_mac_clr_bitrate_mask_iter(void *data,
8299 					     struct ieee80211_sta *sta)
8300 {
8301 	struct ath10k_vif *arvif = data;
8302 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8303 	struct ath10k *ar = arvif->ar;
8304 	int err;
8305 
8306 	/* clear vht peers only */
8307 	if (arsta->arvif != arvif || !sta->vht_cap.vht_supported)
8308 		return;
8309 
8310 	err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
8311 					WMI_PEER_PARAM_FIXED_RATE,
8312 					WMI_FIXED_RATE_NONE);
8313 	if (err)
8314 		ath10k_warn(ar, "failed to clear STA %pM peer fixed rate: %d\n",
8315 			    sta->addr, err);
8316 }
8317 
8318 static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
8319 					  struct ieee80211_vif *vif,
8320 					  const struct cfg80211_bitrate_mask *mask)
8321 {
8322 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8323 	struct cfg80211_chan_def def;
8324 	struct ath10k *ar = arvif->ar;
8325 	enum nl80211_band band;
8326 	const u8 *ht_mcs_mask;
8327 	const u16 *vht_mcs_mask;
8328 	u8 rate;
8329 	u8 nss;
8330 	u8 sgi;
8331 	u8 ldpc;
8332 	int single_nss;
8333 	int ret;
8334 	int vht_num_rates, allow_pfr;
8335 	u8 vht_pfr;
8336 	bool update_bitrate_mask = true;
8337 
8338 	if (ath10k_mac_vif_chan(vif, &def))
8339 		return -EPERM;
8340 
8341 	band = def.chan->band;
8342 	ht_mcs_mask = mask->control[band].ht_mcs;
8343 	vht_mcs_mask = mask->control[band].vht_mcs;
8344 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
8345 
8346 	sgi = mask->control[band].gi;
8347 	if (sgi == NL80211_TXRATE_FORCE_LGI)
8348 		return -EINVAL;
8349 
8350 	allow_pfr = test_bit(ATH10K_FW_FEATURE_PEER_FIXED_RATE,
8351 			     ar->normal_mode_fw.fw_file.fw_features);
8352 	if (allow_pfr) {
8353 		mutex_lock(&ar->conf_mutex);
8354 		ieee80211_iterate_stations_atomic(ar->hw,
8355 						  ath10k_mac_clr_bitrate_mask_iter,
8356 						  arvif);
8357 		mutex_unlock(&ar->conf_mutex);
8358 	}
8359 
8360 	if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask,
8361 						    &vht_num_rates)) {
8362 		ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
8363 							      &rate, &nss,
8364 							      false);
8365 		if (ret) {
8366 			ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
8367 				    arvif->vdev_id, ret);
8368 			return ret;
8369 		}
8370 	} else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
8371 							  &single_nss)) {
8372 		rate = WMI_FIXED_RATE_NONE;
8373 		nss = single_nss;
8374 	} else {
8375 		rate = WMI_FIXED_RATE_NONE;
8376 		nss = min(ar->num_rf_chains,
8377 			  max(ath10k_mac_max_ht_nss(ht_mcs_mask),
8378 			      ath10k_mac_max_vht_nss(vht_mcs_mask)));
8379 
8380 		if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask,
8381 						     allow_pfr)) {
8382 			u8 vht_nss;
8383 
8384 			if (!allow_pfr || vht_num_rates != 1)
8385 				return -EINVAL;
8386 
8387 			/* Reach here, firmware supports peer fixed rate and has
8388 			 * single vht rate, and don't update vif birate_mask, as
8389 			 * the rate only for specific peer.
8390 			 */
8391 			ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
8392 								&vht_pfr,
8393 								&vht_nss,
8394 								true);
8395 			update_bitrate_mask = false;
8396 		} else {
8397 			vht_pfr = 0;
8398 		}
8399 
8400 		mutex_lock(&ar->conf_mutex);
8401 
8402 		if (update_bitrate_mask)
8403 			arvif->bitrate_mask = *mask;
8404 		arvif->vht_num_rates = vht_num_rates;
8405 		arvif->vht_pfr = vht_pfr;
8406 		ieee80211_iterate_stations_atomic(ar->hw,
8407 						  ath10k_mac_set_bitrate_mask_iter,
8408 						  arvif);
8409 
8410 		mutex_unlock(&ar->conf_mutex);
8411 	}
8412 
8413 	mutex_lock(&ar->conf_mutex);
8414 
8415 	ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
8416 	if (ret) {
8417 		ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
8418 			    arvif->vdev_id, ret);
8419 		goto exit;
8420 	}
8421 
8422 exit:
8423 	mutex_unlock(&ar->conf_mutex);
8424 
8425 	return ret;
8426 }
8427 
8428 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
8429 				 struct ieee80211_vif *vif,
8430 				 struct ieee80211_sta *sta,
8431 				 u32 changed)
8432 {
8433 	struct ath10k *ar = hw->priv;
8434 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8435 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8436 	struct ath10k_peer *peer;
8437 	u32 bw, smps;
8438 
8439 	spin_lock_bh(&ar->data_lock);
8440 
8441 	peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
8442 	if (!peer) {
8443 		spin_unlock_bh(&ar->data_lock);
8444 		ath10k_warn(ar, "mac sta rc update failed to find peer %pM on vdev %i\n",
8445 			    sta->addr, arvif->vdev_id);
8446 		return;
8447 	}
8448 
8449 	ath10k_dbg(ar, ATH10K_DBG_STA,
8450 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
8451 		   sta->addr, changed, sta->bandwidth, sta->rx_nss,
8452 		   sta->smps_mode);
8453 
8454 	if (changed & IEEE80211_RC_BW_CHANGED) {
8455 		bw = WMI_PEER_CHWIDTH_20MHZ;
8456 
8457 		switch (sta->bandwidth) {
8458 		case IEEE80211_STA_RX_BW_20:
8459 			bw = WMI_PEER_CHWIDTH_20MHZ;
8460 			break;
8461 		case IEEE80211_STA_RX_BW_40:
8462 			bw = WMI_PEER_CHWIDTH_40MHZ;
8463 			break;
8464 		case IEEE80211_STA_RX_BW_80:
8465 			bw = WMI_PEER_CHWIDTH_80MHZ;
8466 			break;
8467 		case IEEE80211_STA_RX_BW_160:
8468 			bw = WMI_PEER_CHWIDTH_160MHZ;
8469 			break;
8470 		default:
8471 			ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
8472 				    sta->bandwidth, sta->addr);
8473 			bw = WMI_PEER_CHWIDTH_20MHZ;
8474 			break;
8475 		}
8476 
8477 		arsta->bw = bw;
8478 	}
8479 
8480 	if (changed & IEEE80211_RC_NSS_CHANGED)
8481 		arsta->nss = sta->rx_nss;
8482 
8483 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
8484 		smps = WMI_PEER_SMPS_PS_NONE;
8485 
8486 		switch (sta->smps_mode) {
8487 		case IEEE80211_SMPS_AUTOMATIC:
8488 		case IEEE80211_SMPS_OFF:
8489 			smps = WMI_PEER_SMPS_PS_NONE;
8490 			break;
8491 		case IEEE80211_SMPS_STATIC:
8492 			smps = WMI_PEER_SMPS_STATIC;
8493 			break;
8494 		case IEEE80211_SMPS_DYNAMIC:
8495 			smps = WMI_PEER_SMPS_DYNAMIC;
8496 			break;
8497 		case IEEE80211_SMPS_NUM_MODES:
8498 			ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
8499 				    sta->smps_mode, sta->addr);
8500 			smps = WMI_PEER_SMPS_PS_NONE;
8501 			break;
8502 		}
8503 
8504 		arsta->smps = smps;
8505 	}
8506 
8507 	arsta->changed |= changed;
8508 
8509 	spin_unlock_bh(&ar->data_lock);
8510 
8511 	ieee80211_queue_work(hw, &arsta->update_wk);
8512 }
8513 
8514 static void ath10k_offset_tsf(struct ieee80211_hw *hw,
8515 			      struct ieee80211_vif *vif, s64 tsf_offset)
8516 {
8517 	struct ath10k *ar = hw->priv;
8518 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8519 	u32 offset, vdev_param;
8520 	int ret;
8521 
8522 	if (tsf_offset < 0) {
8523 		vdev_param = ar->wmi.vdev_param->dec_tsf;
8524 		offset = -tsf_offset;
8525 	} else {
8526 		vdev_param = ar->wmi.vdev_param->inc_tsf;
8527 		offset = tsf_offset;
8528 	}
8529 
8530 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
8531 					vdev_param, offset);
8532 
8533 	if (ret && ret != -EOPNOTSUPP)
8534 		ath10k_warn(ar, "failed to set tsf offset %d cmd %d: %d\n",
8535 			    offset, vdev_param, ret);
8536 }
8537 
8538 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
8539 			       struct ieee80211_vif *vif,
8540 			       struct ieee80211_ampdu_params *params)
8541 {
8542 	struct ath10k *ar = hw->priv;
8543 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8544 	struct ieee80211_sta *sta = params->sta;
8545 	enum ieee80211_ampdu_mlme_action action = params->action;
8546 	u16 tid = params->tid;
8547 
8548 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %u action %d\n",
8549 		   arvif->vdev_id, sta->addr, tid, action);
8550 
8551 	switch (action) {
8552 	case IEEE80211_AMPDU_RX_START:
8553 	case IEEE80211_AMPDU_RX_STOP:
8554 		/* HTT AddBa/DelBa events trigger mac80211 Rx BA session
8555 		 * creation/removal. Do we need to verify this?
8556 		 */
8557 		return 0;
8558 	case IEEE80211_AMPDU_TX_START:
8559 	case IEEE80211_AMPDU_TX_STOP_CONT:
8560 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
8561 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
8562 	case IEEE80211_AMPDU_TX_OPERATIONAL:
8563 		/* Firmware offloads Tx aggregation entirely so deny mac80211
8564 		 * Tx aggregation requests.
8565 		 */
8566 		return -EOPNOTSUPP;
8567 	}
8568 
8569 	return -EINVAL;
8570 }
8571 
8572 static void
8573 ath10k_mac_update_rx_channel(struct ath10k *ar,
8574 			     struct ieee80211_chanctx_conf *ctx,
8575 			     struct ieee80211_vif_chanctx_switch *vifs,
8576 			     int n_vifs)
8577 {
8578 	struct cfg80211_chan_def *def = NULL;
8579 
8580 	/* Both locks are required because ar->rx_channel is modified. This
8581 	 * allows readers to hold either lock.
8582 	 */
8583 	lockdep_assert_held(&ar->conf_mutex);
8584 	lockdep_assert_held(&ar->data_lock);
8585 
8586 	WARN_ON(ctx && vifs);
8587 	WARN_ON(vifs && !n_vifs);
8588 
8589 	/* FIXME: Sort of an optimization and a workaround. Peers and vifs are
8590 	 * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
8591 	 * ppdu on Rx may reduce performance on low-end systems. It should be
8592 	 * possible to make tables/hashmaps to speed the lookup up (be vary of
8593 	 * cpu data cache lines though regarding sizes) but to keep the initial
8594 	 * implementation simple and less intrusive fallback to the slow lookup
8595 	 * only for multi-channel cases. Single-channel cases will remain to
8596 	 * use the old channel derival and thus performance should not be
8597 	 * affected much.
8598 	 */
8599 	rcu_read_lock();
8600 	if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
8601 		ieee80211_iter_chan_contexts_atomic(ar->hw,
8602 						    ath10k_mac_get_any_chandef_iter,
8603 						    &def);
8604 
8605 		if (vifs)
8606 			def = &vifs[0].new_ctx->def;
8607 
8608 		ar->rx_channel = def->chan;
8609 	} else if ((ctx && ath10k_mac_num_chanctxs(ar) == 0) ||
8610 		   (ctx && (ar->state == ATH10K_STATE_RESTARTED))) {
8611 		/* During driver restart due to firmware assert, since mac80211
8612 		 * already has valid channel context for given radio, channel
8613 		 * context iteration return num_chanctx > 0. So fix rx_channel
8614 		 * when restart is in progress.
8615 		 */
8616 		ar->rx_channel = ctx->def.chan;
8617 	} else {
8618 		ar->rx_channel = NULL;
8619 	}
8620 	rcu_read_unlock();
8621 }
8622 
8623 static void
8624 ath10k_mac_update_vif_chan(struct ath10k *ar,
8625 			   struct ieee80211_vif_chanctx_switch *vifs,
8626 			   int n_vifs)
8627 {
8628 	struct ath10k_vif *arvif;
8629 	int ret;
8630 	int i;
8631 
8632 	lockdep_assert_held(&ar->conf_mutex);
8633 
8634 	/* First stop monitor interface. Some FW versions crash if there's a
8635 	 * lone monitor interface.
8636 	 */
8637 	if (ar->monitor_started)
8638 		ath10k_monitor_stop(ar);
8639 
8640 	for (i = 0; i < n_vifs; i++) {
8641 		arvif = (void *)vifs[i].vif->drv_priv;
8642 
8643 		ath10k_dbg(ar, ATH10K_DBG_MAC,
8644 			   "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
8645 			   arvif->vdev_id,
8646 			   vifs[i].old_ctx->def.chan->center_freq,
8647 			   vifs[i].new_ctx->def.chan->center_freq,
8648 			   vifs[i].old_ctx->def.width,
8649 			   vifs[i].new_ctx->def.width);
8650 
8651 		if (WARN_ON(!arvif->is_started))
8652 			continue;
8653 
8654 		if (WARN_ON(!arvif->is_up))
8655 			continue;
8656 
8657 		ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
8658 		if (ret) {
8659 			ath10k_warn(ar, "failed to down vdev %d: %d\n",
8660 				    arvif->vdev_id, ret);
8661 			continue;
8662 		}
8663 	}
8664 
8665 	/* All relevant vdevs are downed and associated channel resources
8666 	 * should be available for the channel switch now.
8667 	 */
8668 
8669 	spin_lock_bh(&ar->data_lock);
8670 	ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
8671 	spin_unlock_bh(&ar->data_lock);
8672 
8673 	for (i = 0; i < n_vifs; i++) {
8674 		arvif = (void *)vifs[i].vif->drv_priv;
8675 
8676 		if (WARN_ON(!arvif->is_started))
8677 			continue;
8678 
8679 		if (WARN_ON(!arvif->is_up))
8680 			continue;
8681 
8682 		ret = ath10k_mac_setup_bcn_tmpl(arvif);
8683 		if (ret)
8684 			ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
8685 				    ret);
8686 
8687 		ret = ath10k_mac_setup_prb_tmpl(arvif);
8688 		if (ret)
8689 			ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
8690 				    ret);
8691 
8692 		ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
8693 		if (ret) {
8694 			ath10k_warn(ar, "failed to restart vdev %d: %d\n",
8695 				    arvif->vdev_id, ret);
8696 			continue;
8697 		}
8698 
8699 		ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
8700 					 arvif->bssid);
8701 		if (ret) {
8702 			ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
8703 				    arvif->vdev_id, ret);
8704 			continue;
8705 		}
8706 	}
8707 
8708 	ath10k_monitor_recalc(ar);
8709 }
8710 
8711 static int
8712 ath10k_mac_op_add_chanctx(struct ieee80211_hw *hw,
8713 			  struct ieee80211_chanctx_conf *ctx)
8714 {
8715 	struct ath10k *ar = hw->priv;
8716 
8717 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8718 		   "mac chanctx add freq %u width %d ptr %pK\n",
8719 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
8720 
8721 	mutex_lock(&ar->conf_mutex);
8722 
8723 	spin_lock_bh(&ar->data_lock);
8724 	ath10k_mac_update_rx_channel(ar, ctx, NULL, 0);
8725 	spin_unlock_bh(&ar->data_lock);
8726 
8727 	ath10k_recalc_radar_detection(ar);
8728 	ath10k_monitor_recalc(ar);
8729 
8730 	mutex_unlock(&ar->conf_mutex);
8731 
8732 	return 0;
8733 }
8734 
8735 static void
8736 ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
8737 			     struct ieee80211_chanctx_conf *ctx)
8738 {
8739 	struct ath10k *ar = hw->priv;
8740 
8741 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8742 		   "mac chanctx remove freq %u width %d ptr %pK\n",
8743 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
8744 
8745 	mutex_lock(&ar->conf_mutex);
8746 
8747 	spin_lock_bh(&ar->data_lock);
8748 	ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
8749 	spin_unlock_bh(&ar->data_lock);
8750 
8751 	ath10k_recalc_radar_detection(ar);
8752 	ath10k_monitor_recalc(ar);
8753 
8754 	mutex_unlock(&ar->conf_mutex);
8755 }
8756 
8757 struct ath10k_mac_change_chanctx_arg {
8758 	struct ieee80211_chanctx_conf *ctx;
8759 	struct ieee80211_vif_chanctx_switch *vifs;
8760 	int n_vifs;
8761 	int next_vif;
8762 };
8763 
8764 static void
8765 ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
8766 				   struct ieee80211_vif *vif)
8767 {
8768 	struct ath10k_mac_change_chanctx_arg *arg = data;
8769 
8770 	if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
8771 		return;
8772 
8773 	arg->n_vifs++;
8774 }
8775 
8776 static void
8777 ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
8778 				    struct ieee80211_vif *vif)
8779 {
8780 	struct ath10k_mac_change_chanctx_arg *arg = data;
8781 	struct ieee80211_chanctx_conf *ctx;
8782 
8783 	ctx = rcu_access_pointer(vif->chanctx_conf);
8784 	if (ctx != arg->ctx)
8785 		return;
8786 
8787 	if (WARN_ON(arg->next_vif == arg->n_vifs))
8788 		return;
8789 
8790 	arg->vifs[arg->next_vif].vif = vif;
8791 	arg->vifs[arg->next_vif].old_ctx = ctx;
8792 	arg->vifs[arg->next_vif].new_ctx = ctx;
8793 	arg->next_vif++;
8794 }
8795 
8796 static void
8797 ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
8798 			     struct ieee80211_chanctx_conf *ctx,
8799 			     u32 changed)
8800 {
8801 	struct ath10k *ar = hw->priv;
8802 	struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
8803 
8804 	mutex_lock(&ar->conf_mutex);
8805 
8806 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8807 		   "mac chanctx change freq %u width %d ptr %pK changed %x\n",
8808 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
8809 
8810 	/* This shouldn't really happen because channel switching should use
8811 	 * switch_vif_chanctx().
8812 	 */
8813 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
8814 		goto unlock;
8815 
8816 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
8817 		ieee80211_iterate_active_interfaces_atomic(
8818 					hw,
8819 					ATH10K_ITER_NORMAL_FLAGS,
8820 					ath10k_mac_change_chanctx_cnt_iter,
8821 					&arg);
8822 		if (arg.n_vifs == 0)
8823 			goto radar;
8824 
8825 		arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
8826 				   GFP_KERNEL);
8827 		if (!arg.vifs)
8828 			goto radar;
8829 
8830 		ieee80211_iterate_active_interfaces_atomic(
8831 					hw,
8832 					ATH10K_ITER_NORMAL_FLAGS,
8833 					ath10k_mac_change_chanctx_fill_iter,
8834 					&arg);
8835 		ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
8836 		kfree(arg.vifs);
8837 	}
8838 
8839 radar:
8840 	ath10k_recalc_radar_detection(ar);
8841 
8842 	/* FIXME: How to configure Rx chains properly? */
8843 
8844 	/* No other actions are actually necessary. Firmware maintains channel
8845 	 * definitions per vdev internally and there's no host-side channel
8846 	 * context abstraction to configure, e.g. channel width.
8847 	 */
8848 
8849 unlock:
8850 	mutex_unlock(&ar->conf_mutex);
8851 }
8852 
8853 static int
8854 ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
8855 				 struct ieee80211_vif *vif,
8856 				 struct ieee80211_chanctx_conf *ctx)
8857 {
8858 	struct ath10k *ar = hw->priv;
8859 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8860 	int ret;
8861 
8862 	mutex_lock(&ar->conf_mutex);
8863 
8864 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8865 		   "mac chanctx assign ptr %pK vdev_id %i\n",
8866 		   ctx, arvif->vdev_id);
8867 
8868 	if (WARN_ON(arvif->is_started)) {
8869 		mutex_unlock(&ar->conf_mutex);
8870 		return -EBUSY;
8871 	}
8872 
8873 	ret = ath10k_vdev_start(arvif, &ctx->def);
8874 	if (ret) {
8875 		ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
8876 			    arvif->vdev_id, vif->addr,
8877 			    ctx->def.chan->center_freq, ret);
8878 		goto err;
8879 	}
8880 
8881 	arvif->is_started = true;
8882 
8883 	ret = ath10k_mac_vif_setup_ps(arvif);
8884 	if (ret) {
8885 		ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
8886 			    arvif->vdev_id, ret);
8887 		goto err_stop;
8888 	}
8889 
8890 	if (vif->type == NL80211_IFTYPE_MONITOR) {
8891 		ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
8892 		if (ret) {
8893 			ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
8894 				    arvif->vdev_id, ret);
8895 			goto err_stop;
8896 		}
8897 
8898 		arvif->is_up = true;
8899 	}
8900 
8901 	if (ath10k_mac_can_set_cts_prot(arvif)) {
8902 		ret = ath10k_mac_set_cts_prot(arvif);
8903 		if (ret)
8904 			ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
8905 				    arvif->vdev_id, ret);
8906 	}
8907 
8908 	if (ath10k_peer_stats_enabled(ar) &&
8909 	    ar->hw_params.tx_stats_over_pktlog) {
8910 		ar->pktlog_filter |= ATH10K_PKTLOG_PEER_STATS;
8911 		ret = ath10k_wmi_pdev_pktlog_enable(ar,
8912 						    ar->pktlog_filter);
8913 		if (ret) {
8914 			ath10k_warn(ar, "failed to enable pktlog %d\n", ret);
8915 			goto err_stop;
8916 		}
8917 	}
8918 
8919 	mutex_unlock(&ar->conf_mutex);
8920 	return 0;
8921 
8922 err_stop:
8923 	ath10k_vdev_stop(arvif);
8924 	arvif->is_started = false;
8925 	ath10k_mac_vif_setup_ps(arvif);
8926 
8927 err:
8928 	mutex_unlock(&ar->conf_mutex);
8929 	return ret;
8930 }
8931 
8932 static void
8933 ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
8934 				   struct ieee80211_vif *vif,
8935 				   struct ieee80211_chanctx_conf *ctx)
8936 {
8937 	struct ath10k *ar = hw->priv;
8938 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8939 	int ret;
8940 
8941 	mutex_lock(&ar->conf_mutex);
8942 
8943 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8944 		   "mac chanctx unassign ptr %pK vdev_id %i\n",
8945 		   ctx, arvif->vdev_id);
8946 
8947 	WARN_ON(!arvif->is_started);
8948 
8949 	if (vif->type == NL80211_IFTYPE_MONITOR) {
8950 		WARN_ON(!arvif->is_up);
8951 
8952 		ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
8953 		if (ret)
8954 			ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
8955 				    arvif->vdev_id, ret);
8956 
8957 		arvif->is_up = false;
8958 	}
8959 
8960 	ret = ath10k_vdev_stop(arvif);
8961 	if (ret)
8962 		ath10k_warn(ar, "failed to stop vdev %i: %d\n",
8963 			    arvif->vdev_id, ret);
8964 
8965 	arvif->is_started = false;
8966 
8967 	mutex_unlock(&ar->conf_mutex);
8968 }
8969 
8970 static int
8971 ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
8972 				 struct ieee80211_vif_chanctx_switch *vifs,
8973 				 int n_vifs,
8974 				 enum ieee80211_chanctx_switch_mode mode)
8975 {
8976 	struct ath10k *ar = hw->priv;
8977 
8978 	mutex_lock(&ar->conf_mutex);
8979 
8980 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8981 		   "mac chanctx switch n_vifs %d mode %d\n",
8982 		   n_vifs, mode);
8983 	ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
8984 
8985 	mutex_unlock(&ar->conf_mutex);
8986 	return 0;
8987 }
8988 
8989 static void ath10k_mac_op_sta_pre_rcu_remove(struct ieee80211_hw *hw,
8990 					     struct ieee80211_vif *vif,
8991 					     struct ieee80211_sta *sta)
8992 {
8993 	struct ath10k *ar;
8994 	struct ath10k_peer *peer;
8995 
8996 	ar = hw->priv;
8997 
8998 	list_for_each_entry(peer, &ar->peers, list)
8999 		if (peer->sta == sta)
9000 			peer->removed = true;
9001 }
9002 
9003 /* HT MCS parameters with Nss = 1 */
9004 static const struct ath10k_index_ht_data_rate_type supported_ht_mcs_rate_nss1[] = {
9005 	/* MCS  L20   L40   S20  S40 */
9006 	{0,  { 65,  135,  72,  150} },
9007 	{1,  { 130, 270,  144, 300} },
9008 	{2,  { 195, 405,  217, 450} },
9009 	{3,  { 260, 540,  289, 600} },
9010 	{4,  { 390, 810,  433, 900} },
9011 	{5,  { 520, 1080, 578, 1200} },
9012 	{6,  { 585, 1215, 650, 1350} },
9013 	{7,  { 650, 1350, 722, 1500} }
9014 };
9015 
9016 /* HT MCS parameters with Nss = 2 */
9017 static const struct ath10k_index_ht_data_rate_type supported_ht_mcs_rate_nss2[] = {
9018 	/* MCS  L20    L40   S20   S40 */
9019 	{0,  {130,  270,  144,  300} },
9020 	{1,  {260,  540,  289,  600} },
9021 	{2,  {390,  810,  433,  900} },
9022 	{3,  {520,  1080, 578,  1200} },
9023 	{4,  {780,  1620, 867,  1800} },
9024 	{5,  {1040, 2160, 1156, 2400} },
9025 	{6,  {1170, 2430, 1300, 2700} },
9026 	{7,  {1300, 2700, 1444, 3000} }
9027 };
9028 
9029 /* MCS parameters with Nss = 1 */
9030 static const struct ath10k_index_vht_data_rate_type supported_vht_mcs_rate_nss1[] = {
9031 	/* MCS  L80    S80     L40   S40    L20   S20 */
9032 	{0,  {293,  325},  {135,  150},  {65,   72} },
9033 	{1,  {585,  650},  {270,  300},  {130,  144} },
9034 	{2,  {878,  975},  {405,  450},  {195,  217} },
9035 	{3,  {1170, 1300}, {540,  600},  {260,  289} },
9036 	{4,  {1755, 1950}, {810,  900},  {390,  433} },
9037 	{5,  {2340, 2600}, {1080, 1200}, {520,  578} },
9038 	{6,  {2633, 2925}, {1215, 1350}, {585,  650} },
9039 	{7,  {2925, 3250}, {1350, 1500}, {650,  722} },
9040 	{8,  {3510, 3900}, {1620, 1800}, {780,  867} },
9041 	{9,  {3900, 4333}, {1800, 2000}, {780,  867} }
9042 };
9043 
9044 /*MCS parameters with Nss = 2 */
9045 static const struct ath10k_index_vht_data_rate_type supported_vht_mcs_rate_nss2[] = {
9046 	/* MCS  L80    S80     L40   S40    L20   S20 */
9047 	{0,  {585,  650},  {270,  300},  {130,  144} },
9048 	{1,  {1170, 1300}, {540,  600},  {260,  289} },
9049 	{2,  {1755, 1950}, {810,  900},  {390,  433} },
9050 	{3,  {2340, 2600}, {1080, 1200}, {520,  578} },
9051 	{4,  {3510, 3900}, {1620, 1800}, {780,  867} },
9052 	{5,  {4680, 5200}, {2160, 2400}, {1040, 1156} },
9053 	{6,  {5265, 5850}, {2430, 2700}, {1170, 1300} },
9054 	{7,  {5850, 6500}, {2700, 3000}, {1300, 1444} },
9055 	{8,  {7020, 7800}, {3240, 3600}, {1560, 1733} },
9056 	{9,  {7800, 8667}, {3600, 4000}, {1560, 1733} }
9057 };
9058 
9059 static void ath10k_mac_get_rate_flags_ht(struct ath10k *ar, u32 rate, u8 nss, u8 mcs,
9060 					 u8 *flags, u8 *bw)
9061 {
9062 	struct ath10k_index_ht_data_rate_type *mcs_rate;
9063 	u8 index;
9064 	size_t len_nss1 = ARRAY_SIZE(supported_ht_mcs_rate_nss1);
9065 	size_t len_nss2 = ARRAY_SIZE(supported_ht_mcs_rate_nss2);
9066 
9067 	if (mcs >= (len_nss1 + len_nss2)) {
9068 		ath10k_warn(ar, "not supported mcs %d in current rate table", mcs);
9069 		return;
9070 	}
9071 
9072 	mcs_rate = (struct ath10k_index_ht_data_rate_type *)
9073 		   ((nss == 1) ? &supported_ht_mcs_rate_nss1 :
9074 		   &supported_ht_mcs_rate_nss2);
9075 
9076 	if (mcs >= len_nss1)
9077 		index = mcs - len_nss1;
9078 	else
9079 		index = mcs;
9080 
9081 	if (rate == mcs_rate[index].supported_rate[0]) {
9082 		*bw = RATE_INFO_BW_20;
9083 	} else if (rate == mcs_rate[index].supported_rate[1]) {
9084 		*bw |= RATE_INFO_BW_40;
9085 	} else if (rate == mcs_rate[index].supported_rate[2]) {
9086 		*bw |= RATE_INFO_BW_20;
9087 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
9088 	} else if (rate == mcs_rate[index].supported_rate[3]) {
9089 		*bw |= RATE_INFO_BW_40;
9090 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
9091 	} else {
9092 		ath10k_warn(ar, "invalid ht params rate %d 100kbps nss %d mcs %d",
9093 			    rate, nss, mcs);
9094 	}
9095 }
9096 
9097 static void ath10k_mac_get_rate_flags_vht(struct ath10k *ar, u32 rate, u8 nss, u8 mcs,
9098 					  u8 *flags, u8 *bw)
9099 {
9100 	struct ath10k_index_vht_data_rate_type *mcs_rate;
9101 
9102 	mcs_rate = (struct ath10k_index_vht_data_rate_type *)
9103 		   ((nss == 1) ? &supported_vht_mcs_rate_nss1 :
9104 		   &supported_vht_mcs_rate_nss2);
9105 
9106 	if (rate == mcs_rate[mcs].supported_VHT80_rate[0]) {
9107 		*bw = RATE_INFO_BW_80;
9108 	} else if (rate == mcs_rate[mcs].supported_VHT80_rate[1]) {
9109 		*bw = RATE_INFO_BW_80;
9110 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
9111 	} else if (rate == mcs_rate[mcs].supported_VHT40_rate[0]) {
9112 		*bw = RATE_INFO_BW_40;
9113 	} else if (rate == mcs_rate[mcs].supported_VHT40_rate[1]) {
9114 		*bw = RATE_INFO_BW_40;
9115 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
9116 	} else if (rate == mcs_rate[mcs].supported_VHT20_rate[0]) {
9117 		*bw = RATE_INFO_BW_20;
9118 	} else if (rate == mcs_rate[mcs].supported_VHT20_rate[1]) {
9119 		*bw = RATE_INFO_BW_20;
9120 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
9121 	} else {
9122 		ath10k_warn(ar, "invalid vht params rate %d 100kbps nss %d mcs %d",
9123 			    rate, nss, mcs);
9124 	}
9125 }
9126 
9127 static void ath10k_mac_get_rate_flags(struct ath10k *ar, u32 rate,
9128 				      enum ath10k_phy_mode mode, u8 nss, u8 mcs,
9129 				      u8 *flags, u8 *bw)
9130 {
9131 	if (mode == ATH10K_PHY_MODE_HT) {
9132 		*flags = RATE_INFO_FLAGS_MCS;
9133 		ath10k_mac_get_rate_flags_ht(ar, rate, nss, mcs, flags, bw);
9134 	} else if (mode == ATH10K_PHY_MODE_VHT) {
9135 		*flags = RATE_INFO_FLAGS_VHT_MCS;
9136 		ath10k_mac_get_rate_flags_vht(ar, rate, nss, mcs, flags, bw);
9137 	}
9138 }
9139 
9140 static void ath10k_mac_parse_bitrate(struct ath10k *ar, u32 rate_code,
9141 				     u32 bitrate_kbps, struct rate_info *rate)
9142 {
9143 	enum ath10k_phy_mode mode = ATH10K_PHY_MODE_LEGACY;
9144 	enum wmi_rate_preamble preamble = WMI_TLV_GET_HW_RC_PREAM_V1(rate_code);
9145 	u8 nss = WMI_TLV_GET_HW_RC_NSS_V1(rate_code) + 1;
9146 	u8 mcs = WMI_TLV_GET_HW_RC_RATE_V1(rate_code);
9147 	u8 flags = 0, bw = 0;
9148 
9149 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac parse rate code 0x%x bitrate %d kbps\n",
9150 		   rate_code, bitrate_kbps);
9151 
9152 	if (preamble == WMI_RATE_PREAMBLE_HT)
9153 		mode = ATH10K_PHY_MODE_HT;
9154 	else if (preamble == WMI_RATE_PREAMBLE_VHT)
9155 		mode = ATH10K_PHY_MODE_VHT;
9156 
9157 	ath10k_mac_get_rate_flags(ar, bitrate_kbps / 100, mode, nss, mcs, &flags, &bw);
9158 
9159 	ath10k_dbg(ar, ATH10K_DBG_MAC,
9160 		   "mac parse bitrate preamble %d mode %d nss %d mcs %d flags %x bw %d\n",
9161 		   preamble, mode, nss, mcs, flags, bw);
9162 
9163 	rate->flags = flags;
9164 	rate->bw = bw;
9165 	rate->legacy = bitrate_kbps / 100;
9166 	rate->nss = nss;
9167 	rate->mcs = mcs;
9168 }
9169 
9170 static void ath10k_mac_sta_get_peer_stats_info(struct ath10k *ar,
9171 					       struct ieee80211_sta *sta,
9172 					       struct station_info *sinfo)
9173 {
9174 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
9175 	struct ath10k_peer *peer;
9176 	unsigned long time_left;
9177 	int ret;
9178 
9179 	if (!(ar->hw_params.supports_peer_stats_info &&
9180 	      arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA))
9181 		return;
9182 
9183 	spin_lock_bh(&ar->data_lock);
9184 	peer = ath10k_peer_find(ar, arsta->arvif->vdev_id, sta->addr);
9185 	spin_unlock_bh(&ar->data_lock);
9186 	if (!peer)
9187 		return;
9188 
9189 	reinit_completion(&ar->peer_stats_info_complete);
9190 
9191 	ret = ath10k_wmi_request_peer_stats_info(ar,
9192 						 arsta->arvif->vdev_id,
9193 						 WMI_REQUEST_ONE_PEER_STATS_INFO,
9194 						 arsta->arvif->bssid,
9195 						 0);
9196 	if (ret && ret != -EOPNOTSUPP) {
9197 		ath10k_warn(ar, "could not request peer stats info: %d\n", ret);
9198 		return;
9199 	}
9200 
9201 	time_left = wait_for_completion_timeout(&ar->peer_stats_info_complete, 3 * HZ);
9202 	if (time_left == 0) {
9203 		ath10k_warn(ar, "timed out waiting peer stats info\n");
9204 		return;
9205 	}
9206 
9207 	if (arsta->rx_rate_code != 0 && arsta->rx_bitrate_kbps != 0) {
9208 		ath10k_mac_parse_bitrate(ar, arsta->rx_rate_code,
9209 					 arsta->rx_bitrate_kbps,
9210 					 &sinfo->rxrate);
9211 
9212 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
9213 		arsta->rx_rate_code = 0;
9214 		arsta->rx_bitrate_kbps = 0;
9215 	}
9216 
9217 	if (arsta->tx_rate_code != 0 && arsta->tx_bitrate_kbps != 0) {
9218 		ath10k_mac_parse_bitrate(ar, arsta->tx_rate_code,
9219 					 arsta->tx_bitrate_kbps,
9220 					 &sinfo->txrate);
9221 
9222 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
9223 		arsta->tx_rate_code = 0;
9224 		arsta->tx_bitrate_kbps = 0;
9225 	}
9226 }
9227 
9228 static void ath10k_sta_statistics(struct ieee80211_hw *hw,
9229 				  struct ieee80211_vif *vif,
9230 				  struct ieee80211_sta *sta,
9231 				  struct station_info *sinfo)
9232 {
9233 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
9234 	struct ath10k *ar = arsta->arvif->ar;
9235 
9236 	if (!ath10k_peer_stats_enabled(ar))
9237 		return;
9238 
9239 	mutex_lock(&ar->conf_mutex);
9240 	ath10k_debug_fw_stats_request(ar);
9241 	mutex_unlock(&ar->conf_mutex);
9242 
9243 	sinfo->rx_duration = arsta->rx_duration;
9244 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
9245 
9246 	if (arsta->txrate.legacy || arsta->txrate.nss) {
9247 		if (arsta->txrate.legacy) {
9248 			sinfo->txrate.legacy = arsta->txrate.legacy;
9249 		} else {
9250 			sinfo->txrate.mcs = arsta->txrate.mcs;
9251 			sinfo->txrate.nss = arsta->txrate.nss;
9252 			sinfo->txrate.bw = arsta->txrate.bw;
9253 		}
9254 		sinfo->txrate.flags = arsta->txrate.flags;
9255 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
9256 	}
9257 
9258 	if (ar->htt.disable_tx_comp) {
9259 		sinfo->tx_failed = arsta->tx_failed;
9260 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
9261 	}
9262 
9263 	sinfo->tx_retries = arsta->tx_retries;
9264 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
9265 
9266 	ath10k_mac_sta_get_peer_stats_info(ar, sta, sinfo);
9267 }
9268 
9269 static int ath10k_mac_op_set_tid_config(struct ieee80211_hw *hw,
9270 					struct ieee80211_vif *vif,
9271 					struct ieee80211_sta *sta,
9272 					struct cfg80211_tid_config *tid_config)
9273 {
9274 	struct ath10k *ar = hw->priv;
9275 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
9276 	struct ath10k_mac_iter_tid_conf_data data = {};
9277 	struct wmi_per_peer_per_tid_cfg_arg arg = {};
9278 	int ret, i;
9279 
9280 	mutex_lock(&ar->conf_mutex);
9281 	arg.vdev_id = arvif->vdev_id;
9282 
9283 	arvif->tids_rst = 0;
9284 	memset(arvif->tid_conf_changed, 0, sizeof(arvif->tid_conf_changed));
9285 
9286 	for (i = 0; i < tid_config->n_tid_conf; i++) {
9287 		ret = ath10k_mac_parse_tid_config(ar, sta, vif,
9288 						  &tid_config->tid_conf[i],
9289 						  &arg);
9290 		if (ret)
9291 			goto exit;
9292 	}
9293 
9294 	ret = 0;
9295 
9296 	if (sta)
9297 		goto exit;
9298 
9299 	arvif->tids_rst = 0;
9300 	data.curr_vif = vif;
9301 	data.ar = ar;
9302 
9303 	ieee80211_iterate_stations_atomic(hw, ath10k_mac_vif_stations_tid_conf,
9304 					  &data);
9305 
9306 exit:
9307 	mutex_unlock(&ar->conf_mutex);
9308 	return ret;
9309 }
9310 
9311 static int ath10k_mac_op_reset_tid_config(struct ieee80211_hw *hw,
9312 					  struct ieee80211_vif *vif,
9313 					  struct ieee80211_sta *sta,
9314 					  u8 tids)
9315 {
9316 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
9317 	struct ath10k_mac_iter_tid_conf_data data = {};
9318 	struct ath10k *ar = hw->priv;
9319 	int ret = 0;
9320 
9321 	mutex_lock(&ar->conf_mutex);
9322 
9323 	if (sta) {
9324 		arvif->tids_rst = 0;
9325 		ret = ath10k_mac_reset_tid_config(ar, sta, arvif, tids);
9326 		goto exit;
9327 	}
9328 
9329 	arvif->tids_rst = tids;
9330 	data.curr_vif = vif;
9331 	data.ar = ar;
9332 	ieee80211_iterate_stations_atomic(hw, ath10k_mac_vif_stations_tid_conf,
9333 					  &data);
9334 
9335 exit:
9336 	mutex_unlock(&ar->conf_mutex);
9337 	return ret;
9338 }
9339 
9340 static const struct ieee80211_ops ath10k_ops = {
9341 	.tx				= ath10k_mac_op_tx,
9342 	.wake_tx_queue			= ath10k_mac_op_wake_tx_queue,
9343 	.start				= ath10k_start,
9344 	.stop				= ath10k_stop,
9345 	.config				= ath10k_config,
9346 	.add_interface			= ath10k_add_interface,
9347 	.remove_interface		= ath10k_remove_interface,
9348 	.configure_filter		= ath10k_configure_filter,
9349 	.bss_info_changed		= ath10k_bss_info_changed,
9350 	.set_coverage_class		= ath10k_mac_op_set_coverage_class,
9351 	.hw_scan			= ath10k_hw_scan,
9352 	.cancel_hw_scan			= ath10k_cancel_hw_scan,
9353 	.set_key			= ath10k_set_key,
9354 	.set_default_unicast_key        = ath10k_set_default_unicast_key,
9355 	.sta_state			= ath10k_sta_state,
9356 	.sta_set_txpwr			= ath10k_sta_set_txpwr,
9357 	.conf_tx			= ath10k_conf_tx,
9358 	.remain_on_channel		= ath10k_remain_on_channel,
9359 	.cancel_remain_on_channel	= ath10k_cancel_remain_on_channel,
9360 	.set_rts_threshold		= ath10k_set_rts_threshold,
9361 	.set_frag_threshold		= ath10k_mac_op_set_frag_threshold,
9362 	.flush				= ath10k_flush,
9363 	.tx_last_beacon			= ath10k_tx_last_beacon,
9364 	.set_antenna			= ath10k_set_antenna,
9365 	.get_antenna			= ath10k_get_antenna,
9366 	.reconfig_complete		= ath10k_reconfig_complete,
9367 	.get_survey			= ath10k_get_survey,
9368 	.set_bitrate_mask		= ath10k_mac_op_set_bitrate_mask,
9369 	.sta_rc_update			= ath10k_sta_rc_update,
9370 	.offset_tsf			= ath10k_offset_tsf,
9371 	.ampdu_action			= ath10k_ampdu_action,
9372 	.get_et_sset_count		= ath10k_debug_get_et_sset_count,
9373 	.get_et_stats			= ath10k_debug_get_et_stats,
9374 	.get_et_strings			= ath10k_debug_get_et_strings,
9375 	.add_chanctx			= ath10k_mac_op_add_chanctx,
9376 	.remove_chanctx			= ath10k_mac_op_remove_chanctx,
9377 	.change_chanctx			= ath10k_mac_op_change_chanctx,
9378 	.assign_vif_chanctx		= ath10k_mac_op_assign_vif_chanctx,
9379 	.unassign_vif_chanctx		= ath10k_mac_op_unassign_vif_chanctx,
9380 	.switch_vif_chanctx		= ath10k_mac_op_switch_vif_chanctx,
9381 	.sta_pre_rcu_remove		= ath10k_mac_op_sta_pre_rcu_remove,
9382 	.sta_statistics			= ath10k_sta_statistics,
9383 	.set_tid_config			= ath10k_mac_op_set_tid_config,
9384 	.reset_tid_config		= ath10k_mac_op_reset_tid_config,
9385 
9386 	CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
9387 
9388 #ifdef CONFIG_PM
9389 	.suspend			= ath10k_wow_op_suspend,
9390 	.resume				= ath10k_wow_op_resume,
9391 	.set_wakeup			= ath10k_wow_op_set_wakeup,
9392 #endif
9393 #ifdef CONFIG_MAC80211_DEBUGFS
9394 	.sta_add_debugfs		= ath10k_sta_add_debugfs,
9395 #endif
9396 	.set_sar_specs			= ath10k_mac_set_sar_specs,
9397 };
9398 
9399 #define CHAN2G(_channel, _freq, _flags) { \
9400 	.band			= NL80211_BAND_2GHZ, \
9401 	.hw_value		= (_channel), \
9402 	.center_freq		= (_freq), \
9403 	.flags			= (_flags), \
9404 	.max_antenna_gain	= 0, \
9405 	.max_power		= 30, \
9406 }
9407 
9408 #define CHAN5G(_channel, _freq, _flags) { \
9409 	.band			= NL80211_BAND_5GHZ, \
9410 	.hw_value		= (_channel), \
9411 	.center_freq		= (_freq), \
9412 	.flags			= (_flags), \
9413 	.max_antenna_gain	= 0, \
9414 	.max_power		= 30, \
9415 }
9416 
9417 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
9418 	CHAN2G(1, 2412, 0),
9419 	CHAN2G(2, 2417, 0),
9420 	CHAN2G(3, 2422, 0),
9421 	CHAN2G(4, 2427, 0),
9422 	CHAN2G(5, 2432, 0),
9423 	CHAN2G(6, 2437, 0),
9424 	CHAN2G(7, 2442, 0),
9425 	CHAN2G(8, 2447, 0),
9426 	CHAN2G(9, 2452, 0),
9427 	CHAN2G(10, 2457, 0),
9428 	CHAN2G(11, 2462, 0),
9429 	CHAN2G(12, 2467, 0),
9430 	CHAN2G(13, 2472, 0),
9431 	CHAN2G(14, 2484, 0),
9432 };
9433 
9434 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
9435 	CHAN5G(36, 5180, 0),
9436 	CHAN5G(40, 5200, 0),
9437 	CHAN5G(44, 5220, 0),
9438 	CHAN5G(48, 5240, 0),
9439 	CHAN5G(52, 5260, 0),
9440 	CHAN5G(56, 5280, 0),
9441 	CHAN5G(60, 5300, 0),
9442 	CHAN5G(64, 5320, 0),
9443 	CHAN5G(100, 5500, 0),
9444 	CHAN5G(104, 5520, 0),
9445 	CHAN5G(108, 5540, 0),
9446 	CHAN5G(112, 5560, 0),
9447 	CHAN5G(116, 5580, 0),
9448 	CHAN5G(120, 5600, 0),
9449 	CHAN5G(124, 5620, 0),
9450 	CHAN5G(128, 5640, 0),
9451 	CHAN5G(132, 5660, 0),
9452 	CHAN5G(136, 5680, 0),
9453 	CHAN5G(140, 5700, 0),
9454 	CHAN5G(144, 5720, 0),
9455 	CHAN5G(149, 5745, 0),
9456 	CHAN5G(153, 5765, 0),
9457 	CHAN5G(157, 5785, 0),
9458 	CHAN5G(161, 5805, 0),
9459 	CHAN5G(165, 5825, 0),
9460 	CHAN5G(169, 5845, 0),
9461 	CHAN5G(173, 5865, 0),
9462 	/* If you add more, you may need to change ATH10K_MAX_5G_CHAN */
9463 	/* And you will definitely need to change ATH10K_NUM_CHANS in core.h */
9464 };
9465 
9466 struct ath10k *ath10k_mac_create(size_t priv_size)
9467 {
9468 	struct ieee80211_hw *hw;
9469 	struct ieee80211_ops *ops;
9470 	struct ath10k *ar;
9471 
9472 	ops = kmemdup(&ath10k_ops, sizeof(ath10k_ops), GFP_KERNEL);
9473 	if (!ops)
9474 		return NULL;
9475 
9476 	hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, ops);
9477 	if (!hw) {
9478 		kfree(ops);
9479 		return NULL;
9480 	}
9481 
9482 	ar = hw->priv;
9483 	ar->hw = hw;
9484 	ar->ops = ops;
9485 
9486 	return ar;
9487 }
9488 
9489 void ath10k_mac_destroy(struct ath10k *ar)
9490 {
9491 	struct ieee80211_ops *ops = ar->ops;
9492 
9493 	ieee80211_free_hw(ar->hw);
9494 	kfree(ops);
9495 }
9496 
9497 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
9498 	{
9499 		.max	= 8,
9500 		.types	= BIT(NL80211_IFTYPE_STATION)
9501 			| BIT(NL80211_IFTYPE_P2P_CLIENT)
9502 	},
9503 	{
9504 		.max	= 3,
9505 		.types	= BIT(NL80211_IFTYPE_P2P_GO)
9506 	},
9507 	{
9508 		.max	= 1,
9509 		.types	= BIT(NL80211_IFTYPE_P2P_DEVICE)
9510 	},
9511 	{
9512 		.max	= 7,
9513 		.types	= BIT(NL80211_IFTYPE_AP)
9514 #ifdef CONFIG_MAC80211_MESH
9515 			| BIT(NL80211_IFTYPE_MESH_POINT)
9516 #endif
9517 	},
9518 };
9519 
9520 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
9521 	{
9522 		.max	= 8,
9523 		.types	= BIT(NL80211_IFTYPE_AP)
9524 #ifdef CONFIG_MAC80211_MESH
9525 			| BIT(NL80211_IFTYPE_MESH_POINT)
9526 #endif
9527 	},
9528 	{
9529 		.max	= 1,
9530 		.types	= BIT(NL80211_IFTYPE_STATION)
9531 	},
9532 };
9533 
9534 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
9535 	{
9536 		.limits = ath10k_if_limits,
9537 		.n_limits = ARRAY_SIZE(ath10k_if_limits),
9538 		.max_interfaces = 8,
9539 		.num_different_channels = 1,
9540 		.beacon_int_infra_match = true,
9541 	},
9542 };
9543 
9544 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
9545 	{
9546 		.limits = ath10k_10x_if_limits,
9547 		.n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
9548 		.max_interfaces = 8,
9549 		.num_different_channels = 1,
9550 		.beacon_int_infra_match = true,
9551 		.beacon_int_min_gcd = 1,
9552 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
9553 		.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9554 					BIT(NL80211_CHAN_WIDTH_20) |
9555 					BIT(NL80211_CHAN_WIDTH_40) |
9556 					BIT(NL80211_CHAN_WIDTH_80),
9557 #endif
9558 	},
9559 };
9560 
9561 static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
9562 	{
9563 		.max = 2,
9564 		.types = BIT(NL80211_IFTYPE_STATION),
9565 	},
9566 	{
9567 		.max = 2,
9568 		.types = BIT(NL80211_IFTYPE_AP) |
9569 #ifdef CONFIG_MAC80211_MESH
9570 			 BIT(NL80211_IFTYPE_MESH_POINT) |
9571 #endif
9572 			 BIT(NL80211_IFTYPE_P2P_CLIENT) |
9573 			 BIT(NL80211_IFTYPE_P2P_GO),
9574 	},
9575 	{
9576 		.max = 1,
9577 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
9578 	},
9579 };
9580 
9581 static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
9582 	{
9583 		.max = 2,
9584 		.types = BIT(NL80211_IFTYPE_STATION),
9585 	},
9586 	{
9587 		.max = 2,
9588 		.types = BIT(NL80211_IFTYPE_P2P_CLIENT),
9589 	},
9590 	{
9591 		.max = 1,
9592 		.types = BIT(NL80211_IFTYPE_AP) |
9593 #ifdef CONFIG_MAC80211_MESH
9594 			 BIT(NL80211_IFTYPE_MESH_POINT) |
9595 #endif
9596 			 BIT(NL80211_IFTYPE_P2P_GO),
9597 	},
9598 	{
9599 		.max = 1,
9600 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
9601 	},
9602 };
9603 
9604 static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
9605 	{
9606 		.max = 1,
9607 		.types = BIT(NL80211_IFTYPE_STATION),
9608 	},
9609 	{
9610 		.max = 1,
9611 		.types = BIT(NL80211_IFTYPE_ADHOC),
9612 	},
9613 };
9614 
9615 /* FIXME: This is not thouroughly tested. These combinations may over- or
9616  * underestimate hw/fw capabilities.
9617  */
9618 static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
9619 	{
9620 		.limits = ath10k_tlv_if_limit,
9621 		.num_different_channels = 1,
9622 		.max_interfaces = 4,
9623 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
9624 	},
9625 	{
9626 		.limits = ath10k_tlv_if_limit_ibss,
9627 		.num_different_channels = 1,
9628 		.max_interfaces = 2,
9629 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
9630 	},
9631 };
9632 
9633 static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
9634 	{
9635 		.limits = ath10k_tlv_if_limit,
9636 		.num_different_channels = 1,
9637 		.max_interfaces = 4,
9638 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
9639 	},
9640 	{
9641 		.limits = ath10k_tlv_qcs_if_limit,
9642 		.num_different_channels = 2,
9643 		.max_interfaces = 4,
9644 		.n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
9645 	},
9646 	{
9647 		.limits = ath10k_tlv_if_limit_ibss,
9648 		.num_different_channels = 1,
9649 		.max_interfaces = 2,
9650 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
9651 	},
9652 };
9653 
9654 static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
9655 	{
9656 		.max = 1,
9657 		.types = BIT(NL80211_IFTYPE_STATION),
9658 	},
9659 	{
9660 		.max	= 16,
9661 		.types	= BIT(NL80211_IFTYPE_AP)
9662 #ifdef CONFIG_MAC80211_MESH
9663 			| BIT(NL80211_IFTYPE_MESH_POINT)
9664 #endif
9665 	},
9666 };
9667 
9668 static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
9669 	{
9670 		.limits = ath10k_10_4_if_limits,
9671 		.n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
9672 		.max_interfaces = 16,
9673 		.num_different_channels = 1,
9674 		.beacon_int_infra_match = true,
9675 		.beacon_int_min_gcd = 1,
9676 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
9677 		.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9678 					BIT(NL80211_CHAN_WIDTH_20) |
9679 					BIT(NL80211_CHAN_WIDTH_40) |
9680 					BIT(NL80211_CHAN_WIDTH_80) |
9681 					BIT(NL80211_CHAN_WIDTH_80P80) |
9682 					BIT(NL80211_CHAN_WIDTH_160),
9683 #endif
9684 	},
9685 };
9686 
9687 static const struct
9688 ieee80211_iface_combination ath10k_10_4_bcn_int_if_comb[] = {
9689 	{
9690 		.limits = ath10k_10_4_if_limits,
9691 		.n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
9692 		.max_interfaces = 16,
9693 		.num_different_channels = 1,
9694 		.beacon_int_infra_match = true,
9695 		.beacon_int_min_gcd = 100,
9696 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
9697 		.radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9698 					BIT(NL80211_CHAN_WIDTH_20) |
9699 					BIT(NL80211_CHAN_WIDTH_40) |
9700 					BIT(NL80211_CHAN_WIDTH_80) |
9701 					BIT(NL80211_CHAN_WIDTH_80P80) |
9702 					BIT(NL80211_CHAN_WIDTH_160),
9703 #endif
9704 	},
9705 };
9706 
9707 static void ath10k_get_arvif_iter(void *data, u8 *mac,
9708 				  struct ieee80211_vif *vif)
9709 {
9710 	struct ath10k_vif_iter *arvif_iter = data;
9711 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
9712 
9713 	if (arvif->vdev_id == arvif_iter->vdev_id)
9714 		arvif_iter->arvif = arvif;
9715 }
9716 
9717 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
9718 {
9719 	struct ath10k_vif_iter arvif_iter;
9720 
9721 	memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
9722 	arvif_iter.vdev_id = vdev_id;
9723 
9724 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
9725 						   ATH10K_ITER_RESUME_FLAGS,
9726 						   ath10k_get_arvif_iter,
9727 						   &arvif_iter);
9728 	if (!arvif_iter.arvif) {
9729 		ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
9730 		return NULL;
9731 	}
9732 
9733 	return arvif_iter.arvif;
9734 }
9735 
9736 #define WRD_METHOD "WRDD"
9737 #define WRDD_WIFI  (0x07)
9738 
9739 static u32 ath10k_mac_wrdd_get_mcc(struct ath10k *ar, union acpi_object *wrdd)
9740 {
9741 	union acpi_object *mcc_pkg;
9742 	union acpi_object *domain_type;
9743 	union acpi_object *mcc_value;
9744 	u32 i;
9745 
9746 	if (wrdd->type != ACPI_TYPE_PACKAGE ||
9747 	    wrdd->package.count < 2 ||
9748 	    wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
9749 	    wrdd->package.elements[0].integer.value != 0) {
9750 		ath10k_warn(ar, "ignoring malformed/unsupported wrdd structure\n");
9751 		return 0;
9752 	}
9753 
9754 	for (i = 1; i < wrdd->package.count; ++i) {
9755 		mcc_pkg = &wrdd->package.elements[i];
9756 
9757 		if (mcc_pkg->type != ACPI_TYPE_PACKAGE)
9758 			continue;
9759 		if (mcc_pkg->package.count < 2)
9760 			continue;
9761 		if (mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
9762 		    mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER)
9763 			continue;
9764 
9765 		domain_type = &mcc_pkg->package.elements[0];
9766 		if (domain_type->integer.value != WRDD_WIFI)
9767 			continue;
9768 
9769 		mcc_value = &mcc_pkg->package.elements[1];
9770 		return mcc_value->integer.value;
9771 	}
9772 	return 0;
9773 }
9774 
9775 static int ath10k_mac_get_wrdd_regulatory(struct ath10k *ar, u16 *rd)
9776 {
9777 	acpi_handle root_handle;
9778 	acpi_handle handle;
9779 	struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
9780 	acpi_status status;
9781 	u32 alpha2_code;
9782 	char alpha2[3];
9783 
9784 	root_handle = ACPI_HANDLE(ar->dev);
9785 	if (!root_handle)
9786 		return -EOPNOTSUPP;
9787 
9788 	status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
9789 	if (ACPI_FAILURE(status)) {
9790 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
9791 			   "failed to get wrd method %d\n", status);
9792 		return -EIO;
9793 	}
9794 
9795 	status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
9796 	if (ACPI_FAILURE(status)) {
9797 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
9798 			   "failed to call wrdc %d\n", status);
9799 		return -EIO;
9800 	}
9801 
9802 	alpha2_code = ath10k_mac_wrdd_get_mcc(ar, wrdd.pointer);
9803 	kfree(wrdd.pointer);
9804 	if (!alpha2_code)
9805 		return -EIO;
9806 
9807 	alpha2[0] = (alpha2_code >> 8) & 0xff;
9808 	alpha2[1] = (alpha2_code >> 0) & 0xff;
9809 	alpha2[2] = '\0';
9810 
9811 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
9812 		   "regulatory hint from WRDD (alpha2-code): %s\n", alpha2);
9813 
9814 	*rd = ath_regd_find_country_by_name(alpha2);
9815 	if (*rd == 0xffff)
9816 		return -EIO;
9817 
9818 	*rd |= COUNTRY_ERD_FLAG;
9819 	return 0;
9820 }
9821 
9822 static int ath10k_mac_init_rd(struct ath10k *ar)
9823 {
9824 	int ret;
9825 	u16 rd;
9826 
9827 	ret = ath10k_mac_get_wrdd_regulatory(ar, &rd);
9828 	if (ret) {
9829 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
9830 			   "fallback to eeprom programmed regulatory settings\n");
9831 		rd = ar->hw_eeprom_rd;
9832 	}
9833 
9834 	ar->ath_common.regulatory.current_rd = rd;
9835 	return 0;
9836 }
9837 
9838 int ath10k_mac_register(struct ath10k *ar)
9839 {
9840 	static const u32 cipher_suites[] = {
9841 		WLAN_CIPHER_SUITE_WEP40,
9842 		WLAN_CIPHER_SUITE_WEP104,
9843 		WLAN_CIPHER_SUITE_TKIP,
9844 		WLAN_CIPHER_SUITE_CCMP,
9845 
9846 		/* Do not add hardware supported ciphers before this line.
9847 		 * Allow software encryption for all chips. Don't forget to
9848 		 * update n_cipher_suites below.
9849 		 */
9850 		WLAN_CIPHER_SUITE_AES_CMAC,
9851 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
9852 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
9853 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
9854 
9855 		/* Only QCA99x0 and QCA4019 varients support GCMP-128, GCMP-256
9856 		 * and CCMP-256 in hardware.
9857 		 */
9858 		WLAN_CIPHER_SUITE_GCMP,
9859 		WLAN_CIPHER_SUITE_GCMP_256,
9860 		WLAN_CIPHER_SUITE_CCMP_256,
9861 	};
9862 	struct ieee80211_supported_band *band;
9863 	void *channels;
9864 	int ret;
9865 
9866 	if (!is_valid_ether_addr(ar->mac_addr)) {
9867 		ath10k_warn(ar, "invalid MAC address; choosing random\n");
9868 		eth_random_addr(ar->mac_addr);
9869 	}
9870 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
9871 
9872 	SET_IEEE80211_DEV(ar->hw, ar->dev);
9873 
9874 	BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
9875 		      ARRAY_SIZE(ath10k_5ghz_channels)) !=
9876 		     ATH10K_NUM_CHANS);
9877 
9878 	if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
9879 		channels = kmemdup(ath10k_2ghz_channels,
9880 				   sizeof(ath10k_2ghz_channels),
9881 				   GFP_KERNEL);
9882 		if (!channels) {
9883 			ret = -ENOMEM;
9884 			goto err_free;
9885 		}
9886 
9887 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
9888 		band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
9889 		band->channels = channels;
9890 
9891 		if (ar->hw_params.cck_rate_map_rev2) {
9892 			band->n_bitrates = ath10k_g_rates_rev2_size;
9893 			band->bitrates = ath10k_g_rates_rev2;
9894 		} else {
9895 			band->n_bitrates = ath10k_g_rates_size;
9896 			band->bitrates = ath10k_g_rates;
9897 		}
9898 
9899 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
9900 	}
9901 
9902 	if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
9903 		channels = kmemdup(ath10k_5ghz_channels,
9904 				   sizeof(ath10k_5ghz_channels),
9905 				   GFP_KERNEL);
9906 		if (!channels) {
9907 			ret = -ENOMEM;
9908 			goto err_free;
9909 		}
9910 
9911 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
9912 		band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
9913 		band->channels = channels;
9914 		band->n_bitrates = ath10k_a_rates_size;
9915 		band->bitrates = ath10k_a_rates;
9916 		ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
9917 	}
9918 
9919 	wiphy_read_of_freq_limits(ar->hw->wiphy);
9920 	ath10k_mac_setup_ht_vht_cap(ar);
9921 
9922 	ar->hw->wiphy->interface_modes =
9923 		BIT(NL80211_IFTYPE_STATION) |
9924 		BIT(NL80211_IFTYPE_AP) |
9925 		BIT(NL80211_IFTYPE_MESH_POINT);
9926 
9927 	ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
9928 	ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
9929 
9930 	if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->normal_mode_fw.fw_file.fw_features))
9931 		ar->hw->wiphy->interface_modes |=
9932 			BIT(NL80211_IFTYPE_P2P_DEVICE) |
9933 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
9934 			BIT(NL80211_IFTYPE_P2P_GO);
9935 
9936 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
9937 
9938 	if (!test_bit(ATH10K_FW_FEATURE_NO_PS,
9939 		      ar->running_fw->fw_file.fw_features)) {
9940 		ieee80211_hw_set(ar->hw, SUPPORTS_PS);
9941 		ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
9942 	}
9943 
9944 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
9945 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
9946 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
9947 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
9948 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
9949 	ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
9950 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
9951 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
9952 	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
9953 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
9954 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
9955 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
9956 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
9957 
9958 	if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
9959 		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
9960 
9961 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
9962 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
9963 
9964 	if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
9965 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
9966 
9967 	if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
9968 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
9969 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
9970 	}
9971 
9972 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
9973 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
9974 
9975 	if (test_bit(WMI_SERVICE_NLO, ar->wmi.svc_map)) {
9976 		ar->hw->wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
9977 		ar->hw->wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
9978 		ar->hw->wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
9979 		ar->hw->wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
9980 		ar->hw->wiphy->max_sched_scan_plan_interval =
9981 			WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
9982 		ar->hw->wiphy->max_sched_scan_plan_iterations =
9983 			WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
9984 		ar->hw->wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9985 	}
9986 
9987 	ar->hw->vif_data_size = sizeof(struct ath10k_vif);
9988 	ar->hw->sta_data_size = sizeof(struct ath10k_sta);
9989 	ar->hw->txq_data_size = sizeof(struct ath10k_txq);
9990 
9991 	ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
9992 
9993 	if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
9994 		ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
9995 
9996 		/* Firmware delivers WPS/P2P Probe Requests frames to driver so
9997 		 * that userspace (e.g. wpa_supplicant/hostapd) can generate
9998 		 * correct Probe Responses. This is more of a hack advert..
9999 		 */
10000 		ar->hw->wiphy->probe_resp_offload |=
10001 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
10002 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
10003 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
10004 	}
10005 
10006 	if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map) ||
10007 	    test_bit(WMI_SERVICE_TDLS_EXPLICIT_MODE_ONLY, ar->wmi.svc_map)) {
10008 		ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
10009 		if (test_bit(WMI_SERVICE_TDLS_WIDER_BANDWIDTH, ar->wmi.svc_map))
10010 			ieee80211_hw_set(ar->hw, TDLS_WIDER_BW);
10011 	}
10012 
10013 	if (test_bit(WMI_SERVICE_TDLS_UAPSD_BUFFER_STA, ar->wmi.svc_map))
10014 		ieee80211_hw_set(ar->hw, SUPPORTS_TDLS_BUFFER_STA);
10015 
10016 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
10017 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
10018 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
10019 
10020 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
10021 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
10022 				   NL80211_FEATURE_AP_SCAN;
10023 
10024 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
10025 
10026 	ret = ath10k_wow_init(ar);
10027 	if (ret) {
10028 		ath10k_warn(ar, "failed to init wow: %d\n", ret);
10029 		goto err_free;
10030 	}
10031 
10032 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
10033 	wiphy_ext_feature_set(ar->hw->wiphy,
10034 			      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
10035 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_AQL);
10036 
10037 	if (test_bit(WMI_SERVICE_TX_DATA_ACK_RSSI, ar->wmi.svc_map) ||
10038 	    test_bit(WMI_SERVICE_HTT_MGMT_TX_COMP_VALID_FLAGS, ar->wmi.svc_map))
10039 		wiphy_ext_feature_set(ar->hw->wiphy,
10040 				      NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
10041 
10042 	if (ath10k_peer_stats_enabled(ar) ||
10043 	    test_bit(WMI_SERVICE_REPORT_AIRTIME, ar->wmi.svc_map))
10044 		wiphy_ext_feature_set(ar->hw->wiphy,
10045 				      NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
10046 
10047 	if (test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map))
10048 		wiphy_ext_feature_set(ar->hw->wiphy,
10049 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
10050 
10051 	if (test_bit(WMI_SERVICE_TX_PWR_PER_PEER, ar->wmi.svc_map))
10052 		wiphy_ext_feature_set(ar->hw->wiphy,
10053 				      NL80211_EXT_FEATURE_STA_TX_PWR);
10054 
10055 	if (test_bit(WMI_SERVICE_PEER_TID_CONFIGS_SUPPORT, ar->wmi.svc_map)) {
10056 		ar->hw->wiphy->tid_config_support.vif |=
10057 				BIT(NL80211_TID_CONFIG_ATTR_NOACK) |
10058 				BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT) |
10059 				BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG) |
10060 				BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL) |
10061 				BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
10062 				BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
10063 
10064 		if (test_bit(WMI_SERVICE_EXT_PEER_TID_CONFIGS_SUPPORT,
10065 			     ar->wmi.svc_map)) {
10066 			ar->hw->wiphy->tid_config_support.vif |=
10067 				BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
10068 		}
10069 
10070 		ar->hw->wiphy->tid_config_support.peer =
10071 				ar->hw->wiphy->tid_config_support.vif;
10072 		ar->hw->wiphy->max_data_retry_count = ATH10K_MAX_RETRY_COUNT;
10073 	} else {
10074 		ar->ops->set_tid_config = NULL;
10075 	}
10076 	/*
10077 	 * on LL hardware queues are managed entirely by the FW
10078 	 * so we only advertise to mac we can do the queues thing
10079 	 */
10080 	ar->hw->queues = IEEE80211_MAX_QUEUES;
10081 
10082 	/* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
10083 	 * something that vdev_ids can't reach so that we don't stop the queue
10084 	 * accidentally.
10085 	 */
10086 	ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
10087 
10088 	switch (ar->running_fw->fw_file.wmi_op_version) {
10089 	case ATH10K_FW_WMI_OP_VERSION_MAIN:
10090 		ar->hw->wiphy->iface_combinations = ath10k_if_comb;
10091 		ar->hw->wiphy->n_iface_combinations =
10092 			ARRAY_SIZE(ath10k_if_comb);
10093 		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
10094 		break;
10095 	case ATH10K_FW_WMI_OP_VERSION_TLV:
10096 		if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
10097 			ar->hw->wiphy->iface_combinations =
10098 				ath10k_tlv_qcs_if_comb;
10099 			ar->hw->wiphy->n_iface_combinations =
10100 				ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
10101 		} else {
10102 			ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
10103 			ar->hw->wiphy->n_iface_combinations =
10104 				ARRAY_SIZE(ath10k_tlv_if_comb);
10105 		}
10106 		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
10107 		break;
10108 	case ATH10K_FW_WMI_OP_VERSION_10_1:
10109 	case ATH10K_FW_WMI_OP_VERSION_10_2:
10110 	case ATH10K_FW_WMI_OP_VERSION_10_2_4:
10111 		ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
10112 		ar->hw->wiphy->n_iface_combinations =
10113 			ARRAY_SIZE(ath10k_10x_if_comb);
10114 		break;
10115 	case ATH10K_FW_WMI_OP_VERSION_10_4:
10116 		ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
10117 		ar->hw->wiphy->n_iface_combinations =
10118 			ARRAY_SIZE(ath10k_10_4_if_comb);
10119 		if (test_bit(WMI_SERVICE_VDEV_DIFFERENT_BEACON_INTERVAL_SUPPORT,
10120 			     ar->wmi.svc_map)) {
10121 			ar->hw->wiphy->iface_combinations =
10122 				ath10k_10_4_bcn_int_if_comb;
10123 			ar->hw->wiphy->n_iface_combinations =
10124 				ARRAY_SIZE(ath10k_10_4_bcn_int_if_comb);
10125 		}
10126 		break;
10127 	case ATH10K_FW_WMI_OP_VERSION_UNSET:
10128 	case ATH10K_FW_WMI_OP_VERSION_MAX:
10129 		WARN_ON(1);
10130 		ret = -EINVAL;
10131 		goto err_free;
10132 	}
10133 
10134 	if (ar->hw_params.dynamic_sar_support)
10135 		ar->hw->wiphy->sar_capa = &ath10k_sar_capa;
10136 
10137 	if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
10138 		ar->hw->netdev_features = NETIF_F_HW_CSUM;
10139 
10140 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) {
10141 		/* Init ath dfs pattern detector */
10142 		ar->ath_common.debug_mask = ATH_DBG_DFS;
10143 		ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
10144 							     NL80211_DFS_UNSET);
10145 
10146 		if (!ar->dfs_detector)
10147 			ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
10148 	}
10149 
10150 	ret = ath10k_mac_init_rd(ar);
10151 	if (ret) {
10152 		ath10k_err(ar, "failed to derive regdom: %d\n", ret);
10153 		goto err_dfs_detector_exit;
10154 	}
10155 
10156 	/* Disable set_coverage_class for chipsets that do not support it. */
10157 	if (!ar->hw_params.hw_ops->set_coverage_class)
10158 		ar->ops->set_coverage_class = NULL;
10159 
10160 	ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
10161 			    ath10k_reg_notifier);
10162 	if (ret) {
10163 		ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
10164 		goto err_dfs_detector_exit;
10165 	}
10166 
10167 	if (test_bit(WMI_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi.svc_map)) {
10168 		ar->hw->wiphy->features |=
10169 			NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
10170 	}
10171 
10172 	ar->hw->wiphy->cipher_suites = cipher_suites;
10173 
10174 	/* QCA988x and QCA6174 family chips do not support CCMP-256, GCMP-128
10175 	 * and GCMP-256 ciphers in hardware. Fetch number of ciphers supported
10176 	 * from chip specific hw_param table.
10177 	 */
10178 	if (!ar->hw_params.n_cipher_suites ||
10179 	    ar->hw_params.n_cipher_suites > ARRAY_SIZE(cipher_suites)) {
10180 		ath10k_err(ar, "invalid hw_params.n_cipher_suites %d\n",
10181 			   ar->hw_params.n_cipher_suites);
10182 		ar->hw_params.n_cipher_suites = 8;
10183 	}
10184 	ar->hw->wiphy->n_cipher_suites = ar->hw_params.n_cipher_suites;
10185 
10186 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
10187 
10188 	ar->hw->weight_multiplier = ATH10K_AIRTIME_WEIGHT_MULTIPLIER;
10189 
10190 	ret = ieee80211_register_hw(ar->hw);
10191 	if (ret) {
10192 		ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
10193 		goto err_dfs_detector_exit;
10194 	}
10195 
10196 	if (test_bit(WMI_SERVICE_PER_PACKET_SW_ENCRYPT, ar->wmi.svc_map)) {
10197 		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
10198 		ar->hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_AP_VLAN);
10199 	}
10200 
10201 	if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
10202 		ret = regulatory_hint(ar->hw->wiphy,
10203 				      ar->ath_common.regulatory.alpha2);
10204 		if (ret)
10205 			goto err_unregister;
10206 	}
10207 
10208 	return 0;
10209 
10210 err_unregister:
10211 	ieee80211_unregister_hw(ar->hw);
10212 
10213 err_dfs_detector_exit:
10214 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
10215 		ar->dfs_detector->exit(ar->dfs_detector);
10216 
10217 err_free:
10218 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
10219 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
10220 
10221 	SET_IEEE80211_DEV(ar->hw, NULL);
10222 	return ret;
10223 }
10224 
10225 void ath10k_mac_unregister(struct ath10k *ar)
10226 {
10227 	ieee80211_unregister_hw(ar->hw);
10228 
10229 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
10230 		ar->dfs_detector->exit(ar->dfs_detector);
10231 
10232 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
10233 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
10234 
10235 	SET_IEEE80211_DEV(ar->hw, NULL);
10236 }
10237