xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision fe56d05ee6c87f6a1a8c7267affd92c9438249cc)
1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
826d6c16bSSara Sharon  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
926d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
1012d47f0eSAndrei Otcheretianski  * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation
11e705c121SKalle Valo  *
12e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
13e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
14e705c121SKalle Valo  * published by the Free Software Foundation.
15e705c121SKalle Valo  *
16e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
17e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
18e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19e705c121SKalle Valo  * General Public License for more details.
20e705c121SKalle Valo  *
21e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
22e705c121SKalle Valo  * in the file called COPYING.
23e705c121SKalle Valo  *
24e705c121SKalle Valo  * Contact Information:
25cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
26e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27e705c121SKalle Valo  *
28e705c121SKalle Valo  * BSD LICENSE
29e705c121SKalle Valo  *
3026d6c16bSSara Sharon  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
3126d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
3212d47f0eSAndrei Otcheretianski  * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation
33e705c121SKalle Valo  * All rights reserved.
34e705c121SKalle Valo  *
35e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
36e705c121SKalle Valo  * modification, are permitted provided that the following conditions
37e705c121SKalle Valo  * are met:
38e705c121SKalle Valo  *
39e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
40e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
41e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
42e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
43e705c121SKalle Valo  *    the documentation and/or other materials provided with the
44e705c121SKalle Valo  *    distribution.
45e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
46e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
47e705c121SKalle Valo  *    from this software without specific prior written permission.
48e705c121SKalle Valo  *
49e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60e705c121SKalle Valo  *
61e705c121SKalle Valo  *****************************************************************************/
62e705c121SKalle Valo #include <linux/kernel.h>
63e705c121SKalle Valo #include <linux/slab.h>
64e705c121SKalle Valo #include <linux/skbuff.h>
65e705c121SKalle Valo #include <linux/netdevice.h>
66e705c121SKalle Valo #include <linux/etherdevice.h>
67e705c121SKalle Valo #include <linux/ip.h>
68e705c121SKalle Valo #include <linux/if_arp.h>
692f89a5d7SGolan Ben-Ami #include <linux/time.h>
70e705c121SKalle Valo #include <net/mac80211.h>
71e705c121SKalle Valo #include <net/ieee80211_radiotap.h>
72e705c121SKalle Valo #include <net/tcp.h>
73e705c121SKalle Valo 
74e705c121SKalle Valo #include "iwl-op-mode.h"
75e705c121SKalle Valo #include "iwl-io.h"
76e705c121SKalle Valo #include "mvm.h"
77e705c121SKalle Valo #include "sta.h"
78e705c121SKalle Valo #include "time-event.h"
79e705c121SKalle Valo #include "iwl-eeprom-parse.h"
80e705c121SKalle Valo #include "iwl-phy-db.h"
81e705c121SKalle Valo #include "testmode.h"
82d962f9b1SJohannes Berg #include "fw/error-dump.h"
83e705c121SKalle Valo #include "iwl-prph.h"
84e705c121SKalle Valo #include "iwl-nvm-parse.h"
85e705c121SKalle Valo 
86e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
87e705c121SKalle Valo 	{
88e705c121SKalle Valo 		.max = 1,
89e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
90e705c121SKalle Valo 	},
91e705c121SKalle Valo 	{
92e705c121SKalle Valo 		.max = 1,
93e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
94e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
95e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
96e705c121SKalle Valo 	},
97e705c121SKalle Valo 	{
98e705c121SKalle Valo 		.max = 1,
99e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
100e705c121SKalle Valo 	},
101e705c121SKalle Valo };
102e705c121SKalle Valo 
103e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
104e705c121SKalle Valo 	{
105e705c121SKalle Valo 		.num_different_channels = 2,
106e705c121SKalle Valo 		.max_interfaces = 3,
107e705c121SKalle Valo 		.limits = iwl_mvm_limits,
108e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
109e705c121SKalle Valo 	},
110e705c121SKalle Valo };
111e705c121SKalle Valo 
112e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
113e705c121SKalle Valo /*
114e705c121SKalle Valo  * Use the reserved field to indicate magic values.
115e705c121SKalle Valo  * these values will only be used internally by the driver,
116e705c121SKalle Valo  * and won't make it to the fw (reserved will be 0).
117e705c121SKalle Valo  * BC_FILTER_MAGIC_IP - configure the val of this attribute to
118e705c121SKalle Valo  *	be the vif's ip address. in case there is not a single
119e705c121SKalle Valo  *	ip address (0, or more than 1), this attribute will
120e705c121SKalle Valo  *	be skipped.
121e705c121SKalle Valo  * BC_FILTER_MAGIC_MAC - set the val of this attribute to
122e705c121SKalle Valo  *	the LSB bytes of the vif's mac address
123e705c121SKalle Valo  */
124e705c121SKalle Valo enum {
125e705c121SKalle Valo 	BC_FILTER_MAGIC_NONE = 0,
126e705c121SKalle Valo 	BC_FILTER_MAGIC_IP,
127e705c121SKalle Valo 	BC_FILTER_MAGIC_MAC,
128e705c121SKalle Valo };
129e705c121SKalle Valo 
130e705c121SKalle Valo static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
131e705c121SKalle Valo 	{
132e705c121SKalle Valo 		/* arp */
133e705c121SKalle Valo 		.discard = 0,
134e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
135e705c121SKalle Valo 		.attrs = {
136e705c121SKalle Valo 			{
137e705c121SKalle Valo 				/* frame type - arp, hw type - ethernet */
138e705c121SKalle Valo 				.offset_type =
139e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
140e705c121SKalle Valo 				.offset = sizeof(rfc1042_header),
141e705c121SKalle Valo 				.val = cpu_to_be32(0x08060001),
142e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
143e705c121SKalle Valo 			},
144e705c121SKalle Valo 			{
145e705c121SKalle Valo 				/* arp dest ip */
146e705c121SKalle Valo 				.offset_type =
147e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
148e705c121SKalle Valo 				.offset = sizeof(rfc1042_header) + 2 +
149e705c121SKalle Valo 					  sizeof(struct arphdr) +
150e705c121SKalle Valo 					  ETH_ALEN + sizeof(__be32) +
151e705c121SKalle Valo 					  ETH_ALEN,
152e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
153e705c121SKalle Valo 				/* mark it as special field */
154e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
155e705c121SKalle Valo 			},
156e705c121SKalle Valo 		},
157e705c121SKalle Valo 	},
158e705c121SKalle Valo 	{
159e705c121SKalle Valo 		/* dhcp offer bcast */
160e705c121SKalle Valo 		.discard = 0,
161e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
162e705c121SKalle Valo 		.attrs = {
163e705c121SKalle Valo 			{
164e705c121SKalle Valo 				/* udp dest port - 68 (bootp client)*/
165e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
166e705c121SKalle Valo 				.offset = offsetof(struct udphdr, dest),
167e705c121SKalle Valo 				.val = cpu_to_be32(0x00440000),
168e705c121SKalle Valo 				.mask = cpu_to_be32(0xffff0000),
169e705c121SKalle Valo 			},
170e705c121SKalle Valo 			{
171e705c121SKalle Valo 				/* dhcp - lsb bytes of client hw address */
172e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
173e705c121SKalle Valo 				.offset = 38,
174e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
175e705c121SKalle Valo 				/* mark it as special field */
176e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
177e705c121SKalle Valo 			},
178e705c121SKalle Valo 		},
179e705c121SKalle Valo 	},
180e705c121SKalle Valo 	/* last filter must be empty */
181e705c121SKalle Valo 	{},
182e705c121SKalle Valo };
183e705c121SKalle Valo #endif
184e705c121SKalle Valo 
185fc36ffdaSJohannes Berg static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
186fc36ffdaSJohannes Berg 	.max_peers = IWL_MVM_TOF_MAX_APS,
187fc36ffdaSJohannes Berg 	.report_ap_tsf = 1,
188fc36ffdaSJohannes Berg 	.randomize_mac_addr = 1,
189fc36ffdaSJohannes Berg 
190fc36ffdaSJohannes Berg 	.ftm = {
191fc36ffdaSJohannes Berg 		.supported = 1,
192fc36ffdaSJohannes Berg 		.asap = 1,
193fc36ffdaSJohannes Berg 		.non_asap = 1,
194fc36ffdaSJohannes Berg 		.request_lci = 1,
195fc36ffdaSJohannes Berg 		.request_civicloc = 1,
1966815e3d0SAvraham Stern 		.trigger_based = 1,
1976815e3d0SAvraham Stern 		.non_trigger_based = 1,
198fc36ffdaSJohannes Berg 		.max_bursts_exponent = -1, /* all supported */
199fc36ffdaSJohannes Berg 		.max_ftms_per_burst = 0, /* no limits */
200fc36ffdaSJohannes Berg 		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
201fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_20) |
202fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_40) |
203fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_80),
204fc36ffdaSJohannes Berg 		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
205fc36ffdaSJohannes Berg 			     BIT(NL80211_PREAMBLE_HT) |
2066815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_VHT) |
2076815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_HE),
208fc36ffdaSJohannes Berg 	},
209fc36ffdaSJohannes Berg };
210fc36ffdaSJohannes Berg 
2116569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
212c56e00a3SJohannes Berg 				 enum set_key_cmd cmd,
213c56e00a3SJohannes Berg 				 struct ieee80211_vif *vif,
214c56e00a3SJohannes Berg 				 struct ieee80211_sta *sta,
215c56e00a3SJohannes Berg 				 struct ieee80211_key_conf *key);
216c56e00a3SJohannes Berg 
217e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
218e705c121SKalle Valo {
219e705c121SKalle Valo 	int i;
220e705c121SKalle Valo 
221e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
222e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
223e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
224e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
225e705c121SKalle Valo 	}
226e705c121SKalle Valo }
227e705c121SKalle Valo 
228e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
229e705c121SKalle Valo 						  const char *alpha2,
230e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
231e705c121SKalle Valo 						  bool *changed)
232e705c121SKalle Valo {
233e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
234e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
235e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
236e705c121SKalle Valo 	struct iwl_mcc_update_resp *resp;
237e27c506aSGil Adam 	u8 resp_ver;
238e705c121SKalle Valo 
239e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
240e705c121SKalle Valo 
241e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
242e705c121SKalle Valo 
243e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
244e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
245e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
246e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
247e705c121SKalle Valo 		goto out;
248e705c121SKalle Valo 	}
249e705c121SKalle Valo 
25082715ac7SEmmanuel Grumbach 	if (changed) {
25182715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
25282715ac7SEmmanuel Grumbach 
25382715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
25482715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
25582715ac7SEmmanuel Grumbach 	}
256e27c506aSGil Adam 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
257e27c506aSGil Adam 					   MCC_UPDATE_CMD, 0);
258e27c506aSGil Adam 	IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
259e705c121SKalle Valo 
260e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
261e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
262e705c121SKalle Valo 				      resp->channels,
26377e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
2642763bba6SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info),
265e27c506aSGil Adam 				      __le16_to_cpu(resp->cap), resp_ver);
266e705c121SKalle Valo 	/* Store the return source id */
267e705c121SKalle Valo 	src_id = resp->source_id;
268e705c121SKalle Valo 	kfree(resp);
269e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
270e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
271e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
272e705c121SKalle Valo 		goto out;
273e705c121SKalle Valo 	}
274e705c121SKalle Valo 
275e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
276e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
277e705c121SKalle Valo 	mvm->lar_regdom_set = true;
278e705c121SKalle Valo 	mvm->mcc_src = src_id;
279e705c121SKalle Valo 
280e705c121SKalle Valo out:
281e705c121SKalle Valo 	return regd;
282e705c121SKalle Valo }
283e705c121SKalle Valo 
284e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
285e705c121SKalle Valo {
286e705c121SKalle Valo 	bool changed;
287e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
288e705c121SKalle Valo 
289e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
290e705c121SKalle Valo 		return;
291e705c121SKalle Valo 
292e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
293e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
294e705c121SKalle Valo 		/* only update the regulatory core if changed */
295e705c121SKalle Valo 		if (changed)
296e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
297e705c121SKalle Valo 
298e705c121SKalle Valo 		kfree(regd);
299e705c121SKalle Valo 	}
300e705c121SKalle Valo }
301e705c121SKalle Valo 
302e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
303e705c121SKalle Valo 							  bool *changed)
304e705c121SKalle Valo {
305e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
306e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
307e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
308e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
309e705c121SKalle Valo }
310e705c121SKalle Valo 
311e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
312e705c121SKalle Valo {
313e705c121SKalle Valo 	enum iwl_mcc_source used_src;
314e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
315e705c121SKalle Valo 	int ret;
316e705c121SKalle Valo 	bool changed;
317e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
318e705c121SKalle Valo 			rtnl_dereference(mvm->hw->wiphy->regd);
319e705c121SKalle Valo 
320e705c121SKalle Valo 	if (!r)
321e705c121SKalle Valo 		return -ENOENT;
322e705c121SKalle Valo 
323e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
324e705c121SKalle Valo 	used_src = mvm->mcc_src;
325e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
326e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
327e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
328e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
329e705c121SKalle Valo 			kfree(regd);
330e705c121SKalle Valo 	}
331e705c121SKalle Valo 
332e705c121SKalle Valo 	/* Now set our last stored MCC and source */
333e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
334e705c121SKalle Valo 				     &changed);
335e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
336e705c121SKalle Valo 		return -EIO;
337e705c121SKalle Valo 
338e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
339e705c121SKalle Valo 	if (changed)
340e705c121SKalle Valo 		ret = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
341e705c121SKalle Valo 	else
342e705c121SKalle Valo 		ret = 0;
343e705c121SKalle Valo 
344e705c121SKalle Valo 	kfree(regd);
345e705c121SKalle Valo 	return ret;
346e705c121SKalle Valo }
347e705c121SKalle Valo 
3487f2ea521SYueHaibing static const u8 he_if_types_ext_capa_sta[] = {
3497360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
350918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
3517360f99eSEmmanuel Grumbach 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
3527360f99eSEmmanuel Grumbach 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
3537360f99eSEmmanuel Grumbach };
3547360f99eSEmmanuel Grumbach 
3557f2ea521SYueHaibing static const struct wiphy_iftype_ext_capab he_iftypes_ext_capa[] = {
3567360f99eSEmmanuel Grumbach 	{
3577360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
3587360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
3597360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
3607360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
3617360f99eSEmmanuel Grumbach 	},
3627360f99eSEmmanuel Grumbach };
3637360f99eSEmmanuel Grumbach 
364e8503aecSBen Greear static int
365e8503aecSBen Greear iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
366e8503aecSBen Greear {
367e8503aecSBen Greear 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
368e8503aecSBen Greear 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
369e8503aecSBen Greear 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
370e8503aecSBen Greear 	return 0;
371e8503aecSBen Greear }
372e8503aecSBen Greear 
373e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
374e705c121SKalle Valo {
375e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
376e705c121SKalle Valo 	int num_mac, ret, i;
377e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
378e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
379e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
380e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
381e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
382e705c121SKalle Valo 	};
3833f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
3843f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
3853f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3863f37c229SIdo Yariv #endif
387e705c121SKalle Valo 
388e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
389e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
390e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
391e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
392e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
393e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
394e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
395e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
396e705c121SKalle Valo 	ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
397e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
398e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
399e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
400e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
401909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
40230433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
403d270e7b8SIlan Peer 	ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
404520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
405cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
406cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
407cfb21b11SJohannes Berg 	/*
408cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
409cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
410cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
411cfb21b11SJohannes Berg 	 * assert EDC.
412cfb21b11SJohannes Berg 	 *
413cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
414cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
415cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
416cfb21b11SJohannes Berg 	 * devices.
417cfb21b11SJohannes Berg 	 */
4187d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
419438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
420438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
421ecaf71deSGregory Greenman 
4224243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
423ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
424ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
425ecaf71deSGregory Greenman 	}
426ecaf71deSGregory Greenman 
427b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
428b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
429960f864bSJohannes Berg 
430960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
431960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
43265e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
433960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
434960f864bSJohannes Berg 		/*
435960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
436960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
437960f864bSJohannes Berg 		 * for station powersave
438960f864bSJohannes Berg 		 */
439960f864bSJohannes Berg 		return -EINVAL;
440960f864bSJohannes Berg 	}
441e705c121SKalle Valo 
44280938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
44380938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
44480938abcSJohannes Berg 
4452d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
4462d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
4472d7cf549SJohannes Berg 
448cf961e16SLiad Kaufman 	hw->queues = IEEE80211_MAX_QUEUES;
449e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
450e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
451e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
452e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
453e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
454371a17edSJohannes Berg 
455371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
456371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
457371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
458371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
459371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
460371a17edSJohannes Berg 
4614243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
4629f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
4639f66a397SGregory Greenman 
464e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
465e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
466438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
467e705c121SKalle Valo 
4688e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
469e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
470e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
471e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
472e705c121SKalle Valo 
4732a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4742a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4752a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
4762a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4772a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4782a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
4792a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4802a53d166SAyala Beker 	}
4812a53d166SAyala Beker 
482f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
483f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
484f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
485f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
486f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
487f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
488f4bfdc5eSEmmanuel Grumbach 
489e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
490f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
491e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
4928e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4938e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4948e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
4958e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4968e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4978e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
4988e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4998e160ab8SAyala Beker 	}
500e705c121SKalle Valo 
501e705c121SKalle Valo 	/* currently FW API supports only one optional cipher scheme */
502e705c121SKalle Valo 	if (mvm->fw->cs[0].cipher) {
50324ddddf3SJohannes Berg 		const struct iwl_fw_cipher_scheme *fwcs = &mvm->fw->cs[0];
50424ddddf3SJohannes Berg 		struct ieee80211_cipher_scheme *cs = &mvm->cs[0];
50524ddddf3SJohannes Berg 
506e705c121SKalle Valo 		mvm->hw->n_cipher_schemes = 1;
50724ddddf3SJohannes Berg 
50824ddddf3SJohannes Berg 		cs->cipher = le32_to_cpu(fwcs->cipher);
50924ddddf3SJohannes Berg 		cs->iftype = BIT(NL80211_IFTYPE_STATION);
51024ddddf3SJohannes Berg 		cs->hdr_len = fwcs->hdr_len;
51124ddddf3SJohannes Berg 		cs->pn_len = fwcs->pn_len;
51224ddddf3SJohannes Berg 		cs->pn_off = fwcs->pn_off;
51324ddddf3SJohannes Berg 		cs->key_idx_off = fwcs->key_idx_off;
51424ddddf3SJohannes Berg 		cs->key_idx_mask = fwcs->key_idx_mask;
51524ddddf3SJohannes Berg 		cs->key_idx_shift = fwcs->key_idx_shift;
51624ddddf3SJohannes Berg 		cs->mic_len = fwcs->mic_len;
51724ddddf3SJohannes Berg 
51824ddddf3SJohannes Berg 		mvm->hw->cipher_schemes = mvm->cs;
51924ddddf3SJohannes Berg 		mvm->ciphers[hw->wiphy->n_cipher_suites] = cs->cipher;
520e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
521e705c121SKalle Valo 	}
522e705c121SKalle Valo 
523b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
524fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
525b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
526b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
527fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
528fc36ffdaSJohannes Berg 	}
529b73f9a4aSJohannes Berg 
530e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
531e705c121SKalle Valo 	hw->wiphy->features |=
532e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
533e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
534e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
535e705c121SKalle Valo 
536e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
537e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
538e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
539cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
540e705c121SKalle Valo 
541e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
542e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
543e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
544e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
545e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
546e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
547e705c121SKalle Valo 
548e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
549e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
55033e3fd99SAlexander Wetzel 
55133e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
55233e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
55333e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
55433e3fd99SAlexander Wetzel 	 */
55533e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
55633e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
55733e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
558e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
559e47df5bdSJohannes Berg 
560e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
561e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
562e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
563e705c121SKalle Valo 	else
564e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
565e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
566e705c121SKalle Valo 
567e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
568e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
569e705c121SKalle Valo 
570e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
571e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
572e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
573e705c121SKalle Valo 
574e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
575e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
576e705c121SKalle Valo 
577e705c121SKalle Valo 	/* Extract MAC address */
578e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
579e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
580e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
581e705c121SKalle Valo 
582e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
583e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
584e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
585e705c121SKalle Valo 
586e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
587e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
588e705c121SKalle Valo 		       ETH_ALEN);
589e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
590e705c121SKalle Valo 		hw->wiphy->n_addresses++;
591e705c121SKalle Valo 	}
592e705c121SKalle Valo 
593e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
594e705c121SKalle Valo 
595e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
596e705c121SKalle Valo 
597e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
598e705c121SKalle Valo 
599e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
600e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
601e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
602e705c121SKalle Valo 
603e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
604e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
605e705c121SKalle Valo 	else
606e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
607e705c121SKalle Valo 
60857fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
60957fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
61057fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
61157fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
61257fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
61357fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
614e705c121SKalle Valo 
615e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
616e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
617e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
618e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
61957fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
620e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
621e705c121SKalle Valo 	}
622e705c121SKalle Valo 
623e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
624e705c121SKalle Valo 
625e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
626e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
627e705c121SKalle Valo 	else
628e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
629e705c121SKalle Valo 
630ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
631e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
6325d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
633e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
634e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
635e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
636e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
637e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
638e705c121SKalle Valo 
639e705c121SKalle Valo 	/*
640e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
641e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
642e705c121SKalle Valo 	 */
643e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
644e705c121SKalle Valo 
645e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
646e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
647e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
648a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
649e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
650e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
651e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
652e705c121SKalle Valo 
653e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
654e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
655e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
656e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
657e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
658e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
659e705c121SKalle Valo 
660e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
661e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
662e705c121SKalle Valo 		hw->wiphy->features |=
663e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
664e705c121SKalle Valo 
665e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
666e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
667e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
668e705c121SKalle Valo 
6692a42aea7SNathan Errera 	if (iwl_fw_lookup_cmd_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
6702a42aea7SNathan Errera 				  WOWLAN_KEK_KCK_MATERIAL,
6712a42aea7SNathan Errera 				  IWL_FW_CMD_VER_UNKNOWN) == 3)
6722a42aea7SNathan Errera 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
6732a42aea7SNathan Errera 
674aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
675aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
676aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
677aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
678aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
679aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
680aacf8f18SAvrahams Stern 	}
681aacf8f18SAvrahams Stern 
6828f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
6838f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6848f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
6858f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6868f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
6878f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6888f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
6898f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6908f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
6918f691af9SZamir, Roee 	}
6928f691af9SZamir, Roee 
6937360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
6947360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
6957360f99eSEmmanuel Grumbach 		hw->wiphy->iftype_ext_capab = he_iftypes_ext_capa;
6967360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
6977360f99eSEmmanuel Grumbach 			ARRAY_SIZE(he_iftypes_ext_capa);
698918cbf39SSara Sharon 
699918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
700918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
7017360f99eSEmmanuel Grumbach 	}
7027360f99eSEmmanuel Grumbach 
703e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
704e705c121SKalle Valo 
705e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
7063f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
707e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
708e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
709e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
710e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
711e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
712e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
713e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
714e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
715e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
716e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
717e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
718e705c121SKalle Valo 
719e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
720e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
721e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
7225d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
7235d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
724e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
725e705c121SKalle Valo 	}
726e705c121SKalle Valo #endif
727e705c121SKalle Valo 
728e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
729e705c121SKalle Valo 	/* assign default bcast filtering configuration */
730e705c121SKalle Valo 	mvm->bcast_filters = iwl_mvm_default_bcast_filters;
731e705c121SKalle Valo #endif
732e705c121SKalle Valo 
733e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
734e705c121SKalle Valo 	if (ret)
735e705c121SKalle Valo 		return ret;
736e705c121SKalle Valo 
737e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
738e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
739e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
740e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
741e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
742e705c121SKalle Valo 	}
743e705c121SKalle Valo 
744e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
745e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
746e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
747e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
748e705c121SKalle Valo 	}
749e705c121SKalle Valo 
750e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
7515e6a98dcSSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm)) {
7525e6a98dcSSara Sharon 		hw->netdev_features &= ~(IWL_TX_CSUM_NETIF_FLAGS |
7535e6a98dcSSara Sharon 					 NETIF_F_RXCSUM);
7545e6a98dcSSara Sharon 		/* We may support SW TX CSUM */
75541837ca9SEmmanuel Grumbach 		if (IWL_MVM_SW_TX_CSUM_OFFLOAD)
7565e6a98dcSSara Sharon 			hw->netdev_features |= IWL_TX_CSUM_NETIF_FLAGS;
7575e6a98dcSSara Sharon 	}
75841837ca9SEmmanuel Grumbach 
75991b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
76091b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
76191b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
76291b08c2dSAviya Erenfeld 
7639c11d8a9SShaul Triebitz 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
7649c11d8a9SShaul Triebitz 		wiphy_ext_feature_set(hw->wiphy,
7659c11d8a9SShaul Triebitz 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
7669c11d8a9SShaul Triebitz 
767e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
768e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
769e8503aecSBen Greear 
770de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
771de645e89SJohannes Berg 	if (ret) {
772de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
773e705c121SKalle Valo 	}
774e705c121SKalle Valo 
775e705c121SKalle Valo 	return ret;
776e705c121SKalle Valo }
777e705c121SKalle Valo 
778df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
779df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
780df2378abSJohannes Berg {
781df2378abSJohannes Berg 	if (likely(sta)) {
782df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
783df2378abSJohannes Berg 			return;
784df2378abSJohannes Berg 	} else {
785df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
786df2378abSJohannes Berg 			return;
787df2378abSJohannes Berg 	}
788df2378abSJohannes Berg 
789df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
790df2378abSJohannes Berg }
791df2378abSJohannes Berg 
792e705c121SKalle Valo static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
793e705c121SKalle Valo 			   struct ieee80211_tx_control *control,
794e705c121SKalle Valo 			   struct sk_buff *skb)
795e705c121SKalle Valo {
796e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
797e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
798e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
799e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
800cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
801cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
802e705c121SKalle Valo 
803e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
804e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
805e705c121SKalle Valo 		goto drop;
806e705c121SKalle Valo 	}
807e705c121SKalle Valo 
808cfbc6c4cSSara Sharon 	if (offchannel &&
809e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
810e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
811e705c121SKalle Valo 		goto drop;
812e705c121SKalle Valo 
813eb045e6eSDavid Spinadel 	/* treat non-bufferable MMPDUs on AP interfaces as broadcast */
814eb045e6eSDavid Spinadel 	if ((info->control.vif->type == NL80211_IFTYPE_AP ||
815eb045e6eSDavid Spinadel 	     info->control.vif->type == NL80211_IFTYPE_ADHOC) &&
816e705c121SKalle Valo 	    ieee80211_is_mgmt(hdr->frame_control) &&
817eb045e6eSDavid Spinadel 	    !ieee80211_is_bufferable_mmpdu(hdr->frame_control))
818e705c121SKalle Valo 		sta = NULL;
819e705c121SKalle Valo 
820dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
821cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
822cfbc6c4cSSara Sharon 	    !offchannel) {
823dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
824dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
825dc1aca22SAndrei Otcheretianski 		u8 ap_sta_id = READ_ONCE(mvmvif->ap_sta_id);
826dc1aca22SAndrei Otcheretianski 
827be9ae34eSNathan Errera 		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
828dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
829dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
830dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
831dc1aca22SAndrei Otcheretianski 				goto drop;
832dc1aca22SAndrei Otcheretianski 		}
833dc1aca22SAndrei Otcheretianski 	}
834dc1aca22SAndrei Otcheretianski 
835df2378abSJohannes Berg 	iwl_mvm_tx_skb(mvm, skb, sta);
836e705c121SKalle Valo 	return;
837e705c121SKalle Valo  drop:
838e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
839e705c121SKalle Valo }
840e705c121SKalle Valo 
841cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
842e705c121SKalle Valo {
843cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
844cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
845cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
846cfbc6c4cSSara Sharon 
847fba8248eSSara Sharon 	/*
848fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
849fba8248eSSara Sharon 	 * taker all the work.
850fba8248eSSara Sharon 	 *
851fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
852fba8248eSSara Sharon 	 *
853fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
854fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
855fba8248eSSara Sharon 	 *
856fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
857fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
858fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
859fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
860fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
861fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
862fba8248eSSara Sharon 	 *
863fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
864fba8248eSSara Sharon 	 * check the queue, so do nothing.
865fba8248eSSara Sharon 	 */
866fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
867fba8248eSSara Sharon 		return;
868cfbc6c4cSSara Sharon 
869cfbc6c4cSSara Sharon 	rcu_read_lock();
870fba8248eSSara Sharon 	do {
871cfbc6c4cSSara Sharon 		while (likely(!mvmtxq->stopped &&
872cfbc6c4cSSara Sharon 			      (mvm->trans->system_pm_mode ==
873cfbc6c4cSSara Sharon 			       IWL_PLAT_PM_MODE_DISABLED))) {
874cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
875cfbc6c4cSSara Sharon 
876f50d693bSSara Sharon 			if (!skb) {
877f50d693bSSara Sharon 				if (txq->sta)
878f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
879f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
880f50d693bSSara Sharon 						     txq->sta->addr,
881f50d693bSSara Sharon 						     txq->tid);
882cfbc6c4cSSara Sharon 				break;
883f50d693bSSara Sharon 			}
884cfbc6c4cSSara Sharon 
885cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
886cfbc6c4cSSara Sharon 		}
887fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
888cfbc6c4cSSara Sharon 	rcu_read_unlock();
889e705c121SKalle Valo }
890e705c121SKalle Valo 
891cfbc6c4cSSara Sharon static void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
892cfbc6c4cSSara Sharon 				      struct ieee80211_txq *txq)
893e705c121SKalle Valo {
894cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
895cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
896e705c121SKalle Valo 
897cfbc6c4cSSara Sharon 	/*
898cfbc6c4cSSara Sharon 	 * Please note that racing is handled very carefully here:
899cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id is updated during allocation, and mvmtxq->list is
900cfbc6c4cSSara Sharon 	 * deleted afterwards.
901cfbc6c4cSSara Sharon 	 * This means that if:
902cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && list_empty(&mvmtxq->list):
903cfbc6c4cSSara Sharon 	 *	queue is allocated and we can TX.
904cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && !list_empty(&mvmtxq->list):
905cfbc6c4cSSara Sharon 	 *	a race, should defer the frame.
906cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && list_empty(&mvmtxq->list):
907cfbc6c4cSSara Sharon 	 *	need to allocate the queue and defer the frame.
908cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && !list_empty(&mvmtxq->list):
909cfbc6c4cSSara Sharon 	 *	queue is already scheduled for allocation, no need to allocate,
910cfbc6c4cSSara Sharon 	 *	should defer the frame.
911cfbc6c4cSSara Sharon 	 */
912cfbc6c4cSSara Sharon 
913cfbc6c4cSSara Sharon 	/* If the queue is allocated TX and return. */
914cfbc6c4cSSara Sharon 	if (!txq->sta || mvmtxq->txq_id != IWL_MVM_INVALID_QUEUE) {
915cfbc6c4cSSara Sharon 		/*
916cfbc6c4cSSara Sharon 		 * Check that list is empty to avoid a race where txq_id is
917cfbc6c4cSSara Sharon 		 * already updated, but the queue allocation work wasn't
918cfbc6c4cSSara Sharon 		 * finished
919cfbc6c4cSSara Sharon 		 */
920cfbc6c4cSSara Sharon 		if (unlikely(txq->sta && !list_empty(&mvmtxq->list)))
921cfbc6c4cSSara Sharon 			return;
922cfbc6c4cSSara Sharon 
923cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
924cfbc6c4cSSara Sharon 		return;
925cfbc6c4cSSara Sharon 	}
926cfbc6c4cSSara Sharon 
927cfbc6c4cSSara Sharon 	/* The list is being deleted only after the queue is fully allocated. */
928cfbc6c4cSSara Sharon 	if (!list_empty(&mvmtxq->list))
929cfbc6c4cSSara Sharon 		return;
930cfbc6c4cSSara Sharon 
931cfbc6c4cSSara Sharon 	list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
932cfbc6c4cSSara Sharon 	schedule_work(&mvm->add_stream_wk);
933e705c121SKalle Valo }
934e705c121SKalle Valo 
935e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
936e705c121SKalle Valo 	do {								\
937e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
938e705c121SKalle Valo 			break;						\
9397174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
940e705c121SKalle Valo 	} while (0)
941e705c121SKalle Valo 
942e705c121SKalle Valo static void
943e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
944e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
945e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
946e705c121SKalle Valo {
947e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
948e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
949e705c121SKalle Valo 
9506c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
9516c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
9526c042d75SSara Sharon 	if (!trig)
953e705c121SKalle Valo 		return;
954e705c121SKalle Valo 
955e705c121SKalle Valo 	ba_trig = (void *)trig->data;
956e705c121SKalle Valo 
957e705c121SKalle Valo 	switch (action) {
958e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
959e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
960e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
961e705c121SKalle Valo 
962e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
963e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
964e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
965e705c121SKalle Valo 		break;
966e705c121SKalle Valo 		}
967e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
968e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
969e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
970e705c121SKalle Valo 				 sta->addr, tid);
971e705c121SKalle Valo 		break;
972e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
973e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
974e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
975e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
976e705c121SKalle Valo 		break;
977e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
978e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
979e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
980e705c121SKalle Valo 				 sta->addr, tid);
981e705c121SKalle Valo 		break;
982e705c121SKalle Valo 	default:
983e705c121SKalle Valo 		break;
984e705c121SKalle Valo 	}
985e705c121SKalle Valo }
986e705c121SKalle Valo 
987e705c121SKalle Valo static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
988e705c121SKalle Valo 				    struct ieee80211_vif *vif,
98950ea05efSSara Sharon 				    struct ieee80211_ampdu_params *params)
990e705c121SKalle Valo {
991e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
992e705c121SKalle Valo 	int ret;
99350ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
99450ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
99550ea05efSSara Sharon 	u16 tid = params->tid;
99650ea05efSSara Sharon 	u16 *ssn = &params->ssn;
997514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
998bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
99910b2b201SSara Sharon 	u16 timeout = params->timeout;
1000e705c121SKalle Valo 
1001e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
1002e705c121SKalle Valo 		     sta->addr, tid, action);
1003e705c121SKalle Valo 
1004e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
1005e705c121SKalle Valo 		return -EACCES;
1006e705c121SKalle Valo 
1007e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1008e705c121SKalle Valo 
1009e705c121SKalle Valo 	switch (action) {
1010e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
1011b0ffe455SJohannes Berg 		if (iwl_mvm_vif_from_mac80211(vif)->ap_sta_id ==
1012b0ffe455SJohannes Berg 				iwl_mvm_sta_from_mac80211(sta)->sta_id) {
1013b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
1014b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
1015b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
1016b0ffe455SJohannes Berg 
1017b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
1018b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
1019b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
1020b0ffe455SJohannes Berg 		}
1021e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
1022e705c121SKalle Valo 			ret = -EINVAL;
1023e705c121SKalle Valo 			break;
1024e705c121SKalle Valo 		}
102510b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
102610b2b201SSara Sharon 					 timeout);
1027e705c121SKalle Valo 		break;
1028e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
102910b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
103010b2b201SSara Sharon 					 timeout);
1031e705c121SKalle Valo 		break;
1032e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
1033e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
1034e705c121SKalle Valo 			ret = -EINVAL;
1035e705c121SKalle Valo 			break;
1036e705c121SKalle Valo 		}
1037e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1038e705c121SKalle Valo 		break;
1039e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
1040e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1041e705c121SKalle Valo 		break;
1042e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1043e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1044e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1045e705c121SKalle Valo 		break;
1046e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1047bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1048bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
1049e705c121SKalle Valo 		break;
1050e705c121SKalle Valo 	default:
1051e705c121SKalle Valo 		WARN_ON_ONCE(1);
1052e705c121SKalle Valo 		ret = -EINVAL;
1053e705c121SKalle Valo 		break;
1054e705c121SKalle Valo 	}
1055e705c121SKalle Valo 
1056e705c121SKalle Valo 	if (!ret) {
1057e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
1058e705c121SKalle Valo 
1059e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1060e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1061e705c121SKalle Valo 
1062e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1063e705c121SKalle Valo 					    rx_ba_ssn, action);
1064e705c121SKalle Valo 	}
1065e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1066e705c121SKalle Valo 
1067e705c121SKalle Valo 	return ret;
1068e705c121SKalle Valo }
1069e705c121SKalle Valo 
1070e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1071e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1072e705c121SKalle Valo {
1073e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1074e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1075e705c121SKalle Valo 
1076e705c121SKalle Valo 	mvmvif->uploaded = false;
10770ae98812SSara Sharon 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1078e705c121SKalle Valo 
1079e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1080e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1081e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1082e705c121SKalle Valo 
1083e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1084e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
108586e177d8SGregory Greenman 	memset(&mvmvif->probe_resp_data, 0, sizeof(mvmvif->probe_resp_data));
1086e705c121SKalle Valo }
1087e705c121SKalle Valo 
1088e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1089e705c121SKalle Valo {
1090fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1091e705c121SKalle Valo 
10929bf13beeSJohannes Berg 	mvm->cur_aid = 0;
10939bf13beeSJohannes Berg 
1094e705c121SKalle Valo 	mvm->scan_status = 0;
1095e705c121SKalle Valo 	mvm->ps_disabled = false;
1096b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1097e705c121SKalle Valo 
1098e705c121SKalle Valo 	/* just in case one was running */
1099305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1100e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1101e705c121SKalle Valo 
1102fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1103fc36ffdaSJohannes Berg 
1104e705c121SKalle Valo 	/*
1105e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1106e705c121SKalle Valo 	 * gone down during the HW restart
1107e705c121SKalle Valo 	 */
1108e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1109e705c121SKalle Valo 
1110e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1111e705c121SKalle Valo 
1112e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
11139c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1114e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1115e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1116e705c121SKalle Valo 
1117e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1118e705c121SKalle Valo 
1119e705c121SKalle Valo 	mvm->vif_count = 0;
1120e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
11217174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1122baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1123e705c121SKalle Valo 
1124e705c121SKalle Valo 	/* keep statistics ticking */
1125e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1126e705c121SKalle Valo }
1127e705c121SKalle Valo 
1128e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1129e705c121SKalle Valo {
1130e705c121SKalle Valo 	int ret;
1131e705c121SKalle Valo 
1132e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1133e705c121SKalle Valo 
1134bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1135bf8b286fSJohannes Berg 		/*
1136bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1137bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1138bf8b286fSJohannes Berg 		 */
1139bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1140bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1141e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1142e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1143a42b2af3SLuca Coelho 	}
1144e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1145e705c121SKalle Valo 
1146b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1147b108d8c7SShahar S Matityahu 			       NULL);
1148b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1149b108d8c7SShahar S Matityahu 			       NULL);
1150da2eb669SSara Sharon 
1151e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1152e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1153e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1154e705c121SKalle Valo 		 * would do.
1155e705c121SKalle Valo 		 */
1156e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1157e705c121SKalle Valo 	}
1158e705c121SKalle Valo 
1159e705c121SKalle Valo 	return ret;
1160e705c121SKalle Valo }
1161e705c121SKalle Valo 
1162e705c121SKalle Valo static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1163e705c121SKalle Valo {
1164e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1165e705c121SKalle Valo 	int ret;
1166e705c121SKalle Valo 
1167e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1168e705c121SKalle Valo 	ret = __iwl_mvm_mac_start(mvm);
1169e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1170e705c121SKalle Valo 
1171e705c121SKalle Valo 	return ret;
1172e705c121SKalle Valo }
1173e705c121SKalle Valo 
1174e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1175e705c121SKalle Valo {
1176e705c121SKalle Valo 	int ret;
1177e705c121SKalle Valo 
1178e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1179e705c121SKalle Valo 
1180e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
11814d4183c4SEmmanuel Grumbach 
1182e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1183e705c121SKalle Valo 	if (ret)
1184e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1185e705c121SKalle Valo 			ret);
1186e705c121SKalle Valo 
1187f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1188f130bb75SMordechay Goodstein 
1189e705c121SKalle Valo 	/*
1190e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1191e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1192e705c121SKalle Valo 	 */
1193e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1194e705c121SKalle Valo 
1195e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1196e705c121SKalle Valo }
1197e705c121SKalle Valo 
1198e705c121SKalle Valo static void
1199e705c121SKalle Valo iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1200e705c121SKalle Valo 			      enum ieee80211_reconfig_type reconfig_type)
1201e705c121SKalle Valo {
1202e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1203e705c121SKalle Valo 
1204e705c121SKalle Valo 	switch (reconfig_type) {
1205e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1206e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1207e705c121SKalle Valo 		break;
1208e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1209e705c121SKalle Valo 		break;
1210e705c121SKalle Valo 	}
1211e705c121SKalle Valo }
1212e705c121SKalle Valo 
1213e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1214e705c121SKalle Valo {
1215e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1216e705c121SKalle Valo 
1217b68bd2e3SIlan Peer 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1218b68bd2e3SIlan Peer 
1219e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1220e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1221e705c121SKalle Valo 	 */
1222e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1223e705c121SKalle Valo 
1224e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1225e705c121SKalle Valo 
12262c2c3647SNathan Errera 	if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, ADD_STA, 0) < 12)
1227f327236dSSharon 		iwl_mvm_rm_aux_sta(mvm);
1228f327236dSSharon 
1229fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1230e705c121SKalle Valo 
1231e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1232e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1233e705c121SKalle Valo 
1234e705c121SKalle Valo 	/*
1235155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1236155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1237155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1238e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1239e705c121SKalle Valo 	 * HW restart was requested.
1240e705c121SKalle Valo 	 */
1241155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1242155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1243155f7e04SEmmanuel Grumbach 			       &mvm->status))
1244e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1245e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1246e705c121SKalle Valo 
1247e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1248e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1249e705c121SKalle Valo 	 */
1250e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1251e705c121SKalle Valo 		int i;
1252e705c121SKalle Valo 
1253e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1254e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1255e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1256e705c121SKalle Valo 				      i))
1257e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1258e705c121SKalle Valo 		}
1259e705c121SKalle Valo 	}
1260e705c121SKalle Valo }
1261e705c121SKalle Valo 
1262e705c121SKalle Valo static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1263e705c121SKalle Valo {
1264e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1265e705c121SKalle Valo 
1266e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
126724afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1268771147b0SJohannes Berg 
1269771147b0SJohannes Berg 	/*
1270771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1271771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1272771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
12737174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1274771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1275771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1276771147b0SJohannes Berg 	 */
1277771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1278771147b0SJohannes Berg 
1279d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
128069e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1281e705c121SKalle Valo 
1282f9084775SNathan Errera 	/*
1283f9084775SNathan Errera 	 * The work item could be running or queued if the
1284f9084775SNathan Errera 	 * ROC time event stops just as we get here.
1285f9084775SNathan Errera 	 */
1286f9084775SNathan Errera 	flush_work(&mvm->roc_done_wk);
1287f9084775SNathan Errera 
1288e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1289e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1290e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1291e705c121SKalle Valo 
1292e705c121SKalle Valo 	/*
1293e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1294e705c121SKalle Valo 	 * discover that its list is now empty.
1295e705c121SKalle Valo 	 */
1296e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1297e705c121SKalle Valo }
1298e705c121SKalle Valo 
1299e705c121SKalle Valo static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1300e705c121SKalle Valo {
1301e705c121SKalle Valo 	u16 i;
1302e705c121SKalle Valo 
1303e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1304e705c121SKalle Valo 
1305e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1306e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1307e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1308e705c121SKalle Valo 
1309e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1310e705c121SKalle Valo 	return NULL;
1311e705c121SKalle Valo }
1312e705c121SKalle Valo 
1313e705c121SKalle Valo static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1314e705c121SKalle Valo 				s16 tx_power)
1315e705c121SKalle Valo {
13160791c2fcSHaim Dreyfuss 	int len;
1317216cdfb5SLuca Coelho 	struct iwl_dev_tx_power_cmd cmd = {
1318216cdfb5SLuca Coelho 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1319216cdfb5SLuca Coelho 		.common.mac_context_id =
1320e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1321216cdfb5SLuca Coelho 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1322e705c121SKalle Valo 	};
1323fbb7957dSLuca Coelho 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
1324e80bfd11SMordechay Goodstein 					   REDUCE_TX_POWER_CMD,
1325e80bfd11SMordechay Goodstein 					   IWL_FW_CMD_VER_UNKNOWN);
1326e705c121SKalle Valo 
1327e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1328216cdfb5SLuca Coelho 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1329e705c121SKalle Valo 
1330fbb7957dSLuca Coelho 	if (cmd_ver == 6)
1331fbb7957dSLuca Coelho 		len = sizeof(cmd.v6);
1332fbb7957dSLuca Coelho 	else if (fw_has_api(&mvm->fw->ucode_capa,
13330791c2fcSHaim Dreyfuss 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
13340791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
13350791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
13360791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
13370791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
13380791c2fcSHaim Dreyfuss 	else
1339216cdfb5SLuca Coelho 		len = sizeof(cmd.v3);
1340216cdfb5SLuca Coelho 
1341216cdfb5SLuca Coelho 	/* all structs have the same common part, add it */
1342216cdfb5SLuca Coelho 	len += sizeof(cmd.common);
1343e705c121SKalle Valo 
1344e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
1345e705c121SKalle Valo }
1346e705c121SKalle Valo 
1347f6780614SSara Sharon static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1348f6780614SSara Sharon 				       struct ieee80211_vif *vif)
1349f6780614SSara Sharon {
1350f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1351f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1352f6780614SSara Sharon 	int ret;
1353f6780614SSara Sharon 
1354f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1355f6780614SSara Sharon 
1356f6780614SSara Sharon 	if (mvmvif->csa_failed) {
1357f6780614SSara Sharon 		mvmvif->csa_failed = false;
1358f6780614SSara Sharon 		ret = -EIO;
1359f6780614SSara Sharon 		goto out_unlock;
1360f6780614SSara Sharon 	}
1361f6780614SSara Sharon 
1362f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1363f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
1364f6780614SSara Sharon 
1365f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
1366f6780614SSara Sharon 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
1367f6780614SSara Sharon 							  mvmvif->ap_sta_id);
1368f6780614SSara Sharon 
1369f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1370f6780614SSara Sharon 			ret = -EIO;
1371f6780614SSara Sharon 			goto out_unlock;
1372f6780614SSara Sharon 		}
1373f6780614SSara Sharon 
1374f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1375f6780614SSara Sharon 
1376f6780614SSara Sharon 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1377f6780614SSara Sharon 
13780202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
13790202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1380f6780614SSara Sharon 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1381f6780614SSara Sharon 			if (ret)
1382f6780614SSara Sharon 				goto out_unlock;
1383f6780614SSara Sharon 
1384f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1385f6780614SSara Sharon 		}
13860202bcf0SEmmanuel Grumbach 	}
1387f6780614SSara Sharon 
1388f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1389f6780614SSara Sharon 
1390f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1391f6780614SSara Sharon 
1392f6780614SSara Sharon out_unlock:
1393f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1394f6780614SSara Sharon 
1395f6780614SSara Sharon 	return ret;
1396f6780614SSara Sharon }
1397f6780614SSara Sharon 
1398f6780614SSara Sharon static void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1399f6780614SSara Sharon 					 struct ieee80211_vif *vif)
1400f6780614SSara Sharon {
1401f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1402f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1403f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1404f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1405f6780614SSara Sharon 							  mvmvif->color)),
1406f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1407f6780614SSara Sharon 	};
1408f6780614SSara Sharon 
1409f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1410f6780614SSara Sharon 
1411f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
141258ddd9b6SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
141358ddd9b6SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
141458ddd9b6SEmmanuel Grumbach 		iwl_mvm_remove_csa_period(mvm, vif);
141558ddd9b6SEmmanuel Grumbach 	else
1416f6780614SSara Sharon 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1417f6780614SSara Sharon 					     WIDE_ID(MAC_CONF_GROUP,
1418f6780614SSara Sharon 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1419f6780614SSara Sharon 					     0, sizeof(cmd), &cmd));
1420f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1421f6780614SSara Sharon 
1422f6780614SSara Sharon 	WARN_ON(iwl_mvm_post_channel_switch(hw, vif));
1423f6780614SSara Sharon }
1424f6780614SSara Sharon 
1425f6780614SSara Sharon static void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1426f6780614SSara Sharon {
1427f6780614SSara Sharon 	struct iwl_mvm *mvm;
1428f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1429f6780614SSara Sharon 	struct ieee80211_vif *vif;
1430f6780614SSara Sharon 
1431f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1432f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1433f6780614SSara Sharon 	mvm = mvmvif->mvm;
1434f6780614SSara Sharon 
1435f6780614SSara Sharon 	iwl_mvm_abort_channel_switch(mvm->hw, vif);
1436f6780614SSara Sharon 	ieee80211_chswitch_done(vif, false);
1437f6780614SSara Sharon }
1438f6780614SSara Sharon 
1439e705c121SKalle Valo static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1440e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1441e705c121SKalle Valo {
1442e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1443e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1444e705c121SKalle Valo 	int ret;
1445e705c121SKalle Valo 
1446e705c121SKalle Valo 	mvmvif->mvm = mvm;
144786e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
1448e705c121SKalle Valo 
1449e705c121SKalle Valo 	/*
1450e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1451e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1452e705c121SKalle Valo 	 * don't really have to check the types.
1453e705c121SKalle Valo 	 */
1454e705c121SKalle Valo 
1455e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1456e705c121SKalle Valo 
1457e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1458e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1459e705c121SKalle Valo 		mvmvif->beacon_stats.accu_num_beacons +=
1460e705c121SKalle Valo 			mvmvif->beacon_stats.num_beacons;
1461e705c121SKalle Valo 
1462e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
1463e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1464e705c121SKalle Valo 	if (ret)
1465e705c121SKalle Valo 		goto out_unlock;
1466e705c121SKalle Valo 
1467698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1468698478c4SSara Sharon 
1469e705c121SKalle Valo 	/* Counting number of interfaces is needed for legacy PM */
1470e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1471e705c121SKalle Valo 		mvm->vif_count++;
1472e705c121SKalle Valo 
1473e705c121SKalle Valo 	/*
1474e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1475e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1476e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1477e705c121SKalle Valo 	 * only after the binding.
1478e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1479e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1480e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1481e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1482e705c121SKalle Valo 	 * allocating resources :)
1483e705c121SKalle Valo 	 */
1484e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1485e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1486e705c121SKalle Valo 		ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1487e705c121SKalle Valo 		if (ret) {
1488e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1489e705c121SKalle Valo 			goto out_release;
1490e705c121SKalle Valo 		}
1491e705c121SKalle Valo 
149226d6c16bSSara Sharon 		/*
149326d6c16bSSara Sharon 		 * Only queue for this station is the mcast queue,
149426d6c16bSSara Sharon 		 * which shouldn't be in TFD mask anyway
149526d6c16bSSara Sharon 		 */
149626d6c16bSSara Sharon 		ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta,
1497ced19f26SSara Sharon 					       0, vif->type,
1498ced19f26SSara Sharon 					       IWL_STA_MULTICAST);
149926d6c16bSSara Sharon 		if (ret)
150026d6c16bSSara Sharon 			goto out_release;
150126d6c16bSSara Sharon 
1502e705c121SKalle Valo 		iwl_mvm_vif_dbgfs_register(mvm, vif);
1503e705c121SKalle Valo 		goto out_unlock;
1504e705c121SKalle Valo 	}
1505e705c121SKalle Valo 
1506e705c121SKalle Valo 	mvmvif->features |= hw->netdev_features;
1507e705c121SKalle Valo 
1508e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1509e705c121SKalle Valo 	if (ret)
1510e705c121SKalle Valo 		goto out_release;
1511e705c121SKalle Valo 
1512e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1513e705c121SKalle Valo 	if (ret)
1514e705c121SKalle Valo 		goto out_remove_mac;
1515e705c121SKalle Valo 
1516e705c121SKalle Valo 	/* beacon filtering */
1517e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1518e705c121SKalle Valo 	if (ret)
1519e705c121SKalle Valo 		goto out_remove_mac;
1520e705c121SKalle Valo 
1521e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1522e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1523e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1524e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1525e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1526e705c121SKalle Valo 	}
1527e705c121SKalle Valo 
1528e705c121SKalle Valo 	/*
1529e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1530e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1531e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1532e705c121SKalle Valo 	 */
1533e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1534e705c121SKalle Valo 
1535e705c121SKalle Valo 		mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1536e705c121SKalle Valo 		if (!mvmvif->phy_ctxt) {
1537e705c121SKalle Valo 			ret = -ENOSPC;
1538e705c121SKalle Valo 			goto out_free_bf;
1539e705c121SKalle Valo 		}
1540e705c121SKalle Valo 
1541e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1542e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1543e705c121SKalle Valo 		if (ret)
1544e705c121SKalle Valo 			goto out_unref_phy;
1545e705c121SKalle Valo 
1546d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1547e705c121SKalle Valo 		if (ret)
1548e705c121SKalle Valo 			goto out_unbind;
1549e705c121SKalle Valo 
1550e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1551e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1552e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1553e705c121SKalle Valo 	}
1554e705c121SKalle Valo 
1555b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1556f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1557f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1558b0ffe455SJohannes Berg 
1559baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1560baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
1561baf41bc3SShaul Triebitz 
1562e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
1563e705c121SKalle Valo 	goto out_unlock;
1564e705c121SKalle Valo 
1565e705c121SKalle Valo  out_unbind:
1566e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1567e705c121SKalle Valo  out_unref_phy:
1568e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1569e705c121SKalle Valo  out_free_bf:
1570e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1571e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1572e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1573e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1574e705c121SKalle Valo 	}
1575e705c121SKalle Valo  out_remove_mac:
1576e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1577e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1578e705c121SKalle Valo  out_release:
1579e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1580e705c121SKalle Valo 		mvm->vif_count--;
1581e705c121SKalle Valo  out_unlock:
1582e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1583e705c121SKalle Valo 
1584e705c121SKalle Valo 	return ret;
1585e705c121SKalle Valo }
1586e705c121SKalle Valo 
1587e705c121SKalle Valo static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1588e705c121SKalle Valo 					struct ieee80211_vif *vif)
1589e705c121SKalle Valo {
1590e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1591e705c121SKalle Valo 		/*
1592e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1593e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1594e705c121SKalle Valo 		 * queue are sent in ROC session.
1595e705c121SKalle Valo 		 */
1596e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1597e705c121SKalle Valo 	}
1598e705c121SKalle Valo }
1599e705c121SKalle Valo 
1600e705c121SKalle Valo static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1601e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1602e705c121SKalle Valo {
1603e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1604e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
160586e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1606e705c121SKalle Valo 
1607e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1608e705c121SKalle Valo 
1609b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1610b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1611b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1612b0ffe455SJohannes Berg 
1613e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1614e705c121SKalle Valo 
161586e177d8SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->probe_resp_data,
161686e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
161786e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
161886e177d8SGregory Greenman 	if (probe_data)
161986e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
162086e177d8SGregory Greenman 
1621e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1622e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1623e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1624e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1625e705c121SKalle Valo 	}
1626e705c121SKalle Valo 
1627b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1628b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1629b73f9a4aSJohannes Berg 
1630e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1631e705c121SKalle Valo 
1632e705c121SKalle Valo 	/*
1633e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1634e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1635e705c121SKalle Valo 	 */
1636e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1637e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1638e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1639e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1640e705c121SKalle Valo 			mvm->noa_vif = NULL;
1641e705c121SKalle Valo 			mvm->noa_duration = 0;
1642e705c121SKalle Valo 		}
1643e705c121SKalle Valo #endif
164426d6c16bSSara Sharon 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
1645e705c121SKalle Valo 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1646e705c121SKalle Valo 		goto out_release;
1647e705c121SKalle Valo 	}
1648e705c121SKalle Valo 
1649e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1650e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1651d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1652e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1653e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1654e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
1655e705c121SKalle Valo 	}
1656e705c121SKalle Valo 
1657e705c121SKalle Valo 	if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1658e705c121SKalle Valo 		mvm->vif_count--;
1659e705c121SKalle Valo 
1660e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
1661e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1662e705c121SKalle Valo 
1663698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1664698478c4SSara Sharon 
1665baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1666baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1667baf41bc3SShaul Triebitz 
1668e705c121SKalle Valo out_release:
1669e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1670e705c121SKalle Valo }
1671e705c121SKalle Valo 
1672e705c121SKalle Valo static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1673e705c121SKalle Valo {
1674e705c121SKalle Valo 	return 0;
1675e705c121SKalle Valo }
1676e705c121SKalle Valo 
1677e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1678e705c121SKalle Valo 	struct iwl_mvm *mvm;
1679e705c121SKalle Valo 	int port_id;
1680e705c121SKalle Valo };
1681e705c121SKalle Valo 
1682e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1683e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1684e705c121SKalle Valo {
1685e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1686e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1687e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
168897bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
168997bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
169097bce57bSLuca Coelho 		.flags = CMD_ASYNC,
169197bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
169297bce57bSLuca Coelho 	};
1693e705c121SKalle Valo 	int ret, len;
1694e705c121SKalle Valo 
1695e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1696e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1697e705c121SKalle Valo 		return;
1698e705c121SKalle Valo 
1699e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1700e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1701e705c121SKalle Valo 		return;
1702e705c121SKalle Valo 
1703e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1704e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1705e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1706e705c121SKalle Valo 
170797bce57bSLuca Coelho 	hcmd.len[0] = len;
170897bce57bSLuca Coelho 	hcmd.data[0] = cmd;
170997bce57bSLuca Coelho 
171097bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1711e705c121SKalle Valo 	if (ret)
1712e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1713e705c121SKalle Valo }
1714e705c121SKalle Valo 
1715e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1716e705c121SKalle Valo {
1717e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1718e705c121SKalle Valo 		.mvm = mvm,
1719e705c121SKalle Valo 	};
1720e705c121SKalle Valo 
1721e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1722e705c121SKalle Valo 
1723e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1724e705c121SKalle Valo 		return;
1725e705c121SKalle Valo 
1726e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1727e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1728e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1729e705c121SKalle Valo }
1730e705c121SKalle Valo 
1731e705c121SKalle Valo static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1732e705c121SKalle Valo 				     struct netdev_hw_addr_list *mc_list)
1733e705c121SKalle Valo {
1734e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1735e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1736e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1737e705c121SKalle Valo 	int addr_count;
1738e705c121SKalle Valo 	bool pass_all;
1739e705c121SKalle Valo 	int len;
1740e705c121SKalle Valo 
1741e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1742e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1743e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1744e705c121SKalle Valo 	if (pass_all)
1745e705c121SKalle Valo 		addr_count = 0;
1746e705c121SKalle Valo 
1747e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1748e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1749e705c121SKalle Valo 	if (!cmd)
1750e705c121SKalle Valo 		return 0;
1751e705c121SKalle Valo 
1752e705c121SKalle Valo 	if (pass_all) {
1753e705c121SKalle Valo 		cmd->pass_all = 1;
1754e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1755e705c121SKalle Valo 	}
1756e705c121SKalle Valo 
1757e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1758e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1759e705c121SKalle Valo 				   cmd->count, addr->addr);
1760e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1761e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1762e705c121SKalle Valo 		cmd->count++;
1763e705c121SKalle Valo 	}
1764e705c121SKalle Valo 
1765e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1766e705c121SKalle Valo }
1767e705c121SKalle Valo 
1768e705c121SKalle Valo static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1769e705c121SKalle Valo 				     unsigned int changed_flags,
1770e705c121SKalle Valo 				     unsigned int *total_flags,
1771e705c121SKalle Valo 				     u64 multicast)
1772e705c121SKalle Valo {
1773e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1774e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1775e705c121SKalle Valo 
1776e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1777e705c121SKalle Valo 
1778e705c121SKalle Valo 	/* replace previous configuration */
1779e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1780e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1781e705c121SKalle Valo 
1782e705c121SKalle Valo 	if (!cmd)
1783e705c121SKalle Valo 		goto out;
1784e705c121SKalle Valo 
178561e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
178661e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
178761e7d91bSLuca Coelho 
178861e7d91bSLuca Coelho 	if (cmd->pass_all)
178961e7d91bSLuca Coelho 		cmd->count = 0;
179061e7d91bSLuca Coelho 
1791e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1792e705c121SKalle Valo out:
1793e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1794e705c121SKalle Valo 	*total_flags = 0;
1795e705c121SKalle Valo }
1796e705c121SKalle Valo 
1797e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1798e705c121SKalle Valo 					struct ieee80211_vif *vif,
1799e705c121SKalle Valo 					unsigned int filter_flags,
1800e705c121SKalle Valo 					unsigned int changed_flags)
1801e705c121SKalle Valo {
1802e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1803e705c121SKalle Valo 
1804e705c121SKalle Valo 	/* We support only filter for probe requests */
1805e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1806e705c121SKalle Valo 		return;
1807e705c121SKalle Valo 
1808e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1809e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1810e705c121SKalle Valo 	    !vif->p2p)
1811e705c121SKalle Valo 		return;
1812e705c121SKalle Valo 
1813e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1814e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1815e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1816e705c121SKalle Valo }
1817e705c121SKalle Valo 
1818e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1819e705c121SKalle Valo struct iwl_bcast_iter_data {
1820e705c121SKalle Valo 	struct iwl_mvm *mvm;
1821e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd;
1822e705c121SKalle Valo 	u8 current_filter;
1823e705c121SKalle Valo };
1824e705c121SKalle Valo 
1825e705c121SKalle Valo static void
1826e705c121SKalle Valo iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1827e705c121SKalle Valo 			 const struct iwl_fw_bcast_filter *in_filter,
1828e705c121SKalle Valo 			 struct iwl_fw_bcast_filter *out_filter)
1829e705c121SKalle Valo {
1830e705c121SKalle Valo 	struct iwl_fw_bcast_filter_attr *attr;
1831e705c121SKalle Valo 	int i;
1832e705c121SKalle Valo 
1833e705c121SKalle Valo 	memcpy(out_filter, in_filter, sizeof(*out_filter));
1834e705c121SKalle Valo 
1835e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1836e705c121SKalle Valo 		attr = &out_filter->attrs[i];
1837e705c121SKalle Valo 
1838e705c121SKalle Valo 		if (!attr->mask)
1839e705c121SKalle Valo 			break;
1840e705c121SKalle Valo 
1841e705c121SKalle Valo 		switch (attr->reserved1) {
1842e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_IP):
1843e705c121SKalle Valo 			if (vif->bss_conf.arp_addr_cnt != 1) {
1844e705c121SKalle Valo 				attr->mask = 0;
1845e705c121SKalle Valo 				continue;
1846e705c121SKalle Valo 			}
1847e705c121SKalle Valo 
1848e705c121SKalle Valo 			attr->val = vif->bss_conf.arp_addr_list[0];
1849e705c121SKalle Valo 			break;
1850e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1851e705c121SKalle Valo 			attr->val = *(__be32 *)&vif->addr[2];
1852e705c121SKalle Valo 			break;
1853e705c121SKalle Valo 		default:
1854e705c121SKalle Valo 			break;
1855e705c121SKalle Valo 		}
1856e705c121SKalle Valo 		attr->reserved1 = 0;
1857e705c121SKalle Valo 		out_filter->num_attrs++;
1858e705c121SKalle Valo 	}
1859e705c121SKalle Valo }
1860e705c121SKalle Valo 
1861e705c121SKalle Valo static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1862e705c121SKalle Valo 					  struct ieee80211_vif *vif)
1863e705c121SKalle Valo {
1864e705c121SKalle Valo 	struct iwl_bcast_iter_data *data = _data;
1865e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1866e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd = data->cmd;
1867e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1868e705c121SKalle Valo 	struct iwl_fw_bcast_mac *bcast_mac;
1869e705c121SKalle Valo 	int i;
1870e705c121SKalle Valo 
1871e705c121SKalle Valo 	if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1872e705c121SKalle Valo 		return;
1873e705c121SKalle Valo 
1874e705c121SKalle Valo 	bcast_mac = &cmd->macs[mvmvif->id];
1875e705c121SKalle Valo 
1876e705c121SKalle Valo 	/*
1877e705c121SKalle Valo 	 * enable filtering only for associated stations, but not for P2P
1878e705c121SKalle Valo 	 * Clients
1879e705c121SKalle Valo 	 */
1880e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1881e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1882e705c121SKalle Valo 		return;
1883e705c121SKalle Valo 
1884e705c121SKalle Valo 	bcast_mac->default_discard = 1;
1885e705c121SKalle Valo 
1886e705c121SKalle Valo 	/* copy all configured filters */
1887e705c121SKalle Valo 	for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1888e705c121SKalle Valo 		/*
1889e705c121SKalle Valo 		 * Make sure we don't exceed our filters limit.
1890e705c121SKalle Valo 		 * if there is still a valid filter to be configured,
1891e705c121SKalle Valo 		 * be on the safe side and just allow bcast for this mac.
1892e705c121SKalle Valo 		 */
1893e705c121SKalle Valo 		if (WARN_ON_ONCE(data->current_filter >=
1894e705c121SKalle Valo 				 ARRAY_SIZE(cmd->filters))) {
1895e705c121SKalle Valo 			bcast_mac->default_discard = 0;
1896e705c121SKalle Valo 			bcast_mac->attached_filters = 0;
1897e705c121SKalle Valo 			break;
1898e705c121SKalle Valo 		}
1899e705c121SKalle Valo 
1900e705c121SKalle Valo 		iwl_mvm_set_bcast_filter(vif,
1901e705c121SKalle Valo 					 &mvm->bcast_filters[i],
1902e705c121SKalle Valo 					 &cmd->filters[data->current_filter]);
1903e705c121SKalle Valo 
1904e705c121SKalle Valo 		/* skip current filter if it contains no attributes */
1905e705c121SKalle Valo 		if (!cmd->filters[data->current_filter].num_attrs)
1906e705c121SKalle Valo 			continue;
1907e705c121SKalle Valo 
1908e705c121SKalle Valo 		/* attach the filter to current mac */
1909e705c121SKalle Valo 		bcast_mac->attached_filters |=
1910e705c121SKalle Valo 				cpu_to_le16(BIT(data->current_filter));
1911e705c121SKalle Valo 
1912e705c121SKalle Valo 		data->current_filter++;
1913e705c121SKalle Valo 	}
1914e705c121SKalle Valo }
1915e705c121SKalle Valo 
1916e705c121SKalle Valo bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1917e705c121SKalle Valo 				    struct iwl_bcast_filter_cmd *cmd)
1918e705c121SKalle Valo {
1919e705c121SKalle Valo 	struct iwl_bcast_iter_data iter_data = {
1920e705c121SKalle Valo 		.mvm = mvm,
1921e705c121SKalle Valo 		.cmd = cmd,
1922e705c121SKalle Valo 	};
1923e705c121SKalle Valo 
1924e705c121SKalle Valo 	if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1925e705c121SKalle Valo 		return false;
1926e705c121SKalle Valo 
1927e705c121SKalle Valo 	memset(cmd, 0, sizeof(*cmd));
1928e705c121SKalle Valo 	cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1929e705c121SKalle Valo 	cmd->max_macs = ARRAY_SIZE(cmd->macs);
1930e705c121SKalle Valo 
1931e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
1932e705c121SKalle Valo 	/* use debugfs filters/macs if override is configured */
1933e705c121SKalle Valo 	if (mvm->dbgfs_bcast_filtering.override) {
1934e705c121SKalle Valo 		memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1935e705c121SKalle Valo 		       sizeof(cmd->filters));
1936e705c121SKalle Valo 		memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1937e705c121SKalle Valo 		       sizeof(cmd->macs));
1938e705c121SKalle Valo 		return true;
1939e705c121SKalle Valo 	}
1940e705c121SKalle Valo #endif
1941e705c121SKalle Valo 
1942e705c121SKalle Valo 	/* if no filters are configured, do nothing */
1943e705c121SKalle Valo 	if (!mvm->bcast_filters)
1944e705c121SKalle Valo 		return false;
1945e705c121SKalle Valo 
1946e705c121SKalle Valo 	/* configure and attach these filters for each associated sta vif */
1947e705c121SKalle Valo 	ieee80211_iterate_active_interfaces(
1948e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1949e705c121SKalle Valo 		iwl_mvm_bcast_filter_iterator, &iter_data);
1950e705c121SKalle Valo 
1951e705c121SKalle Valo 	return true;
1952e705c121SKalle Valo }
195334672bb3SEliad Peller 
195434672bb3SEliad Peller static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1955e705c121SKalle Valo {
1956e705c121SKalle Valo 	struct iwl_bcast_filter_cmd cmd;
1957e705c121SKalle Valo 
1958e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1959e705c121SKalle Valo 		return 0;
1960e705c121SKalle Valo 
1961e705c121SKalle Valo 	if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1962e705c121SKalle Valo 		return 0;
1963e705c121SKalle Valo 
1964e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1965e705c121SKalle Valo 				    sizeof(cmd), &cmd);
1966e705c121SKalle Valo }
1967e705c121SKalle Valo #else
196834672bb3SEliad Peller static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1969e705c121SKalle Valo {
1970e705c121SKalle Valo 	return 0;
1971e705c121SKalle Valo }
1972e705c121SKalle Valo #endif
1973e705c121SKalle Valo 
1974a07a8f37SSara Sharon static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
1975a07a8f37SSara Sharon 				    struct ieee80211_vif *vif)
1976a07a8f37SSara Sharon {
1977a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1978a07a8f37SSara Sharon 
1979a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1980a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1981a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1982a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1983a07a8f37SSara Sharon 
1984a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1985a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1986a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1987a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1988a07a8f37SSara Sharon }
1989a07a8f37SSara Sharon 
1990f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1991f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1992f92659a1SSara Sharon {
1993f92659a1SSara Sharon 	if (vif->mu_mimo_owner) {
1994f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1995f92659a1SSara Sharon 
1996f92659a1SSara Sharon 		/*
1997f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1998f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1999f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
2000f92659a1SSara Sharon 		 */
2001f92659a1SSara Sharon 		ieee80211_update_mu_groups(vif,
2002f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
2003f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
2004f92659a1SSara Sharon 	}
2005f92659a1SSara Sharon }
2006f92659a1SSara Sharon 
2007f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
2008f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
2009f92659a1SSara Sharon {
2010f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2011f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
2012f92659a1SSara Sharon 
2013f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
2014f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2015f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
2016f92659a1SSara Sharon }
2017f92659a1SSara Sharon 
2018514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2019514c3069SLuca Coelho {
2020514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
2021514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
2022514c3069SLuca Coelho 	u8 residue_bits;
2023514c3069SLuca Coelho 	u8 res;
2024514c3069SLuca Coelho 
2025514c3069SLuca Coelho 	if (bit_num <= 5)
2026514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
2027514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2028514c3069SLuca Coelho 
2029514c3069SLuca Coelho 	/*
2030514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
2031514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
2032514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
2033514c3069SLuca Coelho 	 */
2034514c3069SLuca Coelho 
2035514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
2036514c3069SLuca Coelho 
2037514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
2038514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2039514c3069SLuca Coelho 	      residue_bits;
2040514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2041514c3069SLuca Coelho 
2042514c3069SLuca Coelho 	return res;
2043514c3069SLuca Coelho }
2044514c3069SLuca Coelho 
2045514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2046514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2047514c3069SLuca Coelho {
2048514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2049514c3069SLuca Coelho 	struct iwl_he_sta_context_cmd sta_ctxt_cmd = {
2050514c3069SLuca Coelho 		.sta_id = sta_id,
2051514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2052dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2053514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2054514c3069SLuca Coelho 		.frame_time_rts_th =
2055514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2056514c3069SLuca Coelho 	};
2057d14ae796SSara Sharon 	int size = fw_has_api(&mvm->fw->ucode_capa,
2058d14ae796SSara Sharon 			      IWL_UCODE_TLV_API_MBSSID_HE) ?
2059d14ae796SSara Sharon 		   sizeof(sta_ctxt_cmd) :
2060d14ae796SSara Sharon 		   sizeof(struct iwl_he_sta_context_cmd_v1);
2061514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2062514c3069SLuca Coelho 	u32 flags;
2063514c3069SLuca Coelho 	int i;
2064514c3069SLuca Coelho 
2065514c3069SLuca Coelho 	rcu_read_lock();
2066514c3069SLuca Coelho 
2067514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
206812d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2069514c3069SLuca Coelho 		rcu_read_unlock();
2070514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2071514c3069SLuca Coelho 		return;
2072514c3069SLuca Coelho 	}
2073514c3069SLuca Coelho 
2074514c3069SLuca Coelho 	if (!sta->he_cap.has_he) {
2075514c3069SLuca Coelho 		rcu_read_unlock();
2076514c3069SLuca Coelho 		return;
2077514c3069SLuca Coelho 	}
2078514c3069SLuca Coelho 
2079514c3069SLuca Coelho 	flags = 0;
2080514c3069SLuca Coelho 
20814f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
20824f58121dSIlan Peer 	if (mvmvif->he_ru_2mhz_block)
20834f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
20844f58121dSIlan Peer 
2085514c3069SLuca Coelho 	/* HTC flags */
2086514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[0] &
2087514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP0_HTC_HE)
2088514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2089514c3069SLuca Coelho 	if ((sta->he_cap.he_cap_elem.mac_cap_info[1] &
2090514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2091514c3069SLuca Coelho 	    (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2092514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2093514c3069SLuca Coelho 		u8 link_adap =
2094514c3069SLuca Coelho 			((sta->he_cap.he_cap_elem.mac_cap_info[2] &
2095514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2096514c3069SLuca Coelho 			 (sta->he_cap.he_cap_elem.mac_cap_info[1] &
2097514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2098514c3069SLuca Coelho 
2099514c3069SLuca Coelho 		if (link_adap == 2)
2100514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2101514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2102514c3069SLuca Coelho 		else if (link_adap == 3)
2103514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2104514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2105514c3069SLuca Coelho 	}
2106514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2107514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2108514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[3] &
2109514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2110514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2111514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2112514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2113514c3069SLuca Coelho 
2114189b8d44SNaftali Goldstein 	/*
2115189b8d44SNaftali Goldstein 	 * Initialize the PPE thresholds to "None" (7), as described in Table
2116189b8d44SNaftali Goldstein 	 * 9-262ac of 80211.ax/D3.0.
2117189b8d44SNaftali Goldstein 	 */
2118189b8d44SNaftali Goldstein 	memset(&sta_ctxt_cmd.pkt_ext, 7, sizeof(sta_ctxt_cmd.pkt_ext));
2119189b8d44SNaftali Goldstein 
2120189b8d44SNaftali Goldstein 	/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2121514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.phy_cap_info[6] &
2122514c3069SLuca Coelho 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2123514c3069SLuca Coelho 		u8 nss = (sta->he_cap.ppe_thres[0] &
2124514c3069SLuca Coelho 			  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2125514c3069SLuca Coelho 		u8 ru_index_bitmap =
2126514c3069SLuca Coelho 			(sta->he_cap.ppe_thres[0] &
2127514c3069SLuca Coelho 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2128514c3069SLuca Coelho 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2129514c3069SLuca Coelho 		u8 *ppe = &sta->he_cap.ppe_thres[0];
2130514c3069SLuca Coelho 		u8 ppe_pos_bit = 7; /* Starting after PPE header */
2131514c3069SLuca Coelho 
2132514c3069SLuca Coelho 		/*
2133514c3069SLuca Coelho 		 * FW currently supports only nss == MAX_HE_SUPP_NSS
2134514c3069SLuca Coelho 		 *
2135514c3069SLuca Coelho 		 * If nss > MAX: we can ignore values we don't support
2136514c3069SLuca Coelho 		 * If nss < MAX: we can set zeros in other streams
2137514c3069SLuca Coelho 		 */
2138514c3069SLuca Coelho 		if (nss > MAX_HE_SUPP_NSS) {
2139514c3069SLuca Coelho 			IWL_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2140514c3069SLuca Coelho 				 MAX_HE_SUPP_NSS);
2141514c3069SLuca Coelho 			nss = MAX_HE_SUPP_NSS;
2142514c3069SLuca Coelho 		}
2143514c3069SLuca Coelho 
2144514c3069SLuca Coelho 		for (i = 0; i < nss; i++) {
2145514c3069SLuca Coelho 			u8 ru_index_tmp = ru_index_bitmap << 1;
2146514c3069SLuca Coelho 			u8 bw;
2147514c3069SLuca Coelho 
2148514c3069SLuca Coelho 			for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX; bw++) {
2149514c3069SLuca Coelho 				ru_index_tmp >>= 1;
2150514c3069SLuca Coelho 				if (!(ru_index_tmp & 1))
2151514c3069SLuca Coelho 					continue;
2152514c3069SLuca Coelho 
2153514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][1] =
2154514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2155514c3069SLuca Coelho 							       ppe_pos_bit);
2156514c3069SLuca Coelho 				ppe_pos_bit +=
2157514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2158514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0] =
2159514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2160514c3069SLuca Coelho 							       ppe_pos_bit);
2161514c3069SLuca Coelho 				ppe_pos_bit +=
2162514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2163514c3069SLuca Coelho 			}
2164514c3069SLuca Coelho 		}
2165514c3069SLuca Coelho 
2166514c3069SLuca Coelho 		flags |= STA_CTXT_HE_PACKET_EXT;
216777ff2c6bSLiad Kaufman 	} else if ((sta->he_cap.he_cap_elem.phy_cap_info[9] &
216877ff2c6bSLiad Kaufman 		    IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) !=
216977ff2c6bSLiad Kaufman 		  IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_RESERVED) {
217077ff2c6bSLiad Kaufman 		int low_th = -1;
217177ff2c6bSLiad Kaufman 		int high_th = -1;
217277ff2c6bSLiad Kaufman 
217377ff2c6bSLiad Kaufman 		/* Take the PPE thresholds from the nominal padding info */
217477ff2c6bSLiad Kaufman 		switch (sta->he_cap.he_cap_elem.phy_cap_info[9] &
217577ff2c6bSLiad Kaufman 			IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) {
217677ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_0US:
217777ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
217877ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
217977ff2c6bSLiad Kaufman 			break;
218077ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_8US:
218177ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_BPSK;
218277ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
218377ff2c6bSLiad Kaufman 			break;
218477ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US:
218577ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
218677ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_BPSK;
218777ff2c6bSLiad Kaufman 			break;
218877ff2c6bSLiad Kaufman 		}
218977ff2c6bSLiad Kaufman 
219077ff2c6bSLiad Kaufman 		/* Set the PPE thresholds accordingly */
219177ff2c6bSLiad Kaufman 		if (low_th >= 0 && high_th >= 0) {
21920bfefe2fSLiad Kaufman 			struct iwl_he_pkt_ext *pkt_ext =
21930bfefe2fSLiad Kaufman 				(struct iwl_he_pkt_ext *)&sta_ctxt_cmd.pkt_ext;
219477ff2c6bSLiad Kaufman 
219577ff2c6bSLiad Kaufman 			for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
219677ff2c6bSLiad Kaufman 				u8 bw;
219777ff2c6bSLiad Kaufman 
219877ff2c6bSLiad Kaufman 				for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX;
219977ff2c6bSLiad Kaufman 				     bw++) {
22000bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][0] =
22010bfefe2fSLiad Kaufman 						low_th;
22020bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][1] =
22030bfefe2fSLiad Kaufman 						high_th;
220477ff2c6bSLiad Kaufman 				}
220577ff2c6bSLiad Kaufman 			}
220677ff2c6bSLiad Kaufman 
220777ff2c6bSLiad Kaufman 			flags |= STA_CTXT_HE_PACKET_EXT;
220877ff2c6bSLiad Kaufman 		}
2209514c3069SLuca Coelho 	}
221073f23d91SShaul Triebitz 
221173f23d91SShaul Triebitz 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] &
221273f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
221373f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
221473f23d91SShaul Triebitz 
221573f23d91SShaul Triebitz 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] &
221673f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
221773f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
221873f23d91SShaul Triebitz 
2219514c3069SLuca Coelho 	rcu_read_unlock();
2220514c3069SLuca Coelho 
2221514c3069SLuca Coelho 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2222514c3069SLuca Coelho 	flags |= STA_CTXT_HE_MU_EDCA_CW;
22235cc74f65SShaul Triebitz 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2224514c3069SLuca Coelho 		struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2225514c3069SLuca Coelho 			&mvmvif->queue_params[i].mu_edca_param_rec;
22265cc74f65SShaul Triebitz 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2227514c3069SLuca Coelho 
2228514c3069SLuca Coelho 		if (!mvmvif->queue_params[i].mu_edca) {
2229514c3069SLuca Coelho 			flags &= ~STA_CTXT_HE_MU_EDCA_CW;
2230514c3069SLuca Coelho 			break;
2231514c3069SLuca Coelho 		}
2232514c3069SLuca Coelho 
22335cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].cwmin =
2234514c3069SLuca Coelho 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
22355cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].cwmax =
2236514c3069SLuca Coelho 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
22375cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].aifsn =
2238514c3069SLuca Coelho 			cpu_to_le16(mu_edca->aifsn);
22395cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].mu_time =
2240514c3069SLuca Coelho 			cpu_to_le16(mu_edca->mu_edca_timer);
2241514c3069SLuca Coelho 	}
2242514c3069SLuca Coelho 
2243514c3069SLuca Coelho 
2244514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2245514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2246514c3069SLuca Coelho 
2247514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2248514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2249514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2250514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2251514c3069SLuca Coelho 	}
2252514c3069SLuca Coelho 
2253918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2254918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2255918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2256918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2257d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2258d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2259d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2260d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2261d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2262d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2263918cbf39SSara Sharon 	}
2264514c3069SLuca Coelho 
2265514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2266514c3069SLuca Coelho 
2267514c3069SLuca Coelho 	if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(STA_HE_CTXT_CMD,
2268514c3069SLuca Coelho 						 DATA_PATH_GROUP, 0),
2269d14ae796SSara Sharon 				 0, size, &sta_ctxt_cmd))
2270514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2271514c3069SLuca Coelho }
2272514c3069SLuca Coelho 
2273e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2274e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2275e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
2276e705c121SKalle Valo 					     u32 changes)
2277e705c121SKalle Valo {
2278e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2279e705c121SKalle Valo 	int ret;
2280e705c121SKalle Valo 
2281e705c121SKalle Valo 	/*
2282e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
2283e705c121SKalle Valo 	 * beacon interval, which was not known when the station interface was
2284e705c121SKalle Valo 	 * added.
2285e705c121SKalle Valo 	 */
2286514c3069SLuca Coelho 	if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
2287230ba6c5SLuca Coelho 		if (vif->bss_conf.he_support &&
2288230ba6c5SLuca Coelho 		    !iwlwifi_mod_params.disable_11ax)
2289514c3069SLuca Coelho 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2290514c3069SLuca Coelho 
2291e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2292514c3069SLuca Coelho 	}
2293e705c121SKalle Valo 
229440ecdd01SShaul Triebitz 	/* Update MU EDCA params */
229540ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
229640ecdd01SShaul Triebitz 	    bss_conf->assoc && vif->bss_conf.he_support &&
229740ecdd01SShaul Triebitz 	    !iwlwifi_mod_params.disable_11ax)
229840ecdd01SShaul Triebitz 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
229940ecdd01SShaul Triebitz 
2300e705c121SKalle Valo 	/*
2301e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2302e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2303e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2304e705c121SKalle Valo 	 * branch for disassociation below.
2305e705c121SKalle Valo 	 */
2306e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2307e705c121SKalle Valo 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2308e705c121SKalle Valo 
2309e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
2310e705c121SKalle Valo 	if (ret)
2311e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2312e705c121SKalle Valo 
2313e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2314e705c121SKalle Valo 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2315e705c121SKalle Valo 	mvmvif->associated = bss_conf->assoc;
2316e705c121SKalle Valo 
2317e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2318e705c121SKalle Valo 		if (bss_conf->assoc) {
2319e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2320e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2321e705c121SKalle Valo 			memset(&mvmvif->beacon_stats, 0,
2322e705c121SKalle Valo 			       sizeof(mvmvif->beacon_stats));
2323e705c121SKalle Valo 
2324e705c121SKalle Valo 			/* add quota for this interface */
2325e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2326e705c121SKalle Valo 			if (ret) {
2327e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2328e705c121SKalle Valo 				return;
2329e705c121SKalle Valo 			}
2330e705c121SKalle Valo 
2331e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2332fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2333fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2334fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2335e705c121SKalle Valo 				/*
2336e705c121SKalle Valo 				 * If we're restarting then the firmware will
2337e705c121SKalle Valo 				 * obviously have lost synchronisation with
2338e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2339e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2340e705c121SKalle Valo 				 * scheduling a session protection time event.
2341e705c121SKalle Valo 				 *
2342e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2343e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2344e705c121SKalle Valo 				 * the beacon interval.
2345e705c121SKalle Valo 				 *
2346e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2347e705c121SKalle Valo 				 * than a single interface.
2348fe959c7bSEmmanuel Grumbach 				 *
2349fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2350fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2351fe959c7bSEmmanuel Grumbach 				 * only anyway.
2352e705c121SKalle Valo 				 */
2353e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2354e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2355e705c121SKalle Valo 							5 * dur, false);
2356e705c121SKalle Valo 			}
2357e705c121SKalle Valo 
2358e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2359e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2360e705c121SKalle Valo 			if (vif->p2p) {
2361e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2362e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
2363e705c121SKalle Valo 						    IEEE80211_SMPS_DYNAMIC);
2364e705c121SKalle Valo 			}
23650ae98812SSara Sharon 		} else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
2366e705c121SKalle Valo 			/*
2367e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2368e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2369e705c121SKalle Valo 			 */
237069e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
237169e508b4SIlan Peer 			WARN_ONCE(ret &&
237269e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
237369e508b4SIlan Peer 					    &mvm->status),
2374e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2375e705c121SKalle Valo 
23766b28f978SEmmanuel Grumbach 			/*
23776b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
23786b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
23796b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
23806b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
23816b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
23826b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
23836b28f978SEmmanuel Grumbach 			 */
23846b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
23856b28f978SEmmanuel Grumbach 				      &mvm->status)) {
23866b28f978SEmmanuel Grumbach 				/*
23876b28f978SEmmanuel Grumbach 				 * Remove AP station now that
23886b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
23896b28f978SEmmanuel Grumbach 				 */
23906b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
23916b28f978SEmmanuel Grumbach 							mvmvif->ap_sta_id);
2392e705c121SKalle Valo 				if (ret)
23936b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
23946b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2395e705c121SKalle Valo 
23960ae98812SSara Sharon 				mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
23976b28f978SEmmanuel Grumbach 			}
23986b28f978SEmmanuel Grumbach 
2399e705c121SKalle Valo 			/* remove quota for this interface */
2400e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2401e705c121SKalle Valo 			if (ret)
2402e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2403e705c121SKalle Valo 
2404e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2405e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2406e705c121SKalle Valo 			if (ret)
2407e705c121SKalle Valo 				IWL_ERR(mvm,
2408e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2409e705c121SKalle Valo 					vif->addr);
2410e705c121SKalle Valo 		}
2411e705c121SKalle Valo 
2412a07a8f37SSara Sharon 		/*
2413a07a8f37SSara Sharon 		 * The firmware tracks the MU-MIMO group on its own.
2414f92659a1SSara Sharon 		 * However, on HW restart we should restore this data.
2415a07a8f37SSara Sharon 		 */
2416a07a8f37SSara Sharon 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2417f92659a1SSara Sharon 		    (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
2418a07a8f37SSara Sharon 			ret = iwl_mvm_update_mu_groups(mvm, vif);
2419a07a8f37SSara Sharon 			if (ret)
2420a07a8f37SSara Sharon 				IWL_ERR(mvm,
2421a07a8f37SSara Sharon 					"failed to update VHT MU_MIMO groups\n");
2422a07a8f37SSara Sharon 		}
2423a07a8f37SSara Sharon 
2424e705c121SKalle Valo 		iwl_mvm_recalc_multicast(mvm);
242534672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2426e705c121SKalle Valo 
2427e705c121SKalle Valo 		/* reset rssi values */
2428e705c121SKalle Valo 		mvmvif->bf_data.ave_beacon_signal = 0;
2429e705c121SKalle Valo 
2430e705c121SKalle Valo 		iwl_mvm_bt_coex_vif_change(mvm);
2431e705c121SKalle Valo 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2432e705c121SKalle Valo 				    IEEE80211_SMPS_AUTOMATIC);
2433355346baSAvraham Stern 		if (fw_has_capa(&mvm->fw->ucode_capa,
2434355346baSAvraham Stern 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2435355346baSAvraham Stern 			iwl_mvm_config_scan(mvm);
2436b45242c9SAvraham Stern 	}
2437b45242c9SAvraham Stern 
2438b45242c9SAvraham Stern 	if (changes & BSS_CHANGED_BEACON_INFO) {
2439e705c121SKalle Valo 		/*
2440b45242c9SAvraham Stern 		 * We received a beacon from the associated AP so
2441e705c121SKalle Valo 		 * remove the session protection.
2442fe959c7bSEmmanuel Grumbach 		 * A firmware with the new API will remove it automatically.
2443e705c121SKalle Valo 		 */
2444fe959c7bSEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
2445fe959c7bSEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
24463edfb5f4SAvraham Stern 			iwl_mvm_stop_session_protection(mvm, vif);
2447e705c121SKalle Valo 
2448e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2449e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2450e705c121SKalle Valo 	}
2451e705c121SKalle Valo 
2452283115fbSAvri Altman 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2453283115fbSAvri Altman 		       /*
2454283115fbSAvri Altman 			* Send power command on every beacon change,
2455283115fbSAvri Altman 			* because we may have not enabled beacon abort yet.
2456283115fbSAvri Altman 			*/
2457283115fbSAvri Altman 		       BSS_CHANGED_BEACON_INFO)) {
2458e705c121SKalle Valo 		ret = iwl_mvm_power_update_mac(mvm);
2459e705c121SKalle Valo 		if (ret)
2460e705c121SKalle Valo 			IWL_ERR(mvm, "failed to update power mode\n");
2461e705c121SKalle Valo 	}
2462e705c121SKalle Valo 
2463e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2464e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2465e705c121SKalle Valo 				bss_conf->txpower);
2466e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2467e705c121SKalle Valo 	}
2468e705c121SKalle Valo 
2469e705c121SKalle Valo 	if (changes & BSS_CHANGED_CQM) {
2470e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2471e705c121SKalle Valo 		/* reset cqm events tracking */
2472e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = 0;
2473e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
2474e705c121SKalle Valo 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2475e705c121SKalle Valo 			if (ret)
2476e705c121SKalle Valo 				IWL_ERR(mvm,
2477e705c121SKalle Valo 					"failed to update CQM thresholds\n");
2478e705c121SKalle Valo 		}
2479e705c121SKalle Valo 	}
2480e705c121SKalle Valo 
2481e705c121SKalle Valo 	if (changes & BSS_CHANGED_ARP_FILTER) {
2482e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
248334672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2484e705c121SKalle Valo 	}
2485e705c121SKalle Valo }
2486e705c121SKalle Valo 
2487e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2488e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2489e705c121SKalle Valo {
2490e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2491e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2492c56e00a3SJohannes Berg 	int ret, i;
2493e705c121SKalle Valo 
2494e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2495e705c121SKalle Valo 
2496e705c121SKalle Valo 	/* Send the beacon template */
2497e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2498e705c121SKalle Valo 	if (ret)
2499e705c121SKalle Valo 		goto out_unlock;
2500e705c121SKalle Valo 
2501e705c121SKalle Valo 	/*
2502e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
2503e705c121SKalle Valo 	 * beacon interval, which was not known when the AP interface was added.
2504e705c121SKalle Valo 	 */
2505e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2506e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2507e705c121SKalle Valo 
2508e705c121SKalle Valo 	mvmvif->ap_assoc_sta_count = 0;
2509e705c121SKalle Valo 
2510e705c121SKalle Valo 	/* Add the mac context */
2511e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2512e705c121SKalle Valo 	if (ret)
2513e705c121SKalle Valo 		goto out_unlock;
2514e705c121SKalle Valo 
2515e705c121SKalle Valo 	/* Perform the binding */
2516e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2517e705c121SKalle Valo 	if (ret)
2518e705c121SKalle Valo 		goto out_remove;
2519e705c121SKalle Valo 
252063dd5d02SSara Sharon 	/*
252163dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
252263dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
252363dd5d02SSara Sharon 	 * cause assert 0x2b00.
252463dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
252563dd5d02SSara Sharon 	 * the TLV
252663dd5d02SSara Sharon 	 */
252763dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2528ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2529ced19f26SSara Sharon 		if (ret)
2530ced19f26SSara Sharon 			goto out_unbind;
253163dd5d02SSara Sharon 		/*
253263dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
253363dd5d02SSara Sharon 		 * events are added and applied to the scheduler
253463dd5d02SSara Sharon 		 */
2535e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
253663dd5d02SSara Sharon 		if (ret) {
253763dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
253863dd5d02SSara Sharon 			goto out_unbind;
253963dd5d02SSara Sharon 		}
254063dd5d02SSara Sharon 	} else {
254163dd5d02SSara Sharon 		/*
254263dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
254363dd5d02SSara Sharon 		 * events are added and applied to the scheduler
254463dd5d02SSara Sharon 		 */
254575fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2546e705c121SKalle Valo 		if (ret)
254763dd5d02SSara Sharon 			goto out_unbind;
254875fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
254963dd5d02SSara Sharon 		if (ret) {
255063dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
255163dd5d02SSara Sharon 			goto out_unbind;
255263dd5d02SSara Sharon 		}
255363dd5d02SSara Sharon 	}
255426d6c16bSSara Sharon 
2555e705c121SKalle Valo 	/* must be set before quota calculations */
2556e705c121SKalle Valo 	mvmvif->ap_ibss_active = true;
2557e705c121SKalle Valo 
2558c56e00a3SJohannes Berg 	/* send all the early keys to the device now */
2559c56e00a3SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2560c56e00a3SJohannes Berg 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2561c56e00a3SJohannes Berg 
2562c56e00a3SJohannes Berg 		if (!key)
2563c56e00a3SJohannes Berg 			continue;
2564c56e00a3SJohannes Berg 
2565c56e00a3SJohannes Berg 		mvmvif->ap_early_keys[i] = NULL;
2566c56e00a3SJohannes Berg 
25676569e7d3SJohannes Berg 		ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2568c56e00a3SJohannes Berg 		if (ret)
2569c56e00a3SJohannes Berg 			goto out_quota_failed;
2570c56e00a3SJohannes Berg 	}
2571c56e00a3SJohannes Berg 
257247242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
257347242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, true,
257447242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
257547242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
257647242744STova Mussai 	}
257747242744STova Mussai 
2578e705c121SKalle Valo 	/* power updated needs to be done before quotas */
2579e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2580e705c121SKalle Valo 
2581e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2582e705c121SKalle Valo 	if (ret)
2583e705c121SKalle Valo 		goto out_quota_failed;
2584e705c121SKalle Valo 
2585e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2586e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2587e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2588e705c121SKalle Valo 
2589e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2590e705c121SKalle Valo 
2591e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2592e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2593e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2594e705c121SKalle Valo 
2595b73f9a4aSJohannes Berg 	iwl_mvm_ftm_restart_responder(mvm, vif);
2596b73f9a4aSJohannes Berg 
2597e705c121SKalle Valo 	goto out_unlock;
2598e705c121SKalle Valo 
2599e705c121SKalle Valo out_quota_failed:
2600e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2601e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2602e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2603ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2604e705c121SKalle Valo out_unbind:
2605e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2606e705c121SKalle Valo out_remove:
2607e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2608e705c121SKalle Valo out_unlock:
2609e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2610e705c121SKalle Valo 	return ret;
2611e705c121SKalle Valo }
2612e705c121SKalle Valo 
2613e705c121SKalle Valo static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2614e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2615e705c121SKalle Valo {
2616e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2617e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2618e705c121SKalle Valo 
2619e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
2620e705c121SKalle Valo 
2621e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2622e705c121SKalle Valo 
2623e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2624e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2625e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2626e705c121SKalle Valo 					  &mvmvif->time_event_data);
2627e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2628e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2629e705c121SKalle Valo 	}
2630e705c121SKalle Valo 
2631e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2632e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2633e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2634e705c121SKalle Valo 	}
2635e705c121SKalle Valo 
2636e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2637e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2638e705c121SKalle Valo 
263947242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
264047242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
264147242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
264247242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
264347242744STova Mussai 	}
264447242744STova Mussai 
2645e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2646e705c121SKalle Valo 
2647e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2648e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2649e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2650e705c121SKalle Valo 
2651e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2652ced19f26SSara Sharon 
2653be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
2654be82ecd3SAvraham Stern 
2655ced19f26SSara Sharon 	/*
2656ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2657ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2658ced19f26SSara Sharon 	 * cause assert 0x2b00.
2659ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2660ced19f26SSara Sharon 	 * bcast station).
2661ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2662ced19f26SSara Sharon 	 */
2663ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
266426d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2665e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2666ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2667ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2668e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2669e705c121SKalle Valo 
2670e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2671e705c121SKalle Valo 
2672e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2673e705c121SKalle Valo 
2674e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2675e705c121SKalle Valo }
2676e705c121SKalle Valo 
2677e705c121SKalle Valo static void
2678e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2679e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2680e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
2681e705c121SKalle Valo 				 u32 changes)
2682e705c121SKalle Valo {
2683e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2684e705c121SKalle Valo 
2685e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
2686e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
2687e705c121SKalle Valo 		return;
2688e705c121SKalle Valo 
2689e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2690e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2691e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2692e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2693e705c121SKalle Valo 
2694e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
2695e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
2696e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2697e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
2698e705c121SKalle Valo 
2699e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2700e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2701e705c121SKalle Valo 				bss_conf->txpower);
2702e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2703e705c121SKalle Valo 	}
2704b73f9a4aSJohannes Berg 
2705b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
2706b73f9a4aSJohannes Berg 		int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2707b73f9a4aSJohannes Berg 
2708b73f9a4aSJohannes Berg 		if (ret)
2709b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2710b73f9a4aSJohannes Berg 				 ret);
2711b73f9a4aSJohannes Berg 	}
2712b73f9a4aSJohannes Berg 
2713e705c121SKalle Valo }
2714e705c121SKalle Valo 
2715e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2716e705c121SKalle Valo 				     struct ieee80211_vif *vif,
2717e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
2718e705c121SKalle Valo 				     u32 changes)
2719e705c121SKalle Valo {
2720e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2721e705c121SKalle Valo 
2722e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2723e705c121SKalle Valo 
2724e705c121SKalle Valo 	if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2725e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2726e705c121SKalle Valo 
2727e705c121SKalle Valo 	switch (vif->type) {
2728e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2729e705c121SKalle Valo 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2730e705c121SKalle Valo 		break;
2731e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
2732e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
2733e705c121SKalle Valo 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2734e705c121SKalle Valo 		break;
273591b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
273691b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
273791b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
273891b08c2dSAviya Erenfeld 		break;
2739e705c121SKalle Valo 	default:
2740e705c121SKalle Valo 		/* shouldn't happen */
2741e705c121SKalle Valo 		WARN_ON_ONCE(1);
2742e705c121SKalle Valo 	}
2743e705c121SKalle Valo 
2744e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2745e705c121SKalle Valo }
2746e705c121SKalle Valo 
2747e705c121SKalle Valo static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2748e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2749e705c121SKalle Valo 			       struct ieee80211_scan_request *hw_req)
2750e705c121SKalle Valo {
2751e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2752e705c121SKalle Valo 	int ret;
2753e705c121SKalle Valo 
2754e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
2755e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2756e705c121SKalle Valo 		return -EINVAL;
2757e705c121SKalle Valo 
2758e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2759e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2760e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2761e705c121SKalle Valo 
2762e705c121SKalle Valo 	return ret;
2763e705c121SKalle Valo }
2764e705c121SKalle Valo 
2765e705c121SKalle Valo static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2766e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2767e705c121SKalle Valo {
2768e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2769e705c121SKalle Valo 
2770e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2771e705c121SKalle Valo 
2772e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2773e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
2774e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
2775e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
2776e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
2777e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
2778e705c121SKalle Valo 	*/
2779e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2780e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2781e705c121SKalle Valo 
2782e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2783e705c121SKalle Valo }
2784e705c121SKalle Valo 
2785e705c121SKalle Valo static void
2786e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2787e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
2788e705c121SKalle Valo 				  int num_frames,
2789e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
2790e705c121SKalle Valo 				  bool more_data)
2791e705c121SKalle Valo {
2792e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2793e705c121SKalle Valo 
2794e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
2795e705c121SKalle Valo 
2796e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2797e705c121SKalle Valo 					  tids, more_data, false);
2798e705c121SKalle Valo }
2799e705c121SKalle Valo 
2800e705c121SKalle Valo static void
2801e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2802e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
2803e705c121SKalle Valo 				    int num_frames,
2804e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
2805e705c121SKalle Valo 				    bool more_data)
2806e705c121SKalle Valo {
2807e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2808e705c121SKalle Valo 
28099a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2810e705c121SKalle Valo 
2811e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2812e705c121SKalle Valo 					  tids, more_data, true);
2813e705c121SKalle Valo }
2814e705c121SKalle Valo 
281565e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2816e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
2817e705c121SKalle Valo 				     struct ieee80211_sta *sta)
2818e705c121SKalle Valo {
2819e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2820e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2821e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
2822e705c121SKalle Valo 	int tid;
2823e705c121SKalle Valo 
2824960f864bSJohannes Berg 	/*
2825960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
2826960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
2827960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
2828960f864bSJohannes Berg 	 */
2829960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2830960f864bSJohannes Berg 		return;
2831960f864bSJohannes Berg 
2832e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2833311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
2834e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2835e705c121SKalle Valo 
28366862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
28371c17627bSSara Sharon 			continue;
28381c17627bSSara Sharon 
2839e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
2840e705c121SKalle Valo 
2841dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
2842e705c121SKalle Valo 			continue;
2843e705c121SKalle Valo 
2844e705c121SKalle Valo 		__set_bit(tid, &tids);
2845e705c121SKalle Valo 	}
2846e705c121SKalle Valo 
2847e705c121SKalle Valo 	switch (cmd) {
2848e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
2849e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
2850e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
2851e705c121SKalle Valo 
2852e705c121SKalle Valo 		if (txqs)
2853e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
2854e705c121SKalle Valo 		/*
2855e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
2856e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
2857e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2858e705c121SKalle Valo 		 */
2859e705c121SKalle Valo 		break;
2860e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
28610ae98812SSara Sharon 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
2862e705c121SKalle Valo 			break;
2863e705c121SKalle Valo 
2864e705c121SKalle Valo 		if (txqs)
2865e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
2866e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
2867e705c121SKalle Valo 		break;
2868e705c121SKalle Valo 	default:
2869e705c121SKalle Valo 		break;
2870e705c121SKalle Valo 	}
2871e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2872e705c121SKalle Valo }
2873e705c121SKalle Valo 
287465e25482SJohannes Berg static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
287565e25482SJohannes Berg 				   struct ieee80211_vif *vif,
287665e25482SJohannes Berg 				   enum sta_notify_cmd cmd,
287765e25482SJohannes Berg 				   struct ieee80211_sta *sta)
287865e25482SJohannes Berg {
287965e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
288065e25482SJohannes Berg }
288165e25482SJohannes Berg 
288265e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
288365e25482SJohannes Berg {
288465e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
288565e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
288665e25482SJohannes Berg 	struct ieee80211_sta *sta;
288765e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
288865e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
288965e25482SJohannes Berg 
2890be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
289165e25482SJohannes Berg 		return;
289265e25482SJohannes Berg 
289365e25482SJohannes Berg 	rcu_read_lock();
2894a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
289565e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
289665e25482SJohannes Berg 		rcu_read_unlock();
289765e25482SJohannes Berg 		return;
289865e25482SJohannes Berg 	}
289965e25482SJohannes Berg 
290065e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
290165e25482SJohannes Berg 
290265e25482SJohannes Berg 	if (!mvmsta->vif ||
290365e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
290465e25482SJohannes Berg 		rcu_read_unlock();
290565e25482SJohannes Berg 		return;
290665e25482SJohannes Berg 	}
290765e25482SJohannes Berg 
290865e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
290965e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
291065e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
291165e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
291265e25482SJohannes Berg 			sta);
291365e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
291465e25482SJohannes Berg 	}
291565e25482SJohannes Berg 
291665e25482SJohannes Berg 	if (sleeping) {
291765e25482SJohannes Berg 		switch (notif->type) {
291865e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
291965e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
292065e25482SJohannes Berg 			break;
292165e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
292265e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
292365e25482SJohannes Berg 			break;
292465e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
292565e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
292665e25482SJohannes Berg 			break;
292765e25482SJohannes Berg 		default:
292865e25482SJohannes Berg 			break;
292965e25482SJohannes Berg 		}
293065e25482SJohannes Berg 	}
293165e25482SJohannes Berg 
293265e25482SJohannes Berg 	rcu_read_unlock();
293365e25482SJohannes Berg }
293465e25482SJohannes Berg 
2935e705c121SKalle Valo static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2936e705c121SKalle Valo 				       struct ieee80211_vif *vif,
2937e705c121SKalle Valo 				       struct ieee80211_sta *sta)
2938e705c121SKalle Valo {
2939e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2940e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2941e705c121SKalle Valo 
2942e705c121SKalle Valo 	/*
2943e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
2944e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
2945e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
2946e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
2947e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
2948e705c121SKalle Valo 	 * callback deleted the station.
2949e705c121SKalle Valo 	 */
2950e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2951e705c121SKalle Valo 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2952e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2953e705c121SKalle Valo 				   ERR_PTR(-ENOENT));
2954e705c121SKalle Valo 
2955e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2956e705c121SKalle Valo }
2957e705c121SKalle Valo 
2958e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2959e705c121SKalle Valo 				const u8 *bssid)
2960e705c121SKalle Valo {
2961b0ffe455SJohannes Berg 	int i;
2962b0ffe455SJohannes Berg 
2963b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2964b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
2965b0ffe455SJohannes Berg 
2966b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
2967b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
2968b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
2969b0ffe455SJohannes Berg 	}
2970b0ffe455SJohannes Berg 
2971e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2972e705c121SKalle Valo 		return;
2973e705c121SKalle Valo 
2974c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
2975cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2976cee5a882SAvri Altman 		return;
2977cee5a882SAvri Altman 	}
2978cee5a882SAvri Altman 
297911dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
298011dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
2981e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2982e705c121SKalle Valo 		return;
2983e705c121SKalle Valo 	}
2984e705c121SKalle Valo 
2985b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
2986b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
2987b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2988b0ffe455SJohannes Berg 			return;
2989b0ffe455SJohannes Berg 		}
2990b0ffe455SJohannes Berg 	}
2991b0ffe455SJohannes Berg 
2992e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2993e705c121SKalle Valo }
2994e705c121SKalle Valo 
29951e8f1329SGolan Ben-Ami static void
29961e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
29971e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
29981e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
29991e8f1329SGolan Ben-Ami {
30001e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
30011e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
30021e8f1329SGolan Ben-Ami 
30036c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
30046c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
30056c042d75SSara Sharon 	if (!trig)
30061e8f1329SGolan Ben-Ami 		return;
30071e8f1329SGolan Ben-Ami 
30081e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
30091e8f1329SGolan Ben-Ami 
30101e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
30111e8f1329SGolan Ben-Ami 		return;
30121e8f1329SGolan Ben-Ami 
30131e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
30141e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
30151e8f1329SGolan Ben-Ami 		return;
30161e8f1329SGolan Ben-Ami 
30177174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
30181e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
30191e8f1329SGolan Ben-Ami 				peer_addr, action);
30201e8f1329SGolan Ben-Ami }
30211e8f1329SGolan Ben-Ami 
30224f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
30234f58121dSIlan Peer 	bool tolerated;
30244f58121dSIlan Peer };
30254f58121dSIlan Peer 
30264f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
30274f58121dSIlan Peer 						    struct cfg80211_bss *bss,
30284f58121dSIlan Peer 						    void *_data)
30294f58121dSIlan Peer {
30304f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
30314f58121dSIlan Peer 	const struct element *elem;
30324f58121dSIlan Peer 
30334f58121dSIlan Peer 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, bss->ies->data,
30344f58121dSIlan Peer 				  bss->ies->len);
30354f58121dSIlan Peer 
30364f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
30374f58121dSIlan Peer 	    !(elem->data[10] &
30384f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
30394f58121dSIlan Peer 		data->tolerated = false;
30404f58121dSIlan Peer 	}
30414f58121dSIlan Peer }
30424f58121dSIlan Peer 
30434f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
30444f58121dSIlan Peer 					       struct ieee80211_vif *vif)
30454f58121dSIlan Peer {
30464f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
30474f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
30484f58121dSIlan Peer 		.tolerated = true,
30494f58121dSIlan Peer 	};
30504f58121dSIlan Peer 
30514f58121dSIlan Peer 	if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
30524f58121dSIlan Peer 		mvmvif->he_ru_2mhz_block = false;
30534f58121dSIlan Peer 		return;
30544f58121dSIlan Peer 	}
30554f58121dSIlan Peer 
30564f58121dSIlan Peer 	cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
30574f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
30584f58121dSIlan Peer 			  &iter_data);
30594f58121dSIlan Peer 
30604f58121dSIlan Peer 	/*
30614f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
30624f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
30634f58121dSIlan Peer 	 */
30644f58121dSIlan Peer 	mvmvif->he_ru_2mhz_block = !iter_data.tolerated;
30654f58121dSIlan Peer }
30664f58121dSIlan Peer 
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))
3083*fe56d05eSSara 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 		 */
310624afba76SLiad Kaufman 	}
310724afba76SLiad Kaufman 
3108e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
31096ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3110d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
31116ea29ce5SJohannes Berg 
3112e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3113e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
3114e705c121SKalle Valo 		/*
3115e705c121SKalle Valo 		 * Firmware bug - it'll crash if the beacon interval is less
3116e705c121SKalle Valo 		 * than 16. We can't avoid connecting at all, so refuse the
3117e705c121SKalle Valo 		 * station state change, this will cause mac80211 to abandon
3118e705c121SKalle Valo 		 * attempts to connect to this AP, and eventually wpa_s will
3119e705c121SKalle Valo 		 * blacklist the AP...
3120e705c121SKalle Valo 		 */
3121e705c121SKalle Valo 		if (vif->type == NL80211_IFTYPE_STATION &&
3122e705c121SKalle Valo 		    vif->bss_conf.beacon_int < 16) {
3123e705c121SKalle Valo 			IWL_ERR(mvm,
3124e705c121SKalle Valo 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3125e705c121SKalle Valo 				sta->addr, vif->bss_conf.beacon_int);
3126e705c121SKalle Valo 			ret = -EINVAL;
3127e705c121SKalle Valo 			goto out_unlock;
3128e705c121SKalle Valo 		}
3129e705c121SKalle Valo 
313097cc1694SAvraham Stern 		if (vif->type == NL80211_IFTYPE_STATION)
313197cc1694SAvraham Stern 			vif->bss_conf.he_support = sta->he_cap.has_he;
313297cc1694SAvraham Stern 
3133e705c121SKalle Valo 		if (sta->tdls &&
3134e705c121SKalle Valo 		    (vif->p2p ||
3135e705c121SKalle Valo 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
3136e705c121SKalle Valo 						IWL_MVM_TDLS_STA_COUNT ||
3137e705c121SKalle Valo 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3138e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3139e705c121SKalle Valo 			ret = -EBUSY;
3140e705c121SKalle Valo 			goto out_unlock;
3141e705c121SKalle Valo 		}
3142e705c121SKalle Valo 
3143e705c121SKalle Valo 		ret = iwl_mvm_add_sta(mvm, vif, sta);
31441e8f1329SGolan Ben-Ami 		if (sta->tdls && ret == 0) {
3145e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
31461e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
31471e8f1329SGolan Ben-Ami 						   NL80211_TDLS_SETUP);
31481e8f1329SGolan Ben-Ami 		}
3149438af969SSara Sharon 
3150438af969SSara Sharon 		sta->max_rc_amsdu_len = 1;
3151e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3152e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3153e705c121SKalle Valo 		/*
3154e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3155e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3156e705c121SKalle Valo 		 */
3157e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3158e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3159e705c121SKalle Valo 		ret = 0;
3160e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3161e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
31628be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
31639394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
31648be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count++;
31658be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
316602221a81SShaul Triebitz 			if (vif->bss_conf.he_support &&
316702221a81SShaul Triebitz 			    !iwlwifi_mod_params.disable_11ax)
316802221a81SShaul Triebitz 				iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
31699394662aSLiad Kaufman 		} else if (vif->type == NL80211_IFTYPE_STATION) {
31709394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
31714f58121dSIlan Peer 
31724f58121dSIlan Peer 			mvmvif->he_ru_2mhz_block = false;
31734f58121dSIlan Peer 			if (sta->he_cap.has_he)
31744f58121dSIlan Peer 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif);
31754f58121dSIlan Peer 
31769394662aSLiad Kaufman 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
31778be30c13SAyala Beker 		}
3178735a0045SGoodstein, Mordechay 
31793baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
31803baf7528SAvraham Stern 				     false);
3181735a0045SGoodstein, Mordechay 		ret = iwl_mvm_update_sta(mvm, vif, sta);
3182e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3183e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
318428916a16SEmmanuel Grumbach 		ret = 0;
3185e705c121SKalle Valo 
3186e705c121SKalle Valo 		/* we don't support TDLS during DCM */
3187e705c121SKalle Valo 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
3188e705c121SKalle Valo 			iwl_mvm_teardown_tdls_peers(mvm);
3189e705c121SKalle Valo 
31901e8f1329SGolan Ben-Ami 		if (sta->tdls)
31911e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
31921e8f1329SGolan Ben-Ami 						   NL80211_TDLS_ENABLE_LINK);
31931e8f1329SGolan Ben-Ami 
3194e705c121SKalle Valo 		/* enable beacon filtering */
3195e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
31966b7a5aeaSNaftali Goldstein 
319750f56044SIlan Peer 		/*
319850f56044SIlan Peer 		 * Now that the station is authorized, i.e., keys were already
319950f56044SIlan Peer 		 * installed, need to indicate to the FW that
320050f56044SIlan Peer 		 * multicast data frames can be forwarded to the driver
320150f56044SIlan Peer 		 */
320250f56044SIlan Peer 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
320350f56044SIlan Peer 
32043baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
32053baf7528SAvraham Stern 				     true);
3206e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3207e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
320850f56044SIlan Peer 		/* Multicast data frames are no longer allowed */
320950f56044SIlan Peer 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
321050f56044SIlan Peer 
3211e705c121SKalle Valo 		/* disable beacon filtering */
321269e508b4SIlan Peer 		ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
321369e508b4SIlan Peer 		WARN_ON(ret &&
321469e508b4SIlan Peer 			!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
321569e508b4SIlan Peer 				  &mvm->status));
3216e705c121SKalle Valo 		ret = 0;
3217e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3218e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
32198be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
32208be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
32218be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
32228be30c13SAyala Beker 		}
3223e705c121SKalle Valo 		ret = 0;
3224e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3225e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3226e705c121SKalle Valo 		ret = 0;
3227e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3228e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
3229e705c121SKalle Valo 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
32301e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3231e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
32321e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
32331e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
32341e8f1329SGolan Ben-Ami 		}
323534a880d8SLiad Kaufman 
323644135b7cSIlan Peer 		if (unlikely(ret &&
323744135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
323844135b7cSIlan Peer 				      &mvm->status)))
323944135b7cSIlan Peer 			ret = 0;
3240e705c121SKalle Valo 	} else {
3241e705c121SKalle Valo 		ret = -EIO;
3242e705c121SKalle Valo 	}
3243e705c121SKalle Valo  out_unlock:
3244e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3245e705c121SKalle Valo 
3246e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3247e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3248e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3249e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3250e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3251e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3252e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3253e705c121SKalle Valo 	}
3254e705c121SKalle Valo 
3255e705c121SKalle Valo 	return ret;
3256e705c121SKalle Valo }
3257e705c121SKalle Valo 
3258e705c121SKalle Valo static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3259e705c121SKalle Valo {
3260e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3261e705c121SKalle Valo 
3262e705c121SKalle Valo 	mvm->rts_threshold = value;
3263e705c121SKalle Valo 
3264e705c121SKalle Valo 	return 0;
3265e705c121SKalle Valo }
3266e705c121SKalle Valo 
3267e705c121SKalle Valo static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3268e705c121SKalle Valo 				  struct ieee80211_vif *vif,
3269e705c121SKalle Valo 				  struct ieee80211_sta *sta, u32 changed)
3270e705c121SKalle Valo {
3271e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3272dcfe3b10SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3273dcfe3b10SJohannes Berg 
3274dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
3275dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3276dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
3277dcfe3b10SJohannes Berg 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3278dcfe3b10SJohannes Berg 				     true);
3279e705c121SKalle Valo 
3280e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
3281e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
3282e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
3283e705c121SKalle Valo }
3284e705c121SKalle Valo 
3285e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3286e705c121SKalle Valo 			       struct ieee80211_vif *vif, u16 ac,
3287e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
3288e705c121SKalle Valo {
3289e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3290e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3291e705c121SKalle Valo 
3292e705c121SKalle Valo 	mvmvif->queue_params[ac] = *params;
3293e705c121SKalle Valo 
3294e705c121SKalle Valo 	/*
3295e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
3296e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
3297e705c121SKalle Valo 	 */
3298e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3299e705c121SKalle Valo 		int ret;
3300e705c121SKalle Valo 
3301e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
3302e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3303e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3304e705c121SKalle Valo 		return ret;
3305e705c121SKalle Valo 	}
3306e705c121SKalle Valo 	return 0;
3307e705c121SKalle Valo }
3308e705c121SKalle Valo 
3309e705c121SKalle Valo static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3310d4e36e55SIlan Peer 				       struct ieee80211_vif *vif,
3311d4e36e55SIlan Peer 				       u16 req_duration)
3312e705c121SKalle Valo {
3313e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
33147c70fee5SSara Sharon 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
33157c70fee5SSara Sharon 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
3316e705c121SKalle Valo 
3317d4e36e55SIlan Peer 	if (req_duration > duration)
3318d4e36e55SIlan Peer 		duration = req_duration;
3319d4e36e55SIlan Peer 
3320e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3321fe959c7bSEmmanuel Grumbach 	/* Try really hard to protect the session and hear a beacon
3322fe959c7bSEmmanuel Grumbach 	 * The new session protection command allows us to protect the
3323fe959c7bSEmmanuel Grumbach 	 * session for a much longer time since the firmware will internally
3324fe959c7bSEmmanuel Grumbach 	 * create two events: a 300TU one with a very high priority that
3325fe959c7bSEmmanuel Grumbach 	 * won't be fragmented which should be enough for 99% of the cases,
3326fe959c7bSEmmanuel Grumbach 	 * and another one (which we configure here to be 900TU long) which
3327fe959c7bSEmmanuel Grumbach 	 * will have a slightly lower priority, but more importantly, can be
3328fe959c7bSEmmanuel Grumbach 	 * fragmented so that it'll allow other activities to run.
3329fe959c7bSEmmanuel Grumbach 	 */
3330fe959c7bSEmmanuel Grumbach 	if (fw_has_capa(&mvm->fw->ucode_capa,
3331fe959c7bSEmmanuel Grumbach 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
3332fe959c7bSEmmanuel Grumbach 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
3333b5b878e3SEmmanuel Grumbach 						    min_duration, false);
3334fe959c7bSEmmanuel Grumbach 	else
3335fe959c7bSEmmanuel Grumbach 		iwl_mvm_protect_session(mvm, vif, duration,
3336fe959c7bSEmmanuel Grumbach 					min_duration, 500, false);
3337e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3338e705c121SKalle Valo }
3339e705c121SKalle Valo 
3340e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3341e705c121SKalle Valo 					struct ieee80211_vif *vif,
3342e705c121SKalle Valo 					struct cfg80211_sched_scan_request *req,
3343e705c121SKalle Valo 					struct ieee80211_scan_ies *ies)
3344e705c121SKalle Valo {
3345e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3346e705c121SKalle Valo 
3347e705c121SKalle Valo 	int ret;
3348e705c121SKalle Valo 
3349e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3350e705c121SKalle Valo 
3351e705c121SKalle Valo 	if (!vif->bss_conf.idle) {
3352e705c121SKalle Valo 		ret = -EBUSY;
3353e705c121SKalle Valo 		goto out;
3354e705c121SKalle Valo 	}
3355e705c121SKalle Valo 
3356e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
3357e705c121SKalle Valo 
3358e705c121SKalle Valo out:
3359e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3360e705c121SKalle Valo 	return ret;
3361e705c121SKalle Valo }
3362e705c121SKalle Valo 
3363e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3364e705c121SKalle Valo 				       struct ieee80211_vif *vif)
3365e705c121SKalle Valo {
3366e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3367e705c121SKalle Valo 	int ret;
3368e705c121SKalle Valo 
3369e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3370e705c121SKalle Valo 
3371e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3372e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
3373e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
3374e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
3375e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3376e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
3377e705c121SKalle Valo 	 * not running.
3378e705c121SKalle Valo 	*/
3379e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
3380e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3381e705c121SKalle Valo 		return 0;
3382e705c121SKalle Valo 	}
3383e705c121SKalle Valo 
3384e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
3385e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3386e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
3387e705c121SKalle Valo 
3388e705c121SKalle Valo 	return ret;
3389e705c121SKalle Valo }
3390e705c121SKalle Valo 
33916569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3392e705c121SKalle Valo 				 enum set_key_cmd cmd,
3393e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3394e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3395e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
3396e705c121SKalle Valo {
3397c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3398e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3399f5e28eacSJohannes Berg 	struct iwl_mvm_sta *mvmsta;
3400f5e28eacSJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn;
3401f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
3402c56e00a3SJohannes Berg 	int ret, i;
34034615fd15SEmmanuel Grumbach 	u8 key_offset;
3404e705c121SKalle Valo 
3405e705c121SKalle Valo 	switch (key->cipher) {
3406e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3407286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
3408e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
34091ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
34107f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
34117f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
34127f768ad5SDavid Spinadel 		} else {
34137f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
34147f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
34157f768ad5SDavid Spinadel 		}
3416e705c121SKalle Valo 		break;
3417e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
34182a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
34192a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
342085aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
3421e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3422e705c121SKalle Valo 		break;
3423e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
34248e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
34258e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3426e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
3427e705c121SKalle Valo 		break;
3428e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3429e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3430475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
3431e705c121SKalle Valo 			break;
3432475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
3433475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
3434475c6bdeSJohannes Berg 		/* support HW crypto on TX */
3435475c6bdeSJohannes Berg 		return 0;
3436e705c121SKalle Valo 	default:
3437e705c121SKalle Valo 		/* currently FW supports only one optional cipher scheme */
3438e705c121SKalle Valo 		if (hw->n_cipher_schemes &&
3439e705c121SKalle Valo 		    hw->cipher_schemes->cipher == key->cipher)
3440e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3441e705c121SKalle Valo 		else
3442e705c121SKalle Valo 			return -EOPNOTSUPP;
3443e705c121SKalle Valo 	}
3444e705c121SKalle Valo 
3445e705c121SKalle Valo 	switch (cmd) {
3446e705c121SKalle Valo 	case SET_KEY:
3447e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
3448e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
3449e705c121SKalle Valo 			/*
3450e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
3451e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
3452e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
34538e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
3454e705c121SKalle Valo 			 */
34558e160ab8SAyala Beker 			if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
34568e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3457a1c2ff30SAndrei Otcheretianski 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
345881279c49SJohannes Berg 				ret = -EOPNOTSUPP;
3459a1c2ff30SAndrei Otcheretianski 				break;
3460a1c2ff30SAndrei Otcheretianski 			}
346185aeb58cSDavid Spinadel 
346285aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
346385aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
346485aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
3465e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
3466a1c2ff30SAndrei Otcheretianski 				ret = 0;
3467e705c121SKalle Valo 				break;
3468e705c121SKalle Valo 			}
3469c56e00a3SJohannes Berg 
3470c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
3471c56e00a3SJohannes Berg 				for (i = 0;
3472c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
3473c56e00a3SJohannes Berg 				     i++) {
3474c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
3475c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
3476c56e00a3SJohannes Berg 						break;
3477c56e00a3SJohannes Berg 					}
3478c56e00a3SJohannes Berg 				}
3479c56e00a3SJohannes Berg 
3480c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
3481c56e00a3SJohannes Berg 					ret = -ENOSPC;
3482a1c2ff30SAndrei Otcheretianski 				else
3483a1c2ff30SAndrei Otcheretianski 					ret = 0;
3484c56e00a3SJohannes Berg 
3485c56e00a3SJohannes Berg 				break;
3486c56e00a3SJohannes Berg 			}
348785aeb58cSDavid Spinadel 		}
3488e705c121SKalle Valo 
3489e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
3490e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
3491e705c121SKalle Valo 		 */
3492e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3493e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
3494e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
3495e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
3496e705c121SKalle Valo 			ret = 0;
3497e705c121SKalle Valo 			break;
3498e705c121SKalle Valo 		}
3499e705c121SKalle Valo 
3500f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3501f5e28eacSJohannes Berg 		    sta && iwl_mvm_has_new_rx_api(mvm) &&
3502f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3503f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
35042a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
35052a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3506f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
3507f5e28eacSJohannes Berg 			int tid, q;
3508f5e28eacSJohannes Berg 
3509f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3510f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3511acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
3512acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
3513f5e28eacSJohannes Berg 					 GFP_KERNEL);
3514f5e28eacSJohannes Berg 			if (!ptk_pn) {
3515f5e28eacSJohannes Berg 				ret = -ENOMEM;
3516f5e28eacSJohannes Berg 				break;
3517f5e28eacSJohannes Berg 			}
3518f5e28eacSJohannes Berg 
3519f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3520f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
3521f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
3522f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
3523f5e28eacSJohannes Berg 					       seq.ccmp.pn,
3524f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
3525f5e28eacSJohannes Berg 			}
3526f5e28eacSJohannes Berg 
3527f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3528f5e28eacSJohannes Berg 		}
3529f5e28eacSJohannes Berg 
35304615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
35314615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
35324615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
35334615fd15SEmmanuel Grumbach 		else
35344615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
35354615fd15SEmmanuel Grumbach 
3536e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
35374615fd15SEmmanuel Grumbach 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
3538e705c121SKalle Valo 		if (ret) {
3539e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
3540475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
3541e705c121SKalle Valo 			/*
3542e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
3543475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
3544475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
3545475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
3546e705c121SKalle Valo 			 */
3547475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
3548475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
3549475c6bdeSJohannes Berg 			else
3550e705c121SKalle Valo 				ret = 0;
3551e705c121SKalle Valo 		}
3552e705c121SKalle Valo 
3553e705c121SKalle Valo 		break;
3554e705c121SKalle Valo 	case DISABLE_KEY:
3555c56e00a3SJohannes Berg 		ret = -ENOENT;
3556c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3557c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
3558c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
3559c56e00a3SJohannes Berg 				ret = 0;
3560c56e00a3SJohannes Berg 			}
3561c56e00a3SJohannes Berg 		}
3562c56e00a3SJohannes Berg 
3563c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
3564c56e00a3SJohannes Berg 		if (ret == 0)
3565c56e00a3SJohannes Berg 			break;
3566c56e00a3SJohannes Berg 
3567e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3568e705c121SKalle Valo 			ret = 0;
3569e705c121SKalle Valo 			break;
3570e705c121SKalle Valo 		}
3571e705c121SKalle Valo 
3572f5e28eacSJohannes Berg 		if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3573f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3574f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
35752a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
35762a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3577f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3578f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
3579f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
3580f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
3581f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3582f5e28eacSJohannes Berg 			if (ptk_pn)
3583f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
3584f5e28eacSJohannes Berg 		}
3585f5e28eacSJohannes Berg 
3586e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3587e705c121SKalle Valo 		ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3588e705c121SKalle Valo 		break;
3589e705c121SKalle Valo 	default:
3590e705c121SKalle Valo 		ret = -EINVAL;
3591e705c121SKalle Valo 	}
3592e705c121SKalle Valo 
35936569e7d3SJohannes Berg 	return ret;
35946569e7d3SJohannes Berg }
35956569e7d3SJohannes Berg 
35966569e7d3SJohannes Berg static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
35976569e7d3SJohannes Berg 			       enum set_key_cmd cmd,
35986569e7d3SJohannes Berg 			       struct ieee80211_vif *vif,
35996569e7d3SJohannes Berg 			       struct ieee80211_sta *sta,
36006569e7d3SJohannes Berg 			       struct ieee80211_key_conf *key)
36016569e7d3SJohannes Berg {
36026569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
36036569e7d3SJohannes Berg 	int ret;
36046569e7d3SJohannes Berg 
36056569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
36066569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
3607e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
36086569e7d3SJohannes Berg 
3609e705c121SKalle Valo 	return ret;
3610e705c121SKalle Valo }
3611e705c121SKalle Valo 
3612e705c121SKalle Valo static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3613e705c121SKalle Valo 					struct ieee80211_vif *vif,
3614e705c121SKalle Valo 					struct ieee80211_key_conf *keyconf,
3615e705c121SKalle Valo 					struct ieee80211_sta *sta,
3616e705c121SKalle Valo 					u32 iv32, u16 *phase1key)
3617e705c121SKalle Valo {
3618e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3619e705c121SKalle Valo 
3620e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3621e705c121SKalle Valo 		return;
3622e705c121SKalle Valo 
3623e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3624e705c121SKalle Valo }
3625e705c121SKalle Valo 
3626e705c121SKalle Valo 
3627e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3628e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
3629e705c121SKalle Valo {
3630e705c121SKalle Valo 	struct iwl_mvm *mvm =
3631e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3632e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
3633e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
3634e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
3635e705c121SKalle Valo 
3636e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3637e705c121SKalle Valo 		return true;
3638e705c121SKalle Valo 
3639e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3640e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3641e705c121SKalle Valo 		return true;
3642e705c121SKalle Valo 	}
3643e705c121SKalle Valo 
3644e705c121SKalle Valo 	resp = (void *)pkt->data;
3645e705c121SKalle Valo 
3646e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
3647b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
3648e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
3649e705c121SKalle Valo 
3650e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
3651e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3652e705c121SKalle Valo 		     te_data->uid);
3653e705c121SKalle Valo 
3654e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3655e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3656e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3657e705c121SKalle Valo 
3658e705c121SKalle Valo 	return true;
3659e705c121SKalle Valo }
3660e705c121SKalle Valo 
3661dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3662dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3663dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3664dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3665dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3666e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3667e705c121SKalle Valo 				    struct ieee80211_channel *channel,
3668e705c121SKalle Valo 				    struct ieee80211_vif *vif,
3669e705c121SKalle Valo 				    int duration)
3670e705c121SKalle Valo {
3671afc1e3b4SAvraham Stern 	int res;
3672e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3673e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3674e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
3675e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
3676dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
3677dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
3678dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
3679e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
3680e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3681e705c121SKalle Valo 		.id_and_color =
3682e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3683e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3684e705c121SKalle Valo 	};
368557e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
368657e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
368757e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
368857e861d9SDavid Spinadel 
368957e861d9SDavid Spinadel 	/* Set the channel info data */
369057e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
36913717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
369257e861d9SDavid Spinadel 			      PHY_VHT_CHANNEL_MODE20,
369357e861d9SDavid Spinadel 			      0);
369457e861d9SDavid Spinadel 
369557e861d9SDavid Spinadel 	/* Set the time and duration */
3696afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
3697e705c121SKalle Valo 
3698dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
3699dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
3700dc28e12fSMatti Gottlieb 
3701dc28e12fSMatti Gottlieb 	/*
3702dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
3703dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
3704dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
3705dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
3706dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
3707dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
3708dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
3709dc28e12fSMatti Gottlieb 	 */
3710dc28e12fSMatti Gottlieb 	if (vif->bss_conf.assoc) {
3711dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3712dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
3713dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
3714dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3715dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
3716dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
3717dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
3718dc28e12fSMatti Gottlieb 		}
3719dc28e12fSMatti Gottlieb 	}
3720dc28e12fSMatti Gottlieb 
372157e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
372257e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
3723dc28e12fSMatti Gottlieb 
3724dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
3725903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
3726903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
3727903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
3728903b3f9bSEmmanuel Grumbach 		     "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3729903b3f9bSEmmanuel Grumbach 		     duration, delay, dtim_interval);
3730903b3f9bSEmmanuel Grumbach 
3731e705c121SKalle Valo 	/* Set the node address */
373257e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
3733e705c121SKalle Valo 
3734e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3735e705c121SKalle Valo 
3736e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3737e705c121SKalle Valo 
3738e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3739e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3740e705c121SKalle Valo 		return -EIO;
3741e705c121SKalle Valo 	}
3742e705c121SKalle Valo 
3743e705c121SKalle Valo 	te_data->vif = vif;
3744e705c121SKalle Valo 	te_data->duration = duration;
3745e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
3746e705c121SKalle Valo 
3747e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3748e705c121SKalle Valo 
3749e705c121SKalle Valo 	/*
3750e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
3751e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
3752e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
3753e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
3754e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
3755e705c121SKalle Valo 	 * time another notification (that the time event started)
3756e705c121SKalle Valo 	 * might already be processed unsuccessfully.
3757e705c121SKalle Valo 	 */
3758e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3759e705c121SKalle Valo 				   time_event_response,
3760e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
3761e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
3762e705c121SKalle Valo 
376357e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
3764e705c121SKalle Valo 				   &aux_roc_req);
3765e705c121SKalle Valo 
3766e705c121SKalle Valo 	if (res) {
3767e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3768e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3769e705c121SKalle Valo 		goto out_clear_te;
3770e705c121SKalle Valo 	}
3771e705c121SKalle Valo 
3772e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
3773e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3774e705c121SKalle Valo 	/* should never fail */
3775e705c121SKalle Valo 	WARN_ON_ONCE(res);
3776e705c121SKalle Valo 
3777e705c121SKalle Valo 	if (res) {
3778e705c121SKalle Valo  out_clear_te:
3779e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
3780e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
3781e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3782e705c121SKalle Valo 	}
3783e705c121SKalle Valo 
3784e705c121SKalle Valo 	return res;
3785e705c121SKalle Valo }
3786e705c121SKalle Valo 
3787e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
3788e705c121SKalle Valo 		       struct ieee80211_vif *vif,
3789e705c121SKalle Valo 		       struct ieee80211_channel *channel,
3790e705c121SKalle Valo 		       int duration,
3791e705c121SKalle Valo 		       enum ieee80211_roc_type type)
3792e705c121SKalle Valo {
3793e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3794e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3795e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
3796e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
3797e705c121SKalle Valo 	int ret, i;
3798e705c121SKalle Valo 
3799e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3800e705c121SKalle Valo 			   duration, type);
3801e705c121SKalle Valo 
38029834781cSJohannes Berg 	/*
38039834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
38049834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
38059834781cSJohannes Berg 	 */
3806e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
3807e705c121SKalle Valo 
3808e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3809e705c121SKalle Valo 
3810e705c121SKalle Valo 	switch (vif->type) {
3811e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3812e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
3813e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
3814e705c121SKalle Valo 			/* Use aux roc framework (HS20) */
38152c2c3647SNathan Errera 			if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
38162c2c3647SNathan Errera 						  ADD_STA, 0) >= 12) {
38172c2c3647SNathan Errera 				u32 lmac_id;
38182c2c3647SNathan Errera 
38192c2c3647SNathan Errera 				lmac_id = iwl_mvm_get_lmac_id(mvm->fw,
38202c2c3647SNathan Errera 							      channel->band);
38212c2c3647SNathan Errera 				ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
38222c2c3647SNathan Errera 				if (WARN(ret,
38232c2c3647SNathan Errera 					 "Failed to allocate aux station"))
38242c2c3647SNathan Errera 					goto out_unlock;
38252c2c3647SNathan Errera 			}
3826e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3827e705c121SKalle Valo 						       vif, duration);
3828e705c121SKalle Valo 			goto out_unlock;
3829e705c121SKalle Valo 		}
3830e705c121SKalle Valo 		IWL_ERR(mvm, "hotspot not supported\n");
3831e705c121SKalle Valo 		ret = -EINVAL;
3832e705c121SKalle Valo 		goto out_unlock;
3833e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
3834e705c121SKalle Valo 		/* handle below */
3835e705c121SKalle Valo 		break;
3836e705c121SKalle Valo 	default:
3837e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
3838e705c121SKalle Valo 		ret = -EINVAL;
3839e705c121SKalle Valo 		goto out_unlock;
3840e705c121SKalle Valo 	}
3841e705c121SKalle Valo 
3842e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
3843e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
3844e705c121SKalle Valo 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3845e705c121SKalle Valo 			continue;
3846e705c121SKalle Valo 
3847e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3848e705c121SKalle Valo 			/*
3849e705c121SKalle Valo 			 * Unbind the P2P_DEVICE from the current PHY context,
3850e705c121SKalle Valo 			 * and if the PHY context is not used remove it.
3851e705c121SKalle Valo 			 */
3852e705c121SKalle Valo 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
3853e705c121SKalle Valo 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3854e705c121SKalle Valo 				goto out_unlock;
3855e705c121SKalle Valo 
3856e705c121SKalle Valo 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3857e705c121SKalle Valo 
3858e705c121SKalle Valo 			/* Bind the P2P_DEVICE to the current PHY Context */
3859e705c121SKalle Valo 			mvmvif->phy_ctxt = phy_ctxt;
3860e705c121SKalle Valo 
3861e705c121SKalle Valo 			ret = iwl_mvm_binding_add_vif(mvm, vif);
3862e705c121SKalle Valo 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3863e705c121SKalle Valo 				goto out_unlock;
3864e705c121SKalle Valo 
3865e705c121SKalle Valo 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3866e705c121SKalle Valo 			goto schedule_time_event;
3867e705c121SKalle Valo 		}
3868e705c121SKalle Valo 	}
3869e705c121SKalle Valo 
3870e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
3871e705c121SKalle Valo 	if (channel == mvmvif->phy_ctxt->channel)
3872e705c121SKalle Valo 		goto schedule_time_event;
3873e705c121SKalle Valo 
3874e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
3875e705c121SKalle Valo 
3876e705c121SKalle Valo 	/*
3877e705c121SKalle Valo 	 * Change the PHY context configuration as it is currently referenced
3878e705c121SKalle Valo 	 * only by the P2P Device MAC
3879e705c121SKalle Valo 	 */
3880e705c121SKalle Valo 	if (mvmvif->phy_ctxt->ref == 1) {
3881e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3882e705c121SKalle Valo 					       &chandef, 1, 1);
3883e705c121SKalle Valo 		if (ret)
3884e705c121SKalle Valo 			goto out_unlock;
3885e705c121SKalle Valo 	} else {
3886e705c121SKalle Valo 		/*
3887e705c121SKalle Valo 		 * The PHY context is shared with other MACs. Need to remove the
3888e705c121SKalle Valo 		 * P2P Device from the binding, allocate an new PHY context and
3889e705c121SKalle Valo 		 * create a new binding
3890e705c121SKalle Valo 		 */
3891e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3892e705c121SKalle Valo 		if (!phy_ctxt) {
3893e705c121SKalle Valo 			ret = -ENOSPC;
3894e705c121SKalle Valo 			goto out_unlock;
3895e705c121SKalle Valo 		}
3896e705c121SKalle Valo 
3897e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3898e705c121SKalle Valo 					       1, 1);
3899e705c121SKalle Valo 		if (ret) {
3900e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
3901e705c121SKalle Valo 			goto out_unlock;
3902e705c121SKalle Valo 		}
3903e705c121SKalle Valo 
3904e705c121SKalle Valo 		/* Unbind the P2P_DEVICE from the current PHY context */
3905e705c121SKalle Valo 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
3906e705c121SKalle Valo 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3907e705c121SKalle Valo 			goto out_unlock;
3908e705c121SKalle Valo 
3909e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3910e705c121SKalle Valo 
3911e705c121SKalle Valo 		/* Bind the P2P_DEVICE to the new allocated PHY context */
3912e705c121SKalle Valo 		mvmvif->phy_ctxt = phy_ctxt;
3913e705c121SKalle Valo 
3914e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
3915e705c121SKalle Valo 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3916e705c121SKalle Valo 			goto out_unlock;
3917e705c121SKalle Valo 
3918e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3919e705c121SKalle Valo 	}
3920e705c121SKalle Valo 
3921e705c121SKalle Valo schedule_time_event:
3922e705c121SKalle Valo 	/* Schedule the time events */
3923e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
3924e705c121SKalle Valo 
3925e705c121SKalle Valo out_unlock:
3926e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3927e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3928e705c121SKalle Valo 	return ret;
3929e705c121SKalle Valo }
3930e705c121SKalle Valo 
39315db4c4b9SEmmanuel Grumbach static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
39325db4c4b9SEmmanuel Grumbach 			      struct ieee80211_vif *vif)
3933e705c121SKalle Valo {
3934e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3935e705c121SKalle Valo 
3936e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
3937e705c121SKalle Valo 
3938e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3939fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
3940e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3941e705c121SKalle Valo 
3942e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3943e705c121SKalle Valo 	return 0;
3944e705c121SKalle Valo }
3945e705c121SKalle Valo 
3946b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
3947b73f9a4aSJohannes Berg 	bool responder;
3948b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
3949b73f9a4aSJohannes Berg };
3950b73f9a4aSJohannes Berg 
3951b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
3952b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
3953b73f9a4aSJohannes Berg {
3954b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
3955b73f9a4aSJohannes Berg 
3956b73f9a4aSJohannes Berg 	if (rcu_access_pointer(vif->chanctx_conf) == data->ctx &&
3957b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
3958b73f9a4aSJohannes Berg 		data->responder = true;
3959b73f9a4aSJohannes Berg }
3960b73f9a4aSJohannes Berg 
3961b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
3962b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
3963b73f9a4aSJohannes Berg {
3964b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
3965b73f9a4aSJohannes Berg 		.responder = false,
3966b73f9a4aSJohannes Berg 		.ctx = ctx,
3967b73f9a4aSJohannes Berg 	};
3968b73f9a4aSJohannes Berg 
3969b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
3970b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
3971b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
3972b73f9a4aSJohannes Berg 					&data);
3973b73f9a4aSJohannes Berg 	return data.responder;
3974b73f9a4aSJohannes Berg }
3975b73f9a4aSJohannes Berg 
3976e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
3977e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
3978e705c121SKalle Valo {
3979e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3980e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
3981b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
3982b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
3983e705c121SKalle Valo 	int ret;
3984e705c121SKalle Valo 
3985e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3986e705c121SKalle Valo 
3987e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
3988e705c121SKalle Valo 
3989e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3990e705c121SKalle Valo 	if (!phy_ctxt) {
3991e705c121SKalle Valo 		ret = -ENOSPC;
3992e705c121SKalle Valo 		goto out;
3993e705c121SKalle Valo 	}
3994e705c121SKalle Valo 
3995b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
3996e705c121SKalle Valo 				       ctx->rx_chains_static,
3997e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
3998e705c121SKalle Valo 	if (ret) {
3999e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4000e705c121SKalle Valo 		goto out;
4001e705c121SKalle Valo 	}
4002e705c121SKalle Valo 
4003e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4004e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4005e705c121SKalle Valo out:
4006e705c121SKalle Valo 	return ret;
4007e705c121SKalle Valo }
4008e705c121SKalle Valo 
4009e705c121SKalle Valo static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4010e705c121SKalle Valo 			       struct ieee80211_chanctx_conf *ctx)
4011e705c121SKalle Valo {
4012e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4013e705c121SKalle Valo 	int ret;
4014e705c121SKalle Valo 
4015e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4016e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4017e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4018e705c121SKalle Valo 
4019e705c121SKalle Valo 	return ret;
4020e705c121SKalle Valo }
4021e705c121SKalle Valo 
4022e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4023e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4024e705c121SKalle Valo {
4025e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4026e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4027e705c121SKalle Valo 
4028e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4029e705c121SKalle Valo 
4030e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4031e705c121SKalle Valo }
4032e705c121SKalle Valo 
4033e705c121SKalle Valo static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4034e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx)
4035e705c121SKalle Valo {
4036e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4037e705c121SKalle Valo 
4038e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4039e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4040e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4041e705c121SKalle Valo }
4042e705c121SKalle Valo 
4043e705c121SKalle Valo static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4044e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx,
4045e705c121SKalle Valo 				   u32 changed)
4046e705c121SKalle Valo {
4047e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4048e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4049e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4050b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4051b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4052e705c121SKalle Valo 
4053e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4054e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4055e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4056e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4057e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4058e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4059e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4060e705c121SKalle Valo 		return;
4061e705c121SKalle Valo 
4062e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
40637a20bcceSEmmanuel Grumbach 
40647a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
40657a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4066b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
40677a20bcceSEmmanuel Grumbach 			goto out_unlock;
40687a20bcceSEmmanuel Grumbach 
40697a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
40707a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4071b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
40727a20bcceSEmmanuel Grumbach 			goto out_unlock;
40737a20bcceSEmmanuel Grumbach 	}
40747a20bcceSEmmanuel Grumbach 
4075e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4076b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4077e705c121SKalle Valo 				 ctx->rx_chains_static,
4078e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
40797a20bcceSEmmanuel Grumbach 
40807a20bcceSEmmanuel Grumbach out_unlock:
4081e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4082e705c121SKalle Valo }
4083e705c121SKalle Valo 
4084e705c121SKalle Valo static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4085e705c121SKalle Valo 					struct ieee80211_vif *vif,
4086e705c121SKalle Valo 					struct ieee80211_chanctx_conf *ctx,
4087e705c121SKalle Valo 					bool switching_chanctx)
4088e705c121SKalle Valo {
4089e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4090e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4091e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4092e705c121SKalle Valo 	int ret;
4093e705c121SKalle Valo 
4094e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4095e705c121SKalle Valo 
4096e705c121SKalle Valo 	mvmvif->phy_ctxt = phy_ctxt;
4097e705c121SKalle Valo 
4098e705c121SKalle Valo 	switch (vif->type) {
4099e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4100e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4101e705c121SKalle Valo 		if (switching_chanctx) {
4102e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4103e705c121SKalle Valo 			break;
4104e705c121SKalle Valo 		}
4105244726ecSLuca Coelho 		/* fall through */
4106e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4107e705c121SKalle Valo 		/*
4108e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4109e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4110e705c121SKalle Valo 		 */
4111e705c121SKalle Valo 		ret = 0;
4112e705c121SKalle Valo 		goto out;
4113e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
411419125cb0SAndrei Otcheretianski 		mvmvif->csa_bcn_pending = false;
4115e705c121SKalle Valo 		break;
4116e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4117e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4118e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4119e705c121SKalle Valo 		break;
4120e705c121SKalle Valo 	default:
4121e705c121SKalle Valo 		ret = -EINVAL;
4122e705c121SKalle Valo 		goto out;
4123e705c121SKalle Valo 	}
4124e705c121SKalle Valo 
4125e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4126e705c121SKalle Valo 	if (ret)
4127e705c121SKalle Valo 		goto out;
4128e705c121SKalle Valo 
4129e705c121SKalle Valo 	/*
4130e705c121SKalle Valo 	 * Power state must be updated before quotas,
4131e705c121SKalle Valo 	 * otherwise fw will complain.
4132e705c121SKalle Valo 	 */
4133e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4134e705c121SKalle Valo 
4135e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4136e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4137e705c121SKalle Valo 	 * will handle quota settings.
4138e705c121SKalle Valo 	 */
4139e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4140e705c121SKalle Valo 		mvmvif->monitor_active = true;
4141e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4142e705c121SKalle Valo 		if (ret)
4143e705c121SKalle Valo 			goto out_remove_binding;
41440e39eb03SChaya Rachel Ivgi 
41450e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
41460e39eb03SChaya Rachel Ivgi 		if (ret)
41470e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
41480e39eb03SChaya Rachel Ivgi 
4149e705c121SKalle Valo 	}
4150e705c121SKalle Valo 
4151e705c121SKalle Valo 	/* Handle binding during CSA */
4152e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4153e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4154e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4155e705c121SKalle Valo 	}
4156e705c121SKalle Valo 
4157e705c121SKalle Valo 	if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
415874a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
415974a10252SSara Sharon 
416074a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
416174a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
416219125cb0SAndrei Otcheretianski 			u32 duration = 3 * vif->bss_conf.beacon_int;
4163e705c121SKalle Valo 
4164e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4165e705c121SKalle Valo 			 * beacon on the new channel.
4166e705c121SKalle Valo 			 */
4167e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4168e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4169e705c121SKalle Valo 						true);
417074a10252SSara Sharon 		}
4171e705c121SKalle Valo 
4172e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4173e705c121SKalle Valo 	}
4174e705c121SKalle Valo 
4175e705c121SKalle Valo 	goto out;
4176e705c121SKalle Valo 
4177e705c121SKalle Valo out_remove_binding:
4178e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4179e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4180e705c121SKalle Valo out:
4181e705c121SKalle Valo 	if (ret)
4182e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
4183e705c121SKalle Valo 	return ret;
4184e705c121SKalle Valo }
4185e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4186e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4187e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4188e705c121SKalle Valo {
4189e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4190e705c121SKalle Valo 	int ret;
4191e705c121SKalle Valo 
4192e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4193e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
4194e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4195e705c121SKalle Valo 
4196e705c121SKalle Valo 	return ret;
4197e705c121SKalle Valo }
4198e705c121SKalle Valo 
4199e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4200e705c121SKalle Valo 					   struct ieee80211_vif *vif,
4201e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
4202e705c121SKalle Valo 					   bool switching_chanctx)
4203e705c121SKalle Valo {
4204e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4205e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
4206e705c121SKalle Valo 
4207e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4208e705c121SKalle Valo 
4209e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4210e705c121SKalle Valo 
4211e705c121SKalle Valo 	switch (vif->type) {
4212e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4213e705c121SKalle Valo 		goto out;
4214e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4215e705c121SKalle Valo 		mvmvif->monitor_active = false;
4216e705c121SKalle Valo 		mvmvif->ps_disabled = false;
42170e39eb03SChaya Rachel Ivgi 		iwl_mvm_rm_snif_sta(mvm, vif);
4218e705c121SKalle Valo 		break;
4219e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4220e705c121SKalle Valo 		/* This part is triggered only during CSA */
4221e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
4222e705c121SKalle Valo 			goto out;
4223e705c121SKalle Valo 
4224e705c121SKalle Valo 		mvmvif->csa_countdown = false;
4225e705c121SKalle Valo 
4226e705c121SKalle Valo 		/* Set CS bit on all the stations */
4227e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4228e705c121SKalle Valo 
4229e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
4230e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4231e705c121SKalle Valo 
4232e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
4233e705c121SKalle Valo 		break;
4234e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4235e705c121SKalle Valo 		if (!switching_chanctx)
4236e705c121SKalle Valo 			break;
4237e705c121SKalle Valo 
4238e705c121SKalle Valo 		disabled_vif = vif;
4239e705c121SKalle Valo 
424074a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
424174a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4242e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
4243e705c121SKalle Valo 		break;
4244e705c121SKalle Valo 	default:
4245e705c121SKalle Valo 		break;
4246e705c121SKalle Valo 	}
4247e705c121SKalle Valo 
4248e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
4249e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4250e705c121SKalle Valo 
4251e705c121SKalle Valo out:
4252e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
4253e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4254e705c121SKalle Valo }
4255e705c121SKalle Valo 
4256e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4257e705c121SKalle Valo 					 struct ieee80211_vif *vif,
4258e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
4259e705c121SKalle Valo {
4260e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4261e705c121SKalle Valo 
4262e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4263e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
4264e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4265e705c121SKalle Valo }
4266e705c121SKalle Valo 
4267e705c121SKalle Valo static int
4268e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4269e705c121SKalle Valo 				struct ieee80211_vif_chanctx_switch *vifs)
4270e705c121SKalle Valo {
4271e705c121SKalle Valo 	int ret;
4272e705c121SKalle Valo 
4273e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4274e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4275e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4276e705c121SKalle Valo 
4277e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4278e705c121SKalle Valo 	if (ret) {
4279e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4280e705c121SKalle Valo 		goto out_reassign;
4281e705c121SKalle Valo 	}
4282e705c121SKalle Valo 
4283e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4284e705c121SKalle Valo 					   true);
4285e705c121SKalle Valo 	if (ret) {
4286e705c121SKalle Valo 		IWL_ERR(mvm,
4287e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4288e705c121SKalle Valo 		goto out_remove;
4289e705c121SKalle Valo 	}
4290e705c121SKalle Valo 
4291e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
4292e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
4293e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
4294e705c121SKalle Valo 
4295e705c121SKalle Valo 	goto out;
4296e705c121SKalle Valo 
4297e705c121SKalle Valo out_remove:
4298e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4299e705c121SKalle Valo 
4300e705c121SKalle Valo out_reassign:
4301e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
4302e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4303e705c121SKalle Valo 		goto out_restart;
4304e705c121SKalle Valo 	}
4305e705c121SKalle Valo 
4306e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4307e705c121SKalle Valo 					 true)) {
4308e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4309e705c121SKalle Valo 		goto out_restart;
4310e705c121SKalle Valo 	}
4311e705c121SKalle Valo 
4312e705c121SKalle Valo 	goto out;
4313e705c121SKalle Valo 
4314e705c121SKalle Valo out_restart:
4315e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4316e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4317e705c121SKalle Valo 
4318e705c121SKalle Valo out:
4319e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4320e705c121SKalle Valo 
4321e705c121SKalle Valo 	return ret;
4322e705c121SKalle Valo }
4323e705c121SKalle Valo 
4324e705c121SKalle Valo static int
4325e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4326e705c121SKalle Valo 				    struct ieee80211_vif_chanctx_switch *vifs)
4327e705c121SKalle Valo {
4328e705c121SKalle Valo 	int ret;
4329e705c121SKalle Valo 
4330e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4331e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4332e705c121SKalle Valo 
4333e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4334e705c121SKalle Valo 					   true);
4335e705c121SKalle Valo 	if (ret) {
4336e705c121SKalle Valo 		IWL_ERR(mvm,
4337e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4338e705c121SKalle Valo 		goto out_reassign;
4339e705c121SKalle Valo 	}
4340e705c121SKalle Valo 
4341e705c121SKalle Valo 	goto out;
4342e705c121SKalle Valo 
4343e705c121SKalle Valo out_reassign:
4344e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4345e705c121SKalle Valo 					 true)) {
4346e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4347e705c121SKalle Valo 		goto out_restart;
4348e705c121SKalle Valo 	}
4349e705c121SKalle Valo 
4350e705c121SKalle Valo 	goto out;
4351e705c121SKalle Valo 
4352e705c121SKalle Valo out_restart:
4353e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4354e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4355e705c121SKalle Valo 
4356e705c121SKalle Valo out:
4357e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4358e705c121SKalle Valo 
4359e705c121SKalle Valo 	return ret;
4360e705c121SKalle Valo }
4361e705c121SKalle Valo 
4362e705c121SKalle Valo static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4363e705c121SKalle Valo 				      struct ieee80211_vif_chanctx_switch *vifs,
4364e705c121SKalle Valo 				      int n_vifs,
4365e705c121SKalle Valo 				      enum ieee80211_chanctx_switch_mode mode)
4366e705c121SKalle Valo {
4367e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4368e705c121SKalle Valo 	int ret;
4369e705c121SKalle Valo 
4370e705c121SKalle Valo 	/* we only support a single-vif right now */
4371e705c121SKalle Valo 	if (n_vifs > 1)
4372e705c121SKalle Valo 		return -EOPNOTSUPP;
4373e705c121SKalle Valo 
4374e705c121SKalle Valo 	switch (mode) {
4375e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
4376e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4377e705c121SKalle Valo 		break;
4378e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
4379e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
4380e705c121SKalle Valo 		break;
4381e705c121SKalle Valo 	default:
4382e705c121SKalle Valo 		ret = -EOPNOTSUPP;
4383e705c121SKalle Valo 		break;
4384e705c121SKalle Valo 	}
4385e705c121SKalle Valo 
4386e705c121SKalle Valo 	return ret;
4387e705c121SKalle Valo }
4388e705c121SKalle Valo 
43892f0282dbSJohannes Berg static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
43902f0282dbSJohannes Berg {
43912f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
43922f0282dbSJohannes Berg 
43932f0282dbSJohannes Berg 	return mvm->ibss_manager;
43942f0282dbSJohannes Berg }
43952f0282dbSJohannes Berg 
4396e705c121SKalle Valo static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4397e705c121SKalle Valo 			   struct ieee80211_sta *sta,
4398e705c121SKalle Valo 			   bool set)
4399e705c121SKalle Valo {
4400e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4401e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4402e705c121SKalle Valo 
4403e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
4404e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
4405e705c121SKalle Valo 		return -EINVAL;
4406e705c121SKalle Valo 	}
4407e705c121SKalle Valo 
4408e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4409e705c121SKalle Valo }
4410e705c121SKalle Valo 
4411e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
4412e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4413e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4414e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
4415e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
4416e705c121SKalle Valo };
4417e705c121SKalle Valo 
4418e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4419e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4420e705c121SKalle Valo 				      void *data, int len)
4421e705c121SKalle Valo {
4422e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4423e705c121SKalle Valo 	int err;
4424e705c121SKalle Valo 	u32 noa_duration;
4425e705c121SKalle Valo 
44268cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
44278cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
4428e705c121SKalle Valo 	if (err)
4429e705c121SKalle Valo 		return err;
4430e705c121SKalle Valo 
4431e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
4432e705c121SKalle Valo 		return -EINVAL;
4433e705c121SKalle Valo 
4434e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4435e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
4436e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4437e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
4438e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4439e705c121SKalle Valo 			return -EINVAL;
4440e705c121SKalle Valo 
4441e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4442e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
4443e705c121SKalle Valo 			return -EINVAL;
4444e705c121SKalle Valo 
4445e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
4446e705c121SKalle Valo 		mvm->noa_vif = vif;
4447e705c121SKalle Valo 
444822b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
4449e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4450e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
4451e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4452e705c121SKalle Valo 		    !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
4453e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4454e705c121SKalle Valo 			return -EINVAL;
4455e705c121SKalle Valo 
4456e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
4457e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4458e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4459e705c121SKalle Valo 	}
4460e705c121SKalle Valo 
4461e705c121SKalle Valo 	return -EOPNOTSUPP;
4462e705c121SKalle Valo }
4463e705c121SKalle Valo 
4464e705c121SKalle Valo static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4465e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4466e705c121SKalle Valo 				    void *data, int len)
4467e705c121SKalle Valo {
4468e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4469e705c121SKalle Valo 	int err;
4470e705c121SKalle Valo 
4471e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4472e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4473e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4474e705c121SKalle Valo 
4475e705c121SKalle Valo 	return err;
4476e705c121SKalle Valo }
4477e705c121SKalle Valo #endif
4478e705c121SKalle Valo 
4479e705c121SKalle Valo static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4480e705c121SKalle Valo 				   struct ieee80211_vif *vif,
4481e705c121SKalle Valo 				   struct ieee80211_channel_switch *chsw)
4482e705c121SKalle Valo {
4483e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
4484e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
4485e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
4486e705c121SKalle Valo 	 * (which is when the absence time event starts).
4487e705c121SKalle Valo 	 */
4488e705c121SKalle Valo 
4489e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4490e705c121SKalle Valo 			   "dummy channel switch op\n");
4491e705c121SKalle Valo }
4492e705c121SKalle Valo 
449374a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
449474a10252SSara Sharon 				       struct ieee80211_vif *vif,
449574a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
449674a10252SSara Sharon {
449774a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
449874a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
449974a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
450074a10252SSara Sharon 							  mvmvif->color)),
450174a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
450274a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
450374a10252SSara Sharon 		.cs_count = chsw->count,
450477738865SSara Sharon 		.cs_mode = chsw->block_tx,
450574a10252SSara Sharon 	};
450674a10252SSara Sharon 
450774a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
450874a10252SSara Sharon 
45099cfcf71cSSara Sharon 	if (chsw->delay)
45109cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
45119cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
45129cfcf71cSSara Sharon 
451374a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
451474a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
451574a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
451674a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
451774a10252SSara Sharon }
451874a10252SSara Sharon 
45190202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
45200202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
45210202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
45220202bcf0SEmmanuel Grumbach {
45230202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
45240202bcf0SEmmanuel Grumbach 	u32 apply_time;
45250202bcf0SEmmanuel Grumbach 
45260202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
45270202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
45280202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
45290202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
45300202bcf0SEmmanuel Grumbach 	 * loss).
45310202bcf0SEmmanuel Grumbach 	 */
45320202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
45330202bcf0SEmmanuel Grumbach 		apply_time = 0;
45340202bcf0SEmmanuel Grumbach 	else
45350202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
45360202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
45370202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
45380202bcf0SEmmanuel Grumbach 
45390202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
45400202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
45410202bcf0SEmmanuel Grumbach 
45420202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
45430202bcf0SEmmanuel Grumbach 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
45440202bcf0SEmmanuel Grumbach 
45450202bcf0SEmmanuel Grumbach 		if (ret)
45460202bcf0SEmmanuel Grumbach 			return ret;
45470202bcf0SEmmanuel Grumbach 	}
45480202bcf0SEmmanuel Grumbach 
45490202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
45500202bcf0SEmmanuel Grumbach 				    apply_time);
45510202bcf0SEmmanuel Grumbach 
45520202bcf0SEmmanuel Grumbach 	return 0;
45530202bcf0SEmmanuel Grumbach }
45540202bcf0SEmmanuel Grumbach 
4555f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
4556e705c121SKalle Valo static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4557e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4558e705c121SKalle Valo 				      struct ieee80211_channel_switch *chsw)
4559e705c121SKalle Valo {
4560e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4561e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
4562e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4563e705c121SKalle Valo 	int ret;
4564e705c121SKalle Valo 
4565e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4566e705c121SKalle Valo 
4567e705c121SKalle Valo 	mvmvif->csa_failed = false;
4568e705c121SKalle Valo 
4569e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
4570e705c121SKalle Valo 			   chsw->chandef.center_freq1);
4571e705c121SKalle Valo 
45727174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
45737174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
45747174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
4575e705c121SKalle Valo 
4576e705c121SKalle Valo 	switch (vif->type) {
4577e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4578e705c121SKalle Valo 		csa_vif =
4579e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
4580e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
4581e705c121SKalle Valo 		if (WARN_ONCE(csa_vif && csa_vif->csa_active,
4582e705c121SKalle Valo 			      "Another CSA is already in progress")) {
4583e705c121SKalle Valo 			ret = -EBUSY;
4584e705c121SKalle Valo 			goto out_unlock;
4585e705c121SKalle Valo 		}
4586e705c121SKalle Valo 
4587d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
4588d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
4589d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
4590d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
4591d3a108a4SAndrei Otcheretianski 			goto out_unlock;
4592d3a108a4SAndrei Otcheretianski 		}
4593d3a108a4SAndrei Otcheretianski 
4594e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
4595e705c121SKalle Valo 
4596e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
4597e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
4598e705c121SKalle Valo 			ret = -EBUSY;
4599e705c121SKalle Valo 			goto out_unlock;
4600e705c121SKalle Valo 		}
4601e705c121SKalle Valo 
4602d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
4603d3a108a4SAndrei Otcheretianski 
4604e705c121SKalle Valo 		break;
4605e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4606f6780614SSara Sharon 		if (chsw->block_tx) {
4607f6780614SSara Sharon 			/*
4608f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
4609f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
4610f6780614SSara Sharon 			 */
4611f6780614SSara Sharon 			if (!chsw->count ||
4612f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
4613f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
4614f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
4615f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
4616f6780614SSara Sharon 		}
4617e705c121SKalle Valo 
46180202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
46190202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
46200202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
4621e705c121SKalle Valo 			if (ret)
4622e705c121SKalle Valo 				goto out_unlock;
46230202bcf0SEmmanuel Grumbach 		} else {
462474a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
46250202bcf0SEmmanuel Grumbach 		}
462681b4e44eSSara Sharon 
462781b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
462881b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
4629e705c121SKalle Valo 		break;
4630e705c121SKalle Valo 	default:
4631e705c121SKalle Valo 		break;
4632e705c121SKalle Valo 	}
4633e705c121SKalle Valo 
4634e705c121SKalle Valo 	mvmvif->ps_disabled = true;
4635e705c121SKalle Valo 
4636e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
4637e705c121SKalle Valo 	if (ret)
4638e705c121SKalle Valo 		goto out_unlock;
4639e705c121SKalle Valo 
4640e705c121SKalle Valo 	/* we won't be on this channel any longer */
4641e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
4642e705c121SKalle Valo 
4643e705c121SKalle Valo out_unlock:
4644e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4645e705c121SKalle Valo 
4646e705c121SKalle Valo 	return ret;
4647e705c121SKalle Valo }
4648e705c121SKalle Valo 
4649c37763d2SSara Sharon static void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
4650c37763d2SSara Sharon 					     struct ieee80211_vif *vif,
4651c37763d2SSara Sharon 					     struct ieee80211_channel_switch *chsw)
4652e705c121SKalle Valo {
4653e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4654c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4655c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
4656c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4657c37763d2SSara Sharon 							  mvmvif->color)),
4658c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
4659c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
4660c37763d2SSara Sharon 		.cs_count = chsw->count,
466177738865SSara Sharon 		.cs_mode = chsw->block_tx,
4662c37763d2SSara Sharon 	};
4663e705c121SKalle Valo 
4664c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
4665c37763d2SSara Sharon 		return;
4666e705c121SKalle Valo 
466781b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
466881b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
466981b4e44eSSara Sharon 			/* Second time, give up on this AP*/
467081b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
467181b4e44eSSara Sharon 			ieee80211_chswitch_done(vif, false);
467281b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
467381b4e44eSSara Sharon 			return;
4674e705c121SKalle Valo 		}
467581b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
4676e705c121SKalle Valo 	}
467781b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
4678e705c121SKalle Valo 
4679c37763d2SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d\n", mvmvif->id);
4680e705c121SKalle Valo 
4681c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
4682c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
4683c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
4684c37763d2SSara Sharon 				     CMD_ASYNC, sizeof(cmd), &cmd));
4685e705c121SKalle Valo }
4686e705c121SKalle Valo 
46876110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
46886110d9e5SDavid Spinadel {
4689435d0827SSara Sharon 	int i;
4690435d0827SSara Sharon 
469106195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
4692309c4848SLuca Coelho 		if (drop) {
4693309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
46946110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
4695435d0827SSara Sharon 				iwl_mvm_flushable_queues(mvm) & queues, 0);
4696309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
4697309c4848SLuca Coelho 		} else {
4698435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
4699309c4848SLuca Coelho 		}
4700435d0827SSara Sharon 		return;
4701435d0827SSara Sharon 	}
47026110d9e5SDavid Spinadel 
47036110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
4704be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4705435d0827SSara Sharon 		struct ieee80211_sta *sta;
4706435d0827SSara Sharon 
4707435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
47086110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
47096110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
47106110d9e5SDavid Spinadel 			continue;
47116110d9e5SDavid Spinadel 
471206195639SSara Sharon 		if (drop)
4713d66bd0c4SHaim Dreyfuss 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF, 0);
471406195639SSara Sharon 		else
47156110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
47166110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
47176110d9e5SDavid Spinadel 	}
47186110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
47196110d9e5SDavid Spinadel }
47206110d9e5SDavid Spinadel 
4721e705c121SKalle Valo static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4722e705c121SKalle Valo 			      struct ieee80211_vif *vif, u32 queues, bool drop)
4723e705c121SKalle Valo {
4724e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4725e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
4726e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4727e705c121SKalle Valo 	struct ieee80211_sta *sta;
4728e705c121SKalle Valo 	int i;
4729e705c121SKalle Valo 	u32 msk = 0;
4730e705c121SKalle Valo 
47316110d9e5SDavid Spinadel 	if (!vif) {
47326110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
47336110d9e5SDavid Spinadel 		return;
47346110d9e5SDavid Spinadel 	}
47356110d9e5SDavid Spinadel 
47366110d9e5SDavid Spinadel 	if (vif->type != NL80211_IFTYPE_STATION)
4737e705c121SKalle Valo 		return;
4738e705c121SKalle Valo 
473924afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
474024afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
474124afba76SLiad Kaufman 
4742e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4743e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
4744e705c121SKalle Valo 
4745e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
4746be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4747e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4748e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
4749e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
4750e705c121SKalle Valo 			continue;
4751e705c121SKalle Valo 
4752e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4753e705c121SKalle Valo 		if (mvmsta->vif != vif)
4754e705c121SKalle Valo 			continue;
4755e705c121SKalle Valo 
4756e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
4757e705c121SKalle Valo 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4758e705c121SKalle Valo 
4759d49394a1SSara Sharon 		if (drop) {
4760f9084775SNathan Errera 			if (iwl_mvm_flush_sta(mvm, mvmsta, false))
4761d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
4762d49394a1SSara Sharon 		} else {
4763e705c121SKalle Valo 			msk |= mvmsta->tfd_queue_msk;
4764d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
4765d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
4766e705c121SKalle Valo 		}
4767d49394a1SSara Sharon 	}
4768e705c121SKalle Valo 
4769e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4770e705c121SKalle Valo 
4771e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
4772e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
4773e705c121SKalle Valo 	 */
4774d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
4775a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
4776e705c121SKalle Valo }
4777e705c121SKalle Valo 
4778e705c121SKalle Valo static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4779e705c121SKalle Valo 				  struct survey_info *survey)
4780e705c121SKalle Valo {
4781e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4782e705c121SKalle Valo 	int ret;
4783e705c121SKalle Valo 
4784e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
4785e705c121SKalle Valo 
4786e705c121SKalle Valo 	/* only support global statistics right now */
4787e705c121SKalle Valo 	if (idx != 0)
4788e705c121SKalle Valo 		return -ENOENT;
4789e705c121SKalle Valo 
4790280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4791e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4792e705c121SKalle Valo 		return -ENOENT;
4793e705c121SKalle Valo 
4794e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4795e705c121SKalle Valo 
4796aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
4797e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
4798e705c121SKalle Valo 		if (ret)
4799e705c121SKalle Valo 			goto out;
4800e705c121SKalle Valo 	}
4801e705c121SKalle Valo 
4802e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
4803e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
4804e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
4805e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
4806e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
4807e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
4808e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
4809e705c121SKalle Valo 
4810e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
4811e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
4812e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
4813e705c121SKalle Valo 
4814e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
4815e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
4816e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
4817e705c121SKalle Valo 
4818e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4819e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
4820e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
4821e705c121SKalle Valo 
4822e705c121SKalle Valo 	ret = 0;
4823e705c121SKalle Valo  out:
4824e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4825e705c121SKalle Valo 	return ret;
4826e705c121SKalle Valo }
4827e705c121SKalle Valo 
4828ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
4829ed780545SJohannes Berg {
4830ed780545SJohannes Berg 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
4831ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
4832ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
4833ed780545SJohannes Berg 		break;
4834ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
4835ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
4836ed780545SJohannes Berg 		break;
4837ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
4838ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
4839ed780545SJohannes Berg 		break;
4840ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
4841ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
4842ed780545SJohannes Berg 		break;
4843ed780545SJohannes Berg 	}
4844ed780545SJohannes Berg 
4845ed780545SJohannes Berg 	if (rate_n_flags & RATE_MCS_HT_MSK) {
4846ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
4847ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags, RATE_HT_MCS_INDEX_MSK);
4848ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4849ed780545SJohannes Berg 					  RATE_HT_MCS_NSS_MSK) + 1;
4850ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_SGI_MSK)
4851ed780545SJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
4852ed780545SJohannes Berg 	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
4853ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
4854ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags,
4855ed780545SJohannes Berg 					  RATE_VHT_MCS_RATE_CODE_MSK);
4856ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4857ed780545SJohannes Berg 					  RATE_VHT_MCS_NSS_MSK) + 1;
4858ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_SGI_MSK)
4859ed780545SJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
4860ed780545SJohannes Berg 	} else if (rate_n_flags & RATE_MCS_HE_MSK) {
4861ed780545SJohannes Berg 		u32 gi_ltf = u32_get_bits(rate_n_flags,
4862ed780545SJohannes Berg 					  RATE_MCS_HE_GI_LTF_MSK);
4863ed780545SJohannes Berg 
4864ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
4865ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags,
4866ed780545SJohannes Berg 					  RATE_VHT_MCS_RATE_CODE_MSK);
4867ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4868ed780545SJohannes Berg 					  RATE_VHT_MCS_NSS_MSK) + 1;
4869ed780545SJohannes Berg 
4870ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
4871ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
4872ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
4873ed780545SJohannes Berg 		}
4874ed780545SJohannes Berg 
4875ed780545SJohannes Berg 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
4876ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_SU:
4877ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_EXT_SU:
4878ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4879ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4880ed780545SJohannes Berg 			else if (gi_ltf == 2)
4881ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4882ed780545SJohannes Berg 			else if (rate_n_flags & RATE_MCS_SGI_MSK)
4883ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4884ed780545SJohannes Berg 			else
4885ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4886ed780545SJohannes Berg 			break;
4887ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_MU:
4888ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4889ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4890ed780545SJohannes Berg 			else if (gi_ltf == 2)
4891ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4892ed780545SJohannes Berg 			else
4893ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4894ed780545SJohannes Berg 			break;
4895ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_TRIG:
4896ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4897ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4898ed780545SJohannes Berg 			else
4899ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4900ed780545SJohannes Berg 			break;
4901ed780545SJohannes Berg 		}
4902ed780545SJohannes Berg 
4903ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
4904ed780545SJohannes Berg 			rinfo->he_dcm = 1;
4905ed780545SJohannes Berg 	} else {
4906ed780545SJohannes Berg 		switch (u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK)) {
4907ed780545SJohannes Berg 		case IWL_RATE_1M_PLCP:
4908ed780545SJohannes Berg 			rinfo->legacy = 10;
4909ed780545SJohannes Berg 			break;
4910ed780545SJohannes Berg 		case IWL_RATE_2M_PLCP:
4911ed780545SJohannes Berg 			rinfo->legacy = 20;
4912ed780545SJohannes Berg 			break;
4913ed780545SJohannes Berg 		case IWL_RATE_5M_PLCP:
4914ed780545SJohannes Berg 			rinfo->legacy = 55;
4915ed780545SJohannes Berg 			break;
4916ed780545SJohannes Berg 		case IWL_RATE_11M_PLCP:
4917ed780545SJohannes Berg 			rinfo->legacy = 110;
4918ed780545SJohannes Berg 			break;
4919ed780545SJohannes Berg 		case IWL_RATE_6M_PLCP:
4920ed780545SJohannes Berg 			rinfo->legacy = 60;
4921ed780545SJohannes Berg 			break;
4922ed780545SJohannes Berg 		case IWL_RATE_9M_PLCP:
4923ed780545SJohannes Berg 			rinfo->legacy = 90;
4924ed780545SJohannes Berg 			break;
4925ed780545SJohannes Berg 		case IWL_RATE_12M_PLCP:
4926ed780545SJohannes Berg 			rinfo->legacy = 120;
4927ed780545SJohannes Berg 			break;
4928ed780545SJohannes Berg 		case IWL_RATE_18M_PLCP:
4929ed780545SJohannes Berg 			rinfo->legacy = 180;
4930ed780545SJohannes Berg 			break;
4931ed780545SJohannes Berg 		case IWL_RATE_24M_PLCP:
4932ed780545SJohannes Berg 			rinfo->legacy = 240;
4933ed780545SJohannes Berg 			break;
4934ed780545SJohannes Berg 		case IWL_RATE_36M_PLCP:
4935ed780545SJohannes Berg 			rinfo->legacy = 360;
4936ed780545SJohannes Berg 			break;
4937ed780545SJohannes Berg 		case IWL_RATE_48M_PLCP:
4938ed780545SJohannes Berg 			rinfo->legacy = 480;
4939ed780545SJohannes Berg 			break;
4940ed780545SJohannes Berg 		case IWL_RATE_54M_PLCP:
4941ed780545SJohannes Berg 			rinfo->legacy = 540;
4942ed780545SJohannes Berg 			break;
4943ed780545SJohannes Berg 		}
4944ed780545SJohannes Berg 	}
4945ed780545SJohannes Berg }
4946ed780545SJohannes Berg 
4947e705c121SKalle Valo static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
4948e705c121SKalle Valo 				       struct ieee80211_vif *vif,
4949e705c121SKalle Valo 				       struct ieee80211_sta *sta,
4950e705c121SKalle Valo 				       struct station_info *sinfo)
4951e705c121SKalle Valo {
4952e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4953e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4954e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4955e705c121SKalle Valo 
4956988b5968SSara Sharon 	if (mvmsta->avg_energy) {
495788ad368aSBen Greear 		sinfo->signal_avg = -(s8)mvmsta->avg_energy;
495822d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
4959988b5968SSara Sharon 	}
4960988b5968SSara Sharon 
4961ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
4962ed780545SJohannes Berg 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
4963ed780545SJohannes Berg 
4964ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
4965ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
4966ed780545SJohannes Berg 	}
4967ed780545SJohannes Berg 
4968e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
4969e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
4970e705c121SKalle Valo 		return;
4971e705c121SKalle Valo 
4972e705c121SKalle Valo 	if (!vif->bss_conf.assoc)
4973e705c121SKalle Valo 		return;
4974e705c121SKalle Valo 
4975e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4976e705c121SKalle Valo 
4977e705c121SKalle Valo 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
4978e705c121SKalle Valo 		goto unlock;
4979e705c121SKalle Valo 
4980e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
4981e705c121SKalle Valo 		goto unlock;
4982e705c121SKalle Valo 
4983e705c121SKalle Valo 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
4984e705c121SKalle Valo 			   mvmvif->beacon_stats.accu_num_beacons;
498522d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
4986e705c121SKalle Valo 	if (mvmvif->beacon_stats.avg_signal) {
4987e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
4988e705c121SKalle Valo 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
498922d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
4990e705c121SKalle Valo 	}
4991e705c121SKalle Valo  unlock:
4992e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4993e705c121SKalle Valo }
4994e705c121SKalle Valo 
4995e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
4996e705c121SKalle Valo 					struct ieee80211_vif *vif,
4997e705c121SKalle Valo 					const struct ieee80211_event *event)
4998e705c121SKalle Valo {
49994c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5000e705c121SKalle Valo 	do {								\
50014c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5002e705c121SKalle Valo 			break;						\
50037174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5004e705c121SKalle Valo 	} while (0)
5005e705c121SKalle Valo 
5006e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5007e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5008e705c121SKalle Valo 
50096c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
50106c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
50116c042d75SSara Sharon 	if (!trig)
5012e705c121SKalle Valo 		return;
5013e705c121SKalle Valo 
5014e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
5015e705c121SKalle Valo 
5016e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
5017e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
50184c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
5019e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
5020e705c121SKalle Valo 					    event->u.mlme.reason);
5021e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
50224c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
5023e705c121SKalle Valo 					   "ASSOC TIMEOUT");
5024e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
5025e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
50264c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
5027e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
5028e705c121SKalle Valo 					   event->u.mlme.reason);
5029e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
50304c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
5031e705c121SKalle Valo 					   "AUTH TIMEOUT");
5032e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
50334c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
5034e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
5035e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
50364c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
5037e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
5038e705c121SKalle Valo 	}
5039e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
5040e705c121SKalle Valo }
5041e705c121SKalle Valo 
5042e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5043e705c121SKalle Valo 					  struct ieee80211_vif *vif,
5044e705c121SKalle Valo 					  const struct ieee80211_event *event)
5045e705c121SKalle Valo {
5046e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5047e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
5048e705c121SKalle Valo 
50496c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
50506c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
50516c042d75SSara Sharon 	if (!trig)
5052e705c121SKalle Valo 		return;
5053e705c121SKalle Valo 
5054e705c121SKalle Valo 	ba_trig = (void *)trig->data;
5055e705c121SKalle Valo 
5056e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5057e705c121SKalle Valo 		return;
5058e705c121SKalle Valo 
50597174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
5060e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
5061e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
5062e705c121SKalle Valo 				event->u.ba.ssn);
5063e705c121SKalle Valo }
5064e705c121SKalle Valo 
5065e705c121SKalle Valo static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
5066e705c121SKalle Valo 				       struct ieee80211_vif *vif,
5067e705c121SKalle Valo 				       const struct ieee80211_event *event)
5068e705c121SKalle Valo {
5069e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5070e705c121SKalle Valo 
5071e705c121SKalle Valo 	switch (event->type) {
5072e705c121SKalle Valo 	case MLME_EVENT:
5073e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
5074e705c121SKalle Valo 		break;
5075e705c121SKalle Valo 	case BAR_RX_EVENT:
5076e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
5077e705c121SKalle Valo 		break;
5078e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
5079528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
5080528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
5081e705c121SKalle Valo 		break;
5082e705c121SKalle Valo 	default:
5083e705c121SKalle Valo 		break;
5084e705c121SKalle Valo 	}
5085e705c121SKalle Valo }
5086e705c121SKalle Valo 
5087d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
5088d0ff5d22SSara Sharon 				     struct iwl_mvm_internal_rxq_notif *notif,
5089d0ff5d22SSara Sharon 				     u32 size)
50900636b938SSara Sharon {
50910636b938SSara Sharon 	u32 qmask = BIT(mvm->trans->num_rx_queues) - 1;
50920636b938SSara Sharon 	int ret;
50930636b938SSara Sharon 
50940636b938SSara Sharon 
5095cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
50960636b938SSara Sharon 		return;
50970636b938SSara Sharon 
5098a2113cc4SNaftali Goldstein 	if (notif->sync) {
5099d0ff5d22SSara Sharon 		notif->cookie = mvm->queue_sync_cookie;
5100d0ff5d22SSara Sharon 		atomic_set(&mvm->queue_sync_counter,
5101d0ff5d22SSara Sharon 			   mvm->trans->num_rx_queues);
5102a2113cc4SNaftali Goldstein 	}
5103d0ff5d22SSara Sharon 
51043c514bf8SEmmanuel Grumbach 	ret = iwl_mvm_notify_rx_queue(mvm, qmask, (u8 *)notif,
51053c514bf8SEmmanuel Grumbach 				      size, !notif->sync);
51060636b938SSara Sharon 	if (ret) {
51070636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
51080636b938SSara Sharon 		goto out;
51090636b938SSara Sharon 	}
5110d0ff5d22SSara Sharon 
51116ad04359SJohannes Berg 	if (notif->sync) {
51123c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
51133a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
51146ad04359SJohannes Berg 					 atomic_read(&mvm->queue_sync_counter) == 0 ||
51156ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
51160636b938SSara Sharon 					 HZ);
51176ad04359SJohannes Berg 		WARN_ON_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm));
51186ad04359SJohannes Berg 	}
51190636b938SSara Sharon 
51200636b938SSara Sharon out:
51210636b938SSara Sharon 	atomic_set(&mvm->queue_sync_counter, 0);
5122a2113cc4SNaftali Goldstein 	if (notif->sync)
51230636b938SSara Sharon 		mvm->queue_sync_cookie++;
51240636b938SSara Sharon }
51250636b938SSara Sharon 
51260636b938SSara Sharon static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
51270636b938SSara Sharon {
51280636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5129d0ff5d22SSara Sharon 	struct iwl_mvm_internal_rxq_notif data = {
5130d0ff5d22SSara Sharon 		.type = IWL_MVM_RXQ_EMPTY,
5131d0ff5d22SSara Sharon 		.sync = 1,
5132d0ff5d22SSara Sharon 	};
51330636b938SSara Sharon 
51340636b938SSara Sharon 	mutex_lock(&mvm->mutex);
5135d0ff5d22SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, &data, sizeof(data));
51360636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
51370636b938SSara Sharon }
51380636b938SSara Sharon 
5139b73f9a4aSJohannes Berg static int
5140b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
5141b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
5142b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
5143b73f9a4aSJohannes Berg {
5144b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5145b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5146b73f9a4aSJohannes Berg 
5147b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
5148b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
5149b73f9a4aSJohannes Berg 		return -EINVAL;
5150b73f9a4aSJohannes Berg 
5151b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
5152b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
5153b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
5154b73f9a4aSJohannes Berg 
5155b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
5156b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
5157b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
5158b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
5159b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
5160b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
5161b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
5162b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
5163b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
5164b73f9a4aSJohannes Berg 
5165b73f9a4aSJohannes Berg 	return 0;
5166b73f9a4aSJohannes Berg }
5167b73f9a4aSJohannes Berg 
5168fc36ffdaSJohannes Berg static int iwl_mvm_start_pmsr(struct ieee80211_hw *hw,
5169fc36ffdaSJohannes Berg 			      struct ieee80211_vif *vif,
5170fc36ffdaSJohannes Berg 			      struct cfg80211_pmsr_request *request)
5171fc36ffdaSJohannes Berg {
5172fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5173fc36ffdaSJohannes Berg 	int ret;
5174fc36ffdaSJohannes Berg 
5175fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5176fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
5177fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5178fc36ffdaSJohannes Berg 
5179fc36ffdaSJohannes Berg 	return ret;
5180fc36ffdaSJohannes Berg }
5181fc36ffdaSJohannes Berg 
5182fc36ffdaSJohannes Berg static void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw,
5183fc36ffdaSJohannes Berg 			       struct ieee80211_vif *vif,
5184fc36ffdaSJohannes Berg 			       struct cfg80211_pmsr_request *request)
5185fc36ffdaSJohannes Berg {
5186fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5187fc36ffdaSJohannes Berg 
5188fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5189fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
5190fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5191fc36ffdaSJohannes Berg }
5192fc36ffdaSJohannes Berg 
5193438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
5194438af969SSara Sharon {
5195438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
5196438af969SSara Sharon 
5197438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
5198438af969SSara Sharon 		return false;
5199438af969SSara Sharon 
5200438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
5201438af969SSara Sharon }
5202438af969SSara Sharon 
5203438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
5204438af969SSara Sharon 				      struct sk_buff *head,
5205438af969SSara Sharon 				      struct sk_buff *skb)
5206438af969SSara Sharon {
5207438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5208438af969SSara Sharon 
5209438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
5210438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
5211438af969SSara Sharon 		return false;
5212438af969SSara Sharon 
5213438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
5214438af969SSara Sharon 		return true;
5215438af969SSara Sharon 
5216438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5217438af969SSara Sharon }
5218438af969SSara Sharon 
5219e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
5220e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
5221cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
5222e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
5223e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
5224e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
5225e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
5226e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
5227e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
5228e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
5229e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
5230e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
5231e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
5232e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
5233e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
5234e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
5235e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
5236e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
5237e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
5238e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
5239e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
5240e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
5241e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
5242e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
5243e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
5244e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
5245e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
5246e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
5247e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
5248e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
5249e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
5250e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
5251e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
5252e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
5253e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
5254e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
5255e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
5256e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5257e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
5258e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
5259e705c121SKalle Valo 
5260e705c121SKalle Valo 	.start_ap = iwl_mvm_start_ap_ibss,
5261e705c121SKalle Valo 	.stop_ap = iwl_mvm_stop_ap_ibss,
5262e705c121SKalle Valo 	.join_ibss = iwl_mvm_start_ap_ibss,
5263e705c121SKalle Valo 	.leave_ibss = iwl_mvm_stop_ap_ibss,
5264e705c121SKalle Valo 
52652f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
52662f0282dbSJohannes Berg 
5267e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
5268e705c121SKalle Valo 
5269e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
5270e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
5271e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
527279221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
5273c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
5274e705c121SKalle Valo 
5275e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5276e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5277e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5278e705c121SKalle Valo 
5279e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
5280e705c121SKalle Valo 
52810636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
52820636b938SSara Sharon 
5283e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5284e705c121SKalle Valo 
5285e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
5286e705c121SKalle Valo 	/* look at d3.c */
5287e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
5288e705c121SKalle Valo 	.resume = iwl_mvm_resume,
5289e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
5290e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
5291e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
5292e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
5293e705c121SKalle Valo #endif
5294e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
5295e705c121SKalle Valo #endif
5296e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
5297e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
5298b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
5299fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
5300fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
5301b73f9a4aSJohannes Berg 
5302438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
5303177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
5304177a11cfSGregory Greenman 	.sta_add_debugfs = iwl_mvm_sta_add_debugfs,
5305177a11cfSGregory Greenman #endif
5306e705c121SKalle Valo };
5307