xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision c7ab138eb132e197c6f594b11ca8aa87755d2810)
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 
210e705c121SKalle Valo void iwl_mvm_ref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
211e705c121SKalle Valo {
212e705c121SKalle Valo 	if (!iwl_mvm_is_d0i3_supported(mvm))
213e705c121SKalle Valo 		return;
214e705c121SKalle Valo 
215e705c121SKalle Valo 	IWL_DEBUG_RPM(mvm, "Take mvm reference - type %d\n", ref_type);
216e705c121SKalle Valo 	spin_lock_bh(&mvm->refs_lock);
217e705c121SKalle Valo 	mvm->refs[ref_type]++;
218e705c121SKalle Valo 	spin_unlock_bh(&mvm->refs_lock);
219e705c121SKalle Valo 	iwl_trans_ref(mvm->trans);
220e705c121SKalle Valo }
221e705c121SKalle Valo 
222e705c121SKalle Valo void iwl_mvm_unref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
223e705c121SKalle Valo {
224e705c121SKalle Valo 	if (!iwl_mvm_is_d0i3_supported(mvm))
225e705c121SKalle Valo 		return;
226e705c121SKalle Valo 
227e705c121SKalle Valo 	IWL_DEBUG_RPM(mvm, "Leave mvm reference - type %d\n", ref_type);
228e705c121SKalle Valo 	spin_lock_bh(&mvm->refs_lock);
22916c45822SSara Sharon 	if (WARN_ON(!mvm->refs[ref_type])) {
23016c45822SSara Sharon 		spin_unlock_bh(&mvm->refs_lock);
23116c45822SSara Sharon 		return;
23216c45822SSara Sharon 	}
23316c45822SSara Sharon 	mvm->refs[ref_type]--;
234e705c121SKalle Valo 	spin_unlock_bh(&mvm->refs_lock);
235e705c121SKalle Valo 	iwl_trans_unref(mvm->trans);
236e705c121SKalle Valo }
237e705c121SKalle Valo 
238e705c121SKalle Valo static void iwl_mvm_unref_all_except(struct iwl_mvm *mvm,
239e705c121SKalle Valo 				     enum iwl_mvm_ref_type except_ref)
240e705c121SKalle Valo {
241e705c121SKalle Valo 	int i, j;
242e705c121SKalle Valo 
243e705c121SKalle Valo 	if (!iwl_mvm_is_d0i3_supported(mvm))
244e705c121SKalle Valo 		return;
245e705c121SKalle Valo 
246e705c121SKalle Valo 	spin_lock_bh(&mvm->refs_lock);
247e705c121SKalle Valo 	for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
248e705c121SKalle Valo 		if (except_ref == i || !mvm->refs[i])
249e705c121SKalle Valo 			continue;
250e705c121SKalle Valo 
251e705c121SKalle Valo 		IWL_DEBUG_RPM(mvm, "Cleanup: remove mvm ref type %d (%d)\n",
252e705c121SKalle Valo 			      i, mvm->refs[i]);
253e705c121SKalle Valo 		for (j = 0; j < mvm->refs[i]; j++)
254e705c121SKalle Valo 			iwl_trans_unref(mvm->trans);
255e705c121SKalle Valo 		mvm->refs[i] = 0;
256e705c121SKalle Valo 	}
257e705c121SKalle Valo 	spin_unlock_bh(&mvm->refs_lock);
258e705c121SKalle Valo }
259e705c121SKalle Valo 
260e705c121SKalle Valo bool iwl_mvm_ref_taken(struct iwl_mvm *mvm)
261e705c121SKalle Valo {
262e705c121SKalle Valo 	int i;
263e705c121SKalle Valo 	bool taken = false;
264e705c121SKalle Valo 
265e705c121SKalle Valo 	if (!iwl_mvm_is_d0i3_supported(mvm))
266e705c121SKalle Valo 		return true;
267e705c121SKalle Valo 
268e705c121SKalle Valo 	spin_lock_bh(&mvm->refs_lock);
269e705c121SKalle Valo 	for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
270e705c121SKalle Valo 		if (mvm->refs[i]) {
271e705c121SKalle Valo 			taken = true;
272e705c121SKalle Valo 			break;
273e705c121SKalle Valo 		}
274e705c121SKalle Valo 	}
275e705c121SKalle Valo 	spin_unlock_bh(&mvm->refs_lock);
276e705c121SKalle Valo 
277e705c121SKalle Valo 	return taken;
278e705c121SKalle Valo }
279e705c121SKalle Valo 
280e705c121SKalle Valo int iwl_mvm_ref_sync(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
281e705c121SKalle Valo {
282e705c121SKalle Valo 	iwl_mvm_ref(mvm, ref_type);
283e705c121SKalle Valo 
284e705c121SKalle Valo 	if (!wait_event_timeout(mvm->d0i3_exit_waitq,
285e705c121SKalle Valo 				!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status),
286e705c121SKalle Valo 				HZ)) {
287e705c121SKalle Valo 		WARN_ON_ONCE(1);
288e705c121SKalle Valo 		iwl_mvm_unref(mvm, ref_type);
289e705c121SKalle Valo 		return -EIO;
290e705c121SKalle Valo 	}
291e705c121SKalle Valo 
292e705c121SKalle Valo 	return 0;
293e705c121SKalle Valo }
294e705c121SKalle Valo 
295e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
296e705c121SKalle Valo {
297e705c121SKalle Valo 	int i;
298e705c121SKalle Valo 
299e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
300e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
301e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
302e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
303e705c121SKalle Valo 	}
304e705c121SKalle Valo }
305e705c121SKalle Valo 
306e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
307e705c121SKalle Valo 						  const char *alpha2,
308e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
309e705c121SKalle Valo 						  bool *changed)
310e705c121SKalle Valo {
311e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
312e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
313e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
314e705c121SKalle Valo 	struct iwl_mcc_update_resp *resp;
315e705c121SKalle Valo 
316e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
317e705c121SKalle Valo 
318e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
319e705c121SKalle Valo 
320e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
321e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
322e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
323e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
324e705c121SKalle Valo 		goto out;
325e705c121SKalle Valo 	}
326e705c121SKalle Valo 
32782715ac7SEmmanuel Grumbach 	if (changed) {
32882715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
32982715ac7SEmmanuel Grumbach 
33082715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
33182715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
33282715ac7SEmmanuel Grumbach 	}
333e705c121SKalle Valo 
334e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
335e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
336e705c121SKalle Valo 				      resp->channels,
33777e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
33877e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info));
339e705c121SKalle Valo 	/* Store the return source id */
340e705c121SKalle Valo 	src_id = resp->source_id;
341e705c121SKalle Valo 	kfree(resp);
342e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
343e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
344e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
345e705c121SKalle Valo 		goto out;
346e705c121SKalle Valo 	}
347e705c121SKalle Valo 
348e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
349e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
350e705c121SKalle Valo 	mvm->lar_regdom_set = true;
351e705c121SKalle Valo 	mvm->mcc_src = src_id;
352e705c121SKalle Valo 
353e705c121SKalle Valo out:
354e705c121SKalle Valo 	return regd;
355e705c121SKalle Valo }
356e705c121SKalle Valo 
357e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
358e705c121SKalle Valo {
359e705c121SKalle Valo 	bool changed;
360e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
361e705c121SKalle Valo 
362e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
363e705c121SKalle Valo 		return;
364e705c121SKalle Valo 
365e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
366e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
367e705c121SKalle Valo 		/* only update the regulatory core if changed */
368e705c121SKalle Valo 		if (changed)
369e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
370e705c121SKalle Valo 
371e705c121SKalle Valo 		kfree(regd);
372e705c121SKalle Valo 	}
373e705c121SKalle Valo }
374e705c121SKalle Valo 
375e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
376e705c121SKalle Valo 							  bool *changed)
377e705c121SKalle Valo {
378e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
379e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
380e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
381e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
382e705c121SKalle Valo }
383e705c121SKalle Valo 
384e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
385e705c121SKalle Valo {
386e705c121SKalle Valo 	enum iwl_mcc_source used_src;
387e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
388e705c121SKalle Valo 	int ret;
389e705c121SKalle Valo 	bool changed;
390e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
391e705c121SKalle Valo 			rtnl_dereference(mvm->hw->wiphy->regd);
392e705c121SKalle Valo 
393e705c121SKalle Valo 	if (!r)
394e705c121SKalle Valo 		return -ENOENT;
395e705c121SKalle Valo 
396e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
397e705c121SKalle Valo 	used_src = mvm->mcc_src;
398e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
399e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
400e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
401e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
402e705c121SKalle Valo 			kfree(regd);
403e705c121SKalle Valo 	}
404e705c121SKalle Valo 
405e705c121SKalle Valo 	/* Now set our last stored MCC and source */
406e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
407e705c121SKalle Valo 				     &changed);
408e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
409e705c121SKalle Valo 		return -EIO;
410e705c121SKalle Valo 
411e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
412e705c121SKalle Valo 	if (changed)
413e705c121SKalle Valo 		ret = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
414e705c121SKalle Valo 	else
415e705c121SKalle Valo 		ret = 0;
416e705c121SKalle Valo 
417e705c121SKalle Valo 	kfree(regd);
418e705c121SKalle Valo 	return ret;
419e705c121SKalle Valo }
420e705c121SKalle Valo 
4217360f99eSEmmanuel Grumbach const static u8 he_if_types_ext_capa_sta[] = {
4227360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
423918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
4247360f99eSEmmanuel Grumbach 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
4257360f99eSEmmanuel Grumbach 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
4267360f99eSEmmanuel Grumbach };
4277360f99eSEmmanuel Grumbach 
4287360f99eSEmmanuel Grumbach const static struct wiphy_iftype_ext_capab he_iftypes_ext_capa[] = {
4297360f99eSEmmanuel Grumbach 	{
4307360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
4317360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
4327360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
4337360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
4347360f99eSEmmanuel Grumbach 	},
4357360f99eSEmmanuel Grumbach };
4367360f99eSEmmanuel Grumbach 
437e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
438e705c121SKalle Valo {
439e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
440e705c121SKalle Valo 	int num_mac, ret, i;
441e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
442e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
443e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
444e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
445e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
446e705c121SKalle Valo 	};
4473f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
4483f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
4493f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
4503f37c229SIdo Yariv #endif
451e705c121SKalle Valo 
452e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
453e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
454e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
455e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
456e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
457e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
458e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
459e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
460e705c121SKalle Valo 	ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
461e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
462e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
463e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
464e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
465909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
46630433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
467d270e7b8SIlan Peer 	ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
468520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
469cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
470cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
471438af969SSara Sharon 	ieee80211_hw_set(hw, TX_AMSDU);
472438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
473ecaf71deSGregory Greenman 
4744243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
475ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
476ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
477ecaf71deSGregory Greenman 	}
478ecaf71deSGregory Greenman 
479b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
480b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
481960f864bSJohannes Berg 
482960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
483960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
48465e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
485960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
486960f864bSJohannes Berg 		/*
487960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
488960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
489960f864bSJohannes Berg 		 * for station powersave
490960f864bSJohannes Berg 		 */
491960f864bSJohannes Berg 		return -EINVAL;
492960f864bSJohannes Berg 	}
493e705c121SKalle Valo 
49480938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
49580938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
49680938abcSJohannes Berg 
4972d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
4982d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
4992d7cf549SJohannes Berg 
500cf961e16SLiad Kaufman 	hw->queues = IEEE80211_MAX_QUEUES;
501e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
502e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
503e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
504e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
505e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
506371a17edSJohannes Berg 
507371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
508371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
509371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
510371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
511371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
512371a17edSJohannes Berg 
5134243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
5149f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
5159f66a397SGregory Greenman 
516e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
517e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
518438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
519e705c121SKalle Valo 
5208e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
521e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
522e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
523e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
524e705c121SKalle Valo 
5252a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
5262a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
5272a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
5282a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
5292a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
5302a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
5312a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
5322a53d166SAyala Beker 	}
5332a53d166SAyala Beker 
5344b87e5afSLuca Coelho 	/* Enable 11w if software crypto is not enabled (as the
5354b87e5afSLuca Coelho 	 * firmware will interpret some mgmt packets, so enabling it
5364b87e5afSLuca Coelho 	 * with software crypto isn't safe).
537e705c121SKalle Valo 	 */
5383b37f4c9SJohannes Berg 	if (!iwlwifi_mod_params.swcrypto) {
539e705c121SKalle Valo 		ieee80211_hw_set(hw, MFP_CAPABLE);
540e705c121SKalle Valo 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
541e705c121SKalle Valo 			WLAN_CIPHER_SUITE_AES_CMAC;
542e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
5438e160ab8SAyala Beker 		if (iwl_mvm_has_new_rx_api(mvm)) {
5448e160ab8SAyala Beker 			mvm->ciphers[hw->wiphy->n_cipher_suites] =
5458e160ab8SAyala Beker 				WLAN_CIPHER_SUITE_BIP_GMAC_128;
5468e160ab8SAyala Beker 			hw->wiphy->n_cipher_suites++;
5478e160ab8SAyala Beker 			mvm->ciphers[hw->wiphy->n_cipher_suites] =
5488e160ab8SAyala Beker 				WLAN_CIPHER_SUITE_BIP_GMAC_256;
5498e160ab8SAyala Beker 			hw->wiphy->n_cipher_suites++;
5508e160ab8SAyala Beker 		}
551e705c121SKalle Valo 	}
552e705c121SKalle Valo 
553e705c121SKalle Valo 	/* currently FW API supports only one optional cipher scheme */
554e705c121SKalle Valo 	if (mvm->fw->cs[0].cipher) {
55524ddddf3SJohannes Berg 		const struct iwl_fw_cipher_scheme *fwcs = &mvm->fw->cs[0];
55624ddddf3SJohannes Berg 		struct ieee80211_cipher_scheme *cs = &mvm->cs[0];
55724ddddf3SJohannes Berg 
558e705c121SKalle Valo 		mvm->hw->n_cipher_schemes = 1;
55924ddddf3SJohannes Berg 
56024ddddf3SJohannes Berg 		cs->cipher = le32_to_cpu(fwcs->cipher);
56124ddddf3SJohannes Berg 		cs->iftype = BIT(NL80211_IFTYPE_STATION);
56224ddddf3SJohannes Berg 		cs->hdr_len = fwcs->hdr_len;
56324ddddf3SJohannes Berg 		cs->pn_len = fwcs->pn_len;
56424ddddf3SJohannes Berg 		cs->pn_off = fwcs->pn_off;
56524ddddf3SJohannes Berg 		cs->key_idx_off = fwcs->key_idx_off;
56624ddddf3SJohannes Berg 		cs->key_idx_mask = fwcs->key_idx_mask;
56724ddddf3SJohannes Berg 		cs->key_idx_shift = fwcs->key_idx_shift;
56824ddddf3SJohannes Berg 		cs->mic_len = fwcs->mic_len;
56924ddddf3SJohannes Berg 
57024ddddf3SJohannes Berg 		mvm->hw->cipher_schemes = mvm->cs;
57124ddddf3SJohannes Berg 		mvm->ciphers[hw->wiphy->n_cipher_suites] = cs->cipher;
572e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
573e705c121SKalle Valo 	}
574e705c121SKalle Valo 
575b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
576fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
577b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
578b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
579fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
580fc36ffdaSJohannes Berg 	}
581b73f9a4aSJohannes Berg 
582e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
583e705c121SKalle Valo 	hw->wiphy->features |=
584e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
585e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
586e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
587e705c121SKalle Valo 
588e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
589e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
590e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
591cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
592e705c121SKalle Valo 
593e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
594e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
595e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
596e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
597e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
598e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
599e705c121SKalle Valo 
600e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
601e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
602e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
603e47df5bdSJohannes Berg 
604e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
605e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
606e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
607e705c121SKalle Valo 	else
608e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
609e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
610e705c121SKalle Valo 
611e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
612e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
613e705c121SKalle Valo 
614e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
615e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
616e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
617e705c121SKalle Valo 
618e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
619e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
620e705c121SKalle Valo 
621e705c121SKalle Valo 	/* Extract MAC address */
622e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
623e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
624e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
625e705c121SKalle Valo 
626e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
627e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
628e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
629e705c121SKalle Valo 
630e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
631e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
632e705c121SKalle Valo 		       ETH_ALEN);
633e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
634e705c121SKalle Valo 		hw->wiphy->n_addresses++;
635e705c121SKalle Valo 	}
636e705c121SKalle Valo 
637e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
638e705c121SKalle Valo 
639e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
640e705c121SKalle Valo 
641e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
642e705c121SKalle Valo 
643e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
644e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
645e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
646e705c121SKalle Valo 
647e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
648e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
649e705c121SKalle Valo 	else
650e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
651e705c121SKalle Valo 
65257fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
65357fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
65457fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
65557fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
65657fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
65757fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
658e705c121SKalle Valo 
659e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
660e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
661e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
662e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
66357fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
664e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
665e705c121SKalle Valo 	}
666e705c121SKalle Valo 
667e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
668e705c121SKalle Valo 
669e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
670e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
671e705c121SKalle Valo 	else
672e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
673e705c121SKalle Valo 
674ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
675e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
676e705c121SKalle Valo 	hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
677e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
678e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
679e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
680e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
681e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
682e705c121SKalle Valo 
683e705c121SKalle Valo 	/*
684e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
685e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
686e705c121SKalle Valo 	 */
687e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
688e705c121SKalle Valo 
689e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
690e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
691e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
692a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
693e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
694e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
695e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
696e705c121SKalle Valo 
697e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
698e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
699e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
700e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
701e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
702e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
703e705c121SKalle Valo 
704e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
705e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
706e705c121SKalle Valo 		hw->wiphy->features |=
707e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
708e705c121SKalle Valo 
709e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
710e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
711e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
712e705c121SKalle Valo 
713aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
714aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
715aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
716aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
717aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
718aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
719aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
720aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
721aacf8f18SAvrahams Stern 	}
722aacf8f18SAvrahams Stern 
7238f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
7248f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
7258f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
7268f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
7278f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
7288f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
7298f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
7308f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
7318f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
7328f691af9SZamir, Roee 	}
7338f691af9SZamir, Roee 
7347360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
7357360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
7367360f99eSEmmanuel Grumbach 		hw->wiphy->iftype_ext_capab = he_iftypes_ext_capa;
7377360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
7387360f99eSEmmanuel Grumbach 			ARRAY_SIZE(he_iftypes_ext_capa);
739918cbf39SSara Sharon 
740918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
741918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
7427360f99eSEmmanuel Grumbach 	}
7437360f99eSEmmanuel Grumbach 
744e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
745e705c121SKalle Valo 
746e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
747e705c121SKalle Valo 	if (iwl_mvm_is_d0i3_supported(mvm) &&
748e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
749e705c121SKalle Valo 		mvm->wowlan.flags = WIPHY_WOWLAN_ANY;
750e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
751e705c121SKalle Valo 	}
752e705c121SKalle Valo 
7533f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
754e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
755e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
756e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
757e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
758e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
759e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
760e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
761e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
7623b37f4c9SJohannes Berg 		if (!iwlwifi_mod_params.swcrypto)
763e705c121SKalle Valo 			mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
764e705c121SKalle Valo 					     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
765e705c121SKalle Valo 					     WIPHY_WOWLAN_4WAY_HANDSHAKE;
766e705c121SKalle Valo 
767e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
768e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
769e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
770e705c121SKalle Valo 		mvm->wowlan.max_nd_match_sets = IWL_SCAN_MAX_PROFILES;
771e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
772e705c121SKalle Valo 	}
773e705c121SKalle Valo #endif
774e705c121SKalle Valo 
775e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
776e705c121SKalle Valo 	/* assign default bcast filtering configuration */
777e705c121SKalle Valo 	mvm->bcast_filters = iwl_mvm_default_bcast_filters;
778e705c121SKalle Valo #endif
779e705c121SKalle Valo 
780e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
781e705c121SKalle Valo 	if (ret)
782e705c121SKalle Valo 		return ret;
783e705c121SKalle Valo 
784e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
785e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
786e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
787e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
788e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
789e705c121SKalle Valo 	}
790e705c121SKalle Valo 
791e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
792e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
793e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
794e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
795e705c121SKalle Valo 	}
796e705c121SKalle Valo 
797e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
7985e6a98dcSSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm)) {
7995e6a98dcSSara Sharon 		hw->netdev_features &= ~(IWL_TX_CSUM_NETIF_FLAGS |
8005e6a98dcSSara Sharon 					 NETIF_F_RXCSUM);
8015e6a98dcSSara Sharon 		/* We may support SW TX CSUM */
80241837ca9SEmmanuel Grumbach 		if (IWL_MVM_SW_TX_CSUM_OFFLOAD)
8035e6a98dcSSara Sharon 			hw->netdev_features |= IWL_TX_CSUM_NETIF_FLAGS;
8045e6a98dcSSara Sharon 	}
80541837ca9SEmmanuel Grumbach 
80691b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
80791b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
80891b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
80991b08c2dSAviya Erenfeld 
810de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
811de645e89SJohannes Berg 	if (ret) {
812de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
813e705c121SKalle Valo 	}
814e705c121SKalle Valo 
815e705c121SKalle Valo 	return ret;
816e705c121SKalle Valo }
817e705c121SKalle Valo 
818e705c121SKalle Valo static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
819e705c121SKalle Valo 			     struct ieee80211_sta *sta,
820e705c121SKalle Valo 			     struct sk_buff *skb)
821e705c121SKalle Valo {
822e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
823e705c121SKalle Valo 	bool defer = false;
824e705c121SKalle Valo 
825e705c121SKalle Valo 	/*
826e705c121SKalle Valo 	 * double check the IN_D0I3 flag both before and after
827e705c121SKalle Valo 	 * taking the spinlock, in order to prevent taking
828e705c121SKalle Valo 	 * the spinlock when not needed.
829e705c121SKalle Valo 	 */
830e705c121SKalle Valo 	if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
831e705c121SKalle Valo 		return false;
832e705c121SKalle Valo 
833e705c121SKalle Valo 	spin_lock(&mvm->d0i3_tx_lock);
834e705c121SKalle Valo 	/*
835e705c121SKalle Valo 	 * testing the flag again ensures the skb dequeue
836e705c121SKalle Valo 	 * loop (on d0i3 exit) hasn't run yet.
837e705c121SKalle Valo 	 */
838e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
839e705c121SKalle Valo 		goto out;
840e705c121SKalle Valo 
841e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
8420ae98812SSara Sharon 	if (mvmsta->sta_id == IWL_MVM_INVALID_STA ||
843e705c121SKalle Valo 	    mvmsta->sta_id != mvm->d0i3_ap_sta_id)
844e705c121SKalle Valo 		goto out;
845e705c121SKalle Valo 
846e705c121SKalle Valo 	__skb_queue_tail(&mvm->d0i3_tx, skb);
847e705c121SKalle Valo 
848e705c121SKalle Valo 	/* trigger wakeup */
849e705c121SKalle Valo 	iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
850e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
851e705c121SKalle Valo 
852e705c121SKalle Valo 	defer = true;
853e705c121SKalle Valo out:
854e705c121SKalle Valo 	spin_unlock(&mvm->d0i3_tx_lock);
855e705c121SKalle Valo 	return defer;
856e705c121SKalle Valo }
857e705c121SKalle Valo 
858e705c121SKalle Valo static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
859e705c121SKalle Valo 			   struct ieee80211_tx_control *control,
860e705c121SKalle Valo 			   struct sk_buff *skb)
861e705c121SKalle Valo {
862e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
863e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
864e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
865e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
866cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
867cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
868e705c121SKalle Valo 
869e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
870e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
871e705c121SKalle Valo 		goto drop;
872e705c121SKalle Valo 	}
873e705c121SKalle Valo 
874cfbc6c4cSSara Sharon 	if (offchannel &&
875e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
876e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
877e705c121SKalle Valo 		goto drop;
878e705c121SKalle Valo 
879eb045e6eSDavid Spinadel 	/* treat non-bufferable MMPDUs on AP interfaces as broadcast */
880eb045e6eSDavid Spinadel 	if ((info->control.vif->type == NL80211_IFTYPE_AP ||
881eb045e6eSDavid Spinadel 	     info->control.vif->type == NL80211_IFTYPE_ADHOC) &&
882e705c121SKalle Valo 	    ieee80211_is_mgmt(hdr->frame_control) &&
883eb045e6eSDavid Spinadel 	    !ieee80211_is_bufferable_mmpdu(hdr->frame_control))
884e705c121SKalle Valo 		sta = NULL;
885e705c121SKalle Valo 
886dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
887cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
888cfbc6c4cSSara Sharon 	    !offchannel) {
889dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
890dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
891dc1aca22SAndrei Otcheretianski 		u8 ap_sta_id = READ_ONCE(mvmvif->ap_sta_id);
892dc1aca22SAndrei Otcheretianski 
893dc1aca22SAndrei Otcheretianski 		if (ap_sta_id < IWL_MVM_STATION_COUNT) {
894dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
895dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
896dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
897dc1aca22SAndrei Otcheretianski 				goto drop;
898dc1aca22SAndrei Otcheretianski 		}
899dc1aca22SAndrei Otcheretianski 	}
900dc1aca22SAndrei Otcheretianski 
901e705c121SKalle Valo 	if (sta) {
902e705c121SKalle Valo 		if (iwl_mvm_defer_tx(mvm, sta, skb))
903e705c121SKalle Valo 			return;
904e705c121SKalle Valo 		if (iwl_mvm_tx_skb(mvm, skb, sta))
905e705c121SKalle Valo 			goto drop;
906e705c121SKalle Valo 		return;
907e705c121SKalle Valo 	}
908e705c121SKalle Valo 
909e705c121SKalle Valo 	if (iwl_mvm_tx_skb_non_sta(mvm, skb))
910e705c121SKalle Valo 		goto drop;
911e705c121SKalle Valo 	return;
912e705c121SKalle Valo  drop:
913e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
914e705c121SKalle Valo }
915e705c121SKalle Valo 
916cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
917e705c121SKalle Valo {
918cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
919cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
920cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
921cfbc6c4cSSara Sharon 
922fba8248eSSara Sharon 	/*
923fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
924fba8248eSSara Sharon 	 * taker all the work.
925fba8248eSSara Sharon 	 *
926fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
927fba8248eSSara Sharon 	 *
928fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
929fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
930fba8248eSSara Sharon 	 *
931fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
932fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
933fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
934fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
935fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
936fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
937fba8248eSSara Sharon 	 *
938fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
939fba8248eSSara Sharon 	 * check the queue, so do nothing.
940fba8248eSSara Sharon 	 */
941fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
942fba8248eSSara Sharon 		return;
943cfbc6c4cSSara Sharon 
944cfbc6c4cSSara Sharon 	rcu_read_lock();
945fba8248eSSara Sharon 	do {
946cfbc6c4cSSara Sharon 		while (likely(!mvmtxq->stopped &&
947cfbc6c4cSSara Sharon 			      (mvm->trans->system_pm_mode ==
948cfbc6c4cSSara Sharon 			       IWL_PLAT_PM_MODE_DISABLED))) {
949cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
950cfbc6c4cSSara Sharon 
951f50d693bSSara Sharon 			if (!skb) {
952f50d693bSSara Sharon 				if (txq->sta)
953f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
954f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
955f50d693bSSara Sharon 						     txq->sta->addr,
956f50d693bSSara Sharon 						     txq->tid);
957cfbc6c4cSSara Sharon 				break;
958f50d693bSSara Sharon 			}
959cfbc6c4cSSara Sharon 
960cfbc6c4cSSara Sharon 			if (!txq->sta)
961cfbc6c4cSSara Sharon 				iwl_mvm_tx_skb_non_sta(mvm, skb);
962cfbc6c4cSSara Sharon 			else
963cfbc6c4cSSara Sharon 				iwl_mvm_tx_skb(mvm, skb, txq->sta);
964cfbc6c4cSSara Sharon 		}
965fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
966cfbc6c4cSSara Sharon 	rcu_read_unlock();
967e705c121SKalle Valo }
968e705c121SKalle Valo 
969cfbc6c4cSSara Sharon static void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
970cfbc6c4cSSara Sharon 				      struct ieee80211_txq *txq)
971e705c121SKalle Valo {
972cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
973cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
974e705c121SKalle Valo 
975cfbc6c4cSSara Sharon 	/*
976cfbc6c4cSSara Sharon 	 * Please note that racing is handled very carefully here:
977cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id is updated during allocation, and mvmtxq->list is
978cfbc6c4cSSara Sharon 	 * deleted afterwards.
979cfbc6c4cSSara Sharon 	 * This means that if:
980cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && list_empty(&mvmtxq->list):
981cfbc6c4cSSara Sharon 	 *	queue is allocated and we can TX.
982cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && !list_empty(&mvmtxq->list):
983cfbc6c4cSSara Sharon 	 *	a race, should defer the frame.
984cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && list_empty(&mvmtxq->list):
985cfbc6c4cSSara Sharon 	 *	need to allocate the queue and defer the frame.
986cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && !list_empty(&mvmtxq->list):
987cfbc6c4cSSara Sharon 	 *	queue is already scheduled for allocation, no need to allocate,
988cfbc6c4cSSara Sharon 	 *	should defer the frame.
989cfbc6c4cSSara Sharon 	 */
990cfbc6c4cSSara Sharon 
991cfbc6c4cSSara Sharon 	/* If the queue is allocated TX and return. */
992cfbc6c4cSSara Sharon 	if (!txq->sta || mvmtxq->txq_id != IWL_MVM_INVALID_QUEUE) {
993cfbc6c4cSSara Sharon 		/*
994cfbc6c4cSSara Sharon 		 * Check that list is empty to avoid a race where txq_id is
995cfbc6c4cSSara Sharon 		 * already updated, but the queue allocation work wasn't
996cfbc6c4cSSara Sharon 		 * finished
997cfbc6c4cSSara Sharon 		 */
998cfbc6c4cSSara Sharon 		if (unlikely(txq->sta && !list_empty(&mvmtxq->list)))
999cfbc6c4cSSara Sharon 			return;
1000cfbc6c4cSSara Sharon 
1001cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
1002cfbc6c4cSSara Sharon 		return;
1003cfbc6c4cSSara Sharon 	}
1004cfbc6c4cSSara Sharon 
1005cfbc6c4cSSara Sharon 	/* The list is being deleted only after the queue is fully allocated. */
1006cfbc6c4cSSara Sharon 	if (!list_empty(&mvmtxq->list))
1007cfbc6c4cSSara Sharon 		return;
1008cfbc6c4cSSara Sharon 
1009cfbc6c4cSSara Sharon 	list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
1010cfbc6c4cSSara Sharon 	schedule_work(&mvm->add_stream_wk);
1011e705c121SKalle Valo }
1012e705c121SKalle Valo 
1013e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
1014e705c121SKalle Valo 	do {								\
1015e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
1016e705c121SKalle Valo 			break;						\
10177174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
1018e705c121SKalle Valo 	} while (0)
1019e705c121SKalle Valo 
1020e705c121SKalle Valo static void
1021e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1022e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
1023e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
1024e705c121SKalle Valo {
1025e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
1026e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
1027e705c121SKalle Valo 
10286c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
10296c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
10306c042d75SSara Sharon 	if (!trig)
1031e705c121SKalle Valo 		return;
1032e705c121SKalle Valo 
1033e705c121SKalle Valo 	ba_trig = (void *)trig->data;
1034e705c121SKalle Valo 
1035e705c121SKalle Valo 	switch (action) {
1036e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
1037e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1038e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1039e705c121SKalle Valo 
1040e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
1041e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
1042e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
1043e705c121SKalle Valo 		break;
1044e705c121SKalle Valo 		}
1045e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
1046e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
1047e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
1048e705c121SKalle Valo 				 sta->addr, tid);
1049e705c121SKalle Valo 		break;
1050e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
1051e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
1052e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
1053e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
1054e705c121SKalle Valo 		break;
1055e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
1056e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
1057e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
1058e705c121SKalle Valo 				 sta->addr, tid);
1059e705c121SKalle Valo 		break;
1060e705c121SKalle Valo 	default:
1061e705c121SKalle Valo 		break;
1062e705c121SKalle Valo 	}
1063e705c121SKalle Valo }
1064e705c121SKalle Valo 
1065e705c121SKalle Valo static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
1066e705c121SKalle Valo 				    struct ieee80211_vif *vif,
106750ea05efSSara Sharon 				    struct ieee80211_ampdu_params *params)
1068e705c121SKalle Valo {
1069e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1070e705c121SKalle Valo 	int ret;
1071e705c121SKalle Valo 	bool tx_agg_ref = false;
107250ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
107350ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
107450ea05efSSara Sharon 	u16 tid = params->tid;
107550ea05efSSara Sharon 	u16 *ssn = &params->ssn;
1076514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
1077bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
107810b2b201SSara Sharon 	u16 timeout = params->timeout;
1079e705c121SKalle Valo 
1080e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
1081e705c121SKalle Valo 		     sta->addr, tid, action);
1082e705c121SKalle Valo 
1083e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
1084e705c121SKalle Valo 		return -EACCES;
1085e705c121SKalle Valo 
1086e705c121SKalle Valo 	/* return from D0i3 before starting a new Tx aggregation */
1087e705c121SKalle Valo 	switch (action) {
1088e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
1089e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
1090e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1091e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1092e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1093e705c121SKalle Valo 		/*
1094e705c121SKalle Valo 		 * for tx start, wait synchronously until D0i3 exit to
1095e705c121SKalle Valo 		 * get the correct sequence number for the tid.
1096e705c121SKalle Valo 		 * additionally, some other ampdu actions use direct
1097e705c121SKalle Valo 		 * target access, which is not handled automatically
1098e705c121SKalle Valo 		 * by the trans layer (unlike commands), so wait for
1099e705c121SKalle Valo 		 * d0i3 exit in these cases as well.
1100e705c121SKalle Valo 		 */
1101e705c121SKalle Valo 		ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_TX_AGG);
1102e705c121SKalle Valo 		if (ret)
1103e705c121SKalle Valo 			return ret;
1104e705c121SKalle Valo 
1105e705c121SKalle Valo 		tx_agg_ref = true;
1106e705c121SKalle Valo 		break;
1107e705c121SKalle Valo 	default:
1108e705c121SKalle Valo 		break;
1109e705c121SKalle Valo 	}
1110e705c121SKalle Valo 
1111e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1112e705c121SKalle Valo 
1113e705c121SKalle Valo 	switch (action) {
1114e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
1115b0ffe455SJohannes Berg 		if (iwl_mvm_vif_from_mac80211(vif)->ap_sta_id ==
1116b0ffe455SJohannes Berg 				iwl_mvm_sta_from_mac80211(sta)->sta_id) {
1117b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
1118b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
1119b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
1120b0ffe455SJohannes Berg 
1121b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
1122b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
1123b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
1124b0ffe455SJohannes Berg 		}
1125e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
1126e705c121SKalle Valo 			ret = -EINVAL;
1127e705c121SKalle Valo 			break;
1128e705c121SKalle Valo 		}
112910b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
113010b2b201SSara Sharon 					 timeout);
1131e705c121SKalle Valo 		break;
1132e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
113310b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
113410b2b201SSara Sharon 					 timeout);
1135e705c121SKalle Valo 		break;
1136e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
1137e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
1138e705c121SKalle Valo 			ret = -EINVAL;
1139e705c121SKalle Valo 			break;
1140e705c121SKalle Valo 		}
1141e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1142e705c121SKalle Valo 		break;
1143e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
1144e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1145e705c121SKalle Valo 		break;
1146e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1147e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1148e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1149e705c121SKalle Valo 		break;
1150e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1151bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1152bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
1153e705c121SKalle Valo 		break;
1154e705c121SKalle Valo 	default:
1155e705c121SKalle Valo 		WARN_ON_ONCE(1);
1156e705c121SKalle Valo 		ret = -EINVAL;
1157e705c121SKalle Valo 		break;
1158e705c121SKalle Valo 	}
1159e705c121SKalle Valo 
1160e705c121SKalle Valo 	if (!ret) {
1161e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
1162e705c121SKalle Valo 
1163e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1164e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1165e705c121SKalle Valo 
1166e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1167e705c121SKalle Valo 					    rx_ba_ssn, action);
1168e705c121SKalle Valo 	}
1169e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1170e705c121SKalle Valo 
1171e705c121SKalle Valo 	/*
1172e705c121SKalle Valo 	 * If the tid is marked as started, we won't use it for offloaded
1173e705c121SKalle Valo 	 * traffic on the next D0i3 entry. It's safe to unref.
1174e705c121SKalle Valo 	 */
1175e705c121SKalle Valo 	if (tx_agg_ref)
1176e705c121SKalle Valo 		iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
1177e705c121SKalle Valo 
1178e705c121SKalle Valo 	return ret;
1179e705c121SKalle Valo }
1180e705c121SKalle Valo 
1181e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1182e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1183e705c121SKalle Valo {
1184e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1185e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1186e705c121SKalle Valo 
1187e705c121SKalle Valo 	mvmvif->uploaded = false;
11880ae98812SSara Sharon 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1189e705c121SKalle Valo 
1190e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1191e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1192e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1193e705c121SKalle Valo 
1194e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1195e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
119686e177d8SGregory Greenman 	memset(&mvmvif->probe_resp_data, 0, sizeof(mvmvif->probe_resp_data));
1197e705c121SKalle Valo }
1198e705c121SKalle Valo 
1199e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1200e705c121SKalle Valo {
1201e705c121SKalle Valo 	/* cleanup all stale references (scan, roc), but keep the
1202e705c121SKalle Valo 	 * ucode_down ref until reconfig is complete
1203e705c121SKalle Valo 	 */
1204e705c121SKalle Valo 	iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
1205e705c121SKalle Valo 
1206fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1207e705c121SKalle Valo 
12089bf13beeSJohannes Berg 	mvm->cur_aid = 0;
12099bf13beeSJohannes Berg 
1210e705c121SKalle Valo 	mvm->scan_status = 0;
1211e705c121SKalle Valo 	mvm->ps_disabled = false;
1212b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1213e705c121SKalle Valo 
1214e705c121SKalle Valo 	/* just in case one was running */
1215305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1216e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1217e705c121SKalle Valo 
1218fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1219fc36ffdaSJohannes Berg 
1220e705c121SKalle Valo 	/*
1221e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1222e705c121SKalle Valo 	 * gone down during the HW restart
1223e705c121SKalle Valo 	 */
1224e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1225e705c121SKalle Valo 
1226e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
12270ae98812SSara Sharon 	mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
1228e705c121SKalle Valo 
1229e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
12309c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1231e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1232e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1233e705c121SKalle Valo 
1234e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1235e705c121SKalle Valo 
1236e705c121SKalle Valo 	/* clear any stale d0i3 state */
1237e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
1238e705c121SKalle Valo 
1239e705c121SKalle Valo 	mvm->vif_count = 0;
1240e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
12417174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1242baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1243e705c121SKalle Valo 
1244e705c121SKalle Valo 	/* keep statistics ticking */
1245e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1246e705c121SKalle Valo }
1247e705c121SKalle Valo 
1248e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1249e705c121SKalle Valo {
1250e705c121SKalle Valo 	int ret;
1251e705c121SKalle Valo 
1252e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1253e705c121SKalle Valo 
1254bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1255bf8b286fSJohannes Berg 		/*
1256bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1257bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1258bf8b286fSJohannes Berg 		 */
1259bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1260bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1261e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1262e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1263a42b2af3SLuca Coelho 	} else {
1264a42b2af3SLuca Coelho 		/* Hold the reference to prevent runtime suspend while
1265a42b2af3SLuca Coelho 		 * the start procedure runs.  It's a bit confusing
1266a42b2af3SLuca Coelho 		 * that the UCODE_DOWN reference is taken, but it just
1267a42b2af3SLuca Coelho 		 * means "UCODE is not UP yet". ( TODO: rename this
1268a42b2af3SLuca Coelho 		 * reference).
1269a42b2af3SLuca Coelho 		 */
1270a42b2af3SLuca Coelho 		iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1271a42b2af3SLuca Coelho 	}
1272e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1273e705c121SKalle Valo 
1274da2eb669SSara Sharon 	iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_POST_INIT);
1275da2eb669SSara Sharon 
1276e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1277e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1278e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1279e705c121SKalle Valo 		 * would do.
1280e705c121SKalle Valo 		 */
1281e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1282a75b9b33SLuca Coelho #ifdef CONFIG_PM
1283e705c121SKalle Valo 		iwl_mvm_d0i3_enable_tx(mvm, NULL);
1284a75b9b33SLuca Coelho #endif
1285e705c121SKalle Valo 	}
1286e705c121SKalle Valo 
1287e705c121SKalle Valo 	return ret;
1288e705c121SKalle Valo }
1289e705c121SKalle Valo 
1290e705c121SKalle Valo static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1291e705c121SKalle Valo {
1292e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1293e705c121SKalle Valo 	int ret;
1294e705c121SKalle Valo 
1295e705c121SKalle Valo 	/* Some hw restart cleanups must not hold the mutex */
1296e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1297e705c121SKalle Valo 		/*
1298e705c121SKalle Valo 		 * Make sure we are out of d0i3. This is needed
1299e705c121SKalle Valo 		 * to make sure the reference accounting is correct
1300e705c121SKalle Valo 		 * (and there is no stale d0i3_exit_work).
1301e705c121SKalle Valo 		 */
1302e705c121SKalle Valo 		wait_event_timeout(mvm->d0i3_exit_waitq,
1303e705c121SKalle Valo 				   !test_bit(IWL_MVM_STATUS_IN_D0I3,
1304e705c121SKalle Valo 					     &mvm->status),
1305e705c121SKalle Valo 				   HZ);
1306e705c121SKalle Valo 	}
1307e705c121SKalle Valo 
1308e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1309e705c121SKalle Valo 	ret = __iwl_mvm_mac_start(mvm);
1310e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1311e705c121SKalle Valo 
1312e705c121SKalle Valo 	return ret;
1313e705c121SKalle Valo }
1314e705c121SKalle Valo 
1315e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1316e705c121SKalle Valo {
1317e705c121SKalle Valo 	int ret;
1318e705c121SKalle Valo 
1319e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1320e705c121SKalle Valo 
1321e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1322a75b9b33SLuca Coelho #ifdef CONFIG_PM
1323e705c121SKalle Valo 	iwl_mvm_d0i3_enable_tx(mvm, NULL);
1324a75b9b33SLuca Coelho #endif
1325e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1326e705c121SKalle Valo 	if (ret)
1327e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1328e705c121SKalle Valo 			ret);
1329e705c121SKalle Valo 
1330e705c121SKalle Valo 	/* allow transport/FW low power modes */
1331e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
1332e705c121SKalle Valo 
1333f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1334f130bb75SMordechay Goodstein 
1335e705c121SKalle Valo 	/*
1336e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1337e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1338e705c121SKalle Valo 	 */
1339e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1340e705c121SKalle Valo 
1341e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1342e705c121SKalle Valo }
1343e705c121SKalle Valo 
1344e705c121SKalle Valo static void iwl_mvm_resume_complete(struct iwl_mvm *mvm)
1345e705c121SKalle Valo {
1346b7282643SLuca Coelho 	if (iwl_mvm_is_d0i3_supported(mvm) &&
1347b7282643SLuca Coelho 	    iwl_mvm_enter_d0i3_on_suspend(mvm))
1348b7282643SLuca Coelho 		WARN_ONCE(!wait_event_timeout(mvm->d0i3_exit_waitq,
1349e705c121SKalle Valo 					      !test_bit(IWL_MVM_STATUS_IN_D0I3,
1350e705c121SKalle Valo 							&mvm->status),
1351b7282643SLuca Coelho 					      HZ),
1352b7282643SLuca Coelho 			  "D0i3 exit on resume timed out\n");
1353e705c121SKalle Valo }
1354e705c121SKalle Valo 
1355e705c121SKalle Valo static void
1356e705c121SKalle Valo iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1357e705c121SKalle Valo 			      enum ieee80211_reconfig_type reconfig_type)
1358e705c121SKalle Valo {
1359e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1360e705c121SKalle Valo 
1361e705c121SKalle Valo 	switch (reconfig_type) {
1362e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1363e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1364e705c121SKalle Valo 		break;
1365e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1366e705c121SKalle Valo 		iwl_mvm_resume_complete(mvm);
1367e705c121SKalle Valo 		break;
1368e705c121SKalle Valo 	}
1369e705c121SKalle Valo }
1370e705c121SKalle Valo 
1371e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1372e705c121SKalle Valo {
1373e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1374e705c121SKalle Valo 
1375e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1376e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1377e705c121SKalle Valo 	 */
1378e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1379e705c121SKalle Valo 
1380e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1381e705c121SKalle Valo 
1382e705c121SKalle Valo 	/*
1383e705c121SKalle Valo 	 * The work item could be running or queued if the
1384e705c121SKalle Valo 	 * ROC time event stops just as we get here.
1385e705c121SKalle Valo 	 */
1386e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
1387e705c121SKalle Valo 
1388fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1389e705c121SKalle Valo 
1390e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1391e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1392e705c121SKalle Valo 
1393e705c121SKalle Valo 	/* the fw is stopped, the aux sta is dead: clean up driver state */
1394e705c121SKalle Valo 	iwl_mvm_del_aux_sta(mvm);
1395e705c121SKalle Valo 
1396e705c121SKalle Valo 	/*
1397155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1398155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1399155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1400e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1401e705c121SKalle Valo 	 * HW restart was requested.
1402e705c121SKalle Valo 	 */
1403155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1404155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1405155f7e04SEmmanuel Grumbach 			       &mvm->status))
1406e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1407e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1408e705c121SKalle Valo 
1409e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1410e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1411e705c121SKalle Valo 	 */
1412e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1413e705c121SKalle Valo 		int i;
1414e705c121SKalle Valo 
1415e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1416e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1417e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1418e705c121SKalle Valo 				      i))
1419e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1420e705c121SKalle Valo 		}
1421e705c121SKalle Valo 	}
1422e705c121SKalle Valo }
1423e705c121SKalle Valo 
1424e705c121SKalle Valo static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1425e705c121SKalle Valo {
1426e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1427e705c121SKalle Valo 
1428e705c121SKalle Valo 	flush_work(&mvm->d0i3_exit_work);
1429e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
143024afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1431771147b0SJohannes Berg 
1432771147b0SJohannes Berg 	/*
1433771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1434771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1435771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
14367174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1437771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1438771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1439771147b0SJohannes Berg 	 */
1440771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1441771147b0SJohannes Berg 
1442*c7ab138eSShahar S Matityahu 	iwl_fw_cancel_dumps(&mvm->fwrt);
1443d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
144469e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
14457174beb6SJohannes Berg 	iwl_fw_free_dump_desc(&mvm->fwrt);
1446e705c121SKalle Valo 
1447e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1448e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1449e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1450e705c121SKalle Valo 
1451e705c121SKalle Valo 	/*
1452e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1453e705c121SKalle Valo 	 * discover that its list is now empty.
1454e705c121SKalle Valo 	 */
1455e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1456e705c121SKalle Valo }
1457e705c121SKalle Valo 
1458e705c121SKalle Valo static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1459e705c121SKalle Valo {
1460e705c121SKalle Valo 	u16 i;
1461e705c121SKalle Valo 
1462e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1463e705c121SKalle Valo 
1464e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1465e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1466e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1467e705c121SKalle Valo 
1468e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1469e705c121SKalle Valo 	return NULL;
1470e705c121SKalle Valo }
1471e705c121SKalle Valo 
1472e705c121SKalle Valo static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1473e705c121SKalle Valo 				s16 tx_power)
1474e705c121SKalle Valo {
14750791c2fcSHaim Dreyfuss 	int len;
14760791c2fcSHaim Dreyfuss 	union {
14770791c2fcSHaim Dreyfuss 		struct iwl_dev_tx_power_cmd v5;
14780791c2fcSHaim Dreyfuss 		struct iwl_dev_tx_power_cmd_v4 v4;
14790791c2fcSHaim Dreyfuss 	} cmd = {
14800791c2fcSHaim Dreyfuss 		.v5.v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
14810791c2fcSHaim Dreyfuss 		.v5.v3.mac_context_id =
1482e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
14830791c2fcSHaim Dreyfuss 		.v5.v3.pwr_restriction = cpu_to_le16(8 * tx_power),
1484e705c121SKalle Valo 	};
1485e705c121SKalle Valo 
1486e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
14870791c2fcSHaim Dreyfuss 		cmd.v5.v3.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1488e705c121SKalle Valo 
14890791c2fcSHaim Dreyfuss 	if (fw_has_api(&mvm->fw->ucode_capa,
14900791c2fcSHaim Dreyfuss 		       IWL_UCODE_TLV_API_REDUCE_TX_POWER))
14910791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
14920791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
14930791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
14940791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
14950791c2fcSHaim Dreyfuss 	else
14960791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4.v3);
1497e705c121SKalle Valo 
1498e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
1499e705c121SKalle Valo }
1500e705c121SKalle Valo 
1501f6780614SSara Sharon static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1502f6780614SSara Sharon 				       struct ieee80211_vif *vif)
1503f6780614SSara Sharon {
1504f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1505f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1506f6780614SSara Sharon 	int ret;
1507f6780614SSara Sharon 
1508f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1509f6780614SSara Sharon 
1510f6780614SSara Sharon 	if (mvmvif->csa_failed) {
1511f6780614SSara Sharon 		mvmvif->csa_failed = false;
1512f6780614SSara Sharon 		ret = -EIO;
1513f6780614SSara Sharon 		goto out_unlock;
1514f6780614SSara Sharon 	}
1515f6780614SSara Sharon 
1516f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1517f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
1518f6780614SSara Sharon 
1519f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
1520f6780614SSara Sharon 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
1521f6780614SSara Sharon 							  mvmvif->ap_sta_id);
1522f6780614SSara Sharon 
1523f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1524f6780614SSara Sharon 			ret = -EIO;
1525f6780614SSara Sharon 			goto out_unlock;
1526f6780614SSara Sharon 		}
1527f6780614SSara Sharon 
1528f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1529f6780614SSara Sharon 
1530f6780614SSara Sharon 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1531f6780614SSara Sharon 
1532f6780614SSara Sharon 		ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1533f6780614SSara Sharon 		if (ret)
1534f6780614SSara Sharon 			goto out_unlock;
1535f6780614SSara Sharon 
1536f6780614SSara Sharon 		iwl_mvm_stop_session_protection(mvm, vif);
1537f6780614SSara Sharon 	}
1538f6780614SSara Sharon 
1539f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1540f6780614SSara Sharon 
1541f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1542f6780614SSara Sharon 
1543f6780614SSara Sharon out_unlock:
1544f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1545f6780614SSara Sharon 
1546f6780614SSara Sharon 	return ret;
1547f6780614SSara Sharon }
1548f6780614SSara Sharon 
1549f6780614SSara Sharon static void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1550f6780614SSara Sharon 					 struct ieee80211_vif *vif)
1551f6780614SSara Sharon {
1552f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1553f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1554f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1555f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1556f6780614SSara Sharon 							  mvmvif->color)),
1557f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1558f6780614SSara Sharon 	};
1559f6780614SSara Sharon 
1560f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1561f6780614SSara Sharon 
1562f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1563f6780614SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1564f6780614SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
1565f6780614SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
1566f6780614SSara Sharon 				     0, sizeof(cmd), &cmd));
1567f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1568f6780614SSara Sharon 
1569f6780614SSara Sharon 	WARN_ON(iwl_mvm_post_channel_switch(hw, vif));
1570f6780614SSara Sharon }
1571f6780614SSara Sharon 
1572f6780614SSara Sharon static void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1573f6780614SSara Sharon {
1574f6780614SSara Sharon 	struct iwl_mvm *mvm;
1575f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1576f6780614SSara Sharon 	struct ieee80211_vif *vif;
1577f6780614SSara Sharon 
1578f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1579f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1580f6780614SSara Sharon 	mvm = mvmvif->mvm;
1581f6780614SSara Sharon 
1582f6780614SSara Sharon 	iwl_mvm_abort_channel_switch(mvm->hw, vif);
1583f6780614SSara Sharon 	ieee80211_chswitch_done(vif, false);
1584f6780614SSara Sharon }
1585f6780614SSara Sharon 
1586e705c121SKalle Valo static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1587e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1588e705c121SKalle Valo {
1589e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1590e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1591e705c121SKalle Valo 	int ret;
1592e705c121SKalle Valo 
1593e705c121SKalle Valo 	mvmvif->mvm = mvm;
159486e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
1595e705c121SKalle Valo 
1596e705c121SKalle Valo 	/*
1597e705c121SKalle Valo 	 * make sure D0i3 exit is completed, otherwise a target access
1598e705c121SKalle Valo 	 * during tx queue configuration could be done when still in
1599e705c121SKalle Valo 	 * D0i3 state.
1600e705c121SKalle Valo 	 */
1601e705c121SKalle Valo 	ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_ADD_IF);
1602e705c121SKalle Valo 	if (ret)
1603e705c121SKalle Valo 		return ret;
1604e705c121SKalle Valo 
1605e705c121SKalle Valo 	/*
1606e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1607e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1608e705c121SKalle Valo 	 * don't really have to check the types.
1609e705c121SKalle Valo 	 */
1610e705c121SKalle Valo 
1611e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1612e705c121SKalle Valo 
1613e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1614e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1615e705c121SKalle Valo 		mvmvif->beacon_stats.accu_num_beacons +=
1616e705c121SKalle Valo 			mvmvif->beacon_stats.num_beacons;
1617e705c121SKalle Valo 
1618e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
1619e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1620e705c121SKalle Valo 	if (ret)
1621e705c121SKalle Valo 		goto out_unlock;
1622e705c121SKalle Valo 
1623698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1624698478c4SSara Sharon 
1625e705c121SKalle Valo 	/* Counting number of interfaces is needed for legacy PM */
1626e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1627e705c121SKalle Valo 		mvm->vif_count++;
1628e705c121SKalle Valo 
1629e705c121SKalle Valo 	/*
1630e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1631e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1632e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1633e705c121SKalle Valo 	 * only after the binding.
1634e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1635e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1636e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1637e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1638e705c121SKalle Valo 	 * allocating resources :)
1639e705c121SKalle Valo 	 */
1640e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1641e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1642e705c121SKalle Valo 		ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1643e705c121SKalle Valo 		if (ret) {
1644e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1645e705c121SKalle Valo 			goto out_release;
1646e705c121SKalle Valo 		}
1647e705c121SKalle Valo 
164826d6c16bSSara Sharon 		/*
164926d6c16bSSara Sharon 		 * Only queue for this station is the mcast queue,
165026d6c16bSSara Sharon 		 * which shouldn't be in TFD mask anyway
165126d6c16bSSara Sharon 		 */
165226d6c16bSSara Sharon 		ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta,
1653ced19f26SSara Sharon 					       0, vif->type,
1654ced19f26SSara Sharon 					       IWL_STA_MULTICAST);
165526d6c16bSSara Sharon 		if (ret)
165626d6c16bSSara Sharon 			goto out_release;
165726d6c16bSSara Sharon 
1658e705c121SKalle Valo 		iwl_mvm_vif_dbgfs_register(mvm, vif);
1659e705c121SKalle Valo 		goto out_unlock;
1660e705c121SKalle Valo 	}
1661e705c121SKalle Valo 
1662e705c121SKalle Valo 	mvmvif->features |= hw->netdev_features;
1663e705c121SKalle Valo 
1664e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1665e705c121SKalle Valo 	if (ret)
1666e705c121SKalle Valo 		goto out_release;
1667e705c121SKalle Valo 
1668e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1669e705c121SKalle Valo 	if (ret)
1670e705c121SKalle Valo 		goto out_remove_mac;
1671e705c121SKalle Valo 
1672e705c121SKalle Valo 	/* beacon filtering */
1673e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1674e705c121SKalle Valo 	if (ret)
1675e705c121SKalle Valo 		goto out_remove_mac;
1676e705c121SKalle Valo 
1677e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1678e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1679e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1680e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1681e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1682e705c121SKalle Valo 	}
1683e705c121SKalle Valo 
1684e705c121SKalle Valo 	/*
1685e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1686e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1687e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1688e705c121SKalle Valo 	 */
1689e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1690e705c121SKalle Valo 
1691e705c121SKalle Valo 		mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1692e705c121SKalle Valo 		if (!mvmvif->phy_ctxt) {
1693e705c121SKalle Valo 			ret = -ENOSPC;
1694e705c121SKalle Valo 			goto out_free_bf;
1695e705c121SKalle Valo 		}
1696e705c121SKalle Valo 
1697e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1698e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1699e705c121SKalle Valo 		if (ret)
1700e705c121SKalle Valo 			goto out_unref_phy;
1701e705c121SKalle Valo 
1702d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1703e705c121SKalle Valo 		if (ret)
1704e705c121SKalle Valo 			goto out_unbind;
1705e705c121SKalle Valo 
1706e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1707e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1708e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1709e705c121SKalle Valo 	}
1710e705c121SKalle Valo 
1711b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1712f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1713f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1714b0ffe455SJohannes Berg 
1715baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1716baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
1717baf41bc3SShaul Triebitz 
1718e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
1719e705c121SKalle Valo 	goto out_unlock;
1720e705c121SKalle Valo 
1721e705c121SKalle Valo  out_unbind:
1722e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1723e705c121SKalle Valo  out_unref_phy:
1724e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1725e705c121SKalle Valo  out_free_bf:
1726e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1727e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1728e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1729e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1730e705c121SKalle Valo 	}
1731e705c121SKalle Valo  out_remove_mac:
1732e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1733e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1734e705c121SKalle Valo  out_release:
1735e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1736e705c121SKalle Valo 		mvm->vif_count--;
1737e705c121SKalle Valo  out_unlock:
1738e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1739e705c121SKalle Valo 
1740e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_ADD_IF);
1741e705c121SKalle Valo 
1742e705c121SKalle Valo 	return ret;
1743e705c121SKalle Valo }
1744e705c121SKalle Valo 
1745e705c121SKalle Valo static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1746e705c121SKalle Valo 					struct ieee80211_vif *vif)
1747e705c121SKalle Valo {
1748e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1749e705c121SKalle Valo 		/*
1750e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1751e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1752e705c121SKalle Valo 		 * queue are sent in ROC session.
1753e705c121SKalle Valo 		 */
1754e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1755e705c121SKalle Valo 	}
1756e705c121SKalle Valo }
1757e705c121SKalle Valo 
1758e705c121SKalle Valo static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1759e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1760e705c121SKalle Valo {
1761e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1762e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
176386e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1764e705c121SKalle Valo 
1765e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1766e705c121SKalle Valo 
1767b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1768b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1769b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1770b0ffe455SJohannes Berg 
1771e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1772e705c121SKalle Valo 
177386e177d8SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->probe_resp_data,
177486e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
177586e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
177686e177d8SGregory Greenman 	if (probe_data)
177786e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
177886e177d8SGregory Greenman 
1779e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1780e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1781e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1782e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1783e705c121SKalle Valo 	}
1784e705c121SKalle Valo 
1785b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1786b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1787b73f9a4aSJohannes Berg 
1788e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1789e705c121SKalle Valo 
1790e705c121SKalle Valo 	/*
1791e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1792e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1793e705c121SKalle Valo 	 */
1794e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1795e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1796e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1797e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1798e705c121SKalle Valo 			mvm->noa_vif = NULL;
1799e705c121SKalle Valo 			mvm->noa_duration = 0;
1800e705c121SKalle Valo 		}
1801e705c121SKalle Valo #endif
180226d6c16bSSara Sharon 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
1803e705c121SKalle Valo 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1804e705c121SKalle Valo 		goto out_release;
1805e705c121SKalle Valo 	}
1806e705c121SKalle Valo 
1807e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1808e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1809d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1810e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1811e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1812e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
1813e705c121SKalle Valo 	}
1814e705c121SKalle Valo 
1815e705c121SKalle Valo 	if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1816e705c121SKalle Valo 		mvm->vif_count--;
1817e705c121SKalle Valo 
1818e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
1819e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1820e705c121SKalle Valo 
1821698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1822698478c4SSara Sharon 
1823baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1824baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1825baf41bc3SShaul Triebitz 
1826e705c121SKalle Valo out_release:
1827e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1828e705c121SKalle Valo }
1829e705c121SKalle Valo 
1830e705c121SKalle Valo static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1831e705c121SKalle Valo {
1832e705c121SKalle Valo 	return 0;
1833e705c121SKalle Valo }
1834e705c121SKalle Valo 
1835e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1836e705c121SKalle Valo 	struct iwl_mvm *mvm;
1837e705c121SKalle Valo 	int port_id;
1838e705c121SKalle Valo };
1839e705c121SKalle Valo 
1840e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1841e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1842e705c121SKalle Valo {
1843e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1844e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1845e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
184697bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
184797bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
184897bce57bSLuca Coelho 		.flags = CMD_ASYNC,
184997bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
185097bce57bSLuca Coelho 	};
1851e705c121SKalle Valo 	int ret, len;
1852e705c121SKalle Valo 
1853e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1854e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1855e705c121SKalle Valo 		return;
1856e705c121SKalle Valo 
1857e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1858e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1859e705c121SKalle Valo 		return;
1860e705c121SKalle Valo 
1861e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1862e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1863e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1864e705c121SKalle Valo 
186597bce57bSLuca Coelho 	hcmd.len[0] = len;
186697bce57bSLuca Coelho 	hcmd.data[0] = cmd;
186797bce57bSLuca Coelho 
186897bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1869e705c121SKalle Valo 	if (ret)
1870e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1871e705c121SKalle Valo }
1872e705c121SKalle Valo 
1873e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1874e705c121SKalle Valo {
1875e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1876e705c121SKalle Valo 		.mvm = mvm,
1877e705c121SKalle Valo 	};
1878e705c121SKalle Valo 
1879e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1880e705c121SKalle Valo 
1881e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1882e705c121SKalle Valo 		return;
1883e705c121SKalle Valo 
1884e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1885e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1886e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1887e705c121SKalle Valo }
1888e705c121SKalle Valo 
1889e705c121SKalle Valo static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1890e705c121SKalle Valo 				     struct netdev_hw_addr_list *mc_list)
1891e705c121SKalle Valo {
1892e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1893e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1894e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1895e705c121SKalle Valo 	int addr_count;
1896e705c121SKalle Valo 	bool pass_all;
1897e705c121SKalle Valo 	int len;
1898e705c121SKalle Valo 
1899e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1900e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1901e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1902e705c121SKalle Valo 	if (pass_all)
1903e705c121SKalle Valo 		addr_count = 0;
1904e705c121SKalle Valo 
1905e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1906e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1907e705c121SKalle Valo 	if (!cmd)
1908e705c121SKalle Valo 		return 0;
1909e705c121SKalle Valo 
1910e705c121SKalle Valo 	if (pass_all) {
1911e705c121SKalle Valo 		cmd->pass_all = 1;
1912e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1913e705c121SKalle Valo 	}
1914e705c121SKalle Valo 
1915e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1916e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1917e705c121SKalle Valo 				   cmd->count, addr->addr);
1918e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1919e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1920e705c121SKalle Valo 		cmd->count++;
1921e705c121SKalle Valo 	}
1922e705c121SKalle Valo 
1923e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1924e705c121SKalle Valo }
1925e705c121SKalle Valo 
1926e705c121SKalle Valo static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1927e705c121SKalle Valo 				     unsigned int changed_flags,
1928e705c121SKalle Valo 				     unsigned int *total_flags,
1929e705c121SKalle Valo 				     u64 multicast)
1930e705c121SKalle Valo {
1931e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1932e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1933e705c121SKalle Valo 
1934e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1935e705c121SKalle Valo 
1936e705c121SKalle Valo 	/* replace previous configuration */
1937e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1938e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1939e705c121SKalle Valo 
1940e705c121SKalle Valo 	if (!cmd)
1941e705c121SKalle Valo 		goto out;
1942e705c121SKalle Valo 
194361e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
194461e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
194561e7d91bSLuca Coelho 
194661e7d91bSLuca Coelho 	if (cmd->pass_all)
194761e7d91bSLuca Coelho 		cmd->count = 0;
194861e7d91bSLuca Coelho 
1949e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1950e705c121SKalle Valo out:
1951e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1952e705c121SKalle Valo 	*total_flags = 0;
1953e705c121SKalle Valo }
1954e705c121SKalle Valo 
1955e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1956e705c121SKalle Valo 					struct ieee80211_vif *vif,
1957e705c121SKalle Valo 					unsigned int filter_flags,
1958e705c121SKalle Valo 					unsigned int changed_flags)
1959e705c121SKalle Valo {
1960e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1961e705c121SKalle Valo 
1962e705c121SKalle Valo 	/* We support only filter for probe requests */
1963e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1964e705c121SKalle Valo 		return;
1965e705c121SKalle Valo 
1966e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1967e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1968e705c121SKalle Valo 	    !vif->p2p)
1969e705c121SKalle Valo 		return;
1970e705c121SKalle Valo 
1971e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1972e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1973e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1974e705c121SKalle Valo }
1975e705c121SKalle Valo 
1976e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1977e705c121SKalle Valo struct iwl_bcast_iter_data {
1978e705c121SKalle Valo 	struct iwl_mvm *mvm;
1979e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd;
1980e705c121SKalle Valo 	u8 current_filter;
1981e705c121SKalle Valo };
1982e705c121SKalle Valo 
1983e705c121SKalle Valo static void
1984e705c121SKalle Valo iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1985e705c121SKalle Valo 			 const struct iwl_fw_bcast_filter *in_filter,
1986e705c121SKalle Valo 			 struct iwl_fw_bcast_filter *out_filter)
1987e705c121SKalle Valo {
1988e705c121SKalle Valo 	struct iwl_fw_bcast_filter_attr *attr;
1989e705c121SKalle Valo 	int i;
1990e705c121SKalle Valo 
1991e705c121SKalle Valo 	memcpy(out_filter, in_filter, sizeof(*out_filter));
1992e705c121SKalle Valo 
1993e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1994e705c121SKalle Valo 		attr = &out_filter->attrs[i];
1995e705c121SKalle Valo 
1996e705c121SKalle Valo 		if (!attr->mask)
1997e705c121SKalle Valo 			break;
1998e705c121SKalle Valo 
1999e705c121SKalle Valo 		switch (attr->reserved1) {
2000e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_IP):
2001e705c121SKalle Valo 			if (vif->bss_conf.arp_addr_cnt != 1) {
2002e705c121SKalle Valo 				attr->mask = 0;
2003e705c121SKalle Valo 				continue;
2004e705c121SKalle Valo 			}
2005e705c121SKalle Valo 
2006e705c121SKalle Valo 			attr->val = vif->bss_conf.arp_addr_list[0];
2007e705c121SKalle Valo 			break;
2008e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_MAC):
2009e705c121SKalle Valo 			attr->val = *(__be32 *)&vif->addr[2];
2010e705c121SKalle Valo 			break;
2011e705c121SKalle Valo 		default:
2012e705c121SKalle Valo 			break;
2013e705c121SKalle Valo 		}
2014e705c121SKalle Valo 		attr->reserved1 = 0;
2015e705c121SKalle Valo 		out_filter->num_attrs++;
2016e705c121SKalle Valo 	}
2017e705c121SKalle Valo }
2018e705c121SKalle Valo 
2019e705c121SKalle Valo static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
2020e705c121SKalle Valo 					  struct ieee80211_vif *vif)
2021e705c121SKalle Valo {
2022e705c121SKalle Valo 	struct iwl_bcast_iter_data *data = _data;
2023e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
2024e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd = data->cmd;
2025e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2026e705c121SKalle Valo 	struct iwl_fw_bcast_mac *bcast_mac;
2027e705c121SKalle Valo 	int i;
2028e705c121SKalle Valo 
2029e705c121SKalle Valo 	if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
2030e705c121SKalle Valo 		return;
2031e705c121SKalle Valo 
2032e705c121SKalle Valo 	bcast_mac = &cmd->macs[mvmvif->id];
2033e705c121SKalle Valo 
2034e705c121SKalle Valo 	/*
2035e705c121SKalle Valo 	 * enable filtering only for associated stations, but not for P2P
2036e705c121SKalle Valo 	 * Clients
2037e705c121SKalle Valo 	 */
2038e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
2039e705c121SKalle Valo 	    !vif->bss_conf.assoc)
2040e705c121SKalle Valo 		return;
2041e705c121SKalle Valo 
2042e705c121SKalle Valo 	bcast_mac->default_discard = 1;
2043e705c121SKalle Valo 
2044e705c121SKalle Valo 	/* copy all configured filters */
2045e705c121SKalle Valo 	for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
2046e705c121SKalle Valo 		/*
2047e705c121SKalle Valo 		 * Make sure we don't exceed our filters limit.
2048e705c121SKalle Valo 		 * if there is still a valid filter to be configured,
2049e705c121SKalle Valo 		 * be on the safe side and just allow bcast for this mac.
2050e705c121SKalle Valo 		 */
2051e705c121SKalle Valo 		if (WARN_ON_ONCE(data->current_filter >=
2052e705c121SKalle Valo 				 ARRAY_SIZE(cmd->filters))) {
2053e705c121SKalle Valo 			bcast_mac->default_discard = 0;
2054e705c121SKalle Valo 			bcast_mac->attached_filters = 0;
2055e705c121SKalle Valo 			break;
2056e705c121SKalle Valo 		}
2057e705c121SKalle Valo 
2058e705c121SKalle Valo 		iwl_mvm_set_bcast_filter(vif,
2059e705c121SKalle Valo 					 &mvm->bcast_filters[i],
2060e705c121SKalle Valo 					 &cmd->filters[data->current_filter]);
2061e705c121SKalle Valo 
2062e705c121SKalle Valo 		/* skip current filter if it contains no attributes */
2063e705c121SKalle Valo 		if (!cmd->filters[data->current_filter].num_attrs)
2064e705c121SKalle Valo 			continue;
2065e705c121SKalle Valo 
2066e705c121SKalle Valo 		/* attach the filter to current mac */
2067e705c121SKalle Valo 		bcast_mac->attached_filters |=
2068e705c121SKalle Valo 				cpu_to_le16(BIT(data->current_filter));
2069e705c121SKalle Valo 
2070e705c121SKalle Valo 		data->current_filter++;
2071e705c121SKalle Valo 	}
2072e705c121SKalle Valo }
2073e705c121SKalle Valo 
2074e705c121SKalle Valo bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
2075e705c121SKalle Valo 				    struct iwl_bcast_filter_cmd *cmd)
2076e705c121SKalle Valo {
2077e705c121SKalle Valo 	struct iwl_bcast_iter_data iter_data = {
2078e705c121SKalle Valo 		.mvm = mvm,
2079e705c121SKalle Valo 		.cmd = cmd,
2080e705c121SKalle Valo 	};
2081e705c121SKalle Valo 
2082e705c121SKalle Valo 	if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
2083e705c121SKalle Valo 		return false;
2084e705c121SKalle Valo 
2085e705c121SKalle Valo 	memset(cmd, 0, sizeof(*cmd));
2086e705c121SKalle Valo 	cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
2087e705c121SKalle Valo 	cmd->max_macs = ARRAY_SIZE(cmd->macs);
2088e705c121SKalle Valo 
2089e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
2090e705c121SKalle Valo 	/* use debugfs filters/macs if override is configured */
2091e705c121SKalle Valo 	if (mvm->dbgfs_bcast_filtering.override) {
2092e705c121SKalle Valo 		memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
2093e705c121SKalle Valo 		       sizeof(cmd->filters));
2094e705c121SKalle Valo 		memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
2095e705c121SKalle Valo 		       sizeof(cmd->macs));
2096e705c121SKalle Valo 		return true;
2097e705c121SKalle Valo 	}
2098e705c121SKalle Valo #endif
2099e705c121SKalle Valo 
2100e705c121SKalle Valo 	/* if no filters are configured, do nothing */
2101e705c121SKalle Valo 	if (!mvm->bcast_filters)
2102e705c121SKalle Valo 		return false;
2103e705c121SKalle Valo 
2104e705c121SKalle Valo 	/* configure and attach these filters for each associated sta vif */
2105e705c121SKalle Valo 	ieee80211_iterate_active_interfaces(
2106e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2107e705c121SKalle Valo 		iwl_mvm_bcast_filter_iterator, &iter_data);
2108e705c121SKalle Valo 
2109e705c121SKalle Valo 	return true;
2110e705c121SKalle Valo }
211134672bb3SEliad Peller 
211234672bb3SEliad Peller static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
2113e705c121SKalle Valo {
2114e705c121SKalle Valo 	struct iwl_bcast_filter_cmd cmd;
2115e705c121SKalle Valo 
2116e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
2117e705c121SKalle Valo 		return 0;
2118e705c121SKalle Valo 
2119e705c121SKalle Valo 	if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
2120e705c121SKalle Valo 		return 0;
2121e705c121SKalle Valo 
2122e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
2123e705c121SKalle Valo 				    sizeof(cmd), &cmd);
2124e705c121SKalle Valo }
2125e705c121SKalle Valo #else
212634672bb3SEliad Peller static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
2127e705c121SKalle Valo {
2128e705c121SKalle Valo 	return 0;
2129e705c121SKalle Valo }
2130e705c121SKalle Valo #endif
2131e705c121SKalle Valo 
2132a07a8f37SSara Sharon static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
2133a07a8f37SSara Sharon 				    struct ieee80211_vif *vif)
2134a07a8f37SSara Sharon {
2135a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
2136a07a8f37SSara Sharon 
2137a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
2138a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
2139a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
2140a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
2141a07a8f37SSara Sharon 
2142a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
2143a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
2144a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
2145a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
2146a07a8f37SSara Sharon }
2147a07a8f37SSara Sharon 
2148f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
2149f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
2150f92659a1SSara Sharon {
2151f92659a1SSara Sharon 	if (vif->mu_mimo_owner) {
2152f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
2153f92659a1SSara Sharon 
2154f92659a1SSara Sharon 		/*
2155f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
2156f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
2157f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
2158f92659a1SSara Sharon 		 */
2159f92659a1SSara Sharon 		ieee80211_update_mu_groups(vif,
2160f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
2161f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
2162f92659a1SSara Sharon 	}
2163f92659a1SSara Sharon }
2164f92659a1SSara Sharon 
2165f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
2166f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
2167f92659a1SSara Sharon {
2168f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2169f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
2170f92659a1SSara Sharon 
2171f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
2172f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2173f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
2174f92659a1SSara Sharon }
2175f92659a1SSara Sharon 
2176514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2177514c3069SLuca Coelho {
2178514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
2179514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
2180514c3069SLuca Coelho 	u8 residue_bits;
2181514c3069SLuca Coelho 	u8 res;
2182514c3069SLuca Coelho 
2183514c3069SLuca Coelho 	if (bit_num <= 5)
2184514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
2185514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2186514c3069SLuca Coelho 
2187514c3069SLuca Coelho 	/*
2188514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
2189514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
2190514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
2191514c3069SLuca Coelho 	 */
2192514c3069SLuca Coelho 
2193514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
2194514c3069SLuca Coelho 
2195514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
2196514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2197514c3069SLuca Coelho 	      residue_bits;
2198514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2199514c3069SLuca Coelho 
2200514c3069SLuca Coelho 	return res;
2201514c3069SLuca Coelho }
2202514c3069SLuca Coelho 
2203514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2204514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2205514c3069SLuca Coelho {
2206514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2207514c3069SLuca Coelho 	struct iwl_he_sta_context_cmd sta_ctxt_cmd = {
2208514c3069SLuca Coelho 		.sta_id = sta_id,
2209514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2210514c3069SLuca Coelho 		.bss_color = vif->bss_conf.bss_color,
2211514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2212514c3069SLuca Coelho 		.frame_time_rts_th =
2213514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2214514c3069SLuca Coelho 	};
2215d14ae796SSara Sharon 	int size = fw_has_api(&mvm->fw->ucode_capa,
2216d14ae796SSara Sharon 			      IWL_UCODE_TLV_API_MBSSID_HE) ?
2217d14ae796SSara Sharon 		   sizeof(sta_ctxt_cmd) :
2218d14ae796SSara Sharon 		   sizeof(struct iwl_he_sta_context_cmd_v1);
2219514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2220514c3069SLuca Coelho 	u32 flags;
2221514c3069SLuca Coelho 	int i;
2222514c3069SLuca Coelho 
2223514c3069SLuca Coelho 	rcu_read_lock();
2224514c3069SLuca Coelho 
2225514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2226514c3069SLuca Coelho 	if (IS_ERR(sta)) {
2227514c3069SLuca Coelho 		rcu_read_unlock();
2228514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2229514c3069SLuca Coelho 		return;
2230514c3069SLuca Coelho 	}
2231514c3069SLuca Coelho 
2232514c3069SLuca Coelho 	if (!sta->he_cap.has_he) {
2233514c3069SLuca Coelho 		rcu_read_unlock();
2234514c3069SLuca Coelho 		return;
2235514c3069SLuca Coelho 	}
2236514c3069SLuca Coelho 
2237514c3069SLuca Coelho 	flags = 0;
2238514c3069SLuca Coelho 
2239514c3069SLuca Coelho 	/* HTC flags */
2240514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[0] &
2241514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP0_HTC_HE)
2242514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2243514c3069SLuca Coelho 	if ((sta->he_cap.he_cap_elem.mac_cap_info[1] &
2244514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2245514c3069SLuca Coelho 	    (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2246514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2247514c3069SLuca Coelho 		u8 link_adap =
2248514c3069SLuca Coelho 			((sta->he_cap.he_cap_elem.mac_cap_info[2] &
2249514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2250514c3069SLuca Coelho 			 (sta->he_cap.he_cap_elem.mac_cap_info[1] &
2251514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2252514c3069SLuca Coelho 
2253514c3069SLuca Coelho 		if (link_adap == 2)
2254514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2255514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2256514c3069SLuca Coelho 		else if (link_adap == 3)
2257514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2258514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2259514c3069SLuca Coelho 	}
2260514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2261514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2262514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[3] &
2263514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2264514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2265514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2266514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2267514c3069SLuca Coelho 
2268189b8d44SNaftali Goldstein 	/*
2269189b8d44SNaftali Goldstein 	 * Initialize the PPE thresholds to "None" (7), as described in Table
2270189b8d44SNaftali Goldstein 	 * 9-262ac of 80211.ax/D3.0.
2271189b8d44SNaftali Goldstein 	 */
2272189b8d44SNaftali Goldstein 	memset(&sta_ctxt_cmd.pkt_ext, 7, sizeof(sta_ctxt_cmd.pkt_ext));
2273189b8d44SNaftali Goldstein 
2274189b8d44SNaftali Goldstein 	/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2275514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.phy_cap_info[6] &
2276514c3069SLuca Coelho 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2277514c3069SLuca Coelho 		u8 nss = (sta->he_cap.ppe_thres[0] &
2278514c3069SLuca Coelho 			  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2279514c3069SLuca Coelho 		u8 ru_index_bitmap =
2280514c3069SLuca Coelho 			(sta->he_cap.ppe_thres[0] &
2281514c3069SLuca Coelho 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2282514c3069SLuca Coelho 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2283514c3069SLuca Coelho 		u8 *ppe = &sta->he_cap.ppe_thres[0];
2284514c3069SLuca Coelho 		u8 ppe_pos_bit = 7; /* Starting after PPE header */
2285514c3069SLuca Coelho 
2286514c3069SLuca Coelho 		/*
2287514c3069SLuca Coelho 		 * FW currently supports only nss == MAX_HE_SUPP_NSS
2288514c3069SLuca Coelho 		 *
2289514c3069SLuca Coelho 		 * If nss > MAX: we can ignore values we don't support
2290514c3069SLuca Coelho 		 * If nss < MAX: we can set zeros in other streams
2291514c3069SLuca Coelho 		 */
2292514c3069SLuca Coelho 		if (nss > MAX_HE_SUPP_NSS) {
2293514c3069SLuca Coelho 			IWL_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2294514c3069SLuca Coelho 				 MAX_HE_SUPP_NSS);
2295514c3069SLuca Coelho 			nss = MAX_HE_SUPP_NSS;
2296514c3069SLuca Coelho 		}
2297514c3069SLuca Coelho 
2298514c3069SLuca Coelho 		for (i = 0; i < nss; i++) {
2299514c3069SLuca Coelho 			u8 ru_index_tmp = ru_index_bitmap << 1;
2300514c3069SLuca Coelho 			u8 bw;
2301514c3069SLuca Coelho 
2302514c3069SLuca Coelho 			for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX; bw++) {
2303514c3069SLuca Coelho 				ru_index_tmp >>= 1;
2304514c3069SLuca Coelho 				if (!(ru_index_tmp & 1))
2305514c3069SLuca Coelho 					continue;
2306514c3069SLuca Coelho 
2307514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][1] =
2308514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2309514c3069SLuca Coelho 							       ppe_pos_bit);
2310514c3069SLuca Coelho 				ppe_pos_bit +=
2311514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2312514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0] =
2313514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2314514c3069SLuca Coelho 							       ppe_pos_bit);
2315514c3069SLuca Coelho 				ppe_pos_bit +=
2316514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2317514c3069SLuca Coelho 			}
2318514c3069SLuca Coelho 		}
2319514c3069SLuca Coelho 
2320514c3069SLuca Coelho 		flags |= STA_CTXT_HE_PACKET_EXT;
232177ff2c6bSLiad Kaufman 	} else if ((sta->he_cap.he_cap_elem.phy_cap_info[9] &
232277ff2c6bSLiad Kaufman 		    IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) !=
232377ff2c6bSLiad Kaufman 		  IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_RESERVED) {
232477ff2c6bSLiad Kaufman 		int low_th = -1;
232577ff2c6bSLiad Kaufman 		int high_th = -1;
232677ff2c6bSLiad Kaufman 
232777ff2c6bSLiad Kaufman 		/* Take the PPE thresholds from the nominal padding info */
232877ff2c6bSLiad Kaufman 		switch (sta->he_cap.he_cap_elem.phy_cap_info[9] &
232977ff2c6bSLiad Kaufman 			IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) {
233077ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_0US:
233177ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
233277ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
233377ff2c6bSLiad Kaufman 			break;
233477ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_8US:
233577ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_BPSK;
233677ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
233777ff2c6bSLiad Kaufman 			break;
233877ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US:
233977ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
234077ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_BPSK;
234177ff2c6bSLiad Kaufman 			break;
234277ff2c6bSLiad Kaufman 		}
234377ff2c6bSLiad Kaufman 
234477ff2c6bSLiad Kaufman 		/* Set the PPE thresholds accordingly */
234577ff2c6bSLiad Kaufman 		if (low_th >= 0 && high_th >= 0) {
23460bfefe2fSLiad Kaufman 			struct iwl_he_pkt_ext *pkt_ext =
23470bfefe2fSLiad Kaufman 				(struct iwl_he_pkt_ext *)&sta_ctxt_cmd.pkt_ext;
234877ff2c6bSLiad Kaufman 
234977ff2c6bSLiad Kaufman 			for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
235077ff2c6bSLiad Kaufman 				u8 bw;
235177ff2c6bSLiad Kaufman 
235277ff2c6bSLiad Kaufman 				for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX;
235377ff2c6bSLiad Kaufman 				     bw++) {
23540bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][0] =
23550bfefe2fSLiad Kaufman 						low_th;
23560bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][1] =
23570bfefe2fSLiad Kaufman 						high_th;
235877ff2c6bSLiad Kaufman 				}
235977ff2c6bSLiad Kaufman 			}
236077ff2c6bSLiad Kaufman 
236177ff2c6bSLiad Kaufman 			flags |= STA_CTXT_HE_PACKET_EXT;
236277ff2c6bSLiad Kaufman 		}
2363514c3069SLuca Coelho 	}
2364514c3069SLuca Coelho 	rcu_read_unlock();
2365514c3069SLuca Coelho 
2366514c3069SLuca Coelho 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2367514c3069SLuca Coelho 	flags |= STA_CTXT_HE_MU_EDCA_CW;
2368514c3069SLuca Coelho 	for (i = 0; i < AC_NUM; i++) {
2369514c3069SLuca Coelho 		struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2370514c3069SLuca Coelho 			&mvmvif->queue_params[i].mu_edca_param_rec;
2371514c3069SLuca Coelho 
2372514c3069SLuca Coelho 		if (!mvmvif->queue_params[i].mu_edca) {
2373514c3069SLuca Coelho 			flags &= ~STA_CTXT_HE_MU_EDCA_CW;
2374514c3069SLuca Coelho 			break;
2375514c3069SLuca Coelho 		}
2376514c3069SLuca Coelho 
2377514c3069SLuca Coelho 		sta_ctxt_cmd.trig_based_txf[i].cwmin =
2378514c3069SLuca Coelho 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
2379514c3069SLuca Coelho 		sta_ctxt_cmd.trig_based_txf[i].cwmax =
2380514c3069SLuca Coelho 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
2381514c3069SLuca Coelho 		sta_ctxt_cmd.trig_based_txf[i].aifsn =
2382514c3069SLuca Coelho 			cpu_to_le16(mu_edca->aifsn);
2383514c3069SLuca Coelho 		sta_ctxt_cmd.trig_based_txf[i].mu_time =
2384514c3069SLuca Coelho 			cpu_to_le16(mu_edca->mu_edca_timer);
2385514c3069SLuca Coelho 	}
2386514c3069SLuca Coelho 
2387514c3069SLuca Coelho 	if (vif->bss_conf.multi_sta_back_32bit)
2388514c3069SLuca Coelho 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2389514c3069SLuca Coelho 
2390514c3069SLuca Coelho 	if (vif->bss_conf.ack_enabled)
2391514c3069SLuca Coelho 		flags |= STA_CTXT_HE_ACK_ENABLED;
2392514c3069SLuca Coelho 
2393514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2394514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2395514c3069SLuca Coelho 
2396514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2397514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2398514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2399514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2400514c3069SLuca Coelho 	}
2401514c3069SLuca Coelho 
2402918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2403918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2404918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2405918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2406d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2407d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2408d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2409d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2410d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2411d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2412918cbf39SSara Sharon 	}
2413514c3069SLuca Coelho 
2414514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2415514c3069SLuca Coelho 
2416514c3069SLuca Coelho 	if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(STA_HE_CTXT_CMD,
2417514c3069SLuca Coelho 						 DATA_PATH_GROUP, 0),
2418d14ae796SSara Sharon 				 0, size, &sta_ctxt_cmd))
2419514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2420514c3069SLuca Coelho }
2421514c3069SLuca Coelho 
2422e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2423e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2424e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
2425e705c121SKalle Valo 					     u32 changes)
2426e705c121SKalle Valo {
2427e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2428e705c121SKalle Valo 	int ret;
2429e705c121SKalle Valo 
2430e705c121SKalle Valo 	/*
2431e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
2432e705c121SKalle Valo 	 * beacon interval, which was not known when the station interface was
2433e705c121SKalle Valo 	 * added.
2434e705c121SKalle Valo 	 */
2435514c3069SLuca Coelho 	if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
2436230ba6c5SLuca Coelho 		if (vif->bss_conf.he_support &&
2437230ba6c5SLuca Coelho 		    !iwlwifi_mod_params.disable_11ax)
2438514c3069SLuca Coelho 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2439514c3069SLuca Coelho 
2440e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2441514c3069SLuca Coelho 	}
2442e705c121SKalle Valo 
244340ecdd01SShaul Triebitz 	/* Update MU EDCA params */
244440ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
244540ecdd01SShaul Triebitz 	    bss_conf->assoc && vif->bss_conf.he_support &&
244640ecdd01SShaul Triebitz 	    !iwlwifi_mod_params.disable_11ax)
244740ecdd01SShaul Triebitz 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
244840ecdd01SShaul Triebitz 
2449e705c121SKalle Valo 	/*
2450e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2451e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2452e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2453e705c121SKalle Valo 	 * branch for disassociation below.
2454e705c121SKalle Valo 	 */
2455e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2456e705c121SKalle Valo 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2457e705c121SKalle Valo 
2458e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
2459e705c121SKalle Valo 	if (ret)
2460e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2461e705c121SKalle Valo 
2462e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2463e705c121SKalle Valo 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2464e705c121SKalle Valo 	mvmvif->associated = bss_conf->assoc;
2465e705c121SKalle Valo 
2466e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2467e705c121SKalle Valo 		if (bss_conf->assoc) {
2468e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2469e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2470e705c121SKalle Valo 			memset(&mvmvif->beacon_stats, 0,
2471e705c121SKalle Valo 			       sizeof(mvmvif->beacon_stats));
2472e705c121SKalle Valo 
2473e705c121SKalle Valo 			/* add quota for this interface */
2474e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2475e705c121SKalle Valo 			if (ret) {
2476e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2477e705c121SKalle Valo 				return;
2478e705c121SKalle Valo 			}
2479e705c121SKalle Valo 
2480e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2481e705c121SKalle Valo 				     &mvm->status)) {
2482e705c121SKalle Valo 				/*
2483e705c121SKalle Valo 				 * If we're restarting then the firmware will
2484e705c121SKalle Valo 				 * obviously have lost synchronisation with
2485e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2486e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2487e705c121SKalle Valo 				 * scheduling a session protection time event.
2488e705c121SKalle Valo 				 *
2489e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2490e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2491e705c121SKalle Valo 				 * the beacon interval.
2492e705c121SKalle Valo 				 *
2493e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2494e705c121SKalle Valo 				 * than a single interface.
2495e705c121SKalle Valo 				 */
2496e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2497e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2498e705c121SKalle Valo 							5 * dur, false);
2499e705c121SKalle Valo 			}
2500e705c121SKalle Valo 
2501e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2502e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2503e705c121SKalle Valo 			if (vif->p2p) {
2504e705c121SKalle Valo 				iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
2505e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2506e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
2507e705c121SKalle Valo 						    IEEE80211_SMPS_DYNAMIC);
2508e705c121SKalle Valo 			}
25090ae98812SSara Sharon 		} else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
2510e705c121SKalle Valo 			/*
2511e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2512e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2513e705c121SKalle Valo 			 */
251469e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
251569e508b4SIlan Peer 			WARN_ONCE(ret &&
251669e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
251769e508b4SIlan Peer 					    &mvm->status),
2518e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2519e705c121SKalle Valo 
25206b28f978SEmmanuel Grumbach 			/*
25216b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
25226b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
25236b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
25246b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
25256b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
25266b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
25276b28f978SEmmanuel Grumbach 			 */
25286b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
25296b28f978SEmmanuel Grumbach 				      &mvm->status)) {
25306b28f978SEmmanuel Grumbach 				/*
25316b28f978SEmmanuel Grumbach 				 * Remove AP station now that
25326b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
25336b28f978SEmmanuel Grumbach 				 */
25346b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
25356b28f978SEmmanuel Grumbach 							mvmvif->ap_sta_id);
2536e705c121SKalle Valo 				if (ret)
25376b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
25386b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2539e705c121SKalle Valo 
2540e705c121SKalle Valo 				if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
25416b28f978SEmmanuel Grumbach 					mvm->d0i3_ap_sta_id =
25426b28f978SEmmanuel Grumbach 						IWL_MVM_INVALID_STA;
25430ae98812SSara Sharon 				mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
25446b28f978SEmmanuel Grumbach 			}
25456b28f978SEmmanuel Grumbach 
2546e705c121SKalle Valo 			/* remove quota for this interface */
2547e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2548e705c121SKalle Valo 			if (ret)
2549e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2550e705c121SKalle Valo 
2551e705c121SKalle Valo 			if (vif->p2p)
2552e705c121SKalle Valo 				iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
2553e705c121SKalle Valo 
2554e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2555e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2556e705c121SKalle Valo 			if (ret)
2557e705c121SKalle Valo 				IWL_ERR(mvm,
2558e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2559e705c121SKalle Valo 					vif->addr);
2560e705c121SKalle Valo 		}
2561e705c121SKalle Valo 
2562a07a8f37SSara Sharon 		/*
2563a07a8f37SSara Sharon 		 * The firmware tracks the MU-MIMO group on its own.
2564f92659a1SSara Sharon 		 * However, on HW restart we should restore this data.
2565a07a8f37SSara Sharon 		 */
2566a07a8f37SSara Sharon 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2567f92659a1SSara Sharon 		    (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
2568a07a8f37SSara Sharon 			ret = iwl_mvm_update_mu_groups(mvm, vif);
2569a07a8f37SSara Sharon 			if (ret)
2570a07a8f37SSara Sharon 				IWL_ERR(mvm,
2571a07a8f37SSara Sharon 					"failed to update VHT MU_MIMO groups\n");
2572a07a8f37SSara Sharon 		}
2573a07a8f37SSara Sharon 
2574e705c121SKalle Valo 		iwl_mvm_recalc_multicast(mvm);
257534672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2576e705c121SKalle Valo 
2577e705c121SKalle Valo 		/* reset rssi values */
2578e705c121SKalle Valo 		mvmvif->bf_data.ave_beacon_signal = 0;
2579e705c121SKalle Valo 
2580e705c121SKalle Valo 		iwl_mvm_bt_coex_vif_change(mvm);
2581e705c121SKalle Valo 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2582e705c121SKalle Valo 				    IEEE80211_SMPS_AUTOMATIC);
2583355346baSAvraham Stern 		if (fw_has_capa(&mvm->fw->ucode_capa,
2584355346baSAvraham Stern 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2585355346baSAvraham Stern 			iwl_mvm_config_scan(mvm);
2586b45242c9SAvraham Stern 	}
2587b45242c9SAvraham Stern 
2588b45242c9SAvraham Stern 	if (changes & BSS_CHANGED_BEACON_INFO) {
2589e705c121SKalle Valo 		/*
2590b45242c9SAvraham Stern 		 * We received a beacon from the associated AP so
2591e705c121SKalle Valo 		 * remove the session protection.
2592e705c121SKalle Valo 		 */
25933edfb5f4SAvraham Stern 		iwl_mvm_stop_session_protection(mvm, vif);
2594e705c121SKalle Valo 
2595e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2596e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2597e705c121SKalle Valo 	}
2598e705c121SKalle Valo 
2599283115fbSAvri Altman 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2600283115fbSAvri Altman 		       /*
2601283115fbSAvri Altman 			* Send power command on every beacon change,
2602283115fbSAvri Altman 			* because we may have not enabled beacon abort yet.
2603283115fbSAvri Altman 			*/
2604283115fbSAvri Altman 		       BSS_CHANGED_BEACON_INFO)) {
2605e705c121SKalle Valo 		ret = iwl_mvm_power_update_mac(mvm);
2606e705c121SKalle Valo 		if (ret)
2607e705c121SKalle Valo 			IWL_ERR(mvm, "failed to update power mode\n");
2608e705c121SKalle Valo 	}
2609e705c121SKalle Valo 
2610e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2611e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2612e705c121SKalle Valo 				bss_conf->txpower);
2613e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2614e705c121SKalle Valo 	}
2615e705c121SKalle Valo 
2616e705c121SKalle Valo 	if (changes & BSS_CHANGED_CQM) {
2617e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2618e705c121SKalle Valo 		/* reset cqm events tracking */
2619e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = 0;
2620e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
2621e705c121SKalle Valo 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2622e705c121SKalle Valo 			if (ret)
2623e705c121SKalle Valo 				IWL_ERR(mvm,
2624e705c121SKalle Valo 					"failed to update CQM thresholds\n");
2625e705c121SKalle Valo 		}
2626e705c121SKalle Valo 	}
2627e705c121SKalle Valo 
2628e705c121SKalle Valo 	if (changes & BSS_CHANGED_ARP_FILTER) {
2629e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
263034672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2631e705c121SKalle Valo 	}
2632e705c121SKalle Valo }
2633e705c121SKalle Valo 
2634e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2635e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2636e705c121SKalle Valo {
2637e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2638e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2639e705c121SKalle Valo 	int ret;
2640e705c121SKalle Valo 
2641e705c121SKalle Valo 	/*
2642e705c121SKalle Valo 	 * iwl_mvm_mac_ctxt_add() might read directly from the device
2643e705c121SKalle Valo 	 * (the system time), so make sure it is available.
2644e705c121SKalle Valo 	 */
2645e705c121SKalle Valo 	ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
2646e705c121SKalle Valo 	if (ret)
2647e705c121SKalle Valo 		return ret;
2648e705c121SKalle Valo 
2649e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2650e705c121SKalle Valo 
2651e705c121SKalle Valo 	/* Send the beacon template */
2652e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2653e705c121SKalle Valo 	if (ret)
2654e705c121SKalle Valo 		goto out_unlock;
2655e705c121SKalle Valo 
2656e705c121SKalle Valo 	/*
2657e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
2658e705c121SKalle Valo 	 * beacon interval, which was not known when the AP interface was added.
2659e705c121SKalle Valo 	 */
2660e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2661e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2662e705c121SKalle Valo 
2663e705c121SKalle Valo 	mvmvif->ap_assoc_sta_count = 0;
2664e705c121SKalle Valo 
2665e705c121SKalle Valo 	/* Add the mac context */
2666e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2667e705c121SKalle Valo 	if (ret)
2668e705c121SKalle Valo 		goto out_unlock;
2669e705c121SKalle Valo 
2670e705c121SKalle Valo 	/* Perform the binding */
2671e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2672e705c121SKalle Valo 	if (ret)
2673e705c121SKalle Valo 		goto out_remove;
2674e705c121SKalle Valo 
267563dd5d02SSara Sharon 	/*
267663dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
267763dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
267863dd5d02SSara Sharon 	 * cause assert 0x2b00.
267963dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
268063dd5d02SSara Sharon 	 * the TLV
268163dd5d02SSara Sharon 	 */
268263dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2683ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2684ced19f26SSara Sharon 		if (ret)
2685ced19f26SSara Sharon 			goto out_unbind;
268663dd5d02SSara Sharon 		/*
268763dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
268863dd5d02SSara Sharon 		 * events are added and applied to the scheduler
268963dd5d02SSara Sharon 		 */
2690e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
269163dd5d02SSara Sharon 		if (ret) {
269263dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
269363dd5d02SSara Sharon 			goto out_unbind;
269463dd5d02SSara Sharon 		}
269563dd5d02SSara Sharon 	} else {
269663dd5d02SSara Sharon 		/*
269763dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
269863dd5d02SSara Sharon 		 * events are added and applied to the scheduler
269963dd5d02SSara Sharon 		 */
270075fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2701e705c121SKalle Valo 		if (ret)
270263dd5d02SSara Sharon 			goto out_unbind;
270375fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
270463dd5d02SSara Sharon 		if (ret) {
270563dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
270663dd5d02SSara Sharon 			goto out_unbind;
270763dd5d02SSara Sharon 		}
270863dd5d02SSara Sharon 	}
270926d6c16bSSara Sharon 
2710e705c121SKalle Valo 	/* must be set before quota calculations */
2711e705c121SKalle Valo 	mvmvif->ap_ibss_active = true;
2712e705c121SKalle Valo 
271347242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
271447242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, true,
271547242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
271647242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
271747242744STova Mussai 	}
271847242744STova Mussai 
2719e705c121SKalle Valo 	/* power updated needs to be done before quotas */
2720e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2721e705c121SKalle Valo 
2722e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2723e705c121SKalle Valo 	if (ret)
2724e705c121SKalle Valo 		goto out_quota_failed;
2725e705c121SKalle Valo 
2726e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2727e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2728e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2729e705c121SKalle Valo 
2730e705c121SKalle Valo 	iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
2731e705c121SKalle Valo 
2732e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2733e705c121SKalle Valo 
2734e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2735e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2736e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2737e705c121SKalle Valo 
2738b73f9a4aSJohannes Berg 	iwl_mvm_ftm_restart_responder(mvm, vif);
2739b73f9a4aSJohannes Berg 
2740e705c121SKalle Valo 	goto out_unlock;
2741e705c121SKalle Valo 
2742e705c121SKalle Valo out_quota_failed:
2743e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2744e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2745e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2746ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2747e705c121SKalle Valo out_unbind:
2748e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2749e705c121SKalle Valo out_remove:
2750e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2751e705c121SKalle Valo out_unlock:
2752e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2753e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
2754e705c121SKalle Valo 	return ret;
2755e705c121SKalle Valo }
2756e705c121SKalle Valo 
2757e705c121SKalle Valo static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2758e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2759e705c121SKalle Valo {
2760e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2761e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2762e705c121SKalle Valo 
2763e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
2764e705c121SKalle Valo 
2765e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2766e705c121SKalle Valo 
2767e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2768e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2769e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2770e705c121SKalle Valo 					  &mvmvif->time_event_data);
2771e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2772e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2773e705c121SKalle Valo 	}
2774e705c121SKalle Valo 
2775e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2776e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2777e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2778e705c121SKalle Valo 	}
2779e705c121SKalle Valo 
2780e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2781e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2782e705c121SKalle Valo 
278347242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
278447242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
278547242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
278647242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
278747242744STova Mussai 	}
278847242744STova Mussai 
2789e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2790e705c121SKalle Valo 
2791e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
2792e705c121SKalle Valo 
2793e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2794e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2795e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2796e705c121SKalle Valo 
2797e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2798ced19f26SSara Sharon 
2799ced19f26SSara Sharon 	/*
2800ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2801ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2802ced19f26SSara Sharon 	 * cause assert 0x2b00.
2803ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2804ced19f26SSara Sharon 	 * bcast station).
2805ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2806ced19f26SSara Sharon 	 */
2807ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
280826d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2809e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2810ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2811ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2812e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2813e705c121SKalle Valo 
2814e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2815e705c121SKalle Valo 
2816e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2817e705c121SKalle Valo 
2818e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2819e705c121SKalle Valo }
2820e705c121SKalle Valo 
2821e705c121SKalle Valo static void
2822e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2823e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2824e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
2825e705c121SKalle Valo 				 u32 changes)
2826e705c121SKalle Valo {
2827e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2828e705c121SKalle Valo 
2829e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
2830e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
2831e705c121SKalle Valo 		return;
2832e705c121SKalle Valo 
2833e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2834e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2835e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2836e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2837e705c121SKalle Valo 
2838e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
2839e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
2840e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2841e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
2842e705c121SKalle Valo 
2843e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2844e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2845e705c121SKalle Valo 				bss_conf->txpower);
2846e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2847e705c121SKalle Valo 	}
2848b73f9a4aSJohannes Berg 
2849b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
2850b73f9a4aSJohannes Berg 		int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2851b73f9a4aSJohannes Berg 
2852b73f9a4aSJohannes Berg 		if (ret)
2853b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2854b73f9a4aSJohannes Berg 				 ret);
2855b73f9a4aSJohannes Berg 	}
2856b73f9a4aSJohannes Berg 
2857e705c121SKalle Valo }
2858e705c121SKalle Valo 
2859e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2860e705c121SKalle Valo 				     struct ieee80211_vif *vif,
2861e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
2862e705c121SKalle Valo 				     u32 changes)
2863e705c121SKalle Valo {
2864e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2865e705c121SKalle Valo 
2866e705c121SKalle Valo 	/*
2867e705c121SKalle Valo 	 * iwl_mvm_bss_info_changed_station() might call
2868e705c121SKalle Valo 	 * iwl_mvm_protect_session(), which reads directly from
2869e705c121SKalle Valo 	 * the device (the system time), so make sure it is available.
2870e705c121SKalle Valo 	 */
2871e705c121SKalle Valo 	if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
2872e705c121SKalle Valo 		return;
2873e705c121SKalle Valo 
2874e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2875e705c121SKalle Valo 
2876e705c121SKalle Valo 	if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2877e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2878e705c121SKalle Valo 
2879e705c121SKalle Valo 	switch (vif->type) {
2880e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2881e705c121SKalle Valo 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2882e705c121SKalle Valo 		break;
2883e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
2884e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
2885e705c121SKalle Valo 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2886e705c121SKalle Valo 		break;
288791b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
288891b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
288991b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
289091b08c2dSAviya Erenfeld 		break;
2891e705c121SKalle Valo 	default:
2892e705c121SKalle Valo 		/* shouldn't happen */
2893e705c121SKalle Valo 		WARN_ON_ONCE(1);
2894e705c121SKalle Valo 	}
2895e705c121SKalle Valo 
2896e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2897e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
2898e705c121SKalle Valo }
2899e705c121SKalle Valo 
2900e705c121SKalle Valo static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2901e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2902e705c121SKalle Valo 			       struct ieee80211_scan_request *hw_req)
2903e705c121SKalle Valo {
2904e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2905e705c121SKalle Valo 	int ret;
2906e705c121SKalle Valo 
2907e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
2908e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2909e705c121SKalle Valo 		return -EINVAL;
2910e705c121SKalle Valo 
2911e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2912e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2913e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2914e705c121SKalle Valo 
2915e705c121SKalle Valo 	return ret;
2916e705c121SKalle Valo }
2917e705c121SKalle Valo 
2918e705c121SKalle Valo static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2919e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2920e705c121SKalle Valo {
2921e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2922e705c121SKalle Valo 
2923e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2924e705c121SKalle Valo 
2925e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2926e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
2927e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
2928e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
2929e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
2930e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
2931e705c121SKalle Valo 	*/
2932e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2933e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2934e705c121SKalle Valo 
2935e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2936e705c121SKalle Valo }
2937e705c121SKalle Valo 
2938e705c121SKalle Valo static void
2939e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2940e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
2941e705c121SKalle Valo 				  int num_frames,
2942e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
2943e705c121SKalle Valo 				  bool more_data)
2944e705c121SKalle Valo {
2945e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2946e705c121SKalle Valo 
2947e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
2948e705c121SKalle Valo 
2949e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2950e705c121SKalle Valo 					  tids, more_data, false);
2951e705c121SKalle Valo }
2952e705c121SKalle Valo 
2953e705c121SKalle Valo static void
2954e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2955e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
2956e705c121SKalle Valo 				    int num_frames,
2957e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
2958e705c121SKalle Valo 				    bool more_data)
2959e705c121SKalle Valo {
2960e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2961e705c121SKalle Valo 
29629a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2963e705c121SKalle Valo 
2964e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2965e705c121SKalle Valo 					  tids, more_data, true);
2966e705c121SKalle Valo }
2967e705c121SKalle Valo 
296865e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2969e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
2970e705c121SKalle Valo 				     struct ieee80211_sta *sta)
2971e705c121SKalle Valo {
2972e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2973e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2974e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
2975e705c121SKalle Valo 	int tid;
2976e705c121SKalle Valo 
2977960f864bSJohannes Berg 	/*
2978960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
2979960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
2980960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
2981960f864bSJohannes Berg 	 */
2982960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2983960f864bSJohannes Berg 		return;
2984960f864bSJohannes Berg 
2985e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2986311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
2987e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2988e705c121SKalle Valo 
29896862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
29901c17627bSSara Sharon 			continue;
29911c17627bSSara Sharon 
2992e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
2993e705c121SKalle Valo 
2994dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
2995e705c121SKalle Valo 			continue;
2996e705c121SKalle Valo 
2997e705c121SKalle Valo 		__set_bit(tid, &tids);
2998e705c121SKalle Valo 	}
2999e705c121SKalle Valo 
3000e705c121SKalle Valo 	switch (cmd) {
3001e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
3002e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3003e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
3004e705c121SKalle Valo 
3005e705c121SKalle Valo 		if (txqs)
3006e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3007e705c121SKalle Valo 		/*
3008e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3009e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
3010e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3011e705c121SKalle Valo 		 */
3012e705c121SKalle Valo 		break;
3013e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
30140ae98812SSara Sharon 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
3015e705c121SKalle Valo 			break;
3016e705c121SKalle Valo 
3017e705c121SKalle Valo 		if (txqs)
3018e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3019e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3020e705c121SKalle Valo 		break;
3021e705c121SKalle Valo 	default:
3022e705c121SKalle Valo 		break;
3023e705c121SKalle Valo 	}
3024e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3025e705c121SKalle Valo }
3026e705c121SKalle Valo 
302765e25482SJohannes Berg static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
302865e25482SJohannes Berg 				   struct ieee80211_vif *vif,
302965e25482SJohannes Berg 				   enum sta_notify_cmd cmd,
303065e25482SJohannes Berg 				   struct ieee80211_sta *sta)
303165e25482SJohannes Berg {
303265e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
303365e25482SJohannes Berg }
303465e25482SJohannes Berg 
303565e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
303665e25482SJohannes Berg {
303765e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
303865e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
303965e25482SJohannes Berg 	struct ieee80211_sta *sta;
304065e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
304165e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
304265e25482SJohannes Berg 
304365e25482SJohannes Berg 	if (WARN_ON(notif->sta_id >= ARRAY_SIZE(mvm->fw_id_to_mac_id)))
304465e25482SJohannes Berg 		return;
304565e25482SJohannes Berg 
304665e25482SJohannes Berg 	rcu_read_lock();
3047a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
304865e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
304965e25482SJohannes Berg 		rcu_read_unlock();
305065e25482SJohannes Berg 		return;
305165e25482SJohannes Berg 	}
305265e25482SJohannes Berg 
305365e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
305465e25482SJohannes Berg 
305565e25482SJohannes Berg 	if (!mvmsta->vif ||
305665e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
305765e25482SJohannes Berg 		rcu_read_unlock();
305865e25482SJohannes Berg 		return;
305965e25482SJohannes Berg 	}
306065e25482SJohannes Berg 
306165e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
306265e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
306365e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
306465e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
306565e25482SJohannes Berg 			sta);
306665e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
306765e25482SJohannes Berg 	}
306865e25482SJohannes Berg 
306965e25482SJohannes Berg 	if (sleeping) {
307065e25482SJohannes Berg 		switch (notif->type) {
307165e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
307265e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
307365e25482SJohannes Berg 			break;
307465e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
307565e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
307665e25482SJohannes Berg 			break;
307765e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
307865e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
307965e25482SJohannes Berg 			break;
308065e25482SJohannes Berg 		default:
308165e25482SJohannes Berg 			break;
308265e25482SJohannes Berg 		}
308365e25482SJohannes Berg 	}
308465e25482SJohannes Berg 
308565e25482SJohannes Berg 	rcu_read_unlock();
308665e25482SJohannes Berg }
308765e25482SJohannes Berg 
3088e705c121SKalle Valo static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3089e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3090e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3091e705c121SKalle Valo {
3092e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3093e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3094e705c121SKalle Valo 
3095e705c121SKalle Valo 	/*
3096e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
3097e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
3098e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
3099e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
3100e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
3101e705c121SKalle Valo 	 * callback deleted the station.
3102e705c121SKalle Valo 	 */
3103e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3104e705c121SKalle Valo 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
3105e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
3106e705c121SKalle Valo 				   ERR_PTR(-ENOENT));
3107e705c121SKalle Valo 
3108e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3109e705c121SKalle Valo }
3110e705c121SKalle Valo 
3111e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3112e705c121SKalle Valo 				const u8 *bssid)
3113e705c121SKalle Valo {
3114b0ffe455SJohannes Berg 	int i;
3115b0ffe455SJohannes Berg 
3116b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3117b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
3118b0ffe455SJohannes Berg 
3119b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3120b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3121b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
3122b0ffe455SJohannes Berg 	}
3123b0ffe455SJohannes Berg 
3124e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3125e705c121SKalle Valo 		return;
3126e705c121SKalle Valo 
3127c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3128cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3129cee5a882SAvri Altman 		return;
3130cee5a882SAvri Altman 	}
3131cee5a882SAvri Altman 
313211dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
313311dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3134e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3135e705c121SKalle Valo 		return;
3136e705c121SKalle Valo 	}
3137e705c121SKalle Valo 
3138b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3139b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3140b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3141b0ffe455SJohannes Berg 			return;
3142b0ffe455SJohannes Berg 		}
3143b0ffe455SJohannes Berg 	}
3144b0ffe455SJohannes Berg 
3145e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3146e705c121SKalle Valo }
3147e705c121SKalle Valo 
31481e8f1329SGolan Ben-Ami static void
31491e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
31501e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
31511e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
31521e8f1329SGolan Ben-Ami {
31531e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
31541e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
31551e8f1329SGolan Ben-Ami 
31566c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
31576c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
31586c042d75SSara Sharon 	if (!trig)
31591e8f1329SGolan Ben-Ami 		return;
31601e8f1329SGolan Ben-Ami 
31611e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
31621e8f1329SGolan Ben-Ami 
31631e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
31641e8f1329SGolan Ben-Ami 		return;
31651e8f1329SGolan Ben-Ami 
31661e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
31671e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
31681e8f1329SGolan Ben-Ami 		return;
31691e8f1329SGolan Ben-Ami 
31707174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
31711e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
31721e8f1329SGolan Ben-Ami 				peer_addr, action);
31731e8f1329SGolan Ben-Ami }
31741e8f1329SGolan Ben-Ami 
3175e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3176e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3177e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3178e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3179e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3180e705c121SKalle Valo {
3181e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3182e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
31836ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3184e705c121SKalle Valo 	int ret;
3185e705c121SKalle Valo 
3186e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3187e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3188e705c121SKalle Valo 
3189e705c121SKalle Valo 	/* this would be a mac80211 bug ... but don't crash */
3190e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
3191e705c121SKalle Valo 		return -EINVAL;
3192e705c121SKalle Valo 
319324afba76SLiad Kaufman 	/*
319424afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
319524afba76SLiad Kaufman 	 *
319624afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
319724afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
319824afba76SLiad Kaufman 	 * the queues.
319924afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
320024afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
320124afba76SLiad Kaufman 	 * of.
320224afba76SLiad Kaufman 	 *
320324afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
320424afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
320524afba76SLiad Kaufman 	 */
320624afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3207c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
320824afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
320924afba76SLiad Kaufman 
321024afba76SLiad Kaufman 		/*
321124afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
321224afba76SLiad Kaufman 		 * worker will already remove it if it was on
321324afba76SLiad Kaufman 		 */
321424afba76SLiad Kaufman 	}
321524afba76SLiad Kaufman 
3216e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
32176ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3218d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
32196ea29ce5SJohannes Berg 
3220e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3221e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
3222e705c121SKalle Valo 		/*
3223e705c121SKalle Valo 		 * Firmware bug - it'll crash if the beacon interval is less
3224e705c121SKalle Valo 		 * than 16. We can't avoid connecting at all, so refuse the
3225e705c121SKalle Valo 		 * station state change, this will cause mac80211 to abandon
3226e705c121SKalle Valo 		 * attempts to connect to this AP, and eventually wpa_s will
3227e705c121SKalle Valo 		 * blacklist the AP...
3228e705c121SKalle Valo 		 */
3229e705c121SKalle Valo 		if (vif->type == NL80211_IFTYPE_STATION &&
3230e705c121SKalle Valo 		    vif->bss_conf.beacon_int < 16) {
3231e705c121SKalle Valo 			IWL_ERR(mvm,
3232e705c121SKalle Valo 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3233e705c121SKalle Valo 				sta->addr, vif->bss_conf.beacon_int);
3234e705c121SKalle Valo 			ret = -EINVAL;
3235e705c121SKalle Valo 			goto out_unlock;
3236e705c121SKalle Valo 		}
3237e705c121SKalle Valo 
3238e705c121SKalle Valo 		if (sta->tdls &&
3239e705c121SKalle Valo 		    (vif->p2p ||
3240e705c121SKalle Valo 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
3241e705c121SKalle Valo 						IWL_MVM_TDLS_STA_COUNT ||
3242e705c121SKalle Valo 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3243e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3244e705c121SKalle Valo 			ret = -EBUSY;
3245e705c121SKalle Valo 			goto out_unlock;
3246e705c121SKalle Valo 		}
3247e705c121SKalle Valo 
3248e705c121SKalle Valo 		ret = iwl_mvm_add_sta(mvm, vif, sta);
32491e8f1329SGolan Ben-Ami 		if (sta->tdls && ret == 0) {
3250e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
32511e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
32521e8f1329SGolan Ben-Ami 						   NL80211_TDLS_SETUP);
32531e8f1329SGolan Ben-Ami 		}
3254438af969SSara Sharon 
3255438af969SSara Sharon 		sta->max_rc_amsdu_len = 1;
3256e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3257e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3258e705c121SKalle Valo 		/*
3259e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3260e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3261e705c121SKalle Valo 		 */
3262e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3263e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3264e705c121SKalle Valo 		ret = 0;
3265e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3266e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
32678be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
32689394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
32698be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count++;
32708be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
327102221a81SShaul Triebitz 			if (vif->bss_conf.he_support &&
327202221a81SShaul Triebitz 			    !iwlwifi_mod_params.disable_11ax)
327302221a81SShaul Triebitz 				iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
32749394662aSLiad Kaufman 		} else if (vif->type == NL80211_IFTYPE_STATION) {
32759394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
32769394662aSLiad Kaufman 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
32778be30c13SAyala Beker 		}
3278735a0045SGoodstein, Mordechay 
32793baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
32803baf7528SAvraham Stern 				     false);
3281735a0045SGoodstein, Mordechay 		ret = iwl_mvm_update_sta(mvm, vif, sta);
3282e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3283e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
328428916a16SEmmanuel Grumbach 		ret = 0;
3285e705c121SKalle Valo 
3286e705c121SKalle Valo 		/* we don't support TDLS during DCM */
3287e705c121SKalle Valo 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
3288e705c121SKalle Valo 			iwl_mvm_teardown_tdls_peers(mvm);
3289e705c121SKalle Valo 
32901e8f1329SGolan Ben-Ami 		if (sta->tdls)
32911e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
32921e8f1329SGolan Ben-Ami 						   NL80211_TDLS_ENABLE_LINK);
32931e8f1329SGolan Ben-Ami 
3294e705c121SKalle Valo 		/* enable beacon filtering */
3295e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
32966b7a5aeaSNaftali Goldstein 
32973baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
32983baf7528SAvraham Stern 				     true);
3299e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3300e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
3301e705c121SKalle Valo 		/* disable beacon filtering */
330269e508b4SIlan Peer 		ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
330369e508b4SIlan Peer 		WARN_ON(ret &&
330469e508b4SIlan Peer 			!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
330569e508b4SIlan Peer 				  &mvm->status));
3306e705c121SKalle Valo 		ret = 0;
3307e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3308e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
33098be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
33108be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
33118be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
33128be30c13SAyala Beker 		}
3313e705c121SKalle Valo 		ret = 0;
3314e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3315e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3316e705c121SKalle Valo 		ret = 0;
3317e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3318e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
3319e705c121SKalle Valo 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
33201e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3321e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
33221e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
33231e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
33241e8f1329SGolan Ben-Ami 		}
332534a880d8SLiad Kaufman 
332644135b7cSIlan Peer 		if (unlikely(ret &&
332744135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
332844135b7cSIlan Peer 				      &mvm->status)))
332944135b7cSIlan Peer 			ret = 0;
3330e705c121SKalle Valo 	} else {
3331e705c121SKalle Valo 		ret = -EIO;
3332e705c121SKalle Valo 	}
3333e705c121SKalle Valo  out_unlock:
3334e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3335e705c121SKalle Valo 
3336e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3337e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3338e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3339e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3340e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3341e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3342e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3343e705c121SKalle Valo 	}
3344e705c121SKalle Valo 
3345e705c121SKalle Valo 	return ret;
3346e705c121SKalle Valo }
3347e705c121SKalle Valo 
3348e705c121SKalle Valo static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3349e705c121SKalle Valo {
3350e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3351e705c121SKalle Valo 
3352e705c121SKalle Valo 	mvm->rts_threshold = value;
3353e705c121SKalle Valo 
3354e705c121SKalle Valo 	return 0;
3355e705c121SKalle Valo }
3356e705c121SKalle Valo 
3357e705c121SKalle Valo static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3358e705c121SKalle Valo 				  struct ieee80211_vif *vif,
3359e705c121SKalle Valo 				  struct ieee80211_sta *sta, u32 changed)
3360e705c121SKalle Valo {
3361e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3362dcfe3b10SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3363dcfe3b10SJohannes Berg 
3364dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
3365dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3366dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
3367dcfe3b10SJohannes Berg 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3368dcfe3b10SJohannes Berg 				     true);
3369e705c121SKalle Valo 
3370e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
3371e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
3372e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
3373e705c121SKalle Valo }
3374e705c121SKalle Valo 
3375e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3376e705c121SKalle Valo 			       struct ieee80211_vif *vif, u16 ac,
3377e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
3378e705c121SKalle Valo {
3379e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3380e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3381e705c121SKalle Valo 
3382e705c121SKalle Valo 	mvmvif->queue_params[ac] = *params;
3383e705c121SKalle Valo 
3384e705c121SKalle Valo 	/*
3385e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
3386e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
3387e705c121SKalle Valo 	 */
3388e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3389e705c121SKalle Valo 		int ret;
3390e705c121SKalle Valo 
3391e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
3392e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3393e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3394e705c121SKalle Valo 		return ret;
3395e705c121SKalle Valo 	}
3396e705c121SKalle Valo 	return 0;
3397e705c121SKalle Valo }
3398e705c121SKalle Valo 
3399e705c121SKalle Valo static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3400d4e36e55SIlan Peer 				       struct ieee80211_vif *vif,
3401d4e36e55SIlan Peer 				       u16 req_duration)
3402e705c121SKalle Valo {
3403e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
34047c70fee5SSara Sharon 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
34057c70fee5SSara Sharon 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
3406e705c121SKalle Valo 
3407e705c121SKalle Valo 	/*
3408e705c121SKalle Valo 	 * iwl_mvm_protect_session() reads directly from the device
3409e705c121SKalle Valo 	 * (the system time), so make sure it is available.
3410e705c121SKalle Valo 	 */
3411e705c121SKalle Valo 	if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
3412e705c121SKalle Valo 		return;
3413e705c121SKalle Valo 
3414d4e36e55SIlan Peer 	if (req_duration > duration)
3415d4e36e55SIlan Peer 		duration = req_duration;
3416d4e36e55SIlan Peer 
3417e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3418e705c121SKalle Valo 	/* Try really hard to protect the session and hear a beacon */
3419e705c121SKalle Valo 	iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500, false);
3420e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3421e705c121SKalle Valo 
3422e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
3423e705c121SKalle Valo }
3424e705c121SKalle Valo 
3425e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3426e705c121SKalle Valo 					struct ieee80211_vif *vif,
3427e705c121SKalle Valo 					struct cfg80211_sched_scan_request *req,
3428e705c121SKalle Valo 					struct ieee80211_scan_ies *ies)
3429e705c121SKalle Valo {
3430e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3431e705c121SKalle Valo 
3432e705c121SKalle Valo 	int ret;
3433e705c121SKalle Valo 
3434e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3435e705c121SKalle Valo 
3436e705c121SKalle Valo 	if (!vif->bss_conf.idle) {
3437e705c121SKalle Valo 		ret = -EBUSY;
3438e705c121SKalle Valo 		goto out;
3439e705c121SKalle Valo 	}
3440e705c121SKalle Valo 
3441e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
3442e705c121SKalle Valo 
3443e705c121SKalle Valo out:
3444e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3445e705c121SKalle Valo 	return ret;
3446e705c121SKalle Valo }
3447e705c121SKalle Valo 
3448e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3449e705c121SKalle Valo 				       struct ieee80211_vif *vif)
3450e705c121SKalle Valo {
3451e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3452e705c121SKalle Valo 	int ret;
3453e705c121SKalle Valo 
3454e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3455e705c121SKalle Valo 
3456e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3457e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
3458e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
3459e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
3460e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3461e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
3462e705c121SKalle Valo 	 * not running.
3463e705c121SKalle Valo 	*/
3464e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
3465e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3466e705c121SKalle Valo 		return 0;
3467e705c121SKalle Valo 	}
3468e705c121SKalle Valo 
3469e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
3470e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3471e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
3472e705c121SKalle Valo 
3473e705c121SKalle Valo 	return ret;
3474e705c121SKalle Valo }
3475e705c121SKalle Valo 
3476e705c121SKalle Valo static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3477e705c121SKalle Valo 			       enum set_key_cmd cmd,
3478e705c121SKalle Valo 			       struct ieee80211_vif *vif,
3479e705c121SKalle Valo 			       struct ieee80211_sta *sta,
3480e705c121SKalle Valo 			       struct ieee80211_key_conf *key)
3481e705c121SKalle Valo {
3482e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3483f5e28eacSJohannes Berg 	struct iwl_mvm_sta *mvmsta;
3484f5e28eacSJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn;
3485f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
3486e705c121SKalle Valo 	int ret;
34874615fd15SEmmanuel Grumbach 	u8 key_offset;
3488e705c121SKalle Valo 
34893b37f4c9SJohannes Berg 	if (iwlwifi_mod_params.swcrypto) {
3490e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
3491e705c121SKalle Valo 		return -EOPNOTSUPP;
3492e705c121SKalle Valo 	}
3493e705c121SKalle Valo 
3494e705c121SKalle Valo 	switch (key->cipher) {
3495e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
34967f768ad5SDavid Spinadel 		if (!mvm->trans->cfg->gen2) {
3497e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
34981ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
34997f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
35007f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
35017f768ad5SDavid Spinadel 		} else {
35027f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
35037f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
35047f768ad5SDavid Spinadel 		}
3505e705c121SKalle Valo 		break;
3506e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
35072a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
35082a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
350985aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
3510e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3511e705c121SKalle Valo 		break;
3512e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
35138e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
35148e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3515e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
3516e705c121SKalle Valo 		break;
3517e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3518e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3519475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
3520e705c121SKalle Valo 			break;
3521475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
3522475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
3523475c6bdeSJohannes Berg 		/* support HW crypto on TX */
3524475c6bdeSJohannes Berg 		return 0;
3525e705c121SKalle Valo 	default:
3526e705c121SKalle Valo 		/* currently FW supports only one optional cipher scheme */
3527e705c121SKalle Valo 		if (hw->n_cipher_schemes &&
3528e705c121SKalle Valo 		    hw->cipher_schemes->cipher == key->cipher)
3529e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3530e705c121SKalle Valo 		else
3531e705c121SKalle Valo 			return -EOPNOTSUPP;
3532e705c121SKalle Valo 	}
3533e705c121SKalle Valo 
3534e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3535e705c121SKalle Valo 
3536e705c121SKalle Valo 	switch (cmd) {
3537e705c121SKalle Valo 	case SET_KEY:
3538e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
3539e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
3540e705c121SKalle Valo 			/*
3541e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
3542e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
3543e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
35448e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
3545e705c121SKalle Valo 			 */
35468e160ab8SAyala Beker 			if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
35478e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
35488e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
354981279c49SJohannes Berg 				ret = -EOPNOTSUPP;
355081279c49SJohannes Berg 			else
3551e705c121SKalle Valo 				ret = 0;
355285aeb58cSDavid Spinadel 
355385aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
355485aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
355585aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
3556e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
3557e705c121SKalle Valo 				break;
3558e705c121SKalle Valo 			}
355985aeb58cSDavid Spinadel 		}
3560e705c121SKalle Valo 
3561e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
3562e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
3563e705c121SKalle Valo 		 */
3564e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3565e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
3566e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
3567e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
3568e705c121SKalle Valo 			ret = 0;
3569e705c121SKalle Valo 			break;
3570e705c121SKalle Valo 		}
3571e705c121SKalle Valo 
3572f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3573f5e28eacSJohannes Berg 		    sta && iwl_mvm_has_new_rx_api(mvm) &&
3574f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3575f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
35762a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
35772a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3578f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
3579f5e28eacSJohannes Berg 			int tid, q;
3580f5e28eacSJohannes Berg 
3581f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3582f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3583acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
3584acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
3585f5e28eacSJohannes Berg 					 GFP_KERNEL);
3586f5e28eacSJohannes Berg 			if (!ptk_pn) {
3587f5e28eacSJohannes Berg 				ret = -ENOMEM;
3588f5e28eacSJohannes Berg 				break;
3589f5e28eacSJohannes Berg 			}
3590f5e28eacSJohannes Berg 
3591f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3592f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
3593f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
3594f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
3595f5e28eacSJohannes Berg 					       seq.ccmp.pn,
3596f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
3597f5e28eacSJohannes Berg 			}
3598f5e28eacSJohannes Berg 
3599f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3600f5e28eacSJohannes Berg 		}
3601f5e28eacSJohannes Berg 
36024615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
36034615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
36044615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
36054615fd15SEmmanuel Grumbach 		else
36064615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
36074615fd15SEmmanuel Grumbach 
3608e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
36094615fd15SEmmanuel Grumbach 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
3610e705c121SKalle Valo 		if (ret) {
3611e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
3612475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
3613e705c121SKalle Valo 			/*
3614e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
3615475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
3616475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
3617475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
3618e705c121SKalle Valo 			 */
3619475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
3620475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
3621475c6bdeSJohannes Berg 			else
3622e705c121SKalle Valo 				ret = 0;
3623e705c121SKalle Valo 		}
3624e705c121SKalle Valo 
3625e705c121SKalle Valo 		break;
3626e705c121SKalle Valo 	case DISABLE_KEY:
3627e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3628e705c121SKalle Valo 			ret = 0;
3629e705c121SKalle Valo 			break;
3630e705c121SKalle Valo 		}
3631e705c121SKalle Valo 
3632f5e28eacSJohannes Berg 		if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3633f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3634f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
36352a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
36362a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3637f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3638f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
3639f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
3640f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
3641f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3642f5e28eacSJohannes Berg 			if (ptk_pn)
3643f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
3644f5e28eacSJohannes Berg 		}
3645f5e28eacSJohannes Berg 
3646e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3647e705c121SKalle Valo 		ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3648e705c121SKalle Valo 		break;
3649e705c121SKalle Valo 	default:
3650e705c121SKalle Valo 		ret = -EINVAL;
3651e705c121SKalle Valo 	}
3652e705c121SKalle Valo 
3653e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3654e705c121SKalle Valo 	return ret;
3655e705c121SKalle Valo }
3656e705c121SKalle Valo 
3657e705c121SKalle Valo static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3658e705c121SKalle Valo 					struct ieee80211_vif *vif,
3659e705c121SKalle Valo 					struct ieee80211_key_conf *keyconf,
3660e705c121SKalle Valo 					struct ieee80211_sta *sta,
3661e705c121SKalle Valo 					u32 iv32, u16 *phase1key)
3662e705c121SKalle Valo {
3663e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3664e705c121SKalle Valo 
3665e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3666e705c121SKalle Valo 		return;
3667e705c121SKalle Valo 
3668e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3669e705c121SKalle Valo }
3670e705c121SKalle Valo 
3671e705c121SKalle Valo 
3672e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3673e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
3674e705c121SKalle Valo {
3675e705c121SKalle Valo 	struct iwl_mvm *mvm =
3676e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3677e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
3678e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
3679e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
3680e705c121SKalle Valo 
3681e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3682e705c121SKalle Valo 		return true;
3683e705c121SKalle Valo 
3684e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3685e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3686e705c121SKalle Valo 		return true;
3687e705c121SKalle Valo 	}
3688e705c121SKalle Valo 
3689e705c121SKalle Valo 	resp = (void *)pkt->data;
3690e705c121SKalle Valo 
3691e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
3692b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
3693e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
3694e705c121SKalle Valo 
3695e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
3696e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3697e705c121SKalle Valo 		     te_data->uid);
3698e705c121SKalle Valo 
3699e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3700e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3701e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3702e705c121SKalle Valo 
3703e705c121SKalle Valo 	return true;
3704e705c121SKalle Valo }
3705e705c121SKalle Valo 
3706dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3707dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3708dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3709dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3710dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3711e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3712e705c121SKalle Valo 				    struct ieee80211_channel *channel,
3713e705c121SKalle Valo 				    struct ieee80211_vif *vif,
3714e705c121SKalle Valo 				    int duration)
3715e705c121SKalle Valo {
3716afc1e3b4SAvraham Stern 	int res;
3717e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3718e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3719e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
3720e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
3721dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
3722dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
3723dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
3724e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
3725e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3726e705c121SKalle Valo 		.id_and_color =
3727e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3728e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3729e705c121SKalle Valo 	};
373057e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
373157e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
373257e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
373357e861d9SDavid Spinadel 
373457e861d9SDavid Spinadel 	/* Set the channel info data */
373557e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
373657e861d9SDavid Spinadel 			      (channel->band == NL80211_BAND_2GHZ) ?
373757e861d9SDavid Spinadel 			       PHY_BAND_24 : PHY_BAND_5,
373857e861d9SDavid Spinadel 			      PHY_VHT_CHANNEL_MODE20,
373957e861d9SDavid Spinadel 			      0);
374057e861d9SDavid Spinadel 
374157e861d9SDavid Spinadel 	/* Set the time and duration */
3742afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
3743e705c121SKalle Valo 
3744dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
3745dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
3746dc28e12fSMatti Gottlieb 
3747dc28e12fSMatti Gottlieb 	/*
3748dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
3749dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
3750dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
3751dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
3752dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
3753dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
3754dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
3755dc28e12fSMatti Gottlieb 	 */
3756dc28e12fSMatti Gottlieb 	if (vif->bss_conf.assoc) {
3757dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3758dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
3759dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
3760dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3761dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
3762dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
3763dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
3764dc28e12fSMatti Gottlieb 		}
3765dc28e12fSMatti Gottlieb 	}
3766dc28e12fSMatti Gottlieb 
376757e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
376857e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
3769dc28e12fSMatti Gottlieb 
3770dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
3771dc28e12fSMatti Gottlieb 		     "ROC: Requesting to remain on channel %u for %ums (requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3772dc28e12fSMatti Gottlieb 		     channel->hw_value, req_dur, duration, delay,
3773dc28e12fSMatti Gottlieb 		     dtim_interval);
3774e705c121SKalle Valo 	/* Set the node address */
377557e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
3776e705c121SKalle Valo 
3777e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3778e705c121SKalle Valo 
3779e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3780e705c121SKalle Valo 
3781e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3782e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3783e705c121SKalle Valo 		return -EIO;
3784e705c121SKalle Valo 	}
3785e705c121SKalle Valo 
3786e705c121SKalle Valo 	te_data->vif = vif;
3787e705c121SKalle Valo 	te_data->duration = duration;
3788e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
3789e705c121SKalle Valo 
3790e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3791e705c121SKalle Valo 
3792e705c121SKalle Valo 	/*
3793e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
3794e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
3795e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
3796e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
3797e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
3798e705c121SKalle Valo 	 * time another notification (that the time event started)
3799e705c121SKalle Valo 	 * might already be processed unsuccessfully.
3800e705c121SKalle Valo 	 */
3801e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3802e705c121SKalle Valo 				   time_event_response,
3803e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
3804e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
3805e705c121SKalle Valo 
380657e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
3807e705c121SKalle Valo 				   &aux_roc_req);
3808e705c121SKalle Valo 
3809e705c121SKalle Valo 	if (res) {
3810e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3811e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3812e705c121SKalle Valo 		goto out_clear_te;
3813e705c121SKalle Valo 	}
3814e705c121SKalle Valo 
3815e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
3816e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3817e705c121SKalle Valo 	/* should never fail */
3818e705c121SKalle Valo 	WARN_ON_ONCE(res);
3819e705c121SKalle Valo 
3820e705c121SKalle Valo 	if (res) {
3821e705c121SKalle Valo  out_clear_te:
3822e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
3823e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
3824e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3825e705c121SKalle Valo 	}
3826e705c121SKalle Valo 
3827e705c121SKalle Valo 	return res;
3828e705c121SKalle Valo }
3829e705c121SKalle Valo 
3830e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
3831e705c121SKalle Valo 		       struct ieee80211_vif *vif,
3832e705c121SKalle Valo 		       struct ieee80211_channel *channel,
3833e705c121SKalle Valo 		       int duration,
3834e705c121SKalle Valo 		       enum ieee80211_roc_type type)
3835e705c121SKalle Valo {
3836e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3837e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3838e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
3839e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
3840e705c121SKalle Valo 	int ret, i;
3841e705c121SKalle Valo 
3842e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3843e705c121SKalle Valo 			   duration, type);
3844e705c121SKalle Valo 
38459834781cSJohannes Berg 	/*
38469834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
38479834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
38489834781cSJohannes Berg 	 */
3849e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
3850e705c121SKalle Valo 
3851e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3852e705c121SKalle Valo 
3853e705c121SKalle Valo 	switch (vif->type) {
3854e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3855e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
3856e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
3857e705c121SKalle Valo 			/* Use aux roc framework (HS20) */
3858e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3859e705c121SKalle Valo 						       vif, duration);
3860e705c121SKalle Valo 			goto out_unlock;
3861e705c121SKalle Valo 		}
3862e705c121SKalle Valo 		IWL_ERR(mvm, "hotspot not supported\n");
3863e705c121SKalle Valo 		ret = -EINVAL;
3864e705c121SKalle Valo 		goto out_unlock;
3865e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
3866e705c121SKalle Valo 		/* handle below */
3867e705c121SKalle Valo 		break;
3868e705c121SKalle Valo 	default:
3869e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
3870e705c121SKalle Valo 		ret = -EINVAL;
3871e705c121SKalle Valo 		goto out_unlock;
3872e705c121SKalle Valo 	}
3873e705c121SKalle Valo 
3874e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
3875e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
3876e705c121SKalle Valo 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3877e705c121SKalle Valo 			continue;
3878e705c121SKalle Valo 
3879e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3880e705c121SKalle Valo 			/*
3881e705c121SKalle Valo 			 * Unbind the P2P_DEVICE from the current PHY context,
3882e705c121SKalle Valo 			 * and if the PHY context is not used remove it.
3883e705c121SKalle Valo 			 */
3884e705c121SKalle Valo 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
3885e705c121SKalle Valo 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3886e705c121SKalle Valo 				goto out_unlock;
3887e705c121SKalle Valo 
3888e705c121SKalle Valo 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3889e705c121SKalle Valo 
3890e705c121SKalle Valo 			/* Bind the P2P_DEVICE to the current PHY Context */
3891e705c121SKalle Valo 			mvmvif->phy_ctxt = phy_ctxt;
3892e705c121SKalle Valo 
3893e705c121SKalle Valo 			ret = iwl_mvm_binding_add_vif(mvm, vif);
3894e705c121SKalle Valo 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3895e705c121SKalle Valo 				goto out_unlock;
3896e705c121SKalle Valo 
3897e705c121SKalle Valo 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3898e705c121SKalle Valo 			goto schedule_time_event;
3899e705c121SKalle Valo 		}
3900e705c121SKalle Valo 	}
3901e705c121SKalle Valo 
3902e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
3903e705c121SKalle Valo 	if (channel == mvmvif->phy_ctxt->channel)
3904e705c121SKalle Valo 		goto schedule_time_event;
3905e705c121SKalle Valo 
3906e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
3907e705c121SKalle Valo 
3908e705c121SKalle Valo 	/*
3909e705c121SKalle Valo 	 * Change the PHY context configuration as it is currently referenced
3910e705c121SKalle Valo 	 * only by the P2P Device MAC
3911e705c121SKalle Valo 	 */
3912e705c121SKalle Valo 	if (mvmvif->phy_ctxt->ref == 1) {
3913e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3914e705c121SKalle Valo 					       &chandef, 1, 1);
3915e705c121SKalle Valo 		if (ret)
3916e705c121SKalle Valo 			goto out_unlock;
3917e705c121SKalle Valo 	} else {
3918e705c121SKalle Valo 		/*
3919e705c121SKalle Valo 		 * The PHY context is shared with other MACs. Need to remove the
3920e705c121SKalle Valo 		 * P2P Device from the binding, allocate an new PHY context and
3921e705c121SKalle Valo 		 * create a new binding
3922e705c121SKalle Valo 		 */
3923e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3924e705c121SKalle Valo 		if (!phy_ctxt) {
3925e705c121SKalle Valo 			ret = -ENOSPC;
3926e705c121SKalle Valo 			goto out_unlock;
3927e705c121SKalle Valo 		}
3928e705c121SKalle Valo 
3929e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3930e705c121SKalle Valo 					       1, 1);
3931e705c121SKalle Valo 		if (ret) {
3932e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
3933e705c121SKalle Valo 			goto out_unlock;
3934e705c121SKalle Valo 		}
3935e705c121SKalle Valo 
3936e705c121SKalle Valo 		/* Unbind the P2P_DEVICE from the current PHY context */
3937e705c121SKalle Valo 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
3938e705c121SKalle Valo 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3939e705c121SKalle Valo 			goto out_unlock;
3940e705c121SKalle Valo 
3941e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3942e705c121SKalle Valo 
3943e705c121SKalle Valo 		/* Bind the P2P_DEVICE to the new allocated PHY context */
3944e705c121SKalle Valo 		mvmvif->phy_ctxt = phy_ctxt;
3945e705c121SKalle Valo 
3946e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
3947e705c121SKalle Valo 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3948e705c121SKalle Valo 			goto out_unlock;
3949e705c121SKalle Valo 
3950e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3951e705c121SKalle Valo 	}
3952e705c121SKalle Valo 
3953e705c121SKalle Valo schedule_time_event:
3954e705c121SKalle Valo 	/* Schedule the time events */
3955e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
3956e705c121SKalle Valo 
3957e705c121SKalle Valo out_unlock:
3958e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3959e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3960e705c121SKalle Valo 	return ret;
3961e705c121SKalle Valo }
3962e705c121SKalle Valo 
3963e705c121SKalle Valo static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
3964e705c121SKalle Valo {
3965e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3966e705c121SKalle Valo 
3967e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
3968e705c121SKalle Valo 
3969e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3970e705c121SKalle Valo 	iwl_mvm_stop_roc(mvm);
3971e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3972e705c121SKalle Valo 
3973e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3974e705c121SKalle Valo 	return 0;
3975e705c121SKalle Valo }
3976e705c121SKalle Valo 
3977b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
3978b73f9a4aSJohannes Berg 	bool responder;
3979b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
3980b73f9a4aSJohannes Berg };
3981b73f9a4aSJohannes Berg 
3982b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
3983b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
3984b73f9a4aSJohannes Berg {
3985b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
3986b73f9a4aSJohannes Berg 
3987b73f9a4aSJohannes Berg 	if (rcu_access_pointer(vif->chanctx_conf) == data->ctx &&
3988b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
3989b73f9a4aSJohannes Berg 		data->responder = true;
3990b73f9a4aSJohannes Berg }
3991b73f9a4aSJohannes Berg 
3992b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
3993b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
3994b73f9a4aSJohannes Berg {
3995b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
3996b73f9a4aSJohannes Berg 		.responder = false,
3997b73f9a4aSJohannes Berg 		.ctx = ctx,
3998b73f9a4aSJohannes Berg 	};
3999b73f9a4aSJohannes Berg 
4000b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4001b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
4002b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
4003b73f9a4aSJohannes Berg 					&data);
4004b73f9a4aSJohannes Berg 	return data.responder;
4005b73f9a4aSJohannes Berg }
4006b73f9a4aSJohannes Berg 
4007e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4008e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
4009e705c121SKalle Valo {
4010e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4011e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4012b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4013b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4014e705c121SKalle Valo 	int ret;
4015e705c121SKalle Valo 
4016e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4017e705c121SKalle Valo 
4018e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4019e705c121SKalle Valo 
4020e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4021e705c121SKalle Valo 	if (!phy_ctxt) {
4022e705c121SKalle Valo 		ret = -ENOSPC;
4023e705c121SKalle Valo 		goto out;
4024e705c121SKalle Valo 	}
4025e705c121SKalle Valo 
4026b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4027e705c121SKalle Valo 				       ctx->rx_chains_static,
4028e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
4029e705c121SKalle Valo 	if (ret) {
4030e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4031e705c121SKalle Valo 		goto out;
4032e705c121SKalle Valo 	}
4033e705c121SKalle Valo 
4034e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4035e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4036e705c121SKalle Valo out:
4037e705c121SKalle Valo 	return ret;
4038e705c121SKalle Valo }
4039e705c121SKalle Valo 
4040e705c121SKalle Valo static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4041e705c121SKalle Valo 			       struct ieee80211_chanctx_conf *ctx)
4042e705c121SKalle Valo {
4043e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4044e705c121SKalle Valo 	int ret;
4045e705c121SKalle Valo 
4046e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4047e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4048e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4049e705c121SKalle Valo 
4050e705c121SKalle Valo 	return ret;
4051e705c121SKalle Valo }
4052e705c121SKalle Valo 
4053e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4054e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4055e705c121SKalle Valo {
4056e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4057e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4058e705c121SKalle Valo 
4059e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4060e705c121SKalle Valo 
4061e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4062e705c121SKalle Valo }
4063e705c121SKalle Valo 
4064e705c121SKalle Valo static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4065e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx)
4066e705c121SKalle Valo {
4067e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4068e705c121SKalle Valo 
4069e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4070e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4071e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4072e705c121SKalle Valo }
4073e705c121SKalle Valo 
4074e705c121SKalle Valo static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4075e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx,
4076e705c121SKalle Valo 				   u32 changed)
4077e705c121SKalle Valo {
4078e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4079e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4080e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4081b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4082b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4083e705c121SKalle Valo 
4084e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4085e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4086e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4087e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4088e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4089e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4090e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4091e705c121SKalle Valo 		return;
4092e705c121SKalle Valo 
4093e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
40947a20bcceSEmmanuel Grumbach 
40957a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
40967a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4097b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
40987a20bcceSEmmanuel Grumbach 			goto out_unlock;
40997a20bcceSEmmanuel Grumbach 
41007a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
41017a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4102b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
41037a20bcceSEmmanuel Grumbach 			goto out_unlock;
41047a20bcceSEmmanuel Grumbach 	}
41057a20bcceSEmmanuel Grumbach 
4106e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4107b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4108e705c121SKalle Valo 				 ctx->rx_chains_static,
4109e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
41107a20bcceSEmmanuel Grumbach 
41117a20bcceSEmmanuel Grumbach out_unlock:
4112e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4113e705c121SKalle Valo }
4114e705c121SKalle Valo 
4115e705c121SKalle Valo static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4116e705c121SKalle Valo 					struct ieee80211_vif *vif,
4117e705c121SKalle Valo 					struct ieee80211_chanctx_conf *ctx,
4118e705c121SKalle Valo 					bool switching_chanctx)
4119e705c121SKalle Valo {
4120e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4121e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4122e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4123e705c121SKalle Valo 	int ret;
4124e705c121SKalle Valo 
4125e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4126e705c121SKalle Valo 
4127e705c121SKalle Valo 	mvmvif->phy_ctxt = phy_ctxt;
4128e705c121SKalle Valo 
4129e705c121SKalle Valo 	switch (vif->type) {
4130e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4131e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4132e705c121SKalle Valo 		if (switching_chanctx) {
4133e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4134e705c121SKalle Valo 			break;
4135e705c121SKalle Valo 		}
4136244726ecSLuca Coelho 		/* fall through */
4137e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4138e705c121SKalle Valo 		/*
4139e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4140e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4141e705c121SKalle Valo 		 */
4142e705c121SKalle Valo 		ret = 0;
4143e705c121SKalle Valo 		goto out;
4144e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
414519125cb0SAndrei Otcheretianski 		mvmvif->csa_bcn_pending = false;
4146e705c121SKalle Valo 		break;
4147e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4148e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4149e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4150e705c121SKalle Valo 		break;
4151e705c121SKalle Valo 	default:
4152e705c121SKalle Valo 		ret = -EINVAL;
4153e705c121SKalle Valo 		goto out;
4154e705c121SKalle Valo 	}
4155e705c121SKalle Valo 
4156e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4157e705c121SKalle Valo 	if (ret)
4158e705c121SKalle Valo 		goto out;
4159e705c121SKalle Valo 
4160e705c121SKalle Valo 	/*
4161e705c121SKalle Valo 	 * Power state must be updated before quotas,
4162e705c121SKalle Valo 	 * otherwise fw will complain.
4163e705c121SKalle Valo 	 */
4164e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4165e705c121SKalle Valo 
4166e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4167e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4168e705c121SKalle Valo 	 * will handle quota settings.
4169e705c121SKalle Valo 	 */
4170e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4171e705c121SKalle Valo 		mvmvif->monitor_active = true;
4172e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4173e705c121SKalle Valo 		if (ret)
4174e705c121SKalle Valo 			goto out_remove_binding;
41750e39eb03SChaya Rachel Ivgi 
41760e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
41770e39eb03SChaya Rachel Ivgi 		if (ret)
41780e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
41790e39eb03SChaya Rachel Ivgi 
4180e705c121SKalle Valo 	}
4181e705c121SKalle Valo 
4182e705c121SKalle Valo 	/* Handle binding during CSA */
4183e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4184e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4185e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4186e705c121SKalle Valo 	}
4187e705c121SKalle Valo 
4188e705c121SKalle Valo 	if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
418974a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
419074a10252SSara Sharon 
419174a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
419274a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
419319125cb0SAndrei Otcheretianski 			u32 duration = 3 * vif->bss_conf.beacon_int;
4194e705c121SKalle Valo 
419574a10252SSara Sharon 
4196e705c121SKalle Valo 			/* iwl_mvm_protect_session() reads directly from the
4197e705c121SKalle Valo 			 * device (the system time), so make sure it is
4198e705c121SKalle Valo 			 * available.
4199e705c121SKalle Valo 			 */
4200e705c121SKalle Valo 			ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_CSA);
4201e705c121SKalle Valo 			if (ret)
4202e705c121SKalle Valo 				goto out_remove_binding;
4203e705c121SKalle Valo 
4204e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4205e705c121SKalle Valo 			 * beacon on the new channel.
4206e705c121SKalle Valo 			 */
4207e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4208e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4209e705c121SKalle Valo 						true);
4210e705c121SKalle Valo 
4211e705c121SKalle Valo 			iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_CSA);
421274a10252SSara Sharon 		}
4213e705c121SKalle Valo 
4214e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4215e705c121SKalle Valo 	}
4216e705c121SKalle Valo 
4217e705c121SKalle Valo 	goto out;
4218e705c121SKalle Valo 
4219e705c121SKalle Valo out_remove_binding:
4220e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4221e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4222e705c121SKalle Valo out:
4223e705c121SKalle Valo 	if (ret)
4224e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
4225e705c121SKalle Valo 	return ret;
4226e705c121SKalle Valo }
4227e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4228e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4229e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4230e705c121SKalle Valo {
4231e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4232e705c121SKalle Valo 	int ret;
4233e705c121SKalle Valo 
4234e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4235e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
4236e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4237e705c121SKalle Valo 
4238e705c121SKalle Valo 	return ret;
4239e705c121SKalle Valo }
4240e705c121SKalle Valo 
4241e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4242e705c121SKalle Valo 					   struct ieee80211_vif *vif,
4243e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
4244e705c121SKalle Valo 					   bool switching_chanctx)
4245e705c121SKalle Valo {
4246e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4247e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
4248e705c121SKalle Valo 
4249e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4250e705c121SKalle Valo 
4251e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4252e705c121SKalle Valo 
4253e705c121SKalle Valo 	switch (vif->type) {
4254e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4255e705c121SKalle Valo 		goto out;
4256e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4257e705c121SKalle Valo 		mvmvif->monitor_active = false;
4258e705c121SKalle Valo 		mvmvif->ps_disabled = false;
42590e39eb03SChaya Rachel Ivgi 		iwl_mvm_rm_snif_sta(mvm, vif);
4260e705c121SKalle Valo 		break;
4261e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4262e705c121SKalle Valo 		/* This part is triggered only during CSA */
4263e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
4264e705c121SKalle Valo 			goto out;
4265e705c121SKalle Valo 
4266e705c121SKalle Valo 		mvmvif->csa_countdown = false;
4267e705c121SKalle Valo 
4268e705c121SKalle Valo 		/* Set CS bit on all the stations */
4269e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4270e705c121SKalle Valo 
4271e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
4272e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4273e705c121SKalle Valo 
4274e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
4275e705c121SKalle Valo 		break;
4276e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4277e705c121SKalle Valo 		if (!switching_chanctx)
4278e705c121SKalle Valo 			break;
4279e705c121SKalle Valo 
4280e705c121SKalle Valo 		disabled_vif = vif;
4281e705c121SKalle Valo 
428274a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
428374a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4284e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
4285e705c121SKalle Valo 		break;
4286e705c121SKalle Valo 	default:
4287e705c121SKalle Valo 		break;
4288e705c121SKalle Valo 	}
4289e705c121SKalle Valo 
4290e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
4291e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4292e705c121SKalle Valo 
4293e705c121SKalle Valo out:
4294e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
4295e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4296e705c121SKalle Valo }
4297e705c121SKalle Valo 
4298e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4299e705c121SKalle Valo 					 struct ieee80211_vif *vif,
4300e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
4301e705c121SKalle Valo {
4302e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4303e705c121SKalle Valo 
4304e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4305e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
4306e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4307e705c121SKalle Valo }
4308e705c121SKalle Valo 
4309e705c121SKalle Valo static int
4310e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4311e705c121SKalle Valo 				struct ieee80211_vif_chanctx_switch *vifs)
4312e705c121SKalle Valo {
4313e705c121SKalle Valo 	int ret;
4314e705c121SKalle Valo 
4315e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4316e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4317e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4318e705c121SKalle Valo 
4319e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4320e705c121SKalle Valo 	if (ret) {
4321e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4322e705c121SKalle Valo 		goto out_reassign;
4323e705c121SKalle Valo 	}
4324e705c121SKalle Valo 
4325e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4326e705c121SKalle Valo 					   true);
4327e705c121SKalle Valo 	if (ret) {
4328e705c121SKalle Valo 		IWL_ERR(mvm,
4329e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4330e705c121SKalle Valo 		goto out_remove;
4331e705c121SKalle Valo 	}
4332e705c121SKalle Valo 
4333e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
4334e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
4335e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
4336e705c121SKalle Valo 
4337e705c121SKalle Valo 	goto out;
4338e705c121SKalle Valo 
4339e705c121SKalle Valo out_remove:
4340e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4341e705c121SKalle Valo 
4342e705c121SKalle Valo out_reassign:
4343e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
4344e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4345e705c121SKalle Valo 		goto out_restart;
4346e705c121SKalle Valo 	}
4347e705c121SKalle Valo 
4348e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4349e705c121SKalle Valo 					 true)) {
4350e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4351e705c121SKalle Valo 		goto out_restart;
4352e705c121SKalle Valo 	}
4353e705c121SKalle Valo 
4354e705c121SKalle Valo 	goto out;
4355e705c121SKalle Valo 
4356e705c121SKalle Valo out_restart:
4357e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4358e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4359e705c121SKalle Valo 
4360e705c121SKalle Valo out:
4361e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4362e705c121SKalle Valo 
4363e705c121SKalle Valo 	return ret;
4364e705c121SKalle Valo }
4365e705c121SKalle Valo 
4366e705c121SKalle Valo static int
4367e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4368e705c121SKalle Valo 				    struct ieee80211_vif_chanctx_switch *vifs)
4369e705c121SKalle Valo {
4370e705c121SKalle Valo 	int ret;
4371e705c121SKalle Valo 
4372e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4373e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4374e705c121SKalle Valo 
4375e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4376e705c121SKalle Valo 					   true);
4377e705c121SKalle Valo 	if (ret) {
4378e705c121SKalle Valo 		IWL_ERR(mvm,
4379e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4380e705c121SKalle Valo 		goto out_reassign;
4381e705c121SKalle Valo 	}
4382e705c121SKalle Valo 
4383e705c121SKalle Valo 	goto out;
4384e705c121SKalle Valo 
4385e705c121SKalle Valo out_reassign:
4386e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4387e705c121SKalle Valo 					 true)) {
4388e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4389e705c121SKalle Valo 		goto out_restart;
4390e705c121SKalle Valo 	}
4391e705c121SKalle Valo 
4392e705c121SKalle Valo 	goto out;
4393e705c121SKalle Valo 
4394e705c121SKalle Valo out_restart:
4395e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4396e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4397e705c121SKalle Valo 
4398e705c121SKalle Valo out:
4399e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4400e705c121SKalle Valo 
4401e705c121SKalle Valo 	return ret;
4402e705c121SKalle Valo }
4403e705c121SKalle Valo 
4404e705c121SKalle Valo static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4405e705c121SKalle Valo 				      struct ieee80211_vif_chanctx_switch *vifs,
4406e705c121SKalle Valo 				      int n_vifs,
4407e705c121SKalle Valo 				      enum ieee80211_chanctx_switch_mode mode)
4408e705c121SKalle Valo {
4409e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4410e705c121SKalle Valo 	int ret;
4411e705c121SKalle Valo 
4412e705c121SKalle Valo 	/* we only support a single-vif right now */
4413e705c121SKalle Valo 	if (n_vifs > 1)
4414e705c121SKalle Valo 		return -EOPNOTSUPP;
4415e705c121SKalle Valo 
4416e705c121SKalle Valo 	switch (mode) {
4417e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
4418e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4419e705c121SKalle Valo 		break;
4420e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
4421e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
4422e705c121SKalle Valo 		break;
4423e705c121SKalle Valo 	default:
4424e705c121SKalle Valo 		ret = -EOPNOTSUPP;
4425e705c121SKalle Valo 		break;
4426e705c121SKalle Valo 	}
4427e705c121SKalle Valo 
4428e705c121SKalle Valo 	return ret;
4429e705c121SKalle Valo }
4430e705c121SKalle Valo 
44312f0282dbSJohannes Berg static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
44322f0282dbSJohannes Berg {
44332f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
44342f0282dbSJohannes Berg 
44352f0282dbSJohannes Berg 	return mvm->ibss_manager;
44362f0282dbSJohannes Berg }
44372f0282dbSJohannes Berg 
4438e705c121SKalle Valo static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4439e705c121SKalle Valo 			   struct ieee80211_sta *sta,
4440e705c121SKalle Valo 			   bool set)
4441e705c121SKalle Valo {
4442e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4443e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4444e705c121SKalle Valo 
4445e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
4446e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
4447e705c121SKalle Valo 		return -EINVAL;
4448e705c121SKalle Valo 	}
4449e705c121SKalle Valo 
4450e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4451e705c121SKalle Valo }
4452e705c121SKalle Valo 
4453e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
4454e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4455e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4456e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
4457e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
4458e705c121SKalle Valo };
4459e705c121SKalle Valo 
4460e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4461e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4462e705c121SKalle Valo 				      void *data, int len)
4463e705c121SKalle Valo {
4464e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4465e705c121SKalle Valo 	int err;
4466e705c121SKalle Valo 	u32 noa_duration;
4467e705c121SKalle Valo 
44688cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
44698cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
4470e705c121SKalle Valo 	if (err)
4471e705c121SKalle Valo 		return err;
4472e705c121SKalle Valo 
4473e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
4474e705c121SKalle Valo 		return -EINVAL;
4475e705c121SKalle Valo 
4476e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4477e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
4478e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4479e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
4480e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4481e705c121SKalle Valo 			return -EINVAL;
4482e705c121SKalle Valo 
4483e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4484e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
4485e705c121SKalle Valo 			return -EINVAL;
4486e705c121SKalle Valo 
4487e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
4488e705c121SKalle Valo 		mvm->noa_vif = vif;
4489e705c121SKalle Valo 
449022b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
4491e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4492e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
4493e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4494e705c121SKalle Valo 		    !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
4495e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4496e705c121SKalle Valo 			return -EINVAL;
4497e705c121SKalle Valo 
4498e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
4499e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4500e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4501e705c121SKalle Valo 	}
4502e705c121SKalle Valo 
4503e705c121SKalle Valo 	return -EOPNOTSUPP;
4504e705c121SKalle Valo }
4505e705c121SKalle Valo 
4506e705c121SKalle Valo static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4507e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4508e705c121SKalle Valo 				    void *data, int len)
4509e705c121SKalle Valo {
4510e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4511e705c121SKalle Valo 	int err;
4512e705c121SKalle Valo 
4513e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4514e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4515e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4516e705c121SKalle Valo 
4517e705c121SKalle Valo 	return err;
4518e705c121SKalle Valo }
4519e705c121SKalle Valo #endif
4520e705c121SKalle Valo 
4521e705c121SKalle Valo static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4522e705c121SKalle Valo 				   struct ieee80211_vif *vif,
4523e705c121SKalle Valo 				   struct ieee80211_channel_switch *chsw)
4524e705c121SKalle Valo {
4525e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
4526e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
4527e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
4528e705c121SKalle Valo 	 * (which is when the absence time event starts).
4529e705c121SKalle Valo 	 */
4530e705c121SKalle Valo 
4531e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4532e705c121SKalle Valo 			   "dummy channel switch op\n");
4533e705c121SKalle Valo }
4534e705c121SKalle Valo 
453574a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
453674a10252SSara Sharon 				       struct ieee80211_vif *vif,
453774a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
453874a10252SSara Sharon {
453974a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
454074a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
454174a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
454274a10252SSara Sharon 							  mvmvif->color)),
454374a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
454474a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
454574a10252SSara Sharon 		.cs_count = chsw->count,
454677738865SSara Sharon 		.cs_mode = chsw->block_tx,
454774a10252SSara Sharon 	};
454874a10252SSara Sharon 
454974a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
455074a10252SSara Sharon 
45519cfcf71cSSara Sharon 	if (chsw->delay)
45529cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
45539cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
45549cfcf71cSSara Sharon 
455574a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
455674a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
455774a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
455874a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
455974a10252SSara Sharon }
456074a10252SSara Sharon 
4561f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
4562e705c121SKalle Valo static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4563e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4564e705c121SKalle Valo 				      struct ieee80211_channel_switch *chsw)
4565e705c121SKalle Valo {
4566e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4567e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
4568e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4569e705c121SKalle Valo 	u32 apply_time;
4570e705c121SKalle Valo 	int ret;
4571e705c121SKalle Valo 
4572e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4573e705c121SKalle Valo 
4574e705c121SKalle Valo 	mvmvif->csa_failed = false;
4575e705c121SKalle Valo 
4576e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
4577e705c121SKalle Valo 			   chsw->chandef.center_freq1);
4578e705c121SKalle Valo 
45797174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
45807174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
45817174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
4582e705c121SKalle Valo 
4583e705c121SKalle Valo 	switch (vif->type) {
4584e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4585e705c121SKalle Valo 		csa_vif =
4586e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
4587e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
4588e705c121SKalle Valo 		if (WARN_ONCE(csa_vif && csa_vif->csa_active,
4589e705c121SKalle Valo 			      "Another CSA is already in progress")) {
4590e705c121SKalle Valo 			ret = -EBUSY;
4591e705c121SKalle Valo 			goto out_unlock;
4592e705c121SKalle Valo 		}
4593e705c121SKalle Valo 
4594d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
4595d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
4596d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
4597d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
4598d3a108a4SAndrei Otcheretianski 			goto out_unlock;
4599d3a108a4SAndrei Otcheretianski 		}
4600d3a108a4SAndrei Otcheretianski 
4601e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
4602e705c121SKalle Valo 
4603e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
4604e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
4605e705c121SKalle Valo 			ret = -EBUSY;
4606e705c121SKalle Valo 			goto out_unlock;
4607e705c121SKalle Valo 		}
4608e705c121SKalle Valo 
4609d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
4610d3a108a4SAndrei Otcheretianski 
4611e705c121SKalle Valo 		break;
4612e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4613e705c121SKalle Valo 		/* Schedule the time event to a bit before beacon 1,
4614e705c121SKalle Valo 		 * to make sure we're in the new channel when the
4615f5d8f50fSIlan Peer 		 * GO/AP arrives. In case count <= 1 immediately schedule the
4616f5d8f50fSIlan Peer 		 * TE (this might result with some packet loss or connection
4617f5d8f50fSIlan Peer 		 * loss).
4618e705c121SKalle Valo 		 */
4619f5d8f50fSIlan Peer 		if (chsw->count <= 1)
4620f5d8f50fSIlan Peer 			apply_time = 0;
4621f5d8f50fSIlan Peer 		else
4622e705c121SKalle Valo 			apply_time = chsw->device_timestamp +
4623e705c121SKalle Valo 				((vif->bss_conf.beacon_int * (chsw->count - 1) -
4624e705c121SKalle Valo 				  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
4625e705c121SKalle Valo 
4626f6780614SSara Sharon 		if (chsw->block_tx) {
4627e705c121SKalle Valo 			iwl_mvm_csa_client_absent(mvm, vif);
4628f6780614SSara Sharon 			/*
4629f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
4630f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
4631f6780614SSara Sharon 			 */
4632f6780614SSara Sharon 			if (!chsw->count ||
4633f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
4634f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
4635f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
4636f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
4637f6780614SSara Sharon 		}
4638e705c121SKalle Valo 
4639e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
4640e705c121SKalle Valo 			ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4641e705c121SKalle Valo 			if (ret)
4642e705c121SKalle Valo 				goto out_unlock;
4643e705c121SKalle Valo 		}
4644e705c121SKalle Valo 
464574a10252SSara Sharon 		if (fw_has_capa(&mvm->fw->ucode_capa,
464674a10252SSara Sharon 				IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
464774a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
464874a10252SSara Sharon 		else
464974a10252SSara Sharon 			iwl_mvm_schedule_csa_period(mvm, vif,
465074a10252SSara Sharon 						    vif->bss_conf.beacon_int,
465174a10252SSara Sharon 						    apply_time);
465281b4e44eSSara Sharon 
465381b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
465481b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
4655e705c121SKalle Valo 		break;
4656e705c121SKalle Valo 	default:
4657e705c121SKalle Valo 		break;
4658e705c121SKalle Valo 	}
4659e705c121SKalle Valo 
4660e705c121SKalle Valo 	mvmvif->ps_disabled = true;
4661e705c121SKalle Valo 
4662e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
4663e705c121SKalle Valo 	if (ret)
4664e705c121SKalle Valo 		goto out_unlock;
4665e705c121SKalle Valo 
4666e705c121SKalle Valo 	/* we won't be on this channel any longer */
4667e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
4668e705c121SKalle Valo 
4669e705c121SKalle Valo out_unlock:
4670e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4671e705c121SKalle Valo 
4672e705c121SKalle Valo 	return ret;
4673e705c121SKalle Valo }
4674e705c121SKalle Valo 
4675c37763d2SSara Sharon static void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
4676c37763d2SSara Sharon 					     struct ieee80211_vif *vif,
4677c37763d2SSara Sharon 					     struct ieee80211_channel_switch *chsw)
4678e705c121SKalle Valo {
4679e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4680c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4681c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
4682c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4683c37763d2SSara Sharon 							  mvmvif->color)),
4684c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
4685c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
4686c37763d2SSara Sharon 		.cs_count = chsw->count,
468777738865SSara Sharon 		.cs_mode = chsw->block_tx,
4688c37763d2SSara Sharon 	};
4689e705c121SKalle Valo 
4690c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
4691c37763d2SSara Sharon 		return;
4692e705c121SKalle Valo 
469381b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
469481b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
469581b4e44eSSara Sharon 			/* Second time, give up on this AP*/
469681b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
469781b4e44eSSara Sharon 			ieee80211_chswitch_done(vif, false);
469881b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
469981b4e44eSSara Sharon 			return;
4700e705c121SKalle Valo 		}
470181b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
4702e705c121SKalle Valo 	}
470381b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
4704e705c121SKalle Valo 
4705c37763d2SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d\n", mvmvif->id);
4706e705c121SKalle Valo 
4707c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
4708c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
4709c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
4710c37763d2SSara Sharon 				     CMD_ASYNC, sizeof(cmd), &cmd));
4711e705c121SKalle Valo }
4712e705c121SKalle Valo 
47136110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
47146110d9e5SDavid Spinadel {
4715435d0827SSara Sharon 	int i;
4716435d0827SSara Sharon 
471706195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
4718309c4848SLuca Coelho 		if (drop) {
4719309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
47206110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
4721435d0827SSara Sharon 				iwl_mvm_flushable_queues(mvm) & queues, 0);
4722309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
4723309c4848SLuca Coelho 		} else {
4724435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
4725309c4848SLuca Coelho 		}
4726435d0827SSara Sharon 		return;
4727435d0827SSara Sharon 	}
47286110d9e5SDavid Spinadel 
47296110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
47306110d9e5SDavid Spinadel 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4731435d0827SSara Sharon 		struct ieee80211_sta *sta;
4732435d0827SSara Sharon 
4733435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
47346110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
47356110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
47366110d9e5SDavid Spinadel 			continue;
47376110d9e5SDavid Spinadel 
473806195639SSara Sharon 		if (drop)
473906195639SSara Sharon 			iwl_mvm_flush_sta_tids(mvm, i, 0xFF, 0);
474006195639SSara Sharon 		else
47416110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
47426110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
47436110d9e5SDavid Spinadel 	}
47446110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
47456110d9e5SDavid Spinadel }
47466110d9e5SDavid Spinadel 
4747e705c121SKalle Valo static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4748e705c121SKalle Valo 			      struct ieee80211_vif *vif, u32 queues, bool drop)
4749e705c121SKalle Valo {
4750e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4751e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
4752e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4753e705c121SKalle Valo 	struct ieee80211_sta *sta;
4754e705c121SKalle Valo 	int i;
4755e705c121SKalle Valo 	u32 msk = 0;
4756e705c121SKalle Valo 
47576110d9e5SDavid Spinadel 	if (!vif) {
47586110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
47596110d9e5SDavid Spinadel 		return;
47606110d9e5SDavid Spinadel 	}
47616110d9e5SDavid Spinadel 
47626110d9e5SDavid Spinadel 	if (vif->type != NL80211_IFTYPE_STATION)
4763e705c121SKalle Valo 		return;
4764e705c121SKalle Valo 
476524afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
476624afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
476724afba76SLiad Kaufman 
4768e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4769e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
4770e705c121SKalle Valo 
4771e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
47720ae98812SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4773e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4774e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
4775e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
4776e705c121SKalle Valo 			continue;
4777e705c121SKalle Valo 
4778e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4779e705c121SKalle Valo 		if (mvmsta->vif != vif)
4780e705c121SKalle Valo 			continue;
4781e705c121SKalle Valo 
4782e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
4783e705c121SKalle Valo 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4784e705c121SKalle Valo 
4785d49394a1SSara Sharon 		if (drop) {
4786d49394a1SSara Sharon 			if (iwl_mvm_flush_sta(mvm, mvmsta, false, 0))
4787d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
4788d49394a1SSara Sharon 		} else {
4789e705c121SKalle Valo 			msk |= mvmsta->tfd_queue_msk;
4790d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
4791d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
4792e705c121SKalle Valo 		}
4793d49394a1SSara Sharon 	}
4794e705c121SKalle Valo 
4795e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4796e705c121SKalle Valo 
4797e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
4798e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
4799e705c121SKalle Valo 	 */
4800d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
4801a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
4802e705c121SKalle Valo }
4803e705c121SKalle Valo 
4804e705c121SKalle Valo static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4805e705c121SKalle Valo 				  struct survey_info *survey)
4806e705c121SKalle Valo {
4807e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4808e705c121SKalle Valo 	int ret;
4809e705c121SKalle Valo 
4810e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
4811e705c121SKalle Valo 
4812e705c121SKalle Valo 	/* only support global statistics right now */
4813e705c121SKalle Valo 	if (idx != 0)
4814e705c121SKalle Valo 		return -ENOENT;
4815e705c121SKalle Valo 
4816280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4817e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4818e705c121SKalle Valo 		return -ENOENT;
4819e705c121SKalle Valo 
4820e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4821e705c121SKalle Valo 
4822aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
4823e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
4824e705c121SKalle Valo 		if (ret)
4825e705c121SKalle Valo 			goto out;
4826e705c121SKalle Valo 	}
4827e705c121SKalle Valo 
4828e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
4829e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
4830e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
4831e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
4832e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
4833e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
4834e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
4835e705c121SKalle Valo 
4836e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
4837e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
4838e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
4839e705c121SKalle Valo 
4840e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
4841e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
4842e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
4843e705c121SKalle Valo 
4844e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4845e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
4846e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
4847e705c121SKalle Valo 
4848e705c121SKalle Valo 	ret = 0;
4849e705c121SKalle Valo  out:
4850e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4851e705c121SKalle Valo 	return ret;
4852e705c121SKalle Valo }
4853e705c121SKalle Valo 
4854e705c121SKalle Valo static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
4855e705c121SKalle Valo 				       struct ieee80211_vif *vif,
4856e705c121SKalle Valo 				       struct ieee80211_sta *sta,
4857e705c121SKalle Valo 				       struct station_info *sinfo)
4858e705c121SKalle Valo {
4859e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4860e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4861e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4862e705c121SKalle Valo 
4863988b5968SSara Sharon 	if (mvmsta->avg_energy) {
4864988b5968SSara Sharon 		sinfo->signal_avg = mvmsta->avg_energy;
486522d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
4866988b5968SSara Sharon 	}
4867988b5968SSara Sharon 
4868e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
4869e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
4870e705c121SKalle Valo 		return;
4871e705c121SKalle Valo 
4872e705c121SKalle Valo 	if (!vif->bss_conf.assoc)
4873e705c121SKalle Valo 		return;
4874e705c121SKalle Valo 
4875e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4876e705c121SKalle Valo 
4877e705c121SKalle Valo 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
4878e705c121SKalle Valo 		goto unlock;
4879e705c121SKalle Valo 
4880e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
4881e705c121SKalle Valo 		goto unlock;
4882e705c121SKalle Valo 
4883e705c121SKalle Valo 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
4884e705c121SKalle Valo 			   mvmvif->beacon_stats.accu_num_beacons;
488522d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
4886e705c121SKalle Valo 	if (mvmvif->beacon_stats.avg_signal) {
4887e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
4888e705c121SKalle Valo 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
488922d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
4890e705c121SKalle Valo 	}
4891e705c121SKalle Valo  unlock:
4892e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4893e705c121SKalle Valo }
4894e705c121SKalle Valo 
4895e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
4896e705c121SKalle Valo 					struct ieee80211_vif *vif,
4897e705c121SKalle Valo 					const struct ieee80211_event *event)
4898e705c121SKalle Valo {
48994c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
4900e705c121SKalle Valo 	do {								\
49014c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
4902e705c121SKalle Valo 			break;						\
49037174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
4904e705c121SKalle Valo 	} while (0)
4905e705c121SKalle Valo 
4906e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
4907e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
4908e705c121SKalle Valo 
49096c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
49106c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
49116c042d75SSara Sharon 	if (!trig)
4912e705c121SKalle Valo 		return;
4913e705c121SKalle Valo 
4914e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
4915e705c121SKalle Valo 
4916e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
4917e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
49184c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
4919e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
4920e705c121SKalle Valo 					    event->u.mlme.reason);
4921e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
49224c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
4923e705c121SKalle Valo 					   "ASSOC TIMEOUT");
4924e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
4925e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
49264c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
4927e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
4928e705c121SKalle Valo 					   event->u.mlme.reason);
4929e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
49304c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
4931e705c121SKalle Valo 					   "AUTH TIMEOUT");
4932e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
49334c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
4934e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
4935e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
49364c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
4937e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
4938e705c121SKalle Valo 	}
4939e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
4940e705c121SKalle Valo }
4941e705c121SKalle Valo 
4942e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
4943e705c121SKalle Valo 					  struct ieee80211_vif *vif,
4944e705c121SKalle Valo 					  const struct ieee80211_event *event)
4945e705c121SKalle Valo {
4946e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
4947e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
4948e705c121SKalle Valo 
49496c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
49506c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
49516c042d75SSara Sharon 	if (!trig)
4952e705c121SKalle Valo 		return;
4953e705c121SKalle Valo 
4954e705c121SKalle Valo 	ba_trig = (void *)trig->data;
4955e705c121SKalle Valo 
4956e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
4957e705c121SKalle Valo 		return;
4958e705c121SKalle Valo 
49597174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4960e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
4961e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
4962e705c121SKalle Valo 				event->u.ba.ssn);
4963e705c121SKalle Valo }
4964e705c121SKalle Valo 
4965e705c121SKalle Valo static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
4966e705c121SKalle Valo 				       struct ieee80211_vif *vif,
4967e705c121SKalle Valo 				       const struct ieee80211_event *event)
4968e705c121SKalle Valo {
4969e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4970e705c121SKalle Valo 
4971e705c121SKalle Valo 	switch (event->type) {
4972e705c121SKalle Valo 	case MLME_EVENT:
4973e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
4974e705c121SKalle Valo 		break;
4975e705c121SKalle Valo 	case BAR_RX_EVENT:
4976e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
4977e705c121SKalle Valo 		break;
4978e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
4979528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
4980528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
4981e705c121SKalle Valo 		break;
4982e705c121SKalle Valo 	default:
4983e705c121SKalle Valo 		break;
4984e705c121SKalle Valo 	}
4985e705c121SKalle Valo }
4986e705c121SKalle Valo 
4987d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
4988d0ff5d22SSara Sharon 				     struct iwl_mvm_internal_rxq_notif *notif,
4989d0ff5d22SSara Sharon 				     u32 size)
49900636b938SSara Sharon {
49910636b938SSara Sharon 	u32 qmask = BIT(mvm->trans->num_rx_queues) - 1;
49920636b938SSara Sharon 	int ret;
49930636b938SSara Sharon 
49940636b938SSara Sharon 	lockdep_assert_held(&mvm->mutex);
49950636b938SSara Sharon 
4996cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
49970636b938SSara Sharon 		return;
49980636b938SSara Sharon 
4999d0ff5d22SSara Sharon 	notif->cookie = mvm->queue_sync_cookie;
50000636b938SSara Sharon 
5001d0ff5d22SSara Sharon 	if (notif->sync)
5002d0ff5d22SSara Sharon 		atomic_set(&mvm->queue_sync_counter,
5003d0ff5d22SSara Sharon 			   mvm->trans->num_rx_queues);
5004d0ff5d22SSara Sharon 
5005d0ff5d22SSara Sharon 	ret = iwl_mvm_notify_rx_queue(mvm, qmask, (u8 *)notif, size);
50060636b938SSara Sharon 	if (ret) {
50070636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
50080636b938SSara Sharon 		goto out;
50090636b938SSara Sharon 	}
5010d0ff5d22SSara Sharon 
50116ad04359SJohannes Berg 	if (notif->sync) {
50123a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
50136ad04359SJohannes Berg 					 atomic_read(&mvm->queue_sync_counter) == 0 ||
50146ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
50150636b938SSara Sharon 					 HZ);
50166ad04359SJohannes Berg 		WARN_ON_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm));
50176ad04359SJohannes Berg 	}
50180636b938SSara Sharon 
50190636b938SSara Sharon out:
50200636b938SSara Sharon 	atomic_set(&mvm->queue_sync_counter, 0);
50210636b938SSara Sharon 	mvm->queue_sync_cookie++;
50220636b938SSara Sharon }
50230636b938SSara Sharon 
50240636b938SSara Sharon static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
50250636b938SSara Sharon {
50260636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5027d0ff5d22SSara Sharon 	struct iwl_mvm_internal_rxq_notif data = {
5028d0ff5d22SSara Sharon 		.type = IWL_MVM_RXQ_EMPTY,
5029d0ff5d22SSara Sharon 		.sync = 1,
5030d0ff5d22SSara Sharon 	};
50310636b938SSara Sharon 
50320636b938SSara Sharon 	mutex_lock(&mvm->mutex);
5033d0ff5d22SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, &data, sizeof(data));
50340636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
50350636b938SSara Sharon }
50360636b938SSara Sharon 
5037b73f9a4aSJohannes Berg static int
5038b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
5039b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
5040b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
5041b73f9a4aSJohannes Berg {
5042b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5043b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5044b73f9a4aSJohannes Berg 
5045b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
5046b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
5047b73f9a4aSJohannes Berg 		return -EINVAL;
5048b73f9a4aSJohannes Berg 
5049b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
5050b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
5051b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
5052b73f9a4aSJohannes Berg 
5053b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
5054b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
5055b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
5056b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
5057b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
5058b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
5059b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
5060b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
5061b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
5062b73f9a4aSJohannes Berg 
5063b73f9a4aSJohannes Berg 	return 0;
5064b73f9a4aSJohannes Berg }
5065b73f9a4aSJohannes Berg 
5066fc36ffdaSJohannes Berg static int iwl_mvm_start_pmsr(struct ieee80211_hw *hw,
5067fc36ffdaSJohannes Berg 			      struct ieee80211_vif *vif,
5068fc36ffdaSJohannes Berg 			      struct cfg80211_pmsr_request *request)
5069fc36ffdaSJohannes Berg {
5070fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5071fc36ffdaSJohannes Berg 	int ret;
5072fc36ffdaSJohannes Berg 
5073fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5074fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
5075fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5076fc36ffdaSJohannes Berg 
5077fc36ffdaSJohannes Berg 	return ret;
5078fc36ffdaSJohannes Berg }
5079fc36ffdaSJohannes Berg 
5080fc36ffdaSJohannes Berg static void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw,
5081fc36ffdaSJohannes Berg 			       struct ieee80211_vif *vif,
5082fc36ffdaSJohannes Berg 			       struct cfg80211_pmsr_request *request)
5083fc36ffdaSJohannes Berg {
5084fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5085fc36ffdaSJohannes Berg 
5086fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5087fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
5088fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5089fc36ffdaSJohannes Berg }
5090fc36ffdaSJohannes Berg 
5091438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
5092438af969SSara Sharon {
5093438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
5094438af969SSara Sharon 
5095438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
5096438af969SSara Sharon 		return false;
5097438af969SSara Sharon 
5098438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
5099438af969SSara Sharon }
5100438af969SSara Sharon 
5101438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
5102438af969SSara Sharon 				      struct sk_buff *head,
5103438af969SSara Sharon 				      struct sk_buff *skb)
5104438af969SSara Sharon {
5105438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5106438af969SSara Sharon 
5107438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
5108438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
5109438af969SSara Sharon 		return false;
5110438af969SSara Sharon 
5111438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
5112438af969SSara Sharon 		return true;
5113438af969SSara Sharon 
5114438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5115438af969SSara Sharon }
5116438af969SSara Sharon 
5117e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
5118e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
5119cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
5120e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
5121e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
5122e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
5123e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
5124e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
5125e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
5126e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
5127e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
5128e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
5129e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
5130e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
5131e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
5132e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
5133e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
5134e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
5135e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
5136e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
5137e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
5138e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
5139e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
5140e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
5141e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
5142e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
5143e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
5144e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
5145e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
5146e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
5147e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
5148e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
5149e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
5150e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
5151e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
5152e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
5153e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5154e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
5155e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
5156e705c121SKalle Valo 
5157e705c121SKalle Valo 	.start_ap = iwl_mvm_start_ap_ibss,
5158e705c121SKalle Valo 	.stop_ap = iwl_mvm_stop_ap_ibss,
5159e705c121SKalle Valo 	.join_ibss = iwl_mvm_start_ap_ibss,
5160e705c121SKalle Valo 	.leave_ibss = iwl_mvm_stop_ap_ibss,
5161e705c121SKalle Valo 
51622f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
51632f0282dbSJohannes Berg 
5164e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
5165e705c121SKalle Valo 
5166e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
5167e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
5168e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
516979221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
5170c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
5171e705c121SKalle Valo 
5172e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5173e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5174e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5175e705c121SKalle Valo 
5176e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
5177e705c121SKalle Valo 
51780636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
51790636b938SSara Sharon 
5180e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5181e705c121SKalle Valo 
5182e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
5183e705c121SKalle Valo 	/* look at d3.c */
5184e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
5185e705c121SKalle Valo 	.resume = iwl_mvm_resume,
5186e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
5187e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
5188e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
5189e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
5190e705c121SKalle Valo #endif
5191e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
5192e705c121SKalle Valo #endif
5193e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
5194e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
5195b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
5196fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
5197fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
5198b73f9a4aSJohannes Berg 
5199438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
5200177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
5201177a11cfSGregory Greenman 	.sta_add_debugfs = iwl_mvm_sta_add_debugfs,
5202177a11cfSGregory Greenman #endif
5203e705c121SKalle Valo };
5204