xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision 9410645520e9b820069761f3450ef6661418e279)
18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
3f4eedfd8SJohannes Berg  * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
7e705c121SKalle Valo #include <linux/kernel.h>
8e705c121SKalle Valo #include <linux/slab.h>
9e705c121SKalle Valo #include <linux/skbuff.h>
10e705c121SKalle Valo #include <linux/netdevice.h>
11e705c121SKalle Valo #include <linux/etherdevice.h>
12e705c121SKalle Valo #include <linux/ip.h>
13e705c121SKalle Valo #include <linux/if_arp.h>
142f89a5d7SGolan Ben-Ami #include <linux/time.h>
15e705c121SKalle Valo #include <net/mac80211.h>
16e705c121SKalle Valo #include <net/ieee80211_radiotap.h>
17e705c121SKalle Valo #include <net/tcp.h>
18e705c121SKalle Valo 
195283dd67SMordechay Goodstein #include "iwl-drv.h"
20e705c121SKalle Valo #include "iwl-op-mode.h"
21e705c121SKalle Valo #include "iwl-io.h"
22e705c121SKalle Valo #include "mvm.h"
23e705c121SKalle Valo #include "sta.h"
24e705c121SKalle Valo #include "time-event.h"
256584b9d0SEmmanuel Grumbach #include "iwl-nvm-utils.h"
26e705c121SKalle Valo #include "iwl-phy-db.h"
27e705c121SKalle Valo #include "testmode.h"
28d962f9b1SJohannes Berg #include "fw/error-dump.h"
29e705c121SKalle Valo #include "iwl-prph.h"
30e705c121SKalle Valo #include "iwl-nvm-parse.h"
31cf85123aSAvraham Stern #include "time-sync.h"
32e705c121SKalle Valo 
335c38bedaSJohannes Berg #define IWL_MVM_LIMITS(ap)					\
345c38bedaSJohannes Berg 	{							\
355c38bedaSJohannes Berg 		.max = 1,					\
365c38bedaSJohannes Berg 		.types = BIT(NL80211_IFTYPE_STATION),		\
375c38bedaSJohannes Berg 	},							\
385c38bedaSJohannes Berg 	{							\
395c38bedaSJohannes Berg 		.max = 1,					\
405c38bedaSJohannes Berg 		.types = ap |					\
415c38bedaSJohannes Berg 			 BIT(NL80211_IFTYPE_P2P_CLIENT) |	\
425c38bedaSJohannes Berg 			 BIT(NL80211_IFTYPE_P2P_GO),		\
435c38bedaSJohannes Berg 	},							\
445c38bedaSJohannes Berg 	{							\
455c38bedaSJohannes Berg 		.max = 1,					\
465c38bedaSJohannes Berg 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),	\
475c38bedaSJohannes Berg 	}
485c38bedaSJohannes Berg 
49e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
505c38bedaSJohannes Berg 	IWL_MVM_LIMITS(0)
515c38bedaSJohannes Berg };
525c38bedaSJohannes Berg 
535c38bedaSJohannes Berg static const struct ieee80211_iface_limit iwl_mvm_limits_ap[] = {
545c38bedaSJohannes Berg 	IWL_MVM_LIMITS(BIT(NL80211_IFTYPE_AP))
55e705c121SKalle Valo };
56e705c121SKalle Valo 
57e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
58e705c121SKalle Valo 	{
59e705c121SKalle Valo 		.num_different_channels = 2,
60e705c121SKalle Valo 		.max_interfaces = 3,
61e705c121SKalle Valo 		.limits = iwl_mvm_limits,
624278d88fSJohannes Berg 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
635c38bedaSJohannes Berg 	},
645c38bedaSJohannes Berg 	{
655c38bedaSJohannes Berg 		.num_different_channels = 1,
665c38bedaSJohannes Berg 		.max_interfaces = 3,
675c38bedaSJohannes Berg 		.limits = iwl_mvm_limits_ap,
684278d88fSJohannes Berg 		.n_limits = ARRAY_SIZE(iwl_mvm_limits_ap),
69e705c121SKalle Valo 	},
70e705c121SKalle Valo };
71e705c121SKalle Valo 
72fc36ffdaSJohannes Berg static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
73fc36ffdaSJohannes Berg 	.max_peers = IWL_MVM_TOF_MAX_APS,
74fc36ffdaSJohannes Berg 	.report_ap_tsf = 1,
75fc36ffdaSJohannes Berg 	.randomize_mac_addr = 1,
76fc36ffdaSJohannes Berg 
77fc36ffdaSJohannes Berg 	.ftm = {
78fc36ffdaSJohannes Berg 		.supported = 1,
79fc36ffdaSJohannes Berg 		.asap = 1,
80fc36ffdaSJohannes Berg 		.non_asap = 1,
81fc36ffdaSJohannes Berg 		.request_lci = 1,
82fc36ffdaSJohannes Berg 		.request_civicloc = 1,
836815e3d0SAvraham Stern 		.trigger_based = 1,
846815e3d0SAvraham Stern 		.non_trigger_based = 1,
85fc36ffdaSJohannes Berg 		.max_bursts_exponent = -1, /* all supported */
86fc36ffdaSJohannes Berg 		.max_ftms_per_burst = 0, /* no limits */
87fc36ffdaSJohannes Berg 		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
88fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_20) |
89fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_40) |
908a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_80) |
918a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_160),
92fc36ffdaSJohannes Berg 		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
93fc36ffdaSJohannes Berg 			     BIT(NL80211_PREAMBLE_HT) |
946815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_VHT) |
956815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_HE),
96fc36ffdaSJohannes Berg 	},
97fc36ffdaSJohannes Berg };
98fc36ffdaSJohannes Berg 
996569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
100c56e00a3SJohannes Berg 				 enum set_key_cmd cmd,
101c56e00a3SJohannes Berg 				 struct ieee80211_vif *vif,
102c56e00a3SJohannes Berg 				 struct ieee80211_sta *sta,
103c56e00a3SJohannes Berg 				 struct ieee80211_key_conf *key);
104c56e00a3SJohannes Berg 
iwl_mvm_reset_phy_ctxts(struct iwl_mvm * mvm)105e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
106e705c121SKalle Valo {
107e705c121SKalle Valo 	int i;
108e705c121SKalle Valo 
109e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
110e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
111e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
112e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
113e705c121SKalle Valo 	}
114e705c121SKalle Valo }
115e705c121SKalle Valo 
iwl_mvm_get_regdomain(struct wiphy * wiphy,const char * alpha2,enum iwl_mcc_source src_id,bool * changed)116e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
117e705c121SKalle Valo 						  const char *alpha2,
118e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
119e705c121SKalle Valo 						  bool *changed)
120e705c121SKalle Valo {
121e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
122e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
123e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
124e9b63341SAbhishek Naik 	struct iwl_mcc_update_resp_v8 *resp;
125e27c506aSGil Adam 	u8 resp_ver;
126e705c121SKalle Valo 
127e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
128e705c121SKalle Valo 
129e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
130e705c121SKalle Valo 
131e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
132e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
133e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
134e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
1356d19a5ebSEmmanuel Grumbach 		resp = NULL;
136e705c121SKalle Valo 		goto out;
137e705c121SKalle Valo 	}
138e705c121SKalle Valo 
13982715ac7SEmmanuel Grumbach 	if (changed) {
14082715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
14182715ac7SEmmanuel Grumbach 
14282715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
14382715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
14482715ac7SEmmanuel Grumbach 	}
145e27c506aSGil Adam 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
146e27c506aSGil Adam 					   MCC_UPDATE_CMD, 0);
147e27c506aSGil Adam 	IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
148e705c121SKalle Valo 
149e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
150e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
151e705c121SKalle Valo 				      resp->channels,
15277e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
1532763bba6SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info),
154f9802d53SJohannes Berg 				      le32_to_cpu(resp->cap), resp_ver);
155e705c121SKalle Valo 	/* Store the return source id */
156e705c121SKalle Valo 	src_id = resp->source_id;
157e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
158e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
159e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
160e705c121SKalle Valo 		goto out;
161e705c121SKalle Valo 	}
162e705c121SKalle Valo 
163e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
164e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
165e705c121SKalle Valo 	mvm->lar_regdom_set = true;
166e705c121SKalle Valo 	mvm->mcc_src = src_id;
167e705c121SKalle Valo 
168b312e357SSomashekhar(Som) 	if (!iwl_puncturing_is_allowed_in_bios(mvm->bios_enable_puncturing,
169b312e357SSomashekhar(Som) 					       le16_to_cpu(resp->mcc)))
170ea5cca78SJohannes Berg 		ieee80211_hw_set(mvm->hw, DISALLOW_PUNCTURING);
171ea5cca78SJohannes Berg 	else
172ea5cca78SJohannes Berg 		__clear_bit(IEEE80211_HW_DISALLOW_PUNCTURING, mvm->hw->flags);
173ea5cca78SJohannes Berg 
1746d19a5ebSEmmanuel Grumbach 	iwl_mei_set_country_code(__le16_to_cpu(resp->mcc));
1756d19a5ebSEmmanuel Grumbach 
176e705c121SKalle Valo out:
1776d19a5ebSEmmanuel Grumbach 	kfree(resp);
178e705c121SKalle Valo 	return regd;
179e705c121SKalle Valo }
180e705c121SKalle Valo 
iwl_mvm_update_changed_regdom(struct iwl_mvm * mvm)181e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
182e705c121SKalle Valo {
183e705c121SKalle Valo 	bool changed;
184e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
185e705c121SKalle Valo 
186e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
187e705c121SKalle Valo 		return;
188e705c121SKalle Valo 
189e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
190e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
191e705c121SKalle Valo 		/* only update the regulatory core if changed */
192e705c121SKalle Valo 		if (changed)
193e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
194e705c121SKalle Valo 
195e705c121SKalle Valo 		kfree(regd);
196e705c121SKalle Valo 	}
197e705c121SKalle Valo }
198e705c121SKalle Valo 
iwl_mvm_get_current_regdomain(struct iwl_mvm * mvm,bool * changed)199e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
200e705c121SKalle Valo 							  bool *changed)
201e705c121SKalle Valo {
202e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
203e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
204e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
205e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
206e705c121SKalle Valo }
207e705c121SKalle Valo 
iwl_mvm_init_fw_regd(struct iwl_mvm * mvm,bool force_regd_sync)208ea02a208SGregory Greenman int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm, bool force_regd_sync)
209e705c121SKalle Valo {
210e705c121SKalle Valo 	enum iwl_mcc_source used_src;
211e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
212e705c121SKalle Valo 	int ret;
213e705c121SKalle Valo 	bool changed;
214e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
215a05829a7SJohannes Berg 			wiphy_dereference(mvm->hw->wiphy, mvm->hw->wiphy->regd);
216e705c121SKalle Valo 
217e705c121SKalle Valo 	if (!r)
218e705c121SKalle Valo 		return -ENOENT;
219e705c121SKalle Valo 
220e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
221e705c121SKalle Valo 	used_src = mvm->mcc_src;
222e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
223e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
224e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
225e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
226e705c121SKalle Valo 			kfree(regd);
227e705c121SKalle Valo 	}
228e705c121SKalle Valo 
229e705c121SKalle Valo 	/* Now set our last stored MCC and source */
230e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
231e705c121SKalle Valo 				     &changed);
232e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
233e705c121SKalle Valo 		return -EIO;
234e705c121SKalle Valo 
235ea02a208SGregory Greenman 	/* update cfg80211 if the regdomain was changed or the caller explicitly
236ea02a208SGregory Greenman 	 * asked to update regdomain
237ea02a208SGregory Greenman 	 */
238ea02a208SGregory Greenman 	if (changed || force_regd_sync)
239a05829a7SJohannes Berg 		ret = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
240e705c121SKalle Valo 	else
241e705c121SKalle Valo 		ret = 0;
242e705c121SKalle Valo 
243e705c121SKalle Valo 	kfree(regd);
244e705c121SKalle Valo 	return ret;
245e705c121SKalle Valo }
246e705c121SKalle Valo 
2478e33f046SKrishnanand Prabhu /* Each capability added here should also be add to tm_if_types_ext_capa_sta */
2487f2ea521SYueHaibing static const u8 he_if_types_ext_capa_sta[] = {
2497360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
250918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
251784d4a42SJohannes Berg 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
252784d4a42SJohannes Berg 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
253784d4a42SJohannes Berg 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
2547360f99eSEmmanuel Grumbach };
2557360f99eSEmmanuel Grumbach 
2568e33f046SKrishnanand Prabhu static const u8 tm_if_types_ext_capa_sta[] = {
2578e33f046SKrishnanand Prabhu 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
2588e33f046SKrishnanand Prabhu 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT |
2598e33f046SKrishnanand Prabhu 	       WLAN_EXT_CAPA3_TIMING_MEASUREMENT_SUPPORT,
260784d4a42SJohannes Berg 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
261784d4a42SJohannes Berg 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
262784d4a42SJohannes Berg 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
2638e33f046SKrishnanand Prabhu 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
2648e33f046SKrishnanand Prabhu };
2658e33f046SKrishnanand Prabhu 
2668e33f046SKrishnanand Prabhu /* Additional interface types for which extended capabilities are
2678e33f046SKrishnanand Prabhu  * specified separately
2688e33f046SKrishnanand Prabhu  */
2696107f300SEmmanuel Grumbach 
2706107f300SEmmanuel Grumbach #define IWL_MVM_EMLSR_CAPA	(IEEE80211_EML_CAP_EMLSR_SUPP | \
2716107f300SEmmanuel Grumbach 				 IEEE80211_EML_CAP_EMLSR_PADDING_DELAY_32US << \
2726107f300SEmmanuel Grumbach 					__bf_shf(IEEE80211_EML_CAP_EMLSR_PADDING_DELAY) | \
2736107f300SEmmanuel Grumbach 				 IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_64US << \
2746107f300SEmmanuel Grumbach 					__bf_shf(IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY))
27580018499SAyala Beker #define IWL_MVM_MLD_CAPA_OPS FIELD_PREP_CONST( \
27680018499SAyala Beker 			IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP, \
27780018499SAyala Beker 			IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP_SAME)
2786107f300SEmmanuel Grumbach 
2798e33f046SKrishnanand Prabhu static const struct wiphy_iftype_ext_capab add_iftypes_ext_capa[] = {
2807360f99eSEmmanuel Grumbach 	{
2817360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
2827360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
2837360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
2847360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
2856107f300SEmmanuel Grumbach 		/* relevant only if EHT is supported */
2866107f300SEmmanuel Grumbach 		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
28780018499SAyala Beker 		.mld_capa_and_ops = IWL_MVM_MLD_CAPA_OPS,
2887360f99eSEmmanuel Grumbach 	},
2898e33f046SKrishnanand Prabhu 	{
2908e33f046SKrishnanand Prabhu 		.iftype = NL80211_IFTYPE_STATION,
2918e33f046SKrishnanand Prabhu 		.extended_capabilities = tm_if_types_ext_capa_sta,
2928e33f046SKrishnanand Prabhu 		.extended_capabilities_mask = tm_if_types_ext_capa_sta,
2938e33f046SKrishnanand Prabhu 		.extended_capabilities_len = sizeof(tm_if_types_ext_capa_sta),
2948e33f046SKrishnanand Prabhu 		/* relevant only if EHT is supported */
2956107f300SEmmanuel Grumbach 		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
29680018499SAyala Beker 		.mld_capa_and_ops = IWL_MVM_MLD_CAPA_OPS,
2978e33f046SKrishnanand Prabhu 	},
2987360f99eSEmmanuel Grumbach };
2997360f99eSEmmanuel Grumbach 
iwl_mvm_op_get_antenna(struct ieee80211_hw * hw,u32 * tx_ant,u32 * rx_ant)300cbce62a3SMiri Korenblit int iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
301e8503aecSBen Greear {
302e8503aecSBen Greear 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
303e8503aecSBen Greear 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
304e8503aecSBen Greear 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
305e8503aecSBen Greear 	return 0;
306e8503aecSBen Greear }
307e8503aecSBen Greear 
iwl_mvm_op_set_antenna(struct ieee80211_hw * hw,u32 tx_ant,u32 rx_ant)3084ea1ed1dSEmmanuel Grumbach int iwl_mvm_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3094ea1ed1dSEmmanuel Grumbach {
3104ea1ed1dSEmmanuel Grumbach 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3114ea1ed1dSEmmanuel Grumbach 
3124ea1ed1dSEmmanuel Grumbach 	/* This has been tested on those devices only */
3134ea1ed1dSEmmanuel Grumbach 	if (mvm->trans->trans_cfg->device_family != IWL_DEVICE_FAMILY_9000 &&
3144ea1ed1dSEmmanuel Grumbach 	    mvm->trans->trans_cfg->device_family != IWL_DEVICE_FAMILY_22000)
31596850912SAndrei Otcheretianski 		return -EOPNOTSUPP;
3164ea1ed1dSEmmanuel Grumbach 
3174ea1ed1dSEmmanuel Grumbach 	if (!mvm->nvm_data)
3184ea1ed1dSEmmanuel Grumbach 		return -EBUSY;
3194ea1ed1dSEmmanuel Grumbach 
3204ea1ed1dSEmmanuel Grumbach 	/* mac80211 ensures the device is not started,
3214ea1ed1dSEmmanuel Grumbach 	 * so the firmware cannot be running
3224ea1ed1dSEmmanuel Grumbach 	 */
3234ea1ed1dSEmmanuel Grumbach 
3244ea1ed1dSEmmanuel Grumbach 	mvm->set_tx_ant = tx_ant;
3254ea1ed1dSEmmanuel Grumbach 	mvm->set_rx_ant = rx_ant;
3264ea1ed1dSEmmanuel Grumbach 
3274ea1ed1dSEmmanuel Grumbach 	iwl_reinit_cab(mvm->trans, mvm->nvm_data, tx_ant, rx_ant, mvm->fw);
3284ea1ed1dSEmmanuel Grumbach 
3294ea1ed1dSEmmanuel Grumbach 	return 0;
3304ea1ed1dSEmmanuel Grumbach }
3314ea1ed1dSEmmanuel Grumbach 
iwl_mvm_mac_setup_register(struct iwl_mvm * mvm)332e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
333e705c121SKalle Valo {
334e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
335e705c121SKalle Valo 	int num_mac, ret, i;
336e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
337e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
338e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
339e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
340e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
341e705c121SKalle Valo 	};
3423f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
3433f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
3443f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3453f37c229SIdo Yariv #endif
346a5de7de7SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
347a5de7de7SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
348e705c121SKalle Valo 
349e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
350e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
351e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
352e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
353e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
354e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
355e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
356e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
357e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
358e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
359e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
360e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
361909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
36230433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
363520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
364cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
365cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
366ef2b47b8SJohannes Berg 
3670120e6b3SGregory Greenman 	/* Set this early since we need to have it for the check below */
368b3366330SJohannes Berg 	if (mvm->mld_api_is_used && mvm->nvm_data->sku_cap_11be_enable &&
369b3366330SJohannes Berg 	    !iwlwifi_mod_params.disable_11ax &&
370bbd6d0f8SJohannes Berg 	    !iwlwifi_mod_params.disable_11be) {
3711b431ba4SMiri Korenblit 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
372bbd6d0f8SJohannes Berg 		/* we handle this already earlier, but need it for MLO */
373bbd6d0f8SJohannes Berg 		ieee80211_hw_set(hw, HANDLES_QUIET_CSA);
374bbd6d0f8SJohannes Berg 	}
3750120e6b3SGregory Greenman 
376ef2b47b8SJohannes Berg 	/* With MLD FW API, it tracks timing by itself,
377ef2b47b8SJohannes Berg 	 * no need for any timing from the host
378ef2b47b8SJohannes Berg 	 */
379ef2b47b8SJohannes Berg 	if (!mvm->mld_api_is_used)
380ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
381ef2b47b8SJohannes Berg 
382cfb21b11SJohannes Berg 	/*
383cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
384cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
385cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
386cfb21b11SJohannes Berg 	 * assert EDC.
387cfb21b11SJohannes Berg 	 *
388cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
389cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
390cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
391cfb21b11SJohannes Berg 	 * devices.
392cfb21b11SJohannes Berg 	 */
3937d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
394438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
395438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
396ecaf71deSGregory Greenman 
3974243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
398ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
399ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
400ecaf71deSGregory Greenman 	}
401ecaf71deSGregory Greenman 
40229fa9a98SEmmanuel Grumbach 	/* We want to use the mac80211's reorder buffer for 9000 */
40329fa9a98SEmmanuel Grumbach 	if (iwl_mvm_has_new_rx_api(mvm) &&
40429fa9a98SEmmanuel Grumbach 	    mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_9000)
405b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
406960f864bSJohannes Berg 
407960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
408960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
40965e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
410960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
411960f864bSJohannes Berg 		/*
412960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
413960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
414960f864bSJohannes Berg 		 * for station powersave
415960f864bSJohannes Berg 		 */
416960f864bSJohannes Berg 		return -EINVAL;
417960f864bSJohannes Berg 	}
418e705c121SKalle Valo 
41980938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
42080938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
42180938abcSJohannes Berg 
4222d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
4232d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
4242d7cf549SJohannes Berg 
425366fc672SJohannes Berg 	hw->queues = IEEE80211_NUM_ACS;
426e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
427e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
428e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
429e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
430e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
431371a17edSJohannes Berg 
432371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
433371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
434371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
435371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
436371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
437371a17edSJohannes Berg 
4384243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
4399f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
4409f66a397SGregory Greenman 
441e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
442e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
443438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
444e705c121SKalle Valo 
4458e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
446e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
447e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
448e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
449e705c121SKalle Valo 
4502a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4512a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4522a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
4532a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4542a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4552a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
4562a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4572a53d166SAyala Beker 	}
4582a53d166SAyala Beker 
459f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
460f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
461f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
462f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
463f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
464f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
465f4bfdc5eSEmmanuel Grumbach 
466e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
467f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
468e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
4698e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4708e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4718e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
4728e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4738e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4748e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
4758e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4768e160ab8SAyala Beker 	}
477e705c121SKalle Valo 
478ef2e7a51SIlan Peer 	wiphy_ext_feature_set(hw->wiphy,
479ef2e7a51SIlan Peer 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
480d16b96b5SJohannes Berg 	wiphy_ext_feature_set(hw->wiphy,
481d16b96b5SJohannes Berg 			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
482ef2e7a51SIlan Peer 
483b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
484fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
485b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
486b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
487fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
488fc36ffdaSJohannes Berg 	}
489b73f9a4aSJohannes Berg 
490a5de7de7SJohannes Berg 	if (sec_key_ver &&
491a5de7de7SJohannes Berg 	    fw_has_capa(&mvm->fw->ucode_capa,
492a5de7de7SJohannes Berg 			IWL_UCODE_TLV_CAPA_BIGTK_TX_SUPPORT))
493a5de7de7SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
494a5de7de7SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION);
495a5de7de7SJohannes Berg 	else if (fw_has_capa(&mvm->fw->ucode_capa,
496b1fdc250SJohannes Berg 			     IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
497b1fdc250SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
498b1fdc250SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
499b1fdc250SJohannes Berg 
500cf85123aSAvraham Stern 	if (fw_has_capa(&mvm->fw->ucode_capa,
501cf85123aSAvraham Stern 			IWL_UCODE_TLV_CAPA_TIME_SYNC_BOTH_FTM_TM))
502cf85123aSAvraham Stern 		hw->wiphy->hw_timestamp_max_peers = 1;
503cf85123aSAvraham Stern 
504c289f5cdSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
505c289f5cdSJohannes Berg 			IWL_UCODE_TLV_CAPA_SPP_AMSDU_SUPPORT))
506c289f5cdSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
507c289f5cdSJohannes Berg 				      NL80211_EXT_FEATURE_SPP_AMSDU_SUPPORT);
508c289f5cdSJohannes Berg 
509e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
510e705c121SKalle Valo 	hw->wiphy->features |=
511e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
512e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
513e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
514e705c121SKalle Valo 
515e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
516e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
517e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
518cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
519e705c121SKalle Valo 
520e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
521e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
522e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
523e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
524e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
525e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
526e705c121SKalle Valo 
527e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
528e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
52933e3fd99SAlexander Wetzel 
53033e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
53133e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
53233e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
53333e3fd99SAlexander Wetzel 	 */
53433e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
53533e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
53633e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
537e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
538e47df5bdSJohannes Berg 
539e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
540e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
541e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
542e705c121SKalle Valo 	else
543e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
544e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
545e705c121SKalle Valo 
546a1910a7fSAndrei Otcheretianski 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
547a1910a7fSAndrei Otcheretianski 		wiphy_ext_feature_set(hw->wiphy,
548a1910a7fSAndrei Otcheretianski 				      NL80211_EXT_FEATURE_DFS_CONCURRENT);
549a1910a7fSAndrei Otcheretianski 
550e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
551e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
552eae94cf8SLuca Coelho 	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
553e705c121SKalle Valo 
554e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
555e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
556e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
557e705c121SKalle Valo 
558e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
5594f1847cfSJohannes Berg 	hw->max_listen_interval = IWL_MVM_CONN_LISTEN_INTERVAL;
560e705c121SKalle Valo 
561e705c121SKalle Valo 	/* Extract MAC address */
562e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
563e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
564e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
565e705c121SKalle Valo 
566e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
567e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
568e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
569e705c121SKalle Valo 
570e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
571e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
572e705c121SKalle Valo 		       ETH_ALEN);
573e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
574e705c121SKalle Valo 		hw->wiphy->n_addresses++;
575e705c121SKalle Valo 	}
576e705c121SKalle Valo 
577e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
578e705c121SKalle Valo 
579e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
580e705c121SKalle Valo 
581e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
582e705c121SKalle Valo 
583e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
5848a4475a1SDaniel Gabay 	BUILD_BUG_ON(IWL_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
5858a4475a1SDaniel Gabay 		     IWL_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
586e705c121SKalle Valo 
587e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
5888a4475a1SDaniel Gabay 		mvm->max_scans = IWL_MAX_UMAC_SCANS;
589e705c121SKalle Valo 	else
5908a4475a1SDaniel Gabay 		mvm->max_scans = IWL_MAX_LMAC_SCANS;
591e705c121SKalle Valo 
59257fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
59357fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
59457fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
59557fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
59657fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
59757fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
598e705c121SKalle Valo 
599e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
600e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
601e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
602e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
60357fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
604e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
605e705c121SKalle Valo 	}
606eae94cf8SLuca Coelho 	if (fw_has_capa(&mvm->fw->ucode_capa,
607eae94cf8SLuca Coelho 			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
608eae94cf8SLuca Coelho 	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
609eae94cf8SLuca Coelho 		hw->wiphy->bands[NL80211_BAND_6GHZ] =
610eae94cf8SLuca Coelho 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
611e705c121SKalle Valo 
612e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
613e705c121SKalle Valo 
614e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
615e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
616e705c121SKalle Valo 	else
617e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
618e705c121SKalle Valo 
619ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
620e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
6215d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
622e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
623e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
624e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
625e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
626e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
627e705c121SKalle Valo 
628e705c121SKalle Valo 	/*
629e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
630e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
631e705c121SKalle Valo 	 */
632e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
633e705c121SKalle Valo 
634e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
635e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
636e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
637a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
638e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
639e705c121SKalle Valo 
640b2a7c91bSDaniel Gabay 	/* when firmware supports RLC/SMPS offload, do not set these
641b2a7c91bSDaniel Gabay 	 * driver features, since it's no longer supported by driver.
642b2a7c91bSDaniel Gabay 	 */
643b2a7c91bSDaniel Gabay 	if (!iwl_mvm_has_rlc_offload(mvm))
644b2a7c91bSDaniel Gabay 		hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS |
645b2a7c91bSDaniel Gabay 				       NL80211_FEATURE_DYNAMIC_SMPS;
646b2a7c91bSDaniel Gabay 
647e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
648e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
649e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
650e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
651e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
652e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
653e705c121SKalle Valo 
654e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
655e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
656e705c121SKalle Valo 		hw->wiphy->features |=
657e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
658e705c121SKalle Valo 
659e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
660e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
661e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
662e705c121SKalle Valo 
663971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
6644ec17ce7SDaniel Gabay 				  IWL_FW_CMD_VER_UNKNOWN) >= 3)
6652a42aea7SNathan Errera 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
6662a42aea7SNathan Errera 
667aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
668aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
669aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
670aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
671aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
672aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
673aacf8f18SAvrahams Stern 	}
674aacf8f18SAvrahams Stern 
6758f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
676971cbe50SJohannes Berg 		u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
677773a042fSAvraham Stern 
6788f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6798f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
6808f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6818f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
6828f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6838f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
684773a042fSAvraham Stern 
685773a042fSAvraham Stern 		/* Old firmware also supports probe deferral and suppression */
686773a042fSAvraham Stern 		if (scan_ver < 15)
687773a042fSAvraham Stern 			wiphy_ext_feature_set(hw->wiphy,
688773a042fSAvraham Stern 					      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
6898f691af9SZamir, Roee 	}
6908f691af9SZamir, Roee 
6918e33f046SKrishnanand Prabhu 	hw->wiphy->iftype_ext_capab = NULL;
6928e33f046SKrishnanand Prabhu 	hw->wiphy->num_iftype_ext_capab = 0;
6938e33f046SKrishnanand Prabhu 
6947360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
6957360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
6968e33f046SKrishnanand Prabhu 		hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa;
6977360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
6988e33f046SKrishnanand Prabhu 			ARRAY_SIZE(add_iftypes_ext_capa) - 1;
699918cbf39SSara Sharon 
700918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
701918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
7027360f99eSEmmanuel Grumbach 	}
7037360f99eSEmmanuel Grumbach 
7048e33f046SKrishnanand Prabhu 	if (iwl_fw_lookup_cmd_ver(mvm->fw,
7058e33f046SKrishnanand Prabhu 				  WIDE_ID(DATA_PATH_GROUP,
7068e33f046SKrishnanand Prabhu 					  WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
7078e33f046SKrishnanand Prabhu 				  IWL_FW_CMD_VER_UNKNOWN) >= 1) {
7088e33f046SKrishnanand Prabhu 		IWL_DEBUG_INFO(mvm->trans, "Timing measurement supported\n");
7098e33f046SKrishnanand Prabhu 
7108e33f046SKrishnanand Prabhu 		if (!hw->wiphy->iftype_ext_capab) {
7118e33f046SKrishnanand Prabhu 			hw->wiphy->num_iftype_ext_capab = 1;
7128e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa +
7138e33f046SKrishnanand Prabhu 				ARRAY_SIZE(add_iftypes_ext_capa) - 1;
7148e33f046SKrishnanand Prabhu 		} else {
7158e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa + 1;
7168e33f046SKrishnanand Prabhu 		}
7178e33f046SKrishnanand Prabhu 	}
7188e33f046SKrishnanand Prabhu 
7198b720901SAvraham Stern 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(LOCATION_GROUP,
7208b720901SAvraham Stern 						   TOF_RANGE_REQ_CMD),
7218b720901SAvraham Stern 				  IWL_FW_CMD_VER_UNKNOWN) >= 11) {
7228b720901SAvraham Stern 		wiphy_ext_feature_set(hw->wiphy,
7238b720901SAvraham Stern 				      NL80211_EXT_FEATURE_PROT_RANGE_NEGO_AND_MEASURE);
7244d951e26SIlan Peer 
7254d951e26SIlan Peer 		if (fw_has_capa(&mvm->fw->ucode_capa,
7264d951e26SIlan Peer 				IWL_UCODE_TLV_CAPA_SECURE_LTF_SUPPORT))
7274d951e26SIlan Peer 			wiphy_ext_feature_set(hw->wiphy,
7284d951e26SIlan Peer 					      NL80211_EXT_FEATURE_SECURE_LTF);
7298b720901SAvraham Stern 	}
7308b720901SAvraham Stern 
731e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
732e705c121SKalle Valo 
733eb561c29SJohannes Berg 	ieee80211_hw_set(hw, DISALLOW_PUNCTURING_5GHZ);
734eb561c29SJohannes Berg 
735e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
7363f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
737e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
738e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
739e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
740e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
741e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
742e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
743e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
744e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
745e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
746e705c121SKalle Valo 
747e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
748e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
749e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
7505d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
7515d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
752e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
753e705c121SKalle Valo 	}
754e705c121SKalle Valo #endif
755e705c121SKalle Valo 
756e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
757e705c121SKalle Valo 	if (ret)
758e705c121SKalle Valo 		return ret;
759e705c121SKalle Valo 
760e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
761e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
762e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
763e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
764e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
765e705c121SKalle Valo 	}
766e705c121SKalle Valo 
767e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
768e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
769e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
770e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
771e705c121SKalle Valo 	}
772e705c121SKalle Valo 
773e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
77459fa61f3SEmmanuel Grumbach 	if (!iwl_mvm_is_csum_supported(mvm))
7756772aab7SJohannes Berg 		hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
77641837ca9SEmmanuel Grumbach 
77791b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
77891b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
77991b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
78091b08c2dSAviya Erenfeld 
7819c11d8a9SShaul Triebitz 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
7829c11d8a9SShaul Triebitz 		wiphy_ext_feature_set(hw->wiphy,
7839c11d8a9SShaul Triebitz 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
7849c11d8a9SShaul Triebitz 
785bfcfdb59SEmmanuel Grumbach 	iwl_mvm_vendor_cmds_register(mvm);
786bfcfdb59SEmmanuel Grumbach 
787e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
788e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
789e8503aecSBen Greear 
790de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
791de645e89SJohannes Berg 	if (ret) {
792de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
793e705c121SKalle Valo 	}
794e705c121SKalle Valo 
795e705c121SKalle Valo 	return ret;
796e705c121SKalle Valo }
797e705c121SKalle Valo 
iwl_mvm_tx_skb(struct iwl_mvm * mvm,struct sk_buff * skb,struct ieee80211_sta * sta)798df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
799df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
800df2378abSJohannes Berg {
801df2378abSJohannes Berg 	if (likely(sta)) {
802df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
803df2378abSJohannes Berg 			return;
804df2378abSJohannes Berg 	} else {
805df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
806df2378abSJohannes Berg 			return;
807df2378abSJohannes Berg 	}
808df2378abSJohannes Berg 
809df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
810df2378abSJohannes Berg }
811df2378abSJohannes Berg 
iwl_mvm_mac_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)812cbce62a3SMiri Korenblit void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
813cbce62a3SMiri Korenblit 		    struct ieee80211_tx_control *control, struct sk_buff *skb)
814e705c121SKalle Valo {
815e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
816e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
817e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
818e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
819cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
820cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
82190723da6SShaul Triebitz 	u32 link_id = u32_get_bits(info->control.flags,
82290723da6SShaul Triebitz 				   IEEE80211_TX_CTRL_MLO_LINK);
82390723da6SShaul Triebitz 	struct ieee80211_sta *tmp_sta = sta;
824e705c121SKalle Valo 
825e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
826e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
827e705c121SKalle Valo 		goto drop;
828e705c121SKalle Valo 	}
829e705c121SKalle Valo 
830cfbc6c4cSSara Sharon 	if (offchannel &&
83154d96912SShaul Triebitz 	    !test_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status) &&
832e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
833e705c121SKalle Valo 		goto drop;
834e705c121SKalle Valo 
83510516783SJohannes Berg 	/*
83610516783SJohannes Berg 	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
83710516783SJohannes Berg 	 * so we treat the others as broadcast
83810516783SJohannes Berg 	 */
83910516783SJohannes Berg 	if (ieee80211_is_mgmt(hdr->frame_control))
840e705c121SKalle Valo 		sta = NULL;
841e705c121SKalle Valo 
8421c7e1068SJohannes Berg 	/* this shouldn't even happen: just drop */
843cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
8441c7e1068SJohannes Berg 	    !offchannel)
845dc1aca22SAndrei Otcheretianski 		goto drop;
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 
iwl_mvm_mac_itxq_xmit(struct ieee80211_hw * hw,struct ieee80211_txq * txq)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) &&
906bbd6d0f8SJohannes Berg 			      !test_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA,
907bbd6d0f8SJohannes Berg 					&mvmtxq->state) &&
90800520b7aSEmmanuel Grumbach 			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
909cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
910cfbc6c4cSSara Sharon 
911f50d693bSSara Sharon 			if (!skb) {
912f50d693bSSara Sharon 				if (txq->sta)
913f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
914f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
915f50d693bSSara Sharon 						     txq->sta->addr,
916f50d693bSSara Sharon 						     txq->tid);
917cfbc6c4cSSara Sharon 				break;
918f50d693bSSara Sharon 			}
919cfbc6c4cSSara Sharon 
920cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
921cfbc6c4cSSara Sharon 		}
922fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
923cfbc6c4cSSara Sharon 	rcu_read_unlock();
924e705c121SKalle Valo }
925e705c121SKalle Valo 
iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)926cbce62a3SMiri Korenblit void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
927cfbc6c4cSSara Sharon 			       struct ieee80211_txq *txq)
928e705c121SKalle Valo {
929cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
930cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
931e705c121SKalle Valo 
932923bf981SJohannes Berg 	if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
933923bf981SJohannes Berg 	    !txq->sta) {
934cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
935cfbc6c4cSSara Sharon 		return;
936cfbc6c4cSSara Sharon 	}
937cfbc6c4cSSara Sharon 
938923bf981SJohannes Berg 	/* iwl_mvm_mac_itxq_xmit() will later be called by the worker
939923bf981SJohannes Berg 	 * to handle any packets we leave on the txq now
940923bf981SJohannes Berg 	 */
941cfbc6c4cSSara Sharon 
942923bf981SJohannes Berg 	spin_lock_bh(&mvm->add_stream_lock);
943923bf981SJohannes Berg 	/* The list is being deleted only after the queue is fully allocated. */
944923bf981SJohannes Berg 	if (list_empty(&mvmtxq->list) &&
945923bf981SJohannes Berg 	    /* recheck under lock */
946923bf981SJohannes Berg 	    !test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) {
947cfbc6c4cSSara Sharon 		list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
948cfbc6c4cSSara Sharon 		schedule_work(&mvm->add_stream_wk);
949e705c121SKalle Valo 	}
950923bf981SJohannes Berg 	spin_unlock_bh(&mvm->add_stream_lock);
951923bf981SJohannes Berg }
952e705c121SKalle Valo 
953e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
954e705c121SKalle Valo 	do {								\
955e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
956e705c121SKalle Valo 			break;						\
9577174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
958e705c121SKalle Valo 	} while (0)
959e705c121SKalle Valo 
960e705c121SKalle Valo static void
iwl_mvm_ampdu_check_trigger(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 tid,u16 rx_ba_ssn,enum ieee80211_ampdu_mlme_action action)961e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
962e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
963e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
964e705c121SKalle Valo {
965e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
966e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
967e705c121SKalle Valo 
9686c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
9696c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
9706c042d75SSara Sharon 	if (!trig)
971e705c121SKalle Valo 		return;
972e705c121SKalle Valo 
973e705c121SKalle Valo 	ba_trig = (void *)trig->data;
974e705c121SKalle Valo 
975e705c121SKalle Valo 	switch (action) {
976e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
977e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
978e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
979e705c121SKalle Valo 
980e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
981e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
982e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
983e705c121SKalle Valo 		break;
984e705c121SKalle Valo 		}
985e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
986e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
987e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
988e705c121SKalle Valo 				 sta->addr, tid);
989e705c121SKalle Valo 		break;
990e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
991e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
992e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
993e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
994e705c121SKalle Valo 		break;
995e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
996e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
997e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
998e705c121SKalle Valo 				 sta->addr, tid);
999e705c121SKalle Valo 		break;
1000e705c121SKalle Valo 	default:
1001e705c121SKalle Valo 		break;
1002e705c121SKalle Valo 	}
1003e705c121SKalle Valo }
1004e705c121SKalle Valo 
iwl_mvm_mac_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)1005cbce62a3SMiri Korenblit int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
1006e705c121SKalle Valo 			     struct ieee80211_vif *vif,
100750ea05efSSara Sharon 			     struct ieee80211_ampdu_params *params)
1008e705c121SKalle Valo {
1009e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1010e705c121SKalle Valo 	int ret;
101150ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
101250ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
101350ea05efSSara Sharon 	u16 tid = params->tid;
101450ea05efSSara Sharon 	u16 *ssn = &params->ssn;
1015514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
1016bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
101710b2b201SSara Sharon 	u16 timeout = params->timeout;
1018e705c121SKalle Valo 
1019e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
1020e705c121SKalle Valo 		     sta->addr, tid, action);
1021e705c121SKalle Valo 
1022e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
1023e705c121SKalle Valo 		return -EACCES;
1024e705c121SKalle Valo 
1025e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1026e705c121SKalle Valo 
1027e705c121SKalle Valo 	switch (action) {
1028e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
1029650cadb7SGregory Greenman 		if (iwl_mvm_vif_from_mac80211(vif)->deflink.ap_sta_id ==
1030c8ee33e1SGregory Greenman 		    iwl_mvm_sta_from_mac80211(sta)->deflink.sta_id) {
1031b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
1032b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
1033b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
1034b0ffe455SJohannes Berg 
1035b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
1036b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
1037b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
1038b0ffe455SJohannes Berg 		}
1039e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
1040e705c121SKalle Valo 			ret = -EINVAL;
1041e705c121SKalle Valo 			break;
1042e705c121SKalle Valo 		}
104310b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
104410b2b201SSara Sharon 					 timeout);
1045e705c121SKalle Valo 		break;
1046e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
104710b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
104810b2b201SSara Sharon 					 timeout);
1049e705c121SKalle Valo 		break;
1050e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
1051e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
1052e705c121SKalle Valo 			ret = -EINVAL;
1053e705c121SKalle Valo 			break;
1054e705c121SKalle Valo 		}
1055e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1056e705c121SKalle Valo 		break;
1057e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
1058e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1059e705c121SKalle Valo 		break;
1060e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1061e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1062e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1063e705c121SKalle Valo 		break;
1064e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1065bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1066bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
1067e705c121SKalle Valo 		break;
1068e705c121SKalle Valo 	default:
1069e705c121SKalle Valo 		WARN_ON_ONCE(1);
1070e705c121SKalle Valo 		ret = -EINVAL;
1071e705c121SKalle Valo 		break;
1072e705c121SKalle Valo 	}
1073e705c121SKalle Valo 
1074e705c121SKalle Valo 	if (!ret) {
1075e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
1076e705c121SKalle Valo 
1077e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1078e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1079e705c121SKalle Valo 
1080e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1081e705c121SKalle Valo 					    rx_ba_ssn, action);
1082e705c121SKalle Valo 	}
1083e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1084e705c121SKalle Valo 
1085e705c121SKalle Valo 	return ret;
1086e705c121SKalle Valo }
1087e705c121SKalle Valo 
iwl_mvm_cleanup_iterator(void * data,u8 * mac,struct ieee80211_vif * vif)1088e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1089e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1090e705c121SKalle Valo {
1091e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1092e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1093bf976c81SJohannes Berg 	struct iwl_probe_resp_data *probe_data;
109479faae3aSGregory Greenman 	unsigned int link_id;
1095e705c121SKalle Valo 
1096e705c121SKalle Valo 	mvmvif->uploaded = false;
1097e705c121SKalle Valo 
1098e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1099e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1100e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1101e705c121SKalle Valo 
110254d96912SShaul Triebitz 	mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
110354d96912SShaul Triebitz 
1104c1e458b9SIlan Peer 	mvmvif->bf_enabled = false;
1105c1e458b9SIlan Peer 	mvmvif->ba_enabled = false;
1106828c79d9SEmmanuel Grumbach 	mvmvif->ap_sta = NULL;
110779faae3aSGregory Greenman 
1108f2c2799eSBenjamin Berg 	mvmvif->esr_active = false;
1109f2c2799eSBenjamin Berg 	vif->driver_flags &= ~IEEE80211_VIF_EML_ACTIVE;
1110f2c2799eSBenjamin Berg 
111179faae3aSGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
111279faae3aSGregory Greenman 		mvmvif->link[link_id]->ap_sta_id = IWL_MVM_INVALID_STA;
111379faae3aSGregory Greenman 		mvmvif->link[link_id]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
111479faae3aSGregory Greenman 		mvmvif->link[link_id]->phy_ctxt = NULL;
1115bf976c81SJohannes Berg 		mvmvif->link[link_id]->active = 0;
1116d615ea32SJohannes Berg 		mvmvif->link[link_id]->igtk = NULL;
1117c1e458b9SIlan Peer 		memset(&mvmvif->link[link_id]->bf_data, 0,
1118c1e458b9SIlan Peer 		       sizeof(mvmvif->link[link_id]->bf_data));
111979faae3aSGregory Greenman 	}
1120bf976c81SJohannes Berg 
1121bf976c81SJohannes Berg 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1122bf976c81SJohannes Berg 					       lockdep_is_held(&mvm->mutex));
1123bf976c81SJohannes Berg 	if (probe_data)
1124bf976c81SJohannes Berg 		kfree_rcu(probe_data, rcu_head);
1125bf976c81SJohannes Berg 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1126e705c121SKalle Valo }
1127e705c121SKalle Valo 
iwl_mvm_cleanup_sta_iterator(void * data,struct ieee80211_sta * sta)1128cc3ba78fSBenjamin Berg static void iwl_mvm_cleanup_sta_iterator(void *data, struct ieee80211_sta *sta)
1129cc3ba78fSBenjamin Berg {
1130cc3ba78fSBenjamin Berg 	struct iwl_mvm *mvm = data;
1131cc3ba78fSBenjamin Berg 	struct iwl_mvm_sta *mvm_sta;
1132cc3ba78fSBenjamin Berg 	struct ieee80211_vif *vif;
1133cc3ba78fSBenjamin Berg 	int link_id;
1134cc3ba78fSBenjamin Berg 
1135cc3ba78fSBenjamin Berg 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1136cc3ba78fSBenjamin Berg 	vif = mvm_sta->vif;
1137cc3ba78fSBenjamin Berg 
1138cc3ba78fSBenjamin Berg 	if (!sta->valid_links)
1139cc3ba78fSBenjamin Berg 		return;
1140cc3ba78fSBenjamin Berg 
1141cc3ba78fSBenjamin Berg 	for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
1142cc3ba78fSBenjamin Berg 		struct iwl_mvm_link_sta *mvm_link_sta;
1143cc3ba78fSBenjamin Berg 
1144cc3ba78fSBenjamin Berg 		mvm_link_sta =
1145cc3ba78fSBenjamin Berg 			rcu_dereference_check(mvm_sta->link[link_id],
1146cc3ba78fSBenjamin Berg 					      lockdep_is_held(&mvm->mutex));
1147cc3ba78fSBenjamin Berg 		if (mvm_link_sta && !(vif->active_links & BIT(link_id))) {
1148cc3ba78fSBenjamin Berg 			/*
1149cc3ba78fSBenjamin Berg 			 * We have a link STA but the link is inactive in
1150cc3ba78fSBenjamin Berg 			 * mac80211. This will happen if we failed to
1151cc3ba78fSBenjamin Berg 			 * deactivate the link but mac80211 roll back the
1152cc3ba78fSBenjamin Berg 			 * deactivation of the link.
1153cc3ba78fSBenjamin Berg 			 * Delete the stale data to avoid issues later on.
1154cc3ba78fSBenjamin Berg 			 */
1155cc3ba78fSBenjamin Berg 			iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_link_sta,
1156cc3ba78fSBenjamin Berg 						  link_id, false);
1157cc3ba78fSBenjamin Berg 		}
1158cc3ba78fSBenjamin Berg 	}
1159cc3ba78fSBenjamin Berg }
1160cc3ba78fSBenjamin Berg 
iwl_mvm_restart_cleanup(struct iwl_mvm * mvm)1161e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1162e705c121SKalle Valo {
1163fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1164e705c121SKalle Valo 
11659bf13beeSJohannes Berg 	mvm->cur_aid = 0;
11669bf13beeSJohannes Berg 
1167e705c121SKalle Valo 	mvm->scan_status = 0;
1168e705c121SKalle Valo 	mvm->ps_disabled = false;
1169b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1170e705c121SKalle Valo 
1171e705c121SKalle Valo 	/* just in case one was running */
1172305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1173e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1174e705c121SKalle Valo 
1175fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1176fc36ffdaSJohannes Berg 
1177e705c121SKalle Valo 	/*
1178e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1179e705c121SKalle Valo 	 * gone down during the HW restart
1180e705c121SKalle Valo 	 */
1181e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1182e705c121SKalle Valo 
1183cc3ba78fSBenjamin Berg 	/* cleanup stations as links may be gone after restart */
1184cc3ba78fSBenjamin Berg 	ieee80211_iterate_stations_atomic(mvm->hw,
1185cc3ba78fSBenjamin Berg 					  iwl_mvm_cleanup_sta_iterator, mvm);
1186cc3ba78fSBenjamin Berg 
1187e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1188e705c121SKalle Valo 
1189e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
11909c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1191e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1192e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1193e705c121SKalle Valo 
1194e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1195e705c121SKalle Valo 
1196e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
11977174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1198baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1199fb40cd9dSMiri Korenblit #ifdef CONFIG_IWLWIFI_DEBUGFS
1200fb40cd9dSMiri Korenblit 	mvm->beacon_inject_active = false;
1201fb40cd9dSMiri Korenblit #endif
1202e705c121SKalle Valo 
1203e705c121SKalle Valo 	/* keep statistics ticking */
1204e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1205e705c121SKalle Valo }
1206e705c121SKalle Valo 
__iwl_mvm_mac_start(struct iwl_mvm * mvm)1207e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1208e705c121SKalle Valo {
1209e8bb19c1SEmmanuel Grumbach 	bool fast_resume = false;
1210e705c121SKalle Valo 	int ret;
1211e705c121SKalle Valo 
1212e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1213e705c121SKalle Valo 
12146d19a5ebSEmmanuel Grumbach 	ret = iwl_mvm_mei_get_ownership(mvm);
12156d19a5ebSEmmanuel Grumbach 	if (ret)
12166d19a5ebSEmmanuel Grumbach 		return ret;
12176d19a5ebSEmmanuel Grumbach 
12186d19a5ebSEmmanuel Grumbach 	if (mvm->mei_nvm_data) {
12196d19a5ebSEmmanuel Grumbach 		/* We got the NIC, we can now free the MEI NVM data */
12206d19a5ebSEmmanuel Grumbach 		kfree(mvm->mei_nvm_data);
12216d19a5ebSEmmanuel Grumbach 		mvm->mei_nvm_data = NULL;
12226d19a5ebSEmmanuel Grumbach 
12236d19a5ebSEmmanuel Grumbach 		/*
12246d19a5ebSEmmanuel Grumbach 		 * We can't free the nvm_data we allocated based on the SAP
12256d19a5ebSEmmanuel Grumbach 		 * data because we registered to cfg80211 with the channels
12266d19a5ebSEmmanuel Grumbach 		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
12276d19a5ebSEmmanuel Grumbach 		 * just in order to be able free it later.
12286d19a5ebSEmmanuel Grumbach 		 * NULLify nvm_data so that we will read the NVM from the
12296d19a5ebSEmmanuel Grumbach 		 * firmware this time.
12306d19a5ebSEmmanuel Grumbach 		 */
12316d19a5ebSEmmanuel Grumbach 		mvm->temp_nvm_data = mvm->nvm_data;
12326d19a5ebSEmmanuel Grumbach 		mvm->nvm_data = NULL;
12336d19a5ebSEmmanuel Grumbach 	}
12346d19a5ebSEmmanuel Grumbach 
1235*cac9544cSEmmanuel Grumbach #ifdef CONFIG_PM_SLEEP
1236e8bb19c1SEmmanuel Grumbach 	/* fast_resume will be cleared by iwl_mvm_fast_resume */
1237e8bb19c1SEmmanuel Grumbach 	fast_resume = mvm->fast_resume;
1238e8bb19c1SEmmanuel Grumbach 
1239e8bb19c1SEmmanuel Grumbach 	if (fast_resume) {
1240e8bb19c1SEmmanuel Grumbach 		ret = iwl_mvm_fast_resume(mvm);
1241e8bb19c1SEmmanuel Grumbach 		if (ret) {
1242e8bb19c1SEmmanuel Grumbach 			iwl_mvm_stop_device(mvm);
1243e8bb19c1SEmmanuel Grumbach 			/* iwl_mvm_up() will be called further down */
1244e8bb19c1SEmmanuel Grumbach 		} else {
1245e8bb19c1SEmmanuel Grumbach 			/*
1246e8bb19c1SEmmanuel Grumbach 			 * We clear IWL_MVM_STATUS_FIRMWARE_RUNNING upon
1247e8bb19c1SEmmanuel Grumbach 			 * mac_down() so that debugfs will stop honoring
1248e8bb19c1SEmmanuel Grumbach 			 * requests after we flush all the workers.
1249e8bb19c1SEmmanuel Grumbach 			 * Set the IWL_MVM_STATUS_FIRMWARE_RUNNING bit again
1250e8bb19c1SEmmanuel Grumbach 			 * now that we are back. This is a bit abusing the
1251e8bb19c1SEmmanuel Grumbach 			 * flag since the firmware wasn't really ever stopped,
1252e8bb19c1SEmmanuel Grumbach 			 * but this still serves the purpose.
1253e8bb19c1SEmmanuel Grumbach 			 */
1254e8bb19c1SEmmanuel Grumbach 			set_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1255e8bb19c1SEmmanuel Grumbach 		}
1256e8bb19c1SEmmanuel Grumbach 	}
1257*cac9544cSEmmanuel Grumbach #endif /* CONFIG_PM_SLEEP */
1258e8bb19c1SEmmanuel Grumbach 
1259bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1260bf8b286fSJohannes Berg 		/*
1261bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1262bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1263bf8b286fSJohannes Berg 		 */
1264bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1265bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1266e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1267e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1268a42b2af3SLuca Coelho 	}
1269e8bb19c1SEmmanuel Grumbach 
1270e8bb19c1SEmmanuel Grumbach 	/* we also want to load the firmware if fast_resume failed */
1271e8bb19c1SEmmanuel Grumbach 	if (!fast_resume || ret)
1272e705c121SKalle Valo 		ret = iwl_mvm_up(mvm);
1273e705c121SKalle Valo 
1274b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1275b108d8c7SShahar S Matityahu 			       NULL);
1276b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1277b108d8c7SShahar S Matityahu 			       NULL);
1278da2eb669SSara Sharon 
1279e8fe3b41SIlan Peer 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1280e8fe3b41SIlan Peer 
1281e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1282e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1283e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1284e705c121SKalle Valo 		 * would do.
1285e705c121SKalle Valo 		 */
1286e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1287e705c121SKalle Valo 	}
1288e705c121SKalle Valo 
1289e705c121SKalle Valo 	return ret;
1290e705c121SKalle Valo }
1291e705c121SKalle Valo 
iwl_mvm_mac_start(struct ieee80211_hw * hw)1292cbce62a3SMiri Korenblit int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1293e705c121SKalle Valo {
1294e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1295e705c121SKalle Valo 	int ret;
1296e705c121SKalle Valo 
1297e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
12985283dd67SMordechay Goodstein 
12995283dd67SMordechay Goodstein 	/* we are starting the mac not in error flow, and restart is enabled */
13005283dd67SMordechay Goodstein 	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
13015283dd67SMordechay Goodstein 	    iwlwifi_mod_params.fw_restart) {
13025283dd67SMordechay Goodstein 		/*
13035283dd67SMordechay Goodstein 		 * This will prevent mac80211 recovery flows to trigger during
13045283dd67SMordechay Goodstein 		 * init failures
13055283dd67SMordechay Goodstein 		 */
13065283dd67SMordechay Goodstein 		set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
13075283dd67SMordechay Goodstein 	}
13085283dd67SMordechay Goodstein 
1309e705c121SKalle Valo 	ret = __iwl_mvm_mac_start(mvm);
13105283dd67SMordechay Goodstein 	clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
13115283dd67SMordechay Goodstein 
1312e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1313e705c121SKalle Valo 
13147ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
13157ce1f215SEmmanuel Grumbach 
1316e705c121SKalle Valo 	return ret;
1317e705c121SKalle Valo }
1318e705c121SKalle Valo 
iwl_mvm_restart_complete(struct iwl_mvm * mvm)1319e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1320e705c121SKalle Valo {
1321e705c121SKalle Valo 	int ret;
1322e705c121SKalle Valo 
13233f312651SJohannes Berg 	guard(mvm)(mvm);
1324e705c121SKalle Valo 
1325e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
13264d4183c4SEmmanuel Grumbach 
1327e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1328e705c121SKalle Valo 	if (ret)
1329e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1330e705c121SKalle Valo 			ret);
1331e705c121SKalle Valo 
1332f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1333f130bb75SMordechay Goodstein 
1334e705c121SKalle Valo 	/*
1335e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1336e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1337e705c121SKalle Valo 	 */
1338e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1339e705c121SKalle Valo }
1340e705c121SKalle Valo 
iwl_mvm_mac_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)1341cbce62a3SMiri Korenblit void iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1342e705c121SKalle Valo 				   enum ieee80211_reconfig_type reconfig_type)
1343e705c121SKalle Valo {
1344e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1345e705c121SKalle Valo 
1346e705c121SKalle Valo 	switch (reconfig_type) {
1347e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1348e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1349e705c121SKalle Valo 		break;
1350e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1351e705c121SKalle Valo 		break;
1352e705c121SKalle Valo 	}
1353e705c121SKalle Valo }
1354e705c121SKalle Valo 
__iwl_mvm_mac_stop(struct iwl_mvm * mvm,bool suspend)1355e8bb19c1SEmmanuel Grumbach void __iwl_mvm_mac_stop(struct iwl_mvm *mvm, bool suspend)
1356e705c121SKalle Valo {
1357e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1358e705c121SKalle Valo 
1359b68bd2e3SIlan Peer 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1360b68bd2e3SIlan Peer 
1361e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1362e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1363e705c121SKalle Valo 	 */
1364e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1365e705c121SKalle Valo 
1366e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1367e705c121SKalle Valo 
13681724fc78SEmmanuel Grumbach 	if (!iwl_mvm_has_new_station_api(mvm->fw))
1369f327236dSSharon 		iwl_mvm_rm_aux_sta(mvm);
1370f327236dSSharon 
1371e8bb19c1SEmmanuel Grumbach 	if (suspend &&
1372e8bb19c1SEmmanuel Grumbach 	    mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
1373e8bb19c1SEmmanuel Grumbach 		iwl_mvm_fast_suspend(mvm);
1374e8bb19c1SEmmanuel Grumbach 	else
1375fcb6b92aSChaya Rachel Ivgi 		iwl_mvm_stop_device(mvm);
1376e705c121SKalle Valo 
1377e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1378e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1379e705c121SKalle Valo 
1380e705c121SKalle Valo 	/*
1381155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1382155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1383155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1384e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1385e705c121SKalle Valo 	 * HW restart was requested.
1386e705c121SKalle Valo 	 */
1387155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1388155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1389155f7e04SEmmanuel Grumbach 			       &mvm->status))
1390e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1391e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1392e705c121SKalle Valo 
1393e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1394e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1395e705c121SKalle Valo 	 */
1396e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1397e705c121SKalle Valo 		int i;
1398e705c121SKalle Valo 
1399e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1400e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1401e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1402e705c121SKalle Valo 				      i))
1403e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1404e705c121SKalle Valo 		}
1405e705c121SKalle Valo 	}
1406e705c121SKalle Valo }
1407e705c121SKalle Valo 
iwl_mvm_mac_stop(struct ieee80211_hw * hw,bool suspend)14081decf05dSEmmanuel Grumbach void iwl_mvm_mac_stop(struct ieee80211_hw *hw, bool suspend)
1409e705c121SKalle Valo {
1410e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1411e705c121SKalle Valo 
141238b3998dSIlan Peer 	/* Stop internal MLO scan, if running */
141338b3998dSIlan Peer 	mutex_lock(&mvm->mutex);
141438b3998dSIlan Peer 	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_INT_MLO, false);
141538b3998dSIlan Peer 	mutex_unlock(&mvm->mutex);
141638b3998dSIlan Peer 
14172e194efaSIlan Peer 	wiphy_work_cancel(mvm->hw->wiphy, &mvm->trig_link_selection_wk);
141807bf5297SMiri Korenblit 	wiphy_work_flush(mvm->hw->wiphy, &mvm->async_handlers_wiphy_wk);
1419e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
142024afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1421771147b0SJohannes Berg 
1422771147b0SJohannes Berg 	/*
1423771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1424771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1425771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
14267174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1427771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1428771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1429771147b0SJohannes Berg 	 */
1430771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1431771147b0SJohannes Berg 
1432d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
143369e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1434e705c121SKalle Valo 
1435f9084775SNathan Errera 	/*
1436f9084775SNathan Errera 	 * The work item could be running or queued if the
1437f9084775SNathan Errera 	 * ROC time event stops just as we get here.
1438f9084775SNathan Errera 	 */
1439f9084775SNathan Errera 	flush_work(&mvm->roc_done_wk);
1440f9084775SNathan Errera 
14417ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
14427ce1f215SEmmanuel Grumbach 
1443e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1444e8bb19c1SEmmanuel Grumbach 	__iwl_mvm_mac_stop(mvm, suspend);
1445e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1446e705c121SKalle Valo 
1447e705c121SKalle Valo 	/*
1448e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1449e705c121SKalle Valo 	 * discover that its list is now empty.
1450e705c121SKalle Valo 	 */
1451e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1452fdccafadSMiri Korenblit 	wiphy_work_cancel(hw->wiphy, &mvm->async_handlers_wiphy_wk);
1453e705c121SKalle Valo }
1454e705c121SKalle Valo 
iwl_mvm_get_free_phy_ctxt(struct iwl_mvm * mvm)14551ab26632SMiri Korenblit struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1456e705c121SKalle Valo {
1457e705c121SKalle Valo 	u16 i;
1458e705c121SKalle Valo 
1459e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1460e705c121SKalle Valo 
1461e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1462e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1463e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1464e705c121SKalle Valo 
1465e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1466e705c121SKalle Valo 	return NULL;
1467e705c121SKalle Valo }
1468e705c121SKalle Valo 
iwl_mvm_set_tx_power(struct iwl_mvm * mvm,struct ieee80211_vif * vif,s16 tx_power)1469f551d013SGregory Greenman int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1470e705c121SKalle Valo 			 s16 tx_power)
1471e705c121SKalle Valo {
1472971cbe50SJohannes Berg 	u32 cmd_id = REDUCE_TX_POWER_CMD;
14730791c2fcSHaim Dreyfuss 	int len;
1474cfc13542SEmmanuel Grumbach 	struct iwl_dev_tx_power_cmd_v3_v8 cmd = {
1475216cdfb5SLuca Coelho 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1476216cdfb5SLuca Coelho 		.common.mac_context_id =
1477e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1478e705c121SKalle Valo 	};
1479535f0190SEmmanuel Grumbach 	struct iwl_dev_tx_power_cmd cmd_v9_v10;
1480535f0190SEmmanuel Grumbach 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 3);
1481535f0190SEmmanuel Grumbach 	u16 u_tx_power = tx_power == IWL_DEFAULT_MAX_TX_POWER ?
1482535f0190SEmmanuel Grumbach 		IWL_DEV_MAX_TX_POWER : 8 * tx_power;
1483535f0190SEmmanuel Grumbach 	void *cmd_data = &cmd;
1484e705c121SKalle Valo 
1485535f0190SEmmanuel Grumbach 	cmd.common.pwr_restriction = cpu_to_le16(u_tx_power);
1486e705c121SKalle Valo 
1487535f0190SEmmanuel Grumbach 	if (cmd_ver > 8) {
1488535f0190SEmmanuel Grumbach 		/* Those fields sit on the same place for v9 and v10 */
1489535f0190SEmmanuel Grumbach 		cmd_v9_v10.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC);
1490535f0190SEmmanuel Grumbach 		cmd_v9_v10.common.mac_context_id =
1491535f0190SEmmanuel Grumbach 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id);
1492535f0190SEmmanuel Grumbach 		cmd_v9_v10.common.pwr_restriction = cpu_to_le16(u_tx_power);
1493535f0190SEmmanuel Grumbach 		cmd_data = &cmd_v9_v10;
1494535f0190SEmmanuel Grumbach 	}
1495535f0190SEmmanuel Grumbach 
1496535f0190SEmmanuel Grumbach 	if (cmd_ver == 10)
1497535f0190SEmmanuel Grumbach 		len = sizeof(cmd_v9_v10.v10);
1498535f0190SEmmanuel Grumbach 	else if (cmd_ver == 9)
1499535f0190SEmmanuel Grumbach 		len = sizeof(cmd_v9_v10.v9);
1500535f0190SEmmanuel Grumbach 	else if (cmd_ver == 8)
15018f892e22SEmmanuel Grumbach 		len = sizeof(cmd.v8);
15028f892e22SEmmanuel Grumbach 	else if (cmd_ver == 7)
1503b0aa02b3SAyala Barazani 		len = sizeof(cmd.v7);
1504b0aa02b3SAyala Barazani 	else if (cmd_ver == 6)
1505fbb7957dSLuca Coelho 		len = sizeof(cmd.v6);
1506fbb7957dSLuca Coelho 	else if (fw_has_api(&mvm->fw->ucode_capa,
15070791c2fcSHaim Dreyfuss 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
15080791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
15090791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
15100791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
15110791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
15120791c2fcSHaim Dreyfuss 	else
1513216cdfb5SLuca Coelho 		len = sizeof(cmd.v3);
1514216cdfb5SLuca Coelho 
1515cfc13542SEmmanuel Grumbach 	/* all structs have the same common part, add its length */
1516216cdfb5SLuca Coelho 	len += sizeof(cmd.common);
1517e705c121SKalle Valo 
1518535f0190SEmmanuel Grumbach 	if (cmd_ver < 9)
1519cfc13542SEmmanuel Grumbach 		len += sizeof(cmd.per_band);
1520cfc13542SEmmanuel Grumbach 
1521535f0190SEmmanuel Grumbach 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, cmd_data);
1522535f0190SEmmanuel Grumbach 
1523e705c121SKalle Valo }
1524e705c121SKalle Valo 
iwl_mvm_post_csa_tx(void * data,struct ieee80211_sta * sta)1525bbd6d0f8SJohannes Berg static void iwl_mvm_post_csa_tx(void *data, struct ieee80211_sta *sta)
1526bbd6d0f8SJohannes Berg {
1527bbd6d0f8SJohannes Berg 	struct ieee80211_hw *hw = data;
1528bbd6d0f8SJohannes Berg 	int i;
1529bbd6d0f8SJohannes Berg 
1530bbd6d0f8SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1531bbd6d0f8SJohannes Berg 		struct iwl_mvm_txq *mvmtxq =
1532bbd6d0f8SJohannes Berg 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1533bbd6d0f8SJohannes Berg 
1534bbd6d0f8SJohannes Berg 		clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1535bbd6d0f8SJohannes Berg 		iwl_mvm_mac_itxq_xmit(hw, sta->txq[i]);
1536bbd6d0f8SJohannes Berg 	}
1537bbd6d0f8SJohannes Berg }
1538bbd6d0f8SJohannes Berg 
iwl_mvm_post_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)153903117f30SMiri Korenblit int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1540a469a593SEmmanuel Grumbach 				struct ieee80211_vif *vif,
1541a469a593SEmmanuel Grumbach 				struct ieee80211_bss_conf *link_conf)
1542f6780614SSara Sharon {
1543f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1544f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1545f6780614SSara Sharon 	int ret;
1546f6780614SSara Sharon 
1547f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1548f6780614SSara Sharon 
1549f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1550f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
15513723c7c5SEmmanuel Grumbach 		unsigned int link_id = link_conf->link_id;
15523723c7c5SEmmanuel Grumbach 		u8 ap_sta_id = mvmvif->link[link_id]->ap_sta_id;
1553f6780614SSara Sharon 
1554f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
15552964b57fSJohannes Berg 		mvmvif->csa_blocks_tx = false;
15563723c7c5SEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm, ap_sta_id);
1557f6780614SSara Sharon 
1558f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1559f6780614SSara Sharon 			ret = -EIO;
1560f6780614SSara Sharon 			goto out_unlock;
1561f6780614SSara Sharon 		}
1562f6780614SSara Sharon 
1563f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
156403117f30SMiri Korenblit 		if (mvm->mld_api_is_used)
156503117f30SMiri Korenblit 			iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
156603117f30SMiri Korenblit 		else
1567f6780614SSara Sharon 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1568f6780614SSara Sharon 
15690202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
15700202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
157187f5b5f2SJohannes Berg 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
1572f6780614SSara Sharon 			if (ret)
1573f6780614SSara Sharon 				goto out_unlock;
1574f6780614SSara Sharon 
1575f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1576f6780614SSara Sharon 		}
1577bbd6d0f8SJohannes Berg 	} else if (vif->type == NL80211_IFTYPE_AP && mvmvif->csa_blocks_tx) {
1578bbd6d0f8SJohannes Berg 		struct iwl_mvm_txq *mvmtxq =
1579bbd6d0f8SJohannes Berg 			iwl_mvm_txq_from_mac80211(vif->txq);
1580bbd6d0f8SJohannes Berg 
1581bbd6d0f8SJohannes Berg 		clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1582bbd6d0f8SJohannes Berg 
1583bbd6d0f8SJohannes Berg 		local_bh_disable();
1584bbd6d0f8SJohannes Berg 		iwl_mvm_mac_itxq_xmit(hw, vif->txq);
1585bbd6d0f8SJohannes Berg 		ieee80211_iterate_stations_atomic(hw, iwl_mvm_post_csa_tx, hw);
1586bbd6d0f8SJohannes Berg 		local_bh_enable();
1587bbd6d0f8SJohannes Berg 
1588bbd6d0f8SJohannes Berg 		mvmvif->csa_blocks_tx = false;
15890202bcf0SEmmanuel Grumbach 	}
1590f6780614SSara Sharon 
1591f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1592f6780614SSara Sharon 
1593f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1594f6780614SSara Sharon 
1595f6780614SSara Sharon out_unlock:
1596caf46377SSara Sharon 	if (mvmvif->csa_failed)
1597caf46377SSara Sharon 		ret = -EIO;
1598f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1599f6780614SSara Sharon 
1600f6780614SSara Sharon 	return ret;
1601f6780614SSara Sharon }
1602f6780614SSara Sharon 
iwl_mvm_abort_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)1603cbce62a3SMiri Korenblit void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
16045ecd5d82SJohannes Berg 				  struct ieee80211_vif *vif,
16055ecd5d82SJohannes Berg 				  struct ieee80211_bss_conf *link_conf)
1606f6780614SSara Sharon {
1607f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1608f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1609f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1610f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1611f6780614SSara Sharon 							  mvmvif->color)),
1612f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1613f6780614SSara Sharon 	};
1614f6780614SSara Sharon 
1615ad12b231SNathan Errera 	/*
1616ad12b231SNathan Errera 	 * In the new flow since FW is in charge of the timing,
1617ad12b231SNathan Errera 	 * if driver has canceled the channel switch he will receive the
1618ad12b231SNathan Errera 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1619ad12b231SNathan Errera 	 */
1620ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1621ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1622ad12b231SNathan Errera 		return;
1623ad12b231SNathan Errera 
1624f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1625f6780614SSara Sharon 
1626f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
162758ddd9b6SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
162858ddd9b6SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
162958ddd9b6SEmmanuel Grumbach 		iwl_mvm_remove_csa_period(mvm, vif);
163058ddd9b6SEmmanuel Grumbach 	else
1631f6780614SSara Sharon 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1632f6780614SSara Sharon 					     WIDE_ID(MAC_CONF_GROUP,
1633f6780614SSara Sharon 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1634f6780614SSara Sharon 					     0, sizeof(cmd), &cmd));
1635caf46377SSara Sharon 	mvmvif->csa_failed = true;
1636f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1637f6780614SSara Sharon 
1638a469a593SEmmanuel Grumbach 	/* If we're here, we can't support MLD */
1639a469a593SEmmanuel Grumbach 	iwl_mvm_post_channel_switch(hw, vif, &vif->bss_conf);
1640f6780614SSara Sharon }
1641f6780614SSara Sharon 
iwl_mvm_channel_switch_disconnect_wk(struct work_struct * wk)16421ab26632SMiri Korenblit void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1643f6780614SSara Sharon {
1644f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1645f6780614SSara Sharon 	struct ieee80211_vif *vif;
1646f6780614SSara Sharon 
1647f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1648f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1649f6780614SSara Sharon 
165070162580SShaul Triebitz 	/* Trigger disconnect (should clear the CSA state) */
1651a469a593SEmmanuel Grumbach 	ieee80211_chswitch_done(vif, false, 0);
1652f6780614SSara Sharon }
1653f6780614SSara Sharon 
16545abf3154SMordechay Goodstein static u8
iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def * chandef)16555abf3154SMordechay Goodstein iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
16565abf3154SMordechay Goodstein {
16575abf3154SMordechay Goodstein 	int data_start;
16585abf3154SMordechay Goodstein 	int control_start;
16595abf3154SMordechay Goodstein 	int bw;
16605abf3154SMordechay Goodstein 
16615abf3154SMordechay Goodstein 	if (chandef->width == NL80211_CHAN_WIDTH_320)
16625abf3154SMordechay Goodstein 		bw = 320;
16635abf3154SMordechay Goodstein 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
16645abf3154SMordechay Goodstein 		bw = 160;
16655abf3154SMordechay Goodstein 	else
16665abf3154SMordechay Goodstein 		return 0;
16675abf3154SMordechay Goodstein 
16685abf3154SMordechay Goodstein 	/* data is bw wide so the start is half the width */
16695abf3154SMordechay Goodstein 	data_start = chandef->center_freq1 - bw / 2;
16705abf3154SMordechay Goodstein 	/* control is 20Mhz width */
16715abf3154SMordechay Goodstein 	control_start = chandef->chan->center_freq - 10;
16725abf3154SMordechay Goodstein 
16735abf3154SMordechay Goodstein 	return (control_start - data_start) / 80;
16745abf3154SMordechay Goodstein }
16755abf3154SMordechay Goodstein 
iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)16761be4858eSJohannes Berg static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
16771be4858eSJohannes Berg 					 struct ieee80211_vif *vif)
1678e705c121SKalle Valo {
1679e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
16801be4858eSJohannes Berg 	int ret;
16811ab26632SMiri Korenblit 
16821ab26632SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
1683e705c121SKalle Valo 
16841be4858eSJohannes Berg 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
16851be4858eSJohannes Berg 	if (ret) {
16861be4858eSJohannes Berg 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
16871be4858eSJohannes Berg 		return ret;
16881be4858eSJohannes Berg 	}
16891be4858eSJohannes Berg 
16901be4858eSJohannes Berg 	/* Only queue for this station is the mcast queue,
16911be4858eSJohannes Berg 	 * which shouldn't be in TFD mask anyway
16921be4858eSJohannes Berg 	 */
16931be4858eSJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
16941be4858eSJohannes Berg 					vif->type,
16951be4858eSJohannes Berg 					IWL_STA_MULTICAST);
16961be4858eSJohannes Berg }
16971be4858eSJohannes Berg 
iwl_mvm_prevent_esr_done_wk(struct wiphy * wiphy,struct wiphy_work * wk)1698492bc4e4SMiri Korenblit static void iwl_mvm_prevent_esr_done_wk(struct wiphy *wiphy,
1699492bc4e4SMiri Korenblit 					struct wiphy_work *wk)
1700492bc4e4SMiri Korenblit {
1701492bc4e4SMiri Korenblit 	struct iwl_mvm_vif *mvmvif =
1702492bc4e4SMiri Korenblit 		container_of(wk, struct iwl_mvm_vif, prevent_esr_done_wk.work);
1703492bc4e4SMiri Korenblit 	struct iwl_mvm *mvm = mvmvif->mvm;
17044130c67cSMiri Korenblit 	struct ieee80211_vif *vif =
17054130c67cSMiri Korenblit 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1706492bc4e4SMiri Korenblit 
17073f312651SJohannes Berg 	guard(mvm)(mvm);
1708492bc4e4SMiri Korenblit 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_PREVENTION);
1709492bc4e4SMiri Korenblit }
1710492bc4e4SMiri Korenblit 
iwl_mvm_mlo_int_scan_wk(struct wiphy * wiphy,struct wiphy_work * wk)17112f33561eSMiri Korenblit static void iwl_mvm_mlo_int_scan_wk(struct wiphy *wiphy, struct wiphy_work *wk)
17122f33561eSMiri Korenblit {
17132f33561eSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = container_of(wk, struct iwl_mvm_vif,
17142f33561eSMiri Korenblit 						  mlo_int_scan_wk.work);
17152f33561eSMiri Korenblit 	struct ieee80211_vif *vif =
17162f33561eSMiri Korenblit 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
17172f33561eSMiri Korenblit 
17183f312651SJohannes Berg 	guard(mvm)(mvmvif->mvm);
17192f33561eSMiri Korenblit 	iwl_mvm_int_mlo_scan(mvmvif->mvm, vif);
17202f33561eSMiri Korenblit }
17212f33561eSMiri Korenblit 
iwl_mvm_unblock_esr_tpt(struct wiphy * wiphy,struct wiphy_work * wk)1722ec0d43d2SMiri Korenblit static void iwl_mvm_unblock_esr_tpt(struct wiphy *wiphy, struct wiphy_work *wk)
1723ec0d43d2SMiri Korenblit {
1724ec0d43d2SMiri Korenblit 	struct iwl_mvm_vif *mvmvif =
1725ec0d43d2SMiri Korenblit 		container_of(wk, struct iwl_mvm_vif, unblock_esr_tpt_wk);
1726ec0d43d2SMiri Korenblit 	struct iwl_mvm *mvm = mvmvif->mvm;
17274130c67cSMiri Korenblit 	struct ieee80211_vif *vif =
17284130c67cSMiri Korenblit 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1729ec0d43d2SMiri Korenblit 
17303f312651SJohannes Berg 	guard(mvm)(mvm);
1731ec0d43d2SMiri Korenblit 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT);
1732ec0d43d2SMiri Korenblit }
1733ec0d43d2SMiri Korenblit 
iwl_mvm_mac_init_mvmvif(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif)17340bcc2155SJohannes Berg void iwl_mvm_mac_init_mvmvif(struct iwl_mvm *mvm, struct iwl_mvm_vif *mvmvif)
17350bcc2155SJohannes Berg {
17360bcc2155SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
17370bcc2155SJohannes Berg 
17380bcc2155SJohannes Berg 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
17390bcc2155SJohannes Berg 		return;
17400bcc2155SJohannes Berg 
17410bcc2155SJohannes Berg 	INIT_DELAYED_WORK(&mvmvif->csa_work,
17420bcc2155SJohannes Berg 			  iwl_mvm_channel_switch_disconnect_wk);
1743492bc4e4SMiri Korenblit 
1744492bc4e4SMiri Korenblit 	wiphy_delayed_work_init(&mvmvif->prevent_esr_done_wk,
1745492bc4e4SMiri Korenblit 				iwl_mvm_prevent_esr_done_wk);
17462f33561eSMiri Korenblit 
17472f33561eSMiri Korenblit 	wiphy_delayed_work_init(&mvmvif->mlo_int_scan_wk,
17482f33561eSMiri Korenblit 				iwl_mvm_mlo_int_scan_wk);
1749ec0d43d2SMiri Korenblit 
1750ec0d43d2SMiri Korenblit 	wiphy_work_init(&mvmvif->unblock_esr_tpt_wk,
1751ec0d43d2SMiri Korenblit 			iwl_mvm_unblock_esr_tpt);
17520bcc2155SJohannes Berg }
17530bcc2155SJohannes Berg 
iwl_mvm_mac_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)17541be4858eSJohannes Berg static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
17551be4858eSJohannes Berg 				     struct ieee80211_vif *vif)
17561be4858eSJohannes Berg {
17571be4858eSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
17581be4858eSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
17591be4858eSJohannes Berg 	int ret;
1760b6e3d1baSAnjaneyulu 	int i;
17611be4858eSJohannes Berg 
17621be4858eSJohannes Berg 	mutex_lock(&mvm->mutex);
17631be4858eSJohannes Berg 
17640bcc2155SJohannes Berg 	iwl_mvm_mac_init_mvmvif(mvm, mvmvif);
17650bcc2155SJohannes Berg 
1766e705c121SKalle Valo 	mvmvif->mvm = mvm;
1767650cadb7SGregory Greenman 
1768650cadb7SGregory Greenman 	/* the first link always points to the default one */
1769a8b5d480SIlan Peer 	mvmvif->deflink.fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1770a8b5d480SIlan Peer 	mvmvif->deflink.active = 0;
1771650cadb7SGregory Greenman 	mvmvif->link[0] = &mvmvif->deflink;
1772e705c121SKalle Valo 
1773a8b5d480SIlan Peer 	ret = iwl_mvm_set_link_mapping(mvm, vif, &vif->bss_conf);
1774a8b5d480SIlan Peer 	if (ret)
1775a8b5d480SIlan Peer 		goto out;
1776a8b5d480SIlan Peer 
1777e705c121SKalle Valo 	/*
1778e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1779e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1780e705c121SKalle Valo 	 * don't really have to check the types.
1781e705c121SKalle Valo 	 */
1782e705c121SKalle Valo 
1783e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1784e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1785b6e3d1baSAnjaneyulu 		for_each_mvm_vif_valid_link(mvmvif, i)
1786b6e3d1baSAnjaneyulu 			mvmvif->link[i]->beacon_stats.accu_num_beacons +=
1787b6e3d1baSAnjaneyulu 				mvmvif->link[i]->beacon_stats.num_beacons;
1788e705c121SKalle Valo 
1789e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
17901be4858eSJohannes Berg 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
17911be4858eSJohannes Berg 	if (ret)
17921be4858eSJohannes Berg 		goto out;
1793e705c121SKalle Valo 
1794698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1795698478c4SSara Sharon 
17961be4858eSJohannes Berg 	/* Currently not much to do for NAN */
17971be4858eSJohannes Berg 	if (vif->type == NL80211_IFTYPE_NAN) {
17981be4858eSJohannes Berg 		ret = 0;
17991be4858eSJohannes Berg 		goto out;
18001be4858eSJohannes Berg 	}
18011be4858eSJohannes Berg 
1802e705c121SKalle Valo 	/*
1803e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1804e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1805e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1806e705c121SKalle Valo 	 * only after the binding.
1807e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1808e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1809e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1810e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1811e705c121SKalle Valo 	 * allocating resources :)
1812e705c121SKalle Valo 	 */
1813e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1814e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
18153a3ef394SBenjamin Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1816c36235acSJohannes Berg 			iwl_mvm_vif_dbgfs_add_link(mvm, vif);
18171be4858eSJohannes Berg 		ret = 0;
18181be4858eSJohannes Berg 		goto out;
18191ab26632SMiri Korenblit 	}
18201ab26632SMiri Korenblit 
18211ab26632SMiri Korenblit 	mvmvif->features |= hw->netdev_features;
1822650cadb7SGregory Greenman 
1823e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1824e705c121SKalle Valo 	if (ret)
182598c0de7bSMiri Korenblit 		goto out_unlock;
1826e705c121SKalle Valo 
1827e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1828e705c121SKalle Valo 	if (ret)
1829e705c121SKalle Valo 		goto out_remove_mac;
1830e705c121SKalle Valo 
1831e705c121SKalle Valo 	/* beacon filtering */
183287f5b5f2SJohannes Berg 	ret = iwl_mvm_disable_beacon_filter(mvm, vif);
1833e705c121SKalle Valo 	if (ret)
1834e705c121SKalle Valo 		goto out_remove_mac;
1835e705c121SKalle Valo 
1836e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1837e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1838e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1839e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1840e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1841e705c121SKalle Valo 	}
1842e705c121SKalle Valo 
184384ef7cbeSIlan Peer 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1844e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1845e705c121SKalle Valo 
1846b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1847b0ffe455SJohannes Berg 
18485abf3154SMordechay Goodstein 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1849baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
18505abf3154SMordechay Goodstein 		mvm->monitor_p80 =
18516092077aSJohannes Berg 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chanreq.oper);
18525abf3154SMordechay Goodstein 	}
1853baf41bc3SShaul Triebitz 
18543a3ef394SBenjamin Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1855c36235acSJohannes Berg 		iwl_mvm_vif_dbgfs_add_link(mvm, vif);
18566d19a5ebSEmmanuel Grumbach 
18576d19a5ebSEmmanuel Grumbach 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
18586d19a5ebSEmmanuel Grumbach 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
18596d19a5ebSEmmanuel Grumbach 	    !mvm->csme_vif && mvm->mei_registered) {
18606d19a5ebSEmmanuel Grumbach 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
18616d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
18626d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = vif;
18636d19a5ebSEmmanuel Grumbach 	}
18646d19a5ebSEmmanuel Grumbach 
18651ab26632SMiri Korenblit out:
18661ab26632SMiri Korenblit 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
18671ab26632SMiri Korenblit 		     vif->type == NL80211_IFTYPE_ADHOC))
18681ab26632SMiri Korenblit 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
18691ab26632SMiri Korenblit 
1870e705c121SKalle Valo 	goto out_unlock;
1871e705c121SKalle Valo 
1872e705c121SKalle Valo  out_remove_mac:
1873650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
1874e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1875e705c121SKalle Valo  out_unlock:
1876e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1877e705c121SKalle Valo 
1878e705c121SKalle Valo 	return ret;
1879e705c121SKalle Valo }
1880e705c121SKalle Valo 
iwl_mvm_prepare_mac_removal(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1881cb145863SJohannes Berg void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1882e705c121SKalle Valo 				 struct ieee80211_vif *vif)
1883e705c121SKalle Valo {
18840bcc2155SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
18850bcc2155SJohannes Berg 
1886e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1887e705c121SKalle Valo 		/*
1888e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1889e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1890e705c121SKalle Valo 		 * queue are sent in ROC session.
1891e705c121SKalle Valo 		 */
1892e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1893e705c121SKalle Valo 	}
18940bcc2155SJohannes Berg 
1895492bc4e4SMiri Korenblit 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1896492bc4e4SMiri Korenblit 				  &mvmvif->prevent_esr_done_wk);
1897492bc4e4SMiri Korenblit 
18982f33561eSMiri Korenblit 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
18992f33561eSMiri Korenblit 				  &mvmvif->mlo_int_scan_wk);
19002f33561eSMiri Korenblit 
1901ec0d43d2SMiri Korenblit 	wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
1902ec0d43d2SMiri Korenblit 
19030bcc2155SJohannes Berg 	cancel_delayed_work_sync(&mvmvif->csa_work);
1904e705c121SKalle Valo }
1905e705c121SKalle Valo 
iwl_mvm_mac_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)19067714a405SJohannes Berg static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1907e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1908e705c121SKalle Valo {
1909e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1910e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
191186e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1912e705c121SKalle Valo 
1913e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1914e705c121SKalle Valo 
1915b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1916b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1917b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1918b0ffe455SJohannes Berg 
1919e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1920e705c121SKalle Valo 
19216d19a5ebSEmmanuel Grumbach 	if (vif == mvm->csme_vif) {
19226d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(NULL);
19236d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = NULL;
19246d19a5ebSEmmanuel Grumbach 	}
19256d19a5ebSEmmanuel Grumbach 
1926650cadb7SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
192786e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
1928650cadb7SGregory Greenman 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
192986e177d8SGregory Greenman 	if (probe_data)
193086e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
193186e177d8SGregory Greenman 
1932e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1933e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1934e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1935e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1936e705c121SKalle Valo 	}
1937e705c121SKalle Valo 
1938b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1939b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1940b73f9a4aSJohannes Berg 
1941c36235acSJohannes Berg 	iwl_mvm_vif_dbgfs_rm_link(mvm, vif);
1942e705c121SKalle Valo 
1943e705c121SKalle Valo 	/*
1944e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1945e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1946e705c121SKalle Valo 	 */
1947e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1948e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1949e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1950e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1951e705c121SKalle Valo 			mvm->noa_vif = NULL;
1952e705c121SKalle Valo 			mvm->noa_duration = 0;
1953e705c121SKalle Valo 		}
1954e705c121SKalle Valo #endif
19557714a405SJohannes Berg 		goto out;
1956e705c121SKalle Valo 	}
1957e705c121SKalle Valo 
195860efeca1SMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
195960efeca1SMiri Korenblit 
196084ef7cbeSIlan Peer 	/* Before the interface removal, mac80211 would cancel the ROC, and the
196184ef7cbeSIlan Peer 	 * ROC worker would be scheduled if needed. The worker would be flushed
196284ef7cbeSIlan Peer 	 * in iwl_mvm_prepare_mac_removal() and thus at this point there is no
196384ef7cbeSIlan Peer 	 * binding etc. so nothing needs to be done here.
196484ef7cbeSIlan Peer 	 */
1965e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
196684ef7cbeSIlan Peer 		if (mvmvif->deflink.phy_ctxt) {
1967650cadb7SGregory Greenman 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1968650cadb7SGregory Greenman 			mvmvif->deflink.phy_ctxt = NULL;
1969e705c121SKalle Valo 		}
197084ef7cbeSIlan Peer 		mvm->p2p_device_vif = NULL;
197184ef7cbeSIlan Peer 	}
1972e705c121SKalle Valo 
1973a8b5d480SIlan Peer 	iwl_mvm_unset_link_mapping(mvm, vif, &vif->bss_conf);
1974e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1975e705c121SKalle Valo 
1976698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1977698478c4SSara Sharon 
1978baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1979baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1980baf41bc3SShaul Triebitz 
198160efeca1SMiri Korenblit out:
198260efeca1SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP ||
198360efeca1SMiri Korenblit 	    vif->type == NL80211_IFTYPE_ADHOC) {
1984650cadb7SGregory Greenman 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
198560efeca1SMiri Korenblit 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1986e705c121SKalle Valo 	}
1987e705c121SKalle Valo 
1988e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1989e705c121SKalle Valo }
1990e705c121SKalle Valo 
1991e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1992e705c121SKalle Valo 	struct iwl_mvm *mvm;
1993e705c121SKalle Valo 	int port_id;
1994e705c121SKalle Valo };
1995e705c121SKalle Valo 
iwl_mvm_mc_iface_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)1996e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1997e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1998e705c121SKalle Valo {
1999e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
2000e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
2001e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
200297bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
200397bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
200497bce57bSLuca Coelho 		.flags = CMD_ASYNC,
200597bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
200697bce57bSLuca Coelho 	};
2007e705c121SKalle Valo 	int ret, len;
2008e705c121SKalle Valo 
2009e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
2010e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
2011e705c121SKalle Valo 		return;
2012e705c121SKalle Valo 
2013e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
2014f276e20bSJohannes Berg 	    !vif->cfg.assoc)
2015e705c121SKalle Valo 		return;
2016e705c121SKalle Valo 
2017e705c121SKalle Valo 	cmd->port_id = data->port_id++;
2018e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
2019e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
2020e705c121SKalle Valo 
202197bce57bSLuca Coelho 	hcmd.len[0] = len;
202297bce57bSLuca Coelho 	hcmd.data[0] = cmd;
202397bce57bSLuca Coelho 
202497bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2025e705c121SKalle Valo 	if (ret)
2026e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
2027e705c121SKalle Valo }
2028e705c121SKalle Valo 
iwl_mvm_recalc_multicast(struct iwl_mvm * mvm)2029e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
2030e705c121SKalle Valo {
2031e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
2032e705c121SKalle Valo 		.mvm = mvm,
2033e705c121SKalle Valo 	};
2034db66abeeSJohannes Berg 	int ret;
2035e705c121SKalle Valo 
2036e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2037e705c121SKalle Valo 
2038e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
2039e705c121SKalle Valo 		return;
2040e705c121SKalle Valo 
2041e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
2042e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2043e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
2044db66abeeSJohannes Berg 
2045db66abeeSJohannes Berg 	/*
2046db66abeeSJohannes Berg 	 * Send a (synchronous) ech command so that we wait for the
2047db66abeeSJohannes Berg 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
2048db66abeeSJohannes Berg 	 * the interface iterator. Otherwise, we might get here over
2049db66abeeSJohannes Berg 	 * and over again (by userspace just sending a lot of these)
2050db66abeeSJohannes Berg 	 * and the CPU can send them faster than the firmware can
2051db66abeeSJohannes Berg 	 * process them.
2052db66abeeSJohannes Berg 	 * Note that the CPU is still faster - but with this we'll
2053db66abeeSJohannes Berg 	 * actually send fewer commands overall because the CPU will
2054db66abeeSJohannes Berg 	 * not schedule the work in mac80211 as frequently if it's
2055db66abeeSJohannes Berg 	 * still running when rescheduled (possibly multiple times).
2056db66abeeSJohannes Berg 	 */
2057db66abeeSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
2058db66abeeSJohannes Berg 	if (ret)
2059db66abeeSJohannes Berg 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
2060e705c121SKalle Valo }
2061e705c121SKalle Valo 
iwl_mvm_prepare_multicast(struct ieee80211_hw * hw,struct netdev_hw_addr_list * mc_list)2062cbce62a3SMiri Korenblit u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
2063e705c121SKalle Valo 			      struct netdev_hw_addr_list *mc_list)
2064e705c121SKalle Valo {
2065e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2066e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
2067e705c121SKalle Valo 	struct netdev_hw_addr *addr;
2068e705c121SKalle Valo 	int addr_count;
2069e705c121SKalle Valo 	bool pass_all;
2070e705c121SKalle Valo 	int len;
2071e705c121SKalle Valo 
2072e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
2073e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
2074e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
2075e705c121SKalle Valo 	if (pass_all)
2076e705c121SKalle Valo 		addr_count = 0;
2077e705c121SKalle Valo 
2078e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
2079e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
2080e705c121SKalle Valo 	if (!cmd)
2081e705c121SKalle Valo 		return 0;
2082e705c121SKalle Valo 
2083e705c121SKalle Valo 	if (pass_all) {
2084e705c121SKalle Valo 		cmd->pass_all = 1;
2085e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
2086e705c121SKalle Valo 	}
2087e705c121SKalle Valo 
2088e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
2089e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
2090e705c121SKalle Valo 				   cmd->count, addr->addr);
2091e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
2092e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
2093e705c121SKalle Valo 		cmd->count++;
2094e705c121SKalle Valo 	}
2095e705c121SKalle Valo 
2096e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
2097e705c121SKalle Valo }
2098e705c121SKalle Valo 
iwl_mvm_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)2099cbce62a3SMiri Korenblit void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
2100e705c121SKalle Valo 			      unsigned int changed_flags,
2101cbce62a3SMiri Korenblit 			      unsigned int *total_flags, u64 multicast)
2102e705c121SKalle Valo {
2103e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2104e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
2105e705c121SKalle Valo 
21063f312651SJohannes Berg 	guard(mvm)(mvm);
2107e705c121SKalle Valo 
2108e705c121SKalle Valo 	/* replace previous configuration */
2109e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
2110e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
2111e705c121SKalle Valo 
2112e705c121SKalle Valo 	if (!cmd)
2113e705c121SKalle Valo 		goto out;
2114e705c121SKalle Valo 
211561e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
211661e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
211761e7d91bSLuca Coelho 
211861e7d91bSLuca Coelho 	if (cmd->pass_all)
211961e7d91bSLuca Coelho 		cmd->count = 0;
212061e7d91bSLuca Coelho 
2121e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
2122e705c121SKalle Valo out:
2123e705c121SKalle Valo 	*total_flags = 0;
2124e705c121SKalle Valo }
2125e705c121SKalle Valo 
iwl_mvm_config_iface_filter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int filter_flags,unsigned int changed_flags)2126e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
2127e705c121SKalle Valo 					struct ieee80211_vif *vif,
2128e705c121SKalle Valo 					unsigned int filter_flags,
2129e705c121SKalle Valo 					unsigned int changed_flags)
2130e705c121SKalle Valo {
2131e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2132e705c121SKalle Valo 
2133e705c121SKalle Valo 	/* We support only filter for probe requests */
2134e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
2135e705c121SKalle Valo 		return;
2136e705c121SKalle Valo 
2137e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
2138f276e20bSJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
2139e705c121SKalle Valo 	    !vif->p2p)
2140e705c121SKalle Valo 		return;
2141e705c121SKalle Valo 
21423f312651SJohannes Berg 	guard(mvm)(mvm);
2143e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2144e705c121SKalle Valo }
2145e705c121SKalle Valo 
iwl_mvm_update_mu_groups(struct iwl_mvm * mvm,struct ieee80211_vif * vif)214622c58834SGregory Greenman int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2147a07a8f37SSara Sharon {
2148a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
2149a07a8f37SSara Sharon 
2150a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
2151a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
2152a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
2153a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
2154a07a8f37SSara Sharon 
2155a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
2156a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
2157a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
2158a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
2159a07a8f37SSara Sharon }
2160a07a8f37SSara Sharon 
iwl_mvm_mu_mimo_iface_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)2161f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
2162f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
2163f92659a1SSara Sharon {
2164d0a9123eSJohannes Berg 	if (vif->bss_conf.mu_mimo_owner) {
2165f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
2166f92659a1SSara Sharon 
2167f92659a1SSara Sharon 		/*
2168f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
2169f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
2170f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
2171f92659a1SSara Sharon 		 */
2172afe0d181SJohannes Berg 		ieee80211_update_mu_groups(vif, 0,
2173f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
2174f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
2175f92659a1SSara Sharon 	}
2176f92659a1SSara Sharon }
2177f92659a1SSara Sharon 
iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)2178f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
2179f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
2180f92659a1SSara Sharon {
2181f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2182f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
2183f92659a1SSara Sharon 
2184f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
2185f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2186f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
2187f92659a1SSara Sharon }
2188f92659a1SSara Sharon 
iwl_mvm_he_get_ppe_val(u8 * ppe,u8 ppe_pos_bit)2189514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2190514c3069SLuca Coelho {
2191514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
2192514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
2193514c3069SLuca Coelho 	u8 residue_bits;
2194514c3069SLuca Coelho 	u8 res;
2195514c3069SLuca Coelho 
2196514c3069SLuca Coelho 	if (bit_num <= 5)
2197514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
2198514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2199514c3069SLuca Coelho 
2200514c3069SLuca Coelho 	/*
2201514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
2202514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
2203514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
2204514c3069SLuca Coelho 	 */
2205514c3069SLuca Coelho 
2206514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
2207514c3069SLuca Coelho 
2208514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
2209514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2210514c3069SLuca Coelho 	      residue_bits;
2211514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2212514c3069SLuca Coelho 
2213514c3069SLuca Coelho 	return res;
2214514c3069SLuca Coelho }
2215514c3069SLuca Coelho 
iwl_mvm_parse_ppe(struct iwl_mvm * mvm,struct iwl_he_pkt_ext_v2 * pkt_ext,u8 nss,u8 ru_index_bitmap,u8 * ppe,u8 ppe_pos_bit,bool inheritance)2216091296d3SMiri Korenblit static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
2217091296d3SMiri Korenblit 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
2218cb63eb43SMiri Korenblit 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
2219cb63eb43SMiri Korenblit 			      bool inheritance)
2220091296d3SMiri Korenblit {
2221091296d3SMiri Korenblit 	int i;
2222091296d3SMiri Korenblit 
2223091296d3SMiri Korenblit 	/*
2224091296d3SMiri Korenblit 	* FW currently supports only nss == MAX_HE_SUPP_NSS
2225091296d3SMiri Korenblit 	*
2226091296d3SMiri Korenblit 	* If nss > MAX: we can ignore values we don't support
2227091296d3SMiri Korenblit 	* If nss < MAX: we can set zeros in other streams
2228091296d3SMiri Korenblit 	*/
2229091296d3SMiri Korenblit 	if (nss > MAX_HE_SUPP_NSS) {
22304d8421f2SJason A. Donenfeld 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2231091296d3SMiri Korenblit 			       MAX_HE_SUPP_NSS);
2232091296d3SMiri Korenblit 		nss = MAX_HE_SUPP_NSS;
2233091296d3SMiri Korenblit 	}
2234091296d3SMiri Korenblit 
2235091296d3SMiri Korenblit 	for (i = 0; i < nss; i++) {
2236091296d3SMiri Korenblit 		u8 ru_index_tmp = ru_index_bitmap << 1;
2237091296d3SMiri Korenblit 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2238091296d3SMiri Korenblit 		u8 bw;
2239091296d3SMiri Korenblit 
2240091296d3SMiri Korenblit 		for (bw = 0;
2241091296d3SMiri Korenblit 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2242091296d3SMiri Korenblit 		     bw++) {
2243091296d3SMiri Korenblit 			ru_index_tmp >>= 1;
2244091296d3SMiri Korenblit 
2245cb63eb43SMiri Korenblit 			/*
2246cb63eb43SMiri Korenblit 			* According to the 11be spec, if for a specific BW the PPE Thresholds
2247cb63eb43SMiri Korenblit 			* isn't present - it should inherit the thresholds from the last
2248cb63eb43SMiri Korenblit 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2249cb63eb43SMiri Korenblit 			* this inheritance - continue in this case
2250cb63eb43SMiri Korenblit 			*/
2251cb63eb43SMiri Korenblit 			if (!(ru_index_tmp & 1)) {
2252cb63eb43SMiri Korenblit 				if (inheritance)
2253cb63eb43SMiri Korenblit 					goto set_thresholds;
2254cb63eb43SMiri Korenblit 				else
2255091296d3SMiri Korenblit 					continue;
2256cb63eb43SMiri Korenblit 			}
2257091296d3SMiri Korenblit 
2258091296d3SMiri Korenblit 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2259091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2260091296d3SMiri Korenblit 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2261091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2262091296d3SMiri Korenblit 
2263cb63eb43SMiri Korenblit set_thresholds:
2264091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2265091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2266091296d3SMiri Korenblit 		}
2267091296d3SMiri Korenblit 	}
2268091296d3SMiri Korenblit }
2269091296d3SMiri Korenblit 
iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm * mvm,struct ieee80211_link_sta * link_sta,struct iwl_he_pkt_ext_v2 * pkt_ext,bool inheritance)2270091296d3SMiri Korenblit static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
227157974a55SGregory Greenman 					    struct ieee80211_link_sta *link_sta,
2272cb63eb43SMiri Korenblit 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2273cb63eb43SMiri Korenblit 					    bool inheritance)
2274091296d3SMiri Korenblit {
227557974a55SGregory Greenman 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
227657974a55SGregory Greenman 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
227757974a55SGregory Greenman 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2278091296d3SMiri Korenblit 	u8 ru_index_bitmap =
2279091296d3SMiri Korenblit 		u8_get_bits(*ppe,
2280091296d3SMiri Korenblit 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2281091296d3SMiri Korenblit 	/* Starting after PPE header */
2282091296d3SMiri Korenblit 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2283091296d3SMiri Korenblit 
2284cb63eb43SMiri Korenblit 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2285cb63eb43SMiri Korenblit 			  inheritance);
2286091296d3SMiri Korenblit }
2287091296d3SMiri Korenblit 
22884df6a075SMiri Korenblit static int
iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 * pkt_ext,u8 nominal_padding)22894df6a075SMiri Korenblit iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
22904df6a075SMiri Korenblit 					 u8 nominal_padding)
2291091296d3SMiri Korenblit {
2292091296d3SMiri Korenblit 	int low_th = -1;
2293091296d3SMiri Korenblit 	int high_th = -1;
2294091296d3SMiri Korenblit 	int i;
2295091296d3SMiri Korenblit 
2296cb63eb43SMiri Korenblit 	/* all the macros are the same for EHT and HE */
2297091296d3SMiri Korenblit 	switch (nominal_padding) {
2298cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2299091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2300091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2301091296d3SMiri Korenblit 		break;
2302cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2303091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_BPSK;
2304091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2305091296d3SMiri Korenblit 		break;
2306cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2307cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2308091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2309091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_BPSK;
2310091296d3SMiri Korenblit 		break;
2311091296d3SMiri Korenblit 	}
2312091296d3SMiri Korenblit 
23134df6a075SMiri Korenblit 	if (low_th < 0 || high_th < 0)
23144df6a075SMiri Korenblit 		return -EINVAL;
23154df6a075SMiri Korenblit 
2316091296d3SMiri Korenblit 	/* Set the PPE thresholds accordingly */
2317091296d3SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2318091296d3SMiri Korenblit 		u8 bw;
2319091296d3SMiri Korenblit 
2320091296d3SMiri Korenblit 		for (bw = 0;
2321091296d3SMiri Korenblit 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2322091296d3SMiri Korenblit 			bw++) {
2323091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2324091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2325091296d3SMiri Korenblit 		}
2326091296d3SMiri Korenblit 	}
2327091296d3SMiri Korenblit 
23284df6a075SMiri Korenblit 	return 0;
2329091296d3SMiri Korenblit }
2330091296d3SMiri Korenblit 
iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 * pkt_ext,u8 nominal_padding)2331cb63eb43SMiri Korenblit static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2332cb63eb43SMiri Korenblit 					 u8 nominal_padding)
2333cb63eb43SMiri Korenblit {
2334cb63eb43SMiri Korenblit 	int i;
2335cb63eb43SMiri Korenblit 
2336cb63eb43SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2337cb63eb43SMiri Korenblit 		u8 bw;
2338cb63eb43SMiri Korenblit 
2339cb63eb43SMiri Korenblit 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2340cb63eb43SMiri Korenblit 		     bw++) {
2341cb63eb43SMiri Korenblit 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2342cb63eb43SMiri Korenblit 
2343cb63eb43SMiri Korenblit 			if (nominal_padding >
2344cb63eb43SMiri Korenblit 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2345cb63eb43SMiri Korenblit 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2346cb63eb43SMiri Korenblit 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2347cb63eb43SMiri Korenblit 			else if (nominal_padding ==
2348cb63eb43SMiri Korenblit 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2349cb63eb43SMiri Korenblit 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2350cb63eb43SMiri Korenblit 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2351cb63eb43SMiri Korenblit 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2352cb63eb43SMiri Korenblit 		}
2353cb63eb43SMiri Korenblit 	}
2354cb63eb43SMiri Korenblit }
2355cb63eb43SMiri Korenblit 
23564df6a075SMiri Korenblit /* Set the pkt_ext field according to PPE Thresholds element */
iwl_mvm_set_sta_pkt_ext(struct iwl_mvm * mvm,struct ieee80211_link_sta * link_sta,struct iwl_he_pkt_ext_v2 * pkt_ext)235757974a55SGregory Greenman int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
235857974a55SGregory Greenman 			    struct ieee80211_link_sta *link_sta,
23594df6a075SMiri Korenblit 			    struct iwl_he_pkt_ext_v2 *pkt_ext)
23604df6a075SMiri Korenblit {
23614df6a075SMiri Korenblit 	u8 nominal_padding;
23624df6a075SMiri Korenblit 	int i, ret = 0;
23634df6a075SMiri Korenblit 
236457974a55SGregory Greenman 	if (WARN_ON(!link_sta))
236557974a55SGregory Greenman 		return -EINVAL;
236657974a55SGregory Greenman 
23674df6a075SMiri Korenblit 	/* Initialize the PPE thresholds to "None" (7), as described in Table
23684df6a075SMiri Korenblit 	 * 9-262ac of 80211.ax/D3.0.
23694df6a075SMiri Korenblit 	 */
23704df6a075SMiri Korenblit 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
23714df6a075SMiri Korenblit 	       sizeof(struct iwl_he_pkt_ext_v2));
23724df6a075SMiri Korenblit 
237357974a55SGregory Greenman 	if (link_sta->eht_cap.has_eht) {
23744df6a075SMiri Korenblit 		nominal_padding =
237557974a55SGregory Greenman 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
23764df6a075SMiri Korenblit 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
23774df6a075SMiri Korenblit 
23784df6a075SMiri Korenblit 		/* If PPE Thresholds exists, parse them into a FW-familiar
23794df6a075SMiri Korenblit 		 * format.
23804df6a075SMiri Korenblit 		 */
238157974a55SGregory Greenman 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
23824df6a075SMiri Korenblit 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
238357974a55SGregory Greenman 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
23844df6a075SMiri Korenblit 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
238557974a55SGregory Greenman 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
23864df6a075SMiri Korenblit 			u8 ru_index_bitmap =
23874df6a075SMiri Korenblit 				u16_get_bits(*ppe,
23884df6a075SMiri Korenblit 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
23894df6a075SMiri Korenblit 			 /* Starting after PPE header */
23904df6a075SMiri Korenblit 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
23914df6a075SMiri Korenblit 
23924df6a075SMiri Korenblit 			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
23934df6a075SMiri Korenblit 					  ppe, ppe_pos_bit, true);
239457974a55SGregory Greenman 		/* EHT PPE Thresholds doesn't exist - set the API according to
239557974a55SGregory Greenman 		 * HE PPE Tresholds
23964df6a075SMiri Korenblit 		 */
239757974a55SGregory Greenman 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
23984df6a075SMiri Korenblit 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
23994df6a075SMiri Korenblit 			/* Even though HE Capabilities IE doesn't contain PPE
24004df6a075SMiri Korenblit 			 * Thresholds for BW 320Mhz, thresholds for this BW will
24014df6a075SMiri Korenblit 			 * be filled in with the same values as 160Mhz, due to
24024df6a075SMiri Korenblit 			 * the inheritance, as required.
24034df6a075SMiri Korenblit 			 */
240457974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
24054df6a075SMiri Korenblit 							true);
24064df6a075SMiri Korenblit 
24074df6a075SMiri Korenblit 			/* According to the requirements, for MCSs 12-13 the
24084df6a075SMiri Korenblit 			 * maximum value between HE PPE Threshold and Common
24094df6a075SMiri Korenblit 			 * Nominal Packet Padding needs to be taken
24104df6a075SMiri Korenblit 			 */
24114df6a075SMiri Korenblit 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
24124df6a075SMiri Korenblit 
24134df6a075SMiri Korenblit 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
24144df6a075SMiri Korenblit 		 * take the Thresholds from Common Nominal Packet Padding field
24154df6a075SMiri Korenblit 		 */
24164df6a075SMiri Korenblit 		} else {
24174df6a075SMiri Korenblit 			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
24184df6a075SMiri Korenblit 								       nominal_padding);
24194df6a075SMiri Korenblit 		}
242057974a55SGregory Greenman 	} else if (link_sta->he_cap.has_he) {
24214df6a075SMiri Korenblit 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
242257974a55SGregory Greenman 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
24234df6a075SMiri Korenblit 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
242457974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
24254df6a075SMiri Korenblit 							false);
24264df6a075SMiri Korenblit 		/* PPE Thresholds doesn't exist - set the API PPE values
24274df6a075SMiri Korenblit 		 * according to Common Nominal Packet Padding field.
24284df6a075SMiri Korenblit 		 */
24294df6a075SMiri Korenblit 		} else {
24304df6a075SMiri Korenblit 			nominal_padding =
243157974a55SGregory Greenman 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
24324df6a075SMiri Korenblit 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
24334df6a075SMiri Korenblit 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
24344df6a075SMiri Korenblit 				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
24354df6a075SMiri Korenblit 									       nominal_padding);
24364df6a075SMiri Korenblit 		}
24374df6a075SMiri Korenblit 	}
24384df6a075SMiri Korenblit 
24394df6a075SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
24404df6a075SMiri Korenblit 		int bw;
24414df6a075SMiri Korenblit 
24424df6a075SMiri Korenblit 		for (bw = 0;
24434df6a075SMiri Korenblit 		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
24444df6a075SMiri Korenblit 		     bw++) {
24454df6a075SMiri Korenblit 			u8 *qam_th =
24464df6a075SMiri Korenblit 				&pkt_ext->pkt_ext_qam_th[i][bw][0];
24474df6a075SMiri Korenblit 
24484df6a075SMiri Korenblit 			IWL_DEBUG_HT(mvm,
24494df6a075SMiri Korenblit 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
24504df6a075SMiri Korenblit 				     i, bw, qam_th[0], qam_th[1]);
24514df6a075SMiri Korenblit 		}
24524df6a075SMiri Korenblit 	}
24534df6a075SMiri Korenblit 	return ret;
24544df6a075SMiri Korenblit }
24554df6a075SMiri Korenblit 
24569c4f15caSMiri Korenblit /*
24579c4f15caSMiri Korenblit  * This function sets the MU EDCA parameters ans returns whether MU EDCA
24589c4f15caSMiri Korenblit  * is enabled or not
24599c4f15caSMiri Korenblit  */
iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm * mvm,const struct iwl_mvm_vif_link_info * link_info,struct iwl_he_backoff_conf * trig_based_txf)246055eb1c5fSMiri Korenblit bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2461e119e740SEmmanuel Grumbach 				   const struct iwl_mvm_vif_link_info *link_info,
246255eb1c5fSMiri Korenblit 				   struct iwl_he_backoff_conf *trig_based_txf)
24639c4f15caSMiri Korenblit {
24649c4f15caSMiri Korenblit 	int i;
24659c4f15caSMiri Korenblit 	/* Mark MU EDCA as enabled, unless none detected on some AC */
24669c4f15caSMiri Korenblit 	bool mu_edca_enabled = true;
24679c4f15caSMiri Korenblit 
24689c4f15caSMiri Korenblit 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2469e119e740SEmmanuel Grumbach 		const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2470e119e740SEmmanuel Grumbach 			&link_info->queue_params[i].mu_edca_param_rec;
24719c4f15caSMiri Korenblit 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
24729c4f15caSMiri Korenblit 
2473e119e740SEmmanuel Grumbach 		if (!link_info->queue_params[i].mu_edca) {
24749c4f15caSMiri Korenblit 			mu_edca_enabled = false;
24759c4f15caSMiri Korenblit 			break;
24769c4f15caSMiri Korenblit 		}
24779c4f15caSMiri Korenblit 
24789c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmin =
24799c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
24809c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmax =
24819c4f15caSMiri Korenblit 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
24829c4f15caSMiri Korenblit 		trig_based_txf[ac].aifsn =
24839c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->aifsn & 0xf);
24849c4f15caSMiri Korenblit 		trig_based_txf[ac].mu_time =
24859c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->mu_edca_timer);
24869c4f15caSMiri Korenblit 	}
24879c4f15caSMiri Korenblit 
24889c4f15caSMiri Korenblit 	return mu_edca_enabled;
24899c4f15caSMiri Korenblit }
24909c4f15caSMiri Korenblit 
iwl_mvm_is_nic_ack_enabled(struct iwl_mvm * mvm,struct ieee80211_vif * vif)24919be162a7SMiri Korenblit bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
24929c4f15caSMiri Korenblit {
24939c4f15caSMiri Korenblit 	const struct ieee80211_supported_band *sband;
24949c4f15caSMiri Korenblit 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
24959c4f15caSMiri Korenblit 
24969c4f15caSMiri Korenblit 	/* This capability is the same for all bands,
24979c4f15caSMiri Korenblit 	 * so take it from one of them.
24989c4f15caSMiri Korenblit 	 */
24999c4f15caSMiri Korenblit 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
25001ec7291eSJohannes Berg 	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
25019c4f15caSMiri Korenblit 
25029c4f15caSMiri Korenblit 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
25039c4f15caSMiri Korenblit 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
25049c4f15caSMiri Korenblit }
25059c4f15caSMiri Korenblit 
iwl_mvm_get_sta_htc_flags(struct ieee80211_sta * sta,struct ieee80211_link_sta * link_sta)250657974a55SGregory Greenman __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
250757974a55SGregory Greenman 				 struct ieee80211_link_sta *link_sta)
25084df6a075SMiri Korenblit {
250957974a55SGregory Greenman 	u8 *mac_cap_info =
251057974a55SGregory Greenman 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
25114df6a075SMiri Korenblit 	__le32 htc_flags = 0;
25124df6a075SMiri Korenblit 
25134df6a075SMiri Korenblit 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
25144df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
25154df6a075SMiri Korenblit 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
25164df6a075SMiri Korenblit 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
25174df6a075SMiri Korenblit 		u8 link_adap =
25184df6a075SMiri Korenblit 			((mac_cap_info[2] &
25194df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
25204df6a075SMiri Korenblit 			 (mac_cap_info[1] &
25214df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
25224df6a075SMiri Korenblit 
25234df6a075SMiri Korenblit 		if (link_adap == 2)
25244df6a075SMiri Korenblit 			htc_flags |=
25254df6a075SMiri Korenblit 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
25264df6a075SMiri Korenblit 		else if (link_adap == 3)
25274df6a075SMiri Korenblit 			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
25284df6a075SMiri Korenblit 	}
25294df6a075SMiri Korenblit 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
25304df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
25314df6a075SMiri Korenblit 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
25324df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
25334df6a075SMiri Korenblit 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
25344df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
25354df6a075SMiri Korenblit 
25364df6a075SMiri Korenblit 	return htc_flags;
25374df6a075SMiri Korenblit }
25384df6a075SMiri Korenblit 
iwl_mvm_cfg_he_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u8 sta_id)2539514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2540514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2541514c3069SLuca Coelho {
2542514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
254342506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2544514c3069SLuca Coelho 		.sta_id = sta_id,
2545514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2546dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2547514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2548514c3069SLuca Coelho 		.frame_time_rts_th =
2549514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2550514c3069SLuca Coelho 	};
255142506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
255242506dd2SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
255342506dd2SJohannes Berg 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
255442506dd2SJohannes Berg 	int size;
2555514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2556514c3069SLuca Coelho 	u32 flags;
2557514c3069SLuca Coelho 	int i;
255842506dd2SJohannes Berg 	void *cmd;
255942506dd2SJohannes Berg 
256042506dd2SJohannes Berg 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
256142506dd2SJohannes Berg 		ver = 1;
256242506dd2SJohannes Berg 
256342506dd2SJohannes Berg 	switch (ver) {
256442506dd2SJohannes Berg 	case 1:
256542506dd2SJohannes Berg 		/* same layout as v2 except some data at the end */
256642506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
256742506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
256842506dd2SJohannes Berg 		break;
256942506dd2SJohannes Berg 	case 2:
257042506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
257142506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
257242506dd2SJohannes Berg 		break;
257342506dd2SJohannes Berg 	case 3:
257442506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd;
257542506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
257642506dd2SJohannes Berg 		break;
257742506dd2SJohannes Berg 	default:
257842506dd2SJohannes Berg 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
257942506dd2SJohannes Berg 		return;
258042506dd2SJohannes Berg 	}
2581514c3069SLuca Coelho 
2582514c3069SLuca Coelho 	rcu_read_lock();
2583514c3069SLuca Coelho 
2584514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
258512d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2586514c3069SLuca Coelho 		rcu_read_unlock();
2587514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2588514c3069SLuca Coelho 		return;
2589514c3069SLuca Coelho 	}
2590514c3069SLuca Coelho 
2591046d2e7cSSriram R 	if (!sta->deflink.he_cap.has_he) {
2592514c3069SLuca Coelho 		rcu_read_unlock();
2593514c3069SLuca Coelho 		return;
2594514c3069SLuca Coelho 	}
2595514c3069SLuca Coelho 
2596514c3069SLuca Coelho 	flags = 0;
2597514c3069SLuca Coelho 
25984f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
2599650cadb7SGregory Greenman 	if (mvmvif->deflink.he_ru_2mhz_block)
26004f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
26014f58121dSIlan Peer 
2602514c3069SLuca Coelho 	/* HTC flags */
260357974a55SGregory Greenman 	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2604514c3069SLuca Coelho 
26054df6a075SMiri Korenblit 	/* PPE Thresholds */
260657974a55SGregory Greenman 	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2607cb63eb43SMiri Korenblit 		flags |= STA_CTXT_HE_PACKET_EXT;
260873f23d91SShaul Triebitz 
2609046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
261073f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
261173f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
261273f23d91SShaul Triebitz 
2613046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
261473f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
261573f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
261673f23d91SShaul Triebitz 
2617514c3069SLuca Coelho 	rcu_read_unlock();
2618514c3069SLuca Coelho 
2619e119e740SEmmanuel Grumbach 	if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
26209c4f15caSMiri Korenblit 					  &sta_ctxt_cmd.trig_based_txf[0]))
2621514c3069SLuca Coelho 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2622514c3069SLuca Coelho 
2623514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2624514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2625514c3069SLuca Coelho 
2626514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2627514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2628514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2629514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2630514c3069SLuca Coelho 	}
2631514c3069SLuca Coelho 
26329c4f15caSMiri Korenblit 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2633ee1a02d7SShaul Triebitz 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2634ee1a02d7SShaul Triebitz 
2635918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2636918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2637918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2638918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2639d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2640d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2641d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2642d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2643d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2644d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2645918cbf39SSara Sharon 	}
2646514c3069SLuca Coelho 
2647514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2648514c3069SLuca Coelho 
264942506dd2SJohannes Berg 	if (ver < 3) {
265042506dd2SJohannes Berg 		/* fields before pkt_ext */
265142506dd2SJohannes Berg 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
265242506dd2SJohannes Berg 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
265342506dd2SJohannes Berg 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
265442506dd2SJohannes Berg 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
265542506dd2SJohannes Berg 
265642506dd2SJohannes Berg 		/* pkt_ext */
265742506dd2SJohannes Berg 		for (i = 0;
265842506dd2SJohannes Berg 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
265942506dd2SJohannes Berg 		     i++) {
266042506dd2SJohannes Berg 			u8 bw;
266142506dd2SJohannes Berg 
266242506dd2SJohannes Berg 			for (bw = 0;
266342506dd2SJohannes Berg 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
266442506dd2SJohannes Berg 			     bw++) {
266542506dd2SJohannes Berg 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
266642506dd2SJohannes Berg 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
266742506dd2SJohannes Berg 
266842506dd2SJohannes Berg 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
266942506dd2SJohannes Berg 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
267042506dd2SJohannes Berg 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
267142506dd2SJohannes Berg 			}
267242506dd2SJohannes Berg 		}
267342506dd2SJohannes Berg 
267442506dd2SJohannes Berg 		/* fields after pkt_ext */
267542506dd2SJohannes Berg 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
267642506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
267742506dd2SJohannes Berg 			     sizeof(sta_ctxt_cmd_v2) -
267842506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
267942506dd2SJohannes Berg 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
268042506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
268142506dd2SJohannes Berg 		       (u8 *)&sta_ctxt_cmd +
268242506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
268342506dd2SJohannes Berg 		       sizeof(sta_ctxt_cmd) -
268442506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
268542506dd2SJohannes Berg 		sta_ctxt_cmd_v2.reserved3 = 0;
268642506dd2SJohannes Berg 	}
268742506dd2SJohannes Berg 
268842506dd2SJohannes Berg 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2689514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2690514c3069SLuca Coelho }
2691514c3069SLuca Coelho 
iwl_mvm_protect_assoc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration_override,unsigned int link_id)2692660eba5aSMiri Korenblit void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
269323673041SMiri Korenblit 			   u32 duration_override, unsigned int link_id)
2694af84ac57SJohannes Berg {
2695af84ac57SJohannes Berg 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2696af84ac57SJohannes Berg 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2697af84ac57SJohannes Berg 
2698af84ac57SJohannes Berg 	if (duration_override > duration)
2699af84ac57SJohannes Berg 		duration = duration_override;
2700af84ac57SJohannes Berg 
2701af84ac57SJohannes Berg 	/* Try really hard to protect the session and hear a beacon
2702af84ac57SJohannes Berg 	 * The new session protection command allows us to protect the
2703af84ac57SJohannes Berg 	 * session for a much longer time since the firmware will internally
2704af84ac57SJohannes Berg 	 * create two events: a 300TU one with a very high priority that
2705af84ac57SJohannes Berg 	 * won't be fragmented which should be enough for 99% of the cases,
2706af84ac57SJohannes Berg 	 * and another one (which we configure here to be 900TU long) which
2707af84ac57SJohannes Berg 	 * will have a slightly lower priority, but more importantly, can be
2708af84ac57SJohannes Berg 	 * fragmented so that it'll allow other activities to run.
2709af84ac57SJohannes Berg 	 */
2710af84ac57SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
2711af84ac57SJohannes Berg 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2712af84ac57SJohannes Berg 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
271323673041SMiri Korenblit 						    min_duration, false,
271423673041SMiri Korenblit 						    link_id);
2715af84ac57SJohannes Berg 	else
2716af84ac57SJohannes Berg 		iwl_mvm_protect_session(mvm, vif, duration,
2717af84ac57SJohannes Berg 					min_duration, 500, false);
2718af84ac57SJohannes Berg }
2719af84ac57SJohannes Berg 
2720660eba5aSMiri Korenblit /* Handle association common part to MLD and non-MLD modes */
iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u64 changes)2721660eba5aSMiri Korenblit void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2722660eba5aSMiri Korenblit 					    struct ieee80211_vif *vif,
2723660eba5aSMiri Korenblit 					    u64 changes)
2724660eba5aSMiri Korenblit {
2725660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2726660eba5aSMiri Korenblit 	int ret;
2727c1e458b9SIlan Peer 	int link_id;
2728660eba5aSMiri Korenblit 
2729660eba5aSMiri Korenblit 	/* The firmware tracks the MU-MIMO group on its own.
2730660eba5aSMiri Korenblit 	 * However, on HW restart we should restore this data.
2731660eba5aSMiri Korenblit 	 */
2732660eba5aSMiri Korenblit 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2733660eba5aSMiri Korenblit 	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2734660eba5aSMiri Korenblit 		ret = iwl_mvm_update_mu_groups(mvm, vif);
2735660eba5aSMiri Korenblit 		if (ret)
2736660eba5aSMiri Korenblit 			IWL_ERR(mvm,
2737660eba5aSMiri Korenblit 				"failed to update VHT MU_MIMO groups\n");
2738660eba5aSMiri Korenblit 	}
2739660eba5aSMiri Korenblit 
2740660eba5aSMiri Korenblit 	iwl_mvm_recalc_multicast(mvm);
2741660eba5aSMiri Korenblit 
2742660eba5aSMiri Korenblit 	/* reset rssi values */
2743c1e458b9SIlan Peer 	for_each_mvm_vif_valid_link(mvmvif, link_id)
2744c1e458b9SIlan Peer 		mvmvif->link[link_id]->bf_data.ave_beacon_signal = 0;
2745660eba5aSMiri Korenblit 
2746660eba5aSMiri Korenblit 	iwl_mvm_bt_coex_vif_change(mvm);
27471a3e7039SGregory Greenman 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2748660eba5aSMiri Korenblit 					    IEEE80211_SMPS_AUTOMATIC);
2749660eba5aSMiri Korenblit 	if (fw_has_capa(&mvm->fw->ucode_capa,
2750660eba5aSMiri Korenblit 			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2751660eba5aSMiri Korenblit 		iwl_mvm_config_scan(mvm);
2752660eba5aSMiri Korenblit }
2753660eba5aSMiri Korenblit 
2754660eba5aSMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
27551a3e7039SGregory Greenman void
iwl_mvm_bss_info_changed_station_common(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,u64 changes)27561a3e7039SGregory Greenman iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2757660eba5aSMiri Korenblit 					struct ieee80211_vif *vif,
27581a3e7039SGregory Greenman 					struct ieee80211_bss_conf *link_conf,
2759660eba5aSMiri Korenblit 					u64 changes)
2760660eba5aSMiri Korenblit {
2761660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2762660eba5aSMiri Korenblit 	int ret;
2763660eba5aSMiri Korenblit 
2764660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BEACON_INFO) {
2765660eba5aSMiri Korenblit 		/* We received a beacon from the associated AP so
2766660eba5aSMiri Korenblit 		 * remove the session protection.
2767660eba5aSMiri Korenblit 		 */
2768660eba5aSMiri Korenblit 		iwl_mvm_stop_session_protection(mvm, vif);
2769660eba5aSMiri Korenblit 
2770660eba5aSMiri Korenblit 		iwl_mvm_sf_update(mvm, vif, false);
277187f5b5f2SJohannes Berg 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
2772660eba5aSMiri Korenblit 	}
2773660eba5aSMiri Korenblit 
2774660eba5aSMiri Korenblit 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2775660eba5aSMiri Korenblit 		       /* Send power command on every beacon change,
2776660eba5aSMiri Korenblit 			* because we may have not enabled beacon abort yet.
2777660eba5aSMiri Korenblit 			*/
2778660eba5aSMiri Korenblit 		       BSS_CHANGED_BEACON_INFO)) {
2779660eba5aSMiri Korenblit 		ret = iwl_mvm_power_update_mac(mvm);
2780660eba5aSMiri Korenblit 		if (ret)
2781660eba5aSMiri Korenblit 			IWL_ERR(mvm, "failed to update power mode\n");
2782660eba5aSMiri Korenblit 	}
2783660eba5aSMiri Korenblit 
2784660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_CQM) {
2785c1e458b9SIlan Peer 		struct iwl_mvm_vif_link_info *link_info =
2786c1e458b9SIlan Peer 			mvmvif->link[link_conf->link_id];
2787c1e458b9SIlan Peer 
2788c1e458b9SIlan Peer 		IWL_DEBUG_MAC80211(mvm, "CQM info_changed\n");
2789c1e458b9SIlan Peer 		if (link_info)
2790c1e458b9SIlan Peer 			link_info->bf_data.last_cqm_event = 0;
2791c1e458b9SIlan Peer 
2792c1e458b9SIlan Peer 		if (mvmvif->bf_enabled) {
279322c58834SGregory Greenman 			/* FIXME: need to update per link when FW API will
279422c58834SGregory Greenman 			 * support it
279522c58834SGregory Greenman 			 */
279687f5b5f2SJohannes Berg 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
2797660eba5aSMiri Korenblit 			if (ret)
2798660eba5aSMiri Korenblit 				IWL_ERR(mvm,
2799660eba5aSMiri Korenblit 					"failed to update CQM thresholds\n");
2800660eba5aSMiri Korenblit 		}
2801660eba5aSMiri Korenblit 	}
2802660eba5aSMiri Korenblit 
2803660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BANDWIDTH)
28041a3e7039SGregory Greenman 		iwl_mvm_update_link_smps(vif, link_conf);
28056b82f4e1SJohannes Berg 
28066b82f4e1SJohannes Berg 	if (changes & BSS_CHANGED_TPE) {
28076b82f4e1SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TPE\n");
28086b82f4e1SJohannes Berg 		iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif,
28096b82f4e1SJohannes Berg 							link_conf,
28106b82f4e1SJohannes Berg 							false);
28116b82f4e1SJohannes Berg 	}
2812660eba5aSMiri Korenblit }
2813660eba5aSMiri Korenblit 
iwl_mvm_bss_info_changed_station(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u64 changes)2814e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2815e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2816e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
28177b7090b4SJohannes Berg 					     u64 changes)
2818e705c121SKalle Valo {
2819e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2820e705c121SKalle Valo 	int ret;
2821b6e3d1baSAnjaneyulu 	int i;
2822e705c121SKalle Valo 
2823e705c121SKalle Valo 	/*
2824cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend
2825cdaba917SEmmanuel Grumbach 	 * on the beacon interval, which was not known when the station
2826cdaba917SEmmanuel Grumbach 	 * interface was added.
2827e705c121SKalle Valo 	 */
2828f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2829cb63eb43SMiri Korenblit 		if ((vif->bss_conf.he_support &&
283083f57c93SJohannes Berg 		     !iwlwifi_mod_params.disable_11ax))
2831650cadb7SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2832514c3069SLuca Coelho 
2833e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2834514c3069SLuca Coelho 	}
2835e705c121SKalle Valo 
283640ecdd01SShaul Triebitz 	/* Update MU EDCA params */
283740ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2838cb63eb43SMiri Korenblit 	    vif->cfg.assoc &&
283983f57c93SJohannes Berg 	    (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax))
2840650cadb7SGregory Greenman 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
284140ecdd01SShaul Triebitz 
2842e705c121SKalle Valo 	/*
2843e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2844e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2845e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2846e705c121SKalle Valo 	 * branch for disassociation below.
2847e705c121SKalle Valo 	 */
2848e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2849650cadb7SGregory Greenman 		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2850e705c121SKalle Valo 
2851650cadb7SGregory Greenman 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2852e705c121SKalle Valo 	if (ret)
2853e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2854e705c121SKalle Valo 
2855e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2856650cadb7SGregory Greenman 	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2857f276e20bSJohannes Berg 	mvmvif->associated = vif->cfg.assoc;
2858e705c121SKalle Valo 
2859e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2860f276e20bSJohannes Berg 		if (vif->cfg.assoc) {
28618c626172SJohannes Berg 			mvmvif->session_prot_connection_loss = false;
28628c626172SJohannes Berg 
2863e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2864e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2865b6e3d1baSAnjaneyulu 			for_each_mvm_vif_valid_link(mvmvif, i)
2866b6e3d1baSAnjaneyulu 				memset(&mvmvif->link[i]->beacon_stats, 0,
2867b6e3d1baSAnjaneyulu 				       sizeof(mvmvif->link[i]->beacon_stats));
2868e705c121SKalle Valo 
2869e705c121SKalle Valo 			/* add quota for this interface */
2870e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2871e705c121SKalle Valo 			if (ret) {
2872e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2873e705c121SKalle Valo 				return;
2874e705c121SKalle Valo 			}
2875e705c121SKalle Valo 
2876e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2877fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2878fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2879fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2880e705c121SKalle Valo 				/*
2881e705c121SKalle Valo 				 * If we're restarting then the firmware will
2882e705c121SKalle Valo 				 * obviously have lost synchronisation with
2883e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2884e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2885e705c121SKalle Valo 				 * scheduling a session protection time event.
2886e705c121SKalle Valo 				 *
2887e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2888e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2889e705c121SKalle Valo 				 * the beacon interval.
2890e705c121SKalle Valo 				 *
2891e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2892e705c121SKalle Valo 				 * than a single interface.
2893fe959c7bSEmmanuel Grumbach 				 *
2894fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2895fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2896fe959c7bSEmmanuel Grumbach 				 * only anyway.
2897e705c121SKalle Valo 				 */
2898e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2899e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2900e705c121SKalle Valo 							5 * dur, false);
2901af84ac57SJohannes Berg 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2902af84ac57SJohannes Berg 					     &mvm->status) &&
2903af84ac57SJohannes Berg 				   !vif->bss_conf.dtim_period) {
2904af84ac57SJohannes Berg 				/*
2905af84ac57SJohannes Berg 				 * If we're not restarting and still haven't
2906af84ac57SJohannes Berg 				 * heard a beacon (dtim period unknown) then
2907af84ac57SJohannes Berg 				 * make sure we still have enough minimum time
2908af84ac57SJohannes Berg 				 * remaining in the time event, since the auth
2909af84ac57SJohannes Berg 				 * might actually have taken quite a while
2910af84ac57SJohannes Berg 				 * (especially for SAE) and so the remaining
2911af84ac57SJohannes Berg 				 * time could be small without us having heard
2912af84ac57SJohannes Berg 				 * a beacon yet.
2913af84ac57SJohannes Berg 				 */
291423673041SMiri Korenblit 				iwl_mvm_protect_assoc(mvm, vif, 0, 0);
2915e705c121SKalle Valo 			}
2916e705c121SKalle Valo 
2917e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2918e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2919e705c121SKalle Valo 			if (vif->p2p) {
2920e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2921e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
29221a3e7039SGregory Greenman 						    IEEE80211_SMPS_DYNAMIC, 0);
2923e705c121SKalle Valo 			}
2924650cadb7SGregory Greenman 		} else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
29256d19a5ebSEmmanuel Grumbach 			iwl_mvm_mei_host_disassociated(mvm);
2926e705c121SKalle Valo 			/*
2927e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2928e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2929e705c121SKalle Valo 			 */
293069e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
293169e508b4SIlan Peer 			WARN_ONCE(ret &&
293269e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
293369e508b4SIlan Peer 					    &mvm->status),
2934e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2935e705c121SKalle Valo 
29366b28f978SEmmanuel Grumbach 			/*
29376b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
29386b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
29396b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
29406b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
29416b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
29426b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
29436b28f978SEmmanuel Grumbach 			 */
29446b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
29456b28f978SEmmanuel Grumbach 				      &mvm->status)) {
29465c75a208SJohannes Berg 				/* first remove remaining keys */
2947ba9eef6bSGregory Greenman 				iwl_mvm_sec_key_remove_ap(mvm, vif,
2948072573f6SJohannes Berg 							  &mvmvif->deflink, 0);
29495c75a208SJohannes Berg 
29506b28f978SEmmanuel Grumbach 				/*
29516b28f978SEmmanuel Grumbach 				 * Remove AP station now that
29526b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
29536b28f978SEmmanuel Grumbach 				 */
29546b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
2955650cadb7SGregory Greenman 							mvmvif->deflink.ap_sta_id);
2956e705c121SKalle Valo 				if (ret)
29576b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
29586b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2959e705c121SKalle Valo 
2960650cadb7SGregory Greenman 				mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
29616b28f978SEmmanuel Grumbach 			}
29626b28f978SEmmanuel Grumbach 
2963e705c121SKalle Valo 			/* remove quota for this interface */
2964e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2965e705c121SKalle Valo 			if (ret)
2966e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2967e705c121SKalle Valo 
2968e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2969e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2970e705c121SKalle Valo 			if (ret)
2971e705c121SKalle Valo 				IWL_ERR(mvm,
2972e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2973e705c121SKalle Valo 					vif->addr);
2974e705c121SKalle Valo 		}
2975e705c121SKalle Valo 
2976660eba5aSMiri Korenblit 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
2977a07a8f37SSara Sharon 	}
2978a07a8f37SSara Sharon 
29791a3e7039SGregory Greenman 	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
29801a3e7039SGregory Greenman 						changes);
2981b45242c9SAvraham Stern }
2982b45242c9SAvraham Stern 
iwl_mvm_start_ap_ibss_common(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int * ret)2983f947b62cSMiri Korenblit bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2984f947b62cSMiri Korenblit 				  struct ieee80211_vif *vif,
2985f947b62cSMiri Korenblit 				  int *ret)
2986f947b62cSMiri Korenblit {
2987f947b62cSMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2988f947b62cSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2989f947b62cSMiri Korenblit 	int i;
2990e705c121SKalle Valo 
2991f947b62cSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
2992f947b62cSMiri Korenblit 
2993f947b62cSMiri Korenblit 	mvmvif->ap_assoc_sta_count = 0;
2994f947b62cSMiri Korenblit 
2995f947b62cSMiri Korenblit 	/* must be set before quota calculations */
2996f947b62cSMiri Korenblit 	mvmvif->ap_ibss_active = true;
2997f947b62cSMiri Korenblit 
2998f947b62cSMiri Korenblit 	/* send all the early keys to the device now */
2999f947b62cSMiri Korenblit 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3000f947b62cSMiri Korenblit 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
3001f947b62cSMiri Korenblit 
3002f947b62cSMiri Korenblit 		if (!key)
3003f947b62cSMiri Korenblit 			continue;
3004f947b62cSMiri Korenblit 
3005f947b62cSMiri Korenblit 		mvmvif->ap_early_keys[i] = NULL;
3006f947b62cSMiri Korenblit 
3007f947b62cSMiri Korenblit 		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
3008f947b62cSMiri Korenblit 		if (*ret)
3009f947b62cSMiri Korenblit 			return true;
3010e705c121SKalle Valo 	}
3011e705c121SKalle Valo 
3012f947b62cSMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
3013f947b62cSMiri Korenblit 		iwl_mvm_vif_set_low_latency(mvmvif, true,
3014f947b62cSMiri Korenblit 					    LOW_LATENCY_VIF_TYPE);
3015f947b62cSMiri Korenblit 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
3016e705c121SKalle Valo 	}
3017e705c121SKalle Valo 
3018f947b62cSMiri Korenblit 	/* power updated needs to be done before quotas */
3019f947b62cSMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
3020e705c121SKalle Valo 
3021f947b62cSMiri Korenblit 	return false;
3022e705c121SKalle Valo }
3023e705c121SKalle Valo 
iwl_mvm_start_ap_ibss(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)3024e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
3025ae7ba17bSShaul Triebitz 				 struct ieee80211_vif *vif,
3026b327c84cSGregory Greenman 				 struct ieee80211_bss_conf *link_conf)
3027e705c121SKalle Valo {
3028e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3029e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3030f947b62cSMiri Korenblit 	int ret;
3031e705c121SKalle Valo 
3032e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3033e705c121SKalle Valo 
3034e705c121SKalle Valo 	/*
3035cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend on
3036cdaba917SEmmanuel Grumbach 	 * the beacon interval, which was not known when the AP interface
3037cdaba917SEmmanuel Grumbach 	 * was added.
3038e705c121SKalle Valo 	 */
3039e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
3040e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
3041e705c121SKalle Valo 
304236cf5377SGregory Greenman 	/* For older devices need to send beacon template before adding mac
304336cf5377SGregory Greenman 	 * context. For the newer, the beacon is a resource that belongs to a
304436cf5377SGregory Greenman 	 * MAC, so need to send beacon template after adding the mac.
304536cf5377SGregory Greenman 	 */
304636cf5377SGregory Greenman 	if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
3047e705c121SKalle Valo 		/* Add the mac context */
3048e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3049e705c121SKalle Valo 		if (ret)
3050e705c121SKalle Valo 			goto out_unlock;
3051e705c121SKalle Valo 
305236cf5377SGregory Greenman 		/* Send the beacon template */
305336cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
305436cf5377SGregory Greenman 		if (ret)
305536cf5377SGregory Greenman 			goto out_unlock;
305636cf5377SGregory Greenman 	} else {
305736cf5377SGregory Greenman 		/* Send the beacon template */
305836cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
305936cf5377SGregory Greenman 		if (ret)
306036cf5377SGregory Greenman 			goto out_unlock;
3061e705c121SKalle Valo 
3062e705c121SKalle Valo 		/* Add the mac context */
3063e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3064e705c121SKalle Valo 		if (ret)
3065e705c121SKalle Valo 			goto out_unlock;
306636cf5377SGregory Greenman 	}
3067e705c121SKalle Valo 
3068e705c121SKalle Valo 	/* Perform the binding */
3069e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
3070e705c121SKalle Valo 	if (ret)
3071e705c121SKalle Valo 		goto out_remove;
3072e705c121SKalle Valo 
307363dd5d02SSara Sharon 	/*
307463dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
307563dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
307663dd5d02SSara Sharon 	 * cause assert 0x2b00.
307763dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
307863dd5d02SSara Sharon 	 * the TLV
307963dd5d02SSara Sharon 	 */
308063dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
3081ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
3082ced19f26SSara Sharon 		if (ret)
3083ced19f26SSara Sharon 			goto out_unbind;
308463dd5d02SSara Sharon 		/*
308563dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
308663dd5d02SSara Sharon 		 * events are added and applied to the scheduler
308763dd5d02SSara Sharon 		 */
3088e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
308963dd5d02SSara Sharon 		if (ret) {
309063dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
309163dd5d02SSara Sharon 			goto out_unbind;
309263dd5d02SSara Sharon 		}
309363dd5d02SSara Sharon 	} else {
309463dd5d02SSara Sharon 		/*
309563dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
309663dd5d02SSara Sharon 		 * events are added and applied to the scheduler
309763dd5d02SSara Sharon 		 */
309875fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3099e705c121SKalle Valo 		if (ret)
310063dd5d02SSara Sharon 			goto out_unbind;
310175fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
310263dd5d02SSara Sharon 		if (ret) {
310363dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
310463dd5d02SSara Sharon 			goto out_unbind;
310563dd5d02SSara Sharon 		}
310663dd5d02SSara Sharon 	}
310726d6c16bSSara Sharon 
3108f947b62cSMiri Korenblit 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
3109f947b62cSMiri Korenblit 		goto out_failed;
3110e705c121SKalle Valo 
3111e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
3112e705c121SKalle Valo 	if (ret)
3113f947b62cSMiri Korenblit 		goto out_failed;
3114e705c121SKalle Valo 
3115e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3116e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
3117e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3118e705c121SKalle Valo 
3119e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
3120e705c121SKalle Valo 
3121e705c121SKalle Valo 	/* we don't support TDLS during DCM */
3122e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3123e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
3124e705c121SKalle Valo 
3125fd940de7SAvraham Stern 	iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
3126b73f9a4aSJohannes Berg 
3127e705c121SKalle Valo 	goto out_unlock;
3128e705c121SKalle Valo 
3129f947b62cSMiri Korenblit out_failed:
3130e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3131e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
3132e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3133ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
3134e705c121SKalle Valo out_unbind:
3135e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
3136e705c121SKalle Valo out_remove:
3137e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3138e705c121SKalle Valo out_unlock:
3139e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3140e705c121SKalle Valo 	return ret;
3141e705c121SKalle Valo }
3142e705c121SKalle Valo 
iwl_mvm_start_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)3143ae7ba17bSShaul Triebitz static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
3144ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
3145b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
3146ae7ba17bSShaul Triebitz {
3147b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
3148ae7ba17bSShaul Triebitz }
3149ae7ba17bSShaul Triebitz 
iwl_mvm_start_ibss(struct ieee80211_hw * hw,struct ieee80211_vif * vif)3150ae7ba17bSShaul Triebitz static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
3151e705c121SKalle Valo 			      struct ieee80211_vif *vif)
3152e705c121SKalle Valo {
3153b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
3154ae7ba17bSShaul Triebitz }
3155ae7ba17bSShaul Triebitz 
3156fd1a54c1SMiri Korenblit /* Common part for MLD and non-MLD ops */
iwl_mvm_stop_ap_ibss_common(struct iwl_mvm * mvm,struct ieee80211_vif * vif)3157fd1a54c1SMiri Korenblit void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
3158fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif)
3159ae7ba17bSShaul Triebitz {
3160e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3161e705c121SKalle Valo 
3162fd1a54c1SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
3163e705c121SKalle Valo 
3164fd1a54c1SMiri Korenblit 	iwl_mvm_prepare_mac_removal(mvm, vif);
3165e705c121SKalle Valo 
3166e705c121SKalle Valo 	/* Handle AP stop while in CSA */
3167e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
3168e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
3169e705c121SKalle Valo 					  &mvmvif->time_event_data);
3170e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
3171e705c121SKalle Valo 		mvmvif->csa_countdown = false;
3172e705c121SKalle Valo 	}
3173e705c121SKalle Valo 
3174e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
3175e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
3176e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
3177e705c121SKalle Valo 	}
3178e705c121SKalle Valo 
3179e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
3180e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
3181e705c121SKalle Valo 
318247242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
318347242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
318447242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
318547242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
318647242744STova Mussai 	}
318747242744STova Mussai 
3188e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
3189fd1a54c1SMiri Korenblit }
3190fd1a54c1SMiri Korenblit 
iwl_mvm_stop_ap_ibss(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)3191fd1a54c1SMiri Korenblit static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
3192fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif,
3193fd1a54c1SMiri Korenblit 				 struct ieee80211_bss_conf *link_conf)
3194fd1a54c1SMiri Korenblit {
3195fd1a54c1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3196fd1a54c1SMiri Korenblit 
31973f312651SJohannes Berg 	guard(mvm)(mvm);
3198fd1a54c1SMiri Korenblit 
3199fd1a54c1SMiri Korenblit 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
3200e705c121SKalle Valo 
3201e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3202e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
3203e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3204e705c121SKalle Valo 
3205e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
3206ced19f26SSara Sharon 
3207be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
3208be82ecd3SAvraham Stern 
3209ced19f26SSara Sharon 	/*
3210ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
3211ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
3212ced19f26SSara Sharon 	 * cause assert 0x2b00.
3213ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
3214ced19f26SSara Sharon 	 * bcast station).
3215ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
3216ced19f26SSara Sharon 	 */
3217ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
321826d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
3219e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3220ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3221ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
3222e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
3223e705c121SKalle Valo 
3224e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3225e705c121SKalle Valo 
3226e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3227e705c121SKalle Valo }
3228e705c121SKalle Valo 
iwl_mvm_stop_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)3229ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
3230ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
3231b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
3232ae7ba17bSShaul Triebitz {
3233b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
3234ae7ba17bSShaul Triebitz }
3235ae7ba17bSShaul Triebitz 
iwl_mvm_stop_ibss(struct ieee80211_hw * hw,struct ieee80211_vif * vif)3236ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
3237ae7ba17bSShaul Triebitz 			      struct ieee80211_vif *vif)
3238ae7ba17bSShaul Triebitz {
3239b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3240ae7ba17bSShaul Triebitz }
3241ae7ba17bSShaul Triebitz 
3242e705c121SKalle Valo static void
iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u64 changes)3243e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3244e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3245e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
32467b7090b4SJohannes Berg 				 u64 changes)
3247e705c121SKalle Valo {
3248e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3249e705c121SKalle Valo 
3250e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
3251e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
3252e705c121SKalle Valo 		return;
3253e705c121SKalle Valo 
3254e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3255e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3256e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3257e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3258e705c121SKalle Valo 
3259e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
3260e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
326136cf5377SGregory Greenman 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3262e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
3263e705c121SKalle Valo 
3264b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3265fd940de7SAvraham Stern 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3266b73f9a4aSJohannes Berg 
3267b73f9a4aSJohannes Berg 		if (ret)
3268b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3269b73f9a4aSJohannes Berg 				 ret);
3270b73f9a4aSJohannes Berg 	}
3271b73f9a4aSJohannes Berg 
3272e705c121SKalle Valo }
3273e705c121SKalle Valo 
iwl_mvm_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u64 changes)3274e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3275e705c121SKalle Valo 				     struct ieee80211_vif *vif,
3276e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
32777b7090b4SJohannes Berg 				     u64 changes)
3278e705c121SKalle Valo {
3279e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3280e705c121SKalle Valo 
32813f312651SJohannes Berg 	guard(mvm)(mvm);
3282e705c121SKalle Valo 
3283f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3284e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3285e705c121SKalle Valo 
3286e705c121SKalle Valo 	switch (vif->type) {
3287e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3288df7e3098SMiri Korenblit 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
3289e705c121SKalle Valo 		break;
3290e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3291e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
3292df7e3098SMiri Korenblit 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
3293e705c121SKalle Valo 		break;
329491b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
329591b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
329691b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
329791b08c2dSAviya Erenfeld 		break;
3298e705c121SKalle Valo 	default:
3299e705c121SKalle Valo 		/* shouldn't happen */
3300e705c121SKalle Valo 		WARN_ON_ONCE(1);
3301e705c121SKalle Valo 	}
3302e705c121SKalle Valo 
33039aae43a4SJohannes Berg 	if (changes & BSS_CHANGED_TXPOWER) {
33049aae43a4SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
33059aae43a4SJohannes Berg 				bss_conf->txpower);
33069aae43a4SJohannes Berg 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
33079aae43a4SJohannes Berg 	}
3308e705c121SKalle Valo }
3309e705c121SKalle Valo 
iwl_mvm_mac_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)3310cbce62a3SMiri Korenblit int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3311e705c121SKalle Valo 			struct ieee80211_scan_request *hw_req)
3312e705c121SKalle Valo {
3313e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3314e705c121SKalle Valo 
3315e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
3316e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3317e705c121SKalle Valo 		return -EINVAL;
3318e705c121SKalle Valo 
33193f312651SJohannes Berg 	guard(mvm)(mvm);
33203f312651SJohannes Berg 	return iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3321e705c121SKalle Valo }
3322e705c121SKalle Valo 
iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)3323cbce62a3SMiri Korenblit void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3324e705c121SKalle Valo 				struct ieee80211_vif *vif)
3325e705c121SKalle Valo {
3326e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3327e705c121SKalle Valo 
33283f312651SJohannes Berg 	guard(mvm)(mvm);
3329e705c121SKalle Valo 
3330e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3331e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
3332e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
3333e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
3334e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
3335e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
3336e705c121SKalle Valo 	*/
3337e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3338e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3339e705c121SKalle Valo }
3340e705c121SKalle Valo 
3341cbce62a3SMiri Korenblit void
iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tids,int num_frames,enum ieee80211_frame_release_type reason,bool more_data)3342e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3343e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
3344e705c121SKalle Valo 				  int num_frames,
3345e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
3346e705c121SKalle Valo 				  bool more_data)
3347e705c121SKalle Valo {
3348e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3349e705c121SKalle Valo 
3350e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3351e705c121SKalle Valo 
3352e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3353e705c121SKalle Valo 					  tids, more_data, false);
3354e705c121SKalle Valo }
3355e705c121SKalle Valo 
3356cbce62a3SMiri Korenblit void
iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tids,int num_frames,enum ieee80211_frame_release_type reason,bool more_data)3357e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3358e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
3359e705c121SKalle Valo 				    int num_frames,
3360e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
3361e705c121SKalle Valo 				    bool more_data)
3362e705c121SKalle Valo {
3363e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3364e705c121SKalle Valo 
33659a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3366e705c121SKalle Valo 
3367e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3368e705c121SKalle Valo 					  tids, more_data, true);
3369e705c121SKalle Valo }
3370e705c121SKalle Valo 
__iwl_mvm_mac_sta_notify(struct ieee80211_hw * hw,enum sta_notify_cmd cmd,struct ieee80211_sta * sta)337165e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3372e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
3373e705c121SKalle Valo 				     struct ieee80211_sta *sta)
3374e705c121SKalle Valo {
3375e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3376e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3377e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
3378e705c121SKalle Valo 	int tid;
3379e705c121SKalle Valo 
3380960f864bSJohannes Berg 	/*
3381960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
3382960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
3383960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
3384960f864bSJohannes Berg 	 */
3385960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3386960f864bSJohannes Berg 		return;
3387960f864bSJohannes Berg 
3388e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3389311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3390e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3391e705c121SKalle Valo 
33926862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
33931c17627bSSara Sharon 			continue;
33941c17627bSSara Sharon 
3395e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
3396e705c121SKalle Valo 
3397dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3398e705c121SKalle Valo 			continue;
3399e705c121SKalle Valo 
3400e705c121SKalle Valo 		__set_bit(tid, &tids);
3401e705c121SKalle Valo 	}
3402e705c121SKalle Valo 
3403e705c121SKalle Valo 	switch (cmd) {
3404e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
3405e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3406e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
3407e705c121SKalle Valo 
3408e705c121SKalle Valo 		if (txqs)
3409e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3410e705c121SKalle Valo 		/*
3411e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3412e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
3413e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3414e705c121SKalle Valo 		 */
3415e705c121SKalle Valo 		break;
3416e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
3417c8ee33e1SGregory Greenman 		if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
3418e705c121SKalle Valo 			break;
3419e705c121SKalle Valo 
3420e705c121SKalle Valo 		if (txqs)
3421e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3422e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3423e705c121SKalle Valo 		break;
3424e705c121SKalle Valo 	default:
3425e705c121SKalle Valo 		break;
3426e705c121SKalle Valo 	}
3427e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3428e705c121SKalle Valo }
3429e705c121SKalle Valo 
iwl_mvm_mac_sta_notify(struct ieee80211_hw * hw,struct ieee80211_vif * vif,enum sta_notify_cmd cmd,struct ieee80211_sta * sta)3430cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3431cbce62a3SMiri Korenblit 			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
343265e25482SJohannes Berg {
343365e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
343465e25482SJohannes Berg }
343565e25482SJohannes Berg 
iwl_mvm_sta_pm_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)343665e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
343765e25482SJohannes Berg {
343865e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
343965e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
344065e25482SJohannes Berg 	struct ieee80211_sta *sta;
344165e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
344265e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
344365e25482SJohannes Berg 
3444be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
344565e25482SJohannes Berg 		return;
344665e25482SJohannes Berg 
344765e25482SJohannes Berg 	rcu_read_lock();
3448a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
344965e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
345065e25482SJohannes Berg 		rcu_read_unlock();
345165e25482SJohannes Berg 		return;
345265e25482SJohannes Berg 	}
345365e25482SJohannes Berg 
345465e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
345565e25482SJohannes Berg 
345665e25482SJohannes Berg 	if (!mvmsta->vif ||
345765e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
345865e25482SJohannes Berg 		rcu_read_unlock();
345965e25482SJohannes Berg 		return;
346065e25482SJohannes Berg 	}
346165e25482SJohannes Berg 
346265e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
346365e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
346465e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
346565e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
346665e25482SJohannes Berg 			sta);
346765e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
346865e25482SJohannes Berg 	}
346965e25482SJohannes Berg 
347065e25482SJohannes Berg 	if (sleeping) {
347165e25482SJohannes Berg 		switch (notif->type) {
347265e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
347365e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
347465e25482SJohannes Berg 			break;
347565e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
347665e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
347765e25482SJohannes Berg 			break;
347865e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
347965e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
348065e25482SJohannes Berg 			break;
348165e25482SJohannes Berg 		default:
348265e25482SJohannes Berg 			break;
348365e25482SJohannes Berg 		}
348465e25482SJohannes Berg 	}
348565e25482SJohannes Berg 
348665e25482SJohannes Berg 	rcu_read_unlock();
348765e25482SJohannes Berg }
348865e25482SJohannes Berg 
iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3489cbce62a3SMiri Korenblit void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3490e705c121SKalle Valo 				struct ieee80211_vif *vif,
3491e705c121SKalle Valo 				struct ieee80211_sta *sta)
3492e705c121SKalle Valo {
3493e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3494e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
349579faae3aSGregory Greenman 	unsigned int link_id;
3496e705c121SKalle Valo 
3497e705c121SKalle Valo 	/*
3498e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
3499e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
3500e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
3501e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
3502e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
3503e705c121SKalle Valo 	 * callback deleted the station.
350479faae3aSGregory Greenman 	 * Since there's mvm->mutex here, no need to have RCU lock for
350579faae3aSGregory Greenman 	 * mvm_sta->link access.
3506e705c121SKalle Valo 	 */
35073f312651SJohannes Berg 	guard(mvm)(mvm);
350879faae3aSGregory Greenman 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
350979faae3aSGregory Greenman 		struct iwl_mvm_link_sta *link_sta;
351079faae3aSGregory Greenman 		u32 sta_id;
3511e705c121SKalle Valo 
351279faae3aSGregory Greenman 		if (!mvm_sta->link[link_id])
351379faae3aSGregory Greenman 			continue;
351479faae3aSGregory Greenman 
351579faae3aSGregory Greenman 		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
351679faae3aSGregory Greenman 						     lockdep_is_held(&mvm->mutex));
351779faae3aSGregory Greenman 		sta_id = link_sta->sta_id;
3518b8a85a1dSJohannes Berg 		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
35193e75668bSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
352079faae3aSGregory Greenman 					 ERR_PTR(-ENOENT));
3521b8a85a1dSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3522b8a85a1dSJohannes Berg 		}
352379faae3aSGregory Greenman 	}
3524e705c121SKalle Valo }
3525e705c121SKalle Valo 
iwl_mvm_check_uapsd(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const u8 * bssid)3526e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3527e705c121SKalle Valo 				const u8 *bssid)
3528e705c121SKalle Valo {
3529b0ffe455SJohannes Berg 	int i;
3530b0ffe455SJohannes Berg 
3531b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3532b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
3533b0ffe455SJohannes Berg 
3534b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3535b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3536b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
3537b0ffe455SJohannes Berg 	}
3538b0ffe455SJohannes Berg 
3539e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3540e705c121SKalle Valo 		return;
3541e705c121SKalle Valo 
3542c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3543cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3544cee5a882SAvri Altman 		return;
3545cee5a882SAvri Altman 	}
3546cee5a882SAvri Altman 
354711dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
354811dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3549e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3550e705c121SKalle Valo 		return;
3551e705c121SKalle Valo 	}
3552e705c121SKalle Valo 
3553b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3554b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3555b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3556b0ffe455SJohannes Berg 			return;
3557b0ffe455SJohannes Berg 		}
3558b0ffe455SJohannes Berg 	}
3559b0ffe455SJohannes Berg 
3560e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3561e705c121SKalle Valo }
3562e705c121SKalle Valo 
35631e8f1329SGolan Ben-Ami static void
iwl_mvm_tdls_check_trigger(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u8 * peer_addr,enum nl80211_tdls_operation action)35641e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
35651e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
35661e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
35671e8f1329SGolan Ben-Ami {
35681e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
35691e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
35701e8f1329SGolan Ben-Ami 
35716c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
35726c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
35736c042d75SSara Sharon 	if (!trig)
35741e8f1329SGolan Ben-Ami 		return;
35751e8f1329SGolan Ben-Ami 
35761e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
35771e8f1329SGolan Ben-Ami 
35781e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
35791e8f1329SGolan Ben-Ami 		return;
35801e8f1329SGolan Ben-Ami 
35811e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
35821e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
35831e8f1329SGolan Ben-Ami 		return;
35841e8f1329SGolan Ben-Ami 
35857174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
35861e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
35871e8f1329SGolan Ben-Ami 				peer_addr, action);
35881e8f1329SGolan Ben-Ami }
35891e8f1329SGolan Ben-Ami 
35904f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
35914f58121dSIlan Peer 	bool tolerated;
35924f58121dSIlan Peer };
35934f58121dSIlan Peer 
iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy * wiphy,struct cfg80211_bss * bss,void * _data)35944f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
35954f58121dSIlan Peer 						    struct cfg80211_bss *bss,
35964f58121dSIlan Peer 						    void *_data)
35974f58121dSIlan Peer {
35984f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
35996c608cd6SJohannes Berg 	const struct cfg80211_bss_ies *ies;
36004f58121dSIlan Peer 	const struct element *elem;
36014f58121dSIlan Peer 
36026c608cd6SJohannes Berg 	rcu_read_lock();
36036c608cd6SJohannes Berg 	ies = rcu_dereference(bss->ies);
36046c608cd6SJohannes Berg 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
36056c608cd6SJohannes Berg 				  ies->len);
36064f58121dSIlan Peer 
36074f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
36084f58121dSIlan Peer 	    !(elem->data[10] &
36094f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
36104f58121dSIlan Peer 		data->tolerated = false;
36114f58121dSIlan Peer 	}
36126c608cd6SJohannes Berg 	rcu_read_unlock();
36134f58121dSIlan Peer }
36144f58121dSIlan Peer 
36156e1b5956SJohannes Berg static void
iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,struct ieee80211_bss_conf * link_conf)36166e1b5956SJohannes Berg iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
36176e1b5956SJohannes Berg 				   struct ieee80211_vif *vif,
36186e1b5956SJohannes Berg 				   unsigned int link_id,
36196e1b5956SJohannes Berg 				   struct ieee80211_bss_conf *link_conf)
36204f58121dSIlan Peer {
36214f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
36224f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
36234f58121dSIlan Peer 		.tolerated = true,
36244f58121dSIlan Peer 	};
36254f58121dSIlan Peer 
36266092077aSJohannes Berg 	if (WARN_ON_ONCE(!link_conf->chanreq.oper.chan ||
36276e1b5956SJohannes Berg 			 !mvmvif->link[link_id]))
36286e1b5956SJohannes Berg 		return;
36296e1b5956SJohannes Berg 
36306092077aSJohannes Berg 	if (!(link_conf->chanreq.oper.chan->flags & IEEE80211_CHAN_RADAR)) {
36316e1b5956SJohannes Berg 		mvmvif->link[link_id]->he_ru_2mhz_block = false;
36324f58121dSIlan Peer 		return;
36334f58121dSIlan Peer 	}
36344f58121dSIlan Peer 
36356092077aSJohannes Berg 	cfg80211_bss_iter(hw->wiphy, &link_conf->chanreq.oper,
36364f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
36374f58121dSIlan Peer 			  &iter_data);
36384f58121dSIlan Peer 
36394f58121dSIlan Peer 	/*
36404f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
36414f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
36424f58121dSIlan Peer 	 */
36436e1b5956SJohannes Berg 	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
36444f58121dSIlan Peer }
36454f58121dSIlan Peer 
iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm * mvm,struct ieee80211_vif * vif)3646f7d6ef33SJohannes Berg static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3647f7d6ef33SJohannes Berg 					       struct ieee80211_vif *vif)
3648f7d6ef33SJohannes Berg {
3649f7d6ef33SJohannes Berg 	struct ieee80211_supported_band *sband;
3650f7d6ef33SJohannes Berg 	const struct ieee80211_sta_he_cap *he_cap;
3651f7d6ef33SJohannes Berg 
3652f7d6ef33SJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION)
3653f7d6ef33SJohannes Berg 		return;
3654f7d6ef33SJohannes Berg 
3655f7d6ef33SJohannes Berg 	if (!mvm->cca_40mhz_workaround)
3656f7d6ef33SJohannes Berg 		return;
3657f7d6ef33SJohannes Berg 
3658f7d6ef33SJohannes Berg 	/* decrement and check that we reached zero */
3659f7d6ef33SJohannes Berg 	mvm->cca_40mhz_workaround--;
3660f7d6ef33SJohannes Berg 	if (mvm->cca_40mhz_workaround)
3661f7d6ef33SJohannes Berg 		return;
3662f7d6ef33SJohannes Berg 
3663f7d6ef33SJohannes Berg 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3664f7d6ef33SJohannes Berg 
3665f7d6ef33SJohannes Berg 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3666f7d6ef33SJohannes Berg 
36671ec7291eSJohannes Berg 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3668f7d6ef33SJohannes Berg 
3669f7d6ef33SJohannes Berg 	if (he_cap) {
3670f7d6ef33SJohannes Berg 		/* we know that ours is writable */
36710301bcd5SBjoern A. Zeeb 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3672f7d6ef33SJohannes Berg 
3673f7d6ef33SJohannes Berg 		he->he_cap_elem.phy_cap_info[0] |=
3674f7d6ef33SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3675f7d6ef33SJohannes Berg 	}
3676f7d6ef33SJohannes Berg }
3677f7d6ef33SJohannes Berg 
iwl_mvm_mei_host_associated(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_sta * mvm_sta)36786d19a5ebSEmmanuel Grumbach static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
36796d19a5ebSEmmanuel Grumbach 					struct ieee80211_vif *vif,
36806d19a5ebSEmmanuel Grumbach 					struct iwl_mvm_sta *mvm_sta)
36816d19a5ebSEmmanuel Grumbach {
36826d19a5ebSEmmanuel Grumbach #if IS_ENABLED(CONFIG_IWLMEI)
36836d19a5ebSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
36846d19a5ebSEmmanuel Grumbach 	struct iwl_mei_conn_info conn_info = {
3685f276e20bSJohannes Berg 		.ssid_len = vif->cfg.ssid_len,
36866d19a5ebSEmmanuel Grumbach 	};
36876d19a5ebSEmmanuel Grumbach 
36886d19a5ebSEmmanuel Grumbach 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
36896d19a5ebSEmmanuel Grumbach 		return;
36906d19a5ebSEmmanuel Grumbach 
36916d19a5ebSEmmanuel Grumbach 	if (!mvm->mei_registered)
36926d19a5ebSEmmanuel Grumbach 		return;
36936d19a5ebSEmmanuel Grumbach 
36946c07b73eSJohannes Berg 	/* FIXME: MEI needs to be updated for MLO */
36956092077aSJohannes Berg 	if (!vif->bss_conf.chanreq.oper.chan)
36966c07b73eSJohannes Berg 		return;
36976c07b73eSJohannes Berg 
36986092077aSJohannes Berg 	conn_info.channel = vif->bss_conf.chanreq.oper.chan->hw_value;
36996c07b73eSJohannes Berg 
37006d19a5ebSEmmanuel Grumbach 	switch (mvm_sta->pairwise_cipher) {
3701e5d3a64eSAvraham Stern 	case WLAN_CIPHER_SUITE_TKIP:
3702e5d3a64eSAvraham Stern 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3703e5d3a64eSAvraham Stern 		break;
37046d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_CCMP:
37056d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
37066d19a5ebSEmmanuel Grumbach 		break;
37076d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP:
37086d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
37096d19a5ebSEmmanuel Grumbach 		break;
37106d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP_256:
37116d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
37126d19a5ebSEmmanuel Grumbach 		break;
37136d19a5ebSEmmanuel Grumbach 	case 0:
37146d19a5ebSEmmanuel Grumbach 		/* open profile */
37156d19a5ebSEmmanuel Grumbach 		break;
37166d19a5ebSEmmanuel Grumbach 	default:
37176d19a5ebSEmmanuel Grumbach 		/* cipher not supported, don't send anything to iwlmei */
37186d19a5ebSEmmanuel Grumbach 		return;
37196d19a5ebSEmmanuel Grumbach 	}
37206d19a5ebSEmmanuel Grumbach 
37216d19a5ebSEmmanuel Grumbach 	switch (mvmvif->rekey_data.akm) {
37226d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_SAE & 0xff:
37236d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
37246d19a5ebSEmmanuel Grumbach 		break;
37256d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_PSK & 0xff:
37266d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
37276d19a5ebSEmmanuel Grumbach 		break;
37286d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_8021X & 0xff:
37296d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
37306d19a5ebSEmmanuel Grumbach 		break;
37316d19a5ebSEmmanuel Grumbach 	case 0:
37326d19a5ebSEmmanuel Grumbach 		/* open profile */
37336d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
37346d19a5ebSEmmanuel Grumbach 		break;
37356d19a5ebSEmmanuel Grumbach 	default:
37366d19a5ebSEmmanuel Grumbach 		/* auth method / AKM not supported */
37376d19a5ebSEmmanuel Grumbach 		/* TODO: All the FT vesions of these? */
37386d19a5ebSEmmanuel Grumbach 		return;
37396d19a5ebSEmmanuel Grumbach 	}
37406d19a5ebSEmmanuel Grumbach 
3741f276e20bSJohannes Berg 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
37426d19a5ebSEmmanuel Grumbach 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
37436d19a5ebSEmmanuel Grumbach 
37446d19a5ebSEmmanuel Grumbach 	/* TODO: add support for collocated AP data */
37456d19a5ebSEmmanuel Grumbach 	iwl_mei_host_associated(&conn_info, NULL);
37466d19a5ebSEmmanuel Grumbach #endif
37476d19a5ebSEmmanuel Grumbach }
37486d19a5ebSEmmanuel Grumbach 
iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm * mvm,struct ieee80211_vif * vif,bool force_assoc_off)374987f7e243SMiri Korenblit static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
375087f7e243SMiri Korenblit 					    struct ieee80211_vif *vif,
375187f7e243SMiri Korenblit 					    bool force_assoc_off)
375287f7e243SMiri Korenblit {
375387f7e243SMiri Korenblit 	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
375487f7e243SMiri Korenblit }
375587f7e243SMiri Korenblit 
iwl_mvm_mac_sta_state(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,enum ieee80211_sta_state old_state,enum ieee80211_sta_state new_state)3756e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3757e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3758e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3759e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3760e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3761e705c121SKalle Valo {
3762a2906ea6SJohannes Berg 	static const struct iwl_mvm_sta_state_ops callbacks = {
376387f7e243SMiri Korenblit 		.add_sta = iwl_mvm_add_sta,
376487f7e243SMiri Korenblit 		.update_sta = iwl_mvm_update_sta,
376587f7e243SMiri Korenblit 		.rm_sta = iwl_mvm_rm_sta,
376687f7e243SMiri Korenblit 		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
376787f7e243SMiri Korenblit 	};
376887f7e243SMiri Korenblit 
376987f7e243SMiri Korenblit 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
377087f7e243SMiri Korenblit 					    &callbacks);
377187f7e243SMiri Korenblit }
377287f7e243SMiri Korenblit 
377357974a55SGregory Greenman /* FIXME: temporary making two assumptions in all sta handling functions:
377457974a55SGregory Greenman  *	(1) when setting sta state, the link exists and protected
377557974a55SGregory Greenman  *	(2) if a link is valid in sta then it's valid in vif (can
377657974a55SGregory Greenman  *	use same index in the link array)
377757974a55SGregory Greenman  */
iwl_mvm_rs_rate_init_all_links(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3778f53be9c4SGregory Greenman static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3779f53be9c4SGregory Greenman 					   struct ieee80211_vif *vif,
378015d41834SJohannes Berg 					   struct ieee80211_sta *sta)
3781f53be9c4SGregory Greenman {
3782f53be9c4SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3783f53be9c4SGregory Greenman 	unsigned int link_id;
378457974a55SGregory Greenman 
3785f53be9c4SGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3786f53be9c4SGregory Greenman 		struct ieee80211_bss_conf *conf =
3787a705a782SJohannes Berg 			link_conf_dereference_check(vif, link_id);
3788f53be9c4SGregory Greenman 		struct ieee80211_link_sta *link_sta =
3789a705a782SJohannes Berg 			link_sta_dereference_check(sta, link_id);
3790f53be9c4SGregory Greenman 
3791f53be9c4SGregory Greenman 		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3792f53be9c4SGregory Greenman 			continue;
3793f53be9c4SGregory Greenman 
3794c45217bdSJohannes Berg 		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
379515d41834SJohannes Berg 				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3796f53be9c4SGregory Greenman 	}
3797f53be9c4SGregory Greenman }
379857974a55SGregory Greenman 
iwl_mvm_vif_conf_from_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta)379957974a55SGregory Greenman static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
380057974a55SGregory Greenman 				      struct ieee80211_vif *vif,
380157974a55SGregory Greenman 				      struct ieee80211_sta *sta)
380257974a55SGregory Greenman {
3803207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3804207be64fSMiri Korenblit 	unsigned int link_id;
380557974a55SGregory Greenman 
380657974a55SGregory Greenman 	/* Beacon interval check - firmware will crash if the beacon
380757974a55SGregory Greenman 	 * interval is less than 16. We can't avoid connecting at all,
380857974a55SGregory Greenman 	 * so refuse the station state change, this will cause mac80211
380957974a55SGregory Greenman 	 * to abandon attempts to connect to this AP, and eventually
381057974a55SGregory Greenman 	 * wpa_s will blocklist the AP...
381157974a55SGregory Greenman 	 */
381257974a55SGregory Greenman 
3813207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
381457974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3815207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
381657974a55SGregory Greenman 
3817207be64fSMiri Korenblit 		if (!link_conf)
381857974a55SGregory Greenman 			continue;
381957974a55SGregory Greenman 
382057974a55SGregory Greenman 		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
382157974a55SGregory Greenman 			IWL_ERR(mvm,
382257974a55SGregory Greenman 				"Beacon interval %d for AP %pM is too small\n",
382357974a55SGregory Greenman 				link_conf->beacon_int, link_sta->addr);
382457974a55SGregory Greenman 			return false;
382557974a55SGregory Greenman 		}
382657974a55SGregory Greenman 
382757974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
382857974a55SGregory Greenman 	}
382957974a55SGregory Greenman 
383057974a55SGregory Greenman 	return true;
383157974a55SGregory Greenman }
383257974a55SGregory Greenman 
iwl_mvm_vif_set_he_support(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool is_sta)383357974a55SGregory Greenman static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
383457974a55SGregory Greenman 				       struct ieee80211_vif *vif,
383557974a55SGregory Greenman 				       struct ieee80211_sta *sta,
383657974a55SGregory Greenman 				       bool is_sta)
383757974a55SGregory Greenman {
383857974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3839207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3840207be64fSMiri Korenblit 	unsigned int link_id;
384157974a55SGregory Greenman 
3842207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
384357974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3844207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
384557974a55SGregory Greenman 
3846207be64fSMiri Korenblit 		if (!link_conf || !mvmvif->link[link_id])
384757974a55SGregory Greenman 			continue;
384857974a55SGregory Greenman 
384957974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
385057974a55SGregory Greenman 
385157974a55SGregory Greenman 		if (is_sta) {
3852207be64fSMiri Korenblit 			mvmvif->link[link_id]->he_ru_2mhz_block = false;
385357974a55SGregory Greenman 			if (link_sta->he_cap.has_he)
3854207be64fSMiri Korenblit 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3855207be64fSMiri Korenblit 								   link_id,
38566e1b5956SJohannes Berg 								   link_conf);
385757974a55SGregory Greenman 		}
385857974a55SGregory Greenman 	}
385957974a55SGregory Greenman }
386057974a55SGregory Greenman 
386157974a55SGregory Greenman static int
iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,const struct iwl_mvm_sta_state_ops * callbacks)386257974a55SGregory Greenman iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
386357974a55SGregory Greenman 				   struct ieee80211_vif *vif,
386457974a55SGregory Greenman 				   struct ieee80211_sta *sta,
3865a2906ea6SJohannes Berg 				   const struct iwl_mvm_sta_state_ops *callbacks)
386657974a55SGregory Greenman {
38670c9a8f90SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3868207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
386957974a55SGregory Greenman 	unsigned int i;
387057974a55SGregory Greenman 	int ret;
387157974a55SGregory Greenman 
387257974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
387357974a55SGregory Greenman 
387457974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_STATION &&
387557974a55SGregory Greenman 	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
387657974a55SGregory Greenman 		return -EINVAL;
387757974a55SGregory Greenman 
387857974a55SGregory Greenman 	if (sta->tdls &&
387957974a55SGregory Greenman 	    (vif->p2p ||
388057974a55SGregory Greenman 	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
388157974a55SGregory Greenman 	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
388257974a55SGregory Greenman 		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
388357974a55SGregory Greenman 		return -EBUSY;
388457974a55SGregory Greenman 	}
388557974a55SGregory Greenman 
388657974a55SGregory Greenman 	ret = callbacks->add_sta(mvm, vif, sta);
388757974a55SGregory Greenman 	if (sta->tdls && ret == 0) {
388857974a55SGregory Greenman 		iwl_mvm_recalc_tdls_state(mvm, vif, true);
388957974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
389057974a55SGregory Greenman 					   NL80211_TDLS_SETUP);
389157974a55SGregory Greenman 	}
389257974a55SGregory Greenman 
3893b7198383SEmmanuel Grumbach 	if (ret)
3894b7198383SEmmanuel Grumbach 		return ret;
3895b7198383SEmmanuel Grumbach 
3896207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, i)
389757974a55SGregory Greenman 		link_sta->agg.max_rc_amsdu_len = 1;
3898207be64fSMiri Korenblit 
389957974a55SGregory Greenman 	ieee80211_sta_recalc_aggregates(sta);
390057974a55SGregory Greenman 
39010c9a8f90SJohannes Berg 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
39020c9a8f90SJohannes Berg 		mvmvif->ap_sta = sta;
39030c9a8f90SJohannes Berg 
3904d3b2c6c6SJohannes Berg 	/*
3905d3b2c6c6SJohannes Berg 	 * Initialize the rates here already - this really tells
3906d3b2c6c6SJohannes Berg 	 * the firmware only what the supported legacy rates are
3907d3b2c6c6SJohannes Berg 	 * (may be) since it's initialized already from what the
3908d3b2c6c6SJohannes Berg 	 * AP advertised in the beacon/probe response. This will
3909d3b2c6c6SJohannes Berg 	 * allow the firmware to send auth/assoc frames with one
3910d3b2c6c6SJohannes Berg 	 * of the supported rates already, rather than having to
3911d3b2c6c6SJohannes Berg 	 * use a mandatory rate.
3912d3b2c6c6SJohannes Berg 	 * If we're the AP, we'll just assume mandatory rates at
3913d3b2c6c6SJohannes Berg 	 * this point, but we know nothing about the STA anyway.
3914d3b2c6c6SJohannes Berg 	 */
3915d3b2c6c6SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3916d3b2c6c6SJohannes Berg 
391757974a55SGregory Greenman 	return 0;
391857974a55SGregory Greenman }
391957974a55SGregory Greenman 
392057974a55SGregory Greenman static int
iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw * hw,struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,const struct iwl_mvm_sta_state_ops * callbacks)392157974a55SGregory Greenman iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
392257974a55SGregory Greenman 				struct iwl_mvm *mvm,
392357974a55SGregory Greenman 				struct ieee80211_vif *vif,
392457974a55SGregory Greenman 				struct ieee80211_sta *sta,
3925a2906ea6SJohannes Berg 				const struct iwl_mvm_sta_state_ops *callbacks)
392657974a55SGregory Greenman {
392757974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
392857974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3929207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3930207be64fSMiri Korenblit 	unsigned int link_id;
393157974a55SGregory Greenman 
393257974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
393357974a55SGregory Greenman 
393457974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_AP) {
393557974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
393657974a55SGregory Greenman 		mvmvif->ap_assoc_sta_count++;
393757974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
393857974a55SGregory Greenman 
393957974a55SGregory Greenman 		/* since the below is not for MLD API, it's ok to use
394057974a55SGregory Greenman 		 * the default bss_conf
394157974a55SGregory Greenman 		 */
394257974a55SGregory Greenman 		if (!mvm->mld_api_is_used &&
394383f57c93SJohannes Berg 		    (vif->bss_conf.he_support &&
394483f57c93SJohannes Berg 		     !iwlwifi_mod_params.disable_11ax))
394557974a55SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
394657974a55SGregory Greenman 	} else if (vif->type == NL80211_IFTYPE_STATION) {
394757974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
394857974a55SGregory Greenman 
394957974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
395057974a55SGregory Greenman 
395157974a55SGregory Greenman 		if (!mvm->mld_api_is_used)
395257974a55SGregory Greenman 			goto out;
395357974a55SGregory Greenman 
3954207be64fSMiri Korenblit 		for_each_sta_active_link(vif, sta, link_sta, link_id) {
395557974a55SGregory Greenman 			struct ieee80211_bss_conf *link_conf =
3956207be64fSMiri Korenblit 				link_conf_dereference_protected(vif, link_id);
395757974a55SGregory Greenman 
395857974a55SGregory Greenman 			if (WARN_ON(!link_conf))
395957974a55SGregory Greenman 				return -EINVAL;
3960207be64fSMiri Korenblit 			if (!mvmvif->link[link_id])
3961f14ad95aSJohannes Berg 				continue;
396257974a55SGregory Greenman 
396357974a55SGregory Greenman 			iwl_mvm_link_changed(mvm, vif, link_conf,
396457974a55SGregory Greenman 					     LINK_CONTEXT_MODIFY_ALL &
396557974a55SGregory Greenman 					     ~LINK_CONTEXT_MODIFY_ACTIVE,
396657974a55SGregory Greenman 					     true);
396757974a55SGregory Greenman 		}
396857974a55SGregory Greenman 	}
396957974a55SGregory Greenman 
397057974a55SGregory Greenman out:
397115d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
397257974a55SGregory Greenman 
397357974a55SGregory Greenman 	return callbacks->update_sta(mvm, vif, sta);
397457974a55SGregory Greenman }
397557974a55SGregory Greenman 
397657974a55SGregory Greenman static int
iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,const struct iwl_mvm_sta_state_ops * callbacks)397757974a55SGregory Greenman iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
397857974a55SGregory Greenman 				      struct ieee80211_vif *vif,
397957974a55SGregory Greenman 				      struct ieee80211_sta *sta,
3980a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
398157974a55SGregory Greenman {
398257974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
398357974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
398457974a55SGregory Greenman 
398557974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
398657974a55SGregory Greenman 
398757974a55SGregory Greenman 	/* we don't support TDLS during DCM */
398857974a55SGregory Greenman 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
398957974a55SGregory Greenman 		iwl_mvm_teardown_tdls_peers(mvm);
399057974a55SGregory Greenman 
399157974a55SGregory Greenman 	if (sta->tdls) {
399257974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
399357974a55SGregory Greenman 					   NL80211_TDLS_ENABLE_LINK);
399457974a55SGregory Greenman 	} else {
399557974a55SGregory Greenman 		/* enable beacon filtering */
399687f5b5f2SJohannes Berg 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
399757974a55SGregory Greenman 
399857974a55SGregory Greenman 		mvmvif->authorized = 1;
3999950a3f5fSJohannes Berg 
4000950a3f5fSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
40012f33561eSMiri Korenblit 			mvmvif->link_selection_res = vif->active_links;
400207bf5297SMiri Korenblit 			mvmvif->link_selection_primary =
400307bf5297SMiri Korenblit 				vif->active_links ? __ffs(vif->active_links) : 0;
4004950a3f5fSJohannes Berg 		}
400557974a55SGregory Greenman 
400657974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
400757974a55SGregory Greenman 		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
4008b9be67fbSIlan Peer 
4009492bc4e4SMiri Korenblit 		memset(&mvmvif->last_esr_exit, 0,
4010492bc4e4SMiri Korenblit 		       sizeof(mvmvif->last_esr_exit));
4011492bc4e4SMiri Korenblit 
4012ec0d43d2SMiri Korenblit 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT, 0);
4013ec0d43d2SMiri Korenblit 
4014e619ad55SMiri Korenblit 		/* Block until FW notif will arrive */
4015e619ad55SMiri Korenblit 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_FW, 0);
4016e619ad55SMiri Korenblit 
4017b9be67fbSIlan Peer 		/* when client is authorized (AP station marked as such),
401807bf5297SMiri Korenblit 		 * try to enable the best link(s).
4019b9be67fbSIlan Peer 		 */
4020b9be67fbSIlan Peer 		if (vif->type == NL80211_IFTYPE_STATION &&
4021b9be67fbSIlan Peer 		    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
402207bf5297SMiri Korenblit 			iwl_mvm_select_links(mvm, vif);
402357974a55SGregory Greenman 	}
402457974a55SGregory Greenman 
402515d41834SJohannes Berg 	mvm_sta->authorized = true;
402615d41834SJohannes Berg 
40275a86dcb4SAvraham Stern 	/* MFP is set by default before the station is authorized.
40285a86dcb4SAvraham Stern 	 * Clear it here in case it's not used.
40295a86dcb4SAvraham Stern 	 */
40300fcdf55fSEmmanuel Grumbach 	if (!sta->mfp) {
40310fcdf55fSEmmanuel Grumbach 		int ret = callbacks->update_sta(mvm, vif, sta);
40320fcdf55fSEmmanuel Grumbach 
40330fcdf55fSEmmanuel Grumbach 		if (ret)
40340fcdf55fSEmmanuel Grumbach 			return ret;
40350fcdf55fSEmmanuel Grumbach 	}
40360fcdf55fSEmmanuel Grumbach 
40370fcdf55fSEmmanuel Grumbach 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
40385a86dcb4SAvraham Stern 
403957974a55SGregory Greenman 	return 0;
404057974a55SGregory Greenman }
404157974a55SGregory Greenman 
404257974a55SGregory Greenman static int
iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,const struct iwl_mvm_sta_state_ops * callbacks)404357974a55SGregory Greenman iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
404457974a55SGregory Greenman 				      struct ieee80211_vif *vif,
404557974a55SGregory Greenman 				      struct ieee80211_sta *sta,
4046a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
404757974a55SGregory Greenman {
404857974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
404915d41834SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
405057974a55SGregory Greenman 
405157974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
405257974a55SGregory Greenman 
405315d41834SJohannes Berg 	mvmsta->authorized = false;
405415d41834SJohannes Berg 
405557974a55SGregory Greenman 	/* once we move into assoc state, need to update rate scale to
405657974a55SGregory Greenman 	 * disable using wide bandwidth
405757974a55SGregory Greenman 	 */
405815d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
405957974a55SGregory Greenman 
406057974a55SGregory Greenman 	if (!sta->tdls) {
406157974a55SGregory Greenman 		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
406257974a55SGregory Greenman 		 * yet to avoid sending high prio again for a little
406357974a55SGregory Greenman 		 * time.
406457974a55SGregory Greenman 		 */
406557974a55SGregory Greenman 		mvmvif->authorized = 0;
406607bf5297SMiri Korenblit 		mvmvif->link_selection_res = 0;
406757974a55SGregory Greenman 
406857974a55SGregory Greenman 		/* disable beacon filtering */
406987f5b5f2SJohannes Berg 		iwl_mvm_disable_beacon_filter(mvm, vif);
4070492bc4e4SMiri Korenblit 
4071492bc4e4SMiri Korenblit 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4072492bc4e4SMiri Korenblit 					  &mvmvif->prevent_esr_done_wk);
40739c28ead0SMiri Korenblit 
40742f33561eSMiri Korenblit 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
40752f33561eSMiri Korenblit 					  &mvmvif->mlo_int_scan_wk);
40762f33561eSMiri Korenblit 
4077ec0d43d2SMiri Korenblit 		wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
4078ec0d43d2SMiri Korenblit 
40799c28ead0SMiri Korenblit 		/* No need for the periodic statistics anymore */
40809c28ead0SMiri Korenblit 		if (ieee80211_vif_is_mld(vif) && mvmvif->esr_active)
40819c28ead0SMiri Korenblit 			iwl_mvm_request_periodic_system_statistics(mvm, false);
408257974a55SGregory Greenman 	}
408357974a55SGregory Greenman 
408457974a55SGregory Greenman 	return 0;
408557974a55SGregory Greenman }
408657974a55SGregory Greenman 
408787f7e243SMiri Korenblit /* Common part for MLD and non-MLD modes */
iwl_mvm_mac_sta_state_common(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,enum ieee80211_sta_state old_state,enum ieee80211_sta_state new_state,const struct iwl_mvm_sta_state_ops * callbacks)408887f7e243SMiri Korenblit int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
408987f7e243SMiri Korenblit 				 struct ieee80211_vif *vif,
409087f7e243SMiri Korenblit 				 struct ieee80211_sta *sta,
409187f7e243SMiri Korenblit 				 enum ieee80211_sta_state old_state,
409287f7e243SMiri Korenblit 				 enum ieee80211_sta_state new_state,
4093a2906ea6SJohannes Berg 				 const struct iwl_mvm_sta_state_ops *callbacks)
409487f7e243SMiri Korenblit {
4095e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4096e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
40976ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4098de107600SJohannes Berg 	struct ieee80211_link_sta *link_sta;
409957974a55SGregory Greenman 	unsigned int link_id;
4100e705c121SKalle Valo 	int ret;
4101e705c121SKalle Valo 
4102e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
4103e705c121SKalle Valo 			   sta->addr, old_state, new_state);
4104e705c121SKalle Valo 
410524afba76SLiad Kaufman 	/*
410624afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
410724afba76SLiad Kaufman 	 *
410824afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
410924afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
411024afba76SLiad Kaufman 	 * the queues.
411124afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
411224afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
411324afba76SLiad Kaufman 	 * of.
411424afba76SLiad Kaufman 	 *
411524afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
411624afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
411724afba76SLiad Kaufman 	 */
411824afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
4119c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
412024afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
412124afba76SLiad Kaufman 
412224afba76SLiad Kaufman 		/*
412324afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
412424afba76SLiad Kaufman 		 * worker will already remove it if it was on
412524afba76SLiad Kaufman 		 */
4126f7d6ef33SJohannes Berg 
4127f7d6ef33SJohannes Berg 		/*
4128f7d6ef33SJohannes Berg 		 * Additionally, reset the 40 MHz capability if we disconnected
4129f7d6ef33SJohannes Berg 		 * from the AP now.
4130f7d6ef33SJohannes Berg 		 */
4131f7d6ef33SJohannes Berg 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
4132783336b0SJohannes Berg 
4133783336b0SJohannes Berg 		/* Also free dup data just in case any assertions below fail */
4134783336b0SJohannes Berg 		kfree(mvm_sta->dup_data);
413524afba76SLiad Kaufman 	}
413624afba76SLiad Kaufman 
4137e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
413857974a55SGregory Greenman 
4139828c79d9SEmmanuel Grumbach 	/* this would be a mac80211 bug ... but don't crash, unless we had a
4140828c79d9SEmmanuel Grumbach 	 * firmware crash while we were activating a link, in which case it is
4141828c79d9SEmmanuel Grumbach 	 * legit to have phy_ctxt = NULL. Don't bother not to WARN if we are in
4142828c79d9SEmmanuel Grumbach 	 * recovery flow since we spit tons of error messages anyway.
4143828c79d9SEmmanuel Grumbach 	 */
4144de107600SJohannes Berg 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
41453d6d21b2SJohannes Berg 		if (WARN_ON_ONCE(!mvmvif->link[link_id] ||
41463d6d21b2SJohannes Berg 				 !mvmvif->link[link_id]->phy_ctxt)) {
414757974a55SGregory Greenman 			mutex_unlock(&mvm->mutex);
414857974a55SGregory Greenman 			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
414957974a55SGregory Greenman 					&mvm->status) ? 0 : -EINVAL;
415057974a55SGregory Greenman 		}
415157974a55SGregory Greenman 	}
415257974a55SGregory Greenman 
41536ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
4154d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
41556ea29ce5SJohannes Berg 
4156e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
4157e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
415857974a55SGregory Greenman 		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
415957974a55SGregory Greenman 							 callbacks);
416057974a55SGregory Greenman 		if (ret < 0)
4161e705c121SKalle Valo 			goto out_unlock;
4162e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
4163e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
4164e705c121SKalle Valo 		/*
4165e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
4166e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
4167e705c121SKalle Valo 		 */
4168e705c121SKalle Valo 		mvm->last_ebs_successful = true;
4169e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
4170e705c121SKalle Valo 		ret = 0;
4171e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
4172e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
417357974a55SGregory Greenman 		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
417457974a55SGregory Greenman 						      callbacks);
4175e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
4176e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
417757974a55SGregory Greenman 		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
417857974a55SGregory Greenman 							    callbacks);
4179e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
4180e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
418157974a55SGregory Greenman 		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
418257974a55SGregory Greenman 							    callbacks);
4183e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
4184e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
41858be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
41868be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
418787f7e243SMiri Korenblit 			callbacks->mac_ctxt_changed(mvm, vif, false);
4188d5d8ee52SEmmanuel Grumbach 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
4189cf7a7457SJohannes Berg 			iwl_mvm_stop_session_protection(mvm, vif);
4190e705c121SKalle Valo 		ret = 0;
4191e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
4192e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
4193e705c121SKalle Valo 		ret = 0;
4194e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
4195e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
41960c9a8f90SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
4197d5d8ee52SEmmanuel Grumbach 			iwl_mvm_stop_session_protection(mvm, vif);
41980c9a8f90SJohannes Berg 			mvmvif->ap_sta = NULL;
41990c9a8f90SJohannes Berg 		}
420087f7e243SMiri Korenblit 		ret = callbacks->rm_sta(mvm, vif, sta);
42011e8f1329SGolan Ben-Ami 		if (sta->tdls) {
4202e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
42031e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
42041e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
42051e8f1329SGolan Ben-Ami 		}
420634a880d8SLiad Kaufman 
420744135b7cSIlan Peer 		if (unlikely(ret &&
420844135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
420944135b7cSIlan Peer 				      &mvm->status)))
421044135b7cSIlan Peer 			ret = 0;
4211e705c121SKalle Valo 	} else {
4212e705c121SKalle Valo 		ret = -EIO;
4213e705c121SKalle Valo 	}
4214e705c121SKalle Valo  out_unlock:
4215e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4216e705c121SKalle Valo 
4217e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
4218e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
4219e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
4220e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4221e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
4222e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
4223e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4224e705c121SKalle Valo 	}
4225e705c121SKalle Valo 
4226e705c121SKalle Valo 	return ret;
4227e705c121SKalle Valo }
4228e705c121SKalle Valo 
iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw * hw,u32 value)4229cbce62a3SMiri Korenblit int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4230e705c121SKalle Valo {
4231e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4232e705c121SKalle Valo 
4233e705c121SKalle Valo 	mvm->rts_threshold = value;
4234e705c121SKalle Valo 
4235e705c121SKalle Valo 	return 0;
4236e705c121SKalle Valo }
4237e705c121SKalle Valo 
iwl_mvm_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 changed)4238cbce62a3SMiri Korenblit void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4239e705c121SKalle Valo 			   struct ieee80211_sta *sta, u32 changed)
4240e705c121SKalle Valo {
4241e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4242dcfe3b10SJohannes Berg 
4243dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
4244dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
4245dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
424615d41834SJohannes Berg 		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4247e705c121SKalle Valo 
4248e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
4249e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
4250e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
4251e705c121SKalle Valo }
4252e705c121SKalle Valo 
iwl_mvm_mac_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 ac,const struct ieee80211_tx_queue_params * params)4253e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
4254b3e2130bSJohannes Berg 			       struct ieee80211_vif *vif,
4255b3e2130bSJohannes Berg 			       unsigned int link_id, u16 ac,
4256e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
4257e705c121SKalle Valo {
4258e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4259e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4260e705c121SKalle Valo 
4261650cadb7SGregory Greenman 	mvmvif->deflink.queue_params[ac] = *params;
4262e705c121SKalle Valo 
4263e705c121SKalle Valo 	/*
4264e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
4265e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
4266e705c121SKalle Valo 	 */
4267e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
42683f312651SJohannes Berg 		guard(mvm)(mvm);
42693f312651SJohannes Berg 		return iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4270e705c121SKalle Valo 	}
4271e705c121SKalle Valo 	return 0;
4272e705c121SKalle Valo }
4273e705c121SKalle Valo 
iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_prep_tx_info * info)4274cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4275d4e36e55SIlan Peer 				struct ieee80211_vif *vif,
427615fae341SJohannes Berg 				struct ieee80211_prep_tx_info *info)
4277e705c121SKalle Valo {
42788c626172SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4279e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4280d4e36e55SIlan Peer 
42818c626172SJohannes Berg 	if (info->was_assoc && !mvmvif->session_prot_connection_loss)
42828c626172SJohannes Berg 		return;
42838c626172SJohannes Berg 
42843f312651SJohannes Berg 	guard(mvm)(mvm);
428523673041SMiri Korenblit 	iwl_mvm_protect_assoc(mvm, vif, info->duration, info->link_id);
4286e705c121SKalle Valo }
4287e705c121SKalle Valo 
iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_prep_tx_info * info)4288cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
42896b1259d1SJohannes Berg 				 struct ieee80211_vif *vif,
42906b1259d1SJohannes Berg 				 struct ieee80211_prep_tx_info *info)
42916b1259d1SJohannes Berg {
42926b1259d1SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
42936b1259d1SJohannes Berg 
42946b1259d1SJohannes Berg 	/* for successful cases (auth/assoc), don't cancel session protection */
42956b1259d1SJohannes Berg 	if (info->success)
42966b1259d1SJohannes Berg 		return;
42976b1259d1SJohannes Berg 
42983f312651SJohannes Berg 	guard(mvm)(mvm);
42996b1259d1SJohannes Berg 	iwl_mvm_stop_session_protection(mvm, vif);
43006b1259d1SJohannes Berg }
43016b1259d1SJohannes Berg 
iwl_mvm_mac_sched_scan_start(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_sched_scan_request * req,struct ieee80211_scan_ies * ies)4302cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4303e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4304e705c121SKalle Valo 				 struct cfg80211_sched_scan_request *req,
4305e705c121SKalle Valo 				 struct ieee80211_scan_ies *ies)
4306e705c121SKalle Valo {
4307e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4308e705c121SKalle Valo 
43093f312651SJohannes Berg 	guard(mvm)(mvm);
4310e705c121SKalle Valo 
43113f312651SJohannes Berg 	if (!vif->cfg.idle)
43123f312651SJohannes Berg 		return -EBUSY;
4313e705c121SKalle Valo 
43143f312651SJohannes Berg 	return iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4315e705c121SKalle Valo }
4316e705c121SKalle Valo 
iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw * hw,struct ieee80211_vif * vif)4317cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4318e705c121SKalle Valo 				struct ieee80211_vif *vif)
4319e705c121SKalle Valo {
4320e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4321e705c121SKalle Valo 	int ret;
4322e705c121SKalle Valo 
4323e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4324e705c121SKalle Valo 
4325e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
4326e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
4327e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
4328e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
4329e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4330e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
4331e705c121SKalle Valo 	 * not running.
4332e705c121SKalle Valo 	*/
4333e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4334e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
4335e705c121SKalle Valo 		return 0;
4336e705c121SKalle Valo 	}
4337e705c121SKalle Valo 
4338e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4339e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4340e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
4341e705c121SKalle Valo 
4342e705c121SKalle Valo 	return ret;
4343e705c121SKalle Valo }
4344e705c121SKalle Valo 
__iwl_mvm_mac_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)43456569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4346e705c121SKalle Valo 				 enum set_key_cmd cmd,
4347e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4348e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4349e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
4350e705c121SKalle Valo {
4351c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4352e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
435346c7b05aSEmmanuel Grumbach 	struct iwl_mvm_sta *mvmsta = NULL;
4354d066a530SJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn = NULL;
4355f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
43565c75a208SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
43575c75a208SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4358c56e00a3SJohannes Berg 	int ret, i;
43594615fd15SEmmanuel Grumbach 	u8 key_offset;
4360e705c121SKalle Valo 
436146c7b05aSEmmanuel Grumbach 	if (sta)
43626d19a5ebSEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
43636d19a5ebSEmmanuel Grumbach 
4364e705c121SKalle Valo 	switch (key->cipher) {
4365e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
4366286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
4367e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
43681ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
43697f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
43707f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
43717f768ad5SDavid Spinadel 		} else {
43727f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
43737f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
43747f768ad5SDavid Spinadel 		}
4375e705c121SKalle Valo 		break;
4376e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
43772a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
43782a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
437985aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
4380e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4381e705c121SKalle Valo 		break;
4382e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
43838e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
43848e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4385e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4386e705c121SKalle Valo 		break;
4387e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
4388e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
4389475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
4390e705c121SKalle Valo 			break;
4391475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
4392475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
4393475c6bdeSJohannes Berg 		/* support HW crypto on TX */
4394475c6bdeSJohannes Berg 		return 0;
4395e705c121SKalle Valo 	default:
4396e705c121SKalle Valo 		return -EOPNOTSUPP;
4397e705c121SKalle Valo 	}
4398e705c121SKalle Valo 
4399e705c121SKalle Valo 	switch (cmd) {
4400e705c121SKalle Valo 	case SET_KEY:
4401a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4402a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4403b1fdc250SJohannes Berg 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4404b1fdc250SJohannes Berg 					   key);
4405b1fdc250SJohannes Berg 
4406e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4407e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4408e705c121SKalle Valo 			/*
4409e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
4410e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
4411e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
4412f05d1e04SJohannes Berg 			 * CMAC/GMAC in AP/IBSS modes must be done in software
4413f05d1e04SJohannes Berg 			 * on older NICs.
4414a5de7de7SJohannes Berg 			 *
4415a5de7de7SJohannes Berg 			 * Except, of course, beacon protection - it must be
4416f05d1e04SJohannes Berg 			 * offloaded since we just set a beacon template, and
4417f05d1e04SJohannes Berg 			 * then we must also offload the IGTK (not just BIGTK)
4418f05d1e04SJohannes Berg 			 * for firmware reasons.
4419f05d1e04SJohannes Berg 			 *
4420f05d1e04SJohannes Berg 			 * So just check for beacon protection - if we don't
4421f05d1e04SJohannes Berg 			 * have it we cannot get here with keyidx >= 6, and
4422f05d1e04SJohannes Berg 			 * if we do have it we need to send the key to FW in
4423f05d1e04SJohannes Berg 			 * all cases (CMAC/GMAC).
4424e705c121SKalle Valo 			 */
4425f05d1e04SJohannes Berg 			if (!wiphy_ext_feature_isset(hw->wiphy,
4426f05d1e04SJohannes Berg 						     NL80211_EXT_FEATURE_BEACON_PROTECTION) &&
4427a5de7de7SJohannes Berg 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
44288e160ab8SAyala Beker 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4429a5de7de7SJohannes Berg 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
443081279c49SJohannes Berg 				ret = -EOPNOTSUPP;
4431a1c2ff30SAndrei Otcheretianski 				break;
4432a1c2ff30SAndrei Otcheretianski 			}
443385aeb58cSDavid Spinadel 
443485aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
443585aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
443685aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
4437e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
4438a1c2ff30SAndrei Otcheretianski 				ret = 0;
4439e705c121SKalle Valo 				break;
4440e705c121SKalle Valo 			}
4441c56e00a3SJohannes Berg 
4442c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
4443c56e00a3SJohannes Berg 				for (i = 0;
4444c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4445c56e00a3SJohannes Berg 				     i++) {
4446c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
4447c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
4448c56e00a3SJohannes Berg 						break;
4449c56e00a3SJohannes Berg 					}
4450c56e00a3SJohannes Berg 				}
4451c56e00a3SJohannes Berg 
4452c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4453c56e00a3SJohannes Berg 					ret = -ENOSPC;
4454a1c2ff30SAndrei Otcheretianski 				else
4455a1c2ff30SAndrei Otcheretianski 					ret = 0;
4456c56e00a3SJohannes Berg 
4457c56e00a3SJohannes Berg 				break;
4458c56e00a3SJohannes Berg 			}
445985aeb58cSDavid Spinadel 		}
4460e705c121SKalle Valo 
4461e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
4462e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
4463e705c121SKalle Valo 		 */
4464e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4465e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4466e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
4467e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
4468e705c121SKalle Valo 			ret = 0;
4469e705c121SKalle Valo 			break;
4470e705c121SKalle Valo 		}
4471e705c121SKalle Valo 
4472f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
447346c7b05aSEmmanuel Grumbach 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4474f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4475f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
44762a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
44772a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4478f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
4479f5e28eacSJohannes Berg 			int tid, q;
4480f5e28eacSJohannes Berg 
4481f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4482acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4483acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
4484f5e28eacSJohannes Berg 					 GFP_KERNEL);
4485f5e28eacSJohannes Berg 			if (!ptk_pn) {
4486f5e28eacSJohannes Berg 				ret = -ENOMEM;
4487f5e28eacSJohannes Berg 				break;
4488f5e28eacSJohannes Berg 			}
4489f5e28eacSJohannes Berg 
4490f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4491f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
4492f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
4493f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
4494f5e28eacSJohannes Berg 					       seq.ccmp.pn,
4495f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
4496f5e28eacSJohannes Berg 			}
4497f5e28eacSJohannes Berg 
4498f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4499f5e28eacSJohannes Berg 		}
4500f5e28eacSJohannes Berg 
45014615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
45024615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
45034615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
45044615fd15SEmmanuel Grumbach 		else
45054615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
45064615fd15SEmmanuel Grumbach 
450746c7b05aSEmmanuel Grumbach 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
45086d19a5ebSEmmanuel Grumbach 			mvmsta->pairwise_cipher = key->cipher;
45096d19a5ebSEmmanuel Grumbach 
4510a5de7de7SJohannes Berg 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4511a5de7de7SJohannes Berg 				   sta ? sta->addr : NULL, key->keyidx);
45125c75a208SJohannes Berg 
45135c75a208SJohannes Berg 		if (sec_key_ver)
45145c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
45155c75a208SJohannes Berg 		else
45164615fd15SEmmanuel Grumbach 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
45175c75a208SJohannes Berg 
4518e705c121SKalle Valo 		if (ret) {
4519e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
4520475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
4521d066a530SJohannes Berg 			if (ptk_pn) {
4522d066a530SJohannes Berg 				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4523d066a530SJohannes Berg 				kfree(ptk_pn);
4524d066a530SJohannes Berg 			}
4525e705c121SKalle Valo 			/*
4526e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
4527475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
4528475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
4529475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
4530e705c121SKalle Valo 			 */
4531475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
4532475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
4533475c6bdeSJohannes Berg 			else
4534e705c121SKalle Valo 				ret = 0;
4535e705c121SKalle Valo 		}
4536e705c121SKalle Valo 
4537e705c121SKalle Valo 		break;
4538e705c121SKalle Valo 	case DISABLE_KEY:
4539a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4540a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4541b1fdc250SJohannes Berg 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4542b1fdc250SJohannes Berg 					 NULL);
4543b1fdc250SJohannes Berg 
4544c56e00a3SJohannes Berg 		ret = -ENOENT;
4545c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4546c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
4547c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
4548c56e00a3SJohannes Berg 				ret = 0;
4549c56e00a3SJohannes Berg 			}
4550c56e00a3SJohannes Berg 		}
4551c56e00a3SJohannes Berg 
4552c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
4553c56e00a3SJohannes Berg 		if (ret == 0)
4554c56e00a3SJohannes Berg 			break;
4555c56e00a3SJohannes Berg 
4556e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4557e705c121SKalle Valo 			ret = 0;
4558e705c121SKalle Valo 			break;
4559e705c121SKalle Valo 		}
4560e705c121SKalle Valo 
456146c7b05aSEmmanuel Grumbach 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4562f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4563f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
45642a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
45652a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4566f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
4567f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
4568f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
4569f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4570f5e28eacSJohannes Berg 			if (ptk_pn)
4571f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
4572f5e28eacSJohannes Berg 		}
4573f5e28eacSJohannes Berg 
4574e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
45755c75a208SJohannes Berg 		if (sec_key_ver)
45765c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
45775c75a208SJohannes Berg 		else
4578e705c121SKalle Valo 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4579e705c121SKalle Valo 		break;
4580e705c121SKalle Valo 	default:
4581e705c121SKalle Valo 		ret = -EINVAL;
4582e705c121SKalle Valo 	}
4583e705c121SKalle Valo 
45846569e7d3SJohannes Berg 	return ret;
45856569e7d3SJohannes Berg }
45866569e7d3SJohannes Berg 
iwl_mvm_mac_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)4587cbce62a3SMiri Korenblit int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4588cbce62a3SMiri Korenblit 			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
45896569e7d3SJohannes Berg 			struct ieee80211_key_conf *key)
45906569e7d3SJohannes Berg {
45916569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
45926569e7d3SJohannes Berg 
45933f312651SJohannes Berg 	guard(mvm)(mvm);
45943f312651SJohannes Berg 	return __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4595e705c121SKalle Valo }
4596e705c121SKalle Valo 
iwl_mvm_mac_update_tkip_key(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_key_conf * keyconf,struct ieee80211_sta * sta,u32 iv32,u16 * phase1key)4597cbce62a3SMiri Korenblit void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4598e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4599e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
4600e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4601e705c121SKalle Valo 				 u32 iv32, u16 *phase1key)
4602e705c121SKalle Valo {
4603e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4604e705c121SKalle Valo 
4605e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4606e705c121SKalle Valo 		return;
4607e705c121SKalle Valo 
4608e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4609e705c121SKalle Valo }
4610e705c121SKalle Valo 
4611e705c121SKalle Valo 
iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)4612e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4613e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
4614e705c121SKalle Valo {
4615e705c121SKalle Valo 	struct iwl_mvm *mvm =
4616e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
4617e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
4618e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
4619e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
4620e705c121SKalle Valo 
4621e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4622e705c121SKalle Valo 		return true;
4623e705c121SKalle Valo 
4624e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4625e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4626e705c121SKalle Valo 		return true;
4627e705c121SKalle Valo 	}
4628e705c121SKalle Valo 
4629e705c121SKalle Valo 	resp = (void *)pkt->data;
4630e705c121SKalle Valo 
4631e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
4632b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4633e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
4634e705c121SKalle Valo 
4635e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4636e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4637e705c121SKalle Valo 		     te_data->uid);
4638e705c121SKalle Valo 
4639e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4640e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4641e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4642e705c121SKalle Valo 
4643e705c121SKalle Valo 	return true;
4644e705c121SKalle Valo }
4645e705c121SKalle Valo 
iwl_mvm_send_aux_roc_cmd(struct iwl_mvm * mvm,struct ieee80211_channel * channel,struct ieee80211_vif * vif,int duration)4646e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4647e705c121SKalle Valo 				    struct ieee80211_channel *channel,
4648e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4649e705c121SKalle Valo 				    int duration)
4650e705c121SKalle Valo {
4651afc1e3b4SAvraham Stern 	int res;
4652e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4653e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4654e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4655e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
4656dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
4657e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
4658e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4659e705c121SKalle Valo 		.id_and_color =
4660e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4661e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4662e705c121SKalle Valo 	};
466357e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
466457e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
466557e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
466657e861d9SDavid Spinadel 
466757e861d9SDavid Spinadel 	/* Set the channel info data */
466857e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
46693717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
46707ac87575SJohannes Berg 			      IWL_PHY_CHANNEL_MODE20,
467157e861d9SDavid Spinadel 			      0);
467257e861d9SDavid Spinadel 
467357e861d9SDavid Spinadel 	/* Set the time and duration */
4674afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4675e705c121SKalle Valo 
467667ac248eSShaul Triebitz 	iwl_mvm_roc_duration_and_delay(vif, duration, &req_dur, &delay);
467757e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
467857e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
4679dc28e12fSMatti Gottlieb 
4680dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
4681903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
4682903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
4683903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
468467ac248eSShaul Triebitz 		     "\t(requested = %ums, max_delay = %ums)\n",
468567ac248eSShaul Triebitz 		     duration, delay);
4686903b3f9bSEmmanuel Grumbach 
4687e705c121SKalle Valo 	/* Set the node address */
468857e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4689e705c121SKalle Valo 
4690e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4691e705c121SKalle Valo 
4692e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4693e705c121SKalle Valo 
4694e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4695e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4696e705c121SKalle Valo 		return -EIO;
4697e705c121SKalle Valo 	}
4698e705c121SKalle Valo 
4699e705c121SKalle Valo 	te_data->vif = vif;
4700e705c121SKalle Valo 	te_data->duration = duration;
4701e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
4702e705c121SKalle Valo 
4703e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4704e705c121SKalle Valo 
4705e705c121SKalle Valo 	/*
4706e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
4707e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
4708e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
4709e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4710e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
4711e705c121SKalle Valo 	 * time another notification (that the time event started)
4712e705c121SKalle Valo 	 * might already be processed unsuccessfully.
4713e705c121SKalle Valo 	 */
4714e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4715e705c121SKalle Valo 				   time_event_response,
4716e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
4717e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
4718e705c121SKalle Valo 
471957e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4720e705c121SKalle Valo 				   &aux_roc_req);
4721e705c121SKalle Valo 
4722e705c121SKalle Valo 	if (res) {
4723e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4724e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4725e705c121SKalle Valo 		goto out_clear_te;
4726e705c121SKalle Valo 	}
4727e705c121SKalle Valo 
4728e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4729e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4730e705c121SKalle Valo 	/* should never fail */
4731e705c121SKalle Valo 	WARN_ON_ONCE(res);
4732e705c121SKalle Valo 
4733e705c121SKalle Valo 	if (res) {
4734e705c121SKalle Valo  out_clear_te:
4735e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
4736e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
4737e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4738e705c121SKalle Valo 	}
4739e705c121SKalle Valo 
4740e705c121SKalle Valo 	return res;
4741e705c121SKalle Valo }
4742e705c121SKalle Valo 
iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm * mvm,u32 lmac_id)4743feebebaeSMiri Korenblit static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4744feebebaeSMiri Korenblit {
4745feebebaeSMiri Korenblit 	int ret = 0;
4746feebebaeSMiri Korenblit 
4747feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4748feebebaeSMiri Korenblit 
4749feebebaeSMiri Korenblit 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4750feebebaeSMiri Korenblit 			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4751feebebaeSMiri Korenblit 		IWL_ERR(mvm, "hotspot not supported\n");
4752feebebaeSMiri Korenblit 		return -EINVAL;
4753feebebaeSMiri Korenblit 	}
4754feebebaeSMiri Korenblit 
47551724fc78SEmmanuel Grumbach 	if (iwl_mvm_has_new_station_api(mvm->fw)) {
4756feebebaeSMiri Korenblit 		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4757feebebaeSMiri Korenblit 		WARN(ret, "Failed to allocate aux station");
4758feebebaeSMiri Korenblit 	}
4759feebebaeSMiri Korenblit 
4760feebebaeSMiri Korenblit 	return ret;
4761feebebaeSMiri Korenblit }
4762feebebaeSMiri Korenblit 
iwl_mvm_roc_link(struct iwl_mvm * mvm,struct ieee80211_vif * vif)476384ef7cbeSIlan Peer static int iwl_mvm_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4764feebebaeSMiri Korenblit {
476584ef7cbeSIlan Peer 	int ret;
4766feebebaeSMiri Korenblit 
4767feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4768feebebaeSMiri Korenblit 
4769feebebaeSMiri Korenblit 	ret = iwl_mvm_binding_add_vif(mvm, vif);
477084ef7cbeSIlan Peer 	if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4771feebebaeSMiri Korenblit 		return ret;
477284ef7cbeSIlan Peer 
477384ef7cbeSIlan Peer 	/* The station and queue allocation must be done only after the binding
477484ef7cbeSIlan Peer 	 * is done, as otherwise the FW might incorrectly configure its state.
477584ef7cbeSIlan Peer 	 */
477684ef7cbeSIlan Peer 	return iwl_mvm_add_p2p_bcast_sta(mvm, vif);
4777feebebaeSMiri Korenblit }
4778feebebaeSMiri Korenblit 
iwl_mvm_roc(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * channel,int duration,enum ieee80211_roc_type type)4779e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
4780e705c121SKalle Valo 		       struct ieee80211_vif *vif,
4781e705c121SKalle Valo 		       struct ieee80211_channel *channel,
4782e705c121SKalle Valo 		       int duration,
4783e705c121SKalle Valo 		       enum ieee80211_roc_type type)
4784e705c121SKalle Valo {
4785a2906ea6SJohannes Berg 	static const struct iwl_mvm_roc_ops ops = {
4786fe8b2ad3SMiri Korenblit 		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
478784ef7cbeSIlan Peer 		.link = iwl_mvm_roc_link,
4788fe8b2ad3SMiri Korenblit 	};
4789fe8b2ad3SMiri Korenblit 
4790fe8b2ad3SMiri Korenblit 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4791fe8b2ad3SMiri Korenblit }
4792fe8b2ad3SMiri Korenblit 
iwl_mvm_roc_station(struct iwl_mvm * mvm,struct ieee80211_channel * channel,struct ieee80211_vif * vif,int duration)479367ac248eSShaul Triebitz static int iwl_mvm_roc_station(struct iwl_mvm *mvm,
479467ac248eSShaul Triebitz 			       struct ieee80211_channel *channel,
479567ac248eSShaul Triebitz 			       struct ieee80211_vif *vif,
479667ac248eSShaul Triebitz 			       int duration)
479767ac248eSShaul Triebitz {
479867ac248eSShaul Triebitz 	int ret;
479967ac248eSShaul Triebitz 	u32 cmd_id = WIDE_ID(MAC_CONF_GROUP, ROC_CMD);
480067ac248eSShaul Triebitz 	u8 fw_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
480167ac248eSShaul Triebitz 					  IWL_FW_CMD_VER_UNKNOWN);
480267ac248eSShaul Triebitz 
480367ac248eSShaul Triebitz 	if (fw_ver == IWL_FW_CMD_VER_UNKNOWN) {
480467ac248eSShaul Triebitz 		ret = iwl_mvm_send_aux_roc_cmd(mvm, channel, vif, duration);
48054c2bed60SShaul Triebitz 	} else if (fw_ver >= 3) {
480667ac248eSShaul Triebitz 		ret = iwl_mvm_roc_add_cmd(mvm, channel, vif, duration,
480767ac248eSShaul Triebitz 					  ROC_ACTIVITY_HOTSPOT);
480867ac248eSShaul Triebitz 	} else {
480967ac248eSShaul Triebitz 		ret = -EOPNOTSUPP;
481067ac248eSShaul Triebitz 		IWL_ERR(mvm, "ROC command version %d mismatch!\n", fw_ver);
481167ac248eSShaul Triebitz 	}
481267ac248eSShaul Triebitz 
481367ac248eSShaul Triebitz 	return ret;
481467ac248eSShaul Triebitz }
481567ac248eSShaul Triebitz 
iwl_mvm_roc_p2p(struct iwl_mvm * mvm,struct ieee80211_channel * channel,struct ieee80211_vif * vif,int duration,enum ieee80211_roc_type type)481654d96912SShaul Triebitz static int iwl_mvm_roc_p2p(struct iwl_mvm *mvm,
481754d96912SShaul Triebitz 			   struct ieee80211_channel *channel,
481854d96912SShaul Triebitz 			   struct ieee80211_vif *vif,
481954d96912SShaul Triebitz 			   int duration,
482054d96912SShaul Triebitz 			   enum ieee80211_roc_type type)
482154d96912SShaul Triebitz {
482254d96912SShaul Triebitz 	enum iwl_roc_activity activity;
482354d96912SShaul Triebitz 	int ret;
482454d96912SShaul Triebitz 
482554d96912SShaul Triebitz 	lockdep_assert_held(&mvm->mutex);
482654d96912SShaul Triebitz 
482754d96912SShaul Triebitz 	switch (type) {
482854d96912SShaul Triebitz 	case IEEE80211_ROC_TYPE_NORMAL:
482954d96912SShaul Triebitz 		activity = ROC_ACTIVITY_P2P_DISC;
483054d96912SShaul Triebitz 		break;
483154d96912SShaul Triebitz 	case IEEE80211_ROC_TYPE_MGMT_TX:
483254d96912SShaul Triebitz 		activity = ROC_ACTIVITY_P2P_NEG;
483354d96912SShaul Triebitz 		break;
483454d96912SShaul Triebitz 	default:
483554d96912SShaul Triebitz 		WARN_ONCE(1, "Got an invalid P2P ROC type\n");
483654d96912SShaul Triebitz 		return -EINVAL;
483754d96912SShaul Triebitz 	}
483854d96912SShaul Triebitz 
483954d96912SShaul Triebitz 	ret = iwl_mvm_mld_add_aux_sta(mvm,
484054d96912SShaul Triebitz 				      iwl_mvm_get_lmac_id(mvm, channel->band));
484154d96912SShaul Triebitz 	if (ret)
484254d96912SShaul Triebitz 		return ret;
484354d96912SShaul Triebitz 
484454d96912SShaul Triebitz 	return iwl_mvm_roc_add_cmd(mvm, channel, vif, duration, activity);
484554d96912SShaul Triebitz }
484654d96912SShaul Triebitz 
iwl_mvm_p2p_find_phy_ctxt(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_channel * channel)484734cc3a4aSEmmanuel Grumbach static int iwl_mvm_p2p_find_phy_ctxt(struct iwl_mvm *mvm,
484834cc3a4aSEmmanuel Grumbach 				     struct ieee80211_vif *vif,
484934cc3a4aSEmmanuel Grumbach 				     struct ieee80211_channel *channel)
485034cc3a4aSEmmanuel Grumbach {
485134cc3a4aSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
485234cc3a4aSEmmanuel Grumbach 	struct cfg80211_chan_def chandef;
485334cc3a4aSEmmanuel Grumbach 	int i;
485434cc3a4aSEmmanuel Grumbach 
485534cc3a4aSEmmanuel Grumbach 	lockdep_assert_held(&mvm->mutex);
485634cc3a4aSEmmanuel Grumbach 
485734cc3a4aSEmmanuel Grumbach 	if (mvmvif->deflink.phy_ctxt &&
485834cc3a4aSEmmanuel Grumbach 	    channel == mvmvif->deflink.phy_ctxt->channel)
485934cc3a4aSEmmanuel Grumbach 		return 0;
486034cc3a4aSEmmanuel Grumbach 
486134cc3a4aSEmmanuel Grumbach 	/* Try using a PHY context that is already in use */
486234cc3a4aSEmmanuel Grumbach 	for (i = 0; i < NUM_PHY_CTX; i++) {
486334cc3a4aSEmmanuel Grumbach 		struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[i];
486434cc3a4aSEmmanuel Grumbach 
486534cc3a4aSEmmanuel Grumbach 		if (!phy_ctxt->ref || mvmvif->deflink.phy_ctxt == phy_ctxt)
486634cc3a4aSEmmanuel Grumbach 			continue;
486734cc3a4aSEmmanuel Grumbach 
486834cc3a4aSEmmanuel Grumbach 		if (channel == phy_ctxt->channel) {
486934cc3a4aSEmmanuel Grumbach 			if (mvmvif->deflink.phy_ctxt)
487034cc3a4aSEmmanuel Grumbach 				iwl_mvm_phy_ctxt_unref(mvm,
487134cc3a4aSEmmanuel Grumbach 						       mvmvif->deflink.phy_ctxt);
487234cc3a4aSEmmanuel Grumbach 
487334cc3a4aSEmmanuel Grumbach 			mvmvif->deflink.phy_ctxt = phy_ctxt;
487434cc3a4aSEmmanuel Grumbach 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
487534cc3a4aSEmmanuel Grumbach 			return 0;
487634cc3a4aSEmmanuel Grumbach 		}
487734cc3a4aSEmmanuel Grumbach 	}
487834cc3a4aSEmmanuel Grumbach 
487934cc3a4aSEmmanuel Grumbach 	/* We already have a phy_ctxt, but it's not on the right channel */
488034cc3a4aSEmmanuel Grumbach 	if (mvmvif->deflink.phy_ctxt)
488134cc3a4aSEmmanuel Grumbach 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
488234cc3a4aSEmmanuel Grumbach 
488334cc3a4aSEmmanuel Grumbach 	mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
488434cc3a4aSEmmanuel Grumbach 	if (!mvmvif->deflink.phy_ctxt)
488534cc3a4aSEmmanuel Grumbach 		return -ENOSPC;
488634cc3a4aSEmmanuel Grumbach 
488734cc3a4aSEmmanuel Grumbach 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
488834cc3a4aSEmmanuel Grumbach 
488934cc3a4aSEmmanuel Grumbach 	return iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt,
489032a5690eSJohannes Berg 				    &chandef, NULL, 1, 1);
489134cc3a4aSEmmanuel Grumbach }
489234cc3a4aSEmmanuel Grumbach 
4893fe8b2ad3SMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
iwl_mvm_roc_common(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * channel,int duration,enum ieee80211_roc_type type,const struct iwl_mvm_roc_ops * ops)4894fe8b2ad3SMiri Korenblit int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4895fe8b2ad3SMiri Korenblit 		       struct ieee80211_channel *channel, int duration,
4896fe8b2ad3SMiri Korenblit 		       enum ieee80211_roc_type type,
4897a2906ea6SJohannes Berg 		       const struct iwl_mvm_roc_ops *ops)
4898fe8b2ad3SMiri Korenblit {
4899e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
49005f1fee96SJohannes Berg 	struct ieee80211_vif *bss_vif = iwl_mvm_get_bss_vif(mvm);
4901feebebaeSMiri Korenblit 	u32 lmac_id;
490234cc3a4aSEmmanuel Grumbach 	int ret;
4903e705c121SKalle Valo 
4904e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4905e705c121SKalle Valo 			   duration, type);
4906e705c121SKalle Valo 
49079834781cSJohannes Berg 	/*
49089834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
49099834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
49109834781cSJohannes Berg 	 */
4911e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
4912e705c121SKalle Valo 
49135f1fee96SJohannes Berg 	if (!IS_ERR_OR_NULL(bss_vif)) {
49145f1fee96SJohannes Berg 		ret = iwl_mvm_block_esr_sync(mvm, bss_vif,
49155f1fee96SJohannes Berg 					     IWL_MVM_ESR_BLOCKED_ROC);
4916a1efeb82SYedidya Benshimol 		if (ret)
4917a1efeb82SYedidya Benshimol 			return ret;
49185f1fee96SJohannes Berg 	}
4919a1efeb82SYedidya Benshimol 
49203f312651SJohannes Berg 	guard(mvm)(mvm);
4921e705c121SKalle Valo 
4922e705c121SKalle Valo 	switch (vif->type) {
4923e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4924d51439a6SAriel Malamud 		lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
49252c2c3647SNathan Errera 
4926feebebaeSMiri Korenblit 		/* Use aux roc framework (HS20) */
4927fe8b2ad3SMiri Korenblit 		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4928feebebaeSMiri Korenblit 		if (!ret)
492967ac248eSShaul Triebitz 			ret = iwl_mvm_roc_station(mvm, channel, vif, duration);
49303f312651SJohannes Berg 		return ret;
4931e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
4932e705c121SKalle Valo 		/* handle below */
4933e705c121SKalle Valo 		break;
4934e705c121SKalle Valo 	default:
493584ef7cbeSIlan Peer 		IWL_ERR(mvm, "ROC: Invalid vif type=%u\n", vif->type);
49363f312651SJohannes Berg 		return -EINVAL;
4937e705c121SKalle Valo 	}
4938e705c121SKalle Valo 
493954d96912SShaul Triebitz 	if (iwl_mvm_has_p2p_over_aux(mvm)) {
494054d96912SShaul Triebitz 		ret = iwl_mvm_roc_p2p(mvm, channel, vif, duration, type);
494154d96912SShaul Triebitz 		return ret;
494254d96912SShaul Triebitz 	}
4943e705c121SKalle Valo 
494434cc3a4aSEmmanuel Grumbach 	ret = iwl_mvm_p2p_find_phy_ctxt(mvm, vif, channel);
494534cc3a4aSEmmanuel Grumbach 	if (ret)
49463f312651SJohannes Berg 		return ret;
4947e705c121SKalle Valo 
494884ef7cbeSIlan Peer 	ret = ops->link(mvm, vif);
4949feebebaeSMiri Korenblit 	if (ret)
4950e705c121SKalle Valo 		return ret;
49513f312651SJohannes Berg 
49523f312651SJohannes Berg 	return iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4953e705c121SKalle Valo }
4954e705c121SKalle Valo 
iwl_mvm_cancel_roc(struct ieee80211_hw * hw,struct ieee80211_vif * vif)4955fe8b2ad3SMiri Korenblit int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
49565db4c4b9SEmmanuel Grumbach 		       struct ieee80211_vif *vif)
4957e705c121SKalle Valo {
4958e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4959e705c121SKalle Valo 
4960e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4961e705c121SKalle Valo 
4962fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
4963e705c121SKalle Valo 
4964e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4965e705c121SKalle Valo 	return 0;
4966e705c121SKalle Valo }
4967e705c121SKalle Valo 
4968b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
4969b73f9a4aSJohannes Berg 	bool responder;
4970b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
4971b73f9a4aSJohannes Berg };
4972b73f9a4aSJohannes Berg 
iwl_mvm_ftm_responder_chanctx_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)4973b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4974b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
4975b73f9a4aSJohannes Berg {
4976b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4977b73f9a4aSJohannes Berg 
4978d0a9123eSJohannes Berg 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4979b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4980b73f9a4aSJohannes Berg 		data->responder = true;
4981b73f9a4aSJohannes Berg }
4982b73f9a4aSJohannes Berg 
iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm * mvm,struct ieee80211_chanctx_conf * ctx)4983ce1fa3adSJohannes Berg bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4984b73f9a4aSJohannes Berg 				      struct ieee80211_chanctx_conf *ctx)
4985b73f9a4aSJohannes Berg {
4986b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
4987b73f9a4aSJohannes Berg 		.responder = false,
4988b73f9a4aSJohannes Berg 		.ctx = ctx,
4989b73f9a4aSJohannes Berg 	};
4990b73f9a4aSJohannes Berg 
4991b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4992b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
4993b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
4994b73f9a4aSJohannes Berg 					&data);
4995b73f9a4aSJohannes Berg 	return data.responder;
4996b73f9a4aSJohannes Berg }
4997b73f9a4aSJohannes Berg 
__iwl_mvm_add_chanctx(struct iwl_mvm * mvm,struct ieee80211_chanctx_conf * ctx)4998e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4999e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
5000e705c121SKalle Valo {
5001e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5002e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
5003ce1fa3adSJohannes Berg 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
5004e705c121SKalle Valo 	int ret;
5005e705c121SKalle Valo 
5006e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
5007e705c121SKalle Valo 
5008e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
5009e705c121SKalle Valo 
5010e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
5011e705c121SKalle Valo 	if (!phy_ctxt) {
5012e705c121SKalle Valo 		ret = -ENOSPC;
5013e705c121SKalle Valo 		goto out;
5014e705c121SKalle Valo 	}
5015e705c121SKalle Valo 
501632a5690eSJohannes Berg 	ret = iwl_mvm_phy_ctxt_add(mvm, phy_ctxt, def, &ctx->ap,
5017e705c121SKalle Valo 				   ctx->rx_chains_static,
5018e705c121SKalle Valo 				   ctx->rx_chains_dynamic);
5019e705c121SKalle Valo 	if (ret) {
5020e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
5021e705c121SKalle Valo 		goto out;
5022e705c121SKalle Valo 	}
5023e705c121SKalle Valo 
5024e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
5025e705c121SKalle Valo out:
5026e705c121SKalle Valo 	return ret;
5027e705c121SKalle Valo }
5028e705c121SKalle Valo 
iwl_mvm_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)5029cbce62a3SMiri Korenblit int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
5030e705c121SKalle Valo 			struct ieee80211_chanctx_conf *ctx)
5031e705c121SKalle Valo {
5032e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5033e705c121SKalle Valo 
50343f312651SJohannes Berg 	guard(mvm)(mvm);
50353f312651SJohannes Berg 	return __iwl_mvm_add_chanctx(mvm, ctx);
5036e705c121SKalle Valo }
5037e705c121SKalle Valo 
__iwl_mvm_remove_chanctx(struct iwl_mvm * mvm,struct ieee80211_chanctx_conf * ctx)5038e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
5039e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
5040e705c121SKalle Valo {
5041e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5042e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5043e705c121SKalle Valo 
5044e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
5045e705c121SKalle Valo 
5046e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
5047e705c121SKalle Valo }
5048e705c121SKalle Valo 
iwl_mvm_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)5049cbce62a3SMiri Korenblit void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
5050e705c121SKalle Valo 			    struct ieee80211_chanctx_conf *ctx)
5051e705c121SKalle Valo {
5052e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5053e705c121SKalle Valo 
50543f312651SJohannes Berg 	guard(mvm)(mvm);
5055e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
5056e705c121SKalle Valo }
5057e705c121SKalle Valo 
iwl_mvm_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)5058cbce62a3SMiri Korenblit void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
5059cbce62a3SMiri Korenblit 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
5060e705c121SKalle Valo {
5061e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5062e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5063e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5064ce1fa3adSJohannes Berg 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
5065e705c121SKalle Valo 
5066e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
5067e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
5068e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
5069e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
5070e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
5071e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
5072e705c121SKalle Valo 		      phy_ctxt->ref, changed))
5073e705c121SKalle Valo 		return;
5074e705c121SKalle Valo 
50753f312651SJohannes Berg 	guard(mvm)(mvm);
50767a20bcceSEmmanuel Grumbach 
50777a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
50787a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
5079b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
50803f312651SJohannes Berg 			return;
50817a20bcceSEmmanuel Grumbach 
50827a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
50837a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
5084b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
50853f312651SJohannes Berg 			return;
50867a20bcceSEmmanuel Grumbach 	}
50877a20bcceSEmmanuel Grumbach 
5088e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
508932a5690eSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def, &ctx->ap,
5090e705c121SKalle Valo 				 ctx->rx_chains_static,
5091e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
5092e705c121SKalle Valo }
5093e705c121SKalle Valo 
50948a919a78SMiri Korenblit /*
50958a919a78SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
50968a919a78SMiri Korenblit  *
50978a919a78SMiri Korenblit  * Returns true if we're done assigning the chanctx
50988a919a78SMiri Korenblit  * (either on failure or success)
50998a919a78SMiri Korenblit  */
51004263ac7fSGregory Greenman static bool
__iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_chanctx_conf * ctx,bool switching_chanctx,int * ret)51014263ac7fSGregory Greenman __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
5102e705c121SKalle Valo 				    struct ieee80211_vif *vif,
5103e705c121SKalle Valo 				    struct ieee80211_chanctx_conf *ctx,
510450e81437SMiri Korenblit 				    bool switching_chanctx, int *ret)
5105e705c121SKalle Valo {
5106e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5107e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5108e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5109e705c121SKalle Valo 
5110e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
5111e705c121SKalle Valo 
5112650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = phy_ctxt;
5113e705c121SKalle Valo 
5114e705c121SKalle Valo 	switch (vif->type) {
5115e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5116e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
5117e705c121SKalle Valo 		if (switching_chanctx) {
5118e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
5119e705c121SKalle Valo 			break;
5120e705c121SKalle Valo 		}
51215a2abdcaSGustavo A. R. Silva 		fallthrough;
5122e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
5123e705c121SKalle Valo 		/*
5124e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
5125e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
5126e705c121SKalle Valo 		 */
51278a919a78SMiri Korenblit 		*ret = 0;
51288a919a78SMiri Korenblit 		return true;
5129e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
5130e705c121SKalle Valo 		break;
5131e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
5132e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
5133e705c121SKalle Valo 		mvmvif->ps_disabled = true;
5134e705c121SKalle Valo 		break;
5135e705c121SKalle Valo 	default:
51368a919a78SMiri Korenblit 		*ret = -EINVAL;
51378a919a78SMiri Korenblit 		return true;
5138e705c121SKalle Valo 	}
51398a919a78SMiri Korenblit 	return false;
51408a919a78SMiri Korenblit }
51418a919a78SMiri Korenblit 
__iwl_mvm_assign_vif_chanctx(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx,bool switching_chanctx)51428a919a78SMiri Korenblit static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
51438a919a78SMiri Korenblit 					struct ieee80211_vif *vif,
51444263ac7fSGregory Greenman 					struct ieee80211_bss_conf *link_conf,
51458a919a78SMiri Korenblit 					struct ieee80211_chanctx_conf *ctx,
51468a919a78SMiri Korenblit 					bool switching_chanctx)
51478a919a78SMiri Korenblit {
51488a919a78SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
51498a919a78SMiri Korenblit 	int ret;
51508a919a78SMiri Korenblit 
51514263ac7fSGregory Greenman 	if (WARN_ON(!link_conf))
51524263ac7fSGregory Greenman 		return -EINVAL;
51534263ac7fSGregory Greenman 
51548a919a78SMiri Korenblit 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
51558a919a78SMiri Korenblit 						switching_chanctx, &ret))
51568a919a78SMiri Korenblit 		goto out;
5157e705c121SKalle Valo 
5158e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
5159e705c121SKalle Valo 	if (ret)
5160e705c121SKalle Valo 		goto out;
5161e705c121SKalle Valo 
5162e705c121SKalle Valo 	/*
5163e705c121SKalle Valo 	 * Power state must be updated before quotas,
5164e705c121SKalle Valo 	 * otherwise fw will complain.
5165e705c121SKalle Valo 	 */
5166e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5167e705c121SKalle Valo 
5168e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
5169e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
5170e705c121SKalle Valo 	 * will handle quota settings.
5171e705c121SKalle Valo 	 */
5172e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5173e705c121SKalle Valo 		mvmvif->monitor_active = true;
5174e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
5175e705c121SKalle Valo 		if (ret)
5176e705c121SKalle Valo 			goto out_remove_binding;
51770e39eb03SChaya Rachel Ivgi 
51780e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
51790e39eb03SChaya Rachel Ivgi 		if (ret)
51800e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
51810e39eb03SChaya Rachel Ivgi 
5182e705c121SKalle Valo 	}
5183e705c121SKalle Valo 
5184e705c121SKalle Valo 	/* Handle binding during CSA */
5185e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
5186e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
5187e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
5188e705c121SKalle Valo 	}
5189e705c121SKalle Valo 
51908a919a78SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION) {
51918a919a78SMiri Korenblit 		if (!switching_chanctx) {
51928a919a78SMiri Korenblit 			mvmvif->csa_bcn_pending = false;
51938a919a78SMiri Korenblit 			goto out;
51948a919a78SMiri Korenblit 		}
51958a919a78SMiri Korenblit 
519674a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
519774a10252SSara Sharon 
519874a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
519974a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
520011d0d831SJohannes Berg 			u32 duration = 5 * vif->bss_conf.beacon_int;
5201e705c121SKalle Valo 
5202e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
5203e705c121SKalle Valo 			 * beacon on the new channel.
5204e705c121SKalle Valo 			 */
5205e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
5206e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
5207e705c121SKalle Valo 						true);
520874a10252SSara Sharon 		}
5209e705c121SKalle Valo 
5210e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
52116b82f4e1SJohannes Berg 
52126b82f4e1SJohannes Berg 		iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif,
52136b82f4e1SJohannes Berg 							link_conf,
52146b82f4e1SJohannes Berg 							false);
5215e705c121SKalle Valo 	}
5216e705c121SKalle Valo 
5217e705c121SKalle Valo 	goto out;
5218e705c121SKalle Valo 
5219e705c121SKalle Valo out_remove_binding:
5220e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
5221e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5222e705c121SKalle Valo out:
5223e705c121SKalle Valo 	if (ret)
5224650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = NULL;
5225e705c121SKalle Valo 	return ret;
5226e705c121SKalle Valo }
52278a919a78SMiri Korenblit 
iwl_mvm_assign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)5228e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
5229e705c121SKalle Valo 				      struct ieee80211_vif *vif,
5230727eff4dSGregory Greenman 				      struct ieee80211_bss_conf *link_conf,
5231e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
5232e705c121SKalle Valo {
5233e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5234e705c121SKalle Valo 
52353f312651SJohannes Berg 	guard(mvm)(mvm);
52363f312651SJohannes Berg 	return __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5237e705c121SKalle Valo }
5238e705c121SKalle Valo 
5239daddfae5SMiri Korenblit /*
5240daddfae5SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
5241daddfae5SMiri Korenblit  *
5242daddfae5SMiri Korenblit  * Returns if chanctx unassign chanctx is done
5243daddfae5SMiri Korenblit  * (either on failure or success)
5244daddfae5SMiri Korenblit  */
__iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm * mvm,struct ieee80211_vif * vif,bool switching_chanctx)52454263ac7fSGregory Greenman static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
5246e705c121SKalle Valo 						  struct ieee80211_vif *vif,
5247e705c121SKalle Valo 						  bool switching_chanctx)
5248e705c121SKalle Valo {
5249e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5250e705c121SKalle Valo 
5251e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
5252650cadb7SGregory Greenman 	iwl_mvm_remove_time_event(mvm, mvmvif,
5253650cadb7SGregory Greenman 				  &mvmvif->time_event_data);
5254e705c121SKalle Valo 
5255e705c121SKalle Valo 	switch (vif->type) {
5256e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
5257daddfae5SMiri Korenblit 		return true;
5258e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
5259e705c121SKalle Valo 		mvmvif->monitor_active = false;
5260e705c121SKalle Valo 		mvmvif->ps_disabled = false;
5261e705c121SKalle Valo 		break;
5262e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5263e705c121SKalle Valo 		/* This part is triggered only during CSA */
5264e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
5265daddfae5SMiri Korenblit 			return true;
5266e705c121SKalle Valo 
5267e705c121SKalle Valo 		mvmvif->csa_countdown = false;
5268e705c121SKalle Valo 
5269e705c121SKalle Valo 		/* Set CS bit on all the stations */
5270e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5271e705c121SKalle Valo 
5272e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
5273e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5274e705c121SKalle Valo 
5275e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
5276e705c121SKalle Valo 		break;
5277daddfae5SMiri Korenblit 	default:
5278e705c121SKalle Valo 		break;
5279daddfae5SMiri Korenblit 	}
5280daddfae5SMiri Korenblit 	return false;
5281daddfae5SMiri Korenblit }
5282e705c121SKalle Valo 
__iwl_mvm_unassign_vif_chanctx(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx,bool switching_chanctx)5283e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5284e705c121SKalle Valo 					   struct ieee80211_vif *vif,
52854263ac7fSGregory Greenman 					   struct ieee80211_bss_conf *link_conf,
5286e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
5287e705c121SKalle Valo 					   bool switching_chanctx)
5288e705c121SKalle Valo {
5289e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5290e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
5291e705c121SKalle Valo 
5292daddfae5SMiri Korenblit 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5293e705c121SKalle Valo 		goto out;
5294daddfae5SMiri Korenblit 
5295daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_MONITOR)
5296e705c121SKalle Valo 		iwl_mvm_rm_snif_sta(mvm, vif);
5297e705c121SKalle Valo 
5298e705c121SKalle Valo 
5299daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5300e705c121SKalle Valo 		disabled_vif = vif;
530174a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
530274a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5303e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5304e705c121SKalle Valo 	}
5305e705c121SKalle Valo 
5306e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5307e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
5308e705c121SKalle Valo 
5309e705c121SKalle Valo out:
5310bf544e9aSSara Sharon 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5311bf544e9aSSara Sharon 	    switching_chanctx)
5312bf544e9aSSara Sharon 		return;
5313650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
5314e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5315e705c121SKalle Valo }
5316e705c121SKalle Valo 
iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)5317e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5318e705c121SKalle Valo 					 struct ieee80211_vif *vif,
5319727eff4dSGregory Greenman 					 struct ieee80211_bss_conf *link_conf,
5320e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
5321e705c121SKalle Valo {
5322e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5323e705c121SKalle Valo 
53243f312651SJohannes Berg 	guard(mvm)(mvm);
53254263ac7fSGregory Greenman 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5326e705c121SKalle Valo }
5327e705c121SKalle Valo 
5328e705c121SKalle Valo static int
iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm * mvm,struct ieee80211_vif_chanctx_switch * vifs,const struct iwl_mvm_switch_vif_chanctx_ops * ops)5329e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5330660eba5aSMiri Korenblit 				struct ieee80211_vif_chanctx_switch *vifs,
5331a2906ea6SJohannes Berg 				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5332e705c121SKalle Valo {
5333e705c121SKalle Valo 	int ret;
5334e705c121SKalle Valo 
53353f312651SJohannes Berg 	guard(mvm)(mvm);
53364263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
53374263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5338e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5339e705c121SKalle Valo 
5340e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5341e705c121SKalle Valo 	if (ret) {
5342e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5343e705c121SKalle Valo 		goto out_reassign;
5344e705c121SKalle Valo 	}
5345e705c121SKalle Valo 
53464263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
53474263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5348e705c121SKalle Valo 	if (ret) {
5349e705c121SKalle Valo 		IWL_ERR(mvm,
5350e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5351e705c121SKalle Valo 		goto out_remove;
5352e705c121SKalle Valo 	}
5353e705c121SKalle Valo 
5354e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
5355e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5356e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
5357e705c121SKalle Valo 
53583f312651SJohannes Berg 	return 0;
5359e705c121SKalle Valo 
5360e705c121SKalle Valo out_remove:
5361e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5362e705c121SKalle Valo 
5363e705c121SKalle Valo out_reassign:
5364e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5365e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5366e705c121SKalle Valo 		goto out_restart;
5367e705c121SKalle Valo 	}
5368e705c121SKalle Valo 
53694263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
53704263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5371e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5372e705c121SKalle Valo 		goto out_restart;
5373e705c121SKalle Valo 	}
5374e705c121SKalle Valo 
53753f312651SJohannes Berg 	return ret;
5376e705c121SKalle Valo 
5377e705c121SKalle Valo out_restart:
5378e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5379e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5380e705c121SKalle Valo 	return ret;
5381e705c121SKalle Valo }
5382e705c121SKalle Valo 
5383e705c121SKalle Valo static int
iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm * mvm,struct ieee80211_vif_chanctx_switch * vifs,const struct iwl_mvm_switch_vif_chanctx_ops * ops)5384e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5385660eba5aSMiri Korenblit 				    struct ieee80211_vif_chanctx_switch *vifs,
5386a2906ea6SJohannes Berg 				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5387e705c121SKalle Valo {
5388e705c121SKalle Valo 	int ret;
5389e705c121SKalle Valo 
53903f312651SJohannes Berg 	guard(mvm)(mvm);
53914263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
53924263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5393e705c121SKalle Valo 
53944263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
53954263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5396e705c121SKalle Valo 	if (ret) {
5397e705c121SKalle Valo 		IWL_ERR(mvm,
5398e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5399e705c121SKalle Valo 		goto out_reassign;
5400e705c121SKalle Valo 	}
5401e705c121SKalle Valo 
54023f312651SJohannes Berg 	return 0;
5403e705c121SKalle Valo 
5404e705c121SKalle Valo out_reassign:
54054263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
54064263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5407e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5408e705c121SKalle Valo 		goto out_restart;
5409e705c121SKalle Valo 	}
5410e705c121SKalle Valo 
54113f312651SJohannes Berg 	return ret;
5412e705c121SKalle Valo 
5413e705c121SKalle Valo out_restart:
5414e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5415e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5416e705c121SKalle Valo 	return ret;
5417e705c121SKalle Valo }
5418e705c121SKalle Valo 
5419660eba5aSMiri Korenblit /* Execute the common part for both MLD and non-MLD modes */
5420660eba5aSMiri Korenblit int
iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw * hw,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs,enum ieee80211_chanctx_switch_mode mode,const struct iwl_mvm_switch_vif_chanctx_ops * ops)5421660eba5aSMiri Korenblit iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5422e705c121SKalle Valo 				  struct ieee80211_vif_chanctx_switch *vifs,
5423e705c121SKalle Valo 				  int n_vifs,
5424660eba5aSMiri Korenblit 				  enum ieee80211_chanctx_switch_mode mode,
5425a2906ea6SJohannes Berg 				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5426e705c121SKalle Valo {
5427e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5428e705c121SKalle Valo 	int ret;
5429e705c121SKalle Valo 
5430e705c121SKalle Valo 	/* we only support a single-vif right now */
5431e705c121SKalle Valo 	if (n_vifs > 1)
5432e705c121SKalle Valo 		return -EOPNOTSUPP;
5433e705c121SKalle Valo 
5434e705c121SKalle Valo 	switch (mode) {
5435e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5436660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5437e705c121SKalle Valo 		break;
5438e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
5439660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5440e705c121SKalle Valo 		break;
5441e705c121SKalle Valo 	default:
5442e705c121SKalle Valo 		ret = -EOPNOTSUPP;
5443e705c121SKalle Valo 		break;
5444e705c121SKalle Valo 	}
5445e705c121SKalle Valo 
5446e705c121SKalle Valo 	return ret;
5447e705c121SKalle Valo }
5448e705c121SKalle Valo 
iwl_mvm_switch_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs,enum ieee80211_chanctx_switch_mode mode)5449660eba5aSMiri Korenblit static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5450660eba5aSMiri Korenblit 				      struct ieee80211_vif_chanctx_switch *vifs,
5451660eba5aSMiri Korenblit 				      int n_vifs,
5452660eba5aSMiri Korenblit 				      enum ieee80211_chanctx_switch_mode mode)
5453660eba5aSMiri Korenblit {
5454a2906ea6SJohannes Berg 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5455660eba5aSMiri Korenblit 		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5456660eba5aSMiri Korenblit 		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5457660eba5aSMiri Korenblit 	};
5458660eba5aSMiri Korenblit 
5459660eba5aSMiri Korenblit 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5460660eba5aSMiri Korenblit }
5461660eba5aSMiri Korenblit 
iwl_mvm_tx_last_beacon(struct ieee80211_hw * hw)5462cbce62a3SMiri Korenblit int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
54632f0282dbSJohannes Berg {
54642f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
54652f0282dbSJohannes Berg 
54662f0282dbSJohannes Berg 	return mvm->ibss_manager;
54672f0282dbSJohannes Berg }
54682f0282dbSJohannes Berg 
iwl_mvm_set_tim(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool set)5469a32973eeSJohannes Berg static int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5470e705c121SKalle Valo 			   bool set)
5471e705c121SKalle Valo {
5472e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5473e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5474e705c121SKalle Valo 
5475e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
5476e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
5477e705c121SKalle Valo 		return -EINVAL;
5478e705c121SKalle Valo 	}
5479e705c121SKalle Valo 
548036cf5377SGregory Greenman 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
548136cf5377SGregory Greenman 					       &mvm_sta->vif->bss_conf);
5482e705c121SKalle Valo }
5483e705c121SKalle Valo 
5484e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
5485e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5486e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5487e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5488e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5489e705c121SKalle Valo };
5490e705c121SKalle Valo 
__iwl_mvm_mac_testmode_cmd(struct iwl_mvm * mvm,struct ieee80211_vif * vif,void * data,int len)5491e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5492e705c121SKalle Valo 				      struct ieee80211_vif *vif,
5493e705c121SKalle Valo 				      void *data, int len)
5494e705c121SKalle Valo {
5495e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5496e705c121SKalle Valo 	int err;
5497e705c121SKalle Valo 	u32 noa_duration;
5498e705c121SKalle Valo 
54998cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
55008cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
5501e705c121SKalle Valo 	if (err)
5502e705c121SKalle Valo 		return err;
5503e705c121SKalle Valo 
5504e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
5505e705c121SKalle Valo 		return -EINVAL;
5506e705c121SKalle Valo 
5507e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5508e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
5509e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5510e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
5511e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5512e705c121SKalle Valo 			return -EINVAL;
5513e705c121SKalle Valo 
5514e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5515e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
5516e705c121SKalle Valo 			return -EINVAL;
5517e705c121SKalle Valo 
5518e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
5519e705c121SKalle Valo 		mvm->noa_vif = vif;
5520e705c121SKalle Valo 
552122b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
5522e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5523e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
5524e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5525f276e20bSJohannes Berg 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5526e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5527e705c121SKalle Valo 			return -EINVAL;
5528e705c121SKalle Valo 
5529e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
553087f5b5f2SJohannes Berg 			return iwl_mvm_enable_beacon_filter(mvm, vif);
553187f5b5f2SJohannes Berg 		return iwl_mvm_disable_beacon_filter(mvm, vif);
5532e705c121SKalle Valo 	}
5533e705c121SKalle Valo 
5534e705c121SKalle Valo 	return -EOPNOTSUPP;
5535e705c121SKalle Valo }
5536e705c121SKalle Valo 
iwl_mvm_mac_testmode_cmd(struct ieee80211_hw * hw,struct ieee80211_vif * vif,void * data,int len)5537cbce62a3SMiri Korenblit int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5538e705c121SKalle Valo 			     struct ieee80211_vif *vif,
5539e705c121SKalle Valo 			     void *data, int len)
5540e705c121SKalle Valo {
5541e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5542e705c121SKalle Valo 
55433f312651SJohannes Berg 	guard(mvm)(mvm);
55443f312651SJohannes Berg 	return __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5545e705c121SKalle Valo }
5546e705c121SKalle Valo #endif
5547e705c121SKalle Valo 
iwl_mvm_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)5548cbce62a3SMiri Korenblit void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5549e705c121SKalle Valo 			    struct ieee80211_channel_switch *chsw)
5550e705c121SKalle Valo {
5551e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
5552e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
5553e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
5554e705c121SKalle Valo 	 * (which is when the absence time event starts).
5555e705c121SKalle Valo 	 */
5556e705c121SKalle Valo 
5557e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5558e705c121SKalle Valo 			   "dummy channel switch op\n");
5559e705c121SKalle Valo }
5560e705c121SKalle Valo 
iwl_mvm_schedule_client_csa(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)556174a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
556274a10252SSara Sharon 				       struct ieee80211_vif *vif,
556374a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
556474a10252SSara Sharon {
556574a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
556674a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
556774a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
556874a10252SSara Sharon 							  mvmvif->color)),
556974a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
557074a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
557174a10252SSara Sharon 		.cs_count = chsw->count,
557277738865SSara Sharon 		.cs_mode = chsw->block_tx,
557374a10252SSara Sharon 	};
557474a10252SSara Sharon 
557574a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
557674a10252SSara Sharon 
55779cfcf71cSSara Sharon 	if (chsw->delay)
55789cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
55799cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
55809cfcf71cSSara Sharon 
558174a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
558274a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
558374a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
558474a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
558574a10252SSara Sharon }
558674a10252SSara Sharon 
iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)55870202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
55880202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
55890202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
55900202bcf0SEmmanuel Grumbach {
55910202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
55920202bcf0SEmmanuel Grumbach 	u32 apply_time;
55930202bcf0SEmmanuel Grumbach 
55940202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
55950202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
55960202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
55970202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
55980202bcf0SEmmanuel Grumbach 	 * loss).
55990202bcf0SEmmanuel Grumbach 	 */
56000202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
56010202bcf0SEmmanuel Grumbach 		apply_time = 0;
56020202bcf0SEmmanuel Grumbach 	else
56030202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
56040202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
56050202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
56060202bcf0SEmmanuel Grumbach 
56070202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
56080202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
56090202bcf0SEmmanuel Grumbach 
5610c1e458b9SIlan Peer 	if (mvmvif->bf_enabled) {
561187f5b5f2SJohannes Berg 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif);
56120202bcf0SEmmanuel Grumbach 
56130202bcf0SEmmanuel Grumbach 		if (ret)
56140202bcf0SEmmanuel Grumbach 			return ret;
56150202bcf0SEmmanuel Grumbach 	}
56160202bcf0SEmmanuel Grumbach 
56170202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
56180202bcf0SEmmanuel Grumbach 				    apply_time);
56190202bcf0SEmmanuel Grumbach 
56200202bcf0SEmmanuel Grumbach 	return 0;
56210202bcf0SEmmanuel Grumbach }
56220202bcf0SEmmanuel Grumbach 
iwl_mvm_csa_block_txqs(void * data,struct ieee80211_sta * sta)5623bbd6d0f8SJohannes Berg static void iwl_mvm_csa_block_txqs(void *data, struct ieee80211_sta *sta)
5624bbd6d0f8SJohannes Berg {
5625bbd6d0f8SJohannes Berg 	int i;
5626bbd6d0f8SJohannes Berg 
5627bbd6d0f8SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
5628bbd6d0f8SJohannes Berg 		struct iwl_mvm_txq *mvmtxq =
5629bbd6d0f8SJohannes Berg 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
5630bbd6d0f8SJohannes Berg 
5631bbd6d0f8SJohannes Berg 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5632bbd6d0f8SJohannes Berg 	}
5633bbd6d0f8SJohannes Berg }
5634bbd6d0f8SJohannes Berg 
5635f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
iwl_mvm_pre_channel_switch(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)56362f876f91SJohannes Berg int iwl_mvm_pre_channel_switch(struct iwl_mvm *mvm,
5637e705c121SKalle Valo 			       struct ieee80211_vif *vif,
5638e705c121SKalle Valo 			       struct ieee80211_channel_switch *chsw)
5639e705c121SKalle Valo {
5640e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
5641e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5642bbd6d0f8SJohannes Berg 	struct iwl_mvm_txq *mvmtxq;
5643e705c121SKalle Valo 	int ret;
5644e705c121SKalle Valo 
56452f876f91SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
5646e705c121SKalle Valo 
5647e705c121SKalle Valo 	mvmvif->csa_failed = false;
56482964b57fSJohannes Berg 	mvmvif->csa_blocks_tx = false;
5649e705c121SKalle Valo 
5650e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5651e705c121SKalle Valo 			   chsw->chandef.center_freq1);
5652e705c121SKalle Valo 
56537174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
56547174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
56557174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5656e705c121SKalle Valo 
5657e705c121SKalle Valo 	switch (vif->type) {
5658e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5659e705c121SKalle Valo 		csa_vif =
5660e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
5661e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
5662d0a9123eSJohannes Berg 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
56632f876f91SJohannes Berg 			      "Another CSA is already in progress"))
56642f876f91SJohannes Berg 			return -EBUSY;
5665e705c121SKalle Valo 
5666d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
5667d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
56682f876f91SJohannes Berg 					      lockdep_is_held(&mvm->mutex)))
56692f876f91SJohannes Berg 			return -EBUSY;
5670d3a108a4SAndrei Otcheretianski 
5671e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
5672e705c121SKalle Valo 
5673e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
56742f876f91SJohannes Berg 			      "Previous CSA countdown didn't complete"))
56752f876f91SJohannes Berg 			return -EBUSY;
5676e705c121SKalle Valo 
5677d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5678d3a108a4SAndrei Otcheretianski 
5679bbd6d0f8SJohannes Berg 		if (!chsw->block_tx)
5680bbd6d0f8SJohannes Berg 			break;
5681bbd6d0f8SJohannes Berg 		/* don't need blocking in driver otherwise - mac80211 will do */
5682bbd6d0f8SJohannes Berg 		if (!ieee80211_hw_check(mvm->hw, HANDLES_QUIET_CSA))
5683bbd6d0f8SJohannes Berg 			break;
5684bbd6d0f8SJohannes Berg 
5685bbd6d0f8SJohannes Berg 		mvmvif->csa_blocks_tx = true;
5686bbd6d0f8SJohannes Berg 		mvmtxq = iwl_mvm_txq_from_mac80211(vif->txq);
5687bbd6d0f8SJohannes Berg 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5688bbd6d0f8SJohannes Berg 		ieee80211_iterate_stations_atomic(mvm->hw,
5689bbd6d0f8SJohannes Berg 						  iwl_mvm_csa_block_txqs,
5690bbd6d0f8SJohannes Berg 						  NULL);
5691e705c121SKalle Valo 		break;
5692e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
56932964b57fSJohannes Berg 		mvmvif->csa_blocks_tx = chsw->block_tx;
56942964b57fSJohannes Berg 
56952360acbdSEmmanuel Grumbach 		/*
5696ad12b231SNathan Errera 		 * In the new flow FW is in charge of timing the switch so there
5697ad12b231SNathan Errera 		 * is no need for all of this
5698ad12b231SNathan Errera 		 */
5699ad12b231SNathan Errera 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5700ad12b231SNathan Errera 					    CHANNEL_SWITCH_ERROR_NOTIF,
5701ad12b231SNathan Errera 					    0))
5702ad12b231SNathan Errera 			break;
5703ad12b231SNathan Errera 
5704ad12b231SNathan Errera 		/*
57052360acbdSEmmanuel Grumbach 		 * We haven't configured the firmware to be associated yet since
57062360acbdSEmmanuel Grumbach 		 * we don't know the dtim period. In this case, the firmware can't
57072360acbdSEmmanuel Grumbach 		 * track the beacons.
57082360acbdSEmmanuel Grumbach 		 */
57092f876f91SJohannes Berg 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period)
57102f876f91SJohannes Berg 			return -EBUSY;
57112360acbdSEmmanuel Grumbach 
5712aee2eac7SJohannes Berg 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX &&
5713aee2eac7SJohannes Berg 		    hweight16(vif->valid_links) <= 1)
571487d9564eSJohannes Berg 			schedule_delayed_work(&mvmvif->csa_work, 0);
571587d9564eSJohannes Berg 
5716f6780614SSara Sharon 		if (chsw->block_tx) {
5717f6780614SSara Sharon 			/*
5718f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
5719f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
5720f6780614SSara Sharon 			 */
5721f6780614SSara Sharon 			if (!chsw->count ||
5722f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
5723f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
5724f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
5725f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5726f6780614SSara Sharon 		}
5727e705c121SKalle Valo 
57280202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
57290202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
57300202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5731e705c121SKalle Valo 			if (ret)
57322f876f91SJohannes Berg 				return ret;
57330202bcf0SEmmanuel Grumbach 		} else {
573474a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
57350202bcf0SEmmanuel Grumbach 		}
573681b4e44eSSara Sharon 
573781b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
573881b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
5739e705c121SKalle Valo 		break;
5740e705c121SKalle Valo 	default:
5741e705c121SKalle Valo 		break;
5742e705c121SKalle Valo 	}
5743e705c121SKalle Valo 
5744e705c121SKalle Valo 	mvmvif->ps_disabled = true;
5745e705c121SKalle Valo 
5746e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
5747e705c121SKalle Valo 	if (ret)
57482f876f91SJohannes Berg 		return ret;
5749e705c121SKalle Valo 
5750e705c121SKalle Valo 	/* we won't be on this channel any longer */
5751e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
5752e705c121SKalle Valo 
57532f876f91SJohannes Berg 	return ret;
57542f876f91SJohannes Berg }
57552f876f91SJohannes Berg 
iwl_mvm_mac_pre_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)57562f876f91SJohannes Berg static int iwl_mvm_mac_pre_channel_switch(struct ieee80211_hw *hw,
57572f876f91SJohannes Berg 					  struct ieee80211_vif *vif,
57582f876f91SJohannes Berg 					  struct ieee80211_channel_switch *chsw)
57592f876f91SJohannes Berg {
57602f876f91SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
57612f876f91SJohannes Berg 
57623f312651SJohannes Berg 	guard(mvm)(mvm);
57633f312651SJohannes Berg 	return iwl_mvm_pre_channel_switch(mvm, vif, chsw);
5764e705c121SKalle Valo }
5765e705c121SKalle Valo 
iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)5766cbce62a3SMiri Korenblit void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5767c37763d2SSara Sharon 				      struct ieee80211_vif *vif,
5768c37763d2SSara Sharon 				      struct ieee80211_channel_switch *chsw)
5769e705c121SKalle Valo {
5770e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5771c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5772c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
5773c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5774c37763d2SSara Sharon 							  mvmvif->color)),
5775c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5776c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
5777c37763d2SSara Sharon 		.cs_count = chsw->count,
577877738865SSara Sharon 		.cs_mode = chsw->block_tx,
5779c37763d2SSara Sharon 	};
5780e705c121SKalle Valo 
5781ad12b231SNathan Errera 	/*
5782ad12b231SNathan Errera 	 * In the new flow FW is in charge of timing the switch so there is no
5783ad12b231SNathan Errera 	 * need for all of this
5784ad12b231SNathan Errera 	 */
5785ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5786ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5787ad12b231SNathan Errera 		return;
5788ad12b231SNathan Errera 
5789c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5790c37763d2SSara Sharon 		return;
5791e705c121SKalle Valo 
5792c3eae059SGregory Greenman 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5793c3eae059SGregory Greenman 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5794c3eae059SGregory Greenman 
579581b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
579681b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
57975ecd5d82SJohannes Berg 			struct ieee80211_bss_conf *link_conf;
57985ecd5d82SJohannes Berg 
579981b4e44eSSara Sharon 			/* Second time, give up on this AP*/
58005ecd5d82SJohannes Berg 
58015ecd5d82SJohannes Berg 			link_conf = wiphy_dereference(hw->wiphy,
58025ecd5d82SJohannes Berg 						      vif->link_conf[chsw->link_id]);
58035ecd5d82SJohannes Berg 			if (WARN_ON(!link_conf))
58045ecd5d82SJohannes Berg 				return;
58055ecd5d82SJohannes Berg 
58065ecd5d82SJohannes Berg 			iwl_mvm_abort_channel_switch(hw, vif, link_conf);
5807a469a593SEmmanuel Grumbach 			ieee80211_chswitch_done(vif, false, 0);
580881b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
580981b4e44eSSara Sharon 			return;
5810e705c121SKalle Valo 		}
581181b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
5812e705c121SKalle Valo 	}
581381b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
5814e705c121SKalle Valo 
58153f312651SJohannes Berg 	guard(mvm)(mvm);
5816caf46377SSara Sharon 	if (mvmvif->csa_failed)
58173f312651SJohannes Berg 		return;
5818e705c121SKalle Valo 
5819c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5820c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
5821c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5822caf46377SSara Sharon 				     0, sizeof(cmd), &cmd));
5823e705c121SKalle Valo }
5824e705c121SKalle Valo 
iwl_mvm_flush_no_vif(struct iwl_mvm * mvm,u32 queues,bool drop)58256110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
58266110d9e5SDavid Spinadel {
5827435d0827SSara Sharon 	int i;
5828435d0827SSara Sharon 
582906195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
58303a84454fSEmmanuel Grumbach 		/* we can't ask the firmware anything if it is dead */
58313a84454fSEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
58323a84454fSEmmanuel Grumbach 			     &mvm->status))
58333a84454fSEmmanuel Grumbach 			return;
5834309c4848SLuca Coelho 		if (drop) {
58353f312651SJohannes Berg 			guard(mvm)(mvm);
58366110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
5837d4e3a341SMordechay Goodstein 				iwl_mvm_flushable_queues(mvm) & queues);
5838309c4848SLuca Coelho 		} else {
5839435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5840309c4848SLuca Coelho 		}
5841435d0827SSara Sharon 		return;
5842435d0827SSara Sharon 	}
58436110d9e5SDavid Spinadel 
58443f312651SJohannes Berg 	guard(mvm)(mvm);
5845be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5846435d0827SSara Sharon 		struct ieee80211_sta *sta;
5847435d0827SSara Sharon 
5848435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
58496110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
58506110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
58516110d9e5SDavid Spinadel 			continue;
58526110d9e5SDavid Spinadel 
585306195639SSara Sharon 		if (drop)
5854d4e3a341SMordechay Goodstein 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
585506195639SSara Sharon 		else
58566110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
58576110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
58586110d9e5SDavid Spinadel 	}
58596110d9e5SDavid Spinadel }
58606110d9e5SDavid Spinadel 
iwl_mvm_mac_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)5861cbce62a3SMiri Korenblit void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5862cbce62a3SMiri Korenblit 		       u32 queues, bool drop)
5863e705c121SKalle Valo {
58642964b57fSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5865e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5866e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
5867e705c121SKalle Valo 	struct ieee80211_sta *sta;
5868d3f9cd61SJohannes Berg 	bool ap_sta_done = false;
5869e705c121SKalle Valo 	int i;
5870e705c121SKalle Valo 	u32 msk = 0;
5871e705c121SKalle Valo 
58726110d9e5SDavid Spinadel 	if (!vif) {
58736110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
58746110d9e5SDavid Spinadel 		return;
58756110d9e5SDavid Spinadel 	}
58766110d9e5SDavid Spinadel 
58772964b57fSJohannes Berg 	if (!drop && hweight16(vif->active_links) <= 1) {
58782964b57fSJohannes Berg 		int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
58792964b57fSJohannes Berg 		struct ieee80211_bss_conf *link_conf;
58802964b57fSJohannes Berg 
58812964b57fSJohannes Berg 		link_conf = wiphy_dereference(hw->wiphy,
58822964b57fSJohannes Berg 					      vif->link_conf[link_id]);
58832964b57fSJohannes Berg 		if (WARN_ON(!link_conf))
58842964b57fSJohannes Berg 			return;
58852964b57fSJohannes Berg 		if (link_conf->csa_active && mvmvif->csa_blocks_tx)
58862964b57fSJohannes Berg 			drop = true;
58872964b57fSJohannes Berg 	}
58882964b57fSJohannes Berg 
588924afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
589024afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
589124afba76SLiad Kaufman 
5892e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5893e705c121SKalle Valo 
5894e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
5895be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5896e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5897e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
5898e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
5899e705c121SKalle Valo 			continue;
5900e705c121SKalle Valo 
5901e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5902e705c121SKalle Valo 		if (mvmsta->vif != vif)
5903e705c121SKalle Valo 			continue;
5904e705c121SKalle Valo 
5905d3f9cd61SJohannes Berg 		if (sta == mvmvif->ap_sta) {
5906d3f9cd61SJohannes Berg 			if (ap_sta_done)
5907d3f9cd61SJohannes Berg 				continue;
5908d3f9cd61SJohannes Berg 			ap_sta_done = true;
5909d3f9cd61SJohannes Berg 		}
5910d3f9cd61SJohannes Berg 
5911d49394a1SSara Sharon 		if (drop) {
591239176296SJohannes Berg 			if (iwl_mvm_flush_sta(mvm, mvmsta->deflink.sta_id,
591339176296SJohannes Berg 					      mvmsta->tfd_queue_msk))
5914d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
5915d49394a1SSara Sharon 		} else {
5916d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
5917d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5918f7bd883bSJohannes Berg 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5919f7bd883bSJohannes Berg 				msk |= mvmsta->tfd_queue_msk;
5920e705c121SKalle Valo 		}
5921d49394a1SSara Sharon 	}
5922e705c121SKalle Valo 
5923e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5924e705c121SKalle Valo 
5925e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
5926e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
59273a84454fSEmmanuel Grumbach 	 * If the firmware is dead, this can't work...
5928e705c121SKalle Valo 	 */
59293a84454fSEmmanuel Grumbach 	if (!drop && !iwl_mvm_has_new_tx_api(mvm) &&
59303a84454fSEmmanuel Grumbach 	    !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
59313a84454fSEmmanuel Grumbach 		      &mvm->status))
5932a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5933e705c121SKalle Valo }
5934e705c121SKalle Valo 
iwl_mvm_mac_flush_sta(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)5935a6cc6ccbSJohannes Berg void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5936a6cc6ccbSJohannes Berg 			   struct ieee80211_sta *sta)
5937a6cc6ccbSJohannes Berg {
593843874283SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5939a6cc6ccbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
594043874283SJohannes Berg 	struct iwl_mvm_link_sta *mvm_link_sta;
594143874283SJohannes Berg 	struct ieee80211_link_sta *link_sta;
594243874283SJohannes Berg 	int link_id;
5943a6cc6ccbSJohannes Berg 
59443f312651SJohannes Berg 	guard(mvm)(mvm);
594543874283SJohannes Berg 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
594643874283SJohannes Berg 		mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_id],
5947a6cc6ccbSJohannes Berg 							 lockdep_is_held(&mvm->mutex));
594843874283SJohannes Berg 		if (!mvm_link_sta)
5949a6cc6ccbSJohannes Berg 			continue;
5950a6cc6ccbSJohannes Berg 
595143874283SJohannes Berg 		if (iwl_mvm_flush_sta(mvm, mvm_link_sta->sta_id,
595239176296SJohannes Berg 				      mvmsta->tfd_queue_msk))
5953a6cc6ccbSJohannes Berg 			IWL_ERR(mvm, "flush request fail\n");
5954a6cc6ccbSJohannes Berg 	}
5955a6cc6ccbSJohannes Berg }
5956a6cc6ccbSJohannes Berg 
iwl_mvm_mac_get_acs_survey(struct iwl_mvm * mvm,int idx,struct survey_info * survey)5957fc612222SBenjamin Berg static int iwl_mvm_mac_get_acs_survey(struct iwl_mvm *mvm, int idx,
5958fc612222SBenjamin Berg 				      struct survey_info *survey)
5959fc612222SBenjamin Berg {
5960fc612222SBenjamin Berg 	int chan_idx;
5961fc612222SBenjamin Berg 	enum nl80211_band band;
5962fc612222SBenjamin Berg 	int ret;
5963fc612222SBenjamin Berg 
5964fc612222SBenjamin Berg 	mutex_lock(&mvm->mutex);
5965fc612222SBenjamin Berg 
5966fc612222SBenjamin Berg 	if (!mvm->acs_survey) {
5967fc612222SBenjamin Berg 		ret = -ENOENT;
5968fc612222SBenjamin Berg 		goto out;
5969fc612222SBenjamin Berg 	}
5970fc612222SBenjamin Berg 
5971fc612222SBenjamin Berg 	/* Find and return the next entry that has a non-zero active time */
5972fc612222SBenjamin Berg 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5973fc612222SBenjamin Berg 		struct ieee80211_supported_band *sband =
5974fc612222SBenjamin Berg 			mvm->hw->wiphy->bands[band];
5975fc612222SBenjamin Berg 
5976fc612222SBenjamin Berg 		if (!sband)
5977fc612222SBenjamin Berg 			continue;
5978fc612222SBenjamin Berg 
5979fc612222SBenjamin Berg 		for (chan_idx = 0; chan_idx < sband->n_channels; chan_idx++) {
5980fc612222SBenjamin Berg 			struct iwl_mvm_acs_survey_channel *info =
5981fc612222SBenjamin Berg 				&mvm->acs_survey->bands[band][chan_idx];
5982fc612222SBenjamin Berg 
5983fc612222SBenjamin Berg 			if (!info->time)
5984fc612222SBenjamin Berg 				continue;
5985fc612222SBenjamin Berg 
5986fc612222SBenjamin Berg 			/* Found (the next) channel to report */
5987fc612222SBenjamin Berg 			survey->channel = &sband->channels[chan_idx];
5988fc612222SBenjamin Berg 			survey->filled = SURVEY_INFO_TIME |
5989fc612222SBenjamin Berg 					 SURVEY_INFO_TIME_BUSY |
5990fc612222SBenjamin Berg 					 SURVEY_INFO_TIME_RX |
5991fc612222SBenjamin Berg 					 SURVEY_INFO_TIME_TX;
5992fc612222SBenjamin Berg 			survey->time = info->time;
5993fc612222SBenjamin Berg 			survey->time_busy = info->time_busy;
5994fc612222SBenjamin Berg 			survey->time_rx = info->time_rx;
5995fc612222SBenjamin Berg 			survey->time_tx = info->time_tx;
5996fc612222SBenjamin Berg 			survey->noise = info->noise;
5997fc612222SBenjamin Berg 			if (survey->noise < 0)
5998fc612222SBenjamin Berg 				survey->filled |= SURVEY_INFO_NOISE_DBM;
5999fc612222SBenjamin Berg 
6000fc612222SBenjamin Berg 			/* Clear time so that channel is only reported once */
6001fc612222SBenjamin Berg 			info->time = 0;
6002fc612222SBenjamin Berg 
6003fc612222SBenjamin Berg 			ret = 0;
6004fc612222SBenjamin Berg 			goto out;
6005fc612222SBenjamin Berg 		}
6006fc612222SBenjamin Berg 	}
6007fc612222SBenjamin Berg 
6008fc612222SBenjamin Berg 	ret = -ENOENT;
6009fc612222SBenjamin Berg 
6010fc612222SBenjamin Berg out:
6011fc612222SBenjamin Berg 	mutex_unlock(&mvm->mutex);
6012fc612222SBenjamin Berg 
6013fc612222SBenjamin Berg 	return ret;
6014fc612222SBenjamin Berg }
6015fc612222SBenjamin Berg 
iwl_mvm_mac_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)6016cbce62a3SMiri Korenblit int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
6017e705c121SKalle Valo 			   struct survey_info *survey)
6018e705c121SKalle Valo {
6019e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6020b6e3d1baSAnjaneyulu 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
6021b6e3d1baSAnjaneyulu 					   WIDE_ID(SYSTEM_GROUP,
6022b6e3d1baSAnjaneyulu 						   SYSTEM_STATISTICS_CMD),
6023b6e3d1baSAnjaneyulu 					   IWL_FW_CMD_VER_UNKNOWN);
6024e705c121SKalle Valo 
6025e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
6026e705c121SKalle Valo 
6027280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
6028e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
6029e705c121SKalle Valo 		return -ENOENT;
6030e705c121SKalle Valo 
6031fc612222SBenjamin Berg 	/*
6032fc612222SBenjamin Berg 	 * Return the beacon stats at index zero and pass on following indices
6033fc612222SBenjamin Berg 	 * to the function returning the full survey, most likely for ACS
6034fc612222SBenjamin Berg 	 * (Automatic Channel Selection).
6035fc612222SBenjamin Berg 	 */
6036fc612222SBenjamin Berg 	if (idx > 0)
6037fc612222SBenjamin Berg 		return iwl_mvm_mac_get_acs_survey(mvm, idx - 1, survey);
6038fc612222SBenjamin Berg 
60393f312651SJohannes Berg 	guard(mvm)(mvm);
6040e705c121SKalle Valo 
6041aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
60423f312651SJohannes Berg 		int ret = iwl_mvm_request_statistics(mvm, false);
60433f312651SJohannes Berg 
6044e705c121SKalle Valo 		if (ret)
60453f312651SJohannes Berg 			return ret;
6046e705c121SKalle Valo 	}
6047e705c121SKalle Valo 
6048b6e3d1baSAnjaneyulu 	survey->filled = SURVEY_INFO_TIME_RX |
6049b6e3d1baSAnjaneyulu 			 SURVEY_INFO_TIME_TX;
6050e705c121SKalle Valo 
6051e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
6052e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
6053e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
6054e705c121SKalle Valo 
6055e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
6056e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
6057e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
6058e705c121SKalle Valo 
6059b6e3d1baSAnjaneyulu 	/* the new fw api doesn't support the following fields */
6060b6e3d1baSAnjaneyulu 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
60613f312651SJohannes Berg 		return 0;
6062b6e3d1baSAnjaneyulu 
6063b6e3d1baSAnjaneyulu 	survey->filled |= SURVEY_INFO_TIME |
6064b6e3d1baSAnjaneyulu 			  SURVEY_INFO_TIME_SCAN;
6065b6e3d1baSAnjaneyulu 	survey->time = mvm->accu_radio_stats.on_time_rf +
6066b6e3d1baSAnjaneyulu 		       mvm->radio_stats.on_time_rf;
6067b6e3d1baSAnjaneyulu 	do_div(survey->time, USEC_PER_MSEC);
6068b6e3d1baSAnjaneyulu 
6069e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
6070e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
6071e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
6072e705c121SKalle Valo 
60733f312651SJohannes Berg 	return 0;
6074e705c121SKalle Valo }
6075e705c121SKalle Valo 
iwl_mvm_set_sta_rate(u32 rate_n_flags,struct rate_info * rinfo)6076ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
6077ed780545SJohannes Berg {
607882cdbd11SMiri Korenblit 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
60797138763eSJohannes Berg 	u32 gi_ltf;
608082cdbd11SMiri Korenblit 
608182cdbd11SMiri Korenblit 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
6082ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
6083ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
6084ed780545SJohannes Berg 		break;
6085ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
6086ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
6087ed780545SJohannes Berg 		break;
6088ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
6089ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
6090ed780545SJohannes Berg 		break;
6091ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
6092ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
6093ed780545SJohannes Berg 		break;
609423dcee94SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_320:
609523dcee94SJohannes Berg 		rinfo->bw = RATE_INFO_BW_320;
609623dcee94SJohannes Berg 		break;
6097ed780545SJohannes Berg 	}
6098ed780545SJohannes Berg 
609982cdbd11SMiri Korenblit 	if (format == RATE_MCS_CCK_MSK ||
610082cdbd11SMiri Korenblit 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
610182cdbd11SMiri Korenblit 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
610282cdbd11SMiri Korenblit 
610382cdbd11SMiri Korenblit 		/* add the offset needed to get to the legacy ofdm indices */
610482cdbd11SMiri Korenblit 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
610582cdbd11SMiri Korenblit 			rate += IWL_FIRST_OFDM_RATE;
610682cdbd11SMiri Korenblit 
610782cdbd11SMiri Korenblit 		switch (rate) {
610882cdbd11SMiri Korenblit 		case IWL_RATE_1M_INDEX:
610982cdbd11SMiri Korenblit 			rinfo->legacy = 10;
611082cdbd11SMiri Korenblit 			break;
611182cdbd11SMiri Korenblit 		case IWL_RATE_2M_INDEX:
611282cdbd11SMiri Korenblit 			rinfo->legacy = 20;
611382cdbd11SMiri Korenblit 			break;
611482cdbd11SMiri Korenblit 		case IWL_RATE_5M_INDEX:
611582cdbd11SMiri Korenblit 			rinfo->legacy = 55;
611682cdbd11SMiri Korenblit 			break;
611782cdbd11SMiri Korenblit 		case IWL_RATE_11M_INDEX:
611882cdbd11SMiri Korenblit 			rinfo->legacy = 110;
611982cdbd11SMiri Korenblit 			break;
612082cdbd11SMiri Korenblit 		case IWL_RATE_6M_INDEX:
612182cdbd11SMiri Korenblit 			rinfo->legacy = 60;
612282cdbd11SMiri Korenblit 			break;
612382cdbd11SMiri Korenblit 		case IWL_RATE_9M_INDEX:
612482cdbd11SMiri Korenblit 			rinfo->legacy = 90;
612582cdbd11SMiri Korenblit 			break;
612682cdbd11SMiri Korenblit 		case IWL_RATE_12M_INDEX:
612782cdbd11SMiri Korenblit 			rinfo->legacy = 120;
612882cdbd11SMiri Korenblit 			break;
612982cdbd11SMiri Korenblit 		case IWL_RATE_18M_INDEX:
613082cdbd11SMiri Korenblit 			rinfo->legacy = 180;
613182cdbd11SMiri Korenblit 			break;
613282cdbd11SMiri Korenblit 		case IWL_RATE_24M_INDEX:
613382cdbd11SMiri Korenblit 			rinfo->legacy = 240;
613482cdbd11SMiri Korenblit 			break;
613582cdbd11SMiri Korenblit 		case IWL_RATE_36M_INDEX:
613682cdbd11SMiri Korenblit 			rinfo->legacy = 360;
613782cdbd11SMiri Korenblit 			break;
613882cdbd11SMiri Korenblit 		case IWL_RATE_48M_INDEX:
613982cdbd11SMiri Korenblit 			rinfo->legacy = 480;
614082cdbd11SMiri Korenblit 			break;
614182cdbd11SMiri Korenblit 		case IWL_RATE_54M_INDEX:
614282cdbd11SMiri Korenblit 			rinfo->legacy = 540;
614382cdbd11SMiri Korenblit 		}
614482cdbd11SMiri Korenblit 		return;
614582cdbd11SMiri Korenblit 	}
614682cdbd11SMiri Korenblit 
6147ed780545SJohannes Berg 	rinfo->nss = u32_get_bits(rate_n_flags,
614882cdbd11SMiri Korenblit 				  RATE_MCS_NSS_MSK) + 1;
614982cdbd11SMiri Korenblit 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
615082cdbd11SMiri Korenblit 		RATE_HT_MCS_INDEX(rate_n_flags) :
615182cdbd11SMiri Korenblit 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
615282cdbd11SMiri Korenblit 
61537138763eSJohannes Berg 	if (rate_n_flags & RATE_MCS_SGI_MSK)
61547138763eSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
61557138763eSJohannes Berg 
61567138763eSJohannes Berg 	switch (format) {
615723dcee94SJohannes Berg 	case RATE_MCS_EHT_MSK:
615823dcee94SJohannes Berg 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
615923dcee94SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
616023dcee94SJohannes Berg 		break;
61617138763eSJohannes Berg 	case RATE_MCS_HE_MSK:
61627138763eSJohannes Berg 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
6163ed780545SJohannes Berg 
6164ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
6165ed780545SJohannes Berg 
616682cdbd11SMiri Korenblit 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
6167ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
6168ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
6169ed780545SJohannes Berg 		}
6170ed780545SJohannes Berg 
617182cdbd11SMiri Korenblit 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
617282cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_SU:
617382cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_EXT_SU:
6174ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
6175ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6176ed780545SJohannes Berg 			else if (gi_ltf == 2)
6177ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
617882cdbd11SMiri Korenblit 			else if (gi_ltf == 3)
6179ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
618082cdbd11SMiri Korenblit 			else
618182cdbd11SMiri Korenblit 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6182ed780545SJohannes Berg 			break;
618382cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_MU:
6184ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
6185ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6186ed780545SJohannes Berg 			else if (gi_ltf == 2)
6187ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6188ed780545SJohannes Berg 			else
6189ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6190ed780545SJohannes Berg 			break;
619182cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_TRIG:
6192ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
6193ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6194ed780545SJohannes Berg 			else
6195ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6196ed780545SJohannes Berg 			break;
6197ed780545SJohannes Berg 		}
6198ed780545SJohannes Berg 
6199ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
6200ed780545SJohannes Berg 			rinfo->he_dcm = 1;
62017138763eSJohannes Berg 		break;
62027138763eSJohannes Berg 	case RATE_MCS_HT_MSK:
620382cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
62047138763eSJohannes Berg 		break;
62057138763eSJohannes Berg 	case RATE_MCS_VHT_MSK:
620682cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
62077138763eSJohannes Berg 		break;
6208ed780545SJohannes Berg 	}
6209ed780545SJohannes Berg }
6210ed780545SJohannes Berg 
iwl_mvm_mac_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)6211cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
6212e705c121SKalle Valo 				struct ieee80211_vif *vif,
6213e705c121SKalle Valo 				struct ieee80211_sta *sta,
6214e705c121SKalle Valo 				struct station_info *sinfo)
6215e705c121SKalle Valo {
6216e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6217e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6218e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
6219b6e3d1baSAnjaneyulu 	int i;
6220e705c121SKalle Valo 
6221c8ee33e1SGregory Greenman 	if (mvmsta->deflink.avg_energy) {
6222c8ee33e1SGregory Greenman 		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
622322d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
6224988b5968SSara Sharon 	}
6225988b5968SSara Sharon 
6226ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
6227c8ee33e1SGregory Greenman 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
6228ed780545SJohannes Berg 
6229ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
6230ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6231ed780545SJohannes Berg 	}
6232ed780545SJohannes Berg 
6233e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
6234e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
6235e705c121SKalle Valo 		return;
6236e705c121SKalle Valo 
6237f276e20bSJohannes Berg 	if (!vif->cfg.assoc)
6238e705c121SKalle Valo 		return;
6239e705c121SKalle Valo 
62403f312651SJohannes Berg 	guard(mvm)(mvm);
6241e705c121SKalle Valo 
6242c8ee33e1SGregory Greenman 	if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
62433f312651SJohannes Berg 		return;
6244e705c121SKalle Valo 
6245e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
62463f312651SJohannes Berg 		return;
6247e705c121SKalle Valo 
6248b6e3d1baSAnjaneyulu 	sinfo->rx_beacon = 0;
6249b6e3d1baSAnjaneyulu 	for_each_mvm_vif_valid_link(mvmvif, i)
6250b6e3d1baSAnjaneyulu 		sinfo->rx_beacon += mvmvif->link[i]->beacon_stats.num_beacons +
6251b6e3d1baSAnjaneyulu 			mvmvif->link[i]->beacon_stats.accu_num_beacons;
6252b6e3d1baSAnjaneyulu 
625322d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
6254650cadb7SGregory Greenman 	if (mvmvif->deflink.beacon_stats.avg_signal) {
6255e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
6256650cadb7SGregory Greenman 		sinfo->rx_beacon_signal_avg =
6257650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.avg_signal;
625822d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
6259e705c121SKalle Valo 	}
6260e705c121SKalle Valo }
6261e705c121SKalle Valo 
iwl_mvm_event_mlme_callback_ini(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const struct ieee80211_mlme_event * mlme)6262119c2a13SMordechay Goodstein static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
6263119c2a13SMordechay Goodstein 					    struct ieee80211_vif *vif,
6264119c2a13SMordechay Goodstein 					    const  struct ieee80211_mlme_event *mlme)
6265119c2a13SMordechay Goodstein {
62661a81bddfSMordechay Goodstein 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
62671a81bddfSMordechay Goodstein 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
6268119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6269119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
6270119c2a13SMordechay Goodstein 				       NULL);
6271119c2a13SMordechay Goodstein 		return;
6272119c2a13SMordechay Goodstein 	}
6273119c2a13SMordechay Goodstein 
6274119c2a13SMordechay Goodstein 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
6275119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6276119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_DEASSOC,
6277119c2a13SMordechay Goodstein 				       NULL);
6278119c2a13SMordechay Goodstein 		return;
6279119c2a13SMordechay Goodstein 	}
6280119c2a13SMordechay Goodstein }
6281119c2a13SMordechay Goodstein 
iwl_mvm_event_mlme_callback(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const struct ieee80211_event * event)6282e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
6283e705c121SKalle Valo 					struct ieee80211_vif *vif,
6284e705c121SKalle Valo 					const struct ieee80211_event *event)
6285e705c121SKalle Valo {
62864c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
6287e705c121SKalle Valo 	do {								\
62884c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
6289e705c121SKalle Valo 			break;						\
62907174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
6291e705c121SKalle Valo 	} while (0)
6292e705c121SKalle Valo 
6293e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
6294e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
6295e705c121SKalle Valo 
6296119c2a13SMordechay Goodstein 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
6297119c2a13SMordechay Goodstein 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
6298119c2a13SMordechay Goodstein 		return;
6299119c2a13SMordechay Goodstein 	}
6300119c2a13SMordechay Goodstein 
63016c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
63026c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
63036c042d75SSara Sharon 	if (!trig)
6304e705c121SKalle Valo 		return;
6305e705c121SKalle Valo 
6306e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
6307e705c121SKalle Valo 
6308e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
6309e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
63104c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
6311e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
6312e705c121SKalle Valo 					    event->u.mlme.reason);
6313e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
63144c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
6315e705c121SKalle Valo 					   "ASSOC TIMEOUT");
6316e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
6317e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
63184c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
6319e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
6320e705c121SKalle Valo 					   event->u.mlme.reason);
6321e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
63224c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
6323e705c121SKalle Valo 					   "AUTH TIMEOUT");
6324e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
63254c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
6326e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
6327e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
63284c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
6329e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
6330e705c121SKalle Valo 	}
6331e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
6332e705c121SKalle Valo }
6333e705c121SKalle Valo 
iwl_mvm_event_bar_rx_callback(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const struct ieee80211_event * event)6334e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
6335e705c121SKalle Valo 					  struct ieee80211_vif *vif,
6336e705c121SKalle Valo 					  const struct ieee80211_event *event)
6337e705c121SKalle Valo {
6338e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
6339e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
6340e705c121SKalle Valo 
63416c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
63426c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
63436c042d75SSara Sharon 	if (!trig)
6344e705c121SKalle Valo 		return;
6345e705c121SKalle Valo 
6346e705c121SKalle Valo 	ba_trig = (void *)trig->data;
6347e705c121SKalle Valo 
6348e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
6349e705c121SKalle Valo 		return;
6350e705c121SKalle Valo 
63517174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
6352e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
6353e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
6354e705c121SKalle Valo 				event->u.ba.ssn);
6355e705c121SKalle Valo }
6356e705c121SKalle Valo 
iwl_mvm_mac_event_callback(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct ieee80211_event * event)6357cbce62a3SMiri Korenblit void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
6358e705c121SKalle Valo 				struct ieee80211_vif *vif,
6359e705c121SKalle Valo 				const struct ieee80211_event *event)
6360e705c121SKalle Valo {
6361e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6362e705c121SKalle Valo 
6363e705c121SKalle Valo 	switch (event->type) {
6364e705c121SKalle Valo 	case MLME_EVENT:
6365e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
6366e705c121SKalle Valo 		break;
6367e705c121SKalle Valo 	case BAR_RX_EVENT:
6368e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
6369e705c121SKalle Valo 		break;
6370e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
6371528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
6372528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
6373e705c121SKalle Valo 		break;
6374e705c121SKalle Valo 	default:
6375e705c121SKalle Valo 		break;
6376e705c121SKalle Valo 	}
6377e705c121SKalle Valo }
6378e705c121SKalle Valo 
637987f690f5SShaul Triebitz #define SYNC_RX_QUEUE_TIMEOUT (HZ)
iwl_mvm_sync_rx_queues_internal(struct iwl_mvm * mvm,enum iwl_mvm_rxq_notif_type type,bool sync,const void * data,u32 size)6380d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
63815e1688ceSJohannes Berg 				     enum iwl_mvm_rxq_notif_type type,
63825e1688ceSJohannes Berg 				     bool sync,
63835e1688ceSJohannes Berg 				     const void *data, u32 size)
63840636b938SSara Sharon {
63855e1688ceSJohannes Berg 	struct {
63865e1688ceSJohannes Berg 		struct iwl_rxq_sync_cmd cmd;
63875e1688ceSJohannes Berg 		struct iwl_mvm_internal_rxq_notif notif;
63885e1688ceSJohannes Berg 	} __packed cmd = {
63895e1688ceSJohannes Berg 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
63905e1688ceSJohannes Berg 		.cmd.count =
63915e1688ceSJohannes Berg 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
63925e1688ceSJohannes Berg 				    size),
63935e1688ceSJohannes Berg 		.notif.type = type,
63945e1688ceSJohannes Berg 		.notif.sync = sync,
63955e1688ceSJohannes Berg 	};
63965e1688ceSJohannes Berg 	struct iwl_host_cmd hcmd = {
63975e1688ceSJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
63985e1688ceSJohannes Berg 		.data[0] = &cmd,
63995e1688ceSJohannes Berg 		.len[0] = sizeof(cmd),
64005e1688ceSJohannes Berg 		.data[1] = data,
64015e1688ceSJohannes Berg 		.len[1] = size,
64024d08c0b3SJohannes Berg 		.flags = CMD_SEND_IN_RFKILL | (sync ? 0 : CMD_ASYNC),
64035e1688ceSJohannes Berg 	};
64040636b938SSara Sharon 	int ret;
64050636b938SSara Sharon 
64065e1688ceSJohannes Berg 	/* size must be a multiple of DWORD */
64075e1688ceSJohannes Berg 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
64085e1688ceSJohannes Berg 		return;
64090636b938SSara Sharon 
6410cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
64110636b938SSara Sharon 		return;
64120636b938SSara Sharon 
64135e1688ceSJohannes Berg 	if (sync) {
64145e1688ceSJohannes Berg 		cmd.notif.cookie = mvm->queue_sync_cookie;
64152f7a04c7SJohannes Berg 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6416a2113cc4SNaftali Goldstein 	}
6417d0ff5d22SSara Sharon 
64185e1688ceSJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
64190636b938SSara Sharon 	if (ret) {
64200636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
64210636b938SSara Sharon 		goto out;
64220636b938SSara Sharon 	}
6423d0ff5d22SSara Sharon 
64245e1688ceSJohannes Berg 	if (sync) {
64253c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
64263a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
6427e715c930SEmmanuel Grumbach 					 READ_ONCE(mvm->queue_sync_state) == 0,
642887f690f5SShaul Triebitz 					 SYNC_RX_QUEUE_TIMEOUT);
6429e715c930SEmmanuel Grumbach 		WARN_ONCE(!ret, "queue sync: failed to sync, state is 0x%lx, cookie %d\n",
6430f4eedfd8SJohannes Berg 			  mvm->queue_sync_state,
6431f4eedfd8SJohannes Berg 			  mvm->queue_sync_cookie);
64326ad04359SJohannes Berg 	}
64330636b938SSara Sharon 
64340636b938SSara Sharon out:
64355e1688ceSJohannes Berg 	if (sync) {
64362f7a04c7SJohannes Berg 		mvm->queue_sync_state = 0;
64370636b938SSara Sharon 		mvm->queue_sync_cookie++;
64380636b938SSara Sharon 	}
64395f8a3561SJohannes Berg }
64400636b938SSara Sharon 
iwl_mvm_sync_rx_queues(struct ieee80211_hw * hw)6441cbce62a3SMiri Korenblit void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
64420636b938SSara Sharon {
64430636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
64440636b938SSara Sharon 
64453f312651SJohannes Berg 	guard(mvm)(mvm);
64465e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
64470636b938SSara Sharon }
64480636b938SSara Sharon 
6449cbce62a3SMiri Korenblit int
iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_ftm_responder_stats * stats)6450b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6451b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
6452b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
6453b73f9a4aSJohannes Berg {
6454b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6455b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6456b73f9a4aSJohannes Berg 
6457b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6458b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6459b73f9a4aSJohannes Berg 		return -EINVAL;
6460b73f9a4aSJohannes Berg 
6461b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
6462b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
6463b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
6464b73f9a4aSJohannes Berg 
6465b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6466b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6467b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
6468b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
6469b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6470b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6471b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6472b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6473b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6474b73f9a4aSJohannes Berg 
6475b73f9a4aSJohannes Berg 	return 0;
6476b73f9a4aSJohannes Berg }
6477b73f9a4aSJohannes Berg 
iwl_mvm_start_pmsr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_pmsr_request * request)6478cbce62a3SMiri Korenblit int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6479fc36ffdaSJohannes Berg 		       struct cfg80211_pmsr_request *request)
6480fc36ffdaSJohannes Berg {
6481fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6482fc36ffdaSJohannes Berg 
64833f312651SJohannes Berg 	guard(mvm)(mvm);
64843f312651SJohannes Berg 	return iwl_mvm_ftm_start(mvm, vif, request);
6485fc36ffdaSJohannes Berg }
6486fc36ffdaSJohannes Berg 
iwl_mvm_abort_pmsr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_pmsr_request * request)6487cbce62a3SMiri Korenblit void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6488fc36ffdaSJohannes Berg 			struct cfg80211_pmsr_request *request)
6489fc36ffdaSJohannes Berg {
6490fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6491fc36ffdaSJohannes Berg 
64923f312651SJohannes Berg 	guard(mvm)(mvm);
6493fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
6494fc36ffdaSJohannes Berg }
6495fc36ffdaSJohannes Berg 
iwl_mvm_can_hw_csum(struct sk_buff * skb)6496438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6497438af969SSara Sharon {
6498438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
6499438af969SSara Sharon 
6500438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
6501438af969SSara Sharon 		return false;
6502438af969SSara Sharon 
6503438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6504438af969SSara Sharon }
6505438af969SSara Sharon 
iwl_mvm_mac_can_aggregate(struct ieee80211_hw * hw,struct sk_buff * head,struct sk_buff * skb)6506438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6507438af969SSara Sharon 				      struct sk_buff *head,
6508438af969SSara Sharon 				      struct sk_buff *skb)
6509438af969SSara Sharon {
6510438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6511438af969SSara Sharon 
6512438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
6513438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
6514438af969SSara Sharon 		return false;
6515438af969SSara Sharon 
6516438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
6517438af969SSara Sharon 		return true;
6518438af969SSara Sharon 
6519438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6520438af969SSara Sharon }
6521438af969SSara Sharon 
iwl_mvm_set_hw_timestamp(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_set_hw_timestamp * hwts)6522be8897e2SAvraham Stern int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6523cf85123aSAvraham Stern 			     struct ieee80211_vif *vif,
6524cf85123aSAvraham Stern 			     struct cfg80211_set_hw_timestamp *hwts)
6525cf85123aSAvraham Stern {
6526cf85123aSAvraham Stern 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6527cf85123aSAvraham Stern 	u32 protocols = 0;
6528cf85123aSAvraham Stern 
6529cf85123aSAvraham Stern 	/* HW timestamping is only supported for a specific station */
6530cf85123aSAvraham Stern 	if (!hwts->macaddr)
6531cf85123aSAvraham Stern 		return -EOPNOTSUPP;
6532cf85123aSAvraham Stern 
6533cf85123aSAvraham Stern 	if (hwts->enable)
6534cf85123aSAvraham Stern 		protocols =
6535cf85123aSAvraham Stern 			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6536cf85123aSAvraham Stern 
65373f312651SJohannes Berg 	guard(mvm)(mvm);
65383f312651SJohannes Berg 	return iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6539cf85123aSAvraham Stern }
6540cf85123aSAvraham Stern 
6541e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
6542e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
6543cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6544e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
6545e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
65464ea1ed1dSEmmanuel Grumbach 	.set_antenna = iwl_mvm_op_set_antenna,
6547e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
6548e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
6549e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
6550e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
6551e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
6552e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
6553e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
6554e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
6555e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
6556e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
6557e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
6558e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6559e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6560e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
6561e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
6562e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6563e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6564e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6565e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
6566e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
6567e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
65686b1259d1SJohannes Berg 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6569e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6570e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
6571a6cc6ccbSJohannes Berg 	.flush_sta = iwl_mvm_mac_flush_sta,
6572e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
6573e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6574e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
6575e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
6576e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
6577e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
6578e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
6579e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
6580e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
6581e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6582e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6583e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6584e705c121SKalle Valo 
6585ae7ba17bSShaul Triebitz 	.start_ap = iwl_mvm_start_ap,
6586ae7ba17bSShaul Triebitz 	.stop_ap = iwl_mvm_stop_ap,
6587ae7ba17bSShaul Triebitz 	.join_ibss = iwl_mvm_start_ibss,
6588ae7ba17bSShaul Triebitz 	.leave_ibss = iwl_mvm_stop_ibss,
6589e705c121SKalle Valo 
65902f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
65912f0282dbSJohannes Berg 
6592e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
6593e705c121SKalle Valo 
6594e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
65952f876f91SJohannes Berg 	.pre_channel_switch = iwl_mvm_mac_pre_channel_switch,
6596e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
659779221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
6598c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6599e705c121SKalle Valo 
6600e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6601e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6602e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6603e705c121SKalle Valo 
6604e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
6605e705c121SKalle Valo 
66060636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
66070636b938SSara Sharon 
6608e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
6609e705c121SKalle Valo 
6610e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6611e705c121SKalle Valo 	/* look at d3.c */
6612e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
6613e705c121SKalle Valo 	.resume = iwl_mvm_resume,
6614e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
6615e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
6616e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
6617e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6618e705c121SKalle Valo #endif
6619e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6620e705c121SKalle Valo #endif
6621e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
6622e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
6623b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6624fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
6625fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
6626b73f9a4aSJohannes Berg 
6627438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6628177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
6629c36235acSJohannes Berg 	.vif_add_debugfs = iwl_mvm_vif_add_debugfs,
66303f244876SBenjamin Berg 	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
6631177a11cfSGregory Greenman #endif
6632cf85123aSAvraham Stern 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6633e705c121SKalle Valo };
6634