xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision 3723c7c5f65e09a0f417cc73831faa039a31ecb3)
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);
111e9b63341SAbhishek Naik 	struct iwl_mcc_update_resp_v8 *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),
141e9b63341SAbhishek Naik 				      le32_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  */
2486107f300SEmmanuel Grumbach 
2496107f300SEmmanuel Grumbach #define IWL_MVM_EMLSR_CAPA	(IEEE80211_EML_CAP_EMLSR_SUPP | \
2506107f300SEmmanuel Grumbach 				 IEEE80211_EML_CAP_EMLSR_PADDING_DELAY_32US << \
2516107f300SEmmanuel Grumbach 					__bf_shf(IEEE80211_EML_CAP_EMLSR_PADDING_DELAY) | \
2526107f300SEmmanuel Grumbach 				 IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_64US << \
2536107f300SEmmanuel Grumbach 					__bf_shf(IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY))
2546107f300SEmmanuel Grumbach 
2558e33f046SKrishnanand Prabhu static const struct wiphy_iftype_ext_capab add_iftypes_ext_capa[] = {
2567360f99eSEmmanuel Grumbach 	{
2577360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
2587360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
2597360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
2607360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
2616107f300SEmmanuel Grumbach 		/* relevant only if EHT is supported */
2626107f300SEmmanuel Grumbach 		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
2637360f99eSEmmanuel Grumbach 	},
2648e33f046SKrishnanand Prabhu 	{
2658e33f046SKrishnanand Prabhu 		.iftype = NL80211_IFTYPE_STATION,
2668e33f046SKrishnanand Prabhu 		.extended_capabilities = tm_if_types_ext_capa_sta,
2678e33f046SKrishnanand Prabhu 		.extended_capabilities_mask = tm_if_types_ext_capa_sta,
2688e33f046SKrishnanand Prabhu 		.extended_capabilities_len = sizeof(tm_if_types_ext_capa_sta),
2698e33f046SKrishnanand Prabhu 		/* relevant only if EHT is supported */
2706107f300SEmmanuel Grumbach 		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
2718e33f046SKrishnanand Prabhu 	},
2727360f99eSEmmanuel Grumbach };
2737360f99eSEmmanuel Grumbach 
274cbce62a3SMiri Korenblit int iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
275e8503aecSBen Greear {
276e8503aecSBen Greear 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
277e8503aecSBen Greear 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
278e8503aecSBen Greear 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
279e8503aecSBen Greear 	return 0;
280e8503aecSBen Greear }
281e8503aecSBen Greear 
282e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
283e705c121SKalle Valo {
284e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
285e705c121SKalle Valo 	int num_mac, ret, i;
286e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
287e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
288e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
289e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
290e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
291e705c121SKalle Valo 	};
2923f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
2933f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
2943f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2953f37c229SIdo Yariv #endif
296a5de7de7SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
297a5de7de7SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
298e705c121SKalle Valo 
299e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
300e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
301e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
302e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
303e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
304e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
305e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
306e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
307e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
308e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
309e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
310e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
311909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
31230433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
313520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
314cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
315cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
316ef2b47b8SJohannes Berg 
3170120e6b3SGregory Greenman 	/* Set this early since we need to have it for the check below */
318b3366330SJohannes Berg 	if (mvm->mld_api_is_used && mvm->nvm_data->sku_cap_11be_enable &&
319b3366330SJohannes Berg 	    !iwlwifi_mod_params.disable_11ax &&
320b3366330SJohannes Berg 	    !iwlwifi_mod_params.disable_11be)
3210120e6b3SGregory Greenman 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
3220120e6b3SGregory Greenman 
323ef2b47b8SJohannes Berg 	/* With MLD FW API, it tracks timing by itself,
324ef2b47b8SJohannes Berg 	 * no need for any timing from the host
325ef2b47b8SJohannes Berg 	 */
326ef2b47b8SJohannes Berg 	if (!mvm->mld_api_is_used)
327ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
328ef2b47b8SJohannes Berg 
329ef2b47b8SJohannes Berg 	/* We should probably have this, but mac80211
330ef2b47b8SJohannes Berg 	 * currently doesn't support it for MLO.
331ef2b47b8SJohannes Berg 	 */
332ef2b47b8SJohannes Berg 	if (!(hw->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
333ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
334ef2b47b8SJohannes Berg 
335cfb21b11SJohannes Berg 	/*
336cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
337cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
338cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
339cfb21b11SJohannes Berg 	 * assert EDC.
340cfb21b11SJohannes Berg 	 *
341cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
342cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
343cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
344cfb21b11SJohannes Berg 	 * devices.
345cfb21b11SJohannes Berg 	 */
3467d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
347438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
348438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
349ecaf71deSGregory Greenman 
3504243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
351ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
352ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
353ecaf71deSGregory Greenman 	}
354ecaf71deSGregory Greenman 
355b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
356b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
357960f864bSJohannes Berg 
358960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
359960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
36065e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
361960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
362960f864bSJohannes Berg 		/*
363960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
364960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
365960f864bSJohannes Berg 		 * for station powersave
366960f864bSJohannes Berg 		 */
367960f864bSJohannes Berg 		return -EINVAL;
368960f864bSJohannes Berg 	}
369e705c121SKalle Valo 
37080938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
37180938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
37280938abcSJohannes Berg 
3732d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
3742d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
3752d7cf549SJohannes Berg 
376366fc672SJohannes Berg 	hw->queues = IEEE80211_NUM_ACS;
377e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
378e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
379e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
380e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
381e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
382371a17edSJohannes Berg 
383371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
384371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
385371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
386371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
387371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
388371a17edSJohannes Berg 
3894243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
3909f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
3919f66a397SGregory Greenman 
392e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
393e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
394438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
395e705c121SKalle Valo 
3968e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
397e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
398e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
399e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
400e705c121SKalle Valo 
4012a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4022a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4032a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
4042a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4052a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4062a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
4072a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4082a53d166SAyala Beker 	}
4092a53d166SAyala Beker 
410f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
411f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
412f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
413f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
414f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
415f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
416f4bfdc5eSEmmanuel Grumbach 
417e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
418f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
419e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
4208e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4218e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4228e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
4238e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4248e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4258e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
4268e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4278e160ab8SAyala Beker 	}
428e705c121SKalle Valo 
429ef2e7a51SIlan Peer 	wiphy_ext_feature_set(hw->wiphy,
430ef2e7a51SIlan Peer 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
431d16b96b5SJohannes Berg 	wiphy_ext_feature_set(hw->wiphy,
432d16b96b5SJohannes Berg 			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
433ef2e7a51SIlan Peer 
434b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
435fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
436b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
437b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
438fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
439fc36ffdaSJohannes Berg 	}
440b73f9a4aSJohannes Berg 
441a5de7de7SJohannes Berg 	if (sec_key_ver &&
442a5de7de7SJohannes Berg 	    fw_has_capa(&mvm->fw->ucode_capa,
443a5de7de7SJohannes Berg 			IWL_UCODE_TLV_CAPA_BIGTK_TX_SUPPORT))
444a5de7de7SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
445a5de7de7SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION);
446a5de7de7SJohannes Berg 	else if (fw_has_capa(&mvm->fw->ucode_capa,
447b1fdc250SJohannes Berg 			     IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
448b1fdc250SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
449b1fdc250SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
450b1fdc250SJohannes Berg 
451cf85123aSAvraham Stern 	if (fw_has_capa(&mvm->fw->ucode_capa,
452cf85123aSAvraham Stern 			IWL_UCODE_TLV_CAPA_TIME_SYNC_BOTH_FTM_TM))
453cf85123aSAvraham Stern 		hw->wiphy->hw_timestamp_max_peers = 1;
454cf85123aSAvraham Stern 
455e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
456e705c121SKalle Valo 	hw->wiphy->features |=
457e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
458e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
459e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
460e705c121SKalle Valo 
461e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
462e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
463e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
464cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
465e705c121SKalle Valo 
466e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
467e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
468e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
469e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
470e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
471e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
472e705c121SKalle Valo 
473e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
474e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
47533e3fd99SAlexander Wetzel 
47633e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
47733e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
47833e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
47933e3fd99SAlexander Wetzel 	 */
48033e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
48133e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
48233e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
483e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
484e47df5bdSJohannes Berg 
485e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
486e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
487e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
488e705c121SKalle Valo 	else
489e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
490e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
491e705c121SKalle Valo 
492e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
493e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
494eae94cf8SLuca Coelho 	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
495e705c121SKalle Valo 
496e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
497e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
498e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
499e705c121SKalle Valo 
500e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
501e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
502e705c121SKalle Valo 
503e705c121SKalle Valo 	/* Extract MAC address */
504e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
505e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
506e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
507e705c121SKalle Valo 
508e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
509e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
510e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
511e705c121SKalle Valo 
512e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
513e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
514e705c121SKalle Valo 		       ETH_ALEN);
515e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
516e705c121SKalle Valo 		hw->wiphy->n_addresses++;
517e705c121SKalle Valo 	}
518e705c121SKalle Valo 
519e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
520e705c121SKalle Valo 
521e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
522e705c121SKalle Valo 
523e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
524e705c121SKalle Valo 
525e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
526e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
527e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
528e705c121SKalle Valo 
529e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
530e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
531e705c121SKalle Valo 	else
532e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
533e705c121SKalle Valo 
53457fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
53557fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
53657fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
53757fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
53857fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
53957fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
540e705c121SKalle Valo 
541e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
542e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
543e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
544e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
54557fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
546e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
547e705c121SKalle Valo 	}
548eae94cf8SLuca Coelho 	if (fw_has_capa(&mvm->fw->ucode_capa,
549eae94cf8SLuca Coelho 			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
550eae94cf8SLuca Coelho 	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
551eae94cf8SLuca Coelho 		hw->wiphy->bands[NL80211_BAND_6GHZ] =
552eae94cf8SLuca Coelho 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
553e705c121SKalle Valo 
554e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
555e705c121SKalle Valo 
556e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
557e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
558e705c121SKalle Valo 	else
559e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
560e705c121SKalle Valo 
561ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
562e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
5635d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
564e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
565e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
566e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
567e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
568e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
569e705c121SKalle Valo 
570e705c121SKalle Valo 	/*
571e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
572e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
573e705c121SKalle Valo 	 */
574e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
575e705c121SKalle Valo 
576e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
577e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
578e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
579a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
580e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
581e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
582e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
583e705c121SKalle Valo 
584e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
585e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
586e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
587e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
588e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
589e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
590e705c121SKalle Valo 
591e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
592e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
593e705c121SKalle Valo 		hw->wiphy->features |=
594e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
595e705c121SKalle Valo 
596e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
597e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
598e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
599e705c121SKalle Valo 
600971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
6012a42aea7SNathan Errera 				  IWL_FW_CMD_VER_UNKNOWN) == 3)
6022a42aea7SNathan Errera 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
6032a42aea7SNathan Errera 
604aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
605aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
606aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
607aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
608aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
609aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
610aacf8f18SAvrahams Stern 	}
611aacf8f18SAvrahams Stern 
6128f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
613971cbe50SJohannes Berg 		u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
614773a042fSAvraham Stern 
6158f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6168f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
6178f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6188f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
6198f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6208f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
621773a042fSAvraham Stern 
622773a042fSAvraham Stern 		/* Old firmware also supports probe deferral and suppression */
623773a042fSAvraham Stern 		if (scan_ver < 15)
624773a042fSAvraham Stern 			wiphy_ext_feature_set(hw->wiphy,
625773a042fSAvraham Stern 					      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
6268f691af9SZamir, Roee 	}
6278f691af9SZamir, Roee 
6288e33f046SKrishnanand Prabhu 	hw->wiphy->iftype_ext_capab = NULL;
6298e33f046SKrishnanand Prabhu 	hw->wiphy->num_iftype_ext_capab = 0;
6308e33f046SKrishnanand Prabhu 
6317360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
6327360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
6338e33f046SKrishnanand Prabhu 		hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa;
6347360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
6358e33f046SKrishnanand Prabhu 			ARRAY_SIZE(add_iftypes_ext_capa) - 1;
636918cbf39SSara Sharon 
637918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
638918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
6397360f99eSEmmanuel Grumbach 	}
6407360f99eSEmmanuel Grumbach 
6418e33f046SKrishnanand Prabhu 	if (iwl_fw_lookup_cmd_ver(mvm->fw,
6428e33f046SKrishnanand Prabhu 				  WIDE_ID(DATA_PATH_GROUP,
6438e33f046SKrishnanand Prabhu 					  WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
6448e33f046SKrishnanand Prabhu 				  IWL_FW_CMD_VER_UNKNOWN) >= 1) {
6458e33f046SKrishnanand Prabhu 		IWL_DEBUG_INFO(mvm->trans, "Timing measurement supported\n");
6468e33f046SKrishnanand Prabhu 
6478e33f046SKrishnanand Prabhu 		if (!hw->wiphy->iftype_ext_capab) {
6488e33f046SKrishnanand Prabhu 			hw->wiphy->num_iftype_ext_capab = 1;
6498e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa +
6508e33f046SKrishnanand Prabhu 				ARRAY_SIZE(add_iftypes_ext_capa) - 1;
6518e33f046SKrishnanand Prabhu 		} else {
6528e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa + 1;
6538e33f046SKrishnanand Prabhu 		}
6548e33f046SKrishnanand Prabhu 	}
6558e33f046SKrishnanand Prabhu 
656e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
657e705c121SKalle Valo 
658e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6593f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
660e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
661e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
662e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
663e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
664e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
665e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
666e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
667e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
668e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
669e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
670e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
671e705c121SKalle Valo 
672e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
673e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
674e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
6755d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
6765d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
677e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
678e705c121SKalle Valo 	}
679e705c121SKalle Valo #endif
680e705c121SKalle Valo 
681e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
682e705c121SKalle Valo 	if (ret)
683e705c121SKalle Valo 		return ret;
684e705c121SKalle Valo 
685e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
686e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
687e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
688e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
689e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
690e705c121SKalle Valo 	}
691e705c121SKalle Valo 
692e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
693e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
694e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
695e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
696e705c121SKalle Valo 	}
697e705c121SKalle Valo 
698e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
69959fa61f3SEmmanuel Grumbach 	if (!iwl_mvm_is_csum_supported(mvm))
7006772aab7SJohannes Berg 		hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
70141837ca9SEmmanuel Grumbach 
70291b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
70391b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
70491b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
70591b08c2dSAviya Erenfeld 
7069c11d8a9SShaul Triebitz 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
7079c11d8a9SShaul Triebitz 		wiphy_ext_feature_set(hw->wiphy,
7089c11d8a9SShaul Triebitz 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
7099c11d8a9SShaul Triebitz 
710bfcfdb59SEmmanuel Grumbach 	iwl_mvm_vendor_cmds_register(mvm);
711bfcfdb59SEmmanuel Grumbach 
712e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
713e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
714e8503aecSBen Greear 
715de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
716de645e89SJohannes Berg 	if (ret) {
717de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
718e705c121SKalle Valo 	}
719e705c121SKalle Valo 
720e705c121SKalle Valo 	return ret;
721e705c121SKalle Valo }
722e705c121SKalle Valo 
723df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
724df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
725df2378abSJohannes Berg {
726df2378abSJohannes Berg 	if (likely(sta)) {
727df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
728df2378abSJohannes Berg 			return;
729df2378abSJohannes Berg 	} else {
730df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
731df2378abSJohannes Berg 			return;
732df2378abSJohannes Berg 	}
733df2378abSJohannes Berg 
734df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
735df2378abSJohannes Berg }
736df2378abSJohannes Berg 
737cbce62a3SMiri Korenblit void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
738cbce62a3SMiri Korenblit 		    struct ieee80211_tx_control *control, struct sk_buff *skb)
739e705c121SKalle Valo {
740e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
741e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
742e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
743e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
744cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
745cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
74690723da6SShaul Triebitz 	u32 link_id = u32_get_bits(info->control.flags,
74790723da6SShaul Triebitz 				   IEEE80211_TX_CTRL_MLO_LINK);
74890723da6SShaul Triebitz 	struct ieee80211_sta *tmp_sta = sta;
749e705c121SKalle Valo 
750e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
751e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
752e705c121SKalle Valo 		goto drop;
753e705c121SKalle Valo 	}
754e705c121SKalle Valo 
755cfbc6c4cSSara Sharon 	if (offchannel &&
756e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
757e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
758e705c121SKalle Valo 		goto drop;
759e705c121SKalle Valo 
76010516783SJohannes Berg 	/*
76110516783SJohannes Berg 	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
76210516783SJohannes Berg 	 * so we treat the others as broadcast
76310516783SJohannes Berg 	 */
76410516783SJohannes Berg 	if (ieee80211_is_mgmt(hdr->frame_control))
765e705c121SKalle Valo 		sta = NULL;
766e705c121SKalle Valo 
767dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
768cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
769cfbc6c4cSSara Sharon 	    !offchannel) {
770dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
771dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
772650cadb7SGregory Greenman 		u8 ap_sta_id = READ_ONCE(mvmvif->deflink.ap_sta_id);
773dc1aca22SAndrei Otcheretianski 
774be9ae34eSNathan Errera 		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
775dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
776dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
777dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
778dc1aca22SAndrei Otcheretianski 				goto drop;
779dc1aca22SAndrei Otcheretianski 		}
780dc1aca22SAndrei Otcheretianski 	}
781dc1aca22SAndrei Otcheretianski 
78290723da6SShaul Triebitz 	if (tmp_sta && !sta && link_id != IEEE80211_LINK_UNSPECIFIED &&
78390723da6SShaul Triebitz 	    !ieee80211_is_probe_resp(hdr->frame_control)) {
78490723da6SShaul Triebitz 		/* translate MLD addresses to LINK addresses */
78590723da6SShaul Triebitz 		struct ieee80211_link_sta *link_sta =
78690723da6SShaul Triebitz 			rcu_dereference(tmp_sta->link[link_id]);
78790723da6SShaul Triebitz 		struct ieee80211_bss_conf *link_conf =
78890723da6SShaul Triebitz 			rcu_dereference(info->control.vif->link_conf[link_id]);
78990723da6SShaul Triebitz 		struct ieee80211_mgmt *mgmt;
79090723da6SShaul Triebitz 
79190723da6SShaul Triebitz 		if (WARN_ON(!link_sta || !link_conf))
79290723da6SShaul Triebitz 			goto drop;
79390723da6SShaul Triebitz 
79490723da6SShaul Triebitz 		/* if sta is NULL, the frame is a management frame */
79590723da6SShaul Triebitz 		mgmt = (void *)hdr;
79690723da6SShaul Triebitz 		memcpy(mgmt->da, link_sta->addr, ETH_ALEN);
79790723da6SShaul Triebitz 		memcpy(mgmt->sa, link_conf->addr, ETH_ALEN);
79890723da6SShaul Triebitz 		memcpy(mgmt->bssid, link_conf->bssid, ETH_ALEN);
79990723da6SShaul Triebitz 	}
80090723da6SShaul Triebitz 
801df2378abSJohannes Berg 	iwl_mvm_tx_skb(mvm, skb, sta);
802e705c121SKalle Valo 	return;
803e705c121SKalle Valo  drop:
804e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
805e705c121SKalle Valo }
806e705c121SKalle Valo 
807cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
808e705c121SKalle Valo {
809cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
810cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
811cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
812cfbc6c4cSSara Sharon 
813fba8248eSSara Sharon 	/*
814fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
815fba8248eSSara Sharon 	 * taker all the work.
816fba8248eSSara Sharon 	 *
817fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
818fba8248eSSara Sharon 	 *
819fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
820fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
821fba8248eSSara Sharon 	 *
822fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
823fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
824fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
825fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
826fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
827fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
828fba8248eSSara Sharon 	 *
829fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
830fba8248eSSara Sharon 	 * check the queue, so do nothing.
831fba8248eSSara Sharon 	 */
832fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
833fba8248eSSara Sharon 		return;
834cfbc6c4cSSara Sharon 
835cfbc6c4cSSara Sharon 	rcu_read_lock();
836fba8248eSSara Sharon 	do {
837b58e3d43SJohannes Berg 		while (likely(!test_bit(IWL_MVM_TXQ_STATE_STOP_FULL,
838b58e3d43SJohannes Berg 					&mvmtxq->state) &&
839b58e3d43SJohannes Berg 			      !test_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT,
840b58e3d43SJohannes Berg 					&mvmtxq->state) &&
84100520b7aSEmmanuel Grumbach 			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
842cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
843cfbc6c4cSSara Sharon 
844f50d693bSSara Sharon 			if (!skb) {
845f50d693bSSara Sharon 				if (txq->sta)
846f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
847f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
848f50d693bSSara Sharon 						     txq->sta->addr,
849f50d693bSSara Sharon 						     txq->tid);
850cfbc6c4cSSara Sharon 				break;
851f50d693bSSara Sharon 			}
852cfbc6c4cSSara Sharon 
853cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
854cfbc6c4cSSara Sharon 		}
855fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
856cfbc6c4cSSara Sharon 	rcu_read_unlock();
857e705c121SKalle Valo }
858e705c121SKalle Valo 
859cbce62a3SMiri Korenblit void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
860cfbc6c4cSSara Sharon 			       struct ieee80211_txq *txq)
861e705c121SKalle Valo {
862cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
863cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
864e705c121SKalle Valo 
865923bf981SJohannes Berg 	if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
866923bf981SJohannes Berg 	    !txq->sta) {
867cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
868cfbc6c4cSSara Sharon 		return;
869cfbc6c4cSSara Sharon 	}
870cfbc6c4cSSara Sharon 
871923bf981SJohannes Berg 	/* iwl_mvm_mac_itxq_xmit() will later be called by the worker
872923bf981SJohannes Berg 	 * to handle any packets we leave on the txq now
873923bf981SJohannes Berg 	 */
874cfbc6c4cSSara Sharon 
875923bf981SJohannes Berg 	spin_lock_bh(&mvm->add_stream_lock);
876923bf981SJohannes Berg 	/* The list is being deleted only after the queue is fully allocated. */
877923bf981SJohannes Berg 	if (list_empty(&mvmtxq->list) &&
878923bf981SJohannes Berg 	    /* recheck under lock */
879923bf981SJohannes Berg 	    !test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) {
880cfbc6c4cSSara Sharon 		list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
881cfbc6c4cSSara Sharon 		schedule_work(&mvm->add_stream_wk);
882e705c121SKalle Valo 	}
883923bf981SJohannes Berg 	spin_unlock_bh(&mvm->add_stream_lock);
884923bf981SJohannes Berg }
885e705c121SKalle Valo 
886e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
887e705c121SKalle Valo 	do {								\
888e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
889e705c121SKalle Valo 			break;						\
8907174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
891e705c121SKalle Valo 	} while (0)
892e705c121SKalle Valo 
893e705c121SKalle Valo static void
894e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
895e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
896e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
897e705c121SKalle Valo {
898e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
899e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
900e705c121SKalle Valo 
9016c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
9026c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
9036c042d75SSara Sharon 	if (!trig)
904e705c121SKalle Valo 		return;
905e705c121SKalle Valo 
906e705c121SKalle Valo 	ba_trig = (void *)trig->data;
907e705c121SKalle Valo 
908e705c121SKalle Valo 	switch (action) {
909e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
910e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
911e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
912e705c121SKalle Valo 
913e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
914e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
915e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
916e705c121SKalle Valo 		break;
917e705c121SKalle Valo 		}
918e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
919e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
920e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
921e705c121SKalle Valo 				 sta->addr, tid);
922e705c121SKalle Valo 		break;
923e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
924e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
925e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
926e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
927e705c121SKalle Valo 		break;
928e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
929e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
930e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
931e705c121SKalle Valo 				 sta->addr, tid);
932e705c121SKalle Valo 		break;
933e705c121SKalle Valo 	default:
934e705c121SKalle Valo 		break;
935e705c121SKalle Valo 	}
936e705c121SKalle Valo }
937e705c121SKalle Valo 
938cbce62a3SMiri Korenblit int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
939e705c121SKalle Valo 			     struct ieee80211_vif *vif,
94050ea05efSSara Sharon 			     struct ieee80211_ampdu_params *params)
941e705c121SKalle Valo {
942e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
943e705c121SKalle Valo 	int ret;
94450ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
94550ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
94650ea05efSSara Sharon 	u16 tid = params->tid;
94750ea05efSSara Sharon 	u16 *ssn = &params->ssn;
948514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
949bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
95010b2b201SSara Sharon 	u16 timeout = params->timeout;
951e705c121SKalle Valo 
952e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
953e705c121SKalle Valo 		     sta->addr, tid, action);
954e705c121SKalle Valo 
955e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
956e705c121SKalle Valo 		return -EACCES;
957e705c121SKalle Valo 
958e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
959e705c121SKalle Valo 
960e705c121SKalle Valo 	switch (action) {
961e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
962650cadb7SGregory Greenman 		if (iwl_mvm_vif_from_mac80211(vif)->deflink.ap_sta_id ==
963c8ee33e1SGregory Greenman 		    iwl_mvm_sta_from_mac80211(sta)->deflink.sta_id) {
964b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
965b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
966b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
967b0ffe455SJohannes Berg 
968b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
969b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
970b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
971b0ffe455SJohannes Berg 		}
972e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
973e705c121SKalle Valo 			ret = -EINVAL;
974e705c121SKalle Valo 			break;
975e705c121SKalle Valo 		}
97610b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
97710b2b201SSara Sharon 					 timeout);
978e705c121SKalle Valo 		break;
979e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
98010b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
98110b2b201SSara Sharon 					 timeout);
982e705c121SKalle Valo 		break;
983e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
984e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
985e705c121SKalle Valo 			ret = -EINVAL;
986e705c121SKalle Valo 			break;
987e705c121SKalle Valo 		}
988e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
989e705c121SKalle Valo 		break;
990e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
991e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
992e705c121SKalle Valo 		break;
993e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
994e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
995e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
996e705c121SKalle Valo 		break;
997e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
998bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
999bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
1000e705c121SKalle Valo 		break;
1001e705c121SKalle Valo 	default:
1002e705c121SKalle Valo 		WARN_ON_ONCE(1);
1003e705c121SKalle Valo 		ret = -EINVAL;
1004e705c121SKalle Valo 		break;
1005e705c121SKalle Valo 	}
1006e705c121SKalle Valo 
1007e705c121SKalle Valo 	if (!ret) {
1008e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
1009e705c121SKalle Valo 
1010e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1011e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1012e705c121SKalle Valo 
1013e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1014e705c121SKalle Valo 					    rx_ba_ssn, action);
1015e705c121SKalle Valo 	}
1016e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1017e705c121SKalle Valo 
1018e705c121SKalle Valo 	return ret;
1019e705c121SKalle Valo }
1020e705c121SKalle Valo 
1021e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1022e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1023e705c121SKalle Valo {
1024e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1025e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1026bf976c81SJohannes Berg 	struct iwl_probe_resp_data *probe_data;
102779faae3aSGregory Greenman 	unsigned int link_id;
1028e705c121SKalle Valo 
1029e705c121SKalle Valo 	mvmvif->uploaded = false;
1030e705c121SKalle Valo 
1031e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1032e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1033e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1034e705c121SKalle Valo 
1035e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
103679faae3aSGregory Greenman 
103779faae3aSGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
103879faae3aSGregory Greenman 		mvmvif->link[link_id]->ap_sta_id = IWL_MVM_INVALID_STA;
103979faae3aSGregory Greenman 		mvmvif->link[link_id]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
104079faae3aSGregory Greenman 		mvmvif->link[link_id]->phy_ctxt = NULL;
1041bf976c81SJohannes Berg 		mvmvif->link[link_id]->active = 0;
1042d615ea32SJohannes Berg 		mvmvif->link[link_id]->igtk = NULL;
104379faae3aSGregory Greenman 	}
1044bf976c81SJohannes Berg 
1045bf976c81SJohannes Berg 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1046bf976c81SJohannes Berg 					       lockdep_is_held(&mvm->mutex));
1047bf976c81SJohannes Berg 	if (probe_data)
1048bf976c81SJohannes Berg 		kfree_rcu(probe_data, rcu_head);
1049bf976c81SJohannes Berg 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1050e705c121SKalle Valo }
1051e705c121SKalle Valo 
1052e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1053e705c121SKalle Valo {
1054fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1055e705c121SKalle Valo 
10569bf13beeSJohannes Berg 	mvm->cur_aid = 0;
10579bf13beeSJohannes Berg 
1058e705c121SKalle Valo 	mvm->scan_status = 0;
1059e705c121SKalle Valo 	mvm->ps_disabled = false;
1060b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1061e705c121SKalle Valo 
1062e705c121SKalle Valo 	/* just in case one was running */
1063305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1064e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1065e705c121SKalle Valo 
1066fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1067fc36ffdaSJohannes Berg 
1068e705c121SKalle Valo 	/*
1069e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1070e705c121SKalle Valo 	 * gone down during the HW restart
1071e705c121SKalle Valo 	 */
1072e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1073e705c121SKalle Valo 
1074e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1075e705c121SKalle Valo 
1076e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
10779c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1078e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1079e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1080e705c121SKalle Valo 
1081e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1082e705c121SKalle Valo 
1083e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
10847174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1085baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1086fb40cd9dSMiri Korenblit #ifdef CONFIG_IWLWIFI_DEBUGFS
1087fb40cd9dSMiri Korenblit 	mvm->beacon_inject_active = false;
1088fb40cd9dSMiri Korenblit #endif
1089e705c121SKalle Valo 
1090e705c121SKalle Valo 	/* keep statistics ticking */
1091e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1092e705c121SKalle Valo }
1093e705c121SKalle Valo 
1094e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1095e705c121SKalle Valo {
1096e705c121SKalle Valo 	int ret;
1097e705c121SKalle Valo 
1098e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1099e705c121SKalle Valo 
11006d19a5ebSEmmanuel Grumbach 	ret = iwl_mvm_mei_get_ownership(mvm);
11016d19a5ebSEmmanuel Grumbach 	if (ret)
11026d19a5ebSEmmanuel Grumbach 		return ret;
11036d19a5ebSEmmanuel Grumbach 
11046d19a5ebSEmmanuel Grumbach 	if (mvm->mei_nvm_data) {
11056d19a5ebSEmmanuel Grumbach 		/* We got the NIC, we can now free the MEI NVM data */
11066d19a5ebSEmmanuel Grumbach 		kfree(mvm->mei_nvm_data);
11076d19a5ebSEmmanuel Grumbach 		mvm->mei_nvm_data = NULL;
11086d19a5ebSEmmanuel Grumbach 
11096d19a5ebSEmmanuel Grumbach 		/*
11106d19a5ebSEmmanuel Grumbach 		 * We can't free the nvm_data we allocated based on the SAP
11116d19a5ebSEmmanuel Grumbach 		 * data because we registered to cfg80211 with the channels
11126d19a5ebSEmmanuel Grumbach 		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
11136d19a5ebSEmmanuel Grumbach 		 * just in order to be able free it later.
11146d19a5ebSEmmanuel Grumbach 		 * NULLify nvm_data so that we will read the NVM from the
11156d19a5ebSEmmanuel Grumbach 		 * firmware this time.
11166d19a5ebSEmmanuel Grumbach 		 */
11176d19a5ebSEmmanuel Grumbach 		mvm->temp_nvm_data = mvm->nvm_data;
11186d19a5ebSEmmanuel Grumbach 		mvm->nvm_data = NULL;
11196d19a5ebSEmmanuel Grumbach 	}
11206d19a5ebSEmmanuel Grumbach 
1121bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1122bf8b286fSJohannes Berg 		/*
1123bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1124bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1125bf8b286fSJohannes Berg 		 */
1126bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1127bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1128e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1129e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1130a42b2af3SLuca Coelho 	}
1131e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1132e705c121SKalle Valo 
1133b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1134b108d8c7SShahar S Matityahu 			       NULL);
1135b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1136b108d8c7SShahar S Matityahu 			       NULL);
1137da2eb669SSara Sharon 
1138e8fe3b41SIlan Peer 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1139e8fe3b41SIlan Peer 
1140e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1141e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1142e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1143e705c121SKalle Valo 		 * would do.
1144e705c121SKalle Valo 		 */
1145e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1146e705c121SKalle Valo 	}
1147e705c121SKalle Valo 
1148e705c121SKalle Valo 	return ret;
1149e705c121SKalle Valo }
1150e705c121SKalle Valo 
1151cbce62a3SMiri Korenblit int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1152e705c121SKalle Valo {
1153e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1154e705c121SKalle Valo 	int ret;
11555283dd67SMordechay Goodstein 	int retry, max_retry = 0;
1156e705c121SKalle Valo 
1157e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
11585283dd67SMordechay Goodstein 
11595283dd67SMordechay Goodstein 	/* we are starting the mac not in error flow, and restart is enabled */
11605283dd67SMordechay Goodstein 	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
11615283dd67SMordechay Goodstein 	    iwlwifi_mod_params.fw_restart) {
11625283dd67SMordechay Goodstein 		max_retry = IWL_MAX_INIT_RETRY;
11635283dd67SMordechay Goodstein 		/*
11645283dd67SMordechay Goodstein 		 * This will prevent mac80211 recovery flows to trigger during
11655283dd67SMordechay Goodstein 		 * init failures
11665283dd67SMordechay Goodstein 		 */
11675283dd67SMordechay Goodstein 		set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
11685283dd67SMordechay Goodstein 	}
11695283dd67SMordechay Goodstein 
11705283dd67SMordechay Goodstein 	for (retry = 0; retry <= max_retry; retry++) {
1171e705c121SKalle Valo 		ret = __iwl_mvm_mac_start(mvm);
11725283dd67SMordechay Goodstein 		if (!ret)
11735283dd67SMordechay Goodstein 			break;
11745283dd67SMordechay Goodstein 
1175b8133439SAvraham Stern 		/*
1176b8133439SAvraham Stern 		 * In PLDR sync PCI re-enumeration is needed. no point to retry
1177b8133439SAvraham Stern 		 * mac start before that.
1178b8133439SAvraham Stern 		 */
1179b8133439SAvraham Stern 		if (mvm->pldr_sync) {
1180b8133439SAvraham Stern 			iwl_mei_alive_notif(false);
1181b8133439SAvraham Stern 			iwl_trans_pcie_remove(mvm->trans, true);
1182b8133439SAvraham Stern 			break;
1183b8133439SAvraham Stern 		}
1184b8133439SAvraham Stern 
11855283dd67SMordechay Goodstein 		IWL_ERR(mvm, "mac start retry %d\n", retry);
11865283dd67SMordechay Goodstein 	}
11875283dd67SMordechay Goodstein 	clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
11885283dd67SMordechay Goodstein 
1189e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1190e705c121SKalle Valo 
11917ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
11927ce1f215SEmmanuel Grumbach 
1193e705c121SKalle Valo 	return ret;
1194e705c121SKalle Valo }
1195e705c121SKalle Valo 
1196e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1197e705c121SKalle Valo {
1198e705c121SKalle Valo 	int ret;
1199e705c121SKalle Valo 
1200e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1201e705c121SKalle Valo 
1202e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
12034d4183c4SEmmanuel Grumbach 
1204e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1205e705c121SKalle Valo 	if (ret)
1206e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1207e705c121SKalle Valo 			ret);
1208e705c121SKalle Valo 
1209f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1210f130bb75SMordechay Goodstein 
1211e705c121SKalle Valo 	/*
1212e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1213e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1214e705c121SKalle Valo 	 */
1215e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1216e705c121SKalle Valo 
1217e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1218e705c121SKalle Valo }
1219e705c121SKalle Valo 
1220cbce62a3SMiri Korenblit void iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1221e705c121SKalle Valo 				   enum ieee80211_reconfig_type reconfig_type)
1222e705c121SKalle Valo {
1223e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1224e705c121SKalle Valo 
1225e705c121SKalle Valo 	switch (reconfig_type) {
1226e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1227e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1228e705c121SKalle Valo 		break;
1229e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1230e705c121SKalle Valo 		break;
1231e705c121SKalle Valo 	}
1232e705c121SKalle Valo }
1233e705c121SKalle Valo 
1234e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1235e705c121SKalle Valo {
1236e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1237e705c121SKalle Valo 
1238b68bd2e3SIlan Peer 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1239b68bd2e3SIlan Peer 
1240e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1241e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1242e705c121SKalle Valo 	 */
1243e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1244e705c121SKalle Valo 
1245e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1246e705c121SKalle Valo 
12471724fc78SEmmanuel Grumbach 	if (!iwl_mvm_has_new_station_api(mvm->fw))
1248f327236dSSharon 		iwl_mvm_rm_aux_sta(mvm);
1249f327236dSSharon 
1250fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1251e705c121SKalle Valo 
1252e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1253e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1254e705c121SKalle Valo 
1255e705c121SKalle Valo 	/*
1256155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1257155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1258155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1259e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1260e705c121SKalle Valo 	 * HW restart was requested.
1261e705c121SKalle Valo 	 */
1262155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1263155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1264155f7e04SEmmanuel Grumbach 			       &mvm->status))
1265e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1266e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1267e705c121SKalle Valo 
1268e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1269e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1270e705c121SKalle Valo 	 */
1271e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1272e705c121SKalle Valo 		int i;
1273e705c121SKalle Valo 
1274e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1275e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1276e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1277e705c121SKalle Valo 				      i))
1278e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1279e705c121SKalle Valo 		}
1280e705c121SKalle Valo 	}
1281e705c121SKalle Valo }
1282e705c121SKalle Valo 
1283cbce62a3SMiri Korenblit void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1284e705c121SKalle Valo {
1285e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1286e705c121SKalle Valo 
1287e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
128824afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1289771147b0SJohannes Berg 
1290771147b0SJohannes Berg 	/*
1291771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1292771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1293771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
12947174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1295771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1296771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1297771147b0SJohannes Berg 	 */
1298771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1299771147b0SJohannes Berg 
1300d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
130169e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1302e705c121SKalle Valo 
1303f9084775SNathan Errera 	/*
1304f9084775SNathan Errera 	 * The work item could be running or queued if the
1305f9084775SNathan Errera 	 * ROC time event stops just as we get here.
1306f9084775SNathan Errera 	 */
1307f9084775SNathan Errera 	flush_work(&mvm->roc_done_wk);
1308f9084775SNathan Errera 
13097ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
13107ce1f215SEmmanuel Grumbach 
1311e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1312e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1313e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1314e705c121SKalle Valo 
1315e705c121SKalle Valo 	/*
1316e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1317e705c121SKalle Valo 	 * discover that its list is now empty.
1318e705c121SKalle Valo 	 */
1319e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1320e705c121SKalle Valo }
1321e705c121SKalle Valo 
13221ab26632SMiri Korenblit struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1323e705c121SKalle Valo {
1324e705c121SKalle Valo 	u16 i;
1325e705c121SKalle Valo 
1326e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1327e705c121SKalle Valo 
1328e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1329e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1330e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1331e705c121SKalle Valo 
1332e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1333e705c121SKalle Valo 	return NULL;
1334e705c121SKalle Valo }
1335e705c121SKalle Valo 
1336f551d013SGregory Greenman int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1337e705c121SKalle Valo 			 s16 tx_power)
1338e705c121SKalle Valo {
1339971cbe50SJohannes Berg 	u32 cmd_id = REDUCE_TX_POWER_CMD;
13400791c2fcSHaim Dreyfuss 	int len;
1341216cdfb5SLuca Coelho 	struct iwl_dev_tx_power_cmd cmd = {
1342216cdfb5SLuca Coelho 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1343216cdfb5SLuca Coelho 		.common.mac_context_id =
1344e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1345216cdfb5SLuca Coelho 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1346e705c121SKalle Valo 	};
1347971cbe50SJohannes Berg 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1348e80bfd11SMordechay Goodstein 					   IWL_FW_CMD_VER_UNKNOWN);
1349e705c121SKalle Valo 
1350e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1351216cdfb5SLuca Coelho 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1352e705c121SKalle Valo 
1353b0aa02b3SAyala Barazani 	if (cmd_ver == 7)
1354b0aa02b3SAyala Barazani 		len = sizeof(cmd.v7);
1355b0aa02b3SAyala Barazani 	else if (cmd_ver == 6)
1356fbb7957dSLuca Coelho 		len = sizeof(cmd.v6);
1357fbb7957dSLuca Coelho 	else if (fw_has_api(&mvm->fw->ucode_capa,
13580791c2fcSHaim Dreyfuss 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
13590791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
13600791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
13610791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
13620791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
13630791c2fcSHaim Dreyfuss 	else
1364216cdfb5SLuca Coelho 		len = sizeof(cmd.v3);
1365216cdfb5SLuca Coelho 
1366216cdfb5SLuca Coelho 	/* all structs have the same common part, add it */
1367216cdfb5SLuca Coelho 	len += sizeof(cmd.common);
1368e705c121SKalle Valo 
1369971cbe50SJohannes Berg 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1370e705c121SKalle Valo }
1371e705c121SKalle Valo 
137203117f30SMiri Korenblit int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1373a469a593SEmmanuel Grumbach 				struct ieee80211_vif *vif,
1374a469a593SEmmanuel Grumbach 				struct ieee80211_bss_conf *link_conf)
1375f6780614SSara Sharon {
1376f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1377f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1378f6780614SSara Sharon 	int ret;
1379f6780614SSara Sharon 
1380f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1381f6780614SSara Sharon 
1382f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1383f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
1384*3723c7c5SEmmanuel Grumbach 		unsigned int link_id = link_conf->link_id;
1385*3723c7c5SEmmanuel Grumbach 		u8 ap_sta_id = mvmvif->link[link_id]->ap_sta_id;
1386f6780614SSara Sharon 
1387f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
1388*3723c7c5SEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm, ap_sta_id);
1389f6780614SSara Sharon 
1390f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1391f6780614SSara Sharon 			ret = -EIO;
1392f6780614SSara Sharon 			goto out_unlock;
1393f6780614SSara Sharon 		}
1394f6780614SSara Sharon 
1395f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
139603117f30SMiri Korenblit 		if (mvm->mld_api_is_used)
139703117f30SMiri Korenblit 			iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
139803117f30SMiri Korenblit 		else
1399f6780614SSara Sharon 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1400f6780614SSara Sharon 
14010202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
14020202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1403f6780614SSara Sharon 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1404f6780614SSara Sharon 			if (ret)
1405f6780614SSara Sharon 				goto out_unlock;
1406f6780614SSara Sharon 
1407f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1408f6780614SSara Sharon 		}
14090202bcf0SEmmanuel Grumbach 	}
1410f6780614SSara Sharon 
1411f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1412f6780614SSara Sharon 
1413f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1414f6780614SSara Sharon 
1415f6780614SSara Sharon out_unlock:
1416caf46377SSara Sharon 	if (mvmvif->csa_failed)
1417caf46377SSara Sharon 		ret = -EIO;
1418f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1419f6780614SSara Sharon 
1420f6780614SSara Sharon 	return ret;
1421f6780614SSara Sharon }
1422f6780614SSara Sharon 
1423cbce62a3SMiri Korenblit void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1424f6780614SSara Sharon 				  struct ieee80211_vif *vif)
1425f6780614SSara Sharon {
1426f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1427f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1428f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1429f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1430f6780614SSara Sharon 							  mvmvif->color)),
1431f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1432f6780614SSara Sharon 	};
1433f6780614SSara Sharon 
1434ad12b231SNathan Errera 	/*
1435ad12b231SNathan Errera 	 * In the new flow since FW is in charge of the timing,
1436ad12b231SNathan Errera 	 * if driver has canceled the channel switch he will receive the
1437ad12b231SNathan Errera 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1438ad12b231SNathan Errera 	 */
1439ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1440ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1441ad12b231SNathan Errera 		return;
1442ad12b231SNathan Errera 
1443f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1444f6780614SSara Sharon 
1445f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
144658ddd9b6SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
144758ddd9b6SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
144858ddd9b6SEmmanuel Grumbach 		iwl_mvm_remove_csa_period(mvm, vif);
144958ddd9b6SEmmanuel Grumbach 	else
1450f6780614SSara Sharon 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1451f6780614SSara Sharon 					     WIDE_ID(MAC_CONF_GROUP,
1452f6780614SSara Sharon 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1453f6780614SSara Sharon 					     0, sizeof(cmd), &cmd));
1454caf46377SSara Sharon 	mvmvif->csa_failed = true;
1455f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1456f6780614SSara Sharon 
1457a469a593SEmmanuel Grumbach 	/* If we're here, we can't support MLD */
1458a469a593SEmmanuel Grumbach 	iwl_mvm_post_channel_switch(hw, vif, &vif->bss_conf);
1459f6780614SSara Sharon }
1460f6780614SSara Sharon 
14611ab26632SMiri Korenblit void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1462f6780614SSara Sharon {
1463f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1464f6780614SSara Sharon 	struct ieee80211_vif *vif;
1465f6780614SSara Sharon 
1466f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1467f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1468f6780614SSara Sharon 
146970162580SShaul Triebitz 	/* Trigger disconnect (should clear the CSA state) */
1470a469a593SEmmanuel Grumbach 	ieee80211_chswitch_done(vif, false, 0);
1471f6780614SSara Sharon }
1472f6780614SSara Sharon 
14735abf3154SMordechay Goodstein static u8
14745abf3154SMordechay Goodstein iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
14755abf3154SMordechay Goodstein {
14765abf3154SMordechay Goodstein 	int data_start;
14775abf3154SMordechay Goodstein 	int control_start;
14785abf3154SMordechay Goodstein 	int bw;
14795abf3154SMordechay Goodstein 
14805abf3154SMordechay Goodstein 	if (chandef->width == NL80211_CHAN_WIDTH_320)
14815abf3154SMordechay Goodstein 		bw = 320;
14825abf3154SMordechay Goodstein 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
14835abf3154SMordechay Goodstein 		bw = 160;
14845abf3154SMordechay Goodstein 	else
14855abf3154SMordechay Goodstein 		return 0;
14865abf3154SMordechay Goodstein 
14875abf3154SMordechay Goodstein 	/* data is bw wide so the start is half the width */
14885abf3154SMordechay Goodstein 	data_start = chandef->center_freq1 - bw / 2;
14895abf3154SMordechay Goodstein 	/* control is 20Mhz width */
14905abf3154SMordechay Goodstein 	control_start = chandef->chan->center_freq - 10;
14915abf3154SMordechay Goodstein 
14925abf3154SMordechay Goodstein 	return (control_start - data_start) / 80;
14935abf3154SMordechay Goodstein }
14945abf3154SMordechay Goodstein 
14951be4858eSJohannes Berg static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
14961be4858eSJohannes Berg 					 struct ieee80211_vif *vif)
1497e705c121SKalle Valo {
1498e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
14991be4858eSJohannes Berg 	int ret;
15001ab26632SMiri Korenblit 
15011ab26632SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
1502e705c121SKalle Valo 
15031be4858eSJohannes Berg 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
15041be4858eSJohannes Berg 	if (ret) {
15051be4858eSJohannes Berg 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
15061be4858eSJohannes Berg 		return ret;
15071be4858eSJohannes Berg 	}
15081be4858eSJohannes Berg 
15091be4858eSJohannes Berg 	/* Only queue for this station is the mcast queue,
15101be4858eSJohannes Berg 	 * which shouldn't be in TFD mask anyway
15111be4858eSJohannes Berg 	 */
15121be4858eSJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
15131be4858eSJohannes Berg 					vif->type,
15141be4858eSJohannes Berg 					IWL_STA_MULTICAST);
15151be4858eSJohannes Berg }
15161be4858eSJohannes Berg 
15171be4858eSJohannes Berg static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
15181be4858eSJohannes Berg 				     struct ieee80211_vif *vif)
15191be4858eSJohannes Berg {
15201be4858eSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
15211be4858eSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
15221be4858eSJohannes Berg 	int ret;
15231be4858eSJohannes Berg 
15241be4858eSJohannes Berg 	mutex_lock(&mvm->mutex);
15251be4858eSJohannes Berg 
1526e705c121SKalle Valo 	mvmvif->mvm = mvm;
1527650cadb7SGregory Greenman 
1528650cadb7SGregory Greenman 	/* the first link always points to the default one */
1529650cadb7SGregory Greenman 	mvmvif->link[0] = &mvmvif->deflink;
1530e705c121SKalle Valo 
1531e705c121SKalle Valo 	/*
1532e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1533e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1534e705c121SKalle Valo 	 * don't really have to check the types.
1535e705c121SKalle Valo 	 */
1536e705c121SKalle Valo 
1537e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1538e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1539650cadb7SGregory Greenman 		mvmvif->deflink.beacon_stats.accu_num_beacons +=
1540650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.num_beacons;
1541e705c121SKalle Valo 
1542e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
15431be4858eSJohannes Berg 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
15441be4858eSJohannes Berg 	if (ret)
15451be4858eSJohannes Berg 		goto out;
1546e705c121SKalle Valo 
1547698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1548698478c4SSara Sharon 
15491be4858eSJohannes Berg 	/* Currently not much to do for NAN */
15501be4858eSJohannes Berg 	if (vif->type == NL80211_IFTYPE_NAN) {
15511be4858eSJohannes Berg 		ret = 0;
15521be4858eSJohannes Berg 		goto out;
15531be4858eSJohannes Berg 	}
15541be4858eSJohannes Berg 
1555e705c121SKalle Valo 	/*
1556e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1557e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1558e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1559e705c121SKalle Valo 	 * only after the binding.
1560e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1561e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1562e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1563e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1564e705c121SKalle Valo 	 * allocating resources :)
1565e705c121SKalle Valo 	 */
1566e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1567e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
15681ab26632SMiri Korenblit 		iwl_mvm_vif_dbgfs_register(mvm, vif);
15691be4858eSJohannes Berg 		ret = 0;
15701be4858eSJohannes Berg 		goto out;
15711ab26632SMiri Korenblit 	}
15721ab26632SMiri Korenblit 
15731ab26632SMiri Korenblit 	mvmvif->features |= hw->netdev_features;
1574650cadb7SGregory Greenman 
1575e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1576e705c121SKalle Valo 	if (ret)
157798c0de7bSMiri Korenblit 		goto out_unlock;
1578e705c121SKalle Valo 
1579e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1580e705c121SKalle Valo 	if (ret)
1581e705c121SKalle Valo 		goto out_remove_mac;
1582e705c121SKalle Valo 
1583e705c121SKalle Valo 	/* beacon filtering */
1584e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1585e705c121SKalle Valo 	if (ret)
1586e705c121SKalle Valo 		goto out_remove_mac;
1587e705c121SKalle Valo 
1588e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1589e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1590e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1591e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1592e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1593e705c121SKalle Valo 	}
1594e705c121SKalle Valo 
1595e705c121SKalle Valo 	/*
1596e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1597e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1598e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1599e705c121SKalle Valo 	 */
1600e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1601e705c121SKalle Valo 
1602650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1603650cadb7SGregory Greenman 		if (!mvmvif->deflink.phy_ctxt) {
1604e705c121SKalle Valo 			ret = -ENOSPC;
1605e705c121SKalle Valo 			goto out_free_bf;
1606e705c121SKalle Valo 		}
1607e705c121SKalle Valo 
1608650cadb7SGregory Greenman 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
1609e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1610e705c121SKalle Valo 		if (ret)
1611e705c121SKalle Valo 			goto out_unref_phy;
1612e705c121SKalle Valo 
1613d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1614e705c121SKalle Valo 		if (ret)
1615e705c121SKalle Valo 			goto out_unbind;
1616e705c121SKalle Valo 
1617e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1618e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1619e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1620e705c121SKalle Valo 	}
1621e705c121SKalle Valo 
1622b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1623f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1624f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1625b0ffe455SJohannes Berg 
16265abf3154SMordechay Goodstein 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1627baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
16285abf3154SMordechay Goodstein 		mvm->monitor_p80 =
16295abf3154SMordechay Goodstein 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chandef);
16305abf3154SMordechay Goodstein 	}
1631baf41bc3SShaul Triebitz 
1632e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
16336d19a5ebSEmmanuel Grumbach 
16346d19a5ebSEmmanuel Grumbach 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
16356d19a5ebSEmmanuel Grumbach 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
16366d19a5ebSEmmanuel Grumbach 	    !mvm->csme_vif && mvm->mei_registered) {
16376d19a5ebSEmmanuel Grumbach 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
16386d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
16396d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = vif;
16406d19a5ebSEmmanuel Grumbach 	}
16416d19a5ebSEmmanuel Grumbach 
16421ab26632SMiri Korenblit out:
16431ab26632SMiri Korenblit 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
16441ab26632SMiri Korenblit 		     vif->type == NL80211_IFTYPE_ADHOC))
16451ab26632SMiri Korenblit 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
16461ab26632SMiri Korenblit 
1647e705c121SKalle Valo 	goto out_unlock;
1648e705c121SKalle Valo 
1649e705c121SKalle Valo  out_unbind:
1650e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1651e705c121SKalle Valo  out_unref_phy:
1652650cadb7SGregory Greenman 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1653e705c121SKalle Valo  out_free_bf:
1654e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1655e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1656e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1657e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1658e705c121SKalle Valo 	}
1659e705c121SKalle Valo  out_remove_mac:
1660650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
1661e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1662e705c121SKalle Valo  out_unlock:
1663e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1664e705c121SKalle Valo 
1665e705c121SKalle Valo 	return ret;
1666e705c121SKalle Valo }
1667e705c121SKalle Valo 
1668cb145863SJohannes Berg void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1669e705c121SKalle Valo 				 struct ieee80211_vif *vif)
1670e705c121SKalle Valo {
1671e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1672e705c121SKalle Valo 		/*
1673e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1674e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1675e705c121SKalle Valo 		 * queue are sent in ROC session.
1676e705c121SKalle Valo 		 */
1677e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1678e705c121SKalle Valo 	}
1679e705c121SKalle Valo }
1680e705c121SKalle Valo 
168160efeca1SMiri Korenblit /* This function is doing the common part of removing the interface for
168260efeca1SMiri Korenblit  * both - MLD and non-MLD modes. Returns true if removing the interface
168360efeca1SMiri Korenblit  * is done
168460efeca1SMiri Korenblit  */
1685cb145863SJohannes Berg static bool iwl_mvm_mac_remove_interface_common(struct ieee80211_hw *hw,
1686e705c121SKalle Valo 						struct ieee80211_vif *vif)
1687e705c121SKalle Valo {
1688e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1689e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
169086e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1691e705c121SKalle Valo 
1692e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1693e705c121SKalle Valo 
1694b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1695b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1696b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1697b0ffe455SJohannes Berg 
1698e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1699e705c121SKalle Valo 
17006d19a5ebSEmmanuel Grumbach 	if (vif == mvm->csme_vif) {
17016d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(NULL);
17026d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = NULL;
17036d19a5ebSEmmanuel Grumbach 	}
17046d19a5ebSEmmanuel Grumbach 
1705650cadb7SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
170686e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
1707650cadb7SGregory Greenman 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
170886e177d8SGregory Greenman 	if (probe_data)
170986e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
171086e177d8SGregory Greenman 
1711e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1712e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1713e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1714e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1715e705c121SKalle Valo 	}
1716e705c121SKalle Valo 
1717b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1718b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1719b73f9a4aSJohannes Berg 
1720e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1721e705c121SKalle Valo 
1722e705c121SKalle Valo 	/*
1723e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1724e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1725e705c121SKalle Valo 	 */
1726e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1727e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1728e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1729e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1730e705c121SKalle Valo 			mvm->noa_vif = NULL;
1731e705c121SKalle Valo 			mvm->noa_duration = 0;
1732e705c121SKalle Valo 		}
1733e705c121SKalle Valo #endif
173460efeca1SMiri Korenblit 		return true;
1735e705c121SKalle Valo 	}
1736e705c121SKalle Valo 
173760efeca1SMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
173860efeca1SMiri Korenblit 	return false;
173960efeca1SMiri Korenblit }
174060efeca1SMiri Korenblit 
174160efeca1SMiri Korenblit static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
174260efeca1SMiri Korenblit 					 struct ieee80211_vif *vif)
174360efeca1SMiri Korenblit {
174460efeca1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
174560efeca1SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
174660efeca1SMiri Korenblit 
174760efeca1SMiri Korenblit 	if (iwl_mvm_mac_remove_interface_common(hw, vif))
174860efeca1SMiri Korenblit 		goto out;
174960efeca1SMiri Korenblit 
1750e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1751e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1752d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1753e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1754650cadb7SGregory Greenman 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1755650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = NULL;
1756e705c121SKalle Valo 	}
1757e705c121SKalle Valo 
1758e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1759e705c121SKalle Valo 
1760698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1761698478c4SSara Sharon 
1762baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1763baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1764baf41bc3SShaul Triebitz 
176560efeca1SMiri Korenblit out:
176660efeca1SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP ||
176760efeca1SMiri Korenblit 	    vif->type == NL80211_IFTYPE_ADHOC) {
1768650cadb7SGregory Greenman 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
176960efeca1SMiri Korenblit 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1770e705c121SKalle Valo 	}
1771e705c121SKalle Valo 
1772e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1773e705c121SKalle Valo }
1774e705c121SKalle Valo 
1775e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1776e705c121SKalle Valo 	struct iwl_mvm *mvm;
1777e705c121SKalle Valo 	int port_id;
1778e705c121SKalle Valo };
1779e705c121SKalle Valo 
1780e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1781e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1782e705c121SKalle Valo {
1783e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1784e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1785e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
178697bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
178797bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
178897bce57bSLuca Coelho 		.flags = CMD_ASYNC,
178997bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
179097bce57bSLuca Coelho 	};
1791e705c121SKalle Valo 	int ret, len;
1792e705c121SKalle Valo 
1793e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1794e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1795e705c121SKalle Valo 		return;
1796e705c121SKalle Valo 
1797e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1798f276e20bSJohannes Berg 	    !vif->cfg.assoc)
1799e705c121SKalle Valo 		return;
1800e705c121SKalle Valo 
1801e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1802e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1803e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1804e705c121SKalle Valo 
180597bce57bSLuca Coelho 	hcmd.len[0] = len;
180697bce57bSLuca Coelho 	hcmd.data[0] = cmd;
180797bce57bSLuca Coelho 
180897bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1809e705c121SKalle Valo 	if (ret)
1810e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1811e705c121SKalle Valo }
1812e705c121SKalle Valo 
1813e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1814e705c121SKalle Valo {
1815e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1816e705c121SKalle Valo 		.mvm = mvm,
1817e705c121SKalle Valo 	};
1818db66abeeSJohannes Berg 	int ret;
1819e705c121SKalle Valo 
1820e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1821e705c121SKalle Valo 
1822e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1823e705c121SKalle Valo 		return;
1824e705c121SKalle Valo 
1825e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1826e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1827e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1828db66abeeSJohannes Berg 
1829db66abeeSJohannes Berg 	/*
1830db66abeeSJohannes Berg 	 * Send a (synchronous) ech command so that we wait for the
1831db66abeeSJohannes Berg 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
1832db66abeeSJohannes Berg 	 * the interface iterator. Otherwise, we might get here over
1833db66abeeSJohannes Berg 	 * and over again (by userspace just sending a lot of these)
1834db66abeeSJohannes Berg 	 * and the CPU can send them faster than the firmware can
1835db66abeeSJohannes Berg 	 * process them.
1836db66abeeSJohannes Berg 	 * Note that the CPU is still faster - but with this we'll
1837db66abeeSJohannes Berg 	 * actually send fewer commands overall because the CPU will
1838db66abeeSJohannes Berg 	 * not schedule the work in mac80211 as frequently if it's
1839db66abeeSJohannes Berg 	 * still running when rescheduled (possibly multiple times).
1840db66abeeSJohannes Berg 	 */
1841db66abeeSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1842db66abeeSJohannes Berg 	if (ret)
1843db66abeeSJohannes Berg 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
1844e705c121SKalle Valo }
1845e705c121SKalle Valo 
1846cbce62a3SMiri Korenblit u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1847e705c121SKalle Valo 			      struct netdev_hw_addr_list *mc_list)
1848e705c121SKalle Valo {
1849e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1850e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1851e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1852e705c121SKalle Valo 	int addr_count;
1853e705c121SKalle Valo 	bool pass_all;
1854e705c121SKalle Valo 	int len;
1855e705c121SKalle Valo 
1856e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1857e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1858e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1859e705c121SKalle Valo 	if (pass_all)
1860e705c121SKalle Valo 		addr_count = 0;
1861e705c121SKalle Valo 
1862e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1863e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1864e705c121SKalle Valo 	if (!cmd)
1865e705c121SKalle Valo 		return 0;
1866e705c121SKalle Valo 
1867e705c121SKalle Valo 	if (pass_all) {
1868e705c121SKalle Valo 		cmd->pass_all = 1;
1869e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1870e705c121SKalle Valo 	}
1871e705c121SKalle Valo 
1872e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1873e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1874e705c121SKalle Valo 				   cmd->count, addr->addr);
1875e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1876e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1877e705c121SKalle Valo 		cmd->count++;
1878e705c121SKalle Valo 	}
1879e705c121SKalle Valo 
1880e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1881e705c121SKalle Valo }
1882e705c121SKalle Valo 
1883cbce62a3SMiri Korenblit void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1884e705c121SKalle Valo 			      unsigned int changed_flags,
1885cbce62a3SMiri Korenblit 			      unsigned int *total_flags, u64 multicast)
1886e705c121SKalle Valo {
1887e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1888e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1889e705c121SKalle Valo 
1890e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1891e705c121SKalle Valo 
1892e705c121SKalle Valo 	/* replace previous configuration */
1893e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1894e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1895e705c121SKalle Valo 
1896e705c121SKalle Valo 	if (!cmd)
1897e705c121SKalle Valo 		goto out;
1898e705c121SKalle Valo 
189961e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
190061e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
190161e7d91bSLuca Coelho 
190261e7d91bSLuca Coelho 	if (cmd->pass_all)
190361e7d91bSLuca Coelho 		cmd->count = 0;
190461e7d91bSLuca Coelho 
1905e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1906e705c121SKalle Valo out:
1907e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1908e705c121SKalle Valo 	*total_flags = 0;
1909e705c121SKalle Valo }
1910e705c121SKalle Valo 
1911e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1912e705c121SKalle Valo 					struct ieee80211_vif *vif,
1913e705c121SKalle Valo 					unsigned int filter_flags,
1914e705c121SKalle Valo 					unsigned int changed_flags)
1915e705c121SKalle Valo {
1916e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1917e705c121SKalle Valo 
1918e705c121SKalle Valo 	/* We support only filter for probe requests */
1919e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1920e705c121SKalle Valo 		return;
1921e705c121SKalle Valo 
1922e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1923f276e20bSJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
1924e705c121SKalle Valo 	    !vif->p2p)
1925e705c121SKalle Valo 		return;
1926e705c121SKalle Valo 
1927e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1928e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1929e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1930e705c121SKalle Valo }
1931e705c121SKalle Valo 
193222c58834SGregory Greenman int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1933a07a8f37SSara Sharon {
1934a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1935a07a8f37SSara Sharon 
1936a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1937a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1938a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1939a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1940a07a8f37SSara Sharon 
1941a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1942a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1943a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1944a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1945a07a8f37SSara Sharon }
1946a07a8f37SSara Sharon 
1947f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1948f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1949f92659a1SSara Sharon {
1950d0a9123eSJohannes Berg 	if (vif->bss_conf.mu_mimo_owner) {
1951f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1952f92659a1SSara Sharon 
1953f92659a1SSara Sharon 		/*
1954f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1955f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1956f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
1957f92659a1SSara Sharon 		 */
1958afe0d181SJohannes Berg 		ieee80211_update_mu_groups(vif, 0,
1959f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
1960f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
1961f92659a1SSara Sharon 	}
1962f92659a1SSara Sharon }
1963f92659a1SSara Sharon 
1964f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1965f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
1966f92659a1SSara Sharon {
1967f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1968f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1969f92659a1SSara Sharon 
1970f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
1971f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1972f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
1973f92659a1SSara Sharon }
1974f92659a1SSara Sharon 
1975514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1976514c3069SLuca Coelho {
1977514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
1978514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
1979514c3069SLuca Coelho 	u8 residue_bits;
1980514c3069SLuca Coelho 	u8 res;
1981514c3069SLuca Coelho 
1982514c3069SLuca Coelho 	if (bit_num <= 5)
1983514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
1984514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
1985514c3069SLuca Coelho 
1986514c3069SLuca Coelho 	/*
1987514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
1988514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
1989514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
1990514c3069SLuca Coelho 	 */
1991514c3069SLuca Coelho 
1992514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
1993514c3069SLuca Coelho 
1994514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
1995514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
1996514c3069SLuca Coelho 	      residue_bits;
1997514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
1998514c3069SLuca Coelho 
1999514c3069SLuca Coelho 	return res;
2000514c3069SLuca Coelho }
2001514c3069SLuca Coelho 
2002091296d3SMiri Korenblit static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
2003091296d3SMiri Korenblit 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
2004cb63eb43SMiri Korenblit 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
2005cb63eb43SMiri Korenblit 			      bool inheritance)
2006091296d3SMiri Korenblit {
2007091296d3SMiri Korenblit 	int i;
2008091296d3SMiri Korenblit 
2009091296d3SMiri Korenblit 	/*
2010091296d3SMiri Korenblit 	* FW currently supports only nss == MAX_HE_SUPP_NSS
2011091296d3SMiri Korenblit 	*
2012091296d3SMiri Korenblit 	* If nss > MAX: we can ignore values we don't support
2013091296d3SMiri Korenblit 	* If nss < MAX: we can set zeros in other streams
2014091296d3SMiri Korenblit 	*/
2015091296d3SMiri Korenblit 	if (nss > MAX_HE_SUPP_NSS) {
20164d8421f2SJason A. Donenfeld 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2017091296d3SMiri Korenblit 			       MAX_HE_SUPP_NSS);
2018091296d3SMiri Korenblit 		nss = MAX_HE_SUPP_NSS;
2019091296d3SMiri Korenblit 	}
2020091296d3SMiri Korenblit 
2021091296d3SMiri Korenblit 	for (i = 0; i < nss; i++) {
2022091296d3SMiri Korenblit 		u8 ru_index_tmp = ru_index_bitmap << 1;
2023091296d3SMiri Korenblit 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2024091296d3SMiri Korenblit 		u8 bw;
2025091296d3SMiri Korenblit 
2026091296d3SMiri Korenblit 		for (bw = 0;
2027091296d3SMiri Korenblit 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2028091296d3SMiri Korenblit 		     bw++) {
2029091296d3SMiri Korenblit 			ru_index_tmp >>= 1;
2030091296d3SMiri Korenblit 
2031cb63eb43SMiri Korenblit 			/*
2032cb63eb43SMiri Korenblit 			* According to the 11be spec, if for a specific BW the PPE Thresholds
2033cb63eb43SMiri Korenblit 			* isn't present - it should inherit the thresholds from the last
2034cb63eb43SMiri Korenblit 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2035cb63eb43SMiri Korenblit 			* this inheritance - continue in this case
2036cb63eb43SMiri Korenblit 			*/
2037cb63eb43SMiri Korenblit 			if (!(ru_index_tmp & 1)) {
2038cb63eb43SMiri Korenblit 				if (inheritance)
2039cb63eb43SMiri Korenblit 					goto set_thresholds;
2040cb63eb43SMiri Korenblit 				else
2041091296d3SMiri Korenblit 					continue;
2042cb63eb43SMiri Korenblit 			}
2043091296d3SMiri Korenblit 
2044091296d3SMiri Korenblit 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2045091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2046091296d3SMiri Korenblit 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2047091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2048091296d3SMiri Korenblit 
2049cb63eb43SMiri Korenblit set_thresholds:
2050091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2051091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2052091296d3SMiri Korenblit 		}
2053091296d3SMiri Korenblit 	}
2054091296d3SMiri Korenblit }
2055091296d3SMiri Korenblit 
2056091296d3SMiri Korenblit static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
205757974a55SGregory Greenman 					    struct ieee80211_link_sta *link_sta,
2058cb63eb43SMiri Korenblit 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2059cb63eb43SMiri Korenblit 					    bool inheritance)
2060091296d3SMiri Korenblit {
206157974a55SGregory Greenman 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
206257974a55SGregory Greenman 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
206357974a55SGregory Greenman 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2064091296d3SMiri Korenblit 	u8 ru_index_bitmap =
2065091296d3SMiri Korenblit 		u8_get_bits(*ppe,
2066091296d3SMiri Korenblit 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2067091296d3SMiri Korenblit 	/* Starting after PPE header */
2068091296d3SMiri Korenblit 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2069091296d3SMiri Korenblit 
2070cb63eb43SMiri Korenblit 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2071cb63eb43SMiri Korenblit 			  inheritance);
2072091296d3SMiri Korenblit }
2073091296d3SMiri Korenblit 
20744df6a075SMiri Korenblit static int
20754df6a075SMiri Korenblit iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
20764df6a075SMiri Korenblit 					 u8 nominal_padding)
2077091296d3SMiri Korenblit {
2078091296d3SMiri Korenblit 	int low_th = -1;
2079091296d3SMiri Korenblit 	int high_th = -1;
2080091296d3SMiri Korenblit 	int i;
2081091296d3SMiri Korenblit 
2082cb63eb43SMiri Korenblit 	/* all the macros are the same for EHT and HE */
2083091296d3SMiri Korenblit 	switch (nominal_padding) {
2084cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2085091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2086091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2087091296d3SMiri Korenblit 		break;
2088cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2089091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_BPSK;
2090091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2091091296d3SMiri Korenblit 		break;
2092cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2093cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2094091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2095091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_BPSK;
2096091296d3SMiri Korenblit 		break;
2097091296d3SMiri Korenblit 	}
2098091296d3SMiri Korenblit 
20994df6a075SMiri Korenblit 	if (low_th < 0 || high_th < 0)
21004df6a075SMiri Korenblit 		return -EINVAL;
21014df6a075SMiri Korenblit 
2102091296d3SMiri Korenblit 	/* Set the PPE thresholds accordingly */
2103091296d3SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2104091296d3SMiri Korenblit 		u8 bw;
2105091296d3SMiri Korenblit 
2106091296d3SMiri Korenblit 		for (bw = 0;
2107091296d3SMiri Korenblit 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2108091296d3SMiri Korenblit 			bw++) {
2109091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2110091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2111091296d3SMiri Korenblit 		}
2112091296d3SMiri Korenblit 	}
2113091296d3SMiri Korenblit 
21144df6a075SMiri Korenblit 	return 0;
2115091296d3SMiri Korenblit }
2116091296d3SMiri Korenblit 
2117cb63eb43SMiri Korenblit static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2118cb63eb43SMiri Korenblit 					 u8 nominal_padding)
2119cb63eb43SMiri Korenblit {
2120cb63eb43SMiri Korenblit 	int i;
2121cb63eb43SMiri Korenblit 
2122cb63eb43SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2123cb63eb43SMiri Korenblit 		u8 bw;
2124cb63eb43SMiri Korenblit 
2125cb63eb43SMiri Korenblit 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2126cb63eb43SMiri Korenblit 		     bw++) {
2127cb63eb43SMiri Korenblit 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2128cb63eb43SMiri Korenblit 
2129cb63eb43SMiri Korenblit 			if (nominal_padding >
2130cb63eb43SMiri Korenblit 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2131cb63eb43SMiri Korenblit 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2132cb63eb43SMiri Korenblit 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2133cb63eb43SMiri Korenblit 			else if (nominal_padding ==
2134cb63eb43SMiri Korenblit 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2135cb63eb43SMiri Korenblit 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2136cb63eb43SMiri Korenblit 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2137cb63eb43SMiri Korenblit 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2138cb63eb43SMiri Korenblit 		}
2139cb63eb43SMiri Korenblit 	}
2140cb63eb43SMiri Korenblit }
2141cb63eb43SMiri Korenblit 
21424df6a075SMiri Korenblit /* Set the pkt_ext field according to PPE Thresholds element */
214357974a55SGregory Greenman int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
214457974a55SGregory Greenman 			    struct ieee80211_link_sta *link_sta,
21454df6a075SMiri Korenblit 			    struct iwl_he_pkt_ext_v2 *pkt_ext)
21464df6a075SMiri Korenblit {
21474df6a075SMiri Korenblit 	u8 nominal_padding;
21484df6a075SMiri Korenblit 	int i, ret = 0;
21494df6a075SMiri Korenblit 
215057974a55SGregory Greenman 	if (WARN_ON(!link_sta))
215157974a55SGregory Greenman 		return -EINVAL;
215257974a55SGregory Greenman 
21534df6a075SMiri Korenblit 	/* Initialize the PPE thresholds to "None" (7), as described in Table
21544df6a075SMiri Korenblit 	 * 9-262ac of 80211.ax/D3.0.
21554df6a075SMiri Korenblit 	 */
21564df6a075SMiri Korenblit 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
21574df6a075SMiri Korenblit 	       sizeof(struct iwl_he_pkt_ext_v2));
21584df6a075SMiri Korenblit 
215957974a55SGregory Greenman 	if (link_sta->eht_cap.has_eht) {
21604df6a075SMiri Korenblit 		nominal_padding =
216157974a55SGregory Greenman 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
21624df6a075SMiri Korenblit 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
21634df6a075SMiri Korenblit 
21644df6a075SMiri Korenblit 		/* If PPE Thresholds exists, parse them into a FW-familiar
21654df6a075SMiri Korenblit 		 * format.
21664df6a075SMiri Korenblit 		 */
216757974a55SGregory Greenman 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
21684df6a075SMiri Korenblit 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
216957974a55SGregory Greenman 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
21704df6a075SMiri Korenblit 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
217157974a55SGregory Greenman 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
21724df6a075SMiri Korenblit 			u8 ru_index_bitmap =
21734df6a075SMiri Korenblit 				u16_get_bits(*ppe,
21744df6a075SMiri Korenblit 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
21754df6a075SMiri Korenblit 			 /* Starting after PPE header */
21764df6a075SMiri Korenblit 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
21774df6a075SMiri Korenblit 
21784df6a075SMiri Korenblit 			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
21794df6a075SMiri Korenblit 					  ppe, ppe_pos_bit, true);
218057974a55SGregory Greenman 		/* EHT PPE Thresholds doesn't exist - set the API according to
218157974a55SGregory Greenman 		 * HE PPE Tresholds
21824df6a075SMiri Korenblit 		 */
218357974a55SGregory Greenman 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
21844df6a075SMiri Korenblit 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
21854df6a075SMiri Korenblit 			/* Even though HE Capabilities IE doesn't contain PPE
21864df6a075SMiri Korenblit 			 * Thresholds for BW 320Mhz, thresholds for this BW will
21874df6a075SMiri Korenblit 			 * be filled in with the same values as 160Mhz, due to
21884df6a075SMiri Korenblit 			 * the inheritance, as required.
21894df6a075SMiri Korenblit 			 */
219057974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
21914df6a075SMiri Korenblit 							true);
21924df6a075SMiri Korenblit 
21934df6a075SMiri Korenblit 			/* According to the requirements, for MCSs 12-13 the
21944df6a075SMiri Korenblit 			 * maximum value between HE PPE Threshold and Common
21954df6a075SMiri Korenblit 			 * Nominal Packet Padding needs to be taken
21964df6a075SMiri Korenblit 			 */
21974df6a075SMiri Korenblit 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
21984df6a075SMiri Korenblit 
21994df6a075SMiri Korenblit 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
22004df6a075SMiri Korenblit 		 * take the Thresholds from Common Nominal Packet Padding field
22014df6a075SMiri Korenblit 		 */
22024df6a075SMiri Korenblit 		} else {
22034df6a075SMiri Korenblit 			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
22044df6a075SMiri Korenblit 								       nominal_padding);
22054df6a075SMiri Korenblit 		}
220657974a55SGregory Greenman 	} else if (link_sta->he_cap.has_he) {
22074df6a075SMiri Korenblit 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
220857974a55SGregory Greenman 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
22094df6a075SMiri Korenblit 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
221057974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
22114df6a075SMiri Korenblit 							false);
22124df6a075SMiri Korenblit 		/* PPE Thresholds doesn't exist - set the API PPE values
22134df6a075SMiri Korenblit 		 * according to Common Nominal Packet Padding field.
22144df6a075SMiri Korenblit 		 */
22154df6a075SMiri Korenblit 		} else {
22164df6a075SMiri Korenblit 			nominal_padding =
221757974a55SGregory Greenman 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
22184df6a075SMiri Korenblit 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
22194df6a075SMiri Korenblit 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
22204df6a075SMiri Korenblit 				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
22214df6a075SMiri Korenblit 									       nominal_padding);
22224df6a075SMiri Korenblit 		}
22234df6a075SMiri Korenblit 	}
22244df6a075SMiri Korenblit 
22254df6a075SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
22264df6a075SMiri Korenblit 		int bw;
22274df6a075SMiri Korenblit 
22284df6a075SMiri Korenblit 		for (bw = 0;
22294df6a075SMiri Korenblit 		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
22304df6a075SMiri Korenblit 		     bw++) {
22314df6a075SMiri Korenblit 			u8 *qam_th =
22324df6a075SMiri Korenblit 				&pkt_ext->pkt_ext_qam_th[i][bw][0];
22334df6a075SMiri Korenblit 
22344df6a075SMiri Korenblit 			IWL_DEBUG_HT(mvm,
22354df6a075SMiri Korenblit 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
22364df6a075SMiri Korenblit 				     i, bw, qam_th[0], qam_th[1]);
22374df6a075SMiri Korenblit 		}
22384df6a075SMiri Korenblit 	}
22394df6a075SMiri Korenblit 	return ret;
22404df6a075SMiri Korenblit }
22414df6a075SMiri Korenblit 
22429c4f15caSMiri Korenblit /*
22439c4f15caSMiri Korenblit  * This function sets the MU EDCA parameters ans returns whether MU EDCA
22449c4f15caSMiri Korenblit  * is enabled or not
22459c4f15caSMiri Korenblit  */
224655eb1c5fSMiri Korenblit bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2247e119e740SEmmanuel Grumbach 				   const struct iwl_mvm_vif_link_info *link_info,
224855eb1c5fSMiri Korenblit 				   struct iwl_he_backoff_conf *trig_based_txf)
22499c4f15caSMiri Korenblit {
22509c4f15caSMiri Korenblit 	int i;
22519c4f15caSMiri Korenblit 	/* Mark MU EDCA as enabled, unless none detected on some AC */
22529c4f15caSMiri Korenblit 	bool mu_edca_enabled = true;
22539c4f15caSMiri Korenblit 
22549c4f15caSMiri Korenblit 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2255e119e740SEmmanuel Grumbach 		const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2256e119e740SEmmanuel Grumbach 			&link_info->queue_params[i].mu_edca_param_rec;
22579c4f15caSMiri Korenblit 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
22589c4f15caSMiri Korenblit 
2259e119e740SEmmanuel Grumbach 		if (!link_info->queue_params[i].mu_edca) {
22609c4f15caSMiri Korenblit 			mu_edca_enabled = false;
22619c4f15caSMiri Korenblit 			break;
22629c4f15caSMiri Korenblit 		}
22639c4f15caSMiri Korenblit 
22649c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmin =
22659c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
22669c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmax =
22679c4f15caSMiri Korenblit 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
22689c4f15caSMiri Korenblit 		trig_based_txf[ac].aifsn =
22699c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->aifsn & 0xf);
22709c4f15caSMiri Korenblit 		trig_based_txf[ac].mu_time =
22719c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->mu_edca_timer);
22729c4f15caSMiri Korenblit 	}
22739c4f15caSMiri Korenblit 
22749c4f15caSMiri Korenblit 	return mu_edca_enabled;
22759c4f15caSMiri Korenblit }
22769c4f15caSMiri Korenblit 
22779be162a7SMiri Korenblit bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
22789c4f15caSMiri Korenblit {
22799c4f15caSMiri Korenblit 	const struct ieee80211_supported_band *sband;
22809c4f15caSMiri Korenblit 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
22819c4f15caSMiri Korenblit 
22829c4f15caSMiri Korenblit 	/* This capability is the same for all bands,
22839c4f15caSMiri Korenblit 	 * so take it from one of them.
22849c4f15caSMiri Korenblit 	 */
22859c4f15caSMiri Korenblit 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
22861ec7291eSJohannes Berg 	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
22879c4f15caSMiri Korenblit 
22889c4f15caSMiri Korenblit 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
22899c4f15caSMiri Korenblit 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
22909c4f15caSMiri Korenblit }
22919c4f15caSMiri Korenblit 
229257974a55SGregory Greenman __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
229357974a55SGregory Greenman 				 struct ieee80211_link_sta *link_sta)
22944df6a075SMiri Korenblit {
229557974a55SGregory Greenman 	u8 *mac_cap_info =
229657974a55SGregory Greenman 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
22974df6a075SMiri Korenblit 	__le32 htc_flags = 0;
22984df6a075SMiri Korenblit 
22994df6a075SMiri Korenblit 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
23004df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
23014df6a075SMiri Korenblit 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
23024df6a075SMiri Korenblit 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
23034df6a075SMiri Korenblit 		u8 link_adap =
23044df6a075SMiri Korenblit 			((mac_cap_info[2] &
23054df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
23064df6a075SMiri Korenblit 			 (mac_cap_info[1] &
23074df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
23084df6a075SMiri Korenblit 
23094df6a075SMiri Korenblit 		if (link_adap == 2)
23104df6a075SMiri Korenblit 			htc_flags |=
23114df6a075SMiri Korenblit 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
23124df6a075SMiri Korenblit 		else if (link_adap == 3)
23134df6a075SMiri Korenblit 			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
23144df6a075SMiri Korenblit 	}
23154df6a075SMiri Korenblit 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
23164df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
23174df6a075SMiri Korenblit 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
23184df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
23194df6a075SMiri Korenblit 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
23204df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
23214df6a075SMiri Korenblit 
23224df6a075SMiri Korenblit 	return htc_flags;
23234df6a075SMiri Korenblit }
23244df6a075SMiri Korenblit 
2325514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2326514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2327514c3069SLuca Coelho {
2328514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
232942506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2330514c3069SLuca Coelho 		.sta_id = sta_id,
2331514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2332dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2333514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2334514c3069SLuca Coelho 		.frame_time_rts_th =
2335514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2336514c3069SLuca Coelho 	};
233742506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
233842506dd2SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
233942506dd2SJohannes Berg 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
234042506dd2SJohannes Berg 	int size;
2341514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2342514c3069SLuca Coelho 	u32 flags;
2343514c3069SLuca Coelho 	int i;
234442506dd2SJohannes Berg 	void *cmd;
234542506dd2SJohannes Berg 
234642506dd2SJohannes Berg 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
234742506dd2SJohannes Berg 		ver = 1;
234842506dd2SJohannes Berg 
234942506dd2SJohannes Berg 	switch (ver) {
235042506dd2SJohannes Berg 	case 1:
235142506dd2SJohannes Berg 		/* same layout as v2 except some data at the end */
235242506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
235342506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
235442506dd2SJohannes Berg 		break;
235542506dd2SJohannes Berg 	case 2:
235642506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
235742506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
235842506dd2SJohannes Berg 		break;
235942506dd2SJohannes Berg 	case 3:
236042506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd;
236142506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
236242506dd2SJohannes Berg 		break;
236342506dd2SJohannes Berg 	default:
236442506dd2SJohannes Berg 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
236542506dd2SJohannes Berg 		return;
236642506dd2SJohannes Berg 	}
2367514c3069SLuca Coelho 
2368514c3069SLuca Coelho 	rcu_read_lock();
2369514c3069SLuca Coelho 
2370514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
237112d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2372514c3069SLuca Coelho 		rcu_read_unlock();
2373514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2374514c3069SLuca Coelho 		return;
2375514c3069SLuca Coelho 	}
2376514c3069SLuca Coelho 
2377046d2e7cSSriram R 	if (!sta->deflink.he_cap.has_he) {
2378514c3069SLuca Coelho 		rcu_read_unlock();
2379514c3069SLuca Coelho 		return;
2380514c3069SLuca Coelho 	}
2381514c3069SLuca Coelho 
2382514c3069SLuca Coelho 	flags = 0;
2383514c3069SLuca Coelho 
23844f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
2385650cadb7SGregory Greenman 	if (mvmvif->deflink.he_ru_2mhz_block)
23864f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
23874f58121dSIlan Peer 
2388514c3069SLuca Coelho 	/* HTC flags */
238957974a55SGregory Greenman 	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2390514c3069SLuca Coelho 
23914df6a075SMiri Korenblit 	/* PPE Thresholds */
239257974a55SGregory Greenman 	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2393cb63eb43SMiri Korenblit 		flags |= STA_CTXT_HE_PACKET_EXT;
239473f23d91SShaul Triebitz 
2395046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
239673f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
239773f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
239873f23d91SShaul Triebitz 
2399046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
240073f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
240173f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
240273f23d91SShaul Triebitz 
2403514c3069SLuca Coelho 	rcu_read_unlock();
2404514c3069SLuca Coelho 
2405e119e740SEmmanuel Grumbach 	if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
24069c4f15caSMiri Korenblit 					  &sta_ctxt_cmd.trig_based_txf[0]))
2407514c3069SLuca Coelho 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2408514c3069SLuca Coelho 
2409514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2410514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2411514c3069SLuca Coelho 
2412514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2413514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2414514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2415514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2416514c3069SLuca Coelho 	}
2417514c3069SLuca Coelho 
24189c4f15caSMiri Korenblit 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2419ee1a02d7SShaul Triebitz 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2420ee1a02d7SShaul Triebitz 
2421918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2422918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2423918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2424918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2425d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2426d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2427d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2428d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2429d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2430d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2431918cbf39SSara Sharon 	}
2432514c3069SLuca Coelho 
2433514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2434514c3069SLuca Coelho 
243542506dd2SJohannes Berg 	if (ver < 3) {
243642506dd2SJohannes Berg 		/* fields before pkt_ext */
243742506dd2SJohannes Berg 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
243842506dd2SJohannes Berg 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
243942506dd2SJohannes Berg 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
244042506dd2SJohannes Berg 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
244142506dd2SJohannes Berg 
244242506dd2SJohannes Berg 		/* pkt_ext */
244342506dd2SJohannes Berg 		for (i = 0;
244442506dd2SJohannes Berg 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
244542506dd2SJohannes Berg 		     i++) {
244642506dd2SJohannes Berg 			u8 bw;
244742506dd2SJohannes Berg 
244842506dd2SJohannes Berg 			for (bw = 0;
244942506dd2SJohannes Berg 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
245042506dd2SJohannes Berg 			     bw++) {
245142506dd2SJohannes Berg 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
245242506dd2SJohannes Berg 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
245342506dd2SJohannes Berg 
245442506dd2SJohannes Berg 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
245542506dd2SJohannes Berg 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
245642506dd2SJohannes Berg 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
245742506dd2SJohannes Berg 			}
245842506dd2SJohannes Berg 		}
245942506dd2SJohannes Berg 
246042506dd2SJohannes Berg 		/* fields after pkt_ext */
246142506dd2SJohannes Berg 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
246242506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
246342506dd2SJohannes Berg 			     sizeof(sta_ctxt_cmd_v2) -
246442506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
246542506dd2SJohannes Berg 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
246642506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
246742506dd2SJohannes Berg 		       (u8 *)&sta_ctxt_cmd +
246842506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
246942506dd2SJohannes Berg 		       sizeof(sta_ctxt_cmd) -
247042506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
247142506dd2SJohannes Berg 		sta_ctxt_cmd_v2.reserved3 = 0;
247242506dd2SJohannes Berg 	}
247342506dd2SJohannes Berg 
247442506dd2SJohannes Berg 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2475514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2476514c3069SLuca Coelho }
2477514c3069SLuca Coelho 
2478660eba5aSMiri Korenblit void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2479af84ac57SJohannes Berg 			   u32 duration_override)
2480af84ac57SJohannes Berg {
2481af84ac57SJohannes Berg 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2482af84ac57SJohannes Berg 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2483af84ac57SJohannes Berg 
2484af84ac57SJohannes Berg 	if (duration_override > duration)
2485af84ac57SJohannes Berg 		duration = duration_override;
2486af84ac57SJohannes Berg 
2487af84ac57SJohannes Berg 	/* Try really hard to protect the session and hear a beacon
2488af84ac57SJohannes Berg 	 * The new session protection command allows us to protect the
2489af84ac57SJohannes Berg 	 * session for a much longer time since the firmware will internally
2490af84ac57SJohannes Berg 	 * create two events: a 300TU one with a very high priority that
2491af84ac57SJohannes Berg 	 * won't be fragmented which should be enough for 99% of the cases,
2492af84ac57SJohannes Berg 	 * and another one (which we configure here to be 900TU long) which
2493af84ac57SJohannes Berg 	 * will have a slightly lower priority, but more importantly, can be
2494af84ac57SJohannes Berg 	 * fragmented so that it'll allow other activities to run.
2495af84ac57SJohannes Berg 	 */
2496af84ac57SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
2497af84ac57SJohannes Berg 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2498af84ac57SJohannes Berg 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2499af84ac57SJohannes Berg 						    min_duration, false);
2500af84ac57SJohannes Berg 	else
2501af84ac57SJohannes Berg 		iwl_mvm_protect_session(mvm, vif, duration,
2502af84ac57SJohannes Berg 					min_duration, 500, false);
2503af84ac57SJohannes Berg }
2504af84ac57SJohannes Berg 
2505660eba5aSMiri Korenblit /* Handle association common part to MLD and non-MLD modes */
2506660eba5aSMiri Korenblit void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2507660eba5aSMiri Korenblit 					    struct ieee80211_vif *vif,
2508660eba5aSMiri Korenblit 					    u64 changes)
2509660eba5aSMiri Korenblit {
2510660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2511660eba5aSMiri Korenblit 	int ret;
2512660eba5aSMiri Korenblit 
2513660eba5aSMiri Korenblit 	/* The firmware tracks the MU-MIMO group on its own.
2514660eba5aSMiri Korenblit 	 * However, on HW restart we should restore this data.
2515660eba5aSMiri Korenblit 	 */
2516660eba5aSMiri Korenblit 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2517660eba5aSMiri Korenblit 	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2518660eba5aSMiri Korenblit 		ret = iwl_mvm_update_mu_groups(mvm, vif);
2519660eba5aSMiri Korenblit 		if (ret)
2520660eba5aSMiri Korenblit 			IWL_ERR(mvm,
2521660eba5aSMiri Korenblit 				"failed to update VHT MU_MIMO groups\n");
2522660eba5aSMiri Korenblit 	}
2523660eba5aSMiri Korenblit 
2524660eba5aSMiri Korenblit 	iwl_mvm_recalc_multicast(mvm);
2525660eba5aSMiri Korenblit 
2526660eba5aSMiri Korenblit 	/* reset rssi values */
2527660eba5aSMiri Korenblit 	mvmvif->bf_data.ave_beacon_signal = 0;
2528660eba5aSMiri Korenblit 
2529660eba5aSMiri Korenblit 	iwl_mvm_bt_coex_vif_change(mvm);
25301a3e7039SGregory Greenman 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2531660eba5aSMiri Korenblit 					    IEEE80211_SMPS_AUTOMATIC);
2532660eba5aSMiri Korenblit 	if (fw_has_capa(&mvm->fw->ucode_capa,
2533660eba5aSMiri Korenblit 			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2534660eba5aSMiri Korenblit 		iwl_mvm_config_scan(mvm);
2535660eba5aSMiri Korenblit }
2536660eba5aSMiri Korenblit 
2537660eba5aSMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
25381a3e7039SGregory Greenman void
25391a3e7039SGregory Greenman iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2540660eba5aSMiri Korenblit 					struct ieee80211_vif *vif,
25411a3e7039SGregory Greenman 					struct ieee80211_bss_conf *link_conf,
2542660eba5aSMiri Korenblit 					u64 changes)
2543660eba5aSMiri Korenblit {
2544660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2545660eba5aSMiri Korenblit 	int ret;
2546660eba5aSMiri Korenblit 
2547660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BEACON_INFO) {
2548660eba5aSMiri Korenblit 		/* We received a beacon from the associated AP so
2549660eba5aSMiri Korenblit 		 * remove the session protection.
2550660eba5aSMiri Korenblit 		 */
2551660eba5aSMiri Korenblit 		iwl_mvm_stop_session_protection(mvm, vif);
2552660eba5aSMiri Korenblit 
2553660eba5aSMiri Korenblit 		iwl_mvm_sf_update(mvm, vif, false);
2554660eba5aSMiri Korenblit 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2555660eba5aSMiri Korenblit 	}
2556660eba5aSMiri Korenblit 
2557660eba5aSMiri Korenblit 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2558660eba5aSMiri Korenblit 		       /* Send power command on every beacon change,
2559660eba5aSMiri Korenblit 			* because we may have not enabled beacon abort yet.
2560660eba5aSMiri Korenblit 			*/
2561660eba5aSMiri Korenblit 		       BSS_CHANGED_BEACON_INFO)) {
2562660eba5aSMiri Korenblit 		ret = iwl_mvm_power_update_mac(mvm);
2563660eba5aSMiri Korenblit 		if (ret)
2564660eba5aSMiri Korenblit 			IWL_ERR(mvm, "failed to update power mode\n");
2565660eba5aSMiri Korenblit 	}
2566660eba5aSMiri Korenblit 
2567660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_CQM) {
2568660eba5aSMiri Korenblit 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2569660eba5aSMiri Korenblit 		/* reset cqm events tracking */
2570660eba5aSMiri Korenblit 		mvmvif->bf_data.last_cqm_event = 0;
2571660eba5aSMiri Korenblit 		if (mvmvif->bf_data.bf_enabled) {
257222c58834SGregory Greenman 			/* FIXME: need to update per link when FW API will
257322c58834SGregory Greenman 			 * support it
257422c58834SGregory Greenman 			 */
2575660eba5aSMiri Korenblit 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2576660eba5aSMiri Korenblit 			if (ret)
2577660eba5aSMiri Korenblit 				IWL_ERR(mvm,
2578660eba5aSMiri Korenblit 					"failed to update CQM thresholds\n");
2579660eba5aSMiri Korenblit 		}
2580660eba5aSMiri Korenblit 	}
2581660eba5aSMiri Korenblit 
2582660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BANDWIDTH)
25831a3e7039SGregory Greenman 		iwl_mvm_update_link_smps(vif, link_conf);
2584660eba5aSMiri Korenblit }
2585660eba5aSMiri Korenblit 
2586e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2587e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2588e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
25897b7090b4SJohannes Berg 					     u64 changes)
2590e705c121SKalle Valo {
2591e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2592e705c121SKalle Valo 	int ret;
2593e705c121SKalle Valo 
2594e705c121SKalle Valo 	/*
2595cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend
2596cdaba917SEmmanuel Grumbach 	 * on the beacon interval, which was not known when the station
2597cdaba917SEmmanuel Grumbach 	 * interface was added.
2598e705c121SKalle Valo 	 */
2599f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2600cb63eb43SMiri Korenblit 		if ((vif->bss_conf.he_support &&
2601cb63eb43SMiri Korenblit 		     !iwlwifi_mod_params.disable_11ax) ||
2602cb63eb43SMiri Korenblit 		    (vif->bss_conf.eht_support &&
2603cb63eb43SMiri Korenblit 		     !iwlwifi_mod_params.disable_11be))
2604650cadb7SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2605514c3069SLuca Coelho 
2606e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2607514c3069SLuca Coelho 	}
2608e705c121SKalle Valo 
260940ecdd01SShaul Triebitz 	/* Update MU EDCA params */
261040ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2611cb63eb43SMiri Korenblit 	    vif->cfg.assoc &&
2612cb63eb43SMiri Korenblit 	    ((vif->bss_conf.he_support &&
2613cb63eb43SMiri Korenblit 	      !iwlwifi_mod_params.disable_11ax) ||
2614cb63eb43SMiri Korenblit 	     (vif->bss_conf.eht_support &&
2615cb63eb43SMiri Korenblit 	      !iwlwifi_mod_params.disable_11be)))
2616650cadb7SGregory Greenman 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
261740ecdd01SShaul Triebitz 
2618e705c121SKalle Valo 	/*
2619e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2620e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2621e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2622e705c121SKalle Valo 	 * branch for disassociation below.
2623e705c121SKalle Valo 	 */
2624e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2625650cadb7SGregory Greenman 		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2626e705c121SKalle Valo 
2627650cadb7SGregory Greenman 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2628e705c121SKalle Valo 	if (ret)
2629e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2630e705c121SKalle Valo 
2631e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2632650cadb7SGregory Greenman 	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2633f276e20bSJohannes Berg 	mvmvif->associated = vif->cfg.assoc;
2634e705c121SKalle Valo 
2635e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2636f276e20bSJohannes Berg 		if (vif->cfg.assoc) {
2637e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2638e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2639650cadb7SGregory Greenman 			memset(&mvmvif->deflink.beacon_stats, 0,
2640650cadb7SGregory Greenman 			       sizeof(mvmvif->deflink.beacon_stats));
2641e705c121SKalle Valo 
2642e705c121SKalle Valo 			/* add quota for this interface */
2643e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2644e705c121SKalle Valo 			if (ret) {
2645e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2646e705c121SKalle Valo 				return;
2647e705c121SKalle Valo 			}
2648e705c121SKalle Valo 
2649e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2650fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2651fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2652fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2653e705c121SKalle Valo 				/*
2654e705c121SKalle Valo 				 * If we're restarting then the firmware will
2655e705c121SKalle Valo 				 * obviously have lost synchronisation with
2656e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2657e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2658e705c121SKalle Valo 				 * scheduling a session protection time event.
2659e705c121SKalle Valo 				 *
2660e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2661e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2662e705c121SKalle Valo 				 * the beacon interval.
2663e705c121SKalle Valo 				 *
2664e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2665e705c121SKalle Valo 				 * than a single interface.
2666fe959c7bSEmmanuel Grumbach 				 *
2667fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2668fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2669fe959c7bSEmmanuel Grumbach 				 * only anyway.
2670e705c121SKalle Valo 				 */
2671e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2672e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2673e705c121SKalle Valo 							5 * dur, false);
2674af84ac57SJohannes Berg 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2675af84ac57SJohannes Berg 					     &mvm->status) &&
2676af84ac57SJohannes Berg 				   !vif->bss_conf.dtim_period) {
2677af84ac57SJohannes Berg 				/*
2678af84ac57SJohannes Berg 				 * If we're not restarting and still haven't
2679af84ac57SJohannes Berg 				 * heard a beacon (dtim period unknown) then
2680af84ac57SJohannes Berg 				 * make sure we still have enough minimum time
2681af84ac57SJohannes Berg 				 * remaining in the time event, since the auth
2682af84ac57SJohannes Berg 				 * might actually have taken quite a while
2683af84ac57SJohannes Berg 				 * (especially for SAE) and so the remaining
2684af84ac57SJohannes Berg 				 * time could be small without us having heard
2685af84ac57SJohannes Berg 				 * a beacon yet.
2686af84ac57SJohannes Berg 				 */
2687af84ac57SJohannes Berg 				iwl_mvm_protect_assoc(mvm, vif, 0);
2688e705c121SKalle Valo 			}
2689e705c121SKalle Valo 
2690e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2691e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2692e705c121SKalle Valo 			if (vif->p2p) {
2693e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2694e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
26951a3e7039SGregory Greenman 						    IEEE80211_SMPS_DYNAMIC, 0);
2696e705c121SKalle Valo 			}
2697650cadb7SGregory Greenman 		} else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
26986d19a5ebSEmmanuel Grumbach 			iwl_mvm_mei_host_disassociated(mvm);
2699e705c121SKalle Valo 			/*
2700e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2701e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2702e705c121SKalle Valo 			 */
270369e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
270469e508b4SIlan Peer 			WARN_ONCE(ret &&
270569e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
270669e508b4SIlan Peer 					    &mvm->status),
2707e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2708e705c121SKalle Valo 
27096b28f978SEmmanuel Grumbach 			/*
27106b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
27116b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
27126b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
27136b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
27146b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
27156b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
27166b28f978SEmmanuel Grumbach 			 */
27176b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
27186b28f978SEmmanuel Grumbach 				      &mvm->status)) {
27195c75a208SJohannes Berg 				/* first remove remaining keys */
2720ba9eef6bSGregory Greenman 				iwl_mvm_sec_key_remove_ap(mvm, vif,
2721072573f6SJohannes Berg 							  &mvmvif->deflink, 0);
27225c75a208SJohannes Berg 
27236b28f978SEmmanuel Grumbach 				/*
27246b28f978SEmmanuel Grumbach 				 * Remove AP station now that
27256b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
27266b28f978SEmmanuel Grumbach 				 */
27276b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
2728650cadb7SGregory Greenman 							mvmvif->deflink.ap_sta_id);
2729e705c121SKalle Valo 				if (ret)
27306b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
27316b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2732e705c121SKalle Valo 
2733650cadb7SGregory Greenman 				mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
27346b28f978SEmmanuel Grumbach 			}
27356b28f978SEmmanuel Grumbach 
2736e705c121SKalle Valo 			/* remove quota for this interface */
2737e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2738e705c121SKalle Valo 			if (ret)
2739e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2740e705c121SKalle Valo 
2741e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2742e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2743e705c121SKalle Valo 			if (ret)
2744e705c121SKalle Valo 				IWL_ERR(mvm,
2745e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2746e705c121SKalle Valo 					vif->addr);
2747e705c121SKalle Valo 		}
2748e705c121SKalle Valo 
2749660eba5aSMiri Korenblit 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
2750a07a8f37SSara Sharon 	}
2751a07a8f37SSara Sharon 
27521a3e7039SGregory Greenman 	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
27531a3e7039SGregory Greenman 						changes);
2754b45242c9SAvraham Stern }
2755b45242c9SAvraham Stern 
2756f947b62cSMiri Korenblit bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2757f947b62cSMiri Korenblit 				  struct ieee80211_vif *vif,
2758f947b62cSMiri Korenblit 				  int *ret)
2759f947b62cSMiri Korenblit {
2760f947b62cSMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2761f947b62cSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2762f947b62cSMiri Korenblit 	int i;
2763e705c121SKalle Valo 
2764f947b62cSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
2765f947b62cSMiri Korenblit 
2766f947b62cSMiri Korenblit 	mvmvif->ap_assoc_sta_count = 0;
2767f947b62cSMiri Korenblit 
2768f947b62cSMiri Korenblit 	/* must be set before quota calculations */
2769f947b62cSMiri Korenblit 	mvmvif->ap_ibss_active = true;
2770f947b62cSMiri Korenblit 
2771f947b62cSMiri Korenblit 	/* send all the early keys to the device now */
2772f947b62cSMiri Korenblit 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2773f947b62cSMiri Korenblit 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2774f947b62cSMiri Korenblit 
2775f947b62cSMiri Korenblit 		if (!key)
2776f947b62cSMiri Korenblit 			continue;
2777f947b62cSMiri Korenblit 
2778f947b62cSMiri Korenblit 		mvmvif->ap_early_keys[i] = NULL;
2779f947b62cSMiri Korenblit 
2780f947b62cSMiri Korenblit 		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2781f947b62cSMiri Korenblit 		if (*ret)
2782f947b62cSMiri Korenblit 			return true;
2783e705c121SKalle Valo 	}
2784e705c121SKalle Valo 
2785f947b62cSMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2786f947b62cSMiri Korenblit 		iwl_mvm_vif_set_low_latency(mvmvif, true,
2787f947b62cSMiri Korenblit 					    LOW_LATENCY_VIF_TYPE);
2788f947b62cSMiri Korenblit 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2789e705c121SKalle Valo 	}
2790e705c121SKalle Valo 
2791f947b62cSMiri Korenblit 	/* power updated needs to be done before quotas */
2792f947b62cSMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
2793e705c121SKalle Valo 
2794f947b62cSMiri Korenblit 	return false;
2795e705c121SKalle Valo }
2796e705c121SKalle Valo 
2797e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2798ae7ba17bSShaul Triebitz 				 struct ieee80211_vif *vif,
2799b327c84cSGregory Greenman 				 struct ieee80211_bss_conf *link_conf)
2800e705c121SKalle Valo {
2801e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2802e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2803f947b62cSMiri Korenblit 	int ret;
2804e705c121SKalle Valo 
2805e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2806e705c121SKalle Valo 
2807e705c121SKalle Valo 	/*
2808cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2809cdaba917SEmmanuel Grumbach 	 * the beacon interval, which was not known when the AP interface
2810cdaba917SEmmanuel Grumbach 	 * was added.
2811e705c121SKalle Valo 	 */
2812e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2813e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2814e705c121SKalle Valo 
281536cf5377SGregory Greenman 	/* For older devices need to send beacon template before adding mac
281636cf5377SGregory Greenman 	 * context. For the newer, the beacon is a resource that belongs to a
281736cf5377SGregory Greenman 	 * MAC, so need to send beacon template after adding the mac.
281836cf5377SGregory Greenman 	 */
281936cf5377SGregory Greenman 	if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
2820e705c121SKalle Valo 		/* Add the mac context */
2821e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2822e705c121SKalle Valo 		if (ret)
2823e705c121SKalle Valo 			goto out_unlock;
2824e705c121SKalle Valo 
282536cf5377SGregory Greenman 		/* Send the beacon template */
282636cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
282736cf5377SGregory Greenman 		if (ret)
282836cf5377SGregory Greenman 			goto out_unlock;
282936cf5377SGregory Greenman 	} else {
283036cf5377SGregory Greenman 		/* Send the beacon template */
283136cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
283236cf5377SGregory Greenman 		if (ret)
283336cf5377SGregory Greenman 			goto out_unlock;
2834e705c121SKalle Valo 
2835e705c121SKalle Valo 		/* Add the mac context */
2836e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2837e705c121SKalle Valo 		if (ret)
2838e705c121SKalle Valo 			goto out_unlock;
283936cf5377SGregory Greenman 	}
2840e705c121SKalle Valo 
2841e705c121SKalle Valo 	/* Perform the binding */
2842e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2843e705c121SKalle Valo 	if (ret)
2844e705c121SKalle Valo 		goto out_remove;
2845e705c121SKalle Valo 
284663dd5d02SSara Sharon 	/*
284763dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
284863dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
284963dd5d02SSara Sharon 	 * cause assert 0x2b00.
285063dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
285163dd5d02SSara Sharon 	 * the TLV
285263dd5d02SSara Sharon 	 */
285363dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2854ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2855ced19f26SSara Sharon 		if (ret)
2856ced19f26SSara Sharon 			goto out_unbind;
285763dd5d02SSara Sharon 		/*
285863dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
285963dd5d02SSara Sharon 		 * events are added and applied to the scheduler
286063dd5d02SSara Sharon 		 */
2861e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
286263dd5d02SSara Sharon 		if (ret) {
286363dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
286463dd5d02SSara Sharon 			goto out_unbind;
286563dd5d02SSara Sharon 		}
286663dd5d02SSara Sharon 	} else {
286763dd5d02SSara Sharon 		/*
286863dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
286963dd5d02SSara Sharon 		 * events are added and applied to the scheduler
287063dd5d02SSara Sharon 		 */
287175fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2872e705c121SKalle Valo 		if (ret)
287363dd5d02SSara Sharon 			goto out_unbind;
287475fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
287563dd5d02SSara Sharon 		if (ret) {
287663dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
287763dd5d02SSara Sharon 			goto out_unbind;
287863dd5d02SSara Sharon 		}
287963dd5d02SSara Sharon 	}
288026d6c16bSSara Sharon 
2881f947b62cSMiri Korenblit 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
2882f947b62cSMiri Korenblit 		goto out_failed;
2883e705c121SKalle Valo 
2884e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2885e705c121SKalle Valo 	if (ret)
2886f947b62cSMiri Korenblit 		goto out_failed;
2887e705c121SKalle Valo 
2888e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2889e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2890e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2891e705c121SKalle Valo 
2892e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2893e705c121SKalle Valo 
2894e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2895e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2896e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2897e705c121SKalle Valo 
2898fd940de7SAvraham Stern 	iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
2899b73f9a4aSJohannes Berg 
2900e705c121SKalle Valo 	goto out_unlock;
2901e705c121SKalle Valo 
2902f947b62cSMiri Korenblit out_failed:
2903e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2904e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2905e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2906ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2907e705c121SKalle Valo out_unbind:
2908e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2909e705c121SKalle Valo out_remove:
2910e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2911e705c121SKalle Valo out_unlock:
2912e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2913e705c121SKalle Valo 	return ret;
2914e705c121SKalle Valo }
2915e705c121SKalle Valo 
2916ae7ba17bSShaul Triebitz static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
2917ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
2918b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
2919ae7ba17bSShaul Triebitz {
2920b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
2921ae7ba17bSShaul Triebitz }
2922ae7ba17bSShaul Triebitz 
2923ae7ba17bSShaul Triebitz static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
2924e705c121SKalle Valo 			      struct ieee80211_vif *vif)
2925e705c121SKalle Valo {
2926b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
2927ae7ba17bSShaul Triebitz }
2928ae7ba17bSShaul Triebitz 
2929fd1a54c1SMiri Korenblit /* Common part for MLD and non-MLD ops */
2930fd1a54c1SMiri Korenblit void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
2931fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif)
2932ae7ba17bSShaul Triebitz {
2933e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2934e705c121SKalle Valo 
2935fd1a54c1SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
2936e705c121SKalle Valo 
2937fd1a54c1SMiri Korenblit 	iwl_mvm_prepare_mac_removal(mvm, vif);
2938e705c121SKalle Valo 
2939e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2940e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2941e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2942e705c121SKalle Valo 					  &mvmvif->time_event_data);
2943e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2944e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2945e705c121SKalle Valo 	}
2946e705c121SKalle Valo 
2947e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2948e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2949e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2950e705c121SKalle Valo 	}
2951e705c121SKalle Valo 
2952e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2953e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2954e705c121SKalle Valo 
295547242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
295647242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
295747242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
295847242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
295947242744STova Mussai 	}
296047242744STova Mussai 
2961e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2962fd1a54c1SMiri Korenblit }
2963fd1a54c1SMiri Korenblit 
2964fd1a54c1SMiri Korenblit static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2965fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif,
2966fd1a54c1SMiri Korenblit 				 struct ieee80211_bss_conf *link_conf)
2967fd1a54c1SMiri Korenblit {
2968fd1a54c1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2969fd1a54c1SMiri Korenblit 
2970fd1a54c1SMiri Korenblit 	mutex_lock(&mvm->mutex);
2971fd1a54c1SMiri Korenblit 
2972fd1a54c1SMiri Korenblit 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
2973e705c121SKalle Valo 
2974e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2975e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2976e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2977e705c121SKalle Valo 
2978e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2979ced19f26SSara Sharon 
2980be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
2981be82ecd3SAvraham Stern 
2982ced19f26SSara Sharon 	/*
2983ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2984ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2985ced19f26SSara Sharon 	 * cause assert 0x2b00.
2986ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2987ced19f26SSara Sharon 	 * bcast station).
2988ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2989ced19f26SSara Sharon 	 */
2990ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
299126d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2992e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2993ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2994ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2995e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2996e705c121SKalle Valo 
2997e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2998e705c121SKalle Valo 
2999e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3000e705c121SKalle Valo 
3001e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3002e705c121SKalle Valo }
3003e705c121SKalle Valo 
3004ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
3005ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
3006b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
3007ae7ba17bSShaul Triebitz {
3008b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
3009ae7ba17bSShaul Triebitz }
3010ae7ba17bSShaul Triebitz 
3011ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
3012ae7ba17bSShaul Triebitz 			      struct ieee80211_vif *vif)
3013ae7ba17bSShaul Triebitz {
3014b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3015ae7ba17bSShaul Triebitz }
3016ae7ba17bSShaul Triebitz 
3017e705c121SKalle Valo static void
3018e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3019e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3020e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
30217b7090b4SJohannes Berg 				 u64 changes)
3022e705c121SKalle Valo {
3023e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3024e705c121SKalle Valo 
3025e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
3026e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
3027e705c121SKalle Valo 		return;
3028e705c121SKalle Valo 
3029e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3030e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3031e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3032e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3033e705c121SKalle Valo 
3034e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
3035e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
303636cf5377SGregory Greenman 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3037e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
3038e705c121SKalle Valo 
3039b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3040fd940de7SAvraham Stern 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3041b73f9a4aSJohannes Berg 
3042b73f9a4aSJohannes Berg 		if (ret)
3043b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3044b73f9a4aSJohannes Berg 				 ret);
3045b73f9a4aSJohannes Berg 	}
3046b73f9a4aSJohannes Berg 
3047e705c121SKalle Valo }
3048e705c121SKalle Valo 
3049e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3050e705c121SKalle Valo 				     struct ieee80211_vif *vif,
3051e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
30527b7090b4SJohannes Berg 				     u64 changes)
3053e705c121SKalle Valo {
3054a2906ea6SJohannes Berg 	static const struct iwl_mvm_bss_info_changed_ops callbacks = {
3055660eba5aSMiri Korenblit 		.bss_info_changed_sta = iwl_mvm_bss_info_changed_station,
3056660eba5aSMiri Korenblit 		.bss_info_changed_ap_ibss = iwl_mvm_bss_info_changed_ap_ibss,
3057660eba5aSMiri Korenblit 	};
3058660eba5aSMiri Korenblit 
3059660eba5aSMiri Korenblit 	iwl_mvm_bss_info_changed_common(hw, vif, bss_conf, &callbacks,
3060660eba5aSMiri Korenblit 					changes);
3061660eba5aSMiri Korenblit }
3062660eba5aSMiri Korenblit 
3063660eba5aSMiri Korenblit void
3064660eba5aSMiri Korenblit iwl_mvm_bss_info_changed_common(struct ieee80211_hw *hw,
3065660eba5aSMiri Korenblit 				struct ieee80211_vif *vif,
3066660eba5aSMiri Korenblit 				struct ieee80211_bss_conf *bss_conf,
3067a2906ea6SJohannes Berg 				const struct iwl_mvm_bss_info_changed_ops *callbacks,
3068660eba5aSMiri Korenblit 				u64 changes)
3069660eba5aSMiri Korenblit {
3070e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3071e705c121SKalle Valo 
3072e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3073e705c121SKalle Valo 
3074f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3075e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3076e705c121SKalle Valo 
3077e705c121SKalle Valo 	switch (vif->type) {
3078e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3079660eba5aSMiri Korenblit 		callbacks->bss_info_changed_sta(mvm, vif, bss_conf, changes);
3080e705c121SKalle Valo 		break;
3081e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3082e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
3083660eba5aSMiri Korenblit 		callbacks->bss_info_changed_ap_ibss(mvm, vif, bss_conf,
3084660eba5aSMiri Korenblit 						    changes);
3085e705c121SKalle Valo 		break;
308691b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
308791b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
308891b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
308991b08c2dSAviya Erenfeld 		break;
3090e705c121SKalle Valo 	default:
3091e705c121SKalle Valo 		/* shouldn't happen */
3092e705c121SKalle Valo 		WARN_ON_ONCE(1);
3093e705c121SKalle Valo 	}
3094e705c121SKalle Valo 
30959aae43a4SJohannes Berg 	if (changes & BSS_CHANGED_TXPOWER) {
30969aae43a4SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
30979aae43a4SJohannes Berg 				bss_conf->txpower);
30989aae43a4SJohannes Berg 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
30999aae43a4SJohannes Berg 	}
31009aae43a4SJohannes Berg 
3101e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3102e705c121SKalle Valo }
3103e705c121SKalle Valo 
3104cbce62a3SMiri Korenblit int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3105e705c121SKalle Valo 			struct ieee80211_scan_request *hw_req)
3106e705c121SKalle Valo {
3107e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3108e705c121SKalle Valo 	int ret;
3109e705c121SKalle Valo 
3110e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
3111e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3112e705c121SKalle Valo 		return -EINVAL;
3113e705c121SKalle Valo 
3114e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3115e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3116e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3117e705c121SKalle Valo 
3118e705c121SKalle Valo 	return ret;
3119e705c121SKalle Valo }
3120e705c121SKalle Valo 
3121cbce62a3SMiri Korenblit void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3122e705c121SKalle Valo 				struct ieee80211_vif *vif)
3123e705c121SKalle Valo {
3124e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3125e705c121SKalle Valo 
3126e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3127e705c121SKalle Valo 
3128e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3129e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
3130e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
3131e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
3132e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
3133e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
3134e705c121SKalle Valo 	*/
3135e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3136e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3137e705c121SKalle Valo 
3138e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3139e705c121SKalle Valo }
3140e705c121SKalle Valo 
3141cbce62a3SMiri Korenblit void
3142e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3143e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
3144e705c121SKalle Valo 				  int num_frames,
3145e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
3146e705c121SKalle Valo 				  bool more_data)
3147e705c121SKalle Valo {
3148e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3149e705c121SKalle Valo 
3150e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3151e705c121SKalle Valo 
3152e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3153e705c121SKalle Valo 					  tids, more_data, false);
3154e705c121SKalle Valo }
3155e705c121SKalle Valo 
3156cbce62a3SMiri Korenblit void
3157e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3158e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
3159e705c121SKalle Valo 				    int num_frames,
3160e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
3161e705c121SKalle Valo 				    bool more_data)
3162e705c121SKalle Valo {
3163e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3164e705c121SKalle Valo 
31659a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3166e705c121SKalle Valo 
3167e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3168e705c121SKalle Valo 					  tids, more_data, true);
3169e705c121SKalle Valo }
3170e705c121SKalle Valo 
317165e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3172e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
3173e705c121SKalle Valo 				     struct ieee80211_sta *sta)
3174e705c121SKalle Valo {
3175e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3176e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3177e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
3178e705c121SKalle Valo 	int tid;
3179e705c121SKalle Valo 
3180960f864bSJohannes Berg 	/*
3181960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
3182960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
3183960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
3184960f864bSJohannes Berg 	 */
3185960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3186960f864bSJohannes Berg 		return;
3187960f864bSJohannes Berg 
3188e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3189311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3190e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3191e705c121SKalle Valo 
31926862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
31931c17627bSSara Sharon 			continue;
31941c17627bSSara Sharon 
3195e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
3196e705c121SKalle Valo 
3197dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3198e705c121SKalle Valo 			continue;
3199e705c121SKalle Valo 
3200e705c121SKalle Valo 		__set_bit(tid, &tids);
3201e705c121SKalle Valo 	}
3202e705c121SKalle Valo 
3203e705c121SKalle Valo 	switch (cmd) {
3204e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
3205e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3206e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
3207e705c121SKalle Valo 
3208e705c121SKalle Valo 		if (txqs)
3209e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3210e705c121SKalle Valo 		/*
3211e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3212e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
3213e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3214e705c121SKalle Valo 		 */
3215e705c121SKalle Valo 		break;
3216e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
3217c8ee33e1SGregory Greenman 		if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
3218e705c121SKalle Valo 			break;
3219e705c121SKalle Valo 
3220e705c121SKalle Valo 		if (txqs)
3221e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3222e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3223e705c121SKalle Valo 		break;
3224e705c121SKalle Valo 	default:
3225e705c121SKalle Valo 		break;
3226e705c121SKalle Valo 	}
3227e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3228e705c121SKalle Valo }
3229e705c121SKalle Valo 
3230cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3231cbce62a3SMiri Korenblit 			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
323265e25482SJohannes Berg {
323365e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
323465e25482SJohannes Berg }
323565e25482SJohannes Berg 
323665e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
323765e25482SJohannes Berg {
323865e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
323965e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
324065e25482SJohannes Berg 	struct ieee80211_sta *sta;
324165e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
324265e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
324365e25482SJohannes Berg 
3244be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
324565e25482SJohannes Berg 		return;
324665e25482SJohannes Berg 
324765e25482SJohannes Berg 	rcu_read_lock();
3248a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
324965e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
325065e25482SJohannes Berg 		rcu_read_unlock();
325165e25482SJohannes Berg 		return;
325265e25482SJohannes Berg 	}
325365e25482SJohannes Berg 
325465e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
325565e25482SJohannes Berg 
325665e25482SJohannes Berg 	if (!mvmsta->vif ||
325765e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
325865e25482SJohannes Berg 		rcu_read_unlock();
325965e25482SJohannes Berg 		return;
326065e25482SJohannes Berg 	}
326165e25482SJohannes Berg 
326265e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
326365e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
326465e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
326565e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
326665e25482SJohannes Berg 			sta);
326765e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
326865e25482SJohannes Berg 	}
326965e25482SJohannes Berg 
327065e25482SJohannes Berg 	if (sleeping) {
327165e25482SJohannes Berg 		switch (notif->type) {
327265e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
327365e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
327465e25482SJohannes Berg 			break;
327565e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
327665e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
327765e25482SJohannes Berg 			break;
327865e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
327965e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
328065e25482SJohannes Berg 			break;
328165e25482SJohannes Berg 		default:
328265e25482SJohannes Berg 			break;
328365e25482SJohannes Berg 		}
328465e25482SJohannes Berg 	}
328565e25482SJohannes Berg 
328665e25482SJohannes Berg 	rcu_read_unlock();
328765e25482SJohannes Berg }
328865e25482SJohannes Berg 
3289cbce62a3SMiri Korenblit void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3290e705c121SKalle Valo 				struct ieee80211_vif *vif,
3291e705c121SKalle Valo 				struct ieee80211_sta *sta)
3292e705c121SKalle Valo {
3293e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3294e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
329579faae3aSGregory Greenman 	unsigned int link_id;
3296e705c121SKalle Valo 
3297e705c121SKalle Valo 	/*
3298e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
3299e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
3300e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
3301e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
3302e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
3303e705c121SKalle Valo 	 * callback deleted the station.
330479faae3aSGregory Greenman 	 * Since there's mvm->mutex here, no need to have RCU lock for
330579faae3aSGregory Greenman 	 * mvm_sta->link access.
3306e705c121SKalle Valo 	 */
3307e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
330879faae3aSGregory Greenman 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
330979faae3aSGregory Greenman 		struct iwl_mvm_link_sta *link_sta;
331079faae3aSGregory Greenman 		u32 sta_id;
3311e705c121SKalle Valo 
331279faae3aSGregory Greenman 		if (!mvm_sta->link[link_id])
331379faae3aSGregory Greenman 			continue;
331479faae3aSGregory Greenman 
331579faae3aSGregory Greenman 		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
331679faae3aSGregory Greenman 						     lockdep_is_held(&mvm->mutex));
331779faae3aSGregory Greenman 		sta_id = link_sta->sta_id;
3318b8a85a1dSJohannes Berg 		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
33193e75668bSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
332079faae3aSGregory Greenman 					 ERR_PTR(-ENOENT));
3321b8a85a1dSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3322b8a85a1dSJohannes Berg 		}
332379faae3aSGregory Greenman 	}
3324e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3325e705c121SKalle Valo }
3326e705c121SKalle Valo 
3327e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3328e705c121SKalle Valo 				const u8 *bssid)
3329e705c121SKalle Valo {
3330b0ffe455SJohannes Berg 	int i;
3331b0ffe455SJohannes Berg 
3332b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3333b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
3334b0ffe455SJohannes Berg 
3335b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3336b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3337b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
3338b0ffe455SJohannes Berg 	}
3339b0ffe455SJohannes Berg 
3340e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3341e705c121SKalle Valo 		return;
3342e705c121SKalle Valo 
3343c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3344cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3345cee5a882SAvri Altman 		return;
3346cee5a882SAvri Altman 	}
3347cee5a882SAvri Altman 
334811dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
334911dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3350e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3351e705c121SKalle Valo 		return;
3352e705c121SKalle Valo 	}
3353e705c121SKalle Valo 
3354b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3355b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3356b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3357b0ffe455SJohannes Berg 			return;
3358b0ffe455SJohannes Berg 		}
3359b0ffe455SJohannes Berg 	}
3360b0ffe455SJohannes Berg 
3361e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3362e705c121SKalle Valo }
3363e705c121SKalle Valo 
33641e8f1329SGolan Ben-Ami static void
33651e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
33661e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
33671e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
33681e8f1329SGolan Ben-Ami {
33691e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
33701e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
33711e8f1329SGolan Ben-Ami 
33726c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
33736c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
33746c042d75SSara Sharon 	if (!trig)
33751e8f1329SGolan Ben-Ami 		return;
33761e8f1329SGolan Ben-Ami 
33771e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
33781e8f1329SGolan Ben-Ami 
33791e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
33801e8f1329SGolan Ben-Ami 		return;
33811e8f1329SGolan Ben-Ami 
33821e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
33831e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
33841e8f1329SGolan Ben-Ami 		return;
33851e8f1329SGolan Ben-Ami 
33867174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
33871e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
33881e8f1329SGolan Ben-Ami 				peer_addr, action);
33891e8f1329SGolan Ben-Ami }
33901e8f1329SGolan Ben-Ami 
33914f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
33924f58121dSIlan Peer 	bool tolerated;
33934f58121dSIlan Peer };
33944f58121dSIlan Peer 
33954f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
33964f58121dSIlan Peer 						    struct cfg80211_bss *bss,
33974f58121dSIlan Peer 						    void *_data)
33984f58121dSIlan Peer {
33994f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
34006c608cd6SJohannes Berg 	const struct cfg80211_bss_ies *ies;
34014f58121dSIlan Peer 	const struct element *elem;
34024f58121dSIlan Peer 
34036c608cd6SJohannes Berg 	rcu_read_lock();
34046c608cd6SJohannes Berg 	ies = rcu_dereference(bss->ies);
34056c608cd6SJohannes Berg 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
34066c608cd6SJohannes Berg 				  ies->len);
34074f58121dSIlan Peer 
34084f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
34094f58121dSIlan Peer 	    !(elem->data[10] &
34104f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
34114f58121dSIlan Peer 		data->tolerated = false;
34124f58121dSIlan Peer 	}
34136c608cd6SJohannes Berg 	rcu_read_unlock();
34144f58121dSIlan Peer }
34154f58121dSIlan Peer 
34166e1b5956SJohannes Berg static void
34176e1b5956SJohannes Berg iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
34186e1b5956SJohannes Berg 				   struct ieee80211_vif *vif,
34196e1b5956SJohannes Berg 				   unsigned int link_id,
34206e1b5956SJohannes Berg 				   struct ieee80211_bss_conf *link_conf)
34214f58121dSIlan Peer {
34224f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
34234f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
34244f58121dSIlan Peer 		.tolerated = true,
34254f58121dSIlan Peer 	};
34264f58121dSIlan Peer 
34276e1b5956SJohannes Berg 	if (WARN_ON_ONCE(!link_conf->chandef.chan ||
34286e1b5956SJohannes Berg 			 !mvmvif->link[link_id]))
34296e1b5956SJohannes Berg 		return;
34306e1b5956SJohannes Berg 
34316e1b5956SJohannes Berg 	if (!(link_conf->chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
34326e1b5956SJohannes Berg 		mvmvif->link[link_id]->he_ru_2mhz_block = false;
34334f58121dSIlan Peer 		return;
34344f58121dSIlan Peer 	}
34354f58121dSIlan Peer 
34366e1b5956SJohannes Berg 	cfg80211_bss_iter(hw->wiphy, &link_conf->chandef,
34374f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
34384f58121dSIlan Peer 			  &iter_data);
34394f58121dSIlan Peer 
34404f58121dSIlan Peer 	/*
34414f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
34424f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
34434f58121dSIlan Peer 	 */
34446e1b5956SJohannes Berg 	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
34454f58121dSIlan Peer }
34464f58121dSIlan Peer 
3447f7d6ef33SJohannes Berg static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3448f7d6ef33SJohannes Berg 					       struct ieee80211_vif *vif)
3449f7d6ef33SJohannes Berg {
3450f7d6ef33SJohannes Berg 	struct ieee80211_supported_band *sband;
3451f7d6ef33SJohannes Berg 	const struct ieee80211_sta_he_cap *he_cap;
3452f7d6ef33SJohannes Berg 
3453f7d6ef33SJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION)
3454f7d6ef33SJohannes Berg 		return;
3455f7d6ef33SJohannes Berg 
3456f7d6ef33SJohannes Berg 	if (!mvm->cca_40mhz_workaround)
3457f7d6ef33SJohannes Berg 		return;
3458f7d6ef33SJohannes Berg 
3459f7d6ef33SJohannes Berg 	/* decrement and check that we reached zero */
3460f7d6ef33SJohannes Berg 	mvm->cca_40mhz_workaround--;
3461f7d6ef33SJohannes Berg 	if (mvm->cca_40mhz_workaround)
3462f7d6ef33SJohannes Berg 		return;
3463f7d6ef33SJohannes Berg 
3464f7d6ef33SJohannes Berg 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3465f7d6ef33SJohannes Berg 
3466f7d6ef33SJohannes Berg 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3467f7d6ef33SJohannes Berg 
34681ec7291eSJohannes Berg 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3469f7d6ef33SJohannes Berg 
3470f7d6ef33SJohannes Berg 	if (he_cap) {
3471f7d6ef33SJohannes Berg 		/* we know that ours is writable */
34720301bcd5SBjoern A. Zeeb 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3473f7d6ef33SJohannes Berg 
3474f7d6ef33SJohannes Berg 		he->he_cap_elem.phy_cap_info[0] |=
3475f7d6ef33SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3476f7d6ef33SJohannes Berg 	}
3477f7d6ef33SJohannes Berg }
3478f7d6ef33SJohannes Berg 
34796d19a5ebSEmmanuel Grumbach static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
34806d19a5ebSEmmanuel Grumbach 					struct ieee80211_vif *vif,
34816d19a5ebSEmmanuel Grumbach 					struct iwl_mvm_sta *mvm_sta)
34826d19a5ebSEmmanuel Grumbach {
34836d19a5ebSEmmanuel Grumbach #if IS_ENABLED(CONFIG_IWLMEI)
34846d19a5ebSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
34856d19a5ebSEmmanuel Grumbach 	struct iwl_mei_conn_info conn_info = {
3486f276e20bSJohannes Berg 		.ssid_len = vif->cfg.ssid_len,
34876d19a5ebSEmmanuel Grumbach 	};
34886d19a5ebSEmmanuel Grumbach 
34896d19a5ebSEmmanuel Grumbach 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
34906d19a5ebSEmmanuel Grumbach 		return;
34916d19a5ebSEmmanuel Grumbach 
34926d19a5ebSEmmanuel Grumbach 	if (!mvm->mei_registered)
34936d19a5ebSEmmanuel Grumbach 		return;
34946d19a5ebSEmmanuel Grumbach 
34956c07b73eSJohannes Berg 	/* FIXME: MEI needs to be updated for MLO */
34966c07b73eSJohannes Berg 	if (!vif->bss_conf.chandef.chan)
34976c07b73eSJohannes Berg 		return;
34986c07b73eSJohannes Berg 
34996c07b73eSJohannes Berg 	conn_info.channel = vif->bss_conf.chandef.chan->hw_value;
35006c07b73eSJohannes Berg 
35016d19a5ebSEmmanuel Grumbach 	switch (mvm_sta->pairwise_cipher) {
3502e5d3a64eSAvraham Stern 	case WLAN_CIPHER_SUITE_TKIP:
3503e5d3a64eSAvraham Stern 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3504e5d3a64eSAvraham Stern 		break;
35056d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_CCMP:
35066d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
35076d19a5ebSEmmanuel Grumbach 		break;
35086d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP:
35096d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
35106d19a5ebSEmmanuel Grumbach 		break;
35116d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP_256:
35126d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
35136d19a5ebSEmmanuel Grumbach 		break;
35146d19a5ebSEmmanuel Grumbach 	case 0:
35156d19a5ebSEmmanuel Grumbach 		/* open profile */
35166d19a5ebSEmmanuel Grumbach 		break;
35176d19a5ebSEmmanuel Grumbach 	default:
35186d19a5ebSEmmanuel Grumbach 		/* cipher not supported, don't send anything to iwlmei */
35196d19a5ebSEmmanuel Grumbach 		return;
35206d19a5ebSEmmanuel Grumbach 	}
35216d19a5ebSEmmanuel Grumbach 
35226d19a5ebSEmmanuel Grumbach 	switch (mvmvif->rekey_data.akm) {
35236d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_SAE & 0xff:
35246d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
35256d19a5ebSEmmanuel Grumbach 		break;
35266d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_PSK & 0xff:
35276d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
35286d19a5ebSEmmanuel Grumbach 		break;
35296d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_8021X & 0xff:
35306d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
35316d19a5ebSEmmanuel Grumbach 		break;
35326d19a5ebSEmmanuel Grumbach 	case 0:
35336d19a5ebSEmmanuel Grumbach 		/* open profile */
35346d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
35356d19a5ebSEmmanuel Grumbach 		break;
35366d19a5ebSEmmanuel Grumbach 	default:
35376d19a5ebSEmmanuel Grumbach 		/* auth method / AKM not supported */
35386d19a5ebSEmmanuel Grumbach 		/* TODO: All the FT vesions of these? */
35396d19a5ebSEmmanuel Grumbach 		return;
35406d19a5ebSEmmanuel Grumbach 	}
35416d19a5ebSEmmanuel Grumbach 
3542f276e20bSJohannes Berg 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
35436d19a5ebSEmmanuel Grumbach 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
35446d19a5ebSEmmanuel Grumbach 
35456d19a5ebSEmmanuel Grumbach 	/* TODO: add support for collocated AP data */
35466d19a5ebSEmmanuel Grumbach 	iwl_mei_host_associated(&conn_info, NULL);
35476d19a5ebSEmmanuel Grumbach #endif
35486d19a5ebSEmmanuel Grumbach }
35496d19a5ebSEmmanuel Grumbach 
355087f7e243SMiri Korenblit static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
355187f7e243SMiri Korenblit 					    struct ieee80211_vif *vif,
355287f7e243SMiri Korenblit 					    bool force_assoc_off)
355387f7e243SMiri Korenblit {
355487f7e243SMiri Korenblit 	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
355587f7e243SMiri Korenblit }
355687f7e243SMiri Korenblit 
3557e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3558e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3559e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3560e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3561e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3562e705c121SKalle Valo {
3563a2906ea6SJohannes Berg 	static const struct iwl_mvm_sta_state_ops callbacks = {
356487f7e243SMiri Korenblit 		.add_sta = iwl_mvm_add_sta,
356587f7e243SMiri Korenblit 		.update_sta = iwl_mvm_update_sta,
356687f7e243SMiri Korenblit 		.rm_sta = iwl_mvm_rm_sta,
356787f7e243SMiri Korenblit 		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
356887f7e243SMiri Korenblit 	};
356987f7e243SMiri Korenblit 
357087f7e243SMiri Korenblit 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
357187f7e243SMiri Korenblit 					    &callbacks);
357287f7e243SMiri Korenblit }
357387f7e243SMiri Korenblit 
357457974a55SGregory Greenman /* FIXME: temporary making two assumptions in all sta handling functions:
357557974a55SGregory Greenman  *	(1) when setting sta state, the link exists and protected
357657974a55SGregory Greenman  *	(2) if a link is valid in sta then it's valid in vif (can
357757974a55SGregory Greenman  *	use same index in the link array)
357857974a55SGregory Greenman  */
3579f53be9c4SGregory Greenman static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3580f53be9c4SGregory Greenman 					   struct ieee80211_vif *vif,
358115d41834SJohannes Berg 					   struct ieee80211_sta *sta)
3582f53be9c4SGregory Greenman {
3583f53be9c4SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3584f53be9c4SGregory Greenman 	unsigned int link_id;
358557974a55SGregory Greenman 
3586f53be9c4SGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3587f53be9c4SGregory Greenman 		struct ieee80211_bss_conf *conf =
3588a705a782SJohannes Berg 			link_conf_dereference_check(vif, link_id);
3589f53be9c4SGregory Greenman 		struct ieee80211_link_sta *link_sta =
3590a705a782SJohannes Berg 			link_sta_dereference_check(sta, link_id);
3591f53be9c4SGregory Greenman 
3592f53be9c4SGregory Greenman 		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3593f53be9c4SGregory Greenman 			continue;
3594f53be9c4SGregory Greenman 
3595c45217bdSJohannes Berg 		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
359615d41834SJohannes Berg 				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3597f53be9c4SGregory Greenman 	}
3598f53be9c4SGregory Greenman }
359957974a55SGregory Greenman 
360057974a55SGregory Greenman #define IWL_MVM_MIN_BEACON_INTERVAL_TU 16
360157974a55SGregory Greenman 
360257974a55SGregory Greenman static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
360357974a55SGregory Greenman 				      struct ieee80211_vif *vif,
360457974a55SGregory Greenman 				      struct ieee80211_sta *sta)
360557974a55SGregory Greenman {
3606207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3607207be64fSMiri Korenblit 	unsigned int link_id;
360857974a55SGregory Greenman 
360957974a55SGregory Greenman 	/* Beacon interval check - firmware will crash if the beacon
361057974a55SGregory Greenman 	 * interval is less than 16. We can't avoid connecting at all,
361157974a55SGregory Greenman 	 * so refuse the station state change, this will cause mac80211
361257974a55SGregory Greenman 	 * to abandon attempts to connect to this AP, and eventually
361357974a55SGregory Greenman 	 * wpa_s will blocklist the AP...
361457974a55SGregory Greenman 	 */
361557974a55SGregory Greenman 
3616207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
361757974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3618207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
361957974a55SGregory Greenman 
3620207be64fSMiri Korenblit 		if (!link_conf)
362157974a55SGregory Greenman 			continue;
362257974a55SGregory Greenman 
362357974a55SGregory Greenman 		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
362457974a55SGregory Greenman 			IWL_ERR(mvm,
362557974a55SGregory Greenman 				"Beacon interval %d for AP %pM is too small\n",
362657974a55SGregory Greenman 				link_conf->beacon_int, link_sta->addr);
362757974a55SGregory Greenman 			return false;
362857974a55SGregory Greenman 		}
362957974a55SGregory Greenman 
363057974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
363157974a55SGregory Greenman 	}
363257974a55SGregory Greenman 
363357974a55SGregory Greenman 	return true;
363457974a55SGregory Greenman }
363557974a55SGregory Greenman 
363657974a55SGregory Greenman static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
363757974a55SGregory Greenman 				       struct ieee80211_vif *vif,
363857974a55SGregory Greenman 				       struct ieee80211_sta *sta,
363957974a55SGregory Greenman 				       bool is_sta)
364057974a55SGregory Greenman {
364157974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3642207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3643207be64fSMiri Korenblit 	unsigned int link_id;
364457974a55SGregory Greenman 
3645207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
364657974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3647207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
364857974a55SGregory Greenman 
3649207be64fSMiri Korenblit 		if (!link_conf || !mvmvif->link[link_id])
365057974a55SGregory Greenman 			continue;
365157974a55SGregory Greenman 
365257974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
365357974a55SGregory Greenman 
365457974a55SGregory Greenman 		if (is_sta) {
3655207be64fSMiri Korenblit 			mvmvif->link[link_id]->he_ru_2mhz_block = false;
365657974a55SGregory Greenman 			if (link_sta->he_cap.has_he)
3657207be64fSMiri Korenblit 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3658207be64fSMiri Korenblit 								   link_id,
36596e1b5956SJohannes Berg 								   link_conf);
366057974a55SGregory Greenman 		}
366157974a55SGregory Greenman 	}
366257974a55SGregory Greenman }
366357974a55SGregory Greenman 
366457974a55SGregory Greenman static int
366557974a55SGregory Greenman iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
366657974a55SGregory Greenman 				   struct ieee80211_vif *vif,
366757974a55SGregory Greenman 				   struct ieee80211_sta *sta,
3668a2906ea6SJohannes Berg 				   const struct iwl_mvm_sta_state_ops *callbacks)
366957974a55SGregory Greenman {
36700c9a8f90SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3671207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
367257974a55SGregory Greenman 	unsigned int i;
367357974a55SGregory Greenman 	int ret;
367457974a55SGregory Greenman 
367557974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
367657974a55SGregory Greenman 
367757974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_STATION &&
367857974a55SGregory Greenman 	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
367957974a55SGregory Greenman 		return -EINVAL;
368057974a55SGregory Greenman 
368157974a55SGregory Greenman 	if (sta->tdls &&
368257974a55SGregory Greenman 	    (vif->p2p ||
368357974a55SGregory Greenman 	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
368457974a55SGregory Greenman 	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
368557974a55SGregory Greenman 		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
368657974a55SGregory Greenman 		return -EBUSY;
368757974a55SGregory Greenman 	}
368857974a55SGregory Greenman 
368957974a55SGregory Greenman 	ret = callbacks->add_sta(mvm, vif, sta);
369057974a55SGregory Greenman 	if (sta->tdls && ret == 0) {
369157974a55SGregory Greenman 		iwl_mvm_recalc_tdls_state(mvm, vif, true);
369257974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
369357974a55SGregory Greenman 					   NL80211_TDLS_SETUP);
369457974a55SGregory Greenman 	}
369557974a55SGregory Greenman 
3696207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, i)
369757974a55SGregory Greenman 		link_sta->agg.max_rc_amsdu_len = 1;
3698207be64fSMiri Korenblit 
369957974a55SGregory Greenman 	ieee80211_sta_recalc_aggregates(sta);
370057974a55SGregory Greenman 
37010c9a8f90SJohannes Berg 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
37020c9a8f90SJohannes Berg 		mvmvif->ap_sta = sta;
37030c9a8f90SJohannes Berg 
370457974a55SGregory Greenman 	return 0;
370557974a55SGregory Greenman }
370657974a55SGregory Greenman 
370757974a55SGregory Greenman static int
370857974a55SGregory Greenman iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
370957974a55SGregory Greenman 				struct iwl_mvm *mvm,
371057974a55SGregory Greenman 				struct ieee80211_vif *vif,
371157974a55SGregory Greenman 				struct ieee80211_sta *sta,
3712a2906ea6SJohannes Berg 				const struct iwl_mvm_sta_state_ops *callbacks)
371357974a55SGregory Greenman {
371457974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
371557974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3716207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3717207be64fSMiri Korenblit 	unsigned int link_id;
371857974a55SGregory Greenman 
371957974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
372057974a55SGregory Greenman 
372157974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_AP) {
372257974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
372357974a55SGregory Greenman 		mvmvif->ap_assoc_sta_count++;
372457974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
372557974a55SGregory Greenman 
372657974a55SGregory Greenman 		/* since the below is not for MLD API, it's ok to use
372757974a55SGregory Greenman 		 * the default bss_conf
372857974a55SGregory Greenman 		 */
372957974a55SGregory Greenman 		if (!mvm->mld_api_is_used &&
373057974a55SGregory Greenman 		    ((vif->bss_conf.he_support &&
373157974a55SGregory Greenman 		      !iwlwifi_mod_params.disable_11ax) ||
373257974a55SGregory Greenman 		    (vif->bss_conf.eht_support &&
373357974a55SGregory Greenman 		     !iwlwifi_mod_params.disable_11be)))
373457974a55SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
373557974a55SGregory Greenman 	} else if (vif->type == NL80211_IFTYPE_STATION) {
373657974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
373757974a55SGregory Greenman 
373857974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
373957974a55SGregory Greenman 
374057974a55SGregory Greenman 		if (!mvm->mld_api_is_used)
374157974a55SGregory Greenman 			goto out;
374257974a55SGregory Greenman 
3743207be64fSMiri Korenblit 		for_each_sta_active_link(vif, sta, link_sta, link_id) {
374457974a55SGregory Greenman 			struct ieee80211_bss_conf *link_conf =
3745207be64fSMiri Korenblit 				link_conf_dereference_protected(vif, link_id);
374657974a55SGregory Greenman 
374757974a55SGregory Greenman 			if (WARN_ON(!link_conf))
374857974a55SGregory Greenman 				return -EINVAL;
3749207be64fSMiri Korenblit 			if (!mvmvif->link[link_id])
3750f14ad95aSJohannes Berg 				continue;
375157974a55SGregory Greenman 
375257974a55SGregory Greenman 			iwl_mvm_link_changed(mvm, vif, link_conf,
375357974a55SGregory Greenman 					     LINK_CONTEXT_MODIFY_ALL &
375457974a55SGregory Greenman 					     ~LINK_CONTEXT_MODIFY_ACTIVE,
375557974a55SGregory Greenman 					     true);
375657974a55SGregory Greenman 		}
375757974a55SGregory Greenman 	}
375857974a55SGregory Greenman 
375957974a55SGregory Greenman out:
376015d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
376157974a55SGregory Greenman 
376257974a55SGregory Greenman 	return callbacks->update_sta(mvm, vif, sta);
376357974a55SGregory Greenman }
376457974a55SGregory Greenman 
376557974a55SGregory Greenman static int
376657974a55SGregory Greenman iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
376757974a55SGregory Greenman 				      struct ieee80211_vif *vif,
376857974a55SGregory Greenman 				      struct ieee80211_sta *sta,
3769a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
377057974a55SGregory Greenman {
377157974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
377257974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
377357974a55SGregory Greenman 
377457974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
377557974a55SGregory Greenman 
377657974a55SGregory Greenman 	/* we don't support TDLS during DCM */
377757974a55SGregory Greenman 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
377857974a55SGregory Greenman 		iwl_mvm_teardown_tdls_peers(mvm);
377957974a55SGregory Greenman 
378057974a55SGregory Greenman 	if (sta->tdls) {
378157974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
378257974a55SGregory Greenman 					   NL80211_TDLS_ENABLE_LINK);
378357974a55SGregory Greenman 	} else {
378457974a55SGregory Greenman 		/* enable beacon filtering */
378557974a55SGregory Greenman 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
378657974a55SGregory Greenman 
378757974a55SGregory Greenman 		mvmvif->authorized = 1;
378857974a55SGregory Greenman 
378957974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
379057974a55SGregory Greenman 		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
379157974a55SGregory Greenman 	}
379257974a55SGregory Greenman 
379315d41834SJohannes Berg 	mvm_sta->authorized = true;
379415d41834SJohannes Berg 
379515d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
379657974a55SGregory Greenman 
379757974a55SGregory Greenman 	return 0;
379857974a55SGregory Greenman }
379957974a55SGregory Greenman 
380057974a55SGregory Greenman static int
380157974a55SGregory Greenman iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
380257974a55SGregory Greenman 				      struct ieee80211_vif *vif,
380357974a55SGregory Greenman 				      struct ieee80211_sta *sta,
3804a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
380557974a55SGregory Greenman {
380657974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
380715d41834SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
380857974a55SGregory Greenman 
380957974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
381057974a55SGregory Greenman 
381115d41834SJohannes Berg 	mvmsta->authorized = false;
381215d41834SJohannes Berg 
381357974a55SGregory Greenman 	/* once we move into assoc state, need to update rate scale to
381457974a55SGregory Greenman 	 * disable using wide bandwidth
381557974a55SGregory Greenman 	 */
381615d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
381757974a55SGregory Greenman 
381857974a55SGregory Greenman 	if (!sta->tdls) {
381957974a55SGregory Greenman 		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
382057974a55SGregory Greenman 		 * yet to avoid sending high prio again for a little
382157974a55SGregory Greenman 		 * time.
382257974a55SGregory Greenman 		 */
382357974a55SGregory Greenman 		mvmvif->authorized = 0;
382457974a55SGregory Greenman 
382557974a55SGregory Greenman 		/* disable beacon filtering */
3826fccf5ff1SJohannes Berg 		iwl_mvm_disable_beacon_filter(mvm, vif, 0);
382757974a55SGregory Greenman 	}
382857974a55SGregory Greenman 
382957974a55SGregory Greenman 	return 0;
383057974a55SGregory Greenman }
383157974a55SGregory Greenman 
383287f7e243SMiri Korenblit /* Common part for MLD and non-MLD modes */
383387f7e243SMiri Korenblit int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
383487f7e243SMiri Korenblit 				 struct ieee80211_vif *vif,
383587f7e243SMiri Korenblit 				 struct ieee80211_sta *sta,
383687f7e243SMiri Korenblit 				 enum ieee80211_sta_state old_state,
383787f7e243SMiri Korenblit 				 enum ieee80211_sta_state new_state,
3838a2906ea6SJohannes Berg 				 const struct iwl_mvm_sta_state_ops *callbacks)
383987f7e243SMiri Korenblit {
3840e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3841e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
38426ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3843de107600SJohannes Berg 	struct ieee80211_link_sta *link_sta;
384457974a55SGregory Greenman 	unsigned int link_id;
3845e705c121SKalle Valo 	int ret;
3846e705c121SKalle Valo 
3847e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3848e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3849e705c121SKalle Valo 
385024afba76SLiad Kaufman 	/*
385124afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
385224afba76SLiad Kaufman 	 *
385324afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
385424afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
385524afba76SLiad Kaufman 	 * the queues.
385624afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
385724afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
385824afba76SLiad Kaufman 	 * of.
385924afba76SLiad Kaufman 	 *
386024afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
386124afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
386224afba76SLiad Kaufman 	 */
386324afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3864c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
386524afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
386624afba76SLiad Kaufman 
386724afba76SLiad Kaufman 		/*
386824afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
386924afba76SLiad Kaufman 		 * worker will already remove it if it was on
387024afba76SLiad Kaufman 		 */
3871f7d6ef33SJohannes Berg 
3872f7d6ef33SJohannes Berg 		/*
3873f7d6ef33SJohannes Berg 		 * Additionally, reset the 40 MHz capability if we disconnected
3874f7d6ef33SJohannes Berg 		 * from the AP now.
3875f7d6ef33SJohannes Berg 		 */
3876f7d6ef33SJohannes Berg 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
3877783336b0SJohannes Berg 
3878783336b0SJohannes Berg 		/* Also free dup data just in case any assertions below fail */
3879783336b0SJohannes Berg 		kfree(mvm_sta->dup_data);
388024afba76SLiad Kaufman 	}
388124afba76SLiad Kaufman 
3882e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
388357974a55SGregory Greenman 
388457974a55SGregory Greenman 	/* this would be a mac80211 bug ... but don't crash */
3885de107600SJohannes Berg 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
388657974a55SGregory Greenman 		if (WARN_ON_ONCE(!mvmvif->link[link_id]->phy_ctxt)) {
388757974a55SGregory Greenman 			mutex_unlock(&mvm->mutex);
388857974a55SGregory Greenman 			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
388957974a55SGregory Greenman 					&mvm->status) ? 0 : -EINVAL;
389057974a55SGregory Greenman 		}
389157974a55SGregory Greenman 	}
389257974a55SGregory Greenman 
38936ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3894d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
38956ea29ce5SJohannes Berg 
3896e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3897e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
389857974a55SGregory Greenman 		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
389957974a55SGregory Greenman 							 callbacks);
390057974a55SGregory Greenman 		if (ret < 0)
3901e705c121SKalle Valo 			goto out_unlock;
3902e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3903e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3904e705c121SKalle Valo 		/*
3905e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3906e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3907e705c121SKalle Valo 		 */
3908e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3909e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3910e705c121SKalle Valo 		ret = 0;
3911e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3912e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
391357974a55SGregory Greenman 		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
391457974a55SGregory Greenman 						      callbacks);
3915e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3916e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
391757974a55SGregory Greenman 		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
391857974a55SGregory Greenman 							    callbacks);
3919e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3920e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
392157974a55SGregory Greenman 		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
392257974a55SGregory Greenman 							    callbacks);
3923e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3924e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
39258be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
39268be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
392787f7e243SMiri Korenblit 			callbacks->mac_ctxt_changed(mvm, vif, false);
3928d5d8ee52SEmmanuel Grumbach 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3929cf7a7457SJohannes Berg 			iwl_mvm_stop_session_protection(mvm, vif);
3930e705c121SKalle Valo 		ret = 0;
3931e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3932e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3933e705c121SKalle Valo 		ret = 0;
3934e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3935e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
39360c9a8f90SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
3937d5d8ee52SEmmanuel Grumbach 			iwl_mvm_stop_session_protection(mvm, vif);
39380c9a8f90SJohannes Berg 			mvmvif->ap_sta = NULL;
39390c9a8f90SJohannes Berg 		}
394087f7e243SMiri Korenblit 		ret = callbacks->rm_sta(mvm, vif, sta);
39411e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3942e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
39431e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
39441e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
39451e8f1329SGolan Ben-Ami 		}
394634a880d8SLiad Kaufman 
394744135b7cSIlan Peer 		if (unlikely(ret &&
394844135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
394944135b7cSIlan Peer 				      &mvm->status)))
395044135b7cSIlan Peer 			ret = 0;
3951e705c121SKalle Valo 	} else {
3952e705c121SKalle Valo 		ret = -EIO;
3953e705c121SKalle Valo 	}
3954e705c121SKalle Valo  out_unlock:
3955e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3956e705c121SKalle Valo 
3957e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3958e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3959e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3960e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3961e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3962e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3963e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3964e705c121SKalle Valo 	}
3965e705c121SKalle Valo 
3966e705c121SKalle Valo 	return ret;
3967e705c121SKalle Valo }
3968e705c121SKalle Valo 
3969cbce62a3SMiri Korenblit int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3970e705c121SKalle Valo {
3971e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3972e705c121SKalle Valo 
3973e705c121SKalle Valo 	mvm->rts_threshold = value;
3974e705c121SKalle Valo 
3975e705c121SKalle Valo 	return 0;
3976e705c121SKalle Valo }
3977e705c121SKalle Valo 
3978cbce62a3SMiri Korenblit void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3979e705c121SKalle Valo 			   struct ieee80211_sta *sta, u32 changed)
3980e705c121SKalle Valo {
3981e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3982dcfe3b10SJohannes Berg 
3983dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
3984dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3985dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
398615d41834SJohannes Berg 		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3987e705c121SKalle Valo 
3988e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
3989e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
3990e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
3991e705c121SKalle Valo }
3992e705c121SKalle Valo 
3993e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3994b3e2130bSJohannes Berg 			       struct ieee80211_vif *vif,
3995b3e2130bSJohannes Berg 			       unsigned int link_id, u16 ac,
3996e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
3997e705c121SKalle Valo {
3998e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3999e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4000e705c121SKalle Valo 
4001650cadb7SGregory Greenman 	mvmvif->deflink.queue_params[ac] = *params;
4002e705c121SKalle Valo 
4003e705c121SKalle Valo 	/*
4004e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
4005e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
4006e705c121SKalle Valo 	 */
4007e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
4008e705c121SKalle Valo 		int ret;
4009e705c121SKalle Valo 
4010e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
4011e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4012e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
4013e705c121SKalle Valo 		return ret;
4014e705c121SKalle Valo 	}
4015e705c121SKalle Valo 	return 0;
4016e705c121SKalle Valo }
4017e705c121SKalle Valo 
4018cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4019d4e36e55SIlan Peer 				struct ieee80211_vif *vif,
402015fae341SJohannes Berg 				struct ieee80211_prep_tx_info *info)
4021e705c121SKalle Valo {
4022e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4023d4e36e55SIlan Peer 
4024e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4025af84ac57SJohannes Berg 	iwl_mvm_protect_assoc(mvm, vif, info->duration);
4026e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4027e705c121SKalle Valo }
4028e705c121SKalle Valo 
4029cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
40306b1259d1SJohannes Berg 				 struct ieee80211_vif *vif,
40316b1259d1SJohannes Berg 				 struct ieee80211_prep_tx_info *info)
40326b1259d1SJohannes Berg {
40336b1259d1SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
40346b1259d1SJohannes Berg 
40356b1259d1SJohannes Berg 	/* for successful cases (auth/assoc), don't cancel session protection */
40366b1259d1SJohannes Berg 	if (info->success)
40376b1259d1SJohannes Berg 		return;
40386b1259d1SJohannes Berg 
40396b1259d1SJohannes Berg 	mutex_lock(&mvm->mutex);
40406b1259d1SJohannes Berg 	iwl_mvm_stop_session_protection(mvm, vif);
40416b1259d1SJohannes Berg 	mutex_unlock(&mvm->mutex);
40426b1259d1SJohannes Berg }
40436b1259d1SJohannes Berg 
4044cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4045e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4046e705c121SKalle Valo 				 struct cfg80211_sched_scan_request *req,
4047e705c121SKalle Valo 				 struct ieee80211_scan_ies *ies)
4048e705c121SKalle Valo {
4049e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4050e705c121SKalle Valo 
4051e705c121SKalle Valo 	int ret;
4052e705c121SKalle Valo 
4053e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4054e705c121SKalle Valo 
4055f276e20bSJohannes Berg 	if (!vif->cfg.idle) {
4056e705c121SKalle Valo 		ret = -EBUSY;
4057e705c121SKalle Valo 		goto out;
4058e705c121SKalle Valo 	}
4059e705c121SKalle Valo 
4060e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4061e705c121SKalle Valo 
4062e705c121SKalle Valo out:
4063e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4064e705c121SKalle Valo 	return ret;
4065e705c121SKalle Valo }
4066e705c121SKalle Valo 
4067cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4068e705c121SKalle Valo 				struct ieee80211_vif *vif)
4069e705c121SKalle Valo {
4070e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4071e705c121SKalle Valo 	int ret;
4072e705c121SKalle Valo 
4073e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4074e705c121SKalle Valo 
4075e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
4076e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
4077e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
4078e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
4079e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4080e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
4081e705c121SKalle Valo 	 * not running.
4082e705c121SKalle Valo 	*/
4083e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4084e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
4085e705c121SKalle Valo 		return 0;
4086e705c121SKalle Valo 	}
4087e705c121SKalle Valo 
4088e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4089e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4090e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
4091e705c121SKalle Valo 
4092e705c121SKalle Valo 	return ret;
4093e705c121SKalle Valo }
4094e705c121SKalle Valo 
40956569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4096e705c121SKalle Valo 				 enum set_key_cmd cmd,
4097e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4098e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4099e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
4100e705c121SKalle Valo {
4101c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4102e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
410346c7b05aSEmmanuel Grumbach 	struct iwl_mvm_sta *mvmsta = NULL;
4104d066a530SJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn = NULL;
4105f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
41065c75a208SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
41075c75a208SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4108c56e00a3SJohannes Berg 	int ret, i;
41094615fd15SEmmanuel Grumbach 	u8 key_offset;
4110e705c121SKalle Valo 
411146c7b05aSEmmanuel Grumbach 	if (sta)
41126d19a5ebSEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
41136d19a5ebSEmmanuel Grumbach 
4114e705c121SKalle Valo 	switch (key->cipher) {
4115e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
4116286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
4117e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
41181ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
41197f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
41207f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
41217f768ad5SDavid Spinadel 		} else {
41227f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
41237f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
41247f768ad5SDavid Spinadel 		}
4125e705c121SKalle Valo 		break;
4126e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
41272a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
41282a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
412985aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
4130e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4131e705c121SKalle Valo 		break;
4132e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
41338e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
41348e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4135e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4136e705c121SKalle Valo 		break;
4137e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
4138e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
4139475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
4140e705c121SKalle Valo 			break;
4141475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
4142475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
4143475c6bdeSJohannes Berg 		/* support HW crypto on TX */
4144475c6bdeSJohannes Berg 		return 0;
4145e705c121SKalle Valo 	default:
4146e705c121SKalle Valo 		return -EOPNOTSUPP;
4147e705c121SKalle Valo 	}
4148e705c121SKalle Valo 
4149e705c121SKalle Valo 	switch (cmd) {
4150e705c121SKalle Valo 	case SET_KEY:
4151a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4152a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4153b1fdc250SJohannes Berg 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4154b1fdc250SJohannes Berg 					   key);
4155b1fdc250SJohannes Berg 
4156e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4157e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4158e705c121SKalle Valo 			/*
4159e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
4160e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
4161e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
41628e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
4163a5de7de7SJohannes Berg 			 *
4164a5de7de7SJohannes Berg 			 * Except, of course, beacon protection - it must be
4165a5de7de7SJohannes Berg 			 * offloaded since we just set a beacon template.
4166e705c121SKalle Valo 			 */
4167a5de7de7SJohannes Berg 			if (keyidx < 6 &&
4168a5de7de7SJohannes Berg 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
41698e160ab8SAyala Beker 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4170a5de7de7SJohannes Berg 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
417181279c49SJohannes Berg 				ret = -EOPNOTSUPP;
4172a1c2ff30SAndrei Otcheretianski 				break;
4173a1c2ff30SAndrei Otcheretianski 			}
417485aeb58cSDavid Spinadel 
417585aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
417685aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
417785aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
4178e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
4179a1c2ff30SAndrei Otcheretianski 				ret = 0;
4180e705c121SKalle Valo 				break;
4181e705c121SKalle Valo 			}
4182c56e00a3SJohannes Berg 
4183c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
4184c56e00a3SJohannes Berg 				for (i = 0;
4185c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4186c56e00a3SJohannes Berg 				     i++) {
4187c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
4188c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
4189c56e00a3SJohannes Berg 						break;
4190c56e00a3SJohannes Berg 					}
4191c56e00a3SJohannes Berg 				}
4192c56e00a3SJohannes Berg 
4193c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4194c56e00a3SJohannes Berg 					ret = -ENOSPC;
4195a1c2ff30SAndrei Otcheretianski 				else
4196a1c2ff30SAndrei Otcheretianski 					ret = 0;
4197c56e00a3SJohannes Berg 
4198c56e00a3SJohannes Berg 				break;
4199c56e00a3SJohannes Berg 			}
420085aeb58cSDavid Spinadel 		}
4201e705c121SKalle Valo 
4202e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
4203e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
4204e705c121SKalle Valo 		 */
4205e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4206e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4207e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
4208e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
4209e705c121SKalle Valo 			ret = 0;
4210e705c121SKalle Valo 			break;
4211e705c121SKalle Valo 		}
4212e705c121SKalle Valo 
4213f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
421446c7b05aSEmmanuel Grumbach 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4215f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4216f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
42172a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
42182a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4219f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
4220f5e28eacSJohannes Berg 			int tid, q;
4221f5e28eacSJohannes Berg 
4222f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4223acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4224acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
4225f5e28eacSJohannes Berg 					 GFP_KERNEL);
4226f5e28eacSJohannes Berg 			if (!ptk_pn) {
4227f5e28eacSJohannes Berg 				ret = -ENOMEM;
4228f5e28eacSJohannes Berg 				break;
4229f5e28eacSJohannes Berg 			}
4230f5e28eacSJohannes Berg 
4231f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4232f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
4233f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
4234f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
4235f5e28eacSJohannes Berg 					       seq.ccmp.pn,
4236f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
4237f5e28eacSJohannes Berg 			}
4238f5e28eacSJohannes Berg 
4239f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4240f5e28eacSJohannes Berg 		}
4241f5e28eacSJohannes Berg 
42424615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
42434615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
42444615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
42454615fd15SEmmanuel Grumbach 		else
42464615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
42474615fd15SEmmanuel Grumbach 
424846c7b05aSEmmanuel Grumbach 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
42496d19a5ebSEmmanuel Grumbach 			mvmsta->pairwise_cipher = key->cipher;
42506d19a5ebSEmmanuel Grumbach 
4251a5de7de7SJohannes Berg 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4252a5de7de7SJohannes Berg 				   sta ? sta->addr : NULL, key->keyidx);
42535c75a208SJohannes Berg 
42545c75a208SJohannes Berg 		if (sec_key_ver)
42555c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
42565c75a208SJohannes Berg 		else
42574615fd15SEmmanuel Grumbach 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
42585c75a208SJohannes Berg 
4259e705c121SKalle Valo 		if (ret) {
4260e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
4261475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
4262d066a530SJohannes Berg 			if (ptk_pn) {
4263d066a530SJohannes Berg 				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4264d066a530SJohannes Berg 				kfree(ptk_pn);
4265d066a530SJohannes Berg 			}
4266e705c121SKalle Valo 			/*
4267e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
4268475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
4269475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
4270475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
4271e705c121SKalle Valo 			 */
4272475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
4273475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
4274475c6bdeSJohannes Berg 			else
4275e705c121SKalle Valo 				ret = 0;
4276e705c121SKalle Valo 		}
4277e705c121SKalle Valo 
4278e705c121SKalle Valo 		break;
4279e705c121SKalle Valo 	case DISABLE_KEY:
4280a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4281a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4282b1fdc250SJohannes Berg 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4283b1fdc250SJohannes Berg 					 NULL);
4284b1fdc250SJohannes Berg 
4285c56e00a3SJohannes Berg 		ret = -ENOENT;
4286c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4287c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
4288c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
4289c56e00a3SJohannes Berg 				ret = 0;
4290c56e00a3SJohannes Berg 			}
4291c56e00a3SJohannes Berg 		}
4292c56e00a3SJohannes Berg 
4293c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
4294c56e00a3SJohannes Berg 		if (ret == 0)
4295c56e00a3SJohannes Berg 			break;
4296c56e00a3SJohannes Berg 
4297e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4298e705c121SKalle Valo 			ret = 0;
4299e705c121SKalle Valo 			break;
4300e705c121SKalle Valo 		}
4301e705c121SKalle Valo 
430246c7b05aSEmmanuel Grumbach 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4303f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4304f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
43052a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
43062a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4307f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
4308f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
4309f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
4310f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4311f5e28eacSJohannes Berg 			if (ptk_pn)
4312f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
4313f5e28eacSJohannes Berg 		}
4314f5e28eacSJohannes Berg 
4315e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
43165c75a208SJohannes Berg 		if (sec_key_ver)
43175c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
43185c75a208SJohannes Berg 		else
4319e705c121SKalle Valo 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4320e705c121SKalle Valo 		break;
4321e705c121SKalle Valo 	default:
4322e705c121SKalle Valo 		ret = -EINVAL;
4323e705c121SKalle Valo 	}
4324e705c121SKalle Valo 
43256569e7d3SJohannes Berg 	return ret;
43266569e7d3SJohannes Berg }
43276569e7d3SJohannes Berg 
4328cbce62a3SMiri Korenblit int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4329cbce62a3SMiri Korenblit 			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
43306569e7d3SJohannes Berg 			struct ieee80211_key_conf *key)
43316569e7d3SJohannes Berg {
43326569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
43336569e7d3SJohannes Berg 	int ret;
43346569e7d3SJohannes Berg 
43356569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
43366569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4337e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
43386569e7d3SJohannes Berg 
4339e705c121SKalle Valo 	return ret;
4340e705c121SKalle Valo }
4341e705c121SKalle Valo 
4342cbce62a3SMiri Korenblit void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4343e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4344e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
4345e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4346e705c121SKalle Valo 				 u32 iv32, u16 *phase1key)
4347e705c121SKalle Valo {
4348e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4349e705c121SKalle Valo 
4350e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4351e705c121SKalle Valo 		return;
4352e705c121SKalle Valo 
4353e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4354e705c121SKalle Valo }
4355e705c121SKalle Valo 
4356e705c121SKalle Valo 
4357e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4358e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
4359e705c121SKalle Valo {
4360e705c121SKalle Valo 	struct iwl_mvm *mvm =
4361e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
4362e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
4363e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
4364e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
4365e705c121SKalle Valo 
4366e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4367e705c121SKalle Valo 		return true;
4368e705c121SKalle Valo 
4369e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4370e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4371e705c121SKalle Valo 		return true;
4372e705c121SKalle Valo 	}
4373e705c121SKalle Valo 
4374e705c121SKalle Valo 	resp = (void *)pkt->data;
4375e705c121SKalle Valo 
4376e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
4377b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4378e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
4379e705c121SKalle Valo 
4380e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4381e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4382e705c121SKalle Valo 		     te_data->uid);
4383e705c121SKalle Valo 
4384e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4385e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4386e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4387e705c121SKalle Valo 
4388e705c121SKalle Valo 	return true;
4389e705c121SKalle Valo }
4390e705c121SKalle Valo 
4391dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
4392dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
4393dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
4394dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
4395dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
4396e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4397e705c121SKalle Valo 				    struct ieee80211_channel *channel,
4398e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4399e705c121SKalle Valo 				    int duration)
4400e705c121SKalle Valo {
4401afc1e3b4SAvraham Stern 	int res;
4402e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4403e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4404e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4405e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
4406dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
4407dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
4408dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
4409e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
4410e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4411e705c121SKalle Valo 		.id_and_color =
4412e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4413e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4414e705c121SKalle Valo 	};
441557e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
441657e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
441757e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
441857e861d9SDavid Spinadel 
441957e861d9SDavid Spinadel 	/* Set the channel info data */
442057e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
44213717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
44227ac87575SJohannes Berg 			      IWL_PHY_CHANNEL_MODE20,
442357e861d9SDavid Spinadel 			      0);
442457e861d9SDavid Spinadel 
442557e861d9SDavid Spinadel 	/* Set the time and duration */
4426afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4427e705c121SKalle Valo 
4428dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
4429dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
4430dc28e12fSMatti Gottlieb 
4431dc28e12fSMatti Gottlieb 	/*
4432dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
4433dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
4434dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
4435dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
4436dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
4437dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
4438dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
4439dc28e12fSMatti Gottlieb 	 */
4440f276e20bSJohannes Berg 	if (vif->cfg.assoc) {
4441dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
4442dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
4443dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
4444dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
4445dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
4446dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
4447dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
4448dc28e12fSMatti Gottlieb 		}
4449dc28e12fSMatti Gottlieb 	}
4450dc28e12fSMatti Gottlieb 
445157e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
445257e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
4453dc28e12fSMatti Gottlieb 
4454dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
4455903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
4456903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
4457903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
4458903b3f9bSEmmanuel Grumbach 		     "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
4459903b3f9bSEmmanuel Grumbach 		     duration, delay, dtim_interval);
4460903b3f9bSEmmanuel Grumbach 
4461e705c121SKalle Valo 	/* Set the node address */
446257e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4463e705c121SKalle Valo 
4464e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4465e705c121SKalle Valo 
4466e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4467e705c121SKalle Valo 
4468e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4469e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4470e705c121SKalle Valo 		return -EIO;
4471e705c121SKalle Valo 	}
4472e705c121SKalle Valo 
4473e705c121SKalle Valo 	te_data->vif = vif;
4474e705c121SKalle Valo 	te_data->duration = duration;
4475e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
4476e705c121SKalle Valo 
4477e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4478e705c121SKalle Valo 
4479e705c121SKalle Valo 	/*
4480e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
4481e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
4482e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
4483e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4484e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
4485e705c121SKalle Valo 	 * time another notification (that the time event started)
4486e705c121SKalle Valo 	 * might already be processed unsuccessfully.
4487e705c121SKalle Valo 	 */
4488e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4489e705c121SKalle Valo 				   time_event_response,
4490e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
4491e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
4492e705c121SKalle Valo 
449357e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4494e705c121SKalle Valo 				   &aux_roc_req);
4495e705c121SKalle Valo 
4496e705c121SKalle Valo 	if (res) {
4497e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4498e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4499e705c121SKalle Valo 		goto out_clear_te;
4500e705c121SKalle Valo 	}
4501e705c121SKalle Valo 
4502e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4503e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4504e705c121SKalle Valo 	/* should never fail */
4505e705c121SKalle Valo 	WARN_ON_ONCE(res);
4506e705c121SKalle Valo 
4507e705c121SKalle Valo 	if (res) {
4508e705c121SKalle Valo  out_clear_te:
4509e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
4510e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
4511e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4512e705c121SKalle Valo 	}
4513e705c121SKalle Valo 
4514e705c121SKalle Valo 	return res;
4515e705c121SKalle Valo }
4516e705c121SKalle Valo 
4517feebebaeSMiri Korenblit static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4518feebebaeSMiri Korenblit {
4519feebebaeSMiri Korenblit 	int ret = 0;
4520feebebaeSMiri Korenblit 
4521feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4522feebebaeSMiri Korenblit 
4523feebebaeSMiri Korenblit 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4524feebebaeSMiri Korenblit 			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4525feebebaeSMiri Korenblit 		IWL_ERR(mvm, "hotspot not supported\n");
4526feebebaeSMiri Korenblit 		return -EINVAL;
4527feebebaeSMiri Korenblit 	}
4528feebebaeSMiri Korenblit 
45291724fc78SEmmanuel Grumbach 	if (iwl_mvm_has_new_station_api(mvm->fw)) {
4530feebebaeSMiri Korenblit 		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4531feebebaeSMiri Korenblit 		WARN(ret, "Failed to allocate aux station");
4532feebebaeSMiri Korenblit 	}
4533feebebaeSMiri Korenblit 
4534feebebaeSMiri Korenblit 	return ret;
4535feebebaeSMiri Korenblit }
4536feebebaeSMiri Korenblit 
4537feebebaeSMiri Korenblit static int iwl_mvm_roc_switch_binding(struct iwl_mvm *mvm,
4538feebebaeSMiri Korenblit 				      struct ieee80211_vif *vif,
4539feebebaeSMiri Korenblit 				      struct iwl_mvm_phy_ctxt *new_phy_ctxt)
4540feebebaeSMiri Korenblit {
4541feebebaeSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4542feebebaeSMiri Korenblit 	int ret = 0;
4543feebebaeSMiri Korenblit 
4544feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4545feebebaeSMiri Korenblit 
4546feebebaeSMiri Korenblit 	/* Unbind the P2P_DEVICE from the current PHY context,
4547feebebaeSMiri Korenblit 	 * and if the PHY context is not used remove it.
4548feebebaeSMiri Korenblit 	 */
4549feebebaeSMiri Korenblit 	ret = iwl_mvm_binding_remove_vif(mvm, vif);
4550feebebaeSMiri Korenblit 	if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
4551feebebaeSMiri Korenblit 		return ret;
4552feebebaeSMiri Korenblit 
4553feebebaeSMiri Korenblit 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
4554feebebaeSMiri Korenblit 
4555feebebaeSMiri Korenblit 	/* Bind the P2P_DEVICE to the current PHY Context */
4556feebebaeSMiri Korenblit 	mvmvif->deflink.phy_ctxt = new_phy_ctxt;
4557feebebaeSMiri Korenblit 
4558feebebaeSMiri Korenblit 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4559feebebaeSMiri Korenblit 	WARN(ret, "Failed binding P2P_DEVICE\n");
4560feebebaeSMiri Korenblit 	return ret;
4561feebebaeSMiri Korenblit }
4562feebebaeSMiri Korenblit 
4563e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
4564e705c121SKalle Valo 		       struct ieee80211_vif *vif,
4565e705c121SKalle Valo 		       struct ieee80211_channel *channel,
4566e705c121SKalle Valo 		       int duration,
4567e705c121SKalle Valo 		       enum ieee80211_roc_type type)
4568e705c121SKalle Valo {
4569a2906ea6SJohannes Berg 	static const struct iwl_mvm_roc_ops ops = {
4570fe8b2ad3SMiri Korenblit 		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
4571fe8b2ad3SMiri Korenblit 		.switch_phy_ctxt = iwl_mvm_roc_switch_binding,
4572fe8b2ad3SMiri Korenblit 	};
4573fe8b2ad3SMiri Korenblit 
4574fe8b2ad3SMiri Korenblit 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4575fe8b2ad3SMiri Korenblit }
4576fe8b2ad3SMiri Korenblit 
4577fe8b2ad3SMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
4578fe8b2ad3SMiri Korenblit int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4579fe8b2ad3SMiri Korenblit 		       struct ieee80211_channel *channel, int duration,
4580fe8b2ad3SMiri Korenblit 		       enum ieee80211_roc_type type,
4581a2906ea6SJohannes Berg 		       const struct iwl_mvm_roc_ops *ops)
4582fe8b2ad3SMiri Korenblit {
4583e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4584e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4585e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
4586e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
45878835a64fSJohannes Berg 	bool band_change_removal;
4588e705c121SKalle Valo 	int ret, i;
4589feebebaeSMiri Korenblit 	u32 lmac_id;
4590e705c121SKalle Valo 
4591e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4592e705c121SKalle Valo 			   duration, type);
4593e705c121SKalle Valo 
45949834781cSJohannes Berg 	/*
45959834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
45969834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
45979834781cSJohannes Berg 	 */
4598e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
4599e705c121SKalle Valo 
4600e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4601e705c121SKalle Valo 
4602e705c121SKalle Valo 	switch (vif->type) {
4603e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4604d51439a6SAriel Malamud 		lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
46052c2c3647SNathan Errera 
4606feebebaeSMiri Korenblit 		/* Use aux roc framework (HS20) */
4607fe8b2ad3SMiri Korenblit 		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4608feebebaeSMiri Korenblit 		if (!ret)
4609e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
4610e705c121SKalle Valo 						       vif, duration);
4611e705c121SKalle Valo 		goto out_unlock;
4612e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
4613e705c121SKalle Valo 		/* handle below */
4614e705c121SKalle Valo 		break;
4615e705c121SKalle Valo 	default:
4616e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
4617e705c121SKalle Valo 		ret = -EINVAL;
4618e705c121SKalle Valo 		goto out_unlock;
4619e705c121SKalle Valo 	}
4620e705c121SKalle Valo 
4621e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
4622e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
4623650cadb7SGregory Greenman 		if (phy_ctxt->ref == 0 || mvmvif->deflink.phy_ctxt == phy_ctxt)
4624e705c121SKalle Valo 			continue;
4625e705c121SKalle Valo 
4626e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
4627fe8b2ad3SMiri Korenblit 			ret = ops->switch_phy_ctxt(mvm, vif, phy_ctxt);
4628feebebaeSMiri Korenblit 			if (ret)
4629e705c121SKalle Valo 				goto out_unlock;
4630e705c121SKalle Valo 
4631650cadb7SGregory Greenman 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4632e705c121SKalle Valo 			goto schedule_time_event;
4633e705c121SKalle Valo 		}
4634e705c121SKalle Valo 	}
4635e705c121SKalle Valo 
4636e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
4637650cadb7SGregory Greenman 	if (channel == mvmvif->deflink.phy_ctxt->channel)
4638e705c121SKalle Valo 		goto schedule_time_event;
4639e705c121SKalle Valo 
4640e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4641e705c121SKalle Valo 
4642e705c121SKalle Valo 	/*
46438835a64fSJohannes Berg 	 * Check if the remain-on-channel is on a different band and that
46448835a64fSJohannes Berg 	 * requires context removal, see iwl_mvm_phy_ctxt_changed(). If
46458835a64fSJohannes Berg 	 * so, we'll need to release and then re-configure here, since we
46468835a64fSJohannes Berg 	 * must not remove a PHY context that's part of a binding.
4647e705c121SKalle Valo 	 */
46488835a64fSJohannes Berg 	band_change_removal =
46498835a64fSJohannes Berg 		fw_has_capa(&mvm->fw->ucode_capa,
46508835a64fSJohannes Berg 			    IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
4651650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt->channel->band != chandef.chan->band;
46528835a64fSJohannes Berg 
4653650cadb7SGregory Greenman 	if (mvmvif->deflink.phy_ctxt->ref == 1 && !band_change_removal) {
46548835a64fSJohannes Berg 		/*
46558835a64fSJohannes Berg 		 * Change the PHY context configuration as it is currently
46568835a64fSJohannes Berg 		 * referenced only by the P2P Device MAC (and we can modify it)
46578835a64fSJohannes Berg 		 */
4658650cadb7SGregory Greenman 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->deflink.phy_ctxt,
4659e705c121SKalle Valo 					       &chandef, 1, 1);
4660e705c121SKalle Valo 		if (ret)
4661e705c121SKalle Valo 			goto out_unlock;
4662e705c121SKalle Valo 	} else {
4663e705c121SKalle Valo 		/*
46648835a64fSJohannes Berg 		 * The PHY context is shared with other MACs (or we're trying to
46658835a64fSJohannes Berg 		 * switch bands), so remove the P2P Device from the binding,
46668835a64fSJohannes Berg 		 * allocate an new PHY context and create a new binding.
4667e705c121SKalle Valo 		 */
4668e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4669e705c121SKalle Valo 		if (!phy_ctxt) {
4670e705c121SKalle Valo 			ret = -ENOSPC;
4671e705c121SKalle Valo 			goto out_unlock;
4672e705c121SKalle Valo 		}
4673e705c121SKalle Valo 
4674e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
4675e705c121SKalle Valo 					       1, 1);
4676e705c121SKalle Valo 		if (ret) {
4677e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
4678e705c121SKalle Valo 			goto out_unlock;
4679e705c121SKalle Valo 		}
4680e705c121SKalle Valo 
4681fe8b2ad3SMiri Korenblit 		ret = ops->switch_phy_ctxt(mvm, vif, phy_ctxt);
4682feebebaeSMiri Korenblit 		if (ret)
4683e705c121SKalle Valo 			goto out_unlock;
4684e705c121SKalle Valo 
4685650cadb7SGregory Greenman 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4686e705c121SKalle Valo 	}
4687e705c121SKalle Valo 
4688e705c121SKalle Valo schedule_time_event:
4689e705c121SKalle Valo 	/* Schedule the time events */
4690e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4691e705c121SKalle Valo 
4692e705c121SKalle Valo out_unlock:
4693e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4694e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4695e705c121SKalle Valo 	return ret;
4696e705c121SKalle Valo }
4697e705c121SKalle Valo 
4698fe8b2ad3SMiri Korenblit int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
46995db4c4b9SEmmanuel Grumbach 		       struct ieee80211_vif *vif)
4700e705c121SKalle Valo {
4701e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4702e705c121SKalle Valo 
4703e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4704e705c121SKalle Valo 
4705e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4706fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
4707e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4708e705c121SKalle Valo 
4709e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4710e705c121SKalle Valo 	return 0;
4711e705c121SKalle Valo }
4712e705c121SKalle Valo 
4713b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
4714b73f9a4aSJohannes Berg 	bool responder;
4715b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
4716b73f9a4aSJohannes Berg };
4717b73f9a4aSJohannes Berg 
4718b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4719b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
4720b73f9a4aSJohannes Berg {
4721b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4722b73f9a4aSJohannes Berg 
4723d0a9123eSJohannes Berg 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4724b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4725b73f9a4aSJohannes Berg 		data->responder = true;
4726b73f9a4aSJohannes Berg }
4727b73f9a4aSJohannes Berg 
4728b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4729b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
4730b73f9a4aSJohannes Berg {
4731b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
4732b73f9a4aSJohannes Berg 		.responder = false,
4733b73f9a4aSJohannes Berg 		.ctx = ctx,
4734b73f9a4aSJohannes Berg 	};
4735b73f9a4aSJohannes Berg 
4736b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4737b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
4738b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
4739b73f9a4aSJohannes Berg 					&data);
4740b73f9a4aSJohannes Berg 	return data.responder;
4741b73f9a4aSJohannes Berg }
4742b73f9a4aSJohannes Berg 
4743e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4744e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
4745e705c121SKalle Valo {
4746e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4747e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4748b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4749b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4750e705c121SKalle Valo 	int ret;
4751e705c121SKalle Valo 
4752e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4753e705c121SKalle Valo 
4754e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4755e705c121SKalle Valo 
4756e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4757e705c121SKalle Valo 	if (!phy_ctxt) {
4758e705c121SKalle Valo 		ret = -ENOSPC;
4759e705c121SKalle Valo 		goto out;
4760e705c121SKalle Valo 	}
4761e705c121SKalle Valo 
4762b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4763e705c121SKalle Valo 				       ctx->rx_chains_static,
4764e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
4765e705c121SKalle Valo 	if (ret) {
4766e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4767e705c121SKalle Valo 		goto out;
4768e705c121SKalle Valo 	}
4769e705c121SKalle Valo 
4770e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4771e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4772e705c121SKalle Valo out:
4773e705c121SKalle Valo 	return ret;
4774e705c121SKalle Valo }
4775e705c121SKalle Valo 
4776cbce62a3SMiri Korenblit int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4777e705c121SKalle Valo 			struct ieee80211_chanctx_conf *ctx)
4778e705c121SKalle Valo {
4779e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4780e705c121SKalle Valo 	int ret;
4781e705c121SKalle Valo 
4782e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4783e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4784e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4785e705c121SKalle Valo 
4786e705c121SKalle Valo 	return ret;
4787e705c121SKalle Valo }
4788e705c121SKalle Valo 
4789e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4790e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4791e705c121SKalle Valo {
4792e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4793e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4794e705c121SKalle Valo 
4795e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4796e705c121SKalle Valo 
4797e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4798e705c121SKalle Valo }
4799e705c121SKalle Valo 
4800cbce62a3SMiri Korenblit void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4801e705c121SKalle Valo 			    struct ieee80211_chanctx_conf *ctx)
4802e705c121SKalle Valo {
4803e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4804e705c121SKalle Valo 
4805e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4806e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4807e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4808e705c121SKalle Valo }
4809e705c121SKalle Valo 
4810cbce62a3SMiri Korenblit void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4811cbce62a3SMiri Korenblit 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
4812e705c121SKalle Valo {
4813e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4814e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4815e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4816b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4817b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4818e705c121SKalle Valo 
4819e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4820e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4821e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4822e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4823e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4824e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4825e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4826e705c121SKalle Valo 		return;
4827e705c121SKalle Valo 
4828e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
48297a20bcceSEmmanuel Grumbach 
48307a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
48317a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4832b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
48337a20bcceSEmmanuel Grumbach 			goto out_unlock;
48347a20bcceSEmmanuel Grumbach 
48357a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
48367a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4837b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
48387a20bcceSEmmanuel Grumbach 			goto out_unlock;
48397a20bcceSEmmanuel Grumbach 	}
48407a20bcceSEmmanuel Grumbach 
4841e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4842b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4843e705c121SKalle Valo 				 ctx->rx_chains_static,
4844e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
48457a20bcceSEmmanuel Grumbach 
48467a20bcceSEmmanuel Grumbach out_unlock:
4847e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4848e705c121SKalle Valo }
4849e705c121SKalle Valo 
48508a919a78SMiri Korenblit /*
48518a919a78SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
48528a919a78SMiri Korenblit  *
48538a919a78SMiri Korenblit  * Returns true if we're done assigning the chanctx
48548a919a78SMiri Korenblit  * (either on failure or success)
48558a919a78SMiri Korenblit  */
48564263ac7fSGregory Greenman static bool
48574263ac7fSGregory Greenman __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
4858e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4859e705c121SKalle Valo 				    struct ieee80211_chanctx_conf *ctx,
486050e81437SMiri Korenblit 				    bool switching_chanctx, int *ret)
4861e705c121SKalle Valo {
4862e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4863e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4864e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4865e705c121SKalle Valo 
4866e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4867e705c121SKalle Valo 
4868650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = phy_ctxt;
4869e705c121SKalle Valo 
4870e705c121SKalle Valo 	switch (vif->type) {
4871e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4872e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4873e705c121SKalle Valo 		if (switching_chanctx) {
4874e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4875e705c121SKalle Valo 			break;
4876e705c121SKalle Valo 		}
48775a2abdcaSGustavo A. R. Silva 		fallthrough;
4878e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4879e705c121SKalle Valo 		/*
4880e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4881e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4882e705c121SKalle Valo 		 */
48838a919a78SMiri Korenblit 		*ret = 0;
48848a919a78SMiri Korenblit 		return true;
4885e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4886e705c121SKalle Valo 		break;
4887e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4888e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4889e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4890e705c121SKalle Valo 		break;
4891e705c121SKalle Valo 	default:
48928a919a78SMiri Korenblit 		*ret = -EINVAL;
48938a919a78SMiri Korenblit 		return true;
4894e705c121SKalle Valo 	}
48958a919a78SMiri Korenblit 	return false;
48968a919a78SMiri Korenblit }
48978a919a78SMiri Korenblit 
48988a919a78SMiri Korenblit static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
48998a919a78SMiri Korenblit 					struct ieee80211_vif *vif,
49004263ac7fSGregory Greenman 					struct ieee80211_bss_conf *link_conf,
49018a919a78SMiri Korenblit 					struct ieee80211_chanctx_conf *ctx,
49028a919a78SMiri Korenblit 					bool switching_chanctx)
49038a919a78SMiri Korenblit {
49048a919a78SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
49058a919a78SMiri Korenblit 	int ret;
49068a919a78SMiri Korenblit 
49074263ac7fSGregory Greenman 	if (WARN_ON(!link_conf))
49084263ac7fSGregory Greenman 		return -EINVAL;
49094263ac7fSGregory Greenman 
49108a919a78SMiri Korenblit 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
49118a919a78SMiri Korenblit 						switching_chanctx, &ret))
49128a919a78SMiri Korenblit 		goto out;
4913e705c121SKalle Valo 
4914e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4915e705c121SKalle Valo 	if (ret)
4916e705c121SKalle Valo 		goto out;
4917e705c121SKalle Valo 
4918e705c121SKalle Valo 	/*
4919e705c121SKalle Valo 	 * Power state must be updated before quotas,
4920e705c121SKalle Valo 	 * otherwise fw will complain.
4921e705c121SKalle Valo 	 */
4922e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4923e705c121SKalle Valo 
4924e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4925e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4926e705c121SKalle Valo 	 * will handle quota settings.
4927e705c121SKalle Valo 	 */
4928e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4929e705c121SKalle Valo 		mvmvif->monitor_active = true;
4930e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4931e705c121SKalle Valo 		if (ret)
4932e705c121SKalle Valo 			goto out_remove_binding;
49330e39eb03SChaya Rachel Ivgi 
49340e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
49350e39eb03SChaya Rachel Ivgi 		if (ret)
49360e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
49370e39eb03SChaya Rachel Ivgi 
4938e705c121SKalle Valo 	}
4939e705c121SKalle Valo 
4940e705c121SKalle Valo 	/* Handle binding during CSA */
4941e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4942e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4943e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4944e705c121SKalle Valo 	}
4945e705c121SKalle Valo 
49468a919a78SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION) {
49478a919a78SMiri Korenblit 		if (!switching_chanctx) {
49488a919a78SMiri Korenblit 			mvmvif->csa_bcn_pending = false;
49498a919a78SMiri Korenblit 			goto out;
49508a919a78SMiri Korenblit 		}
49518a919a78SMiri Korenblit 
495274a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
495374a10252SSara Sharon 
495474a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
495574a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
495619125cb0SAndrei Otcheretianski 			u32 duration = 3 * vif->bss_conf.beacon_int;
4957e705c121SKalle Valo 
4958e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4959e705c121SKalle Valo 			 * beacon on the new channel.
4960e705c121SKalle Valo 			 */
4961e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4962e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4963e705c121SKalle Valo 						true);
496474a10252SSara Sharon 		}
4965e705c121SKalle Valo 
4966e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4967e705c121SKalle Valo 	}
4968e705c121SKalle Valo 
4969e705c121SKalle Valo 	goto out;
4970e705c121SKalle Valo 
4971e705c121SKalle Valo out_remove_binding:
4972e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4973e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4974e705c121SKalle Valo out:
4975e705c121SKalle Valo 	if (ret)
4976650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = NULL;
4977e705c121SKalle Valo 	return ret;
4978e705c121SKalle Valo }
49798a919a78SMiri Korenblit 
4980e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4981e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4982727eff4dSGregory Greenman 				      struct ieee80211_bss_conf *link_conf,
4983e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4984e705c121SKalle Valo {
4985e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4986e705c121SKalle Valo 	int ret;
4987e705c121SKalle Valo 
4988e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
49894263ac7fSGregory Greenman 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
4990e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4991e705c121SKalle Valo 
4992e705c121SKalle Valo 	return ret;
4993e705c121SKalle Valo }
4994e705c121SKalle Valo 
4995daddfae5SMiri Korenblit /*
4996daddfae5SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
4997daddfae5SMiri Korenblit  *
4998daddfae5SMiri Korenblit  * Returns if chanctx unassign chanctx is done
4999daddfae5SMiri Korenblit  * (either on failure or success)
5000daddfae5SMiri Korenblit  */
50014263ac7fSGregory Greenman static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
5002e705c121SKalle Valo 						  struct ieee80211_vif *vif,
5003e705c121SKalle Valo 						  bool switching_chanctx)
5004e705c121SKalle Valo {
5005e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5006e705c121SKalle Valo 
5007e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
5008650cadb7SGregory Greenman 	iwl_mvm_remove_time_event(mvm, mvmvif,
5009650cadb7SGregory Greenman 				  &mvmvif->time_event_data);
5010e705c121SKalle Valo 
5011e705c121SKalle Valo 	switch (vif->type) {
5012e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
5013daddfae5SMiri Korenblit 		return true;
5014e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
5015e705c121SKalle Valo 		mvmvif->monitor_active = false;
5016e705c121SKalle Valo 		mvmvif->ps_disabled = false;
5017e705c121SKalle Valo 		break;
5018e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5019e705c121SKalle Valo 		/* This part is triggered only during CSA */
5020e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
5021daddfae5SMiri Korenblit 			return true;
5022e705c121SKalle Valo 
5023e705c121SKalle Valo 		mvmvif->csa_countdown = false;
5024e705c121SKalle Valo 
5025e705c121SKalle Valo 		/* Set CS bit on all the stations */
5026e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5027e705c121SKalle Valo 
5028e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
5029e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5030e705c121SKalle Valo 
5031e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
5032e705c121SKalle Valo 		break;
5033daddfae5SMiri Korenblit 	default:
5034e705c121SKalle Valo 		break;
5035daddfae5SMiri Korenblit 	}
5036daddfae5SMiri Korenblit 	return false;
5037daddfae5SMiri Korenblit }
5038e705c121SKalle Valo 
5039e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5040e705c121SKalle Valo 					   struct ieee80211_vif *vif,
50414263ac7fSGregory Greenman 					   struct ieee80211_bss_conf *link_conf,
5042e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
5043e705c121SKalle Valo 					   bool switching_chanctx)
5044e705c121SKalle Valo {
5045e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5046e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
5047e705c121SKalle Valo 
5048daddfae5SMiri Korenblit 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5049e705c121SKalle Valo 		goto out;
5050daddfae5SMiri Korenblit 
5051daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_MONITOR)
5052e705c121SKalle Valo 		iwl_mvm_rm_snif_sta(mvm, vif);
5053e705c121SKalle Valo 
5054e705c121SKalle Valo 
5055daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5056e705c121SKalle Valo 		disabled_vif = vif;
505774a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
505874a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5059e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5060e705c121SKalle Valo 	}
5061e705c121SKalle Valo 
5062e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5063e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
5064e705c121SKalle Valo 
5065e705c121SKalle Valo out:
5066bf544e9aSSara Sharon 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5067bf544e9aSSara Sharon 	    switching_chanctx)
5068bf544e9aSSara Sharon 		return;
5069650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
5070e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5071e705c121SKalle Valo }
5072e705c121SKalle Valo 
5073e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5074e705c121SKalle Valo 					 struct ieee80211_vif *vif,
5075727eff4dSGregory Greenman 					 struct ieee80211_bss_conf *link_conf,
5076e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
5077e705c121SKalle Valo {
5078e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5079e705c121SKalle Valo 
5080e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
50814263ac7fSGregory Greenman 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5082e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5083e705c121SKalle Valo }
5084e705c121SKalle Valo 
5085e705c121SKalle Valo static int
5086e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5087660eba5aSMiri Korenblit 				struct ieee80211_vif_chanctx_switch *vifs,
5088a2906ea6SJohannes Berg 				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5089e705c121SKalle Valo {
5090e705c121SKalle Valo 	int ret;
5091e705c121SKalle Valo 
5092e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
50934263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
50944263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5095e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5096e705c121SKalle Valo 
5097e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5098e705c121SKalle Valo 	if (ret) {
5099e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5100e705c121SKalle Valo 		goto out_reassign;
5101e705c121SKalle Valo 	}
5102e705c121SKalle Valo 
51034263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51044263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5105e705c121SKalle Valo 	if (ret) {
5106e705c121SKalle Valo 		IWL_ERR(mvm,
5107e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5108e705c121SKalle Valo 		goto out_remove;
5109e705c121SKalle Valo 	}
5110e705c121SKalle Valo 
5111e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
5112e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5113e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
5114e705c121SKalle Valo 
5115e705c121SKalle Valo 	goto out;
5116e705c121SKalle Valo 
5117e705c121SKalle Valo out_remove:
5118e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5119e705c121SKalle Valo 
5120e705c121SKalle Valo out_reassign:
5121e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5122e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5123e705c121SKalle Valo 		goto out_restart;
5124e705c121SKalle Valo 	}
5125e705c121SKalle Valo 
51264263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51274263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5128e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5129e705c121SKalle Valo 		goto out_restart;
5130e705c121SKalle Valo 	}
5131e705c121SKalle Valo 
5132e705c121SKalle Valo 	goto out;
5133e705c121SKalle Valo 
5134e705c121SKalle Valo out_restart:
5135e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5136e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5137e705c121SKalle Valo 
5138e705c121SKalle Valo out:
5139e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5140e705c121SKalle Valo 
5141e705c121SKalle Valo 	return ret;
5142e705c121SKalle Valo }
5143e705c121SKalle Valo 
5144e705c121SKalle Valo static int
5145e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5146660eba5aSMiri Korenblit 				    struct ieee80211_vif_chanctx_switch *vifs,
5147a2906ea6SJohannes Berg 				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5148e705c121SKalle Valo {
5149e705c121SKalle Valo 	int ret;
5150e705c121SKalle Valo 
5151e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
51524263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51534263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5154e705c121SKalle Valo 
51554263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51564263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5157e705c121SKalle Valo 	if (ret) {
5158e705c121SKalle Valo 		IWL_ERR(mvm,
5159e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5160e705c121SKalle Valo 		goto out_reassign;
5161e705c121SKalle Valo 	}
5162e705c121SKalle Valo 
5163e705c121SKalle Valo 	goto out;
5164e705c121SKalle Valo 
5165e705c121SKalle Valo out_reassign:
51664263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51674263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5168e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5169e705c121SKalle Valo 		goto out_restart;
5170e705c121SKalle Valo 	}
5171e705c121SKalle Valo 
5172e705c121SKalle Valo 	goto out;
5173e705c121SKalle Valo 
5174e705c121SKalle Valo out_restart:
5175e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5176e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5177e705c121SKalle Valo 
5178e705c121SKalle Valo out:
5179e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5180e705c121SKalle Valo 
5181e705c121SKalle Valo 	return ret;
5182e705c121SKalle Valo }
5183e705c121SKalle Valo 
5184660eba5aSMiri Korenblit /* Execute the common part for both MLD and non-MLD modes */
5185660eba5aSMiri Korenblit int
5186660eba5aSMiri Korenblit iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5187e705c121SKalle Valo 				  struct ieee80211_vif_chanctx_switch *vifs,
5188e705c121SKalle Valo 				  int n_vifs,
5189660eba5aSMiri Korenblit 				  enum ieee80211_chanctx_switch_mode mode,
5190a2906ea6SJohannes Berg 				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5191e705c121SKalle Valo {
5192e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5193e705c121SKalle Valo 	int ret;
5194e705c121SKalle Valo 
5195e705c121SKalle Valo 	/* we only support a single-vif right now */
5196e705c121SKalle Valo 	if (n_vifs > 1)
5197e705c121SKalle Valo 		return -EOPNOTSUPP;
5198e705c121SKalle Valo 
5199e705c121SKalle Valo 	switch (mode) {
5200e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5201660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5202e705c121SKalle Valo 		break;
5203e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
5204660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5205e705c121SKalle Valo 		break;
5206e705c121SKalle Valo 	default:
5207e705c121SKalle Valo 		ret = -EOPNOTSUPP;
5208e705c121SKalle Valo 		break;
5209e705c121SKalle Valo 	}
5210e705c121SKalle Valo 
5211e705c121SKalle Valo 	return ret;
5212e705c121SKalle Valo }
5213e705c121SKalle Valo 
5214660eba5aSMiri Korenblit static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5215660eba5aSMiri Korenblit 				      struct ieee80211_vif_chanctx_switch *vifs,
5216660eba5aSMiri Korenblit 				      int n_vifs,
5217660eba5aSMiri Korenblit 				      enum ieee80211_chanctx_switch_mode mode)
5218660eba5aSMiri Korenblit {
5219a2906ea6SJohannes Berg 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5220660eba5aSMiri Korenblit 		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5221660eba5aSMiri Korenblit 		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5222660eba5aSMiri Korenblit 	};
5223660eba5aSMiri Korenblit 
5224660eba5aSMiri Korenblit 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5225660eba5aSMiri Korenblit }
5226660eba5aSMiri Korenblit 
5227cbce62a3SMiri Korenblit int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
52282f0282dbSJohannes Berg {
52292f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
52302f0282dbSJohannes Berg 
52312f0282dbSJohannes Berg 	return mvm->ibss_manager;
52322f0282dbSJohannes Berg }
52332f0282dbSJohannes Berg 
5234cbce62a3SMiri Korenblit int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5235e705c121SKalle Valo 		    bool set)
5236e705c121SKalle Valo {
5237e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5238e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5239e705c121SKalle Valo 
5240e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
5241e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
5242e705c121SKalle Valo 		return -EINVAL;
5243e705c121SKalle Valo 	}
5244e705c121SKalle Valo 
524536cf5377SGregory Greenman 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
524636cf5377SGregory Greenman 					       &mvm_sta->vif->bss_conf);
5247e705c121SKalle Valo }
5248e705c121SKalle Valo 
5249e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
5250e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5251e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5252e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5253e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5254e705c121SKalle Valo };
5255e705c121SKalle Valo 
5256e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5257e705c121SKalle Valo 				      struct ieee80211_vif *vif,
5258e705c121SKalle Valo 				      void *data, int len)
5259e705c121SKalle Valo {
5260e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5261e705c121SKalle Valo 	int err;
5262e705c121SKalle Valo 	u32 noa_duration;
5263e705c121SKalle Valo 
52648cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
52658cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
5266e705c121SKalle Valo 	if (err)
5267e705c121SKalle Valo 		return err;
5268e705c121SKalle Valo 
5269e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
5270e705c121SKalle Valo 		return -EINVAL;
5271e705c121SKalle Valo 
5272e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5273e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
5274e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5275e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
5276e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5277e705c121SKalle Valo 			return -EINVAL;
5278e705c121SKalle Valo 
5279e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5280e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
5281e705c121SKalle Valo 			return -EINVAL;
5282e705c121SKalle Valo 
5283e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
5284e705c121SKalle Valo 		mvm->noa_vif = vif;
5285e705c121SKalle Valo 
528622b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
5287e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5288e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
5289e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5290f276e20bSJohannes Berg 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5291e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5292e705c121SKalle Valo 			return -EINVAL;
5293e705c121SKalle Valo 
5294e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
5295e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
5296e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
5297e705c121SKalle Valo 	}
5298e705c121SKalle Valo 
5299e705c121SKalle Valo 	return -EOPNOTSUPP;
5300e705c121SKalle Valo }
5301e705c121SKalle Valo 
5302cbce62a3SMiri Korenblit int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5303e705c121SKalle Valo 			     struct ieee80211_vif *vif,
5304e705c121SKalle Valo 			     void *data, int len)
5305e705c121SKalle Valo {
5306e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5307e705c121SKalle Valo 	int err;
5308e705c121SKalle Valo 
5309e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5310e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5311e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5312e705c121SKalle Valo 
5313e705c121SKalle Valo 	return err;
5314e705c121SKalle Valo }
5315e705c121SKalle Valo #endif
5316e705c121SKalle Valo 
5317cbce62a3SMiri Korenblit void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5318e705c121SKalle Valo 			    struct ieee80211_channel_switch *chsw)
5319e705c121SKalle Valo {
5320e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
5321e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
5322e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
5323e705c121SKalle Valo 	 * (which is when the absence time event starts).
5324e705c121SKalle Valo 	 */
5325e705c121SKalle Valo 
5326e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5327e705c121SKalle Valo 			   "dummy channel switch op\n");
5328e705c121SKalle Valo }
5329e705c121SKalle Valo 
533074a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
533174a10252SSara Sharon 				       struct ieee80211_vif *vif,
533274a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
533374a10252SSara Sharon {
533474a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
533574a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
533674a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
533774a10252SSara Sharon 							  mvmvif->color)),
533874a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
533974a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
534074a10252SSara Sharon 		.cs_count = chsw->count,
534177738865SSara Sharon 		.cs_mode = chsw->block_tx,
534274a10252SSara Sharon 	};
534374a10252SSara Sharon 
534474a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
534574a10252SSara Sharon 
53469cfcf71cSSara Sharon 	if (chsw->delay)
53479cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
53489cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
53499cfcf71cSSara Sharon 
535074a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
535174a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
535274a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
535374a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
535474a10252SSara Sharon }
535574a10252SSara Sharon 
53560202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
53570202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
53580202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
53590202bcf0SEmmanuel Grumbach {
53600202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
53610202bcf0SEmmanuel Grumbach 	u32 apply_time;
53620202bcf0SEmmanuel Grumbach 
53630202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
53640202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
53650202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
53660202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
53670202bcf0SEmmanuel Grumbach 	 * loss).
53680202bcf0SEmmanuel Grumbach 	 */
53690202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
53700202bcf0SEmmanuel Grumbach 		apply_time = 0;
53710202bcf0SEmmanuel Grumbach 	else
53720202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
53730202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
53740202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
53750202bcf0SEmmanuel Grumbach 
53760202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
53770202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
53780202bcf0SEmmanuel Grumbach 
53790202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
53800202bcf0SEmmanuel Grumbach 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
53810202bcf0SEmmanuel Grumbach 
53820202bcf0SEmmanuel Grumbach 		if (ret)
53830202bcf0SEmmanuel Grumbach 			return ret;
53840202bcf0SEmmanuel Grumbach 	}
53850202bcf0SEmmanuel Grumbach 
53860202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
53870202bcf0SEmmanuel Grumbach 				    apply_time);
53880202bcf0SEmmanuel Grumbach 
53890202bcf0SEmmanuel Grumbach 	return 0;
53900202bcf0SEmmanuel Grumbach }
53910202bcf0SEmmanuel Grumbach 
5392f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
5393cbce62a3SMiri Korenblit int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
5394e705c121SKalle Valo 			       struct ieee80211_vif *vif,
5395e705c121SKalle Valo 			       struct ieee80211_channel_switch *chsw)
5396e705c121SKalle Valo {
5397e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5398e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
5399e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5400e705c121SKalle Valo 	int ret;
5401e705c121SKalle Valo 
5402e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5403e705c121SKalle Valo 
5404e705c121SKalle Valo 	mvmvif->csa_failed = false;
5405e705c121SKalle Valo 
5406e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5407e705c121SKalle Valo 			   chsw->chandef.center_freq1);
5408e705c121SKalle Valo 
54097174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
54107174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
54117174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5412e705c121SKalle Valo 
5413e705c121SKalle Valo 	switch (vif->type) {
5414e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5415e705c121SKalle Valo 		csa_vif =
5416e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
5417e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
5418d0a9123eSJohannes Berg 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5419e705c121SKalle Valo 			      "Another CSA is already in progress")) {
5420e705c121SKalle Valo 			ret = -EBUSY;
5421e705c121SKalle Valo 			goto out_unlock;
5422e705c121SKalle Valo 		}
5423e705c121SKalle Valo 
5424d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
5425d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5426d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
5427d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
5428d3a108a4SAndrei Otcheretianski 			goto out_unlock;
5429d3a108a4SAndrei Otcheretianski 		}
5430d3a108a4SAndrei Otcheretianski 
5431e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
5432e705c121SKalle Valo 
5433e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
5434e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
5435e705c121SKalle Valo 			ret = -EBUSY;
5436e705c121SKalle Valo 			goto out_unlock;
5437e705c121SKalle Valo 		}
5438e705c121SKalle Valo 
5439d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5440d3a108a4SAndrei Otcheretianski 
5441e705c121SKalle Valo 		break;
5442e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
54432360acbdSEmmanuel Grumbach 		/*
5444ad12b231SNathan Errera 		 * In the new flow FW is in charge of timing the switch so there
5445ad12b231SNathan Errera 		 * is no need for all of this
5446ad12b231SNathan Errera 		 */
5447ad12b231SNathan Errera 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5448ad12b231SNathan Errera 					    CHANNEL_SWITCH_ERROR_NOTIF,
5449ad12b231SNathan Errera 					    0))
5450ad12b231SNathan Errera 			break;
5451ad12b231SNathan Errera 
5452ad12b231SNathan Errera 		/*
54532360acbdSEmmanuel Grumbach 		 * We haven't configured the firmware to be associated yet since
54542360acbdSEmmanuel Grumbach 		 * we don't know the dtim period. In this case, the firmware can't
54552360acbdSEmmanuel Grumbach 		 * track the beacons.
54562360acbdSEmmanuel Grumbach 		 */
5457f276e20bSJohannes Berg 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period) {
54582360acbdSEmmanuel Grumbach 			ret = -EBUSY;
54592360acbdSEmmanuel Grumbach 			goto out_unlock;
54602360acbdSEmmanuel Grumbach 		}
54612360acbdSEmmanuel Grumbach 
546287d9564eSJohannes Berg 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX)
546387d9564eSJohannes Berg 			schedule_delayed_work(&mvmvif->csa_work, 0);
546487d9564eSJohannes Berg 
5465f6780614SSara Sharon 		if (chsw->block_tx) {
5466f6780614SSara Sharon 			/*
5467f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
5468f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
5469f6780614SSara Sharon 			 */
5470f6780614SSara Sharon 			if (!chsw->count ||
5471f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
5472f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
5473f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
5474f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5475f6780614SSara Sharon 		}
5476e705c121SKalle Valo 
54770202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
54780202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
54790202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5480e705c121SKalle Valo 			if (ret)
5481e705c121SKalle Valo 				goto out_unlock;
54820202bcf0SEmmanuel Grumbach 		} else {
548374a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
54840202bcf0SEmmanuel Grumbach 		}
548581b4e44eSSara Sharon 
548681b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
548781b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
5488e705c121SKalle Valo 		break;
5489e705c121SKalle Valo 	default:
5490e705c121SKalle Valo 		break;
5491e705c121SKalle Valo 	}
5492e705c121SKalle Valo 
5493e705c121SKalle Valo 	mvmvif->ps_disabled = true;
5494e705c121SKalle Valo 
5495e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
5496e705c121SKalle Valo 	if (ret)
5497e705c121SKalle Valo 		goto out_unlock;
5498e705c121SKalle Valo 
5499e705c121SKalle Valo 	/* we won't be on this channel any longer */
5500e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
5501e705c121SKalle Valo 
5502e705c121SKalle Valo out_unlock:
5503e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5504e705c121SKalle Valo 
5505e705c121SKalle Valo 	return ret;
5506e705c121SKalle Valo }
5507e705c121SKalle Valo 
5508cbce62a3SMiri Korenblit void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5509c37763d2SSara Sharon 				      struct ieee80211_vif *vif,
5510c37763d2SSara Sharon 				      struct ieee80211_channel_switch *chsw)
5511e705c121SKalle Valo {
5512e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5513c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5514c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
5515c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5516c37763d2SSara Sharon 							  mvmvif->color)),
5517c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5518c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
5519c37763d2SSara Sharon 		.cs_count = chsw->count,
552077738865SSara Sharon 		.cs_mode = chsw->block_tx,
5521c37763d2SSara Sharon 	};
5522e705c121SKalle Valo 
5523ad12b231SNathan Errera 	/*
5524ad12b231SNathan Errera 	 * In the new flow FW is in charge of timing the switch so there is no
5525ad12b231SNathan Errera 	 * need for all of this
5526ad12b231SNathan Errera 	 */
5527ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5528ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5529ad12b231SNathan Errera 		return;
5530ad12b231SNathan Errera 
5531c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5532c37763d2SSara Sharon 		return;
5533e705c121SKalle Valo 
5534c3eae059SGregory Greenman 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5535c3eae059SGregory Greenman 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5536c3eae059SGregory Greenman 
553781b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
553881b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
553981b4e44eSSara Sharon 			/* Second time, give up on this AP*/
554081b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
5541a469a593SEmmanuel Grumbach 			ieee80211_chswitch_done(vif, false, 0);
554281b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
554381b4e44eSSara Sharon 			return;
5544e705c121SKalle Valo 		}
554581b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
5546e705c121SKalle Valo 	}
554781b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
5548e705c121SKalle Valo 
5549caf46377SSara Sharon 	mutex_lock(&mvm->mutex);
5550caf46377SSara Sharon 	if (mvmvif->csa_failed)
5551caf46377SSara Sharon 		goto out_unlock;
5552e705c121SKalle Valo 
5553c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5554c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
5555c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5556caf46377SSara Sharon 				     0, sizeof(cmd), &cmd));
5557caf46377SSara Sharon out_unlock:
5558caf46377SSara Sharon 	mutex_unlock(&mvm->mutex);
5559e705c121SKalle Valo }
5560e705c121SKalle Valo 
55616110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
55626110d9e5SDavid Spinadel {
5563435d0827SSara Sharon 	int i;
5564435d0827SSara Sharon 
556506195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5566309c4848SLuca Coelho 		if (drop) {
5567309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
55686110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
5569d4e3a341SMordechay Goodstein 				iwl_mvm_flushable_queues(mvm) & queues);
5570309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
5571309c4848SLuca Coelho 		} else {
5572435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5573309c4848SLuca Coelho 		}
5574435d0827SSara Sharon 		return;
5575435d0827SSara Sharon 	}
55766110d9e5SDavid Spinadel 
55776110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
5578be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5579435d0827SSara Sharon 		struct ieee80211_sta *sta;
5580435d0827SSara Sharon 
5581435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
55826110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
55836110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
55846110d9e5SDavid Spinadel 			continue;
55856110d9e5SDavid Spinadel 
558606195639SSara Sharon 		if (drop)
5587d4e3a341SMordechay Goodstein 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
558806195639SSara Sharon 		else
55896110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
55906110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
55916110d9e5SDavid Spinadel 	}
55926110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
55936110d9e5SDavid Spinadel }
55946110d9e5SDavid Spinadel 
5595cbce62a3SMiri Korenblit void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5596cbce62a3SMiri Korenblit 		       u32 queues, bool drop)
5597e705c121SKalle Valo {
5598e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5599e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
5600e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
5601e705c121SKalle Valo 	struct ieee80211_sta *sta;
5602d3f9cd61SJohannes Berg 	bool ap_sta_done = false;
5603e705c121SKalle Valo 	int i;
5604e705c121SKalle Valo 	u32 msk = 0;
5605e705c121SKalle Valo 
56066110d9e5SDavid Spinadel 	if (!vif) {
56076110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
56086110d9e5SDavid Spinadel 		return;
56096110d9e5SDavid Spinadel 	}
56106110d9e5SDavid Spinadel 
561124afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
561224afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
561324afba76SLiad Kaufman 
5614e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5615e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
5616e705c121SKalle Valo 
5617e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
5618be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5619e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5620e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
5621e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
5622e705c121SKalle Valo 			continue;
5623e705c121SKalle Valo 
5624e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5625e705c121SKalle Valo 		if (mvmsta->vif != vif)
5626e705c121SKalle Valo 			continue;
5627e705c121SKalle Valo 
5628d3f9cd61SJohannes Berg 		if (sta == mvmvif->ap_sta) {
5629d3f9cd61SJohannes Berg 			if (ap_sta_done)
5630d3f9cd61SJohannes Berg 				continue;
5631d3f9cd61SJohannes Berg 			ap_sta_done = true;
5632d3f9cd61SJohannes Berg 		}
5633d3f9cd61SJohannes Berg 
5634d49394a1SSara Sharon 		if (drop) {
5635f9084775SNathan Errera 			if (iwl_mvm_flush_sta(mvm, mvmsta, false))
5636d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
5637d49394a1SSara Sharon 		} else {
5638d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
5639d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5640f7bd883bSJohannes Berg 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5641f7bd883bSJohannes Berg 				msk |= mvmsta->tfd_queue_msk;
5642e705c121SKalle Valo 		}
5643d49394a1SSara Sharon 	}
5644e705c121SKalle Valo 
5645e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5646e705c121SKalle Valo 
5647e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
5648e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
5649e705c121SKalle Valo 	 */
5650d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5651a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5652e705c121SKalle Valo }
5653e705c121SKalle Valo 
5654a6cc6ccbSJohannes Berg void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5655a6cc6ccbSJohannes Berg 			   struct ieee80211_sta *sta)
5656a6cc6ccbSJohannes Berg {
5657a6cc6ccbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5658a6cc6ccbSJohannes Berg 	int i;
5659a6cc6ccbSJohannes Berg 
5660a6cc6ccbSJohannes Berg 	mutex_lock(&mvm->mutex);
5661a6cc6ccbSJohannes Berg 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5662a6cc6ccbSJohannes Berg 		struct iwl_mvm_sta *mvmsta;
5663a6cc6ccbSJohannes Berg 		struct ieee80211_sta *tmp;
5664a6cc6ccbSJohannes Berg 
5665a6cc6ccbSJohannes Berg 		tmp = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5666a6cc6ccbSJohannes Berg 						lockdep_is_held(&mvm->mutex));
5667a6cc6ccbSJohannes Berg 		if (tmp != sta)
5668a6cc6ccbSJohannes Berg 			continue;
5669a6cc6ccbSJohannes Berg 
5670a6cc6ccbSJohannes Berg 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5671a6cc6ccbSJohannes Berg 
5672a6cc6ccbSJohannes Berg 		if (iwl_mvm_flush_sta(mvm, mvmsta, false))
5673a6cc6ccbSJohannes Berg 			IWL_ERR(mvm, "flush request fail\n");
5674a6cc6ccbSJohannes Berg 	}
5675a6cc6ccbSJohannes Berg 	mutex_unlock(&mvm->mutex);
5676a6cc6ccbSJohannes Berg }
5677a6cc6ccbSJohannes Berg 
5678cbce62a3SMiri Korenblit int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5679e705c121SKalle Valo 			   struct survey_info *survey)
5680e705c121SKalle Valo {
5681e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5682e705c121SKalle Valo 	int ret;
5683e705c121SKalle Valo 
5684e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
5685e705c121SKalle Valo 
5686e705c121SKalle Valo 	/* only support global statistics right now */
5687e705c121SKalle Valo 	if (idx != 0)
5688e705c121SKalle Valo 		return -ENOENT;
5689e705c121SKalle Valo 
5690280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
5691e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
5692e705c121SKalle Valo 		return -ENOENT;
5693e705c121SKalle Valo 
5694e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5695e705c121SKalle Valo 
5696aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
5697e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
5698e705c121SKalle Valo 		if (ret)
5699e705c121SKalle Valo 			goto out;
5700e705c121SKalle Valo 	}
5701e705c121SKalle Valo 
5702e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
5703e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
5704e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
5705e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
5706e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
5707e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
5708e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
5709e705c121SKalle Valo 
5710e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
5711e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
5712e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
5713e705c121SKalle Valo 
5714e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
5715e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
5716e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
5717e705c121SKalle Valo 
5718e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
5719e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
5720e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
5721e705c121SKalle Valo 
5722e705c121SKalle Valo 	ret = 0;
5723e705c121SKalle Valo  out:
5724e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5725e705c121SKalle Valo 	return ret;
5726e705c121SKalle Valo }
5727e705c121SKalle Valo 
5728ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
5729ed780545SJohannes Berg {
573082cdbd11SMiri Korenblit 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
57317138763eSJohannes Berg 	u32 gi_ltf;
573282cdbd11SMiri Korenblit 
573382cdbd11SMiri Korenblit 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
5734ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
5735ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
5736ed780545SJohannes Berg 		break;
5737ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
5738ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
5739ed780545SJohannes Berg 		break;
5740ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
5741ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
5742ed780545SJohannes Berg 		break;
5743ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
5744ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
5745ed780545SJohannes Berg 		break;
574623dcee94SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_320:
574723dcee94SJohannes Berg 		rinfo->bw = RATE_INFO_BW_320;
574823dcee94SJohannes Berg 		break;
5749ed780545SJohannes Berg 	}
5750ed780545SJohannes Berg 
575182cdbd11SMiri Korenblit 	if (format == RATE_MCS_CCK_MSK ||
575282cdbd11SMiri Korenblit 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
575382cdbd11SMiri Korenblit 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
575482cdbd11SMiri Korenblit 
575582cdbd11SMiri Korenblit 		/* add the offset needed to get to the legacy ofdm indices */
575682cdbd11SMiri Korenblit 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
575782cdbd11SMiri Korenblit 			rate += IWL_FIRST_OFDM_RATE;
575882cdbd11SMiri Korenblit 
575982cdbd11SMiri Korenblit 		switch (rate) {
576082cdbd11SMiri Korenblit 		case IWL_RATE_1M_INDEX:
576182cdbd11SMiri Korenblit 			rinfo->legacy = 10;
576282cdbd11SMiri Korenblit 			break;
576382cdbd11SMiri Korenblit 		case IWL_RATE_2M_INDEX:
576482cdbd11SMiri Korenblit 			rinfo->legacy = 20;
576582cdbd11SMiri Korenblit 			break;
576682cdbd11SMiri Korenblit 		case IWL_RATE_5M_INDEX:
576782cdbd11SMiri Korenblit 			rinfo->legacy = 55;
576882cdbd11SMiri Korenblit 			break;
576982cdbd11SMiri Korenblit 		case IWL_RATE_11M_INDEX:
577082cdbd11SMiri Korenblit 			rinfo->legacy = 110;
577182cdbd11SMiri Korenblit 			break;
577282cdbd11SMiri Korenblit 		case IWL_RATE_6M_INDEX:
577382cdbd11SMiri Korenblit 			rinfo->legacy = 60;
577482cdbd11SMiri Korenblit 			break;
577582cdbd11SMiri Korenblit 		case IWL_RATE_9M_INDEX:
577682cdbd11SMiri Korenblit 			rinfo->legacy = 90;
577782cdbd11SMiri Korenblit 			break;
577882cdbd11SMiri Korenblit 		case IWL_RATE_12M_INDEX:
577982cdbd11SMiri Korenblit 			rinfo->legacy = 120;
578082cdbd11SMiri Korenblit 			break;
578182cdbd11SMiri Korenblit 		case IWL_RATE_18M_INDEX:
578282cdbd11SMiri Korenblit 			rinfo->legacy = 180;
578382cdbd11SMiri Korenblit 			break;
578482cdbd11SMiri Korenblit 		case IWL_RATE_24M_INDEX:
578582cdbd11SMiri Korenblit 			rinfo->legacy = 240;
578682cdbd11SMiri Korenblit 			break;
578782cdbd11SMiri Korenblit 		case IWL_RATE_36M_INDEX:
578882cdbd11SMiri Korenblit 			rinfo->legacy = 360;
578982cdbd11SMiri Korenblit 			break;
579082cdbd11SMiri Korenblit 		case IWL_RATE_48M_INDEX:
579182cdbd11SMiri Korenblit 			rinfo->legacy = 480;
579282cdbd11SMiri Korenblit 			break;
579382cdbd11SMiri Korenblit 		case IWL_RATE_54M_INDEX:
579482cdbd11SMiri Korenblit 			rinfo->legacy = 540;
579582cdbd11SMiri Korenblit 		}
579682cdbd11SMiri Korenblit 		return;
579782cdbd11SMiri Korenblit 	}
579882cdbd11SMiri Korenblit 
5799ed780545SJohannes Berg 	rinfo->nss = u32_get_bits(rate_n_flags,
580082cdbd11SMiri Korenblit 				  RATE_MCS_NSS_MSK) + 1;
580182cdbd11SMiri Korenblit 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
580282cdbd11SMiri Korenblit 		RATE_HT_MCS_INDEX(rate_n_flags) :
580382cdbd11SMiri Korenblit 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
580482cdbd11SMiri Korenblit 
58057138763eSJohannes Berg 	if (rate_n_flags & RATE_MCS_SGI_MSK)
58067138763eSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
58077138763eSJohannes Berg 
58087138763eSJohannes Berg 	switch (format) {
580923dcee94SJohannes Berg 	case RATE_MCS_EHT_MSK:
581023dcee94SJohannes Berg 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
581123dcee94SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
581223dcee94SJohannes Berg 		break;
58137138763eSJohannes Berg 	case RATE_MCS_HE_MSK:
58147138763eSJohannes Berg 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
5815ed780545SJohannes Berg 
5816ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
5817ed780545SJohannes Berg 
581882cdbd11SMiri Korenblit 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
5819ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
5820ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
5821ed780545SJohannes Berg 		}
5822ed780545SJohannes Berg 
582382cdbd11SMiri Korenblit 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
582482cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_SU:
582582cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_EXT_SU:
5826ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5827ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5828ed780545SJohannes Berg 			else if (gi_ltf == 2)
5829ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
583082cdbd11SMiri Korenblit 			else if (gi_ltf == 3)
5831ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
583282cdbd11SMiri Korenblit 			else
583382cdbd11SMiri Korenblit 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5834ed780545SJohannes Berg 			break;
583582cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_MU:
5836ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5837ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5838ed780545SJohannes Berg 			else if (gi_ltf == 2)
5839ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5840ed780545SJohannes Berg 			else
5841ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5842ed780545SJohannes Berg 			break;
584382cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_TRIG:
5844ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5845ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5846ed780545SJohannes Berg 			else
5847ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5848ed780545SJohannes Berg 			break;
5849ed780545SJohannes Berg 		}
5850ed780545SJohannes Berg 
5851ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
5852ed780545SJohannes Berg 			rinfo->he_dcm = 1;
58537138763eSJohannes Berg 		break;
58547138763eSJohannes Berg 	case RATE_MCS_HT_MSK:
585582cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
58567138763eSJohannes Berg 		break;
58577138763eSJohannes Berg 	case RATE_MCS_VHT_MSK:
585882cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
58597138763eSJohannes Berg 		break;
5860ed780545SJohannes Berg 	}
5861ed780545SJohannes Berg }
5862ed780545SJohannes Berg 
5863cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
5864e705c121SKalle Valo 				struct ieee80211_vif *vif,
5865e705c121SKalle Valo 				struct ieee80211_sta *sta,
5866e705c121SKalle Valo 				struct station_info *sinfo)
5867e705c121SKalle Valo {
5868e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5869e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5870e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5871e705c121SKalle Valo 
5872c8ee33e1SGregory Greenman 	if (mvmsta->deflink.avg_energy) {
5873c8ee33e1SGregory Greenman 		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
587422d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
5875988b5968SSara Sharon 	}
5876988b5968SSara Sharon 
5877ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
5878c8ee33e1SGregory Greenman 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
5879ed780545SJohannes Berg 
5880ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5881ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5882ed780545SJohannes Berg 	}
5883ed780545SJohannes Berg 
5884e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
5885e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5886e705c121SKalle Valo 		return;
5887e705c121SKalle Valo 
5888f276e20bSJohannes Berg 	if (!vif->cfg.assoc)
5889e705c121SKalle Valo 		return;
5890e705c121SKalle Valo 
5891e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5892e705c121SKalle Valo 
5893c8ee33e1SGregory Greenman 	if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
5894e705c121SKalle Valo 		goto unlock;
5895e705c121SKalle Valo 
5896e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
5897e705c121SKalle Valo 		goto unlock;
5898e705c121SKalle Valo 
5899650cadb7SGregory Greenman 	sinfo->rx_beacon = mvmvif->deflink.beacon_stats.num_beacons +
5900650cadb7SGregory Greenman 			   mvmvif->deflink.beacon_stats.accu_num_beacons;
590122d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
5902650cadb7SGregory Greenman 	if (mvmvif->deflink.beacon_stats.avg_signal) {
5903e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
5904650cadb7SGregory Greenman 		sinfo->rx_beacon_signal_avg =
5905650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.avg_signal;
590622d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
5907e705c121SKalle Valo 	}
5908e705c121SKalle Valo  unlock:
5909e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5910e705c121SKalle Valo }
5911e705c121SKalle Valo 
5912119c2a13SMordechay Goodstein static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5913119c2a13SMordechay Goodstein 					    struct ieee80211_vif *vif,
5914119c2a13SMordechay Goodstein 					    const  struct ieee80211_mlme_event *mlme)
5915119c2a13SMordechay Goodstein {
59161a81bddfSMordechay Goodstein 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
59171a81bddfSMordechay Goodstein 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
5918119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5919119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5920119c2a13SMordechay Goodstein 				       NULL);
5921119c2a13SMordechay Goodstein 		return;
5922119c2a13SMordechay Goodstein 	}
5923119c2a13SMordechay Goodstein 
5924119c2a13SMordechay Goodstein 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5925119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5926119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_DEASSOC,
5927119c2a13SMordechay Goodstein 				       NULL);
5928119c2a13SMordechay Goodstein 		return;
5929119c2a13SMordechay Goodstein 	}
5930119c2a13SMordechay Goodstein }
5931119c2a13SMordechay Goodstein 
5932e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5933e705c121SKalle Valo 					struct ieee80211_vif *vif,
5934e705c121SKalle Valo 					const struct ieee80211_event *event)
5935e705c121SKalle Valo {
59364c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5937e705c121SKalle Valo 	do {								\
59384c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5939e705c121SKalle Valo 			break;						\
59407174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5941e705c121SKalle Valo 	} while (0)
5942e705c121SKalle Valo 
5943e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5944e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5945e705c121SKalle Valo 
5946119c2a13SMordechay Goodstein 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5947119c2a13SMordechay Goodstein 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5948119c2a13SMordechay Goodstein 		return;
5949119c2a13SMordechay Goodstein 	}
5950119c2a13SMordechay Goodstein 
59516c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
59526c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
59536c042d75SSara Sharon 	if (!trig)
5954e705c121SKalle Valo 		return;
5955e705c121SKalle Valo 
5956e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
5957e705c121SKalle Valo 
5958e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
5959e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
59604c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
5961e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
5962e705c121SKalle Valo 					    event->u.mlme.reason);
5963e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
59644c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
5965e705c121SKalle Valo 					   "ASSOC TIMEOUT");
5966e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
5967e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
59684c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
5969e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
5970e705c121SKalle Valo 					   event->u.mlme.reason);
5971e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
59724c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
5973e705c121SKalle Valo 					   "AUTH TIMEOUT");
5974e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
59754c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
5976e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
5977e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
59784c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
5979e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
5980e705c121SKalle Valo 	}
5981e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
5982e705c121SKalle Valo }
5983e705c121SKalle Valo 
5984e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5985e705c121SKalle Valo 					  struct ieee80211_vif *vif,
5986e705c121SKalle Valo 					  const struct ieee80211_event *event)
5987e705c121SKalle Valo {
5988e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5989e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
5990e705c121SKalle Valo 
59916c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
59926c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
59936c042d75SSara Sharon 	if (!trig)
5994e705c121SKalle Valo 		return;
5995e705c121SKalle Valo 
5996e705c121SKalle Valo 	ba_trig = (void *)trig->data;
5997e705c121SKalle Valo 
5998e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5999e705c121SKalle Valo 		return;
6000e705c121SKalle Valo 
60017174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
6002e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
6003e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
6004e705c121SKalle Valo 				event->u.ba.ssn);
6005e705c121SKalle Valo }
6006e705c121SKalle Valo 
6007cbce62a3SMiri Korenblit void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
6008e705c121SKalle Valo 				struct ieee80211_vif *vif,
6009e705c121SKalle Valo 				const struct ieee80211_event *event)
6010e705c121SKalle Valo {
6011e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6012e705c121SKalle Valo 
6013e705c121SKalle Valo 	switch (event->type) {
6014e705c121SKalle Valo 	case MLME_EVENT:
6015e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
6016e705c121SKalle Valo 		break;
6017e705c121SKalle Valo 	case BAR_RX_EVENT:
6018e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
6019e705c121SKalle Valo 		break;
6020e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
6021528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
6022528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
6023e705c121SKalle Valo 		break;
6024e705c121SKalle Valo 	default:
6025e705c121SKalle Valo 		break;
6026e705c121SKalle Valo 	}
6027e705c121SKalle Valo }
6028e705c121SKalle Valo 
6029d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
60305e1688ceSJohannes Berg 				     enum iwl_mvm_rxq_notif_type type,
60315e1688ceSJohannes Berg 				     bool sync,
60325e1688ceSJohannes Berg 				     const void *data, u32 size)
60330636b938SSara Sharon {
60345e1688ceSJohannes Berg 	struct {
60355e1688ceSJohannes Berg 		struct iwl_rxq_sync_cmd cmd;
60365e1688ceSJohannes Berg 		struct iwl_mvm_internal_rxq_notif notif;
60375e1688ceSJohannes Berg 	} __packed cmd = {
60385e1688ceSJohannes Berg 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
60395e1688ceSJohannes Berg 		.cmd.count =
60405e1688ceSJohannes Berg 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
60415e1688ceSJohannes Berg 				    size),
60425e1688ceSJohannes Berg 		.notif.type = type,
60435e1688ceSJohannes Berg 		.notif.sync = sync,
60445e1688ceSJohannes Berg 	};
60455e1688ceSJohannes Berg 	struct iwl_host_cmd hcmd = {
60465e1688ceSJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
60475e1688ceSJohannes Berg 		.data[0] = &cmd,
60485e1688ceSJohannes Berg 		.len[0] = sizeof(cmd),
60495e1688ceSJohannes Berg 		.data[1] = data,
60505e1688ceSJohannes Berg 		.len[1] = size,
60515e1688ceSJohannes Berg 		.flags = sync ? 0 : CMD_ASYNC,
60525e1688ceSJohannes Berg 	};
60530636b938SSara Sharon 	int ret;
60540636b938SSara Sharon 
60555e1688ceSJohannes Berg 	/* size must be a multiple of DWORD */
60565e1688ceSJohannes Berg 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
60575e1688ceSJohannes Berg 		return;
60580636b938SSara Sharon 
6059cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
60600636b938SSara Sharon 		return;
60610636b938SSara Sharon 
60625e1688ceSJohannes Berg 	if (sync) {
60635e1688ceSJohannes Berg 		cmd.notif.cookie = mvm->queue_sync_cookie;
60642f7a04c7SJohannes Berg 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6065a2113cc4SNaftali Goldstein 	}
6066d0ff5d22SSara Sharon 
60675e1688ceSJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
60680636b938SSara Sharon 	if (ret) {
60690636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
60700636b938SSara Sharon 		goto out;
60710636b938SSara Sharon 	}
6072d0ff5d22SSara Sharon 
60735e1688ceSJohannes Berg 	if (sync) {
60743c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
60753a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
60762f7a04c7SJohannes Berg 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
60776ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
60780636b938SSara Sharon 					 HZ);
60792f7a04c7SJohannes Berg 		WARN_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm),
60802f7a04c7SJohannes Berg 			  "queue sync: failed to sync, state is 0x%lx\n",
60812f7a04c7SJohannes Berg 			  mvm->queue_sync_state);
60826ad04359SJohannes Berg 	}
60830636b938SSara Sharon 
60840636b938SSara Sharon out:
60855e1688ceSJohannes Berg 	if (sync) {
60862f7a04c7SJohannes Berg 		mvm->queue_sync_state = 0;
60870636b938SSara Sharon 		mvm->queue_sync_cookie++;
60880636b938SSara Sharon 	}
60895f8a3561SJohannes Berg }
60900636b938SSara Sharon 
6091cbce62a3SMiri Korenblit void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
60920636b938SSara Sharon {
60930636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
60940636b938SSara Sharon 
60950636b938SSara Sharon 	mutex_lock(&mvm->mutex);
60965e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
60970636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
60980636b938SSara Sharon }
60990636b938SSara Sharon 
6100cbce62a3SMiri Korenblit int
6101b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6102b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
6103b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
6104b73f9a4aSJohannes Berg {
6105b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6106b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6107b73f9a4aSJohannes Berg 
6108b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6109b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6110b73f9a4aSJohannes Berg 		return -EINVAL;
6111b73f9a4aSJohannes Berg 
6112b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
6113b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
6114b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
6115b73f9a4aSJohannes Berg 
6116b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6117b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6118b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
6119b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
6120b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6121b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6122b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6123b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6124b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6125b73f9a4aSJohannes Berg 
6126b73f9a4aSJohannes Berg 	return 0;
6127b73f9a4aSJohannes Berg }
6128b73f9a4aSJohannes Berg 
6129cbce62a3SMiri Korenblit int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6130fc36ffdaSJohannes Berg 		       struct cfg80211_pmsr_request *request)
6131fc36ffdaSJohannes Berg {
6132fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6133fc36ffdaSJohannes Berg 	int ret;
6134fc36ffdaSJohannes Berg 
6135fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
6136fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
6137fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
6138fc36ffdaSJohannes Berg 
6139fc36ffdaSJohannes Berg 	return ret;
6140fc36ffdaSJohannes Berg }
6141fc36ffdaSJohannes Berg 
6142cbce62a3SMiri Korenblit void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6143fc36ffdaSJohannes Berg 			struct cfg80211_pmsr_request *request)
6144fc36ffdaSJohannes Berg {
6145fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6146fc36ffdaSJohannes Berg 
6147fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
6148fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
6149fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
6150fc36ffdaSJohannes Berg }
6151fc36ffdaSJohannes Berg 
6152438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6153438af969SSara Sharon {
6154438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
6155438af969SSara Sharon 
6156438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
6157438af969SSara Sharon 		return false;
6158438af969SSara Sharon 
6159438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6160438af969SSara Sharon }
6161438af969SSara Sharon 
6162438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6163438af969SSara Sharon 				      struct sk_buff *head,
6164438af969SSara Sharon 				      struct sk_buff *skb)
6165438af969SSara Sharon {
6166438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6167438af969SSara Sharon 
6168438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
6169438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
6170438af969SSara Sharon 		return false;
6171438af969SSara Sharon 
6172438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
6173438af969SSara Sharon 		return true;
6174438af969SSara Sharon 
6175438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6176438af969SSara Sharon }
6177438af969SSara Sharon 
6178be8897e2SAvraham Stern int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6179cf85123aSAvraham Stern 			     struct ieee80211_vif *vif,
6180cf85123aSAvraham Stern 			     struct cfg80211_set_hw_timestamp *hwts)
6181cf85123aSAvraham Stern {
6182cf85123aSAvraham Stern 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6183cf85123aSAvraham Stern 	u32 protocols = 0;
6184cf85123aSAvraham Stern 	int ret;
6185cf85123aSAvraham Stern 
6186cf85123aSAvraham Stern 	/* HW timestamping is only supported for a specific station */
6187cf85123aSAvraham Stern 	if (!hwts->macaddr)
6188cf85123aSAvraham Stern 		return -EOPNOTSUPP;
6189cf85123aSAvraham Stern 
6190cf85123aSAvraham Stern 	if (hwts->enable)
6191cf85123aSAvraham Stern 		protocols =
6192cf85123aSAvraham Stern 			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6193cf85123aSAvraham Stern 
6194cf85123aSAvraham Stern 	mutex_lock(&mvm->mutex);
6195cf85123aSAvraham Stern 	ret = iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6196cf85123aSAvraham Stern 	mutex_unlock(&mvm->mutex);
6197cf85123aSAvraham Stern 
6198cf85123aSAvraham Stern 	return ret;
6199cf85123aSAvraham Stern }
6200cf85123aSAvraham Stern 
6201e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
6202e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
6203cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6204e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
6205e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
6206e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
6207e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
6208e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
6209e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
6210e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
6211e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
6212e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
6213e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
6214e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
6215e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
6216e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
6217e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6218e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6219e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
6220e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
6221e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6222e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6223e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6224e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
6225e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
6226e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
62276b1259d1SJohannes Berg 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6228e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6229e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
6230a6cc6ccbSJohannes Berg 	.flush_sta = iwl_mvm_mac_flush_sta,
6231e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
6232e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6233e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
6234e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
6235e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
6236e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
6237e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
6238e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
6239e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
6240e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6241e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6242e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6243e705c121SKalle Valo 
6244ae7ba17bSShaul Triebitz 	.start_ap = iwl_mvm_start_ap,
6245ae7ba17bSShaul Triebitz 	.stop_ap = iwl_mvm_stop_ap,
6246ae7ba17bSShaul Triebitz 	.join_ibss = iwl_mvm_start_ibss,
6247ae7ba17bSShaul Triebitz 	.leave_ibss = iwl_mvm_stop_ibss,
6248e705c121SKalle Valo 
62492f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
62502f0282dbSJohannes Berg 
6251e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
6252e705c121SKalle Valo 
6253e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
6254e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
6255e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
625679221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
6257c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6258e705c121SKalle Valo 
6259e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6260e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6261e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6262e705c121SKalle Valo 
6263e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
6264e705c121SKalle Valo 
62650636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
62660636b938SSara Sharon 
6267e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
6268e705c121SKalle Valo 
6269e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6270e705c121SKalle Valo 	/* look at d3.c */
6271e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
6272e705c121SKalle Valo 	.resume = iwl_mvm_resume,
6273e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
6274e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
6275e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
6276e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6277e705c121SKalle Valo #endif
6278e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6279e705c121SKalle Valo #endif
6280e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
6281e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
6282b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6283fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
6284fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
6285b73f9a4aSJohannes Berg 
6286438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6287177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
62883f244876SBenjamin Berg 	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
6289177a11cfSGregory Greenman #endif
6290cf85123aSAvraham Stern 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6291e705c121SKalle Valo };
6292