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 
393366fc672SJohannes Berg 	hw->queues = IEEE80211_NUM_ACS;
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 
76510516783SJohannes Berg 	/*
76610516783SJohannes Berg 	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
76710516783SJohannes Berg 	 * so we treat the others as broadcast
76810516783SJohannes Berg 	 */
76910516783SJohannes Berg 	if (ieee80211_is_mgmt(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];
2025*4e6b69ecSJohannes Berg 	own_he_cap = ieee80211_get_he_iftype_cap(sband,
2026*4e6b69ecSJohannes Berg 						 ieee80211_vif_type_p2p(vif));
2027ee1a02d7SShaul Triebitz 
2028514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
202912d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2030514c3069SLuca Coelho 		rcu_read_unlock();
2031514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2032514c3069SLuca Coelho 		return;
2033514c3069SLuca Coelho 	}
2034514c3069SLuca Coelho 
2035514c3069SLuca Coelho 	if (!sta->he_cap.has_he) {
2036514c3069SLuca Coelho 		rcu_read_unlock();
2037514c3069SLuca Coelho 		return;
2038514c3069SLuca Coelho 	}
2039514c3069SLuca Coelho 
2040514c3069SLuca Coelho 	flags = 0;
2041514c3069SLuca Coelho 
20424f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
20434f58121dSIlan Peer 	if (mvmvif->he_ru_2mhz_block)
20444f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
20454f58121dSIlan Peer 
2046514c3069SLuca Coelho 	/* HTC flags */
2047514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[0] &
2048514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP0_HTC_HE)
2049514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2050514c3069SLuca Coelho 	if ((sta->he_cap.he_cap_elem.mac_cap_info[1] &
2051514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2052514c3069SLuca Coelho 	    (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2053514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2054514c3069SLuca Coelho 		u8 link_adap =
2055514c3069SLuca Coelho 			((sta->he_cap.he_cap_elem.mac_cap_info[2] &
2056514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2057514c3069SLuca Coelho 			 (sta->he_cap.he_cap_elem.mac_cap_info[1] &
2058514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2059514c3069SLuca Coelho 
2060514c3069SLuca Coelho 		if (link_adap == 2)
2061514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2062514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2063514c3069SLuca Coelho 		else if (link_adap == 3)
2064514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2065514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2066514c3069SLuca Coelho 	}
2067514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2068514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2069514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[3] &
2070514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2071514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2072514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2073514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2074514c3069SLuca Coelho 
2075189b8d44SNaftali Goldstein 	/*
2076189b8d44SNaftali Goldstein 	 * Initialize the PPE thresholds to "None" (7), as described in Table
2077189b8d44SNaftali Goldstein 	 * 9-262ac of 80211.ax/D3.0.
2078189b8d44SNaftali Goldstein 	 */
2079189b8d44SNaftali Goldstein 	memset(&sta_ctxt_cmd.pkt_ext, 7, sizeof(sta_ctxt_cmd.pkt_ext));
2080189b8d44SNaftali Goldstein 
2081189b8d44SNaftali Goldstein 	/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2082514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.phy_cap_info[6] &
2083514c3069SLuca Coelho 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2084514c3069SLuca Coelho 		u8 nss = (sta->he_cap.ppe_thres[0] &
2085514c3069SLuca Coelho 			  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2086514c3069SLuca Coelho 		u8 ru_index_bitmap =
2087514c3069SLuca Coelho 			(sta->he_cap.ppe_thres[0] &
2088514c3069SLuca Coelho 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2089514c3069SLuca Coelho 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2090514c3069SLuca Coelho 		u8 *ppe = &sta->he_cap.ppe_thres[0];
2091514c3069SLuca Coelho 		u8 ppe_pos_bit = 7; /* Starting after PPE header */
2092514c3069SLuca Coelho 
2093514c3069SLuca Coelho 		/*
2094514c3069SLuca Coelho 		 * FW currently supports only nss == MAX_HE_SUPP_NSS
2095514c3069SLuca Coelho 		 *
2096514c3069SLuca Coelho 		 * If nss > MAX: we can ignore values we don't support
2097514c3069SLuca Coelho 		 * If nss < MAX: we can set zeros in other streams
2098514c3069SLuca Coelho 		 */
2099514c3069SLuca Coelho 		if (nss > MAX_HE_SUPP_NSS) {
2100514c3069SLuca Coelho 			IWL_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2101514c3069SLuca Coelho 				 MAX_HE_SUPP_NSS);
2102514c3069SLuca Coelho 			nss = MAX_HE_SUPP_NSS;
2103514c3069SLuca Coelho 		}
2104514c3069SLuca Coelho 
2105514c3069SLuca Coelho 		for (i = 0; i < nss; i++) {
2106514c3069SLuca Coelho 			u8 ru_index_tmp = ru_index_bitmap << 1;
2107514c3069SLuca Coelho 			u8 bw;
2108514c3069SLuca Coelho 
2109514c3069SLuca Coelho 			for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX; bw++) {
2110514c3069SLuca Coelho 				ru_index_tmp >>= 1;
2111514c3069SLuca Coelho 				if (!(ru_index_tmp & 1))
2112514c3069SLuca Coelho 					continue;
2113514c3069SLuca Coelho 
2114514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][1] =
2115514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2116514c3069SLuca Coelho 							       ppe_pos_bit);
2117514c3069SLuca Coelho 				ppe_pos_bit +=
2118514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2119514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0] =
2120514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2121514c3069SLuca Coelho 							       ppe_pos_bit);
2122514c3069SLuca Coelho 				ppe_pos_bit +=
2123514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2124514c3069SLuca Coelho 			}
2125514c3069SLuca Coelho 		}
2126514c3069SLuca Coelho 
2127514c3069SLuca Coelho 		flags |= STA_CTXT_HE_PACKET_EXT;
212877ff2c6bSLiad Kaufman 	} else if ((sta->he_cap.he_cap_elem.phy_cap_info[9] &
212977ff2c6bSLiad Kaufman 		    IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) !=
213077ff2c6bSLiad Kaufman 		  IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_RESERVED) {
213177ff2c6bSLiad Kaufman 		int low_th = -1;
213277ff2c6bSLiad Kaufman 		int high_th = -1;
213377ff2c6bSLiad Kaufman 
213477ff2c6bSLiad Kaufman 		/* Take the PPE thresholds from the nominal padding info */
213577ff2c6bSLiad Kaufman 		switch (sta->he_cap.he_cap_elem.phy_cap_info[9] &
213677ff2c6bSLiad Kaufman 			IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) {
213777ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_0US:
213877ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
213977ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
214077ff2c6bSLiad Kaufman 			break;
214177ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_8US:
214277ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_BPSK;
214377ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
214477ff2c6bSLiad Kaufman 			break;
214577ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US:
214677ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
214777ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_BPSK;
214877ff2c6bSLiad Kaufman 			break;
214977ff2c6bSLiad Kaufman 		}
215077ff2c6bSLiad Kaufman 
215177ff2c6bSLiad Kaufman 		/* Set the PPE thresholds accordingly */
215277ff2c6bSLiad Kaufman 		if (low_th >= 0 && high_th >= 0) {
21530bfefe2fSLiad Kaufman 			struct iwl_he_pkt_ext *pkt_ext =
21540bfefe2fSLiad Kaufman 				(struct iwl_he_pkt_ext *)&sta_ctxt_cmd.pkt_ext;
215577ff2c6bSLiad Kaufman 
215677ff2c6bSLiad Kaufman 			for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
215777ff2c6bSLiad Kaufman 				u8 bw;
215877ff2c6bSLiad Kaufman 
215977ff2c6bSLiad Kaufman 				for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX;
216077ff2c6bSLiad Kaufman 				     bw++) {
21610bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][0] =
21620bfefe2fSLiad Kaufman 						low_th;
21630bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][1] =
21640bfefe2fSLiad Kaufman 						high_th;
216577ff2c6bSLiad Kaufman 				}
216677ff2c6bSLiad Kaufman 			}
216777ff2c6bSLiad Kaufman 
216877ff2c6bSLiad Kaufman 			flags |= STA_CTXT_HE_PACKET_EXT;
216977ff2c6bSLiad Kaufman 		}
2170514c3069SLuca Coelho 	}
217173f23d91SShaul Triebitz 
217273f23d91SShaul Triebitz 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] &
217373f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
217473f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
217573f23d91SShaul Triebitz 
217673f23d91SShaul Triebitz 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] &
217773f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
217873f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
217973f23d91SShaul Triebitz 
2180514c3069SLuca Coelho 	rcu_read_unlock();
2181514c3069SLuca Coelho 
2182514c3069SLuca Coelho 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2183514c3069SLuca Coelho 	flags |= STA_CTXT_HE_MU_EDCA_CW;
21845cc74f65SShaul Triebitz 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2185514c3069SLuca Coelho 		struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2186514c3069SLuca Coelho 			&mvmvif->queue_params[i].mu_edca_param_rec;
21875cc74f65SShaul Triebitz 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2188514c3069SLuca Coelho 
2189514c3069SLuca Coelho 		if (!mvmvif->queue_params[i].mu_edca) {
2190514c3069SLuca Coelho 			flags &= ~STA_CTXT_HE_MU_EDCA_CW;
2191514c3069SLuca Coelho 			break;
2192514c3069SLuca Coelho 		}
2193514c3069SLuca Coelho 
21945cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].cwmin =
2195514c3069SLuca Coelho 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
21965cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].cwmax =
2197514c3069SLuca Coelho 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
21985cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].aifsn =
2199514c3069SLuca Coelho 			cpu_to_le16(mu_edca->aifsn);
22005cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].mu_time =
2201514c3069SLuca Coelho 			cpu_to_le16(mu_edca->mu_edca_timer);
2202514c3069SLuca Coelho 	}
2203514c3069SLuca Coelho 
2204514c3069SLuca Coelho 
2205514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2206514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2207514c3069SLuca Coelho 
2208514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2209514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2210514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2211514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2212514c3069SLuca Coelho 	}
2213514c3069SLuca Coelho 
2214ee1a02d7SShaul Triebitz 	if (own_he_cap && !(own_he_cap->he_cap_elem.mac_cap_info[2] &
2215ee1a02d7SShaul Triebitz 			    IEEE80211_HE_MAC_CAP2_ACK_EN))
2216ee1a02d7SShaul Triebitz 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2217ee1a02d7SShaul Triebitz 
2218918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2219918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2220918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2221918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2222d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2223d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2224d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2225d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2226d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2227d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2228918cbf39SSara Sharon 	}
2229514c3069SLuca Coelho 
2230514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2231514c3069SLuca Coelho 
2232514c3069SLuca Coelho 	if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(STA_HE_CTXT_CMD,
2233514c3069SLuca Coelho 						 DATA_PATH_GROUP, 0),
2234d14ae796SSara Sharon 				 0, size, &sta_ctxt_cmd))
2235514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2236514c3069SLuca Coelho }
2237514c3069SLuca Coelho 
2238e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2239e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2240e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
2241e705c121SKalle Valo 					     u32 changes)
2242e705c121SKalle Valo {
2243e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2244e705c121SKalle Valo 	int ret;
2245e705c121SKalle Valo 
2246e705c121SKalle Valo 	/*
2247cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend
2248cdaba917SEmmanuel Grumbach 	 * on the beacon interval, which was not known when the station
2249cdaba917SEmmanuel Grumbach 	 * interface was added.
2250e705c121SKalle Valo 	 */
2251514c3069SLuca Coelho 	if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
2252230ba6c5SLuca Coelho 		if (vif->bss_conf.he_support &&
2253230ba6c5SLuca Coelho 		    !iwlwifi_mod_params.disable_11ax)
2254514c3069SLuca Coelho 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2255514c3069SLuca Coelho 
2256e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2257514c3069SLuca Coelho 	}
2258e705c121SKalle Valo 
225940ecdd01SShaul Triebitz 	/* Update MU EDCA params */
226040ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
226140ecdd01SShaul Triebitz 	    bss_conf->assoc && vif->bss_conf.he_support &&
226240ecdd01SShaul Triebitz 	    !iwlwifi_mod_params.disable_11ax)
226340ecdd01SShaul Triebitz 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
226440ecdd01SShaul Triebitz 
2265e705c121SKalle Valo 	/*
2266e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2267e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2268e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2269e705c121SKalle Valo 	 * branch for disassociation below.
2270e705c121SKalle Valo 	 */
2271e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2272e705c121SKalle Valo 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2273e705c121SKalle Valo 
2274e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
2275e705c121SKalle Valo 	if (ret)
2276e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2277e705c121SKalle Valo 
2278e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2279e705c121SKalle Valo 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2280e705c121SKalle Valo 	mvmvif->associated = bss_conf->assoc;
2281e705c121SKalle Valo 
2282e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2283e705c121SKalle Valo 		if (bss_conf->assoc) {
2284e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2285e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2286e705c121SKalle Valo 			memset(&mvmvif->beacon_stats, 0,
2287e705c121SKalle Valo 			       sizeof(mvmvif->beacon_stats));
2288e705c121SKalle Valo 
2289e705c121SKalle Valo 			/* add quota for this interface */
2290e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2291e705c121SKalle Valo 			if (ret) {
2292e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2293e705c121SKalle Valo 				return;
2294e705c121SKalle Valo 			}
2295e705c121SKalle Valo 
2296e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2297fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2298fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2299fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2300e705c121SKalle Valo 				/*
2301e705c121SKalle Valo 				 * If we're restarting then the firmware will
2302e705c121SKalle Valo 				 * obviously have lost synchronisation with
2303e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2304e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2305e705c121SKalle Valo 				 * scheduling a session protection time event.
2306e705c121SKalle Valo 				 *
2307e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2308e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2309e705c121SKalle Valo 				 * the beacon interval.
2310e705c121SKalle Valo 				 *
2311e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2312e705c121SKalle Valo 				 * than a single interface.
2313fe959c7bSEmmanuel Grumbach 				 *
2314fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2315fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2316fe959c7bSEmmanuel Grumbach 				 * only anyway.
2317e705c121SKalle Valo 				 */
2318e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2319e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2320e705c121SKalle Valo 							5 * dur, false);
2321e705c121SKalle Valo 			}
2322e705c121SKalle Valo 
2323e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2324e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2325e705c121SKalle Valo 			if (vif->p2p) {
2326e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2327e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
2328e705c121SKalle Valo 						    IEEE80211_SMPS_DYNAMIC);
2329e705c121SKalle Valo 			}
23300ae98812SSara Sharon 		} else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
2331e705c121SKalle Valo 			/*
2332e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2333e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2334e705c121SKalle Valo 			 */
233569e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
233669e508b4SIlan Peer 			WARN_ONCE(ret &&
233769e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
233869e508b4SIlan Peer 					    &mvm->status),
2339e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2340e705c121SKalle Valo 
23416b28f978SEmmanuel Grumbach 			/*
23426b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
23436b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
23446b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
23456b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
23466b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
23476b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
23486b28f978SEmmanuel Grumbach 			 */
23496b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
23506b28f978SEmmanuel Grumbach 				      &mvm->status)) {
23516b28f978SEmmanuel Grumbach 				/*
23526b28f978SEmmanuel Grumbach 				 * Remove AP station now that
23536b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
23546b28f978SEmmanuel Grumbach 				 */
23556b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
23566b28f978SEmmanuel Grumbach 							mvmvif->ap_sta_id);
2357e705c121SKalle Valo 				if (ret)
23586b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
23596b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2360e705c121SKalle Valo 
23610ae98812SSara Sharon 				mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
23626b28f978SEmmanuel Grumbach 			}
23636b28f978SEmmanuel Grumbach 
2364e705c121SKalle Valo 			/* remove quota for this interface */
2365e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2366e705c121SKalle Valo 			if (ret)
2367e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2368e705c121SKalle Valo 
2369e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2370e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2371e705c121SKalle Valo 			if (ret)
2372e705c121SKalle Valo 				IWL_ERR(mvm,
2373e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2374e705c121SKalle Valo 					vif->addr);
2375e705c121SKalle Valo 		}
2376e705c121SKalle Valo 
2377a07a8f37SSara Sharon 		/*
2378a07a8f37SSara Sharon 		 * The firmware tracks the MU-MIMO group on its own.
2379f92659a1SSara Sharon 		 * However, on HW restart we should restore this data.
2380a07a8f37SSara Sharon 		 */
2381a07a8f37SSara Sharon 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2382f92659a1SSara Sharon 		    (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
2383a07a8f37SSara Sharon 			ret = iwl_mvm_update_mu_groups(mvm, vif);
2384a07a8f37SSara Sharon 			if (ret)
2385a07a8f37SSara Sharon 				IWL_ERR(mvm,
2386a07a8f37SSara Sharon 					"failed to update VHT MU_MIMO groups\n");
2387a07a8f37SSara Sharon 		}
2388a07a8f37SSara Sharon 
2389e705c121SKalle Valo 		iwl_mvm_recalc_multicast(mvm);
239034672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2391e705c121SKalle Valo 
2392e705c121SKalle Valo 		/* reset rssi values */
2393e705c121SKalle Valo 		mvmvif->bf_data.ave_beacon_signal = 0;
2394e705c121SKalle Valo 
2395e705c121SKalle Valo 		iwl_mvm_bt_coex_vif_change(mvm);
2396e705c121SKalle Valo 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2397e705c121SKalle Valo 				    IEEE80211_SMPS_AUTOMATIC);
2398355346baSAvraham Stern 		if (fw_has_capa(&mvm->fw->ucode_capa,
2399355346baSAvraham Stern 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2400355346baSAvraham Stern 			iwl_mvm_config_scan(mvm);
2401b45242c9SAvraham Stern 	}
2402b45242c9SAvraham Stern 
2403b45242c9SAvraham Stern 	if (changes & BSS_CHANGED_BEACON_INFO) {
2404e705c121SKalle Valo 		/*
2405b45242c9SAvraham Stern 		 * We received a beacon from the associated AP so
2406e705c121SKalle Valo 		 * remove the session protection.
2407fe959c7bSEmmanuel Grumbach 		 * A firmware with the new API will remove it automatically.
2408e705c121SKalle Valo 		 */
2409fe959c7bSEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
2410fe959c7bSEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
24113edfb5f4SAvraham Stern 			iwl_mvm_stop_session_protection(mvm, vif);
2412e705c121SKalle Valo 
2413e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2414e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2415e705c121SKalle Valo 	}
2416e705c121SKalle Valo 
2417283115fbSAvri Altman 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2418283115fbSAvri Altman 		       /*
2419283115fbSAvri Altman 			* Send power command on every beacon change,
2420283115fbSAvri Altman 			* because we may have not enabled beacon abort yet.
2421283115fbSAvri Altman 			*/
2422283115fbSAvri Altman 		       BSS_CHANGED_BEACON_INFO)) {
2423e705c121SKalle Valo 		ret = iwl_mvm_power_update_mac(mvm);
2424e705c121SKalle Valo 		if (ret)
2425e705c121SKalle Valo 			IWL_ERR(mvm, "failed to update power mode\n");
2426e705c121SKalle Valo 	}
2427e705c121SKalle Valo 
2428e705c121SKalle Valo 	if (changes & BSS_CHANGED_CQM) {
2429e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2430e705c121SKalle Valo 		/* reset cqm events tracking */
2431e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = 0;
2432e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
2433e705c121SKalle Valo 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2434e705c121SKalle Valo 			if (ret)
2435e705c121SKalle Valo 				IWL_ERR(mvm,
2436e705c121SKalle Valo 					"failed to update CQM thresholds\n");
2437e705c121SKalle Valo 		}
2438e705c121SKalle Valo 	}
2439e705c121SKalle Valo 
2440e705c121SKalle Valo 	if (changes & BSS_CHANGED_ARP_FILTER) {
2441e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
244234672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2443e705c121SKalle Valo 	}
2444de34d1c1SJohannes Berg 
2445de34d1c1SJohannes Berg 	if (changes & BSS_CHANGED_BANDWIDTH)
2446de34d1c1SJohannes Berg 		iwl_mvm_apply_fw_smps_request(vif);
2447e705c121SKalle Valo }
2448e705c121SKalle Valo 
2449e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2450e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2451e705c121SKalle Valo {
2452e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2453e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2454c56e00a3SJohannes Berg 	int ret, i;
2455e705c121SKalle Valo 
2456e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2457e705c121SKalle Valo 
2458e705c121SKalle Valo 	/* Send the beacon template */
2459e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2460e705c121SKalle Valo 	if (ret)
2461e705c121SKalle Valo 		goto out_unlock;
2462e705c121SKalle Valo 
2463e705c121SKalle Valo 	/*
2464cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2465cdaba917SEmmanuel Grumbach 	 * the beacon interval, which was not known when the AP interface
2466cdaba917SEmmanuel Grumbach 	 * was added.
2467e705c121SKalle Valo 	 */
2468e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2469e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2470e705c121SKalle Valo 
2471e705c121SKalle Valo 	mvmvif->ap_assoc_sta_count = 0;
2472e705c121SKalle Valo 
2473e705c121SKalle Valo 	/* Add the mac context */
2474e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2475e705c121SKalle Valo 	if (ret)
2476e705c121SKalle Valo 		goto out_unlock;
2477e705c121SKalle Valo 
2478e705c121SKalle Valo 	/* Perform the binding */
2479e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2480e705c121SKalle Valo 	if (ret)
2481e705c121SKalle Valo 		goto out_remove;
2482e705c121SKalle Valo 
248363dd5d02SSara Sharon 	/*
248463dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
248563dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
248663dd5d02SSara Sharon 	 * cause assert 0x2b00.
248763dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
248863dd5d02SSara Sharon 	 * the TLV
248963dd5d02SSara Sharon 	 */
249063dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2491ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2492ced19f26SSara Sharon 		if (ret)
2493ced19f26SSara Sharon 			goto out_unbind;
249463dd5d02SSara Sharon 		/*
249563dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
249663dd5d02SSara Sharon 		 * events are added and applied to the scheduler
249763dd5d02SSara Sharon 		 */
2498e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
249963dd5d02SSara Sharon 		if (ret) {
250063dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
250163dd5d02SSara Sharon 			goto out_unbind;
250263dd5d02SSara Sharon 		}
250363dd5d02SSara Sharon 	} else {
250463dd5d02SSara Sharon 		/*
250563dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
250663dd5d02SSara Sharon 		 * events are added and applied to the scheduler
250763dd5d02SSara Sharon 		 */
250875fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2509e705c121SKalle Valo 		if (ret)
251063dd5d02SSara Sharon 			goto out_unbind;
251175fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
251263dd5d02SSara Sharon 		if (ret) {
251363dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
251463dd5d02SSara Sharon 			goto out_unbind;
251563dd5d02SSara Sharon 		}
251663dd5d02SSara Sharon 	}
251726d6c16bSSara Sharon 
2518e705c121SKalle Valo 	/* must be set before quota calculations */
2519e705c121SKalle Valo 	mvmvif->ap_ibss_active = true;
2520e705c121SKalle Valo 
2521c56e00a3SJohannes Berg 	/* send all the early keys to the device now */
2522c56e00a3SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2523c56e00a3SJohannes Berg 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2524c56e00a3SJohannes Berg 
2525c56e00a3SJohannes Berg 		if (!key)
2526c56e00a3SJohannes Berg 			continue;
2527c56e00a3SJohannes Berg 
2528c56e00a3SJohannes Berg 		mvmvif->ap_early_keys[i] = NULL;
2529c56e00a3SJohannes Berg 
25306569e7d3SJohannes Berg 		ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2531c56e00a3SJohannes Berg 		if (ret)
2532c56e00a3SJohannes Berg 			goto out_quota_failed;
2533c56e00a3SJohannes Berg 	}
2534c56e00a3SJohannes Berg 
253547242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
253647242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, true,
253747242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
253847242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
253947242744STova Mussai 	}
254047242744STova Mussai 
2541e705c121SKalle Valo 	/* power updated needs to be done before quotas */
2542e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2543e705c121SKalle Valo 
2544e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2545e705c121SKalle Valo 	if (ret)
2546e705c121SKalle Valo 		goto out_quota_failed;
2547e705c121SKalle Valo 
2548e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2549e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2550e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2551e705c121SKalle Valo 
2552e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2553e705c121SKalle Valo 
2554e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2555e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2556e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2557e705c121SKalle Valo 
2558b73f9a4aSJohannes Berg 	iwl_mvm_ftm_restart_responder(mvm, vif);
2559b73f9a4aSJohannes Berg 
2560e705c121SKalle Valo 	goto out_unlock;
2561e705c121SKalle Valo 
2562e705c121SKalle Valo out_quota_failed:
2563e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2564e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2565e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2566ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2567e705c121SKalle Valo out_unbind:
2568e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2569e705c121SKalle Valo out_remove:
2570e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2571e705c121SKalle Valo out_unlock:
2572e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2573e705c121SKalle Valo 	return ret;
2574e705c121SKalle Valo }
2575e705c121SKalle Valo 
2576e705c121SKalle Valo static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2577e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2578e705c121SKalle Valo {
2579e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2580e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2581e705c121SKalle Valo 
2582e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
2583e705c121SKalle Valo 
2584e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2585e705c121SKalle Valo 
2586e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2587e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2588e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2589e705c121SKalle Valo 					  &mvmvif->time_event_data);
2590e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2591e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2592e705c121SKalle Valo 	}
2593e705c121SKalle Valo 
2594e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2595e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2596e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2597e705c121SKalle Valo 	}
2598e705c121SKalle Valo 
2599e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2600e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2601e705c121SKalle Valo 
260247242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
260347242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
260447242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
260547242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
260647242744STova Mussai 	}
260747242744STova Mussai 
2608e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2609e705c121SKalle Valo 
2610e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2611e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2612e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2613e705c121SKalle Valo 
2614e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2615ced19f26SSara Sharon 
2616be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
2617be82ecd3SAvraham Stern 
2618ced19f26SSara Sharon 	/*
2619ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2620ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2621ced19f26SSara Sharon 	 * cause assert 0x2b00.
2622ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2623ced19f26SSara Sharon 	 * bcast station).
2624ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2625ced19f26SSara Sharon 	 */
2626ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
262726d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2628e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2629ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2630ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2631e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2632e705c121SKalle Valo 
2633e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2634e705c121SKalle Valo 
2635e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2636e705c121SKalle Valo 
2637e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2638e705c121SKalle Valo }
2639e705c121SKalle Valo 
2640e705c121SKalle Valo static void
2641e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2642e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2643e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
2644e705c121SKalle Valo 				 u32 changes)
2645e705c121SKalle Valo {
2646e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2647e705c121SKalle Valo 
2648e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
2649e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
2650e705c121SKalle Valo 		return;
2651e705c121SKalle Valo 
2652e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2653e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2654e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2655e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2656e705c121SKalle Valo 
2657e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
2658e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
2659e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2660e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
2661e705c121SKalle Valo 
2662b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
2663b73f9a4aSJohannes Berg 		int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2664b73f9a4aSJohannes Berg 
2665b73f9a4aSJohannes Berg 		if (ret)
2666b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2667b73f9a4aSJohannes Berg 				 ret);
2668b73f9a4aSJohannes Berg 	}
2669b73f9a4aSJohannes Berg 
2670e705c121SKalle Valo }
2671e705c121SKalle Valo 
2672e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2673e705c121SKalle Valo 				     struct ieee80211_vif *vif,
2674e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
2675e705c121SKalle Valo 				     u32 changes)
2676e705c121SKalle Valo {
2677e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2678e705c121SKalle Valo 
2679e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2680e705c121SKalle Valo 
2681e705c121SKalle Valo 	if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2682e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2683e705c121SKalle Valo 
2684e705c121SKalle Valo 	switch (vif->type) {
2685e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2686e705c121SKalle Valo 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2687e705c121SKalle Valo 		break;
2688e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
2689e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
2690e705c121SKalle Valo 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2691e705c121SKalle Valo 		break;
269291b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
269391b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
269491b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
269591b08c2dSAviya Erenfeld 		break;
2696e705c121SKalle Valo 	default:
2697e705c121SKalle Valo 		/* shouldn't happen */
2698e705c121SKalle Valo 		WARN_ON_ONCE(1);
2699e705c121SKalle Valo 	}
2700e705c121SKalle Valo 
27019aae43a4SJohannes Berg 	if (changes & BSS_CHANGED_TXPOWER) {
27029aae43a4SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
27039aae43a4SJohannes Berg 				bss_conf->txpower);
27049aae43a4SJohannes Berg 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
27059aae43a4SJohannes Berg 	}
27069aae43a4SJohannes Berg 
2707e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2708e705c121SKalle Valo }
2709e705c121SKalle Valo 
2710e705c121SKalle Valo static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2711e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2712e705c121SKalle Valo 			       struct ieee80211_scan_request *hw_req)
2713e705c121SKalle Valo {
2714e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2715e705c121SKalle Valo 	int ret;
2716e705c121SKalle Valo 
2717e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
2718e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2719e705c121SKalle Valo 		return -EINVAL;
2720e705c121SKalle Valo 
2721e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2722e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2723e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2724e705c121SKalle Valo 
2725e705c121SKalle Valo 	return ret;
2726e705c121SKalle Valo }
2727e705c121SKalle Valo 
2728e705c121SKalle Valo static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2729e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2730e705c121SKalle Valo {
2731e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2732e705c121SKalle Valo 
2733e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2734e705c121SKalle Valo 
2735e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2736e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
2737e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
2738e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
2739e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
2740e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
2741e705c121SKalle Valo 	*/
2742e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2743e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2744e705c121SKalle Valo 
2745e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2746e705c121SKalle Valo }
2747e705c121SKalle Valo 
2748e705c121SKalle Valo static void
2749e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2750e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
2751e705c121SKalle Valo 				  int num_frames,
2752e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
2753e705c121SKalle Valo 				  bool more_data)
2754e705c121SKalle Valo {
2755e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2756e705c121SKalle Valo 
2757e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
2758e705c121SKalle Valo 
2759e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2760e705c121SKalle Valo 					  tids, more_data, false);
2761e705c121SKalle Valo }
2762e705c121SKalle Valo 
2763e705c121SKalle Valo static void
2764e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2765e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
2766e705c121SKalle Valo 				    int num_frames,
2767e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
2768e705c121SKalle Valo 				    bool more_data)
2769e705c121SKalle Valo {
2770e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2771e705c121SKalle Valo 
27729a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2773e705c121SKalle Valo 
2774e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2775e705c121SKalle Valo 					  tids, more_data, true);
2776e705c121SKalle Valo }
2777e705c121SKalle Valo 
277865e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2779e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
2780e705c121SKalle Valo 				     struct ieee80211_sta *sta)
2781e705c121SKalle Valo {
2782e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2783e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2784e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
2785e705c121SKalle Valo 	int tid;
2786e705c121SKalle Valo 
2787960f864bSJohannes Berg 	/*
2788960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
2789960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
2790960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
2791960f864bSJohannes Berg 	 */
2792960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2793960f864bSJohannes Berg 		return;
2794960f864bSJohannes Berg 
2795e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2796311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
2797e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2798e705c121SKalle Valo 
27996862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
28001c17627bSSara Sharon 			continue;
28011c17627bSSara Sharon 
2802e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
2803e705c121SKalle Valo 
2804dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
2805e705c121SKalle Valo 			continue;
2806e705c121SKalle Valo 
2807e705c121SKalle Valo 		__set_bit(tid, &tids);
2808e705c121SKalle Valo 	}
2809e705c121SKalle Valo 
2810e705c121SKalle Valo 	switch (cmd) {
2811e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
2812e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
2813e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
2814e705c121SKalle Valo 
2815e705c121SKalle Valo 		if (txqs)
2816e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
2817e705c121SKalle Valo 		/*
2818e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
2819e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
2820e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2821e705c121SKalle Valo 		 */
2822e705c121SKalle Valo 		break;
2823e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
28240ae98812SSara Sharon 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
2825e705c121SKalle Valo 			break;
2826e705c121SKalle Valo 
2827e705c121SKalle Valo 		if (txqs)
2828e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
2829e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
2830e705c121SKalle Valo 		break;
2831e705c121SKalle Valo 	default:
2832e705c121SKalle Valo 		break;
2833e705c121SKalle Valo 	}
2834e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2835e705c121SKalle Valo }
2836e705c121SKalle Valo 
283765e25482SJohannes Berg static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
283865e25482SJohannes Berg 				   struct ieee80211_vif *vif,
283965e25482SJohannes Berg 				   enum sta_notify_cmd cmd,
284065e25482SJohannes Berg 				   struct ieee80211_sta *sta)
284165e25482SJohannes Berg {
284265e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
284365e25482SJohannes Berg }
284465e25482SJohannes Berg 
284565e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
284665e25482SJohannes Berg {
284765e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
284865e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
284965e25482SJohannes Berg 	struct ieee80211_sta *sta;
285065e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
285165e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
285265e25482SJohannes Berg 
2853be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
285465e25482SJohannes Berg 		return;
285565e25482SJohannes Berg 
285665e25482SJohannes Berg 	rcu_read_lock();
2857a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
285865e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
285965e25482SJohannes Berg 		rcu_read_unlock();
286065e25482SJohannes Berg 		return;
286165e25482SJohannes Berg 	}
286265e25482SJohannes Berg 
286365e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
286465e25482SJohannes Berg 
286565e25482SJohannes Berg 	if (!mvmsta->vif ||
286665e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
286765e25482SJohannes Berg 		rcu_read_unlock();
286865e25482SJohannes Berg 		return;
286965e25482SJohannes Berg 	}
287065e25482SJohannes Berg 
287165e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
287265e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
287365e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
287465e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
287565e25482SJohannes Berg 			sta);
287665e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
287765e25482SJohannes Berg 	}
287865e25482SJohannes Berg 
287965e25482SJohannes Berg 	if (sleeping) {
288065e25482SJohannes Berg 		switch (notif->type) {
288165e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
288265e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
288365e25482SJohannes Berg 			break;
288465e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
288565e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
288665e25482SJohannes Berg 			break;
288765e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
288865e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
288965e25482SJohannes Berg 			break;
289065e25482SJohannes Berg 		default:
289165e25482SJohannes Berg 			break;
289265e25482SJohannes Berg 		}
289365e25482SJohannes Berg 	}
289465e25482SJohannes Berg 
289565e25482SJohannes Berg 	rcu_read_unlock();
289665e25482SJohannes Berg }
289765e25482SJohannes Berg 
2898e705c121SKalle Valo static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2899e705c121SKalle Valo 				       struct ieee80211_vif *vif,
2900e705c121SKalle Valo 				       struct ieee80211_sta *sta)
2901e705c121SKalle Valo {
2902e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2903e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2904e705c121SKalle Valo 
2905e705c121SKalle Valo 	/*
2906e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
2907e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
2908e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
2909e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
2910e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
2911e705c121SKalle Valo 	 * callback deleted the station.
2912e705c121SKalle Valo 	 */
2913e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2914e705c121SKalle Valo 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2915e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2916e705c121SKalle Valo 				   ERR_PTR(-ENOENT));
2917e705c121SKalle Valo 
2918e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2919e705c121SKalle Valo }
2920e705c121SKalle Valo 
2921e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2922e705c121SKalle Valo 				const u8 *bssid)
2923e705c121SKalle Valo {
2924b0ffe455SJohannes Berg 	int i;
2925b0ffe455SJohannes Berg 
2926b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2927b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
2928b0ffe455SJohannes Berg 
2929b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
2930b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
2931b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
2932b0ffe455SJohannes Berg 	}
2933b0ffe455SJohannes Berg 
2934e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2935e705c121SKalle Valo 		return;
2936e705c121SKalle Valo 
2937c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
2938cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2939cee5a882SAvri Altman 		return;
2940cee5a882SAvri Altman 	}
2941cee5a882SAvri Altman 
294211dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
294311dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
2944e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2945e705c121SKalle Valo 		return;
2946e705c121SKalle Valo 	}
2947e705c121SKalle Valo 
2948b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
2949b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
2950b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2951b0ffe455SJohannes Berg 			return;
2952b0ffe455SJohannes Berg 		}
2953b0ffe455SJohannes Berg 	}
2954b0ffe455SJohannes Berg 
2955e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2956e705c121SKalle Valo }
2957e705c121SKalle Valo 
29581e8f1329SGolan Ben-Ami static void
29591e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
29601e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
29611e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
29621e8f1329SGolan Ben-Ami {
29631e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
29641e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
29651e8f1329SGolan Ben-Ami 
29666c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
29676c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
29686c042d75SSara Sharon 	if (!trig)
29691e8f1329SGolan Ben-Ami 		return;
29701e8f1329SGolan Ben-Ami 
29711e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
29721e8f1329SGolan Ben-Ami 
29731e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
29741e8f1329SGolan Ben-Ami 		return;
29751e8f1329SGolan Ben-Ami 
29761e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
29771e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
29781e8f1329SGolan Ben-Ami 		return;
29791e8f1329SGolan Ben-Ami 
29807174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
29811e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
29821e8f1329SGolan Ben-Ami 				peer_addr, action);
29831e8f1329SGolan Ben-Ami }
29841e8f1329SGolan Ben-Ami 
29854f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
29864f58121dSIlan Peer 	bool tolerated;
29874f58121dSIlan Peer };
29884f58121dSIlan Peer 
29894f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
29904f58121dSIlan Peer 						    struct cfg80211_bss *bss,
29914f58121dSIlan Peer 						    void *_data)
29924f58121dSIlan Peer {
29934f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
29946c608cd6SJohannes Berg 	const struct cfg80211_bss_ies *ies;
29954f58121dSIlan Peer 	const struct element *elem;
29964f58121dSIlan Peer 
29976c608cd6SJohannes Berg 	rcu_read_lock();
29986c608cd6SJohannes Berg 	ies = rcu_dereference(bss->ies);
29996c608cd6SJohannes Berg 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
30006c608cd6SJohannes Berg 				  ies->len);
30014f58121dSIlan Peer 
30024f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
30034f58121dSIlan Peer 	    !(elem->data[10] &
30044f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
30054f58121dSIlan Peer 		data->tolerated = false;
30064f58121dSIlan Peer 	}
30076c608cd6SJohannes Berg 	rcu_read_unlock();
30084f58121dSIlan Peer }
30094f58121dSIlan Peer 
30104f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
30114f58121dSIlan Peer 					       struct ieee80211_vif *vif)
30124f58121dSIlan Peer {
30134f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
30144f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
30154f58121dSIlan Peer 		.tolerated = true,
30164f58121dSIlan Peer 	};
30174f58121dSIlan Peer 
30184f58121dSIlan Peer 	if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
30194f58121dSIlan Peer 		mvmvif->he_ru_2mhz_block = false;
30204f58121dSIlan Peer 		return;
30214f58121dSIlan Peer 	}
30224f58121dSIlan Peer 
30234f58121dSIlan Peer 	cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
30244f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
30254f58121dSIlan Peer 			  &iter_data);
30264f58121dSIlan Peer 
30274f58121dSIlan Peer 	/*
30284f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
30294f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
30304f58121dSIlan Peer 	 */
30314f58121dSIlan Peer 	mvmvif->he_ru_2mhz_block = !iter_data.tolerated;
30324f58121dSIlan Peer }
30334f58121dSIlan Peer 
3034f7d6ef33SJohannes Berg static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3035f7d6ef33SJohannes Berg 					       struct ieee80211_vif *vif)
3036f7d6ef33SJohannes Berg {
3037f7d6ef33SJohannes Berg 	struct ieee80211_supported_band *sband;
3038f7d6ef33SJohannes Berg 	const struct ieee80211_sta_he_cap *he_cap;
3039f7d6ef33SJohannes Berg 
3040f7d6ef33SJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION)
3041f7d6ef33SJohannes Berg 		return;
3042f7d6ef33SJohannes Berg 
3043f7d6ef33SJohannes Berg 	if (!mvm->cca_40mhz_workaround)
3044f7d6ef33SJohannes Berg 		return;
3045f7d6ef33SJohannes Berg 
3046f7d6ef33SJohannes Berg 	/* decrement and check that we reached zero */
3047f7d6ef33SJohannes Berg 	mvm->cca_40mhz_workaround--;
3048f7d6ef33SJohannes Berg 	if (mvm->cca_40mhz_workaround)
3049f7d6ef33SJohannes Berg 		return;
3050f7d6ef33SJohannes Berg 
3051f7d6ef33SJohannes Berg 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3052f7d6ef33SJohannes Berg 
3053f7d6ef33SJohannes Berg 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3054f7d6ef33SJohannes Berg 
3055f7d6ef33SJohannes Berg 	he_cap = ieee80211_get_he_iftype_cap(sband,
3056f7d6ef33SJohannes Berg 					     ieee80211_vif_type_p2p(vif));
3057f7d6ef33SJohannes Berg 
3058f7d6ef33SJohannes Berg 	if (he_cap) {
3059f7d6ef33SJohannes Berg 		/* we know that ours is writable */
3060f7d6ef33SJohannes Berg 		struct ieee80211_sta_he_cap *he = (void *)he_cap;
3061f7d6ef33SJohannes Berg 
3062f7d6ef33SJohannes Berg 		he->he_cap_elem.phy_cap_info[0] |=
3063f7d6ef33SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3064f7d6ef33SJohannes Berg 	}
3065f7d6ef33SJohannes Berg }
3066f7d6ef33SJohannes Berg 
3067e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3068e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3069e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3070e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3071e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3072e705c121SKalle Valo {
3073e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3074e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
30756ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3076e705c121SKalle Valo 	int ret;
3077e705c121SKalle Valo 
3078e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3079e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3080e705c121SKalle Valo 
3081e705c121SKalle Valo 	/* this would be a mac80211 bug ... but don't crash */
3082e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
3083fe56d05eSSara Sharon 		return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) ? 0 : -EINVAL;
3084e705c121SKalle Valo 
308524afba76SLiad Kaufman 	/*
308624afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
308724afba76SLiad Kaufman 	 *
308824afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
308924afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
309024afba76SLiad Kaufman 	 * the queues.
309124afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
309224afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
309324afba76SLiad Kaufman 	 * of.
309424afba76SLiad Kaufman 	 *
309524afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
309624afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
309724afba76SLiad Kaufman 	 */
309824afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3099c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
310024afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
310124afba76SLiad Kaufman 
310224afba76SLiad Kaufman 		/*
310324afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
310424afba76SLiad Kaufman 		 * worker will already remove it if it was on
310524afba76SLiad Kaufman 		 */
3106f7d6ef33SJohannes Berg 
3107f7d6ef33SJohannes Berg 		/*
3108f7d6ef33SJohannes Berg 		 * Additionally, reset the 40 MHz capability if we disconnected
3109f7d6ef33SJohannes Berg 		 * from the AP now.
3110f7d6ef33SJohannes Berg 		 */
3111f7d6ef33SJohannes Berg 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
311224afba76SLiad Kaufman 	}
311324afba76SLiad Kaufman 
3114e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
31156ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3116d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
31176ea29ce5SJohannes Berg 
3118e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3119e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
3120e705c121SKalle Valo 		/*
3121e705c121SKalle Valo 		 * Firmware bug - it'll crash if the beacon interval is less
3122e705c121SKalle Valo 		 * than 16. We can't avoid connecting at all, so refuse the
3123e705c121SKalle Valo 		 * station state change, this will cause mac80211 to abandon
3124e705c121SKalle Valo 		 * attempts to connect to this AP, and eventually wpa_s will
3125cdaba917SEmmanuel Grumbach 		 * blocklist the AP...
3126e705c121SKalle Valo 		 */
3127e705c121SKalle Valo 		if (vif->type == NL80211_IFTYPE_STATION &&
3128e705c121SKalle Valo 		    vif->bss_conf.beacon_int < 16) {
3129e705c121SKalle Valo 			IWL_ERR(mvm,
3130e705c121SKalle Valo 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3131e705c121SKalle Valo 				sta->addr, vif->bss_conf.beacon_int);
3132e705c121SKalle Valo 			ret = -EINVAL;
3133e705c121SKalle Valo 			goto out_unlock;
3134e705c121SKalle Valo 		}
3135e705c121SKalle Valo 
313697cc1694SAvraham Stern 		if (vif->type == NL80211_IFTYPE_STATION)
313797cc1694SAvraham Stern 			vif->bss_conf.he_support = sta->he_cap.has_he;
313897cc1694SAvraham Stern 
3139e705c121SKalle Valo 		if (sta->tdls &&
3140e705c121SKalle Valo 		    (vif->p2p ||
3141e705c121SKalle Valo 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
3142e705c121SKalle Valo 						IWL_MVM_TDLS_STA_COUNT ||
3143e705c121SKalle Valo 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3144e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3145e705c121SKalle Valo 			ret = -EBUSY;
3146e705c121SKalle Valo 			goto out_unlock;
3147e705c121SKalle Valo 		}
3148e705c121SKalle Valo 
3149e705c121SKalle Valo 		ret = iwl_mvm_add_sta(mvm, vif, sta);
31501e8f1329SGolan Ben-Ami 		if (sta->tdls && ret == 0) {
3151e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
31521e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
31531e8f1329SGolan Ben-Ami 						   NL80211_TDLS_SETUP);
31541e8f1329SGolan Ben-Ami 		}
3155438af969SSara Sharon 
3156438af969SSara Sharon 		sta->max_rc_amsdu_len = 1;
3157e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3158e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3159e705c121SKalle Valo 		/*
3160e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3161e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3162e705c121SKalle Valo 		 */
3163e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3164e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3165e705c121SKalle Valo 		ret = 0;
3166e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3167e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
31688be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
31699394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
31708be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count++;
31718be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
317202221a81SShaul Triebitz 			if (vif->bss_conf.he_support &&
317302221a81SShaul Triebitz 			    !iwlwifi_mod_params.disable_11ax)
317402221a81SShaul Triebitz 				iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
31759394662aSLiad Kaufman 		} else if (vif->type == NL80211_IFTYPE_STATION) {
31769394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
31774f58121dSIlan Peer 
31784f58121dSIlan Peer 			mvmvif->he_ru_2mhz_block = false;
31794f58121dSIlan Peer 			if (sta->he_cap.has_he)
31804f58121dSIlan Peer 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif);
31814f58121dSIlan Peer 
31829394662aSLiad Kaufman 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
31838be30c13SAyala Beker 		}
3184735a0045SGoodstein, Mordechay 
31853baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
31863baf7528SAvraham Stern 				     false);
3187735a0045SGoodstein, Mordechay 		ret = iwl_mvm_update_sta(mvm, vif, sta);
3188e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3189e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
319028916a16SEmmanuel Grumbach 		ret = 0;
3191e705c121SKalle Valo 
3192e705c121SKalle Valo 		/* we don't support TDLS during DCM */
3193e705c121SKalle Valo 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
3194e705c121SKalle Valo 			iwl_mvm_teardown_tdls_peers(mvm);
3195e705c121SKalle Valo 
31961e8f1329SGolan Ben-Ami 		if (sta->tdls)
31971e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
31981e8f1329SGolan Ben-Ami 						   NL80211_TDLS_ENABLE_LINK);
31991e8f1329SGolan Ben-Ami 
3200e705c121SKalle Valo 		/* enable beacon filtering */
3201e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
32026b7a5aeaSNaftali Goldstein 
320350f56044SIlan Peer 		/*
320450f56044SIlan Peer 		 * Now that the station is authorized, i.e., keys were already
320550f56044SIlan Peer 		 * installed, need to indicate to the FW that
320650f56044SIlan Peer 		 * multicast data frames can be forwarded to the driver
320750f56044SIlan Peer 		 */
320850f56044SIlan Peer 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
320950f56044SIlan Peer 
32103baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
32113baf7528SAvraham Stern 				     true);
3212e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3213e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
321450f56044SIlan Peer 		/* Multicast data frames are no longer allowed */
321550f56044SIlan Peer 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
321650f56044SIlan Peer 
3217e705c121SKalle Valo 		/* disable beacon filtering */
321869e508b4SIlan Peer 		ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
321969e508b4SIlan Peer 		WARN_ON(ret &&
322069e508b4SIlan Peer 			!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
322169e508b4SIlan Peer 				  &mvm->status));
3222e705c121SKalle Valo 		ret = 0;
3223e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3224e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
32258be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
32268be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
32278be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
32288be30c13SAyala Beker 		}
3229e705c121SKalle Valo 		ret = 0;
3230e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3231e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3232e705c121SKalle Valo 		ret = 0;
3233e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3234e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
3235e705c121SKalle Valo 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
32361e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3237e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
32381e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
32391e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
32401e8f1329SGolan Ben-Ami 		}
324134a880d8SLiad Kaufman 
324244135b7cSIlan Peer 		if (unlikely(ret &&
324344135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
324444135b7cSIlan Peer 				      &mvm->status)))
324544135b7cSIlan Peer 			ret = 0;
3246e705c121SKalle Valo 	} else {
3247e705c121SKalle Valo 		ret = -EIO;
3248e705c121SKalle Valo 	}
3249e705c121SKalle Valo  out_unlock:
3250e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3251e705c121SKalle Valo 
3252e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3253e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3254e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3255e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3256e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3257e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3258e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3259e705c121SKalle Valo 	}
3260e705c121SKalle Valo 
3261e705c121SKalle Valo 	return ret;
3262e705c121SKalle Valo }
3263e705c121SKalle Valo 
3264e705c121SKalle Valo static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3265e705c121SKalle Valo {
3266e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3267e705c121SKalle Valo 
3268e705c121SKalle Valo 	mvm->rts_threshold = value;
3269e705c121SKalle Valo 
3270e705c121SKalle Valo 	return 0;
3271e705c121SKalle Valo }
3272e705c121SKalle Valo 
3273e705c121SKalle Valo static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3274e705c121SKalle Valo 				  struct ieee80211_vif *vif,
3275e705c121SKalle Valo 				  struct ieee80211_sta *sta, u32 changed)
3276e705c121SKalle Valo {
3277e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3278dcfe3b10SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3279dcfe3b10SJohannes Berg 
3280dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
3281dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3282dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
3283dcfe3b10SJohannes Berg 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3284dcfe3b10SJohannes Berg 				     true);
3285e705c121SKalle Valo 
3286e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
3287e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
3288e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
3289e705c121SKalle Valo }
3290e705c121SKalle Valo 
3291e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3292e705c121SKalle Valo 			       struct ieee80211_vif *vif, u16 ac,
3293e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
3294e705c121SKalle Valo {
3295e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3296e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3297e705c121SKalle Valo 
3298e705c121SKalle Valo 	mvmvif->queue_params[ac] = *params;
3299e705c121SKalle Valo 
3300e705c121SKalle Valo 	/*
3301e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
3302e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
3303e705c121SKalle Valo 	 */
3304e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3305e705c121SKalle Valo 		int ret;
3306e705c121SKalle Valo 
3307e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
3308e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3309e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3310e705c121SKalle Valo 		return ret;
3311e705c121SKalle Valo 	}
3312e705c121SKalle Valo 	return 0;
3313e705c121SKalle Valo }
3314e705c121SKalle Valo 
3315e705c121SKalle Valo static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3316d4e36e55SIlan Peer 				       struct ieee80211_vif *vif,
331715fae341SJohannes Berg 				       struct ieee80211_prep_tx_info *info)
3318e705c121SKalle Valo {
3319e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
33207c70fee5SSara Sharon 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
33217c70fee5SSara Sharon 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
3322e705c121SKalle Valo 
332315fae341SJohannes Berg 	if (info->duration > duration)
332415fae341SJohannes Berg 		duration = info->duration;
3325d4e36e55SIlan Peer 
3326e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3327fe959c7bSEmmanuel Grumbach 	/* Try really hard to protect the session and hear a beacon
3328fe959c7bSEmmanuel Grumbach 	 * The new session protection command allows us to protect the
3329fe959c7bSEmmanuel Grumbach 	 * session for a much longer time since the firmware will internally
3330fe959c7bSEmmanuel Grumbach 	 * create two events: a 300TU one with a very high priority that
3331fe959c7bSEmmanuel Grumbach 	 * won't be fragmented which should be enough for 99% of the cases,
3332fe959c7bSEmmanuel Grumbach 	 * and another one (which we configure here to be 900TU long) which
3333fe959c7bSEmmanuel Grumbach 	 * will have a slightly lower priority, but more importantly, can be
3334fe959c7bSEmmanuel Grumbach 	 * fragmented so that it'll allow other activities to run.
3335fe959c7bSEmmanuel Grumbach 	 */
3336fe959c7bSEmmanuel Grumbach 	if (fw_has_capa(&mvm->fw->ucode_capa,
3337fe959c7bSEmmanuel Grumbach 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
3338fe959c7bSEmmanuel Grumbach 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
3339b5b878e3SEmmanuel Grumbach 						    min_duration, false);
3340fe959c7bSEmmanuel Grumbach 	else
3341fe959c7bSEmmanuel Grumbach 		iwl_mvm_protect_session(mvm, vif, duration,
3342fe959c7bSEmmanuel Grumbach 					min_duration, 500, false);
3343e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3344e705c121SKalle Valo }
3345e705c121SKalle Valo 
3346e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3347e705c121SKalle Valo 					struct ieee80211_vif *vif,
3348e705c121SKalle Valo 					struct cfg80211_sched_scan_request *req,
3349e705c121SKalle Valo 					struct ieee80211_scan_ies *ies)
3350e705c121SKalle Valo {
3351e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3352e705c121SKalle Valo 
3353e705c121SKalle Valo 	int ret;
3354e705c121SKalle Valo 
3355e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3356e705c121SKalle Valo 
3357e705c121SKalle Valo 	if (!vif->bss_conf.idle) {
3358e705c121SKalle Valo 		ret = -EBUSY;
3359e705c121SKalle Valo 		goto out;
3360e705c121SKalle Valo 	}
3361e705c121SKalle Valo 
3362e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
3363e705c121SKalle Valo 
3364e705c121SKalle Valo out:
3365e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3366e705c121SKalle Valo 	return ret;
3367e705c121SKalle Valo }
3368e705c121SKalle Valo 
3369e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3370e705c121SKalle Valo 				       struct ieee80211_vif *vif)
3371e705c121SKalle Valo {
3372e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3373e705c121SKalle Valo 	int ret;
3374e705c121SKalle Valo 
3375e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3376e705c121SKalle Valo 
3377e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3378e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
3379e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
3380e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
3381e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3382e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
3383e705c121SKalle Valo 	 * not running.
3384e705c121SKalle Valo 	*/
3385e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
3386e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3387e705c121SKalle Valo 		return 0;
3388e705c121SKalle Valo 	}
3389e705c121SKalle Valo 
3390e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
3391e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3392e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
3393e705c121SKalle Valo 
3394e705c121SKalle Valo 	return ret;
3395e705c121SKalle Valo }
3396e705c121SKalle Valo 
33976569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3398e705c121SKalle Valo 				 enum set_key_cmd cmd,
3399e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3400e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3401e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
3402e705c121SKalle Valo {
3403c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3404e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3405f5e28eacSJohannes Berg 	struct iwl_mvm_sta *mvmsta;
3406f5e28eacSJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn;
3407f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
3408c56e00a3SJohannes Berg 	int ret, i;
34094615fd15SEmmanuel Grumbach 	u8 key_offset;
3410e705c121SKalle Valo 
3411e705c121SKalle Valo 	switch (key->cipher) {
3412e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3413286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
3414e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
34151ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
34167f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
34177f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
34187f768ad5SDavid Spinadel 		} else {
34197f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
34207f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
34217f768ad5SDavid Spinadel 		}
3422e705c121SKalle Valo 		break;
3423e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
34242a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
34252a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
342685aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
3427e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3428e705c121SKalle Valo 		break;
3429e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
34308e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
34318e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3432e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
3433e705c121SKalle Valo 		break;
3434e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3435e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3436475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
3437e705c121SKalle Valo 			break;
3438475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
3439475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
3440475c6bdeSJohannes Berg 		/* support HW crypto on TX */
3441475c6bdeSJohannes Berg 		return 0;
3442e705c121SKalle Valo 	default:
3443e705c121SKalle Valo 		/* currently FW supports only one optional cipher scheme */
3444e705c121SKalle Valo 		if (hw->n_cipher_schemes &&
3445e705c121SKalle Valo 		    hw->cipher_schemes->cipher == key->cipher)
3446e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3447e705c121SKalle Valo 		else
3448e705c121SKalle Valo 			return -EOPNOTSUPP;
3449e705c121SKalle Valo 	}
3450e705c121SKalle Valo 
3451e705c121SKalle Valo 	switch (cmd) {
3452e705c121SKalle Valo 	case SET_KEY:
3453b1fdc250SJohannes Berg 		if (keyidx == 6 || keyidx == 7)
3454b1fdc250SJohannes Berg 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
3455b1fdc250SJohannes Berg 					   key);
3456b1fdc250SJohannes Berg 
3457e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
3458e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
3459e705c121SKalle Valo 			/*
3460e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
3461e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
3462e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
34638e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
3464e705c121SKalle Valo 			 */
34658e160ab8SAyala Beker 			if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
34668e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3467a1c2ff30SAndrei Otcheretianski 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
346881279c49SJohannes Berg 				ret = -EOPNOTSUPP;
3469a1c2ff30SAndrei Otcheretianski 				break;
3470a1c2ff30SAndrei Otcheretianski 			}
347185aeb58cSDavid Spinadel 
347285aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
347385aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
347485aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
3475e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
3476a1c2ff30SAndrei Otcheretianski 				ret = 0;
3477e705c121SKalle Valo 				break;
3478e705c121SKalle Valo 			}
3479c56e00a3SJohannes Berg 
3480c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
3481c56e00a3SJohannes Berg 				for (i = 0;
3482c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
3483c56e00a3SJohannes Berg 				     i++) {
3484c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
3485c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
3486c56e00a3SJohannes Berg 						break;
3487c56e00a3SJohannes Berg 					}
3488c56e00a3SJohannes Berg 				}
3489c56e00a3SJohannes Berg 
3490c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
3491c56e00a3SJohannes Berg 					ret = -ENOSPC;
3492a1c2ff30SAndrei Otcheretianski 				else
3493a1c2ff30SAndrei Otcheretianski 					ret = 0;
3494c56e00a3SJohannes Berg 
3495c56e00a3SJohannes Berg 				break;
3496c56e00a3SJohannes Berg 			}
349785aeb58cSDavid Spinadel 		}
3498e705c121SKalle Valo 
3499e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
3500e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
3501e705c121SKalle Valo 		 */
3502e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3503e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
3504e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
3505e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
3506e705c121SKalle Valo 			ret = 0;
3507e705c121SKalle Valo 			break;
3508e705c121SKalle Valo 		}
3509e705c121SKalle Valo 
3510f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3511f5e28eacSJohannes Berg 		    sta && iwl_mvm_has_new_rx_api(mvm) &&
3512f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3513f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
35142a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
35152a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3516f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
3517f5e28eacSJohannes Berg 			int tid, q;
3518f5e28eacSJohannes Berg 
3519f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3520f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3521acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
3522acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
3523f5e28eacSJohannes Berg 					 GFP_KERNEL);
3524f5e28eacSJohannes Berg 			if (!ptk_pn) {
3525f5e28eacSJohannes Berg 				ret = -ENOMEM;
3526f5e28eacSJohannes Berg 				break;
3527f5e28eacSJohannes Berg 			}
3528f5e28eacSJohannes Berg 
3529f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3530f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
3531f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
3532f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
3533f5e28eacSJohannes Berg 					       seq.ccmp.pn,
3534f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
3535f5e28eacSJohannes Berg 			}
3536f5e28eacSJohannes Berg 
3537f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3538f5e28eacSJohannes Berg 		}
3539f5e28eacSJohannes Berg 
35404615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
35414615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
35424615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
35434615fd15SEmmanuel Grumbach 		else
35444615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
35454615fd15SEmmanuel Grumbach 
3546e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
35474615fd15SEmmanuel Grumbach 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
3548e705c121SKalle Valo 		if (ret) {
3549e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
3550475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
3551e705c121SKalle Valo 			/*
3552e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
3553475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
3554475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
3555475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
3556e705c121SKalle Valo 			 */
3557475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
3558475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
3559475c6bdeSJohannes Berg 			else
3560e705c121SKalle Valo 				ret = 0;
3561e705c121SKalle Valo 		}
3562e705c121SKalle Valo 
3563e705c121SKalle Valo 		break;
3564e705c121SKalle Valo 	case DISABLE_KEY:
3565b1fdc250SJohannes Berg 		if (keyidx == 6 || keyidx == 7)
3566b1fdc250SJohannes Berg 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
3567b1fdc250SJohannes Berg 					 NULL);
3568b1fdc250SJohannes Berg 
3569c56e00a3SJohannes Berg 		ret = -ENOENT;
3570c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3571c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
3572c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
3573c56e00a3SJohannes Berg 				ret = 0;
3574c56e00a3SJohannes Berg 			}
3575c56e00a3SJohannes Berg 		}
3576c56e00a3SJohannes Berg 
3577c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
3578c56e00a3SJohannes Berg 		if (ret == 0)
3579c56e00a3SJohannes Berg 			break;
3580c56e00a3SJohannes Berg 
3581e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3582e705c121SKalle Valo 			ret = 0;
3583e705c121SKalle Valo 			break;
3584e705c121SKalle Valo 		}
3585e705c121SKalle Valo 
3586f5e28eacSJohannes Berg 		if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3587f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3588f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
35892a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
35902a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3591f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3592f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
3593f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
3594f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
3595f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3596f5e28eacSJohannes Berg 			if (ptk_pn)
3597f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
3598f5e28eacSJohannes Berg 		}
3599f5e28eacSJohannes Berg 
3600e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3601e705c121SKalle Valo 		ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3602e705c121SKalle Valo 		break;
3603e705c121SKalle Valo 	default:
3604e705c121SKalle Valo 		ret = -EINVAL;
3605e705c121SKalle Valo 	}
3606e705c121SKalle Valo 
36076569e7d3SJohannes Berg 	return ret;
36086569e7d3SJohannes Berg }
36096569e7d3SJohannes Berg 
36106569e7d3SJohannes Berg static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
36116569e7d3SJohannes Berg 			       enum set_key_cmd cmd,
36126569e7d3SJohannes Berg 			       struct ieee80211_vif *vif,
36136569e7d3SJohannes Berg 			       struct ieee80211_sta *sta,
36146569e7d3SJohannes Berg 			       struct ieee80211_key_conf *key)
36156569e7d3SJohannes Berg {
36166569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
36176569e7d3SJohannes Berg 	int ret;
36186569e7d3SJohannes Berg 
36196569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
36206569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
3621e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
36226569e7d3SJohannes Berg 
3623e705c121SKalle Valo 	return ret;
3624e705c121SKalle Valo }
3625e705c121SKalle Valo 
3626e705c121SKalle Valo static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3627e705c121SKalle Valo 					struct ieee80211_vif *vif,
3628e705c121SKalle Valo 					struct ieee80211_key_conf *keyconf,
3629e705c121SKalle Valo 					struct ieee80211_sta *sta,
3630e705c121SKalle Valo 					u32 iv32, u16 *phase1key)
3631e705c121SKalle Valo {
3632e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3633e705c121SKalle Valo 
3634e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3635e705c121SKalle Valo 		return;
3636e705c121SKalle Valo 
3637e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3638e705c121SKalle Valo }
3639e705c121SKalle Valo 
3640e705c121SKalle Valo 
3641e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3642e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
3643e705c121SKalle Valo {
3644e705c121SKalle Valo 	struct iwl_mvm *mvm =
3645e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3646e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
3647e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
3648e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
3649e705c121SKalle Valo 
3650e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3651e705c121SKalle Valo 		return true;
3652e705c121SKalle Valo 
3653e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3654e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3655e705c121SKalle Valo 		return true;
3656e705c121SKalle Valo 	}
3657e705c121SKalle Valo 
3658e705c121SKalle Valo 	resp = (void *)pkt->data;
3659e705c121SKalle Valo 
3660e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
3661b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
3662e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
3663e705c121SKalle Valo 
3664e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
3665e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3666e705c121SKalle Valo 		     te_data->uid);
3667e705c121SKalle Valo 
3668e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3669e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3670e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3671e705c121SKalle Valo 
3672e705c121SKalle Valo 	return true;
3673e705c121SKalle Valo }
3674e705c121SKalle Valo 
3675dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3676dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3677dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3678dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3679dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3680e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3681e705c121SKalle Valo 				    struct ieee80211_channel *channel,
3682e705c121SKalle Valo 				    struct ieee80211_vif *vif,
3683e705c121SKalle Valo 				    int duration)
3684e705c121SKalle Valo {
3685afc1e3b4SAvraham Stern 	int res;
3686e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3687e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3688e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
3689e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
3690dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
3691dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
3692dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
3693e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
3694e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3695e705c121SKalle Valo 		.id_and_color =
3696e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3697e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3698e705c121SKalle Valo 	};
369957e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
370057e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
370157e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
370257e861d9SDavid Spinadel 
370357e861d9SDavid Spinadel 	/* Set the channel info data */
370457e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
37053717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
370657e861d9SDavid Spinadel 			      PHY_VHT_CHANNEL_MODE20,
370757e861d9SDavid Spinadel 			      0);
370857e861d9SDavid Spinadel 
370957e861d9SDavid Spinadel 	/* Set the time and duration */
3710afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
3711e705c121SKalle Valo 
3712dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
3713dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
3714dc28e12fSMatti Gottlieb 
3715dc28e12fSMatti Gottlieb 	/*
3716dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
3717dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
3718dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
3719dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
3720dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
3721dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
3722dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
3723dc28e12fSMatti Gottlieb 	 */
3724dc28e12fSMatti Gottlieb 	if (vif->bss_conf.assoc) {
3725dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3726dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
3727dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
3728dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3729dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
3730dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
3731dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
3732dc28e12fSMatti Gottlieb 		}
3733dc28e12fSMatti Gottlieb 	}
3734dc28e12fSMatti Gottlieb 
373557e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
373657e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
3737dc28e12fSMatti Gottlieb 
3738dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
3739903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
3740903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
3741903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
3742903b3f9bSEmmanuel Grumbach 		     "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3743903b3f9bSEmmanuel Grumbach 		     duration, delay, dtim_interval);
3744903b3f9bSEmmanuel Grumbach 
3745e705c121SKalle Valo 	/* Set the node address */
374657e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
3747e705c121SKalle Valo 
3748e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3749e705c121SKalle Valo 
3750e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3751e705c121SKalle Valo 
3752e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3753e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3754e705c121SKalle Valo 		return -EIO;
3755e705c121SKalle Valo 	}
3756e705c121SKalle Valo 
3757e705c121SKalle Valo 	te_data->vif = vif;
3758e705c121SKalle Valo 	te_data->duration = duration;
3759e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
3760e705c121SKalle Valo 
3761e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3762e705c121SKalle Valo 
3763e705c121SKalle Valo 	/*
3764e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
3765e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
3766e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
3767e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
3768e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
3769e705c121SKalle Valo 	 * time another notification (that the time event started)
3770e705c121SKalle Valo 	 * might already be processed unsuccessfully.
3771e705c121SKalle Valo 	 */
3772e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3773e705c121SKalle Valo 				   time_event_response,
3774e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
3775e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
3776e705c121SKalle Valo 
377757e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
3778e705c121SKalle Valo 				   &aux_roc_req);
3779e705c121SKalle Valo 
3780e705c121SKalle Valo 	if (res) {
3781e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3782e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3783e705c121SKalle Valo 		goto out_clear_te;
3784e705c121SKalle Valo 	}
3785e705c121SKalle Valo 
3786e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
3787e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3788e705c121SKalle Valo 	/* should never fail */
3789e705c121SKalle Valo 	WARN_ON_ONCE(res);
3790e705c121SKalle Valo 
3791e705c121SKalle Valo 	if (res) {
3792e705c121SKalle Valo  out_clear_te:
3793e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
3794e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
3795e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3796e705c121SKalle Valo 	}
3797e705c121SKalle Valo 
3798e705c121SKalle Valo 	return res;
3799e705c121SKalle Valo }
3800e705c121SKalle Valo 
3801e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
3802e705c121SKalle Valo 		       struct ieee80211_vif *vif,
3803e705c121SKalle Valo 		       struct ieee80211_channel *channel,
3804e705c121SKalle Valo 		       int duration,
3805e705c121SKalle Valo 		       enum ieee80211_roc_type type)
3806e705c121SKalle Valo {
3807e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3808e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3809e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
3810e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
38118835a64fSJohannes Berg 	bool band_change_removal;
3812e705c121SKalle Valo 	int ret, i;
3813e705c121SKalle Valo 
3814e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3815e705c121SKalle Valo 			   duration, type);
3816e705c121SKalle Valo 
38179834781cSJohannes Berg 	/*
38189834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
38199834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
38209834781cSJohannes Berg 	 */
3821e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
3822e705c121SKalle Valo 
3823e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3824e705c121SKalle Valo 
3825e705c121SKalle Valo 	switch (vif->type) {
3826e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3827e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
3828e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
3829e705c121SKalle Valo 			/* Use aux roc framework (HS20) */
38302c2c3647SNathan Errera 			if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
38312c2c3647SNathan Errera 						  ADD_STA, 0) >= 12) {
38322c2c3647SNathan Errera 				u32 lmac_id;
38332c2c3647SNathan Errera 
38342c2c3647SNathan Errera 				lmac_id = iwl_mvm_get_lmac_id(mvm->fw,
38352c2c3647SNathan Errera 							      channel->band);
38362c2c3647SNathan Errera 				ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
38372c2c3647SNathan Errera 				if (WARN(ret,
38382c2c3647SNathan Errera 					 "Failed to allocate aux station"))
38392c2c3647SNathan Errera 					goto out_unlock;
38402c2c3647SNathan Errera 			}
3841e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3842e705c121SKalle Valo 						       vif, duration);
3843e705c121SKalle Valo 			goto out_unlock;
3844e705c121SKalle Valo 		}
3845e705c121SKalle Valo 		IWL_ERR(mvm, "hotspot not supported\n");
3846e705c121SKalle Valo 		ret = -EINVAL;
3847e705c121SKalle Valo 		goto out_unlock;
3848e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
3849e705c121SKalle Valo 		/* handle below */
3850e705c121SKalle Valo 		break;
3851e705c121SKalle Valo 	default:
3852e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
3853e705c121SKalle Valo 		ret = -EINVAL;
3854e705c121SKalle Valo 		goto out_unlock;
3855e705c121SKalle Valo 	}
3856e705c121SKalle Valo 
3857e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
3858e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
3859e705c121SKalle Valo 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3860e705c121SKalle Valo 			continue;
3861e705c121SKalle Valo 
3862e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3863e705c121SKalle Valo 			/*
3864e705c121SKalle Valo 			 * Unbind the P2P_DEVICE from the current PHY context,
3865e705c121SKalle Valo 			 * and if the PHY context is not used remove it.
3866e705c121SKalle Valo 			 */
3867e705c121SKalle Valo 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
3868e705c121SKalle Valo 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3869e705c121SKalle Valo 				goto out_unlock;
3870e705c121SKalle Valo 
3871e705c121SKalle Valo 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3872e705c121SKalle Valo 
3873e705c121SKalle Valo 			/* Bind the P2P_DEVICE to the current PHY Context */
3874e705c121SKalle Valo 			mvmvif->phy_ctxt = phy_ctxt;
3875e705c121SKalle Valo 
3876e705c121SKalle Valo 			ret = iwl_mvm_binding_add_vif(mvm, vif);
3877e705c121SKalle Valo 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3878e705c121SKalle Valo 				goto out_unlock;
3879e705c121SKalle Valo 
3880e705c121SKalle Valo 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3881e705c121SKalle Valo 			goto schedule_time_event;
3882e705c121SKalle Valo 		}
3883e705c121SKalle Valo 	}
3884e705c121SKalle Valo 
3885e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
3886e705c121SKalle Valo 	if (channel == mvmvif->phy_ctxt->channel)
3887e705c121SKalle Valo 		goto schedule_time_event;
3888e705c121SKalle Valo 
3889e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
3890e705c121SKalle Valo 
3891e705c121SKalle Valo 	/*
38928835a64fSJohannes Berg 	 * Check if the remain-on-channel is on a different band and that
38938835a64fSJohannes Berg 	 * requires context removal, see iwl_mvm_phy_ctxt_changed(). If
38948835a64fSJohannes Berg 	 * so, we'll need to release and then re-configure here, since we
38958835a64fSJohannes Berg 	 * must not remove a PHY context that's part of a binding.
3896e705c121SKalle Valo 	 */
38978835a64fSJohannes Berg 	band_change_removal =
38988835a64fSJohannes Berg 		fw_has_capa(&mvm->fw->ucode_capa,
38998835a64fSJohannes Berg 			    IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
39008835a64fSJohannes Berg 		mvmvif->phy_ctxt->channel->band != chandef.chan->band;
39018835a64fSJohannes Berg 
39028835a64fSJohannes Berg 	if (mvmvif->phy_ctxt->ref == 1 && !band_change_removal) {
39038835a64fSJohannes Berg 		/*
39048835a64fSJohannes Berg 		 * Change the PHY context configuration as it is currently
39058835a64fSJohannes Berg 		 * referenced only by the P2P Device MAC (and we can modify it)
39068835a64fSJohannes Berg 		 */
3907e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3908e705c121SKalle Valo 					       &chandef, 1, 1);
3909e705c121SKalle Valo 		if (ret)
3910e705c121SKalle Valo 			goto out_unlock;
3911e705c121SKalle Valo 	} else {
3912e705c121SKalle Valo 		/*
39138835a64fSJohannes Berg 		 * The PHY context is shared with other MACs (or we're trying to
39148835a64fSJohannes Berg 		 * switch bands), so remove the P2P Device from the binding,
39158835a64fSJohannes Berg 		 * allocate an new PHY context and create a new binding.
3916e705c121SKalle Valo 		 */
3917e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3918e705c121SKalle Valo 		if (!phy_ctxt) {
3919e705c121SKalle Valo 			ret = -ENOSPC;
3920e705c121SKalle Valo 			goto out_unlock;
3921e705c121SKalle Valo 		}
3922e705c121SKalle Valo 
3923e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3924e705c121SKalle Valo 					       1, 1);
3925e705c121SKalle Valo 		if (ret) {
3926e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
3927e705c121SKalle Valo 			goto out_unlock;
3928e705c121SKalle Valo 		}
3929e705c121SKalle Valo 
3930e705c121SKalle Valo 		/* Unbind the P2P_DEVICE from the current PHY context */
3931e705c121SKalle Valo 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
3932e705c121SKalle Valo 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3933e705c121SKalle Valo 			goto out_unlock;
3934e705c121SKalle Valo 
3935e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3936e705c121SKalle Valo 
3937e705c121SKalle Valo 		/* Bind the P2P_DEVICE to the new allocated PHY context */
3938e705c121SKalle Valo 		mvmvif->phy_ctxt = phy_ctxt;
3939e705c121SKalle Valo 
3940e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
3941e705c121SKalle Valo 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3942e705c121SKalle Valo 			goto out_unlock;
3943e705c121SKalle Valo 
3944e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3945e705c121SKalle Valo 	}
3946e705c121SKalle Valo 
3947e705c121SKalle Valo schedule_time_event:
3948e705c121SKalle Valo 	/* Schedule the time events */
3949e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
3950e705c121SKalle Valo 
3951e705c121SKalle Valo out_unlock:
3952e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3953e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3954e705c121SKalle Valo 	return ret;
3955e705c121SKalle Valo }
3956e705c121SKalle Valo 
39575db4c4b9SEmmanuel Grumbach static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
39585db4c4b9SEmmanuel Grumbach 			      struct ieee80211_vif *vif)
3959e705c121SKalle Valo {
3960e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3961e705c121SKalle Valo 
3962e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
3963e705c121SKalle Valo 
3964e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3965fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
3966e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3967e705c121SKalle Valo 
3968e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3969e705c121SKalle Valo 	return 0;
3970e705c121SKalle Valo }
3971e705c121SKalle Valo 
3972b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
3973b73f9a4aSJohannes Berg 	bool responder;
3974b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
3975b73f9a4aSJohannes Berg };
3976b73f9a4aSJohannes Berg 
3977b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
3978b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
3979b73f9a4aSJohannes Berg {
3980b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
3981b73f9a4aSJohannes Berg 
3982b73f9a4aSJohannes Berg 	if (rcu_access_pointer(vif->chanctx_conf) == data->ctx &&
3983b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
3984b73f9a4aSJohannes Berg 		data->responder = true;
3985b73f9a4aSJohannes Berg }
3986b73f9a4aSJohannes Berg 
3987b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
3988b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
3989b73f9a4aSJohannes Berg {
3990b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
3991b73f9a4aSJohannes Berg 		.responder = false,
3992b73f9a4aSJohannes Berg 		.ctx = ctx,
3993b73f9a4aSJohannes Berg 	};
3994b73f9a4aSJohannes Berg 
3995b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
3996b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
3997b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
3998b73f9a4aSJohannes Berg 					&data);
3999b73f9a4aSJohannes Berg 	return data.responder;
4000b73f9a4aSJohannes Berg }
4001b73f9a4aSJohannes Berg 
4002e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4003e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
4004e705c121SKalle Valo {
4005e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4006e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4007b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4008b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4009e705c121SKalle Valo 	int ret;
4010e705c121SKalle Valo 
4011e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4012e705c121SKalle Valo 
4013e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4014e705c121SKalle Valo 
4015e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4016e705c121SKalle Valo 	if (!phy_ctxt) {
4017e705c121SKalle Valo 		ret = -ENOSPC;
4018e705c121SKalle Valo 		goto out;
4019e705c121SKalle Valo 	}
4020e705c121SKalle Valo 
4021b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4022e705c121SKalle Valo 				       ctx->rx_chains_static,
4023e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
4024e705c121SKalle Valo 	if (ret) {
4025e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4026e705c121SKalle Valo 		goto out;
4027e705c121SKalle Valo 	}
4028e705c121SKalle Valo 
4029e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4030e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4031e705c121SKalle Valo out:
4032e705c121SKalle Valo 	return ret;
4033e705c121SKalle Valo }
4034e705c121SKalle Valo 
4035e705c121SKalle Valo static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4036e705c121SKalle Valo 			       struct ieee80211_chanctx_conf *ctx)
4037e705c121SKalle Valo {
4038e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4039e705c121SKalle Valo 	int ret;
4040e705c121SKalle Valo 
4041e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4042e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4043e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4044e705c121SKalle Valo 
4045e705c121SKalle Valo 	return ret;
4046e705c121SKalle Valo }
4047e705c121SKalle Valo 
4048e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4049e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4050e705c121SKalle Valo {
4051e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4052e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4053e705c121SKalle Valo 
4054e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4055e705c121SKalle Valo 
4056e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4057e705c121SKalle Valo }
4058e705c121SKalle Valo 
4059e705c121SKalle Valo static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4060e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx)
4061e705c121SKalle Valo {
4062e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4063e705c121SKalle Valo 
4064e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4065e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4066e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4067e705c121SKalle Valo }
4068e705c121SKalle Valo 
4069e705c121SKalle Valo static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4070e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx,
4071e705c121SKalle Valo 				   u32 changed)
4072e705c121SKalle Valo {
4073e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4074e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4075e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4076b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4077b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4078e705c121SKalle Valo 
4079e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4080e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4081e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4082e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4083e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4084e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4085e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4086e705c121SKalle Valo 		return;
4087e705c121SKalle Valo 
4088e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
40897a20bcceSEmmanuel Grumbach 
40907a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
40917a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4092b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
40937a20bcceSEmmanuel Grumbach 			goto out_unlock;
40947a20bcceSEmmanuel Grumbach 
40957a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
40967a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4097b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
40987a20bcceSEmmanuel Grumbach 			goto out_unlock;
40997a20bcceSEmmanuel Grumbach 	}
41007a20bcceSEmmanuel Grumbach 
4101e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4102b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4103e705c121SKalle Valo 				 ctx->rx_chains_static,
4104e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
41057a20bcceSEmmanuel Grumbach 
41067a20bcceSEmmanuel Grumbach out_unlock:
4107e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4108e705c121SKalle Valo }
4109e705c121SKalle Valo 
4110e705c121SKalle Valo static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4111e705c121SKalle Valo 					struct ieee80211_vif *vif,
4112e705c121SKalle Valo 					struct ieee80211_chanctx_conf *ctx,
4113e705c121SKalle Valo 					bool switching_chanctx)
4114e705c121SKalle Valo {
4115e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4116e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4117e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4118e705c121SKalle Valo 	int ret;
4119e705c121SKalle Valo 
4120e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4121e705c121SKalle Valo 
4122e705c121SKalle Valo 	mvmvif->phy_ctxt = phy_ctxt;
4123e705c121SKalle Valo 
4124e705c121SKalle Valo 	switch (vif->type) {
4125e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4126e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4127e705c121SKalle Valo 		if (switching_chanctx) {
4128e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4129e705c121SKalle Valo 			break;
4130e705c121SKalle Valo 		}
41315a2abdcaSGustavo A. R. Silva 		fallthrough;
4132e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4133e705c121SKalle Valo 		/*
4134e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4135e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4136e705c121SKalle Valo 		 */
4137e705c121SKalle Valo 		ret = 0;
4138e705c121SKalle Valo 		goto out;
4139e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
414019125cb0SAndrei Otcheretianski 		mvmvif->csa_bcn_pending = false;
4141e705c121SKalle Valo 		break;
4142e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4143e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4144e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4145e705c121SKalle Valo 		break;
4146e705c121SKalle Valo 	default:
4147e705c121SKalle Valo 		ret = -EINVAL;
4148e705c121SKalle Valo 		goto out;
4149e705c121SKalle Valo 	}
4150e705c121SKalle Valo 
4151e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4152e705c121SKalle Valo 	if (ret)
4153e705c121SKalle Valo 		goto out;
4154e705c121SKalle Valo 
4155e705c121SKalle Valo 	/*
4156e705c121SKalle Valo 	 * Power state must be updated before quotas,
4157e705c121SKalle Valo 	 * otherwise fw will complain.
4158e705c121SKalle Valo 	 */
4159e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4160e705c121SKalle Valo 
4161e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4162e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4163e705c121SKalle Valo 	 * will handle quota settings.
4164e705c121SKalle Valo 	 */
4165e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4166e705c121SKalle Valo 		mvmvif->monitor_active = true;
4167e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4168e705c121SKalle Valo 		if (ret)
4169e705c121SKalle Valo 			goto out_remove_binding;
41700e39eb03SChaya Rachel Ivgi 
41710e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
41720e39eb03SChaya Rachel Ivgi 		if (ret)
41730e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
41740e39eb03SChaya Rachel Ivgi 
4175e705c121SKalle Valo 	}
4176e705c121SKalle Valo 
4177e705c121SKalle Valo 	/* Handle binding during CSA */
4178e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4179e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4180e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4181e705c121SKalle Valo 	}
4182e705c121SKalle Valo 
4183e705c121SKalle Valo 	if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
418474a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
418574a10252SSara Sharon 
418674a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
418774a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
418819125cb0SAndrei Otcheretianski 			u32 duration = 3 * vif->bss_conf.beacon_int;
4189e705c121SKalle Valo 
4190e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4191e705c121SKalle Valo 			 * beacon on the new channel.
4192e705c121SKalle Valo 			 */
4193e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4194e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4195e705c121SKalle Valo 						true);
419674a10252SSara Sharon 		}
4197e705c121SKalle Valo 
4198e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4199e705c121SKalle Valo 	}
4200e705c121SKalle Valo 
4201e705c121SKalle Valo 	goto out;
4202e705c121SKalle Valo 
4203e705c121SKalle Valo out_remove_binding:
4204e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4205e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4206e705c121SKalle Valo out:
4207e705c121SKalle Valo 	if (ret)
4208e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
4209e705c121SKalle Valo 	return ret;
4210e705c121SKalle Valo }
4211e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4212e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4213e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4214e705c121SKalle Valo {
4215e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4216e705c121SKalle Valo 	int ret;
4217e705c121SKalle Valo 
4218e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4219e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
4220e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4221e705c121SKalle Valo 
4222e705c121SKalle Valo 	return ret;
4223e705c121SKalle Valo }
4224e705c121SKalle Valo 
4225e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4226e705c121SKalle Valo 					   struct ieee80211_vif *vif,
4227e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
4228e705c121SKalle Valo 					   bool switching_chanctx)
4229e705c121SKalle Valo {
4230e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4231e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
4232e705c121SKalle Valo 
4233e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4234e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4235e705c121SKalle Valo 
4236e705c121SKalle Valo 	switch (vif->type) {
4237e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4238e705c121SKalle Valo 		goto out;
4239e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4240e705c121SKalle Valo 		mvmvif->monitor_active = false;
4241e705c121SKalle Valo 		mvmvif->ps_disabled = false;
42420e39eb03SChaya Rachel Ivgi 		iwl_mvm_rm_snif_sta(mvm, vif);
4243e705c121SKalle Valo 		break;
4244e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4245e705c121SKalle Valo 		/* This part is triggered only during CSA */
4246e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
4247e705c121SKalle Valo 			goto out;
4248e705c121SKalle Valo 
4249e705c121SKalle Valo 		mvmvif->csa_countdown = false;
4250e705c121SKalle Valo 
4251e705c121SKalle Valo 		/* Set CS bit on all the stations */
4252e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4253e705c121SKalle Valo 
4254e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
4255e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4256e705c121SKalle Valo 
4257e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
4258e705c121SKalle Valo 		break;
4259e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4260e705c121SKalle Valo 		if (!switching_chanctx)
4261e705c121SKalle Valo 			break;
4262e705c121SKalle Valo 
4263e705c121SKalle Valo 		disabled_vif = vif;
4264e705c121SKalle Valo 
426574a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
426674a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4267e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
4268e705c121SKalle Valo 		break;
4269e705c121SKalle Valo 	default:
4270e705c121SKalle Valo 		break;
4271e705c121SKalle Valo 	}
4272e705c121SKalle Valo 
4273e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
4274e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4275e705c121SKalle Valo 
4276e705c121SKalle Valo out:
4277bf544e9aSSara Sharon 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
4278bf544e9aSSara Sharon 	    switching_chanctx)
4279bf544e9aSSara Sharon 		return;
4280e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
4281e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4282e705c121SKalle Valo }
4283e705c121SKalle Valo 
4284e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4285e705c121SKalle Valo 					 struct ieee80211_vif *vif,
4286e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
4287e705c121SKalle Valo {
4288e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4289e705c121SKalle Valo 
4290e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4291e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
4292e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4293e705c121SKalle Valo }
4294e705c121SKalle Valo 
4295e705c121SKalle Valo static int
4296e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4297e705c121SKalle Valo 				struct ieee80211_vif_chanctx_switch *vifs)
4298e705c121SKalle Valo {
4299e705c121SKalle Valo 	int ret;
4300e705c121SKalle Valo 
4301e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4302e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4303e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4304e705c121SKalle Valo 
4305e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4306e705c121SKalle Valo 	if (ret) {
4307e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4308e705c121SKalle Valo 		goto out_reassign;
4309e705c121SKalle Valo 	}
4310e705c121SKalle Valo 
4311e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4312e705c121SKalle Valo 					   true);
4313e705c121SKalle Valo 	if (ret) {
4314e705c121SKalle Valo 		IWL_ERR(mvm,
4315e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4316e705c121SKalle Valo 		goto out_remove;
4317e705c121SKalle Valo 	}
4318e705c121SKalle Valo 
4319e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
4320e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
4321e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
4322e705c121SKalle Valo 
4323e705c121SKalle Valo 	goto out;
4324e705c121SKalle Valo 
4325e705c121SKalle Valo out_remove:
4326e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4327e705c121SKalle Valo 
4328e705c121SKalle Valo out_reassign:
4329e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
4330e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4331e705c121SKalle Valo 		goto out_restart;
4332e705c121SKalle Valo 	}
4333e705c121SKalle Valo 
4334e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4335e705c121SKalle Valo 					 true)) {
4336e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4337e705c121SKalle Valo 		goto out_restart;
4338e705c121SKalle Valo 	}
4339e705c121SKalle Valo 
4340e705c121SKalle Valo 	goto out;
4341e705c121SKalle Valo 
4342e705c121SKalle Valo out_restart:
4343e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4344e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4345e705c121SKalle Valo 
4346e705c121SKalle Valo out:
4347e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4348e705c121SKalle Valo 
4349e705c121SKalle Valo 	return ret;
4350e705c121SKalle Valo }
4351e705c121SKalle Valo 
4352e705c121SKalle Valo static int
4353e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4354e705c121SKalle Valo 				    struct ieee80211_vif_chanctx_switch *vifs)
4355e705c121SKalle Valo {
4356e705c121SKalle Valo 	int ret;
4357e705c121SKalle Valo 
4358e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4359e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4360e705c121SKalle Valo 
4361e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4362e705c121SKalle Valo 					   true);
4363e705c121SKalle Valo 	if (ret) {
4364e705c121SKalle Valo 		IWL_ERR(mvm,
4365e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4366e705c121SKalle Valo 		goto out_reassign;
4367e705c121SKalle Valo 	}
4368e705c121SKalle Valo 
4369e705c121SKalle Valo 	goto out;
4370e705c121SKalle Valo 
4371e705c121SKalle Valo out_reassign:
4372e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4373e705c121SKalle Valo 					 true)) {
4374e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4375e705c121SKalle Valo 		goto out_restart;
4376e705c121SKalle Valo 	}
4377e705c121SKalle Valo 
4378e705c121SKalle Valo 	goto out;
4379e705c121SKalle Valo 
4380e705c121SKalle Valo out_restart:
4381e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4382e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4383e705c121SKalle Valo 
4384e705c121SKalle Valo out:
4385e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4386e705c121SKalle Valo 
4387e705c121SKalle Valo 	return ret;
4388e705c121SKalle Valo }
4389e705c121SKalle Valo 
4390e705c121SKalle Valo static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4391e705c121SKalle Valo 				      struct ieee80211_vif_chanctx_switch *vifs,
4392e705c121SKalle Valo 				      int n_vifs,
4393e705c121SKalle Valo 				      enum ieee80211_chanctx_switch_mode mode)
4394e705c121SKalle Valo {
4395e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4396e705c121SKalle Valo 	int ret;
4397e705c121SKalle Valo 
4398e705c121SKalle Valo 	/* we only support a single-vif right now */
4399e705c121SKalle Valo 	if (n_vifs > 1)
4400e705c121SKalle Valo 		return -EOPNOTSUPP;
4401e705c121SKalle Valo 
4402e705c121SKalle Valo 	switch (mode) {
4403e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
4404e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4405e705c121SKalle Valo 		break;
4406e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
4407e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
4408e705c121SKalle Valo 		break;
4409e705c121SKalle Valo 	default:
4410e705c121SKalle Valo 		ret = -EOPNOTSUPP;
4411e705c121SKalle Valo 		break;
4412e705c121SKalle Valo 	}
4413e705c121SKalle Valo 
4414e705c121SKalle Valo 	return ret;
4415e705c121SKalle Valo }
4416e705c121SKalle Valo 
44172f0282dbSJohannes Berg static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
44182f0282dbSJohannes Berg {
44192f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
44202f0282dbSJohannes Berg 
44212f0282dbSJohannes Berg 	return mvm->ibss_manager;
44222f0282dbSJohannes Berg }
44232f0282dbSJohannes Berg 
4424e705c121SKalle Valo static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4425e705c121SKalle Valo 			   struct ieee80211_sta *sta,
4426e705c121SKalle Valo 			   bool set)
4427e705c121SKalle Valo {
4428e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4429e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4430e705c121SKalle Valo 
4431e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
4432e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
4433e705c121SKalle Valo 		return -EINVAL;
4434e705c121SKalle Valo 	}
4435e705c121SKalle Valo 
4436e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4437e705c121SKalle Valo }
4438e705c121SKalle Valo 
4439e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
4440e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4441e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4442e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
4443e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
4444e705c121SKalle Valo };
4445e705c121SKalle Valo 
4446e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4447e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4448e705c121SKalle Valo 				      void *data, int len)
4449e705c121SKalle Valo {
4450e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4451e705c121SKalle Valo 	int err;
4452e705c121SKalle Valo 	u32 noa_duration;
4453e705c121SKalle Valo 
44548cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
44558cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
4456e705c121SKalle Valo 	if (err)
4457e705c121SKalle Valo 		return err;
4458e705c121SKalle Valo 
4459e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
4460e705c121SKalle Valo 		return -EINVAL;
4461e705c121SKalle Valo 
4462e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4463e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
4464e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4465e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
4466e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4467e705c121SKalle Valo 			return -EINVAL;
4468e705c121SKalle Valo 
4469e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4470e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
4471e705c121SKalle Valo 			return -EINVAL;
4472e705c121SKalle Valo 
4473e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
4474e705c121SKalle Valo 		mvm->noa_vif = vif;
4475e705c121SKalle Valo 
447622b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
4477e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4478e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
4479e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4480e705c121SKalle Valo 		    !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
4481e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4482e705c121SKalle Valo 			return -EINVAL;
4483e705c121SKalle Valo 
4484e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
4485e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4486e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4487e705c121SKalle Valo 	}
4488e705c121SKalle Valo 
4489e705c121SKalle Valo 	return -EOPNOTSUPP;
4490e705c121SKalle Valo }
4491e705c121SKalle Valo 
4492e705c121SKalle Valo static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4493e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4494e705c121SKalle Valo 				    void *data, int len)
4495e705c121SKalle Valo {
4496e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4497e705c121SKalle Valo 	int err;
4498e705c121SKalle Valo 
4499e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4500e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4501e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4502e705c121SKalle Valo 
4503e705c121SKalle Valo 	return err;
4504e705c121SKalle Valo }
4505e705c121SKalle Valo #endif
4506e705c121SKalle Valo 
4507e705c121SKalle Valo static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4508e705c121SKalle Valo 				   struct ieee80211_vif *vif,
4509e705c121SKalle Valo 				   struct ieee80211_channel_switch *chsw)
4510e705c121SKalle Valo {
4511e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
4512e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
4513e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
4514e705c121SKalle Valo 	 * (which is when the absence time event starts).
4515e705c121SKalle Valo 	 */
4516e705c121SKalle Valo 
4517e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4518e705c121SKalle Valo 			   "dummy channel switch op\n");
4519e705c121SKalle Valo }
4520e705c121SKalle Valo 
452174a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
452274a10252SSara Sharon 				       struct ieee80211_vif *vif,
452374a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
452474a10252SSara Sharon {
452574a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
452674a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
452774a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
452874a10252SSara Sharon 							  mvmvif->color)),
452974a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
453074a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
453174a10252SSara Sharon 		.cs_count = chsw->count,
453277738865SSara Sharon 		.cs_mode = chsw->block_tx,
453374a10252SSara Sharon 	};
453474a10252SSara Sharon 
453574a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
453674a10252SSara Sharon 
45379cfcf71cSSara Sharon 	if (chsw->delay)
45389cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
45399cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
45409cfcf71cSSara Sharon 
454174a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
454274a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
454374a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
454474a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
454574a10252SSara Sharon }
454674a10252SSara Sharon 
45470202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
45480202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
45490202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
45500202bcf0SEmmanuel Grumbach {
45510202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
45520202bcf0SEmmanuel Grumbach 	u32 apply_time;
45530202bcf0SEmmanuel Grumbach 
45540202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
45550202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
45560202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
45570202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
45580202bcf0SEmmanuel Grumbach 	 * loss).
45590202bcf0SEmmanuel Grumbach 	 */
45600202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
45610202bcf0SEmmanuel Grumbach 		apply_time = 0;
45620202bcf0SEmmanuel Grumbach 	else
45630202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
45640202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
45650202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
45660202bcf0SEmmanuel Grumbach 
45670202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
45680202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
45690202bcf0SEmmanuel Grumbach 
45700202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
45710202bcf0SEmmanuel Grumbach 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
45720202bcf0SEmmanuel Grumbach 
45730202bcf0SEmmanuel Grumbach 		if (ret)
45740202bcf0SEmmanuel Grumbach 			return ret;
45750202bcf0SEmmanuel Grumbach 	}
45760202bcf0SEmmanuel Grumbach 
45770202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
45780202bcf0SEmmanuel Grumbach 				    apply_time);
45790202bcf0SEmmanuel Grumbach 
45800202bcf0SEmmanuel Grumbach 	return 0;
45810202bcf0SEmmanuel Grumbach }
45820202bcf0SEmmanuel Grumbach 
4583f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
4584e705c121SKalle Valo static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4585e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4586e705c121SKalle Valo 				      struct ieee80211_channel_switch *chsw)
4587e705c121SKalle Valo {
4588e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4589e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
4590e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4591e705c121SKalle Valo 	int ret;
4592e705c121SKalle Valo 
4593e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4594e705c121SKalle Valo 
4595e705c121SKalle Valo 	mvmvif->csa_failed = false;
4596e705c121SKalle Valo 
4597e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
4598e705c121SKalle Valo 			   chsw->chandef.center_freq1);
4599e705c121SKalle Valo 
46007174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
46017174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
46027174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
4603e705c121SKalle Valo 
4604e705c121SKalle Valo 	switch (vif->type) {
4605e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4606e705c121SKalle Valo 		csa_vif =
4607e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
4608e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
4609e705c121SKalle Valo 		if (WARN_ONCE(csa_vif && csa_vif->csa_active,
4610e705c121SKalle Valo 			      "Another CSA is already in progress")) {
4611e705c121SKalle Valo 			ret = -EBUSY;
4612e705c121SKalle Valo 			goto out_unlock;
4613e705c121SKalle Valo 		}
4614e705c121SKalle Valo 
4615d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
4616d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
4617d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
4618d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
4619d3a108a4SAndrei Otcheretianski 			goto out_unlock;
4620d3a108a4SAndrei Otcheretianski 		}
4621d3a108a4SAndrei Otcheretianski 
4622e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
4623e705c121SKalle Valo 
4624e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
4625e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
4626e705c121SKalle Valo 			ret = -EBUSY;
4627e705c121SKalle Valo 			goto out_unlock;
4628e705c121SKalle Valo 		}
4629e705c121SKalle Valo 
4630d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
4631d3a108a4SAndrei Otcheretianski 
4632e705c121SKalle Valo 		break;
4633e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
46342360acbdSEmmanuel Grumbach 		/*
46352360acbdSEmmanuel Grumbach 		 * We haven't configured the firmware to be associated yet since
46362360acbdSEmmanuel Grumbach 		 * we don't know the dtim period. In this case, the firmware can't
46372360acbdSEmmanuel Grumbach 		 * track the beacons.
46382360acbdSEmmanuel Grumbach 		 */
46392360acbdSEmmanuel Grumbach 		if (!vif->bss_conf.assoc || !vif->bss_conf.dtim_period) {
46402360acbdSEmmanuel Grumbach 			ret = -EBUSY;
46412360acbdSEmmanuel Grumbach 			goto out_unlock;
46422360acbdSEmmanuel Grumbach 		}
46432360acbdSEmmanuel Grumbach 
464487d9564eSJohannes Berg 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX)
464587d9564eSJohannes Berg 			schedule_delayed_work(&mvmvif->csa_work, 0);
464687d9564eSJohannes Berg 
4647f6780614SSara Sharon 		if (chsw->block_tx) {
4648f6780614SSara Sharon 			/*
4649f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
4650f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
4651f6780614SSara Sharon 			 */
4652f6780614SSara Sharon 			if (!chsw->count ||
4653f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
4654f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
4655f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
4656f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
4657f6780614SSara Sharon 		}
4658e705c121SKalle Valo 
46590202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
46600202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
46610202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
4662e705c121SKalle Valo 			if (ret)
4663e705c121SKalle Valo 				goto out_unlock;
46640202bcf0SEmmanuel Grumbach 		} else {
466574a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
46660202bcf0SEmmanuel Grumbach 		}
466781b4e44eSSara Sharon 
466881b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
466981b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
4670e705c121SKalle Valo 		break;
4671e705c121SKalle Valo 	default:
4672e705c121SKalle Valo 		break;
4673e705c121SKalle Valo 	}
4674e705c121SKalle Valo 
4675e705c121SKalle Valo 	mvmvif->ps_disabled = true;
4676e705c121SKalle Valo 
4677e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
4678e705c121SKalle Valo 	if (ret)
4679e705c121SKalle Valo 		goto out_unlock;
4680e705c121SKalle Valo 
4681e705c121SKalle Valo 	/* we won't be on this channel any longer */
4682e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
4683e705c121SKalle Valo 
4684e705c121SKalle Valo out_unlock:
4685e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4686e705c121SKalle Valo 
4687e705c121SKalle Valo 	return ret;
4688e705c121SKalle Valo }
4689e705c121SKalle Valo 
4690c37763d2SSara Sharon static void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
4691c37763d2SSara Sharon 					     struct ieee80211_vif *vif,
4692c37763d2SSara Sharon 					     struct ieee80211_channel_switch *chsw)
4693e705c121SKalle Valo {
4694e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4695c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4696c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
4697c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4698c37763d2SSara Sharon 							  mvmvif->color)),
4699c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
4700c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
4701c37763d2SSara Sharon 		.cs_count = chsw->count,
470277738865SSara Sharon 		.cs_mode = chsw->block_tx,
4703c37763d2SSara Sharon 	};
4704e705c121SKalle Valo 
4705c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
4706c37763d2SSara Sharon 		return;
4707e705c121SKalle Valo 
470881b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
470981b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
471081b4e44eSSara Sharon 			/* Second time, give up on this AP*/
471181b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
471281b4e44eSSara Sharon 			ieee80211_chswitch_done(vif, false);
471381b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
471481b4e44eSSara Sharon 			return;
4715e705c121SKalle Valo 		}
471681b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
4717e705c121SKalle Valo 	}
471881b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
4719e705c121SKalle Valo 
4720caf46377SSara Sharon 	mutex_lock(&mvm->mutex);
4721caf46377SSara Sharon 	if (mvmvif->csa_failed)
4722caf46377SSara Sharon 		goto out_unlock;
4723e705c121SKalle Valo 
4724b06b5986SEmmanuel Grumbach 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d mode = %d\n",
4725b06b5986SEmmanuel Grumbach 			   mvmvif->id, chsw->count, chsw->block_tx);
4726c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
4727c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
4728c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
4729caf46377SSara Sharon 				     0, sizeof(cmd), &cmd));
4730caf46377SSara Sharon out_unlock:
4731caf46377SSara Sharon 	mutex_unlock(&mvm->mutex);
4732e705c121SKalle Valo }
4733e705c121SKalle Valo 
47346110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
47356110d9e5SDavid Spinadel {
4736435d0827SSara Sharon 	int i;
4737435d0827SSara Sharon 
473806195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
4739309c4848SLuca Coelho 		if (drop) {
4740309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
47416110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
4742d4e3a341SMordechay Goodstein 				iwl_mvm_flushable_queues(mvm) & queues);
4743309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
4744309c4848SLuca Coelho 		} else {
4745435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
4746309c4848SLuca Coelho 		}
4747435d0827SSara Sharon 		return;
4748435d0827SSara Sharon 	}
47496110d9e5SDavid Spinadel 
47506110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
4751be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4752435d0827SSara Sharon 		struct ieee80211_sta *sta;
4753435d0827SSara Sharon 
4754435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
47556110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
47566110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
47576110d9e5SDavid Spinadel 			continue;
47586110d9e5SDavid Spinadel 
475906195639SSara Sharon 		if (drop)
4760d4e3a341SMordechay Goodstein 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
476106195639SSara Sharon 		else
47626110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
47636110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
47646110d9e5SDavid Spinadel 	}
47656110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
47666110d9e5SDavid Spinadel }
47676110d9e5SDavid Spinadel 
4768e705c121SKalle Valo static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4769e705c121SKalle Valo 			      struct ieee80211_vif *vif, u32 queues, bool drop)
4770e705c121SKalle Valo {
4771e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4772e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
4773e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4774e705c121SKalle Valo 	struct ieee80211_sta *sta;
4775e705c121SKalle Valo 	int i;
4776e705c121SKalle Valo 	u32 msk = 0;
4777e705c121SKalle Valo 
47786110d9e5SDavid Spinadel 	if (!vif) {
47796110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
47806110d9e5SDavid Spinadel 		return;
47816110d9e5SDavid Spinadel 	}
47826110d9e5SDavid Spinadel 
47836110d9e5SDavid Spinadel 	if (vif->type != NL80211_IFTYPE_STATION)
4784e705c121SKalle Valo 		return;
4785e705c121SKalle Valo 
478624afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
478724afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
478824afba76SLiad Kaufman 
4789e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4790e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
4791e705c121SKalle Valo 
4792e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
4793be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4794e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4795e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
4796e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
4797e705c121SKalle Valo 			continue;
4798e705c121SKalle Valo 
4799e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4800e705c121SKalle Valo 		if (mvmsta->vif != vif)
4801e705c121SKalle Valo 			continue;
4802e705c121SKalle Valo 
4803e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
4804e705c121SKalle Valo 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4805e705c121SKalle Valo 
4806d49394a1SSara Sharon 		if (drop) {
4807f9084775SNathan Errera 			if (iwl_mvm_flush_sta(mvm, mvmsta, false))
4808d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
4809d49394a1SSara Sharon 		} else {
4810e705c121SKalle Valo 			msk |= mvmsta->tfd_queue_msk;
4811d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
4812d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
4813e705c121SKalle Valo 		}
4814d49394a1SSara Sharon 	}
4815e705c121SKalle Valo 
4816e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4817e705c121SKalle Valo 
4818e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
4819e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
4820e705c121SKalle Valo 	 */
4821d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
4822a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
4823e705c121SKalle Valo }
4824e705c121SKalle Valo 
4825e705c121SKalle Valo static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4826e705c121SKalle Valo 				  struct survey_info *survey)
4827e705c121SKalle Valo {
4828e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4829e705c121SKalle Valo 	int ret;
4830e705c121SKalle Valo 
4831e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
4832e705c121SKalle Valo 
4833e705c121SKalle Valo 	/* only support global statistics right now */
4834e705c121SKalle Valo 	if (idx != 0)
4835e705c121SKalle Valo 		return -ENOENT;
4836e705c121SKalle Valo 
4837280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4838e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4839e705c121SKalle Valo 		return -ENOENT;
4840e705c121SKalle Valo 
4841e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4842e705c121SKalle Valo 
4843aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
4844e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
4845e705c121SKalle Valo 		if (ret)
4846e705c121SKalle Valo 			goto out;
4847e705c121SKalle Valo 	}
4848e705c121SKalle Valo 
4849e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
4850e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
4851e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
4852e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
4853e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
4854e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
4855e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
4856e705c121SKalle Valo 
4857e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
4858e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
4859e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
4860e705c121SKalle Valo 
4861e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
4862e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
4863e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
4864e705c121SKalle Valo 
4865e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4866e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
4867e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
4868e705c121SKalle Valo 
4869e705c121SKalle Valo 	ret = 0;
4870e705c121SKalle Valo  out:
4871e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4872e705c121SKalle Valo 	return ret;
4873e705c121SKalle Valo }
4874e705c121SKalle Valo 
4875ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
4876ed780545SJohannes Berg {
4877ed780545SJohannes Berg 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
4878ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
4879ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
4880ed780545SJohannes Berg 		break;
4881ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
4882ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
4883ed780545SJohannes Berg 		break;
4884ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
4885ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
4886ed780545SJohannes Berg 		break;
4887ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
4888ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
4889ed780545SJohannes Berg 		break;
4890ed780545SJohannes Berg 	}
4891ed780545SJohannes Berg 
4892ed780545SJohannes Berg 	if (rate_n_flags & RATE_MCS_HT_MSK) {
4893ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
4894ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags, RATE_HT_MCS_INDEX_MSK);
4895ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4896ed780545SJohannes Berg 					  RATE_HT_MCS_NSS_MSK) + 1;
4897ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_SGI_MSK)
4898ed780545SJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
4899ed780545SJohannes Berg 	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
4900ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
4901ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags,
4902ed780545SJohannes Berg 					  RATE_VHT_MCS_RATE_CODE_MSK);
4903ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4904ed780545SJohannes Berg 					  RATE_VHT_MCS_NSS_MSK) + 1;
4905ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_SGI_MSK)
4906ed780545SJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
4907ed780545SJohannes Berg 	} else if (rate_n_flags & RATE_MCS_HE_MSK) {
4908ed780545SJohannes Berg 		u32 gi_ltf = u32_get_bits(rate_n_flags,
4909ed780545SJohannes Berg 					  RATE_MCS_HE_GI_LTF_MSK);
4910ed780545SJohannes Berg 
4911ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
4912ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags,
4913ed780545SJohannes Berg 					  RATE_VHT_MCS_RATE_CODE_MSK);
4914ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4915ed780545SJohannes Berg 					  RATE_VHT_MCS_NSS_MSK) + 1;
4916ed780545SJohannes Berg 
4917ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
4918ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
4919ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
4920ed780545SJohannes Berg 		}
4921ed780545SJohannes Berg 
4922ed780545SJohannes Berg 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
4923ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_SU:
4924ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_EXT_SU:
4925ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4926ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4927ed780545SJohannes Berg 			else if (gi_ltf == 2)
4928ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4929ed780545SJohannes Berg 			else if (rate_n_flags & RATE_MCS_SGI_MSK)
4930ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4931ed780545SJohannes Berg 			else
4932ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4933ed780545SJohannes Berg 			break;
4934ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_MU:
4935ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4936ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4937ed780545SJohannes Berg 			else if (gi_ltf == 2)
4938ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4939ed780545SJohannes Berg 			else
4940ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4941ed780545SJohannes Berg 			break;
4942ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_TRIG:
4943ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4944ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4945ed780545SJohannes Berg 			else
4946ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4947ed780545SJohannes Berg 			break;
4948ed780545SJohannes Berg 		}
4949ed780545SJohannes Berg 
4950ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
4951ed780545SJohannes Berg 			rinfo->he_dcm = 1;
4952ed780545SJohannes Berg 	} else {
4953ed780545SJohannes Berg 		switch (u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK)) {
4954ed780545SJohannes Berg 		case IWL_RATE_1M_PLCP:
4955ed780545SJohannes Berg 			rinfo->legacy = 10;
4956ed780545SJohannes Berg 			break;
4957ed780545SJohannes Berg 		case IWL_RATE_2M_PLCP:
4958ed780545SJohannes Berg 			rinfo->legacy = 20;
4959ed780545SJohannes Berg 			break;
4960ed780545SJohannes Berg 		case IWL_RATE_5M_PLCP:
4961ed780545SJohannes Berg 			rinfo->legacy = 55;
4962ed780545SJohannes Berg 			break;
4963ed780545SJohannes Berg 		case IWL_RATE_11M_PLCP:
4964ed780545SJohannes Berg 			rinfo->legacy = 110;
4965ed780545SJohannes Berg 			break;
4966ed780545SJohannes Berg 		case IWL_RATE_6M_PLCP:
4967ed780545SJohannes Berg 			rinfo->legacy = 60;
4968ed780545SJohannes Berg 			break;
4969ed780545SJohannes Berg 		case IWL_RATE_9M_PLCP:
4970ed780545SJohannes Berg 			rinfo->legacy = 90;
4971ed780545SJohannes Berg 			break;
4972ed780545SJohannes Berg 		case IWL_RATE_12M_PLCP:
4973ed780545SJohannes Berg 			rinfo->legacy = 120;
4974ed780545SJohannes Berg 			break;
4975ed780545SJohannes Berg 		case IWL_RATE_18M_PLCP:
4976ed780545SJohannes Berg 			rinfo->legacy = 180;
4977ed780545SJohannes Berg 			break;
4978ed780545SJohannes Berg 		case IWL_RATE_24M_PLCP:
4979ed780545SJohannes Berg 			rinfo->legacy = 240;
4980ed780545SJohannes Berg 			break;
4981ed780545SJohannes Berg 		case IWL_RATE_36M_PLCP:
4982ed780545SJohannes Berg 			rinfo->legacy = 360;
4983ed780545SJohannes Berg 			break;
4984ed780545SJohannes Berg 		case IWL_RATE_48M_PLCP:
4985ed780545SJohannes Berg 			rinfo->legacy = 480;
4986ed780545SJohannes Berg 			break;
4987ed780545SJohannes Berg 		case IWL_RATE_54M_PLCP:
4988ed780545SJohannes Berg 			rinfo->legacy = 540;
4989ed780545SJohannes Berg 			break;
4990ed780545SJohannes Berg 		}
4991ed780545SJohannes Berg 	}
4992ed780545SJohannes Berg }
4993ed780545SJohannes Berg 
4994e705c121SKalle Valo static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
4995e705c121SKalle Valo 				       struct ieee80211_vif *vif,
4996e705c121SKalle Valo 				       struct ieee80211_sta *sta,
4997e705c121SKalle Valo 				       struct station_info *sinfo)
4998e705c121SKalle Valo {
4999e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5000e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5001e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5002e705c121SKalle Valo 
5003988b5968SSara Sharon 	if (mvmsta->avg_energy) {
500488ad368aSBen Greear 		sinfo->signal_avg = -(s8)mvmsta->avg_energy;
500522d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
5006988b5968SSara Sharon 	}
5007988b5968SSara Sharon 
5008ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
5009ed780545SJohannes Berg 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
5010ed780545SJohannes Berg 
5011ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5012ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5013ed780545SJohannes Berg 	}
5014ed780545SJohannes Berg 
5015e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
5016e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5017e705c121SKalle Valo 		return;
5018e705c121SKalle Valo 
5019e705c121SKalle Valo 	if (!vif->bss_conf.assoc)
5020e705c121SKalle Valo 		return;
5021e705c121SKalle Valo 
5022e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5023e705c121SKalle Valo 
5024e705c121SKalle Valo 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
5025e705c121SKalle Valo 		goto unlock;
5026e705c121SKalle Valo 
5027e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
5028e705c121SKalle Valo 		goto unlock;
5029e705c121SKalle Valo 
5030e705c121SKalle Valo 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
5031e705c121SKalle Valo 			   mvmvif->beacon_stats.accu_num_beacons;
503222d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
5033e705c121SKalle Valo 	if (mvmvif->beacon_stats.avg_signal) {
5034e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
5035e705c121SKalle Valo 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
503622d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
5037e705c121SKalle Valo 	}
5038e705c121SKalle Valo  unlock:
5039e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5040e705c121SKalle Valo }
5041e705c121SKalle Valo 
5042119c2a13SMordechay Goodstein static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5043119c2a13SMordechay Goodstein 					    struct ieee80211_vif *vif,
5044119c2a13SMordechay Goodstein 					    const  struct ieee80211_mlme_event *mlme)
5045119c2a13SMordechay Goodstein {
50461a81bddfSMordechay Goodstein 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
50471a81bddfSMordechay Goodstein 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
5048119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5049119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5050119c2a13SMordechay Goodstein 				       NULL);
5051119c2a13SMordechay Goodstein 		return;
5052119c2a13SMordechay Goodstein 	}
5053119c2a13SMordechay Goodstein 
5054119c2a13SMordechay Goodstein 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5055119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5056119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_DEASSOC,
5057119c2a13SMordechay Goodstein 				       NULL);
5058119c2a13SMordechay Goodstein 		return;
5059119c2a13SMordechay Goodstein 	}
5060119c2a13SMordechay Goodstein }
5061119c2a13SMordechay Goodstein 
5062e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5063e705c121SKalle Valo 					struct ieee80211_vif *vif,
5064e705c121SKalle Valo 					const struct ieee80211_event *event)
5065e705c121SKalle Valo {
50664c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5067e705c121SKalle Valo 	do {								\
50684c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5069e705c121SKalle Valo 			break;						\
50707174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5071e705c121SKalle Valo 	} while (0)
5072e705c121SKalle Valo 
5073e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5074e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5075e705c121SKalle Valo 
5076119c2a13SMordechay Goodstein 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5077119c2a13SMordechay Goodstein 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5078119c2a13SMordechay Goodstein 		return;
5079119c2a13SMordechay Goodstein 	}
5080119c2a13SMordechay Goodstein 
50816c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
50826c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
50836c042d75SSara Sharon 	if (!trig)
5084e705c121SKalle Valo 		return;
5085e705c121SKalle Valo 
5086e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
5087e705c121SKalle Valo 
5088e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
5089e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
50904c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
5091e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
5092e705c121SKalle Valo 					    event->u.mlme.reason);
5093e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
50944c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
5095e705c121SKalle Valo 					   "ASSOC TIMEOUT");
5096e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
5097e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
50984c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
5099e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
5100e705c121SKalle Valo 					   event->u.mlme.reason);
5101e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
51024c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
5103e705c121SKalle Valo 					   "AUTH TIMEOUT");
5104e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
51054c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
5106e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
5107e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
51084c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
5109e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
5110e705c121SKalle Valo 	}
5111e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
5112e705c121SKalle Valo }
5113e705c121SKalle Valo 
5114e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5115e705c121SKalle Valo 					  struct ieee80211_vif *vif,
5116e705c121SKalle Valo 					  const struct ieee80211_event *event)
5117e705c121SKalle Valo {
5118e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5119e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
5120e705c121SKalle Valo 
51216c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
51226c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
51236c042d75SSara Sharon 	if (!trig)
5124e705c121SKalle Valo 		return;
5125e705c121SKalle Valo 
5126e705c121SKalle Valo 	ba_trig = (void *)trig->data;
5127e705c121SKalle Valo 
5128e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5129e705c121SKalle Valo 		return;
5130e705c121SKalle Valo 
51317174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
5132e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
5133e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
5134e705c121SKalle Valo 				event->u.ba.ssn);
5135e705c121SKalle Valo }
5136e705c121SKalle Valo 
5137e705c121SKalle Valo static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
5138e705c121SKalle Valo 				       struct ieee80211_vif *vif,
5139e705c121SKalle Valo 				       const struct ieee80211_event *event)
5140e705c121SKalle Valo {
5141e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5142e705c121SKalle Valo 
5143e705c121SKalle Valo 	switch (event->type) {
5144e705c121SKalle Valo 	case MLME_EVENT:
5145e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
5146e705c121SKalle Valo 		break;
5147e705c121SKalle Valo 	case BAR_RX_EVENT:
5148e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
5149e705c121SKalle Valo 		break;
5150e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
5151528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
5152528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
5153e705c121SKalle Valo 		break;
5154e705c121SKalle Valo 	default:
5155e705c121SKalle Valo 		break;
5156e705c121SKalle Valo 	}
5157e705c121SKalle Valo }
5158e705c121SKalle Valo 
5159d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
51605e1688ceSJohannes Berg 				     enum iwl_mvm_rxq_notif_type type,
51615e1688ceSJohannes Berg 				     bool sync,
51625e1688ceSJohannes Berg 				     const void *data, u32 size)
51630636b938SSara Sharon {
51645e1688ceSJohannes Berg 	struct {
51655e1688ceSJohannes Berg 		struct iwl_rxq_sync_cmd cmd;
51665e1688ceSJohannes Berg 		struct iwl_mvm_internal_rxq_notif notif;
51675e1688ceSJohannes Berg 	} __packed cmd = {
51685e1688ceSJohannes Berg 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
51695e1688ceSJohannes Berg 		.cmd.count =
51705e1688ceSJohannes Berg 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
51715e1688ceSJohannes Berg 				    size),
51725e1688ceSJohannes Berg 		.notif.type = type,
51735e1688ceSJohannes Berg 		.notif.sync = sync,
51745e1688ceSJohannes Berg 	};
51755e1688ceSJohannes Berg 	struct iwl_host_cmd hcmd = {
51765e1688ceSJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
51775e1688ceSJohannes Berg 		.data[0] = &cmd,
51785e1688ceSJohannes Berg 		.len[0] = sizeof(cmd),
51795e1688ceSJohannes Berg 		.data[1] = data,
51805e1688ceSJohannes Berg 		.len[1] = size,
51815e1688ceSJohannes Berg 		.flags = sync ? 0 : CMD_ASYNC,
51825e1688ceSJohannes Berg 	};
51830636b938SSara Sharon 	int ret;
51840636b938SSara Sharon 
51855e1688ceSJohannes Berg 	/* size must be a multiple of DWORD */
51865e1688ceSJohannes Berg 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
51875e1688ceSJohannes Berg 		return;
51880636b938SSara Sharon 
5189cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
51900636b938SSara Sharon 		return;
51910636b938SSara Sharon 
51925e1688ceSJohannes Berg 	if (sync) {
51935e1688ceSJohannes Berg 		cmd.notif.cookie = mvm->queue_sync_cookie;
51942f7a04c7SJohannes Berg 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
5195a2113cc4SNaftali Goldstein 	}
5196d0ff5d22SSara Sharon 
51975e1688ceSJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
51980636b938SSara Sharon 	if (ret) {
51990636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
52000636b938SSara Sharon 		goto out;
52010636b938SSara Sharon 	}
5202d0ff5d22SSara Sharon 
52035e1688ceSJohannes Berg 	if (sync) {
52043c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
52053a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
52062f7a04c7SJohannes Berg 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
52076ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
52080636b938SSara Sharon 					 HZ);
52092f7a04c7SJohannes Berg 		WARN_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm),
52102f7a04c7SJohannes Berg 			  "queue sync: failed to sync, state is 0x%lx\n",
52112f7a04c7SJohannes Berg 			  mvm->queue_sync_state);
52126ad04359SJohannes Berg 	}
52130636b938SSara Sharon 
52140636b938SSara Sharon out:
52155e1688ceSJohannes Berg 	if (sync) {
52162f7a04c7SJohannes Berg 		mvm->queue_sync_state = 0;
52170636b938SSara Sharon 		mvm->queue_sync_cookie++;
52180636b938SSara Sharon 	}
52195f8a3561SJohannes Berg }
52200636b938SSara Sharon 
52210636b938SSara Sharon static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
52220636b938SSara Sharon {
52230636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
52240636b938SSara Sharon 
52250636b938SSara Sharon 	mutex_lock(&mvm->mutex);
52265e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
52270636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
52280636b938SSara Sharon }
52290636b938SSara Sharon 
5230b73f9a4aSJohannes Berg static int
5231b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
5232b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
5233b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
5234b73f9a4aSJohannes Berg {
5235b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5236b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5237b73f9a4aSJohannes Berg 
5238b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
5239b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
5240b73f9a4aSJohannes Berg 		return -EINVAL;
5241b73f9a4aSJohannes Berg 
5242b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
5243b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
5244b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
5245b73f9a4aSJohannes Berg 
5246b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
5247b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
5248b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
5249b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
5250b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
5251b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
5252b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
5253b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
5254b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
5255b73f9a4aSJohannes Berg 
5256b73f9a4aSJohannes Berg 	return 0;
5257b73f9a4aSJohannes Berg }
5258b73f9a4aSJohannes Berg 
5259fc36ffdaSJohannes Berg static int iwl_mvm_start_pmsr(struct ieee80211_hw *hw,
5260fc36ffdaSJohannes Berg 			      struct ieee80211_vif *vif,
5261fc36ffdaSJohannes Berg 			      struct cfg80211_pmsr_request *request)
5262fc36ffdaSJohannes Berg {
5263fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5264fc36ffdaSJohannes Berg 	int ret;
5265fc36ffdaSJohannes Berg 
5266fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5267fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
5268fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5269fc36ffdaSJohannes Berg 
5270fc36ffdaSJohannes Berg 	return ret;
5271fc36ffdaSJohannes Berg }
5272fc36ffdaSJohannes Berg 
5273fc36ffdaSJohannes Berg static void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw,
5274fc36ffdaSJohannes Berg 			       struct ieee80211_vif *vif,
5275fc36ffdaSJohannes Berg 			       struct cfg80211_pmsr_request *request)
5276fc36ffdaSJohannes Berg {
5277fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5278fc36ffdaSJohannes Berg 
5279fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5280fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
5281fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5282fc36ffdaSJohannes Berg }
5283fc36ffdaSJohannes Berg 
5284438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
5285438af969SSara Sharon {
5286438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
5287438af969SSara Sharon 
5288438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
5289438af969SSara Sharon 		return false;
5290438af969SSara Sharon 
5291438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
5292438af969SSara Sharon }
5293438af969SSara Sharon 
5294438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
5295438af969SSara Sharon 				      struct sk_buff *head,
5296438af969SSara Sharon 				      struct sk_buff *skb)
5297438af969SSara Sharon {
5298438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5299438af969SSara Sharon 
5300438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
5301438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
5302438af969SSara Sharon 		return false;
5303438af969SSara Sharon 
5304438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
5305438af969SSara Sharon 		return true;
5306438af969SSara Sharon 
5307438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5308438af969SSara Sharon }
5309438af969SSara Sharon 
5310e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
5311e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
5312cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
5313e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
5314e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
5315e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
5316e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
5317e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
5318e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
5319e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
5320e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
5321e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
5322e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
5323e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
5324e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
5325e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
5326e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
5327e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
5328e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
5329e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
5330e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
5331e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
5332e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
5333e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
5334e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
5335e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
5336e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
5337e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
5338e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
5339e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
5340e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
5341e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
5342e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
5343e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
5344e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
5345e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
5346e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
5347e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5348e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
5349e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
5350e705c121SKalle Valo 
5351e705c121SKalle Valo 	.start_ap = iwl_mvm_start_ap_ibss,
5352e705c121SKalle Valo 	.stop_ap = iwl_mvm_stop_ap_ibss,
5353e705c121SKalle Valo 	.join_ibss = iwl_mvm_start_ap_ibss,
5354e705c121SKalle Valo 	.leave_ibss = iwl_mvm_stop_ap_ibss,
5355e705c121SKalle Valo 
53562f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
53572f0282dbSJohannes Berg 
5358e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
5359e705c121SKalle Valo 
5360e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
5361e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
5362e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
536379221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
5364c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
5365e705c121SKalle Valo 
5366e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5367e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5368e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5369e705c121SKalle Valo 
5370e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
5371e705c121SKalle Valo 
53720636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
53730636b938SSara Sharon 
5374e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5375e705c121SKalle Valo 
5376e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
5377e705c121SKalle Valo 	/* look at d3.c */
5378e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
5379e705c121SKalle Valo 	.resume = iwl_mvm_resume,
5380e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
5381e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
5382e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
5383e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
5384e705c121SKalle Valo #endif
5385e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
5386e705c121SKalle Valo #endif
5387e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
5388e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
5389b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
5390fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
5391fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
5392b73f9a4aSJohannes Berg 
5393438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
5394177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
5395177a11cfSGregory Greenman 	.sta_add_debugfs = iwl_mvm_sta_add_debugfs,
5396177a11cfSGregory Greenman #endif
5397e705c121SKalle Valo };
5398