xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision f05d1e04c43fb2047b00a2a73646f73d2ac62724)
18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
3207be64fSMiri Korenblit  * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
7e705c121SKalle Valo #include <linux/kernel.h>
8e705c121SKalle Valo #include <linux/slab.h>
9e705c121SKalle Valo #include <linux/skbuff.h>
10e705c121SKalle Valo #include <linux/netdevice.h>
11e705c121SKalle Valo #include <linux/etherdevice.h>
12e705c121SKalle Valo #include <linux/ip.h>
13e705c121SKalle Valo #include <linux/if_arp.h>
142f89a5d7SGolan Ben-Ami #include <linux/time.h>
15e705c121SKalle Valo #include <net/mac80211.h>
16e705c121SKalle Valo #include <net/ieee80211_radiotap.h>
17e705c121SKalle Valo #include <net/tcp.h>
18e705c121SKalle Valo 
195283dd67SMordechay Goodstein #include "iwl-drv.h"
20e705c121SKalle Valo #include "iwl-op-mode.h"
21e705c121SKalle Valo #include "iwl-io.h"
22e705c121SKalle Valo #include "mvm.h"
23e705c121SKalle Valo #include "sta.h"
24e705c121SKalle Valo #include "time-event.h"
25e705c121SKalle Valo #include "iwl-eeprom-parse.h"
26e705c121SKalle Valo #include "iwl-phy-db.h"
27e705c121SKalle Valo #include "testmode.h"
28d962f9b1SJohannes Berg #include "fw/error-dump.h"
29e705c121SKalle Valo #include "iwl-prph.h"
30e705c121SKalle Valo #include "iwl-nvm-parse.h"
31cf85123aSAvraham Stern #include "time-sync.h"
32e705c121SKalle Valo 
33e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
34e705c121SKalle Valo 	{
35e705c121SKalle Valo 		.max = 1,
36e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
37e705c121SKalle Valo 	},
38e705c121SKalle Valo 	{
39e705c121SKalle Valo 		.max = 1,
40e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
41e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
42e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
43e705c121SKalle Valo 	},
44e705c121SKalle Valo 	{
45e705c121SKalle Valo 		.max = 1,
46e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
47e705c121SKalle Valo 	},
48e705c121SKalle Valo };
49e705c121SKalle Valo 
50e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
51e705c121SKalle Valo 	{
52e705c121SKalle Valo 		.num_different_channels = 2,
53e705c121SKalle Valo 		.max_interfaces = 3,
54e705c121SKalle Valo 		.limits = iwl_mvm_limits,
55e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
56e705c121SKalle Valo 	},
57e705c121SKalle Valo };
58e705c121SKalle Valo 
59fc36ffdaSJohannes Berg static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
60fc36ffdaSJohannes Berg 	.max_peers = IWL_MVM_TOF_MAX_APS,
61fc36ffdaSJohannes Berg 	.report_ap_tsf = 1,
62fc36ffdaSJohannes Berg 	.randomize_mac_addr = 1,
63fc36ffdaSJohannes Berg 
64fc36ffdaSJohannes Berg 	.ftm = {
65fc36ffdaSJohannes Berg 		.supported = 1,
66fc36ffdaSJohannes Berg 		.asap = 1,
67fc36ffdaSJohannes Berg 		.non_asap = 1,
68fc36ffdaSJohannes Berg 		.request_lci = 1,
69fc36ffdaSJohannes Berg 		.request_civicloc = 1,
706815e3d0SAvraham Stern 		.trigger_based = 1,
716815e3d0SAvraham Stern 		.non_trigger_based = 1,
72fc36ffdaSJohannes Berg 		.max_bursts_exponent = -1, /* all supported */
73fc36ffdaSJohannes Berg 		.max_ftms_per_burst = 0, /* no limits */
74fc36ffdaSJohannes Berg 		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
75fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_20) |
76fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_40) |
778a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_80) |
788a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_160),
79fc36ffdaSJohannes Berg 		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
80fc36ffdaSJohannes Berg 			     BIT(NL80211_PREAMBLE_HT) |
816815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_VHT) |
826815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_HE),
83fc36ffdaSJohannes Berg 	},
84fc36ffdaSJohannes Berg };
85fc36ffdaSJohannes Berg 
866569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
87c56e00a3SJohannes Berg 				 enum set_key_cmd cmd,
88c56e00a3SJohannes Berg 				 struct ieee80211_vif *vif,
89c56e00a3SJohannes Berg 				 struct ieee80211_sta *sta,
90c56e00a3SJohannes Berg 				 struct ieee80211_key_conf *key);
91c56e00a3SJohannes Berg 
92e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
93e705c121SKalle Valo {
94e705c121SKalle Valo 	int i;
95e705c121SKalle Valo 
96e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
97e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
98e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
99e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
100e705c121SKalle Valo 	}
101e705c121SKalle Valo }
102e705c121SKalle Valo 
103e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
104e705c121SKalle Valo 						  const char *alpha2,
105e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
106e705c121SKalle Valo 						  bool *changed)
107e705c121SKalle Valo {
108e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
109e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
110e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
111e9b63341SAbhishek Naik 	struct iwl_mcc_update_resp_v8 *resp;
112e27c506aSGil Adam 	u8 resp_ver;
113e705c121SKalle Valo 
114e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
115e705c121SKalle Valo 
116e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
117e705c121SKalle Valo 
118e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
119e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
120e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
121e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
1226d19a5ebSEmmanuel Grumbach 		resp = NULL;
123e705c121SKalle Valo 		goto out;
124e705c121SKalle Valo 	}
125e705c121SKalle Valo 
12682715ac7SEmmanuel Grumbach 	if (changed) {
12782715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
12882715ac7SEmmanuel Grumbach 
12982715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
13082715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
13182715ac7SEmmanuel Grumbach 	}
132e27c506aSGil Adam 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
133e27c506aSGil Adam 					   MCC_UPDATE_CMD, 0);
134e27c506aSGil Adam 	IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
135e705c121SKalle Valo 
136e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
137e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
138e705c121SKalle Valo 				      resp->channels,
13977e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
1402763bba6SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info),
141e9b63341SAbhishek Naik 				      le32_to_cpu(resp->cap), resp_ver);
142e705c121SKalle Valo 	/* Store the return source id */
143e705c121SKalle Valo 	src_id = resp->source_id;
144e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
145e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
146e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
147e705c121SKalle Valo 		goto out;
148e705c121SKalle Valo 	}
149e705c121SKalle Valo 
150e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
151e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
152e705c121SKalle Valo 	mvm->lar_regdom_set = true;
153e705c121SKalle Valo 	mvm->mcc_src = src_id;
154e705c121SKalle Valo 
1556d19a5ebSEmmanuel Grumbach 	iwl_mei_set_country_code(__le16_to_cpu(resp->mcc));
1566d19a5ebSEmmanuel Grumbach 
157e705c121SKalle Valo out:
1586d19a5ebSEmmanuel Grumbach 	kfree(resp);
159e705c121SKalle Valo 	return regd;
160e705c121SKalle Valo }
161e705c121SKalle Valo 
162e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
163e705c121SKalle Valo {
164e705c121SKalle Valo 	bool changed;
165e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
166e705c121SKalle Valo 
167e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
168e705c121SKalle Valo 		return;
169e705c121SKalle Valo 
170e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
171e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
172e705c121SKalle Valo 		/* only update the regulatory core if changed */
173e705c121SKalle Valo 		if (changed)
174e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
175e705c121SKalle Valo 
176e705c121SKalle Valo 		kfree(regd);
177e705c121SKalle Valo 	}
178e705c121SKalle Valo }
179e705c121SKalle Valo 
180e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
181e705c121SKalle Valo 							  bool *changed)
182e705c121SKalle Valo {
183e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
184e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
185e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
186e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
187e705c121SKalle Valo }
188e705c121SKalle Valo 
189e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
190e705c121SKalle Valo {
191e705c121SKalle Valo 	enum iwl_mcc_source used_src;
192e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
193e705c121SKalle Valo 	int ret;
194e705c121SKalle Valo 	bool changed;
195e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
196a05829a7SJohannes Berg 			wiphy_dereference(mvm->hw->wiphy, mvm->hw->wiphy->regd);
197e705c121SKalle Valo 
198e705c121SKalle Valo 	if (!r)
199e705c121SKalle Valo 		return -ENOENT;
200e705c121SKalle Valo 
201e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
202e705c121SKalle Valo 	used_src = mvm->mcc_src;
203e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
204e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
205e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
206e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
207e705c121SKalle Valo 			kfree(regd);
208e705c121SKalle Valo 	}
209e705c121SKalle Valo 
210e705c121SKalle Valo 	/* Now set our last stored MCC and source */
211e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
212e705c121SKalle Valo 				     &changed);
213e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
214e705c121SKalle Valo 		return -EIO;
215e705c121SKalle Valo 
216e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
217e705c121SKalle Valo 	if (changed)
218a05829a7SJohannes Berg 		ret = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
219e705c121SKalle Valo 	else
220e705c121SKalle Valo 		ret = 0;
221e705c121SKalle Valo 
222e705c121SKalle Valo 	kfree(regd);
223e705c121SKalle Valo 	return ret;
224e705c121SKalle Valo }
225e705c121SKalle Valo 
2268e33f046SKrishnanand Prabhu /* Each capability added here should also be add to tm_if_types_ext_capa_sta */
2277f2ea521SYueHaibing static const u8 he_if_types_ext_capa_sta[] = {
2287360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
229918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
230784d4a42SJohannes Berg 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
231784d4a42SJohannes Berg 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
232784d4a42SJohannes Berg 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
2337360f99eSEmmanuel Grumbach };
2347360f99eSEmmanuel Grumbach 
2358e33f046SKrishnanand Prabhu static const u8 tm_if_types_ext_capa_sta[] = {
2368e33f046SKrishnanand Prabhu 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
2378e33f046SKrishnanand Prabhu 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT |
2388e33f046SKrishnanand Prabhu 	       WLAN_EXT_CAPA3_TIMING_MEASUREMENT_SUPPORT,
239784d4a42SJohannes Berg 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
240784d4a42SJohannes Berg 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
241784d4a42SJohannes Berg 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
2428e33f046SKrishnanand Prabhu 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
2438e33f046SKrishnanand Prabhu };
2448e33f046SKrishnanand Prabhu 
2458e33f046SKrishnanand Prabhu /* Additional interface types for which extended capabilities are
2468e33f046SKrishnanand Prabhu  * specified separately
2478e33f046SKrishnanand Prabhu  */
2486107f300SEmmanuel Grumbach 
2496107f300SEmmanuel Grumbach #define IWL_MVM_EMLSR_CAPA	(IEEE80211_EML_CAP_EMLSR_SUPP | \
2506107f300SEmmanuel Grumbach 				 IEEE80211_EML_CAP_EMLSR_PADDING_DELAY_32US << \
2516107f300SEmmanuel Grumbach 					__bf_shf(IEEE80211_EML_CAP_EMLSR_PADDING_DELAY) | \
2526107f300SEmmanuel Grumbach 				 IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_64US << \
2536107f300SEmmanuel Grumbach 					__bf_shf(IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY))
2546107f300SEmmanuel Grumbach 
2558e33f046SKrishnanand Prabhu static const struct wiphy_iftype_ext_capab add_iftypes_ext_capa[] = {
2567360f99eSEmmanuel Grumbach 	{
2577360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
2587360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
2597360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
2607360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
2616107f300SEmmanuel Grumbach 		/* relevant only if EHT is supported */
2626107f300SEmmanuel Grumbach 		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
2637360f99eSEmmanuel Grumbach 	},
2648e33f046SKrishnanand Prabhu 	{
2658e33f046SKrishnanand Prabhu 		.iftype = NL80211_IFTYPE_STATION,
2668e33f046SKrishnanand Prabhu 		.extended_capabilities = tm_if_types_ext_capa_sta,
2678e33f046SKrishnanand Prabhu 		.extended_capabilities_mask = tm_if_types_ext_capa_sta,
2688e33f046SKrishnanand Prabhu 		.extended_capabilities_len = sizeof(tm_if_types_ext_capa_sta),
2698e33f046SKrishnanand Prabhu 		/* relevant only if EHT is supported */
2706107f300SEmmanuel Grumbach 		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
2718e33f046SKrishnanand Prabhu 	},
2727360f99eSEmmanuel Grumbach };
2737360f99eSEmmanuel Grumbach 
274cbce62a3SMiri Korenblit int iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
275e8503aecSBen Greear {
276e8503aecSBen Greear 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
277e8503aecSBen Greear 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
278e8503aecSBen Greear 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
279e8503aecSBen Greear 	return 0;
280e8503aecSBen Greear }
281e8503aecSBen Greear 
2824ea1ed1dSEmmanuel Grumbach int iwl_mvm_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2834ea1ed1dSEmmanuel Grumbach {
2844ea1ed1dSEmmanuel Grumbach 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2854ea1ed1dSEmmanuel Grumbach 
2864ea1ed1dSEmmanuel Grumbach 	/* This has been tested on those devices only */
2874ea1ed1dSEmmanuel Grumbach 	if (mvm->trans->trans_cfg->device_family != IWL_DEVICE_FAMILY_9000 &&
2884ea1ed1dSEmmanuel Grumbach 	    mvm->trans->trans_cfg->device_family != IWL_DEVICE_FAMILY_22000)
2894ea1ed1dSEmmanuel Grumbach 		return -ENOTSUPP;
2904ea1ed1dSEmmanuel Grumbach 
2914ea1ed1dSEmmanuel Grumbach 	if (!mvm->nvm_data)
2924ea1ed1dSEmmanuel Grumbach 		return -EBUSY;
2934ea1ed1dSEmmanuel Grumbach 
2944ea1ed1dSEmmanuel Grumbach 	/* mac80211 ensures the device is not started,
2954ea1ed1dSEmmanuel Grumbach 	 * so the firmware cannot be running
2964ea1ed1dSEmmanuel Grumbach 	 */
2974ea1ed1dSEmmanuel Grumbach 
2984ea1ed1dSEmmanuel Grumbach 	mvm->set_tx_ant = tx_ant;
2994ea1ed1dSEmmanuel Grumbach 	mvm->set_rx_ant = rx_ant;
3004ea1ed1dSEmmanuel Grumbach 
3014ea1ed1dSEmmanuel Grumbach 	iwl_reinit_cab(mvm->trans, mvm->nvm_data, tx_ant, rx_ant, mvm->fw);
3024ea1ed1dSEmmanuel Grumbach 
3034ea1ed1dSEmmanuel Grumbach 	return 0;
3044ea1ed1dSEmmanuel Grumbach }
3054ea1ed1dSEmmanuel Grumbach 
306e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
307e705c121SKalle Valo {
308e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
309e705c121SKalle Valo 	int num_mac, ret, i;
310e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
311e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
312e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
313e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
314e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
315e705c121SKalle Valo 	};
3163f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
3173f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
3183f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3193f37c229SIdo Yariv #endif
320a5de7de7SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
321a5de7de7SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
322e705c121SKalle Valo 
323e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
324e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
325e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
326e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
327e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
328e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
329e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
330e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
331e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
332e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
333e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
334e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
335909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
33630433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
337520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
338cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
339cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
340ef2b47b8SJohannes Berg 
3410120e6b3SGregory Greenman 	/* Set this early since we need to have it for the check below */
342b3366330SJohannes Berg 	if (mvm->mld_api_is_used && mvm->nvm_data->sku_cap_11be_enable &&
343b3366330SJohannes Berg 	    !iwlwifi_mod_params.disable_11ax &&
344b3366330SJohannes Berg 	    !iwlwifi_mod_params.disable_11be)
3450120e6b3SGregory Greenman 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
3460120e6b3SGregory Greenman 
347ef2b47b8SJohannes Berg 	/* With MLD FW API, it tracks timing by itself,
348ef2b47b8SJohannes Berg 	 * no need for any timing from the host
349ef2b47b8SJohannes Berg 	 */
350ef2b47b8SJohannes Berg 	if (!mvm->mld_api_is_used)
351ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
352ef2b47b8SJohannes Berg 
353ef2b47b8SJohannes Berg 	/* We should probably have this, but mac80211
354ef2b47b8SJohannes Berg 	 * currently doesn't support it for MLO.
355ef2b47b8SJohannes Berg 	 */
356ef2b47b8SJohannes Berg 	if (!(hw->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
357ef2b47b8SJohannes Berg 		ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
358ef2b47b8SJohannes Berg 
359cfb21b11SJohannes Berg 	/*
360cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
361cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
362cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
363cfb21b11SJohannes Berg 	 * assert EDC.
364cfb21b11SJohannes Berg 	 *
365cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
366cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
367cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
368cfb21b11SJohannes Berg 	 * devices.
369cfb21b11SJohannes Berg 	 */
3707d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
371438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
372438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
373ecaf71deSGregory Greenman 
3744243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
375ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
376ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
377ecaf71deSGregory Greenman 	}
378ecaf71deSGregory Greenman 
379b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
380b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
381960f864bSJohannes Berg 
382960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
383960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
38465e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
385960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
386960f864bSJohannes Berg 		/*
387960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
388960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
389960f864bSJohannes Berg 		 * for station powersave
390960f864bSJohannes Berg 		 */
391960f864bSJohannes Berg 		return -EINVAL;
392960f864bSJohannes Berg 	}
393e705c121SKalle Valo 
39480938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
39580938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
39680938abcSJohannes Berg 
3972d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
3982d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
3992d7cf549SJohannes Berg 
400366fc672SJohannes Berg 	hw->queues = IEEE80211_NUM_ACS;
401e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
402e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
403e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
404e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
405e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
406371a17edSJohannes Berg 
407371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
408371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
409371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
410371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
411371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
412371a17edSJohannes Berg 
4134243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
4149f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
4159f66a397SGregory Greenman 
416e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
417e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
418438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
419e705c121SKalle Valo 
4208e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
421e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
422e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
423e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
424e705c121SKalle Valo 
4252a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4262a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4272a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
4282a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4292a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4302a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
4312a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
4322a53d166SAyala Beker 	}
4332a53d166SAyala Beker 
434f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
435f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
436f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
437f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
438f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
439f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
440f4bfdc5eSEmmanuel Grumbach 
441e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
442f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
443e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
4448e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
4458e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4468e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
4478e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4488e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
4498e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
4508e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
4518e160ab8SAyala Beker 	}
452e705c121SKalle Valo 
453ef2e7a51SIlan Peer 	wiphy_ext_feature_set(hw->wiphy,
454ef2e7a51SIlan Peer 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
455d16b96b5SJohannes Berg 	wiphy_ext_feature_set(hw->wiphy,
456d16b96b5SJohannes Berg 			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
457ef2e7a51SIlan Peer 
458b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
459fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
460b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
461b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
462fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
463fc36ffdaSJohannes Berg 	}
464b73f9a4aSJohannes Berg 
465a5de7de7SJohannes Berg 	if (sec_key_ver &&
466a5de7de7SJohannes Berg 	    fw_has_capa(&mvm->fw->ucode_capa,
467a5de7de7SJohannes Berg 			IWL_UCODE_TLV_CAPA_BIGTK_TX_SUPPORT))
468a5de7de7SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
469a5de7de7SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION);
470a5de7de7SJohannes Berg 	else if (fw_has_capa(&mvm->fw->ucode_capa,
471b1fdc250SJohannes Berg 			     IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
472b1fdc250SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
473b1fdc250SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
474b1fdc250SJohannes Berg 
475cf85123aSAvraham Stern 	if (fw_has_capa(&mvm->fw->ucode_capa,
476cf85123aSAvraham Stern 			IWL_UCODE_TLV_CAPA_TIME_SYNC_BOTH_FTM_TM))
477cf85123aSAvraham Stern 		hw->wiphy->hw_timestamp_max_peers = 1;
478cf85123aSAvraham Stern 
479e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
480e705c121SKalle Valo 	hw->wiphy->features |=
481e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
482e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
483e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
484e705c121SKalle Valo 
485e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
486e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
487e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
488cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
489e705c121SKalle Valo 
490e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
491e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
492e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
493e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
494e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
495e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
496e705c121SKalle Valo 
497e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
498e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
49933e3fd99SAlexander Wetzel 
50033e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
50133e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
50233e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
50333e3fd99SAlexander Wetzel 	 */
50433e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
50533e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
50633e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
507e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
508e47df5bdSJohannes Berg 
509e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
510e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
511e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
512e705c121SKalle Valo 	else
513e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
514e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
515e705c121SKalle Valo 
516e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
517e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
518eae94cf8SLuca Coelho 	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
519e705c121SKalle Valo 
520e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
521e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
522e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
523e705c121SKalle Valo 
524e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
5254f1847cfSJohannes Berg 	hw->max_listen_interval = IWL_MVM_CONN_LISTEN_INTERVAL;
526e705c121SKalle Valo 
527e705c121SKalle Valo 	/* Extract MAC address */
528e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
529e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
530e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
531e705c121SKalle Valo 
532e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
533e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
534e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
535e705c121SKalle Valo 
536e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
537e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
538e705c121SKalle Valo 		       ETH_ALEN);
539e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
540e705c121SKalle Valo 		hw->wiphy->n_addresses++;
541e705c121SKalle Valo 	}
542e705c121SKalle Valo 
543e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
544e705c121SKalle Valo 
545e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
546e705c121SKalle Valo 
547e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
548e705c121SKalle Valo 
549e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
550e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
551e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
552e705c121SKalle Valo 
553e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
554e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
555e705c121SKalle Valo 	else
556e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
557e705c121SKalle Valo 
55857fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
55957fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
56057fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
56157fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
56257fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
56357fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
564e705c121SKalle Valo 
565e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
566e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
567e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
568e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
56957fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
570e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
571e705c121SKalle Valo 	}
572eae94cf8SLuca Coelho 	if (fw_has_capa(&mvm->fw->ucode_capa,
573eae94cf8SLuca Coelho 			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
574eae94cf8SLuca Coelho 	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
575eae94cf8SLuca Coelho 		hw->wiphy->bands[NL80211_BAND_6GHZ] =
576eae94cf8SLuca Coelho 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
577e705c121SKalle Valo 
578e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
579e705c121SKalle Valo 
580e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
581e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
582e705c121SKalle Valo 	else
583e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
584e705c121SKalle Valo 
585ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
586e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
5875d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
588e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
589e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
590e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
591e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
592e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
593e705c121SKalle Valo 
594e705c121SKalle Valo 	/*
595e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
596e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
597e705c121SKalle Valo 	 */
598e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
599e705c121SKalle Valo 
600e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
601e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
602e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
603a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
604e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
605e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
606e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
607e705c121SKalle Valo 
608e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
609e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
610e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
611e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
612e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
613e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
614e705c121SKalle Valo 
615e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
616e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
617e705c121SKalle Valo 		hw->wiphy->features |=
618e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
619e705c121SKalle Valo 
620e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
621e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
622e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
623e705c121SKalle Valo 
624971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
6252a42aea7SNathan Errera 				  IWL_FW_CMD_VER_UNKNOWN) == 3)
6262a42aea7SNathan Errera 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
6272a42aea7SNathan Errera 
628aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
629aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
630aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
631aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
632aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
633aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
634aacf8f18SAvrahams Stern 	}
635aacf8f18SAvrahams Stern 
6368f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
637971cbe50SJohannes Berg 		u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
638773a042fSAvraham Stern 
6398f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6408f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
6418f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6428f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
6438f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
6448f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
645773a042fSAvraham Stern 
646773a042fSAvraham Stern 		/* Old firmware also supports probe deferral and suppression */
647773a042fSAvraham Stern 		if (scan_ver < 15)
648773a042fSAvraham Stern 			wiphy_ext_feature_set(hw->wiphy,
649773a042fSAvraham Stern 					      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
6508f691af9SZamir, Roee 	}
6518f691af9SZamir, Roee 
6528e33f046SKrishnanand Prabhu 	hw->wiphy->iftype_ext_capab = NULL;
6538e33f046SKrishnanand Prabhu 	hw->wiphy->num_iftype_ext_capab = 0;
6548e33f046SKrishnanand Prabhu 
6557360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
6567360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
6578e33f046SKrishnanand Prabhu 		hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa;
6587360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
6598e33f046SKrishnanand Prabhu 			ARRAY_SIZE(add_iftypes_ext_capa) - 1;
660918cbf39SSara Sharon 
661918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
662918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
6637360f99eSEmmanuel Grumbach 	}
6647360f99eSEmmanuel Grumbach 
6658e33f046SKrishnanand Prabhu 	if (iwl_fw_lookup_cmd_ver(mvm->fw,
6668e33f046SKrishnanand Prabhu 				  WIDE_ID(DATA_PATH_GROUP,
6678e33f046SKrishnanand Prabhu 					  WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
6688e33f046SKrishnanand Prabhu 				  IWL_FW_CMD_VER_UNKNOWN) >= 1) {
6698e33f046SKrishnanand Prabhu 		IWL_DEBUG_INFO(mvm->trans, "Timing measurement supported\n");
6708e33f046SKrishnanand Prabhu 
6718e33f046SKrishnanand Prabhu 		if (!hw->wiphy->iftype_ext_capab) {
6728e33f046SKrishnanand Prabhu 			hw->wiphy->num_iftype_ext_capab = 1;
6738e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa +
6748e33f046SKrishnanand Prabhu 				ARRAY_SIZE(add_iftypes_ext_capa) - 1;
6758e33f046SKrishnanand Prabhu 		} else {
6768e33f046SKrishnanand Prabhu 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa + 1;
6778e33f046SKrishnanand Prabhu 		}
6788e33f046SKrishnanand Prabhu 	}
6798e33f046SKrishnanand Prabhu 
680e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
681e705c121SKalle Valo 
682e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6833f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
684e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
685e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
686e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
687e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
688e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
689e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
690e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
691e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
692e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
693e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
694e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
695e705c121SKalle Valo 
696e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
697e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
698e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
6995d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
7005d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
701e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
702e705c121SKalle Valo 	}
703e705c121SKalle Valo #endif
704e705c121SKalle Valo 
705e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
706e705c121SKalle Valo 	if (ret)
707e705c121SKalle Valo 		return ret;
708e705c121SKalle Valo 
709e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
710e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
711e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
712e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
713e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
714e705c121SKalle Valo 	}
715e705c121SKalle Valo 
716e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
717e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
718e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
719e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
720e705c121SKalle Valo 	}
721e705c121SKalle Valo 
722e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
72359fa61f3SEmmanuel Grumbach 	if (!iwl_mvm_is_csum_supported(mvm))
7246772aab7SJohannes Berg 		hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
72541837ca9SEmmanuel Grumbach 
72691b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
72791b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
72891b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
72991b08c2dSAviya Erenfeld 
7309c11d8a9SShaul Triebitz 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
7319c11d8a9SShaul Triebitz 		wiphy_ext_feature_set(hw->wiphy,
7329c11d8a9SShaul Triebitz 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
7339c11d8a9SShaul Triebitz 
734bfcfdb59SEmmanuel Grumbach 	iwl_mvm_vendor_cmds_register(mvm);
735bfcfdb59SEmmanuel Grumbach 
736e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
737e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
738e8503aecSBen Greear 
739de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
740de645e89SJohannes Berg 	if (ret) {
741de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
742e705c121SKalle Valo 	}
743e705c121SKalle Valo 
744e705c121SKalle Valo 	return ret;
745e705c121SKalle Valo }
746e705c121SKalle Valo 
747df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
748df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
749df2378abSJohannes Berg {
750df2378abSJohannes Berg 	if (likely(sta)) {
751df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
752df2378abSJohannes Berg 			return;
753df2378abSJohannes Berg 	} else {
754df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
755df2378abSJohannes Berg 			return;
756df2378abSJohannes Berg 	}
757df2378abSJohannes Berg 
758df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
759df2378abSJohannes Berg }
760df2378abSJohannes Berg 
761cbce62a3SMiri Korenblit void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
762cbce62a3SMiri Korenblit 		    struct ieee80211_tx_control *control, struct sk_buff *skb)
763e705c121SKalle Valo {
764e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
765e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
766e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
767e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
768cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
769cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
77090723da6SShaul Triebitz 	u32 link_id = u32_get_bits(info->control.flags,
77190723da6SShaul Triebitz 				   IEEE80211_TX_CTRL_MLO_LINK);
77290723da6SShaul Triebitz 	struct ieee80211_sta *tmp_sta = sta;
773e705c121SKalle Valo 
774e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
775e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
776e705c121SKalle Valo 		goto drop;
777e705c121SKalle Valo 	}
778e705c121SKalle Valo 
779cfbc6c4cSSara Sharon 	if (offchannel &&
780e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
781e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
782e705c121SKalle Valo 		goto drop;
783e705c121SKalle Valo 
78410516783SJohannes Berg 	/*
78510516783SJohannes Berg 	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
78610516783SJohannes Berg 	 * so we treat the others as broadcast
78710516783SJohannes Berg 	 */
78810516783SJohannes Berg 	if (ieee80211_is_mgmt(hdr->frame_control))
789e705c121SKalle Valo 		sta = NULL;
790e705c121SKalle Valo 
791dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
792cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
793cfbc6c4cSSara Sharon 	    !offchannel) {
794dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
795dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
796650cadb7SGregory Greenman 		u8 ap_sta_id = READ_ONCE(mvmvif->deflink.ap_sta_id);
797dc1aca22SAndrei Otcheretianski 
798be9ae34eSNathan Errera 		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
799dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
800dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
801dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
802dc1aca22SAndrei Otcheretianski 				goto drop;
803dc1aca22SAndrei Otcheretianski 		}
804dc1aca22SAndrei Otcheretianski 	}
805dc1aca22SAndrei Otcheretianski 
80690723da6SShaul Triebitz 	if (tmp_sta && !sta && link_id != IEEE80211_LINK_UNSPECIFIED &&
80790723da6SShaul Triebitz 	    !ieee80211_is_probe_resp(hdr->frame_control)) {
80890723da6SShaul Triebitz 		/* translate MLD addresses to LINK addresses */
80990723da6SShaul Triebitz 		struct ieee80211_link_sta *link_sta =
81090723da6SShaul Triebitz 			rcu_dereference(tmp_sta->link[link_id]);
81190723da6SShaul Triebitz 		struct ieee80211_bss_conf *link_conf =
81290723da6SShaul Triebitz 			rcu_dereference(info->control.vif->link_conf[link_id]);
81390723da6SShaul Triebitz 		struct ieee80211_mgmt *mgmt;
81490723da6SShaul Triebitz 
81590723da6SShaul Triebitz 		if (WARN_ON(!link_sta || !link_conf))
81690723da6SShaul Triebitz 			goto drop;
81790723da6SShaul Triebitz 
81890723da6SShaul Triebitz 		/* if sta is NULL, the frame is a management frame */
81990723da6SShaul Triebitz 		mgmt = (void *)hdr;
82090723da6SShaul Triebitz 		memcpy(mgmt->da, link_sta->addr, ETH_ALEN);
82190723da6SShaul Triebitz 		memcpy(mgmt->sa, link_conf->addr, ETH_ALEN);
82290723da6SShaul Triebitz 		memcpy(mgmt->bssid, link_conf->bssid, ETH_ALEN);
82390723da6SShaul Triebitz 	}
82490723da6SShaul Triebitz 
825df2378abSJohannes Berg 	iwl_mvm_tx_skb(mvm, skb, sta);
826e705c121SKalle Valo 	return;
827e705c121SKalle Valo  drop:
828e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
829e705c121SKalle Valo }
830e705c121SKalle Valo 
831cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
832e705c121SKalle Valo {
833cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
834cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
835cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
836cfbc6c4cSSara Sharon 
837fba8248eSSara Sharon 	/*
838fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
839fba8248eSSara Sharon 	 * taker all the work.
840fba8248eSSara Sharon 	 *
841fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
842fba8248eSSara Sharon 	 *
843fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
844fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
845fba8248eSSara Sharon 	 *
846fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
847fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
848fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
849fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
850fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
851fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
852fba8248eSSara Sharon 	 *
853fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
854fba8248eSSara Sharon 	 * check the queue, so do nothing.
855fba8248eSSara Sharon 	 */
856fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
857fba8248eSSara Sharon 		return;
858cfbc6c4cSSara Sharon 
859cfbc6c4cSSara Sharon 	rcu_read_lock();
860fba8248eSSara Sharon 	do {
861b58e3d43SJohannes Berg 		while (likely(!test_bit(IWL_MVM_TXQ_STATE_STOP_FULL,
862b58e3d43SJohannes Berg 					&mvmtxq->state) &&
863b58e3d43SJohannes Berg 			      !test_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT,
864b58e3d43SJohannes Berg 					&mvmtxq->state) &&
86500520b7aSEmmanuel Grumbach 			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
866cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
867cfbc6c4cSSara Sharon 
868f50d693bSSara Sharon 			if (!skb) {
869f50d693bSSara Sharon 				if (txq->sta)
870f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
871f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
872f50d693bSSara Sharon 						     txq->sta->addr,
873f50d693bSSara Sharon 						     txq->tid);
874cfbc6c4cSSara Sharon 				break;
875f50d693bSSara Sharon 			}
876cfbc6c4cSSara Sharon 
877cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
878cfbc6c4cSSara Sharon 		}
879fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
880cfbc6c4cSSara Sharon 	rcu_read_unlock();
881e705c121SKalle Valo }
882e705c121SKalle Valo 
883cbce62a3SMiri Korenblit void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
884cfbc6c4cSSara Sharon 			       struct ieee80211_txq *txq)
885e705c121SKalle Valo {
886cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
887cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
888e705c121SKalle Valo 
889923bf981SJohannes Berg 	if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
890923bf981SJohannes Berg 	    !txq->sta) {
891cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
892cfbc6c4cSSara Sharon 		return;
893cfbc6c4cSSara Sharon 	}
894cfbc6c4cSSara Sharon 
895923bf981SJohannes Berg 	/* iwl_mvm_mac_itxq_xmit() will later be called by the worker
896923bf981SJohannes Berg 	 * to handle any packets we leave on the txq now
897923bf981SJohannes Berg 	 */
898cfbc6c4cSSara Sharon 
899923bf981SJohannes Berg 	spin_lock_bh(&mvm->add_stream_lock);
900923bf981SJohannes Berg 	/* The list is being deleted only after the queue is fully allocated. */
901923bf981SJohannes Berg 	if (list_empty(&mvmtxq->list) &&
902923bf981SJohannes Berg 	    /* recheck under lock */
903923bf981SJohannes Berg 	    !test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) {
904cfbc6c4cSSara Sharon 		list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
905cfbc6c4cSSara Sharon 		schedule_work(&mvm->add_stream_wk);
906e705c121SKalle Valo 	}
907923bf981SJohannes Berg 	spin_unlock_bh(&mvm->add_stream_lock);
908923bf981SJohannes Berg }
909e705c121SKalle Valo 
910e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
911e705c121SKalle Valo 	do {								\
912e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
913e705c121SKalle Valo 			break;						\
9147174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
915e705c121SKalle Valo 	} while (0)
916e705c121SKalle Valo 
917e705c121SKalle Valo static void
918e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
919e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
920e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
921e705c121SKalle Valo {
922e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
923e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
924e705c121SKalle Valo 
9256c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
9266c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
9276c042d75SSara Sharon 	if (!trig)
928e705c121SKalle Valo 		return;
929e705c121SKalle Valo 
930e705c121SKalle Valo 	ba_trig = (void *)trig->data;
931e705c121SKalle Valo 
932e705c121SKalle Valo 	switch (action) {
933e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
934e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
935e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
936e705c121SKalle Valo 
937e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
938e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
939e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
940e705c121SKalle Valo 		break;
941e705c121SKalle Valo 		}
942e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
943e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
944e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
945e705c121SKalle Valo 				 sta->addr, tid);
946e705c121SKalle Valo 		break;
947e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
948e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
949e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
950e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
951e705c121SKalle Valo 		break;
952e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
953e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
954e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
955e705c121SKalle Valo 				 sta->addr, tid);
956e705c121SKalle Valo 		break;
957e705c121SKalle Valo 	default:
958e705c121SKalle Valo 		break;
959e705c121SKalle Valo 	}
960e705c121SKalle Valo }
961e705c121SKalle Valo 
962cbce62a3SMiri Korenblit int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
963e705c121SKalle Valo 			     struct ieee80211_vif *vif,
96450ea05efSSara Sharon 			     struct ieee80211_ampdu_params *params)
965e705c121SKalle Valo {
966e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
967e705c121SKalle Valo 	int ret;
96850ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
96950ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
97050ea05efSSara Sharon 	u16 tid = params->tid;
97150ea05efSSara Sharon 	u16 *ssn = &params->ssn;
972514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
973bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
97410b2b201SSara Sharon 	u16 timeout = params->timeout;
975e705c121SKalle Valo 
976e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
977e705c121SKalle Valo 		     sta->addr, tid, action);
978e705c121SKalle Valo 
979e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
980e705c121SKalle Valo 		return -EACCES;
981e705c121SKalle Valo 
982e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
983e705c121SKalle Valo 
984e705c121SKalle Valo 	switch (action) {
985e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
986650cadb7SGregory Greenman 		if (iwl_mvm_vif_from_mac80211(vif)->deflink.ap_sta_id ==
987c8ee33e1SGregory Greenman 		    iwl_mvm_sta_from_mac80211(sta)->deflink.sta_id) {
988b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
989b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
990b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
991b0ffe455SJohannes Berg 
992b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
993b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
994b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
995b0ffe455SJohannes Berg 		}
996e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
997e705c121SKalle Valo 			ret = -EINVAL;
998e705c121SKalle Valo 			break;
999e705c121SKalle Valo 		}
100010b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
100110b2b201SSara Sharon 					 timeout);
1002e705c121SKalle Valo 		break;
1003e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
100410b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
100510b2b201SSara Sharon 					 timeout);
1006e705c121SKalle Valo 		break;
1007e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
1008e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
1009e705c121SKalle Valo 			ret = -EINVAL;
1010e705c121SKalle Valo 			break;
1011e705c121SKalle Valo 		}
1012e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1013e705c121SKalle Valo 		break;
1014e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
1015e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1016e705c121SKalle Valo 		break;
1017e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1018e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1019e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1020e705c121SKalle Valo 		break;
1021e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1022bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1023bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
1024e705c121SKalle Valo 		break;
1025e705c121SKalle Valo 	default:
1026e705c121SKalle Valo 		WARN_ON_ONCE(1);
1027e705c121SKalle Valo 		ret = -EINVAL;
1028e705c121SKalle Valo 		break;
1029e705c121SKalle Valo 	}
1030e705c121SKalle Valo 
1031e705c121SKalle Valo 	if (!ret) {
1032e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
1033e705c121SKalle Valo 
1034e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
1035e705c121SKalle Valo 			rx_ba_ssn = *ssn;
1036e705c121SKalle Valo 
1037e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1038e705c121SKalle Valo 					    rx_ba_ssn, action);
1039e705c121SKalle Valo 	}
1040e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1041e705c121SKalle Valo 
1042e705c121SKalle Valo 	return ret;
1043e705c121SKalle Valo }
1044e705c121SKalle Valo 
1045e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1046e705c121SKalle Valo 				     struct ieee80211_vif *vif)
1047e705c121SKalle Valo {
1048e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
1049e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1050bf976c81SJohannes Berg 	struct iwl_probe_resp_data *probe_data;
105179faae3aSGregory Greenman 	unsigned int link_id;
1052e705c121SKalle Valo 
1053e705c121SKalle Valo 	mvmvif->uploaded = false;
1054e705c121SKalle Valo 
1055e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
1056e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1057e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
1058e705c121SKalle Valo 
1059e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
1060828c79d9SEmmanuel Grumbach 	mvmvif->ap_sta = NULL;
106179faae3aSGregory Greenman 
106279faae3aSGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
106379faae3aSGregory Greenman 		mvmvif->link[link_id]->ap_sta_id = IWL_MVM_INVALID_STA;
106479faae3aSGregory Greenman 		mvmvif->link[link_id]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
106579faae3aSGregory Greenman 		mvmvif->link[link_id]->phy_ctxt = NULL;
1066bf976c81SJohannes Berg 		mvmvif->link[link_id]->active = 0;
1067d615ea32SJohannes Berg 		mvmvif->link[link_id]->igtk = NULL;
106879faae3aSGregory Greenman 	}
1069bf976c81SJohannes Berg 
1070bf976c81SJohannes Berg 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1071bf976c81SJohannes Berg 					       lockdep_is_held(&mvm->mutex));
1072bf976c81SJohannes Berg 	if (probe_data)
1073bf976c81SJohannes Berg 		kfree_rcu(probe_data, rcu_head);
1074bf976c81SJohannes Berg 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1075e705c121SKalle Valo }
1076e705c121SKalle Valo 
1077e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1078e705c121SKalle Valo {
1079fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1080e705c121SKalle Valo 
10819bf13beeSJohannes Berg 	mvm->cur_aid = 0;
10829bf13beeSJohannes Berg 
1083e705c121SKalle Valo 	mvm->scan_status = 0;
1084e705c121SKalle Valo 	mvm->ps_disabled = false;
1085b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
1086e705c121SKalle Valo 
1087e705c121SKalle Valo 	/* just in case one was running */
1088305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
1089e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
1090e705c121SKalle Valo 
1091fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
1092fc36ffdaSJohannes Berg 
1093e705c121SKalle Valo 	/*
1094e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
1095e705c121SKalle Valo 	 * gone down during the HW restart
1096e705c121SKalle Valo 	 */
1097e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1098e705c121SKalle Valo 
1099e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
1100e705c121SKalle Valo 
1101e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
11029c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1103e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1104e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1105e705c121SKalle Valo 
1106e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
1107e705c121SKalle Valo 
1108e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
11097174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1110baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
1111fb40cd9dSMiri Korenblit #ifdef CONFIG_IWLWIFI_DEBUGFS
1112fb40cd9dSMiri Korenblit 	mvm->beacon_inject_active = false;
1113fb40cd9dSMiri Korenblit #endif
1114e705c121SKalle Valo 
1115e705c121SKalle Valo 	/* keep statistics ticking */
1116e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
1117e705c121SKalle Valo }
1118e705c121SKalle Valo 
1119e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1120e705c121SKalle Valo {
1121e705c121SKalle Valo 	int ret;
1122e705c121SKalle Valo 
1123e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1124e705c121SKalle Valo 
11256d19a5ebSEmmanuel Grumbach 	ret = iwl_mvm_mei_get_ownership(mvm);
11266d19a5ebSEmmanuel Grumbach 	if (ret)
11276d19a5ebSEmmanuel Grumbach 		return ret;
11286d19a5ebSEmmanuel Grumbach 
11296d19a5ebSEmmanuel Grumbach 	if (mvm->mei_nvm_data) {
11306d19a5ebSEmmanuel Grumbach 		/* We got the NIC, we can now free the MEI NVM data */
11316d19a5ebSEmmanuel Grumbach 		kfree(mvm->mei_nvm_data);
11326d19a5ebSEmmanuel Grumbach 		mvm->mei_nvm_data = NULL;
11336d19a5ebSEmmanuel Grumbach 
11346d19a5ebSEmmanuel Grumbach 		/*
11356d19a5ebSEmmanuel Grumbach 		 * We can't free the nvm_data we allocated based on the SAP
11366d19a5ebSEmmanuel Grumbach 		 * data because we registered to cfg80211 with the channels
11376d19a5ebSEmmanuel Grumbach 		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
11386d19a5ebSEmmanuel Grumbach 		 * just in order to be able free it later.
11396d19a5ebSEmmanuel Grumbach 		 * NULLify nvm_data so that we will read the NVM from the
11406d19a5ebSEmmanuel Grumbach 		 * firmware this time.
11416d19a5ebSEmmanuel Grumbach 		 */
11426d19a5ebSEmmanuel Grumbach 		mvm->temp_nvm_data = mvm->nvm_data;
11436d19a5ebSEmmanuel Grumbach 		mvm->nvm_data = NULL;
11446d19a5ebSEmmanuel Grumbach 	}
11456d19a5ebSEmmanuel Grumbach 
1146bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1147bf8b286fSJohannes Berg 		/*
1148bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1149bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1150bf8b286fSJohannes Berg 		 */
1151bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1152bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1153e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1154e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1155a42b2af3SLuca Coelho 	}
1156e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1157e705c121SKalle Valo 
1158b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1159b108d8c7SShahar S Matityahu 			       NULL);
1160b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1161b108d8c7SShahar S Matityahu 			       NULL);
1162da2eb669SSara Sharon 
1163e8fe3b41SIlan Peer 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1164e8fe3b41SIlan Peer 
1165e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1166e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1167e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1168e705c121SKalle Valo 		 * would do.
1169e705c121SKalle Valo 		 */
1170e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1171e705c121SKalle Valo 	}
1172e705c121SKalle Valo 
1173e705c121SKalle Valo 	return ret;
1174e705c121SKalle Valo }
1175e705c121SKalle Valo 
1176cbce62a3SMiri Korenblit int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1177e705c121SKalle Valo {
1178e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1179e705c121SKalle Valo 	int ret;
11805283dd67SMordechay Goodstein 	int retry, max_retry = 0;
1181e705c121SKalle Valo 
1182e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
11835283dd67SMordechay Goodstein 
11845283dd67SMordechay Goodstein 	/* we are starting the mac not in error flow, and restart is enabled */
11855283dd67SMordechay Goodstein 	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
11865283dd67SMordechay Goodstein 	    iwlwifi_mod_params.fw_restart) {
11875283dd67SMordechay Goodstein 		max_retry = IWL_MAX_INIT_RETRY;
11885283dd67SMordechay Goodstein 		/*
11895283dd67SMordechay Goodstein 		 * This will prevent mac80211 recovery flows to trigger during
11905283dd67SMordechay Goodstein 		 * init failures
11915283dd67SMordechay Goodstein 		 */
11925283dd67SMordechay Goodstein 		set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
11935283dd67SMordechay Goodstein 	}
11945283dd67SMordechay Goodstein 
11955283dd67SMordechay Goodstein 	for (retry = 0; retry <= max_retry; retry++) {
1196e705c121SKalle Valo 		ret = __iwl_mvm_mac_start(mvm);
1197bdd94061SJohannes Berg 		if (!ret || mvm->pldr_sync)
11985283dd67SMordechay Goodstein 			break;
11995283dd67SMordechay Goodstein 
12005283dd67SMordechay Goodstein 		IWL_ERR(mvm, "mac start retry %d\n", retry);
12015283dd67SMordechay Goodstein 	}
12025283dd67SMordechay Goodstein 	clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
12035283dd67SMordechay Goodstein 
1204e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1205e705c121SKalle Valo 
12067ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
12077ce1f215SEmmanuel Grumbach 
1208e705c121SKalle Valo 	return ret;
1209e705c121SKalle Valo }
1210e705c121SKalle Valo 
1211e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1212e705c121SKalle Valo {
1213e705c121SKalle Valo 	int ret;
1214e705c121SKalle Valo 
1215e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1216e705c121SKalle Valo 
1217e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
12184d4183c4SEmmanuel Grumbach 
1219e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1220e705c121SKalle Valo 	if (ret)
1221e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1222e705c121SKalle Valo 			ret);
1223e705c121SKalle Valo 
1224f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1225f130bb75SMordechay Goodstein 
1226e705c121SKalle Valo 	/*
1227e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1228e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1229e705c121SKalle Valo 	 */
1230e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1231e705c121SKalle Valo 
1232e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1233e705c121SKalle Valo }
1234e705c121SKalle Valo 
1235cbce62a3SMiri Korenblit void iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1236e705c121SKalle Valo 				   enum ieee80211_reconfig_type reconfig_type)
1237e705c121SKalle Valo {
1238e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1239e705c121SKalle Valo 
1240e705c121SKalle Valo 	switch (reconfig_type) {
1241e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1242e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1243e705c121SKalle Valo 		break;
1244e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1245e705c121SKalle Valo 		break;
1246e705c121SKalle Valo 	}
1247e705c121SKalle Valo }
1248e705c121SKalle Valo 
1249e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1250e705c121SKalle Valo {
1251e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1252e705c121SKalle Valo 
1253b68bd2e3SIlan Peer 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1254b68bd2e3SIlan Peer 
1255e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1256e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1257e705c121SKalle Valo 	 */
1258e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1259e705c121SKalle Valo 
1260e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1261e705c121SKalle Valo 
12621724fc78SEmmanuel Grumbach 	if (!iwl_mvm_has_new_station_api(mvm->fw))
1263f327236dSSharon 		iwl_mvm_rm_aux_sta(mvm);
1264f327236dSSharon 
1265fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1266e705c121SKalle Valo 
1267e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1268e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1269e705c121SKalle Valo 
1270e705c121SKalle Valo 	/*
1271155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1272155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1273155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1274e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1275e705c121SKalle Valo 	 * HW restart was requested.
1276e705c121SKalle Valo 	 */
1277155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1278155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1279155f7e04SEmmanuel Grumbach 			       &mvm->status))
1280e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1281e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1282e705c121SKalle Valo 
1283e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1284e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1285e705c121SKalle Valo 	 */
1286e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1287e705c121SKalle Valo 		int i;
1288e705c121SKalle Valo 
1289e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1290e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1291e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1292e705c121SKalle Valo 				      i))
1293e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1294e705c121SKalle Valo 		}
1295e705c121SKalle Valo 	}
1296e705c121SKalle Valo }
1297e705c121SKalle Valo 
1298cbce62a3SMiri Korenblit void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1299e705c121SKalle Valo {
1300e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1301e705c121SKalle Valo 
1302e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
130324afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1304771147b0SJohannes Berg 
1305771147b0SJohannes Berg 	/*
1306771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1307771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1308771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
13097174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1310771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1311771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1312771147b0SJohannes Berg 	 */
1313771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1314771147b0SJohannes Berg 
1315d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
131669e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1317e705c121SKalle Valo 
1318f9084775SNathan Errera 	/*
1319f9084775SNathan Errera 	 * The work item could be running or queued if the
1320f9084775SNathan Errera 	 * ROC time event stops just as we get here.
1321f9084775SNathan Errera 	 */
1322f9084775SNathan Errera 	flush_work(&mvm->roc_done_wk);
1323f9084775SNathan Errera 
13247ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
13257ce1f215SEmmanuel Grumbach 
1326e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1327e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1328e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1329e705c121SKalle Valo 
1330e705c121SKalle Valo 	/*
1331e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1332e705c121SKalle Valo 	 * discover that its list is now empty.
1333e705c121SKalle Valo 	 */
1334e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1335e705c121SKalle Valo }
1336e705c121SKalle Valo 
13371ab26632SMiri Korenblit struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1338e705c121SKalle Valo {
1339e705c121SKalle Valo 	u16 i;
1340e705c121SKalle Valo 
1341e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1342e705c121SKalle Valo 
1343e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1344e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1345e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1346e705c121SKalle Valo 
1347e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1348e705c121SKalle Valo 	return NULL;
1349e705c121SKalle Valo }
1350e705c121SKalle Valo 
1351f551d013SGregory Greenman int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1352e705c121SKalle Valo 			 s16 tx_power)
1353e705c121SKalle Valo {
1354971cbe50SJohannes Berg 	u32 cmd_id = REDUCE_TX_POWER_CMD;
13550791c2fcSHaim Dreyfuss 	int len;
1356216cdfb5SLuca Coelho 	struct iwl_dev_tx_power_cmd cmd = {
1357216cdfb5SLuca Coelho 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1358216cdfb5SLuca Coelho 		.common.mac_context_id =
1359e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1360216cdfb5SLuca Coelho 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1361e705c121SKalle Valo 	};
1362971cbe50SJohannes Berg 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1363e80bfd11SMordechay Goodstein 					   IWL_FW_CMD_VER_UNKNOWN);
1364e705c121SKalle Valo 
1365e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1366216cdfb5SLuca Coelho 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1367e705c121SKalle Valo 
1368b0aa02b3SAyala Barazani 	if (cmd_ver == 7)
1369b0aa02b3SAyala Barazani 		len = sizeof(cmd.v7);
1370b0aa02b3SAyala Barazani 	else if (cmd_ver == 6)
1371fbb7957dSLuca Coelho 		len = sizeof(cmd.v6);
1372fbb7957dSLuca Coelho 	else if (fw_has_api(&mvm->fw->ucode_capa,
13730791c2fcSHaim Dreyfuss 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
13740791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
13750791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
13760791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
13770791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
13780791c2fcSHaim Dreyfuss 	else
1379216cdfb5SLuca Coelho 		len = sizeof(cmd.v3);
1380216cdfb5SLuca Coelho 
1381216cdfb5SLuca Coelho 	/* all structs have the same common part, add it */
1382216cdfb5SLuca Coelho 	len += sizeof(cmd.common);
1383e705c121SKalle Valo 
1384971cbe50SJohannes Berg 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1385e705c121SKalle Valo }
1386e705c121SKalle Valo 
138703117f30SMiri Korenblit int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1388a469a593SEmmanuel Grumbach 				struct ieee80211_vif *vif,
1389a469a593SEmmanuel Grumbach 				struct ieee80211_bss_conf *link_conf)
1390f6780614SSara Sharon {
1391f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1392f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1393f6780614SSara Sharon 	int ret;
1394f6780614SSara Sharon 
1395f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1396f6780614SSara Sharon 
1397f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1398f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
13993723c7c5SEmmanuel Grumbach 		unsigned int link_id = link_conf->link_id;
14003723c7c5SEmmanuel Grumbach 		u8 ap_sta_id = mvmvif->link[link_id]->ap_sta_id;
1401f6780614SSara Sharon 
1402f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
14033723c7c5SEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm, ap_sta_id);
1404f6780614SSara Sharon 
1405f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1406f6780614SSara Sharon 			ret = -EIO;
1407f6780614SSara Sharon 			goto out_unlock;
1408f6780614SSara Sharon 		}
1409f6780614SSara Sharon 
1410f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
141103117f30SMiri Korenblit 		if (mvm->mld_api_is_used)
141203117f30SMiri Korenblit 			iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
141303117f30SMiri Korenblit 		else
1414f6780614SSara Sharon 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1415f6780614SSara Sharon 
14160202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
14170202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1418f6780614SSara Sharon 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1419f6780614SSara Sharon 			if (ret)
1420f6780614SSara Sharon 				goto out_unlock;
1421f6780614SSara Sharon 
1422f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1423f6780614SSara Sharon 		}
14240202bcf0SEmmanuel Grumbach 	}
1425f6780614SSara Sharon 
1426f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1427f6780614SSara Sharon 
1428f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1429f6780614SSara Sharon 
1430f6780614SSara Sharon out_unlock:
1431caf46377SSara Sharon 	if (mvmvif->csa_failed)
1432caf46377SSara Sharon 		ret = -EIO;
1433f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1434f6780614SSara Sharon 
1435f6780614SSara Sharon 	return ret;
1436f6780614SSara Sharon }
1437f6780614SSara Sharon 
1438cbce62a3SMiri Korenblit void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1439f6780614SSara Sharon 				  struct ieee80211_vif *vif)
1440f6780614SSara Sharon {
1441f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1442f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1443f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1444f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1445f6780614SSara Sharon 							  mvmvif->color)),
1446f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1447f6780614SSara Sharon 	};
1448f6780614SSara Sharon 
1449ad12b231SNathan Errera 	/*
1450ad12b231SNathan Errera 	 * In the new flow since FW is in charge of the timing,
1451ad12b231SNathan Errera 	 * if driver has canceled the channel switch he will receive the
1452ad12b231SNathan Errera 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1453ad12b231SNathan Errera 	 */
1454ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1455ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1456ad12b231SNathan Errera 		return;
1457ad12b231SNathan Errera 
1458f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1459f6780614SSara Sharon 
1460f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
146158ddd9b6SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
146258ddd9b6SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
146358ddd9b6SEmmanuel Grumbach 		iwl_mvm_remove_csa_period(mvm, vif);
146458ddd9b6SEmmanuel Grumbach 	else
1465f6780614SSara Sharon 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1466f6780614SSara Sharon 					     WIDE_ID(MAC_CONF_GROUP,
1467f6780614SSara Sharon 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1468f6780614SSara Sharon 					     0, sizeof(cmd), &cmd));
1469caf46377SSara Sharon 	mvmvif->csa_failed = true;
1470f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1471f6780614SSara Sharon 
1472a469a593SEmmanuel Grumbach 	/* If we're here, we can't support MLD */
1473a469a593SEmmanuel Grumbach 	iwl_mvm_post_channel_switch(hw, vif, &vif->bss_conf);
1474f6780614SSara Sharon }
1475f6780614SSara Sharon 
14761ab26632SMiri Korenblit void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1477f6780614SSara Sharon {
1478f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1479f6780614SSara Sharon 	struct ieee80211_vif *vif;
1480f6780614SSara Sharon 
1481f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1482f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1483f6780614SSara Sharon 
148470162580SShaul Triebitz 	/* Trigger disconnect (should clear the CSA state) */
1485a469a593SEmmanuel Grumbach 	ieee80211_chswitch_done(vif, false, 0);
1486f6780614SSara Sharon }
1487f6780614SSara Sharon 
14885abf3154SMordechay Goodstein static u8
14895abf3154SMordechay Goodstein iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
14905abf3154SMordechay Goodstein {
14915abf3154SMordechay Goodstein 	int data_start;
14925abf3154SMordechay Goodstein 	int control_start;
14935abf3154SMordechay Goodstein 	int bw;
14945abf3154SMordechay Goodstein 
14955abf3154SMordechay Goodstein 	if (chandef->width == NL80211_CHAN_WIDTH_320)
14965abf3154SMordechay Goodstein 		bw = 320;
14975abf3154SMordechay Goodstein 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
14985abf3154SMordechay Goodstein 		bw = 160;
14995abf3154SMordechay Goodstein 	else
15005abf3154SMordechay Goodstein 		return 0;
15015abf3154SMordechay Goodstein 
15025abf3154SMordechay Goodstein 	/* data is bw wide so the start is half the width */
15035abf3154SMordechay Goodstein 	data_start = chandef->center_freq1 - bw / 2;
15045abf3154SMordechay Goodstein 	/* control is 20Mhz width */
15055abf3154SMordechay Goodstein 	control_start = chandef->chan->center_freq - 10;
15065abf3154SMordechay Goodstein 
15075abf3154SMordechay Goodstein 	return (control_start - data_start) / 80;
15085abf3154SMordechay Goodstein }
15095abf3154SMordechay Goodstein 
15101be4858eSJohannes Berg static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
15111be4858eSJohannes Berg 					 struct ieee80211_vif *vif)
1512e705c121SKalle Valo {
1513e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
15141be4858eSJohannes Berg 	int ret;
15151ab26632SMiri Korenblit 
15161ab26632SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
1517e705c121SKalle Valo 
15181be4858eSJohannes Berg 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
15191be4858eSJohannes Berg 	if (ret) {
15201be4858eSJohannes Berg 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
15211be4858eSJohannes Berg 		return ret;
15221be4858eSJohannes Berg 	}
15231be4858eSJohannes Berg 
15241be4858eSJohannes Berg 	/* Only queue for this station is the mcast queue,
15251be4858eSJohannes Berg 	 * which shouldn't be in TFD mask anyway
15261be4858eSJohannes Berg 	 */
15271be4858eSJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
15281be4858eSJohannes Berg 					vif->type,
15291be4858eSJohannes Berg 					IWL_STA_MULTICAST);
15301be4858eSJohannes Berg }
15311be4858eSJohannes Berg 
15321be4858eSJohannes Berg static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
15331be4858eSJohannes Berg 				     struct ieee80211_vif *vif)
15341be4858eSJohannes Berg {
15351be4858eSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
15361be4858eSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
15371be4858eSJohannes Berg 	int ret;
15381be4858eSJohannes Berg 
15391be4858eSJohannes Berg 	mutex_lock(&mvm->mutex);
15401be4858eSJohannes Berg 
1541e705c121SKalle Valo 	mvmvif->mvm = mvm;
1542650cadb7SGregory Greenman 
1543650cadb7SGregory Greenman 	/* the first link always points to the default one */
1544650cadb7SGregory Greenman 	mvmvif->link[0] = &mvmvif->deflink;
1545e705c121SKalle Valo 
1546e705c121SKalle Valo 	/*
1547e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1548e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1549e705c121SKalle Valo 	 * don't really have to check the types.
1550e705c121SKalle Valo 	 */
1551e705c121SKalle Valo 
1552e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1553e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1554650cadb7SGregory Greenman 		mvmvif->deflink.beacon_stats.accu_num_beacons +=
1555650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.num_beacons;
1556e705c121SKalle Valo 
1557e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
15581be4858eSJohannes Berg 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
15591be4858eSJohannes Berg 	if (ret)
15601be4858eSJohannes Berg 		goto out;
1561e705c121SKalle Valo 
1562698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1563698478c4SSara Sharon 
15641be4858eSJohannes Berg 	/* Currently not much to do for NAN */
15651be4858eSJohannes Berg 	if (vif->type == NL80211_IFTYPE_NAN) {
15661be4858eSJohannes Berg 		ret = 0;
15671be4858eSJohannes Berg 		goto out;
15681be4858eSJohannes Berg 	}
15691be4858eSJohannes Berg 
1570e705c121SKalle Valo 	/*
1571e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1572e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1573e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1574e705c121SKalle Valo 	 * only after the binding.
1575e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1576e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1577e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1578e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1579e705c121SKalle Valo 	 * allocating resources :)
1580e705c121SKalle Valo 	 */
1581e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1582e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
15831ab26632SMiri Korenblit 		iwl_mvm_vif_dbgfs_register(mvm, vif);
15841be4858eSJohannes Berg 		ret = 0;
15851be4858eSJohannes Berg 		goto out;
15861ab26632SMiri Korenblit 	}
15871ab26632SMiri Korenblit 
15881ab26632SMiri Korenblit 	mvmvif->features |= hw->netdev_features;
1589650cadb7SGregory Greenman 
1590e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1591e705c121SKalle Valo 	if (ret)
159298c0de7bSMiri Korenblit 		goto out_unlock;
1593e705c121SKalle Valo 
1594e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1595e705c121SKalle Valo 	if (ret)
1596e705c121SKalle Valo 		goto out_remove_mac;
1597e705c121SKalle Valo 
1598e705c121SKalle Valo 	/* beacon filtering */
1599e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1600e705c121SKalle Valo 	if (ret)
1601e705c121SKalle Valo 		goto out_remove_mac;
1602e705c121SKalle Valo 
1603e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1604e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1605e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1606e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1607e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1608e705c121SKalle Valo 	}
1609e705c121SKalle Valo 
1610e705c121SKalle Valo 	/*
1611e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1612e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1613e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1614e705c121SKalle Valo 	 */
1615e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1616e705c121SKalle Valo 
1617650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1618650cadb7SGregory Greenman 		if (!mvmvif->deflink.phy_ctxt) {
1619e705c121SKalle Valo 			ret = -ENOSPC;
1620e705c121SKalle Valo 			goto out_free_bf;
1621e705c121SKalle Valo 		}
1622e705c121SKalle Valo 
1623650cadb7SGregory Greenman 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
1624e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1625e705c121SKalle Valo 		if (ret)
1626e705c121SKalle Valo 			goto out_unref_phy;
1627e705c121SKalle Valo 
1628d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1629e705c121SKalle Valo 		if (ret)
1630e705c121SKalle Valo 			goto out_unbind;
1631e705c121SKalle Valo 
1632e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1633e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1634e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1635e705c121SKalle Valo 	}
1636e705c121SKalle Valo 
1637b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1638f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1639f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1640b0ffe455SJohannes Berg 
16415abf3154SMordechay Goodstein 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1642baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
16435abf3154SMordechay Goodstein 		mvm->monitor_p80 =
16445abf3154SMordechay Goodstein 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chandef);
16455abf3154SMordechay Goodstein 	}
1646baf41bc3SShaul Triebitz 
1647e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
16486d19a5ebSEmmanuel Grumbach 
16496d19a5ebSEmmanuel Grumbach 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
16506d19a5ebSEmmanuel Grumbach 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
16516d19a5ebSEmmanuel Grumbach 	    !mvm->csme_vif && mvm->mei_registered) {
16526d19a5ebSEmmanuel Grumbach 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
16536d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
16546d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = vif;
16556d19a5ebSEmmanuel Grumbach 	}
16566d19a5ebSEmmanuel Grumbach 
16571ab26632SMiri Korenblit out:
16581ab26632SMiri Korenblit 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
16591ab26632SMiri Korenblit 		     vif->type == NL80211_IFTYPE_ADHOC))
16601ab26632SMiri Korenblit 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
16611ab26632SMiri Korenblit 
1662e705c121SKalle Valo 	goto out_unlock;
1663e705c121SKalle Valo 
1664e705c121SKalle Valo  out_unbind:
1665e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1666e705c121SKalle Valo  out_unref_phy:
1667650cadb7SGregory Greenman 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1668e705c121SKalle Valo  out_free_bf:
1669e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1670e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1671e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1672e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1673e705c121SKalle Valo 	}
1674e705c121SKalle Valo  out_remove_mac:
1675650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
1676e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1677e705c121SKalle Valo  out_unlock:
1678e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1679e705c121SKalle Valo 
1680e705c121SKalle Valo 	return ret;
1681e705c121SKalle Valo }
1682e705c121SKalle Valo 
1683cb145863SJohannes Berg void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1684e705c121SKalle Valo 				 struct ieee80211_vif *vif)
1685e705c121SKalle Valo {
1686e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1687e705c121SKalle Valo 		/*
1688e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1689e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1690e705c121SKalle Valo 		 * queue are sent in ROC session.
1691e705c121SKalle Valo 		 */
1692e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1693e705c121SKalle Valo 	}
1694e705c121SKalle Valo }
1695e705c121SKalle Valo 
169660efeca1SMiri Korenblit /* This function is doing the common part of removing the interface for
169760efeca1SMiri Korenblit  * both - MLD and non-MLD modes. Returns true if removing the interface
169860efeca1SMiri Korenblit  * is done
169960efeca1SMiri Korenblit  */
1700cb145863SJohannes Berg static bool iwl_mvm_mac_remove_interface_common(struct ieee80211_hw *hw,
1701e705c121SKalle Valo 						struct ieee80211_vif *vif)
1702e705c121SKalle Valo {
1703e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1704e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
170586e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1706e705c121SKalle Valo 
1707e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1708e705c121SKalle Valo 
1709b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1710b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1711b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1712b0ffe455SJohannes Berg 
1713e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1714e705c121SKalle Valo 
17156d19a5ebSEmmanuel Grumbach 	if (vif == mvm->csme_vif) {
17166d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(NULL);
17176d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = NULL;
17186d19a5ebSEmmanuel Grumbach 	}
17196d19a5ebSEmmanuel Grumbach 
1720650cadb7SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
172186e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
1722650cadb7SGregory Greenman 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
172386e177d8SGregory Greenman 	if (probe_data)
172486e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
172586e177d8SGregory Greenman 
1726e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1727e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1728e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1729e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1730e705c121SKalle Valo 	}
1731e705c121SKalle Valo 
1732b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1733b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1734b73f9a4aSJohannes Berg 
1735e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1736e705c121SKalle Valo 
1737e705c121SKalle Valo 	/*
1738e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1739e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1740e705c121SKalle Valo 	 */
1741e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1742e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1743e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1744e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1745e705c121SKalle Valo 			mvm->noa_vif = NULL;
1746e705c121SKalle Valo 			mvm->noa_duration = 0;
1747e705c121SKalle Valo 		}
1748e705c121SKalle Valo #endif
174960efeca1SMiri Korenblit 		return true;
1750e705c121SKalle Valo 	}
1751e705c121SKalle Valo 
175260efeca1SMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
175360efeca1SMiri Korenblit 	return false;
175460efeca1SMiri Korenblit }
175560efeca1SMiri Korenblit 
175660efeca1SMiri Korenblit static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
175760efeca1SMiri Korenblit 					 struct ieee80211_vif *vif)
175860efeca1SMiri Korenblit {
175960efeca1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
176060efeca1SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
176160efeca1SMiri Korenblit 
176260efeca1SMiri Korenblit 	if (iwl_mvm_mac_remove_interface_common(hw, vif))
176360efeca1SMiri Korenblit 		goto out;
176460efeca1SMiri Korenblit 
1765e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1766e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1767d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1768e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1769650cadb7SGregory Greenman 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1770650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = NULL;
1771e705c121SKalle Valo 	}
1772e705c121SKalle Valo 
1773e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1774e705c121SKalle Valo 
1775698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1776698478c4SSara Sharon 
1777baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1778baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1779baf41bc3SShaul Triebitz 
178060efeca1SMiri Korenblit out:
178160efeca1SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP ||
178260efeca1SMiri Korenblit 	    vif->type == NL80211_IFTYPE_ADHOC) {
1783650cadb7SGregory Greenman 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
178460efeca1SMiri Korenblit 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1785e705c121SKalle Valo 	}
1786e705c121SKalle Valo 
1787e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1788e705c121SKalle Valo }
1789e705c121SKalle Valo 
1790e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1791e705c121SKalle Valo 	struct iwl_mvm *mvm;
1792e705c121SKalle Valo 	int port_id;
1793e705c121SKalle Valo };
1794e705c121SKalle Valo 
1795e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1796e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1797e705c121SKalle Valo {
1798e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1799e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1800e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
180197bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
180297bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
180397bce57bSLuca Coelho 		.flags = CMD_ASYNC,
180497bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
180597bce57bSLuca Coelho 	};
1806e705c121SKalle Valo 	int ret, len;
1807e705c121SKalle Valo 
1808e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1809e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1810e705c121SKalle Valo 		return;
1811e705c121SKalle Valo 
1812e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1813f276e20bSJohannes Berg 	    !vif->cfg.assoc)
1814e705c121SKalle Valo 		return;
1815e705c121SKalle Valo 
1816e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1817e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1818e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1819e705c121SKalle Valo 
182097bce57bSLuca Coelho 	hcmd.len[0] = len;
182197bce57bSLuca Coelho 	hcmd.data[0] = cmd;
182297bce57bSLuca Coelho 
182397bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1824e705c121SKalle Valo 	if (ret)
1825e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1826e705c121SKalle Valo }
1827e705c121SKalle Valo 
1828e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1829e705c121SKalle Valo {
1830e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1831e705c121SKalle Valo 		.mvm = mvm,
1832e705c121SKalle Valo 	};
1833db66abeeSJohannes Berg 	int ret;
1834e705c121SKalle Valo 
1835e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1836e705c121SKalle Valo 
1837e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1838e705c121SKalle Valo 		return;
1839e705c121SKalle Valo 
1840e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1841e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1842e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1843db66abeeSJohannes Berg 
1844db66abeeSJohannes Berg 	/*
1845db66abeeSJohannes Berg 	 * Send a (synchronous) ech command so that we wait for the
1846db66abeeSJohannes Berg 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
1847db66abeeSJohannes Berg 	 * the interface iterator. Otherwise, we might get here over
1848db66abeeSJohannes Berg 	 * and over again (by userspace just sending a lot of these)
1849db66abeeSJohannes Berg 	 * and the CPU can send them faster than the firmware can
1850db66abeeSJohannes Berg 	 * process them.
1851db66abeeSJohannes Berg 	 * Note that the CPU is still faster - but with this we'll
1852db66abeeSJohannes Berg 	 * actually send fewer commands overall because the CPU will
1853db66abeeSJohannes Berg 	 * not schedule the work in mac80211 as frequently if it's
1854db66abeeSJohannes Berg 	 * still running when rescheduled (possibly multiple times).
1855db66abeeSJohannes Berg 	 */
1856db66abeeSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1857db66abeeSJohannes Berg 	if (ret)
1858db66abeeSJohannes Berg 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
1859e705c121SKalle Valo }
1860e705c121SKalle Valo 
1861cbce62a3SMiri Korenblit u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1862e705c121SKalle Valo 			      struct netdev_hw_addr_list *mc_list)
1863e705c121SKalle Valo {
1864e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1865e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1866e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1867e705c121SKalle Valo 	int addr_count;
1868e705c121SKalle Valo 	bool pass_all;
1869e705c121SKalle Valo 	int len;
1870e705c121SKalle Valo 
1871e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1872e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1873e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1874e705c121SKalle Valo 	if (pass_all)
1875e705c121SKalle Valo 		addr_count = 0;
1876e705c121SKalle Valo 
1877e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1878e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1879e705c121SKalle Valo 	if (!cmd)
1880e705c121SKalle Valo 		return 0;
1881e705c121SKalle Valo 
1882e705c121SKalle Valo 	if (pass_all) {
1883e705c121SKalle Valo 		cmd->pass_all = 1;
1884e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1885e705c121SKalle Valo 	}
1886e705c121SKalle Valo 
1887e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1888e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1889e705c121SKalle Valo 				   cmd->count, addr->addr);
1890e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1891e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1892e705c121SKalle Valo 		cmd->count++;
1893e705c121SKalle Valo 	}
1894e705c121SKalle Valo 
1895e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1896e705c121SKalle Valo }
1897e705c121SKalle Valo 
1898cbce62a3SMiri Korenblit void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1899e705c121SKalle Valo 			      unsigned int changed_flags,
1900cbce62a3SMiri Korenblit 			      unsigned int *total_flags, u64 multicast)
1901e705c121SKalle Valo {
1902e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1903e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1904e705c121SKalle Valo 
1905e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1906e705c121SKalle Valo 
1907e705c121SKalle Valo 	/* replace previous configuration */
1908e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1909e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1910e705c121SKalle Valo 
1911e705c121SKalle Valo 	if (!cmd)
1912e705c121SKalle Valo 		goto out;
1913e705c121SKalle Valo 
191461e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
191561e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
191661e7d91bSLuca Coelho 
191761e7d91bSLuca Coelho 	if (cmd->pass_all)
191861e7d91bSLuca Coelho 		cmd->count = 0;
191961e7d91bSLuca Coelho 
1920e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1921e705c121SKalle Valo out:
1922e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1923e705c121SKalle Valo 	*total_flags = 0;
1924e705c121SKalle Valo }
1925e705c121SKalle Valo 
1926e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1927e705c121SKalle Valo 					struct ieee80211_vif *vif,
1928e705c121SKalle Valo 					unsigned int filter_flags,
1929e705c121SKalle Valo 					unsigned int changed_flags)
1930e705c121SKalle Valo {
1931e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1932e705c121SKalle Valo 
1933e705c121SKalle Valo 	/* We support only filter for probe requests */
1934e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1935e705c121SKalle Valo 		return;
1936e705c121SKalle Valo 
1937e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1938f276e20bSJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
1939e705c121SKalle Valo 	    !vif->p2p)
1940e705c121SKalle Valo 		return;
1941e705c121SKalle Valo 
1942e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1943e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1944e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1945e705c121SKalle Valo }
1946e705c121SKalle Valo 
194722c58834SGregory Greenman int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1948a07a8f37SSara Sharon {
1949a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1950a07a8f37SSara Sharon 
1951a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1952a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1953a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1954a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1955a07a8f37SSara Sharon 
1956a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1957a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1958a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1959a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1960a07a8f37SSara Sharon }
1961a07a8f37SSara Sharon 
1962f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1963f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1964f92659a1SSara Sharon {
1965d0a9123eSJohannes Berg 	if (vif->bss_conf.mu_mimo_owner) {
1966f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1967f92659a1SSara Sharon 
1968f92659a1SSara Sharon 		/*
1969f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1970f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1971f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
1972f92659a1SSara Sharon 		 */
1973afe0d181SJohannes Berg 		ieee80211_update_mu_groups(vif, 0,
1974f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
1975f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
1976f92659a1SSara Sharon 	}
1977f92659a1SSara Sharon }
1978f92659a1SSara Sharon 
1979f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1980f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
1981f92659a1SSara Sharon {
1982f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1983f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1984f92659a1SSara Sharon 
1985f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
1986f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1987f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
1988f92659a1SSara Sharon }
1989f92659a1SSara Sharon 
1990514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1991514c3069SLuca Coelho {
1992514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
1993514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
1994514c3069SLuca Coelho 	u8 residue_bits;
1995514c3069SLuca Coelho 	u8 res;
1996514c3069SLuca Coelho 
1997514c3069SLuca Coelho 	if (bit_num <= 5)
1998514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
1999514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2000514c3069SLuca Coelho 
2001514c3069SLuca Coelho 	/*
2002514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
2003514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
2004514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
2005514c3069SLuca Coelho 	 */
2006514c3069SLuca Coelho 
2007514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
2008514c3069SLuca Coelho 
2009514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
2010514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2011514c3069SLuca Coelho 	      residue_bits;
2012514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2013514c3069SLuca Coelho 
2014514c3069SLuca Coelho 	return res;
2015514c3069SLuca Coelho }
2016514c3069SLuca Coelho 
2017091296d3SMiri Korenblit static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
2018091296d3SMiri Korenblit 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
2019cb63eb43SMiri Korenblit 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
2020cb63eb43SMiri Korenblit 			      bool inheritance)
2021091296d3SMiri Korenblit {
2022091296d3SMiri Korenblit 	int i;
2023091296d3SMiri Korenblit 
2024091296d3SMiri Korenblit 	/*
2025091296d3SMiri Korenblit 	* FW currently supports only nss == MAX_HE_SUPP_NSS
2026091296d3SMiri Korenblit 	*
2027091296d3SMiri Korenblit 	* If nss > MAX: we can ignore values we don't support
2028091296d3SMiri Korenblit 	* If nss < MAX: we can set zeros in other streams
2029091296d3SMiri Korenblit 	*/
2030091296d3SMiri Korenblit 	if (nss > MAX_HE_SUPP_NSS) {
20314d8421f2SJason A. Donenfeld 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2032091296d3SMiri Korenblit 			       MAX_HE_SUPP_NSS);
2033091296d3SMiri Korenblit 		nss = MAX_HE_SUPP_NSS;
2034091296d3SMiri Korenblit 	}
2035091296d3SMiri Korenblit 
2036091296d3SMiri Korenblit 	for (i = 0; i < nss; i++) {
2037091296d3SMiri Korenblit 		u8 ru_index_tmp = ru_index_bitmap << 1;
2038091296d3SMiri Korenblit 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2039091296d3SMiri Korenblit 		u8 bw;
2040091296d3SMiri Korenblit 
2041091296d3SMiri Korenblit 		for (bw = 0;
2042091296d3SMiri Korenblit 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2043091296d3SMiri Korenblit 		     bw++) {
2044091296d3SMiri Korenblit 			ru_index_tmp >>= 1;
2045091296d3SMiri Korenblit 
2046cb63eb43SMiri Korenblit 			/*
2047cb63eb43SMiri Korenblit 			* According to the 11be spec, if for a specific BW the PPE Thresholds
2048cb63eb43SMiri Korenblit 			* isn't present - it should inherit the thresholds from the last
2049cb63eb43SMiri Korenblit 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2050cb63eb43SMiri Korenblit 			* this inheritance - continue in this case
2051cb63eb43SMiri Korenblit 			*/
2052cb63eb43SMiri Korenblit 			if (!(ru_index_tmp & 1)) {
2053cb63eb43SMiri Korenblit 				if (inheritance)
2054cb63eb43SMiri Korenblit 					goto set_thresholds;
2055cb63eb43SMiri Korenblit 				else
2056091296d3SMiri Korenblit 					continue;
2057cb63eb43SMiri Korenblit 			}
2058091296d3SMiri Korenblit 
2059091296d3SMiri Korenblit 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2060091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2061091296d3SMiri Korenblit 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2062091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2063091296d3SMiri Korenblit 
2064cb63eb43SMiri Korenblit set_thresholds:
2065091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2066091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2067091296d3SMiri Korenblit 		}
2068091296d3SMiri Korenblit 	}
2069091296d3SMiri Korenblit }
2070091296d3SMiri Korenblit 
2071091296d3SMiri Korenblit static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
207257974a55SGregory Greenman 					    struct ieee80211_link_sta *link_sta,
2073cb63eb43SMiri Korenblit 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2074cb63eb43SMiri Korenblit 					    bool inheritance)
2075091296d3SMiri Korenblit {
207657974a55SGregory Greenman 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
207757974a55SGregory Greenman 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
207857974a55SGregory Greenman 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2079091296d3SMiri Korenblit 	u8 ru_index_bitmap =
2080091296d3SMiri Korenblit 		u8_get_bits(*ppe,
2081091296d3SMiri Korenblit 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2082091296d3SMiri Korenblit 	/* Starting after PPE header */
2083091296d3SMiri Korenblit 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2084091296d3SMiri Korenblit 
2085cb63eb43SMiri Korenblit 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2086cb63eb43SMiri Korenblit 			  inheritance);
2087091296d3SMiri Korenblit }
2088091296d3SMiri Korenblit 
20894df6a075SMiri Korenblit static int
20904df6a075SMiri Korenblit iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
20914df6a075SMiri Korenblit 					 u8 nominal_padding)
2092091296d3SMiri Korenblit {
2093091296d3SMiri Korenblit 	int low_th = -1;
2094091296d3SMiri Korenblit 	int high_th = -1;
2095091296d3SMiri Korenblit 	int i;
2096091296d3SMiri Korenblit 
2097cb63eb43SMiri Korenblit 	/* all the macros are the same for EHT and HE */
2098091296d3SMiri Korenblit 	switch (nominal_padding) {
2099cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2100091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2101091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2102091296d3SMiri Korenblit 		break;
2103cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2104091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_BPSK;
2105091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
2106091296d3SMiri Korenblit 		break;
2107cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2108cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2109091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2110091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_BPSK;
2111091296d3SMiri Korenblit 		break;
2112091296d3SMiri Korenblit 	}
2113091296d3SMiri Korenblit 
21144df6a075SMiri Korenblit 	if (low_th < 0 || high_th < 0)
21154df6a075SMiri Korenblit 		return -EINVAL;
21164df6a075SMiri Korenblit 
2117091296d3SMiri Korenblit 	/* Set the PPE thresholds accordingly */
2118091296d3SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2119091296d3SMiri Korenblit 		u8 bw;
2120091296d3SMiri Korenblit 
2121091296d3SMiri Korenblit 		for (bw = 0;
2122091296d3SMiri Korenblit 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2123091296d3SMiri Korenblit 			bw++) {
2124091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2125091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2126091296d3SMiri Korenblit 		}
2127091296d3SMiri Korenblit 	}
2128091296d3SMiri Korenblit 
21294df6a075SMiri Korenblit 	return 0;
2130091296d3SMiri Korenblit }
2131091296d3SMiri Korenblit 
2132cb63eb43SMiri Korenblit static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2133cb63eb43SMiri Korenblit 					 u8 nominal_padding)
2134cb63eb43SMiri Korenblit {
2135cb63eb43SMiri Korenblit 	int i;
2136cb63eb43SMiri Korenblit 
2137cb63eb43SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2138cb63eb43SMiri Korenblit 		u8 bw;
2139cb63eb43SMiri Korenblit 
2140cb63eb43SMiri Korenblit 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2141cb63eb43SMiri Korenblit 		     bw++) {
2142cb63eb43SMiri Korenblit 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2143cb63eb43SMiri Korenblit 
2144cb63eb43SMiri Korenblit 			if (nominal_padding >
2145cb63eb43SMiri Korenblit 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2146cb63eb43SMiri Korenblit 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2147cb63eb43SMiri Korenblit 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2148cb63eb43SMiri Korenblit 			else if (nominal_padding ==
2149cb63eb43SMiri Korenblit 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2150cb63eb43SMiri Korenblit 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2151cb63eb43SMiri Korenblit 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2152cb63eb43SMiri Korenblit 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2153cb63eb43SMiri Korenblit 		}
2154cb63eb43SMiri Korenblit 	}
2155cb63eb43SMiri Korenblit }
2156cb63eb43SMiri Korenblit 
21574df6a075SMiri Korenblit /* Set the pkt_ext field according to PPE Thresholds element */
215857974a55SGregory Greenman int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
215957974a55SGregory Greenman 			    struct ieee80211_link_sta *link_sta,
21604df6a075SMiri Korenblit 			    struct iwl_he_pkt_ext_v2 *pkt_ext)
21614df6a075SMiri Korenblit {
21624df6a075SMiri Korenblit 	u8 nominal_padding;
21634df6a075SMiri Korenblit 	int i, ret = 0;
21644df6a075SMiri Korenblit 
216557974a55SGregory Greenman 	if (WARN_ON(!link_sta))
216657974a55SGregory Greenman 		return -EINVAL;
216757974a55SGregory Greenman 
21684df6a075SMiri Korenblit 	/* Initialize the PPE thresholds to "None" (7), as described in Table
21694df6a075SMiri Korenblit 	 * 9-262ac of 80211.ax/D3.0.
21704df6a075SMiri Korenblit 	 */
21714df6a075SMiri Korenblit 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
21724df6a075SMiri Korenblit 	       sizeof(struct iwl_he_pkt_ext_v2));
21734df6a075SMiri Korenblit 
217457974a55SGregory Greenman 	if (link_sta->eht_cap.has_eht) {
21754df6a075SMiri Korenblit 		nominal_padding =
217657974a55SGregory Greenman 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
21774df6a075SMiri Korenblit 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
21784df6a075SMiri Korenblit 
21794df6a075SMiri Korenblit 		/* If PPE Thresholds exists, parse them into a FW-familiar
21804df6a075SMiri Korenblit 		 * format.
21814df6a075SMiri Korenblit 		 */
218257974a55SGregory Greenman 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
21834df6a075SMiri Korenblit 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
218457974a55SGregory Greenman 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
21854df6a075SMiri Korenblit 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
218657974a55SGregory Greenman 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
21874df6a075SMiri Korenblit 			u8 ru_index_bitmap =
21884df6a075SMiri Korenblit 				u16_get_bits(*ppe,
21894df6a075SMiri Korenblit 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
21904df6a075SMiri Korenblit 			 /* Starting after PPE header */
21914df6a075SMiri Korenblit 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
21924df6a075SMiri Korenblit 
21934df6a075SMiri Korenblit 			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
21944df6a075SMiri Korenblit 					  ppe, ppe_pos_bit, true);
219557974a55SGregory Greenman 		/* EHT PPE Thresholds doesn't exist - set the API according to
219657974a55SGregory Greenman 		 * HE PPE Tresholds
21974df6a075SMiri Korenblit 		 */
219857974a55SGregory Greenman 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
21994df6a075SMiri Korenblit 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
22004df6a075SMiri Korenblit 			/* Even though HE Capabilities IE doesn't contain PPE
22014df6a075SMiri Korenblit 			 * Thresholds for BW 320Mhz, thresholds for this BW will
22024df6a075SMiri Korenblit 			 * be filled in with the same values as 160Mhz, due to
22034df6a075SMiri Korenblit 			 * the inheritance, as required.
22044df6a075SMiri Korenblit 			 */
220557974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
22064df6a075SMiri Korenblit 							true);
22074df6a075SMiri Korenblit 
22084df6a075SMiri Korenblit 			/* According to the requirements, for MCSs 12-13 the
22094df6a075SMiri Korenblit 			 * maximum value between HE PPE Threshold and Common
22104df6a075SMiri Korenblit 			 * Nominal Packet Padding needs to be taken
22114df6a075SMiri Korenblit 			 */
22124df6a075SMiri Korenblit 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
22134df6a075SMiri Korenblit 
22144df6a075SMiri Korenblit 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
22154df6a075SMiri Korenblit 		 * take the Thresholds from Common Nominal Packet Padding field
22164df6a075SMiri Korenblit 		 */
22174df6a075SMiri Korenblit 		} else {
22184df6a075SMiri Korenblit 			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
22194df6a075SMiri Korenblit 								       nominal_padding);
22204df6a075SMiri Korenblit 		}
222157974a55SGregory Greenman 	} else if (link_sta->he_cap.has_he) {
22224df6a075SMiri Korenblit 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
222357974a55SGregory Greenman 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
22244df6a075SMiri Korenblit 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
222557974a55SGregory Greenman 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
22264df6a075SMiri Korenblit 							false);
22274df6a075SMiri Korenblit 		/* PPE Thresholds doesn't exist - set the API PPE values
22284df6a075SMiri Korenblit 		 * according to Common Nominal Packet Padding field.
22294df6a075SMiri Korenblit 		 */
22304df6a075SMiri Korenblit 		} else {
22314df6a075SMiri Korenblit 			nominal_padding =
223257974a55SGregory Greenman 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
22334df6a075SMiri Korenblit 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
22344df6a075SMiri Korenblit 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
22354df6a075SMiri Korenblit 				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
22364df6a075SMiri Korenblit 									       nominal_padding);
22374df6a075SMiri Korenblit 		}
22384df6a075SMiri Korenblit 	}
22394df6a075SMiri Korenblit 
22404df6a075SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
22414df6a075SMiri Korenblit 		int bw;
22424df6a075SMiri Korenblit 
22434df6a075SMiri Korenblit 		for (bw = 0;
22444df6a075SMiri Korenblit 		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
22454df6a075SMiri Korenblit 		     bw++) {
22464df6a075SMiri Korenblit 			u8 *qam_th =
22474df6a075SMiri Korenblit 				&pkt_ext->pkt_ext_qam_th[i][bw][0];
22484df6a075SMiri Korenblit 
22494df6a075SMiri Korenblit 			IWL_DEBUG_HT(mvm,
22504df6a075SMiri Korenblit 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
22514df6a075SMiri Korenblit 				     i, bw, qam_th[0], qam_th[1]);
22524df6a075SMiri Korenblit 		}
22534df6a075SMiri Korenblit 	}
22544df6a075SMiri Korenblit 	return ret;
22554df6a075SMiri Korenblit }
22564df6a075SMiri Korenblit 
22579c4f15caSMiri Korenblit /*
22589c4f15caSMiri Korenblit  * This function sets the MU EDCA parameters ans returns whether MU EDCA
22599c4f15caSMiri Korenblit  * is enabled or not
22609c4f15caSMiri Korenblit  */
226155eb1c5fSMiri Korenblit bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2262e119e740SEmmanuel Grumbach 				   const struct iwl_mvm_vif_link_info *link_info,
226355eb1c5fSMiri Korenblit 				   struct iwl_he_backoff_conf *trig_based_txf)
22649c4f15caSMiri Korenblit {
22659c4f15caSMiri Korenblit 	int i;
22669c4f15caSMiri Korenblit 	/* Mark MU EDCA as enabled, unless none detected on some AC */
22679c4f15caSMiri Korenblit 	bool mu_edca_enabled = true;
22689c4f15caSMiri Korenblit 
22699c4f15caSMiri Korenblit 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2270e119e740SEmmanuel Grumbach 		const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2271e119e740SEmmanuel Grumbach 			&link_info->queue_params[i].mu_edca_param_rec;
22729c4f15caSMiri Korenblit 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
22739c4f15caSMiri Korenblit 
2274e119e740SEmmanuel Grumbach 		if (!link_info->queue_params[i].mu_edca) {
22759c4f15caSMiri Korenblit 			mu_edca_enabled = false;
22769c4f15caSMiri Korenblit 			break;
22779c4f15caSMiri Korenblit 		}
22789c4f15caSMiri Korenblit 
22799c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmin =
22809c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
22819c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmax =
22829c4f15caSMiri Korenblit 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
22839c4f15caSMiri Korenblit 		trig_based_txf[ac].aifsn =
22849c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->aifsn & 0xf);
22859c4f15caSMiri Korenblit 		trig_based_txf[ac].mu_time =
22869c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->mu_edca_timer);
22879c4f15caSMiri Korenblit 	}
22889c4f15caSMiri Korenblit 
22899c4f15caSMiri Korenblit 	return mu_edca_enabled;
22909c4f15caSMiri Korenblit }
22919c4f15caSMiri Korenblit 
22929be162a7SMiri Korenblit bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
22939c4f15caSMiri Korenblit {
22949c4f15caSMiri Korenblit 	const struct ieee80211_supported_band *sband;
22959c4f15caSMiri Korenblit 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
22969c4f15caSMiri Korenblit 
22979c4f15caSMiri Korenblit 	/* This capability is the same for all bands,
22989c4f15caSMiri Korenblit 	 * so take it from one of them.
22999c4f15caSMiri Korenblit 	 */
23009c4f15caSMiri Korenblit 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
23011ec7291eSJohannes Berg 	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
23029c4f15caSMiri Korenblit 
23039c4f15caSMiri Korenblit 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
23049c4f15caSMiri Korenblit 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
23059c4f15caSMiri Korenblit }
23069c4f15caSMiri Korenblit 
230757974a55SGregory Greenman __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
230857974a55SGregory Greenman 				 struct ieee80211_link_sta *link_sta)
23094df6a075SMiri Korenblit {
231057974a55SGregory Greenman 	u8 *mac_cap_info =
231157974a55SGregory Greenman 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
23124df6a075SMiri Korenblit 	__le32 htc_flags = 0;
23134df6a075SMiri Korenblit 
23144df6a075SMiri Korenblit 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
23154df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
23164df6a075SMiri Korenblit 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
23174df6a075SMiri Korenblit 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
23184df6a075SMiri Korenblit 		u8 link_adap =
23194df6a075SMiri Korenblit 			((mac_cap_info[2] &
23204df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
23214df6a075SMiri Korenblit 			 (mac_cap_info[1] &
23224df6a075SMiri Korenblit 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
23234df6a075SMiri Korenblit 
23244df6a075SMiri Korenblit 		if (link_adap == 2)
23254df6a075SMiri Korenblit 			htc_flags |=
23264df6a075SMiri Korenblit 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
23274df6a075SMiri Korenblit 		else if (link_adap == 3)
23284df6a075SMiri Korenblit 			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
23294df6a075SMiri Korenblit 	}
23304df6a075SMiri Korenblit 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
23314df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
23324df6a075SMiri Korenblit 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
23334df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
23344df6a075SMiri Korenblit 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
23354df6a075SMiri Korenblit 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
23364df6a075SMiri Korenblit 
23374df6a075SMiri Korenblit 	return htc_flags;
23384df6a075SMiri Korenblit }
23394df6a075SMiri Korenblit 
2340514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2341514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2342514c3069SLuca Coelho {
2343514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
234442506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2345514c3069SLuca Coelho 		.sta_id = sta_id,
2346514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2347dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2348514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2349514c3069SLuca Coelho 		.frame_time_rts_th =
2350514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2351514c3069SLuca Coelho 	};
235242506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
235342506dd2SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
235442506dd2SJohannes Berg 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
235542506dd2SJohannes Berg 	int size;
2356514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2357514c3069SLuca Coelho 	u32 flags;
2358514c3069SLuca Coelho 	int i;
235942506dd2SJohannes Berg 	void *cmd;
236042506dd2SJohannes Berg 
236142506dd2SJohannes Berg 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
236242506dd2SJohannes Berg 		ver = 1;
236342506dd2SJohannes Berg 
236442506dd2SJohannes Berg 	switch (ver) {
236542506dd2SJohannes Berg 	case 1:
236642506dd2SJohannes Berg 		/* same layout as v2 except some data at the end */
236742506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
236842506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
236942506dd2SJohannes Berg 		break;
237042506dd2SJohannes Berg 	case 2:
237142506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
237242506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
237342506dd2SJohannes Berg 		break;
237442506dd2SJohannes Berg 	case 3:
237542506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd;
237642506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
237742506dd2SJohannes Berg 		break;
237842506dd2SJohannes Berg 	default:
237942506dd2SJohannes Berg 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
238042506dd2SJohannes Berg 		return;
238142506dd2SJohannes Berg 	}
2382514c3069SLuca Coelho 
2383514c3069SLuca Coelho 	rcu_read_lock();
2384514c3069SLuca Coelho 
2385514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
238612d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2387514c3069SLuca Coelho 		rcu_read_unlock();
2388514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2389514c3069SLuca Coelho 		return;
2390514c3069SLuca Coelho 	}
2391514c3069SLuca Coelho 
2392046d2e7cSSriram R 	if (!sta->deflink.he_cap.has_he) {
2393514c3069SLuca Coelho 		rcu_read_unlock();
2394514c3069SLuca Coelho 		return;
2395514c3069SLuca Coelho 	}
2396514c3069SLuca Coelho 
2397514c3069SLuca Coelho 	flags = 0;
2398514c3069SLuca Coelho 
23994f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
2400650cadb7SGregory Greenman 	if (mvmvif->deflink.he_ru_2mhz_block)
24014f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
24024f58121dSIlan Peer 
2403514c3069SLuca Coelho 	/* HTC flags */
240457974a55SGregory Greenman 	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2405514c3069SLuca Coelho 
24064df6a075SMiri Korenblit 	/* PPE Thresholds */
240757974a55SGregory Greenman 	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2408cb63eb43SMiri Korenblit 		flags |= STA_CTXT_HE_PACKET_EXT;
240973f23d91SShaul Triebitz 
2410046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
241173f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
241273f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
241373f23d91SShaul Triebitz 
2414046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
241573f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
241673f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
241773f23d91SShaul Triebitz 
2418514c3069SLuca Coelho 	rcu_read_unlock();
2419514c3069SLuca Coelho 
2420e119e740SEmmanuel Grumbach 	if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
24219c4f15caSMiri Korenblit 					  &sta_ctxt_cmd.trig_based_txf[0]))
2422514c3069SLuca Coelho 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2423514c3069SLuca Coelho 
2424514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2425514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2426514c3069SLuca Coelho 
2427514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2428514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2429514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2430514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2431514c3069SLuca Coelho 	}
2432514c3069SLuca Coelho 
24339c4f15caSMiri Korenblit 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2434ee1a02d7SShaul Triebitz 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2435ee1a02d7SShaul Triebitz 
2436918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2437918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2438918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2439918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2440d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2441d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2442d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2443d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2444d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2445d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2446918cbf39SSara Sharon 	}
2447514c3069SLuca Coelho 
2448514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2449514c3069SLuca Coelho 
245042506dd2SJohannes Berg 	if (ver < 3) {
245142506dd2SJohannes Berg 		/* fields before pkt_ext */
245242506dd2SJohannes Berg 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
245342506dd2SJohannes Berg 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
245442506dd2SJohannes Berg 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
245542506dd2SJohannes Berg 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
245642506dd2SJohannes Berg 
245742506dd2SJohannes Berg 		/* pkt_ext */
245842506dd2SJohannes Berg 		for (i = 0;
245942506dd2SJohannes Berg 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
246042506dd2SJohannes Berg 		     i++) {
246142506dd2SJohannes Berg 			u8 bw;
246242506dd2SJohannes Berg 
246342506dd2SJohannes Berg 			for (bw = 0;
246442506dd2SJohannes Berg 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
246542506dd2SJohannes Berg 			     bw++) {
246642506dd2SJohannes Berg 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
246742506dd2SJohannes Berg 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
246842506dd2SJohannes Berg 
246942506dd2SJohannes Berg 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
247042506dd2SJohannes Berg 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
247142506dd2SJohannes Berg 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
247242506dd2SJohannes Berg 			}
247342506dd2SJohannes Berg 		}
247442506dd2SJohannes Berg 
247542506dd2SJohannes Berg 		/* fields after pkt_ext */
247642506dd2SJohannes Berg 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
247742506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
247842506dd2SJohannes Berg 			     sizeof(sta_ctxt_cmd_v2) -
247942506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
248042506dd2SJohannes Berg 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
248142506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
248242506dd2SJohannes Berg 		       (u8 *)&sta_ctxt_cmd +
248342506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
248442506dd2SJohannes Berg 		       sizeof(sta_ctxt_cmd) -
248542506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
248642506dd2SJohannes Berg 		sta_ctxt_cmd_v2.reserved3 = 0;
248742506dd2SJohannes Berg 	}
248842506dd2SJohannes Berg 
248942506dd2SJohannes Berg 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2490514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2491514c3069SLuca Coelho }
2492514c3069SLuca Coelho 
2493660eba5aSMiri Korenblit void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2494af84ac57SJohannes Berg 			   u32 duration_override)
2495af84ac57SJohannes Berg {
2496af84ac57SJohannes Berg 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2497af84ac57SJohannes Berg 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2498af84ac57SJohannes Berg 
2499af84ac57SJohannes Berg 	if (duration_override > duration)
2500af84ac57SJohannes Berg 		duration = duration_override;
2501af84ac57SJohannes Berg 
2502af84ac57SJohannes Berg 	/* Try really hard to protect the session and hear a beacon
2503af84ac57SJohannes Berg 	 * The new session protection command allows us to protect the
2504af84ac57SJohannes Berg 	 * session for a much longer time since the firmware will internally
2505af84ac57SJohannes Berg 	 * create two events: a 300TU one with a very high priority that
2506af84ac57SJohannes Berg 	 * won't be fragmented which should be enough for 99% of the cases,
2507af84ac57SJohannes Berg 	 * and another one (which we configure here to be 900TU long) which
2508af84ac57SJohannes Berg 	 * will have a slightly lower priority, but more importantly, can be
2509af84ac57SJohannes Berg 	 * fragmented so that it'll allow other activities to run.
2510af84ac57SJohannes Berg 	 */
2511af84ac57SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
2512af84ac57SJohannes Berg 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2513af84ac57SJohannes Berg 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2514af84ac57SJohannes Berg 						    min_duration, false);
2515af84ac57SJohannes Berg 	else
2516af84ac57SJohannes Berg 		iwl_mvm_protect_session(mvm, vif, duration,
2517af84ac57SJohannes Berg 					min_duration, 500, false);
2518af84ac57SJohannes Berg }
2519af84ac57SJohannes Berg 
2520660eba5aSMiri Korenblit /* Handle association common part to MLD and non-MLD modes */
2521660eba5aSMiri Korenblit void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2522660eba5aSMiri Korenblit 					    struct ieee80211_vif *vif,
2523660eba5aSMiri Korenblit 					    u64 changes)
2524660eba5aSMiri Korenblit {
2525660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2526660eba5aSMiri Korenblit 	int ret;
2527660eba5aSMiri Korenblit 
2528660eba5aSMiri Korenblit 	/* The firmware tracks the MU-MIMO group on its own.
2529660eba5aSMiri Korenblit 	 * However, on HW restart we should restore this data.
2530660eba5aSMiri Korenblit 	 */
2531660eba5aSMiri Korenblit 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2532660eba5aSMiri Korenblit 	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2533660eba5aSMiri Korenblit 		ret = iwl_mvm_update_mu_groups(mvm, vif);
2534660eba5aSMiri Korenblit 		if (ret)
2535660eba5aSMiri Korenblit 			IWL_ERR(mvm,
2536660eba5aSMiri Korenblit 				"failed to update VHT MU_MIMO groups\n");
2537660eba5aSMiri Korenblit 	}
2538660eba5aSMiri Korenblit 
2539660eba5aSMiri Korenblit 	iwl_mvm_recalc_multicast(mvm);
2540660eba5aSMiri Korenblit 
2541660eba5aSMiri Korenblit 	/* reset rssi values */
2542660eba5aSMiri Korenblit 	mvmvif->bf_data.ave_beacon_signal = 0;
2543660eba5aSMiri Korenblit 
2544660eba5aSMiri Korenblit 	iwl_mvm_bt_coex_vif_change(mvm);
25451a3e7039SGregory Greenman 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2546660eba5aSMiri Korenblit 					    IEEE80211_SMPS_AUTOMATIC);
2547660eba5aSMiri Korenblit 	if (fw_has_capa(&mvm->fw->ucode_capa,
2548660eba5aSMiri Korenblit 			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2549660eba5aSMiri Korenblit 		iwl_mvm_config_scan(mvm);
2550660eba5aSMiri Korenblit }
2551660eba5aSMiri Korenblit 
2552660eba5aSMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
25531a3e7039SGregory Greenman void
25541a3e7039SGregory Greenman iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2555660eba5aSMiri Korenblit 					struct ieee80211_vif *vif,
25561a3e7039SGregory Greenman 					struct ieee80211_bss_conf *link_conf,
2557660eba5aSMiri Korenblit 					u64 changes)
2558660eba5aSMiri Korenblit {
2559660eba5aSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2560660eba5aSMiri Korenblit 	int ret;
2561660eba5aSMiri Korenblit 
2562660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BEACON_INFO) {
2563660eba5aSMiri Korenblit 		/* We received a beacon from the associated AP so
2564660eba5aSMiri Korenblit 		 * remove the session protection.
2565660eba5aSMiri Korenblit 		 */
2566660eba5aSMiri Korenblit 		iwl_mvm_stop_session_protection(mvm, vif);
2567660eba5aSMiri Korenblit 
2568660eba5aSMiri Korenblit 		iwl_mvm_sf_update(mvm, vif, false);
2569660eba5aSMiri Korenblit 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2570660eba5aSMiri Korenblit 	}
2571660eba5aSMiri Korenblit 
2572660eba5aSMiri Korenblit 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2573660eba5aSMiri Korenblit 		       /* Send power command on every beacon change,
2574660eba5aSMiri Korenblit 			* because we may have not enabled beacon abort yet.
2575660eba5aSMiri Korenblit 			*/
2576660eba5aSMiri Korenblit 		       BSS_CHANGED_BEACON_INFO)) {
2577660eba5aSMiri Korenblit 		ret = iwl_mvm_power_update_mac(mvm);
2578660eba5aSMiri Korenblit 		if (ret)
2579660eba5aSMiri Korenblit 			IWL_ERR(mvm, "failed to update power mode\n");
2580660eba5aSMiri Korenblit 	}
2581660eba5aSMiri Korenblit 
2582660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_CQM) {
2583660eba5aSMiri Korenblit 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2584660eba5aSMiri Korenblit 		/* reset cqm events tracking */
2585660eba5aSMiri Korenblit 		mvmvif->bf_data.last_cqm_event = 0;
2586660eba5aSMiri Korenblit 		if (mvmvif->bf_data.bf_enabled) {
258722c58834SGregory Greenman 			/* FIXME: need to update per link when FW API will
258822c58834SGregory Greenman 			 * support it
258922c58834SGregory Greenman 			 */
2590660eba5aSMiri Korenblit 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2591660eba5aSMiri Korenblit 			if (ret)
2592660eba5aSMiri Korenblit 				IWL_ERR(mvm,
2593660eba5aSMiri Korenblit 					"failed to update CQM thresholds\n");
2594660eba5aSMiri Korenblit 		}
2595660eba5aSMiri Korenblit 	}
2596660eba5aSMiri Korenblit 
2597660eba5aSMiri Korenblit 	if (changes & BSS_CHANGED_BANDWIDTH)
25981a3e7039SGregory Greenman 		iwl_mvm_update_link_smps(vif, link_conf);
2599660eba5aSMiri Korenblit }
2600660eba5aSMiri Korenblit 
2601e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2602e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2603e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
26047b7090b4SJohannes Berg 					     u64 changes)
2605e705c121SKalle Valo {
2606e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2607e705c121SKalle Valo 	int ret;
2608e705c121SKalle Valo 
2609e705c121SKalle Valo 	/*
2610cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend
2611cdaba917SEmmanuel Grumbach 	 * on the beacon interval, which was not known when the station
2612cdaba917SEmmanuel Grumbach 	 * interface was added.
2613e705c121SKalle Valo 	 */
2614f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2615cb63eb43SMiri Korenblit 		if ((vif->bss_conf.he_support &&
2616cb63eb43SMiri Korenblit 		     !iwlwifi_mod_params.disable_11ax) ||
2617cb63eb43SMiri Korenblit 		    (vif->bss_conf.eht_support &&
2618cb63eb43SMiri Korenblit 		     !iwlwifi_mod_params.disable_11be))
2619650cadb7SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2620514c3069SLuca Coelho 
2621e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2622514c3069SLuca Coelho 	}
2623e705c121SKalle Valo 
262440ecdd01SShaul Triebitz 	/* Update MU EDCA params */
262540ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2626cb63eb43SMiri Korenblit 	    vif->cfg.assoc &&
2627cb63eb43SMiri Korenblit 	    ((vif->bss_conf.he_support &&
2628cb63eb43SMiri Korenblit 	      !iwlwifi_mod_params.disable_11ax) ||
2629cb63eb43SMiri Korenblit 	     (vif->bss_conf.eht_support &&
2630cb63eb43SMiri Korenblit 	      !iwlwifi_mod_params.disable_11be)))
2631650cadb7SGregory Greenman 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
263240ecdd01SShaul Triebitz 
2633e705c121SKalle Valo 	/*
2634e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2635e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2636e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2637e705c121SKalle Valo 	 * branch for disassociation below.
2638e705c121SKalle Valo 	 */
2639e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2640650cadb7SGregory Greenman 		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2641e705c121SKalle Valo 
2642650cadb7SGregory Greenman 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2643e705c121SKalle Valo 	if (ret)
2644e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2645e705c121SKalle Valo 
2646e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2647650cadb7SGregory Greenman 	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2648f276e20bSJohannes Berg 	mvmvif->associated = vif->cfg.assoc;
2649e705c121SKalle Valo 
2650e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2651f276e20bSJohannes Berg 		if (vif->cfg.assoc) {
2652e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2653e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2654650cadb7SGregory Greenman 			memset(&mvmvif->deflink.beacon_stats, 0,
2655650cadb7SGregory Greenman 			       sizeof(mvmvif->deflink.beacon_stats));
2656e705c121SKalle Valo 
2657e705c121SKalle Valo 			/* add quota for this interface */
2658e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2659e705c121SKalle Valo 			if (ret) {
2660e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2661e705c121SKalle Valo 				return;
2662e705c121SKalle Valo 			}
2663e705c121SKalle Valo 
2664e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2665fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2666fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2667fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2668e705c121SKalle Valo 				/*
2669e705c121SKalle Valo 				 * If we're restarting then the firmware will
2670e705c121SKalle Valo 				 * obviously have lost synchronisation with
2671e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2672e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2673e705c121SKalle Valo 				 * scheduling a session protection time event.
2674e705c121SKalle Valo 				 *
2675e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2676e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2677e705c121SKalle Valo 				 * the beacon interval.
2678e705c121SKalle Valo 				 *
2679e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2680e705c121SKalle Valo 				 * than a single interface.
2681fe959c7bSEmmanuel Grumbach 				 *
2682fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2683fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2684fe959c7bSEmmanuel Grumbach 				 * only anyway.
2685e705c121SKalle Valo 				 */
2686e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2687e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2688e705c121SKalle Valo 							5 * dur, false);
2689af84ac57SJohannes Berg 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2690af84ac57SJohannes Berg 					     &mvm->status) &&
2691af84ac57SJohannes Berg 				   !vif->bss_conf.dtim_period) {
2692af84ac57SJohannes Berg 				/*
2693af84ac57SJohannes Berg 				 * If we're not restarting and still haven't
2694af84ac57SJohannes Berg 				 * heard a beacon (dtim period unknown) then
2695af84ac57SJohannes Berg 				 * make sure we still have enough minimum time
2696af84ac57SJohannes Berg 				 * remaining in the time event, since the auth
2697af84ac57SJohannes Berg 				 * might actually have taken quite a while
2698af84ac57SJohannes Berg 				 * (especially for SAE) and so the remaining
2699af84ac57SJohannes Berg 				 * time could be small without us having heard
2700af84ac57SJohannes Berg 				 * a beacon yet.
2701af84ac57SJohannes Berg 				 */
2702af84ac57SJohannes Berg 				iwl_mvm_protect_assoc(mvm, vif, 0);
2703e705c121SKalle Valo 			}
2704e705c121SKalle Valo 
2705e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2706e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2707e705c121SKalle Valo 			if (vif->p2p) {
2708e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2709e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
27101a3e7039SGregory Greenman 						    IEEE80211_SMPS_DYNAMIC, 0);
2711e705c121SKalle Valo 			}
2712650cadb7SGregory Greenman 		} else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
27136d19a5ebSEmmanuel Grumbach 			iwl_mvm_mei_host_disassociated(mvm);
2714e705c121SKalle Valo 			/*
2715e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2716e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2717e705c121SKalle Valo 			 */
271869e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
271969e508b4SIlan Peer 			WARN_ONCE(ret &&
272069e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
272169e508b4SIlan Peer 					    &mvm->status),
2722e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2723e705c121SKalle Valo 
27246b28f978SEmmanuel Grumbach 			/*
27256b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
27266b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
27276b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
27286b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
27296b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
27306b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
27316b28f978SEmmanuel Grumbach 			 */
27326b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
27336b28f978SEmmanuel Grumbach 				      &mvm->status)) {
27345c75a208SJohannes Berg 				/* first remove remaining keys */
2735ba9eef6bSGregory Greenman 				iwl_mvm_sec_key_remove_ap(mvm, vif,
2736072573f6SJohannes Berg 							  &mvmvif->deflink, 0);
27375c75a208SJohannes Berg 
27386b28f978SEmmanuel Grumbach 				/*
27396b28f978SEmmanuel Grumbach 				 * Remove AP station now that
27406b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
27416b28f978SEmmanuel Grumbach 				 */
27426b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
2743650cadb7SGregory Greenman 							mvmvif->deflink.ap_sta_id);
2744e705c121SKalle Valo 				if (ret)
27456b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
27466b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2747e705c121SKalle Valo 
2748650cadb7SGregory Greenman 				mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
27496b28f978SEmmanuel Grumbach 			}
27506b28f978SEmmanuel Grumbach 
2751e705c121SKalle Valo 			/* remove quota for this interface */
2752e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2753e705c121SKalle Valo 			if (ret)
2754e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2755e705c121SKalle Valo 
2756e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2757e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2758e705c121SKalle Valo 			if (ret)
2759e705c121SKalle Valo 				IWL_ERR(mvm,
2760e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2761e705c121SKalle Valo 					vif->addr);
2762e705c121SKalle Valo 		}
2763e705c121SKalle Valo 
2764660eba5aSMiri Korenblit 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
2765a07a8f37SSara Sharon 	}
2766a07a8f37SSara Sharon 
27671a3e7039SGregory Greenman 	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
27681a3e7039SGregory Greenman 						changes);
2769b45242c9SAvraham Stern }
2770b45242c9SAvraham Stern 
2771f947b62cSMiri Korenblit bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2772f947b62cSMiri Korenblit 				  struct ieee80211_vif *vif,
2773f947b62cSMiri Korenblit 				  int *ret)
2774f947b62cSMiri Korenblit {
2775f947b62cSMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2776f947b62cSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2777f947b62cSMiri Korenblit 	int i;
2778e705c121SKalle Valo 
2779f947b62cSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
2780f947b62cSMiri Korenblit 
2781f947b62cSMiri Korenblit 	mvmvif->ap_assoc_sta_count = 0;
2782f947b62cSMiri Korenblit 
2783f947b62cSMiri Korenblit 	/* must be set before quota calculations */
2784f947b62cSMiri Korenblit 	mvmvif->ap_ibss_active = true;
2785f947b62cSMiri Korenblit 
2786f947b62cSMiri Korenblit 	/* send all the early keys to the device now */
2787f947b62cSMiri Korenblit 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2788f947b62cSMiri Korenblit 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2789f947b62cSMiri Korenblit 
2790f947b62cSMiri Korenblit 		if (!key)
2791f947b62cSMiri Korenblit 			continue;
2792f947b62cSMiri Korenblit 
2793f947b62cSMiri Korenblit 		mvmvif->ap_early_keys[i] = NULL;
2794f947b62cSMiri Korenblit 
2795f947b62cSMiri Korenblit 		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2796f947b62cSMiri Korenblit 		if (*ret)
2797f947b62cSMiri Korenblit 			return true;
2798e705c121SKalle Valo 	}
2799e705c121SKalle Valo 
2800f947b62cSMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2801f947b62cSMiri Korenblit 		iwl_mvm_vif_set_low_latency(mvmvif, true,
2802f947b62cSMiri Korenblit 					    LOW_LATENCY_VIF_TYPE);
2803f947b62cSMiri Korenblit 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2804e705c121SKalle Valo 	}
2805e705c121SKalle Valo 
2806f947b62cSMiri Korenblit 	/* power updated needs to be done before quotas */
2807f947b62cSMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
2808e705c121SKalle Valo 
2809f947b62cSMiri Korenblit 	return false;
2810e705c121SKalle Valo }
2811e705c121SKalle Valo 
2812e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2813ae7ba17bSShaul Triebitz 				 struct ieee80211_vif *vif,
2814b327c84cSGregory Greenman 				 struct ieee80211_bss_conf *link_conf)
2815e705c121SKalle Valo {
2816e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2817e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2818f947b62cSMiri Korenblit 	int ret;
2819e705c121SKalle Valo 
2820e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2821e705c121SKalle Valo 
2822e705c121SKalle Valo 	/*
2823cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2824cdaba917SEmmanuel Grumbach 	 * the beacon interval, which was not known when the AP interface
2825cdaba917SEmmanuel Grumbach 	 * was added.
2826e705c121SKalle Valo 	 */
2827e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2828e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2829e705c121SKalle Valo 
283036cf5377SGregory Greenman 	/* For older devices need to send beacon template before adding mac
283136cf5377SGregory Greenman 	 * context. For the newer, the beacon is a resource that belongs to a
283236cf5377SGregory Greenman 	 * MAC, so need to send beacon template after adding the mac.
283336cf5377SGregory Greenman 	 */
283436cf5377SGregory Greenman 	if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
2835e705c121SKalle Valo 		/* Add the mac context */
2836e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2837e705c121SKalle Valo 		if (ret)
2838e705c121SKalle Valo 			goto out_unlock;
2839e705c121SKalle Valo 
284036cf5377SGregory Greenman 		/* Send the beacon template */
284136cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
284236cf5377SGregory Greenman 		if (ret)
284336cf5377SGregory Greenman 			goto out_unlock;
284436cf5377SGregory Greenman 	} else {
284536cf5377SGregory Greenman 		/* Send the beacon template */
284636cf5377SGregory Greenman 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
284736cf5377SGregory Greenman 		if (ret)
284836cf5377SGregory Greenman 			goto out_unlock;
2849e705c121SKalle Valo 
2850e705c121SKalle Valo 		/* Add the mac context */
2851e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2852e705c121SKalle Valo 		if (ret)
2853e705c121SKalle Valo 			goto out_unlock;
285436cf5377SGregory Greenman 	}
2855e705c121SKalle Valo 
2856e705c121SKalle Valo 	/* Perform the binding */
2857e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2858e705c121SKalle Valo 	if (ret)
2859e705c121SKalle Valo 		goto out_remove;
2860e705c121SKalle Valo 
286163dd5d02SSara Sharon 	/*
286263dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
286363dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
286463dd5d02SSara Sharon 	 * cause assert 0x2b00.
286563dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
286663dd5d02SSara Sharon 	 * the TLV
286763dd5d02SSara Sharon 	 */
286863dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2869ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2870ced19f26SSara Sharon 		if (ret)
2871ced19f26SSara Sharon 			goto out_unbind;
287263dd5d02SSara Sharon 		/*
287363dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
287463dd5d02SSara Sharon 		 * events are added and applied to the scheduler
287563dd5d02SSara Sharon 		 */
2876e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
287763dd5d02SSara Sharon 		if (ret) {
287863dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
287963dd5d02SSara Sharon 			goto out_unbind;
288063dd5d02SSara Sharon 		}
288163dd5d02SSara Sharon 	} else {
288263dd5d02SSara Sharon 		/*
288363dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
288463dd5d02SSara Sharon 		 * events are added and applied to the scheduler
288563dd5d02SSara Sharon 		 */
288675fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2887e705c121SKalle Valo 		if (ret)
288863dd5d02SSara Sharon 			goto out_unbind;
288975fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
289063dd5d02SSara Sharon 		if (ret) {
289163dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
289263dd5d02SSara Sharon 			goto out_unbind;
289363dd5d02SSara Sharon 		}
289463dd5d02SSara Sharon 	}
289526d6c16bSSara Sharon 
2896f947b62cSMiri Korenblit 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
2897f947b62cSMiri Korenblit 		goto out_failed;
2898e705c121SKalle Valo 
2899e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2900e705c121SKalle Valo 	if (ret)
2901f947b62cSMiri Korenblit 		goto out_failed;
2902e705c121SKalle Valo 
2903e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2904e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2905e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2906e705c121SKalle Valo 
2907e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2908e705c121SKalle Valo 
2909e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2910e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2911e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2912e705c121SKalle Valo 
2913fd940de7SAvraham Stern 	iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
2914b73f9a4aSJohannes Berg 
2915e705c121SKalle Valo 	goto out_unlock;
2916e705c121SKalle Valo 
2917f947b62cSMiri Korenblit out_failed:
2918e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2919e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2920e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2921ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2922e705c121SKalle Valo out_unbind:
2923e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2924e705c121SKalle Valo out_remove:
2925e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2926e705c121SKalle Valo out_unlock:
2927e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2928e705c121SKalle Valo 	return ret;
2929e705c121SKalle Valo }
2930e705c121SKalle Valo 
2931ae7ba17bSShaul Triebitz static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
2932ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
2933b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
2934ae7ba17bSShaul Triebitz {
2935b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
2936ae7ba17bSShaul Triebitz }
2937ae7ba17bSShaul Triebitz 
2938ae7ba17bSShaul Triebitz static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
2939e705c121SKalle Valo 			      struct ieee80211_vif *vif)
2940e705c121SKalle Valo {
2941b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
2942ae7ba17bSShaul Triebitz }
2943ae7ba17bSShaul Triebitz 
2944fd1a54c1SMiri Korenblit /* Common part for MLD and non-MLD ops */
2945fd1a54c1SMiri Korenblit void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
2946fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif)
2947ae7ba17bSShaul Triebitz {
2948e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2949e705c121SKalle Valo 
2950fd1a54c1SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
2951e705c121SKalle Valo 
2952fd1a54c1SMiri Korenblit 	iwl_mvm_prepare_mac_removal(mvm, vif);
2953e705c121SKalle Valo 
2954e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2955e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2956e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2957e705c121SKalle Valo 					  &mvmvif->time_event_data);
2958e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2959e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2960e705c121SKalle Valo 	}
2961e705c121SKalle Valo 
2962e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2963e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2964e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2965e705c121SKalle Valo 	}
2966e705c121SKalle Valo 
2967e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2968e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2969e705c121SKalle Valo 
297047242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
297147242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
297247242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
297347242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
297447242744STova Mussai 	}
297547242744STova Mussai 
2976e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2977fd1a54c1SMiri Korenblit }
2978fd1a54c1SMiri Korenblit 
2979fd1a54c1SMiri Korenblit static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2980fd1a54c1SMiri Korenblit 				 struct ieee80211_vif *vif,
2981fd1a54c1SMiri Korenblit 				 struct ieee80211_bss_conf *link_conf)
2982fd1a54c1SMiri Korenblit {
2983fd1a54c1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2984fd1a54c1SMiri Korenblit 
2985fd1a54c1SMiri Korenblit 	mutex_lock(&mvm->mutex);
2986fd1a54c1SMiri Korenblit 
2987fd1a54c1SMiri Korenblit 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
2988e705c121SKalle Valo 
2989e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2990e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2991e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2992e705c121SKalle Valo 
2993e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2994ced19f26SSara Sharon 
2995be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
2996be82ecd3SAvraham Stern 
2997ced19f26SSara Sharon 	/*
2998ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2999ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
3000ced19f26SSara Sharon 	 * cause assert 0x2b00.
3001ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
3002ced19f26SSara Sharon 	 * bcast station).
3003ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
3004ced19f26SSara Sharon 	 */
3005ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
300626d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
3007e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3008ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3009ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
3010e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
3011e705c121SKalle Valo 
3012e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
3013e705c121SKalle Valo 
3014e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3015e705c121SKalle Valo 
3016e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3017e705c121SKalle Valo }
3018e705c121SKalle Valo 
3019ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
3020ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
3021b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
3022ae7ba17bSShaul Triebitz {
3023b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
3024ae7ba17bSShaul Triebitz }
3025ae7ba17bSShaul Triebitz 
3026ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
3027ae7ba17bSShaul Triebitz 			      struct ieee80211_vif *vif)
3028ae7ba17bSShaul Triebitz {
3029b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3030ae7ba17bSShaul Triebitz }
3031ae7ba17bSShaul Triebitz 
3032e705c121SKalle Valo static void
3033e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3034e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3035e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
30367b7090b4SJohannes Berg 				 u64 changes)
3037e705c121SKalle Valo {
3038e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3039e705c121SKalle Valo 
3040e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
3041e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
3042e705c121SKalle Valo 		return;
3043e705c121SKalle Valo 
3044e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3045e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3046e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3047e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3048e705c121SKalle Valo 
3049e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
3050e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
305136cf5377SGregory Greenman 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3052e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
3053e705c121SKalle Valo 
3054b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3055fd940de7SAvraham Stern 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3056b73f9a4aSJohannes Berg 
3057b73f9a4aSJohannes Berg 		if (ret)
3058b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3059b73f9a4aSJohannes Berg 				 ret);
3060b73f9a4aSJohannes Berg 	}
3061b73f9a4aSJohannes Berg 
3062e705c121SKalle Valo }
3063e705c121SKalle Valo 
3064e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3065e705c121SKalle Valo 				     struct ieee80211_vif *vif,
3066e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
30677b7090b4SJohannes Berg 				     u64 changes)
3068e705c121SKalle Valo {
3069a2906ea6SJohannes Berg 	static const struct iwl_mvm_bss_info_changed_ops callbacks = {
3070660eba5aSMiri Korenblit 		.bss_info_changed_sta = iwl_mvm_bss_info_changed_station,
3071660eba5aSMiri Korenblit 		.bss_info_changed_ap_ibss = iwl_mvm_bss_info_changed_ap_ibss,
3072660eba5aSMiri Korenblit 	};
3073660eba5aSMiri Korenblit 
3074660eba5aSMiri Korenblit 	iwl_mvm_bss_info_changed_common(hw, vif, bss_conf, &callbacks,
3075660eba5aSMiri Korenblit 					changes);
3076660eba5aSMiri Korenblit }
3077660eba5aSMiri Korenblit 
3078660eba5aSMiri Korenblit void
3079660eba5aSMiri Korenblit iwl_mvm_bss_info_changed_common(struct ieee80211_hw *hw,
3080660eba5aSMiri Korenblit 				struct ieee80211_vif *vif,
3081660eba5aSMiri Korenblit 				struct ieee80211_bss_conf *bss_conf,
3082a2906ea6SJohannes Berg 				const struct iwl_mvm_bss_info_changed_ops *callbacks,
3083660eba5aSMiri Korenblit 				u64 changes)
3084660eba5aSMiri Korenblit {
3085e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3086e705c121SKalle Valo 
3087e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3088e705c121SKalle Valo 
3089f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3090e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3091e705c121SKalle Valo 
3092e705c121SKalle Valo 	switch (vif->type) {
3093e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
3094660eba5aSMiri Korenblit 		callbacks->bss_info_changed_sta(mvm, vif, bss_conf, changes);
3095e705c121SKalle Valo 		break;
3096e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
3097e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
3098660eba5aSMiri Korenblit 		callbacks->bss_info_changed_ap_ibss(mvm, vif, bss_conf,
3099660eba5aSMiri Korenblit 						    changes);
3100e705c121SKalle Valo 		break;
310191b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
310291b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
310391b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
310491b08c2dSAviya Erenfeld 		break;
3105e705c121SKalle Valo 	default:
3106e705c121SKalle Valo 		/* shouldn't happen */
3107e705c121SKalle Valo 		WARN_ON_ONCE(1);
3108e705c121SKalle Valo 	}
3109e705c121SKalle Valo 
31109aae43a4SJohannes Berg 	if (changes & BSS_CHANGED_TXPOWER) {
31119aae43a4SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
31129aae43a4SJohannes Berg 				bss_conf->txpower);
31139aae43a4SJohannes Berg 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
31149aae43a4SJohannes Berg 	}
31159aae43a4SJohannes Berg 
3116e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3117e705c121SKalle Valo }
3118e705c121SKalle Valo 
3119cbce62a3SMiri Korenblit int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3120e705c121SKalle Valo 			struct ieee80211_scan_request *hw_req)
3121e705c121SKalle Valo {
3122e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3123e705c121SKalle Valo 	int ret;
3124e705c121SKalle Valo 
3125e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
3126e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3127e705c121SKalle Valo 		return -EINVAL;
3128e705c121SKalle Valo 
3129e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3130e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3131e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3132e705c121SKalle Valo 
3133e705c121SKalle Valo 	return ret;
3134e705c121SKalle Valo }
3135e705c121SKalle Valo 
3136cbce62a3SMiri Korenblit void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3137e705c121SKalle Valo 				struct ieee80211_vif *vif)
3138e705c121SKalle Valo {
3139e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3140e705c121SKalle Valo 
3141e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3142e705c121SKalle Valo 
3143e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3144e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
3145e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
3146e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
3147e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
3148e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
3149e705c121SKalle Valo 	*/
3150e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3151e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3152e705c121SKalle Valo 
3153e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3154e705c121SKalle Valo }
3155e705c121SKalle Valo 
3156cbce62a3SMiri Korenblit void
3157e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3158e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
3159e705c121SKalle Valo 				  int num_frames,
3160e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
3161e705c121SKalle Valo 				  bool more_data)
3162e705c121SKalle Valo {
3163e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3164e705c121SKalle Valo 
3165e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3166e705c121SKalle Valo 
3167e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3168e705c121SKalle Valo 					  tids, more_data, false);
3169e705c121SKalle Valo }
3170e705c121SKalle Valo 
3171cbce62a3SMiri Korenblit void
3172e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3173e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
3174e705c121SKalle Valo 				    int num_frames,
3175e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
3176e705c121SKalle Valo 				    bool more_data)
3177e705c121SKalle Valo {
3178e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3179e705c121SKalle Valo 
31809a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3181e705c121SKalle Valo 
3182e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3183e705c121SKalle Valo 					  tids, more_data, true);
3184e705c121SKalle Valo }
3185e705c121SKalle Valo 
318665e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3187e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
3188e705c121SKalle Valo 				     struct ieee80211_sta *sta)
3189e705c121SKalle Valo {
3190e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3191e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3192e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
3193e705c121SKalle Valo 	int tid;
3194e705c121SKalle Valo 
3195960f864bSJohannes Berg 	/*
3196960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
3197960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
3198960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
3199960f864bSJohannes Berg 	 */
3200960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3201960f864bSJohannes Berg 		return;
3202960f864bSJohannes Berg 
3203e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3204311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3205e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3206e705c121SKalle Valo 
32076862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
32081c17627bSSara Sharon 			continue;
32091c17627bSSara Sharon 
3210e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
3211e705c121SKalle Valo 
3212dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3213e705c121SKalle Valo 			continue;
3214e705c121SKalle Valo 
3215e705c121SKalle Valo 		__set_bit(tid, &tids);
3216e705c121SKalle Valo 	}
3217e705c121SKalle Valo 
3218e705c121SKalle Valo 	switch (cmd) {
3219e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
3220e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3221e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
3222e705c121SKalle Valo 
3223e705c121SKalle Valo 		if (txqs)
3224e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3225e705c121SKalle Valo 		/*
3226e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3227e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
3228e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3229e705c121SKalle Valo 		 */
3230e705c121SKalle Valo 		break;
3231e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
3232c8ee33e1SGregory Greenman 		if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
3233e705c121SKalle Valo 			break;
3234e705c121SKalle Valo 
3235e705c121SKalle Valo 		if (txqs)
3236e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3237e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3238e705c121SKalle Valo 		break;
3239e705c121SKalle Valo 	default:
3240e705c121SKalle Valo 		break;
3241e705c121SKalle Valo 	}
3242e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3243e705c121SKalle Valo }
3244e705c121SKalle Valo 
3245cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3246cbce62a3SMiri Korenblit 			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
324765e25482SJohannes Berg {
324865e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
324965e25482SJohannes Berg }
325065e25482SJohannes Berg 
325165e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
325265e25482SJohannes Berg {
325365e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
325465e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
325565e25482SJohannes Berg 	struct ieee80211_sta *sta;
325665e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
325765e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
325865e25482SJohannes Berg 
3259be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
326065e25482SJohannes Berg 		return;
326165e25482SJohannes Berg 
326265e25482SJohannes Berg 	rcu_read_lock();
3263a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
326465e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
326565e25482SJohannes Berg 		rcu_read_unlock();
326665e25482SJohannes Berg 		return;
326765e25482SJohannes Berg 	}
326865e25482SJohannes Berg 
326965e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
327065e25482SJohannes Berg 
327165e25482SJohannes Berg 	if (!mvmsta->vif ||
327265e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
327365e25482SJohannes Berg 		rcu_read_unlock();
327465e25482SJohannes Berg 		return;
327565e25482SJohannes Berg 	}
327665e25482SJohannes Berg 
327765e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
327865e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
327965e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
328065e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
328165e25482SJohannes Berg 			sta);
328265e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
328365e25482SJohannes Berg 	}
328465e25482SJohannes Berg 
328565e25482SJohannes Berg 	if (sleeping) {
328665e25482SJohannes Berg 		switch (notif->type) {
328765e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
328865e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
328965e25482SJohannes Berg 			break;
329065e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
329165e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
329265e25482SJohannes Berg 			break;
329365e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
329465e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
329565e25482SJohannes Berg 			break;
329665e25482SJohannes Berg 		default:
329765e25482SJohannes Berg 			break;
329865e25482SJohannes Berg 		}
329965e25482SJohannes Berg 	}
330065e25482SJohannes Berg 
330165e25482SJohannes Berg 	rcu_read_unlock();
330265e25482SJohannes Berg }
330365e25482SJohannes Berg 
3304cbce62a3SMiri Korenblit void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3305e705c121SKalle Valo 				struct ieee80211_vif *vif,
3306e705c121SKalle Valo 				struct ieee80211_sta *sta)
3307e705c121SKalle Valo {
3308e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3309e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
331079faae3aSGregory Greenman 	unsigned int link_id;
3311e705c121SKalle Valo 
3312e705c121SKalle Valo 	/*
3313e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
3314e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
3315e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
3316e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
3317e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
3318e705c121SKalle Valo 	 * callback deleted the station.
331979faae3aSGregory Greenman 	 * Since there's mvm->mutex here, no need to have RCU lock for
332079faae3aSGregory Greenman 	 * mvm_sta->link access.
3321e705c121SKalle Valo 	 */
3322e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
332379faae3aSGregory Greenman 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
332479faae3aSGregory Greenman 		struct iwl_mvm_link_sta *link_sta;
332579faae3aSGregory Greenman 		u32 sta_id;
3326e705c121SKalle Valo 
332779faae3aSGregory Greenman 		if (!mvm_sta->link[link_id])
332879faae3aSGregory Greenman 			continue;
332979faae3aSGregory Greenman 
333079faae3aSGregory Greenman 		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
333179faae3aSGregory Greenman 						     lockdep_is_held(&mvm->mutex));
333279faae3aSGregory Greenman 		sta_id = link_sta->sta_id;
3333b8a85a1dSJohannes Berg 		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
33343e75668bSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
333579faae3aSGregory Greenman 					 ERR_PTR(-ENOENT));
3336b8a85a1dSJohannes Berg 			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3337b8a85a1dSJohannes Berg 		}
333879faae3aSGregory Greenman 	}
3339e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3340e705c121SKalle Valo }
3341e705c121SKalle Valo 
3342e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3343e705c121SKalle Valo 				const u8 *bssid)
3344e705c121SKalle Valo {
3345b0ffe455SJohannes Berg 	int i;
3346b0ffe455SJohannes Berg 
3347b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3348b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
3349b0ffe455SJohannes Berg 
3350b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3351b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3352b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
3353b0ffe455SJohannes Berg 	}
3354b0ffe455SJohannes Berg 
3355e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3356e705c121SKalle Valo 		return;
3357e705c121SKalle Valo 
3358c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3359cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3360cee5a882SAvri Altman 		return;
3361cee5a882SAvri Altman 	}
3362cee5a882SAvri Altman 
336311dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
336411dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3365e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3366e705c121SKalle Valo 		return;
3367e705c121SKalle Valo 	}
3368e705c121SKalle Valo 
3369b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3370b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3371b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3372b0ffe455SJohannes Berg 			return;
3373b0ffe455SJohannes Berg 		}
3374b0ffe455SJohannes Berg 	}
3375b0ffe455SJohannes Berg 
3376e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3377e705c121SKalle Valo }
3378e705c121SKalle Valo 
33791e8f1329SGolan Ben-Ami static void
33801e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
33811e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
33821e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
33831e8f1329SGolan Ben-Ami {
33841e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
33851e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
33861e8f1329SGolan Ben-Ami 
33876c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
33886c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
33896c042d75SSara Sharon 	if (!trig)
33901e8f1329SGolan Ben-Ami 		return;
33911e8f1329SGolan Ben-Ami 
33921e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
33931e8f1329SGolan Ben-Ami 
33941e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
33951e8f1329SGolan Ben-Ami 		return;
33961e8f1329SGolan Ben-Ami 
33971e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
33981e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
33991e8f1329SGolan Ben-Ami 		return;
34001e8f1329SGolan Ben-Ami 
34017174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
34021e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
34031e8f1329SGolan Ben-Ami 				peer_addr, action);
34041e8f1329SGolan Ben-Ami }
34051e8f1329SGolan Ben-Ami 
34064f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
34074f58121dSIlan Peer 	bool tolerated;
34084f58121dSIlan Peer };
34094f58121dSIlan Peer 
34104f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
34114f58121dSIlan Peer 						    struct cfg80211_bss *bss,
34124f58121dSIlan Peer 						    void *_data)
34134f58121dSIlan Peer {
34144f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
34156c608cd6SJohannes Berg 	const struct cfg80211_bss_ies *ies;
34164f58121dSIlan Peer 	const struct element *elem;
34174f58121dSIlan Peer 
34186c608cd6SJohannes Berg 	rcu_read_lock();
34196c608cd6SJohannes Berg 	ies = rcu_dereference(bss->ies);
34206c608cd6SJohannes Berg 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
34216c608cd6SJohannes Berg 				  ies->len);
34224f58121dSIlan Peer 
34234f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
34244f58121dSIlan Peer 	    !(elem->data[10] &
34254f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
34264f58121dSIlan Peer 		data->tolerated = false;
34274f58121dSIlan Peer 	}
34286c608cd6SJohannes Berg 	rcu_read_unlock();
34294f58121dSIlan Peer }
34304f58121dSIlan Peer 
34316e1b5956SJohannes Berg static void
34326e1b5956SJohannes Berg iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
34336e1b5956SJohannes Berg 				   struct ieee80211_vif *vif,
34346e1b5956SJohannes Berg 				   unsigned int link_id,
34356e1b5956SJohannes Berg 				   struct ieee80211_bss_conf *link_conf)
34364f58121dSIlan Peer {
34374f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
34384f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
34394f58121dSIlan Peer 		.tolerated = true,
34404f58121dSIlan Peer 	};
34414f58121dSIlan Peer 
34426e1b5956SJohannes Berg 	if (WARN_ON_ONCE(!link_conf->chandef.chan ||
34436e1b5956SJohannes Berg 			 !mvmvif->link[link_id]))
34446e1b5956SJohannes Berg 		return;
34456e1b5956SJohannes Berg 
34466e1b5956SJohannes Berg 	if (!(link_conf->chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
34476e1b5956SJohannes Berg 		mvmvif->link[link_id]->he_ru_2mhz_block = false;
34484f58121dSIlan Peer 		return;
34494f58121dSIlan Peer 	}
34504f58121dSIlan Peer 
34516e1b5956SJohannes Berg 	cfg80211_bss_iter(hw->wiphy, &link_conf->chandef,
34524f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
34534f58121dSIlan Peer 			  &iter_data);
34544f58121dSIlan Peer 
34554f58121dSIlan Peer 	/*
34564f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
34574f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
34584f58121dSIlan Peer 	 */
34596e1b5956SJohannes Berg 	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
34604f58121dSIlan Peer }
34614f58121dSIlan Peer 
3462f7d6ef33SJohannes Berg static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3463f7d6ef33SJohannes Berg 					       struct ieee80211_vif *vif)
3464f7d6ef33SJohannes Berg {
3465f7d6ef33SJohannes Berg 	struct ieee80211_supported_band *sband;
3466f7d6ef33SJohannes Berg 	const struct ieee80211_sta_he_cap *he_cap;
3467f7d6ef33SJohannes Berg 
3468f7d6ef33SJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION)
3469f7d6ef33SJohannes Berg 		return;
3470f7d6ef33SJohannes Berg 
3471f7d6ef33SJohannes Berg 	if (!mvm->cca_40mhz_workaround)
3472f7d6ef33SJohannes Berg 		return;
3473f7d6ef33SJohannes Berg 
3474f7d6ef33SJohannes Berg 	/* decrement and check that we reached zero */
3475f7d6ef33SJohannes Berg 	mvm->cca_40mhz_workaround--;
3476f7d6ef33SJohannes Berg 	if (mvm->cca_40mhz_workaround)
3477f7d6ef33SJohannes Berg 		return;
3478f7d6ef33SJohannes Berg 
3479f7d6ef33SJohannes Berg 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3480f7d6ef33SJohannes Berg 
3481f7d6ef33SJohannes Berg 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3482f7d6ef33SJohannes Berg 
34831ec7291eSJohannes Berg 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3484f7d6ef33SJohannes Berg 
3485f7d6ef33SJohannes Berg 	if (he_cap) {
3486f7d6ef33SJohannes Berg 		/* we know that ours is writable */
34870301bcd5SBjoern A. Zeeb 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3488f7d6ef33SJohannes Berg 
3489f7d6ef33SJohannes Berg 		he->he_cap_elem.phy_cap_info[0] |=
3490f7d6ef33SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3491f7d6ef33SJohannes Berg 	}
3492f7d6ef33SJohannes Berg }
3493f7d6ef33SJohannes Berg 
34946d19a5ebSEmmanuel Grumbach static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
34956d19a5ebSEmmanuel Grumbach 					struct ieee80211_vif *vif,
34966d19a5ebSEmmanuel Grumbach 					struct iwl_mvm_sta *mvm_sta)
34976d19a5ebSEmmanuel Grumbach {
34986d19a5ebSEmmanuel Grumbach #if IS_ENABLED(CONFIG_IWLMEI)
34996d19a5ebSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
35006d19a5ebSEmmanuel Grumbach 	struct iwl_mei_conn_info conn_info = {
3501f276e20bSJohannes Berg 		.ssid_len = vif->cfg.ssid_len,
35026d19a5ebSEmmanuel Grumbach 	};
35036d19a5ebSEmmanuel Grumbach 
35046d19a5ebSEmmanuel Grumbach 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
35056d19a5ebSEmmanuel Grumbach 		return;
35066d19a5ebSEmmanuel Grumbach 
35076d19a5ebSEmmanuel Grumbach 	if (!mvm->mei_registered)
35086d19a5ebSEmmanuel Grumbach 		return;
35096d19a5ebSEmmanuel Grumbach 
35106c07b73eSJohannes Berg 	/* FIXME: MEI needs to be updated for MLO */
35116c07b73eSJohannes Berg 	if (!vif->bss_conf.chandef.chan)
35126c07b73eSJohannes Berg 		return;
35136c07b73eSJohannes Berg 
35146c07b73eSJohannes Berg 	conn_info.channel = vif->bss_conf.chandef.chan->hw_value;
35156c07b73eSJohannes Berg 
35166d19a5ebSEmmanuel Grumbach 	switch (mvm_sta->pairwise_cipher) {
3517e5d3a64eSAvraham Stern 	case WLAN_CIPHER_SUITE_TKIP:
3518e5d3a64eSAvraham Stern 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3519e5d3a64eSAvraham Stern 		break;
35206d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_CCMP:
35216d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
35226d19a5ebSEmmanuel Grumbach 		break;
35236d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP:
35246d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
35256d19a5ebSEmmanuel Grumbach 		break;
35266d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP_256:
35276d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
35286d19a5ebSEmmanuel Grumbach 		break;
35296d19a5ebSEmmanuel Grumbach 	case 0:
35306d19a5ebSEmmanuel Grumbach 		/* open profile */
35316d19a5ebSEmmanuel Grumbach 		break;
35326d19a5ebSEmmanuel Grumbach 	default:
35336d19a5ebSEmmanuel Grumbach 		/* cipher not supported, don't send anything to iwlmei */
35346d19a5ebSEmmanuel Grumbach 		return;
35356d19a5ebSEmmanuel Grumbach 	}
35366d19a5ebSEmmanuel Grumbach 
35376d19a5ebSEmmanuel Grumbach 	switch (mvmvif->rekey_data.akm) {
35386d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_SAE & 0xff:
35396d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
35406d19a5ebSEmmanuel Grumbach 		break;
35416d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_PSK & 0xff:
35426d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
35436d19a5ebSEmmanuel Grumbach 		break;
35446d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_8021X & 0xff:
35456d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
35466d19a5ebSEmmanuel Grumbach 		break;
35476d19a5ebSEmmanuel Grumbach 	case 0:
35486d19a5ebSEmmanuel Grumbach 		/* open profile */
35496d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
35506d19a5ebSEmmanuel Grumbach 		break;
35516d19a5ebSEmmanuel Grumbach 	default:
35526d19a5ebSEmmanuel Grumbach 		/* auth method / AKM not supported */
35536d19a5ebSEmmanuel Grumbach 		/* TODO: All the FT vesions of these? */
35546d19a5ebSEmmanuel Grumbach 		return;
35556d19a5ebSEmmanuel Grumbach 	}
35566d19a5ebSEmmanuel Grumbach 
3557f276e20bSJohannes Berg 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
35586d19a5ebSEmmanuel Grumbach 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
35596d19a5ebSEmmanuel Grumbach 
35606d19a5ebSEmmanuel Grumbach 	/* TODO: add support for collocated AP data */
35616d19a5ebSEmmanuel Grumbach 	iwl_mei_host_associated(&conn_info, NULL);
35626d19a5ebSEmmanuel Grumbach #endif
35636d19a5ebSEmmanuel Grumbach }
35646d19a5ebSEmmanuel Grumbach 
356587f7e243SMiri Korenblit static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
356687f7e243SMiri Korenblit 					    struct ieee80211_vif *vif,
356787f7e243SMiri Korenblit 					    bool force_assoc_off)
356887f7e243SMiri Korenblit {
356987f7e243SMiri Korenblit 	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
357087f7e243SMiri Korenblit }
357187f7e243SMiri Korenblit 
3572e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3573e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3574e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3575e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3576e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3577e705c121SKalle Valo {
3578a2906ea6SJohannes Berg 	static const struct iwl_mvm_sta_state_ops callbacks = {
357987f7e243SMiri Korenblit 		.add_sta = iwl_mvm_add_sta,
358087f7e243SMiri Korenblit 		.update_sta = iwl_mvm_update_sta,
358187f7e243SMiri Korenblit 		.rm_sta = iwl_mvm_rm_sta,
358287f7e243SMiri Korenblit 		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
358387f7e243SMiri Korenblit 	};
358487f7e243SMiri Korenblit 
358587f7e243SMiri Korenblit 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
358687f7e243SMiri Korenblit 					    &callbacks);
358787f7e243SMiri Korenblit }
358887f7e243SMiri Korenblit 
358957974a55SGregory Greenman /* FIXME: temporary making two assumptions in all sta handling functions:
359057974a55SGregory Greenman  *	(1) when setting sta state, the link exists and protected
359157974a55SGregory Greenman  *	(2) if a link is valid in sta then it's valid in vif (can
359257974a55SGregory Greenman  *	use same index in the link array)
359357974a55SGregory Greenman  */
3594f53be9c4SGregory Greenman static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3595f53be9c4SGregory Greenman 					   struct ieee80211_vif *vif,
359615d41834SJohannes Berg 					   struct ieee80211_sta *sta)
3597f53be9c4SGregory Greenman {
3598f53be9c4SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3599f53be9c4SGregory Greenman 	unsigned int link_id;
360057974a55SGregory Greenman 
3601f53be9c4SGregory Greenman 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3602f53be9c4SGregory Greenman 		struct ieee80211_bss_conf *conf =
3603a705a782SJohannes Berg 			link_conf_dereference_check(vif, link_id);
3604f53be9c4SGregory Greenman 		struct ieee80211_link_sta *link_sta =
3605a705a782SJohannes Berg 			link_sta_dereference_check(sta, link_id);
3606f53be9c4SGregory Greenman 
3607f53be9c4SGregory Greenman 		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3608f53be9c4SGregory Greenman 			continue;
3609f53be9c4SGregory Greenman 
3610c45217bdSJohannes Berg 		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
361115d41834SJohannes Berg 				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3612f53be9c4SGregory Greenman 	}
3613f53be9c4SGregory Greenman }
361457974a55SGregory Greenman 
361557974a55SGregory Greenman #define IWL_MVM_MIN_BEACON_INTERVAL_TU 16
361657974a55SGregory Greenman 
361757974a55SGregory Greenman static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
361857974a55SGregory Greenman 				      struct ieee80211_vif *vif,
361957974a55SGregory Greenman 				      struct ieee80211_sta *sta)
362057974a55SGregory Greenman {
3621207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3622207be64fSMiri Korenblit 	unsigned int link_id;
362357974a55SGregory Greenman 
362457974a55SGregory Greenman 	/* Beacon interval check - firmware will crash if the beacon
362557974a55SGregory Greenman 	 * interval is less than 16. We can't avoid connecting at all,
362657974a55SGregory Greenman 	 * so refuse the station state change, this will cause mac80211
362757974a55SGregory Greenman 	 * to abandon attempts to connect to this AP, and eventually
362857974a55SGregory Greenman 	 * wpa_s will blocklist the AP...
362957974a55SGregory Greenman 	 */
363057974a55SGregory Greenman 
3631207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
363257974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3633207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
363457974a55SGregory Greenman 
3635207be64fSMiri Korenblit 		if (!link_conf)
363657974a55SGregory Greenman 			continue;
363757974a55SGregory Greenman 
363857974a55SGregory Greenman 		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
363957974a55SGregory Greenman 			IWL_ERR(mvm,
364057974a55SGregory Greenman 				"Beacon interval %d for AP %pM is too small\n",
364157974a55SGregory Greenman 				link_conf->beacon_int, link_sta->addr);
364257974a55SGregory Greenman 			return false;
364357974a55SGregory Greenman 		}
364457974a55SGregory Greenman 
364557974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
364657974a55SGregory Greenman 	}
364757974a55SGregory Greenman 
364857974a55SGregory Greenman 	return true;
364957974a55SGregory Greenman }
365057974a55SGregory Greenman 
365157974a55SGregory Greenman static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
365257974a55SGregory Greenman 				       struct ieee80211_vif *vif,
365357974a55SGregory Greenman 				       struct ieee80211_sta *sta,
365457974a55SGregory Greenman 				       bool is_sta)
365557974a55SGregory Greenman {
365657974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3657207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3658207be64fSMiri Korenblit 	unsigned int link_id;
365957974a55SGregory Greenman 
3660207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
366157974a55SGregory Greenman 		struct ieee80211_bss_conf *link_conf =
3662207be64fSMiri Korenblit 			link_conf_dereference_protected(vif, link_id);
366357974a55SGregory Greenman 
3664207be64fSMiri Korenblit 		if (!link_conf || !mvmvif->link[link_id])
366557974a55SGregory Greenman 			continue;
366657974a55SGregory Greenman 
366757974a55SGregory Greenman 		link_conf->he_support = link_sta->he_cap.has_he;
366857974a55SGregory Greenman 
366957974a55SGregory Greenman 		if (is_sta) {
3670207be64fSMiri Korenblit 			mvmvif->link[link_id]->he_ru_2mhz_block = false;
367157974a55SGregory Greenman 			if (link_sta->he_cap.has_he)
3672207be64fSMiri Korenblit 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3673207be64fSMiri Korenblit 								   link_id,
36746e1b5956SJohannes Berg 								   link_conf);
367557974a55SGregory Greenman 		}
367657974a55SGregory Greenman 	}
367757974a55SGregory Greenman }
367857974a55SGregory Greenman 
367957974a55SGregory Greenman static int
368057974a55SGregory Greenman iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
368157974a55SGregory Greenman 				   struct ieee80211_vif *vif,
368257974a55SGregory Greenman 				   struct ieee80211_sta *sta,
3683a2906ea6SJohannes Berg 				   const struct iwl_mvm_sta_state_ops *callbacks)
368457974a55SGregory Greenman {
36850c9a8f90SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3686207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
368757974a55SGregory Greenman 	unsigned int i;
368857974a55SGregory Greenman 	int ret;
368957974a55SGregory Greenman 
369057974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
369157974a55SGregory Greenman 
369257974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_STATION &&
369357974a55SGregory Greenman 	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
369457974a55SGregory Greenman 		return -EINVAL;
369557974a55SGregory Greenman 
369657974a55SGregory Greenman 	if (sta->tdls &&
369757974a55SGregory Greenman 	    (vif->p2p ||
369857974a55SGregory Greenman 	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
369957974a55SGregory Greenman 	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
370057974a55SGregory Greenman 		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
370157974a55SGregory Greenman 		return -EBUSY;
370257974a55SGregory Greenman 	}
370357974a55SGregory Greenman 
370457974a55SGregory Greenman 	ret = callbacks->add_sta(mvm, vif, sta);
370557974a55SGregory Greenman 	if (sta->tdls && ret == 0) {
370657974a55SGregory Greenman 		iwl_mvm_recalc_tdls_state(mvm, vif, true);
370757974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
370857974a55SGregory Greenman 					   NL80211_TDLS_SETUP);
370957974a55SGregory Greenman 	}
371057974a55SGregory Greenman 
3711207be64fSMiri Korenblit 	for_each_sta_active_link(vif, sta, link_sta, i)
371257974a55SGregory Greenman 		link_sta->agg.max_rc_amsdu_len = 1;
3713207be64fSMiri Korenblit 
371457974a55SGregory Greenman 	ieee80211_sta_recalc_aggregates(sta);
371557974a55SGregory Greenman 
37160c9a8f90SJohannes Berg 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
37170c9a8f90SJohannes Berg 		mvmvif->ap_sta = sta;
37180c9a8f90SJohannes Berg 
371957974a55SGregory Greenman 	return 0;
372057974a55SGregory Greenman }
372157974a55SGregory Greenman 
372257974a55SGregory Greenman static int
372357974a55SGregory Greenman iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
372457974a55SGregory Greenman 				struct iwl_mvm *mvm,
372557974a55SGregory Greenman 				struct ieee80211_vif *vif,
372657974a55SGregory Greenman 				struct ieee80211_sta *sta,
3727a2906ea6SJohannes Berg 				const struct iwl_mvm_sta_state_ops *callbacks)
372857974a55SGregory Greenman {
372957974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
373057974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3731207be64fSMiri Korenblit 	struct ieee80211_link_sta *link_sta;
3732207be64fSMiri Korenblit 	unsigned int link_id;
373357974a55SGregory Greenman 
373457974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
373557974a55SGregory Greenman 
373657974a55SGregory Greenman 	if (vif->type == NL80211_IFTYPE_AP) {
373757974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
373857974a55SGregory Greenman 		mvmvif->ap_assoc_sta_count++;
373957974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
374057974a55SGregory Greenman 
374157974a55SGregory Greenman 		/* since the below is not for MLD API, it's ok to use
374257974a55SGregory Greenman 		 * the default bss_conf
374357974a55SGregory Greenman 		 */
374457974a55SGregory Greenman 		if (!mvm->mld_api_is_used &&
374557974a55SGregory Greenman 		    ((vif->bss_conf.he_support &&
374657974a55SGregory Greenman 		      !iwlwifi_mod_params.disable_11ax) ||
374757974a55SGregory Greenman 		    (vif->bss_conf.eht_support &&
374857974a55SGregory Greenman 		     !iwlwifi_mod_params.disable_11be)))
374957974a55SGregory Greenman 			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
375057974a55SGregory Greenman 	} else if (vif->type == NL80211_IFTYPE_STATION) {
375157974a55SGregory Greenman 		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
375257974a55SGregory Greenman 
375357974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
375457974a55SGregory Greenman 
375557974a55SGregory Greenman 		if (!mvm->mld_api_is_used)
375657974a55SGregory Greenman 			goto out;
375757974a55SGregory Greenman 
3758207be64fSMiri Korenblit 		for_each_sta_active_link(vif, sta, link_sta, link_id) {
375957974a55SGregory Greenman 			struct ieee80211_bss_conf *link_conf =
3760207be64fSMiri Korenblit 				link_conf_dereference_protected(vif, link_id);
376157974a55SGregory Greenman 
376257974a55SGregory Greenman 			if (WARN_ON(!link_conf))
376357974a55SGregory Greenman 				return -EINVAL;
3764207be64fSMiri Korenblit 			if (!mvmvif->link[link_id])
3765f14ad95aSJohannes Berg 				continue;
376657974a55SGregory Greenman 
376757974a55SGregory Greenman 			iwl_mvm_link_changed(mvm, vif, link_conf,
376857974a55SGregory Greenman 					     LINK_CONTEXT_MODIFY_ALL &
376957974a55SGregory Greenman 					     ~LINK_CONTEXT_MODIFY_ACTIVE,
377057974a55SGregory Greenman 					     true);
377157974a55SGregory Greenman 		}
377257974a55SGregory Greenman 	}
377357974a55SGregory Greenman 
377457974a55SGregory Greenman out:
377515d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
377657974a55SGregory Greenman 
377757974a55SGregory Greenman 	return callbacks->update_sta(mvm, vif, sta);
377857974a55SGregory Greenman }
377957974a55SGregory Greenman 
378057974a55SGregory Greenman static int
378157974a55SGregory Greenman iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
378257974a55SGregory Greenman 				      struct ieee80211_vif *vif,
378357974a55SGregory Greenman 				      struct ieee80211_sta *sta,
3784a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
378557974a55SGregory Greenman {
378657974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
378757974a55SGregory Greenman 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
378857974a55SGregory Greenman 
378957974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
379057974a55SGregory Greenman 
379157974a55SGregory Greenman 	/* we don't support TDLS during DCM */
379257974a55SGregory Greenman 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
379357974a55SGregory Greenman 		iwl_mvm_teardown_tdls_peers(mvm);
379457974a55SGregory Greenman 
379557974a55SGregory Greenman 	if (sta->tdls) {
379657974a55SGregory Greenman 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
379757974a55SGregory Greenman 					   NL80211_TDLS_ENABLE_LINK);
379857974a55SGregory Greenman 	} else {
379957974a55SGregory Greenman 		/* enable beacon filtering */
380057974a55SGregory Greenman 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
380157974a55SGregory Greenman 
380257974a55SGregory Greenman 		mvmvif->authorized = 1;
380357974a55SGregory Greenman 
380457974a55SGregory Greenman 		callbacks->mac_ctxt_changed(mvm, vif, false);
380557974a55SGregory Greenman 		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
380657974a55SGregory Greenman 	}
380757974a55SGregory Greenman 
380815d41834SJohannes Berg 	mvm_sta->authorized = true;
380915d41834SJohannes Berg 
381015d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
381157974a55SGregory Greenman 
38125a86dcb4SAvraham Stern 	/* MFP is set by default before the station is authorized.
38135a86dcb4SAvraham Stern 	 * Clear it here in case it's not used.
38145a86dcb4SAvraham Stern 	 */
38155a86dcb4SAvraham Stern 	if (!sta->mfp)
38165a86dcb4SAvraham Stern 		return callbacks->update_sta(mvm, vif, sta);
38175a86dcb4SAvraham Stern 
381857974a55SGregory Greenman 	return 0;
381957974a55SGregory Greenman }
382057974a55SGregory Greenman 
382157974a55SGregory Greenman static int
382257974a55SGregory Greenman iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
382357974a55SGregory Greenman 				      struct ieee80211_vif *vif,
382457974a55SGregory Greenman 				      struct ieee80211_sta *sta,
3825a2906ea6SJohannes Berg 				      const struct iwl_mvm_sta_state_ops *callbacks)
382657974a55SGregory Greenman {
382757974a55SGregory Greenman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
382815d41834SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
382957974a55SGregory Greenman 
383057974a55SGregory Greenman 	lockdep_assert_held(&mvm->mutex);
383157974a55SGregory Greenman 
383215d41834SJohannes Berg 	mvmsta->authorized = false;
383315d41834SJohannes Berg 
383457974a55SGregory Greenman 	/* once we move into assoc state, need to update rate scale to
383557974a55SGregory Greenman 	 * disable using wide bandwidth
383657974a55SGregory Greenman 	 */
383715d41834SJohannes Berg 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
383857974a55SGregory Greenman 
383957974a55SGregory Greenman 	if (!sta->tdls) {
384057974a55SGregory Greenman 		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
384157974a55SGregory Greenman 		 * yet to avoid sending high prio again for a little
384257974a55SGregory Greenman 		 * time.
384357974a55SGregory Greenman 		 */
384457974a55SGregory Greenman 		mvmvif->authorized = 0;
384557974a55SGregory Greenman 
384657974a55SGregory Greenman 		/* disable beacon filtering */
3847fccf5ff1SJohannes Berg 		iwl_mvm_disable_beacon_filter(mvm, vif, 0);
384857974a55SGregory Greenman 	}
384957974a55SGregory Greenman 
385057974a55SGregory Greenman 	return 0;
385157974a55SGregory Greenman }
385257974a55SGregory Greenman 
385387f7e243SMiri Korenblit /* Common part for MLD and non-MLD modes */
385487f7e243SMiri Korenblit int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
385587f7e243SMiri Korenblit 				 struct ieee80211_vif *vif,
385687f7e243SMiri Korenblit 				 struct ieee80211_sta *sta,
385787f7e243SMiri Korenblit 				 enum ieee80211_sta_state old_state,
385887f7e243SMiri Korenblit 				 enum ieee80211_sta_state new_state,
3859a2906ea6SJohannes Berg 				 const struct iwl_mvm_sta_state_ops *callbacks)
386087f7e243SMiri Korenblit {
3861e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3862e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
38636ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3864de107600SJohannes Berg 	struct ieee80211_link_sta *link_sta;
386557974a55SGregory Greenman 	unsigned int link_id;
3866e705c121SKalle Valo 	int ret;
3867e705c121SKalle Valo 
3868e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3869e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3870e705c121SKalle Valo 
387124afba76SLiad Kaufman 	/*
387224afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
387324afba76SLiad Kaufman 	 *
387424afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
387524afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
387624afba76SLiad Kaufman 	 * the queues.
387724afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
387824afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
387924afba76SLiad Kaufman 	 * of.
388024afba76SLiad Kaufman 	 *
388124afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
388224afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
388324afba76SLiad Kaufman 	 */
388424afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3885c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
388624afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
388724afba76SLiad Kaufman 
388824afba76SLiad Kaufman 		/*
388924afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
389024afba76SLiad Kaufman 		 * worker will already remove it if it was on
389124afba76SLiad Kaufman 		 */
3892f7d6ef33SJohannes Berg 
3893f7d6ef33SJohannes Berg 		/*
3894f7d6ef33SJohannes Berg 		 * Additionally, reset the 40 MHz capability if we disconnected
3895f7d6ef33SJohannes Berg 		 * from the AP now.
3896f7d6ef33SJohannes Berg 		 */
3897f7d6ef33SJohannes Berg 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
3898783336b0SJohannes Berg 
3899783336b0SJohannes Berg 		/* Also free dup data just in case any assertions below fail */
3900783336b0SJohannes Berg 		kfree(mvm_sta->dup_data);
390124afba76SLiad Kaufman 	}
390224afba76SLiad Kaufman 
3903e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
390457974a55SGregory Greenman 
3905828c79d9SEmmanuel Grumbach 	/* this would be a mac80211 bug ... but don't crash, unless we had a
3906828c79d9SEmmanuel Grumbach 	 * firmware crash while we were activating a link, in which case it is
3907828c79d9SEmmanuel Grumbach 	 * legit to have phy_ctxt = NULL. Don't bother not to WARN if we are in
3908828c79d9SEmmanuel Grumbach 	 * recovery flow since we spit tons of error messages anyway.
3909828c79d9SEmmanuel Grumbach 	 */
3910de107600SJohannes Berg 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
39113d6d21b2SJohannes Berg 		if (WARN_ON_ONCE(!mvmvif->link[link_id] ||
39123d6d21b2SJohannes Berg 				 !mvmvif->link[link_id]->phy_ctxt)) {
391357974a55SGregory Greenman 			mutex_unlock(&mvm->mutex);
391457974a55SGregory Greenman 			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
391557974a55SGregory Greenman 					&mvm->status) ? 0 : -EINVAL;
391657974a55SGregory Greenman 		}
391757974a55SGregory Greenman 	}
391857974a55SGregory Greenman 
39196ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3920d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
39216ea29ce5SJohannes Berg 
3922e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3923e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
392457974a55SGregory Greenman 		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
392557974a55SGregory Greenman 							 callbacks);
392657974a55SGregory Greenman 		if (ret < 0)
3927e705c121SKalle Valo 			goto out_unlock;
3928e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3929e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3930e705c121SKalle Valo 		/*
3931e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3932e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3933e705c121SKalle Valo 		 */
3934e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3935e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3936e705c121SKalle Valo 		ret = 0;
3937e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3938e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
393957974a55SGregory Greenman 		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
394057974a55SGregory Greenman 						      callbacks);
3941e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3942e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
394357974a55SGregory Greenman 		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
394457974a55SGregory Greenman 							    callbacks);
3945e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3946e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
394757974a55SGregory Greenman 		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
394857974a55SGregory Greenman 							    callbacks);
3949e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3950e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
39518be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
39528be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
395387f7e243SMiri Korenblit 			callbacks->mac_ctxt_changed(mvm, vif, false);
3954d5d8ee52SEmmanuel Grumbach 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3955cf7a7457SJohannes Berg 			iwl_mvm_stop_session_protection(mvm, vif);
3956e705c121SKalle Valo 		ret = 0;
3957e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3958e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3959e705c121SKalle Valo 		ret = 0;
3960e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3961e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
39620c9a8f90SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
3963d5d8ee52SEmmanuel Grumbach 			iwl_mvm_stop_session_protection(mvm, vif);
39640c9a8f90SJohannes Berg 			mvmvif->ap_sta = NULL;
39650c9a8f90SJohannes Berg 		}
396687f7e243SMiri Korenblit 		ret = callbacks->rm_sta(mvm, vif, sta);
39671e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3968e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
39691e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
39701e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
39711e8f1329SGolan Ben-Ami 		}
397234a880d8SLiad Kaufman 
397344135b7cSIlan Peer 		if (unlikely(ret &&
397444135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
397544135b7cSIlan Peer 				      &mvm->status)))
397644135b7cSIlan Peer 			ret = 0;
3977e705c121SKalle Valo 	} else {
3978e705c121SKalle Valo 		ret = -EIO;
3979e705c121SKalle Valo 	}
3980e705c121SKalle Valo  out_unlock:
3981e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3982e705c121SKalle Valo 
3983e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3984e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3985e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3986e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3987e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3988e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3989e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3990e705c121SKalle Valo 	}
3991e705c121SKalle Valo 
3992e705c121SKalle Valo 	return ret;
3993e705c121SKalle Valo }
3994e705c121SKalle Valo 
3995cbce62a3SMiri Korenblit int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3996e705c121SKalle Valo {
3997e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3998e705c121SKalle Valo 
3999e705c121SKalle Valo 	mvm->rts_threshold = value;
4000e705c121SKalle Valo 
4001e705c121SKalle Valo 	return 0;
4002e705c121SKalle Valo }
4003e705c121SKalle Valo 
4004cbce62a3SMiri Korenblit void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4005e705c121SKalle Valo 			   struct ieee80211_sta *sta, u32 changed)
4006e705c121SKalle Valo {
4007e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4008dcfe3b10SJohannes Berg 
4009dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
4010dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
4011dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
401215d41834SJohannes Berg 		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4013e705c121SKalle Valo 
4014e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
4015e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
4016e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
4017e705c121SKalle Valo }
4018e705c121SKalle Valo 
4019e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
4020b3e2130bSJohannes Berg 			       struct ieee80211_vif *vif,
4021b3e2130bSJohannes Berg 			       unsigned int link_id, u16 ac,
4022e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
4023e705c121SKalle Valo {
4024e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4025e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4026e705c121SKalle Valo 
4027650cadb7SGregory Greenman 	mvmvif->deflink.queue_params[ac] = *params;
4028e705c121SKalle Valo 
4029e705c121SKalle Valo 	/*
4030e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
4031e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
4032e705c121SKalle Valo 	 */
4033e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
4034e705c121SKalle Valo 		int ret;
4035e705c121SKalle Valo 
4036e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
4037e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4038e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
4039e705c121SKalle Valo 		return ret;
4040e705c121SKalle Valo 	}
4041e705c121SKalle Valo 	return 0;
4042e705c121SKalle Valo }
4043e705c121SKalle Valo 
4044cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4045d4e36e55SIlan Peer 				struct ieee80211_vif *vif,
404615fae341SJohannes Berg 				struct ieee80211_prep_tx_info *info)
4047e705c121SKalle Valo {
4048e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4049d4e36e55SIlan Peer 
4050e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4051af84ac57SJohannes Berg 	iwl_mvm_protect_assoc(mvm, vif, info->duration);
4052e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4053e705c121SKalle Valo }
4054e705c121SKalle Valo 
4055cbce62a3SMiri Korenblit void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
40566b1259d1SJohannes Berg 				 struct ieee80211_vif *vif,
40576b1259d1SJohannes Berg 				 struct ieee80211_prep_tx_info *info)
40586b1259d1SJohannes Berg {
40596b1259d1SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
40606b1259d1SJohannes Berg 
40616b1259d1SJohannes Berg 	/* for successful cases (auth/assoc), don't cancel session protection */
40626b1259d1SJohannes Berg 	if (info->success)
40636b1259d1SJohannes Berg 		return;
40646b1259d1SJohannes Berg 
40656b1259d1SJohannes Berg 	mutex_lock(&mvm->mutex);
40666b1259d1SJohannes Berg 	iwl_mvm_stop_session_protection(mvm, vif);
40676b1259d1SJohannes Berg 	mutex_unlock(&mvm->mutex);
40686b1259d1SJohannes Berg }
40696b1259d1SJohannes Berg 
4070cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4071e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4072e705c121SKalle Valo 				 struct cfg80211_sched_scan_request *req,
4073e705c121SKalle Valo 				 struct ieee80211_scan_ies *ies)
4074e705c121SKalle Valo {
4075e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4076e705c121SKalle Valo 
4077e705c121SKalle Valo 	int ret;
4078e705c121SKalle Valo 
4079e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4080e705c121SKalle Valo 
4081f276e20bSJohannes Berg 	if (!vif->cfg.idle) {
4082e705c121SKalle Valo 		ret = -EBUSY;
4083e705c121SKalle Valo 		goto out;
4084e705c121SKalle Valo 	}
4085e705c121SKalle Valo 
4086e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4087e705c121SKalle Valo 
4088e705c121SKalle Valo out:
4089e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4090e705c121SKalle Valo 	return ret;
4091e705c121SKalle Valo }
4092e705c121SKalle Valo 
4093cbce62a3SMiri Korenblit int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4094e705c121SKalle Valo 				struct ieee80211_vif *vif)
4095e705c121SKalle Valo {
4096e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4097e705c121SKalle Valo 	int ret;
4098e705c121SKalle Valo 
4099e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4100e705c121SKalle Valo 
4101e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
4102e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
4103e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
4104e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
4105e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4106e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
4107e705c121SKalle Valo 	 * not running.
4108e705c121SKalle Valo 	*/
4109e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4110e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
4111e705c121SKalle Valo 		return 0;
4112e705c121SKalle Valo 	}
4113e705c121SKalle Valo 
4114e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4115e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4116e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
4117e705c121SKalle Valo 
4118e705c121SKalle Valo 	return ret;
4119e705c121SKalle Valo }
4120e705c121SKalle Valo 
41216569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4122e705c121SKalle Valo 				 enum set_key_cmd cmd,
4123e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4124e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4125e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
4126e705c121SKalle Valo {
4127c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4128e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
412946c7b05aSEmmanuel Grumbach 	struct iwl_mvm_sta *mvmsta = NULL;
4130d066a530SJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn = NULL;
4131f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
41325c75a208SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
41335c75a208SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4134c56e00a3SJohannes Berg 	int ret, i;
41354615fd15SEmmanuel Grumbach 	u8 key_offset;
4136e705c121SKalle Valo 
413746c7b05aSEmmanuel Grumbach 	if (sta)
41386d19a5ebSEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
41396d19a5ebSEmmanuel Grumbach 
4140e705c121SKalle Valo 	switch (key->cipher) {
4141e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
4142286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
4143e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
41441ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
41457f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
41467f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
41477f768ad5SDavid Spinadel 		} else {
41487f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
41497f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
41507f768ad5SDavid Spinadel 		}
4151e705c121SKalle Valo 		break;
4152e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
41532a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
41542a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
415585aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
4156e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4157e705c121SKalle Valo 		break;
4158e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
41598e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
41608e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4161e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4162e705c121SKalle Valo 		break;
4163e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
4164e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
4165475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
4166e705c121SKalle Valo 			break;
4167475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
4168475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
4169475c6bdeSJohannes Berg 		/* support HW crypto on TX */
4170475c6bdeSJohannes Berg 		return 0;
4171e705c121SKalle Valo 	default:
4172e705c121SKalle Valo 		return -EOPNOTSUPP;
4173e705c121SKalle Valo 	}
4174e705c121SKalle Valo 
4175e705c121SKalle Valo 	switch (cmd) {
4176e705c121SKalle Valo 	case SET_KEY:
4177a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4178a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4179b1fdc250SJohannes Berg 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4180b1fdc250SJohannes Berg 					   key);
4181b1fdc250SJohannes Berg 
4182e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4183e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4184e705c121SKalle Valo 			/*
4185e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
4186e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
4187e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
4188*f05d1e04SJohannes Berg 			 * CMAC/GMAC in AP/IBSS modes must be done in software
4189*f05d1e04SJohannes Berg 			 * on older NICs.
4190a5de7de7SJohannes Berg 			 *
4191a5de7de7SJohannes Berg 			 * Except, of course, beacon protection - it must be
4192*f05d1e04SJohannes Berg 			 * offloaded since we just set a beacon template, and
4193*f05d1e04SJohannes Berg 			 * then we must also offload the IGTK (not just BIGTK)
4194*f05d1e04SJohannes Berg 			 * for firmware reasons.
4195*f05d1e04SJohannes Berg 			 *
4196*f05d1e04SJohannes Berg 			 * So just check for beacon protection - if we don't
4197*f05d1e04SJohannes Berg 			 * have it we cannot get here with keyidx >= 6, and
4198*f05d1e04SJohannes Berg 			 * if we do have it we need to send the key to FW in
4199*f05d1e04SJohannes Berg 			 * all cases (CMAC/GMAC).
4200e705c121SKalle Valo 			 */
4201*f05d1e04SJohannes Berg 			if (!wiphy_ext_feature_isset(hw->wiphy,
4202*f05d1e04SJohannes Berg 						     NL80211_EXT_FEATURE_BEACON_PROTECTION) &&
4203a5de7de7SJohannes Berg 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
42048e160ab8SAyala Beker 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4205a5de7de7SJohannes Berg 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
420681279c49SJohannes Berg 				ret = -EOPNOTSUPP;
4207a1c2ff30SAndrei Otcheretianski 				break;
4208a1c2ff30SAndrei Otcheretianski 			}
420985aeb58cSDavid Spinadel 
421085aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
421185aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
421285aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
4213e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
4214a1c2ff30SAndrei Otcheretianski 				ret = 0;
4215e705c121SKalle Valo 				break;
4216e705c121SKalle Valo 			}
4217c56e00a3SJohannes Berg 
4218c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
4219c56e00a3SJohannes Berg 				for (i = 0;
4220c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4221c56e00a3SJohannes Berg 				     i++) {
4222c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
4223c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
4224c56e00a3SJohannes Berg 						break;
4225c56e00a3SJohannes Berg 					}
4226c56e00a3SJohannes Berg 				}
4227c56e00a3SJohannes Berg 
4228c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4229c56e00a3SJohannes Berg 					ret = -ENOSPC;
4230a1c2ff30SAndrei Otcheretianski 				else
4231a1c2ff30SAndrei Otcheretianski 					ret = 0;
4232c56e00a3SJohannes Berg 
4233c56e00a3SJohannes Berg 				break;
4234c56e00a3SJohannes Berg 			}
423585aeb58cSDavid Spinadel 		}
4236e705c121SKalle Valo 
4237e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
4238e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
4239e705c121SKalle Valo 		 */
4240e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4241e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4242e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
4243e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
4244e705c121SKalle Valo 			ret = 0;
4245e705c121SKalle Valo 			break;
4246e705c121SKalle Valo 		}
4247e705c121SKalle Valo 
4248f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
424946c7b05aSEmmanuel Grumbach 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4250f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4251f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
42522a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
42532a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4254f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
4255f5e28eacSJohannes Berg 			int tid, q;
4256f5e28eacSJohannes Berg 
4257f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4258acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4259acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
4260f5e28eacSJohannes Berg 					 GFP_KERNEL);
4261f5e28eacSJohannes Berg 			if (!ptk_pn) {
4262f5e28eacSJohannes Berg 				ret = -ENOMEM;
4263f5e28eacSJohannes Berg 				break;
4264f5e28eacSJohannes Berg 			}
4265f5e28eacSJohannes Berg 
4266f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4267f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
4268f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
4269f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
4270f5e28eacSJohannes Berg 					       seq.ccmp.pn,
4271f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
4272f5e28eacSJohannes Berg 			}
4273f5e28eacSJohannes Berg 
4274f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4275f5e28eacSJohannes Berg 		}
4276f5e28eacSJohannes Berg 
42774615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
42784615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
42794615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
42804615fd15SEmmanuel Grumbach 		else
42814615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
42824615fd15SEmmanuel Grumbach 
428346c7b05aSEmmanuel Grumbach 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
42846d19a5ebSEmmanuel Grumbach 			mvmsta->pairwise_cipher = key->cipher;
42856d19a5ebSEmmanuel Grumbach 
4286a5de7de7SJohannes Berg 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4287a5de7de7SJohannes Berg 				   sta ? sta->addr : NULL, key->keyidx);
42885c75a208SJohannes Berg 
42895c75a208SJohannes Berg 		if (sec_key_ver)
42905c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
42915c75a208SJohannes Berg 		else
42924615fd15SEmmanuel Grumbach 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
42935c75a208SJohannes Berg 
4294e705c121SKalle Valo 		if (ret) {
4295e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
4296475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
4297d066a530SJohannes Berg 			if (ptk_pn) {
4298d066a530SJohannes Berg 				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4299d066a530SJohannes Berg 				kfree(ptk_pn);
4300d066a530SJohannes Berg 			}
4301e705c121SKalle Valo 			/*
4302e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
4303475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
4304475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
4305475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
4306e705c121SKalle Valo 			 */
4307475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
4308475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
4309475c6bdeSJohannes Berg 			else
4310e705c121SKalle Valo 				ret = 0;
4311e705c121SKalle Valo 		}
4312e705c121SKalle Valo 
4313e705c121SKalle Valo 		break;
4314e705c121SKalle Valo 	case DISABLE_KEY:
4315a5de7de7SJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION &&
4316a5de7de7SJohannes Berg 		    (keyidx == 6 || keyidx == 7))
4317b1fdc250SJohannes Berg 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4318b1fdc250SJohannes Berg 					 NULL);
4319b1fdc250SJohannes Berg 
4320c56e00a3SJohannes Berg 		ret = -ENOENT;
4321c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4322c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
4323c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
4324c56e00a3SJohannes Berg 				ret = 0;
4325c56e00a3SJohannes Berg 			}
4326c56e00a3SJohannes Berg 		}
4327c56e00a3SJohannes Berg 
4328c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
4329c56e00a3SJohannes Berg 		if (ret == 0)
4330c56e00a3SJohannes Berg 			break;
4331c56e00a3SJohannes Berg 
4332e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4333e705c121SKalle Valo 			ret = 0;
4334e705c121SKalle Valo 			break;
4335e705c121SKalle Valo 		}
4336e705c121SKalle Valo 
433746c7b05aSEmmanuel Grumbach 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4338f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4339f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
43402a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
43412a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4342f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
4343f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
4344f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
4345f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4346f5e28eacSJohannes Berg 			if (ptk_pn)
4347f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
4348f5e28eacSJohannes Berg 		}
4349f5e28eacSJohannes Berg 
4350e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
43515c75a208SJohannes Berg 		if (sec_key_ver)
43525c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
43535c75a208SJohannes Berg 		else
4354e705c121SKalle Valo 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4355e705c121SKalle Valo 		break;
4356e705c121SKalle Valo 	default:
4357e705c121SKalle Valo 		ret = -EINVAL;
4358e705c121SKalle Valo 	}
4359e705c121SKalle Valo 
43606569e7d3SJohannes Berg 	return ret;
43616569e7d3SJohannes Berg }
43626569e7d3SJohannes Berg 
4363cbce62a3SMiri Korenblit int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4364cbce62a3SMiri Korenblit 			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
43656569e7d3SJohannes Berg 			struct ieee80211_key_conf *key)
43666569e7d3SJohannes Berg {
43676569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
43686569e7d3SJohannes Berg 	int ret;
43696569e7d3SJohannes Berg 
43706569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
43716569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4372e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
43736569e7d3SJohannes Berg 
4374e705c121SKalle Valo 	return ret;
4375e705c121SKalle Valo }
4376e705c121SKalle Valo 
4377cbce62a3SMiri Korenblit void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4378e705c121SKalle Valo 				 struct ieee80211_vif *vif,
4379e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
4380e705c121SKalle Valo 				 struct ieee80211_sta *sta,
4381e705c121SKalle Valo 				 u32 iv32, u16 *phase1key)
4382e705c121SKalle Valo {
4383e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4384e705c121SKalle Valo 
4385e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4386e705c121SKalle Valo 		return;
4387e705c121SKalle Valo 
4388e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4389e705c121SKalle Valo }
4390e705c121SKalle Valo 
4391e705c121SKalle Valo 
4392e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4393e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
4394e705c121SKalle Valo {
4395e705c121SKalle Valo 	struct iwl_mvm *mvm =
4396e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
4397e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
4398e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
4399e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
4400e705c121SKalle Valo 
4401e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4402e705c121SKalle Valo 		return true;
4403e705c121SKalle Valo 
4404e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4405e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4406e705c121SKalle Valo 		return true;
4407e705c121SKalle Valo 	}
4408e705c121SKalle Valo 
4409e705c121SKalle Valo 	resp = (void *)pkt->data;
4410e705c121SKalle Valo 
4411e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
4412b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4413e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
4414e705c121SKalle Valo 
4415e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4416e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4417e705c121SKalle Valo 		     te_data->uid);
4418e705c121SKalle Valo 
4419e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4420e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4421e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4422e705c121SKalle Valo 
4423e705c121SKalle Valo 	return true;
4424e705c121SKalle Valo }
4425e705c121SKalle Valo 
4426dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
4427dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
4428dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
4429dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
4430dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
4431e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4432e705c121SKalle Valo 				    struct ieee80211_channel *channel,
4433e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4434e705c121SKalle Valo 				    int duration)
4435e705c121SKalle Valo {
4436afc1e3b4SAvraham Stern 	int res;
4437e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4438e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4439e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4440e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
4441dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
4442dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
4443dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
4444e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
4445e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4446e705c121SKalle Valo 		.id_and_color =
4447e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4448e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4449e705c121SKalle Valo 	};
445057e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
445157e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
445257e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
445357e861d9SDavid Spinadel 
445457e861d9SDavid Spinadel 	/* Set the channel info data */
445557e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
44563717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
44577ac87575SJohannes Berg 			      IWL_PHY_CHANNEL_MODE20,
445857e861d9SDavid Spinadel 			      0);
445957e861d9SDavid Spinadel 
446057e861d9SDavid Spinadel 	/* Set the time and duration */
4461afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4462e705c121SKalle Valo 
4463dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
4464dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
4465dc28e12fSMatti Gottlieb 
4466dc28e12fSMatti Gottlieb 	/*
4467dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
4468dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
4469dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
4470dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
4471dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
4472dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
4473dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
4474dc28e12fSMatti Gottlieb 	 */
4475f276e20bSJohannes Berg 	if (vif->cfg.assoc) {
4476dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
4477dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
4478dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
4479dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
4480dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
4481dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
4482dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
4483dc28e12fSMatti Gottlieb 		}
4484dc28e12fSMatti Gottlieb 	}
4485dc28e12fSMatti Gottlieb 
448657e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
448757e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
4488dc28e12fSMatti Gottlieb 
4489dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
4490903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
4491903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
4492903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
4493903b3f9bSEmmanuel Grumbach 		     "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
4494903b3f9bSEmmanuel Grumbach 		     duration, delay, dtim_interval);
4495903b3f9bSEmmanuel Grumbach 
4496e705c121SKalle Valo 	/* Set the node address */
449757e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4498e705c121SKalle Valo 
4499e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4500e705c121SKalle Valo 
4501e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4502e705c121SKalle Valo 
4503e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4504e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4505e705c121SKalle Valo 		return -EIO;
4506e705c121SKalle Valo 	}
4507e705c121SKalle Valo 
4508e705c121SKalle Valo 	te_data->vif = vif;
4509e705c121SKalle Valo 	te_data->duration = duration;
4510e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
4511e705c121SKalle Valo 
4512e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4513e705c121SKalle Valo 
4514e705c121SKalle Valo 	/*
4515e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
4516e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
4517e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
4518e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4519e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
4520e705c121SKalle Valo 	 * time another notification (that the time event started)
4521e705c121SKalle Valo 	 * might already be processed unsuccessfully.
4522e705c121SKalle Valo 	 */
4523e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4524e705c121SKalle Valo 				   time_event_response,
4525e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
4526e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
4527e705c121SKalle Valo 
452857e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4529e705c121SKalle Valo 				   &aux_roc_req);
4530e705c121SKalle Valo 
4531e705c121SKalle Valo 	if (res) {
4532e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4533e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4534e705c121SKalle Valo 		goto out_clear_te;
4535e705c121SKalle Valo 	}
4536e705c121SKalle Valo 
4537e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4538e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4539e705c121SKalle Valo 	/* should never fail */
4540e705c121SKalle Valo 	WARN_ON_ONCE(res);
4541e705c121SKalle Valo 
4542e705c121SKalle Valo 	if (res) {
4543e705c121SKalle Valo  out_clear_te:
4544e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
4545e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
4546e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4547e705c121SKalle Valo 	}
4548e705c121SKalle Valo 
4549e705c121SKalle Valo 	return res;
4550e705c121SKalle Valo }
4551e705c121SKalle Valo 
4552feebebaeSMiri Korenblit static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4553feebebaeSMiri Korenblit {
4554feebebaeSMiri Korenblit 	int ret = 0;
4555feebebaeSMiri Korenblit 
4556feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4557feebebaeSMiri Korenblit 
4558feebebaeSMiri Korenblit 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4559feebebaeSMiri Korenblit 			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4560feebebaeSMiri Korenblit 		IWL_ERR(mvm, "hotspot not supported\n");
4561feebebaeSMiri Korenblit 		return -EINVAL;
4562feebebaeSMiri Korenblit 	}
4563feebebaeSMiri Korenblit 
45641724fc78SEmmanuel Grumbach 	if (iwl_mvm_has_new_station_api(mvm->fw)) {
4565feebebaeSMiri Korenblit 		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4566feebebaeSMiri Korenblit 		WARN(ret, "Failed to allocate aux station");
4567feebebaeSMiri Korenblit 	}
4568feebebaeSMiri Korenblit 
4569feebebaeSMiri Korenblit 	return ret;
4570feebebaeSMiri Korenblit }
4571feebebaeSMiri Korenblit 
4572feebebaeSMiri Korenblit static int iwl_mvm_roc_switch_binding(struct iwl_mvm *mvm,
4573feebebaeSMiri Korenblit 				      struct ieee80211_vif *vif,
4574feebebaeSMiri Korenblit 				      struct iwl_mvm_phy_ctxt *new_phy_ctxt)
4575feebebaeSMiri Korenblit {
4576feebebaeSMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4577feebebaeSMiri Korenblit 	int ret = 0;
4578feebebaeSMiri Korenblit 
4579feebebaeSMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
4580feebebaeSMiri Korenblit 
4581feebebaeSMiri Korenblit 	/* Unbind the P2P_DEVICE from the current PHY context,
4582feebebaeSMiri Korenblit 	 * and if the PHY context is not used remove it.
4583feebebaeSMiri Korenblit 	 */
4584feebebaeSMiri Korenblit 	ret = iwl_mvm_binding_remove_vif(mvm, vif);
4585feebebaeSMiri Korenblit 	if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
4586feebebaeSMiri Korenblit 		return ret;
4587feebebaeSMiri Korenblit 
4588feebebaeSMiri Korenblit 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
4589feebebaeSMiri Korenblit 
4590feebebaeSMiri Korenblit 	/* Bind the P2P_DEVICE to the current PHY Context */
4591feebebaeSMiri Korenblit 	mvmvif->deflink.phy_ctxt = new_phy_ctxt;
4592feebebaeSMiri Korenblit 
4593feebebaeSMiri Korenblit 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4594feebebaeSMiri Korenblit 	WARN(ret, "Failed binding P2P_DEVICE\n");
4595feebebaeSMiri Korenblit 	return ret;
4596feebebaeSMiri Korenblit }
4597feebebaeSMiri Korenblit 
4598e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
4599e705c121SKalle Valo 		       struct ieee80211_vif *vif,
4600e705c121SKalle Valo 		       struct ieee80211_channel *channel,
4601e705c121SKalle Valo 		       int duration,
4602e705c121SKalle Valo 		       enum ieee80211_roc_type type)
4603e705c121SKalle Valo {
4604a2906ea6SJohannes Berg 	static const struct iwl_mvm_roc_ops ops = {
4605fe8b2ad3SMiri Korenblit 		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
4606fe8b2ad3SMiri Korenblit 		.switch_phy_ctxt = iwl_mvm_roc_switch_binding,
4607fe8b2ad3SMiri Korenblit 	};
4608fe8b2ad3SMiri Korenblit 
4609fe8b2ad3SMiri Korenblit 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4610fe8b2ad3SMiri Korenblit }
4611fe8b2ad3SMiri Korenblit 
4612fe8b2ad3SMiri Korenblit /* Execute the common part for MLD and non-MLD modes */
4613fe8b2ad3SMiri Korenblit int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4614fe8b2ad3SMiri Korenblit 		       struct ieee80211_channel *channel, int duration,
4615fe8b2ad3SMiri Korenblit 		       enum ieee80211_roc_type type,
4616a2906ea6SJohannes Berg 		       const struct iwl_mvm_roc_ops *ops)
4617fe8b2ad3SMiri Korenblit {
4618e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4619e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4620e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
4621e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
46228835a64fSJohannes Berg 	bool band_change_removal;
4623e705c121SKalle Valo 	int ret, i;
4624feebebaeSMiri Korenblit 	u32 lmac_id;
4625e705c121SKalle Valo 
4626e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4627e705c121SKalle Valo 			   duration, type);
4628e705c121SKalle Valo 
46299834781cSJohannes Berg 	/*
46309834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
46319834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
46329834781cSJohannes Berg 	 */
4633e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
4634e705c121SKalle Valo 
4635e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4636e705c121SKalle Valo 
4637e705c121SKalle Valo 	switch (vif->type) {
4638e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4639d51439a6SAriel Malamud 		lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
46402c2c3647SNathan Errera 
4641feebebaeSMiri Korenblit 		/* Use aux roc framework (HS20) */
4642fe8b2ad3SMiri Korenblit 		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4643feebebaeSMiri Korenblit 		if (!ret)
4644e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
4645e705c121SKalle Valo 						       vif, duration);
4646e705c121SKalle Valo 		goto out_unlock;
4647e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
4648e705c121SKalle Valo 		/* handle below */
4649e705c121SKalle Valo 		break;
4650e705c121SKalle Valo 	default:
4651e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
4652e705c121SKalle Valo 		ret = -EINVAL;
4653e705c121SKalle Valo 		goto out_unlock;
4654e705c121SKalle Valo 	}
4655e705c121SKalle Valo 
4656e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
4657e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
4658650cadb7SGregory Greenman 		if (phy_ctxt->ref == 0 || mvmvif->deflink.phy_ctxt == phy_ctxt)
4659e705c121SKalle Valo 			continue;
4660e705c121SKalle Valo 
4661e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
4662fe8b2ad3SMiri Korenblit 			ret = ops->switch_phy_ctxt(mvm, vif, phy_ctxt);
4663feebebaeSMiri Korenblit 			if (ret)
4664e705c121SKalle Valo 				goto out_unlock;
4665e705c121SKalle Valo 
4666650cadb7SGregory Greenman 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4667e705c121SKalle Valo 			goto schedule_time_event;
4668e705c121SKalle Valo 		}
4669e705c121SKalle Valo 	}
4670e705c121SKalle Valo 
4671e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
4672650cadb7SGregory Greenman 	if (channel == mvmvif->deflink.phy_ctxt->channel)
4673e705c121SKalle Valo 		goto schedule_time_event;
4674e705c121SKalle Valo 
4675e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4676e705c121SKalle Valo 
4677e705c121SKalle Valo 	/*
46788835a64fSJohannes Berg 	 * Check if the remain-on-channel is on a different band and that
46798835a64fSJohannes Berg 	 * requires context removal, see iwl_mvm_phy_ctxt_changed(). If
46808835a64fSJohannes Berg 	 * so, we'll need to release and then re-configure here, since we
46818835a64fSJohannes Berg 	 * must not remove a PHY context that's part of a binding.
4682e705c121SKalle Valo 	 */
46838835a64fSJohannes Berg 	band_change_removal =
46848835a64fSJohannes Berg 		fw_has_capa(&mvm->fw->ucode_capa,
46858835a64fSJohannes Berg 			    IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
4686650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt->channel->band != chandef.chan->band;
46878835a64fSJohannes Berg 
4688650cadb7SGregory Greenman 	if (mvmvif->deflink.phy_ctxt->ref == 1 && !band_change_removal) {
46898835a64fSJohannes Berg 		/*
46908835a64fSJohannes Berg 		 * Change the PHY context configuration as it is currently
46918835a64fSJohannes Berg 		 * referenced only by the P2P Device MAC (and we can modify it)
46928835a64fSJohannes Berg 		 */
4693650cadb7SGregory Greenman 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->deflink.phy_ctxt,
4694e705c121SKalle Valo 					       &chandef, 1, 1);
4695e705c121SKalle Valo 		if (ret)
4696e705c121SKalle Valo 			goto out_unlock;
4697e705c121SKalle Valo 	} else {
4698e705c121SKalle Valo 		/*
46998835a64fSJohannes Berg 		 * The PHY context is shared with other MACs (or we're trying to
47008835a64fSJohannes Berg 		 * switch bands), so remove the P2P Device from the binding,
47018835a64fSJohannes Berg 		 * allocate an new PHY context and create a new binding.
4702e705c121SKalle Valo 		 */
4703e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4704e705c121SKalle Valo 		if (!phy_ctxt) {
4705e705c121SKalle Valo 			ret = -ENOSPC;
4706e705c121SKalle Valo 			goto out_unlock;
4707e705c121SKalle Valo 		}
4708e705c121SKalle Valo 
4709e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
4710e705c121SKalle Valo 					       1, 1);
4711e705c121SKalle Valo 		if (ret) {
4712e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
4713e705c121SKalle Valo 			goto out_unlock;
4714e705c121SKalle Valo 		}
4715e705c121SKalle Valo 
4716fe8b2ad3SMiri Korenblit 		ret = ops->switch_phy_ctxt(mvm, vif, phy_ctxt);
4717feebebaeSMiri Korenblit 		if (ret)
4718e705c121SKalle Valo 			goto out_unlock;
4719e705c121SKalle Valo 
4720650cadb7SGregory Greenman 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4721e705c121SKalle Valo 	}
4722e705c121SKalle Valo 
4723e705c121SKalle Valo schedule_time_event:
4724e705c121SKalle Valo 	/* Schedule the time events */
4725e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4726e705c121SKalle Valo 
4727e705c121SKalle Valo out_unlock:
4728e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4729e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4730e705c121SKalle Valo 	return ret;
4731e705c121SKalle Valo }
4732e705c121SKalle Valo 
4733fe8b2ad3SMiri Korenblit int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
47345db4c4b9SEmmanuel Grumbach 		       struct ieee80211_vif *vif)
4735e705c121SKalle Valo {
4736e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4737e705c121SKalle Valo 
4738e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4739e705c121SKalle Valo 
4740e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4741fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
4742e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4743e705c121SKalle Valo 
4744e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4745e705c121SKalle Valo 	return 0;
4746e705c121SKalle Valo }
4747e705c121SKalle Valo 
4748b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
4749b73f9a4aSJohannes Berg 	bool responder;
4750b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
4751b73f9a4aSJohannes Berg };
4752b73f9a4aSJohannes Berg 
4753b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4754b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
4755b73f9a4aSJohannes Berg {
4756b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4757b73f9a4aSJohannes Berg 
4758d0a9123eSJohannes Berg 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4759b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4760b73f9a4aSJohannes Berg 		data->responder = true;
4761b73f9a4aSJohannes Berg }
4762b73f9a4aSJohannes Berg 
4763b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4764b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
4765b73f9a4aSJohannes Berg {
4766b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
4767b73f9a4aSJohannes Berg 		.responder = false,
4768b73f9a4aSJohannes Berg 		.ctx = ctx,
4769b73f9a4aSJohannes Berg 	};
4770b73f9a4aSJohannes Berg 
4771b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4772b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
4773b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
4774b73f9a4aSJohannes Berg 					&data);
4775b73f9a4aSJohannes Berg 	return data.responder;
4776b73f9a4aSJohannes Berg }
4777b73f9a4aSJohannes Berg 
4778e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4779e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
4780e705c121SKalle Valo {
4781e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4782e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
47833d66848fSShaul Triebitz 	bool use_def = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx) ||
47843d66848fSShaul Triebitz 		iwl_mvm_enable_fils(mvm, ctx);
47853d66848fSShaul Triebitz 	struct cfg80211_chan_def *def = use_def ? &ctx->def : &ctx->min_def;
4786e705c121SKalle Valo 	int ret;
4787e705c121SKalle Valo 
4788e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4789e705c121SKalle Valo 
4790e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4791e705c121SKalle Valo 
4792e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4793e705c121SKalle Valo 	if (!phy_ctxt) {
4794e705c121SKalle Valo 		ret = -ENOSPC;
4795e705c121SKalle Valo 		goto out;
4796e705c121SKalle Valo 	}
4797e705c121SKalle Valo 
4798b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4799e705c121SKalle Valo 				       ctx->rx_chains_static,
4800e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
4801e705c121SKalle Valo 	if (ret) {
4802e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4803e705c121SKalle Valo 		goto out;
4804e705c121SKalle Valo 	}
4805e705c121SKalle Valo 
4806e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4807e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4808e705c121SKalle Valo out:
4809e705c121SKalle Valo 	return ret;
4810e705c121SKalle Valo }
4811e705c121SKalle Valo 
4812cbce62a3SMiri Korenblit int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4813e705c121SKalle Valo 			struct ieee80211_chanctx_conf *ctx)
4814e705c121SKalle Valo {
4815e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4816e705c121SKalle Valo 	int ret;
4817e705c121SKalle Valo 
4818e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4819e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4820e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4821e705c121SKalle Valo 
4822e705c121SKalle Valo 	return ret;
4823e705c121SKalle Valo }
4824e705c121SKalle Valo 
4825e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4826e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4827e705c121SKalle Valo {
4828e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4829e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4830e705c121SKalle Valo 
4831e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4832e705c121SKalle Valo 
4833e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4834e705c121SKalle Valo }
4835e705c121SKalle Valo 
4836cbce62a3SMiri Korenblit void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4837e705c121SKalle Valo 			    struct ieee80211_chanctx_conf *ctx)
4838e705c121SKalle Valo {
4839e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4840e705c121SKalle Valo 
4841e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4842e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4843e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4844e705c121SKalle Valo }
4845e705c121SKalle Valo 
4846cbce62a3SMiri Korenblit void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4847cbce62a3SMiri Korenblit 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
4848e705c121SKalle Valo {
4849e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4850e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4851e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
48523d66848fSShaul Triebitz 	bool use_def = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx) ||
48533d66848fSShaul Triebitz 		iwl_mvm_enable_fils(mvm, ctx);
48543d66848fSShaul Triebitz 	struct cfg80211_chan_def *def = use_def ? &ctx->def : &ctx->min_def;
4855e705c121SKalle Valo 
4856e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4857e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4858e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4859e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4860e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4861e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4862e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4863e705c121SKalle Valo 		return;
4864e705c121SKalle Valo 
4865e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
48667a20bcceSEmmanuel Grumbach 
48677a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
48687a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4869b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
48707a20bcceSEmmanuel Grumbach 			goto out_unlock;
48717a20bcceSEmmanuel Grumbach 
48727a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
48737a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4874b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
48757a20bcceSEmmanuel Grumbach 			goto out_unlock;
48767a20bcceSEmmanuel Grumbach 	}
48777a20bcceSEmmanuel Grumbach 
4878e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4879b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4880e705c121SKalle Valo 				 ctx->rx_chains_static,
4881e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
48827a20bcceSEmmanuel Grumbach 
48837a20bcceSEmmanuel Grumbach out_unlock:
4884e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4885e705c121SKalle Valo }
4886e705c121SKalle Valo 
48878a919a78SMiri Korenblit /*
48888a919a78SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
48898a919a78SMiri Korenblit  *
48908a919a78SMiri Korenblit  * Returns true if we're done assigning the chanctx
48918a919a78SMiri Korenblit  * (either on failure or success)
48928a919a78SMiri Korenblit  */
48934263ac7fSGregory Greenman static bool
48944263ac7fSGregory Greenman __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
4895e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4896e705c121SKalle Valo 				    struct ieee80211_chanctx_conf *ctx,
489750e81437SMiri Korenblit 				    bool switching_chanctx, int *ret)
4898e705c121SKalle Valo {
4899e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4900e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4901e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4902e705c121SKalle Valo 
4903e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4904e705c121SKalle Valo 
4905650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = phy_ctxt;
4906e705c121SKalle Valo 
4907e705c121SKalle Valo 	switch (vif->type) {
4908e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4909e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4910e705c121SKalle Valo 		if (switching_chanctx) {
4911e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4912e705c121SKalle Valo 			break;
4913e705c121SKalle Valo 		}
49145a2abdcaSGustavo A. R. Silva 		fallthrough;
4915e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4916e705c121SKalle Valo 		/*
4917e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4918e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4919e705c121SKalle Valo 		 */
49208a919a78SMiri Korenblit 		*ret = 0;
49218a919a78SMiri Korenblit 		return true;
4922e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4923e705c121SKalle Valo 		break;
4924e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4925e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4926e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4927e705c121SKalle Valo 		break;
4928e705c121SKalle Valo 	default:
49298a919a78SMiri Korenblit 		*ret = -EINVAL;
49308a919a78SMiri Korenblit 		return true;
4931e705c121SKalle Valo 	}
49328a919a78SMiri Korenblit 	return false;
49338a919a78SMiri Korenblit }
49348a919a78SMiri Korenblit 
49358a919a78SMiri Korenblit static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
49368a919a78SMiri Korenblit 					struct ieee80211_vif *vif,
49374263ac7fSGregory Greenman 					struct ieee80211_bss_conf *link_conf,
49388a919a78SMiri Korenblit 					struct ieee80211_chanctx_conf *ctx,
49398a919a78SMiri Korenblit 					bool switching_chanctx)
49408a919a78SMiri Korenblit {
49418a919a78SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
49428a919a78SMiri Korenblit 	int ret;
49438a919a78SMiri Korenblit 
49444263ac7fSGregory Greenman 	if (WARN_ON(!link_conf))
49454263ac7fSGregory Greenman 		return -EINVAL;
49464263ac7fSGregory Greenman 
49478a919a78SMiri Korenblit 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
49488a919a78SMiri Korenblit 						switching_chanctx, &ret))
49498a919a78SMiri Korenblit 		goto out;
4950e705c121SKalle Valo 
4951e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4952e705c121SKalle Valo 	if (ret)
4953e705c121SKalle Valo 		goto out;
4954e705c121SKalle Valo 
4955e705c121SKalle Valo 	/*
4956e705c121SKalle Valo 	 * Power state must be updated before quotas,
4957e705c121SKalle Valo 	 * otherwise fw will complain.
4958e705c121SKalle Valo 	 */
4959e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4960e705c121SKalle Valo 
4961e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4962e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4963e705c121SKalle Valo 	 * will handle quota settings.
4964e705c121SKalle Valo 	 */
4965e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4966e705c121SKalle Valo 		mvmvif->monitor_active = true;
4967e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4968e705c121SKalle Valo 		if (ret)
4969e705c121SKalle Valo 			goto out_remove_binding;
49700e39eb03SChaya Rachel Ivgi 
49710e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
49720e39eb03SChaya Rachel Ivgi 		if (ret)
49730e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
49740e39eb03SChaya Rachel Ivgi 
4975e705c121SKalle Valo 	}
4976e705c121SKalle Valo 
4977e705c121SKalle Valo 	/* Handle binding during CSA */
4978e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4979e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4980e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4981e705c121SKalle Valo 	}
4982e705c121SKalle Valo 
49838a919a78SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION) {
49848a919a78SMiri Korenblit 		if (!switching_chanctx) {
49858a919a78SMiri Korenblit 			mvmvif->csa_bcn_pending = false;
49868a919a78SMiri Korenblit 			goto out;
49878a919a78SMiri Korenblit 		}
49888a919a78SMiri Korenblit 
498974a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
499074a10252SSara Sharon 
499174a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
499274a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
499311d0d831SJohannes Berg 			u32 duration = 5 * vif->bss_conf.beacon_int;
4994e705c121SKalle Valo 
4995e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4996e705c121SKalle Valo 			 * beacon on the new channel.
4997e705c121SKalle Valo 			 */
4998e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4999e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
5000e705c121SKalle Valo 						true);
500174a10252SSara Sharon 		}
5002e705c121SKalle Valo 
5003e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
5004e705c121SKalle Valo 	}
5005e705c121SKalle Valo 
5006e705c121SKalle Valo 	goto out;
5007e705c121SKalle Valo 
5008e705c121SKalle Valo out_remove_binding:
5009e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
5010e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5011e705c121SKalle Valo out:
5012e705c121SKalle Valo 	if (ret)
5013650cadb7SGregory Greenman 		mvmvif->deflink.phy_ctxt = NULL;
5014e705c121SKalle Valo 	return ret;
5015e705c121SKalle Valo }
50168a919a78SMiri Korenblit 
5017e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
5018e705c121SKalle Valo 				      struct ieee80211_vif *vif,
5019727eff4dSGregory Greenman 				      struct ieee80211_bss_conf *link_conf,
5020e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
5021e705c121SKalle Valo {
5022e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5023e705c121SKalle Valo 	int ret;
5024e705c121SKalle Valo 
5025e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
50264263ac7fSGregory Greenman 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5027e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5028e705c121SKalle Valo 
5029e705c121SKalle Valo 	return ret;
5030e705c121SKalle Valo }
5031e705c121SKalle Valo 
5032daddfae5SMiri Korenblit /*
5033daddfae5SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
5034daddfae5SMiri Korenblit  *
5035daddfae5SMiri Korenblit  * Returns if chanctx unassign chanctx is done
5036daddfae5SMiri Korenblit  * (either on failure or success)
5037daddfae5SMiri Korenblit  */
50384263ac7fSGregory Greenman static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
5039e705c121SKalle Valo 						  struct ieee80211_vif *vif,
5040e705c121SKalle Valo 						  bool switching_chanctx)
5041e705c121SKalle Valo {
5042e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5043e705c121SKalle Valo 
5044e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
5045650cadb7SGregory Greenman 	iwl_mvm_remove_time_event(mvm, mvmvif,
5046650cadb7SGregory Greenman 				  &mvmvif->time_event_data);
5047e705c121SKalle Valo 
5048e705c121SKalle Valo 	switch (vif->type) {
5049e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
5050daddfae5SMiri Korenblit 		return true;
5051e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
5052e705c121SKalle Valo 		mvmvif->monitor_active = false;
5053e705c121SKalle Valo 		mvmvif->ps_disabled = false;
5054e705c121SKalle Valo 		break;
5055e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5056e705c121SKalle Valo 		/* This part is triggered only during CSA */
5057e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
5058daddfae5SMiri Korenblit 			return true;
5059e705c121SKalle Valo 
5060e705c121SKalle Valo 		mvmvif->csa_countdown = false;
5061e705c121SKalle Valo 
5062e705c121SKalle Valo 		/* Set CS bit on all the stations */
5063e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5064e705c121SKalle Valo 
5065e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
5066e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5067e705c121SKalle Valo 
5068e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
5069e705c121SKalle Valo 		break;
5070daddfae5SMiri Korenblit 	default:
5071e705c121SKalle Valo 		break;
5072daddfae5SMiri Korenblit 	}
5073daddfae5SMiri Korenblit 	return false;
5074daddfae5SMiri Korenblit }
5075e705c121SKalle Valo 
5076e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5077e705c121SKalle Valo 					   struct ieee80211_vif *vif,
50784263ac7fSGregory Greenman 					   struct ieee80211_bss_conf *link_conf,
5079e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
5080e705c121SKalle Valo 					   bool switching_chanctx)
5081e705c121SKalle Valo {
5082e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5083e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
5084e705c121SKalle Valo 
5085daddfae5SMiri Korenblit 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5086e705c121SKalle Valo 		goto out;
5087daddfae5SMiri Korenblit 
5088daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_MONITOR)
5089e705c121SKalle Valo 		iwl_mvm_rm_snif_sta(mvm, vif);
5090e705c121SKalle Valo 
5091e705c121SKalle Valo 
5092daddfae5SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5093e705c121SKalle Valo 		disabled_vif = vif;
509474a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
509574a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5096e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5097e705c121SKalle Valo 	}
5098e705c121SKalle Valo 
5099e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5100e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
5101e705c121SKalle Valo 
5102e705c121SKalle Valo out:
5103bf544e9aSSara Sharon 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5104bf544e9aSSara Sharon 	    switching_chanctx)
5105bf544e9aSSara Sharon 		return;
5106650cadb7SGregory Greenman 	mvmvif->deflink.phy_ctxt = NULL;
5107e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
5108e705c121SKalle Valo }
5109e705c121SKalle Valo 
5110e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5111e705c121SKalle Valo 					 struct ieee80211_vif *vif,
5112727eff4dSGregory Greenman 					 struct ieee80211_bss_conf *link_conf,
5113e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
5114e705c121SKalle Valo {
5115e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5116e705c121SKalle Valo 
5117e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
51184263ac7fSGregory Greenman 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5119e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5120e705c121SKalle Valo }
5121e705c121SKalle Valo 
5122e705c121SKalle Valo static int
5123e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5124660eba5aSMiri Korenblit 				struct ieee80211_vif_chanctx_switch *vifs,
5125a2906ea6SJohannes Berg 				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5126e705c121SKalle Valo {
5127e705c121SKalle Valo 	int ret;
5128e705c121SKalle Valo 
5129e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
51304263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51314263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5132e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5133e705c121SKalle Valo 
5134e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5135e705c121SKalle Valo 	if (ret) {
5136e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5137e705c121SKalle Valo 		goto out_reassign;
5138e705c121SKalle Valo 	}
5139e705c121SKalle Valo 
51404263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51414263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5142e705c121SKalle Valo 	if (ret) {
5143e705c121SKalle Valo 		IWL_ERR(mvm,
5144e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5145e705c121SKalle Valo 		goto out_remove;
5146e705c121SKalle Valo 	}
5147e705c121SKalle Valo 
5148e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
5149e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5150e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
5151e705c121SKalle Valo 
5152e705c121SKalle Valo 	goto out;
5153e705c121SKalle Valo 
5154e705c121SKalle Valo out_remove:
5155e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5156e705c121SKalle Valo 
5157e705c121SKalle Valo out_reassign:
5158e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5159e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5160e705c121SKalle Valo 		goto out_restart;
5161e705c121SKalle Valo 	}
5162e705c121SKalle Valo 
51634263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51644263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5165e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5166e705c121SKalle Valo 		goto out_restart;
5167e705c121SKalle Valo 	}
5168e705c121SKalle Valo 
5169e705c121SKalle Valo 	goto out;
5170e705c121SKalle Valo 
5171e705c121SKalle Valo out_restart:
5172e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5173e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5174e705c121SKalle Valo 
5175e705c121SKalle Valo out:
5176e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5177e705c121SKalle Valo 
5178e705c121SKalle Valo 	return ret;
5179e705c121SKalle Valo }
5180e705c121SKalle Valo 
5181e705c121SKalle Valo static int
5182e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5183660eba5aSMiri Korenblit 				    struct ieee80211_vif_chanctx_switch *vifs,
5184a2906ea6SJohannes Berg 				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5185e705c121SKalle Valo {
5186e705c121SKalle Valo 	int ret;
5187e705c121SKalle Valo 
5188e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
51894263ac7fSGregory Greenman 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51904263ac7fSGregory Greenman 				    vifs[0].old_ctx, true);
5191e705c121SKalle Valo 
51924263ac7fSGregory Greenman 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
51934263ac7fSGregory Greenman 					vifs[0].new_ctx, true);
5194e705c121SKalle Valo 	if (ret) {
5195e705c121SKalle Valo 		IWL_ERR(mvm,
5196e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
5197e705c121SKalle Valo 		goto out_reassign;
5198e705c121SKalle Valo 	}
5199e705c121SKalle Valo 
5200e705c121SKalle Valo 	goto out;
5201e705c121SKalle Valo 
5202e705c121SKalle Valo out_reassign:
52034263ac7fSGregory Greenman 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
52044263ac7fSGregory Greenman 				      vifs[0].old_ctx, true)) {
5205e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5206e705c121SKalle Valo 		goto out_restart;
5207e705c121SKalle Valo 	}
5208e705c121SKalle Valo 
5209e705c121SKalle Valo 	goto out;
5210e705c121SKalle Valo 
5211e705c121SKalle Valo out_restart:
5212e705c121SKalle Valo 	/* things keep failing, better restart the hw */
5213e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
5214e705c121SKalle Valo 
5215e705c121SKalle Valo out:
5216e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5217e705c121SKalle Valo 
5218e705c121SKalle Valo 	return ret;
5219e705c121SKalle Valo }
5220e705c121SKalle Valo 
5221660eba5aSMiri Korenblit /* Execute the common part for both MLD and non-MLD modes */
5222660eba5aSMiri Korenblit int
5223660eba5aSMiri Korenblit iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5224e705c121SKalle Valo 				  struct ieee80211_vif_chanctx_switch *vifs,
5225e705c121SKalle Valo 				  int n_vifs,
5226660eba5aSMiri Korenblit 				  enum ieee80211_chanctx_switch_mode mode,
5227a2906ea6SJohannes Berg 				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5228e705c121SKalle Valo {
5229e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5230e705c121SKalle Valo 	int ret;
5231e705c121SKalle Valo 
5232e705c121SKalle Valo 	/* we only support a single-vif right now */
5233e705c121SKalle Valo 	if (n_vifs > 1)
5234e705c121SKalle Valo 		return -EOPNOTSUPP;
5235e705c121SKalle Valo 
5236e705c121SKalle Valo 	switch (mode) {
5237e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5238660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5239e705c121SKalle Valo 		break;
5240e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
5241660eba5aSMiri Korenblit 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5242e705c121SKalle Valo 		break;
5243e705c121SKalle Valo 	default:
5244e705c121SKalle Valo 		ret = -EOPNOTSUPP;
5245e705c121SKalle Valo 		break;
5246e705c121SKalle Valo 	}
5247e705c121SKalle Valo 
5248e705c121SKalle Valo 	return ret;
5249e705c121SKalle Valo }
5250e705c121SKalle Valo 
5251660eba5aSMiri Korenblit static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5252660eba5aSMiri Korenblit 				      struct ieee80211_vif_chanctx_switch *vifs,
5253660eba5aSMiri Korenblit 				      int n_vifs,
5254660eba5aSMiri Korenblit 				      enum ieee80211_chanctx_switch_mode mode)
5255660eba5aSMiri Korenblit {
5256a2906ea6SJohannes Berg 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5257660eba5aSMiri Korenblit 		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5258660eba5aSMiri Korenblit 		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5259660eba5aSMiri Korenblit 	};
5260660eba5aSMiri Korenblit 
5261660eba5aSMiri Korenblit 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5262660eba5aSMiri Korenblit }
5263660eba5aSMiri Korenblit 
5264cbce62a3SMiri Korenblit int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
52652f0282dbSJohannes Berg {
52662f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
52672f0282dbSJohannes Berg 
52682f0282dbSJohannes Berg 	return mvm->ibss_manager;
52692f0282dbSJohannes Berg }
52702f0282dbSJohannes Berg 
5271cbce62a3SMiri Korenblit int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5272e705c121SKalle Valo 		    bool set)
5273e705c121SKalle Valo {
5274e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5275e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5276e705c121SKalle Valo 
5277e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
5278e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
5279e705c121SKalle Valo 		return -EINVAL;
5280e705c121SKalle Valo 	}
5281e705c121SKalle Valo 
528236cf5377SGregory Greenman 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
528336cf5377SGregory Greenman 					       &mvm_sta->vif->bss_conf);
5284e705c121SKalle Valo }
5285e705c121SKalle Valo 
5286e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
5287e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5288e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5289e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5290e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5291e705c121SKalle Valo };
5292e705c121SKalle Valo 
5293e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5294e705c121SKalle Valo 				      struct ieee80211_vif *vif,
5295e705c121SKalle Valo 				      void *data, int len)
5296e705c121SKalle Valo {
5297e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5298e705c121SKalle Valo 	int err;
5299e705c121SKalle Valo 	u32 noa_duration;
5300e705c121SKalle Valo 
53018cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
53028cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
5303e705c121SKalle Valo 	if (err)
5304e705c121SKalle Valo 		return err;
5305e705c121SKalle Valo 
5306e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
5307e705c121SKalle Valo 		return -EINVAL;
5308e705c121SKalle Valo 
5309e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5310e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
5311e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5312e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
5313e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5314e705c121SKalle Valo 			return -EINVAL;
5315e705c121SKalle Valo 
5316e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5317e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
5318e705c121SKalle Valo 			return -EINVAL;
5319e705c121SKalle Valo 
5320e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
5321e705c121SKalle Valo 		mvm->noa_vif = vif;
5322e705c121SKalle Valo 
532322b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
5324e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5325e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
5326e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5327f276e20bSJohannes Berg 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5328e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5329e705c121SKalle Valo 			return -EINVAL;
5330e705c121SKalle Valo 
5331e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
5332e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
5333e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
5334e705c121SKalle Valo 	}
5335e705c121SKalle Valo 
5336e705c121SKalle Valo 	return -EOPNOTSUPP;
5337e705c121SKalle Valo }
5338e705c121SKalle Valo 
5339cbce62a3SMiri Korenblit int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5340e705c121SKalle Valo 			     struct ieee80211_vif *vif,
5341e705c121SKalle Valo 			     void *data, int len)
5342e705c121SKalle Valo {
5343e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5344e705c121SKalle Valo 	int err;
5345e705c121SKalle Valo 
5346e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5347e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5348e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5349e705c121SKalle Valo 
5350e705c121SKalle Valo 	return err;
5351e705c121SKalle Valo }
5352e705c121SKalle Valo #endif
5353e705c121SKalle Valo 
5354cbce62a3SMiri Korenblit void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5355e705c121SKalle Valo 			    struct ieee80211_channel_switch *chsw)
5356e705c121SKalle Valo {
5357e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
5358e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
5359e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
5360e705c121SKalle Valo 	 * (which is when the absence time event starts).
5361e705c121SKalle Valo 	 */
5362e705c121SKalle Valo 
5363e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5364e705c121SKalle Valo 			   "dummy channel switch op\n");
5365e705c121SKalle Valo }
5366e705c121SKalle Valo 
536774a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
536874a10252SSara Sharon 				       struct ieee80211_vif *vif,
536974a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
537074a10252SSara Sharon {
537174a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
537274a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
537374a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
537474a10252SSara Sharon 							  mvmvif->color)),
537574a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
537674a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
537774a10252SSara Sharon 		.cs_count = chsw->count,
537877738865SSara Sharon 		.cs_mode = chsw->block_tx,
537974a10252SSara Sharon 	};
538074a10252SSara Sharon 
538174a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
538274a10252SSara Sharon 
53839cfcf71cSSara Sharon 	if (chsw->delay)
53849cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
53859cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
53869cfcf71cSSara Sharon 
538774a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
538874a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
538974a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
539074a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
539174a10252SSara Sharon }
539274a10252SSara Sharon 
53930202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
53940202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
53950202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
53960202bcf0SEmmanuel Grumbach {
53970202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
53980202bcf0SEmmanuel Grumbach 	u32 apply_time;
53990202bcf0SEmmanuel Grumbach 
54000202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
54010202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
54020202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
54030202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
54040202bcf0SEmmanuel Grumbach 	 * loss).
54050202bcf0SEmmanuel Grumbach 	 */
54060202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
54070202bcf0SEmmanuel Grumbach 		apply_time = 0;
54080202bcf0SEmmanuel Grumbach 	else
54090202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
54100202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
54110202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
54120202bcf0SEmmanuel Grumbach 
54130202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
54140202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
54150202bcf0SEmmanuel Grumbach 
54160202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
54170202bcf0SEmmanuel Grumbach 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
54180202bcf0SEmmanuel Grumbach 
54190202bcf0SEmmanuel Grumbach 		if (ret)
54200202bcf0SEmmanuel Grumbach 			return ret;
54210202bcf0SEmmanuel Grumbach 	}
54220202bcf0SEmmanuel Grumbach 
54230202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
54240202bcf0SEmmanuel Grumbach 				    apply_time);
54250202bcf0SEmmanuel Grumbach 
54260202bcf0SEmmanuel Grumbach 	return 0;
54270202bcf0SEmmanuel Grumbach }
54280202bcf0SEmmanuel Grumbach 
5429f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
5430cbce62a3SMiri Korenblit int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
5431e705c121SKalle Valo 			       struct ieee80211_vif *vif,
5432e705c121SKalle Valo 			       struct ieee80211_channel_switch *chsw)
5433e705c121SKalle Valo {
5434e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5435e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
5436e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5437e705c121SKalle Valo 	int ret;
5438e705c121SKalle Valo 
5439e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5440e705c121SKalle Valo 
5441e705c121SKalle Valo 	mvmvif->csa_failed = false;
5442e705c121SKalle Valo 
5443e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5444e705c121SKalle Valo 			   chsw->chandef.center_freq1);
5445e705c121SKalle Valo 
54467174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
54477174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
54487174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5449e705c121SKalle Valo 
5450e705c121SKalle Valo 	switch (vif->type) {
5451e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
5452e705c121SKalle Valo 		csa_vif =
5453e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
5454e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
5455d0a9123eSJohannes Berg 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5456e705c121SKalle Valo 			      "Another CSA is already in progress")) {
5457e705c121SKalle Valo 			ret = -EBUSY;
5458e705c121SKalle Valo 			goto out_unlock;
5459e705c121SKalle Valo 		}
5460e705c121SKalle Valo 
5461d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
5462d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5463d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
5464d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
5465d3a108a4SAndrei Otcheretianski 			goto out_unlock;
5466d3a108a4SAndrei Otcheretianski 		}
5467d3a108a4SAndrei Otcheretianski 
5468e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
5469e705c121SKalle Valo 
5470e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
5471e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
5472e705c121SKalle Valo 			ret = -EBUSY;
5473e705c121SKalle Valo 			goto out_unlock;
5474e705c121SKalle Valo 		}
5475e705c121SKalle Valo 
5476d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5477d3a108a4SAndrei Otcheretianski 
5478e705c121SKalle Valo 		break;
5479e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
54802360acbdSEmmanuel Grumbach 		/*
5481ad12b231SNathan Errera 		 * In the new flow FW is in charge of timing the switch so there
5482ad12b231SNathan Errera 		 * is no need for all of this
5483ad12b231SNathan Errera 		 */
5484ad12b231SNathan Errera 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5485ad12b231SNathan Errera 					    CHANNEL_SWITCH_ERROR_NOTIF,
5486ad12b231SNathan Errera 					    0))
5487ad12b231SNathan Errera 			break;
5488ad12b231SNathan Errera 
5489ad12b231SNathan Errera 		/*
54902360acbdSEmmanuel Grumbach 		 * We haven't configured the firmware to be associated yet since
54912360acbdSEmmanuel Grumbach 		 * we don't know the dtim period. In this case, the firmware can't
54922360acbdSEmmanuel Grumbach 		 * track the beacons.
54932360acbdSEmmanuel Grumbach 		 */
5494f276e20bSJohannes Berg 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period) {
54952360acbdSEmmanuel Grumbach 			ret = -EBUSY;
54962360acbdSEmmanuel Grumbach 			goto out_unlock;
54972360acbdSEmmanuel Grumbach 		}
54982360acbdSEmmanuel Grumbach 
5499aee2eac7SJohannes Berg 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX &&
5500aee2eac7SJohannes Berg 		    hweight16(vif->valid_links) <= 1)
550187d9564eSJohannes Berg 			schedule_delayed_work(&mvmvif->csa_work, 0);
550287d9564eSJohannes Berg 
5503f6780614SSara Sharon 		if (chsw->block_tx) {
5504f6780614SSara Sharon 			/*
5505f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
5506f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
5507f6780614SSara Sharon 			 */
5508f6780614SSara Sharon 			if (!chsw->count ||
5509f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
5510f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
5511f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
5512f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5513f6780614SSara Sharon 		}
5514e705c121SKalle Valo 
55150202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
55160202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
55170202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5518e705c121SKalle Valo 			if (ret)
5519e705c121SKalle Valo 				goto out_unlock;
55200202bcf0SEmmanuel Grumbach 		} else {
552174a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
55220202bcf0SEmmanuel Grumbach 		}
552381b4e44eSSara Sharon 
552481b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
552581b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
5526e705c121SKalle Valo 		break;
5527e705c121SKalle Valo 	default:
5528e705c121SKalle Valo 		break;
5529e705c121SKalle Valo 	}
5530e705c121SKalle Valo 
5531e705c121SKalle Valo 	mvmvif->ps_disabled = true;
5532e705c121SKalle Valo 
5533e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
5534e705c121SKalle Valo 	if (ret)
5535e705c121SKalle Valo 		goto out_unlock;
5536e705c121SKalle Valo 
5537e705c121SKalle Valo 	/* we won't be on this channel any longer */
5538e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
5539e705c121SKalle Valo 
5540e705c121SKalle Valo out_unlock:
5541e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5542e705c121SKalle Valo 
5543e705c121SKalle Valo 	return ret;
5544e705c121SKalle Valo }
5545e705c121SKalle Valo 
5546cbce62a3SMiri Korenblit void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5547c37763d2SSara Sharon 				      struct ieee80211_vif *vif,
5548c37763d2SSara Sharon 				      struct ieee80211_channel_switch *chsw)
5549e705c121SKalle Valo {
5550e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5551c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5552c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
5553c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5554c37763d2SSara Sharon 							  mvmvif->color)),
5555c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5556c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
5557c37763d2SSara Sharon 		.cs_count = chsw->count,
555877738865SSara Sharon 		.cs_mode = chsw->block_tx,
5559c37763d2SSara Sharon 	};
5560e705c121SKalle Valo 
5561ad12b231SNathan Errera 	/*
5562ad12b231SNathan Errera 	 * In the new flow FW is in charge of timing the switch so there is no
5563ad12b231SNathan Errera 	 * need for all of this
5564ad12b231SNathan Errera 	 */
5565ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5566ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5567ad12b231SNathan Errera 		return;
5568ad12b231SNathan Errera 
5569c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5570c37763d2SSara Sharon 		return;
5571e705c121SKalle Valo 
5572c3eae059SGregory Greenman 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5573c3eae059SGregory Greenman 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5574c3eae059SGregory Greenman 
557581b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
557681b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
557781b4e44eSSara Sharon 			/* Second time, give up on this AP*/
557881b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
5579a469a593SEmmanuel Grumbach 			ieee80211_chswitch_done(vif, false, 0);
558081b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
558181b4e44eSSara Sharon 			return;
5582e705c121SKalle Valo 		}
558381b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
5584e705c121SKalle Valo 	}
558581b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
5586e705c121SKalle Valo 
5587caf46377SSara Sharon 	mutex_lock(&mvm->mutex);
5588caf46377SSara Sharon 	if (mvmvif->csa_failed)
5589caf46377SSara Sharon 		goto out_unlock;
5590e705c121SKalle Valo 
5591c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5592c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
5593c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5594caf46377SSara Sharon 				     0, sizeof(cmd), &cmd));
5595caf46377SSara Sharon out_unlock:
5596caf46377SSara Sharon 	mutex_unlock(&mvm->mutex);
5597e705c121SKalle Valo }
5598e705c121SKalle Valo 
55996110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
56006110d9e5SDavid Spinadel {
5601435d0827SSara Sharon 	int i;
5602435d0827SSara Sharon 
560306195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5604309c4848SLuca Coelho 		if (drop) {
5605309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
56066110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
5607d4e3a341SMordechay Goodstein 				iwl_mvm_flushable_queues(mvm) & queues);
5608309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
5609309c4848SLuca Coelho 		} else {
5610435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5611309c4848SLuca Coelho 		}
5612435d0827SSara Sharon 		return;
5613435d0827SSara Sharon 	}
56146110d9e5SDavid Spinadel 
56156110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
5616be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5617435d0827SSara Sharon 		struct ieee80211_sta *sta;
5618435d0827SSara Sharon 
5619435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
56206110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
56216110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
56226110d9e5SDavid Spinadel 			continue;
56236110d9e5SDavid Spinadel 
562406195639SSara Sharon 		if (drop)
5625d4e3a341SMordechay Goodstein 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
562606195639SSara Sharon 		else
56276110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
56286110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
56296110d9e5SDavid Spinadel 	}
56306110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
56316110d9e5SDavid Spinadel }
56326110d9e5SDavid Spinadel 
5633cbce62a3SMiri Korenblit void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5634cbce62a3SMiri Korenblit 		       u32 queues, bool drop)
5635e705c121SKalle Valo {
5636e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5637e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
5638e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
5639e705c121SKalle Valo 	struct ieee80211_sta *sta;
5640d3f9cd61SJohannes Berg 	bool ap_sta_done = false;
5641e705c121SKalle Valo 	int i;
5642e705c121SKalle Valo 	u32 msk = 0;
5643e705c121SKalle Valo 
56446110d9e5SDavid Spinadel 	if (!vif) {
56456110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
56466110d9e5SDavid Spinadel 		return;
56476110d9e5SDavid Spinadel 	}
56486110d9e5SDavid Spinadel 
564924afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
565024afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
565124afba76SLiad Kaufman 
5652e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5653e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
5654e705c121SKalle Valo 
5655e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
5656be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5657e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5658e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
5659e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
5660e705c121SKalle Valo 			continue;
5661e705c121SKalle Valo 
5662e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5663e705c121SKalle Valo 		if (mvmsta->vif != vif)
5664e705c121SKalle Valo 			continue;
5665e705c121SKalle Valo 
5666d3f9cd61SJohannes Berg 		if (sta == mvmvif->ap_sta) {
5667d3f9cd61SJohannes Berg 			if (ap_sta_done)
5668d3f9cd61SJohannes Berg 				continue;
5669d3f9cd61SJohannes Berg 			ap_sta_done = true;
5670d3f9cd61SJohannes Berg 		}
5671d3f9cd61SJohannes Berg 
5672d49394a1SSara Sharon 		if (drop) {
5673f9084775SNathan Errera 			if (iwl_mvm_flush_sta(mvm, mvmsta, false))
5674d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
5675d49394a1SSara Sharon 		} else {
5676d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
5677d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5678f7bd883bSJohannes Berg 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5679f7bd883bSJohannes Berg 				msk |= mvmsta->tfd_queue_msk;
5680e705c121SKalle Valo 		}
5681d49394a1SSara Sharon 	}
5682e705c121SKalle Valo 
5683e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5684e705c121SKalle Valo 
5685e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
5686e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
5687e705c121SKalle Valo 	 */
5688d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5689a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5690e705c121SKalle Valo }
5691e705c121SKalle Valo 
5692a6cc6ccbSJohannes Berg void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5693a6cc6ccbSJohannes Berg 			   struct ieee80211_sta *sta)
5694a6cc6ccbSJohannes Berg {
5695a6cc6ccbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5696a6cc6ccbSJohannes Berg 	int i;
5697a6cc6ccbSJohannes Berg 
5698a6cc6ccbSJohannes Berg 	mutex_lock(&mvm->mutex);
5699a6cc6ccbSJohannes Berg 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5700a6cc6ccbSJohannes Berg 		struct iwl_mvm_sta *mvmsta;
5701a6cc6ccbSJohannes Berg 		struct ieee80211_sta *tmp;
5702a6cc6ccbSJohannes Berg 
5703a6cc6ccbSJohannes Berg 		tmp = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5704a6cc6ccbSJohannes Berg 						lockdep_is_held(&mvm->mutex));
5705a6cc6ccbSJohannes Berg 		if (tmp != sta)
5706a6cc6ccbSJohannes Berg 			continue;
5707a6cc6ccbSJohannes Berg 
5708a6cc6ccbSJohannes Berg 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5709a6cc6ccbSJohannes Berg 
5710a6cc6ccbSJohannes Berg 		if (iwl_mvm_flush_sta(mvm, mvmsta, false))
5711a6cc6ccbSJohannes Berg 			IWL_ERR(mvm, "flush request fail\n");
5712a6cc6ccbSJohannes Berg 	}
5713a6cc6ccbSJohannes Berg 	mutex_unlock(&mvm->mutex);
5714a6cc6ccbSJohannes Berg }
5715a6cc6ccbSJohannes Berg 
5716cbce62a3SMiri Korenblit int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5717e705c121SKalle Valo 			   struct survey_info *survey)
5718e705c121SKalle Valo {
5719e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5720e705c121SKalle Valo 	int ret;
5721e705c121SKalle Valo 
5722e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
5723e705c121SKalle Valo 
5724e705c121SKalle Valo 	/* only support global statistics right now */
5725e705c121SKalle Valo 	if (idx != 0)
5726e705c121SKalle Valo 		return -ENOENT;
5727e705c121SKalle Valo 
5728280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
5729e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
5730e705c121SKalle Valo 		return -ENOENT;
5731e705c121SKalle Valo 
5732e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5733e705c121SKalle Valo 
5734aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
5735e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
5736e705c121SKalle Valo 		if (ret)
5737e705c121SKalle Valo 			goto out;
5738e705c121SKalle Valo 	}
5739e705c121SKalle Valo 
5740e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
5741e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
5742e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
5743e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
5744e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
5745e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
5746e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
5747e705c121SKalle Valo 
5748e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
5749e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
5750e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
5751e705c121SKalle Valo 
5752e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
5753e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
5754e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
5755e705c121SKalle Valo 
5756e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
5757e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
5758e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
5759e705c121SKalle Valo 
5760e705c121SKalle Valo 	ret = 0;
5761e705c121SKalle Valo  out:
5762e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5763e705c121SKalle Valo 	return ret;
5764e705c121SKalle Valo }
5765e705c121SKalle Valo 
5766ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
5767ed780545SJohannes Berg {
576882cdbd11SMiri Korenblit 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
57697138763eSJohannes Berg 	u32 gi_ltf;
577082cdbd11SMiri Korenblit 
577182cdbd11SMiri Korenblit 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
5772ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
5773ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
5774ed780545SJohannes Berg 		break;
5775ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
5776ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
5777ed780545SJohannes Berg 		break;
5778ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
5779ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
5780ed780545SJohannes Berg 		break;
5781ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
5782ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
5783ed780545SJohannes Berg 		break;
578423dcee94SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_320:
578523dcee94SJohannes Berg 		rinfo->bw = RATE_INFO_BW_320;
578623dcee94SJohannes Berg 		break;
5787ed780545SJohannes Berg 	}
5788ed780545SJohannes Berg 
578982cdbd11SMiri Korenblit 	if (format == RATE_MCS_CCK_MSK ||
579082cdbd11SMiri Korenblit 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
579182cdbd11SMiri Korenblit 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
579282cdbd11SMiri Korenblit 
579382cdbd11SMiri Korenblit 		/* add the offset needed to get to the legacy ofdm indices */
579482cdbd11SMiri Korenblit 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
579582cdbd11SMiri Korenblit 			rate += IWL_FIRST_OFDM_RATE;
579682cdbd11SMiri Korenblit 
579782cdbd11SMiri Korenblit 		switch (rate) {
579882cdbd11SMiri Korenblit 		case IWL_RATE_1M_INDEX:
579982cdbd11SMiri Korenblit 			rinfo->legacy = 10;
580082cdbd11SMiri Korenblit 			break;
580182cdbd11SMiri Korenblit 		case IWL_RATE_2M_INDEX:
580282cdbd11SMiri Korenblit 			rinfo->legacy = 20;
580382cdbd11SMiri Korenblit 			break;
580482cdbd11SMiri Korenblit 		case IWL_RATE_5M_INDEX:
580582cdbd11SMiri Korenblit 			rinfo->legacy = 55;
580682cdbd11SMiri Korenblit 			break;
580782cdbd11SMiri Korenblit 		case IWL_RATE_11M_INDEX:
580882cdbd11SMiri Korenblit 			rinfo->legacy = 110;
580982cdbd11SMiri Korenblit 			break;
581082cdbd11SMiri Korenblit 		case IWL_RATE_6M_INDEX:
581182cdbd11SMiri Korenblit 			rinfo->legacy = 60;
581282cdbd11SMiri Korenblit 			break;
581382cdbd11SMiri Korenblit 		case IWL_RATE_9M_INDEX:
581482cdbd11SMiri Korenblit 			rinfo->legacy = 90;
581582cdbd11SMiri Korenblit 			break;
581682cdbd11SMiri Korenblit 		case IWL_RATE_12M_INDEX:
581782cdbd11SMiri Korenblit 			rinfo->legacy = 120;
581882cdbd11SMiri Korenblit 			break;
581982cdbd11SMiri Korenblit 		case IWL_RATE_18M_INDEX:
582082cdbd11SMiri Korenblit 			rinfo->legacy = 180;
582182cdbd11SMiri Korenblit 			break;
582282cdbd11SMiri Korenblit 		case IWL_RATE_24M_INDEX:
582382cdbd11SMiri Korenblit 			rinfo->legacy = 240;
582482cdbd11SMiri Korenblit 			break;
582582cdbd11SMiri Korenblit 		case IWL_RATE_36M_INDEX:
582682cdbd11SMiri Korenblit 			rinfo->legacy = 360;
582782cdbd11SMiri Korenblit 			break;
582882cdbd11SMiri Korenblit 		case IWL_RATE_48M_INDEX:
582982cdbd11SMiri Korenblit 			rinfo->legacy = 480;
583082cdbd11SMiri Korenblit 			break;
583182cdbd11SMiri Korenblit 		case IWL_RATE_54M_INDEX:
583282cdbd11SMiri Korenblit 			rinfo->legacy = 540;
583382cdbd11SMiri Korenblit 		}
583482cdbd11SMiri Korenblit 		return;
583582cdbd11SMiri Korenblit 	}
583682cdbd11SMiri Korenblit 
5837ed780545SJohannes Berg 	rinfo->nss = u32_get_bits(rate_n_flags,
583882cdbd11SMiri Korenblit 				  RATE_MCS_NSS_MSK) + 1;
583982cdbd11SMiri Korenblit 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
584082cdbd11SMiri Korenblit 		RATE_HT_MCS_INDEX(rate_n_flags) :
584182cdbd11SMiri Korenblit 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
584282cdbd11SMiri Korenblit 
58437138763eSJohannes Berg 	if (rate_n_flags & RATE_MCS_SGI_MSK)
58447138763eSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
58457138763eSJohannes Berg 
58467138763eSJohannes Berg 	switch (format) {
584723dcee94SJohannes Berg 	case RATE_MCS_EHT_MSK:
584823dcee94SJohannes Berg 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
584923dcee94SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
585023dcee94SJohannes Berg 		break;
58517138763eSJohannes Berg 	case RATE_MCS_HE_MSK:
58527138763eSJohannes Berg 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
5853ed780545SJohannes Berg 
5854ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
5855ed780545SJohannes Berg 
585682cdbd11SMiri Korenblit 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
5857ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
5858ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
5859ed780545SJohannes Berg 		}
5860ed780545SJohannes Berg 
586182cdbd11SMiri Korenblit 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
586282cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_SU:
586382cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_EXT_SU:
5864ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5865ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5866ed780545SJohannes Berg 			else if (gi_ltf == 2)
5867ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
586882cdbd11SMiri Korenblit 			else if (gi_ltf == 3)
5869ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
587082cdbd11SMiri Korenblit 			else
587182cdbd11SMiri Korenblit 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5872ed780545SJohannes Berg 			break;
587382cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_MU:
5874ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5875ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5876ed780545SJohannes Berg 			else if (gi_ltf == 2)
5877ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5878ed780545SJohannes Berg 			else
5879ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5880ed780545SJohannes Berg 			break;
588182cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_TRIG:
5882ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5883ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5884ed780545SJohannes Berg 			else
5885ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5886ed780545SJohannes Berg 			break;
5887ed780545SJohannes Berg 		}
5888ed780545SJohannes Berg 
5889ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
5890ed780545SJohannes Berg 			rinfo->he_dcm = 1;
58917138763eSJohannes Berg 		break;
58927138763eSJohannes Berg 	case RATE_MCS_HT_MSK:
589382cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
58947138763eSJohannes Berg 		break;
58957138763eSJohannes Berg 	case RATE_MCS_VHT_MSK:
589682cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
58977138763eSJohannes Berg 		break;
5898ed780545SJohannes Berg 	}
5899ed780545SJohannes Berg }
5900ed780545SJohannes Berg 
5901cbce62a3SMiri Korenblit void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
5902e705c121SKalle Valo 				struct ieee80211_vif *vif,
5903e705c121SKalle Valo 				struct ieee80211_sta *sta,
5904e705c121SKalle Valo 				struct station_info *sinfo)
5905e705c121SKalle Valo {
5906e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5907e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5908e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5909e705c121SKalle Valo 
5910c8ee33e1SGregory Greenman 	if (mvmsta->deflink.avg_energy) {
5911c8ee33e1SGregory Greenman 		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
591222d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
5913988b5968SSara Sharon 	}
5914988b5968SSara Sharon 
5915ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
5916c8ee33e1SGregory Greenman 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
5917ed780545SJohannes Berg 
5918ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5919ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5920ed780545SJohannes Berg 	}
5921ed780545SJohannes Berg 
5922e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
5923e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5924e705c121SKalle Valo 		return;
5925e705c121SKalle Valo 
5926f276e20bSJohannes Berg 	if (!vif->cfg.assoc)
5927e705c121SKalle Valo 		return;
5928e705c121SKalle Valo 
5929e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5930e705c121SKalle Valo 
5931c8ee33e1SGregory Greenman 	if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
5932e705c121SKalle Valo 		goto unlock;
5933e705c121SKalle Valo 
5934e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
5935e705c121SKalle Valo 		goto unlock;
5936e705c121SKalle Valo 
5937650cadb7SGregory Greenman 	sinfo->rx_beacon = mvmvif->deflink.beacon_stats.num_beacons +
5938650cadb7SGregory Greenman 			   mvmvif->deflink.beacon_stats.accu_num_beacons;
593922d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
5940650cadb7SGregory Greenman 	if (mvmvif->deflink.beacon_stats.avg_signal) {
5941e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
5942650cadb7SGregory Greenman 		sinfo->rx_beacon_signal_avg =
5943650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.avg_signal;
594422d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
5945e705c121SKalle Valo 	}
5946e705c121SKalle Valo  unlock:
5947e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5948e705c121SKalle Valo }
5949e705c121SKalle Valo 
5950119c2a13SMordechay Goodstein static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5951119c2a13SMordechay Goodstein 					    struct ieee80211_vif *vif,
5952119c2a13SMordechay Goodstein 					    const  struct ieee80211_mlme_event *mlme)
5953119c2a13SMordechay Goodstein {
59541a81bddfSMordechay Goodstein 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
59551a81bddfSMordechay Goodstein 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
5956119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5957119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5958119c2a13SMordechay Goodstein 				       NULL);
5959119c2a13SMordechay Goodstein 		return;
5960119c2a13SMordechay Goodstein 	}
5961119c2a13SMordechay Goodstein 
5962119c2a13SMordechay Goodstein 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5963119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5964119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_DEASSOC,
5965119c2a13SMordechay Goodstein 				       NULL);
5966119c2a13SMordechay Goodstein 		return;
5967119c2a13SMordechay Goodstein 	}
5968119c2a13SMordechay Goodstein }
5969119c2a13SMordechay Goodstein 
5970e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5971e705c121SKalle Valo 					struct ieee80211_vif *vif,
5972e705c121SKalle Valo 					const struct ieee80211_event *event)
5973e705c121SKalle Valo {
59744c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5975e705c121SKalle Valo 	do {								\
59764c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5977e705c121SKalle Valo 			break;						\
59787174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5979e705c121SKalle Valo 	} while (0)
5980e705c121SKalle Valo 
5981e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5982e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5983e705c121SKalle Valo 
5984119c2a13SMordechay Goodstein 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5985119c2a13SMordechay Goodstein 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5986119c2a13SMordechay Goodstein 		return;
5987119c2a13SMordechay Goodstein 	}
5988119c2a13SMordechay Goodstein 
59896c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
59906c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
59916c042d75SSara Sharon 	if (!trig)
5992e705c121SKalle Valo 		return;
5993e705c121SKalle Valo 
5994e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
5995e705c121SKalle Valo 
5996e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
5997e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
59984c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
5999e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
6000e705c121SKalle Valo 					    event->u.mlme.reason);
6001e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
60024c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
6003e705c121SKalle Valo 					   "ASSOC TIMEOUT");
6004e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
6005e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
60064c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
6007e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
6008e705c121SKalle Valo 					   event->u.mlme.reason);
6009e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
60104c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
6011e705c121SKalle Valo 					   "AUTH TIMEOUT");
6012e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
60134c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
6014e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
6015e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
60164c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
6017e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
6018e705c121SKalle Valo 	}
6019e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
6020e705c121SKalle Valo }
6021e705c121SKalle Valo 
6022e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
6023e705c121SKalle Valo 					  struct ieee80211_vif *vif,
6024e705c121SKalle Valo 					  const struct ieee80211_event *event)
6025e705c121SKalle Valo {
6026e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
6027e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
6028e705c121SKalle Valo 
60296c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
60306c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
60316c042d75SSara Sharon 	if (!trig)
6032e705c121SKalle Valo 		return;
6033e705c121SKalle Valo 
6034e705c121SKalle Valo 	ba_trig = (void *)trig->data;
6035e705c121SKalle Valo 
6036e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
6037e705c121SKalle Valo 		return;
6038e705c121SKalle Valo 
60397174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
6040e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
6041e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
6042e705c121SKalle Valo 				event->u.ba.ssn);
6043e705c121SKalle Valo }
6044e705c121SKalle Valo 
6045cbce62a3SMiri Korenblit void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
6046e705c121SKalle Valo 				struct ieee80211_vif *vif,
6047e705c121SKalle Valo 				const struct ieee80211_event *event)
6048e705c121SKalle Valo {
6049e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6050e705c121SKalle Valo 
6051e705c121SKalle Valo 	switch (event->type) {
6052e705c121SKalle Valo 	case MLME_EVENT:
6053e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
6054e705c121SKalle Valo 		break;
6055e705c121SKalle Valo 	case BAR_RX_EVENT:
6056e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
6057e705c121SKalle Valo 		break;
6058e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
6059528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
6060528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
6061e705c121SKalle Valo 		break;
6062e705c121SKalle Valo 	default:
6063e705c121SKalle Valo 		break;
6064e705c121SKalle Valo 	}
6065e705c121SKalle Valo }
6066e705c121SKalle Valo 
6067d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
60685e1688ceSJohannes Berg 				     enum iwl_mvm_rxq_notif_type type,
60695e1688ceSJohannes Berg 				     bool sync,
60705e1688ceSJohannes Berg 				     const void *data, u32 size)
60710636b938SSara Sharon {
60725e1688ceSJohannes Berg 	struct {
60735e1688ceSJohannes Berg 		struct iwl_rxq_sync_cmd cmd;
60745e1688ceSJohannes Berg 		struct iwl_mvm_internal_rxq_notif notif;
60755e1688ceSJohannes Berg 	} __packed cmd = {
60765e1688ceSJohannes Berg 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
60775e1688ceSJohannes Berg 		.cmd.count =
60785e1688ceSJohannes Berg 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
60795e1688ceSJohannes Berg 				    size),
60805e1688ceSJohannes Berg 		.notif.type = type,
60815e1688ceSJohannes Berg 		.notif.sync = sync,
60825e1688ceSJohannes Berg 	};
60835e1688ceSJohannes Berg 	struct iwl_host_cmd hcmd = {
60845e1688ceSJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
60855e1688ceSJohannes Berg 		.data[0] = &cmd,
60865e1688ceSJohannes Berg 		.len[0] = sizeof(cmd),
60875e1688ceSJohannes Berg 		.data[1] = data,
60885e1688ceSJohannes Berg 		.len[1] = size,
60895e1688ceSJohannes Berg 		.flags = sync ? 0 : CMD_ASYNC,
60905e1688ceSJohannes Berg 	};
60910636b938SSara Sharon 	int ret;
60920636b938SSara Sharon 
60935e1688ceSJohannes Berg 	/* size must be a multiple of DWORD */
60945e1688ceSJohannes Berg 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
60955e1688ceSJohannes Berg 		return;
60960636b938SSara Sharon 
6097cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
60980636b938SSara Sharon 		return;
60990636b938SSara Sharon 
61005e1688ceSJohannes Berg 	if (sync) {
61015e1688ceSJohannes Berg 		cmd.notif.cookie = mvm->queue_sync_cookie;
61022f7a04c7SJohannes Berg 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6103a2113cc4SNaftali Goldstein 	}
6104d0ff5d22SSara Sharon 
61055e1688ceSJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
61060636b938SSara Sharon 	if (ret) {
61070636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
61080636b938SSara Sharon 		goto out;
61090636b938SSara Sharon 	}
6110d0ff5d22SSara Sharon 
61115e1688ceSJohannes Berg 	if (sync) {
61123c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
61133a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
61142f7a04c7SJohannes Berg 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
61156ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
61160636b938SSara Sharon 					 HZ);
61172f7a04c7SJohannes Berg 		WARN_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm),
61182f7a04c7SJohannes Berg 			  "queue sync: failed to sync, state is 0x%lx\n",
61192f7a04c7SJohannes Berg 			  mvm->queue_sync_state);
61206ad04359SJohannes Berg 	}
61210636b938SSara Sharon 
61220636b938SSara Sharon out:
61235e1688ceSJohannes Berg 	if (sync) {
61242f7a04c7SJohannes Berg 		mvm->queue_sync_state = 0;
61250636b938SSara Sharon 		mvm->queue_sync_cookie++;
61260636b938SSara Sharon 	}
61275f8a3561SJohannes Berg }
61280636b938SSara Sharon 
6129cbce62a3SMiri Korenblit void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
61300636b938SSara Sharon {
61310636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
61320636b938SSara Sharon 
61330636b938SSara Sharon 	mutex_lock(&mvm->mutex);
61345e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
61350636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
61360636b938SSara Sharon }
61370636b938SSara Sharon 
6138cbce62a3SMiri Korenblit int
6139b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6140b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
6141b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
6142b73f9a4aSJohannes Berg {
6143b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6144b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6145b73f9a4aSJohannes Berg 
6146b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6147b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6148b73f9a4aSJohannes Berg 		return -EINVAL;
6149b73f9a4aSJohannes Berg 
6150b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
6151b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
6152b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
6153b73f9a4aSJohannes Berg 
6154b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6155b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6156b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
6157b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
6158b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6159b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6160b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6161b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6162b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6163b73f9a4aSJohannes Berg 
6164b73f9a4aSJohannes Berg 	return 0;
6165b73f9a4aSJohannes Berg }
6166b73f9a4aSJohannes Berg 
6167cbce62a3SMiri Korenblit int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6168fc36ffdaSJohannes Berg 		       struct cfg80211_pmsr_request *request)
6169fc36ffdaSJohannes Berg {
6170fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6171fc36ffdaSJohannes Berg 	int ret;
6172fc36ffdaSJohannes Berg 
6173fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
6174fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
6175fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
6176fc36ffdaSJohannes Berg 
6177fc36ffdaSJohannes Berg 	return ret;
6178fc36ffdaSJohannes Berg }
6179fc36ffdaSJohannes Berg 
6180cbce62a3SMiri Korenblit void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6181fc36ffdaSJohannes Berg 			struct cfg80211_pmsr_request *request)
6182fc36ffdaSJohannes Berg {
6183fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6184fc36ffdaSJohannes Berg 
6185fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
6186fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
6187fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
6188fc36ffdaSJohannes Berg }
6189fc36ffdaSJohannes Berg 
6190438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6191438af969SSara Sharon {
6192438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
6193438af969SSara Sharon 
6194438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
6195438af969SSara Sharon 		return false;
6196438af969SSara Sharon 
6197438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6198438af969SSara Sharon }
6199438af969SSara Sharon 
6200438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6201438af969SSara Sharon 				      struct sk_buff *head,
6202438af969SSara Sharon 				      struct sk_buff *skb)
6203438af969SSara Sharon {
6204438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6205438af969SSara Sharon 
6206438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
6207438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
6208438af969SSara Sharon 		return false;
6209438af969SSara Sharon 
6210438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
6211438af969SSara Sharon 		return true;
6212438af969SSara Sharon 
6213438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6214438af969SSara Sharon }
6215438af969SSara Sharon 
6216be8897e2SAvraham Stern int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6217cf85123aSAvraham Stern 			     struct ieee80211_vif *vif,
6218cf85123aSAvraham Stern 			     struct cfg80211_set_hw_timestamp *hwts)
6219cf85123aSAvraham Stern {
6220cf85123aSAvraham Stern 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6221cf85123aSAvraham Stern 	u32 protocols = 0;
6222cf85123aSAvraham Stern 	int ret;
6223cf85123aSAvraham Stern 
6224cf85123aSAvraham Stern 	/* HW timestamping is only supported for a specific station */
6225cf85123aSAvraham Stern 	if (!hwts->macaddr)
6226cf85123aSAvraham Stern 		return -EOPNOTSUPP;
6227cf85123aSAvraham Stern 
6228cf85123aSAvraham Stern 	if (hwts->enable)
6229cf85123aSAvraham Stern 		protocols =
6230cf85123aSAvraham Stern 			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6231cf85123aSAvraham Stern 
6232cf85123aSAvraham Stern 	mutex_lock(&mvm->mutex);
6233cf85123aSAvraham Stern 	ret = iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6234cf85123aSAvraham Stern 	mutex_unlock(&mvm->mutex);
6235cf85123aSAvraham Stern 
6236cf85123aSAvraham Stern 	return ret;
6237cf85123aSAvraham Stern }
6238cf85123aSAvraham Stern 
6239e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
6240e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
6241cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6242e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
6243e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
62444ea1ed1dSEmmanuel Grumbach 	.set_antenna = iwl_mvm_op_set_antenna,
6245e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
6246e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
6247e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
6248e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
6249e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
6250e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
6251e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
6252e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
6253e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
6254e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
6255e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
6256e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6257e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6258e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
6259e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
6260e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6261e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6262e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6263e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
6264e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
6265e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
62666b1259d1SJohannes Berg 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6267e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6268e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
6269a6cc6ccbSJohannes Berg 	.flush_sta = iwl_mvm_mac_flush_sta,
6270e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
6271e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6272e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
6273e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
6274e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
6275e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
6276e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
6277e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
6278e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
6279e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6280e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6281e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6282e705c121SKalle Valo 
6283ae7ba17bSShaul Triebitz 	.start_ap = iwl_mvm_start_ap,
6284ae7ba17bSShaul Triebitz 	.stop_ap = iwl_mvm_stop_ap,
6285ae7ba17bSShaul Triebitz 	.join_ibss = iwl_mvm_start_ibss,
6286ae7ba17bSShaul Triebitz 	.leave_ibss = iwl_mvm_stop_ibss,
6287e705c121SKalle Valo 
62882f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
62892f0282dbSJohannes Berg 
6290e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
6291e705c121SKalle Valo 
6292e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
6293e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
6294e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
629579221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
6296c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6297e705c121SKalle Valo 
6298e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6299e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6300e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6301e705c121SKalle Valo 
6302e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
6303e705c121SKalle Valo 
63040636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
63050636b938SSara Sharon 
6306e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
6307e705c121SKalle Valo 
6308e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
6309e705c121SKalle Valo 	/* look at d3.c */
6310e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
6311e705c121SKalle Valo 	.resume = iwl_mvm_resume,
6312e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
6313e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
6314e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
6315e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6316e705c121SKalle Valo #endif
6317e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6318e705c121SKalle Valo #endif
6319e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
6320e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
6321b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6322fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
6323fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
6324b73f9a4aSJohannes Berg 
6325438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6326177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
63273f244876SBenjamin Berg 	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
6328177a11cfSGregory Greenman #endif
6329cf85123aSAvraham Stern 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6330e705c121SKalle Valo };
6331