xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision de34d1c1d30d6d9373f536522c04b86b54711c3b)
18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
38e99ea8dSJohannes Berg  * Copyright (C) 2012-2014, 2018-2020 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
7e705c121SKalle Valo #include <linux/kernel.h>
8e705c121SKalle Valo #include <linux/slab.h>
9e705c121SKalle Valo #include <linux/skbuff.h>
10e705c121SKalle Valo #include <linux/netdevice.h>
11e705c121SKalle Valo #include <linux/etherdevice.h>
12e705c121SKalle Valo #include <linux/ip.h>
13e705c121SKalle Valo #include <linux/if_arp.h>
142f89a5d7SGolan Ben-Ami #include <linux/time.h>
15e705c121SKalle Valo #include <net/mac80211.h>
16e705c121SKalle Valo #include <net/ieee80211_radiotap.h>
17e705c121SKalle Valo #include <net/tcp.h>
18e705c121SKalle Valo 
19e705c121SKalle Valo #include "iwl-op-mode.h"
20e705c121SKalle Valo #include "iwl-io.h"
21e705c121SKalle Valo #include "mvm.h"
22e705c121SKalle Valo #include "sta.h"
23e705c121SKalle Valo #include "time-event.h"
24e705c121SKalle Valo #include "iwl-eeprom-parse.h"
25e705c121SKalle Valo #include "iwl-phy-db.h"
26e705c121SKalle Valo #include "testmode.h"
27d962f9b1SJohannes Berg #include "fw/error-dump.h"
28e705c121SKalle Valo #include "iwl-prph.h"
29e705c121SKalle Valo #include "iwl-nvm-parse.h"
30e705c121SKalle Valo 
31e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
32e705c121SKalle Valo 	{
33e705c121SKalle Valo 		.max = 1,
34e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
35e705c121SKalle Valo 	},
36e705c121SKalle Valo 	{
37e705c121SKalle Valo 		.max = 1,
38e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
39e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
40e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
41e705c121SKalle Valo 	},
42e705c121SKalle Valo 	{
43e705c121SKalle Valo 		.max = 1,
44e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
45e705c121SKalle Valo 	},
46e705c121SKalle Valo };
47e705c121SKalle Valo 
48e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
49e705c121SKalle Valo 	{
50e705c121SKalle Valo 		.num_different_channels = 2,
51e705c121SKalle Valo 		.max_interfaces = 3,
52e705c121SKalle Valo 		.limits = iwl_mvm_limits,
53e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
54e705c121SKalle Valo 	},
55e705c121SKalle Valo };
56e705c121SKalle Valo 
57e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
58e705c121SKalle Valo /*
59e705c121SKalle Valo  * Use the reserved field to indicate magic values.
60e705c121SKalle Valo  * these values will only be used internally by the driver,
61e705c121SKalle Valo  * and won't make it to the fw (reserved will be 0).
62e705c121SKalle Valo  * BC_FILTER_MAGIC_IP - configure the val of this attribute to
63e705c121SKalle Valo  *	be the vif's ip address. in case there is not a single
64e705c121SKalle Valo  *	ip address (0, or more than 1), this attribute will
65e705c121SKalle Valo  *	be skipped.
66e705c121SKalle Valo  * BC_FILTER_MAGIC_MAC - set the val of this attribute to
67e705c121SKalle Valo  *	the LSB bytes of the vif's mac address
68e705c121SKalle Valo  */
69e705c121SKalle Valo enum {
70e705c121SKalle Valo 	BC_FILTER_MAGIC_NONE = 0,
71e705c121SKalle Valo 	BC_FILTER_MAGIC_IP,
72e705c121SKalle Valo 	BC_FILTER_MAGIC_MAC,
73e705c121SKalle Valo };
74e705c121SKalle Valo 
75e705c121SKalle Valo static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
76e705c121SKalle Valo 	{
77e705c121SKalle Valo 		/* arp */
78e705c121SKalle Valo 		.discard = 0,
79e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
80e705c121SKalle Valo 		.attrs = {
81e705c121SKalle Valo 			{
82e705c121SKalle Valo 				/* frame type - arp, hw type - ethernet */
83e705c121SKalle Valo 				.offset_type =
84e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
85e705c121SKalle Valo 				.offset = sizeof(rfc1042_header),
86e705c121SKalle Valo 				.val = cpu_to_be32(0x08060001),
87e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
88e705c121SKalle Valo 			},
89e705c121SKalle Valo 			{
90e705c121SKalle Valo 				/* arp dest ip */
91e705c121SKalle Valo 				.offset_type =
92e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
93e705c121SKalle Valo 				.offset = sizeof(rfc1042_header) + 2 +
94e705c121SKalle Valo 					  sizeof(struct arphdr) +
95e705c121SKalle Valo 					  ETH_ALEN + sizeof(__be32) +
96e705c121SKalle Valo 					  ETH_ALEN,
97e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
98e705c121SKalle Valo 				/* mark it as special field */
99e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
100e705c121SKalle Valo 			},
101e705c121SKalle Valo 		},
102e705c121SKalle Valo 	},
103e705c121SKalle Valo 	{
104e705c121SKalle Valo 		/* dhcp offer bcast */
105e705c121SKalle Valo 		.discard = 0,
106e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
107e705c121SKalle Valo 		.attrs = {
108e705c121SKalle Valo 			{
109e705c121SKalle Valo 				/* udp dest port - 68 (bootp client)*/
110e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
111e705c121SKalle Valo 				.offset = offsetof(struct udphdr, dest),
112e705c121SKalle Valo 				.val = cpu_to_be32(0x00440000),
113e705c121SKalle Valo 				.mask = cpu_to_be32(0xffff0000),
114e705c121SKalle Valo 			},
115e705c121SKalle Valo 			{
116e705c121SKalle Valo 				/* dhcp - lsb bytes of client hw address */
117e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
118e705c121SKalle Valo 				.offset = 38,
119e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
120e705c121SKalle Valo 				/* mark it as special field */
121e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
122e705c121SKalle Valo 			},
123e705c121SKalle Valo 		},
124e705c121SKalle Valo 	},
125e705c121SKalle Valo 	/* last filter must be empty */
126e705c121SKalle Valo 	{},
127e705c121SKalle Valo };
128e705c121SKalle Valo #endif
129e705c121SKalle Valo 
130fc36ffdaSJohannes Berg static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
131fc36ffdaSJohannes Berg 	.max_peers = IWL_MVM_TOF_MAX_APS,
132fc36ffdaSJohannes Berg 	.report_ap_tsf = 1,
133fc36ffdaSJohannes Berg 	.randomize_mac_addr = 1,
134fc36ffdaSJohannes Berg 
135fc36ffdaSJohannes Berg 	.ftm = {
136fc36ffdaSJohannes Berg 		.supported = 1,
137fc36ffdaSJohannes Berg 		.asap = 1,
138fc36ffdaSJohannes Berg 		.non_asap = 1,
139fc36ffdaSJohannes Berg 		.request_lci = 1,
140fc36ffdaSJohannes Berg 		.request_civicloc = 1,
1416815e3d0SAvraham Stern 		.trigger_based = 1,
1426815e3d0SAvraham Stern 		.non_trigger_based = 1,
143fc36ffdaSJohannes Berg 		.max_bursts_exponent = -1, /* all supported */
144fc36ffdaSJohannes Berg 		.max_ftms_per_burst = 0, /* no limits */
145fc36ffdaSJohannes Berg 		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
146fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_20) |
147fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_40) |
148fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_80),
149fc36ffdaSJohannes Berg 		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
150fc36ffdaSJohannes Berg 			     BIT(NL80211_PREAMBLE_HT) |
1516815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_VHT) |
1526815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_HE),
153fc36ffdaSJohannes Berg 	},
154fc36ffdaSJohannes Berg };
155fc36ffdaSJohannes Berg 
1566569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
157c56e00a3SJohannes Berg 				 enum set_key_cmd cmd,
158c56e00a3SJohannes Berg 				 struct ieee80211_vif *vif,
159c56e00a3SJohannes Berg 				 struct ieee80211_sta *sta,
160c56e00a3SJohannes Berg 				 struct ieee80211_key_conf *key);
161c56e00a3SJohannes Berg 
162e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
163e705c121SKalle Valo {
164e705c121SKalle Valo 	int i;
165e705c121SKalle Valo 
166e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
167e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
168e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
169e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
170e705c121SKalle Valo 	}
171e705c121SKalle Valo }
172e705c121SKalle Valo 
173e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
174e705c121SKalle Valo 						  const char *alpha2,
175e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
176e705c121SKalle Valo 						  bool *changed)
177e705c121SKalle Valo {
178e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
179e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
180e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
181e705c121SKalle Valo 	struct iwl_mcc_update_resp *resp;
182e27c506aSGil Adam 	u8 resp_ver;
183e705c121SKalle Valo 
184e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
185e705c121SKalle Valo 
186e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
187e705c121SKalle Valo 
188e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
189e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
190e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
191e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
192e705c121SKalle Valo 		goto out;
193e705c121SKalle Valo 	}
194e705c121SKalle Valo 
19582715ac7SEmmanuel Grumbach 	if (changed) {
19682715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
19782715ac7SEmmanuel Grumbach 
19882715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
19982715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
20082715ac7SEmmanuel Grumbach 	}
201e27c506aSGil Adam 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
202e27c506aSGil Adam 					   MCC_UPDATE_CMD, 0);
203e27c506aSGil Adam 	IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
204e705c121SKalle Valo 
205e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
206e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
207e705c121SKalle Valo 				      resp->channels,
20877e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
2092763bba6SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info),
210e27c506aSGil Adam 				      __le16_to_cpu(resp->cap), resp_ver);
211e705c121SKalle Valo 	/* Store the return source id */
212e705c121SKalle Valo 	src_id = resp->source_id;
213e705c121SKalle Valo 	kfree(resp);
214e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
215e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
216e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
217e705c121SKalle Valo 		goto out;
218e705c121SKalle Valo 	}
219e705c121SKalle Valo 
220e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
221e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
222e705c121SKalle Valo 	mvm->lar_regdom_set = true;
223e705c121SKalle Valo 	mvm->mcc_src = src_id;
224e705c121SKalle Valo 
225e705c121SKalle Valo out:
226e705c121SKalle Valo 	return regd;
227e705c121SKalle Valo }
228e705c121SKalle Valo 
229e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
230e705c121SKalle Valo {
231e705c121SKalle Valo 	bool changed;
232e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
233e705c121SKalle Valo 
234e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
235e705c121SKalle Valo 		return;
236e705c121SKalle Valo 
237e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
238e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
239e705c121SKalle Valo 		/* only update the regulatory core if changed */
240e705c121SKalle Valo 		if (changed)
241e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
242e705c121SKalle Valo 
243e705c121SKalle Valo 		kfree(regd);
244e705c121SKalle Valo 	}
245e705c121SKalle Valo }
246e705c121SKalle Valo 
247e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
248e705c121SKalle Valo 							  bool *changed)
249e705c121SKalle Valo {
250e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
251e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
252e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
253e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
254e705c121SKalle Valo }
255e705c121SKalle Valo 
256e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
257e705c121SKalle Valo {
258e705c121SKalle Valo 	enum iwl_mcc_source used_src;
259e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
260e705c121SKalle Valo 	int ret;
261e705c121SKalle Valo 	bool changed;
262e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
263a05829a7SJohannes Berg 			wiphy_dereference(mvm->hw->wiphy, mvm->hw->wiphy->regd);
264e705c121SKalle Valo 
265e705c121SKalle Valo 	if (!r)
266e705c121SKalle Valo 		return -ENOENT;
267e705c121SKalle Valo 
268e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
269e705c121SKalle Valo 	used_src = mvm->mcc_src;
270e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
271e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
272e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
273e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
274e705c121SKalle Valo 			kfree(regd);
275e705c121SKalle Valo 	}
276e705c121SKalle Valo 
277e705c121SKalle Valo 	/* Now set our last stored MCC and source */
278e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
279e705c121SKalle Valo 				     &changed);
280e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
281e705c121SKalle Valo 		return -EIO;
282e705c121SKalle Valo 
283e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
284e705c121SKalle Valo 	if (changed)
285a05829a7SJohannes Berg 		ret = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
286e705c121SKalle Valo 	else
287e705c121SKalle Valo 		ret = 0;
288e705c121SKalle Valo 
289e705c121SKalle Valo 	kfree(regd);
290e705c121SKalle Valo 	return ret;
291e705c121SKalle Valo }
292e705c121SKalle Valo 
2937f2ea521SYueHaibing static const u8 he_if_types_ext_capa_sta[] = {
2947360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
295918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
2967360f99eSEmmanuel Grumbach 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
2977360f99eSEmmanuel Grumbach 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
2987360f99eSEmmanuel Grumbach };
2997360f99eSEmmanuel Grumbach 
3007f2ea521SYueHaibing static const struct wiphy_iftype_ext_capab he_iftypes_ext_capa[] = {
3017360f99eSEmmanuel Grumbach 	{
3027360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
3037360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
3047360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
3057360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
3067360f99eSEmmanuel Grumbach 	},
3077360f99eSEmmanuel Grumbach };
3087360f99eSEmmanuel Grumbach 
309e8503aecSBen Greear static int
310e8503aecSBen Greear iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
311e8503aecSBen Greear {
312e8503aecSBen Greear 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
313e8503aecSBen Greear 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
314e8503aecSBen Greear 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
315e8503aecSBen Greear 	return 0;
316e8503aecSBen Greear }
317e8503aecSBen Greear 
318e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
319e705c121SKalle Valo {
320e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
321e705c121SKalle Valo 	int num_mac, ret, i;
322e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
323e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
324e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
325e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
326e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
327e705c121SKalle Valo 	};
3283f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
3293f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
3303f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3313f37c229SIdo Yariv #endif
332e705c121SKalle Valo 
333e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
334e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
335e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
336e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
337e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
338e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
339e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
340e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
341e705c121SKalle Valo 	ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
342e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
343e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
344e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
345e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
346909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
34730433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
348d270e7b8SIlan Peer 	ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
349520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
350cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
351cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
352cfb21b11SJohannes Berg 	/*
353cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
354cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
355cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
356cfb21b11SJohannes Berg 	 * assert EDC.
357cfb21b11SJohannes Berg 	 *
358cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
359cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
360cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
361cfb21b11SJohannes Berg 	 * devices.
362cfb21b11SJohannes Berg 	 */
3637d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
364438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
365438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
366ecaf71deSGregory Greenman 
3674243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
368ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
369ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
370ecaf71deSGregory Greenman 	}
371ecaf71deSGregory Greenman 
372b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
373b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
374960f864bSJohannes Berg 
375960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
376960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
37765e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
378960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
379960f864bSJohannes Berg 		/*
380960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
381960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
382960f864bSJohannes Berg 		 * for station powersave
383960f864bSJohannes Berg 		 */
384960f864bSJohannes Berg 		return -EINVAL;
385960f864bSJohannes Berg 	}
386e705c121SKalle Valo 
38780938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
38880938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
38980938abcSJohannes Berg 
3902d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
3912d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
3922d7cf549SJohannes Berg 
393cf961e16SLiad Kaufman 	hw->queues = IEEE80211_MAX_QUEUES;
394e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
395e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
396e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
397e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
398e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
399371a17edSJohannes Berg 
400371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
401371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
402371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
403371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
404371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
405371a17edSJohannes Berg 
4064243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
4079f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
4089f66a397SGregory Greenman 
409e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
410e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
411438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
412e705c121SKalle Valo 
4138e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
414e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
415e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
416e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
417e705c121SKalle Valo 
4182a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4192a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4202a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
4212a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4222a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4232a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
4242a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4252a53d166SAyala Beker 	}
4262a53d166SAyala Beker 
427f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
428f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
429f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
430f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
431f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
432f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
433f4bfdc5eSEmmanuel Grumbach 
434e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
435f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
436e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
4378e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4388e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4398e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
4408e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4418e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4428e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
4438e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4448e160ab8SAyala Beker 	}
445e705c121SKalle Valo 
446e705c121SKalle Valo 	/* currently FW API supports only one optional cipher scheme */
447e705c121SKalle Valo 	if (mvm->fw->cs[0].cipher) {
44824ddddf3SJohannes Berg 		const struct iwl_fw_cipher_scheme *fwcs = &mvm->fw->cs[0];
44924ddddf3SJohannes Berg 		struct ieee80211_cipher_scheme *cs = &mvm->cs[0];
45024ddddf3SJohannes Berg 
451e705c121SKalle Valo 		mvm->hw->n_cipher_schemes = 1;
45224ddddf3SJohannes Berg 
45324ddddf3SJohannes Berg 		cs->cipher = le32_to_cpu(fwcs->cipher);
45424ddddf3SJohannes Berg 		cs->iftype = BIT(NL80211_IFTYPE_STATION);
45524ddddf3SJohannes Berg 		cs->hdr_len = fwcs->hdr_len;
45624ddddf3SJohannes Berg 		cs->pn_len = fwcs->pn_len;
45724ddddf3SJohannes Berg 		cs->pn_off = fwcs->pn_off;
45824ddddf3SJohannes Berg 		cs->key_idx_off = fwcs->key_idx_off;
45924ddddf3SJohannes Berg 		cs->key_idx_mask = fwcs->key_idx_mask;
46024ddddf3SJohannes Berg 		cs->key_idx_shift = fwcs->key_idx_shift;
46124ddddf3SJohannes Berg 		cs->mic_len = fwcs->mic_len;
46224ddddf3SJohannes Berg 
46324ddddf3SJohannes Berg 		mvm->hw->cipher_schemes = mvm->cs;
46424ddddf3SJohannes Berg 		mvm->ciphers[hw->wiphy->n_cipher_suites] = cs->cipher;
465e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
466e705c121SKalle Valo 	}
467e705c121SKalle Valo 
468b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
469fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
470b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
471b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
472fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
473fc36ffdaSJohannes Berg 	}
474b73f9a4aSJohannes Berg 
475b1fdc250SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
476b1fdc250SJohannes Berg 			IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
477b1fdc250SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
478b1fdc250SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
479b1fdc250SJohannes Berg 
480e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
481e705c121SKalle Valo 	hw->wiphy->features |=
482e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
483e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
484e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
485e705c121SKalle Valo 
486e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
487e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
488e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
489cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
490e705c121SKalle Valo 
491e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
492e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
493e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
494e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
495e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
496e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
497e705c121SKalle Valo 
498e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
499e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
50033e3fd99SAlexander Wetzel 
50133e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
50233e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
50333e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
50433e3fd99SAlexander Wetzel 	 */
50533e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
50633e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
50733e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
508e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
509e47df5bdSJohannes Berg 
510e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
511e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
512e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
513e705c121SKalle Valo 	else
514e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
515e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
516e705c121SKalle Valo 
517e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
518e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
519eae94cf8SLuca Coelho 	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
520e705c121SKalle Valo 
521e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
522e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
523e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
524e705c121SKalle Valo 
525e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
526e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
527e705c121SKalle Valo 
528e705c121SKalle Valo 	/* Extract MAC address */
529e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
530e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
531e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
532e705c121SKalle Valo 
533e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
534e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
535e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
536e705c121SKalle Valo 
537e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
538e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
539e705c121SKalle Valo 		       ETH_ALEN);
540e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
541e705c121SKalle Valo 		hw->wiphy->n_addresses++;
542e705c121SKalle Valo 	}
543e705c121SKalle Valo 
544e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
545e705c121SKalle Valo 
546e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
547e705c121SKalle Valo 
548e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
549e705c121SKalle Valo 
550e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
551e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
552e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
553e705c121SKalle Valo 
554e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
555e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
556e705c121SKalle Valo 	else
557e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
558e705c121SKalle Valo 
55957fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
56057fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
56157fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
56257fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
56357fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
56457fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
565e705c121SKalle Valo 
566e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
567e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
568e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
569e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
57057fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
571e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
572e705c121SKalle Valo 	}
573eae94cf8SLuca Coelho 	if (fw_has_capa(&mvm->fw->ucode_capa,
574eae94cf8SLuca Coelho 			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
575eae94cf8SLuca Coelho 	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
576eae94cf8SLuca Coelho 		hw->wiphy->bands[NL80211_BAND_6GHZ] =
577eae94cf8SLuca Coelho 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
578e705c121SKalle Valo 
579e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
580e705c121SKalle Valo 
581e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
582e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
583e705c121SKalle Valo 	else
584e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
585e705c121SKalle Valo 
586ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
587e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
5885d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
589e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
590e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
591e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
592e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
593e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
594e705c121SKalle Valo 
595e705c121SKalle Valo 	/*
596e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
597e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
598e705c121SKalle Valo 	 */
599e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
600e705c121SKalle Valo 
601e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
602e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
603e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
604a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
605e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
606e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
607e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
608e705c121SKalle Valo 
609e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
610e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
611e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
612e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
613e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
614e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
615e705c121SKalle Valo 
616e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
617e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
618e705c121SKalle Valo 		hw->wiphy->features |=
619e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
620e705c121SKalle Valo 
621e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
622e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
623e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
624e705c121SKalle Valo 
6252a42aea7SNathan Errera 	if (iwl_fw_lookup_cmd_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
6262a42aea7SNathan Errera 				  WOWLAN_KEK_KCK_MATERIAL,
6272a42aea7SNathan Errera 				  IWL_FW_CMD_VER_UNKNOWN) == 3)
6282a42aea7SNathan Errera 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
6292a42aea7SNathan Errera 
630aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
631aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
632aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
633aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
634aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
635aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
636aacf8f18SAvrahams Stern 	}
637aacf8f18SAvrahams Stern 
6388f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
6398f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6408f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
6418f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6428f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
6438f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6448f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
6458f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6468f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
6478f691af9SZamir, Roee 	}
6488f691af9SZamir, Roee 
6497360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
6507360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
6517360f99eSEmmanuel Grumbach 		hw->wiphy->iftype_ext_capab = he_iftypes_ext_capa;
6527360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
6537360f99eSEmmanuel Grumbach 			ARRAY_SIZE(he_iftypes_ext_capa);
654918cbf39SSara Sharon 
655918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
656918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
6577360f99eSEmmanuel Grumbach 	}
6587360f99eSEmmanuel Grumbach 
659e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
660e705c121SKalle Valo 
661e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6623f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
663e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
664e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
665e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
666e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
667e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
668e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
669e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
670e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
671e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
672e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
673e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
674e705c121SKalle Valo 
675e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
676e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
677e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
6785d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
6795d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
680e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
681e705c121SKalle Valo 	}
682e705c121SKalle Valo #endif
683e705c121SKalle Valo 
684e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
685e705c121SKalle Valo 	/* assign default bcast filtering configuration */
686e705c121SKalle Valo 	mvm->bcast_filters = iwl_mvm_default_bcast_filters;
687e705c121SKalle Valo #endif
688e705c121SKalle Valo 
689e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
690e705c121SKalle Valo 	if (ret)
691e705c121SKalle Valo 		return ret;
692e705c121SKalle Valo 
693e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
694e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
695e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
696e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
697e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
698e705c121SKalle Valo 	}
699e705c121SKalle Valo 
700e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
701e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
702e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
703e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
704e705c121SKalle Valo 	}
705e705c121SKalle Valo 
706e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
70759fa61f3SEmmanuel Grumbach 	if (!iwl_mvm_is_csum_supported(mvm))
7085e6a98dcSSara Sharon 		hw->netdev_features &= ~(IWL_TX_CSUM_NETIF_FLAGS |
7095e6a98dcSSara Sharon 					 NETIF_F_RXCSUM);
71041837ca9SEmmanuel Grumbach 
71191b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
71291b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
71391b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
71491b08c2dSAviya Erenfeld 
7159c11d8a9SShaul Triebitz 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
7169c11d8a9SShaul Triebitz 		wiphy_ext_feature_set(hw->wiphy,
7179c11d8a9SShaul Triebitz 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
7189c11d8a9SShaul Triebitz 
719e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
720e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
721e8503aecSBen Greear 
722de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
723de645e89SJohannes Berg 	if (ret) {
724de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
725e705c121SKalle Valo 	}
726e705c121SKalle Valo 
727e705c121SKalle Valo 	return ret;
728e705c121SKalle Valo }
729e705c121SKalle Valo 
730df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
731df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
732df2378abSJohannes Berg {
733df2378abSJohannes Berg 	if (likely(sta)) {
734df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
735df2378abSJohannes Berg 			return;
736df2378abSJohannes Berg 	} else {
737df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
738df2378abSJohannes Berg 			return;
739df2378abSJohannes Berg 	}
740df2378abSJohannes Berg 
741df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
742df2378abSJohannes Berg }
743df2378abSJohannes Berg 
744e705c121SKalle Valo static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
745e705c121SKalle Valo 			   struct ieee80211_tx_control *control,
746e705c121SKalle Valo 			   struct sk_buff *skb)
747e705c121SKalle Valo {
748e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
749e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
750e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
751e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
752cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
753cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
754e705c121SKalle Valo 
755e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
756e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
757e705c121SKalle Valo 		goto drop;
758e705c121SKalle Valo 	}
759e705c121SKalle Valo 
760cfbc6c4cSSara Sharon 	if (offchannel &&
761e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
762e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
763e705c121SKalle Valo 		goto drop;
764e705c121SKalle Valo 
765eb045e6eSDavid Spinadel 	/* treat non-bufferable MMPDUs on AP interfaces as broadcast */
766eb045e6eSDavid Spinadel 	if ((info->control.vif->type == NL80211_IFTYPE_AP ||
767eb045e6eSDavid Spinadel 	     info->control.vif->type == NL80211_IFTYPE_ADHOC) &&
768e705c121SKalle Valo 	    ieee80211_is_mgmt(hdr->frame_control) &&
769eb045e6eSDavid Spinadel 	    !ieee80211_is_bufferable_mmpdu(hdr->frame_control))
770e705c121SKalle Valo 		sta = NULL;
771e705c121SKalle Valo 
772dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
773cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
774cfbc6c4cSSara Sharon 	    !offchannel) {
775dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
776dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
777dc1aca22SAndrei Otcheretianski 		u8 ap_sta_id = READ_ONCE(mvmvif->ap_sta_id);
778dc1aca22SAndrei Otcheretianski 
779be9ae34eSNathan Errera 		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
780dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
781dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
782dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
783dc1aca22SAndrei Otcheretianski 				goto drop;
784dc1aca22SAndrei Otcheretianski 		}
785dc1aca22SAndrei Otcheretianski 	}
786dc1aca22SAndrei Otcheretianski 
787df2378abSJohannes Berg 	iwl_mvm_tx_skb(mvm, skb, sta);
788e705c121SKalle Valo 	return;
789e705c121SKalle Valo  drop:
790e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
791e705c121SKalle Valo }
792e705c121SKalle Valo 
793cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
794e705c121SKalle Valo {
795cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
796cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
797cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
798cfbc6c4cSSara Sharon 
799fba8248eSSara Sharon 	/*
800fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
801fba8248eSSara Sharon 	 * taker all the work.
802fba8248eSSara Sharon 	 *
803fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
804fba8248eSSara Sharon 	 *
805fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
806fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
807fba8248eSSara Sharon 	 *
808fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
809fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
810fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
811fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
812fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
813fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
814fba8248eSSara Sharon 	 *
815fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
816fba8248eSSara Sharon 	 * check the queue, so do nothing.
817fba8248eSSara Sharon 	 */
818fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
819fba8248eSSara Sharon 		return;
820cfbc6c4cSSara Sharon 
821cfbc6c4cSSara Sharon 	rcu_read_lock();
822fba8248eSSara Sharon 	do {
823cfbc6c4cSSara Sharon 		while (likely(!mvmtxq->stopped &&
82400520b7aSEmmanuel Grumbach 			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
825cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
826cfbc6c4cSSara Sharon 
827f50d693bSSara Sharon 			if (!skb) {
828f50d693bSSara Sharon 				if (txq->sta)
829f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
830f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
831f50d693bSSara Sharon 						     txq->sta->addr,
832f50d693bSSara Sharon 						     txq->tid);
833cfbc6c4cSSara Sharon 				break;
834f50d693bSSara Sharon 			}
835cfbc6c4cSSara Sharon 
836cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
837cfbc6c4cSSara Sharon 		}
838fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
839cfbc6c4cSSara Sharon 	rcu_read_unlock();
840e705c121SKalle Valo }
841e705c121SKalle Valo 
842cfbc6c4cSSara Sharon static void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
843cfbc6c4cSSara Sharon 				      struct ieee80211_txq *txq)
844e705c121SKalle Valo {
845cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
846cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
847e705c121SKalle Valo 
848cfbc6c4cSSara Sharon 	/*
849cfbc6c4cSSara Sharon 	 * Please note that racing is handled very carefully here:
850cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id is updated during allocation, and mvmtxq->list is
851cfbc6c4cSSara Sharon 	 * deleted afterwards.
852cfbc6c4cSSara Sharon 	 * This means that if:
853cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && list_empty(&mvmtxq->list):
854cfbc6c4cSSara Sharon 	 *	queue is allocated and we can TX.
855cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && !list_empty(&mvmtxq->list):
856cfbc6c4cSSara Sharon 	 *	a race, should defer the frame.
857cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && list_empty(&mvmtxq->list):
858cfbc6c4cSSara Sharon 	 *	need to allocate the queue and defer the frame.
859cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && !list_empty(&mvmtxq->list):
860cfbc6c4cSSara Sharon 	 *	queue is already scheduled for allocation, no need to allocate,
861cfbc6c4cSSara Sharon 	 *	should defer the frame.
862cfbc6c4cSSara Sharon 	 */
863cfbc6c4cSSara Sharon 
864cfbc6c4cSSara Sharon 	/* If the queue is allocated TX and return. */
865cfbc6c4cSSara Sharon 	if (!txq->sta || mvmtxq->txq_id != IWL_MVM_INVALID_QUEUE) {
866cfbc6c4cSSara Sharon 		/*
867cfbc6c4cSSara Sharon 		 * Check that list is empty to avoid a race where txq_id is
868cfbc6c4cSSara Sharon 		 * already updated, but the queue allocation work wasn't
869cfbc6c4cSSara Sharon 		 * finished
870cfbc6c4cSSara Sharon 		 */
871cfbc6c4cSSara Sharon 		if (unlikely(txq->sta && !list_empty(&mvmtxq->list)))
872cfbc6c4cSSara Sharon 			return;
873cfbc6c4cSSara Sharon 
874cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
875cfbc6c4cSSara Sharon 		return;
876cfbc6c4cSSara Sharon 	}
877cfbc6c4cSSara Sharon 
878cfbc6c4cSSara Sharon 	/* The list is being deleted only after the queue is fully allocated. */
879cfbc6c4cSSara Sharon 	if (!list_empty(&mvmtxq->list))
880cfbc6c4cSSara Sharon 		return;
881cfbc6c4cSSara Sharon 
882cfbc6c4cSSara Sharon 	list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
883cfbc6c4cSSara Sharon 	schedule_work(&mvm->add_stream_wk);
884e705c121SKalle Valo }
885e705c121SKalle Valo 
886e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
887e705c121SKalle Valo 	do {								\
888e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
889e705c121SKalle Valo 			break;						\
8907174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
891e705c121SKalle Valo 	} while (0)
892e705c121SKalle Valo 
893e705c121SKalle Valo static void
894e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
895e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
896e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
897e705c121SKalle Valo {
898e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
899e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
900e705c121SKalle Valo 
9016c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
9026c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
9036c042d75SSara Sharon 	if (!trig)
904e705c121SKalle Valo 		return;
905e705c121SKalle Valo 
906e705c121SKalle Valo 	ba_trig = (void *)trig->data;
907e705c121SKalle Valo 
908e705c121SKalle Valo 	switch (action) {
909e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
910e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
911e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
912e705c121SKalle Valo 
913e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
914e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
915e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
916e705c121SKalle Valo 		break;
917e705c121SKalle Valo 		}
918e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
919e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
920e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
921e705c121SKalle Valo 				 sta->addr, tid);
922e705c121SKalle Valo 		break;
923e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
924e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
925e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
926e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
927e705c121SKalle Valo 		break;
928e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
929e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
930e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
931e705c121SKalle Valo 				 sta->addr, tid);
932e705c121SKalle Valo 		break;
933e705c121SKalle Valo 	default:
934e705c121SKalle Valo 		break;
935e705c121SKalle Valo 	}
936e705c121SKalle Valo }
937e705c121SKalle Valo 
938e705c121SKalle Valo static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
939e705c121SKalle Valo 				    struct ieee80211_vif *vif,
94050ea05efSSara Sharon 				    struct ieee80211_ampdu_params *params)
941e705c121SKalle Valo {
942e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
943e705c121SKalle Valo 	int ret;
94450ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
94550ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
94650ea05efSSara Sharon 	u16 tid = params->tid;
94750ea05efSSara Sharon 	u16 *ssn = &params->ssn;
948514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
949bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
95010b2b201SSara Sharon 	u16 timeout = params->timeout;
951e705c121SKalle Valo 
952e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
953e705c121SKalle Valo 		     sta->addr, tid, action);
954e705c121SKalle Valo 
955e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
956e705c121SKalle Valo 		return -EACCES;
957e705c121SKalle Valo 
958e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
959e705c121SKalle Valo 
960e705c121SKalle Valo 	switch (action) {
961e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
962b0ffe455SJohannes Berg 		if (iwl_mvm_vif_from_mac80211(vif)->ap_sta_id ==
963b0ffe455SJohannes Berg 				iwl_mvm_sta_from_mac80211(sta)->sta_id) {
964b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
965b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
966b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
967b0ffe455SJohannes Berg 
968b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
969b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
970b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
971b0ffe455SJohannes Berg 		}
972e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
973e705c121SKalle Valo 			ret = -EINVAL;
974e705c121SKalle Valo 			break;
975e705c121SKalle Valo 		}
97610b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
97710b2b201SSara Sharon 					 timeout);
978e705c121SKalle Valo 		break;
979e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
98010b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
98110b2b201SSara Sharon 					 timeout);
982e705c121SKalle Valo 		break;
983e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
984e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
985e705c121SKalle Valo 			ret = -EINVAL;
986e705c121SKalle Valo 			break;
987e705c121SKalle Valo 		}
988e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
989e705c121SKalle Valo 		break;
990e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
991e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
992e705c121SKalle Valo 		break;
993e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
994e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
995e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
996e705c121SKalle Valo 		break;
997e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
998bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
999bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
1000e705c121SKalle Valo 		break;
1001e705c121SKalle Valo 	default:
1002e705c121SKalle Valo 		WARN_ON_ONCE(1);
1003e705c121SKalle Valo 		ret = -EINVAL;
1004e705c121SKalle Valo 		break;
1005e705c121SKalle Valo 	}
1006e705c121SKalle Valo 
1007e705c121SKalle Valo 	if (!ret) {
1008e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
1009e705c121SKalle Valo 
1010e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1011e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1012e705c121SKalle Valo 
1013e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1014e705c121SKalle Valo 					    rx_ba_ssn, action);
1015e705c121SKalle Valo 	}
1016e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1017e705c121SKalle Valo 
1018e705c121SKalle Valo 	return ret;
1019e705c121SKalle Valo }
1020e705c121SKalle Valo 
1021e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1022e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1023e705c121SKalle Valo {
1024e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1025e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1026e705c121SKalle Valo 
1027e705c121SKalle Valo 	mvmvif->uploaded = false;
10280ae98812SSara Sharon 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1029e705c121SKalle Valo 
1030e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1031e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1032e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1033e705c121SKalle Valo 
1034e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1035e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
103686e177d8SGregory Greenman 	memset(&mvmvif->probe_resp_data, 0, sizeof(mvmvif->probe_resp_data));
1037e705c121SKalle Valo }
1038e705c121SKalle Valo 
1039e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1040e705c121SKalle Valo {
1041fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1042e705c121SKalle Valo 
10439bf13beeSJohannes Berg 	mvm->cur_aid = 0;
10449bf13beeSJohannes Berg 
1045e705c121SKalle Valo 	mvm->scan_status = 0;
1046e705c121SKalle Valo 	mvm->ps_disabled = false;
1047b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1048e705c121SKalle Valo 
1049e705c121SKalle Valo 	/* just in case one was running */
1050305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1051e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1052e705c121SKalle Valo 
1053fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1054fc36ffdaSJohannes Berg 
1055e705c121SKalle Valo 	/*
1056e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1057e705c121SKalle Valo 	 * gone down during the HW restart
1058e705c121SKalle Valo 	 */
1059e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1060e705c121SKalle Valo 
1061e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1062e705c121SKalle Valo 
1063e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
10649c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1065e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1066e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1067e705c121SKalle Valo 
1068e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1069e705c121SKalle Valo 
1070e705c121SKalle Valo 	mvm->vif_count = 0;
1071e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
10727174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1073baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1074e705c121SKalle Valo 
1075e705c121SKalle Valo 	/* keep statistics ticking */
1076e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1077e705c121SKalle Valo }
1078e705c121SKalle Valo 
1079e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1080e705c121SKalle Valo {
1081e705c121SKalle Valo 	int ret;
1082e705c121SKalle Valo 
1083e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1084e705c121SKalle Valo 
1085bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1086bf8b286fSJohannes Berg 		/*
1087bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1088bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1089bf8b286fSJohannes Berg 		 */
1090bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1091bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1092e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1093e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1094a42b2af3SLuca Coelho 	}
1095e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1096e705c121SKalle Valo 
1097b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1098b108d8c7SShahar S Matityahu 			       NULL);
1099b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1100b108d8c7SShahar S Matityahu 			       NULL);
1101da2eb669SSara Sharon 
1102e8fe3b41SIlan Peer 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1103e8fe3b41SIlan Peer 
1104e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1105e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1106e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1107e705c121SKalle Valo 		 * would do.
1108e705c121SKalle Valo 		 */
1109e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1110e705c121SKalle Valo 	}
1111e705c121SKalle Valo 
1112e705c121SKalle Valo 	return ret;
1113e705c121SKalle Valo }
1114e705c121SKalle Valo 
1115e705c121SKalle Valo static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1116e705c121SKalle Valo {
1117e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1118e705c121SKalle Valo 	int ret;
1119e705c121SKalle Valo 
1120e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1121e705c121SKalle Valo 	ret = __iwl_mvm_mac_start(mvm);
1122e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1123e705c121SKalle Valo 
1124e705c121SKalle Valo 	return ret;
1125e705c121SKalle Valo }
1126e705c121SKalle Valo 
1127e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1128e705c121SKalle Valo {
1129e705c121SKalle Valo 	int ret;
1130e705c121SKalle Valo 
1131e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1132e705c121SKalle Valo 
1133e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
11344d4183c4SEmmanuel Grumbach 
1135e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1136e705c121SKalle Valo 	if (ret)
1137e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1138e705c121SKalle Valo 			ret);
1139e705c121SKalle Valo 
1140f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1141f130bb75SMordechay Goodstein 
1142e705c121SKalle Valo 	/*
1143e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1144e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1145e705c121SKalle Valo 	 */
1146e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1147e705c121SKalle Valo 
1148e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1149e705c121SKalle Valo }
1150e705c121SKalle Valo 
1151e705c121SKalle Valo static void
1152e705c121SKalle Valo iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1153e705c121SKalle Valo 			      enum ieee80211_reconfig_type reconfig_type)
1154e705c121SKalle Valo {
1155e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1156e705c121SKalle Valo 
1157e705c121SKalle Valo 	switch (reconfig_type) {
1158e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1159e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1160e705c121SKalle Valo 		break;
1161e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1162e705c121SKalle Valo 		break;
1163e705c121SKalle Valo 	}
1164e705c121SKalle Valo }
1165e705c121SKalle Valo 
1166e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1167e705c121SKalle Valo {
1168e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1169e705c121SKalle Valo 
1170b68bd2e3SIlan Peer 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1171b68bd2e3SIlan Peer 
1172e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1173e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1174e705c121SKalle Valo 	 */
1175e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1176e705c121SKalle Valo 
1177e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1178e705c121SKalle Valo 
11792c2c3647SNathan Errera 	if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, ADD_STA, 0) < 12)
1180f327236dSSharon 		iwl_mvm_rm_aux_sta(mvm);
1181f327236dSSharon 
1182fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1183e705c121SKalle Valo 
1184e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1185e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1186e705c121SKalle Valo 
1187e705c121SKalle Valo 	/*
1188155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1189155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1190155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1191e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1192e705c121SKalle Valo 	 * HW restart was requested.
1193e705c121SKalle Valo 	 */
1194155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1195155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1196155f7e04SEmmanuel Grumbach 			       &mvm->status))
1197e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1198e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1199e705c121SKalle Valo 
1200e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1201e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1202e705c121SKalle Valo 	 */
1203e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1204e705c121SKalle Valo 		int i;
1205e705c121SKalle Valo 
1206e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1207e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1208e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1209e705c121SKalle Valo 				      i))
1210e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1211e705c121SKalle Valo 		}
1212e705c121SKalle Valo 	}
1213e705c121SKalle Valo }
1214e705c121SKalle Valo 
1215e705c121SKalle Valo static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1216e705c121SKalle Valo {
1217e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1218e705c121SKalle Valo 
1219e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
122024afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1221771147b0SJohannes Berg 
1222771147b0SJohannes Berg 	/*
1223771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1224771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1225771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
12267174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1227771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1228771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1229771147b0SJohannes Berg 	 */
1230771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1231771147b0SJohannes Berg 
1232d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
123369e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1234e705c121SKalle Valo 
1235f9084775SNathan Errera 	/*
1236f9084775SNathan Errera 	 * The work item could be running or queued if the
1237f9084775SNathan Errera 	 * ROC time event stops just as we get here.
1238f9084775SNathan Errera 	 */
1239f9084775SNathan Errera 	flush_work(&mvm->roc_done_wk);
1240f9084775SNathan Errera 
1241e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1242e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1243e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1244e705c121SKalle Valo 
1245e705c121SKalle Valo 	/*
1246e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1247e705c121SKalle Valo 	 * discover that its list is now empty.
1248e705c121SKalle Valo 	 */
1249e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1250e705c121SKalle Valo }
1251e705c121SKalle Valo 
1252e705c121SKalle Valo static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1253e705c121SKalle Valo {
1254e705c121SKalle Valo 	u16 i;
1255e705c121SKalle Valo 
1256e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1257e705c121SKalle Valo 
1258e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1259e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1260e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1261e705c121SKalle Valo 
1262e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1263e705c121SKalle Valo 	return NULL;
1264e705c121SKalle Valo }
1265e705c121SKalle Valo 
1266e705c121SKalle Valo static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1267e705c121SKalle Valo 				s16 tx_power)
1268e705c121SKalle Valo {
12690791c2fcSHaim Dreyfuss 	int len;
1270216cdfb5SLuca Coelho 	struct iwl_dev_tx_power_cmd cmd = {
1271216cdfb5SLuca Coelho 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1272216cdfb5SLuca Coelho 		.common.mac_context_id =
1273e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1274216cdfb5SLuca Coelho 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1275e705c121SKalle Valo 	};
1276fbb7957dSLuca Coelho 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
1277e80bfd11SMordechay Goodstein 					   REDUCE_TX_POWER_CMD,
1278e80bfd11SMordechay Goodstein 					   IWL_FW_CMD_VER_UNKNOWN);
1279e705c121SKalle Valo 
1280e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1281216cdfb5SLuca Coelho 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1282e705c121SKalle Valo 
1283fbb7957dSLuca Coelho 	if (cmd_ver == 6)
1284fbb7957dSLuca Coelho 		len = sizeof(cmd.v6);
1285fbb7957dSLuca Coelho 	else if (fw_has_api(&mvm->fw->ucode_capa,
12860791c2fcSHaim Dreyfuss 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
12870791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
12880791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
12890791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
12900791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
12910791c2fcSHaim Dreyfuss 	else
1292216cdfb5SLuca Coelho 		len = sizeof(cmd.v3);
1293216cdfb5SLuca Coelho 
1294216cdfb5SLuca Coelho 	/* all structs have the same common part, add it */
1295216cdfb5SLuca Coelho 	len += sizeof(cmd.common);
1296e705c121SKalle Valo 
1297e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
1298e705c121SKalle Valo }
1299e705c121SKalle Valo 
1300f6780614SSara Sharon static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1301f6780614SSara Sharon 				       struct ieee80211_vif *vif)
1302f6780614SSara Sharon {
1303f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1304f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1305f6780614SSara Sharon 	int ret;
1306f6780614SSara Sharon 
1307f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1308f6780614SSara Sharon 
1309f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1310f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
1311f6780614SSara Sharon 
1312f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
1313f6780614SSara Sharon 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
1314f6780614SSara Sharon 							  mvmvif->ap_sta_id);
1315f6780614SSara Sharon 
1316f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1317f6780614SSara Sharon 			ret = -EIO;
1318f6780614SSara Sharon 			goto out_unlock;
1319f6780614SSara Sharon 		}
1320f6780614SSara Sharon 
1321f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1322f6780614SSara Sharon 
1323f6780614SSara Sharon 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1324f6780614SSara Sharon 
13250202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
13260202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1327f6780614SSara Sharon 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1328f6780614SSara Sharon 			if (ret)
1329f6780614SSara Sharon 				goto out_unlock;
1330f6780614SSara Sharon 
1331f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1332f6780614SSara Sharon 		}
13330202bcf0SEmmanuel Grumbach 	}
1334f6780614SSara Sharon 
1335f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1336f6780614SSara Sharon 
1337f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1338f6780614SSara Sharon 
1339f6780614SSara Sharon out_unlock:
1340caf46377SSara Sharon 	if (mvmvif->csa_failed)
1341caf46377SSara Sharon 		ret = -EIO;
1342f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1343f6780614SSara Sharon 
1344f6780614SSara Sharon 	return ret;
1345f6780614SSara Sharon }
1346f6780614SSara Sharon 
1347f6780614SSara Sharon static void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1348f6780614SSara Sharon 					 struct ieee80211_vif *vif)
1349f6780614SSara Sharon {
1350f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1351f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1352f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1353f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1354f6780614SSara Sharon 							  mvmvif->color)),
1355f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1356f6780614SSara Sharon 	};
1357f6780614SSara Sharon 
1358f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1359f6780614SSara Sharon 
1360f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
136158ddd9b6SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
136258ddd9b6SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
136358ddd9b6SEmmanuel Grumbach 		iwl_mvm_remove_csa_period(mvm, vif);
136458ddd9b6SEmmanuel Grumbach 	else
1365f6780614SSara Sharon 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1366f6780614SSara Sharon 					     WIDE_ID(MAC_CONF_GROUP,
1367f6780614SSara Sharon 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1368f6780614SSara Sharon 					     0, sizeof(cmd), &cmd));
1369caf46377SSara Sharon 	mvmvif->csa_failed = true;
1370f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1371f6780614SSara Sharon 
1372caf46377SSara Sharon 	iwl_mvm_post_channel_switch(hw, vif);
1373f6780614SSara Sharon }
1374f6780614SSara Sharon 
1375f6780614SSara Sharon static void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1376f6780614SSara Sharon {
1377f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1378f6780614SSara Sharon 	struct ieee80211_vif *vif;
1379f6780614SSara Sharon 
1380f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1381f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1382f6780614SSara Sharon 
138370162580SShaul Triebitz 	/* Trigger disconnect (should clear the CSA state) */
1384f6780614SSara Sharon 	ieee80211_chswitch_done(vif, false);
1385f6780614SSara Sharon }
1386f6780614SSara Sharon 
1387e705c121SKalle Valo static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1388e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1389e705c121SKalle Valo {
1390e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1391e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1392e705c121SKalle Valo 	int ret;
1393e705c121SKalle Valo 
1394e705c121SKalle Valo 	mvmvif->mvm = mvm;
139586e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
1396e705c121SKalle Valo 
1397e705c121SKalle Valo 	/*
1398e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1399e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1400e705c121SKalle Valo 	 * don't really have to check the types.
1401e705c121SKalle Valo 	 */
1402e705c121SKalle Valo 
1403e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1404e705c121SKalle Valo 
1405e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1406e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1407e705c121SKalle Valo 		mvmvif->beacon_stats.accu_num_beacons +=
1408e705c121SKalle Valo 			mvmvif->beacon_stats.num_beacons;
1409e705c121SKalle Valo 
1410e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
1411e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1412e705c121SKalle Valo 	if (ret)
1413e705c121SKalle Valo 		goto out_unlock;
1414e705c121SKalle Valo 
1415698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1416698478c4SSara Sharon 
1417e705c121SKalle Valo 	/* Counting number of interfaces is needed for legacy PM */
1418e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1419e705c121SKalle Valo 		mvm->vif_count++;
1420e705c121SKalle Valo 
1421e705c121SKalle Valo 	/*
1422e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1423e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1424e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1425e705c121SKalle Valo 	 * only after the binding.
1426e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1427e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1428e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1429e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1430e705c121SKalle Valo 	 * allocating resources :)
1431e705c121SKalle Valo 	 */
1432e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1433e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1434e705c121SKalle Valo 		ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1435e705c121SKalle Valo 		if (ret) {
1436e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1437e705c121SKalle Valo 			goto out_release;
1438e705c121SKalle Valo 		}
1439e705c121SKalle Valo 
144026d6c16bSSara Sharon 		/*
144126d6c16bSSara Sharon 		 * Only queue for this station is the mcast queue,
144226d6c16bSSara Sharon 		 * which shouldn't be in TFD mask anyway
144326d6c16bSSara Sharon 		 */
144426d6c16bSSara Sharon 		ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta,
1445ced19f26SSara Sharon 					       0, vif->type,
1446ced19f26SSara Sharon 					       IWL_STA_MULTICAST);
144726d6c16bSSara Sharon 		if (ret)
144826d6c16bSSara Sharon 			goto out_release;
144926d6c16bSSara Sharon 
1450e705c121SKalle Valo 		iwl_mvm_vif_dbgfs_register(mvm, vif);
1451e705c121SKalle Valo 		goto out_unlock;
1452e705c121SKalle Valo 	}
1453e705c121SKalle Valo 
1454e705c121SKalle Valo 	mvmvif->features |= hw->netdev_features;
1455e705c121SKalle Valo 
1456e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1457e705c121SKalle Valo 	if (ret)
1458e705c121SKalle Valo 		goto out_release;
1459e705c121SKalle Valo 
1460e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1461e705c121SKalle Valo 	if (ret)
1462e705c121SKalle Valo 		goto out_remove_mac;
1463e705c121SKalle Valo 
1464e705c121SKalle Valo 	/* beacon filtering */
1465e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1466e705c121SKalle Valo 	if (ret)
1467e705c121SKalle Valo 		goto out_remove_mac;
1468e705c121SKalle Valo 
1469e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1470e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1471e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1472e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1473e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1474e705c121SKalle Valo 	}
1475e705c121SKalle Valo 
1476e705c121SKalle Valo 	/*
1477e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1478e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1479e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1480e705c121SKalle Valo 	 */
1481e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1482e705c121SKalle Valo 
1483e705c121SKalle Valo 		mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1484e705c121SKalle Valo 		if (!mvmvif->phy_ctxt) {
1485e705c121SKalle Valo 			ret = -ENOSPC;
1486e705c121SKalle Valo 			goto out_free_bf;
1487e705c121SKalle Valo 		}
1488e705c121SKalle Valo 
1489e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1490e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1491e705c121SKalle Valo 		if (ret)
1492e705c121SKalle Valo 			goto out_unref_phy;
1493e705c121SKalle Valo 
1494d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1495e705c121SKalle Valo 		if (ret)
1496e705c121SKalle Valo 			goto out_unbind;
1497e705c121SKalle Valo 
1498e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1499e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1500e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1501e705c121SKalle Valo 	}
1502e705c121SKalle Valo 
1503b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1504f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1505f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1506b0ffe455SJohannes Berg 
1507baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1508baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
1509baf41bc3SShaul Triebitz 
1510e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
1511e705c121SKalle Valo 	goto out_unlock;
1512e705c121SKalle Valo 
1513e705c121SKalle Valo  out_unbind:
1514e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1515e705c121SKalle Valo  out_unref_phy:
1516e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1517e705c121SKalle Valo  out_free_bf:
1518e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1519e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1520e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1521e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1522e705c121SKalle Valo 	}
1523e705c121SKalle Valo  out_remove_mac:
1524e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1525e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1526e705c121SKalle Valo  out_release:
1527e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1528e705c121SKalle Valo 		mvm->vif_count--;
1529e705c121SKalle Valo  out_unlock:
1530e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1531e705c121SKalle Valo 
1532e705c121SKalle Valo 	return ret;
1533e705c121SKalle Valo }
1534e705c121SKalle Valo 
1535e705c121SKalle Valo static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1536e705c121SKalle Valo 					struct ieee80211_vif *vif)
1537e705c121SKalle Valo {
1538e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1539e705c121SKalle Valo 		/*
1540e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1541e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1542e705c121SKalle Valo 		 * queue are sent in ROC session.
1543e705c121SKalle Valo 		 */
1544e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1545e705c121SKalle Valo 	}
1546e705c121SKalle Valo }
1547e705c121SKalle Valo 
1548e705c121SKalle Valo static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1549e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1550e705c121SKalle Valo {
1551e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1552e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
155386e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1554e705c121SKalle Valo 
1555e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1556e705c121SKalle Valo 
1557b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1558b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1559b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1560b0ffe455SJohannes Berg 
1561e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1562e705c121SKalle Valo 
156386e177d8SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->probe_resp_data,
156486e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
156586e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
156686e177d8SGregory Greenman 	if (probe_data)
156786e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
156886e177d8SGregory Greenman 
1569e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1570e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1571e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1572e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1573e705c121SKalle Valo 	}
1574e705c121SKalle Valo 
1575b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1576b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1577b73f9a4aSJohannes Berg 
1578e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1579e705c121SKalle Valo 
1580e705c121SKalle Valo 	/*
1581e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1582e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1583e705c121SKalle Valo 	 */
1584e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1585e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1586e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1587e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1588e705c121SKalle Valo 			mvm->noa_vif = NULL;
1589e705c121SKalle Valo 			mvm->noa_duration = 0;
1590e705c121SKalle Valo 		}
1591e705c121SKalle Valo #endif
159226d6c16bSSara Sharon 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
1593e705c121SKalle Valo 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1594e705c121SKalle Valo 		goto out_release;
1595e705c121SKalle Valo 	}
1596e705c121SKalle Valo 
1597e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1598e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1599d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1600e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1601e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1602e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
1603e705c121SKalle Valo 	}
1604e705c121SKalle Valo 
1605e705c121SKalle Valo 	if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1606e705c121SKalle Valo 		mvm->vif_count--;
1607e705c121SKalle Valo 
1608e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
1609e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1610e705c121SKalle Valo 
1611698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1612698478c4SSara Sharon 
1613baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1614baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1615baf41bc3SShaul Triebitz 
1616e705c121SKalle Valo out_release:
1617e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1618e705c121SKalle Valo }
1619e705c121SKalle Valo 
1620e705c121SKalle Valo static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1621e705c121SKalle Valo {
1622e705c121SKalle Valo 	return 0;
1623e705c121SKalle Valo }
1624e705c121SKalle Valo 
1625e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1626e705c121SKalle Valo 	struct iwl_mvm *mvm;
1627e705c121SKalle Valo 	int port_id;
1628e705c121SKalle Valo };
1629e705c121SKalle Valo 
1630e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1631e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1632e705c121SKalle Valo {
1633e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1634e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1635e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
163697bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
163797bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
163897bce57bSLuca Coelho 		.flags = CMD_ASYNC,
163997bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
164097bce57bSLuca Coelho 	};
1641e705c121SKalle Valo 	int ret, len;
1642e705c121SKalle Valo 
1643e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1644e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1645e705c121SKalle Valo 		return;
1646e705c121SKalle Valo 
1647e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1648e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1649e705c121SKalle Valo 		return;
1650e705c121SKalle Valo 
1651e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1652e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1653e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1654e705c121SKalle Valo 
165597bce57bSLuca Coelho 	hcmd.len[0] = len;
165697bce57bSLuca Coelho 	hcmd.data[0] = cmd;
165797bce57bSLuca Coelho 
165897bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1659e705c121SKalle Valo 	if (ret)
1660e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1661e705c121SKalle Valo }
1662e705c121SKalle Valo 
1663e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1664e705c121SKalle Valo {
1665e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1666e705c121SKalle Valo 		.mvm = mvm,
1667e705c121SKalle Valo 	};
1668e705c121SKalle Valo 
1669e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1670e705c121SKalle Valo 
1671e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1672e705c121SKalle Valo 		return;
1673e705c121SKalle Valo 
1674e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1675e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1676e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1677e705c121SKalle Valo }
1678e705c121SKalle Valo 
1679e705c121SKalle Valo static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1680e705c121SKalle Valo 				     struct netdev_hw_addr_list *mc_list)
1681e705c121SKalle Valo {
1682e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1683e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1684e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1685e705c121SKalle Valo 	int addr_count;
1686e705c121SKalle Valo 	bool pass_all;
1687e705c121SKalle Valo 	int len;
1688e705c121SKalle Valo 
1689e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1690e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1691e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1692e705c121SKalle Valo 	if (pass_all)
1693e705c121SKalle Valo 		addr_count = 0;
1694e705c121SKalle Valo 
1695e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1696e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1697e705c121SKalle Valo 	if (!cmd)
1698e705c121SKalle Valo 		return 0;
1699e705c121SKalle Valo 
1700e705c121SKalle Valo 	if (pass_all) {
1701e705c121SKalle Valo 		cmd->pass_all = 1;
1702e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1703e705c121SKalle Valo 	}
1704e705c121SKalle Valo 
1705e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1706e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1707e705c121SKalle Valo 				   cmd->count, addr->addr);
1708e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1709e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1710e705c121SKalle Valo 		cmd->count++;
1711e705c121SKalle Valo 	}
1712e705c121SKalle Valo 
1713e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1714e705c121SKalle Valo }
1715e705c121SKalle Valo 
1716e705c121SKalle Valo static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1717e705c121SKalle Valo 				     unsigned int changed_flags,
1718e705c121SKalle Valo 				     unsigned int *total_flags,
1719e705c121SKalle Valo 				     u64 multicast)
1720e705c121SKalle Valo {
1721e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1722e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1723e705c121SKalle Valo 
1724e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1725e705c121SKalle Valo 
1726e705c121SKalle Valo 	/* replace previous configuration */
1727e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1728e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1729e705c121SKalle Valo 
1730e705c121SKalle Valo 	if (!cmd)
1731e705c121SKalle Valo 		goto out;
1732e705c121SKalle Valo 
173361e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
173461e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
173561e7d91bSLuca Coelho 
173661e7d91bSLuca Coelho 	if (cmd->pass_all)
173761e7d91bSLuca Coelho 		cmd->count = 0;
173861e7d91bSLuca Coelho 
1739e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1740e705c121SKalle Valo out:
1741e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1742e705c121SKalle Valo 	*total_flags = 0;
1743e705c121SKalle Valo }
1744e705c121SKalle Valo 
1745e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1746e705c121SKalle Valo 					struct ieee80211_vif *vif,
1747e705c121SKalle Valo 					unsigned int filter_flags,
1748e705c121SKalle Valo 					unsigned int changed_flags)
1749e705c121SKalle Valo {
1750e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1751e705c121SKalle Valo 
1752e705c121SKalle Valo 	/* We support only filter for probe requests */
1753e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1754e705c121SKalle Valo 		return;
1755e705c121SKalle Valo 
1756e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1757e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1758e705c121SKalle Valo 	    !vif->p2p)
1759e705c121SKalle Valo 		return;
1760e705c121SKalle Valo 
1761e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1762e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1763e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1764e705c121SKalle Valo }
1765e705c121SKalle Valo 
1766e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1767e705c121SKalle Valo struct iwl_bcast_iter_data {
1768e705c121SKalle Valo 	struct iwl_mvm *mvm;
1769e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd;
1770e705c121SKalle Valo 	u8 current_filter;
1771e705c121SKalle Valo };
1772e705c121SKalle Valo 
1773e705c121SKalle Valo static void
1774e705c121SKalle Valo iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1775e705c121SKalle Valo 			 const struct iwl_fw_bcast_filter *in_filter,
1776e705c121SKalle Valo 			 struct iwl_fw_bcast_filter *out_filter)
1777e705c121SKalle Valo {
1778e705c121SKalle Valo 	struct iwl_fw_bcast_filter_attr *attr;
1779e705c121SKalle Valo 	int i;
1780e705c121SKalle Valo 
1781e705c121SKalle Valo 	memcpy(out_filter, in_filter, sizeof(*out_filter));
1782e705c121SKalle Valo 
1783e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1784e705c121SKalle Valo 		attr = &out_filter->attrs[i];
1785e705c121SKalle Valo 
1786e705c121SKalle Valo 		if (!attr->mask)
1787e705c121SKalle Valo 			break;
1788e705c121SKalle Valo 
1789e705c121SKalle Valo 		switch (attr->reserved1) {
1790e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_IP):
1791e705c121SKalle Valo 			if (vif->bss_conf.arp_addr_cnt != 1) {
1792e705c121SKalle Valo 				attr->mask = 0;
1793e705c121SKalle Valo 				continue;
1794e705c121SKalle Valo 			}
1795e705c121SKalle Valo 
1796e705c121SKalle Valo 			attr->val = vif->bss_conf.arp_addr_list[0];
1797e705c121SKalle Valo 			break;
1798e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1799e705c121SKalle Valo 			attr->val = *(__be32 *)&vif->addr[2];
1800e705c121SKalle Valo 			break;
1801e705c121SKalle Valo 		default:
1802e705c121SKalle Valo 			break;
1803e705c121SKalle Valo 		}
1804e705c121SKalle Valo 		attr->reserved1 = 0;
1805e705c121SKalle Valo 		out_filter->num_attrs++;
1806e705c121SKalle Valo 	}
1807e705c121SKalle Valo }
1808e705c121SKalle Valo 
1809e705c121SKalle Valo static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1810e705c121SKalle Valo 					  struct ieee80211_vif *vif)
1811e705c121SKalle Valo {
1812e705c121SKalle Valo 	struct iwl_bcast_iter_data *data = _data;
1813e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1814e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd = data->cmd;
1815e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1816e705c121SKalle Valo 	struct iwl_fw_bcast_mac *bcast_mac;
1817e705c121SKalle Valo 	int i;
1818e705c121SKalle Valo 
1819e705c121SKalle Valo 	if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1820e705c121SKalle Valo 		return;
1821e705c121SKalle Valo 
1822e705c121SKalle Valo 	bcast_mac = &cmd->macs[mvmvif->id];
1823e705c121SKalle Valo 
1824e705c121SKalle Valo 	/*
1825e705c121SKalle Valo 	 * enable filtering only for associated stations, but not for P2P
1826e705c121SKalle Valo 	 * Clients
1827e705c121SKalle Valo 	 */
1828e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1829e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1830e705c121SKalle Valo 		return;
1831e705c121SKalle Valo 
1832e705c121SKalle Valo 	bcast_mac->default_discard = 1;
1833e705c121SKalle Valo 
1834e705c121SKalle Valo 	/* copy all configured filters */
1835e705c121SKalle Valo 	for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1836e705c121SKalle Valo 		/*
1837e705c121SKalle Valo 		 * Make sure we don't exceed our filters limit.
1838e705c121SKalle Valo 		 * if there is still a valid filter to be configured,
1839e705c121SKalle Valo 		 * be on the safe side and just allow bcast for this mac.
1840e705c121SKalle Valo 		 */
1841e705c121SKalle Valo 		if (WARN_ON_ONCE(data->current_filter >=
1842e705c121SKalle Valo 				 ARRAY_SIZE(cmd->filters))) {
1843e705c121SKalle Valo 			bcast_mac->default_discard = 0;
1844e705c121SKalle Valo 			bcast_mac->attached_filters = 0;
1845e705c121SKalle Valo 			break;
1846e705c121SKalle Valo 		}
1847e705c121SKalle Valo 
1848e705c121SKalle Valo 		iwl_mvm_set_bcast_filter(vif,
1849e705c121SKalle Valo 					 &mvm->bcast_filters[i],
1850e705c121SKalle Valo 					 &cmd->filters[data->current_filter]);
1851e705c121SKalle Valo 
1852e705c121SKalle Valo 		/* skip current filter if it contains no attributes */
1853e705c121SKalle Valo 		if (!cmd->filters[data->current_filter].num_attrs)
1854e705c121SKalle Valo 			continue;
1855e705c121SKalle Valo 
1856e705c121SKalle Valo 		/* attach the filter to current mac */
1857e705c121SKalle Valo 		bcast_mac->attached_filters |=
1858e705c121SKalle Valo 				cpu_to_le16(BIT(data->current_filter));
1859e705c121SKalle Valo 
1860e705c121SKalle Valo 		data->current_filter++;
1861e705c121SKalle Valo 	}
1862e705c121SKalle Valo }
1863e705c121SKalle Valo 
1864e705c121SKalle Valo bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1865e705c121SKalle Valo 				    struct iwl_bcast_filter_cmd *cmd)
1866e705c121SKalle Valo {
1867e705c121SKalle Valo 	struct iwl_bcast_iter_data iter_data = {
1868e705c121SKalle Valo 		.mvm = mvm,
1869e705c121SKalle Valo 		.cmd = cmd,
1870e705c121SKalle Valo 	};
1871e705c121SKalle Valo 
1872e705c121SKalle Valo 	if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1873e705c121SKalle Valo 		return false;
1874e705c121SKalle Valo 
1875e705c121SKalle Valo 	memset(cmd, 0, sizeof(*cmd));
1876e705c121SKalle Valo 	cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1877e705c121SKalle Valo 	cmd->max_macs = ARRAY_SIZE(cmd->macs);
1878e705c121SKalle Valo 
1879e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
1880e705c121SKalle Valo 	/* use debugfs filters/macs if override is configured */
1881e705c121SKalle Valo 	if (mvm->dbgfs_bcast_filtering.override) {
1882e705c121SKalle Valo 		memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1883e705c121SKalle Valo 		       sizeof(cmd->filters));
1884e705c121SKalle Valo 		memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1885e705c121SKalle Valo 		       sizeof(cmd->macs));
1886e705c121SKalle Valo 		return true;
1887e705c121SKalle Valo 	}
1888e705c121SKalle Valo #endif
1889e705c121SKalle Valo 
1890e705c121SKalle Valo 	/* if no filters are configured, do nothing */
1891e705c121SKalle Valo 	if (!mvm->bcast_filters)
1892e705c121SKalle Valo 		return false;
1893e705c121SKalle Valo 
1894e705c121SKalle Valo 	/* configure and attach these filters for each associated sta vif */
1895e705c121SKalle Valo 	ieee80211_iterate_active_interfaces(
1896e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1897e705c121SKalle Valo 		iwl_mvm_bcast_filter_iterator, &iter_data);
1898e705c121SKalle Valo 
1899e705c121SKalle Valo 	return true;
1900e705c121SKalle Valo }
190134672bb3SEliad Peller 
190234672bb3SEliad Peller static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1903e705c121SKalle Valo {
1904e705c121SKalle Valo 	struct iwl_bcast_filter_cmd cmd;
1905e705c121SKalle Valo 
1906e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1907e705c121SKalle Valo 		return 0;
1908e705c121SKalle Valo 
1909e705c121SKalle Valo 	if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1910e705c121SKalle Valo 		return 0;
1911e705c121SKalle Valo 
1912e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1913e705c121SKalle Valo 				    sizeof(cmd), &cmd);
1914e705c121SKalle Valo }
1915e705c121SKalle Valo #else
191634672bb3SEliad Peller static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1917e705c121SKalle Valo {
1918e705c121SKalle Valo 	return 0;
1919e705c121SKalle Valo }
1920e705c121SKalle Valo #endif
1921e705c121SKalle Valo 
1922a07a8f37SSara Sharon static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
1923a07a8f37SSara Sharon 				    struct ieee80211_vif *vif)
1924a07a8f37SSara Sharon {
1925a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1926a07a8f37SSara Sharon 
1927a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1928a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1929a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1930a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1931a07a8f37SSara Sharon 
1932a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1933a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1934a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1935a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1936a07a8f37SSara Sharon }
1937a07a8f37SSara Sharon 
1938f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1939f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1940f92659a1SSara Sharon {
1941f92659a1SSara Sharon 	if (vif->mu_mimo_owner) {
1942f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1943f92659a1SSara Sharon 
1944f92659a1SSara Sharon 		/*
1945f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1946f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1947f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
1948f92659a1SSara Sharon 		 */
1949f92659a1SSara Sharon 		ieee80211_update_mu_groups(vif,
1950f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
1951f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
1952f92659a1SSara Sharon 	}
1953f92659a1SSara Sharon }
1954f92659a1SSara Sharon 
1955f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1956f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
1957f92659a1SSara Sharon {
1958f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1959f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1960f92659a1SSara Sharon 
1961f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
1962f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1963f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
1964f92659a1SSara Sharon }
1965f92659a1SSara Sharon 
1966514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1967514c3069SLuca Coelho {
1968514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
1969514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
1970514c3069SLuca Coelho 	u8 residue_bits;
1971514c3069SLuca Coelho 	u8 res;
1972514c3069SLuca Coelho 
1973514c3069SLuca Coelho 	if (bit_num <= 5)
1974514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
1975514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
1976514c3069SLuca Coelho 
1977514c3069SLuca Coelho 	/*
1978514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
1979514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
1980514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
1981514c3069SLuca Coelho 	 */
1982514c3069SLuca Coelho 
1983514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
1984514c3069SLuca Coelho 
1985514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
1986514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
1987514c3069SLuca Coelho 	      residue_bits;
1988514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
1989514c3069SLuca Coelho 
1990514c3069SLuca Coelho 	return res;
1991514c3069SLuca Coelho }
1992514c3069SLuca Coelho 
1993514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
1994514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
1995514c3069SLuca Coelho {
1996514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1997514c3069SLuca Coelho 	struct iwl_he_sta_context_cmd sta_ctxt_cmd = {
1998514c3069SLuca Coelho 		.sta_id = sta_id,
1999514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2000dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2001514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2002514c3069SLuca Coelho 		.frame_time_rts_th =
2003514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2004514c3069SLuca Coelho 	};
2005d14ae796SSara Sharon 	int size = fw_has_api(&mvm->fw->ucode_capa,
2006d14ae796SSara Sharon 			      IWL_UCODE_TLV_API_MBSSID_HE) ?
2007d14ae796SSara Sharon 		   sizeof(sta_ctxt_cmd) :
2008d14ae796SSara Sharon 		   sizeof(struct iwl_he_sta_context_cmd_v1);
2009514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2010514c3069SLuca Coelho 	u32 flags;
2011514c3069SLuca Coelho 	int i;
2012ee1a02d7SShaul Triebitz 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
2013ee1a02d7SShaul Triebitz 	struct ieee80211_chanctx_conf *chanctx_conf;
2014ee1a02d7SShaul Triebitz 	const struct ieee80211_supported_band *sband;
2015514c3069SLuca Coelho 
2016514c3069SLuca Coelho 	rcu_read_lock();
2017514c3069SLuca Coelho 
2018ee1a02d7SShaul Triebitz 	chanctx_conf = rcu_dereference(vif->chanctx_conf);
2019ee1a02d7SShaul Triebitz 	if (WARN_ON(!chanctx_conf)) {
2020ee1a02d7SShaul Triebitz 		rcu_read_unlock();
2021ee1a02d7SShaul Triebitz 		return;
2022ee1a02d7SShaul Triebitz 	}
2023ee1a02d7SShaul Triebitz 
2024ee1a02d7SShaul Triebitz 	sband = mvm->hw->wiphy->bands[chanctx_conf->def.chan->band];
2025ee1a02d7SShaul Triebitz 	own_he_cap = ieee80211_get_he_iftype_cap(sband, vif->type);
2026ee1a02d7SShaul Triebitz 
2027514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
202812d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2029514c3069SLuca Coelho 		rcu_read_unlock();
2030514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2031514c3069SLuca Coelho 		return;
2032514c3069SLuca Coelho 	}
2033514c3069SLuca Coelho 
2034514c3069SLuca Coelho 	if (!sta->he_cap.has_he) {
2035514c3069SLuca Coelho 		rcu_read_unlock();
2036514c3069SLuca Coelho 		return;
2037514c3069SLuca Coelho 	}
2038514c3069SLuca Coelho 
2039514c3069SLuca Coelho 	flags = 0;
2040514c3069SLuca Coelho 
20414f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
20424f58121dSIlan Peer 	if (mvmvif->he_ru_2mhz_block)
20434f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
20444f58121dSIlan Peer 
2045514c3069SLuca Coelho 	/* HTC flags */
2046514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[0] &
2047514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP0_HTC_HE)
2048514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2049514c3069SLuca Coelho 	if ((sta->he_cap.he_cap_elem.mac_cap_info[1] &
2050514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2051514c3069SLuca Coelho 	    (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2052514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2053514c3069SLuca Coelho 		u8 link_adap =
2054514c3069SLuca Coelho 			((sta->he_cap.he_cap_elem.mac_cap_info[2] &
2055514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2056514c3069SLuca Coelho 			 (sta->he_cap.he_cap_elem.mac_cap_info[1] &
2057514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2058514c3069SLuca Coelho 
2059514c3069SLuca Coelho 		if (link_adap == 2)
2060514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2061514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2062514c3069SLuca Coelho 		else if (link_adap == 3)
2063514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2064514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2065514c3069SLuca Coelho 	}
2066514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2067514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2068514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[3] &
2069514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2070514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2071514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2072514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2073514c3069SLuca Coelho 
2074189b8d44SNaftali Goldstein 	/*
2075189b8d44SNaftali Goldstein 	 * Initialize the PPE thresholds to "None" (7), as described in Table
2076189b8d44SNaftali Goldstein 	 * 9-262ac of 80211.ax/D3.0.
2077189b8d44SNaftali Goldstein 	 */
2078189b8d44SNaftali Goldstein 	memset(&sta_ctxt_cmd.pkt_ext, 7, sizeof(sta_ctxt_cmd.pkt_ext));
2079189b8d44SNaftali Goldstein 
2080189b8d44SNaftali Goldstein 	/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2081514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.phy_cap_info[6] &
2082514c3069SLuca Coelho 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2083514c3069SLuca Coelho 		u8 nss = (sta->he_cap.ppe_thres[0] &
2084514c3069SLuca Coelho 			  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2085514c3069SLuca Coelho 		u8 ru_index_bitmap =
2086514c3069SLuca Coelho 			(sta->he_cap.ppe_thres[0] &
2087514c3069SLuca Coelho 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2088514c3069SLuca Coelho 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2089514c3069SLuca Coelho 		u8 *ppe = &sta->he_cap.ppe_thres[0];
2090514c3069SLuca Coelho 		u8 ppe_pos_bit = 7; /* Starting after PPE header */
2091514c3069SLuca Coelho 
2092514c3069SLuca Coelho 		/*
2093514c3069SLuca Coelho 		 * FW currently supports only nss == MAX_HE_SUPP_NSS
2094514c3069SLuca Coelho 		 *
2095514c3069SLuca Coelho 		 * If nss > MAX: we can ignore values we don't support
2096514c3069SLuca Coelho 		 * If nss < MAX: we can set zeros in other streams
2097514c3069SLuca Coelho 		 */
2098514c3069SLuca Coelho 		if (nss > MAX_HE_SUPP_NSS) {
2099514c3069SLuca Coelho 			IWL_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2100514c3069SLuca Coelho 				 MAX_HE_SUPP_NSS);
2101514c3069SLuca Coelho 			nss = MAX_HE_SUPP_NSS;
2102514c3069SLuca Coelho 		}
2103514c3069SLuca Coelho 
2104514c3069SLuca Coelho 		for (i = 0; i < nss; i++) {
2105514c3069SLuca Coelho 			u8 ru_index_tmp = ru_index_bitmap << 1;
2106514c3069SLuca Coelho 			u8 bw;
2107514c3069SLuca Coelho 
2108514c3069SLuca Coelho 			for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX; bw++) {
2109514c3069SLuca Coelho 				ru_index_tmp >>= 1;
2110514c3069SLuca Coelho 				if (!(ru_index_tmp & 1))
2111514c3069SLuca Coelho 					continue;
2112514c3069SLuca Coelho 
2113514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][1] =
2114514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2115514c3069SLuca Coelho 							       ppe_pos_bit);
2116514c3069SLuca Coelho 				ppe_pos_bit +=
2117514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2118514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0] =
2119514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2120514c3069SLuca Coelho 							       ppe_pos_bit);
2121514c3069SLuca Coelho 				ppe_pos_bit +=
2122514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2123514c3069SLuca Coelho 			}
2124514c3069SLuca Coelho 		}
2125514c3069SLuca Coelho 
2126514c3069SLuca Coelho 		flags |= STA_CTXT_HE_PACKET_EXT;
212777ff2c6bSLiad Kaufman 	} else if ((sta->he_cap.he_cap_elem.phy_cap_info[9] &
212877ff2c6bSLiad Kaufman 		    IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) !=
212977ff2c6bSLiad Kaufman 		  IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_RESERVED) {
213077ff2c6bSLiad Kaufman 		int low_th = -1;
213177ff2c6bSLiad Kaufman 		int high_th = -1;
213277ff2c6bSLiad Kaufman 
213377ff2c6bSLiad Kaufman 		/* Take the PPE thresholds from the nominal padding info */
213477ff2c6bSLiad Kaufman 		switch (sta->he_cap.he_cap_elem.phy_cap_info[9] &
213577ff2c6bSLiad Kaufman 			IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) {
213677ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_0US:
213777ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
213877ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
213977ff2c6bSLiad Kaufman 			break;
214077ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_8US:
214177ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_BPSK;
214277ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
214377ff2c6bSLiad Kaufman 			break;
214477ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US:
214577ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
214677ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_BPSK;
214777ff2c6bSLiad Kaufman 			break;
214877ff2c6bSLiad Kaufman 		}
214977ff2c6bSLiad Kaufman 
215077ff2c6bSLiad Kaufman 		/* Set the PPE thresholds accordingly */
215177ff2c6bSLiad Kaufman 		if (low_th >= 0 && high_th >= 0) {
21520bfefe2fSLiad Kaufman 			struct iwl_he_pkt_ext *pkt_ext =
21530bfefe2fSLiad Kaufman 				(struct iwl_he_pkt_ext *)&sta_ctxt_cmd.pkt_ext;
215477ff2c6bSLiad Kaufman 
215577ff2c6bSLiad Kaufman 			for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
215677ff2c6bSLiad Kaufman 				u8 bw;
215777ff2c6bSLiad Kaufman 
215877ff2c6bSLiad Kaufman 				for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX;
215977ff2c6bSLiad Kaufman 				     bw++) {
21600bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][0] =
21610bfefe2fSLiad Kaufman 						low_th;
21620bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][1] =
21630bfefe2fSLiad Kaufman 						high_th;
216477ff2c6bSLiad Kaufman 				}
216577ff2c6bSLiad Kaufman 			}
216677ff2c6bSLiad Kaufman 
216777ff2c6bSLiad Kaufman 			flags |= STA_CTXT_HE_PACKET_EXT;
216877ff2c6bSLiad Kaufman 		}
2169514c3069SLuca Coelho 	}
217073f23d91SShaul Triebitz 
217173f23d91SShaul Triebitz 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] &
217273f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
217373f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
217473f23d91SShaul Triebitz 
217573f23d91SShaul Triebitz 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] &
217673f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
217773f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
217873f23d91SShaul Triebitz 
2179514c3069SLuca Coelho 	rcu_read_unlock();
2180514c3069SLuca Coelho 
2181514c3069SLuca Coelho 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2182514c3069SLuca Coelho 	flags |= STA_CTXT_HE_MU_EDCA_CW;
21835cc74f65SShaul Triebitz 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2184514c3069SLuca Coelho 		struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2185514c3069SLuca Coelho 			&mvmvif->queue_params[i].mu_edca_param_rec;
21865cc74f65SShaul Triebitz 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2187514c3069SLuca Coelho 
2188514c3069SLuca Coelho 		if (!mvmvif->queue_params[i].mu_edca) {
2189514c3069SLuca Coelho 			flags &= ~STA_CTXT_HE_MU_EDCA_CW;
2190514c3069SLuca Coelho 			break;
2191514c3069SLuca Coelho 		}
2192514c3069SLuca Coelho 
21935cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].cwmin =
2194514c3069SLuca Coelho 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
21955cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].cwmax =
2196514c3069SLuca Coelho 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
21975cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].aifsn =
2198514c3069SLuca Coelho 			cpu_to_le16(mu_edca->aifsn);
21995cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].mu_time =
2200514c3069SLuca Coelho 			cpu_to_le16(mu_edca->mu_edca_timer);
2201514c3069SLuca Coelho 	}
2202514c3069SLuca Coelho 
2203514c3069SLuca Coelho 
2204514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2205514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2206514c3069SLuca Coelho 
2207514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2208514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2209514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2210514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2211514c3069SLuca Coelho 	}
2212514c3069SLuca Coelho 
2213ee1a02d7SShaul Triebitz 	if (own_he_cap && !(own_he_cap->he_cap_elem.mac_cap_info[2] &
2214ee1a02d7SShaul Triebitz 			    IEEE80211_HE_MAC_CAP2_ACK_EN))
2215ee1a02d7SShaul Triebitz 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2216ee1a02d7SShaul Triebitz 
2217918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2218918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2219918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2220918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2221d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2222d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2223d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2224d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2225d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2226d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2227918cbf39SSara Sharon 	}
2228514c3069SLuca Coelho 
2229514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2230514c3069SLuca Coelho 
2231514c3069SLuca Coelho 	if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(STA_HE_CTXT_CMD,
2232514c3069SLuca Coelho 						 DATA_PATH_GROUP, 0),
2233d14ae796SSara Sharon 				 0, size, &sta_ctxt_cmd))
2234514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2235514c3069SLuca Coelho }
2236514c3069SLuca Coelho 
2237e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2238e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2239e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
2240e705c121SKalle Valo 					     u32 changes)
2241e705c121SKalle Valo {
2242e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2243e705c121SKalle Valo 	int ret;
2244e705c121SKalle Valo 
2245e705c121SKalle Valo 	/*
2246cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend
2247cdaba917SEmmanuel Grumbach 	 * on the beacon interval, which was not known when the station
2248cdaba917SEmmanuel Grumbach 	 * interface was added.
2249e705c121SKalle Valo 	 */
2250514c3069SLuca Coelho 	if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
2251230ba6c5SLuca Coelho 		if (vif->bss_conf.he_support &&
2252230ba6c5SLuca Coelho 		    !iwlwifi_mod_params.disable_11ax)
2253514c3069SLuca Coelho 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2254514c3069SLuca Coelho 
2255e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2256514c3069SLuca Coelho 	}
2257e705c121SKalle Valo 
225840ecdd01SShaul Triebitz 	/* Update MU EDCA params */
225940ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
226040ecdd01SShaul Triebitz 	    bss_conf->assoc && vif->bss_conf.he_support &&
226140ecdd01SShaul Triebitz 	    !iwlwifi_mod_params.disable_11ax)
226240ecdd01SShaul Triebitz 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
226340ecdd01SShaul Triebitz 
2264e705c121SKalle Valo 	/*
2265e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2266e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2267e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2268e705c121SKalle Valo 	 * branch for disassociation below.
2269e705c121SKalle Valo 	 */
2270e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2271e705c121SKalle Valo 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2272e705c121SKalle Valo 
2273e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
2274e705c121SKalle Valo 	if (ret)
2275e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2276e705c121SKalle Valo 
2277e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2278e705c121SKalle Valo 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2279e705c121SKalle Valo 	mvmvif->associated = bss_conf->assoc;
2280e705c121SKalle Valo 
2281e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2282e705c121SKalle Valo 		if (bss_conf->assoc) {
2283e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2284e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2285e705c121SKalle Valo 			memset(&mvmvif->beacon_stats, 0,
2286e705c121SKalle Valo 			       sizeof(mvmvif->beacon_stats));
2287e705c121SKalle Valo 
2288e705c121SKalle Valo 			/* add quota for this interface */
2289e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2290e705c121SKalle Valo 			if (ret) {
2291e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2292e705c121SKalle Valo 				return;
2293e705c121SKalle Valo 			}
2294e705c121SKalle Valo 
2295e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2296fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2297fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2298fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2299e705c121SKalle Valo 				/*
2300e705c121SKalle Valo 				 * If we're restarting then the firmware will
2301e705c121SKalle Valo 				 * obviously have lost synchronisation with
2302e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2303e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2304e705c121SKalle Valo 				 * scheduling a session protection time event.
2305e705c121SKalle Valo 				 *
2306e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2307e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2308e705c121SKalle Valo 				 * the beacon interval.
2309e705c121SKalle Valo 				 *
2310e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2311e705c121SKalle Valo 				 * than a single interface.
2312fe959c7bSEmmanuel Grumbach 				 *
2313fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2314fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2315fe959c7bSEmmanuel Grumbach 				 * only anyway.
2316e705c121SKalle Valo 				 */
2317e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2318e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2319e705c121SKalle Valo 							5 * dur, false);
2320e705c121SKalle Valo 			}
2321e705c121SKalle Valo 
2322e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2323e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2324e705c121SKalle Valo 			if (vif->p2p) {
2325e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2326e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
2327e705c121SKalle Valo 						    IEEE80211_SMPS_DYNAMIC);
2328e705c121SKalle Valo 			}
23290ae98812SSara Sharon 		} else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
2330e705c121SKalle Valo 			/*
2331e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2332e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2333e705c121SKalle Valo 			 */
233469e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
233569e508b4SIlan Peer 			WARN_ONCE(ret &&
233669e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
233769e508b4SIlan Peer 					    &mvm->status),
2338e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2339e705c121SKalle Valo 
23406b28f978SEmmanuel Grumbach 			/*
23416b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
23426b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
23436b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
23446b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
23456b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
23466b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
23476b28f978SEmmanuel Grumbach 			 */
23486b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
23496b28f978SEmmanuel Grumbach 				      &mvm->status)) {
23506b28f978SEmmanuel Grumbach 				/*
23516b28f978SEmmanuel Grumbach 				 * Remove AP station now that
23526b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
23536b28f978SEmmanuel Grumbach 				 */
23546b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
23556b28f978SEmmanuel Grumbach 							mvmvif->ap_sta_id);
2356e705c121SKalle Valo 				if (ret)
23576b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
23586b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2359e705c121SKalle Valo 
23600ae98812SSara Sharon 				mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
23616b28f978SEmmanuel Grumbach 			}
23626b28f978SEmmanuel Grumbach 
2363e705c121SKalle Valo 			/* remove quota for this interface */
2364e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2365e705c121SKalle Valo 			if (ret)
2366e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2367e705c121SKalle Valo 
2368e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2369e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2370e705c121SKalle Valo 			if (ret)
2371e705c121SKalle Valo 				IWL_ERR(mvm,
2372e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2373e705c121SKalle Valo 					vif->addr);
2374e705c121SKalle Valo 		}
2375e705c121SKalle Valo 
2376a07a8f37SSara Sharon 		/*
2377a07a8f37SSara Sharon 		 * The firmware tracks the MU-MIMO group on its own.
2378f92659a1SSara Sharon 		 * However, on HW restart we should restore this data.
2379a07a8f37SSara Sharon 		 */
2380a07a8f37SSara Sharon 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2381f92659a1SSara Sharon 		    (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
2382a07a8f37SSara Sharon 			ret = iwl_mvm_update_mu_groups(mvm, vif);
2383a07a8f37SSara Sharon 			if (ret)
2384a07a8f37SSara Sharon 				IWL_ERR(mvm,
2385a07a8f37SSara Sharon 					"failed to update VHT MU_MIMO groups\n");
2386a07a8f37SSara Sharon 		}
2387a07a8f37SSara Sharon 
2388e705c121SKalle Valo 		iwl_mvm_recalc_multicast(mvm);
238934672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2390e705c121SKalle Valo 
2391e705c121SKalle Valo 		/* reset rssi values */
2392e705c121SKalle Valo 		mvmvif->bf_data.ave_beacon_signal = 0;
2393e705c121SKalle Valo 
2394e705c121SKalle Valo 		iwl_mvm_bt_coex_vif_change(mvm);
2395e705c121SKalle Valo 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2396e705c121SKalle Valo 				    IEEE80211_SMPS_AUTOMATIC);
2397355346baSAvraham Stern 		if (fw_has_capa(&mvm->fw->ucode_capa,
2398355346baSAvraham Stern 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2399355346baSAvraham Stern 			iwl_mvm_config_scan(mvm);
2400b45242c9SAvraham Stern 	}
2401b45242c9SAvraham Stern 
2402b45242c9SAvraham Stern 	if (changes & BSS_CHANGED_BEACON_INFO) {
2403e705c121SKalle Valo 		/*
2404b45242c9SAvraham Stern 		 * We received a beacon from the associated AP so
2405e705c121SKalle Valo 		 * remove the session protection.
2406fe959c7bSEmmanuel Grumbach 		 * A firmware with the new API will remove it automatically.
2407e705c121SKalle Valo 		 */
2408fe959c7bSEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
2409fe959c7bSEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
24103edfb5f4SAvraham Stern 			iwl_mvm_stop_session_protection(mvm, vif);
2411e705c121SKalle Valo 
2412e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2413e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2414e705c121SKalle Valo 	}
2415e705c121SKalle Valo 
2416283115fbSAvri Altman 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2417283115fbSAvri Altman 		       /*
2418283115fbSAvri Altman 			* Send power command on every beacon change,
2419283115fbSAvri Altman 			* because we may have not enabled beacon abort yet.
2420283115fbSAvri Altman 			*/
2421283115fbSAvri Altman 		       BSS_CHANGED_BEACON_INFO)) {
2422e705c121SKalle Valo 		ret = iwl_mvm_power_update_mac(mvm);
2423e705c121SKalle Valo 		if (ret)
2424e705c121SKalle Valo 			IWL_ERR(mvm, "failed to update power mode\n");
2425e705c121SKalle Valo 	}
2426e705c121SKalle Valo 
2427e705c121SKalle Valo 	if (changes & BSS_CHANGED_CQM) {
2428e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2429e705c121SKalle Valo 		/* reset cqm events tracking */
2430e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = 0;
2431e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
2432e705c121SKalle Valo 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2433e705c121SKalle Valo 			if (ret)
2434e705c121SKalle Valo 				IWL_ERR(mvm,
2435e705c121SKalle Valo 					"failed to update CQM thresholds\n");
2436e705c121SKalle Valo 		}
2437e705c121SKalle Valo 	}
2438e705c121SKalle Valo 
2439e705c121SKalle Valo 	if (changes & BSS_CHANGED_ARP_FILTER) {
2440e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
244134672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2442e705c121SKalle Valo 	}
2443*de34d1c1SJohannes Berg 
2444*de34d1c1SJohannes Berg 	if (changes & BSS_CHANGED_BANDWIDTH)
2445*de34d1c1SJohannes Berg 		iwl_mvm_apply_fw_smps_request(vif);
2446e705c121SKalle Valo }
2447e705c121SKalle Valo 
2448e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2449e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2450e705c121SKalle Valo {
2451e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2452e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2453c56e00a3SJohannes Berg 	int ret, i;
2454e705c121SKalle Valo 
2455e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2456e705c121SKalle Valo 
2457e705c121SKalle Valo 	/* Send the beacon template */
2458e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2459e705c121SKalle Valo 	if (ret)
2460e705c121SKalle Valo 		goto out_unlock;
2461e705c121SKalle Valo 
2462e705c121SKalle Valo 	/*
2463cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2464cdaba917SEmmanuel Grumbach 	 * the beacon interval, which was not known when the AP interface
2465cdaba917SEmmanuel Grumbach 	 * was added.
2466e705c121SKalle Valo 	 */
2467e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2468e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2469e705c121SKalle Valo 
2470e705c121SKalle Valo 	mvmvif->ap_assoc_sta_count = 0;
2471e705c121SKalle Valo 
2472e705c121SKalle Valo 	/* Add the mac context */
2473e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2474e705c121SKalle Valo 	if (ret)
2475e705c121SKalle Valo 		goto out_unlock;
2476e705c121SKalle Valo 
2477e705c121SKalle Valo 	/* Perform the binding */
2478e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2479e705c121SKalle Valo 	if (ret)
2480e705c121SKalle Valo 		goto out_remove;
2481e705c121SKalle Valo 
248263dd5d02SSara Sharon 	/*
248363dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
248463dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
248563dd5d02SSara Sharon 	 * cause assert 0x2b00.
248663dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
248763dd5d02SSara Sharon 	 * the TLV
248863dd5d02SSara Sharon 	 */
248963dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2490ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2491ced19f26SSara Sharon 		if (ret)
2492ced19f26SSara Sharon 			goto out_unbind;
249363dd5d02SSara Sharon 		/*
249463dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
249563dd5d02SSara Sharon 		 * events are added and applied to the scheduler
249663dd5d02SSara Sharon 		 */
2497e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
249863dd5d02SSara Sharon 		if (ret) {
249963dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
250063dd5d02SSara Sharon 			goto out_unbind;
250163dd5d02SSara Sharon 		}
250263dd5d02SSara Sharon 	} else {
250363dd5d02SSara Sharon 		/*
250463dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
250563dd5d02SSara Sharon 		 * events are added and applied to the scheduler
250663dd5d02SSara Sharon 		 */
250775fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2508e705c121SKalle Valo 		if (ret)
250963dd5d02SSara Sharon 			goto out_unbind;
251075fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
251163dd5d02SSara Sharon 		if (ret) {
251263dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
251363dd5d02SSara Sharon 			goto out_unbind;
251463dd5d02SSara Sharon 		}
251563dd5d02SSara Sharon 	}
251626d6c16bSSara Sharon 
2517e705c121SKalle Valo 	/* must be set before quota calculations */
2518e705c121SKalle Valo 	mvmvif->ap_ibss_active = true;
2519e705c121SKalle Valo 
2520c56e00a3SJohannes Berg 	/* send all the early keys to the device now */
2521c56e00a3SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2522c56e00a3SJohannes Berg 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2523c56e00a3SJohannes Berg 
2524c56e00a3SJohannes Berg 		if (!key)
2525c56e00a3SJohannes Berg 			continue;
2526c56e00a3SJohannes Berg 
2527c56e00a3SJohannes Berg 		mvmvif->ap_early_keys[i] = NULL;
2528c56e00a3SJohannes Berg 
25296569e7d3SJohannes Berg 		ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2530c56e00a3SJohannes Berg 		if (ret)
2531c56e00a3SJohannes Berg 			goto out_quota_failed;
2532c56e00a3SJohannes Berg 	}
2533c56e00a3SJohannes Berg 
253447242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
253547242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, true,
253647242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
253747242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
253847242744STova Mussai 	}
253947242744STova Mussai 
2540e705c121SKalle Valo 	/* power updated needs to be done before quotas */
2541e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2542e705c121SKalle Valo 
2543e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2544e705c121SKalle Valo 	if (ret)
2545e705c121SKalle Valo 		goto out_quota_failed;
2546e705c121SKalle Valo 
2547e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2548e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2549e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2550e705c121SKalle Valo 
2551e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2552e705c121SKalle Valo 
2553e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2554e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2555e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2556e705c121SKalle Valo 
2557b73f9a4aSJohannes Berg 	iwl_mvm_ftm_restart_responder(mvm, vif);
2558b73f9a4aSJohannes Berg 
2559e705c121SKalle Valo 	goto out_unlock;
2560e705c121SKalle Valo 
2561e705c121SKalle Valo out_quota_failed:
2562e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2563e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2564e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2565ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2566e705c121SKalle Valo out_unbind:
2567e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2568e705c121SKalle Valo out_remove:
2569e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2570e705c121SKalle Valo out_unlock:
2571e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2572e705c121SKalle Valo 	return ret;
2573e705c121SKalle Valo }
2574e705c121SKalle Valo 
2575e705c121SKalle Valo static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2576e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2577e705c121SKalle Valo {
2578e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2579e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2580e705c121SKalle Valo 
2581e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
2582e705c121SKalle Valo 
2583e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2584e705c121SKalle Valo 
2585e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2586e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2587e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2588e705c121SKalle Valo 					  &mvmvif->time_event_data);
2589e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2590e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2591e705c121SKalle Valo 	}
2592e705c121SKalle Valo 
2593e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2594e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2595e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2596e705c121SKalle Valo 	}
2597e705c121SKalle Valo 
2598e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2599e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2600e705c121SKalle Valo 
260147242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
260247242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
260347242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
260447242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
260547242744STova Mussai 	}
260647242744STova Mussai 
2607e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2608e705c121SKalle Valo 
2609e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2610e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2611e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2612e705c121SKalle Valo 
2613e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2614ced19f26SSara Sharon 
2615be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
2616be82ecd3SAvraham Stern 
2617ced19f26SSara Sharon 	/*
2618ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2619ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2620ced19f26SSara Sharon 	 * cause assert 0x2b00.
2621ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2622ced19f26SSara Sharon 	 * bcast station).
2623ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2624ced19f26SSara Sharon 	 */
2625ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
262626d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2627e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2628ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2629ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2630e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2631e705c121SKalle Valo 
2632e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2633e705c121SKalle Valo 
2634e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2635e705c121SKalle Valo 
2636e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2637e705c121SKalle Valo }
2638e705c121SKalle Valo 
2639e705c121SKalle Valo static void
2640e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2641e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2642e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
2643e705c121SKalle Valo 				 u32 changes)
2644e705c121SKalle Valo {
2645e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2646e705c121SKalle Valo 
2647e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
2648e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
2649e705c121SKalle Valo 		return;
2650e705c121SKalle Valo 
2651e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2652e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2653e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2654e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2655e705c121SKalle Valo 
2656e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
2657e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
2658e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2659e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
2660e705c121SKalle Valo 
2661b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
2662b73f9a4aSJohannes Berg 		int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2663b73f9a4aSJohannes Berg 
2664b73f9a4aSJohannes Berg 		if (ret)
2665b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2666b73f9a4aSJohannes Berg 				 ret);
2667b73f9a4aSJohannes Berg 	}
2668b73f9a4aSJohannes Berg 
2669e705c121SKalle Valo }
2670e705c121SKalle Valo 
2671e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2672e705c121SKalle Valo 				     struct ieee80211_vif *vif,
2673e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
2674e705c121SKalle Valo 				     u32 changes)
2675e705c121SKalle Valo {
2676e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2677e705c121SKalle Valo 
2678e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2679e705c121SKalle Valo 
2680e705c121SKalle Valo 	if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2681e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2682e705c121SKalle Valo 
2683e705c121SKalle Valo 	switch (vif->type) {
2684e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2685e705c121SKalle Valo 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2686e705c121SKalle Valo 		break;
2687e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
2688e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
2689e705c121SKalle Valo 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2690e705c121SKalle Valo 		break;
269191b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
269291b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
269391b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
269491b08c2dSAviya Erenfeld 		break;
2695e705c121SKalle Valo 	default:
2696e705c121SKalle Valo 		/* shouldn't happen */
2697e705c121SKalle Valo 		WARN_ON_ONCE(1);
2698e705c121SKalle Valo 	}
2699e705c121SKalle Valo 
27009aae43a4SJohannes Berg 	if (changes & BSS_CHANGED_TXPOWER) {
27019aae43a4SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
27029aae43a4SJohannes Berg 				bss_conf->txpower);
27039aae43a4SJohannes Berg 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
27049aae43a4SJohannes Berg 	}
27059aae43a4SJohannes Berg 
2706e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2707e705c121SKalle Valo }
2708e705c121SKalle Valo 
2709e705c121SKalle Valo static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2710e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2711e705c121SKalle Valo 			       struct ieee80211_scan_request *hw_req)
2712e705c121SKalle Valo {
2713e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2714e705c121SKalle Valo 	int ret;
2715e705c121SKalle Valo 
2716e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
2717e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2718e705c121SKalle Valo 		return -EINVAL;
2719e705c121SKalle Valo 
2720e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2721e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2722e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2723e705c121SKalle Valo 
2724e705c121SKalle Valo 	return ret;
2725e705c121SKalle Valo }
2726e705c121SKalle Valo 
2727e705c121SKalle Valo static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2728e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2729e705c121SKalle Valo {
2730e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2731e705c121SKalle Valo 
2732e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2733e705c121SKalle Valo 
2734e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2735e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
2736e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
2737e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
2738e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
2739e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
2740e705c121SKalle Valo 	*/
2741e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2742e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2743e705c121SKalle Valo 
2744e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2745e705c121SKalle Valo }
2746e705c121SKalle Valo 
2747e705c121SKalle Valo static void
2748e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2749e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
2750e705c121SKalle Valo 				  int num_frames,
2751e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
2752e705c121SKalle Valo 				  bool more_data)
2753e705c121SKalle Valo {
2754e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2755e705c121SKalle Valo 
2756e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
2757e705c121SKalle Valo 
2758e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2759e705c121SKalle Valo 					  tids, more_data, false);
2760e705c121SKalle Valo }
2761e705c121SKalle Valo 
2762e705c121SKalle Valo static void
2763e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2764e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
2765e705c121SKalle Valo 				    int num_frames,
2766e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
2767e705c121SKalle Valo 				    bool more_data)
2768e705c121SKalle Valo {
2769e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2770e705c121SKalle Valo 
27719a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2772e705c121SKalle Valo 
2773e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2774e705c121SKalle Valo 					  tids, more_data, true);
2775e705c121SKalle Valo }
2776e705c121SKalle Valo 
277765e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2778e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
2779e705c121SKalle Valo 				     struct ieee80211_sta *sta)
2780e705c121SKalle Valo {
2781e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2782e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2783e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
2784e705c121SKalle Valo 	int tid;
2785e705c121SKalle Valo 
2786960f864bSJohannes Berg 	/*
2787960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
2788960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
2789960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
2790960f864bSJohannes Berg 	 */
2791960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2792960f864bSJohannes Berg 		return;
2793960f864bSJohannes Berg 
2794e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2795311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
2796e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2797e705c121SKalle Valo 
27986862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
27991c17627bSSara Sharon 			continue;
28001c17627bSSara Sharon 
2801e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
2802e705c121SKalle Valo 
2803dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
2804e705c121SKalle Valo 			continue;
2805e705c121SKalle Valo 
2806e705c121SKalle Valo 		__set_bit(tid, &tids);
2807e705c121SKalle Valo 	}
2808e705c121SKalle Valo 
2809e705c121SKalle Valo 	switch (cmd) {
2810e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
2811e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
2812e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
2813e705c121SKalle Valo 
2814e705c121SKalle Valo 		if (txqs)
2815e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
2816e705c121SKalle Valo 		/*
2817e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
2818e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
2819e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2820e705c121SKalle Valo 		 */
2821e705c121SKalle Valo 		break;
2822e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
28230ae98812SSara Sharon 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
2824e705c121SKalle Valo 			break;
2825e705c121SKalle Valo 
2826e705c121SKalle Valo 		if (txqs)
2827e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
2828e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
2829e705c121SKalle Valo 		break;
2830e705c121SKalle Valo 	default:
2831e705c121SKalle Valo 		break;
2832e705c121SKalle Valo 	}
2833e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2834e705c121SKalle Valo }
2835e705c121SKalle Valo 
283665e25482SJohannes Berg static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
283765e25482SJohannes Berg 				   struct ieee80211_vif *vif,
283865e25482SJohannes Berg 				   enum sta_notify_cmd cmd,
283965e25482SJohannes Berg 				   struct ieee80211_sta *sta)
284065e25482SJohannes Berg {
284165e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
284265e25482SJohannes Berg }
284365e25482SJohannes Berg 
284465e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
284565e25482SJohannes Berg {
284665e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
284765e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
284865e25482SJohannes Berg 	struct ieee80211_sta *sta;
284965e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
285065e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
285165e25482SJohannes Berg 
2852be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
285365e25482SJohannes Berg 		return;
285465e25482SJohannes Berg 
285565e25482SJohannes Berg 	rcu_read_lock();
2856a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
285765e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
285865e25482SJohannes Berg 		rcu_read_unlock();
285965e25482SJohannes Berg 		return;
286065e25482SJohannes Berg 	}
286165e25482SJohannes Berg 
286265e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
286365e25482SJohannes Berg 
286465e25482SJohannes Berg 	if (!mvmsta->vif ||
286565e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
286665e25482SJohannes Berg 		rcu_read_unlock();
286765e25482SJohannes Berg 		return;
286865e25482SJohannes Berg 	}
286965e25482SJohannes Berg 
287065e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
287165e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
287265e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
287365e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
287465e25482SJohannes Berg 			sta);
287565e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
287665e25482SJohannes Berg 	}
287765e25482SJohannes Berg 
287865e25482SJohannes Berg 	if (sleeping) {
287965e25482SJohannes Berg 		switch (notif->type) {
288065e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
288165e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
288265e25482SJohannes Berg 			break;
288365e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
288465e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
288565e25482SJohannes Berg 			break;
288665e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
288765e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
288865e25482SJohannes Berg 			break;
288965e25482SJohannes Berg 		default:
289065e25482SJohannes Berg 			break;
289165e25482SJohannes Berg 		}
289265e25482SJohannes Berg 	}
289365e25482SJohannes Berg 
289465e25482SJohannes Berg 	rcu_read_unlock();
289565e25482SJohannes Berg }
289665e25482SJohannes Berg 
2897e705c121SKalle Valo static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2898e705c121SKalle Valo 				       struct ieee80211_vif *vif,
2899e705c121SKalle Valo 				       struct ieee80211_sta *sta)
2900e705c121SKalle Valo {
2901e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2902e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2903e705c121SKalle Valo 
2904e705c121SKalle Valo 	/*
2905e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
2906e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
2907e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
2908e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
2909e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
2910e705c121SKalle Valo 	 * callback deleted the station.
2911e705c121SKalle Valo 	 */
2912e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2913e705c121SKalle Valo 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2914e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2915e705c121SKalle Valo 				   ERR_PTR(-ENOENT));
2916e705c121SKalle Valo 
2917e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2918e705c121SKalle Valo }
2919e705c121SKalle Valo 
2920e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2921e705c121SKalle Valo 				const u8 *bssid)
2922e705c121SKalle Valo {
2923b0ffe455SJohannes Berg 	int i;
2924b0ffe455SJohannes Berg 
2925b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2926b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
2927b0ffe455SJohannes Berg 
2928b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
2929b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
2930b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
2931b0ffe455SJohannes Berg 	}
2932b0ffe455SJohannes Berg 
2933e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2934e705c121SKalle Valo 		return;
2935e705c121SKalle Valo 
2936c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
2937cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2938cee5a882SAvri Altman 		return;
2939cee5a882SAvri Altman 	}
2940cee5a882SAvri Altman 
294111dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
294211dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
2943e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2944e705c121SKalle Valo 		return;
2945e705c121SKalle Valo 	}
2946e705c121SKalle Valo 
2947b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
2948b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
2949b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2950b0ffe455SJohannes Berg 			return;
2951b0ffe455SJohannes Berg 		}
2952b0ffe455SJohannes Berg 	}
2953b0ffe455SJohannes Berg 
2954e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2955e705c121SKalle Valo }
2956e705c121SKalle Valo 
29571e8f1329SGolan Ben-Ami static void
29581e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
29591e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
29601e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
29611e8f1329SGolan Ben-Ami {
29621e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
29631e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
29641e8f1329SGolan Ben-Ami 
29656c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
29666c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
29676c042d75SSara Sharon 	if (!trig)
29681e8f1329SGolan Ben-Ami 		return;
29691e8f1329SGolan Ben-Ami 
29701e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
29711e8f1329SGolan Ben-Ami 
29721e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
29731e8f1329SGolan Ben-Ami 		return;
29741e8f1329SGolan Ben-Ami 
29751e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
29761e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
29771e8f1329SGolan Ben-Ami 		return;
29781e8f1329SGolan Ben-Ami 
29797174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
29801e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
29811e8f1329SGolan Ben-Ami 				peer_addr, action);
29821e8f1329SGolan Ben-Ami }
29831e8f1329SGolan Ben-Ami 
29844f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
29854f58121dSIlan Peer 	bool tolerated;
29864f58121dSIlan Peer };
29874f58121dSIlan Peer 
29884f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
29894f58121dSIlan Peer 						    struct cfg80211_bss *bss,
29904f58121dSIlan Peer 						    void *_data)
29914f58121dSIlan Peer {
29924f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
29934f58121dSIlan Peer 	const struct element *elem;
29944f58121dSIlan Peer 
29954f58121dSIlan Peer 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, bss->ies->data,
29964f58121dSIlan Peer 				  bss->ies->len);
29974f58121dSIlan Peer 
29984f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
29994f58121dSIlan Peer 	    !(elem->data[10] &
30004f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
30014f58121dSIlan Peer 		data->tolerated = false;
30024f58121dSIlan Peer 	}
30034f58121dSIlan Peer }
30044f58121dSIlan Peer 
30054f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
30064f58121dSIlan Peer 					       struct ieee80211_vif *vif)
30074f58121dSIlan Peer {
30084f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
30094f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
30104f58121dSIlan Peer 		.tolerated = true,
30114f58121dSIlan Peer 	};
30124f58121dSIlan Peer 
30134f58121dSIlan Peer 	if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
30144f58121dSIlan Peer 		mvmvif->he_ru_2mhz_block = false;
30154f58121dSIlan Peer 		return;
30164f58121dSIlan Peer 	}
30174f58121dSIlan Peer 
30184f58121dSIlan Peer 	cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
30194f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
30204f58121dSIlan Peer 			  &iter_data);
30214f58121dSIlan Peer 
30224f58121dSIlan Peer 	/*
30234f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
30244f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
30254f58121dSIlan Peer 	 */
30264f58121dSIlan Peer 	mvmvif->he_ru_2mhz_block = !iter_data.tolerated;
30274f58121dSIlan Peer }
30284f58121dSIlan Peer 
3029f7d6ef33SJohannes Berg static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3030f7d6ef33SJohannes Berg 					       struct ieee80211_vif *vif)
3031f7d6ef33SJohannes Berg {
3032f7d6ef33SJohannes Berg 	struct ieee80211_supported_band *sband;
3033f7d6ef33SJohannes Berg 	const struct ieee80211_sta_he_cap *he_cap;
3034f7d6ef33SJohannes Berg 
3035f7d6ef33SJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION)
3036f7d6ef33SJohannes Berg 		return;
3037f7d6ef33SJohannes Berg 
3038f7d6ef33SJohannes Berg 	if (!mvm->cca_40mhz_workaround)
3039f7d6ef33SJohannes Berg 		return;
3040f7d6ef33SJohannes Berg 
3041f7d6ef33SJohannes Berg 	/* decrement and check that we reached zero */
3042f7d6ef33SJohannes Berg 	mvm->cca_40mhz_workaround--;
3043f7d6ef33SJohannes Berg 	if (mvm->cca_40mhz_workaround)
3044f7d6ef33SJohannes Berg 		return;
3045f7d6ef33SJohannes Berg 
3046f7d6ef33SJohannes Berg 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3047f7d6ef33SJohannes Berg 
3048f7d6ef33SJohannes Berg 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3049f7d6ef33SJohannes Berg 
3050f7d6ef33SJohannes Berg 	he_cap = ieee80211_get_he_iftype_cap(sband,
3051f7d6ef33SJohannes Berg 					     ieee80211_vif_type_p2p(vif));
3052f7d6ef33SJohannes Berg 
3053f7d6ef33SJohannes Berg 	if (he_cap) {
3054f7d6ef33SJohannes Berg 		/* we know that ours is writable */
3055f7d6ef33SJohannes Berg 		struct ieee80211_sta_he_cap *he = (void *)he_cap;
3056f7d6ef33SJohannes Berg 
3057f7d6ef33SJohannes Berg 		he->he_cap_elem.phy_cap_info[0] |=
3058f7d6ef33SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3059f7d6ef33SJohannes Berg 	}
3060f7d6ef33SJohannes Berg }
3061f7d6ef33SJohannes Berg 
3062e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3063e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3064e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3065e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3066e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3067e705c121SKalle Valo {
3068e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3069e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
30706ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3071e705c121SKalle Valo 	int ret;
3072e705c121SKalle Valo 
3073e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3074e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3075e705c121SKalle Valo 
3076e705c121SKalle Valo 	/* this would be a mac80211 bug ... but don't crash */
3077e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
3078fe56d05eSSara Sharon 		return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) ? 0 : -EINVAL;
3079e705c121SKalle Valo 
308024afba76SLiad Kaufman 	/*
308124afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
308224afba76SLiad Kaufman 	 *
308324afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
308424afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
308524afba76SLiad Kaufman 	 * the queues.
308624afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
308724afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
308824afba76SLiad Kaufman 	 * of.
308924afba76SLiad Kaufman 	 *
309024afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
309124afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
309224afba76SLiad Kaufman 	 */
309324afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3094c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
309524afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
309624afba76SLiad Kaufman 
309724afba76SLiad Kaufman 		/*
309824afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
309924afba76SLiad Kaufman 		 * worker will already remove it if it was on
310024afba76SLiad Kaufman 		 */
3101f7d6ef33SJohannes Berg 
3102f7d6ef33SJohannes Berg 		/*
3103f7d6ef33SJohannes Berg 		 * Additionally, reset the 40 MHz capability if we disconnected
3104f7d6ef33SJohannes Berg 		 * from the AP now.
3105f7d6ef33SJohannes Berg 		 */
3106f7d6ef33SJohannes Berg 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
310724afba76SLiad Kaufman 	}
310824afba76SLiad Kaufman 
3109e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
31106ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3111d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
31126ea29ce5SJohannes Berg 
3113e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3114e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
3115e705c121SKalle Valo 		/*
3116e705c121SKalle Valo 		 * Firmware bug - it'll crash if the beacon interval is less
3117e705c121SKalle Valo 		 * than 16. We can't avoid connecting at all, so refuse the
3118e705c121SKalle Valo 		 * station state change, this will cause mac80211 to abandon
3119e705c121SKalle Valo 		 * attempts to connect to this AP, and eventually wpa_s will
3120cdaba917SEmmanuel Grumbach 		 * blocklist the AP...
3121e705c121SKalle Valo 		 */
3122e705c121SKalle Valo 		if (vif->type == NL80211_IFTYPE_STATION &&
3123e705c121SKalle Valo 		    vif->bss_conf.beacon_int < 16) {
3124e705c121SKalle Valo 			IWL_ERR(mvm,
3125e705c121SKalle Valo 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3126e705c121SKalle Valo 				sta->addr, vif->bss_conf.beacon_int);
3127e705c121SKalle Valo 			ret = -EINVAL;
3128e705c121SKalle Valo 			goto out_unlock;
3129e705c121SKalle Valo 		}
3130e705c121SKalle Valo 
313197cc1694SAvraham Stern 		if (vif->type == NL80211_IFTYPE_STATION)
313297cc1694SAvraham Stern 			vif->bss_conf.he_support = sta->he_cap.has_he;
313397cc1694SAvraham Stern 
3134e705c121SKalle Valo 		if (sta->tdls &&
3135e705c121SKalle Valo 		    (vif->p2p ||
3136e705c121SKalle Valo 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
3137e705c121SKalle Valo 						IWL_MVM_TDLS_STA_COUNT ||
3138e705c121SKalle Valo 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3139e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3140e705c121SKalle Valo 			ret = -EBUSY;
3141e705c121SKalle Valo 			goto out_unlock;
3142e705c121SKalle Valo 		}
3143e705c121SKalle Valo 
3144e705c121SKalle Valo 		ret = iwl_mvm_add_sta(mvm, vif, sta);
31451e8f1329SGolan Ben-Ami 		if (sta->tdls && ret == 0) {
3146e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
31471e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
31481e8f1329SGolan Ben-Ami 						   NL80211_TDLS_SETUP);
31491e8f1329SGolan Ben-Ami 		}
3150438af969SSara Sharon 
3151438af969SSara Sharon 		sta->max_rc_amsdu_len = 1;
3152e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3153e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3154e705c121SKalle Valo 		/*
3155e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3156e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3157e705c121SKalle Valo 		 */
3158e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3159e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3160e705c121SKalle Valo 		ret = 0;
3161e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3162e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
31638be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
31649394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
31658be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count++;
31668be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
316702221a81SShaul Triebitz 			if (vif->bss_conf.he_support &&
316802221a81SShaul Triebitz 			    !iwlwifi_mod_params.disable_11ax)
316902221a81SShaul Triebitz 				iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
31709394662aSLiad Kaufman 		} else if (vif->type == NL80211_IFTYPE_STATION) {
31719394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
31724f58121dSIlan Peer 
31734f58121dSIlan Peer 			mvmvif->he_ru_2mhz_block = false;
31744f58121dSIlan Peer 			if (sta->he_cap.has_he)
31754f58121dSIlan Peer 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif);
31764f58121dSIlan Peer 
31779394662aSLiad Kaufman 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
31788be30c13SAyala Beker 		}
3179735a0045SGoodstein, Mordechay 
31803baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
31813baf7528SAvraham Stern 				     false);
3182735a0045SGoodstein, Mordechay 		ret = iwl_mvm_update_sta(mvm, vif, sta);
3183e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3184e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
318528916a16SEmmanuel Grumbach 		ret = 0;
3186e705c121SKalle Valo 
3187e705c121SKalle Valo 		/* we don't support TDLS during DCM */
3188e705c121SKalle Valo 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
3189e705c121SKalle Valo 			iwl_mvm_teardown_tdls_peers(mvm);
3190e705c121SKalle Valo 
31911e8f1329SGolan Ben-Ami 		if (sta->tdls)
31921e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
31931e8f1329SGolan Ben-Ami 						   NL80211_TDLS_ENABLE_LINK);
31941e8f1329SGolan Ben-Ami 
3195e705c121SKalle Valo 		/* enable beacon filtering */
3196e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
31976b7a5aeaSNaftali Goldstein 
319850f56044SIlan Peer 		/*
319950f56044SIlan Peer 		 * Now that the station is authorized, i.e., keys were already
320050f56044SIlan Peer 		 * installed, need to indicate to the FW that
320150f56044SIlan Peer 		 * multicast data frames can be forwarded to the driver
320250f56044SIlan Peer 		 */
320350f56044SIlan Peer 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
320450f56044SIlan Peer 
32053baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
32063baf7528SAvraham Stern 				     true);
3207e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3208e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
320950f56044SIlan Peer 		/* Multicast data frames are no longer allowed */
321050f56044SIlan Peer 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
321150f56044SIlan Peer 
3212e705c121SKalle Valo 		/* disable beacon filtering */
321369e508b4SIlan Peer 		ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
321469e508b4SIlan Peer 		WARN_ON(ret &&
321569e508b4SIlan Peer 			!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
321669e508b4SIlan Peer 				  &mvm->status));
3217e705c121SKalle Valo 		ret = 0;
3218e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3219e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
32208be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
32218be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
32228be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
32238be30c13SAyala Beker 		}
3224e705c121SKalle Valo 		ret = 0;
3225e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3226e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3227e705c121SKalle Valo 		ret = 0;
3228e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3229e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
3230e705c121SKalle Valo 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
32311e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3232e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
32331e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
32341e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
32351e8f1329SGolan Ben-Ami 		}
323634a880d8SLiad Kaufman 
323744135b7cSIlan Peer 		if (unlikely(ret &&
323844135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
323944135b7cSIlan Peer 				      &mvm->status)))
324044135b7cSIlan Peer 			ret = 0;
3241e705c121SKalle Valo 	} else {
3242e705c121SKalle Valo 		ret = -EIO;
3243e705c121SKalle Valo 	}
3244e705c121SKalle Valo  out_unlock:
3245e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3246e705c121SKalle Valo 
3247e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3248e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3249e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3250e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3251e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3252e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3253e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3254e705c121SKalle Valo 	}
3255e705c121SKalle Valo 
3256e705c121SKalle Valo 	return ret;
3257e705c121SKalle Valo }
3258e705c121SKalle Valo 
3259e705c121SKalle Valo static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3260e705c121SKalle Valo {
3261e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3262e705c121SKalle Valo 
3263e705c121SKalle Valo 	mvm->rts_threshold = value;
3264e705c121SKalle Valo 
3265e705c121SKalle Valo 	return 0;
3266e705c121SKalle Valo }
3267e705c121SKalle Valo 
3268e705c121SKalle Valo static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3269e705c121SKalle Valo 				  struct ieee80211_vif *vif,
3270e705c121SKalle Valo 				  struct ieee80211_sta *sta, u32 changed)
3271e705c121SKalle Valo {
3272e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3273dcfe3b10SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3274dcfe3b10SJohannes Berg 
3275dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
3276dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3277dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
3278dcfe3b10SJohannes Berg 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3279dcfe3b10SJohannes Berg 				     true);
3280e705c121SKalle Valo 
3281e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
3282e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
3283e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
3284e705c121SKalle Valo }
3285e705c121SKalle Valo 
3286e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3287e705c121SKalle Valo 			       struct ieee80211_vif *vif, u16 ac,
3288e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
3289e705c121SKalle Valo {
3290e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3291e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3292e705c121SKalle Valo 
3293e705c121SKalle Valo 	mvmvif->queue_params[ac] = *params;
3294e705c121SKalle Valo 
3295e705c121SKalle Valo 	/*
3296e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
3297e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
3298e705c121SKalle Valo 	 */
3299e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3300e705c121SKalle Valo 		int ret;
3301e705c121SKalle Valo 
3302e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
3303e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3304e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3305e705c121SKalle Valo 		return ret;
3306e705c121SKalle Valo 	}
3307e705c121SKalle Valo 	return 0;
3308e705c121SKalle Valo }
3309e705c121SKalle Valo 
3310e705c121SKalle Valo static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3311d4e36e55SIlan Peer 				       struct ieee80211_vif *vif,
331215fae341SJohannes Berg 				       struct ieee80211_prep_tx_info *info)
3313e705c121SKalle Valo {
3314e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
33157c70fee5SSara Sharon 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
33167c70fee5SSara Sharon 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
3317e705c121SKalle Valo 
331815fae341SJohannes Berg 	if (info->duration > duration)
331915fae341SJohannes Berg 		duration = info->duration;
3320d4e36e55SIlan Peer 
3321e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3322fe959c7bSEmmanuel Grumbach 	/* Try really hard to protect the session and hear a beacon
3323fe959c7bSEmmanuel Grumbach 	 * The new session protection command allows us to protect the
3324fe959c7bSEmmanuel Grumbach 	 * session for a much longer time since the firmware will internally
3325fe959c7bSEmmanuel Grumbach 	 * create two events: a 300TU one with a very high priority that
3326fe959c7bSEmmanuel Grumbach 	 * won't be fragmented which should be enough for 99% of the cases,
3327fe959c7bSEmmanuel Grumbach 	 * and another one (which we configure here to be 900TU long) which
3328fe959c7bSEmmanuel Grumbach 	 * will have a slightly lower priority, but more importantly, can be
3329fe959c7bSEmmanuel Grumbach 	 * fragmented so that it'll allow other activities to run.
3330fe959c7bSEmmanuel Grumbach 	 */
3331fe959c7bSEmmanuel Grumbach 	if (fw_has_capa(&mvm->fw->ucode_capa,
3332fe959c7bSEmmanuel Grumbach 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
3333fe959c7bSEmmanuel Grumbach 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
3334b5b878e3SEmmanuel Grumbach 						    min_duration, false);
3335fe959c7bSEmmanuel Grumbach 	else
3336fe959c7bSEmmanuel Grumbach 		iwl_mvm_protect_session(mvm, vif, duration,
3337fe959c7bSEmmanuel Grumbach 					min_duration, 500, false);
3338e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3339e705c121SKalle Valo }
3340e705c121SKalle Valo 
3341e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3342e705c121SKalle Valo 					struct ieee80211_vif *vif,
3343e705c121SKalle Valo 					struct cfg80211_sched_scan_request *req,
3344e705c121SKalle Valo 					struct ieee80211_scan_ies *ies)
3345e705c121SKalle Valo {
3346e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3347e705c121SKalle Valo 
3348e705c121SKalle Valo 	int ret;
3349e705c121SKalle Valo 
3350e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3351e705c121SKalle Valo 
3352e705c121SKalle Valo 	if (!vif->bss_conf.idle) {
3353e705c121SKalle Valo 		ret = -EBUSY;
3354e705c121SKalle Valo 		goto out;
3355e705c121SKalle Valo 	}
3356e705c121SKalle Valo 
3357e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
3358e705c121SKalle Valo 
3359e705c121SKalle Valo out:
3360e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3361e705c121SKalle Valo 	return ret;
3362e705c121SKalle Valo }
3363e705c121SKalle Valo 
3364e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3365e705c121SKalle Valo 				       struct ieee80211_vif *vif)
3366e705c121SKalle Valo {
3367e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3368e705c121SKalle Valo 	int ret;
3369e705c121SKalle Valo 
3370e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3371e705c121SKalle Valo 
3372e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3373e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
3374e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
3375e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
3376e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3377e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
3378e705c121SKalle Valo 	 * not running.
3379e705c121SKalle Valo 	*/
3380e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
3381e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3382e705c121SKalle Valo 		return 0;
3383e705c121SKalle Valo 	}
3384e705c121SKalle Valo 
3385e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
3386e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3387e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
3388e705c121SKalle Valo 
3389e705c121SKalle Valo 	return ret;
3390e705c121SKalle Valo }
3391e705c121SKalle Valo 
33926569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3393e705c121SKalle Valo 				 enum set_key_cmd cmd,
3394e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3395e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3396e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
3397e705c121SKalle Valo {
3398c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3399e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3400f5e28eacSJohannes Berg 	struct iwl_mvm_sta *mvmsta;
3401f5e28eacSJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn;
3402f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
3403c56e00a3SJohannes Berg 	int ret, i;
34044615fd15SEmmanuel Grumbach 	u8 key_offset;
3405e705c121SKalle Valo 
3406e705c121SKalle Valo 	switch (key->cipher) {
3407e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3408286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
3409e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
34101ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
34117f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
34127f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
34137f768ad5SDavid Spinadel 		} else {
34147f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
34157f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
34167f768ad5SDavid Spinadel 		}
3417e705c121SKalle Valo 		break;
3418e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
34192a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
34202a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
342185aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
3422e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3423e705c121SKalle Valo 		break;
3424e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
34258e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
34268e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3427e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
3428e705c121SKalle Valo 		break;
3429e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3430e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3431475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
3432e705c121SKalle Valo 			break;
3433475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
3434475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
3435475c6bdeSJohannes Berg 		/* support HW crypto on TX */
3436475c6bdeSJohannes Berg 		return 0;
3437e705c121SKalle Valo 	default:
3438e705c121SKalle Valo 		/* currently FW supports only one optional cipher scheme */
3439e705c121SKalle Valo 		if (hw->n_cipher_schemes &&
3440e705c121SKalle Valo 		    hw->cipher_schemes->cipher == key->cipher)
3441e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3442e705c121SKalle Valo 		else
3443e705c121SKalle Valo 			return -EOPNOTSUPP;
3444e705c121SKalle Valo 	}
3445e705c121SKalle Valo 
3446e705c121SKalle Valo 	switch (cmd) {
3447e705c121SKalle Valo 	case SET_KEY:
3448b1fdc250SJohannes Berg 		if (keyidx == 6 || keyidx == 7)
3449b1fdc250SJohannes Berg 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
3450b1fdc250SJohannes Berg 					   key);
3451b1fdc250SJohannes Berg 
3452e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
3453e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
3454e705c121SKalle Valo 			/*
3455e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
3456e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
3457e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
34588e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
3459e705c121SKalle Valo 			 */
34608e160ab8SAyala Beker 			if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
34618e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3462a1c2ff30SAndrei Otcheretianski 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
346381279c49SJohannes Berg 				ret = -EOPNOTSUPP;
3464a1c2ff30SAndrei Otcheretianski 				break;
3465a1c2ff30SAndrei Otcheretianski 			}
346685aeb58cSDavid Spinadel 
346785aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
346885aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
346985aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
3470e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
3471a1c2ff30SAndrei Otcheretianski 				ret = 0;
3472e705c121SKalle Valo 				break;
3473e705c121SKalle Valo 			}
3474c56e00a3SJohannes Berg 
3475c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
3476c56e00a3SJohannes Berg 				for (i = 0;
3477c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
3478c56e00a3SJohannes Berg 				     i++) {
3479c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
3480c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
3481c56e00a3SJohannes Berg 						break;
3482c56e00a3SJohannes Berg 					}
3483c56e00a3SJohannes Berg 				}
3484c56e00a3SJohannes Berg 
3485c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
3486c56e00a3SJohannes Berg 					ret = -ENOSPC;
3487a1c2ff30SAndrei Otcheretianski 				else
3488a1c2ff30SAndrei Otcheretianski 					ret = 0;
3489c56e00a3SJohannes Berg 
3490c56e00a3SJohannes Berg 				break;
3491c56e00a3SJohannes Berg 			}
349285aeb58cSDavid Spinadel 		}
3493e705c121SKalle Valo 
3494e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
3495e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
3496e705c121SKalle Valo 		 */
3497e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3498e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
3499e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
3500e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
3501e705c121SKalle Valo 			ret = 0;
3502e705c121SKalle Valo 			break;
3503e705c121SKalle Valo 		}
3504e705c121SKalle Valo 
3505f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3506f5e28eacSJohannes Berg 		    sta && iwl_mvm_has_new_rx_api(mvm) &&
3507f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3508f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
35092a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
35102a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3511f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
3512f5e28eacSJohannes Berg 			int tid, q;
3513f5e28eacSJohannes Berg 
3514f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3515f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3516acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
3517acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
3518f5e28eacSJohannes Berg 					 GFP_KERNEL);
3519f5e28eacSJohannes Berg 			if (!ptk_pn) {
3520f5e28eacSJohannes Berg 				ret = -ENOMEM;
3521f5e28eacSJohannes Berg 				break;
3522f5e28eacSJohannes Berg 			}
3523f5e28eacSJohannes Berg 
3524f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3525f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
3526f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
3527f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
3528f5e28eacSJohannes Berg 					       seq.ccmp.pn,
3529f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
3530f5e28eacSJohannes Berg 			}
3531f5e28eacSJohannes Berg 
3532f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3533f5e28eacSJohannes Berg 		}
3534f5e28eacSJohannes Berg 
35354615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
35364615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
35374615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
35384615fd15SEmmanuel Grumbach 		else
35394615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
35404615fd15SEmmanuel Grumbach 
3541e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
35424615fd15SEmmanuel Grumbach 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
3543e705c121SKalle Valo 		if (ret) {
3544e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
3545475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
3546e705c121SKalle Valo 			/*
3547e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
3548475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
3549475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
3550475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
3551e705c121SKalle Valo 			 */
3552475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
3553475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
3554475c6bdeSJohannes Berg 			else
3555e705c121SKalle Valo 				ret = 0;
3556e705c121SKalle Valo 		}
3557e705c121SKalle Valo 
3558e705c121SKalle Valo 		break;
3559e705c121SKalle Valo 	case DISABLE_KEY:
3560b1fdc250SJohannes Berg 		if (keyidx == 6 || keyidx == 7)
3561b1fdc250SJohannes Berg 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
3562b1fdc250SJohannes Berg 					 NULL);
3563b1fdc250SJohannes Berg 
3564c56e00a3SJohannes Berg 		ret = -ENOENT;
3565c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3566c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
3567c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
3568c56e00a3SJohannes Berg 				ret = 0;
3569c56e00a3SJohannes Berg 			}
3570c56e00a3SJohannes Berg 		}
3571c56e00a3SJohannes Berg 
3572c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
3573c56e00a3SJohannes Berg 		if (ret == 0)
3574c56e00a3SJohannes Berg 			break;
3575c56e00a3SJohannes Berg 
3576e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3577e705c121SKalle Valo 			ret = 0;
3578e705c121SKalle Valo 			break;
3579e705c121SKalle Valo 		}
3580e705c121SKalle Valo 
3581f5e28eacSJohannes Berg 		if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3582f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3583f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
35842a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
35852a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3586f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3587f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
3588f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
3589f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
3590f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3591f5e28eacSJohannes Berg 			if (ptk_pn)
3592f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
3593f5e28eacSJohannes Berg 		}
3594f5e28eacSJohannes Berg 
3595e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3596e705c121SKalle Valo 		ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3597e705c121SKalle Valo 		break;
3598e705c121SKalle Valo 	default:
3599e705c121SKalle Valo 		ret = -EINVAL;
3600e705c121SKalle Valo 	}
3601e705c121SKalle Valo 
36026569e7d3SJohannes Berg 	return ret;
36036569e7d3SJohannes Berg }
36046569e7d3SJohannes Berg 
36056569e7d3SJohannes Berg static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
36066569e7d3SJohannes Berg 			       enum set_key_cmd cmd,
36076569e7d3SJohannes Berg 			       struct ieee80211_vif *vif,
36086569e7d3SJohannes Berg 			       struct ieee80211_sta *sta,
36096569e7d3SJohannes Berg 			       struct ieee80211_key_conf *key)
36106569e7d3SJohannes Berg {
36116569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
36126569e7d3SJohannes Berg 	int ret;
36136569e7d3SJohannes Berg 
36146569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
36156569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
3616e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
36176569e7d3SJohannes Berg 
3618e705c121SKalle Valo 	return ret;
3619e705c121SKalle Valo }
3620e705c121SKalle Valo 
3621e705c121SKalle Valo static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3622e705c121SKalle Valo 					struct ieee80211_vif *vif,
3623e705c121SKalle Valo 					struct ieee80211_key_conf *keyconf,
3624e705c121SKalle Valo 					struct ieee80211_sta *sta,
3625e705c121SKalle Valo 					u32 iv32, u16 *phase1key)
3626e705c121SKalle Valo {
3627e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3628e705c121SKalle Valo 
3629e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3630e705c121SKalle Valo 		return;
3631e705c121SKalle Valo 
3632e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3633e705c121SKalle Valo }
3634e705c121SKalle Valo 
3635e705c121SKalle Valo 
3636e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3637e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
3638e705c121SKalle Valo {
3639e705c121SKalle Valo 	struct iwl_mvm *mvm =
3640e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3641e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
3642e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
3643e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
3644e705c121SKalle Valo 
3645e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3646e705c121SKalle Valo 		return true;
3647e705c121SKalle Valo 
3648e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3649e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3650e705c121SKalle Valo 		return true;
3651e705c121SKalle Valo 	}
3652e705c121SKalle Valo 
3653e705c121SKalle Valo 	resp = (void *)pkt->data;
3654e705c121SKalle Valo 
3655e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
3656b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
3657e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
3658e705c121SKalle Valo 
3659e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
3660e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3661e705c121SKalle Valo 		     te_data->uid);
3662e705c121SKalle Valo 
3663e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3664e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3665e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3666e705c121SKalle Valo 
3667e705c121SKalle Valo 	return true;
3668e705c121SKalle Valo }
3669e705c121SKalle Valo 
3670dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3671dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3672dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3673dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3674dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3675e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3676e705c121SKalle Valo 				    struct ieee80211_channel *channel,
3677e705c121SKalle Valo 				    struct ieee80211_vif *vif,
3678e705c121SKalle Valo 				    int duration)
3679e705c121SKalle Valo {
3680afc1e3b4SAvraham Stern 	int res;
3681e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3682e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3683e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
3684e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
3685dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
3686dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
3687dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
3688e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
3689e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3690e705c121SKalle Valo 		.id_and_color =
3691e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3692e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3693e705c121SKalle Valo 	};
369457e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
369557e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
369657e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
369757e861d9SDavid Spinadel 
369857e861d9SDavid Spinadel 	/* Set the channel info data */
369957e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
37003717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
370157e861d9SDavid Spinadel 			      PHY_VHT_CHANNEL_MODE20,
370257e861d9SDavid Spinadel 			      0);
370357e861d9SDavid Spinadel 
370457e861d9SDavid Spinadel 	/* Set the time and duration */
3705afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
3706e705c121SKalle Valo 
3707dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
3708dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
3709dc28e12fSMatti Gottlieb 
3710dc28e12fSMatti Gottlieb 	/*
3711dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
3712dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
3713dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
3714dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
3715dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
3716dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
3717dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
3718dc28e12fSMatti Gottlieb 	 */
3719dc28e12fSMatti Gottlieb 	if (vif->bss_conf.assoc) {
3720dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3721dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
3722dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
3723dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3724dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
3725dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
3726dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
3727dc28e12fSMatti Gottlieb 		}
3728dc28e12fSMatti Gottlieb 	}
3729dc28e12fSMatti Gottlieb 
373057e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
373157e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
3732dc28e12fSMatti Gottlieb 
3733dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
3734903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
3735903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
3736903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
3737903b3f9bSEmmanuel Grumbach 		     "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3738903b3f9bSEmmanuel Grumbach 		     duration, delay, dtim_interval);
3739903b3f9bSEmmanuel Grumbach 
3740e705c121SKalle Valo 	/* Set the node address */
374157e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
3742e705c121SKalle Valo 
3743e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3744e705c121SKalle Valo 
3745e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3746e705c121SKalle Valo 
3747e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3748e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3749e705c121SKalle Valo 		return -EIO;
3750e705c121SKalle Valo 	}
3751e705c121SKalle Valo 
3752e705c121SKalle Valo 	te_data->vif = vif;
3753e705c121SKalle Valo 	te_data->duration = duration;
3754e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
3755e705c121SKalle Valo 
3756e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3757e705c121SKalle Valo 
3758e705c121SKalle Valo 	/*
3759e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
3760e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
3761e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
3762e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
3763e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
3764e705c121SKalle Valo 	 * time another notification (that the time event started)
3765e705c121SKalle Valo 	 * might already be processed unsuccessfully.
3766e705c121SKalle Valo 	 */
3767e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3768e705c121SKalle Valo 				   time_event_response,
3769e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
3770e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
3771e705c121SKalle Valo 
377257e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
3773e705c121SKalle Valo 				   &aux_roc_req);
3774e705c121SKalle Valo 
3775e705c121SKalle Valo 	if (res) {
3776e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3777e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3778e705c121SKalle Valo 		goto out_clear_te;
3779e705c121SKalle Valo 	}
3780e705c121SKalle Valo 
3781e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
3782e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3783e705c121SKalle Valo 	/* should never fail */
3784e705c121SKalle Valo 	WARN_ON_ONCE(res);
3785e705c121SKalle Valo 
3786e705c121SKalle Valo 	if (res) {
3787e705c121SKalle Valo  out_clear_te:
3788e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
3789e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
3790e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3791e705c121SKalle Valo 	}
3792e705c121SKalle Valo 
3793e705c121SKalle Valo 	return res;
3794e705c121SKalle Valo }
3795e705c121SKalle Valo 
3796e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
3797e705c121SKalle Valo 		       struct ieee80211_vif *vif,
3798e705c121SKalle Valo 		       struct ieee80211_channel *channel,
3799e705c121SKalle Valo 		       int duration,
3800e705c121SKalle Valo 		       enum ieee80211_roc_type type)
3801e705c121SKalle Valo {
3802e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3803e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3804e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
3805e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
38068835a64fSJohannes Berg 	bool band_change_removal;
3807e705c121SKalle Valo 	int ret, i;
3808e705c121SKalle Valo 
3809e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3810e705c121SKalle Valo 			   duration, type);
3811e705c121SKalle Valo 
38129834781cSJohannes Berg 	/*
38139834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
38149834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
38159834781cSJohannes Berg 	 */
3816e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
3817e705c121SKalle Valo 
3818e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3819e705c121SKalle Valo 
3820e705c121SKalle Valo 	switch (vif->type) {
3821e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3822e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
3823e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
3824e705c121SKalle Valo 			/* Use aux roc framework (HS20) */
38252c2c3647SNathan Errera 			if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
38262c2c3647SNathan Errera 						  ADD_STA, 0) >= 12) {
38272c2c3647SNathan Errera 				u32 lmac_id;
38282c2c3647SNathan Errera 
38292c2c3647SNathan Errera 				lmac_id = iwl_mvm_get_lmac_id(mvm->fw,
38302c2c3647SNathan Errera 							      channel->band);
38312c2c3647SNathan Errera 				ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
38322c2c3647SNathan Errera 				if (WARN(ret,
38332c2c3647SNathan Errera 					 "Failed to allocate aux station"))
38342c2c3647SNathan Errera 					goto out_unlock;
38352c2c3647SNathan Errera 			}
3836e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3837e705c121SKalle Valo 						       vif, duration);
3838e705c121SKalle Valo 			goto out_unlock;
3839e705c121SKalle Valo 		}
3840e705c121SKalle Valo 		IWL_ERR(mvm, "hotspot not supported\n");
3841e705c121SKalle Valo 		ret = -EINVAL;
3842e705c121SKalle Valo 		goto out_unlock;
3843e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
3844e705c121SKalle Valo 		/* handle below */
3845e705c121SKalle Valo 		break;
3846e705c121SKalle Valo 	default:
3847e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
3848e705c121SKalle Valo 		ret = -EINVAL;
3849e705c121SKalle Valo 		goto out_unlock;
3850e705c121SKalle Valo 	}
3851e705c121SKalle Valo 
3852e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
3853e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
3854e705c121SKalle Valo 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3855e705c121SKalle Valo 			continue;
3856e705c121SKalle Valo 
3857e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3858e705c121SKalle Valo 			/*
3859e705c121SKalle Valo 			 * Unbind the P2P_DEVICE from the current PHY context,
3860e705c121SKalle Valo 			 * and if the PHY context is not used remove it.
3861e705c121SKalle Valo 			 */
3862e705c121SKalle Valo 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
3863e705c121SKalle Valo 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3864e705c121SKalle Valo 				goto out_unlock;
3865e705c121SKalle Valo 
3866e705c121SKalle Valo 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3867e705c121SKalle Valo 
3868e705c121SKalle Valo 			/* Bind the P2P_DEVICE to the current PHY Context */
3869e705c121SKalle Valo 			mvmvif->phy_ctxt = phy_ctxt;
3870e705c121SKalle Valo 
3871e705c121SKalle Valo 			ret = iwl_mvm_binding_add_vif(mvm, vif);
3872e705c121SKalle Valo 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3873e705c121SKalle Valo 				goto out_unlock;
3874e705c121SKalle Valo 
3875e705c121SKalle Valo 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3876e705c121SKalle Valo 			goto schedule_time_event;
3877e705c121SKalle Valo 		}
3878e705c121SKalle Valo 	}
3879e705c121SKalle Valo 
3880e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
3881e705c121SKalle Valo 	if (channel == mvmvif->phy_ctxt->channel)
3882e705c121SKalle Valo 		goto schedule_time_event;
3883e705c121SKalle Valo 
3884e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
3885e705c121SKalle Valo 
3886e705c121SKalle Valo 	/*
38878835a64fSJohannes Berg 	 * Check if the remain-on-channel is on a different band and that
38888835a64fSJohannes Berg 	 * requires context removal, see iwl_mvm_phy_ctxt_changed(). If
38898835a64fSJohannes Berg 	 * so, we'll need to release and then re-configure here, since we
38908835a64fSJohannes Berg 	 * must not remove a PHY context that's part of a binding.
3891e705c121SKalle Valo 	 */
38928835a64fSJohannes Berg 	band_change_removal =
38938835a64fSJohannes Berg 		fw_has_capa(&mvm->fw->ucode_capa,
38948835a64fSJohannes Berg 			    IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
38958835a64fSJohannes Berg 		mvmvif->phy_ctxt->channel->band != chandef.chan->band;
38968835a64fSJohannes Berg 
38978835a64fSJohannes Berg 	if (mvmvif->phy_ctxt->ref == 1 && !band_change_removal) {
38988835a64fSJohannes Berg 		/*
38998835a64fSJohannes Berg 		 * Change the PHY context configuration as it is currently
39008835a64fSJohannes Berg 		 * referenced only by the P2P Device MAC (and we can modify it)
39018835a64fSJohannes Berg 		 */
3902e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3903e705c121SKalle Valo 					       &chandef, 1, 1);
3904e705c121SKalle Valo 		if (ret)
3905e705c121SKalle Valo 			goto out_unlock;
3906e705c121SKalle Valo 	} else {
3907e705c121SKalle Valo 		/*
39088835a64fSJohannes Berg 		 * The PHY context is shared with other MACs (or we're trying to
39098835a64fSJohannes Berg 		 * switch bands), so remove the P2P Device from the binding,
39108835a64fSJohannes Berg 		 * allocate an new PHY context and create a new binding.
3911e705c121SKalle Valo 		 */
3912e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3913e705c121SKalle Valo 		if (!phy_ctxt) {
3914e705c121SKalle Valo 			ret = -ENOSPC;
3915e705c121SKalle Valo 			goto out_unlock;
3916e705c121SKalle Valo 		}
3917e705c121SKalle Valo 
3918e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3919e705c121SKalle Valo 					       1, 1);
3920e705c121SKalle Valo 		if (ret) {
3921e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
3922e705c121SKalle Valo 			goto out_unlock;
3923e705c121SKalle Valo 		}
3924e705c121SKalle Valo 
3925e705c121SKalle Valo 		/* Unbind the P2P_DEVICE from the current PHY context */
3926e705c121SKalle Valo 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
3927e705c121SKalle Valo 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3928e705c121SKalle Valo 			goto out_unlock;
3929e705c121SKalle Valo 
3930e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3931e705c121SKalle Valo 
3932e705c121SKalle Valo 		/* Bind the P2P_DEVICE to the new allocated PHY context */
3933e705c121SKalle Valo 		mvmvif->phy_ctxt = phy_ctxt;
3934e705c121SKalle Valo 
3935e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
3936e705c121SKalle Valo 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3937e705c121SKalle Valo 			goto out_unlock;
3938e705c121SKalle Valo 
3939e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3940e705c121SKalle Valo 	}
3941e705c121SKalle Valo 
3942e705c121SKalle Valo schedule_time_event:
3943e705c121SKalle Valo 	/* Schedule the time events */
3944e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
3945e705c121SKalle Valo 
3946e705c121SKalle Valo out_unlock:
3947e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3948e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3949e705c121SKalle Valo 	return ret;
3950e705c121SKalle Valo }
3951e705c121SKalle Valo 
39525db4c4b9SEmmanuel Grumbach static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
39535db4c4b9SEmmanuel Grumbach 			      struct ieee80211_vif *vif)
3954e705c121SKalle Valo {
3955e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3956e705c121SKalle Valo 
3957e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
3958e705c121SKalle Valo 
3959e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3960fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
3961e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3962e705c121SKalle Valo 
3963e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3964e705c121SKalle Valo 	return 0;
3965e705c121SKalle Valo }
3966e705c121SKalle Valo 
3967b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
3968b73f9a4aSJohannes Berg 	bool responder;
3969b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
3970b73f9a4aSJohannes Berg };
3971b73f9a4aSJohannes Berg 
3972b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
3973b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
3974b73f9a4aSJohannes Berg {
3975b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
3976b73f9a4aSJohannes Berg 
3977b73f9a4aSJohannes Berg 	if (rcu_access_pointer(vif->chanctx_conf) == data->ctx &&
3978b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
3979b73f9a4aSJohannes Berg 		data->responder = true;
3980b73f9a4aSJohannes Berg }
3981b73f9a4aSJohannes Berg 
3982b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
3983b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
3984b73f9a4aSJohannes Berg {
3985b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
3986b73f9a4aSJohannes Berg 		.responder = false,
3987b73f9a4aSJohannes Berg 		.ctx = ctx,
3988b73f9a4aSJohannes Berg 	};
3989b73f9a4aSJohannes Berg 
3990b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
3991b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
3992b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
3993b73f9a4aSJohannes Berg 					&data);
3994b73f9a4aSJohannes Berg 	return data.responder;
3995b73f9a4aSJohannes Berg }
3996b73f9a4aSJohannes Berg 
3997e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
3998e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
3999e705c121SKalle Valo {
4000e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4001e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4002b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4003b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4004e705c121SKalle Valo 	int ret;
4005e705c121SKalle Valo 
4006e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4007e705c121SKalle Valo 
4008e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4009e705c121SKalle Valo 
4010e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4011e705c121SKalle Valo 	if (!phy_ctxt) {
4012e705c121SKalle Valo 		ret = -ENOSPC;
4013e705c121SKalle Valo 		goto out;
4014e705c121SKalle Valo 	}
4015e705c121SKalle Valo 
4016b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4017e705c121SKalle Valo 				       ctx->rx_chains_static,
4018e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
4019e705c121SKalle Valo 	if (ret) {
4020e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4021e705c121SKalle Valo 		goto out;
4022e705c121SKalle Valo 	}
4023e705c121SKalle Valo 
4024e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4025e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4026e705c121SKalle Valo out:
4027e705c121SKalle Valo 	return ret;
4028e705c121SKalle Valo }
4029e705c121SKalle Valo 
4030e705c121SKalle Valo static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4031e705c121SKalle Valo 			       struct ieee80211_chanctx_conf *ctx)
4032e705c121SKalle Valo {
4033e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4034e705c121SKalle Valo 	int ret;
4035e705c121SKalle Valo 
4036e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4037e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4038e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4039e705c121SKalle Valo 
4040e705c121SKalle Valo 	return ret;
4041e705c121SKalle Valo }
4042e705c121SKalle Valo 
4043e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4044e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4045e705c121SKalle Valo {
4046e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4047e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4048e705c121SKalle Valo 
4049e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4050e705c121SKalle Valo 
4051e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4052e705c121SKalle Valo }
4053e705c121SKalle Valo 
4054e705c121SKalle Valo static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4055e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx)
4056e705c121SKalle Valo {
4057e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4058e705c121SKalle Valo 
4059e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4060e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4061e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4062e705c121SKalle Valo }
4063e705c121SKalle Valo 
4064e705c121SKalle Valo static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4065e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx,
4066e705c121SKalle Valo 				   u32 changed)
4067e705c121SKalle Valo {
4068e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4069e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4070e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4071b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4072b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4073e705c121SKalle Valo 
4074e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4075e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4076e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4077e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4078e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4079e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4080e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4081e705c121SKalle Valo 		return;
4082e705c121SKalle Valo 
4083e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
40847a20bcceSEmmanuel Grumbach 
40857a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
40867a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4087b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
40887a20bcceSEmmanuel Grumbach 			goto out_unlock;
40897a20bcceSEmmanuel Grumbach 
40907a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
40917a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4092b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
40937a20bcceSEmmanuel Grumbach 			goto out_unlock;
40947a20bcceSEmmanuel Grumbach 	}
40957a20bcceSEmmanuel Grumbach 
4096e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4097b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4098e705c121SKalle Valo 				 ctx->rx_chains_static,
4099e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
41007a20bcceSEmmanuel Grumbach 
41017a20bcceSEmmanuel Grumbach out_unlock:
4102e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4103e705c121SKalle Valo }
4104e705c121SKalle Valo 
4105e705c121SKalle Valo static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4106e705c121SKalle Valo 					struct ieee80211_vif *vif,
4107e705c121SKalle Valo 					struct ieee80211_chanctx_conf *ctx,
4108e705c121SKalle Valo 					bool switching_chanctx)
4109e705c121SKalle Valo {
4110e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4111e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4112e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4113e705c121SKalle Valo 	int ret;
4114e705c121SKalle Valo 
4115e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4116e705c121SKalle Valo 
4117e705c121SKalle Valo 	mvmvif->phy_ctxt = phy_ctxt;
4118e705c121SKalle Valo 
4119e705c121SKalle Valo 	switch (vif->type) {
4120e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4121e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4122e705c121SKalle Valo 		if (switching_chanctx) {
4123e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4124e705c121SKalle Valo 			break;
4125e705c121SKalle Valo 		}
41265a2abdcaSGustavo A. R. Silva 		fallthrough;
4127e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4128e705c121SKalle Valo 		/*
4129e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4130e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4131e705c121SKalle Valo 		 */
4132e705c121SKalle Valo 		ret = 0;
4133e705c121SKalle Valo 		goto out;
4134e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
413519125cb0SAndrei Otcheretianski 		mvmvif->csa_bcn_pending = false;
4136e705c121SKalle Valo 		break;
4137e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4138e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4139e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4140e705c121SKalle Valo 		break;
4141e705c121SKalle Valo 	default:
4142e705c121SKalle Valo 		ret = -EINVAL;
4143e705c121SKalle Valo 		goto out;
4144e705c121SKalle Valo 	}
4145e705c121SKalle Valo 
4146e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4147e705c121SKalle Valo 	if (ret)
4148e705c121SKalle Valo 		goto out;
4149e705c121SKalle Valo 
4150e705c121SKalle Valo 	/*
4151e705c121SKalle Valo 	 * Power state must be updated before quotas,
4152e705c121SKalle Valo 	 * otherwise fw will complain.
4153e705c121SKalle Valo 	 */
4154e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4155e705c121SKalle Valo 
4156e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4157e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4158e705c121SKalle Valo 	 * will handle quota settings.
4159e705c121SKalle Valo 	 */
4160e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4161e705c121SKalle Valo 		mvmvif->monitor_active = true;
4162e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4163e705c121SKalle Valo 		if (ret)
4164e705c121SKalle Valo 			goto out_remove_binding;
41650e39eb03SChaya Rachel Ivgi 
41660e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
41670e39eb03SChaya Rachel Ivgi 		if (ret)
41680e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
41690e39eb03SChaya Rachel Ivgi 
4170e705c121SKalle Valo 	}
4171e705c121SKalle Valo 
4172e705c121SKalle Valo 	/* Handle binding during CSA */
4173e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4174e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4175e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4176e705c121SKalle Valo 	}
4177e705c121SKalle Valo 
4178e705c121SKalle Valo 	if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
417974a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
418074a10252SSara Sharon 
418174a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
418274a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
418319125cb0SAndrei Otcheretianski 			u32 duration = 3 * vif->bss_conf.beacon_int;
4184e705c121SKalle Valo 
4185e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4186e705c121SKalle Valo 			 * beacon on the new channel.
4187e705c121SKalle Valo 			 */
4188e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4189e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4190e705c121SKalle Valo 						true);
419174a10252SSara Sharon 		}
4192e705c121SKalle Valo 
4193e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4194e705c121SKalle Valo 	}
4195e705c121SKalle Valo 
4196e705c121SKalle Valo 	goto out;
4197e705c121SKalle Valo 
4198e705c121SKalle Valo out_remove_binding:
4199e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4200e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4201e705c121SKalle Valo out:
4202e705c121SKalle Valo 	if (ret)
4203e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
4204e705c121SKalle Valo 	return ret;
4205e705c121SKalle Valo }
4206e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4207e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4208e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4209e705c121SKalle Valo {
4210e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4211e705c121SKalle Valo 	int ret;
4212e705c121SKalle Valo 
4213e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4214e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
4215e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4216e705c121SKalle Valo 
4217e705c121SKalle Valo 	return ret;
4218e705c121SKalle Valo }
4219e705c121SKalle Valo 
4220e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4221e705c121SKalle Valo 					   struct ieee80211_vif *vif,
4222e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
4223e705c121SKalle Valo 					   bool switching_chanctx)
4224e705c121SKalle Valo {
4225e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4226e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
4227e705c121SKalle Valo 
4228e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4229e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4230e705c121SKalle Valo 
4231e705c121SKalle Valo 	switch (vif->type) {
4232e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4233e705c121SKalle Valo 		goto out;
4234e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4235e705c121SKalle Valo 		mvmvif->monitor_active = false;
4236e705c121SKalle Valo 		mvmvif->ps_disabled = false;
42370e39eb03SChaya Rachel Ivgi 		iwl_mvm_rm_snif_sta(mvm, vif);
4238e705c121SKalle Valo 		break;
4239e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4240e705c121SKalle Valo 		/* This part is triggered only during CSA */
4241e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
4242e705c121SKalle Valo 			goto out;
4243e705c121SKalle Valo 
4244e705c121SKalle Valo 		mvmvif->csa_countdown = false;
4245e705c121SKalle Valo 
4246e705c121SKalle Valo 		/* Set CS bit on all the stations */
4247e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4248e705c121SKalle Valo 
4249e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
4250e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4251e705c121SKalle Valo 
4252e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
4253e705c121SKalle Valo 		break;
4254e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4255e705c121SKalle Valo 		if (!switching_chanctx)
4256e705c121SKalle Valo 			break;
4257e705c121SKalle Valo 
4258e705c121SKalle Valo 		disabled_vif = vif;
4259e705c121SKalle Valo 
426074a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
426174a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4262e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
4263e705c121SKalle Valo 		break;
4264e705c121SKalle Valo 	default:
4265e705c121SKalle Valo 		break;
4266e705c121SKalle Valo 	}
4267e705c121SKalle Valo 
4268e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
4269e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4270e705c121SKalle Valo 
4271e705c121SKalle Valo out:
4272bf544e9aSSara Sharon 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
4273bf544e9aSSara Sharon 	    switching_chanctx)
4274bf544e9aSSara Sharon 		return;
4275e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
4276e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4277e705c121SKalle Valo }
4278e705c121SKalle Valo 
4279e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4280e705c121SKalle Valo 					 struct ieee80211_vif *vif,
4281e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
4282e705c121SKalle Valo {
4283e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4284e705c121SKalle Valo 
4285e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4286e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
4287e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4288e705c121SKalle Valo }
4289e705c121SKalle Valo 
4290e705c121SKalle Valo static int
4291e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4292e705c121SKalle Valo 				struct ieee80211_vif_chanctx_switch *vifs)
4293e705c121SKalle Valo {
4294e705c121SKalle Valo 	int ret;
4295e705c121SKalle Valo 
4296e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4297e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4298e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4299e705c121SKalle Valo 
4300e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4301e705c121SKalle Valo 	if (ret) {
4302e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4303e705c121SKalle Valo 		goto out_reassign;
4304e705c121SKalle Valo 	}
4305e705c121SKalle Valo 
4306e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4307e705c121SKalle Valo 					   true);
4308e705c121SKalle Valo 	if (ret) {
4309e705c121SKalle Valo 		IWL_ERR(mvm,
4310e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4311e705c121SKalle Valo 		goto out_remove;
4312e705c121SKalle Valo 	}
4313e705c121SKalle Valo 
4314e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
4315e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
4316e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
4317e705c121SKalle Valo 
4318e705c121SKalle Valo 	goto out;
4319e705c121SKalle Valo 
4320e705c121SKalle Valo out_remove:
4321e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4322e705c121SKalle Valo 
4323e705c121SKalle Valo out_reassign:
4324e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
4325e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4326e705c121SKalle Valo 		goto out_restart;
4327e705c121SKalle Valo 	}
4328e705c121SKalle Valo 
4329e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4330e705c121SKalle Valo 					 true)) {
4331e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4332e705c121SKalle Valo 		goto out_restart;
4333e705c121SKalle Valo 	}
4334e705c121SKalle Valo 
4335e705c121SKalle Valo 	goto out;
4336e705c121SKalle Valo 
4337e705c121SKalle Valo out_restart:
4338e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4339e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4340e705c121SKalle Valo 
4341e705c121SKalle Valo out:
4342e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4343e705c121SKalle Valo 
4344e705c121SKalle Valo 	return ret;
4345e705c121SKalle Valo }
4346e705c121SKalle Valo 
4347e705c121SKalle Valo static int
4348e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4349e705c121SKalle Valo 				    struct ieee80211_vif_chanctx_switch *vifs)
4350e705c121SKalle Valo {
4351e705c121SKalle Valo 	int ret;
4352e705c121SKalle Valo 
4353e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4354e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4355e705c121SKalle Valo 
4356e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4357e705c121SKalle Valo 					   true);
4358e705c121SKalle Valo 	if (ret) {
4359e705c121SKalle Valo 		IWL_ERR(mvm,
4360e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4361e705c121SKalle Valo 		goto out_reassign;
4362e705c121SKalle Valo 	}
4363e705c121SKalle Valo 
4364e705c121SKalle Valo 	goto out;
4365e705c121SKalle Valo 
4366e705c121SKalle Valo out_reassign:
4367e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4368e705c121SKalle Valo 					 true)) {
4369e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4370e705c121SKalle Valo 		goto out_restart;
4371e705c121SKalle Valo 	}
4372e705c121SKalle Valo 
4373e705c121SKalle Valo 	goto out;
4374e705c121SKalle Valo 
4375e705c121SKalle Valo out_restart:
4376e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4377e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4378e705c121SKalle Valo 
4379e705c121SKalle Valo out:
4380e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4381e705c121SKalle Valo 
4382e705c121SKalle Valo 	return ret;
4383e705c121SKalle Valo }
4384e705c121SKalle Valo 
4385e705c121SKalle Valo static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4386e705c121SKalle Valo 				      struct ieee80211_vif_chanctx_switch *vifs,
4387e705c121SKalle Valo 				      int n_vifs,
4388e705c121SKalle Valo 				      enum ieee80211_chanctx_switch_mode mode)
4389e705c121SKalle Valo {
4390e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4391e705c121SKalle Valo 	int ret;
4392e705c121SKalle Valo 
4393e705c121SKalle Valo 	/* we only support a single-vif right now */
4394e705c121SKalle Valo 	if (n_vifs > 1)
4395e705c121SKalle Valo 		return -EOPNOTSUPP;
4396e705c121SKalle Valo 
4397e705c121SKalle Valo 	switch (mode) {
4398e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
4399e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4400e705c121SKalle Valo 		break;
4401e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
4402e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
4403e705c121SKalle Valo 		break;
4404e705c121SKalle Valo 	default:
4405e705c121SKalle Valo 		ret = -EOPNOTSUPP;
4406e705c121SKalle Valo 		break;
4407e705c121SKalle Valo 	}
4408e705c121SKalle Valo 
4409e705c121SKalle Valo 	return ret;
4410e705c121SKalle Valo }
4411e705c121SKalle Valo 
44122f0282dbSJohannes Berg static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
44132f0282dbSJohannes Berg {
44142f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
44152f0282dbSJohannes Berg 
44162f0282dbSJohannes Berg 	return mvm->ibss_manager;
44172f0282dbSJohannes Berg }
44182f0282dbSJohannes Berg 
4419e705c121SKalle Valo static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4420e705c121SKalle Valo 			   struct ieee80211_sta *sta,
4421e705c121SKalle Valo 			   bool set)
4422e705c121SKalle Valo {
4423e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4424e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4425e705c121SKalle Valo 
4426e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
4427e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
4428e705c121SKalle Valo 		return -EINVAL;
4429e705c121SKalle Valo 	}
4430e705c121SKalle Valo 
4431e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4432e705c121SKalle Valo }
4433e705c121SKalle Valo 
4434e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
4435e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4436e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4437e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
4438e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
4439e705c121SKalle Valo };
4440e705c121SKalle Valo 
4441e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4442e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4443e705c121SKalle Valo 				      void *data, int len)
4444e705c121SKalle Valo {
4445e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4446e705c121SKalle Valo 	int err;
4447e705c121SKalle Valo 	u32 noa_duration;
4448e705c121SKalle Valo 
44498cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
44508cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
4451e705c121SKalle Valo 	if (err)
4452e705c121SKalle Valo 		return err;
4453e705c121SKalle Valo 
4454e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
4455e705c121SKalle Valo 		return -EINVAL;
4456e705c121SKalle Valo 
4457e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4458e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
4459e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4460e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
4461e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4462e705c121SKalle Valo 			return -EINVAL;
4463e705c121SKalle Valo 
4464e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4465e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
4466e705c121SKalle Valo 			return -EINVAL;
4467e705c121SKalle Valo 
4468e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
4469e705c121SKalle Valo 		mvm->noa_vif = vif;
4470e705c121SKalle Valo 
447122b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
4472e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4473e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
4474e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4475e705c121SKalle Valo 		    !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
4476e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4477e705c121SKalle Valo 			return -EINVAL;
4478e705c121SKalle Valo 
4479e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
4480e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4481e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4482e705c121SKalle Valo 	}
4483e705c121SKalle Valo 
4484e705c121SKalle Valo 	return -EOPNOTSUPP;
4485e705c121SKalle Valo }
4486e705c121SKalle Valo 
4487e705c121SKalle Valo static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4488e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4489e705c121SKalle Valo 				    void *data, int len)
4490e705c121SKalle Valo {
4491e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4492e705c121SKalle Valo 	int err;
4493e705c121SKalle Valo 
4494e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4495e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4496e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4497e705c121SKalle Valo 
4498e705c121SKalle Valo 	return err;
4499e705c121SKalle Valo }
4500e705c121SKalle Valo #endif
4501e705c121SKalle Valo 
4502e705c121SKalle Valo static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4503e705c121SKalle Valo 				   struct ieee80211_vif *vif,
4504e705c121SKalle Valo 				   struct ieee80211_channel_switch *chsw)
4505e705c121SKalle Valo {
4506e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
4507e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
4508e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
4509e705c121SKalle Valo 	 * (which is when the absence time event starts).
4510e705c121SKalle Valo 	 */
4511e705c121SKalle Valo 
4512e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4513e705c121SKalle Valo 			   "dummy channel switch op\n");
4514e705c121SKalle Valo }
4515e705c121SKalle Valo 
451674a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
451774a10252SSara Sharon 				       struct ieee80211_vif *vif,
451874a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
451974a10252SSara Sharon {
452074a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
452174a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
452274a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
452374a10252SSara Sharon 							  mvmvif->color)),
452474a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
452574a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
452674a10252SSara Sharon 		.cs_count = chsw->count,
452777738865SSara Sharon 		.cs_mode = chsw->block_tx,
452874a10252SSara Sharon 	};
452974a10252SSara Sharon 
453074a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
453174a10252SSara Sharon 
45329cfcf71cSSara Sharon 	if (chsw->delay)
45339cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
45349cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
45359cfcf71cSSara Sharon 
453674a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
453774a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
453874a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
453974a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
454074a10252SSara Sharon }
454174a10252SSara Sharon 
45420202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
45430202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
45440202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
45450202bcf0SEmmanuel Grumbach {
45460202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
45470202bcf0SEmmanuel Grumbach 	u32 apply_time;
45480202bcf0SEmmanuel Grumbach 
45490202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
45500202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
45510202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
45520202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
45530202bcf0SEmmanuel Grumbach 	 * loss).
45540202bcf0SEmmanuel Grumbach 	 */
45550202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
45560202bcf0SEmmanuel Grumbach 		apply_time = 0;
45570202bcf0SEmmanuel Grumbach 	else
45580202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
45590202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
45600202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
45610202bcf0SEmmanuel Grumbach 
45620202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
45630202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
45640202bcf0SEmmanuel Grumbach 
45650202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
45660202bcf0SEmmanuel Grumbach 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
45670202bcf0SEmmanuel Grumbach 
45680202bcf0SEmmanuel Grumbach 		if (ret)
45690202bcf0SEmmanuel Grumbach 			return ret;
45700202bcf0SEmmanuel Grumbach 	}
45710202bcf0SEmmanuel Grumbach 
45720202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
45730202bcf0SEmmanuel Grumbach 				    apply_time);
45740202bcf0SEmmanuel Grumbach 
45750202bcf0SEmmanuel Grumbach 	return 0;
45760202bcf0SEmmanuel Grumbach }
45770202bcf0SEmmanuel Grumbach 
4578f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
4579e705c121SKalle Valo static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4580e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4581e705c121SKalle Valo 				      struct ieee80211_channel_switch *chsw)
4582e705c121SKalle Valo {
4583e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4584e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
4585e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4586e705c121SKalle Valo 	int ret;
4587e705c121SKalle Valo 
4588e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4589e705c121SKalle Valo 
4590e705c121SKalle Valo 	mvmvif->csa_failed = false;
4591e705c121SKalle Valo 
4592e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
4593e705c121SKalle Valo 			   chsw->chandef.center_freq1);
4594e705c121SKalle Valo 
45957174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
45967174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
45977174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
4598e705c121SKalle Valo 
4599e705c121SKalle Valo 	switch (vif->type) {
4600e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4601e705c121SKalle Valo 		csa_vif =
4602e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
4603e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
4604e705c121SKalle Valo 		if (WARN_ONCE(csa_vif && csa_vif->csa_active,
4605e705c121SKalle Valo 			      "Another CSA is already in progress")) {
4606e705c121SKalle Valo 			ret = -EBUSY;
4607e705c121SKalle Valo 			goto out_unlock;
4608e705c121SKalle Valo 		}
4609e705c121SKalle Valo 
4610d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
4611d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
4612d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
4613d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
4614d3a108a4SAndrei Otcheretianski 			goto out_unlock;
4615d3a108a4SAndrei Otcheretianski 		}
4616d3a108a4SAndrei Otcheretianski 
4617e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
4618e705c121SKalle Valo 
4619e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
4620e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
4621e705c121SKalle Valo 			ret = -EBUSY;
4622e705c121SKalle Valo 			goto out_unlock;
4623e705c121SKalle Valo 		}
4624e705c121SKalle Valo 
4625d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
4626d3a108a4SAndrei Otcheretianski 
4627e705c121SKalle Valo 		break;
4628e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
46292360acbdSEmmanuel Grumbach 		/*
46302360acbdSEmmanuel Grumbach 		 * We haven't configured the firmware to be associated yet since
46312360acbdSEmmanuel Grumbach 		 * we don't know the dtim period. In this case, the firmware can't
46322360acbdSEmmanuel Grumbach 		 * track the beacons.
46332360acbdSEmmanuel Grumbach 		 */
46342360acbdSEmmanuel Grumbach 		if (!vif->bss_conf.assoc || !vif->bss_conf.dtim_period) {
46352360acbdSEmmanuel Grumbach 			ret = -EBUSY;
46362360acbdSEmmanuel Grumbach 			goto out_unlock;
46372360acbdSEmmanuel Grumbach 		}
46382360acbdSEmmanuel Grumbach 
463987d9564eSJohannes Berg 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX)
464087d9564eSJohannes Berg 			schedule_delayed_work(&mvmvif->csa_work, 0);
464187d9564eSJohannes Berg 
4642f6780614SSara Sharon 		if (chsw->block_tx) {
4643f6780614SSara Sharon 			/*
4644f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
4645f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
4646f6780614SSara Sharon 			 */
4647f6780614SSara Sharon 			if (!chsw->count ||
4648f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
4649f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
4650f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
4651f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
4652f6780614SSara Sharon 		}
4653e705c121SKalle Valo 
46540202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
46550202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
46560202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
4657e705c121SKalle Valo 			if (ret)
4658e705c121SKalle Valo 				goto out_unlock;
46590202bcf0SEmmanuel Grumbach 		} else {
466074a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
46610202bcf0SEmmanuel Grumbach 		}
466281b4e44eSSara Sharon 
466381b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
466481b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
4665e705c121SKalle Valo 		break;
4666e705c121SKalle Valo 	default:
4667e705c121SKalle Valo 		break;
4668e705c121SKalle Valo 	}
4669e705c121SKalle Valo 
4670e705c121SKalle Valo 	mvmvif->ps_disabled = true;
4671e705c121SKalle Valo 
4672e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
4673e705c121SKalle Valo 	if (ret)
4674e705c121SKalle Valo 		goto out_unlock;
4675e705c121SKalle Valo 
4676e705c121SKalle Valo 	/* we won't be on this channel any longer */
4677e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
4678e705c121SKalle Valo 
4679e705c121SKalle Valo out_unlock:
4680e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4681e705c121SKalle Valo 
4682e705c121SKalle Valo 	return ret;
4683e705c121SKalle Valo }
4684e705c121SKalle Valo 
4685c37763d2SSara Sharon static void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
4686c37763d2SSara Sharon 					     struct ieee80211_vif *vif,
4687c37763d2SSara Sharon 					     struct ieee80211_channel_switch *chsw)
4688e705c121SKalle Valo {
4689e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4690c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4691c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
4692c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4693c37763d2SSara Sharon 							  mvmvif->color)),
4694c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
4695c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
4696c37763d2SSara Sharon 		.cs_count = chsw->count,
469777738865SSara Sharon 		.cs_mode = chsw->block_tx,
4698c37763d2SSara Sharon 	};
4699e705c121SKalle Valo 
4700c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
4701c37763d2SSara Sharon 		return;
4702e705c121SKalle Valo 
470381b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
470481b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
470581b4e44eSSara Sharon 			/* Second time, give up on this AP*/
470681b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
470781b4e44eSSara Sharon 			ieee80211_chswitch_done(vif, false);
470881b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
470981b4e44eSSara Sharon 			return;
4710e705c121SKalle Valo 		}
471181b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
4712e705c121SKalle Valo 	}
471381b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
4714e705c121SKalle Valo 
4715caf46377SSara Sharon 	mutex_lock(&mvm->mutex);
4716caf46377SSara Sharon 	if (mvmvif->csa_failed)
4717caf46377SSara Sharon 		goto out_unlock;
4718e705c121SKalle Valo 
4719b06b5986SEmmanuel Grumbach 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d mode = %d\n",
4720b06b5986SEmmanuel Grumbach 			   mvmvif->id, chsw->count, chsw->block_tx);
4721c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
4722c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
4723c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
4724caf46377SSara Sharon 				     0, sizeof(cmd), &cmd));
4725caf46377SSara Sharon out_unlock:
4726caf46377SSara Sharon 	mutex_unlock(&mvm->mutex);
4727e705c121SKalle Valo }
4728e705c121SKalle Valo 
47296110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
47306110d9e5SDavid Spinadel {
4731435d0827SSara Sharon 	int i;
4732435d0827SSara Sharon 
473306195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
4734309c4848SLuca Coelho 		if (drop) {
4735309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
47366110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
4737d4e3a341SMordechay Goodstein 				iwl_mvm_flushable_queues(mvm) & queues);
4738309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
4739309c4848SLuca Coelho 		} else {
4740435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
4741309c4848SLuca Coelho 		}
4742435d0827SSara Sharon 		return;
4743435d0827SSara Sharon 	}
47446110d9e5SDavid Spinadel 
47456110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
4746be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4747435d0827SSara Sharon 		struct ieee80211_sta *sta;
4748435d0827SSara Sharon 
4749435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
47506110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
47516110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
47526110d9e5SDavid Spinadel 			continue;
47536110d9e5SDavid Spinadel 
475406195639SSara Sharon 		if (drop)
4755d4e3a341SMordechay Goodstein 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
475606195639SSara Sharon 		else
47576110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
47586110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
47596110d9e5SDavid Spinadel 	}
47606110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
47616110d9e5SDavid Spinadel }
47626110d9e5SDavid Spinadel 
4763e705c121SKalle Valo static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4764e705c121SKalle Valo 			      struct ieee80211_vif *vif, u32 queues, bool drop)
4765e705c121SKalle Valo {
4766e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4767e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
4768e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4769e705c121SKalle Valo 	struct ieee80211_sta *sta;
4770e705c121SKalle Valo 	int i;
4771e705c121SKalle Valo 	u32 msk = 0;
4772e705c121SKalle Valo 
47736110d9e5SDavid Spinadel 	if (!vif) {
47746110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
47756110d9e5SDavid Spinadel 		return;
47766110d9e5SDavid Spinadel 	}
47776110d9e5SDavid Spinadel 
47786110d9e5SDavid Spinadel 	if (vif->type != NL80211_IFTYPE_STATION)
4779e705c121SKalle Valo 		return;
4780e705c121SKalle Valo 
478124afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
478224afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
478324afba76SLiad Kaufman 
4784e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4785e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
4786e705c121SKalle Valo 
4787e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
4788be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4789e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4790e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
4791e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
4792e705c121SKalle Valo 			continue;
4793e705c121SKalle Valo 
4794e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4795e705c121SKalle Valo 		if (mvmsta->vif != vif)
4796e705c121SKalle Valo 			continue;
4797e705c121SKalle Valo 
4798e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
4799e705c121SKalle Valo 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4800e705c121SKalle Valo 
4801d49394a1SSara Sharon 		if (drop) {
4802f9084775SNathan Errera 			if (iwl_mvm_flush_sta(mvm, mvmsta, false))
4803d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
4804d49394a1SSara Sharon 		} else {
4805e705c121SKalle Valo 			msk |= mvmsta->tfd_queue_msk;
4806d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
4807d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
4808e705c121SKalle Valo 		}
4809d49394a1SSara Sharon 	}
4810e705c121SKalle Valo 
4811e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4812e705c121SKalle Valo 
4813e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
4814e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
4815e705c121SKalle Valo 	 */
4816d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
4817a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
4818e705c121SKalle Valo }
4819e705c121SKalle Valo 
4820e705c121SKalle Valo static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4821e705c121SKalle Valo 				  struct survey_info *survey)
4822e705c121SKalle Valo {
4823e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4824e705c121SKalle Valo 	int ret;
4825e705c121SKalle Valo 
4826e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
4827e705c121SKalle Valo 
4828e705c121SKalle Valo 	/* only support global statistics right now */
4829e705c121SKalle Valo 	if (idx != 0)
4830e705c121SKalle Valo 		return -ENOENT;
4831e705c121SKalle Valo 
4832280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4833e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4834e705c121SKalle Valo 		return -ENOENT;
4835e705c121SKalle Valo 
4836e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4837e705c121SKalle Valo 
4838aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
4839e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
4840e705c121SKalle Valo 		if (ret)
4841e705c121SKalle Valo 			goto out;
4842e705c121SKalle Valo 	}
4843e705c121SKalle Valo 
4844e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
4845e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
4846e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
4847e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
4848e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
4849e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
4850e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
4851e705c121SKalle Valo 
4852e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
4853e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
4854e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
4855e705c121SKalle Valo 
4856e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
4857e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
4858e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
4859e705c121SKalle Valo 
4860e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4861e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
4862e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
4863e705c121SKalle Valo 
4864e705c121SKalle Valo 	ret = 0;
4865e705c121SKalle Valo  out:
4866e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4867e705c121SKalle Valo 	return ret;
4868e705c121SKalle Valo }
4869e705c121SKalle Valo 
4870ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
4871ed780545SJohannes Berg {
4872ed780545SJohannes Berg 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
4873ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
4874ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
4875ed780545SJohannes Berg 		break;
4876ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
4877ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
4878ed780545SJohannes Berg 		break;
4879ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
4880ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
4881ed780545SJohannes Berg 		break;
4882ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
4883ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
4884ed780545SJohannes Berg 		break;
4885ed780545SJohannes Berg 	}
4886ed780545SJohannes Berg 
4887ed780545SJohannes Berg 	if (rate_n_flags & RATE_MCS_HT_MSK) {
4888ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
4889ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags, RATE_HT_MCS_INDEX_MSK);
4890ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4891ed780545SJohannes Berg 					  RATE_HT_MCS_NSS_MSK) + 1;
4892ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_SGI_MSK)
4893ed780545SJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
4894ed780545SJohannes Berg 	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
4895ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
4896ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags,
4897ed780545SJohannes Berg 					  RATE_VHT_MCS_RATE_CODE_MSK);
4898ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4899ed780545SJohannes Berg 					  RATE_VHT_MCS_NSS_MSK) + 1;
4900ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_SGI_MSK)
4901ed780545SJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
4902ed780545SJohannes Berg 	} else if (rate_n_flags & RATE_MCS_HE_MSK) {
4903ed780545SJohannes Berg 		u32 gi_ltf = u32_get_bits(rate_n_flags,
4904ed780545SJohannes Berg 					  RATE_MCS_HE_GI_LTF_MSK);
4905ed780545SJohannes Berg 
4906ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
4907ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags,
4908ed780545SJohannes Berg 					  RATE_VHT_MCS_RATE_CODE_MSK);
4909ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4910ed780545SJohannes Berg 					  RATE_VHT_MCS_NSS_MSK) + 1;
4911ed780545SJohannes Berg 
4912ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
4913ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
4914ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
4915ed780545SJohannes Berg 		}
4916ed780545SJohannes Berg 
4917ed780545SJohannes Berg 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
4918ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_SU:
4919ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_EXT_SU:
4920ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4921ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4922ed780545SJohannes Berg 			else if (gi_ltf == 2)
4923ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4924ed780545SJohannes Berg 			else if (rate_n_flags & RATE_MCS_SGI_MSK)
4925ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4926ed780545SJohannes Berg 			else
4927ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4928ed780545SJohannes Berg 			break;
4929ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_MU:
4930ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4931ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4932ed780545SJohannes Berg 			else if (gi_ltf == 2)
4933ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4934ed780545SJohannes Berg 			else
4935ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4936ed780545SJohannes Berg 			break;
4937ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_TRIG:
4938ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4939ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4940ed780545SJohannes Berg 			else
4941ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4942ed780545SJohannes Berg 			break;
4943ed780545SJohannes Berg 		}
4944ed780545SJohannes Berg 
4945ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
4946ed780545SJohannes Berg 			rinfo->he_dcm = 1;
4947ed780545SJohannes Berg 	} else {
4948ed780545SJohannes Berg 		switch (u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK)) {
4949ed780545SJohannes Berg 		case IWL_RATE_1M_PLCP:
4950ed780545SJohannes Berg 			rinfo->legacy = 10;
4951ed780545SJohannes Berg 			break;
4952ed780545SJohannes Berg 		case IWL_RATE_2M_PLCP:
4953ed780545SJohannes Berg 			rinfo->legacy = 20;
4954ed780545SJohannes Berg 			break;
4955ed780545SJohannes Berg 		case IWL_RATE_5M_PLCP:
4956ed780545SJohannes Berg 			rinfo->legacy = 55;
4957ed780545SJohannes Berg 			break;
4958ed780545SJohannes Berg 		case IWL_RATE_11M_PLCP:
4959ed780545SJohannes Berg 			rinfo->legacy = 110;
4960ed780545SJohannes Berg 			break;
4961ed780545SJohannes Berg 		case IWL_RATE_6M_PLCP:
4962ed780545SJohannes Berg 			rinfo->legacy = 60;
4963ed780545SJohannes Berg 			break;
4964ed780545SJohannes Berg 		case IWL_RATE_9M_PLCP:
4965ed780545SJohannes Berg 			rinfo->legacy = 90;
4966ed780545SJohannes Berg 			break;
4967ed780545SJohannes Berg 		case IWL_RATE_12M_PLCP:
4968ed780545SJohannes Berg 			rinfo->legacy = 120;
4969ed780545SJohannes Berg 			break;
4970ed780545SJohannes Berg 		case IWL_RATE_18M_PLCP:
4971ed780545SJohannes Berg 			rinfo->legacy = 180;
4972ed780545SJohannes Berg 			break;
4973ed780545SJohannes Berg 		case IWL_RATE_24M_PLCP:
4974ed780545SJohannes Berg 			rinfo->legacy = 240;
4975ed780545SJohannes Berg 			break;
4976ed780545SJohannes Berg 		case IWL_RATE_36M_PLCP:
4977ed780545SJohannes Berg 			rinfo->legacy = 360;
4978ed780545SJohannes Berg 			break;
4979ed780545SJohannes Berg 		case IWL_RATE_48M_PLCP:
4980ed780545SJohannes Berg 			rinfo->legacy = 480;
4981ed780545SJohannes Berg 			break;
4982ed780545SJohannes Berg 		case IWL_RATE_54M_PLCP:
4983ed780545SJohannes Berg 			rinfo->legacy = 540;
4984ed780545SJohannes Berg 			break;
4985ed780545SJohannes Berg 		}
4986ed780545SJohannes Berg 	}
4987ed780545SJohannes Berg }
4988ed780545SJohannes Berg 
4989e705c121SKalle Valo static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
4990e705c121SKalle Valo 				       struct ieee80211_vif *vif,
4991e705c121SKalle Valo 				       struct ieee80211_sta *sta,
4992e705c121SKalle Valo 				       struct station_info *sinfo)
4993e705c121SKalle Valo {
4994e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4995e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4996e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4997e705c121SKalle Valo 
4998988b5968SSara Sharon 	if (mvmsta->avg_energy) {
499988ad368aSBen Greear 		sinfo->signal_avg = -(s8)mvmsta->avg_energy;
500022d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
5001988b5968SSara Sharon 	}
5002988b5968SSara Sharon 
5003ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
5004ed780545SJohannes Berg 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
5005ed780545SJohannes Berg 
5006ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5007ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5008ed780545SJohannes Berg 	}
5009ed780545SJohannes Berg 
5010e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
5011e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5012e705c121SKalle Valo 		return;
5013e705c121SKalle Valo 
5014e705c121SKalle Valo 	if (!vif->bss_conf.assoc)
5015e705c121SKalle Valo 		return;
5016e705c121SKalle Valo 
5017e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5018e705c121SKalle Valo 
5019e705c121SKalle Valo 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
5020e705c121SKalle Valo 		goto unlock;
5021e705c121SKalle Valo 
5022e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
5023e705c121SKalle Valo 		goto unlock;
5024e705c121SKalle Valo 
5025e705c121SKalle Valo 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
5026e705c121SKalle Valo 			   mvmvif->beacon_stats.accu_num_beacons;
502722d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
5028e705c121SKalle Valo 	if (mvmvif->beacon_stats.avg_signal) {
5029e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
5030e705c121SKalle Valo 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
503122d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
5032e705c121SKalle Valo 	}
5033e705c121SKalle Valo  unlock:
5034e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5035e705c121SKalle Valo }
5036e705c121SKalle Valo 
5037119c2a13SMordechay Goodstein static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5038119c2a13SMordechay Goodstein 					    struct ieee80211_vif *vif,
5039119c2a13SMordechay Goodstein 					    const  struct ieee80211_mlme_event *mlme)
5040119c2a13SMordechay Goodstein {
50411a81bddfSMordechay Goodstein 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
50421a81bddfSMordechay Goodstein 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
5043119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5044119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5045119c2a13SMordechay Goodstein 				       NULL);
5046119c2a13SMordechay Goodstein 		return;
5047119c2a13SMordechay Goodstein 	}
5048119c2a13SMordechay Goodstein 
5049119c2a13SMordechay Goodstein 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5050119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5051119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_DEASSOC,
5052119c2a13SMordechay Goodstein 				       NULL);
5053119c2a13SMordechay Goodstein 		return;
5054119c2a13SMordechay Goodstein 	}
5055119c2a13SMordechay Goodstein }
5056119c2a13SMordechay Goodstein 
5057e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5058e705c121SKalle Valo 					struct ieee80211_vif *vif,
5059e705c121SKalle Valo 					const struct ieee80211_event *event)
5060e705c121SKalle Valo {
50614c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5062e705c121SKalle Valo 	do {								\
50634c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5064e705c121SKalle Valo 			break;						\
50657174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5066e705c121SKalle Valo 	} while (0)
5067e705c121SKalle Valo 
5068e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5069e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5070e705c121SKalle Valo 
5071119c2a13SMordechay Goodstein 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5072119c2a13SMordechay Goodstein 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5073119c2a13SMordechay Goodstein 		return;
5074119c2a13SMordechay Goodstein 	}
5075119c2a13SMordechay Goodstein 
50766c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
50776c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
50786c042d75SSara Sharon 	if (!trig)
5079e705c121SKalle Valo 		return;
5080e705c121SKalle Valo 
5081e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
5082e705c121SKalle Valo 
5083e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
5084e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
50854c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
5086e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
5087e705c121SKalle Valo 					    event->u.mlme.reason);
5088e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
50894c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
5090e705c121SKalle Valo 					   "ASSOC TIMEOUT");
5091e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
5092e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
50934c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
5094e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
5095e705c121SKalle Valo 					   event->u.mlme.reason);
5096e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
50974c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
5098e705c121SKalle Valo 					   "AUTH TIMEOUT");
5099e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
51004c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
5101e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
5102e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
51034c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
5104e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
5105e705c121SKalle Valo 	}
5106e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
5107e705c121SKalle Valo }
5108e705c121SKalle Valo 
5109e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5110e705c121SKalle Valo 					  struct ieee80211_vif *vif,
5111e705c121SKalle Valo 					  const struct ieee80211_event *event)
5112e705c121SKalle Valo {
5113e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5114e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
5115e705c121SKalle Valo 
51166c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
51176c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
51186c042d75SSara Sharon 	if (!trig)
5119e705c121SKalle Valo 		return;
5120e705c121SKalle Valo 
5121e705c121SKalle Valo 	ba_trig = (void *)trig->data;
5122e705c121SKalle Valo 
5123e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5124e705c121SKalle Valo 		return;
5125e705c121SKalle Valo 
51267174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
5127e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
5128e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
5129e705c121SKalle Valo 				event->u.ba.ssn);
5130e705c121SKalle Valo }
5131e705c121SKalle Valo 
5132e705c121SKalle Valo static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
5133e705c121SKalle Valo 				       struct ieee80211_vif *vif,
5134e705c121SKalle Valo 				       const struct ieee80211_event *event)
5135e705c121SKalle Valo {
5136e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5137e705c121SKalle Valo 
5138e705c121SKalle Valo 	switch (event->type) {
5139e705c121SKalle Valo 	case MLME_EVENT:
5140e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
5141e705c121SKalle Valo 		break;
5142e705c121SKalle Valo 	case BAR_RX_EVENT:
5143e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
5144e705c121SKalle Valo 		break;
5145e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
5146528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
5147528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
5148e705c121SKalle Valo 		break;
5149e705c121SKalle Valo 	default:
5150e705c121SKalle Valo 		break;
5151e705c121SKalle Valo 	}
5152e705c121SKalle Valo }
5153e705c121SKalle Valo 
5154d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
51555e1688ceSJohannes Berg 				     enum iwl_mvm_rxq_notif_type type,
51565e1688ceSJohannes Berg 				     bool sync,
51575e1688ceSJohannes Berg 				     const void *data, u32 size)
51580636b938SSara Sharon {
51595e1688ceSJohannes Berg 	struct {
51605e1688ceSJohannes Berg 		struct iwl_rxq_sync_cmd cmd;
51615e1688ceSJohannes Berg 		struct iwl_mvm_internal_rxq_notif notif;
51625e1688ceSJohannes Berg 	} __packed cmd = {
51635e1688ceSJohannes Berg 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
51645e1688ceSJohannes Berg 		.cmd.count =
51655e1688ceSJohannes Berg 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
51665e1688ceSJohannes Berg 				    size),
51675e1688ceSJohannes Berg 		.notif.type = type,
51685e1688ceSJohannes Berg 		.notif.sync = sync,
51695e1688ceSJohannes Berg 	};
51705e1688ceSJohannes Berg 	struct iwl_host_cmd hcmd = {
51715e1688ceSJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
51725e1688ceSJohannes Berg 		.data[0] = &cmd,
51735e1688ceSJohannes Berg 		.len[0] = sizeof(cmd),
51745e1688ceSJohannes Berg 		.data[1] = data,
51755e1688ceSJohannes Berg 		.len[1] = size,
51765e1688ceSJohannes Berg 		.flags = sync ? 0 : CMD_ASYNC,
51775e1688ceSJohannes Berg 	};
51780636b938SSara Sharon 	int ret;
51790636b938SSara Sharon 
51805e1688ceSJohannes Berg 	/* size must be a multiple of DWORD */
51815e1688ceSJohannes Berg 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
51825e1688ceSJohannes Berg 		return;
51830636b938SSara Sharon 
5184cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
51850636b938SSara Sharon 		return;
51860636b938SSara Sharon 
51875e1688ceSJohannes Berg 	if (sync) {
51885e1688ceSJohannes Berg 		cmd.notif.cookie = mvm->queue_sync_cookie;
51892f7a04c7SJohannes Berg 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
5190a2113cc4SNaftali Goldstein 	}
5191d0ff5d22SSara Sharon 
51925e1688ceSJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
51930636b938SSara Sharon 	if (ret) {
51940636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
51950636b938SSara Sharon 		goto out;
51960636b938SSara Sharon 	}
5197d0ff5d22SSara Sharon 
51985e1688ceSJohannes Berg 	if (sync) {
51993c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
52003a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
52012f7a04c7SJohannes Berg 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
52026ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
52030636b938SSara Sharon 					 HZ);
52042f7a04c7SJohannes Berg 		WARN_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm),
52052f7a04c7SJohannes Berg 			  "queue sync: failed to sync, state is 0x%lx\n",
52062f7a04c7SJohannes Berg 			  mvm->queue_sync_state);
52076ad04359SJohannes Berg 	}
52080636b938SSara Sharon 
52090636b938SSara Sharon out:
52105e1688ceSJohannes Berg 	if (sync) {
52112f7a04c7SJohannes Berg 		mvm->queue_sync_state = 0;
52120636b938SSara Sharon 		mvm->queue_sync_cookie++;
52130636b938SSara Sharon 	}
52145f8a3561SJohannes Berg }
52150636b938SSara Sharon 
52160636b938SSara Sharon static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
52170636b938SSara Sharon {
52180636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
52190636b938SSara Sharon 
52200636b938SSara Sharon 	mutex_lock(&mvm->mutex);
52215e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
52220636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
52230636b938SSara Sharon }
52240636b938SSara Sharon 
5225b73f9a4aSJohannes Berg static int
5226b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
5227b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
5228b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
5229b73f9a4aSJohannes Berg {
5230b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5231b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5232b73f9a4aSJohannes Berg 
5233b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
5234b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
5235b73f9a4aSJohannes Berg 		return -EINVAL;
5236b73f9a4aSJohannes Berg 
5237b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
5238b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
5239b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
5240b73f9a4aSJohannes Berg 
5241b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
5242b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
5243b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
5244b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
5245b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
5246b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
5247b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
5248b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
5249b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
5250b73f9a4aSJohannes Berg 
5251b73f9a4aSJohannes Berg 	return 0;
5252b73f9a4aSJohannes Berg }
5253b73f9a4aSJohannes Berg 
5254fc36ffdaSJohannes Berg static int iwl_mvm_start_pmsr(struct ieee80211_hw *hw,
5255fc36ffdaSJohannes Berg 			      struct ieee80211_vif *vif,
5256fc36ffdaSJohannes Berg 			      struct cfg80211_pmsr_request *request)
5257fc36ffdaSJohannes Berg {
5258fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5259fc36ffdaSJohannes Berg 	int ret;
5260fc36ffdaSJohannes Berg 
5261fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5262fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
5263fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5264fc36ffdaSJohannes Berg 
5265fc36ffdaSJohannes Berg 	return ret;
5266fc36ffdaSJohannes Berg }
5267fc36ffdaSJohannes Berg 
5268fc36ffdaSJohannes Berg static void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw,
5269fc36ffdaSJohannes Berg 			       struct ieee80211_vif *vif,
5270fc36ffdaSJohannes Berg 			       struct cfg80211_pmsr_request *request)
5271fc36ffdaSJohannes Berg {
5272fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5273fc36ffdaSJohannes Berg 
5274fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5275fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
5276fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5277fc36ffdaSJohannes Berg }
5278fc36ffdaSJohannes Berg 
5279438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
5280438af969SSara Sharon {
5281438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
5282438af969SSara Sharon 
5283438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
5284438af969SSara Sharon 		return false;
5285438af969SSara Sharon 
5286438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
5287438af969SSara Sharon }
5288438af969SSara Sharon 
5289438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
5290438af969SSara Sharon 				      struct sk_buff *head,
5291438af969SSara Sharon 				      struct sk_buff *skb)
5292438af969SSara Sharon {
5293438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5294438af969SSara Sharon 
5295438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
5296438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
5297438af969SSara Sharon 		return false;
5298438af969SSara Sharon 
5299438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
5300438af969SSara Sharon 		return true;
5301438af969SSara Sharon 
5302438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5303438af969SSara Sharon }
5304438af969SSara Sharon 
5305e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
5306e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
5307cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
5308e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
5309e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
5310e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
5311e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
5312e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
5313e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
5314e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
5315e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
5316e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
5317e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
5318e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
5319e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
5320e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
5321e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
5322e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
5323e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
5324e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
5325e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
5326e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
5327e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
5328e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
5329e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
5330e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
5331e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
5332e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
5333e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
5334e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
5335e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
5336e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
5337e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
5338e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
5339e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
5340e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
5341e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
5342e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5343e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
5344e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
5345e705c121SKalle Valo 
5346e705c121SKalle Valo 	.start_ap = iwl_mvm_start_ap_ibss,
5347e705c121SKalle Valo 	.stop_ap = iwl_mvm_stop_ap_ibss,
5348e705c121SKalle Valo 	.join_ibss = iwl_mvm_start_ap_ibss,
5349e705c121SKalle Valo 	.leave_ibss = iwl_mvm_stop_ap_ibss,
5350e705c121SKalle Valo 
53512f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
53522f0282dbSJohannes Berg 
5353e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
5354e705c121SKalle Valo 
5355e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
5356e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
5357e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
535879221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
5359c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
5360e705c121SKalle Valo 
5361e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5362e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5363e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5364e705c121SKalle Valo 
5365e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
5366e705c121SKalle Valo 
53670636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
53680636b938SSara Sharon 
5369e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5370e705c121SKalle Valo 
5371e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
5372e705c121SKalle Valo 	/* look at d3.c */
5373e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
5374e705c121SKalle Valo 	.resume = iwl_mvm_resume,
5375e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
5376e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
5377e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
5378e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
5379e705c121SKalle Valo #endif
5380e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
5381e705c121SKalle Valo #endif
5382e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
5383e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
5384b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
5385fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
5386fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
5387b73f9a4aSJohannes Berg 
5388438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
5389177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
5390177a11cfSGregory Greenman 	.sta_add_debugfs = iwl_mvm_sta_add_debugfs,
5391177a11cfSGregory Greenman #endif
5392e705c121SKalle Valo };
5393