xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision a5de7de7e78eddd3ae16aec01c5601ae36c1c2e8)
18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
3091296d3SMiri Korenblit  * Copyright (C) 2012-2014, 2018-2022 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
7e705c121SKalle Valo #include <linux/kernel.h>
8e705c121SKalle Valo #include <linux/slab.h>
9e705c121SKalle Valo #include <linux/skbuff.h>
10e705c121SKalle Valo #include <linux/netdevice.h>
11e705c121SKalle Valo #include <linux/etherdevice.h>
12e705c121SKalle Valo #include <linux/ip.h>
13e705c121SKalle Valo #include <linux/if_arp.h>
142f89a5d7SGolan Ben-Ami #include <linux/time.h>
15e705c121SKalle Valo #include <net/mac80211.h>
16e705c121SKalle Valo #include <net/ieee80211_radiotap.h>
17e705c121SKalle Valo #include <net/tcp.h>
18e705c121SKalle Valo 
195283dd67SMordechay Goodstein #include "iwl-drv.h"
20e705c121SKalle Valo #include "iwl-op-mode.h"
21e705c121SKalle Valo #include "iwl-io.h"
22e705c121SKalle Valo #include "mvm.h"
23e705c121SKalle Valo #include "sta.h"
24e705c121SKalle Valo #include "time-event.h"
25e705c121SKalle Valo #include "iwl-eeprom-parse.h"
26e705c121SKalle Valo #include "iwl-phy-db.h"
27e705c121SKalle Valo #include "testmode.h"
28d962f9b1SJohannes Berg #include "fw/error-dump.h"
29e705c121SKalle Valo #include "iwl-prph.h"
30e705c121SKalle Valo #include "iwl-nvm-parse.h"
31e705c121SKalle Valo 
32e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
33e705c121SKalle Valo 	{
34e705c121SKalle Valo 		.max = 1,
35e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
36e705c121SKalle Valo 	},
37e705c121SKalle Valo 	{
38e705c121SKalle Valo 		.max = 1,
39e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
40e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
41e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
42e705c121SKalle Valo 	},
43e705c121SKalle Valo 	{
44e705c121SKalle Valo 		.max = 1,
45e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
46e705c121SKalle Valo 	},
47e705c121SKalle Valo };
48e705c121SKalle Valo 
49e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
50e705c121SKalle Valo 	{
51e705c121SKalle Valo 		.num_different_channels = 2,
52e705c121SKalle Valo 		.max_interfaces = 3,
53e705c121SKalle Valo 		.limits = iwl_mvm_limits,
54e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
55e705c121SKalle Valo 	},
56e705c121SKalle Valo };
57e705c121SKalle Valo 
58fc36ffdaSJohannes Berg static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
59fc36ffdaSJohannes Berg 	.max_peers = IWL_MVM_TOF_MAX_APS,
60fc36ffdaSJohannes Berg 	.report_ap_tsf = 1,
61fc36ffdaSJohannes Berg 	.randomize_mac_addr = 1,
62fc36ffdaSJohannes Berg 
63fc36ffdaSJohannes Berg 	.ftm = {
64fc36ffdaSJohannes Berg 		.supported = 1,
65fc36ffdaSJohannes Berg 		.asap = 1,
66fc36ffdaSJohannes Berg 		.non_asap = 1,
67fc36ffdaSJohannes Berg 		.request_lci = 1,
68fc36ffdaSJohannes Berg 		.request_civicloc = 1,
696815e3d0SAvraham Stern 		.trigger_based = 1,
706815e3d0SAvraham Stern 		.non_trigger_based = 1,
71fc36ffdaSJohannes Berg 		.max_bursts_exponent = -1, /* all supported */
72fc36ffdaSJohannes Berg 		.max_ftms_per_burst = 0, /* no limits */
73fc36ffdaSJohannes Berg 		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
74fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_20) |
75fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_40) |
768a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_80) |
778a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_160),
78fc36ffdaSJohannes Berg 		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
79fc36ffdaSJohannes Berg 			     BIT(NL80211_PREAMBLE_HT) |
806815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_VHT) |
816815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_HE),
82fc36ffdaSJohannes Berg 	},
83fc36ffdaSJohannes Berg };
84fc36ffdaSJohannes Berg 
856569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
86c56e00a3SJohannes Berg 				 enum set_key_cmd cmd,
87c56e00a3SJohannes Berg 				 struct ieee80211_vif *vif,
88c56e00a3SJohannes Berg 				 struct ieee80211_sta *sta,
89c56e00a3SJohannes Berg 				 struct ieee80211_key_conf *key);
90c56e00a3SJohannes Berg 
91e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
92e705c121SKalle Valo {
93e705c121SKalle Valo 	int i;
94e705c121SKalle Valo 
95e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
96e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
97e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
98e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
99e705c121SKalle Valo 	}
100e705c121SKalle Valo }
101e705c121SKalle Valo 
102e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
103e705c121SKalle Valo 						  const char *alpha2,
104e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
105e705c121SKalle Valo 						  bool *changed)
106e705c121SKalle Valo {
107e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
108e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
109e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
110e705c121SKalle Valo 	struct iwl_mcc_update_resp *resp;
111e27c506aSGil Adam 	u8 resp_ver;
112e705c121SKalle Valo 
113e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
114e705c121SKalle Valo 
115e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
116e705c121SKalle Valo 
117e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
118e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
119e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
120e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
1216d19a5ebSEmmanuel Grumbach 		resp = NULL;
122e705c121SKalle Valo 		goto out;
123e705c121SKalle Valo 	}
124e705c121SKalle Valo 
12582715ac7SEmmanuel Grumbach 	if (changed) {
12682715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
12782715ac7SEmmanuel Grumbach 
12882715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
12982715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
13082715ac7SEmmanuel Grumbach 	}
131e27c506aSGil Adam 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
132e27c506aSGil Adam 					   MCC_UPDATE_CMD, 0);
133e27c506aSGil Adam 	IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
134e705c121SKalle Valo 
135e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
136e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
137e705c121SKalle Valo 				      resp->channels,
13877e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
1392763bba6SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info),
140e27c506aSGil Adam 				      __le16_to_cpu(resp->cap), resp_ver);
141e705c121SKalle Valo 	/* Store the return source id */
142e705c121SKalle Valo 	src_id = resp->source_id;
143e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
144e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
145e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
146e705c121SKalle Valo 		goto out;
147e705c121SKalle Valo 	}
148e705c121SKalle Valo 
149e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
150e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
151e705c121SKalle Valo 	mvm->lar_regdom_set = true;
152e705c121SKalle Valo 	mvm->mcc_src = src_id;
153e705c121SKalle Valo 
1546d19a5ebSEmmanuel Grumbach 	iwl_mei_set_country_code(__le16_to_cpu(resp->mcc));
1556d19a5ebSEmmanuel Grumbach 
156e705c121SKalle Valo out:
1576d19a5ebSEmmanuel Grumbach 	kfree(resp);
158e705c121SKalle Valo 	return regd;
159e705c121SKalle Valo }
160e705c121SKalle Valo 
161e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
162e705c121SKalle Valo {
163e705c121SKalle Valo 	bool changed;
164e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
165e705c121SKalle Valo 
166e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
167e705c121SKalle Valo 		return;
168e705c121SKalle Valo 
169e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
170e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
171e705c121SKalle Valo 		/* only update the regulatory core if changed */
172e705c121SKalle Valo 		if (changed)
173e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
174e705c121SKalle Valo 
175e705c121SKalle Valo 		kfree(regd);
176e705c121SKalle Valo 	}
177e705c121SKalle Valo }
178e705c121SKalle Valo 
179e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
180e705c121SKalle Valo 							  bool *changed)
181e705c121SKalle Valo {
182e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
183e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
184e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
185e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
186e705c121SKalle Valo }
187e705c121SKalle Valo 
188e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
189e705c121SKalle Valo {
190e705c121SKalle Valo 	enum iwl_mcc_source used_src;
191e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
192e705c121SKalle Valo 	int ret;
193e705c121SKalle Valo 	bool changed;
194e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
195a05829a7SJohannes Berg 			wiphy_dereference(mvm->hw->wiphy, mvm->hw->wiphy->regd);
196e705c121SKalle Valo 
197e705c121SKalle Valo 	if (!r)
198e705c121SKalle Valo 		return -ENOENT;
199e705c121SKalle Valo 
200e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
201e705c121SKalle Valo 	used_src = mvm->mcc_src;
202e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
203e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
204e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
205e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
206e705c121SKalle Valo 			kfree(regd);
207e705c121SKalle Valo 	}
208e705c121SKalle Valo 
209e705c121SKalle Valo 	/* Now set our last stored MCC and source */
210e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
211e705c121SKalle Valo 				     &changed);
212e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
213e705c121SKalle Valo 		return -EIO;
214e705c121SKalle Valo 
215e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
216e705c121SKalle Valo 	if (changed)
217a05829a7SJohannes Berg 		ret = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
218e705c121SKalle Valo 	else
219e705c121SKalle Valo 		ret = 0;
220e705c121SKalle Valo 
221e705c121SKalle Valo 	kfree(regd);
222e705c121SKalle Valo 	return ret;
223e705c121SKalle Valo }
224e705c121SKalle Valo 
2258e33f046SKrishnanand Prabhu /* Each capability added here should also be add to tm_if_types_ext_capa_sta */
2267f2ea521SYueHaibing static const u8 he_if_types_ext_capa_sta[] = {
2277360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
228918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
2297360f99eSEmmanuel Grumbach 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
2307360f99eSEmmanuel Grumbach };
2317360f99eSEmmanuel Grumbach 
2328e33f046SKrishnanand Prabhu static const u8 tm_if_types_ext_capa_sta[] = {
2338e33f046SKrishnanand Prabhu 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
2348e33f046SKrishnanand Prabhu 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT |
2358e33f046SKrishnanand Prabhu 	       WLAN_EXT_CAPA3_TIMING_MEASUREMENT_SUPPORT,
2368e33f046SKrishnanand Prabhu 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
2378e33f046SKrishnanand Prabhu 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
2388e33f046SKrishnanand Prabhu };
2398e33f046SKrishnanand Prabhu 
2408e33f046SKrishnanand Prabhu /* Additional interface types for which extended capabilities are
2418e33f046SKrishnanand Prabhu  * specified separately
2428e33f046SKrishnanand Prabhu  */
2438e33f046SKrishnanand Prabhu static const struct wiphy_iftype_ext_capab add_iftypes_ext_capa[] = {
2447360f99eSEmmanuel Grumbach 	{
2457360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
2467360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
2477360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
2487360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
2497360f99eSEmmanuel Grumbach 	},
2508e33f046SKrishnanand Prabhu 	{
2518e33f046SKrishnanand Prabhu 		.iftype = NL80211_IFTYPE_STATION,
2528e33f046SKrishnanand Prabhu 		.extended_capabilities = tm_if_types_ext_capa_sta,
2538e33f046SKrishnanand Prabhu 		.extended_capabilities_mask = tm_if_types_ext_capa_sta,
2548e33f046SKrishnanand Prabhu 		.extended_capabilities_len = sizeof(tm_if_types_ext_capa_sta),
2558e33f046SKrishnanand Prabhu 		/* relevant only if EHT is supported */
2568e33f046SKrishnanand Prabhu 		.eml_capabilities = IEEE80211_EML_CAP_EMLSR_SUPP,
2578e33f046SKrishnanand Prabhu 	},
2587360f99eSEmmanuel Grumbach };
2597360f99eSEmmanuel Grumbach 
260e8503aecSBen Greear static int
261e8503aecSBen Greear iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
262e8503aecSBen Greear {
263e8503aecSBen Greear 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
264e8503aecSBen Greear 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
265e8503aecSBen Greear 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
266e8503aecSBen Greear 	return 0;
267e8503aecSBen Greear }
268e8503aecSBen Greear 
269e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
270e705c121SKalle Valo {
271e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
272e705c121SKalle Valo 	int num_mac, ret, i;
273e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
274e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
275e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
276e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
277e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
278e705c121SKalle Valo 	};
2793f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
2803f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
2813f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2823f37c229SIdo Yariv #endif
283*a5de7de7SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
284*a5de7de7SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
285e705c121SKalle Valo 
286e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
287e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
288e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
289e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
290e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
291e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
292e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
293e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
294e705c121SKalle Valo 	ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
295e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
296e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
297e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
298e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
299909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
30030433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
301d270e7b8SIlan Peer 	ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
302520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
303cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
304cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
305cfb21b11SJohannes Berg 	/*
306cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
307cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
308cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
309cfb21b11SJohannes Berg 	 * assert EDC.
310cfb21b11SJohannes Berg 	 *
311cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
312cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
313cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
314cfb21b11SJohannes Berg 	 * devices.
315cfb21b11SJohannes Berg 	 */
3167d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
317438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
318438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
319ecaf71deSGregory Greenman 
3204243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
321ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
322ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
323ecaf71deSGregory Greenman 	}
324ecaf71deSGregory Greenman 
325b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
326b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
327960f864bSJohannes Berg 
328960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
329960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
33065e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
331960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
332960f864bSJohannes Berg 		/*
333960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
334960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
335960f864bSJohannes Berg 		 * for station powersave
336960f864bSJohannes Berg 		 */
337960f864bSJohannes Berg 		return -EINVAL;
338960f864bSJohannes Berg 	}
339e705c121SKalle Valo 
34080938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
34180938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
34280938abcSJohannes Berg 
3432d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
3442d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
3452d7cf549SJohannes Berg 
346366fc672SJohannes Berg 	hw->queues = IEEE80211_NUM_ACS;
347e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
348e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
349e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
350e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
351e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
352371a17edSJohannes Berg 
353371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
354371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
355371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
356371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
357371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
358371a17edSJohannes Berg 
3594243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
3609f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
3619f66a397SGregory Greenman 
362e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
363e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
364438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
365e705c121SKalle Valo 
3668e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
367e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
368e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
369e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
370e705c121SKalle Valo 
3712a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
3722a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
3732a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
3742a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
3752a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
3762a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
3772a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
3782a53d166SAyala Beker 	}
3792a53d166SAyala Beker 
380f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
381f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
382f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
383f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
384f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
385f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
386f4bfdc5eSEmmanuel Grumbach 
387e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
388f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
389e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
3908e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
3918e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
3928e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
3938e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
3948e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
3958e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
3968e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
3978e160ab8SAyala Beker 	}
398e705c121SKalle Valo 
399ef2e7a51SIlan Peer 	wiphy_ext_feature_set(hw->wiphy,
400ef2e7a51SIlan Peer 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
401ef2e7a51SIlan Peer 
402b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
403fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
404b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
405b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
406fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
407fc36ffdaSJohannes Berg 	}
408b73f9a4aSJohannes Berg 
409*a5de7de7SJohannes Berg 	if (sec_key_ver &&
410*a5de7de7SJohannes Berg 	    fw_has_capa(&mvm->fw->ucode_capa,
411*a5de7de7SJohannes Berg 			IWL_UCODE_TLV_CAPA_BIGTK_TX_SUPPORT))
412*a5de7de7SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
413*a5de7de7SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION);
414*a5de7de7SJohannes Berg 	else if (fw_has_capa(&mvm->fw->ucode_capa,
415b1fdc250SJohannes Berg 			     IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
416b1fdc250SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
417b1fdc250SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
418b1fdc250SJohannes Berg 
419e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
420e705c121SKalle Valo 	hw->wiphy->features |=
421e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
422e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
423e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
424e705c121SKalle Valo 
425e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
426e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
427e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
428cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
429e705c121SKalle Valo 
430e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
431e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
432e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
433e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
434e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
435e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
436e705c121SKalle Valo 
437e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
438e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
43933e3fd99SAlexander Wetzel 
44033e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
44133e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
44233e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
44333e3fd99SAlexander Wetzel 	 */
44433e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
44533e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
44633e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
447e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
448e47df5bdSJohannes Berg 
449e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
450e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
451e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
452e705c121SKalle Valo 	else
453e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
454e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
455e705c121SKalle Valo 
456e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
457e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
458eae94cf8SLuca Coelho 	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
459e705c121SKalle Valo 
460e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
461e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
462e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
463e705c121SKalle Valo 
464e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
465e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
466e705c121SKalle Valo 
467e705c121SKalle Valo 	/* Extract MAC address */
468e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
469e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
470e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
471e705c121SKalle Valo 
472e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
473e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
474e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
475e705c121SKalle Valo 
476e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
477e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
478e705c121SKalle Valo 		       ETH_ALEN);
479e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
480e705c121SKalle Valo 		hw->wiphy->n_addresses++;
481e705c121SKalle Valo 	}
482e705c121SKalle Valo 
483e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
484e705c121SKalle Valo 
485e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
486e705c121SKalle Valo 
487e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
488e705c121SKalle Valo 
489e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
490e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
491e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
492e705c121SKalle Valo 
493e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
494e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
495e705c121SKalle Valo 	else
496e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
497e705c121SKalle Valo 
49857fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
49957fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
50057fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
50157fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
50257fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
50357fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
504e705c121SKalle Valo 
505e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
506e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
507e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
508e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
50957fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
510e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
511e705c121SKalle Valo 	}
512eae94cf8SLuca Coelho 	if (fw_has_capa(&mvm->fw->ucode_capa,
513eae94cf8SLuca Coelho 			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
514eae94cf8SLuca Coelho 	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
515eae94cf8SLuca Coelho 		hw->wiphy->bands[NL80211_BAND_6GHZ] =
516eae94cf8SLuca Coelho 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
517e705c121SKalle Valo 
518e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
519e705c121SKalle Valo 
520e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
521e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
522e705c121SKalle Valo 	else
523e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
524e705c121SKalle Valo 
525ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
526e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
5275d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
528e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
529e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
530e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
531e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
532e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
533e705c121SKalle Valo 
534e705c121SKalle Valo 	/*
535e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
536e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
537e705c121SKalle Valo 	 */
538e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
539e705c121SKalle Valo 
540e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
541e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
542e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
543a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
544e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
545e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
546e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
547e705c121SKalle Valo 
548e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
549e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
550e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
551e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
552e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
553e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
554e705c121SKalle Valo 
555e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
556e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
557e705c121SKalle Valo 		hw->wiphy->features |=
558e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
559e705c121SKalle Valo 
560e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
561e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
562e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
563e705c121SKalle Valo 
564971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
5652a42aea7SNathan Errera 				  IWL_FW_CMD_VER_UNKNOWN) == 3)
5662a42aea7SNathan Errera 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
5672a42aea7SNathan Errera 
568aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
569aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
570aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
571aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
572aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
573aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
574aacf8f18SAvrahams Stern 	}
575aacf8f18SAvrahams Stern 
5768f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
577971cbe50SJohannes Berg 		u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
578773a042fSAvraham Stern 
5798f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
5808f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
5818f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
5828f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
5838f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
5848f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
585773a042fSAvraham Stern 
586773a042fSAvraham Stern 		/* Old firmware also supports probe deferral and suppression */
587773a042fSAvraham Stern 		if (scan_ver < 15)
588773a042fSAvraham Stern 			wiphy_ext_feature_set(hw->wiphy,
589773a042fSAvraham Stern 					      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
5908f691af9SZamir, Roee 	}
5918f691af9SZamir, Roee 
5928e33f046SKrishnanand Prabhu 	hw->wiphy->iftype_ext_capab = NULL;
5938e33f046SKrishnanand Prabhu 	hw->wiphy->num_iftype_ext_capab = 0;
5948e33f046SKrishnanand Prabhu 
5957360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
5967360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
5978e33f046SKrishnanand Prabhu 		hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa;
5987360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
5998e33f046SKrishnanand Prabhu 			ARRAY_SIZE(add_iftypes_ext_capa) - 1;
600918cbf39SSara Sharon 
601918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
602918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
6037360f99eSEmmanuel Grumbach 	}
6047360f99eSEmmanuel Grumbach 
6058e33f046SKrishnanand Prabhu 	if (iwl_fw_lookup_cmd_ver(mvm->fw,
6068e33f046SKrishnanand Prabhu 				  WIDE_ID(DATA_PATH_GROUP,
6078e33f046SKrishnanand Prabhu 					  WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
6088e33f046SKrishnanand Prabhu 				  IWL_FW_CMD_VER_UNKNOWN) >= 1) {
6098e33f046SKrishnanand Prabhu 		IWL_DEBUG_INFO(mvm->trans, "Timing measurement supported\n");
6108e33f046SKrishnanand Prabhu 
6118e33f046SKrishnanand Prabhu 		if (!hw->wiphy->iftype_ext_capab) {
6128e33f046SKrishnanand Prabhu 			hw->wiphy->num_iftype_ext_capab = 1;
6138e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa +
6148e33f046SKrishnanand Prabhu 				ARRAY_SIZE(add_iftypes_ext_capa) - 1;
6158e33f046SKrishnanand Prabhu 		} else {
6168e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa + 1;
6178e33f046SKrishnanand Prabhu 		}
6188e33f046SKrishnanand Prabhu 	}
6198e33f046SKrishnanand Prabhu 
620e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
621e705c121SKalle Valo 
622e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6233f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
624e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
625e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
626e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
627e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
628e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
629e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
630e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
631e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
632e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
633e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
634e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
635e705c121SKalle Valo 
636e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
637e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
638e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
6395d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
6405d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
641e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
642e705c121SKalle Valo 	}
643e705c121SKalle Valo #endif
644e705c121SKalle Valo 
645e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
646e705c121SKalle Valo 	if (ret)
647e705c121SKalle Valo 		return ret;
648e705c121SKalle Valo 
649e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
650e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
651e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
652e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
653e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
654e705c121SKalle Valo 	}
655e705c121SKalle Valo 
656e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
657e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
658e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
659e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
660e705c121SKalle Valo 	}
661e705c121SKalle Valo 
662e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
66359fa61f3SEmmanuel Grumbach 	if (!iwl_mvm_is_csum_supported(mvm))
6646772aab7SJohannes Berg 		hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
66541837ca9SEmmanuel Grumbach 
66691b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
66791b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
66891b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
66991b08c2dSAviya Erenfeld 
6709c11d8a9SShaul Triebitz 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
6719c11d8a9SShaul Triebitz 		wiphy_ext_feature_set(hw->wiphy,
6729c11d8a9SShaul Triebitz 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
6739c11d8a9SShaul Triebitz 
674bfcfdb59SEmmanuel Grumbach 	iwl_mvm_vendor_cmds_register(mvm);
675bfcfdb59SEmmanuel Grumbach 
676e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
677e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
678e8503aecSBen Greear 
679de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
680de645e89SJohannes Berg 	if (ret) {
681de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
682e705c121SKalle Valo 	}
683e705c121SKalle Valo 
684e705c121SKalle Valo 	return ret;
685e705c121SKalle Valo }
686e705c121SKalle Valo 
687df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
688df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
689df2378abSJohannes Berg {
690df2378abSJohannes Berg 	if (likely(sta)) {
691df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
692df2378abSJohannes Berg 			return;
693df2378abSJohannes Berg 	} else {
694df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
695df2378abSJohannes Berg 			return;
696df2378abSJohannes Berg 	}
697df2378abSJohannes Berg 
698df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
699df2378abSJohannes Berg }
700df2378abSJohannes Berg 
701e705c121SKalle Valo static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
702e705c121SKalle Valo 			   struct ieee80211_tx_control *control,
703e705c121SKalle Valo 			   struct sk_buff *skb)
704e705c121SKalle Valo {
705e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
706e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
707e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
708e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
709cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
710cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
711e705c121SKalle Valo 
712e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
713e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
714e705c121SKalle Valo 		goto drop;
715e705c121SKalle Valo 	}
716e705c121SKalle Valo 
717cfbc6c4cSSara Sharon 	if (offchannel &&
718e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
719e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
720e705c121SKalle Valo 		goto drop;
721e705c121SKalle Valo 
72210516783SJohannes Berg 	/*
72310516783SJohannes Berg 	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
72410516783SJohannes Berg 	 * so we treat the others as broadcast
72510516783SJohannes Berg 	 */
72610516783SJohannes Berg 	if (ieee80211_is_mgmt(hdr->frame_control))
727e705c121SKalle Valo 		sta = NULL;
728e705c121SKalle Valo 
729dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
730cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
731cfbc6c4cSSara Sharon 	    !offchannel) {
732dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
733dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
734dc1aca22SAndrei Otcheretianski 		u8 ap_sta_id = READ_ONCE(mvmvif->ap_sta_id);
735dc1aca22SAndrei Otcheretianski 
736be9ae34eSNathan Errera 		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
737dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
738dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
739dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
740dc1aca22SAndrei Otcheretianski 				goto drop;
741dc1aca22SAndrei Otcheretianski 		}
742dc1aca22SAndrei Otcheretianski 	}
743dc1aca22SAndrei Otcheretianski 
744df2378abSJohannes Berg 	iwl_mvm_tx_skb(mvm, skb, sta);
745e705c121SKalle Valo 	return;
746e705c121SKalle Valo  drop:
747e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
748e705c121SKalle Valo }
749e705c121SKalle Valo 
750cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
751e705c121SKalle Valo {
752cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
753cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
754cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
755cfbc6c4cSSara Sharon 
756fba8248eSSara Sharon 	/*
757fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
758fba8248eSSara Sharon 	 * taker all the work.
759fba8248eSSara Sharon 	 *
760fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
761fba8248eSSara Sharon 	 *
762fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
763fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
764fba8248eSSara Sharon 	 *
765fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
766fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
767fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
768fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
769fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
770fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
771fba8248eSSara Sharon 	 *
772fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
773fba8248eSSara Sharon 	 * check the queue, so do nothing.
774fba8248eSSara Sharon 	 */
775fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
776fba8248eSSara Sharon 		return;
777cfbc6c4cSSara Sharon 
778cfbc6c4cSSara Sharon 	rcu_read_lock();
779fba8248eSSara Sharon 	do {
780cfbc6c4cSSara Sharon 		while (likely(!mvmtxq->stopped &&
78100520b7aSEmmanuel Grumbach 			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
782cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
783cfbc6c4cSSara Sharon 
784f50d693bSSara Sharon 			if (!skb) {
785f50d693bSSara Sharon 				if (txq->sta)
786f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
787f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
788f50d693bSSara Sharon 						     txq->sta->addr,
789f50d693bSSara Sharon 						     txq->tid);
790cfbc6c4cSSara Sharon 				break;
791f50d693bSSara Sharon 			}
792cfbc6c4cSSara Sharon 
793cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
794cfbc6c4cSSara Sharon 		}
795fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
796cfbc6c4cSSara Sharon 	rcu_read_unlock();
797e705c121SKalle Valo }
798e705c121SKalle Valo 
799cfbc6c4cSSara Sharon static void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
800cfbc6c4cSSara Sharon 				      struct ieee80211_txq *txq)
801e705c121SKalle Valo {
802cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
803cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
804e705c121SKalle Valo 
805cfbc6c4cSSara Sharon 	/*
806cfbc6c4cSSara Sharon 	 * Please note that racing is handled very carefully here:
807cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id is updated during allocation, and mvmtxq->list is
808cfbc6c4cSSara Sharon 	 * deleted afterwards.
809cfbc6c4cSSara Sharon 	 * This means that if:
810cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && list_empty(&mvmtxq->list):
811cfbc6c4cSSara Sharon 	 *	queue is allocated and we can TX.
812cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && !list_empty(&mvmtxq->list):
813cfbc6c4cSSara Sharon 	 *	a race, should defer the frame.
814cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && list_empty(&mvmtxq->list):
815cfbc6c4cSSara Sharon 	 *	need to allocate the queue and defer the frame.
816cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && !list_empty(&mvmtxq->list):
817cfbc6c4cSSara Sharon 	 *	queue is already scheduled for allocation, no need to allocate,
818cfbc6c4cSSara Sharon 	 *	should defer the frame.
819cfbc6c4cSSara Sharon 	 */
820cfbc6c4cSSara Sharon 
821cfbc6c4cSSara Sharon 	/* If the queue is allocated TX and return. */
822cfbc6c4cSSara Sharon 	if (!txq->sta || mvmtxq->txq_id != IWL_MVM_INVALID_QUEUE) {
823cfbc6c4cSSara Sharon 		/*
824cfbc6c4cSSara Sharon 		 * Check that list is empty to avoid a race where txq_id is
825cfbc6c4cSSara Sharon 		 * already updated, but the queue allocation work wasn't
826cfbc6c4cSSara Sharon 		 * finished
827cfbc6c4cSSara Sharon 		 */
828cfbc6c4cSSara Sharon 		if (unlikely(txq->sta && !list_empty(&mvmtxq->list)))
829cfbc6c4cSSara Sharon 			return;
830cfbc6c4cSSara Sharon 
831cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
832cfbc6c4cSSara Sharon 		return;
833cfbc6c4cSSara Sharon 	}
834cfbc6c4cSSara Sharon 
835cfbc6c4cSSara Sharon 	/* The list is being deleted only after the queue is fully allocated. */
836cfbc6c4cSSara Sharon 	if (!list_empty(&mvmtxq->list))
837cfbc6c4cSSara Sharon 		return;
838cfbc6c4cSSara Sharon 
839cfbc6c4cSSara Sharon 	list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
840cfbc6c4cSSara Sharon 	schedule_work(&mvm->add_stream_wk);
841e705c121SKalle Valo }
842e705c121SKalle Valo 
843e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
844e705c121SKalle Valo 	do {								\
845e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
846e705c121SKalle Valo 			break;						\
8477174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
848e705c121SKalle Valo 	} while (0)
849e705c121SKalle Valo 
850e705c121SKalle Valo static void
851e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
852e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
853e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
854e705c121SKalle Valo {
855e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
856e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
857e705c121SKalle Valo 
8586c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
8596c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
8606c042d75SSara Sharon 	if (!trig)
861e705c121SKalle Valo 		return;
862e705c121SKalle Valo 
863e705c121SKalle Valo 	ba_trig = (void *)trig->data;
864e705c121SKalle Valo 
865e705c121SKalle Valo 	switch (action) {
866e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
867e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
868e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
869e705c121SKalle Valo 
870e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
871e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
872e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
873e705c121SKalle Valo 		break;
874e705c121SKalle Valo 		}
875e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
876e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
877e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
878e705c121SKalle Valo 				 sta->addr, tid);
879e705c121SKalle Valo 		break;
880e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
881e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
882e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
883e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
884e705c121SKalle Valo 		break;
885e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
886e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
887e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
888e705c121SKalle Valo 				 sta->addr, tid);
889e705c121SKalle Valo 		break;
890e705c121SKalle Valo 	default:
891e705c121SKalle Valo 		break;
892e705c121SKalle Valo 	}
893e705c121SKalle Valo }
894e705c121SKalle Valo 
895e705c121SKalle Valo static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
896e705c121SKalle Valo 				    struct ieee80211_vif *vif,
89750ea05efSSara Sharon 				    struct ieee80211_ampdu_params *params)
898e705c121SKalle Valo {
899e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
900e705c121SKalle Valo 	int ret;
90150ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
90250ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
90350ea05efSSara Sharon 	u16 tid = params->tid;
90450ea05efSSara Sharon 	u16 *ssn = &params->ssn;
905514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
906bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
90710b2b201SSara Sharon 	u16 timeout = params->timeout;
908e705c121SKalle Valo 
909e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
910e705c121SKalle Valo 		     sta->addr, tid, action);
911e705c121SKalle Valo 
912e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
913e705c121SKalle Valo 		return -EACCES;
914e705c121SKalle Valo 
915e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
916e705c121SKalle Valo 
917e705c121SKalle Valo 	switch (action) {
918e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
919b0ffe455SJohannes Berg 		if (iwl_mvm_vif_from_mac80211(vif)->ap_sta_id ==
920b0ffe455SJohannes Berg 				iwl_mvm_sta_from_mac80211(sta)->sta_id) {
921b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
922b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
923b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
924b0ffe455SJohannes Berg 
925b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
926b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
927b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
928b0ffe455SJohannes Berg 		}
929e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
930e705c121SKalle Valo 			ret = -EINVAL;
931e705c121SKalle Valo 			break;
932e705c121SKalle Valo 		}
93310b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
93410b2b201SSara Sharon 					 timeout);
935e705c121SKalle Valo 		break;
936e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
93710b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
93810b2b201SSara Sharon 					 timeout);
939e705c121SKalle Valo 		break;
940e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
941e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
942e705c121SKalle Valo 			ret = -EINVAL;
943e705c121SKalle Valo 			break;
944e705c121SKalle Valo 		}
945e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
946e705c121SKalle Valo 		break;
947e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
948e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
949e705c121SKalle Valo 		break;
950e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
951e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
952e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
953e705c121SKalle Valo 		break;
954e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
955bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
956bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
957e705c121SKalle Valo 		break;
958e705c121SKalle Valo 	default:
959e705c121SKalle Valo 		WARN_ON_ONCE(1);
960e705c121SKalle Valo 		ret = -EINVAL;
961e705c121SKalle Valo 		break;
962e705c121SKalle Valo 	}
963e705c121SKalle Valo 
964e705c121SKalle Valo 	if (!ret) {
965e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
966e705c121SKalle Valo 
967e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
968e705c121SKalle Valo 			rx_ba_ssn = *ssn;
969e705c121SKalle Valo 
970e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
971e705c121SKalle Valo 					    rx_ba_ssn, action);
972e705c121SKalle Valo 	}
973e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
974e705c121SKalle Valo 
975e705c121SKalle Valo 	return ret;
976e705c121SKalle Valo }
977e705c121SKalle Valo 
978e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
979e705c121SKalle Valo 				     struct ieee80211_vif *vif)
980e705c121SKalle Valo {
981e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
982e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
983e705c121SKalle Valo 
984e705c121SKalle Valo 	mvmvif->uploaded = false;
9850ae98812SSara Sharon 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
986e705c121SKalle Valo 
987e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
988e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
989e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
990e705c121SKalle Valo 
991e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
992e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
99386e177d8SGregory Greenman 	memset(&mvmvif->probe_resp_data, 0, sizeof(mvmvif->probe_resp_data));
994e705c121SKalle Valo }
995e705c121SKalle Valo 
996e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
997e705c121SKalle Valo {
998fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
999e705c121SKalle Valo 
10009bf13beeSJohannes Berg 	mvm->cur_aid = 0;
10019bf13beeSJohannes Berg 
1002e705c121SKalle Valo 	mvm->scan_status = 0;
1003e705c121SKalle Valo 	mvm->ps_disabled = false;
1004b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1005e705c121SKalle Valo 
1006e705c121SKalle Valo 	/* just in case one was running */
1007305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1008e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1009e705c121SKalle Valo 
1010fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1011fc36ffdaSJohannes Berg 
1012e705c121SKalle Valo 	/*
1013e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1014e705c121SKalle Valo 	 * gone down during the HW restart
1015e705c121SKalle Valo 	 */
1016e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1017e705c121SKalle Valo 
1018e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1019e705c121SKalle Valo 
1020e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
10219c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1022e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1023e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1024e705c121SKalle Valo 
1025e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1026e705c121SKalle Valo 
1027e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
10287174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1029baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1030e705c121SKalle Valo 
1031e705c121SKalle Valo 	/* keep statistics ticking */
1032e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1033e705c121SKalle Valo }
1034e705c121SKalle Valo 
1035e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1036e705c121SKalle Valo {
1037e705c121SKalle Valo 	int ret;
1038e705c121SKalle Valo 
1039e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1040e705c121SKalle Valo 
10416d19a5ebSEmmanuel Grumbach 	ret = iwl_mvm_mei_get_ownership(mvm);
10426d19a5ebSEmmanuel Grumbach 	if (ret)
10436d19a5ebSEmmanuel Grumbach 		return ret;
10446d19a5ebSEmmanuel Grumbach 
10456d19a5ebSEmmanuel Grumbach 	if (mvm->mei_nvm_data) {
10466d19a5ebSEmmanuel Grumbach 		/* We got the NIC, we can now free the MEI NVM data */
10476d19a5ebSEmmanuel Grumbach 		kfree(mvm->mei_nvm_data);
10486d19a5ebSEmmanuel Grumbach 		mvm->mei_nvm_data = NULL;
10496d19a5ebSEmmanuel Grumbach 
10506d19a5ebSEmmanuel Grumbach 		/*
10516d19a5ebSEmmanuel Grumbach 		 * We can't free the nvm_data we allocated based on the SAP
10526d19a5ebSEmmanuel Grumbach 		 * data because we registered to cfg80211 with the channels
10536d19a5ebSEmmanuel Grumbach 		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
10546d19a5ebSEmmanuel Grumbach 		 * just in order to be able free it later.
10556d19a5ebSEmmanuel Grumbach 		 * NULLify nvm_data so that we will read the NVM from the
10566d19a5ebSEmmanuel Grumbach 		 * firmware this time.
10576d19a5ebSEmmanuel Grumbach 		 */
10586d19a5ebSEmmanuel Grumbach 		mvm->temp_nvm_data = mvm->nvm_data;
10596d19a5ebSEmmanuel Grumbach 		mvm->nvm_data = NULL;
10606d19a5ebSEmmanuel Grumbach 	}
10616d19a5ebSEmmanuel Grumbach 
1062bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1063bf8b286fSJohannes Berg 		/*
1064bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1065bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1066bf8b286fSJohannes Berg 		 */
1067bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1068bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1069e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1070e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1071a42b2af3SLuca Coelho 	}
1072e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1073e705c121SKalle Valo 
1074b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1075b108d8c7SShahar S Matityahu 			       NULL);
1076b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1077b108d8c7SShahar S Matityahu 			       NULL);
1078da2eb669SSara Sharon 
1079e8fe3b41SIlan Peer 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1080e8fe3b41SIlan Peer 
1081e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1082e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1083e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1084e705c121SKalle Valo 		 * would do.
1085e705c121SKalle Valo 		 */
1086e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1087e705c121SKalle Valo 	}
1088e705c121SKalle Valo 
1089e705c121SKalle Valo 	return ret;
1090e705c121SKalle Valo }
1091e705c121SKalle Valo 
1092e705c121SKalle Valo static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1093e705c121SKalle Valo {
1094e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1095e705c121SKalle Valo 	int ret;
10965283dd67SMordechay Goodstein 	int retry, max_retry = 0;
1097e705c121SKalle Valo 
1098e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
10995283dd67SMordechay Goodstein 
11005283dd67SMordechay Goodstein 	/* we are starting the mac not in error flow, and restart is enabled */
11015283dd67SMordechay Goodstein 	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
11025283dd67SMordechay Goodstein 	    iwlwifi_mod_params.fw_restart) {
11035283dd67SMordechay Goodstein 		max_retry = IWL_MAX_INIT_RETRY;
11045283dd67SMordechay Goodstein 		/*
11055283dd67SMordechay Goodstein 		 * This will prevent mac80211 recovery flows to trigger during
11065283dd67SMordechay Goodstein 		 * init failures
11075283dd67SMordechay Goodstein 		 */
11085283dd67SMordechay Goodstein 		set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
11095283dd67SMordechay Goodstein 	}
11105283dd67SMordechay Goodstein 
11115283dd67SMordechay Goodstein 	for (retry = 0; retry <= max_retry; retry++) {
1112e705c121SKalle Valo 		ret = __iwl_mvm_mac_start(mvm);
11135283dd67SMordechay Goodstein 		if (!ret)
11145283dd67SMordechay Goodstein 			break;
11155283dd67SMordechay Goodstein 
1116b8133439SAvraham Stern 		/*
1117b8133439SAvraham Stern 		 * In PLDR sync PCI re-enumeration is needed. no point to retry
1118b8133439SAvraham Stern 		 * mac start before that.
1119b8133439SAvraham Stern 		 */
1120b8133439SAvraham Stern 		if (mvm->pldr_sync) {
1121b8133439SAvraham Stern 			iwl_mei_alive_notif(false);
1122b8133439SAvraham Stern 			iwl_trans_pcie_remove(mvm->trans, true);
1123b8133439SAvraham Stern 			break;
1124b8133439SAvraham Stern 		}
1125b8133439SAvraham Stern 
11265283dd67SMordechay Goodstein 		IWL_ERR(mvm, "mac start retry %d\n", retry);
11275283dd67SMordechay Goodstein 	}
11285283dd67SMordechay Goodstein 	clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
11295283dd67SMordechay Goodstein 
1130e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1131e705c121SKalle Valo 
11327ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
11337ce1f215SEmmanuel Grumbach 
1134e705c121SKalle Valo 	return ret;
1135e705c121SKalle Valo }
1136e705c121SKalle Valo 
1137e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1138e705c121SKalle Valo {
1139e705c121SKalle Valo 	int ret;
1140e705c121SKalle Valo 
1141e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1142e705c121SKalle Valo 
1143e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
11444d4183c4SEmmanuel Grumbach 
1145e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1146e705c121SKalle Valo 	if (ret)
1147e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1148e705c121SKalle Valo 			ret);
1149e705c121SKalle Valo 
1150f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1151f130bb75SMordechay Goodstein 
1152e705c121SKalle Valo 	/*
1153e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1154e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1155e705c121SKalle Valo 	 */
1156e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1157e705c121SKalle Valo 
1158e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1159e705c121SKalle Valo }
1160e705c121SKalle Valo 
1161e705c121SKalle Valo static void
1162e705c121SKalle Valo iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1163e705c121SKalle Valo 			      enum ieee80211_reconfig_type reconfig_type)
1164e705c121SKalle Valo {
1165e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1166e705c121SKalle Valo 
1167e705c121SKalle Valo 	switch (reconfig_type) {
1168e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1169e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1170e705c121SKalle Valo 		break;
1171e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1172e705c121SKalle Valo 		break;
1173e705c121SKalle Valo 	}
1174e705c121SKalle Valo }
1175e705c121SKalle Valo 
1176e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1177e705c121SKalle Valo {
1178e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1179e705c121SKalle Valo 
1180b68bd2e3SIlan Peer 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1181b68bd2e3SIlan Peer 
1182e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1183e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1184e705c121SKalle Valo 	 */
1185e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1186e705c121SKalle Valo 
1187e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1188e705c121SKalle Valo 
1189971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) < 12)
1190f327236dSSharon 		iwl_mvm_rm_aux_sta(mvm);
1191f327236dSSharon 
1192fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1193e705c121SKalle Valo 
1194e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1195e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1196e705c121SKalle Valo 
1197e705c121SKalle Valo 	/*
1198155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1199155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1200155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1201e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1202e705c121SKalle Valo 	 * HW restart was requested.
1203e705c121SKalle Valo 	 */
1204155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1205155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1206155f7e04SEmmanuel Grumbach 			       &mvm->status))
1207e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1208e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1209e705c121SKalle Valo 
1210e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1211e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1212e705c121SKalle Valo 	 */
1213e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1214e705c121SKalle Valo 		int i;
1215e705c121SKalle Valo 
1216e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1217e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1218e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1219e705c121SKalle Valo 				      i))
1220e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1221e705c121SKalle Valo 		}
1222e705c121SKalle Valo 	}
1223e705c121SKalle Valo }
1224e705c121SKalle Valo 
1225e705c121SKalle Valo static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1226e705c121SKalle Valo {
1227e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1228e705c121SKalle Valo 
1229e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
123024afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1231771147b0SJohannes Berg 
1232771147b0SJohannes Berg 	/*
1233771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1234771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1235771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
12367174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1237771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1238771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1239771147b0SJohannes Berg 	 */
1240771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1241771147b0SJohannes Berg 
1242d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
124369e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1244e705c121SKalle Valo 
1245f9084775SNathan Errera 	/*
1246f9084775SNathan Errera 	 * The work item could be running or queued if the
1247f9084775SNathan Errera 	 * ROC time event stops just as we get here.
1248f9084775SNathan Errera 	 */
1249f9084775SNathan Errera 	flush_work(&mvm->roc_done_wk);
1250f9084775SNathan Errera 
12517ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
12527ce1f215SEmmanuel Grumbach 
1253e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1254e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1255e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1256e705c121SKalle Valo 
1257e705c121SKalle Valo 	/*
1258e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1259e705c121SKalle Valo 	 * discover that its list is now empty.
1260e705c121SKalle Valo 	 */
1261e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1262e705c121SKalle Valo }
1263e705c121SKalle Valo 
12641ab26632SMiri Korenblit struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1265e705c121SKalle Valo {
1266e705c121SKalle Valo 	u16 i;
1267e705c121SKalle Valo 
1268e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1269e705c121SKalle Valo 
1270e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1271e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1272e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1273e705c121SKalle Valo 
1274e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1275e705c121SKalle Valo 	return NULL;
1276e705c121SKalle Valo }
1277e705c121SKalle Valo 
1278e705c121SKalle Valo static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1279e705c121SKalle Valo 				s16 tx_power)
1280e705c121SKalle Valo {
1281971cbe50SJohannes Berg 	u32 cmd_id = REDUCE_TX_POWER_CMD;
12820791c2fcSHaim Dreyfuss 	int len;
1283216cdfb5SLuca Coelho 	struct iwl_dev_tx_power_cmd cmd = {
1284216cdfb5SLuca Coelho 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1285216cdfb5SLuca Coelho 		.common.mac_context_id =
1286e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1287216cdfb5SLuca Coelho 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1288e705c121SKalle Valo 	};
1289971cbe50SJohannes Berg 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1290e80bfd11SMordechay Goodstein 					   IWL_FW_CMD_VER_UNKNOWN);
1291e705c121SKalle Valo 
1292e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1293216cdfb5SLuca Coelho 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1294e705c121SKalle Valo 
1295b0aa02b3SAyala Barazani 	if (cmd_ver == 7)
1296b0aa02b3SAyala Barazani 		len = sizeof(cmd.v7);
1297b0aa02b3SAyala Barazani 	else if (cmd_ver == 6)
1298fbb7957dSLuca Coelho 		len = sizeof(cmd.v6);
1299fbb7957dSLuca Coelho 	else if (fw_has_api(&mvm->fw->ucode_capa,
13000791c2fcSHaim Dreyfuss 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
13010791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
13020791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
13030791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
13040791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
13050791c2fcSHaim Dreyfuss 	else
1306216cdfb5SLuca Coelho 		len = sizeof(cmd.v3);
1307216cdfb5SLuca Coelho 
1308216cdfb5SLuca Coelho 	/* all structs have the same common part, add it */
1309216cdfb5SLuca Coelho 	len += sizeof(cmd.common);
1310e705c121SKalle Valo 
1311971cbe50SJohannes Berg 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1312e705c121SKalle Valo }
1313e705c121SKalle Valo 
1314f6780614SSara Sharon static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1315f6780614SSara Sharon 				       struct ieee80211_vif *vif)
1316f6780614SSara Sharon {
1317f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1318f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1319f6780614SSara Sharon 	int ret;
1320f6780614SSara Sharon 
1321f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1322f6780614SSara Sharon 
1323f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1324f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
1325f6780614SSara Sharon 
1326f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
1327f6780614SSara Sharon 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
1328f6780614SSara Sharon 							  mvmvif->ap_sta_id);
1329f6780614SSara Sharon 
1330f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1331f6780614SSara Sharon 			ret = -EIO;
1332f6780614SSara Sharon 			goto out_unlock;
1333f6780614SSara Sharon 		}
1334f6780614SSara Sharon 
1335f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1336f6780614SSara Sharon 
1337f6780614SSara Sharon 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1338f6780614SSara Sharon 
13390202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
13400202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1341f6780614SSara Sharon 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1342f6780614SSara Sharon 			if (ret)
1343f6780614SSara Sharon 				goto out_unlock;
1344f6780614SSara Sharon 
1345f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1346f6780614SSara Sharon 		}
13470202bcf0SEmmanuel Grumbach 	}
1348f6780614SSara Sharon 
1349f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1350f6780614SSara Sharon 
1351f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1352f6780614SSara Sharon 
1353f6780614SSara Sharon out_unlock:
1354caf46377SSara Sharon 	if (mvmvif->csa_failed)
1355caf46377SSara Sharon 		ret = -EIO;
1356f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1357f6780614SSara Sharon 
1358f6780614SSara Sharon 	return ret;
1359f6780614SSara Sharon }
1360f6780614SSara Sharon 
1361f6780614SSara Sharon static void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1362f6780614SSara Sharon 					 struct ieee80211_vif *vif)
1363f6780614SSara Sharon {
1364f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1365f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1366f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1367f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1368f6780614SSara Sharon 							  mvmvif->color)),
1369f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1370f6780614SSara Sharon 	};
1371f6780614SSara Sharon 
1372ad12b231SNathan Errera 	/*
1373ad12b231SNathan Errera 	 * In the new flow since FW is in charge of the timing,
1374ad12b231SNathan Errera 	 * if driver has canceled the channel switch he will receive the
1375ad12b231SNathan Errera 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1376ad12b231SNathan Errera 	 */
1377ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1378ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1379ad12b231SNathan Errera 		return;
1380ad12b231SNathan Errera 
1381f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1382f6780614SSara Sharon 
1383f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
138458ddd9b6SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
138558ddd9b6SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
138658ddd9b6SEmmanuel Grumbach 		iwl_mvm_remove_csa_period(mvm, vif);
138758ddd9b6SEmmanuel Grumbach 	else
1388f6780614SSara Sharon 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1389f6780614SSara Sharon 					     WIDE_ID(MAC_CONF_GROUP,
1390f6780614SSara Sharon 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1391f6780614SSara Sharon 					     0, sizeof(cmd), &cmd));
1392caf46377SSara Sharon 	mvmvif->csa_failed = true;
1393f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1394f6780614SSara Sharon 
1395caf46377SSara Sharon 	iwl_mvm_post_channel_switch(hw, vif);
1396f6780614SSara Sharon }
1397f6780614SSara Sharon 
13981ab26632SMiri Korenblit void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1399f6780614SSara Sharon {
1400f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1401f6780614SSara Sharon 	struct ieee80211_vif *vif;
1402f6780614SSara Sharon 
1403f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1404f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1405f6780614SSara Sharon 
140670162580SShaul Triebitz 	/* Trigger disconnect (should clear the CSA state) */
1407f6780614SSara Sharon 	ieee80211_chswitch_done(vif, false);
1408f6780614SSara Sharon }
1409f6780614SSara Sharon 
14105abf3154SMordechay Goodstein static u8
14115abf3154SMordechay Goodstein iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
14125abf3154SMordechay Goodstein {
14135abf3154SMordechay Goodstein 	int data_start;
14145abf3154SMordechay Goodstein 	int control_start;
14155abf3154SMordechay Goodstein 	int bw;
14165abf3154SMordechay Goodstein 
14175abf3154SMordechay Goodstein 	if (chandef->width == NL80211_CHAN_WIDTH_320)
14185abf3154SMordechay Goodstein 		bw = 320;
14195abf3154SMordechay Goodstein 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
14205abf3154SMordechay Goodstein 		bw = 160;
14215abf3154SMordechay Goodstein 	else
14225abf3154SMordechay Goodstein 		return 0;
14235abf3154SMordechay Goodstein 
14245abf3154SMordechay Goodstein 	/* data is bw wide so the start is half the width */
14255abf3154SMordechay Goodstein 	data_start = chandef->center_freq1 - bw / 2;
14265abf3154SMordechay Goodstein 	/* control is 20Mhz width */
14275abf3154SMordechay Goodstein 	control_start = chandef->chan->center_freq - 10;
14285abf3154SMordechay Goodstein 
14295abf3154SMordechay Goodstein 	return (control_start - data_start) / 80;
14305abf3154SMordechay Goodstein }
14315abf3154SMordechay Goodstein 
14321ab26632SMiri Korenblit /*
14331ab26632SMiri Korenblit  * Returns true if addding the interface is done
14341ab26632SMiri Korenblit  * (either with success or failure)
14351ab26632SMiri Korenblit  */
14361ab26632SMiri Korenblit bool iwl_mvm_mac_add_interface_common(struct iwl_mvm *mvm,
14371ab26632SMiri Korenblit 				      struct ieee80211_hw *hw,
14381ab26632SMiri Korenblit 				      struct ieee80211_vif *vif, int *ret)
1439e705c121SKalle Valo {
1440e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
14411ab26632SMiri Korenblit 
14421ab26632SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
1443e705c121SKalle Valo 
1444e705c121SKalle Valo 	mvmvif->mvm = mvm;
144586e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
1446e705c121SKalle Valo 
1447e705c121SKalle Valo 	/*
1448e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1449e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1450e705c121SKalle Valo 	 * don't really have to check the types.
1451e705c121SKalle Valo 	 */
1452e705c121SKalle Valo 
1453e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1454e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1455e705c121SKalle Valo 		mvmvif->beacon_stats.accu_num_beacons +=
1456e705c121SKalle Valo 			mvmvif->beacon_stats.num_beacons;
1457e705c121SKalle Valo 
1458e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
14591ab26632SMiri Korenblit 	*ret = iwl_mvm_mac_ctxt_init(mvm, vif);
14601ab26632SMiri Korenblit 	if (*ret)
14611ab26632SMiri Korenblit 		return true;
1462e705c121SKalle Valo 
1463698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1464698478c4SSara Sharon 
1465e705c121SKalle Valo 	/*
1466e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1467e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1468e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1469e705c121SKalle Valo 	 * only after the binding.
1470e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1471e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1472e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1473e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1474e705c121SKalle Valo 	 * allocating resources :)
1475e705c121SKalle Valo 	 */
1476e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1477e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
14781ab26632SMiri Korenblit 		iwl_mvm_vif_dbgfs_register(mvm, vif);
14791ab26632SMiri Korenblit 		return true;
14801ab26632SMiri Korenblit 	}
14811ab26632SMiri Korenblit 
14821ab26632SMiri Korenblit 	mvmvif->features |= hw->netdev_features;
14831ab26632SMiri Korenblit 	return false;
14841ab26632SMiri Korenblit }
14851ab26632SMiri Korenblit 
14861ab26632SMiri Korenblit static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
14871ab26632SMiri Korenblit 					 struct ieee80211_vif *vif)
14881ab26632SMiri Korenblit {
14891ab26632SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
14901ab26632SMiri Korenblit 	int ret;
14911ab26632SMiri Korenblit 
14921ab26632SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
14931ab26632SMiri Korenblit 
1494e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1495e705c121SKalle Valo 	if (ret) {
1496e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
14971ab26632SMiri Korenblit 		return ret;
1498e705c121SKalle Valo 	}
1499e705c121SKalle Valo 
150026d6c16bSSara Sharon 	/*
150126d6c16bSSara Sharon 	 * Only queue for this station is the mcast queue,
150226d6c16bSSara Sharon 	 * which shouldn't be in TFD mask anyway
150326d6c16bSSara Sharon 	 */
15041ab26632SMiri Korenblit 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta, 0, vif->type,
1505ced19f26SSara Sharon 					IWL_STA_MULTICAST);
1506e705c121SKalle Valo }
1507e705c121SKalle Valo 
15081ab26632SMiri Korenblit static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
15091ab26632SMiri Korenblit 				     struct ieee80211_vif *vif)
15101ab26632SMiri Korenblit {
15111ab26632SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
15121ab26632SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
15131ab26632SMiri Korenblit 	int ret;
15141ab26632SMiri Korenblit 
15151ab26632SMiri Korenblit 	mutex_lock(&mvm->mutex);
15161ab26632SMiri Korenblit 
15171ab26632SMiri Korenblit 	/* Common for MLD and non-MLD API */
15181ab26632SMiri Korenblit 	if (iwl_mvm_mac_add_interface_common(mvm, hw, vif, &ret))
15191ab26632SMiri Korenblit 		goto out;
1520e705c121SKalle Valo 
1521e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1522e705c121SKalle Valo 	if (ret)
152398c0de7bSMiri Korenblit 		goto out_unlock;
1524e705c121SKalle Valo 
1525e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1526e705c121SKalle Valo 	if (ret)
1527e705c121SKalle Valo 		goto out_remove_mac;
1528e705c121SKalle Valo 
1529e705c121SKalle Valo 	/* beacon filtering */
1530e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1531e705c121SKalle Valo 	if (ret)
1532e705c121SKalle Valo 		goto out_remove_mac;
1533e705c121SKalle Valo 
1534e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1535e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1536e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1537e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1538e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1539e705c121SKalle Valo 	}
1540e705c121SKalle Valo 
1541e705c121SKalle Valo 	/*
1542e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1543e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1544e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1545e705c121SKalle Valo 	 */
1546e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1547e705c121SKalle Valo 
1548e705c121SKalle Valo 		mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1549e705c121SKalle Valo 		if (!mvmvif->phy_ctxt) {
1550e705c121SKalle Valo 			ret = -ENOSPC;
1551e705c121SKalle Valo 			goto out_free_bf;
1552e705c121SKalle Valo 		}
1553e705c121SKalle Valo 
1554e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1555e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1556e705c121SKalle Valo 		if (ret)
1557e705c121SKalle Valo 			goto out_unref_phy;
1558e705c121SKalle Valo 
1559d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1560e705c121SKalle Valo 		if (ret)
1561e705c121SKalle Valo 			goto out_unbind;
1562e705c121SKalle Valo 
1563e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1564e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1565e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1566e705c121SKalle Valo 	}
1567e705c121SKalle Valo 
1568b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1569f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1570f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1571b0ffe455SJohannes Berg 
15725abf3154SMordechay Goodstein 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1573baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
15745abf3154SMordechay Goodstein 		mvm->monitor_p80 =
15755abf3154SMordechay Goodstein 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chandef);
15765abf3154SMordechay Goodstein 	}
1577baf41bc3SShaul Triebitz 
1578e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
15796d19a5ebSEmmanuel Grumbach 
15806d19a5ebSEmmanuel Grumbach 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
15816d19a5ebSEmmanuel Grumbach 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
15826d19a5ebSEmmanuel Grumbach 	    !mvm->csme_vif && mvm->mei_registered) {
15836d19a5ebSEmmanuel Grumbach 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
15846d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
15856d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = vif;
15866d19a5ebSEmmanuel Grumbach 	}
15876d19a5ebSEmmanuel Grumbach 
15881ab26632SMiri Korenblit out:
15891ab26632SMiri Korenblit 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
15901ab26632SMiri Korenblit 		     vif->type == NL80211_IFTYPE_ADHOC))
15911ab26632SMiri Korenblit 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
15921ab26632SMiri Korenblit 
1593e705c121SKalle Valo 	goto out_unlock;
1594e705c121SKalle Valo 
1595e705c121SKalle Valo  out_unbind:
1596e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1597e705c121SKalle Valo  out_unref_phy:
1598e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1599e705c121SKalle Valo  out_free_bf:
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  out_remove_mac:
1606e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1607e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1608e705c121SKalle Valo  out_unlock:
1609e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1610e705c121SKalle Valo 
1611e705c121SKalle Valo 	return ret;
1612e705c121SKalle Valo }
1613e705c121SKalle Valo 
1614e705c121SKalle Valo static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1615e705c121SKalle Valo 					struct ieee80211_vif *vif)
1616e705c121SKalle Valo {
1617e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1618e705c121SKalle Valo 		/*
1619e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1620e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1621e705c121SKalle Valo 		 * queue are sent in ROC session.
1622e705c121SKalle Valo 		 */
1623e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1624e705c121SKalle Valo 	}
1625e705c121SKalle Valo }
1626e705c121SKalle Valo 
162760efeca1SMiri Korenblit /* This function is doing the common part of removing the interface for
162860efeca1SMiri Korenblit  * both - MLD and non-MLD modes. Returns true if removing the interface
162960efeca1SMiri Korenblit  * is done
163060efeca1SMiri Korenblit  */
163160efeca1SMiri Korenblit bool iwl_mvm_mac_remove_interface_common(struct ieee80211_hw *hw,
1632e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1633e705c121SKalle Valo {
1634e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1635e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
163686e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1637e705c121SKalle Valo 
1638e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1639e705c121SKalle Valo 
1640b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1641b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1642b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1643b0ffe455SJohannes Berg 
1644e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1645e705c121SKalle Valo 
16466d19a5ebSEmmanuel Grumbach 	if (vif == mvm->csme_vif) {
16476d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(NULL);
16486d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = NULL;
16496d19a5ebSEmmanuel Grumbach 	}
16506d19a5ebSEmmanuel Grumbach 
165186e177d8SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->probe_resp_data,
165286e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
165386e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
165486e177d8SGregory Greenman 	if (probe_data)
165586e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
165686e177d8SGregory Greenman 
1657e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1658e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1659e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1660e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1661e705c121SKalle Valo 	}
1662e705c121SKalle Valo 
1663b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1664b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1665b73f9a4aSJohannes Berg 
1666e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1667e705c121SKalle Valo 
1668e705c121SKalle Valo 	/*
1669e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1670e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1671e705c121SKalle Valo 	 */
1672e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1673e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1674e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1675e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1676e705c121SKalle Valo 			mvm->noa_vif = NULL;
1677e705c121SKalle Valo 			mvm->noa_duration = 0;
1678e705c121SKalle Valo 		}
1679e705c121SKalle Valo #endif
168060efeca1SMiri Korenblit 		return true;
1681e705c121SKalle Valo 	}
1682e705c121SKalle Valo 
168360efeca1SMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
168460efeca1SMiri Korenblit 	return false;
168560efeca1SMiri Korenblit }
168660efeca1SMiri Korenblit 
168760efeca1SMiri Korenblit static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
168860efeca1SMiri Korenblit 					 struct ieee80211_vif *vif)
168960efeca1SMiri Korenblit {
169060efeca1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
169160efeca1SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
169260efeca1SMiri Korenblit 
169360efeca1SMiri Korenblit 	if (iwl_mvm_mac_remove_interface_common(hw, vif))
169460efeca1SMiri Korenblit 		goto out;
169560efeca1SMiri Korenblit 
1696e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1697e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1698d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1699e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1700e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1701e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
1702e705c121SKalle Valo 	}
1703e705c121SKalle Valo 
1704e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1705e705c121SKalle Valo 
1706698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1707698478c4SSara Sharon 
1708baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1709baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1710baf41bc3SShaul Triebitz 
171160efeca1SMiri Korenblit out:
171260efeca1SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP ||
171360efeca1SMiri Korenblit 	    vif->type == NL80211_IFTYPE_ADHOC) {
171460efeca1SMiri Korenblit 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
171560efeca1SMiri Korenblit 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
171660efeca1SMiri Korenblit 	}
171760efeca1SMiri Korenblit 
1718e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1719e705c121SKalle Valo }
1720e705c121SKalle Valo 
1721e705c121SKalle Valo static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1722e705c121SKalle Valo {
1723e705c121SKalle Valo 	return 0;
1724e705c121SKalle Valo }
1725e705c121SKalle Valo 
1726e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1727e705c121SKalle Valo 	struct iwl_mvm *mvm;
1728e705c121SKalle Valo 	int port_id;
1729e705c121SKalle Valo };
1730e705c121SKalle Valo 
1731e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1732e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1733e705c121SKalle Valo {
1734e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1735e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1736e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
173797bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
173897bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
173997bce57bSLuca Coelho 		.flags = CMD_ASYNC,
174097bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
174197bce57bSLuca Coelho 	};
1742e705c121SKalle Valo 	int ret, len;
1743e705c121SKalle Valo 
1744e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1745e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1746e705c121SKalle Valo 		return;
1747e705c121SKalle Valo 
1748e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1749f276e20bSJohannes Berg 	    !vif->cfg.assoc)
1750e705c121SKalle Valo 		return;
1751e705c121SKalle Valo 
1752e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1753e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1754e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1755e705c121SKalle Valo 
175697bce57bSLuca Coelho 	hcmd.len[0] = len;
175797bce57bSLuca Coelho 	hcmd.data[0] = cmd;
175897bce57bSLuca Coelho 
175997bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1760e705c121SKalle Valo 	if (ret)
1761e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1762e705c121SKalle Valo }
1763e705c121SKalle Valo 
1764e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1765e705c121SKalle Valo {
1766e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1767e705c121SKalle Valo 		.mvm = mvm,
1768e705c121SKalle Valo 	};
1769db66abeeSJohannes Berg 	int ret;
1770e705c121SKalle Valo 
1771e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1772e705c121SKalle Valo 
1773e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1774e705c121SKalle Valo 		return;
1775e705c121SKalle Valo 
1776e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1777e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1778e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1779db66abeeSJohannes Berg 
1780db66abeeSJohannes Berg 	/*
1781db66abeeSJohannes Berg 	 * Send a (synchronous) ech command so that we wait for the
1782db66abeeSJohannes Berg 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
1783db66abeeSJohannes Berg 	 * the interface iterator. Otherwise, we might get here over
1784db66abeeSJohannes Berg 	 * and over again (by userspace just sending a lot of these)
1785db66abeeSJohannes Berg 	 * and the CPU can send them faster than the firmware can
1786db66abeeSJohannes Berg 	 * process them.
1787db66abeeSJohannes Berg 	 * Note that the CPU is still faster - but with this we'll
1788db66abeeSJohannes Berg 	 * actually send fewer commands overall because the CPU will
1789db66abeeSJohannes Berg 	 * not schedule the work in mac80211 as frequently if it's
1790db66abeeSJohannes Berg 	 * still running when rescheduled (possibly multiple times).
1791db66abeeSJohannes Berg 	 */
1792db66abeeSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1793db66abeeSJohannes Berg 	if (ret)
1794db66abeeSJohannes Berg 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
1795e705c121SKalle Valo }
1796e705c121SKalle Valo 
1797e705c121SKalle Valo static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1798e705c121SKalle Valo 				     struct netdev_hw_addr_list *mc_list)
1799e705c121SKalle Valo {
1800e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1801e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1802e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1803e705c121SKalle Valo 	int addr_count;
1804e705c121SKalle Valo 	bool pass_all;
1805e705c121SKalle Valo 	int len;
1806e705c121SKalle Valo 
1807e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1808e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1809e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1810e705c121SKalle Valo 	if (pass_all)
1811e705c121SKalle Valo 		addr_count = 0;
1812e705c121SKalle Valo 
1813e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1814e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1815e705c121SKalle Valo 	if (!cmd)
1816e705c121SKalle Valo 		return 0;
1817e705c121SKalle Valo 
1818e705c121SKalle Valo 	if (pass_all) {
1819e705c121SKalle Valo 		cmd->pass_all = 1;
1820e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1821e705c121SKalle Valo 	}
1822e705c121SKalle Valo 
1823e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1824e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1825e705c121SKalle Valo 				   cmd->count, addr->addr);
1826e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1827e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1828e705c121SKalle Valo 		cmd->count++;
1829e705c121SKalle Valo 	}
1830e705c121SKalle Valo 
1831e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1832e705c121SKalle Valo }
1833e705c121SKalle Valo 
1834e705c121SKalle Valo static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1835e705c121SKalle Valo 				     unsigned int changed_flags,
1836e705c121SKalle Valo 				     unsigned int *total_flags,
1837e705c121SKalle Valo 				     u64 multicast)
1838e705c121SKalle Valo {
1839e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1840e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1841e705c121SKalle Valo 
1842e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1843e705c121SKalle Valo 
1844e705c121SKalle Valo 	/* replace previous configuration */
1845e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1846e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1847e705c121SKalle Valo 
1848e705c121SKalle Valo 	if (!cmd)
1849e705c121SKalle Valo 		goto out;
1850e705c121SKalle Valo 
185161e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
185261e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
185361e7d91bSLuca Coelho 
185461e7d91bSLuca Coelho 	if (cmd->pass_all)
185561e7d91bSLuca Coelho 		cmd->count = 0;
185661e7d91bSLuca Coelho 
1857e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1858e705c121SKalle Valo out:
1859e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1860e705c121SKalle Valo 	*total_flags = 0;
1861e705c121SKalle Valo }
1862e705c121SKalle Valo 
1863e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1864e705c121SKalle Valo 					struct ieee80211_vif *vif,
1865e705c121SKalle Valo 					unsigned int filter_flags,
1866e705c121SKalle Valo 					unsigned int changed_flags)
1867e705c121SKalle Valo {
1868e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1869e705c121SKalle Valo 
1870e705c121SKalle Valo 	/* We support only filter for probe requests */
1871e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1872e705c121SKalle Valo 		return;
1873e705c121SKalle Valo 
1874e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1875f276e20bSJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
1876e705c121SKalle Valo 	    !vif->p2p)
1877e705c121SKalle Valo 		return;
1878e705c121SKalle Valo 
1879e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1880e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1881e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1882e705c121SKalle Valo }
1883e705c121SKalle Valo 
1884a07a8f37SSara Sharon static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
1885a07a8f37SSara Sharon 				    struct ieee80211_vif *vif)
1886a07a8f37SSara Sharon {
1887a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1888a07a8f37SSara Sharon 
1889a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1890a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1891a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1892a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1893a07a8f37SSara Sharon 
1894a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1895a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1896a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1897a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1898a07a8f37SSara Sharon }
1899a07a8f37SSara Sharon 
1900f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1901f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1902f92659a1SSara Sharon {
1903d0a9123eSJohannes Berg 	if (vif->bss_conf.mu_mimo_owner) {
1904f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1905f92659a1SSara Sharon 
1906f92659a1SSara Sharon 		/*
1907f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1908f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1909f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
1910f92659a1SSara Sharon 		 */
1911afe0d181SJohannes Berg 		ieee80211_update_mu_groups(vif, 0,
1912f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
1913f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
1914f92659a1SSara Sharon 	}
1915f92659a1SSara Sharon }
1916f92659a1SSara Sharon 
1917f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1918f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
1919f92659a1SSara Sharon {
1920f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1921f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1922f92659a1SSara Sharon 
1923f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
1924f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1925f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
1926f92659a1SSara Sharon }
1927f92659a1SSara Sharon 
1928514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1929514c3069SLuca Coelho {
1930514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
1931514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
1932514c3069SLuca Coelho 	u8 residue_bits;
1933514c3069SLuca Coelho 	u8 res;
1934514c3069SLuca Coelho 
1935514c3069SLuca Coelho 	if (bit_num <= 5)
1936514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
1937514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
1938514c3069SLuca Coelho 
1939514c3069SLuca Coelho 	/*
1940514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
1941514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
1942514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
1943514c3069SLuca Coelho 	 */
1944514c3069SLuca Coelho 
1945514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
1946514c3069SLuca Coelho 
1947514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
1948514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
1949514c3069SLuca Coelho 	      residue_bits;
1950514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
1951514c3069SLuca Coelho 
1952514c3069SLuca Coelho 	return res;
1953514c3069SLuca Coelho }
1954514c3069SLuca Coelho 
1955091296d3SMiri Korenblit static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
1956091296d3SMiri Korenblit 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
1957cb63eb43SMiri Korenblit 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
1958cb63eb43SMiri Korenblit 			      bool inheritance)
1959091296d3SMiri Korenblit {
1960091296d3SMiri Korenblit 	int i;
1961091296d3SMiri Korenblit 
1962091296d3SMiri Korenblit 	/*
1963091296d3SMiri Korenblit 	* FW currently supports only nss == MAX_HE_SUPP_NSS
1964091296d3SMiri Korenblit 	*
1965091296d3SMiri Korenblit 	* If nss > MAX: we can ignore values we don't support
1966091296d3SMiri Korenblit 	* If nss < MAX: we can set zeros in other streams
1967091296d3SMiri Korenblit 	*/
1968091296d3SMiri Korenblit 	if (nss > MAX_HE_SUPP_NSS) {
19694d8421f2SJason A. Donenfeld 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
1970091296d3SMiri Korenblit 			       MAX_HE_SUPP_NSS);
1971091296d3SMiri Korenblit 		nss = MAX_HE_SUPP_NSS;
1972091296d3SMiri Korenblit 	}
1973091296d3SMiri Korenblit 
1974091296d3SMiri Korenblit 	for (i = 0; i < nss; i++) {
1975091296d3SMiri Korenblit 		u8 ru_index_tmp = ru_index_bitmap << 1;
1976091296d3SMiri Korenblit 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
1977091296d3SMiri Korenblit 		u8 bw;
1978091296d3SMiri Korenblit 
1979091296d3SMiri Korenblit 		for (bw = 0;
1980091296d3SMiri Korenblit 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
1981091296d3SMiri Korenblit 		     bw++) {
1982091296d3SMiri Korenblit 			ru_index_tmp >>= 1;
1983091296d3SMiri Korenblit 
1984cb63eb43SMiri Korenblit 			/*
1985cb63eb43SMiri Korenblit 			* According to the 11be spec, if for a specific BW the PPE Thresholds
1986cb63eb43SMiri Korenblit 			* isn't present - it should inherit the thresholds from the last
1987cb63eb43SMiri Korenblit 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
1988cb63eb43SMiri Korenblit 			* this inheritance - continue in this case
1989cb63eb43SMiri Korenblit 			*/
1990cb63eb43SMiri Korenblit 			if (!(ru_index_tmp & 1)) {
1991cb63eb43SMiri Korenblit 				if (inheritance)
1992cb63eb43SMiri Korenblit 					goto set_thresholds;
1993cb63eb43SMiri Korenblit 				else
1994091296d3SMiri Korenblit 					continue;
1995cb63eb43SMiri Korenblit 			}
1996091296d3SMiri Korenblit 
1997091296d3SMiri Korenblit 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
1998091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
1999091296d3SMiri Korenblit 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2000091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2001091296d3SMiri Korenblit 
2002cb63eb43SMiri Korenblit set_thresholds:
2003091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2004091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2005091296d3SMiri Korenblit 		}
2006091296d3SMiri Korenblit 	}
2007091296d3SMiri Korenblit }
2008091296d3SMiri Korenblit 
2009091296d3SMiri Korenblit static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
2010091296d3SMiri Korenblit 					    struct ieee80211_sta *sta,
2011cb63eb43SMiri Korenblit 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2012cb63eb43SMiri Korenblit 					    bool inheritance)
2013091296d3SMiri Korenblit {
2014046d2e7cSSriram R 	u8 nss = (sta->deflink.he_cap.ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) + 1;
2015046d2e7cSSriram R 	u8 *ppe = &sta->deflink.he_cap.ppe_thres[0];
2016091296d3SMiri Korenblit 	u8 ru_index_bitmap =
2017091296d3SMiri Korenblit 		u8_get_bits(*ppe,
2018091296d3SMiri Korenblit 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2019091296d3SMiri Korenblit 	/* Starting after PPE header */
2020091296d3SMiri Korenblit 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2021091296d3SMiri Korenblit 
2022cb63eb43SMiri Korenblit 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2023cb63eb43SMiri Korenblit 			  inheritance);
2024091296d3SMiri Korenblit }
2025091296d3SMiri Korenblit 
2026091296d3SMiri Korenblit static void iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
2027091296d3SMiri Korenblit 						     u8 nominal_padding,
2028091296d3SMiri Korenblit 						     u32 *flags)
2029091296d3SMiri Korenblit {
2030091296d3SMiri Korenblit 	int low_th = -1;
2031091296d3SMiri Korenblit 	int high_th = -1;
2032091296d3SMiri Korenblit 	int i;
2033091296d3SMiri Korenblit 
2034cb63eb43SMiri Korenblit 	/* all the macros are the same for EHT and HE */
2035091296d3SMiri Korenblit 	switch (nominal_padding) {
2036cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2037091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2038091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2039091296d3SMiri Korenblit 		break;
2040cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2041091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_BPSK;
2042091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2043091296d3SMiri Korenblit 		break;
2044cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2045cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2046091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2047091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_BPSK;
2048091296d3SMiri Korenblit 		break;
2049091296d3SMiri Korenblit 	}
2050091296d3SMiri Korenblit 
2051091296d3SMiri Korenblit 	/* Set the PPE thresholds accordingly */
2052091296d3SMiri Korenblit 	if (low_th >= 0 && high_th >= 0) {
2053091296d3SMiri Korenblit 		for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2054091296d3SMiri Korenblit 			u8 bw;
2055091296d3SMiri Korenblit 
2056091296d3SMiri Korenblit 			for (bw = 0;
2057091296d3SMiri Korenblit 			     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2058091296d3SMiri Korenblit 			     bw++) {
2059091296d3SMiri Korenblit 				pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2060091296d3SMiri Korenblit 				pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2061091296d3SMiri Korenblit 			}
2062091296d3SMiri Korenblit 		}
2063091296d3SMiri Korenblit 
2064091296d3SMiri Korenblit 		*flags |= STA_CTXT_HE_PACKET_EXT;
2065091296d3SMiri Korenblit 	}
2066091296d3SMiri Korenblit }
2067091296d3SMiri Korenblit 
2068cb63eb43SMiri Korenblit static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2069cb63eb43SMiri Korenblit 					 u8 nominal_padding)
2070cb63eb43SMiri Korenblit {
2071cb63eb43SMiri Korenblit 	int i;
2072cb63eb43SMiri Korenblit 
2073cb63eb43SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2074cb63eb43SMiri Korenblit 		u8 bw;
2075cb63eb43SMiri Korenblit 
2076cb63eb43SMiri Korenblit 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2077cb63eb43SMiri Korenblit 		     bw++) {
2078cb63eb43SMiri Korenblit 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2079cb63eb43SMiri Korenblit 
2080cb63eb43SMiri Korenblit 			if (nominal_padding >
2081cb63eb43SMiri Korenblit 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2082cb63eb43SMiri Korenblit 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2083cb63eb43SMiri Korenblit 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2084cb63eb43SMiri Korenblit 			else if (nominal_padding ==
2085cb63eb43SMiri Korenblit 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2086cb63eb43SMiri Korenblit 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2087cb63eb43SMiri Korenblit 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2088cb63eb43SMiri Korenblit 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2089cb63eb43SMiri Korenblit 		}
2090cb63eb43SMiri Korenblit 	}
2091cb63eb43SMiri Korenblit }
2092cb63eb43SMiri Korenblit 
20939c4f15caSMiri Korenblit /*
20949c4f15caSMiri Korenblit  * This function sets the MU EDCA parameters ans returns whether MU EDCA
20959c4f15caSMiri Korenblit  * is enabled or not
20969c4f15caSMiri Korenblit  */
209755eb1c5fSMiri Korenblit bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
20989c4f15caSMiri Korenblit 				   struct iwl_mvm_vif *mvmvif,
209955eb1c5fSMiri Korenblit 				   struct iwl_he_backoff_conf *trig_based_txf)
21009c4f15caSMiri Korenblit {
21019c4f15caSMiri Korenblit 	int i;
21029c4f15caSMiri Korenblit 	/* Mark MU EDCA as enabled, unless none detected on some AC */
21039c4f15caSMiri Korenblit 	bool mu_edca_enabled = true;
21049c4f15caSMiri Korenblit 
21059c4f15caSMiri Korenblit 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
21069c4f15caSMiri Korenblit 		struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
21079c4f15caSMiri Korenblit 			&mvmvif->queue_params[i].mu_edca_param_rec;
21089c4f15caSMiri Korenblit 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
21099c4f15caSMiri Korenblit 
21109c4f15caSMiri Korenblit 		if (!mvmvif->queue_params[i].mu_edca) {
21119c4f15caSMiri Korenblit 			mu_edca_enabled = false;
21129c4f15caSMiri Korenblit 			break;
21139c4f15caSMiri Korenblit 		}
21149c4f15caSMiri Korenblit 
21159c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmin =
21169c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
21179c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmax =
21189c4f15caSMiri Korenblit 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
21199c4f15caSMiri Korenblit 		trig_based_txf[ac].aifsn =
21209c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->aifsn & 0xf);
21219c4f15caSMiri Korenblit 		trig_based_txf[ac].mu_time =
21229c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->mu_edca_timer);
21239c4f15caSMiri Korenblit 	}
21249c4f15caSMiri Korenblit 
21259c4f15caSMiri Korenblit 	return mu_edca_enabled;
21269c4f15caSMiri Korenblit }
21279c4f15caSMiri Korenblit 
21289be162a7SMiri Korenblit bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
21299c4f15caSMiri Korenblit {
21309c4f15caSMiri Korenblit 	const struct ieee80211_supported_band *sband;
21319c4f15caSMiri Korenblit 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
21329c4f15caSMiri Korenblit 
21339c4f15caSMiri Korenblit 	/* This capability is the same for all bands,
21349c4f15caSMiri Korenblit 	 * so take it from one of them.
21359c4f15caSMiri Korenblit 	 */
21369c4f15caSMiri Korenblit 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
21379c4f15caSMiri Korenblit 	own_he_cap = ieee80211_get_he_iftype_cap(sband,
21389c4f15caSMiri Korenblit 						 ieee80211_vif_type_p2p(vif));
21399c4f15caSMiri Korenblit 
21409c4f15caSMiri Korenblit 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
21419c4f15caSMiri Korenblit 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
21429c4f15caSMiri Korenblit }
21439c4f15caSMiri Korenblit 
2144514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2145514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2146514c3069SLuca Coelho {
2147514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
214842506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2149514c3069SLuca Coelho 		.sta_id = sta_id,
2150514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2151dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2152514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2153514c3069SLuca Coelho 		.frame_time_rts_th =
2154514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2155514c3069SLuca Coelho 	};
215642506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
215742506dd2SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
215842506dd2SJohannes Berg 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
215942506dd2SJohannes Berg 	int size;
2160514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2161514c3069SLuca Coelho 	u32 flags;
2162514c3069SLuca Coelho 	int i;
216342506dd2SJohannes Berg 	void *cmd;
2164cb63eb43SMiri Korenblit 	u8 nominal_padding;
216542506dd2SJohannes Berg 
216642506dd2SJohannes Berg 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
216742506dd2SJohannes Berg 		ver = 1;
216842506dd2SJohannes Berg 
216942506dd2SJohannes Berg 	switch (ver) {
217042506dd2SJohannes Berg 	case 1:
217142506dd2SJohannes Berg 		/* same layout as v2 except some data at the end */
217242506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
217342506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
217442506dd2SJohannes Berg 		break;
217542506dd2SJohannes Berg 	case 2:
217642506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
217742506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
217842506dd2SJohannes Berg 		break;
217942506dd2SJohannes Berg 	case 3:
218042506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd;
218142506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
218242506dd2SJohannes Berg 		break;
218342506dd2SJohannes Berg 	default:
218442506dd2SJohannes Berg 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
218542506dd2SJohannes Berg 		return;
218642506dd2SJohannes Berg 	}
2187514c3069SLuca Coelho 
2188514c3069SLuca Coelho 	rcu_read_lock();
2189514c3069SLuca Coelho 
2190514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
219112d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2192514c3069SLuca Coelho 		rcu_read_unlock();
2193514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2194514c3069SLuca Coelho 		return;
2195514c3069SLuca Coelho 	}
2196514c3069SLuca Coelho 
2197046d2e7cSSriram R 	if (!sta->deflink.he_cap.has_he) {
2198514c3069SLuca Coelho 		rcu_read_unlock();
2199514c3069SLuca Coelho 		return;
2200514c3069SLuca Coelho 	}
2201514c3069SLuca Coelho 
2202514c3069SLuca Coelho 	flags = 0;
2203514c3069SLuca Coelho 
22044f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
22054f58121dSIlan Peer 	if (mvmvif->he_ru_2mhz_block)
22064f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
22074f58121dSIlan Peer 
2208514c3069SLuca Coelho 	/* HTC flags */
2209046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[0] &
2210514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP0_HTC_HE)
2211514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2212046d2e7cSSriram R 	if ((sta->deflink.he_cap.he_cap_elem.mac_cap_info[1] &
2213514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2214046d2e7cSSriram R 	    (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2215514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2216514c3069SLuca Coelho 		u8 link_adap =
2217046d2e7cSSriram R 			((sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2218514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2219046d2e7cSSriram R 			 (sta->deflink.he_cap.he_cap_elem.mac_cap_info[1] &
2220514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2221514c3069SLuca Coelho 
2222514c3069SLuca Coelho 		if (link_adap == 2)
2223514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2224514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2225514c3069SLuca Coelho 		else if (link_adap == 3)
2226514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2227514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2228514c3069SLuca Coelho 	}
2229046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2230514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2231046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[3] &
2232514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2233514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2234046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2235514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2236514c3069SLuca Coelho 
2237189b8d44SNaftali Goldstein 	/*
2238189b8d44SNaftali Goldstein 	 * Initialize the PPE thresholds to "None" (7), as described in Table
2239189b8d44SNaftali Goldstein 	 * 9-262ac of 80211.ax/D3.0.
2240189b8d44SNaftali Goldstein 	 */
2241091296d3SMiri Korenblit 	memset(&sta_ctxt_cmd.pkt_ext, IWL_HE_PKT_EXT_NONE,
2242091296d3SMiri Korenblit 	       sizeof(sta_ctxt_cmd.pkt_ext));
2243189b8d44SNaftali Goldstein 
2244cb63eb43SMiri Korenblit 	if (sta->deflink.eht_cap.has_eht) {
2245cb63eb43SMiri Korenblit 		nominal_padding =
2246cb63eb43SMiri Korenblit 			u8_get_bits(sta->deflink.eht_cap.eht_cap_elem.phy_cap_info[5],
2247cb63eb43SMiri Korenblit 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
2248cb63eb43SMiri Korenblit 
2249cb63eb43SMiri Korenblit 		/* If PPE Thresholds exists, parse them into a FW-familiar format. */
2250cb63eb43SMiri Korenblit 		if (sta->deflink.eht_cap.eht_cap_elem.phy_cap_info[5] &
2251cb63eb43SMiri Korenblit 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
2252cb63eb43SMiri Korenblit 			u8 nss = (sta->deflink.eht_cap.eht_ppe_thres[0] &
2253cb63eb43SMiri Korenblit 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
2254cb63eb43SMiri Korenblit 			u8 *ppe = &sta->deflink.eht_cap.eht_ppe_thres[0];
2255cb63eb43SMiri Korenblit 			u8 ru_index_bitmap =
2256cb63eb43SMiri Korenblit 				u16_get_bits(*ppe,
2257cb63eb43SMiri Korenblit 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2258cb63eb43SMiri Korenblit 			 /* Starting after PPE header */
2259cb63eb43SMiri Korenblit 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
2260cb63eb43SMiri Korenblit 
2261cb63eb43SMiri Korenblit 			iwl_mvm_parse_ppe(mvm,
2262cb63eb43SMiri Korenblit 					  &sta_ctxt_cmd.pkt_ext,
2263cb63eb43SMiri Korenblit 					  nss, ru_index_bitmap, ppe,
2264cb63eb43SMiri Korenblit 					  ppe_pos_bit, true);
2265cb63eb43SMiri Korenblit 			flags |= STA_CTXT_HE_PACKET_EXT;
2266cb63eb43SMiri Korenblit 		/* EHT PPE Thresholds doesn't exist - set the API according to HE PPE Tresholds*/
2267cb63eb43SMiri Korenblit 		} else if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[6] &
2268cb63eb43SMiri Korenblit 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2269cb63eb43SMiri Korenblit 			struct iwl_he_pkt_ext_v2 *pkt_ext =
2270cb63eb43SMiri Korenblit 				&sta_ctxt_cmd.pkt_ext;
2271cb63eb43SMiri Korenblit 
2272cb63eb43SMiri Korenblit 			/*
2273cb63eb43SMiri Korenblit 			* Even though HE Capabilities IE doesn't contain PPE
2274cb63eb43SMiri Korenblit 			* Thresholds for BW 320Mhz, thresholds for this BW will
2275cb63eb43SMiri Korenblit 			* be filled in with the same values as 160Mhz, due to
2276cb63eb43SMiri Korenblit 			* the inheritance, as required.
2277cb63eb43SMiri Korenblit 			*/
2278cb63eb43SMiri Korenblit 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, sta, pkt_ext,
2279cb63eb43SMiri Korenblit 							true);
2280cb63eb43SMiri Korenblit 
2281cb63eb43SMiri Korenblit 			/*
2282cb63eb43SMiri Korenblit 			* According to the requirements, for MCSs 12-13 the maximum value between
2283cb63eb43SMiri Korenblit 			* HE PPE Threshold and Common Nominal Packet Padding needs to be taken
2284cb63eb43SMiri Korenblit 			*/
2285cb63eb43SMiri Korenblit 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
2286cb63eb43SMiri Korenblit 
2287cb63eb43SMiri Korenblit 			flags |= STA_CTXT_HE_PACKET_EXT;
2288cb63eb43SMiri Korenblit 
2289cb63eb43SMiri Korenblit 		/*
2290cb63eb43SMiri Korenblit 		* if PPE Thresholds doesn't present in both EHT IE and HE IE -
2291cb63eb43SMiri Korenblit 		* take the Thresholds from Common Nominal Packet Padding field
2292cb63eb43SMiri Korenblit 		*/
2293cb63eb43SMiri Korenblit 		} else {
2294cb63eb43SMiri Korenblit 			iwl_mvm_set_pkt_ext_from_nominal_padding(&sta_ctxt_cmd.pkt_ext,
2295cb63eb43SMiri Korenblit 								 nominal_padding,
2296cb63eb43SMiri Korenblit 								 &flags);
2297cb63eb43SMiri Korenblit 		}
2298cb63eb43SMiri Korenblit 	} else if (sta->deflink.he_cap.has_he) {
2299189b8d44SNaftali Goldstein 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2300046d2e7cSSriram R 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[6] &
2301514c3069SLuca Coelho 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2302091296d3SMiri Korenblit 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, sta,
2303cb63eb43SMiri Korenblit 							&sta_ctxt_cmd.pkt_ext,
2304cb63eb43SMiri Korenblit 							false);
2305514c3069SLuca Coelho 			flags |= STA_CTXT_HE_PACKET_EXT;
2306cb63eb43SMiri Korenblit 		/*
2307cb63eb43SMiri Korenblit 		* PPE Thresholds doesn't exist - set the API PPE values
2308cb63eb43SMiri Korenblit 		* according to Common Nominal Packet Padding field.
2309cb63eb43SMiri Korenblit 		*/
2310091296d3SMiri Korenblit 		} else {
2311cb63eb43SMiri Korenblit 			nominal_padding =
2312046d2e7cSSriram R 				u8_get_bits(sta->deflink.he_cap.he_cap_elem.phy_cap_info[9],
2313091296d3SMiri Korenblit 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
2314091296d3SMiri Korenblit 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
2315091296d3SMiri Korenblit 				iwl_mvm_set_pkt_ext_from_nominal_padding(&sta_ctxt_cmd.pkt_ext,
2316091296d3SMiri Korenblit 									 nominal_padding,
2317091296d3SMiri Korenblit 									 &flags);
2318514c3069SLuca Coelho 		}
2319cb63eb43SMiri Korenblit 	}
2320cb63eb43SMiri Korenblit 
2321cb63eb43SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2322cb63eb43SMiri Korenblit 		int bw;
2323cb63eb43SMiri Korenblit 
2324cb63eb43SMiri Korenblit 		for (bw = 0;
2325cb63eb43SMiri Korenblit 		     bw < ARRAY_SIZE(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i]);
2326cb63eb43SMiri Korenblit 		     bw++) {
2327cb63eb43SMiri Korenblit 			u8 *qam_th =
2328cb63eb43SMiri Korenblit 				&sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0];
2329cb63eb43SMiri Korenblit 
2330cb63eb43SMiri Korenblit 			IWL_DEBUG_HT(mvm,
2331cb63eb43SMiri Korenblit 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
2332cb63eb43SMiri Korenblit 				     i, bw, qam_th[0], qam_th[1]);
2333cb63eb43SMiri Korenblit 		}
2334cb63eb43SMiri Korenblit 	}
233573f23d91SShaul Triebitz 
2336046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
233773f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
233873f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
233973f23d91SShaul Triebitz 
2340046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
234173f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
234273f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
234373f23d91SShaul Triebitz 
2344514c3069SLuca Coelho 	rcu_read_unlock();
2345514c3069SLuca Coelho 
23469c4f15caSMiri Korenblit 	if (iwl_mvm_set_fw_mu_edca_params(mvm, mvmvif,
23479c4f15caSMiri Korenblit 					  &sta_ctxt_cmd.trig_based_txf[0]))
2348514c3069SLuca Coelho 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2349514c3069SLuca Coelho 
2350514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2351514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2352514c3069SLuca Coelho 
2353514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2354514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2355514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2356514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2357514c3069SLuca Coelho 	}
2358514c3069SLuca Coelho 
23599c4f15caSMiri Korenblit 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2360ee1a02d7SShaul Triebitz 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2361ee1a02d7SShaul Triebitz 
2362918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2363918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2364918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2365918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2366d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2367d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2368d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2369d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2370d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2371d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2372918cbf39SSara Sharon 	}
2373514c3069SLuca Coelho 
2374514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2375514c3069SLuca Coelho 
237642506dd2SJohannes Berg 	if (ver < 3) {
237742506dd2SJohannes Berg 		/* fields before pkt_ext */
237842506dd2SJohannes Berg 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
237942506dd2SJohannes Berg 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
238042506dd2SJohannes Berg 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
238142506dd2SJohannes Berg 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
238242506dd2SJohannes Berg 
238342506dd2SJohannes Berg 		/* pkt_ext */
238442506dd2SJohannes Berg 		for (i = 0;
238542506dd2SJohannes Berg 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
238642506dd2SJohannes Berg 		     i++) {
238742506dd2SJohannes Berg 			u8 bw;
238842506dd2SJohannes Berg 
238942506dd2SJohannes Berg 			for (bw = 0;
239042506dd2SJohannes Berg 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
239142506dd2SJohannes Berg 			     bw++) {
239242506dd2SJohannes Berg 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
239342506dd2SJohannes Berg 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
239442506dd2SJohannes Berg 
239542506dd2SJohannes Berg 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
239642506dd2SJohannes Berg 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
239742506dd2SJohannes Berg 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
239842506dd2SJohannes Berg 			}
239942506dd2SJohannes Berg 		}
240042506dd2SJohannes Berg 
240142506dd2SJohannes Berg 		/* fields after pkt_ext */
240242506dd2SJohannes Berg 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
240342506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
240442506dd2SJohannes Berg 			     sizeof(sta_ctxt_cmd_v2) -
240542506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
240642506dd2SJohannes Berg 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
240742506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
240842506dd2SJohannes Berg 		       (u8 *)&sta_ctxt_cmd +
240942506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
241042506dd2SJohannes Berg 		       sizeof(sta_ctxt_cmd) -
241142506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
241242506dd2SJohannes Berg 		sta_ctxt_cmd_v2.reserved3 = 0;
241342506dd2SJohannes Berg 	}
241442506dd2SJohannes Berg 
241542506dd2SJohannes Berg 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2416514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2417514c3069SLuca Coelho }
2418514c3069SLuca Coelho 
2419af84ac57SJohannes Berg static void iwl_mvm_protect_assoc(struct iwl_mvm *mvm,
2420af84ac57SJohannes Berg 				  struct ieee80211_vif *vif,
2421af84ac57SJohannes Berg 				  u32 duration_override)
2422af84ac57SJohannes Berg {
2423af84ac57SJohannes Berg 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2424af84ac57SJohannes Berg 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2425af84ac57SJohannes Berg 
2426af84ac57SJohannes Berg 	if (duration_override > duration)
2427af84ac57SJohannes Berg 		duration = duration_override;
2428af84ac57SJohannes Berg 
2429af84ac57SJohannes Berg 	/* Try really hard to protect the session and hear a beacon
2430af84ac57SJohannes Berg 	 * The new session protection command allows us to protect the
2431af84ac57SJohannes Berg 	 * session for a much longer time since the firmware will internally
2432af84ac57SJohannes Berg 	 * create two events: a 300TU one with a very high priority that
2433af84ac57SJohannes Berg 	 * won't be fragmented which should be enough for 99% of the cases,
2434af84ac57SJohannes Berg 	 * and another one (which we configure here to be 900TU long) which
2435af84ac57SJohannes Berg 	 * will have a slightly lower priority, but more importantly, can be
2436af84ac57SJohannes Berg 	 * fragmented so that it'll allow other activities to run.
2437af84ac57SJohannes Berg 	 */
2438af84ac57SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
2439af84ac57SJohannes Berg 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2440af84ac57SJohannes Berg 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2441af84ac57SJohannes Berg 						    min_duration, false);
2442af84ac57SJohannes Berg 	else
2443af84ac57SJohannes Berg 		iwl_mvm_protect_session(mvm, vif, duration,
2444af84ac57SJohannes Berg 					min_duration, 500, false);
2445af84ac57SJohannes Berg }
2446af84ac57SJohannes Berg 
2447e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2448e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2449e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
24507b7090b4SJohannes Berg 					     u64 changes)
2451e705c121SKalle Valo {
2452e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2453e705c121SKalle Valo 	int ret;
2454e705c121SKalle Valo 
2455e705c121SKalle Valo 	/*
2456cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend
2457cdaba917SEmmanuel Grumbach 	 * on the beacon interval, which was not known when the station
2458cdaba917SEmmanuel Grumbach 	 * interface was added.
2459e705c121SKalle Valo 	 */
2460f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2461cb63eb43SMiri Korenblit 		if ((vif->bss_conf.he_support &&
2462cb63eb43SMiri Korenblit 		     !iwlwifi_mod_params.disable_11ax) ||
2463cb63eb43SMiri Korenblit 		    (vif->bss_conf.eht_support &&
2464cb63eb43SMiri Korenblit 		     !iwlwifi_mod_params.disable_11be))
2465514c3069SLuca Coelho 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2466514c3069SLuca Coelho 
2467e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2468514c3069SLuca Coelho 	}
2469e705c121SKalle Valo 
247040ecdd01SShaul Triebitz 	/* Update MU EDCA params */
247140ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2472cb63eb43SMiri Korenblit 	    vif->cfg.assoc &&
2473cb63eb43SMiri Korenblit 	    ((vif->bss_conf.he_support &&
2474cb63eb43SMiri Korenblit 	      !iwlwifi_mod_params.disable_11ax) ||
2475cb63eb43SMiri Korenblit 	     (vif->bss_conf.eht_support &&
2476cb63eb43SMiri Korenblit 	      !iwlwifi_mod_params.disable_11be)))
247740ecdd01SShaul Triebitz 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
247840ecdd01SShaul Triebitz 
2479e705c121SKalle Valo 	/*
2480e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2481e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2482e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2483e705c121SKalle Valo 	 * branch for disassociation below.
2484e705c121SKalle Valo 	 */
2485e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2486e705c121SKalle Valo 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2487e705c121SKalle Valo 
2488e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
2489e705c121SKalle Valo 	if (ret)
2490e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2491e705c121SKalle Valo 
2492e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2493e705c121SKalle Valo 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2494f276e20bSJohannes Berg 	mvmvif->associated = vif->cfg.assoc;
2495e705c121SKalle Valo 
2496e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2497f276e20bSJohannes Berg 		if (vif->cfg.assoc) {
2498e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2499e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2500e705c121SKalle Valo 			memset(&mvmvif->beacon_stats, 0,
2501e705c121SKalle Valo 			       sizeof(mvmvif->beacon_stats));
2502e705c121SKalle Valo 
2503e705c121SKalle Valo 			/* add quota for this interface */
2504e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2505e705c121SKalle Valo 			if (ret) {
2506e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2507e705c121SKalle Valo 				return;
2508e705c121SKalle Valo 			}
2509e705c121SKalle Valo 
2510e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2511fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2512fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2513fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2514e705c121SKalle Valo 				/*
2515e705c121SKalle Valo 				 * If we're restarting then the firmware will
2516e705c121SKalle Valo 				 * obviously have lost synchronisation with
2517e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2518e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2519e705c121SKalle Valo 				 * scheduling a session protection time event.
2520e705c121SKalle Valo 				 *
2521e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2522e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2523e705c121SKalle Valo 				 * the beacon interval.
2524e705c121SKalle Valo 				 *
2525e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2526e705c121SKalle Valo 				 * than a single interface.
2527fe959c7bSEmmanuel Grumbach 				 *
2528fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2529fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2530fe959c7bSEmmanuel Grumbach 				 * only anyway.
2531e705c121SKalle Valo 				 */
2532e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2533e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2534e705c121SKalle Valo 							5 * dur, false);
2535af84ac57SJohannes Berg 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2536af84ac57SJohannes Berg 					     &mvm->status) &&
2537af84ac57SJohannes Berg 				   !vif->bss_conf.dtim_period) {
2538af84ac57SJohannes Berg 				/*
2539af84ac57SJohannes Berg 				 * If we're not restarting and still haven't
2540af84ac57SJohannes Berg 				 * heard a beacon (dtim period unknown) then
2541af84ac57SJohannes Berg 				 * make sure we still have enough minimum time
2542af84ac57SJohannes Berg 				 * remaining in the time event, since the auth
2543af84ac57SJohannes Berg 				 * might actually have taken quite a while
2544af84ac57SJohannes Berg 				 * (especially for SAE) and so the remaining
2545af84ac57SJohannes Berg 				 * time could be small without us having heard
2546af84ac57SJohannes Berg 				 * a beacon yet.
2547af84ac57SJohannes Berg 				 */
2548af84ac57SJohannes Berg 				iwl_mvm_protect_assoc(mvm, vif, 0);
2549e705c121SKalle Valo 			}
2550e705c121SKalle Valo 
2551e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2552e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2553e705c121SKalle Valo 			if (vif->p2p) {
2554e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2555e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
2556e705c121SKalle Valo 						    IEEE80211_SMPS_DYNAMIC);
2557e705c121SKalle Valo 			}
25580ae98812SSara Sharon 		} else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
25596d19a5ebSEmmanuel Grumbach 			iwl_mvm_mei_host_disassociated(mvm);
2560e705c121SKalle Valo 			/*
2561e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2562e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2563e705c121SKalle Valo 			 */
256469e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
256569e508b4SIlan Peer 			WARN_ONCE(ret &&
256669e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
256769e508b4SIlan Peer 					    &mvm->status),
2568e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2569e705c121SKalle Valo 
25706b28f978SEmmanuel Grumbach 			/*
25716b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
25726b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
25736b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
25746b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
25756b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
25766b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
25776b28f978SEmmanuel Grumbach 			 */
25786b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
25796b28f978SEmmanuel Grumbach 				      &mvm->status)) {
25805c75a208SJohannes Berg 				/* first remove remaining keys */
25815c75a208SJohannes Berg 				iwl_mvm_sec_key_remove_ap(mvm, vif);
25825c75a208SJohannes Berg 
25836b28f978SEmmanuel Grumbach 				/*
25846b28f978SEmmanuel Grumbach 				 * Remove AP station now that
25856b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
25866b28f978SEmmanuel Grumbach 				 */
25876b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
25886b28f978SEmmanuel Grumbach 							mvmvif->ap_sta_id);
2589e705c121SKalle Valo 				if (ret)
25906b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
25916b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2592e705c121SKalle Valo 
25930ae98812SSara Sharon 				mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
25946b28f978SEmmanuel Grumbach 			}
25956b28f978SEmmanuel Grumbach 
2596e705c121SKalle Valo 			/* remove quota for this interface */
2597e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2598e705c121SKalle Valo 			if (ret)
2599e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2600e705c121SKalle Valo 
2601e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2602e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2603e705c121SKalle Valo 			if (ret)
2604e705c121SKalle Valo 				IWL_ERR(mvm,
2605e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2606e705c121SKalle Valo 					vif->addr);
2607e705c121SKalle Valo 		}
2608e705c121SKalle Valo 
2609a07a8f37SSara Sharon 		/*
2610a07a8f37SSara Sharon 		 * The firmware tracks the MU-MIMO group on its own.
2611f92659a1SSara Sharon 		 * However, on HW restart we should restore this data.
2612a07a8f37SSara Sharon 		 */
2613a07a8f37SSara Sharon 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2614d0a9123eSJohannes Berg 		    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2615a07a8f37SSara Sharon 			ret = iwl_mvm_update_mu_groups(mvm, vif);
2616a07a8f37SSara Sharon 			if (ret)
2617a07a8f37SSara Sharon 				IWL_ERR(mvm,
2618a07a8f37SSara Sharon 					"failed to update VHT MU_MIMO groups\n");
2619a07a8f37SSara Sharon 		}
2620a07a8f37SSara Sharon 
2621e705c121SKalle Valo 		iwl_mvm_recalc_multicast(mvm);
2622e705c121SKalle Valo 
2623e705c121SKalle Valo 		/* reset rssi values */
2624e705c121SKalle Valo 		mvmvif->bf_data.ave_beacon_signal = 0;
2625e705c121SKalle Valo 
2626e705c121SKalle Valo 		iwl_mvm_bt_coex_vif_change(mvm);
2627e705c121SKalle Valo 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2628e705c121SKalle Valo 				    IEEE80211_SMPS_AUTOMATIC);
2629355346baSAvraham Stern 		if (fw_has_capa(&mvm->fw->ucode_capa,
2630355346baSAvraham Stern 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2631355346baSAvraham Stern 			iwl_mvm_config_scan(mvm);
2632b45242c9SAvraham Stern 	}
2633b45242c9SAvraham Stern 
2634b45242c9SAvraham Stern 	if (changes & BSS_CHANGED_BEACON_INFO) {
2635e705c121SKalle Valo 		/*
2636b45242c9SAvraham Stern 		 * We received a beacon from the associated AP so
2637e705c121SKalle Valo 		 * remove the session protection.
2638e705c121SKalle Valo 		 */
26393edfb5f4SAvraham Stern 		iwl_mvm_stop_session_protection(mvm, vif);
2640e705c121SKalle Valo 
2641e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2642e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2643e705c121SKalle Valo 	}
2644e705c121SKalle Valo 
2645283115fbSAvri Altman 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2646283115fbSAvri Altman 		       /*
2647283115fbSAvri Altman 			* Send power command on every beacon change,
2648283115fbSAvri Altman 			* because we may have not enabled beacon abort yet.
2649283115fbSAvri Altman 			*/
2650283115fbSAvri Altman 		       BSS_CHANGED_BEACON_INFO)) {
2651e705c121SKalle Valo 		ret = iwl_mvm_power_update_mac(mvm);
2652e705c121SKalle Valo 		if (ret)
2653e705c121SKalle Valo 			IWL_ERR(mvm, "failed to update power mode\n");
2654e705c121SKalle Valo 	}
2655e705c121SKalle Valo 
2656e705c121SKalle Valo 	if (changes & BSS_CHANGED_CQM) {
2657e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2658e705c121SKalle Valo 		/* reset cqm events tracking */
2659e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = 0;
2660e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
2661e705c121SKalle Valo 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2662e705c121SKalle Valo 			if (ret)
2663e705c121SKalle Valo 				IWL_ERR(mvm,
2664e705c121SKalle Valo 					"failed to update CQM thresholds\n");
2665e705c121SKalle Valo 		}
2666e705c121SKalle Valo 	}
2667e705c121SKalle Valo 
2668de34d1c1SJohannes Berg 	if (changes & BSS_CHANGED_BANDWIDTH)
2669de34d1c1SJohannes Berg 		iwl_mvm_apply_fw_smps_request(vif);
2670e705c121SKalle Valo }
2671e705c121SKalle Valo 
2672e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2673ae7ba17bSShaul Triebitz 				 struct ieee80211_vif *vif,
2674b327c84cSGregory Greenman 				 struct ieee80211_bss_conf *link_conf)
2675e705c121SKalle Valo {
2676e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2677e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2678c56e00a3SJohannes Berg 	int ret, i;
2679e705c121SKalle Valo 
2680e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2681e705c121SKalle Valo 
2682e705c121SKalle Valo 	/* Send the beacon template */
2683e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2684e705c121SKalle Valo 	if (ret)
2685e705c121SKalle Valo 		goto out_unlock;
2686e705c121SKalle Valo 
2687e705c121SKalle Valo 	/*
2688cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2689cdaba917SEmmanuel Grumbach 	 * the beacon interval, which was not known when the AP interface
2690cdaba917SEmmanuel Grumbach 	 * was added.
2691e705c121SKalle Valo 	 */
2692e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2693e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2694e705c121SKalle Valo 
2695e705c121SKalle Valo 	mvmvif->ap_assoc_sta_count = 0;
2696e705c121SKalle Valo 
2697e705c121SKalle Valo 	/* Add the mac context */
2698e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2699e705c121SKalle Valo 	if (ret)
2700e705c121SKalle Valo 		goto out_unlock;
2701e705c121SKalle Valo 
2702e705c121SKalle Valo 	/* Perform the binding */
2703e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2704e705c121SKalle Valo 	if (ret)
2705e705c121SKalle Valo 		goto out_remove;
2706e705c121SKalle Valo 
270763dd5d02SSara Sharon 	/*
270863dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
270963dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
271063dd5d02SSara Sharon 	 * cause assert 0x2b00.
271163dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
271263dd5d02SSara Sharon 	 * the TLV
271363dd5d02SSara Sharon 	 */
271463dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2715ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2716ced19f26SSara Sharon 		if (ret)
2717ced19f26SSara Sharon 			goto out_unbind;
271863dd5d02SSara Sharon 		/*
271963dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
272063dd5d02SSara Sharon 		 * events are added and applied to the scheduler
272163dd5d02SSara Sharon 		 */
2722e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
272363dd5d02SSara Sharon 		if (ret) {
272463dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
272563dd5d02SSara Sharon 			goto out_unbind;
272663dd5d02SSara Sharon 		}
272763dd5d02SSara Sharon 	} else {
272863dd5d02SSara Sharon 		/*
272963dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
273063dd5d02SSara Sharon 		 * events are added and applied to the scheduler
273163dd5d02SSara Sharon 		 */
273275fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2733e705c121SKalle Valo 		if (ret)
273463dd5d02SSara Sharon 			goto out_unbind;
273575fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
273663dd5d02SSara Sharon 		if (ret) {
273763dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
273863dd5d02SSara Sharon 			goto out_unbind;
273963dd5d02SSara Sharon 		}
274063dd5d02SSara Sharon 	}
274126d6c16bSSara Sharon 
2742e705c121SKalle Valo 	/* must be set before quota calculations */
2743e705c121SKalle Valo 	mvmvif->ap_ibss_active = true;
2744e705c121SKalle Valo 
2745c56e00a3SJohannes Berg 	/* send all the early keys to the device now */
2746c56e00a3SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2747c56e00a3SJohannes Berg 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2748c56e00a3SJohannes Berg 
2749c56e00a3SJohannes Berg 		if (!key)
2750c56e00a3SJohannes Berg 			continue;
2751c56e00a3SJohannes Berg 
2752c56e00a3SJohannes Berg 		mvmvif->ap_early_keys[i] = NULL;
2753c56e00a3SJohannes Berg 
27546569e7d3SJohannes Berg 		ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2755c56e00a3SJohannes Berg 		if (ret)
2756c56e00a3SJohannes Berg 			goto out_quota_failed;
2757c56e00a3SJohannes Berg 	}
2758c56e00a3SJohannes Berg 
275947242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
276047242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, true,
276147242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
276247242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
276347242744STova Mussai 	}
276447242744STova Mussai 
2765e705c121SKalle Valo 	/* power updated needs to be done before quotas */
2766e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2767e705c121SKalle Valo 
2768e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2769e705c121SKalle Valo 	if (ret)
2770e705c121SKalle Valo 		goto out_quota_failed;
2771e705c121SKalle Valo 
2772e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2773e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2774e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2775e705c121SKalle Valo 
2776e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2777e705c121SKalle Valo 
2778e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2779e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2780e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2781e705c121SKalle Valo 
2782b73f9a4aSJohannes Berg 	iwl_mvm_ftm_restart_responder(mvm, vif);
2783b73f9a4aSJohannes Berg 
2784e705c121SKalle Valo 	goto out_unlock;
2785e705c121SKalle Valo 
2786e705c121SKalle Valo out_quota_failed:
2787e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2788e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2789e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2790ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2791e705c121SKalle Valo out_unbind:
2792e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2793e705c121SKalle Valo out_remove:
2794e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2795e705c121SKalle Valo out_unlock:
2796e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2797e705c121SKalle Valo 	return ret;
2798e705c121SKalle Valo }
2799e705c121SKalle Valo 
2800ae7ba17bSShaul Triebitz static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
2801ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
2802b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
2803ae7ba17bSShaul Triebitz {
2804b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
2805ae7ba17bSShaul Triebitz }
2806ae7ba17bSShaul Triebitz 
2807ae7ba17bSShaul Triebitz static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
2808e705c121SKalle Valo 			      struct ieee80211_vif *vif)
2809e705c121SKalle Valo {
2810b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
2811ae7ba17bSShaul Triebitz }
2812ae7ba17bSShaul Triebitz 
2813ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2814ae7ba17bSShaul Triebitz 				 struct ieee80211_vif *vif,
2815b327c84cSGregory Greenman 				 struct ieee80211_bss_conf *link_conf)
2816ae7ba17bSShaul Triebitz {
2817e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2818e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2819e705c121SKalle Valo 
2820e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
2821e705c121SKalle Valo 
2822e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2823e705c121SKalle Valo 
2824e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2825e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2826e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2827e705c121SKalle Valo 					  &mvmvif->time_event_data);
2828e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2829e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2830e705c121SKalle Valo 	}
2831e705c121SKalle Valo 
2832e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2833e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2834e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2835e705c121SKalle Valo 	}
2836e705c121SKalle Valo 
2837e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2838e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2839e705c121SKalle Valo 
284047242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
284147242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
284247242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
284347242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
284447242744STova Mussai 	}
284547242744STova Mussai 
2846e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2847e705c121SKalle Valo 
2848e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2849e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2850e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2851e705c121SKalle Valo 
2852e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2853ced19f26SSara Sharon 
2854be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
2855be82ecd3SAvraham Stern 
2856ced19f26SSara Sharon 	/*
2857ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2858ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2859ced19f26SSara Sharon 	 * cause assert 0x2b00.
2860ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2861ced19f26SSara Sharon 	 * bcast station).
2862ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2863ced19f26SSara Sharon 	 */
2864ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
286526d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2866e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2867ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2868ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2869e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2870e705c121SKalle Valo 
2871e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2872e705c121SKalle Valo 
2873e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2874e705c121SKalle Valo 
2875e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2876e705c121SKalle Valo }
2877e705c121SKalle Valo 
2878ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
2879ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
2880b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
2881ae7ba17bSShaul Triebitz {
2882b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
2883ae7ba17bSShaul Triebitz }
2884ae7ba17bSShaul Triebitz 
2885ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
2886ae7ba17bSShaul Triebitz 			      struct ieee80211_vif *vif)
2887ae7ba17bSShaul Triebitz {
2888b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
2889ae7ba17bSShaul Triebitz }
2890ae7ba17bSShaul Triebitz 
2891e705c121SKalle Valo static void
2892e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2893e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2894e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
28957b7090b4SJohannes Berg 				 u64 changes)
2896e705c121SKalle Valo {
2897e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2898e705c121SKalle Valo 
2899e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
2900e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
2901e705c121SKalle Valo 		return;
2902e705c121SKalle Valo 
2903e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2904e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2905e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2906e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2907e705c121SKalle Valo 
2908e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
2909e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
2910e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2911e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
2912e705c121SKalle Valo 
2913b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
2914b73f9a4aSJohannes Berg 		int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2915b73f9a4aSJohannes Berg 
2916b73f9a4aSJohannes Berg 		if (ret)
2917b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2918b73f9a4aSJohannes Berg 				 ret);
2919b73f9a4aSJohannes Berg 	}
2920b73f9a4aSJohannes Berg 
2921e705c121SKalle Valo }
2922e705c121SKalle Valo 
2923e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2924e705c121SKalle Valo 				     struct ieee80211_vif *vif,
2925e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
29267b7090b4SJohannes Berg 				     u64 changes)
2927e705c121SKalle Valo {
2928e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2929e705c121SKalle Valo 
2930e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2931e705c121SKalle Valo 
2932f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
2933e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2934e705c121SKalle Valo 
2935e705c121SKalle Valo 	switch (vif->type) {
2936e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2937e705c121SKalle Valo 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2938e705c121SKalle Valo 		break;
2939e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
2940e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
2941e705c121SKalle Valo 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2942e705c121SKalle Valo 		break;
294391b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
294491b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
294591b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
294691b08c2dSAviya Erenfeld 		break;
2947e705c121SKalle Valo 	default:
2948e705c121SKalle Valo 		/* shouldn't happen */
2949e705c121SKalle Valo 		WARN_ON_ONCE(1);
2950e705c121SKalle Valo 	}
2951e705c121SKalle Valo 
29529aae43a4SJohannes Berg 	if (changes & BSS_CHANGED_TXPOWER) {
29539aae43a4SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
29549aae43a4SJohannes Berg 				bss_conf->txpower);
29559aae43a4SJohannes Berg 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
29569aae43a4SJohannes Berg 	}
29579aae43a4SJohannes Berg 
2958e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2959e705c121SKalle Valo }
2960e705c121SKalle Valo 
2961e705c121SKalle Valo static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2962e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2963e705c121SKalle Valo 			       struct ieee80211_scan_request *hw_req)
2964e705c121SKalle Valo {
2965e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2966e705c121SKalle Valo 	int ret;
2967e705c121SKalle Valo 
2968e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
2969e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2970e705c121SKalle Valo 		return -EINVAL;
2971e705c121SKalle Valo 
2972e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2973e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2974e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2975e705c121SKalle Valo 
2976e705c121SKalle Valo 	return ret;
2977e705c121SKalle Valo }
2978e705c121SKalle Valo 
2979e705c121SKalle Valo static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2980e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2981e705c121SKalle Valo {
2982e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2983e705c121SKalle Valo 
2984e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2985e705c121SKalle Valo 
2986e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2987e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
2988e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
2989e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
2990e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
2991e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
2992e705c121SKalle Valo 	*/
2993e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2994e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2995e705c121SKalle Valo 
2996e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2997e705c121SKalle Valo }
2998e705c121SKalle Valo 
2999e705c121SKalle Valo static void
3000e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3001e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
3002e705c121SKalle Valo 				  int num_frames,
3003e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
3004e705c121SKalle Valo 				  bool more_data)
3005e705c121SKalle Valo {
3006e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3007e705c121SKalle Valo 
3008e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3009e705c121SKalle Valo 
3010e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3011e705c121SKalle Valo 					  tids, more_data, false);
3012e705c121SKalle Valo }
3013e705c121SKalle Valo 
3014e705c121SKalle Valo static void
3015e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3016e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
3017e705c121SKalle Valo 				    int num_frames,
3018e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
3019e705c121SKalle Valo 				    bool more_data)
3020e705c121SKalle Valo {
3021e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3022e705c121SKalle Valo 
30239a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3024e705c121SKalle Valo 
3025e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3026e705c121SKalle Valo 					  tids, more_data, true);
3027e705c121SKalle Valo }
3028e705c121SKalle Valo 
302965e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3030e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
3031e705c121SKalle Valo 				     struct ieee80211_sta *sta)
3032e705c121SKalle Valo {
3033e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3034e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3035e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
3036e705c121SKalle Valo 	int tid;
3037e705c121SKalle Valo 
3038960f864bSJohannes Berg 	/*
3039960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
3040960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
3041960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
3042960f864bSJohannes Berg 	 */
3043960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3044960f864bSJohannes Berg 		return;
3045960f864bSJohannes Berg 
3046e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3047311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3048e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3049e705c121SKalle Valo 
30506862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
30511c17627bSSara Sharon 			continue;
30521c17627bSSara Sharon 
3053e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
3054e705c121SKalle Valo 
3055dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3056e705c121SKalle Valo 			continue;
3057e705c121SKalle Valo 
3058e705c121SKalle Valo 		__set_bit(tid, &tids);
3059e705c121SKalle Valo 	}
3060e705c121SKalle Valo 
3061e705c121SKalle Valo 	switch (cmd) {
3062e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
3063e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3064e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
3065e705c121SKalle Valo 
3066e705c121SKalle Valo 		if (txqs)
3067e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3068e705c121SKalle Valo 		/*
3069e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3070e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
3071e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3072e705c121SKalle Valo 		 */
3073e705c121SKalle Valo 		break;
3074e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
30750ae98812SSara Sharon 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
3076e705c121SKalle Valo 			break;
3077e705c121SKalle Valo 
3078e705c121SKalle Valo 		if (txqs)
3079e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3080e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3081e705c121SKalle Valo 		break;
3082e705c121SKalle Valo 	default:
3083e705c121SKalle Valo 		break;
3084e705c121SKalle Valo 	}
3085e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3086e705c121SKalle Valo }
3087e705c121SKalle Valo 
308865e25482SJohannes Berg static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
308965e25482SJohannes Berg 				   struct ieee80211_vif *vif,
309065e25482SJohannes Berg 				   enum sta_notify_cmd cmd,
309165e25482SJohannes Berg 				   struct ieee80211_sta *sta)
309265e25482SJohannes Berg {
309365e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
309465e25482SJohannes Berg }
309565e25482SJohannes Berg 
309665e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
309765e25482SJohannes Berg {
309865e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
309965e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
310065e25482SJohannes Berg 	struct ieee80211_sta *sta;
310165e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
310265e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
310365e25482SJohannes Berg 
3104be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
310565e25482SJohannes Berg 		return;
310665e25482SJohannes Berg 
310765e25482SJohannes Berg 	rcu_read_lock();
3108a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
310965e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
311065e25482SJohannes Berg 		rcu_read_unlock();
311165e25482SJohannes Berg 		return;
311265e25482SJohannes Berg 	}
311365e25482SJohannes Berg 
311465e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
311565e25482SJohannes Berg 
311665e25482SJohannes Berg 	if (!mvmsta->vif ||
311765e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
311865e25482SJohannes Berg 		rcu_read_unlock();
311965e25482SJohannes Berg 		return;
312065e25482SJohannes Berg 	}
312165e25482SJohannes Berg 
312265e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
312365e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
312465e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
312565e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
312665e25482SJohannes Berg 			sta);
312765e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
312865e25482SJohannes Berg 	}
312965e25482SJohannes Berg 
313065e25482SJohannes Berg 	if (sleeping) {
313165e25482SJohannes Berg 		switch (notif->type) {
313265e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
313365e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
313465e25482SJohannes Berg 			break;
313565e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
313665e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
313765e25482SJohannes Berg 			break;
313865e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
313965e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
314065e25482SJohannes Berg 			break;
314165e25482SJohannes Berg 		default:
314265e25482SJohannes Berg 			break;
314365e25482SJohannes Berg 		}
314465e25482SJohannes Berg 	}
314565e25482SJohannes Berg 
314665e25482SJohannes Berg 	rcu_read_unlock();
314765e25482SJohannes Berg }
314865e25482SJohannes Berg 
3149e705c121SKalle Valo static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3150e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3151e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3152e705c121SKalle Valo {
3153e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3154e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3155e705c121SKalle Valo 
3156e705c121SKalle Valo 	/*
3157e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
3158e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
3159e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
3160e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
3161e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
3162e705c121SKalle Valo 	 * callback deleted the station.
3163e705c121SKalle Valo 	 */
3164e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3165e705c121SKalle Valo 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
3166e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
3167e705c121SKalle Valo 				   ERR_PTR(-ENOENT));
3168e705c121SKalle Valo 
3169e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3170e705c121SKalle Valo }
3171e705c121SKalle Valo 
3172e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3173e705c121SKalle Valo 				const u8 *bssid)
3174e705c121SKalle Valo {
3175b0ffe455SJohannes Berg 	int i;
3176b0ffe455SJohannes Berg 
3177b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3178b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
3179b0ffe455SJohannes Berg 
3180b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3181b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3182b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
3183b0ffe455SJohannes Berg 	}
3184b0ffe455SJohannes Berg 
3185e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3186e705c121SKalle Valo 		return;
3187e705c121SKalle Valo 
3188c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3189cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3190cee5a882SAvri Altman 		return;
3191cee5a882SAvri Altman 	}
3192cee5a882SAvri Altman 
319311dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
319411dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3195e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3196e705c121SKalle Valo 		return;
3197e705c121SKalle Valo 	}
3198e705c121SKalle Valo 
3199b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3200b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3201b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3202b0ffe455SJohannes Berg 			return;
3203b0ffe455SJohannes Berg 		}
3204b0ffe455SJohannes Berg 	}
3205b0ffe455SJohannes Berg 
3206e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3207e705c121SKalle Valo }
3208e705c121SKalle Valo 
32091e8f1329SGolan Ben-Ami static void
32101e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
32111e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
32121e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
32131e8f1329SGolan Ben-Ami {
32141e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
32151e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
32161e8f1329SGolan Ben-Ami 
32176c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
32186c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
32196c042d75SSara Sharon 	if (!trig)
32201e8f1329SGolan Ben-Ami 		return;
32211e8f1329SGolan Ben-Ami 
32221e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
32231e8f1329SGolan Ben-Ami 
32241e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
32251e8f1329SGolan Ben-Ami 		return;
32261e8f1329SGolan Ben-Ami 
32271e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
32281e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
32291e8f1329SGolan Ben-Ami 		return;
32301e8f1329SGolan Ben-Ami 
32317174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
32321e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
32331e8f1329SGolan Ben-Ami 				peer_addr, action);
32341e8f1329SGolan Ben-Ami }
32351e8f1329SGolan Ben-Ami 
32364f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
32374f58121dSIlan Peer 	bool tolerated;
32384f58121dSIlan Peer };
32394f58121dSIlan Peer 
32404f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
32414f58121dSIlan Peer 						    struct cfg80211_bss *bss,
32424f58121dSIlan Peer 						    void *_data)
32434f58121dSIlan Peer {
32444f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
32456c608cd6SJohannes Berg 	const struct cfg80211_bss_ies *ies;
32464f58121dSIlan Peer 	const struct element *elem;
32474f58121dSIlan Peer 
32486c608cd6SJohannes Berg 	rcu_read_lock();
32496c608cd6SJohannes Berg 	ies = rcu_dereference(bss->ies);
32506c608cd6SJohannes Berg 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
32516c608cd6SJohannes Berg 				  ies->len);
32524f58121dSIlan Peer 
32534f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
32544f58121dSIlan Peer 	    !(elem->data[10] &
32554f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
32564f58121dSIlan Peer 		data->tolerated = false;
32574f58121dSIlan Peer 	}
32586c608cd6SJohannes Berg 	rcu_read_unlock();
32594f58121dSIlan Peer }
32604f58121dSIlan Peer 
32614f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
32624f58121dSIlan Peer 					       struct ieee80211_vif *vif)
32634f58121dSIlan Peer {
32644f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
32654f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
32664f58121dSIlan Peer 		.tolerated = true,
32674f58121dSIlan Peer 	};
32684f58121dSIlan Peer 
32694f58121dSIlan Peer 	if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
32704f58121dSIlan Peer 		mvmvif->he_ru_2mhz_block = false;
32714f58121dSIlan Peer 		return;
32724f58121dSIlan Peer 	}
32734f58121dSIlan Peer 
32744f58121dSIlan Peer 	cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
32754f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
32764f58121dSIlan Peer 			  &iter_data);
32774f58121dSIlan Peer 
32784f58121dSIlan Peer 	/*
32794f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
32804f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
32814f58121dSIlan Peer 	 */
32824f58121dSIlan Peer 	mvmvif->he_ru_2mhz_block = !iter_data.tolerated;
32834f58121dSIlan Peer }
32844f58121dSIlan Peer 
3285f7d6ef33SJohannes Berg static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3286f7d6ef33SJohannes Berg 					       struct ieee80211_vif *vif)
3287f7d6ef33SJohannes Berg {
3288f7d6ef33SJohannes Berg 	struct ieee80211_supported_band *sband;
3289f7d6ef33SJohannes Berg 	const struct ieee80211_sta_he_cap *he_cap;
3290f7d6ef33SJohannes Berg 
3291f7d6ef33SJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION)
3292f7d6ef33SJohannes Berg 		return;
3293f7d6ef33SJohannes Berg 
3294f7d6ef33SJohannes Berg 	if (!mvm->cca_40mhz_workaround)
3295f7d6ef33SJohannes Berg 		return;
3296f7d6ef33SJohannes Berg 
3297f7d6ef33SJohannes Berg 	/* decrement and check that we reached zero */
3298f7d6ef33SJohannes Berg 	mvm->cca_40mhz_workaround--;
3299f7d6ef33SJohannes Berg 	if (mvm->cca_40mhz_workaround)
3300f7d6ef33SJohannes Berg 		return;
3301f7d6ef33SJohannes Berg 
3302f7d6ef33SJohannes Berg 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3303f7d6ef33SJohannes Berg 
3304f7d6ef33SJohannes Berg 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3305f7d6ef33SJohannes Berg 
3306f7d6ef33SJohannes Berg 	he_cap = ieee80211_get_he_iftype_cap(sband,
3307f7d6ef33SJohannes Berg 					     ieee80211_vif_type_p2p(vif));
3308f7d6ef33SJohannes Berg 
3309f7d6ef33SJohannes Berg 	if (he_cap) {
3310f7d6ef33SJohannes Berg 		/* we know that ours is writable */
33110301bcd5SBjoern A. Zeeb 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3312f7d6ef33SJohannes Berg 
3313f7d6ef33SJohannes Berg 		he->he_cap_elem.phy_cap_info[0] |=
3314f7d6ef33SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3315f7d6ef33SJohannes Berg 	}
3316f7d6ef33SJohannes Berg }
3317f7d6ef33SJohannes Berg 
33186d19a5ebSEmmanuel Grumbach static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
33196d19a5ebSEmmanuel Grumbach 					struct ieee80211_vif *vif,
33206d19a5ebSEmmanuel Grumbach 					struct iwl_mvm_sta *mvm_sta)
33216d19a5ebSEmmanuel Grumbach {
33226d19a5ebSEmmanuel Grumbach #if IS_ENABLED(CONFIG_IWLMEI)
33236d19a5ebSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
33246d19a5ebSEmmanuel Grumbach 	struct iwl_mei_conn_info conn_info = {
3325f276e20bSJohannes Berg 		.ssid_len = vif->cfg.ssid_len,
33266d19a5ebSEmmanuel Grumbach 		.channel = vif->bss_conf.chandef.chan->hw_value,
33276d19a5ebSEmmanuel Grumbach 	};
33286d19a5ebSEmmanuel Grumbach 
33296d19a5ebSEmmanuel Grumbach 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
33306d19a5ebSEmmanuel Grumbach 		return;
33316d19a5ebSEmmanuel Grumbach 
33326d19a5ebSEmmanuel Grumbach 	if (!mvm->mei_registered)
33336d19a5ebSEmmanuel Grumbach 		return;
33346d19a5ebSEmmanuel Grumbach 
33356d19a5ebSEmmanuel Grumbach 	switch (mvm_sta->pairwise_cipher) {
3336e5d3a64eSAvraham Stern 	case WLAN_CIPHER_SUITE_TKIP:
3337e5d3a64eSAvraham Stern 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3338e5d3a64eSAvraham Stern 		break;
33396d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_CCMP:
33406d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
33416d19a5ebSEmmanuel Grumbach 		break;
33426d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP:
33436d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
33446d19a5ebSEmmanuel Grumbach 		break;
33456d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP_256:
33466d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
33476d19a5ebSEmmanuel Grumbach 		break;
33486d19a5ebSEmmanuel Grumbach 	case 0:
33496d19a5ebSEmmanuel Grumbach 		/* open profile */
33506d19a5ebSEmmanuel Grumbach 		break;
33516d19a5ebSEmmanuel Grumbach 	default:
33526d19a5ebSEmmanuel Grumbach 		/* cipher not supported, don't send anything to iwlmei */
33536d19a5ebSEmmanuel Grumbach 		return;
33546d19a5ebSEmmanuel Grumbach 	}
33556d19a5ebSEmmanuel Grumbach 
33566d19a5ebSEmmanuel Grumbach 	switch (mvmvif->rekey_data.akm) {
33576d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_SAE & 0xff:
33586d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
33596d19a5ebSEmmanuel Grumbach 		break;
33606d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_PSK & 0xff:
33616d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
33626d19a5ebSEmmanuel Grumbach 		break;
33636d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_8021X & 0xff:
33646d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
33656d19a5ebSEmmanuel Grumbach 		break;
33666d19a5ebSEmmanuel Grumbach 	case 0:
33676d19a5ebSEmmanuel Grumbach 		/* open profile */
33686d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
33696d19a5ebSEmmanuel Grumbach 		break;
33706d19a5ebSEmmanuel Grumbach 	default:
33716d19a5ebSEmmanuel Grumbach 		/* auth method / AKM not supported */
33726d19a5ebSEmmanuel Grumbach 		/* TODO: All the FT vesions of these? */
33736d19a5ebSEmmanuel Grumbach 		return;
33746d19a5ebSEmmanuel Grumbach 	}
33756d19a5ebSEmmanuel Grumbach 
3376f276e20bSJohannes Berg 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
33776d19a5ebSEmmanuel Grumbach 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
33786d19a5ebSEmmanuel Grumbach 
33796d19a5ebSEmmanuel Grumbach 	/* TODO: add support for collocated AP data */
33806d19a5ebSEmmanuel Grumbach 	iwl_mei_host_associated(&conn_info, NULL);
33816d19a5ebSEmmanuel Grumbach #endif
33826d19a5ebSEmmanuel Grumbach }
33836d19a5ebSEmmanuel Grumbach 
3384e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3385e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3386e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3387e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3388e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3389e705c121SKalle Valo {
3390e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3391e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
33926ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3393e705c121SKalle Valo 	int ret;
3394e705c121SKalle Valo 
3395e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3396e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3397e705c121SKalle Valo 
3398e705c121SKalle Valo 	/* this would be a mac80211 bug ... but don't crash */
3399e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
3400fe56d05eSSara Sharon 		return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) ? 0 : -EINVAL;
3401e705c121SKalle Valo 
340224afba76SLiad Kaufman 	/*
340324afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
340424afba76SLiad Kaufman 	 *
340524afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
340624afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
340724afba76SLiad Kaufman 	 * the queues.
340824afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
340924afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
341024afba76SLiad Kaufman 	 * of.
341124afba76SLiad Kaufman 	 *
341224afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
341324afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
341424afba76SLiad Kaufman 	 */
341524afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3416c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
341724afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
341824afba76SLiad Kaufman 
341924afba76SLiad Kaufman 		/*
342024afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
342124afba76SLiad Kaufman 		 * worker will already remove it if it was on
342224afba76SLiad Kaufman 		 */
3423f7d6ef33SJohannes Berg 
3424f7d6ef33SJohannes Berg 		/*
3425f7d6ef33SJohannes Berg 		 * Additionally, reset the 40 MHz capability if we disconnected
3426f7d6ef33SJohannes Berg 		 * from the AP now.
3427f7d6ef33SJohannes Berg 		 */
3428f7d6ef33SJohannes Berg 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
342924afba76SLiad Kaufman 	}
343024afba76SLiad Kaufman 
3431e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
34326ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3433d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
34346ea29ce5SJohannes Berg 
3435e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3436e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
3437e705c121SKalle Valo 		/*
3438e705c121SKalle Valo 		 * Firmware bug - it'll crash if the beacon interval is less
3439e705c121SKalle Valo 		 * than 16. We can't avoid connecting at all, so refuse the
3440e705c121SKalle Valo 		 * station state change, this will cause mac80211 to abandon
3441e705c121SKalle Valo 		 * attempts to connect to this AP, and eventually wpa_s will
3442cdaba917SEmmanuel Grumbach 		 * blocklist the AP...
3443e705c121SKalle Valo 		 */
3444e705c121SKalle Valo 		if (vif->type == NL80211_IFTYPE_STATION &&
3445e705c121SKalle Valo 		    vif->bss_conf.beacon_int < 16) {
3446e705c121SKalle Valo 			IWL_ERR(mvm,
3447e705c121SKalle Valo 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3448e705c121SKalle Valo 				sta->addr, vif->bss_conf.beacon_int);
3449e705c121SKalle Valo 			ret = -EINVAL;
3450e705c121SKalle Valo 			goto out_unlock;
3451e705c121SKalle Valo 		}
3452e705c121SKalle Valo 
345397cc1694SAvraham Stern 		if (vif->type == NL80211_IFTYPE_STATION)
3454046d2e7cSSriram R 			vif->bss_conf.he_support = sta->deflink.he_cap.has_he;
345597cc1694SAvraham Stern 
3456e705c121SKalle Valo 		if (sta->tdls &&
3457e705c121SKalle Valo 		    (vif->p2p ||
3458e705c121SKalle Valo 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
3459e705c121SKalle Valo 						IWL_MVM_TDLS_STA_COUNT ||
3460e705c121SKalle Valo 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3461e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3462e705c121SKalle Valo 			ret = -EBUSY;
3463e705c121SKalle Valo 			goto out_unlock;
3464e705c121SKalle Valo 		}
3465e705c121SKalle Valo 
3466e705c121SKalle Valo 		ret = iwl_mvm_add_sta(mvm, vif, sta);
34671e8f1329SGolan Ben-Ami 		if (sta->tdls && ret == 0) {
3468e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
34691e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
34701e8f1329SGolan Ben-Ami 						   NL80211_TDLS_SETUP);
34711e8f1329SGolan Ben-Ami 		}
3472438af969SSara Sharon 
34734c51541dSBenjamin Berg 		sta->deflink.agg.max_rc_amsdu_len = 1;
3474e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3475e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3476e705c121SKalle Valo 		/*
3477e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3478e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3479e705c121SKalle Valo 		 */
3480e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3481e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3482e705c121SKalle Valo 		ret = 0;
3483e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3484e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
34858be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
3486046d2e7cSSriram R 			vif->bss_conf.he_support = sta->deflink.he_cap.has_he;
34878be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count++;
34888be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3489cb63eb43SMiri Korenblit 			if ((vif->bss_conf.he_support &&
3490cb63eb43SMiri Korenblit 			     !iwlwifi_mod_params.disable_11ax) ||
3491cb63eb43SMiri Korenblit 			    (vif->bss_conf.eht_support &&
3492cb63eb43SMiri Korenblit 			     !iwlwifi_mod_params.disable_11be))
349302221a81SShaul Triebitz 				iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
34949394662aSLiad Kaufman 		} else if (vif->type == NL80211_IFTYPE_STATION) {
3495046d2e7cSSriram R 			vif->bss_conf.he_support = sta->deflink.he_cap.has_he;
34964f58121dSIlan Peer 
34974f58121dSIlan Peer 			mvmvif->he_ru_2mhz_block = false;
3498046d2e7cSSriram R 			if (sta->deflink.he_cap.has_he)
34994f58121dSIlan Peer 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif);
35004f58121dSIlan Peer 
35019394662aSLiad Kaufman 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
35028be30c13SAyala Beker 		}
3503735a0045SGoodstein, Mordechay 
35043baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
35053baf7528SAvraham Stern 				     false);
3506735a0045SGoodstein, Mordechay 		ret = iwl_mvm_update_sta(mvm, vif, sta);
3507e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3508e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
350928916a16SEmmanuel Grumbach 		ret = 0;
3510e705c121SKalle Valo 
3511e705c121SKalle Valo 		/* we don't support TDLS during DCM */
3512e705c121SKalle Valo 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
3513e705c121SKalle Valo 			iwl_mvm_teardown_tdls_peers(mvm);
3514e705c121SKalle Valo 
35154634b176SJohannes Berg 		if (sta->tdls) {
35161e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
35171e8f1329SGolan Ben-Ami 						   NL80211_TDLS_ENABLE_LINK);
35184634b176SJohannes Berg 		} else {
3519e705c121SKalle Valo 			/* enable beacon filtering */
3520e705c121SKalle Valo 			WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
35216b7a5aeaSNaftali Goldstein 
35228b75858cSJohannes Berg 			mvmvif->authorized = 1;
35238b75858cSJohannes Berg 
352450f56044SIlan Peer 			/*
352550f56044SIlan Peer 			 * Now that the station is authorized, i.e., keys were already
352650f56044SIlan Peer 			 * installed, need to indicate to the FW that
352750f56044SIlan Peer 			 * multicast data frames can be forwarded to the driver
352850f56044SIlan Peer 			 */
352950f56044SIlan Peer 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
35306d19a5ebSEmmanuel Grumbach 			iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
35314634b176SJohannes Berg 		}
353250f56044SIlan Peer 
35333baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
35343baf7528SAvraham Stern 				     true);
3535e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3536e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
3537d9e95e35SMordechay Goodstein 		/* once we move into assoc state, need to update rate scale to
3538d9e95e35SMordechay Goodstein 		 * disable using wide bandwidth
3539d9e95e35SMordechay Goodstein 		 */
3540d9e95e35SMordechay Goodstein 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3541d9e95e35SMordechay Goodstein 				     false);
35424634b176SJohannes Berg 		if (!sta->tdls) {
354350f56044SIlan Peer 			/* Multicast data frames are no longer allowed */
354450f56044SIlan Peer 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
354550f56044SIlan Peer 
35468b75858cSJohannes Berg 			/*
35478b75858cSJohannes Berg 			 * Set this after the above iwl_mvm_mac_ctxt_changed()
35488b75858cSJohannes Berg 			 * to avoid sending high prio again for a little time.
35498b75858cSJohannes Berg 			 */
35508b75858cSJohannes Berg 			mvmvif->authorized = 0;
35518b75858cSJohannes Berg 
3552e705c121SKalle Valo 			/* disable beacon filtering */
355369e508b4SIlan Peer 			ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
355469e508b4SIlan Peer 			WARN_ON(ret &&
355569e508b4SIlan Peer 				!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
355669e508b4SIlan Peer 					  &mvm->status));
35574634b176SJohannes Berg 		}
3558e705c121SKalle Valo 		ret = 0;
3559e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3560e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
35618be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
35628be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
35638be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3564d5d8ee52SEmmanuel Grumbach 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3565cf7a7457SJohannes Berg 			iwl_mvm_stop_session_protection(mvm, vif);
3566e705c121SKalle Valo 		ret = 0;
3567e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3568e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3569e705c121SKalle Valo 		ret = 0;
3570e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3571e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
3572d5d8ee52SEmmanuel Grumbach 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3573d5d8ee52SEmmanuel Grumbach 			iwl_mvm_stop_session_protection(mvm, vif);
3574e705c121SKalle Valo 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
35751e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3576e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
35771e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
35781e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
35791e8f1329SGolan Ben-Ami 		}
358034a880d8SLiad Kaufman 
358144135b7cSIlan Peer 		if (unlikely(ret &&
358244135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
358344135b7cSIlan Peer 				      &mvm->status)))
358444135b7cSIlan Peer 			ret = 0;
3585e705c121SKalle Valo 	} else {
3586e705c121SKalle Valo 		ret = -EIO;
3587e705c121SKalle Valo 	}
3588e705c121SKalle Valo  out_unlock:
3589e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3590e705c121SKalle Valo 
3591e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3592e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3593e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3594e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3595e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3596e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3597e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3598e705c121SKalle Valo 	}
3599e705c121SKalle Valo 
3600e705c121SKalle Valo 	return ret;
3601e705c121SKalle Valo }
3602e705c121SKalle Valo 
3603e705c121SKalle Valo static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3604e705c121SKalle Valo {
3605e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3606e705c121SKalle Valo 
3607e705c121SKalle Valo 	mvm->rts_threshold = value;
3608e705c121SKalle Valo 
3609e705c121SKalle Valo 	return 0;
3610e705c121SKalle Valo }
3611e705c121SKalle Valo 
3612e705c121SKalle Valo static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3613e705c121SKalle Valo 				  struct ieee80211_vif *vif,
3614e705c121SKalle Valo 				  struct ieee80211_sta *sta, u32 changed)
3615e705c121SKalle Valo {
3616e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3617dcfe3b10SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3618dcfe3b10SJohannes Berg 
3619dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
3620dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3621dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
3622dcfe3b10SJohannes Berg 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3623dcfe3b10SJohannes Berg 				     true);
3624e705c121SKalle Valo 
3625e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
3626e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
3627e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
3628e705c121SKalle Valo }
3629e705c121SKalle Valo 
3630e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3631b3e2130bSJohannes Berg 			       struct ieee80211_vif *vif,
3632b3e2130bSJohannes Berg 			       unsigned int link_id, u16 ac,
3633e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
3634e705c121SKalle Valo {
3635e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3636e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3637e705c121SKalle Valo 
3638e705c121SKalle Valo 	mvmvif->queue_params[ac] = *params;
3639e705c121SKalle Valo 
3640e705c121SKalle Valo 	/*
3641e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
3642e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
3643e705c121SKalle Valo 	 */
3644e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3645e705c121SKalle Valo 		int ret;
3646e705c121SKalle Valo 
3647e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
3648e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3649e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3650e705c121SKalle Valo 		return ret;
3651e705c121SKalle Valo 	}
3652e705c121SKalle Valo 	return 0;
3653e705c121SKalle Valo }
3654e705c121SKalle Valo 
3655e705c121SKalle Valo static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3656d4e36e55SIlan Peer 				       struct ieee80211_vif *vif,
365715fae341SJohannes Berg 				       struct ieee80211_prep_tx_info *info)
3658e705c121SKalle Valo {
3659e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3660d4e36e55SIlan Peer 
3661e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3662af84ac57SJohannes Berg 	iwl_mvm_protect_assoc(mvm, vif, info->duration);
3663e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3664e705c121SKalle Valo }
3665e705c121SKalle Valo 
36666b1259d1SJohannes Berg static void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
36676b1259d1SJohannes Berg 					struct ieee80211_vif *vif,
36686b1259d1SJohannes Berg 					struct ieee80211_prep_tx_info *info)
36696b1259d1SJohannes Berg {
36706b1259d1SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
36716b1259d1SJohannes Berg 
36726b1259d1SJohannes Berg 	/* for successful cases (auth/assoc), don't cancel session protection */
36736b1259d1SJohannes Berg 	if (info->success)
36746b1259d1SJohannes Berg 		return;
36756b1259d1SJohannes Berg 
36766b1259d1SJohannes Berg 	mutex_lock(&mvm->mutex);
36776b1259d1SJohannes Berg 	iwl_mvm_stop_session_protection(mvm, vif);
36786b1259d1SJohannes Berg 	mutex_unlock(&mvm->mutex);
36796b1259d1SJohannes Berg }
36806b1259d1SJohannes Berg 
3681e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3682e705c121SKalle Valo 					struct ieee80211_vif *vif,
3683e705c121SKalle Valo 					struct cfg80211_sched_scan_request *req,
3684e705c121SKalle Valo 					struct ieee80211_scan_ies *ies)
3685e705c121SKalle Valo {
3686e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3687e705c121SKalle Valo 
3688e705c121SKalle Valo 	int ret;
3689e705c121SKalle Valo 
3690e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3691e705c121SKalle Valo 
3692f276e20bSJohannes Berg 	if (!vif->cfg.idle) {
3693e705c121SKalle Valo 		ret = -EBUSY;
3694e705c121SKalle Valo 		goto out;
3695e705c121SKalle Valo 	}
3696e705c121SKalle Valo 
3697e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
3698e705c121SKalle Valo 
3699e705c121SKalle Valo out:
3700e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3701e705c121SKalle Valo 	return ret;
3702e705c121SKalle Valo }
3703e705c121SKalle Valo 
3704e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3705e705c121SKalle Valo 				       struct ieee80211_vif *vif)
3706e705c121SKalle Valo {
3707e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3708e705c121SKalle Valo 	int ret;
3709e705c121SKalle Valo 
3710e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3711e705c121SKalle Valo 
3712e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3713e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
3714e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
3715e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
3716e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3717e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
3718e705c121SKalle Valo 	 * not running.
3719e705c121SKalle Valo 	*/
3720e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
3721e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3722e705c121SKalle Valo 		return 0;
3723e705c121SKalle Valo 	}
3724e705c121SKalle Valo 
3725e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
3726e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3727e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
3728e705c121SKalle Valo 
3729e705c121SKalle Valo 	return ret;
3730e705c121SKalle Valo }
3731e705c121SKalle Valo 
37326569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3733e705c121SKalle Valo 				 enum set_key_cmd cmd,
3734e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3735e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3736e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
3737e705c121SKalle Valo {
3738c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3739e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
374046c7b05aSEmmanuel Grumbach 	struct iwl_mvm_sta *mvmsta = NULL;
3741f5e28eacSJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn;
3742f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
37435c75a208SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
37445c75a208SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
3745c56e00a3SJohannes Berg 	int ret, i;
37464615fd15SEmmanuel Grumbach 	u8 key_offset;
3747e705c121SKalle Valo 
374846c7b05aSEmmanuel Grumbach 	if (sta)
37496d19a5ebSEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
37506d19a5ebSEmmanuel Grumbach 
3751e705c121SKalle Valo 	switch (key->cipher) {
3752e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3753286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
3754e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
37551ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
37567f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
37577f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
37587f768ad5SDavid Spinadel 		} else {
37597f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
37607f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
37617f768ad5SDavid Spinadel 		}
3762e705c121SKalle Valo 		break;
3763e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
37642a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
37652a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
376685aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
3767e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3768e705c121SKalle Valo 		break;
3769e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
37708e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
37718e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3772e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
3773e705c121SKalle Valo 		break;
3774e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3775e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3776475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
3777e705c121SKalle Valo 			break;
3778475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
3779475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
3780475c6bdeSJohannes Berg 		/* support HW crypto on TX */
3781475c6bdeSJohannes Berg 		return 0;
3782e705c121SKalle Valo 	default:
3783e705c121SKalle Valo 		return -EOPNOTSUPP;
3784e705c121SKalle Valo 	}
3785e705c121SKalle Valo 
3786e705c121SKalle Valo 	switch (cmd) {
3787e705c121SKalle Valo 	case SET_KEY:
3788*a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
3789*a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
3790b1fdc250SJohannes Berg 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
3791b1fdc250SJohannes Berg 					   key);
3792b1fdc250SJohannes Berg 
3793e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
3794e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
3795e705c121SKalle Valo 			/*
3796e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
3797e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
3798e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
37998e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
3800*a5de7de7SJohannes Berg 			 *
3801*a5de7de7SJohannes Berg 			 * Except, of course, beacon protection - it must be
3802*a5de7de7SJohannes Berg 			 * offloaded since we just set a beacon template.
3803e705c121SKalle Valo 			 */
3804*a5de7de7SJohannes Berg 			if (keyidx < 6 &&
3805*a5de7de7SJohannes Berg 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
38068e160ab8SAyala Beker 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3807*a5de7de7SJohannes Berg 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
380881279c49SJohannes Berg 				ret = -EOPNOTSUPP;
3809a1c2ff30SAndrei Otcheretianski 				break;
3810a1c2ff30SAndrei Otcheretianski 			}
381185aeb58cSDavid Spinadel 
381285aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
381385aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
381485aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
3815e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
3816a1c2ff30SAndrei Otcheretianski 				ret = 0;
3817e705c121SKalle Valo 				break;
3818e705c121SKalle Valo 			}
3819c56e00a3SJohannes Berg 
3820c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
3821c56e00a3SJohannes Berg 				for (i = 0;
3822c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
3823c56e00a3SJohannes Berg 				     i++) {
3824c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
3825c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
3826c56e00a3SJohannes Berg 						break;
3827c56e00a3SJohannes Berg 					}
3828c56e00a3SJohannes Berg 				}
3829c56e00a3SJohannes Berg 
3830c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
3831c56e00a3SJohannes Berg 					ret = -ENOSPC;
3832a1c2ff30SAndrei Otcheretianski 				else
3833a1c2ff30SAndrei Otcheretianski 					ret = 0;
3834c56e00a3SJohannes Berg 
3835c56e00a3SJohannes Berg 				break;
3836c56e00a3SJohannes Berg 			}
383785aeb58cSDavid Spinadel 		}
3838e705c121SKalle Valo 
3839e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
3840e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
3841e705c121SKalle Valo 		 */
3842e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3843e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
3844e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
3845e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
3846e705c121SKalle Valo 			ret = 0;
3847e705c121SKalle Valo 			break;
3848e705c121SKalle Valo 		}
3849e705c121SKalle Valo 
3850f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
385146c7b05aSEmmanuel Grumbach 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
3852f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3853f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
38542a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
38552a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3856f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
3857f5e28eacSJohannes Berg 			int tid, q;
3858f5e28eacSJohannes Berg 
3859f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3860acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
3861acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
3862f5e28eacSJohannes Berg 					 GFP_KERNEL);
3863f5e28eacSJohannes Berg 			if (!ptk_pn) {
3864f5e28eacSJohannes Berg 				ret = -ENOMEM;
3865f5e28eacSJohannes Berg 				break;
3866f5e28eacSJohannes Berg 			}
3867f5e28eacSJohannes Berg 
3868f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3869f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
3870f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
3871f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
3872f5e28eacSJohannes Berg 					       seq.ccmp.pn,
3873f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
3874f5e28eacSJohannes Berg 			}
3875f5e28eacSJohannes Berg 
3876f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3877f5e28eacSJohannes Berg 		}
3878f5e28eacSJohannes Berg 
38794615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
38804615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
38814615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
38824615fd15SEmmanuel Grumbach 		else
38834615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
38844615fd15SEmmanuel Grumbach 
388546c7b05aSEmmanuel Grumbach 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
38866d19a5ebSEmmanuel Grumbach 			mvmsta->pairwise_cipher = key->cipher;
38876d19a5ebSEmmanuel Grumbach 
3888*a5de7de7SJohannes Berg 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
3889*a5de7de7SJohannes Berg 				   sta ? sta->addr : NULL, key->keyidx);
38905c75a208SJohannes Berg 
38915c75a208SJohannes Berg 		if (sec_key_ver)
38925c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
38935c75a208SJohannes Berg 		else
38944615fd15SEmmanuel Grumbach 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
38955c75a208SJohannes Berg 
3896e705c121SKalle Valo 		if (ret) {
3897e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
3898475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
3899e705c121SKalle Valo 			/*
3900e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
3901475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
3902475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
3903475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
3904e705c121SKalle Valo 			 */
3905475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
3906475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
3907475c6bdeSJohannes Berg 			else
3908e705c121SKalle Valo 				ret = 0;
3909e705c121SKalle Valo 		}
3910e705c121SKalle Valo 
3911e705c121SKalle Valo 		break;
3912e705c121SKalle Valo 	case DISABLE_KEY:
3913*a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
3914*a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
3915b1fdc250SJohannes Berg 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
3916b1fdc250SJohannes Berg 					 NULL);
3917b1fdc250SJohannes Berg 
3918c56e00a3SJohannes Berg 		ret = -ENOENT;
3919c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3920c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
3921c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
3922c56e00a3SJohannes Berg 				ret = 0;
3923c56e00a3SJohannes Berg 			}
3924c56e00a3SJohannes Berg 		}
3925c56e00a3SJohannes Berg 
3926c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
3927c56e00a3SJohannes Berg 		if (ret == 0)
3928c56e00a3SJohannes Berg 			break;
3929c56e00a3SJohannes Berg 
3930e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3931e705c121SKalle Valo 			ret = 0;
3932e705c121SKalle Valo 			break;
3933e705c121SKalle Valo 		}
3934e705c121SKalle Valo 
393546c7b05aSEmmanuel Grumbach 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
3936f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3937f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
39382a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
39392a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3940f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
3941f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
3942f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
3943f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3944f5e28eacSJohannes Berg 			if (ptk_pn)
3945f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
3946f5e28eacSJohannes Berg 		}
3947f5e28eacSJohannes Berg 
3948e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
39495c75a208SJohannes Berg 		if (sec_key_ver)
39505c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
39515c75a208SJohannes Berg 		else
3952e705c121SKalle Valo 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3953e705c121SKalle Valo 		break;
3954e705c121SKalle Valo 	default:
3955e705c121SKalle Valo 		ret = -EINVAL;
3956e705c121SKalle Valo 	}
3957e705c121SKalle Valo 
39586569e7d3SJohannes Berg 	return ret;
39596569e7d3SJohannes Berg }
39606569e7d3SJohannes Berg 
39616569e7d3SJohannes Berg static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
39626569e7d3SJohannes Berg 			       enum set_key_cmd cmd,
39636569e7d3SJohannes Berg 			       struct ieee80211_vif *vif,
39646569e7d3SJohannes Berg 			       struct ieee80211_sta *sta,
39656569e7d3SJohannes Berg 			       struct ieee80211_key_conf *key)
39666569e7d3SJohannes Berg {
39676569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
39686569e7d3SJohannes Berg 	int ret;
39696569e7d3SJohannes Berg 
39706569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
39716569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
3972e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
39736569e7d3SJohannes Berg 
3974e705c121SKalle Valo 	return ret;
3975e705c121SKalle Valo }
3976e705c121SKalle Valo 
3977e705c121SKalle Valo static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3978e705c121SKalle Valo 					struct ieee80211_vif *vif,
3979e705c121SKalle Valo 					struct ieee80211_key_conf *keyconf,
3980e705c121SKalle Valo 					struct ieee80211_sta *sta,
3981e705c121SKalle Valo 					u32 iv32, u16 *phase1key)
3982e705c121SKalle Valo {
3983e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3984e705c121SKalle Valo 
3985e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3986e705c121SKalle Valo 		return;
3987e705c121SKalle Valo 
3988e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3989e705c121SKalle Valo }
3990e705c121SKalle Valo 
3991e705c121SKalle Valo 
3992e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3993e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
3994e705c121SKalle Valo {
3995e705c121SKalle Valo 	struct iwl_mvm *mvm =
3996e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3997e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
3998e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
3999e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
4000e705c121SKalle Valo 
4001e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4002e705c121SKalle Valo 		return true;
4003e705c121SKalle Valo 
4004e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4005e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4006e705c121SKalle Valo 		return true;
4007e705c121SKalle Valo 	}
4008e705c121SKalle Valo 
4009e705c121SKalle Valo 	resp = (void *)pkt->data;
4010e705c121SKalle Valo 
4011e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
4012b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4013e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
4014e705c121SKalle Valo 
4015e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4016e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4017e705c121SKalle Valo 		     te_data->uid);
4018e705c121SKalle Valo 
4019e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4020e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4021e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4022e705c121SKalle Valo 
4023e705c121SKalle Valo 	return true;
4024e705c121SKalle Valo }
4025e705c121SKalle Valo 
4026dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
4027dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
4028dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
4029dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
4030dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
4031e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4032e705c121SKalle Valo 				    struct ieee80211_channel *channel,
4033e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4034e705c121SKalle Valo 				    int duration)
4035e705c121SKalle Valo {
4036afc1e3b4SAvraham Stern 	int res;
4037e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4038e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4039e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4040e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
4041dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
4042dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
4043dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
4044e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
4045e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4046e705c121SKalle Valo 		.id_and_color =
4047e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4048e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4049e705c121SKalle Valo 	};
405057e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
405157e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
405257e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
405357e861d9SDavid Spinadel 
405457e861d9SDavid Spinadel 	/* Set the channel info data */
405557e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
40563717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
40577ac87575SJohannes Berg 			      IWL_PHY_CHANNEL_MODE20,
405857e861d9SDavid Spinadel 			      0);
405957e861d9SDavid Spinadel 
406057e861d9SDavid Spinadel 	/* Set the time and duration */
4061afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4062e705c121SKalle Valo 
4063dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
4064dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
4065dc28e12fSMatti Gottlieb 
4066dc28e12fSMatti Gottlieb 	/*
4067dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
4068dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
4069dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
4070dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
4071dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
4072dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
4073dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
4074dc28e12fSMatti Gottlieb 	 */
4075f276e20bSJohannes Berg 	if (vif->cfg.assoc) {
4076dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
4077dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
4078dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
4079dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
4080dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
4081dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
4082dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
4083dc28e12fSMatti Gottlieb 		}
4084dc28e12fSMatti Gottlieb 	}
4085dc28e12fSMatti Gottlieb 
408657e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
408757e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
4088dc28e12fSMatti Gottlieb 
4089dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
4090903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
4091903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
4092903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
4093903b3f9bSEmmanuel Grumbach 		     "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
4094903b3f9bSEmmanuel Grumbach 		     duration, delay, dtim_interval);
4095903b3f9bSEmmanuel Grumbach 
4096e705c121SKalle Valo 	/* Set the node address */
409757e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4098e705c121SKalle Valo 
4099e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4100e705c121SKalle Valo 
4101e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4102e705c121SKalle Valo 
4103e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4104e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4105e705c121SKalle Valo 		return -EIO;
4106e705c121SKalle Valo 	}
4107e705c121SKalle Valo 
4108e705c121SKalle Valo 	te_data->vif = vif;
4109e705c121SKalle Valo 	te_data->duration = duration;
4110e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
4111e705c121SKalle Valo 
4112e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4113e705c121SKalle Valo 
4114e705c121SKalle Valo 	/*
4115e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
4116e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
4117e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
4118e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4119e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
4120e705c121SKalle Valo 	 * time another notification (that the time event started)
4121e705c121SKalle Valo 	 * might already be processed unsuccessfully.
4122e705c121SKalle Valo 	 */
4123e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4124e705c121SKalle Valo 				   time_event_response,
4125e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
4126e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
4127e705c121SKalle Valo 
412857e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4129e705c121SKalle Valo 				   &aux_roc_req);
4130e705c121SKalle Valo 
4131e705c121SKalle Valo 	if (res) {
4132e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4133e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4134e705c121SKalle Valo 		goto out_clear_te;
4135e705c121SKalle Valo 	}
4136e705c121SKalle Valo 
4137e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4138e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4139e705c121SKalle Valo 	/* should never fail */
4140e705c121SKalle Valo 	WARN_ON_ONCE(res);
4141e705c121SKalle Valo 
4142e705c121SKalle Valo 	if (res) {
4143e705c121SKalle Valo  out_clear_te:
4144e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
4145e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
4146e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4147e705c121SKalle Valo 	}
4148e705c121SKalle Valo 
4149e705c121SKalle Valo 	return res;
4150e705c121SKalle Valo }
4151e705c121SKalle Valo 
4152e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
4153e705c121SKalle Valo 		       struct ieee80211_vif *vif,
4154e705c121SKalle Valo 		       struct ieee80211_channel *channel,
4155e705c121SKalle Valo 		       int duration,
4156e705c121SKalle Valo 		       enum ieee80211_roc_type type)
4157e705c121SKalle Valo {
4158e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4159e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4160e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
4161e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
41628835a64fSJohannes Berg 	bool band_change_removal;
4163e705c121SKalle Valo 	int ret, i;
4164e705c121SKalle Valo 
4165e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4166e705c121SKalle Valo 			   duration, type);
4167e705c121SKalle Valo 
41689834781cSJohannes Berg 	/*
41699834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
41709834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
41719834781cSJohannes Berg 	 */
4172e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
4173e705c121SKalle Valo 
4174e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4175e705c121SKalle Valo 
4176e705c121SKalle Valo 	switch (vif->type) {
4177e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4178e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
4179e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4180e705c121SKalle Valo 			/* Use aux roc framework (HS20) */
4181971cbe50SJohannes Berg 			if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12) {
41822c2c3647SNathan Errera 				u32 lmac_id;
41832c2c3647SNathan Errera 
41842c2c3647SNathan Errera 				lmac_id = iwl_mvm_get_lmac_id(mvm->fw,
41852c2c3647SNathan Errera 							      channel->band);
41862c2c3647SNathan Errera 				ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
41872c2c3647SNathan Errera 				if (WARN(ret,
41882c2c3647SNathan Errera 					 "Failed to allocate aux station"))
41892c2c3647SNathan Errera 					goto out_unlock;
41902c2c3647SNathan Errera 			}
4191e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
4192e705c121SKalle Valo 						       vif, duration);
4193e705c121SKalle Valo 			goto out_unlock;
4194e705c121SKalle Valo 		}
4195e705c121SKalle Valo 		IWL_ERR(mvm, "hotspot not supported\n");
4196e705c121SKalle Valo 		ret = -EINVAL;
4197e705c121SKalle Valo 		goto out_unlock;
4198e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
4199e705c121SKalle Valo 		/* handle below */
4200e705c121SKalle Valo 		break;
4201e705c121SKalle Valo 	default:
4202e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
4203e705c121SKalle Valo 		ret = -EINVAL;
4204e705c121SKalle Valo 		goto out_unlock;
4205e705c121SKalle Valo 	}
4206e705c121SKalle Valo 
4207e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
4208e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
4209e705c121SKalle Valo 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
4210e705c121SKalle Valo 			continue;
4211e705c121SKalle Valo 
4212e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
4213e705c121SKalle Valo 			/*
4214e705c121SKalle Valo 			 * Unbind the P2P_DEVICE from the current PHY context,
4215e705c121SKalle Valo 			 * and if the PHY context is not used remove it.
4216e705c121SKalle Valo 			 */
4217e705c121SKalle Valo 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
4218e705c121SKalle Valo 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
4219e705c121SKalle Valo 				goto out_unlock;
4220e705c121SKalle Valo 
4221e705c121SKalle Valo 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
4222e705c121SKalle Valo 
4223e705c121SKalle Valo 			/* Bind the P2P_DEVICE to the current PHY Context */
4224e705c121SKalle Valo 			mvmvif->phy_ctxt = phy_ctxt;
4225e705c121SKalle Valo 
4226e705c121SKalle Valo 			ret = iwl_mvm_binding_add_vif(mvm, vif);
4227e705c121SKalle Valo 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4228e705c121SKalle Valo 				goto out_unlock;
4229e705c121SKalle Valo 
4230e705c121SKalle Valo 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
4231e705c121SKalle Valo 			goto schedule_time_event;
4232e705c121SKalle Valo 		}
4233e705c121SKalle Valo 	}
4234e705c121SKalle Valo 
4235e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
4236e705c121SKalle Valo 	if (channel == mvmvif->phy_ctxt->channel)
4237e705c121SKalle Valo 		goto schedule_time_event;
4238e705c121SKalle Valo 
4239e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4240e705c121SKalle Valo 
4241e705c121SKalle Valo 	/*
42428835a64fSJohannes Berg 	 * Check if the remain-on-channel is on a different band and that
42438835a64fSJohannes Berg 	 * requires context removal, see iwl_mvm_phy_ctxt_changed(). If
42448835a64fSJohannes Berg 	 * so, we'll need to release and then re-configure here, since we
42458835a64fSJohannes Berg 	 * must not remove a PHY context that's part of a binding.
4246e705c121SKalle Valo 	 */
42478835a64fSJohannes Berg 	band_change_removal =
42488835a64fSJohannes Berg 		fw_has_capa(&mvm->fw->ucode_capa,
42498835a64fSJohannes Berg 			    IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
42508835a64fSJohannes Berg 		mvmvif->phy_ctxt->channel->band != chandef.chan->band;
42518835a64fSJohannes Berg 
42528835a64fSJohannes Berg 	if (mvmvif->phy_ctxt->ref == 1 && !band_change_removal) {
42538835a64fSJohannes Berg 		/*
42548835a64fSJohannes Berg 		 * Change the PHY context configuration as it is currently
42558835a64fSJohannes Berg 		 * referenced only by the P2P Device MAC (and we can modify it)
42568835a64fSJohannes Berg 		 */
4257e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
4258e705c121SKalle Valo 					       &chandef, 1, 1);
4259e705c121SKalle Valo 		if (ret)
4260e705c121SKalle Valo 			goto out_unlock;
4261e705c121SKalle Valo 	} else {
4262e705c121SKalle Valo 		/*
42638835a64fSJohannes Berg 		 * The PHY context is shared with other MACs (or we're trying to
42648835a64fSJohannes Berg 		 * switch bands), so remove the P2P Device from the binding,
42658835a64fSJohannes Berg 		 * allocate an new PHY context and create a new binding.
4266e705c121SKalle Valo 		 */
4267e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4268e705c121SKalle Valo 		if (!phy_ctxt) {
4269e705c121SKalle Valo 			ret = -ENOSPC;
4270e705c121SKalle Valo 			goto out_unlock;
4271e705c121SKalle Valo 		}
4272e705c121SKalle Valo 
4273e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
4274e705c121SKalle Valo 					       1, 1);
4275e705c121SKalle Valo 		if (ret) {
4276e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
4277e705c121SKalle Valo 			goto out_unlock;
4278e705c121SKalle Valo 		}
4279e705c121SKalle Valo 
4280e705c121SKalle Valo 		/* Unbind the P2P_DEVICE from the current PHY context */
4281e705c121SKalle Valo 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
4282e705c121SKalle Valo 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
4283e705c121SKalle Valo 			goto out_unlock;
4284e705c121SKalle Valo 
4285e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
4286e705c121SKalle Valo 
4287e705c121SKalle Valo 		/* Bind the P2P_DEVICE to the new allocated PHY context */
4288e705c121SKalle Valo 		mvmvif->phy_ctxt = phy_ctxt;
4289e705c121SKalle Valo 
4290e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
4291e705c121SKalle Valo 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4292e705c121SKalle Valo 			goto out_unlock;
4293e705c121SKalle Valo 
4294e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
4295e705c121SKalle Valo 	}
4296e705c121SKalle Valo 
4297e705c121SKalle Valo schedule_time_event:
4298e705c121SKalle Valo 	/* Schedule the time events */
4299e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4300e705c121SKalle Valo 
4301e705c121SKalle Valo out_unlock:
4302e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4303e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4304e705c121SKalle Valo 	return ret;
4305e705c121SKalle Valo }
4306e705c121SKalle Valo 
43075db4c4b9SEmmanuel Grumbach static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
43085db4c4b9SEmmanuel Grumbach 			      struct ieee80211_vif *vif)
4309e705c121SKalle Valo {
4310e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4311e705c121SKalle Valo 
4312e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4313e705c121SKalle Valo 
4314e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4315fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
4316e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4317e705c121SKalle Valo 
4318e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4319e705c121SKalle Valo 	return 0;
4320e705c121SKalle Valo }
4321e705c121SKalle Valo 
4322b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
4323b73f9a4aSJohannes Berg 	bool responder;
4324b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
4325b73f9a4aSJohannes Berg };
4326b73f9a4aSJohannes Berg 
4327b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4328b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
4329b73f9a4aSJohannes Berg {
4330b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4331b73f9a4aSJohannes Berg 
4332d0a9123eSJohannes Berg 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4333b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4334b73f9a4aSJohannes Berg 		data->responder = true;
4335b73f9a4aSJohannes Berg }
4336b73f9a4aSJohannes Berg 
4337b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4338b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
4339b73f9a4aSJohannes Berg {
4340b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
4341b73f9a4aSJohannes Berg 		.responder = false,
4342b73f9a4aSJohannes Berg 		.ctx = ctx,
4343b73f9a4aSJohannes Berg 	};
4344b73f9a4aSJohannes Berg 
4345b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4346b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
4347b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
4348b73f9a4aSJohannes Berg 					&data);
4349b73f9a4aSJohannes Berg 	return data.responder;
4350b73f9a4aSJohannes Berg }
4351b73f9a4aSJohannes Berg 
4352e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4353e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
4354e705c121SKalle Valo {
4355e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4356e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4357b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4358b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4359e705c121SKalle Valo 	int ret;
4360e705c121SKalle Valo 
4361e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4362e705c121SKalle Valo 
4363e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4364e705c121SKalle Valo 
4365e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4366e705c121SKalle Valo 	if (!phy_ctxt) {
4367e705c121SKalle Valo 		ret = -ENOSPC;
4368e705c121SKalle Valo 		goto out;
4369e705c121SKalle Valo 	}
4370e705c121SKalle Valo 
4371b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4372e705c121SKalle Valo 				       ctx->rx_chains_static,
4373e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
4374e705c121SKalle Valo 	if (ret) {
4375e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4376e705c121SKalle Valo 		goto out;
4377e705c121SKalle Valo 	}
4378e705c121SKalle Valo 
4379e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4380e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4381e705c121SKalle Valo out:
4382e705c121SKalle Valo 	return ret;
4383e705c121SKalle Valo }
4384e705c121SKalle Valo 
4385e705c121SKalle Valo static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4386e705c121SKalle Valo 			       struct ieee80211_chanctx_conf *ctx)
4387e705c121SKalle Valo {
4388e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4389e705c121SKalle Valo 	int ret;
4390e705c121SKalle Valo 
4391e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4392e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4393e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4394e705c121SKalle Valo 
4395e705c121SKalle Valo 	return ret;
4396e705c121SKalle Valo }
4397e705c121SKalle Valo 
4398e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4399e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4400e705c121SKalle Valo {
4401e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4402e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4403e705c121SKalle Valo 
4404e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4405e705c121SKalle Valo 
4406e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4407e705c121SKalle Valo }
4408e705c121SKalle Valo 
4409e705c121SKalle Valo static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4410e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx)
4411e705c121SKalle Valo {
4412e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4413e705c121SKalle Valo 
4414e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4415e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4416e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4417e705c121SKalle Valo }
4418e705c121SKalle Valo 
4419e705c121SKalle Valo static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4420e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx,
4421e705c121SKalle Valo 				   u32 changed)
4422e705c121SKalle Valo {
4423e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4424e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4425e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4426b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4427b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4428e705c121SKalle Valo 
4429e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4430e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4431e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4432e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4433e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4434e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4435e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4436e705c121SKalle Valo 		return;
4437e705c121SKalle Valo 
4438e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
44397a20bcceSEmmanuel Grumbach 
44407a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
44417a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4442b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
44437a20bcceSEmmanuel Grumbach 			goto out_unlock;
44447a20bcceSEmmanuel Grumbach 
44457a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
44467a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4447b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
44487a20bcceSEmmanuel Grumbach 			goto out_unlock;
44497a20bcceSEmmanuel Grumbach 	}
44507a20bcceSEmmanuel Grumbach 
4451e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4452b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4453e705c121SKalle Valo 				 ctx->rx_chains_static,
4454e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
44557a20bcceSEmmanuel Grumbach 
44567a20bcceSEmmanuel Grumbach out_unlock:
4457e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4458e705c121SKalle Valo }
4459e705c121SKalle Valo 
44608a919a78SMiri Korenblit /*
44618a919a78SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
44628a919a78SMiri Korenblit  *
44638a919a78SMiri Korenblit  * Returns true if we're done assigning the chanctx
44648a919a78SMiri Korenblit  * (either on failure or success)
44658a919a78SMiri Korenblit  */
446650e81437SMiri Korenblit bool __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
4467e705c121SKalle Valo 					 struct ieee80211_vif *vif,
4468e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx,
446950e81437SMiri Korenblit 					 bool switching_chanctx, int *ret)
4470e705c121SKalle Valo {
4471e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4472e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4473e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4474e705c121SKalle Valo 
4475e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4476e705c121SKalle Valo 
4477e705c121SKalle Valo 	mvmvif->phy_ctxt = phy_ctxt;
4478e705c121SKalle Valo 
4479e705c121SKalle Valo 	switch (vif->type) {
4480e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4481e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4482e705c121SKalle Valo 		if (switching_chanctx) {
4483e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4484e705c121SKalle Valo 			break;
4485e705c121SKalle Valo 		}
44865a2abdcaSGustavo A. R. Silva 		fallthrough;
4487e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4488e705c121SKalle Valo 		/*
4489e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4490e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4491e705c121SKalle Valo 		 */
44928a919a78SMiri Korenblit 		*ret = 0;
44938a919a78SMiri Korenblit 		return true;
4494e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4495e705c121SKalle Valo 		break;
4496e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4497e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4498e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4499e705c121SKalle Valo 		break;
4500e705c121SKalle Valo 	default:
45018a919a78SMiri Korenblit 		*ret = -EINVAL;
45028a919a78SMiri Korenblit 		return true;
4503e705c121SKalle Valo 	}
45048a919a78SMiri Korenblit 	return false;
45058a919a78SMiri Korenblit }
45068a919a78SMiri Korenblit 
45078a919a78SMiri Korenblit static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
45088a919a78SMiri Korenblit 					struct ieee80211_vif *vif,
45098a919a78SMiri Korenblit 					struct ieee80211_chanctx_conf *ctx,
45108a919a78SMiri Korenblit 					bool switching_chanctx)
45118a919a78SMiri Korenblit {
45128a919a78SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
45138a919a78SMiri Korenblit 	int ret;
45148a919a78SMiri Korenblit 
45158a919a78SMiri Korenblit 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
45168a919a78SMiri Korenblit 						switching_chanctx, &ret))
45178a919a78SMiri Korenblit 		goto out;
4518e705c121SKalle Valo 
4519e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4520e705c121SKalle Valo 	if (ret)
4521e705c121SKalle Valo 		goto out;
4522e705c121SKalle Valo 
4523e705c121SKalle Valo 	/*
4524e705c121SKalle Valo 	 * Power state must be updated before quotas,
4525e705c121SKalle Valo 	 * otherwise fw will complain.
4526e705c121SKalle Valo 	 */
4527e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4528e705c121SKalle Valo 
4529e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4530e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4531e705c121SKalle Valo 	 * will handle quota settings.
4532e705c121SKalle Valo 	 */
4533e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4534e705c121SKalle Valo 		mvmvif->monitor_active = true;
4535e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4536e705c121SKalle Valo 		if (ret)
4537e705c121SKalle Valo 			goto out_remove_binding;
45380e39eb03SChaya Rachel Ivgi 
45390e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
45400e39eb03SChaya Rachel Ivgi 		if (ret)
45410e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
45420e39eb03SChaya Rachel Ivgi 
4543e705c121SKalle Valo 	}
4544e705c121SKalle Valo 
4545e705c121SKalle Valo 	/* Handle binding during CSA */
4546e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4547e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4548e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4549e705c121SKalle Valo 	}
4550e705c121SKalle Valo 
45518a919a78SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION) {
45528a919a78SMiri Korenblit 		if (!switching_chanctx) {
45538a919a78SMiri Korenblit 			mvmvif->csa_bcn_pending = false;
45548a919a78SMiri Korenblit 			goto out;
45558a919a78SMiri Korenblit 		}
45568a919a78SMiri Korenblit 
455774a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
455874a10252SSara Sharon 
455974a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
456074a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
456119125cb0SAndrei Otcheretianski 			u32 duration = 3 * vif->bss_conf.beacon_int;
4562e705c121SKalle Valo 
4563e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4564e705c121SKalle Valo 			 * beacon on the new channel.
4565e705c121SKalle Valo 			 */
4566e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4567e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4568e705c121SKalle Valo 						true);
456974a10252SSara Sharon 		}
4570e705c121SKalle Valo 
4571e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4572e705c121SKalle Valo 	}
4573e705c121SKalle Valo 
4574e705c121SKalle Valo 	goto out;
4575e705c121SKalle Valo 
4576e705c121SKalle Valo out_remove_binding:
4577e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4578e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4579e705c121SKalle Valo out:
4580e705c121SKalle Valo 	if (ret)
4581e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
4582e705c121SKalle Valo 	return ret;
4583e705c121SKalle Valo }
45848a919a78SMiri Korenblit 
4585e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4586e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4587727eff4dSGregory Greenman 				      struct ieee80211_bss_conf *link_conf,
4588e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4589e705c121SKalle Valo {
4590e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4591e705c121SKalle Valo 	int ret;
4592e705c121SKalle Valo 
4593e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4594e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
4595e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4596e705c121SKalle Valo 
4597e705c121SKalle Valo 	return ret;
4598e705c121SKalle Valo }
4599e705c121SKalle Valo 
4600daddfae5SMiri Korenblit /*
4601daddfae5SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
4602daddfae5SMiri Korenblit  *
4603daddfae5SMiri Korenblit  * Returns if chanctx unassign chanctx is done
4604daddfae5SMiri Korenblit  * (either on failure or success)
4605daddfae5SMiri Korenblit  */
46066f71e90eSMiri Korenblit bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
4607daddfae5SMiri Korenblit 					   struct ieee80211_vif *vif,
4608daddfae5SMiri Korenblit 					   bool switching_chanctx)
4609daddfae5SMiri Korenblit {
4610daddfae5SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4611daddfae5SMiri Korenblit 
4612daddfae5SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4613daddfae5SMiri Korenblit 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4614daddfae5SMiri Korenblit 
4615daddfae5SMiri Korenblit 	switch (vif->type) {
4616daddfae5SMiri Korenblit 	case NL80211_IFTYPE_ADHOC:
4617daddfae5SMiri Korenblit 		return true;
4618daddfae5SMiri Korenblit 	case NL80211_IFTYPE_MONITOR:
4619daddfae5SMiri Korenblit 		mvmvif->monitor_active = false;
4620daddfae5SMiri Korenblit 		mvmvif->ps_disabled = false;
4621daddfae5SMiri Korenblit 		break;
4622daddfae5SMiri Korenblit 	case NL80211_IFTYPE_AP:
4623daddfae5SMiri Korenblit 		/* This part is triggered only during CSA */
4624daddfae5SMiri Korenblit 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
4625daddfae5SMiri Korenblit 			return true;
4626daddfae5SMiri Korenblit 
4627daddfae5SMiri Korenblit 		mvmvif->csa_countdown = false;
4628daddfae5SMiri Korenblit 
4629daddfae5SMiri Korenblit 		/* Save blocked iface, the timeout is set on the next beacon */
4630daddfae5SMiri Korenblit 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4631daddfae5SMiri Korenblit 
4632daddfae5SMiri Korenblit 		mvmvif->ap_ibss_active = false;
4633daddfae5SMiri Korenblit 		break;
4634daddfae5SMiri Korenblit 	default:
4635daddfae5SMiri Korenblit 		break;
4636daddfae5SMiri Korenblit 	}
4637daddfae5SMiri Korenblit 	return false;
4638daddfae5SMiri Korenblit }
4639daddfae5SMiri Korenblit 
4640e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4641e705c121SKalle Valo 					   struct ieee80211_vif *vif,
4642e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
4643e705c121SKalle Valo 					   bool switching_chanctx)
4644e705c121SKalle Valo {
4645e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4646e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
4647e705c121SKalle Valo 
4648daddfae5SMiri Korenblit 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
4649e705c121SKalle Valo 		goto out;
4650daddfae5SMiri Korenblit 
4651daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_MONITOR)
46520e39eb03SChaya Rachel Ivgi 		iwl_mvm_rm_snif_sta(mvm, vif);
4653e705c121SKalle Valo 
4654daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP)
4655e705c121SKalle Valo 		/* Set CS bit on all the stations */
4656e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4657e705c121SKalle Valo 
4658daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
4659e705c121SKalle Valo 		disabled_vif = vif;
466074a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
466174a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4662e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
4663e705c121SKalle Valo 	}
4664e705c121SKalle Valo 
4665e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
4666e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4667e705c121SKalle Valo 
4668e705c121SKalle Valo out:
4669bf544e9aSSara Sharon 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
4670bf544e9aSSara Sharon 	    switching_chanctx)
4671bf544e9aSSara Sharon 		return;
4672e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
4673e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4674e705c121SKalle Valo }
4675e705c121SKalle Valo 
4676e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4677e705c121SKalle Valo 					 struct ieee80211_vif *vif,
4678727eff4dSGregory Greenman 					 struct ieee80211_bss_conf *link_conf,
4679e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
4680e705c121SKalle Valo {
4681e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4682e705c121SKalle Valo 
4683e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4684e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
4685e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4686e705c121SKalle Valo }
4687e705c121SKalle Valo 
4688e705c121SKalle Valo static int
4689e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4690e705c121SKalle Valo 				struct ieee80211_vif_chanctx_switch *vifs)
4691e705c121SKalle Valo {
4692e705c121SKalle Valo 	int ret;
4693e705c121SKalle Valo 
4694e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4695e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4696e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4697e705c121SKalle Valo 
4698e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4699e705c121SKalle Valo 	if (ret) {
4700e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4701e705c121SKalle Valo 		goto out_reassign;
4702e705c121SKalle Valo 	}
4703e705c121SKalle Valo 
4704e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4705e705c121SKalle Valo 					   true);
4706e705c121SKalle Valo 	if (ret) {
4707e705c121SKalle Valo 		IWL_ERR(mvm,
4708e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4709e705c121SKalle Valo 		goto out_remove;
4710e705c121SKalle Valo 	}
4711e705c121SKalle Valo 
4712e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
4713e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
4714e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
4715e705c121SKalle Valo 
4716e705c121SKalle Valo 	goto out;
4717e705c121SKalle Valo 
4718e705c121SKalle Valo out_remove:
4719e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4720e705c121SKalle Valo 
4721e705c121SKalle Valo out_reassign:
4722e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
4723e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4724e705c121SKalle Valo 		goto out_restart;
4725e705c121SKalle Valo 	}
4726e705c121SKalle Valo 
4727e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4728e705c121SKalle Valo 					 true)) {
4729e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4730e705c121SKalle Valo 		goto out_restart;
4731e705c121SKalle Valo 	}
4732e705c121SKalle Valo 
4733e705c121SKalle Valo 	goto out;
4734e705c121SKalle Valo 
4735e705c121SKalle Valo out_restart:
4736e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4737e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4738e705c121SKalle Valo 
4739e705c121SKalle Valo out:
4740e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4741e705c121SKalle Valo 
4742e705c121SKalle Valo 	return ret;
4743e705c121SKalle Valo }
4744e705c121SKalle Valo 
4745e705c121SKalle Valo static int
4746e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4747e705c121SKalle Valo 				    struct ieee80211_vif_chanctx_switch *vifs)
4748e705c121SKalle Valo {
4749e705c121SKalle Valo 	int ret;
4750e705c121SKalle Valo 
4751e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4752e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4753e705c121SKalle Valo 
4754e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4755e705c121SKalle Valo 					   true);
4756e705c121SKalle Valo 	if (ret) {
4757e705c121SKalle Valo 		IWL_ERR(mvm,
4758e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4759e705c121SKalle Valo 		goto out_reassign;
4760e705c121SKalle Valo 	}
4761e705c121SKalle Valo 
4762e705c121SKalle Valo 	goto out;
4763e705c121SKalle Valo 
4764e705c121SKalle Valo out_reassign:
4765e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4766e705c121SKalle Valo 					 true)) {
4767e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4768e705c121SKalle Valo 		goto out_restart;
4769e705c121SKalle Valo 	}
4770e705c121SKalle Valo 
4771e705c121SKalle Valo 	goto out;
4772e705c121SKalle Valo 
4773e705c121SKalle Valo out_restart:
4774e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4775e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4776e705c121SKalle Valo 
4777e705c121SKalle Valo out:
4778e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4779e705c121SKalle Valo 
4780e705c121SKalle Valo 	return ret;
4781e705c121SKalle Valo }
4782e705c121SKalle Valo 
4783e705c121SKalle Valo static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4784e705c121SKalle Valo 				      struct ieee80211_vif_chanctx_switch *vifs,
4785e705c121SKalle Valo 				      int n_vifs,
4786e705c121SKalle Valo 				      enum ieee80211_chanctx_switch_mode mode)
4787e705c121SKalle Valo {
4788e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4789e705c121SKalle Valo 	int ret;
4790e705c121SKalle Valo 
4791e705c121SKalle Valo 	/* we only support a single-vif right now */
4792e705c121SKalle Valo 	if (n_vifs > 1)
4793e705c121SKalle Valo 		return -EOPNOTSUPP;
4794e705c121SKalle Valo 
4795e705c121SKalle Valo 	switch (mode) {
4796e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
4797e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4798e705c121SKalle Valo 		break;
4799e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
4800e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
4801e705c121SKalle Valo 		break;
4802e705c121SKalle Valo 	default:
4803e705c121SKalle Valo 		ret = -EOPNOTSUPP;
4804e705c121SKalle Valo 		break;
4805e705c121SKalle Valo 	}
4806e705c121SKalle Valo 
4807e705c121SKalle Valo 	return ret;
4808e705c121SKalle Valo }
4809e705c121SKalle Valo 
48102f0282dbSJohannes Berg static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
48112f0282dbSJohannes Berg {
48122f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
48132f0282dbSJohannes Berg 
48142f0282dbSJohannes Berg 	return mvm->ibss_manager;
48152f0282dbSJohannes Berg }
48162f0282dbSJohannes Berg 
4817e705c121SKalle Valo static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4818e705c121SKalle Valo 			   struct ieee80211_sta *sta,
4819e705c121SKalle Valo 			   bool set)
4820e705c121SKalle Valo {
4821e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4822e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4823e705c121SKalle Valo 
4824e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
4825e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
4826e705c121SKalle Valo 		return -EINVAL;
4827e705c121SKalle Valo 	}
4828e705c121SKalle Valo 
4829e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4830e705c121SKalle Valo }
4831e705c121SKalle Valo 
4832e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
4833e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4834e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4835e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
4836e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
4837e705c121SKalle Valo };
4838e705c121SKalle Valo 
4839e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4840e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4841e705c121SKalle Valo 				      void *data, int len)
4842e705c121SKalle Valo {
4843e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4844e705c121SKalle Valo 	int err;
4845e705c121SKalle Valo 	u32 noa_duration;
4846e705c121SKalle Valo 
48478cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
48488cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
4849e705c121SKalle Valo 	if (err)
4850e705c121SKalle Valo 		return err;
4851e705c121SKalle Valo 
4852e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
4853e705c121SKalle Valo 		return -EINVAL;
4854e705c121SKalle Valo 
4855e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4856e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
4857e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4858e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
4859e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4860e705c121SKalle Valo 			return -EINVAL;
4861e705c121SKalle Valo 
4862e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4863e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
4864e705c121SKalle Valo 			return -EINVAL;
4865e705c121SKalle Valo 
4866e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
4867e705c121SKalle Valo 		mvm->noa_vif = vif;
4868e705c121SKalle Valo 
486922b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
4870e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4871e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
4872e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4873f276e20bSJohannes Berg 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
4874e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4875e705c121SKalle Valo 			return -EINVAL;
4876e705c121SKalle Valo 
4877e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
4878e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4879e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4880e705c121SKalle Valo 	}
4881e705c121SKalle Valo 
4882e705c121SKalle Valo 	return -EOPNOTSUPP;
4883e705c121SKalle Valo }
4884e705c121SKalle Valo 
4885e705c121SKalle Valo static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4886e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4887e705c121SKalle Valo 				    void *data, int len)
4888e705c121SKalle Valo {
4889e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4890e705c121SKalle Valo 	int err;
4891e705c121SKalle Valo 
4892e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4893e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4894e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4895e705c121SKalle Valo 
4896e705c121SKalle Valo 	return err;
4897e705c121SKalle Valo }
4898e705c121SKalle Valo #endif
4899e705c121SKalle Valo 
4900e705c121SKalle Valo static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4901e705c121SKalle Valo 				   struct ieee80211_vif *vif,
4902e705c121SKalle Valo 				   struct ieee80211_channel_switch *chsw)
4903e705c121SKalle Valo {
4904e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
4905e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
4906e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
4907e705c121SKalle Valo 	 * (which is when the absence time event starts).
4908e705c121SKalle Valo 	 */
4909e705c121SKalle Valo 
4910e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4911e705c121SKalle Valo 			   "dummy channel switch op\n");
4912e705c121SKalle Valo }
4913e705c121SKalle Valo 
491474a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
491574a10252SSara Sharon 				       struct ieee80211_vif *vif,
491674a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
491774a10252SSara Sharon {
491874a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
491974a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
492074a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
492174a10252SSara Sharon 							  mvmvif->color)),
492274a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
492374a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
492474a10252SSara Sharon 		.cs_count = chsw->count,
492577738865SSara Sharon 		.cs_mode = chsw->block_tx,
492674a10252SSara Sharon 	};
492774a10252SSara Sharon 
492874a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
492974a10252SSara Sharon 
49309cfcf71cSSara Sharon 	if (chsw->delay)
49319cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
49329cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
49339cfcf71cSSara Sharon 
493474a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
493574a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
493674a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
493774a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
493874a10252SSara Sharon }
493974a10252SSara Sharon 
49400202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
49410202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
49420202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
49430202bcf0SEmmanuel Grumbach {
49440202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
49450202bcf0SEmmanuel Grumbach 	u32 apply_time;
49460202bcf0SEmmanuel Grumbach 
49470202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
49480202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
49490202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
49500202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
49510202bcf0SEmmanuel Grumbach 	 * loss).
49520202bcf0SEmmanuel Grumbach 	 */
49530202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
49540202bcf0SEmmanuel Grumbach 		apply_time = 0;
49550202bcf0SEmmanuel Grumbach 	else
49560202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
49570202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
49580202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
49590202bcf0SEmmanuel Grumbach 
49600202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
49610202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
49620202bcf0SEmmanuel Grumbach 
49630202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
49640202bcf0SEmmanuel Grumbach 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
49650202bcf0SEmmanuel Grumbach 
49660202bcf0SEmmanuel Grumbach 		if (ret)
49670202bcf0SEmmanuel Grumbach 			return ret;
49680202bcf0SEmmanuel Grumbach 	}
49690202bcf0SEmmanuel Grumbach 
49700202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
49710202bcf0SEmmanuel Grumbach 				    apply_time);
49720202bcf0SEmmanuel Grumbach 
49730202bcf0SEmmanuel Grumbach 	return 0;
49740202bcf0SEmmanuel Grumbach }
49750202bcf0SEmmanuel Grumbach 
4976f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
4977e705c121SKalle Valo static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4978e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4979e705c121SKalle Valo 				      struct ieee80211_channel_switch *chsw)
4980e705c121SKalle Valo {
4981e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4982e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
4983e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4984e705c121SKalle Valo 	int ret;
4985e705c121SKalle Valo 
4986e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4987e705c121SKalle Valo 
4988e705c121SKalle Valo 	mvmvif->csa_failed = false;
4989e705c121SKalle Valo 
4990e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
4991e705c121SKalle Valo 			   chsw->chandef.center_freq1);
4992e705c121SKalle Valo 
49937174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
49947174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
49957174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
4996e705c121SKalle Valo 
4997e705c121SKalle Valo 	switch (vif->type) {
4998e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4999e705c121SKalle Valo 		csa_vif =
5000e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
5001e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
5002d0a9123eSJohannes Berg 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5003e705c121SKalle Valo 			      "Another CSA is already in progress")) {
5004e705c121SKalle Valo 			ret = -EBUSY;
5005e705c121SKalle Valo 			goto out_unlock;
5006e705c121SKalle Valo 		}
5007e705c121SKalle Valo 
5008d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
5009d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5010d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
5011d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
5012d3a108a4SAndrei Otcheretianski 			goto out_unlock;
5013d3a108a4SAndrei Otcheretianski 		}
5014d3a108a4SAndrei Otcheretianski 
5015e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
5016e705c121SKalle Valo 
5017e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
5018e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
5019e705c121SKalle Valo 			ret = -EBUSY;
5020e705c121SKalle Valo 			goto out_unlock;
5021e705c121SKalle Valo 		}
5022e705c121SKalle Valo 
5023d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5024d3a108a4SAndrei Otcheretianski 
5025e705c121SKalle Valo 		break;
5026e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
50272360acbdSEmmanuel Grumbach 		/*
5028ad12b231SNathan Errera 		 * In the new flow FW is in charge of timing the switch so there
5029ad12b231SNathan Errera 		 * is no need for all of this
5030ad12b231SNathan Errera 		 */
5031ad12b231SNathan Errera 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5032ad12b231SNathan Errera 					    CHANNEL_SWITCH_ERROR_NOTIF,
5033ad12b231SNathan Errera 					    0))
5034ad12b231SNathan Errera 			break;
5035ad12b231SNathan Errera 
5036ad12b231SNathan Errera 		/*
50372360acbdSEmmanuel Grumbach 		 * We haven't configured the firmware to be associated yet since
50382360acbdSEmmanuel Grumbach 		 * we don't know the dtim period. In this case, the firmware can't
50392360acbdSEmmanuel Grumbach 		 * track the beacons.
50402360acbdSEmmanuel Grumbach 		 */
5041f276e20bSJohannes Berg 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period) {
50422360acbdSEmmanuel Grumbach 			ret = -EBUSY;
50432360acbdSEmmanuel Grumbach 			goto out_unlock;
50442360acbdSEmmanuel Grumbach 		}
50452360acbdSEmmanuel Grumbach 
504687d9564eSJohannes Berg 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX)
504787d9564eSJohannes Berg 			schedule_delayed_work(&mvmvif->csa_work, 0);
504887d9564eSJohannes Berg 
5049f6780614SSara Sharon 		if (chsw->block_tx) {
5050f6780614SSara Sharon 			/*
5051f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
5052f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
5053f6780614SSara Sharon 			 */
5054f6780614SSara Sharon 			if (!chsw->count ||
5055f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
5056f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
5057f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
5058f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5059f6780614SSara Sharon 		}
5060e705c121SKalle Valo 
50610202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
50620202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
50630202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5064e705c121SKalle Valo 			if (ret)
5065e705c121SKalle Valo 				goto out_unlock;
50660202bcf0SEmmanuel Grumbach 		} else {
506774a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
50680202bcf0SEmmanuel Grumbach 		}
506981b4e44eSSara Sharon 
507081b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
507181b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
5072e705c121SKalle Valo 		break;
5073e705c121SKalle Valo 	default:
5074e705c121SKalle Valo 		break;
5075e705c121SKalle Valo 	}
5076e705c121SKalle Valo 
5077e705c121SKalle Valo 	mvmvif->ps_disabled = true;
5078e705c121SKalle Valo 
5079e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
5080e705c121SKalle Valo 	if (ret)
5081e705c121SKalle Valo 		goto out_unlock;
5082e705c121SKalle Valo 
5083e705c121SKalle Valo 	/* we won't be on this channel any longer */
5084e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
5085e705c121SKalle Valo 
5086e705c121SKalle Valo out_unlock:
5087e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5088e705c121SKalle Valo 
5089e705c121SKalle Valo 	return ret;
5090e705c121SKalle Valo }
5091e705c121SKalle Valo 
5092c37763d2SSara Sharon static void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5093c37763d2SSara Sharon 					     struct ieee80211_vif *vif,
5094c37763d2SSara Sharon 					     struct ieee80211_channel_switch *chsw)
5095e705c121SKalle Valo {
5096e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5097c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5098c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
5099c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5100c37763d2SSara Sharon 							  mvmvif->color)),
5101c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5102c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
5103c37763d2SSara Sharon 		.cs_count = chsw->count,
510477738865SSara Sharon 		.cs_mode = chsw->block_tx,
5105c37763d2SSara Sharon 	};
5106e705c121SKalle Valo 
5107ad12b231SNathan Errera 	/*
5108ad12b231SNathan Errera 	 * In the new flow FW is in charge of timing the switch so there is no
5109ad12b231SNathan Errera 	 * need for all of this
5110ad12b231SNathan Errera 	 */
5111ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5112ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5113ad12b231SNathan Errera 		return;
5114ad12b231SNathan Errera 
5115c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5116c37763d2SSara Sharon 		return;
5117e705c121SKalle Valo 
5118c3eae059SGregory Greenman 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5119c3eae059SGregory Greenman 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5120c3eae059SGregory Greenman 
512181b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
512281b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
512381b4e44eSSara Sharon 			/* Second time, give up on this AP*/
512481b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
512581b4e44eSSara Sharon 			ieee80211_chswitch_done(vif, false);
512681b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
512781b4e44eSSara Sharon 			return;
5128e705c121SKalle Valo 		}
512981b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
5130e705c121SKalle Valo 	}
513181b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
5132e705c121SKalle Valo 
5133caf46377SSara Sharon 	mutex_lock(&mvm->mutex);
5134caf46377SSara Sharon 	if (mvmvif->csa_failed)
5135caf46377SSara Sharon 		goto out_unlock;
5136e705c121SKalle Valo 
5137c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5138c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
5139c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5140caf46377SSara Sharon 				     0, sizeof(cmd), &cmd));
5141caf46377SSara Sharon out_unlock:
5142caf46377SSara Sharon 	mutex_unlock(&mvm->mutex);
5143e705c121SKalle Valo }
5144e705c121SKalle Valo 
51456110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
51466110d9e5SDavid Spinadel {
5147435d0827SSara Sharon 	int i;
5148435d0827SSara Sharon 
514906195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5150309c4848SLuca Coelho 		if (drop) {
5151309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
51526110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
5153d4e3a341SMordechay Goodstein 				iwl_mvm_flushable_queues(mvm) & queues);
5154309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
5155309c4848SLuca Coelho 		} else {
5156435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5157309c4848SLuca Coelho 		}
5158435d0827SSara Sharon 		return;
5159435d0827SSara Sharon 	}
51606110d9e5SDavid Spinadel 
51616110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
5162be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5163435d0827SSara Sharon 		struct ieee80211_sta *sta;
5164435d0827SSara Sharon 
5165435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
51666110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
51676110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
51686110d9e5SDavid Spinadel 			continue;
51696110d9e5SDavid Spinadel 
517006195639SSara Sharon 		if (drop)
5171d4e3a341SMordechay Goodstein 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
517206195639SSara Sharon 		else
51736110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
51746110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
51756110d9e5SDavid Spinadel 	}
51766110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
51776110d9e5SDavid Spinadel }
51786110d9e5SDavid Spinadel 
5179e705c121SKalle Valo static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
5180e705c121SKalle Valo 			      struct ieee80211_vif *vif, u32 queues, bool drop)
5181e705c121SKalle Valo {
5182e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5183e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
5184e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
5185e705c121SKalle Valo 	struct ieee80211_sta *sta;
5186e705c121SKalle Valo 	int i;
5187e705c121SKalle Valo 	u32 msk = 0;
5188e705c121SKalle Valo 
51896110d9e5SDavid Spinadel 	if (!vif) {
51906110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
51916110d9e5SDavid Spinadel 		return;
51926110d9e5SDavid Spinadel 	}
51936110d9e5SDavid Spinadel 
51946110d9e5SDavid Spinadel 	if (vif->type != NL80211_IFTYPE_STATION)
5195e705c121SKalle Valo 		return;
5196e705c121SKalle Valo 
519724afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
519824afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
519924afba76SLiad Kaufman 
5200e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5201e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
5202e705c121SKalle Valo 
5203e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
5204be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5205e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5206e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
5207e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
5208e705c121SKalle Valo 			continue;
5209e705c121SKalle Valo 
5210e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5211e705c121SKalle Valo 		if (mvmsta->vif != vif)
5212e705c121SKalle Valo 			continue;
5213e705c121SKalle Valo 
5214e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
5215e705c121SKalle Valo 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
5216e705c121SKalle Valo 
5217d49394a1SSara Sharon 		if (drop) {
5218f9084775SNathan Errera 			if (iwl_mvm_flush_sta(mvm, mvmsta, false))
5219d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
5220d49394a1SSara Sharon 		} else {
5221d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
5222d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5223f7bd883bSJohannes Berg 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5224f7bd883bSJohannes Berg 				msk |= mvmsta->tfd_queue_msk;
5225e705c121SKalle Valo 		}
5226d49394a1SSara Sharon 	}
5227e705c121SKalle Valo 
5228e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5229e705c121SKalle Valo 
5230e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
5231e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
5232e705c121SKalle Valo 	 */
5233d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5234a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5235e705c121SKalle Valo }
5236e705c121SKalle Valo 
5237e705c121SKalle Valo static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5238e705c121SKalle Valo 				  struct survey_info *survey)
5239e705c121SKalle Valo {
5240e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5241e705c121SKalle Valo 	int ret;
5242e705c121SKalle Valo 
5243e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
5244e705c121SKalle Valo 
5245e705c121SKalle Valo 	/* only support global statistics right now */
5246e705c121SKalle Valo 	if (idx != 0)
5247e705c121SKalle Valo 		return -ENOENT;
5248e705c121SKalle Valo 
5249280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
5250e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
5251e705c121SKalle Valo 		return -ENOENT;
5252e705c121SKalle Valo 
5253e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5254e705c121SKalle Valo 
5255aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
5256e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
5257e705c121SKalle Valo 		if (ret)
5258e705c121SKalle Valo 			goto out;
5259e705c121SKalle Valo 	}
5260e705c121SKalle Valo 
5261e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
5262e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
5263e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
5264e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
5265e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
5266e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
5267e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
5268e705c121SKalle Valo 
5269e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
5270e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
5271e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
5272e705c121SKalle Valo 
5273e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
5274e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
5275e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
5276e705c121SKalle Valo 
5277e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
5278e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
5279e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
5280e705c121SKalle Valo 
5281e705c121SKalle Valo 	ret = 0;
5282e705c121SKalle Valo  out:
5283e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5284e705c121SKalle Valo 	return ret;
5285e705c121SKalle Valo }
5286e705c121SKalle Valo 
5287ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
5288ed780545SJohannes Berg {
528982cdbd11SMiri Korenblit 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
52907138763eSJohannes Berg 	u32 gi_ltf;
529182cdbd11SMiri Korenblit 
529282cdbd11SMiri Korenblit 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
5293ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
5294ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
5295ed780545SJohannes Berg 		break;
5296ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
5297ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
5298ed780545SJohannes Berg 		break;
5299ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
5300ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
5301ed780545SJohannes Berg 		break;
5302ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
5303ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
5304ed780545SJohannes Berg 		break;
530523dcee94SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_320:
530623dcee94SJohannes Berg 		rinfo->bw = RATE_INFO_BW_320;
530723dcee94SJohannes Berg 		break;
5308ed780545SJohannes Berg 	}
5309ed780545SJohannes Berg 
531082cdbd11SMiri Korenblit 	if (format == RATE_MCS_CCK_MSK ||
531182cdbd11SMiri Korenblit 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
531282cdbd11SMiri Korenblit 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
531382cdbd11SMiri Korenblit 
531482cdbd11SMiri Korenblit 		/* add the offset needed to get to the legacy ofdm indices */
531582cdbd11SMiri Korenblit 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
531682cdbd11SMiri Korenblit 			rate += IWL_FIRST_OFDM_RATE;
531782cdbd11SMiri Korenblit 
531882cdbd11SMiri Korenblit 		switch (rate) {
531982cdbd11SMiri Korenblit 		case IWL_RATE_1M_INDEX:
532082cdbd11SMiri Korenblit 			rinfo->legacy = 10;
532182cdbd11SMiri Korenblit 			break;
532282cdbd11SMiri Korenblit 		case IWL_RATE_2M_INDEX:
532382cdbd11SMiri Korenblit 			rinfo->legacy = 20;
532482cdbd11SMiri Korenblit 			break;
532582cdbd11SMiri Korenblit 		case IWL_RATE_5M_INDEX:
532682cdbd11SMiri Korenblit 			rinfo->legacy = 55;
532782cdbd11SMiri Korenblit 			break;
532882cdbd11SMiri Korenblit 		case IWL_RATE_11M_INDEX:
532982cdbd11SMiri Korenblit 			rinfo->legacy = 110;
533082cdbd11SMiri Korenblit 			break;
533182cdbd11SMiri Korenblit 		case IWL_RATE_6M_INDEX:
533282cdbd11SMiri Korenblit 			rinfo->legacy = 60;
533382cdbd11SMiri Korenblit 			break;
533482cdbd11SMiri Korenblit 		case IWL_RATE_9M_INDEX:
533582cdbd11SMiri Korenblit 			rinfo->legacy = 90;
533682cdbd11SMiri Korenblit 			break;
533782cdbd11SMiri Korenblit 		case IWL_RATE_12M_INDEX:
533882cdbd11SMiri Korenblit 			rinfo->legacy = 120;
533982cdbd11SMiri Korenblit 			break;
534082cdbd11SMiri Korenblit 		case IWL_RATE_18M_INDEX:
534182cdbd11SMiri Korenblit 			rinfo->legacy = 180;
534282cdbd11SMiri Korenblit 			break;
534382cdbd11SMiri Korenblit 		case IWL_RATE_24M_INDEX:
534482cdbd11SMiri Korenblit 			rinfo->legacy = 240;
534582cdbd11SMiri Korenblit 			break;
534682cdbd11SMiri Korenblit 		case IWL_RATE_36M_INDEX:
534782cdbd11SMiri Korenblit 			rinfo->legacy = 360;
534882cdbd11SMiri Korenblit 			break;
534982cdbd11SMiri Korenblit 		case IWL_RATE_48M_INDEX:
535082cdbd11SMiri Korenblit 			rinfo->legacy = 480;
535182cdbd11SMiri Korenblit 			break;
535282cdbd11SMiri Korenblit 		case IWL_RATE_54M_INDEX:
535382cdbd11SMiri Korenblit 			rinfo->legacy = 540;
535482cdbd11SMiri Korenblit 		}
535582cdbd11SMiri Korenblit 		return;
535682cdbd11SMiri Korenblit 	}
535782cdbd11SMiri Korenblit 
5358ed780545SJohannes Berg 	rinfo->nss = u32_get_bits(rate_n_flags,
535982cdbd11SMiri Korenblit 				  RATE_MCS_NSS_MSK) + 1;
536082cdbd11SMiri Korenblit 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
536182cdbd11SMiri Korenblit 		RATE_HT_MCS_INDEX(rate_n_flags) :
536282cdbd11SMiri Korenblit 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
536382cdbd11SMiri Korenblit 
53647138763eSJohannes Berg 	if (rate_n_flags & RATE_MCS_SGI_MSK)
53657138763eSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
53667138763eSJohannes Berg 
53677138763eSJohannes Berg 	switch (format) {
536823dcee94SJohannes Berg 	case RATE_MCS_EHT_MSK:
536923dcee94SJohannes Berg 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
537023dcee94SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
537123dcee94SJohannes Berg 		break;
53727138763eSJohannes Berg 	case RATE_MCS_HE_MSK:
53737138763eSJohannes Berg 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
5374ed780545SJohannes Berg 
5375ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
5376ed780545SJohannes Berg 
537782cdbd11SMiri Korenblit 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
5378ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
5379ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
5380ed780545SJohannes Berg 		}
5381ed780545SJohannes Berg 
538282cdbd11SMiri Korenblit 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
538382cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_SU:
538482cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_EXT_SU:
5385ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5386ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5387ed780545SJohannes Berg 			else if (gi_ltf == 2)
5388ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
538982cdbd11SMiri Korenblit 			else if (gi_ltf == 3)
5390ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
539182cdbd11SMiri Korenblit 			else
539282cdbd11SMiri Korenblit 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5393ed780545SJohannes Berg 			break;
539482cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_MU:
5395ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5396ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5397ed780545SJohannes Berg 			else if (gi_ltf == 2)
5398ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5399ed780545SJohannes Berg 			else
5400ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5401ed780545SJohannes Berg 			break;
540282cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_TRIG:
5403ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5404ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5405ed780545SJohannes Berg 			else
5406ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5407ed780545SJohannes Berg 			break;
5408ed780545SJohannes Berg 		}
5409ed780545SJohannes Berg 
5410ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
5411ed780545SJohannes Berg 			rinfo->he_dcm = 1;
54127138763eSJohannes Berg 		break;
54137138763eSJohannes Berg 	case RATE_MCS_HT_MSK:
541482cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
54157138763eSJohannes Berg 		break;
54167138763eSJohannes Berg 	case RATE_MCS_VHT_MSK:
541782cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
54187138763eSJohannes Berg 		break;
5419ed780545SJohannes Berg 	}
5420ed780545SJohannes Berg }
5421ed780545SJohannes Berg 
5422e705c121SKalle Valo static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
5423e705c121SKalle Valo 				       struct ieee80211_vif *vif,
5424e705c121SKalle Valo 				       struct ieee80211_sta *sta,
5425e705c121SKalle Valo 				       struct station_info *sinfo)
5426e705c121SKalle Valo {
5427e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5428e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5429e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5430e705c121SKalle Valo 
5431988b5968SSara Sharon 	if (mvmsta->avg_energy) {
543288ad368aSBen Greear 		sinfo->signal_avg = -(s8)mvmsta->avg_energy;
543322d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
5434988b5968SSara Sharon 	}
5435988b5968SSara Sharon 
5436ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
5437ed780545SJohannes Berg 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
5438ed780545SJohannes Berg 
5439ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5440ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5441ed780545SJohannes Berg 	}
5442ed780545SJohannes Berg 
5443e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
5444e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5445e705c121SKalle Valo 		return;
5446e705c121SKalle Valo 
5447f276e20bSJohannes Berg 	if (!vif->cfg.assoc)
5448e705c121SKalle Valo 		return;
5449e705c121SKalle Valo 
5450e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5451e705c121SKalle Valo 
5452e705c121SKalle Valo 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
5453e705c121SKalle Valo 		goto unlock;
5454e705c121SKalle Valo 
5455e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
5456e705c121SKalle Valo 		goto unlock;
5457e705c121SKalle Valo 
5458e705c121SKalle Valo 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
5459e705c121SKalle Valo 			   mvmvif->beacon_stats.accu_num_beacons;
546022d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
5461e705c121SKalle Valo 	if (mvmvif->beacon_stats.avg_signal) {
5462e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
5463e705c121SKalle Valo 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
546422d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
5465e705c121SKalle Valo 	}
5466e705c121SKalle Valo  unlock:
5467e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5468e705c121SKalle Valo }
5469e705c121SKalle Valo 
5470119c2a13SMordechay Goodstein static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5471119c2a13SMordechay Goodstein 					    struct ieee80211_vif *vif,
5472119c2a13SMordechay Goodstein 					    const  struct ieee80211_mlme_event *mlme)
5473119c2a13SMordechay Goodstein {
54741a81bddfSMordechay Goodstein 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
54751a81bddfSMordechay Goodstein 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
5476119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5477119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5478119c2a13SMordechay Goodstein 				       NULL);
5479119c2a13SMordechay Goodstein 		return;
5480119c2a13SMordechay Goodstein 	}
5481119c2a13SMordechay Goodstein 
5482119c2a13SMordechay Goodstein 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5483119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5484119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_DEASSOC,
5485119c2a13SMordechay Goodstein 				       NULL);
5486119c2a13SMordechay Goodstein 		return;
5487119c2a13SMordechay Goodstein 	}
5488119c2a13SMordechay Goodstein }
5489119c2a13SMordechay Goodstein 
5490e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5491e705c121SKalle Valo 					struct ieee80211_vif *vif,
5492e705c121SKalle Valo 					const struct ieee80211_event *event)
5493e705c121SKalle Valo {
54944c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5495e705c121SKalle Valo 	do {								\
54964c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5497e705c121SKalle Valo 			break;						\
54987174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5499e705c121SKalle Valo 	} while (0)
5500e705c121SKalle Valo 
5501e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5502e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5503e705c121SKalle Valo 
5504119c2a13SMordechay Goodstein 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5505119c2a13SMordechay Goodstein 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5506119c2a13SMordechay Goodstein 		return;
5507119c2a13SMordechay Goodstein 	}
5508119c2a13SMordechay Goodstein 
55096c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
55106c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
55116c042d75SSara Sharon 	if (!trig)
5512e705c121SKalle Valo 		return;
5513e705c121SKalle Valo 
5514e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
5515e705c121SKalle Valo 
5516e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
5517e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
55184c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
5519e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
5520e705c121SKalle Valo 					    event->u.mlme.reason);
5521e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
55224c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
5523e705c121SKalle Valo 					   "ASSOC TIMEOUT");
5524e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
5525e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
55264c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
5527e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
5528e705c121SKalle Valo 					   event->u.mlme.reason);
5529e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
55304c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
5531e705c121SKalle Valo 					   "AUTH TIMEOUT");
5532e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
55334c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
5534e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
5535e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
55364c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
5537e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
5538e705c121SKalle Valo 	}
5539e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
5540e705c121SKalle Valo }
5541e705c121SKalle Valo 
5542e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5543e705c121SKalle Valo 					  struct ieee80211_vif *vif,
5544e705c121SKalle Valo 					  const struct ieee80211_event *event)
5545e705c121SKalle Valo {
5546e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5547e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
5548e705c121SKalle Valo 
55496c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
55506c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
55516c042d75SSara Sharon 	if (!trig)
5552e705c121SKalle Valo 		return;
5553e705c121SKalle Valo 
5554e705c121SKalle Valo 	ba_trig = (void *)trig->data;
5555e705c121SKalle Valo 
5556e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5557e705c121SKalle Valo 		return;
5558e705c121SKalle Valo 
55597174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
5560e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
5561e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
5562e705c121SKalle Valo 				event->u.ba.ssn);
5563e705c121SKalle Valo }
5564e705c121SKalle Valo 
5565e705c121SKalle Valo static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
5566e705c121SKalle Valo 				       struct ieee80211_vif *vif,
5567e705c121SKalle Valo 				       const struct ieee80211_event *event)
5568e705c121SKalle Valo {
5569e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5570e705c121SKalle Valo 
5571e705c121SKalle Valo 	switch (event->type) {
5572e705c121SKalle Valo 	case MLME_EVENT:
5573e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
5574e705c121SKalle Valo 		break;
5575e705c121SKalle Valo 	case BAR_RX_EVENT:
5576e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
5577e705c121SKalle Valo 		break;
5578e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
5579528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
5580528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
5581e705c121SKalle Valo 		break;
5582e705c121SKalle Valo 	default:
5583e705c121SKalle Valo 		break;
5584e705c121SKalle Valo 	}
5585e705c121SKalle Valo }
5586e705c121SKalle Valo 
5587d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
55885e1688ceSJohannes Berg 				     enum iwl_mvm_rxq_notif_type type,
55895e1688ceSJohannes Berg 				     bool sync,
55905e1688ceSJohannes Berg 				     const void *data, u32 size)
55910636b938SSara Sharon {
55925e1688ceSJohannes Berg 	struct {
55935e1688ceSJohannes Berg 		struct iwl_rxq_sync_cmd cmd;
55945e1688ceSJohannes Berg 		struct iwl_mvm_internal_rxq_notif notif;
55955e1688ceSJohannes Berg 	} __packed cmd = {
55965e1688ceSJohannes Berg 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
55975e1688ceSJohannes Berg 		.cmd.count =
55985e1688ceSJohannes Berg 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
55995e1688ceSJohannes Berg 				    size),
56005e1688ceSJohannes Berg 		.notif.type = type,
56015e1688ceSJohannes Berg 		.notif.sync = sync,
56025e1688ceSJohannes Berg 	};
56035e1688ceSJohannes Berg 	struct iwl_host_cmd hcmd = {
56045e1688ceSJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
56055e1688ceSJohannes Berg 		.data[0] = &cmd,
56065e1688ceSJohannes Berg 		.len[0] = sizeof(cmd),
56075e1688ceSJohannes Berg 		.data[1] = data,
56085e1688ceSJohannes Berg 		.len[1] = size,
56095e1688ceSJohannes Berg 		.flags = sync ? 0 : CMD_ASYNC,
56105e1688ceSJohannes Berg 	};
56110636b938SSara Sharon 	int ret;
56120636b938SSara Sharon 
56135e1688ceSJohannes Berg 	/* size must be a multiple of DWORD */
56145e1688ceSJohannes Berg 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
56155e1688ceSJohannes Berg 		return;
56160636b938SSara Sharon 
5617cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
56180636b938SSara Sharon 		return;
56190636b938SSara Sharon 
56205e1688ceSJohannes Berg 	if (sync) {
56215e1688ceSJohannes Berg 		cmd.notif.cookie = mvm->queue_sync_cookie;
56222f7a04c7SJohannes Berg 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
5623a2113cc4SNaftali Goldstein 	}
5624d0ff5d22SSara Sharon 
56255e1688ceSJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
56260636b938SSara Sharon 	if (ret) {
56270636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
56280636b938SSara Sharon 		goto out;
56290636b938SSara Sharon 	}
5630d0ff5d22SSara Sharon 
56315e1688ceSJohannes Berg 	if (sync) {
56323c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
56333a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
56342f7a04c7SJohannes Berg 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
56356ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
56360636b938SSara Sharon 					 HZ);
56372f7a04c7SJohannes Berg 		WARN_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm),
56382f7a04c7SJohannes Berg 			  "queue sync: failed to sync, state is 0x%lx\n",
56392f7a04c7SJohannes Berg 			  mvm->queue_sync_state);
56406ad04359SJohannes Berg 	}
56410636b938SSara Sharon 
56420636b938SSara Sharon out:
56435e1688ceSJohannes Berg 	if (sync) {
56442f7a04c7SJohannes Berg 		mvm->queue_sync_state = 0;
56450636b938SSara Sharon 		mvm->queue_sync_cookie++;
56460636b938SSara Sharon 	}
56475f8a3561SJohannes Berg }
56480636b938SSara Sharon 
56490636b938SSara Sharon static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
56500636b938SSara Sharon {
56510636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
56520636b938SSara Sharon 
56530636b938SSara Sharon 	mutex_lock(&mvm->mutex);
56545e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
56550636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
56560636b938SSara Sharon }
56570636b938SSara Sharon 
5658b73f9a4aSJohannes Berg static int
5659b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
5660b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
5661b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
5662b73f9a4aSJohannes Berg {
5663b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5664b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5665b73f9a4aSJohannes Berg 
5666b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
5667b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
5668b73f9a4aSJohannes Berg 		return -EINVAL;
5669b73f9a4aSJohannes Berg 
5670b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
5671b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
5672b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
5673b73f9a4aSJohannes Berg 
5674b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
5675b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
5676b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
5677b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
5678b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
5679b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
5680b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
5681b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
5682b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
5683b73f9a4aSJohannes Berg 
5684b73f9a4aSJohannes Berg 	return 0;
5685b73f9a4aSJohannes Berg }
5686b73f9a4aSJohannes Berg 
5687fc36ffdaSJohannes Berg static int iwl_mvm_start_pmsr(struct ieee80211_hw *hw,
5688fc36ffdaSJohannes Berg 			      struct ieee80211_vif *vif,
5689fc36ffdaSJohannes Berg 			      struct cfg80211_pmsr_request *request)
5690fc36ffdaSJohannes Berg {
5691fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5692fc36ffdaSJohannes Berg 	int ret;
5693fc36ffdaSJohannes Berg 
5694fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5695fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
5696fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5697fc36ffdaSJohannes Berg 
5698fc36ffdaSJohannes Berg 	return ret;
5699fc36ffdaSJohannes Berg }
5700fc36ffdaSJohannes Berg 
5701fc36ffdaSJohannes Berg static void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw,
5702fc36ffdaSJohannes Berg 			       struct ieee80211_vif *vif,
5703fc36ffdaSJohannes Berg 			       struct cfg80211_pmsr_request *request)
5704fc36ffdaSJohannes Berg {
5705fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5706fc36ffdaSJohannes Berg 
5707fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5708fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
5709fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5710fc36ffdaSJohannes Berg }
5711fc36ffdaSJohannes Berg 
5712438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
5713438af969SSara Sharon {
5714438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
5715438af969SSara Sharon 
5716438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
5717438af969SSara Sharon 		return false;
5718438af969SSara Sharon 
5719438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
5720438af969SSara Sharon }
5721438af969SSara Sharon 
5722438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
5723438af969SSara Sharon 				      struct sk_buff *head,
5724438af969SSara Sharon 				      struct sk_buff *skb)
5725438af969SSara Sharon {
5726438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5727438af969SSara Sharon 
57280792df68SJohannes Berg 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
57290792df68SJohannes Berg 		return iwl_mvm_tx_csum_bz(mvm, head, true) ==
57300792df68SJohannes Berg 		       iwl_mvm_tx_csum_bz(mvm, skb, true);
57310792df68SJohannes Berg 
5732438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
5733438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
5734438af969SSara Sharon 		return false;
5735438af969SSara Sharon 
5736438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
5737438af969SSara Sharon 		return true;
5738438af969SSara Sharon 
5739438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5740438af969SSara Sharon }
5741438af969SSara Sharon 
5742e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
5743e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
5744cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
5745e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
5746e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
5747e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
5748e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
5749e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
5750e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
5751e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
5752e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
5753e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
5754e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
5755e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
5756e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
5757e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
5758e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
5759e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
5760e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
5761e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
5762e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
5763e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
5764e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
5765e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
5766e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
5767e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
57686b1259d1SJohannes Berg 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
5769e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
5770e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
5771e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
5772e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
5773e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
5774e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
5775e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
5776e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
5777e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
5778e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
5779e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
5780e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5781e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
5782e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
5783e705c121SKalle Valo 
5784ae7ba17bSShaul Triebitz 	.start_ap = iwl_mvm_start_ap,
5785ae7ba17bSShaul Triebitz 	.stop_ap = iwl_mvm_stop_ap,
5786ae7ba17bSShaul Triebitz 	.join_ibss = iwl_mvm_start_ibss,
5787ae7ba17bSShaul Triebitz 	.leave_ibss = iwl_mvm_stop_ibss,
5788e705c121SKalle Valo 
57892f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
57902f0282dbSJohannes Berg 
5791e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
5792e705c121SKalle Valo 
5793e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
5794e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
5795e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
579679221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
5797c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
5798e705c121SKalle Valo 
5799e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5800e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5801e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5802e705c121SKalle Valo 
5803e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
5804e705c121SKalle Valo 
58050636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
58060636b938SSara Sharon 
5807e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5808e705c121SKalle Valo 
5809e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
5810e705c121SKalle Valo 	/* look at d3.c */
5811e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
5812e705c121SKalle Valo 	.resume = iwl_mvm_resume,
5813e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
5814e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
5815e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
5816e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
5817e705c121SKalle Valo #endif
5818e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
5819e705c121SKalle Valo #endif
5820e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
5821e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
5822b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
5823fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
5824fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
5825b73f9a4aSJohannes Berg 
5826438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
5827177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
5828177a11cfSGregory Greenman 	.sta_add_debugfs = iwl_mvm_sta_add_debugfs,
5829177a11cfSGregory Greenman #endif
5830e705c121SKalle Valo };
5831