xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision 1ec7291e247055fab3a088e1a333a31e7c06e2dd)
18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
3207be64fSMiri Korenblit  * Copyright (C) 2012-2014, 2018-2023 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"
31cf85123aSAvraham Stern #include "time-sync.h"
32e705c121SKalle Valo 
33e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
34e705c121SKalle Valo 	{
35e705c121SKalle Valo 		.max = 1,
36e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
37e705c121SKalle Valo 	},
38e705c121SKalle Valo 	{
39e705c121SKalle Valo 		.max = 1,
40e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
41e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
42e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
43e705c121SKalle Valo 	},
44e705c121SKalle Valo 	{
45e705c121SKalle Valo 		.max = 1,
46e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
47e705c121SKalle Valo 	},
48e705c121SKalle Valo };
49e705c121SKalle Valo 
50e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
51e705c121SKalle Valo 	{
52e705c121SKalle Valo 		.num_different_channels = 2,
53e705c121SKalle Valo 		.max_interfaces = 3,
54e705c121SKalle Valo 		.limits = iwl_mvm_limits,
55e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
56e705c121SKalle Valo 	},
57e705c121SKalle Valo };
58e705c121SKalle Valo 
59fc36ffdaSJohannes Berg static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
60fc36ffdaSJohannes Berg 	.max_peers = IWL_MVM_TOF_MAX_APS,
61fc36ffdaSJohannes Berg 	.report_ap_tsf = 1,
62fc36ffdaSJohannes Berg 	.randomize_mac_addr = 1,
63fc36ffdaSJohannes Berg 
64fc36ffdaSJohannes Berg 	.ftm = {
65fc36ffdaSJohannes Berg 		.supported = 1,
66fc36ffdaSJohannes Berg 		.asap = 1,
67fc36ffdaSJohannes Berg 		.non_asap = 1,
68fc36ffdaSJohannes Berg 		.request_lci = 1,
69fc36ffdaSJohannes Berg 		.request_civicloc = 1,
706815e3d0SAvraham Stern 		.trigger_based = 1,
716815e3d0SAvraham Stern 		.non_trigger_based = 1,
72fc36ffdaSJohannes Berg 		.max_bursts_exponent = -1, /* all supported */
73fc36ffdaSJohannes Berg 		.max_ftms_per_burst = 0, /* no limits */
74fc36ffdaSJohannes Berg 		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
75fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_20) |
76fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_40) |
778a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_80) |
788a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_160),
79fc36ffdaSJohannes Berg 		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
80fc36ffdaSJohannes Berg 			     BIT(NL80211_PREAMBLE_HT) |
816815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_VHT) |
826815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_HE),
83fc36ffdaSJohannes Berg 	},
84fc36ffdaSJohannes Berg };
85fc36ffdaSJohannes Berg 
866569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
87c56e00a3SJohannes Berg 				 enum set_key_cmd cmd,
88c56e00a3SJohannes Berg 				 struct ieee80211_vif *vif,
89c56e00a3SJohannes Berg 				 struct ieee80211_sta *sta,
90c56e00a3SJohannes Berg 				 struct ieee80211_key_conf *key);
91c56e00a3SJohannes Berg 
92e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
93e705c121SKalle Valo {
94e705c121SKalle Valo 	int i;
95e705c121SKalle Valo 
96e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
97e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
98e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
99e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
100e705c121SKalle Valo 	}
101e705c121SKalle Valo }
102e705c121SKalle Valo 
103e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
104e705c121SKalle Valo 						  const char *alpha2,
105e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
106e705c121SKalle Valo 						  bool *changed)
107e705c121SKalle Valo {
108e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
109e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
110e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1117d528eafSJohannes Berg 	struct iwl_mcc_update_resp *resp;
112e27c506aSGil Adam 	u8 resp_ver;
113e705c121SKalle Valo 
114e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
115e705c121SKalle Valo 
116e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
117e705c121SKalle Valo 
118e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
119e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
120e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
121e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
1226d19a5ebSEmmanuel Grumbach 		resp = NULL;
123e705c121SKalle Valo 		goto out;
124e705c121SKalle Valo 	}
125e705c121SKalle Valo 
12682715ac7SEmmanuel Grumbach 	if (changed) {
12782715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
12882715ac7SEmmanuel Grumbach 
12982715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
13082715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
13182715ac7SEmmanuel Grumbach 	}
132e27c506aSGil Adam 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
133e27c506aSGil Adam 					   MCC_UPDATE_CMD, 0);
134e27c506aSGil Adam 	IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
135e705c121SKalle Valo 
136e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
137e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
138e705c121SKalle Valo 				      resp->channels,
13977e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
1402763bba6SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info),
1417d528eafSJohannes Berg 				      __le16_to_cpu(resp->cap), resp_ver);
142e705c121SKalle Valo 	/* Store the return source id */
143e705c121SKalle Valo 	src_id = resp->source_id;
144e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
145e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
146e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
147e705c121SKalle Valo 		goto out;
148e705c121SKalle Valo 	}
149e705c121SKalle Valo 
150e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
151e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
152e705c121SKalle Valo 	mvm->lar_regdom_set = true;
153e705c121SKalle Valo 	mvm->mcc_src = src_id;
154e705c121SKalle Valo 
1556d19a5ebSEmmanuel Grumbach 	iwl_mei_set_country_code(__le16_to_cpu(resp->mcc));
1566d19a5ebSEmmanuel Grumbach 
157e705c121SKalle Valo out:
1586d19a5ebSEmmanuel Grumbach 	kfree(resp);
159e705c121SKalle Valo 	return regd;
160e705c121SKalle Valo }
161e705c121SKalle Valo 
162e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
163e705c121SKalle Valo {
164e705c121SKalle Valo 	bool changed;
165e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
166e705c121SKalle Valo 
167e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
168e705c121SKalle Valo 		return;
169e705c121SKalle Valo 
170e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
171e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
172e705c121SKalle Valo 		/* only update the regulatory core if changed */
173e705c121SKalle Valo 		if (changed)
174e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
175e705c121SKalle Valo 
176e705c121SKalle Valo 		kfree(regd);
177e705c121SKalle Valo 	}
178e705c121SKalle Valo }
179e705c121SKalle Valo 
180e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
181e705c121SKalle Valo 							  bool *changed)
182e705c121SKalle Valo {
183e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
184e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
185e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
186e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
187e705c121SKalle Valo }
188e705c121SKalle Valo 
189e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
190e705c121SKalle Valo {
191e705c121SKalle Valo 	enum iwl_mcc_source used_src;
192e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
193e705c121SKalle Valo 	int ret;
194e705c121SKalle Valo 	bool changed;
195e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
196a05829a7SJohannes Berg 			wiphy_dereference(mvm->hw->wiphy, mvm->hw->wiphy->regd);
197e705c121SKalle Valo 
198e705c121SKalle Valo 	if (!r)
199e705c121SKalle Valo 		return -ENOENT;
200e705c121SKalle Valo 
201e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
202e705c121SKalle Valo 	used_src = mvm->mcc_src;
203e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
204e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
205e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
206e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
207e705c121SKalle Valo 			kfree(regd);
208e705c121SKalle Valo 	}
209e705c121SKalle Valo 
210e705c121SKalle Valo 	/* Now set our last stored MCC and source */
211e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
212e705c121SKalle Valo 				     &changed);
213e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
214e705c121SKalle Valo 		return -EIO;
215e705c121SKalle Valo 
216e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
217e705c121SKalle Valo 	if (changed)
218a05829a7SJohannes Berg 		ret = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
219e705c121SKalle Valo 	else
220e705c121SKalle Valo 		ret = 0;
221e705c121SKalle Valo 
222e705c121SKalle Valo 	kfree(regd);
223e705c121SKalle Valo 	return ret;
224e705c121SKalle Valo }
225e705c121SKalle Valo 
2268e33f046SKrishnanand Prabhu /* Each capability added here should also be add to tm_if_types_ext_capa_sta */
2277f2ea521SYueHaibing static const u8 he_if_types_ext_capa_sta[] = {
2287360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
229918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
230784d4a42SJohannes Berg 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
231784d4a42SJohannes Berg 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
232784d4a42SJohannes Berg 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
2337360f99eSEmmanuel Grumbach };
2347360f99eSEmmanuel Grumbach 
2358e33f046SKrishnanand Prabhu static const u8 tm_if_types_ext_capa_sta[] = {
2368e33f046SKrishnanand Prabhu 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
2378e33f046SKrishnanand Prabhu 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT |
2388e33f046SKrishnanand Prabhu 	       WLAN_EXT_CAPA3_TIMING_MEASUREMENT_SUPPORT,
239784d4a42SJohannes Berg 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
240784d4a42SJohannes Berg 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
241784d4a42SJohannes Berg 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
2428e33f046SKrishnanand Prabhu 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
2438e33f046SKrishnanand Prabhu };
2448e33f046SKrishnanand Prabhu 
2458e33f046SKrishnanand Prabhu /* Additional interface types for which extended capabilities are
2468e33f046SKrishnanand Prabhu  * specified separately
2478e33f046SKrishnanand Prabhu  */
2488e33f046SKrishnanand Prabhu static const struct wiphy_iftype_ext_capab add_iftypes_ext_capa[] = {
2497360f99eSEmmanuel Grumbach 	{
2507360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
2517360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
2527360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
2537360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
2547360f99eSEmmanuel Grumbach 	},
2558e33f046SKrishnanand Prabhu 	{
2568e33f046SKrishnanand Prabhu 		.iftype = NL80211_IFTYPE_STATION,
2578e33f046SKrishnanand Prabhu 		.extended_capabilities = tm_if_types_ext_capa_sta,
2588e33f046SKrishnanand Prabhu 		.extended_capabilities_mask = tm_if_types_ext_capa_sta,
2598e33f046SKrishnanand Prabhu 		.extended_capabilities_len = sizeof(tm_if_types_ext_capa_sta),
2608e33f046SKrishnanand Prabhu 		/* relevant only if EHT is supported */
2618e33f046SKrishnanand Prabhu 		.eml_capabilities = IEEE80211_EML_CAP_EMLSR_SUPP,
2628e33f046SKrishnanand Prabhu 	},
2637360f99eSEmmanuel Grumbach };
2647360f99eSEmmanuel Grumbach 
265cbce62a3SMiri Korenblit int iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
266e8503aecSBen Greear {
267e8503aecSBen Greear 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
268e8503aecSBen Greear 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
269e8503aecSBen Greear 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
270e8503aecSBen Greear 	return 0;
271e8503aecSBen Greear }
272e8503aecSBen Greear 
273e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
274e705c121SKalle Valo {
275e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
276e705c121SKalle Valo 	int num_mac, ret, i;
277e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
278e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
279e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
280e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
281e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
282e705c121SKalle Valo 	};
2833f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
2843f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
2853f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2863f37c229SIdo Yariv #endif
287a5de7de7SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
288a5de7de7SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
289e705c121SKalle Valo 
290e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
291e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
292e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
293e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
294e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
295e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
296e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
297e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
298e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
299e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
300e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
301e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
302909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
30330433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
304520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
305cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
306cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
307ef2b47b8SJohannes Berg 
3080120e6b3SGregory Greenman 	/* Set this early since we need to have it for the check below */
3090120e6b3SGregory Greenman 	if (mvm->mld_api_is_used &&
3100120e6b3SGregory Greenman 	    mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
3110120e6b3SGregory Greenman 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
3120120e6b3SGregory Greenman 
313ef2b47b8SJohannes Berg 	/* With MLD FW API, it tracks timing by itself,
314ef2b47b8SJohannes Berg 	 * no need for any timing from the host
315ef2b47b8SJohannes Berg 	 */
316ef2b47b8SJohannes Berg 	if (!mvm->mld_api_is_used)
317ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
318ef2b47b8SJohannes Berg 
319ef2b47b8SJohannes Berg 	/* We should probably have this, but mac80211
320ef2b47b8SJohannes Berg 	 * currently doesn't support it for MLO.
321ef2b47b8SJohannes Berg 	 */
322ef2b47b8SJohannes Berg 	if (!(hw->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
323ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
324ef2b47b8SJohannes Berg 
325cfb21b11SJohannes Berg 	/*
326cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
327cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
328cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
329cfb21b11SJohannes Berg 	 * assert EDC.
330cfb21b11SJohannes Berg 	 *
331cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
332cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
333cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
334cfb21b11SJohannes Berg 	 * devices.
335cfb21b11SJohannes Berg 	 */
3367d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
337438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
338438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
339ecaf71deSGregory Greenman 
3404243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
341ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
342ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
343ecaf71deSGregory Greenman 	}
344ecaf71deSGregory Greenman 
345b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
346b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
347960f864bSJohannes Berg 
348960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
349960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
35065e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
351960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
352960f864bSJohannes Berg 		/*
353960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
354960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
355960f864bSJohannes Berg 		 * for station powersave
356960f864bSJohannes Berg 		 */
357960f864bSJohannes Berg 		return -EINVAL;
358960f864bSJohannes Berg 	}
359e705c121SKalle Valo 
36080938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
36180938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
36280938abcSJohannes Berg 
3632d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
3642d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
3652d7cf549SJohannes Berg 
366366fc672SJohannes Berg 	hw->queues = IEEE80211_NUM_ACS;
367e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
368e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
369e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
370e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
371e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
372371a17edSJohannes Berg 
373371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
374371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
375371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
376371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
377371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
378371a17edSJohannes Berg 
3794243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
3809f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
3819f66a397SGregory Greenman 
382e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
383e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
384438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
385e705c121SKalle Valo 
3868e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
387e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
388e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
389e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
390e705c121SKalle Valo 
3912a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
3922a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
3932a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
3942a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
3952a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
3962a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
3972a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
3982a53d166SAyala Beker 	}
3992a53d166SAyala Beker 
400f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
401f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
402f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
403f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
404f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
405f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
406f4bfdc5eSEmmanuel Grumbach 
407e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
408f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
409e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
4108e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4118e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4128e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
4138e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4148e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4158e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
4168e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4178e160ab8SAyala Beker 	}
418e705c121SKalle Valo 
419ef2e7a51SIlan Peer 	wiphy_ext_feature_set(hw->wiphy,
420ef2e7a51SIlan Peer 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
421d16b96b5SJohannes Berg 	wiphy_ext_feature_set(hw->wiphy,
422d16b96b5SJohannes Berg 			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
423ef2e7a51SIlan Peer 
424b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
425fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
426b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
427b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
428fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
429fc36ffdaSJohannes Berg 	}
430b73f9a4aSJohannes Berg 
431a5de7de7SJohannes Berg 	if (sec_key_ver &&
432a5de7de7SJohannes Berg 	    fw_has_capa(&mvm->fw->ucode_capa,
433a5de7de7SJohannes Berg 			IWL_UCODE_TLV_CAPA_BIGTK_TX_SUPPORT))
434a5de7de7SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
435a5de7de7SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION);
436a5de7de7SJohannes Berg 	else if (fw_has_capa(&mvm->fw->ucode_capa,
437b1fdc250SJohannes Berg 			     IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
438b1fdc250SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
439b1fdc250SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
440b1fdc250SJohannes Berg 
441cf85123aSAvraham Stern 	if (fw_has_capa(&mvm->fw->ucode_capa,
442cf85123aSAvraham Stern 			IWL_UCODE_TLV_CAPA_TIME_SYNC_BOTH_FTM_TM))
443cf85123aSAvraham Stern 		hw->wiphy->hw_timestamp_max_peers = 1;
444cf85123aSAvraham Stern 
445e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
446e705c121SKalle Valo 	hw->wiphy->features |=
447e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
448e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
449e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
450e705c121SKalle Valo 
451e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
452e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
453e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
454cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
455e705c121SKalle Valo 
456e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
457e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
458e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
459e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
460e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
461e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
462e705c121SKalle Valo 
463e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
464e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
46533e3fd99SAlexander Wetzel 
46633e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
46733e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
46833e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
46933e3fd99SAlexander Wetzel 	 */
47033e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
47133e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
47233e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
473e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
474e47df5bdSJohannes Berg 
475e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
476e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
477e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
478e705c121SKalle Valo 	else
479e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
480e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
481e705c121SKalle Valo 
482e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
483e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
484eae94cf8SLuca Coelho 	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
485e705c121SKalle Valo 
486e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
487e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
488e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
489e705c121SKalle Valo 
490e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
491e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
492e705c121SKalle Valo 
493e705c121SKalle Valo 	/* Extract MAC address */
494e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
495e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
496e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
497e705c121SKalle Valo 
498e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
499e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
500e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
501e705c121SKalle Valo 
502e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
503e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
504e705c121SKalle Valo 		       ETH_ALEN);
505e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
506e705c121SKalle Valo 		hw->wiphy->n_addresses++;
507e705c121SKalle Valo 	}
508e705c121SKalle Valo 
509e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
510e705c121SKalle Valo 
511e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
512e705c121SKalle Valo 
513e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
514e705c121SKalle Valo 
515e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
516e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
517e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
518e705c121SKalle Valo 
519e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
520e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
521e705c121SKalle Valo 	else
522e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
523e705c121SKalle Valo 
52457fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
52557fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
52657fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
52757fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
52857fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
52957fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
530e705c121SKalle Valo 
531e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
532e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
533e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
534e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
53557fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
536e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
537e705c121SKalle Valo 	}
538eae94cf8SLuca Coelho 	if (fw_has_capa(&mvm->fw->ucode_capa,
539eae94cf8SLuca Coelho 			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
540eae94cf8SLuca Coelho 	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
541eae94cf8SLuca Coelho 		hw->wiphy->bands[NL80211_BAND_6GHZ] =
542eae94cf8SLuca Coelho 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
543e705c121SKalle Valo 
544e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
545e705c121SKalle Valo 
546e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
547e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
548e705c121SKalle Valo 	else
549e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
550e705c121SKalle Valo 
551ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
552e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
5535d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
554e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
555e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
556e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
557e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
558e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
559e705c121SKalle Valo 
560e705c121SKalle Valo 	/*
561e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
562e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
563e705c121SKalle Valo 	 */
564e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
565e705c121SKalle Valo 
566e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
567e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
568e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
569a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
570e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
571e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
572e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
573e705c121SKalle Valo 
574e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
575e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
576e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
577e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
578e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
579e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
580e705c121SKalle Valo 
581e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
582e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
583e705c121SKalle Valo 		hw->wiphy->features |=
584e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
585e705c121SKalle Valo 
586e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
587e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
588e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
589e705c121SKalle Valo 
590971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
5912a42aea7SNathan Errera 				  IWL_FW_CMD_VER_UNKNOWN) == 3)
5922a42aea7SNathan Errera 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
5932a42aea7SNathan Errera 
594aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
595aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
596aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
597aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
598aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
599aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
600aacf8f18SAvrahams Stern 	}
601aacf8f18SAvrahams Stern 
6028f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
603971cbe50SJohannes Berg 		u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
604773a042fSAvraham Stern 
6058f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6068f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
6078f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6088f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
6098f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6108f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
611773a042fSAvraham Stern 
612773a042fSAvraham Stern 		/* Old firmware also supports probe deferral and suppression */
613773a042fSAvraham Stern 		if (scan_ver < 15)
614773a042fSAvraham Stern 			wiphy_ext_feature_set(hw->wiphy,
615773a042fSAvraham Stern 					      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
6168f691af9SZamir, Roee 	}
6178f691af9SZamir, Roee 
6188e33f046SKrishnanand Prabhu 	hw->wiphy->iftype_ext_capab = NULL;
6198e33f046SKrishnanand Prabhu 	hw->wiphy->num_iftype_ext_capab = 0;
6208e33f046SKrishnanand Prabhu 
6217360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
6227360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
6238e33f046SKrishnanand Prabhu 		hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa;
6247360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
6258e33f046SKrishnanand Prabhu 			ARRAY_SIZE(add_iftypes_ext_capa) - 1;
626918cbf39SSara Sharon 
627918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
628918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
6297360f99eSEmmanuel Grumbach 	}
6307360f99eSEmmanuel Grumbach 
6318e33f046SKrishnanand Prabhu 	if (iwl_fw_lookup_cmd_ver(mvm->fw,
6328e33f046SKrishnanand Prabhu 				  WIDE_ID(DATA_PATH_GROUP,
6338e33f046SKrishnanand Prabhu 					  WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
6348e33f046SKrishnanand Prabhu 				  IWL_FW_CMD_VER_UNKNOWN) >= 1) {
6358e33f046SKrishnanand Prabhu 		IWL_DEBUG_INFO(mvm->trans, "Timing measurement supported\n");
6368e33f046SKrishnanand Prabhu 
6378e33f046SKrishnanand Prabhu 		if (!hw->wiphy->iftype_ext_capab) {
6388e33f046SKrishnanand Prabhu 			hw->wiphy->num_iftype_ext_capab = 1;
6398e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa +
6408e33f046SKrishnanand Prabhu 				ARRAY_SIZE(add_iftypes_ext_capa) - 1;
6418e33f046SKrishnanand Prabhu 		} else {
6428e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa + 1;
6438e33f046SKrishnanand Prabhu 		}
6448e33f046SKrishnanand Prabhu 	}
6458e33f046SKrishnanand Prabhu 
646e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
647e705c121SKalle Valo 
648e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6493f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
650e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
651e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
652e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
653e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
654e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
655e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
656e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
657e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
658e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
659e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
660e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
661e705c121SKalle Valo 
662e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
663e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
664e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
6655d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
6665d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
667e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
668e705c121SKalle Valo 	}
669e705c121SKalle Valo #endif
670e705c121SKalle Valo 
671e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
672e705c121SKalle Valo 	if (ret)
673e705c121SKalle Valo 		return ret;
674e705c121SKalle Valo 
675e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
676e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
677e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
678e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
679e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
680e705c121SKalle Valo 	}
681e705c121SKalle Valo 
682e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
683e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
684e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
685e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
686e705c121SKalle Valo 	}
687e705c121SKalle Valo 
688e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
68959fa61f3SEmmanuel Grumbach 	if (!iwl_mvm_is_csum_supported(mvm))
6906772aab7SJohannes Berg 		hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
69141837ca9SEmmanuel Grumbach 
69291b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
69391b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
69491b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
69591b08c2dSAviya Erenfeld 
6969c11d8a9SShaul Triebitz 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
6979c11d8a9SShaul Triebitz 		wiphy_ext_feature_set(hw->wiphy,
6989c11d8a9SShaul Triebitz 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
6999c11d8a9SShaul Triebitz 
700bfcfdb59SEmmanuel Grumbach 	iwl_mvm_vendor_cmds_register(mvm);
701bfcfdb59SEmmanuel Grumbach 
702e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
703e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
704e8503aecSBen Greear 
705de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
706de645e89SJohannes Berg 	if (ret) {
707de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
708e705c121SKalle Valo 	}
709e705c121SKalle Valo 
710e705c121SKalle Valo 	return ret;
711e705c121SKalle Valo }
712e705c121SKalle Valo 
713df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
714df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
715df2378abSJohannes Berg {
716df2378abSJohannes Berg 	if (likely(sta)) {
717df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
718df2378abSJohannes Berg 			return;
719df2378abSJohannes Berg 	} else {
720df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
721df2378abSJohannes Berg 			return;
722df2378abSJohannes Berg 	}
723df2378abSJohannes Berg 
724df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
725df2378abSJohannes Berg }
726df2378abSJohannes Berg 
727cbce62a3SMiri Korenblit void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
728cbce62a3SMiri Korenblit 		    struct ieee80211_tx_control *control, struct sk_buff *skb)
729e705c121SKalle Valo {
730e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
731e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
732e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
733e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
734cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
735cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
73690723da6SShaul Triebitz 	u32 link_id = u32_get_bits(info->control.flags,
73790723da6SShaul Triebitz 				   IEEE80211_TX_CTRL_MLO_LINK);
73890723da6SShaul Triebitz 	struct ieee80211_sta *tmp_sta = sta;
739e705c121SKalle Valo 
740e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
741e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
742e705c121SKalle Valo 		goto drop;
743e705c121SKalle Valo 	}
744e705c121SKalle Valo 
745cfbc6c4cSSara Sharon 	if (offchannel &&
746e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
747e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
748e705c121SKalle Valo 		goto drop;
749e705c121SKalle Valo 
75010516783SJohannes Berg 	/*
75110516783SJohannes Berg 	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
75210516783SJohannes Berg 	 * so we treat the others as broadcast
75310516783SJohannes Berg 	 */
75410516783SJohannes Berg 	if (ieee80211_is_mgmt(hdr->frame_control))
755e705c121SKalle Valo 		sta = NULL;
756e705c121SKalle Valo 
757dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
758cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
759cfbc6c4cSSara Sharon 	    !offchannel) {
760dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
761dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
762650cadb7SGregory Greenman 		u8 ap_sta_id = READ_ONCE(mvmvif->deflink.ap_sta_id);
763dc1aca22SAndrei Otcheretianski 
764be9ae34eSNathan Errera 		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
765dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
766dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
767dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
768dc1aca22SAndrei Otcheretianski 				goto drop;
769dc1aca22SAndrei Otcheretianski 		}
770dc1aca22SAndrei Otcheretianski 	}
771dc1aca22SAndrei Otcheretianski 
77290723da6SShaul Triebitz 	if (tmp_sta && !sta && link_id != IEEE80211_LINK_UNSPECIFIED &&
77390723da6SShaul Triebitz 	    !ieee80211_is_probe_resp(hdr->frame_control)) {
77490723da6SShaul Triebitz 		/* translate MLD addresses to LINK addresses */
77590723da6SShaul Triebitz 		struct ieee80211_link_sta *link_sta =
77690723da6SShaul Triebitz 			rcu_dereference(tmp_sta->link[link_id]);
77790723da6SShaul Triebitz 		struct ieee80211_bss_conf *link_conf =
77890723da6SShaul Triebitz 			rcu_dereference(info->control.vif->link_conf[link_id]);
77990723da6SShaul Triebitz 		struct ieee80211_mgmt *mgmt;
78090723da6SShaul Triebitz 
78190723da6SShaul Triebitz 		if (WARN_ON(!link_sta || !link_conf))
78290723da6SShaul Triebitz 			goto drop;
78390723da6SShaul Triebitz 
78490723da6SShaul Triebitz 		/* if sta is NULL, the frame is a management frame */
78590723da6SShaul Triebitz 		mgmt = (void *)hdr;
78690723da6SShaul Triebitz 		memcpy(mgmt->da, link_sta->addr, ETH_ALEN);
78790723da6SShaul Triebitz 		memcpy(mgmt->sa, link_conf->addr, ETH_ALEN);
78890723da6SShaul Triebitz 		memcpy(mgmt->bssid, link_conf->bssid, ETH_ALEN);
78990723da6SShaul Triebitz 	}
79090723da6SShaul Triebitz 
791df2378abSJohannes Berg 	iwl_mvm_tx_skb(mvm, skb, sta);
792e705c121SKalle Valo 	return;
793e705c121SKalle Valo  drop:
794e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
795e705c121SKalle Valo }
796e705c121SKalle Valo 
797cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
798e705c121SKalle Valo {
799cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
800cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
801cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
802cfbc6c4cSSara Sharon 
803fba8248eSSara Sharon 	/*
804fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
805fba8248eSSara Sharon 	 * taker all the work.
806fba8248eSSara Sharon 	 *
807fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
808fba8248eSSara Sharon 	 *
809fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
810fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
811fba8248eSSara Sharon 	 *
812fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
813fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
814fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
815fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
816fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
817fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
818fba8248eSSara Sharon 	 *
819fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
820fba8248eSSara Sharon 	 * check the queue, so do nothing.
821fba8248eSSara Sharon 	 */
822fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
823fba8248eSSara Sharon 		return;
824cfbc6c4cSSara Sharon 
825cfbc6c4cSSara Sharon 	rcu_read_lock();
826fba8248eSSara Sharon 	do {
827b58e3d43SJohannes Berg 		while (likely(!test_bit(IWL_MVM_TXQ_STATE_STOP_FULL,
828b58e3d43SJohannes Berg 					&mvmtxq->state) &&
829b58e3d43SJohannes Berg 			      !test_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT,
830b58e3d43SJohannes Berg 					&mvmtxq->state) &&
83100520b7aSEmmanuel Grumbach 			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
832cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
833cfbc6c4cSSara Sharon 
834f50d693bSSara Sharon 			if (!skb) {
835f50d693bSSara Sharon 				if (txq->sta)
836f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
837f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
838f50d693bSSara Sharon 						     txq->sta->addr,
839f50d693bSSara Sharon 						     txq->tid);
840cfbc6c4cSSara Sharon 				break;
841f50d693bSSara Sharon 			}
842cfbc6c4cSSara Sharon 
843cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
844cfbc6c4cSSara Sharon 		}
845fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
846cfbc6c4cSSara Sharon 	rcu_read_unlock();
847e705c121SKalle Valo }
848e705c121SKalle Valo 
849cbce62a3SMiri Korenblit void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
850cfbc6c4cSSara Sharon 			       struct ieee80211_txq *txq)
851e705c121SKalle Valo {
852cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
853cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
854e705c121SKalle Valo 
855923bf981SJohannes Berg 	if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
856923bf981SJohannes Berg 	    !txq->sta) {
857cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
858cfbc6c4cSSara Sharon 		return;
859cfbc6c4cSSara Sharon 	}
860cfbc6c4cSSara Sharon 
861923bf981SJohannes Berg 	/* iwl_mvm_mac_itxq_xmit() will later be called by the worker
862923bf981SJohannes Berg 	 * to handle any packets we leave on the txq now
863923bf981SJohannes Berg 	 */
864cfbc6c4cSSara Sharon 
865923bf981SJohannes Berg 	spin_lock_bh(&mvm->add_stream_lock);
866923bf981SJohannes Berg 	/* The list is being deleted only after the queue is fully allocated. */
867923bf981SJohannes Berg 	if (list_empty(&mvmtxq->list) &&
868923bf981SJohannes Berg 	    /* recheck under lock */
869923bf981SJohannes Berg 	    !test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) {
870cfbc6c4cSSara Sharon 		list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
871cfbc6c4cSSara Sharon 		schedule_work(&mvm->add_stream_wk);
872e705c121SKalle Valo 	}
873923bf981SJohannes Berg 	spin_unlock_bh(&mvm->add_stream_lock);
874923bf981SJohannes Berg }
875e705c121SKalle Valo 
876e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
877e705c121SKalle Valo 	do {								\
878e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
879e705c121SKalle Valo 			break;						\
8807174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
881e705c121SKalle Valo 	} while (0)
882e705c121SKalle Valo 
883e705c121SKalle Valo static void
884e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
885e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
886e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
887e705c121SKalle Valo {
888e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
889e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
890e705c121SKalle Valo 
8916c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
8926c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
8936c042d75SSara Sharon 	if (!trig)
894e705c121SKalle Valo 		return;
895e705c121SKalle Valo 
896e705c121SKalle Valo 	ba_trig = (void *)trig->data;
897e705c121SKalle Valo 
898e705c121SKalle Valo 	switch (action) {
899e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
900e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
901e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
902e705c121SKalle Valo 
903e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
904e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
905e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
906e705c121SKalle Valo 		break;
907e705c121SKalle Valo 		}
908e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
909e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
910e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
911e705c121SKalle Valo 				 sta->addr, tid);
912e705c121SKalle Valo 		break;
913e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
914e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
915e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
916e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
917e705c121SKalle Valo 		break;
918e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
919e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
920e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
921e705c121SKalle Valo 				 sta->addr, tid);
922e705c121SKalle Valo 		break;
923e705c121SKalle Valo 	default:
924e705c121SKalle Valo 		break;
925e705c121SKalle Valo 	}
926e705c121SKalle Valo }
927e705c121SKalle Valo 
928cbce62a3SMiri Korenblit int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
929e705c121SKalle Valo 			     struct ieee80211_vif *vif,
93050ea05efSSara Sharon 			     struct ieee80211_ampdu_params *params)
931e705c121SKalle Valo {
932e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
933e705c121SKalle Valo 	int ret;
93450ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
93550ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
93650ea05efSSara Sharon 	u16 tid = params->tid;
93750ea05efSSara Sharon 	u16 *ssn = &params->ssn;
938514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
939bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
94010b2b201SSara Sharon 	u16 timeout = params->timeout;
941e705c121SKalle Valo 
942e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
943e705c121SKalle Valo 		     sta->addr, tid, action);
944e705c121SKalle Valo 
945e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
946e705c121SKalle Valo 		return -EACCES;
947e705c121SKalle Valo 
948e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
949e705c121SKalle Valo 
950e705c121SKalle Valo 	switch (action) {
951e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
952650cadb7SGregory Greenman 		if (iwl_mvm_vif_from_mac80211(vif)->deflink.ap_sta_id ==
953c8ee33e1SGregory Greenman 		    iwl_mvm_sta_from_mac80211(sta)->deflink.sta_id) {
954b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
955b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
956b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
957b0ffe455SJohannes Berg 
958b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
959b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
960b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
961b0ffe455SJohannes Berg 		}
962e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
963e705c121SKalle Valo 			ret = -EINVAL;
964e705c121SKalle Valo 			break;
965e705c121SKalle Valo 		}
96610b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
96710b2b201SSara Sharon 					 timeout);
968e705c121SKalle Valo 		break;
969e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
97010b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
97110b2b201SSara Sharon 					 timeout);
972e705c121SKalle Valo 		break;
973e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
974e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
975e705c121SKalle Valo 			ret = -EINVAL;
976e705c121SKalle Valo 			break;
977e705c121SKalle Valo 		}
978e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
979e705c121SKalle Valo 		break;
980e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
981e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
982e705c121SKalle Valo 		break;
983e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
984e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
985e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
986e705c121SKalle Valo 		break;
987e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
988bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
989bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
990e705c121SKalle Valo 		break;
991e705c121SKalle Valo 	default:
992e705c121SKalle Valo 		WARN_ON_ONCE(1);
993e705c121SKalle Valo 		ret = -EINVAL;
994e705c121SKalle Valo 		break;
995e705c121SKalle Valo 	}
996e705c121SKalle Valo 
997e705c121SKalle Valo 	if (!ret) {
998e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
999e705c121SKalle Valo 
1000e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1001e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1002e705c121SKalle Valo 
1003e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1004e705c121SKalle Valo 					    rx_ba_ssn, action);
1005e705c121SKalle Valo 	}
1006e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1007e705c121SKalle Valo 
1008e705c121SKalle Valo 	return ret;
1009e705c121SKalle Valo }
1010e705c121SKalle Valo 
1011e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1012e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1013e705c121SKalle Valo {
1014e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1015e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1016bf976c81SJohannes Berg 	struct iwl_probe_resp_data *probe_data;
101779faae3aSGregory Greenman 	unsigned int link_id;
1018e705c121SKalle Valo 
1019e705c121SKalle Valo 	mvmvif->uploaded = false;
1020e705c121SKalle Valo 
1021e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1022e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1023e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1024e705c121SKalle Valo 
1025e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
102679faae3aSGregory Greenman 
102779faae3aSGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
102879faae3aSGregory Greenman 		mvmvif->link[link_id]->ap_sta_id = IWL_MVM_INVALID_STA;
102979faae3aSGregory Greenman 		mvmvif->link[link_id]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
103079faae3aSGregory Greenman 		mvmvif->link[link_id]->phy_ctxt = NULL;
1031bf976c81SJohannes Berg 		mvmvif->link[link_id]->active = 0;
103279faae3aSGregory Greenman 	}
1033bf976c81SJohannes Berg 
1034bf976c81SJohannes Berg 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1035bf976c81SJohannes Berg 					       lockdep_is_held(&mvm->mutex));
1036bf976c81SJohannes Berg 	if (probe_data)
1037bf976c81SJohannes Berg 		kfree_rcu(probe_data, rcu_head);
1038bf976c81SJohannes Berg 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1039e705c121SKalle Valo }
1040e705c121SKalle Valo 
1041e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1042e705c121SKalle Valo {
1043fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1044e705c121SKalle Valo 
10459bf13beeSJohannes Berg 	mvm->cur_aid = 0;
10469bf13beeSJohannes Berg 
1047e705c121SKalle Valo 	mvm->scan_status = 0;
1048e705c121SKalle Valo 	mvm->ps_disabled = false;
1049b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1050e705c121SKalle Valo 
1051e705c121SKalle Valo 	/* just in case one was running */
1052305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1053e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1054e705c121SKalle Valo 
1055fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1056fc36ffdaSJohannes Berg 
1057e705c121SKalle Valo 	/*
1058e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1059e705c121SKalle Valo 	 * gone down during the HW restart
1060e705c121SKalle Valo 	 */
1061e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1062e705c121SKalle Valo 
1063e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1064e705c121SKalle Valo 
1065e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
10669c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1067e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1068e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1069e705c121SKalle Valo 
1070e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1071e705c121SKalle Valo 
1072e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
10737174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1074baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1075fb40cd9dSMiri Korenblit #ifdef CONFIG_IWLWIFI_DEBUGFS
1076fb40cd9dSMiri Korenblit 	mvm->beacon_inject_active = false;
1077fb40cd9dSMiri Korenblit #endif
1078e705c121SKalle Valo 
1079e705c121SKalle Valo 	/* keep statistics ticking */
1080e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1081e705c121SKalle Valo }
1082e705c121SKalle Valo 
1083e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1084e705c121SKalle Valo {
1085e705c121SKalle Valo 	int ret;
1086e705c121SKalle Valo 
1087e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1088e705c121SKalle Valo 
10896d19a5ebSEmmanuel Grumbach 	ret = iwl_mvm_mei_get_ownership(mvm);
10906d19a5ebSEmmanuel Grumbach 	if (ret)
10916d19a5ebSEmmanuel Grumbach 		return ret;
10926d19a5ebSEmmanuel Grumbach 
10936d19a5ebSEmmanuel Grumbach 	if (mvm->mei_nvm_data) {
10946d19a5ebSEmmanuel Grumbach 		/* We got the NIC, we can now free the MEI NVM data */
10956d19a5ebSEmmanuel Grumbach 		kfree(mvm->mei_nvm_data);
10966d19a5ebSEmmanuel Grumbach 		mvm->mei_nvm_data = NULL;
10976d19a5ebSEmmanuel Grumbach 
10986d19a5ebSEmmanuel Grumbach 		/*
10996d19a5ebSEmmanuel Grumbach 		 * We can't free the nvm_data we allocated based on the SAP
11006d19a5ebSEmmanuel Grumbach 		 * data because we registered to cfg80211 with the channels
11016d19a5ebSEmmanuel Grumbach 		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
11026d19a5ebSEmmanuel Grumbach 		 * just in order to be able free it later.
11036d19a5ebSEmmanuel Grumbach 		 * NULLify nvm_data so that we will read the NVM from the
11046d19a5ebSEmmanuel Grumbach 		 * firmware this time.
11056d19a5ebSEmmanuel Grumbach 		 */
11066d19a5ebSEmmanuel Grumbach 		mvm->temp_nvm_data = mvm->nvm_data;
11076d19a5ebSEmmanuel Grumbach 		mvm->nvm_data = NULL;
11086d19a5ebSEmmanuel Grumbach 	}
11096d19a5ebSEmmanuel Grumbach 
1110bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1111bf8b286fSJohannes Berg 		/*
1112bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1113bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1114bf8b286fSJohannes Berg 		 */
1115bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1116bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1117e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1118e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1119a42b2af3SLuca Coelho 	}
1120e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1121e705c121SKalle Valo 
1122b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1123b108d8c7SShahar S Matityahu 			       NULL);
1124b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1125b108d8c7SShahar S Matityahu 			       NULL);
1126da2eb669SSara Sharon 
1127e8fe3b41SIlan Peer 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1128e8fe3b41SIlan Peer 
1129e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1130e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1131e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1132e705c121SKalle Valo 		 * would do.
1133e705c121SKalle Valo 		 */
1134e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1135e705c121SKalle Valo 	}
1136e705c121SKalle Valo 
1137e705c121SKalle Valo 	return ret;
1138e705c121SKalle Valo }
1139e705c121SKalle Valo 
1140cbce62a3SMiri Korenblit int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1141e705c121SKalle Valo {
1142e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1143e705c121SKalle Valo 	int ret;
11445283dd67SMordechay Goodstein 	int retry, max_retry = 0;
1145e705c121SKalle Valo 
1146e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
11475283dd67SMordechay Goodstein 
11485283dd67SMordechay Goodstein 	/* we are starting the mac not in error flow, and restart is enabled */
11495283dd67SMordechay Goodstein 	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
11505283dd67SMordechay Goodstein 	    iwlwifi_mod_params.fw_restart) {
11515283dd67SMordechay Goodstein 		max_retry = IWL_MAX_INIT_RETRY;
11525283dd67SMordechay Goodstein 		/*
11535283dd67SMordechay Goodstein 		 * This will prevent mac80211 recovery flows to trigger during
11545283dd67SMordechay Goodstein 		 * init failures
11555283dd67SMordechay Goodstein 		 */
11565283dd67SMordechay Goodstein 		set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
11575283dd67SMordechay Goodstein 	}
11585283dd67SMordechay Goodstein 
11595283dd67SMordechay Goodstein 	for (retry = 0; retry <= max_retry; retry++) {
1160e705c121SKalle Valo 		ret = __iwl_mvm_mac_start(mvm);
11615283dd67SMordechay Goodstein 		if (!ret)
11625283dd67SMordechay Goodstein 			break;
11635283dd67SMordechay Goodstein 
1164b8133439SAvraham Stern 		/*
1165b8133439SAvraham Stern 		 * In PLDR sync PCI re-enumeration is needed. no point to retry
1166b8133439SAvraham Stern 		 * mac start before that.
1167b8133439SAvraham Stern 		 */
1168b8133439SAvraham Stern 		if (mvm->pldr_sync) {
1169b8133439SAvraham Stern 			iwl_mei_alive_notif(false);
1170b8133439SAvraham Stern 			iwl_trans_pcie_remove(mvm->trans, true);
1171b8133439SAvraham Stern 			break;
1172b8133439SAvraham Stern 		}
1173b8133439SAvraham Stern 
11745283dd67SMordechay Goodstein 		IWL_ERR(mvm, "mac start retry %d\n", retry);
11755283dd67SMordechay Goodstein 	}
11765283dd67SMordechay Goodstein 	clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
11775283dd67SMordechay Goodstein 
1178e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1179e705c121SKalle Valo 
11807ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
11817ce1f215SEmmanuel Grumbach 
1182e705c121SKalle Valo 	return ret;
1183e705c121SKalle Valo }
1184e705c121SKalle Valo 
1185e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1186e705c121SKalle Valo {
1187e705c121SKalle Valo 	int ret;
1188e705c121SKalle Valo 
1189e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1190e705c121SKalle Valo 
1191e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
11924d4183c4SEmmanuel Grumbach 
1193e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1194e705c121SKalle Valo 	if (ret)
1195e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1196e705c121SKalle Valo 			ret);
1197e705c121SKalle Valo 
1198f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1199f130bb75SMordechay Goodstein 
1200e705c121SKalle Valo 	/*
1201e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1202e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1203e705c121SKalle Valo 	 */
1204e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1205e705c121SKalle Valo 
1206e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1207e705c121SKalle Valo }
1208e705c121SKalle Valo 
1209cbce62a3SMiri Korenblit void iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1210e705c121SKalle Valo 				   enum ieee80211_reconfig_type reconfig_type)
1211e705c121SKalle Valo {
1212e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1213e705c121SKalle Valo 
1214e705c121SKalle Valo 	switch (reconfig_type) {
1215e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1216e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1217e705c121SKalle Valo 		break;
1218e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1219e705c121SKalle Valo 		break;
1220e705c121SKalle Valo 	}
1221e705c121SKalle Valo }
1222e705c121SKalle Valo 
1223e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1224e705c121SKalle Valo {
1225e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1226e705c121SKalle Valo 
1227b68bd2e3SIlan Peer 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1228b68bd2e3SIlan Peer 
1229e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1230e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1231e705c121SKalle Valo 	 */
1232e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1233e705c121SKalle Valo 
1234e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1235e705c121SKalle Valo 
1236971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) < 12)
1237f327236dSSharon 		iwl_mvm_rm_aux_sta(mvm);
1238f327236dSSharon 
1239fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1240e705c121SKalle Valo 
1241e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1242e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1243e705c121SKalle Valo 
1244e705c121SKalle Valo 	/*
1245155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1246155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1247155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1248e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1249e705c121SKalle Valo 	 * HW restart was requested.
1250e705c121SKalle Valo 	 */
1251155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1252155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1253155f7e04SEmmanuel Grumbach 			       &mvm->status))
1254e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1255e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1256e705c121SKalle Valo 
1257e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1258e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1259e705c121SKalle Valo 	 */
1260e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1261e705c121SKalle Valo 		int i;
1262e705c121SKalle Valo 
1263e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1264e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1265e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1266e705c121SKalle Valo 				      i))
1267e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1268e705c121SKalle Valo 		}
1269e705c121SKalle Valo 	}
1270e705c121SKalle Valo }
1271e705c121SKalle Valo 
1272cbce62a3SMiri Korenblit void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1273e705c121SKalle Valo {
1274e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1275e705c121SKalle Valo 
1276e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
127724afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1278771147b0SJohannes Berg 
1279771147b0SJohannes Berg 	/*
1280771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1281771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1282771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
12837174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1284771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1285771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1286771147b0SJohannes Berg 	 */
1287771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1288771147b0SJohannes Berg 
1289d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
129069e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1291e705c121SKalle Valo 
1292f9084775SNathan Errera 	/*
1293f9084775SNathan Errera 	 * The work item could be running or queued if the
1294f9084775SNathan Errera 	 * ROC time event stops just as we get here.
1295f9084775SNathan Errera 	 */
1296f9084775SNathan Errera 	flush_work(&mvm->roc_done_wk);
1297f9084775SNathan Errera 
12987ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
12997ce1f215SEmmanuel Grumbach 
1300e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1301e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1302e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1303e705c121SKalle Valo 
1304e705c121SKalle Valo 	/*
1305e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1306e705c121SKalle Valo 	 * discover that its list is now empty.
1307e705c121SKalle Valo 	 */
1308e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1309e705c121SKalle Valo }
1310e705c121SKalle Valo 
13111ab26632SMiri Korenblit struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1312e705c121SKalle Valo {
1313e705c121SKalle Valo 	u16 i;
1314e705c121SKalle Valo 
1315e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1316e705c121SKalle Valo 
1317e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1318e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1319e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1320e705c121SKalle Valo 
1321e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1322e705c121SKalle Valo 	return NULL;
1323e705c121SKalle Valo }
1324e705c121SKalle Valo 
1325f551d013SGregory Greenman int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1326e705c121SKalle Valo 			 s16 tx_power)
1327e705c121SKalle Valo {
1328971cbe50SJohannes Berg 	u32 cmd_id = REDUCE_TX_POWER_CMD;
13290791c2fcSHaim Dreyfuss 	int len;
1330216cdfb5SLuca Coelho 	struct iwl_dev_tx_power_cmd cmd = {
1331216cdfb5SLuca Coelho 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1332216cdfb5SLuca Coelho 		.common.mac_context_id =
1333e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1334216cdfb5SLuca Coelho 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1335e705c121SKalle Valo 	};
1336971cbe50SJohannes Berg 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1337e80bfd11SMordechay Goodstein 					   IWL_FW_CMD_VER_UNKNOWN);
1338e705c121SKalle Valo 
1339e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1340216cdfb5SLuca Coelho 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1341e705c121SKalle Valo 
1342b0aa02b3SAyala Barazani 	if (cmd_ver == 7)
1343b0aa02b3SAyala Barazani 		len = sizeof(cmd.v7);
1344b0aa02b3SAyala Barazani 	else if (cmd_ver == 6)
1345fbb7957dSLuca Coelho 		len = sizeof(cmd.v6);
1346fbb7957dSLuca Coelho 	else if (fw_has_api(&mvm->fw->ucode_capa,
13470791c2fcSHaim Dreyfuss 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
13480791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
13490791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
13500791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
13510791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
13520791c2fcSHaim Dreyfuss 	else
1353216cdfb5SLuca Coelho 		len = sizeof(cmd.v3);
1354216cdfb5SLuca Coelho 
1355216cdfb5SLuca Coelho 	/* all structs have the same common part, add it */
1356216cdfb5SLuca Coelho 	len += sizeof(cmd.common);
1357e705c121SKalle Valo 
1358971cbe50SJohannes Berg 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1359e705c121SKalle Valo }
1360e705c121SKalle Valo 
136103117f30SMiri Korenblit int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1362f6780614SSara Sharon 				struct ieee80211_vif *vif)
1363f6780614SSara Sharon {
1364f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1365f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1366f6780614SSara Sharon 	int ret;
1367f6780614SSara Sharon 
1368f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1369f6780614SSara Sharon 
1370f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1371f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
1372f6780614SSara Sharon 
1373f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
1374f6780614SSara Sharon 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
1375650cadb7SGregory Greenman 							  mvmvif->deflink.ap_sta_id);
1376f6780614SSara Sharon 
1377f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1378f6780614SSara Sharon 			ret = -EIO;
1379f6780614SSara Sharon 			goto out_unlock;
1380f6780614SSara Sharon 		}
1381f6780614SSara Sharon 
1382f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
138303117f30SMiri Korenblit 		if (mvm->mld_api_is_used)
138403117f30SMiri Korenblit 			iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
138503117f30SMiri Korenblit 		else
1386f6780614SSara Sharon 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1387f6780614SSara Sharon 
13880202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
13890202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1390f6780614SSara Sharon 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1391f6780614SSara Sharon 			if (ret)
1392f6780614SSara Sharon 				goto out_unlock;
1393f6780614SSara Sharon 
1394f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1395f6780614SSara Sharon 		}
13960202bcf0SEmmanuel Grumbach 	}
1397f6780614SSara Sharon 
1398f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1399f6780614SSara Sharon 
1400f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1401f6780614SSara Sharon 
1402f6780614SSara Sharon out_unlock:
1403caf46377SSara Sharon 	if (mvmvif->csa_failed)
1404caf46377SSara Sharon 		ret = -EIO;
1405f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1406f6780614SSara Sharon 
1407f6780614SSara Sharon 	return ret;
1408f6780614SSara Sharon }
1409f6780614SSara Sharon 
1410cbce62a3SMiri Korenblit void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1411f6780614SSara Sharon 				  struct ieee80211_vif *vif)
1412f6780614SSara Sharon {
1413f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1414f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1415f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1416f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1417f6780614SSara Sharon 							  mvmvif->color)),
1418f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1419f6780614SSara Sharon 	};
1420f6780614SSara Sharon 
1421ad12b231SNathan Errera 	/*
1422ad12b231SNathan Errera 	 * In the new flow since FW is in charge of the timing,
1423ad12b231SNathan Errera 	 * if driver has canceled the channel switch he will receive the
1424ad12b231SNathan Errera 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1425ad12b231SNathan Errera 	 */
1426ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1427ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1428ad12b231SNathan Errera 		return;
1429ad12b231SNathan Errera 
1430f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1431f6780614SSara Sharon 
1432f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
143358ddd9b6SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
143458ddd9b6SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
143558ddd9b6SEmmanuel Grumbach 		iwl_mvm_remove_csa_period(mvm, vif);
143658ddd9b6SEmmanuel Grumbach 	else
1437f6780614SSara Sharon 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1438f6780614SSara Sharon 					     WIDE_ID(MAC_CONF_GROUP,
1439f6780614SSara Sharon 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1440f6780614SSara Sharon 					     0, sizeof(cmd), &cmd));
1441caf46377SSara Sharon 	mvmvif->csa_failed = true;
1442f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1443f6780614SSara Sharon 
1444caf46377SSara Sharon 	iwl_mvm_post_channel_switch(hw, vif);
1445f6780614SSara Sharon }
1446f6780614SSara Sharon 
14471ab26632SMiri Korenblit void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1448f6780614SSara Sharon {
1449f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1450f6780614SSara Sharon 	struct ieee80211_vif *vif;
1451f6780614SSara Sharon 
1452f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1453f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1454f6780614SSara Sharon 
145570162580SShaul Triebitz 	/* Trigger disconnect (should clear the CSA state) */
1456f6780614SSara Sharon 	ieee80211_chswitch_done(vif, false);
1457f6780614SSara Sharon }
1458f6780614SSara Sharon 
14595abf3154SMordechay Goodstein static u8
14605abf3154SMordechay Goodstein iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
14615abf3154SMordechay Goodstein {
14625abf3154SMordechay Goodstein 	int data_start;
14635abf3154SMordechay Goodstein 	int control_start;
14645abf3154SMordechay Goodstein 	int bw;
14655abf3154SMordechay Goodstein 
14665abf3154SMordechay Goodstein 	if (chandef->width == NL80211_CHAN_WIDTH_320)
14675abf3154SMordechay Goodstein 		bw = 320;
14685abf3154SMordechay Goodstein 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
14695abf3154SMordechay Goodstein 		bw = 160;
14705abf3154SMordechay Goodstein 	else
14715abf3154SMordechay Goodstein 		return 0;
14725abf3154SMordechay Goodstein 
14735abf3154SMordechay Goodstein 	/* data is bw wide so the start is half the width */
14745abf3154SMordechay Goodstein 	data_start = chandef->center_freq1 - bw / 2;
14755abf3154SMordechay Goodstein 	/* control is 20Mhz width */
14765abf3154SMordechay Goodstein 	control_start = chandef->chan->center_freq - 10;
14775abf3154SMordechay Goodstein 
14785abf3154SMordechay Goodstein 	return (control_start - data_start) / 80;
14795abf3154SMordechay Goodstein }
14805abf3154SMordechay Goodstein 
14811be4858eSJohannes Berg static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
14821be4858eSJohannes Berg 					 struct ieee80211_vif *vif)
1483e705c121SKalle Valo {
1484e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
14851be4858eSJohannes Berg 	int ret;
14861ab26632SMiri Korenblit 
14871ab26632SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
1488e705c121SKalle Valo 
14891be4858eSJohannes Berg 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
14901be4858eSJohannes Berg 	if (ret) {
14911be4858eSJohannes Berg 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
14921be4858eSJohannes Berg 		return ret;
14931be4858eSJohannes Berg 	}
14941be4858eSJohannes Berg 
14951be4858eSJohannes Berg 	/* Only queue for this station is the mcast queue,
14961be4858eSJohannes Berg 	 * which shouldn't be in TFD mask anyway
14971be4858eSJohannes Berg 	 */
14981be4858eSJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
14991be4858eSJohannes Berg 					vif->type,
15001be4858eSJohannes Berg 					IWL_STA_MULTICAST);
15011be4858eSJohannes Berg }
15021be4858eSJohannes Berg 
15031be4858eSJohannes Berg static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
15041be4858eSJohannes Berg 				     struct ieee80211_vif *vif)
15051be4858eSJohannes Berg {
15061be4858eSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
15071be4858eSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
15081be4858eSJohannes Berg 	int ret;
15091be4858eSJohannes Berg 
15101be4858eSJohannes Berg 	mutex_lock(&mvm->mutex);
15111be4858eSJohannes Berg 
1512e705c121SKalle Valo 	mvmvif->mvm = mvm;
1513650cadb7SGregory Greenman 
1514650cadb7SGregory Greenman 	/* the first link always points to the default one */
1515650cadb7SGregory Greenman 	mvmvif->link[0] = &mvmvif->deflink;
1516e705c121SKalle Valo 
1517e705c121SKalle Valo 	/*
1518e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1519e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1520e705c121SKalle Valo 	 * don't really have to check the types.
1521e705c121SKalle Valo 	 */
1522e705c121SKalle Valo 
1523e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1524e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1525650cadb7SGregory Greenman 		mvmvif->deflink.beacon_stats.accu_num_beacons +=
1526650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.num_beacons;
1527e705c121SKalle Valo 
1528e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
15291be4858eSJohannes Berg 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
15301be4858eSJohannes Berg 	if (ret)
15311be4858eSJohannes Berg 		goto out;
1532e705c121SKalle Valo 
1533698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1534698478c4SSara Sharon 
15351be4858eSJohannes Berg 	/* Currently not much to do for NAN */
15361be4858eSJohannes Berg 	if (vif->type == NL80211_IFTYPE_NAN) {
15371be4858eSJohannes Berg 		ret = 0;
15381be4858eSJohannes Berg 		goto out;
15391be4858eSJohannes Berg 	}
15401be4858eSJohannes Berg 
1541e705c121SKalle Valo 	/*
1542e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1543e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1544e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1545e705c121SKalle Valo 	 * only after the binding.
1546e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1547e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1548e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1549e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1550e705c121SKalle Valo 	 * allocating resources :)
1551e705c121SKalle Valo 	 */
1552e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1553e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
15541ab26632SMiri Korenblit 		iwl_mvm_vif_dbgfs_register(mvm, vif);
15551be4858eSJohannes Berg 		ret = 0;
15561be4858eSJohannes Berg 		goto out;
15571ab26632SMiri Korenblit 	}
15581ab26632SMiri Korenblit 
15591ab26632SMiri Korenblit 	mvmvif->features |= hw->netdev_features;
1560650cadb7SGregory Greenman 
1561e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1562e705c121SKalle Valo 	if (ret)
156398c0de7bSMiri Korenblit 		goto out_unlock;
1564e705c121SKalle Valo 
1565e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1566e705c121SKalle Valo 	if (ret)
1567e705c121SKalle Valo 		goto out_remove_mac;
1568e705c121SKalle Valo 
1569e705c121SKalle Valo 	/* beacon filtering */
1570e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1571e705c121SKalle Valo 	if (ret)
1572e705c121SKalle Valo 		goto out_remove_mac;
1573e705c121SKalle Valo 
1574e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1575e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1576e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1577e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1578e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1579e705c121SKalle Valo 	}
1580e705c121SKalle Valo 
1581e705c121SKalle Valo 	/*
1582e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1583e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1584e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1585e705c121SKalle Valo 	 */
1586e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1587e705c121SKalle Valo 
1588650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1589650cadb7SGregory Greenman 		if (!mvmvif->deflink.phy_ctxt) {
1590e705c121SKalle Valo 			ret = -ENOSPC;
1591e705c121SKalle Valo 			goto out_free_bf;
1592e705c121SKalle Valo 		}
1593e705c121SKalle Valo 
1594650cadb7SGregory Greenman 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
1595e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1596e705c121SKalle Valo 		if (ret)
1597e705c121SKalle Valo 			goto out_unref_phy;
1598e705c121SKalle Valo 
1599d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1600e705c121SKalle Valo 		if (ret)
1601e705c121SKalle Valo 			goto out_unbind;
1602e705c121SKalle Valo 
1603e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1604e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1605e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1606e705c121SKalle Valo 	}
1607e705c121SKalle Valo 
1608b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1609f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1610f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1611b0ffe455SJohannes Berg 
16125abf3154SMordechay Goodstein 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1613baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
16145abf3154SMordechay Goodstein 		mvm->monitor_p80 =
16155abf3154SMordechay Goodstein 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chandef);
16165abf3154SMordechay Goodstein 	}
1617baf41bc3SShaul Triebitz 
1618e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
16196d19a5ebSEmmanuel Grumbach 
16206d19a5ebSEmmanuel Grumbach 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
16216d19a5ebSEmmanuel Grumbach 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
16226d19a5ebSEmmanuel Grumbach 	    !mvm->csme_vif && mvm->mei_registered) {
16236d19a5ebSEmmanuel Grumbach 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
16246d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
16256d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = vif;
16266d19a5ebSEmmanuel Grumbach 	}
16276d19a5ebSEmmanuel Grumbach 
16281ab26632SMiri Korenblit out:
16291ab26632SMiri Korenblit 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
16301ab26632SMiri Korenblit 		     vif->type == NL80211_IFTYPE_ADHOC))
16311ab26632SMiri Korenblit 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
16321ab26632SMiri Korenblit 
1633e705c121SKalle Valo 	goto out_unlock;
1634e705c121SKalle Valo 
1635e705c121SKalle Valo  out_unbind:
1636e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1637e705c121SKalle Valo  out_unref_phy:
1638650cadb7SGregory Greenman 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1639e705c121SKalle Valo  out_free_bf:
1640e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1641e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1642e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1643e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1644e705c121SKalle Valo 	}
1645e705c121SKalle Valo  out_remove_mac:
1646650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
1647e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1648e705c121SKalle Valo  out_unlock:
1649e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1650e705c121SKalle Valo 
1651e705c121SKalle Valo 	return ret;
1652e705c121SKalle Valo }
1653e705c121SKalle Valo 
1654cb145863SJohannes Berg void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1655e705c121SKalle Valo 				 struct ieee80211_vif *vif)
1656e705c121SKalle Valo {
1657e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1658e705c121SKalle Valo 		/*
1659e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1660e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1661e705c121SKalle Valo 		 * queue are sent in ROC session.
1662e705c121SKalle Valo 		 */
1663e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1664e705c121SKalle Valo 	}
1665e705c121SKalle Valo }
1666e705c121SKalle Valo 
166760efeca1SMiri Korenblit /* This function is doing the common part of removing the interface for
166860efeca1SMiri Korenblit  * both - MLD and non-MLD modes. Returns true if removing the interface
166960efeca1SMiri Korenblit  * is done
167060efeca1SMiri Korenblit  */
1671cb145863SJohannes Berg static bool iwl_mvm_mac_remove_interface_common(struct ieee80211_hw *hw,
1672e705c121SKalle Valo 						struct ieee80211_vif *vif)
1673e705c121SKalle Valo {
1674e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1675e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
167686e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1677e705c121SKalle Valo 
1678e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1679e705c121SKalle Valo 
1680b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1681b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1682b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1683b0ffe455SJohannes Berg 
1684e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1685e705c121SKalle Valo 
16866d19a5ebSEmmanuel Grumbach 	if (vif == mvm->csme_vif) {
16876d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(NULL);
16886d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = NULL;
16896d19a5ebSEmmanuel Grumbach 	}
16906d19a5ebSEmmanuel Grumbach 
1691650cadb7SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
169286e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
1693650cadb7SGregory Greenman 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
169486e177d8SGregory Greenman 	if (probe_data)
169586e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
169686e177d8SGregory Greenman 
1697e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1698e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1699e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1700e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1701e705c121SKalle Valo 	}
1702e705c121SKalle Valo 
1703b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1704b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1705b73f9a4aSJohannes Berg 
1706e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1707e705c121SKalle Valo 
1708e705c121SKalle Valo 	/*
1709e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1710e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1711e705c121SKalle Valo 	 */
1712e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1713e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1714e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1715e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1716e705c121SKalle Valo 			mvm->noa_vif = NULL;
1717e705c121SKalle Valo 			mvm->noa_duration = 0;
1718e705c121SKalle Valo 		}
1719e705c121SKalle Valo #endif
172060efeca1SMiri Korenblit 		return true;
1721e705c121SKalle Valo 	}
1722e705c121SKalle Valo 
172360efeca1SMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
172460efeca1SMiri Korenblit 	return false;
172560efeca1SMiri Korenblit }
172660efeca1SMiri Korenblit 
172760efeca1SMiri Korenblit static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
172860efeca1SMiri Korenblit 					 struct ieee80211_vif *vif)
172960efeca1SMiri Korenblit {
173060efeca1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
173160efeca1SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
173260efeca1SMiri Korenblit 
173360efeca1SMiri Korenblit 	if (iwl_mvm_mac_remove_interface_common(hw, vif))
173460efeca1SMiri Korenblit 		goto out;
173560efeca1SMiri Korenblit 
1736e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1737e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1738d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1739e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1740650cadb7SGregory Greenman 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1741650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = NULL;
1742e705c121SKalle Valo 	}
1743e705c121SKalle Valo 
1744e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1745e705c121SKalle Valo 
1746698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1747698478c4SSara Sharon 
1748baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1749baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1750baf41bc3SShaul Triebitz 
175160efeca1SMiri Korenblit out:
175260efeca1SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP ||
175360efeca1SMiri Korenblit 	    vif->type == NL80211_IFTYPE_ADHOC) {
1754650cadb7SGregory Greenman 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
175560efeca1SMiri Korenblit 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1756e705c121SKalle Valo 	}
1757e705c121SKalle Valo 
1758e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1759e705c121SKalle Valo }
1760e705c121SKalle Valo 
1761e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1762e705c121SKalle Valo 	struct iwl_mvm *mvm;
1763e705c121SKalle Valo 	int port_id;
1764e705c121SKalle Valo };
1765e705c121SKalle Valo 
1766e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1767e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1768e705c121SKalle Valo {
1769e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1770e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1771e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
177297bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
177397bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
177497bce57bSLuca Coelho 		.flags = CMD_ASYNC,
177597bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
177697bce57bSLuca Coelho 	};
1777e705c121SKalle Valo 	int ret, len;
1778e705c121SKalle Valo 
1779e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1780e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1781e705c121SKalle Valo 		return;
1782e705c121SKalle Valo 
1783e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1784f276e20bSJohannes Berg 	    !vif->cfg.assoc)
1785e705c121SKalle Valo 		return;
1786e705c121SKalle Valo 
1787e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1788e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1789e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1790e705c121SKalle Valo 
179197bce57bSLuca Coelho 	hcmd.len[0] = len;
179297bce57bSLuca Coelho 	hcmd.data[0] = cmd;
179397bce57bSLuca Coelho 
179497bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1795e705c121SKalle Valo 	if (ret)
1796e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1797e705c121SKalle Valo }
1798e705c121SKalle Valo 
1799e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1800e705c121SKalle Valo {
1801e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1802e705c121SKalle Valo 		.mvm = mvm,
1803e705c121SKalle Valo 	};
1804db66abeeSJohannes Berg 	int ret;
1805e705c121SKalle Valo 
1806e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1807e705c121SKalle Valo 
1808e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1809e705c121SKalle Valo 		return;
1810e705c121SKalle Valo 
1811e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1812e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1813e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1814db66abeeSJohannes Berg 
1815db66abeeSJohannes Berg 	/*
1816db66abeeSJohannes Berg 	 * Send a (synchronous) ech command so that we wait for the
1817db66abeeSJohannes Berg 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
1818db66abeeSJohannes Berg 	 * the interface iterator. Otherwise, we might get here over
1819db66abeeSJohannes Berg 	 * and over again (by userspace just sending a lot of these)
1820db66abeeSJohannes Berg 	 * and the CPU can send them faster than the firmware can
1821db66abeeSJohannes Berg 	 * process them.
1822db66abeeSJohannes Berg 	 * Note that the CPU is still faster - but with this we'll
1823db66abeeSJohannes Berg 	 * actually send fewer commands overall because the CPU will
1824db66abeeSJohannes Berg 	 * not schedule the work in mac80211 as frequently if it's
1825db66abeeSJohannes Berg 	 * still running when rescheduled (possibly multiple times).
1826db66abeeSJohannes Berg 	 */
1827db66abeeSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1828db66abeeSJohannes Berg 	if (ret)
1829db66abeeSJohannes Berg 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
1830e705c121SKalle Valo }
1831e705c121SKalle Valo 
1832cbce62a3SMiri Korenblit u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1833e705c121SKalle Valo 			      struct netdev_hw_addr_list *mc_list)
1834e705c121SKalle Valo {
1835e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1836e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1837e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1838e705c121SKalle Valo 	int addr_count;
1839e705c121SKalle Valo 	bool pass_all;
1840e705c121SKalle Valo 	int len;
1841e705c121SKalle Valo 
1842e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1843e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1844e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1845e705c121SKalle Valo 	if (pass_all)
1846e705c121SKalle Valo 		addr_count = 0;
1847e705c121SKalle Valo 
1848e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1849e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1850e705c121SKalle Valo 	if (!cmd)
1851e705c121SKalle Valo 		return 0;
1852e705c121SKalle Valo 
1853e705c121SKalle Valo 	if (pass_all) {
1854e705c121SKalle Valo 		cmd->pass_all = 1;
1855e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1856e705c121SKalle Valo 	}
1857e705c121SKalle Valo 
1858e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1859e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1860e705c121SKalle Valo 				   cmd->count, addr->addr);
1861e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1862e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1863e705c121SKalle Valo 		cmd->count++;
1864e705c121SKalle Valo 	}
1865e705c121SKalle Valo 
1866e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1867e705c121SKalle Valo }
1868e705c121SKalle Valo 
1869cbce62a3SMiri Korenblit void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1870e705c121SKalle Valo 			      unsigned int changed_flags,
1871cbce62a3SMiri Korenblit 			      unsigned int *total_flags, u64 multicast)
1872e705c121SKalle Valo {
1873e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1874e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1875e705c121SKalle Valo 
1876e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1877e705c121SKalle Valo 
1878e705c121SKalle Valo 	/* replace previous configuration */
1879e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1880e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1881e705c121SKalle Valo 
1882e705c121SKalle Valo 	if (!cmd)
1883e705c121SKalle Valo 		goto out;
1884e705c121SKalle Valo 
188561e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
188661e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
188761e7d91bSLuca Coelho 
188861e7d91bSLuca Coelho 	if (cmd->pass_all)
188961e7d91bSLuca Coelho 		cmd->count = 0;
189061e7d91bSLuca Coelho 
1891e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1892e705c121SKalle Valo out:
1893e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1894e705c121SKalle Valo 	*total_flags = 0;
1895e705c121SKalle Valo }
1896e705c121SKalle Valo 
1897e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1898e705c121SKalle Valo 					struct ieee80211_vif *vif,
1899e705c121SKalle Valo 					unsigned int filter_flags,
1900e705c121SKalle Valo 					unsigned int changed_flags)
1901e705c121SKalle Valo {
1902e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1903e705c121SKalle Valo 
1904e705c121SKalle Valo 	/* We support only filter for probe requests */
1905e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1906e705c121SKalle Valo 		return;
1907e705c121SKalle Valo 
1908e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1909f276e20bSJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
1910e705c121SKalle Valo 	    !vif->p2p)
1911e705c121SKalle Valo 		return;
1912e705c121SKalle Valo 
1913e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1914e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1915e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1916e705c121SKalle Valo }
1917e705c121SKalle Valo 
191822c58834SGregory Greenman int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1919a07a8f37SSara Sharon {
1920a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1921a07a8f37SSara Sharon 
1922a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1923a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1924a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1925a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1926a07a8f37SSara Sharon 
1927a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1928a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1929a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1930a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1931a07a8f37SSara Sharon }
1932a07a8f37SSara Sharon 
1933f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1934f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1935f92659a1SSara Sharon {
1936d0a9123eSJohannes Berg 	if (vif->bss_conf.mu_mimo_owner) {
1937f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1938f92659a1SSara Sharon 
1939f92659a1SSara Sharon 		/*
1940f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1941f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1942f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
1943f92659a1SSara Sharon 		 */
1944afe0d181SJohannes Berg 		ieee80211_update_mu_groups(vif, 0,
1945f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
1946f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
1947f92659a1SSara Sharon 	}
1948f92659a1SSara Sharon }
1949f92659a1SSara Sharon 
1950f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1951f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
1952f92659a1SSara Sharon {
1953f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1954f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1955f92659a1SSara Sharon 
1956f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
1957f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1958f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
1959f92659a1SSara Sharon }
1960f92659a1SSara Sharon 
1961514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1962514c3069SLuca Coelho {
1963514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
1964514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
1965514c3069SLuca Coelho 	u8 residue_bits;
1966514c3069SLuca Coelho 	u8 res;
1967514c3069SLuca Coelho 
1968514c3069SLuca Coelho 	if (bit_num <= 5)
1969514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
1970514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
1971514c3069SLuca Coelho 
1972514c3069SLuca Coelho 	/*
1973514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
1974514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
1975514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
1976514c3069SLuca Coelho 	 */
1977514c3069SLuca Coelho 
1978514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
1979514c3069SLuca Coelho 
1980514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
1981514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
1982514c3069SLuca Coelho 	      residue_bits;
1983514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
1984514c3069SLuca Coelho 
1985514c3069SLuca Coelho 	return res;
1986514c3069SLuca Coelho }
1987514c3069SLuca Coelho 
1988091296d3SMiri Korenblit static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
1989091296d3SMiri Korenblit 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
1990cb63eb43SMiri Korenblit 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
1991cb63eb43SMiri Korenblit 			      bool inheritance)
1992091296d3SMiri Korenblit {
1993091296d3SMiri Korenblit 	int i;
1994091296d3SMiri Korenblit 
1995091296d3SMiri Korenblit 	/*
1996091296d3SMiri Korenblit 	* FW currently supports only nss == MAX_HE_SUPP_NSS
1997091296d3SMiri Korenblit 	*
1998091296d3SMiri Korenblit 	* If nss > MAX: we can ignore values we don't support
1999091296d3SMiri Korenblit 	* If nss < MAX: we can set zeros in other streams
2000091296d3SMiri Korenblit 	*/
2001091296d3SMiri Korenblit 	if (nss > MAX_HE_SUPP_NSS) {
20024d8421f2SJason A. Donenfeld 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2003091296d3SMiri Korenblit 			       MAX_HE_SUPP_NSS);
2004091296d3SMiri Korenblit 		nss = MAX_HE_SUPP_NSS;
2005091296d3SMiri Korenblit 	}
2006091296d3SMiri Korenblit 
2007091296d3SMiri Korenblit 	for (i = 0; i < nss; i++) {
2008091296d3SMiri Korenblit 		u8 ru_index_tmp = ru_index_bitmap << 1;
2009091296d3SMiri Korenblit 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2010091296d3SMiri Korenblit 		u8 bw;
2011091296d3SMiri Korenblit 
2012091296d3SMiri Korenblit 		for (bw = 0;
2013091296d3SMiri Korenblit 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2014091296d3SMiri Korenblit 		     bw++) {
2015091296d3SMiri Korenblit 			ru_index_tmp >>= 1;
2016091296d3SMiri Korenblit 
2017cb63eb43SMiri Korenblit 			/*
2018cb63eb43SMiri Korenblit 			* According to the 11be spec, if for a specific BW the PPE Thresholds
2019cb63eb43SMiri Korenblit 			* isn't present - it should inherit the thresholds from the last
2020cb63eb43SMiri Korenblit 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2021cb63eb43SMiri Korenblit 			* this inheritance - continue in this case
2022cb63eb43SMiri Korenblit 			*/
2023cb63eb43SMiri Korenblit 			if (!(ru_index_tmp & 1)) {
2024cb63eb43SMiri Korenblit 				if (inheritance)
2025cb63eb43SMiri Korenblit 					goto set_thresholds;
2026cb63eb43SMiri Korenblit 				else
2027091296d3SMiri Korenblit 					continue;
2028cb63eb43SMiri Korenblit 			}
2029091296d3SMiri Korenblit 
2030091296d3SMiri Korenblit 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2031091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2032091296d3SMiri Korenblit 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2033091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2034091296d3SMiri Korenblit 
2035cb63eb43SMiri Korenblit set_thresholds:
2036091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2037091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2038091296d3SMiri Korenblit 		}
2039091296d3SMiri Korenblit 	}
2040091296d3SMiri Korenblit }
2041091296d3SMiri Korenblit 
2042091296d3SMiri Korenblit static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
204357974a55SGregory Greenman 					    struct ieee80211_link_sta *link_sta,
2044cb63eb43SMiri Korenblit 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2045cb63eb43SMiri Korenblit 					    bool inheritance)
2046091296d3SMiri Korenblit {
204757974a55SGregory Greenman 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
204857974a55SGregory Greenman 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
204957974a55SGregory Greenman 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2050091296d3SMiri Korenblit 	u8 ru_index_bitmap =
2051091296d3SMiri Korenblit 		u8_get_bits(*ppe,
2052091296d3SMiri Korenblit 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2053091296d3SMiri Korenblit 	/* Starting after PPE header */
2054091296d3SMiri Korenblit 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2055091296d3SMiri Korenblit 
2056cb63eb43SMiri Korenblit 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2057cb63eb43SMiri Korenblit 			  inheritance);
2058091296d3SMiri Korenblit }
2059091296d3SMiri Korenblit 
20604df6a075SMiri Korenblit static int
20614df6a075SMiri Korenblit iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
20624df6a075SMiri Korenblit 					 u8 nominal_padding)
2063091296d3SMiri Korenblit {
2064091296d3SMiri Korenblit 	int low_th = -1;
2065091296d3SMiri Korenblit 	int high_th = -1;
2066091296d3SMiri Korenblit 	int i;
2067091296d3SMiri Korenblit 
2068cb63eb43SMiri Korenblit 	/* all the macros are the same for EHT and HE */
2069091296d3SMiri Korenblit 	switch (nominal_padding) {
2070cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2071091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2072091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2073091296d3SMiri Korenblit 		break;
2074cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2075091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_BPSK;
2076091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2077091296d3SMiri Korenblit 		break;
2078cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2079cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2080091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2081091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_BPSK;
2082091296d3SMiri Korenblit 		break;
2083091296d3SMiri Korenblit 	}
2084091296d3SMiri Korenblit 
20854df6a075SMiri Korenblit 	if (low_th < 0 || high_th < 0)
20864df6a075SMiri Korenblit 		return -EINVAL;
20874df6a075SMiri Korenblit 
2088091296d3SMiri Korenblit 	/* Set the PPE thresholds accordingly */
2089091296d3SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2090091296d3SMiri Korenblit 		u8 bw;
2091091296d3SMiri Korenblit 
2092091296d3SMiri Korenblit 		for (bw = 0;
2093091296d3SMiri Korenblit 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2094091296d3SMiri Korenblit 			bw++) {
2095091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2096091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2097091296d3SMiri Korenblit 		}
2098091296d3SMiri Korenblit 	}
2099091296d3SMiri Korenblit 
21004df6a075SMiri Korenblit 	return 0;
2101091296d3SMiri Korenblit }
2102091296d3SMiri Korenblit 
2103cb63eb43SMiri Korenblit static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2104cb63eb43SMiri Korenblit 					 u8 nominal_padding)
2105cb63eb43SMiri Korenblit {
2106cb63eb43SMiri Korenblit 	int i;
2107cb63eb43SMiri Korenblit 
2108cb63eb43SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2109cb63eb43SMiri Korenblit 		u8 bw;
2110cb63eb43SMiri Korenblit 
2111cb63eb43SMiri Korenblit 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2112cb63eb43SMiri Korenblit 		     bw++) {
2113cb63eb43SMiri Korenblit 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2114cb63eb43SMiri Korenblit 
2115cb63eb43SMiri Korenblit 			if (nominal_padding >
2116cb63eb43SMiri Korenblit 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2117cb63eb43SMiri Korenblit 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2118cb63eb43SMiri Korenblit 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2119cb63eb43SMiri Korenblit 			else if (nominal_padding ==
2120cb63eb43SMiri Korenblit 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2121cb63eb43SMiri Korenblit 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2122cb63eb43SMiri Korenblit 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2123cb63eb43SMiri Korenblit 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2124cb63eb43SMiri Korenblit 		}
2125cb63eb43SMiri Korenblit 	}
2126cb63eb43SMiri Korenblit }
2127cb63eb43SMiri Korenblit 
21284df6a075SMiri Korenblit /* Set the pkt_ext field according to PPE Thresholds element */
212957974a55SGregory Greenman int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
213057974a55SGregory Greenman 			    struct ieee80211_link_sta *link_sta,
21314df6a075SMiri Korenblit 			    struct iwl_he_pkt_ext_v2 *pkt_ext)
21324df6a075SMiri Korenblit {
21334df6a075SMiri Korenblit 	u8 nominal_padding;
21344df6a075SMiri Korenblit 	int i, ret = 0;
21354df6a075SMiri Korenblit 
213657974a55SGregory Greenman 	if (WARN_ON(!link_sta))
213757974a55SGregory Greenman 		return -EINVAL;
213857974a55SGregory Greenman 
21394df6a075SMiri Korenblit 	/* Initialize the PPE thresholds to "None" (7), as described in Table
21404df6a075SMiri Korenblit 	 * 9-262ac of 80211.ax/D3.0.
21414df6a075SMiri Korenblit 	 */
21424df6a075SMiri Korenblit 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
21434df6a075SMiri Korenblit 	       sizeof(struct iwl_he_pkt_ext_v2));
21444df6a075SMiri Korenblit 
214557974a55SGregory Greenman 	if (link_sta->eht_cap.has_eht) {
21464df6a075SMiri Korenblit 		nominal_padding =
214757974a55SGregory Greenman 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
21484df6a075SMiri Korenblit 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
21494df6a075SMiri Korenblit 
21504df6a075SMiri Korenblit 		/* If PPE Thresholds exists, parse them into a FW-familiar
21514df6a075SMiri Korenblit 		 * format.
21524df6a075SMiri Korenblit 		 */
215357974a55SGregory Greenman 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
21544df6a075SMiri Korenblit 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
215557974a55SGregory Greenman 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
21564df6a075SMiri Korenblit 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
215757974a55SGregory Greenman 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
21584df6a075SMiri Korenblit 			u8 ru_index_bitmap =
21594df6a075SMiri Korenblit 				u16_get_bits(*ppe,
21604df6a075SMiri Korenblit 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
21614df6a075SMiri Korenblit 			 /* Starting after PPE header */
21624df6a075SMiri Korenblit 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
21634df6a075SMiri Korenblit 
21644df6a075SMiri Korenblit 			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
21654df6a075SMiri Korenblit 					  ppe, ppe_pos_bit, true);
216657974a55SGregory Greenman 		/* EHT PPE Thresholds doesn't exist - set the API according to
216757974a55SGregory Greenman 		 * HE PPE Tresholds
21684df6a075SMiri Korenblit 		 */
216957974a55SGregory Greenman 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
21704df6a075SMiri Korenblit 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
21714df6a075SMiri Korenblit 			/* Even though HE Capabilities IE doesn't contain PPE
21724df6a075SMiri Korenblit 			 * Thresholds for BW 320Mhz, thresholds for this BW will
21734df6a075SMiri Korenblit 			 * be filled in with the same values as 160Mhz, due to
21744df6a075SMiri Korenblit 			 * the inheritance, as required.
21754df6a075SMiri Korenblit 			 */
217657974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
21774df6a075SMiri Korenblit 							true);
21784df6a075SMiri Korenblit 
21794df6a075SMiri Korenblit 			/* According to the requirements, for MCSs 12-13 the
21804df6a075SMiri Korenblit 			 * maximum value between HE PPE Threshold and Common
21814df6a075SMiri Korenblit 			 * Nominal Packet Padding needs to be taken
21824df6a075SMiri Korenblit 			 */
21834df6a075SMiri Korenblit 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
21844df6a075SMiri Korenblit 
21854df6a075SMiri Korenblit 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
21864df6a075SMiri Korenblit 		 * take the Thresholds from Common Nominal Packet Padding field
21874df6a075SMiri Korenblit 		 */
21884df6a075SMiri Korenblit 		} else {
21894df6a075SMiri Korenblit 			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
21904df6a075SMiri Korenblit 								       nominal_padding);
21914df6a075SMiri Korenblit 		}
219257974a55SGregory Greenman 	} else if (link_sta->he_cap.has_he) {
21934df6a075SMiri Korenblit 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
219457974a55SGregory Greenman 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
21954df6a075SMiri Korenblit 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
219657974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
21974df6a075SMiri Korenblit 							false);
21984df6a075SMiri Korenblit 		/* PPE Thresholds doesn't exist - set the API PPE values
21994df6a075SMiri Korenblit 		 * according to Common Nominal Packet Padding field.
22004df6a075SMiri Korenblit 		 */
22014df6a075SMiri Korenblit 		} else {
22024df6a075SMiri Korenblit 			nominal_padding =
220357974a55SGregory Greenman 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
22044df6a075SMiri Korenblit 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
22054df6a075SMiri Korenblit 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
22064df6a075SMiri Korenblit 				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
22074df6a075SMiri Korenblit 									       nominal_padding);
22084df6a075SMiri Korenblit 		}
22094df6a075SMiri Korenblit 	}
22104df6a075SMiri Korenblit 
22114df6a075SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
22124df6a075SMiri Korenblit 		int bw;
22134df6a075SMiri Korenblit 
22144df6a075SMiri Korenblit 		for (bw = 0;
22154df6a075SMiri Korenblit 		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
22164df6a075SMiri Korenblit 		     bw++) {
22174df6a075SMiri Korenblit 			u8 *qam_th =
22184df6a075SMiri Korenblit 				&pkt_ext->pkt_ext_qam_th[i][bw][0];
22194df6a075SMiri Korenblit 
22204df6a075SMiri Korenblit 			IWL_DEBUG_HT(mvm,
22214df6a075SMiri Korenblit 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
22224df6a075SMiri Korenblit 				     i, bw, qam_th[0], qam_th[1]);
22234df6a075SMiri Korenblit 		}
22244df6a075SMiri Korenblit 	}
22254df6a075SMiri Korenblit 	return ret;
22264df6a075SMiri Korenblit }
22274df6a075SMiri Korenblit 
22289c4f15caSMiri Korenblit /*
22299c4f15caSMiri Korenblit  * This function sets the MU EDCA parameters ans returns whether MU EDCA
22309c4f15caSMiri Korenblit  * is enabled or not
22319c4f15caSMiri Korenblit  */
223255eb1c5fSMiri Korenblit bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
22339c4f15caSMiri Korenblit 				   struct iwl_mvm_vif *mvmvif,
223455eb1c5fSMiri Korenblit 				   struct iwl_he_backoff_conf *trig_based_txf)
22359c4f15caSMiri Korenblit {
22369c4f15caSMiri Korenblit 	int i;
22379c4f15caSMiri Korenblit 	/* Mark MU EDCA as enabled, unless none detected on some AC */
22389c4f15caSMiri Korenblit 	bool mu_edca_enabled = true;
22399c4f15caSMiri Korenblit 
22409c4f15caSMiri Korenblit 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
22419c4f15caSMiri Korenblit 		struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2242650cadb7SGregory Greenman 			&mvmvif->deflink.queue_params[i].mu_edca_param_rec;
22439c4f15caSMiri Korenblit 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
22449c4f15caSMiri Korenblit 
2245650cadb7SGregory Greenman 		if (!mvmvif->deflink.queue_params[i].mu_edca) {
22469c4f15caSMiri Korenblit 			mu_edca_enabled = false;
22479c4f15caSMiri Korenblit 			break;
22489c4f15caSMiri Korenblit 		}
22499c4f15caSMiri Korenblit 
22509c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmin =
22519c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
22529c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmax =
22539c4f15caSMiri Korenblit 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
22549c4f15caSMiri Korenblit 		trig_based_txf[ac].aifsn =
22559c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->aifsn & 0xf);
22569c4f15caSMiri Korenblit 		trig_based_txf[ac].mu_time =
22579c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->mu_edca_timer);
22589c4f15caSMiri Korenblit 	}
22599c4f15caSMiri Korenblit 
22609c4f15caSMiri Korenblit 	return mu_edca_enabled;
22619c4f15caSMiri Korenblit }
22629c4f15caSMiri Korenblit 
22639be162a7SMiri Korenblit bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
22649c4f15caSMiri Korenblit {
22659c4f15caSMiri Korenblit 	const struct ieee80211_supported_band *sband;
22669c4f15caSMiri Korenblit 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
22679c4f15caSMiri Korenblit 
22689c4f15caSMiri Korenblit 	/* This capability is the same for all bands,
22699c4f15caSMiri Korenblit 	 * so take it from one of them.
22709c4f15caSMiri Korenblit 	 */
22719c4f15caSMiri Korenblit 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
2272*1ec7291eSJohannes Berg 	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
22739c4f15caSMiri Korenblit 
22749c4f15caSMiri Korenblit 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
22759c4f15caSMiri Korenblit 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
22769c4f15caSMiri Korenblit }
22779c4f15caSMiri Korenblit 
227857974a55SGregory Greenman __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
227957974a55SGregory Greenman 				 struct ieee80211_link_sta *link_sta)
22804df6a075SMiri Korenblit {
228157974a55SGregory Greenman 	u8 *mac_cap_info =
228257974a55SGregory Greenman 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
22834df6a075SMiri Korenblit 	__le32 htc_flags = 0;
22844df6a075SMiri Korenblit 
22854df6a075SMiri Korenblit 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
22864df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
22874df6a075SMiri Korenblit 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
22884df6a075SMiri Korenblit 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
22894df6a075SMiri Korenblit 		u8 link_adap =
22904df6a075SMiri Korenblit 			((mac_cap_info[2] &
22914df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
22924df6a075SMiri Korenblit 			 (mac_cap_info[1] &
22934df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
22944df6a075SMiri Korenblit 
22954df6a075SMiri Korenblit 		if (link_adap == 2)
22964df6a075SMiri Korenblit 			htc_flags |=
22974df6a075SMiri Korenblit 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
22984df6a075SMiri Korenblit 		else if (link_adap == 3)
22994df6a075SMiri Korenblit 			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
23004df6a075SMiri Korenblit 	}
23014df6a075SMiri Korenblit 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
23024df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
23034df6a075SMiri Korenblit 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
23044df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
23054df6a075SMiri Korenblit 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
23064df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
23074df6a075SMiri Korenblit 
23084df6a075SMiri Korenblit 	return htc_flags;
23094df6a075SMiri Korenblit }
23104df6a075SMiri Korenblit 
2311514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2312514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2313514c3069SLuca Coelho {
2314514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
231542506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2316514c3069SLuca Coelho 		.sta_id = sta_id,
2317514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2318dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2319514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2320514c3069SLuca Coelho 		.frame_time_rts_th =
2321514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2322514c3069SLuca Coelho 	};
232342506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
232442506dd2SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
232542506dd2SJohannes Berg 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
232642506dd2SJohannes Berg 	int size;
2327514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2328514c3069SLuca Coelho 	u32 flags;
2329514c3069SLuca Coelho 	int i;
233042506dd2SJohannes Berg 	void *cmd;
233142506dd2SJohannes Berg 
233242506dd2SJohannes Berg 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
233342506dd2SJohannes Berg 		ver = 1;
233442506dd2SJohannes Berg 
233542506dd2SJohannes Berg 	switch (ver) {
233642506dd2SJohannes Berg 	case 1:
233742506dd2SJohannes Berg 		/* same layout as v2 except some data at the end */
233842506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
233942506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
234042506dd2SJohannes Berg 		break;
234142506dd2SJohannes Berg 	case 2:
234242506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
234342506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
234442506dd2SJohannes Berg 		break;
234542506dd2SJohannes Berg 	case 3:
234642506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd;
234742506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
234842506dd2SJohannes Berg 		break;
234942506dd2SJohannes Berg 	default:
235042506dd2SJohannes Berg 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
235142506dd2SJohannes Berg 		return;
235242506dd2SJohannes Berg 	}
2353514c3069SLuca Coelho 
2354514c3069SLuca Coelho 	rcu_read_lock();
2355514c3069SLuca Coelho 
2356514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
235712d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2358514c3069SLuca Coelho 		rcu_read_unlock();
2359514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2360514c3069SLuca Coelho 		return;
2361514c3069SLuca Coelho 	}
2362514c3069SLuca Coelho 
2363046d2e7cSSriram R 	if (!sta->deflink.he_cap.has_he) {
2364514c3069SLuca Coelho 		rcu_read_unlock();
2365514c3069SLuca Coelho 		return;
2366514c3069SLuca Coelho 	}
2367514c3069SLuca Coelho 
2368514c3069SLuca Coelho 	flags = 0;
2369514c3069SLuca Coelho 
23704f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
2371650cadb7SGregory Greenman 	if (mvmvif->deflink.he_ru_2mhz_block)
23724f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
23734f58121dSIlan Peer 
2374514c3069SLuca Coelho 	/* HTC flags */
237557974a55SGregory Greenman 	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2376514c3069SLuca Coelho 
23774df6a075SMiri Korenblit 	/* PPE Thresholds */
237857974a55SGregory Greenman 	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2379cb63eb43SMiri Korenblit 		flags |= STA_CTXT_HE_PACKET_EXT;
238073f23d91SShaul Triebitz 
2381046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
238273f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
238373f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
238473f23d91SShaul Triebitz 
2385046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
238673f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
238773f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
238873f23d91SShaul Triebitz 
2389514c3069SLuca Coelho 	rcu_read_unlock();
2390514c3069SLuca Coelho 
23919c4f15caSMiri Korenblit 	if (iwl_mvm_set_fw_mu_edca_params(mvm, mvmvif,
23929c4f15caSMiri Korenblit 					  &sta_ctxt_cmd.trig_based_txf[0]))
2393514c3069SLuca Coelho 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2394514c3069SLuca Coelho 
2395514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2396514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2397514c3069SLuca Coelho 
2398514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2399514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2400514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2401514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2402514c3069SLuca Coelho 	}
2403514c3069SLuca Coelho 
24049c4f15caSMiri Korenblit 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2405ee1a02d7SShaul Triebitz 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2406ee1a02d7SShaul Triebitz 
2407918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2408918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2409918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2410918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2411d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2412d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2413d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2414d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2415d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2416d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2417918cbf39SSara Sharon 	}
2418514c3069SLuca Coelho 
2419514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2420514c3069SLuca Coelho 
242142506dd2SJohannes Berg 	if (ver < 3) {
242242506dd2SJohannes Berg 		/* fields before pkt_ext */
242342506dd2SJohannes Berg 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
242442506dd2SJohannes Berg 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
242542506dd2SJohannes Berg 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
242642506dd2SJohannes Berg 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
242742506dd2SJohannes Berg 
242842506dd2SJohannes Berg 		/* pkt_ext */
242942506dd2SJohannes Berg 		for (i = 0;
243042506dd2SJohannes Berg 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
243142506dd2SJohannes Berg 		     i++) {
243242506dd2SJohannes Berg 			u8 bw;
243342506dd2SJohannes Berg 
243442506dd2SJohannes Berg 			for (bw = 0;
243542506dd2SJohannes Berg 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
243642506dd2SJohannes Berg 			     bw++) {
243742506dd2SJohannes Berg 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
243842506dd2SJohannes Berg 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
243942506dd2SJohannes Berg 
244042506dd2SJohannes Berg 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
244142506dd2SJohannes Berg 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
244242506dd2SJohannes Berg 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
244342506dd2SJohannes Berg 			}
244442506dd2SJohannes Berg 		}
244542506dd2SJohannes Berg 
244642506dd2SJohannes Berg 		/* fields after pkt_ext */
244742506dd2SJohannes Berg 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
244842506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
244942506dd2SJohannes Berg 			     sizeof(sta_ctxt_cmd_v2) -
245042506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
245142506dd2SJohannes Berg 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
245242506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
245342506dd2SJohannes Berg 		       (u8 *)&sta_ctxt_cmd +
245442506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
245542506dd2SJohannes Berg 		       sizeof(sta_ctxt_cmd) -
245642506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
245742506dd2SJohannes Berg 		sta_ctxt_cmd_v2.reserved3 = 0;
245842506dd2SJohannes Berg 	}
245942506dd2SJohannes Berg 
246042506dd2SJohannes Berg 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2461514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2462514c3069SLuca Coelho }
2463514c3069SLuca Coelho 
2464660eba5aSMiri Korenblit void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2465af84ac57SJohannes Berg 			   u32 duration_override)
2466af84ac57SJohannes Berg {
2467af84ac57SJohannes Berg 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2468af84ac57SJohannes Berg 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2469af84ac57SJohannes Berg 
2470af84ac57SJohannes Berg 	if (duration_override > duration)
2471af84ac57SJohannes Berg 		duration = duration_override;
2472af84ac57SJohannes Berg 
2473af84ac57SJohannes Berg 	/* Try really hard to protect the session and hear a beacon
2474af84ac57SJohannes Berg 	 * The new session protection command allows us to protect the
2475af84ac57SJohannes Berg 	 * session for a much longer time since the firmware will internally
2476af84ac57SJohannes Berg 	 * create two events: a 300TU one with a very high priority that
2477af84ac57SJohannes Berg 	 * won't be fragmented which should be enough for 99% of the cases,
2478af84ac57SJohannes Berg 	 * and another one (which we configure here to be 900TU long) which
2479af84ac57SJohannes Berg 	 * will have a slightly lower priority, but more importantly, can be
2480af84ac57SJohannes Berg 	 * fragmented so that it'll allow other activities to run.
2481af84ac57SJohannes Berg 	 */
2482af84ac57SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
2483af84ac57SJohannes Berg 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2484af84ac57SJohannes Berg 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2485af84ac57SJohannes Berg 						    min_duration, false);
2486af84ac57SJohannes Berg 	else
2487af84ac57SJohannes Berg 		iwl_mvm_protect_session(mvm, vif, duration,
2488af84ac57SJohannes Berg 					min_duration, 500, false);
2489af84ac57SJohannes Berg }
2490af84ac57SJohannes Berg 
2491660eba5aSMiri Korenblit /* Handle association common part to MLD and non-MLD modes */
2492660eba5aSMiri Korenblit void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2493660eba5aSMiri Korenblit 					    struct ieee80211_vif *vif,
2494660eba5aSMiri Korenblit 					    u64 changes)
2495660eba5aSMiri Korenblit {
2496660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2497660eba5aSMiri Korenblit 	int ret;
2498660eba5aSMiri Korenblit 
2499660eba5aSMiri Korenblit 	/* The firmware tracks the MU-MIMO group on its own.
2500660eba5aSMiri Korenblit 	 * However, on HW restart we should restore this data.
2501660eba5aSMiri Korenblit 	 */
2502660eba5aSMiri Korenblit 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2503660eba5aSMiri Korenblit 	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2504660eba5aSMiri Korenblit 		ret = iwl_mvm_update_mu_groups(mvm, vif);
2505660eba5aSMiri Korenblit 		if (ret)
2506660eba5aSMiri Korenblit 			IWL_ERR(mvm,
2507660eba5aSMiri Korenblit 				"failed to update VHT MU_MIMO groups\n");
2508660eba5aSMiri Korenblit 	}
2509660eba5aSMiri Korenblit 
2510660eba5aSMiri Korenblit 	iwl_mvm_recalc_multicast(mvm);
2511660eba5aSMiri Korenblit 
2512660eba5aSMiri Korenblit 	/* reset rssi values */
2513660eba5aSMiri Korenblit 	mvmvif->bf_data.ave_beacon_signal = 0;
2514660eba5aSMiri Korenblit 
2515660eba5aSMiri Korenblit 	iwl_mvm_bt_coex_vif_change(mvm);
25161a3e7039SGregory Greenman 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2517660eba5aSMiri Korenblit 					    IEEE80211_SMPS_AUTOMATIC);
2518660eba5aSMiri Korenblit 	if (fw_has_capa(&mvm->fw->ucode_capa,
2519660eba5aSMiri Korenblit 			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2520660eba5aSMiri Korenblit 		iwl_mvm_config_scan(mvm);
2521660eba5aSMiri Korenblit }
2522660eba5aSMiri Korenblit 
2523660eba5aSMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
25241a3e7039SGregory Greenman void
25251a3e7039SGregory Greenman iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2526660eba5aSMiri Korenblit 					struct ieee80211_vif *vif,
25271a3e7039SGregory Greenman 					struct ieee80211_bss_conf *link_conf,
2528660eba5aSMiri Korenblit 					u64 changes)
2529660eba5aSMiri Korenblit {
2530660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2531660eba5aSMiri Korenblit 	int ret;
2532660eba5aSMiri Korenblit 
2533660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BEACON_INFO) {
2534660eba5aSMiri Korenblit 		/* We received a beacon from the associated AP so
2535660eba5aSMiri Korenblit 		 * remove the session protection.
2536660eba5aSMiri Korenblit 		 */
2537660eba5aSMiri Korenblit 		iwl_mvm_stop_session_protection(mvm, vif);
2538660eba5aSMiri Korenblit 
2539660eba5aSMiri Korenblit 		iwl_mvm_sf_update(mvm, vif, false);
2540660eba5aSMiri Korenblit 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2541660eba5aSMiri Korenblit 	}
2542660eba5aSMiri Korenblit 
2543660eba5aSMiri Korenblit 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2544660eba5aSMiri Korenblit 		       /* Send power command on every beacon change,
2545660eba5aSMiri Korenblit 			* because we may have not enabled beacon abort yet.
2546660eba5aSMiri Korenblit 			*/
2547660eba5aSMiri Korenblit 		       BSS_CHANGED_BEACON_INFO)) {
2548660eba5aSMiri Korenblit 		ret = iwl_mvm_power_update_mac(mvm);
2549660eba5aSMiri Korenblit 		if (ret)
2550660eba5aSMiri Korenblit 			IWL_ERR(mvm, "failed to update power mode\n");
2551660eba5aSMiri Korenblit 	}
2552660eba5aSMiri Korenblit 
2553660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_CQM) {
2554660eba5aSMiri Korenblit 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2555660eba5aSMiri Korenblit 		/* reset cqm events tracking */
2556660eba5aSMiri Korenblit 		mvmvif->bf_data.last_cqm_event = 0;
2557660eba5aSMiri Korenblit 		if (mvmvif->bf_data.bf_enabled) {
255822c58834SGregory Greenman 			/* FIXME: need to update per link when FW API will
255922c58834SGregory Greenman 			 * support it
256022c58834SGregory Greenman 			 */
2561660eba5aSMiri Korenblit 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2562660eba5aSMiri Korenblit 			if (ret)
2563660eba5aSMiri Korenblit 				IWL_ERR(mvm,
2564660eba5aSMiri Korenblit 					"failed to update CQM thresholds\n");
2565660eba5aSMiri Korenblit 		}
2566660eba5aSMiri Korenblit 	}
2567660eba5aSMiri Korenblit 
2568660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BANDWIDTH)
25691a3e7039SGregory Greenman 		iwl_mvm_update_link_smps(vif, link_conf);
2570660eba5aSMiri Korenblit }
2571660eba5aSMiri Korenblit 
2572e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2573e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2574e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
25757b7090b4SJohannes Berg 					     u64 changes)
2576e705c121SKalle Valo {
2577e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2578e705c121SKalle Valo 	int ret;
2579e705c121SKalle Valo 
2580e705c121SKalle Valo 	/*
2581cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend
2582cdaba917SEmmanuel Grumbach 	 * on the beacon interval, which was not known when the station
2583cdaba917SEmmanuel Grumbach 	 * interface was added.
2584e705c121SKalle Valo 	 */
2585f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2586cb63eb43SMiri Korenblit 		if ((vif->bss_conf.he_support &&
2587cb63eb43SMiri Korenblit 		     !iwlwifi_mod_params.disable_11ax) ||
2588cb63eb43SMiri Korenblit 		    (vif->bss_conf.eht_support &&
2589cb63eb43SMiri Korenblit 		     !iwlwifi_mod_params.disable_11be))
2590650cadb7SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2591514c3069SLuca Coelho 
2592e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2593514c3069SLuca Coelho 	}
2594e705c121SKalle Valo 
259540ecdd01SShaul Triebitz 	/* Update MU EDCA params */
259640ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2597cb63eb43SMiri Korenblit 	    vif->cfg.assoc &&
2598cb63eb43SMiri Korenblit 	    ((vif->bss_conf.he_support &&
2599cb63eb43SMiri Korenblit 	      !iwlwifi_mod_params.disable_11ax) ||
2600cb63eb43SMiri Korenblit 	     (vif->bss_conf.eht_support &&
2601cb63eb43SMiri Korenblit 	      !iwlwifi_mod_params.disable_11be)))
2602650cadb7SGregory Greenman 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
260340ecdd01SShaul Triebitz 
2604e705c121SKalle Valo 	/*
2605e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2606e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2607e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2608e705c121SKalle Valo 	 * branch for disassociation below.
2609e705c121SKalle Valo 	 */
2610e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2611650cadb7SGregory Greenman 		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2612e705c121SKalle Valo 
2613650cadb7SGregory Greenman 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2614e705c121SKalle Valo 	if (ret)
2615e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2616e705c121SKalle Valo 
2617e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2618650cadb7SGregory Greenman 	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2619f276e20bSJohannes Berg 	mvmvif->associated = vif->cfg.assoc;
2620e705c121SKalle Valo 
2621e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2622f276e20bSJohannes Berg 		if (vif->cfg.assoc) {
2623e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2624e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2625650cadb7SGregory Greenman 			memset(&mvmvif->deflink.beacon_stats, 0,
2626650cadb7SGregory Greenman 			       sizeof(mvmvif->deflink.beacon_stats));
2627e705c121SKalle Valo 
2628e705c121SKalle Valo 			/* add quota for this interface */
2629e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2630e705c121SKalle Valo 			if (ret) {
2631e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2632e705c121SKalle Valo 				return;
2633e705c121SKalle Valo 			}
2634e705c121SKalle Valo 
2635e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2636fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2637fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2638fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2639e705c121SKalle Valo 				/*
2640e705c121SKalle Valo 				 * If we're restarting then the firmware will
2641e705c121SKalle Valo 				 * obviously have lost synchronisation with
2642e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2643e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2644e705c121SKalle Valo 				 * scheduling a session protection time event.
2645e705c121SKalle Valo 				 *
2646e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2647e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2648e705c121SKalle Valo 				 * the beacon interval.
2649e705c121SKalle Valo 				 *
2650e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2651e705c121SKalle Valo 				 * than a single interface.
2652fe959c7bSEmmanuel Grumbach 				 *
2653fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2654fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2655fe959c7bSEmmanuel Grumbach 				 * only anyway.
2656e705c121SKalle Valo 				 */
2657e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2658e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2659e705c121SKalle Valo 							5 * dur, false);
2660af84ac57SJohannes Berg 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2661af84ac57SJohannes Berg 					     &mvm->status) &&
2662af84ac57SJohannes Berg 				   !vif->bss_conf.dtim_period) {
2663af84ac57SJohannes Berg 				/*
2664af84ac57SJohannes Berg 				 * If we're not restarting and still haven't
2665af84ac57SJohannes Berg 				 * heard a beacon (dtim period unknown) then
2666af84ac57SJohannes Berg 				 * make sure we still have enough minimum time
2667af84ac57SJohannes Berg 				 * remaining in the time event, since the auth
2668af84ac57SJohannes Berg 				 * might actually have taken quite a while
2669af84ac57SJohannes Berg 				 * (especially for SAE) and so the remaining
2670af84ac57SJohannes Berg 				 * time could be small without us having heard
2671af84ac57SJohannes Berg 				 * a beacon yet.
2672af84ac57SJohannes Berg 				 */
2673af84ac57SJohannes Berg 				iwl_mvm_protect_assoc(mvm, vif, 0);
2674e705c121SKalle Valo 			}
2675e705c121SKalle Valo 
2676e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2677e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2678e705c121SKalle Valo 			if (vif->p2p) {
2679e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2680e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
26811a3e7039SGregory Greenman 						    IEEE80211_SMPS_DYNAMIC, 0);
2682e705c121SKalle Valo 			}
2683650cadb7SGregory Greenman 		} else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
26846d19a5ebSEmmanuel Grumbach 			iwl_mvm_mei_host_disassociated(mvm);
2685e705c121SKalle Valo 			/*
2686e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2687e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2688e705c121SKalle Valo 			 */
268969e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
269069e508b4SIlan Peer 			WARN_ONCE(ret &&
269169e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
269269e508b4SIlan Peer 					    &mvm->status),
2693e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2694e705c121SKalle Valo 
26956b28f978SEmmanuel Grumbach 			/*
26966b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
26976b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
26986b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
26996b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
27006b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
27016b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
27026b28f978SEmmanuel Grumbach 			 */
27036b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
27046b28f978SEmmanuel Grumbach 				      &mvm->status)) {
27055c75a208SJohannes Berg 				/* first remove remaining keys */
2706ba9eef6bSGregory Greenman 				iwl_mvm_sec_key_remove_ap(mvm, vif,
2707072573f6SJohannes Berg 							  &mvmvif->deflink, 0);
27085c75a208SJohannes Berg 
27096b28f978SEmmanuel Grumbach 				/*
27106b28f978SEmmanuel Grumbach 				 * Remove AP station now that
27116b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
27126b28f978SEmmanuel Grumbach 				 */
27136b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
2714650cadb7SGregory Greenman 							mvmvif->deflink.ap_sta_id);
2715e705c121SKalle Valo 				if (ret)
27166b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
27176b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2718e705c121SKalle Valo 
2719650cadb7SGregory Greenman 				mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
27206b28f978SEmmanuel Grumbach 			}
27216b28f978SEmmanuel Grumbach 
2722e705c121SKalle Valo 			/* remove quota for this interface */
2723e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2724e705c121SKalle Valo 			if (ret)
2725e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2726e705c121SKalle Valo 
2727e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2728e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2729e705c121SKalle Valo 			if (ret)
2730e705c121SKalle Valo 				IWL_ERR(mvm,
2731e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2732e705c121SKalle Valo 					vif->addr);
2733e705c121SKalle Valo 		}
2734e705c121SKalle Valo 
2735660eba5aSMiri Korenblit 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
2736a07a8f37SSara Sharon 	}
2737a07a8f37SSara Sharon 
27381a3e7039SGregory Greenman 	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
27391a3e7039SGregory Greenman 						changes);
2740b45242c9SAvraham Stern }
2741b45242c9SAvraham Stern 
2742f947b62cSMiri Korenblit bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2743f947b62cSMiri Korenblit 				  struct ieee80211_vif *vif,
2744f947b62cSMiri Korenblit 				  int *ret)
2745f947b62cSMiri Korenblit {
2746f947b62cSMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2747f947b62cSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2748f947b62cSMiri Korenblit 	int i;
2749e705c121SKalle Valo 
2750f947b62cSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
2751f947b62cSMiri Korenblit 
2752f947b62cSMiri Korenblit 	mvmvif->ap_assoc_sta_count = 0;
2753f947b62cSMiri Korenblit 
2754f947b62cSMiri Korenblit 	/* must be set before quota calculations */
2755f947b62cSMiri Korenblit 	mvmvif->ap_ibss_active = true;
2756f947b62cSMiri Korenblit 
2757f947b62cSMiri Korenblit 	/* send all the early keys to the device now */
2758f947b62cSMiri Korenblit 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2759f947b62cSMiri Korenblit 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2760f947b62cSMiri Korenblit 
2761f947b62cSMiri Korenblit 		if (!key)
2762f947b62cSMiri Korenblit 			continue;
2763f947b62cSMiri Korenblit 
2764f947b62cSMiri Korenblit 		mvmvif->ap_early_keys[i] = NULL;
2765f947b62cSMiri Korenblit 
2766f947b62cSMiri Korenblit 		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2767f947b62cSMiri Korenblit 		if (*ret)
2768f947b62cSMiri Korenblit 			return true;
2769e705c121SKalle Valo 	}
2770e705c121SKalle Valo 
2771f947b62cSMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2772f947b62cSMiri Korenblit 		iwl_mvm_vif_set_low_latency(mvmvif, true,
2773f947b62cSMiri Korenblit 					    LOW_LATENCY_VIF_TYPE);
2774f947b62cSMiri Korenblit 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2775e705c121SKalle Valo 	}
2776e705c121SKalle Valo 
2777f947b62cSMiri Korenblit 	/* power updated needs to be done before quotas */
2778f947b62cSMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
2779e705c121SKalle Valo 
2780f947b62cSMiri Korenblit 	return false;
2781e705c121SKalle Valo }
2782e705c121SKalle Valo 
2783e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2784ae7ba17bSShaul Triebitz 				 struct ieee80211_vif *vif,
2785b327c84cSGregory Greenman 				 struct ieee80211_bss_conf *link_conf)
2786e705c121SKalle Valo {
2787e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2788e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2789f947b62cSMiri Korenblit 	int ret;
2790e705c121SKalle Valo 
2791e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2792e705c121SKalle Valo 
2793e705c121SKalle Valo 	/*
2794cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2795cdaba917SEmmanuel Grumbach 	 * the beacon interval, which was not known when the AP interface
2796cdaba917SEmmanuel Grumbach 	 * was added.
2797e705c121SKalle Valo 	 */
2798e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2799e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2800e705c121SKalle Valo 
280136cf5377SGregory Greenman 	/* For older devices need to send beacon template before adding mac
280236cf5377SGregory Greenman 	 * context. For the newer, the beacon is a resource that belongs to a
280336cf5377SGregory Greenman 	 * MAC, so need to send beacon template after adding the mac.
280436cf5377SGregory Greenman 	 */
280536cf5377SGregory Greenman 	if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
2806e705c121SKalle Valo 		/* Add the mac context */
2807e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2808e705c121SKalle Valo 		if (ret)
2809e705c121SKalle Valo 			goto out_unlock;
2810e705c121SKalle Valo 
281136cf5377SGregory Greenman 		/* Send the beacon template */
281236cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
281336cf5377SGregory Greenman 		if (ret)
281436cf5377SGregory Greenman 			goto out_unlock;
281536cf5377SGregory Greenman 	} else {
281636cf5377SGregory Greenman 		/* Send the beacon template */
281736cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
281836cf5377SGregory Greenman 		if (ret)
281936cf5377SGregory Greenman 			goto out_unlock;
2820e705c121SKalle Valo 
2821e705c121SKalle Valo 		/* Add the mac context */
2822e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2823e705c121SKalle Valo 		if (ret)
2824e705c121SKalle Valo 			goto out_unlock;
282536cf5377SGregory Greenman 	}
2826e705c121SKalle Valo 
2827e705c121SKalle Valo 	/* Perform the binding */
2828e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2829e705c121SKalle Valo 	if (ret)
2830e705c121SKalle Valo 		goto out_remove;
2831e705c121SKalle Valo 
283263dd5d02SSara Sharon 	/*
283363dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
283463dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
283563dd5d02SSara Sharon 	 * cause assert 0x2b00.
283663dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
283763dd5d02SSara Sharon 	 * the TLV
283863dd5d02SSara Sharon 	 */
283963dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2840ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2841ced19f26SSara Sharon 		if (ret)
2842ced19f26SSara Sharon 			goto out_unbind;
284363dd5d02SSara Sharon 		/*
284463dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
284563dd5d02SSara Sharon 		 * events are added and applied to the scheduler
284663dd5d02SSara Sharon 		 */
2847e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
284863dd5d02SSara Sharon 		if (ret) {
284963dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
285063dd5d02SSara Sharon 			goto out_unbind;
285163dd5d02SSara Sharon 		}
285263dd5d02SSara Sharon 	} else {
285363dd5d02SSara Sharon 		/*
285463dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
285563dd5d02SSara Sharon 		 * events are added and applied to the scheduler
285663dd5d02SSara Sharon 		 */
285775fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2858e705c121SKalle Valo 		if (ret)
285963dd5d02SSara Sharon 			goto out_unbind;
286075fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
286163dd5d02SSara Sharon 		if (ret) {
286263dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
286363dd5d02SSara Sharon 			goto out_unbind;
286463dd5d02SSara Sharon 		}
286563dd5d02SSara Sharon 	}
286626d6c16bSSara Sharon 
2867f947b62cSMiri Korenblit 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
2868f947b62cSMiri Korenblit 		goto out_failed;
2869e705c121SKalle Valo 
2870e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2871e705c121SKalle Valo 	if (ret)
2872f947b62cSMiri Korenblit 		goto out_failed;
2873e705c121SKalle Valo 
2874e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2875e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2876e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2877e705c121SKalle Valo 
2878e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2879e705c121SKalle Valo 
2880e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2881e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2882e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2883e705c121SKalle Valo 
2884b73f9a4aSJohannes Berg 	iwl_mvm_ftm_restart_responder(mvm, vif);
2885b73f9a4aSJohannes Berg 
2886e705c121SKalle Valo 	goto out_unlock;
2887e705c121SKalle Valo 
2888f947b62cSMiri Korenblit out_failed:
2889e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2890e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2891e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2892ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2893e705c121SKalle Valo out_unbind:
2894e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2895e705c121SKalle Valo out_remove:
2896e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2897e705c121SKalle Valo out_unlock:
2898e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2899e705c121SKalle Valo 	return ret;
2900e705c121SKalle Valo }
2901e705c121SKalle Valo 
2902ae7ba17bSShaul Triebitz static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
2903ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
2904b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
2905ae7ba17bSShaul Triebitz {
2906b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
2907ae7ba17bSShaul Triebitz }
2908ae7ba17bSShaul Triebitz 
2909ae7ba17bSShaul Triebitz static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
2910e705c121SKalle Valo 			      struct ieee80211_vif *vif)
2911e705c121SKalle Valo {
2912b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
2913ae7ba17bSShaul Triebitz }
2914ae7ba17bSShaul Triebitz 
2915fd1a54c1SMiri Korenblit /* Common part for MLD and non-MLD ops */
2916fd1a54c1SMiri Korenblit void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
2917fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif)
2918ae7ba17bSShaul Triebitz {
2919e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2920e705c121SKalle Valo 
2921fd1a54c1SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
2922e705c121SKalle Valo 
2923fd1a54c1SMiri Korenblit 	iwl_mvm_prepare_mac_removal(mvm, vif);
2924e705c121SKalle Valo 
2925e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2926e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2927e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2928e705c121SKalle Valo 					  &mvmvif->time_event_data);
2929e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2930e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2931e705c121SKalle Valo 	}
2932e705c121SKalle Valo 
2933e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2934e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2935e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2936e705c121SKalle Valo 	}
2937e705c121SKalle Valo 
2938e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2939e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2940e705c121SKalle Valo 
294147242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
294247242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
294347242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
294447242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
294547242744STova Mussai 	}
294647242744STova Mussai 
2947e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2948fd1a54c1SMiri Korenblit }
2949fd1a54c1SMiri Korenblit 
2950fd1a54c1SMiri Korenblit static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2951fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif,
2952fd1a54c1SMiri Korenblit 				 struct ieee80211_bss_conf *link_conf)
2953fd1a54c1SMiri Korenblit {
2954fd1a54c1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2955fd1a54c1SMiri Korenblit 
2956fd1a54c1SMiri Korenblit 	mutex_lock(&mvm->mutex);
2957fd1a54c1SMiri Korenblit 
2958fd1a54c1SMiri Korenblit 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
2959e705c121SKalle Valo 
2960e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2961e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2962e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2963e705c121SKalle Valo 
2964e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2965ced19f26SSara Sharon 
2966be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
2967be82ecd3SAvraham Stern 
2968ced19f26SSara Sharon 	/*
2969ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2970ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2971ced19f26SSara Sharon 	 * cause assert 0x2b00.
2972ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2973ced19f26SSara Sharon 	 * bcast station).
2974ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2975ced19f26SSara Sharon 	 */
2976ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
297726d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2978e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2979ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2980ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2981e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2982e705c121SKalle Valo 
2983e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2984e705c121SKalle Valo 
2985e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2986e705c121SKalle Valo 
2987e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2988e705c121SKalle Valo }
2989e705c121SKalle Valo 
2990ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
2991ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
2992b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
2993ae7ba17bSShaul Triebitz {
2994b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
2995ae7ba17bSShaul Triebitz }
2996ae7ba17bSShaul Triebitz 
2997ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
2998ae7ba17bSShaul Triebitz 			      struct ieee80211_vif *vif)
2999ae7ba17bSShaul Triebitz {
3000b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3001ae7ba17bSShaul Triebitz }
3002ae7ba17bSShaul Triebitz 
3003e705c121SKalle Valo static void
3004e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3005e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3006e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
30077b7090b4SJohannes Berg 				 u64 changes)
3008e705c121SKalle Valo {
3009e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3010e705c121SKalle Valo 
3011e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
3012e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
3013e705c121SKalle Valo 		return;
3014e705c121SKalle Valo 
3015e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3016e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3017e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3018e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3019e705c121SKalle Valo 
3020e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
3021e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
302236cf5377SGregory Greenman 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3023e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
3024e705c121SKalle Valo 
3025b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3026b73f9a4aSJohannes Berg 		int ret = iwl_mvm_ftm_start_responder(mvm, vif);
3027b73f9a4aSJohannes Berg 
3028b73f9a4aSJohannes Berg 		if (ret)
3029b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3030b73f9a4aSJohannes Berg 				 ret);
3031b73f9a4aSJohannes Berg 	}
3032b73f9a4aSJohannes Berg 
3033e705c121SKalle Valo }
3034e705c121SKalle Valo 
3035e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3036e705c121SKalle Valo 				     struct ieee80211_vif *vif,
3037e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
30387b7090b4SJohannes Berg 				     u64 changes)
3039e705c121SKalle Valo {
3040a2906ea6SJohannes Berg 	static const struct iwl_mvm_bss_info_changed_ops callbacks = {
3041660eba5aSMiri Korenblit 		.bss_info_changed_sta = iwl_mvm_bss_info_changed_station,
3042660eba5aSMiri Korenblit 		.bss_info_changed_ap_ibss = iwl_mvm_bss_info_changed_ap_ibss,
3043660eba5aSMiri Korenblit 	};
3044660eba5aSMiri Korenblit 
3045660eba5aSMiri Korenblit 	iwl_mvm_bss_info_changed_common(hw, vif, bss_conf, &callbacks,
3046660eba5aSMiri Korenblit 					changes);
3047660eba5aSMiri Korenblit }
3048660eba5aSMiri Korenblit 
3049660eba5aSMiri Korenblit void
3050660eba5aSMiri Korenblit iwl_mvm_bss_info_changed_common(struct ieee80211_hw *hw,
3051660eba5aSMiri Korenblit 				struct ieee80211_vif *vif,
3052660eba5aSMiri Korenblit 				struct ieee80211_bss_conf *bss_conf,
3053a2906ea6SJohannes Berg 				const struct iwl_mvm_bss_info_changed_ops *callbacks,
3054660eba5aSMiri Korenblit 				u64 changes)
3055660eba5aSMiri Korenblit {
3056e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3057e705c121SKalle Valo 
3058e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3059e705c121SKalle Valo 
3060f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3061e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3062e705c121SKalle Valo 
3063e705c121SKalle Valo 	switch (vif->type) {
3064e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3065660eba5aSMiri Korenblit 		callbacks->bss_info_changed_sta(mvm, vif, bss_conf, changes);
3066e705c121SKalle Valo 		break;
3067e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3068e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
3069660eba5aSMiri Korenblit 		callbacks->bss_info_changed_ap_ibss(mvm, vif, bss_conf,
3070660eba5aSMiri Korenblit 						    changes);
3071e705c121SKalle Valo 		break;
307291b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
307391b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
307491b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
307591b08c2dSAviya Erenfeld 		break;
3076e705c121SKalle Valo 	default:
3077e705c121SKalle Valo 		/* shouldn't happen */
3078e705c121SKalle Valo 		WARN_ON_ONCE(1);
3079e705c121SKalle Valo 	}
3080e705c121SKalle Valo 
30819aae43a4SJohannes Berg 	if (changes & BSS_CHANGED_TXPOWER) {
30829aae43a4SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
30839aae43a4SJohannes Berg 				bss_conf->txpower);
30849aae43a4SJohannes Berg 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
30859aae43a4SJohannes Berg 	}
30869aae43a4SJohannes Berg 
3087e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3088e705c121SKalle Valo }
3089e705c121SKalle Valo 
3090cbce62a3SMiri Korenblit int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3091e705c121SKalle Valo 			struct ieee80211_scan_request *hw_req)
3092e705c121SKalle Valo {
3093e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3094e705c121SKalle Valo 	int ret;
3095e705c121SKalle Valo 
3096e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
3097e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3098e705c121SKalle Valo 		return -EINVAL;
3099e705c121SKalle Valo 
3100e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3101e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3102e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3103e705c121SKalle Valo 
3104e705c121SKalle Valo 	return ret;
3105e705c121SKalle Valo }
3106e705c121SKalle Valo 
3107cbce62a3SMiri Korenblit void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3108e705c121SKalle Valo 				struct ieee80211_vif *vif)
3109e705c121SKalle Valo {
3110e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3111e705c121SKalle Valo 
3112e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3113e705c121SKalle Valo 
3114e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3115e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
3116e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
3117e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
3118e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
3119e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
3120e705c121SKalle Valo 	*/
3121e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3122e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3123e705c121SKalle Valo 
3124e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3125e705c121SKalle Valo }
3126e705c121SKalle Valo 
3127cbce62a3SMiri Korenblit void
3128e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3129e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
3130e705c121SKalle Valo 				  int num_frames,
3131e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
3132e705c121SKalle Valo 				  bool more_data)
3133e705c121SKalle Valo {
3134e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3135e705c121SKalle Valo 
3136e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3137e705c121SKalle Valo 
3138e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3139e705c121SKalle Valo 					  tids, more_data, false);
3140e705c121SKalle Valo }
3141e705c121SKalle Valo 
3142cbce62a3SMiri Korenblit void
3143e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3144e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
3145e705c121SKalle Valo 				    int num_frames,
3146e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
3147e705c121SKalle Valo 				    bool more_data)
3148e705c121SKalle Valo {
3149e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3150e705c121SKalle Valo 
31519a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3152e705c121SKalle Valo 
3153e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3154e705c121SKalle Valo 					  tids, more_data, true);
3155e705c121SKalle Valo }
3156e705c121SKalle Valo 
315765e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3158e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
3159e705c121SKalle Valo 				     struct ieee80211_sta *sta)
3160e705c121SKalle Valo {
3161e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3162e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3163e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
3164e705c121SKalle Valo 	int tid;
3165e705c121SKalle Valo 
3166960f864bSJohannes Berg 	/*
3167960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
3168960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
3169960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
3170960f864bSJohannes Berg 	 */
3171960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3172960f864bSJohannes Berg 		return;
3173960f864bSJohannes Berg 
3174e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3175311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3176e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3177e705c121SKalle Valo 
31786862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
31791c17627bSSara Sharon 			continue;
31801c17627bSSara Sharon 
3181e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
3182e705c121SKalle Valo 
3183dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3184e705c121SKalle Valo 			continue;
3185e705c121SKalle Valo 
3186e705c121SKalle Valo 		__set_bit(tid, &tids);
3187e705c121SKalle Valo 	}
3188e705c121SKalle Valo 
3189e705c121SKalle Valo 	switch (cmd) {
3190e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
3191e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3192e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
3193e705c121SKalle Valo 
3194e705c121SKalle Valo 		if (txqs)
3195e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3196e705c121SKalle Valo 		/*
3197e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3198e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
3199e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3200e705c121SKalle Valo 		 */
3201e705c121SKalle Valo 		break;
3202e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
3203c8ee33e1SGregory Greenman 		if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
3204e705c121SKalle Valo 			break;
3205e705c121SKalle Valo 
3206e705c121SKalle Valo 		if (txqs)
3207e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3208e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3209e705c121SKalle Valo 		break;
3210e705c121SKalle Valo 	default:
3211e705c121SKalle Valo 		break;
3212e705c121SKalle Valo 	}
3213e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3214e705c121SKalle Valo }
3215e705c121SKalle Valo 
3216cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3217cbce62a3SMiri Korenblit 			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
321865e25482SJohannes Berg {
321965e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
322065e25482SJohannes Berg }
322165e25482SJohannes Berg 
322265e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
322365e25482SJohannes Berg {
322465e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
322565e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
322665e25482SJohannes Berg 	struct ieee80211_sta *sta;
322765e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
322865e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
322965e25482SJohannes Berg 
3230be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
323165e25482SJohannes Berg 		return;
323265e25482SJohannes Berg 
323365e25482SJohannes Berg 	rcu_read_lock();
3234a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
323565e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
323665e25482SJohannes Berg 		rcu_read_unlock();
323765e25482SJohannes Berg 		return;
323865e25482SJohannes Berg 	}
323965e25482SJohannes Berg 
324065e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
324165e25482SJohannes Berg 
324265e25482SJohannes Berg 	if (!mvmsta->vif ||
324365e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
324465e25482SJohannes Berg 		rcu_read_unlock();
324565e25482SJohannes Berg 		return;
324665e25482SJohannes Berg 	}
324765e25482SJohannes Berg 
324865e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
324965e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
325065e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
325165e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
325265e25482SJohannes Berg 			sta);
325365e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
325465e25482SJohannes Berg 	}
325565e25482SJohannes Berg 
325665e25482SJohannes Berg 	if (sleeping) {
325765e25482SJohannes Berg 		switch (notif->type) {
325865e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
325965e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
326065e25482SJohannes Berg 			break;
326165e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
326265e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
326365e25482SJohannes Berg 			break;
326465e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
326565e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
326665e25482SJohannes Berg 			break;
326765e25482SJohannes Berg 		default:
326865e25482SJohannes Berg 			break;
326965e25482SJohannes Berg 		}
327065e25482SJohannes Berg 	}
327165e25482SJohannes Berg 
327265e25482SJohannes Berg 	rcu_read_unlock();
327365e25482SJohannes Berg }
327465e25482SJohannes Berg 
3275cbce62a3SMiri Korenblit void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3276e705c121SKalle Valo 				struct ieee80211_vif *vif,
3277e705c121SKalle Valo 				struct ieee80211_sta *sta)
3278e705c121SKalle Valo {
3279e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3280e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
328179faae3aSGregory Greenman 	unsigned int link_id;
3282e705c121SKalle Valo 
3283e705c121SKalle Valo 	/*
3284e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
3285e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
3286e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
3287e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
3288e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
3289e705c121SKalle Valo 	 * callback deleted the station.
329079faae3aSGregory Greenman 	 * Since there's mvm->mutex here, no need to have RCU lock for
329179faae3aSGregory Greenman 	 * mvm_sta->link access.
3292e705c121SKalle Valo 	 */
3293e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
329479faae3aSGregory Greenman 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
329579faae3aSGregory Greenman 		struct iwl_mvm_link_sta *link_sta;
329679faae3aSGregory Greenman 		u32 sta_id;
3297e705c121SKalle Valo 
329879faae3aSGregory Greenman 		if (!mvm_sta->link[link_id])
329979faae3aSGregory Greenman 			continue;
330079faae3aSGregory Greenman 
330179faae3aSGregory Greenman 		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
330279faae3aSGregory Greenman 						     lockdep_is_held(&mvm->mutex));
330379faae3aSGregory Greenman 		sta_id = link_sta->sta_id;
3304b8a85a1dSJohannes Berg 		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
33053e75668bSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
330679faae3aSGregory Greenman 					 ERR_PTR(-ENOENT));
3307b8a85a1dSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3308b8a85a1dSJohannes Berg 		}
330979faae3aSGregory Greenman 	}
3310e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3311e705c121SKalle Valo }
3312e705c121SKalle Valo 
3313e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3314e705c121SKalle Valo 				const u8 *bssid)
3315e705c121SKalle Valo {
3316b0ffe455SJohannes Berg 	int i;
3317b0ffe455SJohannes Berg 
3318b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3319b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
3320b0ffe455SJohannes Berg 
3321b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3322b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3323b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
3324b0ffe455SJohannes Berg 	}
3325b0ffe455SJohannes Berg 
3326e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3327e705c121SKalle Valo 		return;
3328e705c121SKalle Valo 
3329c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3330cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3331cee5a882SAvri Altman 		return;
3332cee5a882SAvri Altman 	}
3333cee5a882SAvri Altman 
333411dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
333511dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3336e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3337e705c121SKalle Valo 		return;
3338e705c121SKalle Valo 	}
3339e705c121SKalle Valo 
3340b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3341b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3342b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3343b0ffe455SJohannes Berg 			return;
3344b0ffe455SJohannes Berg 		}
3345b0ffe455SJohannes Berg 	}
3346b0ffe455SJohannes Berg 
3347e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3348e705c121SKalle Valo }
3349e705c121SKalle Valo 
33501e8f1329SGolan Ben-Ami static void
33511e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
33521e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
33531e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
33541e8f1329SGolan Ben-Ami {
33551e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
33561e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
33571e8f1329SGolan Ben-Ami 
33586c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
33596c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
33606c042d75SSara Sharon 	if (!trig)
33611e8f1329SGolan Ben-Ami 		return;
33621e8f1329SGolan Ben-Ami 
33631e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
33641e8f1329SGolan Ben-Ami 
33651e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
33661e8f1329SGolan Ben-Ami 		return;
33671e8f1329SGolan Ben-Ami 
33681e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
33691e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
33701e8f1329SGolan Ben-Ami 		return;
33711e8f1329SGolan Ben-Ami 
33727174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
33731e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
33741e8f1329SGolan Ben-Ami 				peer_addr, action);
33751e8f1329SGolan Ben-Ami }
33761e8f1329SGolan Ben-Ami 
33774f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
33784f58121dSIlan Peer 	bool tolerated;
33794f58121dSIlan Peer };
33804f58121dSIlan Peer 
33814f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
33824f58121dSIlan Peer 						    struct cfg80211_bss *bss,
33834f58121dSIlan Peer 						    void *_data)
33844f58121dSIlan Peer {
33854f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
33866c608cd6SJohannes Berg 	const struct cfg80211_bss_ies *ies;
33874f58121dSIlan Peer 	const struct element *elem;
33884f58121dSIlan Peer 
33896c608cd6SJohannes Berg 	rcu_read_lock();
33906c608cd6SJohannes Berg 	ies = rcu_dereference(bss->ies);
33916c608cd6SJohannes Berg 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
33926c608cd6SJohannes Berg 				  ies->len);
33934f58121dSIlan Peer 
33944f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
33954f58121dSIlan Peer 	    !(elem->data[10] &
33964f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
33974f58121dSIlan Peer 		data->tolerated = false;
33984f58121dSIlan Peer 	}
33996c608cd6SJohannes Berg 	rcu_read_unlock();
34004f58121dSIlan Peer }
34014f58121dSIlan Peer 
34026e1b5956SJohannes Berg static void
34036e1b5956SJohannes Berg iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
34046e1b5956SJohannes Berg 				   struct ieee80211_vif *vif,
34056e1b5956SJohannes Berg 				   unsigned int link_id,
34066e1b5956SJohannes Berg 				   struct ieee80211_bss_conf *link_conf)
34074f58121dSIlan Peer {
34084f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
34094f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
34104f58121dSIlan Peer 		.tolerated = true,
34114f58121dSIlan Peer 	};
34124f58121dSIlan Peer 
34136e1b5956SJohannes Berg 	if (WARN_ON_ONCE(!link_conf->chandef.chan ||
34146e1b5956SJohannes Berg 			 !mvmvif->link[link_id]))
34156e1b5956SJohannes Berg 		return;
34166e1b5956SJohannes Berg 
34176e1b5956SJohannes Berg 	if (!(link_conf->chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
34186e1b5956SJohannes Berg 		mvmvif->link[link_id]->he_ru_2mhz_block = false;
34194f58121dSIlan Peer 		return;
34204f58121dSIlan Peer 	}
34214f58121dSIlan Peer 
34226e1b5956SJohannes Berg 	cfg80211_bss_iter(hw->wiphy, &link_conf->chandef,
34234f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
34244f58121dSIlan Peer 			  &iter_data);
34254f58121dSIlan Peer 
34264f58121dSIlan Peer 	/*
34274f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
34284f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
34294f58121dSIlan Peer 	 */
34306e1b5956SJohannes Berg 	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
34314f58121dSIlan Peer }
34324f58121dSIlan Peer 
3433f7d6ef33SJohannes Berg static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3434f7d6ef33SJohannes Berg 					       struct ieee80211_vif *vif)
3435f7d6ef33SJohannes Berg {
3436f7d6ef33SJohannes Berg 	struct ieee80211_supported_band *sband;
3437f7d6ef33SJohannes Berg 	const struct ieee80211_sta_he_cap *he_cap;
3438f7d6ef33SJohannes Berg 
3439f7d6ef33SJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION)
3440f7d6ef33SJohannes Berg 		return;
3441f7d6ef33SJohannes Berg 
3442f7d6ef33SJohannes Berg 	if (!mvm->cca_40mhz_workaround)
3443f7d6ef33SJohannes Berg 		return;
3444f7d6ef33SJohannes Berg 
3445f7d6ef33SJohannes Berg 	/* decrement and check that we reached zero */
3446f7d6ef33SJohannes Berg 	mvm->cca_40mhz_workaround--;
3447f7d6ef33SJohannes Berg 	if (mvm->cca_40mhz_workaround)
3448f7d6ef33SJohannes Berg 		return;
3449f7d6ef33SJohannes Berg 
3450f7d6ef33SJohannes Berg 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3451f7d6ef33SJohannes Berg 
3452f7d6ef33SJohannes Berg 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3453f7d6ef33SJohannes Berg 
3454*1ec7291eSJohannes Berg 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3455f7d6ef33SJohannes Berg 
3456f7d6ef33SJohannes Berg 	if (he_cap) {
3457f7d6ef33SJohannes Berg 		/* we know that ours is writable */
34580301bcd5SBjoern A. Zeeb 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3459f7d6ef33SJohannes Berg 
3460f7d6ef33SJohannes Berg 		he->he_cap_elem.phy_cap_info[0] |=
3461f7d6ef33SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3462f7d6ef33SJohannes Berg 	}
3463f7d6ef33SJohannes Berg }
3464f7d6ef33SJohannes Berg 
34656d19a5ebSEmmanuel Grumbach static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
34666d19a5ebSEmmanuel Grumbach 					struct ieee80211_vif *vif,
34676d19a5ebSEmmanuel Grumbach 					struct iwl_mvm_sta *mvm_sta)
34686d19a5ebSEmmanuel Grumbach {
34696d19a5ebSEmmanuel Grumbach #if IS_ENABLED(CONFIG_IWLMEI)
34706d19a5ebSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
34716d19a5ebSEmmanuel Grumbach 	struct iwl_mei_conn_info conn_info = {
3472f276e20bSJohannes Berg 		.ssid_len = vif->cfg.ssid_len,
34736d19a5ebSEmmanuel Grumbach 	};
34746d19a5ebSEmmanuel Grumbach 
34756d19a5ebSEmmanuel Grumbach 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
34766d19a5ebSEmmanuel Grumbach 		return;
34776d19a5ebSEmmanuel Grumbach 
34786d19a5ebSEmmanuel Grumbach 	if (!mvm->mei_registered)
34796d19a5ebSEmmanuel Grumbach 		return;
34806d19a5ebSEmmanuel Grumbach 
34816c07b73eSJohannes Berg 	/* FIXME: MEI needs to be updated for MLO */
34826c07b73eSJohannes Berg 	if (!vif->bss_conf.chandef.chan)
34836c07b73eSJohannes Berg 		return;
34846c07b73eSJohannes Berg 
34856c07b73eSJohannes Berg 	conn_info.channel = vif->bss_conf.chandef.chan->hw_value;
34866c07b73eSJohannes Berg 
34876d19a5ebSEmmanuel Grumbach 	switch (mvm_sta->pairwise_cipher) {
3488e5d3a64eSAvraham Stern 	case WLAN_CIPHER_SUITE_TKIP:
3489e5d3a64eSAvraham Stern 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3490e5d3a64eSAvraham Stern 		break;
34916d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_CCMP:
34926d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
34936d19a5ebSEmmanuel Grumbach 		break;
34946d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP:
34956d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
34966d19a5ebSEmmanuel Grumbach 		break;
34976d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP_256:
34986d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
34996d19a5ebSEmmanuel Grumbach 		break;
35006d19a5ebSEmmanuel Grumbach 	case 0:
35016d19a5ebSEmmanuel Grumbach 		/* open profile */
35026d19a5ebSEmmanuel Grumbach 		break;
35036d19a5ebSEmmanuel Grumbach 	default:
35046d19a5ebSEmmanuel Grumbach 		/* cipher not supported, don't send anything to iwlmei */
35056d19a5ebSEmmanuel Grumbach 		return;
35066d19a5ebSEmmanuel Grumbach 	}
35076d19a5ebSEmmanuel Grumbach 
35086d19a5ebSEmmanuel Grumbach 	switch (mvmvif->rekey_data.akm) {
35096d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_SAE & 0xff:
35106d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
35116d19a5ebSEmmanuel Grumbach 		break;
35126d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_PSK & 0xff:
35136d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
35146d19a5ebSEmmanuel Grumbach 		break;
35156d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_8021X & 0xff:
35166d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
35176d19a5ebSEmmanuel Grumbach 		break;
35186d19a5ebSEmmanuel Grumbach 	case 0:
35196d19a5ebSEmmanuel Grumbach 		/* open profile */
35206d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
35216d19a5ebSEmmanuel Grumbach 		break;
35226d19a5ebSEmmanuel Grumbach 	default:
35236d19a5ebSEmmanuel Grumbach 		/* auth method / AKM not supported */
35246d19a5ebSEmmanuel Grumbach 		/* TODO: All the FT vesions of these? */
35256d19a5ebSEmmanuel Grumbach 		return;
35266d19a5ebSEmmanuel Grumbach 	}
35276d19a5ebSEmmanuel Grumbach 
3528f276e20bSJohannes Berg 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
35296d19a5ebSEmmanuel Grumbach 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
35306d19a5ebSEmmanuel Grumbach 
35316d19a5ebSEmmanuel Grumbach 	/* TODO: add support for collocated AP data */
35326d19a5ebSEmmanuel Grumbach 	iwl_mei_host_associated(&conn_info, NULL);
35336d19a5ebSEmmanuel Grumbach #endif
35346d19a5ebSEmmanuel Grumbach }
35356d19a5ebSEmmanuel Grumbach 
353687f7e243SMiri Korenblit static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
353787f7e243SMiri Korenblit 					    struct ieee80211_vif *vif,
353887f7e243SMiri Korenblit 					    bool force_assoc_off)
353987f7e243SMiri Korenblit {
354087f7e243SMiri Korenblit 	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
354187f7e243SMiri Korenblit }
354287f7e243SMiri Korenblit 
3543e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3544e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3545e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3546e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3547e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3548e705c121SKalle Valo {
3549a2906ea6SJohannes Berg 	static const struct iwl_mvm_sta_state_ops callbacks = {
355087f7e243SMiri Korenblit 		.add_sta = iwl_mvm_add_sta,
355187f7e243SMiri Korenblit 		.update_sta = iwl_mvm_update_sta,
355287f7e243SMiri Korenblit 		.rm_sta = iwl_mvm_rm_sta,
355387f7e243SMiri Korenblit 		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
355487f7e243SMiri Korenblit 	};
355587f7e243SMiri Korenblit 
355687f7e243SMiri Korenblit 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
355787f7e243SMiri Korenblit 					    &callbacks);
355887f7e243SMiri Korenblit }
355987f7e243SMiri Korenblit 
356057974a55SGregory Greenman /* FIXME: temporary making two assumptions in all sta handling functions:
356157974a55SGregory Greenman  *	(1) when setting sta state, the link exists and protected
356257974a55SGregory Greenman  *	(2) if a link is valid in sta then it's valid in vif (can
356357974a55SGregory Greenman  *	use same index in the link array)
356457974a55SGregory Greenman  */
3565f53be9c4SGregory Greenman static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3566f53be9c4SGregory Greenman 					   struct ieee80211_vif *vif,
356715d41834SJohannes Berg 					   struct ieee80211_sta *sta)
3568f53be9c4SGregory Greenman {
3569f53be9c4SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3570f53be9c4SGregory Greenman 	unsigned int link_id;
357157974a55SGregory Greenman 
3572f53be9c4SGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3573f53be9c4SGregory Greenman 		struct ieee80211_bss_conf *conf =
3574a705a782SJohannes Berg 			link_conf_dereference_check(vif, link_id);
3575f53be9c4SGregory Greenman 		struct ieee80211_link_sta *link_sta =
3576a705a782SJohannes Berg 			link_sta_dereference_check(sta, link_id);
3577f53be9c4SGregory Greenman 
3578f53be9c4SGregory Greenman 		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3579f53be9c4SGregory Greenman 			continue;
3580f53be9c4SGregory Greenman 
3581c45217bdSJohannes Berg 		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
358215d41834SJohannes Berg 				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3583f53be9c4SGregory Greenman 	}
3584f53be9c4SGregory Greenman }
358557974a55SGregory Greenman 
358657974a55SGregory Greenman #define IWL_MVM_MIN_BEACON_INTERVAL_TU 16
358757974a55SGregory Greenman 
358857974a55SGregory Greenman static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
358957974a55SGregory Greenman 				      struct ieee80211_vif *vif,
359057974a55SGregory Greenman 				      struct ieee80211_sta *sta)
359157974a55SGregory Greenman {
3592207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3593207be64fSMiri Korenblit 	unsigned int link_id;
359457974a55SGregory Greenman 
359557974a55SGregory Greenman 	/* Beacon interval check - firmware will crash if the beacon
359657974a55SGregory Greenman 	 * interval is less than 16. We can't avoid connecting at all,
359757974a55SGregory Greenman 	 * so refuse the station state change, this will cause mac80211
359857974a55SGregory Greenman 	 * to abandon attempts to connect to this AP, and eventually
359957974a55SGregory Greenman 	 * wpa_s will blocklist the AP...
360057974a55SGregory Greenman 	 */
360157974a55SGregory Greenman 
3602207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
360357974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3604207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
360557974a55SGregory Greenman 
3606207be64fSMiri Korenblit 		if (!link_conf)
360757974a55SGregory Greenman 			continue;
360857974a55SGregory Greenman 
360957974a55SGregory Greenman 		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
361057974a55SGregory Greenman 			IWL_ERR(mvm,
361157974a55SGregory Greenman 				"Beacon interval %d for AP %pM is too small\n",
361257974a55SGregory Greenman 				link_conf->beacon_int, link_sta->addr);
361357974a55SGregory Greenman 			return false;
361457974a55SGregory Greenman 		}
361557974a55SGregory Greenman 
361657974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
361757974a55SGregory Greenman 	}
361857974a55SGregory Greenman 
361957974a55SGregory Greenman 	return true;
362057974a55SGregory Greenman }
362157974a55SGregory Greenman 
362257974a55SGregory Greenman static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
362357974a55SGregory Greenman 				       struct ieee80211_vif *vif,
362457974a55SGregory Greenman 				       struct ieee80211_sta *sta,
362557974a55SGregory Greenman 				       bool is_sta)
362657974a55SGregory Greenman {
362757974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3628207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3629207be64fSMiri Korenblit 	unsigned int link_id;
363057974a55SGregory Greenman 
3631207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
363257974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3633207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
363457974a55SGregory Greenman 
3635207be64fSMiri Korenblit 		if (!link_conf || !mvmvif->link[link_id])
363657974a55SGregory Greenman 			continue;
363757974a55SGregory Greenman 
363857974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
363957974a55SGregory Greenman 
364057974a55SGregory Greenman 		if (is_sta) {
3641207be64fSMiri Korenblit 			mvmvif->link[link_id]->he_ru_2mhz_block = false;
364257974a55SGregory Greenman 			if (link_sta->he_cap.has_he)
3643207be64fSMiri Korenblit 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3644207be64fSMiri Korenblit 								   link_id,
36456e1b5956SJohannes Berg 								   link_conf);
364657974a55SGregory Greenman 		}
364757974a55SGregory Greenman 	}
364857974a55SGregory Greenman }
364957974a55SGregory Greenman 
365057974a55SGregory Greenman static int
365157974a55SGregory Greenman iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
365257974a55SGregory Greenman 				   struct ieee80211_vif *vif,
365357974a55SGregory Greenman 				   struct ieee80211_sta *sta,
3654a2906ea6SJohannes Berg 				   const struct iwl_mvm_sta_state_ops *callbacks)
365557974a55SGregory Greenman {
36560c9a8f90SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3657207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
365857974a55SGregory Greenman 	unsigned int i;
365957974a55SGregory Greenman 	int ret;
366057974a55SGregory Greenman 
366157974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
366257974a55SGregory Greenman 
366357974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_STATION &&
366457974a55SGregory Greenman 	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
366557974a55SGregory Greenman 		return -EINVAL;
366657974a55SGregory Greenman 
366757974a55SGregory Greenman 	if (sta->tdls &&
366857974a55SGregory Greenman 	    (vif->p2p ||
366957974a55SGregory Greenman 	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
367057974a55SGregory Greenman 	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
367157974a55SGregory Greenman 		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
367257974a55SGregory Greenman 		return -EBUSY;
367357974a55SGregory Greenman 	}
367457974a55SGregory Greenman 
367557974a55SGregory Greenman 	ret = callbacks->add_sta(mvm, vif, sta);
367657974a55SGregory Greenman 	if (sta->tdls && ret == 0) {
367757974a55SGregory Greenman 		iwl_mvm_recalc_tdls_state(mvm, vif, true);
367857974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
367957974a55SGregory Greenman 					   NL80211_TDLS_SETUP);
368057974a55SGregory Greenman 	}
368157974a55SGregory Greenman 
3682207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, i)
368357974a55SGregory Greenman 		link_sta->agg.max_rc_amsdu_len = 1;
3684207be64fSMiri Korenblit 
368557974a55SGregory Greenman 	ieee80211_sta_recalc_aggregates(sta);
368657974a55SGregory Greenman 
36870c9a8f90SJohannes Berg 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
36880c9a8f90SJohannes Berg 		mvmvif->ap_sta = sta;
36890c9a8f90SJohannes Berg 
369057974a55SGregory Greenman 	return 0;
369157974a55SGregory Greenman }
369257974a55SGregory Greenman 
369357974a55SGregory Greenman static int
369457974a55SGregory Greenman iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
369557974a55SGregory Greenman 				struct iwl_mvm *mvm,
369657974a55SGregory Greenman 				struct ieee80211_vif *vif,
369757974a55SGregory Greenman 				struct ieee80211_sta *sta,
3698a2906ea6SJohannes Berg 				const struct iwl_mvm_sta_state_ops *callbacks)
369957974a55SGregory Greenman {
370057974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
370157974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3702207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3703207be64fSMiri Korenblit 	unsigned int link_id;
370457974a55SGregory Greenman 
370557974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
370657974a55SGregory Greenman 
370757974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_AP) {
370857974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
370957974a55SGregory Greenman 		mvmvif->ap_assoc_sta_count++;
371057974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
371157974a55SGregory Greenman 
371257974a55SGregory Greenman 		/* since the below is not for MLD API, it's ok to use
371357974a55SGregory Greenman 		 * the default bss_conf
371457974a55SGregory Greenman 		 */
371557974a55SGregory Greenman 		if (!mvm->mld_api_is_used &&
371657974a55SGregory Greenman 		    ((vif->bss_conf.he_support &&
371757974a55SGregory Greenman 		      !iwlwifi_mod_params.disable_11ax) ||
371857974a55SGregory Greenman 		    (vif->bss_conf.eht_support &&
371957974a55SGregory Greenman 		     !iwlwifi_mod_params.disable_11be)))
372057974a55SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
372157974a55SGregory Greenman 	} else if (vif->type == NL80211_IFTYPE_STATION) {
372257974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
372357974a55SGregory Greenman 
372457974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
372557974a55SGregory Greenman 
372657974a55SGregory Greenman 		if (!mvm->mld_api_is_used)
372757974a55SGregory Greenman 			goto out;
372857974a55SGregory Greenman 
3729207be64fSMiri Korenblit 		for_each_sta_active_link(vif, sta, link_sta, link_id) {
373057974a55SGregory Greenman 			struct ieee80211_bss_conf *link_conf =
3731207be64fSMiri Korenblit 				link_conf_dereference_protected(vif, link_id);
373257974a55SGregory Greenman 
373357974a55SGregory Greenman 			if (WARN_ON(!link_conf))
373457974a55SGregory Greenman 				return -EINVAL;
3735207be64fSMiri Korenblit 			if (!mvmvif->link[link_id])
3736f14ad95aSJohannes Berg 				continue;
373757974a55SGregory Greenman 
373857974a55SGregory Greenman 			iwl_mvm_link_changed(mvm, vif, link_conf,
373957974a55SGregory Greenman 					     LINK_CONTEXT_MODIFY_ALL &
374057974a55SGregory Greenman 					     ~LINK_CONTEXT_MODIFY_ACTIVE,
374157974a55SGregory Greenman 					     true);
374257974a55SGregory Greenman 		}
374357974a55SGregory Greenman 	}
374457974a55SGregory Greenman 
374557974a55SGregory Greenman out:
374615d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
374757974a55SGregory Greenman 
374857974a55SGregory Greenman 	return callbacks->update_sta(mvm, vif, sta);
374957974a55SGregory Greenman }
375057974a55SGregory Greenman 
375157974a55SGregory Greenman static int
375257974a55SGregory Greenman iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
375357974a55SGregory Greenman 				      struct ieee80211_vif *vif,
375457974a55SGregory Greenman 				      struct ieee80211_sta *sta,
3755a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
375657974a55SGregory Greenman {
375757974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
375857974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
375957974a55SGregory Greenman 
376057974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
376157974a55SGregory Greenman 
376257974a55SGregory Greenman 	/* we don't support TDLS during DCM */
376357974a55SGregory Greenman 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
376457974a55SGregory Greenman 		iwl_mvm_teardown_tdls_peers(mvm);
376557974a55SGregory Greenman 
376657974a55SGregory Greenman 	if (sta->tdls) {
376757974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
376857974a55SGregory Greenman 					   NL80211_TDLS_ENABLE_LINK);
376957974a55SGregory Greenman 	} else {
377057974a55SGregory Greenman 		/* enable beacon filtering */
377157974a55SGregory Greenman 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
377257974a55SGregory Greenman 
377357974a55SGregory Greenman 		mvmvif->authorized = 1;
377457974a55SGregory Greenman 
377557974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
377657974a55SGregory Greenman 		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
377757974a55SGregory Greenman 	}
377857974a55SGregory Greenman 
377915d41834SJohannes Berg 	mvm_sta->authorized = true;
378015d41834SJohannes Berg 
378115d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
378257974a55SGregory Greenman 
378357974a55SGregory Greenman 	return 0;
378457974a55SGregory Greenman }
378557974a55SGregory Greenman 
378657974a55SGregory Greenman static int
378757974a55SGregory Greenman iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
378857974a55SGregory Greenman 				      struct ieee80211_vif *vif,
378957974a55SGregory Greenman 				      struct ieee80211_sta *sta,
3790a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
379157974a55SGregory Greenman {
379257974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
379315d41834SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
379457974a55SGregory Greenman 
379557974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
379657974a55SGregory Greenman 
379715d41834SJohannes Berg 	mvmsta->authorized = false;
379815d41834SJohannes Berg 
379957974a55SGregory Greenman 	/* once we move into assoc state, need to update rate scale to
380057974a55SGregory Greenman 	 * disable using wide bandwidth
380157974a55SGregory Greenman 	 */
380215d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
380357974a55SGregory Greenman 
380457974a55SGregory Greenman 	if (!sta->tdls) {
380557974a55SGregory Greenman 		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
380657974a55SGregory Greenman 		 * yet to avoid sending high prio again for a little
380757974a55SGregory Greenman 		 * time.
380857974a55SGregory Greenman 		 */
380957974a55SGregory Greenman 		mvmvif->authorized = 0;
381057974a55SGregory Greenman 
381157974a55SGregory Greenman 		/* disable beacon filtering */
3812fccf5ff1SJohannes Berg 		iwl_mvm_disable_beacon_filter(mvm, vif, 0);
381357974a55SGregory Greenman 	}
381457974a55SGregory Greenman 
381557974a55SGregory Greenman 	return 0;
381657974a55SGregory Greenman }
381757974a55SGregory Greenman 
381887f7e243SMiri Korenblit /* Common part for MLD and non-MLD modes */
381987f7e243SMiri Korenblit int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
382087f7e243SMiri Korenblit 				 struct ieee80211_vif *vif,
382187f7e243SMiri Korenblit 				 struct ieee80211_sta *sta,
382287f7e243SMiri Korenblit 				 enum ieee80211_sta_state old_state,
382387f7e243SMiri Korenblit 				 enum ieee80211_sta_state new_state,
3824a2906ea6SJohannes Berg 				 const struct iwl_mvm_sta_state_ops *callbacks)
382587f7e243SMiri Korenblit {
3826e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3827e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
38286ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
382957974a55SGregory Greenman 	unsigned int link_id;
3830e705c121SKalle Valo 	int ret;
3831e705c121SKalle Valo 
3832e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3833e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3834e705c121SKalle Valo 
383524afba76SLiad Kaufman 	/*
383624afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
383724afba76SLiad Kaufman 	 *
383824afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
383924afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
384024afba76SLiad Kaufman 	 * the queues.
384124afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
384224afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
384324afba76SLiad Kaufman 	 * of.
384424afba76SLiad Kaufman 	 *
384524afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
384624afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
384724afba76SLiad Kaufman 	 */
384824afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3849c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
385024afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
385124afba76SLiad Kaufman 
385224afba76SLiad Kaufman 		/*
385324afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
385424afba76SLiad Kaufman 		 * worker will already remove it if it was on
385524afba76SLiad Kaufman 		 */
3856f7d6ef33SJohannes Berg 
3857f7d6ef33SJohannes Berg 		/*
3858f7d6ef33SJohannes Berg 		 * Additionally, reset the 40 MHz capability if we disconnected
3859f7d6ef33SJohannes Berg 		 * from the AP now.
3860f7d6ef33SJohannes Berg 		 */
3861f7d6ef33SJohannes Berg 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
3862783336b0SJohannes Berg 
3863783336b0SJohannes Berg 		/* Also free dup data just in case any assertions below fail */
3864783336b0SJohannes Berg 		kfree(mvm_sta->dup_data);
386524afba76SLiad Kaufman 	}
386624afba76SLiad Kaufman 
3867e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
386857974a55SGregory Greenman 
386957974a55SGregory Greenman 	/* this would be a mac80211 bug ... but don't crash */
387057974a55SGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
387157974a55SGregory Greenman 		if (WARN_ON_ONCE(!mvmvif->link[link_id]->phy_ctxt)) {
387257974a55SGregory Greenman 			mutex_unlock(&mvm->mutex);
387357974a55SGregory Greenman 			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
387457974a55SGregory Greenman 					&mvm->status) ? 0 : -EINVAL;
387557974a55SGregory Greenman 		}
387657974a55SGregory Greenman 	}
387757974a55SGregory Greenman 
38786ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3879d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
38806ea29ce5SJohannes Berg 
3881e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3882e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
388357974a55SGregory Greenman 		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
388457974a55SGregory Greenman 							 callbacks);
388557974a55SGregory Greenman 		if (ret < 0)
3886e705c121SKalle Valo 			goto out_unlock;
3887e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3888e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3889e705c121SKalle Valo 		/*
3890e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3891e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3892e705c121SKalle Valo 		 */
3893e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3894e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3895e705c121SKalle Valo 		ret = 0;
3896e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3897e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
389857974a55SGregory Greenman 		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
389957974a55SGregory Greenman 						      callbacks);
3900e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3901e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
390257974a55SGregory Greenman 		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
390357974a55SGregory Greenman 							    callbacks);
3904e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3905e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
390657974a55SGregory Greenman 		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
390757974a55SGregory Greenman 							    callbacks);
3908e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3909e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
39108be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
39118be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
391287f7e243SMiri Korenblit 			callbacks->mac_ctxt_changed(mvm, vif, false);
3913d5d8ee52SEmmanuel Grumbach 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3914cf7a7457SJohannes Berg 			iwl_mvm_stop_session_protection(mvm, vif);
3915e705c121SKalle Valo 		ret = 0;
3916e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3917e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3918e705c121SKalle Valo 		ret = 0;
3919e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3920e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
39210c9a8f90SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
3922d5d8ee52SEmmanuel Grumbach 			iwl_mvm_stop_session_protection(mvm, vif);
39230c9a8f90SJohannes Berg 			mvmvif->ap_sta = NULL;
39240c9a8f90SJohannes Berg 		}
392587f7e243SMiri Korenblit 		ret = callbacks->rm_sta(mvm, vif, sta);
39261e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3927e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
39281e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
39291e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
39301e8f1329SGolan Ben-Ami 		}
393134a880d8SLiad Kaufman 
393244135b7cSIlan Peer 		if (unlikely(ret &&
393344135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
393444135b7cSIlan Peer 				      &mvm->status)))
393544135b7cSIlan Peer 			ret = 0;
3936e705c121SKalle Valo 	} else {
3937e705c121SKalle Valo 		ret = -EIO;
3938e705c121SKalle Valo 	}
3939e705c121SKalle Valo  out_unlock:
3940e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3941e705c121SKalle Valo 
3942e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3943e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3944e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3945e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3946e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3947e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3948e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3949e705c121SKalle Valo 	}
3950e705c121SKalle Valo 
3951e705c121SKalle Valo 	return ret;
3952e705c121SKalle Valo }
3953e705c121SKalle Valo 
3954cbce62a3SMiri Korenblit int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3955e705c121SKalle Valo {
3956e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3957e705c121SKalle Valo 
3958e705c121SKalle Valo 	mvm->rts_threshold = value;
3959e705c121SKalle Valo 
3960e705c121SKalle Valo 	return 0;
3961e705c121SKalle Valo }
3962e705c121SKalle Valo 
3963cbce62a3SMiri Korenblit void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3964e705c121SKalle Valo 			   struct ieee80211_sta *sta, u32 changed)
3965e705c121SKalle Valo {
3966e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3967dcfe3b10SJohannes Berg 
3968dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
3969dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3970dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
397115d41834SJohannes Berg 		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3972e705c121SKalle Valo 
3973e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
3974e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
3975e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
3976e705c121SKalle Valo }
3977e705c121SKalle Valo 
3978e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3979b3e2130bSJohannes Berg 			       struct ieee80211_vif *vif,
3980b3e2130bSJohannes Berg 			       unsigned int link_id, u16 ac,
3981e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
3982e705c121SKalle Valo {
3983e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3984e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3985e705c121SKalle Valo 
3986650cadb7SGregory Greenman 	mvmvif->deflink.queue_params[ac] = *params;
3987e705c121SKalle Valo 
3988e705c121SKalle Valo 	/*
3989e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
3990e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
3991e705c121SKalle Valo 	 */
3992e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3993e705c121SKalle Valo 		int ret;
3994e705c121SKalle Valo 
3995e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
3996e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3997e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3998e705c121SKalle Valo 		return ret;
3999e705c121SKalle Valo 	}
4000e705c121SKalle Valo 	return 0;
4001e705c121SKalle Valo }
4002e705c121SKalle Valo 
4003cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4004d4e36e55SIlan Peer 				struct ieee80211_vif *vif,
400515fae341SJohannes Berg 				struct ieee80211_prep_tx_info *info)
4006e705c121SKalle Valo {
4007e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4008d4e36e55SIlan Peer 
4009e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4010af84ac57SJohannes Berg 	iwl_mvm_protect_assoc(mvm, vif, info->duration);
4011e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4012e705c121SKalle Valo }
4013e705c121SKalle Valo 
4014cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
40156b1259d1SJohannes Berg 				 struct ieee80211_vif *vif,
40166b1259d1SJohannes Berg 				 struct ieee80211_prep_tx_info *info)
40176b1259d1SJohannes Berg {
40186b1259d1SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
40196b1259d1SJohannes Berg 
40206b1259d1SJohannes Berg 	/* for successful cases (auth/assoc), don't cancel session protection */
40216b1259d1SJohannes Berg 	if (info->success)
40226b1259d1SJohannes Berg 		return;
40236b1259d1SJohannes Berg 
40246b1259d1SJohannes Berg 	mutex_lock(&mvm->mutex);
40256b1259d1SJohannes Berg 	iwl_mvm_stop_session_protection(mvm, vif);
40266b1259d1SJohannes Berg 	mutex_unlock(&mvm->mutex);
40276b1259d1SJohannes Berg }
40286b1259d1SJohannes Berg 
4029cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4030e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4031e705c121SKalle Valo 				 struct cfg80211_sched_scan_request *req,
4032e705c121SKalle Valo 				 struct ieee80211_scan_ies *ies)
4033e705c121SKalle Valo {
4034e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4035e705c121SKalle Valo 
4036e705c121SKalle Valo 	int ret;
4037e705c121SKalle Valo 
4038e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4039e705c121SKalle Valo 
4040f276e20bSJohannes Berg 	if (!vif->cfg.idle) {
4041e705c121SKalle Valo 		ret = -EBUSY;
4042e705c121SKalle Valo 		goto out;
4043e705c121SKalle Valo 	}
4044e705c121SKalle Valo 
4045e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4046e705c121SKalle Valo 
4047e705c121SKalle Valo out:
4048e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4049e705c121SKalle Valo 	return ret;
4050e705c121SKalle Valo }
4051e705c121SKalle Valo 
4052cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4053e705c121SKalle Valo 				struct ieee80211_vif *vif)
4054e705c121SKalle Valo {
4055e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4056e705c121SKalle Valo 	int ret;
4057e705c121SKalle Valo 
4058e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4059e705c121SKalle Valo 
4060e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
4061e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
4062e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
4063e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
4064e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4065e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
4066e705c121SKalle Valo 	 * not running.
4067e705c121SKalle Valo 	*/
4068e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4069e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
4070e705c121SKalle Valo 		return 0;
4071e705c121SKalle Valo 	}
4072e705c121SKalle Valo 
4073e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4074e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4075e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
4076e705c121SKalle Valo 
4077e705c121SKalle Valo 	return ret;
4078e705c121SKalle Valo }
4079e705c121SKalle Valo 
40806569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4081e705c121SKalle Valo 				 enum set_key_cmd cmd,
4082e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4083e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4084e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
4085e705c121SKalle Valo {
4086c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4087e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
408846c7b05aSEmmanuel Grumbach 	struct iwl_mvm_sta *mvmsta = NULL;
4089d066a530SJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn = NULL;
4090f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
40915c75a208SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
40925c75a208SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4093c56e00a3SJohannes Berg 	int ret, i;
40944615fd15SEmmanuel Grumbach 	u8 key_offset;
4095e705c121SKalle Valo 
409646c7b05aSEmmanuel Grumbach 	if (sta)
40976d19a5ebSEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
40986d19a5ebSEmmanuel Grumbach 
4099e705c121SKalle Valo 	switch (key->cipher) {
4100e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
4101286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
4102e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
41031ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
41047f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
41057f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
41067f768ad5SDavid Spinadel 		} else {
41077f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
41087f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
41097f768ad5SDavid Spinadel 		}
4110e705c121SKalle Valo 		break;
4111e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
41122a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
41132a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
411485aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
4115e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4116e705c121SKalle Valo 		break;
4117e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
41188e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
41198e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4120e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4121e705c121SKalle Valo 		break;
4122e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
4123e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
4124475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
4125e705c121SKalle Valo 			break;
4126475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
4127475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
4128475c6bdeSJohannes Berg 		/* support HW crypto on TX */
4129475c6bdeSJohannes Berg 		return 0;
4130e705c121SKalle Valo 	default:
4131e705c121SKalle Valo 		return -EOPNOTSUPP;
4132e705c121SKalle Valo 	}
4133e705c121SKalle Valo 
4134e705c121SKalle Valo 	switch (cmd) {
4135e705c121SKalle Valo 	case SET_KEY:
4136a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4137a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4138b1fdc250SJohannes Berg 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4139b1fdc250SJohannes Berg 					   key);
4140b1fdc250SJohannes Berg 
4141e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4142e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4143e705c121SKalle Valo 			/*
4144e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
4145e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
4146e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
41478e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
4148a5de7de7SJohannes Berg 			 *
4149a5de7de7SJohannes Berg 			 * Except, of course, beacon protection - it must be
4150a5de7de7SJohannes Berg 			 * offloaded since we just set a beacon template.
4151e705c121SKalle Valo 			 */
4152a5de7de7SJohannes Berg 			if (keyidx < 6 &&
4153a5de7de7SJohannes Berg 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
41548e160ab8SAyala Beker 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4155a5de7de7SJohannes Berg 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
415681279c49SJohannes Berg 				ret = -EOPNOTSUPP;
4157a1c2ff30SAndrei Otcheretianski 				break;
4158a1c2ff30SAndrei Otcheretianski 			}
415985aeb58cSDavid Spinadel 
416085aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
416185aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
416285aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
4163e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
4164a1c2ff30SAndrei Otcheretianski 				ret = 0;
4165e705c121SKalle Valo 				break;
4166e705c121SKalle Valo 			}
4167c56e00a3SJohannes Berg 
4168c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
4169c56e00a3SJohannes Berg 				for (i = 0;
4170c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4171c56e00a3SJohannes Berg 				     i++) {
4172c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
4173c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
4174c56e00a3SJohannes Berg 						break;
4175c56e00a3SJohannes Berg 					}
4176c56e00a3SJohannes Berg 				}
4177c56e00a3SJohannes Berg 
4178c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4179c56e00a3SJohannes Berg 					ret = -ENOSPC;
4180a1c2ff30SAndrei Otcheretianski 				else
4181a1c2ff30SAndrei Otcheretianski 					ret = 0;
4182c56e00a3SJohannes Berg 
4183c56e00a3SJohannes Berg 				break;
4184c56e00a3SJohannes Berg 			}
418585aeb58cSDavid Spinadel 		}
4186e705c121SKalle Valo 
4187e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
4188e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
4189e705c121SKalle Valo 		 */
4190e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4191e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4192e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
4193e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
4194e705c121SKalle Valo 			ret = 0;
4195e705c121SKalle Valo 			break;
4196e705c121SKalle Valo 		}
4197e705c121SKalle Valo 
4198f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
419946c7b05aSEmmanuel Grumbach 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4200f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4201f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
42022a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
42032a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4204f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
4205f5e28eacSJohannes Berg 			int tid, q;
4206f5e28eacSJohannes Berg 
4207f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4208acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4209acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
4210f5e28eacSJohannes Berg 					 GFP_KERNEL);
4211f5e28eacSJohannes Berg 			if (!ptk_pn) {
4212f5e28eacSJohannes Berg 				ret = -ENOMEM;
4213f5e28eacSJohannes Berg 				break;
4214f5e28eacSJohannes Berg 			}
4215f5e28eacSJohannes Berg 
4216f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4217f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
4218f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
4219f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
4220f5e28eacSJohannes Berg 					       seq.ccmp.pn,
4221f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
4222f5e28eacSJohannes Berg 			}
4223f5e28eacSJohannes Berg 
4224f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4225f5e28eacSJohannes Berg 		}
4226f5e28eacSJohannes Berg 
42274615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
42284615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
42294615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
42304615fd15SEmmanuel Grumbach 		else
42314615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
42324615fd15SEmmanuel Grumbach 
423346c7b05aSEmmanuel Grumbach 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
42346d19a5ebSEmmanuel Grumbach 			mvmsta->pairwise_cipher = key->cipher;
42356d19a5ebSEmmanuel Grumbach 
4236a5de7de7SJohannes Berg 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4237a5de7de7SJohannes Berg 				   sta ? sta->addr : NULL, key->keyidx);
42385c75a208SJohannes Berg 
42395c75a208SJohannes Berg 		if (sec_key_ver)
42405c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
42415c75a208SJohannes Berg 		else
42424615fd15SEmmanuel Grumbach 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
42435c75a208SJohannes Berg 
4244e705c121SKalle Valo 		if (ret) {
4245e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
4246475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
4247d066a530SJohannes Berg 			if (ptk_pn) {
4248d066a530SJohannes Berg 				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4249d066a530SJohannes Berg 				kfree(ptk_pn);
4250d066a530SJohannes Berg 			}
4251e705c121SKalle Valo 			/*
4252e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
4253475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
4254475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
4255475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
4256e705c121SKalle Valo 			 */
4257475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
4258475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
4259475c6bdeSJohannes Berg 			else
4260e705c121SKalle Valo 				ret = 0;
4261e705c121SKalle Valo 		}
4262e705c121SKalle Valo 
4263e705c121SKalle Valo 		break;
4264e705c121SKalle Valo 	case DISABLE_KEY:
4265a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4266a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4267b1fdc250SJohannes Berg 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4268b1fdc250SJohannes Berg 					 NULL);
4269b1fdc250SJohannes Berg 
4270c56e00a3SJohannes Berg 		ret = -ENOENT;
4271c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4272c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
4273c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
4274c56e00a3SJohannes Berg 				ret = 0;
4275c56e00a3SJohannes Berg 			}
4276c56e00a3SJohannes Berg 		}
4277c56e00a3SJohannes Berg 
4278c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
4279c56e00a3SJohannes Berg 		if (ret == 0)
4280c56e00a3SJohannes Berg 			break;
4281c56e00a3SJohannes Berg 
4282e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4283e705c121SKalle Valo 			ret = 0;
4284e705c121SKalle Valo 			break;
4285e705c121SKalle Valo 		}
4286e705c121SKalle Valo 
428746c7b05aSEmmanuel Grumbach 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4288f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4289f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
42902a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
42912a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4292f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
4293f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
4294f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
4295f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4296f5e28eacSJohannes Berg 			if (ptk_pn)
4297f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
4298f5e28eacSJohannes Berg 		}
4299f5e28eacSJohannes Berg 
4300e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
43015c75a208SJohannes Berg 		if (sec_key_ver)
43025c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
43035c75a208SJohannes Berg 		else
4304e705c121SKalle Valo 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4305e705c121SKalle Valo 		break;
4306e705c121SKalle Valo 	default:
4307e705c121SKalle Valo 		ret = -EINVAL;
4308e705c121SKalle Valo 	}
4309e705c121SKalle Valo 
43106569e7d3SJohannes Berg 	return ret;
43116569e7d3SJohannes Berg }
43126569e7d3SJohannes Berg 
4313cbce62a3SMiri Korenblit int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4314cbce62a3SMiri Korenblit 			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
43156569e7d3SJohannes Berg 			struct ieee80211_key_conf *key)
43166569e7d3SJohannes Berg {
43176569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
43186569e7d3SJohannes Berg 	int ret;
43196569e7d3SJohannes Berg 
43206569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
43216569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4322e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
43236569e7d3SJohannes Berg 
4324e705c121SKalle Valo 	return ret;
4325e705c121SKalle Valo }
4326e705c121SKalle Valo 
4327cbce62a3SMiri Korenblit void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4328e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4329e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
4330e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4331e705c121SKalle Valo 				 u32 iv32, u16 *phase1key)
4332e705c121SKalle Valo {
4333e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4334e705c121SKalle Valo 
4335e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4336e705c121SKalle Valo 		return;
4337e705c121SKalle Valo 
4338e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4339e705c121SKalle Valo }
4340e705c121SKalle Valo 
4341e705c121SKalle Valo 
4342e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4343e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
4344e705c121SKalle Valo {
4345e705c121SKalle Valo 	struct iwl_mvm *mvm =
4346e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
4347e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
4348e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
4349e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
4350e705c121SKalle Valo 
4351e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4352e705c121SKalle Valo 		return true;
4353e705c121SKalle Valo 
4354e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4355e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4356e705c121SKalle Valo 		return true;
4357e705c121SKalle Valo 	}
4358e705c121SKalle Valo 
4359e705c121SKalle Valo 	resp = (void *)pkt->data;
4360e705c121SKalle Valo 
4361e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
4362b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4363e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
4364e705c121SKalle Valo 
4365e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4366e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4367e705c121SKalle Valo 		     te_data->uid);
4368e705c121SKalle Valo 
4369e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4370e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4371e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4372e705c121SKalle Valo 
4373e705c121SKalle Valo 	return true;
4374e705c121SKalle Valo }
4375e705c121SKalle Valo 
4376dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
4377dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
4378dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
4379dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
4380dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
4381e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4382e705c121SKalle Valo 				    struct ieee80211_channel *channel,
4383e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4384e705c121SKalle Valo 				    int duration)
4385e705c121SKalle Valo {
4386afc1e3b4SAvraham Stern 	int res;
4387e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4388e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4389e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4390e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
4391dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
4392dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
4393dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
4394e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
4395e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4396e705c121SKalle Valo 		.id_and_color =
4397e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4398e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4399e705c121SKalle Valo 	};
440057e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
440157e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
440257e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
440357e861d9SDavid Spinadel 
440457e861d9SDavid Spinadel 	/* Set the channel info data */
440557e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
44063717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
44077ac87575SJohannes Berg 			      IWL_PHY_CHANNEL_MODE20,
440857e861d9SDavid Spinadel 			      0);
440957e861d9SDavid Spinadel 
441057e861d9SDavid Spinadel 	/* Set the time and duration */
4411afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4412e705c121SKalle Valo 
4413dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
4414dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
4415dc28e12fSMatti Gottlieb 
4416dc28e12fSMatti Gottlieb 	/*
4417dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
4418dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
4419dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
4420dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
4421dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
4422dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
4423dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
4424dc28e12fSMatti Gottlieb 	 */
4425f276e20bSJohannes Berg 	if (vif->cfg.assoc) {
4426dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
4427dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
4428dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
4429dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
4430dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
4431dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
4432dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
4433dc28e12fSMatti Gottlieb 		}
4434dc28e12fSMatti Gottlieb 	}
4435dc28e12fSMatti Gottlieb 
443657e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
443757e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
4438dc28e12fSMatti Gottlieb 
4439dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
4440903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
4441903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
4442903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
4443903b3f9bSEmmanuel Grumbach 		     "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
4444903b3f9bSEmmanuel Grumbach 		     duration, delay, dtim_interval);
4445903b3f9bSEmmanuel Grumbach 
4446e705c121SKalle Valo 	/* Set the node address */
444757e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4448e705c121SKalle Valo 
4449e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4450e705c121SKalle Valo 
4451e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4452e705c121SKalle Valo 
4453e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4454e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4455e705c121SKalle Valo 		return -EIO;
4456e705c121SKalle Valo 	}
4457e705c121SKalle Valo 
4458e705c121SKalle Valo 	te_data->vif = vif;
4459e705c121SKalle Valo 	te_data->duration = duration;
4460e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
4461e705c121SKalle Valo 
4462e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4463e705c121SKalle Valo 
4464e705c121SKalle Valo 	/*
4465e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
4466e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
4467e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
4468e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4469e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
4470e705c121SKalle Valo 	 * time another notification (that the time event started)
4471e705c121SKalle Valo 	 * might already be processed unsuccessfully.
4472e705c121SKalle Valo 	 */
4473e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4474e705c121SKalle Valo 				   time_event_response,
4475e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
4476e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
4477e705c121SKalle Valo 
447857e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4479e705c121SKalle Valo 				   &aux_roc_req);
4480e705c121SKalle Valo 
4481e705c121SKalle Valo 	if (res) {
4482e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4483e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4484e705c121SKalle Valo 		goto out_clear_te;
4485e705c121SKalle Valo 	}
4486e705c121SKalle Valo 
4487e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4488e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4489e705c121SKalle Valo 	/* should never fail */
4490e705c121SKalle Valo 	WARN_ON_ONCE(res);
4491e705c121SKalle Valo 
4492e705c121SKalle Valo 	if (res) {
4493e705c121SKalle Valo  out_clear_te:
4494e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
4495e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
4496e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4497e705c121SKalle Valo 	}
4498e705c121SKalle Valo 
4499e705c121SKalle Valo 	return res;
4500e705c121SKalle Valo }
4501e705c121SKalle Valo 
4502feebebaeSMiri Korenblit static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4503feebebaeSMiri Korenblit {
4504feebebaeSMiri Korenblit 	int ret = 0;
4505feebebaeSMiri Korenblit 
4506feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4507feebebaeSMiri Korenblit 
4508feebebaeSMiri Korenblit 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4509feebebaeSMiri Korenblit 			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4510feebebaeSMiri Korenblit 		IWL_ERR(mvm, "hotspot not supported\n");
4511feebebaeSMiri Korenblit 		return -EINVAL;
4512feebebaeSMiri Korenblit 	}
4513feebebaeSMiri Korenblit 
4514feebebaeSMiri Korenblit 	if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12) {
4515feebebaeSMiri Korenblit 		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4516feebebaeSMiri Korenblit 		WARN(ret, "Failed to allocate aux station");
4517feebebaeSMiri Korenblit 	}
4518feebebaeSMiri Korenblit 
4519feebebaeSMiri Korenblit 	return ret;
4520feebebaeSMiri Korenblit }
4521feebebaeSMiri Korenblit 
4522feebebaeSMiri Korenblit static int iwl_mvm_roc_switch_binding(struct iwl_mvm *mvm,
4523feebebaeSMiri Korenblit 				      struct ieee80211_vif *vif,
4524feebebaeSMiri Korenblit 				      struct iwl_mvm_phy_ctxt *new_phy_ctxt)
4525feebebaeSMiri Korenblit {
4526feebebaeSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4527feebebaeSMiri Korenblit 	int ret = 0;
4528feebebaeSMiri Korenblit 
4529feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4530feebebaeSMiri Korenblit 
4531feebebaeSMiri Korenblit 	/* Unbind the P2P_DEVICE from the current PHY context,
4532feebebaeSMiri Korenblit 	 * and if the PHY context is not used remove it.
4533feebebaeSMiri Korenblit 	 */
4534feebebaeSMiri Korenblit 	ret = iwl_mvm_binding_remove_vif(mvm, vif);
4535feebebaeSMiri Korenblit 	if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
4536feebebaeSMiri Korenblit 		return ret;
4537feebebaeSMiri Korenblit 
4538feebebaeSMiri Korenblit 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
4539feebebaeSMiri Korenblit 
4540feebebaeSMiri Korenblit 	/* Bind the P2P_DEVICE to the current PHY Context */
4541feebebaeSMiri Korenblit 	mvmvif->deflink.phy_ctxt = new_phy_ctxt;
4542feebebaeSMiri Korenblit 
4543feebebaeSMiri Korenblit 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4544feebebaeSMiri Korenblit 	WARN(ret, "Failed binding P2P_DEVICE\n");
4545feebebaeSMiri Korenblit 	return ret;
4546feebebaeSMiri Korenblit }
4547feebebaeSMiri Korenblit 
4548e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
4549e705c121SKalle Valo 		       struct ieee80211_vif *vif,
4550e705c121SKalle Valo 		       struct ieee80211_channel *channel,
4551e705c121SKalle Valo 		       int duration,
4552e705c121SKalle Valo 		       enum ieee80211_roc_type type)
4553e705c121SKalle Valo {
4554a2906ea6SJohannes Berg 	static const struct iwl_mvm_roc_ops ops = {
4555fe8b2ad3SMiri Korenblit 		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
4556fe8b2ad3SMiri Korenblit 		.switch_phy_ctxt = iwl_mvm_roc_switch_binding,
4557fe8b2ad3SMiri Korenblit 	};
4558fe8b2ad3SMiri Korenblit 
4559fe8b2ad3SMiri Korenblit 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4560fe8b2ad3SMiri Korenblit }
4561fe8b2ad3SMiri Korenblit 
4562fe8b2ad3SMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
4563fe8b2ad3SMiri Korenblit int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4564fe8b2ad3SMiri Korenblit 		       struct ieee80211_channel *channel, int duration,
4565fe8b2ad3SMiri Korenblit 		       enum ieee80211_roc_type type,
4566a2906ea6SJohannes Berg 		       const struct iwl_mvm_roc_ops *ops)
4567fe8b2ad3SMiri Korenblit {
4568e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4569e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4570e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
4571e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
45728835a64fSJohannes Berg 	bool band_change_removal;
4573e705c121SKalle Valo 	int ret, i;
4574feebebaeSMiri Korenblit 	u32 lmac_id;
4575e705c121SKalle Valo 
4576e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4577e705c121SKalle Valo 			   duration, type);
4578e705c121SKalle Valo 
45799834781cSJohannes Berg 	/*
45809834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
45819834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
45829834781cSJohannes Berg 	 */
4583e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
4584e705c121SKalle Valo 
4585e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4586e705c121SKalle Valo 
4587e705c121SKalle Valo 	switch (vif->type) {
4588e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4589feebebaeSMiri Korenblit 		lmac_id = iwl_mvm_get_lmac_id(mvm->fw, channel->band);
45902c2c3647SNathan Errera 
4591feebebaeSMiri Korenblit 		/* Use aux roc framework (HS20) */
4592fe8b2ad3SMiri Korenblit 		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4593feebebaeSMiri Korenblit 		if (!ret)
4594e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
4595e705c121SKalle Valo 						       vif, duration);
4596e705c121SKalle Valo 		goto out_unlock;
4597e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
4598e705c121SKalle Valo 		/* handle below */
4599e705c121SKalle Valo 		break;
4600e705c121SKalle Valo 	default:
4601e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
4602e705c121SKalle Valo 		ret = -EINVAL;
4603e705c121SKalle Valo 		goto out_unlock;
4604e705c121SKalle Valo 	}
4605e705c121SKalle Valo 
4606e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
4607e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
4608650cadb7SGregory Greenman 		if (phy_ctxt->ref == 0 || mvmvif->deflink.phy_ctxt == phy_ctxt)
4609e705c121SKalle Valo 			continue;
4610e705c121SKalle Valo 
4611e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
4612fe8b2ad3SMiri Korenblit 			ret = ops->switch_phy_ctxt(mvm, vif, phy_ctxt);
4613feebebaeSMiri Korenblit 			if (ret)
4614e705c121SKalle Valo 				goto out_unlock;
4615e705c121SKalle Valo 
4616650cadb7SGregory Greenman 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4617e705c121SKalle Valo 			goto schedule_time_event;
4618e705c121SKalle Valo 		}
4619e705c121SKalle Valo 	}
4620e705c121SKalle Valo 
4621e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
4622650cadb7SGregory Greenman 	if (channel == mvmvif->deflink.phy_ctxt->channel)
4623e705c121SKalle Valo 		goto schedule_time_event;
4624e705c121SKalle Valo 
4625e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4626e705c121SKalle Valo 
4627e705c121SKalle Valo 	/*
46288835a64fSJohannes Berg 	 * Check if the remain-on-channel is on a different band and that
46298835a64fSJohannes Berg 	 * requires context removal, see iwl_mvm_phy_ctxt_changed(). If
46308835a64fSJohannes Berg 	 * so, we'll need to release and then re-configure here, since we
46318835a64fSJohannes Berg 	 * must not remove a PHY context that's part of a binding.
4632e705c121SKalle Valo 	 */
46338835a64fSJohannes Berg 	band_change_removal =
46348835a64fSJohannes Berg 		fw_has_capa(&mvm->fw->ucode_capa,
46358835a64fSJohannes Berg 			    IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
4636650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt->channel->band != chandef.chan->band;
46378835a64fSJohannes Berg 
4638650cadb7SGregory Greenman 	if (mvmvif->deflink.phy_ctxt->ref == 1 && !band_change_removal) {
46398835a64fSJohannes Berg 		/*
46408835a64fSJohannes Berg 		 * Change the PHY context configuration as it is currently
46418835a64fSJohannes Berg 		 * referenced only by the P2P Device MAC (and we can modify it)
46428835a64fSJohannes Berg 		 */
4643650cadb7SGregory Greenman 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->deflink.phy_ctxt,
4644e705c121SKalle Valo 					       &chandef, 1, 1);
4645e705c121SKalle Valo 		if (ret)
4646e705c121SKalle Valo 			goto out_unlock;
4647e705c121SKalle Valo 	} else {
4648e705c121SKalle Valo 		/*
46498835a64fSJohannes Berg 		 * The PHY context is shared with other MACs (or we're trying to
46508835a64fSJohannes Berg 		 * switch bands), so remove the P2P Device from the binding,
46518835a64fSJohannes Berg 		 * allocate an new PHY context and create a new binding.
4652e705c121SKalle Valo 		 */
4653e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4654e705c121SKalle Valo 		if (!phy_ctxt) {
4655e705c121SKalle Valo 			ret = -ENOSPC;
4656e705c121SKalle Valo 			goto out_unlock;
4657e705c121SKalle Valo 		}
4658e705c121SKalle Valo 
4659e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
4660e705c121SKalle Valo 					       1, 1);
4661e705c121SKalle Valo 		if (ret) {
4662e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
4663e705c121SKalle Valo 			goto out_unlock;
4664e705c121SKalle Valo 		}
4665e705c121SKalle Valo 
4666fe8b2ad3SMiri Korenblit 		ret = ops->switch_phy_ctxt(mvm, vif, phy_ctxt);
4667feebebaeSMiri Korenblit 		if (ret)
4668e705c121SKalle Valo 			goto out_unlock;
4669e705c121SKalle Valo 
4670650cadb7SGregory Greenman 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4671e705c121SKalle Valo 	}
4672e705c121SKalle Valo 
4673e705c121SKalle Valo schedule_time_event:
4674e705c121SKalle Valo 	/* Schedule the time events */
4675e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4676e705c121SKalle Valo 
4677e705c121SKalle Valo out_unlock:
4678e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4679e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4680e705c121SKalle Valo 	return ret;
4681e705c121SKalle Valo }
4682e705c121SKalle Valo 
4683fe8b2ad3SMiri Korenblit int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
46845db4c4b9SEmmanuel Grumbach 		       struct ieee80211_vif *vif)
4685e705c121SKalle Valo {
4686e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4687e705c121SKalle Valo 
4688e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4689e705c121SKalle Valo 
4690e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4691fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
4692e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4693e705c121SKalle Valo 
4694e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4695e705c121SKalle Valo 	return 0;
4696e705c121SKalle Valo }
4697e705c121SKalle Valo 
4698b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
4699b73f9a4aSJohannes Berg 	bool responder;
4700b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
4701b73f9a4aSJohannes Berg };
4702b73f9a4aSJohannes Berg 
4703b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4704b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
4705b73f9a4aSJohannes Berg {
4706b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4707b73f9a4aSJohannes Berg 
4708d0a9123eSJohannes Berg 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4709b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4710b73f9a4aSJohannes Berg 		data->responder = true;
4711b73f9a4aSJohannes Berg }
4712b73f9a4aSJohannes Berg 
4713b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4714b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
4715b73f9a4aSJohannes Berg {
4716b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
4717b73f9a4aSJohannes Berg 		.responder = false,
4718b73f9a4aSJohannes Berg 		.ctx = ctx,
4719b73f9a4aSJohannes Berg 	};
4720b73f9a4aSJohannes Berg 
4721b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4722b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
4723b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
4724b73f9a4aSJohannes Berg 					&data);
4725b73f9a4aSJohannes Berg 	return data.responder;
4726b73f9a4aSJohannes Berg }
4727b73f9a4aSJohannes Berg 
4728e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4729e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
4730e705c121SKalle Valo {
4731e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4732e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4733b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4734b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4735e705c121SKalle Valo 	int ret;
4736e705c121SKalle Valo 
4737e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4738e705c121SKalle Valo 
4739e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4740e705c121SKalle Valo 
4741e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4742e705c121SKalle Valo 	if (!phy_ctxt) {
4743e705c121SKalle Valo 		ret = -ENOSPC;
4744e705c121SKalle Valo 		goto out;
4745e705c121SKalle Valo 	}
4746e705c121SKalle Valo 
4747b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4748e705c121SKalle Valo 				       ctx->rx_chains_static,
4749e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
4750e705c121SKalle Valo 	if (ret) {
4751e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4752e705c121SKalle Valo 		goto out;
4753e705c121SKalle Valo 	}
4754e705c121SKalle Valo 
4755e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4756e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4757e705c121SKalle Valo out:
4758e705c121SKalle Valo 	return ret;
4759e705c121SKalle Valo }
4760e705c121SKalle Valo 
4761cbce62a3SMiri Korenblit int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4762e705c121SKalle Valo 			struct ieee80211_chanctx_conf *ctx)
4763e705c121SKalle Valo {
4764e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4765e705c121SKalle Valo 	int ret;
4766e705c121SKalle Valo 
4767e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4768e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4769e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4770e705c121SKalle Valo 
4771e705c121SKalle Valo 	return ret;
4772e705c121SKalle Valo }
4773e705c121SKalle Valo 
4774e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4775e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4776e705c121SKalle Valo {
4777e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4778e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4779e705c121SKalle Valo 
4780e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4781e705c121SKalle Valo 
4782e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4783e705c121SKalle Valo }
4784e705c121SKalle Valo 
4785cbce62a3SMiri Korenblit void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4786e705c121SKalle Valo 			    struct ieee80211_chanctx_conf *ctx)
4787e705c121SKalle Valo {
4788e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4789e705c121SKalle Valo 
4790e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4791e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4792e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4793e705c121SKalle Valo }
4794e705c121SKalle Valo 
4795cbce62a3SMiri Korenblit void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4796cbce62a3SMiri Korenblit 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
4797e705c121SKalle Valo {
4798e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4799e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4800e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4801b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4802b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4803e705c121SKalle Valo 
4804e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4805e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4806e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4807e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4808e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4809e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4810e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4811e705c121SKalle Valo 		return;
4812e705c121SKalle Valo 
4813e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
48147a20bcceSEmmanuel Grumbach 
48157a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
48167a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4817b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
48187a20bcceSEmmanuel Grumbach 			goto out_unlock;
48197a20bcceSEmmanuel Grumbach 
48207a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
48217a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4822b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
48237a20bcceSEmmanuel Grumbach 			goto out_unlock;
48247a20bcceSEmmanuel Grumbach 	}
48257a20bcceSEmmanuel Grumbach 
4826e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4827b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4828e705c121SKalle Valo 				 ctx->rx_chains_static,
4829e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
48307a20bcceSEmmanuel Grumbach 
48317a20bcceSEmmanuel Grumbach out_unlock:
4832e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4833e705c121SKalle Valo }
4834e705c121SKalle Valo 
48358a919a78SMiri Korenblit /*
48368a919a78SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
48378a919a78SMiri Korenblit  *
48388a919a78SMiri Korenblit  * Returns true if we're done assigning the chanctx
48398a919a78SMiri Korenblit  * (either on failure or success)
48408a919a78SMiri Korenblit  */
48414263ac7fSGregory Greenman static bool
48424263ac7fSGregory Greenman __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
4843e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4844e705c121SKalle Valo 				    struct ieee80211_chanctx_conf *ctx,
484550e81437SMiri Korenblit 				    bool switching_chanctx, int *ret)
4846e705c121SKalle Valo {
4847e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4848e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4849e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4850e705c121SKalle Valo 
4851e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4852e705c121SKalle Valo 
4853650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = phy_ctxt;
4854e705c121SKalle Valo 
4855e705c121SKalle Valo 	switch (vif->type) {
4856e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4857e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4858e705c121SKalle Valo 		if (switching_chanctx) {
4859e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4860e705c121SKalle Valo 			break;
4861e705c121SKalle Valo 		}
48625a2abdcaSGustavo A. R. Silva 		fallthrough;
4863e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4864e705c121SKalle Valo 		/*
4865e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4866e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4867e705c121SKalle Valo 		 */
48688a919a78SMiri Korenblit 		*ret = 0;
48698a919a78SMiri Korenblit 		return true;
4870e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4871e705c121SKalle Valo 		break;
4872e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4873e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4874e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4875e705c121SKalle Valo 		break;
4876e705c121SKalle Valo 	default:
48778a919a78SMiri Korenblit 		*ret = -EINVAL;
48788a919a78SMiri Korenblit 		return true;
4879e705c121SKalle Valo 	}
48808a919a78SMiri Korenblit 	return false;
48818a919a78SMiri Korenblit }
48828a919a78SMiri Korenblit 
48838a919a78SMiri Korenblit static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
48848a919a78SMiri Korenblit 					struct ieee80211_vif *vif,
48854263ac7fSGregory Greenman 					struct ieee80211_bss_conf *link_conf,
48868a919a78SMiri Korenblit 					struct ieee80211_chanctx_conf *ctx,
48878a919a78SMiri Korenblit 					bool switching_chanctx)
48888a919a78SMiri Korenblit {
48898a919a78SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
48908a919a78SMiri Korenblit 	int ret;
48918a919a78SMiri Korenblit 
48924263ac7fSGregory Greenman 	if (WARN_ON(!link_conf))
48934263ac7fSGregory Greenman 		return -EINVAL;
48944263ac7fSGregory Greenman 
48958a919a78SMiri Korenblit 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
48968a919a78SMiri Korenblit 						switching_chanctx, &ret))
48978a919a78SMiri Korenblit 		goto out;
4898e705c121SKalle Valo 
4899e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4900e705c121SKalle Valo 	if (ret)
4901e705c121SKalle Valo 		goto out;
4902e705c121SKalle Valo 
4903e705c121SKalle Valo 	/*
4904e705c121SKalle Valo 	 * Power state must be updated before quotas,
4905e705c121SKalle Valo 	 * otherwise fw will complain.
4906e705c121SKalle Valo 	 */
4907e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4908e705c121SKalle Valo 
4909e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4910e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4911e705c121SKalle Valo 	 * will handle quota settings.
4912e705c121SKalle Valo 	 */
4913e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4914e705c121SKalle Valo 		mvmvif->monitor_active = true;
4915e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4916e705c121SKalle Valo 		if (ret)
4917e705c121SKalle Valo 			goto out_remove_binding;
49180e39eb03SChaya Rachel Ivgi 
49190e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
49200e39eb03SChaya Rachel Ivgi 		if (ret)
49210e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
49220e39eb03SChaya Rachel Ivgi 
4923e705c121SKalle Valo 	}
4924e705c121SKalle Valo 
4925e705c121SKalle Valo 	/* Handle binding during CSA */
4926e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4927e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4928e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4929e705c121SKalle Valo 	}
4930e705c121SKalle Valo 
49318a919a78SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION) {
49328a919a78SMiri Korenblit 		if (!switching_chanctx) {
49338a919a78SMiri Korenblit 			mvmvif->csa_bcn_pending = false;
49348a919a78SMiri Korenblit 			goto out;
49358a919a78SMiri Korenblit 		}
49368a919a78SMiri Korenblit 
493774a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
493874a10252SSara Sharon 
493974a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
494074a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
494119125cb0SAndrei Otcheretianski 			u32 duration = 3 * vif->bss_conf.beacon_int;
4942e705c121SKalle Valo 
4943e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4944e705c121SKalle Valo 			 * beacon on the new channel.
4945e705c121SKalle Valo 			 */
4946e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4947e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4948e705c121SKalle Valo 						true);
494974a10252SSara Sharon 		}
4950e705c121SKalle Valo 
4951e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4952e705c121SKalle Valo 	}
4953e705c121SKalle Valo 
4954e705c121SKalle Valo 	goto out;
4955e705c121SKalle Valo 
4956e705c121SKalle Valo out_remove_binding:
4957e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4958e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4959e705c121SKalle Valo out:
4960e705c121SKalle Valo 	if (ret)
4961650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = NULL;
4962e705c121SKalle Valo 	return ret;
4963e705c121SKalle Valo }
49648a919a78SMiri Korenblit 
4965e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4966e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4967727eff4dSGregory Greenman 				      struct ieee80211_bss_conf *link_conf,
4968e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4969e705c121SKalle Valo {
4970e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4971e705c121SKalle Valo 	int ret;
4972e705c121SKalle Valo 
4973e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
49744263ac7fSGregory Greenman 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
4975e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4976e705c121SKalle Valo 
4977e705c121SKalle Valo 	return ret;
4978e705c121SKalle Valo }
4979e705c121SKalle Valo 
4980daddfae5SMiri Korenblit /*
4981daddfae5SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
4982daddfae5SMiri Korenblit  *
4983daddfae5SMiri Korenblit  * Returns if chanctx unassign chanctx is done
4984daddfae5SMiri Korenblit  * (either on failure or success)
4985daddfae5SMiri Korenblit  */
49864263ac7fSGregory Greenman static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
4987e705c121SKalle Valo 						  struct ieee80211_vif *vif,
4988e705c121SKalle Valo 						  bool switching_chanctx)
4989e705c121SKalle Valo {
4990e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4991e705c121SKalle Valo 
4992e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4993650cadb7SGregory Greenman 	iwl_mvm_remove_time_event(mvm, mvmvif,
4994650cadb7SGregory Greenman 				  &mvmvif->time_event_data);
4995e705c121SKalle Valo 
4996e705c121SKalle Valo 	switch (vif->type) {
4997e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4998daddfae5SMiri Korenblit 		return true;
4999e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
5000e705c121SKalle Valo 		mvmvif->monitor_active = false;
5001e705c121SKalle Valo 		mvmvif->ps_disabled = false;
5002e705c121SKalle Valo 		break;
5003e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5004e705c121SKalle Valo 		/* This part is triggered only during CSA */
5005e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
5006daddfae5SMiri Korenblit 			return true;
5007e705c121SKalle Valo 
5008e705c121SKalle Valo 		mvmvif->csa_countdown = false;
5009e705c121SKalle Valo 
5010e705c121SKalle Valo 		/* Set CS bit on all the stations */
5011e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5012e705c121SKalle Valo 
5013e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
5014e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5015e705c121SKalle Valo 
5016e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
5017e705c121SKalle Valo 		break;
5018daddfae5SMiri Korenblit 	default:
5019e705c121SKalle Valo 		break;
5020daddfae5SMiri Korenblit 	}
5021daddfae5SMiri Korenblit 	return false;
5022daddfae5SMiri Korenblit }
5023e705c121SKalle Valo 
5024e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5025e705c121SKalle Valo 					   struct ieee80211_vif *vif,
50264263ac7fSGregory Greenman 					   struct ieee80211_bss_conf *link_conf,
5027e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
5028e705c121SKalle Valo 					   bool switching_chanctx)
5029e705c121SKalle Valo {
5030e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5031e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
5032e705c121SKalle Valo 
5033daddfae5SMiri Korenblit 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5034e705c121SKalle Valo 		goto out;
5035daddfae5SMiri Korenblit 
5036daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_MONITOR)
5037e705c121SKalle Valo 		iwl_mvm_rm_snif_sta(mvm, vif);
5038e705c121SKalle Valo 
5039e705c121SKalle Valo 
5040daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5041e705c121SKalle Valo 		disabled_vif = vif;
504274a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
504374a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5044e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5045e705c121SKalle Valo 	}
5046e705c121SKalle Valo 
5047e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5048e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
5049e705c121SKalle Valo 
5050e705c121SKalle Valo out:
5051bf544e9aSSara Sharon 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5052bf544e9aSSara Sharon 	    switching_chanctx)
5053bf544e9aSSara Sharon 		return;
5054650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
5055e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5056e705c121SKalle Valo }
5057e705c121SKalle Valo 
5058e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5059e705c121SKalle Valo 					 struct ieee80211_vif *vif,
5060727eff4dSGregory Greenman 					 struct ieee80211_bss_conf *link_conf,
5061e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
5062e705c121SKalle Valo {
5063e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5064e705c121SKalle Valo 
5065e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
50664263ac7fSGregory Greenman 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5067e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5068e705c121SKalle Valo }
5069e705c121SKalle Valo 
5070e705c121SKalle Valo static int
5071e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5072660eba5aSMiri Korenblit 				struct ieee80211_vif_chanctx_switch *vifs,
5073a2906ea6SJohannes Berg 				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5074e705c121SKalle Valo {
5075e705c121SKalle Valo 	int ret;
5076e705c121SKalle Valo 
5077e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
50784263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
50794263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5080e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5081e705c121SKalle Valo 
5082e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5083e705c121SKalle Valo 	if (ret) {
5084e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5085e705c121SKalle Valo 		goto out_reassign;
5086e705c121SKalle Valo 	}
5087e705c121SKalle Valo 
50884263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
50894263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5090e705c121SKalle Valo 	if (ret) {
5091e705c121SKalle Valo 		IWL_ERR(mvm,
5092e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5093e705c121SKalle Valo 		goto out_remove;
5094e705c121SKalle Valo 	}
5095e705c121SKalle Valo 
5096e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
5097e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5098e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
5099e705c121SKalle Valo 
5100e705c121SKalle Valo 	goto out;
5101e705c121SKalle Valo 
5102e705c121SKalle Valo out_remove:
5103e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5104e705c121SKalle Valo 
5105e705c121SKalle Valo out_reassign:
5106e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5107e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5108e705c121SKalle Valo 		goto out_restart;
5109e705c121SKalle Valo 	}
5110e705c121SKalle Valo 
51114263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51124263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5113e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5114e705c121SKalle Valo 		goto out_restart;
5115e705c121SKalle Valo 	}
5116e705c121SKalle Valo 
5117e705c121SKalle Valo 	goto out;
5118e705c121SKalle Valo 
5119e705c121SKalle Valo out_restart:
5120e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5121e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5122e705c121SKalle Valo 
5123e705c121SKalle Valo out:
5124e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5125e705c121SKalle Valo 
5126e705c121SKalle Valo 	return ret;
5127e705c121SKalle Valo }
5128e705c121SKalle Valo 
5129e705c121SKalle Valo static int
5130e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5131660eba5aSMiri Korenblit 				    struct ieee80211_vif_chanctx_switch *vifs,
5132a2906ea6SJohannes Berg 				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5133e705c121SKalle Valo {
5134e705c121SKalle Valo 	int ret;
5135e705c121SKalle Valo 
5136e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
51374263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51384263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5139e705c121SKalle Valo 
51404263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51414263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5142e705c121SKalle Valo 	if (ret) {
5143e705c121SKalle Valo 		IWL_ERR(mvm,
5144e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5145e705c121SKalle Valo 		goto out_reassign;
5146e705c121SKalle Valo 	}
5147e705c121SKalle Valo 
5148e705c121SKalle Valo 	goto out;
5149e705c121SKalle Valo 
5150e705c121SKalle Valo out_reassign:
51514263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51524263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5153e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5154e705c121SKalle Valo 		goto out_restart;
5155e705c121SKalle Valo 	}
5156e705c121SKalle Valo 
5157e705c121SKalle Valo 	goto out;
5158e705c121SKalle Valo 
5159e705c121SKalle Valo out_restart:
5160e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5161e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5162e705c121SKalle Valo 
5163e705c121SKalle Valo out:
5164e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5165e705c121SKalle Valo 
5166e705c121SKalle Valo 	return ret;
5167e705c121SKalle Valo }
5168e705c121SKalle Valo 
5169660eba5aSMiri Korenblit /* Execute the common part for both MLD and non-MLD modes */
5170660eba5aSMiri Korenblit int
5171660eba5aSMiri Korenblit iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5172e705c121SKalle Valo 				  struct ieee80211_vif_chanctx_switch *vifs,
5173e705c121SKalle Valo 				  int n_vifs,
5174660eba5aSMiri Korenblit 				  enum ieee80211_chanctx_switch_mode mode,
5175a2906ea6SJohannes Berg 				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5176e705c121SKalle Valo {
5177e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5178e705c121SKalle Valo 	int ret;
5179e705c121SKalle Valo 
5180e705c121SKalle Valo 	/* we only support a single-vif right now */
5181e705c121SKalle Valo 	if (n_vifs > 1)
5182e705c121SKalle Valo 		return -EOPNOTSUPP;
5183e705c121SKalle Valo 
5184e705c121SKalle Valo 	switch (mode) {
5185e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5186660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5187e705c121SKalle Valo 		break;
5188e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
5189660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5190e705c121SKalle Valo 		break;
5191e705c121SKalle Valo 	default:
5192e705c121SKalle Valo 		ret = -EOPNOTSUPP;
5193e705c121SKalle Valo 		break;
5194e705c121SKalle Valo 	}
5195e705c121SKalle Valo 
5196e705c121SKalle Valo 	return ret;
5197e705c121SKalle Valo }
5198e705c121SKalle Valo 
5199660eba5aSMiri Korenblit static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5200660eba5aSMiri Korenblit 				      struct ieee80211_vif_chanctx_switch *vifs,
5201660eba5aSMiri Korenblit 				      int n_vifs,
5202660eba5aSMiri Korenblit 				      enum ieee80211_chanctx_switch_mode mode)
5203660eba5aSMiri Korenblit {
5204a2906ea6SJohannes Berg 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5205660eba5aSMiri Korenblit 		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5206660eba5aSMiri Korenblit 		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5207660eba5aSMiri Korenblit 	};
5208660eba5aSMiri Korenblit 
5209660eba5aSMiri Korenblit 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5210660eba5aSMiri Korenblit }
5211660eba5aSMiri Korenblit 
5212cbce62a3SMiri Korenblit int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
52132f0282dbSJohannes Berg {
52142f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
52152f0282dbSJohannes Berg 
52162f0282dbSJohannes Berg 	return mvm->ibss_manager;
52172f0282dbSJohannes Berg }
52182f0282dbSJohannes Berg 
5219cbce62a3SMiri Korenblit int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5220e705c121SKalle Valo 		    bool set)
5221e705c121SKalle Valo {
5222e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5223e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5224e705c121SKalle Valo 
5225e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
5226e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
5227e705c121SKalle Valo 		return -EINVAL;
5228e705c121SKalle Valo 	}
5229e705c121SKalle Valo 
523036cf5377SGregory Greenman 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
523136cf5377SGregory Greenman 					       &mvm_sta->vif->bss_conf);
5232e705c121SKalle Valo }
5233e705c121SKalle Valo 
5234e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
5235e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5236e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5237e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5238e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5239e705c121SKalle Valo };
5240e705c121SKalle Valo 
5241e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5242e705c121SKalle Valo 				      struct ieee80211_vif *vif,
5243e705c121SKalle Valo 				      void *data, int len)
5244e705c121SKalle Valo {
5245e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5246e705c121SKalle Valo 	int err;
5247e705c121SKalle Valo 	u32 noa_duration;
5248e705c121SKalle Valo 
52498cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
52508cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
5251e705c121SKalle Valo 	if (err)
5252e705c121SKalle Valo 		return err;
5253e705c121SKalle Valo 
5254e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
5255e705c121SKalle Valo 		return -EINVAL;
5256e705c121SKalle Valo 
5257e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5258e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
5259e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5260e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
5261e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5262e705c121SKalle Valo 			return -EINVAL;
5263e705c121SKalle Valo 
5264e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5265e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
5266e705c121SKalle Valo 			return -EINVAL;
5267e705c121SKalle Valo 
5268e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
5269e705c121SKalle Valo 		mvm->noa_vif = vif;
5270e705c121SKalle Valo 
527122b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
5272e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5273e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
5274e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5275f276e20bSJohannes Berg 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5276e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5277e705c121SKalle Valo 			return -EINVAL;
5278e705c121SKalle Valo 
5279e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
5280e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
5281e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
5282e705c121SKalle Valo 	}
5283e705c121SKalle Valo 
5284e705c121SKalle Valo 	return -EOPNOTSUPP;
5285e705c121SKalle Valo }
5286e705c121SKalle Valo 
5287cbce62a3SMiri Korenblit int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5288e705c121SKalle Valo 			     struct ieee80211_vif *vif,
5289e705c121SKalle Valo 			     void *data, int len)
5290e705c121SKalle Valo {
5291e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5292e705c121SKalle Valo 	int err;
5293e705c121SKalle Valo 
5294e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5295e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5296e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5297e705c121SKalle Valo 
5298e705c121SKalle Valo 	return err;
5299e705c121SKalle Valo }
5300e705c121SKalle Valo #endif
5301e705c121SKalle Valo 
5302cbce62a3SMiri Korenblit void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5303e705c121SKalle Valo 			    struct ieee80211_channel_switch *chsw)
5304e705c121SKalle Valo {
5305e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
5306e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
5307e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
5308e705c121SKalle Valo 	 * (which is when the absence time event starts).
5309e705c121SKalle Valo 	 */
5310e705c121SKalle Valo 
5311e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5312e705c121SKalle Valo 			   "dummy channel switch op\n");
5313e705c121SKalle Valo }
5314e705c121SKalle Valo 
531574a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
531674a10252SSara Sharon 				       struct ieee80211_vif *vif,
531774a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
531874a10252SSara Sharon {
531974a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
532074a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
532174a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
532274a10252SSara Sharon 							  mvmvif->color)),
532374a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
532474a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
532574a10252SSara Sharon 		.cs_count = chsw->count,
532677738865SSara Sharon 		.cs_mode = chsw->block_tx,
532774a10252SSara Sharon 	};
532874a10252SSara Sharon 
532974a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
533074a10252SSara Sharon 
53319cfcf71cSSara Sharon 	if (chsw->delay)
53329cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
53339cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
53349cfcf71cSSara Sharon 
533574a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
533674a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
533774a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
533874a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
533974a10252SSara Sharon }
534074a10252SSara Sharon 
53410202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
53420202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
53430202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
53440202bcf0SEmmanuel Grumbach {
53450202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
53460202bcf0SEmmanuel Grumbach 	u32 apply_time;
53470202bcf0SEmmanuel Grumbach 
53480202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
53490202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
53500202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
53510202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
53520202bcf0SEmmanuel Grumbach 	 * loss).
53530202bcf0SEmmanuel Grumbach 	 */
53540202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
53550202bcf0SEmmanuel Grumbach 		apply_time = 0;
53560202bcf0SEmmanuel Grumbach 	else
53570202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
53580202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
53590202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
53600202bcf0SEmmanuel Grumbach 
53610202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
53620202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
53630202bcf0SEmmanuel Grumbach 
53640202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
53650202bcf0SEmmanuel Grumbach 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
53660202bcf0SEmmanuel Grumbach 
53670202bcf0SEmmanuel Grumbach 		if (ret)
53680202bcf0SEmmanuel Grumbach 			return ret;
53690202bcf0SEmmanuel Grumbach 	}
53700202bcf0SEmmanuel Grumbach 
53710202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
53720202bcf0SEmmanuel Grumbach 				    apply_time);
53730202bcf0SEmmanuel Grumbach 
53740202bcf0SEmmanuel Grumbach 	return 0;
53750202bcf0SEmmanuel Grumbach }
53760202bcf0SEmmanuel Grumbach 
5377f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
5378cbce62a3SMiri Korenblit int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
5379e705c121SKalle Valo 			       struct ieee80211_vif *vif,
5380e705c121SKalle Valo 			       struct ieee80211_channel_switch *chsw)
5381e705c121SKalle Valo {
5382e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5383e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
5384e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5385e705c121SKalle Valo 	int ret;
5386e705c121SKalle Valo 
5387e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5388e705c121SKalle Valo 
5389e705c121SKalle Valo 	mvmvif->csa_failed = false;
5390e705c121SKalle Valo 
5391e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5392e705c121SKalle Valo 			   chsw->chandef.center_freq1);
5393e705c121SKalle Valo 
53947174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
53957174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
53967174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5397e705c121SKalle Valo 
5398e705c121SKalle Valo 	switch (vif->type) {
5399e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5400e705c121SKalle Valo 		csa_vif =
5401e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
5402e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
5403d0a9123eSJohannes Berg 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5404e705c121SKalle Valo 			      "Another CSA is already in progress")) {
5405e705c121SKalle Valo 			ret = -EBUSY;
5406e705c121SKalle Valo 			goto out_unlock;
5407e705c121SKalle Valo 		}
5408e705c121SKalle Valo 
5409d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
5410d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5411d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
5412d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
5413d3a108a4SAndrei Otcheretianski 			goto out_unlock;
5414d3a108a4SAndrei Otcheretianski 		}
5415d3a108a4SAndrei Otcheretianski 
5416e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
5417e705c121SKalle Valo 
5418e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
5419e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
5420e705c121SKalle Valo 			ret = -EBUSY;
5421e705c121SKalle Valo 			goto out_unlock;
5422e705c121SKalle Valo 		}
5423e705c121SKalle Valo 
5424d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5425d3a108a4SAndrei Otcheretianski 
5426e705c121SKalle Valo 		break;
5427e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
54282360acbdSEmmanuel Grumbach 		/*
5429ad12b231SNathan Errera 		 * In the new flow FW is in charge of timing the switch so there
5430ad12b231SNathan Errera 		 * is no need for all of this
5431ad12b231SNathan Errera 		 */
5432ad12b231SNathan Errera 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5433ad12b231SNathan Errera 					    CHANNEL_SWITCH_ERROR_NOTIF,
5434ad12b231SNathan Errera 					    0))
5435ad12b231SNathan Errera 			break;
5436ad12b231SNathan Errera 
5437ad12b231SNathan Errera 		/*
54382360acbdSEmmanuel Grumbach 		 * We haven't configured the firmware to be associated yet since
54392360acbdSEmmanuel Grumbach 		 * we don't know the dtim period. In this case, the firmware can't
54402360acbdSEmmanuel Grumbach 		 * track the beacons.
54412360acbdSEmmanuel Grumbach 		 */
5442f276e20bSJohannes Berg 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period) {
54432360acbdSEmmanuel Grumbach 			ret = -EBUSY;
54442360acbdSEmmanuel Grumbach 			goto out_unlock;
54452360acbdSEmmanuel Grumbach 		}
54462360acbdSEmmanuel Grumbach 
544787d9564eSJohannes Berg 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX)
544887d9564eSJohannes Berg 			schedule_delayed_work(&mvmvif->csa_work, 0);
544987d9564eSJohannes Berg 
5450f6780614SSara Sharon 		if (chsw->block_tx) {
5451f6780614SSara Sharon 			/*
5452f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
5453f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
5454f6780614SSara Sharon 			 */
5455f6780614SSara Sharon 			if (!chsw->count ||
5456f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
5457f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
5458f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
5459f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5460f6780614SSara Sharon 		}
5461e705c121SKalle Valo 
54620202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
54630202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
54640202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5465e705c121SKalle Valo 			if (ret)
5466e705c121SKalle Valo 				goto out_unlock;
54670202bcf0SEmmanuel Grumbach 		} else {
546874a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
54690202bcf0SEmmanuel Grumbach 		}
547081b4e44eSSara Sharon 
547181b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
547281b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
5473e705c121SKalle Valo 		break;
5474e705c121SKalle Valo 	default:
5475e705c121SKalle Valo 		break;
5476e705c121SKalle Valo 	}
5477e705c121SKalle Valo 
5478e705c121SKalle Valo 	mvmvif->ps_disabled = true;
5479e705c121SKalle Valo 
5480e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
5481e705c121SKalle Valo 	if (ret)
5482e705c121SKalle Valo 		goto out_unlock;
5483e705c121SKalle Valo 
5484e705c121SKalle Valo 	/* we won't be on this channel any longer */
5485e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
5486e705c121SKalle Valo 
5487e705c121SKalle Valo out_unlock:
5488e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5489e705c121SKalle Valo 
5490e705c121SKalle Valo 	return ret;
5491e705c121SKalle Valo }
5492e705c121SKalle Valo 
5493cbce62a3SMiri Korenblit void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5494c37763d2SSara Sharon 				      struct ieee80211_vif *vif,
5495c37763d2SSara Sharon 				      struct ieee80211_channel_switch *chsw)
5496e705c121SKalle Valo {
5497e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5498c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5499c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
5500c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5501c37763d2SSara Sharon 							  mvmvif->color)),
5502c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5503c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
5504c37763d2SSara Sharon 		.cs_count = chsw->count,
550577738865SSara Sharon 		.cs_mode = chsw->block_tx,
5506c37763d2SSara Sharon 	};
5507e705c121SKalle Valo 
5508ad12b231SNathan Errera 	/*
5509ad12b231SNathan Errera 	 * In the new flow FW is in charge of timing the switch so there is no
5510ad12b231SNathan Errera 	 * need for all of this
5511ad12b231SNathan Errera 	 */
5512ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5513ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5514ad12b231SNathan Errera 		return;
5515ad12b231SNathan Errera 
5516c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5517c37763d2SSara Sharon 		return;
5518e705c121SKalle Valo 
5519c3eae059SGregory Greenman 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5520c3eae059SGregory Greenman 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5521c3eae059SGregory Greenman 
552281b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
552381b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
552481b4e44eSSara Sharon 			/* Second time, give up on this AP*/
552581b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
552681b4e44eSSara Sharon 			ieee80211_chswitch_done(vif, false);
552781b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
552881b4e44eSSara Sharon 			return;
5529e705c121SKalle Valo 		}
553081b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
5531e705c121SKalle Valo 	}
553281b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
5533e705c121SKalle Valo 
5534caf46377SSara Sharon 	mutex_lock(&mvm->mutex);
5535caf46377SSara Sharon 	if (mvmvif->csa_failed)
5536caf46377SSara Sharon 		goto out_unlock;
5537e705c121SKalle Valo 
5538c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5539c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
5540c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5541caf46377SSara Sharon 				     0, sizeof(cmd), &cmd));
5542caf46377SSara Sharon out_unlock:
5543caf46377SSara Sharon 	mutex_unlock(&mvm->mutex);
5544e705c121SKalle Valo }
5545e705c121SKalle Valo 
55466110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
55476110d9e5SDavid Spinadel {
5548435d0827SSara Sharon 	int i;
5549435d0827SSara Sharon 
555006195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5551309c4848SLuca Coelho 		if (drop) {
5552309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
55536110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
5554d4e3a341SMordechay Goodstein 				iwl_mvm_flushable_queues(mvm) & queues);
5555309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
5556309c4848SLuca Coelho 		} else {
5557435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5558309c4848SLuca Coelho 		}
5559435d0827SSara Sharon 		return;
5560435d0827SSara Sharon 	}
55616110d9e5SDavid Spinadel 
55626110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
5563be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5564435d0827SSara Sharon 		struct ieee80211_sta *sta;
5565435d0827SSara Sharon 
5566435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
55676110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
55686110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
55696110d9e5SDavid Spinadel 			continue;
55706110d9e5SDavid Spinadel 
557106195639SSara Sharon 		if (drop)
5572d4e3a341SMordechay Goodstein 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
557306195639SSara Sharon 		else
55746110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
55756110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
55766110d9e5SDavid Spinadel 	}
55776110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
55786110d9e5SDavid Spinadel }
55796110d9e5SDavid Spinadel 
5580cbce62a3SMiri Korenblit void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5581cbce62a3SMiri Korenblit 		       u32 queues, bool drop)
5582e705c121SKalle Valo {
5583e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5584e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
5585e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
5586e705c121SKalle Valo 	struct ieee80211_sta *sta;
5587d3f9cd61SJohannes Berg 	bool ap_sta_done = false;
5588e705c121SKalle Valo 	int i;
5589e705c121SKalle Valo 	u32 msk = 0;
5590e705c121SKalle Valo 
55916110d9e5SDavid Spinadel 	if (!vif) {
55926110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
55936110d9e5SDavid Spinadel 		return;
55946110d9e5SDavid Spinadel 	}
55956110d9e5SDavid Spinadel 
55966110d9e5SDavid Spinadel 	if (vif->type != NL80211_IFTYPE_STATION)
5597e705c121SKalle Valo 		return;
5598e705c121SKalle Valo 
559924afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
560024afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
560124afba76SLiad Kaufman 
5602e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5603e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
5604e705c121SKalle Valo 
5605e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
5606be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5607e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5608e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
5609e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
5610e705c121SKalle Valo 			continue;
5611e705c121SKalle Valo 
5612e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5613e705c121SKalle Valo 		if (mvmsta->vif != vif)
5614e705c121SKalle Valo 			continue;
5615e705c121SKalle Valo 
5616d3f9cd61SJohannes Berg 		if (sta == mvmvif->ap_sta) {
5617d3f9cd61SJohannes Berg 			if (ap_sta_done)
5618d3f9cd61SJohannes Berg 				continue;
5619d3f9cd61SJohannes Berg 			ap_sta_done = true;
5620d3f9cd61SJohannes Berg 		}
5621d3f9cd61SJohannes Berg 
5622e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
5623d3f9cd61SJohannes Berg 		WARN_ON_ONCE(sta != mvmvif->ap_sta && !sta->tdls);
5624e705c121SKalle Valo 
5625d49394a1SSara Sharon 		if (drop) {
5626f9084775SNathan Errera 			if (iwl_mvm_flush_sta(mvm, mvmsta, false))
5627d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
5628d49394a1SSara Sharon 		} else {
5629d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
5630d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5631f7bd883bSJohannes Berg 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5632f7bd883bSJohannes Berg 				msk |= mvmsta->tfd_queue_msk;
5633e705c121SKalle Valo 		}
5634d49394a1SSara Sharon 	}
5635e705c121SKalle Valo 
5636e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5637e705c121SKalle Valo 
5638e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
5639e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
5640e705c121SKalle Valo 	 */
5641d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5642a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5643e705c121SKalle Valo }
5644e705c121SKalle Valo 
5645cbce62a3SMiri Korenblit int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5646e705c121SKalle Valo 			   struct survey_info *survey)
5647e705c121SKalle Valo {
5648e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5649e705c121SKalle Valo 	int ret;
5650e705c121SKalle Valo 
5651e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
5652e705c121SKalle Valo 
5653e705c121SKalle Valo 	/* only support global statistics right now */
5654e705c121SKalle Valo 	if (idx != 0)
5655e705c121SKalle Valo 		return -ENOENT;
5656e705c121SKalle Valo 
5657280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
5658e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
5659e705c121SKalle Valo 		return -ENOENT;
5660e705c121SKalle Valo 
5661e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5662e705c121SKalle Valo 
5663aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
5664e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
5665e705c121SKalle Valo 		if (ret)
5666e705c121SKalle Valo 			goto out;
5667e705c121SKalle Valo 	}
5668e705c121SKalle Valo 
5669e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
5670e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
5671e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
5672e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
5673e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
5674e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
5675e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
5676e705c121SKalle Valo 
5677e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
5678e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
5679e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
5680e705c121SKalle Valo 
5681e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
5682e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
5683e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
5684e705c121SKalle Valo 
5685e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
5686e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
5687e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
5688e705c121SKalle Valo 
5689e705c121SKalle Valo 	ret = 0;
5690e705c121SKalle Valo  out:
5691e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5692e705c121SKalle Valo 	return ret;
5693e705c121SKalle Valo }
5694e705c121SKalle Valo 
5695ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
5696ed780545SJohannes Berg {
569782cdbd11SMiri Korenblit 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
56987138763eSJohannes Berg 	u32 gi_ltf;
569982cdbd11SMiri Korenblit 
570082cdbd11SMiri Korenblit 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
5701ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
5702ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
5703ed780545SJohannes Berg 		break;
5704ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
5705ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
5706ed780545SJohannes Berg 		break;
5707ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
5708ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
5709ed780545SJohannes Berg 		break;
5710ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
5711ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
5712ed780545SJohannes Berg 		break;
571323dcee94SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_320:
571423dcee94SJohannes Berg 		rinfo->bw = RATE_INFO_BW_320;
571523dcee94SJohannes Berg 		break;
5716ed780545SJohannes Berg 	}
5717ed780545SJohannes Berg 
571882cdbd11SMiri Korenblit 	if (format == RATE_MCS_CCK_MSK ||
571982cdbd11SMiri Korenblit 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
572082cdbd11SMiri Korenblit 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
572182cdbd11SMiri Korenblit 
572282cdbd11SMiri Korenblit 		/* add the offset needed to get to the legacy ofdm indices */
572382cdbd11SMiri Korenblit 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
572482cdbd11SMiri Korenblit 			rate += IWL_FIRST_OFDM_RATE;
572582cdbd11SMiri Korenblit 
572682cdbd11SMiri Korenblit 		switch (rate) {
572782cdbd11SMiri Korenblit 		case IWL_RATE_1M_INDEX:
572882cdbd11SMiri Korenblit 			rinfo->legacy = 10;
572982cdbd11SMiri Korenblit 			break;
573082cdbd11SMiri Korenblit 		case IWL_RATE_2M_INDEX:
573182cdbd11SMiri Korenblit 			rinfo->legacy = 20;
573282cdbd11SMiri Korenblit 			break;
573382cdbd11SMiri Korenblit 		case IWL_RATE_5M_INDEX:
573482cdbd11SMiri Korenblit 			rinfo->legacy = 55;
573582cdbd11SMiri Korenblit 			break;
573682cdbd11SMiri Korenblit 		case IWL_RATE_11M_INDEX:
573782cdbd11SMiri Korenblit 			rinfo->legacy = 110;
573882cdbd11SMiri Korenblit 			break;
573982cdbd11SMiri Korenblit 		case IWL_RATE_6M_INDEX:
574082cdbd11SMiri Korenblit 			rinfo->legacy = 60;
574182cdbd11SMiri Korenblit 			break;
574282cdbd11SMiri Korenblit 		case IWL_RATE_9M_INDEX:
574382cdbd11SMiri Korenblit 			rinfo->legacy = 90;
574482cdbd11SMiri Korenblit 			break;
574582cdbd11SMiri Korenblit 		case IWL_RATE_12M_INDEX:
574682cdbd11SMiri Korenblit 			rinfo->legacy = 120;
574782cdbd11SMiri Korenblit 			break;
574882cdbd11SMiri Korenblit 		case IWL_RATE_18M_INDEX:
574982cdbd11SMiri Korenblit 			rinfo->legacy = 180;
575082cdbd11SMiri Korenblit 			break;
575182cdbd11SMiri Korenblit 		case IWL_RATE_24M_INDEX:
575282cdbd11SMiri Korenblit 			rinfo->legacy = 240;
575382cdbd11SMiri Korenblit 			break;
575482cdbd11SMiri Korenblit 		case IWL_RATE_36M_INDEX:
575582cdbd11SMiri Korenblit 			rinfo->legacy = 360;
575682cdbd11SMiri Korenblit 			break;
575782cdbd11SMiri Korenblit 		case IWL_RATE_48M_INDEX:
575882cdbd11SMiri Korenblit 			rinfo->legacy = 480;
575982cdbd11SMiri Korenblit 			break;
576082cdbd11SMiri Korenblit 		case IWL_RATE_54M_INDEX:
576182cdbd11SMiri Korenblit 			rinfo->legacy = 540;
576282cdbd11SMiri Korenblit 		}
576382cdbd11SMiri Korenblit 		return;
576482cdbd11SMiri Korenblit 	}
576582cdbd11SMiri Korenblit 
5766ed780545SJohannes Berg 	rinfo->nss = u32_get_bits(rate_n_flags,
576782cdbd11SMiri Korenblit 				  RATE_MCS_NSS_MSK) + 1;
576882cdbd11SMiri Korenblit 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
576982cdbd11SMiri Korenblit 		RATE_HT_MCS_INDEX(rate_n_flags) :
577082cdbd11SMiri Korenblit 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
577182cdbd11SMiri Korenblit 
57727138763eSJohannes Berg 	if (rate_n_flags & RATE_MCS_SGI_MSK)
57737138763eSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
57747138763eSJohannes Berg 
57757138763eSJohannes Berg 	switch (format) {
577623dcee94SJohannes Berg 	case RATE_MCS_EHT_MSK:
577723dcee94SJohannes Berg 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
577823dcee94SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
577923dcee94SJohannes Berg 		break;
57807138763eSJohannes Berg 	case RATE_MCS_HE_MSK:
57817138763eSJohannes Berg 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
5782ed780545SJohannes Berg 
5783ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
5784ed780545SJohannes Berg 
578582cdbd11SMiri Korenblit 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
5786ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
5787ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
5788ed780545SJohannes Berg 		}
5789ed780545SJohannes Berg 
579082cdbd11SMiri Korenblit 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
579182cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_SU:
579282cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_EXT_SU:
5793ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5794ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5795ed780545SJohannes Berg 			else if (gi_ltf == 2)
5796ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
579782cdbd11SMiri Korenblit 			else if (gi_ltf == 3)
5798ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
579982cdbd11SMiri Korenblit 			else
580082cdbd11SMiri Korenblit 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5801ed780545SJohannes Berg 			break;
580282cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_MU:
5803ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5804ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5805ed780545SJohannes Berg 			else if (gi_ltf == 2)
5806ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5807ed780545SJohannes Berg 			else
5808ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5809ed780545SJohannes Berg 			break;
581082cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_TRIG:
5811ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5812ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5813ed780545SJohannes Berg 			else
5814ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5815ed780545SJohannes Berg 			break;
5816ed780545SJohannes Berg 		}
5817ed780545SJohannes Berg 
5818ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
5819ed780545SJohannes Berg 			rinfo->he_dcm = 1;
58207138763eSJohannes Berg 		break;
58217138763eSJohannes Berg 	case RATE_MCS_HT_MSK:
582282cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
58237138763eSJohannes Berg 		break;
58247138763eSJohannes Berg 	case RATE_MCS_VHT_MSK:
582582cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
58267138763eSJohannes Berg 		break;
5827ed780545SJohannes Berg 	}
5828ed780545SJohannes Berg }
5829ed780545SJohannes Berg 
5830cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
5831e705c121SKalle Valo 				struct ieee80211_vif *vif,
5832e705c121SKalle Valo 				struct ieee80211_sta *sta,
5833e705c121SKalle Valo 				struct station_info *sinfo)
5834e705c121SKalle Valo {
5835e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5836e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5837e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5838e705c121SKalle Valo 
5839c8ee33e1SGregory Greenman 	if (mvmsta->deflink.avg_energy) {
5840c8ee33e1SGregory Greenman 		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
584122d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
5842988b5968SSara Sharon 	}
5843988b5968SSara Sharon 
5844ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
5845c8ee33e1SGregory Greenman 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
5846ed780545SJohannes Berg 
5847ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5848ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5849ed780545SJohannes Berg 	}
5850ed780545SJohannes Berg 
5851e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
5852e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5853e705c121SKalle Valo 		return;
5854e705c121SKalle Valo 
5855f276e20bSJohannes Berg 	if (!vif->cfg.assoc)
5856e705c121SKalle Valo 		return;
5857e705c121SKalle Valo 
5858e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5859e705c121SKalle Valo 
5860c8ee33e1SGregory Greenman 	if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
5861e705c121SKalle Valo 		goto unlock;
5862e705c121SKalle Valo 
5863e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
5864e705c121SKalle Valo 		goto unlock;
5865e705c121SKalle Valo 
5866650cadb7SGregory Greenman 	sinfo->rx_beacon = mvmvif->deflink.beacon_stats.num_beacons +
5867650cadb7SGregory Greenman 			   mvmvif->deflink.beacon_stats.accu_num_beacons;
586822d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
5869650cadb7SGregory Greenman 	if (mvmvif->deflink.beacon_stats.avg_signal) {
5870e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
5871650cadb7SGregory Greenman 		sinfo->rx_beacon_signal_avg =
5872650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.avg_signal;
587322d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
5874e705c121SKalle Valo 	}
5875e705c121SKalle Valo  unlock:
5876e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5877e705c121SKalle Valo }
5878e705c121SKalle Valo 
5879119c2a13SMordechay Goodstein static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5880119c2a13SMordechay Goodstein 					    struct ieee80211_vif *vif,
5881119c2a13SMordechay Goodstein 					    const  struct ieee80211_mlme_event *mlme)
5882119c2a13SMordechay Goodstein {
58831a81bddfSMordechay Goodstein 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
58841a81bddfSMordechay Goodstein 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
5885119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5886119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5887119c2a13SMordechay Goodstein 				       NULL);
5888119c2a13SMordechay Goodstein 		return;
5889119c2a13SMordechay Goodstein 	}
5890119c2a13SMordechay Goodstein 
5891119c2a13SMordechay Goodstein 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5892119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5893119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_DEASSOC,
5894119c2a13SMordechay Goodstein 				       NULL);
5895119c2a13SMordechay Goodstein 		return;
5896119c2a13SMordechay Goodstein 	}
5897119c2a13SMordechay Goodstein }
5898119c2a13SMordechay Goodstein 
5899e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5900e705c121SKalle Valo 					struct ieee80211_vif *vif,
5901e705c121SKalle Valo 					const struct ieee80211_event *event)
5902e705c121SKalle Valo {
59034c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5904e705c121SKalle Valo 	do {								\
59054c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5906e705c121SKalle Valo 			break;						\
59077174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5908e705c121SKalle Valo 	} while (0)
5909e705c121SKalle Valo 
5910e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5911e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5912e705c121SKalle Valo 
5913119c2a13SMordechay Goodstein 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5914119c2a13SMordechay Goodstein 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5915119c2a13SMordechay Goodstein 		return;
5916119c2a13SMordechay Goodstein 	}
5917119c2a13SMordechay Goodstein 
59186c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
59196c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
59206c042d75SSara Sharon 	if (!trig)
5921e705c121SKalle Valo 		return;
5922e705c121SKalle Valo 
5923e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
5924e705c121SKalle Valo 
5925e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
5926e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
59274c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
5928e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
5929e705c121SKalle Valo 					    event->u.mlme.reason);
5930e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
59314c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
5932e705c121SKalle Valo 					   "ASSOC TIMEOUT");
5933e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
5934e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
59354c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
5936e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
5937e705c121SKalle Valo 					   event->u.mlme.reason);
5938e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
59394c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
5940e705c121SKalle Valo 					   "AUTH TIMEOUT");
5941e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
59424c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
5943e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
5944e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
59454c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
5946e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
5947e705c121SKalle Valo 	}
5948e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
5949e705c121SKalle Valo }
5950e705c121SKalle Valo 
5951e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5952e705c121SKalle Valo 					  struct ieee80211_vif *vif,
5953e705c121SKalle Valo 					  const struct ieee80211_event *event)
5954e705c121SKalle Valo {
5955e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5956e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
5957e705c121SKalle Valo 
59586c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
59596c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
59606c042d75SSara Sharon 	if (!trig)
5961e705c121SKalle Valo 		return;
5962e705c121SKalle Valo 
5963e705c121SKalle Valo 	ba_trig = (void *)trig->data;
5964e705c121SKalle Valo 
5965e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5966e705c121SKalle Valo 		return;
5967e705c121SKalle Valo 
59687174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
5969e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
5970e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
5971e705c121SKalle Valo 				event->u.ba.ssn);
5972e705c121SKalle Valo }
5973e705c121SKalle Valo 
5974cbce62a3SMiri Korenblit void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
5975e705c121SKalle Valo 				struct ieee80211_vif *vif,
5976e705c121SKalle Valo 				const struct ieee80211_event *event)
5977e705c121SKalle Valo {
5978e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5979e705c121SKalle Valo 
5980e705c121SKalle Valo 	switch (event->type) {
5981e705c121SKalle Valo 	case MLME_EVENT:
5982e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
5983e705c121SKalle Valo 		break;
5984e705c121SKalle Valo 	case BAR_RX_EVENT:
5985e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
5986e705c121SKalle Valo 		break;
5987e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
5988528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
5989528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
5990e705c121SKalle Valo 		break;
5991e705c121SKalle Valo 	default:
5992e705c121SKalle Valo 		break;
5993e705c121SKalle Valo 	}
5994e705c121SKalle Valo }
5995e705c121SKalle Valo 
5996d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
59975e1688ceSJohannes Berg 				     enum iwl_mvm_rxq_notif_type type,
59985e1688ceSJohannes Berg 				     bool sync,
59995e1688ceSJohannes Berg 				     const void *data, u32 size)
60000636b938SSara Sharon {
60015e1688ceSJohannes Berg 	struct {
60025e1688ceSJohannes Berg 		struct iwl_rxq_sync_cmd cmd;
60035e1688ceSJohannes Berg 		struct iwl_mvm_internal_rxq_notif notif;
60045e1688ceSJohannes Berg 	} __packed cmd = {
60055e1688ceSJohannes Berg 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
60065e1688ceSJohannes Berg 		.cmd.count =
60075e1688ceSJohannes Berg 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
60085e1688ceSJohannes Berg 				    size),
60095e1688ceSJohannes Berg 		.notif.type = type,
60105e1688ceSJohannes Berg 		.notif.sync = sync,
60115e1688ceSJohannes Berg 	};
60125e1688ceSJohannes Berg 	struct iwl_host_cmd hcmd = {
60135e1688ceSJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
60145e1688ceSJohannes Berg 		.data[0] = &cmd,
60155e1688ceSJohannes Berg 		.len[0] = sizeof(cmd),
60165e1688ceSJohannes Berg 		.data[1] = data,
60175e1688ceSJohannes Berg 		.len[1] = size,
60185e1688ceSJohannes Berg 		.flags = sync ? 0 : CMD_ASYNC,
60195e1688ceSJohannes Berg 	};
60200636b938SSara Sharon 	int ret;
60210636b938SSara Sharon 
60225e1688ceSJohannes Berg 	/* size must be a multiple of DWORD */
60235e1688ceSJohannes Berg 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
60245e1688ceSJohannes Berg 		return;
60250636b938SSara Sharon 
6026cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
60270636b938SSara Sharon 		return;
60280636b938SSara Sharon 
60295e1688ceSJohannes Berg 	if (sync) {
60305e1688ceSJohannes Berg 		cmd.notif.cookie = mvm->queue_sync_cookie;
60312f7a04c7SJohannes Berg 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6032a2113cc4SNaftali Goldstein 	}
6033d0ff5d22SSara Sharon 
60345e1688ceSJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
60350636b938SSara Sharon 	if (ret) {
60360636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
60370636b938SSara Sharon 		goto out;
60380636b938SSara Sharon 	}
6039d0ff5d22SSara Sharon 
60405e1688ceSJohannes Berg 	if (sync) {
60413c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
60423a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
60432f7a04c7SJohannes Berg 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
60446ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
60450636b938SSara Sharon 					 HZ);
60462f7a04c7SJohannes Berg 		WARN_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm),
60472f7a04c7SJohannes Berg 			  "queue sync: failed to sync, state is 0x%lx\n",
60482f7a04c7SJohannes Berg 			  mvm->queue_sync_state);
60496ad04359SJohannes Berg 	}
60500636b938SSara Sharon 
60510636b938SSara Sharon out:
60525e1688ceSJohannes Berg 	if (sync) {
60532f7a04c7SJohannes Berg 		mvm->queue_sync_state = 0;
60540636b938SSara Sharon 		mvm->queue_sync_cookie++;
60550636b938SSara Sharon 	}
60565f8a3561SJohannes Berg }
60570636b938SSara Sharon 
6058cbce62a3SMiri Korenblit void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
60590636b938SSara Sharon {
60600636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
60610636b938SSara Sharon 
60620636b938SSara Sharon 	mutex_lock(&mvm->mutex);
60635e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
60640636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
60650636b938SSara Sharon }
60660636b938SSara Sharon 
6067cbce62a3SMiri Korenblit int
6068b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6069b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
6070b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
6071b73f9a4aSJohannes Berg {
6072b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6073b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6074b73f9a4aSJohannes Berg 
6075b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6076b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6077b73f9a4aSJohannes Berg 		return -EINVAL;
6078b73f9a4aSJohannes Berg 
6079b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
6080b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
6081b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
6082b73f9a4aSJohannes Berg 
6083b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6084b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6085b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
6086b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
6087b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6088b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6089b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6090b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6091b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6092b73f9a4aSJohannes Berg 
6093b73f9a4aSJohannes Berg 	return 0;
6094b73f9a4aSJohannes Berg }
6095b73f9a4aSJohannes Berg 
6096cbce62a3SMiri Korenblit int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6097fc36ffdaSJohannes Berg 		       struct cfg80211_pmsr_request *request)
6098fc36ffdaSJohannes Berg {
6099fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6100fc36ffdaSJohannes Berg 	int ret;
6101fc36ffdaSJohannes Berg 
6102fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
6103fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
6104fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
6105fc36ffdaSJohannes Berg 
6106fc36ffdaSJohannes Berg 	return ret;
6107fc36ffdaSJohannes Berg }
6108fc36ffdaSJohannes Berg 
6109cbce62a3SMiri Korenblit void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6110fc36ffdaSJohannes Berg 			struct cfg80211_pmsr_request *request)
6111fc36ffdaSJohannes Berg {
6112fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6113fc36ffdaSJohannes Berg 
6114fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
6115fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
6116fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
6117fc36ffdaSJohannes Berg }
6118fc36ffdaSJohannes Berg 
6119438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6120438af969SSara Sharon {
6121438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
6122438af969SSara Sharon 
6123438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
6124438af969SSara Sharon 		return false;
6125438af969SSara Sharon 
6126438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6127438af969SSara Sharon }
6128438af969SSara Sharon 
6129438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6130438af969SSara Sharon 				      struct sk_buff *head,
6131438af969SSara Sharon 				      struct sk_buff *skb)
6132438af969SSara Sharon {
6133438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6134438af969SSara Sharon 
6135a0c8ab93SJohannes Berg 	if (iwl_mvm_has_new_tx_csum(mvm))
61360792df68SJohannes Berg 		return iwl_mvm_tx_csum_bz(mvm, head, true) ==
61370792df68SJohannes Berg 		       iwl_mvm_tx_csum_bz(mvm, skb, true);
61380792df68SJohannes Berg 
6139438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
6140438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
6141438af969SSara Sharon 		return false;
6142438af969SSara Sharon 
6143438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
6144438af969SSara Sharon 		return true;
6145438af969SSara Sharon 
6146438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6147438af969SSara Sharon }
6148438af969SSara Sharon 
6149be8897e2SAvraham Stern int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6150cf85123aSAvraham Stern 			     struct ieee80211_vif *vif,
6151cf85123aSAvraham Stern 			     struct cfg80211_set_hw_timestamp *hwts)
6152cf85123aSAvraham Stern {
6153cf85123aSAvraham Stern 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6154cf85123aSAvraham Stern 	u32 protocols = 0;
6155cf85123aSAvraham Stern 	int ret;
6156cf85123aSAvraham Stern 
6157cf85123aSAvraham Stern 	/* HW timestamping is only supported for a specific station */
6158cf85123aSAvraham Stern 	if (!hwts->macaddr)
6159cf85123aSAvraham Stern 		return -EOPNOTSUPP;
6160cf85123aSAvraham Stern 
6161cf85123aSAvraham Stern 	if (hwts->enable)
6162cf85123aSAvraham Stern 		protocols =
6163cf85123aSAvraham Stern 			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6164cf85123aSAvraham Stern 
6165cf85123aSAvraham Stern 	mutex_lock(&mvm->mutex);
6166cf85123aSAvraham Stern 	ret = iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6167cf85123aSAvraham Stern 	mutex_unlock(&mvm->mutex);
6168cf85123aSAvraham Stern 
6169cf85123aSAvraham Stern 	return ret;
6170cf85123aSAvraham Stern }
6171cf85123aSAvraham Stern 
6172e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
6173e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
6174cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6175e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
6176e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
6177e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
6178e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
6179e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
6180e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
6181e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
6182e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
6183e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
6184e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
6185e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
6186e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
6187e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
6188e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6189e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6190e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
6191e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
6192e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6193e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6194e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6195e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
6196e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
6197e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
61986b1259d1SJohannes Berg 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6199e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6200e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
6201e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
6202e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6203e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
6204e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
6205e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
6206e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
6207e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
6208e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
6209e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
6210e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6211e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6212e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6213e705c121SKalle Valo 
6214ae7ba17bSShaul Triebitz 	.start_ap = iwl_mvm_start_ap,
6215ae7ba17bSShaul Triebitz 	.stop_ap = iwl_mvm_stop_ap,
6216ae7ba17bSShaul Triebitz 	.join_ibss = iwl_mvm_start_ibss,
6217ae7ba17bSShaul Triebitz 	.leave_ibss = iwl_mvm_stop_ibss,
6218e705c121SKalle Valo 
62192f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
62202f0282dbSJohannes Berg 
6221e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
6222e705c121SKalle Valo 
6223e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
6224e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
6225e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
622679221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
6227c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6228e705c121SKalle Valo 
6229e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6230e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6231e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6232e705c121SKalle Valo 
6233e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
6234e705c121SKalle Valo 
62350636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
62360636b938SSara Sharon 
6237e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
6238e705c121SKalle Valo 
6239e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6240e705c121SKalle Valo 	/* look at d3.c */
6241e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
6242e705c121SKalle Valo 	.resume = iwl_mvm_resume,
6243e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
6244e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
6245e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
6246e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6247e705c121SKalle Valo #endif
6248e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6249e705c121SKalle Valo #endif
6250e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
6251e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
6252b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6253fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
6254fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
6255b73f9a4aSJohannes Berg 
6256438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6257177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
6258177a11cfSGregory Greenman 	.sta_add_debugfs = iwl_mvm_sta_add_debugfs,
6259177a11cfSGregory Greenman #endif
6260cf85123aSAvraham Stern 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6261e705c121SKalle Valo };
6262