xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision fe959c7b20495fe05de5f65ea0968a7b128afa9e)
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  *
8e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
926d6c16bSSara Sharon  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
1026d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11c37763d2SSara Sharon  * Copyright(c) 2018 - 2019 Intel Corporation
12e705c121SKalle Valo  *
13e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
14e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
15e705c121SKalle Valo  * published by the Free Software Foundation.
16e705c121SKalle Valo  *
17e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
18e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
19e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20e705c121SKalle Valo  * General Public License for more details.
21e705c121SKalle Valo  *
22e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
23e705c121SKalle Valo  * in the file called COPYING.
24e705c121SKalle Valo  *
25e705c121SKalle Valo  * Contact Information:
26cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
27e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * BSD LICENSE
30e705c121SKalle Valo  *
31e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
3226d6c16bSSara Sharon  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
3326d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34c37763d2SSara Sharon  * Copyright(c) 2018 - 2019 Intel Corporation
35e705c121SKalle Valo  * All rights reserved.
36e705c121SKalle Valo  *
37e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
38e705c121SKalle Valo  * modification, are permitted provided that the following conditions
39e705c121SKalle Valo  * are met:
40e705c121SKalle Valo  *
41e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
42e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
43e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
44e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
45e705c121SKalle Valo  *    the documentation and/or other materials provided with the
46e705c121SKalle Valo  *    distribution.
47e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
48e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
49e705c121SKalle Valo  *    from this software without specific prior written permission.
50e705c121SKalle Valo  *
51e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62e705c121SKalle Valo  *
63e705c121SKalle Valo  *****************************************************************************/
64e705c121SKalle Valo #include <linux/kernel.h>
65e705c121SKalle Valo #include <linux/slab.h>
66e705c121SKalle Valo #include <linux/skbuff.h>
67e705c121SKalle Valo #include <linux/netdevice.h>
68e705c121SKalle Valo #include <linux/etherdevice.h>
69e705c121SKalle Valo #include <linux/ip.h>
70e705c121SKalle Valo #include <linux/if_arp.h>
712f89a5d7SGolan Ben-Ami #include <linux/time.h>
72e705c121SKalle Valo #include <net/mac80211.h>
73e705c121SKalle Valo #include <net/ieee80211_radiotap.h>
74e705c121SKalle Valo #include <net/tcp.h>
75e705c121SKalle Valo 
76e705c121SKalle Valo #include "iwl-op-mode.h"
77e705c121SKalle Valo #include "iwl-io.h"
78e705c121SKalle Valo #include "mvm.h"
79e705c121SKalle Valo #include "sta.h"
80e705c121SKalle Valo #include "time-event.h"
81e705c121SKalle Valo #include "iwl-eeprom-parse.h"
82e705c121SKalle Valo #include "iwl-phy-db.h"
83e705c121SKalle Valo #include "testmode.h"
84d962f9b1SJohannes Berg #include "fw/error-dump.h"
85e705c121SKalle Valo #include "iwl-prph.h"
86e705c121SKalle Valo #include "iwl-nvm-parse.h"
87e705c121SKalle Valo 
88e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
89e705c121SKalle Valo 	{
90e705c121SKalle Valo 		.max = 1,
91e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
92e705c121SKalle Valo 	},
93e705c121SKalle Valo 	{
94e705c121SKalle Valo 		.max = 1,
95e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
96e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
97e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
98e705c121SKalle Valo 	},
99e705c121SKalle Valo 	{
100e705c121SKalle Valo 		.max = 1,
101e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
102e705c121SKalle Valo 	},
103e705c121SKalle Valo };
104e705c121SKalle Valo 
105e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
106e705c121SKalle Valo 	{
107e705c121SKalle Valo 		.num_different_channels = 2,
108e705c121SKalle Valo 		.max_interfaces = 3,
109e705c121SKalle Valo 		.limits = iwl_mvm_limits,
110e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
111e705c121SKalle Valo 	},
112e705c121SKalle Valo };
113e705c121SKalle Valo 
114e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
115e705c121SKalle Valo /*
116e705c121SKalle Valo  * Use the reserved field to indicate magic values.
117e705c121SKalle Valo  * these values will only be used internally by the driver,
118e705c121SKalle Valo  * and won't make it to the fw (reserved will be 0).
119e705c121SKalle Valo  * BC_FILTER_MAGIC_IP - configure the val of this attribute to
120e705c121SKalle Valo  *	be the vif's ip address. in case there is not a single
121e705c121SKalle Valo  *	ip address (0, or more than 1), this attribute will
122e705c121SKalle Valo  *	be skipped.
123e705c121SKalle Valo  * BC_FILTER_MAGIC_MAC - set the val of this attribute to
124e705c121SKalle Valo  *	the LSB bytes of the vif's mac address
125e705c121SKalle Valo  */
126e705c121SKalle Valo enum {
127e705c121SKalle Valo 	BC_FILTER_MAGIC_NONE = 0,
128e705c121SKalle Valo 	BC_FILTER_MAGIC_IP,
129e705c121SKalle Valo 	BC_FILTER_MAGIC_MAC,
130e705c121SKalle Valo };
131e705c121SKalle Valo 
132e705c121SKalle Valo static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
133e705c121SKalle Valo 	{
134e705c121SKalle Valo 		/* arp */
135e705c121SKalle Valo 		.discard = 0,
136e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
137e705c121SKalle Valo 		.attrs = {
138e705c121SKalle Valo 			{
139e705c121SKalle Valo 				/* frame type - arp, hw type - ethernet */
140e705c121SKalle Valo 				.offset_type =
141e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
142e705c121SKalle Valo 				.offset = sizeof(rfc1042_header),
143e705c121SKalle Valo 				.val = cpu_to_be32(0x08060001),
144e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
145e705c121SKalle Valo 			},
146e705c121SKalle Valo 			{
147e705c121SKalle Valo 				/* arp dest ip */
148e705c121SKalle Valo 				.offset_type =
149e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
150e705c121SKalle Valo 				.offset = sizeof(rfc1042_header) + 2 +
151e705c121SKalle Valo 					  sizeof(struct arphdr) +
152e705c121SKalle Valo 					  ETH_ALEN + sizeof(__be32) +
153e705c121SKalle Valo 					  ETH_ALEN,
154e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
155e705c121SKalle Valo 				/* mark it as special field */
156e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
157e705c121SKalle Valo 			},
158e705c121SKalle Valo 		},
159e705c121SKalle Valo 	},
160e705c121SKalle Valo 	{
161e705c121SKalle Valo 		/* dhcp offer bcast */
162e705c121SKalle Valo 		.discard = 0,
163e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
164e705c121SKalle Valo 		.attrs = {
165e705c121SKalle Valo 			{
166e705c121SKalle Valo 				/* udp dest port - 68 (bootp client)*/
167e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
168e705c121SKalle Valo 				.offset = offsetof(struct udphdr, dest),
169e705c121SKalle Valo 				.val = cpu_to_be32(0x00440000),
170e705c121SKalle Valo 				.mask = cpu_to_be32(0xffff0000),
171e705c121SKalle Valo 			},
172e705c121SKalle Valo 			{
173e705c121SKalle Valo 				/* dhcp - lsb bytes of client hw address */
174e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
175e705c121SKalle Valo 				.offset = 38,
176e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
177e705c121SKalle Valo 				/* mark it as special field */
178e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
179e705c121SKalle Valo 			},
180e705c121SKalle Valo 		},
181e705c121SKalle Valo 	},
182e705c121SKalle Valo 	/* last filter must be empty */
183e705c121SKalle Valo 	{},
184e705c121SKalle Valo };
185e705c121SKalle Valo #endif
186e705c121SKalle Valo 
187fc36ffdaSJohannes Berg static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
188fc36ffdaSJohannes Berg 	.max_peers = IWL_MVM_TOF_MAX_APS,
189fc36ffdaSJohannes Berg 	.report_ap_tsf = 1,
190fc36ffdaSJohannes Berg 	.randomize_mac_addr = 1,
191fc36ffdaSJohannes Berg 
192fc36ffdaSJohannes Berg 	.ftm = {
193fc36ffdaSJohannes Berg 		.supported = 1,
194fc36ffdaSJohannes Berg 		.asap = 1,
195fc36ffdaSJohannes Berg 		.non_asap = 1,
196fc36ffdaSJohannes Berg 		.request_lci = 1,
197fc36ffdaSJohannes Berg 		.request_civicloc = 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) |
206fc36ffdaSJohannes Berg 			     BIT(NL80211_PREAMBLE_VHT),
207fc36ffdaSJohannes Berg 	},
208fc36ffdaSJohannes Berg };
209fc36ffdaSJohannes Berg 
2106569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
211c56e00a3SJohannes Berg 				 enum set_key_cmd cmd,
212c56e00a3SJohannes Berg 				 struct ieee80211_vif *vif,
213c56e00a3SJohannes Berg 				 struct ieee80211_sta *sta,
214c56e00a3SJohannes Berg 				 struct ieee80211_key_conf *key);
215c56e00a3SJohannes Berg 
216e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
217e705c121SKalle Valo {
218e705c121SKalle Valo 	int i;
219e705c121SKalle Valo 
220e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
221e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
222e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
223e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
224e705c121SKalle Valo 	}
225e705c121SKalle Valo }
226e705c121SKalle Valo 
227e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
228e705c121SKalle Valo 						  const char *alpha2,
229e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
230e705c121SKalle Valo 						  bool *changed)
231e705c121SKalle Valo {
232e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
233e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
234e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
235e705c121SKalle Valo 	struct iwl_mcc_update_resp *resp;
236e705c121SKalle Valo 
237e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
238e705c121SKalle Valo 
239e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
240e705c121SKalle Valo 
241e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
242e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
243e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
244e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
245e705c121SKalle Valo 		goto out;
246e705c121SKalle Valo 	}
247e705c121SKalle Valo 
24882715ac7SEmmanuel Grumbach 	if (changed) {
24982715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
25082715ac7SEmmanuel Grumbach 
25182715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
25282715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
25382715ac7SEmmanuel Grumbach 	}
254e705c121SKalle Valo 
255e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
256e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
257e705c121SKalle Valo 				      resp->channels,
25877e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
25977e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info));
260e705c121SKalle Valo 	/* Store the return source id */
261e705c121SKalle Valo 	src_id = resp->source_id;
262e705c121SKalle Valo 	kfree(resp);
263e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
264e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
265e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
266e705c121SKalle Valo 		goto out;
267e705c121SKalle Valo 	}
268e705c121SKalle Valo 
269e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
270e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
271e705c121SKalle Valo 	mvm->lar_regdom_set = true;
272e705c121SKalle Valo 	mvm->mcc_src = src_id;
273e705c121SKalle Valo 
274e705c121SKalle Valo out:
275e705c121SKalle Valo 	return regd;
276e705c121SKalle Valo }
277e705c121SKalle Valo 
278e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
279e705c121SKalle Valo {
280e705c121SKalle Valo 	bool changed;
281e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
282e705c121SKalle Valo 
283e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
284e705c121SKalle Valo 		return;
285e705c121SKalle Valo 
286e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
287e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
288e705c121SKalle Valo 		/* only update the regulatory core if changed */
289e705c121SKalle Valo 		if (changed)
290e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
291e705c121SKalle Valo 
292e705c121SKalle Valo 		kfree(regd);
293e705c121SKalle Valo 	}
294e705c121SKalle Valo }
295e705c121SKalle Valo 
296e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
297e705c121SKalle Valo 							  bool *changed)
298e705c121SKalle Valo {
299e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
300e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
301e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
302e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
303e705c121SKalle Valo }
304e705c121SKalle Valo 
305e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
306e705c121SKalle Valo {
307e705c121SKalle Valo 	enum iwl_mcc_source used_src;
308e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
309e705c121SKalle Valo 	int ret;
310e705c121SKalle Valo 	bool changed;
311e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
312e705c121SKalle Valo 			rtnl_dereference(mvm->hw->wiphy->regd);
313e705c121SKalle Valo 
314e705c121SKalle Valo 	if (!r)
315e705c121SKalle Valo 		return -ENOENT;
316e705c121SKalle Valo 
317e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
318e705c121SKalle Valo 	used_src = mvm->mcc_src;
319e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
320e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
321e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
322e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
323e705c121SKalle Valo 			kfree(regd);
324e705c121SKalle Valo 	}
325e705c121SKalle Valo 
326e705c121SKalle Valo 	/* Now set our last stored MCC and source */
327e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
328e705c121SKalle Valo 				     &changed);
329e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
330e705c121SKalle Valo 		return -EIO;
331e705c121SKalle Valo 
332e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
333e705c121SKalle Valo 	if (changed)
334e705c121SKalle Valo 		ret = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
335e705c121SKalle Valo 	else
336e705c121SKalle Valo 		ret = 0;
337e705c121SKalle Valo 
338e705c121SKalle Valo 	kfree(regd);
339e705c121SKalle Valo 	return ret;
340e705c121SKalle Valo }
341e705c121SKalle Valo 
3427360f99eSEmmanuel Grumbach const static u8 he_if_types_ext_capa_sta[] = {
3437360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
344918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
3457360f99eSEmmanuel Grumbach 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
3467360f99eSEmmanuel Grumbach 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
3477360f99eSEmmanuel Grumbach };
3487360f99eSEmmanuel Grumbach 
3497360f99eSEmmanuel Grumbach const static struct wiphy_iftype_ext_capab he_iftypes_ext_capa[] = {
3507360f99eSEmmanuel Grumbach 	{
3517360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
3527360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
3537360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
3547360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
3557360f99eSEmmanuel Grumbach 	},
3567360f99eSEmmanuel Grumbach };
3577360f99eSEmmanuel Grumbach 
358e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
359e705c121SKalle Valo {
360e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
361e705c121SKalle Valo 	int num_mac, ret, i;
362e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
363e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
364e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
365e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
366e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
367e705c121SKalle Valo 	};
3683f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
3693f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
3703f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3713f37c229SIdo Yariv #endif
372e705c121SKalle Valo 
373e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
374e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
375e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
376e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
377e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
378e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
379e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
380e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
381e705c121SKalle Valo 	ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
382e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
383e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
384e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
385e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
386909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
38730433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
388d270e7b8SIlan Peer 	ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
389520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
390cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
391cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
392cfb21b11SJohannes Berg 	/*
393cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
394cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
395cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
396cfb21b11SJohannes Berg 	 * assert EDC.
397cfb21b11SJohannes Berg 	 *
398cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
399cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
400cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
401cfb21b11SJohannes Berg 	 * devices.
402cfb21b11SJohannes Berg 	 */
4037d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
404438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
405438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
406ecaf71deSGregory Greenman 
4074243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
408ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
409ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
410ecaf71deSGregory Greenman 	}
411ecaf71deSGregory Greenman 
412b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
413b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
414960f864bSJohannes Berg 
415960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
416960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
41765e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
418960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
419960f864bSJohannes Berg 		/*
420960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
421960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
422960f864bSJohannes Berg 		 * for station powersave
423960f864bSJohannes Berg 		 */
424960f864bSJohannes Berg 		return -EINVAL;
425960f864bSJohannes Berg 	}
426e705c121SKalle Valo 
42780938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
42880938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
42980938abcSJohannes Berg 
4302d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
4312d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
4322d7cf549SJohannes Berg 
433cf961e16SLiad Kaufman 	hw->queues = IEEE80211_MAX_QUEUES;
434e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
435e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
436e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
437e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
438e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
439371a17edSJohannes Berg 
440371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
441371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
442371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
443371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
444371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
445371a17edSJohannes Berg 
4464243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
4479f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
4489f66a397SGregory Greenman 
449e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
450e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
451438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
452e705c121SKalle Valo 
4538e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
454e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
455e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
456e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
457e705c121SKalle Valo 
4582a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4592a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4602a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
4612a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4622a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4632a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
4642a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4652a53d166SAyala Beker 	}
4662a53d166SAyala Beker 
4674b87e5afSLuca Coelho 	/* Enable 11w if software crypto is not enabled (as the
4684b87e5afSLuca Coelho 	 * firmware will interpret some mgmt packets, so enabling it
4694b87e5afSLuca Coelho 	 * with software crypto isn't safe).
470e705c121SKalle Valo 	 */
4713b37f4c9SJohannes Berg 	if (!iwlwifi_mod_params.swcrypto) {
472e705c121SKalle Valo 		ieee80211_hw_set(hw, MFP_CAPABLE);
473e705c121SKalle Valo 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
474e705c121SKalle Valo 			WLAN_CIPHER_SUITE_AES_CMAC;
475e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
4768e160ab8SAyala Beker 		if (iwl_mvm_has_new_rx_api(mvm)) {
4778e160ab8SAyala Beker 			mvm->ciphers[hw->wiphy->n_cipher_suites] =
4788e160ab8SAyala Beker 				WLAN_CIPHER_SUITE_BIP_GMAC_128;
4798e160ab8SAyala Beker 			hw->wiphy->n_cipher_suites++;
4808e160ab8SAyala Beker 			mvm->ciphers[hw->wiphy->n_cipher_suites] =
4818e160ab8SAyala Beker 				WLAN_CIPHER_SUITE_BIP_GMAC_256;
4828e160ab8SAyala Beker 			hw->wiphy->n_cipher_suites++;
4838e160ab8SAyala Beker 		}
484e705c121SKalle Valo 	}
485e705c121SKalle Valo 
486e705c121SKalle Valo 	/* currently FW API supports only one optional cipher scheme */
487e705c121SKalle Valo 	if (mvm->fw->cs[0].cipher) {
48824ddddf3SJohannes Berg 		const struct iwl_fw_cipher_scheme *fwcs = &mvm->fw->cs[0];
48924ddddf3SJohannes Berg 		struct ieee80211_cipher_scheme *cs = &mvm->cs[0];
49024ddddf3SJohannes Berg 
491e705c121SKalle Valo 		mvm->hw->n_cipher_schemes = 1;
49224ddddf3SJohannes Berg 
49324ddddf3SJohannes Berg 		cs->cipher = le32_to_cpu(fwcs->cipher);
49424ddddf3SJohannes Berg 		cs->iftype = BIT(NL80211_IFTYPE_STATION);
49524ddddf3SJohannes Berg 		cs->hdr_len = fwcs->hdr_len;
49624ddddf3SJohannes Berg 		cs->pn_len = fwcs->pn_len;
49724ddddf3SJohannes Berg 		cs->pn_off = fwcs->pn_off;
49824ddddf3SJohannes Berg 		cs->key_idx_off = fwcs->key_idx_off;
49924ddddf3SJohannes Berg 		cs->key_idx_mask = fwcs->key_idx_mask;
50024ddddf3SJohannes Berg 		cs->key_idx_shift = fwcs->key_idx_shift;
50124ddddf3SJohannes Berg 		cs->mic_len = fwcs->mic_len;
50224ddddf3SJohannes Berg 
50324ddddf3SJohannes Berg 		mvm->hw->cipher_schemes = mvm->cs;
50424ddddf3SJohannes Berg 		mvm->ciphers[hw->wiphy->n_cipher_suites] = cs->cipher;
505e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
506e705c121SKalle Valo 	}
507e705c121SKalle Valo 
508b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
509fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
510b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
511b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
512fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
513fc36ffdaSJohannes Berg 	}
514b73f9a4aSJohannes Berg 
515e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
516e705c121SKalle Valo 	hw->wiphy->features |=
517e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
518e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
519e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
520e705c121SKalle Valo 
521e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
522e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
523e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
524cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
525e705c121SKalle Valo 
526e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
527e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
528e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
529e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
530e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
531e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
532e705c121SKalle Valo 
533e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
534e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
535e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
536e47df5bdSJohannes Berg 
537e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
538e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
539e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
540e705c121SKalle Valo 	else
541e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
542e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
543e705c121SKalle Valo 
544e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
545e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
546e705c121SKalle Valo 
547e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
548e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
549e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
550e705c121SKalle Valo 
551e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
552e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
553e705c121SKalle Valo 
554e705c121SKalle Valo 	/* Extract MAC address */
555e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
556e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
557e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
558e705c121SKalle Valo 
559e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
560e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
561e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
562e705c121SKalle Valo 
563e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
564e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
565e705c121SKalle Valo 		       ETH_ALEN);
566e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
567e705c121SKalle Valo 		hw->wiphy->n_addresses++;
568e705c121SKalle Valo 	}
569e705c121SKalle Valo 
570e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
571e705c121SKalle Valo 
572e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
573e705c121SKalle Valo 
574e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
575e705c121SKalle Valo 
576e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
577e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
578e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
579e705c121SKalle Valo 
580e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
581e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
582e705c121SKalle Valo 	else
583e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
584e705c121SKalle Valo 
58557fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
58657fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
58757fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
58857fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
58957fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
59057fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
591e705c121SKalle Valo 
592e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
593e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
594e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
595e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
59657fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
597e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
598e705c121SKalle Valo 	}
599e705c121SKalle Valo 
600e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
601e705c121SKalle Valo 
602e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
603e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
604e705c121SKalle Valo 	else
605e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
606e705c121SKalle Valo 
607ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
608e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
609e705c121SKalle Valo 	hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
610e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
611e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
612e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
613e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
614e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
615e705c121SKalle Valo 
616e705c121SKalle Valo 	/*
617e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
618e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
619e705c121SKalle Valo 	 */
620e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
621e705c121SKalle Valo 
622e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
623e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
624e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
625a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
626e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
627e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
628e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
629e705c121SKalle Valo 
630e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
631e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
632e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
633e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
634e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
635e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
636e705c121SKalle Valo 
637e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
638e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
639e705c121SKalle Valo 		hw->wiphy->features |=
640e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
641e705c121SKalle Valo 
642e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
643e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
644e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
645e705c121SKalle Valo 
646aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
647aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
648aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
649aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
650aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
651aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
652aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
653aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
654aacf8f18SAvrahams Stern 	}
655aacf8f18SAvrahams Stern 
6568f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
6578f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6588f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
6598f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6608f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
6618f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6628f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
6638f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6648f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
6658f691af9SZamir, Roee 	}
6668f691af9SZamir, Roee 
6677360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
6687360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
6697360f99eSEmmanuel Grumbach 		hw->wiphy->iftype_ext_capab = he_iftypes_ext_capa;
6707360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
6717360f99eSEmmanuel Grumbach 			ARRAY_SIZE(he_iftypes_ext_capa);
672918cbf39SSara Sharon 
673918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
674918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
6757360f99eSEmmanuel Grumbach 	}
6767360f99eSEmmanuel Grumbach 
677e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
678e705c121SKalle Valo 
679e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6803f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
681e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
682e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
683e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
684e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
685e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
686e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
687e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
688e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
6893b37f4c9SJohannes Berg 		if (!iwlwifi_mod_params.swcrypto)
690e705c121SKalle Valo 			mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
691e705c121SKalle Valo 					     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
692e705c121SKalle Valo 					     WIPHY_WOWLAN_4WAY_HANDSHAKE;
693e705c121SKalle Valo 
694e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
695e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
696e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
697e705c121SKalle Valo 		mvm->wowlan.max_nd_match_sets = IWL_SCAN_MAX_PROFILES;
698e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
699e705c121SKalle Valo 	}
700e705c121SKalle Valo #endif
701e705c121SKalle Valo 
702e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
703e705c121SKalle Valo 	/* assign default bcast filtering configuration */
704e705c121SKalle Valo 	mvm->bcast_filters = iwl_mvm_default_bcast_filters;
705e705c121SKalle Valo #endif
706e705c121SKalle Valo 
707e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
708e705c121SKalle Valo 	if (ret)
709e705c121SKalle Valo 		return ret;
710e705c121SKalle Valo 
711e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
712e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
713e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
714e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
715e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
716e705c121SKalle Valo 	}
717e705c121SKalle Valo 
718e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
719e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
720e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
721e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
722e705c121SKalle Valo 	}
723e705c121SKalle Valo 
724e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
7255e6a98dcSSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm)) {
7265e6a98dcSSara Sharon 		hw->netdev_features &= ~(IWL_TX_CSUM_NETIF_FLAGS |
7275e6a98dcSSara Sharon 					 NETIF_F_RXCSUM);
7285e6a98dcSSara Sharon 		/* We may support SW TX CSUM */
72941837ca9SEmmanuel Grumbach 		if (IWL_MVM_SW_TX_CSUM_OFFLOAD)
7305e6a98dcSSara Sharon 			hw->netdev_features |= IWL_TX_CSUM_NETIF_FLAGS;
7315e6a98dcSSara Sharon 	}
73241837ca9SEmmanuel Grumbach 
73391b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
73491b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
73591b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
73691b08c2dSAviya Erenfeld 
737de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
738de645e89SJohannes Berg 	if (ret) {
739de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
740e705c121SKalle Valo 	}
741e705c121SKalle Valo 
742e705c121SKalle Valo 	return ret;
743e705c121SKalle Valo }
744e705c121SKalle Valo 
745e705c121SKalle Valo static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
746e705c121SKalle Valo 			   struct ieee80211_tx_control *control,
747e705c121SKalle Valo 			   struct sk_buff *skb)
748e705c121SKalle Valo {
749e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
750e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
751e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
752e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
753cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
754cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
755e705c121SKalle Valo 
756e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
757e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
758e705c121SKalle Valo 		goto drop;
759e705c121SKalle Valo 	}
760e705c121SKalle Valo 
761cfbc6c4cSSara Sharon 	if (offchannel &&
762e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
763e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
764e705c121SKalle Valo 		goto drop;
765e705c121SKalle Valo 
766eb045e6eSDavid Spinadel 	/* treat non-bufferable MMPDUs on AP interfaces as broadcast */
767eb045e6eSDavid Spinadel 	if ((info->control.vif->type == NL80211_IFTYPE_AP ||
768eb045e6eSDavid Spinadel 	     info->control.vif->type == NL80211_IFTYPE_ADHOC) &&
769e705c121SKalle Valo 	    ieee80211_is_mgmt(hdr->frame_control) &&
770eb045e6eSDavid Spinadel 	    !ieee80211_is_bufferable_mmpdu(hdr->frame_control))
771e705c121SKalle Valo 		sta = NULL;
772e705c121SKalle Valo 
773dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
774cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
775cfbc6c4cSSara Sharon 	    !offchannel) {
776dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
777dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
778dc1aca22SAndrei Otcheretianski 		u8 ap_sta_id = READ_ONCE(mvmvif->ap_sta_id);
779dc1aca22SAndrei Otcheretianski 
780dc1aca22SAndrei Otcheretianski 		if (ap_sta_id < IWL_MVM_STATION_COUNT) {
781dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
782dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
783dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
784dc1aca22SAndrei Otcheretianski 				goto drop;
785dc1aca22SAndrei Otcheretianski 		}
786dc1aca22SAndrei Otcheretianski 	}
787dc1aca22SAndrei Otcheretianski 
788e705c121SKalle Valo 	if (sta) {
789e705c121SKalle Valo 		if (iwl_mvm_tx_skb(mvm, skb, sta))
790e705c121SKalle Valo 			goto drop;
791e705c121SKalle Valo 		return;
792e705c121SKalle Valo 	}
793e705c121SKalle Valo 
794e705c121SKalle Valo 	if (iwl_mvm_tx_skb_non_sta(mvm, skb))
795e705c121SKalle Valo 		goto drop;
796e705c121SKalle Valo 	return;
797e705c121SKalle Valo  drop:
798e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
799e705c121SKalle Valo }
800e705c121SKalle Valo 
801cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
802e705c121SKalle Valo {
803cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
804cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
805cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
806cfbc6c4cSSara Sharon 
807fba8248eSSara Sharon 	/*
808fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
809fba8248eSSara Sharon 	 * taker all the work.
810fba8248eSSara Sharon 	 *
811fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
812fba8248eSSara Sharon 	 *
813fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
814fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
815fba8248eSSara Sharon 	 *
816fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
817fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
818fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
819fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
820fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
821fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
822fba8248eSSara Sharon 	 *
823fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
824fba8248eSSara Sharon 	 * check the queue, so do nothing.
825fba8248eSSara Sharon 	 */
826fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
827fba8248eSSara Sharon 		return;
828cfbc6c4cSSara Sharon 
829cfbc6c4cSSara Sharon 	rcu_read_lock();
830fba8248eSSara Sharon 	do {
831cfbc6c4cSSara Sharon 		while (likely(!mvmtxq->stopped &&
832cfbc6c4cSSara Sharon 			      (mvm->trans->system_pm_mode ==
833cfbc6c4cSSara Sharon 			       IWL_PLAT_PM_MODE_DISABLED))) {
834cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
835cfbc6c4cSSara Sharon 
836f50d693bSSara Sharon 			if (!skb) {
837f50d693bSSara Sharon 				if (txq->sta)
838f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
839f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
840f50d693bSSara Sharon 						     txq->sta->addr,
841f50d693bSSara Sharon 						     txq->tid);
842cfbc6c4cSSara Sharon 				break;
843f50d693bSSara Sharon 			}
844cfbc6c4cSSara Sharon 
845cfbc6c4cSSara Sharon 			if (!txq->sta)
846cfbc6c4cSSara Sharon 				iwl_mvm_tx_skb_non_sta(mvm, skb);
847cfbc6c4cSSara Sharon 			else
848cfbc6c4cSSara Sharon 				iwl_mvm_tx_skb(mvm, skb, txq->sta);
849cfbc6c4cSSara Sharon 		}
850fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
851cfbc6c4cSSara Sharon 	rcu_read_unlock();
852e705c121SKalle Valo }
853e705c121SKalle Valo 
854cfbc6c4cSSara Sharon static void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
855cfbc6c4cSSara Sharon 				      struct ieee80211_txq *txq)
856e705c121SKalle Valo {
857cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
858cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
859e705c121SKalle Valo 
860cfbc6c4cSSara Sharon 	/*
861cfbc6c4cSSara Sharon 	 * Please note that racing is handled very carefully here:
862cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id is updated during allocation, and mvmtxq->list is
863cfbc6c4cSSara Sharon 	 * deleted afterwards.
864cfbc6c4cSSara Sharon 	 * This means that if:
865cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && list_empty(&mvmtxq->list):
866cfbc6c4cSSara Sharon 	 *	queue is allocated and we can TX.
867cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && !list_empty(&mvmtxq->list):
868cfbc6c4cSSara Sharon 	 *	a race, should defer the frame.
869cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && list_empty(&mvmtxq->list):
870cfbc6c4cSSara Sharon 	 *	need to allocate the queue and defer the frame.
871cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && !list_empty(&mvmtxq->list):
872cfbc6c4cSSara Sharon 	 *	queue is already scheduled for allocation, no need to allocate,
873cfbc6c4cSSara Sharon 	 *	should defer the frame.
874cfbc6c4cSSara Sharon 	 */
875cfbc6c4cSSara Sharon 
876cfbc6c4cSSara Sharon 	/* If the queue is allocated TX and return. */
877cfbc6c4cSSara Sharon 	if (!txq->sta || mvmtxq->txq_id != IWL_MVM_INVALID_QUEUE) {
878cfbc6c4cSSara Sharon 		/*
879cfbc6c4cSSara Sharon 		 * Check that list is empty to avoid a race where txq_id is
880cfbc6c4cSSara Sharon 		 * already updated, but the queue allocation work wasn't
881cfbc6c4cSSara Sharon 		 * finished
882cfbc6c4cSSara Sharon 		 */
883cfbc6c4cSSara Sharon 		if (unlikely(txq->sta && !list_empty(&mvmtxq->list)))
884cfbc6c4cSSara Sharon 			return;
885cfbc6c4cSSara Sharon 
886cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
887cfbc6c4cSSara Sharon 		return;
888cfbc6c4cSSara Sharon 	}
889cfbc6c4cSSara Sharon 
890cfbc6c4cSSara Sharon 	/* The list is being deleted only after the queue is fully allocated. */
891cfbc6c4cSSara Sharon 	if (!list_empty(&mvmtxq->list))
892cfbc6c4cSSara Sharon 		return;
893cfbc6c4cSSara Sharon 
894cfbc6c4cSSara Sharon 	list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
895cfbc6c4cSSara Sharon 	schedule_work(&mvm->add_stream_wk);
896e705c121SKalle Valo }
897e705c121SKalle Valo 
898e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
899e705c121SKalle Valo 	do {								\
900e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
901e705c121SKalle Valo 			break;						\
9027174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
903e705c121SKalle Valo 	} while (0)
904e705c121SKalle Valo 
905e705c121SKalle Valo static void
906e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
907e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
908e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
909e705c121SKalle Valo {
910e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
911e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
912e705c121SKalle Valo 
9136c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
9146c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
9156c042d75SSara Sharon 	if (!trig)
916e705c121SKalle Valo 		return;
917e705c121SKalle Valo 
918e705c121SKalle Valo 	ba_trig = (void *)trig->data;
919e705c121SKalle Valo 
920e705c121SKalle Valo 	switch (action) {
921e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
922e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
923e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
924e705c121SKalle Valo 
925e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
926e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
927e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
928e705c121SKalle Valo 		break;
929e705c121SKalle Valo 		}
930e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
931e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
932e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
933e705c121SKalle Valo 				 sta->addr, tid);
934e705c121SKalle Valo 		break;
935e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
936e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
937e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
938e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
939e705c121SKalle Valo 		break;
940e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
941e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
942e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
943e705c121SKalle Valo 				 sta->addr, tid);
944e705c121SKalle Valo 		break;
945e705c121SKalle Valo 	default:
946e705c121SKalle Valo 		break;
947e705c121SKalle Valo 	}
948e705c121SKalle Valo }
949e705c121SKalle Valo 
950e705c121SKalle Valo static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
951e705c121SKalle Valo 				    struct ieee80211_vif *vif,
95250ea05efSSara Sharon 				    struct ieee80211_ampdu_params *params)
953e705c121SKalle Valo {
954e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
955e705c121SKalle Valo 	int ret;
95650ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
95750ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
95850ea05efSSara Sharon 	u16 tid = params->tid;
95950ea05efSSara Sharon 	u16 *ssn = &params->ssn;
960514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
961bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
96210b2b201SSara Sharon 	u16 timeout = params->timeout;
963e705c121SKalle Valo 
964e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
965e705c121SKalle Valo 		     sta->addr, tid, action);
966e705c121SKalle Valo 
967e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
968e705c121SKalle Valo 		return -EACCES;
969e705c121SKalle Valo 
970e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
971e705c121SKalle Valo 
972e705c121SKalle Valo 	switch (action) {
973e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
974b0ffe455SJohannes Berg 		if (iwl_mvm_vif_from_mac80211(vif)->ap_sta_id ==
975b0ffe455SJohannes Berg 				iwl_mvm_sta_from_mac80211(sta)->sta_id) {
976b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
977b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
978b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
979b0ffe455SJohannes Berg 
980b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
981b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
982b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
983b0ffe455SJohannes Berg 		}
984e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
985e705c121SKalle Valo 			ret = -EINVAL;
986e705c121SKalle Valo 			break;
987e705c121SKalle Valo 		}
98810b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
98910b2b201SSara Sharon 					 timeout);
990e705c121SKalle Valo 		break;
991e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
99210b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
99310b2b201SSara Sharon 					 timeout);
994e705c121SKalle Valo 		break;
995e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
996e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
997e705c121SKalle Valo 			ret = -EINVAL;
998e705c121SKalle Valo 			break;
999e705c121SKalle Valo 		}
1000e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1001e705c121SKalle Valo 		break;
1002e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
1003e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1004e705c121SKalle Valo 		break;
1005e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1006e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1007e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1008e705c121SKalle Valo 		break;
1009e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1010bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1011bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
1012e705c121SKalle Valo 		break;
1013e705c121SKalle Valo 	default:
1014e705c121SKalle Valo 		WARN_ON_ONCE(1);
1015e705c121SKalle Valo 		ret = -EINVAL;
1016e705c121SKalle Valo 		break;
1017e705c121SKalle Valo 	}
1018e705c121SKalle Valo 
1019e705c121SKalle Valo 	if (!ret) {
1020e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
1021e705c121SKalle Valo 
1022e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1023e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1024e705c121SKalle Valo 
1025e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1026e705c121SKalle Valo 					    rx_ba_ssn, action);
1027e705c121SKalle Valo 	}
1028e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1029e705c121SKalle Valo 
1030e705c121SKalle Valo 	return ret;
1031e705c121SKalle Valo }
1032e705c121SKalle Valo 
1033e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1034e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1035e705c121SKalle Valo {
1036e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1037e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1038e705c121SKalle Valo 
1039e705c121SKalle Valo 	mvmvif->uploaded = false;
10400ae98812SSara Sharon 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1041e705c121SKalle Valo 
1042e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1043e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1044e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1045e705c121SKalle Valo 
1046e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1047e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
104886e177d8SGregory Greenman 	memset(&mvmvif->probe_resp_data, 0, sizeof(mvmvif->probe_resp_data));
1049e705c121SKalle Valo }
1050e705c121SKalle Valo 
1051e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1052e705c121SKalle Valo {
1053fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1054e705c121SKalle Valo 
10559bf13beeSJohannes Berg 	mvm->cur_aid = 0;
10569bf13beeSJohannes Berg 
1057e705c121SKalle Valo 	mvm->scan_status = 0;
1058e705c121SKalle Valo 	mvm->ps_disabled = false;
1059b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1060e705c121SKalle Valo 
1061e705c121SKalle Valo 	/* just in case one was running */
1062305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1063e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1064e705c121SKalle Valo 
1065fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1066fc36ffdaSJohannes Berg 
1067e705c121SKalle Valo 	/*
1068e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1069e705c121SKalle Valo 	 * gone down during the HW restart
1070e705c121SKalle Valo 	 */
1071e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1072e705c121SKalle Valo 
1073e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1074e705c121SKalle Valo 
1075e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
10769c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1077e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1078e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1079e705c121SKalle Valo 
1080e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1081e705c121SKalle Valo 
1082e705c121SKalle Valo 	mvm->vif_count = 0;
1083e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
10847174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1085baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1086e705c121SKalle Valo 
1087e705c121SKalle Valo 	/* keep statistics ticking */
1088e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1089e705c121SKalle Valo }
1090e705c121SKalle Valo 
1091e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1092e705c121SKalle Valo {
1093e705c121SKalle Valo 	int ret;
1094e705c121SKalle Valo 
1095e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1096e705c121SKalle Valo 
1097bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1098bf8b286fSJohannes Berg 		/*
1099bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1100bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1101bf8b286fSJohannes Berg 		 */
1102bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1103bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1104e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1105e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1106a42b2af3SLuca Coelho 	}
1107e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1108e705c121SKalle Valo 
1109b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1110b108d8c7SShahar S Matityahu 			       NULL);
1111b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1112b108d8c7SShahar S Matityahu 			       NULL);
1113da2eb669SSara Sharon 
1114e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1115e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1116e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1117e705c121SKalle Valo 		 * would do.
1118e705c121SKalle Valo 		 */
1119e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1120e705c121SKalle Valo 	}
1121e705c121SKalle Valo 
1122e705c121SKalle Valo 	return ret;
1123e705c121SKalle Valo }
1124e705c121SKalle Valo 
1125e705c121SKalle Valo static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1126e705c121SKalle Valo {
1127e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1128e705c121SKalle Valo 	int ret;
1129e705c121SKalle Valo 
1130e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1131e705c121SKalle Valo 	ret = __iwl_mvm_mac_start(mvm);
1132e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1133e705c121SKalle Valo 
1134e705c121SKalle Valo 	return ret;
1135e705c121SKalle Valo }
1136e705c121SKalle Valo 
1137e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1138e705c121SKalle Valo {
1139e705c121SKalle Valo 	int ret;
1140e705c121SKalle Valo 
1141e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1142e705c121SKalle Valo 
1143e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
11444d4183c4SEmmanuel Grumbach 
1145e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1146e705c121SKalle Valo 	if (ret)
1147e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1148e705c121SKalle Valo 			ret);
1149e705c121SKalle Valo 
1150f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1151f130bb75SMordechay Goodstein 
1152e705c121SKalle Valo 	/*
1153e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1154e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1155e705c121SKalle Valo 	 */
1156e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1157e705c121SKalle Valo 
1158e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1159e705c121SKalle Valo }
1160e705c121SKalle Valo 
1161e705c121SKalle Valo static void
1162e705c121SKalle Valo iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1163e705c121SKalle Valo 			      enum ieee80211_reconfig_type reconfig_type)
1164e705c121SKalle Valo {
1165e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1166e705c121SKalle Valo 
1167e705c121SKalle Valo 	switch (reconfig_type) {
1168e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1169e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1170e705c121SKalle Valo 		break;
1171e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1172e705c121SKalle Valo 		break;
1173e705c121SKalle Valo 	}
1174e705c121SKalle Valo }
1175e705c121SKalle Valo 
1176e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1177e705c121SKalle Valo {
1178e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1179e705c121SKalle Valo 
1180e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1181e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1182e705c121SKalle Valo 	 */
1183e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1184e705c121SKalle Valo 
1185e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1186e705c121SKalle Valo 
1187e705c121SKalle Valo 	/*
1188e705c121SKalle Valo 	 * The work item could be running or queued if the
1189e705c121SKalle Valo 	 * ROC time event stops just as we get here.
1190e705c121SKalle Valo 	 */
1191e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
1192e705c121SKalle Valo 
1193fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1194e705c121SKalle Valo 
1195e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1196e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1197e705c121SKalle Valo 
1198e705c121SKalle Valo 	/* the fw is stopped, the aux sta is dead: clean up driver state */
1199e705c121SKalle Valo 	iwl_mvm_del_aux_sta(mvm);
1200e705c121SKalle Valo 
1201e705c121SKalle Valo 	/*
1202155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1203155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1204155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1205e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1206e705c121SKalle Valo 	 * HW restart was requested.
1207e705c121SKalle Valo 	 */
1208155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1209155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1210155f7e04SEmmanuel Grumbach 			       &mvm->status))
1211e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1212e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1213e705c121SKalle Valo 
1214e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1215e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1216e705c121SKalle Valo 	 */
1217e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1218e705c121SKalle Valo 		int i;
1219e705c121SKalle Valo 
1220e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1221e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1222e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1223e705c121SKalle Valo 				      i))
1224e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1225e705c121SKalle Valo 		}
1226e705c121SKalle Valo 	}
1227e705c121SKalle Valo }
1228e705c121SKalle Valo 
1229e705c121SKalle Valo static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1230e705c121SKalle Valo {
1231e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1232e705c121SKalle Valo 
1233e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
123424afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1235771147b0SJohannes Berg 
1236771147b0SJohannes Berg 	/*
1237771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1238771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1239771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
12407174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1241771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1242771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1243771147b0SJohannes Berg 	 */
1244771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1245771147b0SJohannes Berg 
1246d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
124769e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
12487174beb6SJohannes Berg 	iwl_fw_free_dump_desc(&mvm->fwrt);
1249e705c121SKalle Valo 
1250e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1251e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1252e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1253e705c121SKalle Valo 
1254e705c121SKalle Valo 	/*
1255e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1256e705c121SKalle Valo 	 * discover that its list is now empty.
1257e705c121SKalle Valo 	 */
1258e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1259e705c121SKalle Valo }
1260e705c121SKalle Valo 
1261e705c121SKalle Valo static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1262e705c121SKalle Valo {
1263e705c121SKalle Valo 	u16 i;
1264e705c121SKalle Valo 
1265e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1266e705c121SKalle Valo 
1267e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1268e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1269e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1270e705c121SKalle Valo 
1271e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1272e705c121SKalle Valo 	return NULL;
1273e705c121SKalle Valo }
1274e705c121SKalle Valo 
1275e705c121SKalle Valo static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1276e705c121SKalle Valo 				s16 tx_power)
1277e705c121SKalle Valo {
12780791c2fcSHaim Dreyfuss 	int len;
12790791c2fcSHaim Dreyfuss 	union {
12800791c2fcSHaim Dreyfuss 		struct iwl_dev_tx_power_cmd v5;
12810791c2fcSHaim Dreyfuss 		struct iwl_dev_tx_power_cmd_v4 v4;
12820791c2fcSHaim Dreyfuss 	} cmd = {
12830791c2fcSHaim Dreyfuss 		.v5.v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
12840791c2fcSHaim Dreyfuss 		.v5.v3.mac_context_id =
1285e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
12860791c2fcSHaim Dreyfuss 		.v5.v3.pwr_restriction = cpu_to_le16(8 * tx_power),
1287e705c121SKalle Valo 	};
1288e705c121SKalle Valo 
1289e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
12900791c2fcSHaim Dreyfuss 		cmd.v5.v3.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1291e705c121SKalle Valo 
12920791c2fcSHaim Dreyfuss 	if (fw_has_api(&mvm->fw->ucode_capa,
12930791c2fcSHaim Dreyfuss 		       IWL_UCODE_TLV_API_REDUCE_TX_POWER))
12940791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
12950791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
12960791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
12970791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
12980791c2fcSHaim Dreyfuss 	else
12990791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4.v3);
1300e705c121SKalle Valo 
1301e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
1302e705c121SKalle Valo }
1303e705c121SKalle Valo 
1304f6780614SSara Sharon static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1305f6780614SSara Sharon 				       struct ieee80211_vif *vif)
1306f6780614SSara Sharon {
1307f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1308f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1309f6780614SSara Sharon 	int ret;
1310f6780614SSara Sharon 
1311f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1312f6780614SSara Sharon 
1313f6780614SSara Sharon 	if (mvmvif->csa_failed) {
1314f6780614SSara Sharon 		mvmvif->csa_failed = false;
1315f6780614SSara Sharon 		ret = -EIO;
1316f6780614SSara Sharon 		goto out_unlock;
1317f6780614SSara Sharon 	}
1318f6780614SSara Sharon 
1319f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1320f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
1321f6780614SSara Sharon 
1322f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
1323f6780614SSara Sharon 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
1324f6780614SSara Sharon 							  mvmvif->ap_sta_id);
1325f6780614SSara Sharon 
1326f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1327f6780614SSara Sharon 			ret = -EIO;
1328f6780614SSara Sharon 			goto out_unlock;
1329f6780614SSara Sharon 		}
1330f6780614SSara Sharon 
13310202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
13320202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
1333f6780614SSara Sharon 			iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1334f6780614SSara Sharon 
1335f6780614SSara Sharon 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1336f6780614SSara Sharon 
13370202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
13380202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1339f6780614SSara Sharon 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1340f6780614SSara Sharon 			if (ret)
1341f6780614SSara Sharon 				goto out_unlock;
1342f6780614SSara Sharon 
1343f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1344f6780614SSara Sharon 		}
13450202bcf0SEmmanuel Grumbach 	}
1346f6780614SSara Sharon 
1347f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1348f6780614SSara Sharon 
1349f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1350f6780614SSara Sharon 
1351f6780614SSara Sharon out_unlock:
1352f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1353f6780614SSara Sharon 
1354f6780614SSara Sharon 	return ret;
1355f6780614SSara Sharon }
1356f6780614SSara Sharon 
1357f6780614SSara Sharon static void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1358f6780614SSara Sharon 					 struct ieee80211_vif *vif)
1359f6780614SSara Sharon {
1360f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1361f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1362f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1363f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1364f6780614SSara Sharon 							  mvmvif->color)),
1365f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1366f6780614SSara Sharon 	};
1367f6780614SSara Sharon 
1368f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1369f6780614SSara Sharon 
1370f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1371f6780614SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1372f6780614SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
1373f6780614SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
1374f6780614SSara Sharon 				     0, sizeof(cmd), &cmd));
1375f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1376f6780614SSara Sharon 
1377f6780614SSara Sharon 	WARN_ON(iwl_mvm_post_channel_switch(hw, vif));
1378f6780614SSara Sharon }
1379f6780614SSara Sharon 
1380f6780614SSara Sharon static void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1381f6780614SSara Sharon {
1382f6780614SSara Sharon 	struct iwl_mvm *mvm;
1383f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1384f6780614SSara Sharon 	struct ieee80211_vif *vif;
1385f6780614SSara Sharon 
1386f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1387f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1388f6780614SSara Sharon 	mvm = mvmvif->mvm;
1389f6780614SSara Sharon 
1390f6780614SSara Sharon 	iwl_mvm_abort_channel_switch(mvm->hw, vif);
1391f6780614SSara Sharon 	ieee80211_chswitch_done(vif, false);
1392f6780614SSara Sharon }
1393f6780614SSara Sharon 
1394e705c121SKalle Valo static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1395e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1396e705c121SKalle Valo {
1397e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1398e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1399e705c121SKalle Valo 	int ret;
1400e705c121SKalle Valo 
1401e705c121SKalle Valo 	mvmvif->mvm = mvm;
140286e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
1403e705c121SKalle Valo 
1404e705c121SKalle Valo 	/*
1405e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1406e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1407e705c121SKalle Valo 	 * don't really have to check the types.
1408e705c121SKalle Valo 	 */
1409e705c121SKalle Valo 
1410e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1411e705c121SKalle Valo 
1412e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1413e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1414e705c121SKalle Valo 		mvmvif->beacon_stats.accu_num_beacons +=
1415e705c121SKalle Valo 			mvmvif->beacon_stats.num_beacons;
1416e705c121SKalle Valo 
1417e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
1418e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1419e705c121SKalle Valo 	if (ret)
1420e705c121SKalle Valo 		goto out_unlock;
1421e705c121SKalle Valo 
1422698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1423698478c4SSara Sharon 
1424e705c121SKalle Valo 	/* Counting number of interfaces is needed for legacy PM */
1425e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1426e705c121SKalle Valo 		mvm->vif_count++;
1427e705c121SKalle Valo 
1428e705c121SKalle Valo 	/*
1429e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1430e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1431e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1432e705c121SKalle Valo 	 * only after the binding.
1433e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1434e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1435e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1436e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1437e705c121SKalle Valo 	 * allocating resources :)
1438e705c121SKalle Valo 	 */
1439e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1440e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1441e705c121SKalle Valo 		ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1442e705c121SKalle Valo 		if (ret) {
1443e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1444e705c121SKalle Valo 			goto out_release;
1445e705c121SKalle Valo 		}
1446e705c121SKalle Valo 
144726d6c16bSSara Sharon 		/*
144826d6c16bSSara Sharon 		 * Only queue for this station is the mcast queue,
144926d6c16bSSara Sharon 		 * which shouldn't be in TFD mask anyway
145026d6c16bSSara Sharon 		 */
145126d6c16bSSara Sharon 		ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta,
1452ced19f26SSara Sharon 					       0, vif->type,
1453ced19f26SSara Sharon 					       IWL_STA_MULTICAST);
145426d6c16bSSara Sharon 		if (ret)
145526d6c16bSSara Sharon 			goto out_release;
145626d6c16bSSara Sharon 
1457e705c121SKalle Valo 		iwl_mvm_vif_dbgfs_register(mvm, vif);
1458e705c121SKalle Valo 		goto out_unlock;
1459e705c121SKalle Valo 	}
1460e705c121SKalle Valo 
1461e705c121SKalle Valo 	mvmvif->features |= hw->netdev_features;
1462e705c121SKalle Valo 
1463e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1464e705c121SKalle Valo 	if (ret)
1465e705c121SKalle Valo 		goto out_release;
1466e705c121SKalle Valo 
1467e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1468e705c121SKalle Valo 	if (ret)
1469e705c121SKalle Valo 		goto out_remove_mac;
1470e705c121SKalle Valo 
1471e705c121SKalle Valo 	/* beacon filtering */
1472e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1473e705c121SKalle Valo 	if (ret)
1474e705c121SKalle Valo 		goto out_remove_mac;
1475e705c121SKalle Valo 
1476e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1477e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1478e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1479e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1480e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1481e705c121SKalle Valo 	}
1482e705c121SKalle Valo 
1483e705c121SKalle Valo 	/*
1484e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1485e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1486e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1487e705c121SKalle Valo 	 */
1488e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1489e705c121SKalle Valo 
1490e705c121SKalle Valo 		mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1491e705c121SKalle Valo 		if (!mvmvif->phy_ctxt) {
1492e705c121SKalle Valo 			ret = -ENOSPC;
1493e705c121SKalle Valo 			goto out_free_bf;
1494e705c121SKalle Valo 		}
1495e705c121SKalle Valo 
1496e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1497e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1498e705c121SKalle Valo 		if (ret)
1499e705c121SKalle Valo 			goto out_unref_phy;
1500e705c121SKalle Valo 
1501d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1502e705c121SKalle Valo 		if (ret)
1503e705c121SKalle Valo 			goto out_unbind;
1504e705c121SKalle Valo 
1505e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1506e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1507e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1508e705c121SKalle Valo 	}
1509e705c121SKalle Valo 
1510b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1511f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1512f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1513b0ffe455SJohannes Berg 
1514baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1515baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
1516baf41bc3SShaul Triebitz 
1517e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
1518e705c121SKalle Valo 	goto out_unlock;
1519e705c121SKalle Valo 
1520e705c121SKalle Valo  out_unbind:
1521e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1522e705c121SKalle Valo  out_unref_phy:
1523e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1524e705c121SKalle Valo  out_free_bf:
1525e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1526e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1527e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1528e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1529e705c121SKalle Valo 	}
1530e705c121SKalle Valo  out_remove_mac:
1531e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1532e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1533e705c121SKalle Valo  out_release:
1534e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1535e705c121SKalle Valo 		mvm->vif_count--;
1536e705c121SKalle Valo  out_unlock:
1537e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1538e705c121SKalle Valo 
1539e705c121SKalle Valo 	return ret;
1540e705c121SKalle Valo }
1541e705c121SKalle Valo 
1542e705c121SKalle Valo static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1543e705c121SKalle Valo 					struct ieee80211_vif *vif)
1544e705c121SKalle Valo {
1545e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1546e705c121SKalle Valo 		/*
1547e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1548e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1549e705c121SKalle Valo 		 * queue are sent in ROC session.
1550e705c121SKalle Valo 		 */
1551e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1552e705c121SKalle Valo 	}
1553e705c121SKalle Valo }
1554e705c121SKalle Valo 
1555e705c121SKalle Valo static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1556e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1557e705c121SKalle Valo {
1558e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1559e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
156086e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1561e705c121SKalle Valo 
1562e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1563e705c121SKalle Valo 
1564b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1565b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1566b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1567b0ffe455SJohannes Berg 
1568e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1569e705c121SKalle Valo 
157086e177d8SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->probe_resp_data,
157186e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
157286e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
157386e177d8SGregory Greenman 	if (probe_data)
157486e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
157586e177d8SGregory Greenman 
1576e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1577e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1578e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1579e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1580e705c121SKalle Valo 	}
1581e705c121SKalle Valo 
1582b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1583b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1584b73f9a4aSJohannes Berg 
1585e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1586e705c121SKalle Valo 
1587e705c121SKalle Valo 	/*
1588e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1589e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1590e705c121SKalle Valo 	 */
1591e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1592e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1593e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1594e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1595e705c121SKalle Valo 			mvm->noa_vif = NULL;
1596e705c121SKalle Valo 			mvm->noa_duration = 0;
1597e705c121SKalle Valo 		}
1598e705c121SKalle Valo #endif
159926d6c16bSSara Sharon 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
1600e705c121SKalle Valo 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1601e705c121SKalle Valo 		goto out_release;
1602e705c121SKalle Valo 	}
1603e705c121SKalle Valo 
1604e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1605e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1606d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1607e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1608e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1609e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
1610e705c121SKalle Valo 	}
1611e705c121SKalle Valo 
1612e705c121SKalle Valo 	if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1613e705c121SKalle Valo 		mvm->vif_count--;
1614e705c121SKalle Valo 
1615e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
1616e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1617e705c121SKalle Valo 
1618698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1619698478c4SSara Sharon 
1620baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1621baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1622baf41bc3SShaul Triebitz 
1623e705c121SKalle Valo out_release:
1624e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1625e705c121SKalle Valo }
1626e705c121SKalle Valo 
1627e705c121SKalle Valo static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1628e705c121SKalle Valo {
1629e705c121SKalle Valo 	return 0;
1630e705c121SKalle Valo }
1631e705c121SKalle Valo 
1632e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1633e705c121SKalle Valo 	struct iwl_mvm *mvm;
1634e705c121SKalle Valo 	int port_id;
1635e705c121SKalle Valo };
1636e705c121SKalle Valo 
1637e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1638e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1639e705c121SKalle Valo {
1640e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1641e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1642e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
164397bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
164497bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
164597bce57bSLuca Coelho 		.flags = CMD_ASYNC,
164697bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
164797bce57bSLuca Coelho 	};
1648e705c121SKalle Valo 	int ret, len;
1649e705c121SKalle Valo 
1650e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1651e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1652e705c121SKalle Valo 		return;
1653e705c121SKalle Valo 
1654e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1655e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1656e705c121SKalle Valo 		return;
1657e705c121SKalle Valo 
1658e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1659e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1660e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1661e705c121SKalle Valo 
166297bce57bSLuca Coelho 	hcmd.len[0] = len;
166397bce57bSLuca Coelho 	hcmd.data[0] = cmd;
166497bce57bSLuca Coelho 
166597bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1666e705c121SKalle Valo 	if (ret)
1667e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1668e705c121SKalle Valo }
1669e705c121SKalle Valo 
1670e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1671e705c121SKalle Valo {
1672e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1673e705c121SKalle Valo 		.mvm = mvm,
1674e705c121SKalle Valo 	};
1675e705c121SKalle Valo 
1676e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1677e705c121SKalle Valo 
1678e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1679e705c121SKalle Valo 		return;
1680e705c121SKalle Valo 
1681e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1682e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1683e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1684e705c121SKalle Valo }
1685e705c121SKalle Valo 
1686e705c121SKalle Valo static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1687e705c121SKalle Valo 				     struct netdev_hw_addr_list *mc_list)
1688e705c121SKalle Valo {
1689e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1690e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1691e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1692e705c121SKalle Valo 	int addr_count;
1693e705c121SKalle Valo 	bool pass_all;
1694e705c121SKalle Valo 	int len;
1695e705c121SKalle Valo 
1696e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1697e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1698e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1699e705c121SKalle Valo 	if (pass_all)
1700e705c121SKalle Valo 		addr_count = 0;
1701e705c121SKalle Valo 
1702e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1703e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1704e705c121SKalle Valo 	if (!cmd)
1705e705c121SKalle Valo 		return 0;
1706e705c121SKalle Valo 
1707e705c121SKalle Valo 	if (pass_all) {
1708e705c121SKalle Valo 		cmd->pass_all = 1;
1709e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1710e705c121SKalle Valo 	}
1711e705c121SKalle Valo 
1712e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1713e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1714e705c121SKalle Valo 				   cmd->count, addr->addr);
1715e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1716e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1717e705c121SKalle Valo 		cmd->count++;
1718e705c121SKalle Valo 	}
1719e705c121SKalle Valo 
1720e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1721e705c121SKalle Valo }
1722e705c121SKalle Valo 
1723e705c121SKalle Valo static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1724e705c121SKalle Valo 				     unsigned int changed_flags,
1725e705c121SKalle Valo 				     unsigned int *total_flags,
1726e705c121SKalle Valo 				     u64 multicast)
1727e705c121SKalle Valo {
1728e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1729e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1730e705c121SKalle Valo 
1731e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1732e705c121SKalle Valo 
1733e705c121SKalle Valo 	/* replace previous configuration */
1734e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1735e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1736e705c121SKalle Valo 
1737e705c121SKalle Valo 	if (!cmd)
1738e705c121SKalle Valo 		goto out;
1739e705c121SKalle Valo 
174061e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
174161e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
174261e7d91bSLuca Coelho 
174361e7d91bSLuca Coelho 	if (cmd->pass_all)
174461e7d91bSLuca Coelho 		cmd->count = 0;
174561e7d91bSLuca Coelho 
1746e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1747e705c121SKalle Valo out:
1748e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1749e705c121SKalle Valo 	*total_flags = 0;
1750e705c121SKalle Valo }
1751e705c121SKalle Valo 
1752e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1753e705c121SKalle Valo 					struct ieee80211_vif *vif,
1754e705c121SKalle Valo 					unsigned int filter_flags,
1755e705c121SKalle Valo 					unsigned int changed_flags)
1756e705c121SKalle Valo {
1757e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1758e705c121SKalle Valo 
1759e705c121SKalle Valo 	/* We support only filter for probe requests */
1760e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1761e705c121SKalle Valo 		return;
1762e705c121SKalle Valo 
1763e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1764e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1765e705c121SKalle Valo 	    !vif->p2p)
1766e705c121SKalle Valo 		return;
1767e705c121SKalle Valo 
1768e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1769e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1770e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1771e705c121SKalle Valo }
1772e705c121SKalle Valo 
1773e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1774e705c121SKalle Valo struct iwl_bcast_iter_data {
1775e705c121SKalle Valo 	struct iwl_mvm *mvm;
1776e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd;
1777e705c121SKalle Valo 	u8 current_filter;
1778e705c121SKalle Valo };
1779e705c121SKalle Valo 
1780e705c121SKalle Valo static void
1781e705c121SKalle Valo iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1782e705c121SKalle Valo 			 const struct iwl_fw_bcast_filter *in_filter,
1783e705c121SKalle Valo 			 struct iwl_fw_bcast_filter *out_filter)
1784e705c121SKalle Valo {
1785e705c121SKalle Valo 	struct iwl_fw_bcast_filter_attr *attr;
1786e705c121SKalle Valo 	int i;
1787e705c121SKalle Valo 
1788e705c121SKalle Valo 	memcpy(out_filter, in_filter, sizeof(*out_filter));
1789e705c121SKalle Valo 
1790e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1791e705c121SKalle Valo 		attr = &out_filter->attrs[i];
1792e705c121SKalle Valo 
1793e705c121SKalle Valo 		if (!attr->mask)
1794e705c121SKalle Valo 			break;
1795e705c121SKalle Valo 
1796e705c121SKalle Valo 		switch (attr->reserved1) {
1797e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_IP):
1798e705c121SKalle Valo 			if (vif->bss_conf.arp_addr_cnt != 1) {
1799e705c121SKalle Valo 				attr->mask = 0;
1800e705c121SKalle Valo 				continue;
1801e705c121SKalle Valo 			}
1802e705c121SKalle Valo 
1803e705c121SKalle Valo 			attr->val = vif->bss_conf.arp_addr_list[0];
1804e705c121SKalle Valo 			break;
1805e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1806e705c121SKalle Valo 			attr->val = *(__be32 *)&vif->addr[2];
1807e705c121SKalle Valo 			break;
1808e705c121SKalle Valo 		default:
1809e705c121SKalle Valo 			break;
1810e705c121SKalle Valo 		}
1811e705c121SKalle Valo 		attr->reserved1 = 0;
1812e705c121SKalle Valo 		out_filter->num_attrs++;
1813e705c121SKalle Valo 	}
1814e705c121SKalle Valo }
1815e705c121SKalle Valo 
1816e705c121SKalle Valo static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1817e705c121SKalle Valo 					  struct ieee80211_vif *vif)
1818e705c121SKalle Valo {
1819e705c121SKalle Valo 	struct iwl_bcast_iter_data *data = _data;
1820e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1821e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd = data->cmd;
1822e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1823e705c121SKalle Valo 	struct iwl_fw_bcast_mac *bcast_mac;
1824e705c121SKalle Valo 	int i;
1825e705c121SKalle Valo 
1826e705c121SKalle Valo 	if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1827e705c121SKalle Valo 		return;
1828e705c121SKalle Valo 
1829e705c121SKalle Valo 	bcast_mac = &cmd->macs[mvmvif->id];
1830e705c121SKalle Valo 
1831e705c121SKalle Valo 	/*
1832e705c121SKalle Valo 	 * enable filtering only for associated stations, but not for P2P
1833e705c121SKalle Valo 	 * Clients
1834e705c121SKalle Valo 	 */
1835e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1836e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1837e705c121SKalle Valo 		return;
1838e705c121SKalle Valo 
1839e705c121SKalle Valo 	bcast_mac->default_discard = 1;
1840e705c121SKalle Valo 
1841e705c121SKalle Valo 	/* copy all configured filters */
1842e705c121SKalle Valo 	for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1843e705c121SKalle Valo 		/*
1844e705c121SKalle Valo 		 * Make sure we don't exceed our filters limit.
1845e705c121SKalle Valo 		 * if there is still a valid filter to be configured,
1846e705c121SKalle Valo 		 * be on the safe side and just allow bcast for this mac.
1847e705c121SKalle Valo 		 */
1848e705c121SKalle Valo 		if (WARN_ON_ONCE(data->current_filter >=
1849e705c121SKalle Valo 				 ARRAY_SIZE(cmd->filters))) {
1850e705c121SKalle Valo 			bcast_mac->default_discard = 0;
1851e705c121SKalle Valo 			bcast_mac->attached_filters = 0;
1852e705c121SKalle Valo 			break;
1853e705c121SKalle Valo 		}
1854e705c121SKalle Valo 
1855e705c121SKalle Valo 		iwl_mvm_set_bcast_filter(vif,
1856e705c121SKalle Valo 					 &mvm->bcast_filters[i],
1857e705c121SKalle Valo 					 &cmd->filters[data->current_filter]);
1858e705c121SKalle Valo 
1859e705c121SKalle Valo 		/* skip current filter if it contains no attributes */
1860e705c121SKalle Valo 		if (!cmd->filters[data->current_filter].num_attrs)
1861e705c121SKalle Valo 			continue;
1862e705c121SKalle Valo 
1863e705c121SKalle Valo 		/* attach the filter to current mac */
1864e705c121SKalle Valo 		bcast_mac->attached_filters |=
1865e705c121SKalle Valo 				cpu_to_le16(BIT(data->current_filter));
1866e705c121SKalle Valo 
1867e705c121SKalle Valo 		data->current_filter++;
1868e705c121SKalle Valo 	}
1869e705c121SKalle Valo }
1870e705c121SKalle Valo 
1871e705c121SKalle Valo bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1872e705c121SKalle Valo 				    struct iwl_bcast_filter_cmd *cmd)
1873e705c121SKalle Valo {
1874e705c121SKalle Valo 	struct iwl_bcast_iter_data iter_data = {
1875e705c121SKalle Valo 		.mvm = mvm,
1876e705c121SKalle Valo 		.cmd = cmd,
1877e705c121SKalle Valo 	};
1878e705c121SKalle Valo 
1879e705c121SKalle Valo 	if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1880e705c121SKalle Valo 		return false;
1881e705c121SKalle Valo 
1882e705c121SKalle Valo 	memset(cmd, 0, sizeof(*cmd));
1883e705c121SKalle Valo 	cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1884e705c121SKalle Valo 	cmd->max_macs = ARRAY_SIZE(cmd->macs);
1885e705c121SKalle Valo 
1886e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
1887e705c121SKalle Valo 	/* use debugfs filters/macs if override is configured */
1888e705c121SKalle Valo 	if (mvm->dbgfs_bcast_filtering.override) {
1889e705c121SKalle Valo 		memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1890e705c121SKalle Valo 		       sizeof(cmd->filters));
1891e705c121SKalle Valo 		memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1892e705c121SKalle Valo 		       sizeof(cmd->macs));
1893e705c121SKalle Valo 		return true;
1894e705c121SKalle Valo 	}
1895e705c121SKalle Valo #endif
1896e705c121SKalle Valo 
1897e705c121SKalle Valo 	/* if no filters are configured, do nothing */
1898e705c121SKalle Valo 	if (!mvm->bcast_filters)
1899e705c121SKalle Valo 		return false;
1900e705c121SKalle Valo 
1901e705c121SKalle Valo 	/* configure and attach these filters for each associated sta vif */
1902e705c121SKalle Valo 	ieee80211_iterate_active_interfaces(
1903e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1904e705c121SKalle Valo 		iwl_mvm_bcast_filter_iterator, &iter_data);
1905e705c121SKalle Valo 
1906e705c121SKalle Valo 	return true;
1907e705c121SKalle Valo }
190834672bb3SEliad Peller 
190934672bb3SEliad Peller static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1910e705c121SKalle Valo {
1911e705c121SKalle Valo 	struct iwl_bcast_filter_cmd cmd;
1912e705c121SKalle Valo 
1913e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1914e705c121SKalle Valo 		return 0;
1915e705c121SKalle Valo 
1916e705c121SKalle Valo 	if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1917e705c121SKalle Valo 		return 0;
1918e705c121SKalle Valo 
1919e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1920e705c121SKalle Valo 				    sizeof(cmd), &cmd);
1921e705c121SKalle Valo }
1922e705c121SKalle Valo #else
192334672bb3SEliad Peller static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1924e705c121SKalle Valo {
1925e705c121SKalle Valo 	return 0;
1926e705c121SKalle Valo }
1927e705c121SKalle Valo #endif
1928e705c121SKalle Valo 
1929a07a8f37SSara Sharon static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
1930a07a8f37SSara Sharon 				    struct ieee80211_vif *vif)
1931a07a8f37SSara Sharon {
1932a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1933a07a8f37SSara Sharon 
1934a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1935a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1936a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1937a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1938a07a8f37SSara Sharon 
1939a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1940a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1941a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1942a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1943a07a8f37SSara Sharon }
1944a07a8f37SSara Sharon 
1945f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1946f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1947f92659a1SSara Sharon {
1948f92659a1SSara Sharon 	if (vif->mu_mimo_owner) {
1949f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1950f92659a1SSara Sharon 
1951f92659a1SSara Sharon 		/*
1952f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1953f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1954f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
1955f92659a1SSara Sharon 		 */
1956f92659a1SSara Sharon 		ieee80211_update_mu_groups(vif,
1957f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
1958f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
1959f92659a1SSara Sharon 	}
1960f92659a1SSara Sharon }
1961f92659a1SSara Sharon 
1962f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1963f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
1964f92659a1SSara Sharon {
1965f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1966f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1967f92659a1SSara Sharon 
1968f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
1969f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1970f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
1971f92659a1SSara Sharon }
1972f92659a1SSara Sharon 
1973514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1974514c3069SLuca Coelho {
1975514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
1976514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
1977514c3069SLuca Coelho 	u8 residue_bits;
1978514c3069SLuca Coelho 	u8 res;
1979514c3069SLuca Coelho 
1980514c3069SLuca Coelho 	if (bit_num <= 5)
1981514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
1982514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
1983514c3069SLuca Coelho 
1984514c3069SLuca Coelho 	/*
1985514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
1986514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
1987514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
1988514c3069SLuca Coelho 	 */
1989514c3069SLuca Coelho 
1990514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
1991514c3069SLuca Coelho 
1992514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
1993514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
1994514c3069SLuca Coelho 	      residue_bits;
1995514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
1996514c3069SLuca Coelho 
1997514c3069SLuca Coelho 	return res;
1998514c3069SLuca Coelho }
1999514c3069SLuca Coelho 
2000514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2001514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2002514c3069SLuca Coelho {
2003514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2004514c3069SLuca Coelho 	struct iwl_he_sta_context_cmd sta_ctxt_cmd = {
2005514c3069SLuca Coelho 		.sta_id = sta_id,
2006514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2007514c3069SLuca Coelho 		.bss_color = vif->bss_conf.bss_color,
2008514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2009514c3069SLuca Coelho 		.frame_time_rts_th =
2010514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2011514c3069SLuca Coelho 	};
2012d14ae796SSara Sharon 	int size = fw_has_api(&mvm->fw->ucode_capa,
2013d14ae796SSara Sharon 			      IWL_UCODE_TLV_API_MBSSID_HE) ?
2014d14ae796SSara Sharon 		   sizeof(sta_ctxt_cmd) :
2015d14ae796SSara Sharon 		   sizeof(struct iwl_he_sta_context_cmd_v1);
2016514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2017514c3069SLuca Coelho 	u32 flags;
2018514c3069SLuca Coelho 	int i;
2019514c3069SLuca Coelho 
2020514c3069SLuca Coelho 	rcu_read_lock();
2021514c3069SLuca Coelho 
2022514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2023514c3069SLuca Coelho 	if (IS_ERR(sta)) {
2024514c3069SLuca Coelho 		rcu_read_unlock();
2025514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2026514c3069SLuca Coelho 		return;
2027514c3069SLuca Coelho 	}
2028514c3069SLuca Coelho 
2029514c3069SLuca Coelho 	if (!sta->he_cap.has_he) {
2030514c3069SLuca Coelho 		rcu_read_unlock();
2031514c3069SLuca Coelho 		return;
2032514c3069SLuca Coelho 	}
2033514c3069SLuca Coelho 
2034514c3069SLuca Coelho 	flags = 0;
2035514c3069SLuca Coelho 
20364f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
20374f58121dSIlan Peer 	if (mvmvif->he_ru_2mhz_block)
20384f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
20394f58121dSIlan Peer 
2040514c3069SLuca Coelho 	/* HTC flags */
2041514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[0] &
2042514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP0_HTC_HE)
2043514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2044514c3069SLuca Coelho 	if ((sta->he_cap.he_cap_elem.mac_cap_info[1] &
2045514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2046514c3069SLuca Coelho 	    (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2047514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2048514c3069SLuca Coelho 		u8 link_adap =
2049514c3069SLuca Coelho 			((sta->he_cap.he_cap_elem.mac_cap_info[2] &
2050514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2051514c3069SLuca Coelho 			 (sta->he_cap.he_cap_elem.mac_cap_info[1] &
2052514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2053514c3069SLuca Coelho 
2054514c3069SLuca Coelho 		if (link_adap == 2)
2055514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2056514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2057514c3069SLuca Coelho 		else if (link_adap == 3)
2058514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2059514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2060514c3069SLuca Coelho 	}
2061514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2062514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2063514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[3] &
2064514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2065514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2066514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2067514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2068514c3069SLuca Coelho 
2069189b8d44SNaftali Goldstein 	/*
2070189b8d44SNaftali Goldstein 	 * Initialize the PPE thresholds to "None" (7), as described in Table
2071189b8d44SNaftali Goldstein 	 * 9-262ac of 80211.ax/D3.0.
2072189b8d44SNaftali Goldstein 	 */
2073189b8d44SNaftali Goldstein 	memset(&sta_ctxt_cmd.pkt_ext, 7, sizeof(sta_ctxt_cmd.pkt_ext));
2074189b8d44SNaftali Goldstein 
2075189b8d44SNaftali Goldstein 	/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2076514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.phy_cap_info[6] &
2077514c3069SLuca Coelho 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2078514c3069SLuca Coelho 		u8 nss = (sta->he_cap.ppe_thres[0] &
2079514c3069SLuca Coelho 			  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2080514c3069SLuca Coelho 		u8 ru_index_bitmap =
2081514c3069SLuca Coelho 			(sta->he_cap.ppe_thres[0] &
2082514c3069SLuca Coelho 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2083514c3069SLuca Coelho 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2084514c3069SLuca Coelho 		u8 *ppe = &sta->he_cap.ppe_thres[0];
2085514c3069SLuca Coelho 		u8 ppe_pos_bit = 7; /* Starting after PPE header */
2086514c3069SLuca Coelho 
2087514c3069SLuca Coelho 		/*
2088514c3069SLuca Coelho 		 * FW currently supports only nss == MAX_HE_SUPP_NSS
2089514c3069SLuca Coelho 		 *
2090514c3069SLuca Coelho 		 * If nss > MAX: we can ignore values we don't support
2091514c3069SLuca Coelho 		 * If nss < MAX: we can set zeros in other streams
2092514c3069SLuca Coelho 		 */
2093514c3069SLuca Coelho 		if (nss > MAX_HE_SUPP_NSS) {
2094514c3069SLuca Coelho 			IWL_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2095514c3069SLuca Coelho 				 MAX_HE_SUPP_NSS);
2096514c3069SLuca Coelho 			nss = MAX_HE_SUPP_NSS;
2097514c3069SLuca Coelho 		}
2098514c3069SLuca Coelho 
2099514c3069SLuca Coelho 		for (i = 0; i < nss; i++) {
2100514c3069SLuca Coelho 			u8 ru_index_tmp = ru_index_bitmap << 1;
2101514c3069SLuca Coelho 			u8 bw;
2102514c3069SLuca Coelho 
2103514c3069SLuca Coelho 			for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX; bw++) {
2104514c3069SLuca Coelho 				ru_index_tmp >>= 1;
2105514c3069SLuca Coelho 				if (!(ru_index_tmp & 1))
2106514c3069SLuca Coelho 					continue;
2107514c3069SLuca Coelho 
2108514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][1] =
2109514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2110514c3069SLuca Coelho 							       ppe_pos_bit);
2111514c3069SLuca Coelho 				ppe_pos_bit +=
2112514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2113514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0] =
2114514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2115514c3069SLuca Coelho 							       ppe_pos_bit);
2116514c3069SLuca Coelho 				ppe_pos_bit +=
2117514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2118514c3069SLuca Coelho 			}
2119514c3069SLuca Coelho 		}
2120514c3069SLuca Coelho 
2121514c3069SLuca Coelho 		flags |= STA_CTXT_HE_PACKET_EXT;
212277ff2c6bSLiad Kaufman 	} else if ((sta->he_cap.he_cap_elem.phy_cap_info[9] &
212377ff2c6bSLiad Kaufman 		    IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) !=
212477ff2c6bSLiad Kaufman 		  IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_RESERVED) {
212577ff2c6bSLiad Kaufman 		int low_th = -1;
212677ff2c6bSLiad Kaufman 		int high_th = -1;
212777ff2c6bSLiad Kaufman 
212877ff2c6bSLiad Kaufman 		/* Take the PPE thresholds from the nominal padding info */
212977ff2c6bSLiad Kaufman 		switch (sta->he_cap.he_cap_elem.phy_cap_info[9] &
213077ff2c6bSLiad Kaufman 			IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) {
213177ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_0US:
213277ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
213377ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
213477ff2c6bSLiad Kaufman 			break;
213577ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_8US:
213677ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_BPSK;
213777ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
213877ff2c6bSLiad Kaufman 			break;
213977ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US:
214077ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
214177ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_BPSK;
214277ff2c6bSLiad Kaufman 			break;
214377ff2c6bSLiad Kaufman 		}
214477ff2c6bSLiad Kaufman 
214577ff2c6bSLiad Kaufman 		/* Set the PPE thresholds accordingly */
214677ff2c6bSLiad Kaufman 		if (low_th >= 0 && high_th >= 0) {
21470bfefe2fSLiad Kaufman 			struct iwl_he_pkt_ext *pkt_ext =
21480bfefe2fSLiad Kaufman 				(struct iwl_he_pkt_ext *)&sta_ctxt_cmd.pkt_ext;
214977ff2c6bSLiad Kaufman 
215077ff2c6bSLiad Kaufman 			for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
215177ff2c6bSLiad Kaufman 				u8 bw;
215277ff2c6bSLiad Kaufman 
215377ff2c6bSLiad Kaufman 				for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX;
215477ff2c6bSLiad Kaufman 				     bw++) {
21550bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][0] =
21560bfefe2fSLiad Kaufman 						low_th;
21570bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][1] =
21580bfefe2fSLiad Kaufman 						high_th;
215977ff2c6bSLiad Kaufman 				}
216077ff2c6bSLiad Kaufman 			}
216177ff2c6bSLiad Kaufman 
216277ff2c6bSLiad Kaufman 			flags |= STA_CTXT_HE_PACKET_EXT;
216377ff2c6bSLiad Kaufman 		}
2164514c3069SLuca Coelho 	}
2165514c3069SLuca Coelho 	rcu_read_unlock();
2166514c3069SLuca Coelho 
2167514c3069SLuca Coelho 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2168514c3069SLuca Coelho 	flags |= STA_CTXT_HE_MU_EDCA_CW;
21695cc74f65SShaul Triebitz 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2170514c3069SLuca Coelho 		struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2171514c3069SLuca Coelho 			&mvmvif->queue_params[i].mu_edca_param_rec;
21725cc74f65SShaul Triebitz 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2173514c3069SLuca Coelho 
2174514c3069SLuca Coelho 		if (!mvmvif->queue_params[i].mu_edca) {
2175514c3069SLuca Coelho 			flags &= ~STA_CTXT_HE_MU_EDCA_CW;
2176514c3069SLuca Coelho 			break;
2177514c3069SLuca Coelho 		}
2178514c3069SLuca Coelho 
21795cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].cwmin =
2180514c3069SLuca Coelho 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
21815cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].cwmax =
2182514c3069SLuca Coelho 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
21835cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].aifsn =
2184514c3069SLuca Coelho 			cpu_to_le16(mu_edca->aifsn);
21855cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].mu_time =
2186514c3069SLuca Coelho 			cpu_to_le16(mu_edca->mu_edca_timer);
2187514c3069SLuca Coelho 	}
2188514c3069SLuca Coelho 
2189514c3069SLuca Coelho 	if (vif->bss_conf.multi_sta_back_32bit)
2190514c3069SLuca Coelho 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2191514c3069SLuca Coelho 
2192514c3069SLuca Coelho 	if (vif->bss_conf.ack_enabled)
2193514c3069SLuca Coelho 		flags |= STA_CTXT_HE_ACK_ENABLED;
2194514c3069SLuca Coelho 
2195514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2196514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2197514c3069SLuca Coelho 
2198514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2199514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2200514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2201514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2202514c3069SLuca Coelho 	}
2203514c3069SLuca Coelho 
2204918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2205918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2206918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2207918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2208d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2209d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2210d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2211d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2212d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2213d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2214918cbf39SSara Sharon 	}
2215514c3069SLuca Coelho 
2216514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2217514c3069SLuca Coelho 
2218514c3069SLuca Coelho 	if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(STA_HE_CTXT_CMD,
2219514c3069SLuca Coelho 						 DATA_PATH_GROUP, 0),
2220d14ae796SSara Sharon 				 0, size, &sta_ctxt_cmd))
2221514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2222514c3069SLuca Coelho }
2223514c3069SLuca Coelho 
2224e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2225e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2226e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
2227e705c121SKalle Valo 					     u32 changes)
2228e705c121SKalle Valo {
2229e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2230e705c121SKalle Valo 	int ret;
2231e705c121SKalle Valo 
2232e705c121SKalle Valo 	/*
2233e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
2234e705c121SKalle Valo 	 * beacon interval, which was not known when the station interface was
2235e705c121SKalle Valo 	 * added.
2236e705c121SKalle Valo 	 */
2237514c3069SLuca Coelho 	if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
2238230ba6c5SLuca Coelho 		if (vif->bss_conf.he_support &&
2239230ba6c5SLuca Coelho 		    !iwlwifi_mod_params.disable_11ax)
2240514c3069SLuca Coelho 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2241514c3069SLuca Coelho 
2242e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2243514c3069SLuca Coelho 	}
2244e705c121SKalle Valo 
224540ecdd01SShaul Triebitz 	/* Update MU EDCA params */
224640ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
224740ecdd01SShaul Triebitz 	    bss_conf->assoc && vif->bss_conf.he_support &&
224840ecdd01SShaul Triebitz 	    !iwlwifi_mod_params.disable_11ax)
224940ecdd01SShaul Triebitz 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
225040ecdd01SShaul Triebitz 
2251e705c121SKalle Valo 	/*
2252e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2253e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2254e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2255e705c121SKalle Valo 	 * branch for disassociation below.
2256e705c121SKalle Valo 	 */
2257e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2258e705c121SKalle Valo 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2259e705c121SKalle Valo 
2260e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
2261e705c121SKalle Valo 	if (ret)
2262e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2263e705c121SKalle Valo 
2264e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2265e705c121SKalle Valo 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2266e705c121SKalle Valo 	mvmvif->associated = bss_conf->assoc;
2267e705c121SKalle Valo 
2268e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2269e705c121SKalle Valo 		if (bss_conf->assoc) {
2270e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2271e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2272e705c121SKalle Valo 			memset(&mvmvif->beacon_stats, 0,
2273e705c121SKalle Valo 			       sizeof(mvmvif->beacon_stats));
2274e705c121SKalle Valo 
2275e705c121SKalle Valo 			/* add quota for this interface */
2276e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2277e705c121SKalle Valo 			if (ret) {
2278e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2279e705c121SKalle Valo 				return;
2280e705c121SKalle Valo 			}
2281e705c121SKalle Valo 
2282e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2283*fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2284*fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2285*fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2286e705c121SKalle Valo 				/*
2287e705c121SKalle Valo 				 * If we're restarting then the firmware will
2288e705c121SKalle Valo 				 * obviously have lost synchronisation with
2289e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2290e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2291e705c121SKalle Valo 				 * scheduling a session protection time event.
2292e705c121SKalle Valo 				 *
2293e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2294e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2295e705c121SKalle Valo 				 * the beacon interval.
2296e705c121SKalle Valo 				 *
2297e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2298e705c121SKalle Valo 				 * than a single interface.
2299*fe959c7bSEmmanuel Grumbach 				 *
2300*fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2301*fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2302*fe959c7bSEmmanuel Grumbach 				 * only anyway.
2303e705c121SKalle Valo 				 */
2304e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2305e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2306e705c121SKalle Valo 							5 * dur, false);
2307e705c121SKalle Valo 			}
2308e705c121SKalle Valo 
2309e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2310e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2311e705c121SKalle Valo 			if (vif->p2p) {
2312e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2313e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
2314e705c121SKalle Valo 						    IEEE80211_SMPS_DYNAMIC);
2315e705c121SKalle Valo 			}
23160ae98812SSara Sharon 		} else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
2317e705c121SKalle Valo 			/*
2318e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2319e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2320e705c121SKalle Valo 			 */
232169e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
232269e508b4SIlan Peer 			WARN_ONCE(ret &&
232369e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
232469e508b4SIlan Peer 					    &mvm->status),
2325e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2326e705c121SKalle Valo 
23276b28f978SEmmanuel Grumbach 			/*
23286b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
23296b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
23306b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
23316b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
23326b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
23336b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
23346b28f978SEmmanuel Grumbach 			 */
23356b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
23366b28f978SEmmanuel Grumbach 				      &mvm->status)) {
23376b28f978SEmmanuel Grumbach 				/*
23386b28f978SEmmanuel Grumbach 				 * Remove AP station now that
23396b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
23406b28f978SEmmanuel Grumbach 				 */
23416b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
23426b28f978SEmmanuel Grumbach 							mvmvif->ap_sta_id);
2343e705c121SKalle Valo 				if (ret)
23446b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
23456b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2346e705c121SKalle Valo 
23470ae98812SSara Sharon 				mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
23486b28f978SEmmanuel Grumbach 			}
23496b28f978SEmmanuel Grumbach 
2350e705c121SKalle Valo 			/* remove quota for this interface */
2351e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2352e705c121SKalle Valo 			if (ret)
2353e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2354e705c121SKalle Valo 
2355e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2356e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2357e705c121SKalle Valo 			if (ret)
2358e705c121SKalle Valo 				IWL_ERR(mvm,
2359e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2360e705c121SKalle Valo 					vif->addr);
2361e705c121SKalle Valo 		}
2362e705c121SKalle Valo 
2363a07a8f37SSara Sharon 		/*
2364a07a8f37SSara Sharon 		 * The firmware tracks the MU-MIMO group on its own.
2365f92659a1SSara Sharon 		 * However, on HW restart we should restore this data.
2366a07a8f37SSara Sharon 		 */
2367a07a8f37SSara Sharon 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2368f92659a1SSara Sharon 		    (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
2369a07a8f37SSara Sharon 			ret = iwl_mvm_update_mu_groups(mvm, vif);
2370a07a8f37SSara Sharon 			if (ret)
2371a07a8f37SSara Sharon 				IWL_ERR(mvm,
2372a07a8f37SSara Sharon 					"failed to update VHT MU_MIMO groups\n");
2373a07a8f37SSara Sharon 		}
2374a07a8f37SSara Sharon 
2375e705c121SKalle Valo 		iwl_mvm_recalc_multicast(mvm);
237634672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2377e705c121SKalle Valo 
2378e705c121SKalle Valo 		/* reset rssi values */
2379e705c121SKalle Valo 		mvmvif->bf_data.ave_beacon_signal = 0;
2380e705c121SKalle Valo 
2381e705c121SKalle Valo 		iwl_mvm_bt_coex_vif_change(mvm);
2382e705c121SKalle Valo 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2383e705c121SKalle Valo 				    IEEE80211_SMPS_AUTOMATIC);
2384355346baSAvraham Stern 		if (fw_has_capa(&mvm->fw->ucode_capa,
2385355346baSAvraham Stern 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2386355346baSAvraham Stern 			iwl_mvm_config_scan(mvm);
2387b45242c9SAvraham Stern 	}
2388b45242c9SAvraham Stern 
2389b45242c9SAvraham Stern 	if (changes & BSS_CHANGED_BEACON_INFO) {
2390e705c121SKalle Valo 		/*
2391b45242c9SAvraham Stern 		 * We received a beacon from the associated AP so
2392e705c121SKalle Valo 		 * remove the session protection.
2393*fe959c7bSEmmanuel Grumbach 		 * A firmware with the new API will remove it automatically.
2394e705c121SKalle Valo 		 */
2395*fe959c7bSEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
2396*fe959c7bSEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
23973edfb5f4SAvraham Stern 			iwl_mvm_stop_session_protection(mvm, vif);
2398e705c121SKalle Valo 
2399e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2400e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2401e705c121SKalle Valo 	}
2402e705c121SKalle Valo 
2403283115fbSAvri Altman 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2404283115fbSAvri Altman 		       /*
2405283115fbSAvri Altman 			* Send power command on every beacon change,
2406283115fbSAvri Altman 			* because we may have not enabled beacon abort yet.
2407283115fbSAvri Altman 			*/
2408283115fbSAvri Altman 		       BSS_CHANGED_BEACON_INFO)) {
2409e705c121SKalle Valo 		ret = iwl_mvm_power_update_mac(mvm);
2410e705c121SKalle Valo 		if (ret)
2411e705c121SKalle Valo 			IWL_ERR(mvm, "failed to update power mode\n");
2412e705c121SKalle Valo 	}
2413e705c121SKalle Valo 
2414e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2415e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2416e705c121SKalle Valo 				bss_conf->txpower);
2417e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2418e705c121SKalle Valo 	}
2419e705c121SKalle Valo 
2420e705c121SKalle Valo 	if (changes & BSS_CHANGED_CQM) {
2421e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2422e705c121SKalle Valo 		/* reset cqm events tracking */
2423e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = 0;
2424e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
2425e705c121SKalle Valo 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2426e705c121SKalle Valo 			if (ret)
2427e705c121SKalle Valo 				IWL_ERR(mvm,
2428e705c121SKalle Valo 					"failed to update CQM thresholds\n");
2429e705c121SKalle Valo 		}
2430e705c121SKalle Valo 	}
2431e705c121SKalle Valo 
2432e705c121SKalle Valo 	if (changes & BSS_CHANGED_ARP_FILTER) {
2433e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
243434672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2435e705c121SKalle Valo 	}
2436e705c121SKalle Valo }
2437e705c121SKalle Valo 
2438e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2439e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2440e705c121SKalle Valo {
2441e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2442e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2443c56e00a3SJohannes Berg 	int ret, i;
2444e705c121SKalle Valo 
2445e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2446e705c121SKalle Valo 
2447e705c121SKalle Valo 	/* Send the beacon template */
2448e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2449e705c121SKalle Valo 	if (ret)
2450e705c121SKalle Valo 		goto out_unlock;
2451e705c121SKalle Valo 
2452e705c121SKalle Valo 	/*
2453e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
2454e705c121SKalle Valo 	 * beacon interval, which was not known when the AP interface was added.
2455e705c121SKalle Valo 	 */
2456e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2457e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2458e705c121SKalle Valo 
2459e705c121SKalle Valo 	mvmvif->ap_assoc_sta_count = 0;
2460e705c121SKalle Valo 
2461e705c121SKalle Valo 	/* Add the mac context */
2462e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2463e705c121SKalle Valo 	if (ret)
2464e705c121SKalle Valo 		goto out_unlock;
2465e705c121SKalle Valo 
2466e705c121SKalle Valo 	/* Perform the binding */
2467e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2468e705c121SKalle Valo 	if (ret)
2469e705c121SKalle Valo 		goto out_remove;
2470e705c121SKalle Valo 
247163dd5d02SSara Sharon 	/*
247263dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
247363dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
247463dd5d02SSara Sharon 	 * cause assert 0x2b00.
247563dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
247663dd5d02SSara Sharon 	 * the TLV
247763dd5d02SSara Sharon 	 */
247863dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2479ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2480ced19f26SSara Sharon 		if (ret)
2481ced19f26SSara Sharon 			goto out_unbind;
248263dd5d02SSara Sharon 		/*
248363dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
248463dd5d02SSara Sharon 		 * events are added and applied to the scheduler
248563dd5d02SSara Sharon 		 */
2486e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
248763dd5d02SSara Sharon 		if (ret) {
248863dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
248963dd5d02SSara Sharon 			goto out_unbind;
249063dd5d02SSara Sharon 		}
249163dd5d02SSara Sharon 	} else {
249263dd5d02SSara Sharon 		/*
249363dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
249463dd5d02SSara Sharon 		 * events are added and applied to the scheduler
249563dd5d02SSara Sharon 		 */
249675fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2497e705c121SKalle Valo 		if (ret)
249863dd5d02SSara Sharon 			goto out_unbind;
249975fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
250063dd5d02SSara Sharon 		if (ret) {
250163dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
250263dd5d02SSara Sharon 			goto out_unbind;
250363dd5d02SSara Sharon 		}
250463dd5d02SSara Sharon 	}
250526d6c16bSSara Sharon 
2506e705c121SKalle Valo 	/* must be set before quota calculations */
2507e705c121SKalle Valo 	mvmvif->ap_ibss_active = true;
2508e705c121SKalle Valo 
2509c56e00a3SJohannes Berg 	/* send all the early keys to the device now */
2510c56e00a3SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2511c56e00a3SJohannes Berg 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2512c56e00a3SJohannes Berg 
2513c56e00a3SJohannes Berg 		if (!key)
2514c56e00a3SJohannes Berg 			continue;
2515c56e00a3SJohannes Berg 
2516c56e00a3SJohannes Berg 		mvmvif->ap_early_keys[i] = NULL;
2517c56e00a3SJohannes Berg 
25186569e7d3SJohannes Berg 		ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2519c56e00a3SJohannes Berg 		if (ret)
2520c56e00a3SJohannes Berg 			goto out_quota_failed;
2521c56e00a3SJohannes Berg 	}
2522c56e00a3SJohannes Berg 
252347242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
252447242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, true,
252547242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
252647242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
252747242744STova Mussai 	}
252847242744STova Mussai 
2529e705c121SKalle Valo 	/* power updated needs to be done before quotas */
2530e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2531e705c121SKalle Valo 
2532e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2533e705c121SKalle Valo 	if (ret)
2534e705c121SKalle Valo 		goto out_quota_failed;
2535e705c121SKalle Valo 
2536e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2537e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2538e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2539e705c121SKalle Valo 
2540e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2541e705c121SKalle Valo 
2542e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2543e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2544e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2545e705c121SKalle Valo 
2546b73f9a4aSJohannes Berg 	iwl_mvm_ftm_restart_responder(mvm, vif);
2547b73f9a4aSJohannes Berg 
2548e705c121SKalle Valo 	goto out_unlock;
2549e705c121SKalle Valo 
2550e705c121SKalle Valo out_quota_failed:
2551e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2552e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2553e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2554ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2555e705c121SKalle Valo out_unbind:
2556e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2557e705c121SKalle Valo out_remove:
2558e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2559e705c121SKalle Valo out_unlock:
2560e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2561e705c121SKalle Valo 	return ret;
2562e705c121SKalle Valo }
2563e705c121SKalle Valo 
2564e705c121SKalle Valo static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2565e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2566e705c121SKalle Valo {
2567e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2568e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2569e705c121SKalle Valo 
2570e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
2571e705c121SKalle Valo 
2572e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2573e705c121SKalle Valo 
2574e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2575e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2576e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2577e705c121SKalle Valo 					  &mvmvif->time_event_data);
2578e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2579e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2580e705c121SKalle Valo 	}
2581e705c121SKalle Valo 
2582e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2583e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2584e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2585e705c121SKalle Valo 	}
2586e705c121SKalle Valo 
2587e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2588e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2589e705c121SKalle Valo 
259047242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
259147242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
259247242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
259347242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
259447242744STova Mussai 	}
259547242744STova Mussai 
2596e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2597e705c121SKalle Valo 
2598e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2599e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2600e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2601e705c121SKalle Valo 
2602e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2603ced19f26SSara Sharon 
2604ced19f26SSara Sharon 	/*
2605ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2606ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2607ced19f26SSara Sharon 	 * cause assert 0x2b00.
2608ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2609ced19f26SSara Sharon 	 * bcast station).
2610ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2611ced19f26SSara Sharon 	 */
2612ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
261326d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2614e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2615ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2616ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2617e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2618e705c121SKalle Valo 
2619e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2620e705c121SKalle Valo 
2621e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2622e705c121SKalle Valo 
2623e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2624e705c121SKalle Valo }
2625e705c121SKalle Valo 
2626e705c121SKalle Valo static void
2627e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2628e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2629e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
2630e705c121SKalle Valo 				 u32 changes)
2631e705c121SKalle Valo {
2632e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2633e705c121SKalle Valo 
2634e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
2635e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
2636e705c121SKalle Valo 		return;
2637e705c121SKalle Valo 
2638e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2639e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2640e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2641e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2642e705c121SKalle Valo 
2643e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
2644e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
2645e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2646e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
2647e705c121SKalle Valo 
2648e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2649e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2650e705c121SKalle Valo 				bss_conf->txpower);
2651e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2652e705c121SKalle Valo 	}
2653b73f9a4aSJohannes Berg 
2654b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
2655b73f9a4aSJohannes Berg 		int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2656b73f9a4aSJohannes Berg 
2657b73f9a4aSJohannes Berg 		if (ret)
2658b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2659b73f9a4aSJohannes Berg 				 ret);
2660b73f9a4aSJohannes Berg 	}
2661b73f9a4aSJohannes Berg 
2662e705c121SKalle Valo }
2663e705c121SKalle Valo 
2664e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2665e705c121SKalle Valo 				     struct ieee80211_vif *vif,
2666e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
2667e705c121SKalle Valo 				     u32 changes)
2668e705c121SKalle Valo {
2669e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2670e705c121SKalle Valo 
2671e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2672e705c121SKalle Valo 
2673e705c121SKalle Valo 	if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2674e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2675e705c121SKalle Valo 
2676e705c121SKalle Valo 	switch (vif->type) {
2677e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2678e705c121SKalle Valo 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2679e705c121SKalle Valo 		break;
2680e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
2681e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
2682e705c121SKalle Valo 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2683e705c121SKalle Valo 		break;
268491b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
268591b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
268691b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
268791b08c2dSAviya Erenfeld 		break;
2688e705c121SKalle Valo 	default:
2689e705c121SKalle Valo 		/* shouldn't happen */
2690e705c121SKalle Valo 		WARN_ON_ONCE(1);
2691e705c121SKalle Valo 	}
2692e705c121SKalle Valo 
2693e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2694e705c121SKalle Valo }
2695e705c121SKalle Valo 
2696e705c121SKalle Valo static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2697e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2698e705c121SKalle Valo 			       struct ieee80211_scan_request *hw_req)
2699e705c121SKalle Valo {
2700e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2701e705c121SKalle Valo 	int ret;
2702e705c121SKalle Valo 
2703e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
2704e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2705e705c121SKalle Valo 		return -EINVAL;
2706e705c121SKalle Valo 
2707e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2708e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2709e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2710e705c121SKalle Valo 
2711e705c121SKalle Valo 	return ret;
2712e705c121SKalle Valo }
2713e705c121SKalle Valo 
2714e705c121SKalle Valo static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2715e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2716e705c121SKalle Valo {
2717e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2718e705c121SKalle Valo 
2719e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2720e705c121SKalle Valo 
2721e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2722e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
2723e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
2724e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
2725e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
2726e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
2727e705c121SKalle Valo 	*/
2728e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2729e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2730e705c121SKalle Valo 
2731e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2732e705c121SKalle Valo }
2733e705c121SKalle Valo 
2734e705c121SKalle Valo static void
2735e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2736e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
2737e705c121SKalle Valo 				  int num_frames,
2738e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
2739e705c121SKalle Valo 				  bool more_data)
2740e705c121SKalle Valo {
2741e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2742e705c121SKalle Valo 
2743e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
2744e705c121SKalle Valo 
2745e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2746e705c121SKalle Valo 					  tids, more_data, false);
2747e705c121SKalle Valo }
2748e705c121SKalle Valo 
2749e705c121SKalle Valo static void
2750e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2751e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
2752e705c121SKalle Valo 				    int num_frames,
2753e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
2754e705c121SKalle Valo 				    bool more_data)
2755e705c121SKalle Valo {
2756e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2757e705c121SKalle Valo 
27589a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2759e705c121SKalle Valo 
2760e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2761e705c121SKalle Valo 					  tids, more_data, true);
2762e705c121SKalle Valo }
2763e705c121SKalle Valo 
276465e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2765e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
2766e705c121SKalle Valo 				     struct ieee80211_sta *sta)
2767e705c121SKalle Valo {
2768e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2769e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2770e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
2771e705c121SKalle Valo 	int tid;
2772e705c121SKalle Valo 
2773960f864bSJohannes Berg 	/*
2774960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
2775960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
2776960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
2777960f864bSJohannes Berg 	 */
2778960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2779960f864bSJohannes Berg 		return;
2780960f864bSJohannes Berg 
2781e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2782311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
2783e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2784e705c121SKalle Valo 
27856862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
27861c17627bSSara Sharon 			continue;
27871c17627bSSara Sharon 
2788e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
2789e705c121SKalle Valo 
2790dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
2791e705c121SKalle Valo 			continue;
2792e705c121SKalle Valo 
2793e705c121SKalle Valo 		__set_bit(tid, &tids);
2794e705c121SKalle Valo 	}
2795e705c121SKalle Valo 
2796e705c121SKalle Valo 	switch (cmd) {
2797e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
2798e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
2799e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
2800e705c121SKalle Valo 
2801e705c121SKalle Valo 		if (txqs)
2802e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
2803e705c121SKalle Valo 		/*
2804e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
2805e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
2806e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2807e705c121SKalle Valo 		 */
2808e705c121SKalle Valo 		break;
2809e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
28100ae98812SSara Sharon 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
2811e705c121SKalle Valo 			break;
2812e705c121SKalle Valo 
2813e705c121SKalle Valo 		if (txqs)
2814e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
2815e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
2816e705c121SKalle Valo 		break;
2817e705c121SKalle Valo 	default:
2818e705c121SKalle Valo 		break;
2819e705c121SKalle Valo 	}
2820e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2821e705c121SKalle Valo }
2822e705c121SKalle Valo 
282365e25482SJohannes Berg static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
282465e25482SJohannes Berg 				   struct ieee80211_vif *vif,
282565e25482SJohannes Berg 				   enum sta_notify_cmd cmd,
282665e25482SJohannes Berg 				   struct ieee80211_sta *sta)
282765e25482SJohannes Berg {
282865e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
282965e25482SJohannes Berg }
283065e25482SJohannes Berg 
283165e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
283265e25482SJohannes Berg {
283365e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
283465e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
283565e25482SJohannes Berg 	struct ieee80211_sta *sta;
283665e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
283765e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
283865e25482SJohannes Berg 
283965e25482SJohannes Berg 	if (WARN_ON(notif->sta_id >= ARRAY_SIZE(mvm->fw_id_to_mac_id)))
284065e25482SJohannes Berg 		return;
284165e25482SJohannes Berg 
284265e25482SJohannes Berg 	rcu_read_lock();
2843a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
284465e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
284565e25482SJohannes Berg 		rcu_read_unlock();
284665e25482SJohannes Berg 		return;
284765e25482SJohannes Berg 	}
284865e25482SJohannes Berg 
284965e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
285065e25482SJohannes Berg 
285165e25482SJohannes Berg 	if (!mvmsta->vif ||
285265e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
285365e25482SJohannes Berg 		rcu_read_unlock();
285465e25482SJohannes Berg 		return;
285565e25482SJohannes Berg 	}
285665e25482SJohannes Berg 
285765e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
285865e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
285965e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
286065e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
286165e25482SJohannes Berg 			sta);
286265e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
286365e25482SJohannes Berg 	}
286465e25482SJohannes Berg 
286565e25482SJohannes Berg 	if (sleeping) {
286665e25482SJohannes Berg 		switch (notif->type) {
286765e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
286865e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
286965e25482SJohannes Berg 			break;
287065e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
287165e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
287265e25482SJohannes Berg 			break;
287365e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
287465e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
287565e25482SJohannes Berg 			break;
287665e25482SJohannes Berg 		default:
287765e25482SJohannes Berg 			break;
287865e25482SJohannes Berg 		}
287965e25482SJohannes Berg 	}
288065e25482SJohannes Berg 
288165e25482SJohannes Berg 	rcu_read_unlock();
288265e25482SJohannes Berg }
288365e25482SJohannes Berg 
2884e705c121SKalle Valo static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2885e705c121SKalle Valo 				       struct ieee80211_vif *vif,
2886e705c121SKalle Valo 				       struct ieee80211_sta *sta)
2887e705c121SKalle Valo {
2888e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2889e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2890e705c121SKalle Valo 
2891e705c121SKalle Valo 	/*
2892e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
2893e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
2894e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
2895e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
2896e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
2897e705c121SKalle Valo 	 * callback deleted the station.
2898e705c121SKalle Valo 	 */
2899e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2900e705c121SKalle Valo 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2901e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2902e705c121SKalle Valo 				   ERR_PTR(-ENOENT));
2903e705c121SKalle Valo 
2904e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2905e705c121SKalle Valo }
2906e705c121SKalle Valo 
2907e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2908e705c121SKalle Valo 				const u8 *bssid)
2909e705c121SKalle Valo {
2910b0ffe455SJohannes Berg 	int i;
2911b0ffe455SJohannes Berg 
2912b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2913b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
2914b0ffe455SJohannes Berg 
2915b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
2916b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
2917b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
2918b0ffe455SJohannes Berg 	}
2919b0ffe455SJohannes Berg 
2920e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2921e705c121SKalle Valo 		return;
2922e705c121SKalle Valo 
2923c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
2924cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2925cee5a882SAvri Altman 		return;
2926cee5a882SAvri Altman 	}
2927cee5a882SAvri Altman 
292811dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
292911dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
2930e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2931e705c121SKalle Valo 		return;
2932e705c121SKalle Valo 	}
2933e705c121SKalle Valo 
2934b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
2935b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
2936b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2937b0ffe455SJohannes Berg 			return;
2938b0ffe455SJohannes Berg 		}
2939b0ffe455SJohannes Berg 	}
2940b0ffe455SJohannes Berg 
2941e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2942e705c121SKalle Valo }
2943e705c121SKalle Valo 
29441e8f1329SGolan Ben-Ami static void
29451e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
29461e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
29471e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
29481e8f1329SGolan Ben-Ami {
29491e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
29501e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
29511e8f1329SGolan Ben-Ami 
29526c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
29536c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
29546c042d75SSara Sharon 	if (!trig)
29551e8f1329SGolan Ben-Ami 		return;
29561e8f1329SGolan Ben-Ami 
29571e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
29581e8f1329SGolan Ben-Ami 
29591e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
29601e8f1329SGolan Ben-Ami 		return;
29611e8f1329SGolan Ben-Ami 
29621e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
29631e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
29641e8f1329SGolan Ben-Ami 		return;
29651e8f1329SGolan Ben-Ami 
29667174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
29671e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
29681e8f1329SGolan Ben-Ami 				peer_addr, action);
29691e8f1329SGolan Ben-Ami }
29701e8f1329SGolan Ben-Ami 
29714f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
29724f58121dSIlan Peer 	bool tolerated;
29734f58121dSIlan Peer };
29744f58121dSIlan Peer 
29754f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
29764f58121dSIlan Peer 						    struct cfg80211_bss *bss,
29774f58121dSIlan Peer 						    void *_data)
29784f58121dSIlan Peer {
29794f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
29804f58121dSIlan Peer 	const struct element *elem;
29814f58121dSIlan Peer 
29824f58121dSIlan Peer 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, bss->ies->data,
29834f58121dSIlan Peer 				  bss->ies->len);
29844f58121dSIlan Peer 
29854f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
29864f58121dSIlan Peer 	    !(elem->data[10] &
29874f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
29884f58121dSIlan Peer 		data->tolerated = false;
29894f58121dSIlan Peer 	}
29904f58121dSIlan Peer }
29914f58121dSIlan Peer 
29924f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
29934f58121dSIlan Peer 					       struct ieee80211_vif *vif)
29944f58121dSIlan Peer {
29954f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
29964f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
29974f58121dSIlan Peer 		.tolerated = true,
29984f58121dSIlan Peer 	};
29994f58121dSIlan Peer 
30004f58121dSIlan Peer 	if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
30014f58121dSIlan Peer 		mvmvif->he_ru_2mhz_block = false;
30024f58121dSIlan Peer 		return;
30034f58121dSIlan Peer 	}
30044f58121dSIlan Peer 
30054f58121dSIlan Peer 	cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
30064f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
30074f58121dSIlan Peer 			  &iter_data);
30084f58121dSIlan Peer 
30094f58121dSIlan Peer 	/*
30104f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
30114f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
30124f58121dSIlan Peer 	 */
30134f58121dSIlan Peer 	mvmvif->he_ru_2mhz_block = !iter_data.tolerated;
30144f58121dSIlan Peer }
30154f58121dSIlan Peer 
3016e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3017e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3018e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3019e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3020e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3021e705c121SKalle Valo {
3022e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3023e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
30246ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3025e705c121SKalle Valo 	int ret;
3026e705c121SKalle Valo 
3027e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3028e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3029e705c121SKalle Valo 
3030e705c121SKalle Valo 	/* this would be a mac80211 bug ... but don't crash */
3031e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
3032e705c121SKalle Valo 		return -EINVAL;
3033e705c121SKalle Valo 
303424afba76SLiad Kaufman 	/*
303524afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
303624afba76SLiad Kaufman 	 *
303724afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
303824afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
303924afba76SLiad Kaufman 	 * the queues.
304024afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
304124afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
304224afba76SLiad Kaufman 	 * of.
304324afba76SLiad Kaufman 	 *
304424afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
304524afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
304624afba76SLiad Kaufman 	 */
304724afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3048c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
304924afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
305024afba76SLiad Kaufman 
305124afba76SLiad Kaufman 		/*
305224afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
305324afba76SLiad Kaufman 		 * worker will already remove it if it was on
305424afba76SLiad Kaufman 		 */
305524afba76SLiad Kaufman 	}
305624afba76SLiad Kaufman 
3057e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
30586ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3059d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
30606ea29ce5SJohannes Berg 
3061e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3062e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
3063e705c121SKalle Valo 		/*
3064e705c121SKalle Valo 		 * Firmware bug - it'll crash if the beacon interval is less
3065e705c121SKalle Valo 		 * than 16. We can't avoid connecting at all, so refuse the
3066e705c121SKalle Valo 		 * station state change, this will cause mac80211 to abandon
3067e705c121SKalle Valo 		 * attempts to connect to this AP, and eventually wpa_s will
3068e705c121SKalle Valo 		 * blacklist the AP...
3069e705c121SKalle Valo 		 */
3070e705c121SKalle Valo 		if (vif->type == NL80211_IFTYPE_STATION &&
3071e705c121SKalle Valo 		    vif->bss_conf.beacon_int < 16) {
3072e705c121SKalle Valo 			IWL_ERR(mvm,
3073e705c121SKalle Valo 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3074e705c121SKalle Valo 				sta->addr, vif->bss_conf.beacon_int);
3075e705c121SKalle Valo 			ret = -EINVAL;
3076e705c121SKalle Valo 			goto out_unlock;
3077e705c121SKalle Valo 		}
3078e705c121SKalle Valo 
3079e705c121SKalle Valo 		if (sta->tdls &&
3080e705c121SKalle Valo 		    (vif->p2p ||
3081e705c121SKalle Valo 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
3082e705c121SKalle Valo 						IWL_MVM_TDLS_STA_COUNT ||
3083e705c121SKalle Valo 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3084e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3085e705c121SKalle Valo 			ret = -EBUSY;
3086e705c121SKalle Valo 			goto out_unlock;
3087e705c121SKalle Valo 		}
3088e705c121SKalle Valo 
3089e705c121SKalle Valo 		ret = iwl_mvm_add_sta(mvm, vif, sta);
30901e8f1329SGolan Ben-Ami 		if (sta->tdls && ret == 0) {
3091e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
30921e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
30931e8f1329SGolan Ben-Ami 						   NL80211_TDLS_SETUP);
30941e8f1329SGolan Ben-Ami 		}
3095438af969SSara Sharon 
3096438af969SSara Sharon 		sta->max_rc_amsdu_len = 1;
3097e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3098e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3099e705c121SKalle Valo 		/*
3100e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3101e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3102e705c121SKalle Valo 		 */
3103e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3104e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3105e705c121SKalle Valo 		ret = 0;
3106e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3107e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
31088be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
31099394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
31108be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count++;
31118be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
311202221a81SShaul Triebitz 			if (vif->bss_conf.he_support &&
311302221a81SShaul Triebitz 			    !iwlwifi_mod_params.disable_11ax)
311402221a81SShaul Triebitz 				iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
31159394662aSLiad Kaufman 		} else if (vif->type == NL80211_IFTYPE_STATION) {
31169394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
31174f58121dSIlan Peer 
31184f58121dSIlan Peer 			mvmvif->he_ru_2mhz_block = false;
31194f58121dSIlan Peer 			if (sta->he_cap.has_he)
31204f58121dSIlan Peer 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif);
31214f58121dSIlan Peer 
31229394662aSLiad Kaufman 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
31238be30c13SAyala Beker 		}
3124735a0045SGoodstein, Mordechay 
31253baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
31263baf7528SAvraham Stern 				     false);
3127735a0045SGoodstein, Mordechay 		ret = iwl_mvm_update_sta(mvm, vif, sta);
3128e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3129e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
313028916a16SEmmanuel Grumbach 		ret = 0;
3131e705c121SKalle Valo 
3132e705c121SKalle Valo 		/* we don't support TDLS during DCM */
3133e705c121SKalle Valo 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
3134e705c121SKalle Valo 			iwl_mvm_teardown_tdls_peers(mvm);
3135e705c121SKalle Valo 
31361e8f1329SGolan Ben-Ami 		if (sta->tdls)
31371e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
31381e8f1329SGolan Ben-Ami 						   NL80211_TDLS_ENABLE_LINK);
31391e8f1329SGolan Ben-Ami 
3140e705c121SKalle Valo 		/* enable beacon filtering */
3141e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
31426b7a5aeaSNaftali Goldstein 
314350f56044SIlan Peer 		/*
314450f56044SIlan Peer 		 * Now that the station is authorized, i.e., keys were already
314550f56044SIlan Peer 		 * installed, need to indicate to the FW that
314650f56044SIlan Peer 		 * multicast data frames can be forwarded to the driver
314750f56044SIlan Peer 		 */
314850f56044SIlan Peer 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
314950f56044SIlan Peer 
31503baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
31513baf7528SAvraham Stern 				     true);
3152e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3153e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
315450f56044SIlan Peer 		/* Multicast data frames are no longer allowed */
315550f56044SIlan Peer 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
315650f56044SIlan Peer 
3157e705c121SKalle Valo 		/* disable beacon filtering */
315869e508b4SIlan Peer 		ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
315969e508b4SIlan Peer 		WARN_ON(ret &&
316069e508b4SIlan Peer 			!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
316169e508b4SIlan Peer 				  &mvm->status));
3162e705c121SKalle Valo 		ret = 0;
3163e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3164e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
31658be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
31668be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
31678be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
31688be30c13SAyala Beker 		}
3169e705c121SKalle Valo 		ret = 0;
3170e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3171e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3172e705c121SKalle Valo 		ret = 0;
3173e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3174e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
3175e705c121SKalle Valo 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
31761e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3177e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
31781e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
31791e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
31801e8f1329SGolan Ben-Ami 		}
318134a880d8SLiad Kaufman 
318244135b7cSIlan Peer 		if (unlikely(ret &&
318344135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
318444135b7cSIlan Peer 				      &mvm->status)))
318544135b7cSIlan Peer 			ret = 0;
3186e705c121SKalle Valo 	} else {
3187e705c121SKalle Valo 		ret = -EIO;
3188e705c121SKalle Valo 	}
3189e705c121SKalle Valo  out_unlock:
3190e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3191e705c121SKalle Valo 
3192e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3193e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3194e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3195e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3196e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3197e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3198e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3199e705c121SKalle Valo 	}
3200e705c121SKalle Valo 
3201e705c121SKalle Valo 	return ret;
3202e705c121SKalle Valo }
3203e705c121SKalle Valo 
3204e705c121SKalle Valo static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3205e705c121SKalle Valo {
3206e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3207e705c121SKalle Valo 
3208e705c121SKalle Valo 	mvm->rts_threshold = value;
3209e705c121SKalle Valo 
3210e705c121SKalle Valo 	return 0;
3211e705c121SKalle Valo }
3212e705c121SKalle Valo 
3213e705c121SKalle Valo static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3214e705c121SKalle Valo 				  struct ieee80211_vif *vif,
3215e705c121SKalle Valo 				  struct ieee80211_sta *sta, u32 changed)
3216e705c121SKalle Valo {
3217e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3218dcfe3b10SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3219dcfe3b10SJohannes Berg 
3220dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
3221dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3222dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
3223dcfe3b10SJohannes Berg 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3224dcfe3b10SJohannes Berg 				     true);
3225e705c121SKalle Valo 
3226e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
3227e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
3228e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
3229e705c121SKalle Valo }
3230e705c121SKalle Valo 
3231e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3232e705c121SKalle Valo 			       struct ieee80211_vif *vif, u16 ac,
3233e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
3234e705c121SKalle Valo {
3235e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3236e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3237e705c121SKalle Valo 
3238e705c121SKalle Valo 	mvmvif->queue_params[ac] = *params;
3239e705c121SKalle Valo 
3240e705c121SKalle Valo 	/*
3241e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
3242e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
3243e705c121SKalle Valo 	 */
3244e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3245e705c121SKalle Valo 		int ret;
3246e705c121SKalle Valo 
3247e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
3248e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3249e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3250e705c121SKalle Valo 		return ret;
3251e705c121SKalle Valo 	}
3252e705c121SKalle Valo 	return 0;
3253e705c121SKalle Valo }
3254e705c121SKalle Valo 
3255e705c121SKalle Valo static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3256d4e36e55SIlan Peer 				       struct ieee80211_vif *vif,
3257d4e36e55SIlan Peer 				       u16 req_duration)
3258e705c121SKalle Valo {
3259e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
32607c70fee5SSara Sharon 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
32617c70fee5SSara Sharon 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
3262e705c121SKalle Valo 
3263d4e36e55SIlan Peer 	if (req_duration > duration)
3264d4e36e55SIlan Peer 		duration = req_duration;
3265d4e36e55SIlan Peer 
3266e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3267*fe959c7bSEmmanuel Grumbach 	/* Try really hard to protect the session and hear a beacon
3268*fe959c7bSEmmanuel Grumbach 	 * The new session protection command allows us to protect the
3269*fe959c7bSEmmanuel Grumbach 	 * session for a much longer time since the firmware will internally
3270*fe959c7bSEmmanuel Grumbach 	 * create two events: a 300TU one with a very high priority that
3271*fe959c7bSEmmanuel Grumbach 	 * won't be fragmented which should be enough for 99% of the cases,
3272*fe959c7bSEmmanuel Grumbach 	 * and another one (which we configure here to be 900TU long) which
3273*fe959c7bSEmmanuel Grumbach 	 * will have a slightly lower priority, but more importantly, can be
3274*fe959c7bSEmmanuel Grumbach 	 * fragmented so that it'll allow other activities to run.
3275*fe959c7bSEmmanuel Grumbach 	 */
3276*fe959c7bSEmmanuel Grumbach 	if (fw_has_capa(&mvm->fw->ucode_capa,
3277*fe959c7bSEmmanuel Grumbach 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
3278*fe959c7bSEmmanuel Grumbach 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
3279*fe959c7bSEmmanuel Grumbach 						    min_duration);
3280*fe959c7bSEmmanuel Grumbach 	else
3281*fe959c7bSEmmanuel Grumbach 		iwl_mvm_protect_session(mvm, vif, duration,
3282*fe959c7bSEmmanuel Grumbach 					min_duration, 500, false);
3283e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3284e705c121SKalle Valo }
3285e705c121SKalle Valo 
3286e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3287e705c121SKalle Valo 					struct ieee80211_vif *vif,
3288e705c121SKalle Valo 					struct cfg80211_sched_scan_request *req,
3289e705c121SKalle Valo 					struct ieee80211_scan_ies *ies)
3290e705c121SKalle Valo {
3291e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3292e705c121SKalle Valo 
3293e705c121SKalle Valo 	int ret;
3294e705c121SKalle Valo 
3295e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3296e705c121SKalle Valo 
3297e705c121SKalle Valo 	if (!vif->bss_conf.idle) {
3298e705c121SKalle Valo 		ret = -EBUSY;
3299e705c121SKalle Valo 		goto out;
3300e705c121SKalle Valo 	}
3301e705c121SKalle Valo 
3302e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
3303e705c121SKalle Valo 
3304e705c121SKalle Valo out:
3305e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3306e705c121SKalle Valo 	return ret;
3307e705c121SKalle Valo }
3308e705c121SKalle Valo 
3309e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3310e705c121SKalle Valo 				       struct ieee80211_vif *vif)
3311e705c121SKalle Valo {
3312e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3313e705c121SKalle Valo 	int ret;
3314e705c121SKalle Valo 
3315e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3316e705c121SKalle Valo 
3317e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3318e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
3319e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
3320e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
3321e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3322e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
3323e705c121SKalle Valo 	 * not running.
3324e705c121SKalle Valo 	*/
3325e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
3326e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3327e705c121SKalle Valo 		return 0;
3328e705c121SKalle Valo 	}
3329e705c121SKalle Valo 
3330e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
3331e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3332e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
3333e705c121SKalle Valo 
3334e705c121SKalle Valo 	return ret;
3335e705c121SKalle Valo }
3336e705c121SKalle Valo 
33376569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3338e705c121SKalle Valo 				 enum set_key_cmd cmd,
3339e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3340e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3341e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
3342e705c121SKalle Valo {
3343c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3344e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3345f5e28eacSJohannes Berg 	struct iwl_mvm_sta *mvmsta;
3346f5e28eacSJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn;
3347f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
3348c56e00a3SJohannes Berg 	int ret, i;
33494615fd15SEmmanuel Grumbach 	u8 key_offset;
3350e705c121SKalle Valo 
33513b37f4c9SJohannes Berg 	if (iwlwifi_mod_params.swcrypto) {
3352e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
3353e705c121SKalle Valo 		return -EOPNOTSUPP;
3354e705c121SKalle Valo 	}
3355e705c121SKalle Valo 
3356e705c121SKalle Valo 	switch (key->cipher) {
3357e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3358286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
3359e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
33601ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
33617f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
33627f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
33637f768ad5SDavid Spinadel 		} else {
33647f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
33657f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
33667f768ad5SDavid Spinadel 		}
3367e705c121SKalle Valo 		break;
3368e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
33692a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
33702a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
337185aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
3372e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3373e705c121SKalle Valo 		break;
3374e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
33758e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
33768e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3377e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
3378e705c121SKalle Valo 		break;
3379e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3380e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3381475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
3382e705c121SKalle Valo 			break;
3383475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
3384475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
3385475c6bdeSJohannes Berg 		/* support HW crypto on TX */
3386475c6bdeSJohannes Berg 		return 0;
3387e705c121SKalle Valo 	default:
3388e705c121SKalle Valo 		/* currently FW supports only one optional cipher scheme */
3389e705c121SKalle Valo 		if (hw->n_cipher_schemes &&
3390e705c121SKalle Valo 		    hw->cipher_schemes->cipher == key->cipher)
3391e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3392e705c121SKalle Valo 		else
3393e705c121SKalle Valo 			return -EOPNOTSUPP;
3394e705c121SKalle Valo 	}
3395e705c121SKalle Valo 
3396e705c121SKalle Valo 	switch (cmd) {
3397e705c121SKalle Valo 	case SET_KEY:
3398e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
3399e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
3400e705c121SKalle Valo 			/*
3401e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
3402e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
3403e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
34048e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
3405e705c121SKalle Valo 			 */
34068e160ab8SAyala Beker 			if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
34078e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
34088e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
340981279c49SJohannes Berg 				ret = -EOPNOTSUPP;
341081279c49SJohannes Berg 			else
3411e705c121SKalle Valo 				ret = 0;
341285aeb58cSDavid Spinadel 
341385aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
341485aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
341585aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
3416e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
3417e705c121SKalle Valo 				break;
3418e705c121SKalle Valo 			}
3419c56e00a3SJohannes Berg 
3420c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
3421c56e00a3SJohannes Berg 				for (i = 0;
3422c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
3423c56e00a3SJohannes Berg 				     i++) {
3424c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
3425c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
3426c56e00a3SJohannes Berg 						break;
3427c56e00a3SJohannes Berg 					}
3428c56e00a3SJohannes Berg 				}
3429c56e00a3SJohannes Berg 
3430c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
3431c56e00a3SJohannes Berg 					ret = -ENOSPC;
3432c56e00a3SJohannes Berg 
3433c56e00a3SJohannes Berg 				break;
3434c56e00a3SJohannes Berg 			}
343585aeb58cSDavid Spinadel 		}
3436e705c121SKalle Valo 
3437e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
3438e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
3439e705c121SKalle Valo 		 */
3440e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3441e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
3442e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
3443e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
3444e705c121SKalle Valo 			ret = 0;
3445e705c121SKalle Valo 			break;
3446e705c121SKalle Valo 		}
3447e705c121SKalle Valo 
3448f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3449f5e28eacSJohannes Berg 		    sta && iwl_mvm_has_new_rx_api(mvm) &&
3450f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3451f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
34522a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
34532a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3454f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
3455f5e28eacSJohannes Berg 			int tid, q;
3456f5e28eacSJohannes Berg 
3457f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3458f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3459acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
3460acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
3461f5e28eacSJohannes Berg 					 GFP_KERNEL);
3462f5e28eacSJohannes Berg 			if (!ptk_pn) {
3463f5e28eacSJohannes Berg 				ret = -ENOMEM;
3464f5e28eacSJohannes Berg 				break;
3465f5e28eacSJohannes Berg 			}
3466f5e28eacSJohannes Berg 
3467f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3468f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
3469f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
3470f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
3471f5e28eacSJohannes Berg 					       seq.ccmp.pn,
3472f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
3473f5e28eacSJohannes Berg 			}
3474f5e28eacSJohannes Berg 
3475f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3476f5e28eacSJohannes Berg 		}
3477f5e28eacSJohannes Berg 
34784615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
34794615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
34804615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
34814615fd15SEmmanuel Grumbach 		else
34824615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
34834615fd15SEmmanuel Grumbach 
3484e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
34854615fd15SEmmanuel Grumbach 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
3486e705c121SKalle Valo 		if (ret) {
3487e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
3488475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
3489e705c121SKalle Valo 			/*
3490e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
3491475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
3492475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
3493475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
3494e705c121SKalle Valo 			 */
3495475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
3496475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
3497475c6bdeSJohannes Berg 			else
3498e705c121SKalle Valo 				ret = 0;
3499e705c121SKalle Valo 		}
3500e705c121SKalle Valo 
3501e705c121SKalle Valo 		break;
3502e705c121SKalle Valo 	case DISABLE_KEY:
3503c56e00a3SJohannes Berg 		ret = -ENOENT;
3504c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3505c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
3506c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
3507c56e00a3SJohannes Berg 				ret = 0;
3508c56e00a3SJohannes Berg 			}
3509c56e00a3SJohannes Berg 		}
3510c56e00a3SJohannes Berg 
3511c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
3512c56e00a3SJohannes Berg 		if (ret == 0)
3513c56e00a3SJohannes Berg 			break;
3514c56e00a3SJohannes Berg 
3515e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3516e705c121SKalle Valo 			ret = 0;
3517e705c121SKalle Valo 			break;
3518e705c121SKalle Valo 		}
3519e705c121SKalle Valo 
3520f5e28eacSJohannes Berg 		if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3521f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3522f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
35232a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
35242a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3525f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3526f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
3527f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
3528f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
3529f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3530f5e28eacSJohannes Berg 			if (ptk_pn)
3531f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
3532f5e28eacSJohannes Berg 		}
3533f5e28eacSJohannes Berg 
3534e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3535e705c121SKalle Valo 		ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3536e705c121SKalle Valo 		break;
3537e705c121SKalle Valo 	default:
3538e705c121SKalle Valo 		ret = -EINVAL;
3539e705c121SKalle Valo 	}
3540e705c121SKalle Valo 
35416569e7d3SJohannes Berg 	return ret;
35426569e7d3SJohannes Berg }
35436569e7d3SJohannes Berg 
35446569e7d3SJohannes Berg static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
35456569e7d3SJohannes Berg 			       enum set_key_cmd cmd,
35466569e7d3SJohannes Berg 			       struct ieee80211_vif *vif,
35476569e7d3SJohannes Berg 			       struct ieee80211_sta *sta,
35486569e7d3SJohannes Berg 			       struct ieee80211_key_conf *key)
35496569e7d3SJohannes Berg {
35506569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
35516569e7d3SJohannes Berg 	int ret;
35526569e7d3SJohannes Berg 
35536569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
35546569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
3555e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
35566569e7d3SJohannes Berg 
3557e705c121SKalle Valo 	return ret;
3558e705c121SKalle Valo }
3559e705c121SKalle Valo 
3560e705c121SKalle Valo static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3561e705c121SKalle Valo 					struct ieee80211_vif *vif,
3562e705c121SKalle Valo 					struct ieee80211_key_conf *keyconf,
3563e705c121SKalle Valo 					struct ieee80211_sta *sta,
3564e705c121SKalle Valo 					u32 iv32, u16 *phase1key)
3565e705c121SKalle Valo {
3566e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3567e705c121SKalle Valo 
3568e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3569e705c121SKalle Valo 		return;
3570e705c121SKalle Valo 
3571e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3572e705c121SKalle Valo }
3573e705c121SKalle Valo 
3574e705c121SKalle Valo 
3575e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3576e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
3577e705c121SKalle Valo {
3578e705c121SKalle Valo 	struct iwl_mvm *mvm =
3579e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3580e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
3581e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
3582e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
3583e705c121SKalle Valo 
3584e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3585e705c121SKalle Valo 		return true;
3586e705c121SKalle Valo 
3587e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3588e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3589e705c121SKalle Valo 		return true;
3590e705c121SKalle Valo 	}
3591e705c121SKalle Valo 
3592e705c121SKalle Valo 	resp = (void *)pkt->data;
3593e705c121SKalle Valo 
3594e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
3595b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
3596e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
3597e705c121SKalle Valo 
3598e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
3599e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3600e705c121SKalle Valo 		     te_data->uid);
3601e705c121SKalle Valo 
3602e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3603e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3604e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3605e705c121SKalle Valo 
3606e705c121SKalle Valo 	return true;
3607e705c121SKalle Valo }
3608e705c121SKalle Valo 
3609dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3610dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3611dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3612dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3613dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3614e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3615e705c121SKalle Valo 				    struct ieee80211_channel *channel,
3616e705c121SKalle Valo 				    struct ieee80211_vif *vif,
3617e705c121SKalle Valo 				    int duration)
3618e705c121SKalle Valo {
3619afc1e3b4SAvraham Stern 	int res;
3620e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3621e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3622e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
3623e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
3624dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
3625dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
3626dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
3627e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
3628e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3629e705c121SKalle Valo 		.id_and_color =
3630e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3631e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3632e705c121SKalle Valo 	};
363357e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
363457e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
363557e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
363657e861d9SDavid Spinadel 
363757e861d9SDavid Spinadel 	/* Set the channel info data */
363857e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
363957e861d9SDavid Spinadel 			      (channel->band == NL80211_BAND_2GHZ) ?
364057e861d9SDavid Spinadel 			       PHY_BAND_24 : PHY_BAND_5,
364157e861d9SDavid Spinadel 			      PHY_VHT_CHANNEL_MODE20,
364257e861d9SDavid Spinadel 			      0);
364357e861d9SDavid Spinadel 
364457e861d9SDavid Spinadel 	/* Set the time and duration */
3645afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
3646e705c121SKalle Valo 
3647dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
3648dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
3649dc28e12fSMatti Gottlieb 
3650dc28e12fSMatti Gottlieb 	/*
3651dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
3652dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
3653dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
3654dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
3655dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
3656dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
3657dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
3658dc28e12fSMatti Gottlieb 	 */
3659dc28e12fSMatti Gottlieb 	if (vif->bss_conf.assoc) {
3660dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3661dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
3662dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
3663dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3664dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
3665dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
3666dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
3667dc28e12fSMatti Gottlieb 		}
3668dc28e12fSMatti Gottlieb 	}
3669dc28e12fSMatti Gottlieb 
367057e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
367157e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
3672dc28e12fSMatti Gottlieb 
3673dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
3674dc28e12fSMatti Gottlieb 		     "ROC: Requesting to remain on channel %u for %ums (requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3675dc28e12fSMatti Gottlieb 		     channel->hw_value, req_dur, duration, delay,
3676dc28e12fSMatti Gottlieb 		     dtim_interval);
3677e705c121SKalle Valo 	/* Set the node address */
367857e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
3679e705c121SKalle Valo 
3680e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3681e705c121SKalle Valo 
3682e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3683e705c121SKalle Valo 
3684e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3685e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3686e705c121SKalle Valo 		return -EIO;
3687e705c121SKalle Valo 	}
3688e705c121SKalle Valo 
3689e705c121SKalle Valo 	te_data->vif = vif;
3690e705c121SKalle Valo 	te_data->duration = duration;
3691e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
3692e705c121SKalle Valo 
3693e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3694e705c121SKalle Valo 
3695e705c121SKalle Valo 	/*
3696e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
3697e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
3698e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
3699e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
3700e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
3701e705c121SKalle Valo 	 * time another notification (that the time event started)
3702e705c121SKalle Valo 	 * might already be processed unsuccessfully.
3703e705c121SKalle Valo 	 */
3704e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3705e705c121SKalle Valo 				   time_event_response,
3706e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
3707e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
3708e705c121SKalle Valo 
370957e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
3710e705c121SKalle Valo 				   &aux_roc_req);
3711e705c121SKalle Valo 
3712e705c121SKalle Valo 	if (res) {
3713e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3714e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3715e705c121SKalle Valo 		goto out_clear_te;
3716e705c121SKalle Valo 	}
3717e705c121SKalle Valo 
3718e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
3719e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3720e705c121SKalle Valo 	/* should never fail */
3721e705c121SKalle Valo 	WARN_ON_ONCE(res);
3722e705c121SKalle Valo 
3723e705c121SKalle Valo 	if (res) {
3724e705c121SKalle Valo  out_clear_te:
3725e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
3726e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
3727e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3728e705c121SKalle Valo 	}
3729e705c121SKalle Valo 
3730e705c121SKalle Valo 	return res;
3731e705c121SKalle Valo }
3732e705c121SKalle Valo 
3733e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
3734e705c121SKalle Valo 		       struct ieee80211_vif *vif,
3735e705c121SKalle Valo 		       struct ieee80211_channel *channel,
3736e705c121SKalle Valo 		       int duration,
3737e705c121SKalle Valo 		       enum ieee80211_roc_type type)
3738e705c121SKalle Valo {
3739e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3740e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3741e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
3742e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
3743e705c121SKalle Valo 	int ret, i;
3744e705c121SKalle Valo 
3745e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3746e705c121SKalle Valo 			   duration, type);
3747e705c121SKalle Valo 
37489834781cSJohannes Berg 	/*
37499834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
37509834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
37519834781cSJohannes Berg 	 */
3752e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
3753e705c121SKalle Valo 
3754e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3755e705c121SKalle Valo 
3756e705c121SKalle Valo 	switch (vif->type) {
3757e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3758e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
3759e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
3760e705c121SKalle Valo 			/* Use aux roc framework (HS20) */
3761e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3762e705c121SKalle Valo 						       vif, duration);
3763e705c121SKalle Valo 			goto out_unlock;
3764e705c121SKalle Valo 		}
3765e705c121SKalle Valo 		IWL_ERR(mvm, "hotspot not supported\n");
3766e705c121SKalle Valo 		ret = -EINVAL;
3767e705c121SKalle Valo 		goto out_unlock;
3768e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
3769e705c121SKalle Valo 		/* handle below */
3770e705c121SKalle Valo 		break;
3771e705c121SKalle Valo 	default:
3772e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
3773e705c121SKalle Valo 		ret = -EINVAL;
3774e705c121SKalle Valo 		goto out_unlock;
3775e705c121SKalle Valo 	}
3776e705c121SKalle Valo 
3777e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
3778e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
3779e705c121SKalle Valo 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3780e705c121SKalle Valo 			continue;
3781e705c121SKalle Valo 
3782e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3783e705c121SKalle Valo 			/*
3784e705c121SKalle Valo 			 * Unbind the P2P_DEVICE from the current PHY context,
3785e705c121SKalle Valo 			 * and if the PHY context is not used remove it.
3786e705c121SKalle Valo 			 */
3787e705c121SKalle Valo 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
3788e705c121SKalle Valo 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3789e705c121SKalle Valo 				goto out_unlock;
3790e705c121SKalle Valo 
3791e705c121SKalle Valo 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3792e705c121SKalle Valo 
3793e705c121SKalle Valo 			/* Bind the P2P_DEVICE to the current PHY Context */
3794e705c121SKalle Valo 			mvmvif->phy_ctxt = phy_ctxt;
3795e705c121SKalle Valo 
3796e705c121SKalle Valo 			ret = iwl_mvm_binding_add_vif(mvm, vif);
3797e705c121SKalle Valo 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3798e705c121SKalle Valo 				goto out_unlock;
3799e705c121SKalle Valo 
3800e705c121SKalle Valo 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3801e705c121SKalle Valo 			goto schedule_time_event;
3802e705c121SKalle Valo 		}
3803e705c121SKalle Valo 	}
3804e705c121SKalle Valo 
3805e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
3806e705c121SKalle Valo 	if (channel == mvmvif->phy_ctxt->channel)
3807e705c121SKalle Valo 		goto schedule_time_event;
3808e705c121SKalle Valo 
3809e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
3810e705c121SKalle Valo 
3811e705c121SKalle Valo 	/*
3812e705c121SKalle Valo 	 * Change the PHY context configuration as it is currently referenced
3813e705c121SKalle Valo 	 * only by the P2P Device MAC
3814e705c121SKalle Valo 	 */
3815e705c121SKalle Valo 	if (mvmvif->phy_ctxt->ref == 1) {
3816e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3817e705c121SKalle Valo 					       &chandef, 1, 1);
3818e705c121SKalle Valo 		if (ret)
3819e705c121SKalle Valo 			goto out_unlock;
3820e705c121SKalle Valo 	} else {
3821e705c121SKalle Valo 		/*
3822e705c121SKalle Valo 		 * The PHY context is shared with other MACs. Need to remove the
3823e705c121SKalle Valo 		 * P2P Device from the binding, allocate an new PHY context and
3824e705c121SKalle Valo 		 * create a new binding
3825e705c121SKalle Valo 		 */
3826e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3827e705c121SKalle Valo 		if (!phy_ctxt) {
3828e705c121SKalle Valo 			ret = -ENOSPC;
3829e705c121SKalle Valo 			goto out_unlock;
3830e705c121SKalle Valo 		}
3831e705c121SKalle Valo 
3832e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3833e705c121SKalle Valo 					       1, 1);
3834e705c121SKalle Valo 		if (ret) {
3835e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
3836e705c121SKalle Valo 			goto out_unlock;
3837e705c121SKalle Valo 		}
3838e705c121SKalle Valo 
3839e705c121SKalle Valo 		/* Unbind the P2P_DEVICE from the current PHY context */
3840e705c121SKalle Valo 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
3841e705c121SKalle Valo 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3842e705c121SKalle Valo 			goto out_unlock;
3843e705c121SKalle Valo 
3844e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3845e705c121SKalle Valo 
3846e705c121SKalle Valo 		/* Bind the P2P_DEVICE to the new allocated PHY context */
3847e705c121SKalle Valo 		mvmvif->phy_ctxt = phy_ctxt;
3848e705c121SKalle Valo 
3849e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
3850e705c121SKalle Valo 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3851e705c121SKalle Valo 			goto out_unlock;
3852e705c121SKalle Valo 
3853e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3854e705c121SKalle Valo 	}
3855e705c121SKalle Valo 
3856e705c121SKalle Valo schedule_time_event:
3857e705c121SKalle Valo 	/* Schedule the time events */
3858e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
3859e705c121SKalle Valo 
3860e705c121SKalle Valo out_unlock:
3861e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3862e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3863e705c121SKalle Valo 	return ret;
3864e705c121SKalle Valo }
3865e705c121SKalle Valo 
38665db4c4b9SEmmanuel Grumbach static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
38675db4c4b9SEmmanuel Grumbach 			      struct ieee80211_vif *vif)
3868e705c121SKalle Valo {
3869e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3870e705c121SKalle Valo 
3871e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
3872e705c121SKalle Valo 
3873e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3874*fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
3875e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3876e705c121SKalle Valo 
3877e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3878e705c121SKalle Valo 	return 0;
3879e705c121SKalle Valo }
3880e705c121SKalle Valo 
3881b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
3882b73f9a4aSJohannes Berg 	bool responder;
3883b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
3884b73f9a4aSJohannes Berg };
3885b73f9a4aSJohannes Berg 
3886b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
3887b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
3888b73f9a4aSJohannes Berg {
3889b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
3890b73f9a4aSJohannes Berg 
3891b73f9a4aSJohannes Berg 	if (rcu_access_pointer(vif->chanctx_conf) == data->ctx &&
3892b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
3893b73f9a4aSJohannes Berg 		data->responder = true;
3894b73f9a4aSJohannes Berg }
3895b73f9a4aSJohannes Berg 
3896b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
3897b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
3898b73f9a4aSJohannes Berg {
3899b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
3900b73f9a4aSJohannes Berg 		.responder = false,
3901b73f9a4aSJohannes Berg 		.ctx = ctx,
3902b73f9a4aSJohannes Berg 	};
3903b73f9a4aSJohannes Berg 
3904b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
3905b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
3906b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
3907b73f9a4aSJohannes Berg 					&data);
3908b73f9a4aSJohannes Berg 	return data.responder;
3909b73f9a4aSJohannes Berg }
3910b73f9a4aSJohannes Berg 
3911e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
3912e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
3913e705c121SKalle Valo {
3914e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3915e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
3916b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
3917b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
3918e705c121SKalle Valo 	int ret;
3919e705c121SKalle Valo 
3920e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3921e705c121SKalle Valo 
3922e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
3923e705c121SKalle Valo 
3924e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3925e705c121SKalle Valo 	if (!phy_ctxt) {
3926e705c121SKalle Valo 		ret = -ENOSPC;
3927e705c121SKalle Valo 		goto out;
3928e705c121SKalle Valo 	}
3929e705c121SKalle Valo 
3930b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
3931e705c121SKalle Valo 				       ctx->rx_chains_static,
3932e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
3933e705c121SKalle Valo 	if (ret) {
3934e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
3935e705c121SKalle Valo 		goto out;
3936e705c121SKalle Valo 	}
3937e705c121SKalle Valo 
3938e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
3939e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
3940e705c121SKalle Valo out:
3941e705c121SKalle Valo 	return ret;
3942e705c121SKalle Valo }
3943e705c121SKalle Valo 
3944e705c121SKalle Valo static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
3945e705c121SKalle Valo 			       struct ieee80211_chanctx_conf *ctx)
3946e705c121SKalle Valo {
3947e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3948e705c121SKalle Valo 	int ret;
3949e705c121SKalle Valo 
3950e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3951e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
3952e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3953e705c121SKalle Valo 
3954e705c121SKalle Valo 	return ret;
3955e705c121SKalle Valo }
3956e705c121SKalle Valo 
3957e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
3958e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
3959e705c121SKalle Valo {
3960e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3961e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3962e705c121SKalle Valo 
3963e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3964e705c121SKalle Valo 
3965e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
3966e705c121SKalle Valo }
3967e705c121SKalle Valo 
3968e705c121SKalle Valo static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
3969e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx)
3970e705c121SKalle Valo {
3971e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3972e705c121SKalle Valo 
3973e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3974e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
3975e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3976e705c121SKalle Valo }
3977e705c121SKalle Valo 
3978e705c121SKalle Valo static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
3979e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx,
3980e705c121SKalle Valo 				   u32 changed)
3981e705c121SKalle Valo {
3982e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3983e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3984e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3985b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
3986b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
3987e705c121SKalle Valo 
3988e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
3989e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
3990e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
3991e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
3992e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
3993e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
3994e705c121SKalle Valo 		      phy_ctxt->ref, changed))
3995e705c121SKalle Valo 		return;
3996e705c121SKalle Valo 
3997e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
39987a20bcceSEmmanuel Grumbach 
39997a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
40007a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4001b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
40027a20bcceSEmmanuel Grumbach 			goto out_unlock;
40037a20bcceSEmmanuel Grumbach 
40047a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
40057a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4006b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
40077a20bcceSEmmanuel Grumbach 			goto out_unlock;
40087a20bcceSEmmanuel Grumbach 	}
40097a20bcceSEmmanuel Grumbach 
4010e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4011b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4012e705c121SKalle Valo 				 ctx->rx_chains_static,
4013e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
40147a20bcceSEmmanuel Grumbach 
40157a20bcceSEmmanuel Grumbach out_unlock:
4016e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4017e705c121SKalle Valo }
4018e705c121SKalle Valo 
4019e705c121SKalle Valo static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4020e705c121SKalle Valo 					struct ieee80211_vif *vif,
4021e705c121SKalle Valo 					struct ieee80211_chanctx_conf *ctx,
4022e705c121SKalle Valo 					bool switching_chanctx)
4023e705c121SKalle Valo {
4024e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4025e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4026e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4027e705c121SKalle Valo 	int ret;
4028e705c121SKalle Valo 
4029e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4030e705c121SKalle Valo 
4031e705c121SKalle Valo 	mvmvif->phy_ctxt = phy_ctxt;
4032e705c121SKalle Valo 
4033e705c121SKalle Valo 	switch (vif->type) {
4034e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4035e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4036e705c121SKalle Valo 		if (switching_chanctx) {
4037e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4038e705c121SKalle Valo 			break;
4039e705c121SKalle Valo 		}
4040244726ecSLuca Coelho 		/* fall through */
4041e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4042e705c121SKalle Valo 		/*
4043e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4044e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4045e705c121SKalle Valo 		 */
4046e705c121SKalle Valo 		ret = 0;
4047e705c121SKalle Valo 		goto out;
4048e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
404919125cb0SAndrei Otcheretianski 		mvmvif->csa_bcn_pending = false;
4050e705c121SKalle Valo 		break;
4051e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4052e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4053e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4054e705c121SKalle Valo 		break;
4055e705c121SKalle Valo 	default:
4056e705c121SKalle Valo 		ret = -EINVAL;
4057e705c121SKalle Valo 		goto out;
4058e705c121SKalle Valo 	}
4059e705c121SKalle Valo 
4060e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4061e705c121SKalle Valo 	if (ret)
4062e705c121SKalle Valo 		goto out;
4063e705c121SKalle Valo 
4064e705c121SKalle Valo 	/*
4065e705c121SKalle Valo 	 * Power state must be updated before quotas,
4066e705c121SKalle Valo 	 * otherwise fw will complain.
4067e705c121SKalle Valo 	 */
4068e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4069e705c121SKalle Valo 
4070e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4071e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4072e705c121SKalle Valo 	 * will handle quota settings.
4073e705c121SKalle Valo 	 */
4074e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4075e705c121SKalle Valo 		mvmvif->monitor_active = true;
4076e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4077e705c121SKalle Valo 		if (ret)
4078e705c121SKalle Valo 			goto out_remove_binding;
40790e39eb03SChaya Rachel Ivgi 
40800e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
40810e39eb03SChaya Rachel Ivgi 		if (ret)
40820e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
40830e39eb03SChaya Rachel Ivgi 
4084e705c121SKalle Valo 	}
4085e705c121SKalle Valo 
4086e705c121SKalle Valo 	/* Handle binding during CSA */
4087e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4088e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4089e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4090e705c121SKalle Valo 	}
4091e705c121SKalle Valo 
4092e705c121SKalle Valo 	if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
409374a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
409474a10252SSara Sharon 
409574a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
409674a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
409719125cb0SAndrei Otcheretianski 			u32 duration = 3 * vif->bss_conf.beacon_int;
4098e705c121SKalle Valo 
4099e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4100e705c121SKalle Valo 			 * beacon on the new channel.
4101e705c121SKalle Valo 			 */
4102e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4103e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4104e705c121SKalle Valo 						true);
410574a10252SSara Sharon 		}
4106e705c121SKalle Valo 
4107e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4108e705c121SKalle Valo 	}
4109e705c121SKalle Valo 
4110e705c121SKalle Valo 	goto out;
4111e705c121SKalle Valo 
4112e705c121SKalle Valo out_remove_binding:
4113e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4114e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4115e705c121SKalle Valo out:
4116e705c121SKalle Valo 	if (ret)
4117e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
4118e705c121SKalle Valo 	return ret;
4119e705c121SKalle Valo }
4120e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4121e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4122e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4123e705c121SKalle Valo {
4124e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4125e705c121SKalle Valo 	int ret;
4126e705c121SKalle Valo 
4127e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4128e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
4129e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4130e705c121SKalle Valo 
4131e705c121SKalle Valo 	return ret;
4132e705c121SKalle Valo }
4133e705c121SKalle Valo 
4134e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4135e705c121SKalle Valo 					   struct ieee80211_vif *vif,
4136e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
4137e705c121SKalle Valo 					   bool switching_chanctx)
4138e705c121SKalle Valo {
4139e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4140e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
4141e705c121SKalle Valo 
4142e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4143e705c121SKalle Valo 
4144e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4145e705c121SKalle Valo 
4146e705c121SKalle Valo 	switch (vif->type) {
4147e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4148e705c121SKalle Valo 		goto out;
4149e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4150e705c121SKalle Valo 		mvmvif->monitor_active = false;
4151e705c121SKalle Valo 		mvmvif->ps_disabled = false;
41520e39eb03SChaya Rachel Ivgi 		iwl_mvm_rm_snif_sta(mvm, vif);
4153e705c121SKalle Valo 		break;
4154e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4155e705c121SKalle Valo 		/* This part is triggered only during CSA */
4156e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
4157e705c121SKalle Valo 			goto out;
4158e705c121SKalle Valo 
4159e705c121SKalle Valo 		mvmvif->csa_countdown = false;
4160e705c121SKalle Valo 
4161e705c121SKalle Valo 		/* Set CS bit on all the stations */
4162e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4163e705c121SKalle Valo 
4164e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
4165e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4166e705c121SKalle Valo 
4167e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
4168e705c121SKalle Valo 		break;
4169e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4170e705c121SKalle Valo 		if (!switching_chanctx)
4171e705c121SKalle Valo 			break;
4172e705c121SKalle Valo 
4173e705c121SKalle Valo 		disabled_vif = vif;
4174e705c121SKalle Valo 
417574a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
417674a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4177e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
4178e705c121SKalle Valo 		break;
4179e705c121SKalle Valo 	default:
4180e705c121SKalle Valo 		break;
4181e705c121SKalle Valo 	}
4182e705c121SKalle Valo 
4183e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
4184e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4185e705c121SKalle Valo 
4186e705c121SKalle Valo out:
4187e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
4188e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4189e705c121SKalle Valo }
4190e705c121SKalle Valo 
4191e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4192e705c121SKalle Valo 					 struct ieee80211_vif *vif,
4193e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
4194e705c121SKalle Valo {
4195e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4196e705c121SKalle Valo 
4197e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4198e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
4199e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4200e705c121SKalle Valo }
4201e705c121SKalle Valo 
4202e705c121SKalle Valo static int
4203e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4204e705c121SKalle Valo 				struct ieee80211_vif_chanctx_switch *vifs)
4205e705c121SKalle Valo {
4206e705c121SKalle Valo 	int ret;
4207e705c121SKalle Valo 
4208e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4209e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4210e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4211e705c121SKalle Valo 
4212e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4213e705c121SKalle Valo 	if (ret) {
4214e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4215e705c121SKalle Valo 		goto out_reassign;
4216e705c121SKalle Valo 	}
4217e705c121SKalle Valo 
4218e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4219e705c121SKalle Valo 					   true);
4220e705c121SKalle Valo 	if (ret) {
4221e705c121SKalle Valo 		IWL_ERR(mvm,
4222e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4223e705c121SKalle Valo 		goto out_remove;
4224e705c121SKalle Valo 	}
4225e705c121SKalle Valo 
4226e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
4227e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
4228e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
4229e705c121SKalle Valo 
4230e705c121SKalle Valo 	goto out;
4231e705c121SKalle Valo 
4232e705c121SKalle Valo out_remove:
4233e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4234e705c121SKalle Valo 
4235e705c121SKalle Valo out_reassign:
4236e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
4237e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4238e705c121SKalle Valo 		goto out_restart;
4239e705c121SKalle Valo 	}
4240e705c121SKalle Valo 
4241e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4242e705c121SKalle Valo 					 true)) {
4243e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4244e705c121SKalle Valo 		goto out_restart;
4245e705c121SKalle Valo 	}
4246e705c121SKalle Valo 
4247e705c121SKalle Valo 	goto out;
4248e705c121SKalle Valo 
4249e705c121SKalle Valo out_restart:
4250e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4251e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4252e705c121SKalle Valo 
4253e705c121SKalle Valo out:
4254e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4255e705c121SKalle Valo 
4256e705c121SKalle Valo 	return ret;
4257e705c121SKalle Valo }
4258e705c121SKalle Valo 
4259e705c121SKalle Valo static int
4260e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4261e705c121SKalle Valo 				    struct ieee80211_vif_chanctx_switch *vifs)
4262e705c121SKalle Valo {
4263e705c121SKalle Valo 	int ret;
4264e705c121SKalle Valo 
4265e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4266e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4267e705c121SKalle Valo 
4268e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4269e705c121SKalle Valo 					   true);
4270e705c121SKalle Valo 	if (ret) {
4271e705c121SKalle Valo 		IWL_ERR(mvm,
4272e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4273e705c121SKalle Valo 		goto out_reassign;
4274e705c121SKalle Valo 	}
4275e705c121SKalle Valo 
4276e705c121SKalle Valo 	goto out;
4277e705c121SKalle Valo 
4278e705c121SKalle Valo out_reassign:
4279e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4280e705c121SKalle Valo 					 true)) {
4281e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4282e705c121SKalle Valo 		goto out_restart;
4283e705c121SKalle Valo 	}
4284e705c121SKalle Valo 
4285e705c121SKalle Valo 	goto out;
4286e705c121SKalle Valo 
4287e705c121SKalle Valo out_restart:
4288e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4289e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4290e705c121SKalle Valo 
4291e705c121SKalle Valo out:
4292e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4293e705c121SKalle Valo 
4294e705c121SKalle Valo 	return ret;
4295e705c121SKalle Valo }
4296e705c121SKalle Valo 
4297e705c121SKalle Valo static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4298e705c121SKalle Valo 				      struct ieee80211_vif_chanctx_switch *vifs,
4299e705c121SKalle Valo 				      int n_vifs,
4300e705c121SKalle Valo 				      enum ieee80211_chanctx_switch_mode mode)
4301e705c121SKalle Valo {
4302e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4303e705c121SKalle Valo 	int ret;
4304e705c121SKalle Valo 
4305e705c121SKalle Valo 	/* we only support a single-vif right now */
4306e705c121SKalle Valo 	if (n_vifs > 1)
4307e705c121SKalle Valo 		return -EOPNOTSUPP;
4308e705c121SKalle Valo 
4309e705c121SKalle Valo 	switch (mode) {
4310e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
4311e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4312e705c121SKalle Valo 		break;
4313e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
4314e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
4315e705c121SKalle Valo 		break;
4316e705c121SKalle Valo 	default:
4317e705c121SKalle Valo 		ret = -EOPNOTSUPP;
4318e705c121SKalle Valo 		break;
4319e705c121SKalle Valo 	}
4320e705c121SKalle Valo 
4321e705c121SKalle Valo 	return ret;
4322e705c121SKalle Valo }
4323e705c121SKalle Valo 
43242f0282dbSJohannes Berg static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
43252f0282dbSJohannes Berg {
43262f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
43272f0282dbSJohannes Berg 
43282f0282dbSJohannes Berg 	return mvm->ibss_manager;
43292f0282dbSJohannes Berg }
43302f0282dbSJohannes Berg 
4331e705c121SKalle Valo static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4332e705c121SKalle Valo 			   struct ieee80211_sta *sta,
4333e705c121SKalle Valo 			   bool set)
4334e705c121SKalle Valo {
4335e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4336e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4337e705c121SKalle Valo 
4338e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
4339e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
4340e705c121SKalle Valo 		return -EINVAL;
4341e705c121SKalle Valo 	}
4342e705c121SKalle Valo 
4343e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4344e705c121SKalle Valo }
4345e705c121SKalle Valo 
4346e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
4347e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4348e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4349e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
4350e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
4351e705c121SKalle Valo };
4352e705c121SKalle Valo 
4353e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4354e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4355e705c121SKalle Valo 				      void *data, int len)
4356e705c121SKalle Valo {
4357e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4358e705c121SKalle Valo 	int err;
4359e705c121SKalle Valo 	u32 noa_duration;
4360e705c121SKalle Valo 
43618cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
43628cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
4363e705c121SKalle Valo 	if (err)
4364e705c121SKalle Valo 		return err;
4365e705c121SKalle Valo 
4366e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
4367e705c121SKalle Valo 		return -EINVAL;
4368e705c121SKalle Valo 
4369e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4370e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
4371e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4372e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
4373e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4374e705c121SKalle Valo 			return -EINVAL;
4375e705c121SKalle Valo 
4376e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4377e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
4378e705c121SKalle Valo 			return -EINVAL;
4379e705c121SKalle Valo 
4380e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
4381e705c121SKalle Valo 		mvm->noa_vif = vif;
4382e705c121SKalle Valo 
438322b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
4384e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4385e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
4386e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4387e705c121SKalle Valo 		    !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
4388e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4389e705c121SKalle Valo 			return -EINVAL;
4390e705c121SKalle Valo 
4391e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
4392e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4393e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4394e705c121SKalle Valo 	}
4395e705c121SKalle Valo 
4396e705c121SKalle Valo 	return -EOPNOTSUPP;
4397e705c121SKalle Valo }
4398e705c121SKalle Valo 
4399e705c121SKalle Valo static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4400e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4401e705c121SKalle Valo 				    void *data, int len)
4402e705c121SKalle Valo {
4403e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4404e705c121SKalle Valo 	int err;
4405e705c121SKalle Valo 
4406e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4407e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4408e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4409e705c121SKalle Valo 
4410e705c121SKalle Valo 	return err;
4411e705c121SKalle Valo }
4412e705c121SKalle Valo #endif
4413e705c121SKalle Valo 
4414e705c121SKalle Valo static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4415e705c121SKalle Valo 				   struct ieee80211_vif *vif,
4416e705c121SKalle Valo 				   struct ieee80211_channel_switch *chsw)
4417e705c121SKalle Valo {
4418e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
4419e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
4420e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
4421e705c121SKalle Valo 	 * (which is when the absence time event starts).
4422e705c121SKalle Valo 	 */
4423e705c121SKalle Valo 
4424e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4425e705c121SKalle Valo 			   "dummy channel switch op\n");
4426e705c121SKalle Valo }
4427e705c121SKalle Valo 
442874a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
442974a10252SSara Sharon 				       struct ieee80211_vif *vif,
443074a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
443174a10252SSara Sharon {
443274a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
443374a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
443474a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
443574a10252SSara Sharon 							  mvmvif->color)),
443674a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
443774a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
443874a10252SSara Sharon 		.cs_count = chsw->count,
443977738865SSara Sharon 		.cs_mode = chsw->block_tx,
444074a10252SSara Sharon 	};
444174a10252SSara Sharon 
444274a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
444374a10252SSara Sharon 
44449cfcf71cSSara Sharon 	if (chsw->delay)
44459cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
44469cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
44479cfcf71cSSara Sharon 
444874a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
444974a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
445074a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
445174a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
445274a10252SSara Sharon }
445374a10252SSara Sharon 
44540202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
44550202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
44560202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
44570202bcf0SEmmanuel Grumbach {
44580202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
44590202bcf0SEmmanuel Grumbach 	u32 apply_time;
44600202bcf0SEmmanuel Grumbach 
44610202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
44620202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
44630202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
44640202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
44650202bcf0SEmmanuel Grumbach 	 * loss).
44660202bcf0SEmmanuel Grumbach 	 */
44670202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
44680202bcf0SEmmanuel Grumbach 		apply_time = 0;
44690202bcf0SEmmanuel Grumbach 	else
44700202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
44710202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
44720202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
44730202bcf0SEmmanuel Grumbach 
44740202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
44750202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
44760202bcf0SEmmanuel Grumbach 
44770202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
44780202bcf0SEmmanuel Grumbach 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
44790202bcf0SEmmanuel Grumbach 
44800202bcf0SEmmanuel Grumbach 		if (ret)
44810202bcf0SEmmanuel Grumbach 			return ret;
44820202bcf0SEmmanuel Grumbach 	}
44830202bcf0SEmmanuel Grumbach 
44840202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
44850202bcf0SEmmanuel Grumbach 				    apply_time);
44860202bcf0SEmmanuel Grumbach 
44870202bcf0SEmmanuel Grumbach 	return 0;
44880202bcf0SEmmanuel Grumbach }
44890202bcf0SEmmanuel Grumbach 
4490f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
4491e705c121SKalle Valo static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4492e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4493e705c121SKalle Valo 				      struct ieee80211_channel_switch *chsw)
4494e705c121SKalle Valo {
4495e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4496e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
4497e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4498e705c121SKalle Valo 	int ret;
4499e705c121SKalle Valo 
4500e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4501e705c121SKalle Valo 
4502e705c121SKalle Valo 	mvmvif->csa_failed = false;
4503e705c121SKalle Valo 
4504e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
4505e705c121SKalle Valo 			   chsw->chandef.center_freq1);
4506e705c121SKalle Valo 
45077174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
45087174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
45097174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
4510e705c121SKalle Valo 
4511e705c121SKalle Valo 	switch (vif->type) {
4512e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4513e705c121SKalle Valo 		csa_vif =
4514e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
4515e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
4516e705c121SKalle Valo 		if (WARN_ONCE(csa_vif && csa_vif->csa_active,
4517e705c121SKalle Valo 			      "Another CSA is already in progress")) {
4518e705c121SKalle Valo 			ret = -EBUSY;
4519e705c121SKalle Valo 			goto out_unlock;
4520e705c121SKalle Valo 		}
4521e705c121SKalle Valo 
4522d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
4523d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
4524d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
4525d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
4526d3a108a4SAndrei Otcheretianski 			goto out_unlock;
4527d3a108a4SAndrei Otcheretianski 		}
4528d3a108a4SAndrei Otcheretianski 
4529e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
4530e705c121SKalle Valo 
4531e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
4532e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
4533e705c121SKalle Valo 			ret = -EBUSY;
4534e705c121SKalle Valo 			goto out_unlock;
4535e705c121SKalle Valo 		}
4536e705c121SKalle Valo 
4537d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
4538d3a108a4SAndrei Otcheretianski 
4539e705c121SKalle Valo 		break;
4540e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4541f6780614SSara Sharon 		if (chsw->block_tx) {
4542f6780614SSara Sharon 			/*
4543f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
4544f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
4545f6780614SSara Sharon 			 */
4546f6780614SSara Sharon 			if (!chsw->count ||
4547f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
4548f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
4549f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
4550f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
4551f6780614SSara Sharon 		}
4552e705c121SKalle Valo 
45530202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
45540202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
45550202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
4556e705c121SKalle Valo 			if (ret)
4557e705c121SKalle Valo 				goto out_unlock;
45580202bcf0SEmmanuel Grumbach 		} else {
455974a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
45600202bcf0SEmmanuel Grumbach 		}
456181b4e44eSSara Sharon 
456281b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
456381b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
4564e705c121SKalle Valo 		break;
4565e705c121SKalle Valo 	default:
4566e705c121SKalle Valo 		break;
4567e705c121SKalle Valo 	}
4568e705c121SKalle Valo 
4569e705c121SKalle Valo 	mvmvif->ps_disabled = true;
4570e705c121SKalle Valo 
4571e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
4572e705c121SKalle Valo 	if (ret)
4573e705c121SKalle Valo 		goto out_unlock;
4574e705c121SKalle Valo 
4575e705c121SKalle Valo 	/* we won't be on this channel any longer */
4576e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
4577e705c121SKalle Valo 
4578e705c121SKalle Valo out_unlock:
4579e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4580e705c121SKalle Valo 
4581e705c121SKalle Valo 	return ret;
4582e705c121SKalle Valo }
4583e705c121SKalle Valo 
4584c37763d2SSara Sharon static void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
4585c37763d2SSara Sharon 					     struct ieee80211_vif *vif,
4586c37763d2SSara Sharon 					     struct ieee80211_channel_switch *chsw)
4587e705c121SKalle Valo {
4588e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4589c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4590c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
4591c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4592c37763d2SSara Sharon 							  mvmvif->color)),
4593c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
4594c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
4595c37763d2SSara Sharon 		.cs_count = chsw->count,
459677738865SSara Sharon 		.cs_mode = chsw->block_tx,
4597c37763d2SSara Sharon 	};
4598e705c121SKalle Valo 
4599c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
4600c37763d2SSara Sharon 		return;
4601e705c121SKalle Valo 
460281b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
460381b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
460481b4e44eSSara Sharon 			/* Second time, give up on this AP*/
460581b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
460681b4e44eSSara Sharon 			ieee80211_chswitch_done(vif, false);
460781b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
460881b4e44eSSara Sharon 			return;
4609e705c121SKalle Valo 		}
461081b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
4611e705c121SKalle Valo 	}
461281b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
4613e705c121SKalle Valo 
4614c37763d2SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d\n", mvmvif->id);
4615e705c121SKalle Valo 
4616c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
4617c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
4618c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
4619c37763d2SSara Sharon 				     CMD_ASYNC, sizeof(cmd), &cmd));
4620e705c121SKalle Valo }
4621e705c121SKalle Valo 
46226110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
46236110d9e5SDavid Spinadel {
4624435d0827SSara Sharon 	int i;
4625435d0827SSara Sharon 
462606195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
4627309c4848SLuca Coelho 		if (drop) {
4628309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
46296110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
4630435d0827SSara Sharon 				iwl_mvm_flushable_queues(mvm) & queues, 0);
4631309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
4632309c4848SLuca Coelho 		} else {
4633435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
4634309c4848SLuca Coelho 		}
4635435d0827SSara Sharon 		return;
4636435d0827SSara Sharon 	}
46376110d9e5SDavid Spinadel 
46386110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
46396110d9e5SDavid Spinadel 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4640435d0827SSara Sharon 		struct ieee80211_sta *sta;
4641435d0827SSara Sharon 
4642435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
46436110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
46446110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
46456110d9e5SDavid Spinadel 			continue;
46466110d9e5SDavid Spinadel 
464706195639SSara Sharon 		if (drop)
464806195639SSara Sharon 			iwl_mvm_flush_sta_tids(mvm, i, 0xFF, 0);
464906195639SSara Sharon 		else
46506110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
46516110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
46526110d9e5SDavid Spinadel 	}
46536110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
46546110d9e5SDavid Spinadel }
46556110d9e5SDavid Spinadel 
4656e705c121SKalle Valo static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4657e705c121SKalle Valo 			      struct ieee80211_vif *vif, u32 queues, bool drop)
4658e705c121SKalle Valo {
4659e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4660e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
4661e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4662e705c121SKalle Valo 	struct ieee80211_sta *sta;
4663e705c121SKalle Valo 	int i;
4664e705c121SKalle Valo 	u32 msk = 0;
4665e705c121SKalle Valo 
46666110d9e5SDavid Spinadel 	if (!vif) {
46676110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
46686110d9e5SDavid Spinadel 		return;
46696110d9e5SDavid Spinadel 	}
46706110d9e5SDavid Spinadel 
46716110d9e5SDavid Spinadel 	if (vif->type != NL80211_IFTYPE_STATION)
4672e705c121SKalle Valo 		return;
4673e705c121SKalle Valo 
467424afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
467524afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
467624afba76SLiad Kaufman 
4677e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4678e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
4679e705c121SKalle Valo 
4680e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
46810ae98812SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4682e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4683e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
4684e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
4685e705c121SKalle Valo 			continue;
4686e705c121SKalle Valo 
4687e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4688e705c121SKalle Valo 		if (mvmsta->vif != vif)
4689e705c121SKalle Valo 			continue;
4690e705c121SKalle Valo 
4691e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
4692e705c121SKalle Valo 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4693e705c121SKalle Valo 
4694d49394a1SSara Sharon 		if (drop) {
4695d49394a1SSara Sharon 			if (iwl_mvm_flush_sta(mvm, mvmsta, false, 0))
4696d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
4697d49394a1SSara Sharon 		} else {
4698e705c121SKalle Valo 			msk |= mvmsta->tfd_queue_msk;
4699d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
4700d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
4701e705c121SKalle Valo 		}
4702d49394a1SSara Sharon 	}
4703e705c121SKalle Valo 
4704e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4705e705c121SKalle Valo 
4706e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
4707e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
4708e705c121SKalle Valo 	 */
4709d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
4710a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
4711e705c121SKalle Valo }
4712e705c121SKalle Valo 
4713e705c121SKalle Valo static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4714e705c121SKalle Valo 				  struct survey_info *survey)
4715e705c121SKalle Valo {
4716e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4717e705c121SKalle Valo 	int ret;
4718e705c121SKalle Valo 
4719e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
4720e705c121SKalle Valo 
4721e705c121SKalle Valo 	/* only support global statistics right now */
4722e705c121SKalle Valo 	if (idx != 0)
4723e705c121SKalle Valo 		return -ENOENT;
4724e705c121SKalle Valo 
4725280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4726e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4727e705c121SKalle Valo 		return -ENOENT;
4728e705c121SKalle Valo 
4729e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4730e705c121SKalle Valo 
4731aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
4732e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
4733e705c121SKalle Valo 		if (ret)
4734e705c121SKalle Valo 			goto out;
4735e705c121SKalle Valo 	}
4736e705c121SKalle Valo 
4737e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
4738e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
4739e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
4740e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
4741e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
4742e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
4743e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
4744e705c121SKalle Valo 
4745e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
4746e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
4747e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
4748e705c121SKalle Valo 
4749e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
4750e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
4751e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
4752e705c121SKalle Valo 
4753e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4754e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
4755e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
4756e705c121SKalle Valo 
4757e705c121SKalle Valo 	ret = 0;
4758e705c121SKalle Valo  out:
4759e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4760e705c121SKalle Valo 	return ret;
4761e705c121SKalle Valo }
4762e705c121SKalle Valo 
4763e705c121SKalle Valo static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
4764e705c121SKalle Valo 				       struct ieee80211_vif *vif,
4765e705c121SKalle Valo 				       struct ieee80211_sta *sta,
4766e705c121SKalle Valo 				       struct station_info *sinfo)
4767e705c121SKalle Valo {
4768e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4769e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4770e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4771e705c121SKalle Valo 
4772988b5968SSara Sharon 	if (mvmsta->avg_energy) {
4773988b5968SSara Sharon 		sinfo->signal_avg = mvmsta->avg_energy;
477422d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
4775988b5968SSara Sharon 	}
4776988b5968SSara Sharon 
4777e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
4778e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
4779e705c121SKalle Valo 		return;
4780e705c121SKalle Valo 
4781e705c121SKalle Valo 	if (!vif->bss_conf.assoc)
4782e705c121SKalle Valo 		return;
4783e705c121SKalle Valo 
4784e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4785e705c121SKalle Valo 
4786e705c121SKalle Valo 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
4787e705c121SKalle Valo 		goto unlock;
4788e705c121SKalle Valo 
4789e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
4790e705c121SKalle Valo 		goto unlock;
4791e705c121SKalle Valo 
4792e705c121SKalle Valo 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
4793e705c121SKalle Valo 			   mvmvif->beacon_stats.accu_num_beacons;
479422d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
4795e705c121SKalle Valo 	if (mvmvif->beacon_stats.avg_signal) {
4796e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
4797e705c121SKalle Valo 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
479822d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
4799e705c121SKalle Valo 	}
4800e705c121SKalle Valo  unlock:
4801e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4802e705c121SKalle Valo }
4803e705c121SKalle Valo 
4804e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
4805e705c121SKalle Valo 					struct ieee80211_vif *vif,
4806e705c121SKalle Valo 					const struct ieee80211_event *event)
4807e705c121SKalle Valo {
48084c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
4809e705c121SKalle Valo 	do {								\
48104c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
4811e705c121SKalle Valo 			break;						\
48127174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
4813e705c121SKalle Valo 	} while (0)
4814e705c121SKalle Valo 
4815e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
4816e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
4817e705c121SKalle Valo 
48186c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
48196c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
48206c042d75SSara Sharon 	if (!trig)
4821e705c121SKalle Valo 		return;
4822e705c121SKalle Valo 
4823e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
4824e705c121SKalle Valo 
4825e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
4826e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
48274c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
4828e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
4829e705c121SKalle Valo 					    event->u.mlme.reason);
4830e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
48314c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
4832e705c121SKalle Valo 					   "ASSOC TIMEOUT");
4833e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
4834e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
48354c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
4836e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
4837e705c121SKalle Valo 					   event->u.mlme.reason);
4838e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
48394c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
4840e705c121SKalle Valo 					   "AUTH TIMEOUT");
4841e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
48424c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
4843e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
4844e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
48454c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
4846e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
4847e705c121SKalle Valo 	}
4848e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
4849e705c121SKalle Valo }
4850e705c121SKalle Valo 
4851e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
4852e705c121SKalle Valo 					  struct ieee80211_vif *vif,
4853e705c121SKalle Valo 					  const struct ieee80211_event *event)
4854e705c121SKalle Valo {
4855e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
4856e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
4857e705c121SKalle Valo 
48586c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
48596c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
48606c042d75SSara Sharon 	if (!trig)
4861e705c121SKalle Valo 		return;
4862e705c121SKalle Valo 
4863e705c121SKalle Valo 	ba_trig = (void *)trig->data;
4864e705c121SKalle Valo 
4865e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
4866e705c121SKalle Valo 		return;
4867e705c121SKalle Valo 
48687174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4869e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
4870e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
4871e705c121SKalle Valo 				event->u.ba.ssn);
4872e705c121SKalle Valo }
4873e705c121SKalle Valo 
4874e705c121SKalle Valo static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
4875e705c121SKalle Valo 				       struct ieee80211_vif *vif,
4876e705c121SKalle Valo 				       const struct ieee80211_event *event)
4877e705c121SKalle Valo {
4878e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4879e705c121SKalle Valo 
4880e705c121SKalle Valo 	switch (event->type) {
4881e705c121SKalle Valo 	case MLME_EVENT:
4882e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
4883e705c121SKalle Valo 		break;
4884e705c121SKalle Valo 	case BAR_RX_EVENT:
4885e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
4886e705c121SKalle Valo 		break;
4887e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
4888528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
4889528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
4890e705c121SKalle Valo 		break;
4891e705c121SKalle Valo 	default:
4892e705c121SKalle Valo 		break;
4893e705c121SKalle Valo 	}
4894e705c121SKalle Valo }
4895e705c121SKalle Valo 
4896d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
4897d0ff5d22SSara Sharon 				     struct iwl_mvm_internal_rxq_notif *notif,
4898d0ff5d22SSara Sharon 				     u32 size)
48990636b938SSara Sharon {
49000636b938SSara Sharon 	u32 qmask = BIT(mvm->trans->num_rx_queues) - 1;
49010636b938SSara Sharon 	int ret;
49020636b938SSara Sharon 
49030636b938SSara Sharon 
4904cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
49050636b938SSara Sharon 		return;
49060636b938SSara Sharon 
4907d0ff5d22SSara Sharon 	notif->cookie = mvm->queue_sync_cookie;
49080636b938SSara Sharon 
4909d0ff5d22SSara Sharon 	if (notif->sync)
4910d0ff5d22SSara Sharon 		atomic_set(&mvm->queue_sync_counter,
4911d0ff5d22SSara Sharon 			   mvm->trans->num_rx_queues);
4912d0ff5d22SSara Sharon 
49133c514bf8SEmmanuel Grumbach 	ret = iwl_mvm_notify_rx_queue(mvm, qmask, (u8 *)notif,
49143c514bf8SEmmanuel Grumbach 				      size, !notif->sync);
49150636b938SSara Sharon 	if (ret) {
49160636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
49170636b938SSara Sharon 		goto out;
49180636b938SSara Sharon 	}
4919d0ff5d22SSara Sharon 
49206ad04359SJohannes Berg 	if (notif->sync) {
49213c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
49223a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
49236ad04359SJohannes Berg 					 atomic_read(&mvm->queue_sync_counter) == 0 ||
49246ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
49250636b938SSara Sharon 					 HZ);
49266ad04359SJohannes Berg 		WARN_ON_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm));
49276ad04359SJohannes Berg 	}
49280636b938SSara Sharon 
49290636b938SSara Sharon out:
49300636b938SSara Sharon 	atomic_set(&mvm->queue_sync_counter, 0);
49310636b938SSara Sharon 	mvm->queue_sync_cookie++;
49320636b938SSara Sharon }
49330636b938SSara Sharon 
49340636b938SSara Sharon static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
49350636b938SSara Sharon {
49360636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4937d0ff5d22SSara Sharon 	struct iwl_mvm_internal_rxq_notif data = {
4938d0ff5d22SSara Sharon 		.type = IWL_MVM_RXQ_EMPTY,
4939d0ff5d22SSara Sharon 		.sync = 1,
4940d0ff5d22SSara Sharon 	};
49410636b938SSara Sharon 
49420636b938SSara Sharon 	mutex_lock(&mvm->mutex);
4943d0ff5d22SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, &data, sizeof(data));
49440636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
49450636b938SSara Sharon }
49460636b938SSara Sharon 
4947b73f9a4aSJohannes Berg static int
4948b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
4949b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
4950b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
4951b73f9a4aSJohannes Berg {
4952b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4953b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4954b73f9a4aSJohannes Berg 
4955b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
4956b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
4957b73f9a4aSJohannes Berg 		return -EINVAL;
4958b73f9a4aSJohannes Berg 
4959b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
4960b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
4961b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
4962b73f9a4aSJohannes Berg 
4963b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
4964b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
4965b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
4966b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
4967b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
4968b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
4969b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
4970b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
4971b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
4972b73f9a4aSJohannes Berg 
4973b73f9a4aSJohannes Berg 	return 0;
4974b73f9a4aSJohannes Berg }
4975b73f9a4aSJohannes Berg 
4976fc36ffdaSJohannes Berg static int iwl_mvm_start_pmsr(struct ieee80211_hw *hw,
4977fc36ffdaSJohannes Berg 			      struct ieee80211_vif *vif,
4978fc36ffdaSJohannes Berg 			      struct cfg80211_pmsr_request *request)
4979fc36ffdaSJohannes Berg {
4980fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4981fc36ffdaSJohannes Berg 	int ret;
4982fc36ffdaSJohannes Berg 
4983fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
4984fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
4985fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
4986fc36ffdaSJohannes Berg 
4987fc36ffdaSJohannes Berg 	return ret;
4988fc36ffdaSJohannes Berg }
4989fc36ffdaSJohannes Berg 
4990fc36ffdaSJohannes Berg static void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw,
4991fc36ffdaSJohannes Berg 			       struct ieee80211_vif *vif,
4992fc36ffdaSJohannes Berg 			       struct cfg80211_pmsr_request *request)
4993fc36ffdaSJohannes Berg {
4994fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4995fc36ffdaSJohannes Berg 
4996fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
4997fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
4998fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
4999fc36ffdaSJohannes Berg }
5000fc36ffdaSJohannes Berg 
5001438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
5002438af969SSara Sharon {
5003438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
5004438af969SSara Sharon 
5005438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
5006438af969SSara Sharon 		return false;
5007438af969SSara Sharon 
5008438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
5009438af969SSara Sharon }
5010438af969SSara Sharon 
5011438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
5012438af969SSara Sharon 				      struct sk_buff *head,
5013438af969SSara Sharon 				      struct sk_buff *skb)
5014438af969SSara Sharon {
5015438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5016438af969SSara Sharon 
5017438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
5018438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
5019438af969SSara Sharon 		return false;
5020438af969SSara Sharon 
5021438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
5022438af969SSara Sharon 		return true;
5023438af969SSara Sharon 
5024438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5025438af969SSara Sharon }
5026438af969SSara Sharon 
5027e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
5028e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
5029cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
5030e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
5031e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
5032e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
5033e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
5034e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
5035e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
5036e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
5037e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
5038e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
5039e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
5040e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
5041e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
5042e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
5043e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
5044e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
5045e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
5046e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
5047e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
5048e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
5049e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
5050e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
5051e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
5052e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
5053e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
5054e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
5055e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
5056e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
5057e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
5058e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
5059e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
5060e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
5061e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
5062e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
5063e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5064e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
5065e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
5066e705c121SKalle Valo 
5067e705c121SKalle Valo 	.start_ap = iwl_mvm_start_ap_ibss,
5068e705c121SKalle Valo 	.stop_ap = iwl_mvm_stop_ap_ibss,
5069e705c121SKalle Valo 	.join_ibss = iwl_mvm_start_ap_ibss,
5070e705c121SKalle Valo 	.leave_ibss = iwl_mvm_stop_ap_ibss,
5071e705c121SKalle Valo 
50722f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
50732f0282dbSJohannes Berg 
5074e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
5075e705c121SKalle Valo 
5076e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
5077e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
5078e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
507979221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
5080c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
5081e705c121SKalle Valo 
5082e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5083e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5084e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5085e705c121SKalle Valo 
5086e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
5087e705c121SKalle Valo 
50880636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
50890636b938SSara Sharon 
5090e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5091e705c121SKalle Valo 
5092e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
5093e705c121SKalle Valo 	/* look at d3.c */
5094e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
5095e705c121SKalle Valo 	.resume = iwl_mvm_resume,
5096e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
5097e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
5098e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
5099e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
5100e705c121SKalle Valo #endif
5101e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
5102e705c121SKalle Valo #endif
5103e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
5104e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
5105b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
5106fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
5107fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
5108b73f9a4aSJohannes Berg 
5109438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
5110177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
5111177a11cfSGregory Greenman 	.sta_add_debugfs = iwl_mvm_sta_add_debugfs,
5112177a11cfSGregory Greenman #endif
5113e705c121SKalle Valo };
5114