xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision 88ad368a84d8df81c7b832a6aba3487c63019b29)
1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
826d6c16bSSara Sharon  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
926d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
1012d47f0eSAndrei Otcheretianski  * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation
11e705c121SKalle Valo  *
12e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
13e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
14e705c121SKalle Valo  * published by the Free Software Foundation.
15e705c121SKalle Valo  *
16e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
17e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
18e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19e705c121SKalle Valo  * General Public License for more details.
20e705c121SKalle Valo  *
21e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
22e705c121SKalle Valo  * in the file called COPYING.
23e705c121SKalle Valo  *
24e705c121SKalle Valo  * Contact Information:
25cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
26e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27e705c121SKalle Valo  *
28e705c121SKalle Valo  * BSD LICENSE
29e705c121SKalle Valo  *
3026d6c16bSSara Sharon  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
3126d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
3212d47f0eSAndrei Otcheretianski  * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation
33e705c121SKalle Valo  * All rights reserved.
34e705c121SKalle Valo  *
35e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
36e705c121SKalle Valo  * modification, are permitted provided that the following conditions
37e705c121SKalle Valo  * are met:
38e705c121SKalle Valo  *
39e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
40e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
41e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
42e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
43e705c121SKalle Valo  *    the documentation and/or other materials provided with the
44e705c121SKalle Valo  *    distribution.
45e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
46e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
47e705c121SKalle Valo  *    from this software without specific prior written permission.
48e705c121SKalle Valo  *
49e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60e705c121SKalle Valo  *
61e705c121SKalle Valo  *****************************************************************************/
62e705c121SKalle Valo #include <linux/kernel.h>
63e705c121SKalle Valo #include <linux/slab.h>
64e705c121SKalle Valo #include <linux/skbuff.h>
65e705c121SKalle Valo #include <linux/netdevice.h>
66e705c121SKalle Valo #include <linux/etherdevice.h>
67e705c121SKalle Valo #include <linux/ip.h>
68e705c121SKalle Valo #include <linux/if_arp.h>
692f89a5d7SGolan Ben-Ami #include <linux/time.h>
70e705c121SKalle Valo #include <net/mac80211.h>
71e705c121SKalle Valo #include <net/ieee80211_radiotap.h>
72e705c121SKalle Valo #include <net/tcp.h>
73e705c121SKalle Valo 
74e705c121SKalle Valo #include "iwl-op-mode.h"
75e705c121SKalle Valo #include "iwl-io.h"
76e705c121SKalle Valo #include "mvm.h"
77e705c121SKalle Valo #include "sta.h"
78e705c121SKalle Valo #include "time-event.h"
79e705c121SKalle Valo #include "iwl-eeprom-parse.h"
80e705c121SKalle Valo #include "iwl-phy-db.h"
81e705c121SKalle Valo #include "testmode.h"
82d962f9b1SJohannes Berg #include "fw/error-dump.h"
83e705c121SKalle Valo #include "iwl-prph.h"
84e705c121SKalle Valo #include "iwl-nvm-parse.h"
85e705c121SKalle Valo 
86e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
87e705c121SKalle Valo 	{
88e705c121SKalle Valo 		.max = 1,
89e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
90e705c121SKalle Valo 	},
91e705c121SKalle Valo 	{
92e705c121SKalle Valo 		.max = 1,
93e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
94e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
95e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
96e705c121SKalle Valo 	},
97e705c121SKalle Valo 	{
98e705c121SKalle Valo 		.max = 1,
99e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
100e705c121SKalle Valo 	},
101e705c121SKalle Valo };
102e705c121SKalle Valo 
103e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
104e705c121SKalle Valo 	{
105e705c121SKalle Valo 		.num_different_channels = 2,
106e705c121SKalle Valo 		.max_interfaces = 3,
107e705c121SKalle Valo 		.limits = iwl_mvm_limits,
108e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
109e705c121SKalle Valo 	},
110e705c121SKalle Valo };
111e705c121SKalle Valo 
112e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
113e705c121SKalle Valo /*
114e705c121SKalle Valo  * Use the reserved field to indicate magic values.
115e705c121SKalle Valo  * these values will only be used internally by the driver,
116e705c121SKalle Valo  * and won't make it to the fw (reserved will be 0).
117e705c121SKalle Valo  * BC_FILTER_MAGIC_IP - configure the val of this attribute to
118e705c121SKalle Valo  *	be the vif's ip address. in case there is not a single
119e705c121SKalle Valo  *	ip address (0, or more than 1), this attribute will
120e705c121SKalle Valo  *	be skipped.
121e705c121SKalle Valo  * BC_FILTER_MAGIC_MAC - set the val of this attribute to
122e705c121SKalle Valo  *	the LSB bytes of the vif's mac address
123e705c121SKalle Valo  */
124e705c121SKalle Valo enum {
125e705c121SKalle Valo 	BC_FILTER_MAGIC_NONE = 0,
126e705c121SKalle Valo 	BC_FILTER_MAGIC_IP,
127e705c121SKalle Valo 	BC_FILTER_MAGIC_MAC,
128e705c121SKalle Valo };
129e705c121SKalle Valo 
130e705c121SKalle Valo static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
131e705c121SKalle Valo 	{
132e705c121SKalle Valo 		/* arp */
133e705c121SKalle Valo 		.discard = 0,
134e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
135e705c121SKalle Valo 		.attrs = {
136e705c121SKalle Valo 			{
137e705c121SKalle Valo 				/* frame type - arp, hw type - ethernet */
138e705c121SKalle Valo 				.offset_type =
139e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
140e705c121SKalle Valo 				.offset = sizeof(rfc1042_header),
141e705c121SKalle Valo 				.val = cpu_to_be32(0x08060001),
142e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
143e705c121SKalle Valo 			},
144e705c121SKalle Valo 			{
145e705c121SKalle Valo 				/* arp dest ip */
146e705c121SKalle Valo 				.offset_type =
147e705c121SKalle Valo 					BCAST_FILTER_OFFSET_PAYLOAD_START,
148e705c121SKalle Valo 				.offset = sizeof(rfc1042_header) + 2 +
149e705c121SKalle Valo 					  sizeof(struct arphdr) +
150e705c121SKalle Valo 					  ETH_ALEN + sizeof(__be32) +
151e705c121SKalle Valo 					  ETH_ALEN,
152e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
153e705c121SKalle Valo 				/* mark it as special field */
154e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
155e705c121SKalle Valo 			},
156e705c121SKalle Valo 		},
157e705c121SKalle Valo 	},
158e705c121SKalle Valo 	{
159e705c121SKalle Valo 		/* dhcp offer bcast */
160e705c121SKalle Valo 		.discard = 0,
161e705c121SKalle Valo 		.frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
162e705c121SKalle Valo 		.attrs = {
163e705c121SKalle Valo 			{
164e705c121SKalle Valo 				/* udp dest port - 68 (bootp client)*/
165e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
166e705c121SKalle Valo 				.offset = offsetof(struct udphdr, dest),
167e705c121SKalle Valo 				.val = cpu_to_be32(0x00440000),
168e705c121SKalle Valo 				.mask = cpu_to_be32(0xffff0000),
169e705c121SKalle Valo 			},
170e705c121SKalle Valo 			{
171e705c121SKalle Valo 				/* dhcp - lsb bytes of client hw address */
172e705c121SKalle Valo 				.offset_type = BCAST_FILTER_OFFSET_IP_END,
173e705c121SKalle Valo 				.offset = 38,
174e705c121SKalle Valo 				.mask = cpu_to_be32(0xffffffff),
175e705c121SKalle Valo 				/* mark it as special field */
176e705c121SKalle Valo 				.reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
177e705c121SKalle Valo 			},
178e705c121SKalle Valo 		},
179e705c121SKalle Valo 	},
180e705c121SKalle Valo 	/* last filter must be empty */
181e705c121SKalle Valo 	{},
182e705c121SKalle Valo };
183e705c121SKalle Valo #endif
184e705c121SKalle Valo 
185fc36ffdaSJohannes Berg static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
186fc36ffdaSJohannes Berg 	.max_peers = IWL_MVM_TOF_MAX_APS,
187fc36ffdaSJohannes Berg 	.report_ap_tsf = 1,
188fc36ffdaSJohannes Berg 	.randomize_mac_addr = 1,
189fc36ffdaSJohannes Berg 
190fc36ffdaSJohannes Berg 	.ftm = {
191fc36ffdaSJohannes Berg 		.supported = 1,
192fc36ffdaSJohannes Berg 		.asap = 1,
193fc36ffdaSJohannes Berg 		.non_asap = 1,
194fc36ffdaSJohannes Berg 		.request_lci = 1,
195fc36ffdaSJohannes Berg 		.request_civicloc = 1,
1966815e3d0SAvraham Stern 		.trigger_based = 1,
1976815e3d0SAvraham Stern 		.non_trigger_based = 1,
198fc36ffdaSJohannes Berg 		.max_bursts_exponent = -1, /* all supported */
199fc36ffdaSJohannes Berg 		.max_ftms_per_burst = 0, /* no limits */
200fc36ffdaSJohannes Berg 		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
201fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_20) |
202fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_40) |
203fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_80),
204fc36ffdaSJohannes Berg 		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
205fc36ffdaSJohannes Berg 			     BIT(NL80211_PREAMBLE_HT) |
2066815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_VHT) |
2076815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_HE),
208fc36ffdaSJohannes Berg 	},
209fc36ffdaSJohannes Berg };
210fc36ffdaSJohannes Berg 
2116569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
212c56e00a3SJohannes Berg 				 enum set_key_cmd cmd,
213c56e00a3SJohannes Berg 				 struct ieee80211_vif *vif,
214c56e00a3SJohannes Berg 				 struct ieee80211_sta *sta,
215c56e00a3SJohannes Berg 				 struct ieee80211_key_conf *key);
216c56e00a3SJohannes Berg 
217e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
218e705c121SKalle Valo {
219e705c121SKalle Valo 	int i;
220e705c121SKalle Valo 
221e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
222e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
223e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
224e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
225e705c121SKalle Valo 	}
226e705c121SKalle Valo }
227e705c121SKalle Valo 
228e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
229e705c121SKalle Valo 						  const char *alpha2,
230e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
231e705c121SKalle Valo 						  bool *changed)
232e705c121SKalle Valo {
233e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
234e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
235e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
236e705c121SKalle Valo 	struct iwl_mcc_update_resp *resp;
237e705c121SKalle Valo 
238e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
239e705c121SKalle Valo 
240e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
241e705c121SKalle Valo 
242e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
243e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
244e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
245e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
246e705c121SKalle Valo 		goto out;
247e705c121SKalle Valo 	}
248e705c121SKalle Valo 
24982715ac7SEmmanuel Grumbach 	if (changed) {
25082715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
25182715ac7SEmmanuel Grumbach 
25282715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
25382715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
25482715ac7SEmmanuel Grumbach 	}
255e705c121SKalle Valo 
256e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
257e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
258e705c121SKalle Valo 				      resp->channels,
25977e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
2602763bba6SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info),
2612763bba6SHaim Dreyfuss 				      __le16_to_cpu(resp->cap));
262e705c121SKalle Valo 	/* Store the return source id */
263e705c121SKalle Valo 	src_id = resp->source_id;
264e705c121SKalle Valo 	kfree(resp);
265e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
266e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
267e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
268e705c121SKalle Valo 		goto out;
269e705c121SKalle Valo 	}
270e705c121SKalle Valo 
271e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
272e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
273e705c121SKalle Valo 	mvm->lar_regdom_set = true;
274e705c121SKalle Valo 	mvm->mcc_src = src_id;
275e705c121SKalle Valo 
276e705c121SKalle Valo out:
277e705c121SKalle Valo 	return regd;
278e705c121SKalle Valo }
279e705c121SKalle Valo 
280e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
281e705c121SKalle Valo {
282e705c121SKalle Valo 	bool changed;
283e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
284e705c121SKalle Valo 
285e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
286e705c121SKalle Valo 		return;
287e705c121SKalle Valo 
288e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
289e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
290e705c121SKalle Valo 		/* only update the regulatory core if changed */
291e705c121SKalle Valo 		if (changed)
292e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
293e705c121SKalle Valo 
294e705c121SKalle Valo 		kfree(regd);
295e705c121SKalle Valo 	}
296e705c121SKalle Valo }
297e705c121SKalle Valo 
298e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
299e705c121SKalle Valo 							  bool *changed)
300e705c121SKalle Valo {
301e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
302e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
303e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
304e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
305e705c121SKalle Valo }
306e705c121SKalle Valo 
307e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
308e705c121SKalle Valo {
309e705c121SKalle Valo 	enum iwl_mcc_source used_src;
310e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
311e705c121SKalle Valo 	int ret;
312e705c121SKalle Valo 	bool changed;
313e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
314e705c121SKalle Valo 			rtnl_dereference(mvm->hw->wiphy->regd);
315e705c121SKalle Valo 
316e705c121SKalle Valo 	if (!r)
317e705c121SKalle Valo 		return -ENOENT;
318e705c121SKalle Valo 
319e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
320e705c121SKalle Valo 	used_src = mvm->mcc_src;
321e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
322e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
323e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
324e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
325e705c121SKalle Valo 			kfree(regd);
326e705c121SKalle Valo 	}
327e705c121SKalle Valo 
328e705c121SKalle Valo 	/* Now set our last stored MCC and source */
329e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
330e705c121SKalle Valo 				     &changed);
331e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
332e705c121SKalle Valo 		return -EIO;
333e705c121SKalle Valo 
334e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
335e705c121SKalle Valo 	if (changed)
336e705c121SKalle Valo 		ret = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
337e705c121SKalle Valo 	else
338e705c121SKalle Valo 		ret = 0;
339e705c121SKalle Valo 
340e705c121SKalle Valo 	kfree(regd);
341e705c121SKalle Valo 	return ret;
342e705c121SKalle Valo }
343e705c121SKalle Valo 
3447f2ea521SYueHaibing static const u8 he_if_types_ext_capa_sta[] = {
3457360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
346918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
3477360f99eSEmmanuel Grumbach 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
3487360f99eSEmmanuel Grumbach 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
3497360f99eSEmmanuel Grumbach };
3507360f99eSEmmanuel Grumbach 
3517f2ea521SYueHaibing static const struct wiphy_iftype_ext_capab he_iftypes_ext_capa[] = {
3527360f99eSEmmanuel Grumbach 	{
3537360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
3547360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
3557360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
3567360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
3577360f99eSEmmanuel Grumbach 	},
3587360f99eSEmmanuel Grumbach };
3597360f99eSEmmanuel Grumbach 
360e8503aecSBen Greear static int
361e8503aecSBen Greear iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
362e8503aecSBen Greear {
363e8503aecSBen Greear 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
364e8503aecSBen Greear 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
365e8503aecSBen Greear 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
366e8503aecSBen Greear 	return 0;
367e8503aecSBen Greear }
368e8503aecSBen Greear 
369e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
370e705c121SKalle Valo {
371e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
372e705c121SKalle Valo 	int num_mac, ret, i;
373e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
374e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
375e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
376e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
377e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
378e705c121SKalle Valo 	};
3793f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
3803f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
3813f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3823f37c229SIdo Yariv #endif
383e705c121SKalle Valo 
384e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
385e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
386e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
387e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
388e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
389e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
390e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
391e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
392e705c121SKalle Valo 	ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
393e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
394e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
395e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
396e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
397909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
39830433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
399d270e7b8SIlan Peer 	ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
400520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
401cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
402cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
403cfb21b11SJohannes Berg 	/*
404cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
405cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
406cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
407cfb21b11SJohannes Berg 	 * assert EDC.
408cfb21b11SJohannes Berg 	 *
409cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
410cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
411cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
412cfb21b11SJohannes Berg 	 * devices.
413cfb21b11SJohannes Berg 	 */
4147d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
415438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
416438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
417ecaf71deSGregory Greenman 
4184243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
419ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
420ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
421ecaf71deSGregory Greenman 	}
422ecaf71deSGregory Greenman 
423b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
424b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
425960f864bSJohannes Berg 
426960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
427960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
42865e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
429960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
430960f864bSJohannes Berg 		/*
431960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
432960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
433960f864bSJohannes Berg 		 * for station powersave
434960f864bSJohannes Berg 		 */
435960f864bSJohannes Berg 		return -EINVAL;
436960f864bSJohannes Berg 	}
437e705c121SKalle Valo 
43880938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
43980938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
44080938abcSJohannes Berg 
4412d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
4422d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
4432d7cf549SJohannes Berg 
444cf961e16SLiad Kaufman 	hw->queues = IEEE80211_MAX_QUEUES;
445e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
446e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
447e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
448e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
449e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
450371a17edSJohannes Berg 
451371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
452371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
453371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
454371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
455371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
456371a17edSJohannes Berg 
4574243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
4589f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
4599f66a397SGregory Greenman 
460e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
461e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
462438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
463e705c121SKalle Valo 
4648e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
465e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
466e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
467e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
468e705c121SKalle Valo 
4692a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4702a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4712a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
4722a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4732a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4742a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
4752a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4762a53d166SAyala Beker 	}
4772a53d166SAyala Beker 
478f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
479f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
480f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
481f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
482f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
483f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
484f4bfdc5eSEmmanuel Grumbach 
485e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
486f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
487e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
4888e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4898e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4908e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
4918e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4928e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4938e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
4948e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4958e160ab8SAyala Beker 	}
496e705c121SKalle Valo 
497e705c121SKalle Valo 	/* currently FW API supports only one optional cipher scheme */
498e705c121SKalle Valo 	if (mvm->fw->cs[0].cipher) {
49924ddddf3SJohannes Berg 		const struct iwl_fw_cipher_scheme *fwcs = &mvm->fw->cs[0];
50024ddddf3SJohannes Berg 		struct ieee80211_cipher_scheme *cs = &mvm->cs[0];
50124ddddf3SJohannes Berg 
502e705c121SKalle Valo 		mvm->hw->n_cipher_schemes = 1;
50324ddddf3SJohannes Berg 
50424ddddf3SJohannes Berg 		cs->cipher = le32_to_cpu(fwcs->cipher);
50524ddddf3SJohannes Berg 		cs->iftype = BIT(NL80211_IFTYPE_STATION);
50624ddddf3SJohannes Berg 		cs->hdr_len = fwcs->hdr_len;
50724ddddf3SJohannes Berg 		cs->pn_len = fwcs->pn_len;
50824ddddf3SJohannes Berg 		cs->pn_off = fwcs->pn_off;
50924ddddf3SJohannes Berg 		cs->key_idx_off = fwcs->key_idx_off;
51024ddddf3SJohannes Berg 		cs->key_idx_mask = fwcs->key_idx_mask;
51124ddddf3SJohannes Berg 		cs->key_idx_shift = fwcs->key_idx_shift;
51224ddddf3SJohannes Berg 		cs->mic_len = fwcs->mic_len;
51324ddddf3SJohannes Berg 
51424ddddf3SJohannes Berg 		mvm->hw->cipher_schemes = mvm->cs;
51524ddddf3SJohannes Berg 		mvm->ciphers[hw->wiphy->n_cipher_suites] = cs->cipher;
516e705c121SKalle Valo 		hw->wiphy->n_cipher_suites++;
517e705c121SKalle Valo 	}
518e705c121SKalle Valo 
519b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
520fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
521b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
522b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
523fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
524fc36ffdaSJohannes Berg 	}
525b73f9a4aSJohannes Berg 
526e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
527e705c121SKalle Valo 	hw->wiphy->features |=
528e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
529e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
530e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
531e705c121SKalle Valo 
532e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
533e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
534e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
535cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
536e705c121SKalle Valo 
537e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
538e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
539e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
540e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
541e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
542e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
543e705c121SKalle Valo 
544e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
545e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
54633e3fd99SAlexander Wetzel 
54733e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
54833e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
54933e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
55033e3fd99SAlexander Wetzel 	 */
55133e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
55233e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
55333e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
554e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
555e47df5bdSJohannes Berg 
556e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
557e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
558e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
559e705c121SKalle Valo 	else
560e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
561e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
562e705c121SKalle Valo 
563e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
564e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
565e705c121SKalle Valo 
566e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
567e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
568e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
569e705c121SKalle Valo 
570e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
571e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
572e705c121SKalle Valo 
573e705c121SKalle Valo 	/* Extract MAC address */
574e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
575e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
576e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
577e705c121SKalle Valo 
578e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
579e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
580e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
581e705c121SKalle Valo 
582e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
583e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
584e705c121SKalle Valo 		       ETH_ALEN);
585e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
586e705c121SKalle Valo 		hw->wiphy->n_addresses++;
587e705c121SKalle Valo 	}
588e705c121SKalle Valo 
589e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
590e705c121SKalle Valo 
591e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
592e705c121SKalle Valo 
593e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
594e705c121SKalle Valo 
595e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
596e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
597e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
598e705c121SKalle Valo 
599e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
600e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
601e705c121SKalle Valo 	else
602e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
603e705c121SKalle Valo 
60457fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
60557fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
60657fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
60757fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
60857fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
60957fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
610e705c121SKalle Valo 
611e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
612e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
613e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
614e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
61557fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
616e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
617e705c121SKalle Valo 	}
618e705c121SKalle Valo 
619e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
620e705c121SKalle Valo 
621e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
622e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
623e705c121SKalle Valo 	else
624e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
625e705c121SKalle Valo 
626ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
627e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
6285d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
629e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
630e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
631e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
632e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
633e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
634e705c121SKalle Valo 
635e705c121SKalle Valo 	/*
636e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
637e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
638e705c121SKalle Valo 	 */
639e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
640e705c121SKalle Valo 
641e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
642e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
643e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
644a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
645e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
646e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
647e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
648e705c121SKalle Valo 
649e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
650e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
651e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
652e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
653e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
654e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
655e705c121SKalle Valo 
656e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
657e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
658e705c121SKalle Valo 		hw->wiphy->features |=
659e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
660e705c121SKalle Valo 
661e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
662e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
663e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
664e705c121SKalle Valo 
665aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
666aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
667aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
668aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
669aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
670aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
671aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
672aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
673aacf8f18SAvrahams Stern 	}
674aacf8f18SAvrahams Stern 
6758f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
6768f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6778f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
6788f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6798f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
6808f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6818f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
6828f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6838f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
6848f691af9SZamir, Roee 	}
6858f691af9SZamir, Roee 
6867360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
6877360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
6887360f99eSEmmanuel Grumbach 		hw->wiphy->iftype_ext_capab = he_iftypes_ext_capa;
6897360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
6907360f99eSEmmanuel Grumbach 			ARRAY_SIZE(he_iftypes_ext_capa);
691918cbf39SSara Sharon 
692918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
693918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
6947360f99eSEmmanuel Grumbach 	}
6957360f99eSEmmanuel Grumbach 
696e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
697e705c121SKalle Valo 
698e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6993f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
700e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
701e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
702e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
703e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
704e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
705e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
706e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
707e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
708e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
709e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
710e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
711e705c121SKalle Valo 
712e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
713e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
714e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
7155d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
7165d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
717e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
718e705c121SKalle Valo 	}
719e705c121SKalle Valo #endif
720e705c121SKalle Valo 
721e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
722e705c121SKalle Valo 	/* assign default bcast filtering configuration */
723e705c121SKalle Valo 	mvm->bcast_filters = iwl_mvm_default_bcast_filters;
724e705c121SKalle Valo #endif
725e705c121SKalle Valo 
726e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
727e705c121SKalle Valo 	if (ret)
728e705c121SKalle Valo 		return ret;
729e705c121SKalle Valo 
730e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
731e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
732e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
733e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
734e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
735e705c121SKalle Valo 	}
736e705c121SKalle Valo 
737e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
738e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
739e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
740e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
741e705c121SKalle Valo 	}
742e705c121SKalle Valo 
743e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
7445e6a98dcSSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm)) {
7455e6a98dcSSara Sharon 		hw->netdev_features &= ~(IWL_TX_CSUM_NETIF_FLAGS |
7465e6a98dcSSara Sharon 					 NETIF_F_RXCSUM);
7475e6a98dcSSara Sharon 		/* We may support SW TX CSUM */
74841837ca9SEmmanuel Grumbach 		if (IWL_MVM_SW_TX_CSUM_OFFLOAD)
7495e6a98dcSSara Sharon 			hw->netdev_features |= IWL_TX_CSUM_NETIF_FLAGS;
7505e6a98dcSSara Sharon 	}
75141837ca9SEmmanuel Grumbach 
75291b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
75391b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
75491b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
75591b08c2dSAviya Erenfeld 
756e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
757e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
758e8503aecSBen Greear 
759de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
760de645e89SJohannes Berg 	if (ret) {
761de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
762e705c121SKalle Valo 	}
763e705c121SKalle Valo 
764e705c121SKalle Valo 	return ret;
765e705c121SKalle Valo }
766e705c121SKalle Valo 
767df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
768df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
769df2378abSJohannes Berg {
770df2378abSJohannes Berg 	if (likely(sta)) {
771df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
772df2378abSJohannes Berg 			return;
773df2378abSJohannes Berg 	} else {
774df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
775df2378abSJohannes Berg 			return;
776df2378abSJohannes Berg 	}
777df2378abSJohannes Berg 
778df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
779df2378abSJohannes Berg }
780df2378abSJohannes Berg 
781e705c121SKalle Valo static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
782e705c121SKalle Valo 			   struct ieee80211_tx_control *control,
783e705c121SKalle Valo 			   struct sk_buff *skb)
784e705c121SKalle Valo {
785e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
786e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
787e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
788e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
789cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
790cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
791e705c121SKalle Valo 
792e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
793e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
794e705c121SKalle Valo 		goto drop;
795e705c121SKalle Valo 	}
796e705c121SKalle Valo 
797cfbc6c4cSSara Sharon 	if (offchannel &&
798e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
799e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
800e705c121SKalle Valo 		goto drop;
801e705c121SKalle Valo 
802eb045e6eSDavid Spinadel 	/* treat non-bufferable MMPDUs on AP interfaces as broadcast */
803eb045e6eSDavid Spinadel 	if ((info->control.vif->type == NL80211_IFTYPE_AP ||
804eb045e6eSDavid Spinadel 	     info->control.vif->type == NL80211_IFTYPE_ADHOC) &&
805e705c121SKalle Valo 	    ieee80211_is_mgmt(hdr->frame_control) &&
806eb045e6eSDavid Spinadel 	    !ieee80211_is_bufferable_mmpdu(hdr->frame_control))
807e705c121SKalle Valo 		sta = NULL;
808e705c121SKalle Valo 
809dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
810cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
811cfbc6c4cSSara Sharon 	    !offchannel) {
812dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
813dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
814dc1aca22SAndrei Otcheretianski 		u8 ap_sta_id = READ_ONCE(mvmvif->ap_sta_id);
815dc1aca22SAndrei Otcheretianski 
816dc1aca22SAndrei Otcheretianski 		if (ap_sta_id < IWL_MVM_STATION_COUNT) {
817dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
818dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
819dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
820dc1aca22SAndrei Otcheretianski 				goto drop;
821dc1aca22SAndrei Otcheretianski 		}
822dc1aca22SAndrei Otcheretianski 	}
823dc1aca22SAndrei Otcheretianski 
824df2378abSJohannes Berg 	iwl_mvm_tx_skb(mvm, skb, sta);
825e705c121SKalle Valo 	return;
826e705c121SKalle Valo  drop:
827e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
828e705c121SKalle Valo }
829e705c121SKalle Valo 
830cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
831e705c121SKalle Valo {
832cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
833cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
834cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
835cfbc6c4cSSara Sharon 
836fba8248eSSara Sharon 	/*
837fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
838fba8248eSSara Sharon 	 * taker all the work.
839fba8248eSSara Sharon 	 *
840fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
841fba8248eSSara Sharon 	 *
842fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
843fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
844fba8248eSSara Sharon 	 *
845fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
846fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
847fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
848fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
849fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
850fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
851fba8248eSSara Sharon 	 *
852fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
853fba8248eSSara Sharon 	 * check the queue, so do nothing.
854fba8248eSSara Sharon 	 */
855fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
856fba8248eSSara Sharon 		return;
857cfbc6c4cSSara Sharon 
858cfbc6c4cSSara Sharon 	rcu_read_lock();
859fba8248eSSara Sharon 	do {
860cfbc6c4cSSara Sharon 		while (likely(!mvmtxq->stopped &&
861cfbc6c4cSSara Sharon 			      (mvm->trans->system_pm_mode ==
862cfbc6c4cSSara Sharon 			       IWL_PLAT_PM_MODE_DISABLED))) {
863cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
864cfbc6c4cSSara Sharon 
865f50d693bSSara Sharon 			if (!skb) {
866f50d693bSSara Sharon 				if (txq->sta)
867f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
868f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
869f50d693bSSara Sharon 						     txq->sta->addr,
870f50d693bSSara Sharon 						     txq->tid);
871cfbc6c4cSSara Sharon 				break;
872f50d693bSSara Sharon 			}
873cfbc6c4cSSara Sharon 
874cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
875cfbc6c4cSSara Sharon 		}
876fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
877cfbc6c4cSSara Sharon 	rcu_read_unlock();
878e705c121SKalle Valo }
879e705c121SKalle Valo 
880cfbc6c4cSSara Sharon static void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
881cfbc6c4cSSara Sharon 				      struct ieee80211_txq *txq)
882e705c121SKalle Valo {
883cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
884cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
885e705c121SKalle Valo 
886cfbc6c4cSSara Sharon 	/*
887cfbc6c4cSSara Sharon 	 * Please note that racing is handled very carefully here:
888cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id is updated during allocation, and mvmtxq->list is
889cfbc6c4cSSara Sharon 	 * deleted afterwards.
890cfbc6c4cSSara Sharon 	 * This means that if:
891cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && list_empty(&mvmtxq->list):
892cfbc6c4cSSara Sharon 	 *	queue is allocated and we can TX.
893cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && !list_empty(&mvmtxq->list):
894cfbc6c4cSSara Sharon 	 *	a race, should defer the frame.
895cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && list_empty(&mvmtxq->list):
896cfbc6c4cSSara Sharon 	 *	need to allocate the queue and defer the frame.
897cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && !list_empty(&mvmtxq->list):
898cfbc6c4cSSara Sharon 	 *	queue is already scheduled for allocation, no need to allocate,
899cfbc6c4cSSara Sharon 	 *	should defer the frame.
900cfbc6c4cSSara Sharon 	 */
901cfbc6c4cSSara Sharon 
902cfbc6c4cSSara Sharon 	/* If the queue is allocated TX and return. */
903cfbc6c4cSSara Sharon 	if (!txq->sta || mvmtxq->txq_id != IWL_MVM_INVALID_QUEUE) {
904cfbc6c4cSSara Sharon 		/*
905cfbc6c4cSSara Sharon 		 * Check that list is empty to avoid a race where txq_id is
906cfbc6c4cSSara Sharon 		 * already updated, but the queue allocation work wasn't
907cfbc6c4cSSara Sharon 		 * finished
908cfbc6c4cSSara Sharon 		 */
909cfbc6c4cSSara Sharon 		if (unlikely(txq->sta && !list_empty(&mvmtxq->list)))
910cfbc6c4cSSara Sharon 			return;
911cfbc6c4cSSara Sharon 
912cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
913cfbc6c4cSSara Sharon 		return;
914cfbc6c4cSSara Sharon 	}
915cfbc6c4cSSara Sharon 
916cfbc6c4cSSara Sharon 	/* The list is being deleted only after the queue is fully allocated. */
917cfbc6c4cSSara Sharon 	if (!list_empty(&mvmtxq->list))
918cfbc6c4cSSara Sharon 		return;
919cfbc6c4cSSara Sharon 
920cfbc6c4cSSara Sharon 	list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
921cfbc6c4cSSara Sharon 	schedule_work(&mvm->add_stream_wk);
922e705c121SKalle Valo }
923e705c121SKalle Valo 
924e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
925e705c121SKalle Valo 	do {								\
926e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
927e705c121SKalle Valo 			break;						\
9287174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
929e705c121SKalle Valo 	} while (0)
930e705c121SKalle Valo 
931e705c121SKalle Valo static void
932e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
933e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
934e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
935e705c121SKalle Valo {
936e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
937e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
938e705c121SKalle Valo 
9396c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
9406c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
9416c042d75SSara Sharon 	if (!trig)
942e705c121SKalle Valo 		return;
943e705c121SKalle Valo 
944e705c121SKalle Valo 	ba_trig = (void *)trig->data;
945e705c121SKalle Valo 
946e705c121SKalle Valo 	switch (action) {
947e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
948e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
949e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
950e705c121SKalle Valo 
951e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
952e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
953e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
954e705c121SKalle Valo 		break;
955e705c121SKalle Valo 		}
956e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
957e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
958e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
959e705c121SKalle Valo 				 sta->addr, tid);
960e705c121SKalle Valo 		break;
961e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
962e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
963e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
964e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
965e705c121SKalle Valo 		break;
966e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
967e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
968e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
969e705c121SKalle Valo 				 sta->addr, tid);
970e705c121SKalle Valo 		break;
971e705c121SKalle Valo 	default:
972e705c121SKalle Valo 		break;
973e705c121SKalle Valo 	}
974e705c121SKalle Valo }
975e705c121SKalle Valo 
976e705c121SKalle Valo static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
977e705c121SKalle Valo 				    struct ieee80211_vif *vif,
97850ea05efSSara Sharon 				    struct ieee80211_ampdu_params *params)
979e705c121SKalle Valo {
980e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
981e705c121SKalle Valo 	int ret;
98250ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
98350ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
98450ea05efSSara Sharon 	u16 tid = params->tid;
98550ea05efSSara Sharon 	u16 *ssn = &params->ssn;
986514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
987bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
98810b2b201SSara Sharon 	u16 timeout = params->timeout;
989e705c121SKalle Valo 
990e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
991e705c121SKalle Valo 		     sta->addr, tid, action);
992e705c121SKalle Valo 
993e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
994e705c121SKalle Valo 		return -EACCES;
995e705c121SKalle Valo 
996e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
997e705c121SKalle Valo 
998e705c121SKalle Valo 	switch (action) {
999e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
1000b0ffe455SJohannes Berg 		if (iwl_mvm_vif_from_mac80211(vif)->ap_sta_id ==
1001b0ffe455SJohannes Berg 				iwl_mvm_sta_from_mac80211(sta)->sta_id) {
1002b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
1003b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
1004b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
1005b0ffe455SJohannes Berg 
1006b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
1007b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
1008b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
1009b0ffe455SJohannes Berg 		}
1010e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
1011e705c121SKalle Valo 			ret = -EINVAL;
1012e705c121SKalle Valo 			break;
1013e705c121SKalle Valo 		}
101410b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
101510b2b201SSara Sharon 					 timeout);
1016e705c121SKalle Valo 		break;
1017e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
101810b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
101910b2b201SSara Sharon 					 timeout);
1020e705c121SKalle Valo 		break;
1021e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
1022e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
1023e705c121SKalle Valo 			ret = -EINVAL;
1024e705c121SKalle Valo 			break;
1025e705c121SKalle Valo 		}
1026e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1027e705c121SKalle Valo 		break;
1028e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
1029e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1030e705c121SKalle Valo 		break;
1031e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1032e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1033e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1034e705c121SKalle Valo 		break;
1035e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1036bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1037bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
1038e705c121SKalle Valo 		break;
1039e705c121SKalle Valo 	default:
1040e705c121SKalle Valo 		WARN_ON_ONCE(1);
1041e705c121SKalle Valo 		ret = -EINVAL;
1042e705c121SKalle Valo 		break;
1043e705c121SKalle Valo 	}
1044e705c121SKalle Valo 
1045e705c121SKalle Valo 	if (!ret) {
1046e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
1047e705c121SKalle Valo 
1048e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1049e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1050e705c121SKalle Valo 
1051e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1052e705c121SKalle Valo 					    rx_ba_ssn, action);
1053e705c121SKalle Valo 	}
1054e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1055e705c121SKalle Valo 
1056e705c121SKalle Valo 	return ret;
1057e705c121SKalle Valo }
1058e705c121SKalle Valo 
1059e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1060e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1061e705c121SKalle Valo {
1062e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1063e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1064e705c121SKalle Valo 
1065e705c121SKalle Valo 	mvmvif->uploaded = false;
10660ae98812SSara Sharon 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1067e705c121SKalle Valo 
1068e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1069e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1070e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1071e705c121SKalle Valo 
1072e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1073e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
107486e177d8SGregory Greenman 	memset(&mvmvif->probe_resp_data, 0, sizeof(mvmvif->probe_resp_data));
1075e705c121SKalle Valo }
1076e705c121SKalle Valo 
1077e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1078e705c121SKalle Valo {
1079fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1080e705c121SKalle Valo 
10819bf13beeSJohannes Berg 	mvm->cur_aid = 0;
10829bf13beeSJohannes Berg 
1083e705c121SKalle Valo 	mvm->scan_status = 0;
1084e705c121SKalle Valo 	mvm->ps_disabled = false;
1085b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1086e705c121SKalle Valo 
1087e705c121SKalle Valo 	/* just in case one was running */
1088305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1089e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1090e705c121SKalle Valo 
1091fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1092fc36ffdaSJohannes Berg 
1093e705c121SKalle Valo 	/*
1094e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1095e705c121SKalle Valo 	 * gone down during the HW restart
1096e705c121SKalle Valo 	 */
1097e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1098e705c121SKalle Valo 
1099e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1100e705c121SKalle Valo 
1101e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
11029c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1103e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1104e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1105e705c121SKalle Valo 
1106e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1107e705c121SKalle Valo 
1108e705c121SKalle Valo 	mvm->vif_count = 0;
1109e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
11107174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1111baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1112e705c121SKalle Valo 
1113e705c121SKalle Valo 	/* keep statistics ticking */
1114e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1115e705c121SKalle Valo }
1116e705c121SKalle Valo 
1117e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1118e705c121SKalle Valo {
1119e705c121SKalle Valo 	int ret;
1120e705c121SKalle Valo 
1121e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1122e705c121SKalle Valo 
1123bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1124bf8b286fSJohannes Berg 		/*
1125bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1126bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1127bf8b286fSJohannes Berg 		 */
1128bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1129bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1130e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1131e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1132a42b2af3SLuca Coelho 	}
1133e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1134e705c121SKalle Valo 
1135b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1136b108d8c7SShahar S Matityahu 			       NULL);
1137b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1138b108d8c7SShahar S Matityahu 			       NULL);
1139da2eb669SSara Sharon 
1140e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1141e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1142e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1143e705c121SKalle Valo 		 * would do.
1144e705c121SKalle Valo 		 */
1145e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1146e705c121SKalle Valo 	}
1147e705c121SKalle Valo 
1148e705c121SKalle Valo 	return ret;
1149e705c121SKalle Valo }
1150e705c121SKalle Valo 
1151e705c121SKalle Valo static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1152e705c121SKalle Valo {
1153e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1154e705c121SKalle Valo 	int ret;
1155e705c121SKalle Valo 
1156e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1157e705c121SKalle Valo 	ret = __iwl_mvm_mac_start(mvm);
1158e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1159e705c121SKalle Valo 
1160e705c121SKalle Valo 	return ret;
1161e705c121SKalle Valo }
1162e705c121SKalle Valo 
1163e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1164e705c121SKalle Valo {
1165e705c121SKalle Valo 	int ret;
1166e705c121SKalle Valo 
1167e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1168e705c121SKalle Valo 
1169e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
11704d4183c4SEmmanuel Grumbach 
1171e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1172e705c121SKalle Valo 	if (ret)
1173e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1174e705c121SKalle Valo 			ret);
1175e705c121SKalle Valo 
1176f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1177f130bb75SMordechay Goodstein 
1178e705c121SKalle Valo 	/*
1179e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1180e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1181e705c121SKalle Valo 	 */
1182e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1183e705c121SKalle Valo 
1184e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1185e705c121SKalle Valo }
1186e705c121SKalle Valo 
1187e705c121SKalle Valo static void
1188e705c121SKalle Valo iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1189e705c121SKalle Valo 			      enum ieee80211_reconfig_type reconfig_type)
1190e705c121SKalle Valo {
1191e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1192e705c121SKalle Valo 
1193e705c121SKalle Valo 	switch (reconfig_type) {
1194e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1195e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1196e705c121SKalle Valo 		break;
1197e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1198e705c121SKalle Valo 		break;
1199e705c121SKalle Valo 	}
1200e705c121SKalle Valo }
1201e705c121SKalle Valo 
1202e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1203e705c121SKalle Valo {
1204e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1205e705c121SKalle Valo 
1206e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1207e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1208e705c121SKalle Valo 	 */
1209e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1210e705c121SKalle Valo 
1211e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1212e705c121SKalle Valo 
1213e705c121SKalle Valo 	/*
1214e705c121SKalle Valo 	 * The work item could be running or queued if the
1215e705c121SKalle Valo 	 * ROC time event stops just as we get here.
1216e705c121SKalle Valo 	 */
1217e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
1218e705c121SKalle Valo 
1219f327236dSSharon 	iwl_mvm_rm_aux_sta(mvm);
1220f327236dSSharon 
1221fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1222e705c121SKalle Valo 
1223e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1224e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1225e705c121SKalle Valo 
1226e705c121SKalle Valo 	/*
1227155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1228155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1229155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1230e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1231e705c121SKalle Valo 	 * HW restart was requested.
1232e705c121SKalle Valo 	 */
1233155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1234155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1235155f7e04SEmmanuel Grumbach 			       &mvm->status))
1236e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1237e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1238e705c121SKalle Valo 
1239e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1240e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1241e705c121SKalle Valo 	 */
1242e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1243e705c121SKalle Valo 		int i;
1244e705c121SKalle Valo 
1245e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1246e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1247e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1248e705c121SKalle Valo 				      i))
1249e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1250e705c121SKalle Valo 		}
1251e705c121SKalle Valo 	}
1252e705c121SKalle Valo }
1253e705c121SKalle Valo 
1254e705c121SKalle Valo static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1255e705c121SKalle Valo {
1256e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1257e705c121SKalle Valo 
1258e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
125924afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1260771147b0SJohannes Berg 
1261771147b0SJohannes Berg 	/*
1262771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1263771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1264771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
12657174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1266771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1267771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1268771147b0SJohannes Berg 	 */
1269771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1270771147b0SJohannes Berg 
1271d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
127269e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
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 {
13020791c2fcSHaim Dreyfuss 	int len;
13030791c2fcSHaim Dreyfuss 	union {
13040791c2fcSHaim Dreyfuss 		struct iwl_dev_tx_power_cmd v5;
13050791c2fcSHaim Dreyfuss 		struct iwl_dev_tx_power_cmd_v4 v4;
13060791c2fcSHaim Dreyfuss 	} cmd = {
13070791c2fcSHaim Dreyfuss 		.v5.v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
13080791c2fcSHaim Dreyfuss 		.v5.v3.mac_context_id =
1309e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
13100791c2fcSHaim Dreyfuss 		.v5.v3.pwr_restriction = cpu_to_le16(8 * tx_power),
1311e705c121SKalle Valo 	};
1312e705c121SKalle Valo 
1313e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
13140791c2fcSHaim Dreyfuss 		cmd.v5.v3.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1315e705c121SKalle Valo 
13160791c2fcSHaim Dreyfuss 	if (fw_has_api(&mvm->fw->ucode_capa,
13170791c2fcSHaim Dreyfuss 		       IWL_UCODE_TLV_API_REDUCE_TX_POWER))
13180791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
13190791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
13200791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
13210791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
13220791c2fcSHaim Dreyfuss 	else
13230791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4.v3);
1324e705c121SKalle Valo 
1325e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
1326e705c121SKalle Valo }
1327e705c121SKalle Valo 
1328f6780614SSara Sharon static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1329f6780614SSara Sharon 				       struct ieee80211_vif *vif)
1330f6780614SSara Sharon {
1331f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1332f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1333f6780614SSara Sharon 	int ret;
1334f6780614SSara Sharon 
1335f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1336f6780614SSara Sharon 
1337f6780614SSara Sharon 	if (mvmvif->csa_failed) {
1338f6780614SSara Sharon 		mvmvif->csa_failed = false;
1339f6780614SSara Sharon 		ret = -EIO;
1340f6780614SSara Sharon 		goto out_unlock;
1341f6780614SSara Sharon 	}
1342f6780614SSara Sharon 
1343f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1344f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
1345f6780614SSara Sharon 
1346f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
1347f6780614SSara Sharon 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
1348f6780614SSara Sharon 							  mvmvif->ap_sta_id);
1349f6780614SSara Sharon 
1350f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1351f6780614SSara Sharon 			ret = -EIO;
1352f6780614SSara Sharon 			goto out_unlock;
1353f6780614SSara Sharon 		}
1354f6780614SSara Sharon 
13550202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
13560202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
1357f6780614SSara Sharon 			iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1358f6780614SSara Sharon 
1359f6780614SSara Sharon 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1360f6780614SSara Sharon 
13610202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
13620202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1363f6780614SSara Sharon 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1364f6780614SSara Sharon 			if (ret)
1365f6780614SSara Sharon 				goto out_unlock;
1366f6780614SSara Sharon 
1367f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1368f6780614SSara Sharon 		}
13690202bcf0SEmmanuel Grumbach 	}
1370f6780614SSara Sharon 
1371f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1372f6780614SSara Sharon 
1373f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1374f6780614SSara Sharon 
1375f6780614SSara Sharon out_unlock:
1376f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1377f6780614SSara Sharon 
1378f6780614SSara Sharon 	return ret;
1379f6780614SSara Sharon }
1380f6780614SSara Sharon 
1381f6780614SSara Sharon static void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1382f6780614SSara Sharon 					 struct ieee80211_vif *vif)
1383f6780614SSara Sharon {
1384f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1385f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1386f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1387f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1388f6780614SSara Sharon 							  mvmvif->color)),
1389f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1390f6780614SSara Sharon 	};
1391f6780614SSara Sharon 
1392f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1393f6780614SSara Sharon 
1394f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1395f6780614SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1396f6780614SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
1397f6780614SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
1398f6780614SSara Sharon 				     0, sizeof(cmd), &cmd));
1399f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1400f6780614SSara Sharon 
1401f6780614SSara Sharon 	WARN_ON(iwl_mvm_post_channel_switch(hw, vif));
1402f6780614SSara Sharon }
1403f6780614SSara Sharon 
1404f6780614SSara Sharon static void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1405f6780614SSara Sharon {
1406f6780614SSara Sharon 	struct iwl_mvm *mvm;
1407f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1408f6780614SSara Sharon 	struct ieee80211_vif *vif;
1409f6780614SSara Sharon 
1410f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1411f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1412f6780614SSara Sharon 	mvm = mvmvif->mvm;
1413f6780614SSara Sharon 
1414f6780614SSara Sharon 	iwl_mvm_abort_channel_switch(mvm->hw, vif);
1415f6780614SSara Sharon 	ieee80211_chswitch_done(vif, false);
1416f6780614SSara Sharon }
1417f6780614SSara Sharon 
1418e705c121SKalle Valo static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1419e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1420e705c121SKalle Valo {
1421e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1422e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1423e705c121SKalle Valo 	int ret;
1424e705c121SKalle Valo 
1425e705c121SKalle Valo 	mvmvif->mvm = mvm;
142686e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
1427e705c121SKalle Valo 
1428e705c121SKalle Valo 	/*
1429e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1430e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1431e705c121SKalle Valo 	 * don't really have to check the types.
1432e705c121SKalle Valo 	 */
1433e705c121SKalle Valo 
1434e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1435e705c121SKalle Valo 
1436e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1437e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1438e705c121SKalle Valo 		mvmvif->beacon_stats.accu_num_beacons +=
1439e705c121SKalle Valo 			mvmvif->beacon_stats.num_beacons;
1440e705c121SKalle Valo 
1441e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
1442e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1443e705c121SKalle Valo 	if (ret)
1444e705c121SKalle Valo 		goto out_unlock;
1445e705c121SKalle Valo 
1446698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1447698478c4SSara Sharon 
1448e705c121SKalle Valo 	/* Counting number of interfaces is needed for legacy PM */
1449e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1450e705c121SKalle Valo 		mvm->vif_count++;
1451e705c121SKalle Valo 
1452e705c121SKalle Valo 	/*
1453e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1454e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1455e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1456e705c121SKalle Valo 	 * only after the binding.
1457e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1458e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1459e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1460e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1461e705c121SKalle Valo 	 * allocating resources :)
1462e705c121SKalle Valo 	 */
1463e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1464e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1465e705c121SKalle Valo 		ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1466e705c121SKalle Valo 		if (ret) {
1467e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1468e705c121SKalle Valo 			goto out_release;
1469e705c121SKalle Valo 		}
1470e705c121SKalle Valo 
147126d6c16bSSara Sharon 		/*
147226d6c16bSSara Sharon 		 * Only queue for this station is the mcast queue,
147326d6c16bSSara Sharon 		 * which shouldn't be in TFD mask anyway
147426d6c16bSSara Sharon 		 */
147526d6c16bSSara Sharon 		ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta,
1476ced19f26SSara Sharon 					       0, vif->type,
1477ced19f26SSara Sharon 					       IWL_STA_MULTICAST);
147826d6c16bSSara Sharon 		if (ret)
147926d6c16bSSara Sharon 			goto out_release;
148026d6c16bSSara Sharon 
1481e705c121SKalle Valo 		iwl_mvm_vif_dbgfs_register(mvm, vif);
1482e705c121SKalle Valo 		goto out_unlock;
1483e705c121SKalle Valo 	}
1484e705c121SKalle Valo 
1485e705c121SKalle Valo 	mvmvif->features |= hw->netdev_features;
1486e705c121SKalle Valo 
1487e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1488e705c121SKalle Valo 	if (ret)
1489e705c121SKalle Valo 		goto out_release;
1490e705c121SKalle Valo 
1491e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1492e705c121SKalle Valo 	if (ret)
1493e705c121SKalle Valo 		goto out_remove_mac;
1494e705c121SKalle Valo 
1495e705c121SKalle Valo 	/* beacon filtering */
1496e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1497e705c121SKalle Valo 	if (ret)
1498e705c121SKalle Valo 		goto out_remove_mac;
1499e705c121SKalle Valo 
1500e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1501e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1502e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1503e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1504e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1505e705c121SKalle Valo 	}
1506e705c121SKalle Valo 
1507e705c121SKalle Valo 	/*
1508e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1509e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1510e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1511e705c121SKalle Valo 	 */
1512e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1513e705c121SKalle Valo 
1514e705c121SKalle Valo 		mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1515e705c121SKalle Valo 		if (!mvmvif->phy_ctxt) {
1516e705c121SKalle Valo 			ret = -ENOSPC;
1517e705c121SKalle Valo 			goto out_free_bf;
1518e705c121SKalle Valo 		}
1519e705c121SKalle Valo 
1520e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1521e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1522e705c121SKalle Valo 		if (ret)
1523e705c121SKalle Valo 			goto out_unref_phy;
1524e705c121SKalle Valo 
1525d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1526e705c121SKalle Valo 		if (ret)
1527e705c121SKalle Valo 			goto out_unbind;
1528e705c121SKalle Valo 
1529e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1530e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1531e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1532e705c121SKalle Valo 	}
1533e705c121SKalle Valo 
1534b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1535f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1536f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1537b0ffe455SJohannes Berg 
1538baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1539baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
1540baf41bc3SShaul Triebitz 
1541e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
1542e705c121SKalle Valo 	goto out_unlock;
1543e705c121SKalle Valo 
1544e705c121SKalle Valo  out_unbind:
1545e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1546e705c121SKalle Valo  out_unref_phy:
1547e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1548e705c121SKalle Valo  out_free_bf:
1549e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1550e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1551e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1552e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1553e705c121SKalle Valo 	}
1554e705c121SKalle Valo  out_remove_mac:
1555e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1556e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1557e705c121SKalle Valo  out_release:
1558e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1559e705c121SKalle Valo 		mvm->vif_count--;
1560e705c121SKalle Valo  out_unlock:
1561e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1562e705c121SKalle Valo 
1563e705c121SKalle Valo 	return ret;
1564e705c121SKalle Valo }
1565e705c121SKalle Valo 
1566e705c121SKalle Valo static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1567e705c121SKalle Valo 					struct ieee80211_vif *vif)
1568e705c121SKalle Valo {
1569e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1570e705c121SKalle Valo 		/*
1571e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1572e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1573e705c121SKalle Valo 		 * queue are sent in ROC session.
1574e705c121SKalle Valo 		 */
1575e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1576e705c121SKalle Valo 	}
1577e705c121SKalle Valo }
1578e705c121SKalle Valo 
1579e705c121SKalle Valo static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1580e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1581e705c121SKalle Valo {
1582e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1583e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
158486e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1585e705c121SKalle Valo 
1586e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1587e705c121SKalle Valo 
1588b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1589b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1590b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1591b0ffe455SJohannes Berg 
1592e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1593e705c121SKalle Valo 
159486e177d8SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->probe_resp_data,
159586e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
159686e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
159786e177d8SGregory Greenman 	if (probe_data)
159886e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
159986e177d8SGregory Greenman 
1600e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1601e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1602e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1603e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1604e705c121SKalle Valo 	}
1605e705c121SKalle Valo 
1606b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1607b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1608b73f9a4aSJohannes Berg 
1609e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1610e705c121SKalle Valo 
1611e705c121SKalle Valo 	/*
1612e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1613e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1614e705c121SKalle Valo 	 */
1615e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1616e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1617e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1618e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1619e705c121SKalle Valo 			mvm->noa_vif = NULL;
1620e705c121SKalle Valo 			mvm->noa_duration = 0;
1621e705c121SKalle Valo 		}
1622e705c121SKalle Valo #endif
162326d6c16bSSara Sharon 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
1624e705c121SKalle Valo 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1625e705c121SKalle Valo 		goto out_release;
1626e705c121SKalle Valo 	}
1627e705c121SKalle Valo 
1628e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1629e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1630d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1631e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1632e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1633e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
1634e705c121SKalle Valo 	}
1635e705c121SKalle Valo 
1636e705c121SKalle Valo 	if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1637e705c121SKalle Valo 		mvm->vif_count--;
1638e705c121SKalle Valo 
1639e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
1640e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1641e705c121SKalle Valo 
1642698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1643698478c4SSara Sharon 
1644baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1645baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1646baf41bc3SShaul Triebitz 
1647e705c121SKalle Valo out_release:
1648e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1649e705c121SKalle Valo }
1650e705c121SKalle Valo 
1651e705c121SKalle Valo static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1652e705c121SKalle Valo {
1653e705c121SKalle Valo 	return 0;
1654e705c121SKalle Valo }
1655e705c121SKalle Valo 
1656e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1657e705c121SKalle Valo 	struct iwl_mvm *mvm;
1658e705c121SKalle Valo 	int port_id;
1659e705c121SKalle Valo };
1660e705c121SKalle Valo 
1661e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1662e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1663e705c121SKalle Valo {
1664e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1665e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1666e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
166797bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
166897bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
166997bce57bSLuca Coelho 		.flags = CMD_ASYNC,
167097bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
167197bce57bSLuca Coelho 	};
1672e705c121SKalle Valo 	int ret, len;
1673e705c121SKalle Valo 
1674e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1675e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1676e705c121SKalle Valo 		return;
1677e705c121SKalle Valo 
1678e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1679e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1680e705c121SKalle Valo 		return;
1681e705c121SKalle Valo 
1682e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1683e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1684e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1685e705c121SKalle Valo 
168697bce57bSLuca Coelho 	hcmd.len[0] = len;
168797bce57bSLuca Coelho 	hcmd.data[0] = cmd;
168897bce57bSLuca Coelho 
168997bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1690e705c121SKalle Valo 	if (ret)
1691e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1692e705c121SKalle Valo }
1693e705c121SKalle Valo 
1694e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1695e705c121SKalle Valo {
1696e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1697e705c121SKalle Valo 		.mvm = mvm,
1698e705c121SKalle Valo 	};
1699e705c121SKalle Valo 
1700e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1701e705c121SKalle Valo 
1702e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1703e705c121SKalle Valo 		return;
1704e705c121SKalle Valo 
1705e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1706e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1707e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1708e705c121SKalle Valo }
1709e705c121SKalle Valo 
1710e705c121SKalle Valo static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1711e705c121SKalle Valo 				     struct netdev_hw_addr_list *mc_list)
1712e705c121SKalle Valo {
1713e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1714e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1715e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1716e705c121SKalle Valo 	int addr_count;
1717e705c121SKalle Valo 	bool pass_all;
1718e705c121SKalle Valo 	int len;
1719e705c121SKalle Valo 
1720e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1721e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1722e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1723e705c121SKalle Valo 	if (pass_all)
1724e705c121SKalle Valo 		addr_count = 0;
1725e705c121SKalle Valo 
1726e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1727e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1728e705c121SKalle Valo 	if (!cmd)
1729e705c121SKalle Valo 		return 0;
1730e705c121SKalle Valo 
1731e705c121SKalle Valo 	if (pass_all) {
1732e705c121SKalle Valo 		cmd->pass_all = 1;
1733e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1734e705c121SKalle Valo 	}
1735e705c121SKalle Valo 
1736e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1737e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1738e705c121SKalle Valo 				   cmd->count, addr->addr);
1739e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1740e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1741e705c121SKalle Valo 		cmd->count++;
1742e705c121SKalle Valo 	}
1743e705c121SKalle Valo 
1744e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1745e705c121SKalle Valo }
1746e705c121SKalle Valo 
1747e705c121SKalle Valo static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1748e705c121SKalle Valo 				     unsigned int changed_flags,
1749e705c121SKalle Valo 				     unsigned int *total_flags,
1750e705c121SKalle Valo 				     u64 multicast)
1751e705c121SKalle Valo {
1752e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1753e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1754e705c121SKalle Valo 
1755e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1756e705c121SKalle Valo 
1757e705c121SKalle Valo 	/* replace previous configuration */
1758e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1759e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1760e705c121SKalle Valo 
1761e705c121SKalle Valo 	if (!cmd)
1762e705c121SKalle Valo 		goto out;
1763e705c121SKalle Valo 
176461e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
176561e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
176661e7d91bSLuca Coelho 
176761e7d91bSLuca Coelho 	if (cmd->pass_all)
176861e7d91bSLuca Coelho 		cmd->count = 0;
176961e7d91bSLuca Coelho 
1770e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1771e705c121SKalle Valo out:
1772e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1773e705c121SKalle Valo 	*total_flags = 0;
1774e705c121SKalle Valo }
1775e705c121SKalle Valo 
1776e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1777e705c121SKalle Valo 					struct ieee80211_vif *vif,
1778e705c121SKalle Valo 					unsigned int filter_flags,
1779e705c121SKalle Valo 					unsigned int changed_flags)
1780e705c121SKalle Valo {
1781e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1782e705c121SKalle Valo 
1783e705c121SKalle Valo 	/* We support only filter for probe requests */
1784e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1785e705c121SKalle Valo 		return;
1786e705c121SKalle Valo 
1787e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1788e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1789e705c121SKalle Valo 	    !vif->p2p)
1790e705c121SKalle Valo 		return;
1791e705c121SKalle Valo 
1792e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1793e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1794e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1795e705c121SKalle Valo }
1796e705c121SKalle Valo 
1797e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1798e705c121SKalle Valo struct iwl_bcast_iter_data {
1799e705c121SKalle Valo 	struct iwl_mvm *mvm;
1800e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd;
1801e705c121SKalle Valo 	u8 current_filter;
1802e705c121SKalle Valo };
1803e705c121SKalle Valo 
1804e705c121SKalle Valo static void
1805e705c121SKalle Valo iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1806e705c121SKalle Valo 			 const struct iwl_fw_bcast_filter *in_filter,
1807e705c121SKalle Valo 			 struct iwl_fw_bcast_filter *out_filter)
1808e705c121SKalle Valo {
1809e705c121SKalle Valo 	struct iwl_fw_bcast_filter_attr *attr;
1810e705c121SKalle Valo 	int i;
1811e705c121SKalle Valo 
1812e705c121SKalle Valo 	memcpy(out_filter, in_filter, sizeof(*out_filter));
1813e705c121SKalle Valo 
1814e705c121SKalle Valo 	for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1815e705c121SKalle Valo 		attr = &out_filter->attrs[i];
1816e705c121SKalle Valo 
1817e705c121SKalle Valo 		if (!attr->mask)
1818e705c121SKalle Valo 			break;
1819e705c121SKalle Valo 
1820e705c121SKalle Valo 		switch (attr->reserved1) {
1821e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_IP):
1822e705c121SKalle Valo 			if (vif->bss_conf.arp_addr_cnt != 1) {
1823e705c121SKalle Valo 				attr->mask = 0;
1824e705c121SKalle Valo 				continue;
1825e705c121SKalle Valo 			}
1826e705c121SKalle Valo 
1827e705c121SKalle Valo 			attr->val = vif->bss_conf.arp_addr_list[0];
1828e705c121SKalle Valo 			break;
1829e705c121SKalle Valo 		case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1830e705c121SKalle Valo 			attr->val = *(__be32 *)&vif->addr[2];
1831e705c121SKalle Valo 			break;
1832e705c121SKalle Valo 		default:
1833e705c121SKalle Valo 			break;
1834e705c121SKalle Valo 		}
1835e705c121SKalle Valo 		attr->reserved1 = 0;
1836e705c121SKalle Valo 		out_filter->num_attrs++;
1837e705c121SKalle Valo 	}
1838e705c121SKalle Valo }
1839e705c121SKalle Valo 
1840e705c121SKalle Valo static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1841e705c121SKalle Valo 					  struct ieee80211_vif *vif)
1842e705c121SKalle Valo {
1843e705c121SKalle Valo 	struct iwl_bcast_iter_data *data = _data;
1844e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1845e705c121SKalle Valo 	struct iwl_bcast_filter_cmd *cmd = data->cmd;
1846e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1847e705c121SKalle Valo 	struct iwl_fw_bcast_mac *bcast_mac;
1848e705c121SKalle Valo 	int i;
1849e705c121SKalle Valo 
1850e705c121SKalle Valo 	if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1851e705c121SKalle Valo 		return;
1852e705c121SKalle Valo 
1853e705c121SKalle Valo 	bcast_mac = &cmd->macs[mvmvif->id];
1854e705c121SKalle Valo 
1855e705c121SKalle Valo 	/*
1856e705c121SKalle Valo 	 * enable filtering only for associated stations, but not for P2P
1857e705c121SKalle Valo 	 * Clients
1858e705c121SKalle Valo 	 */
1859e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1860e705c121SKalle Valo 	    !vif->bss_conf.assoc)
1861e705c121SKalle Valo 		return;
1862e705c121SKalle Valo 
1863e705c121SKalle Valo 	bcast_mac->default_discard = 1;
1864e705c121SKalle Valo 
1865e705c121SKalle Valo 	/* copy all configured filters */
1866e705c121SKalle Valo 	for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1867e705c121SKalle Valo 		/*
1868e705c121SKalle Valo 		 * Make sure we don't exceed our filters limit.
1869e705c121SKalle Valo 		 * if there is still a valid filter to be configured,
1870e705c121SKalle Valo 		 * be on the safe side and just allow bcast for this mac.
1871e705c121SKalle Valo 		 */
1872e705c121SKalle Valo 		if (WARN_ON_ONCE(data->current_filter >=
1873e705c121SKalle Valo 				 ARRAY_SIZE(cmd->filters))) {
1874e705c121SKalle Valo 			bcast_mac->default_discard = 0;
1875e705c121SKalle Valo 			bcast_mac->attached_filters = 0;
1876e705c121SKalle Valo 			break;
1877e705c121SKalle Valo 		}
1878e705c121SKalle Valo 
1879e705c121SKalle Valo 		iwl_mvm_set_bcast_filter(vif,
1880e705c121SKalle Valo 					 &mvm->bcast_filters[i],
1881e705c121SKalle Valo 					 &cmd->filters[data->current_filter]);
1882e705c121SKalle Valo 
1883e705c121SKalle Valo 		/* skip current filter if it contains no attributes */
1884e705c121SKalle Valo 		if (!cmd->filters[data->current_filter].num_attrs)
1885e705c121SKalle Valo 			continue;
1886e705c121SKalle Valo 
1887e705c121SKalle Valo 		/* attach the filter to current mac */
1888e705c121SKalle Valo 		bcast_mac->attached_filters |=
1889e705c121SKalle Valo 				cpu_to_le16(BIT(data->current_filter));
1890e705c121SKalle Valo 
1891e705c121SKalle Valo 		data->current_filter++;
1892e705c121SKalle Valo 	}
1893e705c121SKalle Valo }
1894e705c121SKalle Valo 
1895e705c121SKalle Valo bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1896e705c121SKalle Valo 				    struct iwl_bcast_filter_cmd *cmd)
1897e705c121SKalle Valo {
1898e705c121SKalle Valo 	struct iwl_bcast_iter_data iter_data = {
1899e705c121SKalle Valo 		.mvm = mvm,
1900e705c121SKalle Valo 		.cmd = cmd,
1901e705c121SKalle Valo 	};
1902e705c121SKalle Valo 
1903e705c121SKalle Valo 	if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1904e705c121SKalle Valo 		return false;
1905e705c121SKalle Valo 
1906e705c121SKalle Valo 	memset(cmd, 0, sizeof(*cmd));
1907e705c121SKalle Valo 	cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1908e705c121SKalle Valo 	cmd->max_macs = ARRAY_SIZE(cmd->macs);
1909e705c121SKalle Valo 
1910e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
1911e705c121SKalle Valo 	/* use debugfs filters/macs if override is configured */
1912e705c121SKalle Valo 	if (mvm->dbgfs_bcast_filtering.override) {
1913e705c121SKalle Valo 		memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1914e705c121SKalle Valo 		       sizeof(cmd->filters));
1915e705c121SKalle Valo 		memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1916e705c121SKalle Valo 		       sizeof(cmd->macs));
1917e705c121SKalle Valo 		return true;
1918e705c121SKalle Valo 	}
1919e705c121SKalle Valo #endif
1920e705c121SKalle Valo 
1921e705c121SKalle Valo 	/* if no filters are configured, do nothing */
1922e705c121SKalle Valo 	if (!mvm->bcast_filters)
1923e705c121SKalle Valo 		return false;
1924e705c121SKalle Valo 
1925e705c121SKalle Valo 	/* configure and attach these filters for each associated sta vif */
1926e705c121SKalle Valo 	ieee80211_iterate_active_interfaces(
1927e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1928e705c121SKalle Valo 		iwl_mvm_bcast_filter_iterator, &iter_data);
1929e705c121SKalle Valo 
1930e705c121SKalle Valo 	return true;
1931e705c121SKalle Valo }
193234672bb3SEliad Peller 
193334672bb3SEliad Peller static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1934e705c121SKalle Valo {
1935e705c121SKalle Valo 	struct iwl_bcast_filter_cmd cmd;
1936e705c121SKalle Valo 
1937e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1938e705c121SKalle Valo 		return 0;
1939e705c121SKalle Valo 
1940e705c121SKalle Valo 	if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1941e705c121SKalle Valo 		return 0;
1942e705c121SKalle Valo 
1943e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1944e705c121SKalle Valo 				    sizeof(cmd), &cmd);
1945e705c121SKalle Valo }
1946e705c121SKalle Valo #else
194734672bb3SEliad Peller static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1948e705c121SKalle Valo {
1949e705c121SKalle Valo 	return 0;
1950e705c121SKalle Valo }
1951e705c121SKalle Valo #endif
1952e705c121SKalle Valo 
1953a07a8f37SSara Sharon static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
1954a07a8f37SSara Sharon 				    struct ieee80211_vif *vif)
1955a07a8f37SSara Sharon {
1956a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1957a07a8f37SSara Sharon 
1958a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1959a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1960a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1961a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1962a07a8f37SSara Sharon 
1963a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1964a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1965a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1966a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1967a07a8f37SSara Sharon }
1968a07a8f37SSara Sharon 
1969f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1970f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1971f92659a1SSara Sharon {
1972f92659a1SSara Sharon 	if (vif->mu_mimo_owner) {
1973f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1974f92659a1SSara Sharon 
1975f92659a1SSara Sharon 		/*
1976f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1977f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1978f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
1979f92659a1SSara Sharon 		 */
1980f92659a1SSara Sharon 		ieee80211_update_mu_groups(vif,
1981f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
1982f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
1983f92659a1SSara Sharon 	}
1984f92659a1SSara Sharon }
1985f92659a1SSara Sharon 
1986f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1987f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
1988f92659a1SSara Sharon {
1989f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1990f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1991f92659a1SSara Sharon 
1992f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
1993f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1994f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
1995f92659a1SSara Sharon }
1996f92659a1SSara Sharon 
1997514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1998514c3069SLuca Coelho {
1999514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
2000514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
2001514c3069SLuca Coelho 	u8 residue_bits;
2002514c3069SLuca Coelho 	u8 res;
2003514c3069SLuca Coelho 
2004514c3069SLuca Coelho 	if (bit_num <= 5)
2005514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
2006514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2007514c3069SLuca Coelho 
2008514c3069SLuca Coelho 	/*
2009514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
2010514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
2011514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
2012514c3069SLuca Coelho 	 */
2013514c3069SLuca Coelho 
2014514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
2015514c3069SLuca Coelho 
2016514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
2017514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2018514c3069SLuca Coelho 	      residue_bits;
2019514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2020514c3069SLuca Coelho 
2021514c3069SLuca Coelho 	return res;
2022514c3069SLuca Coelho }
2023514c3069SLuca Coelho 
2024514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2025514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2026514c3069SLuca Coelho {
2027514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2028514c3069SLuca Coelho 	struct iwl_he_sta_context_cmd sta_ctxt_cmd = {
2029514c3069SLuca Coelho 		.sta_id = sta_id,
2030514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2031dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2032514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2033514c3069SLuca Coelho 		.frame_time_rts_th =
2034514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2035514c3069SLuca Coelho 	};
2036d14ae796SSara Sharon 	int size = fw_has_api(&mvm->fw->ucode_capa,
2037d14ae796SSara Sharon 			      IWL_UCODE_TLV_API_MBSSID_HE) ?
2038d14ae796SSara Sharon 		   sizeof(sta_ctxt_cmd) :
2039d14ae796SSara Sharon 		   sizeof(struct iwl_he_sta_context_cmd_v1);
2040514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2041514c3069SLuca Coelho 	u32 flags;
2042514c3069SLuca Coelho 	int i;
2043514c3069SLuca Coelho 
2044514c3069SLuca Coelho 	rcu_read_lock();
2045514c3069SLuca Coelho 
2046514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
204712d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2048514c3069SLuca Coelho 		rcu_read_unlock();
2049514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2050514c3069SLuca Coelho 		return;
2051514c3069SLuca Coelho 	}
2052514c3069SLuca Coelho 
2053514c3069SLuca Coelho 	if (!sta->he_cap.has_he) {
2054514c3069SLuca Coelho 		rcu_read_unlock();
2055514c3069SLuca Coelho 		return;
2056514c3069SLuca Coelho 	}
2057514c3069SLuca Coelho 
2058514c3069SLuca Coelho 	flags = 0;
2059514c3069SLuca Coelho 
20604f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
20614f58121dSIlan Peer 	if (mvmvif->he_ru_2mhz_block)
20624f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
20634f58121dSIlan Peer 
2064514c3069SLuca Coelho 	/* HTC flags */
2065514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[0] &
2066514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP0_HTC_HE)
2067514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2068514c3069SLuca Coelho 	if ((sta->he_cap.he_cap_elem.mac_cap_info[1] &
2069514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2070514c3069SLuca Coelho 	    (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2071514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2072514c3069SLuca Coelho 		u8 link_adap =
2073514c3069SLuca Coelho 			((sta->he_cap.he_cap_elem.mac_cap_info[2] &
2074514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2075514c3069SLuca Coelho 			 (sta->he_cap.he_cap_elem.mac_cap_info[1] &
2076514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2077514c3069SLuca Coelho 
2078514c3069SLuca Coelho 		if (link_adap == 2)
2079514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2080514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2081514c3069SLuca Coelho 		else if (link_adap == 3)
2082514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2083514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2084514c3069SLuca Coelho 	}
2085514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2086514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2087514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[3] &
2088514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2089514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2090514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2091514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2092514c3069SLuca Coelho 
2093189b8d44SNaftali Goldstein 	/*
2094189b8d44SNaftali Goldstein 	 * Initialize the PPE thresholds to "None" (7), as described in Table
2095189b8d44SNaftali Goldstein 	 * 9-262ac of 80211.ax/D3.0.
2096189b8d44SNaftali Goldstein 	 */
2097189b8d44SNaftali Goldstein 	memset(&sta_ctxt_cmd.pkt_ext, 7, sizeof(sta_ctxt_cmd.pkt_ext));
2098189b8d44SNaftali Goldstein 
2099189b8d44SNaftali Goldstein 	/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2100514c3069SLuca Coelho 	if (sta->he_cap.he_cap_elem.phy_cap_info[6] &
2101514c3069SLuca Coelho 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2102514c3069SLuca Coelho 		u8 nss = (sta->he_cap.ppe_thres[0] &
2103514c3069SLuca Coelho 			  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2104514c3069SLuca Coelho 		u8 ru_index_bitmap =
2105514c3069SLuca Coelho 			(sta->he_cap.ppe_thres[0] &
2106514c3069SLuca Coelho 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2107514c3069SLuca Coelho 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2108514c3069SLuca Coelho 		u8 *ppe = &sta->he_cap.ppe_thres[0];
2109514c3069SLuca Coelho 		u8 ppe_pos_bit = 7; /* Starting after PPE header */
2110514c3069SLuca Coelho 
2111514c3069SLuca Coelho 		/*
2112514c3069SLuca Coelho 		 * FW currently supports only nss == MAX_HE_SUPP_NSS
2113514c3069SLuca Coelho 		 *
2114514c3069SLuca Coelho 		 * If nss > MAX: we can ignore values we don't support
2115514c3069SLuca Coelho 		 * If nss < MAX: we can set zeros in other streams
2116514c3069SLuca Coelho 		 */
2117514c3069SLuca Coelho 		if (nss > MAX_HE_SUPP_NSS) {
2118514c3069SLuca Coelho 			IWL_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2119514c3069SLuca Coelho 				 MAX_HE_SUPP_NSS);
2120514c3069SLuca Coelho 			nss = MAX_HE_SUPP_NSS;
2121514c3069SLuca Coelho 		}
2122514c3069SLuca Coelho 
2123514c3069SLuca Coelho 		for (i = 0; i < nss; i++) {
2124514c3069SLuca Coelho 			u8 ru_index_tmp = ru_index_bitmap << 1;
2125514c3069SLuca Coelho 			u8 bw;
2126514c3069SLuca Coelho 
2127514c3069SLuca Coelho 			for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX; bw++) {
2128514c3069SLuca Coelho 				ru_index_tmp >>= 1;
2129514c3069SLuca Coelho 				if (!(ru_index_tmp & 1))
2130514c3069SLuca Coelho 					continue;
2131514c3069SLuca Coelho 
2132514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][1] =
2133514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2134514c3069SLuca Coelho 							       ppe_pos_bit);
2135514c3069SLuca Coelho 				ppe_pos_bit +=
2136514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2137514c3069SLuca Coelho 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0] =
2138514c3069SLuca Coelho 					iwl_mvm_he_get_ppe_val(ppe,
2139514c3069SLuca Coelho 							       ppe_pos_bit);
2140514c3069SLuca Coelho 				ppe_pos_bit +=
2141514c3069SLuca Coelho 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2142514c3069SLuca Coelho 			}
2143514c3069SLuca Coelho 		}
2144514c3069SLuca Coelho 
2145514c3069SLuca Coelho 		flags |= STA_CTXT_HE_PACKET_EXT;
214677ff2c6bSLiad Kaufman 	} else if ((sta->he_cap.he_cap_elem.phy_cap_info[9] &
214777ff2c6bSLiad Kaufman 		    IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) !=
214877ff2c6bSLiad Kaufman 		  IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_RESERVED) {
214977ff2c6bSLiad Kaufman 		int low_th = -1;
215077ff2c6bSLiad Kaufman 		int high_th = -1;
215177ff2c6bSLiad Kaufman 
215277ff2c6bSLiad Kaufman 		/* Take the PPE thresholds from the nominal padding info */
215377ff2c6bSLiad Kaufman 		switch (sta->he_cap.he_cap_elem.phy_cap_info[9] &
215477ff2c6bSLiad Kaufman 			IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) {
215577ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_0US:
215677ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
215777ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
215877ff2c6bSLiad Kaufman 			break;
215977ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_8US:
216077ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_BPSK;
216177ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_NONE;
216277ff2c6bSLiad Kaufman 			break;
216377ff2c6bSLiad Kaufman 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US:
216477ff2c6bSLiad Kaufman 			low_th = IWL_HE_PKT_EXT_NONE;
216577ff2c6bSLiad Kaufman 			high_th = IWL_HE_PKT_EXT_BPSK;
216677ff2c6bSLiad Kaufman 			break;
216777ff2c6bSLiad Kaufman 		}
216877ff2c6bSLiad Kaufman 
216977ff2c6bSLiad Kaufman 		/* Set the PPE thresholds accordingly */
217077ff2c6bSLiad Kaufman 		if (low_th >= 0 && high_th >= 0) {
21710bfefe2fSLiad Kaufman 			struct iwl_he_pkt_ext *pkt_ext =
21720bfefe2fSLiad Kaufman 				(struct iwl_he_pkt_ext *)&sta_ctxt_cmd.pkt_ext;
217377ff2c6bSLiad Kaufman 
217477ff2c6bSLiad Kaufman 			for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
217577ff2c6bSLiad Kaufman 				u8 bw;
217677ff2c6bSLiad Kaufman 
217777ff2c6bSLiad Kaufman 				for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX;
217877ff2c6bSLiad Kaufman 				     bw++) {
21790bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][0] =
21800bfefe2fSLiad Kaufman 						low_th;
21810bfefe2fSLiad Kaufman 					pkt_ext->pkt_ext_qam_th[i][bw][1] =
21820bfefe2fSLiad Kaufman 						high_th;
218377ff2c6bSLiad Kaufman 				}
218477ff2c6bSLiad Kaufman 			}
218577ff2c6bSLiad Kaufman 
218677ff2c6bSLiad Kaufman 			flags |= STA_CTXT_HE_PACKET_EXT;
218777ff2c6bSLiad Kaufman 		}
2188514c3069SLuca Coelho 	}
218973f23d91SShaul Triebitz 
219073f23d91SShaul Triebitz 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] &
219173f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
219273f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
219373f23d91SShaul Triebitz 
219473f23d91SShaul Triebitz 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] &
219573f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
219673f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
219773f23d91SShaul Triebitz 
2198514c3069SLuca Coelho 	rcu_read_unlock();
2199514c3069SLuca Coelho 
2200514c3069SLuca Coelho 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2201514c3069SLuca Coelho 	flags |= STA_CTXT_HE_MU_EDCA_CW;
22025cc74f65SShaul Triebitz 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2203514c3069SLuca Coelho 		struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2204514c3069SLuca Coelho 			&mvmvif->queue_params[i].mu_edca_param_rec;
22055cc74f65SShaul Triebitz 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2206514c3069SLuca Coelho 
2207514c3069SLuca Coelho 		if (!mvmvif->queue_params[i].mu_edca) {
2208514c3069SLuca Coelho 			flags &= ~STA_CTXT_HE_MU_EDCA_CW;
2209514c3069SLuca Coelho 			break;
2210514c3069SLuca Coelho 		}
2211514c3069SLuca Coelho 
22125cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].cwmin =
2213514c3069SLuca Coelho 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
22145cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].cwmax =
2215514c3069SLuca Coelho 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
22165cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].aifsn =
2217514c3069SLuca Coelho 			cpu_to_le16(mu_edca->aifsn);
22185cc74f65SShaul Triebitz 		sta_ctxt_cmd.trig_based_txf[ac].mu_time =
2219514c3069SLuca Coelho 			cpu_to_le16(mu_edca->mu_edca_timer);
2220514c3069SLuca Coelho 	}
2221514c3069SLuca Coelho 
2222514c3069SLuca Coelho 
2223514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2224514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2225514c3069SLuca Coelho 
2226514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2227514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2228514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2229514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2230514c3069SLuca Coelho 	}
2231514c3069SLuca Coelho 
2232918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2233918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2234918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2235918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2236d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2237d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2238d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2239d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2240d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2241d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2242918cbf39SSara Sharon 	}
2243514c3069SLuca Coelho 
2244514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2245514c3069SLuca Coelho 
2246514c3069SLuca Coelho 	if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(STA_HE_CTXT_CMD,
2247514c3069SLuca Coelho 						 DATA_PATH_GROUP, 0),
2248d14ae796SSara Sharon 				 0, size, &sta_ctxt_cmd))
2249514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2250514c3069SLuca Coelho }
2251514c3069SLuca Coelho 
2252e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2253e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2254e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
2255e705c121SKalle Valo 					     u32 changes)
2256e705c121SKalle Valo {
2257e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2258e705c121SKalle Valo 	int ret;
2259e705c121SKalle Valo 
2260e705c121SKalle Valo 	/*
2261e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
2262e705c121SKalle Valo 	 * beacon interval, which was not known when the station interface was
2263e705c121SKalle Valo 	 * added.
2264e705c121SKalle Valo 	 */
2265514c3069SLuca Coelho 	if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
2266230ba6c5SLuca Coelho 		if (vif->bss_conf.he_support &&
2267230ba6c5SLuca Coelho 		    !iwlwifi_mod_params.disable_11ax)
2268514c3069SLuca Coelho 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2269514c3069SLuca Coelho 
2270e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2271514c3069SLuca Coelho 	}
2272e705c121SKalle Valo 
227340ecdd01SShaul Triebitz 	/* Update MU EDCA params */
227440ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
227540ecdd01SShaul Triebitz 	    bss_conf->assoc && vif->bss_conf.he_support &&
227640ecdd01SShaul Triebitz 	    !iwlwifi_mod_params.disable_11ax)
227740ecdd01SShaul Triebitz 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
227840ecdd01SShaul Triebitz 
2279e705c121SKalle Valo 	/*
2280e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2281e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2282e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2283e705c121SKalle Valo 	 * branch for disassociation below.
2284e705c121SKalle Valo 	 */
2285e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2286e705c121SKalle Valo 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2287e705c121SKalle Valo 
2288e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
2289e705c121SKalle Valo 	if (ret)
2290e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2291e705c121SKalle Valo 
2292e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2293e705c121SKalle Valo 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2294e705c121SKalle Valo 	mvmvif->associated = bss_conf->assoc;
2295e705c121SKalle Valo 
2296e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2297e705c121SKalle Valo 		if (bss_conf->assoc) {
2298e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2299e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2300e705c121SKalle Valo 			memset(&mvmvif->beacon_stats, 0,
2301e705c121SKalle Valo 			       sizeof(mvmvif->beacon_stats));
2302e705c121SKalle Valo 
2303e705c121SKalle Valo 			/* add quota for this interface */
2304e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2305e705c121SKalle Valo 			if (ret) {
2306e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2307e705c121SKalle Valo 				return;
2308e705c121SKalle Valo 			}
2309e705c121SKalle Valo 
2310e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2311fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2312fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2313fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2314e705c121SKalle Valo 				/*
2315e705c121SKalle Valo 				 * If we're restarting then the firmware will
2316e705c121SKalle Valo 				 * obviously have lost synchronisation with
2317e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2318e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2319e705c121SKalle Valo 				 * scheduling a session protection time event.
2320e705c121SKalle Valo 				 *
2321e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2322e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2323e705c121SKalle Valo 				 * the beacon interval.
2324e705c121SKalle Valo 				 *
2325e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2326e705c121SKalle Valo 				 * than a single interface.
2327fe959c7bSEmmanuel Grumbach 				 *
2328fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2329fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2330fe959c7bSEmmanuel Grumbach 				 * only anyway.
2331e705c121SKalle Valo 				 */
2332e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2333e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2334e705c121SKalle Valo 							5 * dur, false);
2335e705c121SKalle Valo 			}
2336e705c121SKalle Valo 
2337e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2338e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2339e705c121SKalle Valo 			if (vif->p2p) {
2340e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2341e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
2342e705c121SKalle Valo 						    IEEE80211_SMPS_DYNAMIC);
2343e705c121SKalle Valo 			}
23440ae98812SSara Sharon 		} else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
2345e705c121SKalle Valo 			/*
2346e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2347e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2348e705c121SKalle Valo 			 */
234969e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
235069e508b4SIlan Peer 			WARN_ONCE(ret &&
235169e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
235269e508b4SIlan Peer 					    &mvm->status),
2353e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2354e705c121SKalle Valo 
23556b28f978SEmmanuel Grumbach 			/*
23566b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
23576b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
23586b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
23596b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
23606b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
23616b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
23626b28f978SEmmanuel Grumbach 			 */
23636b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
23646b28f978SEmmanuel Grumbach 				      &mvm->status)) {
23656b28f978SEmmanuel Grumbach 				/*
23666b28f978SEmmanuel Grumbach 				 * Remove AP station now that
23676b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
23686b28f978SEmmanuel Grumbach 				 */
23696b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
23706b28f978SEmmanuel Grumbach 							mvmvif->ap_sta_id);
2371e705c121SKalle Valo 				if (ret)
23726b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
23736b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2374e705c121SKalle Valo 
23750ae98812SSara Sharon 				mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
23766b28f978SEmmanuel Grumbach 			}
23776b28f978SEmmanuel Grumbach 
2378e705c121SKalle Valo 			/* remove quota for this interface */
2379e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2380e705c121SKalle Valo 			if (ret)
2381e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2382e705c121SKalle Valo 
2383e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2384e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2385e705c121SKalle Valo 			if (ret)
2386e705c121SKalle Valo 				IWL_ERR(mvm,
2387e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2388e705c121SKalle Valo 					vif->addr);
2389e705c121SKalle Valo 		}
2390e705c121SKalle Valo 
2391a07a8f37SSara Sharon 		/*
2392a07a8f37SSara Sharon 		 * The firmware tracks the MU-MIMO group on its own.
2393f92659a1SSara Sharon 		 * However, on HW restart we should restore this data.
2394a07a8f37SSara Sharon 		 */
2395a07a8f37SSara Sharon 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2396f92659a1SSara Sharon 		    (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
2397a07a8f37SSara Sharon 			ret = iwl_mvm_update_mu_groups(mvm, vif);
2398a07a8f37SSara Sharon 			if (ret)
2399a07a8f37SSara Sharon 				IWL_ERR(mvm,
2400a07a8f37SSara Sharon 					"failed to update VHT MU_MIMO groups\n");
2401a07a8f37SSara Sharon 		}
2402a07a8f37SSara Sharon 
2403e705c121SKalle Valo 		iwl_mvm_recalc_multicast(mvm);
240434672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2405e705c121SKalle Valo 
2406e705c121SKalle Valo 		/* reset rssi values */
2407e705c121SKalle Valo 		mvmvif->bf_data.ave_beacon_signal = 0;
2408e705c121SKalle Valo 
2409e705c121SKalle Valo 		iwl_mvm_bt_coex_vif_change(mvm);
2410e705c121SKalle Valo 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2411e705c121SKalle Valo 				    IEEE80211_SMPS_AUTOMATIC);
2412355346baSAvraham Stern 		if (fw_has_capa(&mvm->fw->ucode_capa,
2413355346baSAvraham Stern 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2414355346baSAvraham Stern 			iwl_mvm_config_scan(mvm);
2415b45242c9SAvraham Stern 	}
2416b45242c9SAvraham Stern 
2417b45242c9SAvraham Stern 	if (changes & BSS_CHANGED_BEACON_INFO) {
2418e705c121SKalle Valo 		/*
2419b45242c9SAvraham Stern 		 * We received a beacon from the associated AP so
2420e705c121SKalle Valo 		 * remove the session protection.
2421fe959c7bSEmmanuel Grumbach 		 * A firmware with the new API will remove it automatically.
2422e705c121SKalle Valo 		 */
2423fe959c7bSEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
2424fe959c7bSEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
24253edfb5f4SAvraham Stern 			iwl_mvm_stop_session_protection(mvm, vif);
2426e705c121SKalle Valo 
2427e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2428e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2429e705c121SKalle Valo 	}
2430e705c121SKalle Valo 
2431283115fbSAvri Altman 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2432283115fbSAvri Altman 		       /*
2433283115fbSAvri Altman 			* Send power command on every beacon change,
2434283115fbSAvri Altman 			* because we may have not enabled beacon abort yet.
2435283115fbSAvri Altman 			*/
2436283115fbSAvri Altman 		       BSS_CHANGED_BEACON_INFO)) {
2437e705c121SKalle Valo 		ret = iwl_mvm_power_update_mac(mvm);
2438e705c121SKalle Valo 		if (ret)
2439e705c121SKalle Valo 			IWL_ERR(mvm, "failed to update power mode\n");
2440e705c121SKalle Valo 	}
2441e705c121SKalle Valo 
2442e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2443e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2444e705c121SKalle Valo 				bss_conf->txpower);
2445e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2446e705c121SKalle Valo 	}
2447e705c121SKalle Valo 
2448e705c121SKalle Valo 	if (changes & BSS_CHANGED_CQM) {
2449e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2450e705c121SKalle Valo 		/* reset cqm events tracking */
2451e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = 0;
2452e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
2453e705c121SKalle Valo 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2454e705c121SKalle Valo 			if (ret)
2455e705c121SKalle Valo 				IWL_ERR(mvm,
2456e705c121SKalle Valo 					"failed to update CQM thresholds\n");
2457e705c121SKalle Valo 		}
2458e705c121SKalle Valo 	}
2459e705c121SKalle Valo 
2460e705c121SKalle Valo 	if (changes & BSS_CHANGED_ARP_FILTER) {
2461e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
246234672bb3SEliad Peller 		iwl_mvm_configure_bcast_filter(mvm);
2463e705c121SKalle Valo 	}
2464e705c121SKalle Valo }
2465e705c121SKalle Valo 
2466e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2467e705c121SKalle Valo 				 struct ieee80211_vif *vif)
2468e705c121SKalle Valo {
2469e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2470e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2471c56e00a3SJohannes Berg 	int ret, i;
2472e705c121SKalle Valo 
2473e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2474e705c121SKalle Valo 
2475e705c121SKalle Valo 	/* Send the beacon template */
2476e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2477e705c121SKalle Valo 	if (ret)
2478e705c121SKalle Valo 		goto out_unlock;
2479e705c121SKalle Valo 
2480e705c121SKalle Valo 	/*
2481e705c121SKalle Valo 	 * Re-calculate the tsf id, as the master-slave relations depend on the
2482e705c121SKalle Valo 	 * beacon interval, which was not known when the AP interface was added.
2483e705c121SKalle Valo 	 */
2484e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2485e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2486e705c121SKalle Valo 
2487e705c121SKalle Valo 	mvmvif->ap_assoc_sta_count = 0;
2488e705c121SKalle Valo 
2489e705c121SKalle Valo 	/* Add the mac context */
2490e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2491e705c121SKalle Valo 	if (ret)
2492e705c121SKalle Valo 		goto out_unlock;
2493e705c121SKalle Valo 
2494e705c121SKalle Valo 	/* Perform the binding */
2495e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2496e705c121SKalle Valo 	if (ret)
2497e705c121SKalle Valo 		goto out_remove;
2498e705c121SKalle Valo 
249963dd5d02SSara Sharon 	/*
250063dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
250163dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
250263dd5d02SSara Sharon 	 * cause assert 0x2b00.
250363dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
250463dd5d02SSara Sharon 	 * the TLV
250563dd5d02SSara Sharon 	 */
250663dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2507ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2508ced19f26SSara Sharon 		if (ret)
2509ced19f26SSara Sharon 			goto out_unbind;
251063dd5d02SSara Sharon 		/*
251163dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
251263dd5d02SSara Sharon 		 * events are added and applied to the scheduler
251363dd5d02SSara Sharon 		 */
2514e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
251563dd5d02SSara Sharon 		if (ret) {
251663dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
251763dd5d02SSara Sharon 			goto out_unbind;
251863dd5d02SSara Sharon 		}
251963dd5d02SSara Sharon 	} else {
252063dd5d02SSara Sharon 		/*
252163dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
252263dd5d02SSara Sharon 		 * events are added and applied to the scheduler
252363dd5d02SSara Sharon 		 */
252475fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2525e705c121SKalle Valo 		if (ret)
252663dd5d02SSara Sharon 			goto out_unbind;
252775fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
252863dd5d02SSara Sharon 		if (ret) {
252963dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
253063dd5d02SSara Sharon 			goto out_unbind;
253163dd5d02SSara Sharon 		}
253263dd5d02SSara Sharon 	}
253326d6c16bSSara Sharon 
2534e705c121SKalle Valo 	/* must be set before quota calculations */
2535e705c121SKalle Valo 	mvmvif->ap_ibss_active = true;
2536e705c121SKalle Valo 
2537c56e00a3SJohannes Berg 	/* send all the early keys to the device now */
2538c56e00a3SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2539c56e00a3SJohannes Berg 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2540c56e00a3SJohannes Berg 
2541c56e00a3SJohannes Berg 		if (!key)
2542c56e00a3SJohannes Berg 			continue;
2543c56e00a3SJohannes Berg 
2544c56e00a3SJohannes Berg 		mvmvif->ap_early_keys[i] = NULL;
2545c56e00a3SJohannes Berg 
25466569e7d3SJohannes Berg 		ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2547c56e00a3SJohannes Berg 		if (ret)
2548c56e00a3SJohannes Berg 			goto out_quota_failed;
2549c56e00a3SJohannes Berg 	}
2550c56e00a3SJohannes Berg 
255147242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
255247242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, true,
255347242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
255447242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
255547242744STova Mussai 	}
255647242744STova Mussai 
2557e705c121SKalle Valo 	/* power updated needs to be done before quotas */
2558e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2559e705c121SKalle Valo 
2560e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2561e705c121SKalle Valo 	if (ret)
2562e705c121SKalle Valo 		goto out_quota_failed;
2563e705c121SKalle Valo 
2564e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2565e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2566e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2567e705c121SKalle Valo 
2568e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2569e705c121SKalle Valo 
2570e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2571e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2572e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2573e705c121SKalle Valo 
2574b73f9a4aSJohannes Berg 	iwl_mvm_ftm_restart_responder(mvm, vif);
2575b73f9a4aSJohannes Berg 
2576e705c121SKalle Valo 	goto out_unlock;
2577e705c121SKalle Valo 
2578e705c121SKalle Valo out_quota_failed:
2579e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2580e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2581e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2582ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2583e705c121SKalle Valo out_unbind:
2584e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2585e705c121SKalle Valo out_remove:
2586e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2587e705c121SKalle Valo out_unlock:
2588e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2589e705c121SKalle Valo 	return ret;
2590e705c121SKalle Valo }
2591e705c121SKalle Valo 
2592e705c121SKalle Valo static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2593e705c121SKalle Valo 				 struct ieee80211_vif *vif)
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);
2597e705c121SKalle Valo 
2598e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
2599e705c121SKalle Valo 
2600e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2601e705c121SKalle Valo 
2602e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2603e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2604e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2605e705c121SKalle Valo 					  &mvmvif->time_event_data);
2606e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2607e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2608e705c121SKalle Valo 	}
2609e705c121SKalle Valo 
2610e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2611e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2612e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2613e705c121SKalle Valo 	}
2614e705c121SKalle Valo 
2615e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2616e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2617e705c121SKalle Valo 
261847242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
261947242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
262047242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
262147242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
262247242744STova Mussai 	}
262347242744STova Mussai 
2624e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2625e705c121SKalle Valo 
2626e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2627e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2628e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2629e705c121SKalle Valo 
2630e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2631ced19f26SSara Sharon 
2632ced19f26SSara Sharon 	/*
2633ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2634ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2635ced19f26SSara Sharon 	 * cause assert 0x2b00.
2636ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2637ced19f26SSara Sharon 	 * bcast station).
2638ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2639ced19f26SSara Sharon 	 */
2640ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
264126d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2642e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2643ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2644ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2645e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2646e705c121SKalle Valo 
2647e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2648e705c121SKalle Valo 
2649e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2650e705c121SKalle Valo 
2651e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2652e705c121SKalle Valo }
2653e705c121SKalle Valo 
2654e705c121SKalle Valo static void
2655e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2656e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2657e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
2658e705c121SKalle Valo 				 u32 changes)
2659e705c121SKalle Valo {
2660e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2661e705c121SKalle Valo 
2662e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
2663e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
2664e705c121SKalle Valo 		return;
2665e705c121SKalle Valo 
2666e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2667e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2668e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2669e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2670e705c121SKalle Valo 
2671e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
2672e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
2673e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2674e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
2675e705c121SKalle Valo 
2676e705c121SKalle Valo 	if (changes & BSS_CHANGED_TXPOWER) {
2677e705c121SKalle Valo 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2678e705c121SKalle Valo 				bss_conf->txpower);
2679e705c121SKalle Valo 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2680e705c121SKalle Valo 	}
2681b73f9a4aSJohannes Berg 
2682b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
2683b73f9a4aSJohannes Berg 		int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2684b73f9a4aSJohannes Berg 
2685b73f9a4aSJohannes Berg 		if (ret)
2686b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2687b73f9a4aSJohannes Berg 				 ret);
2688b73f9a4aSJohannes Berg 	}
2689b73f9a4aSJohannes Berg 
2690e705c121SKalle Valo }
2691e705c121SKalle Valo 
2692e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2693e705c121SKalle Valo 				     struct ieee80211_vif *vif,
2694e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
2695e705c121SKalle Valo 				     u32 changes)
2696e705c121SKalle Valo {
2697e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2698e705c121SKalle Valo 
2699e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2700e705c121SKalle Valo 
2701e705c121SKalle Valo 	if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2702e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2703e705c121SKalle Valo 
2704e705c121SKalle Valo 	switch (vif->type) {
2705e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2706e705c121SKalle Valo 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2707e705c121SKalle Valo 		break;
2708e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
2709e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
2710e705c121SKalle Valo 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2711e705c121SKalle Valo 		break;
271291b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
271391b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
271491b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
271591b08c2dSAviya Erenfeld 		break;
2716e705c121SKalle Valo 	default:
2717e705c121SKalle Valo 		/* shouldn't happen */
2718e705c121SKalle Valo 		WARN_ON_ONCE(1);
2719e705c121SKalle Valo 	}
2720e705c121SKalle Valo 
2721e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2722e705c121SKalle Valo }
2723e705c121SKalle Valo 
2724e705c121SKalle Valo static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2725e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2726e705c121SKalle Valo 			       struct ieee80211_scan_request *hw_req)
2727e705c121SKalle Valo {
2728e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2729e705c121SKalle Valo 	int ret;
2730e705c121SKalle Valo 
2731e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
2732e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2733e705c121SKalle Valo 		return -EINVAL;
2734e705c121SKalle Valo 
2735e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2736e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2737e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2738e705c121SKalle Valo 
2739e705c121SKalle Valo 	return ret;
2740e705c121SKalle Valo }
2741e705c121SKalle Valo 
2742e705c121SKalle Valo static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2743e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2744e705c121SKalle Valo {
2745e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2746e705c121SKalle Valo 
2747e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2748e705c121SKalle Valo 
2749e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2750e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
2751e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
2752e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
2753e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
2754e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
2755e705c121SKalle Valo 	*/
2756e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2757e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2758e705c121SKalle Valo 
2759e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2760e705c121SKalle Valo }
2761e705c121SKalle Valo 
2762e705c121SKalle Valo static void
2763e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2764e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
2765e705c121SKalle Valo 				  int num_frames,
2766e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
2767e705c121SKalle Valo 				  bool more_data)
2768e705c121SKalle Valo {
2769e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2770e705c121SKalle Valo 
2771e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
2772e705c121SKalle Valo 
2773e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2774e705c121SKalle Valo 					  tids, more_data, false);
2775e705c121SKalle Valo }
2776e705c121SKalle Valo 
2777e705c121SKalle Valo static void
2778e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2779e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
2780e705c121SKalle Valo 				    int num_frames,
2781e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
2782e705c121SKalle Valo 				    bool more_data)
2783e705c121SKalle Valo {
2784e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2785e705c121SKalle Valo 
27869a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2787e705c121SKalle Valo 
2788e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2789e705c121SKalle Valo 					  tids, more_data, true);
2790e705c121SKalle Valo }
2791e705c121SKalle Valo 
279265e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2793e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
2794e705c121SKalle Valo 				     struct ieee80211_sta *sta)
2795e705c121SKalle Valo {
2796e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2797e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2798e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
2799e705c121SKalle Valo 	int tid;
2800e705c121SKalle Valo 
2801960f864bSJohannes Berg 	/*
2802960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
2803960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
2804960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
2805960f864bSJohannes Berg 	 */
2806960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2807960f864bSJohannes Berg 		return;
2808960f864bSJohannes Berg 
2809e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2810311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
2811e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2812e705c121SKalle Valo 
28136862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
28141c17627bSSara Sharon 			continue;
28151c17627bSSara Sharon 
2816e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
2817e705c121SKalle Valo 
2818dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
2819e705c121SKalle Valo 			continue;
2820e705c121SKalle Valo 
2821e705c121SKalle Valo 		__set_bit(tid, &tids);
2822e705c121SKalle Valo 	}
2823e705c121SKalle Valo 
2824e705c121SKalle Valo 	switch (cmd) {
2825e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
2826e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
2827e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
2828e705c121SKalle Valo 
2829e705c121SKalle Valo 		if (txqs)
2830e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
2831e705c121SKalle Valo 		/*
2832e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
2833e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
2834e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2835e705c121SKalle Valo 		 */
2836e705c121SKalle Valo 		break;
2837e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
28380ae98812SSara Sharon 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
2839e705c121SKalle Valo 			break;
2840e705c121SKalle Valo 
2841e705c121SKalle Valo 		if (txqs)
2842e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
2843e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
2844e705c121SKalle Valo 		break;
2845e705c121SKalle Valo 	default:
2846e705c121SKalle Valo 		break;
2847e705c121SKalle Valo 	}
2848e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2849e705c121SKalle Valo }
2850e705c121SKalle Valo 
285165e25482SJohannes Berg static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
285265e25482SJohannes Berg 				   struct ieee80211_vif *vif,
285365e25482SJohannes Berg 				   enum sta_notify_cmd cmd,
285465e25482SJohannes Berg 				   struct ieee80211_sta *sta)
285565e25482SJohannes Berg {
285665e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
285765e25482SJohannes Berg }
285865e25482SJohannes Berg 
285965e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
286065e25482SJohannes Berg {
286165e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
286265e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
286365e25482SJohannes Berg 	struct ieee80211_sta *sta;
286465e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
286565e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
286665e25482SJohannes Berg 
286765e25482SJohannes Berg 	if (WARN_ON(notif->sta_id >= ARRAY_SIZE(mvm->fw_id_to_mac_id)))
286865e25482SJohannes Berg 		return;
286965e25482SJohannes Berg 
287065e25482SJohannes Berg 	rcu_read_lock();
2871a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
287265e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
287365e25482SJohannes Berg 		rcu_read_unlock();
287465e25482SJohannes Berg 		return;
287565e25482SJohannes Berg 	}
287665e25482SJohannes Berg 
287765e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
287865e25482SJohannes Berg 
287965e25482SJohannes Berg 	if (!mvmsta->vif ||
288065e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
288165e25482SJohannes Berg 		rcu_read_unlock();
288265e25482SJohannes Berg 		return;
288365e25482SJohannes Berg 	}
288465e25482SJohannes Berg 
288565e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
288665e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
288765e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
288865e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
288965e25482SJohannes Berg 			sta);
289065e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
289165e25482SJohannes Berg 	}
289265e25482SJohannes Berg 
289365e25482SJohannes Berg 	if (sleeping) {
289465e25482SJohannes Berg 		switch (notif->type) {
289565e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
289665e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
289765e25482SJohannes Berg 			break;
289865e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
289965e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
290065e25482SJohannes Berg 			break;
290165e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
290265e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
290365e25482SJohannes Berg 			break;
290465e25482SJohannes Berg 		default:
290565e25482SJohannes Berg 			break;
290665e25482SJohannes Berg 		}
290765e25482SJohannes Berg 	}
290865e25482SJohannes Berg 
290965e25482SJohannes Berg 	rcu_read_unlock();
291065e25482SJohannes Berg }
291165e25482SJohannes Berg 
2912e705c121SKalle Valo static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2913e705c121SKalle Valo 				       struct ieee80211_vif *vif,
2914e705c121SKalle Valo 				       struct ieee80211_sta *sta)
2915e705c121SKalle Valo {
2916e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2917e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2918e705c121SKalle Valo 
2919e705c121SKalle Valo 	/*
2920e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
2921e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
2922e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
2923e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
2924e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
2925e705c121SKalle Valo 	 * callback deleted the station.
2926e705c121SKalle Valo 	 */
2927e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2928e705c121SKalle Valo 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2929e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2930e705c121SKalle Valo 				   ERR_PTR(-ENOENT));
2931e705c121SKalle Valo 
2932e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2933e705c121SKalle Valo }
2934e705c121SKalle Valo 
2935e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2936e705c121SKalle Valo 				const u8 *bssid)
2937e705c121SKalle Valo {
2938b0ffe455SJohannes Berg 	int i;
2939b0ffe455SJohannes Berg 
2940b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2941b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
2942b0ffe455SJohannes Berg 
2943b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
2944b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
2945b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
2946b0ffe455SJohannes Berg 	}
2947b0ffe455SJohannes Berg 
2948e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2949e705c121SKalle Valo 		return;
2950e705c121SKalle Valo 
2951c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
2952cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2953cee5a882SAvri Altman 		return;
2954cee5a882SAvri Altman 	}
2955cee5a882SAvri Altman 
295611dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
295711dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
2958e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2959e705c121SKalle Valo 		return;
2960e705c121SKalle Valo 	}
2961e705c121SKalle Valo 
2962b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
2963b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
2964b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2965b0ffe455SJohannes Berg 			return;
2966b0ffe455SJohannes Berg 		}
2967b0ffe455SJohannes Berg 	}
2968b0ffe455SJohannes Berg 
2969e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2970e705c121SKalle Valo }
2971e705c121SKalle Valo 
29721e8f1329SGolan Ben-Ami static void
29731e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
29741e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
29751e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
29761e8f1329SGolan Ben-Ami {
29771e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
29781e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
29791e8f1329SGolan Ben-Ami 
29806c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
29816c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
29826c042d75SSara Sharon 	if (!trig)
29831e8f1329SGolan Ben-Ami 		return;
29841e8f1329SGolan Ben-Ami 
29851e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
29861e8f1329SGolan Ben-Ami 
29871e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
29881e8f1329SGolan Ben-Ami 		return;
29891e8f1329SGolan Ben-Ami 
29901e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
29911e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
29921e8f1329SGolan Ben-Ami 		return;
29931e8f1329SGolan Ben-Ami 
29947174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
29951e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
29961e8f1329SGolan Ben-Ami 				peer_addr, action);
29971e8f1329SGolan Ben-Ami }
29981e8f1329SGolan Ben-Ami 
29994f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
30004f58121dSIlan Peer 	bool tolerated;
30014f58121dSIlan Peer };
30024f58121dSIlan Peer 
30034f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
30044f58121dSIlan Peer 						    struct cfg80211_bss *bss,
30054f58121dSIlan Peer 						    void *_data)
30064f58121dSIlan Peer {
30074f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
30084f58121dSIlan Peer 	const struct element *elem;
30094f58121dSIlan Peer 
30104f58121dSIlan Peer 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, bss->ies->data,
30114f58121dSIlan Peer 				  bss->ies->len);
30124f58121dSIlan Peer 
30134f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
30144f58121dSIlan Peer 	    !(elem->data[10] &
30154f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
30164f58121dSIlan Peer 		data->tolerated = false;
30174f58121dSIlan Peer 	}
30184f58121dSIlan Peer }
30194f58121dSIlan Peer 
30204f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
30214f58121dSIlan Peer 					       struct ieee80211_vif *vif)
30224f58121dSIlan Peer {
30234f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
30244f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
30254f58121dSIlan Peer 		.tolerated = true,
30264f58121dSIlan Peer 	};
30274f58121dSIlan Peer 
30284f58121dSIlan Peer 	if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
30294f58121dSIlan Peer 		mvmvif->he_ru_2mhz_block = false;
30304f58121dSIlan Peer 		return;
30314f58121dSIlan Peer 	}
30324f58121dSIlan Peer 
30334f58121dSIlan Peer 	cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
30344f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
30354f58121dSIlan Peer 			  &iter_data);
30364f58121dSIlan Peer 
30374f58121dSIlan Peer 	/*
30384f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
30394f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
30404f58121dSIlan Peer 	 */
30414f58121dSIlan Peer 	mvmvif->he_ru_2mhz_block = !iter_data.tolerated;
30424f58121dSIlan Peer }
30434f58121dSIlan Peer 
3044e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3045e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3046e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3047e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3048e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3049e705c121SKalle Valo {
3050e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3051e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
30526ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3053e705c121SKalle Valo 	int ret;
3054e705c121SKalle Valo 
3055e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3056e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3057e705c121SKalle Valo 
3058e705c121SKalle Valo 	/* this would be a mac80211 bug ... but don't crash */
3059e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
3060e705c121SKalle Valo 		return -EINVAL;
3061e705c121SKalle Valo 
306224afba76SLiad Kaufman 	/*
306324afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
306424afba76SLiad Kaufman 	 *
306524afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
306624afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
306724afba76SLiad Kaufman 	 * the queues.
306824afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
306924afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
307024afba76SLiad Kaufman 	 * of.
307124afba76SLiad Kaufman 	 *
307224afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
307324afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
307424afba76SLiad Kaufman 	 */
307524afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3076c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
307724afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
307824afba76SLiad Kaufman 
307924afba76SLiad Kaufman 		/*
308024afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
308124afba76SLiad Kaufman 		 * worker will already remove it if it was on
308224afba76SLiad Kaufman 		 */
308324afba76SLiad Kaufman 	}
308424afba76SLiad Kaufman 
3085e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
30866ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3087d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
30886ea29ce5SJohannes Berg 
3089e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3090e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
3091e705c121SKalle Valo 		/*
3092e705c121SKalle Valo 		 * Firmware bug - it'll crash if the beacon interval is less
3093e705c121SKalle Valo 		 * than 16. We can't avoid connecting at all, so refuse the
3094e705c121SKalle Valo 		 * station state change, this will cause mac80211 to abandon
3095e705c121SKalle Valo 		 * attempts to connect to this AP, and eventually wpa_s will
3096e705c121SKalle Valo 		 * blacklist the AP...
3097e705c121SKalle Valo 		 */
3098e705c121SKalle Valo 		if (vif->type == NL80211_IFTYPE_STATION &&
3099e705c121SKalle Valo 		    vif->bss_conf.beacon_int < 16) {
3100e705c121SKalle Valo 			IWL_ERR(mvm,
3101e705c121SKalle Valo 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3102e705c121SKalle Valo 				sta->addr, vif->bss_conf.beacon_int);
3103e705c121SKalle Valo 			ret = -EINVAL;
3104e705c121SKalle Valo 			goto out_unlock;
3105e705c121SKalle Valo 		}
3106e705c121SKalle Valo 
3107e705c121SKalle Valo 		if (sta->tdls &&
3108e705c121SKalle Valo 		    (vif->p2p ||
3109e705c121SKalle Valo 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
3110e705c121SKalle Valo 						IWL_MVM_TDLS_STA_COUNT ||
3111e705c121SKalle Valo 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3112e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3113e705c121SKalle Valo 			ret = -EBUSY;
3114e705c121SKalle Valo 			goto out_unlock;
3115e705c121SKalle Valo 		}
3116e705c121SKalle Valo 
3117e705c121SKalle Valo 		ret = iwl_mvm_add_sta(mvm, vif, sta);
31181e8f1329SGolan Ben-Ami 		if (sta->tdls && ret == 0) {
3119e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
31201e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
31211e8f1329SGolan Ben-Ami 						   NL80211_TDLS_SETUP);
31221e8f1329SGolan Ben-Ami 		}
3123438af969SSara Sharon 
3124438af969SSara Sharon 		sta->max_rc_amsdu_len = 1;
3125e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3126e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3127e705c121SKalle Valo 		/*
3128e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3129e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3130e705c121SKalle Valo 		 */
3131e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3132e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3133e705c121SKalle Valo 		ret = 0;
3134e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3135e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
31368be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
31379394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
31388be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count++;
31398be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
314002221a81SShaul Triebitz 			if (vif->bss_conf.he_support &&
314102221a81SShaul Triebitz 			    !iwlwifi_mod_params.disable_11ax)
314202221a81SShaul Triebitz 				iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
31439394662aSLiad Kaufman 		} else if (vif->type == NL80211_IFTYPE_STATION) {
31449394662aSLiad Kaufman 			vif->bss_conf.he_support = sta->he_cap.has_he;
31454f58121dSIlan Peer 
31464f58121dSIlan Peer 			mvmvif->he_ru_2mhz_block = false;
31474f58121dSIlan Peer 			if (sta->he_cap.has_he)
31484f58121dSIlan Peer 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif);
31494f58121dSIlan Peer 
31509394662aSLiad Kaufman 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
31518be30c13SAyala Beker 		}
3152735a0045SGoodstein, Mordechay 
31533baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
31543baf7528SAvraham Stern 				     false);
3155735a0045SGoodstein, Mordechay 		ret = iwl_mvm_update_sta(mvm, vif, sta);
3156e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3157e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
315828916a16SEmmanuel Grumbach 		ret = 0;
3159e705c121SKalle Valo 
3160e705c121SKalle Valo 		/* we don't support TDLS during DCM */
3161e705c121SKalle Valo 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
3162e705c121SKalle Valo 			iwl_mvm_teardown_tdls_peers(mvm);
3163e705c121SKalle Valo 
31641e8f1329SGolan Ben-Ami 		if (sta->tdls)
31651e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
31661e8f1329SGolan Ben-Ami 						   NL80211_TDLS_ENABLE_LINK);
31671e8f1329SGolan Ben-Ami 
3168e705c121SKalle Valo 		/* enable beacon filtering */
3169e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
31706b7a5aeaSNaftali Goldstein 
317150f56044SIlan Peer 		/*
317250f56044SIlan Peer 		 * Now that the station is authorized, i.e., keys were already
317350f56044SIlan Peer 		 * installed, need to indicate to the FW that
317450f56044SIlan Peer 		 * multicast data frames can be forwarded to the driver
317550f56044SIlan Peer 		 */
317650f56044SIlan Peer 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
317750f56044SIlan Peer 
31783baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
31793baf7528SAvraham Stern 				     true);
3180e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3181e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
318250f56044SIlan Peer 		/* Multicast data frames are no longer allowed */
318350f56044SIlan Peer 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
318450f56044SIlan Peer 
3185e705c121SKalle Valo 		/* disable beacon filtering */
318669e508b4SIlan Peer 		ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
318769e508b4SIlan Peer 		WARN_ON(ret &&
318869e508b4SIlan Peer 			!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
318969e508b4SIlan Peer 				  &mvm->status));
3190e705c121SKalle Valo 		ret = 0;
3191e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3192e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
31938be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
31948be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
31958be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
31968be30c13SAyala Beker 		}
3197e705c121SKalle Valo 		ret = 0;
3198e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3199e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3200e705c121SKalle Valo 		ret = 0;
3201e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3202e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
3203e705c121SKalle Valo 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
32041e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3205e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
32061e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
32071e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
32081e8f1329SGolan Ben-Ami 		}
320934a880d8SLiad Kaufman 
321044135b7cSIlan Peer 		if (unlikely(ret &&
321144135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
321244135b7cSIlan Peer 				      &mvm->status)))
321344135b7cSIlan Peer 			ret = 0;
3214e705c121SKalle Valo 	} else {
3215e705c121SKalle Valo 		ret = -EIO;
3216e705c121SKalle Valo 	}
3217e705c121SKalle Valo  out_unlock:
3218e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3219e705c121SKalle Valo 
3220e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3221e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3222e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3223e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3224e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3225e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3226e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3227e705c121SKalle Valo 	}
3228e705c121SKalle Valo 
3229e705c121SKalle Valo 	return ret;
3230e705c121SKalle Valo }
3231e705c121SKalle Valo 
3232e705c121SKalle Valo static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3233e705c121SKalle Valo {
3234e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3235e705c121SKalle Valo 
3236e705c121SKalle Valo 	mvm->rts_threshold = value;
3237e705c121SKalle Valo 
3238e705c121SKalle Valo 	return 0;
3239e705c121SKalle Valo }
3240e705c121SKalle Valo 
3241e705c121SKalle Valo static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3242e705c121SKalle Valo 				  struct ieee80211_vif *vif,
3243e705c121SKalle Valo 				  struct ieee80211_sta *sta, u32 changed)
3244e705c121SKalle Valo {
3245e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3246dcfe3b10SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3247dcfe3b10SJohannes Berg 
3248dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
3249dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3250dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
3251dcfe3b10SJohannes Berg 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3252dcfe3b10SJohannes Berg 				     true);
3253e705c121SKalle Valo 
3254e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
3255e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
3256e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
3257e705c121SKalle Valo }
3258e705c121SKalle Valo 
3259e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3260e705c121SKalle Valo 			       struct ieee80211_vif *vif, u16 ac,
3261e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
3262e705c121SKalle Valo {
3263e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3264e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3265e705c121SKalle Valo 
3266e705c121SKalle Valo 	mvmvif->queue_params[ac] = *params;
3267e705c121SKalle Valo 
3268e705c121SKalle Valo 	/*
3269e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
3270e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
3271e705c121SKalle Valo 	 */
3272e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3273e705c121SKalle Valo 		int ret;
3274e705c121SKalle Valo 
3275e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
3276e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3277e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3278e705c121SKalle Valo 		return ret;
3279e705c121SKalle Valo 	}
3280e705c121SKalle Valo 	return 0;
3281e705c121SKalle Valo }
3282e705c121SKalle Valo 
3283e705c121SKalle Valo static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3284d4e36e55SIlan Peer 				       struct ieee80211_vif *vif,
3285d4e36e55SIlan Peer 				       u16 req_duration)
3286e705c121SKalle Valo {
3287e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
32887c70fee5SSara Sharon 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
32897c70fee5SSara Sharon 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
3290e705c121SKalle Valo 
3291d4e36e55SIlan Peer 	if (req_duration > duration)
3292d4e36e55SIlan Peer 		duration = req_duration;
3293d4e36e55SIlan Peer 
3294e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3295fe959c7bSEmmanuel Grumbach 	/* Try really hard to protect the session and hear a beacon
3296fe959c7bSEmmanuel Grumbach 	 * The new session protection command allows us to protect the
3297fe959c7bSEmmanuel Grumbach 	 * session for a much longer time since the firmware will internally
3298fe959c7bSEmmanuel Grumbach 	 * create two events: a 300TU one with a very high priority that
3299fe959c7bSEmmanuel Grumbach 	 * won't be fragmented which should be enough for 99% of the cases,
3300fe959c7bSEmmanuel Grumbach 	 * and another one (which we configure here to be 900TU long) which
3301fe959c7bSEmmanuel Grumbach 	 * will have a slightly lower priority, but more importantly, can be
3302fe959c7bSEmmanuel Grumbach 	 * fragmented so that it'll allow other activities to run.
3303fe959c7bSEmmanuel Grumbach 	 */
3304fe959c7bSEmmanuel Grumbach 	if (fw_has_capa(&mvm->fw->ucode_capa,
3305fe959c7bSEmmanuel Grumbach 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
3306fe959c7bSEmmanuel Grumbach 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
3307b5b878e3SEmmanuel Grumbach 						    min_duration, false);
3308fe959c7bSEmmanuel Grumbach 	else
3309fe959c7bSEmmanuel Grumbach 		iwl_mvm_protect_session(mvm, vif, duration,
3310fe959c7bSEmmanuel Grumbach 					min_duration, 500, false);
3311e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3312e705c121SKalle Valo }
3313e705c121SKalle Valo 
3314e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3315e705c121SKalle Valo 					struct ieee80211_vif *vif,
3316e705c121SKalle Valo 					struct cfg80211_sched_scan_request *req,
3317e705c121SKalle Valo 					struct ieee80211_scan_ies *ies)
3318e705c121SKalle Valo {
3319e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3320e705c121SKalle Valo 
3321e705c121SKalle Valo 	int ret;
3322e705c121SKalle Valo 
3323e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3324e705c121SKalle Valo 
3325e705c121SKalle Valo 	if (!vif->bss_conf.idle) {
3326e705c121SKalle Valo 		ret = -EBUSY;
3327e705c121SKalle Valo 		goto out;
3328e705c121SKalle Valo 	}
3329e705c121SKalle Valo 
3330e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
3331e705c121SKalle Valo 
3332e705c121SKalle Valo out:
3333e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3334e705c121SKalle Valo 	return ret;
3335e705c121SKalle Valo }
3336e705c121SKalle Valo 
3337e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3338e705c121SKalle Valo 				       struct ieee80211_vif *vif)
3339e705c121SKalle Valo {
3340e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3341e705c121SKalle Valo 	int ret;
3342e705c121SKalle Valo 
3343e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3344e705c121SKalle Valo 
3345e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3346e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
3347e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
3348e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
3349e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3350e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
3351e705c121SKalle Valo 	 * not running.
3352e705c121SKalle Valo 	*/
3353e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
3354e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3355e705c121SKalle Valo 		return 0;
3356e705c121SKalle Valo 	}
3357e705c121SKalle Valo 
3358e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
3359e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3360e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
3361e705c121SKalle Valo 
3362e705c121SKalle Valo 	return ret;
3363e705c121SKalle Valo }
3364e705c121SKalle Valo 
33656569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3366e705c121SKalle Valo 				 enum set_key_cmd cmd,
3367e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3368e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3369e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
3370e705c121SKalle Valo {
3371c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3372e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3373f5e28eacSJohannes Berg 	struct iwl_mvm_sta *mvmsta;
3374f5e28eacSJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn;
3375f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
3376c56e00a3SJohannes Berg 	int ret, i;
33774615fd15SEmmanuel Grumbach 	u8 key_offset;
3378e705c121SKalle Valo 
3379e705c121SKalle Valo 	switch (key->cipher) {
3380e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3381286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
3382e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
33831ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
33847f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
33857f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
33867f768ad5SDavid Spinadel 		} else {
33877f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
33887f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
33897f768ad5SDavid Spinadel 		}
3390e705c121SKalle Valo 		break;
3391e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
33922a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
33932a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
339485aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
3395e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3396e705c121SKalle Valo 		break;
3397e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
33988e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
33998e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3400e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
3401e705c121SKalle Valo 		break;
3402e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3403e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3404475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
3405e705c121SKalle Valo 			break;
3406475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
3407475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
3408475c6bdeSJohannes Berg 		/* support HW crypto on TX */
3409475c6bdeSJohannes Berg 		return 0;
3410e705c121SKalle Valo 	default:
3411e705c121SKalle Valo 		/* currently FW supports only one optional cipher scheme */
3412e705c121SKalle Valo 		if (hw->n_cipher_schemes &&
3413e705c121SKalle Valo 		    hw->cipher_schemes->cipher == key->cipher)
3414e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3415e705c121SKalle Valo 		else
3416e705c121SKalle Valo 			return -EOPNOTSUPP;
3417e705c121SKalle Valo 	}
3418e705c121SKalle Valo 
3419e705c121SKalle Valo 	switch (cmd) {
3420e705c121SKalle Valo 	case SET_KEY:
3421e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
3422e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
3423e705c121SKalle Valo 			/*
3424e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
3425e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
3426e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
34278e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
3428e705c121SKalle Valo 			 */
34298e160ab8SAyala Beker 			if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
34308e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
34318e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
343281279c49SJohannes Berg 				ret = -EOPNOTSUPP;
343381279c49SJohannes Berg 			else
3434e705c121SKalle Valo 				ret = 0;
343585aeb58cSDavid Spinadel 
343685aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
343785aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
343885aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
3439e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
3440e705c121SKalle Valo 				break;
3441e705c121SKalle Valo 			}
3442c56e00a3SJohannes Berg 
3443c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
3444c56e00a3SJohannes Berg 				for (i = 0;
3445c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
3446c56e00a3SJohannes Berg 				     i++) {
3447c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
3448c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
3449c56e00a3SJohannes Berg 						break;
3450c56e00a3SJohannes Berg 					}
3451c56e00a3SJohannes Berg 				}
3452c56e00a3SJohannes Berg 
3453c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
3454c56e00a3SJohannes Berg 					ret = -ENOSPC;
3455c56e00a3SJohannes Berg 
3456c56e00a3SJohannes Berg 				break;
3457c56e00a3SJohannes Berg 			}
345885aeb58cSDavid Spinadel 		}
3459e705c121SKalle Valo 
3460e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
3461e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
3462e705c121SKalle Valo 		 */
3463e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3464e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
3465e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
3466e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
3467e705c121SKalle Valo 			ret = 0;
3468e705c121SKalle Valo 			break;
3469e705c121SKalle Valo 		}
3470e705c121SKalle Valo 
3471f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3472f5e28eacSJohannes Berg 		    sta && iwl_mvm_has_new_rx_api(mvm) &&
3473f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3474f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
34752a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
34762a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3477f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
3478f5e28eacSJohannes Berg 			int tid, q;
3479f5e28eacSJohannes Berg 
3480f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3481f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3482acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
3483acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
3484f5e28eacSJohannes Berg 					 GFP_KERNEL);
3485f5e28eacSJohannes Berg 			if (!ptk_pn) {
3486f5e28eacSJohannes Berg 				ret = -ENOMEM;
3487f5e28eacSJohannes Berg 				break;
3488f5e28eacSJohannes Berg 			}
3489f5e28eacSJohannes Berg 
3490f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3491f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
3492f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
3493f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
3494f5e28eacSJohannes Berg 					       seq.ccmp.pn,
3495f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
3496f5e28eacSJohannes Berg 			}
3497f5e28eacSJohannes Berg 
3498f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3499f5e28eacSJohannes Berg 		}
3500f5e28eacSJohannes Berg 
35014615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
35024615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
35034615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
35044615fd15SEmmanuel Grumbach 		else
35054615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
35064615fd15SEmmanuel Grumbach 
3507e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
35084615fd15SEmmanuel Grumbach 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
3509e705c121SKalle Valo 		if (ret) {
3510e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
3511475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
3512e705c121SKalle Valo 			/*
3513e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
3514475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
3515475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
3516475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
3517e705c121SKalle Valo 			 */
3518475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
3519475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
3520475c6bdeSJohannes Berg 			else
3521e705c121SKalle Valo 				ret = 0;
3522e705c121SKalle Valo 		}
3523e705c121SKalle Valo 
3524e705c121SKalle Valo 		break;
3525e705c121SKalle Valo 	case DISABLE_KEY:
3526c56e00a3SJohannes Berg 		ret = -ENOENT;
3527c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3528c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
3529c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
3530c56e00a3SJohannes Berg 				ret = 0;
3531c56e00a3SJohannes Berg 			}
3532c56e00a3SJohannes Berg 		}
3533c56e00a3SJohannes Berg 
3534c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
3535c56e00a3SJohannes Berg 		if (ret == 0)
3536c56e00a3SJohannes Berg 			break;
3537c56e00a3SJohannes Berg 
3538e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3539e705c121SKalle Valo 			ret = 0;
3540e705c121SKalle Valo 			break;
3541e705c121SKalle Valo 		}
3542e705c121SKalle Valo 
3543f5e28eacSJohannes Berg 		if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3544f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3545f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
35462a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
35472a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3548f5e28eacSJohannes Berg 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3549f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
3550f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
3551f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
3552f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3553f5e28eacSJohannes Berg 			if (ptk_pn)
3554f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
3555f5e28eacSJohannes Berg 		}
3556f5e28eacSJohannes Berg 
3557e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3558e705c121SKalle Valo 		ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3559e705c121SKalle Valo 		break;
3560e705c121SKalle Valo 	default:
3561e705c121SKalle Valo 		ret = -EINVAL;
3562e705c121SKalle Valo 	}
3563e705c121SKalle Valo 
35646569e7d3SJohannes Berg 	return ret;
35656569e7d3SJohannes Berg }
35666569e7d3SJohannes Berg 
35676569e7d3SJohannes Berg static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
35686569e7d3SJohannes Berg 			       enum set_key_cmd cmd,
35696569e7d3SJohannes Berg 			       struct ieee80211_vif *vif,
35706569e7d3SJohannes Berg 			       struct ieee80211_sta *sta,
35716569e7d3SJohannes Berg 			       struct ieee80211_key_conf *key)
35726569e7d3SJohannes Berg {
35736569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
35746569e7d3SJohannes Berg 	int ret;
35756569e7d3SJohannes Berg 
35766569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
35776569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
3578e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
35796569e7d3SJohannes Berg 
3580e705c121SKalle Valo 	return ret;
3581e705c121SKalle Valo }
3582e705c121SKalle Valo 
3583e705c121SKalle Valo static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3584e705c121SKalle Valo 					struct ieee80211_vif *vif,
3585e705c121SKalle Valo 					struct ieee80211_key_conf *keyconf,
3586e705c121SKalle Valo 					struct ieee80211_sta *sta,
3587e705c121SKalle Valo 					u32 iv32, u16 *phase1key)
3588e705c121SKalle Valo {
3589e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3590e705c121SKalle Valo 
3591e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3592e705c121SKalle Valo 		return;
3593e705c121SKalle Valo 
3594e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3595e705c121SKalle Valo }
3596e705c121SKalle Valo 
3597e705c121SKalle Valo 
3598e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3599e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
3600e705c121SKalle Valo {
3601e705c121SKalle Valo 	struct iwl_mvm *mvm =
3602e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3603e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
3604e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
3605e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
3606e705c121SKalle Valo 
3607e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3608e705c121SKalle Valo 		return true;
3609e705c121SKalle Valo 
3610e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3611e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3612e705c121SKalle Valo 		return true;
3613e705c121SKalle Valo 	}
3614e705c121SKalle Valo 
3615e705c121SKalle Valo 	resp = (void *)pkt->data;
3616e705c121SKalle Valo 
3617e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
3618b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
3619e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
3620e705c121SKalle Valo 
3621e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
3622e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3623e705c121SKalle Valo 		     te_data->uid);
3624e705c121SKalle Valo 
3625e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3626e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3627e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3628e705c121SKalle Valo 
3629e705c121SKalle Valo 	return true;
3630e705c121SKalle Valo }
3631e705c121SKalle Valo 
3632dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3633dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3634dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3635dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3636dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3637e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3638e705c121SKalle Valo 				    struct ieee80211_channel *channel,
3639e705c121SKalle Valo 				    struct ieee80211_vif *vif,
3640e705c121SKalle Valo 				    int duration)
3641e705c121SKalle Valo {
3642afc1e3b4SAvraham Stern 	int res;
3643e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3644e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3645e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
3646e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
3647dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
3648dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
3649dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
3650e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
3651e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3652e705c121SKalle Valo 		.id_and_color =
3653e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3654e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3655e705c121SKalle Valo 	};
365657e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
365757e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
365857e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
365957e861d9SDavid Spinadel 
366057e861d9SDavid Spinadel 	/* Set the channel info data */
366157e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
36623717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
366357e861d9SDavid Spinadel 			      PHY_VHT_CHANNEL_MODE20,
366457e861d9SDavid Spinadel 			      0);
366557e861d9SDavid Spinadel 
366657e861d9SDavid Spinadel 	/* Set the time and duration */
3667afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
3668e705c121SKalle Valo 
3669dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
3670dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
3671dc28e12fSMatti Gottlieb 
3672dc28e12fSMatti Gottlieb 	/*
3673dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
3674dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
3675dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
3676dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
3677dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
3678dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
3679dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
3680dc28e12fSMatti Gottlieb 	 */
3681dc28e12fSMatti Gottlieb 	if (vif->bss_conf.assoc) {
3682dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3683dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
3684dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
3685dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3686dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
3687dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
3688dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
3689dc28e12fSMatti Gottlieb 		}
3690dc28e12fSMatti Gottlieb 	}
3691dc28e12fSMatti Gottlieb 
369257e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
369357e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
3694dc28e12fSMatti Gottlieb 
3695dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
3696dc28e12fSMatti Gottlieb 		     "ROC: Requesting to remain on channel %u for %ums (requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3697dc28e12fSMatti Gottlieb 		     channel->hw_value, req_dur, duration, delay,
3698dc28e12fSMatti Gottlieb 		     dtim_interval);
3699e705c121SKalle Valo 	/* Set the node address */
370057e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
3701e705c121SKalle Valo 
3702e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3703e705c121SKalle Valo 
3704e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3705e705c121SKalle Valo 
3706e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3707e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3708e705c121SKalle Valo 		return -EIO;
3709e705c121SKalle Valo 	}
3710e705c121SKalle Valo 
3711e705c121SKalle Valo 	te_data->vif = vif;
3712e705c121SKalle Valo 	te_data->duration = duration;
3713e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
3714e705c121SKalle Valo 
3715e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3716e705c121SKalle Valo 
3717e705c121SKalle Valo 	/*
3718e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
3719e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
3720e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
3721e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
3722e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
3723e705c121SKalle Valo 	 * time another notification (that the time event started)
3724e705c121SKalle Valo 	 * might already be processed unsuccessfully.
3725e705c121SKalle Valo 	 */
3726e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3727e705c121SKalle Valo 				   time_event_response,
3728e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
3729e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
3730e705c121SKalle Valo 
373157e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
3732e705c121SKalle Valo 				   &aux_roc_req);
3733e705c121SKalle Valo 
3734e705c121SKalle Valo 	if (res) {
3735e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3736e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3737e705c121SKalle Valo 		goto out_clear_te;
3738e705c121SKalle Valo 	}
3739e705c121SKalle Valo 
3740e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
3741e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3742e705c121SKalle Valo 	/* should never fail */
3743e705c121SKalle Valo 	WARN_ON_ONCE(res);
3744e705c121SKalle Valo 
3745e705c121SKalle Valo 	if (res) {
3746e705c121SKalle Valo  out_clear_te:
3747e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
3748e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
3749e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
3750e705c121SKalle Valo 	}
3751e705c121SKalle Valo 
3752e705c121SKalle Valo 	return res;
3753e705c121SKalle Valo }
3754e705c121SKalle Valo 
3755e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
3756e705c121SKalle Valo 		       struct ieee80211_vif *vif,
3757e705c121SKalle Valo 		       struct ieee80211_channel *channel,
3758e705c121SKalle Valo 		       int duration,
3759e705c121SKalle Valo 		       enum ieee80211_roc_type type)
3760e705c121SKalle Valo {
3761e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3762e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3763e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
3764e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
3765e705c121SKalle Valo 	int ret, i;
3766e705c121SKalle Valo 
3767e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3768e705c121SKalle Valo 			   duration, type);
3769e705c121SKalle Valo 
37709834781cSJohannes Berg 	/*
37719834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
37729834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
37739834781cSJohannes Berg 	 */
3774e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
3775e705c121SKalle Valo 
3776e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3777e705c121SKalle Valo 
3778e705c121SKalle Valo 	switch (vif->type) {
3779e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3780e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
3781e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
3782e705c121SKalle Valo 			/* Use aux roc framework (HS20) */
3783e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3784e705c121SKalle Valo 						       vif, duration);
3785e705c121SKalle Valo 			goto out_unlock;
3786e705c121SKalle Valo 		}
3787e705c121SKalle Valo 		IWL_ERR(mvm, "hotspot not supported\n");
3788e705c121SKalle Valo 		ret = -EINVAL;
3789e705c121SKalle Valo 		goto out_unlock;
3790e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
3791e705c121SKalle Valo 		/* handle below */
3792e705c121SKalle Valo 		break;
3793e705c121SKalle Valo 	default:
3794e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
3795e705c121SKalle Valo 		ret = -EINVAL;
3796e705c121SKalle Valo 		goto out_unlock;
3797e705c121SKalle Valo 	}
3798e705c121SKalle Valo 
3799e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
3800e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
3801e705c121SKalle Valo 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3802e705c121SKalle Valo 			continue;
3803e705c121SKalle Valo 
3804e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3805e705c121SKalle Valo 			/*
3806e705c121SKalle Valo 			 * Unbind the P2P_DEVICE from the current PHY context,
3807e705c121SKalle Valo 			 * and if the PHY context is not used remove it.
3808e705c121SKalle Valo 			 */
3809e705c121SKalle Valo 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
3810e705c121SKalle Valo 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3811e705c121SKalle Valo 				goto out_unlock;
3812e705c121SKalle Valo 
3813e705c121SKalle Valo 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3814e705c121SKalle Valo 
3815e705c121SKalle Valo 			/* Bind the P2P_DEVICE to the current PHY Context */
3816e705c121SKalle Valo 			mvmvif->phy_ctxt = phy_ctxt;
3817e705c121SKalle Valo 
3818e705c121SKalle Valo 			ret = iwl_mvm_binding_add_vif(mvm, vif);
3819e705c121SKalle Valo 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3820e705c121SKalle Valo 				goto out_unlock;
3821e705c121SKalle Valo 
3822e705c121SKalle Valo 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3823e705c121SKalle Valo 			goto schedule_time_event;
3824e705c121SKalle Valo 		}
3825e705c121SKalle Valo 	}
3826e705c121SKalle Valo 
3827e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
3828e705c121SKalle Valo 	if (channel == mvmvif->phy_ctxt->channel)
3829e705c121SKalle Valo 		goto schedule_time_event;
3830e705c121SKalle Valo 
3831e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
3832e705c121SKalle Valo 
3833e705c121SKalle Valo 	/*
3834e705c121SKalle Valo 	 * Change the PHY context configuration as it is currently referenced
3835e705c121SKalle Valo 	 * only by the P2P Device MAC
3836e705c121SKalle Valo 	 */
3837e705c121SKalle Valo 	if (mvmvif->phy_ctxt->ref == 1) {
3838e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3839e705c121SKalle Valo 					       &chandef, 1, 1);
3840e705c121SKalle Valo 		if (ret)
3841e705c121SKalle Valo 			goto out_unlock;
3842e705c121SKalle Valo 	} else {
3843e705c121SKalle Valo 		/*
3844e705c121SKalle Valo 		 * The PHY context is shared with other MACs. Need to remove the
3845e705c121SKalle Valo 		 * P2P Device from the binding, allocate an new PHY context and
3846e705c121SKalle Valo 		 * create a new binding
3847e705c121SKalle Valo 		 */
3848e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3849e705c121SKalle Valo 		if (!phy_ctxt) {
3850e705c121SKalle Valo 			ret = -ENOSPC;
3851e705c121SKalle Valo 			goto out_unlock;
3852e705c121SKalle Valo 		}
3853e705c121SKalle Valo 
3854e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3855e705c121SKalle Valo 					       1, 1);
3856e705c121SKalle Valo 		if (ret) {
3857e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
3858e705c121SKalle Valo 			goto out_unlock;
3859e705c121SKalle Valo 		}
3860e705c121SKalle Valo 
3861e705c121SKalle Valo 		/* Unbind the P2P_DEVICE from the current PHY context */
3862e705c121SKalle Valo 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
3863e705c121SKalle Valo 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3864e705c121SKalle Valo 			goto out_unlock;
3865e705c121SKalle Valo 
3866e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3867e705c121SKalle Valo 
3868e705c121SKalle Valo 		/* Bind the P2P_DEVICE to the new allocated PHY context */
3869e705c121SKalle Valo 		mvmvif->phy_ctxt = phy_ctxt;
3870e705c121SKalle Valo 
3871e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
3872e705c121SKalle Valo 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3873e705c121SKalle Valo 			goto out_unlock;
3874e705c121SKalle Valo 
3875e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3876e705c121SKalle Valo 	}
3877e705c121SKalle Valo 
3878e705c121SKalle Valo schedule_time_event:
3879e705c121SKalle Valo 	/* Schedule the time events */
3880e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
3881e705c121SKalle Valo 
3882e705c121SKalle Valo out_unlock:
3883e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3884e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3885e705c121SKalle Valo 	return ret;
3886e705c121SKalle Valo }
3887e705c121SKalle Valo 
38885db4c4b9SEmmanuel Grumbach static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
38895db4c4b9SEmmanuel Grumbach 			      struct ieee80211_vif *vif)
3890e705c121SKalle Valo {
3891e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3892e705c121SKalle Valo 
3893e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
3894e705c121SKalle Valo 
3895e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3896fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
3897e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3898e705c121SKalle Valo 
3899e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
3900e705c121SKalle Valo 	return 0;
3901e705c121SKalle Valo }
3902e705c121SKalle Valo 
3903b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
3904b73f9a4aSJohannes Berg 	bool responder;
3905b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
3906b73f9a4aSJohannes Berg };
3907b73f9a4aSJohannes Berg 
3908b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
3909b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
3910b73f9a4aSJohannes Berg {
3911b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
3912b73f9a4aSJohannes Berg 
3913b73f9a4aSJohannes Berg 	if (rcu_access_pointer(vif->chanctx_conf) == data->ctx &&
3914b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
3915b73f9a4aSJohannes Berg 		data->responder = true;
3916b73f9a4aSJohannes Berg }
3917b73f9a4aSJohannes Berg 
3918b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
3919b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
3920b73f9a4aSJohannes Berg {
3921b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
3922b73f9a4aSJohannes Berg 		.responder = false,
3923b73f9a4aSJohannes Berg 		.ctx = ctx,
3924b73f9a4aSJohannes Berg 	};
3925b73f9a4aSJohannes Berg 
3926b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
3927b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
3928b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
3929b73f9a4aSJohannes Berg 					&data);
3930b73f9a4aSJohannes Berg 	return data.responder;
3931b73f9a4aSJohannes Berg }
3932b73f9a4aSJohannes Berg 
3933e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
3934e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
3935e705c121SKalle Valo {
3936e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3937e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
3938b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
3939b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
3940e705c121SKalle Valo 	int ret;
3941e705c121SKalle Valo 
3942e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3943e705c121SKalle Valo 
3944e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
3945e705c121SKalle Valo 
3946e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3947e705c121SKalle Valo 	if (!phy_ctxt) {
3948e705c121SKalle Valo 		ret = -ENOSPC;
3949e705c121SKalle Valo 		goto out;
3950e705c121SKalle Valo 	}
3951e705c121SKalle Valo 
3952b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
3953e705c121SKalle Valo 				       ctx->rx_chains_static,
3954e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
3955e705c121SKalle Valo 	if (ret) {
3956e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
3957e705c121SKalle Valo 		goto out;
3958e705c121SKalle Valo 	}
3959e705c121SKalle Valo 
3960e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
3961e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
3962e705c121SKalle Valo out:
3963e705c121SKalle Valo 	return ret;
3964e705c121SKalle Valo }
3965e705c121SKalle Valo 
3966e705c121SKalle Valo static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
3967e705c121SKalle Valo 			       struct ieee80211_chanctx_conf *ctx)
3968e705c121SKalle Valo {
3969e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3970e705c121SKalle Valo 	int ret;
3971e705c121SKalle Valo 
3972e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3973e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
3974e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3975e705c121SKalle Valo 
3976e705c121SKalle Valo 	return ret;
3977e705c121SKalle Valo }
3978e705c121SKalle Valo 
3979e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
3980e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
3981e705c121SKalle Valo {
3982e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3983e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3984e705c121SKalle Valo 
3985e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3986e705c121SKalle Valo 
3987e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
3988e705c121SKalle Valo }
3989e705c121SKalle Valo 
3990e705c121SKalle Valo static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
3991e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx)
3992e705c121SKalle Valo {
3993e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3994e705c121SKalle Valo 
3995e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3996e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
3997e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3998e705c121SKalle Valo }
3999e705c121SKalle Valo 
4000e705c121SKalle Valo static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4001e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx,
4002e705c121SKalle Valo 				   u32 changed)
4003e705c121SKalle Valo {
4004e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4005e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4006e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4007b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4008b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4009e705c121SKalle Valo 
4010e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4011e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4012e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4013e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4014e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4015e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4016e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4017e705c121SKalle Valo 		return;
4018e705c121SKalle Valo 
4019e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
40207a20bcceSEmmanuel Grumbach 
40217a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
40227a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4023b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
40247a20bcceSEmmanuel Grumbach 			goto out_unlock;
40257a20bcceSEmmanuel Grumbach 
40267a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
40277a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4028b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
40297a20bcceSEmmanuel Grumbach 			goto out_unlock;
40307a20bcceSEmmanuel Grumbach 	}
40317a20bcceSEmmanuel Grumbach 
4032e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4033b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4034e705c121SKalle Valo 				 ctx->rx_chains_static,
4035e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
40367a20bcceSEmmanuel Grumbach 
40377a20bcceSEmmanuel Grumbach out_unlock:
4038e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4039e705c121SKalle Valo }
4040e705c121SKalle Valo 
4041e705c121SKalle Valo static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4042e705c121SKalle Valo 					struct ieee80211_vif *vif,
4043e705c121SKalle Valo 					struct ieee80211_chanctx_conf *ctx,
4044e705c121SKalle Valo 					bool switching_chanctx)
4045e705c121SKalle Valo {
4046e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4047e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4048e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4049e705c121SKalle Valo 	int ret;
4050e705c121SKalle Valo 
4051e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4052e705c121SKalle Valo 
4053e705c121SKalle Valo 	mvmvif->phy_ctxt = phy_ctxt;
4054e705c121SKalle Valo 
4055e705c121SKalle Valo 	switch (vif->type) {
4056e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4057e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4058e705c121SKalle Valo 		if (switching_chanctx) {
4059e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4060e705c121SKalle Valo 			break;
4061e705c121SKalle Valo 		}
4062244726ecSLuca Coelho 		/* fall through */
4063e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4064e705c121SKalle Valo 		/*
4065e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4066e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4067e705c121SKalle Valo 		 */
4068e705c121SKalle Valo 		ret = 0;
4069e705c121SKalle Valo 		goto out;
4070e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
407119125cb0SAndrei Otcheretianski 		mvmvif->csa_bcn_pending = false;
4072e705c121SKalle Valo 		break;
4073e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4074e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4075e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4076e705c121SKalle Valo 		break;
4077e705c121SKalle Valo 	default:
4078e705c121SKalle Valo 		ret = -EINVAL;
4079e705c121SKalle Valo 		goto out;
4080e705c121SKalle Valo 	}
4081e705c121SKalle Valo 
4082e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4083e705c121SKalle Valo 	if (ret)
4084e705c121SKalle Valo 		goto out;
4085e705c121SKalle Valo 
4086e705c121SKalle Valo 	/*
4087e705c121SKalle Valo 	 * Power state must be updated before quotas,
4088e705c121SKalle Valo 	 * otherwise fw will complain.
4089e705c121SKalle Valo 	 */
4090e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4091e705c121SKalle Valo 
4092e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4093e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4094e705c121SKalle Valo 	 * will handle quota settings.
4095e705c121SKalle Valo 	 */
4096e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4097e705c121SKalle Valo 		mvmvif->monitor_active = true;
4098e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4099e705c121SKalle Valo 		if (ret)
4100e705c121SKalle Valo 			goto out_remove_binding;
41010e39eb03SChaya Rachel Ivgi 
41020e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
41030e39eb03SChaya Rachel Ivgi 		if (ret)
41040e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
41050e39eb03SChaya Rachel Ivgi 
4106e705c121SKalle Valo 	}
4107e705c121SKalle Valo 
4108e705c121SKalle Valo 	/* Handle binding during CSA */
4109e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4110e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4111e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4112e705c121SKalle Valo 	}
4113e705c121SKalle Valo 
4114e705c121SKalle Valo 	if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
411574a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
411674a10252SSara Sharon 
411774a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
411874a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
411919125cb0SAndrei Otcheretianski 			u32 duration = 3 * vif->bss_conf.beacon_int;
4120e705c121SKalle Valo 
4121e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4122e705c121SKalle Valo 			 * beacon on the new channel.
4123e705c121SKalle Valo 			 */
4124e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4125e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4126e705c121SKalle Valo 						true);
412774a10252SSara Sharon 		}
4128e705c121SKalle Valo 
4129e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4130e705c121SKalle Valo 	}
4131e705c121SKalle Valo 
4132e705c121SKalle Valo 	goto out;
4133e705c121SKalle Valo 
4134e705c121SKalle Valo out_remove_binding:
4135e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4136e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4137e705c121SKalle Valo out:
4138e705c121SKalle Valo 	if (ret)
4139e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
4140e705c121SKalle Valo 	return ret;
4141e705c121SKalle Valo }
4142e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4143e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4144e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4145e705c121SKalle Valo {
4146e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4147e705c121SKalle Valo 	int ret;
4148e705c121SKalle Valo 
4149e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4150e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
4151e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4152e705c121SKalle Valo 
4153e705c121SKalle Valo 	return ret;
4154e705c121SKalle Valo }
4155e705c121SKalle Valo 
4156e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4157e705c121SKalle Valo 					   struct ieee80211_vif *vif,
4158e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
4159e705c121SKalle Valo 					   bool switching_chanctx)
4160e705c121SKalle Valo {
4161e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4162e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
4163e705c121SKalle Valo 
4164e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4165e705c121SKalle Valo 
4166e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4167e705c121SKalle Valo 
4168e705c121SKalle Valo 	switch (vif->type) {
4169e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4170e705c121SKalle Valo 		goto out;
4171e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4172e705c121SKalle Valo 		mvmvif->monitor_active = false;
4173e705c121SKalle Valo 		mvmvif->ps_disabled = false;
41740e39eb03SChaya Rachel Ivgi 		iwl_mvm_rm_snif_sta(mvm, vif);
4175e705c121SKalle Valo 		break;
4176e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4177e705c121SKalle Valo 		/* This part is triggered only during CSA */
4178e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
4179e705c121SKalle Valo 			goto out;
4180e705c121SKalle Valo 
4181e705c121SKalle Valo 		mvmvif->csa_countdown = false;
4182e705c121SKalle Valo 
4183e705c121SKalle Valo 		/* Set CS bit on all the stations */
4184e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4185e705c121SKalle Valo 
4186e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
4187e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4188e705c121SKalle Valo 
4189e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
4190e705c121SKalle Valo 		break;
4191e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4192e705c121SKalle Valo 		if (!switching_chanctx)
4193e705c121SKalle Valo 			break;
4194e705c121SKalle Valo 
4195e705c121SKalle Valo 		disabled_vif = vif;
4196e705c121SKalle Valo 
419774a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
419874a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4199e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
4200e705c121SKalle Valo 		break;
4201e705c121SKalle Valo 	default:
4202e705c121SKalle Valo 		break;
4203e705c121SKalle Valo 	}
4204e705c121SKalle Valo 
4205e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
4206e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4207e705c121SKalle Valo 
4208e705c121SKalle Valo out:
4209e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
4210e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4211e705c121SKalle Valo }
4212e705c121SKalle Valo 
4213e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4214e705c121SKalle Valo 					 struct ieee80211_vif *vif,
4215e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
4216e705c121SKalle Valo {
4217e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4218e705c121SKalle Valo 
4219e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4220e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
4221e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4222e705c121SKalle Valo }
4223e705c121SKalle Valo 
4224e705c121SKalle Valo static int
4225e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4226e705c121SKalle Valo 				struct ieee80211_vif_chanctx_switch *vifs)
4227e705c121SKalle Valo {
4228e705c121SKalle Valo 	int ret;
4229e705c121SKalle Valo 
4230e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4231e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4232e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4233e705c121SKalle Valo 
4234e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4235e705c121SKalle Valo 	if (ret) {
4236e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4237e705c121SKalle Valo 		goto out_reassign;
4238e705c121SKalle Valo 	}
4239e705c121SKalle Valo 
4240e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4241e705c121SKalle Valo 					   true);
4242e705c121SKalle Valo 	if (ret) {
4243e705c121SKalle Valo 		IWL_ERR(mvm,
4244e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4245e705c121SKalle Valo 		goto out_remove;
4246e705c121SKalle Valo 	}
4247e705c121SKalle Valo 
4248e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
4249e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
4250e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
4251e705c121SKalle Valo 
4252e705c121SKalle Valo 	goto out;
4253e705c121SKalle Valo 
4254e705c121SKalle Valo out_remove:
4255e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4256e705c121SKalle Valo 
4257e705c121SKalle Valo out_reassign:
4258e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
4259e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4260e705c121SKalle Valo 		goto out_restart;
4261e705c121SKalle Valo 	}
4262e705c121SKalle Valo 
4263e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4264e705c121SKalle Valo 					 true)) {
4265e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4266e705c121SKalle Valo 		goto out_restart;
4267e705c121SKalle Valo 	}
4268e705c121SKalle Valo 
4269e705c121SKalle Valo 	goto out;
4270e705c121SKalle Valo 
4271e705c121SKalle Valo out_restart:
4272e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4273e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4274e705c121SKalle Valo 
4275e705c121SKalle Valo out:
4276e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4277e705c121SKalle Valo 
4278e705c121SKalle Valo 	return ret;
4279e705c121SKalle Valo }
4280e705c121SKalle Valo 
4281e705c121SKalle Valo static int
4282e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4283e705c121SKalle Valo 				    struct ieee80211_vif_chanctx_switch *vifs)
4284e705c121SKalle Valo {
4285e705c121SKalle Valo 	int ret;
4286e705c121SKalle Valo 
4287e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4288e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4289e705c121SKalle Valo 
4290e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4291e705c121SKalle Valo 					   true);
4292e705c121SKalle Valo 	if (ret) {
4293e705c121SKalle Valo 		IWL_ERR(mvm,
4294e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4295e705c121SKalle Valo 		goto out_reassign;
4296e705c121SKalle Valo 	}
4297e705c121SKalle Valo 
4298e705c121SKalle Valo 	goto out;
4299e705c121SKalle Valo 
4300e705c121SKalle Valo out_reassign:
4301e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4302e705c121SKalle Valo 					 true)) {
4303e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4304e705c121SKalle Valo 		goto out_restart;
4305e705c121SKalle Valo 	}
4306e705c121SKalle Valo 
4307e705c121SKalle Valo 	goto out;
4308e705c121SKalle Valo 
4309e705c121SKalle Valo out_restart:
4310e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4311e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4312e705c121SKalle Valo 
4313e705c121SKalle Valo out:
4314e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4315e705c121SKalle Valo 
4316e705c121SKalle Valo 	return ret;
4317e705c121SKalle Valo }
4318e705c121SKalle Valo 
4319e705c121SKalle Valo static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4320e705c121SKalle Valo 				      struct ieee80211_vif_chanctx_switch *vifs,
4321e705c121SKalle Valo 				      int n_vifs,
4322e705c121SKalle Valo 				      enum ieee80211_chanctx_switch_mode mode)
4323e705c121SKalle Valo {
4324e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4325e705c121SKalle Valo 	int ret;
4326e705c121SKalle Valo 
4327e705c121SKalle Valo 	/* we only support a single-vif right now */
4328e705c121SKalle Valo 	if (n_vifs > 1)
4329e705c121SKalle Valo 		return -EOPNOTSUPP;
4330e705c121SKalle Valo 
4331e705c121SKalle Valo 	switch (mode) {
4332e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
4333e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4334e705c121SKalle Valo 		break;
4335e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
4336e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
4337e705c121SKalle Valo 		break;
4338e705c121SKalle Valo 	default:
4339e705c121SKalle Valo 		ret = -EOPNOTSUPP;
4340e705c121SKalle Valo 		break;
4341e705c121SKalle Valo 	}
4342e705c121SKalle Valo 
4343e705c121SKalle Valo 	return ret;
4344e705c121SKalle Valo }
4345e705c121SKalle Valo 
43462f0282dbSJohannes Berg static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
43472f0282dbSJohannes Berg {
43482f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
43492f0282dbSJohannes Berg 
43502f0282dbSJohannes Berg 	return mvm->ibss_manager;
43512f0282dbSJohannes Berg }
43522f0282dbSJohannes Berg 
4353e705c121SKalle Valo static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4354e705c121SKalle Valo 			   struct ieee80211_sta *sta,
4355e705c121SKalle Valo 			   bool set)
4356e705c121SKalle Valo {
4357e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4358e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4359e705c121SKalle Valo 
4360e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
4361e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
4362e705c121SKalle Valo 		return -EINVAL;
4363e705c121SKalle Valo 	}
4364e705c121SKalle Valo 
4365e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4366e705c121SKalle Valo }
4367e705c121SKalle Valo 
4368e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
4369e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4370e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4371e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
4372e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
4373e705c121SKalle Valo };
4374e705c121SKalle Valo 
4375e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4376e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4377e705c121SKalle Valo 				      void *data, int len)
4378e705c121SKalle Valo {
4379e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4380e705c121SKalle Valo 	int err;
4381e705c121SKalle Valo 	u32 noa_duration;
4382e705c121SKalle Valo 
43838cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
43848cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
4385e705c121SKalle Valo 	if (err)
4386e705c121SKalle Valo 		return err;
4387e705c121SKalle Valo 
4388e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
4389e705c121SKalle Valo 		return -EINVAL;
4390e705c121SKalle Valo 
4391e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4392e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
4393e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4394e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
4395e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4396e705c121SKalle Valo 			return -EINVAL;
4397e705c121SKalle Valo 
4398e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4399e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
4400e705c121SKalle Valo 			return -EINVAL;
4401e705c121SKalle Valo 
4402e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
4403e705c121SKalle Valo 		mvm->noa_vif = vif;
4404e705c121SKalle Valo 
440522b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
4406e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4407e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
4408e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4409e705c121SKalle Valo 		    !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
4410e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4411e705c121SKalle Valo 			return -EINVAL;
4412e705c121SKalle Valo 
4413e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
4414e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4415e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4416e705c121SKalle Valo 	}
4417e705c121SKalle Valo 
4418e705c121SKalle Valo 	return -EOPNOTSUPP;
4419e705c121SKalle Valo }
4420e705c121SKalle Valo 
4421e705c121SKalle Valo static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4422e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4423e705c121SKalle Valo 				    void *data, int len)
4424e705c121SKalle Valo {
4425e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4426e705c121SKalle Valo 	int err;
4427e705c121SKalle Valo 
4428e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4429e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4430e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4431e705c121SKalle Valo 
4432e705c121SKalle Valo 	return err;
4433e705c121SKalle Valo }
4434e705c121SKalle Valo #endif
4435e705c121SKalle Valo 
4436e705c121SKalle Valo static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4437e705c121SKalle Valo 				   struct ieee80211_vif *vif,
4438e705c121SKalle Valo 				   struct ieee80211_channel_switch *chsw)
4439e705c121SKalle Valo {
4440e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
4441e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
4442e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
4443e705c121SKalle Valo 	 * (which is when the absence time event starts).
4444e705c121SKalle Valo 	 */
4445e705c121SKalle Valo 
4446e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4447e705c121SKalle Valo 			   "dummy channel switch op\n");
4448e705c121SKalle Valo }
4449e705c121SKalle Valo 
445074a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
445174a10252SSara Sharon 				       struct ieee80211_vif *vif,
445274a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
445374a10252SSara Sharon {
445474a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
445574a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
445674a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
445774a10252SSara Sharon 							  mvmvif->color)),
445874a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
445974a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
446074a10252SSara Sharon 		.cs_count = chsw->count,
446177738865SSara Sharon 		.cs_mode = chsw->block_tx,
446274a10252SSara Sharon 	};
446374a10252SSara Sharon 
446474a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
446574a10252SSara Sharon 
44669cfcf71cSSara Sharon 	if (chsw->delay)
44679cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
44689cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
44699cfcf71cSSara Sharon 
447074a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
447174a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
447274a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
447374a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
447474a10252SSara Sharon }
447574a10252SSara Sharon 
44760202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
44770202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
44780202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
44790202bcf0SEmmanuel Grumbach {
44800202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
44810202bcf0SEmmanuel Grumbach 	u32 apply_time;
44820202bcf0SEmmanuel Grumbach 
44830202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
44840202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
44850202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
44860202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
44870202bcf0SEmmanuel Grumbach 	 * loss).
44880202bcf0SEmmanuel Grumbach 	 */
44890202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
44900202bcf0SEmmanuel Grumbach 		apply_time = 0;
44910202bcf0SEmmanuel Grumbach 	else
44920202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
44930202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
44940202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
44950202bcf0SEmmanuel Grumbach 
44960202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
44970202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
44980202bcf0SEmmanuel Grumbach 
44990202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
45000202bcf0SEmmanuel Grumbach 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
45010202bcf0SEmmanuel Grumbach 
45020202bcf0SEmmanuel Grumbach 		if (ret)
45030202bcf0SEmmanuel Grumbach 			return ret;
45040202bcf0SEmmanuel Grumbach 	}
45050202bcf0SEmmanuel Grumbach 
45060202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
45070202bcf0SEmmanuel Grumbach 				    apply_time);
45080202bcf0SEmmanuel Grumbach 
45090202bcf0SEmmanuel Grumbach 	return 0;
45100202bcf0SEmmanuel Grumbach }
45110202bcf0SEmmanuel Grumbach 
4512f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
4513e705c121SKalle Valo static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4514e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4515e705c121SKalle Valo 				      struct ieee80211_channel_switch *chsw)
4516e705c121SKalle Valo {
4517e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4518e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
4519e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4520e705c121SKalle Valo 	int ret;
4521e705c121SKalle Valo 
4522e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4523e705c121SKalle Valo 
4524e705c121SKalle Valo 	mvmvif->csa_failed = false;
4525e705c121SKalle Valo 
4526e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
4527e705c121SKalle Valo 			   chsw->chandef.center_freq1);
4528e705c121SKalle Valo 
45297174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
45307174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
45317174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
4532e705c121SKalle Valo 
4533e705c121SKalle Valo 	switch (vif->type) {
4534e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4535e705c121SKalle Valo 		csa_vif =
4536e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
4537e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
4538e705c121SKalle Valo 		if (WARN_ONCE(csa_vif && csa_vif->csa_active,
4539e705c121SKalle Valo 			      "Another CSA is already in progress")) {
4540e705c121SKalle Valo 			ret = -EBUSY;
4541e705c121SKalle Valo 			goto out_unlock;
4542e705c121SKalle Valo 		}
4543e705c121SKalle Valo 
4544d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
4545d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
4546d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
4547d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
4548d3a108a4SAndrei Otcheretianski 			goto out_unlock;
4549d3a108a4SAndrei Otcheretianski 		}
4550d3a108a4SAndrei Otcheretianski 
4551e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
4552e705c121SKalle Valo 
4553e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
4554e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
4555e705c121SKalle Valo 			ret = -EBUSY;
4556e705c121SKalle Valo 			goto out_unlock;
4557e705c121SKalle Valo 		}
4558e705c121SKalle Valo 
4559d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
4560d3a108a4SAndrei Otcheretianski 
4561e705c121SKalle Valo 		break;
4562e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4563f6780614SSara Sharon 		if (chsw->block_tx) {
4564f6780614SSara Sharon 			/*
4565f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
4566f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
4567f6780614SSara Sharon 			 */
4568f6780614SSara Sharon 			if (!chsw->count ||
4569f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
4570f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
4571f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
4572f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
4573f6780614SSara Sharon 		}
4574e705c121SKalle Valo 
45750202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
45760202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
45770202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
4578e705c121SKalle Valo 			if (ret)
4579e705c121SKalle Valo 				goto out_unlock;
45800202bcf0SEmmanuel Grumbach 		} else {
458174a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
45820202bcf0SEmmanuel Grumbach 		}
458381b4e44eSSara Sharon 
458481b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
458581b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
4586e705c121SKalle Valo 		break;
4587e705c121SKalle Valo 	default:
4588e705c121SKalle Valo 		break;
4589e705c121SKalle Valo 	}
4590e705c121SKalle Valo 
4591e705c121SKalle Valo 	mvmvif->ps_disabled = true;
4592e705c121SKalle Valo 
4593e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
4594e705c121SKalle Valo 	if (ret)
4595e705c121SKalle Valo 		goto out_unlock;
4596e705c121SKalle Valo 
4597e705c121SKalle Valo 	/* we won't be on this channel any longer */
4598e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
4599e705c121SKalle Valo 
4600e705c121SKalle Valo out_unlock:
4601e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4602e705c121SKalle Valo 
4603e705c121SKalle Valo 	return ret;
4604e705c121SKalle Valo }
4605e705c121SKalle Valo 
4606c37763d2SSara Sharon static void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
4607c37763d2SSara Sharon 					     struct ieee80211_vif *vif,
4608c37763d2SSara Sharon 					     struct ieee80211_channel_switch *chsw)
4609e705c121SKalle Valo {
4610e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4611c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4612c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
4613c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4614c37763d2SSara Sharon 							  mvmvif->color)),
4615c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
4616c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
4617c37763d2SSara Sharon 		.cs_count = chsw->count,
461877738865SSara Sharon 		.cs_mode = chsw->block_tx,
4619c37763d2SSara Sharon 	};
4620e705c121SKalle Valo 
4621c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
4622c37763d2SSara Sharon 		return;
4623e705c121SKalle Valo 
462481b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
462581b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
462681b4e44eSSara Sharon 			/* Second time, give up on this AP*/
462781b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
462881b4e44eSSara Sharon 			ieee80211_chswitch_done(vif, false);
462981b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
463081b4e44eSSara Sharon 			return;
4631e705c121SKalle Valo 		}
463281b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
4633e705c121SKalle Valo 	}
463481b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
4635e705c121SKalle Valo 
4636c37763d2SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d\n", mvmvif->id);
4637e705c121SKalle Valo 
4638c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
4639c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
4640c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
4641c37763d2SSara Sharon 				     CMD_ASYNC, sizeof(cmd), &cmd));
4642e705c121SKalle Valo }
4643e705c121SKalle Valo 
46446110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
46456110d9e5SDavid Spinadel {
4646435d0827SSara Sharon 	int i;
4647435d0827SSara Sharon 
464806195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
4649309c4848SLuca Coelho 		if (drop) {
4650309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
46516110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
4652435d0827SSara Sharon 				iwl_mvm_flushable_queues(mvm) & queues, 0);
4653309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
4654309c4848SLuca Coelho 		} else {
4655435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
4656309c4848SLuca Coelho 		}
4657435d0827SSara Sharon 		return;
4658435d0827SSara Sharon 	}
46596110d9e5SDavid Spinadel 
46606110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
46616110d9e5SDavid Spinadel 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4662435d0827SSara Sharon 		struct ieee80211_sta *sta;
4663435d0827SSara Sharon 
4664435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
46656110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
46666110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
46676110d9e5SDavid Spinadel 			continue;
46686110d9e5SDavid Spinadel 
466906195639SSara Sharon 		if (drop)
4670d66bd0c4SHaim Dreyfuss 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF, 0);
467106195639SSara Sharon 		else
46726110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
46736110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
46746110d9e5SDavid Spinadel 	}
46756110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
46766110d9e5SDavid Spinadel }
46776110d9e5SDavid Spinadel 
4678e705c121SKalle Valo static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4679e705c121SKalle Valo 			      struct ieee80211_vif *vif, u32 queues, bool drop)
4680e705c121SKalle Valo {
4681e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4682e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
4683e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4684e705c121SKalle Valo 	struct ieee80211_sta *sta;
4685e705c121SKalle Valo 	int i;
4686e705c121SKalle Valo 	u32 msk = 0;
4687e705c121SKalle Valo 
46886110d9e5SDavid Spinadel 	if (!vif) {
46896110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
46906110d9e5SDavid Spinadel 		return;
46916110d9e5SDavid Spinadel 	}
46926110d9e5SDavid Spinadel 
46936110d9e5SDavid Spinadel 	if (vif->type != NL80211_IFTYPE_STATION)
4694e705c121SKalle Valo 		return;
4695e705c121SKalle Valo 
469624afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
469724afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
469824afba76SLiad Kaufman 
4699e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4700e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
4701e705c121SKalle Valo 
4702e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
47030ae98812SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4704e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4705e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
4706e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
4707e705c121SKalle Valo 			continue;
4708e705c121SKalle Valo 
4709e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4710e705c121SKalle Valo 		if (mvmsta->vif != vif)
4711e705c121SKalle Valo 			continue;
4712e705c121SKalle Valo 
4713e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
4714e705c121SKalle Valo 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4715e705c121SKalle Valo 
4716d49394a1SSara Sharon 		if (drop) {
4717d49394a1SSara Sharon 			if (iwl_mvm_flush_sta(mvm, mvmsta, false, 0))
4718d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
4719d49394a1SSara Sharon 		} else {
4720e705c121SKalle Valo 			msk |= mvmsta->tfd_queue_msk;
4721d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
4722d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
4723e705c121SKalle Valo 		}
4724d49394a1SSara Sharon 	}
4725e705c121SKalle Valo 
4726e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4727e705c121SKalle Valo 
4728e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
4729e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
4730e705c121SKalle Valo 	 */
4731d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
4732a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
4733e705c121SKalle Valo }
4734e705c121SKalle Valo 
4735e705c121SKalle Valo static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4736e705c121SKalle Valo 				  struct survey_info *survey)
4737e705c121SKalle Valo {
4738e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4739e705c121SKalle Valo 	int ret;
4740e705c121SKalle Valo 
4741e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
4742e705c121SKalle Valo 
4743e705c121SKalle Valo 	/* only support global statistics right now */
4744e705c121SKalle Valo 	if (idx != 0)
4745e705c121SKalle Valo 		return -ENOENT;
4746e705c121SKalle Valo 
4747280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4748e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4749e705c121SKalle Valo 		return -ENOENT;
4750e705c121SKalle Valo 
4751e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4752e705c121SKalle Valo 
4753aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
4754e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
4755e705c121SKalle Valo 		if (ret)
4756e705c121SKalle Valo 			goto out;
4757e705c121SKalle Valo 	}
4758e705c121SKalle Valo 
4759e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
4760e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
4761e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
4762e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
4763e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
4764e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
4765e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
4766e705c121SKalle Valo 
4767e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
4768e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
4769e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
4770e705c121SKalle Valo 
4771e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
4772e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
4773e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
4774e705c121SKalle Valo 
4775e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4776e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
4777e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
4778e705c121SKalle Valo 
4779e705c121SKalle Valo 	ret = 0;
4780e705c121SKalle Valo  out:
4781e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4782e705c121SKalle Valo 	return ret;
4783e705c121SKalle Valo }
4784e705c121SKalle Valo 
4785ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
4786ed780545SJohannes Berg {
4787ed780545SJohannes Berg 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
4788ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
4789ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
4790ed780545SJohannes Berg 		break;
4791ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
4792ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
4793ed780545SJohannes Berg 		break;
4794ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
4795ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
4796ed780545SJohannes Berg 		break;
4797ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
4798ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
4799ed780545SJohannes Berg 		break;
4800ed780545SJohannes Berg 	}
4801ed780545SJohannes Berg 
4802ed780545SJohannes Berg 	if (rate_n_flags & RATE_MCS_HT_MSK) {
4803ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
4804ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags, RATE_HT_MCS_INDEX_MSK);
4805ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4806ed780545SJohannes Berg 					  RATE_HT_MCS_NSS_MSK) + 1;
4807ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_SGI_MSK)
4808ed780545SJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
4809ed780545SJohannes Berg 	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
4810ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
4811ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags,
4812ed780545SJohannes Berg 					  RATE_VHT_MCS_RATE_CODE_MSK);
4813ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4814ed780545SJohannes Berg 					  RATE_VHT_MCS_NSS_MSK) + 1;
4815ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_SGI_MSK)
4816ed780545SJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
4817ed780545SJohannes Berg 	} else if (rate_n_flags & RATE_MCS_HE_MSK) {
4818ed780545SJohannes Berg 		u32 gi_ltf = u32_get_bits(rate_n_flags,
4819ed780545SJohannes Berg 					  RATE_MCS_HE_GI_LTF_MSK);
4820ed780545SJohannes Berg 
4821ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
4822ed780545SJohannes Berg 		rinfo->mcs = u32_get_bits(rate_n_flags,
4823ed780545SJohannes Berg 					  RATE_VHT_MCS_RATE_CODE_MSK);
4824ed780545SJohannes Berg 		rinfo->nss = u32_get_bits(rate_n_flags,
4825ed780545SJohannes Berg 					  RATE_VHT_MCS_NSS_MSK) + 1;
4826ed780545SJohannes Berg 
4827ed780545SJohannes Berg 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
4828ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
4829ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
4830ed780545SJohannes Berg 		}
4831ed780545SJohannes Berg 
4832ed780545SJohannes Berg 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
4833ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_SU:
4834ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_EXT_SU:
4835ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4836ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4837ed780545SJohannes Berg 			else if (gi_ltf == 2)
4838ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4839ed780545SJohannes Berg 			else if (rate_n_flags & RATE_MCS_SGI_MSK)
4840ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4841ed780545SJohannes Berg 			else
4842ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4843ed780545SJohannes Berg 			break;
4844ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_MU:
4845ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4846ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
4847ed780545SJohannes Berg 			else if (gi_ltf == 2)
4848ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4849ed780545SJohannes Berg 			else
4850ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4851ed780545SJohannes Berg 			break;
4852ed780545SJohannes Berg 		case RATE_MCS_HE_TYPE_TRIG:
4853ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
4854ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
4855ed780545SJohannes Berg 			else
4856ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
4857ed780545SJohannes Berg 			break;
4858ed780545SJohannes Berg 		}
4859ed780545SJohannes Berg 
4860ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
4861ed780545SJohannes Berg 			rinfo->he_dcm = 1;
4862ed780545SJohannes Berg 	} else {
4863ed780545SJohannes Berg 		switch (u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK)) {
4864ed780545SJohannes Berg 		case IWL_RATE_1M_PLCP:
4865ed780545SJohannes Berg 			rinfo->legacy = 10;
4866ed780545SJohannes Berg 			break;
4867ed780545SJohannes Berg 		case IWL_RATE_2M_PLCP:
4868ed780545SJohannes Berg 			rinfo->legacy = 20;
4869ed780545SJohannes Berg 			break;
4870ed780545SJohannes Berg 		case IWL_RATE_5M_PLCP:
4871ed780545SJohannes Berg 			rinfo->legacy = 55;
4872ed780545SJohannes Berg 			break;
4873ed780545SJohannes Berg 		case IWL_RATE_11M_PLCP:
4874ed780545SJohannes Berg 			rinfo->legacy = 110;
4875ed780545SJohannes Berg 			break;
4876ed780545SJohannes Berg 		case IWL_RATE_6M_PLCP:
4877ed780545SJohannes Berg 			rinfo->legacy = 60;
4878ed780545SJohannes Berg 			break;
4879ed780545SJohannes Berg 		case IWL_RATE_9M_PLCP:
4880ed780545SJohannes Berg 			rinfo->legacy = 90;
4881ed780545SJohannes Berg 			break;
4882ed780545SJohannes Berg 		case IWL_RATE_12M_PLCP:
4883ed780545SJohannes Berg 			rinfo->legacy = 120;
4884ed780545SJohannes Berg 			break;
4885ed780545SJohannes Berg 		case IWL_RATE_18M_PLCP:
4886ed780545SJohannes Berg 			rinfo->legacy = 180;
4887ed780545SJohannes Berg 			break;
4888ed780545SJohannes Berg 		case IWL_RATE_24M_PLCP:
4889ed780545SJohannes Berg 			rinfo->legacy = 240;
4890ed780545SJohannes Berg 			break;
4891ed780545SJohannes Berg 		case IWL_RATE_36M_PLCP:
4892ed780545SJohannes Berg 			rinfo->legacy = 360;
4893ed780545SJohannes Berg 			break;
4894ed780545SJohannes Berg 		case IWL_RATE_48M_PLCP:
4895ed780545SJohannes Berg 			rinfo->legacy = 480;
4896ed780545SJohannes Berg 			break;
4897ed780545SJohannes Berg 		case IWL_RATE_54M_PLCP:
4898ed780545SJohannes Berg 			rinfo->legacy = 540;
4899ed780545SJohannes Berg 			break;
4900ed780545SJohannes Berg 		}
4901ed780545SJohannes Berg 	}
4902ed780545SJohannes Berg }
4903ed780545SJohannes Berg 
4904e705c121SKalle Valo static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
4905e705c121SKalle Valo 				       struct ieee80211_vif *vif,
4906e705c121SKalle Valo 				       struct ieee80211_sta *sta,
4907e705c121SKalle Valo 				       struct station_info *sinfo)
4908e705c121SKalle Valo {
4909e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4910e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4911e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4912e705c121SKalle Valo 
4913988b5968SSara Sharon 	if (mvmsta->avg_energy) {
4914*88ad368aSBen Greear 		sinfo->signal_avg = -(s8)mvmsta->avg_energy;
491522d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
4916988b5968SSara Sharon 	}
4917988b5968SSara Sharon 
4918ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
4919ed780545SJohannes Berg 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
4920ed780545SJohannes Berg 
4921ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
4922ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
4923ed780545SJohannes Berg 	}
4924ed780545SJohannes Berg 
4925e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
4926e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
4927e705c121SKalle Valo 		return;
4928e705c121SKalle Valo 
4929e705c121SKalle Valo 	if (!vif->bss_conf.assoc)
4930e705c121SKalle Valo 		return;
4931e705c121SKalle Valo 
4932e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4933e705c121SKalle Valo 
4934e705c121SKalle Valo 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
4935e705c121SKalle Valo 		goto unlock;
4936e705c121SKalle Valo 
4937e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
4938e705c121SKalle Valo 		goto unlock;
4939e705c121SKalle Valo 
4940e705c121SKalle Valo 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
4941e705c121SKalle Valo 			   mvmvif->beacon_stats.accu_num_beacons;
494222d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
4943e705c121SKalle Valo 	if (mvmvif->beacon_stats.avg_signal) {
4944e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
4945e705c121SKalle Valo 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
494622d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
4947e705c121SKalle Valo 	}
4948e705c121SKalle Valo  unlock:
4949e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4950e705c121SKalle Valo }
4951e705c121SKalle Valo 
4952e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
4953e705c121SKalle Valo 					struct ieee80211_vif *vif,
4954e705c121SKalle Valo 					const struct ieee80211_event *event)
4955e705c121SKalle Valo {
49564c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
4957e705c121SKalle Valo 	do {								\
49584c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
4959e705c121SKalle Valo 			break;						\
49607174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
4961e705c121SKalle Valo 	} while (0)
4962e705c121SKalle Valo 
4963e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
4964e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
4965e705c121SKalle Valo 
49666c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
49676c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
49686c042d75SSara Sharon 	if (!trig)
4969e705c121SKalle Valo 		return;
4970e705c121SKalle Valo 
4971e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
4972e705c121SKalle Valo 
4973e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
4974e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
49754c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
4976e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
4977e705c121SKalle Valo 					    event->u.mlme.reason);
4978e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
49794c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
4980e705c121SKalle Valo 					   "ASSOC TIMEOUT");
4981e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
4982e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
49834c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
4984e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
4985e705c121SKalle Valo 					   event->u.mlme.reason);
4986e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
49874c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
4988e705c121SKalle Valo 					   "AUTH TIMEOUT");
4989e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
49904c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
4991e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
4992e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
49934c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
4994e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
4995e705c121SKalle Valo 	}
4996e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
4997e705c121SKalle Valo }
4998e705c121SKalle Valo 
4999e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5000e705c121SKalle Valo 					  struct ieee80211_vif *vif,
5001e705c121SKalle Valo 					  const struct ieee80211_event *event)
5002e705c121SKalle Valo {
5003e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5004e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
5005e705c121SKalle Valo 
50066c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
50076c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
50086c042d75SSara Sharon 	if (!trig)
5009e705c121SKalle Valo 		return;
5010e705c121SKalle Valo 
5011e705c121SKalle Valo 	ba_trig = (void *)trig->data;
5012e705c121SKalle Valo 
5013e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5014e705c121SKalle Valo 		return;
5015e705c121SKalle Valo 
50167174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
5017e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
5018e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
5019e705c121SKalle Valo 				event->u.ba.ssn);
5020e705c121SKalle Valo }
5021e705c121SKalle Valo 
5022e705c121SKalle Valo static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
5023e705c121SKalle Valo 				       struct ieee80211_vif *vif,
5024e705c121SKalle Valo 				       const struct ieee80211_event *event)
5025e705c121SKalle Valo {
5026e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5027e705c121SKalle Valo 
5028e705c121SKalle Valo 	switch (event->type) {
5029e705c121SKalle Valo 	case MLME_EVENT:
5030e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
5031e705c121SKalle Valo 		break;
5032e705c121SKalle Valo 	case BAR_RX_EVENT:
5033e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
5034e705c121SKalle Valo 		break;
5035e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
5036528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
5037528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
5038e705c121SKalle Valo 		break;
5039e705c121SKalle Valo 	default:
5040e705c121SKalle Valo 		break;
5041e705c121SKalle Valo 	}
5042e705c121SKalle Valo }
5043e705c121SKalle Valo 
5044d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
5045d0ff5d22SSara Sharon 				     struct iwl_mvm_internal_rxq_notif *notif,
5046d0ff5d22SSara Sharon 				     u32 size)
50470636b938SSara Sharon {
50480636b938SSara Sharon 	u32 qmask = BIT(mvm->trans->num_rx_queues) - 1;
50490636b938SSara Sharon 	int ret;
50500636b938SSara Sharon 
50510636b938SSara Sharon 
5052cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
50530636b938SSara Sharon 		return;
50540636b938SSara Sharon 
5055a2113cc4SNaftali Goldstein 	if (notif->sync) {
5056d0ff5d22SSara Sharon 		notif->cookie = mvm->queue_sync_cookie;
5057d0ff5d22SSara Sharon 		atomic_set(&mvm->queue_sync_counter,
5058d0ff5d22SSara Sharon 			   mvm->trans->num_rx_queues);
5059a2113cc4SNaftali Goldstein 	}
5060d0ff5d22SSara Sharon 
50613c514bf8SEmmanuel Grumbach 	ret = iwl_mvm_notify_rx_queue(mvm, qmask, (u8 *)notif,
50623c514bf8SEmmanuel Grumbach 				      size, !notif->sync);
50630636b938SSara Sharon 	if (ret) {
50640636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
50650636b938SSara Sharon 		goto out;
50660636b938SSara Sharon 	}
5067d0ff5d22SSara Sharon 
50686ad04359SJohannes Berg 	if (notif->sync) {
50693c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
50703a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
50716ad04359SJohannes Berg 					 atomic_read(&mvm->queue_sync_counter) == 0 ||
50726ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
50730636b938SSara Sharon 					 HZ);
50746ad04359SJohannes Berg 		WARN_ON_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm));
50756ad04359SJohannes Berg 	}
50760636b938SSara Sharon 
50770636b938SSara Sharon out:
50780636b938SSara Sharon 	atomic_set(&mvm->queue_sync_counter, 0);
5079a2113cc4SNaftali Goldstein 	if (notif->sync)
50800636b938SSara Sharon 		mvm->queue_sync_cookie++;
50810636b938SSara Sharon }
50820636b938SSara Sharon 
50830636b938SSara Sharon static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
50840636b938SSara Sharon {
50850636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5086d0ff5d22SSara Sharon 	struct iwl_mvm_internal_rxq_notif data = {
5087d0ff5d22SSara Sharon 		.type = IWL_MVM_RXQ_EMPTY,
5088d0ff5d22SSara Sharon 		.sync = 1,
5089d0ff5d22SSara Sharon 	};
50900636b938SSara Sharon 
50910636b938SSara Sharon 	mutex_lock(&mvm->mutex);
5092d0ff5d22SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, &data, sizeof(data));
50930636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
50940636b938SSara Sharon }
50950636b938SSara Sharon 
5096b73f9a4aSJohannes Berg static int
5097b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
5098b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
5099b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
5100b73f9a4aSJohannes Berg {
5101b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5102b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5103b73f9a4aSJohannes Berg 
5104b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
5105b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
5106b73f9a4aSJohannes Berg 		return -EINVAL;
5107b73f9a4aSJohannes Berg 
5108b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
5109b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
5110b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
5111b73f9a4aSJohannes Berg 
5112b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
5113b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
5114b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
5115b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
5116b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
5117b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
5118b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
5119b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
5120b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
5121b73f9a4aSJohannes Berg 
5122b73f9a4aSJohannes Berg 	return 0;
5123b73f9a4aSJohannes Berg }
5124b73f9a4aSJohannes Berg 
5125fc36ffdaSJohannes Berg static int iwl_mvm_start_pmsr(struct ieee80211_hw *hw,
5126fc36ffdaSJohannes Berg 			      struct ieee80211_vif *vif,
5127fc36ffdaSJohannes Berg 			      struct cfg80211_pmsr_request *request)
5128fc36ffdaSJohannes Berg {
5129fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5130fc36ffdaSJohannes Berg 	int ret;
5131fc36ffdaSJohannes Berg 
5132fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5133fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
5134fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5135fc36ffdaSJohannes Berg 
5136fc36ffdaSJohannes Berg 	return ret;
5137fc36ffdaSJohannes Berg }
5138fc36ffdaSJohannes Berg 
5139fc36ffdaSJohannes Berg static void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw,
5140fc36ffdaSJohannes Berg 			       struct ieee80211_vif *vif,
5141fc36ffdaSJohannes Berg 			       struct cfg80211_pmsr_request *request)
5142fc36ffdaSJohannes Berg {
5143fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5144fc36ffdaSJohannes Berg 
5145fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5146fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
5147fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5148fc36ffdaSJohannes Berg }
5149fc36ffdaSJohannes Berg 
5150438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
5151438af969SSara Sharon {
5152438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
5153438af969SSara Sharon 
5154438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
5155438af969SSara Sharon 		return false;
5156438af969SSara Sharon 
5157438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
5158438af969SSara Sharon }
5159438af969SSara Sharon 
5160438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
5161438af969SSara Sharon 				      struct sk_buff *head,
5162438af969SSara Sharon 				      struct sk_buff *skb)
5163438af969SSara Sharon {
5164438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5165438af969SSara Sharon 
5166438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
5167438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
5168438af969SSara Sharon 		return false;
5169438af969SSara Sharon 
5170438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
5171438af969SSara Sharon 		return true;
5172438af969SSara Sharon 
5173438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5174438af969SSara Sharon }
5175438af969SSara Sharon 
5176e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
5177e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
5178cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
5179e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
5180e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
5181e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
5182e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
5183e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
5184e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
5185e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
5186e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
5187e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
5188e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
5189e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
5190e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
5191e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
5192e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
5193e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
5194e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
5195e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
5196e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
5197e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
5198e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
5199e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
5200e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
5201e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
5202e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
5203e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
5204e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
5205e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
5206e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
5207e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
5208e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
5209e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
5210e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
5211e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
5212e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
5213e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5214e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
5215e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
5216e705c121SKalle Valo 
5217e705c121SKalle Valo 	.start_ap = iwl_mvm_start_ap_ibss,
5218e705c121SKalle Valo 	.stop_ap = iwl_mvm_stop_ap_ibss,
5219e705c121SKalle Valo 	.join_ibss = iwl_mvm_start_ap_ibss,
5220e705c121SKalle Valo 	.leave_ibss = iwl_mvm_stop_ap_ibss,
5221e705c121SKalle Valo 
52222f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
52232f0282dbSJohannes Berg 
5224e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
5225e705c121SKalle Valo 
5226e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
5227e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
5228e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
522979221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
5230c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
5231e705c121SKalle Valo 
5232e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5233e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5234e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5235e705c121SKalle Valo 
5236e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
5237e705c121SKalle Valo 
52380636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
52390636b938SSara Sharon 
5240e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5241e705c121SKalle Valo 
5242e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
5243e705c121SKalle Valo 	/* look at d3.c */
5244e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
5245e705c121SKalle Valo 	.resume = iwl_mvm_resume,
5246e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
5247e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
5248e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
5249e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
5250e705c121SKalle Valo #endif
5251e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
5252e705c121SKalle Valo #endif
5253e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
5254e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
5255b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
5256fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
5257fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
5258b73f9a4aSJohannes Berg 
5259438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
5260177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
5261177a11cfSGregory Greenman 	.sta_add_debugfs = iwl_mvm_sta_add_debugfs,
5262177a11cfSGregory Greenman #endif
5263e705c121SKalle Valo };
5264