xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision 9c3deeb51b664db183e00688caf8da2a096d16e5)
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.
9e705c121SKalle Valo  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10e705c121SKalle Valo  *
11e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
12e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
13e705c121SKalle Valo  * published by the Free Software Foundation.
14e705c121SKalle Valo  *
15e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
16e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
17e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18e705c121SKalle Valo  * General Public License for more details.
19e705c121SKalle Valo  *
20e705c121SKalle Valo  * You should have received a copy of the GNU General Public License
21e705c121SKalle Valo  * along with this program; if not, write to the Free Software
22e705c121SKalle Valo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23e705c121SKalle Valo  * USA
24e705c121SKalle Valo  *
25e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
26e705c121SKalle Valo  * in the file called COPYING.
27e705c121SKalle Valo  *
28e705c121SKalle Valo  * Contact Information:
29cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
30e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31e705c121SKalle Valo  *
32e705c121SKalle Valo  * BSD LICENSE
33e705c121SKalle Valo  *
34e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35e705c121SKalle Valo  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
36e705c121SKalle Valo  * All rights reserved.
37e705c121SKalle Valo  *
38e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
39e705c121SKalle Valo  * modification, are permitted provided that the following conditions
40e705c121SKalle Valo  * are met:
41e705c121SKalle Valo  *
42e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
43e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
44e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
45e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
46e705c121SKalle Valo  *    the documentation and/or other materials provided with the
47e705c121SKalle Valo  *    distribution.
48e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
49e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
50e705c121SKalle Valo  *    from this software without specific prior written permission.
51e705c121SKalle Valo  *
52e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63e705c121SKalle Valo  *
64e705c121SKalle Valo  *****************************************************************************/
65e705c121SKalle Valo #include <linux/kernel.h>
66e705c121SKalle Valo #include <linux/slab.h>
67e705c121SKalle Valo #include <linux/skbuff.h>
68e705c121SKalle Valo #include <linux/netdevice.h>
69e705c121SKalle Valo #include <linux/etherdevice.h>
70e705c121SKalle Valo #include <linux/ip.h>
71e705c121SKalle Valo #include <linux/if_arp.h>
72e705c121SKalle Valo #include <linux/devcoredump.h>
732f89a5d7SGolan Ben-Ami #include <linux/time.h>
74e705c121SKalle Valo #include <net/mac80211.h>
75e705c121SKalle Valo #include <net/ieee80211_radiotap.h>
76e705c121SKalle Valo #include <net/tcp.h>
77e705c121SKalle Valo 
78e705c121SKalle Valo #include "iwl-op-mode.h"
79e705c121SKalle Valo #include "iwl-io.h"
80e705c121SKalle Valo #include "mvm.h"
81e705c121SKalle Valo #include "sta.h"
82e705c121SKalle Valo #include "time-event.h"
83e705c121SKalle Valo #include "iwl-eeprom-parse.h"
84e705c121SKalle Valo #include "iwl-phy-db.h"
85e705c121SKalle Valo #include "testmode.h"
86e705c121SKalle Valo #include "iwl-fw-error-dump.h"
87e705c121SKalle Valo #include "iwl-prph.h"
88e705c121SKalle Valo #include "iwl-csr.h"
89e705c121SKalle Valo #include "iwl-nvm-parse.h"
902f89a5d7SGolan Ben-Ami #include "fw-dbg.h"
91e705c121SKalle Valo 
92e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
93e705c121SKalle Valo 	{
94e705c121SKalle Valo 		.max = 1,
95e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
96e705c121SKalle Valo 	},
97e705c121SKalle Valo 	{
98e705c121SKalle Valo 		.max = 1,
99e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
100e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
101e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
102e705c121SKalle Valo 	},
103e705c121SKalle Valo 	{
104e705c121SKalle Valo 		.max = 1,
105e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
106e705c121SKalle Valo 	},
107e705c121SKalle Valo };
108e705c121SKalle Valo 
109e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
110e705c121SKalle Valo 	{
111e705c121SKalle Valo 		.num_different_channels = 2,
112e705c121SKalle Valo 		.max_interfaces = 3,
113e705c121SKalle Valo 		.limits = iwl_mvm_limits,
114e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
115e705c121SKalle Valo 	},
116e705c121SKalle Valo };
117e705c121SKalle Valo 
118e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
119e705c121SKalle Valo static const struct nl80211_wowlan_tcp_data_token_feature
120e705c121SKalle Valo iwl_mvm_wowlan_tcp_token_feature = {
121e705c121SKalle Valo 	.min_len = 0,
122e705c121SKalle Valo 	.max_len = 255,
123e705c121SKalle Valo 	.bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
124e705c121SKalle Valo };
125e705c121SKalle Valo 
126e705c121SKalle Valo static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
127e705c121SKalle Valo 	.tok = &iwl_mvm_wowlan_tcp_token_feature,
128e705c121SKalle Valo 	.data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
129e705c121SKalle Valo 			    sizeof(struct ethhdr) -
130e705c121SKalle Valo 			    sizeof(struct iphdr) -
131e705c121SKalle Valo 			    sizeof(struct tcphdr),
132e705c121SKalle Valo 	.data_interval_max = 65535, /* __le16 in API */
133e705c121SKalle Valo 	.wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
134e705c121SKalle Valo 			    sizeof(struct ethhdr) -
135e705c121SKalle Valo 			    sizeof(struct iphdr) -
136e705c121SKalle Valo 			    sizeof(struct tcphdr),
137e705c121SKalle Valo 	.seq = true,
138e705c121SKalle Valo };
139e705c121SKalle Valo #endif
140e705c121SKalle Valo 
141e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
142e705c121SKalle Valo /*
143e705c121SKalle Valo  * Use the reserved field to indicate magic values.
144e705c121SKalle Valo  * these values will only be used internally by the driver,
145e705c121SKalle Valo  * and won't make it to the fw (reserved will be 0).
146e705c121SKalle Valo  * BC_FILTER_MAGIC_IP - configure the val of this attribute to
147e705c121SKalle Valo  *	be the vif's ip address. in case there is not a single
148e705c121SKalle Valo  *	ip address (0, or more than 1), this attribute will
149e705c121SKalle Valo  *	be skipped.
150e705c121SKalle Valo  * BC_FILTER_MAGIC_MAC - set the val of this attribute to
151e705c121SKalle Valo  *	the LSB bytes of the vif's mac address
152e705c121SKalle Valo  */
153e705c121SKalle Valo enum {
154e705c121SKalle Valo 	BC_FILTER_MAGIC_NONE = 0,
155e705c121SKalle Valo 	BC_FILTER_MAGIC_IP,
156e705c121SKalle Valo 	BC_FILTER_MAGIC_MAC,
157e705c121SKalle Valo };
158e705c121SKalle Valo 
159e705c121SKalle Valo static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
160e705c121SKalle Valo 	{
161e705c121SKalle Valo 		/* arp */
162e705c121SKalle Valo 		.discard = 0,
163e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
164e705c121SKalle Valo 		.attrs = {
165e705c121SKalle Valo 			{
166e705c121SKalle Valo 				/* frame type - arp, hw type - ethernet */
167e705c121SKalle Valo 				.offset_type =
168e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
169e705c121SKalle Valo 				.offset = sizeof(rfc1042_header),
170e705c121SKalle Valo 				.val = cpu_to_be32(0x08060001),
171e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
172e705c121SKalle Valo 			},
173e705c121SKalle Valo 			{
174e705c121SKalle Valo 				/* arp dest ip */
175e705c121SKalle Valo 				.offset_type =
176e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
177e705c121SKalle Valo 				.offset = sizeof(rfc1042_header) + 2 +
178e705c121SKalle Valo 					  sizeof(struct arphdr) +
179e705c121SKalle Valo 					  ETH_ALEN + sizeof(__be32) +
180e705c121SKalle Valo 					  ETH_ALEN,
181e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
182e705c121SKalle Valo 				/* mark it as special field */
183e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
184e705c121SKalle Valo 			},
185e705c121SKalle Valo 		},
186e705c121SKalle Valo 	},
187e705c121SKalle Valo 	{
188e705c121SKalle Valo 		/* dhcp offer bcast */
189e705c121SKalle Valo 		.discard = 0,
190e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
191e705c121SKalle Valo 		.attrs = {
192e705c121SKalle Valo 			{
193e705c121SKalle Valo 				/* udp dest port - 68 (bootp client)*/
194e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
195e705c121SKalle Valo 				.offset = offsetof(struct udphdr, dest),
196e705c121SKalle Valo 				.val = cpu_to_be32(0x00440000),
197e705c121SKalle Valo 				.mask = cpu_to_be32(0xffff0000),
198e705c121SKalle Valo 			},
199e705c121SKalle Valo 			{
200e705c121SKalle Valo 				/* dhcp - lsb bytes of client hw address */
201e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
202e705c121SKalle Valo 				.offset = 38,
203e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
204e705c121SKalle Valo 				/* mark it as special field */
205e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
206e705c121SKalle Valo 			},
207e705c121SKalle Valo 		},
208e705c121SKalle Valo 	},
209e705c121SKalle Valo 	/* last filter must be empty */
210e705c121SKalle Valo 	{},
211e705c121SKalle Valo };
212e705c121SKalle Valo #endif
213e705c121SKalle Valo 
214e705c121SKalle Valo void iwl_mvm_ref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
215e705c121SKalle Valo {
216e705c121SKalle Valo 	if (!iwl_mvm_is_d0i3_supported(mvm))
217e705c121SKalle Valo 		return;
218e705c121SKalle Valo 
219e705c121SKalle Valo 	IWL_DEBUG_RPM(mvm, "Take mvm reference - type %d\n", ref_type);
220e705c121SKalle Valo 	spin_lock_bh(&mvm->refs_lock);
221e705c121SKalle Valo 	mvm->refs[ref_type]++;
222e705c121SKalle Valo 	spin_unlock_bh(&mvm->refs_lock);
223e705c121SKalle Valo 	iwl_trans_ref(mvm->trans);
224e705c121SKalle Valo }
225e705c121SKalle Valo 
226e705c121SKalle Valo void iwl_mvm_unref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
227e705c121SKalle Valo {
228e705c121SKalle Valo 	if (!iwl_mvm_is_d0i3_supported(mvm))
229e705c121SKalle Valo 		return;
230e705c121SKalle Valo 
231e705c121SKalle Valo 	IWL_DEBUG_RPM(mvm, "Leave mvm reference - type %d\n", ref_type);
232e705c121SKalle Valo 	spin_lock_bh(&mvm->refs_lock);
233e705c121SKalle Valo 	WARN_ON(!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 
327e705c121SKalle Valo 	if (changed)
328e705c121SKalle Valo 		*changed = (resp->status == MCC_RESP_NEW_CHAN_PROFILE);
329e705c121SKalle Valo 
330e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
331e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
332e705c121SKalle Valo 				      resp->channels,
333e705c121SKalle Valo 				      __le16_to_cpu(resp->mcc));
334e705c121SKalle Valo 	/* Store the return source id */
335e705c121SKalle Valo 	src_id = resp->source_id;
336e705c121SKalle Valo 	kfree(resp);
337e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
338e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
339e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
340e705c121SKalle Valo 		goto out;
341e705c121SKalle Valo 	}
342e705c121SKalle Valo 
343e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
344e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
345e705c121SKalle Valo 	mvm->lar_regdom_set = true;
346e705c121SKalle Valo 	mvm->mcc_src = src_id;
347e705c121SKalle Valo 
348e705c121SKalle Valo out:
349e705c121SKalle Valo 	return regd;
350e705c121SKalle Valo }
351e705c121SKalle Valo 
352e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
353e705c121SKalle Valo {
354e705c121SKalle Valo 	bool changed;
355e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
356e705c121SKalle Valo 
357e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
358e705c121SKalle Valo 		return;
359e705c121SKalle Valo 
360e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
361e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
362e705c121SKalle Valo 		/* only update the regulatory core if changed */
363e705c121SKalle Valo 		if (changed)
364e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
365e705c121SKalle Valo 
366e705c121SKalle Valo 		kfree(regd);
367e705c121SKalle Valo 	}
368e705c121SKalle Valo }
369e705c121SKalle Valo 
370e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
371e705c121SKalle Valo 							  bool *changed)
372e705c121SKalle Valo {
373e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
374e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
375e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
376e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
377e705c121SKalle Valo }
378e705c121SKalle Valo 
379e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
380e705c121SKalle Valo {
381e705c121SKalle Valo 	enum iwl_mcc_source used_src;
382e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
383e705c121SKalle Valo 	int ret;
384e705c121SKalle Valo 	bool changed;
385e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
386e705c121SKalle Valo 			rtnl_dereference(mvm->hw->wiphy->regd);
387e705c121SKalle Valo 
388e705c121SKalle Valo 	if (!r)
389e705c121SKalle Valo 		return -ENOENT;
390e705c121SKalle Valo 
391e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
392e705c121SKalle Valo 	used_src = mvm->mcc_src;
393e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
394e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
395e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
396e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
397e705c121SKalle Valo 			kfree(regd);
398e705c121SKalle Valo 	}
399e705c121SKalle Valo 
400e705c121SKalle Valo 	/* Now set our last stored MCC and source */
401e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
402e705c121SKalle Valo 				     &changed);
403e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
404e705c121SKalle Valo 		return -EIO;
405e705c121SKalle Valo 
406e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
407e705c121SKalle Valo 	if (changed)
408e705c121SKalle Valo 		ret = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
409e705c121SKalle Valo 	else
410e705c121SKalle Valo 		ret = 0;
411e705c121SKalle Valo 
412e705c121SKalle Valo 	kfree(regd);
413e705c121SKalle Valo 	return ret;
414e705c121SKalle Valo }
415e705c121SKalle Valo 
416e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
417e705c121SKalle Valo {
418e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
419e705c121SKalle Valo 	int num_mac, ret, i;
420e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
421e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
422e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
423e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
424e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
425e705c121SKalle Valo 	};
426e705c121SKalle Valo 
427e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
428e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
429e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
430e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
431e705c121SKalle Valo 	ieee80211_hw_set(hw, QUEUE_CONTROL);
432e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
433e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
434e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
435e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
436e705c121SKalle Valo 	ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
437e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
438e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
439e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
440e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
441e705c121SKalle Valo 
4422d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
4432d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
4442d7cf549SJohannes Berg 
445e705c121SKalle Valo 	hw->queues = mvm->first_agg_queue;
446e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
447e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
448e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
449e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
450e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
451e705c121SKalle Valo 	hw->rate_control_algorithm = "iwl-mvm-rs";
452e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
453e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
454e705c121SKalle Valo 
455e705c121SKalle Valo 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 2);
456e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
457e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
458e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
459e705c121SKalle Valo 
460e705c121SKalle Valo 	/*
461e705c121SKalle Valo 	 * Enable 11w if advertised by firmware and software crypto
462e705c121SKalle Valo 	 * is not enabled (as the firmware will interpret some mgmt
463e705c121SKalle Valo 	 * packets, so enabling it with software crypto isn't safe)
464e705c121SKalle Valo 	 */
465e705c121SKalle Valo 	if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
466e705c121SKalle Valo 	    !iwlwifi_mod_params.sw_crypto) {
467e705c121SKalle Valo 		ieee80211_hw_set(hw, MFP_CAPABLE);
468e705c121SKalle Valo 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
469e705c121SKalle Valo 			WLAN_CIPHER_SUITE_AES_CMAC;
470e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
471e705c121SKalle Valo 	}
472e705c121SKalle Valo 
473e705c121SKalle Valo 	/* currently FW API supports only one optional cipher scheme */
474e705c121SKalle Valo 	if (mvm->fw->cs[0].cipher) {
475e705c121SKalle Valo 		mvm->hw->n_cipher_schemes = 1;
476e705c121SKalle Valo 		mvm->hw->cipher_schemes = &mvm->fw->cs[0];
477e705c121SKalle Valo 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
478e705c121SKalle Valo 			mvm->fw->cs[0].cipher;
479e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
480e705c121SKalle Valo 	}
481e705c121SKalle Valo 
482e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
483e705c121SKalle Valo 	hw->wiphy->features |=
484e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
485e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
486e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
487e705c121SKalle Valo 
488e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
489e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
490e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
491e705c121SKalle Valo 
492e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
493e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
494e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
495e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
496e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
497e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
498e705c121SKalle Valo 
499e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
500e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
501e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
502e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
503e705c121SKalle Valo 	else
504e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
505e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
506e705c121SKalle Valo 
507e705c121SKalle Valo 	if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_GO_UAPSD)
508e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
509e705c121SKalle Valo 
510e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
511e705c121SKalle Valo 
512e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
513e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
514e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
515e705c121SKalle Valo 
516e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
517e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
518e705c121SKalle Valo 	/* we can compensate an offset of up to 3 channels = 15 MHz */
519e705c121SKalle Valo 	hw->wiphy->max_adj_channel_rssi_comp = 3 * 5;
520e705c121SKalle Valo 
521e705c121SKalle Valo 	/* Extract MAC address */
522e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
523e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
524e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
525e705c121SKalle Valo 
526e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
527e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
528e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
529e705c121SKalle Valo 
530e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
531e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
532e705c121SKalle Valo 		       ETH_ALEN);
533e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
534e705c121SKalle Valo 		hw->wiphy->n_addresses++;
535e705c121SKalle Valo 	}
536e705c121SKalle Valo 
537e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
538e705c121SKalle Valo 
539e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
540e705c121SKalle Valo 
541e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
542e705c121SKalle Valo 
543e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
544e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
545e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
546e705c121SKalle Valo 
547e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
548e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
549e705c121SKalle Valo 	else
550e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
551e705c121SKalle Valo 
552e705c121SKalle Valo 	if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
553e705c121SKalle Valo 		hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
554e705c121SKalle Valo 			&mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
555e705c121SKalle Valo 	if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels) {
556e705c121SKalle Valo 		hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
557e705c121SKalle Valo 			&mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
558e705c121SKalle Valo 
559e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
560e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
561e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
562e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
563e705c121SKalle Valo 			hw->wiphy->bands[IEEE80211_BAND_5GHZ]->vht_cap.cap |=
564e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
565e705c121SKalle Valo 	}
566e705c121SKalle Valo 
567e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
568e705c121SKalle Valo 
569e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
570e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
571e705c121SKalle Valo 	else
572e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
573e705c121SKalle Valo 
574e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
575e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
576e705c121SKalle Valo 	hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
577e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
578e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
579e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
580e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
581e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
582e705c121SKalle Valo 
583e705c121SKalle Valo 	/*
584e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
585e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
586e705c121SKalle Valo 	 */
587e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
588e705c121SKalle Valo 
589e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
590e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
591e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
592e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
593e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
594e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
595e705c121SKalle Valo 
596e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
597e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
598e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
599e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
600e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
601e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
602e705c121SKalle Valo 
603e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
604e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
605e705c121SKalle Valo 		hw->wiphy->features |=
606e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
607e705c121SKalle Valo 
608e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
609e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
610e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
611e705c121SKalle Valo 
612e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
613e705c121SKalle Valo 
614e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
615e705c121SKalle Valo 	if (iwl_mvm_is_d0i3_supported(mvm) &&
616e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
617e705c121SKalle Valo 		mvm->wowlan.flags = WIPHY_WOWLAN_ANY;
618e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
619e705c121SKalle Valo 	}
620e705c121SKalle Valo 
621e705c121SKalle Valo 	if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
622e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
623e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
624e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
625e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
626e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
627e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
628e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
629e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
630e705c121SKalle Valo 		if (!iwlwifi_mod_params.sw_crypto)
631e705c121SKalle Valo 			mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
632e705c121SKalle Valo 					     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
633e705c121SKalle Valo 					     WIPHY_WOWLAN_4WAY_HANDSHAKE;
634e705c121SKalle Valo 
635e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
636e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
637e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
638e705c121SKalle Valo 		mvm->wowlan.max_nd_match_sets = IWL_SCAN_MAX_PROFILES;
639e705c121SKalle Valo 		mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
640e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
641e705c121SKalle Valo 	}
642e705c121SKalle Valo #endif
643e705c121SKalle Valo 
644e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
645e705c121SKalle Valo 	/* assign default bcast filtering configuration */
646e705c121SKalle Valo 	mvm->bcast_filters = iwl_mvm_default_bcast_filters;
647e705c121SKalle Valo #endif
648e705c121SKalle Valo 
649e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
650e705c121SKalle Valo 	if (ret)
651e705c121SKalle Valo 		return ret;
652e705c121SKalle Valo 
653e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
654e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
655e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
656e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
657e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
658e705c121SKalle Valo 	}
659e705c121SKalle Valo 
660e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
661e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
662e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
663e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
664e705c121SKalle Valo 	}
665e705c121SKalle Valo 
666e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
667e705c121SKalle Valo 	if (!iwl_mvm_is_csum_supported(mvm))
668e705c121SKalle Valo 		hw->netdev_features &= ~NETIF_F_RXCSUM;
669e705c121SKalle Valo 
67041837ca9SEmmanuel Grumbach 	if (IWL_MVM_SW_TX_CSUM_OFFLOAD)
671a3713f8bSEmmanuel Grumbach 		hw->netdev_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
672a3713f8bSEmmanuel Grumbach 			NETIF_F_TSO | NETIF_F_TSO6;
67341837ca9SEmmanuel Grumbach 
674e705c121SKalle Valo 	ret = ieee80211_register_hw(mvm->hw);
675e705c121SKalle Valo 	if (ret)
676e705c121SKalle Valo 		iwl_mvm_leds_exit(mvm);
677e705c121SKalle Valo 
678e705c121SKalle Valo 	return ret;
679e705c121SKalle Valo }
680e705c121SKalle Valo 
681e705c121SKalle Valo static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
682e705c121SKalle Valo 			     struct ieee80211_sta *sta,
683e705c121SKalle Valo 			     struct sk_buff *skb)
684e705c121SKalle Valo {
685e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
686e705c121SKalle Valo 	bool defer = false;
687e705c121SKalle Valo 
688e705c121SKalle Valo 	/*
689e705c121SKalle Valo 	 * double check the IN_D0I3 flag both before and after
690e705c121SKalle Valo 	 * taking the spinlock, in order to prevent taking
691e705c121SKalle Valo 	 * the spinlock when not needed.
692e705c121SKalle Valo 	 */
693e705c121SKalle Valo 	if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
694e705c121SKalle Valo 		return false;
695e705c121SKalle Valo 
696e705c121SKalle Valo 	spin_lock(&mvm->d0i3_tx_lock);
697e705c121SKalle Valo 	/*
698e705c121SKalle Valo 	 * testing the flag again ensures the skb dequeue
699e705c121SKalle Valo 	 * loop (on d0i3 exit) hasn't run yet.
700e705c121SKalle Valo 	 */
701e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
702e705c121SKalle Valo 		goto out;
703e705c121SKalle Valo 
704e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
705e705c121SKalle Valo 	if (mvmsta->sta_id == IWL_MVM_STATION_COUNT ||
706e705c121SKalle Valo 	    mvmsta->sta_id != mvm->d0i3_ap_sta_id)
707e705c121SKalle Valo 		goto out;
708e705c121SKalle Valo 
709e705c121SKalle Valo 	__skb_queue_tail(&mvm->d0i3_tx, skb);
710e705c121SKalle Valo 	ieee80211_stop_queues(mvm->hw);
711e705c121SKalle Valo 
712e705c121SKalle Valo 	/* trigger wakeup */
713e705c121SKalle Valo 	iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
714e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
715e705c121SKalle Valo 
716e705c121SKalle Valo 	defer = true;
717e705c121SKalle Valo out:
718e705c121SKalle Valo 	spin_unlock(&mvm->d0i3_tx_lock);
719e705c121SKalle Valo 	return defer;
720e705c121SKalle Valo }
721e705c121SKalle Valo 
722e705c121SKalle Valo static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
723e705c121SKalle Valo 			   struct ieee80211_tx_control *control,
724e705c121SKalle Valo 			   struct sk_buff *skb)
725e705c121SKalle Valo {
726e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
727e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
728e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
729e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
730e705c121SKalle Valo 
731e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
732e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
733e705c121SKalle Valo 		goto drop;
734e705c121SKalle Valo 	}
735e705c121SKalle Valo 
736e705c121SKalle Valo 	if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
737e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
738e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
739e705c121SKalle Valo 		goto drop;
740e705c121SKalle Valo 
741e705c121SKalle Valo 	/* treat non-bufferable MMPDUs as broadcast if sta is sleeping */
742e705c121SKalle Valo 	if (unlikely(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER &&
743e705c121SKalle Valo 		     ieee80211_is_mgmt(hdr->frame_control) &&
744e705c121SKalle Valo 		     !ieee80211_is_deauth(hdr->frame_control) &&
745e705c121SKalle Valo 		     !ieee80211_is_disassoc(hdr->frame_control) &&
746e705c121SKalle Valo 		     !ieee80211_is_action(hdr->frame_control)))
747e705c121SKalle Valo 		sta = NULL;
748e705c121SKalle Valo 
749e705c121SKalle Valo 	if (sta) {
750e705c121SKalle Valo 		if (iwl_mvm_defer_tx(mvm, sta, skb))
751e705c121SKalle Valo 			return;
752e705c121SKalle Valo 		if (iwl_mvm_tx_skb(mvm, skb, sta))
753e705c121SKalle Valo 			goto drop;
754e705c121SKalle Valo 		return;
755e705c121SKalle Valo 	}
756e705c121SKalle Valo 
757e705c121SKalle Valo 	if (iwl_mvm_tx_skb_non_sta(mvm, skb))
758e705c121SKalle Valo 		goto drop;
759e705c121SKalle Valo 	return;
760e705c121SKalle Valo  drop:
761e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
762e705c121SKalle Valo }
763e705c121SKalle Valo 
764e705c121SKalle Valo static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
765e705c121SKalle Valo {
766e705c121SKalle Valo 	if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
767e705c121SKalle Valo 		return false;
768e705c121SKalle Valo 	return true;
769e705c121SKalle Valo }
770e705c121SKalle Valo 
771e705c121SKalle Valo static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
772e705c121SKalle Valo {
773e705c121SKalle Valo 	if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
774e705c121SKalle Valo 		return false;
775e705c121SKalle Valo 	if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
776e705c121SKalle Valo 		return true;
777e705c121SKalle Valo 
778e705c121SKalle Valo 	/* enabled by default */
779e705c121SKalle Valo 	return true;
780e705c121SKalle Valo }
781e705c121SKalle Valo 
782e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)	\
783e705c121SKalle Valo 	do {							\
784e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))	\
785e705c121SKalle Valo 			break;					\
786e705c121SKalle Valo 		iwl_mvm_fw_dbg_collect_trig(_mvm, _trig, _fmt);	\
787e705c121SKalle Valo 	} while (0)
788e705c121SKalle Valo 
789e705c121SKalle Valo static void
790e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
791e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
792e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
793e705c121SKalle Valo {
794e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
795e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
796e705c121SKalle Valo 
797e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
798e705c121SKalle Valo 		return;
799e705c121SKalle Valo 
800e705c121SKalle Valo 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
801e705c121SKalle Valo 	ba_trig = (void *)trig->data;
802e705c121SKalle Valo 
803e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trig))
804e705c121SKalle Valo 		return;
805e705c121SKalle Valo 
806e705c121SKalle Valo 	switch (action) {
807e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
808e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
809e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
810e705c121SKalle Valo 
811e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
812e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
813e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
814e705c121SKalle Valo 		break;
815e705c121SKalle Valo 		}
816e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
817e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
818e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
819e705c121SKalle Valo 				 sta->addr, tid);
820e705c121SKalle Valo 		break;
821e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
822e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
823e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
824e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
825e705c121SKalle Valo 		break;
826e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
827e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
828e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
829e705c121SKalle Valo 				 sta->addr, tid);
830e705c121SKalle Valo 		break;
831e705c121SKalle Valo 	default:
832e705c121SKalle Valo 		break;
833e705c121SKalle Valo 	}
834e705c121SKalle Valo }
835e705c121SKalle Valo 
836e705c121SKalle Valo static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
837e705c121SKalle Valo 				    struct ieee80211_vif *vif,
838e705c121SKalle Valo 				    enum ieee80211_ampdu_mlme_action action,
839e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tid,
840e705c121SKalle Valo 				    u16 *ssn, u8 buf_size, bool amsdu)
841e705c121SKalle Valo {
842e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
843e705c121SKalle Valo 	int ret;
844e705c121SKalle Valo 	bool tx_agg_ref = false;
845e705c121SKalle Valo 
846e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
847e705c121SKalle Valo 		     sta->addr, tid, action);
848e705c121SKalle Valo 
849e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
850e705c121SKalle Valo 		return -EACCES;
851e705c121SKalle Valo 
852e705c121SKalle Valo 	/* return from D0i3 before starting a new Tx aggregation */
853e705c121SKalle Valo 	switch (action) {
854e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
855e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
856e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
857e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
858e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
859e705c121SKalle Valo 		/*
860e705c121SKalle Valo 		 * for tx start, wait synchronously until D0i3 exit to
861e705c121SKalle Valo 		 * get the correct sequence number for the tid.
862e705c121SKalle Valo 		 * additionally, some other ampdu actions use direct
863e705c121SKalle Valo 		 * target access, which is not handled automatically
864e705c121SKalle Valo 		 * by the trans layer (unlike commands), so wait for
865e705c121SKalle Valo 		 * d0i3 exit in these cases as well.
866e705c121SKalle Valo 		 */
867e705c121SKalle Valo 		ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_TX_AGG);
868e705c121SKalle Valo 		if (ret)
869e705c121SKalle Valo 			return ret;
870e705c121SKalle Valo 
871e705c121SKalle Valo 		tx_agg_ref = true;
872e705c121SKalle Valo 		break;
873e705c121SKalle Valo 	default:
874e705c121SKalle Valo 		break;
875e705c121SKalle Valo 	}
876e705c121SKalle Valo 
877e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
878e705c121SKalle Valo 
879e705c121SKalle Valo 	switch (action) {
880e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
881e705c121SKalle Valo 		if (!iwl_enable_rx_ampdu(mvm->cfg)) {
882e705c121SKalle Valo 			ret = -EINVAL;
883e705c121SKalle Valo 			break;
884e705c121SKalle Valo 		}
885e705c121SKalle Valo 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
886e705c121SKalle Valo 		break;
887e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
888e705c121SKalle Valo 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
889e705c121SKalle Valo 		break;
890e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
891e705c121SKalle Valo 		if (!iwl_enable_tx_ampdu(mvm->cfg)) {
892e705c121SKalle Valo 			ret = -EINVAL;
893e705c121SKalle Valo 			break;
894e705c121SKalle Valo 		}
895e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
896e705c121SKalle Valo 		break;
897e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
898e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
899e705c121SKalle Valo 		break;
900e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
901e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
902e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
903e705c121SKalle Valo 		break;
904e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
905e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
906e705c121SKalle Valo 		break;
907e705c121SKalle Valo 	default:
908e705c121SKalle Valo 		WARN_ON_ONCE(1);
909e705c121SKalle Valo 		ret = -EINVAL;
910e705c121SKalle Valo 		break;
911e705c121SKalle Valo 	}
912e705c121SKalle Valo 
913e705c121SKalle Valo 	if (!ret) {
914e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
915e705c121SKalle Valo 
916e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
917e705c121SKalle Valo 			rx_ba_ssn = *ssn;
918e705c121SKalle Valo 
919e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
920e705c121SKalle Valo 					    rx_ba_ssn, action);
921e705c121SKalle Valo 	}
922e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
923e705c121SKalle Valo 
924e705c121SKalle Valo 	/*
925e705c121SKalle Valo 	 * If the tid is marked as started, we won't use it for offloaded
926e705c121SKalle Valo 	 * traffic on the next D0i3 entry. It's safe to unref.
927e705c121SKalle Valo 	 */
928e705c121SKalle Valo 	if (tx_agg_ref)
929e705c121SKalle Valo 		iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
930e705c121SKalle Valo 
931e705c121SKalle Valo 	return ret;
932e705c121SKalle Valo }
933e705c121SKalle Valo 
934e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
935e705c121SKalle Valo 				     struct ieee80211_vif *vif)
936e705c121SKalle Valo {
937e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
938e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
939e705c121SKalle Valo 
940e705c121SKalle Valo 	mvmvif->uploaded = false;
941e705c121SKalle Valo 	mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
942e705c121SKalle Valo 
943e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
944e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
945e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
946e705c121SKalle Valo 
947e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
948e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
949e705c121SKalle Valo }
950e705c121SKalle Valo 
951e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
952e705c121SKalle Valo {
953e705c121SKalle Valo 	/* clear the D3 reconfig, we only need it to avoid dumping a
954e705c121SKalle Valo 	 * firmware coredump on reconfiguration, we shouldn't do that
955e705c121SKalle Valo 	 * on D3->D0 transition
956e705c121SKalle Valo 	 */
957e705c121SKalle Valo 	if (!test_and_clear_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status)) {
958e705c121SKalle Valo 		mvm->fw_dump_desc = &iwl_mvm_dump_desc_assert;
959e705c121SKalle Valo 		iwl_mvm_fw_error_dump(mvm);
960e705c121SKalle Valo 	}
961e705c121SKalle Valo 
962e705c121SKalle Valo 	/* cleanup all stale references (scan, roc), but keep the
963e705c121SKalle Valo 	 * ucode_down ref until reconfig is complete
964e705c121SKalle Valo 	 */
965e705c121SKalle Valo 	iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
966e705c121SKalle Valo 
967e705c121SKalle Valo 	iwl_trans_stop_device(mvm->trans);
968e705c121SKalle Valo 
969e705c121SKalle Valo 	mvm->scan_status = 0;
970e705c121SKalle Valo 	mvm->ps_disabled = false;
971e705c121SKalle Valo 	mvm->calibrating = false;
972e705c121SKalle Valo 
973e705c121SKalle Valo 	/* just in case one was running */
974305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
975e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
976e705c121SKalle Valo 
977e705c121SKalle Valo 	/*
978e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
979e705c121SKalle Valo 	 * gone down during the HW restart
980e705c121SKalle Valo 	 */
981e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
982e705c121SKalle Valo 
983e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
984e705c121SKalle Valo 	mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
985e705c121SKalle Valo 
986e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
987*9c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
988e705c121SKalle Valo 	memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
989e705c121SKalle Valo 	memset(mvm->tfd_drained, 0, sizeof(mvm->tfd_drained));
990e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
991e705c121SKalle Valo 	memset(&mvm->last_bt_notif_old, 0, sizeof(mvm->last_bt_notif_old));
992e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
993e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd_old, 0, sizeof(mvm->last_bt_ci_cmd_old));
994e705c121SKalle Valo 	memset(&mvm->bt_ack_kill_msk, 0, sizeof(mvm->bt_ack_kill_msk));
995e705c121SKalle Valo 	memset(&mvm->bt_cts_kill_msk, 0, sizeof(mvm->bt_cts_kill_msk));
996e705c121SKalle Valo 
997e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
998e705c121SKalle Valo 
999e705c121SKalle Valo 	/* clear any stale d0i3 state */
1000e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
1001e705c121SKalle Valo 
1002e705c121SKalle Valo 	mvm->vif_count = 0;
1003e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
1004e705c121SKalle Valo 	mvm->fw_dbg_conf = FW_DBG_INVALID;
1005355346baSAvraham Stern 	mvm->scan_type = IWL_SCAN_TYPE_NOT_SET;
1006e705c121SKalle Valo 
1007e705c121SKalle Valo 	/* keep statistics ticking */
1008e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1009e705c121SKalle Valo }
1010e705c121SKalle Valo 
1011e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1012e705c121SKalle Valo {
1013e705c121SKalle Valo 	int ret;
1014e705c121SKalle Valo 
1015e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1016e705c121SKalle Valo 
1017a42b2af3SLuca Coelho 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1018e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1019e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1020a42b2af3SLuca Coelho 	} else {
1021a42b2af3SLuca Coelho 		/* Hold the reference to prevent runtime suspend while
1022a42b2af3SLuca Coelho 		 * the start procedure runs.  It's a bit confusing
1023a42b2af3SLuca Coelho 		 * that the UCODE_DOWN reference is taken, but it just
1024a42b2af3SLuca Coelho 		 * means "UCODE is not UP yet". ( TODO: rename this
1025a42b2af3SLuca Coelho 		 * reference).
1026a42b2af3SLuca Coelho 		 */
1027a42b2af3SLuca Coelho 		iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1028a42b2af3SLuca Coelho 	}
1029e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1030e705c121SKalle Valo 
1031e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1032e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1033e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1034e705c121SKalle Valo 		 * would do.
1035e705c121SKalle Valo 		 */
1036e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1037e705c121SKalle Valo 		iwl_mvm_d0i3_enable_tx(mvm, NULL);
1038e705c121SKalle Valo 	}
1039e705c121SKalle Valo 
1040e705c121SKalle Valo 	return ret;
1041e705c121SKalle Valo }
1042e705c121SKalle Valo 
1043e705c121SKalle Valo static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1044e705c121SKalle Valo {
1045e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1046e705c121SKalle Valo 	int ret;
1047e705c121SKalle Valo 
1048e705c121SKalle Valo 	/* Some hw restart cleanups must not hold the mutex */
1049e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1050e705c121SKalle Valo 		/*
1051e705c121SKalle Valo 		 * Make sure we are out of d0i3. This is needed
1052e705c121SKalle Valo 		 * to make sure the reference accounting is correct
1053e705c121SKalle Valo 		 * (and there is no stale d0i3_exit_work).
1054e705c121SKalle Valo 		 */
1055e705c121SKalle Valo 		wait_event_timeout(mvm->d0i3_exit_waitq,
1056e705c121SKalle Valo 				   !test_bit(IWL_MVM_STATUS_IN_D0I3,
1057e705c121SKalle Valo 					     &mvm->status),
1058e705c121SKalle Valo 				   HZ);
1059e705c121SKalle Valo 	}
1060e705c121SKalle Valo 
1061e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1062e705c121SKalle Valo 	ret = __iwl_mvm_mac_start(mvm);
1063e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1064e705c121SKalle Valo 
1065e705c121SKalle Valo 	return ret;
1066e705c121SKalle Valo }
1067e705c121SKalle Valo 
1068e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1069e705c121SKalle Valo {
1070e705c121SKalle Valo 	int ret;
1071e705c121SKalle Valo 
1072e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1073e705c121SKalle Valo 
1074e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1075e705c121SKalle Valo 	iwl_mvm_d0i3_enable_tx(mvm, NULL);
1076e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1077e705c121SKalle Valo 	if (ret)
1078e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1079e705c121SKalle Valo 			ret);
1080e705c121SKalle Valo 
1081e705c121SKalle Valo 	/* allow transport/FW low power modes */
1082e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
1083e705c121SKalle Valo 
1084e705c121SKalle Valo 	/*
1085e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1086e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1087e705c121SKalle Valo 	 */
1088e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1089e705c121SKalle Valo 
1090e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1091e705c121SKalle Valo }
1092e705c121SKalle Valo 
1093e705c121SKalle Valo static void iwl_mvm_resume_complete(struct iwl_mvm *mvm)
1094e705c121SKalle Valo {
1095b7282643SLuca Coelho 	if (iwl_mvm_is_d0i3_supported(mvm) &&
1096b7282643SLuca Coelho 	    iwl_mvm_enter_d0i3_on_suspend(mvm))
1097b7282643SLuca Coelho 		WARN_ONCE(!wait_event_timeout(mvm->d0i3_exit_waitq,
1098e705c121SKalle Valo 					      !test_bit(IWL_MVM_STATUS_IN_D0I3,
1099e705c121SKalle Valo 							&mvm->status),
1100b7282643SLuca Coelho 					      HZ),
1101b7282643SLuca Coelho 			  "D0i3 exit on resume timed out\n");
1102e705c121SKalle Valo }
1103e705c121SKalle Valo 
1104e705c121SKalle Valo static void
1105e705c121SKalle Valo iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1106e705c121SKalle Valo 			      enum ieee80211_reconfig_type reconfig_type)
1107e705c121SKalle Valo {
1108e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1109e705c121SKalle Valo 
1110e705c121SKalle Valo 	switch (reconfig_type) {
1111e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1112e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1113e705c121SKalle Valo 		break;
1114e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1115e705c121SKalle Valo 		iwl_mvm_resume_complete(mvm);
1116e705c121SKalle Valo 		break;
1117e705c121SKalle Valo 	}
1118e705c121SKalle Valo }
1119e705c121SKalle Valo 
1120e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1121e705c121SKalle Valo {
1122e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1123e705c121SKalle Valo 
1124e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1125e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1126e705c121SKalle Valo 	 */
1127e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1128e705c121SKalle Valo 
1129e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1130e705c121SKalle Valo 
1131e705c121SKalle Valo 	/*
1132e705c121SKalle Valo 	 * The work item could be running or queued if the
1133e705c121SKalle Valo 	 * ROC time event stops just as we get here.
1134e705c121SKalle Valo 	 */
1135e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
1136e705c121SKalle Valo 
1137e705c121SKalle Valo 	iwl_trans_stop_device(mvm->trans);
1138e705c121SKalle Valo 
1139e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1140e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1141e705c121SKalle Valo 
1142e705c121SKalle Valo 	/* the fw is stopped, the aux sta is dead: clean up driver state */
1143e705c121SKalle Valo 	iwl_mvm_del_aux_sta(mvm);
1144e705c121SKalle Valo 
1145e705c121SKalle Valo 	/*
1146e705c121SKalle Valo 	 * Clear IN_HW_RESTART flag when stopping the hw (as restart_complete()
1147e705c121SKalle Valo 	 * won't be called in this case).
1148e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1149e705c121SKalle Valo 	 * HW restart was requested.
1150e705c121SKalle Valo 	 */
1151e705c121SKalle Valo 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1152e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1153e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1154e705c121SKalle Valo 
1155e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1156e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1157e705c121SKalle Valo 	 */
1158e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1159e705c121SKalle Valo 		int i;
1160e705c121SKalle Valo 
1161e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1162e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1163e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1164e705c121SKalle Valo 				      i))
1165e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1166e705c121SKalle Valo 		}
1167e705c121SKalle Valo 	}
1168e705c121SKalle Valo 
1169e705c121SKalle Valo 	mvm->ucode_loaded = false;
1170e705c121SKalle Valo }
1171e705c121SKalle Valo 
1172e705c121SKalle Valo static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1173e705c121SKalle Valo {
1174e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1175e705c121SKalle Valo 
1176e705c121SKalle Valo 	flush_work(&mvm->d0i3_exit_work);
1177e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
1178e705c121SKalle Valo 	cancel_delayed_work_sync(&mvm->fw_dump_wk);
1179e705c121SKalle Valo 	iwl_mvm_free_fw_dump_desc(mvm);
1180e705c121SKalle Valo 
1181e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1182e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1183e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1184e705c121SKalle Valo 
1185e705c121SKalle Valo 	/*
1186e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1187e705c121SKalle Valo 	 * discover that its list is now empty.
1188e705c121SKalle Valo 	 */
1189e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1190e705c121SKalle Valo }
1191e705c121SKalle Valo 
1192e705c121SKalle Valo static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1193e705c121SKalle Valo {
1194e705c121SKalle Valo 	u16 i;
1195e705c121SKalle Valo 
1196e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1197e705c121SKalle Valo 
1198e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1199e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1200e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1201e705c121SKalle Valo 
1202e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1203e705c121SKalle Valo 	return NULL;
1204e705c121SKalle Valo }
1205e705c121SKalle Valo 
1206e705c121SKalle Valo static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1207e705c121SKalle Valo 				s16 tx_power)
1208e705c121SKalle Valo {
1209e705c121SKalle Valo 	struct iwl_dev_tx_power_cmd cmd = {
1210e705c121SKalle Valo 		.v2.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1211e705c121SKalle Valo 		.v2.mac_context_id =
1212e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1213e705c121SKalle Valo 		.v2.pwr_restriction = cpu_to_le16(8 * tx_power),
1214e705c121SKalle Valo 	};
1215e705c121SKalle Valo 	int len = sizeof(cmd);
1216e705c121SKalle Valo 
1217e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1218e705c121SKalle Valo 		cmd.v2.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1219e705c121SKalle Valo 
1220e705c121SKalle Valo 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_TX_POWER_CHAIN))
1221e705c121SKalle Valo 		len = sizeof(cmd.v2);
1222e705c121SKalle Valo 
1223e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
1224e705c121SKalle Valo }
1225e705c121SKalle Valo 
1226e705c121SKalle Valo static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1227e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1228e705c121SKalle Valo {
1229e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1230e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1231e705c121SKalle Valo 	int ret;
1232e705c121SKalle Valo 
1233e705c121SKalle Valo 	mvmvif->mvm = mvm;
1234e705c121SKalle Valo 
1235e705c121SKalle Valo 	/*
1236e705c121SKalle Valo 	 * make sure D0i3 exit is completed, otherwise a target access
1237e705c121SKalle Valo 	 * during tx queue configuration could be done when still in
1238e705c121SKalle Valo 	 * D0i3 state.
1239e705c121SKalle Valo 	 */
1240e705c121SKalle Valo 	ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_ADD_IF);
1241e705c121SKalle Valo 	if (ret)
1242e705c121SKalle Valo 		return ret;
1243e705c121SKalle Valo 
1244e705c121SKalle Valo 	/*
1245e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1246e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1247e705c121SKalle Valo 	 * don't really have to check the types.
1248e705c121SKalle Valo 	 */
1249e705c121SKalle Valo 
1250e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1251e705c121SKalle Valo 
1252e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1253e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1254e705c121SKalle Valo 		mvmvif->beacon_stats.accu_num_beacons +=
1255e705c121SKalle Valo 			mvmvif->beacon_stats.num_beacons;
1256e705c121SKalle Valo 
1257e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
1258e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1259e705c121SKalle Valo 	if (ret)
1260e705c121SKalle Valo 		goto out_unlock;
1261e705c121SKalle Valo 
1262e705c121SKalle Valo 	/* Counting number of interfaces is needed for legacy PM */
1263e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1264e705c121SKalle Valo 		mvm->vif_count++;
1265e705c121SKalle Valo 
1266e705c121SKalle Valo 	/*
1267e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1268e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1269e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1270e705c121SKalle Valo 	 * only after the binding.
1271e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1272e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1273e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1274e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1275e705c121SKalle Valo 	 * allocating resources :)
1276e705c121SKalle Valo 	 */
1277e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1278e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1279e705c121SKalle Valo 		ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1280e705c121SKalle Valo 		if (ret) {
1281e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1282e705c121SKalle Valo 			goto out_release;
1283e705c121SKalle Valo 		}
1284e705c121SKalle Valo 
1285e705c121SKalle Valo 		iwl_mvm_vif_dbgfs_register(mvm, vif);
1286e705c121SKalle Valo 		goto out_unlock;
1287e705c121SKalle Valo 	}
1288e705c121SKalle Valo 
1289e705c121SKalle Valo 	mvmvif->features |= hw->netdev_features;
1290e705c121SKalle Valo 
1291e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1292e705c121SKalle Valo 	if (ret)
1293e705c121SKalle Valo 		goto out_release;
1294e705c121SKalle Valo 
1295e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1296e705c121SKalle Valo 	if (ret)
1297e705c121SKalle Valo 		goto out_remove_mac;
1298e705c121SKalle Valo 
1299e705c121SKalle Valo 	/* beacon filtering */
1300e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1301e705c121SKalle Valo 	if (ret)
1302e705c121SKalle Valo 		goto out_remove_mac;
1303e705c121SKalle Valo 
1304e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1305e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1306e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1307e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1308e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1309e705c121SKalle Valo 	}
1310e705c121SKalle Valo 
1311e705c121SKalle Valo 	/*
1312e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1313e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1314e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1315e705c121SKalle Valo 	 */
1316e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1317e705c121SKalle Valo 
1318e705c121SKalle Valo 		mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1319e705c121SKalle Valo 		if (!mvmvif->phy_ctxt) {
1320e705c121SKalle Valo 			ret = -ENOSPC;
1321e705c121SKalle Valo 			goto out_free_bf;
1322e705c121SKalle Valo 		}
1323e705c121SKalle Valo 
1324e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1325e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1326e705c121SKalle Valo 		if (ret)
1327e705c121SKalle Valo 			goto out_unref_phy;
1328e705c121SKalle Valo 
1329e705c121SKalle Valo 		ret = iwl_mvm_add_bcast_sta(mvm, vif);
1330e705c121SKalle Valo 		if (ret)
1331e705c121SKalle Valo 			goto out_unbind;
1332e705c121SKalle Valo 
1333e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1334e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1335e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1336e705c121SKalle Valo 	}
1337e705c121SKalle Valo 
1338e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
1339e705c121SKalle Valo 	goto out_unlock;
1340e705c121SKalle Valo 
1341e705c121SKalle Valo  out_unbind:
1342e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1343e705c121SKalle Valo  out_unref_phy:
1344e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1345e705c121SKalle Valo  out_free_bf:
1346e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1347e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1348e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1349e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1350e705c121SKalle Valo 	}
1351e705c121SKalle Valo  out_remove_mac:
1352e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1353e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1354e705c121SKalle Valo  out_release:
1355e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1356e705c121SKalle Valo 		mvm->vif_count--;
1357e705c121SKalle Valo 
1358e705c121SKalle Valo 	iwl_mvm_mac_ctxt_release(mvm, vif);
1359e705c121SKalle Valo  out_unlock:
1360e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1361e705c121SKalle Valo 
1362e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_ADD_IF);
1363e705c121SKalle Valo 
1364e705c121SKalle Valo 	return ret;
1365e705c121SKalle Valo }
1366e705c121SKalle Valo 
1367e705c121SKalle Valo static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1368e705c121SKalle Valo 					struct ieee80211_vif *vif)
1369e705c121SKalle Valo {
1370e705c121SKalle Valo 	u32 tfd_msk = iwl_mvm_mac_get_queues_mask(vif);
1371e705c121SKalle Valo 
1372e705c121SKalle Valo 	if (tfd_msk) {
1373e705c121SKalle Valo 		/*
1374e705c121SKalle Valo 		 * mac80211 first removes all the stations of the vif and
1375e705c121SKalle Valo 		 * then removes the vif. When it removes a station it also
1376e705c121SKalle Valo 		 * flushes the AMPDU session. So by now, all the AMPDU sessions
1377e705c121SKalle Valo 		 * of all the stations of this vif are closed, and the queues
1378e705c121SKalle Valo 		 * of these AMPDU sessions are properly closed.
1379e705c121SKalle Valo 		 * We still need to take care of the shared queues of the vif.
1380e705c121SKalle Valo 		 * Flush them here.
1381e705c121SKalle Valo 		 */
1382e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
1383e705c121SKalle Valo 		iwl_mvm_flush_tx_path(mvm, tfd_msk, 0);
1384e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
1385e705c121SKalle Valo 
1386e705c121SKalle Valo 		/*
1387e705c121SKalle Valo 		 * There are transports that buffer a few frames in the host.
1388e705c121SKalle Valo 		 * For these, the flush above isn't enough since while we were
1389e705c121SKalle Valo 		 * flushing, the transport might have sent more frames to the
1390e705c121SKalle Valo 		 * device. To solve this, wait here until the transport is
1391e705c121SKalle Valo 		 * empty. Technically, this could have replaced the flush
1392e705c121SKalle Valo 		 * above, but flush is much faster than draining. So flush
1393e705c121SKalle Valo 		 * first, and drain to make sure we have no frames in the
1394e705c121SKalle Valo 		 * transport anymore.
1395e705c121SKalle Valo 		 * If a station still had frames on the shared queues, it is
1396e705c121SKalle Valo 		 * already marked as draining, so to complete the draining, we
1397e705c121SKalle Valo 		 * just need to wait until the transport is empty.
1398e705c121SKalle Valo 		 */
1399e705c121SKalle Valo 		iwl_trans_wait_tx_queue_empty(mvm->trans, tfd_msk);
1400e705c121SKalle Valo 	}
1401e705c121SKalle Valo 
1402e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1403e705c121SKalle Valo 		/*
1404e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1405e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1406e705c121SKalle Valo 		 * queue are sent in ROC session.
1407e705c121SKalle Valo 		 */
1408e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1409e705c121SKalle Valo 	} else {
1410e705c121SKalle Valo 		/*
1411e705c121SKalle Valo 		 * By now, all the AC queues are empty. The AGG queues are
1412e705c121SKalle Valo 		 * empty too. We already got all the Tx responses for all the
1413e705c121SKalle Valo 		 * packets in the queues. The drain work can have been
1414e705c121SKalle Valo 		 * triggered. Flush it.
1415e705c121SKalle Valo 		 */
1416e705c121SKalle Valo 		flush_work(&mvm->sta_drained_wk);
1417e705c121SKalle Valo 	}
1418e705c121SKalle Valo }
1419e705c121SKalle Valo 
1420e705c121SKalle Valo static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1421e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1422e705c121SKalle Valo {
1423e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1424e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1425e705c121SKalle Valo 
1426e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1427e705c121SKalle Valo 
1428e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1429e705c121SKalle Valo 
1430e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1431e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1432e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1433e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1434e705c121SKalle Valo 	}
1435e705c121SKalle Valo 
1436e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1437e705c121SKalle Valo 
1438e705c121SKalle Valo 	/*
1439e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1440e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1441e705c121SKalle Valo 	 */
1442e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1443e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1444e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1445e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1446e705c121SKalle Valo 			mvm->noa_vif = NULL;
1447e705c121SKalle Valo 			mvm->noa_duration = 0;
1448e705c121SKalle Valo 		}
1449e705c121SKalle Valo #endif
1450e705c121SKalle Valo 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1451e705c121SKalle Valo 		goto out_release;
1452e705c121SKalle Valo 	}
1453e705c121SKalle Valo 
1454e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1455e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1456e705c121SKalle Valo 		iwl_mvm_rm_bcast_sta(mvm, vif);
1457e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1458e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1459e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
1460e705c121SKalle Valo 	}
1461e705c121SKalle Valo 
1462e705c121SKalle Valo 	if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1463e705c121SKalle Valo 		mvm->vif_count--;
1464e705c121SKalle Valo 
1465e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
1466e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1467e705c121SKalle Valo 
1468e705c121SKalle Valo out_release:
1469e705c121SKalle Valo 	iwl_mvm_mac_ctxt_release(mvm, vif);
1470e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1471e705c121SKalle Valo }
1472e705c121SKalle Valo 
1473e705c121SKalle Valo static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1474e705c121SKalle Valo {
1475e705c121SKalle Valo 	return 0;
1476e705c121SKalle Valo }
1477e705c121SKalle Valo 
1478e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1479e705c121SKalle Valo 	struct iwl_mvm *mvm;
1480e705c121SKalle Valo 	int port_id;
1481e705c121SKalle Valo };
1482e705c121SKalle Valo 
1483e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1484e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1485e705c121SKalle Valo {
1486e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1487e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1488e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1489e705c121SKalle Valo 	int ret, len;
1490e705c121SKalle Valo 
1491e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1492e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1493e705c121SKalle Valo 		return;
1494e705c121SKalle Valo 
1495e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1496e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1497e705c121SKalle Valo 		return;
1498e705c121SKalle Valo 
1499e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1500e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1501e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1502e705c121SKalle Valo 
1503e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_ASYNC, len, cmd);
1504e705c121SKalle Valo 	if (ret)
1505e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1506e705c121SKalle Valo }
1507e705c121SKalle Valo 
1508e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1509e705c121SKalle Valo {
1510e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1511e705c121SKalle Valo 		.mvm = mvm,
1512e705c121SKalle Valo 	};
1513e705c121SKalle Valo 
1514e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1515e705c121SKalle Valo 
1516e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1517e705c121SKalle Valo 		return;
1518e705c121SKalle Valo 
1519e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1520e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1521e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1522e705c121SKalle Valo }
1523e705c121SKalle Valo 
1524e705c121SKalle Valo static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1525e705c121SKalle Valo 				     struct netdev_hw_addr_list *mc_list)
1526e705c121SKalle Valo {
1527e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1528e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1529e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1530e705c121SKalle Valo 	int addr_count;
1531e705c121SKalle Valo 	bool pass_all;
1532e705c121SKalle Valo 	int len;
1533e705c121SKalle Valo 
1534e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1535e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1536e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1537e705c121SKalle Valo 	if (pass_all)
1538e705c121SKalle Valo 		addr_count = 0;
1539e705c121SKalle Valo 
1540e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1541e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1542e705c121SKalle Valo 	if (!cmd)
1543e705c121SKalle Valo 		return 0;
1544e705c121SKalle Valo 
1545e705c121SKalle Valo 	if (pass_all) {
1546e705c121SKalle Valo 		cmd->pass_all = 1;
1547e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1548e705c121SKalle Valo 	}
1549e705c121SKalle Valo 
1550e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1551e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1552e705c121SKalle Valo 				   cmd->count, addr->addr);
1553e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1554e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1555e705c121SKalle Valo 		cmd->count++;
1556e705c121SKalle Valo 	}
1557e705c121SKalle Valo 
1558e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1559e705c121SKalle Valo }
1560e705c121SKalle Valo 
1561e705c121SKalle Valo static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1562e705c121SKalle Valo 				     unsigned int changed_flags,
1563e705c121SKalle Valo 				     unsigned int *total_flags,
1564e705c121SKalle Valo 				     u64 multicast)
1565e705c121SKalle Valo {
1566e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1567e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1568e705c121SKalle Valo 
1569e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1570e705c121SKalle Valo 
1571e705c121SKalle Valo 	/* replace previous configuration */
1572e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1573e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1574e705c121SKalle Valo 
1575e705c121SKalle Valo 	if (!cmd)
1576e705c121SKalle Valo 		goto out;
1577e705c121SKalle Valo 
1578e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1579e705c121SKalle Valo out:
1580e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1581e705c121SKalle Valo 	*total_flags = 0;
1582e705c121SKalle Valo }
1583e705c121SKalle Valo 
1584e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1585e705c121SKalle Valo 					struct ieee80211_vif *vif,
1586e705c121SKalle Valo 					unsigned int filter_flags,
1587e705c121SKalle Valo 					unsigned int changed_flags)
1588e705c121SKalle Valo {
1589e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1590e705c121SKalle Valo 
1591e705c121SKalle Valo 	/* We support only filter for probe requests */
1592e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1593e705c121SKalle Valo 		return;
1594e705c121SKalle Valo 
1595e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1596e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1597e705c121SKalle Valo 	    !vif->p2p)
1598e705c121SKalle Valo 		return;
1599e705c121SKalle Valo 
1600e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1601e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1602e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1603e705c121SKalle Valo }
1604e705c121SKalle Valo 
1605e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1606e705c121SKalle Valo struct iwl_bcast_iter_data {
1607e705c121SKalle Valo 	struct iwl_mvm *mvm;
1608e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd;
1609e705c121SKalle Valo 	u8 current_filter;
1610e705c121SKalle Valo };
1611e705c121SKalle Valo 
1612e705c121SKalle Valo static void
1613e705c121SKalle Valo iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1614e705c121SKalle Valo 			 const struct iwl_fw_bcast_filter *in_filter,
1615e705c121SKalle Valo 			 struct iwl_fw_bcast_filter *out_filter)
1616e705c121SKalle Valo {
1617e705c121SKalle Valo 	struct iwl_fw_bcast_filter_attr *attr;
1618e705c121SKalle Valo 	int i;
1619e705c121SKalle Valo 
1620e705c121SKalle Valo 	memcpy(out_filter, in_filter, sizeof(*out_filter));
1621e705c121SKalle Valo 
1622e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1623e705c121SKalle Valo 		attr = &out_filter->attrs[i];
1624e705c121SKalle Valo 
1625e705c121SKalle Valo 		if (!attr->mask)
1626e705c121SKalle Valo 			break;
1627e705c121SKalle Valo 
1628e705c121SKalle Valo 		switch (attr->reserved1) {
1629e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_IP):
1630e705c121SKalle Valo 			if (vif->bss_conf.arp_addr_cnt != 1) {
1631e705c121SKalle Valo 				attr->mask = 0;
1632e705c121SKalle Valo 				continue;
1633e705c121SKalle Valo 			}
1634e705c121SKalle Valo 
1635e705c121SKalle Valo 			attr->val = vif->bss_conf.arp_addr_list[0];
1636e705c121SKalle Valo 			break;
1637e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1638e705c121SKalle Valo 			attr->val = *(__be32 *)&vif->addr[2];
1639e705c121SKalle Valo 			break;
1640e705c121SKalle Valo 		default:
1641e705c121SKalle Valo 			break;
1642e705c121SKalle Valo 		}
1643e705c121SKalle Valo 		attr->reserved1 = 0;
1644e705c121SKalle Valo 		out_filter->num_attrs++;
1645e705c121SKalle Valo 	}
1646e705c121SKalle Valo }
1647e705c121SKalle Valo 
1648e705c121SKalle Valo static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1649e705c121SKalle Valo 					  struct ieee80211_vif *vif)
1650e705c121SKalle Valo {
1651e705c121SKalle Valo 	struct iwl_bcast_iter_data *data = _data;
1652e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1653e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd = data->cmd;
1654e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1655e705c121SKalle Valo 	struct iwl_fw_bcast_mac *bcast_mac;
1656e705c121SKalle Valo 	int i;
1657e705c121SKalle Valo 
1658e705c121SKalle Valo 	if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1659e705c121SKalle Valo 		return;
1660e705c121SKalle Valo 
1661e705c121SKalle Valo 	bcast_mac = &cmd->macs[mvmvif->id];
1662e705c121SKalle Valo 
1663e705c121SKalle Valo 	/*
1664e705c121SKalle Valo 	 * enable filtering only for associated stations, but not for P2P
1665e705c121SKalle Valo 	 * Clients
1666e705c121SKalle Valo 	 */
1667e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1668e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1669e705c121SKalle Valo 		return;
1670e705c121SKalle Valo 
1671e705c121SKalle Valo 	bcast_mac->default_discard = 1;
1672e705c121SKalle Valo 
1673e705c121SKalle Valo 	/* copy all configured filters */
1674e705c121SKalle Valo 	for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1675e705c121SKalle Valo 		/*
1676e705c121SKalle Valo 		 * Make sure we don't exceed our filters limit.
1677e705c121SKalle Valo 		 * if there is still a valid filter to be configured,
1678e705c121SKalle Valo 		 * be on the safe side and just allow bcast for this mac.
1679e705c121SKalle Valo 		 */
1680e705c121SKalle Valo 		if (WARN_ON_ONCE(data->current_filter >=
1681e705c121SKalle Valo 				 ARRAY_SIZE(cmd->filters))) {
1682e705c121SKalle Valo 			bcast_mac->default_discard = 0;
1683e705c121SKalle Valo 			bcast_mac->attached_filters = 0;
1684e705c121SKalle Valo 			break;
1685e705c121SKalle Valo 		}
1686e705c121SKalle Valo 
1687e705c121SKalle Valo 		iwl_mvm_set_bcast_filter(vif,
1688e705c121SKalle Valo 					 &mvm->bcast_filters[i],
1689e705c121SKalle Valo 					 &cmd->filters[data->current_filter]);
1690e705c121SKalle Valo 
1691e705c121SKalle Valo 		/* skip current filter if it contains no attributes */
1692e705c121SKalle Valo 		if (!cmd->filters[data->current_filter].num_attrs)
1693e705c121SKalle Valo 			continue;
1694e705c121SKalle Valo 
1695e705c121SKalle Valo 		/* attach the filter to current mac */
1696e705c121SKalle Valo 		bcast_mac->attached_filters |=
1697e705c121SKalle Valo 				cpu_to_le16(BIT(data->current_filter));
1698e705c121SKalle Valo 
1699e705c121SKalle Valo 		data->current_filter++;
1700e705c121SKalle Valo 	}
1701e705c121SKalle Valo }
1702e705c121SKalle Valo 
1703e705c121SKalle Valo bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1704e705c121SKalle Valo 				    struct iwl_bcast_filter_cmd *cmd)
1705e705c121SKalle Valo {
1706e705c121SKalle Valo 	struct iwl_bcast_iter_data iter_data = {
1707e705c121SKalle Valo 		.mvm = mvm,
1708e705c121SKalle Valo 		.cmd = cmd,
1709e705c121SKalle Valo 	};
1710e705c121SKalle Valo 
1711e705c121SKalle Valo 	if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1712e705c121SKalle Valo 		return false;
1713e705c121SKalle Valo 
1714e705c121SKalle Valo 	memset(cmd, 0, sizeof(*cmd));
1715e705c121SKalle Valo 	cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1716e705c121SKalle Valo 	cmd->max_macs = ARRAY_SIZE(cmd->macs);
1717e705c121SKalle Valo 
1718e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
1719e705c121SKalle Valo 	/* use debugfs filters/macs if override is configured */
1720e705c121SKalle Valo 	if (mvm->dbgfs_bcast_filtering.override) {
1721e705c121SKalle Valo 		memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1722e705c121SKalle Valo 		       sizeof(cmd->filters));
1723e705c121SKalle Valo 		memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1724e705c121SKalle Valo 		       sizeof(cmd->macs));
1725e705c121SKalle Valo 		return true;
1726e705c121SKalle Valo 	}
1727e705c121SKalle Valo #endif
1728e705c121SKalle Valo 
1729e705c121SKalle Valo 	/* if no filters are configured, do nothing */
1730e705c121SKalle Valo 	if (!mvm->bcast_filters)
1731e705c121SKalle Valo 		return false;
1732e705c121SKalle Valo 
1733e705c121SKalle Valo 	/* configure and attach these filters for each associated sta vif */
1734e705c121SKalle Valo 	ieee80211_iterate_active_interfaces(
1735e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1736e705c121SKalle Valo 		iwl_mvm_bcast_filter_iterator, &iter_data);
1737e705c121SKalle Valo 
1738e705c121SKalle Valo 	return true;
1739e705c121SKalle Valo }
174034672bb3SEliad Peller 
174134672bb3SEliad Peller static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1742e705c121SKalle Valo {
1743e705c121SKalle Valo 	struct iwl_bcast_filter_cmd cmd;
1744e705c121SKalle Valo 
1745e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1746e705c121SKalle Valo 		return 0;
1747e705c121SKalle Valo 
1748e705c121SKalle Valo 	if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1749e705c121SKalle Valo 		return 0;
1750e705c121SKalle Valo 
1751e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1752e705c121SKalle Valo 				    sizeof(cmd), &cmd);
1753e705c121SKalle Valo }
1754e705c121SKalle Valo #else
175534672bb3SEliad Peller static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1756e705c121SKalle Valo {
1757e705c121SKalle Valo 	return 0;
1758e705c121SKalle Valo }
1759e705c121SKalle Valo #endif
1760e705c121SKalle Valo 
1761e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
1762e705c121SKalle Valo 					     struct ieee80211_vif *vif,
1763e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
1764e705c121SKalle Valo 					     u32 changes)
1765e705c121SKalle Valo {
1766e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1767e705c121SKalle Valo 	int ret;
1768e705c121SKalle Valo 
1769e705c121SKalle Valo 	/*
1770e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
1771e705c121SKalle Valo 	 * beacon interval, which was not known when the station interface was
1772e705c121SKalle Valo 	 * added.
1773e705c121SKalle Valo 	 */
1774e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
1775e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1776e705c121SKalle Valo 
1777e705c121SKalle Valo 	/*
1778e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
1779e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
1780e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
1781e705c121SKalle Valo 	 * branch for disassociation below.
1782e705c121SKalle Valo 	 */
1783e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
1784e705c121SKalle Valo 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1785e705c121SKalle Valo 
1786e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
1787e705c121SKalle Valo 	if (ret)
1788e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1789e705c121SKalle Valo 
1790e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
1791e705c121SKalle Valo 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1792e705c121SKalle Valo 	mvmvif->associated = bss_conf->assoc;
1793e705c121SKalle Valo 
1794e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
1795e705c121SKalle Valo 		if (bss_conf->assoc) {
1796e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
1797e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
1798e705c121SKalle Valo 			memset(&mvmvif->beacon_stats, 0,
1799e705c121SKalle Valo 			       sizeof(mvmvif->beacon_stats));
1800e705c121SKalle Valo 
1801e705c121SKalle Valo 			/* add quota for this interface */
1802e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
1803e705c121SKalle Valo 			if (ret) {
1804e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
1805e705c121SKalle Valo 				return;
1806e705c121SKalle Valo 			}
1807e705c121SKalle Valo 
1808e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1809e705c121SKalle Valo 				     &mvm->status)) {
1810e705c121SKalle Valo 				/*
1811e705c121SKalle Valo 				 * If we're restarting then the firmware will
1812e705c121SKalle Valo 				 * obviously have lost synchronisation with
1813e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
1814e705c121SKalle Valo 				 * itself, but we can make it more reliable by
1815e705c121SKalle Valo 				 * scheduling a session protection time event.
1816e705c121SKalle Valo 				 *
1817e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
1818e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
1819e705c121SKalle Valo 				 * the beacon interval.
1820e705c121SKalle Valo 				 *
1821e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
1822e705c121SKalle Valo 				 * than a single interface.
1823e705c121SKalle Valo 				 */
1824e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
1825e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
1826e705c121SKalle Valo 							5 * dur, false);
1827e705c121SKalle Valo 			}
1828e705c121SKalle Valo 
1829e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
1830e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
1831e705c121SKalle Valo 			if (vif->p2p) {
1832e705c121SKalle Valo 				iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
1833e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
1834e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
1835e705c121SKalle Valo 						    IEEE80211_SMPS_DYNAMIC);
1836e705c121SKalle Valo 			}
1837e705c121SKalle Valo 		} else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1838e705c121SKalle Valo 			/*
1839e705c121SKalle Valo 			 * If update fails - SF might be running in associated
1840e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
1841e705c121SKalle Valo 			 */
1842e705c121SKalle Valo 			WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
1843e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
1844e705c121SKalle Valo 
1845e705c121SKalle Valo 			/* remove AP station now that the MAC is unassoc */
1846e705c121SKalle Valo 			ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
1847e705c121SKalle Valo 			if (ret)
1848e705c121SKalle Valo 				IWL_ERR(mvm, "failed to remove AP station\n");
1849e705c121SKalle Valo 
1850e705c121SKalle Valo 			if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
1851e705c121SKalle Valo 				mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1852e705c121SKalle Valo 			mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1853e705c121SKalle Valo 			/* remove quota for this interface */
1854e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
1855e705c121SKalle Valo 			if (ret)
1856e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
1857e705c121SKalle Valo 
1858e705c121SKalle Valo 			if (vif->p2p)
1859e705c121SKalle Valo 				iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
1860e705c121SKalle Valo 
1861e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
1862e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1863e705c121SKalle Valo 			if (ret)
1864e705c121SKalle Valo 				IWL_ERR(mvm,
1865e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
1866e705c121SKalle Valo 					vif->addr);
1867e705c121SKalle Valo 		}
1868e705c121SKalle Valo 
1869e705c121SKalle Valo 		iwl_mvm_recalc_multicast(mvm);
187034672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
1871e705c121SKalle Valo 
1872e705c121SKalle Valo 		/* reset rssi values */
1873e705c121SKalle Valo 		mvmvif->bf_data.ave_beacon_signal = 0;
1874e705c121SKalle Valo 
1875e705c121SKalle Valo 		iwl_mvm_bt_coex_vif_change(mvm);
1876e705c121SKalle Valo 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
1877e705c121SKalle Valo 				    IEEE80211_SMPS_AUTOMATIC);
1878355346baSAvraham Stern 		if (fw_has_capa(&mvm->fw->ucode_capa,
1879355346baSAvraham Stern 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1880355346baSAvraham Stern 			iwl_mvm_config_scan(mvm);
1881e705c121SKalle Valo 	} else if (changes & BSS_CHANGED_BEACON_INFO) {
1882e705c121SKalle Valo 		/*
1883e705c121SKalle Valo 		 * We received a beacon _after_ association so
1884e705c121SKalle Valo 		 * remove the session protection.
1885e705c121SKalle Valo 		 */
1886e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
1887e705c121SKalle Valo 					  &mvmvif->time_event_data);
1888e705c121SKalle Valo 	}
1889e705c121SKalle Valo 
1890e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON_INFO) {
1891e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
1892e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
1893e705c121SKalle Valo 	}
1894e705c121SKalle Valo 
1895e705c121SKalle Valo 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS)) {
1896e705c121SKalle Valo 		ret = iwl_mvm_power_update_mac(mvm);
1897e705c121SKalle Valo 		if (ret)
1898e705c121SKalle Valo 			IWL_ERR(mvm, "failed to update power mode\n");
1899e705c121SKalle Valo 	}
1900e705c121SKalle Valo 
1901e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
1902e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
1903e705c121SKalle Valo 				bss_conf->txpower);
1904e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
1905e705c121SKalle Valo 	}
1906e705c121SKalle Valo 
1907e705c121SKalle Valo 	if (changes & BSS_CHANGED_CQM) {
1908e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
1909e705c121SKalle Valo 		/* reset cqm events tracking */
1910e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = 0;
1911e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
1912e705c121SKalle Valo 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1913e705c121SKalle Valo 			if (ret)
1914e705c121SKalle Valo 				IWL_ERR(mvm,
1915e705c121SKalle Valo 					"failed to update CQM thresholds\n");
1916e705c121SKalle Valo 		}
1917e705c121SKalle Valo 	}
1918e705c121SKalle Valo 
1919e705c121SKalle Valo 	if (changes & BSS_CHANGED_ARP_FILTER) {
1920e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
192134672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
1922e705c121SKalle Valo 	}
1923e705c121SKalle Valo }
1924e705c121SKalle Valo 
1925e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
1926e705c121SKalle Valo 				 struct ieee80211_vif *vif)
1927e705c121SKalle Valo {
1928e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1929e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1930e705c121SKalle Valo 	int ret;
1931e705c121SKalle Valo 
1932e705c121SKalle Valo 	/*
1933e705c121SKalle Valo 	 * iwl_mvm_mac_ctxt_add() might read directly from the device
1934e705c121SKalle Valo 	 * (the system time), so make sure it is available.
1935e705c121SKalle Valo 	 */
1936e705c121SKalle Valo 	ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
1937e705c121SKalle Valo 	if (ret)
1938e705c121SKalle Valo 		return ret;
1939e705c121SKalle Valo 
1940e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1941e705c121SKalle Valo 
1942e705c121SKalle Valo 	/* Send the beacon template */
1943e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
1944e705c121SKalle Valo 	if (ret)
1945e705c121SKalle Valo 		goto out_unlock;
1946e705c121SKalle Valo 
1947e705c121SKalle Valo 	/*
1948e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
1949e705c121SKalle Valo 	 * beacon interval, which was not known when the AP interface was added.
1950e705c121SKalle Valo 	 */
1951e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
1952e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1953e705c121SKalle Valo 
1954e705c121SKalle Valo 	mvmvif->ap_assoc_sta_count = 0;
1955e705c121SKalle Valo 
1956e705c121SKalle Valo 	/* Add the mac context */
1957e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1958e705c121SKalle Valo 	if (ret)
1959e705c121SKalle Valo 		goto out_unlock;
1960e705c121SKalle Valo 
1961e705c121SKalle Valo 	/* Perform the binding */
1962e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
1963e705c121SKalle Valo 	if (ret)
1964e705c121SKalle Valo 		goto out_remove;
1965e705c121SKalle Valo 
1966e705c121SKalle Valo 	/* Send the bcast station. At this stage the TBTT and DTIM time events
1967e705c121SKalle Valo 	 * are added and applied to the scheduler */
1968e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
1969e705c121SKalle Valo 	if (ret)
1970e705c121SKalle Valo 		goto out_unbind;
1971e705c121SKalle Valo 
1972e705c121SKalle Valo 	/* must be set before quota calculations */
1973e705c121SKalle Valo 	mvmvif->ap_ibss_active = true;
1974e705c121SKalle Valo 
1975e705c121SKalle Valo 	/* power updated needs to be done before quotas */
1976e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
1977e705c121SKalle Valo 
1978e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
1979e705c121SKalle Valo 	if (ret)
1980e705c121SKalle Valo 		goto out_quota_failed;
1981e705c121SKalle Valo 
1982e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1983e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
1984e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
1985e705c121SKalle Valo 
1986e705c121SKalle Valo 	iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
1987e705c121SKalle Valo 
1988e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
1989e705c121SKalle Valo 
1990e705c121SKalle Valo 	/* we don't support TDLS during DCM */
1991e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
1992e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
1993e705c121SKalle Valo 
1994e705c121SKalle Valo 	goto out_unlock;
1995e705c121SKalle Valo 
1996e705c121SKalle Valo out_quota_failed:
1997e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
1998e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
1999e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2000e705c121SKalle Valo out_unbind:
2001e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2002e705c121SKalle Valo out_remove:
2003e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2004e705c121SKalle Valo out_unlock:
2005e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2006e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
2007e705c121SKalle Valo 	return ret;
2008e705c121SKalle Valo }
2009e705c121SKalle Valo 
2010e705c121SKalle Valo static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2011e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2012e705c121SKalle Valo {
2013e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2014e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2015e705c121SKalle Valo 
2016e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
2017e705c121SKalle Valo 
2018e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2019e705c121SKalle Valo 
2020e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2021e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2022e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2023e705c121SKalle Valo 					  &mvmvif->time_event_data);
2024e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2025e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2026e705c121SKalle Valo 	}
2027e705c121SKalle Valo 
2028e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2029e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2030e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2031e705c121SKalle Valo 	}
2032e705c121SKalle Valo 
2033e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2034e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2035e705c121SKalle Valo 
2036e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2037e705c121SKalle Valo 
2038e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
2039e705c121SKalle Valo 
2040e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2041e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2042e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2043e705c121SKalle Valo 
2044e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2045e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2046e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2047e705c121SKalle Valo 
2048e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2049e705c121SKalle Valo 
2050e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2051e705c121SKalle Valo 
2052e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2053e705c121SKalle Valo }
2054e705c121SKalle Valo 
2055e705c121SKalle Valo static void
2056e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2057e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2058e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
2059e705c121SKalle Valo 				 u32 changes)
2060e705c121SKalle Valo {
2061e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2062e705c121SKalle Valo 
2063e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
2064e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
2065e705c121SKalle Valo 		return;
2066e705c121SKalle Valo 
2067e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2068e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2069e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2070e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2071e705c121SKalle Valo 
2072e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
2073e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
2074e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2075e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
2076e705c121SKalle Valo 
2077e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2078e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2079e705c121SKalle Valo 				bss_conf->txpower);
2080e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2081e705c121SKalle Valo 	}
2082e705c121SKalle Valo 
2083e705c121SKalle Valo }
2084e705c121SKalle Valo 
2085e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2086e705c121SKalle Valo 				     struct ieee80211_vif *vif,
2087e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
2088e705c121SKalle Valo 				     u32 changes)
2089e705c121SKalle Valo {
2090e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2091e705c121SKalle Valo 
2092e705c121SKalle Valo 	/*
2093e705c121SKalle Valo 	 * iwl_mvm_bss_info_changed_station() might call
2094e705c121SKalle Valo 	 * iwl_mvm_protect_session(), which reads directly from
2095e705c121SKalle Valo 	 * the device (the system time), so make sure it is available.
2096e705c121SKalle Valo 	 */
2097e705c121SKalle Valo 	if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
2098e705c121SKalle Valo 		return;
2099e705c121SKalle Valo 
2100e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2101e705c121SKalle Valo 
2102e705c121SKalle Valo 	if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2103e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2104e705c121SKalle Valo 
2105e705c121SKalle Valo 	switch (vif->type) {
2106e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2107e705c121SKalle Valo 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2108e705c121SKalle Valo 		break;
2109e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
2110e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
2111e705c121SKalle Valo 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2112e705c121SKalle Valo 		break;
2113e705c121SKalle Valo 	default:
2114e705c121SKalle Valo 		/* shouldn't happen */
2115e705c121SKalle Valo 		WARN_ON_ONCE(1);
2116e705c121SKalle Valo 	}
2117e705c121SKalle Valo 
2118e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2119e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
2120e705c121SKalle Valo }
2121e705c121SKalle Valo 
2122e705c121SKalle Valo static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2123e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2124e705c121SKalle Valo 			       struct ieee80211_scan_request *hw_req)
2125e705c121SKalle Valo {
2126e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2127e705c121SKalle Valo 	int ret;
2128e705c121SKalle Valo 
2129e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
2130e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2131e705c121SKalle Valo 		return -EINVAL;
2132e705c121SKalle Valo 
2133e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2134e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2135e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2136e705c121SKalle Valo 
2137e705c121SKalle Valo 	return ret;
2138e705c121SKalle Valo }
2139e705c121SKalle Valo 
2140e705c121SKalle Valo static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2141e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2142e705c121SKalle Valo {
2143e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2144e705c121SKalle Valo 
2145e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2146e705c121SKalle Valo 
2147e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2148e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
2149e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
2150e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
2151e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
2152e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
2153e705c121SKalle Valo 	*/
2154e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2155e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2156e705c121SKalle Valo 
2157e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2158e705c121SKalle Valo }
2159e705c121SKalle Valo 
2160e705c121SKalle Valo static void
2161e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2162e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
2163e705c121SKalle Valo 				  int num_frames,
2164e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
2165e705c121SKalle Valo 				  bool more_data)
2166e705c121SKalle Valo {
2167e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2168e705c121SKalle Valo 
2169e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
2170e705c121SKalle Valo 
2171e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2172e705c121SKalle Valo 					  tids, more_data, false);
2173e705c121SKalle Valo }
2174e705c121SKalle Valo 
2175e705c121SKalle Valo static void
2176e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2177e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
2178e705c121SKalle Valo 				    int num_frames,
2179e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
2180e705c121SKalle Valo 				    bool more_data)
2181e705c121SKalle Valo {
2182e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2183e705c121SKalle Valo 
2184e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from agg queue */
2185e705c121SKalle Valo 
2186e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2187e705c121SKalle Valo 					  tids, more_data, true);
2188e705c121SKalle Valo }
2189e705c121SKalle Valo 
2190e705c121SKalle Valo static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2191e705c121SKalle Valo 				   struct ieee80211_vif *vif,
2192e705c121SKalle Valo 				   enum sta_notify_cmd cmd,
2193e705c121SKalle Valo 				   struct ieee80211_sta *sta)
2194e705c121SKalle Valo {
2195e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2196e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2197e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
2198e705c121SKalle Valo 	int tid;
2199e705c121SKalle Valo 
2200e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2201e705c121SKalle Valo 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
2202e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2203e705c121SKalle Valo 
2204e705c121SKalle Valo 		if (tid_data->state != IWL_AGG_ON &&
2205e705c121SKalle Valo 		    tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA)
2206e705c121SKalle Valo 			continue;
2207e705c121SKalle Valo 
2208e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
2209e705c121SKalle Valo 
2210e705c121SKalle Valo 		if (iwl_mvm_tid_queued(tid_data) == 0)
2211e705c121SKalle Valo 			continue;
2212e705c121SKalle Valo 
2213e705c121SKalle Valo 		__set_bit(tid, &tids);
2214e705c121SKalle Valo 	}
2215e705c121SKalle Valo 
2216e705c121SKalle Valo 	switch (cmd) {
2217e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
2218e705c121SKalle Valo 		if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
2219e705c121SKalle Valo 			ieee80211_sta_block_awake(hw, sta, true);
2220e705c121SKalle Valo 
2221e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
2222e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
2223e705c121SKalle Valo 
2224e705c121SKalle Valo 		if (txqs)
2225e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
2226e705c121SKalle Valo 		/*
2227e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
2228e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
2229e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2230e705c121SKalle Valo 		 */
2231e705c121SKalle Valo 		break;
2232e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
2233e705c121SKalle Valo 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
2234e705c121SKalle Valo 			break;
2235e705c121SKalle Valo 
2236e705c121SKalle Valo 		if (txqs)
2237e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
2238e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
2239e705c121SKalle Valo 		break;
2240e705c121SKalle Valo 	default:
2241e705c121SKalle Valo 		break;
2242e705c121SKalle Valo 	}
2243e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2244e705c121SKalle Valo }
2245e705c121SKalle Valo 
2246e705c121SKalle Valo static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2247e705c121SKalle Valo 				       struct ieee80211_vif *vif,
2248e705c121SKalle Valo 				       struct ieee80211_sta *sta)
2249e705c121SKalle Valo {
2250e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2251e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2252e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2253e705c121SKalle Valo 
2254e705c121SKalle Valo 	/*
2255e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
2256e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
2257e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
2258e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
2259e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
2260e705c121SKalle Valo 	 * callback deleted the station.
2261e705c121SKalle Valo 	 */
2262e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2263e705c121SKalle Valo 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2264e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2265e705c121SKalle Valo 				   ERR_PTR(-ENOENT));
2266e705c121SKalle Valo 
2267e705c121SKalle Valo 	if (mvm_sta->vif->type == NL80211_IFTYPE_AP) {
2268e705c121SKalle Valo 		mvmvif->ap_assoc_sta_count--;
2269e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2270e705c121SKalle Valo 	}
2271e705c121SKalle Valo 
2272e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2273e705c121SKalle Valo }
2274e705c121SKalle Valo 
2275e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2276e705c121SKalle Valo 				const u8 *bssid)
2277e705c121SKalle Valo {
2278e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2279e705c121SKalle Valo 		return;
2280e705c121SKalle Valo 
2281e705c121SKalle Valo 	if (iwlwifi_mod_params.uapsd_disable) {
2282e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2283e705c121SKalle Valo 		return;
2284e705c121SKalle Valo 	}
2285e705c121SKalle Valo 
2286e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2287e705c121SKalle Valo }
2288e705c121SKalle Valo 
22891e8f1329SGolan Ben-Ami static void
22901e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
22911e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
22921e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
22931e8f1329SGolan Ben-Ami {
22941e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
22951e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
22961e8f1329SGolan Ben-Ami 
22971e8f1329SGolan Ben-Ami 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TDLS))
22981e8f1329SGolan Ben-Ami 		return;
22991e8f1329SGolan Ben-Ami 
23001e8f1329SGolan Ben-Ami 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TDLS);
23011e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
23021e8f1329SGolan Ben-Ami 	if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trig))
23031e8f1329SGolan Ben-Ami 		return;
23041e8f1329SGolan Ben-Ami 
23051e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
23061e8f1329SGolan Ben-Ami 		return;
23071e8f1329SGolan Ben-Ami 
23081e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
23091e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
23101e8f1329SGolan Ben-Ami 		return;
23111e8f1329SGolan Ben-Ami 
23121e8f1329SGolan Ben-Ami 	iwl_mvm_fw_dbg_collect_trig(mvm, trig,
23131e8f1329SGolan Ben-Ami 				    "TDLS event occurred, peer %pM, action %d",
23141e8f1329SGolan Ben-Ami 				    peer_addr, action);
23151e8f1329SGolan Ben-Ami }
23161e8f1329SGolan Ben-Ami 
2317e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
2318e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2319e705c121SKalle Valo 				 struct ieee80211_sta *sta,
2320e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
2321e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
2322e705c121SKalle Valo {
2323e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2324e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2325e705c121SKalle Valo 	int ret;
2326e705c121SKalle Valo 
2327e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
2328e705c121SKalle Valo 			   sta->addr, old_state, new_state);
2329e705c121SKalle Valo 
2330e705c121SKalle Valo 	/* this would be a mac80211 bug ... but don't crash */
2331e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
2332e705c121SKalle Valo 		return -EINVAL;
2333e705c121SKalle Valo 
2334e705c121SKalle Valo 	/* if a STA is being removed, reuse its ID */
2335e705c121SKalle Valo 	flush_work(&mvm->sta_drained_wk);
2336e705c121SKalle Valo 
2337e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2338e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
2339e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
2340e705c121SKalle Valo 		/*
2341e705c121SKalle Valo 		 * Firmware bug - it'll crash if the beacon interval is less
2342e705c121SKalle Valo 		 * than 16. We can't avoid connecting at all, so refuse the
2343e705c121SKalle Valo 		 * station state change, this will cause mac80211 to abandon
2344e705c121SKalle Valo 		 * attempts to connect to this AP, and eventually wpa_s will
2345e705c121SKalle Valo 		 * blacklist the AP...
2346e705c121SKalle Valo 		 */
2347e705c121SKalle Valo 		if (vif->type == NL80211_IFTYPE_STATION &&
2348e705c121SKalle Valo 		    vif->bss_conf.beacon_int < 16) {
2349e705c121SKalle Valo 			IWL_ERR(mvm,
2350e705c121SKalle Valo 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
2351e705c121SKalle Valo 				sta->addr, vif->bss_conf.beacon_int);
2352e705c121SKalle Valo 			ret = -EINVAL;
2353e705c121SKalle Valo 			goto out_unlock;
2354e705c121SKalle Valo 		}
2355e705c121SKalle Valo 
2356e705c121SKalle Valo 		if (sta->tdls &&
2357e705c121SKalle Valo 		    (vif->p2p ||
2358e705c121SKalle Valo 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
2359e705c121SKalle Valo 						IWL_MVM_TDLS_STA_COUNT ||
2360e705c121SKalle Valo 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
2361e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
2362e705c121SKalle Valo 			ret = -EBUSY;
2363e705c121SKalle Valo 			goto out_unlock;
2364e705c121SKalle Valo 		}
2365e705c121SKalle Valo 
2366e705c121SKalle Valo 		ret = iwl_mvm_add_sta(mvm, vif, sta);
23671e8f1329SGolan Ben-Ami 		if (sta->tdls && ret == 0) {
2368e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
23691e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
23701e8f1329SGolan Ben-Ami 						   NL80211_TDLS_SETUP);
23711e8f1329SGolan Ben-Ami 		}
2372e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
2373e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
2374e705c121SKalle Valo 		/*
2375e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
2376e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
2377e705c121SKalle Valo 		 */
2378e705c121SKalle Valo 		mvm->last_ebs_successful = true;
2379e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
2380e705c121SKalle Valo 		ret = 0;
2381e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
2382e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
2383e705c121SKalle Valo 		ret = iwl_mvm_update_sta(mvm, vif, sta);
2384e705c121SKalle Valo 		if (ret == 0)
2385e705c121SKalle Valo 			iwl_mvm_rs_rate_init(mvm, sta,
2386e705c121SKalle Valo 					     mvmvif->phy_ctxt->channel->band,
2387e705c121SKalle Valo 					     true);
2388e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
2389e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
2390e705c121SKalle Valo 
2391e705c121SKalle Valo 		/* we don't support TDLS during DCM */
2392e705c121SKalle Valo 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
2393e705c121SKalle Valo 			iwl_mvm_teardown_tdls_peers(mvm);
2394e705c121SKalle Valo 
23951e8f1329SGolan Ben-Ami 		if (sta->tdls)
23961e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
23971e8f1329SGolan Ben-Ami 						   NL80211_TDLS_ENABLE_LINK);
23981e8f1329SGolan Ben-Ami 
2399e705c121SKalle Valo 		/* enable beacon filtering */
2400e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2401e705c121SKalle Valo 		ret = 0;
2402e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
2403e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
2404e705c121SKalle Valo 		/* disable beacon filtering */
2405e705c121SKalle Valo 		WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
2406e705c121SKalle Valo 		ret = 0;
2407e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
2408e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
2409e705c121SKalle Valo 		ret = 0;
2410e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
2411e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
2412e705c121SKalle Valo 		ret = 0;
2413e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
2414e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
2415e705c121SKalle Valo 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
24161e8f1329SGolan Ben-Ami 		if (sta->tdls) {
2417e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
24181e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
24191e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
24201e8f1329SGolan Ben-Ami 		}
2421e705c121SKalle Valo 	} else {
2422e705c121SKalle Valo 		ret = -EIO;
2423e705c121SKalle Valo 	}
2424e705c121SKalle Valo  out_unlock:
2425e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2426e705c121SKalle Valo 
2427e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
2428e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
2429e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
2430e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
2431e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
2432e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
2433e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
2434e705c121SKalle Valo 	}
2435e705c121SKalle Valo 
2436e705c121SKalle Valo 	return ret;
2437e705c121SKalle Valo }
2438e705c121SKalle Valo 
2439e705c121SKalle Valo static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2440e705c121SKalle Valo {
2441e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2442e705c121SKalle Valo 
2443e705c121SKalle Valo 	mvm->rts_threshold = value;
2444e705c121SKalle Valo 
2445e705c121SKalle Valo 	return 0;
2446e705c121SKalle Valo }
2447e705c121SKalle Valo 
2448e705c121SKalle Valo static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
2449e705c121SKalle Valo 				  struct ieee80211_vif *vif,
2450e705c121SKalle Valo 				  struct ieee80211_sta *sta, u32 changed)
2451e705c121SKalle Valo {
2452e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2453e705c121SKalle Valo 
2454e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
2455e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
2456e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2457e705c121SKalle Valo }
2458e705c121SKalle Valo 
2459e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
2460e705c121SKalle Valo 			       struct ieee80211_vif *vif, u16 ac,
2461e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
2462e705c121SKalle Valo {
2463e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2464e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2465e705c121SKalle Valo 
2466e705c121SKalle Valo 	mvmvif->queue_params[ac] = *params;
2467e705c121SKalle Valo 
2468e705c121SKalle Valo 	/*
2469e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
2470e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
2471e705c121SKalle Valo 	 */
2472e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2473e705c121SKalle Valo 		int ret;
2474e705c121SKalle Valo 
2475e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
2476e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2477e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
2478e705c121SKalle Valo 		return ret;
2479e705c121SKalle Valo 	}
2480e705c121SKalle Valo 	return 0;
2481e705c121SKalle Valo }
2482e705c121SKalle Valo 
2483e705c121SKalle Valo static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
2484e705c121SKalle Valo 				      struct ieee80211_vif *vif)
2485e705c121SKalle Valo {
2486e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2487e705c121SKalle Valo 	u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
2488e705c121SKalle Valo 			   200 + vif->bss_conf.beacon_int);
2489e705c121SKalle Valo 	u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
2490e705c121SKalle Valo 			       100 + vif->bss_conf.beacon_int);
2491e705c121SKalle Valo 
2492e705c121SKalle Valo 	if (WARN_ON_ONCE(vif->bss_conf.assoc))
2493e705c121SKalle Valo 		return;
2494e705c121SKalle Valo 
2495e705c121SKalle Valo 	/*
2496e705c121SKalle Valo 	 * iwl_mvm_protect_session() reads directly from the device
2497e705c121SKalle Valo 	 * (the system time), so make sure it is available.
2498e705c121SKalle Valo 	 */
2499e705c121SKalle Valo 	if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
2500e705c121SKalle Valo 		return;
2501e705c121SKalle Valo 
2502e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2503e705c121SKalle Valo 	/* Try really hard to protect the session and hear a beacon */
2504e705c121SKalle Valo 	iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500, false);
2505e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2506e705c121SKalle Valo 
2507e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
2508e705c121SKalle Valo }
2509e705c121SKalle Valo 
2510e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
2511e705c121SKalle Valo 					struct ieee80211_vif *vif,
2512e705c121SKalle Valo 					struct cfg80211_sched_scan_request *req,
2513e705c121SKalle Valo 					struct ieee80211_scan_ies *ies)
2514e705c121SKalle Valo {
2515e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2516e705c121SKalle Valo 
2517e705c121SKalle Valo 	int ret;
2518e705c121SKalle Valo 
2519e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2520e705c121SKalle Valo 
2521e705c121SKalle Valo 	if (!vif->bss_conf.idle) {
2522e705c121SKalle Valo 		ret = -EBUSY;
2523e705c121SKalle Valo 		goto out;
2524e705c121SKalle Valo 	}
2525e705c121SKalle Valo 
2526e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
2527e705c121SKalle Valo 
2528e705c121SKalle Valo out:
2529e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2530e705c121SKalle Valo 	return ret;
2531e705c121SKalle Valo }
2532e705c121SKalle Valo 
2533e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
2534e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2535e705c121SKalle Valo {
2536e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2537e705c121SKalle Valo 	int ret;
2538e705c121SKalle Valo 
2539e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2540e705c121SKalle Valo 
2541e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2542e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
2543e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
2544e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
2545e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
2546e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
2547e705c121SKalle Valo 	 * not running.
2548e705c121SKalle Valo 	*/
2549e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
2550e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
2551e705c121SKalle Valo 		return 0;
2552e705c121SKalle Valo 	}
2553e705c121SKalle Valo 
2554e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
2555e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2556e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
2557e705c121SKalle Valo 
2558e705c121SKalle Valo 	return ret;
2559e705c121SKalle Valo }
2560e705c121SKalle Valo 
2561e705c121SKalle Valo static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
2562e705c121SKalle Valo 			       enum set_key_cmd cmd,
2563e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2564e705c121SKalle Valo 			       struct ieee80211_sta *sta,
2565e705c121SKalle Valo 			       struct ieee80211_key_conf *key)
2566e705c121SKalle Valo {
2567e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2568e705c121SKalle Valo 	int ret;
25694615fd15SEmmanuel Grumbach 	u8 key_offset;
2570e705c121SKalle Valo 
2571e705c121SKalle Valo 	if (iwlwifi_mod_params.sw_crypto) {
2572e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
2573e705c121SKalle Valo 		return -EOPNOTSUPP;
2574e705c121SKalle Valo 	}
2575e705c121SKalle Valo 
2576e705c121SKalle Valo 	switch (key->cipher) {
2577e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
2578e705c121SKalle Valo 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2579e705c121SKalle Valo 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2580e705c121SKalle Valo 		break;
2581e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
2582e705c121SKalle Valo 		key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2583e705c121SKalle Valo 		break;
2584e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
2585e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
2586e705c121SKalle Valo 		break;
2587e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
2588e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
2589e705c121SKalle Valo 		/* For non-client mode, only use WEP keys for TX as we probably
2590e705c121SKalle Valo 		 * don't have a station yet anyway and would then have to keep
2591e705c121SKalle Valo 		 * track of the keys, linking them to each of the clients/peers
2592e705c121SKalle Valo 		 * as they appear. For now, don't do that, for performance WEP
2593e705c121SKalle Valo 		 * offload doesn't really matter much, but we need it for some
2594e705c121SKalle Valo 		 * other offload features in client mode.
2595e705c121SKalle Valo 		 */
2596e705c121SKalle Valo 		if (vif->type != NL80211_IFTYPE_STATION)
2597e705c121SKalle Valo 			return 0;
2598e705c121SKalle Valo 		break;
2599e705c121SKalle Valo 	default:
2600e705c121SKalle Valo 		/* currently FW supports only one optional cipher scheme */
2601e705c121SKalle Valo 		if (hw->n_cipher_schemes &&
2602e705c121SKalle Valo 		    hw->cipher_schemes->cipher == key->cipher)
2603e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2604e705c121SKalle Valo 		else
2605e705c121SKalle Valo 			return -EOPNOTSUPP;
2606e705c121SKalle Valo 	}
2607e705c121SKalle Valo 
2608e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2609e705c121SKalle Valo 
2610e705c121SKalle Valo 	switch (cmd) {
2611e705c121SKalle Valo 	case SET_KEY:
2612e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
2613e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
2614e705c121SKalle Valo 			/*
2615e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
2616e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
2617e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
2618e705c121SKalle Valo 			 */
2619e705c121SKalle Valo 			ret = 0;
2620e705c121SKalle Valo 			key->hw_key_idx = STA_KEY_IDX_INVALID;
2621e705c121SKalle Valo 			break;
2622e705c121SKalle Valo 		}
2623e705c121SKalle Valo 
2624e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
2625e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
2626e705c121SKalle Valo 		 */
2627e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2628e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
2629e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
2630e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
2631e705c121SKalle Valo 			ret = 0;
2632e705c121SKalle Valo 			break;
2633e705c121SKalle Valo 		}
2634e705c121SKalle Valo 
26354615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
26364615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
26374615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
26384615fd15SEmmanuel Grumbach 		else
26394615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
26404615fd15SEmmanuel Grumbach 
2641e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
26424615fd15SEmmanuel Grumbach 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
2643e705c121SKalle Valo 		if (ret) {
2644e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
2645e705c121SKalle Valo 			/*
2646e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
2647e705c121SKalle Valo 			 * in the device for TX so still return 0
2648e705c121SKalle Valo 			 */
2649e705c121SKalle Valo 			key->hw_key_idx = STA_KEY_IDX_INVALID;
2650e705c121SKalle Valo 			ret = 0;
2651e705c121SKalle Valo 		}
2652e705c121SKalle Valo 
2653e705c121SKalle Valo 		break;
2654e705c121SKalle Valo 	case DISABLE_KEY:
2655e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
2656e705c121SKalle Valo 			ret = 0;
2657e705c121SKalle Valo 			break;
2658e705c121SKalle Valo 		}
2659e705c121SKalle Valo 
2660e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
2661e705c121SKalle Valo 		ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
2662e705c121SKalle Valo 		break;
2663e705c121SKalle Valo 	default:
2664e705c121SKalle Valo 		ret = -EINVAL;
2665e705c121SKalle Valo 	}
2666e705c121SKalle Valo 
2667e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2668e705c121SKalle Valo 	return ret;
2669e705c121SKalle Valo }
2670e705c121SKalle Valo 
2671e705c121SKalle Valo static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
2672e705c121SKalle Valo 					struct ieee80211_vif *vif,
2673e705c121SKalle Valo 					struct ieee80211_key_conf *keyconf,
2674e705c121SKalle Valo 					struct ieee80211_sta *sta,
2675e705c121SKalle Valo 					u32 iv32, u16 *phase1key)
2676e705c121SKalle Valo {
2677e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2678e705c121SKalle Valo 
2679e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
2680e705c121SKalle Valo 		return;
2681e705c121SKalle Valo 
2682e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
2683e705c121SKalle Valo }
2684e705c121SKalle Valo 
2685e705c121SKalle Valo 
2686e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
2687e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
2688e705c121SKalle Valo {
2689e705c121SKalle Valo 	struct iwl_mvm *mvm =
2690e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
2691e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
2692e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
2693e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
2694e705c121SKalle Valo 
2695e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
2696e705c121SKalle Valo 		return true;
2697e705c121SKalle Valo 
2698e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
2699e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
2700e705c121SKalle Valo 		return true;
2701e705c121SKalle Valo 	}
2702e705c121SKalle Valo 
2703e705c121SKalle Valo 	resp = (void *)pkt->data;
2704e705c121SKalle Valo 
2705e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
2706e705c121SKalle Valo 		     "Aux ROC: Recieved response from ucode: status=%d uid=%d\n",
2707e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
2708e705c121SKalle Valo 
2709e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
2710e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
2711e705c121SKalle Valo 		     te_data->uid);
2712e705c121SKalle Valo 
2713e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
2714e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
2715e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
2716e705c121SKalle Valo 
2717e705c121SKalle Valo 	return true;
2718e705c121SKalle Valo }
2719e705c121SKalle Valo 
2720dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
2721dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
2722dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
2723dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
2724dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
2725e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
2726e705c121SKalle Valo 				    struct ieee80211_channel *channel,
2727e705c121SKalle Valo 				    struct ieee80211_vif *vif,
2728e705c121SKalle Valo 				    int duration)
2729e705c121SKalle Valo {
2730e705c121SKalle Valo 	int res, time_reg = DEVICE_SYSTEM_TIME_REG;
2731e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2732e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
2733e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
2734e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
2735dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
2736dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
2737dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
2738e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
2739e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
2740e705c121SKalle Valo 		.id_and_color =
2741e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
2742e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
2743e705c121SKalle Valo 		/* Set the channel info data */
2744e705c121SKalle Valo 		.channel_info.band = (channel->band == IEEE80211_BAND_2GHZ) ?
2745e705c121SKalle Valo 			PHY_BAND_24 : PHY_BAND_5,
2746e705c121SKalle Valo 		.channel_info.channel = channel->hw_value,
2747e705c121SKalle Valo 		.channel_info.width = PHY_VHT_CHANNEL_MODE20,
2748e705c121SKalle Valo 		/* Set the time and duration */
2749e705c121SKalle Valo 		.apply_time = cpu_to_le32(iwl_read_prph(mvm->trans, time_reg)),
2750e705c121SKalle Valo 	 };
2751e705c121SKalle Valo 
2752dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
2753dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
2754dc28e12fSMatti Gottlieb 
2755dc28e12fSMatti Gottlieb 	/*
2756dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
2757dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
2758dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
2759dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
2760dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
2761dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
2762dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
2763dc28e12fSMatti Gottlieb 	 */
2764dc28e12fSMatti Gottlieb 	if (vif->bss_conf.assoc) {
2765dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
2766dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
2767dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
2768dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
2769dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
2770dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
2771dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
2772dc28e12fSMatti Gottlieb 		}
2773dc28e12fSMatti Gottlieb 	}
2774dc28e12fSMatti Gottlieb 
2775dc28e12fSMatti Gottlieb 	aux_roc_req.duration = cpu_to_le32(req_dur);
2776dc28e12fSMatti Gottlieb 	aux_roc_req.apply_time_max_delay = cpu_to_le32(delay);
2777dc28e12fSMatti Gottlieb 
2778dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
2779dc28e12fSMatti Gottlieb 		     "ROC: Requesting to remain on channel %u for %ums (requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
2780dc28e12fSMatti Gottlieb 		     channel->hw_value, req_dur, duration, delay,
2781dc28e12fSMatti Gottlieb 		     dtim_interval);
2782e705c121SKalle Valo 	/* Set the node address */
2783e705c121SKalle Valo 	memcpy(aux_roc_req.node_addr, vif->addr, ETH_ALEN);
2784e705c121SKalle Valo 
2785e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2786e705c121SKalle Valo 
2787e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
2788e705c121SKalle Valo 
2789e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
2790e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
2791e705c121SKalle Valo 		return -EIO;
2792e705c121SKalle Valo 	}
2793e705c121SKalle Valo 
2794e705c121SKalle Valo 	te_data->vif = vif;
2795e705c121SKalle Valo 	te_data->duration = duration;
2796e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
2797e705c121SKalle Valo 
2798e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
2799e705c121SKalle Valo 
2800e705c121SKalle Valo 	/*
2801e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
2802e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
2803e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
2804e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
2805e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
2806e705c121SKalle Valo 	 * time another notification (that the time event started)
2807e705c121SKalle Valo 	 * might already be processed unsuccessfully.
2808e705c121SKalle Valo 	 */
2809e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
2810e705c121SKalle Valo 				   time_event_response,
2811e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
2812e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
2813e705c121SKalle Valo 
2814e705c121SKalle Valo 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, sizeof(aux_roc_req),
2815e705c121SKalle Valo 				   &aux_roc_req);
2816e705c121SKalle Valo 
2817e705c121SKalle Valo 	if (res) {
2818e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
2819e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
2820e705c121SKalle Valo 		goto out_clear_te;
2821e705c121SKalle Valo 	}
2822e705c121SKalle Valo 
2823e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
2824e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
2825e705c121SKalle Valo 	/* should never fail */
2826e705c121SKalle Valo 	WARN_ON_ONCE(res);
2827e705c121SKalle Valo 
2828e705c121SKalle Valo 	if (res) {
2829e705c121SKalle Valo  out_clear_te:
2830e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
2831e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
2832e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
2833e705c121SKalle Valo 	}
2834e705c121SKalle Valo 
2835e705c121SKalle Valo 	return res;
2836e705c121SKalle Valo }
2837e705c121SKalle Valo 
2838e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
2839e705c121SKalle Valo 		       struct ieee80211_vif *vif,
2840e705c121SKalle Valo 		       struct ieee80211_channel *channel,
2841e705c121SKalle Valo 		       int duration,
2842e705c121SKalle Valo 		       enum ieee80211_roc_type type)
2843e705c121SKalle Valo {
2844e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2845e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2846e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
2847e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
2848e705c121SKalle Valo 	int ret, i;
2849e705c121SKalle Valo 
2850e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
2851e705c121SKalle Valo 			   duration, type);
2852e705c121SKalle Valo 
2853e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
2854e705c121SKalle Valo 
2855e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2856e705c121SKalle Valo 
2857e705c121SKalle Valo 	switch (vif->type) {
2858e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2859e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
2860e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
2861e705c121SKalle Valo 			/* Use aux roc framework (HS20) */
2862e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
2863e705c121SKalle Valo 						       vif, duration);
2864e705c121SKalle Valo 			goto out_unlock;
2865e705c121SKalle Valo 		}
2866e705c121SKalle Valo 		IWL_ERR(mvm, "hotspot not supported\n");
2867e705c121SKalle Valo 		ret = -EINVAL;
2868e705c121SKalle Valo 		goto out_unlock;
2869e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
2870e705c121SKalle Valo 		/* handle below */
2871e705c121SKalle Valo 		break;
2872e705c121SKalle Valo 	default:
2873e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
2874e705c121SKalle Valo 		ret = -EINVAL;
2875e705c121SKalle Valo 		goto out_unlock;
2876e705c121SKalle Valo 	}
2877e705c121SKalle Valo 
2878e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
2879e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
2880e705c121SKalle Valo 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
2881e705c121SKalle Valo 			continue;
2882e705c121SKalle Valo 
2883e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
2884e705c121SKalle Valo 			/*
2885e705c121SKalle Valo 			 * Unbind the P2P_DEVICE from the current PHY context,
2886e705c121SKalle Valo 			 * and if the PHY context is not used remove it.
2887e705c121SKalle Valo 			 */
2888e705c121SKalle Valo 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
2889e705c121SKalle Valo 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2890e705c121SKalle Valo 				goto out_unlock;
2891e705c121SKalle Valo 
2892e705c121SKalle Valo 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2893e705c121SKalle Valo 
2894e705c121SKalle Valo 			/* Bind the P2P_DEVICE to the current PHY Context */
2895e705c121SKalle Valo 			mvmvif->phy_ctxt = phy_ctxt;
2896e705c121SKalle Valo 
2897e705c121SKalle Valo 			ret = iwl_mvm_binding_add_vif(mvm, vif);
2898e705c121SKalle Valo 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2899e705c121SKalle Valo 				goto out_unlock;
2900e705c121SKalle Valo 
2901e705c121SKalle Valo 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2902e705c121SKalle Valo 			goto schedule_time_event;
2903e705c121SKalle Valo 		}
2904e705c121SKalle Valo 	}
2905e705c121SKalle Valo 
2906e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
2907e705c121SKalle Valo 	if (channel == mvmvif->phy_ctxt->channel)
2908e705c121SKalle Valo 		goto schedule_time_event;
2909e705c121SKalle Valo 
2910e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
2911e705c121SKalle Valo 
2912e705c121SKalle Valo 	/*
2913e705c121SKalle Valo 	 * Change the PHY context configuration as it is currently referenced
2914e705c121SKalle Valo 	 * only by the P2P Device MAC
2915e705c121SKalle Valo 	 */
2916e705c121SKalle Valo 	if (mvmvif->phy_ctxt->ref == 1) {
2917e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
2918e705c121SKalle Valo 					       &chandef, 1, 1);
2919e705c121SKalle Valo 		if (ret)
2920e705c121SKalle Valo 			goto out_unlock;
2921e705c121SKalle Valo 	} else {
2922e705c121SKalle Valo 		/*
2923e705c121SKalle Valo 		 * The PHY context is shared with other MACs. Need to remove the
2924e705c121SKalle Valo 		 * P2P Device from the binding, allocate an new PHY context and
2925e705c121SKalle Valo 		 * create a new binding
2926e705c121SKalle Valo 		 */
2927e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2928e705c121SKalle Valo 		if (!phy_ctxt) {
2929e705c121SKalle Valo 			ret = -ENOSPC;
2930e705c121SKalle Valo 			goto out_unlock;
2931e705c121SKalle Valo 		}
2932e705c121SKalle Valo 
2933e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
2934e705c121SKalle Valo 					       1, 1);
2935e705c121SKalle Valo 		if (ret) {
2936e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
2937e705c121SKalle Valo 			goto out_unlock;
2938e705c121SKalle Valo 		}
2939e705c121SKalle Valo 
2940e705c121SKalle Valo 		/* Unbind the P2P_DEVICE from the current PHY context */
2941e705c121SKalle Valo 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
2942e705c121SKalle Valo 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2943e705c121SKalle Valo 			goto out_unlock;
2944e705c121SKalle Valo 
2945e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2946e705c121SKalle Valo 
2947e705c121SKalle Valo 		/* Bind the P2P_DEVICE to the new allocated PHY context */
2948e705c121SKalle Valo 		mvmvif->phy_ctxt = phy_ctxt;
2949e705c121SKalle Valo 
2950e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
2951e705c121SKalle Valo 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2952e705c121SKalle Valo 			goto out_unlock;
2953e705c121SKalle Valo 
2954e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2955e705c121SKalle Valo 	}
2956e705c121SKalle Valo 
2957e705c121SKalle Valo schedule_time_event:
2958e705c121SKalle Valo 	/* Schedule the time events */
2959e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
2960e705c121SKalle Valo 
2961e705c121SKalle Valo out_unlock:
2962e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2963e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
2964e705c121SKalle Valo 	return ret;
2965e705c121SKalle Valo }
2966e705c121SKalle Valo 
2967e705c121SKalle Valo static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
2968e705c121SKalle Valo {
2969e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2970e705c121SKalle Valo 
2971e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
2972e705c121SKalle Valo 
2973e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2974e705c121SKalle Valo 	iwl_mvm_stop_roc(mvm);
2975e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2976e705c121SKalle Valo 
2977e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
2978e705c121SKalle Valo 	return 0;
2979e705c121SKalle Valo }
2980e705c121SKalle Valo 
2981e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
2982e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
2983e705c121SKalle Valo {
2984e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2985e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
2986e705c121SKalle Valo 	int ret;
2987e705c121SKalle Valo 
2988e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2989e705c121SKalle Valo 
2990e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
2991e705c121SKalle Valo 
2992e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2993e705c121SKalle Valo 	if (!phy_ctxt) {
2994e705c121SKalle Valo 		ret = -ENOSPC;
2995e705c121SKalle Valo 		goto out;
2996e705c121SKalle Valo 	}
2997e705c121SKalle Valo 
2998e705c121SKalle Valo 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2999e705c121SKalle Valo 				       ctx->rx_chains_static,
3000e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
3001e705c121SKalle Valo 	if (ret) {
3002e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
3003e705c121SKalle Valo 		goto out;
3004e705c121SKalle Valo 	}
3005e705c121SKalle Valo 
3006e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
3007e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
3008e705c121SKalle Valo out:
3009e705c121SKalle Valo 	return ret;
3010e705c121SKalle Valo }
3011e705c121SKalle Valo 
3012e705c121SKalle Valo static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
3013e705c121SKalle Valo 			       struct ieee80211_chanctx_conf *ctx)
3014e705c121SKalle Valo {
3015e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3016e705c121SKalle Valo 	int ret;
3017e705c121SKalle Valo 
3018e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3019e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
3020e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3021e705c121SKalle Valo 
3022e705c121SKalle Valo 	return ret;
3023e705c121SKalle Valo }
3024e705c121SKalle Valo 
3025e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
3026e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
3027e705c121SKalle Valo {
3028e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3029e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3030e705c121SKalle Valo 
3031e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3032e705c121SKalle Valo 
3033e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
3034e705c121SKalle Valo }
3035e705c121SKalle Valo 
3036e705c121SKalle Valo static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
3037e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx)
3038e705c121SKalle Valo {
3039e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3040e705c121SKalle Valo 
3041e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3042e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
3043e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3044e705c121SKalle Valo }
3045e705c121SKalle Valo 
3046e705c121SKalle Valo static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
3047e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx,
3048e705c121SKalle Valo 				   u32 changed)
3049e705c121SKalle Valo {
3050e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3051e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3052e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3053e705c121SKalle Valo 
3054e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
3055e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
3056e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
3057e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
3058e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
3059e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
3060e705c121SKalle Valo 		      phy_ctxt->ref, changed))
3061e705c121SKalle Valo 		return;
3062e705c121SKalle Valo 
3063e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3064e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
3065e705c121SKalle Valo 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
3066e705c121SKalle Valo 				 ctx->rx_chains_static,
3067e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
3068e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3069e705c121SKalle Valo }
3070e705c121SKalle Valo 
3071e705c121SKalle Valo static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
3072e705c121SKalle Valo 					struct ieee80211_vif *vif,
3073e705c121SKalle Valo 					struct ieee80211_chanctx_conf *ctx,
3074e705c121SKalle Valo 					bool switching_chanctx)
3075e705c121SKalle Valo {
3076e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3077e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3078e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3079e705c121SKalle Valo 	int ret;
3080e705c121SKalle Valo 
3081e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3082e705c121SKalle Valo 
3083e705c121SKalle Valo 	mvmvif->phy_ctxt = phy_ctxt;
3084e705c121SKalle Valo 
3085e705c121SKalle Valo 	switch (vif->type) {
3086e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3087e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
3088e705c121SKalle Valo 		if (switching_chanctx) {
3089e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
3090e705c121SKalle Valo 			break;
3091e705c121SKalle Valo 		}
3092e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
3093e705c121SKalle Valo 		/*
3094e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
3095e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
3096e705c121SKalle Valo 		 */
3097e705c121SKalle Valo 		ret = 0;
3098e705c121SKalle Valo 		goto out;
3099e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3100e705c121SKalle Valo 		break;
3101e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
3102e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
3103e705c121SKalle Valo 		mvmvif->ps_disabled = true;
3104e705c121SKalle Valo 		break;
3105e705c121SKalle Valo 	default:
3106e705c121SKalle Valo 		ret = -EINVAL;
3107e705c121SKalle Valo 		goto out;
3108e705c121SKalle Valo 	}
3109e705c121SKalle Valo 
3110e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
3111e705c121SKalle Valo 	if (ret)
3112e705c121SKalle Valo 		goto out;
3113e705c121SKalle Valo 
3114e705c121SKalle Valo 	/*
3115e705c121SKalle Valo 	 * Power state must be updated before quotas,
3116e705c121SKalle Valo 	 * otherwise fw will complain.
3117e705c121SKalle Valo 	 */
3118e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3119e705c121SKalle Valo 
3120e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
3121e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
3122e705c121SKalle Valo 	 * will handle quota settings.
3123e705c121SKalle Valo 	 */
3124e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
3125e705c121SKalle Valo 		mvmvif->monitor_active = true;
3126e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
3127e705c121SKalle Valo 		if (ret)
3128e705c121SKalle Valo 			goto out_remove_binding;
3129e705c121SKalle Valo 	}
3130e705c121SKalle Valo 
3131e705c121SKalle Valo 	/* Handle binding during CSA */
3132e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
3133e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
3134e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3135e705c121SKalle Valo 	}
3136e705c121SKalle Valo 
3137e705c121SKalle Valo 	if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
3138e705c121SKalle Valo 		u32 duration = 2 * vif->bss_conf.beacon_int;
3139e705c121SKalle Valo 
3140e705c121SKalle Valo 		/* iwl_mvm_protect_session() reads directly from the
3141e705c121SKalle Valo 		 * device (the system time), so make sure it is
3142e705c121SKalle Valo 		 * available.
3143e705c121SKalle Valo 		 */
3144e705c121SKalle Valo 		ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_CSA);
3145e705c121SKalle Valo 		if (ret)
3146e705c121SKalle Valo 			goto out_remove_binding;
3147e705c121SKalle Valo 
3148e705c121SKalle Valo 		/* Protect the session to make sure we hear the first
3149e705c121SKalle Valo 		 * beacon on the new channel.
3150e705c121SKalle Valo 		 */
3151e705c121SKalle Valo 		iwl_mvm_protect_session(mvm, vif, duration, duration,
3152e705c121SKalle Valo 					vif->bss_conf.beacon_int / 2,
3153e705c121SKalle Valo 					true);
3154e705c121SKalle Valo 
3155e705c121SKalle Valo 		iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_CSA);
3156e705c121SKalle Valo 
3157e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
3158e705c121SKalle Valo 	}
3159e705c121SKalle Valo 
3160e705c121SKalle Valo 	goto out;
3161e705c121SKalle Valo 
3162e705c121SKalle Valo out_remove_binding:
3163e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
3164e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3165e705c121SKalle Valo out:
3166e705c121SKalle Valo 	if (ret)
3167e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
3168e705c121SKalle Valo 	return ret;
3169e705c121SKalle Valo }
3170e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
3171e705c121SKalle Valo 				      struct ieee80211_vif *vif,
3172e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
3173e705c121SKalle Valo {
3174e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3175e705c121SKalle Valo 	int ret;
3176e705c121SKalle Valo 
3177e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3178e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
3179e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3180e705c121SKalle Valo 
3181e705c121SKalle Valo 	return ret;
3182e705c121SKalle Valo }
3183e705c121SKalle Valo 
3184e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
3185e705c121SKalle Valo 					   struct ieee80211_vif *vif,
3186e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
3187e705c121SKalle Valo 					   bool switching_chanctx)
3188e705c121SKalle Valo {
3189e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3190e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
3191e705c121SKalle Valo 
3192e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3193e705c121SKalle Valo 
3194e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
3195e705c121SKalle Valo 
3196e705c121SKalle Valo 	switch (vif->type) {
3197e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
3198e705c121SKalle Valo 		goto out;
3199e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
3200e705c121SKalle Valo 		mvmvif->monitor_active = false;
3201e705c121SKalle Valo 		mvmvif->ps_disabled = false;
3202e705c121SKalle Valo 		break;
3203e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3204e705c121SKalle Valo 		/* This part is triggered only during CSA */
3205e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
3206e705c121SKalle Valo 			goto out;
3207e705c121SKalle Valo 
3208e705c121SKalle Valo 		mvmvif->csa_countdown = false;
3209e705c121SKalle Valo 
3210e705c121SKalle Valo 		/* Set CS bit on all the stations */
3211e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
3212e705c121SKalle Valo 
3213e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
3214e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
3215e705c121SKalle Valo 
3216e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
3217e705c121SKalle Valo 		break;
3218e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3219e705c121SKalle Valo 		if (!switching_chanctx)
3220e705c121SKalle Valo 			break;
3221e705c121SKalle Valo 
3222e705c121SKalle Valo 		disabled_vif = vif;
3223e705c121SKalle Valo 
3224e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
3225e705c121SKalle Valo 		break;
3226e705c121SKalle Valo 	default:
3227e705c121SKalle Valo 		break;
3228e705c121SKalle Valo 	}
3229e705c121SKalle Valo 
3230e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
3231e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
3232e705c121SKalle Valo 
3233e705c121SKalle Valo out:
3234e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
3235e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3236e705c121SKalle Valo }
3237e705c121SKalle Valo 
3238e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
3239e705c121SKalle Valo 					 struct ieee80211_vif *vif,
3240e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
3241e705c121SKalle Valo {
3242e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3243e705c121SKalle Valo 
3244e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3245e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
3246e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3247e705c121SKalle Valo }
3248e705c121SKalle Valo 
3249e705c121SKalle Valo static int
3250e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
3251e705c121SKalle Valo 				struct ieee80211_vif_chanctx_switch *vifs)
3252e705c121SKalle Valo {
3253e705c121SKalle Valo 	int ret;
3254e705c121SKalle Valo 
3255e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3256e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
3257e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
3258e705c121SKalle Valo 
3259e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
3260e705c121SKalle Valo 	if (ret) {
3261e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
3262e705c121SKalle Valo 		goto out_reassign;
3263e705c121SKalle Valo 	}
3264e705c121SKalle Valo 
3265e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
3266e705c121SKalle Valo 					   true);
3267e705c121SKalle Valo 	if (ret) {
3268e705c121SKalle Valo 		IWL_ERR(mvm,
3269e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
3270e705c121SKalle Valo 		goto out_remove;
3271e705c121SKalle Valo 	}
3272e705c121SKalle Valo 
3273e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
3274e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3275e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
3276e705c121SKalle Valo 
3277e705c121SKalle Valo 	goto out;
3278e705c121SKalle Valo 
3279e705c121SKalle Valo out_remove:
3280e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
3281e705c121SKalle Valo 
3282e705c121SKalle Valo out_reassign:
3283e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
3284e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
3285e705c121SKalle Valo 		goto out_restart;
3286e705c121SKalle Valo 	}
3287e705c121SKalle Valo 
3288e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
3289e705c121SKalle Valo 					 true)) {
3290e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
3291e705c121SKalle Valo 		goto out_restart;
3292e705c121SKalle Valo 	}
3293e705c121SKalle Valo 
3294e705c121SKalle Valo 	goto out;
3295e705c121SKalle Valo 
3296e705c121SKalle Valo out_restart:
3297e705c121SKalle Valo 	/* things keep failing, better restart the hw */
3298e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
3299e705c121SKalle Valo 
3300e705c121SKalle Valo out:
3301e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3302e705c121SKalle Valo 
3303e705c121SKalle Valo 	return ret;
3304e705c121SKalle Valo }
3305e705c121SKalle Valo 
3306e705c121SKalle Valo static int
3307e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
3308e705c121SKalle Valo 				    struct ieee80211_vif_chanctx_switch *vifs)
3309e705c121SKalle Valo {
3310e705c121SKalle Valo 	int ret;
3311e705c121SKalle Valo 
3312e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3313e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
3314e705c121SKalle Valo 
3315e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
3316e705c121SKalle Valo 					   true);
3317e705c121SKalle Valo 	if (ret) {
3318e705c121SKalle Valo 		IWL_ERR(mvm,
3319e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
3320e705c121SKalle Valo 		goto out_reassign;
3321e705c121SKalle Valo 	}
3322e705c121SKalle Valo 
3323e705c121SKalle Valo 	goto out;
3324e705c121SKalle Valo 
3325e705c121SKalle Valo out_reassign:
3326e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
3327e705c121SKalle Valo 					 true)) {
3328e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
3329e705c121SKalle Valo 		goto out_restart;
3330e705c121SKalle Valo 	}
3331e705c121SKalle Valo 
3332e705c121SKalle Valo 	goto out;
3333e705c121SKalle Valo 
3334e705c121SKalle Valo out_restart:
3335e705c121SKalle Valo 	/* things keep failing, better restart the hw */
3336e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
3337e705c121SKalle Valo 
3338e705c121SKalle Valo out:
3339e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3340e705c121SKalle Valo 
3341e705c121SKalle Valo 	return ret;
3342e705c121SKalle Valo }
3343e705c121SKalle Valo 
3344e705c121SKalle Valo static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
3345e705c121SKalle Valo 				      struct ieee80211_vif_chanctx_switch *vifs,
3346e705c121SKalle Valo 				      int n_vifs,
3347e705c121SKalle Valo 				      enum ieee80211_chanctx_switch_mode mode)
3348e705c121SKalle Valo {
3349e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3350e705c121SKalle Valo 	int ret;
3351e705c121SKalle Valo 
3352e705c121SKalle Valo 	/* we only support a single-vif right now */
3353e705c121SKalle Valo 	if (n_vifs > 1)
3354e705c121SKalle Valo 		return -EOPNOTSUPP;
3355e705c121SKalle Valo 
3356e705c121SKalle Valo 	switch (mode) {
3357e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
3358e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
3359e705c121SKalle Valo 		break;
3360e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
3361e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
3362e705c121SKalle Valo 		break;
3363e705c121SKalle Valo 	default:
3364e705c121SKalle Valo 		ret = -EOPNOTSUPP;
3365e705c121SKalle Valo 		break;
3366e705c121SKalle Valo 	}
3367e705c121SKalle Valo 
3368e705c121SKalle Valo 	return ret;
3369e705c121SKalle Valo }
3370e705c121SKalle Valo 
3371e705c121SKalle Valo static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
3372e705c121SKalle Valo 			   struct ieee80211_sta *sta,
3373e705c121SKalle Valo 			   bool set)
3374e705c121SKalle Valo {
3375e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3376e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3377e705c121SKalle Valo 
3378e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
3379e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
3380e705c121SKalle Valo 		return -EINVAL;
3381e705c121SKalle Valo 	}
3382e705c121SKalle Valo 
3383e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
3384e705c121SKalle Valo }
3385e705c121SKalle Valo 
3386e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
3387e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
3388e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
3389e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
3390e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
3391e705c121SKalle Valo };
3392e705c121SKalle Valo 
3393e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
3394e705c121SKalle Valo 				      struct ieee80211_vif *vif,
3395e705c121SKalle Valo 				      void *data, int len)
3396e705c121SKalle Valo {
3397e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
3398e705c121SKalle Valo 	int err;
3399e705c121SKalle Valo 	u32 noa_duration;
3400e705c121SKalle Valo 
3401e705c121SKalle Valo 	err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
3402e705c121SKalle Valo 	if (err)
3403e705c121SKalle Valo 		return err;
3404e705c121SKalle Valo 
3405e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
3406e705c121SKalle Valo 		return -EINVAL;
3407e705c121SKalle Valo 
3408e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
3409e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
3410e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
3411e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
3412e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
3413e705c121SKalle Valo 			return -EINVAL;
3414e705c121SKalle Valo 
3415e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
3416e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
3417e705c121SKalle Valo 			return -EINVAL;
3418e705c121SKalle Valo 
3419e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
3420e705c121SKalle Valo 		mvm->noa_vif = vif;
3421e705c121SKalle Valo 
3422e705c121SKalle Valo 		return iwl_mvm_update_quotas(mvm, false, NULL);
3423e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
3424e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
3425e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
3426e705c121SKalle Valo 		    !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
3427e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
3428e705c121SKalle Valo 			return -EINVAL;
3429e705c121SKalle Valo 
3430e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
3431e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
3432e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3433e705c121SKalle Valo 	}
3434e705c121SKalle Valo 
3435e705c121SKalle Valo 	return -EOPNOTSUPP;
3436e705c121SKalle Valo }
3437e705c121SKalle Valo 
3438e705c121SKalle Valo static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
3439e705c121SKalle Valo 				    struct ieee80211_vif *vif,
3440e705c121SKalle Valo 				    void *data, int len)
3441e705c121SKalle Valo {
3442e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3443e705c121SKalle Valo 	int err;
3444e705c121SKalle Valo 
3445e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3446e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
3447e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3448e705c121SKalle Valo 
3449e705c121SKalle Valo 	return err;
3450e705c121SKalle Valo }
3451e705c121SKalle Valo #endif
3452e705c121SKalle Valo 
3453e705c121SKalle Valo static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
3454e705c121SKalle Valo 				   struct ieee80211_vif *vif,
3455e705c121SKalle Valo 				   struct ieee80211_channel_switch *chsw)
3456e705c121SKalle Valo {
3457e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
3458e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
3459e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
3460e705c121SKalle Valo 	 * (which is when the absence time event starts).
3461e705c121SKalle Valo 	 */
3462e705c121SKalle Valo 
3463e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
3464e705c121SKalle Valo 			   "dummy channel switch op\n");
3465e705c121SKalle Valo }
3466e705c121SKalle Valo 
3467e705c121SKalle Valo static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
3468e705c121SKalle Valo 				      struct ieee80211_vif *vif,
3469e705c121SKalle Valo 				      struct ieee80211_channel_switch *chsw)
3470e705c121SKalle Valo {
3471e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3472e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
3473e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3474e705c121SKalle Valo 	u32 apply_time;
3475e705c121SKalle Valo 	int ret;
3476e705c121SKalle Valo 
3477e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3478e705c121SKalle Valo 
3479e705c121SKalle Valo 	mvmvif->csa_failed = false;
3480e705c121SKalle Valo 
3481e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
3482e705c121SKalle Valo 			   chsw->chandef.center_freq1);
3483e705c121SKalle Valo 
3484e705c121SKalle Valo 	iwl_fw_dbg_trigger_simple_stop(mvm, vif, FW_DBG_TRIGGER_CHANNEL_SWITCH);
3485e705c121SKalle Valo 
3486e705c121SKalle Valo 	switch (vif->type) {
3487e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3488e705c121SKalle Valo 		csa_vif =
3489e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
3490e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
3491e705c121SKalle Valo 		if (WARN_ONCE(csa_vif && csa_vif->csa_active,
3492e705c121SKalle Valo 			      "Another CSA is already in progress")) {
3493e705c121SKalle Valo 			ret = -EBUSY;
3494e705c121SKalle Valo 			goto out_unlock;
3495e705c121SKalle Valo 		}
3496e705c121SKalle Valo 
3497e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
3498e705c121SKalle Valo 
3499e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
3500e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
3501e705c121SKalle Valo 			ret = -EBUSY;
3502e705c121SKalle Valo 			goto out_unlock;
3503e705c121SKalle Valo 		}
3504e705c121SKalle Valo 
3505e705c121SKalle Valo 		break;
3506e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3507e705c121SKalle Valo 		/* Schedule the time event to a bit before beacon 1,
3508e705c121SKalle Valo 		 * to make sure we're in the new channel when the
3509e705c121SKalle Valo 		 * GO/AP arrives.
3510e705c121SKalle Valo 		 */
3511e705c121SKalle Valo 		apply_time = chsw->device_timestamp +
3512e705c121SKalle Valo 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
3513e705c121SKalle Valo 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
3514e705c121SKalle Valo 
3515e705c121SKalle Valo 		if (chsw->block_tx)
3516e705c121SKalle Valo 			iwl_mvm_csa_client_absent(mvm, vif);
3517e705c121SKalle Valo 
3518e705c121SKalle Valo 		iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
3519e705c121SKalle Valo 					    apply_time);
3520e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
3521e705c121SKalle Valo 			ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3522e705c121SKalle Valo 			if (ret)
3523e705c121SKalle Valo 				goto out_unlock;
3524e705c121SKalle Valo 		}
3525e705c121SKalle Valo 
3526e705c121SKalle Valo 		break;
3527e705c121SKalle Valo 	default:
3528e705c121SKalle Valo 		break;
3529e705c121SKalle Valo 	}
3530e705c121SKalle Valo 
3531e705c121SKalle Valo 	mvmvif->ps_disabled = true;
3532e705c121SKalle Valo 
3533e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
3534e705c121SKalle Valo 	if (ret)
3535e705c121SKalle Valo 		goto out_unlock;
3536e705c121SKalle Valo 
3537e705c121SKalle Valo 	/* we won't be on this channel any longer */
3538e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
3539e705c121SKalle Valo 
3540e705c121SKalle Valo out_unlock:
3541e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3542e705c121SKalle Valo 
3543e705c121SKalle Valo 	return ret;
3544e705c121SKalle Valo }
3545e705c121SKalle Valo 
3546e705c121SKalle Valo static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
3547e705c121SKalle Valo 				       struct ieee80211_vif *vif)
3548e705c121SKalle Valo {
3549e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3550e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3551e705c121SKalle Valo 	int ret;
3552e705c121SKalle Valo 
3553e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3554e705c121SKalle Valo 
3555e705c121SKalle Valo 	if (mvmvif->csa_failed) {
3556e705c121SKalle Valo 		mvmvif->csa_failed = false;
3557e705c121SKalle Valo 		ret = -EIO;
3558e705c121SKalle Valo 		goto out_unlock;
3559e705c121SKalle Valo 	}
3560e705c121SKalle Valo 
3561e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
3562e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta;
3563e705c121SKalle Valo 
3564e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
3565e705c121SKalle Valo 							  mvmvif->ap_sta_id);
3566e705c121SKalle Valo 
3567e705c121SKalle Valo 		if (WARN_ON(!mvmsta)) {
3568e705c121SKalle Valo 			ret = -EIO;
3569e705c121SKalle Valo 			goto out_unlock;
3570e705c121SKalle Valo 		}
3571e705c121SKalle Valo 
3572e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
3573e705c121SKalle Valo 
3574e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3575e705c121SKalle Valo 
3576e705c121SKalle Valo 		ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
3577e705c121SKalle Valo 		if (ret)
3578e705c121SKalle Valo 			goto out_unlock;
3579e705c121SKalle Valo 
3580e705c121SKalle Valo 		iwl_mvm_stop_session_protection(mvm, vif);
3581e705c121SKalle Valo 	}
3582e705c121SKalle Valo 
3583e705c121SKalle Valo 	mvmvif->ps_disabled = false;
3584e705c121SKalle Valo 
3585e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
3586e705c121SKalle Valo 
3587e705c121SKalle Valo out_unlock:
3588e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3589e705c121SKalle Valo 
3590e705c121SKalle Valo 	return ret;
3591e705c121SKalle Valo }
3592e705c121SKalle Valo 
3593e705c121SKalle Valo static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
3594e705c121SKalle Valo 			      struct ieee80211_vif *vif, u32 queues, bool drop)
3595e705c121SKalle Valo {
3596e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3597e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
3598e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
3599e705c121SKalle Valo 	struct ieee80211_sta *sta;
3600e705c121SKalle Valo 	int i;
3601e705c121SKalle Valo 	u32 msk = 0;
3602e705c121SKalle Valo 
3603e705c121SKalle Valo 	if (!vif || vif->type != NL80211_IFTYPE_STATION)
3604e705c121SKalle Valo 		return;
3605e705c121SKalle Valo 
3606e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3607e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
3608e705c121SKalle Valo 
3609e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
3610e705c121SKalle Valo 	for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
3611e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3612e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3613e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
3614e705c121SKalle Valo 			continue;
3615e705c121SKalle Valo 
3616e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
3617e705c121SKalle Valo 		if (mvmsta->vif != vif)
3618e705c121SKalle Valo 			continue;
3619e705c121SKalle Valo 
3620e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
3621e705c121SKalle Valo 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
3622e705c121SKalle Valo 
3623e705c121SKalle Valo 		msk |= mvmsta->tfd_queue_msk;
3624e705c121SKalle Valo 	}
3625e705c121SKalle Valo 
3626e705c121SKalle Valo 	if (drop) {
3627e705c121SKalle Valo 		if (iwl_mvm_flush_tx_path(mvm, msk, 0))
3628e705c121SKalle Valo 			IWL_ERR(mvm, "flush request fail\n");
3629e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3630e705c121SKalle Valo 	} else {
3631e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3632e705c121SKalle Valo 
3633e705c121SKalle Valo 		/* this can take a while, and we may need/want other operations
3634e705c121SKalle Valo 		 * to succeed while doing this, so do it without the mutex held
3635e705c121SKalle Valo 		 */
3636e705c121SKalle Valo 		iwl_trans_wait_tx_queue_empty(mvm->trans, msk);
3637e705c121SKalle Valo 	}
3638e705c121SKalle Valo }
3639e705c121SKalle Valo 
3640e705c121SKalle Valo static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
3641e705c121SKalle Valo 				  struct survey_info *survey)
3642e705c121SKalle Valo {
3643e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3644e705c121SKalle Valo 	int ret;
3645e705c121SKalle Valo 
3646e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
3647e705c121SKalle Valo 
3648e705c121SKalle Valo 	/* only support global statistics right now */
3649e705c121SKalle Valo 	if (idx != 0)
3650e705c121SKalle Valo 		return -ENOENT;
3651e705c121SKalle Valo 
3652e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
3653e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
3654e705c121SKalle Valo 		return -ENOENT;
3655e705c121SKalle Valo 
3656e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3657e705c121SKalle Valo 
3658e705c121SKalle Valo 	if (mvm->ucode_loaded) {
3659e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
3660e705c121SKalle Valo 		if (ret)
3661e705c121SKalle Valo 			goto out;
3662e705c121SKalle Valo 	}
3663e705c121SKalle Valo 
3664e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
3665e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
3666e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
3667e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
3668e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
3669e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
3670e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
3671e705c121SKalle Valo 
3672e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
3673e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
3674e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
3675e705c121SKalle Valo 
3676e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
3677e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
3678e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
3679e705c121SKalle Valo 
3680e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
3681e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
3682e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
3683e705c121SKalle Valo 
3684e705c121SKalle Valo 	ret = 0;
3685e705c121SKalle Valo  out:
3686e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3687e705c121SKalle Valo 	return ret;
3688e705c121SKalle Valo }
3689e705c121SKalle Valo 
3690e705c121SKalle Valo static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
3691e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3692e705c121SKalle Valo 				       struct ieee80211_sta *sta,
3693e705c121SKalle Valo 				       struct station_info *sinfo)
3694e705c121SKalle Valo {
3695e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3696e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3697e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3698e705c121SKalle Valo 
3699e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
3700e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
3701e705c121SKalle Valo 		return;
3702e705c121SKalle Valo 
3703e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
3704e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
3705e705c121SKalle Valo 		return;
3706e705c121SKalle Valo 
3707e705c121SKalle Valo 	if (!vif->bss_conf.assoc)
3708e705c121SKalle Valo 		return;
3709e705c121SKalle Valo 
3710e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3711e705c121SKalle Valo 
3712e705c121SKalle Valo 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
3713e705c121SKalle Valo 		goto unlock;
3714e705c121SKalle Valo 
3715e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
3716e705c121SKalle Valo 		goto unlock;
3717e705c121SKalle Valo 
3718e705c121SKalle Valo 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
3719e705c121SKalle Valo 			   mvmvif->beacon_stats.accu_num_beacons;
3720e705c121SKalle Valo 	sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX);
3721e705c121SKalle Valo 	if (mvmvif->beacon_stats.avg_signal) {
3722e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
3723e705c121SKalle Valo 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
3724e705c121SKalle Valo 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
3725e705c121SKalle Valo 	}
3726e705c121SKalle Valo  unlock:
3727e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3728e705c121SKalle Valo }
3729e705c121SKalle Valo 
3730e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
3731e705c121SKalle Valo 					struct ieee80211_vif *vif,
3732e705c121SKalle Valo 					const struct ieee80211_event *event)
3733e705c121SKalle Valo {
3734e705c121SKalle Valo #define CHECK_MLME_TRIGGER(_mvm, _trig, _buf, _cnt, _fmt...)	\
3735e705c121SKalle Valo 	do {							\
3736e705c121SKalle Valo 		if ((_cnt) && --(_cnt))				\
3737e705c121SKalle Valo 			break;					\
3738e705c121SKalle Valo 		iwl_mvm_fw_dbg_collect_trig(_mvm, _trig, _fmt);\
3739e705c121SKalle Valo 	} while (0)
3740e705c121SKalle Valo 
3741e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
3742e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
3743e705c121SKalle Valo 
3744e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_MLME))
3745e705c121SKalle Valo 		return;
3746e705c121SKalle Valo 
3747e705c121SKalle Valo 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_MLME);
3748e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
3749e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trig))
3750e705c121SKalle Valo 		return;
3751e705c121SKalle Valo 
3752e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
3753e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
3754e705c121SKalle Valo 			CHECK_MLME_TRIGGER(mvm, trig, buf,
3755e705c121SKalle Valo 					   trig_mlme->stop_assoc_denied,
3756e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
3757e705c121SKalle Valo 					    event->u.mlme.reason);
3758e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
3759e705c121SKalle Valo 			CHECK_MLME_TRIGGER(mvm, trig, buf,
3760e705c121SKalle Valo 					   trig_mlme->stop_assoc_timeout,
3761e705c121SKalle Valo 					   "ASSOC TIMEOUT");
3762e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
3763e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
3764e705c121SKalle Valo 			CHECK_MLME_TRIGGER(mvm, trig, buf,
3765e705c121SKalle Valo 					   trig_mlme->stop_auth_denied,
3766e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
3767e705c121SKalle Valo 					   event->u.mlme.reason);
3768e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
3769e705c121SKalle Valo 			CHECK_MLME_TRIGGER(mvm, trig, buf,
3770e705c121SKalle Valo 					   trig_mlme->stop_auth_timeout,
3771e705c121SKalle Valo 					   "AUTH TIMEOUT");
3772e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
3773e705c121SKalle Valo 		CHECK_MLME_TRIGGER(mvm, trig, buf,
3774e705c121SKalle Valo 				   trig_mlme->stop_rx_deauth,
3775e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
3776e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
3777e705c121SKalle Valo 		CHECK_MLME_TRIGGER(mvm, trig, buf,
3778e705c121SKalle Valo 				   trig_mlme->stop_tx_deauth,
3779e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
3780e705c121SKalle Valo 	}
3781e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
3782e705c121SKalle Valo }
3783e705c121SKalle Valo 
3784e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
3785e705c121SKalle Valo 					  struct ieee80211_vif *vif,
3786e705c121SKalle Valo 					  const struct ieee80211_event *event)
3787e705c121SKalle Valo {
3788e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
3789e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
3790e705c121SKalle Valo 
3791e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
3792e705c121SKalle Valo 		return;
3793e705c121SKalle Valo 
3794e705c121SKalle Valo 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
3795e705c121SKalle Valo 	ba_trig = (void *)trig->data;
3796e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trig))
3797e705c121SKalle Valo 		return;
3798e705c121SKalle Valo 
3799e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
3800e705c121SKalle Valo 		return;
3801e705c121SKalle Valo 
3802e705c121SKalle Valo 	iwl_mvm_fw_dbg_collect_trig(mvm, trig,
3803e705c121SKalle Valo 				    "BAR received from %pM, tid %d, ssn %d",
3804e705c121SKalle Valo 				    event->u.ba.sta->addr, event->u.ba.tid,
3805e705c121SKalle Valo 				    event->u.ba.ssn);
3806e705c121SKalle Valo }
3807e705c121SKalle Valo 
3808e705c121SKalle Valo static void
3809e705c121SKalle Valo iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
3810e705c121SKalle Valo 				     struct ieee80211_vif *vif,
3811e705c121SKalle Valo 				     const struct ieee80211_event *event)
3812e705c121SKalle Valo {
3813e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
3814e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
3815e705c121SKalle Valo 
3816e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
3817e705c121SKalle Valo 		return;
3818e705c121SKalle Valo 
3819e705c121SKalle Valo 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
3820e705c121SKalle Valo 	ba_trig = (void *)trig->data;
3821e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trig))
3822e705c121SKalle Valo 		return;
3823e705c121SKalle Valo 
3824e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(event->u.ba.tid)))
3825e705c121SKalle Valo 		return;
3826e705c121SKalle Valo 
3827e705c121SKalle Valo 	iwl_mvm_fw_dbg_collect_trig(mvm, trig,
3828e705c121SKalle Valo 				    "Frame from %pM timed out, tid %d",
3829e705c121SKalle Valo 				    event->u.ba.sta->addr, event->u.ba.tid);
3830e705c121SKalle Valo }
3831e705c121SKalle Valo 
3832e705c121SKalle Valo static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
3833e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3834e705c121SKalle Valo 				       const struct ieee80211_event *event)
3835e705c121SKalle Valo {
3836e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3837e705c121SKalle Valo 
3838e705c121SKalle Valo 	switch (event->type) {
3839e705c121SKalle Valo 	case MLME_EVENT:
3840e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
3841e705c121SKalle Valo 		break;
3842e705c121SKalle Valo 	case BAR_RX_EVENT:
3843e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
3844e705c121SKalle Valo 		break;
3845e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
3846e705c121SKalle Valo 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event);
3847e705c121SKalle Valo 		break;
3848e705c121SKalle Valo 	default:
3849e705c121SKalle Valo 		break;
3850e705c121SKalle Valo 	}
3851e705c121SKalle Valo }
3852e705c121SKalle Valo 
3853e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
3854e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
3855e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
3856e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
3857e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
3858e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
3859e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
3860e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
3861e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
3862e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
3863e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
3864e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
3865e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
3866e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
3867e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
3868e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
3869e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
3870e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
3871e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
3872e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
3873e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
3874e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
3875e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
3876e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
3877e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
3878e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
3879e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
3880e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
3881e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
3882e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
3883e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
3884e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
3885e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
3886e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
3887e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
3888e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
3889e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
3890e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
3891e705c121SKalle Valo 
3892e705c121SKalle Valo 	.start_ap = iwl_mvm_start_ap_ibss,
3893e705c121SKalle Valo 	.stop_ap = iwl_mvm_stop_ap_ibss,
3894e705c121SKalle Valo 	.join_ibss = iwl_mvm_start_ap_ibss,
3895e705c121SKalle Valo 	.leave_ibss = iwl_mvm_stop_ap_ibss,
3896e705c121SKalle Valo 
3897e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
3898e705c121SKalle Valo 
3899e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
3900e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
3901e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
3902e705c121SKalle Valo 
3903e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
3904e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
3905e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
3906e705c121SKalle Valo 
3907e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
3908e705c121SKalle Valo 
3909e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
3910e705c121SKalle Valo 
3911e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
3912e705c121SKalle Valo 	/* look at d3.c */
3913e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
3914e705c121SKalle Valo 	.resume = iwl_mvm_resume,
3915e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
3916e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
3917e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
3918e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
3919e705c121SKalle Valo #endif
3920e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
3921e705c121SKalle Valo #endif
3922e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
3923e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
3924e705c121SKalle Valo };
3925