xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision cb8550e15bd1c90ebce9444769a856b799340320)
1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
8e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
926d6c16bSSara Sharon  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
1026d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11e705c121SKalle Valo  *
12e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
13e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
14e705c121SKalle Valo  * published by the Free Software Foundation.
15e705c121SKalle Valo  *
16e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
17e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
18e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19e705c121SKalle Valo  * General Public License for more details.
20e705c121SKalle Valo  *
21e705c121SKalle Valo  * You should have received a copy of the GNU General Public License
22e705c121SKalle Valo  * along with this program; if not, write to the Free Software
23e705c121SKalle Valo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24e705c121SKalle Valo  * USA
25e705c121SKalle Valo  *
26e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
27e705c121SKalle Valo  * in the file called COPYING.
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * Contact Information:
30cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
31e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32e705c121SKalle Valo  *
33e705c121SKalle Valo  * BSD LICENSE
34e705c121SKalle Valo  *
35e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
3626d6c16bSSara Sharon  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
3726d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
38e705c121SKalle Valo  * All rights reserved.
39e705c121SKalle Valo  *
40e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
41e705c121SKalle Valo  * modification, are permitted provided that the following conditions
42e705c121SKalle Valo  * are met:
43e705c121SKalle Valo  *
44e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
45e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
46e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
47e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
48e705c121SKalle Valo  *    the documentation and/or other materials provided with the
49e705c121SKalle Valo  *    distribution.
50e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
51e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
52e705c121SKalle Valo  *    from this software without specific prior written permission.
53e705c121SKalle Valo  *
54e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65e705c121SKalle Valo  *
66e705c121SKalle Valo  *****************************************************************************/
67e705c121SKalle Valo #include <linux/kernel.h>
68e705c121SKalle Valo #include <linux/slab.h>
69e705c121SKalle Valo #include <linux/skbuff.h>
70e705c121SKalle Valo #include <linux/netdevice.h>
71e705c121SKalle Valo #include <linux/etherdevice.h>
72e705c121SKalle Valo #include <linux/ip.h>
73e705c121SKalle Valo #include <linux/if_arp.h>
742f89a5d7SGolan Ben-Ami #include <linux/time.h>
75e705c121SKalle Valo #include <net/mac80211.h>
76e705c121SKalle Valo #include <net/ieee80211_radiotap.h>
77e705c121SKalle Valo #include <net/tcp.h>
78e705c121SKalle Valo 
79e705c121SKalle Valo #include "iwl-op-mode.h"
80e705c121SKalle Valo #include "iwl-io.h"
81e705c121SKalle Valo #include "mvm.h"
82e705c121SKalle Valo #include "sta.h"
83e705c121SKalle Valo #include "time-event.h"
84e705c121SKalle Valo #include "iwl-eeprom-parse.h"
85e705c121SKalle Valo #include "iwl-phy-db.h"
86e705c121SKalle Valo #include "testmode.h"
87d962f9b1SJohannes Berg #include "fw/error-dump.h"
88e705c121SKalle Valo #include "iwl-prph.h"
89e705c121SKalle Valo #include "iwl-nvm-parse.h"
90e705c121SKalle Valo 
91e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
92e705c121SKalle Valo 	{
93e705c121SKalle Valo 		.max = 1,
94e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
95e705c121SKalle Valo 	},
96e705c121SKalle Valo 	{
97e705c121SKalle Valo 		.max = 1,
98e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
99e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
100e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
101e705c121SKalle Valo 	},
102e705c121SKalle Valo 	{
103e705c121SKalle Valo 		.max = 1,
104e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
105e705c121SKalle Valo 	},
106e705c121SKalle Valo };
107e705c121SKalle Valo 
108e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
109e705c121SKalle Valo 	{
110e705c121SKalle Valo 		.num_different_channels = 2,
111e705c121SKalle Valo 		.max_interfaces = 3,
112e705c121SKalle Valo 		.limits = iwl_mvm_limits,
113e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
114e705c121SKalle Valo 	},
115e705c121SKalle Valo };
116e705c121SKalle Valo 
117e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
118e705c121SKalle Valo static const struct nl80211_wowlan_tcp_data_token_feature
119e705c121SKalle Valo iwl_mvm_wowlan_tcp_token_feature = {
120e705c121SKalle Valo 	.min_len = 0,
121e705c121SKalle Valo 	.max_len = 255,
122e705c121SKalle Valo 	.bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
123e705c121SKalle Valo };
124e705c121SKalle Valo 
125e705c121SKalle Valo static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
126e705c121SKalle Valo 	.tok = &iwl_mvm_wowlan_tcp_token_feature,
127e705c121SKalle Valo 	.data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
128e705c121SKalle Valo 			    sizeof(struct ethhdr) -
129e705c121SKalle Valo 			    sizeof(struct iphdr) -
130e705c121SKalle Valo 			    sizeof(struct tcphdr),
131e705c121SKalle Valo 	.data_interval_max = 65535, /* __le16 in API */
132e705c121SKalle Valo 	.wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
133e705c121SKalle Valo 			    sizeof(struct ethhdr) -
134e705c121SKalle Valo 			    sizeof(struct iphdr) -
135e705c121SKalle Valo 			    sizeof(struct tcphdr),
136e705c121SKalle Valo 	.seq = true,
137e705c121SKalle Valo };
138e705c121SKalle Valo #endif
139e705c121SKalle Valo 
140e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
141e705c121SKalle Valo /*
142e705c121SKalle Valo  * Use the reserved field to indicate magic values.
143e705c121SKalle Valo  * these values will only be used internally by the driver,
144e705c121SKalle Valo  * and won't make it to the fw (reserved will be 0).
145e705c121SKalle Valo  * BC_FILTER_MAGIC_IP - configure the val of this attribute to
146e705c121SKalle Valo  *	be the vif's ip address. in case there is not a single
147e705c121SKalle Valo  *	ip address (0, or more than 1), this attribute will
148e705c121SKalle Valo  *	be skipped.
149e705c121SKalle Valo  * BC_FILTER_MAGIC_MAC - set the val of this attribute to
150e705c121SKalle Valo  *	the LSB bytes of the vif's mac address
151e705c121SKalle Valo  */
152e705c121SKalle Valo enum {
153e705c121SKalle Valo 	BC_FILTER_MAGIC_NONE = 0,
154e705c121SKalle Valo 	BC_FILTER_MAGIC_IP,
155e705c121SKalle Valo 	BC_FILTER_MAGIC_MAC,
156e705c121SKalle Valo };
157e705c121SKalle Valo 
158e705c121SKalle Valo static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
159e705c121SKalle Valo 	{
160e705c121SKalle Valo 		/* arp */
161e705c121SKalle Valo 		.discard = 0,
162e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
163e705c121SKalle Valo 		.attrs = {
164e705c121SKalle Valo 			{
165e705c121SKalle Valo 				/* frame type - arp, hw type - ethernet */
166e705c121SKalle Valo 				.offset_type =
167e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
168e705c121SKalle Valo 				.offset = sizeof(rfc1042_header),
169e705c121SKalle Valo 				.val = cpu_to_be32(0x08060001),
170e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
171e705c121SKalle Valo 			},
172e705c121SKalle Valo 			{
173e705c121SKalle Valo 				/* arp dest ip */
174e705c121SKalle Valo 				.offset_type =
175e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
176e705c121SKalle Valo 				.offset = sizeof(rfc1042_header) + 2 +
177e705c121SKalle Valo 					  sizeof(struct arphdr) +
178e705c121SKalle Valo 					  ETH_ALEN + sizeof(__be32) +
179e705c121SKalle Valo 					  ETH_ALEN,
180e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
181e705c121SKalle Valo 				/* mark it as special field */
182e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
183e705c121SKalle Valo 			},
184e705c121SKalle Valo 		},
185e705c121SKalle Valo 	},
186e705c121SKalle Valo 	{
187e705c121SKalle Valo 		/* dhcp offer bcast */
188e705c121SKalle Valo 		.discard = 0,
189e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
190e705c121SKalle Valo 		.attrs = {
191e705c121SKalle Valo 			{
192e705c121SKalle Valo 				/* udp dest port - 68 (bootp client)*/
193e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
194e705c121SKalle Valo 				.offset = offsetof(struct udphdr, dest),
195e705c121SKalle Valo 				.val = cpu_to_be32(0x00440000),
196e705c121SKalle Valo 				.mask = cpu_to_be32(0xffff0000),
197e705c121SKalle Valo 			},
198e705c121SKalle Valo 			{
199e705c121SKalle Valo 				/* dhcp - lsb bytes of client hw address */
200e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
201e705c121SKalle Valo 				.offset = 38,
202e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
203e705c121SKalle Valo 				/* mark it as special field */
204e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
205e705c121SKalle Valo 			},
206e705c121SKalle Valo 		},
207e705c121SKalle Valo 	},
208e705c121SKalle Valo 	/* last filter must be empty */
209e705c121SKalle Valo 	{},
210e705c121SKalle Valo };
211e705c121SKalle Valo #endif
212e705c121SKalle Valo 
213e705c121SKalle Valo void iwl_mvm_ref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
214e705c121SKalle Valo {
215e705c121SKalle Valo 	if (!iwl_mvm_is_d0i3_supported(mvm))
216e705c121SKalle Valo 		return;
217e705c121SKalle Valo 
218e705c121SKalle Valo 	IWL_DEBUG_RPM(mvm, "Take mvm reference - type %d\n", ref_type);
219e705c121SKalle Valo 	spin_lock_bh(&mvm->refs_lock);
220e705c121SKalle Valo 	mvm->refs[ref_type]++;
221e705c121SKalle Valo 	spin_unlock_bh(&mvm->refs_lock);
222e705c121SKalle Valo 	iwl_trans_ref(mvm->trans);
223e705c121SKalle Valo }
224e705c121SKalle Valo 
225e705c121SKalle Valo void iwl_mvm_unref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
226e705c121SKalle Valo {
227e705c121SKalle Valo 	if (!iwl_mvm_is_d0i3_supported(mvm))
228e705c121SKalle Valo 		return;
229e705c121SKalle Valo 
230e705c121SKalle Valo 	IWL_DEBUG_RPM(mvm, "Leave mvm reference - type %d\n", ref_type);
231e705c121SKalle Valo 	spin_lock_bh(&mvm->refs_lock);
23216c45822SSara Sharon 	if (WARN_ON(!mvm->refs[ref_type])) {
23316c45822SSara Sharon 		spin_unlock_bh(&mvm->refs_lock);
23416c45822SSara Sharon 		return;
23516c45822SSara Sharon 	}
23616c45822SSara Sharon 	mvm->refs[ref_type]--;
237e705c121SKalle Valo 	spin_unlock_bh(&mvm->refs_lock);
238e705c121SKalle Valo 	iwl_trans_unref(mvm->trans);
239e705c121SKalle Valo }
240e705c121SKalle Valo 
241e705c121SKalle Valo static void iwl_mvm_unref_all_except(struct iwl_mvm *mvm,
242e705c121SKalle Valo 				     enum iwl_mvm_ref_type except_ref)
243e705c121SKalle Valo {
244e705c121SKalle Valo 	int i, j;
245e705c121SKalle Valo 
246e705c121SKalle Valo 	if (!iwl_mvm_is_d0i3_supported(mvm))
247e705c121SKalle Valo 		return;
248e705c121SKalle Valo 
249e705c121SKalle Valo 	spin_lock_bh(&mvm->refs_lock);
250e705c121SKalle Valo 	for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
251e705c121SKalle Valo 		if (except_ref == i || !mvm->refs[i])
252e705c121SKalle Valo 			continue;
253e705c121SKalle Valo 
254e705c121SKalle Valo 		IWL_DEBUG_RPM(mvm, "Cleanup: remove mvm ref type %d (%d)\n",
255e705c121SKalle Valo 			      i, mvm->refs[i]);
256e705c121SKalle Valo 		for (j = 0; j < mvm->refs[i]; j++)
257e705c121SKalle Valo 			iwl_trans_unref(mvm->trans);
258e705c121SKalle Valo 		mvm->refs[i] = 0;
259e705c121SKalle Valo 	}
260e705c121SKalle Valo 	spin_unlock_bh(&mvm->refs_lock);
261e705c121SKalle Valo }
262e705c121SKalle Valo 
263e705c121SKalle Valo bool iwl_mvm_ref_taken(struct iwl_mvm *mvm)
264e705c121SKalle Valo {
265e705c121SKalle Valo 	int i;
266e705c121SKalle Valo 	bool taken = false;
267e705c121SKalle Valo 
268e705c121SKalle Valo 	if (!iwl_mvm_is_d0i3_supported(mvm))
269e705c121SKalle Valo 		return true;
270e705c121SKalle Valo 
271e705c121SKalle Valo 	spin_lock_bh(&mvm->refs_lock);
272e705c121SKalle Valo 	for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
273e705c121SKalle Valo 		if (mvm->refs[i]) {
274e705c121SKalle Valo 			taken = true;
275e705c121SKalle Valo 			break;
276e705c121SKalle Valo 		}
277e705c121SKalle Valo 	}
278e705c121SKalle Valo 	spin_unlock_bh(&mvm->refs_lock);
279e705c121SKalle Valo 
280e705c121SKalle Valo 	return taken;
281e705c121SKalle Valo }
282e705c121SKalle Valo 
283e705c121SKalle Valo int iwl_mvm_ref_sync(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
284e705c121SKalle Valo {
285e705c121SKalle Valo 	iwl_mvm_ref(mvm, ref_type);
286e705c121SKalle Valo 
287e705c121SKalle Valo 	if (!wait_event_timeout(mvm->d0i3_exit_waitq,
288e705c121SKalle Valo 				!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status),
289e705c121SKalle Valo 				HZ)) {
290e705c121SKalle Valo 		WARN_ON_ONCE(1);
291e705c121SKalle Valo 		iwl_mvm_unref(mvm, ref_type);
292e705c121SKalle Valo 		return -EIO;
293e705c121SKalle Valo 	}
294e705c121SKalle Valo 
295e705c121SKalle Valo 	return 0;
296e705c121SKalle Valo }
297e705c121SKalle Valo 
298e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
299e705c121SKalle Valo {
300e705c121SKalle Valo 	int i;
301e705c121SKalle Valo 
302e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
303e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
304e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
305e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
306e705c121SKalle Valo 	}
307e705c121SKalle Valo }
308e705c121SKalle Valo 
309e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
310e705c121SKalle Valo 						  const char *alpha2,
311e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
312e705c121SKalle Valo 						  bool *changed)
313e705c121SKalle Valo {
314e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
315e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
316e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
317e705c121SKalle Valo 	struct iwl_mcc_update_resp *resp;
318e705c121SKalle Valo 
319e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
320e705c121SKalle Valo 
321e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
322e705c121SKalle Valo 
323e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
324e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
325e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
326e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
327e705c121SKalle Valo 		goto out;
328e705c121SKalle Valo 	}
329e705c121SKalle Valo 
330e705c121SKalle Valo 	if (changed)
331e705c121SKalle Valo 		*changed = (resp->status == MCC_RESP_NEW_CHAN_PROFILE);
332e705c121SKalle Valo 
333e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
334e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
335e705c121SKalle Valo 				      resp->channels,
336e705c121SKalle Valo 				      __le16_to_cpu(resp->mcc));
337e705c121SKalle Valo 	/* Store the return source id */
338e705c121SKalle Valo 	src_id = resp->source_id;
339e705c121SKalle Valo 	kfree(resp);
340e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
341e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
342e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
343e705c121SKalle Valo 		goto out;
344e705c121SKalle Valo 	}
345e705c121SKalle Valo 
346e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
347e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
348e705c121SKalle Valo 	mvm->lar_regdom_set = true;
349e705c121SKalle Valo 	mvm->mcc_src = src_id;
350e705c121SKalle Valo 
351e705c121SKalle Valo out:
352e705c121SKalle Valo 	return regd;
353e705c121SKalle Valo }
354e705c121SKalle Valo 
355e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
356e705c121SKalle Valo {
357e705c121SKalle Valo 	bool changed;
358e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
359e705c121SKalle Valo 
360e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
361e705c121SKalle Valo 		return;
362e705c121SKalle Valo 
363e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
364e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
365e705c121SKalle Valo 		/* only update the regulatory core if changed */
366e705c121SKalle Valo 		if (changed)
367e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
368e705c121SKalle Valo 
369e705c121SKalle Valo 		kfree(regd);
370e705c121SKalle Valo 	}
371e705c121SKalle Valo }
372e705c121SKalle Valo 
373e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
374e705c121SKalle Valo 							  bool *changed)
375e705c121SKalle Valo {
376e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
377e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
378e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
379e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
380e705c121SKalle Valo }
381e705c121SKalle Valo 
382e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
383e705c121SKalle Valo {
384e705c121SKalle Valo 	enum iwl_mcc_source used_src;
385e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
386e705c121SKalle Valo 	int ret;
387e705c121SKalle Valo 	bool changed;
388e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
389e705c121SKalle Valo 			rtnl_dereference(mvm->hw->wiphy->regd);
390e705c121SKalle Valo 
391e705c121SKalle Valo 	if (!r)
392e705c121SKalle Valo 		return -ENOENT;
393e705c121SKalle Valo 
394e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
395e705c121SKalle Valo 	used_src = mvm->mcc_src;
396e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
397e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
398e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
399e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
400e705c121SKalle Valo 			kfree(regd);
401e705c121SKalle Valo 	}
402e705c121SKalle Valo 
403e705c121SKalle Valo 	/* Now set our last stored MCC and source */
404e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
405e705c121SKalle Valo 				     &changed);
406e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
407e705c121SKalle Valo 		return -EIO;
408e705c121SKalle Valo 
409e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
410e705c121SKalle Valo 	if (changed)
411e705c121SKalle Valo 		ret = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
412e705c121SKalle Valo 	else
413e705c121SKalle Valo 		ret = 0;
414e705c121SKalle Valo 
415e705c121SKalle Valo 	kfree(regd);
416e705c121SKalle Valo 	return ret;
417e705c121SKalle Valo }
418e705c121SKalle Valo 
419e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
420e705c121SKalle Valo {
421e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
422e705c121SKalle Valo 	int num_mac, ret, i;
423e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
424e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
425e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
426e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
427e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
428e705c121SKalle Valo 	};
429e705c121SKalle Valo 
430e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
431e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
432e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
433e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
434e705c121SKalle Valo 	ieee80211_hw_set(hw, QUEUE_CONTROL);
435e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
436e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
437e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
438e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
439e705c121SKalle Valo 	ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
440e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
441e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
442e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
443e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
444909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
44530433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
446b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
447b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
448960f864bSJohannes Berg 
449960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
450960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
45165e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
452960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
453960f864bSJohannes Berg 		/*
454960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
455960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
456960f864bSJohannes Berg 		 * for station powersave
457960f864bSJohannes Berg 		 */
458960f864bSJohannes Berg 		return -EINVAL;
459960f864bSJohannes Berg 	}
460e705c121SKalle Valo 
46180938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
46280938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
46380938abcSJohannes Berg 
4642d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
4652d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
4662d7cf549SJohannes Berg 
467cf961e16SLiad Kaufman 	hw->queues = IEEE80211_MAX_QUEUES;
468e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
469e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
470e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
471e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
472e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
473371a17edSJohannes Berg 
474371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
475371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
476371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
477371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
478371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
479371a17edSJohannes Berg 
480e705c121SKalle Valo 	hw->rate_control_algorithm = "iwl-mvm-rs";
481e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
482e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
483e705c121SKalle Valo 
4848e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
485e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
486e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
487e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
488e705c121SKalle Valo 
4892a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4902a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4912a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
4922a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4932a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4942a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
4952a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4962a53d166SAyala Beker 	}
4972a53d166SAyala Beker 
4984b87e5afSLuca Coelho 	/* Enable 11w if software crypto is not enabled (as the
4994b87e5afSLuca Coelho 	 * firmware will interpret some mgmt packets, so enabling it
5004b87e5afSLuca Coelho 	 * with software crypto isn't safe).
501e705c121SKalle Valo 	 */
5023b37f4c9SJohannes Berg 	if (!iwlwifi_mod_params.swcrypto) {
503e705c121SKalle Valo 		ieee80211_hw_set(hw, MFP_CAPABLE);
504e705c121SKalle Valo 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
505e705c121SKalle Valo 			WLAN_CIPHER_SUITE_AES_CMAC;
506e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
5078e160ab8SAyala Beker 		if (iwl_mvm_has_new_rx_api(mvm)) {
5088e160ab8SAyala Beker 			mvm->ciphers[hw->wiphy->n_cipher_suites] =
5098e160ab8SAyala Beker 				WLAN_CIPHER_SUITE_BIP_GMAC_128;
5108e160ab8SAyala Beker 			hw->wiphy->n_cipher_suites++;
5118e160ab8SAyala Beker 			mvm->ciphers[hw->wiphy->n_cipher_suites] =
5128e160ab8SAyala Beker 				WLAN_CIPHER_SUITE_BIP_GMAC_256;
5138e160ab8SAyala Beker 			hw->wiphy->n_cipher_suites++;
5148e160ab8SAyala Beker 		}
515e705c121SKalle Valo 	}
516e705c121SKalle Valo 
517e705c121SKalle Valo 	/* currently FW API supports only one optional cipher scheme */
518e705c121SKalle Valo 	if (mvm->fw->cs[0].cipher) {
51924ddddf3SJohannes Berg 		const struct iwl_fw_cipher_scheme *fwcs = &mvm->fw->cs[0];
52024ddddf3SJohannes Berg 		struct ieee80211_cipher_scheme *cs = &mvm->cs[0];
52124ddddf3SJohannes Berg 
522e705c121SKalle Valo 		mvm->hw->n_cipher_schemes = 1;
52324ddddf3SJohannes Berg 
52424ddddf3SJohannes Berg 		cs->cipher = le32_to_cpu(fwcs->cipher);
52524ddddf3SJohannes Berg 		cs->iftype = BIT(NL80211_IFTYPE_STATION);
52624ddddf3SJohannes Berg 		cs->hdr_len = fwcs->hdr_len;
52724ddddf3SJohannes Berg 		cs->pn_len = fwcs->pn_len;
52824ddddf3SJohannes Berg 		cs->pn_off = fwcs->pn_off;
52924ddddf3SJohannes Berg 		cs->key_idx_off = fwcs->key_idx_off;
53024ddddf3SJohannes Berg 		cs->key_idx_mask = fwcs->key_idx_mask;
53124ddddf3SJohannes Berg 		cs->key_idx_shift = fwcs->key_idx_shift;
53224ddddf3SJohannes Berg 		cs->mic_len = fwcs->mic_len;
53324ddddf3SJohannes Berg 
53424ddddf3SJohannes Berg 		mvm->hw->cipher_schemes = mvm->cs;
53524ddddf3SJohannes Berg 		mvm->ciphers[hw->wiphy->n_cipher_suites] = cs->cipher;
536e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
537e705c121SKalle Valo 	}
538e705c121SKalle Valo 
539e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
540e705c121SKalle Valo 	hw->wiphy->features |=
541e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
542e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
543e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
544e705c121SKalle Valo 
545e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
546e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
547e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
548e705c121SKalle Valo 
549e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
550e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
551e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
552e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
553e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
554e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
555e705c121SKalle Valo 
556e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
557e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
558e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
559e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
560e705c121SKalle Valo 	else
561e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
562e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
563e705c121SKalle Valo 
564e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
565e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
566e705c121SKalle Valo 
567e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
568e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
569e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
570e705c121SKalle Valo 
571e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
572e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
573e705c121SKalle Valo 	/* we can compensate an offset of up to 3 channels = 15 MHz */
574e705c121SKalle Valo 	hw->wiphy->max_adj_channel_rssi_comp = 3 * 5;
575e705c121SKalle Valo 
576e705c121SKalle Valo 	/* Extract MAC address */
577e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
578e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
579e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
580e705c121SKalle Valo 
581e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
582e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
583e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
584e705c121SKalle Valo 
585e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
586e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
587e705c121SKalle Valo 		       ETH_ALEN);
588e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
589e705c121SKalle Valo 		hw->wiphy->n_addresses++;
590e705c121SKalle Valo 	}
591e705c121SKalle Valo 
592e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
593e705c121SKalle Valo 
594e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
595e705c121SKalle Valo 
596e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
597e705c121SKalle Valo 
598e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
599e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
600e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
601e705c121SKalle Valo 
602e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
603e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
604e705c121SKalle Valo 	else
605e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
606e705c121SKalle Valo 
60757fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
60857fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
60957fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
61057fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
61157fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
61257fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
613e705c121SKalle Valo 
614e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
615e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
616e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
617e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
61857fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
619e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
620e705c121SKalle Valo 	}
621e705c121SKalle Valo 
622e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
623e705c121SKalle Valo 
624e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
625e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
626e705c121SKalle Valo 	else
627e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
628e705c121SKalle Valo 
629ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
630e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
631e705c121SKalle Valo 	hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
632e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
633e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
634e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
635e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
636e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
637e705c121SKalle Valo 
638e705c121SKalle Valo 	/*
639e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
640e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
641e705c121SKalle Valo 	 */
642e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
643e705c121SKalle Valo 
644e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
645e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
646e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
647a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
648e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
649e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
650e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
651e705c121SKalle Valo 
652e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
653e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
654e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
655e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
656e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
657e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
658e705c121SKalle Valo 
659e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
660e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
661e705c121SKalle Valo 		hw->wiphy->features |=
662e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
663e705c121SKalle Valo 
664e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
665e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
666e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
667e705c121SKalle Valo 
668aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
669aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
670aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
671aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
672aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
673aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
674aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
675aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
676aacf8f18SAvrahams Stern 	}
677aacf8f18SAvrahams Stern 
678e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
679e705c121SKalle Valo 
680e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
681e705c121SKalle Valo 	if (iwl_mvm_is_d0i3_supported(mvm) &&
682e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
683e705c121SKalle Valo 		mvm->wowlan.flags = WIPHY_WOWLAN_ANY;
684e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
685e705c121SKalle Valo 	}
686e705c121SKalle Valo 
687eef187a7SSara Sharon 	if (mvm->fw->img[IWL_UCODE_WOWLAN].num_sec &&
688e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
689e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
690e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
691e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
692e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
693e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
694e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
695e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
6963b37f4c9SJohannes Berg 		if (!iwlwifi_mod_params.swcrypto)
697e705c121SKalle Valo 			mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
698e705c121SKalle Valo 					     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
699e705c121SKalle Valo 					     WIPHY_WOWLAN_4WAY_HANDSHAKE;
700e705c121SKalle Valo 
701e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
702e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
703e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
704e705c121SKalle Valo 		mvm->wowlan.max_nd_match_sets = IWL_SCAN_MAX_PROFILES;
705e705c121SKalle Valo 		mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
706e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
707e705c121SKalle Valo 	}
708e705c121SKalle Valo #endif
709e705c121SKalle Valo 
710e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
711e705c121SKalle Valo 	/* assign default bcast filtering configuration */
712e705c121SKalle Valo 	mvm->bcast_filters = iwl_mvm_default_bcast_filters;
713e705c121SKalle Valo #endif
714e705c121SKalle Valo 
715e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
716e705c121SKalle Valo 	if (ret)
717e705c121SKalle Valo 		return ret;
718e705c121SKalle Valo 
719e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
720e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
721e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
722e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
723e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
724e705c121SKalle Valo 	}
725e705c121SKalle Valo 
726e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
727e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
728e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
729e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
730e705c121SKalle Valo 	}
731e705c121SKalle Valo 
732e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
7335e6a98dcSSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm)) {
7345e6a98dcSSara Sharon 		hw->netdev_features &= ~(IWL_TX_CSUM_NETIF_FLAGS |
7355e6a98dcSSara Sharon 					 NETIF_F_RXCSUM);
7365e6a98dcSSara Sharon 		/* We may support SW TX CSUM */
73741837ca9SEmmanuel Grumbach 		if (IWL_MVM_SW_TX_CSUM_OFFLOAD)
7385e6a98dcSSara Sharon 			hw->netdev_features |= IWL_TX_CSUM_NETIF_FLAGS;
7395e6a98dcSSara Sharon 	}
74041837ca9SEmmanuel Grumbach 
741e705c121SKalle Valo 	ret = ieee80211_register_hw(mvm->hw);
742e705c121SKalle Valo 	if (ret)
743e705c121SKalle Valo 		iwl_mvm_leds_exit(mvm);
744de8ba41bSLiad Kaufman 	mvm->init_status |= IWL_MVM_INIT_STATUS_REG_HW_INIT_COMPLETE;
745e705c121SKalle Valo 
74691b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
74791b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
74891b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
74991b08c2dSAviya Erenfeld 
750e705c121SKalle Valo 	return ret;
751e705c121SKalle Valo }
752e705c121SKalle Valo 
753e705c121SKalle Valo static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
754e705c121SKalle Valo 			     struct ieee80211_sta *sta,
755e705c121SKalle Valo 			     struct sk_buff *skb)
756e705c121SKalle Valo {
757e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
758e705c121SKalle Valo 	bool defer = false;
759e705c121SKalle Valo 
760e705c121SKalle Valo 	/*
761e705c121SKalle Valo 	 * double check the IN_D0I3 flag both before and after
762e705c121SKalle Valo 	 * taking the spinlock, in order to prevent taking
763e705c121SKalle Valo 	 * the spinlock when not needed.
764e705c121SKalle Valo 	 */
765e705c121SKalle Valo 	if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
766e705c121SKalle Valo 		return false;
767e705c121SKalle Valo 
768e705c121SKalle Valo 	spin_lock(&mvm->d0i3_tx_lock);
769e705c121SKalle Valo 	/*
770e705c121SKalle Valo 	 * testing the flag again ensures the skb dequeue
771e705c121SKalle Valo 	 * loop (on d0i3 exit) hasn't run yet.
772e705c121SKalle Valo 	 */
773e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
774e705c121SKalle Valo 		goto out;
775e705c121SKalle Valo 
776e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
7770ae98812SSara Sharon 	if (mvmsta->sta_id == IWL_MVM_INVALID_STA ||
778e705c121SKalle Valo 	    mvmsta->sta_id != mvm->d0i3_ap_sta_id)
779e705c121SKalle Valo 		goto out;
780e705c121SKalle Valo 
781e705c121SKalle Valo 	__skb_queue_tail(&mvm->d0i3_tx, skb);
782e705c121SKalle Valo 	ieee80211_stop_queues(mvm->hw);
783e705c121SKalle Valo 
784e705c121SKalle Valo 	/* trigger wakeup */
785e705c121SKalle Valo 	iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
786e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
787e705c121SKalle Valo 
788e705c121SKalle Valo 	defer = true;
789e705c121SKalle Valo out:
790e705c121SKalle Valo 	spin_unlock(&mvm->d0i3_tx_lock);
791e705c121SKalle Valo 	return defer;
792e705c121SKalle Valo }
793e705c121SKalle Valo 
794e705c121SKalle Valo static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
795e705c121SKalle Valo 			   struct ieee80211_tx_control *control,
796e705c121SKalle Valo 			   struct sk_buff *skb)
797e705c121SKalle Valo {
798e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
799e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
800e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
801e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
802e705c121SKalle Valo 
803e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
804e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
805e705c121SKalle Valo 		goto drop;
806e705c121SKalle Valo 	}
807e705c121SKalle Valo 
808d20e30afSEmmanuel Grumbach 	if (info->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
809e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
810e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
811e705c121SKalle Valo 		goto drop;
812e705c121SKalle Valo 
813eb045e6eSDavid Spinadel 	/* treat non-bufferable MMPDUs on AP interfaces as broadcast */
814eb045e6eSDavid Spinadel 	if ((info->control.vif->type == NL80211_IFTYPE_AP ||
815eb045e6eSDavid Spinadel 	     info->control.vif->type == NL80211_IFTYPE_ADHOC) &&
816e705c121SKalle Valo 	    ieee80211_is_mgmt(hdr->frame_control) &&
817eb045e6eSDavid Spinadel 	    !ieee80211_is_bufferable_mmpdu(hdr->frame_control))
818e705c121SKalle Valo 		sta = NULL;
819e705c121SKalle Valo 
820e705c121SKalle Valo 	if (sta) {
821e705c121SKalle Valo 		if (iwl_mvm_defer_tx(mvm, sta, skb))
822e705c121SKalle Valo 			return;
823e705c121SKalle Valo 		if (iwl_mvm_tx_skb(mvm, skb, sta))
824e705c121SKalle Valo 			goto drop;
825e705c121SKalle Valo 		return;
826e705c121SKalle Valo 	}
827e705c121SKalle Valo 
828e705c121SKalle Valo 	if (iwl_mvm_tx_skb_non_sta(mvm, skb))
829e705c121SKalle Valo 		goto drop;
830e705c121SKalle Valo 	return;
831e705c121SKalle Valo  drop:
832e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
833e705c121SKalle Valo }
834e705c121SKalle Valo 
835e705c121SKalle Valo static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
836e705c121SKalle Valo {
837e705c121SKalle Valo 	if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
838e705c121SKalle Valo 		return false;
839e705c121SKalle Valo 	return true;
840e705c121SKalle Valo }
841e705c121SKalle Valo 
842e705c121SKalle Valo static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
843e705c121SKalle Valo {
844e705c121SKalle Valo 	if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
845e705c121SKalle Valo 		return false;
846e705c121SKalle Valo 	if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
847e705c121SKalle Valo 		return true;
848e705c121SKalle Valo 
849e705c121SKalle Valo 	/* enabled by default */
850e705c121SKalle Valo 	return true;
851e705c121SKalle Valo }
852e705c121SKalle Valo 
853e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
854e705c121SKalle Valo 	do {								\
855e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
856e705c121SKalle Valo 			break;						\
8577174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
858e705c121SKalle Valo 	} while (0)
859e705c121SKalle Valo 
860e705c121SKalle Valo static void
861e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
862e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
863e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
864e705c121SKalle Valo {
865e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
866e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
867e705c121SKalle Valo 
868e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
869e705c121SKalle Valo 		return;
870e705c121SKalle Valo 
871e705c121SKalle Valo 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
872e705c121SKalle Valo 	ba_trig = (void *)trig->data;
873e705c121SKalle Valo 
8747174beb6SJohannes Berg 	if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
8757174beb6SJohannes Berg 					   ieee80211_vif_to_wdev(vif), trig))
876e705c121SKalle Valo 		return;
877e705c121SKalle Valo 
878e705c121SKalle Valo 	switch (action) {
879e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
880e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
881e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
882e705c121SKalle Valo 
883e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
884e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
885e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
886e705c121SKalle Valo 		break;
887e705c121SKalle Valo 		}
888e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
889e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
890e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
891e705c121SKalle Valo 				 sta->addr, tid);
892e705c121SKalle Valo 		break;
893e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
894e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
895e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
896e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
897e705c121SKalle Valo 		break;
898e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
899e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
900e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
901e705c121SKalle Valo 				 sta->addr, tid);
902e705c121SKalle Valo 		break;
903e705c121SKalle Valo 	default:
904e705c121SKalle Valo 		break;
905e705c121SKalle Valo 	}
906e705c121SKalle Valo }
907e705c121SKalle Valo 
908e705c121SKalle Valo static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
909e705c121SKalle Valo 				    struct ieee80211_vif *vif,
91050ea05efSSara Sharon 				    struct ieee80211_ampdu_params *params)
911e705c121SKalle Valo {
912e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
913e705c121SKalle Valo 	int ret;
914e705c121SKalle Valo 	bool tx_agg_ref = false;
91550ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
91650ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
91750ea05efSSara Sharon 	u16 tid = params->tid;
91850ea05efSSara Sharon 	u16 *ssn = &params->ssn;
91950ea05efSSara Sharon 	u8 buf_size = params->buf_size;
920bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
92110b2b201SSara Sharon 	u16 timeout = params->timeout;
922e705c121SKalle Valo 
923e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
924e705c121SKalle Valo 		     sta->addr, tid, action);
925e705c121SKalle Valo 
926e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
927e705c121SKalle Valo 		return -EACCES;
928e705c121SKalle Valo 
929e705c121SKalle Valo 	/* return from D0i3 before starting a new Tx aggregation */
930e705c121SKalle Valo 	switch (action) {
931e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
932e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
933e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
934e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
935e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
936e705c121SKalle Valo 		/*
937e705c121SKalle Valo 		 * for tx start, wait synchronously until D0i3 exit to
938e705c121SKalle Valo 		 * get the correct sequence number for the tid.
939e705c121SKalle Valo 		 * additionally, some other ampdu actions use direct
940e705c121SKalle Valo 		 * target access, which is not handled automatically
941e705c121SKalle Valo 		 * by the trans layer (unlike commands), so wait for
942e705c121SKalle Valo 		 * d0i3 exit in these cases as well.
943e705c121SKalle Valo 		 */
944e705c121SKalle Valo 		ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_TX_AGG);
945e705c121SKalle Valo 		if (ret)
946e705c121SKalle Valo 			return ret;
947e705c121SKalle Valo 
948e705c121SKalle Valo 		tx_agg_ref = true;
949e705c121SKalle Valo 		break;
950e705c121SKalle Valo 	default:
951e705c121SKalle Valo 		break;
952e705c121SKalle Valo 	}
953e705c121SKalle Valo 
954e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
955e705c121SKalle Valo 
956e705c121SKalle Valo 	switch (action) {
957e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
958e705c121SKalle Valo 		if (!iwl_enable_rx_ampdu(mvm->cfg)) {
959e705c121SKalle Valo 			ret = -EINVAL;
960e705c121SKalle Valo 			break;
961e705c121SKalle Valo 		}
96210b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
96310b2b201SSara Sharon 					 timeout);
964e705c121SKalle Valo 		break;
965e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
96610b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
96710b2b201SSara Sharon 					 timeout);
968e705c121SKalle Valo 		break;
969e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
970e705c121SKalle Valo 		if (!iwl_enable_tx_ampdu(mvm->cfg)) {
971e705c121SKalle Valo 			ret = -EINVAL;
972e705c121SKalle Valo 			break;
973e705c121SKalle Valo 		}
974e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
975e705c121SKalle Valo 		break;
976e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
977e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
978e705c121SKalle Valo 		break;
979e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
980e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
981e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
982e705c121SKalle Valo 		break;
983e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
984bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
985bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
986e705c121SKalle Valo 		break;
987e705c121SKalle Valo 	default:
988e705c121SKalle Valo 		WARN_ON_ONCE(1);
989e705c121SKalle Valo 		ret = -EINVAL;
990e705c121SKalle Valo 		break;
991e705c121SKalle Valo 	}
992e705c121SKalle Valo 
993e705c121SKalle Valo 	if (!ret) {
994e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
995e705c121SKalle Valo 
996e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
997e705c121SKalle Valo 			rx_ba_ssn = *ssn;
998e705c121SKalle Valo 
999e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1000e705c121SKalle Valo 					    rx_ba_ssn, action);
1001e705c121SKalle Valo 	}
1002e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1003e705c121SKalle Valo 
1004e705c121SKalle Valo 	/*
1005e705c121SKalle Valo 	 * If the tid is marked as started, we won't use it for offloaded
1006e705c121SKalle Valo 	 * traffic on the next D0i3 entry. It's safe to unref.
1007e705c121SKalle Valo 	 */
1008e705c121SKalle Valo 	if (tx_agg_ref)
1009e705c121SKalle Valo 		iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
1010e705c121SKalle Valo 
1011e705c121SKalle Valo 	return ret;
1012e705c121SKalle Valo }
1013e705c121SKalle Valo 
1014e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1015e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1016e705c121SKalle Valo {
1017e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1018e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1019e705c121SKalle Valo 
1020e705c121SKalle Valo 	mvmvif->uploaded = false;
10210ae98812SSara Sharon 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1022e705c121SKalle Valo 
1023e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1024e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1025e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1026e705c121SKalle Valo 
1027e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1028e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
1029e705c121SKalle Valo }
1030e705c121SKalle Valo 
1031e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1032e705c121SKalle Valo {
1033e705c121SKalle Valo 	/* clear the D3 reconfig, we only need it to avoid dumping a
1034e705c121SKalle Valo 	 * firmware coredump on reconfiguration, we shouldn't do that
1035e705c121SKalle Valo 	 * on D3->D0 transition
1036e705c121SKalle Valo 	 */
1037e705c121SKalle Valo 	if (!test_and_clear_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status)) {
10387174beb6SJohannes Berg 		mvm->fwrt.dump.desc = &iwl_dump_desc_assert;
10397174beb6SJohannes Berg 		iwl_fw_error_dump(&mvm->fwrt);
1040e705c121SKalle Valo 	}
1041e705c121SKalle Valo 
1042e705c121SKalle Valo 	/* cleanup all stale references (scan, roc), but keep the
1043e705c121SKalle Valo 	 * ucode_down ref until reconfig is complete
1044e705c121SKalle Valo 	 */
1045e705c121SKalle Valo 	iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
1046e705c121SKalle Valo 
1047fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1048e705c121SKalle Valo 
1049e705c121SKalle Valo 	mvm->scan_status = 0;
1050e705c121SKalle Valo 	mvm->ps_disabled = false;
1051e705c121SKalle Valo 	mvm->calibrating = false;
1052e705c121SKalle Valo 
1053e705c121SKalle Valo 	/* just in case one was running */
1054305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1055e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1056e705c121SKalle Valo 
1057e705c121SKalle Valo 	/*
1058e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1059e705c121SKalle Valo 	 * gone down during the HW restart
1060e705c121SKalle Valo 	 */
1061e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1062e705c121SKalle Valo 
1063e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
10640ae98812SSara Sharon 	mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
1065e705c121SKalle Valo 
1066e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
10679c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
106824afba76SLiad Kaufman 	memset(mvm->sta_deferred_frames, 0, sizeof(mvm->sta_deferred_frames));
1069e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1070e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1071e705c121SKalle Valo 
1072e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1073e705c121SKalle Valo 
1074e705c121SKalle Valo 	/* clear any stale d0i3 state */
1075e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
1076e705c121SKalle Valo 
1077e705c121SKalle Valo 	mvm->vif_count = 0;
1078e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
10797174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1080baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1081e705c121SKalle Valo 
1082e705c121SKalle Valo 	/* keep statistics ticking */
1083e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1084e705c121SKalle Valo }
1085e705c121SKalle Valo 
1086e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1087e705c121SKalle Valo {
1088e705c121SKalle Valo 	int ret;
1089e705c121SKalle Valo 
1090e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1091e705c121SKalle Valo 
1092bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1093bf8b286fSJohannes Berg 		/*
1094bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1095bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1096bf8b286fSJohannes Berg 		 */
1097bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1098bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1099e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1100e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1101a42b2af3SLuca Coelho 	} else {
1102a42b2af3SLuca Coelho 		/* Hold the reference to prevent runtime suspend while
1103a42b2af3SLuca Coelho 		 * the start procedure runs.  It's a bit confusing
1104a42b2af3SLuca Coelho 		 * that the UCODE_DOWN reference is taken, but it just
1105a42b2af3SLuca Coelho 		 * means "UCODE is not UP yet". ( TODO: rename this
1106a42b2af3SLuca Coelho 		 * reference).
1107a42b2af3SLuca Coelho 		 */
1108a42b2af3SLuca Coelho 		iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1109a42b2af3SLuca Coelho 	}
1110e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1111e705c121SKalle Valo 
1112e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1113e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1114e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1115e705c121SKalle Valo 		 * would do.
1116e705c121SKalle Valo 		 */
1117e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1118e705c121SKalle Valo 		iwl_mvm_d0i3_enable_tx(mvm, NULL);
1119e705c121SKalle Valo 	}
1120e705c121SKalle Valo 
1121e705c121SKalle Valo 	return ret;
1122e705c121SKalle Valo }
1123e705c121SKalle Valo 
1124e705c121SKalle Valo static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1125e705c121SKalle Valo {
1126e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1127e705c121SKalle Valo 	int ret;
1128e705c121SKalle Valo 
1129e705c121SKalle Valo 	/* Some hw restart cleanups must not hold the mutex */
1130e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1131e705c121SKalle Valo 		/*
1132e705c121SKalle Valo 		 * Make sure we are out of d0i3. This is needed
1133e705c121SKalle Valo 		 * to make sure the reference accounting is correct
1134e705c121SKalle Valo 		 * (and there is no stale d0i3_exit_work).
1135e705c121SKalle Valo 		 */
1136e705c121SKalle Valo 		wait_event_timeout(mvm->d0i3_exit_waitq,
1137e705c121SKalle Valo 				   !test_bit(IWL_MVM_STATUS_IN_D0I3,
1138e705c121SKalle Valo 					     &mvm->status),
1139e705c121SKalle Valo 				   HZ);
1140e705c121SKalle Valo 	}
1141e705c121SKalle Valo 
1142e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1143e705c121SKalle Valo 	ret = __iwl_mvm_mac_start(mvm);
1144e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1145e705c121SKalle Valo 
1146e705c121SKalle Valo 	return ret;
1147e705c121SKalle Valo }
1148e705c121SKalle Valo 
1149e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1150e705c121SKalle Valo {
1151e705c121SKalle Valo 	int ret;
1152e705c121SKalle Valo 
1153e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1154e705c121SKalle Valo 
1155e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1156e705c121SKalle Valo 	iwl_mvm_d0i3_enable_tx(mvm, NULL);
1157e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1158e705c121SKalle Valo 	if (ret)
1159e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1160e705c121SKalle Valo 			ret);
1161e705c121SKalle Valo 
1162e705c121SKalle Valo 	/* allow transport/FW low power modes */
1163e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
1164e705c121SKalle Valo 
1165e705c121SKalle Valo 	/*
1166e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1167e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1168e705c121SKalle Valo 	 */
1169e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1170e705c121SKalle Valo 
1171e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1172e705c121SKalle Valo }
1173e705c121SKalle Valo 
1174e705c121SKalle Valo static void iwl_mvm_resume_complete(struct iwl_mvm *mvm)
1175e705c121SKalle Valo {
1176b7282643SLuca Coelho 	if (iwl_mvm_is_d0i3_supported(mvm) &&
1177b7282643SLuca Coelho 	    iwl_mvm_enter_d0i3_on_suspend(mvm))
1178b7282643SLuca Coelho 		WARN_ONCE(!wait_event_timeout(mvm->d0i3_exit_waitq,
1179e705c121SKalle Valo 					      !test_bit(IWL_MVM_STATUS_IN_D0I3,
1180e705c121SKalle Valo 							&mvm->status),
1181b7282643SLuca Coelho 					      HZ),
1182b7282643SLuca Coelho 			  "D0i3 exit on resume timed out\n");
1183e705c121SKalle Valo }
1184e705c121SKalle Valo 
1185e705c121SKalle Valo static void
1186e705c121SKalle Valo iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1187e705c121SKalle Valo 			      enum ieee80211_reconfig_type reconfig_type)
1188e705c121SKalle Valo {
1189e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1190e705c121SKalle Valo 
1191e705c121SKalle Valo 	switch (reconfig_type) {
1192e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1193e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1194e705c121SKalle Valo 		break;
1195e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1196e705c121SKalle Valo 		iwl_mvm_resume_complete(mvm);
1197e705c121SKalle Valo 		break;
1198e705c121SKalle Valo 	}
1199e705c121SKalle Valo }
1200e705c121SKalle Valo 
1201e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1202e705c121SKalle Valo {
1203e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1204e705c121SKalle Valo 
1205e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1206e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1207e705c121SKalle Valo 	 */
1208e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1209e705c121SKalle Valo 
1210e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1211e705c121SKalle Valo 
1212e705c121SKalle Valo 	/*
1213e705c121SKalle Valo 	 * The work item could be running or queued if the
1214e705c121SKalle Valo 	 * ROC time event stops just as we get here.
1215e705c121SKalle Valo 	 */
1216e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
1217e705c121SKalle Valo 
1218fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1219e705c121SKalle Valo 
1220e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1221e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1222e705c121SKalle Valo 
1223e705c121SKalle Valo 	/* the fw is stopped, the aux sta is dead: clean up driver state */
1224e705c121SKalle Valo 	iwl_mvm_del_aux_sta(mvm);
1225e705c121SKalle Valo 
1226e705c121SKalle Valo 	/*
1227e705c121SKalle Valo 	 * Clear IN_HW_RESTART flag when stopping the hw (as restart_complete()
1228e705c121SKalle Valo 	 * won't be called in this case).
1229e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1230e705c121SKalle Valo 	 * HW restart was requested.
1231e705c121SKalle Valo 	 */
1232e705c121SKalle Valo 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1233e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1234e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1235e705c121SKalle Valo 
1236e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1237e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1238e705c121SKalle Valo 	 */
1239e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1240e705c121SKalle Valo 		int i;
1241e705c121SKalle Valo 
1242e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1243e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1244e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1245e705c121SKalle Valo 				      i))
1246e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1247e705c121SKalle Valo 		}
1248e705c121SKalle Valo 	}
1249e705c121SKalle Valo }
1250e705c121SKalle Valo 
1251e705c121SKalle Valo static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1252e705c121SKalle Valo {
1253e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1254e705c121SKalle Valo 
1255e705c121SKalle Valo 	flush_work(&mvm->d0i3_exit_work);
1256e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
125724afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1258771147b0SJohannes Berg 
1259771147b0SJohannes Berg 	/*
1260771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1261771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1262771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
12637174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1264771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1265771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1266771147b0SJohannes Berg 	 */
1267771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1268771147b0SJohannes Berg 
12697174beb6SJohannes Berg 	iwl_fw_cancel_dump(&mvm->fwrt);
1270d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
127169e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
12727174beb6SJohannes Berg 	iwl_fw_free_dump_desc(&mvm->fwrt);
1273e705c121SKalle Valo 
1274e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1275e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1276e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1277e705c121SKalle Valo 
1278e705c121SKalle Valo 	/*
1279e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1280e705c121SKalle Valo 	 * discover that its list is now empty.
1281e705c121SKalle Valo 	 */
1282e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1283e705c121SKalle Valo }
1284e705c121SKalle Valo 
1285e705c121SKalle Valo static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1286e705c121SKalle Valo {
1287e705c121SKalle Valo 	u16 i;
1288e705c121SKalle Valo 
1289e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1290e705c121SKalle Valo 
1291e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1292e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1293e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1294e705c121SKalle Valo 
1295e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1296e705c121SKalle Valo 	return NULL;
1297e705c121SKalle Valo }
1298e705c121SKalle Valo 
1299e705c121SKalle Valo static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1300e705c121SKalle Valo 				s16 tx_power)
1301e705c121SKalle Valo {
1302e705c121SKalle Valo 	struct iwl_dev_tx_power_cmd cmd = {
13034b87e5afSLuca Coelho 		.v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
13044b87e5afSLuca Coelho 		.v3.mac_context_id =
1305e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
13064b87e5afSLuca Coelho 		.v3.pwr_restriction = cpu_to_le16(8 * tx_power),
1307e705c121SKalle Valo 	};
1308e705c121SKalle Valo 	int len = sizeof(cmd);
1309e705c121SKalle Valo 
1310e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
13114b87e5afSLuca Coelho 		cmd.v3.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1312e705c121SKalle Valo 
131355bfa4b9SLuca Coelho 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
131455bfa4b9SLuca Coelho 		len = sizeof(cmd.v3);
1315e705c121SKalle Valo 
1316e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
1317e705c121SKalle Valo }
1318e705c121SKalle Valo 
1319e705c121SKalle Valo static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1320e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1321e705c121SKalle Valo {
1322e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1323e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1324e705c121SKalle Valo 	int ret;
1325e705c121SKalle Valo 
1326e705c121SKalle Valo 	mvmvif->mvm = mvm;
1327e705c121SKalle Valo 
1328e705c121SKalle Valo 	/*
1329e705c121SKalle Valo 	 * make sure D0i3 exit is completed, otherwise a target access
1330e705c121SKalle Valo 	 * during tx queue configuration could be done when still in
1331e705c121SKalle Valo 	 * D0i3 state.
1332e705c121SKalle Valo 	 */
1333e705c121SKalle Valo 	ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_ADD_IF);
1334e705c121SKalle Valo 	if (ret)
1335e705c121SKalle Valo 		return ret;
1336e705c121SKalle Valo 
1337e705c121SKalle Valo 	/*
1338e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1339e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1340e705c121SKalle Valo 	 * don't really have to check the types.
1341e705c121SKalle Valo 	 */
1342e705c121SKalle Valo 
1343e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1344e705c121SKalle Valo 
1345e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1346e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1347e705c121SKalle Valo 		mvmvif->beacon_stats.accu_num_beacons +=
1348e705c121SKalle Valo 			mvmvif->beacon_stats.num_beacons;
1349e705c121SKalle Valo 
1350e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
1351e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1352e705c121SKalle Valo 	if (ret)
1353e705c121SKalle Valo 		goto out_unlock;
1354e705c121SKalle Valo 
1355e705c121SKalle Valo 	/* Counting number of interfaces is needed for legacy PM */
1356e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1357e705c121SKalle Valo 		mvm->vif_count++;
1358e705c121SKalle Valo 
1359e705c121SKalle Valo 	/*
1360e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1361e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1362e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1363e705c121SKalle Valo 	 * only after the binding.
1364e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1365e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1366e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1367e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1368e705c121SKalle Valo 	 * allocating resources :)
1369e705c121SKalle Valo 	 */
1370e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1371e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1372e705c121SKalle Valo 		ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1373e705c121SKalle Valo 		if (ret) {
1374e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1375e705c121SKalle Valo 			goto out_release;
1376e705c121SKalle Valo 		}
1377e705c121SKalle Valo 
137826d6c16bSSara Sharon 		/*
137926d6c16bSSara Sharon 		 * Only queue for this station is the mcast queue,
138026d6c16bSSara Sharon 		 * which shouldn't be in TFD mask anyway
138126d6c16bSSara Sharon 		 */
138226d6c16bSSara Sharon 		ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta,
1383ced19f26SSara Sharon 					       0, vif->type,
1384ced19f26SSara Sharon 					       IWL_STA_MULTICAST);
138526d6c16bSSara Sharon 		if (ret)
138626d6c16bSSara Sharon 			goto out_release;
138726d6c16bSSara Sharon 
1388e705c121SKalle Valo 		iwl_mvm_vif_dbgfs_register(mvm, vif);
1389e705c121SKalle Valo 		goto out_unlock;
1390e705c121SKalle Valo 	}
1391e705c121SKalle Valo 
1392e705c121SKalle Valo 	mvmvif->features |= hw->netdev_features;
1393e705c121SKalle Valo 
1394e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1395e705c121SKalle Valo 	if (ret)
1396e705c121SKalle Valo 		goto out_release;
1397e705c121SKalle Valo 
1398e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1399e705c121SKalle Valo 	if (ret)
1400e705c121SKalle Valo 		goto out_remove_mac;
1401e705c121SKalle Valo 
1402e705c121SKalle Valo 	/* beacon filtering */
1403e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1404e705c121SKalle Valo 	if (ret)
1405e705c121SKalle Valo 		goto out_remove_mac;
1406e705c121SKalle Valo 
1407e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1408e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1409e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1410e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1411e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1412e705c121SKalle Valo 	}
1413e705c121SKalle Valo 
1414e705c121SKalle Valo 	/*
1415e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1416e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1417e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1418e705c121SKalle Valo 	 */
1419e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1420e705c121SKalle Valo 
1421e705c121SKalle Valo 		mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1422e705c121SKalle Valo 		if (!mvmvif->phy_ctxt) {
1423e705c121SKalle Valo 			ret = -ENOSPC;
1424e705c121SKalle Valo 			goto out_free_bf;
1425e705c121SKalle Valo 		}
1426e705c121SKalle Valo 
1427e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1428e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1429e705c121SKalle Valo 		if (ret)
1430e705c121SKalle Valo 			goto out_unref_phy;
1431e705c121SKalle Valo 
1432d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1433e705c121SKalle Valo 		if (ret)
1434e705c121SKalle Valo 			goto out_unbind;
1435e705c121SKalle Valo 
1436e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1437e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1438e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1439e705c121SKalle Valo 	}
1440e705c121SKalle Valo 
1441baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1442baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
1443baf41bc3SShaul Triebitz 
1444e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
1445e705c121SKalle Valo 	goto out_unlock;
1446e705c121SKalle Valo 
1447e705c121SKalle Valo  out_unbind:
1448e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1449e705c121SKalle Valo  out_unref_phy:
1450e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1451e705c121SKalle Valo  out_free_bf:
1452e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1453e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1454e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1455e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1456e705c121SKalle Valo 	}
1457e705c121SKalle Valo  out_remove_mac:
1458e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1459e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1460e705c121SKalle Valo  out_release:
1461e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1462e705c121SKalle Valo 		mvm->vif_count--;
1463e705c121SKalle Valo  out_unlock:
1464e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1465e705c121SKalle Valo 
1466e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_ADD_IF);
1467e705c121SKalle Valo 
1468e705c121SKalle Valo 	return ret;
1469e705c121SKalle Valo }
1470e705c121SKalle Valo 
1471e705c121SKalle Valo static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1472e705c121SKalle Valo 					struct ieee80211_vif *vif)
1473e705c121SKalle Valo {
1474e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1475e705c121SKalle Valo 		/*
1476e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1477e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1478e705c121SKalle Valo 		 * queue are sent in ROC session.
1479e705c121SKalle Valo 		 */
1480e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1481e705c121SKalle Valo 	}
1482e705c121SKalle Valo }
1483e705c121SKalle Valo 
1484e705c121SKalle Valo static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1485e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1486e705c121SKalle Valo {
1487e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1488e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1489e705c121SKalle Valo 
1490e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1491e705c121SKalle Valo 
1492e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1493e705c121SKalle Valo 
1494e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1495e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1496e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1497e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1498e705c121SKalle Valo 	}
1499e705c121SKalle Valo 
1500e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1501e705c121SKalle Valo 
1502e705c121SKalle Valo 	/*
1503e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1504e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1505e705c121SKalle Valo 	 */
1506e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1507e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1508e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1509e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1510e705c121SKalle Valo 			mvm->noa_vif = NULL;
1511e705c121SKalle Valo 			mvm->noa_duration = 0;
1512e705c121SKalle Valo 		}
1513e705c121SKalle Valo #endif
151426d6c16bSSara Sharon 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
1515e705c121SKalle Valo 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1516e705c121SKalle Valo 		goto out_release;
1517e705c121SKalle Valo 	}
1518e705c121SKalle Valo 
1519e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1520e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1521d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1522e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1523e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1524e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
1525e705c121SKalle Valo 	}
1526e705c121SKalle Valo 
1527e705c121SKalle Valo 	if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1528e705c121SKalle Valo 		mvm->vif_count--;
1529e705c121SKalle Valo 
1530e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
1531e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1532e705c121SKalle Valo 
1533baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1534baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1535baf41bc3SShaul Triebitz 
1536e705c121SKalle Valo out_release:
1537e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1538e705c121SKalle Valo }
1539e705c121SKalle Valo 
1540e705c121SKalle Valo static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1541e705c121SKalle Valo {
1542e705c121SKalle Valo 	return 0;
1543e705c121SKalle Valo }
1544e705c121SKalle Valo 
1545e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1546e705c121SKalle Valo 	struct iwl_mvm *mvm;
1547e705c121SKalle Valo 	int port_id;
1548e705c121SKalle Valo };
1549e705c121SKalle Valo 
1550e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1551e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1552e705c121SKalle Valo {
1553e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1554e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1555e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
155697bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
155797bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
155897bce57bSLuca Coelho 		.flags = CMD_ASYNC,
155997bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
156097bce57bSLuca Coelho 	};
1561e705c121SKalle Valo 	int ret, len;
1562e705c121SKalle Valo 
1563e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1564e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1565e705c121SKalle Valo 		return;
1566e705c121SKalle Valo 
1567e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1568e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1569e705c121SKalle Valo 		return;
1570e705c121SKalle Valo 
1571e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1572e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1573e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1574e705c121SKalle Valo 
157597bce57bSLuca Coelho 	hcmd.len[0] = len;
157697bce57bSLuca Coelho 	hcmd.data[0] = cmd;
157797bce57bSLuca Coelho 
157897bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1579e705c121SKalle Valo 	if (ret)
1580e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1581e705c121SKalle Valo }
1582e705c121SKalle Valo 
1583e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1584e705c121SKalle Valo {
1585e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1586e705c121SKalle Valo 		.mvm = mvm,
1587e705c121SKalle Valo 	};
1588e705c121SKalle Valo 
1589e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1590e705c121SKalle Valo 
1591e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1592e705c121SKalle Valo 		return;
1593e705c121SKalle Valo 
1594e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1595e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1596e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1597e705c121SKalle Valo }
1598e705c121SKalle Valo 
1599e705c121SKalle Valo static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1600e705c121SKalle Valo 				     struct netdev_hw_addr_list *mc_list)
1601e705c121SKalle Valo {
1602e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1603e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1604e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1605e705c121SKalle Valo 	int addr_count;
1606e705c121SKalle Valo 	bool pass_all;
1607e705c121SKalle Valo 	int len;
1608e705c121SKalle Valo 
1609e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1610e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1611e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1612e705c121SKalle Valo 	if (pass_all)
1613e705c121SKalle Valo 		addr_count = 0;
1614e705c121SKalle Valo 
1615e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1616e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1617e705c121SKalle Valo 	if (!cmd)
1618e705c121SKalle Valo 		return 0;
1619e705c121SKalle Valo 
1620e705c121SKalle Valo 	if (pass_all) {
1621e705c121SKalle Valo 		cmd->pass_all = 1;
1622e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1623e705c121SKalle Valo 	}
1624e705c121SKalle Valo 
1625e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1626e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1627e705c121SKalle Valo 				   cmd->count, addr->addr);
1628e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1629e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1630e705c121SKalle Valo 		cmd->count++;
1631e705c121SKalle Valo 	}
1632e705c121SKalle Valo 
1633e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1634e705c121SKalle Valo }
1635e705c121SKalle Valo 
1636e705c121SKalle Valo static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1637e705c121SKalle Valo 				     unsigned int changed_flags,
1638e705c121SKalle Valo 				     unsigned int *total_flags,
1639e705c121SKalle Valo 				     u64 multicast)
1640e705c121SKalle Valo {
1641e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1642e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1643e705c121SKalle Valo 
1644e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1645e705c121SKalle Valo 
1646e705c121SKalle Valo 	/* replace previous configuration */
1647e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1648e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1649e705c121SKalle Valo 
1650e705c121SKalle Valo 	if (!cmd)
1651e705c121SKalle Valo 		goto out;
1652e705c121SKalle Valo 
165361e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
165461e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
165561e7d91bSLuca Coelho 
165661e7d91bSLuca Coelho 	if (cmd->pass_all)
165761e7d91bSLuca Coelho 		cmd->count = 0;
165861e7d91bSLuca Coelho 
1659e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1660e705c121SKalle Valo out:
1661e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1662e705c121SKalle Valo 	*total_flags = 0;
1663e705c121SKalle Valo }
1664e705c121SKalle Valo 
1665e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1666e705c121SKalle Valo 					struct ieee80211_vif *vif,
1667e705c121SKalle Valo 					unsigned int filter_flags,
1668e705c121SKalle Valo 					unsigned int changed_flags)
1669e705c121SKalle Valo {
1670e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1671e705c121SKalle Valo 
1672e705c121SKalle Valo 	/* We support only filter for probe requests */
1673e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1674e705c121SKalle Valo 		return;
1675e705c121SKalle Valo 
1676e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1677e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1678e705c121SKalle Valo 	    !vif->p2p)
1679e705c121SKalle Valo 		return;
1680e705c121SKalle Valo 
1681e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1682e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1683e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1684e705c121SKalle Valo }
1685e705c121SKalle Valo 
1686e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1687e705c121SKalle Valo struct iwl_bcast_iter_data {
1688e705c121SKalle Valo 	struct iwl_mvm *mvm;
1689e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd;
1690e705c121SKalle Valo 	u8 current_filter;
1691e705c121SKalle Valo };
1692e705c121SKalle Valo 
1693e705c121SKalle Valo static void
1694e705c121SKalle Valo iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1695e705c121SKalle Valo 			 const struct iwl_fw_bcast_filter *in_filter,
1696e705c121SKalle Valo 			 struct iwl_fw_bcast_filter *out_filter)
1697e705c121SKalle Valo {
1698e705c121SKalle Valo 	struct iwl_fw_bcast_filter_attr *attr;
1699e705c121SKalle Valo 	int i;
1700e705c121SKalle Valo 
1701e705c121SKalle Valo 	memcpy(out_filter, in_filter, sizeof(*out_filter));
1702e705c121SKalle Valo 
1703e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1704e705c121SKalle Valo 		attr = &out_filter->attrs[i];
1705e705c121SKalle Valo 
1706e705c121SKalle Valo 		if (!attr->mask)
1707e705c121SKalle Valo 			break;
1708e705c121SKalle Valo 
1709e705c121SKalle Valo 		switch (attr->reserved1) {
1710e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_IP):
1711e705c121SKalle Valo 			if (vif->bss_conf.arp_addr_cnt != 1) {
1712e705c121SKalle Valo 				attr->mask = 0;
1713e705c121SKalle Valo 				continue;
1714e705c121SKalle Valo 			}
1715e705c121SKalle Valo 
1716e705c121SKalle Valo 			attr->val = vif->bss_conf.arp_addr_list[0];
1717e705c121SKalle Valo 			break;
1718e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1719e705c121SKalle Valo 			attr->val = *(__be32 *)&vif->addr[2];
1720e705c121SKalle Valo 			break;
1721e705c121SKalle Valo 		default:
1722e705c121SKalle Valo 			break;
1723e705c121SKalle Valo 		}
1724e705c121SKalle Valo 		attr->reserved1 = 0;
1725e705c121SKalle Valo 		out_filter->num_attrs++;
1726e705c121SKalle Valo 	}
1727e705c121SKalle Valo }
1728e705c121SKalle Valo 
1729e705c121SKalle Valo static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1730e705c121SKalle Valo 					  struct ieee80211_vif *vif)
1731e705c121SKalle Valo {
1732e705c121SKalle Valo 	struct iwl_bcast_iter_data *data = _data;
1733e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1734e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd = data->cmd;
1735e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1736e705c121SKalle Valo 	struct iwl_fw_bcast_mac *bcast_mac;
1737e705c121SKalle Valo 	int i;
1738e705c121SKalle Valo 
1739e705c121SKalle Valo 	if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1740e705c121SKalle Valo 		return;
1741e705c121SKalle Valo 
1742e705c121SKalle Valo 	bcast_mac = &cmd->macs[mvmvif->id];
1743e705c121SKalle Valo 
1744e705c121SKalle Valo 	/*
1745e705c121SKalle Valo 	 * enable filtering only for associated stations, but not for P2P
1746e705c121SKalle Valo 	 * Clients
1747e705c121SKalle Valo 	 */
1748e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1749e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1750e705c121SKalle Valo 		return;
1751e705c121SKalle Valo 
1752e705c121SKalle Valo 	bcast_mac->default_discard = 1;
1753e705c121SKalle Valo 
1754e705c121SKalle Valo 	/* copy all configured filters */
1755e705c121SKalle Valo 	for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1756e705c121SKalle Valo 		/*
1757e705c121SKalle Valo 		 * Make sure we don't exceed our filters limit.
1758e705c121SKalle Valo 		 * if there is still a valid filter to be configured,
1759e705c121SKalle Valo 		 * be on the safe side and just allow bcast for this mac.
1760e705c121SKalle Valo 		 */
1761e705c121SKalle Valo 		if (WARN_ON_ONCE(data->current_filter >=
1762e705c121SKalle Valo 				 ARRAY_SIZE(cmd->filters))) {
1763e705c121SKalle Valo 			bcast_mac->default_discard = 0;
1764e705c121SKalle Valo 			bcast_mac->attached_filters = 0;
1765e705c121SKalle Valo 			break;
1766e705c121SKalle Valo 		}
1767e705c121SKalle Valo 
1768e705c121SKalle Valo 		iwl_mvm_set_bcast_filter(vif,
1769e705c121SKalle Valo 					 &mvm->bcast_filters[i],
1770e705c121SKalle Valo 					 &cmd->filters[data->current_filter]);
1771e705c121SKalle Valo 
1772e705c121SKalle Valo 		/* skip current filter if it contains no attributes */
1773e705c121SKalle Valo 		if (!cmd->filters[data->current_filter].num_attrs)
1774e705c121SKalle Valo 			continue;
1775e705c121SKalle Valo 
1776e705c121SKalle Valo 		/* attach the filter to current mac */
1777e705c121SKalle Valo 		bcast_mac->attached_filters |=
1778e705c121SKalle Valo 				cpu_to_le16(BIT(data->current_filter));
1779e705c121SKalle Valo 
1780e705c121SKalle Valo 		data->current_filter++;
1781e705c121SKalle Valo 	}
1782e705c121SKalle Valo }
1783e705c121SKalle Valo 
1784e705c121SKalle Valo bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1785e705c121SKalle Valo 				    struct iwl_bcast_filter_cmd *cmd)
1786e705c121SKalle Valo {
1787e705c121SKalle Valo 	struct iwl_bcast_iter_data iter_data = {
1788e705c121SKalle Valo 		.mvm = mvm,
1789e705c121SKalle Valo 		.cmd = cmd,
1790e705c121SKalle Valo 	};
1791e705c121SKalle Valo 
1792e705c121SKalle Valo 	if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1793e705c121SKalle Valo 		return false;
1794e705c121SKalle Valo 
1795e705c121SKalle Valo 	memset(cmd, 0, sizeof(*cmd));
1796e705c121SKalle Valo 	cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1797e705c121SKalle Valo 	cmd->max_macs = ARRAY_SIZE(cmd->macs);
1798e705c121SKalle Valo 
1799e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
1800e705c121SKalle Valo 	/* use debugfs filters/macs if override is configured */
1801e705c121SKalle Valo 	if (mvm->dbgfs_bcast_filtering.override) {
1802e705c121SKalle Valo 		memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1803e705c121SKalle Valo 		       sizeof(cmd->filters));
1804e705c121SKalle Valo 		memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1805e705c121SKalle Valo 		       sizeof(cmd->macs));
1806e705c121SKalle Valo 		return true;
1807e705c121SKalle Valo 	}
1808e705c121SKalle Valo #endif
1809e705c121SKalle Valo 
1810e705c121SKalle Valo 	/* if no filters are configured, do nothing */
1811e705c121SKalle Valo 	if (!mvm->bcast_filters)
1812e705c121SKalle Valo 		return false;
1813e705c121SKalle Valo 
1814e705c121SKalle Valo 	/* configure and attach these filters for each associated sta vif */
1815e705c121SKalle Valo 	ieee80211_iterate_active_interfaces(
1816e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1817e705c121SKalle Valo 		iwl_mvm_bcast_filter_iterator, &iter_data);
1818e705c121SKalle Valo 
1819e705c121SKalle Valo 	return true;
1820e705c121SKalle Valo }
182134672bb3SEliad Peller 
182234672bb3SEliad Peller static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1823e705c121SKalle Valo {
1824e705c121SKalle Valo 	struct iwl_bcast_filter_cmd cmd;
1825e705c121SKalle Valo 
1826e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1827e705c121SKalle Valo 		return 0;
1828e705c121SKalle Valo 
1829e705c121SKalle Valo 	if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1830e705c121SKalle Valo 		return 0;
1831e705c121SKalle Valo 
1832e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1833e705c121SKalle Valo 				    sizeof(cmd), &cmd);
1834e705c121SKalle Valo }
1835e705c121SKalle Valo #else
183634672bb3SEliad Peller static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1837e705c121SKalle Valo {
1838e705c121SKalle Valo 	return 0;
1839e705c121SKalle Valo }
1840e705c121SKalle Valo #endif
1841e705c121SKalle Valo 
1842a07a8f37SSara Sharon static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
1843a07a8f37SSara Sharon 				    struct ieee80211_vif *vif)
1844a07a8f37SSara Sharon {
1845a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1846a07a8f37SSara Sharon 
1847a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1848a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1849a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1850a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1851a07a8f37SSara Sharon 
1852a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1853a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1854a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1855a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1856a07a8f37SSara Sharon }
1857a07a8f37SSara Sharon 
1858f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1859f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1860f92659a1SSara Sharon {
1861f92659a1SSara Sharon 	if (vif->mu_mimo_owner) {
1862f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1863f92659a1SSara Sharon 
1864f92659a1SSara Sharon 		/*
1865f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1866f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1867f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
1868f92659a1SSara Sharon 		 */
1869f92659a1SSara Sharon 		ieee80211_update_mu_groups(vif,
1870f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
1871f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
1872f92659a1SSara Sharon 	}
1873f92659a1SSara Sharon }
1874f92659a1SSara Sharon 
1875f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1876f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
1877f92659a1SSara Sharon {
1878f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1879f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1880f92659a1SSara Sharon 
1881f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
1882f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1883f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
1884f92659a1SSara Sharon }
1885f92659a1SSara Sharon 
1886e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
1887e705c121SKalle Valo 					     struct ieee80211_vif *vif,
1888e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
1889e705c121SKalle Valo 					     u32 changes)
1890e705c121SKalle Valo {
1891e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1892e705c121SKalle Valo 	int ret;
1893e705c121SKalle Valo 
1894e705c121SKalle Valo 	/*
1895e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
1896e705c121SKalle Valo 	 * beacon interval, which was not known when the station interface was
1897e705c121SKalle Valo 	 * added.
1898e705c121SKalle Valo 	 */
1899e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
1900e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1901e705c121SKalle Valo 
1902e705c121SKalle Valo 	/*
1903e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
1904e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
1905e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
1906e705c121SKalle Valo 	 * branch for disassociation below.
1907e705c121SKalle Valo 	 */
1908e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
1909e705c121SKalle Valo 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1910e705c121SKalle Valo 
1911e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
1912e705c121SKalle Valo 	if (ret)
1913e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1914e705c121SKalle Valo 
1915e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
1916e705c121SKalle Valo 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1917e705c121SKalle Valo 	mvmvif->associated = bss_conf->assoc;
1918e705c121SKalle Valo 
1919e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
1920e705c121SKalle Valo 		if (bss_conf->assoc) {
1921e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
1922e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
1923e705c121SKalle Valo 			memset(&mvmvif->beacon_stats, 0,
1924e705c121SKalle Valo 			       sizeof(mvmvif->beacon_stats));
1925e705c121SKalle Valo 
1926e705c121SKalle Valo 			/* add quota for this interface */
1927e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
1928e705c121SKalle Valo 			if (ret) {
1929e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
1930e705c121SKalle Valo 				return;
1931e705c121SKalle Valo 			}
1932e705c121SKalle Valo 
1933e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1934e705c121SKalle Valo 				     &mvm->status)) {
1935e705c121SKalle Valo 				/*
1936e705c121SKalle Valo 				 * If we're restarting then the firmware will
1937e705c121SKalle Valo 				 * obviously have lost synchronisation with
1938e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
1939e705c121SKalle Valo 				 * itself, but we can make it more reliable by
1940e705c121SKalle Valo 				 * scheduling a session protection time event.
1941e705c121SKalle Valo 				 *
1942e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
1943e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
1944e705c121SKalle Valo 				 * the beacon interval.
1945e705c121SKalle Valo 				 *
1946e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
1947e705c121SKalle Valo 				 * than a single interface.
1948e705c121SKalle Valo 				 */
1949e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
1950e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
1951e705c121SKalle Valo 							5 * dur, false);
1952e705c121SKalle Valo 			}
1953e705c121SKalle Valo 
1954e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
1955e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
1956e705c121SKalle Valo 			if (vif->p2p) {
1957e705c121SKalle Valo 				iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
1958e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
1959e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
1960e705c121SKalle Valo 						    IEEE80211_SMPS_DYNAMIC);
1961e705c121SKalle Valo 			}
19620ae98812SSara Sharon 		} else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
1963e705c121SKalle Valo 			/*
1964e705c121SKalle Valo 			 * If update fails - SF might be running in associated
1965e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
1966e705c121SKalle Valo 			 */
1967e705c121SKalle Valo 			WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
1968e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
1969e705c121SKalle Valo 
19706b28f978SEmmanuel Grumbach 			/*
19716b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
19726b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
19736b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
19746b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
19756b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
19766b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
19776b28f978SEmmanuel Grumbach 			 */
19786b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
19796b28f978SEmmanuel Grumbach 				      &mvm->status)) {
19806b28f978SEmmanuel Grumbach 				/*
19816b28f978SEmmanuel Grumbach 				 * Remove AP station now that
19826b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
19836b28f978SEmmanuel Grumbach 				 */
19846b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
19856b28f978SEmmanuel Grumbach 							mvmvif->ap_sta_id);
1986e705c121SKalle Valo 				if (ret)
19876b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
19886b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
1989e705c121SKalle Valo 
1990e705c121SKalle Valo 				if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
19916b28f978SEmmanuel Grumbach 					mvm->d0i3_ap_sta_id =
19926b28f978SEmmanuel Grumbach 						IWL_MVM_INVALID_STA;
19930ae98812SSara Sharon 				mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
19946b28f978SEmmanuel Grumbach 			}
19956b28f978SEmmanuel Grumbach 
1996e705c121SKalle Valo 			/* remove quota for this interface */
1997e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
1998e705c121SKalle Valo 			if (ret)
1999e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2000e705c121SKalle Valo 
2001e705c121SKalle Valo 			if (vif->p2p)
2002e705c121SKalle Valo 				iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
2003e705c121SKalle Valo 
2004e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2005e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2006e705c121SKalle Valo 			if (ret)
2007e705c121SKalle Valo 				IWL_ERR(mvm,
2008e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2009e705c121SKalle Valo 					vif->addr);
2010e705c121SKalle Valo 		}
2011e705c121SKalle Valo 
2012a07a8f37SSara Sharon 		/*
2013a07a8f37SSara Sharon 		 * The firmware tracks the MU-MIMO group on its own.
2014f92659a1SSara Sharon 		 * However, on HW restart we should restore this data.
2015a07a8f37SSara Sharon 		 */
2016a07a8f37SSara Sharon 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2017f92659a1SSara Sharon 		    (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
2018a07a8f37SSara Sharon 			ret = iwl_mvm_update_mu_groups(mvm, vif);
2019a07a8f37SSara Sharon 			if (ret)
2020a07a8f37SSara Sharon 				IWL_ERR(mvm,
2021a07a8f37SSara Sharon 					"failed to update VHT MU_MIMO groups\n");
2022a07a8f37SSara Sharon 		}
2023a07a8f37SSara Sharon 
2024e705c121SKalle Valo 		iwl_mvm_recalc_multicast(mvm);
202534672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2026e705c121SKalle Valo 
2027e705c121SKalle Valo 		/* reset rssi values */
2028e705c121SKalle Valo 		mvmvif->bf_data.ave_beacon_signal = 0;
2029e705c121SKalle Valo 
2030e705c121SKalle Valo 		iwl_mvm_bt_coex_vif_change(mvm);
2031e705c121SKalle Valo 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2032e705c121SKalle Valo 				    IEEE80211_SMPS_AUTOMATIC);
2033355346baSAvraham Stern 		if (fw_has_capa(&mvm->fw->ucode_capa,
2034355346baSAvraham Stern 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2035355346baSAvraham Stern 			iwl_mvm_config_scan(mvm);
2036b45242c9SAvraham Stern 	}
2037b45242c9SAvraham Stern 
2038b45242c9SAvraham Stern 	if (changes & BSS_CHANGED_BEACON_INFO) {
2039e705c121SKalle Valo 		/*
2040b45242c9SAvraham Stern 		 * We received a beacon from the associated AP so
2041e705c121SKalle Valo 		 * remove the session protection.
2042e705c121SKalle Valo 		 */
20433edfb5f4SAvraham Stern 		iwl_mvm_stop_session_protection(mvm, vif);
2044e705c121SKalle Valo 
2045e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2046e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2047e705c121SKalle Valo 	}
2048e705c121SKalle Valo 
2049283115fbSAvri Altman 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2050283115fbSAvri Altman 		       /*
2051283115fbSAvri Altman 			* Send power command on every beacon change,
2052283115fbSAvri Altman 			* because we may have not enabled beacon abort yet.
2053283115fbSAvri Altman 			*/
2054283115fbSAvri Altman 		       BSS_CHANGED_BEACON_INFO)) {
2055e705c121SKalle Valo 		ret = iwl_mvm_power_update_mac(mvm);
2056e705c121SKalle Valo 		if (ret)
2057e705c121SKalle Valo 			IWL_ERR(mvm, "failed to update power mode\n");
2058e705c121SKalle Valo 	}
2059e705c121SKalle Valo 
2060e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2061e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2062e705c121SKalle Valo 				bss_conf->txpower);
2063e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2064e705c121SKalle Valo 	}
2065e705c121SKalle Valo 
2066e705c121SKalle Valo 	if (changes & BSS_CHANGED_CQM) {
2067e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2068e705c121SKalle Valo 		/* reset cqm events tracking */
2069e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = 0;
2070e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
2071e705c121SKalle Valo 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2072e705c121SKalle Valo 			if (ret)
2073e705c121SKalle Valo 				IWL_ERR(mvm,
2074e705c121SKalle Valo 					"failed to update CQM thresholds\n");
2075e705c121SKalle Valo 		}
2076e705c121SKalle Valo 	}
2077e705c121SKalle Valo 
2078e705c121SKalle Valo 	if (changes & BSS_CHANGED_ARP_FILTER) {
2079e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
208034672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2081e705c121SKalle Valo 	}
2082e705c121SKalle Valo }
2083e705c121SKalle Valo 
2084e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2085e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2086e705c121SKalle Valo {
2087e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2088e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2089e705c121SKalle Valo 	int ret;
2090e705c121SKalle Valo 
2091e705c121SKalle Valo 	/*
2092e705c121SKalle Valo 	 * iwl_mvm_mac_ctxt_add() might read directly from the device
2093e705c121SKalle Valo 	 * (the system time), so make sure it is available.
2094e705c121SKalle Valo 	 */
2095e705c121SKalle Valo 	ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
2096e705c121SKalle Valo 	if (ret)
2097e705c121SKalle Valo 		return ret;
2098e705c121SKalle Valo 
2099e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2100e705c121SKalle Valo 
2101e705c121SKalle Valo 	/* Send the beacon template */
2102e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2103e705c121SKalle Valo 	if (ret)
2104e705c121SKalle Valo 		goto out_unlock;
2105e705c121SKalle Valo 
2106e705c121SKalle Valo 	/*
2107e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
2108e705c121SKalle Valo 	 * beacon interval, which was not known when the AP interface was added.
2109e705c121SKalle Valo 	 */
2110e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2111e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2112e705c121SKalle Valo 
2113e705c121SKalle Valo 	mvmvif->ap_assoc_sta_count = 0;
2114e705c121SKalle Valo 
2115e705c121SKalle Valo 	/* Add the mac context */
2116e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2117e705c121SKalle Valo 	if (ret)
2118e705c121SKalle Valo 		goto out_unlock;
2119e705c121SKalle Valo 
2120e705c121SKalle Valo 	/* Perform the binding */
2121e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2122e705c121SKalle Valo 	if (ret)
2123e705c121SKalle Valo 		goto out_remove;
2124e705c121SKalle Valo 
2125ced19f26SSara Sharon 	ret = iwl_mvm_add_mcast_sta(mvm, vif);
2126ced19f26SSara Sharon 	if (ret)
2127ced19f26SSara Sharon 		goto out_unbind;
2128ced19f26SSara Sharon 
2129e705c121SKalle Valo 	/* Send the bcast station. At this stage the TBTT and DTIM time events
2130e705c121SKalle Valo 	 * are added and applied to the scheduler */
2131e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2132e705c121SKalle Valo 	if (ret)
2133ced19f26SSara Sharon 		goto out_rm_mcast;
213426d6c16bSSara Sharon 
2135e705c121SKalle Valo 	/* must be set before quota calculations */
2136e705c121SKalle Valo 	mvmvif->ap_ibss_active = true;
2137e705c121SKalle Valo 
2138e705c121SKalle Valo 	/* power updated needs to be done before quotas */
2139e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2140e705c121SKalle Valo 
2141e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2142e705c121SKalle Valo 	if (ret)
2143e705c121SKalle Valo 		goto out_quota_failed;
2144e705c121SKalle Valo 
2145e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2146e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2147e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2148e705c121SKalle Valo 
2149e705c121SKalle Valo 	iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
2150e705c121SKalle Valo 
2151e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2152e705c121SKalle Valo 
2153e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2154e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2155e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2156e705c121SKalle Valo 
2157e705c121SKalle Valo 	goto out_unlock;
2158e705c121SKalle Valo 
2159e705c121SKalle Valo out_quota_failed:
2160e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2161e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2162e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2163ced19f26SSara Sharon out_rm_mcast:
2164ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2165e705c121SKalle Valo out_unbind:
2166e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2167e705c121SKalle Valo out_remove:
2168e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2169e705c121SKalle Valo out_unlock:
2170e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2171e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
2172e705c121SKalle Valo 	return ret;
2173e705c121SKalle Valo }
2174e705c121SKalle Valo 
2175e705c121SKalle Valo static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2176e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2177e705c121SKalle Valo {
2178e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2179e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2180e705c121SKalle Valo 
2181e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
2182e705c121SKalle Valo 
2183e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2184e705c121SKalle Valo 
2185e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2186e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2187e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2188e705c121SKalle Valo 					  &mvmvif->time_event_data);
2189e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2190e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2191e705c121SKalle Valo 	}
2192e705c121SKalle Valo 
2193e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2194e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2195e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2196e705c121SKalle Valo 	}
2197e705c121SKalle Valo 
2198e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2199e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2200e705c121SKalle Valo 
2201e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2202e705c121SKalle Valo 
2203e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
2204e705c121SKalle Valo 
2205e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2206e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2207e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2208e705c121SKalle Valo 
2209e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2210ced19f26SSara Sharon 
2211ced19f26SSara Sharon 	/*
2212ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2213ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2214ced19f26SSara Sharon 	 * cause assert 0x2b00.
2215ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2216ced19f26SSara Sharon 	 * bcast station).
2217ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2218ced19f26SSara Sharon 	 */
2219ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
222026d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2221e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2222ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2223ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2224e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2225e705c121SKalle Valo 
2226e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2227e705c121SKalle Valo 
2228e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2229e705c121SKalle Valo 
2230e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2231e705c121SKalle Valo }
2232e705c121SKalle Valo 
2233e705c121SKalle Valo static void
2234e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2235e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2236e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
2237e705c121SKalle Valo 				 u32 changes)
2238e705c121SKalle Valo {
2239e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2240e705c121SKalle Valo 
2241e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
2242e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
2243e705c121SKalle Valo 		return;
2244e705c121SKalle Valo 
2245e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2246e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2247e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2248e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2249e705c121SKalle Valo 
2250e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
2251e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
2252e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2253e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
2254e705c121SKalle Valo 
2255e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2256e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2257e705c121SKalle Valo 				bss_conf->txpower);
2258e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2259e705c121SKalle Valo 	}
2260e705c121SKalle Valo }
2261e705c121SKalle Valo 
2262e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2263e705c121SKalle Valo 				     struct ieee80211_vif *vif,
2264e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
2265e705c121SKalle Valo 				     u32 changes)
2266e705c121SKalle Valo {
2267e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2268e705c121SKalle Valo 
2269e705c121SKalle Valo 	/*
2270e705c121SKalle Valo 	 * iwl_mvm_bss_info_changed_station() might call
2271e705c121SKalle Valo 	 * iwl_mvm_protect_session(), which reads directly from
2272e705c121SKalle Valo 	 * the device (the system time), so make sure it is available.
2273e705c121SKalle Valo 	 */
2274e705c121SKalle Valo 	if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
2275e705c121SKalle Valo 		return;
2276e705c121SKalle Valo 
2277e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2278e705c121SKalle Valo 
2279e705c121SKalle Valo 	if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2280e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2281e705c121SKalle Valo 
2282e705c121SKalle Valo 	switch (vif->type) {
2283e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2284e705c121SKalle Valo 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2285e705c121SKalle Valo 		break;
2286e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
2287e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
2288e705c121SKalle Valo 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2289e705c121SKalle Valo 		break;
229091b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
229191b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
229291b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
229391b08c2dSAviya Erenfeld 		break;
2294e705c121SKalle Valo 	default:
2295e705c121SKalle Valo 		/* shouldn't happen */
2296e705c121SKalle Valo 		WARN_ON_ONCE(1);
2297e705c121SKalle Valo 	}
2298e705c121SKalle Valo 
2299e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2300e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
2301e705c121SKalle Valo }
2302e705c121SKalle Valo 
2303e705c121SKalle Valo static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2304e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2305e705c121SKalle Valo 			       struct ieee80211_scan_request *hw_req)
2306e705c121SKalle Valo {
2307e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2308e705c121SKalle Valo 	int ret;
2309e705c121SKalle Valo 
2310e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
2311e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2312e705c121SKalle Valo 		return -EINVAL;
2313e705c121SKalle Valo 
2314e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2315e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2316e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2317e705c121SKalle Valo 
2318e705c121SKalle Valo 	return ret;
2319e705c121SKalle Valo }
2320e705c121SKalle Valo 
2321e705c121SKalle Valo static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2322e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2323e705c121SKalle Valo {
2324e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2325e705c121SKalle Valo 
2326e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2327e705c121SKalle Valo 
2328e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2329e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
2330e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
2331e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
2332e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
2333e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
2334e705c121SKalle Valo 	*/
2335e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2336e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2337e705c121SKalle Valo 
2338e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2339e705c121SKalle Valo }
2340e705c121SKalle Valo 
2341e705c121SKalle Valo static void
2342e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2343e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
2344e705c121SKalle Valo 				  int num_frames,
2345e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
2346e705c121SKalle Valo 				  bool more_data)
2347e705c121SKalle Valo {
2348e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2349e705c121SKalle Valo 
2350e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
2351e705c121SKalle Valo 
2352e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2353e705c121SKalle Valo 					  tids, more_data, false);
2354e705c121SKalle Valo }
2355e705c121SKalle Valo 
2356e705c121SKalle Valo static void
2357e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2358e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
2359e705c121SKalle Valo 				    int num_frames,
2360e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
2361e705c121SKalle Valo 				    bool more_data)
2362e705c121SKalle Valo {
2363e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2364e705c121SKalle Valo 
23659a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2366e705c121SKalle Valo 
2367e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2368e705c121SKalle Valo 					  tids, more_data, true);
2369e705c121SKalle Valo }
2370e705c121SKalle Valo 
237165e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2372e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
2373e705c121SKalle Valo 				     struct ieee80211_sta *sta)
2374e705c121SKalle Valo {
2375e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2376e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2377e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
2378e705c121SKalle Valo 	int tid;
2379e705c121SKalle Valo 
2380960f864bSJohannes Berg 	/*
2381960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
2382960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
2383960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
2384960f864bSJohannes Berg 	 */
2385960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2386960f864bSJohannes Berg 		return;
2387960f864bSJohannes Berg 
2388e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2389e705c121SKalle Valo 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
2390e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2391e705c121SKalle Valo 
23926862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
23931c17627bSSara Sharon 			continue;
23941c17627bSSara Sharon 
2395e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
2396e705c121SKalle Valo 
2397dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
2398e705c121SKalle Valo 			continue;
2399e705c121SKalle Valo 
2400e705c121SKalle Valo 		__set_bit(tid, &tids);
2401e705c121SKalle Valo 	}
2402e705c121SKalle Valo 
2403e705c121SKalle Valo 	switch (cmd) {
2404e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
2405e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
2406e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
2407e705c121SKalle Valo 
2408e705c121SKalle Valo 		if (txqs)
2409e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
2410e705c121SKalle Valo 		/*
2411e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
2412e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
2413e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2414e705c121SKalle Valo 		 */
2415e705c121SKalle Valo 		break;
2416e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
24170ae98812SSara Sharon 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
2418e705c121SKalle Valo 			break;
2419e705c121SKalle Valo 
2420e705c121SKalle Valo 		if (txqs)
2421e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
2422e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
2423e705c121SKalle Valo 		break;
2424e705c121SKalle Valo 	default:
2425e705c121SKalle Valo 		break;
2426e705c121SKalle Valo 	}
2427e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2428e705c121SKalle Valo }
2429e705c121SKalle Valo 
243065e25482SJohannes Berg static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
243165e25482SJohannes Berg 				   struct ieee80211_vif *vif,
243265e25482SJohannes Berg 				   enum sta_notify_cmd cmd,
243365e25482SJohannes Berg 				   struct ieee80211_sta *sta)
243465e25482SJohannes Berg {
243565e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
243665e25482SJohannes Berg }
243765e25482SJohannes Berg 
243865e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
243965e25482SJohannes Berg {
244065e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
244165e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
244265e25482SJohannes Berg 	struct ieee80211_sta *sta;
244365e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
244465e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
244565e25482SJohannes Berg 
244665e25482SJohannes Berg 	if (WARN_ON(notif->sta_id >= ARRAY_SIZE(mvm->fw_id_to_mac_id)))
244765e25482SJohannes Berg 		return;
244865e25482SJohannes Berg 
244965e25482SJohannes Berg 	rcu_read_lock();
2450a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
245165e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
245265e25482SJohannes Berg 		rcu_read_unlock();
245365e25482SJohannes Berg 		return;
245465e25482SJohannes Berg 	}
245565e25482SJohannes Berg 
245665e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
245765e25482SJohannes Berg 
245865e25482SJohannes Berg 	if (!mvmsta->vif ||
245965e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
246065e25482SJohannes Berg 		rcu_read_unlock();
246165e25482SJohannes Berg 		return;
246265e25482SJohannes Berg 	}
246365e25482SJohannes Berg 
246465e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
246565e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
246665e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
246765e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
246865e25482SJohannes Berg 			sta);
246965e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
247065e25482SJohannes Berg 	}
247165e25482SJohannes Berg 
247265e25482SJohannes Berg 	if (sleeping) {
247365e25482SJohannes Berg 		switch (notif->type) {
247465e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
247565e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
247665e25482SJohannes Berg 			break;
247765e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
247865e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
247965e25482SJohannes Berg 			break;
248065e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
248165e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
248265e25482SJohannes Berg 			break;
248365e25482SJohannes Berg 		default:
248465e25482SJohannes Berg 			break;
248565e25482SJohannes Berg 		}
248665e25482SJohannes Berg 	}
248765e25482SJohannes Berg 
248865e25482SJohannes Berg 	rcu_read_unlock();
248965e25482SJohannes Berg }
249065e25482SJohannes Berg 
2491e705c121SKalle Valo static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2492e705c121SKalle Valo 				       struct ieee80211_vif *vif,
2493e705c121SKalle Valo 				       struct ieee80211_sta *sta)
2494e705c121SKalle Valo {
2495e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2496e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2497e705c121SKalle Valo 
2498e705c121SKalle Valo 	/*
2499e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
2500e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
2501e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
2502e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
2503e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
2504e705c121SKalle Valo 	 * callback deleted the station.
2505e705c121SKalle Valo 	 */
2506e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2507e705c121SKalle Valo 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2508e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2509e705c121SKalle Valo 				   ERR_PTR(-ENOENT));
2510e705c121SKalle Valo 
2511e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2512e705c121SKalle Valo }
2513e705c121SKalle Valo 
2514e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2515e705c121SKalle Valo 				const u8 *bssid)
2516e705c121SKalle Valo {
2517e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2518e705c121SKalle Valo 		return;
2519e705c121SKalle Valo 
2520c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
2521cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2522cee5a882SAvri Altman 		return;
2523cee5a882SAvri Altman 	}
2524cee5a882SAvri Altman 
252511dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
252611dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
2527e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2528e705c121SKalle Valo 		return;
2529e705c121SKalle Valo 	}
2530e705c121SKalle Valo 
2531e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2532e705c121SKalle Valo }
2533e705c121SKalle Valo 
25341e8f1329SGolan Ben-Ami static void
25351e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
25361e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
25371e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
25381e8f1329SGolan Ben-Ami {
25391e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
25401e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
25411e8f1329SGolan Ben-Ami 
25421e8f1329SGolan Ben-Ami 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TDLS))
25431e8f1329SGolan Ben-Ami 		return;
25441e8f1329SGolan Ben-Ami 
25451e8f1329SGolan Ben-Ami 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TDLS);
25461e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
25477174beb6SJohannes Berg 	if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
25487174beb6SJohannes Berg 					   ieee80211_vif_to_wdev(vif), trig))
25491e8f1329SGolan Ben-Ami 		return;
25501e8f1329SGolan Ben-Ami 
25511e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
25521e8f1329SGolan Ben-Ami 		return;
25531e8f1329SGolan Ben-Ami 
25541e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
25551e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
25561e8f1329SGolan Ben-Ami 		return;
25571e8f1329SGolan Ben-Ami 
25587174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
25591e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
25601e8f1329SGolan Ben-Ami 				peer_addr, action);
25611e8f1329SGolan Ben-Ami }
25621e8f1329SGolan Ben-Ami 
256324afba76SLiad Kaufman static void iwl_mvm_purge_deferred_tx_frames(struct iwl_mvm *mvm,
256424afba76SLiad Kaufman 					     struct iwl_mvm_sta *mvm_sta)
256524afba76SLiad Kaufman {
256624afba76SLiad Kaufman 	struct iwl_mvm_tid_data *tid_data;
256724afba76SLiad Kaufman 	struct sk_buff *skb;
256824afba76SLiad Kaufman 	int i;
256924afba76SLiad Kaufman 
257024afba76SLiad Kaufman 	spin_lock_bh(&mvm_sta->lock);
257124afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
257224afba76SLiad Kaufman 		tid_data = &mvm_sta->tid_data[i];
25737e39a00dSAvraham Stern 
25747e39a00dSAvraham Stern 		while ((skb = __skb_dequeue(&tid_data->deferred_tx_frames))) {
25757e39a00dSAvraham Stern 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
25767e39a00dSAvraham Stern 
25777e39a00dSAvraham Stern 			/*
25787e39a00dSAvraham Stern 			 * The first deferred frame should've stopped the MAC
25797e39a00dSAvraham Stern 			 * queues, so we should never get a second deferred
25807e39a00dSAvraham Stern 			 * frame for the RA/TID.
25817e39a00dSAvraham Stern 			 */
25828458e48aSAvraham Stern 			iwl_mvm_start_mac_queues(mvm, BIT(info->hw_queue));
258324afba76SLiad Kaufman 			ieee80211_free_txskb(mvm->hw, skb);
258424afba76SLiad Kaufman 		}
25857e39a00dSAvraham Stern 	}
258624afba76SLiad Kaufman 	spin_unlock_bh(&mvm_sta->lock);
258724afba76SLiad Kaufman }
258824afba76SLiad Kaufman 
2589e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
2590e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2591e705c121SKalle Valo 				 struct ieee80211_sta *sta,
2592e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
2593e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
2594e705c121SKalle Valo {
2595e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2596e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
25976ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2598e705c121SKalle Valo 	int ret;
2599e705c121SKalle Valo 
2600e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
2601e705c121SKalle Valo 			   sta->addr, old_state, new_state);
2602e705c121SKalle Valo 
2603e705c121SKalle Valo 	/* this would be a mac80211 bug ... but don't crash */
2604e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
2605e705c121SKalle Valo 		return -EINVAL;
2606e705c121SKalle Valo 
260724afba76SLiad Kaufman 	/*
260824afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
260924afba76SLiad Kaufman 	 *
261024afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
261124afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
261224afba76SLiad Kaufman 	 * the queues.
261324afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
261424afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
261524afba76SLiad Kaufman 	 * of.
261624afba76SLiad Kaufman 	 *
261724afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
261824afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
261924afba76SLiad Kaufman 	 */
262024afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
2621c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
262224afba76SLiad Kaufman 		iwl_mvm_purge_deferred_tx_frames(mvm, mvm_sta);
262324afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
262424afba76SLiad Kaufman 
262524afba76SLiad Kaufman 		/*
262624afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
262724afba76SLiad Kaufman 		 * worker will already remove it if it was on
262824afba76SLiad Kaufman 		 */
262924afba76SLiad Kaufman 	}
263024afba76SLiad Kaufman 
2631e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
26326ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
26336ea29ce5SJohannes Berg 	mvm_sta->associated = new_state >= IEEE80211_STA_ASSOC;
26346ea29ce5SJohannes Berg 
2635e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
2636e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
2637e705c121SKalle Valo 		/*
2638e705c121SKalle Valo 		 * Firmware bug - it'll crash if the beacon interval is less
2639e705c121SKalle Valo 		 * than 16. We can't avoid connecting at all, so refuse the
2640e705c121SKalle Valo 		 * station state change, this will cause mac80211 to abandon
2641e705c121SKalle Valo 		 * attempts to connect to this AP, and eventually wpa_s will
2642e705c121SKalle Valo 		 * blacklist the AP...
2643e705c121SKalle Valo 		 */
2644e705c121SKalle Valo 		if (vif->type == NL80211_IFTYPE_STATION &&
2645e705c121SKalle Valo 		    vif->bss_conf.beacon_int < 16) {
2646e705c121SKalle Valo 			IWL_ERR(mvm,
2647e705c121SKalle Valo 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
2648e705c121SKalle Valo 				sta->addr, vif->bss_conf.beacon_int);
2649e705c121SKalle Valo 			ret = -EINVAL;
2650e705c121SKalle Valo 			goto out_unlock;
2651e705c121SKalle Valo 		}
2652e705c121SKalle Valo 
2653e705c121SKalle Valo 		if (sta->tdls &&
2654e705c121SKalle Valo 		    (vif->p2p ||
2655e705c121SKalle Valo 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
2656e705c121SKalle Valo 						IWL_MVM_TDLS_STA_COUNT ||
2657e705c121SKalle Valo 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
2658e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
2659e705c121SKalle Valo 			ret = -EBUSY;
2660e705c121SKalle Valo 			goto out_unlock;
2661e705c121SKalle Valo 		}
2662e705c121SKalle Valo 
2663e705c121SKalle Valo 		ret = iwl_mvm_add_sta(mvm, vif, sta);
26641e8f1329SGolan Ben-Ami 		if (sta->tdls && ret == 0) {
2665e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
26661e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
26671e8f1329SGolan Ben-Ami 						   NL80211_TDLS_SETUP);
26681e8f1329SGolan Ben-Ami 		}
2669e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
2670e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
2671e705c121SKalle Valo 		/*
2672e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
2673e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
2674e705c121SKalle Valo 		 */
2675e705c121SKalle Valo 		mvm->last_ebs_successful = true;
2676e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
2677e705c121SKalle Valo 		ret = 0;
2678e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
2679e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
26808be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
26818be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count++;
26828be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
26838be30c13SAyala Beker 		}
2684735a0045SGoodstein, Mordechay 
2685735a0045SGoodstein, Mordechay 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
2686e705c121SKalle Valo 				     true);
2687735a0045SGoodstein, Mordechay 		ret = iwl_mvm_update_sta(mvm, vif, sta);
2688e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
2689e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
2690e705c121SKalle Valo 
2691e705c121SKalle Valo 		/* we don't support TDLS during DCM */
2692e705c121SKalle Valo 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
2693e705c121SKalle Valo 			iwl_mvm_teardown_tdls_peers(mvm);
2694e705c121SKalle Valo 
26951e8f1329SGolan Ben-Ami 		if (sta->tdls)
26961e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
26971e8f1329SGolan Ben-Ami 						   NL80211_TDLS_ENABLE_LINK);
26981e8f1329SGolan Ben-Ami 
2699e705c121SKalle Valo 		/* enable beacon filtering */
2700e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2701e705c121SKalle Valo 		ret = 0;
2702e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
2703e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
2704e705c121SKalle Valo 		/* disable beacon filtering */
2705e705c121SKalle Valo 		WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
2706e705c121SKalle Valo 		ret = 0;
2707e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
2708e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
27098be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
27108be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
27118be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
27128be30c13SAyala Beker 		}
2713e705c121SKalle Valo 		ret = 0;
2714e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
2715e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
2716e705c121SKalle Valo 		ret = 0;
2717e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
2718e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
2719e705c121SKalle Valo 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
27201e8f1329SGolan Ben-Ami 		if (sta->tdls) {
2721e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
27221e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
27231e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
27241e8f1329SGolan Ben-Ami 		}
2725e705c121SKalle Valo 	} else {
2726e705c121SKalle Valo 		ret = -EIO;
2727e705c121SKalle Valo 	}
2728e705c121SKalle Valo  out_unlock:
2729e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2730e705c121SKalle Valo 
2731e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
2732e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
2733e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
2734e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
2735e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
2736e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
2737e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
2738e705c121SKalle Valo 	}
2739e705c121SKalle Valo 
2740e705c121SKalle Valo 	return ret;
2741e705c121SKalle Valo }
2742e705c121SKalle Valo 
2743e705c121SKalle Valo static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2744e705c121SKalle Valo {
2745e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2746e705c121SKalle Valo 
2747e705c121SKalle Valo 	mvm->rts_threshold = value;
2748e705c121SKalle Valo 
2749e705c121SKalle Valo 	return 0;
2750e705c121SKalle Valo }
2751e705c121SKalle Valo 
2752e705c121SKalle Valo static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
2753e705c121SKalle Valo 				  struct ieee80211_vif *vif,
2754e705c121SKalle Valo 				  struct ieee80211_sta *sta, u32 changed)
2755e705c121SKalle Valo {
2756e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2757e705c121SKalle Valo 
2758e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
2759e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
2760e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2761e705c121SKalle Valo }
2762e705c121SKalle Valo 
2763e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
2764e705c121SKalle Valo 			       struct ieee80211_vif *vif, u16 ac,
2765e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
2766e705c121SKalle Valo {
2767e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2768e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2769e705c121SKalle Valo 
2770e705c121SKalle Valo 	mvmvif->queue_params[ac] = *params;
2771e705c121SKalle Valo 
2772e705c121SKalle Valo 	/*
2773e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
2774e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
2775e705c121SKalle Valo 	 */
2776e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2777e705c121SKalle Valo 		int ret;
2778e705c121SKalle Valo 
2779e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
2780e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2781e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
2782e705c121SKalle Valo 		return ret;
2783e705c121SKalle Valo 	}
2784e705c121SKalle Valo 	return 0;
2785e705c121SKalle Valo }
2786e705c121SKalle Valo 
2787e705c121SKalle Valo static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
2788e705c121SKalle Valo 				      struct ieee80211_vif *vif)
2789e705c121SKalle Valo {
2790e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
27917c70fee5SSara Sharon 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
27927c70fee5SSara Sharon 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2793e705c121SKalle Valo 
2794e705c121SKalle Valo 	if (WARN_ON_ONCE(vif->bss_conf.assoc))
2795e705c121SKalle Valo 		return;
2796e705c121SKalle Valo 
2797e705c121SKalle Valo 	/*
2798e705c121SKalle Valo 	 * iwl_mvm_protect_session() reads directly from the device
2799e705c121SKalle Valo 	 * (the system time), so make sure it is available.
2800e705c121SKalle Valo 	 */
2801e705c121SKalle Valo 	if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
2802e705c121SKalle Valo 		return;
2803e705c121SKalle Valo 
2804e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2805e705c121SKalle Valo 	/* Try really hard to protect the session and hear a beacon */
2806e705c121SKalle Valo 	iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500, false);
2807e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2808e705c121SKalle Valo 
2809e705c121SKalle Valo 	iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
2810e705c121SKalle Valo }
2811e705c121SKalle Valo 
2812e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
2813e705c121SKalle Valo 					struct ieee80211_vif *vif,
2814e705c121SKalle Valo 					struct cfg80211_sched_scan_request *req,
2815e705c121SKalle Valo 					struct ieee80211_scan_ies *ies)
2816e705c121SKalle Valo {
2817e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2818e705c121SKalle Valo 
2819e705c121SKalle Valo 	int ret;
2820e705c121SKalle Valo 
2821e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2822e705c121SKalle Valo 
2823e705c121SKalle Valo 	if (!vif->bss_conf.idle) {
2824e705c121SKalle Valo 		ret = -EBUSY;
2825e705c121SKalle Valo 		goto out;
2826e705c121SKalle Valo 	}
2827e705c121SKalle Valo 
2828e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
2829e705c121SKalle Valo 
2830e705c121SKalle Valo out:
2831e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2832e705c121SKalle Valo 	return ret;
2833e705c121SKalle Valo }
2834e705c121SKalle Valo 
2835e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
2836e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2837e705c121SKalle Valo {
2838e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2839e705c121SKalle Valo 	int ret;
2840e705c121SKalle Valo 
2841e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2842e705c121SKalle Valo 
2843e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2844e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
2845e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
2846e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
2847e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
2848e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
2849e705c121SKalle Valo 	 * not running.
2850e705c121SKalle Valo 	*/
2851e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
2852e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
2853e705c121SKalle Valo 		return 0;
2854e705c121SKalle Valo 	}
2855e705c121SKalle Valo 
2856e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
2857e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2858e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
2859e705c121SKalle Valo 
2860e705c121SKalle Valo 	return ret;
2861e705c121SKalle Valo }
2862e705c121SKalle Valo 
2863e705c121SKalle Valo static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
2864e705c121SKalle Valo 			       enum set_key_cmd cmd,
2865e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2866e705c121SKalle Valo 			       struct ieee80211_sta *sta,
2867e705c121SKalle Valo 			       struct ieee80211_key_conf *key)
2868e705c121SKalle Valo {
2869e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2870f5e28eacSJohannes Berg 	struct iwl_mvm_sta *mvmsta;
2871f5e28eacSJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn;
2872f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
2873e705c121SKalle Valo 	int ret;
28744615fd15SEmmanuel Grumbach 	u8 key_offset;
2875e705c121SKalle Valo 
28763b37f4c9SJohannes Berg 	if (iwlwifi_mod_params.swcrypto) {
2877e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
2878e705c121SKalle Valo 		return -EOPNOTSUPP;
2879e705c121SKalle Valo 	}
2880e705c121SKalle Valo 
2881e705c121SKalle Valo 	switch (key->cipher) {
2882e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
2883e705c121SKalle Valo 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
28841ad4f639SEliad Peller 		key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2885e705c121SKalle Valo 		break;
2886e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
28872a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
28882a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
288985aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
2890e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2891e705c121SKalle Valo 		break;
2892e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
28938e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
28948e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2895e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
2896e705c121SKalle Valo 		break;
2897e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
2898e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
2899e705c121SKalle Valo 		/* For non-client mode, only use WEP keys for TX as we probably
2900e705c121SKalle Valo 		 * don't have a station yet anyway and would then have to keep
2901e705c121SKalle Valo 		 * track of the keys, linking them to each of the clients/peers
2902e705c121SKalle Valo 		 * as they appear. For now, don't do that, for performance WEP
2903e705c121SKalle Valo 		 * offload doesn't really matter much, but we need it for some
2904e705c121SKalle Valo 		 * other offload features in client mode.
2905e705c121SKalle Valo 		 */
2906e705c121SKalle Valo 		if (vif->type != NL80211_IFTYPE_STATION)
2907e705c121SKalle Valo 			return 0;
2908e705c121SKalle Valo 		break;
2909e705c121SKalle Valo 	default:
2910e705c121SKalle Valo 		/* currently FW supports only one optional cipher scheme */
2911e705c121SKalle Valo 		if (hw->n_cipher_schemes &&
2912e705c121SKalle Valo 		    hw->cipher_schemes->cipher == key->cipher)
2913e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2914e705c121SKalle Valo 		else
2915e705c121SKalle Valo 			return -EOPNOTSUPP;
2916e705c121SKalle Valo 	}
2917e705c121SKalle Valo 
2918e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2919e705c121SKalle Valo 
2920e705c121SKalle Valo 	switch (cmd) {
2921e705c121SKalle Valo 	case SET_KEY:
2922e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
2923e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
2924e705c121SKalle Valo 			/*
2925e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
2926e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
2927e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
29288e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
2929e705c121SKalle Valo 			 */
29308e160ab8SAyala Beker 			if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
29318e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
29328e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
293381279c49SJohannes Berg 				ret = -EOPNOTSUPP;
293481279c49SJohannes Berg 			else
2935e705c121SKalle Valo 				ret = 0;
293685aeb58cSDavid Spinadel 
293785aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
293885aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
293985aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
2940e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
2941e705c121SKalle Valo 				break;
2942e705c121SKalle Valo 			}
294385aeb58cSDavid Spinadel 		}
2944e705c121SKalle Valo 
2945e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
2946e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
2947e705c121SKalle Valo 		 */
2948e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2949e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
2950e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
2951e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
2952e705c121SKalle Valo 			ret = 0;
2953e705c121SKalle Valo 			break;
2954e705c121SKalle Valo 		}
2955e705c121SKalle Valo 
2956f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2957f5e28eacSJohannes Berg 		    sta && iwl_mvm_has_new_rx_api(mvm) &&
2958f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
2959f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
29602a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
29612a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
2962f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
2963f5e28eacSJohannes Berg 			int tid, q;
2964f5e28eacSJohannes Berg 
2965f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
2966f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
2967f5e28eacSJohannes Berg 			ptk_pn = kzalloc(sizeof(*ptk_pn) +
2968f5e28eacSJohannes Berg 					 mvm->trans->num_rx_queues *
2969f5e28eacSJohannes Berg 						sizeof(ptk_pn->q[0]),
2970f5e28eacSJohannes Berg 					 GFP_KERNEL);
2971f5e28eacSJohannes Berg 			if (!ptk_pn) {
2972f5e28eacSJohannes Berg 				ret = -ENOMEM;
2973f5e28eacSJohannes Berg 				break;
2974f5e28eacSJohannes Berg 			}
2975f5e28eacSJohannes Berg 
2976f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
2977f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
2978f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
2979f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
2980f5e28eacSJohannes Berg 					       seq.ccmp.pn,
2981f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
2982f5e28eacSJohannes Berg 			}
2983f5e28eacSJohannes Berg 
2984f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
2985f5e28eacSJohannes Berg 		}
2986f5e28eacSJohannes Berg 
29874615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
29884615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
29894615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
29904615fd15SEmmanuel Grumbach 		else
29914615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
29924615fd15SEmmanuel Grumbach 
2993e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
29944615fd15SEmmanuel Grumbach 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
2995e705c121SKalle Valo 		if (ret) {
2996e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
2997e705c121SKalle Valo 			/*
2998e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
2999e705c121SKalle Valo 			 * in the device for TX so still return 0
3000e705c121SKalle Valo 			 */
3001e705c121SKalle Valo 			key->hw_key_idx = STA_KEY_IDX_INVALID;
3002e705c121SKalle Valo 			ret = 0;
3003e705c121SKalle Valo 		}
3004e705c121SKalle Valo 
3005e705c121SKalle Valo 		break;
3006e705c121SKalle Valo 	case DISABLE_KEY:
3007e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3008e705c121SKalle Valo 			ret = 0;
3009e705c121SKalle Valo 			break;
3010e705c121SKalle Valo 		}
3011e705c121SKalle Valo 
3012f5e28eacSJohannes Berg 		if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3013f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3014f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
30152a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
30162a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3017f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3018f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
3019f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
3020f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
3021f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3022f5e28eacSJohannes Berg 			if (ptk_pn)
3023f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
3024f5e28eacSJohannes Berg 		}
3025f5e28eacSJohannes Berg 
3026e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3027e705c121SKalle Valo 		ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3028e705c121SKalle Valo 		break;
3029e705c121SKalle Valo 	default:
3030e705c121SKalle Valo 		ret = -EINVAL;
3031e705c121SKalle Valo 	}
3032e705c121SKalle Valo 
3033e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3034e705c121SKalle Valo 	return ret;
3035e705c121SKalle Valo }
3036e705c121SKalle Valo 
3037e705c121SKalle Valo static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3038e705c121SKalle Valo 					struct ieee80211_vif *vif,
3039e705c121SKalle Valo 					struct ieee80211_key_conf *keyconf,
3040e705c121SKalle Valo 					struct ieee80211_sta *sta,
3041e705c121SKalle Valo 					u32 iv32, u16 *phase1key)
3042e705c121SKalle Valo {
3043e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3044e705c121SKalle Valo 
3045e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3046e705c121SKalle Valo 		return;
3047e705c121SKalle Valo 
3048e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3049e705c121SKalle Valo }
3050e705c121SKalle Valo 
3051e705c121SKalle Valo 
3052e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3053e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
3054e705c121SKalle Valo {
3055e705c121SKalle Valo 	struct iwl_mvm *mvm =
3056e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3057e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
3058e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
3059e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
3060e705c121SKalle Valo 
3061e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3062e705c121SKalle Valo 		return true;
3063e705c121SKalle Valo 
3064e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3065e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3066e705c121SKalle Valo 		return true;
3067e705c121SKalle Valo 	}
3068e705c121SKalle Valo 
3069e705c121SKalle Valo 	resp = (void *)pkt->data;
3070e705c121SKalle Valo 
3071e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
3072e705c121SKalle Valo 		     "Aux ROC: Recieved response from ucode: status=%d uid=%d\n",
3073e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
3074e705c121SKalle Valo 
3075e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
3076e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3077e705c121SKalle Valo 		     te_data->uid);
3078e705c121SKalle Valo 
3079e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3080e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3081e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3082e705c121SKalle Valo 
3083e705c121SKalle Valo 	return true;
3084e705c121SKalle Valo }
3085e705c121SKalle Valo 
3086dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3087dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3088dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3089dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3090dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3091e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3092e705c121SKalle Valo 				    struct ieee80211_channel *channel,
3093e705c121SKalle Valo 				    struct ieee80211_vif *vif,
3094e705c121SKalle Valo 				    int duration)
3095e705c121SKalle Valo {
3096e705c121SKalle Valo 	int res, time_reg = DEVICE_SYSTEM_TIME_REG;
3097e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3098e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3099e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
3100e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
3101dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
3102dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
3103dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
3104e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
3105e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3106e705c121SKalle Valo 		.id_and_color =
3107e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3108e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3109e705c121SKalle Valo 		/* Set the channel info data */
311057fbcce3SJohannes Berg 		.channel_info.band = (channel->band == NL80211_BAND_2GHZ) ?
3111e705c121SKalle Valo 			PHY_BAND_24 : PHY_BAND_5,
3112e705c121SKalle Valo 		.channel_info.channel = channel->hw_value,
3113e705c121SKalle Valo 		.channel_info.width = PHY_VHT_CHANNEL_MODE20,
3114e705c121SKalle Valo 		/* Set the time and duration */
3115e705c121SKalle Valo 		.apply_time = cpu_to_le32(iwl_read_prph(mvm->trans, time_reg)),
3116e705c121SKalle Valo 	 };
3117e705c121SKalle Valo 
3118dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
3119dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
3120dc28e12fSMatti Gottlieb 
3121dc28e12fSMatti Gottlieb 	/*
3122dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
3123dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
3124dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
3125dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
3126dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
3127dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
3128dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
3129dc28e12fSMatti Gottlieb 	 */
3130dc28e12fSMatti Gottlieb 	if (vif->bss_conf.assoc) {
3131dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3132dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
3133dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
3134dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3135dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
3136dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
3137dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
3138dc28e12fSMatti Gottlieb 		}
3139dc28e12fSMatti Gottlieb 	}
3140dc28e12fSMatti Gottlieb 
3141dc28e12fSMatti Gottlieb 	aux_roc_req.duration = cpu_to_le32(req_dur);
3142dc28e12fSMatti Gottlieb 	aux_roc_req.apply_time_max_delay = cpu_to_le32(delay);
3143dc28e12fSMatti Gottlieb 
3144dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
3145dc28e12fSMatti Gottlieb 		     "ROC: Requesting to remain on channel %u for %ums (requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3146dc28e12fSMatti Gottlieb 		     channel->hw_value, req_dur, duration, delay,
3147dc28e12fSMatti Gottlieb 		     dtim_interval);
3148e705c121SKalle Valo 	/* Set the node address */
3149e705c121SKalle Valo 	memcpy(aux_roc_req.node_addr, vif->addr, ETH_ALEN);
3150e705c121SKalle Valo 
3151e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3152e705c121SKalle Valo 
3153e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3154e705c121SKalle Valo 
3155e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3156e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3157e705c121SKalle Valo 		return -EIO;
3158e705c121SKalle Valo 	}
3159e705c121SKalle Valo 
3160e705c121SKalle Valo 	te_data->vif = vif;
3161e705c121SKalle Valo 	te_data->duration = duration;
3162e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
3163e705c121SKalle Valo 
3164e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3165e705c121SKalle Valo 
3166e705c121SKalle Valo 	/*
3167e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
3168e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
3169e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
3170e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
3171e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
3172e705c121SKalle Valo 	 * time another notification (that the time event started)
3173e705c121SKalle Valo 	 * might already be processed unsuccessfully.
3174e705c121SKalle Valo 	 */
3175e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3176e705c121SKalle Valo 				   time_event_response,
3177e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
3178e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
3179e705c121SKalle Valo 
3180e705c121SKalle Valo 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, sizeof(aux_roc_req),
3181e705c121SKalle Valo 				   &aux_roc_req);
3182e705c121SKalle Valo 
3183e705c121SKalle Valo 	if (res) {
3184e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3185e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3186e705c121SKalle Valo 		goto out_clear_te;
3187e705c121SKalle Valo 	}
3188e705c121SKalle Valo 
3189e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
3190e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3191e705c121SKalle Valo 	/* should never fail */
3192e705c121SKalle Valo 	WARN_ON_ONCE(res);
3193e705c121SKalle Valo 
3194e705c121SKalle Valo 	if (res) {
3195e705c121SKalle Valo  out_clear_te:
3196e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
3197e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
3198e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3199e705c121SKalle Valo 	}
3200e705c121SKalle Valo 
3201e705c121SKalle Valo 	return res;
3202e705c121SKalle Valo }
3203e705c121SKalle Valo 
3204e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
3205e705c121SKalle Valo 		       struct ieee80211_vif *vif,
3206e705c121SKalle Valo 		       struct ieee80211_channel *channel,
3207e705c121SKalle Valo 		       int duration,
3208e705c121SKalle Valo 		       enum ieee80211_roc_type type)
3209e705c121SKalle Valo {
3210e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3211e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3212e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
3213e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
3214e705c121SKalle Valo 	int ret, i;
3215e705c121SKalle Valo 
3216e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3217e705c121SKalle Valo 			   duration, type);
3218e705c121SKalle Valo 
3219e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
3220e705c121SKalle Valo 
3221e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3222e705c121SKalle Valo 
3223e705c121SKalle Valo 	switch (vif->type) {
3224e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3225e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
3226e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
3227e705c121SKalle Valo 			/* Use aux roc framework (HS20) */
3228e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3229e705c121SKalle Valo 						       vif, duration);
3230e705c121SKalle Valo 			goto out_unlock;
3231e705c121SKalle Valo 		}
3232e705c121SKalle Valo 		IWL_ERR(mvm, "hotspot not supported\n");
3233e705c121SKalle Valo 		ret = -EINVAL;
3234e705c121SKalle Valo 		goto out_unlock;
3235e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
3236e705c121SKalle Valo 		/* handle below */
3237e705c121SKalle Valo 		break;
3238e705c121SKalle Valo 	default:
3239e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
3240e705c121SKalle Valo 		ret = -EINVAL;
3241e705c121SKalle Valo 		goto out_unlock;
3242e705c121SKalle Valo 	}
3243e705c121SKalle Valo 
3244e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
3245e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
3246e705c121SKalle Valo 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3247e705c121SKalle Valo 			continue;
3248e705c121SKalle Valo 
3249e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3250e705c121SKalle Valo 			/*
3251e705c121SKalle Valo 			 * Unbind the P2P_DEVICE from the current PHY context,
3252e705c121SKalle Valo 			 * and if the PHY context is not used remove it.
3253e705c121SKalle Valo 			 */
3254e705c121SKalle Valo 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
3255e705c121SKalle Valo 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3256e705c121SKalle Valo 				goto out_unlock;
3257e705c121SKalle Valo 
3258e705c121SKalle Valo 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3259e705c121SKalle Valo 
3260e705c121SKalle Valo 			/* Bind the P2P_DEVICE to the current PHY Context */
3261e705c121SKalle Valo 			mvmvif->phy_ctxt = phy_ctxt;
3262e705c121SKalle Valo 
3263e705c121SKalle Valo 			ret = iwl_mvm_binding_add_vif(mvm, vif);
3264e705c121SKalle Valo 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3265e705c121SKalle Valo 				goto out_unlock;
3266e705c121SKalle Valo 
3267e705c121SKalle Valo 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3268e705c121SKalle Valo 			goto schedule_time_event;
3269e705c121SKalle Valo 		}
3270e705c121SKalle Valo 	}
3271e705c121SKalle Valo 
3272e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
3273e705c121SKalle Valo 	if (channel == mvmvif->phy_ctxt->channel)
3274e705c121SKalle Valo 		goto schedule_time_event;
3275e705c121SKalle Valo 
3276e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
3277e705c121SKalle Valo 
3278e705c121SKalle Valo 	/*
3279e705c121SKalle Valo 	 * Change the PHY context configuration as it is currently referenced
3280e705c121SKalle Valo 	 * only by the P2P Device MAC
3281e705c121SKalle Valo 	 */
3282e705c121SKalle Valo 	if (mvmvif->phy_ctxt->ref == 1) {
3283e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3284e705c121SKalle Valo 					       &chandef, 1, 1);
3285e705c121SKalle Valo 		if (ret)
3286e705c121SKalle Valo 			goto out_unlock;
3287e705c121SKalle Valo 	} else {
3288e705c121SKalle Valo 		/*
3289e705c121SKalle Valo 		 * The PHY context is shared with other MACs. Need to remove the
3290e705c121SKalle Valo 		 * P2P Device from the binding, allocate an new PHY context and
3291e705c121SKalle Valo 		 * create a new binding
3292e705c121SKalle Valo 		 */
3293e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3294e705c121SKalle Valo 		if (!phy_ctxt) {
3295e705c121SKalle Valo 			ret = -ENOSPC;
3296e705c121SKalle Valo 			goto out_unlock;
3297e705c121SKalle Valo 		}
3298e705c121SKalle Valo 
3299e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3300e705c121SKalle Valo 					       1, 1);
3301e705c121SKalle Valo 		if (ret) {
3302e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
3303e705c121SKalle Valo 			goto out_unlock;
3304e705c121SKalle Valo 		}
3305e705c121SKalle Valo 
3306e705c121SKalle Valo 		/* Unbind the P2P_DEVICE from the current PHY context */
3307e705c121SKalle Valo 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
3308e705c121SKalle Valo 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3309e705c121SKalle Valo 			goto out_unlock;
3310e705c121SKalle Valo 
3311e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3312e705c121SKalle Valo 
3313e705c121SKalle Valo 		/* Bind the P2P_DEVICE to the new allocated PHY context */
3314e705c121SKalle Valo 		mvmvif->phy_ctxt = phy_ctxt;
3315e705c121SKalle Valo 
3316e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
3317e705c121SKalle Valo 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3318e705c121SKalle Valo 			goto out_unlock;
3319e705c121SKalle Valo 
3320e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3321e705c121SKalle Valo 	}
3322e705c121SKalle Valo 
3323e705c121SKalle Valo schedule_time_event:
3324e705c121SKalle Valo 	/* Schedule the time events */
3325e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
3326e705c121SKalle Valo 
3327e705c121SKalle Valo out_unlock:
3328e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3329e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3330e705c121SKalle Valo 	return ret;
3331e705c121SKalle Valo }
3332e705c121SKalle Valo 
3333e705c121SKalle Valo static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
3334e705c121SKalle Valo {
3335e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3336e705c121SKalle Valo 
3337e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
3338e705c121SKalle Valo 
3339e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3340e705c121SKalle Valo 	iwl_mvm_stop_roc(mvm);
3341e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3342e705c121SKalle Valo 
3343e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3344e705c121SKalle Valo 	return 0;
3345e705c121SKalle Valo }
3346e705c121SKalle Valo 
3347e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
3348e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
3349e705c121SKalle Valo {
3350e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3351e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
3352e705c121SKalle Valo 	int ret;
3353e705c121SKalle Valo 
3354e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3355e705c121SKalle Valo 
3356e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
3357e705c121SKalle Valo 
3358e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3359e705c121SKalle Valo 	if (!phy_ctxt) {
3360e705c121SKalle Valo 		ret = -ENOSPC;
3361e705c121SKalle Valo 		goto out;
3362e705c121SKalle Valo 	}
3363e705c121SKalle Valo 
3364e705c121SKalle Valo 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
3365e705c121SKalle Valo 				       ctx->rx_chains_static,
3366e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
3367e705c121SKalle Valo 	if (ret) {
3368e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
3369e705c121SKalle Valo 		goto out;
3370e705c121SKalle Valo 	}
3371e705c121SKalle Valo 
3372e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
3373e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
3374e705c121SKalle Valo out:
3375e705c121SKalle Valo 	return ret;
3376e705c121SKalle Valo }
3377e705c121SKalle Valo 
3378e705c121SKalle Valo static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
3379e705c121SKalle Valo 			       struct ieee80211_chanctx_conf *ctx)
3380e705c121SKalle Valo {
3381e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3382e705c121SKalle Valo 	int ret;
3383e705c121SKalle Valo 
3384e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3385e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
3386e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3387e705c121SKalle Valo 
3388e705c121SKalle Valo 	return ret;
3389e705c121SKalle Valo }
3390e705c121SKalle Valo 
3391e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
3392e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
3393e705c121SKalle Valo {
3394e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3395e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3396e705c121SKalle Valo 
3397e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3398e705c121SKalle Valo 
3399e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
3400e705c121SKalle Valo }
3401e705c121SKalle Valo 
3402e705c121SKalle Valo static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
3403e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx)
3404e705c121SKalle Valo {
3405e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3406e705c121SKalle Valo 
3407e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3408e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
3409e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3410e705c121SKalle Valo }
3411e705c121SKalle Valo 
3412e705c121SKalle Valo static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
3413e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx,
3414e705c121SKalle Valo 				   u32 changed)
3415e705c121SKalle Valo {
3416e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3417e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3418e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3419e705c121SKalle Valo 
3420e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
3421e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
3422e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
3423e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
3424e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
3425e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
3426e705c121SKalle Valo 		      phy_ctxt->ref, changed))
3427e705c121SKalle Valo 		return;
3428e705c121SKalle Valo 
3429e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
34307a20bcceSEmmanuel Grumbach 
34317a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
34327a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
34337a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width == ctx->min_def.width)
34347a20bcceSEmmanuel Grumbach 			goto out_unlock;
34357a20bcceSEmmanuel Grumbach 
34367a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
34377a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
34387a20bcceSEmmanuel Grumbach 		    ctx->min_def.width <= NL80211_CHAN_WIDTH_20)
34397a20bcceSEmmanuel Grumbach 			goto out_unlock;
34407a20bcceSEmmanuel Grumbach 	}
34417a20bcceSEmmanuel Grumbach 
3442e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
3443e705c121SKalle Valo 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
3444e705c121SKalle Valo 				 ctx->rx_chains_static,
3445e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
34467a20bcceSEmmanuel Grumbach 
34477a20bcceSEmmanuel Grumbach out_unlock:
3448e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3449e705c121SKalle Valo }
3450e705c121SKalle Valo 
3451e705c121SKalle Valo static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
3452e705c121SKalle Valo 					struct ieee80211_vif *vif,
3453e705c121SKalle Valo 					struct ieee80211_chanctx_conf *ctx,
3454e705c121SKalle Valo 					bool switching_chanctx)
3455e705c121SKalle Valo {
3456e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3457e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3458e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3459e705c121SKalle Valo 	int ret;
3460e705c121SKalle Valo 
3461e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3462e705c121SKalle Valo 
3463e705c121SKalle Valo 	mvmvif->phy_ctxt = phy_ctxt;
3464e705c121SKalle Valo 
3465e705c121SKalle Valo 	switch (vif->type) {
3466e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3467e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
3468e705c121SKalle Valo 		if (switching_chanctx) {
3469e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
3470e705c121SKalle Valo 			break;
3471e705c121SKalle Valo 		}
3472e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
3473e705c121SKalle Valo 		/*
3474e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
3475e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
3476e705c121SKalle Valo 		 */
3477e705c121SKalle Valo 		ret = 0;
3478e705c121SKalle Valo 		goto out;
3479e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3480e705c121SKalle Valo 		break;
3481e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
3482e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
3483e705c121SKalle Valo 		mvmvif->ps_disabled = true;
3484e705c121SKalle Valo 		break;
3485e705c121SKalle Valo 	default:
3486e705c121SKalle Valo 		ret = -EINVAL;
3487e705c121SKalle Valo 		goto out;
3488e705c121SKalle Valo 	}
3489e705c121SKalle Valo 
3490e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
3491e705c121SKalle Valo 	if (ret)
3492e705c121SKalle Valo 		goto out;
3493e705c121SKalle Valo 
3494e705c121SKalle Valo 	/*
3495e705c121SKalle Valo 	 * Power state must be updated before quotas,
3496e705c121SKalle Valo 	 * otherwise fw will complain.
3497e705c121SKalle Valo 	 */
3498e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3499e705c121SKalle Valo 
3500e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
3501e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
3502e705c121SKalle Valo 	 * will handle quota settings.
3503e705c121SKalle Valo 	 */
3504e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
3505e705c121SKalle Valo 		mvmvif->monitor_active = true;
3506e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
3507e705c121SKalle Valo 		if (ret)
3508e705c121SKalle Valo 			goto out_remove_binding;
35090e39eb03SChaya Rachel Ivgi 
35100e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
35110e39eb03SChaya Rachel Ivgi 		if (ret)
35120e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
35130e39eb03SChaya Rachel Ivgi 
3514e705c121SKalle Valo 	}
3515e705c121SKalle Valo 
3516e705c121SKalle Valo 	/* Handle binding during CSA */
3517e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
3518e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
3519e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3520e705c121SKalle Valo 	}
3521e705c121SKalle Valo 
3522e705c121SKalle Valo 	if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
3523e705c121SKalle Valo 		u32 duration = 2 * vif->bss_conf.beacon_int;
3524e705c121SKalle Valo 
3525e705c121SKalle Valo 		/* iwl_mvm_protect_session() reads directly from the
3526e705c121SKalle Valo 		 * device (the system time), so make sure it is
3527e705c121SKalle Valo 		 * available.
3528e705c121SKalle Valo 		 */
3529e705c121SKalle Valo 		ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_CSA);
3530e705c121SKalle Valo 		if (ret)
3531e705c121SKalle Valo 			goto out_remove_binding;
3532e705c121SKalle Valo 
3533e705c121SKalle Valo 		/* Protect the session to make sure we hear the first
3534e705c121SKalle Valo 		 * beacon on the new channel.
3535e705c121SKalle Valo 		 */
3536e705c121SKalle Valo 		iwl_mvm_protect_session(mvm, vif, duration, duration,
3537e705c121SKalle Valo 					vif->bss_conf.beacon_int / 2,
3538e705c121SKalle Valo 					true);
3539e705c121SKalle Valo 
3540e705c121SKalle Valo 		iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_CSA);
3541e705c121SKalle Valo 
3542e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
3543e705c121SKalle Valo 	}
3544e705c121SKalle Valo 
3545e705c121SKalle Valo 	goto out;
3546e705c121SKalle Valo 
3547e705c121SKalle Valo out_remove_binding:
3548e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
3549e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3550e705c121SKalle Valo out:
3551e705c121SKalle Valo 	if (ret)
3552e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
3553e705c121SKalle Valo 	return ret;
3554e705c121SKalle Valo }
3555e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
3556e705c121SKalle Valo 				      struct ieee80211_vif *vif,
3557e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
3558e705c121SKalle Valo {
3559e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3560e705c121SKalle Valo 	int ret;
3561e705c121SKalle Valo 
3562e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3563e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
3564e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3565e705c121SKalle Valo 
3566e705c121SKalle Valo 	return ret;
3567e705c121SKalle Valo }
3568e705c121SKalle Valo 
3569e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
3570e705c121SKalle Valo 					   struct ieee80211_vif *vif,
3571e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
3572e705c121SKalle Valo 					   bool switching_chanctx)
3573e705c121SKalle Valo {
3574e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3575e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
3576e705c121SKalle Valo 
3577e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3578e705c121SKalle Valo 
3579e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
3580e705c121SKalle Valo 
3581e705c121SKalle Valo 	switch (vif->type) {
3582e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
3583e705c121SKalle Valo 		goto out;
3584e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
3585e705c121SKalle Valo 		mvmvif->monitor_active = false;
3586e705c121SKalle Valo 		mvmvif->ps_disabled = false;
35870e39eb03SChaya Rachel Ivgi 		iwl_mvm_rm_snif_sta(mvm, vif);
3588e705c121SKalle Valo 		break;
3589e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3590e705c121SKalle Valo 		/* This part is triggered only during CSA */
3591e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
3592e705c121SKalle Valo 			goto out;
3593e705c121SKalle Valo 
3594e705c121SKalle Valo 		mvmvif->csa_countdown = false;
3595e705c121SKalle Valo 
3596e705c121SKalle Valo 		/* Set CS bit on all the stations */
3597e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
3598e705c121SKalle Valo 
3599e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
3600e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
3601e705c121SKalle Valo 
3602e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
3603e705c121SKalle Valo 		break;
3604e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3605e705c121SKalle Valo 		if (!switching_chanctx)
3606e705c121SKalle Valo 			break;
3607e705c121SKalle Valo 
3608e705c121SKalle Valo 		disabled_vif = vif;
3609e705c121SKalle Valo 
3610e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
3611e705c121SKalle Valo 		break;
3612e705c121SKalle Valo 	default:
3613e705c121SKalle Valo 		break;
3614e705c121SKalle Valo 	}
3615e705c121SKalle Valo 
3616e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
3617e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
3618e705c121SKalle Valo 
3619e705c121SKalle Valo out:
3620e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
3621e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3622e705c121SKalle Valo }
3623e705c121SKalle Valo 
3624e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
3625e705c121SKalle Valo 					 struct ieee80211_vif *vif,
3626e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
3627e705c121SKalle Valo {
3628e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3629e705c121SKalle Valo 
3630e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3631e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
3632e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3633e705c121SKalle Valo }
3634e705c121SKalle Valo 
3635e705c121SKalle Valo static int
3636e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
3637e705c121SKalle Valo 				struct ieee80211_vif_chanctx_switch *vifs)
3638e705c121SKalle Valo {
3639e705c121SKalle Valo 	int ret;
3640e705c121SKalle Valo 
3641e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3642e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
3643e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
3644e705c121SKalle Valo 
3645e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
3646e705c121SKalle Valo 	if (ret) {
3647e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
3648e705c121SKalle Valo 		goto out_reassign;
3649e705c121SKalle Valo 	}
3650e705c121SKalle Valo 
3651e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
3652e705c121SKalle Valo 					   true);
3653e705c121SKalle Valo 	if (ret) {
3654e705c121SKalle Valo 		IWL_ERR(mvm,
3655e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
3656e705c121SKalle Valo 		goto out_remove;
3657e705c121SKalle Valo 	}
3658e705c121SKalle Valo 
3659e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
3660e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3661e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
3662e705c121SKalle Valo 
3663e705c121SKalle Valo 	goto out;
3664e705c121SKalle Valo 
3665e705c121SKalle Valo out_remove:
3666e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
3667e705c121SKalle Valo 
3668e705c121SKalle Valo out_reassign:
3669e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
3670e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
3671e705c121SKalle Valo 		goto out_restart;
3672e705c121SKalle Valo 	}
3673e705c121SKalle Valo 
3674e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
3675e705c121SKalle Valo 					 true)) {
3676e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
3677e705c121SKalle Valo 		goto out_restart;
3678e705c121SKalle Valo 	}
3679e705c121SKalle Valo 
3680e705c121SKalle Valo 	goto out;
3681e705c121SKalle Valo 
3682e705c121SKalle Valo out_restart:
3683e705c121SKalle Valo 	/* things keep failing, better restart the hw */
3684e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
3685e705c121SKalle Valo 
3686e705c121SKalle Valo out:
3687e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3688e705c121SKalle Valo 
3689e705c121SKalle Valo 	return ret;
3690e705c121SKalle Valo }
3691e705c121SKalle Valo 
3692e705c121SKalle Valo static int
3693e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
3694e705c121SKalle Valo 				    struct ieee80211_vif_chanctx_switch *vifs)
3695e705c121SKalle Valo {
3696e705c121SKalle Valo 	int ret;
3697e705c121SKalle Valo 
3698e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3699e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
3700e705c121SKalle Valo 
3701e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
3702e705c121SKalle Valo 					   true);
3703e705c121SKalle Valo 	if (ret) {
3704e705c121SKalle Valo 		IWL_ERR(mvm,
3705e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
3706e705c121SKalle Valo 		goto out_reassign;
3707e705c121SKalle Valo 	}
3708e705c121SKalle Valo 
3709e705c121SKalle Valo 	goto out;
3710e705c121SKalle Valo 
3711e705c121SKalle Valo out_reassign:
3712e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
3713e705c121SKalle Valo 					 true)) {
3714e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
3715e705c121SKalle Valo 		goto out_restart;
3716e705c121SKalle Valo 	}
3717e705c121SKalle Valo 
3718e705c121SKalle Valo 	goto out;
3719e705c121SKalle Valo 
3720e705c121SKalle Valo out_restart:
3721e705c121SKalle Valo 	/* things keep failing, better restart the hw */
3722e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
3723e705c121SKalle Valo 
3724e705c121SKalle Valo out:
3725e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3726e705c121SKalle Valo 
3727e705c121SKalle Valo 	return ret;
3728e705c121SKalle Valo }
3729e705c121SKalle Valo 
3730e705c121SKalle Valo static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
3731e705c121SKalle Valo 				      struct ieee80211_vif_chanctx_switch *vifs,
3732e705c121SKalle Valo 				      int n_vifs,
3733e705c121SKalle Valo 				      enum ieee80211_chanctx_switch_mode mode)
3734e705c121SKalle Valo {
3735e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3736e705c121SKalle Valo 	int ret;
3737e705c121SKalle Valo 
3738e705c121SKalle Valo 	/* we only support a single-vif right now */
3739e705c121SKalle Valo 	if (n_vifs > 1)
3740e705c121SKalle Valo 		return -EOPNOTSUPP;
3741e705c121SKalle Valo 
3742e705c121SKalle Valo 	switch (mode) {
3743e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
3744e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
3745e705c121SKalle Valo 		break;
3746e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
3747e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
3748e705c121SKalle Valo 		break;
3749e705c121SKalle Valo 	default:
3750e705c121SKalle Valo 		ret = -EOPNOTSUPP;
3751e705c121SKalle Valo 		break;
3752e705c121SKalle Valo 	}
3753e705c121SKalle Valo 
3754e705c121SKalle Valo 	return ret;
3755e705c121SKalle Valo }
3756e705c121SKalle Valo 
37572f0282dbSJohannes Berg static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
37582f0282dbSJohannes Berg {
37592f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
37602f0282dbSJohannes Berg 
37612f0282dbSJohannes Berg 	return mvm->ibss_manager;
37622f0282dbSJohannes Berg }
37632f0282dbSJohannes Berg 
3764e705c121SKalle Valo static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
3765e705c121SKalle Valo 			   struct ieee80211_sta *sta,
3766e705c121SKalle Valo 			   bool set)
3767e705c121SKalle Valo {
3768e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3769e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3770e705c121SKalle Valo 
3771e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
3772e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
3773e705c121SKalle Valo 		return -EINVAL;
3774e705c121SKalle Valo 	}
3775e705c121SKalle Valo 
3776e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
3777e705c121SKalle Valo }
3778e705c121SKalle Valo 
3779e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
3780e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
3781e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
3782e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
3783e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
3784e705c121SKalle Valo };
3785e705c121SKalle Valo 
3786e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
3787e705c121SKalle Valo 				      struct ieee80211_vif *vif,
3788e705c121SKalle Valo 				      void *data, int len)
3789e705c121SKalle Valo {
3790e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
3791e705c121SKalle Valo 	int err;
3792e705c121SKalle Valo 	u32 noa_duration;
3793e705c121SKalle Valo 
3794fceb6435SJohannes Berg 	err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy,
3795fceb6435SJohannes Berg 			NULL);
3796e705c121SKalle Valo 	if (err)
3797e705c121SKalle Valo 		return err;
3798e705c121SKalle Valo 
3799e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
3800e705c121SKalle Valo 		return -EINVAL;
3801e705c121SKalle Valo 
3802e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
3803e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
3804e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
3805e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
3806e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
3807e705c121SKalle Valo 			return -EINVAL;
3808e705c121SKalle Valo 
3809e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
3810e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
3811e705c121SKalle Valo 			return -EINVAL;
3812e705c121SKalle Valo 
3813e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
3814e705c121SKalle Valo 		mvm->noa_vif = vif;
3815e705c121SKalle Valo 
3816e705c121SKalle Valo 		return iwl_mvm_update_quotas(mvm, false, NULL);
3817e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
3818e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
3819e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
3820e705c121SKalle Valo 		    !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
3821e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
3822e705c121SKalle Valo 			return -EINVAL;
3823e705c121SKalle Valo 
3824e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
3825e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
3826e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3827e705c121SKalle Valo 	}
3828e705c121SKalle Valo 
3829e705c121SKalle Valo 	return -EOPNOTSUPP;
3830e705c121SKalle Valo }
3831e705c121SKalle Valo 
3832e705c121SKalle Valo static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
3833e705c121SKalle Valo 				    struct ieee80211_vif *vif,
3834e705c121SKalle Valo 				    void *data, int len)
3835e705c121SKalle Valo {
3836e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3837e705c121SKalle Valo 	int err;
3838e705c121SKalle Valo 
3839e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3840e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
3841e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3842e705c121SKalle Valo 
3843e705c121SKalle Valo 	return err;
3844e705c121SKalle Valo }
3845e705c121SKalle Valo #endif
3846e705c121SKalle Valo 
3847e705c121SKalle Valo static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
3848e705c121SKalle Valo 				   struct ieee80211_vif *vif,
3849e705c121SKalle Valo 				   struct ieee80211_channel_switch *chsw)
3850e705c121SKalle Valo {
3851e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
3852e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
3853e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
3854e705c121SKalle Valo 	 * (which is when the absence time event starts).
3855e705c121SKalle Valo 	 */
3856e705c121SKalle Valo 
3857e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
3858e705c121SKalle Valo 			   "dummy channel switch op\n");
3859e705c121SKalle Valo }
3860e705c121SKalle Valo 
3861e705c121SKalle Valo static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
3862e705c121SKalle Valo 				      struct ieee80211_vif *vif,
3863e705c121SKalle Valo 				      struct ieee80211_channel_switch *chsw)
3864e705c121SKalle Valo {
3865e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3866e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
3867e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3868e705c121SKalle Valo 	u32 apply_time;
3869e705c121SKalle Valo 	int ret;
3870e705c121SKalle Valo 
3871e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3872e705c121SKalle Valo 
3873e705c121SKalle Valo 	mvmvif->csa_failed = false;
3874e705c121SKalle Valo 
3875e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
3876e705c121SKalle Valo 			   chsw->chandef.center_freq1);
3877e705c121SKalle Valo 
38787174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
38797174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
38807174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
3881e705c121SKalle Valo 
3882e705c121SKalle Valo 	switch (vif->type) {
3883e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3884e705c121SKalle Valo 		csa_vif =
3885e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
3886e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
3887e705c121SKalle Valo 		if (WARN_ONCE(csa_vif && csa_vif->csa_active,
3888e705c121SKalle Valo 			      "Another CSA is already in progress")) {
3889e705c121SKalle Valo 			ret = -EBUSY;
3890e705c121SKalle Valo 			goto out_unlock;
3891e705c121SKalle Valo 		}
3892e705c121SKalle Valo 
3893d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
3894d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
3895d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
3896d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
3897d3a108a4SAndrei Otcheretianski 			goto out_unlock;
3898d3a108a4SAndrei Otcheretianski 		}
3899d3a108a4SAndrei Otcheretianski 
3900e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
3901e705c121SKalle Valo 
3902e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
3903e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
3904e705c121SKalle Valo 			ret = -EBUSY;
3905e705c121SKalle Valo 			goto out_unlock;
3906e705c121SKalle Valo 		}
3907e705c121SKalle Valo 
3908d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
3909d3a108a4SAndrei Otcheretianski 
3910e705c121SKalle Valo 		break;
3911e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3912e705c121SKalle Valo 		/* Schedule the time event to a bit before beacon 1,
3913e705c121SKalle Valo 		 * to make sure we're in the new channel when the
3914f5d8f50fSIlan Peer 		 * GO/AP arrives. In case count <= 1 immediately schedule the
3915f5d8f50fSIlan Peer 		 * TE (this might result with some packet loss or connection
3916f5d8f50fSIlan Peer 		 * loss).
3917e705c121SKalle Valo 		 */
3918f5d8f50fSIlan Peer 		if (chsw->count <= 1)
3919f5d8f50fSIlan Peer 			apply_time = 0;
3920f5d8f50fSIlan Peer 		else
3921e705c121SKalle Valo 			apply_time = chsw->device_timestamp +
3922e705c121SKalle Valo 				((vif->bss_conf.beacon_int * (chsw->count - 1) -
3923e705c121SKalle Valo 				  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
3924e705c121SKalle Valo 
3925e705c121SKalle Valo 		if (chsw->block_tx)
3926e705c121SKalle Valo 			iwl_mvm_csa_client_absent(mvm, vif);
3927e705c121SKalle Valo 
3928e705c121SKalle Valo 		iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
3929e705c121SKalle Valo 					    apply_time);
3930e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
3931e705c121SKalle Valo 			ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3932e705c121SKalle Valo 			if (ret)
3933e705c121SKalle Valo 				goto out_unlock;
3934e705c121SKalle Valo 		}
3935e705c121SKalle Valo 
3936e705c121SKalle Valo 		break;
3937e705c121SKalle Valo 	default:
3938e705c121SKalle Valo 		break;
3939e705c121SKalle Valo 	}
3940e705c121SKalle Valo 
3941e705c121SKalle Valo 	mvmvif->ps_disabled = true;
3942e705c121SKalle Valo 
3943e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
3944e705c121SKalle Valo 	if (ret)
3945e705c121SKalle Valo 		goto out_unlock;
3946e705c121SKalle Valo 
3947e705c121SKalle Valo 	/* we won't be on this channel any longer */
3948e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
3949e705c121SKalle Valo 
3950e705c121SKalle Valo out_unlock:
3951e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3952e705c121SKalle Valo 
3953e705c121SKalle Valo 	return ret;
3954e705c121SKalle Valo }
3955e705c121SKalle Valo 
3956e705c121SKalle Valo static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
3957e705c121SKalle Valo 				       struct ieee80211_vif *vif)
3958e705c121SKalle Valo {
3959e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3960e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3961e705c121SKalle Valo 	int ret;
3962e705c121SKalle Valo 
3963e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3964e705c121SKalle Valo 
3965e705c121SKalle Valo 	if (mvmvif->csa_failed) {
3966e705c121SKalle Valo 		mvmvif->csa_failed = false;
3967e705c121SKalle Valo 		ret = -EIO;
3968e705c121SKalle Valo 		goto out_unlock;
3969e705c121SKalle Valo 	}
3970e705c121SKalle Valo 
3971e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
3972e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta;
3973e705c121SKalle Valo 
3974e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
3975e705c121SKalle Valo 							  mvmvif->ap_sta_id);
3976e705c121SKalle Valo 
3977e705c121SKalle Valo 		if (WARN_ON(!mvmsta)) {
3978e705c121SKalle Valo 			ret = -EIO;
3979e705c121SKalle Valo 			goto out_unlock;
3980e705c121SKalle Valo 		}
3981e705c121SKalle Valo 
3982e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
3983e705c121SKalle Valo 
3984e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3985e705c121SKalle Valo 
3986e705c121SKalle Valo 		ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
3987e705c121SKalle Valo 		if (ret)
3988e705c121SKalle Valo 			goto out_unlock;
3989e705c121SKalle Valo 
3990e705c121SKalle Valo 		iwl_mvm_stop_session_protection(mvm, vif);
3991e705c121SKalle Valo 	}
3992e705c121SKalle Valo 
3993e705c121SKalle Valo 	mvmvif->ps_disabled = false;
3994e705c121SKalle Valo 
3995e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
3996e705c121SKalle Valo 
3997e705c121SKalle Valo out_unlock:
3998e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3999e705c121SKalle Valo 
4000e705c121SKalle Valo 	return ret;
4001e705c121SKalle Valo }
4002e705c121SKalle Valo 
40036110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
40046110d9e5SDavid Spinadel {
40056110d9e5SDavid Spinadel 	if (drop) {
40066110d9e5SDavid Spinadel 		if (iwl_mvm_has_new_tx_api(mvm))
40076110d9e5SDavid Spinadel 			/* TODO new tx api */
40086110d9e5SDavid Spinadel 			WARN_ONCE(1,
40096110d9e5SDavid Spinadel 				  "Need to implement flush TX queue\n");
40106110d9e5SDavid Spinadel 		else
40116110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
40126110d9e5SDavid Spinadel 				iwl_mvm_flushable_queues(mvm) & queues,
40136110d9e5SDavid Spinadel 				0);
40146110d9e5SDavid Spinadel 	} else {
40156110d9e5SDavid Spinadel 		if (iwl_mvm_has_new_tx_api(mvm)) {
40166110d9e5SDavid Spinadel 			struct ieee80211_sta *sta;
40176110d9e5SDavid Spinadel 			int i;
40186110d9e5SDavid Spinadel 
40196110d9e5SDavid Spinadel 			mutex_lock(&mvm->mutex);
40206110d9e5SDavid Spinadel 
40216110d9e5SDavid Spinadel 			for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
40226110d9e5SDavid Spinadel 				sta = rcu_dereference_protected(
40236110d9e5SDavid Spinadel 						mvm->fw_id_to_mac_id[i],
40246110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
40256110d9e5SDavid Spinadel 				if (IS_ERR_OR_NULL(sta))
40266110d9e5SDavid Spinadel 					continue;
40276110d9e5SDavid Spinadel 
40286110d9e5SDavid Spinadel 				iwl_mvm_wait_sta_queues_empty(mvm,
40296110d9e5SDavid Spinadel 						iwl_mvm_sta_from_mac80211(sta));
40306110d9e5SDavid Spinadel 			}
40316110d9e5SDavid Spinadel 
40326110d9e5SDavid Spinadel 			mutex_unlock(&mvm->mutex);
40336110d9e5SDavid Spinadel 		} else {
40346110d9e5SDavid Spinadel 			iwl_trans_wait_tx_queues_empty(mvm->trans,
40356110d9e5SDavid Spinadel 						       queues);
40366110d9e5SDavid Spinadel 		}
40376110d9e5SDavid Spinadel 	}
40386110d9e5SDavid Spinadel }
40396110d9e5SDavid Spinadel 
4040e705c121SKalle Valo static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4041e705c121SKalle Valo 			      struct ieee80211_vif *vif, u32 queues, bool drop)
4042e705c121SKalle Valo {
4043e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4044e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
4045e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4046e705c121SKalle Valo 	struct ieee80211_sta *sta;
4047e705c121SKalle Valo 	int i;
4048e705c121SKalle Valo 	u32 msk = 0;
4049e705c121SKalle Valo 
40506110d9e5SDavid Spinadel 	if (!vif) {
40516110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
40526110d9e5SDavid Spinadel 		return;
40536110d9e5SDavid Spinadel 	}
40546110d9e5SDavid Spinadel 
40556110d9e5SDavid Spinadel 	if (vif->type != NL80211_IFTYPE_STATION)
4056e705c121SKalle Valo 		return;
4057e705c121SKalle Valo 
405824afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
405924afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
406024afba76SLiad Kaufman 
4061e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4062e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
4063e705c121SKalle Valo 
4064e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
40650ae98812SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4066e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4067e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
4068e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
4069e705c121SKalle Valo 			continue;
4070e705c121SKalle Valo 
4071e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4072e705c121SKalle Valo 		if (mvmsta->vif != vif)
4073e705c121SKalle Valo 			continue;
4074e705c121SKalle Valo 
4075e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
4076e705c121SKalle Valo 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4077e705c121SKalle Valo 
4078d49394a1SSara Sharon 		if (drop) {
4079d49394a1SSara Sharon 			if (iwl_mvm_flush_sta(mvm, mvmsta, false, 0))
4080d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
4081d49394a1SSara Sharon 		} else {
4082e705c121SKalle Valo 			msk |= mvmsta->tfd_queue_msk;
4083d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
4084d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
4085e705c121SKalle Valo 		}
4086d49394a1SSara Sharon 	}
4087e705c121SKalle Valo 
4088e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4089e705c121SKalle Valo 
4090e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
4091e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
4092e705c121SKalle Valo 	 */
4093d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
4094a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
4095e705c121SKalle Valo }
4096e705c121SKalle Valo 
4097e705c121SKalle Valo static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4098e705c121SKalle Valo 				  struct survey_info *survey)
4099e705c121SKalle Valo {
4100e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4101e705c121SKalle Valo 	int ret;
4102e705c121SKalle Valo 
4103e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
4104e705c121SKalle Valo 
4105e705c121SKalle Valo 	/* only support global statistics right now */
4106e705c121SKalle Valo 	if (idx != 0)
4107e705c121SKalle Valo 		return -ENOENT;
4108e705c121SKalle Valo 
4109280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4110e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4111e705c121SKalle Valo 		return -ENOENT;
4112e705c121SKalle Valo 
4113e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4114e705c121SKalle Valo 
4115aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
4116e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
4117e705c121SKalle Valo 		if (ret)
4118e705c121SKalle Valo 			goto out;
4119e705c121SKalle Valo 	}
4120e705c121SKalle Valo 
4121e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
4122e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
4123e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
4124e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
4125e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
4126e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
4127e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
4128e705c121SKalle Valo 
4129e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
4130e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
4131e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
4132e705c121SKalle Valo 
4133e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
4134e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
4135e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
4136e705c121SKalle Valo 
4137e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4138e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
4139e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
4140e705c121SKalle Valo 
4141e705c121SKalle Valo 	ret = 0;
4142e705c121SKalle Valo  out:
4143e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4144e705c121SKalle Valo 	return ret;
4145e705c121SKalle Valo }
4146e705c121SKalle Valo 
4147e705c121SKalle Valo static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
4148e705c121SKalle Valo 				       struct ieee80211_vif *vif,
4149e705c121SKalle Valo 				       struct ieee80211_sta *sta,
4150e705c121SKalle Valo 				       struct station_info *sinfo)
4151e705c121SKalle Valo {
4152e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4153e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4154e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4155e705c121SKalle Valo 
4156988b5968SSara Sharon 	if (mvmsta->avg_energy) {
4157988b5968SSara Sharon 		sinfo->signal_avg = mvmsta->avg_energy;
4158988b5968SSara Sharon 		sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
4159988b5968SSara Sharon 	}
4160988b5968SSara Sharon 
4161280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4162e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4163e705c121SKalle Valo 		return;
4164e705c121SKalle Valo 
4165e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
4166e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
4167e705c121SKalle Valo 		return;
4168e705c121SKalle Valo 
4169e705c121SKalle Valo 	if (!vif->bss_conf.assoc)
4170e705c121SKalle Valo 		return;
4171e705c121SKalle Valo 
4172e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4173e705c121SKalle Valo 
4174e705c121SKalle Valo 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
4175e705c121SKalle Valo 		goto unlock;
4176e705c121SKalle Valo 
4177e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
4178e705c121SKalle Valo 		goto unlock;
4179e705c121SKalle Valo 
4180e705c121SKalle Valo 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
4181e705c121SKalle Valo 			   mvmvif->beacon_stats.accu_num_beacons;
4182e705c121SKalle Valo 	sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX);
4183e705c121SKalle Valo 	if (mvmvif->beacon_stats.avg_signal) {
4184e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
4185e705c121SKalle Valo 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
4186e705c121SKalle Valo 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
4187e705c121SKalle Valo 	}
4188e705c121SKalle Valo  unlock:
4189e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4190e705c121SKalle Valo }
4191e705c121SKalle Valo 
4192e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
4193e705c121SKalle Valo 					struct ieee80211_vif *vif,
4194e705c121SKalle Valo 					const struct ieee80211_event *event)
4195e705c121SKalle Valo {
41964c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
4197e705c121SKalle Valo 	do {								\
41984c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
4199e705c121SKalle Valo 			break;						\
42007174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
4201e705c121SKalle Valo 	} while (0)
4202e705c121SKalle Valo 
4203e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
4204e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
4205e705c121SKalle Valo 
4206e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_MLME))
4207e705c121SKalle Valo 		return;
4208e705c121SKalle Valo 
4209e705c121SKalle Valo 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_MLME);
4210e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
42117174beb6SJohannes Berg 	if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
42127174beb6SJohannes Berg 					   ieee80211_vif_to_wdev(vif), trig))
4213e705c121SKalle Valo 		return;
4214e705c121SKalle Valo 
4215e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
4216e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
42174c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
4218e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
4219e705c121SKalle Valo 					    event->u.mlme.reason);
4220e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
42214c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
4222e705c121SKalle Valo 					   "ASSOC TIMEOUT");
4223e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
4224e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
42254c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
4226e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
4227e705c121SKalle Valo 					   event->u.mlme.reason);
4228e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
42294c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
4230e705c121SKalle Valo 					   "AUTH TIMEOUT");
4231e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
42324c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
4233e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
4234e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
42354c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
4236e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
4237e705c121SKalle Valo 	}
4238e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
4239e705c121SKalle Valo }
4240e705c121SKalle Valo 
4241e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
4242e705c121SKalle Valo 					  struct ieee80211_vif *vif,
4243e705c121SKalle Valo 					  const struct ieee80211_event *event)
4244e705c121SKalle Valo {
4245e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
4246e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
4247e705c121SKalle Valo 
4248e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
4249e705c121SKalle Valo 		return;
4250e705c121SKalle Valo 
4251e705c121SKalle Valo 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
4252e705c121SKalle Valo 	ba_trig = (void *)trig->data;
42537174beb6SJohannes Berg 	if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
42547174beb6SJohannes Berg 					   ieee80211_vif_to_wdev(vif), trig))
4255e705c121SKalle Valo 		return;
4256e705c121SKalle Valo 
4257e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
4258e705c121SKalle Valo 		return;
4259e705c121SKalle Valo 
42607174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4261e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
4262e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
4263e705c121SKalle Valo 				event->u.ba.ssn);
4264e705c121SKalle Valo }
4265e705c121SKalle Valo 
4266e705c121SKalle Valo static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
4267e705c121SKalle Valo 				       struct ieee80211_vif *vif,
4268e705c121SKalle Valo 				       const struct ieee80211_event *event)
4269e705c121SKalle Valo {
4270e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4271e705c121SKalle Valo 
4272e705c121SKalle Valo 	switch (event->type) {
4273e705c121SKalle Valo 	case MLME_EVENT:
4274e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
4275e705c121SKalle Valo 		break;
4276e705c121SKalle Valo 	case BAR_RX_EVENT:
4277e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
4278e705c121SKalle Valo 		break;
4279e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
4280528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
4281528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
4282e705c121SKalle Valo 		break;
4283e705c121SKalle Valo 	default:
4284e705c121SKalle Valo 		break;
4285e705c121SKalle Valo 	}
4286e705c121SKalle Valo }
4287e705c121SKalle Valo 
4288d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
4289d0ff5d22SSara Sharon 				     struct iwl_mvm_internal_rxq_notif *notif,
4290d0ff5d22SSara Sharon 				     u32 size)
42910636b938SSara Sharon {
42920636b938SSara Sharon 	u32 qmask = BIT(mvm->trans->num_rx_queues) - 1;
42930636b938SSara Sharon 	int ret;
42940636b938SSara Sharon 
42950636b938SSara Sharon 	lockdep_assert_held(&mvm->mutex);
42960636b938SSara Sharon 
4297*cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
42980636b938SSara Sharon 		return;
42990636b938SSara Sharon 
4300d0ff5d22SSara Sharon 	notif->cookie = mvm->queue_sync_cookie;
43010636b938SSara Sharon 
4302d0ff5d22SSara Sharon 	if (notif->sync)
4303d0ff5d22SSara Sharon 		atomic_set(&mvm->queue_sync_counter,
4304d0ff5d22SSara Sharon 			   mvm->trans->num_rx_queues);
4305d0ff5d22SSara Sharon 
4306*cb8550e1SSara Sharon 	/* TODO - remove this when we have RXQ config API */
4307*cb8550e1SSara Sharon 	if (mvm->trans->cfg->device_family == IWL_DEVICE_FAMILY_A000) {
4308*cb8550e1SSara Sharon 		qmask = BIT(0);
4309*cb8550e1SSara Sharon 		if (notif->sync)
4310*cb8550e1SSara Sharon 			atomic_set(&mvm->queue_sync_counter, 1);
4311*cb8550e1SSara Sharon 	}
4312*cb8550e1SSara Sharon 
4313d0ff5d22SSara Sharon 	ret = iwl_mvm_notify_rx_queue(mvm, qmask, (u8 *)notif, size);
43140636b938SSara Sharon 	if (ret) {
43150636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
43160636b938SSara Sharon 		goto out;
43170636b938SSara Sharon 	}
4318d0ff5d22SSara Sharon 
43196ad04359SJohannes Berg 	if (notif->sync) {
43203a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
43216ad04359SJohannes Berg 					 atomic_read(&mvm->queue_sync_counter) == 0 ||
43226ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
43230636b938SSara Sharon 					 HZ);
43246ad04359SJohannes Berg 		WARN_ON_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm));
43256ad04359SJohannes Berg 	}
43260636b938SSara Sharon 
43270636b938SSara Sharon out:
43280636b938SSara Sharon 	atomic_set(&mvm->queue_sync_counter, 0);
43290636b938SSara Sharon 	mvm->queue_sync_cookie++;
43300636b938SSara Sharon }
43310636b938SSara Sharon 
43320636b938SSara Sharon static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
43330636b938SSara Sharon {
43340636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4335d0ff5d22SSara Sharon 	struct iwl_mvm_internal_rxq_notif data = {
4336d0ff5d22SSara Sharon 		.type = IWL_MVM_RXQ_EMPTY,
4337d0ff5d22SSara Sharon 		.sync = 1,
4338d0ff5d22SSara Sharon 	};
43390636b938SSara Sharon 
43400636b938SSara Sharon 	mutex_lock(&mvm->mutex);
4341d0ff5d22SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, &data, sizeof(data));
43420636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
43430636b938SSara Sharon }
43440636b938SSara Sharon 
4345e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
4346e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
4347e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
4348e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
4349e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
4350e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
4351e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
4352e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
4353e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
4354e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
4355e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
4356e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
4357e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
4358e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
4359e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
4360e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
4361e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
4362e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
4363e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
4364e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
4365e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
4366e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
4367e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
4368e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
4369e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
4370e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
4371e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
4372e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
4373e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
4374e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
4375e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
4376e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
4377e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
4378e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
4379e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
4380e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
4381e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
4382e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
4383e705c121SKalle Valo 
4384e705c121SKalle Valo 	.start_ap = iwl_mvm_start_ap_ibss,
4385e705c121SKalle Valo 	.stop_ap = iwl_mvm_stop_ap_ibss,
4386e705c121SKalle Valo 	.join_ibss = iwl_mvm_start_ap_ibss,
4387e705c121SKalle Valo 	.leave_ibss = iwl_mvm_stop_ap_ibss,
4388e705c121SKalle Valo 
43892f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
43902f0282dbSJohannes Berg 
4391e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
4392e705c121SKalle Valo 
4393e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
4394e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
4395e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
4396e705c121SKalle Valo 
4397e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
4398e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
4399e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
4400e705c121SKalle Valo 
4401e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
4402e705c121SKalle Valo 
44030636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
44040636b938SSara Sharon 
4405e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
4406e705c121SKalle Valo 
4407e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
4408e705c121SKalle Valo 	/* look at d3.c */
4409e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
4410e705c121SKalle Valo 	.resume = iwl_mvm_resume,
4411e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
4412e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
4413e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
4414e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
4415e705c121SKalle Valo #endif
4416e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
4417e705c121SKalle Valo #endif
4418e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
4419e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
4420e705c121SKalle Valo };
4421