xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision 0d2fc8821a7d667180ce27732697105db843a1b9)
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"
25e705c121SKalle Valo #include "iwl-eeprom-parse.h"
26e705c121SKalle Valo #include "iwl-phy-db.h"
27e705c121SKalle Valo #include "testmode.h"
28d962f9b1SJohannes Berg #include "fw/error-dump.h"
29e705c121SKalle Valo #include "iwl-prph.h"
30e705c121SKalle Valo #include "iwl-nvm-parse.h"
31cf85123aSAvraham Stern #include "time-sync.h"
32e705c121SKalle Valo 
33e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
34e705c121SKalle Valo 	{
35e705c121SKalle Valo 		.max = 1,
36e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
37e705c121SKalle Valo 	},
38e705c121SKalle Valo 	{
39e705c121SKalle Valo 		.max = 1,
40e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
41e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
42e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
43e705c121SKalle Valo 	},
44e705c121SKalle Valo 	{
45e705c121SKalle Valo 		.max = 1,
46e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
47e705c121SKalle Valo 	},
48e705c121SKalle Valo };
49e705c121SKalle Valo 
50e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
51e705c121SKalle Valo 	{
52e705c121SKalle Valo 		.num_different_channels = 2,
53e705c121SKalle Valo 		.max_interfaces = 3,
54e705c121SKalle Valo 		.limits = iwl_mvm_limits,
55e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
56e705c121SKalle Valo 	},
57e705c121SKalle Valo };
58e705c121SKalle Valo 
59fc36ffdaSJohannes Berg static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
60fc36ffdaSJohannes Berg 	.max_peers = IWL_MVM_TOF_MAX_APS,
61fc36ffdaSJohannes Berg 	.report_ap_tsf = 1,
62fc36ffdaSJohannes Berg 	.randomize_mac_addr = 1,
63fc36ffdaSJohannes Berg 
64fc36ffdaSJohannes Berg 	.ftm = {
65fc36ffdaSJohannes Berg 		.supported = 1,
66fc36ffdaSJohannes Berg 		.asap = 1,
67fc36ffdaSJohannes Berg 		.non_asap = 1,
68fc36ffdaSJohannes Berg 		.request_lci = 1,
69fc36ffdaSJohannes Berg 		.request_civicloc = 1,
706815e3d0SAvraham Stern 		.trigger_based = 1,
716815e3d0SAvraham Stern 		.non_trigger_based = 1,
72fc36ffdaSJohannes Berg 		.max_bursts_exponent = -1, /* all supported */
73fc36ffdaSJohannes Berg 		.max_ftms_per_burst = 0, /* no limits */
74fc36ffdaSJohannes Berg 		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
75fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_20) |
76fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_40) |
778a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_80) |
788a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_160),
79fc36ffdaSJohannes Berg 		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
80fc36ffdaSJohannes Berg 			     BIT(NL80211_PREAMBLE_HT) |
816815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_VHT) |
826815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_HE),
83fc36ffdaSJohannes Berg 	},
84fc36ffdaSJohannes Berg };
85fc36ffdaSJohannes Berg 
866569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
87c56e00a3SJohannes Berg 				 enum set_key_cmd cmd,
88c56e00a3SJohannes Berg 				 struct ieee80211_vif *vif,
89c56e00a3SJohannes Berg 				 struct ieee80211_sta *sta,
90c56e00a3SJohannes Berg 				 struct ieee80211_key_conf *key);
91c56e00a3SJohannes Berg 
92e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
93e705c121SKalle Valo {
94e705c121SKalle Valo 	int i;
95e705c121SKalle Valo 
96e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
97e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
98e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
99e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
100e705c121SKalle Valo 	}
101e705c121SKalle Valo }
102e705c121SKalle Valo 
103e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
104e705c121SKalle Valo 						  const char *alpha2,
105e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
106e705c121SKalle Valo 						  bool *changed)
107e705c121SKalle Valo {
108e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
109e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
110e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
111e9b63341SAbhishek Naik 	struct iwl_mcc_update_resp_v8 *resp;
112e27c506aSGil Adam 	u8 resp_ver;
113e705c121SKalle Valo 
114e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
115e705c121SKalle Valo 
116e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
117e705c121SKalle Valo 
118e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
119e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
120e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
121e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
1226d19a5ebSEmmanuel Grumbach 		resp = NULL;
123e705c121SKalle Valo 		goto out;
124e705c121SKalle Valo 	}
125e705c121SKalle Valo 
12682715ac7SEmmanuel Grumbach 	if (changed) {
12782715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
12882715ac7SEmmanuel Grumbach 
12982715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
13082715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
13182715ac7SEmmanuel Grumbach 	}
132e27c506aSGil Adam 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
133e27c506aSGil Adam 					   MCC_UPDATE_CMD, 0);
134e27c506aSGil Adam 	IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
135e705c121SKalle Valo 
136e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
137e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
138e705c121SKalle Valo 				      resp->channels,
13977e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
1402763bba6SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info),
141*0d2fc882SMukesh Sisodiya 				      le32_to_cpu(resp->cap), resp_ver,
142*0d2fc882SMukesh 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 &&
362b3366330SJohannes Berg 	    !iwlwifi_mod_params.disable_11be)
3630120e6b3SGregory Greenman 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
3640120e6b3SGregory Greenman 
365ef2b47b8SJohannes Berg 	/* With MLD FW API, it tracks timing by itself,
366ef2b47b8SJohannes Berg 	 * no need for any timing from the host
367ef2b47b8SJohannes Berg 	 */
368ef2b47b8SJohannes Berg 	if (!mvm->mld_api_is_used)
369ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
370ef2b47b8SJohannes Berg 
371ef2b47b8SJohannes Berg 	/* We should probably have this, but mac80211
372ef2b47b8SJohannes Berg 	 * currently doesn't support it for MLO.
373ef2b47b8SJohannes Berg 	 */
374ef2b47b8SJohannes Berg 	if (!(hw->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
375ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
376ef2b47b8SJohannes Berg 
377cfb21b11SJohannes Berg 	/*
378cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
379cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
380cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
381cfb21b11SJohannes Berg 	 * assert EDC.
382cfb21b11SJohannes Berg 	 *
383cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
384cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
385cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
386cfb21b11SJohannes Berg 	 * devices.
387cfb21b11SJohannes Berg 	 */
3887d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
389438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
390438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
391ecaf71deSGregory Greenman 
3924243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
393ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
394ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
395ecaf71deSGregory Greenman 	}
396ecaf71deSGregory Greenman 
39729fa9a98SEmmanuel Grumbach 	/* We want to use the mac80211's reorder buffer for 9000 */
39829fa9a98SEmmanuel Grumbach 	if (iwl_mvm_has_new_rx_api(mvm) &&
39929fa9a98SEmmanuel Grumbach 	    mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_9000)
400b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
401960f864bSJohannes Berg 
402960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
403960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
40465e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
405960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
406960f864bSJohannes Berg 		/*
407960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
408960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
409960f864bSJohannes Berg 		 * for station powersave
410960f864bSJohannes Berg 		 */
411960f864bSJohannes Berg 		return -EINVAL;
412960f864bSJohannes Berg 	}
413e705c121SKalle Valo 
41480938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
41580938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
41680938abcSJohannes Berg 
4172d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
4182d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
4192d7cf549SJohannes Berg 
420366fc672SJohannes Berg 	hw->queues = IEEE80211_NUM_ACS;
421e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
422e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
423e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
424e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
425e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
426371a17edSJohannes Berg 
427371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
428371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
429371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
430371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
431371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
432371a17edSJohannes Berg 
4334243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
4349f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
4359f66a397SGregory Greenman 
436e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
437e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
438438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
439e705c121SKalle Valo 
4408e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
441e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
442e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
443e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
444e705c121SKalle Valo 
4452a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4462a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4472a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
4482a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4492a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4502a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
4512a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4522a53d166SAyala Beker 	}
4532a53d166SAyala Beker 
454f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
455f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
456f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
457f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
458f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
459f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
460f4bfdc5eSEmmanuel Grumbach 
461e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
462f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
463e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
4648e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4658e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4668e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
4678e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4688e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4698e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
4708e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4718e160ab8SAyala Beker 	}
472e705c121SKalle Valo 
473ef2e7a51SIlan Peer 	wiphy_ext_feature_set(hw->wiphy,
474ef2e7a51SIlan Peer 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
475d16b96b5SJohannes Berg 	wiphy_ext_feature_set(hw->wiphy,
476d16b96b5SJohannes Berg 			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
477ef2e7a51SIlan Peer 
478b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
479fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
480b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
481b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
482fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
483fc36ffdaSJohannes Berg 	}
484b73f9a4aSJohannes Berg 
485a5de7de7SJohannes Berg 	if (sec_key_ver &&
486a5de7de7SJohannes Berg 	    fw_has_capa(&mvm->fw->ucode_capa,
487a5de7de7SJohannes Berg 			IWL_UCODE_TLV_CAPA_BIGTK_TX_SUPPORT))
488a5de7de7SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
489a5de7de7SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION);
490a5de7de7SJohannes Berg 	else if (fw_has_capa(&mvm->fw->ucode_capa,
491b1fdc250SJohannes Berg 			     IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
492b1fdc250SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
493b1fdc250SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
494b1fdc250SJohannes Berg 
495cf85123aSAvraham Stern 	if (fw_has_capa(&mvm->fw->ucode_capa,
496cf85123aSAvraham Stern 			IWL_UCODE_TLV_CAPA_TIME_SYNC_BOTH_FTM_TM))
497cf85123aSAvraham Stern 		hw->wiphy->hw_timestamp_max_peers = 1;
498cf85123aSAvraham Stern 
499c289f5cdSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
500c289f5cdSJohannes Berg 			IWL_UCODE_TLV_CAPA_SPP_AMSDU_SUPPORT))
501c289f5cdSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
502c289f5cdSJohannes Berg 				      NL80211_EXT_FEATURE_SPP_AMSDU_SUPPORT);
503c289f5cdSJohannes Berg 
504e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
505e705c121SKalle Valo 	hw->wiphy->features |=
506e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
507e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
508e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
509e705c121SKalle Valo 
510e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
511e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
512e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
513cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
514e705c121SKalle Valo 
515e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
516e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
517e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
518e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
519e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
520e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
521e705c121SKalle Valo 
522e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
523e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
52433e3fd99SAlexander Wetzel 
52533e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
52633e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
52733e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
52833e3fd99SAlexander Wetzel 	 */
52933e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
53033e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
53133e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
532e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
533e47df5bdSJohannes Berg 
534e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
535e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
536e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
537e705c121SKalle Valo 	else
538e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
539e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
540e705c121SKalle Valo 
541a1910a7fSAndrei Otcheretianski 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
542a1910a7fSAndrei Otcheretianski 		wiphy_ext_feature_set(hw->wiphy,
543a1910a7fSAndrei Otcheretianski 				      NL80211_EXT_FEATURE_DFS_CONCURRENT);
544a1910a7fSAndrei Otcheretianski 
545e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
546e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
547eae94cf8SLuca Coelho 	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
548e705c121SKalle Valo 
549e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
550e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
551e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
552e705c121SKalle Valo 
553e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
5544f1847cfSJohannes Berg 	hw->max_listen_interval = IWL_MVM_CONN_LISTEN_INTERVAL;
555e705c121SKalle Valo 
556e705c121SKalle Valo 	/* Extract MAC address */
557e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
558e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
559e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
560e705c121SKalle Valo 
561e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
562e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
563e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
564e705c121SKalle Valo 
565e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
566e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
567e705c121SKalle Valo 		       ETH_ALEN);
568e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
569e705c121SKalle Valo 		hw->wiphy->n_addresses++;
570e705c121SKalle Valo 	}
571e705c121SKalle Valo 
572e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
573e705c121SKalle Valo 
574e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
575e705c121SKalle Valo 
576e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
577e705c121SKalle Valo 
578e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
579e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
580e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
581e705c121SKalle Valo 
582e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
583e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
584e705c121SKalle Valo 	else
585e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
586e705c121SKalle Valo 
58757fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
58857fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
58957fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
59057fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
59157fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
59257fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
593e705c121SKalle Valo 
594e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
595e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
596e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
597e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
59857fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
599e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
600e705c121SKalle Valo 	}
601eae94cf8SLuca Coelho 	if (fw_has_capa(&mvm->fw->ucode_capa,
602eae94cf8SLuca Coelho 			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
603eae94cf8SLuca Coelho 	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
604eae94cf8SLuca Coelho 		hw->wiphy->bands[NL80211_BAND_6GHZ] =
605eae94cf8SLuca Coelho 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
606e705c121SKalle Valo 
607e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
608e705c121SKalle Valo 
609e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
610e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
611e705c121SKalle Valo 	else
612e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
613e705c121SKalle Valo 
614ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
615e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
6165d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
617e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
618e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
619e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
620e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
621e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
622e705c121SKalle Valo 
623e705c121SKalle Valo 	/*
624e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
625e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
626e705c121SKalle Valo 	 */
627e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
628e705c121SKalle Valo 
629e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
630e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
631e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
632a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
633e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
634e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
635e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
636e705c121SKalle Valo 
637e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
638e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
639e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
640e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
641e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
642e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
643e705c121SKalle Valo 
644e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
645e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
646e705c121SKalle Valo 		hw->wiphy->features |=
647e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
648e705c121SKalle Valo 
649e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
650e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
651e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
652e705c121SKalle Valo 
653971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
6542a42aea7SNathan Errera 				  IWL_FW_CMD_VER_UNKNOWN) == 3)
6552a42aea7SNathan Errera 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
6562a42aea7SNathan Errera 
657aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
658aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
659aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
660aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
661aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
662aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
663aacf8f18SAvrahams Stern 	}
664aacf8f18SAvrahams Stern 
6658f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
666971cbe50SJohannes Berg 		u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
667773a042fSAvraham Stern 
6688f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6698f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
6708f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6718f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
6728f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6738f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
674773a042fSAvraham Stern 
675773a042fSAvraham Stern 		/* Old firmware also supports probe deferral and suppression */
676773a042fSAvraham Stern 		if (scan_ver < 15)
677773a042fSAvraham Stern 			wiphy_ext_feature_set(hw->wiphy,
678773a042fSAvraham Stern 					      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
6798f691af9SZamir, Roee 	}
6808f691af9SZamir, Roee 
6818e33f046SKrishnanand Prabhu 	hw->wiphy->iftype_ext_capab = NULL;
6828e33f046SKrishnanand Prabhu 	hw->wiphy->num_iftype_ext_capab = 0;
6838e33f046SKrishnanand Prabhu 
6847360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
6857360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
6868e33f046SKrishnanand Prabhu 		hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa;
6877360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
6888e33f046SKrishnanand Prabhu 			ARRAY_SIZE(add_iftypes_ext_capa) - 1;
689918cbf39SSara Sharon 
690918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
691918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
6927360f99eSEmmanuel Grumbach 	}
6937360f99eSEmmanuel Grumbach 
6948e33f046SKrishnanand Prabhu 	if (iwl_fw_lookup_cmd_ver(mvm->fw,
6958e33f046SKrishnanand Prabhu 				  WIDE_ID(DATA_PATH_GROUP,
6968e33f046SKrishnanand Prabhu 					  WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
6978e33f046SKrishnanand Prabhu 				  IWL_FW_CMD_VER_UNKNOWN) >= 1) {
6988e33f046SKrishnanand Prabhu 		IWL_DEBUG_INFO(mvm->trans, "Timing measurement supported\n");
6998e33f046SKrishnanand Prabhu 
7008e33f046SKrishnanand Prabhu 		if (!hw->wiphy->iftype_ext_capab) {
7018e33f046SKrishnanand Prabhu 			hw->wiphy->num_iftype_ext_capab = 1;
7028e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa +
7038e33f046SKrishnanand Prabhu 				ARRAY_SIZE(add_iftypes_ext_capa) - 1;
7048e33f046SKrishnanand Prabhu 		} else {
7058e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa + 1;
7068e33f046SKrishnanand Prabhu 		}
7078e33f046SKrishnanand Prabhu 	}
7088e33f046SKrishnanand Prabhu 
7098b720901SAvraham Stern 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(LOCATION_GROUP,
7108b720901SAvraham Stern 						   TOF_RANGE_REQ_CMD),
7118b720901SAvraham Stern 				  IWL_FW_CMD_VER_UNKNOWN) >= 11) {
7128b720901SAvraham Stern 		wiphy_ext_feature_set(hw->wiphy,
7138b720901SAvraham Stern 				      NL80211_EXT_FEATURE_PROT_RANGE_NEGO_AND_MEASURE);
7144d951e26SIlan Peer 
7154d951e26SIlan Peer 		if (fw_has_capa(&mvm->fw->ucode_capa,
7164d951e26SIlan Peer 				IWL_UCODE_TLV_CAPA_SECURE_LTF_SUPPORT))
7174d951e26SIlan Peer 			wiphy_ext_feature_set(hw->wiphy,
7184d951e26SIlan Peer 					      NL80211_EXT_FEATURE_SECURE_LTF);
7198b720901SAvraham Stern 	}
7208b720901SAvraham Stern 
721e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
722e705c121SKalle Valo 
723e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
7243f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
725e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
726e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
727e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
728e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
729e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
730e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
731e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
732e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
733e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
734e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
735e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
736e705c121SKalle Valo 
737e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
738e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
739e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
7405d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
7415d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
742e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
743e705c121SKalle Valo 	}
744e705c121SKalle Valo #endif
745e705c121SKalle Valo 
746e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
747e705c121SKalle Valo 	if (ret)
748e705c121SKalle Valo 		return ret;
749e705c121SKalle Valo 
750e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
751e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
752e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
753e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
754e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
755e705c121SKalle Valo 	}
756e705c121SKalle Valo 
757e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
758e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
759e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
760e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
761e705c121SKalle Valo 	}
762e705c121SKalle Valo 
763e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
76459fa61f3SEmmanuel Grumbach 	if (!iwl_mvm_is_csum_supported(mvm))
7656772aab7SJohannes Berg 		hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
76641837ca9SEmmanuel Grumbach 
76791b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
76891b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
76991b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
77091b08c2dSAviya Erenfeld 
7719c11d8a9SShaul Triebitz 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
7729c11d8a9SShaul Triebitz 		wiphy_ext_feature_set(hw->wiphy,
7739c11d8a9SShaul Triebitz 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
7749c11d8a9SShaul Triebitz 
775bfcfdb59SEmmanuel Grumbach 	iwl_mvm_vendor_cmds_register(mvm);
776bfcfdb59SEmmanuel Grumbach 
777e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
778e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
779e8503aecSBen Greear 
780de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
781de645e89SJohannes Berg 	if (ret) {
782de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
783e705c121SKalle Valo 	}
784e705c121SKalle Valo 
785e705c121SKalle Valo 	return ret;
786e705c121SKalle Valo }
787e705c121SKalle Valo 
788df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
789df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
790df2378abSJohannes Berg {
791df2378abSJohannes Berg 	if (likely(sta)) {
792df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
793df2378abSJohannes Berg 			return;
794df2378abSJohannes Berg 	} else {
795df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
796df2378abSJohannes Berg 			return;
797df2378abSJohannes Berg 	}
798df2378abSJohannes Berg 
799df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
800df2378abSJohannes Berg }
801df2378abSJohannes Berg 
802cbce62a3SMiri Korenblit void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
803cbce62a3SMiri Korenblit 		    struct ieee80211_tx_control *control, struct sk_buff *skb)
804e705c121SKalle Valo {
805e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
806e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
807e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
808e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
809cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
810cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
81190723da6SShaul Triebitz 	u32 link_id = u32_get_bits(info->control.flags,
81290723da6SShaul Triebitz 				   IEEE80211_TX_CTRL_MLO_LINK);
81390723da6SShaul Triebitz 	struct ieee80211_sta *tmp_sta = sta;
814e705c121SKalle Valo 
815e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
816e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
817e705c121SKalle Valo 		goto drop;
818e705c121SKalle Valo 	}
819e705c121SKalle Valo 
820cfbc6c4cSSara Sharon 	if (offchannel &&
821e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
822e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
823e705c121SKalle Valo 		goto drop;
824e705c121SKalle Valo 
82510516783SJohannes Berg 	/*
82610516783SJohannes Berg 	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
82710516783SJohannes Berg 	 * so we treat the others as broadcast
82810516783SJohannes Berg 	 */
82910516783SJohannes Berg 	if (ieee80211_is_mgmt(hdr->frame_control))
830e705c121SKalle Valo 		sta = NULL;
831e705c121SKalle Valo 
832dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
833cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
834cfbc6c4cSSara Sharon 	    !offchannel) {
835dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
836dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
837650cadb7SGregory Greenman 		u8 ap_sta_id = READ_ONCE(mvmvif->deflink.ap_sta_id);
838dc1aca22SAndrei Otcheretianski 
839be9ae34eSNathan Errera 		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
840dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
841dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
842dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
843dc1aca22SAndrei Otcheretianski 				goto drop;
844dc1aca22SAndrei Otcheretianski 		}
845dc1aca22SAndrei Otcheretianski 	}
846dc1aca22SAndrei Otcheretianski 
84790723da6SShaul Triebitz 	if (tmp_sta && !sta && link_id != IEEE80211_LINK_UNSPECIFIED &&
84890723da6SShaul Triebitz 	    !ieee80211_is_probe_resp(hdr->frame_control)) {
84990723da6SShaul Triebitz 		/* translate MLD addresses to LINK addresses */
85090723da6SShaul Triebitz 		struct ieee80211_link_sta *link_sta =
85190723da6SShaul Triebitz 			rcu_dereference(tmp_sta->link[link_id]);
85290723da6SShaul Triebitz 		struct ieee80211_bss_conf *link_conf =
85390723da6SShaul Triebitz 			rcu_dereference(info->control.vif->link_conf[link_id]);
85490723da6SShaul Triebitz 		struct ieee80211_mgmt *mgmt;
85590723da6SShaul Triebitz 
85690723da6SShaul Triebitz 		if (WARN_ON(!link_sta || !link_conf))
85790723da6SShaul Triebitz 			goto drop;
85890723da6SShaul Triebitz 
85990723da6SShaul Triebitz 		/* if sta is NULL, the frame is a management frame */
86090723da6SShaul Triebitz 		mgmt = (void *)hdr;
86190723da6SShaul Triebitz 		memcpy(mgmt->da, link_sta->addr, ETH_ALEN);
86290723da6SShaul Triebitz 		memcpy(mgmt->sa, link_conf->addr, ETH_ALEN);
86390723da6SShaul Triebitz 		memcpy(mgmt->bssid, link_conf->bssid, ETH_ALEN);
86490723da6SShaul Triebitz 	}
86590723da6SShaul Triebitz 
866df2378abSJohannes Berg 	iwl_mvm_tx_skb(mvm, skb, sta);
867e705c121SKalle Valo 	return;
868e705c121SKalle Valo  drop:
869e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
870e705c121SKalle Valo }
871e705c121SKalle Valo 
872cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
873e705c121SKalle Valo {
874cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
875cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
876cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
877cfbc6c4cSSara Sharon 
878fba8248eSSara Sharon 	/*
879fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
880fba8248eSSara Sharon 	 * taker all the work.
881fba8248eSSara Sharon 	 *
882fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
883fba8248eSSara Sharon 	 *
884fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
885fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
886fba8248eSSara Sharon 	 *
887fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
888fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
889fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
890fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
891fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
892fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
893fba8248eSSara Sharon 	 *
894fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
895fba8248eSSara Sharon 	 * check the queue, so do nothing.
896fba8248eSSara Sharon 	 */
897fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
898fba8248eSSara Sharon 		return;
899cfbc6c4cSSara Sharon 
900cfbc6c4cSSara Sharon 	rcu_read_lock();
901fba8248eSSara Sharon 	do {
902b58e3d43SJohannes Berg 		while (likely(!test_bit(IWL_MVM_TXQ_STATE_STOP_FULL,
903b58e3d43SJohannes Berg 					&mvmtxq->state) &&
904b58e3d43SJohannes Berg 			      !test_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT,
905b58e3d43SJohannes Berg 					&mvmtxq->state) &&
90600520b7aSEmmanuel Grumbach 			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
907cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
908cfbc6c4cSSara Sharon 
909f50d693bSSara Sharon 			if (!skb) {
910f50d693bSSara Sharon 				if (txq->sta)
911f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
912f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
913f50d693bSSara Sharon 						     txq->sta->addr,
914f50d693bSSara Sharon 						     txq->tid);
915cfbc6c4cSSara Sharon 				break;
916f50d693bSSara Sharon 			}
917cfbc6c4cSSara Sharon 
918cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
919cfbc6c4cSSara Sharon 		}
920fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
921cfbc6c4cSSara Sharon 	rcu_read_unlock();
922e705c121SKalle Valo }
923e705c121SKalle Valo 
924cbce62a3SMiri Korenblit void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
925cfbc6c4cSSara Sharon 			       struct ieee80211_txq *txq)
926e705c121SKalle Valo {
927cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
928cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
929e705c121SKalle Valo 
930923bf981SJohannes Berg 	if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
931923bf981SJohannes Berg 	    !txq->sta) {
932cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
933cfbc6c4cSSara Sharon 		return;
934cfbc6c4cSSara Sharon 	}
935cfbc6c4cSSara Sharon 
936923bf981SJohannes Berg 	/* iwl_mvm_mac_itxq_xmit() will later be called by the worker
937923bf981SJohannes Berg 	 * to handle any packets we leave on the txq now
938923bf981SJohannes Berg 	 */
939cfbc6c4cSSara Sharon 
940923bf981SJohannes Berg 	spin_lock_bh(&mvm->add_stream_lock);
941923bf981SJohannes Berg 	/* The list is being deleted only after the queue is fully allocated. */
942923bf981SJohannes Berg 	if (list_empty(&mvmtxq->list) &&
943923bf981SJohannes Berg 	    /* recheck under lock */
944923bf981SJohannes Berg 	    !test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) {
945cfbc6c4cSSara Sharon 		list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
946cfbc6c4cSSara Sharon 		schedule_work(&mvm->add_stream_wk);
947e705c121SKalle Valo 	}
948923bf981SJohannes Berg 	spin_unlock_bh(&mvm->add_stream_lock);
949923bf981SJohannes Berg }
950e705c121SKalle Valo 
951e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
952e705c121SKalle Valo 	do {								\
953e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
954e705c121SKalle Valo 			break;						\
9557174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
956e705c121SKalle Valo 	} while (0)
957e705c121SKalle Valo 
958e705c121SKalle Valo static void
959e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
960e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
961e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
962e705c121SKalle Valo {
963e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
964e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
965e705c121SKalle Valo 
9666c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
9676c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
9686c042d75SSara Sharon 	if (!trig)
969e705c121SKalle Valo 		return;
970e705c121SKalle Valo 
971e705c121SKalle Valo 	ba_trig = (void *)trig->data;
972e705c121SKalle Valo 
973e705c121SKalle Valo 	switch (action) {
974e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
975e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
976e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
977e705c121SKalle Valo 
978e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
979e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
980e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
981e705c121SKalle Valo 		break;
982e705c121SKalle Valo 		}
983e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
984e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
985e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
986e705c121SKalle Valo 				 sta->addr, tid);
987e705c121SKalle Valo 		break;
988e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
989e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
990e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
991e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
992e705c121SKalle Valo 		break;
993e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
994e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
995e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
996e705c121SKalle Valo 				 sta->addr, tid);
997e705c121SKalle Valo 		break;
998e705c121SKalle Valo 	default:
999e705c121SKalle Valo 		break;
1000e705c121SKalle Valo 	}
1001e705c121SKalle Valo }
1002e705c121SKalle Valo 
1003cbce62a3SMiri Korenblit int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
1004e705c121SKalle Valo 			     struct ieee80211_vif *vif,
100550ea05efSSara Sharon 			     struct ieee80211_ampdu_params *params)
1006e705c121SKalle Valo {
1007e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1008e705c121SKalle Valo 	int ret;
100950ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
101050ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
101150ea05efSSara Sharon 	u16 tid = params->tid;
101250ea05efSSara Sharon 	u16 *ssn = &params->ssn;
1013514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
1014bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
101510b2b201SSara Sharon 	u16 timeout = params->timeout;
1016e705c121SKalle Valo 
1017e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
1018e705c121SKalle Valo 		     sta->addr, tid, action);
1019e705c121SKalle Valo 
1020e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
1021e705c121SKalle Valo 		return -EACCES;
1022e705c121SKalle Valo 
1023e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1024e705c121SKalle Valo 
1025e705c121SKalle Valo 	switch (action) {
1026e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
1027650cadb7SGregory Greenman 		if (iwl_mvm_vif_from_mac80211(vif)->deflink.ap_sta_id ==
1028c8ee33e1SGregory Greenman 		    iwl_mvm_sta_from_mac80211(sta)->deflink.sta_id) {
1029b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
1030b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
1031b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
1032b0ffe455SJohannes Berg 
1033b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
1034b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
1035b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
1036b0ffe455SJohannes Berg 		}
1037e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
1038e705c121SKalle Valo 			ret = -EINVAL;
1039e705c121SKalle Valo 			break;
1040e705c121SKalle Valo 		}
104110b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
104210b2b201SSara Sharon 					 timeout);
1043e705c121SKalle Valo 		break;
1044e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
104510b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
104610b2b201SSara Sharon 					 timeout);
1047e705c121SKalle Valo 		break;
1048e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
1049e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
1050e705c121SKalle Valo 			ret = -EINVAL;
1051e705c121SKalle Valo 			break;
1052e705c121SKalle Valo 		}
1053e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1054e705c121SKalle Valo 		break;
1055e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
1056e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1057e705c121SKalle Valo 		break;
1058e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1059e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1060e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1061e705c121SKalle Valo 		break;
1062e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1063bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1064bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
1065e705c121SKalle Valo 		break;
1066e705c121SKalle Valo 	default:
1067e705c121SKalle Valo 		WARN_ON_ONCE(1);
1068e705c121SKalle Valo 		ret = -EINVAL;
1069e705c121SKalle Valo 		break;
1070e705c121SKalle Valo 	}
1071e705c121SKalle Valo 
1072e705c121SKalle Valo 	if (!ret) {
1073e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
1074e705c121SKalle Valo 
1075e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1076e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1077e705c121SKalle Valo 
1078e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1079e705c121SKalle Valo 					    rx_ba_ssn, action);
1080e705c121SKalle Valo 	}
1081e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1082e705c121SKalle Valo 
1083e705c121SKalle Valo 	return ret;
1084e705c121SKalle Valo }
1085e705c121SKalle Valo 
1086e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1087e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1088e705c121SKalle Valo {
1089e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1090e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1091bf976c81SJohannes Berg 	struct iwl_probe_resp_data *probe_data;
109279faae3aSGregory Greenman 	unsigned int link_id;
1093e705c121SKalle Valo 
1094e705c121SKalle Valo 	mvmvif->uploaded = false;
1095e705c121SKalle Valo 
1096e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1097e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1098e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1099e705c121SKalle Valo 
1100e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
1101828c79d9SEmmanuel Grumbach 	mvmvif->ap_sta = NULL;
110279faae3aSGregory Greenman 
110379faae3aSGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
110479faae3aSGregory Greenman 		mvmvif->link[link_id]->ap_sta_id = IWL_MVM_INVALID_STA;
110579faae3aSGregory Greenman 		mvmvif->link[link_id]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
110679faae3aSGregory Greenman 		mvmvif->link[link_id]->phy_ctxt = NULL;
1107bf976c81SJohannes Berg 		mvmvif->link[link_id]->active = 0;
1108d615ea32SJohannes Berg 		mvmvif->link[link_id]->igtk = NULL;
110979faae3aSGregory Greenman 	}
1110bf976c81SJohannes Berg 
1111bf976c81SJohannes Berg 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1112bf976c81SJohannes Berg 					       lockdep_is_held(&mvm->mutex));
1113bf976c81SJohannes Berg 	if (probe_data)
1114bf976c81SJohannes Berg 		kfree_rcu(probe_data, rcu_head);
1115bf976c81SJohannes Berg 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1116e705c121SKalle Valo }
1117e705c121SKalle Valo 
1118e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1119e705c121SKalle Valo {
1120fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1121e705c121SKalle Valo 
11229bf13beeSJohannes Berg 	mvm->cur_aid = 0;
11239bf13beeSJohannes Berg 
1124e705c121SKalle Valo 	mvm->scan_status = 0;
1125e705c121SKalle Valo 	mvm->ps_disabled = false;
1126b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1127e705c121SKalle Valo 
1128e705c121SKalle Valo 	/* just in case one was running */
1129305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1130e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1131e705c121SKalle Valo 
1132fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1133fc36ffdaSJohannes Berg 
1134e705c121SKalle Valo 	/*
1135e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1136e705c121SKalle Valo 	 * gone down during the HW restart
1137e705c121SKalle Valo 	 */
1138e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1139e705c121SKalle Valo 
1140e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1141e705c121SKalle Valo 
1142e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
11439c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1144e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1145e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1146e705c121SKalle Valo 
1147e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1148e705c121SKalle Valo 
1149e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
11507174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1151baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1152fb40cd9dSMiri Korenblit #ifdef CONFIG_IWLWIFI_DEBUGFS
1153fb40cd9dSMiri Korenblit 	mvm->beacon_inject_active = false;
1154fb40cd9dSMiri Korenblit #endif
1155e705c121SKalle Valo 
1156e705c121SKalle Valo 	/* keep statistics ticking */
1157e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1158e705c121SKalle Valo }
1159e705c121SKalle Valo 
1160e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1161e705c121SKalle Valo {
1162e705c121SKalle Valo 	int ret;
1163e705c121SKalle Valo 
1164e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1165e705c121SKalle Valo 
11666d19a5ebSEmmanuel Grumbach 	ret = iwl_mvm_mei_get_ownership(mvm);
11676d19a5ebSEmmanuel Grumbach 	if (ret)
11686d19a5ebSEmmanuel Grumbach 		return ret;
11696d19a5ebSEmmanuel Grumbach 
11706d19a5ebSEmmanuel Grumbach 	if (mvm->mei_nvm_data) {
11716d19a5ebSEmmanuel Grumbach 		/* We got the NIC, we can now free the MEI NVM data */
11726d19a5ebSEmmanuel Grumbach 		kfree(mvm->mei_nvm_data);
11736d19a5ebSEmmanuel Grumbach 		mvm->mei_nvm_data = NULL;
11746d19a5ebSEmmanuel Grumbach 
11756d19a5ebSEmmanuel Grumbach 		/*
11766d19a5ebSEmmanuel Grumbach 		 * We can't free the nvm_data we allocated based on the SAP
11776d19a5ebSEmmanuel Grumbach 		 * data because we registered to cfg80211 with the channels
11786d19a5ebSEmmanuel Grumbach 		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
11796d19a5ebSEmmanuel Grumbach 		 * just in order to be able free it later.
11806d19a5ebSEmmanuel Grumbach 		 * NULLify nvm_data so that we will read the NVM from the
11816d19a5ebSEmmanuel Grumbach 		 * firmware this time.
11826d19a5ebSEmmanuel Grumbach 		 */
11836d19a5ebSEmmanuel Grumbach 		mvm->temp_nvm_data = mvm->nvm_data;
11846d19a5ebSEmmanuel Grumbach 		mvm->nvm_data = NULL;
11856d19a5ebSEmmanuel Grumbach 	}
11866d19a5ebSEmmanuel Grumbach 
1187bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1188bf8b286fSJohannes Berg 		/*
1189bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1190bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1191bf8b286fSJohannes Berg 		 */
1192bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1193bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1194e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1195e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1196a42b2af3SLuca Coelho 	}
1197e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1198e705c121SKalle Valo 
1199b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1200b108d8c7SShahar S Matityahu 			       NULL);
1201b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1202b108d8c7SShahar S Matityahu 			       NULL);
1203da2eb669SSara Sharon 
1204e8fe3b41SIlan Peer 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1205e8fe3b41SIlan Peer 
1206e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1207e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1208e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1209e705c121SKalle Valo 		 * would do.
1210e705c121SKalle Valo 		 */
1211e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1212e705c121SKalle Valo 	}
1213e705c121SKalle Valo 
1214e705c121SKalle Valo 	return ret;
1215e705c121SKalle Valo }
1216e705c121SKalle Valo 
1217cbce62a3SMiri Korenblit int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1218e705c121SKalle Valo {
1219e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1220e705c121SKalle Valo 	int ret;
1221e705c121SKalle Valo 
1222e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
12235283dd67SMordechay Goodstein 
12245283dd67SMordechay Goodstein 	/* we are starting the mac not in error flow, and restart is enabled */
12255283dd67SMordechay Goodstein 	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
12265283dd67SMordechay Goodstein 	    iwlwifi_mod_params.fw_restart) {
12275283dd67SMordechay Goodstein 		/*
12285283dd67SMordechay Goodstein 		 * This will prevent mac80211 recovery flows to trigger during
12295283dd67SMordechay Goodstein 		 * init failures
12305283dd67SMordechay Goodstein 		 */
12315283dd67SMordechay Goodstein 		set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
12325283dd67SMordechay Goodstein 	}
12335283dd67SMordechay Goodstein 
1234e705c121SKalle Valo 	ret = __iwl_mvm_mac_start(mvm);
12355283dd67SMordechay Goodstein 	clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
12365283dd67SMordechay Goodstein 
1237e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1238e705c121SKalle Valo 
12397ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
12407ce1f215SEmmanuel Grumbach 
1241e705c121SKalle Valo 	return ret;
1242e705c121SKalle Valo }
1243e705c121SKalle Valo 
1244e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1245e705c121SKalle Valo {
1246e705c121SKalle Valo 	int ret;
1247e705c121SKalle Valo 
1248e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1249e705c121SKalle Valo 
1250e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
12514d4183c4SEmmanuel Grumbach 
1252e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1253e705c121SKalle Valo 	if (ret)
1254e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1255e705c121SKalle Valo 			ret);
1256e705c121SKalle Valo 
1257f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1258f130bb75SMordechay Goodstein 
1259e705c121SKalle Valo 	/*
1260e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1261e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1262e705c121SKalle Valo 	 */
1263e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1264e705c121SKalle Valo 
1265e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1266e705c121SKalle Valo }
1267e705c121SKalle Valo 
1268cbce62a3SMiri Korenblit void iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1269e705c121SKalle Valo 				   enum ieee80211_reconfig_type reconfig_type)
1270e705c121SKalle Valo {
1271e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1272e705c121SKalle Valo 
1273e705c121SKalle Valo 	switch (reconfig_type) {
1274e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1275e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1276e705c121SKalle Valo 		break;
1277e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1278e705c121SKalle Valo 		break;
1279e705c121SKalle Valo 	}
1280e705c121SKalle Valo }
1281e705c121SKalle Valo 
1282e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1283e705c121SKalle Valo {
1284e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1285e705c121SKalle Valo 
1286b68bd2e3SIlan Peer 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1287b68bd2e3SIlan Peer 
1288e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1289e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1290e705c121SKalle Valo 	 */
1291e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1292e705c121SKalle Valo 
1293e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1294e705c121SKalle Valo 
12951724fc78SEmmanuel Grumbach 	if (!iwl_mvm_has_new_station_api(mvm->fw))
1296f327236dSSharon 		iwl_mvm_rm_aux_sta(mvm);
1297f327236dSSharon 
1298fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1299e705c121SKalle Valo 
1300e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1301e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1302e705c121SKalle Valo 
1303e705c121SKalle Valo 	/*
1304155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1305155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1306155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1307e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1308e705c121SKalle Valo 	 * HW restart was requested.
1309e705c121SKalle Valo 	 */
1310155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1311155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1312155f7e04SEmmanuel Grumbach 			       &mvm->status))
1313e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1314e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1315e705c121SKalle Valo 
1316e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1317e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1318e705c121SKalle Valo 	 */
1319e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1320e705c121SKalle Valo 		int i;
1321e705c121SKalle Valo 
1322e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1323e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1324e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1325e705c121SKalle Valo 				      i))
1326e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1327e705c121SKalle Valo 		}
1328e705c121SKalle Valo 	}
1329e705c121SKalle Valo }
1330e705c121SKalle Valo 
1331cbce62a3SMiri Korenblit void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1332e705c121SKalle Valo {
1333e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1334e705c121SKalle Valo 
1335e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
133624afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1337771147b0SJohannes Berg 
1338771147b0SJohannes Berg 	/*
1339771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1340771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1341771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
13427174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1343771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1344771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1345771147b0SJohannes Berg 	 */
1346771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1347771147b0SJohannes Berg 
1348d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
134969e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1350e705c121SKalle Valo 
1351f9084775SNathan Errera 	/*
1352f9084775SNathan Errera 	 * The work item could be running or queued if the
1353f9084775SNathan Errera 	 * ROC time event stops just as we get here.
1354f9084775SNathan Errera 	 */
1355f9084775SNathan Errera 	flush_work(&mvm->roc_done_wk);
1356f9084775SNathan Errera 
13577ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
13587ce1f215SEmmanuel Grumbach 
1359e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1360e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1361e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1362e705c121SKalle Valo 
1363e705c121SKalle Valo 	/*
1364e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1365e705c121SKalle Valo 	 * discover that its list is now empty.
1366e705c121SKalle Valo 	 */
1367e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1368fdccafadSMiri Korenblit 	wiphy_work_cancel(hw->wiphy, &mvm->async_handlers_wiphy_wk);
1369e705c121SKalle Valo }
1370e705c121SKalle Valo 
13711ab26632SMiri Korenblit struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1372e705c121SKalle Valo {
1373e705c121SKalle Valo 	u16 i;
1374e705c121SKalle Valo 
1375e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1376e705c121SKalle Valo 
1377e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1378e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1379e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1380e705c121SKalle Valo 
1381e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1382e705c121SKalle Valo 	return NULL;
1383e705c121SKalle Valo }
1384e705c121SKalle Valo 
1385f551d013SGregory Greenman int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1386e705c121SKalle Valo 			 s16 tx_power)
1387e705c121SKalle Valo {
1388971cbe50SJohannes Berg 	u32 cmd_id = REDUCE_TX_POWER_CMD;
13890791c2fcSHaim Dreyfuss 	int len;
1390216cdfb5SLuca Coelho 	struct iwl_dev_tx_power_cmd cmd = {
1391216cdfb5SLuca Coelho 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1392216cdfb5SLuca Coelho 		.common.mac_context_id =
1393e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1394216cdfb5SLuca Coelho 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1395e705c121SKalle Valo 	};
1396971cbe50SJohannes Berg 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1397e80bfd11SMordechay Goodstein 					   IWL_FW_CMD_VER_UNKNOWN);
1398e705c121SKalle Valo 
1399e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1400216cdfb5SLuca Coelho 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1401e705c121SKalle Valo 
1402b0aa02b3SAyala Barazani 	if (cmd_ver == 7)
1403b0aa02b3SAyala Barazani 		len = sizeof(cmd.v7);
1404b0aa02b3SAyala Barazani 	else if (cmd_ver == 6)
1405fbb7957dSLuca Coelho 		len = sizeof(cmd.v6);
1406fbb7957dSLuca Coelho 	else if (fw_has_api(&mvm->fw->ucode_capa,
14070791c2fcSHaim Dreyfuss 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
14080791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
14090791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
14100791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
14110791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
14120791c2fcSHaim Dreyfuss 	else
1413216cdfb5SLuca Coelho 		len = sizeof(cmd.v3);
1414216cdfb5SLuca Coelho 
1415216cdfb5SLuca Coelho 	/* all structs have the same common part, add it */
1416216cdfb5SLuca Coelho 	len += sizeof(cmd.common);
1417e705c121SKalle Valo 
1418971cbe50SJohannes Berg 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1419e705c121SKalle Valo }
1420e705c121SKalle Valo 
142103117f30SMiri Korenblit int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1422a469a593SEmmanuel Grumbach 				struct ieee80211_vif *vif,
1423a469a593SEmmanuel Grumbach 				struct ieee80211_bss_conf *link_conf)
1424f6780614SSara Sharon {
1425f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1426f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1427f6780614SSara Sharon 	int ret;
1428f6780614SSara Sharon 
1429f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1430f6780614SSara Sharon 
1431f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1432f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
14333723c7c5SEmmanuel Grumbach 		unsigned int link_id = link_conf->link_id;
14343723c7c5SEmmanuel Grumbach 		u8 ap_sta_id = mvmvif->link[link_id]->ap_sta_id;
1435f6780614SSara Sharon 
1436f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
14373723c7c5SEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm, ap_sta_id);
1438f6780614SSara Sharon 
1439f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1440f6780614SSara Sharon 			ret = -EIO;
1441f6780614SSara Sharon 			goto out_unlock;
1442f6780614SSara Sharon 		}
1443f6780614SSara Sharon 
1444f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
144503117f30SMiri Korenblit 		if (mvm->mld_api_is_used)
144603117f30SMiri Korenblit 			iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
144703117f30SMiri Korenblit 		else
1448f6780614SSara Sharon 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1449f6780614SSara Sharon 
14500202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
14510202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
145287f5b5f2SJohannes Berg 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
1453f6780614SSara Sharon 			if (ret)
1454f6780614SSara Sharon 				goto out_unlock;
1455f6780614SSara Sharon 
1456f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1457f6780614SSara Sharon 		}
14580202bcf0SEmmanuel Grumbach 	}
1459f6780614SSara Sharon 
1460f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1461f6780614SSara Sharon 
1462f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1463f6780614SSara Sharon 
1464f6780614SSara Sharon out_unlock:
1465caf46377SSara Sharon 	if (mvmvif->csa_failed)
1466caf46377SSara Sharon 		ret = -EIO;
1467f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1468f6780614SSara Sharon 
1469f6780614SSara Sharon 	return ret;
1470f6780614SSara Sharon }
1471f6780614SSara Sharon 
1472cbce62a3SMiri Korenblit void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1473f6780614SSara Sharon 				  struct ieee80211_vif *vif)
1474f6780614SSara Sharon {
1475f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1476f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1477f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1478f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1479f6780614SSara Sharon 							  mvmvif->color)),
1480f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1481f6780614SSara Sharon 	};
1482f6780614SSara Sharon 
1483ad12b231SNathan Errera 	/*
1484ad12b231SNathan Errera 	 * In the new flow since FW is in charge of the timing,
1485ad12b231SNathan Errera 	 * if driver has canceled the channel switch he will receive the
1486ad12b231SNathan Errera 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1487ad12b231SNathan Errera 	 */
1488ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1489ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1490ad12b231SNathan Errera 		return;
1491ad12b231SNathan Errera 
1492f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1493f6780614SSara Sharon 
1494f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
149558ddd9b6SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
149658ddd9b6SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
149758ddd9b6SEmmanuel Grumbach 		iwl_mvm_remove_csa_period(mvm, vif);
149858ddd9b6SEmmanuel Grumbach 	else
1499f6780614SSara Sharon 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1500f6780614SSara Sharon 					     WIDE_ID(MAC_CONF_GROUP,
1501f6780614SSara Sharon 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1502f6780614SSara Sharon 					     0, sizeof(cmd), &cmd));
1503caf46377SSara Sharon 	mvmvif->csa_failed = true;
1504f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1505f6780614SSara Sharon 
1506a469a593SEmmanuel Grumbach 	/* If we're here, we can't support MLD */
1507a469a593SEmmanuel Grumbach 	iwl_mvm_post_channel_switch(hw, vif, &vif->bss_conf);
1508f6780614SSara Sharon }
1509f6780614SSara Sharon 
15101ab26632SMiri Korenblit void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1511f6780614SSara Sharon {
1512f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1513f6780614SSara Sharon 	struct ieee80211_vif *vif;
1514f6780614SSara Sharon 
1515f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1516f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1517f6780614SSara Sharon 
151870162580SShaul Triebitz 	/* Trigger disconnect (should clear the CSA state) */
1519a469a593SEmmanuel Grumbach 	ieee80211_chswitch_done(vif, false, 0);
1520f6780614SSara Sharon }
1521f6780614SSara Sharon 
15225abf3154SMordechay Goodstein static u8
15235abf3154SMordechay Goodstein iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
15245abf3154SMordechay Goodstein {
15255abf3154SMordechay Goodstein 	int data_start;
15265abf3154SMordechay Goodstein 	int control_start;
15275abf3154SMordechay Goodstein 	int bw;
15285abf3154SMordechay Goodstein 
15295abf3154SMordechay Goodstein 	if (chandef->width == NL80211_CHAN_WIDTH_320)
15305abf3154SMordechay Goodstein 		bw = 320;
15315abf3154SMordechay Goodstein 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
15325abf3154SMordechay Goodstein 		bw = 160;
15335abf3154SMordechay Goodstein 	else
15345abf3154SMordechay Goodstein 		return 0;
15355abf3154SMordechay Goodstein 
15365abf3154SMordechay Goodstein 	/* data is bw wide so the start is half the width */
15375abf3154SMordechay Goodstein 	data_start = chandef->center_freq1 - bw / 2;
15385abf3154SMordechay Goodstein 	/* control is 20Mhz width */
15395abf3154SMordechay Goodstein 	control_start = chandef->chan->center_freq - 10;
15405abf3154SMordechay Goodstein 
15415abf3154SMordechay Goodstein 	return (control_start - data_start) / 80;
15425abf3154SMordechay Goodstein }
15435abf3154SMordechay Goodstein 
15441be4858eSJohannes Berg static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
15451be4858eSJohannes Berg 					 struct ieee80211_vif *vif)
1546e705c121SKalle Valo {
1547e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
15481be4858eSJohannes Berg 	int ret;
15491ab26632SMiri Korenblit 
15501ab26632SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
1551e705c121SKalle Valo 
15521be4858eSJohannes Berg 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
15531be4858eSJohannes Berg 	if (ret) {
15541be4858eSJohannes Berg 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
15551be4858eSJohannes Berg 		return ret;
15561be4858eSJohannes Berg 	}
15571be4858eSJohannes Berg 
15581be4858eSJohannes Berg 	/* Only queue for this station is the mcast queue,
15591be4858eSJohannes Berg 	 * which shouldn't be in TFD mask anyway
15601be4858eSJohannes Berg 	 */
15611be4858eSJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
15621be4858eSJohannes Berg 					vif->type,
15631be4858eSJohannes Berg 					IWL_STA_MULTICAST);
15641be4858eSJohannes Berg }
15651be4858eSJohannes Berg 
15661be4858eSJohannes Berg static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
15671be4858eSJohannes Berg 				     struct ieee80211_vif *vif)
15681be4858eSJohannes Berg {
15691be4858eSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
15701be4858eSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
15711be4858eSJohannes Berg 	int ret;
1572b6e3d1baSAnjaneyulu 	int i;
15731be4858eSJohannes Berg 
15741be4858eSJohannes Berg 	mutex_lock(&mvm->mutex);
15751be4858eSJohannes Berg 
1576e705c121SKalle Valo 	mvmvif->mvm = mvm;
1577650cadb7SGregory Greenman 
1578650cadb7SGregory Greenman 	/* the first link always points to the default one */
1579650cadb7SGregory Greenman 	mvmvif->link[0] = &mvmvif->deflink;
1580e705c121SKalle Valo 
1581e705c121SKalle Valo 	/*
1582e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1583e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1584e705c121SKalle Valo 	 * don't really have to check the types.
1585e705c121SKalle Valo 	 */
1586e705c121SKalle Valo 
1587e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1588e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1589b6e3d1baSAnjaneyulu 		for_each_mvm_vif_valid_link(mvmvif, i)
1590b6e3d1baSAnjaneyulu 			mvmvif->link[i]->beacon_stats.accu_num_beacons +=
1591b6e3d1baSAnjaneyulu 				mvmvif->link[i]->beacon_stats.num_beacons;
1592e705c121SKalle Valo 
1593e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
15941be4858eSJohannes Berg 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
15951be4858eSJohannes Berg 	if (ret)
15961be4858eSJohannes Berg 		goto out;
1597e705c121SKalle Valo 
1598698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1599698478c4SSara Sharon 
16001be4858eSJohannes Berg 	/* Currently not much to do for NAN */
16011be4858eSJohannes Berg 	if (vif->type == NL80211_IFTYPE_NAN) {
16021be4858eSJohannes Berg 		ret = 0;
16031be4858eSJohannes Berg 		goto out;
16041be4858eSJohannes Berg 	}
16051be4858eSJohannes Berg 
1606e705c121SKalle Valo 	/*
1607e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1608e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1609e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1610e705c121SKalle Valo 	 * only after the binding.
1611e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1612e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1613e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1614e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1615e705c121SKalle Valo 	 * allocating resources :)
1616e705c121SKalle Valo 	 */
1617e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1618e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
16193a3ef394SBenjamin Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1620c36235acSJohannes Berg 			iwl_mvm_vif_dbgfs_add_link(mvm, vif);
16211be4858eSJohannes Berg 		ret = 0;
16221be4858eSJohannes Berg 		goto out;
16231ab26632SMiri Korenblit 	}
16241ab26632SMiri Korenblit 
16251ab26632SMiri Korenblit 	mvmvif->features |= hw->netdev_features;
1626650cadb7SGregory Greenman 
1627e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1628e705c121SKalle Valo 	if (ret)
162998c0de7bSMiri Korenblit 		goto out_unlock;
1630e705c121SKalle Valo 
1631e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1632e705c121SKalle Valo 	if (ret)
1633e705c121SKalle Valo 		goto out_remove_mac;
1634e705c121SKalle Valo 
1635e705c121SKalle Valo 	/* beacon filtering */
163687f5b5f2SJohannes Berg 	ret = iwl_mvm_disable_beacon_filter(mvm, vif);
1637e705c121SKalle Valo 	if (ret)
1638e705c121SKalle Valo 		goto out_remove_mac;
1639e705c121SKalle Valo 
1640e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1641e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1642e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1643e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1644e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1645e705c121SKalle Valo 	}
1646e705c121SKalle Valo 
164784ef7cbeSIlan Peer 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1648e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1649e705c121SKalle Valo 
1650b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1651f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1652f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1653b0ffe455SJohannes Berg 
16545abf3154SMordechay Goodstein 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1655baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
16565abf3154SMordechay Goodstein 		mvm->monitor_p80 =
16576092077aSJohannes Berg 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chanreq.oper);
16585abf3154SMordechay Goodstein 	}
1659baf41bc3SShaul Triebitz 
16603a3ef394SBenjamin Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1661c36235acSJohannes Berg 		iwl_mvm_vif_dbgfs_add_link(mvm, vif);
16626d19a5ebSEmmanuel Grumbach 
16636d19a5ebSEmmanuel Grumbach 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
16646d19a5ebSEmmanuel Grumbach 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
16656d19a5ebSEmmanuel Grumbach 	    !mvm->csme_vif && mvm->mei_registered) {
16666d19a5ebSEmmanuel Grumbach 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
16676d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
16686d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = vif;
16696d19a5ebSEmmanuel Grumbach 	}
16706d19a5ebSEmmanuel Grumbach 
16711ab26632SMiri Korenblit out:
16721ab26632SMiri Korenblit 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
16731ab26632SMiri Korenblit 		     vif->type == NL80211_IFTYPE_ADHOC))
16741ab26632SMiri Korenblit 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
16751ab26632SMiri Korenblit 
1676e705c121SKalle Valo 	goto out_unlock;
1677e705c121SKalle Valo 
1678e705c121SKalle Valo  out_remove_mac:
1679650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
1680e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1681e705c121SKalle Valo  out_unlock:
1682e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1683e705c121SKalle Valo 
1684e705c121SKalle Valo 	return ret;
1685e705c121SKalle Valo }
1686e705c121SKalle Valo 
1687cb145863SJohannes Berg void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1688e705c121SKalle Valo 				 struct ieee80211_vif *vif)
1689e705c121SKalle Valo {
1690e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1691e705c121SKalle Valo 		/*
1692e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1693e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1694e705c121SKalle Valo 		 * queue are sent in ROC session.
1695e705c121SKalle Valo 		 */
1696e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1697e705c121SKalle Valo 	}
1698e705c121SKalle Valo }
1699e705c121SKalle Valo 
170060efeca1SMiri Korenblit /* This function is doing the common part of removing the interface for
170160efeca1SMiri Korenblit  * both - MLD and non-MLD modes. Returns true if removing the interface
170260efeca1SMiri Korenblit  * is done
170360efeca1SMiri Korenblit  */
1704cb145863SJohannes Berg static bool iwl_mvm_mac_remove_interface_common(struct ieee80211_hw *hw,
1705e705c121SKalle Valo 						struct ieee80211_vif *vif)
1706e705c121SKalle Valo {
1707e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1708e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
170986e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1710e705c121SKalle Valo 
1711e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1712e705c121SKalle Valo 
1713b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1714b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1715b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1716b0ffe455SJohannes Berg 
1717e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1718e705c121SKalle Valo 
17196d19a5ebSEmmanuel Grumbach 	if (vif == mvm->csme_vif) {
17206d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(NULL);
17216d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = NULL;
17226d19a5ebSEmmanuel Grumbach 	}
17236d19a5ebSEmmanuel Grumbach 
1724650cadb7SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
172586e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
1726650cadb7SGregory Greenman 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
172786e177d8SGregory Greenman 	if (probe_data)
172886e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
172986e177d8SGregory Greenman 
1730e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1731e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1732e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1733e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1734e705c121SKalle Valo 	}
1735e705c121SKalle Valo 
1736b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1737b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1738b73f9a4aSJohannes Berg 
1739c36235acSJohannes Berg 	iwl_mvm_vif_dbgfs_rm_link(mvm, vif);
1740e705c121SKalle Valo 
1741e705c121SKalle Valo 	/*
1742e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1743e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1744e705c121SKalle Valo 	 */
1745e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1746e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1747e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1748e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1749e705c121SKalle Valo 			mvm->noa_vif = NULL;
1750e705c121SKalle Valo 			mvm->noa_duration = 0;
1751e705c121SKalle Valo 		}
1752e705c121SKalle Valo #endif
175360efeca1SMiri Korenblit 		return true;
1754e705c121SKalle Valo 	}
1755e705c121SKalle Valo 
175660efeca1SMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
175760efeca1SMiri Korenblit 	return false;
175860efeca1SMiri Korenblit }
175960efeca1SMiri Korenblit 
176060efeca1SMiri Korenblit static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
176160efeca1SMiri Korenblit 					 struct ieee80211_vif *vif)
176260efeca1SMiri Korenblit {
176360efeca1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
176460efeca1SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
176560efeca1SMiri Korenblit 
176660efeca1SMiri Korenblit 	if (iwl_mvm_mac_remove_interface_common(hw, vif))
176760efeca1SMiri Korenblit 		goto out;
176860efeca1SMiri Korenblit 
176984ef7cbeSIlan Peer 	/* Before the interface removal, mac80211 would cancel the ROC, and the
177084ef7cbeSIlan Peer 	 * ROC worker would be scheduled if needed. The worker would be flushed
177184ef7cbeSIlan Peer 	 * in iwl_mvm_prepare_mac_removal() and thus at this point there is no
177284ef7cbeSIlan Peer 	 * binding etc. so nothing needs to be done here.
177384ef7cbeSIlan Peer 	 */
1774e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
177584ef7cbeSIlan Peer 		if (mvmvif->deflink.phy_ctxt) {
1776650cadb7SGregory Greenman 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1777650cadb7SGregory Greenman 			mvmvif->deflink.phy_ctxt = NULL;
1778e705c121SKalle Valo 		}
177984ef7cbeSIlan Peer 		mvm->p2p_device_vif = NULL;
178084ef7cbeSIlan Peer 	}
1781e705c121SKalle Valo 
1782e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1783e705c121SKalle Valo 
1784698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1785698478c4SSara Sharon 
1786baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1787baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1788baf41bc3SShaul Triebitz 
178960efeca1SMiri Korenblit out:
179060efeca1SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP ||
179160efeca1SMiri Korenblit 	    vif->type == NL80211_IFTYPE_ADHOC) {
1792650cadb7SGregory Greenman 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
179360efeca1SMiri Korenblit 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1794e705c121SKalle Valo 	}
1795e705c121SKalle Valo 
1796e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1797e705c121SKalle Valo }
1798e705c121SKalle Valo 
1799e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1800e705c121SKalle Valo 	struct iwl_mvm *mvm;
1801e705c121SKalle Valo 	int port_id;
1802e705c121SKalle Valo };
1803e705c121SKalle Valo 
1804e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1805e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1806e705c121SKalle Valo {
1807e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1808e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1809e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
181097bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
181197bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
181297bce57bSLuca Coelho 		.flags = CMD_ASYNC,
181397bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
181497bce57bSLuca Coelho 	};
1815e705c121SKalle Valo 	int ret, len;
1816e705c121SKalle Valo 
1817e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1818e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1819e705c121SKalle Valo 		return;
1820e705c121SKalle Valo 
1821e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1822f276e20bSJohannes Berg 	    !vif->cfg.assoc)
1823e705c121SKalle Valo 		return;
1824e705c121SKalle Valo 
1825e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1826e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1827e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1828e705c121SKalle Valo 
182997bce57bSLuca Coelho 	hcmd.len[0] = len;
183097bce57bSLuca Coelho 	hcmd.data[0] = cmd;
183197bce57bSLuca Coelho 
183297bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1833e705c121SKalle Valo 	if (ret)
1834e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1835e705c121SKalle Valo }
1836e705c121SKalle Valo 
1837e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1838e705c121SKalle Valo {
1839e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1840e705c121SKalle Valo 		.mvm = mvm,
1841e705c121SKalle Valo 	};
1842db66abeeSJohannes Berg 	int ret;
1843e705c121SKalle Valo 
1844e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1845e705c121SKalle Valo 
1846e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1847e705c121SKalle Valo 		return;
1848e705c121SKalle Valo 
1849e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1850e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1851e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1852db66abeeSJohannes Berg 
1853db66abeeSJohannes Berg 	/*
1854db66abeeSJohannes Berg 	 * Send a (synchronous) ech command so that we wait for the
1855db66abeeSJohannes Berg 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
1856db66abeeSJohannes Berg 	 * the interface iterator. Otherwise, we might get here over
1857db66abeeSJohannes Berg 	 * and over again (by userspace just sending a lot of these)
1858db66abeeSJohannes Berg 	 * and the CPU can send them faster than the firmware can
1859db66abeeSJohannes Berg 	 * process them.
1860db66abeeSJohannes Berg 	 * Note that the CPU is still faster - but with this we'll
1861db66abeeSJohannes Berg 	 * actually send fewer commands overall because the CPU will
1862db66abeeSJohannes Berg 	 * not schedule the work in mac80211 as frequently if it's
1863db66abeeSJohannes Berg 	 * still running when rescheduled (possibly multiple times).
1864db66abeeSJohannes Berg 	 */
1865db66abeeSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1866db66abeeSJohannes Berg 	if (ret)
1867db66abeeSJohannes Berg 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
1868e705c121SKalle Valo }
1869e705c121SKalle Valo 
1870cbce62a3SMiri Korenblit u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1871e705c121SKalle Valo 			      struct netdev_hw_addr_list *mc_list)
1872e705c121SKalle Valo {
1873e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1874e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1875e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1876e705c121SKalle Valo 	int addr_count;
1877e705c121SKalle Valo 	bool pass_all;
1878e705c121SKalle Valo 	int len;
1879e705c121SKalle Valo 
1880e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1881e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1882e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1883e705c121SKalle Valo 	if (pass_all)
1884e705c121SKalle Valo 		addr_count = 0;
1885e705c121SKalle Valo 
1886e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1887e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1888e705c121SKalle Valo 	if (!cmd)
1889e705c121SKalle Valo 		return 0;
1890e705c121SKalle Valo 
1891e705c121SKalle Valo 	if (pass_all) {
1892e705c121SKalle Valo 		cmd->pass_all = 1;
1893e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1894e705c121SKalle Valo 	}
1895e705c121SKalle Valo 
1896e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1897e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1898e705c121SKalle Valo 				   cmd->count, addr->addr);
1899e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1900e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1901e705c121SKalle Valo 		cmd->count++;
1902e705c121SKalle Valo 	}
1903e705c121SKalle Valo 
1904e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1905e705c121SKalle Valo }
1906e705c121SKalle Valo 
1907cbce62a3SMiri Korenblit void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1908e705c121SKalle Valo 			      unsigned int changed_flags,
1909cbce62a3SMiri Korenblit 			      unsigned int *total_flags, u64 multicast)
1910e705c121SKalle Valo {
1911e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1912e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1913e705c121SKalle Valo 
1914e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1915e705c121SKalle Valo 
1916e705c121SKalle Valo 	/* replace previous configuration */
1917e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1918e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1919e705c121SKalle Valo 
1920e705c121SKalle Valo 	if (!cmd)
1921e705c121SKalle Valo 		goto out;
1922e705c121SKalle Valo 
192361e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
192461e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
192561e7d91bSLuca Coelho 
192661e7d91bSLuca Coelho 	if (cmd->pass_all)
192761e7d91bSLuca Coelho 		cmd->count = 0;
192861e7d91bSLuca Coelho 
1929e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1930e705c121SKalle Valo out:
1931e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1932e705c121SKalle Valo 	*total_flags = 0;
1933e705c121SKalle Valo }
1934e705c121SKalle Valo 
1935e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1936e705c121SKalle Valo 					struct ieee80211_vif *vif,
1937e705c121SKalle Valo 					unsigned int filter_flags,
1938e705c121SKalle Valo 					unsigned int changed_flags)
1939e705c121SKalle Valo {
1940e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1941e705c121SKalle Valo 
1942e705c121SKalle Valo 	/* We support only filter for probe requests */
1943e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1944e705c121SKalle Valo 		return;
1945e705c121SKalle Valo 
1946e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1947f276e20bSJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
1948e705c121SKalle Valo 	    !vif->p2p)
1949e705c121SKalle Valo 		return;
1950e705c121SKalle Valo 
1951e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1952e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1953e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1954e705c121SKalle Valo }
1955e705c121SKalle Valo 
195622c58834SGregory Greenman int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1957a07a8f37SSara Sharon {
1958a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1959a07a8f37SSara Sharon 
1960a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1961a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1962a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1963a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1964a07a8f37SSara Sharon 
1965a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1966a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1967a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1968a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1969a07a8f37SSara Sharon }
1970a07a8f37SSara Sharon 
1971f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1972f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1973f92659a1SSara Sharon {
1974d0a9123eSJohannes Berg 	if (vif->bss_conf.mu_mimo_owner) {
1975f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1976f92659a1SSara Sharon 
1977f92659a1SSara Sharon 		/*
1978f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1979f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1980f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
1981f92659a1SSara Sharon 		 */
1982afe0d181SJohannes Berg 		ieee80211_update_mu_groups(vif, 0,
1983f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
1984f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
1985f92659a1SSara Sharon 	}
1986f92659a1SSara Sharon }
1987f92659a1SSara Sharon 
1988f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1989f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
1990f92659a1SSara Sharon {
1991f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1992f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1993f92659a1SSara Sharon 
1994f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
1995f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1996f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
1997f92659a1SSara Sharon }
1998f92659a1SSara Sharon 
1999514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2000514c3069SLuca Coelho {
2001514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
2002514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
2003514c3069SLuca Coelho 	u8 residue_bits;
2004514c3069SLuca Coelho 	u8 res;
2005514c3069SLuca Coelho 
2006514c3069SLuca Coelho 	if (bit_num <= 5)
2007514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
2008514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2009514c3069SLuca Coelho 
2010514c3069SLuca Coelho 	/*
2011514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
2012514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
2013514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
2014514c3069SLuca Coelho 	 */
2015514c3069SLuca Coelho 
2016514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
2017514c3069SLuca Coelho 
2018514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
2019514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2020514c3069SLuca Coelho 	      residue_bits;
2021514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2022514c3069SLuca Coelho 
2023514c3069SLuca Coelho 	return res;
2024514c3069SLuca Coelho }
2025514c3069SLuca Coelho 
2026091296d3SMiri Korenblit static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
2027091296d3SMiri Korenblit 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
2028cb63eb43SMiri Korenblit 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
2029cb63eb43SMiri Korenblit 			      bool inheritance)
2030091296d3SMiri Korenblit {
2031091296d3SMiri Korenblit 	int i;
2032091296d3SMiri Korenblit 
2033091296d3SMiri Korenblit 	/*
2034091296d3SMiri Korenblit 	* FW currently supports only nss == MAX_HE_SUPP_NSS
2035091296d3SMiri Korenblit 	*
2036091296d3SMiri Korenblit 	* If nss > MAX: we can ignore values we don't support
2037091296d3SMiri Korenblit 	* If nss < MAX: we can set zeros in other streams
2038091296d3SMiri Korenblit 	*/
2039091296d3SMiri Korenblit 	if (nss > MAX_HE_SUPP_NSS) {
20404d8421f2SJason A. Donenfeld 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2041091296d3SMiri Korenblit 			       MAX_HE_SUPP_NSS);
2042091296d3SMiri Korenblit 		nss = MAX_HE_SUPP_NSS;
2043091296d3SMiri Korenblit 	}
2044091296d3SMiri Korenblit 
2045091296d3SMiri Korenblit 	for (i = 0; i < nss; i++) {
2046091296d3SMiri Korenblit 		u8 ru_index_tmp = ru_index_bitmap << 1;
2047091296d3SMiri Korenblit 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2048091296d3SMiri Korenblit 		u8 bw;
2049091296d3SMiri Korenblit 
2050091296d3SMiri Korenblit 		for (bw = 0;
2051091296d3SMiri Korenblit 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2052091296d3SMiri Korenblit 		     bw++) {
2053091296d3SMiri Korenblit 			ru_index_tmp >>= 1;
2054091296d3SMiri Korenblit 
2055cb63eb43SMiri Korenblit 			/*
2056cb63eb43SMiri Korenblit 			* According to the 11be spec, if for a specific BW the PPE Thresholds
2057cb63eb43SMiri Korenblit 			* isn't present - it should inherit the thresholds from the last
2058cb63eb43SMiri Korenblit 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2059cb63eb43SMiri Korenblit 			* this inheritance - continue in this case
2060cb63eb43SMiri Korenblit 			*/
2061cb63eb43SMiri Korenblit 			if (!(ru_index_tmp & 1)) {
2062cb63eb43SMiri Korenblit 				if (inheritance)
2063cb63eb43SMiri Korenblit 					goto set_thresholds;
2064cb63eb43SMiri Korenblit 				else
2065091296d3SMiri Korenblit 					continue;
2066cb63eb43SMiri Korenblit 			}
2067091296d3SMiri Korenblit 
2068091296d3SMiri Korenblit 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2069091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2070091296d3SMiri Korenblit 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2071091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2072091296d3SMiri Korenblit 
2073cb63eb43SMiri Korenblit set_thresholds:
2074091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2075091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2076091296d3SMiri Korenblit 		}
2077091296d3SMiri Korenblit 	}
2078091296d3SMiri Korenblit }
2079091296d3SMiri Korenblit 
2080091296d3SMiri Korenblit static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
208157974a55SGregory Greenman 					    struct ieee80211_link_sta *link_sta,
2082cb63eb43SMiri Korenblit 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2083cb63eb43SMiri Korenblit 					    bool inheritance)
2084091296d3SMiri Korenblit {
208557974a55SGregory Greenman 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
208657974a55SGregory Greenman 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
208757974a55SGregory Greenman 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2088091296d3SMiri Korenblit 	u8 ru_index_bitmap =
2089091296d3SMiri Korenblit 		u8_get_bits(*ppe,
2090091296d3SMiri Korenblit 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2091091296d3SMiri Korenblit 	/* Starting after PPE header */
2092091296d3SMiri Korenblit 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2093091296d3SMiri Korenblit 
2094cb63eb43SMiri Korenblit 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2095cb63eb43SMiri Korenblit 			  inheritance);
2096091296d3SMiri Korenblit }
2097091296d3SMiri Korenblit 
20984df6a075SMiri Korenblit static int
20994df6a075SMiri Korenblit iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
21004df6a075SMiri Korenblit 					 u8 nominal_padding)
2101091296d3SMiri Korenblit {
2102091296d3SMiri Korenblit 	int low_th = -1;
2103091296d3SMiri Korenblit 	int high_th = -1;
2104091296d3SMiri Korenblit 	int i;
2105091296d3SMiri Korenblit 
2106cb63eb43SMiri Korenblit 	/* all the macros are the same for EHT and HE */
2107091296d3SMiri Korenblit 	switch (nominal_padding) {
2108cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2109091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2110091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2111091296d3SMiri Korenblit 		break;
2112cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2113091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_BPSK;
2114091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2115091296d3SMiri Korenblit 		break;
2116cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2117cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2118091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2119091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_BPSK;
2120091296d3SMiri Korenblit 		break;
2121091296d3SMiri Korenblit 	}
2122091296d3SMiri Korenblit 
21234df6a075SMiri Korenblit 	if (low_th < 0 || high_th < 0)
21244df6a075SMiri Korenblit 		return -EINVAL;
21254df6a075SMiri Korenblit 
2126091296d3SMiri Korenblit 	/* Set the PPE thresholds accordingly */
2127091296d3SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2128091296d3SMiri Korenblit 		u8 bw;
2129091296d3SMiri Korenblit 
2130091296d3SMiri Korenblit 		for (bw = 0;
2131091296d3SMiri Korenblit 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2132091296d3SMiri Korenblit 			bw++) {
2133091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2134091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2135091296d3SMiri Korenblit 		}
2136091296d3SMiri Korenblit 	}
2137091296d3SMiri Korenblit 
21384df6a075SMiri Korenblit 	return 0;
2139091296d3SMiri Korenblit }
2140091296d3SMiri Korenblit 
2141cb63eb43SMiri Korenblit static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2142cb63eb43SMiri Korenblit 					 u8 nominal_padding)
2143cb63eb43SMiri Korenblit {
2144cb63eb43SMiri Korenblit 	int i;
2145cb63eb43SMiri Korenblit 
2146cb63eb43SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2147cb63eb43SMiri Korenblit 		u8 bw;
2148cb63eb43SMiri Korenblit 
2149cb63eb43SMiri Korenblit 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2150cb63eb43SMiri Korenblit 		     bw++) {
2151cb63eb43SMiri Korenblit 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2152cb63eb43SMiri Korenblit 
2153cb63eb43SMiri Korenblit 			if (nominal_padding >
2154cb63eb43SMiri Korenblit 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2155cb63eb43SMiri Korenblit 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2156cb63eb43SMiri Korenblit 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2157cb63eb43SMiri Korenblit 			else if (nominal_padding ==
2158cb63eb43SMiri Korenblit 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2159cb63eb43SMiri Korenblit 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2160cb63eb43SMiri Korenblit 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2161cb63eb43SMiri Korenblit 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2162cb63eb43SMiri Korenblit 		}
2163cb63eb43SMiri Korenblit 	}
2164cb63eb43SMiri Korenblit }
2165cb63eb43SMiri Korenblit 
21664df6a075SMiri Korenblit /* Set the pkt_ext field according to PPE Thresholds element */
216757974a55SGregory Greenman int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
216857974a55SGregory Greenman 			    struct ieee80211_link_sta *link_sta,
21694df6a075SMiri Korenblit 			    struct iwl_he_pkt_ext_v2 *pkt_ext)
21704df6a075SMiri Korenblit {
21714df6a075SMiri Korenblit 	u8 nominal_padding;
21724df6a075SMiri Korenblit 	int i, ret = 0;
21734df6a075SMiri Korenblit 
217457974a55SGregory Greenman 	if (WARN_ON(!link_sta))
217557974a55SGregory Greenman 		return -EINVAL;
217657974a55SGregory Greenman 
21774df6a075SMiri Korenblit 	/* Initialize the PPE thresholds to "None" (7), as described in Table
21784df6a075SMiri Korenblit 	 * 9-262ac of 80211.ax/D3.0.
21794df6a075SMiri Korenblit 	 */
21804df6a075SMiri Korenblit 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
21814df6a075SMiri Korenblit 	       sizeof(struct iwl_he_pkt_ext_v2));
21824df6a075SMiri Korenblit 
218357974a55SGregory Greenman 	if (link_sta->eht_cap.has_eht) {
21844df6a075SMiri Korenblit 		nominal_padding =
218557974a55SGregory Greenman 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
21864df6a075SMiri Korenblit 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
21874df6a075SMiri Korenblit 
21884df6a075SMiri Korenblit 		/* If PPE Thresholds exists, parse them into a FW-familiar
21894df6a075SMiri Korenblit 		 * format.
21904df6a075SMiri Korenblit 		 */
219157974a55SGregory Greenman 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
21924df6a075SMiri Korenblit 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
219357974a55SGregory Greenman 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
21944df6a075SMiri Korenblit 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
219557974a55SGregory Greenman 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
21964df6a075SMiri Korenblit 			u8 ru_index_bitmap =
21974df6a075SMiri Korenblit 				u16_get_bits(*ppe,
21984df6a075SMiri Korenblit 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
21994df6a075SMiri Korenblit 			 /* Starting after PPE header */
22004df6a075SMiri Korenblit 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
22014df6a075SMiri Korenblit 
22024df6a075SMiri Korenblit 			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
22034df6a075SMiri Korenblit 					  ppe, ppe_pos_bit, true);
220457974a55SGregory Greenman 		/* EHT PPE Thresholds doesn't exist - set the API according to
220557974a55SGregory Greenman 		 * HE PPE Tresholds
22064df6a075SMiri Korenblit 		 */
220757974a55SGregory Greenman 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
22084df6a075SMiri Korenblit 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
22094df6a075SMiri Korenblit 			/* Even though HE Capabilities IE doesn't contain PPE
22104df6a075SMiri Korenblit 			 * Thresholds for BW 320Mhz, thresholds for this BW will
22114df6a075SMiri Korenblit 			 * be filled in with the same values as 160Mhz, due to
22124df6a075SMiri Korenblit 			 * the inheritance, as required.
22134df6a075SMiri Korenblit 			 */
221457974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
22154df6a075SMiri Korenblit 							true);
22164df6a075SMiri Korenblit 
22174df6a075SMiri Korenblit 			/* According to the requirements, for MCSs 12-13 the
22184df6a075SMiri Korenblit 			 * maximum value between HE PPE Threshold and Common
22194df6a075SMiri Korenblit 			 * Nominal Packet Padding needs to be taken
22204df6a075SMiri Korenblit 			 */
22214df6a075SMiri Korenblit 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
22224df6a075SMiri Korenblit 
22234df6a075SMiri Korenblit 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
22244df6a075SMiri Korenblit 		 * take the Thresholds from Common Nominal Packet Padding field
22254df6a075SMiri Korenblit 		 */
22264df6a075SMiri Korenblit 		} else {
22274df6a075SMiri Korenblit 			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
22284df6a075SMiri Korenblit 								       nominal_padding);
22294df6a075SMiri Korenblit 		}
223057974a55SGregory Greenman 	} else if (link_sta->he_cap.has_he) {
22314df6a075SMiri Korenblit 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
223257974a55SGregory Greenman 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
22334df6a075SMiri Korenblit 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
223457974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
22354df6a075SMiri Korenblit 							false);
22364df6a075SMiri Korenblit 		/* PPE Thresholds doesn't exist - set the API PPE values
22374df6a075SMiri Korenblit 		 * according to Common Nominal Packet Padding field.
22384df6a075SMiri Korenblit 		 */
22394df6a075SMiri Korenblit 		} else {
22404df6a075SMiri Korenblit 			nominal_padding =
224157974a55SGregory Greenman 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
22424df6a075SMiri Korenblit 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
22434df6a075SMiri Korenblit 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
22444df6a075SMiri Korenblit 				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
22454df6a075SMiri Korenblit 									       nominal_padding);
22464df6a075SMiri Korenblit 		}
22474df6a075SMiri Korenblit 	}
22484df6a075SMiri Korenblit 
22494df6a075SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
22504df6a075SMiri Korenblit 		int bw;
22514df6a075SMiri Korenblit 
22524df6a075SMiri Korenblit 		for (bw = 0;
22534df6a075SMiri Korenblit 		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
22544df6a075SMiri Korenblit 		     bw++) {
22554df6a075SMiri Korenblit 			u8 *qam_th =
22564df6a075SMiri Korenblit 				&pkt_ext->pkt_ext_qam_th[i][bw][0];
22574df6a075SMiri Korenblit 
22584df6a075SMiri Korenblit 			IWL_DEBUG_HT(mvm,
22594df6a075SMiri Korenblit 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
22604df6a075SMiri Korenblit 				     i, bw, qam_th[0], qam_th[1]);
22614df6a075SMiri Korenblit 		}
22624df6a075SMiri Korenblit 	}
22634df6a075SMiri Korenblit 	return ret;
22644df6a075SMiri Korenblit }
22654df6a075SMiri Korenblit 
22669c4f15caSMiri Korenblit /*
22679c4f15caSMiri Korenblit  * This function sets the MU EDCA parameters ans returns whether MU EDCA
22689c4f15caSMiri Korenblit  * is enabled or not
22699c4f15caSMiri Korenblit  */
227055eb1c5fSMiri Korenblit bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2271e119e740SEmmanuel Grumbach 				   const struct iwl_mvm_vif_link_info *link_info,
227255eb1c5fSMiri Korenblit 				   struct iwl_he_backoff_conf *trig_based_txf)
22739c4f15caSMiri Korenblit {
22749c4f15caSMiri Korenblit 	int i;
22759c4f15caSMiri Korenblit 	/* Mark MU EDCA as enabled, unless none detected on some AC */
22769c4f15caSMiri Korenblit 	bool mu_edca_enabled = true;
22779c4f15caSMiri Korenblit 
22789c4f15caSMiri Korenblit 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2279e119e740SEmmanuel Grumbach 		const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2280e119e740SEmmanuel Grumbach 			&link_info->queue_params[i].mu_edca_param_rec;
22819c4f15caSMiri Korenblit 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
22829c4f15caSMiri Korenblit 
2283e119e740SEmmanuel Grumbach 		if (!link_info->queue_params[i].mu_edca) {
22849c4f15caSMiri Korenblit 			mu_edca_enabled = false;
22859c4f15caSMiri Korenblit 			break;
22869c4f15caSMiri Korenblit 		}
22879c4f15caSMiri Korenblit 
22889c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmin =
22899c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
22909c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmax =
22919c4f15caSMiri Korenblit 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
22929c4f15caSMiri Korenblit 		trig_based_txf[ac].aifsn =
22939c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->aifsn & 0xf);
22949c4f15caSMiri Korenblit 		trig_based_txf[ac].mu_time =
22959c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->mu_edca_timer);
22969c4f15caSMiri Korenblit 	}
22979c4f15caSMiri Korenblit 
22989c4f15caSMiri Korenblit 	return mu_edca_enabled;
22999c4f15caSMiri Korenblit }
23009c4f15caSMiri Korenblit 
23019be162a7SMiri Korenblit bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
23029c4f15caSMiri Korenblit {
23039c4f15caSMiri Korenblit 	const struct ieee80211_supported_band *sband;
23049c4f15caSMiri Korenblit 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
23059c4f15caSMiri Korenblit 
23069c4f15caSMiri Korenblit 	/* This capability is the same for all bands,
23079c4f15caSMiri Korenblit 	 * so take it from one of them.
23089c4f15caSMiri Korenblit 	 */
23099c4f15caSMiri Korenblit 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
23101ec7291eSJohannes Berg 	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
23119c4f15caSMiri Korenblit 
23129c4f15caSMiri Korenblit 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
23139c4f15caSMiri Korenblit 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
23149c4f15caSMiri Korenblit }
23159c4f15caSMiri Korenblit 
231657974a55SGregory Greenman __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
231757974a55SGregory Greenman 				 struct ieee80211_link_sta *link_sta)
23184df6a075SMiri Korenblit {
231957974a55SGregory Greenman 	u8 *mac_cap_info =
232057974a55SGregory Greenman 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
23214df6a075SMiri Korenblit 	__le32 htc_flags = 0;
23224df6a075SMiri Korenblit 
23234df6a075SMiri Korenblit 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
23244df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
23254df6a075SMiri Korenblit 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
23264df6a075SMiri Korenblit 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
23274df6a075SMiri Korenblit 		u8 link_adap =
23284df6a075SMiri Korenblit 			((mac_cap_info[2] &
23294df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
23304df6a075SMiri Korenblit 			 (mac_cap_info[1] &
23314df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
23324df6a075SMiri Korenblit 
23334df6a075SMiri Korenblit 		if (link_adap == 2)
23344df6a075SMiri Korenblit 			htc_flags |=
23354df6a075SMiri Korenblit 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
23364df6a075SMiri Korenblit 		else if (link_adap == 3)
23374df6a075SMiri Korenblit 			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
23384df6a075SMiri Korenblit 	}
23394df6a075SMiri Korenblit 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
23404df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
23414df6a075SMiri Korenblit 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
23424df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
23434df6a075SMiri Korenblit 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
23444df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
23454df6a075SMiri Korenblit 
23464df6a075SMiri Korenblit 	return htc_flags;
23474df6a075SMiri Korenblit }
23484df6a075SMiri Korenblit 
2349514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2350514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2351514c3069SLuca Coelho {
2352514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
235342506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2354514c3069SLuca Coelho 		.sta_id = sta_id,
2355514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2356dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2357514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2358514c3069SLuca Coelho 		.frame_time_rts_th =
2359514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2360514c3069SLuca Coelho 	};
236142506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
236242506dd2SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
236342506dd2SJohannes Berg 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
236442506dd2SJohannes Berg 	int size;
2365514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2366514c3069SLuca Coelho 	u32 flags;
2367514c3069SLuca Coelho 	int i;
236842506dd2SJohannes Berg 	void *cmd;
236942506dd2SJohannes Berg 
237042506dd2SJohannes Berg 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
237142506dd2SJohannes Berg 		ver = 1;
237242506dd2SJohannes Berg 
237342506dd2SJohannes Berg 	switch (ver) {
237442506dd2SJohannes Berg 	case 1:
237542506dd2SJohannes Berg 		/* same layout as v2 except some data at the end */
237642506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
237742506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
237842506dd2SJohannes Berg 		break;
237942506dd2SJohannes Berg 	case 2:
238042506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
238142506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
238242506dd2SJohannes Berg 		break;
238342506dd2SJohannes Berg 	case 3:
238442506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd;
238542506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
238642506dd2SJohannes Berg 		break;
238742506dd2SJohannes Berg 	default:
238842506dd2SJohannes Berg 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
238942506dd2SJohannes Berg 		return;
239042506dd2SJohannes Berg 	}
2391514c3069SLuca Coelho 
2392514c3069SLuca Coelho 	rcu_read_lock();
2393514c3069SLuca Coelho 
2394514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
239512d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2396514c3069SLuca Coelho 		rcu_read_unlock();
2397514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2398514c3069SLuca Coelho 		return;
2399514c3069SLuca Coelho 	}
2400514c3069SLuca Coelho 
2401046d2e7cSSriram R 	if (!sta->deflink.he_cap.has_he) {
2402514c3069SLuca Coelho 		rcu_read_unlock();
2403514c3069SLuca Coelho 		return;
2404514c3069SLuca Coelho 	}
2405514c3069SLuca Coelho 
2406514c3069SLuca Coelho 	flags = 0;
2407514c3069SLuca Coelho 
24084f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
2409650cadb7SGregory Greenman 	if (mvmvif->deflink.he_ru_2mhz_block)
24104f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
24114f58121dSIlan Peer 
2412514c3069SLuca Coelho 	/* HTC flags */
241357974a55SGregory Greenman 	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2414514c3069SLuca Coelho 
24154df6a075SMiri Korenblit 	/* PPE Thresholds */
241657974a55SGregory Greenman 	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2417cb63eb43SMiri Korenblit 		flags |= STA_CTXT_HE_PACKET_EXT;
241873f23d91SShaul Triebitz 
2419046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
242073f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
242173f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
242273f23d91SShaul Triebitz 
2423046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
242473f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
242573f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
242673f23d91SShaul Triebitz 
2427514c3069SLuca Coelho 	rcu_read_unlock();
2428514c3069SLuca Coelho 
2429e119e740SEmmanuel Grumbach 	if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
24309c4f15caSMiri Korenblit 					  &sta_ctxt_cmd.trig_based_txf[0]))
2431514c3069SLuca Coelho 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2432514c3069SLuca Coelho 
2433514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2434514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2435514c3069SLuca Coelho 
2436514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2437514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2438514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2439514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2440514c3069SLuca Coelho 	}
2441514c3069SLuca Coelho 
24429c4f15caSMiri Korenblit 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2443ee1a02d7SShaul Triebitz 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2444ee1a02d7SShaul Triebitz 
2445918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2446918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2447918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2448918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2449d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2450d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2451d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2452d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2453d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2454d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2455918cbf39SSara Sharon 	}
2456514c3069SLuca Coelho 
2457514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2458514c3069SLuca Coelho 
245942506dd2SJohannes Berg 	if (ver < 3) {
246042506dd2SJohannes Berg 		/* fields before pkt_ext */
246142506dd2SJohannes Berg 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
246242506dd2SJohannes Berg 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
246342506dd2SJohannes Berg 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
246442506dd2SJohannes Berg 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
246542506dd2SJohannes Berg 
246642506dd2SJohannes Berg 		/* pkt_ext */
246742506dd2SJohannes Berg 		for (i = 0;
246842506dd2SJohannes Berg 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
246942506dd2SJohannes Berg 		     i++) {
247042506dd2SJohannes Berg 			u8 bw;
247142506dd2SJohannes Berg 
247242506dd2SJohannes Berg 			for (bw = 0;
247342506dd2SJohannes Berg 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
247442506dd2SJohannes Berg 			     bw++) {
247542506dd2SJohannes Berg 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
247642506dd2SJohannes Berg 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
247742506dd2SJohannes Berg 
247842506dd2SJohannes Berg 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
247942506dd2SJohannes Berg 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
248042506dd2SJohannes Berg 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
248142506dd2SJohannes Berg 			}
248242506dd2SJohannes Berg 		}
248342506dd2SJohannes Berg 
248442506dd2SJohannes Berg 		/* fields after pkt_ext */
248542506dd2SJohannes Berg 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
248642506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
248742506dd2SJohannes Berg 			     sizeof(sta_ctxt_cmd_v2) -
248842506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
248942506dd2SJohannes Berg 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
249042506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
249142506dd2SJohannes Berg 		       (u8 *)&sta_ctxt_cmd +
249242506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
249342506dd2SJohannes Berg 		       sizeof(sta_ctxt_cmd) -
249442506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
249542506dd2SJohannes Berg 		sta_ctxt_cmd_v2.reserved3 = 0;
249642506dd2SJohannes Berg 	}
249742506dd2SJohannes Berg 
249842506dd2SJohannes Berg 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2499514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2500514c3069SLuca Coelho }
2501514c3069SLuca Coelho 
2502660eba5aSMiri Korenblit void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
250323673041SMiri Korenblit 			   u32 duration_override, unsigned int link_id)
2504af84ac57SJohannes Berg {
2505af84ac57SJohannes Berg 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2506af84ac57SJohannes Berg 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2507af84ac57SJohannes Berg 
2508af84ac57SJohannes Berg 	if (duration_override > duration)
2509af84ac57SJohannes Berg 		duration = duration_override;
2510af84ac57SJohannes Berg 
2511af84ac57SJohannes Berg 	/* Try really hard to protect the session and hear a beacon
2512af84ac57SJohannes Berg 	 * The new session protection command allows us to protect the
2513af84ac57SJohannes Berg 	 * session for a much longer time since the firmware will internally
2514af84ac57SJohannes Berg 	 * create two events: a 300TU one with a very high priority that
2515af84ac57SJohannes Berg 	 * won't be fragmented which should be enough for 99% of the cases,
2516af84ac57SJohannes Berg 	 * and another one (which we configure here to be 900TU long) which
2517af84ac57SJohannes Berg 	 * will have a slightly lower priority, but more importantly, can be
2518af84ac57SJohannes Berg 	 * fragmented so that it'll allow other activities to run.
2519af84ac57SJohannes Berg 	 */
2520af84ac57SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
2521af84ac57SJohannes Berg 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2522af84ac57SJohannes Berg 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
252323673041SMiri Korenblit 						    min_duration, false,
252423673041SMiri Korenblit 						    link_id);
2525af84ac57SJohannes Berg 	else
2526af84ac57SJohannes Berg 		iwl_mvm_protect_session(mvm, vif, duration,
2527af84ac57SJohannes Berg 					min_duration, 500, false);
2528af84ac57SJohannes Berg }
2529af84ac57SJohannes Berg 
2530660eba5aSMiri Korenblit /* Handle association common part to MLD and non-MLD modes */
2531660eba5aSMiri Korenblit void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2532660eba5aSMiri Korenblit 					    struct ieee80211_vif *vif,
2533660eba5aSMiri Korenblit 					    u64 changes)
2534660eba5aSMiri Korenblit {
2535660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2536660eba5aSMiri Korenblit 	int ret;
2537660eba5aSMiri Korenblit 
2538660eba5aSMiri Korenblit 	/* The firmware tracks the MU-MIMO group on its own.
2539660eba5aSMiri Korenblit 	 * However, on HW restart we should restore this data.
2540660eba5aSMiri Korenblit 	 */
2541660eba5aSMiri Korenblit 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2542660eba5aSMiri Korenblit 	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2543660eba5aSMiri Korenblit 		ret = iwl_mvm_update_mu_groups(mvm, vif);
2544660eba5aSMiri Korenblit 		if (ret)
2545660eba5aSMiri Korenblit 			IWL_ERR(mvm,
2546660eba5aSMiri Korenblit 				"failed to update VHT MU_MIMO groups\n");
2547660eba5aSMiri Korenblit 	}
2548660eba5aSMiri Korenblit 
2549660eba5aSMiri Korenblit 	iwl_mvm_recalc_multicast(mvm);
2550660eba5aSMiri Korenblit 
2551660eba5aSMiri Korenblit 	/* reset rssi values */
2552660eba5aSMiri Korenblit 	mvmvif->bf_data.ave_beacon_signal = 0;
2553660eba5aSMiri Korenblit 
2554660eba5aSMiri Korenblit 	iwl_mvm_bt_coex_vif_change(mvm);
25551a3e7039SGregory Greenman 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2556660eba5aSMiri Korenblit 					    IEEE80211_SMPS_AUTOMATIC);
2557660eba5aSMiri Korenblit 	if (fw_has_capa(&mvm->fw->ucode_capa,
2558660eba5aSMiri Korenblit 			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2559660eba5aSMiri Korenblit 		iwl_mvm_config_scan(mvm);
2560660eba5aSMiri Korenblit }
2561660eba5aSMiri Korenblit 
2562660eba5aSMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
25631a3e7039SGregory Greenman void
25641a3e7039SGregory Greenman iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2565660eba5aSMiri Korenblit 					struct ieee80211_vif *vif,
25661a3e7039SGregory Greenman 					struct ieee80211_bss_conf *link_conf,
2567660eba5aSMiri Korenblit 					u64 changes)
2568660eba5aSMiri Korenblit {
2569660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2570660eba5aSMiri Korenblit 	int ret;
2571660eba5aSMiri Korenblit 
2572660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BEACON_INFO) {
2573660eba5aSMiri Korenblit 		/* We received a beacon from the associated AP so
2574660eba5aSMiri Korenblit 		 * remove the session protection.
2575660eba5aSMiri Korenblit 		 */
2576660eba5aSMiri Korenblit 		iwl_mvm_stop_session_protection(mvm, vif);
2577660eba5aSMiri Korenblit 
2578660eba5aSMiri Korenblit 		iwl_mvm_sf_update(mvm, vif, false);
257987f5b5f2SJohannes Berg 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
2580660eba5aSMiri Korenblit 	}
2581660eba5aSMiri Korenblit 
2582660eba5aSMiri Korenblit 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2583660eba5aSMiri Korenblit 		       /* Send power command on every beacon change,
2584660eba5aSMiri Korenblit 			* because we may have not enabled beacon abort yet.
2585660eba5aSMiri Korenblit 			*/
2586660eba5aSMiri Korenblit 		       BSS_CHANGED_BEACON_INFO)) {
2587660eba5aSMiri Korenblit 		ret = iwl_mvm_power_update_mac(mvm);
2588660eba5aSMiri Korenblit 		if (ret)
2589660eba5aSMiri Korenblit 			IWL_ERR(mvm, "failed to update power mode\n");
2590660eba5aSMiri Korenblit 	}
2591660eba5aSMiri Korenblit 
2592660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_CQM) {
2593660eba5aSMiri Korenblit 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2594660eba5aSMiri Korenblit 		/* reset cqm events tracking */
2595660eba5aSMiri Korenblit 		mvmvif->bf_data.last_cqm_event = 0;
2596660eba5aSMiri Korenblit 		if (mvmvif->bf_data.bf_enabled) {
259722c58834SGregory Greenman 			/* FIXME: need to update per link when FW API will
259822c58834SGregory Greenman 			 * support it
259922c58834SGregory Greenman 			 */
260087f5b5f2SJohannes Berg 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
2601660eba5aSMiri Korenblit 			if (ret)
2602660eba5aSMiri Korenblit 				IWL_ERR(mvm,
2603660eba5aSMiri Korenblit 					"failed to update CQM thresholds\n");
2604660eba5aSMiri Korenblit 		}
2605660eba5aSMiri Korenblit 	}
2606660eba5aSMiri Korenblit 
2607660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BANDWIDTH)
26081a3e7039SGregory Greenman 		iwl_mvm_update_link_smps(vif, link_conf);
2609660eba5aSMiri Korenblit }
2610660eba5aSMiri Korenblit 
2611e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2612e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2613e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
26147b7090b4SJohannes Berg 					     u64 changes)
2615e705c121SKalle Valo {
2616e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2617e705c121SKalle Valo 	int ret;
2618b6e3d1baSAnjaneyulu 	int i;
2619e705c121SKalle Valo 
2620e705c121SKalle Valo 	/*
2621cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend
2622cdaba917SEmmanuel Grumbach 	 * on the beacon interval, which was not known when the station
2623cdaba917SEmmanuel Grumbach 	 * interface was added.
2624e705c121SKalle Valo 	 */
2625f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2626cb63eb43SMiri Korenblit 		if ((vif->bss_conf.he_support &&
262783f57c93SJohannes Berg 		     !iwlwifi_mod_params.disable_11ax))
2628650cadb7SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2629514c3069SLuca Coelho 
2630e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2631514c3069SLuca Coelho 	}
2632e705c121SKalle Valo 
263340ecdd01SShaul Triebitz 	/* Update MU EDCA params */
263440ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2635cb63eb43SMiri Korenblit 	    vif->cfg.assoc &&
263683f57c93SJohannes Berg 	    (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax))
2637650cadb7SGregory Greenman 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
263840ecdd01SShaul Triebitz 
2639e705c121SKalle Valo 	/*
2640e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2641e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2642e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2643e705c121SKalle Valo 	 * branch for disassociation below.
2644e705c121SKalle Valo 	 */
2645e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2646650cadb7SGregory Greenman 		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2647e705c121SKalle Valo 
2648650cadb7SGregory Greenman 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2649e705c121SKalle Valo 	if (ret)
2650e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2651e705c121SKalle Valo 
2652e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2653650cadb7SGregory Greenman 	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2654f276e20bSJohannes Berg 	mvmvif->associated = vif->cfg.assoc;
2655e705c121SKalle Valo 
2656e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2657f276e20bSJohannes Berg 		if (vif->cfg.assoc) {
2658e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2659e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2660b6e3d1baSAnjaneyulu 			for_each_mvm_vif_valid_link(mvmvif, i)
2661b6e3d1baSAnjaneyulu 				memset(&mvmvif->link[i]->beacon_stats, 0,
2662b6e3d1baSAnjaneyulu 				       sizeof(mvmvif->link[i]->beacon_stats));
2663e705c121SKalle Valo 
2664e705c121SKalle Valo 			/* add quota for this interface */
2665e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2666e705c121SKalle Valo 			if (ret) {
2667e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2668e705c121SKalle Valo 				return;
2669e705c121SKalle Valo 			}
2670e705c121SKalle Valo 
2671e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2672fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2673fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2674fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2675e705c121SKalle Valo 				/*
2676e705c121SKalle Valo 				 * If we're restarting then the firmware will
2677e705c121SKalle Valo 				 * obviously have lost synchronisation with
2678e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2679e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2680e705c121SKalle Valo 				 * scheduling a session protection time event.
2681e705c121SKalle Valo 				 *
2682e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2683e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2684e705c121SKalle Valo 				 * the beacon interval.
2685e705c121SKalle Valo 				 *
2686e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2687e705c121SKalle Valo 				 * than a single interface.
2688fe959c7bSEmmanuel Grumbach 				 *
2689fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2690fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2691fe959c7bSEmmanuel Grumbach 				 * only anyway.
2692e705c121SKalle Valo 				 */
2693e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2694e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2695e705c121SKalle Valo 							5 * dur, false);
2696af84ac57SJohannes Berg 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2697af84ac57SJohannes Berg 					     &mvm->status) &&
2698af84ac57SJohannes Berg 				   !vif->bss_conf.dtim_period) {
2699af84ac57SJohannes Berg 				/*
2700af84ac57SJohannes Berg 				 * If we're not restarting and still haven't
2701af84ac57SJohannes Berg 				 * heard a beacon (dtim period unknown) then
2702af84ac57SJohannes Berg 				 * make sure we still have enough minimum time
2703af84ac57SJohannes Berg 				 * remaining in the time event, since the auth
2704af84ac57SJohannes Berg 				 * might actually have taken quite a while
2705af84ac57SJohannes Berg 				 * (especially for SAE) and so the remaining
2706af84ac57SJohannes Berg 				 * time could be small without us having heard
2707af84ac57SJohannes Berg 				 * a beacon yet.
2708af84ac57SJohannes Berg 				 */
270923673041SMiri Korenblit 				iwl_mvm_protect_assoc(mvm, vif, 0, 0);
2710e705c121SKalle Valo 			}
2711e705c121SKalle Valo 
2712e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2713e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2714e705c121SKalle Valo 			if (vif->p2p) {
2715e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2716e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
27171a3e7039SGregory Greenman 						    IEEE80211_SMPS_DYNAMIC, 0);
2718e705c121SKalle Valo 			}
2719650cadb7SGregory Greenman 		} else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
27206d19a5ebSEmmanuel Grumbach 			iwl_mvm_mei_host_disassociated(mvm);
2721e705c121SKalle Valo 			/*
2722e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2723e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2724e705c121SKalle Valo 			 */
272569e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
272669e508b4SIlan Peer 			WARN_ONCE(ret &&
272769e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
272869e508b4SIlan Peer 					    &mvm->status),
2729e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2730e705c121SKalle Valo 
27316b28f978SEmmanuel Grumbach 			/*
27326b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
27336b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
27346b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
27356b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
27366b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
27376b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
27386b28f978SEmmanuel Grumbach 			 */
27396b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
27406b28f978SEmmanuel Grumbach 				      &mvm->status)) {
27415c75a208SJohannes Berg 				/* first remove remaining keys */
2742ba9eef6bSGregory Greenman 				iwl_mvm_sec_key_remove_ap(mvm, vif,
2743072573f6SJohannes Berg 							  &mvmvif->deflink, 0);
27445c75a208SJohannes Berg 
27456b28f978SEmmanuel Grumbach 				/*
27466b28f978SEmmanuel Grumbach 				 * Remove AP station now that
27476b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
27486b28f978SEmmanuel Grumbach 				 */
27496b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
2750650cadb7SGregory Greenman 							mvmvif->deflink.ap_sta_id);
2751e705c121SKalle Valo 				if (ret)
27526b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
27536b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2754e705c121SKalle Valo 
2755650cadb7SGregory Greenman 				mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
27566b28f978SEmmanuel Grumbach 			}
27576b28f978SEmmanuel Grumbach 
2758e705c121SKalle Valo 			/* remove quota for this interface */
2759e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2760e705c121SKalle Valo 			if (ret)
2761e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2762e705c121SKalle Valo 
2763e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2764e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2765e705c121SKalle Valo 			if (ret)
2766e705c121SKalle Valo 				IWL_ERR(mvm,
2767e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2768e705c121SKalle Valo 					vif->addr);
2769e705c121SKalle Valo 		}
2770e705c121SKalle Valo 
2771660eba5aSMiri Korenblit 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
2772a07a8f37SSara Sharon 	}
2773a07a8f37SSara Sharon 
27741a3e7039SGregory Greenman 	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
27751a3e7039SGregory Greenman 						changes);
2776b45242c9SAvraham Stern }
2777b45242c9SAvraham Stern 
2778f947b62cSMiri Korenblit bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2779f947b62cSMiri Korenblit 				  struct ieee80211_vif *vif,
2780f947b62cSMiri Korenblit 				  int *ret)
2781f947b62cSMiri Korenblit {
2782f947b62cSMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2783f947b62cSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2784f947b62cSMiri Korenblit 	int i;
2785e705c121SKalle Valo 
2786f947b62cSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
2787f947b62cSMiri Korenblit 
2788f947b62cSMiri Korenblit 	mvmvif->ap_assoc_sta_count = 0;
2789f947b62cSMiri Korenblit 
2790f947b62cSMiri Korenblit 	/* must be set before quota calculations */
2791f947b62cSMiri Korenblit 	mvmvif->ap_ibss_active = true;
2792f947b62cSMiri Korenblit 
2793f947b62cSMiri Korenblit 	/* send all the early keys to the device now */
2794f947b62cSMiri Korenblit 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2795f947b62cSMiri Korenblit 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2796f947b62cSMiri Korenblit 
2797f947b62cSMiri Korenblit 		if (!key)
2798f947b62cSMiri Korenblit 			continue;
2799f947b62cSMiri Korenblit 
2800f947b62cSMiri Korenblit 		mvmvif->ap_early_keys[i] = NULL;
2801f947b62cSMiri Korenblit 
2802f947b62cSMiri Korenblit 		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2803f947b62cSMiri Korenblit 		if (*ret)
2804f947b62cSMiri Korenblit 			return true;
2805e705c121SKalle Valo 	}
2806e705c121SKalle Valo 
2807f947b62cSMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2808f947b62cSMiri Korenblit 		iwl_mvm_vif_set_low_latency(mvmvif, true,
2809f947b62cSMiri Korenblit 					    LOW_LATENCY_VIF_TYPE);
2810f947b62cSMiri Korenblit 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2811e705c121SKalle Valo 	}
2812e705c121SKalle Valo 
2813f947b62cSMiri Korenblit 	/* power updated needs to be done before quotas */
2814f947b62cSMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
2815e705c121SKalle Valo 
2816f947b62cSMiri Korenblit 	return false;
2817e705c121SKalle Valo }
2818e705c121SKalle Valo 
2819e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2820ae7ba17bSShaul Triebitz 				 struct ieee80211_vif *vif,
2821b327c84cSGregory Greenman 				 struct ieee80211_bss_conf *link_conf)
2822e705c121SKalle Valo {
2823e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2824e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2825f947b62cSMiri Korenblit 	int ret;
2826e705c121SKalle Valo 
2827e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2828e705c121SKalle Valo 
2829e705c121SKalle Valo 	/*
2830cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2831cdaba917SEmmanuel Grumbach 	 * the beacon interval, which was not known when the AP interface
2832cdaba917SEmmanuel Grumbach 	 * was added.
2833e705c121SKalle Valo 	 */
2834e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2835e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2836e705c121SKalle Valo 
283736cf5377SGregory Greenman 	/* For older devices need to send beacon template before adding mac
283836cf5377SGregory Greenman 	 * context. For the newer, the beacon is a resource that belongs to a
283936cf5377SGregory Greenman 	 * MAC, so need to send beacon template after adding the mac.
284036cf5377SGregory Greenman 	 */
284136cf5377SGregory Greenman 	if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
2842e705c121SKalle Valo 		/* Add the mac context */
2843e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2844e705c121SKalle Valo 		if (ret)
2845e705c121SKalle Valo 			goto out_unlock;
2846e705c121SKalle Valo 
284736cf5377SGregory Greenman 		/* Send the beacon template */
284836cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
284936cf5377SGregory Greenman 		if (ret)
285036cf5377SGregory Greenman 			goto out_unlock;
285136cf5377SGregory Greenman 	} else {
285236cf5377SGregory Greenman 		/* Send the beacon template */
285336cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
285436cf5377SGregory Greenman 		if (ret)
285536cf5377SGregory Greenman 			goto out_unlock;
2856e705c121SKalle Valo 
2857e705c121SKalle Valo 		/* Add the mac context */
2858e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2859e705c121SKalle Valo 		if (ret)
2860e705c121SKalle Valo 			goto out_unlock;
286136cf5377SGregory Greenman 	}
2862e705c121SKalle Valo 
2863e705c121SKalle Valo 	/* Perform the binding */
2864e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2865e705c121SKalle Valo 	if (ret)
2866e705c121SKalle Valo 		goto out_remove;
2867e705c121SKalle Valo 
286863dd5d02SSara Sharon 	/*
286963dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
287063dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
287163dd5d02SSara Sharon 	 * cause assert 0x2b00.
287263dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
287363dd5d02SSara Sharon 	 * the TLV
287463dd5d02SSara Sharon 	 */
287563dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2876ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2877ced19f26SSara Sharon 		if (ret)
2878ced19f26SSara Sharon 			goto out_unbind;
287963dd5d02SSara Sharon 		/*
288063dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
288163dd5d02SSara Sharon 		 * events are added and applied to the scheduler
288263dd5d02SSara Sharon 		 */
2883e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
288463dd5d02SSara Sharon 		if (ret) {
288563dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
288663dd5d02SSara Sharon 			goto out_unbind;
288763dd5d02SSara Sharon 		}
288863dd5d02SSara Sharon 	} else {
288963dd5d02SSara Sharon 		/*
289063dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
289163dd5d02SSara Sharon 		 * events are added and applied to the scheduler
289263dd5d02SSara Sharon 		 */
289375fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2894e705c121SKalle Valo 		if (ret)
289563dd5d02SSara Sharon 			goto out_unbind;
289675fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
289763dd5d02SSara Sharon 		if (ret) {
289863dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
289963dd5d02SSara Sharon 			goto out_unbind;
290063dd5d02SSara Sharon 		}
290163dd5d02SSara Sharon 	}
290226d6c16bSSara Sharon 
2903f947b62cSMiri Korenblit 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
2904f947b62cSMiri Korenblit 		goto out_failed;
2905e705c121SKalle Valo 
2906e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2907e705c121SKalle Valo 	if (ret)
2908f947b62cSMiri Korenblit 		goto out_failed;
2909e705c121SKalle Valo 
2910e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2911e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2912e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2913e705c121SKalle Valo 
2914e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2915e705c121SKalle Valo 
2916e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2917e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2918e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2919e705c121SKalle Valo 
2920fd940de7SAvraham Stern 	iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
2921b73f9a4aSJohannes Berg 
2922e705c121SKalle Valo 	goto out_unlock;
2923e705c121SKalle Valo 
2924f947b62cSMiri Korenblit out_failed:
2925e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2926e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2927e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2928ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2929e705c121SKalle Valo out_unbind:
2930e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2931e705c121SKalle Valo out_remove:
2932e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2933e705c121SKalle Valo out_unlock:
2934e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2935e705c121SKalle Valo 	return ret;
2936e705c121SKalle Valo }
2937e705c121SKalle Valo 
2938ae7ba17bSShaul Triebitz static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
2939ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
2940b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
2941ae7ba17bSShaul Triebitz {
2942b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
2943ae7ba17bSShaul Triebitz }
2944ae7ba17bSShaul Triebitz 
2945ae7ba17bSShaul Triebitz static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
2946e705c121SKalle Valo 			      struct ieee80211_vif *vif)
2947e705c121SKalle Valo {
2948b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
2949ae7ba17bSShaul Triebitz }
2950ae7ba17bSShaul Triebitz 
2951fd1a54c1SMiri Korenblit /* Common part for MLD and non-MLD ops */
2952fd1a54c1SMiri Korenblit void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
2953fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif)
2954ae7ba17bSShaul Triebitz {
2955e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2956e705c121SKalle Valo 
2957fd1a54c1SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
2958e705c121SKalle Valo 
2959fd1a54c1SMiri Korenblit 	iwl_mvm_prepare_mac_removal(mvm, vif);
2960e705c121SKalle Valo 
2961e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2962e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2963e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2964e705c121SKalle Valo 					  &mvmvif->time_event_data);
2965e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2966e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2967e705c121SKalle Valo 	}
2968e705c121SKalle Valo 
2969e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2970e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2971e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2972e705c121SKalle Valo 	}
2973e705c121SKalle Valo 
2974e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2975e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2976e705c121SKalle Valo 
297747242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
297847242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
297947242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
298047242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
298147242744STova Mussai 	}
298247242744STova Mussai 
2983e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2984fd1a54c1SMiri Korenblit }
2985fd1a54c1SMiri Korenblit 
2986fd1a54c1SMiri Korenblit static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2987fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif,
2988fd1a54c1SMiri Korenblit 				 struct ieee80211_bss_conf *link_conf)
2989fd1a54c1SMiri Korenblit {
2990fd1a54c1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2991fd1a54c1SMiri Korenblit 
2992fd1a54c1SMiri Korenblit 	mutex_lock(&mvm->mutex);
2993fd1a54c1SMiri Korenblit 
2994fd1a54c1SMiri Korenblit 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
2995e705c121SKalle Valo 
2996e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2997e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2998e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2999e705c121SKalle Valo 
3000e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
3001ced19f26SSara Sharon 
3002be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
3003be82ecd3SAvraham Stern 
3004ced19f26SSara Sharon 	/*
3005ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
3006ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
3007ced19f26SSara Sharon 	 * cause assert 0x2b00.
3008ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
3009ced19f26SSara Sharon 	 * bcast station).
3010ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
3011ced19f26SSara Sharon 	 */
3012ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
301326d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
3014e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3015ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3016ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
3017e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
3018e705c121SKalle Valo 
3019e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3020e705c121SKalle Valo 
3021e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3022e705c121SKalle Valo 
3023e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3024e705c121SKalle Valo }
3025e705c121SKalle Valo 
3026ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
3027ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
3028b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
3029ae7ba17bSShaul Triebitz {
3030b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
3031ae7ba17bSShaul Triebitz }
3032ae7ba17bSShaul Triebitz 
3033ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
3034ae7ba17bSShaul Triebitz 			      struct ieee80211_vif *vif)
3035ae7ba17bSShaul Triebitz {
3036b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3037ae7ba17bSShaul Triebitz }
3038ae7ba17bSShaul Triebitz 
3039e705c121SKalle Valo static void
3040e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3041e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3042e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
30437b7090b4SJohannes Berg 				 u64 changes)
3044e705c121SKalle Valo {
3045e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3046e705c121SKalle Valo 
3047e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
3048e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
3049e705c121SKalle Valo 		return;
3050e705c121SKalle Valo 
3051e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3052e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3053e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3054e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3055e705c121SKalle Valo 
3056e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
3057e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
305836cf5377SGregory Greenman 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3059e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
3060e705c121SKalle Valo 
3061b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3062fd940de7SAvraham Stern 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3063b73f9a4aSJohannes Berg 
3064b73f9a4aSJohannes Berg 		if (ret)
3065b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3066b73f9a4aSJohannes Berg 				 ret);
3067b73f9a4aSJohannes Berg 	}
3068b73f9a4aSJohannes Berg 
3069e705c121SKalle Valo }
3070e705c121SKalle Valo 
3071e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3072e705c121SKalle Valo 				     struct ieee80211_vif *vif,
3073e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
30747b7090b4SJohannes Berg 				     u64 changes)
3075e705c121SKalle Valo {
3076e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3077e705c121SKalle Valo 
3078e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3079e705c121SKalle Valo 
3080f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3081e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3082e705c121SKalle Valo 
3083e705c121SKalle Valo 	switch (vif->type) {
3084e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3085df7e3098SMiri Korenblit 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
3086e705c121SKalle Valo 		break;
3087e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3088e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
3089df7e3098SMiri Korenblit 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
3090e705c121SKalle Valo 		break;
309191b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
309291b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
309391b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
309491b08c2dSAviya Erenfeld 		break;
3095e705c121SKalle Valo 	default:
3096e705c121SKalle Valo 		/* shouldn't happen */
3097e705c121SKalle Valo 		WARN_ON_ONCE(1);
3098e705c121SKalle Valo 	}
3099e705c121SKalle Valo 
31009aae43a4SJohannes Berg 	if (changes & BSS_CHANGED_TXPOWER) {
31019aae43a4SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
31029aae43a4SJohannes Berg 				bss_conf->txpower);
31039aae43a4SJohannes Berg 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
31049aae43a4SJohannes Berg 	}
31059aae43a4SJohannes Berg 
3106e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3107e705c121SKalle Valo }
3108e705c121SKalle Valo 
3109cbce62a3SMiri Korenblit int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3110e705c121SKalle Valo 			struct ieee80211_scan_request *hw_req)
3111e705c121SKalle Valo {
3112e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3113e705c121SKalle Valo 	int ret;
3114e705c121SKalle Valo 
3115e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
3116e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3117e705c121SKalle Valo 		return -EINVAL;
3118e705c121SKalle Valo 
3119e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3120e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3121e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3122e705c121SKalle Valo 
3123e705c121SKalle Valo 	return ret;
3124e705c121SKalle Valo }
3125e705c121SKalle Valo 
3126cbce62a3SMiri Korenblit void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3127e705c121SKalle Valo 				struct ieee80211_vif *vif)
3128e705c121SKalle Valo {
3129e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3130e705c121SKalle Valo 
3131e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3132e705c121SKalle Valo 
3133e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3134e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
3135e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
3136e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
3137e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
3138e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
3139e705c121SKalle Valo 	*/
3140e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3141e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3142e705c121SKalle Valo 
3143e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3144e705c121SKalle Valo }
3145e705c121SKalle Valo 
3146cbce62a3SMiri Korenblit void
3147e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3148e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
3149e705c121SKalle Valo 				  int num_frames,
3150e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
3151e705c121SKalle Valo 				  bool more_data)
3152e705c121SKalle Valo {
3153e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3154e705c121SKalle Valo 
3155e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3156e705c121SKalle Valo 
3157e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3158e705c121SKalle Valo 					  tids, more_data, false);
3159e705c121SKalle Valo }
3160e705c121SKalle Valo 
3161cbce62a3SMiri Korenblit void
3162e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3163e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
3164e705c121SKalle Valo 				    int num_frames,
3165e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
3166e705c121SKalle Valo 				    bool more_data)
3167e705c121SKalle Valo {
3168e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3169e705c121SKalle Valo 
31709a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3171e705c121SKalle Valo 
3172e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3173e705c121SKalle Valo 					  tids, more_data, true);
3174e705c121SKalle Valo }
3175e705c121SKalle Valo 
317665e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3177e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
3178e705c121SKalle Valo 				     struct ieee80211_sta *sta)
3179e705c121SKalle Valo {
3180e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3181e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3182e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
3183e705c121SKalle Valo 	int tid;
3184e705c121SKalle Valo 
3185960f864bSJohannes Berg 	/*
3186960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
3187960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
3188960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
3189960f864bSJohannes Berg 	 */
3190960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3191960f864bSJohannes Berg 		return;
3192960f864bSJohannes Berg 
3193e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3194311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3195e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3196e705c121SKalle Valo 
31976862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
31981c17627bSSara Sharon 			continue;
31991c17627bSSara Sharon 
3200e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
3201e705c121SKalle Valo 
3202dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3203e705c121SKalle Valo 			continue;
3204e705c121SKalle Valo 
3205e705c121SKalle Valo 		__set_bit(tid, &tids);
3206e705c121SKalle Valo 	}
3207e705c121SKalle Valo 
3208e705c121SKalle Valo 	switch (cmd) {
3209e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
3210e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3211e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
3212e705c121SKalle Valo 
3213e705c121SKalle Valo 		if (txqs)
3214e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3215e705c121SKalle Valo 		/*
3216e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3217e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
3218e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3219e705c121SKalle Valo 		 */
3220e705c121SKalle Valo 		break;
3221e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
3222c8ee33e1SGregory Greenman 		if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
3223e705c121SKalle Valo 			break;
3224e705c121SKalle Valo 
3225e705c121SKalle Valo 		if (txqs)
3226e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3227e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3228e705c121SKalle Valo 		break;
3229e705c121SKalle Valo 	default:
3230e705c121SKalle Valo 		break;
3231e705c121SKalle Valo 	}
3232e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3233e705c121SKalle Valo }
3234e705c121SKalle Valo 
3235cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3236cbce62a3SMiri Korenblit 			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
323765e25482SJohannes Berg {
323865e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
323965e25482SJohannes Berg }
324065e25482SJohannes Berg 
324165e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
324265e25482SJohannes Berg {
324365e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
324465e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
324565e25482SJohannes Berg 	struct ieee80211_sta *sta;
324665e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
324765e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
324865e25482SJohannes Berg 
3249be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
325065e25482SJohannes Berg 		return;
325165e25482SJohannes Berg 
325265e25482SJohannes Berg 	rcu_read_lock();
3253a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
325465e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
325565e25482SJohannes Berg 		rcu_read_unlock();
325665e25482SJohannes Berg 		return;
325765e25482SJohannes Berg 	}
325865e25482SJohannes Berg 
325965e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
326065e25482SJohannes Berg 
326165e25482SJohannes Berg 	if (!mvmsta->vif ||
326265e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
326365e25482SJohannes Berg 		rcu_read_unlock();
326465e25482SJohannes Berg 		return;
326565e25482SJohannes Berg 	}
326665e25482SJohannes Berg 
326765e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
326865e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
326965e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
327065e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
327165e25482SJohannes Berg 			sta);
327265e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
327365e25482SJohannes Berg 	}
327465e25482SJohannes Berg 
327565e25482SJohannes Berg 	if (sleeping) {
327665e25482SJohannes Berg 		switch (notif->type) {
327765e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
327865e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
327965e25482SJohannes Berg 			break;
328065e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
328165e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
328265e25482SJohannes Berg 			break;
328365e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
328465e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
328565e25482SJohannes Berg 			break;
328665e25482SJohannes Berg 		default:
328765e25482SJohannes Berg 			break;
328865e25482SJohannes Berg 		}
328965e25482SJohannes Berg 	}
329065e25482SJohannes Berg 
329165e25482SJohannes Berg 	rcu_read_unlock();
329265e25482SJohannes Berg }
329365e25482SJohannes Berg 
3294cbce62a3SMiri Korenblit void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3295e705c121SKalle Valo 				struct ieee80211_vif *vif,
3296e705c121SKalle Valo 				struct ieee80211_sta *sta)
3297e705c121SKalle Valo {
3298e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3299e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
330079faae3aSGregory Greenman 	unsigned int link_id;
3301e705c121SKalle Valo 
3302e705c121SKalle Valo 	/*
3303e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
3304e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
3305e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
3306e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
3307e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
3308e705c121SKalle Valo 	 * callback deleted the station.
330979faae3aSGregory Greenman 	 * Since there's mvm->mutex here, no need to have RCU lock for
331079faae3aSGregory Greenman 	 * mvm_sta->link access.
3311e705c121SKalle Valo 	 */
3312e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
331379faae3aSGregory Greenman 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
331479faae3aSGregory Greenman 		struct iwl_mvm_link_sta *link_sta;
331579faae3aSGregory Greenman 		u32 sta_id;
3316e705c121SKalle Valo 
331779faae3aSGregory Greenman 		if (!mvm_sta->link[link_id])
331879faae3aSGregory Greenman 			continue;
331979faae3aSGregory Greenman 
332079faae3aSGregory Greenman 		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
332179faae3aSGregory Greenman 						     lockdep_is_held(&mvm->mutex));
332279faae3aSGregory Greenman 		sta_id = link_sta->sta_id;
3323b8a85a1dSJohannes Berg 		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
33243e75668bSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
332579faae3aSGregory Greenman 					 ERR_PTR(-ENOENT));
3326b8a85a1dSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3327b8a85a1dSJohannes Berg 		}
332879faae3aSGregory Greenman 	}
3329e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3330e705c121SKalle Valo }
3331e705c121SKalle Valo 
3332e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3333e705c121SKalle Valo 				const u8 *bssid)
3334e705c121SKalle Valo {
3335b0ffe455SJohannes Berg 	int i;
3336b0ffe455SJohannes Berg 
3337b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3338b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
3339b0ffe455SJohannes Berg 
3340b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3341b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3342b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
3343b0ffe455SJohannes Berg 	}
3344b0ffe455SJohannes Berg 
3345e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3346e705c121SKalle Valo 		return;
3347e705c121SKalle Valo 
3348c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3349cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3350cee5a882SAvri Altman 		return;
3351cee5a882SAvri Altman 	}
3352cee5a882SAvri Altman 
335311dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
335411dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3355e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3356e705c121SKalle Valo 		return;
3357e705c121SKalle Valo 	}
3358e705c121SKalle Valo 
3359b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3360b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3361b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3362b0ffe455SJohannes Berg 			return;
3363b0ffe455SJohannes Berg 		}
3364b0ffe455SJohannes Berg 	}
3365b0ffe455SJohannes Berg 
3366e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3367e705c121SKalle Valo }
3368e705c121SKalle Valo 
33691e8f1329SGolan Ben-Ami static void
33701e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
33711e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
33721e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
33731e8f1329SGolan Ben-Ami {
33741e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
33751e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
33761e8f1329SGolan Ben-Ami 
33776c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
33786c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
33796c042d75SSara Sharon 	if (!trig)
33801e8f1329SGolan Ben-Ami 		return;
33811e8f1329SGolan Ben-Ami 
33821e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
33831e8f1329SGolan Ben-Ami 
33841e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
33851e8f1329SGolan Ben-Ami 		return;
33861e8f1329SGolan Ben-Ami 
33871e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
33881e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
33891e8f1329SGolan Ben-Ami 		return;
33901e8f1329SGolan Ben-Ami 
33917174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
33921e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
33931e8f1329SGolan Ben-Ami 				peer_addr, action);
33941e8f1329SGolan Ben-Ami }
33951e8f1329SGolan Ben-Ami 
33964f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
33974f58121dSIlan Peer 	bool tolerated;
33984f58121dSIlan Peer };
33994f58121dSIlan Peer 
34004f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
34014f58121dSIlan Peer 						    struct cfg80211_bss *bss,
34024f58121dSIlan Peer 						    void *_data)
34034f58121dSIlan Peer {
34044f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
34056c608cd6SJohannes Berg 	const struct cfg80211_bss_ies *ies;
34064f58121dSIlan Peer 	const struct element *elem;
34074f58121dSIlan Peer 
34086c608cd6SJohannes Berg 	rcu_read_lock();
34096c608cd6SJohannes Berg 	ies = rcu_dereference(bss->ies);
34106c608cd6SJohannes Berg 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
34116c608cd6SJohannes Berg 				  ies->len);
34124f58121dSIlan Peer 
34134f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
34144f58121dSIlan Peer 	    !(elem->data[10] &
34154f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
34164f58121dSIlan Peer 		data->tolerated = false;
34174f58121dSIlan Peer 	}
34186c608cd6SJohannes Berg 	rcu_read_unlock();
34194f58121dSIlan Peer }
34204f58121dSIlan Peer 
34216e1b5956SJohannes Berg static void
34226e1b5956SJohannes Berg iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
34236e1b5956SJohannes Berg 				   struct ieee80211_vif *vif,
34246e1b5956SJohannes Berg 				   unsigned int link_id,
34256e1b5956SJohannes Berg 				   struct ieee80211_bss_conf *link_conf)
34264f58121dSIlan Peer {
34274f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
34284f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
34294f58121dSIlan Peer 		.tolerated = true,
34304f58121dSIlan Peer 	};
34314f58121dSIlan Peer 
34326092077aSJohannes Berg 	if (WARN_ON_ONCE(!link_conf->chanreq.oper.chan ||
34336e1b5956SJohannes Berg 			 !mvmvif->link[link_id]))
34346e1b5956SJohannes Berg 		return;
34356e1b5956SJohannes Berg 
34366092077aSJohannes Berg 	if (!(link_conf->chanreq.oper.chan->flags & IEEE80211_CHAN_RADAR)) {
34376e1b5956SJohannes Berg 		mvmvif->link[link_id]->he_ru_2mhz_block = false;
34384f58121dSIlan Peer 		return;
34394f58121dSIlan Peer 	}
34404f58121dSIlan Peer 
34416092077aSJohannes Berg 	cfg80211_bss_iter(hw->wiphy, &link_conf->chanreq.oper,
34424f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
34434f58121dSIlan Peer 			  &iter_data);
34444f58121dSIlan Peer 
34454f58121dSIlan Peer 	/*
34464f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
34474f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
34484f58121dSIlan Peer 	 */
34496e1b5956SJohannes Berg 	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
34504f58121dSIlan Peer }
34514f58121dSIlan Peer 
3452f7d6ef33SJohannes Berg static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3453f7d6ef33SJohannes Berg 					       struct ieee80211_vif *vif)
3454f7d6ef33SJohannes Berg {
3455f7d6ef33SJohannes Berg 	struct ieee80211_supported_band *sband;
3456f7d6ef33SJohannes Berg 	const struct ieee80211_sta_he_cap *he_cap;
3457f7d6ef33SJohannes Berg 
3458f7d6ef33SJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION)
3459f7d6ef33SJohannes Berg 		return;
3460f7d6ef33SJohannes Berg 
3461f7d6ef33SJohannes Berg 	if (!mvm->cca_40mhz_workaround)
3462f7d6ef33SJohannes Berg 		return;
3463f7d6ef33SJohannes Berg 
3464f7d6ef33SJohannes Berg 	/* decrement and check that we reached zero */
3465f7d6ef33SJohannes Berg 	mvm->cca_40mhz_workaround--;
3466f7d6ef33SJohannes Berg 	if (mvm->cca_40mhz_workaround)
3467f7d6ef33SJohannes Berg 		return;
3468f7d6ef33SJohannes Berg 
3469f7d6ef33SJohannes Berg 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3470f7d6ef33SJohannes Berg 
3471f7d6ef33SJohannes Berg 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3472f7d6ef33SJohannes Berg 
34731ec7291eSJohannes Berg 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3474f7d6ef33SJohannes Berg 
3475f7d6ef33SJohannes Berg 	if (he_cap) {
3476f7d6ef33SJohannes Berg 		/* we know that ours is writable */
34770301bcd5SBjoern A. Zeeb 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3478f7d6ef33SJohannes Berg 
3479f7d6ef33SJohannes Berg 		he->he_cap_elem.phy_cap_info[0] |=
3480f7d6ef33SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3481f7d6ef33SJohannes Berg 	}
3482f7d6ef33SJohannes Berg }
3483f7d6ef33SJohannes Berg 
34846d19a5ebSEmmanuel Grumbach static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
34856d19a5ebSEmmanuel Grumbach 					struct ieee80211_vif *vif,
34866d19a5ebSEmmanuel Grumbach 					struct iwl_mvm_sta *mvm_sta)
34876d19a5ebSEmmanuel Grumbach {
34886d19a5ebSEmmanuel Grumbach #if IS_ENABLED(CONFIG_IWLMEI)
34896d19a5ebSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
34906d19a5ebSEmmanuel Grumbach 	struct iwl_mei_conn_info conn_info = {
3491f276e20bSJohannes Berg 		.ssid_len = vif->cfg.ssid_len,
34926d19a5ebSEmmanuel Grumbach 	};
34936d19a5ebSEmmanuel Grumbach 
34946d19a5ebSEmmanuel Grumbach 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
34956d19a5ebSEmmanuel Grumbach 		return;
34966d19a5ebSEmmanuel Grumbach 
34976d19a5ebSEmmanuel Grumbach 	if (!mvm->mei_registered)
34986d19a5ebSEmmanuel Grumbach 		return;
34996d19a5ebSEmmanuel Grumbach 
35006c07b73eSJohannes Berg 	/* FIXME: MEI needs to be updated for MLO */
35016092077aSJohannes Berg 	if (!vif->bss_conf.chanreq.oper.chan)
35026c07b73eSJohannes Berg 		return;
35036c07b73eSJohannes Berg 
35046092077aSJohannes Berg 	conn_info.channel = vif->bss_conf.chanreq.oper.chan->hw_value;
35056c07b73eSJohannes Berg 
35066d19a5ebSEmmanuel Grumbach 	switch (mvm_sta->pairwise_cipher) {
3507e5d3a64eSAvraham Stern 	case WLAN_CIPHER_SUITE_TKIP:
3508e5d3a64eSAvraham Stern 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3509e5d3a64eSAvraham Stern 		break;
35106d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_CCMP:
35116d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
35126d19a5ebSEmmanuel Grumbach 		break;
35136d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP:
35146d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
35156d19a5ebSEmmanuel Grumbach 		break;
35166d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP_256:
35176d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
35186d19a5ebSEmmanuel Grumbach 		break;
35196d19a5ebSEmmanuel Grumbach 	case 0:
35206d19a5ebSEmmanuel Grumbach 		/* open profile */
35216d19a5ebSEmmanuel Grumbach 		break;
35226d19a5ebSEmmanuel Grumbach 	default:
35236d19a5ebSEmmanuel Grumbach 		/* cipher not supported, don't send anything to iwlmei */
35246d19a5ebSEmmanuel Grumbach 		return;
35256d19a5ebSEmmanuel Grumbach 	}
35266d19a5ebSEmmanuel Grumbach 
35276d19a5ebSEmmanuel Grumbach 	switch (mvmvif->rekey_data.akm) {
35286d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_SAE & 0xff:
35296d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
35306d19a5ebSEmmanuel Grumbach 		break;
35316d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_PSK & 0xff:
35326d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
35336d19a5ebSEmmanuel Grumbach 		break;
35346d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_8021X & 0xff:
35356d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
35366d19a5ebSEmmanuel Grumbach 		break;
35376d19a5ebSEmmanuel Grumbach 	case 0:
35386d19a5ebSEmmanuel Grumbach 		/* open profile */
35396d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
35406d19a5ebSEmmanuel Grumbach 		break;
35416d19a5ebSEmmanuel Grumbach 	default:
35426d19a5ebSEmmanuel Grumbach 		/* auth method / AKM not supported */
35436d19a5ebSEmmanuel Grumbach 		/* TODO: All the FT vesions of these? */
35446d19a5ebSEmmanuel Grumbach 		return;
35456d19a5ebSEmmanuel Grumbach 	}
35466d19a5ebSEmmanuel Grumbach 
3547f276e20bSJohannes Berg 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
35486d19a5ebSEmmanuel Grumbach 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
35496d19a5ebSEmmanuel Grumbach 
35506d19a5ebSEmmanuel Grumbach 	/* TODO: add support for collocated AP data */
35516d19a5ebSEmmanuel Grumbach 	iwl_mei_host_associated(&conn_info, NULL);
35526d19a5ebSEmmanuel Grumbach #endif
35536d19a5ebSEmmanuel Grumbach }
35546d19a5ebSEmmanuel Grumbach 
355587f7e243SMiri Korenblit static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
355687f7e243SMiri Korenblit 					    struct ieee80211_vif *vif,
355787f7e243SMiri Korenblit 					    bool force_assoc_off)
355887f7e243SMiri Korenblit {
355987f7e243SMiri Korenblit 	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
356087f7e243SMiri Korenblit }
356187f7e243SMiri Korenblit 
3562e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3563e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3564e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3565e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3566e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3567e705c121SKalle Valo {
3568a2906ea6SJohannes Berg 	static const struct iwl_mvm_sta_state_ops callbacks = {
356987f7e243SMiri Korenblit 		.add_sta = iwl_mvm_add_sta,
357087f7e243SMiri Korenblit 		.update_sta = iwl_mvm_update_sta,
357187f7e243SMiri Korenblit 		.rm_sta = iwl_mvm_rm_sta,
357287f7e243SMiri Korenblit 		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
357387f7e243SMiri Korenblit 	};
357487f7e243SMiri Korenblit 
357587f7e243SMiri Korenblit 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
357687f7e243SMiri Korenblit 					    &callbacks);
357787f7e243SMiri Korenblit }
357887f7e243SMiri Korenblit 
357957974a55SGregory Greenman /* FIXME: temporary making two assumptions in all sta handling functions:
358057974a55SGregory Greenman  *	(1) when setting sta state, the link exists and protected
358157974a55SGregory Greenman  *	(2) if a link is valid in sta then it's valid in vif (can
358257974a55SGregory Greenman  *	use same index in the link array)
358357974a55SGregory Greenman  */
3584f53be9c4SGregory Greenman static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3585f53be9c4SGregory Greenman 					   struct ieee80211_vif *vif,
358615d41834SJohannes Berg 					   struct ieee80211_sta *sta)
3587f53be9c4SGregory Greenman {
3588f53be9c4SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3589f53be9c4SGregory Greenman 	unsigned int link_id;
359057974a55SGregory Greenman 
3591f53be9c4SGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3592f53be9c4SGregory Greenman 		struct ieee80211_bss_conf *conf =
3593a705a782SJohannes Berg 			link_conf_dereference_check(vif, link_id);
3594f53be9c4SGregory Greenman 		struct ieee80211_link_sta *link_sta =
3595a705a782SJohannes Berg 			link_sta_dereference_check(sta, link_id);
3596f53be9c4SGregory Greenman 
3597f53be9c4SGregory Greenman 		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3598f53be9c4SGregory Greenman 			continue;
3599f53be9c4SGregory Greenman 
3600c45217bdSJohannes Berg 		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
360115d41834SJohannes Berg 				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3602f53be9c4SGregory Greenman 	}
3603f53be9c4SGregory Greenman }
360457974a55SGregory Greenman 
360557974a55SGregory Greenman #define IWL_MVM_MIN_BEACON_INTERVAL_TU 16
360657974a55SGregory Greenman 
360757974a55SGregory Greenman static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
360857974a55SGregory Greenman 				      struct ieee80211_vif *vif,
360957974a55SGregory Greenman 				      struct ieee80211_sta *sta)
361057974a55SGregory Greenman {
3611207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3612207be64fSMiri Korenblit 	unsigned int link_id;
361357974a55SGregory Greenman 
361457974a55SGregory Greenman 	/* Beacon interval check - firmware will crash if the beacon
361557974a55SGregory Greenman 	 * interval is less than 16. We can't avoid connecting at all,
361657974a55SGregory Greenman 	 * so refuse the station state change, this will cause mac80211
361757974a55SGregory Greenman 	 * to abandon attempts to connect to this AP, and eventually
361857974a55SGregory Greenman 	 * wpa_s will blocklist the AP...
361957974a55SGregory Greenman 	 */
362057974a55SGregory Greenman 
3621207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
362257974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3623207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
362457974a55SGregory Greenman 
3625207be64fSMiri Korenblit 		if (!link_conf)
362657974a55SGregory Greenman 			continue;
362757974a55SGregory Greenman 
362857974a55SGregory Greenman 		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
362957974a55SGregory Greenman 			IWL_ERR(mvm,
363057974a55SGregory Greenman 				"Beacon interval %d for AP %pM is too small\n",
363157974a55SGregory Greenman 				link_conf->beacon_int, link_sta->addr);
363257974a55SGregory Greenman 			return false;
363357974a55SGregory Greenman 		}
363457974a55SGregory Greenman 
363557974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
363657974a55SGregory Greenman 	}
363757974a55SGregory Greenman 
363857974a55SGregory Greenman 	return true;
363957974a55SGregory Greenman }
364057974a55SGregory Greenman 
364157974a55SGregory Greenman static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
364257974a55SGregory Greenman 				       struct ieee80211_vif *vif,
364357974a55SGregory Greenman 				       struct ieee80211_sta *sta,
364457974a55SGregory Greenman 				       bool is_sta)
364557974a55SGregory Greenman {
364657974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3647207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3648207be64fSMiri Korenblit 	unsigned int link_id;
364957974a55SGregory Greenman 
3650207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
365157974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3652207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
365357974a55SGregory Greenman 
3654207be64fSMiri Korenblit 		if (!link_conf || !mvmvif->link[link_id])
365557974a55SGregory Greenman 			continue;
365657974a55SGregory Greenman 
365757974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
365857974a55SGregory Greenman 
365957974a55SGregory Greenman 		if (is_sta) {
3660207be64fSMiri Korenblit 			mvmvif->link[link_id]->he_ru_2mhz_block = false;
366157974a55SGregory Greenman 			if (link_sta->he_cap.has_he)
3662207be64fSMiri Korenblit 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3663207be64fSMiri Korenblit 								   link_id,
36646e1b5956SJohannes Berg 								   link_conf);
366557974a55SGregory Greenman 		}
366657974a55SGregory Greenman 	}
366757974a55SGregory Greenman }
366857974a55SGregory Greenman 
366957974a55SGregory Greenman static int
367057974a55SGregory Greenman iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
367157974a55SGregory Greenman 				   struct ieee80211_vif *vif,
367257974a55SGregory Greenman 				   struct ieee80211_sta *sta,
3673a2906ea6SJohannes Berg 				   const struct iwl_mvm_sta_state_ops *callbacks)
367457974a55SGregory Greenman {
36750c9a8f90SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3676207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
367757974a55SGregory Greenman 	unsigned int i;
367857974a55SGregory Greenman 	int ret;
367957974a55SGregory Greenman 
368057974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
368157974a55SGregory Greenman 
368257974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_STATION &&
368357974a55SGregory Greenman 	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
368457974a55SGregory Greenman 		return -EINVAL;
368557974a55SGregory Greenman 
368657974a55SGregory Greenman 	if (sta->tdls &&
368757974a55SGregory Greenman 	    (vif->p2p ||
368857974a55SGregory Greenman 	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
368957974a55SGregory Greenman 	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
369057974a55SGregory Greenman 		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
369157974a55SGregory Greenman 		return -EBUSY;
369257974a55SGregory Greenman 	}
369357974a55SGregory Greenman 
369457974a55SGregory Greenman 	ret = callbacks->add_sta(mvm, vif, sta);
369557974a55SGregory Greenman 	if (sta->tdls && ret == 0) {
369657974a55SGregory Greenman 		iwl_mvm_recalc_tdls_state(mvm, vif, true);
369757974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
369857974a55SGregory Greenman 					   NL80211_TDLS_SETUP);
369957974a55SGregory Greenman 	}
370057974a55SGregory Greenman 
3701207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, i)
370257974a55SGregory Greenman 		link_sta->agg.max_rc_amsdu_len = 1;
3703207be64fSMiri Korenblit 
370457974a55SGregory Greenman 	ieee80211_sta_recalc_aggregates(sta);
370557974a55SGregory Greenman 
37060c9a8f90SJohannes Berg 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
37070c9a8f90SJohannes Berg 		mvmvif->ap_sta = sta;
37080c9a8f90SJohannes Berg 
3709d3b2c6c6SJohannes Berg 	/*
3710d3b2c6c6SJohannes Berg 	 * Initialize the rates here already - this really tells
3711d3b2c6c6SJohannes Berg 	 * the firmware only what the supported legacy rates are
3712d3b2c6c6SJohannes Berg 	 * (may be) since it's initialized already from what the
3713d3b2c6c6SJohannes Berg 	 * AP advertised in the beacon/probe response. This will
3714d3b2c6c6SJohannes Berg 	 * allow the firmware to send auth/assoc frames with one
3715d3b2c6c6SJohannes Berg 	 * of the supported rates already, rather than having to
3716d3b2c6c6SJohannes Berg 	 * use a mandatory rate.
3717d3b2c6c6SJohannes Berg 	 * If we're the AP, we'll just assume mandatory rates at
3718d3b2c6c6SJohannes Berg 	 * this point, but we know nothing about the STA anyway.
3719d3b2c6c6SJohannes Berg 	 */
3720d3b2c6c6SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3721d3b2c6c6SJohannes Berg 
372257974a55SGregory Greenman 	return 0;
372357974a55SGregory Greenman }
372457974a55SGregory Greenman 
372557974a55SGregory Greenman static int
372657974a55SGregory Greenman iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
372757974a55SGregory Greenman 				struct iwl_mvm *mvm,
372857974a55SGregory Greenman 				struct ieee80211_vif *vif,
372957974a55SGregory Greenman 				struct ieee80211_sta *sta,
3730a2906ea6SJohannes Berg 				const struct iwl_mvm_sta_state_ops *callbacks)
373157974a55SGregory Greenman {
373257974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
373357974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3734207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3735207be64fSMiri Korenblit 	unsigned int link_id;
373657974a55SGregory Greenman 
373757974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
373857974a55SGregory Greenman 
373957974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_AP) {
374057974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
374157974a55SGregory Greenman 		mvmvif->ap_assoc_sta_count++;
374257974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
374357974a55SGregory Greenman 
374457974a55SGregory Greenman 		/* since the below is not for MLD API, it's ok to use
374557974a55SGregory Greenman 		 * the default bss_conf
374657974a55SGregory Greenman 		 */
374757974a55SGregory Greenman 		if (!mvm->mld_api_is_used &&
374883f57c93SJohannes Berg 		    (vif->bss_conf.he_support &&
374983f57c93SJohannes Berg 		     !iwlwifi_mod_params.disable_11ax))
375057974a55SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
375157974a55SGregory Greenman 	} else if (vif->type == NL80211_IFTYPE_STATION) {
375257974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
375357974a55SGregory Greenman 
375457974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
375557974a55SGregory Greenman 
375657974a55SGregory Greenman 		if (!mvm->mld_api_is_used)
375757974a55SGregory Greenman 			goto out;
375857974a55SGregory Greenman 
3759207be64fSMiri Korenblit 		for_each_sta_active_link(vif, sta, link_sta, link_id) {
376057974a55SGregory Greenman 			struct ieee80211_bss_conf *link_conf =
3761207be64fSMiri Korenblit 				link_conf_dereference_protected(vif, link_id);
376257974a55SGregory Greenman 
376357974a55SGregory Greenman 			if (WARN_ON(!link_conf))
376457974a55SGregory Greenman 				return -EINVAL;
3765207be64fSMiri Korenblit 			if (!mvmvif->link[link_id])
3766f14ad95aSJohannes Berg 				continue;
376757974a55SGregory Greenman 
376857974a55SGregory Greenman 			iwl_mvm_link_changed(mvm, vif, link_conf,
376957974a55SGregory Greenman 					     LINK_CONTEXT_MODIFY_ALL &
377057974a55SGregory Greenman 					     ~LINK_CONTEXT_MODIFY_ACTIVE,
377157974a55SGregory Greenman 					     true);
377257974a55SGregory Greenman 		}
377357974a55SGregory Greenman 	}
377457974a55SGregory Greenman 
377557974a55SGregory Greenman out:
377615d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
377757974a55SGregory Greenman 
377857974a55SGregory Greenman 	return callbacks->update_sta(mvm, vif, sta);
377957974a55SGregory Greenman }
378057974a55SGregory Greenman 
378157974a55SGregory Greenman static int
378257974a55SGregory Greenman iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
378357974a55SGregory Greenman 				      struct ieee80211_vif *vif,
378457974a55SGregory Greenman 				      struct ieee80211_sta *sta,
3785a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
378657974a55SGregory Greenman {
378757974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
378857974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
378957974a55SGregory Greenman 
379057974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
379157974a55SGregory Greenman 
379257974a55SGregory Greenman 	/* we don't support TDLS during DCM */
379357974a55SGregory Greenman 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
379457974a55SGregory Greenman 		iwl_mvm_teardown_tdls_peers(mvm);
379557974a55SGregory Greenman 
379657974a55SGregory Greenman 	if (sta->tdls) {
379757974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
379857974a55SGregory Greenman 					   NL80211_TDLS_ENABLE_LINK);
379957974a55SGregory Greenman 	} else {
380057974a55SGregory Greenman 		/* enable beacon filtering */
380187f5b5f2SJohannes Berg 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
380257974a55SGregory Greenman 
380357974a55SGregory Greenman 		mvmvif->authorized = 1;
380457974a55SGregory Greenman 
380557974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
380657974a55SGregory Greenman 		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
3807b9be67fbSIlan Peer 
3808b9be67fbSIlan Peer 		/* when client is authorized (AP station marked as such),
3809b9be67fbSIlan Peer 		 * try to enable more links
3810b9be67fbSIlan Peer 		 */
3811b9be67fbSIlan Peer 		if (vif->type == NL80211_IFTYPE_STATION &&
3812b9be67fbSIlan Peer 		    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3813b9be67fbSIlan Peer 			iwl_mvm_mld_select_links(mvm, vif, false);
381457974a55SGregory Greenman 	}
381557974a55SGregory Greenman 
381615d41834SJohannes Berg 	mvm_sta->authorized = true;
381715d41834SJohannes Berg 
38185a86dcb4SAvraham Stern 	/* MFP is set by default before the station is authorized.
38195a86dcb4SAvraham Stern 	 * Clear it here in case it's not used.
38205a86dcb4SAvraham Stern 	 */
38210fcdf55fSEmmanuel Grumbach 	if (!sta->mfp) {
38220fcdf55fSEmmanuel Grumbach 		int ret = callbacks->update_sta(mvm, vif, sta);
38230fcdf55fSEmmanuel Grumbach 
38240fcdf55fSEmmanuel Grumbach 		if (ret)
38250fcdf55fSEmmanuel Grumbach 			return ret;
38260fcdf55fSEmmanuel Grumbach 	}
38270fcdf55fSEmmanuel Grumbach 
38280fcdf55fSEmmanuel Grumbach 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
38295a86dcb4SAvraham Stern 
383057974a55SGregory Greenman 	return 0;
383157974a55SGregory Greenman }
383257974a55SGregory Greenman 
383357974a55SGregory Greenman static int
383457974a55SGregory Greenman iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
383557974a55SGregory Greenman 				      struct ieee80211_vif *vif,
383657974a55SGregory Greenman 				      struct ieee80211_sta *sta,
3837a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
383857974a55SGregory Greenman {
383957974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
384015d41834SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
384157974a55SGregory Greenman 
384257974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
384357974a55SGregory Greenman 
384415d41834SJohannes Berg 	mvmsta->authorized = false;
384515d41834SJohannes Berg 
384657974a55SGregory Greenman 	/* once we move into assoc state, need to update rate scale to
384757974a55SGregory Greenman 	 * disable using wide bandwidth
384857974a55SGregory Greenman 	 */
384915d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
385057974a55SGregory Greenman 
385157974a55SGregory Greenman 	if (!sta->tdls) {
385257974a55SGregory Greenman 		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
385357974a55SGregory Greenman 		 * yet to avoid sending high prio again for a little
385457974a55SGregory Greenman 		 * time.
385557974a55SGregory Greenman 		 */
385657974a55SGregory Greenman 		mvmvif->authorized = 0;
385757974a55SGregory Greenman 
385857974a55SGregory Greenman 		/* disable beacon filtering */
385987f5b5f2SJohannes Berg 		iwl_mvm_disable_beacon_filter(mvm, vif);
386057974a55SGregory Greenman 	}
386157974a55SGregory Greenman 
386257974a55SGregory Greenman 	return 0;
386357974a55SGregory Greenman }
386457974a55SGregory Greenman 
386587f7e243SMiri Korenblit /* Common part for MLD and non-MLD modes */
386687f7e243SMiri Korenblit int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
386787f7e243SMiri Korenblit 				 struct ieee80211_vif *vif,
386887f7e243SMiri Korenblit 				 struct ieee80211_sta *sta,
386987f7e243SMiri Korenblit 				 enum ieee80211_sta_state old_state,
387087f7e243SMiri Korenblit 				 enum ieee80211_sta_state new_state,
3871a2906ea6SJohannes Berg 				 const struct iwl_mvm_sta_state_ops *callbacks)
387287f7e243SMiri Korenblit {
3873e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3874e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
38756ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3876de107600SJohannes Berg 	struct ieee80211_link_sta *link_sta;
387757974a55SGregory Greenman 	unsigned int link_id;
3878e705c121SKalle Valo 	int ret;
3879e705c121SKalle Valo 
3880e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3881e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3882e705c121SKalle Valo 
388324afba76SLiad Kaufman 	/*
388424afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
388524afba76SLiad Kaufman 	 *
388624afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
388724afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
388824afba76SLiad Kaufman 	 * the queues.
388924afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
389024afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
389124afba76SLiad Kaufman 	 * of.
389224afba76SLiad Kaufman 	 *
389324afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
389424afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
389524afba76SLiad Kaufman 	 */
389624afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3897c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
389824afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
389924afba76SLiad Kaufman 
390024afba76SLiad Kaufman 		/*
390124afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
390224afba76SLiad Kaufman 		 * worker will already remove it if it was on
390324afba76SLiad Kaufman 		 */
3904f7d6ef33SJohannes Berg 
3905f7d6ef33SJohannes Berg 		/*
3906f7d6ef33SJohannes Berg 		 * Additionally, reset the 40 MHz capability if we disconnected
3907f7d6ef33SJohannes Berg 		 * from the AP now.
3908f7d6ef33SJohannes Berg 		 */
3909f7d6ef33SJohannes Berg 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
3910783336b0SJohannes Berg 
3911783336b0SJohannes Berg 		/* Also free dup data just in case any assertions below fail */
3912783336b0SJohannes Berg 		kfree(mvm_sta->dup_data);
391324afba76SLiad Kaufman 	}
391424afba76SLiad Kaufman 
3915e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
391657974a55SGregory Greenman 
3917828c79d9SEmmanuel Grumbach 	/* this would be a mac80211 bug ... but don't crash, unless we had a
3918828c79d9SEmmanuel Grumbach 	 * firmware crash while we were activating a link, in which case it is
3919828c79d9SEmmanuel Grumbach 	 * legit to have phy_ctxt = NULL. Don't bother not to WARN if we are in
3920828c79d9SEmmanuel Grumbach 	 * recovery flow since we spit tons of error messages anyway.
3921828c79d9SEmmanuel Grumbach 	 */
3922de107600SJohannes Berg 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
39233d6d21b2SJohannes Berg 		if (WARN_ON_ONCE(!mvmvif->link[link_id] ||
39243d6d21b2SJohannes Berg 				 !mvmvif->link[link_id]->phy_ctxt)) {
392557974a55SGregory Greenman 			mutex_unlock(&mvm->mutex);
392657974a55SGregory Greenman 			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
392757974a55SGregory Greenman 					&mvm->status) ? 0 : -EINVAL;
392857974a55SGregory Greenman 		}
392957974a55SGregory Greenman 	}
393057974a55SGregory Greenman 
39316ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3932d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
39336ea29ce5SJohannes Berg 
3934e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3935e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
393657974a55SGregory Greenman 		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
393757974a55SGregory Greenman 							 callbacks);
393857974a55SGregory Greenman 		if (ret < 0)
3939e705c121SKalle Valo 			goto out_unlock;
3940e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3941e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3942e705c121SKalle Valo 		/*
3943e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3944e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3945e705c121SKalle Valo 		 */
3946e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3947e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3948e705c121SKalle Valo 		ret = 0;
3949e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3950e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
395157974a55SGregory Greenman 		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
395257974a55SGregory Greenman 						      callbacks);
3953e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3954e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
395557974a55SGregory Greenman 		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
395657974a55SGregory Greenman 							    callbacks);
3957e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3958e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
395957974a55SGregory Greenman 		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
396057974a55SGregory Greenman 							    callbacks);
3961e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3962e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
39638be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
39648be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
396587f7e243SMiri Korenblit 			callbacks->mac_ctxt_changed(mvm, vif, false);
3966d5d8ee52SEmmanuel Grumbach 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3967cf7a7457SJohannes Berg 			iwl_mvm_stop_session_protection(mvm, vif);
3968e705c121SKalle Valo 		ret = 0;
3969e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3970e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3971e705c121SKalle Valo 		ret = 0;
3972e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3973e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
39740c9a8f90SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
3975d5d8ee52SEmmanuel Grumbach 			iwl_mvm_stop_session_protection(mvm, vif);
39760c9a8f90SJohannes Berg 			mvmvif->ap_sta = NULL;
39770c9a8f90SJohannes Berg 		}
397887f7e243SMiri Korenblit 		ret = callbacks->rm_sta(mvm, vif, sta);
39791e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3980e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
39811e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
39821e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
39831e8f1329SGolan Ben-Ami 		}
398434a880d8SLiad Kaufman 
398544135b7cSIlan Peer 		if (unlikely(ret &&
398644135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
398744135b7cSIlan Peer 				      &mvm->status)))
398844135b7cSIlan Peer 			ret = 0;
3989e705c121SKalle Valo 	} else {
3990e705c121SKalle Valo 		ret = -EIO;
3991e705c121SKalle Valo 	}
3992e705c121SKalle Valo  out_unlock:
3993e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3994e705c121SKalle Valo 
3995e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3996e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3997e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3998e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3999e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
4000e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
4001e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4002e705c121SKalle Valo 	}
4003e705c121SKalle Valo 
4004e705c121SKalle Valo 	return ret;
4005e705c121SKalle Valo }
4006e705c121SKalle Valo 
4007cbce62a3SMiri Korenblit int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4008e705c121SKalle Valo {
4009e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4010e705c121SKalle Valo 
4011e705c121SKalle Valo 	mvm->rts_threshold = value;
4012e705c121SKalle Valo 
4013e705c121SKalle Valo 	return 0;
4014e705c121SKalle Valo }
4015e705c121SKalle Valo 
4016cbce62a3SMiri Korenblit void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4017e705c121SKalle Valo 			   struct ieee80211_sta *sta, u32 changed)
4018e705c121SKalle Valo {
4019e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4020dcfe3b10SJohannes Berg 
4021dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
4022dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
4023dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
402415d41834SJohannes Berg 		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4025e705c121SKalle Valo 
4026e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
4027e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
4028e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
4029e705c121SKalle Valo }
4030e705c121SKalle Valo 
4031e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
4032b3e2130bSJohannes Berg 			       struct ieee80211_vif *vif,
4033b3e2130bSJohannes Berg 			       unsigned int link_id, u16 ac,
4034e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
4035e705c121SKalle Valo {
4036e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4037e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4038e705c121SKalle Valo 
4039650cadb7SGregory Greenman 	mvmvif->deflink.queue_params[ac] = *params;
4040e705c121SKalle Valo 
4041e705c121SKalle Valo 	/*
4042e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
4043e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
4044e705c121SKalle Valo 	 */
4045e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
4046e705c121SKalle Valo 		int ret;
4047e705c121SKalle Valo 
4048e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
4049e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4050e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
4051e705c121SKalle Valo 		return ret;
4052e705c121SKalle Valo 	}
4053e705c121SKalle Valo 	return 0;
4054e705c121SKalle Valo }
4055e705c121SKalle Valo 
4056cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4057d4e36e55SIlan Peer 				struct ieee80211_vif *vif,
405815fae341SJohannes Berg 				struct ieee80211_prep_tx_info *info)
4059e705c121SKalle Valo {
4060e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4061d4e36e55SIlan Peer 
4062e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
406323673041SMiri Korenblit 	iwl_mvm_protect_assoc(mvm, vif, info->duration, info->link_id);
4064e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4065e705c121SKalle Valo }
4066e705c121SKalle Valo 
4067cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
40686b1259d1SJohannes Berg 				 struct ieee80211_vif *vif,
40696b1259d1SJohannes Berg 				 struct ieee80211_prep_tx_info *info)
40706b1259d1SJohannes Berg {
40716b1259d1SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
40726b1259d1SJohannes Berg 
40736b1259d1SJohannes Berg 	/* for successful cases (auth/assoc), don't cancel session protection */
40746b1259d1SJohannes Berg 	if (info->success)
40756b1259d1SJohannes Berg 		return;
40766b1259d1SJohannes Berg 
40776b1259d1SJohannes Berg 	mutex_lock(&mvm->mutex);
40786b1259d1SJohannes Berg 	iwl_mvm_stop_session_protection(mvm, vif);
40796b1259d1SJohannes Berg 	mutex_unlock(&mvm->mutex);
40806b1259d1SJohannes Berg }
40816b1259d1SJohannes Berg 
4082cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4083e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4084e705c121SKalle Valo 				 struct cfg80211_sched_scan_request *req,
4085e705c121SKalle Valo 				 struct ieee80211_scan_ies *ies)
4086e705c121SKalle Valo {
4087e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4088e705c121SKalle Valo 
4089e705c121SKalle Valo 	int ret;
4090e705c121SKalle Valo 
4091e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4092e705c121SKalle Valo 
4093f276e20bSJohannes Berg 	if (!vif->cfg.idle) {
4094e705c121SKalle Valo 		ret = -EBUSY;
4095e705c121SKalle Valo 		goto out;
4096e705c121SKalle Valo 	}
4097e705c121SKalle Valo 
4098e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4099e705c121SKalle Valo 
4100e705c121SKalle Valo out:
4101e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4102e705c121SKalle Valo 	return ret;
4103e705c121SKalle Valo }
4104e705c121SKalle Valo 
4105cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4106e705c121SKalle Valo 				struct ieee80211_vif *vif)
4107e705c121SKalle Valo {
4108e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4109e705c121SKalle Valo 	int ret;
4110e705c121SKalle Valo 
4111e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4112e705c121SKalle Valo 
4113e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
4114e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
4115e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
4116e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
4117e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4118e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
4119e705c121SKalle Valo 	 * not running.
4120e705c121SKalle Valo 	*/
4121e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4122e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
4123e705c121SKalle Valo 		return 0;
4124e705c121SKalle Valo 	}
4125e705c121SKalle Valo 
4126e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4127e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4128e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
4129e705c121SKalle Valo 
4130e705c121SKalle Valo 	return ret;
4131e705c121SKalle Valo }
4132e705c121SKalle Valo 
41336569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4134e705c121SKalle Valo 				 enum set_key_cmd cmd,
4135e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4136e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4137e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
4138e705c121SKalle Valo {
4139c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4140e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
414146c7b05aSEmmanuel Grumbach 	struct iwl_mvm_sta *mvmsta = NULL;
4142d066a530SJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn = NULL;
4143f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
41445c75a208SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
41455c75a208SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4146c56e00a3SJohannes Berg 	int ret, i;
41474615fd15SEmmanuel Grumbach 	u8 key_offset;
4148e705c121SKalle Valo 
414946c7b05aSEmmanuel Grumbach 	if (sta)
41506d19a5ebSEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
41516d19a5ebSEmmanuel Grumbach 
4152e705c121SKalle Valo 	switch (key->cipher) {
4153e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
4154286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
4155e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
41561ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
41577f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
41587f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
41597f768ad5SDavid Spinadel 		} else {
41607f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
41617f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
41627f768ad5SDavid Spinadel 		}
4163e705c121SKalle Valo 		break;
4164e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
41652a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
41662a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
416785aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
4168e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4169e705c121SKalle Valo 		break;
4170e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
41718e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
41728e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4173e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4174e705c121SKalle Valo 		break;
4175e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
4176e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
4177475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
4178e705c121SKalle Valo 			break;
4179475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
4180475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
4181475c6bdeSJohannes Berg 		/* support HW crypto on TX */
4182475c6bdeSJohannes Berg 		return 0;
4183e705c121SKalle Valo 	default:
4184e705c121SKalle Valo 		return -EOPNOTSUPP;
4185e705c121SKalle Valo 	}
4186e705c121SKalle Valo 
4187e705c121SKalle Valo 	switch (cmd) {
4188e705c121SKalle Valo 	case SET_KEY:
4189a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4190a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4191b1fdc250SJohannes Berg 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4192b1fdc250SJohannes Berg 					   key);
4193b1fdc250SJohannes Berg 
4194e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4195e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4196e705c121SKalle Valo 			/*
4197e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
4198e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
4199e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
4200f05d1e04SJohannes Berg 			 * CMAC/GMAC in AP/IBSS modes must be done in software
4201f05d1e04SJohannes Berg 			 * on older NICs.
4202a5de7de7SJohannes Berg 			 *
4203a5de7de7SJohannes Berg 			 * Except, of course, beacon protection - it must be
4204f05d1e04SJohannes Berg 			 * offloaded since we just set a beacon template, and
4205f05d1e04SJohannes Berg 			 * then we must also offload the IGTK (not just BIGTK)
4206f05d1e04SJohannes Berg 			 * for firmware reasons.
4207f05d1e04SJohannes Berg 			 *
4208f05d1e04SJohannes Berg 			 * So just check for beacon protection - if we don't
4209f05d1e04SJohannes Berg 			 * have it we cannot get here with keyidx >= 6, and
4210f05d1e04SJohannes Berg 			 * if we do have it we need to send the key to FW in
4211f05d1e04SJohannes Berg 			 * all cases (CMAC/GMAC).
4212e705c121SKalle Valo 			 */
4213f05d1e04SJohannes Berg 			if (!wiphy_ext_feature_isset(hw->wiphy,
4214f05d1e04SJohannes Berg 						     NL80211_EXT_FEATURE_BEACON_PROTECTION) &&
4215a5de7de7SJohannes Berg 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
42168e160ab8SAyala Beker 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4217a5de7de7SJohannes Berg 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
421881279c49SJohannes Berg 				ret = -EOPNOTSUPP;
4219a1c2ff30SAndrei Otcheretianski 				break;
4220a1c2ff30SAndrei Otcheretianski 			}
422185aeb58cSDavid Spinadel 
422285aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
422385aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
422485aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
4225e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
4226a1c2ff30SAndrei Otcheretianski 				ret = 0;
4227e705c121SKalle Valo 				break;
4228e705c121SKalle Valo 			}
4229c56e00a3SJohannes Berg 
4230c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
4231c56e00a3SJohannes Berg 				for (i = 0;
4232c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4233c56e00a3SJohannes Berg 				     i++) {
4234c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
4235c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
4236c56e00a3SJohannes Berg 						break;
4237c56e00a3SJohannes Berg 					}
4238c56e00a3SJohannes Berg 				}
4239c56e00a3SJohannes Berg 
4240c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4241c56e00a3SJohannes Berg 					ret = -ENOSPC;
4242a1c2ff30SAndrei Otcheretianski 				else
4243a1c2ff30SAndrei Otcheretianski 					ret = 0;
4244c56e00a3SJohannes Berg 
4245c56e00a3SJohannes Berg 				break;
4246c56e00a3SJohannes Berg 			}
424785aeb58cSDavid Spinadel 		}
4248e705c121SKalle Valo 
4249e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
4250e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
4251e705c121SKalle Valo 		 */
4252e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4253e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4254e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
4255e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
4256e705c121SKalle Valo 			ret = 0;
4257e705c121SKalle Valo 			break;
4258e705c121SKalle Valo 		}
4259e705c121SKalle Valo 
4260f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
426146c7b05aSEmmanuel Grumbach 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4262f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4263f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
42642a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
42652a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4266f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
4267f5e28eacSJohannes Berg 			int tid, q;
4268f5e28eacSJohannes Berg 
4269f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4270acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4271acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
4272f5e28eacSJohannes Berg 					 GFP_KERNEL);
4273f5e28eacSJohannes Berg 			if (!ptk_pn) {
4274f5e28eacSJohannes Berg 				ret = -ENOMEM;
4275f5e28eacSJohannes Berg 				break;
4276f5e28eacSJohannes Berg 			}
4277f5e28eacSJohannes Berg 
4278f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4279f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
4280f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
4281f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
4282f5e28eacSJohannes Berg 					       seq.ccmp.pn,
4283f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
4284f5e28eacSJohannes Berg 			}
4285f5e28eacSJohannes Berg 
4286f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4287f5e28eacSJohannes Berg 		}
4288f5e28eacSJohannes Berg 
42894615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
42904615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
42914615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
42924615fd15SEmmanuel Grumbach 		else
42934615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
42944615fd15SEmmanuel Grumbach 
429546c7b05aSEmmanuel Grumbach 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
42966d19a5ebSEmmanuel Grumbach 			mvmsta->pairwise_cipher = key->cipher;
42976d19a5ebSEmmanuel Grumbach 
4298a5de7de7SJohannes Berg 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4299a5de7de7SJohannes Berg 				   sta ? sta->addr : NULL, key->keyidx);
43005c75a208SJohannes Berg 
43015c75a208SJohannes Berg 		if (sec_key_ver)
43025c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
43035c75a208SJohannes Berg 		else
43044615fd15SEmmanuel Grumbach 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
43055c75a208SJohannes Berg 
4306e705c121SKalle Valo 		if (ret) {
4307e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
4308475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
4309d066a530SJohannes Berg 			if (ptk_pn) {
4310d066a530SJohannes Berg 				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4311d066a530SJohannes Berg 				kfree(ptk_pn);
4312d066a530SJohannes Berg 			}
4313e705c121SKalle Valo 			/*
4314e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
4315475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
4316475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
4317475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
4318e705c121SKalle Valo 			 */
4319475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
4320475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
4321475c6bdeSJohannes Berg 			else
4322e705c121SKalle Valo 				ret = 0;
4323e705c121SKalle Valo 		}
4324e705c121SKalle Valo 
4325e705c121SKalle Valo 		break;
4326e705c121SKalle Valo 	case DISABLE_KEY:
4327a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4328a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4329b1fdc250SJohannes Berg 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4330b1fdc250SJohannes Berg 					 NULL);
4331b1fdc250SJohannes Berg 
4332c56e00a3SJohannes Berg 		ret = -ENOENT;
4333c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4334c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
4335c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
4336c56e00a3SJohannes Berg 				ret = 0;
4337c56e00a3SJohannes Berg 			}
4338c56e00a3SJohannes Berg 		}
4339c56e00a3SJohannes Berg 
4340c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
4341c56e00a3SJohannes Berg 		if (ret == 0)
4342c56e00a3SJohannes Berg 			break;
4343c56e00a3SJohannes Berg 
4344e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4345e705c121SKalle Valo 			ret = 0;
4346e705c121SKalle Valo 			break;
4347e705c121SKalle Valo 		}
4348e705c121SKalle Valo 
434946c7b05aSEmmanuel Grumbach 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4350f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4351f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
43522a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
43532a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4354f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
4355f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
4356f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
4357f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4358f5e28eacSJohannes Berg 			if (ptk_pn)
4359f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
4360f5e28eacSJohannes Berg 		}
4361f5e28eacSJohannes Berg 
4362e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
43635c75a208SJohannes Berg 		if (sec_key_ver)
43645c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
43655c75a208SJohannes Berg 		else
4366e705c121SKalle Valo 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4367e705c121SKalle Valo 		break;
4368e705c121SKalle Valo 	default:
4369e705c121SKalle Valo 		ret = -EINVAL;
4370e705c121SKalle Valo 	}
4371e705c121SKalle Valo 
43726569e7d3SJohannes Berg 	return ret;
43736569e7d3SJohannes Berg }
43746569e7d3SJohannes Berg 
4375cbce62a3SMiri Korenblit int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4376cbce62a3SMiri Korenblit 			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
43776569e7d3SJohannes Berg 			struct ieee80211_key_conf *key)
43786569e7d3SJohannes Berg {
43796569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
43806569e7d3SJohannes Berg 	int ret;
43816569e7d3SJohannes Berg 
43826569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
43836569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4384e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
43856569e7d3SJohannes Berg 
4386e705c121SKalle Valo 	return ret;
4387e705c121SKalle Valo }
4388e705c121SKalle Valo 
4389cbce62a3SMiri Korenblit void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4390e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4391e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
4392e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4393e705c121SKalle Valo 				 u32 iv32, u16 *phase1key)
4394e705c121SKalle Valo {
4395e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4396e705c121SKalle Valo 
4397e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4398e705c121SKalle Valo 		return;
4399e705c121SKalle Valo 
4400e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4401e705c121SKalle Valo }
4402e705c121SKalle Valo 
4403e705c121SKalle Valo 
4404e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4405e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
4406e705c121SKalle Valo {
4407e705c121SKalle Valo 	struct iwl_mvm *mvm =
4408e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
4409e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
4410e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
4411e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
4412e705c121SKalle Valo 
4413e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4414e705c121SKalle Valo 		return true;
4415e705c121SKalle Valo 
4416e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4417e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4418e705c121SKalle Valo 		return true;
4419e705c121SKalle Valo 	}
4420e705c121SKalle Valo 
4421e705c121SKalle Valo 	resp = (void *)pkt->data;
4422e705c121SKalle Valo 
4423e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
4424b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4425e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
4426e705c121SKalle Valo 
4427e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4428e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4429e705c121SKalle Valo 		     te_data->uid);
4430e705c121SKalle Valo 
4431e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4432e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4433e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4434e705c121SKalle Valo 
4435e705c121SKalle Valo 	return true;
4436e705c121SKalle Valo }
4437e705c121SKalle Valo 
4438e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4439e705c121SKalle Valo 				    struct ieee80211_channel *channel,
4440e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4441e705c121SKalle Valo 				    int duration)
4442e705c121SKalle Valo {
4443afc1e3b4SAvraham Stern 	int res;
4444e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4445e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4446e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4447e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
4448dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
4449e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
4450e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4451e705c121SKalle Valo 		.id_and_color =
4452e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4453e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4454e705c121SKalle Valo 	};
445557e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
445657e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
445757e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
445857e861d9SDavid Spinadel 
445957e861d9SDavid Spinadel 	/* Set the channel info data */
446057e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
44613717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
44627ac87575SJohannes Berg 			      IWL_PHY_CHANNEL_MODE20,
446357e861d9SDavid Spinadel 			      0);
446457e861d9SDavid Spinadel 
446557e861d9SDavid Spinadel 	/* Set the time and duration */
4466afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4467e705c121SKalle Valo 
446867ac248eSShaul Triebitz 	iwl_mvm_roc_duration_and_delay(vif, duration, &req_dur, &delay);
446957e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
447057e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
4471dc28e12fSMatti Gottlieb 
4472dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
4473903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
4474903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
4475903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
447667ac248eSShaul Triebitz 		     "\t(requested = %ums, max_delay = %ums)\n",
447767ac248eSShaul Triebitz 		     duration, delay);
4478903b3f9bSEmmanuel Grumbach 
4479e705c121SKalle Valo 	/* Set the node address */
448057e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4481e705c121SKalle Valo 
4482e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4483e705c121SKalle Valo 
4484e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4485e705c121SKalle Valo 
4486e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4487e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4488e705c121SKalle Valo 		return -EIO;
4489e705c121SKalle Valo 	}
4490e705c121SKalle Valo 
4491e705c121SKalle Valo 	te_data->vif = vif;
4492e705c121SKalle Valo 	te_data->duration = duration;
4493e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
4494e705c121SKalle Valo 
4495e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4496e705c121SKalle Valo 
4497e705c121SKalle Valo 	/*
4498e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
4499e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
4500e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
4501e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4502e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
4503e705c121SKalle Valo 	 * time another notification (that the time event started)
4504e705c121SKalle Valo 	 * might already be processed unsuccessfully.
4505e705c121SKalle Valo 	 */
4506e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4507e705c121SKalle Valo 				   time_event_response,
4508e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
4509e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
4510e705c121SKalle Valo 
451157e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4512e705c121SKalle Valo 				   &aux_roc_req);
4513e705c121SKalle Valo 
4514e705c121SKalle Valo 	if (res) {
4515e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4516e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4517e705c121SKalle Valo 		goto out_clear_te;
4518e705c121SKalle Valo 	}
4519e705c121SKalle Valo 
4520e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4521e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4522e705c121SKalle Valo 	/* should never fail */
4523e705c121SKalle Valo 	WARN_ON_ONCE(res);
4524e705c121SKalle Valo 
4525e705c121SKalle Valo 	if (res) {
4526e705c121SKalle Valo  out_clear_te:
4527e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
4528e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
4529e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4530e705c121SKalle Valo 	}
4531e705c121SKalle Valo 
4532e705c121SKalle Valo 	return res;
4533e705c121SKalle Valo }
4534e705c121SKalle Valo 
4535feebebaeSMiri Korenblit static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4536feebebaeSMiri Korenblit {
4537feebebaeSMiri Korenblit 	int ret = 0;
4538feebebaeSMiri Korenblit 
4539feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4540feebebaeSMiri Korenblit 
4541feebebaeSMiri Korenblit 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4542feebebaeSMiri Korenblit 			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4543feebebaeSMiri Korenblit 		IWL_ERR(mvm, "hotspot not supported\n");
4544feebebaeSMiri Korenblit 		return -EINVAL;
4545feebebaeSMiri Korenblit 	}
4546feebebaeSMiri Korenblit 
45471724fc78SEmmanuel Grumbach 	if (iwl_mvm_has_new_station_api(mvm->fw)) {
4548feebebaeSMiri Korenblit 		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4549feebebaeSMiri Korenblit 		WARN(ret, "Failed to allocate aux station");
4550feebebaeSMiri Korenblit 	}
4551feebebaeSMiri Korenblit 
4552feebebaeSMiri Korenblit 	return ret;
4553feebebaeSMiri Korenblit }
4554feebebaeSMiri Korenblit 
455584ef7cbeSIlan Peer static int iwl_mvm_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4556feebebaeSMiri Korenblit {
455784ef7cbeSIlan Peer 	int ret;
4558feebebaeSMiri Korenblit 
4559feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4560feebebaeSMiri Korenblit 
4561feebebaeSMiri Korenblit 	ret = iwl_mvm_binding_add_vif(mvm, vif);
456284ef7cbeSIlan Peer 	if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4563feebebaeSMiri Korenblit 		return ret;
456484ef7cbeSIlan Peer 
456584ef7cbeSIlan Peer 	/* The station and queue allocation must be done only after the binding
456684ef7cbeSIlan Peer 	 * is done, as otherwise the FW might incorrectly configure its state.
456784ef7cbeSIlan Peer 	 */
456884ef7cbeSIlan Peer 	return iwl_mvm_add_p2p_bcast_sta(mvm, vif);
4569feebebaeSMiri Korenblit }
4570feebebaeSMiri Korenblit 
4571e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
4572e705c121SKalle Valo 		       struct ieee80211_vif *vif,
4573e705c121SKalle Valo 		       struct ieee80211_channel *channel,
4574e705c121SKalle Valo 		       int duration,
4575e705c121SKalle Valo 		       enum ieee80211_roc_type type)
4576e705c121SKalle Valo {
4577a2906ea6SJohannes Berg 	static const struct iwl_mvm_roc_ops ops = {
4578fe8b2ad3SMiri Korenblit 		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
457984ef7cbeSIlan Peer 		.link = iwl_mvm_roc_link,
4580fe8b2ad3SMiri Korenblit 	};
4581fe8b2ad3SMiri Korenblit 
4582fe8b2ad3SMiri Korenblit 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4583fe8b2ad3SMiri Korenblit }
4584fe8b2ad3SMiri Korenblit 
458567ac248eSShaul Triebitz static int iwl_mvm_roc_station(struct iwl_mvm *mvm,
458667ac248eSShaul Triebitz 			       struct ieee80211_channel *channel,
458767ac248eSShaul Triebitz 			       struct ieee80211_vif *vif,
458867ac248eSShaul Triebitz 			       int duration)
458967ac248eSShaul Triebitz {
459067ac248eSShaul Triebitz 	int ret;
459167ac248eSShaul Triebitz 	u32 cmd_id = WIDE_ID(MAC_CONF_GROUP, ROC_CMD);
459267ac248eSShaul Triebitz 	u8 fw_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
459367ac248eSShaul Triebitz 					  IWL_FW_CMD_VER_UNKNOWN);
459467ac248eSShaul Triebitz 
459567ac248eSShaul Triebitz 	if (fw_ver == IWL_FW_CMD_VER_UNKNOWN) {
459667ac248eSShaul Triebitz 		ret = iwl_mvm_send_aux_roc_cmd(mvm, channel, vif, duration);
459767ac248eSShaul Triebitz 	} else if (fw_ver == 3) {
459867ac248eSShaul Triebitz 		ret = iwl_mvm_roc_add_cmd(mvm, channel, vif, duration,
459967ac248eSShaul Triebitz 					  ROC_ACTIVITY_HOTSPOT);
460067ac248eSShaul Triebitz 	} else {
460167ac248eSShaul Triebitz 		ret = -EOPNOTSUPP;
460267ac248eSShaul Triebitz 		IWL_ERR(mvm, "ROC command version %d mismatch!\n", fw_ver);
460367ac248eSShaul Triebitz 	}
460467ac248eSShaul Triebitz 
460567ac248eSShaul Triebitz 	return ret;
460667ac248eSShaul Triebitz }
460767ac248eSShaul Triebitz 
460834cc3a4aSEmmanuel Grumbach static int iwl_mvm_p2p_find_phy_ctxt(struct iwl_mvm *mvm,
460934cc3a4aSEmmanuel Grumbach 				     struct ieee80211_vif *vif,
461034cc3a4aSEmmanuel Grumbach 				     struct ieee80211_channel *channel)
461134cc3a4aSEmmanuel Grumbach {
461234cc3a4aSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
461334cc3a4aSEmmanuel Grumbach 	struct cfg80211_chan_def chandef;
461434cc3a4aSEmmanuel Grumbach 	int i;
461534cc3a4aSEmmanuel Grumbach 
461634cc3a4aSEmmanuel Grumbach 	lockdep_assert_held(&mvm->mutex);
461734cc3a4aSEmmanuel Grumbach 
461834cc3a4aSEmmanuel Grumbach 	if (mvmvif->deflink.phy_ctxt &&
461934cc3a4aSEmmanuel Grumbach 	    channel == mvmvif->deflink.phy_ctxt->channel)
462034cc3a4aSEmmanuel Grumbach 		return 0;
462134cc3a4aSEmmanuel Grumbach 
462234cc3a4aSEmmanuel Grumbach 	/* Try using a PHY context that is already in use */
462334cc3a4aSEmmanuel Grumbach 	for (i = 0; i < NUM_PHY_CTX; i++) {
462434cc3a4aSEmmanuel Grumbach 		struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[i];
462534cc3a4aSEmmanuel Grumbach 
462634cc3a4aSEmmanuel Grumbach 		if (!phy_ctxt->ref || mvmvif->deflink.phy_ctxt == phy_ctxt)
462734cc3a4aSEmmanuel Grumbach 			continue;
462834cc3a4aSEmmanuel Grumbach 
462934cc3a4aSEmmanuel Grumbach 		if (channel == phy_ctxt->channel) {
463034cc3a4aSEmmanuel Grumbach 			if (mvmvif->deflink.phy_ctxt)
463134cc3a4aSEmmanuel Grumbach 				iwl_mvm_phy_ctxt_unref(mvm,
463234cc3a4aSEmmanuel Grumbach 						       mvmvif->deflink.phy_ctxt);
463334cc3a4aSEmmanuel Grumbach 
463434cc3a4aSEmmanuel Grumbach 			mvmvif->deflink.phy_ctxt = phy_ctxt;
463534cc3a4aSEmmanuel Grumbach 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
463634cc3a4aSEmmanuel Grumbach 			return 0;
463734cc3a4aSEmmanuel Grumbach 		}
463834cc3a4aSEmmanuel Grumbach 	}
463934cc3a4aSEmmanuel Grumbach 
464034cc3a4aSEmmanuel Grumbach 	/* We already have a phy_ctxt, but it's not on the right channel */
464134cc3a4aSEmmanuel Grumbach 	if (mvmvif->deflink.phy_ctxt)
464234cc3a4aSEmmanuel Grumbach 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
464334cc3a4aSEmmanuel Grumbach 
464434cc3a4aSEmmanuel Grumbach 	mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
464534cc3a4aSEmmanuel Grumbach 	if (!mvmvif->deflink.phy_ctxt)
464634cc3a4aSEmmanuel Grumbach 		return -ENOSPC;
464734cc3a4aSEmmanuel Grumbach 
464834cc3a4aSEmmanuel Grumbach 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
464934cc3a4aSEmmanuel Grumbach 
465034cc3a4aSEmmanuel Grumbach 	return iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt,
465134cc3a4aSEmmanuel Grumbach 				    &chandef, 1, 1);
465234cc3a4aSEmmanuel Grumbach }
465334cc3a4aSEmmanuel Grumbach 
4654fe8b2ad3SMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
4655fe8b2ad3SMiri Korenblit int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4656fe8b2ad3SMiri Korenblit 		       struct ieee80211_channel *channel, int duration,
4657fe8b2ad3SMiri Korenblit 		       enum ieee80211_roc_type type,
4658a2906ea6SJohannes Berg 		       const struct iwl_mvm_roc_ops *ops)
4659fe8b2ad3SMiri Korenblit {
4660e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4661feebebaeSMiri Korenblit 	u32 lmac_id;
466234cc3a4aSEmmanuel Grumbach 	int ret;
4663e705c121SKalle Valo 
4664e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4665e705c121SKalle Valo 			   duration, type);
4666e705c121SKalle Valo 
46679834781cSJohannes Berg 	/*
46689834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
46699834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
46709834781cSJohannes Berg 	 */
4671e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
4672e705c121SKalle Valo 
4673e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4674e705c121SKalle Valo 
4675e705c121SKalle Valo 	switch (vif->type) {
4676e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4677d51439a6SAriel Malamud 		lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
46782c2c3647SNathan Errera 
4679feebebaeSMiri Korenblit 		/* Use aux roc framework (HS20) */
4680fe8b2ad3SMiri Korenblit 		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4681feebebaeSMiri Korenblit 		if (!ret)
468267ac248eSShaul Triebitz 			ret = iwl_mvm_roc_station(mvm, channel, vif, duration);
4683e705c121SKalle Valo 		goto out_unlock;
4684e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
4685e705c121SKalle Valo 		/* handle below */
4686e705c121SKalle Valo 		break;
4687e705c121SKalle Valo 	default:
468884ef7cbeSIlan Peer 		IWL_ERR(mvm, "ROC: Invalid vif type=%u\n", vif->type);
4689e705c121SKalle Valo 		ret = -EINVAL;
4690e705c121SKalle Valo 		goto out_unlock;
4691e705c121SKalle Valo 	}
4692e705c121SKalle Valo 
4693e705c121SKalle Valo 
469434cc3a4aSEmmanuel Grumbach 	ret = iwl_mvm_p2p_find_phy_ctxt(mvm, vif, channel);
469534cc3a4aSEmmanuel Grumbach 	if (ret)
4696e705c121SKalle Valo 		goto out_unlock;
4697e705c121SKalle Valo 
469884ef7cbeSIlan Peer 	ret = ops->link(mvm, vif);
4699feebebaeSMiri Korenblit 	if (ret)
4700e705c121SKalle Valo 		goto out_unlock;
4701e705c121SKalle Valo 
4702e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4703e705c121SKalle Valo out_unlock:
4704e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4705e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4706e705c121SKalle Valo 	return ret;
4707e705c121SKalle Valo }
4708e705c121SKalle Valo 
4709fe8b2ad3SMiri Korenblit int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
47105db4c4b9SEmmanuel Grumbach 		       struct ieee80211_vif *vif)
4711e705c121SKalle Valo {
4712e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4713e705c121SKalle Valo 
4714e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4715e705c121SKalle Valo 
4716e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4717fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
4718e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4719e705c121SKalle Valo 
4720e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4721e705c121SKalle Valo 	return 0;
4722e705c121SKalle Valo }
4723e705c121SKalle Valo 
4724b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
4725b73f9a4aSJohannes Berg 	bool responder;
4726b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
4727b73f9a4aSJohannes Berg };
4728b73f9a4aSJohannes Berg 
4729b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4730b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
4731b73f9a4aSJohannes Berg {
4732b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4733b73f9a4aSJohannes Berg 
4734d0a9123eSJohannes Berg 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4735b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4736b73f9a4aSJohannes Berg 		data->responder = true;
4737b73f9a4aSJohannes Berg }
4738b73f9a4aSJohannes Berg 
4739ce1fa3adSJohannes Berg bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4740b73f9a4aSJohannes Berg 				      struct ieee80211_chanctx_conf *ctx)
4741b73f9a4aSJohannes Berg {
4742b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
4743b73f9a4aSJohannes Berg 		.responder = false,
4744b73f9a4aSJohannes Berg 		.ctx = ctx,
4745b73f9a4aSJohannes Berg 	};
4746b73f9a4aSJohannes Berg 
4747b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4748b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
4749b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
4750b73f9a4aSJohannes Berg 					&data);
4751b73f9a4aSJohannes Berg 	return data.responder;
4752b73f9a4aSJohannes Berg }
4753b73f9a4aSJohannes Berg 
4754e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4755e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
4756e705c121SKalle Valo {
4757e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4758e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4759ce1fa3adSJohannes Berg 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
4760e705c121SKalle Valo 	int ret;
4761e705c121SKalle Valo 
4762e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4763e705c121SKalle Valo 
4764e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4765e705c121SKalle Valo 
4766e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4767e705c121SKalle Valo 	if (!phy_ctxt) {
4768e705c121SKalle Valo 		ret = -ENOSPC;
4769e705c121SKalle Valo 		goto out;
4770e705c121SKalle Valo 	}
4771e705c121SKalle Valo 
4772f3276ff0SEmmanuel Grumbach 	ret = iwl_mvm_phy_ctxt_add(mvm, phy_ctxt, def,
4773e705c121SKalle Valo 				   ctx->rx_chains_static,
4774e705c121SKalle Valo 				   ctx->rx_chains_dynamic);
4775e705c121SKalle Valo 	if (ret) {
4776e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4777e705c121SKalle Valo 		goto out;
4778e705c121SKalle Valo 	}
4779e705c121SKalle Valo 
4780e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4781e705c121SKalle Valo out:
4782e705c121SKalle Valo 	return ret;
4783e705c121SKalle Valo }
4784e705c121SKalle Valo 
4785cbce62a3SMiri Korenblit int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4786e705c121SKalle Valo 			struct ieee80211_chanctx_conf *ctx)
4787e705c121SKalle Valo {
4788e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4789e705c121SKalle Valo 	int ret;
4790e705c121SKalle Valo 
4791e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4792e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4793e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4794e705c121SKalle Valo 
4795e705c121SKalle Valo 	return ret;
4796e705c121SKalle Valo }
4797e705c121SKalle Valo 
4798e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4799e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4800e705c121SKalle Valo {
4801e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4802e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4803e705c121SKalle Valo 
4804e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4805e705c121SKalle Valo 
4806e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4807e705c121SKalle Valo }
4808e705c121SKalle Valo 
4809cbce62a3SMiri Korenblit void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4810e705c121SKalle Valo 			    struct ieee80211_chanctx_conf *ctx)
4811e705c121SKalle Valo {
4812e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4813e705c121SKalle Valo 
4814e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4815e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4816e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4817e705c121SKalle Valo }
4818e705c121SKalle Valo 
4819cbce62a3SMiri Korenblit void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4820cbce62a3SMiri Korenblit 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
4821e705c121SKalle Valo {
4822e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4823e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4824e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4825ce1fa3adSJohannes Berg 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
4826e705c121SKalle Valo 
4827e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4828e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4829e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4830e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4831e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4832e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4833e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4834e705c121SKalle Valo 		return;
4835e705c121SKalle Valo 
4836e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
48377a20bcceSEmmanuel Grumbach 
48387a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
48397a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4840b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
48417a20bcceSEmmanuel Grumbach 			goto out_unlock;
48427a20bcceSEmmanuel Grumbach 
48437a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
48447a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4845b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
48467a20bcceSEmmanuel Grumbach 			goto out_unlock;
48477a20bcceSEmmanuel Grumbach 	}
48487a20bcceSEmmanuel Grumbach 
4849e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4850b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4851e705c121SKalle Valo 				 ctx->rx_chains_static,
4852e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
48537a20bcceSEmmanuel Grumbach 
48547a20bcceSEmmanuel Grumbach out_unlock:
4855e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4856e705c121SKalle Valo }
4857e705c121SKalle Valo 
48588a919a78SMiri Korenblit /*
48598a919a78SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
48608a919a78SMiri Korenblit  *
48618a919a78SMiri Korenblit  * Returns true if we're done assigning the chanctx
48628a919a78SMiri Korenblit  * (either on failure or success)
48638a919a78SMiri Korenblit  */
48644263ac7fSGregory Greenman static bool
48654263ac7fSGregory Greenman __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
4866e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4867e705c121SKalle Valo 				    struct ieee80211_chanctx_conf *ctx,
486850e81437SMiri Korenblit 				    bool switching_chanctx, int *ret)
4869e705c121SKalle Valo {
4870e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4871e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4872e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4873e705c121SKalle Valo 
4874e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4875e705c121SKalle Valo 
4876650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = phy_ctxt;
4877e705c121SKalle Valo 
4878e705c121SKalle Valo 	switch (vif->type) {
4879e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4880e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4881e705c121SKalle Valo 		if (switching_chanctx) {
4882e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4883e705c121SKalle Valo 			break;
4884e705c121SKalle Valo 		}
48855a2abdcaSGustavo A. R. Silva 		fallthrough;
4886e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4887e705c121SKalle Valo 		/*
4888e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4889e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4890e705c121SKalle Valo 		 */
48918a919a78SMiri Korenblit 		*ret = 0;
48928a919a78SMiri Korenblit 		return true;
4893e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4894e705c121SKalle Valo 		break;
4895e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4896e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4897e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4898e705c121SKalle Valo 		break;
4899e705c121SKalle Valo 	default:
49008a919a78SMiri Korenblit 		*ret = -EINVAL;
49018a919a78SMiri Korenblit 		return true;
4902e705c121SKalle Valo 	}
49038a919a78SMiri Korenblit 	return false;
49048a919a78SMiri Korenblit }
49058a919a78SMiri Korenblit 
49068a919a78SMiri Korenblit static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
49078a919a78SMiri Korenblit 					struct ieee80211_vif *vif,
49084263ac7fSGregory Greenman 					struct ieee80211_bss_conf *link_conf,
49098a919a78SMiri Korenblit 					struct ieee80211_chanctx_conf *ctx,
49108a919a78SMiri Korenblit 					bool switching_chanctx)
49118a919a78SMiri Korenblit {
49128a919a78SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
49138a919a78SMiri Korenblit 	int ret;
49148a919a78SMiri Korenblit 
49154263ac7fSGregory Greenman 	if (WARN_ON(!link_conf))
49164263ac7fSGregory Greenman 		return -EINVAL;
49174263ac7fSGregory Greenman 
49188a919a78SMiri Korenblit 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
49198a919a78SMiri Korenblit 						switching_chanctx, &ret))
49208a919a78SMiri Korenblit 		goto out;
4921e705c121SKalle Valo 
4922e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4923e705c121SKalle Valo 	if (ret)
4924e705c121SKalle Valo 		goto out;
4925e705c121SKalle Valo 
4926e705c121SKalle Valo 	/*
4927e705c121SKalle Valo 	 * Power state must be updated before quotas,
4928e705c121SKalle Valo 	 * otherwise fw will complain.
4929e705c121SKalle Valo 	 */
4930e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4931e705c121SKalle Valo 
4932e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4933e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4934e705c121SKalle Valo 	 * will handle quota settings.
4935e705c121SKalle Valo 	 */
4936e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4937e705c121SKalle Valo 		mvmvif->monitor_active = true;
4938e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4939e705c121SKalle Valo 		if (ret)
4940e705c121SKalle Valo 			goto out_remove_binding;
49410e39eb03SChaya Rachel Ivgi 
49420e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
49430e39eb03SChaya Rachel Ivgi 		if (ret)
49440e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
49450e39eb03SChaya Rachel Ivgi 
4946e705c121SKalle Valo 	}
4947e705c121SKalle Valo 
4948e705c121SKalle Valo 	/* Handle binding during CSA */
4949e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4950e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4951e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4952e705c121SKalle Valo 	}
4953e705c121SKalle Valo 
49548a919a78SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION) {
49558a919a78SMiri Korenblit 		if (!switching_chanctx) {
49568a919a78SMiri Korenblit 			mvmvif->csa_bcn_pending = false;
49578a919a78SMiri Korenblit 			goto out;
49588a919a78SMiri Korenblit 		}
49598a919a78SMiri Korenblit 
496074a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
496174a10252SSara Sharon 
496274a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
496374a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
496411d0d831SJohannes Berg 			u32 duration = 5 * vif->bss_conf.beacon_int;
4965e705c121SKalle Valo 
4966e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4967e705c121SKalle Valo 			 * beacon on the new channel.
4968e705c121SKalle Valo 			 */
4969e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4970e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4971e705c121SKalle Valo 						true);
497274a10252SSara Sharon 		}
4973e705c121SKalle Valo 
4974e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4975e705c121SKalle Valo 	}
4976e705c121SKalle Valo 
4977e705c121SKalle Valo 	goto out;
4978e705c121SKalle Valo 
4979e705c121SKalle Valo out_remove_binding:
4980e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4981e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4982e705c121SKalle Valo out:
4983e705c121SKalle Valo 	if (ret)
4984650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = NULL;
4985e705c121SKalle Valo 	return ret;
4986e705c121SKalle Valo }
49878a919a78SMiri Korenblit 
4988e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4989e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4990727eff4dSGregory Greenman 				      struct ieee80211_bss_conf *link_conf,
4991e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4992e705c121SKalle Valo {
4993e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4994e705c121SKalle Valo 	int ret;
4995e705c121SKalle Valo 
4996e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
49974263ac7fSGregory Greenman 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
4998e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4999e705c121SKalle Valo 
5000e705c121SKalle Valo 	return ret;
5001e705c121SKalle Valo }
5002e705c121SKalle Valo 
5003daddfae5SMiri Korenblit /*
5004daddfae5SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
5005daddfae5SMiri Korenblit  *
5006daddfae5SMiri Korenblit  * Returns if chanctx unassign chanctx is done
5007daddfae5SMiri Korenblit  * (either on failure or success)
5008daddfae5SMiri Korenblit  */
50094263ac7fSGregory Greenman static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
5010e705c121SKalle Valo 						  struct ieee80211_vif *vif,
5011e705c121SKalle Valo 						  bool switching_chanctx)
5012e705c121SKalle Valo {
5013e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5014e705c121SKalle Valo 
5015e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
5016650cadb7SGregory Greenman 	iwl_mvm_remove_time_event(mvm, mvmvif,
5017650cadb7SGregory Greenman 				  &mvmvif->time_event_data);
5018e705c121SKalle Valo 
5019e705c121SKalle Valo 	switch (vif->type) {
5020e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
5021daddfae5SMiri Korenblit 		return true;
5022e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
5023e705c121SKalle Valo 		mvmvif->monitor_active = false;
5024e705c121SKalle Valo 		mvmvif->ps_disabled = false;
5025e705c121SKalle Valo 		break;
5026e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5027e705c121SKalle Valo 		/* This part is triggered only during CSA */
5028e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
5029daddfae5SMiri Korenblit 			return true;
5030e705c121SKalle Valo 
5031e705c121SKalle Valo 		mvmvif->csa_countdown = false;
5032e705c121SKalle Valo 
5033e705c121SKalle Valo 		/* Set CS bit on all the stations */
5034e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5035e705c121SKalle Valo 
5036e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
5037e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5038e705c121SKalle Valo 
5039e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
5040e705c121SKalle Valo 		break;
5041daddfae5SMiri Korenblit 	default:
5042e705c121SKalle Valo 		break;
5043daddfae5SMiri Korenblit 	}
5044daddfae5SMiri Korenblit 	return false;
5045daddfae5SMiri Korenblit }
5046e705c121SKalle Valo 
5047e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5048e705c121SKalle Valo 					   struct ieee80211_vif *vif,
50494263ac7fSGregory Greenman 					   struct ieee80211_bss_conf *link_conf,
5050e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
5051e705c121SKalle Valo 					   bool switching_chanctx)
5052e705c121SKalle Valo {
5053e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5054e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
5055e705c121SKalle Valo 
5056daddfae5SMiri Korenblit 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5057e705c121SKalle Valo 		goto out;
5058daddfae5SMiri Korenblit 
5059daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_MONITOR)
5060e705c121SKalle Valo 		iwl_mvm_rm_snif_sta(mvm, vif);
5061e705c121SKalle Valo 
5062e705c121SKalle Valo 
5063daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5064e705c121SKalle Valo 		disabled_vif = vif;
506574a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
506674a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5067e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5068e705c121SKalle Valo 	}
5069e705c121SKalle Valo 
5070e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5071e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
5072e705c121SKalle Valo 
5073e705c121SKalle Valo out:
5074bf544e9aSSara Sharon 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5075bf544e9aSSara Sharon 	    switching_chanctx)
5076bf544e9aSSara Sharon 		return;
5077650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
5078e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5079e705c121SKalle Valo }
5080e705c121SKalle Valo 
5081e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5082e705c121SKalle Valo 					 struct ieee80211_vif *vif,
5083727eff4dSGregory Greenman 					 struct ieee80211_bss_conf *link_conf,
5084e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
5085e705c121SKalle Valo {
5086e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5087e705c121SKalle Valo 
5088e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
50894263ac7fSGregory Greenman 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5090e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5091e705c121SKalle Valo }
5092e705c121SKalle Valo 
5093e705c121SKalle Valo static int
5094e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5095660eba5aSMiri Korenblit 				struct ieee80211_vif_chanctx_switch *vifs,
5096a2906ea6SJohannes Berg 				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5097e705c121SKalle Valo {
5098e705c121SKalle Valo 	int ret;
5099e705c121SKalle Valo 
5100e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
51014263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51024263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5103e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5104e705c121SKalle Valo 
5105e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5106e705c121SKalle Valo 	if (ret) {
5107e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5108e705c121SKalle Valo 		goto out_reassign;
5109e705c121SKalle Valo 	}
5110e705c121SKalle Valo 
51114263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51124263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5113e705c121SKalle Valo 	if (ret) {
5114e705c121SKalle Valo 		IWL_ERR(mvm,
5115e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5116e705c121SKalle Valo 		goto out_remove;
5117e705c121SKalle Valo 	}
5118e705c121SKalle Valo 
5119e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
5120e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5121e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
5122e705c121SKalle Valo 
5123e705c121SKalle Valo 	goto out;
5124e705c121SKalle Valo 
5125e705c121SKalle Valo out_remove:
5126e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5127e705c121SKalle Valo 
5128e705c121SKalle Valo out_reassign:
5129e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5130e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5131e705c121SKalle Valo 		goto out_restart;
5132e705c121SKalle Valo 	}
5133e705c121SKalle Valo 
51344263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51354263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5136e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5137e705c121SKalle Valo 		goto out_restart;
5138e705c121SKalle Valo 	}
5139e705c121SKalle Valo 
5140e705c121SKalle Valo 	goto out;
5141e705c121SKalle Valo 
5142e705c121SKalle Valo out_restart:
5143e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5144e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5145e705c121SKalle Valo 
5146e705c121SKalle Valo out:
5147e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5148e705c121SKalle Valo 
5149e705c121SKalle Valo 	return ret;
5150e705c121SKalle Valo }
5151e705c121SKalle Valo 
5152e705c121SKalle Valo static int
5153e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5154660eba5aSMiri Korenblit 				    struct ieee80211_vif_chanctx_switch *vifs,
5155a2906ea6SJohannes Berg 				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5156e705c121SKalle Valo {
5157e705c121SKalle Valo 	int ret;
5158e705c121SKalle Valo 
5159e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
51604263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51614263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5162e705c121SKalle Valo 
51634263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51644263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5165e705c121SKalle Valo 	if (ret) {
5166e705c121SKalle Valo 		IWL_ERR(mvm,
5167e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5168e705c121SKalle Valo 		goto out_reassign;
5169e705c121SKalle Valo 	}
5170e705c121SKalle Valo 
5171e705c121SKalle Valo 	goto out;
5172e705c121SKalle Valo 
5173e705c121SKalle Valo out_reassign:
51744263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51754263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5176e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5177e705c121SKalle Valo 		goto out_restart;
5178e705c121SKalle Valo 	}
5179e705c121SKalle Valo 
5180e705c121SKalle Valo 	goto out;
5181e705c121SKalle Valo 
5182e705c121SKalle Valo out_restart:
5183e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5184e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5185e705c121SKalle Valo 
5186e705c121SKalle Valo out:
5187e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5188e705c121SKalle Valo 
5189e705c121SKalle Valo 	return ret;
5190e705c121SKalle Valo }
5191e705c121SKalle Valo 
5192660eba5aSMiri Korenblit /* Execute the common part for both MLD and non-MLD modes */
5193660eba5aSMiri Korenblit int
5194660eba5aSMiri Korenblit iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5195e705c121SKalle Valo 				  struct ieee80211_vif_chanctx_switch *vifs,
5196e705c121SKalle Valo 				  int n_vifs,
5197660eba5aSMiri Korenblit 				  enum ieee80211_chanctx_switch_mode mode,
5198a2906ea6SJohannes Berg 				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5199e705c121SKalle Valo {
5200e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5201e705c121SKalle Valo 	int ret;
5202e705c121SKalle Valo 
5203e705c121SKalle Valo 	/* we only support a single-vif right now */
5204e705c121SKalle Valo 	if (n_vifs > 1)
5205e705c121SKalle Valo 		return -EOPNOTSUPP;
5206e705c121SKalle Valo 
5207e705c121SKalle Valo 	switch (mode) {
5208e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5209660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5210e705c121SKalle Valo 		break;
5211e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
5212660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5213e705c121SKalle Valo 		break;
5214e705c121SKalle Valo 	default:
5215e705c121SKalle Valo 		ret = -EOPNOTSUPP;
5216e705c121SKalle Valo 		break;
5217e705c121SKalle Valo 	}
5218e705c121SKalle Valo 
5219e705c121SKalle Valo 	return ret;
5220e705c121SKalle Valo }
5221e705c121SKalle Valo 
5222660eba5aSMiri Korenblit static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5223660eba5aSMiri Korenblit 				      struct ieee80211_vif_chanctx_switch *vifs,
5224660eba5aSMiri Korenblit 				      int n_vifs,
5225660eba5aSMiri Korenblit 				      enum ieee80211_chanctx_switch_mode mode)
5226660eba5aSMiri Korenblit {
5227a2906ea6SJohannes Berg 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5228660eba5aSMiri Korenblit 		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5229660eba5aSMiri Korenblit 		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5230660eba5aSMiri Korenblit 	};
5231660eba5aSMiri Korenblit 
5232660eba5aSMiri Korenblit 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5233660eba5aSMiri Korenblit }
5234660eba5aSMiri Korenblit 
5235cbce62a3SMiri Korenblit int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
52362f0282dbSJohannes Berg {
52372f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
52382f0282dbSJohannes Berg 
52392f0282dbSJohannes Berg 	return mvm->ibss_manager;
52402f0282dbSJohannes Berg }
52412f0282dbSJohannes Berg 
5242a32973eeSJohannes Berg static int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5243e705c121SKalle Valo 			   bool set)
5244e705c121SKalle Valo {
5245e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5246e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5247e705c121SKalle Valo 
5248e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
5249e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
5250e705c121SKalle Valo 		return -EINVAL;
5251e705c121SKalle Valo 	}
5252e705c121SKalle Valo 
525336cf5377SGregory Greenman 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
525436cf5377SGregory Greenman 					       &mvm_sta->vif->bss_conf);
5255e705c121SKalle Valo }
5256e705c121SKalle Valo 
5257e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
5258e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5259e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5260e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5261e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5262e705c121SKalle Valo };
5263e705c121SKalle Valo 
5264e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5265e705c121SKalle Valo 				      struct ieee80211_vif *vif,
5266e705c121SKalle Valo 				      void *data, int len)
5267e705c121SKalle Valo {
5268e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5269e705c121SKalle Valo 	int err;
5270e705c121SKalle Valo 	u32 noa_duration;
5271e705c121SKalle Valo 
52728cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
52738cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
5274e705c121SKalle Valo 	if (err)
5275e705c121SKalle Valo 		return err;
5276e705c121SKalle Valo 
5277e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
5278e705c121SKalle Valo 		return -EINVAL;
5279e705c121SKalle Valo 
5280e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5281e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
5282e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5283e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
5284e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5285e705c121SKalle Valo 			return -EINVAL;
5286e705c121SKalle Valo 
5287e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5288e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
5289e705c121SKalle Valo 			return -EINVAL;
5290e705c121SKalle Valo 
5291e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
5292e705c121SKalle Valo 		mvm->noa_vif = vif;
5293e705c121SKalle Valo 
529422b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
5295e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5296e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
5297e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5298f276e20bSJohannes Berg 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5299e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5300e705c121SKalle Valo 			return -EINVAL;
5301e705c121SKalle Valo 
5302e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
530387f5b5f2SJohannes Berg 			return iwl_mvm_enable_beacon_filter(mvm, vif);
530487f5b5f2SJohannes Berg 		return iwl_mvm_disable_beacon_filter(mvm, vif);
5305e705c121SKalle Valo 	}
5306e705c121SKalle Valo 
5307e705c121SKalle Valo 	return -EOPNOTSUPP;
5308e705c121SKalle Valo }
5309e705c121SKalle Valo 
5310cbce62a3SMiri Korenblit int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5311e705c121SKalle Valo 			     struct ieee80211_vif *vif,
5312e705c121SKalle Valo 			     void *data, int len)
5313e705c121SKalle Valo {
5314e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5315e705c121SKalle Valo 	int err;
5316e705c121SKalle Valo 
5317e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5318e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5319e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5320e705c121SKalle Valo 
5321e705c121SKalle Valo 	return err;
5322e705c121SKalle Valo }
5323e705c121SKalle Valo #endif
5324e705c121SKalle Valo 
5325cbce62a3SMiri Korenblit void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5326e705c121SKalle Valo 			    struct ieee80211_channel_switch *chsw)
5327e705c121SKalle Valo {
5328e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
5329e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
5330e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
5331e705c121SKalle Valo 	 * (which is when the absence time event starts).
5332e705c121SKalle Valo 	 */
5333e705c121SKalle Valo 
5334e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5335e705c121SKalle Valo 			   "dummy channel switch op\n");
5336e705c121SKalle Valo }
5337e705c121SKalle Valo 
533874a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
533974a10252SSara Sharon 				       struct ieee80211_vif *vif,
534074a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
534174a10252SSara Sharon {
534274a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
534374a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
534474a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
534574a10252SSara Sharon 							  mvmvif->color)),
534674a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
534774a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
534874a10252SSara Sharon 		.cs_count = chsw->count,
534977738865SSara Sharon 		.cs_mode = chsw->block_tx,
535074a10252SSara Sharon 	};
535174a10252SSara Sharon 
535274a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
535374a10252SSara Sharon 
53549cfcf71cSSara Sharon 	if (chsw->delay)
53559cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
53569cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
53579cfcf71cSSara Sharon 
535874a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
535974a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
536074a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
536174a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
536274a10252SSara Sharon }
536374a10252SSara Sharon 
53640202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
53650202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
53660202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
53670202bcf0SEmmanuel Grumbach {
53680202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
53690202bcf0SEmmanuel Grumbach 	u32 apply_time;
53700202bcf0SEmmanuel Grumbach 
53710202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
53720202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
53730202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
53740202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
53750202bcf0SEmmanuel Grumbach 	 * loss).
53760202bcf0SEmmanuel Grumbach 	 */
53770202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
53780202bcf0SEmmanuel Grumbach 		apply_time = 0;
53790202bcf0SEmmanuel Grumbach 	else
53800202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
53810202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
53820202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
53830202bcf0SEmmanuel Grumbach 
53840202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
53850202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
53860202bcf0SEmmanuel Grumbach 
53870202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
538887f5b5f2SJohannes Berg 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif);
53890202bcf0SEmmanuel Grumbach 
53900202bcf0SEmmanuel Grumbach 		if (ret)
53910202bcf0SEmmanuel Grumbach 			return ret;
53920202bcf0SEmmanuel Grumbach 	}
53930202bcf0SEmmanuel Grumbach 
53940202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
53950202bcf0SEmmanuel Grumbach 				    apply_time);
53960202bcf0SEmmanuel Grumbach 
53970202bcf0SEmmanuel Grumbach 	return 0;
53980202bcf0SEmmanuel Grumbach }
53990202bcf0SEmmanuel Grumbach 
5400f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
5401cbce62a3SMiri Korenblit int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
5402e705c121SKalle Valo 			       struct ieee80211_vif *vif,
5403e705c121SKalle Valo 			       struct ieee80211_channel_switch *chsw)
5404e705c121SKalle Valo {
5405e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5406e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
5407e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5408e705c121SKalle Valo 	int ret;
5409e705c121SKalle Valo 
5410e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5411e705c121SKalle Valo 
5412e705c121SKalle Valo 	mvmvif->csa_failed = false;
5413e705c121SKalle Valo 
5414e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5415e705c121SKalle Valo 			   chsw->chandef.center_freq1);
5416e705c121SKalle Valo 
54177174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
54187174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
54197174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5420e705c121SKalle Valo 
5421e705c121SKalle Valo 	switch (vif->type) {
5422e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5423e705c121SKalle Valo 		csa_vif =
5424e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
5425e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
5426d0a9123eSJohannes Berg 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5427e705c121SKalle Valo 			      "Another CSA is already in progress")) {
5428e705c121SKalle Valo 			ret = -EBUSY;
5429e705c121SKalle Valo 			goto out_unlock;
5430e705c121SKalle Valo 		}
5431e705c121SKalle Valo 
5432d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
5433d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5434d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
5435d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
5436d3a108a4SAndrei Otcheretianski 			goto out_unlock;
5437d3a108a4SAndrei Otcheretianski 		}
5438d3a108a4SAndrei Otcheretianski 
5439e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
5440e705c121SKalle Valo 
5441e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
5442e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
5443e705c121SKalle Valo 			ret = -EBUSY;
5444e705c121SKalle Valo 			goto out_unlock;
5445e705c121SKalle Valo 		}
5446e705c121SKalle Valo 
5447d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5448d3a108a4SAndrei Otcheretianski 
5449e705c121SKalle Valo 		break;
5450e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
54512360acbdSEmmanuel Grumbach 		/*
5452ad12b231SNathan Errera 		 * In the new flow FW is in charge of timing the switch so there
5453ad12b231SNathan Errera 		 * is no need for all of this
5454ad12b231SNathan Errera 		 */
5455ad12b231SNathan Errera 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5456ad12b231SNathan Errera 					    CHANNEL_SWITCH_ERROR_NOTIF,
5457ad12b231SNathan Errera 					    0))
5458ad12b231SNathan Errera 			break;
5459ad12b231SNathan Errera 
5460ad12b231SNathan Errera 		/*
54612360acbdSEmmanuel Grumbach 		 * We haven't configured the firmware to be associated yet since
54622360acbdSEmmanuel Grumbach 		 * we don't know the dtim period. In this case, the firmware can't
54632360acbdSEmmanuel Grumbach 		 * track the beacons.
54642360acbdSEmmanuel Grumbach 		 */
5465f276e20bSJohannes Berg 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period) {
54662360acbdSEmmanuel Grumbach 			ret = -EBUSY;
54672360acbdSEmmanuel Grumbach 			goto out_unlock;
54682360acbdSEmmanuel Grumbach 		}
54692360acbdSEmmanuel Grumbach 
5470aee2eac7SJohannes Berg 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX &&
5471aee2eac7SJohannes Berg 		    hweight16(vif->valid_links) <= 1)
547287d9564eSJohannes Berg 			schedule_delayed_work(&mvmvif->csa_work, 0);
547387d9564eSJohannes Berg 
5474f6780614SSara Sharon 		if (chsw->block_tx) {
5475f6780614SSara Sharon 			/*
5476f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
5477f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
5478f6780614SSara Sharon 			 */
5479f6780614SSara Sharon 			if (!chsw->count ||
5480f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
5481f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
5482f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
5483f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5484f6780614SSara Sharon 		}
5485e705c121SKalle Valo 
54860202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
54870202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
54880202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5489e705c121SKalle Valo 			if (ret)
5490e705c121SKalle Valo 				goto out_unlock;
54910202bcf0SEmmanuel Grumbach 		} else {
549274a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
54930202bcf0SEmmanuel Grumbach 		}
549481b4e44eSSara Sharon 
549581b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
549681b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
5497e705c121SKalle Valo 		break;
5498e705c121SKalle Valo 	default:
5499e705c121SKalle Valo 		break;
5500e705c121SKalle Valo 	}
5501e705c121SKalle Valo 
5502e705c121SKalle Valo 	mvmvif->ps_disabled = true;
5503e705c121SKalle Valo 
5504e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
5505e705c121SKalle Valo 	if (ret)
5506e705c121SKalle Valo 		goto out_unlock;
5507e705c121SKalle Valo 
5508e705c121SKalle Valo 	/* we won't be on this channel any longer */
5509e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
5510e705c121SKalle Valo 
5511e705c121SKalle Valo out_unlock:
5512e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5513e705c121SKalle Valo 
5514e705c121SKalle Valo 	return ret;
5515e705c121SKalle Valo }
5516e705c121SKalle Valo 
5517cbce62a3SMiri Korenblit void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5518c37763d2SSara Sharon 				      struct ieee80211_vif *vif,
5519c37763d2SSara Sharon 				      struct ieee80211_channel_switch *chsw)
5520e705c121SKalle Valo {
5521e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5522c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5523c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
5524c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5525c37763d2SSara Sharon 							  mvmvif->color)),
5526c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5527c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
5528c37763d2SSara Sharon 		.cs_count = chsw->count,
552977738865SSara Sharon 		.cs_mode = chsw->block_tx,
5530c37763d2SSara Sharon 	};
5531e705c121SKalle Valo 
5532ad12b231SNathan Errera 	/*
5533ad12b231SNathan Errera 	 * In the new flow FW is in charge of timing the switch so there is no
5534ad12b231SNathan Errera 	 * need for all of this
5535ad12b231SNathan Errera 	 */
5536ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5537ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5538ad12b231SNathan Errera 		return;
5539ad12b231SNathan Errera 
5540c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5541c37763d2SSara Sharon 		return;
5542e705c121SKalle Valo 
5543c3eae059SGregory Greenman 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5544c3eae059SGregory Greenman 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5545c3eae059SGregory Greenman 
554681b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
554781b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
554881b4e44eSSara Sharon 			/* Second time, give up on this AP*/
554981b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
5550a469a593SEmmanuel Grumbach 			ieee80211_chswitch_done(vif, false, 0);
555181b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
555281b4e44eSSara Sharon 			return;
5553e705c121SKalle Valo 		}
555481b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
5555e705c121SKalle Valo 	}
555681b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
5557e705c121SKalle Valo 
5558caf46377SSara Sharon 	mutex_lock(&mvm->mutex);
5559caf46377SSara Sharon 	if (mvmvif->csa_failed)
5560caf46377SSara Sharon 		goto out_unlock;
5561e705c121SKalle Valo 
5562c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5563c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
5564c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5565caf46377SSara Sharon 				     0, sizeof(cmd), &cmd));
5566caf46377SSara Sharon out_unlock:
5567caf46377SSara Sharon 	mutex_unlock(&mvm->mutex);
5568e705c121SKalle Valo }
5569e705c121SKalle Valo 
55706110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
55716110d9e5SDavid Spinadel {
5572435d0827SSara Sharon 	int i;
5573435d0827SSara Sharon 
557406195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5575309c4848SLuca Coelho 		if (drop) {
5576309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
55776110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
5578d4e3a341SMordechay Goodstein 				iwl_mvm_flushable_queues(mvm) & queues);
5579309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
5580309c4848SLuca Coelho 		} else {
5581435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5582309c4848SLuca Coelho 		}
5583435d0827SSara Sharon 		return;
5584435d0827SSara Sharon 	}
55856110d9e5SDavid Spinadel 
55866110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
5587be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5588435d0827SSara Sharon 		struct ieee80211_sta *sta;
5589435d0827SSara Sharon 
5590435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
55916110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
55926110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
55936110d9e5SDavid Spinadel 			continue;
55946110d9e5SDavid Spinadel 
559506195639SSara Sharon 		if (drop)
5596d4e3a341SMordechay Goodstein 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
559706195639SSara Sharon 		else
55986110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
55996110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
56006110d9e5SDavid Spinadel 	}
56016110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
56026110d9e5SDavid Spinadel }
56036110d9e5SDavid Spinadel 
5604cbce62a3SMiri Korenblit void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5605cbce62a3SMiri Korenblit 		       u32 queues, bool drop)
5606e705c121SKalle Valo {
5607e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5608e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
5609e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
5610e705c121SKalle Valo 	struct ieee80211_sta *sta;
5611d3f9cd61SJohannes Berg 	bool ap_sta_done = false;
5612e705c121SKalle Valo 	int i;
5613e705c121SKalle Valo 	u32 msk = 0;
5614e705c121SKalle Valo 
56156110d9e5SDavid Spinadel 	if (!vif) {
56166110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
56176110d9e5SDavid Spinadel 		return;
56186110d9e5SDavid Spinadel 	}
56196110d9e5SDavid Spinadel 
562024afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
562124afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
562224afba76SLiad Kaufman 
5623e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5624e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
5625e705c121SKalle Valo 
5626e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
5627be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5628e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5629e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
5630e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
5631e705c121SKalle Valo 			continue;
5632e705c121SKalle Valo 
5633e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5634e705c121SKalle Valo 		if (mvmsta->vif != vif)
5635e705c121SKalle Valo 			continue;
5636e705c121SKalle Valo 
5637d3f9cd61SJohannes Berg 		if (sta == mvmvif->ap_sta) {
5638d3f9cd61SJohannes Berg 			if (ap_sta_done)
5639d3f9cd61SJohannes Berg 				continue;
5640d3f9cd61SJohannes Berg 			ap_sta_done = true;
5641d3f9cd61SJohannes Berg 		}
5642d3f9cd61SJohannes Berg 
5643d49394a1SSara Sharon 		if (drop) {
564439176296SJohannes Berg 			if (iwl_mvm_flush_sta(mvm, mvmsta->deflink.sta_id,
564539176296SJohannes Berg 					      mvmsta->tfd_queue_msk))
5646d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
5647d49394a1SSara Sharon 		} else {
5648d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
5649d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5650f7bd883bSJohannes Berg 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5651f7bd883bSJohannes Berg 				msk |= mvmsta->tfd_queue_msk;
5652e705c121SKalle Valo 		}
5653d49394a1SSara Sharon 	}
5654e705c121SKalle Valo 
5655e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5656e705c121SKalle Valo 
5657e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
5658e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
5659e705c121SKalle Valo 	 */
5660d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5661a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5662e705c121SKalle Valo }
5663e705c121SKalle Valo 
5664a6cc6ccbSJohannes Berg void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5665a6cc6ccbSJohannes Berg 			   struct ieee80211_sta *sta)
5666a6cc6ccbSJohannes Berg {
566743874283SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5668a6cc6ccbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
566943874283SJohannes Berg 	struct iwl_mvm_link_sta *mvm_link_sta;
567043874283SJohannes Berg 	struct ieee80211_link_sta *link_sta;
567143874283SJohannes Berg 	int link_id;
5672a6cc6ccbSJohannes Berg 
5673a6cc6ccbSJohannes Berg 	mutex_lock(&mvm->mutex);
567443874283SJohannes Berg 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
567543874283SJohannes Berg 		mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_id],
5676a6cc6ccbSJohannes Berg 							 lockdep_is_held(&mvm->mutex));
567743874283SJohannes Berg 		if (!mvm_link_sta)
5678a6cc6ccbSJohannes Berg 			continue;
5679a6cc6ccbSJohannes Berg 
568043874283SJohannes Berg 		if (iwl_mvm_flush_sta(mvm, mvm_link_sta->sta_id,
568139176296SJohannes Berg 				      mvmsta->tfd_queue_msk))
5682a6cc6ccbSJohannes Berg 			IWL_ERR(mvm, "flush request fail\n");
5683a6cc6ccbSJohannes Berg 	}
5684a6cc6ccbSJohannes Berg 	mutex_unlock(&mvm->mutex);
5685a6cc6ccbSJohannes Berg }
5686a6cc6ccbSJohannes Berg 
5687cbce62a3SMiri Korenblit int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5688e705c121SKalle Valo 			   struct survey_info *survey)
5689e705c121SKalle Valo {
5690e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5691b6e3d1baSAnjaneyulu 	int ret = 0;
5692b6e3d1baSAnjaneyulu 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
5693b6e3d1baSAnjaneyulu 					   WIDE_ID(SYSTEM_GROUP,
5694b6e3d1baSAnjaneyulu 						   SYSTEM_STATISTICS_CMD),
5695b6e3d1baSAnjaneyulu 					   IWL_FW_CMD_VER_UNKNOWN);
5696e705c121SKalle Valo 
5697e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
5698e705c121SKalle Valo 
5699e705c121SKalle Valo 	/* only support global statistics right now */
5700e705c121SKalle Valo 	if (idx != 0)
5701e705c121SKalle Valo 		return -ENOENT;
5702e705c121SKalle Valo 
5703280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
5704e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
5705e705c121SKalle Valo 		return -ENOENT;
5706e705c121SKalle Valo 
5707e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5708e705c121SKalle Valo 
5709aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
5710e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
5711e705c121SKalle Valo 		if (ret)
5712e705c121SKalle Valo 			goto out;
5713e705c121SKalle Valo 	}
5714e705c121SKalle Valo 
5715b6e3d1baSAnjaneyulu 	survey->filled = SURVEY_INFO_TIME_RX |
5716b6e3d1baSAnjaneyulu 			 SURVEY_INFO_TIME_TX;
5717e705c121SKalle Valo 
5718e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
5719e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
5720e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
5721e705c121SKalle Valo 
5722e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
5723e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
5724e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
5725e705c121SKalle Valo 
5726b6e3d1baSAnjaneyulu 	/* the new fw api doesn't support the following fields */
5727b6e3d1baSAnjaneyulu 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
5728b6e3d1baSAnjaneyulu 		goto out;
5729b6e3d1baSAnjaneyulu 
5730b6e3d1baSAnjaneyulu 	survey->filled |= SURVEY_INFO_TIME |
5731b6e3d1baSAnjaneyulu 			  SURVEY_INFO_TIME_SCAN;
5732b6e3d1baSAnjaneyulu 	survey->time = mvm->accu_radio_stats.on_time_rf +
5733b6e3d1baSAnjaneyulu 		       mvm->radio_stats.on_time_rf;
5734b6e3d1baSAnjaneyulu 	do_div(survey->time, USEC_PER_MSEC);
5735b6e3d1baSAnjaneyulu 
5736e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
5737e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
5738e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
5739e705c121SKalle Valo 
5740e705c121SKalle Valo  out:
5741e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5742e705c121SKalle Valo 	return ret;
5743e705c121SKalle Valo }
5744e705c121SKalle Valo 
5745ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
5746ed780545SJohannes Berg {
574782cdbd11SMiri Korenblit 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
57487138763eSJohannes Berg 	u32 gi_ltf;
574982cdbd11SMiri Korenblit 
575082cdbd11SMiri Korenblit 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
5751ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
5752ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
5753ed780545SJohannes Berg 		break;
5754ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
5755ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
5756ed780545SJohannes Berg 		break;
5757ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
5758ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
5759ed780545SJohannes Berg 		break;
5760ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
5761ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
5762ed780545SJohannes Berg 		break;
576323dcee94SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_320:
576423dcee94SJohannes Berg 		rinfo->bw = RATE_INFO_BW_320;
576523dcee94SJohannes Berg 		break;
5766ed780545SJohannes Berg 	}
5767ed780545SJohannes Berg 
576882cdbd11SMiri Korenblit 	if (format == RATE_MCS_CCK_MSK ||
576982cdbd11SMiri Korenblit 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
577082cdbd11SMiri Korenblit 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
577182cdbd11SMiri Korenblit 
577282cdbd11SMiri Korenblit 		/* add the offset needed to get to the legacy ofdm indices */
577382cdbd11SMiri Korenblit 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
577482cdbd11SMiri Korenblit 			rate += IWL_FIRST_OFDM_RATE;
577582cdbd11SMiri Korenblit 
577682cdbd11SMiri Korenblit 		switch (rate) {
577782cdbd11SMiri Korenblit 		case IWL_RATE_1M_INDEX:
577882cdbd11SMiri Korenblit 			rinfo->legacy = 10;
577982cdbd11SMiri Korenblit 			break;
578082cdbd11SMiri Korenblit 		case IWL_RATE_2M_INDEX:
578182cdbd11SMiri Korenblit 			rinfo->legacy = 20;
578282cdbd11SMiri Korenblit 			break;
578382cdbd11SMiri Korenblit 		case IWL_RATE_5M_INDEX:
578482cdbd11SMiri Korenblit 			rinfo->legacy = 55;
578582cdbd11SMiri Korenblit 			break;
578682cdbd11SMiri Korenblit 		case IWL_RATE_11M_INDEX:
578782cdbd11SMiri Korenblit 			rinfo->legacy = 110;
578882cdbd11SMiri Korenblit 			break;
578982cdbd11SMiri Korenblit 		case IWL_RATE_6M_INDEX:
579082cdbd11SMiri Korenblit 			rinfo->legacy = 60;
579182cdbd11SMiri Korenblit 			break;
579282cdbd11SMiri Korenblit 		case IWL_RATE_9M_INDEX:
579382cdbd11SMiri Korenblit 			rinfo->legacy = 90;
579482cdbd11SMiri Korenblit 			break;
579582cdbd11SMiri Korenblit 		case IWL_RATE_12M_INDEX:
579682cdbd11SMiri Korenblit 			rinfo->legacy = 120;
579782cdbd11SMiri Korenblit 			break;
579882cdbd11SMiri Korenblit 		case IWL_RATE_18M_INDEX:
579982cdbd11SMiri Korenblit 			rinfo->legacy = 180;
580082cdbd11SMiri Korenblit 			break;
580182cdbd11SMiri Korenblit 		case IWL_RATE_24M_INDEX:
580282cdbd11SMiri Korenblit 			rinfo->legacy = 240;
580382cdbd11SMiri Korenblit 			break;
580482cdbd11SMiri Korenblit 		case IWL_RATE_36M_INDEX:
580582cdbd11SMiri Korenblit 			rinfo->legacy = 360;
580682cdbd11SMiri Korenblit 			break;
580782cdbd11SMiri Korenblit 		case IWL_RATE_48M_INDEX:
580882cdbd11SMiri Korenblit 			rinfo->legacy = 480;
580982cdbd11SMiri Korenblit 			break;
581082cdbd11SMiri Korenblit 		case IWL_RATE_54M_INDEX:
581182cdbd11SMiri Korenblit 			rinfo->legacy = 540;
581282cdbd11SMiri Korenblit 		}
581382cdbd11SMiri Korenblit 		return;
581482cdbd11SMiri Korenblit 	}
581582cdbd11SMiri Korenblit 
5816ed780545SJohannes Berg 	rinfo->nss = u32_get_bits(rate_n_flags,
581782cdbd11SMiri Korenblit 				  RATE_MCS_NSS_MSK) + 1;
581882cdbd11SMiri Korenblit 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
581982cdbd11SMiri Korenblit 		RATE_HT_MCS_INDEX(rate_n_flags) :
582082cdbd11SMiri Korenblit 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
582182cdbd11SMiri Korenblit 
58227138763eSJohannes Berg 	if (rate_n_flags & RATE_MCS_SGI_MSK)
58237138763eSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
58247138763eSJohannes Berg 
58257138763eSJohannes Berg 	switch (format) {
582623dcee94SJohannes Berg 	case RATE_MCS_EHT_MSK:
582723dcee94SJohannes Berg 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
582823dcee94SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
582923dcee94SJohannes Berg 		break;
58307138763eSJohannes Berg 	case RATE_MCS_HE_MSK:
58317138763eSJohannes Berg 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
5832ed780545SJohannes Berg 
5833ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
5834ed780545SJohannes Berg 
583582cdbd11SMiri Korenblit 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
5836ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
5837ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
5838ed780545SJohannes Berg 		}
5839ed780545SJohannes Berg 
584082cdbd11SMiri Korenblit 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
584182cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_SU:
584282cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_EXT_SU:
5843ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5844ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5845ed780545SJohannes Berg 			else if (gi_ltf == 2)
5846ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
584782cdbd11SMiri Korenblit 			else if (gi_ltf == 3)
5848ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
584982cdbd11SMiri Korenblit 			else
585082cdbd11SMiri Korenblit 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5851ed780545SJohannes Berg 			break;
585282cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_MU:
5853ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5854ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5855ed780545SJohannes Berg 			else if (gi_ltf == 2)
5856ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5857ed780545SJohannes Berg 			else
5858ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5859ed780545SJohannes Berg 			break;
586082cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_TRIG:
5861ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5862ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5863ed780545SJohannes Berg 			else
5864ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5865ed780545SJohannes Berg 			break;
5866ed780545SJohannes Berg 		}
5867ed780545SJohannes Berg 
5868ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
5869ed780545SJohannes Berg 			rinfo->he_dcm = 1;
58707138763eSJohannes Berg 		break;
58717138763eSJohannes Berg 	case RATE_MCS_HT_MSK:
587282cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
58737138763eSJohannes Berg 		break;
58747138763eSJohannes Berg 	case RATE_MCS_VHT_MSK:
587582cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
58767138763eSJohannes Berg 		break;
5877ed780545SJohannes Berg 	}
5878ed780545SJohannes Berg }
5879ed780545SJohannes Berg 
5880cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
5881e705c121SKalle Valo 				struct ieee80211_vif *vif,
5882e705c121SKalle Valo 				struct ieee80211_sta *sta,
5883e705c121SKalle Valo 				struct station_info *sinfo)
5884e705c121SKalle Valo {
5885e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5886e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5887e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5888b6e3d1baSAnjaneyulu 	int i;
5889e705c121SKalle Valo 
5890c8ee33e1SGregory Greenman 	if (mvmsta->deflink.avg_energy) {
5891c8ee33e1SGregory Greenman 		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
589222d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
5893988b5968SSara Sharon 	}
5894988b5968SSara Sharon 
5895ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
5896c8ee33e1SGregory Greenman 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
5897ed780545SJohannes Berg 
5898ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5899ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5900ed780545SJohannes Berg 	}
5901ed780545SJohannes Berg 
5902e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
5903e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5904e705c121SKalle Valo 		return;
5905e705c121SKalle Valo 
5906f276e20bSJohannes Berg 	if (!vif->cfg.assoc)
5907e705c121SKalle Valo 		return;
5908e705c121SKalle Valo 
5909e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5910e705c121SKalle Valo 
5911c8ee33e1SGregory Greenman 	if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
5912e705c121SKalle Valo 		goto unlock;
5913e705c121SKalle Valo 
5914e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
5915e705c121SKalle Valo 		goto unlock;
5916e705c121SKalle Valo 
5917b6e3d1baSAnjaneyulu 	sinfo->rx_beacon = 0;
5918b6e3d1baSAnjaneyulu 	for_each_mvm_vif_valid_link(mvmvif, i)
5919b6e3d1baSAnjaneyulu 		sinfo->rx_beacon += mvmvif->link[i]->beacon_stats.num_beacons +
5920b6e3d1baSAnjaneyulu 			mvmvif->link[i]->beacon_stats.accu_num_beacons;
5921b6e3d1baSAnjaneyulu 
592222d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
5923650cadb7SGregory Greenman 	if (mvmvif->deflink.beacon_stats.avg_signal) {
5924e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
5925650cadb7SGregory Greenman 		sinfo->rx_beacon_signal_avg =
5926650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.avg_signal;
592722d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
5928e705c121SKalle Valo 	}
5929e705c121SKalle Valo  unlock:
5930e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5931e705c121SKalle Valo }
5932e705c121SKalle Valo 
5933119c2a13SMordechay Goodstein static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5934119c2a13SMordechay Goodstein 					    struct ieee80211_vif *vif,
5935119c2a13SMordechay Goodstein 					    const  struct ieee80211_mlme_event *mlme)
5936119c2a13SMordechay Goodstein {
59371a81bddfSMordechay Goodstein 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
59381a81bddfSMordechay Goodstein 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
5939119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5940119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5941119c2a13SMordechay Goodstein 				       NULL);
5942119c2a13SMordechay Goodstein 		return;
5943119c2a13SMordechay Goodstein 	}
5944119c2a13SMordechay Goodstein 
5945119c2a13SMordechay Goodstein 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5946119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5947119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_DEASSOC,
5948119c2a13SMordechay Goodstein 				       NULL);
5949119c2a13SMordechay Goodstein 		return;
5950119c2a13SMordechay Goodstein 	}
5951119c2a13SMordechay Goodstein }
5952119c2a13SMordechay Goodstein 
5953e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5954e705c121SKalle Valo 					struct ieee80211_vif *vif,
5955e705c121SKalle Valo 					const struct ieee80211_event *event)
5956e705c121SKalle Valo {
59574c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5958e705c121SKalle Valo 	do {								\
59594c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5960e705c121SKalle Valo 			break;						\
59617174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5962e705c121SKalle Valo 	} while (0)
5963e705c121SKalle Valo 
5964e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5965e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5966e705c121SKalle Valo 
5967119c2a13SMordechay Goodstein 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5968119c2a13SMordechay Goodstein 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5969119c2a13SMordechay Goodstein 		return;
5970119c2a13SMordechay Goodstein 	}
5971119c2a13SMordechay Goodstein 
59726c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
59736c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
59746c042d75SSara Sharon 	if (!trig)
5975e705c121SKalle Valo 		return;
5976e705c121SKalle Valo 
5977e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
5978e705c121SKalle Valo 
5979e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
5980e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
59814c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
5982e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
5983e705c121SKalle Valo 					    event->u.mlme.reason);
5984e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
59854c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
5986e705c121SKalle Valo 					   "ASSOC TIMEOUT");
5987e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
5988e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
59894c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
5990e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
5991e705c121SKalle Valo 					   event->u.mlme.reason);
5992e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
59934c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
5994e705c121SKalle Valo 					   "AUTH TIMEOUT");
5995e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
59964c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
5997e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
5998e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
59994c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
6000e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
6001e705c121SKalle Valo 	}
6002e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
6003e705c121SKalle Valo }
6004e705c121SKalle Valo 
6005e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
6006e705c121SKalle Valo 					  struct ieee80211_vif *vif,
6007e705c121SKalle Valo 					  const struct ieee80211_event *event)
6008e705c121SKalle Valo {
6009e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
6010e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
6011e705c121SKalle Valo 
60126c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
60136c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
60146c042d75SSara Sharon 	if (!trig)
6015e705c121SKalle Valo 		return;
6016e705c121SKalle Valo 
6017e705c121SKalle Valo 	ba_trig = (void *)trig->data;
6018e705c121SKalle Valo 
6019e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
6020e705c121SKalle Valo 		return;
6021e705c121SKalle Valo 
60227174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
6023e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
6024e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
6025e705c121SKalle Valo 				event->u.ba.ssn);
6026e705c121SKalle Valo }
6027e705c121SKalle Valo 
6028cbce62a3SMiri Korenblit void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
6029e705c121SKalle Valo 				struct ieee80211_vif *vif,
6030e705c121SKalle Valo 				const struct ieee80211_event *event)
6031e705c121SKalle Valo {
6032e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6033e705c121SKalle Valo 
6034e705c121SKalle Valo 	switch (event->type) {
6035e705c121SKalle Valo 	case MLME_EVENT:
6036e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
6037e705c121SKalle Valo 		break;
6038e705c121SKalle Valo 	case BAR_RX_EVENT:
6039e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
6040e705c121SKalle Valo 		break;
6041e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
6042528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
6043528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
6044e705c121SKalle Valo 		break;
6045e705c121SKalle Valo 	default:
6046e705c121SKalle Valo 		break;
6047e705c121SKalle Valo 	}
6048e705c121SKalle Valo }
6049e705c121SKalle Valo 
605087f690f5SShaul Triebitz #define SYNC_RX_QUEUE_TIMEOUT (HZ)
6051d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
60525e1688ceSJohannes Berg 				     enum iwl_mvm_rxq_notif_type type,
60535e1688ceSJohannes Berg 				     bool sync,
60545e1688ceSJohannes Berg 				     const void *data, u32 size)
60550636b938SSara Sharon {
60565e1688ceSJohannes Berg 	struct {
60575e1688ceSJohannes Berg 		struct iwl_rxq_sync_cmd cmd;
60585e1688ceSJohannes Berg 		struct iwl_mvm_internal_rxq_notif notif;
60595e1688ceSJohannes Berg 	} __packed cmd = {
60605e1688ceSJohannes Berg 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
60615e1688ceSJohannes Berg 		.cmd.count =
60625e1688ceSJohannes Berg 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
60635e1688ceSJohannes Berg 				    size),
60645e1688ceSJohannes Berg 		.notif.type = type,
60655e1688ceSJohannes Berg 		.notif.sync = sync,
60665e1688ceSJohannes Berg 	};
60675e1688ceSJohannes Berg 	struct iwl_host_cmd hcmd = {
60685e1688ceSJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
60695e1688ceSJohannes Berg 		.data[0] = &cmd,
60705e1688ceSJohannes Berg 		.len[0] = sizeof(cmd),
60715e1688ceSJohannes Berg 		.data[1] = data,
60725e1688ceSJohannes Berg 		.len[1] = size,
60735e1688ceSJohannes Berg 		.flags = sync ? 0 : CMD_ASYNC,
60745e1688ceSJohannes Berg 	};
60750636b938SSara Sharon 	int ret;
60760636b938SSara Sharon 
60775e1688ceSJohannes Berg 	/* size must be a multiple of DWORD */
60785e1688ceSJohannes Berg 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
60795e1688ceSJohannes Berg 		return;
60800636b938SSara Sharon 
6081cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
60820636b938SSara Sharon 		return;
60830636b938SSara Sharon 
60845e1688ceSJohannes Berg 	if (sync) {
60855e1688ceSJohannes Berg 		cmd.notif.cookie = mvm->queue_sync_cookie;
60862f7a04c7SJohannes Berg 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6087a2113cc4SNaftali Goldstein 	}
6088d0ff5d22SSara Sharon 
60895e1688ceSJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
60900636b938SSara Sharon 	if (ret) {
60910636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
60920636b938SSara Sharon 		goto out;
60930636b938SSara Sharon 	}
6094d0ff5d22SSara Sharon 
60955e1688ceSJohannes Berg 	if (sync) {
60963c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
60973a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
60982f7a04c7SJohannes Berg 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
60990dd2b42cSJohannes Berg 					 iwl_mvm_is_radio_hw_killed(mvm),
610087f690f5SShaul Triebitz 					 SYNC_RX_QUEUE_TIMEOUT);
61010dd2b42cSJohannes Berg 		WARN_ONCE(!ret && !iwl_mvm_is_radio_hw_killed(mvm),
6102f4eedfd8SJohannes Berg 			  "queue sync: failed to sync, state is 0x%lx, cookie %d\n",
6103f4eedfd8SJohannes Berg 			  mvm->queue_sync_state,
6104f4eedfd8SJohannes Berg 			  mvm->queue_sync_cookie);
61056ad04359SJohannes Berg 	}
61060636b938SSara Sharon 
61070636b938SSara Sharon out:
61085e1688ceSJohannes Berg 	if (sync) {
61092f7a04c7SJohannes Berg 		mvm->queue_sync_state = 0;
61100636b938SSara Sharon 		mvm->queue_sync_cookie++;
61110636b938SSara Sharon 	}
61125f8a3561SJohannes Berg }
61130636b938SSara Sharon 
6114cbce62a3SMiri Korenblit void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
61150636b938SSara Sharon {
61160636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
61170636b938SSara Sharon 
61180636b938SSara Sharon 	mutex_lock(&mvm->mutex);
61195e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
61200636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
61210636b938SSara Sharon }
61220636b938SSara Sharon 
6123cbce62a3SMiri Korenblit int
6124b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6125b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
6126b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
6127b73f9a4aSJohannes Berg {
6128b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6129b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6130b73f9a4aSJohannes Berg 
6131b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6132b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6133b73f9a4aSJohannes Berg 		return -EINVAL;
6134b73f9a4aSJohannes Berg 
6135b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
6136b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
6137b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
6138b73f9a4aSJohannes Berg 
6139b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6140b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6141b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
6142b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
6143b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6144b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6145b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6146b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6147b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6148b73f9a4aSJohannes Berg 
6149b73f9a4aSJohannes Berg 	return 0;
6150b73f9a4aSJohannes Berg }
6151b73f9a4aSJohannes Berg 
6152cbce62a3SMiri Korenblit int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6153fc36ffdaSJohannes Berg 		       struct cfg80211_pmsr_request *request)
6154fc36ffdaSJohannes Berg {
6155fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6156fc36ffdaSJohannes Berg 	int ret;
6157fc36ffdaSJohannes Berg 
6158fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
6159fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
6160fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
6161fc36ffdaSJohannes Berg 
6162fc36ffdaSJohannes Berg 	return ret;
6163fc36ffdaSJohannes Berg }
6164fc36ffdaSJohannes Berg 
6165cbce62a3SMiri Korenblit void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6166fc36ffdaSJohannes Berg 			struct cfg80211_pmsr_request *request)
6167fc36ffdaSJohannes Berg {
6168fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6169fc36ffdaSJohannes Berg 
6170fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
6171fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
6172fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
6173fc36ffdaSJohannes Berg }
6174fc36ffdaSJohannes Berg 
6175438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6176438af969SSara Sharon {
6177438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
6178438af969SSara Sharon 
6179438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
6180438af969SSara Sharon 		return false;
6181438af969SSara Sharon 
6182438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6183438af969SSara Sharon }
6184438af969SSara Sharon 
6185438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6186438af969SSara Sharon 				      struct sk_buff *head,
6187438af969SSara Sharon 				      struct sk_buff *skb)
6188438af969SSara Sharon {
6189438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6190438af969SSara Sharon 
6191438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
6192438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
6193438af969SSara Sharon 		return false;
6194438af969SSara Sharon 
6195438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
6196438af969SSara Sharon 		return true;
6197438af969SSara Sharon 
6198438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6199438af969SSara Sharon }
6200438af969SSara Sharon 
6201be8897e2SAvraham Stern int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6202cf85123aSAvraham Stern 			     struct ieee80211_vif *vif,
6203cf85123aSAvraham Stern 			     struct cfg80211_set_hw_timestamp *hwts)
6204cf85123aSAvraham Stern {
6205cf85123aSAvraham Stern 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6206cf85123aSAvraham Stern 	u32 protocols = 0;
6207cf85123aSAvraham Stern 	int ret;
6208cf85123aSAvraham Stern 
6209cf85123aSAvraham Stern 	/* HW timestamping is only supported for a specific station */
6210cf85123aSAvraham Stern 	if (!hwts->macaddr)
6211cf85123aSAvraham Stern 		return -EOPNOTSUPP;
6212cf85123aSAvraham Stern 
6213cf85123aSAvraham Stern 	if (hwts->enable)
6214cf85123aSAvraham Stern 		protocols =
6215cf85123aSAvraham Stern 			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6216cf85123aSAvraham Stern 
6217cf85123aSAvraham Stern 	mutex_lock(&mvm->mutex);
6218cf85123aSAvraham Stern 	ret = iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6219cf85123aSAvraham Stern 	mutex_unlock(&mvm->mutex);
6220cf85123aSAvraham Stern 
6221cf85123aSAvraham Stern 	return ret;
6222cf85123aSAvraham Stern }
6223cf85123aSAvraham Stern 
6224e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
6225e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
6226cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6227e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
6228e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
62294ea1ed1dSEmmanuel Grumbach 	.set_antenna = iwl_mvm_op_set_antenna,
6230e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
6231e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
6232e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
6233e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
6234e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
6235e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
6236e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
6237e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
6238e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
6239e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
6240e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
6241e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6242e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6243e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
6244e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
6245e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6246e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6247e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6248e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
6249e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
6250e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
62516b1259d1SJohannes Berg 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6252e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6253e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
6254a6cc6ccbSJohannes Berg 	.flush_sta = iwl_mvm_mac_flush_sta,
6255e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
6256e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6257e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
6258e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
6259e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
6260e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
6261e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
6262e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
6263e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
6264e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6265e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6266e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6267e705c121SKalle Valo 
6268ae7ba17bSShaul Triebitz 	.start_ap = iwl_mvm_start_ap,
6269ae7ba17bSShaul Triebitz 	.stop_ap = iwl_mvm_stop_ap,
6270ae7ba17bSShaul Triebitz 	.join_ibss = iwl_mvm_start_ibss,
6271ae7ba17bSShaul Triebitz 	.leave_ibss = iwl_mvm_stop_ibss,
6272e705c121SKalle Valo 
62732f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
62742f0282dbSJohannes Berg 
6275e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
6276e705c121SKalle Valo 
6277e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
6278e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
6279e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
628079221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
6281c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6282e705c121SKalle Valo 
6283e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6284e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6285e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6286e705c121SKalle Valo 
6287e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
6288e705c121SKalle Valo 
62890636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
62900636b938SSara Sharon 
6291e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
6292e705c121SKalle Valo 
6293e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6294e705c121SKalle Valo 	/* look at d3.c */
6295e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
6296e705c121SKalle Valo 	.resume = iwl_mvm_resume,
6297e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
6298e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
6299e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
6300e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6301e705c121SKalle Valo #endif
6302e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6303e705c121SKalle Valo #endif
6304e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
6305e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
6306b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6307fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
6308fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
6309b73f9a4aSJohannes Berg 
6310438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6311177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
6312c36235acSJohannes Berg 	.vif_add_debugfs = iwl_mvm_vif_add_debugfs,
63133f244876SBenjamin Berg 	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
6314177a11cfSGregory Greenman #endif
6315cf85123aSAvraham Stern 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6316e705c121SKalle Valo };
6317