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