18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
3091296d3SMiri Korenblit  * Copyright (C) 2012-2014, 2018-2022 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"
31e705c121SKalle Valo 
32e705c121SKalle Valo static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
33e705c121SKalle Valo 	{
34e705c121SKalle Valo 		.max = 1,
35e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_STATION),
36e705c121SKalle Valo 	},
37e705c121SKalle Valo 	{
38e705c121SKalle Valo 		.max = 1,
39e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_AP) |
40e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
41e705c121SKalle Valo 			BIT(NL80211_IFTYPE_P2P_GO),
42e705c121SKalle Valo 	},
43e705c121SKalle Valo 	{
44e705c121SKalle Valo 		.max = 1,
45e705c121SKalle Valo 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
46e705c121SKalle Valo 	},
47e705c121SKalle Valo };
48e705c121SKalle Valo 
49e705c121SKalle Valo static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
50e705c121SKalle Valo 	{
51e705c121SKalle Valo 		.num_different_channels = 2,
52e705c121SKalle Valo 		.max_interfaces = 3,
53e705c121SKalle Valo 		.limits = iwl_mvm_limits,
54e705c121SKalle Valo 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
55e705c121SKalle Valo 	},
56e705c121SKalle Valo };
57e705c121SKalle Valo 
58fc36ffdaSJohannes Berg static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
59fc36ffdaSJohannes Berg 	.max_peers = IWL_MVM_TOF_MAX_APS,
60fc36ffdaSJohannes Berg 	.report_ap_tsf = 1,
61fc36ffdaSJohannes Berg 	.randomize_mac_addr = 1,
62fc36ffdaSJohannes Berg 
63fc36ffdaSJohannes Berg 	.ftm = {
64fc36ffdaSJohannes Berg 		.supported = 1,
65fc36ffdaSJohannes Berg 		.asap = 1,
66fc36ffdaSJohannes Berg 		.non_asap = 1,
67fc36ffdaSJohannes Berg 		.request_lci = 1,
68fc36ffdaSJohannes Berg 		.request_civicloc = 1,
696815e3d0SAvraham Stern 		.trigger_based = 1,
706815e3d0SAvraham Stern 		.non_trigger_based = 1,
71fc36ffdaSJohannes Berg 		.max_bursts_exponent = -1, /* all supported */
72fc36ffdaSJohannes Berg 		.max_ftms_per_burst = 0, /* no limits */
73fc36ffdaSJohannes Berg 		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
74fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_20) |
75fc36ffdaSJohannes Berg 			      BIT(NL80211_CHAN_WIDTH_40) |
768a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_80) |
778a2c1516SAvraham Stern 			      BIT(NL80211_CHAN_WIDTH_160),
78fc36ffdaSJohannes Berg 		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
79fc36ffdaSJohannes Berg 			     BIT(NL80211_PREAMBLE_HT) |
806815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_VHT) |
816815e3d0SAvraham Stern 			     BIT(NL80211_PREAMBLE_HE),
82fc36ffdaSJohannes Berg 	},
83fc36ffdaSJohannes Berg };
84fc36ffdaSJohannes Berg 
856569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
86c56e00a3SJohannes Berg 				 enum set_key_cmd cmd,
87c56e00a3SJohannes Berg 				 struct ieee80211_vif *vif,
88c56e00a3SJohannes Berg 				 struct ieee80211_sta *sta,
89c56e00a3SJohannes Berg 				 struct ieee80211_key_conf *key);
90c56e00a3SJohannes Berg 
91e705c121SKalle Valo static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
92e705c121SKalle Valo {
93e705c121SKalle Valo 	int i;
94e705c121SKalle Valo 
95e705c121SKalle Valo 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
96e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
97e705c121SKalle Valo 		mvm->phy_ctxts[i].id = i;
98e705c121SKalle Valo 		mvm->phy_ctxts[i].ref = 0;
99e705c121SKalle Valo 	}
100e705c121SKalle Valo }
101e705c121SKalle Valo 
102e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
103e705c121SKalle Valo 						  const char *alpha2,
104e705c121SKalle Valo 						  enum iwl_mcc_source src_id,
105e705c121SKalle Valo 						  bool *changed)
106e705c121SKalle Valo {
107e705c121SKalle Valo 	struct ieee80211_regdomain *regd = NULL;
108e705c121SKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
109e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
110e705c121SKalle Valo 	struct iwl_mcc_update_resp *resp;
111e27c506aSGil Adam 	u8 resp_ver;
112e705c121SKalle Valo 
113e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
114e705c121SKalle Valo 
115e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
116e705c121SKalle Valo 
117e705c121SKalle Valo 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
118e705c121SKalle Valo 	if (IS_ERR_OR_NULL(resp)) {
119e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
120e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(resp));
1216d19a5ebSEmmanuel Grumbach 		resp = NULL;
122e705c121SKalle Valo 		goto out;
123e705c121SKalle Valo 	}
124e705c121SKalle Valo 
12582715ac7SEmmanuel Grumbach 	if (changed) {
12682715ac7SEmmanuel Grumbach 		u32 status = le32_to_cpu(resp->status);
12782715ac7SEmmanuel Grumbach 
12882715ac7SEmmanuel Grumbach 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
12982715ac7SEmmanuel Grumbach 			    status == MCC_RESP_ILLEGAL);
13082715ac7SEmmanuel Grumbach 	}
131e27c506aSGil Adam 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
132e27c506aSGil Adam 					   MCC_UPDATE_CMD, 0);
133e27c506aSGil Adam 	IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
134e705c121SKalle Valo 
135e705c121SKalle Valo 	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
136e705c121SKalle Valo 				      __le32_to_cpu(resp->n_channels),
137e705c121SKalle Valo 				      resp->channels,
13877e30e10SHaim Dreyfuss 				      __le16_to_cpu(resp->mcc),
1392763bba6SHaim Dreyfuss 				      __le16_to_cpu(resp->geo_info),
140e27c506aSGil Adam 				      __le16_to_cpu(resp->cap), resp_ver);
141e705c121SKalle Valo 	/* Store the return source id */
142e705c121SKalle Valo 	src_id = resp->source_id;
143e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd)) {
144e705c121SKalle Valo 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
145e705c121SKalle Valo 			      PTR_ERR_OR_ZERO(regd));
146e705c121SKalle Valo 		goto out;
147e705c121SKalle Valo 	}
148e705c121SKalle Valo 
149e705c121SKalle Valo 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
150e705c121SKalle Valo 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
151e705c121SKalle Valo 	mvm->lar_regdom_set = true;
152e705c121SKalle Valo 	mvm->mcc_src = src_id;
153e705c121SKalle Valo 
1546d19a5ebSEmmanuel Grumbach 	iwl_mei_set_country_code(__le16_to_cpu(resp->mcc));
1556d19a5ebSEmmanuel Grumbach 
156e705c121SKalle Valo out:
1576d19a5ebSEmmanuel Grumbach 	kfree(resp);
158e705c121SKalle Valo 	return regd;
159e705c121SKalle Valo }
160e705c121SKalle Valo 
161e705c121SKalle Valo void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
162e705c121SKalle Valo {
163e705c121SKalle Valo 	bool changed;
164e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
165e705c121SKalle Valo 
166e705c121SKalle Valo 	if (!iwl_mvm_is_lar_supported(mvm))
167e705c121SKalle Valo 		return;
168e705c121SKalle Valo 
169e705c121SKalle Valo 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
170e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(regd)) {
171e705c121SKalle Valo 		/* only update the regulatory core if changed */
172e705c121SKalle Valo 		if (changed)
173e705c121SKalle Valo 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
174e705c121SKalle Valo 
175e705c121SKalle Valo 		kfree(regd);
176e705c121SKalle Valo 	}
177e705c121SKalle Valo }
178e705c121SKalle Valo 
179e705c121SKalle Valo struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
180e705c121SKalle Valo 							  bool *changed)
181e705c121SKalle Valo {
182e705c121SKalle Valo 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
183e705c121SKalle Valo 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
184e705c121SKalle Valo 				     MCC_SOURCE_GET_CURRENT :
185e705c121SKalle Valo 				     MCC_SOURCE_OLD_FW, changed);
186e705c121SKalle Valo }
187e705c121SKalle Valo 
188e705c121SKalle Valo int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
189e705c121SKalle Valo {
190e705c121SKalle Valo 	enum iwl_mcc_source used_src;
191e705c121SKalle Valo 	struct ieee80211_regdomain *regd;
192e705c121SKalle Valo 	int ret;
193e705c121SKalle Valo 	bool changed;
194e705c121SKalle Valo 	const struct ieee80211_regdomain *r =
195a05829a7SJohannes Berg 			wiphy_dereference(mvm->hw->wiphy, mvm->hw->wiphy->regd);
196e705c121SKalle Valo 
197e705c121SKalle Valo 	if (!r)
198e705c121SKalle Valo 		return -ENOENT;
199e705c121SKalle Valo 
200e705c121SKalle Valo 	/* save the last source in case we overwrite it below */
201e705c121SKalle Valo 	used_src = mvm->mcc_src;
202e705c121SKalle Valo 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
203e705c121SKalle Valo 		/* Notify the firmware we support wifi location updates */
204e705c121SKalle Valo 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
205e705c121SKalle Valo 		if (!IS_ERR_OR_NULL(regd))
206e705c121SKalle Valo 			kfree(regd);
207e705c121SKalle Valo 	}
208e705c121SKalle Valo 
209e705c121SKalle Valo 	/* Now set our last stored MCC and source */
210e705c121SKalle Valo 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
211e705c121SKalle Valo 				     &changed);
212e705c121SKalle Valo 	if (IS_ERR_OR_NULL(regd))
213e705c121SKalle Valo 		return -EIO;
214e705c121SKalle Valo 
215e705c121SKalle Valo 	/* update cfg80211 if the regdomain was changed */
216e705c121SKalle Valo 	if (changed)
217a05829a7SJohannes Berg 		ret = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
218e705c121SKalle Valo 	else
219e705c121SKalle Valo 		ret = 0;
220e705c121SKalle Valo 
221e705c121SKalle Valo 	kfree(regd);
222e705c121SKalle Valo 	return ret;
223e705c121SKalle Valo }
224e705c121SKalle Valo 
2257f2ea521SYueHaibing static const u8 he_if_types_ext_capa_sta[] = {
2267360f99eSEmmanuel Grumbach 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
227918cbf39SSara Sharon 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
2287360f99eSEmmanuel Grumbach 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
2297360f99eSEmmanuel Grumbach };
2307360f99eSEmmanuel Grumbach 
2317f2ea521SYueHaibing static const struct wiphy_iftype_ext_capab he_iftypes_ext_capa[] = {
2327360f99eSEmmanuel Grumbach 	{
2337360f99eSEmmanuel Grumbach 		.iftype = NL80211_IFTYPE_STATION,
2347360f99eSEmmanuel Grumbach 		.extended_capabilities = he_if_types_ext_capa_sta,
2357360f99eSEmmanuel Grumbach 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
2367360f99eSEmmanuel Grumbach 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
2377360f99eSEmmanuel Grumbach 	},
2387360f99eSEmmanuel Grumbach };
2397360f99eSEmmanuel Grumbach 
240e8503aecSBen Greear static int
241e8503aecSBen Greear iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
242e8503aecSBen Greear {
243e8503aecSBen Greear 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
244e8503aecSBen Greear 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
245e8503aecSBen Greear 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
246e8503aecSBen Greear 	return 0;
247e8503aecSBen Greear }
248e8503aecSBen Greear 
249e705c121SKalle Valo int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
250e705c121SKalle Valo {
251e705c121SKalle Valo 	struct ieee80211_hw *hw = mvm->hw;
252e705c121SKalle Valo 	int num_mac, ret, i;
253e705c121SKalle Valo 	static const u32 mvm_ciphers[] = {
254e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP40,
255e705c121SKalle Valo 		WLAN_CIPHER_SUITE_WEP104,
256e705c121SKalle Valo 		WLAN_CIPHER_SUITE_TKIP,
257e705c121SKalle Valo 		WLAN_CIPHER_SUITE_CCMP,
258e705c121SKalle Valo 	};
2593f37c229SIdo Yariv #ifdef CONFIG_PM_SLEEP
2603f37c229SIdo Yariv 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
2613f37c229SIdo Yariv 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2623f37c229SIdo Yariv #endif
263e705c121SKalle Valo 
264e705c121SKalle Valo 	/* Tell mac80211 our characteristics */
265e705c121SKalle Valo 	ieee80211_hw_set(hw, SIGNAL_DBM);
266e705c121SKalle Valo 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
267e705c121SKalle Valo 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
268e705c121SKalle Valo 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
269e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_PS);
270e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
271e705c121SKalle Valo 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
272e705c121SKalle Valo 	ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
273e705c121SKalle Valo 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
274e705c121SKalle Valo 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
275e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
276e705c121SKalle Valo 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
277909ddf0bSJohannes Berg 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
27830433d3bSJohannes Berg 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
279d270e7b8SIlan Peer 	ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
280520229e4SShaul Triebitz 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
281cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
282cfbc6c4cSSara Sharon 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
283cfb21b11SJohannes Berg 	/*
284cfb21b11SJohannes Berg 	 * On older devices, enabling TX A-MSDU occasionally leads to
285cfb21b11SJohannes Berg 	 * something getting messed up, the command read from the FIFO
286cfb21b11SJohannes Berg 	 * gets out of sync and isn't a TX command, so that we have an
287cfb21b11SJohannes Berg 	 * assert EDC.
288cfb21b11SJohannes Berg 	 *
289cfb21b11SJohannes Berg 	 * It's not clear where the bug is, but since we didn't used to
290cfb21b11SJohannes Berg 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
291cfb21b11SJohannes Berg 	 * for older devices. We also don't see this issue on any newer
292cfb21b11SJohannes Berg 	 * devices.
293cfb21b11SJohannes Berg 	 */
2947d34a7d7SLuca Coelho 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
295438af969SSara Sharon 		ieee80211_hw_set(hw, TX_AMSDU);
296438af969SSara Sharon 	ieee80211_hw_set(hw, TX_FRAG_LIST);
297ecaf71deSGregory Greenman 
2984243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm)) {
299ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
300ecaf71deSGregory Greenman 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
301ecaf71deSGregory Greenman 	}
302ecaf71deSGregory Greenman 
303b915c101SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
304b915c101SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
305960f864bSJohannes Berg 
306960f864bSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
307960f864bSJohannes Berg 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
30865e25482SJohannes Berg 		ieee80211_hw_set(hw, AP_LINK_PS);
309960f864bSJohannes Berg 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
310960f864bSJohannes Berg 		/*
311960f864bSJohannes Berg 		 * we absolutely need this for the new TX API since that comes
312960f864bSJohannes Berg 		 * with many more queues than the current code can deal with
313960f864bSJohannes Berg 		 * for station powersave
314960f864bSJohannes Berg 		 */
315960f864bSJohannes Berg 		return -EINVAL;
316960f864bSJohannes Berg 	}
317e705c121SKalle Valo 
31880938abcSJohannes Berg 	if (mvm->trans->num_rx_queues > 1)
31980938abcSJohannes Berg 		ieee80211_hw_set(hw, USES_RSS);
32080938abcSJohannes Berg 
3212d7cf549SJohannes Berg 	if (mvm->trans->max_skb_frags)
3222d7cf549SJohannes Berg 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
3232d7cf549SJohannes Berg 
324366fc672SJohannes Berg 	hw->queues = IEEE80211_NUM_ACS;
325e705c121SKalle Valo 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
326e705c121SKalle Valo 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
327e705c121SKalle Valo 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
328e705c121SKalle Valo 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
329e705c121SKalle Valo 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
330371a17edSJohannes Berg 
331371a17edSJohannes Berg 	hw->radiotap_timestamp.units_pos =
332371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
333371a17edSJohannes Berg 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
334371a17edSJohannes Berg 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
335371a17edSJohannes Berg 	hw->radiotap_timestamp.accuracy = 22;
336371a17edSJohannes Berg 
3374243edb4SEmmanuel Grumbach 	if (!iwl_mvm_has_tlc_offload(mvm))
3389f66a397SGregory Greenman 		hw->rate_control_algorithm = RS_NAME;
3399f66a397SGregory Greenman 
340e705c121SKalle Valo 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
341e705c121SKalle Valo 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
342438af969SSara Sharon 	hw->max_tx_fragments = mvm->trans->max_skb_frags;
343e705c121SKalle Valo 
3448e160ab8SAyala Beker 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
345e705c121SKalle Valo 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
346e705c121SKalle Valo 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
347e705c121SKalle Valo 	hw->wiphy->cipher_suites = mvm->ciphers;
348e705c121SKalle Valo 
3492a53d166SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
3502a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
3512a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP;
3522a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
3532a53d166SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
3542a53d166SAyala Beker 			WLAN_CIPHER_SUITE_GCMP_256;
3552a53d166SAyala Beker 		hw->wiphy->n_cipher_suites++;
3562a53d166SAyala Beker 	}
3572a53d166SAyala Beker 
358f4bfdc5eSEmmanuel Grumbach 	if (iwlwifi_mod_params.swcrypto)
359f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
360f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
361f4bfdc5eSEmmanuel Grumbach 	if (!iwlwifi_mod_params.bt_coex_active)
362f4bfdc5eSEmmanuel Grumbach 		IWL_ERR(mvm,
363f4bfdc5eSEmmanuel Grumbach 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
364f4bfdc5eSEmmanuel Grumbach 
365e705c121SKalle Valo 	ieee80211_hw_set(hw, MFP_CAPABLE);
366f4bfdc5eSEmmanuel Grumbach 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
367e705c121SKalle Valo 	hw->wiphy->n_cipher_suites++;
3688e160ab8SAyala Beker 	if (iwl_mvm_has_new_rx_api(mvm)) {
3698e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
3708e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
3718e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
3728e160ab8SAyala Beker 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
3738e160ab8SAyala Beker 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
3748e160ab8SAyala Beker 		hw->wiphy->n_cipher_suites++;
3758e160ab8SAyala Beker 	}
376e705c121SKalle Valo 
377ef2e7a51SIlan Peer 	wiphy_ext_feature_set(hw->wiphy,
378ef2e7a51SIlan Peer 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
379ef2e7a51SIlan Peer 
380b73f9a4aSJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
381fc36ffdaSJohannes Berg 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
382b73f9a4aSJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
383b73f9a4aSJohannes Berg 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
384fc36ffdaSJohannes Berg 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
385fc36ffdaSJohannes Berg 	}
386b73f9a4aSJohannes Berg 
387b1fdc250SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
388b1fdc250SJohannes Berg 			IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
389b1fdc250SJohannes Berg 		wiphy_ext_feature_set(hw->wiphy,
390b1fdc250SJohannes Berg 				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
391b1fdc250SJohannes Berg 
392e705c121SKalle Valo 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
393e705c121SKalle Valo 	hw->wiphy->features |=
394e705c121SKalle Valo 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
395e705c121SKalle Valo 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
396e705c121SKalle Valo 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
397e705c121SKalle Valo 
398e705c121SKalle Valo 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
399e705c121SKalle Valo 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
400e705c121SKalle Valo 	hw->chanctx_data_size = sizeof(u16);
401cfbc6c4cSSara Sharon 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
402e705c121SKalle Valo 
403e705c121SKalle Valo 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
404e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
405e705c121SKalle Valo 		BIT(NL80211_IFTYPE_AP) |
406e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_GO) |
407e705c121SKalle Valo 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
408e705c121SKalle Valo 		BIT(NL80211_IFTYPE_ADHOC);
409e705c121SKalle Valo 
410e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
411e47df5bdSJohannes Berg 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
41233e3fd99SAlexander Wetzel 
41333e3fd99SAlexander Wetzel 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
41433e3fd99SAlexander Wetzel 	 * the hw, preventing us to control which key(id) to use per MPDU.
41533e3fd99SAlexander Wetzel 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
41633e3fd99SAlexander Wetzel 	 */
41733e3fd99SAlexander Wetzel 	if (!iwl_mvm_has_new_tx_api(mvm))
41833e3fd99SAlexander Wetzel 		wiphy_ext_feature_set(hw->wiphy,
41933e3fd99SAlexander Wetzel 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
420e47df5bdSJohannes Berg 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
421e47df5bdSJohannes Berg 
422e705c121SKalle Valo 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
423e705c121SKalle Valo 	if (iwl_mvm_is_lar_supported(mvm))
424e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
425e705c121SKalle Valo 	else
426e705c121SKalle Valo 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
427e705c121SKalle Valo 					       REGULATORY_DISABLE_BEACON_HINTS;
428e705c121SKalle Valo 
429e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
430e705c121SKalle Valo 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
431eae94cf8SLuca Coelho 	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
432e705c121SKalle Valo 
433e705c121SKalle Valo 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
434e705c121SKalle Valo 	hw->wiphy->n_iface_combinations =
435e705c121SKalle Valo 		ARRAY_SIZE(iwl_mvm_iface_combinations);
436e705c121SKalle Valo 
437e705c121SKalle Valo 	hw->wiphy->max_remain_on_channel_duration = 10000;
438e705c121SKalle Valo 	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
439e705c121SKalle Valo 
440e705c121SKalle Valo 	/* Extract MAC address */
441e705c121SKalle Valo 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
442e705c121SKalle Valo 	hw->wiphy->addresses = mvm->addresses;
443e705c121SKalle Valo 	hw->wiphy->n_addresses = 1;
444e705c121SKalle Valo 
445e705c121SKalle Valo 	/* Extract additional MAC addresses if available */
446e705c121SKalle Valo 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
447e705c121SKalle Valo 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
448e705c121SKalle Valo 
449e705c121SKalle Valo 	for (i = 1; i < num_mac; i++) {
450e705c121SKalle Valo 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
451e705c121SKalle Valo 		       ETH_ALEN);
452e705c121SKalle Valo 		mvm->addresses[i].addr[5]++;
453e705c121SKalle Valo 		hw->wiphy->n_addresses++;
454e705c121SKalle Valo 	}
455e705c121SKalle Valo 
456e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
457e705c121SKalle Valo 
458e705c121SKalle Valo 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
459e705c121SKalle Valo 
460e705c121SKalle Valo 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
461e705c121SKalle Valo 
462e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
463e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
464e705c121SKalle Valo 		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
465e705c121SKalle Valo 
466e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
467e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
468e705c121SKalle Valo 	else
469e705c121SKalle Valo 		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
470e705c121SKalle Valo 
47157fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
47257fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
47357fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
47457fbcce3SJohannes Berg 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
47557fbcce3SJohannes Berg 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
47657fbcce3SJohannes Berg 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
477e705c121SKalle Valo 
478e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
479e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
480e705c121SKalle Valo 		    fw_has_api(&mvm->fw->ucode_capa,
481e705c121SKalle Valo 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
48257fbcce3SJohannes Berg 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
483e705c121SKalle Valo 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
484e705c121SKalle Valo 	}
485eae94cf8SLuca Coelho 	if (fw_has_capa(&mvm->fw->ucode_capa,
486eae94cf8SLuca Coelho 			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
487eae94cf8SLuca Coelho 	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
488eae94cf8SLuca Coelho 		hw->wiphy->bands[NL80211_BAND_6GHZ] =
489eae94cf8SLuca Coelho 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
490e705c121SKalle Valo 
491e705c121SKalle Valo 	hw->wiphy->hw_version = mvm->trans->hw_id;
492e705c121SKalle Valo 
493e705c121SKalle Valo 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
494e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
495e705c121SKalle Valo 	else
496e705c121SKalle Valo 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
497e705c121SKalle Valo 
498ca986ad9SArend Van Spriel 	hw->wiphy->max_sched_scan_reqs = 1;
499e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
5005d1234baSTova Mussai 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
501e705c121SKalle Valo 	/* we create the 802.11 header and zero length SSID IE. */
502e705c121SKalle Valo 	hw->wiphy->max_sched_scan_ie_len =
503e705c121SKalle Valo 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
504e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
505e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
506e705c121SKalle Valo 
507e705c121SKalle Valo 	/*
508e705c121SKalle Valo 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
509e705c121SKalle Valo 	 * infinite loop, so the maximum number of iterations is actually 254.
510e705c121SKalle Valo 	 */
511e705c121SKalle Valo 	hw->wiphy->max_sched_scan_plan_iterations = 254;
512e705c121SKalle Valo 
513e705c121SKalle Valo 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
514e705c121SKalle Valo 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
515e705c121SKalle Valo 			       NL80211_FEATURE_P2P_GO_OPPPS |
516a904a08bSPeer, Ilan 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
517e705c121SKalle Valo 			       NL80211_FEATURE_DYNAMIC_SMPS |
518e705c121SKalle Valo 			       NL80211_FEATURE_STATIC_SMPS |
519e705c121SKalle Valo 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
520e705c121SKalle Valo 
521e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
522e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
523e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
524e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
525e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
526e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
527e705c121SKalle Valo 
528e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
529e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
530e705c121SKalle Valo 		hw->wiphy->features |=
531e705c121SKalle Valo 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
532e705c121SKalle Valo 
533e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
534e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
535e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
536e705c121SKalle Valo 
537971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
5382a42aea7SNathan Errera 				  IWL_FW_CMD_VER_UNKNOWN) == 3)
5392a42aea7SNathan Errera 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
5402a42aea7SNathan Errera 
541aacf8f18SAvrahams Stern 	if (fw_has_api(&mvm->fw->ucode_capa,
542aacf8f18SAvrahams Stern 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
543aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
544aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
545aacf8f18SAvrahams Stern 		wiphy_ext_feature_set(hw->wiphy,
546aacf8f18SAvrahams Stern 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
547aacf8f18SAvrahams Stern 	}
548aacf8f18SAvrahams Stern 
5498f691af9SZamir, Roee 	if (iwl_mvm_is_oce_supported(mvm)) {
550971cbe50SJohannes Berg 		u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
551773a042fSAvraham Stern 
5528f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
5538f691af9SZamir, Roee 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
5548f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
5558f691af9SZamir, Roee 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
5568f691af9SZamir, Roee 		wiphy_ext_feature_set(hw->wiphy,
5578f691af9SZamir, Roee 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
558773a042fSAvraham Stern 
559773a042fSAvraham Stern 		/* Old firmware also supports probe deferral and suppression */
560773a042fSAvraham Stern 		if (scan_ver < 15)
561773a042fSAvraham Stern 			wiphy_ext_feature_set(hw->wiphy,
562773a042fSAvraham Stern 					      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
5638f691af9SZamir, Roee 	}
5648f691af9SZamir, Roee 
5657360f99eSEmmanuel Grumbach 	if (mvm->nvm_data->sku_cap_11ax_enable &&
5667360f99eSEmmanuel Grumbach 	    !iwlwifi_mod_params.disable_11ax) {
5677360f99eSEmmanuel Grumbach 		hw->wiphy->iftype_ext_capab = he_iftypes_ext_capa;
5687360f99eSEmmanuel Grumbach 		hw->wiphy->num_iftype_ext_capab =
5697360f99eSEmmanuel Grumbach 			ARRAY_SIZE(he_iftypes_ext_capa);
570918cbf39SSara Sharon 
571918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
572918cbf39SSara Sharon 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
5737360f99eSEmmanuel Grumbach 	}
5747360f99eSEmmanuel Grumbach 
575e705c121SKalle Valo 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
576e705c121SKalle Valo 
577e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
5783f37c229SIdo Yariv 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
579e705c121SKalle Valo 	    mvm->trans->ops->d3_suspend &&
580e705c121SKalle Valo 	    mvm->trans->ops->d3_resume &&
581e705c121SKalle Valo 	    device_can_wakeup(mvm->trans->dev)) {
582e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
583e705c121SKalle Valo 				     WIPHY_WOWLAN_DISCONNECT |
584e705c121SKalle Valo 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
585e705c121SKalle Valo 				     WIPHY_WOWLAN_RFKILL_RELEASE |
586e705c121SKalle Valo 				     WIPHY_WOWLAN_NET_DETECT;
587e705c121SKalle Valo 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
588e705c121SKalle Valo 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
589e705c121SKalle Valo 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
590e705c121SKalle Valo 
591e705c121SKalle Valo 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
592e705c121SKalle Valo 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
593e705c121SKalle Valo 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
5945d1234baSTova Mussai 		mvm->wowlan.max_nd_match_sets =
5955d1234baSTova Mussai 			iwl_umac_scan_get_max_profiles(mvm->fw);
596e705c121SKalle Valo 		hw->wiphy->wowlan = &mvm->wowlan;
597e705c121SKalle Valo 	}
598e705c121SKalle Valo #endif
599e705c121SKalle Valo 
600e705c121SKalle Valo 	ret = iwl_mvm_leds_init(mvm);
601e705c121SKalle Valo 	if (ret)
602e705c121SKalle Valo 		return ret;
603e705c121SKalle Valo 
604e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
605e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
606e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
607e705c121SKalle Valo 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
608e705c121SKalle Valo 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
609e705c121SKalle Valo 	}
610e705c121SKalle Valo 
611e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa,
612e705c121SKalle Valo 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
613e705c121SKalle Valo 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
614e705c121SKalle Valo 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
615e705c121SKalle Valo 	}
616e705c121SKalle Valo 
617e705c121SKalle Valo 	hw->netdev_features |= mvm->cfg->features;
61859fa61f3SEmmanuel Grumbach 	if (!iwl_mvm_is_csum_supported(mvm))
6196772aab7SJohannes Berg 		hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
62041837ca9SEmmanuel Grumbach 
62191b08c2dSAviya Erenfeld 	if (mvm->cfg->vht_mu_mimo_supported)
62291b08c2dSAviya Erenfeld 		wiphy_ext_feature_set(hw->wiphy,
62391b08c2dSAviya Erenfeld 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
62491b08c2dSAviya Erenfeld 
6259c11d8a9SShaul Triebitz 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
6269c11d8a9SShaul Triebitz 		wiphy_ext_feature_set(hw->wiphy,
6279c11d8a9SShaul Triebitz 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
6289c11d8a9SShaul Triebitz 
629bfcfdb59SEmmanuel Grumbach 	iwl_mvm_vendor_cmds_register(mvm);
630bfcfdb59SEmmanuel Grumbach 
631e8503aecSBen Greear 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
632e8503aecSBen Greear 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
633e8503aecSBen Greear 
634de645e89SJohannes Berg 	ret = ieee80211_register_hw(mvm->hw);
635de645e89SJohannes Berg 	if (ret) {
636de645e89SJohannes Berg 		iwl_mvm_leds_exit(mvm);
637e705c121SKalle Valo 	}
638e705c121SKalle Valo 
639e705c121SKalle Valo 	return ret;
640e705c121SKalle Valo }
641e705c121SKalle Valo 
642df2378abSJohannes Berg static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
643df2378abSJohannes Berg 			   struct ieee80211_sta *sta)
644df2378abSJohannes Berg {
645df2378abSJohannes Berg 	if (likely(sta)) {
646df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
647df2378abSJohannes Berg 			return;
648df2378abSJohannes Berg 	} else {
649df2378abSJohannes Berg 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
650df2378abSJohannes Berg 			return;
651df2378abSJohannes Berg 	}
652df2378abSJohannes Berg 
653df2378abSJohannes Berg 	ieee80211_free_txskb(mvm->hw, skb);
654df2378abSJohannes Berg }
655df2378abSJohannes Berg 
656e705c121SKalle Valo static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
657e705c121SKalle Valo 			   struct ieee80211_tx_control *control,
658e705c121SKalle Valo 			   struct sk_buff *skb)
659e705c121SKalle Valo {
660e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
661e705c121SKalle Valo 	struct ieee80211_sta *sta = control->sta;
662e705c121SKalle Valo 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
663e705c121SKalle Valo 	struct ieee80211_hdr *hdr = (void *)skb->data;
664cfbc6c4cSSara Sharon 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
665cfbc6c4cSSara Sharon 		IEEE80211_TX_CTL_TX_OFFCHAN;
666e705c121SKalle Valo 
667e705c121SKalle Valo 	if (iwl_mvm_is_radio_killed(mvm)) {
668e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
669e705c121SKalle Valo 		goto drop;
670e705c121SKalle Valo 	}
671e705c121SKalle Valo 
672cfbc6c4cSSara Sharon 	if (offchannel &&
673e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
674e705c121SKalle Valo 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
675e705c121SKalle Valo 		goto drop;
676e705c121SKalle Valo 
67710516783SJohannes Berg 	/*
67810516783SJohannes Berg 	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
67910516783SJohannes Berg 	 * so we treat the others as broadcast
68010516783SJohannes Berg 	 */
68110516783SJohannes Berg 	if (ieee80211_is_mgmt(hdr->frame_control))
682e705c121SKalle Valo 		sta = NULL;
683e705c121SKalle Valo 
684dc1aca22SAndrei Otcheretianski 	/* If there is no sta, and it's not offchannel - send through AP */
685cfbc6c4cSSara Sharon 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
686cfbc6c4cSSara Sharon 	    !offchannel) {
687dc1aca22SAndrei Otcheretianski 		struct iwl_mvm_vif *mvmvif =
688dc1aca22SAndrei Otcheretianski 			iwl_mvm_vif_from_mac80211(info->control.vif);
689dc1aca22SAndrei Otcheretianski 		u8 ap_sta_id = READ_ONCE(mvmvif->ap_sta_id);
690dc1aca22SAndrei Otcheretianski 
691be9ae34eSNathan Errera 		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
692dc1aca22SAndrei Otcheretianski 			/* mac80211 holds rcu read lock */
693dc1aca22SAndrei Otcheretianski 			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
694dc1aca22SAndrei Otcheretianski 			if (IS_ERR_OR_NULL(sta))
695dc1aca22SAndrei Otcheretianski 				goto drop;
696dc1aca22SAndrei Otcheretianski 		}
697dc1aca22SAndrei Otcheretianski 	}
698dc1aca22SAndrei Otcheretianski 
699df2378abSJohannes Berg 	iwl_mvm_tx_skb(mvm, skb, sta);
700e705c121SKalle Valo 	return;
701e705c121SKalle Valo  drop:
702e705c121SKalle Valo 	ieee80211_free_txskb(hw, skb);
703e705c121SKalle Valo }
704e705c121SKalle Valo 
705cfbc6c4cSSara Sharon void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
706e705c121SKalle Valo {
707cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
708cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
709cfbc6c4cSSara Sharon 	struct sk_buff *skb = NULL;
710cfbc6c4cSSara Sharon 
711fba8248eSSara Sharon 	/*
712fba8248eSSara Sharon 	 * No need for threads to be pending here, they can leave the first
713fba8248eSSara Sharon 	 * taker all the work.
714fba8248eSSara Sharon 	 *
715fba8248eSSara Sharon 	 * mvmtxq->tx_request logic:
716fba8248eSSara Sharon 	 *
717fba8248eSSara Sharon 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
718fba8248eSSara Sharon 	 * will now start TX and other threads should quit.
719fba8248eSSara Sharon 	 *
720fba8248eSSara Sharon 	 * If 1, another thread is currently TXing, set to 2 to indicate to
721fba8248eSSara Sharon 	 * that thread that there was another request. Since that request may
722fba8248eSSara Sharon 	 * have raced with the check whether the queue is empty, the TXing
723fba8248eSSara Sharon 	 * thread should check the queue's status one more time before leaving.
724fba8248eSSara Sharon 	 * This check is done in order to not leave any TX hanging in the queue
725fba8248eSSara Sharon 	 * until the next TX invocation (which may not even happen).
726fba8248eSSara Sharon 	 *
727fba8248eSSara Sharon 	 * If 2, another thread is currently TXing, and it will already double
728fba8248eSSara Sharon 	 * check the queue, so do nothing.
729fba8248eSSara Sharon 	 */
730fba8248eSSara Sharon 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
731fba8248eSSara Sharon 		return;
732cfbc6c4cSSara Sharon 
733cfbc6c4cSSara Sharon 	rcu_read_lock();
734fba8248eSSara Sharon 	do {
735cfbc6c4cSSara Sharon 		while (likely(!mvmtxq->stopped &&
73600520b7aSEmmanuel Grumbach 			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
737cfbc6c4cSSara Sharon 			skb = ieee80211_tx_dequeue(hw, txq);
738cfbc6c4cSSara Sharon 
739f50d693bSSara Sharon 			if (!skb) {
740f50d693bSSara Sharon 				if (txq->sta)
741f50d693bSSara Sharon 					IWL_DEBUG_TX(mvm,
742f50d693bSSara Sharon 						     "TXQ of sta %pM tid %d is now empty\n",
743f50d693bSSara Sharon 						     txq->sta->addr,
744f50d693bSSara Sharon 						     txq->tid);
745cfbc6c4cSSara Sharon 				break;
746f50d693bSSara Sharon 			}
747cfbc6c4cSSara Sharon 
748cfbc6c4cSSara Sharon 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
749cfbc6c4cSSara Sharon 		}
750fba8248eSSara Sharon 	} while (atomic_dec_return(&mvmtxq->tx_request));
751cfbc6c4cSSara Sharon 	rcu_read_unlock();
752e705c121SKalle Valo }
753e705c121SKalle Valo 
754cfbc6c4cSSara Sharon static void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
755cfbc6c4cSSara Sharon 				      struct ieee80211_txq *txq)
756e705c121SKalle Valo {
757cfbc6c4cSSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
758cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
759e705c121SKalle Valo 
760cfbc6c4cSSara Sharon 	/*
761cfbc6c4cSSara Sharon 	 * Please note that racing is handled very carefully here:
762cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id is updated during allocation, and mvmtxq->list is
763cfbc6c4cSSara Sharon 	 * deleted afterwards.
764cfbc6c4cSSara Sharon 	 * This means that if:
765cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && list_empty(&mvmtxq->list):
766cfbc6c4cSSara Sharon 	 *	queue is allocated and we can TX.
767cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id != INVALID_QUEUE && !list_empty(&mvmtxq->list):
768cfbc6c4cSSara Sharon 	 *	a race, should defer the frame.
769cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && list_empty(&mvmtxq->list):
770cfbc6c4cSSara Sharon 	 *	need to allocate the queue and defer the frame.
771cfbc6c4cSSara Sharon 	 * mvmtxq->txq_id == INVALID_QUEUE && !list_empty(&mvmtxq->list):
772cfbc6c4cSSara Sharon 	 *	queue is already scheduled for allocation, no need to allocate,
773cfbc6c4cSSara Sharon 	 *	should defer the frame.
774cfbc6c4cSSara Sharon 	 */
775cfbc6c4cSSara Sharon 
776cfbc6c4cSSara Sharon 	/* If the queue is allocated TX and return. */
777cfbc6c4cSSara Sharon 	if (!txq->sta || mvmtxq->txq_id != IWL_MVM_INVALID_QUEUE) {
778cfbc6c4cSSara Sharon 		/*
779cfbc6c4cSSara Sharon 		 * Check that list is empty to avoid a race where txq_id is
780cfbc6c4cSSara Sharon 		 * already updated, but the queue allocation work wasn't
781cfbc6c4cSSara Sharon 		 * finished
782cfbc6c4cSSara Sharon 		 */
783cfbc6c4cSSara Sharon 		if (unlikely(txq->sta && !list_empty(&mvmtxq->list)))
784cfbc6c4cSSara Sharon 			return;
785cfbc6c4cSSara Sharon 
786cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(hw, txq);
787cfbc6c4cSSara Sharon 		return;
788cfbc6c4cSSara Sharon 	}
789cfbc6c4cSSara Sharon 
790cfbc6c4cSSara Sharon 	/* The list is being deleted only after the queue is fully allocated. */
791cfbc6c4cSSara Sharon 	if (!list_empty(&mvmtxq->list))
792cfbc6c4cSSara Sharon 		return;
793cfbc6c4cSSara Sharon 
794cfbc6c4cSSara Sharon 	list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
795cfbc6c4cSSara Sharon 	schedule_work(&mvm->add_stream_wk);
796e705c121SKalle Valo }
797e705c121SKalle Valo 
798e705c121SKalle Valo #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
799e705c121SKalle Valo 	do {								\
800e705c121SKalle Valo 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
801e705c121SKalle Valo 			break;						\
8027174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
803e705c121SKalle Valo 	} while (0)
804e705c121SKalle Valo 
805e705c121SKalle Valo static void
806e705c121SKalle Valo iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
807e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
808e705c121SKalle Valo 			    enum ieee80211_ampdu_mlme_action action)
809e705c121SKalle Valo {
810e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
811e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
812e705c121SKalle Valo 
8136c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
8146c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
8156c042d75SSara Sharon 	if (!trig)
816e705c121SKalle Valo 		return;
817e705c121SKalle Valo 
818e705c121SKalle Valo 	ba_trig = (void *)trig->data;
819e705c121SKalle Valo 
820e705c121SKalle Valo 	switch (action) {
821e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
822e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
823e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
824e705c121SKalle Valo 
825e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
826e705c121SKalle Valo 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
827e705c121SKalle Valo 				 sta->addr, tid, tid_data->ssn);
828e705c121SKalle Valo 		break;
829e705c121SKalle Valo 		}
830e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
831e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
832e705c121SKalle Valo 				 "TX AGG STOP: MAC %pM tid %d\n",
833e705c121SKalle Valo 				 sta->addr, tid);
834e705c121SKalle Valo 		break;
835e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
836e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
837e705c121SKalle Valo 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
838e705c121SKalle Valo 				 sta->addr, tid, rx_ba_ssn);
839e705c121SKalle Valo 		break;
840e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
841e705c121SKalle Valo 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
842e705c121SKalle Valo 				 "RX AGG STOP: MAC %pM tid %d\n",
843e705c121SKalle Valo 				 sta->addr, tid);
844e705c121SKalle Valo 		break;
845e705c121SKalle Valo 	default:
846e705c121SKalle Valo 		break;
847e705c121SKalle Valo 	}
848e705c121SKalle Valo }
849e705c121SKalle Valo 
850e705c121SKalle Valo static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
851e705c121SKalle Valo 				    struct ieee80211_vif *vif,
85250ea05efSSara Sharon 				    struct ieee80211_ampdu_params *params)
853e705c121SKalle Valo {
854e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
855e705c121SKalle Valo 	int ret;
85650ea05efSSara Sharon 	struct ieee80211_sta *sta = params->sta;
85750ea05efSSara Sharon 	enum ieee80211_ampdu_mlme_action action = params->action;
85850ea05efSSara Sharon 	u16 tid = params->tid;
85950ea05efSSara Sharon 	u16 *ssn = &params->ssn;
860514c3069SLuca Coelho 	u16 buf_size = params->buf_size;
861bb81bb68SEmmanuel Grumbach 	bool amsdu = params->amsdu;
86210b2b201SSara Sharon 	u16 timeout = params->timeout;
863e705c121SKalle Valo 
864e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
865e705c121SKalle Valo 		     sta->addr, tid, action);
866e705c121SKalle Valo 
867e705c121SKalle Valo 	if (!(mvm->nvm_data->sku_cap_11n_enable))
868e705c121SKalle Valo 		return -EACCES;
869e705c121SKalle Valo 
870e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
871e705c121SKalle Valo 
872e705c121SKalle Valo 	switch (action) {
873e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_START:
874b0ffe455SJohannes Berg 		if (iwl_mvm_vif_from_mac80211(vif)->ap_sta_id ==
875b0ffe455SJohannes Berg 				iwl_mvm_sta_from_mac80211(sta)->sta_id) {
876b0ffe455SJohannes Berg 			struct iwl_mvm_vif *mvmvif;
877b0ffe455SJohannes Berg 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
878b0ffe455SJohannes Berg 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
879b0ffe455SJohannes Berg 
880b0ffe455SJohannes Berg 			mdata->opened_rx_ba_sessions = true;
881b0ffe455SJohannes Berg 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
882b0ffe455SJohannes Berg 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
883b0ffe455SJohannes Berg 		}
884e78da25eSJohannes Berg 		if (!iwl_enable_rx_ampdu()) {
885e705c121SKalle Valo 			ret = -EINVAL;
886e705c121SKalle Valo 			break;
887e705c121SKalle Valo 		}
88810b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
88910b2b201SSara Sharon 					 timeout);
890e705c121SKalle Valo 		break;
891e705c121SKalle Valo 	case IEEE80211_AMPDU_RX_STOP:
89210b2b201SSara Sharon 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
89310b2b201SSara Sharon 					 timeout);
894e705c121SKalle Valo 		break;
895e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_START:
896e78da25eSJohannes Berg 		if (!iwl_enable_tx_ampdu()) {
897e705c121SKalle Valo 			ret = -EINVAL;
898e705c121SKalle Valo 			break;
899e705c121SKalle Valo 		}
900e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
901e705c121SKalle Valo 		break;
902e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_CONT:
903e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
904e705c121SKalle Valo 		break;
905e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
906e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
907e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
908e705c121SKalle Valo 		break;
909e705c121SKalle Valo 	case IEEE80211_AMPDU_TX_OPERATIONAL:
910bb81bb68SEmmanuel Grumbach 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
911bb81bb68SEmmanuel Grumbach 					      buf_size, amsdu);
912e705c121SKalle Valo 		break;
913e705c121SKalle Valo 	default:
914e705c121SKalle Valo 		WARN_ON_ONCE(1);
915e705c121SKalle Valo 		ret = -EINVAL;
916e705c121SKalle Valo 		break;
917e705c121SKalle Valo 	}
918e705c121SKalle Valo 
919e705c121SKalle Valo 	if (!ret) {
920e705c121SKalle Valo 		u16 rx_ba_ssn = 0;
921e705c121SKalle Valo 
922e705c121SKalle Valo 		if (action == IEEE80211_AMPDU_RX_START)
923e705c121SKalle Valo 			rx_ba_ssn = *ssn;
924e705c121SKalle Valo 
925e705c121SKalle Valo 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
926e705c121SKalle Valo 					    rx_ba_ssn, action);
927e705c121SKalle Valo 	}
928e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
929e705c121SKalle Valo 
930e705c121SKalle Valo 	return ret;
931e705c121SKalle Valo }
932e705c121SKalle Valo 
933e705c121SKalle Valo static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
934e705c121SKalle Valo 				     struct ieee80211_vif *vif)
935e705c121SKalle Valo {
936e705c121SKalle Valo 	struct iwl_mvm *mvm = data;
937e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
938e705c121SKalle Valo 
939e705c121SKalle Valo 	mvmvif->uploaded = false;
9400ae98812SSara Sharon 	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
941e705c121SKalle Valo 
942e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
943e705c121SKalle Valo 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
944e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
945e705c121SKalle Valo 
946e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
947e705c121SKalle Valo 	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
94886e177d8SGregory Greenman 	memset(&mvmvif->probe_resp_data, 0, sizeof(mvmvif->probe_resp_data));
949e705c121SKalle Valo }
950e705c121SKalle Valo 
951e705c121SKalle Valo static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
952e705c121SKalle Valo {
953fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
954e705c121SKalle Valo 
9559bf13beeSJohannes Berg 	mvm->cur_aid = 0;
9569bf13beeSJohannes Berg 
957e705c121SKalle Valo 	mvm->scan_status = 0;
958e705c121SKalle Valo 	mvm->ps_disabled = false;
959b3500b47SEmmanuel Grumbach 	mvm->rfkill_safe_init_done = false;
960e705c121SKalle Valo 
961e705c121SKalle Valo 	/* just in case one was running */
962305d236eSEliad Peller 	iwl_mvm_cleanup_roc_te(mvm);
963e705c121SKalle Valo 	ieee80211_remain_on_channel_expired(mvm->hw);
964e705c121SKalle Valo 
965fc36ffdaSJohannes Berg 	iwl_mvm_ftm_restart(mvm);
966fc36ffdaSJohannes Berg 
967e705c121SKalle Valo 	/*
968e705c121SKalle Valo 	 * cleanup all interfaces, even inactive ones, as some might have
969e705c121SKalle Valo 	 * gone down during the HW restart
970e705c121SKalle Valo 	 */
971e705c121SKalle Valo 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
972e705c121SKalle Valo 
973e705c121SKalle Valo 	mvm->p2p_device_vif = NULL;
974e705c121SKalle Valo 
975e705c121SKalle Valo 	iwl_mvm_reset_phy_ctxts(mvm);
9769c3deeb5SLuca Coelho 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
977e705c121SKalle Valo 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
978e705c121SKalle Valo 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
979e705c121SKalle Valo 
980e705c121SKalle Valo 	ieee80211_wake_queues(mvm->hw);
981e705c121SKalle Valo 
982e705c121SKalle Valo 	mvm->rx_ba_sessions = 0;
9837174beb6SJohannes Berg 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
984baf41bc3SShaul Triebitz 	mvm->monitor_on = false;
985e705c121SKalle Valo 
986e705c121SKalle Valo 	/* keep statistics ticking */
987e705c121SKalle Valo 	iwl_mvm_accu_radio_stats(mvm);
988e705c121SKalle Valo }
989e705c121SKalle Valo 
990e705c121SKalle Valo int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
991e705c121SKalle Valo {
992e705c121SKalle Valo 	int ret;
993e705c121SKalle Valo 
994e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
995e705c121SKalle Valo 
9966d19a5ebSEmmanuel Grumbach 	ret = iwl_mvm_mei_get_ownership(mvm);
9976d19a5ebSEmmanuel Grumbach 	if (ret)
9986d19a5ebSEmmanuel Grumbach 		return ret;
9996d19a5ebSEmmanuel Grumbach 
10006d19a5ebSEmmanuel Grumbach 	if (mvm->mei_nvm_data) {
10016d19a5ebSEmmanuel Grumbach 		/* We got the NIC, we can now free the MEI NVM data */
10026d19a5ebSEmmanuel Grumbach 		kfree(mvm->mei_nvm_data);
10036d19a5ebSEmmanuel Grumbach 		mvm->mei_nvm_data = NULL;
10046d19a5ebSEmmanuel Grumbach 
10056d19a5ebSEmmanuel Grumbach 		/*
10066d19a5ebSEmmanuel Grumbach 		 * We can't free the nvm_data we allocated based on the SAP
10076d19a5ebSEmmanuel Grumbach 		 * data because we registered to cfg80211 with the channels
10086d19a5ebSEmmanuel Grumbach 		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
10096d19a5ebSEmmanuel Grumbach 		 * just in order to be able free it later.
10106d19a5ebSEmmanuel Grumbach 		 * NULLify nvm_data so that we will read the NVM from the
10116d19a5ebSEmmanuel Grumbach 		 * firmware this time.
10126d19a5ebSEmmanuel Grumbach 		 */
10136d19a5ebSEmmanuel Grumbach 		mvm->temp_nvm_data = mvm->nvm_data;
10146d19a5ebSEmmanuel Grumbach 		mvm->nvm_data = NULL;
10156d19a5ebSEmmanuel Grumbach 	}
10166d19a5ebSEmmanuel Grumbach 
1017bf8b286fSJohannes Berg 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1018bf8b286fSJohannes Berg 		/*
1019bf8b286fSJohannes Berg 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1020bf8b286fSJohannes Berg 		 * so later code will - from now on - see that we're doing it.
1021bf8b286fSJohannes Berg 		 */
1022bf8b286fSJohannes Berg 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1023bf8b286fSJohannes Berg 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1024e705c121SKalle Valo 		/* Clean up some internal and mac80211 state on restart */
1025e705c121SKalle Valo 		iwl_mvm_restart_cleanup(mvm);
1026a42b2af3SLuca Coelho 	}
1027e705c121SKalle Valo 	ret = iwl_mvm_up(mvm);
1028e705c121SKalle Valo 
1029b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1030b108d8c7SShahar S Matityahu 			       NULL);
1031b108d8c7SShahar S Matityahu 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1032b108d8c7SShahar S Matityahu 			       NULL);
1033da2eb669SSara Sharon 
1034e8fe3b41SIlan Peer 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1035e8fe3b41SIlan Peer 
1036e705c121SKalle Valo 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1037e705c121SKalle Valo 		/* Something went wrong - we need to finish some cleanup
1038e705c121SKalle Valo 		 * that normally iwl_mvm_mac_restart_complete() below
1039e705c121SKalle Valo 		 * would do.
1040e705c121SKalle Valo 		 */
1041e705c121SKalle Valo 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1042e705c121SKalle Valo 	}
1043e705c121SKalle Valo 
1044e705c121SKalle Valo 	return ret;
1045e705c121SKalle Valo }
1046e705c121SKalle Valo 
1047e705c121SKalle Valo static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1048e705c121SKalle Valo {
1049e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1050e705c121SKalle Valo 	int ret;
10515283dd67SMordechay Goodstein 	int retry, max_retry = 0;
1052e705c121SKalle Valo 
1053e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
10545283dd67SMordechay Goodstein 
10555283dd67SMordechay Goodstein 	/* we are starting the mac not in error flow, and restart is enabled */
10565283dd67SMordechay Goodstein 	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
10575283dd67SMordechay Goodstein 	    iwlwifi_mod_params.fw_restart) {
10585283dd67SMordechay Goodstein 		max_retry = IWL_MAX_INIT_RETRY;
10595283dd67SMordechay Goodstein 		/*
10605283dd67SMordechay Goodstein 		 * This will prevent mac80211 recovery flows to trigger during
10615283dd67SMordechay Goodstein 		 * init failures
10625283dd67SMordechay Goodstein 		 */
10635283dd67SMordechay Goodstein 		set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
10645283dd67SMordechay Goodstein 	}
10655283dd67SMordechay Goodstein 
10665283dd67SMordechay Goodstein 	for (retry = 0; retry <= max_retry; retry++) {
1067e705c121SKalle Valo 		ret = __iwl_mvm_mac_start(mvm);
10685283dd67SMordechay Goodstein 		if (!ret)
10695283dd67SMordechay Goodstein 			break;
10705283dd67SMordechay Goodstein 
1071b8133439SAvraham Stern 		/*
1072b8133439SAvraham Stern 		 * In PLDR sync PCI re-enumeration is needed. no point to retry
1073b8133439SAvraham Stern 		 * mac start before that.
1074b8133439SAvraham Stern 		 */
1075b8133439SAvraham Stern 		if (mvm->pldr_sync) {
1076b8133439SAvraham Stern 			iwl_mei_alive_notif(false);
1077b8133439SAvraham Stern 			iwl_trans_pcie_remove(mvm->trans, true);
1078b8133439SAvraham Stern 			break;
1079b8133439SAvraham Stern 		}
1080b8133439SAvraham Stern 
10815283dd67SMordechay Goodstein 		IWL_ERR(mvm, "mac start retry %d\n", retry);
10825283dd67SMordechay Goodstein 	}
10835283dd67SMordechay Goodstein 	clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
10845283dd67SMordechay Goodstein 
1085e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1086e705c121SKalle Valo 
10877ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
10887ce1f215SEmmanuel Grumbach 
1089e705c121SKalle Valo 	return ret;
1090e705c121SKalle Valo }
1091e705c121SKalle Valo 
1092e705c121SKalle Valo static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1093e705c121SKalle Valo {
1094e705c121SKalle Valo 	int ret;
1095e705c121SKalle Valo 
1096e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1097e705c121SKalle Valo 
1098e705c121SKalle Valo 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
10994d4183c4SEmmanuel Grumbach 
1100e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1101e705c121SKalle Valo 	if (ret)
1102e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1103e705c121SKalle Valo 			ret);
1104e705c121SKalle Valo 
1105f130bb75SMordechay Goodstein 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1106f130bb75SMordechay Goodstein 
1107e705c121SKalle Valo 	/*
1108e705c121SKalle Valo 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1109e705c121SKalle Valo 	 * of packets the FW sent out, so we must reconnect.
1110e705c121SKalle Valo 	 */
1111e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
1112e705c121SKalle Valo 
1113e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1114e705c121SKalle Valo }
1115e705c121SKalle Valo 
1116e705c121SKalle Valo static void
1117e705c121SKalle Valo iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1118e705c121SKalle Valo 			      enum ieee80211_reconfig_type reconfig_type)
1119e705c121SKalle Valo {
1120e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1121e705c121SKalle Valo 
1122e705c121SKalle Valo 	switch (reconfig_type) {
1123e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_RESTART:
1124e705c121SKalle Valo 		iwl_mvm_restart_complete(mvm);
1125e705c121SKalle Valo 		break;
1126e705c121SKalle Valo 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1127e705c121SKalle Valo 		break;
1128e705c121SKalle Valo 	}
1129e705c121SKalle Valo }
1130e705c121SKalle Valo 
1131e705c121SKalle Valo void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1132e705c121SKalle Valo {
1133e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1134e705c121SKalle Valo 
1135b68bd2e3SIlan Peer 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1136b68bd2e3SIlan Peer 
1137e705c121SKalle Valo 	/* firmware counters are obviously reset now, but we shouldn't
1138e705c121SKalle Valo 	 * partially track so also clear the fw_reset_accu counters.
1139e705c121SKalle Valo 	 */
1140e705c121SKalle Valo 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1141e705c121SKalle Valo 
1142e705c121SKalle Valo 	/* async_handlers_wk is now blocked */
1143e705c121SKalle Valo 
1144971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) < 12)
1145f327236dSSharon 		iwl_mvm_rm_aux_sta(mvm);
1146f327236dSSharon 
1147fcb6b92aSChaya Rachel Ivgi 	iwl_mvm_stop_device(mvm);
1148e705c121SKalle Valo 
1149e705c121SKalle Valo 	iwl_mvm_async_handlers_purge(mvm);
1150e705c121SKalle Valo 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1151e705c121SKalle Valo 
1152e705c121SKalle Valo 	/*
1153155f7e04SEmmanuel Grumbach 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1154155f7e04SEmmanuel Grumbach 	 * hw (as restart_complete() won't be called in this case) and mac80211
1155155f7e04SEmmanuel Grumbach 	 * won't execute the restart.
1156e705c121SKalle Valo 	 * But make sure to cleanup interfaces that have gone down before/during
1157e705c121SKalle Valo 	 * HW restart was requested.
1158e705c121SKalle Valo 	 */
1159155f7e04SEmmanuel Grumbach 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1160155f7e04SEmmanuel Grumbach 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1161155f7e04SEmmanuel Grumbach 			       &mvm->status))
1162e705c121SKalle Valo 		ieee80211_iterate_interfaces(mvm->hw, 0,
1163e705c121SKalle Valo 					     iwl_mvm_cleanup_iterator, mvm);
1164e705c121SKalle Valo 
1165e705c121SKalle Valo 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1166e705c121SKalle Valo 	 * make sure there's nothing left there and warn if any is found.
1167e705c121SKalle Valo 	 */
1168e705c121SKalle Valo 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1169e705c121SKalle Valo 		int i;
1170e705c121SKalle Valo 
1171e705c121SKalle Valo 		for (i = 0; i < mvm->max_scans; i++) {
1172e705c121SKalle Valo 			if (WARN_ONCE(mvm->scan_uid_status[i],
1173e705c121SKalle Valo 				      "UMAC scan UID %d status was not cleaned\n",
1174e705c121SKalle Valo 				      i))
1175e705c121SKalle Valo 				mvm->scan_uid_status[i] = 0;
1176e705c121SKalle Valo 		}
1177e705c121SKalle Valo 	}
1178e705c121SKalle Valo }
1179e705c121SKalle Valo 
1180e705c121SKalle Valo static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1181e705c121SKalle Valo {
1182e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1183e705c121SKalle Valo 
1184e705c121SKalle Valo 	flush_work(&mvm->async_handlers_wk);
118524afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
1186771147b0SJohannes Berg 
1187771147b0SJohannes Berg 	/*
1188771147b0SJohannes Berg 	 * Lock and clear the firmware running bit here already, so that
1189771147b0SJohannes Berg 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1190771147b0SJohannes Berg 	 * be able to proceed. This is important here because one of those
11917174beb6SJohannes Berg 	 * debugfs files causes the firmware dump to be triggered, and if we
1192771147b0SJohannes Berg 	 * don't stop debugfs accesses before canceling that it could be
1193771147b0SJohannes Berg 	 * retriggered after we flush it but before we've cleared the bit.
1194771147b0SJohannes Berg 	 */
1195771147b0SJohannes Berg 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1196771147b0SJohannes Berg 
1197d3a108a4SAndrei Otcheretianski 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
119869e04642SLuca Coelho 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1199e705c121SKalle Valo 
1200f9084775SNathan Errera 	/*
1201f9084775SNathan Errera 	 * The work item could be running or queued if the
1202f9084775SNathan Errera 	 * ROC time event stops just as we get here.
1203f9084775SNathan Errera 	 */
1204f9084775SNathan Errera 	flush_work(&mvm->roc_done_wk);
1205f9084775SNathan Errera 
12067ce1f215SEmmanuel Grumbach 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
12077ce1f215SEmmanuel Grumbach 
1208e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1209e705c121SKalle Valo 	__iwl_mvm_mac_stop(mvm);
1210e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1211e705c121SKalle Valo 
1212e705c121SKalle Valo 	/*
1213e705c121SKalle Valo 	 * The worker might have been waiting for the mutex, let it run and
1214e705c121SKalle Valo 	 * discover that its list is now empty.
1215e705c121SKalle Valo 	 */
1216e705c121SKalle Valo 	cancel_work_sync(&mvm->async_handlers_wk);
1217e705c121SKalle Valo }
1218e705c121SKalle Valo 
12191ab26632SMiri Korenblit struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1220e705c121SKalle Valo {
1221e705c121SKalle Valo 	u16 i;
1222e705c121SKalle Valo 
1223e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1224e705c121SKalle Valo 
1225e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++)
1226e705c121SKalle Valo 		if (!mvm->phy_ctxts[i].ref)
1227e705c121SKalle Valo 			return &mvm->phy_ctxts[i];
1228e705c121SKalle Valo 
1229e705c121SKalle Valo 	IWL_ERR(mvm, "No available PHY context\n");
1230e705c121SKalle Valo 	return NULL;
1231e705c121SKalle Valo }
1232e705c121SKalle Valo 
1233e705c121SKalle Valo static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1234e705c121SKalle Valo 				s16 tx_power)
1235e705c121SKalle Valo {
1236971cbe50SJohannes Berg 	u32 cmd_id = REDUCE_TX_POWER_CMD;
12370791c2fcSHaim Dreyfuss 	int len;
1238216cdfb5SLuca Coelho 	struct iwl_dev_tx_power_cmd cmd = {
1239216cdfb5SLuca Coelho 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1240216cdfb5SLuca Coelho 		.common.mac_context_id =
1241e705c121SKalle Valo 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1242216cdfb5SLuca Coelho 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1243e705c121SKalle Valo 	};
1244971cbe50SJohannes Berg 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1245e80bfd11SMordechay Goodstein 					   IWL_FW_CMD_VER_UNKNOWN);
1246e705c121SKalle Valo 
1247e705c121SKalle Valo 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1248216cdfb5SLuca Coelho 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1249e705c121SKalle Valo 
1250b0aa02b3SAyala Barazani 	if (cmd_ver == 7)
1251b0aa02b3SAyala Barazani 		len = sizeof(cmd.v7);
1252b0aa02b3SAyala Barazani 	else if (cmd_ver == 6)
1253fbb7957dSLuca Coelho 		len = sizeof(cmd.v6);
1254fbb7957dSLuca Coelho 	else if (fw_has_api(&mvm->fw->ucode_capa,
12550791c2fcSHaim Dreyfuss 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
12560791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v5);
12570791c2fcSHaim Dreyfuss 	else if (fw_has_capa(&mvm->fw->ucode_capa,
12580791c2fcSHaim Dreyfuss 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
12590791c2fcSHaim Dreyfuss 		len = sizeof(cmd.v4);
12600791c2fcSHaim Dreyfuss 	else
1261216cdfb5SLuca Coelho 		len = sizeof(cmd.v3);
1262216cdfb5SLuca Coelho 
1263216cdfb5SLuca Coelho 	/* all structs have the same common part, add it */
1264216cdfb5SLuca Coelho 	len += sizeof(cmd.common);
1265e705c121SKalle Valo 
1266971cbe50SJohannes Berg 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1267e705c121SKalle Valo }
1268e705c121SKalle Valo 
1269f6780614SSara Sharon static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1270f6780614SSara Sharon 				       struct ieee80211_vif *vif)
1271f6780614SSara Sharon {
1272f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1273f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1274f6780614SSara Sharon 	int ret;
1275f6780614SSara Sharon 
1276f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
1277f6780614SSara Sharon 
1278f6780614SSara Sharon 	if (vif->type == NL80211_IFTYPE_STATION) {
1279f6780614SSara Sharon 		struct iwl_mvm_sta *mvmsta;
1280f6780614SSara Sharon 
1281f6780614SSara Sharon 		mvmvif->csa_bcn_pending = false;
1282f6780614SSara Sharon 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
1283f6780614SSara Sharon 							  mvmvif->ap_sta_id);
1284f6780614SSara Sharon 
1285f6780614SSara Sharon 		if (WARN_ON(!mvmsta)) {
1286f6780614SSara Sharon 			ret = -EIO;
1287f6780614SSara Sharon 			goto out_unlock;
1288f6780614SSara Sharon 		}
1289f6780614SSara Sharon 
1290f6780614SSara Sharon 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1291f6780614SSara Sharon 
1292f6780614SSara Sharon 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1293f6780614SSara Sharon 
12940202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
12950202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1296f6780614SSara Sharon 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1297f6780614SSara Sharon 			if (ret)
1298f6780614SSara Sharon 				goto out_unlock;
1299f6780614SSara Sharon 
1300f6780614SSara Sharon 			iwl_mvm_stop_session_protection(mvm, vif);
1301f6780614SSara Sharon 		}
13020202bcf0SEmmanuel Grumbach 	}
1303f6780614SSara Sharon 
1304f6780614SSara Sharon 	mvmvif->ps_disabled = false;
1305f6780614SSara Sharon 
1306f6780614SSara Sharon 	ret = iwl_mvm_power_update_ps(mvm);
1307f6780614SSara Sharon 
1308f6780614SSara Sharon out_unlock:
1309caf46377SSara Sharon 	if (mvmvif->csa_failed)
1310caf46377SSara Sharon 		ret = -EIO;
1311f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1312f6780614SSara Sharon 
1313f6780614SSara Sharon 	return ret;
1314f6780614SSara Sharon }
1315f6780614SSara Sharon 
1316f6780614SSara Sharon static void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1317f6780614SSara Sharon 					 struct ieee80211_vif *vif)
1318f6780614SSara Sharon {
1319f6780614SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1320f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1321f6780614SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
1322f6780614SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1323f6780614SSara Sharon 							  mvmvif->color)),
1324f6780614SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1325f6780614SSara Sharon 	};
1326f6780614SSara Sharon 
1327ad12b231SNathan Errera 	/*
1328ad12b231SNathan Errera 	 * In the new flow since FW is in charge of the timing,
1329ad12b231SNathan Errera 	 * if driver has canceled the channel switch he will receive the
1330ad12b231SNathan Errera 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1331ad12b231SNathan Errera 	 */
1332ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1333ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1334ad12b231SNathan Errera 		return;
1335ad12b231SNathan Errera 
1336f6780614SSara Sharon 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1337f6780614SSara Sharon 
1338f6780614SSara Sharon 	mutex_lock(&mvm->mutex);
133958ddd9b6SEmmanuel Grumbach 	if (!fw_has_capa(&mvm->fw->ucode_capa,
134058ddd9b6SEmmanuel Grumbach 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
134158ddd9b6SEmmanuel Grumbach 		iwl_mvm_remove_csa_period(mvm, vif);
134258ddd9b6SEmmanuel Grumbach 	else
1343f6780614SSara Sharon 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1344f6780614SSara Sharon 					     WIDE_ID(MAC_CONF_GROUP,
1345f6780614SSara Sharon 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1346f6780614SSara Sharon 					     0, sizeof(cmd), &cmd));
1347caf46377SSara Sharon 	mvmvif->csa_failed = true;
1348f6780614SSara Sharon 	mutex_unlock(&mvm->mutex);
1349f6780614SSara Sharon 
1350caf46377SSara Sharon 	iwl_mvm_post_channel_switch(hw, vif);
1351f6780614SSara Sharon }
1352f6780614SSara Sharon 
13531ab26632SMiri Korenblit void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1354f6780614SSara Sharon {
1355f6780614SSara Sharon 	struct iwl_mvm_vif *mvmvif;
1356f6780614SSara Sharon 	struct ieee80211_vif *vif;
1357f6780614SSara Sharon 
1358f6780614SSara Sharon 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1359f6780614SSara Sharon 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1360f6780614SSara Sharon 
136170162580SShaul Triebitz 	/* Trigger disconnect (should clear the CSA state) */
1362f6780614SSara Sharon 	ieee80211_chswitch_done(vif, false);
1363f6780614SSara Sharon }
1364f6780614SSara Sharon 
13655abf3154SMordechay Goodstein static u8
13665abf3154SMordechay Goodstein iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
13675abf3154SMordechay Goodstein {
13685abf3154SMordechay Goodstein 	int data_start;
13695abf3154SMordechay Goodstein 	int control_start;
13705abf3154SMordechay Goodstein 	int bw;
13715abf3154SMordechay Goodstein 
13725abf3154SMordechay Goodstein 	if (chandef->width == NL80211_CHAN_WIDTH_320)
13735abf3154SMordechay Goodstein 		bw = 320;
13745abf3154SMordechay Goodstein 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
13755abf3154SMordechay Goodstein 		bw = 160;
13765abf3154SMordechay Goodstein 	else
13775abf3154SMordechay Goodstein 		return 0;
13785abf3154SMordechay Goodstein 
13795abf3154SMordechay Goodstein 	/* data is bw wide so the start is half the width */
13805abf3154SMordechay Goodstein 	data_start = chandef->center_freq1 - bw / 2;
13815abf3154SMordechay Goodstein 	/* control is 20Mhz width */
13825abf3154SMordechay Goodstein 	control_start = chandef->chan->center_freq - 10;
13835abf3154SMordechay Goodstein 
13845abf3154SMordechay Goodstein 	return (control_start - data_start) / 80;
13855abf3154SMordechay Goodstein }
13865abf3154SMordechay Goodstein 
13871ab26632SMiri Korenblit /*
13881ab26632SMiri Korenblit  * Returns true if addding the interface is done
13891ab26632SMiri Korenblit  * (either with success or failure)
13901ab26632SMiri Korenblit  */
13911ab26632SMiri Korenblit bool iwl_mvm_mac_add_interface_common(struct iwl_mvm *mvm,
13921ab26632SMiri Korenblit 				      struct ieee80211_hw *hw,
13931ab26632SMiri Korenblit 				      struct ieee80211_vif *vif, int *ret)
1394e705c121SKalle Valo {
1395e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
13961ab26632SMiri Korenblit 
13971ab26632SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
1398e705c121SKalle Valo 
1399e705c121SKalle Valo 	mvmvif->mvm = mvm;
140086e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
1401e705c121SKalle Valo 
1402e705c121SKalle Valo 	/*
1403e705c121SKalle Valo 	 * Not much to do here. The stack will not allow interface
1404e705c121SKalle Valo 	 * types or combinations that we didn't advertise, so we
1405e705c121SKalle Valo 	 * don't really have to check the types.
1406e705c121SKalle Valo 	 */
1407e705c121SKalle Valo 
1408e705c121SKalle Valo 	/* make sure that beacon statistics don't go backwards with FW reset */
1409e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1410e705c121SKalle Valo 		mvmvif->beacon_stats.accu_num_beacons +=
1411e705c121SKalle Valo 			mvmvif->beacon_stats.num_beacons;
1412e705c121SKalle Valo 
1413e705c121SKalle Valo 	/* Allocate resources for the MAC context, and add it to the fw  */
14141ab26632SMiri Korenblit 	*ret = iwl_mvm_mac_ctxt_init(mvm, vif);
14151ab26632SMiri Korenblit 	if (*ret)
14161ab26632SMiri Korenblit 		return true;
1417e705c121SKalle Valo 
1418698478c4SSara Sharon 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1419698478c4SSara Sharon 
1420e705c121SKalle Valo 	/*
1421e705c121SKalle Valo 	 * The AP binding flow can be done only after the beacon
1422e705c121SKalle Valo 	 * template is configured (which happens only in the mac80211
1423e705c121SKalle Valo 	 * start_ap() flow), and adding the broadcast station can happen
1424e705c121SKalle Valo 	 * only after the binding.
1425e705c121SKalle Valo 	 * In addition, since modifying the MAC before adding a bcast
1426e705c121SKalle Valo 	 * station is not allowed by the FW, delay the adding of MAC context to
1427e705c121SKalle Valo 	 * the point where we can also add the bcast station.
1428e705c121SKalle Valo 	 * In short: there's not much we can do at this point, other than
1429e705c121SKalle Valo 	 * allocating resources :)
1430e705c121SKalle Valo 	 */
1431e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1432e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
14331ab26632SMiri Korenblit 		iwl_mvm_vif_dbgfs_register(mvm, vif);
14341ab26632SMiri Korenblit 		return true;
14351ab26632SMiri Korenblit 	}
14361ab26632SMiri Korenblit 
14371ab26632SMiri Korenblit 	mvmvif->features |= hw->netdev_features;
14381ab26632SMiri Korenblit 	return false;
14391ab26632SMiri Korenblit }
14401ab26632SMiri Korenblit 
14411ab26632SMiri Korenblit static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
14421ab26632SMiri Korenblit 					 struct ieee80211_vif *vif)
14431ab26632SMiri Korenblit {
14441ab26632SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
14451ab26632SMiri Korenblit 	int ret;
14461ab26632SMiri Korenblit 
14471ab26632SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
14481ab26632SMiri Korenblit 
1449e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1450e705c121SKalle Valo 	if (ret) {
1451e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
14521ab26632SMiri Korenblit 		return ret;
1453e705c121SKalle Valo 	}
1454e705c121SKalle Valo 
145526d6c16bSSara Sharon 	/*
145626d6c16bSSara Sharon 	 * Only queue for this station is the mcast queue,
145726d6c16bSSara Sharon 	 * which shouldn't be in TFD mask anyway
145826d6c16bSSara Sharon 	 */
14591ab26632SMiri Korenblit 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta, 0, vif->type,
1460ced19f26SSara Sharon 					IWL_STA_MULTICAST);
1461e705c121SKalle Valo }
1462e705c121SKalle Valo 
14631ab26632SMiri Korenblit static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
14641ab26632SMiri Korenblit 				     struct ieee80211_vif *vif)
14651ab26632SMiri Korenblit {
14661ab26632SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
14671ab26632SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
14681ab26632SMiri Korenblit 	int ret;
14691ab26632SMiri Korenblit 
14701ab26632SMiri Korenblit 	mutex_lock(&mvm->mutex);
14711ab26632SMiri Korenblit 
14721ab26632SMiri Korenblit 	/* Common for MLD and non-MLD API */
14731ab26632SMiri Korenblit 	if (iwl_mvm_mac_add_interface_common(mvm, hw, vif, &ret))
14741ab26632SMiri Korenblit 		goto out;
1475e705c121SKalle Valo 
1476e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1477e705c121SKalle Valo 	if (ret)
147898c0de7bSMiri Korenblit 		goto out_unlock;
1479e705c121SKalle Valo 
1480e705c121SKalle Valo 	ret = iwl_mvm_power_update_mac(mvm);
1481e705c121SKalle Valo 	if (ret)
1482e705c121SKalle Valo 		goto out_remove_mac;
1483e705c121SKalle Valo 
1484e705c121SKalle Valo 	/* beacon filtering */
1485e705c121SKalle Valo 	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1486e705c121SKalle Valo 	if (ret)
1487e705c121SKalle Valo 		goto out_remove_mac;
1488e705c121SKalle Valo 
1489e705c121SKalle Valo 	if (!mvm->bf_allowed_vif &&
1490e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1491e705c121SKalle Valo 		mvm->bf_allowed_vif = mvmvif;
1492e705c121SKalle Valo 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1493e705c121SKalle Valo 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1494e705c121SKalle Valo 	}
1495e705c121SKalle Valo 
1496e705c121SKalle Valo 	/*
1497e705c121SKalle Valo 	 * P2P_DEVICE interface does not have a channel context assigned to it,
1498e705c121SKalle Valo 	 * so a dedicated PHY context is allocated to it and the corresponding
1499e705c121SKalle Valo 	 * MAC context is bound to it at this stage.
1500e705c121SKalle Valo 	 */
1501e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1502e705c121SKalle Valo 
1503e705c121SKalle Valo 		mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1504e705c121SKalle Valo 		if (!mvmvif->phy_ctxt) {
1505e705c121SKalle Valo 			ret = -ENOSPC;
1506e705c121SKalle Valo 			goto out_free_bf;
1507e705c121SKalle Valo 		}
1508e705c121SKalle Valo 
1509e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1510e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
1511e705c121SKalle Valo 		if (ret)
1512e705c121SKalle Valo 			goto out_unref_phy;
1513e705c121SKalle Valo 
1514d197358bSLuca Coelho 		ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1515e705c121SKalle Valo 		if (ret)
1516e705c121SKalle Valo 			goto out_unbind;
1517e705c121SKalle Valo 
1518e705c121SKalle Valo 		/* Save a pointer to p2p device vif, so it can later be used to
1519e705c121SKalle Valo 		 * update the p2p device MAC when a GO is started/stopped */
1520e705c121SKalle Valo 		mvm->p2p_device_vif = vif;
1521e705c121SKalle Valo 	}
1522e705c121SKalle Valo 
1523b0ffe455SJohannes Berg 	iwl_mvm_tcm_add_vif(mvm, vif);
1524f6780614SSara Sharon 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1525f6780614SSara Sharon 			  iwl_mvm_channel_switch_disconnect_wk);
1526b0ffe455SJohannes Berg 
15275abf3154SMordechay Goodstein 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1528baf41bc3SShaul Triebitz 		mvm->monitor_on = true;
15295abf3154SMordechay Goodstein 		mvm->monitor_p80 =
15305abf3154SMordechay Goodstein 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chandef);
15315abf3154SMordechay Goodstein 	}
1532baf41bc3SShaul Triebitz 
1533e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_register(mvm, vif);
15346d19a5ebSEmmanuel Grumbach 
15356d19a5ebSEmmanuel Grumbach 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
15366d19a5ebSEmmanuel Grumbach 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
15376d19a5ebSEmmanuel Grumbach 	    !mvm->csme_vif && mvm->mei_registered) {
15386d19a5ebSEmmanuel Grumbach 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
15396d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
15406d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = vif;
15416d19a5ebSEmmanuel Grumbach 	}
15426d19a5ebSEmmanuel Grumbach 
15431ab26632SMiri Korenblit out:
15441ab26632SMiri Korenblit 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
15451ab26632SMiri Korenblit 		     vif->type == NL80211_IFTYPE_ADHOC))
15461ab26632SMiri Korenblit 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
15471ab26632SMiri Korenblit 
1548e705c121SKalle Valo 	goto out_unlock;
1549e705c121SKalle Valo 
1550e705c121SKalle Valo  out_unbind:
1551e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
1552e705c121SKalle Valo  out_unref_phy:
1553e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1554e705c121SKalle Valo  out_free_bf:
1555e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1556e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1557e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1558e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1559e705c121SKalle Valo 	}
1560e705c121SKalle Valo  out_remove_mac:
1561e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
1562e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1563e705c121SKalle Valo  out_unlock:
1564e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1565e705c121SKalle Valo 
1566e705c121SKalle Valo 	return ret;
1567e705c121SKalle Valo }
1568e705c121SKalle Valo 
1569e705c121SKalle Valo static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1570e705c121SKalle Valo 					struct ieee80211_vif *vif)
1571e705c121SKalle Valo {
1572e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1573e705c121SKalle Valo 		/*
1574e705c121SKalle Valo 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1575e705c121SKalle Valo 		 * We assume here that all the packets sent to the OFFCHANNEL
1576e705c121SKalle Valo 		 * queue are sent in ROC session.
1577e705c121SKalle Valo 		 */
1578e705c121SKalle Valo 		flush_work(&mvm->roc_done_wk);
1579e705c121SKalle Valo 	}
1580e705c121SKalle Valo }
1581e705c121SKalle Valo 
158260efeca1SMiri Korenblit /* This function is doing the common part of removing the interface for
158360efeca1SMiri Korenblit  * both - MLD and non-MLD modes. Returns true if removing the interface
158460efeca1SMiri Korenblit  * is done
158560efeca1SMiri Korenblit  */
158660efeca1SMiri Korenblit bool iwl_mvm_mac_remove_interface_common(struct ieee80211_hw *hw,
1587e705c121SKalle Valo 					 struct ieee80211_vif *vif)
1588e705c121SKalle Valo {
1589e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1590e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
159186e177d8SGregory Greenman 	struct iwl_probe_resp_data *probe_data;
1592e705c121SKalle Valo 
1593e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
1594e705c121SKalle Valo 
1595b0ffe455SJohannes Berg 	if (!(vif->type == NL80211_IFTYPE_AP ||
1596b0ffe455SJohannes Berg 	      vif->type == NL80211_IFTYPE_ADHOC))
1597b0ffe455SJohannes Berg 		iwl_mvm_tcm_rm_vif(mvm, vif);
1598b0ffe455SJohannes Berg 
1599e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1600e705c121SKalle Valo 
16016d19a5ebSEmmanuel Grumbach 	if (vif == mvm->csme_vif) {
16026d19a5ebSEmmanuel Grumbach 		iwl_mei_set_netdev(NULL);
16036d19a5ebSEmmanuel Grumbach 		mvm->csme_vif = NULL;
16046d19a5ebSEmmanuel Grumbach 	}
16056d19a5ebSEmmanuel Grumbach 
160686e177d8SGregory Greenman 	probe_data = rcu_dereference_protected(mvmvif->probe_resp_data,
160786e177d8SGregory Greenman 					       lockdep_is_held(&mvm->mutex));
160886e177d8SGregory Greenman 	RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
160986e177d8SGregory Greenman 	if (probe_data)
161086e177d8SGregory Greenman 		kfree_rcu(probe_data, rcu_head);
161186e177d8SGregory Greenman 
1612e705c121SKalle Valo 	if (mvm->bf_allowed_vif == mvmvif) {
1613e705c121SKalle Valo 		mvm->bf_allowed_vif = NULL;
1614e705c121SKalle Valo 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1615e705c121SKalle Valo 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1616e705c121SKalle Valo 	}
1617e705c121SKalle Valo 
1618b73f9a4aSJohannes Berg 	if (vif->bss_conf.ftm_responder)
1619b73f9a4aSJohannes Berg 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1620b73f9a4aSJohannes Berg 
1621e705c121SKalle Valo 	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1622e705c121SKalle Valo 
1623e705c121SKalle Valo 	/*
1624e705c121SKalle Valo 	 * For AP/GO interface, the tear down of the resources allocated to the
1625e705c121SKalle Valo 	 * interface is be handled as part of the stop_ap flow.
1626e705c121SKalle Valo 	 */
1627e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP ||
1628e705c121SKalle Valo 	    vif->type == NL80211_IFTYPE_ADHOC) {
1629e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
1630e705c121SKalle Valo 		if (vif == mvm->noa_vif) {
1631e705c121SKalle Valo 			mvm->noa_vif = NULL;
1632e705c121SKalle Valo 			mvm->noa_duration = 0;
1633e705c121SKalle Valo 		}
1634e705c121SKalle Valo #endif
163560efeca1SMiri Korenblit 		return true;
1636e705c121SKalle Valo 	}
1637e705c121SKalle Valo 
163860efeca1SMiri Korenblit 	iwl_mvm_power_update_mac(mvm);
163960efeca1SMiri Korenblit 	return false;
164060efeca1SMiri Korenblit }
164160efeca1SMiri Korenblit 
164260efeca1SMiri Korenblit static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
164360efeca1SMiri Korenblit 					 struct ieee80211_vif *vif)
164460efeca1SMiri Korenblit {
164560efeca1SMiri Korenblit 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
164660efeca1SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
164760efeca1SMiri Korenblit 
164860efeca1SMiri Korenblit 	if (iwl_mvm_mac_remove_interface_common(hw, vif))
164960efeca1SMiri Korenblit 		goto out;
165060efeca1SMiri Korenblit 
1651e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1652e705c121SKalle Valo 		mvm->p2p_device_vif = NULL;
1653d197358bSLuca Coelho 		iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1654e705c121SKalle Valo 		iwl_mvm_binding_remove_vif(mvm, vif);
1655e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1656e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
1657e705c121SKalle Valo 	}
1658e705c121SKalle Valo 
1659e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1660e705c121SKalle Valo 
1661698478c4SSara Sharon 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1662698478c4SSara Sharon 
1663baf41bc3SShaul Triebitz 	if (vif->type == NL80211_IFTYPE_MONITOR)
1664baf41bc3SShaul Triebitz 		mvm->monitor_on = false;
1665baf41bc3SShaul Triebitz 
166660efeca1SMiri Korenblit out:
166760efeca1SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_AP ||
166860efeca1SMiri Korenblit 	    vif->type == NL80211_IFTYPE_ADHOC) {
166960efeca1SMiri Korenblit 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
167060efeca1SMiri Korenblit 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
167160efeca1SMiri Korenblit 	}
167260efeca1SMiri Korenblit 
1673e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1674e705c121SKalle Valo }
1675e705c121SKalle Valo 
1676e705c121SKalle Valo static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1677e705c121SKalle Valo {
1678e705c121SKalle Valo 	return 0;
1679e705c121SKalle Valo }
1680e705c121SKalle Valo 
1681e705c121SKalle Valo struct iwl_mvm_mc_iter_data {
1682e705c121SKalle Valo 	struct iwl_mvm *mvm;
1683e705c121SKalle Valo 	int port_id;
1684e705c121SKalle Valo };
1685e705c121SKalle Valo 
1686e705c121SKalle Valo static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1687e705c121SKalle Valo 				      struct ieee80211_vif *vif)
1688e705c121SKalle Valo {
1689e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data *data = _data;
1690e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
1691e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
169297bce57bSLuca Coelho 	struct iwl_host_cmd hcmd = {
169397bce57bSLuca Coelho 		.id = MCAST_FILTER_CMD,
169497bce57bSLuca Coelho 		.flags = CMD_ASYNC,
169597bce57bSLuca Coelho 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
169697bce57bSLuca Coelho 	};
1697e705c121SKalle Valo 	int ret, len;
1698e705c121SKalle Valo 
1699e705c121SKalle Valo 	/* if we don't have free ports, mcast frames will be dropped */
1700e705c121SKalle Valo 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1701e705c121SKalle Valo 		return;
1702e705c121SKalle Valo 
1703e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION ||
1704f276e20bSJohannes Berg 	    !vif->cfg.assoc)
1705e705c121SKalle Valo 		return;
1706e705c121SKalle Valo 
1707e705c121SKalle Valo 	cmd->port_id = data->port_id++;
1708e705c121SKalle Valo 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1709e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1710e705c121SKalle Valo 
171197bce57bSLuca Coelho 	hcmd.len[0] = len;
171297bce57bSLuca Coelho 	hcmd.data[0] = cmd;
171397bce57bSLuca Coelho 
171497bce57bSLuca Coelho 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1715e705c121SKalle Valo 	if (ret)
1716e705c121SKalle Valo 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1717e705c121SKalle Valo }
1718e705c121SKalle Valo 
1719e705c121SKalle Valo static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1720e705c121SKalle Valo {
1721e705c121SKalle Valo 	struct iwl_mvm_mc_iter_data iter_data = {
1722e705c121SKalle Valo 		.mvm = mvm,
1723e705c121SKalle Valo 	};
1724db66abeeSJohannes Berg 	int ret;
1725e705c121SKalle Valo 
1726e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1727e705c121SKalle Valo 
1728e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1729e705c121SKalle Valo 		return;
1730e705c121SKalle Valo 
1731e705c121SKalle Valo 	ieee80211_iterate_active_interfaces_atomic(
1732e705c121SKalle Valo 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1733e705c121SKalle Valo 		iwl_mvm_mc_iface_iterator, &iter_data);
1734db66abeeSJohannes Berg 
1735db66abeeSJohannes Berg 	/*
1736db66abeeSJohannes Berg 	 * Send a (synchronous) ech command so that we wait for the
1737db66abeeSJohannes Berg 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
1738db66abeeSJohannes Berg 	 * the interface iterator. Otherwise, we might get here over
1739db66abeeSJohannes Berg 	 * and over again (by userspace just sending a lot of these)
1740db66abeeSJohannes Berg 	 * and the CPU can send them faster than the firmware can
1741db66abeeSJohannes Berg 	 * process them.
1742db66abeeSJohannes Berg 	 * Note that the CPU is still faster - but with this we'll
1743db66abeeSJohannes Berg 	 * actually send fewer commands overall because the CPU will
1744db66abeeSJohannes Berg 	 * not schedule the work in mac80211 as frequently if it's
1745db66abeeSJohannes Berg 	 * still running when rescheduled (possibly multiple times).
1746db66abeeSJohannes Berg 	 */
1747db66abeeSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1748db66abeeSJohannes Berg 	if (ret)
1749db66abeeSJohannes Berg 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
1750e705c121SKalle Valo }
1751e705c121SKalle Valo 
1752e705c121SKalle Valo static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1753e705c121SKalle Valo 				     struct netdev_hw_addr_list *mc_list)
1754e705c121SKalle Valo {
1755e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1756e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd;
1757e705c121SKalle Valo 	struct netdev_hw_addr *addr;
1758e705c121SKalle Valo 	int addr_count;
1759e705c121SKalle Valo 	bool pass_all;
1760e705c121SKalle Valo 	int len;
1761e705c121SKalle Valo 
1762e705c121SKalle Valo 	addr_count = netdev_hw_addr_list_count(mc_list);
1763e705c121SKalle Valo 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1764e705c121SKalle Valo 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1765e705c121SKalle Valo 	if (pass_all)
1766e705c121SKalle Valo 		addr_count = 0;
1767e705c121SKalle Valo 
1768e705c121SKalle Valo 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1769e705c121SKalle Valo 	cmd = kzalloc(len, GFP_ATOMIC);
1770e705c121SKalle Valo 	if (!cmd)
1771e705c121SKalle Valo 		return 0;
1772e705c121SKalle Valo 
1773e705c121SKalle Valo 	if (pass_all) {
1774e705c121SKalle Valo 		cmd->pass_all = 1;
1775e705c121SKalle Valo 		return (u64)(unsigned long)cmd;
1776e705c121SKalle Valo 	}
1777e705c121SKalle Valo 
1778e705c121SKalle Valo 	netdev_hw_addr_list_for_each(addr, mc_list) {
1779e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1780e705c121SKalle Valo 				   cmd->count, addr->addr);
1781e705c121SKalle Valo 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1782e705c121SKalle Valo 		       addr->addr, ETH_ALEN);
1783e705c121SKalle Valo 		cmd->count++;
1784e705c121SKalle Valo 	}
1785e705c121SKalle Valo 
1786e705c121SKalle Valo 	return (u64)(unsigned long)cmd;
1787e705c121SKalle Valo }
1788e705c121SKalle Valo 
1789e705c121SKalle Valo static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1790e705c121SKalle Valo 				     unsigned int changed_flags,
1791e705c121SKalle Valo 				     unsigned int *total_flags,
1792e705c121SKalle Valo 				     u64 multicast)
1793e705c121SKalle Valo {
1794e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1795e705c121SKalle Valo 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1796e705c121SKalle Valo 
1797e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1798e705c121SKalle Valo 
1799e705c121SKalle Valo 	/* replace previous configuration */
1800e705c121SKalle Valo 	kfree(mvm->mcast_filter_cmd);
1801e705c121SKalle Valo 	mvm->mcast_filter_cmd = cmd;
1802e705c121SKalle Valo 
1803e705c121SKalle Valo 	if (!cmd)
1804e705c121SKalle Valo 		goto out;
1805e705c121SKalle Valo 
180661e7d91bSLuca Coelho 	if (changed_flags & FIF_ALLMULTI)
180761e7d91bSLuca Coelho 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
180861e7d91bSLuca Coelho 
180961e7d91bSLuca Coelho 	if (cmd->pass_all)
181061e7d91bSLuca Coelho 		cmd->count = 0;
181161e7d91bSLuca Coelho 
1812e705c121SKalle Valo 	iwl_mvm_recalc_multicast(mvm);
1813e705c121SKalle Valo out:
1814e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1815e705c121SKalle Valo 	*total_flags = 0;
1816e705c121SKalle Valo }
1817e705c121SKalle Valo 
1818e705c121SKalle Valo static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1819e705c121SKalle Valo 					struct ieee80211_vif *vif,
1820e705c121SKalle Valo 					unsigned int filter_flags,
1821e705c121SKalle Valo 					unsigned int changed_flags)
1822e705c121SKalle Valo {
1823e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1824e705c121SKalle Valo 
1825e705c121SKalle Valo 	/* We support only filter for probe requests */
1826e705c121SKalle Valo 	if (!(changed_flags & FIF_PROBE_REQ))
1827e705c121SKalle Valo 		return;
1828e705c121SKalle Valo 
1829e705c121SKalle Valo 	/* Supported only for p2p client interfaces */
1830f276e20bSJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
1831e705c121SKalle Valo 	    !vif->p2p)
1832e705c121SKalle Valo 		return;
1833e705c121SKalle Valo 
1834e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1835e705c121SKalle Valo 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1836e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1837e705c121SKalle Valo }
1838e705c121SKalle Valo 
1839a07a8f37SSara Sharon static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
1840a07a8f37SSara Sharon 				    struct ieee80211_vif *vif)
1841a07a8f37SSara Sharon {
1842a07a8f37SSara Sharon 	struct iwl_mu_group_mgmt_cmd cmd = {};
1843a07a8f37SSara Sharon 
1844a07a8f37SSara Sharon 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1845a07a8f37SSara Sharon 	       WLAN_MEMBERSHIP_LEN);
1846a07a8f37SSara Sharon 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1847a07a8f37SSara Sharon 	       WLAN_USER_POSITION_LEN);
1848a07a8f37SSara Sharon 
1849a07a8f37SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
1850a07a8f37SSara Sharon 				    WIDE_ID(DATA_PATH_GROUP,
1851a07a8f37SSara Sharon 					    UPDATE_MU_GROUPS_CMD),
1852a07a8f37SSara Sharon 				    0, sizeof(cmd), &cmd);
1853a07a8f37SSara Sharon }
1854a07a8f37SSara Sharon 
1855f92659a1SSara Sharon static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1856f92659a1SSara Sharon 					   struct ieee80211_vif *vif)
1857f92659a1SSara Sharon {
1858d0a9123eSJohannes Berg 	if (vif->bss_conf.mu_mimo_owner) {
1859f92659a1SSara Sharon 		struct iwl_mu_group_mgmt_notif *notif = _data;
1860f92659a1SSara Sharon 
1861f92659a1SSara Sharon 		/*
1862f92659a1SSara Sharon 		 * MU-MIMO Group Id action frame is little endian. We treat
1863f92659a1SSara Sharon 		 * the data received from firmware as if it came from the
1864f92659a1SSara Sharon 		 * action frame, so no conversion is needed.
1865f92659a1SSara Sharon 		 */
1866afe0d181SJohannes Berg 		ieee80211_update_mu_groups(vif, 0,
1867f92659a1SSara Sharon 					   (u8 *)&notif->membership_status,
1868f92659a1SSara Sharon 					   (u8 *)&notif->user_position);
1869f92659a1SSara Sharon 	}
1870f92659a1SSara Sharon }
1871f92659a1SSara Sharon 
1872f92659a1SSara Sharon void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1873f92659a1SSara Sharon 			       struct iwl_rx_cmd_buffer *rxb)
1874f92659a1SSara Sharon {
1875f92659a1SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1876f92659a1SSara Sharon 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1877f92659a1SSara Sharon 
1878f92659a1SSara Sharon 	ieee80211_iterate_active_interfaces_atomic(
1879f92659a1SSara Sharon 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1880f92659a1SSara Sharon 			iwl_mvm_mu_mimo_iface_iterator, notif);
1881f92659a1SSara Sharon }
1882f92659a1SSara Sharon 
1883514c3069SLuca Coelho static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1884514c3069SLuca Coelho {
1885514c3069SLuca Coelho 	u8 byte_num = ppe_pos_bit / 8;
1886514c3069SLuca Coelho 	u8 bit_num = ppe_pos_bit % 8;
1887514c3069SLuca Coelho 	u8 residue_bits;
1888514c3069SLuca Coelho 	u8 res;
1889514c3069SLuca Coelho 
1890514c3069SLuca Coelho 	if (bit_num <= 5)
1891514c3069SLuca Coelho 		return (ppe[byte_num] >> bit_num) &
1892514c3069SLuca Coelho 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
1893514c3069SLuca Coelho 
1894514c3069SLuca Coelho 	/*
1895514c3069SLuca Coelho 	 * If bit_num > 5, we have to combine bits with next byte.
1896514c3069SLuca Coelho 	 * Calculate how many bits we need to take from current byte (called
1897514c3069SLuca Coelho 	 * here "residue_bits"), and add them to bits from next byte.
1898514c3069SLuca Coelho 	 */
1899514c3069SLuca Coelho 
1900514c3069SLuca Coelho 	residue_bits = 8 - bit_num;
1901514c3069SLuca Coelho 
1902514c3069SLuca Coelho 	res = (ppe[byte_num + 1] &
1903514c3069SLuca Coelho 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
1904514c3069SLuca Coelho 	      residue_bits;
1905514c3069SLuca Coelho 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
1906514c3069SLuca Coelho 
1907514c3069SLuca Coelho 	return res;
1908514c3069SLuca Coelho }
1909514c3069SLuca Coelho 
1910091296d3SMiri Korenblit static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
1911091296d3SMiri Korenblit 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
1912cb63eb43SMiri Korenblit 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
1913cb63eb43SMiri Korenblit 			      bool inheritance)
1914091296d3SMiri Korenblit {
1915091296d3SMiri Korenblit 	int i;
1916091296d3SMiri Korenblit 
1917091296d3SMiri Korenblit 	/*
1918091296d3SMiri Korenblit 	* FW currently supports only nss == MAX_HE_SUPP_NSS
1919091296d3SMiri Korenblit 	*
1920091296d3SMiri Korenblit 	* If nss > MAX: we can ignore values we don't support
1921091296d3SMiri Korenblit 	* If nss < MAX: we can set zeros in other streams
1922091296d3SMiri Korenblit 	*/
1923091296d3SMiri Korenblit 	if (nss > MAX_HE_SUPP_NSS) {
19244d8421f2SJason A. Donenfeld 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
1925091296d3SMiri Korenblit 			       MAX_HE_SUPP_NSS);
1926091296d3SMiri Korenblit 		nss = MAX_HE_SUPP_NSS;
1927091296d3SMiri Korenblit 	}
1928091296d3SMiri Korenblit 
1929091296d3SMiri Korenblit 	for (i = 0; i < nss; i++) {
1930091296d3SMiri Korenblit 		u8 ru_index_tmp = ru_index_bitmap << 1;
1931091296d3SMiri Korenblit 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
1932091296d3SMiri Korenblit 		u8 bw;
1933091296d3SMiri Korenblit 
1934091296d3SMiri Korenblit 		for (bw = 0;
1935091296d3SMiri Korenblit 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
1936091296d3SMiri Korenblit 		     bw++) {
1937091296d3SMiri Korenblit 			ru_index_tmp >>= 1;
1938091296d3SMiri Korenblit 
1939cb63eb43SMiri Korenblit 			/*
1940cb63eb43SMiri Korenblit 			* According to the 11be spec, if for a specific BW the PPE Thresholds
1941cb63eb43SMiri Korenblit 			* isn't present - it should inherit the thresholds from the last
1942cb63eb43SMiri Korenblit 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
1943cb63eb43SMiri Korenblit 			* this inheritance - continue in this case
1944cb63eb43SMiri Korenblit 			*/
1945cb63eb43SMiri Korenblit 			if (!(ru_index_tmp & 1)) {
1946cb63eb43SMiri Korenblit 				if (inheritance)
1947cb63eb43SMiri Korenblit 					goto set_thresholds;
1948cb63eb43SMiri Korenblit 				else
1949091296d3SMiri Korenblit 					continue;
1950cb63eb43SMiri Korenblit 			}
1951091296d3SMiri Korenblit 
1952091296d3SMiri Korenblit 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
1953091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
1954091296d3SMiri Korenblit 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
1955091296d3SMiri Korenblit 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
1956091296d3SMiri Korenblit 
1957cb63eb43SMiri Korenblit set_thresholds:
1958091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
1959091296d3SMiri Korenblit 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
1960091296d3SMiri Korenblit 		}
1961091296d3SMiri Korenblit 	}
1962091296d3SMiri Korenblit }
1963091296d3SMiri Korenblit 
1964091296d3SMiri Korenblit static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
1965091296d3SMiri Korenblit 					    struct ieee80211_sta *sta,
1966cb63eb43SMiri Korenblit 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
1967cb63eb43SMiri Korenblit 					    bool inheritance)
1968091296d3SMiri Korenblit {
1969046d2e7cSSriram R 	u8 nss = (sta->deflink.he_cap.ppe_thres[0] & IEEE80211_PPE_THRES_NSS_MASK) + 1;
1970046d2e7cSSriram R 	u8 *ppe = &sta->deflink.he_cap.ppe_thres[0];
1971091296d3SMiri Korenblit 	u8 ru_index_bitmap =
1972091296d3SMiri Korenblit 		u8_get_bits(*ppe,
1973091296d3SMiri Korenblit 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
1974091296d3SMiri Korenblit 	/* Starting after PPE header */
1975091296d3SMiri Korenblit 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
1976091296d3SMiri Korenblit 
1977cb63eb43SMiri Korenblit 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
1978cb63eb43SMiri Korenblit 			  inheritance);
1979091296d3SMiri Korenblit }
1980091296d3SMiri Korenblit 
1981091296d3SMiri Korenblit static void iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
1982091296d3SMiri Korenblit 						     u8 nominal_padding,
1983091296d3SMiri Korenblit 						     u32 *flags)
1984091296d3SMiri Korenblit {
1985091296d3SMiri Korenblit 	int low_th = -1;
1986091296d3SMiri Korenblit 	int high_th = -1;
1987091296d3SMiri Korenblit 	int i;
1988091296d3SMiri Korenblit 
1989cb63eb43SMiri Korenblit 	/* all the macros are the same for EHT and HE */
1990091296d3SMiri Korenblit 	switch (nominal_padding) {
1991cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
1992091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
1993091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
1994091296d3SMiri Korenblit 		break;
1995cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
1996091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_BPSK;
1997091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_NONE;
1998091296d3SMiri Korenblit 		break;
1999cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2000cb63eb43SMiri Korenblit 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2001091296d3SMiri Korenblit 		low_th = IWL_HE_PKT_EXT_NONE;
2002091296d3SMiri Korenblit 		high_th = IWL_HE_PKT_EXT_BPSK;
2003091296d3SMiri Korenblit 		break;
2004091296d3SMiri Korenblit 	}
2005091296d3SMiri Korenblit 
2006091296d3SMiri Korenblit 	/* Set the PPE thresholds accordingly */
2007091296d3SMiri Korenblit 	if (low_th >= 0 && high_th >= 0) {
2008091296d3SMiri Korenblit 		for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2009091296d3SMiri Korenblit 			u8 bw;
2010091296d3SMiri Korenblit 
2011091296d3SMiri Korenblit 			for (bw = 0;
2012091296d3SMiri Korenblit 			     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2013091296d3SMiri Korenblit 			     bw++) {
2014091296d3SMiri Korenblit 				pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2015091296d3SMiri Korenblit 				pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2016091296d3SMiri Korenblit 			}
2017091296d3SMiri Korenblit 		}
2018091296d3SMiri Korenblit 
2019091296d3SMiri Korenblit 		*flags |= STA_CTXT_HE_PACKET_EXT;
2020091296d3SMiri Korenblit 	}
2021091296d3SMiri Korenblit }
2022091296d3SMiri Korenblit 
2023cb63eb43SMiri Korenblit static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2024cb63eb43SMiri Korenblit 					 u8 nominal_padding)
2025cb63eb43SMiri Korenblit {
2026cb63eb43SMiri Korenblit 	int i;
2027cb63eb43SMiri Korenblit 
2028cb63eb43SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2029cb63eb43SMiri Korenblit 		u8 bw;
2030cb63eb43SMiri Korenblit 
2031cb63eb43SMiri Korenblit 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2032cb63eb43SMiri Korenblit 		     bw++) {
2033cb63eb43SMiri Korenblit 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2034cb63eb43SMiri Korenblit 
2035cb63eb43SMiri Korenblit 			if (nominal_padding >
2036cb63eb43SMiri Korenblit 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2037cb63eb43SMiri Korenblit 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2038cb63eb43SMiri Korenblit 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2039cb63eb43SMiri Korenblit 			else if (nominal_padding ==
2040cb63eb43SMiri Korenblit 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2041cb63eb43SMiri Korenblit 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2042cb63eb43SMiri Korenblit 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2043cb63eb43SMiri Korenblit 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2044cb63eb43SMiri Korenblit 		}
2045cb63eb43SMiri Korenblit 	}
2046cb63eb43SMiri Korenblit }
2047cb63eb43SMiri Korenblit 
20489c4f15caSMiri Korenblit /*
20499c4f15caSMiri Korenblit  * This function sets the MU EDCA parameters ans returns whether MU EDCA
20509c4f15caSMiri Korenblit  * is enabled or not
20519c4f15caSMiri Korenblit  */
205255eb1c5fSMiri Korenblit bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
20539c4f15caSMiri Korenblit 				   struct iwl_mvm_vif *mvmvif,
205455eb1c5fSMiri Korenblit 				   struct iwl_he_backoff_conf *trig_based_txf)
20559c4f15caSMiri Korenblit {
20569c4f15caSMiri Korenblit 	int i;
20579c4f15caSMiri Korenblit 	/* Mark MU EDCA as enabled, unless none detected on some AC */
20589c4f15caSMiri Korenblit 	bool mu_edca_enabled = true;
20599c4f15caSMiri Korenblit 
20609c4f15caSMiri Korenblit 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
20619c4f15caSMiri Korenblit 		struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
20629c4f15caSMiri Korenblit 			&mvmvif->queue_params[i].mu_edca_param_rec;
20639c4f15caSMiri Korenblit 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
20649c4f15caSMiri Korenblit 
20659c4f15caSMiri Korenblit 		if (!mvmvif->queue_params[i].mu_edca) {
20669c4f15caSMiri Korenblit 			mu_edca_enabled = false;
20679c4f15caSMiri Korenblit 			break;
20689c4f15caSMiri Korenblit 		}
20699c4f15caSMiri Korenblit 
20709c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmin =
20719c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
20729c4f15caSMiri Korenblit 		trig_based_txf[ac].cwmax =
20739c4f15caSMiri Korenblit 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
20749c4f15caSMiri Korenblit 		trig_based_txf[ac].aifsn =
20759c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->aifsn & 0xf);
20769c4f15caSMiri Korenblit 		trig_based_txf[ac].mu_time =
20779c4f15caSMiri Korenblit 			cpu_to_le16(mu_edca->mu_edca_timer);
20789c4f15caSMiri Korenblit 	}
20799c4f15caSMiri Korenblit 
20809c4f15caSMiri Korenblit 	return mu_edca_enabled;
20819c4f15caSMiri Korenblit }
20829c4f15caSMiri Korenblit 
20839be162a7SMiri Korenblit bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
20849c4f15caSMiri Korenblit {
20859c4f15caSMiri Korenblit 	const struct ieee80211_supported_band *sband;
20869c4f15caSMiri Korenblit 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
20879c4f15caSMiri Korenblit 
20889c4f15caSMiri Korenblit 	/* This capability is the same for all bands,
20899c4f15caSMiri Korenblit 	 * so take it from one of them.
20909c4f15caSMiri Korenblit 	 */
20919c4f15caSMiri Korenblit 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
20929c4f15caSMiri Korenblit 	own_he_cap = ieee80211_get_he_iftype_cap(sband,
20939c4f15caSMiri Korenblit 						 ieee80211_vif_type_p2p(vif));
20949c4f15caSMiri Korenblit 
20959c4f15caSMiri Korenblit 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
20969c4f15caSMiri Korenblit 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
20979c4f15caSMiri Korenblit }
20989c4f15caSMiri Korenblit 
2099514c3069SLuca Coelho static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2100514c3069SLuca Coelho 			       struct ieee80211_vif *vif, u8 sta_id)
2101514c3069SLuca Coelho {
2102514c3069SLuca Coelho 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
210342506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2104514c3069SLuca Coelho 		.sta_id = sta_id,
2105514c3069SLuca Coelho 		.tid_limit = IWL_MAX_TID_COUNT,
2106dd56e902SJohn Crispin 		.bss_color = vif->bss_conf.he_bss_color.color,
2107514c3069SLuca Coelho 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2108514c3069SLuca Coelho 		.frame_time_rts_th =
2109514c3069SLuca Coelho 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2110514c3069SLuca Coelho 	};
211142506dd2SJohannes Berg 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
211242506dd2SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
211342506dd2SJohannes Berg 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
211442506dd2SJohannes Berg 	int size;
2115514c3069SLuca Coelho 	struct ieee80211_sta *sta;
2116514c3069SLuca Coelho 	u32 flags;
2117514c3069SLuca Coelho 	int i;
211842506dd2SJohannes Berg 	void *cmd;
2119cb63eb43SMiri Korenblit 	u8 nominal_padding;
212042506dd2SJohannes Berg 
212142506dd2SJohannes Berg 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
212242506dd2SJohannes Berg 		ver = 1;
212342506dd2SJohannes Berg 
212442506dd2SJohannes Berg 	switch (ver) {
212542506dd2SJohannes Berg 	case 1:
212642506dd2SJohannes Berg 		/* same layout as v2 except some data at the end */
212742506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
212842506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
212942506dd2SJohannes Berg 		break;
213042506dd2SJohannes Berg 	case 2:
213142506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd_v2;
213242506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
213342506dd2SJohannes Berg 		break;
213442506dd2SJohannes Berg 	case 3:
213542506dd2SJohannes Berg 		cmd = &sta_ctxt_cmd;
213642506dd2SJohannes Berg 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
213742506dd2SJohannes Berg 		break;
213842506dd2SJohannes Berg 	default:
213942506dd2SJohannes Berg 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
214042506dd2SJohannes Berg 		return;
214142506dd2SJohannes Berg 	}
2142514c3069SLuca Coelho 
2143514c3069SLuca Coelho 	rcu_read_lock();
2144514c3069SLuca Coelho 
2145514c3069SLuca Coelho 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
214612d47f0eSAndrei Otcheretianski 	if (IS_ERR_OR_NULL(sta)) {
2147514c3069SLuca Coelho 		rcu_read_unlock();
2148514c3069SLuca Coelho 		WARN(1, "Can't find STA to configure HE\n");
2149514c3069SLuca Coelho 		return;
2150514c3069SLuca Coelho 	}
2151514c3069SLuca Coelho 
2152046d2e7cSSriram R 	if (!sta->deflink.he_cap.has_he) {
2153514c3069SLuca Coelho 		rcu_read_unlock();
2154514c3069SLuca Coelho 		return;
2155514c3069SLuca Coelho 	}
2156514c3069SLuca Coelho 
2157514c3069SLuca Coelho 	flags = 0;
2158514c3069SLuca Coelho 
21594f58121dSIlan Peer 	/* Block 26-tone RU OFDMA transmissions */
21604f58121dSIlan Peer 	if (mvmvif->he_ru_2mhz_block)
21614f58121dSIlan Peer 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
21624f58121dSIlan Peer 
2163514c3069SLuca Coelho 	/* HTC flags */
2164046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[0] &
2165514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP0_HTC_HE)
2166514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2167046d2e7cSSriram R 	if ((sta->deflink.he_cap.he_cap_elem.mac_cap_info[1] &
2168514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2169046d2e7cSSriram R 	    (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2170514c3069SLuca Coelho 	      IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2171514c3069SLuca Coelho 		u8 link_adap =
2172046d2e7cSSriram R 			((sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2173514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2174046d2e7cSSriram R 			 (sta->deflink.he_cap.he_cap_elem.mac_cap_info[1] &
2175514c3069SLuca Coelho 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2176514c3069SLuca Coelho 
2177514c3069SLuca Coelho 		if (link_adap == 2)
2178514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2179514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2180514c3069SLuca Coelho 		else if (link_adap == 3)
2181514c3069SLuca Coelho 			sta_ctxt_cmd.htc_flags |=
2182514c3069SLuca Coelho 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2183514c3069SLuca Coelho 	}
2184046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2185514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2186046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[3] &
2187514c3069SLuca Coelho 	    IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2188514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2189046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2190514c3069SLuca Coelho 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2191514c3069SLuca Coelho 
2192189b8d44SNaftali Goldstein 	/*
2193189b8d44SNaftali Goldstein 	 * Initialize the PPE thresholds to "None" (7), as described in Table
2194189b8d44SNaftali Goldstein 	 * 9-262ac of 80211.ax/D3.0.
2195189b8d44SNaftali Goldstein 	 */
2196091296d3SMiri Korenblit 	memset(&sta_ctxt_cmd.pkt_ext, IWL_HE_PKT_EXT_NONE,
2197091296d3SMiri Korenblit 	       sizeof(sta_ctxt_cmd.pkt_ext));
2198189b8d44SNaftali Goldstein 
2199cb63eb43SMiri Korenblit 	if (sta->deflink.eht_cap.has_eht) {
2200cb63eb43SMiri Korenblit 		nominal_padding =
2201cb63eb43SMiri Korenblit 			u8_get_bits(sta->deflink.eht_cap.eht_cap_elem.phy_cap_info[5],
2202cb63eb43SMiri Korenblit 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
2203cb63eb43SMiri Korenblit 
2204cb63eb43SMiri Korenblit 		/* If PPE Thresholds exists, parse them into a FW-familiar format. */
2205cb63eb43SMiri Korenblit 		if (sta->deflink.eht_cap.eht_cap_elem.phy_cap_info[5] &
2206cb63eb43SMiri Korenblit 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
2207cb63eb43SMiri Korenblit 			u8 nss = (sta->deflink.eht_cap.eht_ppe_thres[0] &
2208cb63eb43SMiri Korenblit 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
2209cb63eb43SMiri Korenblit 			u8 *ppe = &sta->deflink.eht_cap.eht_ppe_thres[0];
2210cb63eb43SMiri Korenblit 			u8 ru_index_bitmap =
2211cb63eb43SMiri Korenblit 				u16_get_bits(*ppe,
2212cb63eb43SMiri Korenblit 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2213cb63eb43SMiri Korenblit 			 /* Starting after PPE header */
2214cb63eb43SMiri Korenblit 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
2215cb63eb43SMiri Korenblit 
2216cb63eb43SMiri Korenblit 			iwl_mvm_parse_ppe(mvm,
2217cb63eb43SMiri Korenblit 					  &sta_ctxt_cmd.pkt_ext,
2218cb63eb43SMiri Korenblit 					  nss, ru_index_bitmap, ppe,
2219cb63eb43SMiri Korenblit 					  ppe_pos_bit, true);
2220cb63eb43SMiri Korenblit 			flags |= STA_CTXT_HE_PACKET_EXT;
2221cb63eb43SMiri Korenblit 		/* EHT PPE Thresholds doesn't exist - set the API according to HE PPE Tresholds*/
2222cb63eb43SMiri Korenblit 		} else if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[6] &
2223cb63eb43SMiri Korenblit 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2224cb63eb43SMiri Korenblit 			struct iwl_he_pkt_ext_v2 *pkt_ext =
2225cb63eb43SMiri Korenblit 				&sta_ctxt_cmd.pkt_ext;
2226cb63eb43SMiri Korenblit 
2227cb63eb43SMiri Korenblit 			/*
2228cb63eb43SMiri Korenblit 			* Even though HE Capabilities IE doesn't contain PPE
2229cb63eb43SMiri Korenblit 			* Thresholds for BW 320Mhz, thresholds for this BW will
2230cb63eb43SMiri Korenblit 			* be filled in with the same values as 160Mhz, due to
2231cb63eb43SMiri Korenblit 			* the inheritance, as required.
2232cb63eb43SMiri Korenblit 			*/
2233cb63eb43SMiri Korenblit 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, sta, pkt_ext,
2234cb63eb43SMiri Korenblit 							true);
2235cb63eb43SMiri Korenblit 
2236cb63eb43SMiri Korenblit 			/*
2237cb63eb43SMiri Korenblit 			* According to the requirements, for MCSs 12-13 the maximum value between
2238cb63eb43SMiri Korenblit 			* HE PPE Threshold and Common Nominal Packet Padding needs to be taken
2239cb63eb43SMiri Korenblit 			*/
2240cb63eb43SMiri Korenblit 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
2241cb63eb43SMiri Korenblit 
2242cb63eb43SMiri Korenblit 			flags |= STA_CTXT_HE_PACKET_EXT;
2243cb63eb43SMiri Korenblit 
2244cb63eb43SMiri Korenblit 		/*
2245cb63eb43SMiri Korenblit 		* if PPE Thresholds doesn't present in both EHT IE and HE IE -
2246cb63eb43SMiri Korenblit 		* take the Thresholds from Common Nominal Packet Padding field
2247cb63eb43SMiri Korenblit 		*/
2248cb63eb43SMiri Korenblit 		} else {
2249cb63eb43SMiri Korenblit 			iwl_mvm_set_pkt_ext_from_nominal_padding(&sta_ctxt_cmd.pkt_ext,
2250cb63eb43SMiri Korenblit 								 nominal_padding,
2251cb63eb43SMiri Korenblit 								 &flags);
2252cb63eb43SMiri Korenblit 		}
2253cb63eb43SMiri Korenblit 	} else if (sta->deflink.he_cap.has_he) {
2254189b8d44SNaftali Goldstein 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2255046d2e7cSSriram R 		if (sta->deflink.he_cap.he_cap_elem.phy_cap_info[6] &
2256514c3069SLuca Coelho 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2257091296d3SMiri Korenblit 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, sta,
2258cb63eb43SMiri Korenblit 							&sta_ctxt_cmd.pkt_ext,
2259cb63eb43SMiri Korenblit 							false);
2260514c3069SLuca Coelho 			flags |= STA_CTXT_HE_PACKET_EXT;
2261cb63eb43SMiri Korenblit 		/*
2262cb63eb43SMiri Korenblit 		* PPE Thresholds doesn't exist - set the API PPE values
2263cb63eb43SMiri Korenblit 		* according to Common Nominal Packet Padding field.
2264cb63eb43SMiri Korenblit 		*/
2265091296d3SMiri Korenblit 		} else {
2266cb63eb43SMiri Korenblit 			nominal_padding =
2267046d2e7cSSriram R 				u8_get_bits(sta->deflink.he_cap.he_cap_elem.phy_cap_info[9],
2268091296d3SMiri Korenblit 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
2269091296d3SMiri Korenblit 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
2270091296d3SMiri Korenblit 				iwl_mvm_set_pkt_ext_from_nominal_padding(&sta_ctxt_cmd.pkt_ext,
2271091296d3SMiri Korenblit 									 nominal_padding,
2272091296d3SMiri Korenblit 									 &flags);
2273514c3069SLuca Coelho 		}
2274cb63eb43SMiri Korenblit 	}
2275cb63eb43SMiri Korenblit 
2276cb63eb43SMiri Korenblit 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2277cb63eb43SMiri Korenblit 		int bw;
2278cb63eb43SMiri Korenblit 
2279cb63eb43SMiri Korenblit 		for (bw = 0;
2280cb63eb43SMiri Korenblit 		     bw < ARRAY_SIZE(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i]);
2281cb63eb43SMiri Korenblit 		     bw++) {
2282cb63eb43SMiri Korenblit 			u8 *qam_th =
2283cb63eb43SMiri Korenblit 				&sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0];
2284cb63eb43SMiri Korenblit 
2285cb63eb43SMiri Korenblit 			IWL_DEBUG_HT(mvm,
2286cb63eb43SMiri Korenblit 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
2287cb63eb43SMiri Korenblit 				     i, bw, qam_th[0], qam_th[1]);
2288cb63eb43SMiri Korenblit 		}
2289cb63eb43SMiri Korenblit 	}
229073f23d91SShaul Triebitz 
2291046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
229273f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
229373f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
229473f23d91SShaul Triebitz 
2295046d2e7cSSriram R 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
229673f23d91SShaul Triebitz 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
229773f23d91SShaul Triebitz 		flags |= STA_CTXT_HE_ACK_ENABLED;
229873f23d91SShaul Triebitz 
2299514c3069SLuca Coelho 	rcu_read_unlock();
2300514c3069SLuca Coelho 
23019c4f15caSMiri Korenblit 	if (iwl_mvm_set_fw_mu_edca_params(mvm, mvmvif,
23029c4f15caSMiri Korenblit 					  &sta_ctxt_cmd.trig_based_txf[0]))
2303514c3069SLuca Coelho 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2304514c3069SLuca Coelho 
2305514c3069SLuca Coelho 	if (vif->bss_conf.uora_exists) {
2306514c3069SLuca Coelho 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2307514c3069SLuca Coelho 
2308514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmin =
2309514c3069SLuca Coelho 			vif->bss_conf.uora_ocw_range & 0x7;
2310514c3069SLuca Coelho 		sta_ctxt_cmd.rand_alloc_ecwmax =
2311514c3069SLuca Coelho 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2312514c3069SLuca Coelho 	}
2313514c3069SLuca Coelho 
23149c4f15caSMiri Korenblit 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2315ee1a02d7SShaul Triebitz 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2316ee1a02d7SShaul Triebitz 
2317918cbf39SSara Sharon 	if (vif->bss_conf.nontransmitted) {
2318918cbf39SSara Sharon 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2319918cbf39SSara Sharon 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2320918cbf39SSara Sharon 				vif->bss_conf.transmitter_bssid);
2321d14ae796SSara Sharon 		sta_ctxt_cmd.max_bssid_indicator =
2322d14ae796SSara Sharon 			vif->bss_conf.bssid_indicator;
2323d14ae796SSara Sharon 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2324d14ae796SSara Sharon 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2325d14ae796SSara Sharon 		sta_ctxt_cmd.profile_periodicity =
2326d14ae796SSara Sharon 			vif->bss_conf.profile_periodicity;
2327918cbf39SSara Sharon 	}
2328514c3069SLuca Coelho 
2329514c3069SLuca Coelho 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2330514c3069SLuca Coelho 
233142506dd2SJohannes Berg 	if (ver < 3) {
233242506dd2SJohannes Berg 		/* fields before pkt_ext */
233342506dd2SJohannes Berg 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
233442506dd2SJohannes Berg 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
233542506dd2SJohannes Berg 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
233642506dd2SJohannes Berg 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
233742506dd2SJohannes Berg 
233842506dd2SJohannes Berg 		/* pkt_ext */
233942506dd2SJohannes Berg 		for (i = 0;
234042506dd2SJohannes Berg 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
234142506dd2SJohannes Berg 		     i++) {
234242506dd2SJohannes Berg 			u8 bw;
234342506dd2SJohannes Berg 
234442506dd2SJohannes Berg 			for (bw = 0;
234542506dd2SJohannes Berg 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
234642506dd2SJohannes Berg 			     bw++) {
234742506dd2SJohannes Berg 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
234842506dd2SJohannes Berg 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
234942506dd2SJohannes Berg 
235042506dd2SJohannes Berg 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
235142506dd2SJohannes Berg 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
235242506dd2SJohannes Berg 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
235342506dd2SJohannes Berg 			}
235442506dd2SJohannes Berg 		}
235542506dd2SJohannes Berg 
235642506dd2SJohannes Berg 		/* fields after pkt_ext */
235742506dd2SJohannes Berg 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
235842506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
235942506dd2SJohannes Berg 			     sizeof(sta_ctxt_cmd_v2) -
236042506dd2SJohannes Berg 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
236142506dd2SJohannes Berg 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
236242506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
236342506dd2SJohannes Berg 		       (u8 *)&sta_ctxt_cmd +
236442506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
236542506dd2SJohannes Berg 		       sizeof(sta_ctxt_cmd) -
236642506dd2SJohannes Berg 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
236742506dd2SJohannes Berg 		sta_ctxt_cmd_v2.reserved3 = 0;
236842506dd2SJohannes Berg 	}
236942506dd2SJohannes Berg 
237042506dd2SJohannes Berg 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2371514c3069SLuca Coelho 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2372514c3069SLuca Coelho }
2373514c3069SLuca Coelho 
2374af84ac57SJohannes Berg static void iwl_mvm_protect_assoc(struct iwl_mvm *mvm,
2375af84ac57SJohannes Berg 				  struct ieee80211_vif *vif,
2376af84ac57SJohannes Berg 				  u32 duration_override)
2377af84ac57SJohannes Berg {
2378af84ac57SJohannes Berg 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2379af84ac57SJohannes Berg 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2380af84ac57SJohannes Berg 
2381af84ac57SJohannes Berg 	if (duration_override > duration)
2382af84ac57SJohannes Berg 		duration = duration_override;
2383af84ac57SJohannes Berg 
2384af84ac57SJohannes Berg 	/* Try really hard to protect the session and hear a beacon
2385af84ac57SJohannes Berg 	 * The new session protection command allows us to protect the
2386af84ac57SJohannes Berg 	 * session for a much longer time since the firmware will internally
2387af84ac57SJohannes Berg 	 * create two events: a 300TU one with a very high priority that
2388af84ac57SJohannes Berg 	 * won't be fragmented which should be enough for 99% of the cases,
2389af84ac57SJohannes Berg 	 * and another one (which we configure here to be 900TU long) which
2390af84ac57SJohannes Berg 	 * will have a slightly lower priority, but more importantly, can be
2391af84ac57SJohannes Berg 	 * fragmented so that it'll allow other activities to run.
2392af84ac57SJohannes Berg 	 */
2393af84ac57SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
2394af84ac57SJohannes Berg 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2395af84ac57SJohannes Berg 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2396af84ac57SJohannes Berg 						    min_duration, false);
2397af84ac57SJohannes Berg 	else
2398af84ac57SJohannes Berg 		iwl_mvm_protect_session(mvm, vif, duration,
2399af84ac57SJohannes Berg 					min_duration, 500, false);
2400af84ac57SJohannes Berg }
2401af84ac57SJohannes Berg 
2402e705c121SKalle Valo static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2403e705c121SKalle Valo 					     struct ieee80211_vif *vif,
2404e705c121SKalle Valo 					     struct ieee80211_bss_conf *bss_conf,
24057b7090b4SJohannes Berg 					     u64 changes)
2406e705c121SKalle Valo {
2407e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2408e705c121SKalle Valo 	int ret;
2409e705c121SKalle Valo 
2410e705c121SKalle Valo 	/*
2411cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend
2412cdaba917SEmmanuel Grumbach 	 * on the beacon interval, which was not known when the station
2413cdaba917SEmmanuel Grumbach 	 * interface was added.
2414e705c121SKalle Valo 	 */
2415f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2416cb63eb43SMiri Korenblit 		if ((vif->bss_conf.he_support &&
2417cb63eb43SMiri Korenblit 		     !iwlwifi_mod_params.disable_11ax) ||
2418cb63eb43SMiri Korenblit 		    (vif->bss_conf.eht_support &&
2419cb63eb43SMiri Korenblit 		     !iwlwifi_mod_params.disable_11be))
2420514c3069SLuca Coelho 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2421514c3069SLuca Coelho 
2422e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2423514c3069SLuca Coelho 	}
2424e705c121SKalle Valo 
242540ecdd01SShaul Triebitz 	/* Update MU EDCA params */
242640ecdd01SShaul Triebitz 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2427cb63eb43SMiri Korenblit 	    vif->cfg.assoc &&
2428cb63eb43SMiri Korenblit 	    ((vif->bss_conf.he_support &&
2429cb63eb43SMiri Korenblit 	      !iwlwifi_mod_params.disable_11ax) ||
2430cb63eb43SMiri Korenblit 	     (vif->bss_conf.eht_support &&
2431cb63eb43SMiri Korenblit 	      !iwlwifi_mod_params.disable_11be)))
243240ecdd01SShaul Triebitz 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
243340ecdd01SShaul Triebitz 
2434e705c121SKalle Valo 	/*
2435e705c121SKalle Valo 	 * If we're not associated yet, take the (new) BSSID before associating
2436e705c121SKalle Valo 	 * so the firmware knows. If we're already associated, then use the old
2437e705c121SKalle Valo 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2438e705c121SKalle Valo 	 * branch for disassociation below.
2439e705c121SKalle Valo 	 */
2440e705c121SKalle Valo 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2441e705c121SKalle Valo 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2442e705c121SKalle Valo 
2443e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
2444e705c121SKalle Valo 	if (ret)
2445e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2446e705c121SKalle Valo 
2447e705c121SKalle Valo 	/* after sending it once, adopt mac80211 data */
2448e705c121SKalle Valo 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2449f276e20bSJohannes Berg 	mvmvif->associated = vif->cfg.assoc;
2450e705c121SKalle Valo 
2451e705c121SKalle Valo 	if (changes & BSS_CHANGED_ASSOC) {
2452f276e20bSJohannes Berg 		if (vif->cfg.assoc) {
2453e705c121SKalle Valo 			/* clear statistics to get clean beacon counter */
2454e705c121SKalle Valo 			iwl_mvm_request_statistics(mvm, true);
2455e705c121SKalle Valo 			memset(&mvmvif->beacon_stats, 0,
2456e705c121SKalle Valo 			       sizeof(mvmvif->beacon_stats));
2457e705c121SKalle Valo 
2458e705c121SKalle Valo 			/* add quota for this interface */
2459e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2460e705c121SKalle Valo 			if (ret) {
2461e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2462e705c121SKalle Valo 				return;
2463e705c121SKalle Valo 			}
2464e705c121SKalle Valo 
2465e705c121SKalle Valo 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2466fe959c7bSEmmanuel Grumbach 				     &mvm->status) &&
2467fe959c7bSEmmanuel Grumbach 			    !fw_has_capa(&mvm->fw->ucode_capa,
2468fe959c7bSEmmanuel Grumbach 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2469e705c121SKalle Valo 				/*
2470e705c121SKalle Valo 				 * If we're restarting then the firmware will
2471e705c121SKalle Valo 				 * obviously have lost synchronisation with
2472e705c121SKalle Valo 				 * the AP. It will attempt to synchronise by
2473e705c121SKalle Valo 				 * itself, but we can make it more reliable by
2474e705c121SKalle Valo 				 * scheduling a session protection time event.
2475e705c121SKalle Valo 				 *
2476e705c121SKalle Valo 				 * The firmware needs to receive a beacon to
2477e705c121SKalle Valo 				 * catch up with synchronisation, use 110% of
2478e705c121SKalle Valo 				 * the beacon interval.
2479e705c121SKalle Valo 				 *
2480e705c121SKalle Valo 				 * Set a large maximum delay to allow for more
2481e705c121SKalle Valo 				 * than a single interface.
2482fe959c7bSEmmanuel Grumbach 				 *
2483fe959c7bSEmmanuel Grumbach 				 * For new firmware versions, rely on the
2484fe959c7bSEmmanuel Grumbach 				 * firmware. This is relevant for DCM scenarios
2485fe959c7bSEmmanuel Grumbach 				 * only anyway.
2486e705c121SKalle Valo 				 */
2487e705c121SKalle Valo 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2488e705c121SKalle Valo 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2489e705c121SKalle Valo 							5 * dur, false);
2490af84ac57SJohannes Berg 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2491af84ac57SJohannes Berg 					     &mvm->status) &&
2492af84ac57SJohannes Berg 				   !vif->bss_conf.dtim_period) {
2493af84ac57SJohannes Berg 				/*
2494af84ac57SJohannes Berg 				 * If we're not restarting and still haven't
2495af84ac57SJohannes Berg 				 * heard a beacon (dtim period unknown) then
2496af84ac57SJohannes Berg 				 * make sure we still have enough minimum time
2497af84ac57SJohannes Berg 				 * remaining in the time event, since the auth
2498af84ac57SJohannes Berg 				 * might actually have taken quite a while
2499af84ac57SJohannes Berg 				 * (especially for SAE) and so the remaining
2500af84ac57SJohannes Berg 				 * time could be small without us having heard
2501af84ac57SJohannes Berg 				 * a beacon yet.
2502af84ac57SJohannes Berg 				 */
2503af84ac57SJohannes Berg 				iwl_mvm_protect_assoc(mvm, vif, 0);
2504e705c121SKalle Valo 			}
2505e705c121SKalle Valo 
2506e705c121SKalle Valo 			iwl_mvm_sf_update(mvm, vif, false);
2507e705c121SKalle Valo 			iwl_mvm_power_vif_assoc(mvm, vif);
2508e705c121SKalle Valo 			if (vif->p2p) {
2509e705c121SKalle Valo 				iwl_mvm_update_smps(mvm, vif,
2510e705c121SKalle Valo 						    IWL_MVM_SMPS_REQ_PROT,
2511e705c121SKalle Valo 						    IEEE80211_SMPS_DYNAMIC);
2512e705c121SKalle Valo 			}
25130ae98812SSara Sharon 		} else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
25146d19a5ebSEmmanuel Grumbach 			iwl_mvm_mei_host_disassociated(mvm);
2515e705c121SKalle Valo 			/*
2516e705c121SKalle Valo 			 * If update fails - SF might be running in associated
2517e705c121SKalle Valo 			 * mode while disassociated - which is forbidden.
2518e705c121SKalle Valo 			 */
251969e508b4SIlan Peer 			ret = iwl_mvm_sf_update(mvm, vif, false);
252069e508b4SIlan Peer 			WARN_ONCE(ret &&
252169e508b4SIlan Peer 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
252269e508b4SIlan Peer 					    &mvm->status),
2523e705c121SKalle Valo 				  "Failed to update SF upon disassociation\n");
2524e705c121SKalle Valo 
25256b28f978SEmmanuel Grumbach 			/*
25266b28f978SEmmanuel Grumbach 			 * If we get an assert during the connection (after the
25276b28f978SEmmanuel Grumbach 			 * station has been added, but before the vif is set
25286b28f978SEmmanuel Grumbach 			 * to associated), mac80211 will re-add the station and
25296b28f978SEmmanuel Grumbach 			 * then configure the vif. Since the vif is not
25306b28f978SEmmanuel Grumbach 			 * associated, we would remove the station here and
25316b28f978SEmmanuel Grumbach 			 * this would fail the recovery.
25326b28f978SEmmanuel Grumbach 			 */
25336b28f978SEmmanuel Grumbach 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
25346b28f978SEmmanuel Grumbach 				      &mvm->status)) {
25355c75a208SJohannes Berg 				/* first remove remaining keys */
25365c75a208SJohannes Berg 				iwl_mvm_sec_key_remove_ap(mvm, vif);
25375c75a208SJohannes Berg 
25386b28f978SEmmanuel Grumbach 				/*
25396b28f978SEmmanuel Grumbach 				 * Remove AP station now that
25406b28f978SEmmanuel Grumbach 				 * the MAC is unassoc
25416b28f978SEmmanuel Grumbach 				 */
25426b28f978SEmmanuel Grumbach 				ret = iwl_mvm_rm_sta_id(mvm, vif,
25436b28f978SEmmanuel Grumbach 							mvmvif->ap_sta_id);
2544e705c121SKalle Valo 				if (ret)
25456b28f978SEmmanuel Grumbach 					IWL_ERR(mvm,
25466b28f978SEmmanuel Grumbach 						"failed to remove AP station\n");
2547e705c121SKalle Valo 
25480ae98812SSara Sharon 				mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
25496b28f978SEmmanuel Grumbach 			}
25506b28f978SEmmanuel Grumbach 
2551e705c121SKalle Valo 			/* remove quota for this interface */
2552e705c121SKalle Valo 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2553e705c121SKalle Valo 			if (ret)
2554e705c121SKalle Valo 				IWL_ERR(mvm, "failed to update quotas\n");
2555e705c121SKalle Valo 
2556e705c121SKalle Valo 			/* this will take the cleared BSSID from bss_conf */
2557e705c121SKalle Valo 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2558e705c121SKalle Valo 			if (ret)
2559e705c121SKalle Valo 				IWL_ERR(mvm,
2560e705c121SKalle Valo 					"failed to update MAC %pM (clear after unassoc)\n",
2561e705c121SKalle Valo 					vif->addr);
2562e705c121SKalle Valo 		}
2563e705c121SKalle Valo 
2564a07a8f37SSara Sharon 		/*
2565a07a8f37SSara Sharon 		 * The firmware tracks the MU-MIMO group on its own.
2566f92659a1SSara Sharon 		 * However, on HW restart we should restore this data.
2567a07a8f37SSara Sharon 		 */
2568a07a8f37SSara Sharon 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2569d0a9123eSJohannes Berg 		    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2570a07a8f37SSara Sharon 			ret = iwl_mvm_update_mu_groups(mvm, vif);
2571a07a8f37SSara Sharon 			if (ret)
2572a07a8f37SSara Sharon 				IWL_ERR(mvm,
2573a07a8f37SSara Sharon 					"failed to update VHT MU_MIMO groups\n");
2574a07a8f37SSara Sharon 		}
2575a07a8f37SSara Sharon 
2576e705c121SKalle Valo 		iwl_mvm_recalc_multicast(mvm);
2577e705c121SKalle Valo 
2578e705c121SKalle Valo 		/* reset rssi values */
2579e705c121SKalle Valo 		mvmvif->bf_data.ave_beacon_signal = 0;
2580e705c121SKalle Valo 
2581e705c121SKalle Valo 		iwl_mvm_bt_coex_vif_change(mvm);
2582e705c121SKalle Valo 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2583e705c121SKalle Valo 				    IEEE80211_SMPS_AUTOMATIC);
2584355346baSAvraham Stern 		if (fw_has_capa(&mvm->fw->ucode_capa,
2585355346baSAvraham Stern 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2586355346baSAvraham Stern 			iwl_mvm_config_scan(mvm);
2587b45242c9SAvraham Stern 	}
2588b45242c9SAvraham Stern 
2589b45242c9SAvraham Stern 	if (changes & BSS_CHANGED_BEACON_INFO) {
2590e705c121SKalle Valo 		/*
2591b45242c9SAvraham Stern 		 * We received a beacon from the associated AP so
2592e705c121SKalle Valo 		 * remove the session protection.
2593e705c121SKalle Valo 		 */
25943edfb5f4SAvraham Stern 		iwl_mvm_stop_session_protection(mvm, vif);
2595e705c121SKalle Valo 
2596e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
2597e705c121SKalle Valo 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2598e705c121SKalle Valo 	}
2599e705c121SKalle Valo 
2600283115fbSAvri Altman 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2601283115fbSAvri Altman 		       /*
2602283115fbSAvri Altman 			* Send power command on every beacon change,
2603283115fbSAvri Altman 			* because we may have not enabled beacon abort yet.
2604283115fbSAvri Altman 			*/
2605283115fbSAvri Altman 		       BSS_CHANGED_BEACON_INFO)) {
2606e705c121SKalle Valo 		ret = iwl_mvm_power_update_mac(mvm);
2607e705c121SKalle Valo 		if (ret)
2608e705c121SKalle Valo 			IWL_ERR(mvm, "failed to update power mode\n");
2609e705c121SKalle Valo 	}
2610e705c121SKalle Valo 
2611e705c121SKalle Valo 	if (changes & BSS_CHANGED_CQM) {
2612e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2613e705c121SKalle Valo 		/* reset cqm events tracking */
2614e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = 0;
2615e705c121SKalle Valo 		if (mvmvif->bf_data.bf_enabled) {
2616e705c121SKalle Valo 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2617e705c121SKalle Valo 			if (ret)
2618e705c121SKalle Valo 				IWL_ERR(mvm,
2619e705c121SKalle Valo 					"failed to update CQM thresholds\n");
2620e705c121SKalle Valo 		}
2621e705c121SKalle Valo 	}
2622e705c121SKalle Valo 
2623de34d1c1SJohannes Berg 	if (changes & BSS_CHANGED_BANDWIDTH)
2624de34d1c1SJohannes Berg 		iwl_mvm_apply_fw_smps_request(vif);
2625e705c121SKalle Valo }
2626e705c121SKalle Valo 
2627e705c121SKalle Valo static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2628ae7ba17bSShaul Triebitz 				 struct ieee80211_vif *vif,
2629b327c84cSGregory Greenman 				 struct ieee80211_bss_conf *link_conf)
2630e705c121SKalle Valo {
2631e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2632e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2633c56e00a3SJohannes Berg 	int ret, i;
2634e705c121SKalle Valo 
2635e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2636e705c121SKalle Valo 
2637e705c121SKalle Valo 	/* Send the beacon template */
2638e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2639e705c121SKalle Valo 	if (ret)
2640e705c121SKalle Valo 		goto out_unlock;
2641e705c121SKalle Valo 
2642e705c121SKalle Valo 	/*
2643cdaba917SEmmanuel Grumbach 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2644cdaba917SEmmanuel Grumbach 	 * the beacon interval, which was not known when the AP interface
2645cdaba917SEmmanuel Grumbach 	 * was added.
2646e705c121SKalle Valo 	 */
2647e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP)
2648e705c121SKalle Valo 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2649e705c121SKalle Valo 
2650e705c121SKalle Valo 	mvmvif->ap_assoc_sta_count = 0;
2651e705c121SKalle Valo 
2652e705c121SKalle Valo 	/* Add the mac context */
2653e705c121SKalle Valo 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2654e705c121SKalle Valo 	if (ret)
2655e705c121SKalle Valo 		goto out_unlock;
2656e705c121SKalle Valo 
2657e705c121SKalle Valo 	/* Perform the binding */
2658e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2659e705c121SKalle Valo 	if (ret)
2660e705c121SKalle Valo 		goto out_remove;
2661e705c121SKalle Valo 
266263dd5d02SSara Sharon 	/*
266363dd5d02SSara Sharon 	 * This is not very nice, but the simplest:
266463dd5d02SSara Sharon 	 * For older FWs adding the mcast sta before the bcast station may
266563dd5d02SSara Sharon 	 * cause assert 0x2b00.
266663dd5d02SSara Sharon 	 * This is fixed in later FW so make the order of removal depend on
266763dd5d02SSara Sharon 	 * the TLV
266863dd5d02SSara Sharon 	 */
266963dd5d02SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2670ced19f26SSara Sharon 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2671ced19f26SSara Sharon 		if (ret)
2672ced19f26SSara Sharon 			goto out_unbind;
267363dd5d02SSara Sharon 		/*
267463dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
267563dd5d02SSara Sharon 		 * events are added and applied to the scheduler
267663dd5d02SSara Sharon 		 */
2677e705c121SKalle Valo 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
267863dd5d02SSara Sharon 		if (ret) {
267963dd5d02SSara Sharon 			iwl_mvm_rm_mcast_sta(mvm, vif);
268063dd5d02SSara Sharon 			goto out_unbind;
268163dd5d02SSara Sharon 		}
268263dd5d02SSara Sharon 	} else {
268363dd5d02SSara Sharon 		/*
268463dd5d02SSara Sharon 		 * Send the bcast station. At this stage the TBTT and DTIM time
268563dd5d02SSara Sharon 		 * events are added and applied to the scheduler
268663dd5d02SSara Sharon 		 */
268775fd4fecSJohannes Berg 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2688e705c121SKalle Valo 		if (ret)
268963dd5d02SSara Sharon 			goto out_unbind;
269075fd4fecSJohannes Berg 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
269163dd5d02SSara Sharon 		if (ret) {
269263dd5d02SSara Sharon 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
269363dd5d02SSara Sharon 			goto out_unbind;
269463dd5d02SSara Sharon 		}
269563dd5d02SSara Sharon 	}
269626d6c16bSSara Sharon 
2697e705c121SKalle Valo 	/* must be set before quota calculations */
2698e705c121SKalle Valo 	mvmvif->ap_ibss_active = true;
2699e705c121SKalle Valo 
2700c56e00a3SJohannes Berg 	/* send all the early keys to the device now */
2701c56e00a3SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2702c56e00a3SJohannes Berg 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2703c56e00a3SJohannes Berg 
2704c56e00a3SJohannes Berg 		if (!key)
2705c56e00a3SJohannes Berg 			continue;
2706c56e00a3SJohannes Berg 
2707c56e00a3SJohannes Berg 		mvmvif->ap_early_keys[i] = NULL;
2708c56e00a3SJohannes Berg 
27096569e7d3SJohannes Berg 		ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2710c56e00a3SJohannes Berg 		if (ret)
2711c56e00a3SJohannes Berg 			goto out_quota_failed;
2712c56e00a3SJohannes Berg 	}
2713c56e00a3SJohannes Berg 
271447242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
271547242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, true,
271647242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
271747242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
271847242744STova Mussai 	}
271947242744STova Mussai 
2720e705c121SKalle Valo 	/* power updated needs to be done before quotas */
2721e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2722e705c121SKalle Valo 
2723e705c121SKalle Valo 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2724e705c121SKalle Valo 	if (ret)
2725e705c121SKalle Valo 		goto out_quota_failed;
2726e705c121SKalle Valo 
2727e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2728e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2729e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2730e705c121SKalle Valo 
2731e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2732e705c121SKalle Valo 
2733e705c121SKalle Valo 	/* we don't support TDLS during DCM */
2734e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2735e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
2736e705c121SKalle Valo 
2737b73f9a4aSJohannes Berg 	iwl_mvm_ftm_restart_responder(mvm, vif);
2738b73f9a4aSJohannes Berg 
2739e705c121SKalle Valo 	goto out_unlock;
2740e705c121SKalle Valo 
2741e705c121SKalle Valo out_quota_failed:
2742e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2743e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2744e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2745ced19f26SSara Sharon 	iwl_mvm_rm_mcast_sta(mvm, vif);
2746e705c121SKalle Valo out_unbind:
2747e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2748e705c121SKalle Valo out_remove:
2749e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2750e705c121SKalle Valo out_unlock:
2751e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2752e705c121SKalle Valo 	return ret;
2753e705c121SKalle Valo }
2754e705c121SKalle Valo 
2755ae7ba17bSShaul Triebitz static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
2756ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
2757b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
2758ae7ba17bSShaul Triebitz {
2759b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
2760ae7ba17bSShaul Triebitz }
2761ae7ba17bSShaul Triebitz 
2762ae7ba17bSShaul Triebitz static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
2763e705c121SKalle Valo 			      struct ieee80211_vif *vif)
2764e705c121SKalle Valo {
2765b327c84cSGregory Greenman 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
2766ae7ba17bSShaul Triebitz }
2767ae7ba17bSShaul Triebitz 
2768ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2769ae7ba17bSShaul Triebitz 				 struct ieee80211_vif *vif,
2770b327c84cSGregory Greenman 				 struct ieee80211_bss_conf *link_conf)
2771ae7ba17bSShaul Triebitz {
2772e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2773e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2774e705c121SKalle Valo 
2775e705c121SKalle Valo 	iwl_mvm_prepare_mac_removal(mvm, vif);
2776e705c121SKalle Valo 
2777e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2778e705c121SKalle Valo 
2779e705c121SKalle Valo 	/* Handle AP stop while in CSA */
2780e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2781e705c121SKalle Valo 		iwl_mvm_remove_time_event(mvm, mvmvif,
2782e705c121SKalle Valo 					  &mvmvif->time_event_data);
2783e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2784e705c121SKalle Valo 		mvmvif->csa_countdown = false;
2785e705c121SKalle Valo 	}
2786e705c121SKalle Valo 
2787e705c121SKalle Valo 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2788e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2789e705c121SKalle Valo 		mvm->csa_tx_block_bcn_timeout = 0;
2790e705c121SKalle Valo 	}
2791e705c121SKalle Valo 
2792e705c121SKalle Valo 	mvmvif->ap_ibss_active = false;
2793e705c121SKalle Valo 	mvm->ap_last_beacon_gp2 = 0;
2794e705c121SKalle Valo 
279547242744STova Mussai 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
279647242744STova Mussai 		iwl_mvm_vif_set_low_latency(mvmvif, false,
279747242744STova Mussai 					    LOW_LATENCY_VIF_TYPE);
279847242744STova Mussai 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
279947242744STova Mussai 	}
280047242744STova Mussai 
2801e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
2802e705c121SKalle Valo 
2803e705c121SKalle Valo 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2804e705c121SKalle Valo 	if (vif->p2p && mvm->p2p_device_vif)
2805e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2806e705c121SKalle Valo 
2807e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, NULL);
2808ced19f26SSara Sharon 
2809be82ecd3SAvraham Stern 	iwl_mvm_ftm_responder_clear(mvm, vif);
2810be82ecd3SAvraham Stern 
2811ced19f26SSara Sharon 	/*
2812ced19f26SSara Sharon 	 * This is not very nice, but the simplest:
2813ced19f26SSara Sharon 	 * For older FWs removing the mcast sta before the bcast station may
2814ced19f26SSara Sharon 	 * cause assert 0x2b00.
2815ced19f26SSara Sharon 	 * This is fixed in later FW (which will stop beaconing when removing
2816ced19f26SSara Sharon 	 * bcast station).
2817ced19f26SSara Sharon 	 * So make the order of removal depend on the TLV
2818ced19f26SSara Sharon 	 */
2819ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
282026d6c16bSSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2821e705c121SKalle Valo 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2822ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2823ced19f26SSara Sharon 		iwl_mvm_rm_mcast_sta(mvm, vif);
2824e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
2825e705c121SKalle Valo 
2826e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
2827e705c121SKalle Valo 
2828e705c121SKalle Valo 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2829e705c121SKalle Valo 
2830e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2831e705c121SKalle Valo }
2832e705c121SKalle Valo 
2833ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
2834ae7ba17bSShaul Triebitz 			    struct ieee80211_vif *vif,
2835b327c84cSGregory Greenman 			    struct ieee80211_bss_conf *link_conf)
2836ae7ba17bSShaul Triebitz {
2837b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
2838ae7ba17bSShaul Triebitz }
2839ae7ba17bSShaul Triebitz 
2840ae7ba17bSShaul Triebitz static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
2841ae7ba17bSShaul Triebitz 			      struct ieee80211_vif *vif)
2842ae7ba17bSShaul Triebitz {
2843b327c84cSGregory Greenman 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
2844ae7ba17bSShaul Triebitz }
2845ae7ba17bSShaul Triebitz 
2846e705c121SKalle Valo static void
2847e705c121SKalle Valo iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2848e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2849e705c121SKalle Valo 				 struct ieee80211_bss_conf *bss_conf,
28507b7090b4SJohannes Berg 				 u64 changes)
2851e705c121SKalle Valo {
2852e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2853e705c121SKalle Valo 
2854e705c121SKalle Valo 	/* Changes will be applied when the AP/IBSS is started */
2855e705c121SKalle Valo 	if (!mvmvif->ap_ibss_active)
2856e705c121SKalle Valo 		return;
2857e705c121SKalle Valo 
2858e705c121SKalle Valo 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2859e705c121SKalle Valo 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2860e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2861e705c121SKalle Valo 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2862e705c121SKalle Valo 
2863e705c121SKalle Valo 	/* Need to send a new beacon template to the FW */
2864e705c121SKalle Valo 	if (changes & BSS_CHANGED_BEACON &&
2865e705c121SKalle Valo 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2866e705c121SKalle Valo 		IWL_WARN(mvm, "Failed updating beacon data\n");
2867e705c121SKalle Valo 
2868b73f9a4aSJohannes Berg 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
2869b73f9a4aSJohannes Berg 		int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2870b73f9a4aSJohannes Berg 
2871b73f9a4aSJohannes Berg 		if (ret)
2872b73f9a4aSJohannes Berg 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2873b73f9a4aSJohannes Berg 				 ret);
2874b73f9a4aSJohannes Berg 	}
2875b73f9a4aSJohannes Berg 
2876e705c121SKalle Valo }
2877e705c121SKalle Valo 
2878e705c121SKalle Valo static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2879e705c121SKalle Valo 				     struct ieee80211_vif *vif,
2880e705c121SKalle Valo 				     struct ieee80211_bss_conf *bss_conf,
28817b7090b4SJohannes Berg 				     u64 changes)
2882e705c121SKalle Valo {
2883e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2884e705c121SKalle Valo 
2885e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2886e705c121SKalle Valo 
2887f276e20bSJohannes Berg 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
2888e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2889e705c121SKalle Valo 
2890e705c121SKalle Valo 	switch (vif->type) {
2891e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
2892e705c121SKalle Valo 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2893e705c121SKalle Valo 		break;
2894e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
2895e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
2896e705c121SKalle Valo 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2897e705c121SKalle Valo 		break;
289891b08c2dSAviya Erenfeld 	case NL80211_IFTYPE_MONITOR:
289991b08c2dSAviya Erenfeld 		if (changes & BSS_CHANGED_MU_GROUPS)
290091b08c2dSAviya Erenfeld 			iwl_mvm_update_mu_groups(mvm, vif);
290191b08c2dSAviya Erenfeld 		break;
2902e705c121SKalle Valo 	default:
2903e705c121SKalle Valo 		/* shouldn't happen */
2904e705c121SKalle Valo 		WARN_ON_ONCE(1);
2905e705c121SKalle Valo 	}
2906e705c121SKalle Valo 
29079aae43a4SJohannes Berg 	if (changes & BSS_CHANGED_TXPOWER) {
29089aae43a4SJohannes Berg 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
29099aae43a4SJohannes Berg 				bss_conf->txpower);
29109aae43a4SJohannes Berg 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
29119aae43a4SJohannes Berg 	}
29129aae43a4SJohannes Berg 
2913e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2914e705c121SKalle Valo }
2915e705c121SKalle Valo 
2916e705c121SKalle Valo static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2917e705c121SKalle Valo 			       struct ieee80211_vif *vif,
2918e705c121SKalle Valo 			       struct ieee80211_scan_request *hw_req)
2919e705c121SKalle Valo {
2920e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2921e705c121SKalle Valo 	int ret;
2922e705c121SKalle Valo 
2923e705c121SKalle Valo 	if (hw_req->req.n_channels == 0 ||
2924e705c121SKalle Valo 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2925e705c121SKalle Valo 		return -EINVAL;
2926e705c121SKalle Valo 
2927e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2928e705c121SKalle Valo 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2929e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2930e705c121SKalle Valo 
2931e705c121SKalle Valo 	return ret;
2932e705c121SKalle Valo }
2933e705c121SKalle Valo 
2934e705c121SKalle Valo static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2935e705c121SKalle Valo 				       struct ieee80211_vif *vif)
2936e705c121SKalle Valo {
2937e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2938e705c121SKalle Valo 
2939e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
2940e705c121SKalle Valo 
2941e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
2942e705c121SKalle Valo 	 * us to stop a hw_scan when it's already stopped.  This can
2943e705c121SKalle Valo 	 * happen, for instance, if we stopped the scan ourselves,
2944e705c121SKalle Valo 	 * called ieee80211_scan_completed() and the userspace called
2945e705c121SKalle Valo 	 * cancel scan scan before ieee80211_scan_work() could run.
2946e705c121SKalle Valo 	 * To handle that, simply return if the scan is not running.
2947e705c121SKalle Valo 	*/
2948e705c121SKalle Valo 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2949e705c121SKalle Valo 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2950e705c121SKalle Valo 
2951e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
2952e705c121SKalle Valo }
2953e705c121SKalle Valo 
2954e705c121SKalle Valo static void
2955e705c121SKalle Valo iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2956e705c121SKalle Valo 				  struct ieee80211_sta *sta, u16 tids,
2957e705c121SKalle Valo 				  int num_frames,
2958e705c121SKalle Valo 				  enum ieee80211_frame_release_type reason,
2959e705c121SKalle Valo 				  bool more_data)
2960e705c121SKalle Valo {
2961e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2962e705c121SKalle Valo 
2963e705c121SKalle Valo 	/* Called when we need to transmit (a) frame(s) from mac80211 */
2964e705c121SKalle Valo 
2965e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2966e705c121SKalle Valo 					  tids, more_data, false);
2967e705c121SKalle Valo }
2968e705c121SKalle Valo 
2969e705c121SKalle Valo static void
2970e705c121SKalle Valo iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2971e705c121SKalle Valo 				    struct ieee80211_sta *sta, u16 tids,
2972e705c121SKalle Valo 				    int num_frames,
2973e705c121SKalle Valo 				    enum ieee80211_frame_release_type reason,
2974e705c121SKalle Valo 				    bool more_data)
2975e705c121SKalle Valo {
2976e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2977e705c121SKalle Valo 
29789a3fcf91SSara Sharon 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2979e705c121SKalle Valo 
2980e705c121SKalle Valo 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2981e705c121SKalle Valo 					  tids, more_data, true);
2982e705c121SKalle Valo }
2983e705c121SKalle Valo 
298465e25482SJohannes Berg static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2985e705c121SKalle Valo 				     enum sta_notify_cmd cmd,
2986e705c121SKalle Valo 				     struct ieee80211_sta *sta)
2987e705c121SKalle Valo {
2988e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2989e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2990e705c121SKalle Valo 	unsigned long txqs = 0, tids = 0;
2991e705c121SKalle Valo 	int tid;
2992e705c121SKalle Valo 
2993960f864bSJohannes Berg 	/*
2994960f864bSJohannes Berg 	 * If we have TVQM then we get too high queue numbers - luckily
2995960f864bSJohannes Berg 	 * we really shouldn't get here with that because such hardware
2996960f864bSJohannes Berg 	 * should have firmware supporting buffer station offload.
2997960f864bSJohannes Berg 	 */
2998960f864bSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2999960f864bSJohannes Berg 		return;
3000960f864bSJohannes Berg 
3001e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3002311590a3SEmmanuel Grumbach 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3003e705c121SKalle Valo 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3004e705c121SKalle Valo 
30056862fceeSSara Sharon 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
30061c17627bSSara Sharon 			continue;
30071c17627bSSara Sharon 
3008e705c121SKalle Valo 		__set_bit(tid_data->txq_id, &txqs);
3009e705c121SKalle Valo 
3010dd32162dSLiad Kaufman 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3011e705c121SKalle Valo 			continue;
3012e705c121SKalle Valo 
3013e705c121SKalle Valo 		__set_bit(tid, &tids);
3014e705c121SKalle Valo 	}
3015e705c121SKalle Valo 
3016e705c121SKalle Valo 	switch (cmd) {
3017e705c121SKalle Valo 	case STA_NOTIFY_SLEEP:
3018e705c121SKalle Valo 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3019e705c121SKalle Valo 			ieee80211_sta_set_buffered(sta, tid, true);
3020e705c121SKalle Valo 
3021e705c121SKalle Valo 		if (txqs)
3022e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3023e705c121SKalle Valo 		/*
3024e705c121SKalle Valo 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3025e705c121SKalle Valo 		 * queues to this station will not be transmitted. The fw will
3026e705c121SKalle Valo 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3027e705c121SKalle Valo 		 */
3028e705c121SKalle Valo 		break;
3029e705c121SKalle Valo 	case STA_NOTIFY_AWAKE:
30300ae98812SSara Sharon 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
3031e705c121SKalle Valo 			break;
3032e705c121SKalle Valo 
3033e705c121SKalle Valo 		if (txqs)
3034e705c121SKalle Valo 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3035e705c121SKalle Valo 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3036e705c121SKalle Valo 		break;
3037e705c121SKalle Valo 	default:
3038e705c121SKalle Valo 		break;
3039e705c121SKalle Valo 	}
3040e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3041e705c121SKalle Valo }
3042e705c121SKalle Valo 
304365e25482SJohannes Berg static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
304465e25482SJohannes Berg 				   struct ieee80211_vif *vif,
304565e25482SJohannes Berg 				   enum sta_notify_cmd cmd,
304665e25482SJohannes Berg 				   struct ieee80211_sta *sta)
304765e25482SJohannes Berg {
304865e25482SJohannes Berg 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
304965e25482SJohannes Berg }
305065e25482SJohannes Berg 
305165e25482SJohannes Berg void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
305265e25482SJohannes Berg {
305365e25482SJohannes Berg 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
305465e25482SJohannes Berg 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
305565e25482SJohannes Berg 	struct ieee80211_sta *sta;
305665e25482SJohannes Berg 	struct iwl_mvm_sta *mvmsta;
305765e25482SJohannes Berg 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
305865e25482SJohannes Berg 
3059be9ae34eSNathan Errera 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
306065e25482SJohannes Berg 		return;
306165e25482SJohannes Berg 
306265e25482SJohannes Berg 	rcu_read_lock();
3063a9560029SSara Sharon 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
306465e25482SJohannes Berg 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
306565e25482SJohannes Berg 		rcu_read_unlock();
306665e25482SJohannes Berg 		return;
306765e25482SJohannes Berg 	}
306865e25482SJohannes Berg 
306965e25482SJohannes Berg 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
307065e25482SJohannes Berg 
307165e25482SJohannes Berg 	if (!mvmsta->vif ||
307265e25482SJohannes Berg 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
307365e25482SJohannes Berg 		rcu_read_unlock();
307465e25482SJohannes Berg 		return;
307565e25482SJohannes Berg 	}
307665e25482SJohannes Berg 
307765e25482SJohannes Berg 	if (mvmsta->sleeping != sleeping) {
307865e25482SJohannes Berg 		mvmsta->sleeping = sleeping;
307965e25482SJohannes Berg 		__iwl_mvm_mac_sta_notify(mvm->hw,
308065e25482SJohannes Berg 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
308165e25482SJohannes Berg 			sta);
308265e25482SJohannes Berg 		ieee80211_sta_ps_transition(sta, sleeping);
308365e25482SJohannes Berg 	}
308465e25482SJohannes Berg 
308565e25482SJohannes Berg 	if (sleeping) {
308665e25482SJohannes Berg 		switch (notif->type) {
308765e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_AWAKE:
308865e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_ASLEEP:
308965e25482SJohannes Berg 			break;
309065e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_UAPSD:
309165e25482SJohannes Berg 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
309265e25482SJohannes Berg 			break;
309365e25482SJohannes Berg 		case IWL_MVM_PM_EVENT_PS_POLL:
309465e25482SJohannes Berg 			ieee80211_sta_pspoll(sta);
309565e25482SJohannes Berg 			break;
309665e25482SJohannes Berg 		default:
309765e25482SJohannes Berg 			break;
309865e25482SJohannes Berg 		}
309965e25482SJohannes Berg 	}
310065e25482SJohannes Berg 
310165e25482SJohannes Berg 	rcu_read_unlock();
310265e25482SJohannes Berg }
310365e25482SJohannes Berg 
3104e705c121SKalle Valo static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3105e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3106e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3107e705c121SKalle Valo {
3108e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3109e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3110e705c121SKalle Valo 
3111e705c121SKalle Valo 	/*
3112e705c121SKalle Valo 	 * This is called before mac80211 does RCU synchronisation,
3113e705c121SKalle Valo 	 * so here we already invalidate our internal RCU-protected
3114e705c121SKalle Valo 	 * station pointer. The rest of the code will thus no longer
3115e705c121SKalle Valo 	 * be able to find the station this way, and we don't rely
3116e705c121SKalle Valo 	 * on further RCU synchronisation after the sta_state()
3117e705c121SKalle Valo 	 * callback deleted the station.
3118e705c121SKalle Valo 	 */
3119e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3120e705c121SKalle Valo 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
3121e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
3122e705c121SKalle Valo 				   ERR_PTR(-ENOENT));
3123e705c121SKalle Valo 
3124e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3125e705c121SKalle Valo }
3126e705c121SKalle Valo 
3127e705c121SKalle Valo static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3128e705c121SKalle Valo 				const u8 *bssid)
3129e705c121SKalle Valo {
3130b0ffe455SJohannes Berg 	int i;
3131b0ffe455SJohannes Berg 
3132b0ffe455SJohannes Berg 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3133b0ffe455SJohannes Berg 		struct iwl_mvm_tcm_mac *mdata;
3134b0ffe455SJohannes Berg 
3135b0ffe455SJohannes Berg 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3136b0ffe455SJohannes Berg 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3137b0ffe455SJohannes Berg 		mdata->opened_rx_ba_sessions = false;
3138b0ffe455SJohannes Berg 	}
3139b0ffe455SJohannes Berg 
3140e705c121SKalle Valo 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3141e705c121SKalle Valo 		return;
3142e705c121SKalle Valo 
3143c5241b0cSAvraham Stern 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3144cee5a882SAvri Altman 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3145cee5a882SAvri Altman 		return;
3146cee5a882SAvri Altman 	}
3147cee5a882SAvri Altman 
314811dee0b4SEmmanuel Grumbach 	if (!vif->p2p &&
314911dee0b4SEmmanuel Grumbach 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3150e705c121SKalle Valo 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3151e705c121SKalle Valo 		return;
3152e705c121SKalle Valo 	}
3153e705c121SKalle Valo 
3154b0ffe455SJohannes Berg 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3155b0ffe455SJohannes Berg 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3156b0ffe455SJohannes Berg 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3157b0ffe455SJohannes Berg 			return;
3158b0ffe455SJohannes Berg 		}
3159b0ffe455SJohannes Berg 	}
3160b0ffe455SJohannes Berg 
3161e705c121SKalle Valo 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3162e705c121SKalle Valo }
3163e705c121SKalle Valo 
31641e8f1329SGolan Ben-Ami static void
31651e8f1329SGolan Ben-Ami iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
31661e8f1329SGolan Ben-Ami 			   struct ieee80211_vif *vif, u8 *peer_addr,
31671e8f1329SGolan Ben-Ami 			   enum nl80211_tdls_operation action)
31681e8f1329SGolan Ben-Ami {
31691e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tlv *trig;
31701e8f1329SGolan Ben-Ami 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
31711e8f1329SGolan Ben-Ami 
31726c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
31736c042d75SSara Sharon 				     FW_DBG_TRIGGER_TDLS);
31746c042d75SSara Sharon 	if (!trig)
31751e8f1329SGolan Ben-Ami 		return;
31761e8f1329SGolan Ben-Ami 
31771e8f1329SGolan Ben-Ami 	tdls_trig = (void *)trig->data;
31781e8f1329SGolan Ben-Ami 
31791e8f1329SGolan Ben-Ami 	if (!(tdls_trig->action_bitmap & BIT(action)))
31801e8f1329SGolan Ben-Ami 		return;
31811e8f1329SGolan Ben-Ami 
31821e8f1329SGolan Ben-Ami 	if (tdls_trig->peer_mode &&
31831e8f1329SGolan Ben-Ami 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
31841e8f1329SGolan Ben-Ami 		return;
31851e8f1329SGolan Ben-Ami 
31867174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
31871e8f1329SGolan Ben-Ami 				"TDLS event occurred, peer %pM, action %d",
31881e8f1329SGolan Ben-Ami 				peer_addr, action);
31891e8f1329SGolan Ben-Ami }
31901e8f1329SGolan Ben-Ami 
31914f58121dSIlan Peer struct iwl_mvm_he_obss_narrow_bw_ru_data {
31924f58121dSIlan Peer 	bool tolerated;
31934f58121dSIlan Peer };
31944f58121dSIlan Peer 
31954f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
31964f58121dSIlan Peer 						    struct cfg80211_bss *bss,
31974f58121dSIlan Peer 						    void *_data)
31984f58121dSIlan Peer {
31994f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
32006c608cd6SJohannes Berg 	const struct cfg80211_bss_ies *ies;
32014f58121dSIlan Peer 	const struct element *elem;
32024f58121dSIlan Peer 
32036c608cd6SJohannes Berg 	rcu_read_lock();
32046c608cd6SJohannes Berg 	ies = rcu_dereference(bss->ies);
32056c608cd6SJohannes Berg 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
32066c608cd6SJohannes Berg 				  ies->len);
32074f58121dSIlan Peer 
32084f58121dSIlan Peer 	if (!elem || elem->datalen < 10 ||
32094f58121dSIlan Peer 	    !(elem->data[10] &
32104f58121dSIlan Peer 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
32114f58121dSIlan Peer 		data->tolerated = false;
32124f58121dSIlan Peer 	}
32136c608cd6SJohannes Berg 	rcu_read_unlock();
32144f58121dSIlan Peer }
32154f58121dSIlan Peer 
32164f58121dSIlan Peer static void iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
32174f58121dSIlan Peer 					       struct ieee80211_vif *vif)
32184f58121dSIlan Peer {
32194f58121dSIlan Peer 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
32204f58121dSIlan Peer 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
32214f58121dSIlan Peer 		.tolerated = true,
32224f58121dSIlan Peer 	};
32234f58121dSIlan Peer 
32244f58121dSIlan Peer 	if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
32254f58121dSIlan Peer 		mvmvif->he_ru_2mhz_block = false;
32264f58121dSIlan Peer 		return;
32274f58121dSIlan Peer 	}
32284f58121dSIlan Peer 
32294f58121dSIlan Peer 	cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
32304f58121dSIlan Peer 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
32314f58121dSIlan Peer 			  &iter_data);
32324f58121dSIlan Peer 
32334f58121dSIlan Peer 	/*
32344f58121dSIlan Peer 	 * If there is at least one AP on radar channel that cannot
32354f58121dSIlan Peer 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
32364f58121dSIlan Peer 	 */
32374f58121dSIlan Peer 	mvmvif->he_ru_2mhz_block = !iter_data.tolerated;
32384f58121dSIlan Peer }
32394f58121dSIlan Peer 
3240f7d6ef33SJohannes Berg static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3241f7d6ef33SJohannes Berg 					       struct ieee80211_vif *vif)
3242f7d6ef33SJohannes Berg {
3243f7d6ef33SJohannes Berg 	struct ieee80211_supported_band *sband;
3244f7d6ef33SJohannes Berg 	const struct ieee80211_sta_he_cap *he_cap;
3245f7d6ef33SJohannes Berg 
3246f7d6ef33SJohannes Berg 	if (vif->type != NL80211_IFTYPE_STATION)
3247f7d6ef33SJohannes Berg 		return;
3248f7d6ef33SJohannes Berg 
3249f7d6ef33SJohannes Berg 	if (!mvm->cca_40mhz_workaround)
3250f7d6ef33SJohannes Berg 		return;
3251f7d6ef33SJohannes Berg 
3252f7d6ef33SJohannes Berg 	/* decrement and check that we reached zero */
3253f7d6ef33SJohannes Berg 	mvm->cca_40mhz_workaround--;
3254f7d6ef33SJohannes Berg 	if (mvm->cca_40mhz_workaround)
3255f7d6ef33SJohannes Berg 		return;
3256f7d6ef33SJohannes Berg 
3257f7d6ef33SJohannes Berg 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3258f7d6ef33SJohannes Berg 
3259f7d6ef33SJohannes Berg 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3260f7d6ef33SJohannes Berg 
3261f7d6ef33SJohannes Berg 	he_cap = ieee80211_get_he_iftype_cap(sband,
3262f7d6ef33SJohannes Berg 					     ieee80211_vif_type_p2p(vif));
3263f7d6ef33SJohannes Berg 
3264f7d6ef33SJohannes Berg 	if (he_cap) {
3265f7d6ef33SJohannes Berg 		/* we know that ours is writable */
32660301bcd5SBjoern A. Zeeb 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3267f7d6ef33SJohannes Berg 
3268f7d6ef33SJohannes Berg 		he->he_cap_elem.phy_cap_info[0] |=
3269f7d6ef33SJohannes Berg 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3270f7d6ef33SJohannes Berg 	}
3271f7d6ef33SJohannes Berg }
3272f7d6ef33SJohannes Berg 
32736d19a5ebSEmmanuel Grumbach static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
32746d19a5ebSEmmanuel Grumbach 					struct ieee80211_vif *vif,
32756d19a5ebSEmmanuel Grumbach 					struct iwl_mvm_sta *mvm_sta)
32766d19a5ebSEmmanuel Grumbach {
32776d19a5ebSEmmanuel Grumbach #if IS_ENABLED(CONFIG_IWLMEI)
32786d19a5ebSEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
32796d19a5ebSEmmanuel Grumbach 	struct iwl_mei_conn_info conn_info = {
3280f276e20bSJohannes Berg 		.ssid_len = vif->cfg.ssid_len,
32816d19a5ebSEmmanuel Grumbach 		.channel = vif->bss_conf.chandef.chan->hw_value,
32826d19a5ebSEmmanuel Grumbach 	};
32836d19a5ebSEmmanuel Grumbach 
32846d19a5ebSEmmanuel Grumbach 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
32856d19a5ebSEmmanuel Grumbach 		return;
32866d19a5ebSEmmanuel Grumbach 
32876d19a5ebSEmmanuel Grumbach 	if (!mvm->mei_registered)
32886d19a5ebSEmmanuel Grumbach 		return;
32896d19a5ebSEmmanuel Grumbach 
32906d19a5ebSEmmanuel Grumbach 	switch (mvm_sta->pairwise_cipher) {
3291e5d3a64eSAvraham Stern 	case WLAN_CIPHER_SUITE_TKIP:
3292e5d3a64eSAvraham Stern 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3293e5d3a64eSAvraham Stern 		break;
32946d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_CCMP:
32956d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
32966d19a5ebSEmmanuel Grumbach 		break;
32976d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP:
32986d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
32996d19a5ebSEmmanuel Grumbach 		break;
33006d19a5ebSEmmanuel Grumbach 	case WLAN_CIPHER_SUITE_GCMP_256:
33016d19a5ebSEmmanuel Grumbach 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
33026d19a5ebSEmmanuel Grumbach 		break;
33036d19a5ebSEmmanuel Grumbach 	case 0:
33046d19a5ebSEmmanuel Grumbach 		/* open profile */
33056d19a5ebSEmmanuel Grumbach 		break;
33066d19a5ebSEmmanuel Grumbach 	default:
33076d19a5ebSEmmanuel Grumbach 		/* cipher not supported, don't send anything to iwlmei */
33086d19a5ebSEmmanuel Grumbach 		return;
33096d19a5ebSEmmanuel Grumbach 	}
33106d19a5ebSEmmanuel Grumbach 
33116d19a5ebSEmmanuel Grumbach 	switch (mvmvif->rekey_data.akm) {
33126d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_SAE & 0xff:
33136d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
33146d19a5ebSEmmanuel Grumbach 		break;
33156d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_PSK & 0xff:
33166d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
33176d19a5ebSEmmanuel Grumbach 		break;
33186d19a5ebSEmmanuel Grumbach 	case WLAN_AKM_SUITE_8021X & 0xff:
33196d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
33206d19a5ebSEmmanuel Grumbach 		break;
33216d19a5ebSEmmanuel Grumbach 	case 0:
33226d19a5ebSEmmanuel Grumbach 		/* open profile */
33236d19a5ebSEmmanuel Grumbach 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
33246d19a5ebSEmmanuel Grumbach 		break;
33256d19a5ebSEmmanuel Grumbach 	default:
33266d19a5ebSEmmanuel Grumbach 		/* auth method / AKM not supported */
33276d19a5ebSEmmanuel Grumbach 		/* TODO: All the FT vesions of these? */
33286d19a5ebSEmmanuel Grumbach 		return;
33296d19a5ebSEmmanuel Grumbach 	}
33306d19a5ebSEmmanuel Grumbach 
3331f276e20bSJohannes Berg 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
33326d19a5ebSEmmanuel Grumbach 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
33336d19a5ebSEmmanuel Grumbach 
33346d19a5ebSEmmanuel Grumbach 	/* TODO: add support for collocated AP data */
33356d19a5ebSEmmanuel Grumbach 	iwl_mei_host_associated(&conn_info, NULL);
33366d19a5ebSEmmanuel Grumbach #endif
33376d19a5ebSEmmanuel Grumbach }
33386d19a5ebSEmmanuel Grumbach 
3339e705c121SKalle Valo static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3340e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3341e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3342e705c121SKalle Valo 				 enum ieee80211_sta_state old_state,
3343e705c121SKalle Valo 				 enum ieee80211_sta_state new_state)
3344e705c121SKalle Valo {
3345e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3346e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
33476ea29ce5SJohannes Berg 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3348e705c121SKalle Valo 	int ret;
3349e705c121SKalle Valo 
3350e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3351e705c121SKalle Valo 			   sta->addr, old_state, new_state);
3352e705c121SKalle Valo 
3353e705c121SKalle Valo 	/* this would be a mac80211 bug ... but don't crash */
3354e705c121SKalle Valo 	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
3355fe56d05eSSara Sharon 		return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) ? 0 : -EINVAL;
3356e705c121SKalle Valo 
335724afba76SLiad Kaufman 	/*
335824afba76SLiad Kaufman 	 * If we are in a STA removal flow and in DQA mode:
335924afba76SLiad Kaufman 	 *
336024afba76SLiad Kaufman 	 * This is after the sync_rcu part, so the queues have already been
336124afba76SLiad Kaufman 	 * flushed. No more TXs on their way in mac80211's path, and no more in
336224afba76SLiad Kaufman 	 * the queues.
336324afba76SLiad Kaufman 	 * Also, we won't be getting any new TX frames for this station.
336424afba76SLiad Kaufman 	 * What we might have are deferred TX frames that need to be taken care
336524afba76SLiad Kaufman 	 * of.
336624afba76SLiad Kaufman 	 *
336724afba76SLiad Kaufman 	 * Drop any still-queued deferred-frame before removing the STA, and
336824afba76SLiad Kaufman 	 * make sure the worker is no longer handling frames for this STA.
336924afba76SLiad Kaufman 	 */
337024afba76SLiad Kaufman 	if (old_state == IEEE80211_STA_NONE &&
3371c8f54701SJohannes Berg 	    new_state == IEEE80211_STA_NOTEXIST) {
337224afba76SLiad Kaufman 		flush_work(&mvm->add_stream_wk);
337324afba76SLiad Kaufman 
337424afba76SLiad Kaufman 		/*
337524afba76SLiad Kaufman 		 * No need to make sure deferred TX indication is off since the
337624afba76SLiad Kaufman 		 * worker will already remove it if it was on
337724afba76SLiad Kaufman 		 */
3378f7d6ef33SJohannes Berg 
3379f7d6ef33SJohannes Berg 		/*
3380f7d6ef33SJohannes Berg 		 * Additionally, reset the 40 MHz capability if we disconnected
3381f7d6ef33SJohannes Berg 		 * from the AP now.
3382f7d6ef33SJohannes Berg 		 */
3383f7d6ef33SJohannes Berg 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
338424afba76SLiad Kaufman 	}
338524afba76SLiad Kaufman 
3386e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
33876ea29ce5SJohannes Berg 	/* track whether or not the station is associated */
3388d94c5a82SGregory Greenman 	mvm_sta->sta_state = new_state;
33896ea29ce5SJohannes Berg 
3390e705c121SKalle Valo 	if (old_state == IEEE80211_STA_NOTEXIST &&
3391e705c121SKalle Valo 	    new_state == IEEE80211_STA_NONE) {
3392e705c121SKalle Valo 		/*
3393e705c121SKalle Valo 		 * Firmware bug - it'll crash if the beacon interval is less
3394e705c121SKalle Valo 		 * than 16. We can't avoid connecting at all, so refuse the
3395e705c121SKalle Valo 		 * station state change, this will cause mac80211 to abandon
3396e705c121SKalle Valo 		 * attempts to connect to this AP, and eventually wpa_s will
3397cdaba917SEmmanuel Grumbach 		 * blocklist the AP...
3398e705c121SKalle Valo 		 */
3399e705c121SKalle Valo 		if (vif->type == NL80211_IFTYPE_STATION &&
3400e705c121SKalle Valo 		    vif->bss_conf.beacon_int < 16) {
3401e705c121SKalle Valo 			IWL_ERR(mvm,
3402e705c121SKalle Valo 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3403e705c121SKalle Valo 				sta->addr, vif->bss_conf.beacon_int);
3404e705c121SKalle Valo 			ret = -EINVAL;
3405e705c121SKalle Valo 			goto out_unlock;
3406e705c121SKalle Valo 		}
3407e705c121SKalle Valo 
340897cc1694SAvraham Stern 		if (vif->type == NL80211_IFTYPE_STATION)
3409046d2e7cSSriram R 			vif->bss_conf.he_support = sta->deflink.he_cap.has_he;
341097cc1694SAvraham Stern 
3411e705c121SKalle Valo 		if (sta->tdls &&
3412e705c121SKalle Valo 		    (vif->p2p ||
3413e705c121SKalle Valo 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
3414e705c121SKalle Valo 						IWL_MVM_TDLS_STA_COUNT ||
3415e705c121SKalle Valo 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3416e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3417e705c121SKalle Valo 			ret = -EBUSY;
3418e705c121SKalle Valo 			goto out_unlock;
3419e705c121SKalle Valo 		}
3420e705c121SKalle Valo 
3421e705c121SKalle Valo 		ret = iwl_mvm_add_sta(mvm, vif, sta);
34221e8f1329SGolan Ben-Ami 		if (sta->tdls && ret == 0) {
3423e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
34241e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
34251e8f1329SGolan Ben-Ami 						   NL80211_TDLS_SETUP);
34261e8f1329SGolan Ben-Ami 		}
3427438af969SSara Sharon 
34284c51541dSBenjamin Berg 		sta->deflink.agg.max_rc_amsdu_len = 1;
3429e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3430e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
3431e705c121SKalle Valo 		/*
3432e705c121SKalle Valo 		 * EBS may be disabled due to previous failures reported by FW.
3433e705c121SKalle Valo 		 * Reset EBS status here assuming environment has been changed.
3434e705c121SKalle Valo 		 */
3435e705c121SKalle Valo 		mvm->last_ebs_successful = true;
3436e705c121SKalle Valo 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3437e705c121SKalle Valo 		ret = 0;
3438e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3439e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
34408be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
3441046d2e7cSSriram R 			vif->bss_conf.he_support = sta->deflink.he_cap.has_he;
34428be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count++;
34438be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3444cb63eb43SMiri Korenblit 			if ((vif->bss_conf.he_support &&
3445cb63eb43SMiri Korenblit 			     !iwlwifi_mod_params.disable_11ax) ||
3446cb63eb43SMiri Korenblit 			    (vif->bss_conf.eht_support &&
3447cb63eb43SMiri Korenblit 			     !iwlwifi_mod_params.disable_11be))
344802221a81SShaul Triebitz 				iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
34499394662aSLiad Kaufman 		} else if (vif->type == NL80211_IFTYPE_STATION) {
3450046d2e7cSSriram R 			vif->bss_conf.he_support = sta->deflink.he_cap.has_he;
34514f58121dSIlan Peer 
34524f58121dSIlan Peer 			mvmvif->he_ru_2mhz_block = false;
3453046d2e7cSSriram R 			if (sta->deflink.he_cap.has_he)
34544f58121dSIlan Peer 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif);
34554f58121dSIlan Peer 
34569394662aSLiad Kaufman 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
34578be30c13SAyala Beker 		}
3458735a0045SGoodstein, Mordechay 
34593baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
34603baf7528SAvraham Stern 				     false);
3461735a0045SGoodstein, Mordechay 		ret = iwl_mvm_update_sta(mvm, vif, sta);
3462e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3463e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTHORIZED) {
346428916a16SEmmanuel Grumbach 		ret = 0;
3465e705c121SKalle Valo 
3466e705c121SKalle Valo 		/* we don't support TDLS during DCM */
3467e705c121SKalle Valo 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
3468e705c121SKalle Valo 			iwl_mvm_teardown_tdls_peers(mvm);
3469e705c121SKalle Valo 
34704634b176SJohannes Berg 		if (sta->tdls) {
34711e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
34721e8f1329SGolan Ben-Ami 						   NL80211_TDLS_ENABLE_LINK);
34734634b176SJohannes Berg 		} else {
3474e705c121SKalle Valo 			/* enable beacon filtering */
3475e705c121SKalle Valo 			WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
34766b7a5aeaSNaftali Goldstein 
34778b75858cSJohannes Berg 			mvmvif->authorized = 1;
34788b75858cSJohannes Berg 
347950f56044SIlan Peer 			/*
348050f56044SIlan Peer 			 * Now that the station is authorized, i.e., keys were already
348150f56044SIlan Peer 			 * installed, need to indicate to the FW that
348250f56044SIlan Peer 			 * multicast data frames can be forwarded to the driver
348350f56044SIlan Peer 			 */
348450f56044SIlan Peer 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
34856d19a5ebSEmmanuel Grumbach 			iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
34864634b176SJohannes Berg 		}
348750f56044SIlan Peer 
34883baf7528SAvraham Stern 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
34893baf7528SAvraham Stern 				     true);
3490e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3491e705c121SKalle Valo 		   new_state == IEEE80211_STA_ASSOC) {
3492d9e95e35SMordechay Goodstein 		/* once we move into assoc state, need to update rate scale to
3493d9e95e35SMordechay Goodstein 		 * disable using wide bandwidth
3494d9e95e35SMordechay Goodstein 		 */
3495d9e95e35SMordechay Goodstein 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3496d9e95e35SMordechay Goodstein 				     false);
34974634b176SJohannes Berg 		if (!sta->tdls) {
349850f56044SIlan Peer 			/* Multicast data frames are no longer allowed */
349950f56044SIlan Peer 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
350050f56044SIlan Peer 
35018b75858cSJohannes Berg 			/*
35028b75858cSJohannes Berg 			 * Set this after the above iwl_mvm_mac_ctxt_changed()
35038b75858cSJohannes Berg 			 * to avoid sending high prio again for a little time.
35048b75858cSJohannes Berg 			 */
35058b75858cSJohannes Berg 			mvmvif->authorized = 0;
35068b75858cSJohannes Berg 
3507e705c121SKalle Valo 			/* disable beacon filtering */
350869e508b4SIlan Peer 			ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
350969e508b4SIlan Peer 			WARN_ON(ret &&
351069e508b4SIlan Peer 				!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
351169e508b4SIlan Peer 					  &mvm->status));
35124634b176SJohannes Berg 		}
3513e705c121SKalle Valo 		ret = 0;
3514e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_ASSOC &&
3515e705c121SKalle Valo 		   new_state == IEEE80211_STA_AUTH) {
35168be30c13SAyala Beker 		if (vif->type == NL80211_IFTYPE_AP) {
35178be30c13SAyala Beker 			mvmvif->ap_assoc_sta_count--;
35188be30c13SAyala Beker 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3519d5d8ee52SEmmanuel Grumbach 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3520cf7a7457SJohannes Berg 			iwl_mvm_stop_session_protection(mvm, vif);
3521e705c121SKalle Valo 		ret = 0;
3522e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_AUTH &&
3523e705c121SKalle Valo 		   new_state == IEEE80211_STA_NONE) {
3524e705c121SKalle Valo 		ret = 0;
3525e705c121SKalle Valo 	} else if (old_state == IEEE80211_STA_NONE &&
3526e705c121SKalle Valo 		   new_state == IEEE80211_STA_NOTEXIST) {
3527d5d8ee52SEmmanuel Grumbach 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3528d5d8ee52SEmmanuel Grumbach 			iwl_mvm_stop_session_protection(mvm, vif);
3529e705c121SKalle Valo 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
35301e8f1329SGolan Ben-Ami 		if (sta->tdls) {
3531e705c121SKalle Valo 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
35321e8f1329SGolan Ben-Ami 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
35331e8f1329SGolan Ben-Ami 						   NL80211_TDLS_DISABLE_LINK);
35341e8f1329SGolan Ben-Ami 		}
353534a880d8SLiad Kaufman 
353644135b7cSIlan Peer 		if (unlikely(ret &&
353744135b7cSIlan Peer 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
353844135b7cSIlan Peer 				      &mvm->status)))
353944135b7cSIlan Peer 			ret = 0;
3540e705c121SKalle Valo 	} else {
3541e705c121SKalle Valo 		ret = -EIO;
3542e705c121SKalle Valo 	}
3543e705c121SKalle Valo  out_unlock:
3544e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3545e705c121SKalle Valo 
3546e705c121SKalle Valo 	if (sta->tdls && ret == 0) {
3547e705c121SKalle Valo 		if (old_state == IEEE80211_STA_NOTEXIST &&
3548e705c121SKalle Valo 		    new_state == IEEE80211_STA_NONE)
3549e705c121SKalle Valo 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3550e705c121SKalle Valo 		else if (old_state == IEEE80211_STA_NONE &&
3551e705c121SKalle Valo 			 new_state == IEEE80211_STA_NOTEXIST)
3552e705c121SKalle Valo 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3553e705c121SKalle Valo 	}
3554e705c121SKalle Valo 
3555e705c121SKalle Valo 	return ret;
3556e705c121SKalle Valo }
3557e705c121SKalle Valo 
3558e705c121SKalle Valo static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3559e705c121SKalle Valo {
3560e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3561e705c121SKalle Valo 
3562e705c121SKalle Valo 	mvm->rts_threshold = value;
3563e705c121SKalle Valo 
3564e705c121SKalle Valo 	return 0;
3565e705c121SKalle Valo }
3566e705c121SKalle Valo 
3567e705c121SKalle Valo static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3568e705c121SKalle Valo 				  struct ieee80211_vif *vif,
3569e705c121SKalle Valo 				  struct ieee80211_sta *sta, u32 changed)
3570e705c121SKalle Valo {
3571e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3572dcfe3b10SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3573dcfe3b10SJohannes Berg 
3574dcfe3b10SJohannes Berg 	if (changed & (IEEE80211_RC_BW_CHANGED |
3575dcfe3b10SJohannes Berg 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3576dcfe3b10SJohannes Berg 		       IEEE80211_RC_NSS_CHANGED))
3577dcfe3b10SJohannes Berg 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3578dcfe3b10SJohannes Berg 				     true);
3579e705c121SKalle Valo 
3580e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
3581e705c121SKalle Valo 	    changed & IEEE80211_RC_NSS_CHANGED)
3582e705c121SKalle Valo 		iwl_mvm_sf_update(mvm, vif, false);
3583e705c121SKalle Valo }
3584e705c121SKalle Valo 
3585e705c121SKalle Valo static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3586b3e2130bSJohannes Berg 			       struct ieee80211_vif *vif,
3587b3e2130bSJohannes Berg 			       unsigned int link_id, u16 ac,
3588e705c121SKalle Valo 			       const struct ieee80211_tx_queue_params *params)
3589e705c121SKalle Valo {
3590e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3591e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3592e705c121SKalle Valo 
3593e705c121SKalle Valo 	mvmvif->queue_params[ac] = *params;
3594e705c121SKalle Valo 
3595e705c121SKalle Valo 	/*
3596e705c121SKalle Valo 	 * No need to update right away, we'll get BSS_CHANGED_QOS
3597e705c121SKalle Valo 	 * The exception is P2P_DEVICE interface which needs immediate update.
3598e705c121SKalle Valo 	 */
3599e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3600e705c121SKalle Valo 		int ret;
3601e705c121SKalle Valo 
3602e705c121SKalle Valo 		mutex_lock(&mvm->mutex);
3603e705c121SKalle Valo 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3604e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3605e705c121SKalle Valo 		return ret;
3606e705c121SKalle Valo 	}
3607e705c121SKalle Valo 	return 0;
3608e705c121SKalle Valo }
3609e705c121SKalle Valo 
3610e705c121SKalle Valo static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3611d4e36e55SIlan Peer 				       struct ieee80211_vif *vif,
361215fae341SJohannes Berg 				       struct ieee80211_prep_tx_info *info)
3613e705c121SKalle Valo {
3614e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3615d4e36e55SIlan Peer 
3616e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3617af84ac57SJohannes Berg 	iwl_mvm_protect_assoc(mvm, vif, info->duration);
3618e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3619e705c121SKalle Valo }
3620e705c121SKalle Valo 
36216b1259d1SJohannes Berg static void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
36226b1259d1SJohannes Berg 					struct ieee80211_vif *vif,
36236b1259d1SJohannes Berg 					struct ieee80211_prep_tx_info *info)
36246b1259d1SJohannes Berg {
36256b1259d1SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
36266b1259d1SJohannes Berg 
36276b1259d1SJohannes Berg 	/* for successful cases (auth/assoc), don't cancel session protection */
36286b1259d1SJohannes Berg 	if (info->success)
36296b1259d1SJohannes Berg 		return;
36306b1259d1SJohannes Berg 
36316b1259d1SJohannes Berg 	mutex_lock(&mvm->mutex);
36326b1259d1SJohannes Berg 	iwl_mvm_stop_session_protection(mvm, vif);
36336b1259d1SJohannes Berg 	mutex_unlock(&mvm->mutex);
36346b1259d1SJohannes Berg }
36356b1259d1SJohannes Berg 
3636e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3637e705c121SKalle Valo 					struct ieee80211_vif *vif,
3638e705c121SKalle Valo 					struct cfg80211_sched_scan_request *req,
3639e705c121SKalle Valo 					struct ieee80211_scan_ies *ies)
3640e705c121SKalle Valo {
3641e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3642e705c121SKalle Valo 
3643e705c121SKalle Valo 	int ret;
3644e705c121SKalle Valo 
3645e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3646e705c121SKalle Valo 
3647f276e20bSJohannes Berg 	if (!vif->cfg.idle) {
3648e705c121SKalle Valo 		ret = -EBUSY;
3649e705c121SKalle Valo 		goto out;
3650e705c121SKalle Valo 	}
3651e705c121SKalle Valo 
3652e705c121SKalle Valo 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
3653e705c121SKalle Valo 
3654e705c121SKalle Valo out:
3655e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3656e705c121SKalle Valo 	return ret;
3657e705c121SKalle Valo }
3658e705c121SKalle Valo 
3659e705c121SKalle Valo static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3660e705c121SKalle Valo 				       struct ieee80211_vif *vif)
3661e705c121SKalle Valo {
3662e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3663e705c121SKalle Valo 	int ret;
3664e705c121SKalle Valo 
3665e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
3666e705c121SKalle Valo 
3667e705c121SKalle Valo 	/* Due to a race condition, it's possible that mac80211 asks
3668e705c121SKalle Valo 	 * us to stop a sched_scan when it's already stopped.  This
3669e705c121SKalle Valo 	 * can happen, for instance, if we stopped the scan ourselves,
3670e705c121SKalle Valo 	 * called ieee80211_sched_scan_stopped() and the userspace called
3671e705c121SKalle Valo 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3672e705c121SKalle Valo 	 * could run.  To handle this, simply return if the scan is
3673e705c121SKalle Valo 	 * not running.
3674e705c121SKalle Valo 	*/
3675e705c121SKalle Valo 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
3676e705c121SKalle Valo 		mutex_unlock(&mvm->mutex);
3677e705c121SKalle Valo 		return 0;
3678e705c121SKalle Valo 	}
3679e705c121SKalle Valo 
3680e705c121SKalle Valo 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
3681e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
3682e705c121SKalle Valo 	iwl_mvm_wait_for_async_handlers(mvm);
3683e705c121SKalle Valo 
3684e705c121SKalle Valo 	return ret;
3685e705c121SKalle Valo }
3686e705c121SKalle Valo 
36876569e7d3SJohannes Berg static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3688e705c121SKalle Valo 				 enum set_key_cmd cmd,
3689e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3690e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3691e705c121SKalle Valo 				 struct ieee80211_key_conf *key)
3692e705c121SKalle Valo {
3693c56e00a3SJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3694e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
369546c7b05aSEmmanuel Grumbach 	struct iwl_mvm_sta *mvmsta = NULL;
3696f5e28eacSJohannes Berg 	struct iwl_mvm_key_pn *ptk_pn;
3697f5e28eacSJohannes Berg 	int keyidx = key->keyidx;
36985c75a208SJohannes Berg 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
36995c75a208SJohannes Berg 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
3700c56e00a3SJohannes Berg 	int ret, i;
37014615fd15SEmmanuel Grumbach 	u8 key_offset;
3702e705c121SKalle Valo 
370346c7b05aSEmmanuel Grumbach 	if (sta)
37046d19a5ebSEmmanuel Grumbach 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
37056d19a5ebSEmmanuel Grumbach 
3706e705c121SKalle Valo 	switch (key->cipher) {
3707e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3708286ca8ebSLuca Coelho 		if (!mvm->trans->trans_cfg->gen2) {
3709e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
37101ad4f639SEliad Peller 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
37117f768ad5SDavid Spinadel 		} else if (vif->type == NL80211_IFTYPE_STATION) {
37127f768ad5SDavid Spinadel 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
37137f768ad5SDavid Spinadel 		} else {
37147f768ad5SDavid Spinadel 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
37157f768ad5SDavid Spinadel 			return -EOPNOTSUPP;
37167f768ad5SDavid Spinadel 		}
3717e705c121SKalle Valo 		break;
3718e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
37192a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
37202a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
372185aeb58cSDavid Spinadel 		if (!iwl_mvm_has_new_tx_api(mvm))
3722e705c121SKalle Valo 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3723e705c121SKalle Valo 		break;
3724e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_AES_CMAC:
37258e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
37268e160ab8SAyala Beker 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3727e705c121SKalle Valo 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
3728e705c121SKalle Valo 		break;
3729e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3730e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3731475c6bdeSJohannes Berg 		if (vif->type == NL80211_IFTYPE_STATION)
3732e705c121SKalle Valo 			break;
3733475c6bdeSJohannes Berg 		if (iwl_mvm_has_new_tx_api(mvm))
3734475c6bdeSJohannes Berg 			return -EOPNOTSUPP;
3735475c6bdeSJohannes Berg 		/* support HW crypto on TX */
3736475c6bdeSJohannes Berg 		return 0;
3737e705c121SKalle Valo 	default:
3738e705c121SKalle Valo 		return -EOPNOTSUPP;
3739e705c121SKalle Valo 	}
3740e705c121SKalle Valo 
3741e705c121SKalle Valo 	switch (cmd) {
3742e705c121SKalle Valo 	case SET_KEY:
3743b1fdc250SJohannes Berg 		if (keyidx == 6 || keyidx == 7)
3744b1fdc250SJohannes Berg 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
3745b1fdc250SJohannes Berg 					   key);
3746b1fdc250SJohannes Berg 
3747e705c121SKalle Valo 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
3748e705c121SKalle Valo 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
3749e705c121SKalle Valo 			/*
3750e705c121SKalle Valo 			 * GTK on AP interface is a TX-only key, return 0;
3751e705c121SKalle Valo 			 * on IBSS they're per-station and because we're lazy
3752e705c121SKalle Valo 			 * we don't support them for RX, so do the same.
37538e160ab8SAyala Beker 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
3754e705c121SKalle Valo 			 */
37558e160ab8SAyala Beker 			if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
37568e160ab8SAyala Beker 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3757a1c2ff30SAndrei Otcheretianski 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
375881279c49SJohannes Berg 				ret = -EOPNOTSUPP;
3759a1c2ff30SAndrei Otcheretianski 				break;
3760a1c2ff30SAndrei Otcheretianski 			}
376185aeb58cSDavid Spinadel 
376285aeb58cSDavid Spinadel 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
376385aeb58cSDavid Spinadel 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
376485aeb58cSDavid Spinadel 			    !iwl_mvm_has_new_tx_api(mvm)) {
3765e705c121SKalle Valo 				key->hw_key_idx = STA_KEY_IDX_INVALID;
3766a1c2ff30SAndrei Otcheretianski 				ret = 0;
3767e705c121SKalle Valo 				break;
3768e705c121SKalle Valo 			}
3769c56e00a3SJohannes Berg 
3770c56e00a3SJohannes Berg 			if (!mvmvif->ap_ibss_active) {
3771c56e00a3SJohannes Berg 				for (i = 0;
3772c56e00a3SJohannes Berg 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
3773c56e00a3SJohannes Berg 				     i++) {
3774c56e00a3SJohannes Berg 					if (!mvmvif->ap_early_keys[i]) {
3775c56e00a3SJohannes Berg 						mvmvif->ap_early_keys[i] = key;
3776c56e00a3SJohannes Berg 						break;
3777c56e00a3SJohannes Berg 					}
3778c56e00a3SJohannes Berg 				}
3779c56e00a3SJohannes Berg 
3780c56e00a3SJohannes Berg 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
3781c56e00a3SJohannes Berg 					ret = -ENOSPC;
3782a1c2ff30SAndrei Otcheretianski 				else
3783a1c2ff30SAndrei Otcheretianski 					ret = 0;
3784c56e00a3SJohannes Berg 
3785c56e00a3SJohannes Berg 				break;
3786c56e00a3SJohannes Berg 			}
378785aeb58cSDavid Spinadel 		}
3788e705c121SKalle Valo 
3789e705c121SKalle Valo 		/* During FW restart, in order to restore the state as it was,
3790e705c121SKalle Valo 		 * don't try to reprogram keys we previously failed for.
3791e705c121SKalle Valo 		 */
3792e705c121SKalle Valo 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3793e705c121SKalle Valo 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
3794e705c121SKalle Valo 			IWL_DEBUG_MAC80211(mvm,
3795e705c121SKalle Valo 					   "skip invalid idx key programming during restart\n");
3796e705c121SKalle Valo 			ret = 0;
3797e705c121SKalle Valo 			break;
3798e705c121SKalle Valo 		}
3799e705c121SKalle Valo 
3800f5e28eacSJohannes Berg 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
380146c7b05aSEmmanuel Grumbach 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
3802f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3803f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
38042a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
38052a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3806f5e28eacSJohannes Berg 			struct ieee80211_key_seq seq;
3807f5e28eacSJohannes Berg 			int tid, q;
3808f5e28eacSJohannes Berg 
3809f5e28eacSJohannes Berg 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3810acafe7e3SKees Cook 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
3811acafe7e3SKees Cook 						     mvm->trans->num_rx_queues),
3812f5e28eacSJohannes Berg 					 GFP_KERNEL);
3813f5e28eacSJohannes Berg 			if (!ptk_pn) {
3814f5e28eacSJohannes Berg 				ret = -ENOMEM;
3815f5e28eacSJohannes Berg 				break;
3816f5e28eacSJohannes Berg 			}
3817f5e28eacSJohannes Berg 
3818f5e28eacSJohannes Berg 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3819f5e28eacSJohannes Berg 				ieee80211_get_key_rx_seq(key, tid, &seq);
3820f5e28eacSJohannes Berg 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
3821f5e28eacSJohannes Berg 					memcpy(ptk_pn->q[q].pn[tid],
3822f5e28eacSJohannes Berg 					       seq.ccmp.pn,
3823f5e28eacSJohannes Berg 					       IEEE80211_CCMP_PN_LEN);
3824f5e28eacSJohannes Berg 			}
3825f5e28eacSJohannes Berg 
3826f5e28eacSJohannes Berg 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3827f5e28eacSJohannes Berg 		}
3828f5e28eacSJohannes Berg 
38294615fd15SEmmanuel Grumbach 		/* in HW restart reuse the index, otherwise request a new one */
38304615fd15SEmmanuel Grumbach 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
38314615fd15SEmmanuel Grumbach 			key_offset = key->hw_key_idx;
38324615fd15SEmmanuel Grumbach 		else
38334615fd15SEmmanuel Grumbach 			key_offset = STA_KEY_IDX_INVALID;
38344615fd15SEmmanuel Grumbach 
383546c7b05aSEmmanuel Grumbach 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
38366d19a5ebSEmmanuel Grumbach 			mvmsta->pairwise_cipher = key->cipher;
38376d19a5ebSEmmanuel Grumbach 
3838e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
38395c75a208SJohannes Berg 
38405c75a208SJohannes Berg 		if (sec_key_ver)
38415c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
38425c75a208SJohannes Berg 		else
38434615fd15SEmmanuel Grumbach 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
38445c75a208SJohannes Berg 
3845e705c121SKalle Valo 		if (ret) {
3846e705c121SKalle Valo 			IWL_WARN(mvm, "set key failed\n");
3847475c6bdeSJohannes Berg 			key->hw_key_idx = STA_KEY_IDX_INVALID;
3848e705c121SKalle Valo 			/*
3849e705c121SKalle Valo 			 * can't add key for RX, but we don't need it
3850475c6bdeSJohannes Berg 			 * in the device for TX so still return 0,
3851475c6bdeSJohannes Berg 			 * unless we have new TX API where we cannot
3852475c6bdeSJohannes Berg 			 * put key material into the TX_CMD
3853e705c121SKalle Valo 			 */
3854475c6bdeSJohannes Berg 			if (iwl_mvm_has_new_tx_api(mvm))
3855475c6bdeSJohannes Berg 				ret = -EOPNOTSUPP;
3856475c6bdeSJohannes Berg 			else
3857e705c121SKalle Valo 				ret = 0;
3858e705c121SKalle Valo 		}
3859e705c121SKalle Valo 
3860e705c121SKalle Valo 		break;
3861e705c121SKalle Valo 	case DISABLE_KEY:
3862b1fdc250SJohannes Berg 		if (keyidx == 6 || keyidx == 7)
3863b1fdc250SJohannes Berg 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
3864b1fdc250SJohannes Berg 					 NULL);
3865b1fdc250SJohannes Berg 
3866c56e00a3SJohannes Berg 		ret = -ENOENT;
3867c56e00a3SJohannes Berg 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3868c56e00a3SJohannes Berg 			if (mvmvif->ap_early_keys[i] == key) {
3869c56e00a3SJohannes Berg 				mvmvif->ap_early_keys[i] = NULL;
3870c56e00a3SJohannes Berg 				ret = 0;
3871c56e00a3SJohannes Berg 			}
3872c56e00a3SJohannes Berg 		}
3873c56e00a3SJohannes Berg 
3874c56e00a3SJohannes Berg 		/* found in pending list - don't do anything else */
3875c56e00a3SJohannes Berg 		if (ret == 0)
3876c56e00a3SJohannes Berg 			break;
3877c56e00a3SJohannes Berg 
3878e705c121SKalle Valo 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3879e705c121SKalle Valo 			ret = 0;
3880e705c121SKalle Valo 			break;
3881e705c121SKalle Valo 		}
3882e705c121SKalle Valo 
388346c7b05aSEmmanuel Grumbach 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
3884f5e28eacSJohannes Berg 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3885f5e28eacSJohannes Berg 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
38862a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
38872a53d166SAyala Beker 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3888f5e28eacSJohannes Berg 			ptk_pn = rcu_dereference_protected(
3889f5e28eacSJohannes Berg 						mvmsta->ptk_pn[keyidx],
3890f5e28eacSJohannes Berg 						lockdep_is_held(&mvm->mutex));
3891f5e28eacSJohannes Berg 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3892f5e28eacSJohannes Berg 			if (ptk_pn)
3893f5e28eacSJohannes Berg 				kfree_rcu(ptk_pn, rcu_head);
3894f5e28eacSJohannes Berg 		}
3895f5e28eacSJohannes Berg 
3896e705c121SKalle Valo 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
38975c75a208SJohannes Berg 		if (sec_key_ver)
38985c75a208SJohannes Berg 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
38995c75a208SJohannes Berg 		else
3900e705c121SKalle Valo 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3901e705c121SKalle Valo 		break;
3902e705c121SKalle Valo 	default:
3903e705c121SKalle Valo 		ret = -EINVAL;
3904e705c121SKalle Valo 	}
3905e705c121SKalle Valo 
39066569e7d3SJohannes Berg 	return ret;
39076569e7d3SJohannes Berg }
39086569e7d3SJohannes Berg 
39096569e7d3SJohannes Berg static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
39106569e7d3SJohannes Berg 			       enum set_key_cmd cmd,
39116569e7d3SJohannes Berg 			       struct ieee80211_vif *vif,
39126569e7d3SJohannes Berg 			       struct ieee80211_sta *sta,
39136569e7d3SJohannes Berg 			       struct ieee80211_key_conf *key)
39146569e7d3SJohannes Berg {
39156569e7d3SJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
39166569e7d3SJohannes Berg 	int ret;
39176569e7d3SJohannes Berg 
39186569e7d3SJohannes Berg 	mutex_lock(&mvm->mutex);
39196569e7d3SJohannes Berg 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
3920e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
39216569e7d3SJohannes Berg 
3922e705c121SKalle Valo 	return ret;
3923e705c121SKalle Valo }
3924e705c121SKalle Valo 
3925e705c121SKalle Valo static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3926e705c121SKalle Valo 					struct ieee80211_vif *vif,
3927e705c121SKalle Valo 					struct ieee80211_key_conf *keyconf,
3928e705c121SKalle Valo 					struct ieee80211_sta *sta,
3929e705c121SKalle Valo 					u32 iv32, u16 *phase1key)
3930e705c121SKalle Valo {
3931e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3932e705c121SKalle Valo 
3933e705c121SKalle Valo 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3934e705c121SKalle Valo 		return;
3935e705c121SKalle Valo 
3936e705c121SKalle Valo 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3937e705c121SKalle Valo }
3938e705c121SKalle Valo 
3939e705c121SKalle Valo 
3940e705c121SKalle Valo static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3941e705c121SKalle Valo 			       struct iwl_rx_packet *pkt, void *data)
3942e705c121SKalle Valo {
3943e705c121SKalle Valo 	struct iwl_mvm *mvm =
3944e705c121SKalle Valo 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3945e705c121SKalle Valo 	struct iwl_hs20_roc_res *resp;
3946e705c121SKalle Valo 	int resp_len = iwl_rx_packet_payload_len(pkt);
3947e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = data;
3948e705c121SKalle Valo 
3949e705c121SKalle Valo 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3950e705c121SKalle Valo 		return true;
3951e705c121SKalle Valo 
3952e705c121SKalle Valo 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3953e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3954e705c121SKalle Valo 		return true;
3955e705c121SKalle Valo 	}
3956e705c121SKalle Valo 
3957e705c121SKalle Valo 	resp = (void *)pkt->data;
3958e705c121SKalle Valo 
3959e705c121SKalle Valo 	IWL_DEBUG_TE(mvm,
3960b71a9c35SColin Ian King 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
3961e705c121SKalle Valo 		     resp->status, resp->event_unique_id);
3962e705c121SKalle Valo 
3963e705c121SKalle Valo 	te_data->uid = le32_to_cpu(resp->event_unique_id);
3964e705c121SKalle Valo 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3965e705c121SKalle Valo 		     te_data->uid);
3966e705c121SKalle Valo 
3967e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
3968e705c121SKalle Valo 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3969e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
3970e705c121SKalle Valo 
3971e705c121SKalle Valo 	return true;
3972e705c121SKalle Valo }
3973e705c121SKalle Valo 
3974dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3975dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3976dc28e12fSMatti Gottlieb #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3977dc28e12fSMatti Gottlieb #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3978dc28e12fSMatti Gottlieb #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3979e705c121SKalle Valo static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3980e705c121SKalle Valo 				    struct ieee80211_channel *channel,
3981e705c121SKalle Valo 				    struct ieee80211_vif *vif,
3982e705c121SKalle Valo 				    int duration)
3983e705c121SKalle Valo {
3984afc1e3b4SAvraham Stern 	int res;
3985e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3986e705c121SKalle Valo 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3987e705c121SKalle Valo 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
3988e705c121SKalle Valo 	struct iwl_notification_wait wait_time_event;
3989dc28e12fSMatti Gottlieb 	u32 dtim_interval = vif->bss_conf.dtim_period *
3990dc28e12fSMatti Gottlieb 		vif->bss_conf.beacon_int;
3991dc28e12fSMatti Gottlieb 	u32 req_dur, delay;
3992e705c121SKalle Valo 	struct iwl_hs20_roc_req aux_roc_req = {
3993e705c121SKalle Valo 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3994e705c121SKalle Valo 		.id_and_color =
3995e705c121SKalle Valo 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3996e705c121SKalle Valo 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3997e705c121SKalle Valo 	};
399857e861d9SDavid Spinadel 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
399957e861d9SDavid Spinadel 		&aux_roc_req.channel_info);
400057e861d9SDavid Spinadel 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
400157e861d9SDavid Spinadel 
400257e861d9SDavid Spinadel 	/* Set the channel info data */
400357e861d9SDavid Spinadel 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
40043717f91aSTova Mussai 			      iwl_mvm_phy_band_from_nl80211(channel->band),
40057ac87575SJohannes Berg 			      IWL_PHY_CHANNEL_MODE20,
400657e861d9SDavid Spinadel 			      0);
400757e861d9SDavid Spinadel 
400857e861d9SDavid Spinadel 	/* Set the time and duration */
4009afc1e3b4SAvraham Stern 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4010e705c121SKalle Valo 
4011dc28e12fSMatti Gottlieb 	delay = AUX_ROC_MIN_DELAY;
4012dc28e12fSMatti Gottlieb 	req_dur = MSEC_TO_TU(duration);
4013dc28e12fSMatti Gottlieb 
4014dc28e12fSMatti Gottlieb 	/*
4015dc28e12fSMatti Gottlieb 	 * If we are associated we want the delay time to be at least one
4016dc28e12fSMatti Gottlieb 	 * dtim interval so that the FW can wait until after the DTIM and
4017dc28e12fSMatti Gottlieb 	 * then start the time event, this will potentially allow us to
4018dc28e12fSMatti Gottlieb 	 * remain off-channel for the max duration.
4019dc28e12fSMatti Gottlieb 	 * Since we want to use almost a whole dtim interval we would also
4020dc28e12fSMatti Gottlieb 	 * like the delay to be for 2-3 dtim intervals, in case there are
4021dc28e12fSMatti Gottlieb 	 * other time events with higher priority.
4022dc28e12fSMatti Gottlieb 	 */
4023f276e20bSJohannes Berg 	if (vif->cfg.assoc) {
4024dc28e12fSMatti Gottlieb 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
4025dc28e12fSMatti Gottlieb 		/* We cannot remain off-channel longer than the DTIM interval */
4026dc28e12fSMatti Gottlieb 		if (dtim_interval <= req_dur) {
4027dc28e12fSMatti Gottlieb 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
4028dc28e12fSMatti Gottlieb 			if (req_dur <= AUX_ROC_MIN_DURATION)
4029dc28e12fSMatti Gottlieb 				req_dur = dtim_interval -
4030dc28e12fSMatti Gottlieb 					AUX_ROC_MIN_SAFETY_BUFFER;
4031dc28e12fSMatti Gottlieb 		}
4032dc28e12fSMatti Gottlieb 	}
4033dc28e12fSMatti Gottlieb 
403457e861d9SDavid Spinadel 	tail->duration = cpu_to_le32(req_dur);
403557e861d9SDavid Spinadel 	tail->apply_time_max_delay = cpu_to_le32(delay);
4036dc28e12fSMatti Gottlieb 
4037dc28e12fSMatti Gottlieb 	IWL_DEBUG_TE(mvm,
4038903b3f9bSEmmanuel Grumbach 		     "ROC: Requesting to remain on channel %u for %ums\n",
4039903b3f9bSEmmanuel Grumbach 		     channel->hw_value, req_dur);
4040903b3f9bSEmmanuel Grumbach 	IWL_DEBUG_TE(mvm,
4041903b3f9bSEmmanuel Grumbach 		     "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
4042903b3f9bSEmmanuel Grumbach 		     duration, delay, dtim_interval);
4043903b3f9bSEmmanuel Grumbach 
4044e705c121SKalle Valo 	/* Set the node address */
404557e861d9SDavid Spinadel 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4046e705c121SKalle Valo 
4047e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4048e705c121SKalle Valo 
4049e705c121SKalle Valo 	spin_lock_bh(&mvm->time_event_lock);
4050e705c121SKalle Valo 
4051e705c121SKalle Valo 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4052e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4053e705c121SKalle Valo 		return -EIO;
4054e705c121SKalle Valo 	}
4055e705c121SKalle Valo 
4056e705c121SKalle Valo 	te_data->vif = vif;
4057e705c121SKalle Valo 	te_data->duration = duration;
4058e705c121SKalle Valo 	te_data->id = HOT_SPOT_CMD;
4059e705c121SKalle Valo 
4060e705c121SKalle Valo 	spin_unlock_bh(&mvm->time_event_lock);
4061e705c121SKalle Valo 
4062e705c121SKalle Valo 	/*
4063e705c121SKalle Valo 	 * Use a notification wait, which really just processes the
4064e705c121SKalle Valo 	 * command response and doesn't wait for anything, in order
4065e705c121SKalle Valo 	 * to be able to process the response and get the UID inside
4066e705c121SKalle Valo 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4067e705c121SKalle Valo 	 * stores the buffer and then wakes up this thread, by which
4068e705c121SKalle Valo 	 * time another notification (that the time event started)
4069e705c121SKalle Valo 	 * might already be processed unsuccessfully.
4070e705c121SKalle Valo 	 */
4071e705c121SKalle Valo 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4072e705c121SKalle Valo 				   time_event_response,
4073e705c121SKalle Valo 				   ARRAY_SIZE(time_event_response),
4074e705c121SKalle Valo 				   iwl_mvm_rx_aux_roc, te_data);
4075e705c121SKalle Valo 
407657e861d9SDavid Spinadel 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4077e705c121SKalle Valo 				   &aux_roc_req);
4078e705c121SKalle Valo 
4079e705c121SKalle Valo 	if (res) {
4080e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4081e705c121SKalle Valo 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4082e705c121SKalle Valo 		goto out_clear_te;
4083e705c121SKalle Valo 	}
4084e705c121SKalle Valo 
4085e705c121SKalle Valo 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4086e705c121SKalle Valo 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4087e705c121SKalle Valo 	/* should never fail */
4088e705c121SKalle Valo 	WARN_ON_ONCE(res);
4089e705c121SKalle Valo 
4090e705c121SKalle Valo 	if (res) {
4091e705c121SKalle Valo  out_clear_te:
4092e705c121SKalle Valo 		spin_lock_bh(&mvm->time_event_lock);
4093e705c121SKalle Valo 		iwl_mvm_te_clear_data(mvm, te_data);
4094e705c121SKalle Valo 		spin_unlock_bh(&mvm->time_event_lock);
4095e705c121SKalle Valo 	}
4096e705c121SKalle Valo 
4097e705c121SKalle Valo 	return res;
4098e705c121SKalle Valo }
4099e705c121SKalle Valo 
4100e705c121SKalle Valo static int iwl_mvm_roc(struct ieee80211_hw *hw,
4101e705c121SKalle Valo 		       struct ieee80211_vif *vif,
4102e705c121SKalle Valo 		       struct ieee80211_channel *channel,
4103e705c121SKalle Valo 		       int duration,
4104e705c121SKalle Valo 		       enum ieee80211_roc_type type)
4105e705c121SKalle Valo {
4106e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4107e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4108e705c121SKalle Valo 	struct cfg80211_chan_def chandef;
4109e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
41108835a64fSJohannes Berg 	bool band_change_removal;
4111e705c121SKalle Valo 	int ret, i;
4112e705c121SKalle Valo 
4113e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4114e705c121SKalle Valo 			   duration, type);
4115e705c121SKalle Valo 
41169834781cSJohannes Berg 	/*
41179834781cSJohannes Berg 	 * Flush the done work, just in case it's still pending, so that
41189834781cSJohannes Berg 	 * the work it does can complete and we can accept new frames.
41199834781cSJohannes Berg 	 */
4120e705c121SKalle Valo 	flush_work(&mvm->roc_done_wk);
4121e705c121SKalle Valo 
4122e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4123e705c121SKalle Valo 
4124e705c121SKalle Valo 	switch (vif->type) {
4125e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4126e705c121SKalle Valo 		if (fw_has_capa(&mvm->fw->ucode_capa,
4127e705c121SKalle Valo 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4128e705c121SKalle Valo 			/* Use aux roc framework (HS20) */
4129971cbe50SJohannes Berg 			if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12) {
41302c2c3647SNathan Errera 				u32 lmac_id;
41312c2c3647SNathan Errera 
41322c2c3647SNathan Errera 				lmac_id = iwl_mvm_get_lmac_id(mvm->fw,
41332c2c3647SNathan Errera 							      channel->band);
41342c2c3647SNathan Errera 				ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
41352c2c3647SNathan Errera 				if (WARN(ret,
41362c2c3647SNathan Errera 					 "Failed to allocate aux station"))
41372c2c3647SNathan Errera 					goto out_unlock;
41382c2c3647SNathan Errera 			}
4139e705c121SKalle Valo 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
4140e705c121SKalle Valo 						       vif, duration);
4141e705c121SKalle Valo 			goto out_unlock;
4142e705c121SKalle Valo 		}
4143e705c121SKalle Valo 		IWL_ERR(mvm, "hotspot not supported\n");
4144e705c121SKalle Valo 		ret = -EINVAL;
4145e705c121SKalle Valo 		goto out_unlock;
4146e705c121SKalle Valo 	case NL80211_IFTYPE_P2P_DEVICE:
4147e705c121SKalle Valo 		/* handle below */
4148e705c121SKalle Valo 		break;
4149e705c121SKalle Valo 	default:
4150e705c121SKalle Valo 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
4151e705c121SKalle Valo 		ret = -EINVAL;
4152e705c121SKalle Valo 		goto out_unlock;
4153e705c121SKalle Valo 	}
4154e705c121SKalle Valo 
4155e705c121SKalle Valo 	for (i = 0; i < NUM_PHY_CTX; i++) {
4156e705c121SKalle Valo 		phy_ctxt = &mvm->phy_ctxts[i];
4157e705c121SKalle Valo 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
4158e705c121SKalle Valo 			continue;
4159e705c121SKalle Valo 
4160e705c121SKalle Valo 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
4161e705c121SKalle Valo 			/*
4162e705c121SKalle Valo 			 * Unbind the P2P_DEVICE from the current PHY context,
4163e705c121SKalle Valo 			 * and if the PHY context is not used remove it.
4164e705c121SKalle Valo 			 */
4165e705c121SKalle Valo 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
4166e705c121SKalle Valo 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
4167e705c121SKalle Valo 				goto out_unlock;
4168e705c121SKalle Valo 
4169e705c121SKalle Valo 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
4170e705c121SKalle Valo 
4171e705c121SKalle Valo 			/* Bind the P2P_DEVICE to the current PHY Context */
4172e705c121SKalle Valo 			mvmvif->phy_ctxt = phy_ctxt;
4173e705c121SKalle Valo 
4174e705c121SKalle Valo 			ret = iwl_mvm_binding_add_vif(mvm, vif);
4175e705c121SKalle Valo 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4176e705c121SKalle Valo 				goto out_unlock;
4177e705c121SKalle Valo 
4178e705c121SKalle Valo 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
4179e705c121SKalle Valo 			goto schedule_time_event;
4180e705c121SKalle Valo 		}
4181e705c121SKalle Valo 	}
4182e705c121SKalle Valo 
4183e705c121SKalle Valo 	/* Need to update the PHY context only if the ROC channel changed */
4184e705c121SKalle Valo 	if (channel == mvmvif->phy_ctxt->channel)
4185e705c121SKalle Valo 		goto schedule_time_event;
4186e705c121SKalle Valo 
4187e705c121SKalle Valo 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4188e705c121SKalle Valo 
4189e705c121SKalle Valo 	/*
41908835a64fSJohannes Berg 	 * Check if the remain-on-channel is on a different band and that
41918835a64fSJohannes Berg 	 * requires context removal, see iwl_mvm_phy_ctxt_changed(). If
41928835a64fSJohannes Berg 	 * so, we'll need to release and then re-configure here, since we
41938835a64fSJohannes Berg 	 * must not remove a PHY context that's part of a binding.
4194e705c121SKalle Valo 	 */
41958835a64fSJohannes Berg 	band_change_removal =
41968835a64fSJohannes Berg 		fw_has_capa(&mvm->fw->ucode_capa,
41978835a64fSJohannes Berg 			    IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
41988835a64fSJohannes Berg 		mvmvif->phy_ctxt->channel->band != chandef.chan->band;
41998835a64fSJohannes Berg 
42008835a64fSJohannes Berg 	if (mvmvif->phy_ctxt->ref == 1 && !band_change_removal) {
42018835a64fSJohannes Berg 		/*
42028835a64fSJohannes Berg 		 * Change the PHY context configuration as it is currently
42038835a64fSJohannes Berg 		 * referenced only by the P2P Device MAC (and we can modify it)
42048835a64fSJohannes Berg 		 */
4205e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
4206e705c121SKalle Valo 					       &chandef, 1, 1);
4207e705c121SKalle Valo 		if (ret)
4208e705c121SKalle Valo 			goto out_unlock;
4209e705c121SKalle Valo 	} else {
4210e705c121SKalle Valo 		/*
42118835a64fSJohannes Berg 		 * The PHY context is shared with other MACs (or we're trying to
42128835a64fSJohannes Berg 		 * switch bands), so remove the P2P Device from the binding,
42138835a64fSJohannes Berg 		 * allocate an new PHY context and create a new binding.
4214e705c121SKalle Valo 		 */
4215e705c121SKalle Valo 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4216e705c121SKalle Valo 		if (!phy_ctxt) {
4217e705c121SKalle Valo 			ret = -ENOSPC;
4218e705c121SKalle Valo 			goto out_unlock;
4219e705c121SKalle Valo 		}
4220e705c121SKalle Valo 
4221e705c121SKalle Valo 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
4222e705c121SKalle Valo 					       1, 1);
4223e705c121SKalle Valo 		if (ret) {
4224e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to change PHY context\n");
4225e705c121SKalle Valo 			goto out_unlock;
4226e705c121SKalle Valo 		}
4227e705c121SKalle Valo 
4228e705c121SKalle Valo 		/* Unbind the P2P_DEVICE from the current PHY context */
4229e705c121SKalle Valo 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
4230e705c121SKalle Valo 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
4231e705c121SKalle Valo 			goto out_unlock;
4232e705c121SKalle Valo 
4233e705c121SKalle Valo 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
4234e705c121SKalle Valo 
4235e705c121SKalle Valo 		/* Bind the P2P_DEVICE to the new allocated PHY context */
4236e705c121SKalle Valo 		mvmvif->phy_ctxt = phy_ctxt;
4237e705c121SKalle Valo 
4238e705c121SKalle Valo 		ret = iwl_mvm_binding_add_vif(mvm, vif);
4239e705c121SKalle Valo 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4240e705c121SKalle Valo 			goto out_unlock;
4241e705c121SKalle Valo 
4242e705c121SKalle Valo 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
4243e705c121SKalle Valo 	}
4244e705c121SKalle Valo 
4245e705c121SKalle Valo schedule_time_event:
4246e705c121SKalle Valo 	/* Schedule the time events */
4247e705c121SKalle Valo 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4248e705c121SKalle Valo 
4249e705c121SKalle Valo out_unlock:
4250e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4251e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4252e705c121SKalle Valo 	return ret;
4253e705c121SKalle Valo }
4254e705c121SKalle Valo 
42555db4c4b9SEmmanuel Grumbach static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
42565db4c4b9SEmmanuel Grumbach 			      struct ieee80211_vif *vif)
4257e705c121SKalle Valo {
4258e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4259e705c121SKalle Valo 
4260e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4261e705c121SKalle Valo 
4262e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4263fe959c7bSEmmanuel Grumbach 	iwl_mvm_stop_roc(mvm, vif);
4264e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4265e705c121SKalle Valo 
4266e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4267e705c121SKalle Valo 	return 0;
4268e705c121SKalle Valo }
4269e705c121SKalle Valo 
4270b73f9a4aSJohannes Berg struct iwl_mvm_ftm_responder_iter_data {
4271b73f9a4aSJohannes Berg 	bool responder;
4272b73f9a4aSJohannes Berg 	struct ieee80211_chanctx_conf *ctx;
4273b73f9a4aSJohannes Berg };
4274b73f9a4aSJohannes Berg 
4275b73f9a4aSJohannes Berg static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4276b73f9a4aSJohannes Berg 					       struct ieee80211_vif *vif)
4277b73f9a4aSJohannes Berg {
4278b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4279b73f9a4aSJohannes Berg 
4280d0a9123eSJohannes Berg 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4281b73f9a4aSJohannes Berg 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4282b73f9a4aSJohannes Berg 		data->responder = true;
4283b73f9a4aSJohannes Berg }
4284b73f9a4aSJohannes Berg 
4285b73f9a4aSJohannes Berg static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4286b73f9a4aSJohannes Berg 					     struct ieee80211_chanctx_conf *ctx)
4287b73f9a4aSJohannes Berg {
4288b73f9a4aSJohannes Berg 	struct iwl_mvm_ftm_responder_iter_data data = {
4289b73f9a4aSJohannes Berg 		.responder = false,
4290b73f9a4aSJohannes Berg 		.ctx = ctx,
4291b73f9a4aSJohannes Berg 	};
4292b73f9a4aSJohannes Berg 
4293b73f9a4aSJohannes Berg 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4294b73f9a4aSJohannes Berg 					IEEE80211_IFACE_ITER_NORMAL,
4295b73f9a4aSJohannes Berg 					iwl_mvm_ftm_responder_chanctx_iter,
4296b73f9a4aSJohannes Berg 					&data);
4297b73f9a4aSJohannes Berg 	return data.responder;
4298b73f9a4aSJohannes Berg }
4299b73f9a4aSJohannes Berg 
4300e705c121SKalle Valo static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4301e705c121SKalle Valo 				 struct ieee80211_chanctx_conf *ctx)
4302e705c121SKalle Valo {
4303e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4304e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4305b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4306b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4307e705c121SKalle Valo 	int ret;
4308e705c121SKalle Valo 
4309e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4310e705c121SKalle Valo 
4311e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4312e705c121SKalle Valo 
4313e705c121SKalle Valo 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4314e705c121SKalle Valo 	if (!phy_ctxt) {
4315e705c121SKalle Valo 		ret = -ENOSPC;
4316e705c121SKalle Valo 		goto out;
4317e705c121SKalle Valo 	}
4318e705c121SKalle Valo 
4319b73f9a4aSJohannes Berg 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4320e705c121SKalle Valo 				       ctx->rx_chains_static,
4321e705c121SKalle Valo 				       ctx->rx_chains_dynamic);
4322e705c121SKalle Valo 	if (ret) {
4323e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to add PHY context\n");
4324e705c121SKalle Valo 		goto out;
4325e705c121SKalle Valo 	}
4326e705c121SKalle Valo 
4327e705c121SKalle Valo 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4328e705c121SKalle Valo 	*phy_ctxt_id = phy_ctxt->id;
4329e705c121SKalle Valo out:
4330e705c121SKalle Valo 	return ret;
4331e705c121SKalle Valo }
4332e705c121SKalle Valo 
4333e705c121SKalle Valo static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4334e705c121SKalle Valo 			       struct ieee80211_chanctx_conf *ctx)
4335e705c121SKalle Valo {
4336e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4337e705c121SKalle Valo 	int ret;
4338e705c121SKalle Valo 
4339e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4340e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4341e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4342e705c121SKalle Valo 
4343e705c121SKalle Valo 	return ret;
4344e705c121SKalle Valo }
4345e705c121SKalle Valo 
4346e705c121SKalle Valo static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4347e705c121SKalle Valo 				     struct ieee80211_chanctx_conf *ctx)
4348e705c121SKalle Valo {
4349e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4350e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4351e705c121SKalle Valo 
4352e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4353e705c121SKalle Valo 
4354e705c121SKalle Valo 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4355e705c121SKalle Valo }
4356e705c121SKalle Valo 
4357e705c121SKalle Valo static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4358e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx)
4359e705c121SKalle Valo {
4360e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4361e705c121SKalle Valo 
4362e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4363e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, ctx);
4364e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4365e705c121SKalle Valo }
4366e705c121SKalle Valo 
4367e705c121SKalle Valo static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4368e705c121SKalle Valo 				   struct ieee80211_chanctx_conf *ctx,
4369e705c121SKalle Valo 				   u32 changed)
4370e705c121SKalle Valo {
4371e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4372e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4373e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4374b73f9a4aSJohannes Berg 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4375b73f9a4aSJohannes Berg 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4376e705c121SKalle Valo 
4377e705c121SKalle Valo 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4378e705c121SKalle Valo 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4379e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4380e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4381e705c121SKalle Valo 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4382e705c121SKalle Valo 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4383e705c121SKalle Valo 		      phy_ctxt->ref, changed))
4384e705c121SKalle Valo 		return;
4385e705c121SKalle Valo 
4386e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
43877a20bcceSEmmanuel Grumbach 
43887a20bcceSEmmanuel Grumbach 	/* we are only changing the min_width, may be a noop */
43897a20bcceSEmmanuel Grumbach 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4390b73f9a4aSJohannes Berg 		if (phy_ctxt->width == def->width)
43917a20bcceSEmmanuel Grumbach 			goto out_unlock;
43927a20bcceSEmmanuel Grumbach 
43937a20bcceSEmmanuel Grumbach 		/* we are just toggling between 20_NOHT and 20 */
43947a20bcceSEmmanuel Grumbach 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4395b73f9a4aSJohannes Berg 		    def->width <= NL80211_CHAN_WIDTH_20)
43967a20bcceSEmmanuel Grumbach 			goto out_unlock;
43977a20bcceSEmmanuel Grumbach 	}
43987a20bcceSEmmanuel Grumbach 
4399e705c121SKalle Valo 	iwl_mvm_bt_coex_vif_change(mvm);
4400b73f9a4aSJohannes Berg 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4401e705c121SKalle Valo 				 ctx->rx_chains_static,
4402e705c121SKalle Valo 				 ctx->rx_chains_dynamic);
44037a20bcceSEmmanuel Grumbach 
44047a20bcceSEmmanuel Grumbach out_unlock:
4405e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4406e705c121SKalle Valo }
4407e705c121SKalle Valo 
4408*8a919a78SMiri Korenblit /*
4409*8a919a78SMiri Korenblit  * This function executes the common part for MLD and non-MLD modes.
4410*8a919a78SMiri Korenblit  *
4411*8a919a78SMiri Korenblit  * Returns true if we're done assigning the chanctx
4412*8a919a78SMiri Korenblit  * (either on failure or success)
4413*8a919a78SMiri Korenblit  */
4414*8a919a78SMiri Korenblit static bool __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
4415e705c121SKalle Valo 						struct ieee80211_vif *vif,
4416e705c121SKalle Valo 						struct ieee80211_chanctx_conf *ctx,
4417*8a919a78SMiri Korenblit 						bool switching_chanctx,
4418*8a919a78SMiri Korenblit 						int *ret)
4419e705c121SKalle Valo {
4420e705c121SKalle Valo 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4421e705c121SKalle Valo 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4422e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4423e705c121SKalle Valo 
4424e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4425e705c121SKalle Valo 
4426e705c121SKalle Valo 	mvmvif->phy_ctxt = phy_ctxt;
4427e705c121SKalle Valo 
4428e705c121SKalle Valo 	switch (vif->type) {
4429e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4430e705c121SKalle Valo 		/* only needed if we're switching chanctx (i.e. during CSA) */
4431e705c121SKalle Valo 		if (switching_chanctx) {
4432e705c121SKalle Valo 			mvmvif->ap_ibss_active = true;
4433e705c121SKalle Valo 			break;
4434e705c121SKalle Valo 		}
44355a2abdcaSGustavo A. R. Silva 		fallthrough;
4436e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4437e705c121SKalle Valo 		/*
4438e705c121SKalle Valo 		 * The AP binding flow is handled as part of the start_ap flow
4439e705c121SKalle Valo 		 * (in bss_info_changed), similarly for IBSS.
4440e705c121SKalle Valo 		 */
4441*8a919a78SMiri Korenblit 		*ret = 0;
4442*8a919a78SMiri Korenblit 		return true;
4443e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4444e705c121SKalle Valo 		break;
4445e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4446e705c121SKalle Valo 		/* always disable PS when a monitor interface is active */
4447e705c121SKalle Valo 		mvmvif->ps_disabled = true;
4448e705c121SKalle Valo 		break;
4449e705c121SKalle Valo 	default:
4450*8a919a78SMiri Korenblit 		*ret = -EINVAL;
4451*8a919a78SMiri Korenblit 		return true;
4452e705c121SKalle Valo 	}
4453*8a919a78SMiri Korenblit 	return false;
4454*8a919a78SMiri Korenblit }
4455*8a919a78SMiri Korenblit 
4456*8a919a78SMiri Korenblit static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4457*8a919a78SMiri Korenblit 					struct ieee80211_vif *vif,
4458*8a919a78SMiri Korenblit 					struct ieee80211_chanctx_conf *ctx,
4459*8a919a78SMiri Korenblit 					bool switching_chanctx)
4460*8a919a78SMiri Korenblit {
4461*8a919a78SMiri Korenblit 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4462*8a919a78SMiri Korenblit 	int ret;
4463*8a919a78SMiri Korenblit 
4464*8a919a78SMiri Korenblit 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
4465*8a919a78SMiri Korenblit 						switching_chanctx, &ret))
4466*8a919a78SMiri Korenblit 		goto out;
4467e705c121SKalle Valo 
4468e705c121SKalle Valo 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4469e705c121SKalle Valo 	if (ret)
4470e705c121SKalle Valo 		goto out;
4471e705c121SKalle Valo 
4472e705c121SKalle Valo 	/*
4473e705c121SKalle Valo 	 * Power state must be updated before quotas,
4474e705c121SKalle Valo 	 * otherwise fw will complain.
4475e705c121SKalle Valo 	 */
4476e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4477e705c121SKalle Valo 
4478e705c121SKalle Valo 	/* Setting the quota at this stage is only required for monitor
4479e705c121SKalle Valo 	 * interfaces. For the other types, the bss_info changed flow
4480e705c121SKalle Valo 	 * will handle quota settings.
4481e705c121SKalle Valo 	 */
4482e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4483e705c121SKalle Valo 		mvmvif->monitor_active = true;
4484e705c121SKalle Valo 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4485e705c121SKalle Valo 		if (ret)
4486e705c121SKalle Valo 			goto out_remove_binding;
44870e39eb03SChaya Rachel Ivgi 
44880e39eb03SChaya Rachel Ivgi 		ret = iwl_mvm_add_snif_sta(mvm, vif);
44890e39eb03SChaya Rachel Ivgi 		if (ret)
44900e39eb03SChaya Rachel Ivgi 			goto out_remove_binding;
44910e39eb03SChaya Rachel Ivgi 
4492e705c121SKalle Valo 	}
4493e705c121SKalle Valo 
4494e705c121SKalle Valo 	/* Handle binding during CSA */
4495e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_AP) {
4496e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4497e705c121SKalle Valo 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4498e705c121SKalle Valo 	}
4499e705c121SKalle Valo 
4500*8a919a78SMiri Korenblit 	if (vif->type == NL80211_IFTYPE_STATION) {
4501*8a919a78SMiri Korenblit 		if (!switching_chanctx) {
4502*8a919a78SMiri Korenblit 			mvmvif->csa_bcn_pending = false;
4503*8a919a78SMiri Korenblit 			goto out;
4504*8a919a78SMiri Korenblit 		}
4505*8a919a78SMiri Korenblit 
450674a10252SSara Sharon 		mvmvif->csa_bcn_pending = true;
450774a10252SSara Sharon 
450874a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
450974a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
451019125cb0SAndrei Otcheretianski 			u32 duration = 3 * vif->bss_conf.beacon_int;
4511e705c121SKalle Valo 
4512e705c121SKalle Valo 			/* Protect the session to make sure we hear the first
4513e705c121SKalle Valo 			 * beacon on the new channel.
4514e705c121SKalle Valo 			 */
4515e705c121SKalle Valo 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4516e705c121SKalle Valo 						vif->bss_conf.beacon_int / 2,
4517e705c121SKalle Valo 						true);
451874a10252SSara Sharon 		}
4519e705c121SKalle Valo 
4520e705c121SKalle Valo 		iwl_mvm_update_quotas(mvm, false, NULL);
4521e705c121SKalle Valo 	}
4522e705c121SKalle Valo 
4523e705c121SKalle Valo 	goto out;
4524e705c121SKalle Valo 
4525e705c121SKalle Valo out_remove_binding:
4526e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4527e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4528e705c121SKalle Valo out:
4529e705c121SKalle Valo 	if (ret)
4530e705c121SKalle Valo 		mvmvif->phy_ctxt = NULL;
4531e705c121SKalle Valo 	return ret;
4532e705c121SKalle Valo }
4533*8a919a78SMiri Korenblit 
4534e705c121SKalle Valo static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4535e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4536727eff4dSGregory Greenman 				      struct ieee80211_bss_conf *link_conf,
4537e705c121SKalle Valo 				      struct ieee80211_chanctx_conf *ctx)
4538e705c121SKalle Valo {
4539e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4540e705c121SKalle Valo 	int ret;
4541e705c121SKalle Valo 
4542e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4543e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
4544e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4545e705c121SKalle Valo 
4546e705c121SKalle Valo 	return ret;
4547e705c121SKalle Valo }
4548e705c121SKalle Valo 
4549e705c121SKalle Valo static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4550e705c121SKalle Valo 					   struct ieee80211_vif *vif,
4551e705c121SKalle Valo 					   struct ieee80211_chanctx_conf *ctx,
4552e705c121SKalle Valo 					   bool switching_chanctx)
4553e705c121SKalle Valo {
4554e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4555e705c121SKalle Valo 	struct ieee80211_vif *disabled_vif = NULL;
4556e705c121SKalle Valo 
4557e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
4558e705c121SKalle Valo 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4559e705c121SKalle Valo 
4560e705c121SKalle Valo 	switch (vif->type) {
4561e705c121SKalle Valo 	case NL80211_IFTYPE_ADHOC:
4562e705c121SKalle Valo 		goto out;
4563e705c121SKalle Valo 	case NL80211_IFTYPE_MONITOR:
4564e705c121SKalle Valo 		mvmvif->monitor_active = false;
4565e705c121SKalle Valo 		mvmvif->ps_disabled = false;
45660e39eb03SChaya Rachel Ivgi 		iwl_mvm_rm_snif_sta(mvm, vif);
4567e705c121SKalle Valo 		break;
4568e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4569e705c121SKalle Valo 		/* This part is triggered only during CSA */
4570e705c121SKalle Valo 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
4571e705c121SKalle Valo 			goto out;
4572e705c121SKalle Valo 
4573e705c121SKalle Valo 		mvmvif->csa_countdown = false;
4574e705c121SKalle Valo 
4575e705c121SKalle Valo 		/* Set CS bit on all the stations */
4576e705c121SKalle Valo 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4577e705c121SKalle Valo 
4578e705c121SKalle Valo 		/* Save blocked iface, the timeout is set on the next beacon */
4579e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4580e705c121SKalle Valo 
4581e705c121SKalle Valo 		mvmvif->ap_ibss_active = false;
4582e705c121SKalle Valo 		break;
4583e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
4584e705c121SKalle Valo 		if (!switching_chanctx)
4585e705c121SKalle Valo 			break;
4586e705c121SKalle Valo 
4587e705c121SKalle Valo 		disabled_vif = vif;
4588e705c121SKalle Valo 
458974a10252SSara Sharon 		if (!fw_has_capa(&mvm->fw->ucode_capa,
459074a10252SSara Sharon 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4591e705c121SKalle Valo 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
4592e705c121SKalle Valo 		break;
4593e705c121SKalle Valo 	default:
4594e705c121SKalle Valo 		break;
4595e705c121SKalle Valo 	}
4596e705c121SKalle Valo 
4597e705c121SKalle Valo 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
4598e705c121SKalle Valo 	iwl_mvm_binding_remove_vif(mvm, vif);
4599e705c121SKalle Valo 
4600e705c121SKalle Valo out:
4601bf544e9aSSara Sharon 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
4602bf544e9aSSara Sharon 	    switching_chanctx)
4603bf544e9aSSara Sharon 		return;
4604e705c121SKalle Valo 	mvmvif->phy_ctxt = NULL;
4605e705c121SKalle Valo 	iwl_mvm_power_update_mac(mvm);
4606e705c121SKalle Valo }
4607e705c121SKalle Valo 
4608e705c121SKalle Valo static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4609e705c121SKalle Valo 					 struct ieee80211_vif *vif,
4610727eff4dSGregory Greenman 					 struct ieee80211_bss_conf *link_conf,
4611e705c121SKalle Valo 					 struct ieee80211_chanctx_conf *ctx)
4612e705c121SKalle Valo {
4613e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4614e705c121SKalle Valo 
4615e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4616e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
4617e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4618e705c121SKalle Valo }
4619e705c121SKalle Valo 
4620e705c121SKalle Valo static int
4621e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4622e705c121SKalle Valo 				struct ieee80211_vif_chanctx_switch *vifs)
4623e705c121SKalle Valo {
4624e705c121SKalle Valo 	int ret;
4625e705c121SKalle Valo 
4626e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4627e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4628e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4629e705c121SKalle Valo 
4630e705c121SKalle Valo 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4631e705c121SKalle Valo 	if (ret) {
4632e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4633e705c121SKalle Valo 		goto out_reassign;
4634e705c121SKalle Valo 	}
4635e705c121SKalle Valo 
4636e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4637e705c121SKalle Valo 					   true);
4638e705c121SKalle Valo 	if (ret) {
4639e705c121SKalle Valo 		IWL_ERR(mvm,
4640e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4641e705c121SKalle Valo 		goto out_remove;
4642e705c121SKalle Valo 	}
4643e705c121SKalle Valo 
4644e705c121SKalle Valo 	/* we don't support TDLS during DCM - can be caused by channel switch */
4645e705c121SKalle Valo 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
4646e705c121SKalle Valo 		iwl_mvm_teardown_tdls_peers(mvm);
4647e705c121SKalle Valo 
4648e705c121SKalle Valo 	goto out;
4649e705c121SKalle Valo 
4650e705c121SKalle Valo out_remove:
4651e705c121SKalle Valo 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4652e705c121SKalle Valo 
4653e705c121SKalle Valo out_reassign:
4654e705c121SKalle Valo 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
4655e705c121SKalle Valo 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4656e705c121SKalle Valo 		goto out_restart;
4657e705c121SKalle Valo 	}
4658e705c121SKalle Valo 
4659e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4660e705c121SKalle Valo 					 true)) {
4661e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4662e705c121SKalle Valo 		goto out_restart;
4663e705c121SKalle Valo 	}
4664e705c121SKalle Valo 
4665e705c121SKalle Valo 	goto out;
4666e705c121SKalle Valo 
4667e705c121SKalle Valo out_restart:
4668e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4669e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4670e705c121SKalle Valo 
4671e705c121SKalle Valo out:
4672e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4673e705c121SKalle Valo 
4674e705c121SKalle Valo 	return ret;
4675e705c121SKalle Valo }
4676e705c121SKalle Valo 
4677e705c121SKalle Valo static int
4678e705c121SKalle Valo iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4679e705c121SKalle Valo 				    struct ieee80211_vif_chanctx_switch *vifs)
4680e705c121SKalle Valo {
4681e705c121SKalle Valo 	int ret;
4682e705c121SKalle Valo 
4683e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4684e705c121SKalle Valo 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4685e705c121SKalle Valo 
4686e705c121SKalle Valo 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4687e705c121SKalle Valo 					   true);
4688e705c121SKalle Valo 	if (ret) {
4689e705c121SKalle Valo 		IWL_ERR(mvm,
4690e705c121SKalle Valo 			"failed to assign new_ctx during channel switch\n");
4691e705c121SKalle Valo 		goto out_reassign;
4692e705c121SKalle Valo 	}
4693e705c121SKalle Valo 
4694e705c121SKalle Valo 	goto out;
4695e705c121SKalle Valo 
4696e705c121SKalle Valo out_reassign:
4697e705c121SKalle Valo 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4698e705c121SKalle Valo 					 true)) {
4699e705c121SKalle Valo 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4700e705c121SKalle Valo 		goto out_restart;
4701e705c121SKalle Valo 	}
4702e705c121SKalle Valo 
4703e705c121SKalle Valo 	goto out;
4704e705c121SKalle Valo 
4705e705c121SKalle Valo out_restart:
4706e705c121SKalle Valo 	/* things keep failing, better restart the hw */
4707e705c121SKalle Valo 	iwl_mvm_nic_restart(mvm, false);
4708e705c121SKalle Valo 
4709e705c121SKalle Valo out:
4710e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4711e705c121SKalle Valo 
4712e705c121SKalle Valo 	return ret;
4713e705c121SKalle Valo }
4714e705c121SKalle Valo 
4715e705c121SKalle Valo static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4716e705c121SKalle Valo 				      struct ieee80211_vif_chanctx_switch *vifs,
4717e705c121SKalle Valo 				      int n_vifs,
4718e705c121SKalle Valo 				      enum ieee80211_chanctx_switch_mode mode)
4719e705c121SKalle Valo {
4720e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4721e705c121SKalle Valo 	int ret;
4722e705c121SKalle Valo 
4723e705c121SKalle Valo 	/* we only support a single-vif right now */
4724e705c121SKalle Valo 	if (n_vifs > 1)
4725e705c121SKalle Valo 		return -EOPNOTSUPP;
4726e705c121SKalle Valo 
4727e705c121SKalle Valo 	switch (mode) {
4728e705c121SKalle Valo 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
4729e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4730e705c121SKalle Valo 		break;
4731e705c121SKalle Valo 	case CHANCTX_SWMODE_REASSIGN_VIF:
4732e705c121SKalle Valo 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
4733e705c121SKalle Valo 		break;
4734e705c121SKalle Valo 	default:
4735e705c121SKalle Valo 		ret = -EOPNOTSUPP;
4736e705c121SKalle Valo 		break;
4737e705c121SKalle Valo 	}
4738e705c121SKalle Valo 
4739e705c121SKalle Valo 	return ret;
4740e705c121SKalle Valo }
4741e705c121SKalle Valo 
47422f0282dbSJohannes Berg static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
47432f0282dbSJohannes Berg {
47442f0282dbSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
47452f0282dbSJohannes Berg 
47462f0282dbSJohannes Berg 	return mvm->ibss_manager;
47472f0282dbSJohannes Berg }
47482f0282dbSJohannes Berg 
4749e705c121SKalle Valo static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4750e705c121SKalle Valo 			   struct ieee80211_sta *sta,
4751e705c121SKalle Valo 			   bool set)
4752e705c121SKalle Valo {
4753e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4754e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4755e705c121SKalle Valo 
4756e705c121SKalle Valo 	if (!mvm_sta || !mvm_sta->vif) {
4757e705c121SKalle Valo 		IWL_ERR(mvm, "Station is not associated to a vif\n");
4758e705c121SKalle Valo 		return -EINVAL;
4759e705c121SKalle Valo 	}
4760e705c121SKalle Valo 
4761e705c121SKalle Valo 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4762e705c121SKalle Valo }
4763e705c121SKalle Valo 
4764e705c121SKalle Valo #ifdef CONFIG_NL80211_TESTMODE
4765e705c121SKalle Valo static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4766e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4767e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
4768e705c121SKalle Valo 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
4769e705c121SKalle Valo };
4770e705c121SKalle Valo 
4771e705c121SKalle Valo static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4772e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4773e705c121SKalle Valo 				      void *data, int len)
4774e705c121SKalle Valo {
4775e705c121SKalle Valo 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4776e705c121SKalle Valo 	int err;
4777e705c121SKalle Valo 	u32 noa_duration;
4778e705c121SKalle Valo 
47798cb08174SJohannes Berg 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
47808cb08174SJohannes Berg 				   iwl_mvm_tm_policy, NULL);
4781e705c121SKalle Valo 	if (err)
4782e705c121SKalle Valo 		return err;
4783e705c121SKalle Valo 
4784e705c121SKalle Valo 	if (!tb[IWL_MVM_TM_ATTR_CMD])
4785e705c121SKalle Valo 		return -EINVAL;
4786e705c121SKalle Valo 
4787e705c121SKalle Valo 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4788e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_NOA:
4789e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4790e705c121SKalle Valo 		    !vif->bss_conf.enable_beacon ||
4791e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4792e705c121SKalle Valo 			return -EINVAL;
4793e705c121SKalle Valo 
4794e705c121SKalle Valo 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4795e705c121SKalle Valo 		if (noa_duration >= vif->bss_conf.beacon_int)
4796e705c121SKalle Valo 			return -EINVAL;
4797e705c121SKalle Valo 
4798e705c121SKalle Valo 		mvm->noa_duration = noa_duration;
4799e705c121SKalle Valo 		mvm->noa_vif = vif;
4800e705c121SKalle Valo 
480122b21041SShaul Triebitz 		return iwl_mvm_update_quotas(mvm, true, NULL);
4802e705c121SKalle Valo 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4803e705c121SKalle Valo 		/* must be associated client vif - ignore authorized */
4804e705c121SKalle Valo 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4805f276e20bSJohannes Berg 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
4806e705c121SKalle Valo 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4807e705c121SKalle Valo 			return -EINVAL;
4808e705c121SKalle Valo 
4809e705c121SKalle Valo 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
4810e705c121SKalle Valo 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4811e705c121SKalle Valo 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4812e705c121SKalle Valo 	}
4813e705c121SKalle Valo 
4814e705c121SKalle Valo 	return -EOPNOTSUPP;
4815e705c121SKalle Valo }
4816e705c121SKalle Valo 
4817e705c121SKalle Valo static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4818e705c121SKalle Valo 				    struct ieee80211_vif *vif,
4819e705c121SKalle Valo 				    void *data, int len)
4820e705c121SKalle Valo {
4821e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4822e705c121SKalle Valo 	int err;
4823e705c121SKalle Valo 
4824e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4825e705c121SKalle Valo 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4826e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
4827e705c121SKalle Valo 
4828e705c121SKalle Valo 	return err;
4829e705c121SKalle Valo }
4830e705c121SKalle Valo #endif
4831e705c121SKalle Valo 
4832e705c121SKalle Valo static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4833e705c121SKalle Valo 				   struct ieee80211_vif *vif,
4834e705c121SKalle Valo 				   struct ieee80211_channel_switch *chsw)
4835e705c121SKalle Valo {
4836e705c121SKalle Valo 	/* By implementing this operation, we prevent mac80211 from
4837e705c121SKalle Valo 	 * starting its own channel switch timer, so that we can call
4838e705c121SKalle Valo 	 * ieee80211_chswitch_done() ourselves at the right time
4839e705c121SKalle Valo 	 * (which is when the absence time event starts).
4840e705c121SKalle Valo 	 */
4841e705c121SKalle Valo 
4842e705c121SKalle Valo 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4843e705c121SKalle Valo 			   "dummy channel switch op\n");
4844e705c121SKalle Valo }
4845e705c121SKalle Valo 
484674a10252SSara Sharon static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
484774a10252SSara Sharon 				       struct ieee80211_vif *vif,
484874a10252SSara Sharon 				       struct ieee80211_channel_switch *chsw)
484974a10252SSara Sharon {
485074a10252SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
485174a10252SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
485274a10252SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
485374a10252SSara Sharon 							  mvmvif->color)),
485474a10252SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
485574a10252SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
485674a10252SSara Sharon 		.cs_count = chsw->count,
485777738865SSara Sharon 		.cs_mode = chsw->block_tx,
485874a10252SSara Sharon 	};
485974a10252SSara Sharon 
486074a10252SSara Sharon 	lockdep_assert_held(&mvm->mutex);
486174a10252SSara Sharon 
48629cfcf71cSSara Sharon 	if (chsw->delay)
48639cfcf71cSSara Sharon 		cmd.cs_delayed_bcn_count =
48649cfcf71cSSara Sharon 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
48659cfcf71cSSara Sharon 
486674a10252SSara Sharon 	return iwl_mvm_send_cmd_pdu(mvm,
486774a10252SSara Sharon 				    WIDE_ID(MAC_CONF_GROUP,
486874a10252SSara Sharon 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
486974a10252SSara Sharon 				    0, sizeof(cmd), &cmd);
487074a10252SSara Sharon }
487174a10252SSara Sharon 
48720202bcf0SEmmanuel Grumbach static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
48730202bcf0SEmmanuel Grumbach 				       struct ieee80211_vif *vif,
48740202bcf0SEmmanuel Grumbach 				       struct ieee80211_channel_switch *chsw)
48750202bcf0SEmmanuel Grumbach {
48760202bcf0SEmmanuel Grumbach 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
48770202bcf0SEmmanuel Grumbach 	u32 apply_time;
48780202bcf0SEmmanuel Grumbach 
48790202bcf0SEmmanuel Grumbach 	/* Schedule the time event to a bit before beacon 1,
48800202bcf0SEmmanuel Grumbach 	 * to make sure we're in the new channel when the
48810202bcf0SEmmanuel Grumbach 	 * GO/AP arrives. In case count <= 1 immediately schedule the
48820202bcf0SEmmanuel Grumbach 	 * TE (this might result with some packet loss or connection
48830202bcf0SEmmanuel Grumbach 	 * loss).
48840202bcf0SEmmanuel Grumbach 	 */
48850202bcf0SEmmanuel Grumbach 	if (chsw->count <= 1)
48860202bcf0SEmmanuel Grumbach 		apply_time = 0;
48870202bcf0SEmmanuel Grumbach 	else
48880202bcf0SEmmanuel Grumbach 		apply_time = chsw->device_timestamp +
48890202bcf0SEmmanuel Grumbach 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
48900202bcf0SEmmanuel Grumbach 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
48910202bcf0SEmmanuel Grumbach 
48920202bcf0SEmmanuel Grumbach 	if (chsw->block_tx)
48930202bcf0SEmmanuel Grumbach 		iwl_mvm_csa_client_absent(mvm, vif);
48940202bcf0SEmmanuel Grumbach 
48950202bcf0SEmmanuel Grumbach 	if (mvmvif->bf_data.bf_enabled) {
48960202bcf0SEmmanuel Grumbach 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
48970202bcf0SEmmanuel Grumbach 
48980202bcf0SEmmanuel Grumbach 		if (ret)
48990202bcf0SEmmanuel Grumbach 			return ret;
49000202bcf0SEmmanuel Grumbach 	}
49010202bcf0SEmmanuel Grumbach 
49020202bcf0SEmmanuel Grumbach 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
49030202bcf0SEmmanuel Grumbach 				    apply_time);
49040202bcf0SEmmanuel Grumbach 
49050202bcf0SEmmanuel Grumbach 	return 0;
49060202bcf0SEmmanuel Grumbach }
49070202bcf0SEmmanuel Grumbach 
4908f6780614SSara Sharon #define IWL_MAX_CSA_BLOCK_TX 1500
4909e705c121SKalle Valo static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4910e705c121SKalle Valo 				      struct ieee80211_vif *vif,
4911e705c121SKalle Valo 				      struct ieee80211_channel_switch *chsw)
4912e705c121SKalle Valo {
4913e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4914e705c121SKalle Valo 	struct ieee80211_vif *csa_vif;
4915e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4916e705c121SKalle Valo 	int ret;
4917e705c121SKalle Valo 
4918e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
4919e705c121SKalle Valo 
4920e705c121SKalle Valo 	mvmvif->csa_failed = false;
4921e705c121SKalle Valo 
4922e705c121SKalle Valo 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
4923e705c121SKalle Valo 			   chsw->chandef.center_freq1);
4924e705c121SKalle Valo 
49257174beb6SJohannes Berg 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
49267174beb6SJohannes Berg 				       ieee80211_vif_to_wdev(vif),
49277174beb6SJohannes Berg 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
4928e705c121SKalle Valo 
4929e705c121SKalle Valo 	switch (vif->type) {
4930e705c121SKalle Valo 	case NL80211_IFTYPE_AP:
4931e705c121SKalle Valo 		csa_vif =
4932e705c121SKalle Valo 			rcu_dereference_protected(mvm->csa_vif,
4933e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
4934d0a9123eSJohannes Berg 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
4935e705c121SKalle Valo 			      "Another CSA is already in progress")) {
4936e705c121SKalle Valo 			ret = -EBUSY;
4937e705c121SKalle Valo 			goto out_unlock;
4938e705c121SKalle Valo 		}
4939e705c121SKalle Valo 
4940d3a108a4SAndrei Otcheretianski 		/* we still didn't unblock tx. prevent new CS meanwhile */
4941d3a108a4SAndrei Otcheretianski 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
4942d3a108a4SAndrei Otcheretianski 					      lockdep_is_held(&mvm->mutex))) {
4943d3a108a4SAndrei Otcheretianski 			ret = -EBUSY;
4944d3a108a4SAndrei Otcheretianski 			goto out_unlock;
4945d3a108a4SAndrei Otcheretianski 		}
4946d3a108a4SAndrei Otcheretianski 
4947e705c121SKalle Valo 		rcu_assign_pointer(mvm->csa_vif, vif);
4948e705c121SKalle Valo 
4949e705c121SKalle Valo 		if (WARN_ONCE(mvmvif->csa_countdown,
4950e705c121SKalle Valo 			      "Previous CSA countdown didn't complete")) {
4951e705c121SKalle Valo 			ret = -EBUSY;
4952e705c121SKalle Valo 			goto out_unlock;
4953e705c121SKalle Valo 		}
4954e705c121SKalle Valo 
4955d3a108a4SAndrei Otcheretianski 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
4956d3a108a4SAndrei Otcheretianski 
4957e705c121SKalle Valo 		break;
4958e705c121SKalle Valo 	case NL80211_IFTYPE_STATION:
49592360acbdSEmmanuel Grumbach 		/*
4960ad12b231SNathan Errera 		 * In the new flow FW is in charge of timing the switch so there
4961ad12b231SNathan Errera 		 * is no need for all of this
4962ad12b231SNathan Errera 		 */
4963ad12b231SNathan Errera 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
4964ad12b231SNathan Errera 					    CHANNEL_SWITCH_ERROR_NOTIF,
4965ad12b231SNathan Errera 					    0))
4966ad12b231SNathan Errera 			break;
4967ad12b231SNathan Errera 
4968ad12b231SNathan Errera 		/*
49692360acbdSEmmanuel Grumbach 		 * We haven't configured the firmware to be associated yet since
49702360acbdSEmmanuel Grumbach 		 * we don't know the dtim period. In this case, the firmware can't
49712360acbdSEmmanuel Grumbach 		 * track the beacons.
49722360acbdSEmmanuel Grumbach 		 */
4973f276e20bSJohannes Berg 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period) {
49742360acbdSEmmanuel Grumbach 			ret = -EBUSY;
49752360acbdSEmmanuel Grumbach 			goto out_unlock;
49762360acbdSEmmanuel Grumbach 		}
49772360acbdSEmmanuel Grumbach 
497887d9564eSJohannes Berg 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX)
497987d9564eSJohannes Berg 			schedule_delayed_work(&mvmvif->csa_work, 0);
498087d9564eSJohannes Berg 
4981f6780614SSara Sharon 		if (chsw->block_tx) {
4982f6780614SSara Sharon 			/*
4983f6780614SSara Sharon 			 * In case of undetermined / long time with immediate
4984f6780614SSara Sharon 			 * quiet monitor status to gracefully disconnect
4985f6780614SSara Sharon 			 */
4986f6780614SSara Sharon 			if (!chsw->count ||
4987f6780614SSara Sharon 			    chsw->count * vif->bss_conf.beacon_int >
4988f6780614SSara Sharon 			    IWL_MAX_CSA_BLOCK_TX)
4989f6780614SSara Sharon 				schedule_delayed_work(&mvmvif->csa_work,
4990f6780614SSara Sharon 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
4991f6780614SSara Sharon 		}
4992e705c121SKalle Valo 
49930202bcf0SEmmanuel Grumbach 		if (!fw_has_capa(&mvm->fw->ucode_capa,
49940202bcf0SEmmanuel Grumbach 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
49950202bcf0SEmmanuel Grumbach 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
4996e705c121SKalle Valo 			if (ret)
4997e705c121SKalle Valo 				goto out_unlock;
49980202bcf0SEmmanuel Grumbach 		} else {
499974a10252SSara Sharon 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
50000202bcf0SEmmanuel Grumbach 		}
500181b4e44eSSara Sharon 
500281b4e44eSSara Sharon 		mvmvif->csa_count = chsw->count;
500381b4e44eSSara Sharon 		mvmvif->csa_misbehave = false;
5004e705c121SKalle Valo 		break;
5005e705c121SKalle Valo 	default:
5006e705c121SKalle Valo 		break;
5007e705c121SKalle Valo 	}
5008e705c121SKalle Valo 
5009e705c121SKalle Valo 	mvmvif->ps_disabled = true;
5010e705c121SKalle Valo 
5011e705c121SKalle Valo 	ret = iwl_mvm_power_update_ps(mvm);
5012e705c121SKalle Valo 	if (ret)
5013e705c121SKalle Valo 		goto out_unlock;
5014e705c121SKalle Valo 
5015e705c121SKalle Valo 	/* we won't be on this channel any longer */
5016e705c121SKalle Valo 	iwl_mvm_teardown_tdls_peers(mvm);
5017e705c121SKalle Valo 
5018e705c121SKalle Valo out_unlock:
5019e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5020e705c121SKalle Valo 
5021e705c121SKalle Valo 	return ret;
5022e705c121SKalle Valo }
5023e705c121SKalle Valo 
5024c37763d2SSara Sharon static void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5025c37763d2SSara Sharon 					     struct ieee80211_vif *vif,
5026c37763d2SSara Sharon 					     struct ieee80211_channel_switch *chsw)
5027e705c121SKalle Valo {
5028e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5029c37763d2SSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5030c37763d2SSara Sharon 	struct iwl_chan_switch_te_cmd cmd = {
5031c37763d2SSara Sharon 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5032c37763d2SSara Sharon 							  mvmvif->color)),
5033c37763d2SSara Sharon 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5034c37763d2SSara Sharon 		.tsf = cpu_to_le32(chsw->timestamp),
5035c37763d2SSara Sharon 		.cs_count = chsw->count,
503677738865SSara Sharon 		.cs_mode = chsw->block_tx,
5037c37763d2SSara Sharon 	};
5038e705c121SKalle Valo 
5039ad12b231SNathan Errera 	/*
5040ad12b231SNathan Errera 	 * In the new flow FW is in charge of timing the switch so there is no
5041ad12b231SNathan Errera 	 * need for all of this
5042ad12b231SNathan Errera 	 */
5043ad12b231SNathan Errera 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5044ad12b231SNathan Errera 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5045ad12b231SNathan Errera 		return;
5046ad12b231SNathan Errera 
5047c37763d2SSara Sharon 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5048c37763d2SSara Sharon 		return;
5049e705c121SKalle Valo 
5050c3eae059SGregory Greenman 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5051c3eae059SGregory Greenman 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5052c3eae059SGregory Greenman 
505381b4e44eSSara Sharon 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
505481b4e44eSSara Sharon 		if (mvmvif->csa_misbehave) {
505581b4e44eSSara Sharon 			/* Second time, give up on this AP*/
505681b4e44eSSara Sharon 			iwl_mvm_abort_channel_switch(hw, vif);
505781b4e44eSSara Sharon 			ieee80211_chswitch_done(vif, false);
505881b4e44eSSara Sharon 			mvmvif->csa_misbehave = false;
505981b4e44eSSara Sharon 			return;
5060e705c121SKalle Valo 		}
506181b4e44eSSara Sharon 		mvmvif->csa_misbehave = true;
5062e705c121SKalle Valo 	}
506381b4e44eSSara Sharon 	mvmvif->csa_count = chsw->count;
5064e705c121SKalle Valo 
5065caf46377SSara Sharon 	mutex_lock(&mvm->mutex);
5066caf46377SSara Sharon 	if (mvmvif->csa_failed)
5067caf46377SSara Sharon 		goto out_unlock;
5068e705c121SKalle Valo 
5069c37763d2SSara Sharon 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5070c37763d2SSara Sharon 				     WIDE_ID(MAC_CONF_GROUP,
5071c37763d2SSara Sharon 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5072caf46377SSara Sharon 				     0, sizeof(cmd), &cmd));
5073caf46377SSara Sharon out_unlock:
5074caf46377SSara Sharon 	mutex_unlock(&mvm->mutex);
5075e705c121SKalle Valo }
5076e705c121SKalle Valo 
50776110d9e5SDavid Spinadel static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
50786110d9e5SDavid Spinadel {
5079435d0827SSara Sharon 	int i;
5080435d0827SSara Sharon 
508106195639SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5082309c4848SLuca Coelho 		if (drop) {
5083309c4848SLuca Coelho 			mutex_lock(&mvm->mutex);
50846110d9e5SDavid Spinadel 			iwl_mvm_flush_tx_path(mvm,
5085d4e3a341SMordechay Goodstein 				iwl_mvm_flushable_queues(mvm) & queues);
5086309c4848SLuca Coelho 			mutex_unlock(&mvm->mutex);
5087309c4848SLuca Coelho 		} else {
5088435d0827SSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5089309c4848SLuca Coelho 		}
5090435d0827SSara Sharon 		return;
5091435d0827SSara Sharon 	}
50926110d9e5SDavid Spinadel 
50936110d9e5SDavid Spinadel 	mutex_lock(&mvm->mutex);
5094be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5095435d0827SSara Sharon 		struct ieee80211_sta *sta;
5096435d0827SSara Sharon 
5097435d0827SSara Sharon 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
50986110d9e5SDavid Spinadel 						lockdep_is_held(&mvm->mutex));
50996110d9e5SDavid Spinadel 		if (IS_ERR_OR_NULL(sta))
51006110d9e5SDavid Spinadel 			continue;
51016110d9e5SDavid Spinadel 
510206195639SSara Sharon 		if (drop)
5103d4e3a341SMordechay Goodstein 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
510406195639SSara Sharon 		else
51056110d9e5SDavid Spinadel 			iwl_mvm_wait_sta_queues_empty(mvm,
51066110d9e5SDavid Spinadel 					iwl_mvm_sta_from_mac80211(sta));
51076110d9e5SDavid Spinadel 	}
51086110d9e5SDavid Spinadel 	mutex_unlock(&mvm->mutex);
51096110d9e5SDavid Spinadel }
51106110d9e5SDavid Spinadel 
5111e705c121SKalle Valo static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
5112e705c121SKalle Valo 			      struct ieee80211_vif *vif, u32 queues, bool drop)
5113e705c121SKalle Valo {
5114e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5115e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif;
5116e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
5117e705c121SKalle Valo 	struct ieee80211_sta *sta;
5118e705c121SKalle Valo 	int i;
5119e705c121SKalle Valo 	u32 msk = 0;
5120e705c121SKalle Valo 
51216110d9e5SDavid Spinadel 	if (!vif) {
51226110d9e5SDavid Spinadel 		iwl_mvm_flush_no_vif(mvm, queues, drop);
51236110d9e5SDavid Spinadel 		return;
51246110d9e5SDavid Spinadel 	}
51256110d9e5SDavid Spinadel 
51266110d9e5SDavid Spinadel 	if (vif->type != NL80211_IFTYPE_STATION)
5127e705c121SKalle Valo 		return;
5128e705c121SKalle Valo 
512924afba76SLiad Kaufman 	/* Make sure we're done with the deferred traffic before flushing */
513024afba76SLiad Kaufman 	flush_work(&mvm->add_stream_wk);
513124afba76SLiad Kaufman 
5132e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5133e705c121SKalle Valo 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
5134e705c121SKalle Valo 
5135e705c121SKalle Valo 	/* flush the AP-station and all TDLS peers */
5136be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5137e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5138e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
5139e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
5140e705c121SKalle Valo 			continue;
5141e705c121SKalle Valo 
5142e705c121SKalle Valo 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5143e705c121SKalle Valo 		if (mvmsta->vif != vif)
5144e705c121SKalle Valo 			continue;
5145e705c121SKalle Valo 
5146e705c121SKalle Valo 		/* make sure only TDLS peers or the AP are flushed */
5147e705c121SKalle Valo 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
5148e705c121SKalle Valo 
5149d49394a1SSara Sharon 		if (drop) {
5150f9084775SNathan Errera 			if (iwl_mvm_flush_sta(mvm, mvmsta, false))
5151d49394a1SSara Sharon 				IWL_ERR(mvm, "flush request fail\n");
5152d49394a1SSara Sharon 		} else {
5153d6d517b7SSara Sharon 			if (iwl_mvm_has_new_tx_api(mvm))
5154d6d517b7SSara Sharon 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5155f7bd883bSJohannes Berg 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5156f7bd883bSJohannes Berg 				msk |= mvmsta->tfd_queue_msk;
5157e705c121SKalle Valo 		}
5158d49394a1SSara Sharon 	}
5159e705c121SKalle Valo 
5160e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5161e705c121SKalle Valo 
5162e705c121SKalle Valo 	/* this can take a while, and we may need/want other operations
5163e705c121SKalle Valo 	 * to succeed while doing this, so do it without the mutex held
5164e705c121SKalle Valo 	 */
5165d6d517b7SSara Sharon 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5166a1a57877SSara Sharon 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5167e705c121SKalle Valo }
5168e705c121SKalle Valo 
5169e705c121SKalle Valo static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5170e705c121SKalle Valo 				  struct survey_info *survey)
5171e705c121SKalle Valo {
5172e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5173e705c121SKalle Valo 	int ret;
5174e705c121SKalle Valo 
5175e705c121SKalle Valo 	memset(survey, 0, sizeof(*survey));
5176e705c121SKalle Valo 
5177e705c121SKalle Valo 	/* only support global statistics right now */
5178e705c121SKalle Valo 	if (idx != 0)
5179e705c121SKalle Valo 		return -ENOENT;
5180e705c121SKalle Valo 
5181280a3efaSJohannes Berg 	if (!fw_has_capa(&mvm->fw->ucode_capa,
5182e705c121SKalle Valo 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
5183e705c121SKalle Valo 		return -ENOENT;
5184e705c121SKalle Valo 
5185e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5186e705c121SKalle Valo 
5187aab6930dSJohannes Berg 	if (iwl_mvm_firmware_running(mvm)) {
5188e705c121SKalle Valo 		ret = iwl_mvm_request_statistics(mvm, false);
5189e705c121SKalle Valo 		if (ret)
5190e705c121SKalle Valo 			goto out;
5191e705c121SKalle Valo 	}
5192e705c121SKalle Valo 
5193e705c121SKalle Valo 	survey->filled = SURVEY_INFO_TIME |
5194e705c121SKalle Valo 			 SURVEY_INFO_TIME_RX |
5195e705c121SKalle Valo 			 SURVEY_INFO_TIME_TX |
5196e705c121SKalle Valo 			 SURVEY_INFO_TIME_SCAN;
5197e705c121SKalle Valo 	survey->time = mvm->accu_radio_stats.on_time_rf +
5198e705c121SKalle Valo 		       mvm->radio_stats.on_time_rf;
5199e705c121SKalle Valo 	do_div(survey->time, USEC_PER_MSEC);
5200e705c121SKalle Valo 
5201e705c121SKalle Valo 	survey->time_rx = mvm->accu_radio_stats.rx_time +
5202e705c121SKalle Valo 			  mvm->radio_stats.rx_time;
5203e705c121SKalle Valo 	do_div(survey->time_rx, USEC_PER_MSEC);
5204e705c121SKalle Valo 
5205e705c121SKalle Valo 	survey->time_tx = mvm->accu_radio_stats.tx_time +
5206e705c121SKalle Valo 			  mvm->radio_stats.tx_time;
5207e705c121SKalle Valo 	do_div(survey->time_tx, USEC_PER_MSEC);
5208e705c121SKalle Valo 
5209e705c121SKalle Valo 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
5210e705c121SKalle Valo 			    mvm->radio_stats.on_time_scan;
5211e705c121SKalle Valo 	do_div(survey->time_scan, USEC_PER_MSEC);
5212e705c121SKalle Valo 
5213e705c121SKalle Valo 	ret = 0;
5214e705c121SKalle Valo  out:
5215e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5216e705c121SKalle Valo 	return ret;
5217e705c121SKalle Valo }
5218e705c121SKalle Valo 
5219ed780545SJohannes Berg static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
5220ed780545SJohannes Berg {
522182cdbd11SMiri Korenblit 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
52227138763eSJohannes Berg 	u32 gi_ltf;
522382cdbd11SMiri Korenblit 
522482cdbd11SMiri Korenblit 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
5225ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_20:
5226ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
5227ed780545SJohannes Berg 		break;
5228ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_40:
5229ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
5230ed780545SJohannes Berg 		break;
5231ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_80:
5232ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
5233ed780545SJohannes Berg 		break;
5234ed780545SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_160:
5235ed780545SJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
5236ed780545SJohannes Berg 		break;
523723dcee94SJohannes Berg 	case RATE_MCS_CHAN_WIDTH_320:
523823dcee94SJohannes Berg 		rinfo->bw = RATE_INFO_BW_320;
523923dcee94SJohannes Berg 		break;
5240ed780545SJohannes Berg 	}
5241ed780545SJohannes Berg 
524282cdbd11SMiri Korenblit 	if (format == RATE_MCS_CCK_MSK ||
524382cdbd11SMiri Korenblit 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
524482cdbd11SMiri Korenblit 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
524582cdbd11SMiri Korenblit 
524682cdbd11SMiri Korenblit 		/* add the offset needed to get to the legacy ofdm indices */
524782cdbd11SMiri Korenblit 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
524882cdbd11SMiri Korenblit 			rate += IWL_FIRST_OFDM_RATE;
524982cdbd11SMiri Korenblit 
525082cdbd11SMiri Korenblit 		switch (rate) {
525182cdbd11SMiri Korenblit 		case IWL_RATE_1M_INDEX:
525282cdbd11SMiri Korenblit 			rinfo->legacy = 10;
525382cdbd11SMiri Korenblit 			break;
525482cdbd11SMiri Korenblit 		case IWL_RATE_2M_INDEX:
525582cdbd11SMiri Korenblit 			rinfo->legacy = 20;
525682cdbd11SMiri Korenblit 			break;
525782cdbd11SMiri Korenblit 		case IWL_RATE_5M_INDEX:
525882cdbd11SMiri Korenblit 			rinfo->legacy = 55;
525982cdbd11SMiri Korenblit 			break;
526082cdbd11SMiri Korenblit 		case IWL_RATE_11M_INDEX:
526182cdbd11SMiri Korenblit 			rinfo->legacy = 110;
526282cdbd11SMiri Korenblit 			break;
526382cdbd11SMiri Korenblit 		case IWL_RATE_6M_INDEX:
526482cdbd11SMiri Korenblit 			rinfo->legacy = 60;
526582cdbd11SMiri Korenblit 			break;
526682cdbd11SMiri Korenblit 		case IWL_RATE_9M_INDEX:
526782cdbd11SMiri Korenblit 			rinfo->legacy = 90;
526882cdbd11SMiri Korenblit 			break;
526982cdbd11SMiri Korenblit 		case IWL_RATE_12M_INDEX:
527082cdbd11SMiri Korenblit 			rinfo->legacy = 120;
527182cdbd11SMiri Korenblit 			break;
527282cdbd11SMiri Korenblit 		case IWL_RATE_18M_INDEX:
527382cdbd11SMiri Korenblit 			rinfo->legacy = 180;
527482cdbd11SMiri Korenblit 			break;
527582cdbd11SMiri Korenblit 		case IWL_RATE_24M_INDEX:
527682cdbd11SMiri Korenblit 			rinfo->legacy = 240;
527782cdbd11SMiri Korenblit 			break;
527882cdbd11SMiri Korenblit 		case IWL_RATE_36M_INDEX:
527982cdbd11SMiri Korenblit 			rinfo->legacy = 360;
528082cdbd11SMiri Korenblit 			break;
528182cdbd11SMiri Korenblit 		case IWL_RATE_48M_INDEX:
528282cdbd11SMiri Korenblit 			rinfo->legacy = 480;
528382cdbd11SMiri Korenblit 			break;
528482cdbd11SMiri Korenblit 		case IWL_RATE_54M_INDEX:
528582cdbd11SMiri Korenblit 			rinfo->legacy = 540;
528682cdbd11SMiri Korenblit 		}
528782cdbd11SMiri Korenblit 		return;
528882cdbd11SMiri Korenblit 	}
528982cdbd11SMiri Korenblit 
5290ed780545SJohannes Berg 	rinfo->nss = u32_get_bits(rate_n_flags,
529182cdbd11SMiri Korenblit 				  RATE_MCS_NSS_MSK) + 1;
529282cdbd11SMiri Korenblit 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
529382cdbd11SMiri Korenblit 		RATE_HT_MCS_INDEX(rate_n_flags) :
529482cdbd11SMiri Korenblit 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
529582cdbd11SMiri Korenblit 
52967138763eSJohannes Berg 	if (rate_n_flags & RATE_MCS_SGI_MSK)
52977138763eSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
52987138763eSJohannes Berg 
52997138763eSJohannes Berg 	switch (format) {
530023dcee94SJohannes Berg 	case RATE_MCS_EHT_MSK:
530123dcee94SJohannes Berg 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
530223dcee94SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
530323dcee94SJohannes Berg 		break;
53047138763eSJohannes Berg 	case RATE_MCS_HE_MSK:
53057138763eSJohannes Berg 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
5306ed780545SJohannes Berg 
5307ed780545SJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
5308ed780545SJohannes Berg 
530982cdbd11SMiri Korenblit 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
5310ed780545SJohannes Berg 			rinfo->bw = RATE_INFO_BW_HE_RU;
5311ed780545SJohannes Berg 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
5312ed780545SJohannes Berg 		}
5313ed780545SJohannes Berg 
531482cdbd11SMiri Korenblit 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
531582cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_SU:
531682cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_EXT_SU:
5317ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5318ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5319ed780545SJohannes Berg 			else if (gi_ltf == 2)
5320ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
532182cdbd11SMiri Korenblit 			else if (gi_ltf == 3)
5322ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
532382cdbd11SMiri Korenblit 			else
532482cdbd11SMiri Korenblit 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5325ed780545SJohannes Berg 			break;
532682cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_MU:
5327ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5328ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5329ed780545SJohannes Berg 			else if (gi_ltf == 2)
5330ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5331ed780545SJohannes Berg 			else
5332ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5333ed780545SJohannes Berg 			break;
533482cdbd11SMiri Korenblit 		case RATE_MCS_HE_TYPE_TRIG:
5335ed780545SJohannes Berg 			if (gi_ltf == 0 || gi_ltf == 1)
5336ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5337ed780545SJohannes Berg 			else
5338ed780545SJohannes Berg 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5339ed780545SJohannes Berg 			break;
5340ed780545SJohannes Berg 		}
5341ed780545SJohannes Berg 
5342ed780545SJohannes Berg 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
5343ed780545SJohannes Berg 			rinfo->he_dcm = 1;
53447138763eSJohannes Berg 		break;
53457138763eSJohannes Berg 	case RATE_MCS_HT_MSK:
534682cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
53477138763eSJohannes Berg 		break;
53487138763eSJohannes Berg 	case RATE_MCS_VHT_MSK:
534982cdbd11SMiri Korenblit 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
53507138763eSJohannes Berg 		break;
5351ed780545SJohannes Berg 	}
5352ed780545SJohannes Berg }
5353ed780545SJohannes Berg 
5354e705c121SKalle Valo static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
5355e705c121SKalle Valo 				       struct ieee80211_vif *vif,
5356e705c121SKalle Valo 				       struct ieee80211_sta *sta,
5357e705c121SKalle Valo 				       struct station_info *sinfo)
5358e705c121SKalle Valo {
5359e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5360e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5361e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5362e705c121SKalle Valo 
5363988b5968SSara Sharon 	if (mvmsta->avg_energy) {
536488ad368aSBen Greear 		sinfo->signal_avg = -(s8)mvmsta->avg_energy;
536522d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
5366988b5968SSara Sharon 	}
5367988b5968SSara Sharon 
5368ed780545SJohannes Berg 	if (iwl_mvm_has_tlc_offload(mvm)) {
5369ed780545SJohannes Berg 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
5370ed780545SJohannes Berg 
5371ed780545SJohannes Berg 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5372ed780545SJohannes Berg 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5373ed780545SJohannes Berg 	}
5374ed780545SJohannes Berg 
5375e705c121SKalle Valo 	/* if beacon filtering isn't on mac80211 does it anyway */
5376e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5377e705c121SKalle Valo 		return;
5378e705c121SKalle Valo 
5379f276e20bSJohannes Berg 	if (!vif->cfg.assoc)
5380e705c121SKalle Valo 		return;
5381e705c121SKalle Valo 
5382e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
5383e705c121SKalle Valo 
5384e705c121SKalle Valo 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
5385e705c121SKalle Valo 		goto unlock;
5386e705c121SKalle Valo 
5387e705c121SKalle Valo 	if (iwl_mvm_request_statistics(mvm, false))
5388e705c121SKalle Valo 		goto unlock;
5389e705c121SKalle Valo 
5390e705c121SKalle Valo 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
5391e705c121SKalle Valo 			   mvmvif->beacon_stats.accu_num_beacons;
539222d0d2faSOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
5393e705c121SKalle Valo 	if (mvmvif->beacon_stats.avg_signal) {
5394e705c121SKalle Valo 		/* firmware only reports a value after RXing a few beacons */
5395e705c121SKalle Valo 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
539622d0d2faSOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
5397e705c121SKalle Valo 	}
5398e705c121SKalle Valo  unlock:
5399e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
5400e705c121SKalle Valo }
5401e705c121SKalle Valo 
5402119c2a13SMordechay Goodstein static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5403119c2a13SMordechay Goodstein 					    struct ieee80211_vif *vif,
5404119c2a13SMordechay Goodstein 					    const  struct ieee80211_mlme_event *mlme)
5405119c2a13SMordechay Goodstein {
54061a81bddfSMordechay Goodstein 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
54071a81bddfSMordechay Goodstein 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
5408119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5409119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5410119c2a13SMordechay Goodstein 				       NULL);
5411119c2a13SMordechay Goodstein 		return;
5412119c2a13SMordechay Goodstein 	}
5413119c2a13SMordechay Goodstein 
5414119c2a13SMordechay Goodstein 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5415119c2a13SMordechay Goodstein 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5416119c2a13SMordechay Goodstein 				       IWL_FW_INI_TIME_POINT_DEASSOC,
5417119c2a13SMordechay Goodstein 				       NULL);
5418119c2a13SMordechay Goodstein 		return;
5419119c2a13SMordechay Goodstein 	}
5420119c2a13SMordechay Goodstein }
5421119c2a13SMordechay Goodstein 
5422e705c121SKalle Valo static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5423e705c121SKalle Valo 					struct ieee80211_vif *vif,
5424e705c121SKalle Valo 					const struct ieee80211_event *event)
5425e705c121SKalle Valo {
54264c324a51SLuca Coelho #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5427e705c121SKalle Valo 	do {								\
54284c324a51SLuca Coelho 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5429e705c121SKalle Valo 			break;						\
54307174beb6SJohannes Berg 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5431e705c121SKalle Valo 	} while (0)
5432e705c121SKalle Valo 
5433e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5434e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5435e705c121SKalle Valo 
5436119c2a13SMordechay Goodstein 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5437119c2a13SMordechay Goodstein 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5438119c2a13SMordechay Goodstein 		return;
5439119c2a13SMordechay Goodstein 	}
5440119c2a13SMordechay Goodstein 
54416c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
54426c042d75SSara Sharon 				     FW_DBG_TRIGGER_MLME);
54436c042d75SSara Sharon 	if (!trig)
5444e705c121SKalle Valo 		return;
5445e705c121SKalle Valo 
5446e705c121SKalle Valo 	trig_mlme = (void *)trig->data;
5447e705c121SKalle Valo 
5448e705c121SKalle Valo 	if (event->u.mlme.data == ASSOC_EVENT) {
5449e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
54504c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_denied,
5451e705c121SKalle Valo 					   "DENIED ASSOC: reason %d",
5452e705c121SKalle Valo 					    event->u.mlme.reason);
5453e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
54544c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
5455e705c121SKalle Valo 					   "ASSOC TIMEOUT");
5456e705c121SKalle Valo 	} else if (event->u.mlme.data == AUTH_EVENT) {
5457e705c121SKalle Valo 		if (event->u.mlme.status == MLME_DENIED)
54584c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_denied,
5459e705c121SKalle Valo 					   "DENIED AUTH: reason %d",
5460e705c121SKalle Valo 					   event->u.mlme.reason);
5461e705c121SKalle Valo 		else if (event->u.mlme.status == MLME_TIMEOUT)
54624c324a51SLuca Coelho 			CHECK_MLME_TRIGGER(stop_auth_timeout,
5463e705c121SKalle Valo 					   "AUTH TIMEOUT");
5464e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
54654c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_rx_deauth,
5466e705c121SKalle Valo 				   "DEAUTH RX %d", event->u.mlme.reason);
5467e705c121SKalle Valo 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
54684c324a51SLuca Coelho 		CHECK_MLME_TRIGGER(stop_tx_deauth,
5469e705c121SKalle Valo 				   "DEAUTH TX %d", event->u.mlme.reason);
5470e705c121SKalle Valo 	}
5471e705c121SKalle Valo #undef CHECK_MLME_TRIGGER
5472e705c121SKalle Valo }
5473e705c121SKalle Valo 
5474e705c121SKalle Valo static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5475e705c121SKalle Valo 					  struct ieee80211_vif *vif,
5476e705c121SKalle Valo 					  const struct ieee80211_event *event)
5477e705c121SKalle Valo {
5478e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
5479e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_ba *ba_trig;
5480e705c121SKalle Valo 
54816c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
54826c042d75SSara Sharon 				     FW_DBG_TRIGGER_BA);
54836c042d75SSara Sharon 	if (!trig)
5484e705c121SKalle Valo 		return;
5485e705c121SKalle Valo 
5486e705c121SKalle Valo 	ba_trig = (void *)trig->data;
5487e705c121SKalle Valo 
5488e705c121SKalle Valo 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5489e705c121SKalle Valo 		return;
5490e705c121SKalle Valo 
54917174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
5492e705c121SKalle Valo 				"BAR received from %pM, tid %d, ssn %d",
5493e705c121SKalle Valo 				event->u.ba.sta->addr, event->u.ba.tid,
5494e705c121SKalle Valo 				event->u.ba.ssn);
5495e705c121SKalle Valo }
5496e705c121SKalle Valo 
5497e705c121SKalle Valo static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
5498e705c121SKalle Valo 				       struct ieee80211_vif *vif,
5499e705c121SKalle Valo 				       const struct ieee80211_event *event)
5500e705c121SKalle Valo {
5501e705c121SKalle Valo 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5502e705c121SKalle Valo 
5503e705c121SKalle Valo 	switch (event->type) {
5504e705c121SKalle Valo 	case MLME_EVENT:
5505e705c121SKalle Valo 		iwl_mvm_event_mlme_callback(mvm, vif, event);
5506e705c121SKalle Valo 		break;
5507e705c121SKalle Valo 	case BAR_RX_EVENT:
5508e705c121SKalle Valo 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
5509e705c121SKalle Valo 		break;
5510e705c121SKalle Valo 	case BA_FRAME_TIMEOUT:
5511528a542aSEmmanuel Grumbach 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
5512528a542aSEmmanuel Grumbach 						     event->u.ba.tid);
5513e705c121SKalle Valo 		break;
5514e705c121SKalle Valo 	default:
5515e705c121SKalle Valo 		break;
5516e705c121SKalle Valo 	}
5517e705c121SKalle Valo }
5518e705c121SKalle Valo 
5519d0ff5d22SSara Sharon void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
55205e1688ceSJohannes Berg 				     enum iwl_mvm_rxq_notif_type type,
55215e1688ceSJohannes Berg 				     bool sync,
55225e1688ceSJohannes Berg 				     const void *data, u32 size)
55230636b938SSara Sharon {
55245e1688ceSJohannes Berg 	struct {
55255e1688ceSJohannes Berg 		struct iwl_rxq_sync_cmd cmd;
55265e1688ceSJohannes Berg 		struct iwl_mvm_internal_rxq_notif notif;
55275e1688ceSJohannes Berg 	} __packed cmd = {
55285e1688ceSJohannes Berg 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
55295e1688ceSJohannes Berg 		.cmd.count =
55305e1688ceSJohannes Berg 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
55315e1688ceSJohannes Berg 				    size),
55325e1688ceSJohannes Berg 		.notif.type = type,
55335e1688ceSJohannes Berg 		.notif.sync = sync,
55345e1688ceSJohannes Berg 	};
55355e1688ceSJohannes Berg 	struct iwl_host_cmd hcmd = {
55365e1688ceSJohannes Berg 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
55375e1688ceSJohannes Berg 		.data[0] = &cmd,
55385e1688ceSJohannes Berg 		.len[0] = sizeof(cmd),
55395e1688ceSJohannes Berg 		.data[1] = data,
55405e1688ceSJohannes Berg 		.len[1] = size,
55415e1688ceSJohannes Berg 		.flags = sync ? 0 : CMD_ASYNC,
55425e1688ceSJohannes Berg 	};
55430636b938SSara Sharon 	int ret;
55440636b938SSara Sharon 
55455e1688ceSJohannes Berg 	/* size must be a multiple of DWORD */
55465e1688ceSJohannes Berg 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
55475e1688ceSJohannes Berg 		return;
55480636b938SSara Sharon 
5549cb8550e1SSara Sharon 	if (!iwl_mvm_has_new_rx_api(mvm))
55500636b938SSara Sharon 		return;
55510636b938SSara Sharon 
55525e1688ceSJohannes Berg 	if (sync) {
55535e1688ceSJohannes Berg 		cmd.notif.cookie = mvm->queue_sync_cookie;
55542f7a04c7SJohannes Berg 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
5555a2113cc4SNaftali Goldstein 	}
5556d0ff5d22SSara Sharon 
55575e1688ceSJohannes Berg 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
55580636b938SSara Sharon 	if (ret) {
55590636b938SSara Sharon 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
55600636b938SSara Sharon 		goto out;
55610636b938SSara Sharon 	}
5562d0ff5d22SSara Sharon 
55635e1688ceSJohannes Berg 	if (sync) {
55643c514bf8SEmmanuel Grumbach 		lockdep_assert_held(&mvm->mutex);
55653a732c65SSara Sharon 		ret = wait_event_timeout(mvm->rx_sync_waitq,
55662f7a04c7SJohannes Berg 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
55676ad04359SJohannes Berg 					 iwl_mvm_is_radio_killed(mvm),
55680636b938SSara Sharon 					 HZ);
55692f7a04c7SJohannes Berg 		WARN_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm),
55702f7a04c7SJohannes Berg 			  "queue sync: failed to sync, state is 0x%lx\n",
55712f7a04c7SJohannes Berg 			  mvm->queue_sync_state);
55726ad04359SJohannes Berg 	}
55730636b938SSara Sharon 
55740636b938SSara Sharon out:
55755e1688ceSJohannes Berg 	if (sync) {
55762f7a04c7SJohannes Berg 		mvm->queue_sync_state = 0;
55770636b938SSara Sharon 		mvm->queue_sync_cookie++;
55780636b938SSara Sharon 	}
55795f8a3561SJohannes Berg }
55800636b938SSara Sharon 
55810636b938SSara Sharon static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
55820636b938SSara Sharon {
55830636b938SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
55840636b938SSara Sharon 
55850636b938SSara Sharon 	mutex_lock(&mvm->mutex);
55865e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
55870636b938SSara Sharon 	mutex_unlock(&mvm->mutex);
55880636b938SSara Sharon }
55890636b938SSara Sharon 
5590b73f9a4aSJohannes Berg static int
5591b73f9a4aSJohannes Berg iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
5592b73f9a4aSJohannes Berg 				    struct ieee80211_vif *vif,
5593b73f9a4aSJohannes Berg 				    struct cfg80211_ftm_responder_stats *stats)
5594b73f9a4aSJohannes Berg {
5595b73f9a4aSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5596b73f9a4aSJohannes Berg 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5597b73f9a4aSJohannes Berg 
5598b73f9a4aSJohannes Berg 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
5599b73f9a4aSJohannes Berg 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
5600b73f9a4aSJohannes Berg 		return -EINVAL;
5601b73f9a4aSJohannes Berg 
5602b73f9a4aSJohannes Berg 	mutex_lock(&mvm->mutex);
5603b73f9a4aSJohannes Berg 	*stats = mvm->ftm_resp_stats;
5604b73f9a4aSJohannes Berg 	mutex_unlock(&mvm->mutex);
5605b73f9a4aSJohannes Berg 
5606b73f9a4aSJohannes Berg 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
5607b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
5608b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
5609b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
5610b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
5611b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
5612b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
5613b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
5614b73f9a4aSJohannes Berg 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
5615b73f9a4aSJohannes Berg 
5616b73f9a4aSJohannes Berg 	return 0;
5617b73f9a4aSJohannes Berg }
5618b73f9a4aSJohannes Berg 
5619fc36ffdaSJohannes Berg static int iwl_mvm_start_pmsr(struct ieee80211_hw *hw,
5620fc36ffdaSJohannes Berg 			      struct ieee80211_vif *vif,
5621fc36ffdaSJohannes Berg 			      struct cfg80211_pmsr_request *request)
5622fc36ffdaSJohannes Berg {
5623fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5624fc36ffdaSJohannes Berg 	int ret;
5625fc36ffdaSJohannes Berg 
5626fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5627fc36ffdaSJohannes Berg 	ret = iwl_mvm_ftm_start(mvm, vif, request);
5628fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5629fc36ffdaSJohannes Berg 
5630fc36ffdaSJohannes Berg 	return ret;
5631fc36ffdaSJohannes Berg }
5632fc36ffdaSJohannes Berg 
5633fc36ffdaSJohannes Berg static void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw,
5634fc36ffdaSJohannes Berg 			       struct ieee80211_vif *vif,
5635fc36ffdaSJohannes Berg 			       struct cfg80211_pmsr_request *request)
5636fc36ffdaSJohannes Berg {
5637fc36ffdaSJohannes Berg 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5638fc36ffdaSJohannes Berg 
5639fc36ffdaSJohannes Berg 	mutex_lock(&mvm->mutex);
5640fc36ffdaSJohannes Berg 	iwl_mvm_ftm_abort(mvm, request);
5641fc36ffdaSJohannes Berg 	mutex_unlock(&mvm->mutex);
5642fc36ffdaSJohannes Berg }
5643fc36ffdaSJohannes Berg 
5644438af969SSara Sharon static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
5645438af969SSara Sharon {
5646438af969SSara Sharon 	u8 protocol = ip_hdr(skb)->protocol;
5647438af969SSara Sharon 
5648438af969SSara Sharon 	if (!IS_ENABLED(CONFIG_INET))
5649438af969SSara Sharon 		return false;
5650438af969SSara Sharon 
5651438af969SSara Sharon 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
5652438af969SSara Sharon }
5653438af969SSara Sharon 
5654438af969SSara Sharon static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
5655438af969SSara Sharon 				      struct sk_buff *head,
5656438af969SSara Sharon 				      struct sk_buff *skb)
5657438af969SSara Sharon {
5658438af969SSara Sharon 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5659438af969SSara Sharon 
56600792df68SJohannes Berg 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
56610792df68SJohannes Berg 		return iwl_mvm_tx_csum_bz(mvm, head, true) ==
56620792df68SJohannes Berg 		       iwl_mvm_tx_csum_bz(mvm, skb, true);
56630792df68SJohannes Berg 
5664438af969SSara Sharon 	/* For now don't aggregate IPv6 in AMSDU */
5665438af969SSara Sharon 	if (skb->protocol != htons(ETH_P_IP))
5666438af969SSara Sharon 		return false;
5667438af969SSara Sharon 
5668438af969SSara Sharon 	if (!iwl_mvm_is_csum_supported(mvm))
5669438af969SSara Sharon 		return true;
5670438af969SSara Sharon 
5671438af969SSara Sharon 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5672438af969SSara Sharon }
5673438af969SSara Sharon 
5674e705c121SKalle Valo const struct ieee80211_ops iwl_mvm_hw_ops = {
5675e705c121SKalle Valo 	.tx = iwl_mvm_mac_tx,
5676cfbc6c4cSSara Sharon 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
5677e705c121SKalle Valo 	.ampdu_action = iwl_mvm_mac_ampdu_action,
5678e8503aecSBen Greear 	.get_antenna = iwl_mvm_op_get_antenna,
5679e705c121SKalle Valo 	.start = iwl_mvm_mac_start,
5680e705c121SKalle Valo 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
5681e705c121SKalle Valo 	.stop = iwl_mvm_mac_stop,
5682e705c121SKalle Valo 	.add_interface = iwl_mvm_mac_add_interface,
5683e705c121SKalle Valo 	.remove_interface = iwl_mvm_mac_remove_interface,
5684e705c121SKalle Valo 	.config = iwl_mvm_mac_config,
5685e705c121SKalle Valo 	.prepare_multicast = iwl_mvm_prepare_multicast,
5686e705c121SKalle Valo 	.configure_filter = iwl_mvm_configure_filter,
5687e705c121SKalle Valo 	.config_iface_filter = iwl_mvm_config_iface_filter,
5688e705c121SKalle Valo 	.bss_info_changed = iwl_mvm_bss_info_changed,
5689e705c121SKalle Valo 	.hw_scan = iwl_mvm_mac_hw_scan,
5690e705c121SKalle Valo 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
5691e705c121SKalle Valo 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
5692e705c121SKalle Valo 	.sta_state = iwl_mvm_mac_sta_state,
5693e705c121SKalle Valo 	.sta_notify = iwl_mvm_mac_sta_notify,
5694e705c121SKalle Valo 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
5695e705c121SKalle Valo 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
5696e705c121SKalle Valo 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
5697e705c121SKalle Valo 	.sta_rc_update = iwl_mvm_sta_rc_update,
5698e705c121SKalle Valo 	.conf_tx = iwl_mvm_mac_conf_tx,
5699e705c121SKalle Valo 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
57006b1259d1SJohannes Berg 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
5701e705c121SKalle Valo 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
5702e705c121SKalle Valo 	.flush = iwl_mvm_mac_flush,
5703e705c121SKalle Valo 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
5704e705c121SKalle Valo 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
5705e705c121SKalle Valo 	.set_key = iwl_mvm_mac_set_key,
5706e705c121SKalle Valo 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
5707e705c121SKalle Valo 	.remain_on_channel = iwl_mvm_roc,
5708e705c121SKalle Valo 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
5709e705c121SKalle Valo 	.add_chanctx = iwl_mvm_add_chanctx,
5710e705c121SKalle Valo 	.remove_chanctx = iwl_mvm_remove_chanctx,
5711e705c121SKalle Valo 	.change_chanctx = iwl_mvm_change_chanctx,
5712e705c121SKalle Valo 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5713e705c121SKalle Valo 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
5714e705c121SKalle Valo 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
5715e705c121SKalle Valo 
5716ae7ba17bSShaul Triebitz 	.start_ap = iwl_mvm_start_ap,
5717ae7ba17bSShaul Triebitz 	.stop_ap = iwl_mvm_stop_ap,
5718ae7ba17bSShaul Triebitz 	.join_ibss = iwl_mvm_start_ibss,
5719ae7ba17bSShaul Triebitz 	.leave_ibss = iwl_mvm_stop_ibss,
5720e705c121SKalle Valo 
57212f0282dbSJohannes Berg 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
57222f0282dbSJohannes Berg 
5723e705c121SKalle Valo 	.set_tim = iwl_mvm_set_tim,
5724e705c121SKalle Valo 
5725e705c121SKalle Valo 	.channel_switch = iwl_mvm_channel_switch,
5726e705c121SKalle Valo 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
5727e705c121SKalle Valo 	.post_channel_switch = iwl_mvm_post_channel_switch,
572879221126SSara Sharon 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
5729c37763d2SSara Sharon 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
5730e705c121SKalle Valo 
5731e705c121SKalle Valo 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5732e705c121SKalle Valo 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5733e705c121SKalle Valo 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5734e705c121SKalle Valo 
5735e705c121SKalle Valo 	.event_callback = iwl_mvm_mac_event_callback,
5736e705c121SKalle Valo 
57370636b938SSara Sharon 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
57380636b938SSara Sharon 
5739e705c121SKalle Valo 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5740e705c121SKalle Valo 
5741e705c121SKalle Valo #ifdef CONFIG_PM_SLEEP
5742e705c121SKalle Valo 	/* look at d3.c */
5743e705c121SKalle Valo 	.suspend = iwl_mvm_suspend,
5744e705c121SKalle Valo 	.resume = iwl_mvm_resume,
5745e705c121SKalle Valo 	.set_wakeup = iwl_mvm_set_wakeup,
5746e705c121SKalle Valo 	.set_rekey_data = iwl_mvm_set_rekey_data,
5747e705c121SKalle Valo #if IS_ENABLED(CONFIG_IPV6)
5748e705c121SKalle Valo 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
5749e705c121SKalle Valo #endif
5750e705c121SKalle Valo 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
5751e705c121SKalle Valo #endif
5752e705c121SKalle Valo 	.get_survey = iwl_mvm_mac_get_survey,
5753e705c121SKalle Valo 	.sta_statistics = iwl_mvm_mac_sta_statistics,
5754b73f9a4aSJohannes Berg 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
5755fc36ffdaSJohannes Berg 	.start_pmsr = iwl_mvm_start_pmsr,
5756fc36ffdaSJohannes Berg 	.abort_pmsr = iwl_mvm_abort_pmsr,
5757b73f9a4aSJohannes Berg 
5758438af969SSara Sharon 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
5759177a11cfSGregory Greenman #ifdef CONFIG_IWLWIFI_DEBUGFS
5760177a11cfSGregory Greenman 	.sta_add_debugfs = iwl_mvm_sta_add_debugfs,
5761177a11cfSGregory Greenman #endif
5762e705c121SKalle Valo };
5763