xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision 5f1fee9644759d07f3d3a468389c7c18027083d7)
18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
3f4eedfd8SJohannes Berg  * Copyright (C) 2012-2014, 2018-2024 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"
256584b9d0SEmmanuel Grumbach #include "iwl-nvm-utils.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),
1410d2fc882SMukesh Sisodiya 				      le32_to_cpu(resp->cap), resp_ver,
1420d2fc882SMukesh Sisodiya 				      mvm->fwrt.uats_enabled);
143e705c121SKalle Valo 	/* Store the return source id */
144e705c121SKalle Valo 	src_id = resp->source_id;
145e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
146e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
147e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
148e705c121SKalle Valo 		goto out;
149e705c121SKalle Valo 	}
150e705c121SKalle Valo 
151e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
152e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
153e705c121SKalle Valo 	mvm->lar_regdom_set = true;
154e705c121SKalle Valo 	mvm->mcc_src = src_id;
155e705c121SKalle Valo 
156ea5cca78SJohannes Berg 	/* Some kind of regulatory mess means we need to currently disallow
157ea5cca78SJohannes Berg 	 * puncturing in the US and Canada. Do that here, at least until we
158ea5cca78SJohannes Berg 	 * figure out the new chanctx APIs for puncturing.
159ea5cca78SJohannes Berg 	 */
160ea5cca78SJohannes Berg 	if (resp->mcc == cpu_to_le16(IWL_MCC_US) ||
161ea5cca78SJohannes Berg 	    resp->mcc == cpu_to_le16(IWL_MCC_CANADA))
162ea5cca78SJohannes Berg 		ieee80211_hw_set(mvm->hw, DISALLOW_PUNCTURING);
163ea5cca78SJohannes Berg 	else
164ea5cca78SJohannes Berg 		__clear_bit(IEEE80211_HW_DISALLOW_PUNCTURING, mvm->hw->flags);
165ea5cca78SJohannes Berg 
1666d19a5ebSEmmanuel Grumbach 	iwl_mei_set_country_code(__le16_to_cpu(resp->mcc));
1676d19a5ebSEmmanuel Grumbach 
168e705c121SKalle Valo out:
1696d19a5ebSEmmanuel Grumbach 	kfree(resp);
170e705c121SKalle Valo 	return regd;
171e705c121SKalle Valo }
172e705c121SKalle Valo 
173e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
174e705c121SKalle Valo {
175e705c121SKalle Valo 	bool changed;
176e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
177e705c121SKalle Valo 
178e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
179e705c121SKalle Valo 		return;
180e705c121SKalle Valo 
181e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
182e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
183e705c121SKalle Valo 		/* only update the regulatory core if changed */
184e705c121SKalle Valo 		if (changed)
185e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
186e705c121SKalle Valo 
187e705c121SKalle Valo 		kfree(regd);
188e705c121SKalle Valo 	}
189e705c121SKalle Valo }
190e705c121SKalle Valo 
191e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
192e705c121SKalle Valo 							  bool *changed)
193e705c121SKalle Valo {
194e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
195e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
196e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
197e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
198e705c121SKalle Valo }
199e705c121SKalle Valo 
200ea02a208SGregory Greenman int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm, bool force_regd_sync)
201e705c121SKalle Valo {
202e705c121SKalle Valo 	enum iwl_mcc_source used_src;
203e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
204e705c121SKalle Valo 	int ret;
205e705c121SKalle Valo 	bool changed;
206e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
207a05829a7SJohannes Berg 			wiphy_dereference(mvm->hw->wiphy, mvm->hw->wiphy->regd);
208e705c121SKalle Valo 
209e705c121SKalle Valo 	if (!r)
210e705c121SKalle Valo 		return -ENOENT;
211e705c121SKalle Valo 
212e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
213e705c121SKalle Valo 	used_src = mvm->mcc_src;
214e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
215e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
216e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
217e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
218e705c121SKalle Valo 			kfree(regd);
219e705c121SKalle Valo 	}
220e705c121SKalle Valo 
221e705c121SKalle Valo 	/* Now set our last stored MCC and source */
222e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
223e705c121SKalle Valo 				     &changed);
224e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
225e705c121SKalle Valo 		return -EIO;
226e705c121SKalle Valo 
227ea02a208SGregory Greenman 	/* update cfg80211 if the regdomain was changed or the caller explicitly
228ea02a208SGregory Greenman 	 * asked to update regdomain
229ea02a208SGregory Greenman 	 */
230ea02a208SGregory Greenman 	if (changed || force_regd_sync)
231a05829a7SJohannes Berg 		ret = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
232e705c121SKalle Valo 	else
233e705c121SKalle Valo 		ret = 0;
234e705c121SKalle Valo 
235e705c121SKalle Valo 	kfree(regd);
236e705c121SKalle Valo 	return ret;
237e705c121SKalle Valo }
238e705c121SKalle Valo 
2398e33f046SKrishnanand Prabhu /* Each capability added here should also be add to tm_if_types_ext_capa_sta */
2407f2ea521SYueHaibing static const u8 he_if_types_ext_capa_sta[] = {
2417360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
242918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
243784d4a42SJohannes Berg 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
244784d4a42SJohannes Berg 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
245784d4a42SJohannes Berg 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
2467360f99eSEmmanuel Grumbach };
2477360f99eSEmmanuel Grumbach 
2488e33f046SKrishnanand Prabhu static const u8 tm_if_types_ext_capa_sta[] = {
2498e33f046SKrishnanand Prabhu 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
2508e33f046SKrishnanand Prabhu 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT |
2518e33f046SKrishnanand Prabhu 	       WLAN_EXT_CAPA3_TIMING_MEASUREMENT_SUPPORT,
252784d4a42SJohannes Berg 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
253784d4a42SJohannes Berg 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
254784d4a42SJohannes Berg 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
2558e33f046SKrishnanand Prabhu 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
2568e33f046SKrishnanand Prabhu };
2578e33f046SKrishnanand Prabhu 
2588e33f046SKrishnanand Prabhu /* Additional interface types for which extended capabilities are
2598e33f046SKrishnanand Prabhu  * specified separately
2608e33f046SKrishnanand Prabhu  */
2616107f300SEmmanuel Grumbach 
2626107f300SEmmanuel Grumbach #define IWL_MVM_EMLSR_CAPA	(IEEE80211_EML_CAP_EMLSR_SUPP | \
2636107f300SEmmanuel Grumbach 				 IEEE80211_EML_CAP_EMLSR_PADDING_DELAY_32US << \
2646107f300SEmmanuel Grumbach 					__bf_shf(IEEE80211_EML_CAP_EMLSR_PADDING_DELAY) | \
2656107f300SEmmanuel Grumbach 				 IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_64US << \
2666107f300SEmmanuel Grumbach 					__bf_shf(IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY))
26780018499SAyala Beker #define IWL_MVM_MLD_CAPA_OPS FIELD_PREP_CONST( \
26880018499SAyala Beker 			IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP, \
26980018499SAyala Beker 			IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP_SAME)
2706107f300SEmmanuel Grumbach 
2718e33f046SKrishnanand Prabhu static const struct wiphy_iftype_ext_capab add_iftypes_ext_capa[] = {
2727360f99eSEmmanuel Grumbach 	{
2737360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
2747360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
2757360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
2767360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
2776107f300SEmmanuel Grumbach 		/* relevant only if EHT is supported */
2786107f300SEmmanuel Grumbach 		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
27980018499SAyala Beker 		.mld_capa_and_ops = IWL_MVM_MLD_CAPA_OPS,
2807360f99eSEmmanuel Grumbach 	},
2818e33f046SKrishnanand Prabhu 	{
2828e33f046SKrishnanand Prabhu 		.iftype = NL80211_IFTYPE_STATION,
2838e33f046SKrishnanand Prabhu 		.extended_capabilities = tm_if_types_ext_capa_sta,
2848e33f046SKrishnanand Prabhu 		.extended_capabilities_mask = tm_if_types_ext_capa_sta,
2858e33f046SKrishnanand Prabhu 		.extended_capabilities_len = sizeof(tm_if_types_ext_capa_sta),
2868e33f046SKrishnanand Prabhu 		/* relevant only if EHT is supported */
2876107f300SEmmanuel Grumbach 		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
28880018499SAyala Beker 		.mld_capa_and_ops = IWL_MVM_MLD_CAPA_OPS,
2898e33f046SKrishnanand Prabhu 	},
2907360f99eSEmmanuel Grumbach };
2917360f99eSEmmanuel Grumbach 
292cbce62a3SMiri Korenblit int iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
293e8503aecSBen Greear {
294e8503aecSBen Greear 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
295e8503aecSBen Greear 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
296e8503aecSBen Greear 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
297e8503aecSBen Greear 	return 0;
298e8503aecSBen Greear }
299e8503aecSBen Greear 
3004ea1ed1dSEmmanuel Grumbach int iwl_mvm_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3014ea1ed1dSEmmanuel Grumbach {
3024ea1ed1dSEmmanuel Grumbach 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3034ea1ed1dSEmmanuel Grumbach 
3044ea1ed1dSEmmanuel Grumbach 	/* This has been tested on those devices only */
3054ea1ed1dSEmmanuel Grumbach 	if (mvm->trans->trans_cfg->device_family != IWL_DEVICE_FAMILY_9000 &&
3064ea1ed1dSEmmanuel Grumbach 	    mvm->trans->trans_cfg->device_family != IWL_DEVICE_FAMILY_22000)
30796850912SAndrei Otcheretianski 		return -EOPNOTSUPP;
3084ea1ed1dSEmmanuel Grumbach 
3094ea1ed1dSEmmanuel Grumbach 	if (!mvm->nvm_data)
3104ea1ed1dSEmmanuel Grumbach 		return -EBUSY;
3114ea1ed1dSEmmanuel Grumbach 
3124ea1ed1dSEmmanuel Grumbach 	/* mac80211 ensures the device is not started,
3134ea1ed1dSEmmanuel Grumbach 	 * so the firmware cannot be running
3144ea1ed1dSEmmanuel Grumbach 	 */
3154ea1ed1dSEmmanuel Grumbach 
3164ea1ed1dSEmmanuel Grumbach 	mvm->set_tx_ant = tx_ant;
3174ea1ed1dSEmmanuel Grumbach 	mvm->set_rx_ant = rx_ant;
3184ea1ed1dSEmmanuel Grumbach 
3194ea1ed1dSEmmanuel Grumbach 	iwl_reinit_cab(mvm->trans, mvm->nvm_data, tx_ant, rx_ant, mvm->fw);
3204ea1ed1dSEmmanuel Grumbach 
3214ea1ed1dSEmmanuel Grumbach 	return 0;
3224ea1ed1dSEmmanuel Grumbach }
3234ea1ed1dSEmmanuel Grumbach 
324e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
325e705c121SKalle Valo {
326e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
327e705c121SKalle Valo 	int num_mac, ret, i;
328e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
329e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
330e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
331e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
332e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
333e705c121SKalle Valo 	};
3343f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
3353f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
3363f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3373f37c229SIdo Yariv #endif
338a5de7de7SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
339a5de7de7SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
340e705c121SKalle Valo 
341e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
342e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
343e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
344e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
345e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
346e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
347e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
348e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
349e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
350e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
351e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
352e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
353909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
35430433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
355520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
356cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
357cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
358ef2b47b8SJohannes Berg 
3590120e6b3SGregory Greenman 	/* Set this early since we need to have it for the check below */
360b3366330SJohannes Berg 	if (mvm->mld_api_is_used && mvm->nvm_data->sku_cap_11be_enable &&
361b3366330SJohannes Berg 	    !iwlwifi_mod_params.disable_11ax &&
362bbd6d0f8SJohannes Berg 	    !iwlwifi_mod_params.disable_11be) {
3635f404005SJohannes Berg 		hw->wiphy->flags |= WIPHY_FLAG_DISABLE_WEXT;
364bbd6d0f8SJohannes Berg 		/* we handle this already earlier, but need it for MLO */
365bbd6d0f8SJohannes Berg 		ieee80211_hw_set(hw, HANDLES_QUIET_CSA);
366bbd6d0f8SJohannes Berg 	}
3670120e6b3SGregory Greenman 
368ef2b47b8SJohannes Berg 	/* With MLD FW API, it tracks timing by itself,
369ef2b47b8SJohannes Berg 	 * no need for any timing from the host
370ef2b47b8SJohannes Berg 	 */
371ef2b47b8SJohannes Berg 	if (!mvm->mld_api_is_used)
372ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
373ef2b47b8SJohannes Berg 
374ef2b47b8SJohannes Berg 	/* We should probably have this, but mac80211
375ef2b47b8SJohannes Berg 	 * currently doesn't support it for MLO.
376ef2b47b8SJohannes Berg 	 */
377ef2b47b8SJohannes Berg 	if (!(hw->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
378ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
379ef2b47b8SJohannes Berg 
380cfb21b11SJohannes Berg 	/*
381cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
382cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
383cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
384cfb21b11SJohannes Berg 	 * assert EDC.
385cfb21b11SJohannes Berg 	 *
386cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
387cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
388cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
389cfb21b11SJohannes Berg 	 * devices.
390cfb21b11SJohannes Berg 	 */
3917d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
392438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
393438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
394ecaf71deSGregory Greenman 
3954243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
396ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
397ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
398ecaf71deSGregory Greenman 	}
399ecaf71deSGregory Greenman 
40029fa9a98SEmmanuel Grumbach 	/* We want to use the mac80211's reorder buffer for 9000 */
40129fa9a98SEmmanuel Grumbach 	if (iwl_mvm_has_new_rx_api(mvm) &&
40229fa9a98SEmmanuel Grumbach 	    mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_9000)
403b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
404960f864bSJohannes Berg 
405960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
406960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
40765e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
408960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
409960f864bSJohannes Berg 		/*
410960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
411960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
412960f864bSJohannes Berg 		 * for station powersave
413960f864bSJohannes Berg 		 */
414960f864bSJohannes Berg 		return -EINVAL;
415960f864bSJohannes Berg 	}
416e705c121SKalle Valo 
41780938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
41880938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
41980938abcSJohannes Berg 
4202d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
4212d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
4222d7cf549SJohannes Berg 
423366fc672SJohannes Berg 	hw->queues = IEEE80211_NUM_ACS;
424e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
425e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
426e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
427e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
428e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
429371a17edSJohannes Berg 
430371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
431371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
432371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
433371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
434371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
435371a17edSJohannes Berg 
4364243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
4379f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
4389f66a397SGregory Greenman 
439e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
440e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
441438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
442e705c121SKalle Valo 
4438e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
444e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
445e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
446e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
447e705c121SKalle Valo 
4482a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4492a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4502a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
4512a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4522a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4532a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
4542a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4552a53d166SAyala Beker 	}
4562a53d166SAyala Beker 
457f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
458f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
459f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
460f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
461f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
462f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
463f4bfdc5eSEmmanuel Grumbach 
464e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
465f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
466e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
4678e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4688e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4698e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
4708e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4718e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4728e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
4738e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4748e160ab8SAyala Beker 	}
475e705c121SKalle Valo 
476ef2e7a51SIlan Peer 	wiphy_ext_feature_set(hw->wiphy,
477ef2e7a51SIlan Peer 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
478d16b96b5SJohannes Berg 	wiphy_ext_feature_set(hw->wiphy,
479d16b96b5SJohannes Berg 			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
480ef2e7a51SIlan Peer 
481b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
482fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
483b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
484b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
485fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
486fc36ffdaSJohannes Berg 	}
487b73f9a4aSJohannes Berg 
488a5de7de7SJohannes Berg 	if (sec_key_ver &&
489a5de7de7SJohannes Berg 	    fw_has_capa(&mvm->fw->ucode_capa,
490a5de7de7SJohannes Berg 			IWL_UCODE_TLV_CAPA_BIGTK_TX_SUPPORT))
491a5de7de7SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
492a5de7de7SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION);
493a5de7de7SJohannes Berg 	else if (fw_has_capa(&mvm->fw->ucode_capa,
494b1fdc250SJohannes Berg 			     IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
495b1fdc250SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
496b1fdc250SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
497b1fdc250SJohannes Berg 
498cf85123aSAvraham Stern 	if (fw_has_capa(&mvm->fw->ucode_capa,
499cf85123aSAvraham Stern 			IWL_UCODE_TLV_CAPA_TIME_SYNC_BOTH_FTM_TM))
500cf85123aSAvraham Stern 		hw->wiphy->hw_timestamp_max_peers = 1;
501cf85123aSAvraham Stern 
502c289f5cdSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
503c289f5cdSJohannes Berg 			IWL_UCODE_TLV_CAPA_SPP_AMSDU_SUPPORT))
504c289f5cdSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
505c289f5cdSJohannes Berg 				      NL80211_EXT_FEATURE_SPP_AMSDU_SUPPORT);
506c289f5cdSJohannes Berg 
507e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
508e705c121SKalle Valo 	hw->wiphy->features |=
509e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
510e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
511e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
512e705c121SKalle Valo 
513e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
514e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
515e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
516cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
517e705c121SKalle Valo 
518e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
519e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
520e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
521e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
522e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
523e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
524e705c121SKalle Valo 
525e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
526e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
52733e3fd99SAlexander Wetzel 
52833e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
52933e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
53033e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
53133e3fd99SAlexander Wetzel 	 */
53233e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
53333e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
53433e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
535e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
536e47df5bdSJohannes Berg 
537e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
538e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
539e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
540e705c121SKalle Valo 	else
541e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
542e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
543e705c121SKalle Valo 
544a1910a7fSAndrei Otcheretianski 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
545a1910a7fSAndrei Otcheretianski 		wiphy_ext_feature_set(hw->wiphy,
546a1910a7fSAndrei Otcheretianski 				      NL80211_EXT_FEATURE_DFS_CONCURRENT);
547a1910a7fSAndrei Otcheretianski 
548e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
549e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
550eae94cf8SLuca Coelho 	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
551e705c121SKalle Valo 
552e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
553e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
554e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
555e705c121SKalle Valo 
556e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
5574f1847cfSJohannes Berg 	hw->max_listen_interval = IWL_MVM_CONN_LISTEN_INTERVAL;
558e705c121SKalle Valo 
559e705c121SKalle Valo 	/* Extract MAC address */
560e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
561e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
562e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
563e705c121SKalle Valo 
564e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
565e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
566e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
567e705c121SKalle Valo 
568e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
569e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
570e705c121SKalle Valo 		       ETH_ALEN);
571e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
572e705c121SKalle Valo 		hw->wiphy->n_addresses++;
573e705c121SKalle Valo 	}
574e705c121SKalle Valo 
575e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
576e705c121SKalle Valo 
577e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
578e705c121SKalle Valo 
579e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
580e705c121SKalle Valo 
581e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
582e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
583e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
584e705c121SKalle Valo 
585e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
586e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
587e705c121SKalle Valo 	else
588e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
589e705c121SKalle Valo 
59057fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
59157fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
59257fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
59357fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
59457fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
59557fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
596e705c121SKalle Valo 
597e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
598e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
599e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
600e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
60157fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
602e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
603e705c121SKalle Valo 	}
604eae94cf8SLuca Coelho 	if (fw_has_capa(&mvm->fw->ucode_capa,
605eae94cf8SLuca Coelho 			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
606eae94cf8SLuca Coelho 	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
607eae94cf8SLuca Coelho 		hw->wiphy->bands[NL80211_BAND_6GHZ] =
608eae94cf8SLuca Coelho 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
609e705c121SKalle Valo 
610e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
611e705c121SKalle Valo 
612e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
613e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
614e705c121SKalle Valo 	else
615e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
616e705c121SKalle Valo 
617ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
618e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
6195d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
620e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
621e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
622e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
623e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
624e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
625e705c121SKalle Valo 
626e705c121SKalle Valo 	/*
627e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
628e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
629e705c121SKalle Valo 	 */
630e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
631e705c121SKalle Valo 
632e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
633e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
634e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
635a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
636e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
637e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
638e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
639e705c121SKalle Valo 
640e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
641e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
642e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
643e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
644e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
645e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
646e705c121SKalle Valo 
647e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
648e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
649e705c121SKalle Valo 		hw->wiphy->features |=
650e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
651e705c121SKalle Valo 
652e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
653e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
654e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
655e705c121SKalle Valo 
656971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
6572a42aea7SNathan Errera 				  IWL_FW_CMD_VER_UNKNOWN) == 3)
6582a42aea7SNathan Errera 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
6592a42aea7SNathan Errera 
660aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
661aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
662aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
663aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
664aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
665aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
666aacf8f18SAvrahams Stern 	}
667aacf8f18SAvrahams Stern 
6688f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
669971cbe50SJohannes Berg 		u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
670773a042fSAvraham Stern 
6718f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6728f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
6738f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6748f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
6758f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6768f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
677773a042fSAvraham Stern 
678773a042fSAvraham Stern 		/* Old firmware also supports probe deferral and suppression */
679773a042fSAvraham Stern 		if (scan_ver < 15)
680773a042fSAvraham Stern 			wiphy_ext_feature_set(hw->wiphy,
681773a042fSAvraham Stern 					      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
6828f691af9SZamir, Roee 	}
6838f691af9SZamir, Roee 
6848e33f046SKrishnanand Prabhu 	hw->wiphy->iftype_ext_capab = NULL;
6858e33f046SKrishnanand Prabhu 	hw->wiphy->num_iftype_ext_capab = 0;
6868e33f046SKrishnanand Prabhu 
6877360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
6887360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
6898e33f046SKrishnanand Prabhu 		hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa;
6907360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
6918e33f046SKrishnanand Prabhu 			ARRAY_SIZE(add_iftypes_ext_capa) - 1;
692918cbf39SSara Sharon 
693918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
694918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
6957360f99eSEmmanuel Grumbach 	}
6967360f99eSEmmanuel Grumbach 
6978e33f046SKrishnanand Prabhu 	if (iwl_fw_lookup_cmd_ver(mvm->fw,
6988e33f046SKrishnanand Prabhu 				  WIDE_ID(DATA_PATH_GROUP,
6998e33f046SKrishnanand Prabhu 					  WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
7008e33f046SKrishnanand Prabhu 				  IWL_FW_CMD_VER_UNKNOWN) >= 1) {
7018e33f046SKrishnanand Prabhu 		IWL_DEBUG_INFO(mvm->trans, "Timing measurement supported\n");
7028e33f046SKrishnanand Prabhu 
7038e33f046SKrishnanand Prabhu 		if (!hw->wiphy->iftype_ext_capab) {
7048e33f046SKrishnanand Prabhu 			hw->wiphy->num_iftype_ext_capab = 1;
7058e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa +
7068e33f046SKrishnanand Prabhu 				ARRAY_SIZE(add_iftypes_ext_capa) - 1;
7078e33f046SKrishnanand Prabhu 		} else {
7088e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa + 1;
7098e33f046SKrishnanand Prabhu 		}
7108e33f046SKrishnanand Prabhu 	}
7118e33f046SKrishnanand Prabhu 
7128b720901SAvraham Stern 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(LOCATION_GROUP,
7138b720901SAvraham Stern 						   TOF_RANGE_REQ_CMD),
7148b720901SAvraham Stern 				  IWL_FW_CMD_VER_UNKNOWN) >= 11) {
7158b720901SAvraham Stern 		wiphy_ext_feature_set(hw->wiphy,
7168b720901SAvraham Stern 				      NL80211_EXT_FEATURE_PROT_RANGE_NEGO_AND_MEASURE);
7174d951e26SIlan Peer 
7184d951e26SIlan Peer 		if (fw_has_capa(&mvm->fw->ucode_capa,
7194d951e26SIlan Peer 				IWL_UCODE_TLV_CAPA_SECURE_LTF_SUPPORT))
7204d951e26SIlan Peer 			wiphy_ext_feature_set(hw->wiphy,
7214d951e26SIlan Peer 					      NL80211_EXT_FEATURE_SECURE_LTF);
7228b720901SAvraham Stern 	}
7238b720901SAvraham Stern 
724e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
725e705c121SKalle Valo 
726eb561c29SJohannes Berg 	ieee80211_hw_set(hw, DISALLOW_PUNCTURING_5GHZ);
727eb561c29SJohannes Berg 
728e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
7293f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
730e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
731e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
732e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
733e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
734e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
735e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
736e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
737e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
738e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
739e705c121SKalle Valo 
740e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
741e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
742e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
7435d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
7445d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
745e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
746e705c121SKalle Valo 	}
747e705c121SKalle Valo #endif
748e705c121SKalle Valo 
749e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
750e705c121SKalle Valo 	if (ret)
751e705c121SKalle Valo 		return ret;
752e705c121SKalle Valo 
753e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
754e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
755e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
756e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
757e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
758e705c121SKalle Valo 	}
759e705c121SKalle Valo 
760e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
761e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
762e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
763e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
764e705c121SKalle Valo 	}
765e705c121SKalle Valo 
766e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
76759fa61f3SEmmanuel Grumbach 	if (!iwl_mvm_is_csum_supported(mvm))
7686772aab7SJohannes Berg 		hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
76941837ca9SEmmanuel Grumbach 
77091b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
77191b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
77291b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
77391b08c2dSAviya Erenfeld 
7749c11d8a9SShaul Triebitz 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
7759c11d8a9SShaul Triebitz 		wiphy_ext_feature_set(hw->wiphy,
7769c11d8a9SShaul Triebitz 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
7779c11d8a9SShaul Triebitz 
778bfcfdb59SEmmanuel Grumbach 	iwl_mvm_vendor_cmds_register(mvm);
779bfcfdb59SEmmanuel Grumbach 
780e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
781e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
782e8503aecSBen Greear 
783de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
784de645e89SJohannes Berg 	if (ret) {
785de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
786e705c121SKalle Valo 	}
787e705c121SKalle Valo 
788e705c121SKalle Valo 	return ret;
789e705c121SKalle Valo }
790e705c121SKalle Valo 
791df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
792df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
793df2378abSJohannes Berg {
794df2378abSJohannes Berg 	if (likely(sta)) {
795df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
796df2378abSJohannes Berg 			return;
797df2378abSJohannes Berg 	} else {
798df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
799df2378abSJohannes Berg 			return;
800df2378abSJohannes Berg 	}
801df2378abSJohannes Berg 
802df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
803df2378abSJohannes Berg }
804df2378abSJohannes Berg 
805cbce62a3SMiri Korenblit void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
806cbce62a3SMiri Korenblit 		    struct ieee80211_tx_control *control, struct sk_buff *skb)
807e705c121SKalle Valo {
808e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
809e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
810e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
811e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
812cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
813cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
81490723da6SShaul Triebitz 	u32 link_id = u32_get_bits(info->control.flags,
81590723da6SShaul Triebitz 				   IEEE80211_TX_CTRL_MLO_LINK);
81690723da6SShaul Triebitz 	struct ieee80211_sta *tmp_sta = sta;
817e705c121SKalle Valo 
818e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
819e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
820e705c121SKalle Valo 		goto drop;
821e705c121SKalle Valo 	}
822e705c121SKalle Valo 
823cfbc6c4cSSara Sharon 	if (offchannel &&
824e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
825e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
826e705c121SKalle Valo 		goto drop;
827e705c121SKalle Valo 
82810516783SJohannes Berg 	/*
82910516783SJohannes Berg 	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
83010516783SJohannes Berg 	 * so we treat the others as broadcast
83110516783SJohannes Berg 	 */
83210516783SJohannes Berg 	if (ieee80211_is_mgmt(hdr->frame_control))
833e705c121SKalle Valo 		sta = NULL;
834e705c121SKalle Valo 
835dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
836cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
837cfbc6c4cSSara Sharon 	    !offchannel) {
838dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
839dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
840650cadb7SGregory Greenman 		u8 ap_sta_id = READ_ONCE(mvmvif->deflink.ap_sta_id);
841dc1aca22SAndrei Otcheretianski 
842be9ae34eSNathan Errera 		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
843dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
844dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
845dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
846dc1aca22SAndrei Otcheretianski 				goto drop;
847dc1aca22SAndrei Otcheretianski 		}
848dc1aca22SAndrei Otcheretianski 	}
849dc1aca22SAndrei Otcheretianski 
85090723da6SShaul Triebitz 	if (tmp_sta && !sta && link_id != IEEE80211_LINK_UNSPECIFIED &&
85190723da6SShaul Triebitz 	    !ieee80211_is_probe_resp(hdr->frame_control)) {
85290723da6SShaul Triebitz 		/* translate MLD addresses to LINK addresses */
85390723da6SShaul Triebitz 		struct ieee80211_link_sta *link_sta =
85490723da6SShaul Triebitz 			rcu_dereference(tmp_sta->link[link_id]);
85590723da6SShaul Triebitz 		struct ieee80211_bss_conf *link_conf =
85690723da6SShaul Triebitz 			rcu_dereference(info->control.vif->link_conf[link_id]);
85790723da6SShaul Triebitz 		struct ieee80211_mgmt *mgmt;
85890723da6SShaul Triebitz 
85990723da6SShaul Triebitz 		if (WARN_ON(!link_sta || !link_conf))
86090723da6SShaul Triebitz 			goto drop;
86190723da6SShaul Triebitz 
86290723da6SShaul Triebitz 		/* if sta is NULL, the frame is a management frame */
86390723da6SShaul Triebitz 		mgmt = (void *)hdr;
86490723da6SShaul Triebitz 		memcpy(mgmt->da, link_sta->addr, ETH_ALEN);
86590723da6SShaul Triebitz 		memcpy(mgmt->sa, link_conf->addr, ETH_ALEN);
86690723da6SShaul Triebitz 		memcpy(mgmt->bssid, link_conf->bssid, ETH_ALEN);
86790723da6SShaul Triebitz 	}
86890723da6SShaul Triebitz 
869df2378abSJohannes Berg 	iwl_mvm_tx_skb(mvm, skb, sta);
870e705c121SKalle Valo 	return;
871e705c121SKalle Valo  drop:
872e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
873e705c121SKalle Valo }
874e705c121SKalle Valo 
875cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
876e705c121SKalle Valo {
877cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
878cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
879cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
880cfbc6c4cSSara Sharon 
881fba8248eSSara Sharon 	/*
882fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
883fba8248eSSara Sharon 	 * taker all the work.
884fba8248eSSara Sharon 	 *
885fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
886fba8248eSSara Sharon 	 *
887fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
888fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
889fba8248eSSara Sharon 	 *
890fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
891fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
892fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
893fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
894fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
895fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
896fba8248eSSara Sharon 	 *
897fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
898fba8248eSSara Sharon 	 * check the queue, so do nothing.
899fba8248eSSara Sharon 	 */
900fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
901fba8248eSSara Sharon 		return;
902cfbc6c4cSSara Sharon 
903cfbc6c4cSSara Sharon 	rcu_read_lock();
904fba8248eSSara Sharon 	do {
905b58e3d43SJohannes Berg 		while (likely(!test_bit(IWL_MVM_TXQ_STATE_STOP_FULL,
906b58e3d43SJohannes Berg 					&mvmtxq->state) &&
907b58e3d43SJohannes Berg 			      !test_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT,
908b58e3d43SJohannes Berg 					&mvmtxq->state) &&
909bbd6d0f8SJohannes Berg 			      !test_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA,
910bbd6d0f8SJohannes Berg 					&mvmtxq->state) &&
91100520b7aSEmmanuel Grumbach 			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
912cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
913cfbc6c4cSSara Sharon 
914f50d693bSSara Sharon 			if (!skb) {
915f50d693bSSara Sharon 				if (txq->sta)
916f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
917f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
918f50d693bSSara Sharon 						     txq->sta->addr,
919f50d693bSSara Sharon 						     txq->tid);
920cfbc6c4cSSara Sharon 				break;
921f50d693bSSara Sharon 			}
922cfbc6c4cSSara Sharon 
923cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
924cfbc6c4cSSara Sharon 		}
925fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
926cfbc6c4cSSara Sharon 	rcu_read_unlock();
927e705c121SKalle Valo }
928e705c121SKalle Valo 
929cbce62a3SMiri Korenblit void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
930cfbc6c4cSSara Sharon 			       struct ieee80211_txq *txq)
931e705c121SKalle Valo {
932cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
933cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
934e705c121SKalle Valo 
935923bf981SJohannes Berg 	if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
936923bf981SJohannes Berg 	    !txq->sta) {
937cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
938cfbc6c4cSSara Sharon 		return;
939cfbc6c4cSSara Sharon 	}
940cfbc6c4cSSara Sharon 
941923bf981SJohannes Berg 	/* iwl_mvm_mac_itxq_xmit() will later be called by the worker
942923bf981SJohannes Berg 	 * to handle any packets we leave on the txq now
943923bf981SJohannes Berg 	 */
944cfbc6c4cSSara Sharon 
945923bf981SJohannes Berg 	spin_lock_bh(&mvm->add_stream_lock);
946923bf981SJohannes Berg 	/* The list is being deleted only after the queue is fully allocated. */
947923bf981SJohannes Berg 	if (list_empty(&mvmtxq->list) &&
948923bf981SJohannes Berg 	    /* recheck under lock */
949923bf981SJohannes Berg 	    !test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) {
950cfbc6c4cSSara Sharon 		list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
951cfbc6c4cSSara Sharon 		schedule_work(&mvm->add_stream_wk);
952e705c121SKalle Valo 	}
953923bf981SJohannes Berg 	spin_unlock_bh(&mvm->add_stream_lock);
954923bf981SJohannes Berg }
955e705c121SKalle Valo 
956e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
957e705c121SKalle Valo 	do {								\
958e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
959e705c121SKalle Valo 			break;						\
9607174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
961e705c121SKalle Valo 	} while (0)
962e705c121SKalle Valo 
963e705c121SKalle Valo static void
964e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
965e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
966e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
967e705c121SKalle Valo {
968e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
969e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
970e705c121SKalle Valo 
9716c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
9726c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
9736c042d75SSara Sharon 	if (!trig)
974e705c121SKalle Valo 		return;
975e705c121SKalle Valo 
976e705c121SKalle Valo 	ba_trig = (void *)trig->data;
977e705c121SKalle Valo 
978e705c121SKalle Valo 	switch (action) {
979e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
980e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
981e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
982e705c121SKalle Valo 
983e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
984e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
985e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
986e705c121SKalle Valo 		break;
987e705c121SKalle Valo 		}
988e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
989e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
990e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
991e705c121SKalle Valo 				 sta->addr, tid);
992e705c121SKalle Valo 		break;
993e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
994e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
995e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
996e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
997e705c121SKalle Valo 		break;
998e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
999e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
1000e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
1001e705c121SKalle Valo 				 sta->addr, tid);
1002e705c121SKalle Valo 		break;
1003e705c121SKalle Valo 	default:
1004e705c121SKalle Valo 		break;
1005e705c121SKalle Valo 	}
1006e705c121SKalle Valo }
1007e705c121SKalle Valo 
1008cbce62a3SMiri Korenblit int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
1009e705c121SKalle Valo 			     struct ieee80211_vif *vif,
101050ea05efSSara Sharon 			     struct ieee80211_ampdu_params *params)
1011e705c121SKalle Valo {
1012e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1013e705c121SKalle Valo 	int ret;
101450ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
101550ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
101650ea05efSSara Sharon 	u16 tid = params->tid;
101750ea05efSSara Sharon 	u16 *ssn = &params->ssn;
1018514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
1019bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
102010b2b201SSara Sharon 	u16 timeout = params->timeout;
1021e705c121SKalle Valo 
1022e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
1023e705c121SKalle Valo 		     sta->addr, tid, action);
1024e705c121SKalle Valo 
1025e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
1026e705c121SKalle Valo 		return -EACCES;
1027e705c121SKalle Valo 
1028e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1029e705c121SKalle Valo 
1030e705c121SKalle Valo 	switch (action) {
1031e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
1032650cadb7SGregory Greenman 		if (iwl_mvm_vif_from_mac80211(vif)->deflink.ap_sta_id ==
1033c8ee33e1SGregory Greenman 		    iwl_mvm_sta_from_mac80211(sta)->deflink.sta_id) {
1034b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
1035b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
1036b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
1037b0ffe455SJohannes Berg 
1038b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
1039b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
1040b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
1041b0ffe455SJohannes Berg 		}
1042e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
1043e705c121SKalle Valo 			ret = -EINVAL;
1044e705c121SKalle Valo 			break;
1045e705c121SKalle Valo 		}
104610b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
104710b2b201SSara Sharon 					 timeout);
1048e705c121SKalle Valo 		break;
1049e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
105010b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
105110b2b201SSara Sharon 					 timeout);
1052e705c121SKalle Valo 		break;
1053e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
1054e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
1055e705c121SKalle Valo 			ret = -EINVAL;
1056e705c121SKalle Valo 			break;
1057e705c121SKalle Valo 		}
1058e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1059e705c121SKalle Valo 		break;
1060e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
1061e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1062e705c121SKalle Valo 		break;
1063e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1064e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1065e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1066e705c121SKalle Valo 		break;
1067e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1068bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1069bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
1070e705c121SKalle Valo 		break;
1071e705c121SKalle Valo 	default:
1072e705c121SKalle Valo 		WARN_ON_ONCE(1);
1073e705c121SKalle Valo 		ret = -EINVAL;
1074e705c121SKalle Valo 		break;
1075e705c121SKalle Valo 	}
1076e705c121SKalle Valo 
1077e705c121SKalle Valo 	if (!ret) {
1078e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
1079e705c121SKalle Valo 
1080e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1081e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1082e705c121SKalle Valo 
1083e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1084e705c121SKalle Valo 					    rx_ba_ssn, action);
1085e705c121SKalle Valo 	}
1086e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1087e705c121SKalle Valo 
1088e705c121SKalle Valo 	return ret;
1089e705c121SKalle Valo }
1090e705c121SKalle Valo 
1091e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1092e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1093e705c121SKalle Valo {
1094e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1095e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1096bf976c81SJohannes Berg 	struct iwl_probe_resp_data *probe_data;
109779faae3aSGregory Greenman 	unsigned int link_id;
1098e705c121SKalle Valo 
1099e705c121SKalle Valo 	mvmvif->uploaded = false;
1100e705c121SKalle Valo 
1101e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1102e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1103e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1104e705c121SKalle Valo 
1105c1e458b9SIlan Peer 	mvmvif->bf_enabled = false;
1106c1e458b9SIlan Peer 	mvmvif->ba_enabled = false;
1107828c79d9SEmmanuel Grumbach 	mvmvif->ap_sta = NULL;
110879faae3aSGregory Greenman 
1109f2c2799eSBenjamin Berg 	mvmvif->esr_active = false;
1110f2c2799eSBenjamin Berg 	vif->driver_flags &= ~IEEE80211_VIF_EML_ACTIVE;
1111f2c2799eSBenjamin Berg 
111279faae3aSGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
111379faae3aSGregory Greenman 		mvmvif->link[link_id]->ap_sta_id = IWL_MVM_INVALID_STA;
111479faae3aSGregory Greenman 		mvmvif->link[link_id]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
111579faae3aSGregory Greenman 		mvmvif->link[link_id]->phy_ctxt = NULL;
1116bf976c81SJohannes Berg 		mvmvif->link[link_id]->active = 0;
1117d615ea32SJohannes Berg 		mvmvif->link[link_id]->igtk = NULL;
1118c1e458b9SIlan Peer 		memset(&mvmvif->link[link_id]->bf_data, 0,
1119c1e458b9SIlan Peer 		       sizeof(mvmvif->link[link_id]->bf_data));
112079faae3aSGregory Greenman 	}
1121bf976c81SJohannes Berg 
1122bf976c81SJohannes Berg 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1123bf976c81SJohannes Berg 					       lockdep_is_held(&mvm->mutex));
1124bf976c81SJohannes Berg 	if (probe_data)
1125bf976c81SJohannes Berg 		kfree_rcu(probe_data, rcu_head);
1126bf976c81SJohannes Berg 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1127e705c121SKalle Valo }
1128e705c121SKalle Valo 
1129cc3ba78fSBenjamin Berg static void iwl_mvm_cleanup_sta_iterator(void *data, struct ieee80211_sta *sta)
1130cc3ba78fSBenjamin Berg {
1131cc3ba78fSBenjamin Berg 	struct iwl_mvm *mvm = data;
1132cc3ba78fSBenjamin Berg 	struct iwl_mvm_sta *mvm_sta;
1133cc3ba78fSBenjamin Berg 	struct ieee80211_vif *vif;
1134cc3ba78fSBenjamin Berg 	int link_id;
1135cc3ba78fSBenjamin Berg 
1136cc3ba78fSBenjamin Berg 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1137cc3ba78fSBenjamin Berg 	vif = mvm_sta->vif;
1138cc3ba78fSBenjamin Berg 
1139cc3ba78fSBenjamin Berg 	if (!sta->valid_links)
1140cc3ba78fSBenjamin Berg 		return;
1141cc3ba78fSBenjamin Berg 
1142cc3ba78fSBenjamin Berg 	for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
1143cc3ba78fSBenjamin Berg 		struct iwl_mvm_link_sta *mvm_link_sta;
1144cc3ba78fSBenjamin Berg 
1145cc3ba78fSBenjamin Berg 		mvm_link_sta =
1146cc3ba78fSBenjamin Berg 			rcu_dereference_check(mvm_sta->link[link_id],
1147cc3ba78fSBenjamin Berg 					      lockdep_is_held(&mvm->mutex));
1148cc3ba78fSBenjamin Berg 		if (mvm_link_sta && !(vif->active_links & BIT(link_id))) {
1149cc3ba78fSBenjamin Berg 			/*
1150cc3ba78fSBenjamin Berg 			 * We have a link STA but the link is inactive in
1151cc3ba78fSBenjamin Berg 			 * mac80211. This will happen if we failed to
1152cc3ba78fSBenjamin Berg 			 * deactivate the link but mac80211 roll back the
1153cc3ba78fSBenjamin Berg 			 * deactivation of the link.
1154cc3ba78fSBenjamin Berg 			 * Delete the stale data to avoid issues later on.
1155cc3ba78fSBenjamin Berg 			 */
1156cc3ba78fSBenjamin Berg 			iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_link_sta,
1157cc3ba78fSBenjamin Berg 						  link_id, false);
1158cc3ba78fSBenjamin Berg 		}
1159cc3ba78fSBenjamin Berg 	}
1160cc3ba78fSBenjamin Berg }
1161cc3ba78fSBenjamin Berg 
1162e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1163e705c121SKalle Valo {
1164fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1165e705c121SKalle Valo 
11669bf13beeSJohannes Berg 	mvm->cur_aid = 0;
11679bf13beeSJohannes Berg 
1168e705c121SKalle Valo 	mvm->scan_status = 0;
1169e705c121SKalle Valo 	mvm->ps_disabled = false;
1170b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1171e705c121SKalle Valo 
1172e705c121SKalle Valo 	/* just in case one was running */
1173305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1174e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1175e705c121SKalle Valo 
1176fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1177fc36ffdaSJohannes Berg 
1178e705c121SKalle Valo 	/*
1179e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1180e705c121SKalle Valo 	 * gone down during the HW restart
1181e705c121SKalle Valo 	 */
1182e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1183e705c121SKalle Valo 
1184cc3ba78fSBenjamin Berg 	/* cleanup stations as links may be gone after restart */
1185cc3ba78fSBenjamin Berg 	ieee80211_iterate_stations_atomic(mvm->hw,
1186cc3ba78fSBenjamin Berg 					  iwl_mvm_cleanup_sta_iterator, mvm);
1187cc3ba78fSBenjamin Berg 
1188e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1189e705c121SKalle Valo 
1190e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
11919c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1192e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1193e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1194e705c121SKalle Valo 
1195e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1196e705c121SKalle Valo 
1197e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
11987174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1199baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1200fb40cd9dSMiri Korenblit #ifdef CONFIG_IWLWIFI_DEBUGFS
1201fb40cd9dSMiri Korenblit 	mvm->beacon_inject_active = false;
1202fb40cd9dSMiri Korenblit #endif
1203e705c121SKalle Valo 
1204e705c121SKalle Valo 	/* keep statistics ticking */
1205e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1206e705c121SKalle Valo }
1207e705c121SKalle Valo 
1208e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1209e705c121SKalle Valo {
1210e705c121SKalle Valo 	int ret;
1211e705c121SKalle Valo 
1212e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1213e705c121SKalle Valo 
12146d19a5ebSEmmanuel Grumbach 	ret = iwl_mvm_mei_get_ownership(mvm);
12156d19a5ebSEmmanuel Grumbach 	if (ret)
12166d19a5ebSEmmanuel Grumbach 		return ret;
12176d19a5ebSEmmanuel Grumbach 
12186d19a5ebSEmmanuel Grumbach 	if (mvm->mei_nvm_data) {
12196d19a5ebSEmmanuel Grumbach 		/* We got the NIC, we can now free the MEI NVM data */
12206d19a5ebSEmmanuel Grumbach 		kfree(mvm->mei_nvm_data);
12216d19a5ebSEmmanuel Grumbach 		mvm->mei_nvm_data = NULL;
12226d19a5ebSEmmanuel Grumbach 
12236d19a5ebSEmmanuel Grumbach 		/*
12246d19a5ebSEmmanuel Grumbach 		 * We can't free the nvm_data we allocated based on the SAP
12256d19a5ebSEmmanuel Grumbach 		 * data because we registered to cfg80211 with the channels
12266d19a5ebSEmmanuel Grumbach 		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
12276d19a5ebSEmmanuel Grumbach 		 * just in order to be able free it later.
12286d19a5ebSEmmanuel Grumbach 		 * NULLify nvm_data so that we will read the NVM from the
12296d19a5ebSEmmanuel Grumbach 		 * firmware this time.
12306d19a5ebSEmmanuel Grumbach 		 */
12316d19a5ebSEmmanuel Grumbach 		mvm->temp_nvm_data = mvm->nvm_data;
12326d19a5ebSEmmanuel Grumbach 		mvm->nvm_data = NULL;
12336d19a5ebSEmmanuel Grumbach 	}
12346d19a5ebSEmmanuel Grumbach 
1235bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1236bf8b286fSJohannes Berg 		/*
1237bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1238bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1239bf8b286fSJohannes Berg 		 */
1240bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1241bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1242e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1243e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1244a42b2af3SLuca Coelho 	}
1245e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1246e705c121SKalle Valo 
1247b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1248b108d8c7SShahar S Matityahu 			       NULL);
1249b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1250b108d8c7SShahar S Matityahu 			       NULL);
1251da2eb669SSara Sharon 
1252e8fe3b41SIlan Peer 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1253e8fe3b41SIlan Peer 
1254e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1255e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1256e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1257e705c121SKalle Valo 		 * would do.
1258e705c121SKalle Valo 		 */
1259e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1260e705c121SKalle Valo 	}
1261e705c121SKalle Valo 
1262e705c121SKalle Valo 	return ret;
1263e705c121SKalle Valo }
1264e705c121SKalle Valo 
1265cbce62a3SMiri Korenblit int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1266e705c121SKalle Valo {
1267e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1268e705c121SKalle Valo 	int ret;
1269e705c121SKalle Valo 
1270e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
12715283dd67SMordechay Goodstein 
12725283dd67SMordechay Goodstein 	/* we are starting the mac not in error flow, and restart is enabled */
12735283dd67SMordechay Goodstein 	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
12745283dd67SMordechay Goodstein 	    iwlwifi_mod_params.fw_restart) {
12755283dd67SMordechay Goodstein 		/*
12765283dd67SMordechay Goodstein 		 * This will prevent mac80211 recovery flows to trigger during
12775283dd67SMordechay Goodstein 		 * init failures
12785283dd67SMordechay Goodstein 		 */
12795283dd67SMordechay Goodstein 		set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
12805283dd67SMordechay Goodstein 	}
12815283dd67SMordechay Goodstein 
1282e705c121SKalle Valo 	ret = __iwl_mvm_mac_start(mvm);
12835283dd67SMordechay Goodstein 	clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
12845283dd67SMordechay Goodstein 
1285e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1286e705c121SKalle Valo 
12877ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
12887ce1f215SEmmanuel Grumbach 
1289e705c121SKalle Valo 	return ret;
1290e705c121SKalle Valo }
1291e705c121SKalle Valo 
1292e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1293e705c121SKalle Valo {
1294e705c121SKalle Valo 	int ret;
1295e705c121SKalle Valo 
12963f312651SJohannes Berg 	guard(mvm)(mvm);
1297e705c121SKalle Valo 
1298e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
12994d4183c4SEmmanuel Grumbach 
1300e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1301e705c121SKalle Valo 	if (ret)
1302e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1303e705c121SKalle Valo 			ret);
1304e705c121SKalle Valo 
1305f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1306f130bb75SMordechay Goodstein 
1307e705c121SKalle Valo 	/*
1308e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1309e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1310e705c121SKalle Valo 	 */
1311e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1312e705c121SKalle Valo }
1313e705c121SKalle Valo 
1314cbce62a3SMiri Korenblit void iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1315e705c121SKalle Valo 				   enum ieee80211_reconfig_type reconfig_type)
1316e705c121SKalle Valo {
1317e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1318e705c121SKalle Valo 
1319e705c121SKalle Valo 	switch (reconfig_type) {
1320e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1321e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1322e705c121SKalle Valo 		break;
1323e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1324e705c121SKalle Valo 		break;
1325e705c121SKalle Valo 	}
1326e705c121SKalle Valo }
1327e705c121SKalle Valo 
1328e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1329e705c121SKalle Valo {
1330e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1331e705c121SKalle Valo 
1332b68bd2e3SIlan Peer 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1333b68bd2e3SIlan Peer 
1334e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1335e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1336e705c121SKalle Valo 	 */
1337e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1338e705c121SKalle Valo 
1339e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1340e705c121SKalle Valo 
13411724fc78SEmmanuel Grumbach 	if (!iwl_mvm_has_new_station_api(mvm->fw))
1342f327236dSSharon 		iwl_mvm_rm_aux_sta(mvm);
1343f327236dSSharon 
1344fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1345e705c121SKalle Valo 
1346e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1347e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1348e705c121SKalle Valo 
1349e705c121SKalle Valo 	/*
1350155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1351155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1352155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1353e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1354e705c121SKalle Valo 	 * HW restart was requested.
1355e705c121SKalle Valo 	 */
1356155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1357155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1358155f7e04SEmmanuel Grumbach 			       &mvm->status))
1359e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1360e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1361e705c121SKalle Valo 
1362e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1363e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1364e705c121SKalle Valo 	 */
1365e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1366e705c121SKalle Valo 		int i;
1367e705c121SKalle Valo 
1368e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1369e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1370e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1371e705c121SKalle Valo 				      i))
1372e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1373e705c121SKalle Valo 		}
1374e705c121SKalle Valo 	}
1375e705c121SKalle Valo }
1376e705c121SKalle Valo 
1377cbce62a3SMiri Korenblit void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1378e705c121SKalle Valo {
1379e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1380e705c121SKalle Valo 
138138b3998dSIlan Peer 	/* Stop internal MLO scan, if running */
138238b3998dSIlan Peer 	mutex_lock(&mvm->mutex);
138338b3998dSIlan Peer 	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_INT_MLO, false);
138438b3998dSIlan Peer 	mutex_unlock(&mvm->mutex);
138538b3998dSIlan Peer 
13862e194efaSIlan Peer 	wiphy_work_cancel(mvm->hw->wiphy, &mvm->trig_link_selection_wk);
138707bf5297SMiri Korenblit 	wiphy_work_flush(mvm->hw->wiphy, &mvm->async_handlers_wiphy_wk);
1388e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
138924afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1390771147b0SJohannes Berg 
1391771147b0SJohannes Berg 	/*
1392771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1393771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1394771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
13957174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1396771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1397771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1398771147b0SJohannes Berg 	 */
1399771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1400771147b0SJohannes Berg 
1401d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
140269e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1403e705c121SKalle Valo 
1404f9084775SNathan Errera 	/*
1405f9084775SNathan Errera 	 * The work item could be running or queued if the
1406f9084775SNathan Errera 	 * ROC time event stops just as we get here.
1407f9084775SNathan Errera 	 */
1408f9084775SNathan Errera 	flush_work(&mvm->roc_done_wk);
1409f9084775SNathan Errera 
14107ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
14117ce1f215SEmmanuel Grumbach 
1412e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1413e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1414e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1415e705c121SKalle Valo 
1416e705c121SKalle Valo 	/*
1417e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1418e705c121SKalle Valo 	 * discover that its list is now empty.
1419e705c121SKalle Valo 	 */
1420e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1421fdccafadSMiri Korenblit 	wiphy_work_cancel(hw->wiphy, &mvm->async_handlers_wiphy_wk);
1422e705c121SKalle Valo }
1423e705c121SKalle Valo 
14241ab26632SMiri Korenblit struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1425e705c121SKalle Valo {
1426e705c121SKalle Valo 	u16 i;
1427e705c121SKalle Valo 
1428e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1429e705c121SKalle Valo 
1430e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1431e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1432e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1433e705c121SKalle Valo 
1434e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1435e705c121SKalle Valo 	return NULL;
1436e705c121SKalle Valo }
1437e705c121SKalle Valo 
1438f551d013SGregory Greenman int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1439e705c121SKalle Valo 			 s16 tx_power)
1440e705c121SKalle Valo {
1441971cbe50SJohannes Berg 	u32 cmd_id = REDUCE_TX_POWER_CMD;
14420791c2fcSHaim Dreyfuss 	int len;
1443216cdfb5SLuca Coelho 	struct iwl_dev_tx_power_cmd cmd = {
1444216cdfb5SLuca Coelho 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1445216cdfb5SLuca Coelho 		.common.mac_context_id =
1446e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1447216cdfb5SLuca Coelho 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1448e705c121SKalle Valo 	};
1449971cbe50SJohannes Berg 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1450e80bfd11SMordechay Goodstein 					   IWL_FW_CMD_VER_UNKNOWN);
1451e705c121SKalle Valo 
1452e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1453216cdfb5SLuca Coelho 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1454e705c121SKalle Valo 
14558f892e22SEmmanuel Grumbach 	if (cmd_ver == 8)
14568f892e22SEmmanuel Grumbach 		len = sizeof(cmd.v8);
14578f892e22SEmmanuel Grumbach 	else if (cmd_ver == 7)
1458b0aa02b3SAyala Barazani 		len = sizeof(cmd.v7);
1459b0aa02b3SAyala Barazani 	else if (cmd_ver == 6)
1460fbb7957dSLuca Coelho 		len = sizeof(cmd.v6);
1461fbb7957dSLuca Coelho 	else if (fw_has_api(&mvm->fw->ucode_capa,
14620791c2fcSHaim Dreyfuss 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
14630791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
14640791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
14650791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
14660791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
14670791c2fcSHaim Dreyfuss 	else
1468216cdfb5SLuca Coelho 		len = sizeof(cmd.v3);
1469216cdfb5SLuca Coelho 
1470216cdfb5SLuca Coelho 	/* all structs have the same common part, add it */
1471216cdfb5SLuca Coelho 	len += sizeof(cmd.common);
1472e705c121SKalle Valo 
1473971cbe50SJohannes Berg 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1474e705c121SKalle Valo }
1475e705c121SKalle Valo 
1476bbd6d0f8SJohannes Berg static void iwl_mvm_post_csa_tx(void *data, struct ieee80211_sta *sta)
1477bbd6d0f8SJohannes Berg {
1478bbd6d0f8SJohannes Berg 	struct ieee80211_hw *hw = data;
1479bbd6d0f8SJohannes Berg 	int i;
1480bbd6d0f8SJohannes Berg 
1481bbd6d0f8SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1482bbd6d0f8SJohannes Berg 		struct iwl_mvm_txq *mvmtxq =
1483bbd6d0f8SJohannes Berg 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1484bbd6d0f8SJohannes Berg 
1485bbd6d0f8SJohannes Berg 		clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1486bbd6d0f8SJohannes Berg 		iwl_mvm_mac_itxq_xmit(hw, sta->txq[i]);
1487bbd6d0f8SJohannes Berg 	}
1488bbd6d0f8SJohannes Berg }
1489bbd6d0f8SJohannes Berg 
149003117f30SMiri Korenblit int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1491a469a593SEmmanuel Grumbach 				struct ieee80211_vif *vif,
1492a469a593SEmmanuel Grumbach 				struct ieee80211_bss_conf *link_conf)
1493f6780614SSara Sharon {
1494f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1495f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1496f6780614SSara Sharon 	int ret;
1497f6780614SSara Sharon 
1498f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1499f6780614SSara Sharon 
1500f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1501f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
15023723c7c5SEmmanuel Grumbach 		unsigned int link_id = link_conf->link_id;
15033723c7c5SEmmanuel Grumbach 		u8 ap_sta_id = mvmvif->link[link_id]->ap_sta_id;
1504f6780614SSara Sharon 
1505f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
15062964b57fSJohannes Berg 		mvmvif->csa_blocks_tx = false;
15073723c7c5SEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm, ap_sta_id);
1508f6780614SSara Sharon 
1509f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1510f6780614SSara Sharon 			ret = -EIO;
1511f6780614SSara Sharon 			goto out_unlock;
1512f6780614SSara Sharon 		}
1513f6780614SSara Sharon 
1514f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
151503117f30SMiri Korenblit 		if (mvm->mld_api_is_used)
151603117f30SMiri Korenblit 			iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
151703117f30SMiri Korenblit 		else
1518f6780614SSara Sharon 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1519f6780614SSara Sharon 
15200202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
15210202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
152287f5b5f2SJohannes Berg 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
1523f6780614SSara Sharon 			if (ret)
1524f6780614SSara Sharon 				goto out_unlock;
1525f6780614SSara Sharon 
1526f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1527f6780614SSara Sharon 		}
1528bbd6d0f8SJohannes Berg 	} else if (vif->type == NL80211_IFTYPE_AP && mvmvif->csa_blocks_tx) {
1529bbd6d0f8SJohannes Berg 		struct iwl_mvm_txq *mvmtxq =
1530bbd6d0f8SJohannes Berg 			iwl_mvm_txq_from_mac80211(vif->txq);
1531bbd6d0f8SJohannes Berg 
1532bbd6d0f8SJohannes Berg 		clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1533bbd6d0f8SJohannes Berg 
1534bbd6d0f8SJohannes Berg 		local_bh_disable();
1535bbd6d0f8SJohannes Berg 		iwl_mvm_mac_itxq_xmit(hw, vif->txq);
1536bbd6d0f8SJohannes Berg 		ieee80211_iterate_stations_atomic(hw, iwl_mvm_post_csa_tx, hw);
1537bbd6d0f8SJohannes Berg 		local_bh_enable();
1538bbd6d0f8SJohannes Berg 
1539bbd6d0f8SJohannes Berg 		mvmvif->csa_blocks_tx = false;
15400202bcf0SEmmanuel Grumbach 	}
1541f6780614SSara Sharon 
1542f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1543f6780614SSara Sharon 
1544f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1545f6780614SSara Sharon 
1546f6780614SSara Sharon out_unlock:
1547caf46377SSara Sharon 	if (mvmvif->csa_failed)
1548caf46377SSara Sharon 		ret = -EIO;
1549f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1550f6780614SSara Sharon 
1551f6780614SSara Sharon 	return ret;
1552f6780614SSara Sharon }
1553f6780614SSara Sharon 
1554cbce62a3SMiri Korenblit void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
15555ecd5d82SJohannes Berg 				  struct ieee80211_vif *vif,
15565ecd5d82SJohannes Berg 				  struct ieee80211_bss_conf *link_conf)
1557f6780614SSara Sharon {
1558f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1559f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1560f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1561f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1562f6780614SSara Sharon 							  mvmvif->color)),
1563f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1564f6780614SSara Sharon 	};
1565f6780614SSara Sharon 
1566ad12b231SNathan Errera 	/*
1567ad12b231SNathan Errera 	 * In the new flow since FW is in charge of the timing,
1568ad12b231SNathan Errera 	 * if driver has canceled the channel switch he will receive the
1569ad12b231SNathan Errera 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1570ad12b231SNathan Errera 	 */
1571ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1572ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1573ad12b231SNathan Errera 		return;
1574ad12b231SNathan Errera 
1575f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1576f6780614SSara Sharon 
1577f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
157858ddd9b6SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
157958ddd9b6SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
158058ddd9b6SEmmanuel Grumbach 		iwl_mvm_remove_csa_period(mvm, vif);
158158ddd9b6SEmmanuel Grumbach 	else
1582f6780614SSara Sharon 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1583f6780614SSara Sharon 					     WIDE_ID(MAC_CONF_GROUP,
1584f6780614SSara Sharon 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1585f6780614SSara Sharon 					     0, sizeof(cmd), &cmd));
1586caf46377SSara Sharon 	mvmvif->csa_failed = true;
1587f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1588f6780614SSara Sharon 
1589a469a593SEmmanuel Grumbach 	/* If we're here, we can't support MLD */
1590a469a593SEmmanuel Grumbach 	iwl_mvm_post_channel_switch(hw, vif, &vif->bss_conf);
1591f6780614SSara Sharon }
1592f6780614SSara Sharon 
15931ab26632SMiri Korenblit void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1594f6780614SSara Sharon {
1595f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1596f6780614SSara Sharon 	struct ieee80211_vif *vif;
1597f6780614SSara Sharon 
1598f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1599f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1600f6780614SSara Sharon 
160170162580SShaul Triebitz 	/* Trigger disconnect (should clear the CSA state) */
1602a469a593SEmmanuel Grumbach 	ieee80211_chswitch_done(vif, false, 0);
1603f6780614SSara Sharon }
1604f6780614SSara Sharon 
16055abf3154SMordechay Goodstein static u8
16065abf3154SMordechay Goodstein iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
16075abf3154SMordechay Goodstein {
16085abf3154SMordechay Goodstein 	int data_start;
16095abf3154SMordechay Goodstein 	int control_start;
16105abf3154SMordechay Goodstein 	int bw;
16115abf3154SMordechay Goodstein 
16125abf3154SMordechay Goodstein 	if (chandef->width == NL80211_CHAN_WIDTH_320)
16135abf3154SMordechay Goodstein 		bw = 320;
16145abf3154SMordechay Goodstein 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
16155abf3154SMordechay Goodstein 		bw = 160;
16165abf3154SMordechay Goodstein 	else
16175abf3154SMordechay Goodstein 		return 0;
16185abf3154SMordechay Goodstein 
16195abf3154SMordechay Goodstein 	/* data is bw wide so the start is half the width */
16205abf3154SMordechay Goodstein 	data_start = chandef->center_freq1 - bw / 2;
16215abf3154SMordechay Goodstein 	/* control is 20Mhz width */
16225abf3154SMordechay Goodstein 	control_start = chandef->chan->center_freq - 10;
16235abf3154SMordechay Goodstein 
16245abf3154SMordechay Goodstein 	return (control_start - data_start) / 80;
16255abf3154SMordechay Goodstein }
16265abf3154SMordechay Goodstein 
16271be4858eSJohannes Berg static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
16281be4858eSJohannes Berg 					 struct ieee80211_vif *vif)
1629e705c121SKalle Valo {
1630e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
16311be4858eSJohannes Berg 	int ret;
16321ab26632SMiri Korenblit 
16331ab26632SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
1634e705c121SKalle Valo 
16351be4858eSJohannes Berg 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
16361be4858eSJohannes Berg 	if (ret) {
16371be4858eSJohannes Berg 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
16381be4858eSJohannes Berg 		return ret;
16391be4858eSJohannes Berg 	}
16401be4858eSJohannes Berg 
16411be4858eSJohannes Berg 	/* Only queue for this station is the mcast queue,
16421be4858eSJohannes Berg 	 * which shouldn't be in TFD mask anyway
16431be4858eSJohannes Berg 	 */
16441be4858eSJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
16451be4858eSJohannes Berg 					vif->type,
16461be4858eSJohannes Berg 					IWL_STA_MULTICAST);
16471be4858eSJohannes Berg }
16481be4858eSJohannes Berg 
1649492bc4e4SMiri Korenblit static void iwl_mvm_prevent_esr_done_wk(struct wiphy *wiphy,
1650492bc4e4SMiri Korenblit 					struct wiphy_work *wk)
1651492bc4e4SMiri Korenblit {
1652492bc4e4SMiri Korenblit 	struct iwl_mvm_vif *mvmvif =
1653492bc4e4SMiri Korenblit 		container_of(wk, struct iwl_mvm_vif, prevent_esr_done_wk.work);
1654492bc4e4SMiri Korenblit 	struct iwl_mvm *mvm = mvmvif->mvm;
1655492bc4e4SMiri Korenblit 	struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
1656492bc4e4SMiri Korenblit 
16573f312651SJohannes Berg 	guard(mvm)(mvm);
1658492bc4e4SMiri Korenblit 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_PREVENTION);
1659492bc4e4SMiri Korenblit }
1660492bc4e4SMiri Korenblit 
16612f33561eSMiri Korenblit static void iwl_mvm_mlo_int_scan_wk(struct wiphy *wiphy, struct wiphy_work *wk)
16622f33561eSMiri Korenblit {
16632f33561eSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = container_of(wk, struct iwl_mvm_vif,
16642f33561eSMiri Korenblit 						  mlo_int_scan_wk.work);
16652f33561eSMiri Korenblit 	struct ieee80211_vif *vif =
16662f33561eSMiri Korenblit 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
16672f33561eSMiri Korenblit 
16683f312651SJohannes Berg 	guard(mvm)(mvmvif->mvm);
16692f33561eSMiri Korenblit 	iwl_mvm_int_mlo_scan(mvmvif->mvm, vif);
16702f33561eSMiri Korenblit }
16712f33561eSMiri Korenblit 
1672ec0d43d2SMiri Korenblit static void iwl_mvm_unblock_esr_tpt(struct wiphy *wiphy, struct wiphy_work *wk)
1673ec0d43d2SMiri Korenblit {
1674ec0d43d2SMiri Korenblit 	struct iwl_mvm_vif *mvmvif =
1675ec0d43d2SMiri Korenblit 		container_of(wk, struct iwl_mvm_vif, unblock_esr_tpt_wk);
1676ec0d43d2SMiri Korenblit 	struct iwl_mvm *mvm = mvmvif->mvm;
1677ec0d43d2SMiri Korenblit 	struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
1678ec0d43d2SMiri Korenblit 
16793f312651SJohannes Berg 	guard(mvm)(mvm);
1680ec0d43d2SMiri Korenblit 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT);
1681ec0d43d2SMiri Korenblit }
1682ec0d43d2SMiri Korenblit 
16830bcc2155SJohannes Berg void iwl_mvm_mac_init_mvmvif(struct iwl_mvm *mvm, struct iwl_mvm_vif *mvmvif)
16840bcc2155SJohannes Berg {
16850bcc2155SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
16860bcc2155SJohannes Berg 
16870bcc2155SJohannes Berg 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
16880bcc2155SJohannes Berg 		return;
16890bcc2155SJohannes Berg 
16900bcc2155SJohannes Berg 	INIT_DELAYED_WORK(&mvmvif->csa_work,
16910bcc2155SJohannes Berg 			  iwl_mvm_channel_switch_disconnect_wk);
1692492bc4e4SMiri Korenblit 
1693492bc4e4SMiri Korenblit 	wiphy_delayed_work_init(&mvmvif->prevent_esr_done_wk,
1694492bc4e4SMiri Korenblit 				iwl_mvm_prevent_esr_done_wk);
16952f33561eSMiri Korenblit 
16962f33561eSMiri Korenblit 	wiphy_delayed_work_init(&mvmvif->mlo_int_scan_wk,
16972f33561eSMiri Korenblit 				iwl_mvm_mlo_int_scan_wk);
1698ec0d43d2SMiri Korenblit 
1699ec0d43d2SMiri Korenblit 	wiphy_work_init(&mvmvif->unblock_esr_tpt_wk,
1700ec0d43d2SMiri Korenblit 			iwl_mvm_unblock_esr_tpt);
17010bcc2155SJohannes Berg }
17020bcc2155SJohannes Berg 
17031be4858eSJohannes Berg static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
17041be4858eSJohannes Berg 				     struct ieee80211_vif *vif)
17051be4858eSJohannes Berg {
17061be4858eSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
17071be4858eSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
17081be4858eSJohannes Berg 	int ret;
1709b6e3d1baSAnjaneyulu 	int i;
17101be4858eSJohannes Berg 
17111be4858eSJohannes Berg 	mutex_lock(&mvm->mutex);
17121be4858eSJohannes Berg 
17130bcc2155SJohannes Berg 	iwl_mvm_mac_init_mvmvif(mvm, mvmvif);
17140bcc2155SJohannes Berg 
1715e705c121SKalle Valo 	mvmvif->mvm = mvm;
1716650cadb7SGregory Greenman 
1717650cadb7SGregory Greenman 	/* the first link always points to the default one */
1718a8b5d480SIlan Peer 	mvmvif->deflink.fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1719a8b5d480SIlan Peer 	mvmvif->deflink.active = 0;
1720650cadb7SGregory Greenman 	mvmvif->link[0] = &mvmvif->deflink;
1721e705c121SKalle Valo 
1722a8b5d480SIlan Peer 	ret = iwl_mvm_set_link_mapping(mvm, vif, &vif->bss_conf);
1723a8b5d480SIlan Peer 	if (ret)
1724a8b5d480SIlan Peer 		goto out;
1725a8b5d480SIlan Peer 
1726e705c121SKalle Valo 	/*
1727e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1728e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1729e705c121SKalle Valo 	 * don't really have to check the types.
1730e705c121SKalle Valo 	 */
1731e705c121SKalle Valo 
1732e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1733e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1734b6e3d1baSAnjaneyulu 		for_each_mvm_vif_valid_link(mvmvif, i)
1735b6e3d1baSAnjaneyulu 			mvmvif->link[i]->beacon_stats.accu_num_beacons +=
1736b6e3d1baSAnjaneyulu 				mvmvif->link[i]->beacon_stats.num_beacons;
1737e705c121SKalle Valo 
1738e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
17391be4858eSJohannes Berg 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
17401be4858eSJohannes Berg 	if (ret)
17411be4858eSJohannes Berg 		goto out;
1742e705c121SKalle Valo 
1743698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1744698478c4SSara Sharon 
17451be4858eSJohannes Berg 	/* Currently not much to do for NAN */
17461be4858eSJohannes Berg 	if (vif->type == NL80211_IFTYPE_NAN) {
17471be4858eSJohannes Berg 		ret = 0;
17481be4858eSJohannes Berg 		goto out;
17491be4858eSJohannes Berg 	}
17501be4858eSJohannes Berg 
1751e705c121SKalle Valo 	/*
1752e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1753e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1754e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1755e705c121SKalle Valo 	 * only after the binding.
1756e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1757e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1758e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1759e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1760e705c121SKalle Valo 	 * allocating resources :)
1761e705c121SKalle Valo 	 */
1762e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1763e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
17643a3ef394SBenjamin Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1765c36235acSJohannes Berg 			iwl_mvm_vif_dbgfs_add_link(mvm, vif);
17661be4858eSJohannes Berg 		ret = 0;
17671be4858eSJohannes Berg 		goto out;
17681ab26632SMiri Korenblit 	}
17691ab26632SMiri Korenblit 
17701ab26632SMiri Korenblit 	mvmvif->features |= hw->netdev_features;
1771650cadb7SGregory Greenman 
1772e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1773e705c121SKalle Valo 	if (ret)
177498c0de7bSMiri Korenblit 		goto out_unlock;
1775e705c121SKalle Valo 
1776e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1777e705c121SKalle Valo 	if (ret)
1778e705c121SKalle Valo 		goto out_remove_mac;
1779e705c121SKalle Valo 
1780e705c121SKalle Valo 	/* beacon filtering */
178187f5b5f2SJohannes Berg 	ret = iwl_mvm_disable_beacon_filter(mvm, vif);
1782e705c121SKalle Valo 	if (ret)
1783e705c121SKalle Valo 		goto out_remove_mac;
1784e705c121SKalle Valo 
1785e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1786e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1787e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1788e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1789e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1790e705c121SKalle Valo 	}
1791e705c121SKalle Valo 
179284ef7cbeSIlan Peer 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1793e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1794e705c121SKalle Valo 
1795b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1796b0ffe455SJohannes Berg 
17975abf3154SMordechay Goodstein 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1798baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
17995abf3154SMordechay Goodstein 		mvm->monitor_p80 =
18006092077aSJohannes Berg 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chanreq.oper);
18015abf3154SMordechay Goodstein 	}
1802baf41bc3SShaul Triebitz 
18033a3ef394SBenjamin Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1804c36235acSJohannes Berg 		iwl_mvm_vif_dbgfs_add_link(mvm, vif);
18056d19a5ebSEmmanuel Grumbach 
18066d19a5ebSEmmanuel Grumbach 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
18076d19a5ebSEmmanuel Grumbach 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
18086d19a5ebSEmmanuel Grumbach 	    !mvm->csme_vif && mvm->mei_registered) {
18096d19a5ebSEmmanuel Grumbach 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
18106d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
18116d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = vif;
18126d19a5ebSEmmanuel Grumbach 	}
18136d19a5ebSEmmanuel Grumbach 
18141ab26632SMiri Korenblit out:
18151ab26632SMiri Korenblit 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
18161ab26632SMiri Korenblit 		     vif->type == NL80211_IFTYPE_ADHOC))
18171ab26632SMiri Korenblit 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
18181ab26632SMiri Korenblit 
1819e705c121SKalle Valo 	goto out_unlock;
1820e705c121SKalle Valo 
1821e705c121SKalle Valo  out_remove_mac:
1822650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
1823e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1824e705c121SKalle Valo  out_unlock:
1825e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1826e705c121SKalle Valo 
1827e705c121SKalle Valo 	return ret;
1828e705c121SKalle Valo }
1829e705c121SKalle Valo 
1830cb145863SJohannes Berg void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1831e705c121SKalle Valo 				 struct ieee80211_vif *vif)
1832e705c121SKalle Valo {
18330bcc2155SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
18340bcc2155SJohannes Berg 
1835e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1836e705c121SKalle Valo 		/*
1837e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1838e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1839e705c121SKalle Valo 		 * queue are sent in ROC session.
1840e705c121SKalle Valo 		 */
1841e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1842e705c121SKalle Valo 	}
18430bcc2155SJohannes Berg 
1844492bc4e4SMiri Korenblit 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1845492bc4e4SMiri Korenblit 				  &mvmvif->prevent_esr_done_wk);
1846492bc4e4SMiri Korenblit 
18472f33561eSMiri Korenblit 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
18482f33561eSMiri Korenblit 				  &mvmvif->mlo_int_scan_wk);
18492f33561eSMiri Korenblit 
1850ec0d43d2SMiri Korenblit 	wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
1851ec0d43d2SMiri Korenblit 
18520bcc2155SJohannes Berg 	cancel_delayed_work_sync(&mvmvif->csa_work);
1853e705c121SKalle Valo }
1854e705c121SKalle Valo 
185560efeca1SMiri Korenblit /* This function is doing the common part of removing the interface for
185660efeca1SMiri Korenblit  * both - MLD and non-MLD modes. Returns true if removing the interface
185760efeca1SMiri Korenblit  * is done
185860efeca1SMiri Korenblit  */
1859cb145863SJohannes Berg static bool iwl_mvm_mac_remove_interface_common(struct ieee80211_hw *hw,
1860e705c121SKalle Valo 						struct ieee80211_vif *vif)
1861e705c121SKalle Valo {
1862e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1863e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
186486e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1865e705c121SKalle Valo 
1866e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1867e705c121SKalle Valo 
1868b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1869b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1870b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1871b0ffe455SJohannes Berg 
1872e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1873e705c121SKalle Valo 
18746d19a5ebSEmmanuel Grumbach 	if (vif == mvm->csme_vif) {
18756d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(NULL);
18766d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = NULL;
18776d19a5ebSEmmanuel Grumbach 	}
18786d19a5ebSEmmanuel Grumbach 
1879650cadb7SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
188086e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
1881650cadb7SGregory Greenman 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
188286e177d8SGregory Greenman 	if (probe_data)
188386e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
188486e177d8SGregory Greenman 
1885e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1886e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1887e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1888e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1889e705c121SKalle Valo 	}
1890e705c121SKalle Valo 
1891b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1892b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1893b73f9a4aSJohannes Berg 
1894c36235acSJohannes Berg 	iwl_mvm_vif_dbgfs_rm_link(mvm, vif);
1895e705c121SKalle Valo 
1896e705c121SKalle Valo 	/*
1897e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1898e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1899e705c121SKalle Valo 	 */
1900e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1901e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1902e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1903e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1904e705c121SKalle Valo 			mvm->noa_vif = NULL;
1905e705c121SKalle Valo 			mvm->noa_duration = 0;
1906e705c121SKalle Valo 		}
1907e705c121SKalle Valo #endif
190860efeca1SMiri Korenblit 		return true;
1909e705c121SKalle Valo 	}
1910e705c121SKalle Valo 
191160efeca1SMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
191260efeca1SMiri Korenblit 	return false;
191360efeca1SMiri Korenblit }
191460efeca1SMiri Korenblit 
191560efeca1SMiri Korenblit static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
191660efeca1SMiri Korenblit 					 struct ieee80211_vif *vif)
191760efeca1SMiri Korenblit {
191860efeca1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
191960efeca1SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
192060efeca1SMiri Korenblit 
192160efeca1SMiri Korenblit 	if (iwl_mvm_mac_remove_interface_common(hw, vif))
192260efeca1SMiri Korenblit 		goto out;
192360efeca1SMiri Korenblit 
192484ef7cbeSIlan Peer 	/* Before the interface removal, mac80211 would cancel the ROC, and the
192584ef7cbeSIlan Peer 	 * ROC worker would be scheduled if needed. The worker would be flushed
192684ef7cbeSIlan Peer 	 * in iwl_mvm_prepare_mac_removal() and thus at this point there is no
192784ef7cbeSIlan Peer 	 * binding etc. so nothing needs to be done here.
192884ef7cbeSIlan Peer 	 */
1929e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
193084ef7cbeSIlan Peer 		if (mvmvif->deflink.phy_ctxt) {
1931650cadb7SGregory Greenman 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1932650cadb7SGregory Greenman 			mvmvif->deflink.phy_ctxt = NULL;
1933e705c121SKalle Valo 		}
193484ef7cbeSIlan Peer 		mvm->p2p_device_vif = NULL;
193584ef7cbeSIlan Peer 	}
1936e705c121SKalle Valo 
1937a8b5d480SIlan Peer 	iwl_mvm_unset_link_mapping(mvm, vif, &vif->bss_conf);
1938e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1939e705c121SKalle Valo 
1940698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1941698478c4SSara Sharon 
1942baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1943baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1944baf41bc3SShaul Triebitz 
194560efeca1SMiri Korenblit out:
194660efeca1SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP ||
194760efeca1SMiri Korenblit 	    vif->type == NL80211_IFTYPE_ADHOC) {
1948650cadb7SGregory Greenman 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
194960efeca1SMiri Korenblit 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1950e705c121SKalle Valo 	}
1951e705c121SKalle Valo 
1952e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1953e705c121SKalle Valo }
1954e705c121SKalle Valo 
1955e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1956e705c121SKalle Valo 	struct iwl_mvm *mvm;
1957e705c121SKalle Valo 	int port_id;
1958e705c121SKalle Valo };
1959e705c121SKalle Valo 
1960e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1961e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1962e705c121SKalle Valo {
1963e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1964e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1965e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
196697bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
196797bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
196897bce57bSLuca Coelho 		.flags = CMD_ASYNC,
196997bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
197097bce57bSLuca Coelho 	};
1971e705c121SKalle Valo 	int ret, len;
1972e705c121SKalle Valo 
1973e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1974e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1975e705c121SKalle Valo 		return;
1976e705c121SKalle Valo 
1977e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1978f276e20bSJohannes Berg 	    !vif->cfg.assoc)
1979e705c121SKalle Valo 		return;
1980e705c121SKalle Valo 
1981e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1982e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1983e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1984e705c121SKalle Valo 
198597bce57bSLuca Coelho 	hcmd.len[0] = len;
198697bce57bSLuca Coelho 	hcmd.data[0] = cmd;
198797bce57bSLuca Coelho 
198897bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1989e705c121SKalle Valo 	if (ret)
1990e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1991e705c121SKalle Valo }
1992e705c121SKalle Valo 
1993e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1994e705c121SKalle Valo {
1995e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1996e705c121SKalle Valo 		.mvm = mvm,
1997e705c121SKalle Valo 	};
1998db66abeeSJohannes Berg 	int ret;
1999e705c121SKalle Valo 
2000e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2001e705c121SKalle Valo 
2002e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
2003e705c121SKalle Valo 		return;
2004e705c121SKalle Valo 
2005e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
2006e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2007e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
2008db66abeeSJohannes Berg 
2009db66abeeSJohannes Berg 	/*
2010db66abeeSJohannes Berg 	 * Send a (synchronous) ech command so that we wait for the
2011db66abeeSJohannes Berg 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
2012db66abeeSJohannes Berg 	 * the interface iterator. Otherwise, we might get here over
2013db66abeeSJohannes Berg 	 * and over again (by userspace just sending a lot of these)
2014db66abeeSJohannes Berg 	 * and the CPU can send them faster than the firmware can
2015db66abeeSJohannes Berg 	 * process them.
2016db66abeeSJohannes Berg 	 * Note that the CPU is still faster - but with this we'll
2017db66abeeSJohannes Berg 	 * actually send fewer commands overall because the CPU will
2018db66abeeSJohannes Berg 	 * not schedule the work in mac80211 as frequently if it's
2019db66abeeSJohannes Berg 	 * still running when rescheduled (possibly multiple times).
2020db66abeeSJohannes Berg 	 */
2021db66abeeSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
2022db66abeeSJohannes Berg 	if (ret)
2023db66abeeSJohannes Berg 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
2024e705c121SKalle Valo }
2025e705c121SKalle Valo 
2026cbce62a3SMiri Korenblit u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
2027e705c121SKalle Valo 			      struct netdev_hw_addr_list *mc_list)
2028e705c121SKalle Valo {
2029e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2030e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
2031e705c121SKalle Valo 	struct netdev_hw_addr *addr;
2032e705c121SKalle Valo 	int addr_count;
2033e705c121SKalle Valo 	bool pass_all;
2034e705c121SKalle Valo 	int len;
2035e705c121SKalle Valo 
2036e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
2037e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
2038e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
2039e705c121SKalle Valo 	if (pass_all)
2040e705c121SKalle Valo 		addr_count = 0;
2041e705c121SKalle Valo 
2042e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
2043e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
2044e705c121SKalle Valo 	if (!cmd)
2045e705c121SKalle Valo 		return 0;
2046e705c121SKalle Valo 
2047e705c121SKalle Valo 	if (pass_all) {
2048e705c121SKalle Valo 		cmd->pass_all = 1;
2049e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
2050e705c121SKalle Valo 	}
2051e705c121SKalle Valo 
2052e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
2053e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
2054e705c121SKalle Valo 				   cmd->count, addr->addr);
2055e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
2056e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
2057e705c121SKalle Valo 		cmd->count++;
2058e705c121SKalle Valo 	}
2059e705c121SKalle Valo 
2060e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
2061e705c121SKalle Valo }
2062e705c121SKalle Valo 
2063cbce62a3SMiri Korenblit void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
2064e705c121SKalle Valo 			      unsigned int changed_flags,
2065cbce62a3SMiri Korenblit 			      unsigned int *total_flags, u64 multicast)
2066e705c121SKalle Valo {
2067e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2068e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
2069e705c121SKalle Valo 
20703f312651SJohannes Berg 	guard(mvm)(mvm);
2071e705c121SKalle Valo 
2072e705c121SKalle Valo 	/* replace previous configuration */
2073e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
2074e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
2075e705c121SKalle Valo 
2076e705c121SKalle Valo 	if (!cmd)
2077e705c121SKalle Valo 		goto out;
2078e705c121SKalle Valo 
207961e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
208061e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
208161e7d91bSLuca Coelho 
208261e7d91bSLuca Coelho 	if (cmd->pass_all)
208361e7d91bSLuca Coelho 		cmd->count = 0;
208461e7d91bSLuca Coelho 
2085e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
2086e705c121SKalle Valo out:
2087e705c121SKalle Valo 	*total_flags = 0;
2088e705c121SKalle Valo }
2089e705c121SKalle Valo 
2090e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
2091e705c121SKalle Valo 					struct ieee80211_vif *vif,
2092e705c121SKalle Valo 					unsigned int filter_flags,
2093e705c121SKalle Valo 					unsigned int changed_flags)
2094e705c121SKalle Valo {
2095e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2096e705c121SKalle Valo 
2097e705c121SKalle Valo 	/* We support only filter for probe requests */
2098e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
2099e705c121SKalle Valo 		return;
2100e705c121SKalle Valo 
2101e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
2102f276e20bSJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
2103e705c121SKalle Valo 	    !vif->p2p)
2104e705c121SKalle Valo 		return;
2105e705c121SKalle Valo 
21063f312651SJohannes Berg 	guard(mvm)(mvm);
2107e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2108e705c121SKalle Valo }
2109e705c121SKalle Valo 
211022c58834SGregory Greenman int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2111a07a8f37SSara Sharon {
2112a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
2113a07a8f37SSara Sharon 
2114a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
2115a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
2116a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
2117a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
2118a07a8f37SSara Sharon 
2119a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
2120a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
2121a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
2122a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
2123a07a8f37SSara Sharon }
2124a07a8f37SSara Sharon 
2125f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
2126f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
2127f92659a1SSara Sharon {
2128d0a9123eSJohannes Berg 	if (vif->bss_conf.mu_mimo_owner) {
2129f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
2130f92659a1SSara Sharon 
2131f92659a1SSara Sharon 		/*
2132f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
2133f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
2134f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
2135f92659a1SSara Sharon 		 */
2136afe0d181SJohannes Berg 		ieee80211_update_mu_groups(vif, 0,
2137f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
2138f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
2139f92659a1SSara Sharon 	}
2140f92659a1SSara Sharon }
2141f92659a1SSara Sharon 
2142f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
2143f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
2144f92659a1SSara Sharon {
2145f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2146f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
2147f92659a1SSara Sharon 
2148f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
2149f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2150f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
2151f92659a1SSara Sharon }
2152f92659a1SSara Sharon 
2153514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2154514c3069SLuca Coelho {
2155514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
2156514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
2157514c3069SLuca Coelho 	u8 residue_bits;
2158514c3069SLuca Coelho 	u8 res;
2159514c3069SLuca Coelho 
2160514c3069SLuca Coelho 	if (bit_num <= 5)
2161514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
2162514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2163514c3069SLuca Coelho 
2164514c3069SLuca Coelho 	/*
2165514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
2166514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
2167514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
2168514c3069SLuca Coelho 	 */
2169514c3069SLuca Coelho 
2170514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
2171514c3069SLuca Coelho 
2172514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
2173514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2174514c3069SLuca Coelho 	      residue_bits;
2175514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2176514c3069SLuca Coelho 
2177514c3069SLuca Coelho 	return res;
2178514c3069SLuca Coelho }
2179514c3069SLuca Coelho 
2180091296d3SMiri Korenblit static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
2181091296d3SMiri Korenblit 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
2182cb63eb43SMiri Korenblit 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
2183cb63eb43SMiri Korenblit 			      bool inheritance)
2184091296d3SMiri Korenblit {
2185091296d3SMiri Korenblit 	int i;
2186091296d3SMiri Korenblit 
2187091296d3SMiri Korenblit 	/*
2188091296d3SMiri Korenblit 	* FW currently supports only nss == MAX_HE_SUPP_NSS
2189091296d3SMiri Korenblit 	*
2190091296d3SMiri Korenblit 	* If nss > MAX: we can ignore values we don't support
2191091296d3SMiri Korenblit 	* If nss < MAX: we can set zeros in other streams
2192091296d3SMiri Korenblit 	*/
2193091296d3SMiri Korenblit 	if (nss > MAX_HE_SUPP_NSS) {
21944d8421f2SJason A. Donenfeld 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2195091296d3SMiri Korenblit 			       MAX_HE_SUPP_NSS);
2196091296d3SMiri Korenblit 		nss = MAX_HE_SUPP_NSS;
2197091296d3SMiri Korenblit 	}
2198091296d3SMiri Korenblit 
2199091296d3SMiri Korenblit 	for (i = 0; i < nss; i++) {
2200091296d3SMiri Korenblit 		u8 ru_index_tmp = ru_index_bitmap << 1;
2201091296d3SMiri Korenblit 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2202091296d3SMiri Korenblit 		u8 bw;
2203091296d3SMiri Korenblit 
2204091296d3SMiri Korenblit 		for (bw = 0;
2205091296d3SMiri Korenblit 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2206091296d3SMiri Korenblit 		     bw++) {
2207091296d3SMiri Korenblit 			ru_index_tmp >>= 1;
2208091296d3SMiri Korenblit 
2209cb63eb43SMiri Korenblit 			/*
2210cb63eb43SMiri Korenblit 			* According to the 11be spec, if for a specific BW the PPE Thresholds
2211cb63eb43SMiri Korenblit 			* isn't present - it should inherit the thresholds from the last
2212cb63eb43SMiri Korenblit 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2213cb63eb43SMiri Korenblit 			* this inheritance - continue in this case
2214cb63eb43SMiri Korenblit 			*/
2215cb63eb43SMiri Korenblit 			if (!(ru_index_tmp & 1)) {
2216cb63eb43SMiri Korenblit 				if (inheritance)
2217cb63eb43SMiri Korenblit 					goto set_thresholds;
2218cb63eb43SMiri Korenblit 				else
2219091296d3SMiri Korenblit 					continue;
2220cb63eb43SMiri Korenblit 			}
2221091296d3SMiri Korenblit 
2222091296d3SMiri Korenblit 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2223091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2224091296d3SMiri Korenblit 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2225091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2226091296d3SMiri Korenblit 
2227cb63eb43SMiri Korenblit set_thresholds:
2228091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2229091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2230091296d3SMiri Korenblit 		}
2231091296d3SMiri Korenblit 	}
2232091296d3SMiri Korenblit }
2233091296d3SMiri Korenblit 
2234091296d3SMiri Korenblit static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
223557974a55SGregory Greenman 					    struct ieee80211_link_sta *link_sta,
2236cb63eb43SMiri Korenblit 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2237cb63eb43SMiri Korenblit 					    bool inheritance)
2238091296d3SMiri Korenblit {
223957974a55SGregory Greenman 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
224057974a55SGregory Greenman 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
224157974a55SGregory Greenman 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2242091296d3SMiri Korenblit 	u8 ru_index_bitmap =
2243091296d3SMiri Korenblit 		u8_get_bits(*ppe,
2244091296d3SMiri Korenblit 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2245091296d3SMiri Korenblit 	/* Starting after PPE header */
2246091296d3SMiri Korenblit 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2247091296d3SMiri Korenblit 
2248cb63eb43SMiri Korenblit 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2249cb63eb43SMiri Korenblit 			  inheritance);
2250091296d3SMiri Korenblit }
2251091296d3SMiri Korenblit 
22524df6a075SMiri Korenblit static int
22534df6a075SMiri Korenblit iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
22544df6a075SMiri Korenblit 					 u8 nominal_padding)
2255091296d3SMiri Korenblit {
2256091296d3SMiri Korenblit 	int low_th = -1;
2257091296d3SMiri Korenblit 	int high_th = -1;
2258091296d3SMiri Korenblit 	int i;
2259091296d3SMiri Korenblit 
2260cb63eb43SMiri Korenblit 	/* all the macros are the same for EHT and HE */
2261091296d3SMiri Korenblit 	switch (nominal_padding) {
2262cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2263091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2264091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2265091296d3SMiri Korenblit 		break;
2266cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2267091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_BPSK;
2268091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2269091296d3SMiri Korenblit 		break;
2270cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2271cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2272091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2273091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_BPSK;
2274091296d3SMiri Korenblit 		break;
2275091296d3SMiri Korenblit 	}
2276091296d3SMiri Korenblit 
22774df6a075SMiri Korenblit 	if (low_th < 0 || high_th < 0)
22784df6a075SMiri Korenblit 		return -EINVAL;
22794df6a075SMiri Korenblit 
2280091296d3SMiri Korenblit 	/* Set the PPE thresholds accordingly */
2281091296d3SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2282091296d3SMiri Korenblit 		u8 bw;
2283091296d3SMiri Korenblit 
2284091296d3SMiri Korenblit 		for (bw = 0;
2285091296d3SMiri Korenblit 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2286091296d3SMiri Korenblit 			bw++) {
2287091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2288091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2289091296d3SMiri Korenblit 		}
2290091296d3SMiri Korenblit 	}
2291091296d3SMiri Korenblit 
22924df6a075SMiri Korenblit 	return 0;
2293091296d3SMiri Korenblit }
2294091296d3SMiri Korenblit 
2295cb63eb43SMiri Korenblit static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2296cb63eb43SMiri Korenblit 					 u8 nominal_padding)
2297cb63eb43SMiri Korenblit {
2298cb63eb43SMiri Korenblit 	int i;
2299cb63eb43SMiri Korenblit 
2300cb63eb43SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2301cb63eb43SMiri Korenblit 		u8 bw;
2302cb63eb43SMiri Korenblit 
2303cb63eb43SMiri Korenblit 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2304cb63eb43SMiri Korenblit 		     bw++) {
2305cb63eb43SMiri Korenblit 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2306cb63eb43SMiri Korenblit 
2307cb63eb43SMiri Korenblit 			if (nominal_padding >
2308cb63eb43SMiri Korenblit 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2309cb63eb43SMiri Korenblit 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2310cb63eb43SMiri Korenblit 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2311cb63eb43SMiri Korenblit 			else if (nominal_padding ==
2312cb63eb43SMiri Korenblit 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2313cb63eb43SMiri Korenblit 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2314cb63eb43SMiri Korenblit 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2315cb63eb43SMiri Korenblit 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2316cb63eb43SMiri Korenblit 		}
2317cb63eb43SMiri Korenblit 	}
2318cb63eb43SMiri Korenblit }
2319cb63eb43SMiri Korenblit 
23204df6a075SMiri Korenblit /* Set the pkt_ext field according to PPE Thresholds element */
232157974a55SGregory Greenman int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
232257974a55SGregory Greenman 			    struct ieee80211_link_sta *link_sta,
23234df6a075SMiri Korenblit 			    struct iwl_he_pkt_ext_v2 *pkt_ext)
23244df6a075SMiri Korenblit {
23254df6a075SMiri Korenblit 	u8 nominal_padding;
23264df6a075SMiri Korenblit 	int i, ret = 0;
23274df6a075SMiri Korenblit 
232857974a55SGregory Greenman 	if (WARN_ON(!link_sta))
232957974a55SGregory Greenman 		return -EINVAL;
233057974a55SGregory Greenman 
23314df6a075SMiri Korenblit 	/* Initialize the PPE thresholds to "None" (7), as described in Table
23324df6a075SMiri Korenblit 	 * 9-262ac of 80211.ax/D3.0.
23334df6a075SMiri Korenblit 	 */
23344df6a075SMiri Korenblit 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
23354df6a075SMiri Korenblit 	       sizeof(struct iwl_he_pkt_ext_v2));
23364df6a075SMiri Korenblit 
233757974a55SGregory Greenman 	if (link_sta->eht_cap.has_eht) {
23384df6a075SMiri Korenblit 		nominal_padding =
233957974a55SGregory Greenman 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
23404df6a075SMiri Korenblit 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
23414df6a075SMiri Korenblit 
23424df6a075SMiri Korenblit 		/* If PPE Thresholds exists, parse them into a FW-familiar
23434df6a075SMiri Korenblit 		 * format.
23444df6a075SMiri Korenblit 		 */
234557974a55SGregory Greenman 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
23464df6a075SMiri Korenblit 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
234757974a55SGregory Greenman 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
23484df6a075SMiri Korenblit 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
234957974a55SGregory Greenman 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
23504df6a075SMiri Korenblit 			u8 ru_index_bitmap =
23514df6a075SMiri Korenblit 				u16_get_bits(*ppe,
23524df6a075SMiri Korenblit 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
23534df6a075SMiri Korenblit 			 /* Starting after PPE header */
23544df6a075SMiri Korenblit 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
23554df6a075SMiri Korenblit 
23564df6a075SMiri Korenblit 			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
23574df6a075SMiri Korenblit 					  ppe, ppe_pos_bit, true);
235857974a55SGregory Greenman 		/* EHT PPE Thresholds doesn't exist - set the API according to
235957974a55SGregory Greenman 		 * HE PPE Tresholds
23604df6a075SMiri Korenblit 		 */
236157974a55SGregory Greenman 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
23624df6a075SMiri Korenblit 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
23634df6a075SMiri Korenblit 			/* Even though HE Capabilities IE doesn't contain PPE
23644df6a075SMiri Korenblit 			 * Thresholds for BW 320Mhz, thresholds for this BW will
23654df6a075SMiri Korenblit 			 * be filled in with the same values as 160Mhz, due to
23664df6a075SMiri Korenblit 			 * the inheritance, as required.
23674df6a075SMiri Korenblit 			 */
236857974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
23694df6a075SMiri Korenblit 							true);
23704df6a075SMiri Korenblit 
23714df6a075SMiri Korenblit 			/* According to the requirements, for MCSs 12-13 the
23724df6a075SMiri Korenblit 			 * maximum value between HE PPE Threshold and Common
23734df6a075SMiri Korenblit 			 * Nominal Packet Padding needs to be taken
23744df6a075SMiri Korenblit 			 */
23754df6a075SMiri Korenblit 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
23764df6a075SMiri Korenblit 
23774df6a075SMiri Korenblit 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
23784df6a075SMiri Korenblit 		 * take the Thresholds from Common Nominal Packet Padding field
23794df6a075SMiri Korenblit 		 */
23804df6a075SMiri Korenblit 		} else {
23814df6a075SMiri Korenblit 			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
23824df6a075SMiri Korenblit 								       nominal_padding);
23834df6a075SMiri Korenblit 		}
238457974a55SGregory Greenman 	} else if (link_sta->he_cap.has_he) {
23854df6a075SMiri Korenblit 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
238657974a55SGregory Greenman 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
23874df6a075SMiri Korenblit 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
238857974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
23894df6a075SMiri Korenblit 							false);
23904df6a075SMiri Korenblit 		/* PPE Thresholds doesn't exist - set the API PPE values
23914df6a075SMiri Korenblit 		 * according to Common Nominal Packet Padding field.
23924df6a075SMiri Korenblit 		 */
23934df6a075SMiri Korenblit 		} else {
23944df6a075SMiri Korenblit 			nominal_padding =
239557974a55SGregory Greenman 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
23964df6a075SMiri Korenblit 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
23974df6a075SMiri Korenblit 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
23984df6a075SMiri Korenblit 				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
23994df6a075SMiri Korenblit 									       nominal_padding);
24004df6a075SMiri Korenblit 		}
24014df6a075SMiri Korenblit 	}
24024df6a075SMiri Korenblit 
24034df6a075SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
24044df6a075SMiri Korenblit 		int bw;
24054df6a075SMiri Korenblit 
24064df6a075SMiri Korenblit 		for (bw = 0;
24074df6a075SMiri Korenblit 		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
24084df6a075SMiri Korenblit 		     bw++) {
24094df6a075SMiri Korenblit 			u8 *qam_th =
24104df6a075SMiri Korenblit 				&pkt_ext->pkt_ext_qam_th[i][bw][0];
24114df6a075SMiri Korenblit 
24124df6a075SMiri Korenblit 			IWL_DEBUG_HT(mvm,
24134df6a075SMiri Korenblit 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
24144df6a075SMiri Korenblit 				     i, bw, qam_th[0], qam_th[1]);
24154df6a075SMiri Korenblit 		}
24164df6a075SMiri Korenblit 	}
24174df6a075SMiri Korenblit 	return ret;
24184df6a075SMiri Korenblit }
24194df6a075SMiri Korenblit 
24209c4f15caSMiri Korenblit /*
24219c4f15caSMiri Korenblit  * This function sets the MU EDCA parameters ans returns whether MU EDCA
24229c4f15caSMiri Korenblit  * is enabled or not
24239c4f15caSMiri Korenblit  */
242455eb1c5fSMiri Korenblit bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2425e119e740SEmmanuel Grumbach 				   const struct iwl_mvm_vif_link_info *link_info,
242655eb1c5fSMiri Korenblit 				   struct iwl_he_backoff_conf *trig_based_txf)
24279c4f15caSMiri Korenblit {
24289c4f15caSMiri Korenblit 	int i;
24299c4f15caSMiri Korenblit 	/* Mark MU EDCA as enabled, unless none detected on some AC */
24309c4f15caSMiri Korenblit 	bool mu_edca_enabled = true;
24319c4f15caSMiri Korenblit 
24329c4f15caSMiri Korenblit 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2433e119e740SEmmanuel Grumbach 		const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2434e119e740SEmmanuel Grumbach 			&link_info->queue_params[i].mu_edca_param_rec;
24359c4f15caSMiri Korenblit 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
24369c4f15caSMiri Korenblit 
2437e119e740SEmmanuel Grumbach 		if (!link_info->queue_params[i].mu_edca) {
24389c4f15caSMiri Korenblit 			mu_edca_enabled = false;
24399c4f15caSMiri Korenblit 			break;
24409c4f15caSMiri Korenblit 		}
24419c4f15caSMiri Korenblit 
24429c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmin =
24439c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
24449c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmax =
24459c4f15caSMiri Korenblit 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
24469c4f15caSMiri Korenblit 		trig_based_txf[ac].aifsn =
24479c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->aifsn & 0xf);
24489c4f15caSMiri Korenblit 		trig_based_txf[ac].mu_time =
24499c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->mu_edca_timer);
24509c4f15caSMiri Korenblit 	}
24519c4f15caSMiri Korenblit 
24529c4f15caSMiri Korenblit 	return mu_edca_enabled;
24539c4f15caSMiri Korenblit }
24549c4f15caSMiri Korenblit 
24559be162a7SMiri Korenblit bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
24569c4f15caSMiri Korenblit {
24579c4f15caSMiri Korenblit 	const struct ieee80211_supported_band *sband;
24589c4f15caSMiri Korenblit 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
24599c4f15caSMiri Korenblit 
24609c4f15caSMiri Korenblit 	/* This capability is the same for all bands,
24619c4f15caSMiri Korenblit 	 * so take it from one of them.
24629c4f15caSMiri Korenblit 	 */
24639c4f15caSMiri Korenblit 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
24641ec7291eSJohannes Berg 	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
24659c4f15caSMiri Korenblit 
24669c4f15caSMiri Korenblit 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
24679c4f15caSMiri Korenblit 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
24689c4f15caSMiri Korenblit }
24699c4f15caSMiri Korenblit 
247057974a55SGregory Greenman __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
247157974a55SGregory Greenman 				 struct ieee80211_link_sta *link_sta)
24724df6a075SMiri Korenblit {
247357974a55SGregory Greenman 	u8 *mac_cap_info =
247457974a55SGregory Greenman 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
24754df6a075SMiri Korenblit 	__le32 htc_flags = 0;
24764df6a075SMiri Korenblit 
24774df6a075SMiri Korenblit 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
24784df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
24794df6a075SMiri Korenblit 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
24804df6a075SMiri Korenblit 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
24814df6a075SMiri Korenblit 		u8 link_adap =
24824df6a075SMiri Korenblit 			((mac_cap_info[2] &
24834df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
24844df6a075SMiri Korenblit 			 (mac_cap_info[1] &
24854df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
24864df6a075SMiri Korenblit 
24874df6a075SMiri Korenblit 		if (link_adap == 2)
24884df6a075SMiri Korenblit 			htc_flags |=
24894df6a075SMiri Korenblit 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
24904df6a075SMiri Korenblit 		else if (link_adap == 3)
24914df6a075SMiri Korenblit 			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
24924df6a075SMiri Korenblit 	}
24934df6a075SMiri Korenblit 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
24944df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
24954df6a075SMiri Korenblit 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
24964df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
24974df6a075SMiri Korenblit 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
24984df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
24994df6a075SMiri Korenblit 
25004df6a075SMiri Korenblit 	return htc_flags;
25014df6a075SMiri Korenblit }
25024df6a075SMiri Korenblit 
2503514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2504514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2505514c3069SLuca Coelho {
2506514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
250742506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2508514c3069SLuca Coelho 		.sta_id = sta_id,
2509514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2510dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2511514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2512514c3069SLuca Coelho 		.frame_time_rts_th =
2513514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2514514c3069SLuca Coelho 	};
251542506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
251642506dd2SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
251742506dd2SJohannes Berg 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
251842506dd2SJohannes Berg 	int size;
2519514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2520514c3069SLuca Coelho 	u32 flags;
2521514c3069SLuca Coelho 	int i;
252242506dd2SJohannes Berg 	void *cmd;
252342506dd2SJohannes Berg 
252442506dd2SJohannes Berg 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
252542506dd2SJohannes Berg 		ver = 1;
252642506dd2SJohannes Berg 
252742506dd2SJohannes Berg 	switch (ver) {
252842506dd2SJohannes Berg 	case 1:
252942506dd2SJohannes Berg 		/* same layout as v2 except some data at the end */
253042506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
253142506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
253242506dd2SJohannes Berg 		break;
253342506dd2SJohannes Berg 	case 2:
253442506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
253542506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
253642506dd2SJohannes Berg 		break;
253742506dd2SJohannes Berg 	case 3:
253842506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd;
253942506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
254042506dd2SJohannes Berg 		break;
254142506dd2SJohannes Berg 	default:
254242506dd2SJohannes Berg 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
254342506dd2SJohannes Berg 		return;
254442506dd2SJohannes Berg 	}
2545514c3069SLuca Coelho 
2546514c3069SLuca Coelho 	rcu_read_lock();
2547514c3069SLuca Coelho 
2548514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
254912d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2550514c3069SLuca Coelho 		rcu_read_unlock();
2551514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2552514c3069SLuca Coelho 		return;
2553514c3069SLuca Coelho 	}
2554514c3069SLuca Coelho 
2555046d2e7cSSriram R 	if (!sta->deflink.he_cap.has_he) {
2556514c3069SLuca Coelho 		rcu_read_unlock();
2557514c3069SLuca Coelho 		return;
2558514c3069SLuca Coelho 	}
2559514c3069SLuca Coelho 
2560514c3069SLuca Coelho 	flags = 0;
2561514c3069SLuca Coelho 
25624f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
2563650cadb7SGregory Greenman 	if (mvmvif->deflink.he_ru_2mhz_block)
25644f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
25654f58121dSIlan Peer 
2566514c3069SLuca Coelho 	/* HTC flags */
256757974a55SGregory Greenman 	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2568514c3069SLuca Coelho 
25694df6a075SMiri Korenblit 	/* PPE Thresholds */
257057974a55SGregory Greenman 	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2571cb63eb43SMiri Korenblit 		flags |= STA_CTXT_HE_PACKET_EXT;
257273f23d91SShaul Triebitz 
2573046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
257473f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
257573f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
257673f23d91SShaul Triebitz 
2577046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
257873f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
257973f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
258073f23d91SShaul Triebitz 
2581514c3069SLuca Coelho 	rcu_read_unlock();
2582514c3069SLuca Coelho 
2583e119e740SEmmanuel Grumbach 	if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
25849c4f15caSMiri Korenblit 					  &sta_ctxt_cmd.trig_based_txf[0]))
2585514c3069SLuca Coelho 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2586514c3069SLuca Coelho 
2587514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2588514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2589514c3069SLuca Coelho 
2590514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2591514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2592514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2593514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2594514c3069SLuca Coelho 	}
2595514c3069SLuca Coelho 
25969c4f15caSMiri Korenblit 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2597ee1a02d7SShaul Triebitz 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2598ee1a02d7SShaul Triebitz 
2599918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2600918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2601918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2602918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2603d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2604d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2605d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2606d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2607d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2608d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2609918cbf39SSara Sharon 	}
2610514c3069SLuca Coelho 
2611514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2612514c3069SLuca Coelho 
261342506dd2SJohannes Berg 	if (ver < 3) {
261442506dd2SJohannes Berg 		/* fields before pkt_ext */
261542506dd2SJohannes Berg 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
261642506dd2SJohannes Berg 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
261742506dd2SJohannes Berg 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
261842506dd2SJohannes Berg 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
261942506dd2SJohannes Berg 
262042506dd2SJohannes Berg 		/* pkt_ext */
262142506dd2SJohannes Berg 		for (i = 0;
262242506dd2SJohannes Berg 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
262342506dd2SJohannes Berg 		     i++) {
262442506dd2SJohannes Berg 			u8 bw;
262542506dd2SJohannes Berg 
262642506dd2SJohannes Berg 			for (bw = 0;
262742506dd2SJohannes Berg 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
262842506dd2SJohannes Berg 			     bw++) {
262942506dd2SJohannes Berg 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
263042506dd2SJohannes Berg 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
263142506dd2SJohannes Berg 
263242506dd2SJohannes Berg 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
263342506dd2SJohannes Berg 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
263442506dd2SJohannes Berg 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
263542506dd2SJohannes Berg 			}
263642506dd2SJohannes Berg 		}
263742506dd2SJohannes Berg 
263842506dd2SJohannes Berg 		/* fields after pkt_ext */
263942506dd2SJohannes Berg 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
264042506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
264142506dd2SJohannes Berg 			     sizeof(sta_ctxt_cmd_v2) -
264242506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
264342506dd2SJohannes Berg 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
264442506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
264542506dd2SJohannes Berg 		       (u8 *)&sta_ctxt_cmd +
264642506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
264742506dd2SJohannes Berg 		       sizeof(sta_ctxt_cmd) -
264842506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
264942506dd2SJohannes Berg 		sta_ctxt_cmd_v2.reserved3 = 0;
265042506dd2SJohannes Berg 	}
265142506dd2SJohannes Berg 
265242506dd2SJohannes Berg 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2653514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2654514c3069SLuca Coelho }
2655514c3069SLuca Coelho 
2656660eba5aSMiri Korenblit void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
265723673041SMiri Korenblit 			   u32 duration_override, unsigned int link_id)
2658af84ac57SJohannes Berg {
2659af84ac57SJohannes Berg 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2660af84ac57SJohannes Berg 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2661af84ac57SJohannes Berg 
2662af84ac57SJohannes Berg 	if (duration_override > duration)
2663af84ac57SJohannes Berg 		duration = duration_override;
2664af84ac57SJohannes Berg 
2665af84ac57SJohannes Berg 	/* Try really hard to protect the session and hear a beacon
2666af84ac57SJohannes Berg 	 * The new session protection command allows us to protect the
2667af84ac57SJohannes Berg 	 * session for a much longer time since the firmware will internally
2668af84ac57SJohannes Berg 	 * create two events: a 300TU one with a very high priority that
2669af84ac57SJohannes Berg 	 * won't be fragmented which should be enough for 99% of the cases,
2670af84ac57SJohannes Berg 	 * and another one (which we configure here to be 900TU long) which
2671af84ac57SJohannes Berg 	 * will have a slightly lower priority, but more importantly, can be
2672af84ac57SJohannes Berg 	 * fragmented so that it'll allow other activities to run.
2673af84ac57SJohannes Berg 	 */
2674af84ac57SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
2675af84ac57SJohannes Berg 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2676af84ac57SJohannes Berg 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
267723673041SMiri Korenblit 						    min_duration, false,
267823673041SMiri Korenblit 						    link_id);
2679af84ac57SJohannes Berg 	else
2680af84ac57SJohannes Berg 		iwl_mvm_protect_session(mvm, vif, duration,
2681af84ac57SJohannes Berg 					min_duration, 500, false);
2682af84ac57SJohannes Berg }
2683af84ac57SJohannes Berg 
2684660eba5aSMiri Korenblit /* Handle association common part to MLD and non-MLD modes */
2685660eba5aSMiri Korenblit void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2686660eba5aSMiri Korenblit 					    struct ieee80211_vif *vif,
2687660eba5aSMiri Korenblit 					    u64 changes)
2688660eba5aSMiri Korenblit {
2689660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2690660eba5aSMiri Korenblit 	int ret;
2691c1e458b9SIlan Peer 	int link_id;
2692660eba5aSMiri Korenblit 
2693660eba5aSMiri Korenblit 	/* The firmware tracks the MU-MIMO group on its own.
2694660eba5aSMiri Korenblit 	 * However, on HW restart we should restore this data.
2695660eba5aSMiri Korenblit 	 */
2696660eba5aSMiri Korenblit 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2697660eba5aSMiri Korenblit 	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2698660eba5aSMiri Korenblit 		ret = iwl_mvm_update_mu_groups(mvm, vif);
2699660eba5aSMiri Korenblit 		if (ret)
2700660eba5aSMiri Korenblit 			IWL_ERR(mvm,
2701660eba5aSMiri Korenblit 				"failed to update VHT MU_MIMO groups\n");
2702660eba5aSMiri Korenblit 	}
2703660eba5aSMiri Korenblit 
2704660eba5aSMiri Korenblit 	iwl_mvm_recalc_multicast(mvm);
2705660eba5aSMiri Korenblit 
2706660eba5aSMiri Korenblit 	/* reset rssi values */
2707c1e458b9SIlan Peer 	for_each_mvm_vif_valid_link(mvmvif, link_id)
2708c1e458b9SIlan Peer 		mvmvif->link[link_id]->bf_data.ave_beacon_signal = 0;
2709660eba5aSMiri Korenblit 
2710660eba5aSMiri Korenblit 	iwl_mvm_bt_coex_vif_change(mvm);
27111a3e7039SGregory Greenman 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2712660eba5aSMiri Korenblit 					    IEEE80211_SMPS_AUTOMATIC);
2713660eba5aSMiri Korenblit 	if (fw_has_capa(&mvm->fw->ucode_capa,
2714660eba5aSMiri Korenblit 			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2715660eba5aSMiri Korenblit 		iwl_mvm_config_scan(mvm);
2716660eba5aSMiri Korenblit }
2717660eba5aSMiri Korenblit 
2718660eba5aSMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
27191a3e7039SGregory Greenman void
27201a3e7039SGregory Greenman iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2721660eba5aSMiri Korenblit 					struct ieee80211_vif *vif,
27221a3e7039SGregory Greenman 					struct ieee80211_bss_conf *link_conf,
2723660eba5aSMiri Korenblit 					u64 changes)
2724660eba5aSMiri Korenblit {
2725660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2726660eba5aSMiri Korenblit 	int ret;
2727660eba5aSMiri Korenblit 
2728660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BEACON_INFO) {
2729660eba5aSMiri Korenblit 		/* We received a beacon from the associated AP so
2730660eba5aSMiri Korenblit 		 * remove the session protection.
2731660eba5aSMiri Korenblit 		 */
2732660eba5aSMiri Korenblit 		iwl_mvm_stop_session_protection(mvm, vif);
2733660eba5aSMiri Korenblit 
2734660eba5aSMiri Korenblit 		iwl_mvm_sf_update(mvm, vif, false);
273587f5b5f2SJohannes Berg 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
2736660eba5aSMiri Korenblit 	}
2737660eba5aSMiri Korenblit 
2738660eba5aSMiri Korenblit 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2739660eba5aSMiri Korenblit 		       /* Send power command on every beacon change,
2740660eba5aSMiri Korenblit 			* because we may have not enabled beacon abort yet.
2741660eba5aSMiri Korenblit 			*/
2742660eba5aSMiri Korenblit 		       BSS_CHANGED_BEACON_INFO)) {
2743660eba5aSMiri Korenblit 		ret = iwl_mvm_power_update_mac(mvm);
2744660eba5aSMiri Korenblit 		if (ret)
2745660eba5aSMiri Korenblit 			IWL_ERR(mvm, "failed to update power mode\n");
2746660eba5aSMiri Korenblit 	}
2747660eba5aSMiri Korenblit 
2748660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_CQM) {
2749c1e458b9SIlan Peer 		struct iwl_mvm_vif_link_info *link_info =
2750c1e458b9SIlan Peer 			mvmvif->link[link_conf->link_id];
2751c1e458b9SIlan Peer 
2752c1e458b9SIlan Peer 		IWL_DEBUG_MAC80211(mvm, "CQM info_changed\n");
2753c1e458b9SIlan Peer 		if (link_info)
2754c1e458b9SIlan Peer 			link_info->bf_data.last_cqm_event = 0;
2755c1e458b9SIlan Peer 
2756c1e458b9SIlan Peer 		if (mvmvif->bf_enabled) {
275722c58834SGregory Greenman 			/* FIXME: need to update per link when FW API will
275822c58834SGregory Greenman 			 * support it
275922c58834SGregory Greenman 			 */
276087f5b5f2SJohannes Berg 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
2761660eba5aSMiri Korenblit 			if (ret)
2762660eba5aSMiri Korenblit 				IWL_ERR(mvm,
2763660eba5aSMiri Korenblit 					"failed to update CQM thresholds\n");
2764660eba5aSMiri Korenblit 		}
2765660eba5aSMiri Korenblit 	}
2766660eba5aSMiri Korenblit 
2767660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BANDWIDTH)
27681a3e7039SGregory Greenman 		iwl_mvm_update_link_smps(vif, link_conf);
27696b82f4e1SJohannes Berg 
27706b82f4e1SJohannes Berg 	if (changes & BSS_CHANGED_TPE) {
27716b82f4e1SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TPE\n");
27726b82f4e1SJohannes Berg 		iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif,
27736b82f4e1SJohannes Berg 							link_conf,
27746b82f4e1SJohannes Berg 							false);
27756b82f4e1SJohannes Berg 	}
2776660eba5aSMiri Korenblit }
2777660eba5aSMiri Korenblit 
2778e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2779e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2780e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
27817b7090b4SJohannes Berg 					     u64 changes)
2782e705c121SKalle Valo {
2783e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2784e705c121SKalle Valo 	int ret;
2785b6e3d1baSAnjaneyulu 	int i;
2786e705c121SKalle Valo 
2787e705c121SKalle Valo 	/*
2788cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend
2789cdaba917SEmmanuel Grumbach 	 * on the beacon interval, which was not known when the station
2790cdaba917SEmmanuel Grumbach 	 * interface was added.
2791e705c121SKalle Valo 	 */
2792f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2793cb63eb43SMiri Korenblit 		if ((vif->bss_conf.he_support &&
279483f57c93SJohannes Berg 		     !iwlwifi_mod_params.disable_11ax))
2795650cadb7SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2796514c3069SLuca Coelho 
2797e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2798514c3069SLuca Coelho 	}
2799e705c121SKalle Valo 
280040ecdd01SShaul Triebitz 	/* Update MU EDCA params */
280140ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2802cb63eb43SMiri Korenblit 	    vif->cfg.assoc &&
280383f57c93SJohannes Berg 	    (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax))
2804650cadb7SGregory Greenman 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
280540ecdd01SShaul Triebitz 
2806e705c121SKalle Valo 	/*
2807e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2808e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2809e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2810e705c121SKalle Valo 	 * branch for disassociation below.
2811e705c121SKalle Valo 	 */
2812e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2813650cadb7SGregory Greenman 		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2814e705c121SKalle Valo 
2815650cadb7SGregory Greenman 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2816e705c121SKalle Valo 	if (ret)
2817e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2818e705c121SKalle Valo 
2819e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2820650cadb7SGregory Greenman 	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2821f276e20bSJohannes Berg 	mvmvif->associated = vif->cfg.assoc;
2822e705c121SKalle Valo 
2823e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2824f276e20bSJohannes Berg 		if (vif->cfg.assoc) {
2825e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2826e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2827b6e3d1baSAnjaneyulu 			for_each_mvm_vif_valid_link(mvmvif, i)
2828b6e3d1baSAnjaneyulu 				memset(&mvmvif->link[i]->beacon_stats, 0,
2829b6e3d1baSAnjaneyulu 				       sizeof(mvmvif->link[i]->beacon_stats));
2830e705c121SKalle Valo 
2831e705c121SKalle Valo 			/* add quota for this interface */
2832e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2833e705c121SKalle Valo 			if (ret) {
2834e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2835e705c121SKalle Valo 				return;
2836e705c121SKalle Valo 			}
2837e705c121SKalle Valo 
2838e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2839fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2840fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2841fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2842e705c121SKalle Valo 				/*
2843e705c121SKalle Valo 				 * If we're restarting then the firmware will
2844e705c121SKalle Valo 				 * obviously have lost synchronisation with
2845e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2846e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2847e705c121SKalle Valo 				 * scheduling a session protection time event.
2848e705c121SKalle Valo 				 *
2849e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2850e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2851e705c121SKalle Valo 				 * the beacon interval.
2852e705c121SKalle Valo 				 *
2853e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2854e705c121SKalle Valo 				 * than a single interface.
2855fe959c7bSEmmanuel Grumbach 				 *
2856fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2857fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2858fe959c7bSEmmanuel Grumbach 				 * only anyway.
2859e705c121SKalle Valo 				 */
2860e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2861e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2862e705c121SKalle Valo 							5 * dur, false);
2863af84ac57SJohannes Berg 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2864af84ac57SJohannes Berg 					     &mvm->status) &&
2865af84ac57SJohannes Berg 				   !vif->bss_conf.dtim_period) {
2866af84ac57SJohannes Berg 				/*
2867af84ac57SJohannes Berg 				 * If we're not restarting and still haven't
2868af84ac57SJohannes Berg 				 * heard a beacon (dtim period unknown) then
2869af84ac57SJohannes Berg 				 * make sure we still have enough minimum time
2870af84ac57SJohannes Berg 				 * remaining in the time event, since the auth
2871af84ac57SJohannes Berg 				 * might actually have taken quite a while
2872af84ac57SJohannes Berg 				 * (especially for SAE) and so the remaining
2873af84ac57SJohannes Berg 				 * time could be small without us having heard
2874af84ac57SJohannes Berg 				 * a beacon yet.
2875af84ac57SJohannes Berg 				 */
287623673041SMiri Korenblit 				iwl_mvm_protect_assoc(mvm, vif, 0, 0);
2877e705c121SKalle Valo 			}
2878e705c121SKalle Valo 
2879e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2880e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2881e705c121SKalle Valo 			if (vif->p2p) {
2882e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2883e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
28841a3e7039SGregory Greenman 						    IEEE80211_SMPS_DYNAMIC, 0);
2885e705c121SKalle Valo 			}
2886650cadb7SGregory Greenman 		} else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
28876d19a5ebSEmmanuel Grumbach 			iwl_mvm_mei_host_disassociated(mvm);
2888e705c121SKalle Valo 			/*
2889e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2890e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2891e705c121SKalle Valo 			 */
289269e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
289369e508b4SIlan Peer 			WARN_ONCE(ret &&
289469e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
289569e508b4SIlan Peer 					    &mvm->status),
2896e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2897e705c121SKalle Valo 
28986b28f978SEmmanuel Grumbach 			/*
28996b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
29006b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
29016b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
29026b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
29036b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
29046b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
29056b28f978SEmmanuel Grumbach 			 */
29066b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
29076b28f978SEmmanuel Grumbach 				      &mvm->status)) {
29085c75a208SJohannes Berg 				/* first remove remaining keys */
2909ba9eef6bSGregory Greenman 				iwl_mvm_sec_key_remove_ap(mvm, vif,
2910072573f6SJohannes Berg 							  &mvmvif->deflink, 0);
29115c75a208SJohannes Berg 
29126b28f978SEmmanuel Grumbach 				/*
29136b28f978SEmmanuel Grumbach 				 * Remove AP station now that
29146b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
29156b28f978SEmmanuel Grumbach 				 */
29166b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
2917650cadb7SGregory Greenman 							mvmvif->deflink.ap_sta_id);
2918e705c121SKalle Valo 				if (ret)
29196b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
29206b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2921e705c121SKalle Valo 
2922650cadb7SGregory Greenman 				mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
29236b28f978SEmmanuel Grumbach 			}
29246b28f978SEmmanuel Grumbach 
2925e705c121SKalle Valo 			/* remove quota for this interface */
2926e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2927e705c121SKalle Valo 			if (ret)
2928e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2929e705c121SKalle Valo 
2930e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2931e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2932e705c121SKalle Valo 			if (ret)
2933e705c121SKalle Valo 				IWL_ERR(mvm,
2934e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2935e705c121SKalle Valo 					vif->addr);
2936e705c121SKalle Valo 		}
2937e705c121SKalle Valo 
2938660eba5aSMiri Korenblit 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
2939a07a8f37SSara Sharon 	}
2940a07a8f37SSara Sharon 
29411a3e7039SGregory Greenman 	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
29421a3e7039SGregory Greenman 						changes);
2943b45242c9SAvraham Stern }
2944b45242c9SAvraham Stern 
2945f947b62cSMiri Korenblit bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2946f947b62cSMiri Korenblit 				  struct ieee80211_vif *vif,
2947f947b62cSMiri Korenblit 				  int *ret)
2948f947b62cSMiri Korenblit {
2949f947b62cSMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2950f947b62cSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2951f947b62cSMiri Korenblit 	int i;
2952e705c121SKalle Valo 
2953f947b62cSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
2954f947b62cSMiri Korenblit 
2955f947b62cSMiri Korenblit 	mvmvif->ap_assoc_sta_count = 0;
2956f947b62cSMiri Korenblit 
2957f947b62cSMiri Korenblit 	/* must be set before quota calculations */
2958f947b62cSMiri Korenblit 	mvmvif->ap_ibss_active = true;
2959f947b62cSMiri Korenblit 
2960f947b62cSMiri Korenblit 	/* send all the early keys to the device now */
2961f947b62cSMiri Korenblit 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2962f947b62cSMiri Korenblit 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2963f947b62cSMiri Korenblit 
2964f947b62cSMiri Korenblit 		if (!key)
2965f947b62cSMiri Korenblit 			continue;
2966f947b62cSMiri Korenblit 
2967f947b62cSMiri Korenblit 		mvmvif->ap_early_keys[i] = NULL;
2968f947b62cSMiri Korenblit 
2969f947b62cSMiri Korenblit 		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2970f947b62cSMiri Korenblit 		if (*ret)
2971f947b62cSMiri Korenblit 			return true;
2972e705c121SKalle Valo 	}
2973e705c121SKalle Valo 
2974f947b62cSMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2975f947b62cSMiri Korenblit 		iwl_mvm_vif_set_low_latency(mvmvif, true,
2976f947b62cSMiri Korenblit 					    LOW_LATENCY_VIF_TYPE);
2977f947b62cSMiri Korenblit 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2978e705c121SKalle Valo 	}
2979e705c121SKalle Valo 
2980f947b62cSMiri Korenblit 	/* power updated needs to be done before quotas */
2981f947b62cSMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
2982e705c121SKalle Valo 
2983f947b62cSMiri Korenblit 	return false;
2984e705c121SKalle Valo }
2985e705c121SKalle Valo 
2986e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2987ae7ba17bSShaul Triebitz 				 struct ieee80211_vif *vif,
2988b327c84cSGregory Greenman 				 struct ieee80211_bss_conf *link_conf)
2989e705c121SKalle Valo {
2990e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2991e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2992f947b62cSMiri Korenblit 	int ret;
2993e705c121SKalle Valo 
2994e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2995e705c121SKalle Valo 
2996e705c121SKalle Valo 	/*
2997cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2998cdaba917SEmmanuel Grumbach 	 * the beacon interval, which was not known when the AP interface
2999cdaba917SEmmanuel Grumbach 	 * was added.
3000e705c121SKalle Valo 	 */
3001e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
3002e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
3003e705c121SKalle Valo 
300436cf5377SGregory Greenman 	/* For older devices need to send beacon template before adding mac
300536cf5377SGregory Greenman 	 * context. For the newer, the beacon is a resource that belongs to a
300636cf5377SGregory Greenman 	 * MAC, so need to send beacon template after adding the mac.
300736cf5377SGregory Greenman 	 */
300836cf5377SGregory Greenman 	if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
3009e705c121SKalle Valo 		/* Add the mac context */
3010e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3011e705c121SKalle Valo 		if (ret)
3012e705c121SKalle Valo 			goto out_unlock;
3013e705c121SKalle Valo 
301436cf5377SGregory Greenman 		/* Send the beacon template */
301536cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
301636cf5377SGregory Greenman 		if (ret)
301736cf5377SGregory Greenman 			goto out_unlock;
301836cf5377SGregory Greenman 	} else {
301936cf5377SGregory Greenman 		/* Send the beacon template */
302036cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
302136cf5377SGregory Greenman 		if (ret)
302236cf5377SGregory Greenman 			goto out_unlock;
3023e705c121SKalle Valo 
3024e705c121SKalle Valo 		/* Add the mac context */
3025e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3026e705c121SKalle Valo 		if (ret)
3027e705c121SKalle Valo 			goto out_unlock;
302836cf5377SGregory Greenman 	}
3029e705c121SKalle Valo 
3030e705c121SKalle Valo 	/* Perform the binding */
3031e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
3032e705c121SKalle Valo 	if (ret)
3033e705c121SKalle Valo 		goto out_remove;
3034e705c121SKalle Valo 
303563dd5d02SSara Sharon 	/*
303663dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
303763dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
303863dd5d02SSara Sharon 	 * cause assert 0x2b00.
303963dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
304063dd5d02SSara Sharon 	 * the TLV
304163dd5d02SSara Sharon 	 */
304263dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
3043ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
3044ced19f26SSara Sharon 		if (ret)
3045ced19f26SSara Sharon 			goto out_unbind;
304663dd5d02SSara Sharon 		/*
304763dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
304863dd5d02SSara Sharon 		 * events are added and applied to the scheduler
304963dd5d02SSara Sharon 		 */
3050e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
305163dd5d02SSara Sharon 		if (ret) {
305263dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
305363dd5d02SSara Sharon 			goto out_unbind;
305463dd5d02SSara Sharon 		}
305563dd5d02SSara Sharon 	} else {
305663dd5d02SSara Sharon 		/*
305763dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
305863dd5d02SSara Sharon 		 * events are added and applied to the scheduler
305963dd5d02SSara Sharon 		 */
306075fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3061e705c121SKalle Valo 		if (ret)
306263dd5d02SSara Sharon 			goto out_unbind;
306375fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
306463dd5d02SSara Sharon 		if (ret) {
306563dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
306663dd5d02SSara Sharon 			goto out_unbind;
306763dd5d02SSara Sharon 		}
306863dd5d02SSara Sharon 	}
306926d6c16bSSara Sharon 
3070f947b62cSMiri Korenblit 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
3071f947b62cSMiri Korenblit 		goto out_failed;
3072e705c121SKalle Valo 
3073e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
3074e705c121SKalle Valo 	if (ret)
3075f947b62cSMiri Korenblit 		goto out_failed;
3076e705c121SKalle Valo 
3077e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3078e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
3079e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3080e705c121SKalle Valo 
3081e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
3082e705c121SKalle Valo 
3083e705c121SKalle Valo 	/* we don't support TDLS during DCM */
3084e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3085e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
3086e705c121SKalle Valo 
3087fd940de7SAvraham Stern 	iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
3088b73f9a4aSJohannes Berg 
3089e705c121SKalle Valo 	goto out_unlock;
3090e705c121SKalle Valo 
3091f947b62cSMiri Korenblit out_failed:
3092e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3093e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
3094e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3095ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
3096e705c121SKalle Valo out_unbind:
3097e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
3098e705c121SKalle Valo out_remove:
3099e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3100e705c121SKalle Valo out_unlock:
3101e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3102e705c121SKalle Valo 	return ret;
3103e705c121SKalle Valo }
3104e705c121SKalle Valo 
3105ae7ba17bSShaul Triebitz static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
3106ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
3107b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
3108ae7ba17bSShaul Triebitz {
3109b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
3110ae7ba17bSShaul Triebitz }
3111ae7ba17bSShaul Triebitz 
3112ae7ba17bSShaul Triebitz static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
3113e705c121SKalle Valo 			      struct ieee80211_vif *vif)
3114e705c121SKalle Valo {
3115b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
3116ae7ba17bSShaul Triebitz }
3117ae7ba17bSShaul Triebitz 
3118fd1a54c1SMiri Korenblit /* Common part for MLD and non-MLD ops */
3119fd1a54c1SMiri Korenblit void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
3120fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif)
3121ae7ba17bSShaul Triebitz {
3122e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3123e705c121SKalle Valo 
3124fd1a54c1SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
3125e705c121SKalle Valo 
3126fd1a54c1SMiri Korenblit 	iwl_mvm_prepare_mac_removal(mvm, vif);
3127e705c121SKalle Valo 
3128e705c121SKalle Valo 	/* Handle AP stop while in CSA */
3129e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
3130e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
3131e705c121SKalle Valo 					  &mvmvif->time_event_data);
3132e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
3133e705c121SKalle Valo 		mvmvif->csa_countdown = false;
3134e705c121SKalle Valo 	}
3135e705c121SKalle Valo 
3136e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
3137e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
3138e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
3139e705c121SKalle Valo 	}
3140e705c121SKalle Valo 
3141e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
3142e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
3143e705c121SKalle Valo 
314447242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
314547242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
314647242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
314747242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
314847242744STova Mussai 	}
314947242744STova Mussai 
3150e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
3151fd1a54c1SMiri Korenblit }
3152fd1a54c1SMiri Korenblit 
3153fd1a54c1SMiri Korenblit static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
3154fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif,
3155fd1a54c1SMiri Korenblit 				 struct ieee80211_bss_conf *link_conf)
3156fd1a54c1SMiri Korenblit {
3157fd1a54c1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3158fd1a54c1SMiri Korenblit 
31593f312651SJohannes Berg 	guard(mvm)(mvm);
3160fd1a54c1SMiri Korenblit 
3161fd1a54c1SMiri Korenblit 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
3162e705c121SKalle Valo 
3163e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3164e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
3165e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3166e705c121SKalle Valo 
3167e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
3168ced19f26SSara Sharon 
3169be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
3170be82ecd3SAvraham Stern 
3171ced19f26SSara Sharon 	/*
3172ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
3173ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
3174ced19f26SSara Sharon 	 * cause assert 0x2b00.
3175ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
3176ced19f26SSara Sharon 	 * bcast station).
3177ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
3178ced19f26SSara Sharon 	 */
3179ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
318026d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
3181e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3182ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3183ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
3184e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
3185e705c121SKalle Valo 
3186e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3187e705c121SKalle Valo 
3188e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3189e705c121SKalle Valo }
3190e705c121SKalle Valo 
3191ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
3192ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
3193b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
3194ae7ba17bSShaul Triebitz {
3195b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
3196ae7ba17bSShaul Triebitz }
3197ae7ba17bSShaul Triebitz 
3198ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
3199ae7ba17bSShaul Triebitz 			      struct ieee80211_vif *vif)
3200ae7ba17bSShaul Triebitz {
3201b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3202ae7ba17bSShaul Triebitz }
3203ae7ba17bSShaul Triebitz 
3204e705c121SKalle Valo static void
3205e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3206e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3207e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
32087b7090b4SJohannes Berg 				 u64 changes)
3209e705c121SKalle Valo {
3210e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3211e705c121SKalle Valo 
3212e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
3213e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
3214e705c121SKalle Valo 		return;
3215e705c121SKalle Valo 
3216e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3217e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3218e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3219e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3220e705c121SKalle Valo 
3221e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
3222e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
322336cf5377SGregory Greenman 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3224e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
3225e705c121SKalle Valo 
3226b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3227fd940de7SAvraham Stern 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3228b73f9a4aSJohannes Berg 
3229b73f9a4aSJohannes Berg 		if (ret)
3230b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3231b73f9a4aSJohannes Berg 				 ret);
3232b73f9a4aSJohannes Berg 	}
3233b73f9a4aSJohannes Berg 
3234e705c121SKalle Valo }
3235e705c121SKalle Valo 
3236e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3237e705c121SKalle Valo 				     struct ieee80211_vif *vif,
3238e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
32397b7090b4SJohannes Berg 				     u64 changes)
3240e705c121SKalle Valo {
3241e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3242e705c121SKalle Valo 
32433f312651SJohannes Berg 	guard(mvm)(mvm);
3244e705c121SKalle Valo 
3245f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3246e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3247e705c121SKalle Valo 
3248e705c121SKalle Valo 	switch (vif->type) {
3249e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3250df7e3098SMiri Korenblit 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
3251e705c121SKalle Valo 		break;
3252e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3253e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
3254df7e3098SMiri Korenblit 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
3255e705c121SKalle Valo 		break;
325691b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
325791b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
325891b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
325991b08c2dSAviya Erenfeld 		break;
3260e705c121SKalle Valo 	default:
3261e705c121SKalle Valo 		/* shouldn't happen */
3262e705c121SKalle Valo 		WARN_ON_ONCE(1);
3263e705c121SKalle Valo 	}
3264e705c121SKalle Valo 
32659aae43a4SJohannes Berg 	if (changes & BSS_CHANGED_TXPOWER) {
32669aae43a4SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
32679aae43a4SJohannes Berg 				bss_conf->txpower);
32689aae43a4SJohannes Berg 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
32699aae43a4SJohannes Berg 	}
3270e705c121SKalle Valo }
3271e705c121SKalle Valo 
3272cbce62a3SMiri Korenblit int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3273e705c121SKalle Valo 			struct ieee80211_scan_request *hw_req)
3274e705c121SKalle Valo {
3275e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3276e705c121SKalle Valo 
3277e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
3278e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3279e705c121SKalle Valo 		return -EINVAL;
3280e705c121SKalle Valo 
32813f312651SJohannes Berg 	guard(mvm)(mvm);
32823f312651SJohannes Berg 	return iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3283e705c121SKalle Valo }
3284e705c121SKalle Valo 
3285cbce62a3SMiri Korenblit void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3286e705c121SKalle Valo 				struct ieee80211_vif *vif)
3287e705c121SKalle Valo {
3288e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3289e705c121SKalle Valo 
32903f312651SJohannes Berg 	guard(mvm)(mvm);
3291e705c121SKalle Valo 
3292e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3293e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
3294e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
3295e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
3296e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
3297e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
3298e705c121SKalle Valo 	*/
3299e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3300e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3301e705c121SKalle Valo }
3302e705c121SKalle Valo 
3303cbce62a3SMiri Korenblit void
3304e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3305e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
3306e705c121SKalle Valo 				  int num_frames,
3307e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
3308e705c121SKalle Valo 				  bool more_data)
3309e705c121SKalle Valo {
3310e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3311e705c121SKalle Valo 
3312e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3313e705c121SKalle Valo 
3314e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3315e705c121SKalle Valo 					  tids, more_data, false);
3316e705c121SKalle Valo }
3317e705c121SKalle Valo 
3318cbce62a3SMiri Korenblit void
3319e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3320e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
3321e705c121SKalle Valo 				    int num_frames,
3322e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
3323e705c121SKalle Valo 				    bool more_data)
3324e705c121SKalle Valo {
3325e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3326e705c121SKalle Valo 
33279a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3328e705c121SKalle Valo 
3329e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3330e705c121SKalle Valo 					  tids, more_data, true);
3331e705c121SKalle Valo }
3332e705c121SKalle Valo 
333365e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3334e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
3335e705c121SKalle Valo 				     struct ieee80211_sta *sta)
3336e705c121SKalle Valo {
3337e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3338e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3339e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
3340e705c121SKalle Valo 	int tid;
3341e705c121SKalle Valo 
3342960f864bSJohannes Berg 	/*
3343960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
3344960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
3345960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
3346960f864bSJohannes Berg 	 */
3347960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3348960f864bSJohannes Berg 		return;
3349960f864bSJohannes Berg 
3350e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3351311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3352e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3353e705c121SKalle Valo 
33546862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
33551c17627bSSara Sharon 			continue;
33561c17627bSSara Sharon 
3357e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
3358e705c121SKalle Valo 
3359dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3360e705c121SKalle Valo 			continue;
3361e705c121SKalle Valo 
3362e705c121SKalle Valo 		__set_bit(tid, &tids);
3363e705c121SKalle Valo 	}
3364e705c121SKalle Valo 
3365e705c121SKalle Valo 	switch (cmd) {
3366e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
3367e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3368e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
3369e705c121SKalle Valo 
3370e705c121SKalle Valo 		if (txqs)
3371e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3372e705c121SKalle Valo 		/*
3373e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3374e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
3375e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3376e705c121SKalle Valo 		 */
3377e705c121SKalle Valo 		break;
3378e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
3379c8ee33e1SGregory Greenman 		if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
3380e705c121SKalle Valo 			break;
3381e705c121SKalle Valo 
3382e705c121SKalle Valo 		if (txqs)
3383e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3384e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3385e705c121SKalle Valo 		break;
3386e705c121SKalle Valo 	default:
3387e705c121SKalle Valo 		break;
3388e705c121SKalle Valo 	}
3389e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3390e705c121SKalle Valo }
3391e705c121SKalle Valo 
3392cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3393cbce62a3SMiri Korenblit 			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
339465e25482SJohannes Berg {
339565e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
339665e25482SJohannes Berg }
339765e25482SJohannes Berg 
339865e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
339965e25482SJohannes Berg {
340065e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
340165e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
340265e25482SJohannes Berg 	struct ieee80211_sta *sta;
340365e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
340465e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
340565e25482SJohannes Berg 
3406be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
340765e25482SJohannes Berg 		return;
340865e25482SJohannes Berg 
340965e25482SJohannes Berg 	rcu_read_lock();
3410a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
341165e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
341265e25482SJohannes Berg 		rcu_read_unlock();
341365e25482SJohannes Berg 		return;
341465e25482SJohannes Berg 	}
341565e25482SJohannes Berg 
341665e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
341765e25482SJohannes Berg 
341865e25482SJohannes Berg 	if (!mvmsta->vif ||
341965e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
342065e25482SJohannes Berg 		rcu_read_unlock();
342165e25482SJohannes Berg 		return;
342265e25482SJohannes Berg 	}
342365e25482SJohannes Berg 
342465e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
342565e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
342665e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
342765e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
342865e25482SJohannes Berg 			sta);
342965e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
343065e25482SJohannes Berg 	}
343165e25482SJohannes Berg 
343265e25482SJohannes Berg 	if (sleeping) {
343365e25482SJohannes Berg 		switch (notif->type) {
343465e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
343565e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
343665e25482SJohannes Berg 			break;
343765e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
343865e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
343965e25482SJohannes Berg 			break;
344065e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
344165e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
344265e25482SJohannes Berg 			break;
344365e25482SJohannes Berg 		default:
344465e25482SJohannes Berg 			break;
344565e25482SJohannes Berg 		}
344665e25482SJohannes Berg 	}
344765e25482SJohannes Berg 
344865e25482SJohannes Berg 	rcu_read_unlock();
344965e25482SJohannes Berg }
345065e25482SJohannes Berg 
3451cbce62a3SMiri Korenblit void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3452e705c121SKalle Valo 				struct ieee80211_vif *vif,
3453e705c121SKalle Valo 				struct ieee80211_sta *sta)
3454e705c121SKalle Valo {
3455e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3456e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
345779faae3aSGregory Greenman 	unsigned int link_id;
3458e705c121SKalle Valo 
3459e705c121SKalle Valo 	/*
3460e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
3461e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
3462e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
3463e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
3464e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
3465e705c121SKalle Valo 	 * callback deleted the station.
346679faae3aSGregory Greenman 	 * Since there's mvm->mutex here, no need to have RCU lock for
346779faae3aSGregory Greenman 	 * mvm_sta->link access.
3468e705c121SKalle Valo 	 */
34693f312651SJohannes Berg 	guard(mvm)(mvm);
347079faae3aSGregory Greenman 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
347179faae3aSGregory Greenman 		struct iwl_mvm_link_sta *link_sta;
347279faae3aSGregory Greenman 		u32 sta_id;
3473e705c121SKalle Valo 
347479faae3aSGregory Greenman 		if (!mvm_sta->link[link_id])
347579faae3aSGregory Greenman 			continue;
347679faae3aSGregory Greenman 
347779faae3aSGregory Greenman 		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
347879faae3aSGregory Greenman 						     lockdep_is_held(&mvm->mutex));
347979faae3aSGregory Greenman 		sta_id = link_sta->sta_id;
3480b8a85a1dSJohannes Berg 		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
34813e75668bSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
348279faae3aSGregory Greenman 					 ERR_PTR(-ENOENT));
3483b8a85a1dSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3484b8a85a1dSJohannes Berg 		}
348579faae3aSGregory Greenman 	}
3486e705c121SKalle Valo }
3487e705c121SKalle Valo 
3488e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3489e705c121SKalle Valo 				const u8 *bssid)
3490e705c121SKalle Valo {
3491b0ffe455SJohannes Berg 	int i;
3492b0ffe455SJohannes Berg 
3493b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3494b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
3495b0ffe455SJohannes Berg 
3496b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3497b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3498b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
3499b0ffe455SJohannes Berg 	}
3500b0ffe455SJohannes Berg 
3501e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3502e705c121SKalle Valo 		return;
3503e705c121SKalle Valo 
3504c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3505cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3506cee5a882SAvri Altman 		return;
3507cee5a882SAvri Altman 	}
3508cee5a882SAvri Altman 
350911dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
351011dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3511e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3512e705c121SKalle Valo 		return;
3513e705c121SKalle Valo 	}
3514e705c121SKalle Valo 
3515b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3516b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3517b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3518b0ffe455SJohannes Berg 			return;
3519b0ffe455SJohannes Berg 		}
3520b0ffe455SJohannes Berg 	}
3521b0ffe455SJohannes Berg 
3522e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3523e705c121SKalle Valo }
3524e705c121SKalle Valo 
35251e8f1329SGolan Ben-Ami static void
35261e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
35271e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
35281e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
35291e8f1329SGolan Ben-Ami {
35301e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
35311e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
35321e8f1329SGolan Ben-Ami 
35336c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
35346c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
35356c042d75SSara Sharon 	if (!trig)
35361e8f1329SGolan Ben-Ami 		return;
35371e8f1329SGolan Ben-Ami 
35381e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
35391e8f1329SGolan Ben-Ami 
35401e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
35411e8f1329SGolan Ben-Ami 		return;
35421e8f1329SGolan Ben-Ami 
35431e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
35441e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
35451e8f1329SGolan Ben-Ami 		return;
35461e8f1329SGolan Ben-Ami 
35477174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
35481e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
35491e8f1329SGolan Ben-Ami 				peer_addr, action);
35501e8f1329SGolan Ben-Ami }
35511e8f1329SGolan Ben-Ami 
35524f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
35534f58121dSIlan Peer 	bool tolerated;
35544f58121dSIlan Peer };
35554f58121dSIlan Peer 
35564f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
35574f58121dSIlan Peer 						    struct cfg80211_bss *bss,
35584f58121dSIlan Peer 						    void *_data)
35594f58121dSIlan Peer {
35604f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
35616c608cd6SJohannes Berg 	const struct cfg80211_bss_ies *ies;
35624f58121dSIlan Peer 	const struct element *elem;
35634f58121dSIlan Peer 
35646c608cd6SJohannes Berg 	rcu_read_lock();
35656c608cd6SJohannes Berg 	ies = rcu_dereference(bss->ies);
35666c608cd6SJohannes Berg 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
35676c608cd6SJohannes Berg 				  ies->len);
35684f58121dSIlan Peer 
35694f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
35704f58121dSIlan Peer 	    !(elem->data[10] &
35714f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
35724f58121dSIlan Peer 		data->tolerated = false;
35734f58121dSIlan Peer 	}
35746c608cd6SJohannes Berg 	rcu_read_unlock();
35754f58121dSIlan Peer }
35764f58121dSIlan Peer 
35776e1b5956SJohannes Berg static void
35786e1b5956SJohannes Berg iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
35796e1b5956SJohannes Berg 				   struct ieee80211_vif *vif,
35806e1b5956SJohannes Berg 				   unsigned int link_id,
35816e1b5956SJohannes Berg 				   struct ieee80211_bss_conf *link_conf)
35824f58121dSIlan Peer {
35834f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
35844f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
35854f58121dSIlan Peer 		.tolerated = true,
35864f58121dSIlan Peer 	};
35874f58121dSIlan Peer 
35886092077aSJohannes Berg 	if (WARN_ON_ONCE(!link_conf->chanreq.oper.chan ||
35896e1b5956SJohannes Berg 			 !mvmvif->link[link_id]))
35906e1b5956SJohannes Berg 		return;
35916e1b5956SJohannes Berg 
35926092077aSJohannes Berg 	if (!(link_conf->chanreq.oper.chan->flags & IEEE80211_CHAN_RADAR)) {
35936e1b5956SJohannes Berg 		mvmvif->link[link_id]->he_ru_2mhz_block = false;
35944f58121dSIlan Peer 		return;
35954f58121dSIlan Peer 	}
35964f58121dSIlan Peer 
35976092077aSJohannes Berg 	cfg80211_bss_iter(hw->wiphy, &link_conf->chanreq.oper,
35984f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
35994f58121dSIlan Peer 			  &iter_data);
36004f58121dSIlan Peer 
36014f58121dSIlan Peer 	/*
36024f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
36034f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
36044f58121dSIlan Peer 	 */
36056e1b5956SJohannes Berg 	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
36064f58121dSIlan Peer }
36074f58121dSIlan Peer 
3608f7d6ef33SJohannes Berg static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3609f7d6ef33SJohannes Berg 					       struct ieee80211_vif *vif)
3610f7d6ef33SJohannes Berg {
3611f7d6ef33SJohannes Berg 	struct ieee80211_supported_band *sband;
3612f7d6ef33SJohannes Berg 	const struct ieee80211_sta_he_cap *he_cap;
3613f7d6ef33SJohannes Berg 
3614f7d6ef33SJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION)
3615f7d6ef33SJohannes Berg 		return;
3616f7d6ef33SJohannes Berg 
3617f7d6ef33SJohannes Berg 	if (!mvm->cca_40mhz_workaround)
3618f7d6ef33SJohannes Berg 		return;
3619f7d6ef33SJohannes Berg 
3620f7d6ef33SJohannes Berg 	/* decrement and check that we reached zero */
3621f7d6ef33SJohannes Berg 	mvm->cca_40mhz_workaround--;
3622f7d6ef33SJohannes Berg 	if (mvm->cca_40mhz_workaround)
3623f7d6ef33SJohannes Berg 		return;
3624f7d6ef33SJohannes Berg 
3625f7d6ef33SJohannes Berg 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3626f7d6ef33SJohannes Berg 
3627f7d6ef33SJohannes Berg 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3628f7d6ef33SJohannes Berg 
36291ec7291eSJohannes Berg 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3630f7d6ef33SJohannes Berg 
3631f7d6ef33SJohannes Berg 	if (he_cap) {
3632f7d6ef33SJohannes Berg 		/* we know that ours is writable */
36330301bcd5SBjoern A. Zeeb 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3634f7d6ef33SJohannes Berg 
3635f7d6ef33SJohannes Berg 		he->he_cap_elem.phy_cap_info[0] |=
3636f7d6ef33SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3637f7d6ef33SJohannes Berg 	}
3638f7d6ef33SJohannes Berg }
3639f7d6ef33SJohannes Berg 
36406d19a5ebSEmmanuel Grumbach static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
36416d19a5ebSEmmanuel Grumbach 					struct ieee80211_vif *vif,
36426d19a5ebSEmmanuel Grumbach 					struct iwl_mvm_sta *mvm_sta)
36436d19a5ebSEmmanuel Grumbach {
36446d19a5ebSEmmanuel Grumbach #if IS_ENABLED(CONFIG_IWLMEI)
36456d19a5ebSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
36466d19a5ebSEmmanuel Grumbach 	struct iwl_mei_conn_info conn_info = {
3647f276e20bSJohannes Berg 		.ssid_len = vif->cfg.ssid_len,
36486d19a5ebSEmmanuel Grumbach 	};
36496d19a5ebSEmmanuel Grumbach 
36506d19a5ebSEmmanuel Grumbach 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
36516d19a5ebSEmmanuel Grumbach 		return;
36526d19a5ebSEmmanuel Grumbach 
36536d19a5ebSEmmanuel Grumbach 	if (!mvm->mei_registered)
36546d19a5ebSEmmanuel Grumbach 		return;
36556d19a5ebSEmmanuel Grumbach 
36566c07b73eSJohannes Berg 	/* FIXME: MEI needs to be updated for MLO */
36576092077aSJohannes Berg 	if (!vif->bss_conf.chanreq.oper.chan)
36586c07b73eSJohannes Berg 		return;
36596c07b73eSJohannes Berg 
36606092077aSJohannes Berg 	conn_info.channel = vif->bss_conf.chanreq.oper.chan->hw_value;
36616c07b73eSJohannes Berg 
36626d19a5ebSEmmanuel Grumbach 	switch (mvm_sta->pairwise_cipher) {
3663e5d3a64eSAvraham Stern 	case WLAN_CIPHER_SUITE_TKIP:
3664e5d3a64eSAvraham Stern 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3665e5d3a64eSAvraham Stern 		break;
36666d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_CCMP:
36676d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
36686d19a5ebSEmmanuel Grumbach 		break;
36696d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP:
36706d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
36716d19a5ebSEmmanuel Grumbach 		break;
36726d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP_256:
36736d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
36746d19a5ebSEmmanuel Grumbach 		break;
36756d19a5ebSEmmanuel Grumbach 	case 0:
36766d19a5ebSEmmanuel Grumbach 		/* open profile */
36776d19a5ebSEmmanuel Grumbach 		break;
36786d19a5ebSEmmanuel Grumbach 	default:
36796d19a5ebSEmmanuel Grumbach 		/* cipher not supported, don't send anything to iwlmei */
36806d19a5ebSEmmanuel Grumbach 		return;
36816d19a5ebSEmmanuel Grumbach 	}
36826d19a5ebSEmmanuel Grumbach 
36836d19a5ebSEmmanuel Grumbach 	switch (mvmvif->rekey_data.akm) {
36846d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_SAE & 0xff:
36856d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
36866d19a5ebSEmmanuel Grumbach 		break;
36876d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_PSK & 0xff:
36886d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
36896d19a5ebSEmmanuel Grumbach 		break;
36906d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_8021X & 0xff:
36916d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
36926d19a5ebSEmmanuel Grumbach 		break;
36936d19a5ebSEmmanuel Grumbach 	case 0:
36946d19a5ebSEmmanuel Grumbach 		/* open profile */
36956d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
36966d19a5ebSEmmanuel Grumbach 		break;
36976d19a5ebSEmmanuel Grumbach 	default:
36986d19a5ebSEmmanuel Grumbach 		/* auth method / AKM not supported */
36996d19a5ebSEmmanuel Grumbach 		/* TODO: All the FT vesions of these? */
37006d19a5ebSEmmanuel Grumbach 		return;
37016d19a5ebSEmmanuel Grumbach 	}
37026d19a5ebSEmmanuel Grumbach 
3703f276e20bSJohannes Berg 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
37046d19a5ebSEmmanuel Grumbach 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
37056d19a5ebSEmmanuel Grumbach 
37066d19a5ebSEmmanuel Grumbach 	/* TODO: add support for collocated AP data */
37076d19a5ebSEmmanuel Grumbach 	iwl_mei_host_associated(&conn_info, NULL);
37086d19a5ebSEmmanuel Grumbach #endif
37096d19a5ebSEmmanuel Grumbach }
37106d19a5ebSEmmanuel Grumbach 
371187f7e243SMiri Korenblit static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
371287f7e243SMiri Korenblit 					    struct ieee80211_vif *vif,
371387f7e243SMiri Korenblit 					    bool force_assoc_off)
371487f7e243SMiri Korenblit {
371587f7e243SMiri Korenblit 	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
371687f7e243SMiri Korenblit }
371787f7e243SMiri Korenblit 
3718e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3719e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3720e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3721e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3722e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3723e705c121SKalle Valo {
3724a2906ea6SJohannes Berg 	static const struct iwl_mvm_sta_state_ops callbacks = {
372587f7e243SMiri Korenblit 		.add_sta = iwl_mvm_add_sta,
372687f7e243SMiri Korenblit 		.update_sta = iwl_mvm_update_sta,
372787f7e243SMiri Korenblit 		.rm_sta = iwl_mvm_rm_sta,
372887f7e243SMiri Korenblit 		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
372987f7e243SMiri Korenblit 	};
373087f7e243SMiri Korenblit 
373187f7e243SMiri Korenblit 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
373287f7e243SMiri Korenblit 					    &callbacks);
373387f7e243SMiri Korenblit }
373487f7e243SMiri Korenblit 
373557974a55SGregory Greenman /* FIXME: temporary making two assumptions in all sta handling functions:
373657974a55SGregory Greenman  *	(1) when setting sta state, the link exists and protected
373757974a55SGregory Greenman  *	(2) if a link is valid in sta then it's valid in vif (can
373857974a55SGregory Greenman  *	use same index in the link array)
373957974a55SGregory Greenman  */
3740f53be9c4SGregory Greenman static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3741f53be9c4SGregory Greenman 					   struct ieee80211_vif *vif,
374215d41834SJohannes Berg 					   struct ieee80211_sta *sta)
3743f53be9c4SGregory Greenman {
3744f53be9c4SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3745f53be9c4SGregory Greenman 	unsigned int link_id;
374657974a55SGregory Greenman 
3747f53be9c4SGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3748f53be9c4SGregory Greenman 		struct ieee80211_bss_conf *conf =
3749a705a782SJohannes Berg 			link_conf_dereference_check(vif, link_id);
3750f53be9c4SGregory Greenman 		struct ieee80211_link_sta *link_sta =
3751a705a782SJohannes Berg 			link_sta_dereference_check(sta, link_id);
3752f53be9c4SGregory Greenman 
3753f53be9c4SGregory Greenman 		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3754f53be9c4SGregory Greenman 			continue;
3755f53be9c4SGregory Greenman 
3756c45217bdSJohannes Berg 		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
375715d41834SJohannes Berg 				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3758f53be9c4SGregory Greenman 	}
3759f53be9c4SGregory Greenman }
376057974a55SGregory Greenman 
376157974a55SGregory Greenman static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
376257974a55SGregory Greenman 				      struct ieee80211_vif *vif,
376357974a55SGregory Greenman 				      struct ieee80211_sta *sta)
376457974a55SGregory Greenman {
3765207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3766207be64fSMiri Korenblit 	unsigned int link_id;
376757974a55SGregory Greenman 
376857974a55SGregory Greenman 	/* Beacon interval check - firmware will crash if the beacon
376957974a55SGregory Greenman 	 * interval is less than 16. We can't avoid connecting at all,
377057974a55SGregory Greenman 	 * so refuse the station state change, this will cause mac80211
377157974a55SGregory Greenman 	 * to abandon attempts to connect to this AP, and eventually
377257974a55SGregory Greenman 	 * wpa_s will blocklist the AP...
377357974a55SGregory Greenman 	 */
377457974a55SGregory Greenman 
3775207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
377657974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3777207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
377857974a55SGregory Greenman 
3779207be64fSMiri Korenblit 		if (!link_conf)
378057974a55SGregory Greenman 			continue;
378157974a55SGregory Greenman 
378257974a55SGregory Greenman 		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
378357974a55SGregory Greenman 			IWL_ERR(mvm,
378457974a55SGregory Greenman 				"Beacon interval %d for AP %pM is too small\n",
378557974a55SGregory Greenman 				link_conf->beacon_int, link_sta->addr);
378657974a55SGregory Greenman 			return false;
378757974a55SGregory Greenman 		}
378857974a55SGregory Greenman 
378957974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
379057974a55SGregory Greenman 	}
379157974a55SGregory Greenman 
379257974a55SGregory Greenman 	return true;
379357974a55SGregory Greenman }
379457974a55SGregory Greenman 
379557974a55SGregory Greenman static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
379657974a55SGregory Greenman 				       struct ieee80211_vif *vif,
379757974a55SGregory Greenman 				       struct ieee80211_sta *sta,
379857974a55SGregory Greenman 				       bool is_sta)
379957974a55SGregory Greenman {
380057974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3801207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3802207be64fSMiri Korenblit 	unsigned int link_id;
380357974a55SGregory Greenman 
3804207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
380557974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3806207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
380757974a55SGregory Greenman 
3808207be64fSMiri Korenblit 		if (!link_conf || !mvmvif->link[link_id])
380957974a55SGregory Greenman 			continue;
381057974a55SGregory Greenman 
381157974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
381257974a55SGregory Greenman 
381357974a55SGregory Greenman 		if (is_sta) {
3814207be64fSMiri Korenblit 			mvmvif->link[link_id]->he_ru_2mhz_block = false;
381557974a55SGregory Greenman 			if (link_sta->he_cap.has_he)
3816207be64fSMiri Korenblit 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3817207be64fSMiri Korenblit 								   link_id,
38186e1b5956SJohannes Berg 								   link_conf);
381957974a55SGregory Greenman 		}
382057974a55SGregory Greenman 	}
382157974a55SGregory Greenman }
382257974a55SGregory Greenman 
382357974a55SGregory Greenman static int
382457974a55SGregory Greenman iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
382557974a55SGregory Greenman 				   struct ieee80211_vif *vif,
382657974a55SGregory Greenman 				   struct ieee80211_sta *sta,
3827a2906ea6SJohannes Berg 				   const struct iwl_mvm_sta_state_ops *callbacks)
382857974a55SGregory Greenman {
38290c9a8f90SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3830207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
383157974a55SGregory Greenman 	unsigned int i;
383257974a55SGregory Greenman 	int ret;
383357974a55SGregory Greenman 
383457974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
383557974a55SGregory Greenman 
383657974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_STATION &&
383757974a55SGregory Greenman 	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
383857974a55SGregory Greenman 		return -EINVAL;
383957974a55SGregory Greenman 
384057974a55SGregory Greenman 	if (sta->tdls &&
384157974a55SGregory Greenman 	    (vif->p2p ||
384257974a55SGregory Greenman 	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
384357974a55SGregory Greenman 	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
384457974a55SGregory Greenman 		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
384557974a55SGregory Greenman 		return -EBUSY;
384657974a55SGregory Greenman 	}
384757974a55SGregory Greenman 
384857974a55SGregory Greenman 	ret = callbacks->add_sta(mvm, vif, sta);
384957974a55SGregory Greenman 	if (sta->tdls && ret == 0) {
385057974a55SGregory Greenman 		iwl_mvm_recalc_tdls_state(mvm, vif, true);
385157974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
385257974a55SGregory Greenman 					   NL80211_TDLS_SETUP);
385357974a55SGregory Greenman 	}
385457974a55SGregory Greenman 
3855b7198383SEmmanuel Grumbach 	if (ret)
3856b7198383SEmmanuel Grumbach 		return ret;
3857b7198383SEmmanuel Grumbach 
3858207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, i)
385957974a55SGregory Greenman 		link_sta->agg.max_rc_amsdu_len = 1;
3860207be64fSMiri Korenblit 
386157974a55SGregory Greenman 	ieee80211_sta_recalc_aggregates(sta);
386257974a55SGregory Greenman 
38630c9a8f90SJohannes Berg 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
38640c9a8f90SJohannes Berg 		mvmvif->ap_sta = sta;
38650c9a8f90SJohannes Berg 
3866d3b2c6c6SJohannes Berg 	/*
3867d3b2c6c6SJohannes Berg 	 * Initialize the rates here already - this really tells
3868d3b2c6c6SJohannes Berg 	 * the firmware only what the supported legacy rates are
3869d3b2c6c6SJohannes Berg 	 * (may be) since it's initialized already from what the
3870d3b2c6c6SJohannes Berg 	 * AP advertised in the beacon/probe response. This will
3871d3b2c6c6SJohannes Berg 	 * allow the firmware to send auth/assoc frames with one
3872d3b2c6c6SJohannes Berg 	 * of the supported rates already, rather than having to
3873d3b2c6c6SJohannes Berg 	 * use a mandatory rate.
3874d3b2c6c6SJohannes Berg 	 * If we're the AP, we'll just assume mandatory rates at
3875d3b2c6c6SJohannes Berg 	 * this point, but we know nothing about the STA anyway.
3876d3b2c6c6SJohannes Berg 	 */
3877d3b2c6c6SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3878d3b2c6c6SJohannes Berg 
387957974a55SGregory Greenman 	return 0;
388057974a55SGregory Greenman }
388157974a55SGregory Greenman 
388257974a55SGregory Greenman static int
388357974a55SGregory Greenman iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
388457974a55SGregory Greenman 				struct iwl_mvm *mvm,
388557974a55SGregory Greenman 				struct ieee80211_vif *vif,
388657974a55SGregory Greenman 				struct ieee80211_sta *sta,
3887a2906ea6SJohannes Berg 				const struct iwl_mvm_sta_state_ops *callbacks)
388857974a55SGregory Greenman {
388957974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
389057974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3891207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3892207be64fSMiri Korenblit 	unsigned int link_id;
389357974a55SGregory Greenman 
389457974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
389557974a55SGregory Greenman 
389657974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_AP) {
389757974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
389857974a55SGregory Greenman 		mvmvif->ap_assoc_sta_count++;
389957974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
390057974a55SGregory Greenman 
390157974a55SGregory Greenman 		/* since the below is not for MLD API, it's ok to use
390257974a55SGregory Greenman 		 * the default bss_conf
390357974a55SGregory Greenman 		 */
390457974a55SGregory Greenman 		if (!mvm->mld_api_is_used &&
390583f57c93SJohannes Berg 		    (vif->bss_conf.he_support &&
390683f57c93SJohannes Berg 		     !iwlwifi_mod_params.disable_11ax))
390757974a55SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
390857974a55SGregory Greenman 	} else if (vif->type == NL80211_IFTYPE_STATION) {
390957974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
391057974a55SGregory Greenman 
391157974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
391257974a55SGregory Greenman 
391357974a55SGregory Greenman 		if (!mvm->mld_api_is_used)
391457974a55SGregory Greenman 			goto out;
391557974a55SGregory Greenman 
3916207be64fSMiri Korenblit 		for_each_sta_active_link(vif, sta, link_sta, link_id) {
391757974a55SGregory Greenman 			struct ieee80211_bss_conf *link_conf =
3918207be64fSMiri Korenblit 				link_conf_dereference_protected(vif, link_id);
391957974a55SGregory Greenman 
392057974a55SGregory Greenman 			if (WARN_ON(!link_conf))
392157974a55SGregory Greenman 				return -EINVAL;
3922207be64fSMiri Korenblit 			if (!mvmvif->link[link_id])
3923f14ad95aSJohannes Berg 				continue;
392457974a55SGregory Greenman 
392557974a55SGregory Greenman 			iwl_mvm_link_changed(mvm, vif, link_conf,
392657974a55SGregory Greenman 					     LINK_CONTEXT_MODIFY_ALL &
392757974a55SGregory Greenman 					     ~LINK_CONTEXT_MODIFY_ACTIVE,
392857974a55SGregory Greenman 					     true);
392957974a55SGregory Greenman 		}
393057974a55SGregory Greenman 	}
393157974a55SGregory Greenman 
393257974a55SGregory Greenman out:
393315d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
393457974a55SGregory Greenman 
393557974a55SGregory Greenman 	return callbacks->update_sta(mvm, vif, sta);
393657974a55SGregory Greenman }
393757974a55SGregory Greenman 
393857974a55SGregory Greenman static int
393957974a55SGregory Greenman iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
394057974a55SGregory Greenman 				      struct ieee80211_vif *vif,
394157974a55SGregory Greenman 				      struct ieee80211_sta *sta,
3942a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
394357974a55SGregory Greenman {
394457974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
394557974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
394657974a55SGregory Greenman 
394757974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
394857974a55SGregory Greenman 
394957974a55SGregory Greenman 	/* we don't support TDLS during DCM */
395057974a55SGregory Greenman 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
395157974a55SGregory Greenman 		iwl_mvm_teardown_tdls_peers(mvm);
395257974a55SGregory Greenman 
395357974a55SGregory Greenman 	if (sta->tdls) {
395457974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
395557974a55SGregory Greenman 					   NL80211_TDLS_ENABLE_LINK);
395657974a55SGregory Greenman 	} else {
395757974a55SGregory Greenman 		/* enable beacon filtering */
395887f5b5f2SJohannes Berg 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
395957974a55SGregory Greenman 
396057974a55SGregory Greenman 		mvmvif->authorized = 1;
3961950a3f5fSJohannes Berg 
3962950a3f5fSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
39632f33561eSMiri Korenblit 			mvmvif->link_selection_res = vif->active_links;
396407bf5297SMiri Korenblit 			mvmvif->link_selection_primary =
396507bf5297SMiri Korenblit 				vif->active_links ? __ffs(vif->active_links) : 0;
3966950a3f5fSJohannes Berg 		}
396757974a55SGregory Greenman 
396857974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
396957974a55SGregory Greenman 		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
3970b9be67fbSIlan Peer 
3971492bc4e4SMiri Korenblit 		memset(&mvmvif->last_esr_exit, 0,
3972492bc4e4SMiri Korenblit 		       sizeof(mvmvif->last_esr_exit));
3973492bc4e4SMiri Korenblit 
3974ec0d43d2SMiri Korenblit 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT, 0);
3975ec0d43d2SMiri Korenblit 
3976e619ad55SMiri Korenblit 		/* Block until FW notif will arrive */
3977e619ad55SMiri Korenblit 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_FW, 0);
3978e619ad55SMiri Korenblit 
3979b9be67fbSIlan Peer 		/* when client is authorized (AP station marked as such),
398007bf5297SMiri Korenblit 		 * try to enable the best link(s).
3981b9be67fbSIlan Peer 		 */
3982b9be67fbSIlan Peer 		if (vif->type == NL80211_IFTYPE_STATION &&
3983b9be67fbSIlan Peer 		    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
398407bf5297SMiri Korenblit 			iwl_mvm_select_links(mvm, vif);
398557974a55SGregory Greenman 	}
398657974a55SGregory Greenman 
398715d41834SJohannes Berg 	mvm_sta->authorized = true;
398815d41834SJohannes Berg 
39895a86dcb4SAvraham Stern 	/* MFP is set by default before the station is authorized.
39905a86dcb4SAvraham Stern 	 * Clear it here in case it's not used.
39915a86dcb4SAvraham Stern 	 */
39920fcdf55fSEmmanuel Grumbach 	if (!sta->mfp) {
39930fcdf55fSEmmanuel Grumbach 		int ret = callbacks->update_sta(mvm, vif, sta);
39940fcdf55fSEmmanuel Grumbach 
39950fcdf55fSEmmanuel Grumbach 		if (ret)
39960fcdf55fSEmmanuel Grumbach 			return ret;
39970fcdf55fSEmmanuel Grumbach 	}
39980fcdf55fSEmmanuel Grumbach 
39990fcdf55fSEmmanuel Grumbach 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
40005a86dcb4SAvraham Stern 
400157974a55SGregory Greenman 	return 0;
400257974a55SGregory Greenman }
400357974a55SGregory Greenman 
400457974a55SGregory Greenman static int
400557974a55SGregory Greenman iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
400657974a55SGregory Greenman 				      struct ieee80211_vif *vif,
400757974a55SGregory Greenman 				      struct ieee80211_sta *sta,
4008a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
400957974a55SGregory Greenman {
401057974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
401115d41834SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
401257974a55SGregory Greenman 
401357974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
401457974a55SGregory Greenman 
401515d41834SJohannes Berg 	mvmsta->authorized = false;
401615d41834SJohannes Berg 
401757974a55SGregory Greenman 	/* once we move into assoc state, need to update rate scale to
401857974a55SGregory Greenman 	 * disable using wide bandwidth
401957974a55SGregory Greenman 	 */
402015d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
402157974a55SGregory Greenman 
402257974a55SGregory Greenman 	if (!sta->tdls) {
402357974a55SGregory Greenman 		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
402457974a55SGregory Greenman 		 * yet to avoid sending high prio again for a little
402557974a55SGregory Greenman 		 * time.
402657974a55SGregory Greenman 		 */
402757974a55SGregory Greenman 		mvmvif->authorized = 0;
402807bf5297SMiri Korenblit 		mvmvif->link_selection_res = 0;
402957974a55SGregory Greenman 
403057974a55SGregory Greenman 		/* disable beacon filtering */
403187f5b5f2SJohannes Berg 		iwl_mvm_disable_beacon_filter(mvm, vif);
4032492bc4e4SMiri Korenblit 
4033492bc4e4SMiri Korenblit 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4034492bc4e4SMiri Korenblit 					  &mvmvif->prevent_esr_done_wk);
40359c28ead0SMiri Korenblit 
40362f33561eSMiri Korenblit 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
40372f33561eSMiri Korenblit 					  &mvmvif->mlo_int_scan_wk);
40382f33561eSMiri Korenblit 
4039ec0d43d2SMiri Korenblit 		wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
4040ec0d43d2SMiri Korenblit 
40419c28ead0SMiri Korenblit 		/* No need for the periodic statistics anymore */
40429c28ead0SMiri Korenblit 		if (ieee80211_vif_is_mld(vif) && mvmvif->esr_active)
40439c28ead0SMiri Korenblit 			iwl_mvm_request_periodic_system_statistics(mvm, false);
404457974a55SGregory Greenman 	}
404557974a55SGregory Greenman 
404657974a55SGregory Greenman 	return 0;
404757974a55SGregory Greenman }
404857974a55SGregory Greenman 
404987f7e243SMiri Korenblit /* Common part for MLD and non-MLD modes */
405087f7e243SMiri Korenblit int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
405187f7e243SMiri Korenblit 				 struct ieee80211_vif *vif,
405287f7e243SMiri Korenblit 				 struct ieee80211_sta *sta,
405387f7e243SMiri Korenblit 				 enum ieee80211_sta_state old_state,
405487f7e243SMiri Korenblit 				 enum ieee80211_sta_state new_state,
4055a2906ea6SJohannes Berg 				 const struct iwl_mvm_sta_state_ops *callbacks)
405687f7e243SMiri Korenblit {
4057e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4058e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
40596ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4060de107600SJohannes Berg 	struct ieee80211_link_sta *link_sta;
406157974a55SGregory Greenman 	unsigned int link_id;
4062e705c121SKalle Valo 	int ret;
4063e705c121SKalle Valo 
4064e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
4065e705c121SKalle Valo 			   sta->addr, old_state, new_state);
4066e705c121SKalle Valo 
406724afba76SLiad Kaufman 	/*
406824afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
406924afba76SLiad Kaufman 	 *
407024afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
407124afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
407224afba76SLiad Kaufman 	 * the queues.
407324afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
407424afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
407524afba76SLiad Kaufman 	 * of.
407624afba76SLiad Kaufman 	 *
407724afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
407824afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
407924afba76SLiad Kaufman 	 */
408024afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
4081c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
408224afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
408324afba76SLiad Kaufman 
408424afba76SLiad Kaufman 		/*
408524afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
408624afba76SLiad Kaufman 		 * worker will already remove it if it was on
408724afba76SLiad Kaufman 		 */
4088f7d6ef33SJohannes Berg 
4089f7d6ef33SJohannes Berg 		/*
4090f7d6ef33SJohannes Berg 		 * Additionally, reset the 40 MHz capability if we disconnected
4091f7d6ef33SJohannes Berg 		 * from the AP now.
4092f7d6ef33SJohannes Berg 		 */
4093f7d6ef33SJohannes Berg 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
4094783336b0SJohannes Berg 
4095783336b0SJohannes Berg 		/* Also free dup data just in case any assertions below fail */
4096783336b0SJohannes Berg 		kfree(mvm_sta->dup_data);
409724afba76SLiad Kaufman 	}
409824afba76SLiad Kaufman 
4099e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
410057974a55SGregory Greenman 
4101828c79d9SEmmanuel Grumbach 	/* this would be a mac80211 bug ... but don't crash, unless we had a
4102828c79d9SEmmanuel Grumbach 	 * firmware crash while we were activating a link, in which case it is
4103828c79d9SEmmanuel Grumbach 	 * legit to have phy_ctxt = NULL. Don't bother not to WARN if we are in
4104828c79d9SEmmanuel Grumbach 	 * recovery flow since we spit tons of error messages anyway.
4105828c79d9SEmmanuel Grumbach 	 */
4106de107600SJohannes Berg 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
41073d6d21b2SJohannes Berg 		if (WARN_ON_ONCE(!mvmvif->link[link_id] ||
41083d6d21b2SJohannes Berg 				 !mvmvif->link[link_id]->phy_ctxt)) {
410957974a55SGregory Greenman 			mutex_unlock(&mvm->mutex);
411057974a55SGregory Greenman 			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
411157974a55SGregory Greenman 					&mvm->status) ? 0 : -EINVAL;
411257974a55SGregory Greenman 		}
411357974a55SGregory Greenman 	}
411457974a55SGregory Greenman 
41156ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
4116d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
41176ea29ce5SJohannes Berg 
4118e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
4119e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
412057974a55SGregory Greenman 		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
412157974a55SGregory Greenman 							 callbacks);
412257974a55SGregory Greenman 		if (ret < 0)
4123e705c121SKalle Valo 			goto out_unlock;
4124e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
4125e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
4126e705c121SKalle Valo 		/*
4127e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
4128e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
4129e705c121SKalle Valo 		 */
4130e705c121SKalle Valo 		mvm->last_ebs_successful = true;
4131e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
4132e705c121SKalle Valo 		ret = 0;
4133e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
4134e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
413557974a55SGregory Greenman 		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
413657974a55SGregory Greenman 						      callbacks);
4137e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
4138e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
413957974a55SGregory Greenman 		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
414057974a55SGregory Greenman 							    callbacks);
4141e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
4142e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
414357974a55SGregory Greenman 		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
414457974a55SGregory Greenman 							    callbacks);
4145e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
4146e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
41478be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
41488be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
414987f7e243SMiri Korenblit 			callbacks->mac_ctxt_changed(mvm, vif, false);
4150d5d8ee52SEmmanuel Grumbach 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
4151cf7a7457SJohannes Berg 			iwl_mvm_stop_session_protection(mvm, vif);
4152e705c121SKalle Valo 		ret = 0;
4153e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
4154e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
4155e705c121SKalle Valo 		ret = 0;
4156e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
4157e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
41580c9a8f90SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
4159d5d8ee52SEmmanuel Grumbach 			iwl_mvm_stop_session_protection(mvm, vif);
41600c9a8f90SJohannes Berg 			mvmvif->ap_sta = NULL;
41610c9a8f90SJohannes Berg 		}
416287f7e243SMiri Korenblit 		ret = callbacks->rm_sta(mvm, vif, sta);
41631e8f1329SGolan Ben-Ami 		if (sta->tdls) {
4164e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
41651e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
41661e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
41671e8f1329SGolan Ben-Ami 		}
416834a880d8SLiad Kaufman 
416944135b7cSIlan Peer 		if (unlikely(ret &&
417044135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
417144135b7cSIlan Peer 				      &mvm->status)))
417244135b7cSIlan Peer 			ret = 0;
4173e705c121SKalle Valo 	} else {
4174e705c121SKalle Valo 		ret = -EIO;
4175e705c121SKalle Valo 	}
4176e705c121SKalle Valo  out_unlock:
4177e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4178e705c121SKalle Valo 
4179e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
4180e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
4181e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
4182e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4183e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
4184e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
4185e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4186e705c121SKalle Valo 	}
4187e705c121SKalle Valo 
4188e705c121SKalle Valo 	return ret;
4189e705c121SKalle Valo }
4190e705c121SKalle Valo 
4191cbce62a3SMiri Korenblit int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4192e705c121SKalle Valo {
4193e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4194e705c121SKalle Valo 
4195e705c121SKalle Valo 	mvm->rts_threshold = value;
4196e705c121SKalle Valo 
4197e705c121SKalle Valo 	return 0;
4198e705c121SKalle Valo }
4199e705c121SKalle Valo 
4200cbce62a3SMiri Korenblit void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4201e705c121SKalle Valo 			   struct ieee80211_sta *sta, u32 changed)
4202e705c121SKalle Valo {
4203e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4204dcfe3b10SJohannes Berg 
4205dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
4206dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
4207dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
420815d41834SJohannes Berg 		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4209e705c121SKalle Valo 
4210e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
4211e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
4212e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
4213e705c121SKalle Valo }
4214e705c121SKalle Valo 
4215e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
4216b3e2130bSJohannes Berg 			       struct ieee80211_vif *vif,
4217b3e2130bSJohannes Berg 			       unsigned int link_id, u16 ac,
4218e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
4219e705c121SKalle Valo {
4220e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4221e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4222e705c121SKalle Valo 
4223650cadb7SGregory Greenman 	mvmvif->deflink.queue_params[ac] = *params;
4224e705c121SKalle Valo 
4225e705c121SKalle Valo 	/*
4226e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
4227e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
4228e705c121SKalle Valo 	 */
4229e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
42303f312651SJohannes Berg 		guard(mvm)(mvm);
42313f312651SJohannes Berg 		return iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4232e705c121SKalle Valo 	}
4233e705c121SKalle Valo 	return 0;
4234e705c121SKalle Valo }
4235e705c121SKalle Valo 
4236cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4237d4e36e55SIlan Peer 				struct ieee80211_vif *vif,
423815fae341SJohannes Berg 				struct ieee80211_prep_tx_info *info)
4239e705c121SKalle Valo {
4240e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4241d4e36e55SIlan Peer 
42423f312651SJohannes Berg 	guard(mvm)(mvm);
424323673041SMiri Korenblit 	iwl_mvm_protect_assoc(mvm, vif, info->duration, info->link_id);
4244e705c121SKalle Valo }
4245e705c121SKalle Valo 
4246cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
42476b1259d1SJohannes Berg 				 struct ieee80211_vif *vif,
42486b1259d1SJohannes Berg 				 struct ieee80211_prep_tx_info *info)
42496b1259d1SJohannes Berg {
42506b1259d1SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
42516b1259d1SJohannes Berg 
42526b1259d1SJohannes Berg 	/* for successful cases (auth/assoc), don't cancel session protection */
42536b1259d1SJohannes Berg 	if (info->success)
42546b1259d1SJohannes Berg 		return;
42556b1259d1SJohannes Berg 
42563f312651SJohannes Berg 	guard(mvm)(mvm);
42576b1259d1SJohannes Berg 	iwl_mvm_stop_session_protection(mvm, vif);
42586b1259d1SJohannes Berg }
42596b1259d1SJohannes Berg 
4260cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4261e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4262e705c121SKalle Valo 				 struct cfg80211_sched_scan_request *req,
4263e705c121SKalle Valo 				 struct ieee80211_scan_ies *ies)
4264e705c121SKalle Valo {
4265e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4266e705c121SKalle Valo 
42673f312651SJohannes Berg 	guard(mvm)(mvm);
4268e705c121SKalle Valo 
42693f312651SJohannes Berg 	if (!vif->cfg.idle)
42703f312651SJohannes Berg 		return -EBUSY;
4271e705c121SKalle Valo 
42723f312651SJohannes Berg 	return iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4273e705c121SKalle Valo }
4274e705c121SKalle Valo 
4275cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4276e705c121SKalle Valo 				struct ieee80211_vif *vif)
4277e705c121SKalle Valo {
4278e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4279e705c121SKalle Valo 	int ret;
4280e705c121SKalle Valo 
4281e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4282e705c121SKalle Valo 
4283e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
4284e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
4285e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
4286e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
4287e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4288e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
4289e705c121SKalle Valo 	 * not running.
4290e705c121SKalle Valo 	*/
4291e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4292e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
4293e705c121SKalle Valo 		return 0;
4294e705c121SKalle Valo 	}
4295e705c121SKalle Valo 
4296e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4297e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4298e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
4299e705c121SKalle Valo 
4300e705c121SKalle Valo 	return ret;
4301e705c121SKalle Valo }
4302e705c121SKalle Valo 
43036569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4304e705c121SKalle Valo 				 enum set_key_cmd cmd,
4305e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4306e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4307e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
4308e705c121SKalle Valo {
4309c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4310e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
431146c7b05aSEmmanuel Grumbach 	struct iwl_mvm_sta *mvmsta = NULL;
4312d066a530SJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn = NULL;
4313f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
43145c75a208SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
43155c75a208SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4316c56e00a3SJohannes Berg 	int ret, i;
43174615fd15SEmmanuel Grumbach 	u8 key_offset;
4318e705c121SKalle Valo 
431946c7b05aSEmmanuel Grumbach 	if (sta)
43206d19a5ebSEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
43216d19a5ebSEmmanuel Grumbach 
4322e705c121SKalle Valo 	switch (key->cipher) {
4323e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
4324286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
4325e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
43261ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
43277f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
43287f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
43297f768ad5SDavid Spinadel 		} else {
43307f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
43317f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
43327f768ad5SDavid Spinadel 		}
4333e705c121SKalle Valo 		break;
4334e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
43352a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
43362a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
433785aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
4338e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4339e705c121SKalle Valo 		break;
4340e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
43418e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
43428e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4343e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4344e705c121SKalle Valo 		break;
4345e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
4346e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
4347475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
4348e705c121SKalle Valo 			break;
4349475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
4350475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
4351475c6bdeSJohannes Berg 		/* support HW crypto on TX */
4352475c6bdeSJohannes Berg 		return 0;
4353e705c121SKalle Valo 	default:
4354e705c121SKalle Valo 		return -EOPNOTSUPP;
4355e705c121SKalle Valo 	}
4356e705c121SKalle Valo 
4357e705c121SKalle Valo 	switch (cmd) {
4358e705c121SKalle Valo 	case SET_KEY:
4359a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4360a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4361b1fdc250SJohannes Berg 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4362b1fdc250SJohannes Berg 					   key);
4363b1fdc250SJohannes Berg 
4364e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4365e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4366e705c121SKalle Valo 			/*
4367e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
4368e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
4369e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
4370f05d1e04SJohannes Berg 			 * CMAC/GMAC in AP/IBSS modes must be done in software
4371f05d1e04SJohannes Berg 			 * on older NICs.
4372a5de7de7SJohannes Berg 			 *
4373a5de7de7SJohannes Berg 			 * Except, of course, beacon protection - it must be
4374f05d1e04SJohannes Berg 			 * offloaded since we just set a beacon template, and
4375f05d1e04SJohannes Berg 			 * then we must also offload the IGTK (not just BIGTK)
4376f05d1e04SJohannes Berg 			 * for firmware reasons.
4377f05d1e04SJohannes Berg 			 *
4378f05d1e04SJohannes Berg 			 * So just check for beacon protection - if we don't
4379f05d1e04SJohannes Berg 			 * have it we cannot get here with keyidx >= 6, and
4380f05d1e04SJohannes Berg 			 * if we do have it we need to send the key to FW in
4381f05d1e04SJohannes Berg 			 * all cases (CMAC/GMAC).
4382e705c121SKalle Valo 			 */
4383f05d1e04SJohannes Berg 			if (!wiphy_ext_feature_isset(hw->wiphy,
4384f05d1e04SJohannes Berg 						     NL80211_EXT_FEATURE_BEACON_PROTECTION) &&
4385a5de7de7SJohannes Berg 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
43868e160ab8SAyala Beker 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4387a5de7de7SJohannes Berg 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
438881279c49SJohannes Berg 				ret = -EOPNOTSUPP;
4389a1c2ff30SAndrei Otcheretianski 				break;
4390a1c2ff30SAndrei Otcheretianski 			}
439185aeb58cSDavid Spinadel 
439285aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
439385aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
439485aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
4395e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
4396a1c2ff30SAndrei Otcheretianski 				ret = 0;
4397e705c121SKalle Valo 				break;
4398e705c121SKalle Valo 			}
4399c56e00a3SJohannes Berg 
4400c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
4401c56e00a3SJohannes Berg 				for (i = 0;
4402c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4403c56e00a3SJohannes Berg 				     i++) {
4404c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
4405c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
4406c56e00a3SJohannes Berg 						break;
4407c56e00a3SJohannes Berg 					}
4408c56e00a3SJohannes Berg 				}
4409c56e00a3SJohannes Berg 
4410c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4411c56e00a3SJohannes Berg 					ret = -ENOSPC;
4412a1c2ff30SAndrei Otcheretianski 				else
4413a1c2ff30SAndrei Otcheretianski 					ret = 0;
4414c56e00a3SJohannes Berg 
4415c56e00a3SJohannes Berg 				break;
4416c56e00a3SJohannes Berg 			}
441785aeb58cSDavid Spinadel 		}
4418e705c121SKalle Valo 
4419e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
4420e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
4421e705c121SKalle Valo 		 */
4422e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4423e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4424e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
4425e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
4426e705c121SKalle Valo 			ret = 0;
4427e705c121SKalle Valo 			break;
4428e705c121SKalle Valo 		}
4429e705c121SKalle Valo 
4430f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
443146c7b05aSEmmanuel Grumbach 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4432f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4433f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
44342a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
44352a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4436f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
4437f5e28eacSJohannes Berg 			int tid, q;
4438f5e28eacSJohannes Berg 
4439f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4440acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4441acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
4442f5e28eacSJohannes Berg 					 GFP_KERNEL);
4443f5e28eacSJohannes Berg 			if (!ptk_pn) {
4444f5e28eacSJohannes Berg 				ret = -ENOMEM;
4445f5e28eacSJohannes Berg 				break;
4446f5e28eacSJohannes Berg 			}
4447f5e28eacSJohannes Berg 
4448f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4449f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
4450f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
4451f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
4452f5e28eacSJohannes Berg 					       seq.ccmp.pn,
4453f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
4454f5e28eacSJohannes Berg 			}
4455f5e28eacSJohannes Berg 
4456f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4457f5e28eacSJohannes Berg 		}
4458f5e28eacSJohannes Berg 
44594615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
44604615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
44614615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
44624615fd15SEmmanuel Grumbach 		else
44634615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
44644615fd15SEmmanuel Grumbach 
446546c7b05aSEmmanuel Grumbach 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
44666d19a5ebSEmmanuel Grumbach 			mvmsta->pairwise_cipher = key->cipher;
44676d19a5ebSEmmanuel Grumbach 
4468a5de7de7SJohannes Berg 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4469a5de7de7SJohannes Berg 				   sta ? sta->addr : NULL, key->keyidx);
44705c75a208SJohannes Berg 
44715c75a208SJohannes Berg 		if (sec_key_ver)
44725c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
44735c75a208SJohannes Berg 		else
44744615fd15SEmmanuel Grumbach 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
44755c75a208SJohannes Berg 
4476e705c121SKalle Valo 		if (ret) {
4477e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
4478475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
4479d066a530SJohannes Berg 			if (ptk_pn) {
4480d066a530SJohannes Berg 				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4481d066a530SJohannes Berg 				kfree(ptk_pn);
4482d066a530SJohannes Berg 			}
4483e705c121SKalle Valo 			/*
4484e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
4485475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
4486475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
4487475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
4488e705c121SKalle Valo 			 */
4489475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
4490475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
4491475c6bdeSJohannes Berg 			else
4492e705c121SKalle Valo 				ret = 0;
4493e705c121SKalle Valo 		}
4494e705c121SKalle Valo 
4495e705c121SKalle Valo 		break;
4496e705c121SKalle Valo 	case DISABLE_KEY:
4497a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4498a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4499b1fdc250SJohannes Berg 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4500b1fdc250SJohannes Berg 					 NULL);
4501b1fdc250SJohannes Berg 
4502c56e00a3SJohannes Berg 		ret = -ENOENT;
4503c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4504c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
4505c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
4506c56e00a3SJohannes Berg 				ret = 0;
4507c56e00a3SJohannes Berg 			}
4508c56e00a3SJohannes Berg 		}
4509c56e00a3SJohannes Berg 
4510c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
4511c56e00a3SJohannes Berg 		if (ret == 0)
4512c56e00a3SJohannes Berg 			break;
4513c56e00a3SJohannes Berg 
4514e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4515e705c121SKalle Valo 			ret = 0;
4516e705c121SKalle Valo 			break;
4517e705c121SKalle Valo 		}
4518e705c121SKalle Valo 
451946c7b05aSEmmanuel Grumbach 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4520f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4521f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
45222a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
45232a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4524f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
4525f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
4526f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
4527f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4528f5e28eacSJohannes Berg 			if (ptk_pn)
4529f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
4530f5e28eacSJohannes Berg 		}
4531f5e28eacSJohannes Berg 
4532e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
45335c75a208SJohannes Berg 		if (sec_key_ver)
45345c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
45355c75a208SJohannes Berg 		else
4536e705c121SKalle Valo 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4537e705c121SKalle Valo 		break;
4538e705c121SKalle Valo 	default:
4539e705c121SKalle Valo 		ret = -EINVAL;
4540e705c121SKalle Valo 	}
4541e705c121SKalle Valo 
45426569e7d3SJohannes Berg 	return ret;
45436569e7d3SJohannes Berg }
45446569e7d3SJohannes Berg 
4545cbce62a3SMiri Korenblit int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4546cbce62a3SMiri Korenblit 			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
45476569e7d3SJohannes Berg 			struct ieee80211_key_conf *key)
45486569e7d3SJohannes Berg {
45496569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
45506569e7d3SJohannes Berg 
45513f312651SJohannes Berg 	guard(mvm)(mvm);
45523f312651SJohannes Berg 	return __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4553e705c121SKalle Valo }
4554e705c121SKalle Valo 
4555cbce62a3SMiri Korenblit void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4556e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4557e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
4558e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4559e705c121SKalle Valo 				 u32 iv32, u16 *phase1key)
4560e705c121SKalle Valo {
4561e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4562e705c121SKalle Valo 
4563e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4564e705c121SKalle Valo 		return;
4565e705c121SKalle Valo 
4566e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4567e705c121SKalle Valo }
4568e705c121SKalle Valo 
4569e705c121SKalle Valo 
4570e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4571e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
4572e705c121SKalle Valo {
4573e705c121SKalle Valo 	struct iwl_mvm *mvm =
4574e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
4575e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
4576e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
4577e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
4578e705c121SKalle Valo 
4579e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4580e705c121SKalle Valo 		return true;
4581e705c121SKalle Valo 
4582e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4583e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4584e705c121SKalle Valo 		return true;
4585e705c121SKalle Valo 	}
4586e705c121SKalle Valo 
4587e705c121SKalle Valo 	resp = (void *)pkt->data;
4588e705c121SKalle Valo 
4589e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
4590b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4591e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
4592e705c121SKalle Valo 
4593e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4594e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4595e705c121SKalle Valo 		     te_data->uid);
4596e705c121SKalle Valo 
4597e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4598e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4599e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4600e705c121SKalle Valo 
4601e705c121SKalle Valo 	return true;
4602e705c121SKalle Valo }
4603e705c121SKalle Valo 
4604e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4605e705c121SKalle Valo 				    struct ieee80211_channel *channel,
4606e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4607e705c121SKalle Valo 				    int duration)
4608e705c121SKalle Valo {
4609afc1e3b4SAvraham Stern 	int res;
4610e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4611e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4612e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4613e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
4614dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
4615e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
4616e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4617e705c121SKalle Valo 		.id_and_color =
4618e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4619e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4620e705c121SKalle Valo 	};
462157e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
462257e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
462357e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
462457e861d9SDavid Spinadel 
462557e861d9SDavid Spinadel 	/* Set the channel info data */
462657e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
46273717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
46287ac87575SJohannes Berg 			      IWL_PHY_CHANNEL_MODE20,
462957e861d9SDavid Spinadel 			      0);
463057e861d9SDavid Spinadel 
463157e861d9SDavid Spinadel 	/* Set the time and duration */
4632afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4633e705c121SKalle Valo 
463467ac248eSShaul Triebitz 	iwl_mvm_roc_duration_and_delay(vif, duration, &req_dur, &delay);
463557e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
463657e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
4637dc28e12fSMatti Gottlieb 
4638dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
4639903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
4640903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
4641903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
464267ac248eSShaul Triebitz 		     "\t(requested = %ums, max_delay = %ums)\n",
464367ac248eSShaul Triebitz 		     duration, delay);
4644903b3f9bSEmmanuel Grumbach 
4645e705c121SKalle Valo 	/* Set the node address */
464657e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4647e705c121SKalle Valo 
4648e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4649e705c121SKalle Valo 
4650e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4651e705c121SKalle Valo 
4652e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4653e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4654e705c121SKalle Valo 		return -EIO;
4655e705c121SKalle Valo 	}
4656e705c121SKalle Valo 
4657e705c121SKalle Valo 	te_data->vif = vif;
4658e705c121SKalle Valo 	te_data->duration = duration;
4659e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
4660e705c121SKalle Valo 
4661e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4662e705c121SKalle Valo 
4663e705c121SKalle Valo 	/*
4664e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
4665e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
4666e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
4667e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4668e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
4669e705c121SKalle Valo 	 * time another notification (that the time event started)
4670e705c121SKalle Valo 	 * might already be processed unsuccessfully.
4671e705c121SKalle Valo 	 */
4672e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4673e705c121SKalle Valo 				   time_event_response,
4674e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
4675e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
4676e705c121SKalle Valo 
467757e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4678e705c121SKalle Valo 				   &aux_roc_req);
4679e705c121SKalle Valo 
4680e705c121SKalle Valo 	if (res) {
4681e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4682e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4683e705c121SKalle Valo 		goto out_clear_te;
4684e705c121SKalle Valo 	}
4685e705c121SKalle Valo 
4686e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4687e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4688e705c121SKalle Valo 	/* should never fail */
4689e705c121SKalle Valo 	WARN_ON_ONCE(res);
4690e705c121SKalle Valo 
4691e705c121SKalle Valo 	if (res) {
4692e705c121SKalle Valo  out_clear_te:
4693e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
4694e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
4695e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4696e705c121SKalle Valo 	}
4697e705c121SKalle Valo 
4698e705c121SKalle Valo 	return res;
4699e705c121SKalle Valo }
4700e705c121SKalle Valo 
4701feebebaeSMiri Korenblit static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4702feebebaeSMiri Korenblit {
4703feebebaeSMiri Korenblit 	int ret = 0;
4704feebebaeSMiri Korenblit 
4705feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4706feebebaeSMiri Korenblit 
4707feebebaeSMiri Korenblit 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4708feebebaeSMiri Korenblit 			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4709feebebaeSMiri Korenblit 		IWL_ERR(mvm, "hotspot not supported\n");
4710feebebaeSMiri Korenblit 		return -EINVAL;
4711feebebaeSMiri Korenblit 	}
4712feebebaeSMiri Korenblit 
47131724fc78SEmmanuel Grumbach 	if (iwl_mvm_has_new_station_api(mvm->fw)) {
4714feebebaeSMiri Korenblit 		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4715feebebaeSMiri Korenblit 		WARN(ret, "Failed to allocate aux station");
4716feebebaeSMiri Korenblit 	}
4717feebebaeSMiri Korenblit 
4718feebebaeSMiri Korenblit 	return ret;
4719feebebaeSMiri Korenblit }
4720feebebaeSMiri Korenblit 
472184ef7cbeSIlan Peer static int iwl_mvm_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4722feebebaeSMiri Korenblit {
472384ef7cbeSIlan Peer 	int ret;
4724feebebaeSMiri Korenblit 
4725feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4726feebebaeSMiri Korenblit 
4727feebebaeSMiri Korenblit 	ret = iwl_mvm_binding_add_vif(mvm, vif);
472884ef7cbeSIlan Peer 	if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4729feebebaeSMiri Korenblit 		return ret;
473084ef7cbeSIlan Peer 
473184ef7cbeSIlan Peer 	/* The station and queue allocation must be done only after the binding
473284ef7cbeSIlan Peer 	 * is done, as otherwise the FW might incorrectly configure its state.
473384ef7cbeSIlan Peer 	 */
473484ef7cbeSIlan Peer 	return iwl_mvm_add_p2p_bcast_sta(mvm, vif);
4735feebebaeSMiri Korenblit }
4736feebebaeSMiri Korenblit 
4737e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
4738e705c121SKalle Valo 		       struct ieee80211_vif *vif,
4739e705c121SKalle Valo 		       struct ieee80211_channel *channel,
4740e705c121SKalle Valo 		       int duration,
4741e705c121SKalle Valo 		       enum ieee80211_roc_type type)
4742e705c121SKalle Valo {
4743a2906ea6SJohannes Berg 	static const struct iwl_mvm_roc_ops ops = {
4744fe8b2ad3SMiri Korenblit 		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
474584ef7cbeSIlan Peer 		.link = iwl_mvm_roc_link,
4746fe8b2ad3SMiri Korenblit 	};
4747fe8b2ad3SMiri Korenblit 
4748fe8b2ad3SMiri Korenblit 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4749fe8b2ad3SMiri Korenblit }
4750fe8b2ad3SMiri Korenblit 
475167ac248eSShaul Triebitz static int iwl_mvm_roc_station(struct iwl_mvm *mvm,
475267ac248eSShaul Triebitz 			       struct ieee80211_channel *channel,
475367ac248eSShaul Triebitz 			       struct ieee80211_vif *vif,
475467ac248eSShaul Triebitz 			       int duration)
475567ac248eSShaul Triebitz {
475667ac248eSShaul Triebitz 	int ret;
475767ac248eSShaul Triebitz 	u32 cmd_id = WIDE_ID(MAC_CONF_GROUP, ROC_CMD);
475867ac248eSShaul Triebitz 	u8 fw_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
475967ac248eSShaul Triebitz 					  IWL_FW_CMD_VER_UNKNOWN);
476067ac248eSShaul Triebitz 
476167ac248eSShaul Triebitz 	if (fw_ver == IWL_FW_CMD_VER_UNKNOWN) {
476267ac248eSShaul Triebitz 		ret = iwl_mvm_send_aux_roc_cmd(mvm, channel, vif, duration);
476367ac248eSShaul Triebitz 	} else if (fw_ver == 3) {
476467ac248eSShaul Triebitz 		ret = iwl_mvm_roc_add_cmd(mvm, channel, vif, duration,
476567ac248eSShaul Triebitz 					  ROC_ACTIVITY_HOTSPOT);
476667ac248eSShaul Triebitz 	} else {
476767ac248eSShaul Triebitz 		ret = -EOPNOTSUPP;
476867ac248eSShaul Triebitz 		IWL_ERR(mvm, "ROC command version %d mismatch!\n", fw_ver);
476967ac248eSShaul Triebitz 	}
477067ac248eSShaul Triebitz 
477167ac248eSShaul Triebitz 	return ret;
477267ac248eSShaul Triebitz }
477367ac248eSShaul Triebitz 
477434cc3a4aSEmmanuel Grumbach static int iwl_mvm_p2p_find_phy_ctxt(struct iwl_mvm *mvm,
477534cc3a4aSEmmanuel Grumbach 				     struct ieee80211_vif *vif,
477634cc3a4aSEmmanuel Grumbach 				     struct ieee80211_channel *channel)
477734cc3a4aSEmmanuel Grumbach {
477834cc3a4aSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
477934cc3a4aSEmmanuel Grumbach 	struct cfg80211_chan_def chandef;
478034cc3a4aSEmmanuel Grumbach 	int i;
478134cc3a4aSEmmanuel Grumbach 
478234cc3a4aSEmmanuel Grumbach 	lockdep_assert_held(&mvm->mutex);
478334cc3a4aSEmmanuel Grumbach 
478434cc3a4aSEmmanuel Grumbach 	if (mvmvif->deflink.phy_ctxt &&
478534cc3a4aSEmmanuel Grumbach 	    channel == mvmvif->deflink.phy_ctxt->channel)
478634cc3a4aSEmmanuel Grumbach 		return 0;
478734cc3a4aSEmmanuel Grumbach 
478834cc3a4aSEmmanuel Grumbach 	/* Try using a PHY context that is already in use */
478934cc3a4aSEmmanuel Grumbach 	for (i = 0; i < NUM_PHY_CTX; i++) {
479034cc3a4aSEmmanuel Grumbach 		struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[i];
479134cc3a4aSEmmanuel Grumbach 
479234cc3a4aSEmmanuel Grumbach 		if (!phy_ctxt->ref || mvmvif->deflink.phy_ctxt == phy_ctxt)
479334cc3a4aSEmmanuel Grumbach 			continue;
479434cc3a4aSEmmanuel Grumbach 
479534cc3a4aSEmmanuel Grumbach 		if (channel == phy_ctxt->channel) {
479634cc3a4aSEmmanuel Grumbach 			if (mvmvif->deflink.phy_ctxt)
479734cc3a4aSEmmanuel Grumbach 				iwl_mvm_phy_ctxt_unref(mvm,
479834cc3a4aSEmmanuel Grumbach 						       mvmvif->deflink.phy_ctxt);
479934cc3a4aSEmmanuel Grumbach 
480034cc3a4aSEmmanuel Grumbach 			mvmvif->deflink.phy_ctxt = phy_ctxt;
480134cc3a4aSEmmanuel Grumbach 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
480234cc3a4aSEmmanuel Grumbach 			return 0;
480334cc3a4aSEmmanuel Grumbach 		}
480434cc3a4aSEmmanuel Grumbach 	}
480534cc3a4aSEmmanuel Grumbach 
480634cc3a4aSEmmanuel Grumbach 	/* We already have a phy_ctxt, but it's not on the right channel */
480734cc3a4aSEmmanuel Grumbach 	if (mvmvif->deflink.phy_ctxt)
480834cc3a4aSEmmanuel Grumbach 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
480934cc3a4aSEmmanuel Grumbach 
481034cc3a4aSEmmanuel Grumbach 	mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
481134cc3a4aSEmmanuel Grumbach 	if (!mvmvif->deflink.phy_ctxt)
481234cc3a4aSEmmanuel Grumbach 		return -ENOSPC;
481334cc3a4aSEmmanuel Grumbach 
481434cc3a4aSEmmanuel Grumbach 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
481534cc3a4aSEmmanuel Grumbach 
481634cc3a4aSEmmanuel Grumbach 	return iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt,
481732a5690eSJohannes Berg 				    &chandef, NULL, 1, 1);
481834cc3a4aSEmmanuel Grumbach }
481934cc3a4aSEmmanuel Grumbach 
4820fe8b2ad3SMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
4821fe8b2ad3SMiri Korenblit int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4822fe8b2ad3SMiri Korenblit 		       struct ieee80211_channel *channel, int duration,
4823fe8b2ad3SMiri Korenblit 		       enum ieee80211_roc_type type,
4824a2906ea6SJohannes Berg 		       const struct iwl_mvm_roc_ops *ops)
4825fe8b2ad3SMiri Korenblit {
4826e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4827*5f1fee96SJohannes Berg 	struct ieee80211_vif *bss_vif = iwl_mvm_get_bss_vif(mvm);
4828feebebaeSMiri Korenblit 	u32 lmac_id;
482934cc3a4aSEmmanuel Grumbach 	int ret;
4830e705c121SKalle Valo 
4831e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4832e705c121SKalle Valo 			   duration, type);
4833e705c121SKalle Valo 
48349834781cSJohannes Berg 	/*
48359834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
48369834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
48379834781cSJohannes Berg 	 */
4838e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
4839e705c121SKalle Valo 
4840*5f1fee96SJohannes Berg 	if (!IS_ERR_OR_NULL(bss_vif)) {
4841*5f1fee96SJohannes Berg 		ret = iwl_mvm_block_esr_sync(mvm, bss_vif,
4842*5f1fee96SJohannes Berg 					     IWL_MVM_ESR_BLOCKED_ROC);
4843a1efeb82SYedidya Benshimol 		if (ret)
4844a1efeb82SYedidya Benshimol 			return ret;
4845*5f1fee96SJohannes Berg 	}
4846a1efeb82SYedidya Benshimol 
48473f312651SJohannes Berg 	guard(mvm)(mvm);
4848e705c121SKalle Valo 
4849e705c121SKalle Valo 	switch (vif->type) {
4850e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4851d51439a6SAriel Malamud 		lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
48522c2c3647SNathan Errera 
4853feebebaeSMiri Korenblit 		/* Use aux roc framework (HS20) */
4854fe8b2ad3SMiri Korenblit 		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4855feebebaeSMiri Korenblit 		if (!ret)
485667ac248eSShaul Triebitz 			ret = iwl_mvm_roc_station(mvm, channel, vif, duration);
48573f312651SJohannes Berg 		return ret;
4858e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
4859e705c121SKalle Valo 		/* handle below */
4860e705c121SKalle Valo 		break;
4861e705c121SKalle Valo 	default:
486284ef7cbeSIlan Peer 		IWL_ERR(mvm, "ROC: Invalid vif type=%u\n", vif->type);
48633f312651SJohannes Berg 		return -EINVAL;
4864e705c121SKalle Valo 	}
4865e705c121SKalle Valo 
4866e705c121SKalle Valo 
486734cc3a4aSEmmanuel Grumbach 	ret = iwl_mvm_p2p_find_phy_ctxt(mvm, vif, channel);
486834cc3a4aSEmmanuel Grumbach 	if (ret)
48693f312651SJohannes Berg 		return ret;
4870e705c121SKalle Valo 
487184ef7cbeSIlan Peer 	ret = ops->link(mvm, vif);
4872feebebaeSMiri Korenblit 	if (ret)
4873e705c121SKalle Valo 		return ret;
48743f312651SJohannes Berg 
48753f312651SJohannes Berg 	return iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4876e705c121SKalle Valo }
4877e705c121SKalle Valo 
4878fe8b2ad3SMiri Korenblit int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
48795db4c4b9SEmmanuel Grumbach 		       struct ieee80211_vif *vif)
4880e705c121SKalle Valo {
4881e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4882e705c121SKalle Valo 
4883e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4884e705c121SKalle Valo 
4885fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
4886e705c121SKalle Valo 
4887e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4888e705c121SKalle Valo 	return 0;
4889e705c121SKalle Valo }
4890e705c121SKalle Valo 
4891b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
4892b73f9a4aSJohannes Berg 	bool responder;
4893b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
4894b73f9a4aSJohannes Berg };
4895b73f9a4aSJohannes Berg 
4896b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4897b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
4898b73f9a4aSJohannes Berg {
4899b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4900b73f9a4aSJohannes Berg 
4901d0a9123eSJohannes Berg 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4902b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4903b73f9a4aSJohannes Berg 		data->responder = true;
4904b73f9a4aSJohannes Berg }
4905b73f9a4aSJohannes Berg 
4906ce1fa3adSJohannes Berg bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4907b73f9a4aSJohannes Berg 				      struct ieee80211_chanctx_conf *ctx)
4908b73f9a4aSJohannes Berg {
4909b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
4910b73f9a4aSJohannes Berg 		.responder = false,
4911b73f9a4aSJohannes Berg 		.ctx = ctx,
4912b73f9a4aSJohannes Berg 	};
4913b73f9a4aSJohannes Berg 
4914b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4915b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
4916b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
4917b73f9a4aSJohannes Berg 					&data);
4918b73f9a4aSJohannes Berg 	return data.responder;
4919b73f9a4aSJohannes Berg }
4920b73f9a4aSJohannes Berg 
4921e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4922e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
4923e705c121SKalle Valo {
4924e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4925e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4926ce1fa3adSJohannes Berg 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
4927e705c121SKalle Valo 	int ret;
4928e705c121SKalle Valo 
4929e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4930e705c121SKalle Valo 
4931e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4932e705c121SKalle Valo 
4933e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4934e705c121SKalle Valo 	if (!phy_ctxt) {
4935e705c121SKalle Valo 		ret = -ENOSPC;
4936e705c121SKalle Valo 		goto out;
4937e705c121SKalle Valo 	}
4938e705c121SKalle Valo 
493932a5690eSJohannes Berg 	ret = iwl_mvm_phy_ctxt_add(mvm, phy_ctxt, def, &ctx->ap,
4940e705c121SKalle Valo 				   ctx->rx_chains_static,
4941e705c121SKalle Valo 				   ctx->rx_chains_dynamic);
4942e705c121SKalle Valo 	if (ret) {
4943e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4944e705c121SKalle Valo 		goto out;
4945e705c121SKalle Valo 	}
4946e705c121SKalle Valo 
4947e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4948e705c121SKalle Valo out:
4949e705c121SKalle Valo 	return ret;
4950e705c121SKalle Valo }
4951e705c121SKalle Valo 
4952cbce62a3SMiri Korenblit int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4953e705c121SKalle Valo 			struct ieee80211_chanctx_conf *ctx)
4954e705c121SKalle Valo {
4955e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4956e705c121SKalle Valo 
49573f312651SJohannes Berg 	guard(mvm)(mvm);
49583f312651SJohannes Berg 	return __iwl_mvm_add_chanctx(mvm, ctx);
4959e705c121SKalle Valo }
4960e705c121SKalle Valo 
4961e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4962e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4963e705c121SKalle Valo {
4964e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4965e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4966e705c121SKalle Valo 
4967e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4968e705c121SKalle Valo 
4969e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4970e705c121SKalle Valo }
4971e705c121SKalle Valo 
4972cbce62a3SMiri Korenblit void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4973e705c121SKalle Valo 			    struct ieee80211_chanctx_conf *ctx)
4974e705c121SKalle Valo {
4975e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4976e705c121SKalle Valo 
49773f312651SJohannes Berg 	guard(mvm)(mvm);
4978e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4979e705c121SKalle Valo }
4980e705c121SKalle Valo 
4981cbce62a3SMiri Korenblit void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4982cbce62a3SMiri Korenblit 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
4983e705c121SKalle Valo {
4984e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4985e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4986e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4987ce1fa3adSJohannes Berg 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
4988e705c121SKalle Valo 
4989e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4990e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4991e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4992e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4993e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4994e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4995e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4996e705c121SKalle Valo 		return;
4997e705c121SKalle Valo 
49983f312651SJohannes Berg 	guard(mvm)(mvm);
49997a20bcceSEmmanuel Grumbach 
50007a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
50017a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
5002b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
50033f312651SJohannes Berg 			return;
50047a20bcceSEmmanuel Grumbach 
50057a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
50067a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
5007b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
50083f312651SJohannes Berg 			return;
50097a20bcceSEmmanuel Grumbach 	}
50107a20bcceSEmmanuel Grumbach 
5011e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
501232a5690eSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def, &ctx->ap,
5013e705c121SKalle Valo 				 ctx->rx_chains_static,
5014e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
5015e705c121SKalle Valo }
5016e705c121SKalle Valo 
50178a919a78SMiri Korenblit /*
50188a919a78SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
50198a919a78SMiri Korenblit  *
50208a919a78SMiri Korenblit  * Returns true if we're done assigning the chanctx
50218a919a78SMiri Korenblit  * (either on failure or success)
50228a919a78SMiri Korenblit  */
50234263ac7fSGregory Greenman static bool
50244263ac7fSGregory Greenman __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
5025e705c121SKalle Valo 				    struct ieee80211_vif *vif,
5026e705c121SKalle Valo 				    struct ieee80211_chanctx_conf *ctx,
502750e81437SMiri Korenblit 				    bool switching_chanctx, int *ret)
5028e705c121SKalle Valo {
5029e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5030e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5031e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5032e705c121SKalle Valo 
5033e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
5034e705c121SKalle Valo 
5035650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = phy_ctxt;
5036e705c121SKalle Valo 
5037e705c121SKalle Valo 	switch (vif->type) {
5038e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5039e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
5040e705c121SKalle Valo 		if (switching_chanctx) {
5041e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
5042e705c121SKalle Valo 			break;
5043e705c121SKalle Valo 		}
50445a2abdcaSGustavo A. R. Silva 		fallthrough;
5045e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
5046e705c121SKalle Valo 		/*
5047e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
5048e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
5049e705c121SKalle Valo 		 */
50508a919a78SMiri Korenblit 		*ret = 0;
50518a919a78SMiri Korenblit 		return true;
5052e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
5053e705c121SKalle Valo 		break;
5054e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
5055e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
5056e705c121SKalle Valo 		mvmvif->ps_disabled = true;
5057e705c121SKalle Valo 		break;
5058e705c121SKalle Valo 	default:
50598a919a78SMiri Korenblit 		*ret = -EINVAL;
50608a919a78SMiri Korenblit 		return true;
5061e705c121SKalle Valo 	}
50628a919a78SMiri Korenblit 	return false;
50638a919a78SMiri Korenblit }
50648a919a78SMiri Korenblit 
50658a919a78SMiri Korenblit static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
50668a919a78SMiri Korenblit 					struct ieee80211_vif *vif,
50674263ac7fSGregory Greenman 					struct ieee80211_bss_conf *link_conf,
50688a919a78SMiri Korenblit 					struct ieee80211_chanctx_conf *ctx,
50698a919a78SMiri Korenblit 					bool switching_chanctx)
50708a919a78SMiri Korenblit {
50718a919a78SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
50728a919a78SMiri Korenblit 	int ret;
50738a919a78SMiri Korenblit 
50744263ac7fSGregory Greenman 	if (WARN_ON(!link_conf))
50754263ac7fSGregory Greenman 		return -EINVAL;
50764263ac7fSGregory Greenman 
50778a919a78SMiri Korenblit 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
50788a919a78SMiri Korenblit 						switching_chanctx, &ret))
50798a919a78SMiri Korenblit 		goto out;
5080e705c121SKalle Valo 
5081e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
5082e705c121SKalle Valo 	if (ret)
5083e705c121SKalle Valo 		goto out;
5084e705c121SKalle Valo 
5085e705c121SKalle Valo 	/*
5086e705c121SKalle Valo 	 * Power state must be updated before quotas,
5087e705c121SKalle Valo 	 * otherwise fw will complain.
5088e705c121SKalle Valo 	 */
5089e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5090e705c121SKalle Valo 
5091e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
5092e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
5093e705c121SKalle Valo 	 * will handle quota settings.
5094e705c121SKalle Valo 	 */
5095e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5096e705c121SKalle Valo 		mvmvif->monitor_active = true;
5097e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
5098e705c121SKalle Valo 		if (ret)
5099e705c121SKalle Valo 			goto out_remove_binding;
51000e39eb03SChaya Rachel Ivgi 
51010e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
51020e39eb03SChaya Rachel Ivgi 		if (ret)
51030e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
51040e39eb03SChaya Rachel Ivgi 
5105e705c121SKalle Valo 	}
5106e705c121SKalle Valo 
5107e705c121SKalle Valo 	/* Handle binding during CSA */
5108e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
5109e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
5110e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
5111e705c121SKalle Valo 	}
5112e705c121SKalle Valo 
51138a919a78SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION) {
51148a919a78SMiri Korenblit 		if (!switching_chanctx) {
51158a919a78SMiri Korenblit 			mvmvif->csa_bcn_pending = false;
51168a919a78SMiri Korenblit 			goto out;
51178a919a78SMiri Korenblit 		}
51188a919a78SMiri Korenblit 
511974a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
512074a10252SSara Sharon 
512174a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
512274a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
512311d0d831SJohannes Berg 			u32 duration = 5 * vif->bss_conf.beacon_int;
5124e705c121SKalle Valo 
5125e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
5126e705c121SKalle Valo 			 * beacon on the new channel.
5127e705c121SKalle Valo 			 */
5128e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
5129e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
5130e705c121SKalle Valo 						true);
513174a10252SSara Sharon 		}
5132e705c121SKalle Valo 
5133e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
51346b82f4e1SJohannes Berg 
51356b82f4e1SJohannes Berg 		iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif,
51366b82f4e1SJohannes Berg 							link_conf,
51376b82f4e1SJohannes Berg 							false);
5138e705c121SKalle Valo 	}
5139e705c121SKalle Valo 
5140e705c121SKalle Valo 	goto out;
5141e705c121SKalle Valo 
5142e705c121SKalle Valo out_remove_binding:
5143e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
5144e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5145e705c121SKalle Valo out:
5146e705c121SKalle Valo 	if (ret)
5147650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = NULL;
5148e705c121SKalle Valo 	return ret;
5149e705c121SKalle Valo }
51508a919a78SMiri Korenblit 
5151e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
5152e705c121SKalle Valo 				      struct ieee80211_vif *vif,
5153727eff4dSGregory Greenman 				      struct ieee80211_bss_conf *link_conf,
5154e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
5155e705c121SKalle Valo {
5156e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5157e705c121SKalle Valo 
51583f312651SJohannes Berg 	guard(mvm)(mvm);
51593f312651SJohannes Berg 	return __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5160e705c121SKalle Valo }
5161e705c121SKalle Valo 
5162daddfae5SMiri Korenblit /*
5163daddfae5SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
5164daddfae5SMiri Korenblit  *
5165daddfae5SMiri Korenblit  * Returns if chanctx unassign chanctx is done
5166daddfae5SMiri Korenblit  * (either on failure or success)
5167daddfae5SMiri Korenblit  */
51684263ac7fSGregory Greenman static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
5169e705c121SKalle Valo 						  struct ieee80211_vif *vif,
5170e705c121SKalle Valo 						  bool switching_chanctx)
5171e705c121SKalle Valo {
5172e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5173e705c121SKalle Valo 
5174e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
5175650cadb7SGregory Greenman 	iwl_mvm_remove_time_event(mvm, mvmvif,
5176650cadb7SGregory Greenman 				  &mvmvif->time_event_data);
5177e705c121SKalle Valo 
5178e705c121SKalle Valo 	switch (vif->type) {
5179e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
5180daddfae5SMiri Korenblit 		return true;
5181e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
5182e705c121SKalle Valo 		mvmvif->monitor_active = false;
5183e705c121SKalle Valo 		mvmvif->ps_disabled = false;
5184e705c121SKalle Valo 		break;
5185e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5186e705c121SKalle Valo 		/* This part is triggered only during CSA */
5187e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
5188daddfae5SMiri Korenblit 			return true;
5189e705c121SKalle Valo 
5190e705c121SKalle Valo 		mvmvif->csa_countdown = false;
5191e705c121SKalle Valo 
5192e705c121SKalle Valo 		/* Set CS bit on all the stations */
5193e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5194e705c121SKalle Valo 
5195e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
5196e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5197e705c121SKalle Valo 
5198e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
5199e705c121SKalle Valo 		break;
5200daddfae5SMiri Korenblit 	default:
5201e705c121SKalle Valo 		break;
5202daddfae5SMiri Korenblit 	}
5203daddfae5SMiri Korenblit 	return false;
5204daddfae5SMiri Korenblit }
5205e705c121SKalle Valo 
5206e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5207e705c121SKalle Valo 					   struct ieee80211_vif *vif,
52084263ac7fSGregory Greenman 					   struct ieee80211_bss_conf *link_conf,
5209e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
5210e705c121SKalle Valo 					   bool switching_chanctx)
5211e705c121SKalle Valo {
5212e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5213e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
5214e705c121SKalle Valo 
5215daddfae5SMiri Korenblit 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5216e705c121SKalle Valo 		goto out;
5217daddfae5SMiri Korenblit 
5218daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_MONITOR)
5219e705c121SKalle Valo 		iwl_mvm_rm_snif_sta(mvm, vif);
5220e705c121SKalle Valo 
5221e705c121SKalle Valo 
5222daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5223e705c121SKalle Valo 		disabled_vif = vif;
522474a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
522574a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5226e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5227e705c121SKalle Valo 	}
5228e705c121SKalle Valo 
5229e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5230e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
5231e705c121SKalle Valo 
5232e705c121SKalle Valo out:
5233bf544e9aSSara Sharon 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5234bf544e9aSSara Sharon 	    switching_chanctx)
5235bf544e9aSSara Sharon 		return;
5236650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
5237e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5238e705c121SKalle Valo }
5239e705c121SKalle Valo 
5240e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5241e705c121SKalle Valo 					 struct ieee80211_vif *vif,
5242727eff4dSGregory Greenman 					 struct ieee80211_bss_conf *link_conf,
5243e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
5244e705c121SKalle Valo {
5245e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5246e705c121SKalle Valo 
52473f312651SJohannes Berg 	guard(mvm)(mvm);
52484263ac7fSGregory Greenman 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5249e705c121SKalle Valo }
5250e705c121SKalle Valo 
5251e705c121SKalle Valo static int
5252e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5253660eba5aSMiri Korenblit 				struct ieee80211_vif_chanctx_switch *vifs,
5254a2906ea6SJohannes Berg 				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5255e705c121SKalle Valo {
5256e705c121SKalle Valo 	int ret;
5257e705c121SKalle Valo 
52583f312651SJohannes Berg 	guard(mvm)(mvm);
52594263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
52604263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5261e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5262e705c121SKalle Valo 
5263e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5264e705c121SKalle Valo 	if (ret) {
5265e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5266e705c121SKalle Valo 		goto out_reassign;
5267e705c121SKalle Valo 	}
5268e705c121SKalle Valo 
52694263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
52704263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5271e705c121SKalle Valo 	if (ret) {
5272e705c121SKalle Valo 		IWL_ERR(mvm,
5273e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5274e705c121SKalle Valo 		goto out_remove;
5275e705c121SKalle Valo 	}
5276e705c121SKalle Valo 
5277e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
5278e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5279e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
5280e705c121SKalle Valo 
52813f312651SJohannes Berg 	return 0;
5282e705c121SKalle Valo 
5283e705c121SKalle Valo out_remove:
5284e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5285e705c121SKalle Valo 
5286e705c121SKalle Valo out_reassign:
5287e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5288e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5289e705c121SKalle Valo 		goto out_restart;
5290e705c121SKalle Valo 	}
5291e705c121SKalle Valo 
52924263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
52934263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5294e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5295e705c121SKalle Valo 		goto out_restart;
5296e705c121SKalle Valo 	}
5297e705c121SKalle Valo 
52983f312651SJohannes Berg 	return ret;
5299e705c121SKalle Valo 
5300e705c121SKalle Valo out_restart:
5301e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5302e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5303e705c121SKalle Valo 	return ret;
5304e705c121SKalle Valo }
5305e705c121SKalle Valo 
5306e705c121SKalle Valo static int
5307e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5308660eba5aSMiri Korenblit 				    struct ieee80211_vif_chanctx_switch *vifs,
5309a2906ea6SJohannes Berg 				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5310e705c121SKalle Valo {
5311e705c121SKalle Valo 	int ret;
5312e705c121SKalle Valo 
53133f312651SJohannes Berg 	guard(mvm)(mvm);
53144263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
53154263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5316e705c121SKalle Valo 
53174263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
53184263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5319e705c121SKalle Valo 	if (ret) {
5320e705c121SKalle Valo 		IWL_ERR(mvm,
5321e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5322e705c121SKalle Valo 		goto out_reassign;
5323e705c121SKalle Valo 	}
5324e705c121SKalle Valo 
53253f312651SJohannes Berg 	return 0;
5326e705c121SKalle Valo 
5327e705c121SKalle Valo out_reassign:
53284263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
53294263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5330e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5331e705c121SKalle Valo 		goto out_restart;
5332e705c121SKalle Valo 	}
5333e705c121SKalle Valo 
53343f312651SJohannes Berg 	return ret;
5335e705c121SKalle Valo 
5336e705c121SKalle Valo out_restart:
5337e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5338e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5339e705c121SKalle Valo 	return ret;
5340e705c121SKalle Valo }
5341e705c121SKalle Valo 
5342660eba5aSMiri Korenblit /* Execute the common part for both MLD and non-MLD modes */
5343660eba5aSMiri Korenblit int
5344660eba5aSMiri Korenblit iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5345e705c121SKalle Valo 				  struct ieee80211_vif_chanctx_switch *vifs,
5346e705c121SKalle Valo 				  int n_vifs,
5347660eba5aSMiri Korenblit 				  enum ieee80211_chanctx_switch_mode mode,
5348a2906ea6SJohannes Berg 				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5349e705c121SKalle Valo {
5350e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5351e705c121SKalle Valo 	int ret;
5352e705c121SKalle Valo 
5353e705c121SKalle Valo 	/* we only support a single-vif right now */
5354e705c121SKalle Valo 	if (n_vifs > 1)
5355e705c121SKalle Valo 		return -EOPNOTSUPP;
5356e705c121SKalle Valo 
5357e705c121SKalle Valo 	switch (mode) {
5358e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5359660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5360e705c121SKalle Valo 		break;
5361e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
5362660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5363e705c121SKalle Valo 		break;
5364e705c121SKalle Valo 	default:
5365e705c121SKalle Valo 		ret = -EOPNOTSUPP;
5366e705c121SKalle Valo 		break;
5367e705c121SKalle Valo 	}
5368e705c121SKalle Valo 
5369e705c121SKalle Valo 	return ret;
5370e705c121SKalle Valo }
5371e705c121SKalle Valo 
5372660eba5aSMiri Korenblit static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5373660eba5aSMiri Korenblit 				      struct ieee80211_vif_chanctx_switch *vifs,
5374660eba5aSMiri Korenblit 				      int n_vifs,
5375660eba5aSMiri Korenblit 				      enum ieee80211_chanctx_switch_mode mode)
5376660eba5aSMiri Korenblit {
5377a2906ea6SJohannes Berg 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5378660eba5aSMiri Korenblit 		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5379660eba5aSMiri Korenblit 		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5380660eba5aSMiri Korenblit 	};
5381660eba5aSMiri Korenblit 
5382660eba5aSMiri Korenblit 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5383660eba5aSMiri Korenblit }
5384660eba5aSMiri Korenblit 
5385cbce62a3SMiri Korenblit int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
53862f0282dbSJohannes Berg {
53872f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
53882f0282dbSJohannes Berg 
53892f0282dbSJohannes Berg 	return mvm->ibss_manager;
53902f0282dbSJohannes Berg }
53912f0282dbSJohannes Berg 
5392a32973eeSJohannes Berg static int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5393e705c121SKalle Valo 			   bool set)
5394e705c121SKalle Valo {
5395e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5396e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5397e705c121SKalle Valo 
5398e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
5399e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
5400e705c121SKalle Valo 		return -EINVAL;
5401e705c121SKalle Valo 	}
5402e705c121SKalle Valo 
540336cf5377SGregory Greenman 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
540436cf5377SGregory Greenman 					       &mvm_sta->vif->bss_conf);
5405e705c121SKalle Valo }
5406e705c121SKalle Valo 
5407e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
5408e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5409e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5410e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5411e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5412e705c121SKalle Valo };
5413e705c121SKalle Valo 
5414e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5415e705c121SKalle Valo 				      struct ieee80211_vif *vif,
5416e705c121SKalle Valo 				      void *data, int len)
5417e705c121SKalle Valo {
5418e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5419e705c121SKalle Valo 	int err;
5420e705c121SKalle Valo 	u32 noa_duration;
5421e705c121SKalle Valo 
54228cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
54238cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
5424e705c121SKalle Valo 	if (err)
5425e705c121SKalle Valo 		return err;
5426e705c121SKalle Valo 
5427e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
5428e705c121SKalle Valo 		return -EINVAL;
5429e705c121SKalle Valo 
5430e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5431e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
5432e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5433e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
5434e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5435e705c121SKalle Valo 			return -EINVAL;
5436e705c121SKalle Valo 
5437e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5438e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
5439e705c121SKalle Valo 			return -EINVAL;
5440e705c121SKalle Valo 
5441e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
5442e705c121SKalle Valo 		mvm->noa_vif = vif;
5443e705c121SKalle Valo 
544422b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
5445e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5446e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
5447e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5448f276e20bSJohannes Berg 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5449e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5450e705c121SKalle Valo 			return -EINVAL;
5451e705c121SKalle Valo 
5452e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
545387f5b5f2SJohannes Berg 			return iwl_mvm_enable_beacon_filter(mvm, vif);
545487f5b5f2SJohannes Berg 		return iwl_mvm_disable_beacon_filter(mvm, vif);
5455e705c121SKalle Valo 	}
5456e705c121SKalle Valo 
5457e705c121SKalle Valo 	return -EOPNOTSUPP;
5458e705c121SKalle Valo }
5459e705c121SKalle Valo 
5460cbce62a3SMiri Korenblit int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5461e705c121SKalle Valo 			     struct ieee80211_vif *vif,
5462e705c121SKalle Valo 			     void *data, int len)
5463e705c121SKalle Valo {
5464e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5465e705c121SKalle Valo 
54663f312651SJohannes Berg 	guard(mvm)(mvm);
54673f312651SJohannes Berg 	return __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5468e705c121SKalle Valo }
5469e705c121SKalle Valo #endif
5470e705c121SKalle Valo 
5471cbce62a3SMiri Korenblit void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5472e705c121SKalle Valo 			    struct ieee80211_channel_switch *chsw)
5473e705c121SKalle Valo {
5474e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
5475e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
5476e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
5477e705c121SKalle Valo 	 * (which is when the absence time event starts).
5478e705c121SKalle Valo 	 */
5479e705c121SKalle Valo 
5480e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5481e705c121SKalle Valo 			   "dummy channel switch op\n");
5482e705c121SKalle Valo }
5483e705c121SKalle Valo 
548474a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
548574a10252SSara Sharon 				       struct ieee80211_vif *vif,
548674a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
548774a10252SSara Sharon {
548874a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
548974a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
549074a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
549174a10252SSara Sharon 							  mvmvif->color)),
549274a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
549374a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
549474a10252SSara Sharon 		.cs_count = chsw->count,
549577738865SSara Sharon 		.cs_mode = chsw->block_tx,
549674a10252SSara Sharon 	};
549774a10252SSara Sharon 
549874a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
549974a10252SSara Sharon 
55009cfcf71cSSara Sharon 	if (chsw->delay)
55019cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
55029cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
55039cfcf71cSSara Sharon 
550474a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
550574a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
550674a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
550774a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
550874a10252SSara Sharon }
550974a10252SSara Sharon 
55100202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
55110202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
55120202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
55130202bcf0SEmmanuel Grumbach {
55140202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
55150202bcf0SEmmanuel Grumbach 	u32 apply_time;
55160202bcf0SEmmanuel Grumbach 
55170202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
55180202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
55190202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
55200202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
55210202bcf0SEmmanuel Grumbach 	 * loss).
55220202bcf0SEmmanuel Grumbach 	 */
55230202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
55240202bcf0SEmmanuel Grumbach 		apply_time = 0;
55250202bcf0SEmmanuel Grumbach 	else
55260202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
55270202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
55280202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
55290202bcf0SEmmanuel Grumbach 
55300202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
55310202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
55320202bcf0SEmmanuel Grumbach 
5533c1e458b9SIlan Peer 	if (mvmvif->bf_enabled) {
553487f5b5f2SJohannes Berg 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif);
55350202bcf0SEmmanuel Grumbach 
55360202bcf0SEmmanuel Grumbach 		if (ret)
55370202bcf0SEmmanuel Grumbach 			return ret;
55380202bcf0SEmmanuel Grumbach 	}
55390202bcf0SEmmanuel Grumbach 
55400202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
55410202bcf0SEmmanuel Grumbach 				    apply_time);
55420202bcf0SEmmanuel Grumbach 
55430202bcf0SEmmanuel Grumbach 	return 0;
55440202bcf0SEmmanuel Grumbach }
55450202bcf0SEmmanuel Grumbach 
5546bbd6d0f8SJohannes Berg static void iwl_mvm_csa_block_txqs(void *data, struct ieee80211_sta *sta)
5547bbd6d0f8SJohannes Berg {
5548bbd6d0f8SJohannes Berg 	int i;
5549bbd6d0f8SJohannes Berg 
5550bbd6d0f8SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
5551bbd6d0f8SJohannes Berg 		struct iwl_mvm_txq *mvmtxq =
5552bbd6d0f8SJohannes Berg 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
5553bbd6d0f8SJohannes Berg 
5554bbd6d0f8SJohannes Berg 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5555bbd6d0f8SJohannes Berg 	}
5556bbd6d0f8SJohannes Berg }
5557bbd6d0f8SJohannes Berg 
5558f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
55592f876f91SJohannes Berg int iwl_mvm_pre_channel_switch(struct iwl_mvm *mvm,
5560e705c121SKalle Valo 			       struct ieee80211_vif *vif,
5561e705c121SKalle Valo 			       struct ieee80211_channel_switch *chsw)
5562e705c121SKalle Valo {
5563e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
5564e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5565bbd6d0f8SJohannes Berg 	struct iwl_mvm_txq *mvmtxq;
5566e705c121SKalle Valo 	int ret;
5567e705c121SKalle Valo 
55682f876f91SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
5569e705c121SKalle Valo 
5570e705c121SKalle Valo 	mvmvif->csa_failed = false;
55712964b57fSJohannes Berg 	mvmvif->csa_blocks_tx = false;
5572e705c121SKalle Valo 
5573e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5574e705c121SKalle Valo 			   chsw->chandef.center_freq1);
5575e705c121SKalle Valo 
55767174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
55777174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
55787174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5579e705c121SKalle Valo 
5580e705c121SKalle Valo 	switch (vif->type) {
5581e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5582e705c121SKalle Valo 		csa_vif =
5583e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
5584e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
5585d0a9123eSJohannes Berg 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
55862f876f91SJohannes Berg 			      "Another CSA is already in progress"))
55872f876f91SJohannes Berg 			return -EBUSY;
5588e705c121SKalle Valo 
5589d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
5590d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
55912f876f91SJohannes Berg 					      lockdep_is_held(&mvm->mutex)))
55922f876f91SJohannes Berg 			return -EBUSY;
5593d3a108a4SAndrei Otcheretianski 
5594e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
5595e705c121SKalle Valo 
5596e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
55972f876f91SJohannes Berg 			      "Previous CSA countdown didn't complete"))
55982f876f91SJohannes Berg 			return -EBUSY;
5599e705c121SKalle Valo 
5600d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5601d3a108a4SAndrei Otcheretianski 
5602bbd6d0f8SJohannes Berg 		if (!chsw->block_tx)
5603bbd6d0f8SJohannes Berg 			break;
5604bbd6d0f8SJohannes Berg 		/* don't need blocking in driver otherwise - mac80211 will do */
5605bbd6d0f8SJohannes Berg 		if (!ieee80211_hw_check(mvm->hw, HANDLES_QUIET_CSA))
5606bbd6d0f8SJohannes Berg 			break;
5607bbd6d0f8SJohannes Berg 
5608bbd6d0f8SJohannes Berg 		mvmvif->csa_blocks_tx = true;
5609bbd6d0f8SJohannes Berg 		mvmtxq = iwl_mvm_txq_from_mac80211(vif->txq);
5610bbd6d0f8SJohannes Berg 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5611bbd6d0f8SJohannes Berg 		ieee80211_iterate_stations_atomic(mvm->hw,
5612bbd6d0f8SJohannes Berg 						  iwl_mvm_csa_block_txqs,
5613bbd6d0f8SJohannes Berg 						  NULL);
5614e705c121SKalle Valo 		break;
5615e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
56162964b57fSJohannes Berg 		mvmvif->csa_blocks_tx = chsw->block_tx;
56172964b57fSJohannes Berg 
56182360acbdSEmmanuel Grumbach 		/*
5619ad12b231SNathan Errera 		 * In the new flow FW is in charge of timing the switch so there
5620ad12b231SNathan Errera 		 * is no need for all of this
5621ad12b231SNathan Errera 		 */
5622ad12b231SNathan Errera 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5623ad12b231SNathan Errera 					    CHANNEL_SWITCH_ERROR_NOTIF,
5624ad12b231SNathan Errera 					    0))
5625ad12b231SNathan Errera 			break;
5626ad12b231SNathan Errera 
5627ad12b231SNathan Errera 		/*
56282360acbdSEmmanuel Grumbach 		 * We haven't configured the firmware to be associated yet since
56292360acbdSEmmanuel Grumbach 		 * we don't know the dtim period. In this case, the firmware can't
56302360acbdSEmmanuel Grumbach 		 * track the beacons.
56312360acbdSEmmanuel Grumbach 		 */
56322f876f91SJohannes Berg 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period)
56332f876f91SJohannes Berg 			return -EBUSY;
56342360acbdSEmmanuel Grumbach 
5635aee2eac7SJohannes Berg 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX &&
5636aee2eac7SJohannes Berg 		    hweight16(vif->valid_links) <= 1)
563787d9564eSJohannes Berg 			schedule_delayed_work(&mvmvif->csa_work, 0);
563887d9564eSJohannes Berg 
5639f6780614SSara Sharon 		if (chsw->block_tx) {
5640f6780614SSara Sharon 			/*
5641f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
5642f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
5643f6780614SSara Sharon 			 */
5644f6780614SSara Sharon 			if (!chsw->count ||
5645f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
5646f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
5647f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
5648f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5649f6780614SSara Sharon 		}
5650e705c121SKalle Valo 
56510202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
56520202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
56530202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5654e705c121SKalle Valo 			if (ret)
56552f876f91SJohannes Berg 				return ret;
56560202bcf0SEmmanuel Grumbach 		} else {
565774a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
56580202bcf0SEmmanuel Grumbach 		}
565981b4e44eSSara Sharon 
566081b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
566181b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
5662e705c121SKalle Valo 		break;
5663e705c121SKalle Valo 	default:
5664e705c121SKalle Valo 		break;
5665e705c121SKalle Valo 	}
5666e705c121SKalle Valo 
5667e705c121SKalle Valo 	mvmvif->ps_disabled = true;
5668e705c121SKalle Valo 
5669e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
5670e705c121SKalle Valo 	if (ret)
56712f876f91SJohannes Berg 		return ret;
5672e705c121SKalle Valo 
5673e705c121SKalle Valo 	/* we won't be on this channel any longer */
5674e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
5675e705c121SKalle Valo 
56762f876f91SJohannes Berg 	return ret;
56772f876f91SJohannes Berg }
56782f876f91SJohannes Berg 
56792f876f91SJohannes Berg static int iwl_mvm_mac_pre_channel_switch(struct ieee80211_hw *hw,
56802f876f91SJohannes Berg 					  struct ieee80211_vif *vif,
56812f876f91SJohannes Berg 					  struct ieee80211_channel_switch *chsw)
56822f876f91SJohannes Berg {
56832f876f91SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
56842f876f91SJohannes Berg 
56853f312651SJohannes Berg 	guard(mvm)(mvm);
56863f312651SJohannes Berg 	return iwl_mvm_pre_channel_switch(mvm, vif, chsw);
5687e705c121SKalle Valo }
5688e705c121SKalle Valo 
5689cbce62a3SMiri Korenblit void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5690c37763d2SSara Sharon 				      struct ieee80211_vif *vif,
5691c37763d2SSara Sharon 				      struct ieee80211_channel_switch *chsw)
5692e705c121SKalle Valo {
5693e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5694c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5695c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
5696c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5697c37763d2SSara Sharon 							  mvmvif->color)),
5698c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5699c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
5700c37763d2SSara Sharon 		.cs_count = chsw->count,
570177738865SSara Sharon 		.cs_mode = chsw->block_tx,
5702c37763d2SSara Sharon 	};
5703e705c121SKalle Valo 
5704ad12b231SNathan Errera 	/*
5705ad12b231SNathan Errera 	 * In the new flow FW is in charge of timing the switch so there is no
5706ad12b231SNathan Errera 	 * need for all of this
5707ad12b231SNathan Errera 	 */
5708ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5709ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5710ad12b231SNathan Errera 		return;
5711ad12b231SNathan Errera 
5712c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5713c37763d2SSara Sharon 		return;
5714e705c121SKalle Valo 
5715c3eae059SGregory Greenman 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5716c3eae059SGregory Greenman 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5717c3eae059SGregory Greenman 
571881b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
571981b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
57205ecd5d82SJohannes Berg 			struct ieee80211_bss_conf *link_conf;
57215ecd5d82SJohannes Berg 
572281b4e44eSSara Sharon 			/* Second time, give up on this AP*/
57235ecd5d82SJohannes Berg 
57245ecd5d82SJohannes Berg 			link_conf = wiphy_dereference(hw->wiphy,
57255ecd5d82SJohannes Berg 						      vif->link_conf[chsw->link_id]);
57265ecd5d82SJohannes Berg 			if (WARN_ON(!link_conf))
57275ecd5d82SJohannes Berg 				return;
57285ecd5d82SJohannes Berg 
57295ecd5d82SJohannes Berg 			iwl_mvm_abort_channel_switch(hw, vif, link_conf);
5730a469a593SEmmanuel Grumbach 			ieee80211_chswitch_done(vif, false, 0);
573181b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
573281b4e44eSSara Sharon 			return;
5733e705c121SKalle Valo 		}
573481b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
5735e705c121SKalle Valo 	}
573681b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
5737e705c121SKalle Valo 
57383f312651SJohannes Berg 	guard(mvm)(mvm);
5739caf46377SSara Sharon 	if (mvmvif->csa_failed)
57403f312651SJohannes Berg 		return;
5741e705c121SKalle Valo 
5742c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5743c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
5744c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5745caf46377SSara Sharon 				     0, sizeof(cmd), &cmd));
5746e705c121SKalle Valo }
5747e705c121SKalle Valo 
57486110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
57496110d9e5SDavid Spinadel {
5750435d0827SSara Sharon 	int i;
5751435d0827SSara Sharon 
575206195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5753309c4848SLuca Coelho 		if (drop) {
57543f312651SJohannes Berg 			guard(mvm)(mvm);
57556110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
5756d4e3a341SMordechay Goodstein 				iwl_mvm_flushable_queues(mvm) & queues);
5757309c4848SLuca Coelho 		} else {
5758435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5759309c4848SLuca Coelho 		}
5760435d0827SSara Sharon 		return;
5761435d0827SSara Sharon 	}
57626110d9e5SDavid Spinadel 
57633f312651SJohannes Berg 	guard(mvm)(mvm);
5764be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5765435d0827SSara Sharon 		struct ieee80211_sta *sta;
5766435d0827SSara Sharon 
5767435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
57686110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
57696110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
57706110d9e5SDavid Spinadel 			continue;
57716110d9e5SDavid Spinadel 
577206195639SSara Sharon 		if (drop)
5773d4e3a341SMordechay Goodstein 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
577406195639SSara Sharon 		else
57756110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
57766110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
57776110d9e5SDavid Spinadel 	}
57786110d9e5SDavid Spinadel }
57796110d9e5SDavid Spinadel 
5780cbce62a3SMiri Korenblit void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5781cbce62a3SMiri Korenblit 		       u32 queues, bool drop)
5782e705c121SKalle Valo {
57832964b57fSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5784e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5785e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
5786e705c121SKalle Valo 	struct ieee80211_sta *sta;
5787d3f9cd61SJohannes Berg 	bool ap_sta_done = false;
5788e705c121SKalle Valo 	int i;
5789e705c121SKalle Valo 	u32 msk = 0;
5790e705c121SKalle Valo 
57916110d9e5SDavid Spinadel 	if (!vif) {
57926110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
57936110d9e5SDavid Spinadel 		return;
57946110d9e5SDavid Spinadel 	}
57956110d9e5SDavid Spinadel 
57962964b57fSJohannes Berg 	if (!drop && hweight16(vif->active_links) <= 1) {
57972964b57fSJohannes Berg 		int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
57982964b57fSJohannes Berg 		struct ieee80211_bss_conf *link_conf;
57992964b57fSJohannes Berg 
58002964b57fSJohannes Berg 		link_conf = wiphy_dereference(hw->wiphy,
58012964b57fSJohannes Berg 					      vif->link_conf[link_id]);
58022964b57fSJohannes Berg 		if (WARN_ON(!link_conf))
58032964b57fSJohannes Berg 			return;
58042964b57fSJohannes Berg 		if (link_conf->csa_active && mvmvif->csa_blocks_tx)
58052964b57fSJohannes Berg 			drop = true;
58062964b57fSJohannes Berg 	}
58072964b57fSJohannes Berg 
580824afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
580924afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
581024afba76SLiad Kaufman 
5811e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5812e705c121SKalle Valo 
5813e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
5814be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5815e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5816e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
5817e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
5818e705c121SKalle Valo 			continue;
5819e705c121SKalle Valo 
5820e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5821e705c121SKalle Valo 		if (mvmsta->vif != vif)
5822e705c121SKalle Valo 			continue;
5823e705c121SKalle Valo 
5824d3f9cd61SJohannes Berg 		if (sta == mvmvif->ap_sta) {
5825d3f9cd61SJohannes Berg 			if (ap_sta_done)
5826d3f9cd61SJohannes Berg 				continue;
5827d3f9cd61SJohannes Berg 			ap_sta_done = true;
5828d3f9cd61SJohannes Berg 		}
5829d3f9cd61SJohannes Berg 
5830d49394a1SSara Sharon 		if (drop) {
583139176296SJohannes Berg 			if (iwl_mvm_flush_sta(mvm, mvmsta->deflink.sta_id,
583239176296SJohannes Berg 					      mvmsta->tfd_queue_msk))
5833d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
5834d49394a1SSara Sharon 		} else {
5835d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
5836d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5837f7bd883bSJohannes Berg 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5838f7bd883bSJohannes Berg 				msk |= mvmsta->tfd_queue_msk;
5839e705c121SKalle Valo 		}
5840d49394a1SSara Sharon 	}
5841e705c121SKalle Valo 
5842e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5843e705c121SKalle Valo 
5844e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
5845e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
5846e705c121SKalle Valo 	 */
5847d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5848a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5849e705c121SKalle Valo }
5850e705c121SKalle Valo 
5851a6cc6ccbSJohannes Berg void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5852a6cc6ccbSJohannes Berg 			   struct ieee80211_sta *sta)
5853a6cc6ccbSJohannes Berg {
585443874283SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5855a6cc6ccbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
585643874283SJohannes Berg 	struct iwl_mvm_link_sta *mvm_link_sta;
585743874283SJohannes Berg 	struct ieee80211_link_sta *link_sta;
585843874283SJohannes Berg 	int link_id;
5859a6cc6ccbSJohannes Berg 
58603f312651SJohannes Berg 	guard(mvm)(mvm);
586143874283SJohannes Berg 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
586243874283SJohannes Berg 		mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_id],
5863a6cc6ccbSJohannes Berg 							 lockdep_is_held(&mvm->mutex));
586443874283SJohannes Berg 		if (!mvm_link_sta)
5865a6cc6ccbSJohannes Berg 			continue;
5866a6cc6ccbSJohannes Berg 
586743874283SJohannes Berg 		if (iwl_mvm_flush_sta(mvm, mvm_link_sta->sta_id,
586839176296SJohannes Berg 				      mvmsta->tfd_queue_msk))
5869a6cc6ccbSJohannes Berg 			IWL_ERR(mvm, "flush request fail\n");
5870a6cc6ccbSJohannes Berg 	}
5871a6cc6ccbSJohannes Berg }
5872a6cc6ccbSJohannes Berg 
5873fc612222SBenjamin Berg static int iwl_mvm_mac_get_acs_survey(struct iwl_mvm *mvm, int idx,
5874fc612222SBenjamin Berg 				      struct survey_info *survey)
5875fc612222SBenjamin Berg {
5876fc612222SBenjamin Berg 	int chan_idx;
5877fc612222SBenjamin Berg 	enum nl80211_band band;
5878fc612222SBenjamin Berg 	int ret;
5879fc612222SBenjamin Berg 
5880fc612222SBenjamin Berg 	mutex_lock(&mvm->mutex);
5881fc612222SBenjamin Berg 
5882fc612222SBenjamin Berg 	if (!mvm->acs_survey) {
5883fc612222SBenjamin Berg 		ret = -ENOENT;
5884fc612222SBenjamin Berg 		goto out;
5885fc612222SBenjamin Berg 	}
5886fc612222SBenjamin Berg 
5887fc612222SBenjamin Berg 	/* Find and return the next entry that has a non-zero active time */
5888fc612222SBenjamin Berg 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5889fc612222SBenjamin Berg 		struct ieee80211_supported_band *sband =
5890fc612222SBenjamin Berg 			mvm->hw->wiphy->bands[band];
5891fc612222SBenjamin Berg 
5892fc612222SBenjamin Berg 		if (!sband)
5893fc612222SBenjamin Berg 			continue;
5894fc612222SBenjamin Berg 
5895fc612222SBenjamin Berg 		for (chan_idx = 0; chan_idx < sband->n_channels; chan_idx++) {
5896fc612222SBenjamin Berg 			struct iwl_mvm_acs_survey_channel *info =
5897fc612222SBenjamin Berg 				&mvm->acs_survey->bands[band][chan_idx];
5898fc612222SBenjamin Berg 
5899fc612222SBenjamin Berg 			if (!info->time)
5900fc612222SBenjamin Berg 				continue;
5901fc612222SBenjamin Berg 
5902fc612222SBenjamin Berg 			/* Found (the next) channel to report */
5903fc612222SBenjamin Berg 			survey->channel = &sband->channels[chan_idx];
5904fc612222SBenjamin Berg 			survey->filled = SURVEY_INFO_TIME |
5905fc612222SBenjamin Berg 					 SURVEY_INFO_TIME_BUSY |
5906fc612222SBenjamin Berg 					 SURVEY_INFO_TIME_RX |
5907fc612222SBenjamin Berg 					 SURVEY_INFO_TIME_TX;
5908fc612222SBenjamin Berg 			survey->time = info->time;
5909fc612222SBenjamin Berg 			survey->time_busy = info->time_busy;
5910fc612222SBenjamin Berg 			survey->time_rx = info->time_rx;
5911fc612222SBenjamin Berg 			survey->time_tx = info->time_tx;
5912fc612222SBenjamin Berg 			survey->noise = info->noise;
5913fc612222SBenjamin Berg 			if (survey->noise < 0)
5914fc612222SBenjamin Berg 				survey->filled |= SURVEY_INFO_NOISE_DBM;
5915fc612222SBenjamin Berg 
5916fc612222SBenjamin Berg 			/* Clear time so that channel is only reported once */
5917fc612222SBenjamin Berg 			info->time = 0;
5918fc612222SBenjamin Berg 
5919fc612222SBenjamin Berg 			ret = 0;
5920fc612222SBenjamin Berg 			goto out;
5921fc612222SBenjamin Berg 		}
5922fc612222SBenjamin Berg 	}
5923fc612222SBenjamin Berg 
5924fc612222SBenjamin Berg 	ret = -ENOENT;
5925fc612222SBenjamin Berg 
5926fc612222SBenjamin Berg out:
5927fc612222SBenjamin Berg 	mutex_unlock(&mvm->mutex);
5928fc612222SBenjamin Berg 
5929fc612222SBenjamin Berg 	return ret;
5930fc612222SBenjamin Berg }
5931fc612222SBenjamin Berg 
5932cbce62a3SMiri Korenblit int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5933e705c121SKalle Valo 			   struct survey_info *survey)
5934e705c121SKalle Valo {
5935e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5936b6e3d1baSAnjaneyulu 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
5937b6e3d1baSAnjaneyulu 					   WIDE_ID(SYSTEM_GROUP,
5938b6e3d1baSAnjaneyulu 						   SYSTEM_STATISTICS_CMD),
5939b6e3d1baSAnjaneyulu 					   IWL_FW_CMD_VER_UNKNOWN);
5940e705c121SKalle Valo 
5941e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
5942e705c121SKalle Valo 
5943280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
5944e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
5945e705c121SKalle Valo 		return -ENOENT;
5946e705c121SKalle Valo 
5947fc612222SBenjamin Berg 	/*
5948fc612222SBenjamin Berg 	 * Return the beacon stats at index zero and pass on following indices
5949fc612222SBenjamin Berg 	 * to the function returning the full survey, most likely for ACS
5950fc612222SBenjamin Berg 	 * (Automatic Channel Selection).
5951fc612222SBenjamin Berg 	 */
5952fc612222SBenjamin Berg 	if (idx > 0)
5953fc612222SBenjamin Berg 		return iwl_mvm_mac_get_acs_survey(mvm, idx - 1, survey);
5954fc612222SBenjamin Berg 
59553f312651SJohannes Berg 	guard(mvm)(mvm);
5956e705c121SKalle Valo 
5957aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
59583f312651SJohannes Berg 		int ret = iwl_mvm_request_statistics(mvm, false);
59593f312651SJohannes Berg 
5960e705c121SKalle Valo 		if (ret)
59613f312651SJohannes Berg 			return ret;
5962e705c121SKalle Valo 	}
5963e705c121SKalle Valo 
5964b6e3d1baSAnjaneyulu 	survey->filled = SURVEY_INFO_TIME_RX |
5965b6e3d1baSAnjaneyulu 			 SURVEY_INFO_TIME_TX;
5966e705c121SKalle Valo 
5967e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
5968e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
5969e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
5970e705c121SKalle Valo 
5971e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
5972e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
5973e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
5974e705c121SKalle Valo 
5975b6e3d1baSAnjaneyulu 	/* the new fw api doesn't support the following fields */
5976b6e3d1baSAnjaneyulu 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
59773f312651SJohannes Berg 		return 0;
5978b6e3d1baSAnjaneyulu 
5979b6e3d1baSAnjaneyulu 	survey->filled |= SURVEY_INFO_TIME |
5980b6e3d1baSAnjaneyulu 			  SURVEY_INFO_TIME_SCAN;
5981b6e3d1baSAnjaneyulu 	survey->time = mvm->accu_radio_stats.on_time_rf +
5982b6e3d1baSAnjaneyulu 		       mvm->radio_stats.on_time_rf;
5983b6e3d1baSAnjaneyulu 	do_div(survey->time, USEC_PER_MSEC);
5984b6e3d1baSAnjaneyulu 
5985e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
5986e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
5987e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
5988e705c121SKalle Valo 
59893f312651SJohannes Berg 	return 0;
5990e705c121SKalle Valo }
5991e705c121SKalle Valo 
5992ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
5993ed780545SJohannes Berg {
599482cdbd11SMiri Korenblit 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
59957138763eSJohannes Berg 	u32 gi_ltf;
599682cdbd11SMiri Korenblit 
599782cdbd11SMiri Korenblit 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
5998ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
5999ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
6000ed780545SJohannes Berg 		break;
6001ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
6002ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
6003ed780545SJohannes Berg 		break;
6004ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
6005ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
6006ed780545SJohannes Berg 		break;
6007ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
6008ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
6009ed780545SJohannes Berg 		break;
601023dcee94SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_320:
601123dcee94SJohannes Berg 		rinfo->bw = RATE_INFO_BW_320;
601223dcee94SJohannes Berg 		break;
6013ed780545SJohannes Berg 	}
6014ed780545SJohannes Berg 
601582cdbd11SMiri Korenblit 	if (format == RATE_MCS_CCK_MSK ||
601682cdbd11SMiri Korenblit 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
601782cdbd11SMiri Korenblit 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
601882cdbd11SMiri Korenblit 
601982cdbd11SMiri Korenblit 		/* add the offset needed to get to the legacy ofdm indices */
602082cdbd11SMiri Korenblit 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
602182cdbd11SMiri Korenblit 			rate += IWL_FIRST_OFDM_RATE;
602282cdbd11SMiri Korenblit 
602382cdbd11SMiri Korenblit 		switch (rate) {
602482cdbd11SMiri Korenblit 		case IWL_RATE_1M_INDEX:
602582cdbd11SMiri Korenblit 			rinfo->legacy = 10;
602682cdbd11SMiri Korenblit 			break;
602782cdbd11SMiri Korenblit 		case IWL_RATE_2M_INDEX:
602882cdbd11SMiri Korenblit 			rinfo->legacy = 20;
602982cdbd11SMiri Korenblit 			break;
603082cdbd11SMiri Korenblit 		case IWL_RATE_5M_INDEX:
603182cdbd11SMiri Korenblit 			rinfo->legacy = 55;
603282cdbd11SMiri Korenblit 			break;
603382cdbd11SMiri Korenblit 		case IWL_RATE_11M_INDEX:
603482cdbd11SMiri Korenblit 			rinfo->legacy = 110;
603582cdbd11SMiri Korenblit 			break;
603682cdbd11SMiri Korenblit 		case IWL_RATE_6M_INDEX:
603782cdbd11SMiri Korenblit 			rinfo->legacy = 60;
603882cdbd11SMiri Korenblit 			break;
603982cdbd11SMiri Korenblit 		case IWL_RATE_9M_INDEX:
604082cdbd11SMiri Korenblit 			rinfo->legacy = 90;
604182cdbd11SMiri Korenblit 			break;
604282cdbd11SMiri Korenblit 		case IWL_RATE_12M_INDEX:
604382cdbd11SMiri Korenblit 			rinfo->legacy = 120;
604482cdbd11SMiri Korenblit 			break;
604582cdbd11SMiri Korenblit 		case IWL_RATE_18M_INDEX:
604682cdbd11SMiri Korenblit 			rinfo->legacy = 180;
604782cdbd11SMiri Korenblit 			break;
604882cdbd11SMiri Korenblit 		case IWL_RATE_24M_INDEX:
604982cdbd11SMiri Korenblit 			rinfo->legacy = 240;
605082cdbd11SMiri Korenblit 			break;
605182cdbd11SMiri Korenblit 		case IWL_RATE_36M_INDEX:
605282cdbd11SMiri Korenblit 			rinfo->legacy = 360;
605382cdbd11SMiri Korenblit 			break;
605482cdbd11SMiri Korenblit 		case IWL_RATE_48M_INDEX:
605582cdbd11SMiri Korenblit 			rinfo->legacy = 480;
605682cdbd11SMiri Korenblit 			break;
605782cdbd11SMiri Korenblit 		case IWL_RATE_54M_INDEX:
605882cdbd11SMiri Korenblit 			rinfo->legacy = 540;
605982cdbd11SMiri Korenblit 		}
606082cdbd11SMiri Korenblit 		return;
606182cdbd11SMiri Korenblit 	}
606282cdbd11SMiri Korenblit 
6063ed780545SJohannes Berg 	rinfo->nss = u32_get_bits(rate_n_flags,
606482cdbd11SMiri Korenblit 				  RATE_MCS_NSS_MSK) + 1;
606582cdbd11SMiri Korenblit 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
606682cdbd11SMiri Korenblit 		RATE_HT_MCS_INDEX(rate_n_flags) :
606782cdbd11SMiri Korenblit 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
606882cdbd11SMiri Korenblit 
60697138763eSJohannes Berg 	if (rate_n_flags & RATE_MCS_SGI_MSK)
60707138763eSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
60717138763eSJohannes Berg 
60727138763eSJohannes Berg 	switch (format) {
607323dcee94SJohannes Berg 	case RATE_MCS_EHT_MSK:
607423dcee94SJohannes Berg 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
607523dcee94SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
607623dcee94SJohannes Berg 		break;
60777138763eSJohannes Berg 	case RATE_MCS_HE_MSK:
60787138763eSJohannes Berg 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
6079ed780545SJohannes Berg 
6080ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
6081ed780545SJohannes Berg 
608282cdbd11SMiri Korenblit 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
6083ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
6084ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
6085ed780545SJohannes Berg 		}
6086ed780545SJohannes Berg 
608782cdbd11SMiri Korenblit 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
608882cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_SU:
608982cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_EXT_SU:
6090ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
6091ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6092ed780545SJohannes Berg 			else if (gi_ltf == 2)
6093ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
609482cdbd11SMiri Korenblit 			else if (gi_ltf == 3)
6095ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
609682cdbd11SMiri Korenblit 			else
609782cdbd11SMiri Korenblit 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6098ed780545SJohannes Berg 			break;
609982cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_MU:
6100ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
6101ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6102ed780545SJohannes Berg 			else if (gi_ltf == 2)
6103ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6104ed780545SJohannes Berg 			else
6105ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6106ed780545SJohannes Berg 			break;
610782cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_TRIG:
6108ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
6109ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6110ed780545SJohannes Berg 			else
6111ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6112ed780545SJohannes Berg 			break;
6113ed780545SJohannes Berg 		}
6114ed780545SJohannes Berg 
6115ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
6116ed780545SJohannes Berg 			rinfo->he_dcm = 1;
61177138763eSJohannes Berg 		break;
61187138763eSJohannes Berg 	case RATE_MCS_HT_MSK:
611982cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
61207138763eSJohannes Berg 		break;
61217138763eSJohannes Berg 	case RATE_MCS_VHT_MSK:
612282cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
61237138763eSJohannes Berg 		break;
6124ed780545SJohannes Berg 	}
6125ed780545SJohannes Berg }
6126ed780545SJohannes Berg 
6127cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
6128e705c121SKalle Valo 				struct ieee80211_vif *vif,
6129e705c121SKalle Valo 				struct ieee80211_sta *sta,
6130e705c121SKalle Valo 				struct station_info *sinfo)
6131e705c121SKalle Valo {
6132e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6133e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6134e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
6135b6e3d1baSAnjaneyulu 	int i;
6136e705c121SKalle Valo 
6137c8ee33e1SGregory Greenman 	if (mvmsta->deflink.avg_energy) {
6138c8ee33e1SGregory Greenman 		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
613922d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
6140988b5968SSara Sharon 	}
6141988b5968SSara Sharon 
6142ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
6143c8ee33e1SGregory Greenman 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
6144ed780545SJohannes Berg 
6145ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
6146ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6147ed780545SJohannes Berg 	}
6148ed780545SJohannes Berg 
6149e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
6150e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
6151e705c121SKalle Valo 		return;
6152e705c121SKalle Valo 
6153f276e20bSJohannes Berg 	if (!vif->cfg.assoc)
6154e705c121SKalle Valo 		return;
6155e705c121SKalle Valo 
61563f312651SJohannes Berg 	guard(mvm)(mvm);
6157e705c121SKalle Valo 
6158c8ee33e1SGregory Greenman 	if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
61593f312651SJohannes Berg 		return;
6160e705c121SKalle Valo 
6161e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
61623f312651SJohannes Berg 		return;
6163e705c121SKalle Valo 
6164b6e3d1baSAnjaneyulu 	sinfo->rx_beacon = 0;
6165b6e3d1baSAnjaneyulu 	for_each_mvm_vif_valid_link(mvmvif, i)
6166b6e3d1baSAnjaneyulu 		sinfo->rx_beacon += mvmvif->link[i]->beacon_stats.num_beacons +
6167b6e3d1baSAnjaneyulu 			mvmvif->link[i]->beacon_stats.accu_num_beacons;
6168b6e3d1baSAnjaneyulu 
616922d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
6170650cadb7SGregory Greenman 	if (mvmvif->deflink.beacon_stats.avg_signal) {
6171e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
6172650cadb7SGregory Greenman 		sinfo->rx_beacon_signal_avg =
6173650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.avg_signal;
617422d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
6175e705c121SKalle Valo 	}
6176e705c121SKalle Valo }
6177e705c121SKalle Valo 
6178119c2a13SMordechay Goodstein static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
6179119c2a13SMordechay Goodstein 					    struct ieee80211_vif *vif,
6180119c2a13SMordechay Goodstein 					    const  struct ieee80211_mlme_event *mlme)
6181119c2a13SMordechay Goodstein {
61821a81bddfSMordechay Goodstein 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
61831a81bddfSMordechay Goodstein 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
6184119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6185119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
6186119c2a13SMordechay Goodstein 				       NULL);
6187119c2a13SMordechay Goodstein 		return;
6188119c2a13SMordechay Goodstein 	}
6189119c2a13SMordechay Goodstein 
6190119c2a13SMordechay Goodstein 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
6191119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6192119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_DEASSOC,
6193119c2a13SMordechay Goodstein 				       NULL);
6194119c2a13SMordechay Goodstein 		return;
6195119c2a13SMordechay Goodstein 	}
6196119c2a13SMordechay Goodstein }
6197119c2a13SMordechay Goodstein 
6198e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
6199e705c121SKalle Valo 					struct ieee80211_vif *vif,
6200e705c121SKalle Valo 					const struct ieee80211_event *event)
6201e705c121SKalle Valo {
62024c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
6203e705c121SKalle Valo 	do {								\
62044c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
6205e705c121SKalle Valo 			break;						\
62067174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
6207e705c121SKalle Valo 	} while (0)
6208e705c121SKalle Valo 
6209e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
6210e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
6211e705c121SKalle Valo 
6212119c2a13SMordechay Goodstein 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
6213119c2a13SMordechay Goodstein 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
6214119c2a13SMordechay Goodstein 		return;
6215119c2a13SMordechay Goodstein 	}
6216119c2a13SMordechay Goodstein 
62176c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
62186c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
62196c042d75SSara Sharon 	if (!trig)
6220e705c121SKalle Valo 		return;
6221e705c121SKalle Valo 
6222e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
6223e705c121SKalle Valo 
6224e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
6225e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
62264c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
6227e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
6228e705c121SKalle Valo 					    event->u.mlme.reason);
6229e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
62304c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
6231e705c121SKalle Valo 					   "ASSOC TIMEOUT");
6232e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
6233e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
62344c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
6235e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
6236e705c121SKalle Valo 					   event->u.mlme.reason);
6237e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
62384c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
6239e705c121SKalle Valo 					   "AUTH TIMEOUT");
6240e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
62414c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
6242e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
6243e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
62444c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
6245e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
6246e705c121SKalle Valo 	}
6247e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
6248e705c121SKalle Valo }
6249e705c121SKalle Valo 
6250e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
6251e705c121SKalle Valo 					  struct ieee80211_vif *vif,
6252e705c121SKalle Valo 					  const struct ieee80211_event *event)
6253e705c121SKalle Valo {
6254e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
6255e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
6256e705c121SKalle Valo 
62576c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
62586c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
62596c042d75SSara Sharon 	if (!trig)
6260e705c121SKalle Valo 		return;
6261e705c121SKalle Valo 
6262e705c121SKalle Valo 	ba_trig = (void *)trig->data;
6263e705c121SKalle Valo 
6264e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
6265e705c121SKalle Valo 		return;
6266e705c121SKalle Valo 
62677174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
6268e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
6269e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
6270e705c121SKalle Valo 				event->u.ba.ssn);
6271e705c121SKalle Valo }
6272e705c121SKalle Valo 
6273cbce62a3SMiri Korenblit void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
6274e705c121SKalle Valo 				struct ieee80211_vif *vif,
6275e705c121SKalle Valo 				const struct ieee80211_event *event)
6276e705c121SKalle Valo {
6277e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6278e705c121SKalle Valo 
6279e705c121SKalle Valo 	switch (event->type) {
6280e705c121SKalle Valo 	case MLME_EVENT:
6281e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
6282e705c121SKalle Valo 		break;
6283e705c121SKalle Valo 	case BAR_RX_EVENT:
6284e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
6285e705c121SKalle Valo 		break;
6286e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
6287528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
6288528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
6289e705c121SKalle Valo 		break;
6290e705c121SKalle Valo 	default:
6291e705c121SKalle Valo 		break;
6292e705c121SKalle Valo 	}
6293e705c121SKalle Valo }
6294e705c121SKalle Valo 
629587f690f5SShaul Triebitz #define SYNC_RX_QUEUE_TIMEOUT (HZ)
6296d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
62975e1688ceSJohannes Berg 				     enum iwl_mvm_rxq_notif_type type,
62985e1688ceSJohannes Berg 				     bool sync,
62995e1688ceSJohannes Berg 				     const void *data, u32 size)
63000636b938SSara Sharon {
63015e1688ceSJohannes Berg 	struct {
63025e1688ceSJohannes Berg 		struct iwl_rxq_sync_cmd cmd;
63035e1688ceSJohannes Berg 		struct iwl_mvm_internal_rxq_notif notif;
63045e1688ceSJohannes Berg 	} __packed cmd = {
63055e1688ceSJohannes Berg 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
63065e1688ceSJohannes Berg 		.cmd.count =
63075e1688ceSJohannes Berg 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
63085e1688ceSJohannes Berg 				    size),
63095e1688ceSJohannes Berg 		.notif.type = type,
63105e1688ceSJohannes Berg 		.notif.sync = sync,
63115e1688ceSJohannes Berg 	};
63125e1688ceSJohannes Berg 	struct iwl_host_cmd hcmd = {
63135e1688ceSJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
63145e1688ceSJohannes Berg 		.data[0] = &cmd,
63155e1688ceSJohannes Berg 		.len[0] = sizeof(cmd),
63165e1688ceSJohannes Berg 		.data[1] = data,
63175e1688ceSJohannes Berg 		.len[1] = size,
63184d08c0b3SJohannes Berg 		.flags = CMD_SEND_IN_RFKILL | (sync ? 0 : CMD_ASYNC),
63195e1688ceSJohannes Berg 	};
63200636b938SSara Sharon 	int ret;
63210636b938SSara Sharon 
63225e1688ceSJohannes Berg 	/* size must be a multiple of DWORD */
63235e1688ceSJohannes Berg 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
63245e1688ceSJohannes Berg 		return;
63250636b938SSara Sharon 
6326cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
63270636b938SSara Sharon 		return;
63280636b938SSara Sharon 
63295e1688ceSJohannes Berg 	if (sync) {
63305e1688ceSJohannes Berg 		cmd.notif.cookie = mvm->queue_sync_cookie;
63312f7a04c7SJohannes Berg 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6332a2113cc4SNaftali Goldstein 	}
6333d0ff5d22SSara Sharon 
63345e1688ceSJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
63350636b938SSara Sharon 	if (ret) {
63360636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
63370636b938SSara Sharon 		goto out;
63380636b938SSara Sharon 	}
6339d0ff5d22SSara Sharon 
63405e1688ceSJohannes Berg 	if (sync) {
63413c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
63423a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
63432f7a04c7SJohannes Berg 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
63440dd2b42cSJohannes Berg 					 iwl_mvm_is_radio_hw_killed(mvm),
634587f690f5SShaul Triebitz 					 SYNC_RX_QUEUE_TIMEOUT);
63460dd2b42cSJohannes Berg 		WARN_ONCE(!ret && !iwl_mvm_is_radio_hw_killed(mvm),
6347f4eedfd8SJohannes Berg 			  "queue sync: failed to sync, state is 0x%lx, cookie %d\n",
6348f4eedfd8SJohannes Berg 			  mvm->queue_sync_state,
6349f4eedfd8SJohannes Berg 			  mvm->queue_sync_cookie);
63506ad04359SJohannes Berg 	}
63510636b938SSara Sharon 
63520636b938SSara Sharon out:
63535e1688ceSJohannes Berg 	if (sync) {
63542f7a04c7SJohannes Berg 		mvm->queue_sync_state = 0;
63550636b938SSara Sharon 		mvm->queue_sync_cookie++;
63560636b938SSara Sharon 	}
63575f8a3561SJohannes Berg }
63580636b938SSara Sharon 
6359cbce62a3SMiri Korenblit void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
63600636b938SSara Sharon {
63610636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
63620636b938SSara Sharon 
63633f312651SJohannes Berg 	guard(mvm)(mvm);
63645e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
63650636b938SSara Sharon }
63660636b938SSara Sharon 
6367cbce62a3SMiri Korenblit int
6368b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6369b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
6370b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
6371b73f9a4aSJohannes Berg {
6372b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6373b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6374b73f9a4aSJohannes Berg 
6375b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6376b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6377b73f9a4aSJohannes Berg 		return -EINVAL;
6378b73f9a4aSJohannes Berg 
6379b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
6380b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
6381b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
6382b73f9a4aSJohannes Berg 
6383b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6384b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6385b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
6386b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
6387b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6388b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6389b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6390b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6391b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6392b73f9a4aSJohannes Berg 
6393b73f9a4aSJohannes Berg 	return 0;
6394b73f9a4aSJohannes Berg }
6395b73f9a4aSJohannes Berg 
6396cbce62a3SMiri Korenblit int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6397fc36ffdaSJohannes Berg 		       struct cfg80211_pmsr_request *request)
6398fc36ffdaSJohannes Berg {
6399fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6400fc36ffdaSJohannes Berg 
64013f312651SJohannes Berg 	guard(mvm)(mvm);
64023f312651SJohannes Berg 	return iwl_mvm_ftm_start(mvm, vif, request);
6403fc36ffdaSJohannes Berg }
6404fc36ffdaSJohannes Berg 
6405cbce62a3SMiri Korenblit void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6406fc36ffdaSJohannes Berg 			struct cfg80211_pmsr_request *request)
6407fc36ffdaSJohannes Berg {
6408fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6409fc36ffdaSJohannes Berg 
64103f312651SJohannes Berg 	guard(mvm)(mvm);
6411fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
6412fc36ffdaSJohannes Berg }
6413fc36ffdaSJohannes Berg 
6414438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6415438af969SSara Sharon {
6416438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
6417438af969SSara Sharon 
6418438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
6419438af969SSara Sharon 		return false;
6420438af969SSara Sharon 
6421438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6422438af969SSara Sharon }
6423438af969SSara Sharon 
6424438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6425438af969SSara Sharon 				      struct sk_buff *head,
6426438af969SSara Sharon 				      struct sk_buff *skb)
6427438af969SSara Sharon {
6428438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6429438af969SSara Sharon 
6430438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
6431438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
6432438af969SSara Sharon 		return false;
6433438af969SSara Sharon 
6434438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
6435438af969SSara Sharon 		return true;
6436438af969SSara Sharon 
6437438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6438438af969SSara Sharon }
6439438af969SSara Sharon 
6440be8897e2SAvraham Stern int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6441cf85123aSAvraham Stern 			     struct ieee80211_vif *vif,
6442cf85123aSAvraham Stern 			     struct cfg80211_set_hw_timestamp *hwts)
6443cf85123aSAvraham Stern {
6444cf85123aSAvraham Stern 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6445cf85123aSAvraham Stern 	u32 protocols = 0;
6446cf85123aSAvraham Stern 
6447cf85123aSAvraham Stern 	/* HW timestamping is only supported for a specific station */
6448cf85123aSAvraham Stern 	if (!hwts->macaddr)
6449cf85123aSAvraham Stern 		return -EOPNOTSUPP;
6450cf85123aSAvraham Stern 
6451cf85123aSAvraham Stern 	if (hwts->enable)
6452cf85123aSAvraham Stern 		protocols =
6453cf85123aSAvraham Stern 			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6454cf85123aSAvraham Stern 
64553f312651SJohannes Berg 	guard(mvm)(mvm);
64563f312651SJohannes Berg 	return iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6457cf85123aSAvraham Stern }
6458cf85123aSAvraham Stern 
6459e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
6460e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
6461cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6462e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
6463e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
64644ea1ed1dSEmmanuel Grumbach 	.set_antenna = iwl_mvm_op_set_antenna,
6465e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
6466e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
6467e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
6468e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
6469e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
6470e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
6471e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
6472e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
6473e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
6474e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
6475e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
6476e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6477e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6478e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
6479e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
6480e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6481e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6482e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6483e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
6484e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
6485e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
64866b1259d1SJohannes Berg 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6487e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6488e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
6489a6cc6ccbSJohannes Berg 	.flush_sta = iwl_mvm_mac_flush_sta,
6490e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
6491e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6492e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
6493e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
6494e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
6495e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
6496e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
6497e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
6498e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
6499e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6500e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6501e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6502e705c121SKalle Valo 
6503ae7ba17bSShaul Triebitz 	.start_ap = iwl_mvm_start_ap,
6504ae7ba17bSShaul Triebitz 	.stop_ap = iwl_mvm_stop_ap,
6505ae7ba17bSShaul Triebitz 	.join_ibss = iwl_mvm_start_ibss,
6506ae7ba17bSShaul Triebitz 	.leave_ibss = iwl_mvm_stop_ibss,
6507e705c121SKalle Valo 
65082f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
65092f0282dbSJohannes Berg 
6510e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
6511e705c121SKalle Valo 
6512e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
65132f876f91SJohannes Berg 	.pre_channel_switch = iwl_mvm_mac_pre_channel_switch,
6514e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
651579221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
6516c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6517e705c121SKalle Valo 
6518e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6519e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6520e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6521e705c121SKalle Valo 
6522e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
6523e705c121SKalle Valo 
65240636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
65250636b938SSara Sharon 
6526e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
6527e705c121SKalle Valo 
6528e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6529e705c121SKalle Valo 	/* look at d3.c */
6530e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
6531e705c121SKalle Valo 	.resume = iwl_mvm_resume,
6532e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
6533e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
6534e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
6535e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6536e705c121SKalle Valo #endif
6537e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6538e705c121SKalle Valo #endif
6539e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
6540e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
6541b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6542fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
6543fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
6544b73f9a4aSJohannes Berg 
6545438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6546177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
6547c36235acSJohannes Berg 	.vif_add_debugfs = iwl_mvm_vif_add_debugfs,
65483f244876SBenjamin Berg 	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
6549177a11cfSGregory Greenman #endif
6550cf85123aSAvraham Stern 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6551e705c121SKalle Valo };
6552