1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2/*
3 * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
6 */
7#include <linux/kernel.h>
8#include <linux/slab.h>
9#include <linux/skbuff.h>
10#include <linux/netdevice.h>
11#include <linux/etherdevice.h>
12#include <linux/ip.h>
13#include <linux/if_arp.h>
14#include <linux/time.h>
15#include <net/mac80211.h>
16#include <net/ieee80211_radiotap.h>
17#include <net/tcp.h>
18
19#include "iwl-drv.h"
20#include "iwl-op-mode.h"
21#include "iwl-io.h"
22#include "mvm.h"
23#include "sta.h"
24#include "time-event.h"
25#include "iwl-eeprom-parse.h"
26#include "iwl-phy-db.h"
27#include "testmode.h"
28#include "fw/error-dump.h"
29#include "iwl-prph.h"
30#include "iwl-nvm-parse.h"
31#include "time-sync.h"
32
33static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
34	{
35		.max = 1,
36		.types = BIT(NL80211_IFTYPE_STATION),
37	},
38	{
39		.max = 1,
40		.types = BIT(NL80211_IFTYPE_AP) |
41			BIT(NL80211_IFTYPE_P2P_CLIENT) |
42			BIT(NL80211_IFTYPE_P2P_GO),
43	},
44	{
45		.max = 1,
46		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
47	},
48};
49
50static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
51	{
52		.num_different_channels = 2,
53		.max_interfaces = 3,
54		.limits = iwl_mvm_limits,
55		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
56	},
57};
58
59static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
60	.max_peers = IWL_MVM_TOF_MAX_APS,
61	.report_ap_tsf = 1,
62	.randomize_mac_addr = 1,
63
64	.ftm = {
65		.supported = 1,
66		.asap = 1,
67		.non_asap = 1,
68		.request_lci = 1,
69		.request_civicloc = 1,
70		.trigger_based = 1,
71		.non_trigger_based = 1,
72		.max_bursts_exponent = -1, /* all supported */
73		.max_ftms_per_burst = 0, /* no limits */
74		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
75			      BIT(NL80211_CHAN_WIDTH_20) |
76			      BIT(NL80211_CHAN_WIDTH_40) |
77			      BIT(NL80211_CHAN_WIDTH_80) |
78			      BIT(NL80211_CHAN_WIDTH_160),
79		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
80			     BIT(NL80211_PREAMBLE_HT) |
81			     BIT(NL80211_PREAMBLE_VHT) |
82			     BIT(NL80211_PREAMBLE_HE),
83	},
84};
85
86static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
87				 enum set_key_cmd cmd,
88				 struct ieee80211_vif *vif,
89				 struct ieee80211_sta *sta,
90				 struct ieee80211_key_conf *key);
91
92static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
93{
94	int i;
95
96	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
97	for (i = 0; i < NUM_PHY_CTX; i++) {
98		mvm->phy_ctxts[i].id = i;
99		mvm->phy_ctxts[i].ref = 0;
100	}
101}
102
103struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
104						  const char *alpha2,
105						  enum iwl_mcc_source src_id,
106						  bool *changed)
107{
108	struct ieee80211_regdomain *regd = NULL;
109	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
110	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
111	struct iwl_mcc_update_resp_v8 *resp;
112	u8 resp_ver;
113
114	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
115
116	lockdep_assert_held(&mvm->mutex);
117
118	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
119	if (IS_ERR_OR_NULL(resp)) {
120		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
121			      PTR_ERR_OR_ZERO(resp));
122		resp = NULL;
123		goto out;
124	}
125
126	if (changed) {
127		u32 status = le32_to_cpu(resp->status);
128
129		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
130			    status == MCC_RESP_ILLEGAL);
131	}
132	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
133					   MCC_UPDATE_CMD, 0);
134	IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
135
136	regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
137				      __le32_to_cpu(resp->n_channels),
138				      resp->channels,
139				      __le16_to_cpu(resp->mcc),
140				      __le16_to_cpu(resp->geo_info),
141				      le32_to_cpu(resp->cap), resp_ver);
142	/* Store the return source id */
143	src_id = resp->source_id;
144	if (IS_ERR_OR_NULL(regd)) {
145		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
146			      PTR_ERR_OR_ZERO(regd));
147		goto out;
148	}
149
150	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
151		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
152	mvm->lar_regdom_set = true;
153	mvm->mcc_src = src_id;
154
155	iwl_mei_set_country_code(__le16_to_cpu(resp->mcc));
156
157out:
158	kfree(resp);
159	return regd;
160}
161
162void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
163{
164	bool changed;
165	struct ieee80211_regdomain *regd;
166
167	if (!iwl_mvm_is_lar_supported(mvm))
168		return;
169
170	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
171	if (!IS_ERR_OR_NULL(regd)) {
172		/* only update the regulatory core if changed */
173		if (changed)
174			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
175
176		kfree(regd);
177	}
178}
179
180struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
181							  bool *changed)
182{
183	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
184				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
185				     MCC_SOURCE_GET_CURRENT :
186				     MCC_SOURCE_OLD_FW, changed);
187}
188
189int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
190{
191	enum iwl_mcc_source used_src;
192	struct ieee80211_regdomain *regd;
193	int ret;
194	bool changed;
195	const struct ieee80211_regdomain *r =
196			wiphy_dereference(mvm->hw->wiphy, mvm->hw->wiphy->regd);
197
198	if (!r)
199		return -ENOENT;
200
201	/* save the last source in case we overwrite it below */
202	used_src = mvm->mcc_src;
203	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
204		/* Notify the firmware we support wifi location updates */
205		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
206		if (!IS_ERR_OR_NULL(regd))
207			kfree(regd);
208	}
209
210	/* Now set our last stored MCC and source */
211	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
212				     &changed);
213	if (IS_ERR_OR_NULL(regd))
214		return -EIO;
215
216	/* update cfg80211 if the regdomain was changed */
217	if (changed)
218		ret = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
219	else
220		ret = 0;
221
222	kfree(regd);
223	return ret;
224}
225
226/* Each capability added here should also be add to tm_if_types_ext_capa_sta */
227static const u8 he_if_types_ext_capa_sta[] = {
228	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
229	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
230	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
231	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
232	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
233};
234
235static const u8 tm_if_types_ext_capa_sta[] = {
236	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
237	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT |
238	       WLAN_EXT_CAPA3_TIMING_MEASUREMENT_SUPPORT,
239	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
240	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
241	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
242	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
243};
244
245/* Additional interface types for which extended capabilities are
246 * specified separately
247 */
248
249#define IWL_MVM_EMLSR_CAPA	(IEEE80211_EML_CAP_EMLSR_SUPP | \
250				 IEEE80211_EML_CAP_EMLSR_PADDING_DELAY_32US << \
251					__bf_shf(IEEE80211_EML_CAP_EMLSR_PADDING_DELAY) | \
252				 IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_64US << \
253					__bf_shf(IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY))
254
255static const struct wiphy_iftype_ext_capab add_iftypes_ext_capa[] = {
256	{
257		.iftype = NL80211_IFTYPE_STATION,
258		.extended_capabilities = he_if_types_ext_capa_sta,
259		.extended_capabilities_mask = he_if_types_ext_capa_sta,
260		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
261		/* relevant only if EHT is supported */
262		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
263	},
264	{
265		.iftype = NL80211_IFTYPE_STATION,
266		.extended_capabilities = tm_if_types_ext_capa_sta,
267		.extended_capabilities_mask = tm_if_types_ext_capa_sta,
268		.extended_capabilities_len = sizeof(tm_if_types_ext_capa_sta),
269		/* relevant only if EHT is supported */
270		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
271	},
272};
273
274int iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
275{
276	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
277	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
278	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
279	return 0;
280}
281
282int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
283{
284	struct ieee80211_hw *hw = mvm->hw;
285	int num_mac, ret, i;
286	static const u32 mvm_ciphers[] = {
287		WLAN_CIPHER_SUITE_WEP40,
288		WLAN_CIPHER_SUITE_WEP104,
289		WLAN_CIPHER_SUITE_TKIP,
290		WLAN_CIPHER_SUITE_CCMP,
291	};
292#ifdef CONFIG_PM_SLEEP
293	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
294				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
295#endif
296	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
297	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
298
299	/* Tell mac80211 our characteristics */
300	ieee80211_hw_set(hw, SIGNAL_DBM);
301	ieee80211_hw_set(hw, SPECTRUM_MGMT);
302	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
303	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
304	ieee80211_hw_set(hw, SUPPORTS_PS);
305	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
306	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
307	ieee80211_hw_set(hw, CONNECTION_MONITOR);
308	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
309	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
310	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
311	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
312	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
313	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
314	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
315	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
316
317	/* Set this early since we need to have it for the check below */
318	if (mvm->mld_api_is_used && mvm->nvm_data->sku_cap_11be_enable &&
319	    !iwlwifi_mod_params.disable_11ax &&
320	    !iwlwifi_mod_params.disable_11be)
321		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
322
323	/* With MLD FW API, it tracks timing by itself,
324	 * no need for any timing from the host
325	 */
326	if (!mvm->mld_api_is_used)
327		ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
328
329	/* We should probably have this, but mac80211
330	 * currently doesn't support it for MLO.
331	 */
332	if (!(hw->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
333		ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
334
335	/*
336	 * On older devices, enabling TX A-MSDU occasionally leads to
337	 * something getting messed up, the command read from the FIFO
338	 * gets out of sync and isn't a TX command, so that we have an
339	 * assert EDC.
340	 *
341	 * It's not clear where the bug is, but since we didn't used to
342	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
343	 * for older devices. We also don't see this issue on any newer
344	 * devices.
345	 */
346	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
347		ieee80211_hw_set(hw, TX_AMSDU);
348	ieee80211_hw_set(hw, TX_FRAG_LIST);
349
350	if (iwl_mvm_has_tlc_offload(mvm)) {
351		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
352		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
353	}
354
355	if (iwl_mvm_has_new_rx_api(mvm))
356		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
357
358	if (fw_has_capa(&mvm->fw->ucode_capa,
359			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
360		ieee80211_hw_set(hw, AP_LINK_PS);
361	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
362		/*
363		 * we absolutely need this for the new TX API since that comes
364		 * with many more queues than the current code can deal with
365		 * for station powersave
366		 */
367		return -EINVAL;
368	}
369
370	if (mvm->trans->num_rx_queues > 1)
371		ieee80211_hw_set(hw, USES_RSS);
372
373	if (mvm->trans->max_skb_frags)
374		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
375
376	hw->queues = IEEE80211_NUM_ACS;
377	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
378	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
379				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
380	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
381		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
382
383	hw->radiotap_timestamp.units_pos =
384		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
385		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
386	/* this is the case for CCK frames, it's better (only 8) for OFDM */
387	hw->radiotap_timestamp.accuracy = 22;
388
389	if (!iwl_mvm_has_tlc_offload(mvm))
390		hw->rate_control_algorithm = RS_NAME;
391
392	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
393	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
394	hw->max_tx_fragments = mvm->trans->max_skb_frags;
395
396	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
397	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
398	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
399	hw->wiphy->cipher_suites = mvm->ciphers;
400
401	if (iwl_mvm_has_new_rx_api(mvm)) {
402		mvm->ciphers[hw->wiphy->n_cipher_suites] =
403			WLAN_CIPHER_SUITE_GCMP;
404		hw->wiphy->n_cipher_suites++;
405		mvm->ciphers[hw->wiphy->n_cipher_suites] =
406			WLAN_CIPHER_SUITE_GCMP_256;
407		hw->wiphy->n_cipher_suites++;
408	}
409
410	if (iwlwifi_mod_params.swcrypto)
411		IWL_ERR(mvm,
412			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
413	if (!iwlwifi_mod_params.bt_coex_active)
414		IWL_ERR(mvm,
415			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
416
417	ieee80211_hw_set(hw, MFP_CAPABLE);
418	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
419	hw->wiphy->n_cipher_suites++;
420	if (iwl_mvm_has_new_rx_api(mvm)) {
421		mvm->ciphers[hw->wiphy->n_cipher_suites] =
422			WLAN_CIPHER_SUITE_BIP_GMAC_128;
423		hw->wiphy->n_cipher_suites++;
424		mvm->ciphers[hw->wiphy->n_cipher_suites] =
425			WLAN_CIPHER_SUITE_BIP_GMAC_256;
426		hw->wiphy->n_cipher_suites++;
427	}
428
429	wiphy_ext_feature_set(hw->wiphy,
430			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
431	wiphy_ext_feature_set(hw->wiphy,
432			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
433
434	if (fw_has_capa(&mvm->fw->ucode_capa,
435			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
436		wiphy_ext_feature_set(hw->wiphy,
437				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
438		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
439	}
440
441	if (sec_key_ver &&
442	    fw_has_capa(&mvm->fw->ucode_capa,
443			IWL_UCODE_TLV_CAPA_BIGTK_TX_SUPPORT))
444		wiphy_ext_feature_set(hw->wiphy,
445				      NL80211_EXT_FEATURE_BEACON_PROTECTION);
446	else if (fw_has_capa(&mvm->fw->ucode_capa,
447			     IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
448		wiphy_ext_feature_set(hw->wiphy,
449				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
450
451	if (fw_has_capa(&mvm->fw->ucode_capa,
452			IWL_UCODE_TLV_CAPA_TIME_SYNC_BOTH_FTM_TM))
453		hw->wiphy->hw_timestamp_max_peers = 1;
454
455	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
456	hw->wiphy->features |=
457		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
458		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
459		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
460
461	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
462	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
463	hw->chanctx_data_size = sizeof(u16);
464	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
465
466	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
467		BIT(NL80211_IFTYPE_P2P_CLIENT) |
468		BIT(NL80211_IFTYPE_AP) |
469		BIT(NL80211_IFTYPE_P2P_GO) |
470		BIT(NL80211_IFTYPE_P2P_DEVICE) |
471		BIT(NL80211_IFTYPE_ADHOC);
472
473	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
474	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
475
476	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
477	 * the hw, preventing us to control which key(id) to use per MPDU.
478	 * Till that's fixed we can't use Extended Key ID for the newer cards.
479	 */
480	if (!iwl_mvm_has_new_tx_api(mvm))
481		wiphy_ext_feature_set(hw->wiphy,
482				      NL80211_EXT_FEATURE_EXT_KEY_ID);
483	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
484
485	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
486	if (iwl_mvm_is_lar_supported(mvm))
487		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
488	else
489		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
490					       REGULATORY_DISABLE_BEACON_HINTS;
491
492	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
493	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
494	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
495
496	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
497	hw->wiphy->n_iface_combinations =
498		ARRAY_SIZE(iwl_mvm_iface_combinations);
499
500	hw->wiphy->max_remain_on_channel_duration = 10000;
501	hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
502
503	/* Extract MAC address */
504	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
505	hw->wiphy->addresses = mvm->addresses;
506	hw->wiphy->n_addresses = 1;
507
508	/* Extract additional MAC addresses if available */
509	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
510		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
511
512	for (i = 1; i < num_mac; i++) {
513		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
514		       ETH_ALEN);
515		mvm->addresses[i].addr[5]++;
516		hw->wiphy->n_addresses++;
517	}
518
519	iwl_mvm_reset_phy_ctxts(mvm);
520
521	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
522
523	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
524
525	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
526	BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
527		     IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
528
529	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
530		mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
531	else
532		mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
533
534	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
535		hw->wiphy->bands[NL80211_BAND_2GHZ] =
536			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
537	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
538		hw->wiphy->bands[NL80211_BAND_5GHZ] =
539			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
540
541		if (fw_has_capa(&mvm->fw->ucode_capa,
542				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
543		    fw_has_api(&mvm->fw->ucode_capa,
544			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
545			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
546				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
547	}
548	if (fw_has_capa(&mvm->fw->ucode_capa,
549			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
550	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
551		hw->wiphy->bands[NL80211_BAND_6GHZ] =
552			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
553
554	hw->wiphy->hw_version = mvm->trans->hw_id;
555
556	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
557		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
558	else
559		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
560
561	hw->wiphy->max_sched_scan_reqs = 1;
562	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
563	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
564	/* we create the 802.11 header and zero length SSID IE. */
565	hw->wiphy->max_sched_scan_ie_len =
566		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
567	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
568	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
569
570	/*
571	 * the firmware uses u8 for num of iterations, but 0xff is saved for
572	 * infinite loop, so the maximum number of iterations is actually 254.
573	 */
574	hw->wiphy->max_sched_scan_plan_iterations = 254;
575
576	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
577			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
578			       NL80211_FEATURE_P2P_GO_OPPPS |
579			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
580			       NL80211_FEATURE_DYNAMIC_SMPS |
581			       NL80211_FEATURE_STATIC_SMPS |
582			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
583
584	if (fw_has_capa(&mvm->fw->ucode_capa,
585			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
586		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
587	if (fw_has_capa(&mvm->fw->ucode_capa,
588			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
589		hw->wiphy->features |= NL80211_FEATURE_QUIET;
590
591	if (fw_has_capa(&mvm->fw->ucode_capa,
592			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
593		hw->wiphy->features |=
594			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
595
596	if (fw_has_capa(&mvm->fw->ucode_capa,
597			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
598		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
599
600	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
601				  IWL_FW_CMD_VER_UNKNOWN) == 3)
602		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
603
604	if (fw_has_api(&mvm->fw->ucode_capa,
605		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
606		wiphy_ext_feature_set(hw->wiphy,
607				      NL80211_EXT_FEATURE_SCAN_START_TIME);
608		wiphy_ext_feature_set(hw->wiphy,
609				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
610	}
611
612	if (iwl_mvm_is_oce_supported(mvm)) {
613		u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
614
615		wiphy_ext_feature_set(hw->wiphy,
616			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
617		wiphy_ext_feature_set(hw->wiphy,
618			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
619		wiphy_ext_feature_set(hw->wiphy,
620			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
621
622		/* Old firmware also supports probe deferral and suppression */
623		if (scan_ver < 15)
624			wiphy_ext_feature_set(hw->wiphy,
625					      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
626	}
627
628	hw->wiphy->iftype_ext_capab = NULL;
629	hw->wiphy->num_iftype_ext_capab = 0;
630
631	if (mvm->nvm_data->sku_cap_11ax_enable &&
632	    !iwlwifi_mod_params.disable_11ax) {
633		hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa;
634		hw->wiphy->num_iftype_ext_capab =
635			ARRAY_SIZE(add_iftypes_ext_capa) - 1;
636
637		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
638		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
639	}
640
641	if (iwl_fw_lookup_cmd_ver(mvm->fw,
642				  WIDE_ID(DATA_PATH_GROUP,
643					  WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
644				  IWL_FW_CMD_VER_UNKNOWN) >= 1) {
645		IWL_DEBUG_INFO(mvm->trans, "Timing measurement supported\n");
646
647		if (!hw->wiphy->iftype_ext_capab) {
648			hw->wiphy->num_iftype_ext_capab = 1;
649			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa +
650				ARRAY_SIZE(add_iftypes_ext_capa) - 1;
651		} else {
652			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa + 1;
653		}
654	}
655
656	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
657
658#ifdef CONFIG_PM_SLEEP
659	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
660	    mvm->trans->ops->d3_suspend &&
661	    mvm->trans->ops->d3_resume &&
662	    device_can_wakeup(mvm->trans->dev)) {
663		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
664				     WIPHY_WOWLAN_DISCONNECT |
665				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
666				     WIPHY_WOWLAN_RFKILL_RELEASE |
667				     WIPHY_WOWLAN_NET_DETECT;
668		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
669				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
670				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
671
672		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
673		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
674		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
675		mvm->wowlan.max_nd_match_sets =
676			iwl_umac_scan_get_max_profiles(mvm->fw);
677		hw->wiphy->wowlan = &mvm->wowlan;
678	}
679#endif
680
681	ret = iwl_mvm_leds_init(mvm);
682	if (ret)
683		return ret;
684
685	if (fw_has_capa(&mvm->fw->ucode_capa,
686			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
687		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
688		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
689		ieee80211_hw_set(hw, TDLS_WIDER_BW);
690	}
691
692	if (fw_has_capa(&mvm->fw->ucode_capa,
693			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
694		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
695		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
696	}
697
698	hw->netdev_features |= mvm->cfg->features;
699	if (!iwl_mvm_is_csum_supported(mvm))
700		hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
701
702	if (mvm->cfg->vht_mu_mimo_supported)
703		wiphy_ext_feature_set(hw->wiphy,
704				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
705
706	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
707		wiphy_ext_feature_set(hw->wiphy,
708				      NL80211_EXT_FEATURE_PROTECTED_TWT);
709
710	iwl_mvm_vendor_cmds_register(mvm);
711
712	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
713	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
714
715	ret = ieee80211_register_hw(mvm->hw);
716	if (ret) {
717		iwl_mvm_leds_exit(mvm);
718	}
719
720	return ret;
721}
722
723static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
724			   struct ieee80211_sta *sta)
725{
726	if (likely(sta)) {
727		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
728			return;
729	} else {
730		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
731			return;
732	}
733
734	ieee80211_free_txskb(mvm->hw, skb);
735}
736
737void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
738		    struct ieee80211_tx_control *control, struct sk_buff *skb)
739{
740	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
741	struct ieee80211_sta *sta = control->sta;
742	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
743	struct ieee80211_hdr *hdr = (void *)skb->data;
744	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
745		IEEE80211_TX_CTL_TX_OFFCHAN;
746	u32 link_id = u32_get_bits(info->control.flags,
747				   IEEE80211_TX_CTRL_MLO_LINK);
748	struct ieee80211_sta *tmp_sta = sta;
749
750	if (iwl_mvm_is_radio_killed(mvm)) {
751		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
752		goto drop;
753	}
754
755	if (offchannel &&
756	    !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
757	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
758		goto drop;
759
760	/*
761	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
762	 * so we treat the others as broadcast
763	 */
764	if (ieee80211_is_mgmt(hdr->frame_control))
765		sta = NULL;
766
767	/* If there is no sta, and it's not offchannel - send through AP */
768	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
769	    !offchannel) {
770		struct iwl_mvm_vif *mvmvif =
771			iwl_mvm_vif_from_mac80211(info->control.vif);
772		u8 ap_sta_id = READ_ONCE(mvmvif->deflink.ap_sta_id);
773
774		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
775			/* mac80211 holds rcu read lock */
776			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
777			if (IS_ERR_OR_NULL(sta))
778				goto drop;
779		}
780	}
781
782	if (tmp_sta && !sta && link_id != IEEE80211_LINK_UNSPECIFIED &&
783	    !ieee80211_is_probe_resp(hdr->frame_control)) {
784		/* translate MLD addresses to LINK addresses */
785		struct ieee80211_link_sta *link_sta =
786			rcu_dereference(tmp_sta->link[link_id]);
787		struct ieee80211_bss_conf *link_conf =
788			rcu_dereference(info->control.vif->link_conf[link_id]);
789		struct ieee80211_mgmt *mgmt;
790
791		if (WARN_ON(!link_sta || !link_conf))
792			goto drop;
793
794		/* if sta is NULL, the frame is a management frame */
795		mgmt = (void *)hdr;
796		memcpy(mgmt->da, link_sta->addr, ETH_ALEN);
797		memcpy(mgmt->sa, link_conf->addr, ETH_ALEN);
798		memcpy(mgmt->bssid, link_conf->bssid, ETH_ALEN);
799	}
800
801	iwl_mvm_tx_skb(mvm, skb, sta);
802	return;
803 drop:
804	ieee80211_free_txskb(hw, skb);
805}
806
807void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
808{
809	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
810	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
811	struct sk_buff *skb = NULL;
812
813	/*
814	 * No need for threads to be pending here, they can leave the first
815	 * taker all the work.
816	 *
817	 * mvmtxq->tx_request logic:
818	 *
819	 * If 0, no one is currently TXing, set to 1 to indicate current thread
820	 * will now start TX and other threads should quit.
821	 *
822	 * If 1, another thread is currently TXing, set to 2 to indicate to
823	 * that thread that there was another request. Since that request may
824	 * have raced with the check whether the queue is empty, the TXing
825	 * thread should check the queue's status one more time before leaving.
826	 * This check is done in order to not leave any TX hanging in the queue
827	 * until the next TX invocation (which may not even happen).
828	 *
829	 * If 2, another thread is currently TXing, and it will already double
830	 * check the queue, so do nothing.
831	 */
832	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
833		return;
834
835	rcu_read_lock();
836	do {
837		while (likely(!test_bit(IWL_MVM_TXQ_STATE_STOP_FULL,
838					&mvmtxq->state) &&
839			      !test_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT,
840					&mvmtxq->state) &&
841			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
842			skb = ieee80211_tx_dequeue(hw, txq);
843
844			if (!skb) {
845				if (txq->sta)
846					IWL_DEBUG_TX(mvm,
847						     "TXQ of sta %pM tid %d is now empty\n",
848						     txq->sta->addr,
849						     txq->tid);
850				break;
851			}
852
853			iwl_mvm_tx_skb(mvm, skb, txq->sta);
854		}
855	} while (atomic_dec_return(&mvmtxq->tx_request));
856	rcu_read_unlock();
857}
858
859void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
860			       struct ieee80211_txq *txq)
861{
862	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
863	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
864
865	if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
866	    !txq->sta) {
867		iwl_mvm_mac_itxq_xmit(hw, txq);
868		return;
869	}
870
871	/* iwl_mvm_mac_itxq_xmit() will later be called by the worker
872	 * to handle any packets we leave on the txq now
873	 */
874
875	spin_lock_bh(&mvm->add_stream_lock);
876	/* The list is being deleted only after the queue is fully allocated. */
877	if (list_empty(&mvmtxq->list) &&
878	    /* recheck under lock */
879	    !test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) {
880		list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
881		schedule_work(&mvm->add_stream_wk);
882	}
883	spin_unlock_bh(&mvm->add_stream_lock);
884}
885
886#define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
887	do {								\
888		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
889			break;						\
890		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
891	} while (0)
892
893static void
894iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
895			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
896			    enum ieee80211_ampdu_mlme_action action)
897{
898	struct iwl_fw_dbg_trigger_tlv *trig;
899	struct iwl_fw_dbg_trigger_ba *ba_trig;
900
901	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
902				     FW_DBG_TRIGGER_BA);
903	if (!trig)
904		return;
905
906	ba_trig = (void *)trig->data;
907
908	switch (action) {
909	case IEEE80211_AMPDU_TX_OPERATIONAL: {
910		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
911		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
912
913		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
914				 "TX AGG START: MAC %pM tid %d ssn %d\n",
915				 sta->addr, tid, tid_data->ssn);
916		break;
917		}
918	case IEEE80211_AMPDU_TX_STOP_CONT:
919		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
920				 "TX AGG STOP: MAC %pM tid %d\n",
921				 sta->addr, tid);
922		break;
923	case IEEE80211_AMPDU_RX_START:
924		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
925				 "RX AGG START: MAC %pM tid %d ssn %d\n",
926				 sta->addr, tid, rx_ba_ssn);
927		break;
928	case IEEE80211_AMPDU_RX_STOP:
929		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
930				 "RX AGG STOP: MAC %pM tid %d\n",
931				 sta->addr, tid);
932		break;
933	default:
934		break;
935	}
936}
937
938int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
939			     struct ieee80211_vif *vif,
940			     struct ieee80211_ampdu_params *params)
941{
942	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
943	int ret;
944	struct ieee80211_sta *sta = params->sta;
945	enum ieee80211_ampdu_mlme_action action = params->action;
946	u16 tid = params->tid;
947	u16 *ssn = &params->ssn;
948	u16 buf_size = params->buf_size;
949	bool amsdu = params->amsdu;
950	u16 timeout = params->timeout;
951
952	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
953		     sta->addr, tid, action);
954
955	if (!(mvm->nvm_data->sku_cap_11n_enable))
956		return -EACCES;
957
958	mutex_lock(&mvm->mutex);
959
960	switch (action) {
961	case IEEE80211_AMPDU_RX_START:
962		if (iwl_mvm_vif_from_mac80211(vif)->deflink.ap_sta_id ==
963		    iwl_mvm_sta_from_mac80211(sta)->deflink.sta_id) {
964			struct iwl_mvm_vif *mvmvif;
965			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
966			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
967
968			mdata->opened_rx_ba_sessions = true;
969			mvmvif = iwl_mvm_vif_from_mac80211(vif);
970			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
971		}
972		if (!iwl_enable_rx_ampdu()) {
973			ret = -EINVAL;
974			break;
975		}
976		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
977					 timeout);
978		break;
979	case IEEE80211_AMPDU_RX_STOP:
980		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
981					 timeout);
982		break;
983	case IEEE80211_AMPDU_TX_START:
984		if (!iwl_enable_tx_ampdu()) {
985			ret = -EINVAL;
986			break;
987		}
988		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
989		break;
990	case IEEE80211_AMPDU_TX_STOP_CONT:
991		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
992		break;
993	case IEEE80211_AMPDU_TX_STOP_FLUSH:
994	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
995		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
996		break;
997	case IEEE80211_AMPDU_TX_OPERATIONAL:
998		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
999					      buf_size, amsdu);
1000		break;
1001	default:
1002		WARN_ON_ONCE(1);
1003		ret = -EINVAL;
1004		break;
1005	}
1006
1007	if (!ret) {
1008		u16 rx_ba_ssn = 0;
1009
1010		if (action == IEEE80211_AMPDU_RX_START)
1011			rx_ba_ssn = *ssn;
1012
1013		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1014					    rx_ba_ssn, action);
1015	}
1016	mutex_unlock(&mvm->mutex);
1017
1018	return ret;
1019}
1020
1021static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1022				     struct ieee80211_vif *vif)
1023{
1024	struct iwl_mvm *mvm = data;
1025	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1026	struct iwl_probe_resp_data *probe_data;
1027	unsigned int link_id;
1028
1029	mvmvif->uploaded = false;
1030
1031	spin_lock_bh(&mvm->time_event_lock);
1032	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1033	spin_unlock_bh(&mvm->time_event_lock);
1034
1035	memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
1036
1037	for_each_mvm_vif_valid_link(mvmvif, link_id) {
1038		mvmvif->link[link_id]->ap_sta_id = IWL_MVM_INVALID_STA;
1039		mvmvif->link[link_id]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1040		mvmvif->link[link_id]->phy_ctxt = NULL;
1041		mvmvif->link[link_id]->active = 0;
1042		mvmvif->link[link_id]->igtk = NULL;
1043	}
1044
1045	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1046					       lockdep_is_held(&mvm->mutex));
1047	if (probe_data)
1048		kfree_rcu(probe_data, rcu_head);
1049	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1050}
1051
1052static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1053{
1054	iwl_mvm_stop_device(mvm);
1055
1056	mvm->cur_aid = 0;
1057
1058	mvm->scan_status = 0;
1059	mvm->ps_disabled = false;
1060	mvm->rfkill_safe_init_done = false;
1061
1062	/* just in case one was running */
1063	iwl_mvm_cleanup_roc_te(mvm);
1064	ieee80211_remain_on_channel_expired(mvm->hw);
1065
1066	iwl_mvm_ftm_restart(mvm);
1067
1068	/*
1069	 * cleanup all interfaces, even inactive ones, as some might have
1070	 * gone down during the HW restart
1071	 */
1072	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1073
1074	mvm->p2p_device_vif = NULL;
1075
1076	iwl_mvm_reset_phy_ctxts(mvm);
1077	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1078	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1079	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1080
1081	ieee80211_wake_queues(mvm->hw);
1082
1083	mvm->rx_ba_sessions = 0;
1084	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1085	mvm->monitor_on = false;
1086#ifdef CONFIG_IWLWIFI_DEBUGFS
1087	mvm->beacon_inject_active = false;
1088#endif
1089
1090	/* keep statistics ticking */
1091	iwl_mvm_accu_radio_stats(mvm);
1092}
1093
1094int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1095{
1096	int ret;
1097
1098	lockdep_assert_held(&mvm->mutex);
1099
1100	ret = iwl_mvm_mei_get_ownership(mvm);
1101	if (ret)
1102		return ret;
1103
1104	if (mvm->mei_nvm_data) {
1105		/* We got the NIC, we can now free the MEI NVM data */
1106		kfree(mvm->mei_nvm_data);
1107		mvm->mei_nvm_data = NULL;
1108
1109		/*
1110		 * We can't free the nvm_data we allocated based on the SAP
1111		 * data because we registered to cfg80211 with the channels
1112		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
1113		 * just in order to be able free it later.
1114		 * NULLify nvm_data so that we will read the NVM from the
1115		 * firmware this time.
1116		 */
1117		mvm->temp_nvm_data = mvm->nvm_data;
1118		mvm->nvm_data = NULL;
1119	}
1120
1121	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1122		/*
1123		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1124		 * so later code will - from now on - see that we're doing it.
1125		 */
1126		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1127		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1128		/* Clean up some internal and mac80211 state on restart */
1129		iwl_mvm_restart_cleanup(mvm);
1130	}
1131	ret = iwl_mvm_up(mvm);
1132
1133	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1134			       NULL);
1135	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1136			       NULL);
1137
1138	mvm->last_reset_or_resume_time_jiffies = jiffies;
1139
1140	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1141		/* Something went wrong - we need to finish some cleanup
1142		 * that normally iwl_mvm_mac_restart_complete() below
1143		 * would do.
1144		 */
1145		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1146	}
1147
1148	return ret;
1149}
1150
1151int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1152{
1153	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1154	int ret;
1155	int retry, max_retry = 0;
1156
1157	mutex_lock(&mvm->mutex);
1158
1159	/* we are starting the mac not in error flow, and restart is enabled */
1160	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
1161	    iwlwifi_mod_params.fw_restart) {
1162		max_retry = IWL_MAX_INIT_RETRY;
1163		/*
1164		 * This will prevent mac80211 recovery flows to trigger during
1165		 * init failures
1166		 */
1167		set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
1168	}
1169
1170	for (retry = 0; retry <= max_retry; retry++) {
1171		ret = __iwl_mvm_mac_start(mvm);
1172		if (!ret)
1173			break;
1174
1175		/*
1176		 * In PLDR sync PCI re-enumeration is needed. no point to retry
1177		 * mac start before that.
1178		 */
1179		if (mvm->pldr_sync) {
1180			iwl_mei_alive_notif(false);
1181			iwl_trans_pcie_remove(mvm->trans, true);
1182			break;
1183		}
1184
1185		IWL_ERR(mvm, "mac start retry %d\n", retry);
1186	}
1187	clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
1188
1189	mutex_unlock(&mvm->mutex);
1190
1191	iwl_mvm_mei_set_sw_rfkill_state(mvm);
1192
1193	return ret;
1194}
1195
1196static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1197{
1198	int ret;
1199
1200	mutex_lock(&mvm->mutex);
1201
1202	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1203
1204	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1205	if (ret)
1206		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1207			ret);
1208
1209	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1210
1211	/*
1212	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1213	 * of packets the FW sent out, so we must reconnect.
1214	 */
1215	iwl_mvm_teardown_tdls_peers(mvm);
1216
1217	mutex_unlock(&mvm->mutex);
1218}
1219
1220void iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1221				   enum ieee80211_reconfig_type reconfig_type)
1222{
1223	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1224
1225	switch (reconfig_type) {
1226	case IEEE80211_RECONFIG_TYPE_RESTART:
1227		iwl_mvm_restart_complete(mvm);
1228		break;
1229	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1230		break;
1231	}
1232}
1233
1234void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1235{
1236	lockdep_assert_held(&mvm->mutex);
1237
1238	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1239
1240	/* firmware counters are obviously reset now, but we shouldn't
1241	 * partially track so also clear the fw_reset_accu counters.
1242	 */
1243	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1244
1245	/* async_handlers_wk is now blocked */
1246
1247	if (!iwl_mvm_has_new_station_api(mvm->fw))
1248		iwl_mvm_rm_aux_sta(mvm);
1249
1250	iwl_mvm_stop_device(mvm);
1251
1252	iwl_mvm_async_handlers_purge(mvm);
1253	/* async_handlers_list is empty and will stay empty: HW is stopped */
1254
1255	/*
1256	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1257	 * hw (as restart_complete() won't be called in this case) and mac80211
1258	 * won't execute the restart.
1259	 * But make sure to cleanup interfaces that have gone down before/during
1260	 * HW restart was requested.
1261	 */
1262	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1263	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1264			       &mvm->status))
1265		ieee80211_iterate_interfaces(mvm->hw, 0,
1266					     iwl_mvm_cleanup_iterator, mvm);
1267
1268	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1269	 * make sure there's nothing left there and warn if any is found.
1270	 */
1271	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1272		int i;
1273
1274		for (i = 0; i < mvm->max_scans; i++) {
1275			if (WARN_ONCE(mvm->scan_uid_status[i],
1276				      "UMAC scan UID %d status was not cleaned\n",
1277				      i))
1278				mvm->scan_uid_status[i] = 0;
1279		}
1280	}
1281}
1282
1283void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1284{
1285	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1286
1287	flush_work(&mvm->async_handlers_wk);
1288	flush_work(&mvm->add_stream_wk);
1289
1290	/*
1291	 * Lock and clear the firmware running bit here already, so that
1292	 * new commands coming in elsewhere, e.g. from debugfs, will not
1293	 * be able to proceed. This is important here because one of those
1294	 * debugfs files causes the firmware dump to be triggered, and if we
1295	 * don't stop debugfs accesses before canceling that it could be
1296	 * retriggered after we flush it but before we've cleared the bit.
1297	 */
1298	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1299
1300	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
1301	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1302
1303	/*
1304	 * The work item could be running or queued if the
1305	 * ROC time event stops just as we get here.
1306	 */
1307	flush_work(&mvm->roc_done_wk);
1308
1309	iwl_mvm_mei_set_sw_rfkill_state(mvm);
1310
1311	mutex_lock(&mvm->mutex);
1312	__iwl_mvm_mac_stop(mvm);
1313	mutex_unlock(&mvm->mutex);
1314
1315	/*
1316	 * The worker might have been waiting for the mutex, let it run and
1317	 * discover that its list is now empty.
1318	 */
1319	cancel_work_sync(&mvm->async_handlers_wk);
1320}
1321
1322struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1323{
1324	u16 i;
1325
1326	lockdep_assert_held(&mvm->mutex);
1327
1328	for (i = 0; i < NUM_PHY_CTX; i++)
1329		if (!mvm->phy_ctxts[i].ref)
1330			return &mvm->phy_ctxts[i];
1331
1332	IWL_ERR(mvm, "No available PHY context\n");
1333	return NULL;
1334}
1335
1336int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1337			 s16 tx_power)
1338{
1339	u32 cmd_id = REDUCE_TX_POWER_CMD;
1340	int len;
1341	struct iwl_dev_tx_power_cmd cmd = {
1342		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1343		.common.mac_context_id =
1344			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1345		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1346	};
1347	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1348					   IWL_FW_CMD_VER_UNKNOWN);
1349
1350	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1351		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1352
1353	if (cmd_ver == 7)
1354		len = sizeof(cmd.v7);
1355	else if (cmd_ver == 6)
1356		len = sizeof(cmd.v6);
1357	else if (fw_has_api(&mvm->fw->ucode_capa,
1358			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
1359		len = sizeof(cmd.v5);
1360	else if (fw_has_capa(&mvm->fw->ucode_capa,
1361			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
1362		len = sizeof(cmd.v4);
1363	else
1364		len = sizeof(cmd.v3);
1365
1366	/* all structs have the same common part, add it */
1367	len += sizeof(cmd.common);
1368
1369	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1370}
1371
1372int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1373				struct ieee80211_vif *vif)
1374{
1375	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1376	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1377	int ret;
1378
1379	mutex_lock(&mvm->mutex);
1380
1381	if (vif->type == NL80211_IFTYPE_STATION) {
1382		struct iwl_mvm_sta *mvmsta;
1383
1384		mvmvif->csa_bcn_pending = false;
1385		mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
1386							  mvmvif->deflink.ap_sta_id);
1387
1388		if (WARN_ON(!mvmsta)) {
1389			ret = -EIO;
1390			goto out_unlock;
1391		}
1392
1393		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1394		if (mvm->mld_api_is_used)
1395			iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
1396		else
1397			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1398
1399		if (!fw_has_capa(&mvm->fw->ucode_capa,
1400				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1401			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1402			if (ret)
1403				goto out_unlock;
1404
1405			iwl_mvm_stop_session_protection(mvm, vif);
1406		}
1407	}
1408
1409	mvmvif->ps_disabled = false;
1410
1411	ret = iwl_mvm_power_update_ps(mvm);
1412
1413out_unlock:
1414	if (mvmvif->csa_failed)
1415		ret = -EIO;
1416	mutex_unlock(&mvm->mutex);
1417
1418	return ret;
1419}
1420
1421void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1422				  struct ieee80211_vif *vif)
1423{
1424	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1425	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1426	struct iwl_chan_switch_te_cmd cmd = {
1427		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1428							  mvmvif->color)),
1429		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1430	};
1431
1432	/*
1433	 * In the new flow since FW is in charge of the timing,
1434	 * if driver has canceled the channel switch he will receive the
1435	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1436	 */
1437	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1438				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1439		return;
1440
1441	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1442
1443	mutex_lock(&mvm->mutex);
1444	if (!fw_has_capa(&mvm->fw->ucode_capa,
1445			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
1446		iwl_mvm_remove_csa_period(mvm, vif);
1447	else
1448		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1449					     WIDE_ID(MAC_CONF_GROUP,
1450						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1451					     0, sizeof(cmd), &cmd));
1452	mvmvif->csa_failed = true;
1453	mutex_unlock(&mvm->mutex);
1454
1455	iwl_mvm_post_channel_switch(hw, vif);
1456}
1457
1458void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1459{
1460	struct iwl_mvm_vif *mvmvif;
1461	struct ieee80211_vif *vif;
1462
1463	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1464	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1465
1466	/* Trigger disconnect (should clear the CSA state) */
1467	ieee80211_chswitch_done(vif, false);
1468}
1469
1470static u8
1471iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
1472{
1473	int data_start;
1474	int control_start;
1475	int bw;
1476
1477	if (chandef->width == NL80211_CHAN_WIDTH_320)
1478		bw = 320;
1479	else if (chandef->width == NL80211_CHAN_WIDTH_160)
1480		bw = 160;
1481	else
1482		return 0;
1483
1484	/* data is bw wide so the start is half the width */
1485	data_start = chandef->center_freq1 - bw / 2;
1486	/* control is 20Mhz width */
1487	control_start = chandef->chan->center_freq - 10;
1488
1489	return (control_start - data_start) / 80;
1490}
1491
1492static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
1493					 struct ieee80211_vif *vif)
1494{
1495	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1496	int ret;
1497
1498	lockdep_assert_held(&mvm->mutex);
1499
1500	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1501	if (ret) {
1502		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1503		return ret;
1504	}
1505
1506	/* Only queue for this station is the mcast queue,
1507	 * which shouldn't be in TFD mask anyway
1508	 */
1509	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
1510					vif->type,
1511					IWL_STA_MULTICAST);
1512}
1513
1514static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1515				     struct ieee80211_vif *vif)
1516{
1517	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1518	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1519	int ret;
1520
1521	mutex_lock(&mvm->mutex);
1522
1523	mvmvif->mvm = mvm;
1524
1525	/* the first link always points to the default one */
1526	mvmvif->link[0] = &mvmvif->deflink;
1527
1528	/*
1529	 * Not much to do here. The stack will not allow interface
1530	 * types or combinations that we didn't advertise, so we
1531	 * don't really have to check the types.
1532	 */
1533
1534	/* make sure that beacon statistics don't go backwards with FW reset */
1535	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1536		mvmvif->deflink.beacon_stats.accu_num_beacons +=
1537			mvmvif->deflink.beacon_stats.num_beacons;
1538
1539	/* Allocate resources for the MAC context, and add it to the fw  */
1540	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1541	if (ret)
1542		goto out;
1543
1544	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1545
1546	/* Currently not much to do for NAN */
1547	if (vif->type == NL80211_IFTYPE_NAN) {
1548		ret = 0;
1549		goto out;
1550	}
1551
1552	/*
1553	 * The AP binding flow can be done only after the beacon
1554	 * template is configured (which happens only in the mac80211
1555	 * start_ap() flow), and adding the broadcast station can happen
1556	 * only after the binding.
1557	 * In addition, since modifying the MAC before adding a bcast
1558	 * station is not allowed by the FW, delay the adding of MAC context to
1559	 * the point where we can also add the bcast station.
1560	 * In short: there's not much we can do at this point, other than
1561	 * allocating resources :)
1562	 */
1563	if (vif->type == NL80211_IFTYPE_AP ||
1564	    vif->type == NL80211_IFTYPE_ADHOC) {
1565		iwl_mvm_vif_dbgfs_register(mvm, vif);
1566		ret = 0;
1567		goto out;
1568	}
1569
1570	mvmvif->features |= hw->netdev_features;
1571
1572	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1573	if (ret)
1574		goto out_unlock;
1575
1576	ret = iwl_mvm_power_update_mac(mvm);
1577	if (ret)
1578		goto out_remove_mac;
1579
1580	/* beacon filtering */
1581	ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1582	if (ret)
1583		goto out_remove_mac;
1584
1585	if (!mvm->bf_allowed_vif &&
1586	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1587		mvm->bf_allowed_vif = mvmvif;
1588		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1589				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1590	}
1591
1592	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1593		mvm->p2p_device_vif = vif;
1594
1595	iwl_mvm_tcm_add_vif(mvm, vif);
1596	INIT_DELAYED_WORK(&mvmvif->csa_work,
1597			  iwl_mvm_channel_switch_disconnect_wk);
1598
1599	if (vif->type == NL80211_IFTYPE_MONITOR) {
1600		mvm->monitor_on = true;
1601		mvm->monitor_p80 =
1602			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chandef);
1603	}
1604
1605	iwl_mvm_vif_dbgfs_register(mvm, vif);
1606
1607	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1608	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
1609	    !mvm->csme_vif && mvm->mei_registered) {
1610		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
1611		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
1612		mvm->csme_vif = vif;
1613	}
1614
1615out:
1616	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
1617		     vif->type == NL80211_IFTYPE_ADHOC))
1618		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
1619
1620	goto out_unlock;
1621
1622	if (mvm->bf_allowed_vif == mvmvif) {
1623		mvm->bf_allowed_vif = NULL;
1624		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1625				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1626	}
1627 out_remove_mac:
1628	mvmvif->deflink.phy_ctxt = NULL;
1629	iwl_mvm_mac_ctxt_remove(mvm, vif);
1630 out_unlock:
1631	mutex_unlock(&mvm->mutex);
1632
1633	return ret;
1634}
1635
1636void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1637				 struct ieee80211_vif *vif)
1638{
1639	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1640		/*
1641		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1642		 * We assume here that all the packets sent to the OFFCHANNEL
1643		 * queue are sent in ROC session.
1644		 */
1645		flush_work(&mvm->roc_done_wk);
1646	}
1647}
1648
1649/* This function is doing the common part of removing the interface for
1650 * both - MLD and non-MLD modes. Returns true if removing the interface
1651 * is done
1652 */
1653static bool iwl_mvm_mac_remove_interface_common(struct ieee80211_hw *hw,
1654						struct ieee80211_vif *vif)
1655{
1656	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1657	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1658	struct iwl_probe_resp_data *probe_data;
1659
1660	iwl_mvm_prepare_mac_removal(mvm, vif);
1661
1662	if (!(vif->type == NL80211_IFTYPE_AP ||
1663	      vif->type == NL80211_IFTYPE_ADHOC))
1664		iwl_mvm_tcm_rm_vif(mvm, vif);
1665
1666	mutex_lock(&mvm->mutex);
1667
1668	if (vif == mvm->csme_vif) {
1669		iwl_mei_set_netdev(NULL);
1670		mvm->csme_vif = NULL;
1671	}
1672
1673	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1674					       lockdep_is_held(&mvm->mutex));
1675	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1676	if (probe_data)
1677		kfree_rcu(probe_data, rcu_head);
1678
1679	if (mvm->bf_allowed_vif == mvmvif) {
1680		mvm->bf_allowed_vif = NULL;
1681		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1682				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1683	}
1684
1685	if (vif->bss_conf.ftm_responder)
1686		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1687
1688	iwl_mvm_vif_dbgfs_clean(mvm, vif);
1689
1690	/*
1691	 * For AP/GO interface, the tear down of the resources allocated to the
1692	 * interface is be handled as part of the stop_ap flow.
1693	 */
1694	if (vif->type == NL80211_IFTYPE_AP ||
1695	    vif->type == NL80211_IFTYPE_ADHOC) {
1696#ifdef CONFIG_NL80211_TESTMODE
1697		if (vif == mvm->noa_vif) {
1698			mvm->noa_vif = NULL;
1699			mvm->noa_duration = 0;
1700		}
1701#endif
1702		return true;
1703	}
1704
1705	iwl_mvm_power_update_mac(mvm);
1706	return false;
1707}
1708
1709static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1710					 struct ieee80211_vif *vif)
1711{
1712	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1713	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1714
1715	if (iwl_mvm_mac_remove_interface_common(hw, vif))
1716		goto out;
1717
1718	/* Before the interface removal, mac80211 would cancel the ROC, and the
1719	 * ROC worker would be scheduled if needed. The worker would be flushed
1720	 * in iwl_mvm_prepare_mac_removal() and thus at this point there is no
1721	 * binding etc. so nothing needs to be done here.
1722	 */
1723	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1724		if (mvmvif->deflink.phy_ctxt) {
1725			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1726			mvmvif->deflink.phy_ctxt = NULL;
1727		}
1728		mvm->p2p_device_vif = NULL;
1729	}
1730
1731	iwl_mvm_mac_ctxt_remove(mvm, vif);
1732
1733	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1734
1735	if (vif->type == NL80211_IFTYPE_MONITOR)
1736		mvm->monitor_on = false;
1737
1738out:
1739	if (vif->type == NL80211_IFTYPE_AP ||
1740	    vif->type == NL80211_IFTYPE_ADHOC) {
1741		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
1742		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1743	}
1744
1745	mutex_unlock(&mvm->mutex);
1746}
1747
1748struct iwl_mvm_mc_iter_data {
1749	struct iwl_mvm *mvm;
1750	int port_id;
1751};
1752
1753static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1754				      struct ieee80211_vif *vif)
1755{
1756	struct iwl_mvm_mc_iter_data *data = _data;
1757	struct iwl_mvm *mvm = data->mvm;
1758	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1759	struct iwl_host_cmd hcmd = {
1760		.id = MCAST_FILTER_CMD,
1761		.flags = CMD_ASYNC,
1762		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1763	};
1764	int ret, len;
1765
1766	/* if we don't have free ports, mcast frames will be dropped */
1767	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1768		return;
1769
1770	if (vif->type != NL80211_IFTYPE_STATION ||
1771	    !vif->cfg.assoc)
1772		return;
1773
1774	cmd->port_id = data->port_id++;
1775	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1776	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1777
1778	hcmd.len[0] = len;
1779	hcmd.data[0] = cmd;
1780
1781	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1782	if (ret)
1783		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1784}
1785
1786static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1787{
1788	struct iwl_mvm_mc_iter_data iter_data = {
1789		.mvm = mvm,
1790	};
1791	int ret;
1792
1793	lockdep_assert_held(&mvm->mutex);
1794
1795	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1796		return;
1797
1798	ieee80211_iterate_active_interfaces_atomic(
1799		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1800		iwl_mvm_mc_iface_iterator, &iter_data);
1801
1802	/*
1803	 * Send a (synchronous) ech command so that we wait for the
1804	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
1805	 * the interface iterator. Otherwise, we might get here over
1806	 * and over again (by userspace just sending a lot of these)
1807	 * and the CPU can send them faster than the firmware can
1808	 * process them.
1809	 * Note that the CPU is still faster - but with this we'll
1810	 * actually send fewer commands overall because the CPU will
1811	 * not schedule the work in mac80211 as frequently if it's
1812	 * still running when rescheduled (possibly multiple times).
1813	 */
1814	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1815	if (ret)
1816		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
1817}
1818
1819u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1820			      struct netdev_hw_addr_list *mc_list)
1821{
1822	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1823	struct iwl_mcast_filter_cmd *cmd;
1824	struct netdev_hw_addr *addr;
1825	int addr_count;
1826	bool pass_all;
1827	int len;
1828
1829	addr_count = netdev_hw_addr_list_count(mc_list);
1830	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1831		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1832	if (pass_all)
1833		addr_count = 0;
1834
1835	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1836	cmd = kzalloc(len, GFP_ATOMIC);
1837	if (!cmd)
1838		return 0;
1839
1840	if (pass_all) {
1841		cmd->pass_all = 1;
1842		return (u64)(unsigned long)cmd;
1843	}
1844
1845	netdev_hw_addr_list_for_each(addr, mc_list) {
1846		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1847				   cmd->count, addr->addr);
1848		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1849		       addr->addr, ETH_ALEN);
1850		cmd->count++;
1851	}
1852
1853	return (u64)(unsigned long)cmd;
1854}
1855
1856void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1857			      unsigned int changed_flags,
1858			      unsigned int *total_flags, u64 multicast)
1859{
1860	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1861	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1862
1863	mutex_lock(&mvm->mutex);
1864
1865	/* replace previous configuration */
1866	kfree(mvm->mcast_filter_cmd);
1867	mvm->mcast_filter_cmd = cmd;
1868
1869	if (!cmd)
1870		goto out;
1871
1872	if (changed_flags & FIF_ALLMULTI)
1873		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
1874
1875	if (cmd->pass_all)
1876		cmd->count = 0;
1877
1878	iwl_mvm_recalc_multicast(mvm);
1879out:
1880	mutex_unlock(&mvm->mutex);
1881	*total_flags = 0;
1882}
1883
1884static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1885					struct ieee80211_vif *vif,
1886					unsigned int filter_flags,
1887					unsigned int changed_flags)
1888{
1889	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1890
1891	/* We support only filter for probe requests */
1892	if (!(changed_flags & FIF_PROBE_REQ))
1893		return;
1894
1895	/* Supported only for p2p client interfaces */
1896	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
1897	    !vif->p2p)
1898		return;
1899
1900	mutex_lock(&mvm->mutex);
1901	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1902	mutex_unlock(&mvm->mutex);
1903}
1904
1905int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1906{
1907	struct iwl_mu_group_mgmt_cmd cmd = {};
1908
1909	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1910	       WLAN_MEMBERSHIP_LEN);
1911	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1912	       WLAN_USER_POSITION_LEN);
1913
1914	return iwl_mvm_send_cmd_pdu(mvm,
1915				    WIDE_ID(DATA_PATH_GROUP,
1916					    UPDATE_MU_GROUPS_CMD),
1917				    0, sizeof(cmd), &cmd);
1918}
1919
1920static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1921					   struct ieee80211_vif *vif)
1922{
1923	if (vif->bss_conf.mu_mimo_owner) {
1924		struct iwl_mu_group_mgmt_notif *notif = _data;
1925
1926		/*
1927		 * MU-MIMO Group Id action frame is little endian. We treat
1928		 * the data received from firmware as if it came from the
1929		 * action frame, so no conversion is needed.
1930		 */
1931		ieee80211_update_mu_groups(vif, 0,
1932					   (u8 *)&notif->membership_status,
1933					   (u8 *)&notif->user_position);
1934	}
1935}
1936
1937void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1938			       struct iwl_rx_cmd_buffer *rxb)
1939{
1940	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1941	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1942
1943	ieee80211_iterate_active_interfaces_atomic(
1944			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1945			iwl_mvm_mu_mimo_iface_iterator, notif);
1946}
1947
1948static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1949{
1950	u8 byte_num = ppe_pos_bit / 8;
1951	u8 bit_num = ppe_pos_bit % 8;
1952	u8 residue_bits;
1953	u8 res;
1954
1955	if (bit_num <= 5)
1956		return (ppe[byte_num] >> bit_num) &
1957		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
1958
1959	/*
1960	 * If bit_num > 5, we have to combine bits with next byte.
1961	 * Calculate how many bits we need to take from current byte (called
1962	 * here "residue_bits"), and add them to bits from next byte.
1963	 */
1964
1965	residue_bits = 8 - bit_num;
1966
1967	res = (ppe[byte_num + 1] &
1968	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
1969	      residue_bits;
1970	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
1971
1972	return res;
1973}
1974
1975static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
1976			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
1977			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
1978			      bool inheritance)
1979{
1980	int i;
1981
1982	/*
1983	* FW currently supports only nss == MAX_HE_SUPP_NSS
1984	*
1985	* If nss > MAX: we can ignore values we don't support
1986	* If nss < MAX: we can set zeros in other streams
1987	*/
1988	if (nss > MAX_HE_SUPP_NSS) {
1989		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
1990			       MAX_HE_SUPP_NSS);
1991		nss = MAX_HE_SUPP_NSS;
1992	}
1993
1994	for (i = 0; i < nss; i++) {
1995		u8 ru_index_tmp = ru_index_bitmap << 1;
1996		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
1997		u8 bw;
1998
1999		for (bw = 0;
2000		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2001		     bw++) {
2002			ru_index_tmp >>= 1;
2003
2004			/*
2005			* According to the 11be spec, if for a specific BW the PPE Thresholds
2006			* isn't present - it should inherit the thresholds from the last
2007			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2008			* this inheritance - continue in this case
2009			*/
2010			if (!(ru_index_tmp & 1)) {
2011				if (inheritance)
2012					goto set_thresholds;
2013				else
2014					continue;
2015			}
2016
2017			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2018			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2019			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2020			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2021
2022set_thresholds:
2023			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2024			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2025		}
2026	}
2027}
2028
2029static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
2030					    struct ieee80211_link_sta *link_sta,
2031					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2032					    bool inheritance)
2033{
2034	u8 nss = (link_sta->he_cap.ppe_thres[0] &
2035		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2036	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2037	u8 ru_index_bitmap =
2038		u8_get_bits(*ppe,
2039			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2040	/* Starting after PPE header */
2041	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2042
2043	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2044			  inheritance);
2045}
2046
2047static int
2048iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
2049					 u8 nominal_padding)
2050{
2051	int low_th = -1;
2052	int high_th = -1;
2053	int i;
2054
2055	/* all the macros are the same for EHT and HE */
2056	switch (nominal_padding) {
2057	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2058		low_th = IWL_HE_PKT_EXT_NONE;
2059		high_th = IWL_HE_PKT_EXT_NONE;
2060		break;
2061	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2062		low_th = IWL_HE_PKT_EXT_BPSK;
2063		high_th = IWL_HE_PKT_EXT_NONE;
2064		break;
2065	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2066	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2067		low_th = IWL_HE_PKT_EXT_NONE;
2068		high_th = IWL_HE_PKT_EXT_BPSK;
2069		break;
2070	}
2071
2072	if (low_th < 0 || high_th < 0)
2073		return -EINVAL;
2074
2075	/* Set the PPE thresholds accordingly */
2076	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2077		u8 bw;
2078
2079		for (bw = 0;
2080			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2081			bw++) {
2082			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2083			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2084		}
2085	}
2086
2087	return 0;
2088}
2089
2090static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2091					 u8 nominal_padding)
2092{
2093	int i;
2094
2095	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2096		u8 bw;
2097
2098		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2099		     bw++) {
2100			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2101
2102			if (nominal_padding >
2103			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2104			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2105				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2106			else if (nominal_padding ==
2107				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2108				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2109				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2110				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2111		}
2112	}
2113}
2114
2115/* Set the pkt_ext field according to PPE Thresholds element */
2116int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
2117			    struct ieee80211_link_sta *link_sta,
2118			    struct iwl_he_pkt_ext_v2 *pkt_ext)
2119{
2120	u8 nominal_padding;
2121	int i, ret = 0;
2122
2123	if (WARN_ON(!link_sta))
2124		return -EINVAL;
2125
2126	/* Initialize the PPE thresholds to "None" (7), as described in Table
2127	 * 9-262ac of 80211.ax/D3.0.
2128	 */
2129	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
2130	       sizeof(struct iwl_he_pkt_ext_v2));
2131
2132	if (link_sta->eht_cap.has_eht) {
2133		nominal_padding =
2134			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
2135				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
2136
2137		/* If PPE Thresholds exists, parse them into a FW-familiar
2138		 * format.
2139		 */
2140		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
2141		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
2142			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
2143				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
2144			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
2145			u8 ru_index_bitmap =
2146				u16_get_bits(*ppe,
2147					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2148			 /* Starting after PPE header */
2149			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
2150
2151			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
2152					  ppe, ppe_pos_bit, true);
2153		/* EHT PPE Thresholds doesn't exist - set the API according to
2154		 * HE PPE Tresholds
2155		 */
2156		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2157			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2158			/* Even though HE Capabilities IE doesn't contain PPE
2159			 * Thresholds for BW 320Mhz, thresholds for this BW will
2160			 * be filled in with the same values as 160Mhz, due to
2161			 * the inheritance, as required.
2162			 */
2163			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2164							true);
2165
2166			/* According to the requirements, for MCSs 12-13 the
2167			 * maximum value between HE PPE Threshold and Common
2168			 * Nominal Packet Padding needs to be taken
2169			 */
2170			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
2171
2172		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
2173		 * take the Thresholds from Common Nominal Packet Padding field
2174		 */
2175		} else {
2176			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2177								       nominal_padding);
2178		}
2179	} else if (link_sta->he_cap.has_he) {
2180		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2181		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2182			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2183			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2184							false);
2185		/* PPE Thresholds doesn't exist - set the API PPE values
2186		 * according to Common Nominal Packet Padding field.
2187		 */
2188		} else {
2189			nominal_padding =
2190				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
2191					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
2192			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
2193				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2194									       nominal_padding);
2195		}
2196	}
2197
2198	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2199		int bw;
2200
2201		for (bw = 0;
2202		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
2203		     bw++) {
2204			u8 *qam_th =
2205				&pkt_ext->pkt_ext_qam_th[i][bw][0];
2206
2207			IWL_DEBUG_HT(mvm,
2208				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
2209				     i, bw, qam_th[0], qam_th[1]);
2210		}
2211	}
2212	return ret;
2213}
2214
2215/*
2216 * This function sets the MU EDCA parameters ans returns whether MU EDCA
2217 * is enabled or not
2218 */
2219bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2220				   const struct iwl_mvm_vif_link_info *link_info,
2221				   struct iwl_he_backoff_conf *trig_based_txf)
2222{
2223	int i;
2224	/* Mark MU EDCA as enabled, unless none detected on some AC */
2225	bool mu_edca_enabled = true;
2226
2227	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2228		const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2229			&link_info->queue_params[i].mu_edca_param_rec;
2230		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2231
2232		if (!link_info->queue_params[i].mu_edca) {
2233			mu_edca_enabled = false;
2234			break;
2235		}
2236
2237		trig_based_txf[ac].cwmin =
2238			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
2239		trig_based_txf[ac].cwmax =
2240			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
2241		trig_based_txf[ac].aifsn =
2242			cpu_to_le16(mu_edca->aifsn & 0xf);
2243		trig_based_txf[ac].mu_time =
2244			cpu_to_le16(mu_edca->mu_edca_timer);
2245	}
2246
2247	return mu_edca_enabled;
2248}
2249
2250bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2251{
2252	const struct ieee80211_supported_band *sband;
2253	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
2254
2255	/* This capability is the same for all bands,
2256	 * so take it from one of them.
2257	 */
2258	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
2259	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
2260
2261	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
2262			       IEEE80211_HE_MAC_CAP2_ACK_EN));
2263}
2264
2265__le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
2266				 struct ieee80211_link_sta *link_sta)
2267{
2268	u8 *mac_cap_info =
2269		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
2270	__le32 htc_flags = 0;
2271
2272	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
2273		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2274	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2275	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2276		u8 link_adap =
2277			((mac_cap_info[2] &
2278			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2279			 (mac_cap_info[1] &
2280			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2281
2282		if (link_adap == 2)
2283			htc_flags |=
2284				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2285		else if (link_adap == 3)
2286			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2287	}
2288	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2289		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2290	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2291		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2292	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2293		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2294
2295	return htc_flags;
2296}
2297
2298static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2299			       struct ieee80211_vif *vif, u8 sta_id)
2300{
2301	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2302	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2303		.sta_id = sta_id,
2304		.tid_limit = IWL_MAX_TID_COUNT,
2305		.bss_color = vif->bss_conf.he_bss_color.color,
2306		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2307		.frame_time_rts_th =
2308			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2309	};
2310	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
2311	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
2312	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
2313	int size;
2314	struct ieee80211_sta *sta;
2315	u32 flags;
2316	int i;
2317	void *cmd;
2318
2319	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
2320		ver = 1;
2321
2322	switch (ver) {
2323	case 1:
2324		/* same layout as v2 except some data at the end */
2325		cmd = &sta_ctxt_cmd_v2;
2326		size = sizeof(struct iwl_he_sta_context_cmd_v1);
2327		break;
2328	case 2:
2329		cmd = &sta_ctxt_cmd_v2;
2330		size = sizeof(struct iwl_he_sta_context_cmd_v2);
2331		break;
2332	case 3:
2333		cmd = &sta_ctxt_cmd;
2334		size = sizeof(struct iwl_he_sta_context_cmd_v3);
2335		break;
2336	default:
2337		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
2338		return;
2339	}
2340
2341	rcu_read_lock();
2342
2343	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2344	if (IS_ERR_OR_NULL(sta)) {
2345		rcu_read_unlock();
2346		WARN(1, "Can't find STA to configure HE\n");
2347		return;
2348	}
2349
2350	if (!sta->deflink.he_cap.has_he) {
2351		rcu_read_unlock();
2352		return;
2353	}
2354
2355	flags = 0;
2356
2357	/* Block 26-tone RU OFDMA transmissions */
2358	if (mvmvif->deflink.he_ru_2mhz_block)
2359		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
2360
2361	/* HTC flags */
2362	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2363
2364	/* PPE Thresholds */
2365	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2366		flags |= STA_CTXT_HE_PACKET_EXT;
2367
2368	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2369	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
2370		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2371
2372	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2373	    IEEE80211_HE_MAC_CAP2_ACK_EN)
2374		flags |= STA_CTXT_HE_ACK_ENABLED;
2375
2376	rcu_read_unlock();
2377
2378	if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
2379					  &sta_ctxt_cmd.trig_based_txf[0]))
2380		flags |= STA_CTXT_HE_MU_EDCA_CW;
2381
2382	if (vif->bss_conf.uora_exists) {
2383		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2384
2385		sta_ctxt_cmd.rand_alloc_ecwmin =
2386			vif->bss_conf.uora_ocw_range & 0x7;
2387		sta_ctxt_cmd.rand_alloc_ecwmax =
2388			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2389	}
2390
2391	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2392		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2393
2394	if (vif->bss_conf.nontransmitted) {
2395		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2396		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2397				vif->bss_conf.transmitter_bssid);
2398		sta_ctxt_cmd.max_bssid_indicator =
2399			vif->bss_conf.bssid_indicator;
2400		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2401		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2402		sta_ctxt_cmd.profile_periodicity =
2403			vif->bss_conf.profile_periodicity;
2404	}
2405
2406	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2407
2408	if (ver < 3) {
2409		/* fields before pkt_ext */
2410		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
2411			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
2412		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
2413		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
2414
2415		/* pkt_ext */
2416		for (i = 0;
2417		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
2418		     i++) {
2419			u8 bw;
2420
2421			for (bw = 0;
2422			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
2423			     bw++) {
2424				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
2425					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
2426
2427				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
2428				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
2429				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
2430			}
2431		}
2432
2433		/* fields after pkt_ext */
2434		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
2435			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
2436			     sizeof(sta_ctxt_cmd_v2) -
2437			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
2438		memcpy((u8 *)&sta_ctxt_cmd_v2 +
2439				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
2440		       (u8 *)&sta_ctxt_cmd +
2441				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
2442		       sizeof(sta_ctxt_cmd) -
2443				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
2444		sta_ctxt_cmd_v2.reserved3 = 0;
2445	}
2446
2447	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2448		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2449}
2450
2451void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2452			   u32 duration_override)
2453{
2454	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2455	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2456
2457	if (duration_override > duration)
2458		duration = duration_override;
2459
2460	/* Try really hard to protect the session and hear a beacon
2461	 * The new session protection command allows us to protect the
2462	 * session for a much longer time since the firmware will internally
2463	 * create two events: a 300TU one with a very high priority that
2464	 * won't be fragmented which should be enough for 99% of the cases,
2465	 * and another one (which we configure here to be 900TU long) which
2466	 * will have a slightly lower priority, but more importantly, can be
2467	 * fragmented so that it'll allow other activities to run.
2468	 */
2469	if (fw_has_capa(&mvm->fw->ucode_capa,
2470			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2471		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2472						    min_duration, false);
2473	else
2474		iwl_mvm_protect_session(mvm, vif, duration,
2475					min_duration, 500, false);
2476}
2477
2478/* Handle association common part to MLD and non-MLD modes */
2479void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2480					    struct ieee80211_vif *vif,
2481					    u64 changes)
2482{
2483	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2484	int ret;
2485
2486	/* The firmware tracks the MU-MIMO group on its own.
2487	 * However, on HW restart we should restore this data.
2488	 */
2489	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2490	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2491		ret = iwl_mvm_update_mu_groups(mvm, vif);
2492		if (ret)
2493			IWL_ERR(mvm,
2494				"failed to update VHT MU_MIMO groups\n");
2495	}
2496
2497	iwl_mvm_recalc_multicast(mvm);
2498
2499	/* reset rssi values */
2500	mvmvif->bf_data.ave_beacon_signal = 0;
2501
2502	iwl_mvm_bt_coex_vif_change(mvm);
2503	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2504					    IEEE80211_SMPS_AUTOMATIC);
2505	if (fw_has_capa(&mvm->fw->ucode_capa,
2506			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2507		iwl_mvm_config_scan(mvm);
2508}
2509
2510/* Execute the common part for MLD and non-MLD modes */
2511void
2512iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2513					struct ieee80211_vif *vif,
2514					struct ieee80211_bss_conf *link_conf,
2515					u64 changes)
2516{
2517	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2518	int ret;
2519
2520	if (changes & BSS_CHANGED_BEACON_INFO) {
2521		/* We received a beacon from the associated AP so
2522		 * remove the session protection.
2523		 */
2524		iwl_mvm_stop_session_protection(mvm, vif);
2525
2526		iwl_mvm_sf_update(mvm, vif, false);
2527		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2528	}
2529
2530	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2531		       /* Send power command on every beacon change,
2532			* because we may have not enabled beacon abort yet.
2533			*/
2534		       BSS_CHANGED_BEACON_INFO)) {
2535		ret = iwl_mvm_power_update_mac(mvm);
2536		if (ret)
2537			IWL_ERR(mvm, "failed to update power mode\n");
2538	}
2539
2540	if (changes & BSS_CHANGED_CQM) {
2541		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2542		/* reset cqm events tracking */
2543		mvmvif->bf_data.last_cqm_event = 0;
2544		if (mvmvif->bf_data.bf_enabled) {
2545			/* FIXME: need to update per link when FW API will
2546			 * support it
2547			 */
2548			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2549			if (ret)
2550				IWL_ERR(mvm,
2551					"failed to update CQM thresholds\n");
2552		}
2553	}
2554
2555	if (changes & BSS_CHANGED_BANDWIDTH)
2556		iwl_mvm_update_link_smps(vif, link_conf);
2557}
2558
2559static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2560					     struct ieee80211_vif *vif,
2561					     struct ieee80211_bss_conf *bss_conf,
2562					     u64 changes)
2563{
2564	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2565	int ret;
2566
2567	/*
2568	 * Re-calculate the tsf id, as the leader-follower relations depend
2569	 * on the beacon interval, which was not known when the station
2570	 * interface was added.
2571	 */
2572	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2573		if ((vif->bss_conf.he_support &&
2574		     !iwlwifi_mod_params.disable_11ax) ||
2575		    (vif->bss_conf.eht_support &&
2576		     !iwlwifi_mod_params.disable_11be))
2577			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2578
2579		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2580	}
2581
2582	/* Update MU EDCA params */
2583	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2584	    vif->cfg.assoc &&
2585	    ((vif->bss_conf.he_support &&
2586	      !iwlwifi_mod_params.disable_11ax) ||
2587	     (vif->bss_conf.eht_support &&
2588	      !iwlwifi_mod_params.disable_11be)))
2589		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2590
2591	/*
2592	 * If we're not associated yet, take the (new) BSSID before associating
2593	 * so the firmware knows. If we're already associated, then use the old
2594	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2595	 * branch for disassociation below.
2596	 */
2597	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2598		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2599
2600	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2601	if (ret)
2602		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2603
2604	/* after sending it once, adopt mac80211 data */
2605	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2606	mvmvif->associated = vif->cfg.assoc;
2607
2608	if (changes & BSS_CHANGED_ASSOC) {
2609		if (vif->cfg.assoc) {
2610			/* clear statistics to get clean beacon counter */
2611			iwl_mvm_request_statistics(mvm, true);
2612			memset(&mvmvif->deflink.beacon_stats, 0,
2613			       sizeof(mvmvif->deflink.beacon_stats));
2614
2615			/* add quota for this interface */
2616			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2617			if (ret) {
2618				IWL_ERR(mvm, "failed to update quotas\n");
2619				return;
2620			}
2621
2622			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2623				     &mvm->status) &&
2624			    !fw_has_capa(&mvm->fw->ucode_capa,
2625					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2626				/*
2627				 * If we're restarting then the firmware will
2628				 * obviously have lost synchronisation with
2629				 * the AP. It will attempt to synchronise by
2630				 * itself, but we can make it more reliable by
2631				 * scheduling a session protection time event.
2632				 *
2633				 * The firmware needs to receive a beacon to
2634				 * catch up with synchronisation, use 110% of
2635				 * the beacon interval.
2636				 *
2637				 * Set a large maximum delay to allow for more
2638				 * than a single interface.
2639				 *
2640				 * For new firmware versions, rely on the
2641				 * firmware. This is relevant for DCM scenarios
2642				 * only anyway.
2643				 */
2644				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2645				iwl_mvm_protect_session(mvm, vif, dur, dur,
2646							5 * dur, false);
2647			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2648					     &mvm->status) &&
2649				   !vif->bss_conf.dtim_period) {
2650				/*
2651				 * If we're not restarting and still haven't
2652				 * heard a beacon (dtim period unknown) then
2653				 * make sure we still have enough minimum time
2654				 * remaining in the time event, since the auth
2655				 * might actually have taken quite a while
2656				 * (especially for SAE) and so the remaining
2657				 * time could be small without us having heard
2658				 * a beacon yet.
2659				 */
2660				iwl_mvm_protect_assoc(mvm, vif, 0);
2661			}
2662
2663			iwl_mvm_sf_update(mvm, vif, false);
2664			iwl_mvm_power_vif_assoc(mvm, vif);
2665			if (vif->p2p) {
2666				iwl_mvm_update_smps(mvm, vif,
2667						    IWL_MVM_SMPS_REQ_PROT,
2668						    IEEE80211_SMPS_DYNAMIC, 0);
2669			}
2670		} else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
2671			iwl_mvm_mei_host_disassociated(mvm);
2672			/*
2673			 * If update fails - SF might be running in associated
2674			 * mode while disassociated - which is forbidden.
2675			 */
2676			ret = iwl_mvm_sf_update(mvm, vif, false);
2677			WARN_ONCE(ret &&
2678				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
2679					    &mvm->status),
2680				  "Failed to update SF upon disassociation\n");
2681
2682			/*
2683			 * If we get an assert during the connection (after the
2684			 * station has been added, but before the vif is set
2685			 * to associated), mac80211 will re-add the station and
2686			 * then configure the vif. Since the vif is not
2687			 * associated, we would remove the station here and
2688			 * this would fail the recovery.
2689			 */
2690			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2691				      &mvm->status)) {
2692				/* first remove remaining keys */
2693				iwl_mvm_sec_key_remove_ap(mvm, vif,
2694							  &mvmvif->deflink, 0);
2695
2696				/*
2697				 * Remove AP station now that
2698				 * the MAC is unassoc
2699				 */
2700				ret = iwl_mvm_rm_sta_id(mvm, vif,
2701							mvmvif->deflink.ap_sta_id);
2702				if (ret)
2703					IWL_ERR(mvm,
2704						"failed to remove AP station\n");
2705
2706				mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
2707			}
2708
2709			/* remove quota for this interface */
2710			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2711			if (ret)
2712				IWL_ERR(mvm, "failed to update quotas\n");
2713
2714			/* this will take the cleared BSSID from bss_conf */
2715			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2716			if (ret)
2717				IWL_ERR(mvm,
2718					"failed to update MAC %pM (clear after unassoc)\n",
2719					vif->addr);
2720		}
2721
2722		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
2723	}
2724
2725	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
2726						changes);
2727}
2728
2729bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2730				  struct ieee80211_vif *vif,
2731				  int *ret)
2732{
2733	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2734	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2735	int i;
2736
2737	lockdep_assert_held(&mvm->mutex);
2738
2739	mvmvif->ap_assoc_sta_count = 0;
2740
2741	/* must be set before quota calculations */
2742	mvmvif->ap_ibss_active = true;
2743
2744	/* send all the early keys to the device now */
2745	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2746		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2747
2748		if (!key)
2749			continue;
2750
2751		mvmvif->ap_early_keys[i] = NULL;
2752
2753		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2754		if (*ret)
2755			return true;
2756	}
2757
2758	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2759		iwl_mvm_vif_set_low_latency(mvmvif, true,
2760					    LOW_LATENCY_VIF_TYPE);
2761		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2762	}
2763
2764	/* power updated needs to be done before quotas */
2765	iwl_mvm_power_update_mac(mvm);
2766
2767	return false;
2768}
2769
2770static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2771				 struct ieee80211_vif *vif,
2772				 struct ieee80211_bss_conf *link_conf)
2773{
2774	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2775	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2776	int ret;
2777
2778	mutex_lock(&mvm->mutex);
2779
2780	/*
2781	 * Re-calculate the tsf id, as the leader-follower relations depend on
2782	 * the beacon interval, which was not known when the AP interface
2783	 * was added.
2784	 */
2785	if (vif->type == NL80211_IFTYPE_AP)
2786		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2787
2788	/* For older devices need to send beacon template before adding mac
2789	 * context. For the newer, the beacon is a resource that belongs to a
2790	 * MAC, so need to send beacon template after adding the mac.
2791	 */
2792	if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
2793		/* Add the mac context */
2794		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2795		if (ret)
2796			goto out_unlock;
2797
2798		/* Send the beacon template */
2799		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
2800		if (ret)
2801			goto out_unlock;
2802	} else {
2803		/* Send the beacon template */
2804		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
2805		if (ret)
2806			goto out_unlock;
2807
2808		/* Add the mac context */
2809		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2810		if (ret)
2811			goto out_unlock;
2812	}
2813
2814	/* Perform the binding */
2815	ret = iwl_mvm_binding_add_vif(mvm, vif);
2816	if (ret)
2817		goto out_remove;
2818
2819	/*
2820	 * This is not very nice, but the simplest:
2821	 * For older FWs adding the mcast sta before the bcast station may
2822	 * cause assert 0x2b00.
2823	 * This is fixed in later FW so make the order of removal depend on
2824	 * the TLV
2825	 */
2826	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2827		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2828		if (ret)
2829			goto out_unbind;
2830		/*
2831		 * Send the bcast station. At this stage the TBTT and DTIM time
2832		 * events are added and applied to the scheduler
2833		 */
2834		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2835		if (ret) {
2836			iwl_mvm_rm_mcast_sta(mvm, vif);
2837			goto out_unbind;
2838		}
2839	} else {
2840		/*
2841		 * Send the bcast station. At this stage the TBTT and DTIM time
2842		 * events are added and applied to the scheduler
2843		 */
2844		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2845		if (ret)
2846			goto out_unbind;
2847		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2848		if (ret) {
2849			iwl_mvm_send_rm_bcast_sta(mvm, vif);
2850			goto out_unbind;
2851		}
2852	}
2853
2854	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
2855		goto out_failed;
2856
2857	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2858	if (ret)
2859		goto out_failed;
2860
2861	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2862	if (vif->p2p && mvm->p2p_device_vif)
2863		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2864
2865	iwl_mvm_bt_coex_vif_change(mvm);
2866
2867	/* we don't support TDLS during DCM */
2868	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2869		iwl_mvm_teardown_tdls_peers(mvm);
2870
2871	iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
2872
2873	goto out_unlock;
2874
2875out_failed:
2876	iwl_mvm_power_update_mac(mvm);
2877	mvmvif->ap_ibss_active = false;
2878	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2879	iwl_mvm_rm_mcast_sta(mvm, vif);
2880out_unbind:
2881	iwl_mvm_binding_remove_vif(mvm, vif);
2882out_remove:
2883	iwl_mvm_mac_ctxt_remove(mvm, vif);
2884out_unlock:
2885	mutex_unlock(&mvm->mutex);
2886	return ret;
2887}
2888
2889static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
2890			    struct ieee80211_vif *vif,
2891			    struct ieee80211_bss_conf *link_conf)
2892{
2893	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
2894}
2895
2896static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
2897			      struct ieee80211_vif *vif)
2898{
2899	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
2900}
2901
2902/* Common part for MLD and non-MLD ops */
2903void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
2904				 struct ieee80211_vif *vif)
2905{
2906	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2907
2908	lockdep_assert_held(&mvm->mutex);
2909
2910	iwl_mvm_prepare_mac_removal(mvm, vif);
2911
2912	/* Handle AP stop while in CSA */
2913	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2914		iwl_mvm_remove_time_event(mvm, mvmvif,
2915					  &mvmvif->time_event_data);
2916		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2917		mvmvif->csa_countdown = false;
2918	}
2919
2920	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2921		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2922		mvm->csa_tx_block_bcn_timeout = 0;
2923	}
2924
2925	mvmvif->ap_ibss_active = false;
2926	mvm->ap_last_beacon_gp2 = 0;
2927
2928	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2929		iwl_mvm_vif_set_low_latency(mvmvif, false,
2930					    LOW_LATENCY_VIF_TYPE);
2931		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
2932	}
2933
2934	iwl_mvm_bt_coex_vif_change(mvm);
2935}
2936
2937static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2938				 struct ieee80211_vif *vif,
2939				 struct ieee80211_bss_conf *link_conf)
2940{
2941	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2942
2943	mutex_lock(&mvm->mutex);
2944
2945	iwl_mvm_stop_ap_ibss_common(mvm, vif);
2946
2947	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2948	if (vif->p2p && mvm->p2p_device_vif)
2949		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2950
2951	iwl_mvm_update_quotas(mvm, false, NULL);
2952
2953	iwl_mvm_ftm_responder_clear(mvm, vif);
2954
2955	/*
2956	 * This is not very nice, but the simplest:
2957	 * For older FWs removing the mcast sta before the bcast station may
2958	 * cause assert 0x2b00.
2959	 * This is fixed in later FW (which will stop beaconing when removing
2960	 * bcast station).
2961	 * So make the order of removal depend on the TLV
2962	 */
2963	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2964		iwl_mvm_rm_mcast_sta(mvm, vif);
2965	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2966	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2967		iwl_mvm_rm_mcast_sta(mvm, vif);
2968	iwl_mvm_binding_remove_vif(mvm, vif);
2969
2970	iwl_mvm_power_update_mac(mvm);
2971
2972	iwl_mvm_mac_ctxt_remove(mvm, vif);
2973
2974	mutex_unlock(&mvm->mutex);
2975}
2976
2977static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
2978			    struct ieee80211_vif *vif,
2979			    struct ieee80211_bss_conf *link_conf)
2980{
2981	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
2982}
2983
2984static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
2985			      struct ieee80211_vif *vif)
2986{
2987	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
2988}
2989
2990static void
2991iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2992				 struct ieee80211_vif *vif,
2993				 struct ieee80211_bss_conf *bss_conf,
2994				 u64 changes)
2995{
2996	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2997
2998	/* Changes will be applied when the AP/IBSS is started */
2999	if (!mvmvif->ap_ibss_active)
3000		return;
3001
3002	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3003		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3004	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3005		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3006
3007	/* Need to send a new beacon template to the FW */
3008	if (changes & BSS_CHANGED_BEACON &&
3009	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3010		IWL_WARN(mvm, "Failed updating beacon data\n");
3011
3012	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3013		int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3014
3015		if (ret)
3016			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3017				 ret);
3018	}
3019
3020}
3021
3022static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3023				     struct ieee80211_vif *vif,
3024				     struct ieee80211_bss_conf *bss_conf,
3025				     u64 changes)
3026{
3027	static const struct iwl_mvm_bss_info_changed_ops callbacks = {
3028		.bss_info_changed_sta = iwl_mvm_bss_info_changed_station,
3029		.bss_info_changed_ap_ibss = iwl_mvm_bss_info_changed_ap_ibss,
3030	};
3031
3032	iwl_mvm_bss_info_changed_common(hw, vif, bss_conf, &callbacks,
3033					changes);
3034}
3035
3036void
3037iwl_mvm_bss_info_changed_common(struct ieee80211_hw *hw,
3038				struct ieee80211_vif *vif,
3039				struct ieee80211_bss_conf *bss_conf,
3040				const struct iwl_mvm_bss_info_changed_ops *callbacks,
3041				u64 changes)
3042{
3043	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3044
3045	mutex_lock(&mvm->mutex);
3046
3047	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3048		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3049
3050	switch (vif->type) {
3051	case NL80211_IFTYPE_STATION:
3052		callbacks->bss_info_changed_sta(mvm, vif, bss_conf, changes);
3053		break;
3054	case NL80211_IFTYPE_AP:
3055	case NL80211_IFTYPE_ADHOC:
3056		callbacks->bss_info_changed_ap_ibss(mvm, vif, bss_conf,
3057						    changes);
3058		break;
3059	case NL80211_IFTYPE_MONITOR:
3060		if (changes & BSS_CHANGED_MU_GROUPS)
3061			iwl_mvm_update_mu_groups(mvm, vif);
3062		break;
3063	default:
3064		/* shouldn't happen */
3065		WARN_ON_ONCE(1);
3066	}
3067
3068	if (changes & BSS_CHANGED_TXPOWER) {
3069		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
3070				bss_conf->txpower);
3071		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
3072	}
3073
3074	mutex_unlock(&mvm->mutex);
3075}
3076
3077int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3078			struct ieee80211_scan_request *hw_req)
3079{
3080	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3081	int ret;
3082
3083	if (hw_req->req.n_channels == 0 ||
3084	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3085		return -EINVAL;
3086
3087	mutex_lock(&mvm->mutex);
3088	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3089	mutex_unlock(&mvm->mutex);
3090
3091	return ret;
3092}
3093
3094void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3095				struct ieee80211_vif *vif)
3096{
3097	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3098
3099	mutex_lock(&mvm->mutex);
3100
3101	/* Due to a race condition, it's possible that mac80211 asks
3102	 * us to stop a hw_scan when it's already stopped.  This can
3103	 * happen, for instance, if we stopped the scan ourselves,
3104	 * called ieee80211_scan_completed() and the userspace called
3105	 * cancel scan scan before ieee80211_scan_work() could run.
3106	 * To handle that, simply return if the scan is not running.
3107	*/
3108	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3109		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3110
3111	mutex_unlock(&mvm->mutex);
3112}
3113
3114void
3115iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3116				  struct ieee80211_sta *sta, u16 tids,
3117				  int num_frames,
3118				  enum ieee80211_frame_release_type reason,
3119				  bool more_data)
3120{
3121	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3122
3123	/* Called when we need to transmit (a) frame(s) from mac80211 */
3124
3125	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3126					  tids, more_data, false);
3127}
3128
3129void
3130iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3131				    struct ieee80211_sta *sta, u16 tids,
3132				    int num_frames,
3133				    enum ieee80211_frame_release_type reason,
3134				    bool more_data)
3135{
3136	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3137
3138	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3139
3140	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3141					  tids, more_data, true);
3142}
3143
3144static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3145				     enum sta_notify_cmd cmd,
3146				     struct ieee80211_sta *sta)
3147{
3148	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3149	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3150	unsigned long txqs = 0, tids = 0;
3151	int tid;
3152
3153	/*
3154	 * If we have TVQM then we get too high queue numbers - luckily
3155	 * we really shouldn't get here with that because such hardware
3156	 * should have firmware supporting buffer station offload.
3157	 */
3158	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3159		return;
3160
3161	spin_lock_bh(&mvmsta->lock);
3162	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3163		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3164
3165		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
3166			continue;
3167
3168		__set_bit(tid_data->txq_id, &txqs);
3169
3170		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3171			continue;
3172
3173		__set_bit(tid, &tids);
3174	}
3175
3176	switch (cmd) {
3177	case STA_NOTIFY_SLEEP:
3178		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3179			ieee80211_sta_set_buffered(sta, tid, true);
3180
3181		if (txqs)
3182			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3183		/*
3184		 * The fw updates the STA to be asleep. Tx packets on the Tx
3185		 * queues to this station will not be transmitted. The fw will
3186		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3187		 */
3188		break;
3189	case STA_NOTIFY_AWAKE:
3190		if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
3191			break;
3192
3193		if (txqs)
3194			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3195		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3196		break;
3197	default:
3198		break;
3199	}
3200	spin_unlock_bh(&mvmsta->lock);
3201}
3202
3203void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3204			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3205{
3206	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
3207}
3208
3209void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
3210{
3211	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3212	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
3213	struct ieee80211_sta *sta;
3214	struct iwl_mvm_sta *mvmsta;
3215	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
3216
3217	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
3218		return;
3219
3220	rcu_read_lock();
3221	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
3222	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
3223		rcu_read_unlock();
3224		return;
3225	}
3226
3227	mvmsta = iwl_mvm_sta_from_mac80211(sta);
3228
3229	if (!mvmsta->vif ||
3230	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
3231		rcu_read_unlock();
3232		return;
3233	}
3234
3235	if (mvmsta->sleeping != sleeping) {
3236		mvmsta->sleeping = sleeping;
3237		__iwl_mvm_mac_sta_notify(mvm->hw,
3238			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
3239			sta);
3240		ieee80211_sta_ps_transition(sta, sleeping);
3241	}
3242
3243	if (sleeping) {
3244		switch (notif->type) {
3245		case IWL_MVM_PM_EVENT_AWAKE:
3246		case IWL_MVM_PM_EVENT_ASLEEP:
3247			break;
3248		case IWL_MVM_PM_EVENT_UAPSD:
3249			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
3250			break;
3251		case IWL_MVM_PM_EVENT_PS_POLL:
3252			ieee80211_sta_pspoll(sta);
3253			break;
3254		default:
3255			break;
3256		}
3257	}
3258
3259	rcu_read_unlock();
3260}
3261
3262void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3263				struct ieee80211_vif *vif,
3264				struct ieee80211_sta *sta)
3265{
3266	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3267	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3268	unsigned int link_id;
3269
3270	/*
3271	 * This is called before mac80211 does RCU synchronisation,
3272	 * so here we already invalidate our internal RCU-protected
3273	 * station pointer. The rest of the code will thus no longer
3274	 * be able to find the station this way, and we don't rely
3275	 * on further RCU synchronisation after the sta_state()
3276	 * callback deleted the station.
3277	 * Since there's mvm->mutex here, no need to have RCU lock for
3278	 * mvm_sta->link access.
3279	 */
3280	mutex_lock(&mvm->mutex);
3281	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
3282		struct iwl_mvm_link_sta *link_sta;
3283		u32 sta_id;
3284
3285		if (!mvm_sta->link[link_id])
3286			continue;
3287
3288		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
3289						     lockdep_is_held(&mvm->mutex));
3290		sta_id = link_sta->sta_id;
3291		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
3292			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
3293					 ERR_PTR(-ENOENT));
3294			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3295		}
3296	}
3297	mutex_unlock(&mvm->mutex);
3298}
3299
3300static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3301				const u8 *bssid)
3302{
3303	int i;
3304
3305	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3306		struct iwl_mvm_tcm_mac *mdata;
3307
3308		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3309		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3310		mdata->opened_rx_ba_sessions = false;
3311	}
3312
3313	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3314		return;
3315
3316	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3317		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3318		return;
3319	}
3320
3321	if (!vif->p2p &&
3322	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3323		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3324		return;
3325	}
3326
3327	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3328		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3329			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3330			return;
3331		}
3332	}
3333
3334	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3335}
3336
3337static void
3338iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
3339			   struct ieee80211_vif *vif, u8 *peer_addr,
3340			   enum nl80211_tdls_operation action)
3341{
3342	struct iwl_fw_dbg_trigger_tlv *trig;
3343	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
3344
3345	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
3346				     FW_DBG_TRIGGER_TDLS);
3347	if (!trig)
3348		return;
3349
3350	tdls_trig = (void *)trig->data;
3351
3352	if (!(tdls_trig->action_bitmap & BIT(action)))
3353		return;
3354
3355	if (tdls_trig->peer_mode &&
3356	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
3357		return;
3358
3359	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
3360				"TDLS event occurred, peer %pM, action %d",
3361				peer_addr, action);
3362}
3363
3364struct iwl_mvm_he_obss_narrow_bw_ru_data {
3365	bool tolerated;
3366};
3367
3368static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
3369						    struct cfg80211_bss *bss,
3370						    void *_data)
3371{
3372	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
3373	const struct cfg80211_bss_ies *ies;
3374	const struct element *elem;
3375
3376	rcu_read_lock();
3377	ies = rcu_dereference(bss->ies);
3378	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
3379				  ies->len);
3380
3381	if (!elem || elem->datalen < 10 ||
3382	    !(elem->data[10] &
3383	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
3384		data->tolerated = false;
3385	}
3386	rcu_read_unlock();
3387}
3388
3389static void
3390iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
3391				   struct ieee80211_vif *vif,
3392				   unsigned int link_id,
3393				   struct ieee80211_bss_conf *link_conf)
3394{
3395	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3396	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
3397		.tolerated = true,
3398	};
3399
3400	if (WARN_ON_ONCE(!link_conf->chandef.chan ||
3401			 !mvmvif->link[link_id]))
3402		return;
3403
3404	if (!(link_conf->chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
3405		mvmvif->link[link_id]->he_ru_2mhz_block = false;
3406		return;
3407	}
3408
3409	cfg80211_bss_iter(hw->wiphy, &link_conf->chandef,
3410			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
3411			  &iter_data);
3412
3413	/*
3414	 * If there is at least one AP on radar channel that cannot
3415	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
3416	 */
3417	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
3418}
3419
3420static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3421					       struct ieee80211_vif *vif)
3422{
3423	struct ieee80211_supported_band *sband;
3424	const struct ieee80211_sta_he_cap *he_cap;
3425
3426	if (vif->type != NL80211_IFTYPE_STATION)
3427		return;
3428
3429	if (!mvm->cca_40mhz_workaround)
3430		return;
3431
3432	/* decrement and check that we reached zero */
3433	mvm->cca_40mhz_workaround--;
3434	if (mvm->cca_40mhz_workaround)
3435		return;
3436
3437	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3438
3439	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3440
3441	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3442
3443	if (he_cap) {
3444		/* we know that ours is writable */
3445		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3446
3447		he->he_cap_elem.phy_cap_info[0] |=
3448			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3449	}
3450}
3451
3452static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
3453					struct ieee80211_vif *vif,
3454					struct iwl_mvm_sta *mvm_sta)
3455{
3456#if IS_ENABLED(CONFIG_IWLMEI)
3457	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3458	struct iwl_mei_conn_info conn_info = {
3459		.ssid_len = vif->cfg.ssid_len,
3460	};
3461
3462	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3463		return;
3464
3465	if (!mvm->mei_registered)
3466		return;
3467
3468	/* FIXME: MEI needs to be updated for MLO */
3469	if (!vif->bss_conf.chandef.chan)
3470		return;
3471
3472	conn_info.channel = vif->bss_conf.chandef.chan->hw_value;
3473
3474	switch (mvm_sta->pairwise_cipher) {
3475	case WLAN_CIPHER_SUITE_TKIP:
3476		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3477		break;
3478	case WLAN_CIPHER_SUITE_CCMP:
3479		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
3480		break;
3481	case WLAN_CIPHER_SUITE_GCMP:
3482		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
3483		break;
3484	case WLAN_CIPHER_SUITE_GCMP_256:
3485		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
3486		break;
3487	case 0:
3488		/* open profile */
3489		break;
3490	default:
3491		/* cipher not supported, don't send anything to iwlmei */
3492		return;
3493	}
3494
3495	switch (mvmvif->rekey_data.akm) {
3496	case WLAN_AKM_SUITE_SAE & 0xff:
3497		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
3498		break;
3499	case WLAN_AKM_SUITE_PSK & 0xff:
3500		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
3501		break;
3502	case WLAN_AKM_SUITE_8021X & 0xff:
3503		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
3504		break;
3505	case 0:
3506		/* open profile */
3507		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
3508		break;
3509	default:
3510		/* auth method / AKM not supported */
3511		/* TODO: All the FT vesions of these? */
3512		return;
3513	}
3514
3515	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
3516	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
3517
3518	/* TODO: add support for collocated AP data */
3519	iwl_mei_host_associated(&conn_info, NULL);
3520#endif
3521}
3522
3523static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
3524					    struct ieee80211_vif *vif,
3525					    bool force_assoc_off)
3526{
3527	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
3528}
3529
3530static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3531				 struct ieee80211_vif *vif,
3532				 struct ieee80211_sta *sta,
3533				 enum ieee80211_sta_state old_state,
3534				 enum ieee80211_sta_state new_state)
3535{
3536	static const struct iwl_mvm_sta_state_ops callbacks = {
3537		.add_sta = iwl_mvm_add_sta,
3538		.update_sta = iwl_mvm_update_sta,
3539		.rm_sta = iwl_mvm_rm_sta,
3540		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
3541	};
3542
3543	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
3544					    &callbacks);
3545}
3546
3547/* FIXME: temporary making two assumptions in all sta handling functions:
3548 *	(1) when setting sta state, the link exists and protected
3549 *	(2) if a link is valid in sta then it's valid in vif (can
3550 *	use same index in the link array)
3551 */
3552static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3553					   struct ieee80211_vif *vif,
3554					   struct ieee80211_sta *sta)
3555{
3556	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3557	unsigned int link_id;
3558
3559	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3560		struct ieee80211_bss_conf *conf =
3561			link_conf_dereference_check(vif, link_id);
3562		struct ieee80211_link_sta *link_sta =
3563			link_sta_dereference_check(sta, link_id);
3564
3565		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3566			continue;
3567
3568		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
3569				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3570	}
3571}
3572
3573#define IWL_MVM_MIN_BEACON_INTERVAL_TU 16
3574
3575static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
3576				      struct ieee80211_vif *vif,
3577				      struct ieee80211_sta *sta)
3578{
3579	struct ieee80211_link_sta *link_sta;
3580	unsigned int link_id;
3581
3582	/* Beacon interval check - firmware will crash if the beacon
3583	 * interval is less than 16. We can't avoid connecting at all,
3584	 * so refuse the station state change, this will cause mac80211
3585	 * to abandon attempts to connect to this AP, and eventually
3586	 * wpa_s will blocklist the AP...
3587	 */
3588
3589	for_each_sta_active_link(vif, sta, link_sta, link_id) {
3590		struct ieee80211_bss_conf *link_conf =
3591			link_conf_dereference_protected(vif, link_id);
3592
3593		if (!link_conf)
3594			continue;
3595
3596		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
3597			IWL_ERR(mvm,
3598				"Beacon interval %d for AP %pM is too small\n",
3599				link_conf->beacon_int, link_sta->addr);
3600			return false;
3601		}
3602
3603		link_conf->he_support = link_sta->he_cap.has_he;
3604	}
3605
3606	return true;
3607}
3608
3609static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
3610				       struct ieee80211_vif *vif,
3611				       struct ieee80211_sta *sta,
3612				       bool is_sta)
3613{
3614	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3615	struct ieee80211_link_sta *link_sta;
3616	unsigned int link_id;
3617
3618	for_each_sta_active_link(vif, sta, link_sta, link_id) {
3619		struct ieee80211_bss_conf *link_conf =
3620			link_conf_dereference_protected(vif, link_id);
3621
3622		if (!link_conf || !mvmvif->link[link_id])
3623			continue;
3624
3625		link_conf->he_support = link_sta->he_cap.has_he;
3626
3627		if (is_sta) {
3628			mvmvif->link[link_id]->he_ru_2mhz_block = false;
3629			if (link_sta->he_cap.has_he)
3630				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3631								   link_id,
3632								   link_conf);
3633		}
3634	}
3635}
3636
3637static int
3638iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
3639				   struct ieee80211_vif *vif,
3640				   struct ieee80211_sta *sta,
3641				   const struct iwl_mvm_sta_state_ops *callbacks)
3642{
3643	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3644	struct ieee80211_link_sta *link_sta;
3645	unsigned int i;
3646	int ret;
3647
3648	lockdep_assert_held(&mvm->mutex);
3649
3650	if (vif->type == NL80211_IFTYPE_STATION &&
3651	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
3652		return -EINVAL;
3653
3654	if (sta->tdls &&
3655	    (vif->p2p ||
3656	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
3657	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3658		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3659		return -EBUSY;
3660	}
3661
3662	ret = callbacks->add_sta(mvm, vif, sta);
3663	if (sta->tdls && ret == 0) {
3664		iwl_mvm_recalc_tdls_state(mvm, vif, true);
3665		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3666					   NL80211_TDLS_SETUP);
3667	}
3668
3669	if (ret)
3670		return ret;
3671
3672	for_each_sta_active_link(vif, sta, link_sta, i)
3673		link_sta->agg.max_rc_amsdu_len = 1;
3674
3675	ieee80211_sta_recalc_aggregates(sta);
3676
3677	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3678		mvmvif->ap_sta = sta;
3679
3680	/*
3681	 * Initialize the rates here already - this really tells
3682	 * the firmware only what the supported legacy rates are
3683	 * (may be) since it's initialized already from what the
3684	 * AP advertised in the beacon/probe response. This will
3685	 * allow the firmware to send auth/assoc frames with one
3686	 * of the supported rates already, rather than having to
3687	 * use a mandatory rate.
3688	 * If we're the AP, we'll just assume mandatory rates at
3689	 * this point, but we know nothing about the STA anyway.
3690	 */
3691	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3692
3693	return 0;
3694}
3695
3696static int
3697iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
3698				struct iwl_mvm *mvm,
3699				struct ieee80211_vif *vif,
3700				struct ieee80211_sta *sta,
3701				const struct iwl_mvm_sta_state_ops *callbacks)
3702{
3703	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3704	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3705	struct ieee80211_link_sta *link_sta;
3706	unsigned int link_id;
3707
3708	lockdep_assert_held(&mvm->mutex);
3709
3710	if (vif->type == NL80211_IFTYPE_AP) {
3711		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
3712		mvmvif->ap_assoc_sta_count++;
3713		callbacks->mac_ctxt_changed(mvm, vif, false);
3714
3715		/* since the below is not for MLD API, it's ok to use
3716		 * the default bss_conf
3717		 */
3718		if (!mvm->mld_api_is_used &&
3719		    ((vif->bss_conf.he_support &&
3720		      !iwlwifi_mod_params.disable_11ax) ||
3721		    (vif->bss_conf.eht_support &&
3722		     !iwlwifi_mod_params.disable_11be)))
3723			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
3724	} else if (vif->type == NL80211_IFTYPE_STATION) {
3725		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
3726
3727		callbacks->mac_ctxt_changed(mvm, vif, false);
3728
3729		if (!mvm->mld_api_is_used)
3730			goto out;
3731
3732		for_each_sta_active_link(vif, sta, link_sta, link_id) {
3733			struct ieee80211_bss_conf *link_conf =
3734				link_conf_dereference_protected(vif, link_id);
3735
3736			if (WARN_ON(!link_conf))
3737				return -EINVAL;
3738			if (!mvmvif->link[link_id])
3739				continue;
3740
3741			iwl_mvm_link_changed(mvm, vif, link_conf,
3742					     LINK_CONTEXT_MODIFY_ALL &
3743					     ~LINK_CONTEXT_MODIFY_ACTIVE,
3744					     true);
3745		}
3746	}
3747
3748out:
3749	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3750
3751	return callbacks->update_sta(mvm, vif, sta);
3752}
3753
3754static int
3755iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
3756				      struct ieee80211_vif *vif,
3757				      struct ieee80211_sta *sta,
3758				      const struct iwl_mvm_sta_state_ops *callbacks)
3759{
3760	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3761	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3762
3763	lockdep_assert_held(&mvm->mutex);
3764
3765	/* we don't support TDLS during DCM */
3766	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3767		iwl_mvm_teardown_tdls_peers(mvm);
3768
3769	if (sta->tdls) {
3770		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3771					   NL80211_TDLS_ENABLE_LINK);
3772	} else {
3773		/* enable beacon filtering */
3774		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
3775
3776		mvmvif->authorized = 1;
3777
3778		callbacks->mac_ctxt_changed(mvm, vif, false);
3779		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
3780	}
3781
3782	mvm_sta->authorized = true;
3783
3784	/* MFP is set by default before the station is authorized.
3785	 * Clear it here in case it's not used.
3786	 */
3787	if (!sta->mfp) {
3788		int ret = callbacks->update_sta(mvm, vif, sta);
3789
3790		if (ret)
3791			return ret;
3792	}
3793
3794	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3795
3796	return 0;
3797}
3798
3799static int
3800iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
3801				      struct ieee80211_vif *vif,
3802				      struct ieee80211_sta *sta,
3803				      const struct iwl_mvm_sta_state_ops *callbacks)
3804{
3805	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3806	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3807
3808	lockdep_assert_held(&mvm->mutex);
3809
3810	mvmsta->authorized = false;
3811
3812	/* once we move into assoc state, need to update rate scale to
3813	 * disable using wide bandwidth
3814	 */
3815	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3816
3817	if (!sta->tdls) {
3818		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
3819		 * yet to avoid sending high prio again for a little
3820		 * time.
3821		 */
3822		mvmvif->authorized = 0;
3823
3824		/* disable beacon filtering */
3825		iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3826	}
3827
3828	return 0;
3829}
3830
3831/* Common part for MLD and non-MLD modes */
3832int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
3833				 struct ieee80211_vif *vif,
3834				 struct ieee80211_sta *sta,
3835				 enum ieee80211_sta_state old_state,
3836				 enum ieee80211_sta_state new_state,
3837				 const struct iwl_mvm_sta_state_ops *callbacks)
3838{
3839	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3840	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3841	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3842	struct ieee80211_link_sta *link_sta;
3843	unsigned int link_id;
3844	int ret;
3845
3846	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3847			   sta->addr, old_state, new_state);
3848
3849	/*
3850	 * If we are in a STA removal flow and in DQA mode:
3851	 *
3852	 * This is after the sync_rcu part, so the queues have already been
3853	 * flushed. No more TXs on their way in mac80211's path, and no more in
3854	 * the queues.
3855	 * Also, we won't be getting any new TX frames for this station.
3856	 * What we might have are deferred TX frames that need to be taken care
3857	 * of.
3858	 *
3859	 * Drop any still-queued deferred-frame before removing the STA, and
3860	 * make sure the worker is no longer handling frames for this STA.
3861	 */
3862	if (old_state == IEEE80211_STA_NONE &&
3863	    new_state == IEEE80211_STA_NOTEXIST) {
3864		flush_work(&mvm->add_stream_wk);
3865
3866		/*
3867		 * No need to make sure deferred TX indication is off since the
3868		 * worker will already remove it if it was on
3869		 */
3870
3871		/*
3872		 * Additionally, reset the 40 MHz capability if we disconnected
3873		 * from the AP now.
3874		 */
3875		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
3876
3877		/* Also free dup data just in case any assertions below fail */
3878		kfree(mvm_sta->dup_data);
3879	}
3880
3881	mutex_lock(&mvm->mutex);
3882
3883	/* this would be a mac80211 bug ... but don't crash */
3884	for_each_sta_active_link(vif, sta, link_sta, link_id) {
3885		if (WARN_ON_ONCE(!mvmvif->link[link_id]->phy_ctxt)) {
3886			mutex_unlock(&mvm->mutex);
3887			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
3888					&mvm->status) ? 0 : -EINVAL;
3889		}
3890	}
3891
3892	/* track whether or not the station is associated */
3893	mvm_sta->sta_state = new_state;
3894
3895	if (old_state == IEEE80211_STA_NOTEXIST &&
3896	    new_state == IEEE80211_STA_NONE) {
3897		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
3898							 callbacks);
3899		if (ret < 0)
3900			goto out_unlock;
3901	} else if (old_state == IEEE80211_STA_NONE &&
3902		   new_state == IEEE80211_STA_AUTH) {
3903		/*
3904		 * EBS may be disabled due to previous failures reported by FW.
3905		 * Reset EBS status here assuming environment has been changed.
3906		 */
3907		mvm->last_ebs_successful = true;
3908		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3909		ret = 0;
3910	} else if (old_state == IEEE80211_STA_AUTH &&
3911		   new_state == IEEE80211_STA_ASSOC) {
3912		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
3913						      callbacks);
3914	} else if (old_state == IEEE80211_STA_ASSOC &&
3915		   new_state == IEEE80211_STA_AUTHORIZED) {
3916		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
3917							    callbacks);
3918	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3919		   new_state == IEEE80211_STA_ASSOC) {
3920		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
3921							    callbacks);
3922	} else if (old_state == IEEE80211_STA_ASSOC &&
3923		   new_state == IEEE80211_STA_AUTH) {
3924		if (vif->type == NL80211_IFTYPE_AP) {
3925			mvmvif->ap_assoc_sta_count--;
3926			callbacks->mac_ctxt_changed(mvm, vif, false);
3927		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3928			iwl_mvm_stop_session_protection(mvm, vif);
3929		ret = 0;
3930	} else if (old_state == IEEE80211_STA_AUTH &&
3931		   new_state == IEEE80211_STA_NONE) {
3932		ret = 0;
3933	} else if (old_state == IEEE80211_STA_NONE &&
3934		   new_state == IEEE80211_STA_NOTEXIST) {
3935		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
3936			iwl_mvm_stop_session_protection(mvm, vif);
3937			mvmvif->ap_sta = NULL;
3938		}
3939		ret = callbacks->rm_sta(mvm, vif, sta);
3940		if (sta->tdls) {
3941			iwl_mvm_recalc_tdls_state(mvm, vif, false);
3942			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3943						   NL80211_TDLS_DISABLE_LINK);
3944		}
3945
3946		if (unlikely(ret &&
3947			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
3948				      &mvm->status)))
3949			ret = 0;
3950	} else {
3951		ret = -EIO;
3952	}
3953 out_unlock:
3954	mutex_unlock(&mvm->mutex);
3955
3956	if (sta->tdls && ret == 0) {
3957		if (old_state == IEEE80211_STA_NOTEXIST &&
3958		    new_state == IEEE80211_STA_NONE)
3959			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3960		else if (old_state == IEEE80211_STA_NONE &&
3961			 new_state == IEEE80211_STA_NOTEXIST)
3962			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3963	}
3964
3965	return ret;
3966}
3967
3968int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3969{
3970	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3971
3972	mvm->rts_threshold = value;
3973
3974	return 0;
3975}
3976
3977void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3978			   struct ieee80211_sta *sta, u32 changed)
3979{
3980	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3981
3982	if (changed & (IEEE80211_RC_BW_CHANGED |
3983		       IEEE80211_RC_SUPP_RATES_CHANGED |
3984		       IEEE80211_RC_NSS_CHANGED))
3985		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3986
3987	if (vif->type == NL80211_IFTYPE_STATION &&
3988	    changed & IEEE80211_RC_NSS_CHANGED)
3989		iwl_mvm_sf_update(mvm, vif, false);
3990}
3991
3992static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3993			       struct ieee80211_vif *vif,
3994			       unsigned int link_id, u16 ac,
3995			       const struct ieee80211_tx_queue_params *params)
3996{
3997	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3998	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3999
4000	mvmvif->deflink.queue_params[ac] = *params;
4001
4002	/*
4003	 * No need to update right away, we'll get BSS_CHANGED_QOS
4004	 * The exception is P2P_DEVICE interface which needs immediate update.
4005	 */
4006	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
4007		int ret;
4008
4009		mutex_lock(&mvm->mutex);
4010		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4011		mutex_unlock(&mvm->mutex);
4012		return ret;
4013	}
4014	return 0;
4015}
4016
4017void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4018				struct ieee80211_vif *vif,
4019				struct ieee80211_prep_tx_info *info)
4020{
4021	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4022
4023	mutex_lock(&mvm->mutex);
4024	iwl_mvm_protect_assoc(mvm, vif, info->duration);
4025	mutex_unlock(&mvm->mutex);
4026}
4027
4028void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
4029				 struct ieee80211_vif *vif,
4030				 struct ieee80211_prep_tx_info *info)
4031{
4032	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4033
4034	/* for successful cases (auth/assoc), don't cancel session protection */
4035	if (info->success)
4036		return;
4037
4038	mutex_lock(&mvm->mutex);
4039	iwl_mvm_stop_session_protection(mvm, vif);
4040	mutex_unlock(&mvm->mutex);
4041}
4042
4043int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4044				 struct ieee80211_vif *vif,
4045				 struct cfg80211_sched_scan_request *req,
4046				 struct ieee80211_scan_ies *ies)
4047{
4048	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4049
4050	int ret;
4051
4052	mutex_lock(&mvm->mutex);
4053
4054	if (!vif->cfg.idle) {
4055		ret = -EBUSY;
4056		goto out;
4057	}
4058
4059	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4060
4061out:
4062	mutex_unlock(&mvm->mutex);
4063	return ret;
4064}
4065
4066int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4067				struct ieee80211_vif *vif)
4068{
4069	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4070	int ret;
4071
4072	mutex_lock(&mvm->mutex);
4073
4074	/* Due to a race condition, it's possible that mac80211 asks
4075	 * us to stop a sched_scan when it's already stopped.  This
4076	 * can happen, for instance, if we stopped the scan ourselves,
4077	 * called ieee80211_sched_scan_stopped() and the userspace called
4078	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4079	 * could run.  To handle this, simply return if the scan is
4080	 * not running.
4081	*/
4082	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4083		mutex_unlock(&mvm->mutex);
4084		return 0;
4085	}
4086
4087	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4088	mutex_unlock(&mvm->mutex);
4089	iwl_mvm_wait_for_async_handlers(mvm);
4090
4091	return ret;
4092}
4093
4094static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4095				 enum set_key_cmd cmd,
4096				 struct ieee80211_vif *vif,
4097				 struct ieee80211_sta *sta,
4098				 struct ieee80211_key_conf *key)
4099{
4100	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4101	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4102	struct iwl_mvm_sta *mvmsta = NULL;
4103	struct iwl_mvm_key_pn *ptk_pn = NULL;
4104	int keyidx = key->keyidx;
4105	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
4106	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4107	int ret, i;
4108	u8 key_offset;
4109
4110	if (sta)
4111		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4112
4113	switch (key->cipher) {
4114	case WLAN_CIPHER_SUITE_TKIP:
4115		if (!mvm->trans->trans_cfg->gen2) {
4116			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
4117			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4118		} else if (vif->type == NL80211_IFTYPE_STATION) {
4119			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
4120		} else {
4121			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
4122			return -EOPNOTSUPP;
4123		}
4124		break;
4125	case WLAN_CIPHER_SUITE_CCMP:
4126	case WLAN_CIPHER_SUITE_GCMP:
4127	case WLAN_CIPHER_SUITE_GCMP_256:
4128		if (!iwl_mvm_has_new_tx_api(mvm))
4129			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4130		break;
4131	case WLAN_CIPHER_SUITE_AES_CMAC:
4132	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
4133	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4134		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4135		break;
4136	case WLAN_CIPHER_SUITE_WEP40:
4137	case WLAN_CIPHER_SUITE_WEP104:
4138		if (vif->type == NL80211_IFTYPE_STATION)
4139			break;
4140		if (iwl_mvm_has_new_tx_api(mvm))
4141			return -EOPNOTSUPP;
4142		/* support HW crypto on TX */
4143		return 0;
4144	default:
4145		return -EOPNOTSUPP;
4146	}
4147
4148	switch (cmd) {
4149	case SET_KEY:
4150		if (vif->type == NL80211_IFTYPE_STATION &&
4151		    (keyidx == 6 || keyidx == 7))
4152			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4153					   key);
4154
4155		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4156		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4157			/*
4158			 * GTK on AP interface is a TX-only key, return 0;
4159			 * on IBSS they're per-station and because we're lazy
4160			 * we don't support them for RX, so do the same.
4161			 * CMAC/GMAC in AP/IBSS modes must be done in software.
4162			 *
4163			 * Except, of course, beacon protection - it must be
4164			 * offloaded since we just set a beacon template.
4165			 */
4166			if (keyidx < 6 &&
4167			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
4168			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4169			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
4170				ret = -EOPNOTSUPP;
4171				break;
4172			}
4173
4174			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
4175			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
4176			    !iwl_mvm_has_new_tx_api(mvm)) {
4177				key->hw_key_idx = STA_KEY_IDX_INVALID;
4178				ret = 0;
4179				break;
4180			}
4181
4182			if (!mvmvif->ap_ibss_active) {
4183				for (i = 0;
4184				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4185				     i++) {
4186					if (!mvmvif->ap_early_keys[i]) {
4187						mvmvif->ap_early_keys[i] = key;
4188						break;
4189					}
4190				}
4191
4192				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4193					ret = -ENOSPC;
4194				else
4195					ret = 0;
4196
4197				break;
4198			}
4199		}
4200
4201		/* During FW restart, in order to restore the state as it was,
4202		 * don't try to reprogram keys we previously failed for.
4203		 */
4204		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4205		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4206			IWL_DEBUG_MAC80211(mvm,
4207					   "skip invalid idx key programming during restart\n");
4208			ret = 0;
4209			break;
4210		}
4211
4212		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4213		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4214		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4215		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4216		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4217		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4218			struct ieee80211_key_seq seq;
4219			int tid, q;
4220
4221			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4222			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4223						     mvm->trans->num_rx_queues),
4224					 GFP_KERNEL);
4225			if (!ptk_pn) {
4226				ret = -ENOMEM;
4227				break;
4228			}
4229
4230			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4231				ieee80211_get_key_rx_seq(key, tid, &seq);
4232				for (q = 0; q < mvm->trans->num_rx_queues; q++)
4233					memcpy(ptk_pn->q[q].pn[tid],
4234					       seq.ccmp.pn,
4235					       IEEE80211_CCMP_PN_LEN);
4236			}
4237
4238			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4239		}
4240
4241		/* in HW restart reuse the index, otherwise request a new one */
4242		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4243			key_offset = key->hw_key_idx;
4244		else
4245			key_offset = STA_KEY_IDX_INVALID;
4246
4247		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4248			mvmsta->pairwise_cipher = key->cipher;
4249
4250		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4251				   sta ? sta->addr : NULL, key->keyidx);
4252
4253		if (sec_key_ver)
4254			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
4255		else
4256			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
4257
4258		if (ret) {
4259			IWL_WARN(mvm, "set key failed\n");
4260			key->hw_key_idx = STA_KEY_IDX_INVALID;
4261			if (ptk_pn) {
4262				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4263				kfree(ptk_pn);
4264			}
4265			/*
4266			 * can't add key for RX, but we don't need it
4267			 * in the device for TX so still return 0,
4268			 * unless we have new TX API where we cannot
4269			 * put key material into the TX_CMD
4270			 */
4271			if (iwl_mvm_has_new_tx_api(mvm))
4272				ret = -EOPNOTSUPP;
4273			else
4274				ret = 0;
4275		}
4276
4277		break;
4278	case DISABLE_KEY:
4279		if (vif->type == NL80211_IFTYPE_STATION &&
4280		    (keyidx == 6 || keyidx == 7))
4281			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4282					 NULL);
4283
4284		ret = -ENOENT;
4285		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4286			if (mvmvif->ap_early_keys[i] == key) {
4287				mvmvif->ap_early_keys[i] = NULL;
4288				ret = 0;
4289			}
4290		}
4291
4292		/* found in pending list - don't do anything else */
4293		if (ret == 0)
4294			break;
4295
4296		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4297			ret = 0;
4298			break;
4299		}
4300
4301		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4302		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4303		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4304		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4305		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4306			ptk_pn = rcu_dereference_protected(
4307						mvmsta->ptk_pn[keyidx],
4308						lockdep_is_held(&mvm->mutex));
4309			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4310			if (ptk_pn)
4311				kfree_rcu(ptk_pn, rcu_head);
4312		}
4313
4314		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
4315		if (sec_key_ver)
4316			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
4317		else
4318			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4319		break;
4320	default:
4321		ret = -EINVAL;
4322	}
4323
4324	return ret;
4325}
4326
4327int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4328			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4329			struct ieee80211_key_conf *key)
4330{
4331	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4332	int ret;
4333
4334	mutex_lock(&mvm->mutex);
4335	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4336	mutex_unlock(&mvm->mutex);
4337
4338	return ret;
4339}
4340
4341void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4342				 struct ieee80211_vif *vif,
4343				 struct ieee80211_key_conf *keyconf,
4344				 struct ieee80211_sta *sta,
4345				 u32 iv32, u16 *phase1key)
4346{
4347	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4348
4349	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4350		return;
4351
4352	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4353}
4354
4355
4356static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4357			       struct iwl_rx_packet *pkt, void *data)
4358{
4359	struct iwl_mvm *mvm =
4360		container_of(notif_wait, struct iwl_mvm, notif_wait);
4361	struct iwl_hs20_roc_res *resp;
4362	int resp_len = iwl_rx_packet_payload_len(pkt);
4363	struct iwl_mvm_time_event_data *te_data = data;
4364
4365	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4366		return true;
4367
4368	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4369		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4370		return true;
4371	}
4372
4373	resp = (void *)pkt->data;
4374
4375	IWL_DEBUG_TE(mvm,
4376		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4377		     resp->status, resp->event_unique_id);
4378
4379	te_data->uid = le32_to_cpu(resp->event_unique_id);
4380	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4381		     te_data->uid);
4382
4383	spin_lock_bh(&mvm->time_event_lock);
4384	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4385	spin_unlock_bh(&mvm->time_event_lock);
4386
4387	return true;
4388}
4389
4390#define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
4391#define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
4392#define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
4393#define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
4394#define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
4395static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4396				    struct ieee80211_channel *channel,
4397				    struct ieee80211_vif *vif,
4398				    int duration)
4399{
4400	int res;
4401	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4402	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4403	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4404	struct iwl_notification_wait wait_time_event;
4405	u32 dtim_interval = vif->bss_conf.dtim_period *
4406		vif->bss_conf.beacon_int;
4407	u32 req_dur, delay;
4408	struct iwl_hs20_roc_req aux_roc_req = {
4409		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4410		.id_and_color =
4411			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4412		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4413	};
4414	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
4415		&aux_roc_req.channel_info);
4416	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
4417
4418	/* Set the channel info data */
4419	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
4420			      iwl_mvm_phy_band_from_nl80211(channel->band),
4421			      IWL_PHY_CHANNEL_MODE20,
4422			      0);
4423
4424	/* Set the time and duration */
4425	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4426
4427	delay = AUX_ROC_MIN_DELAY;
4428	req_dur = MSEC_TO_TU(duration);
4429
4430	/*
4431	 * If we are associated we want the delay time to be at least one
4432	 * dtim interval so that the FW can wait until after the DTIM and
4433	 * then start the time event, this will potentially allow us to
4434	 * remain off-channel for the max duration.
4435	 * Since we want to use almost a whole dtim interval we would also
4436	 * like the delay to be for 2-3 dtim intervals, in case there are
4437	 * other time events with higher priority.
4438	 */
4439	if (vif->cfg.assoc) {
4440		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
4441		/* We cannot remain off-channel longer than the DTIM interval */
4442		if (dtim_interval <= req_dur) {
4443			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
4444			if (req_dur <= AUX_ROC_MIN_DURATION)
4445				req_dur = dtim_interval -
4446					AUX_ROC_MIN_SAFETY_BUFFER;
4447		}
4448	}
4449
4450	tail->duration = cpu_to_le32(req_dur);
4451	tail->apply_time_max_delay = cpu_to_le32(delay);
4452
4453	IWL_DEBUG_TE(mvm,
4454		     "ROC: Requesting to remain on channel %u for %ums\n",
4455		     channel->hw_value, req_dur);
4456	IWL_DEBUG_TE(mvm,
4457		     "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
4458		     duration, delay, dtim_interval);
4459
4460	/* Set the node address */
4461	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4462
4463	lockdep_assert_held(&mvm->mutex);
4464
4465	spin_lock_bh(&mvm->time_event_lock);
4466
4467	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4468		spin_unlock_bh(&mvm->time_event_lock);
4469		return -EIO;
4470	}
4471
4472	te_data->vif = vif;
4473	te_data->duration = duration;
4474	te_data->id = HOT_SPOT_CMD;
4475
4476	spin_unlock_bh(&mvm->time_event_lock);
4477
4478	/*
4479	 * Use a notification wait, which really just processes the
4480	 * command response and doesn't wait for anything, in order
4481	 * to be able to process the response and get the UID inside
4482	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4483	 * stores the buffer and then wakes up this thread, by which
4484	 * time another notification (that the time event started)
4485	 * might already be processed unsuccessfully.
4486	 */
4487	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4488				   time_event_response,
4489				   ARRAY_SIZE(time_event_response),
4490				   iwl_mvm_rx_aux_roc, te_data);
4491
4492	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4493				   &aux_roc_req);
4494
4495	if (res) {
4496		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4497		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4498		goto out_clear_te;
4499	}
4500
4501	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4502	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4503	/* should never fail */
4504	WARN_ON_ONCE(res);
4505
4506	if (res) {
4507 out_clear_te:
4508		spin_lock_bh(&mvm->time_event_lock);
4509		iwl_mvm_te_clear_data(mvm, te_data);
4510		spin_unlock_bh(&mvm->time_event_lock);
4511	}
4512
4513	return res;
4514}
4515
4516static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4517{
4518	int ret = 0;
4519
4520	lockdep_assert_held(&mvm->mutex);
4521
4522	if (!fw_has_capa(&mvm->fw->ucode_capa,
4523			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4524		IWL_ERR(mvm, "hotspot not supported\n");
4525		return -EINVAL;
4526	}
4527
4528	if (iwl_mvm_has_new_station_api(mvm->fw)) {
4529		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4530		WARN(ret, "Failed to allocate aux station");
4531	}
4532
4533	return ret;
4534}
4535
4536static int iwl_mvm_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4537{
4538	int ret;
4539
4540	lockdep_assert_held(&mvm->mutex);
4541
4542	ret = iwl_mvm_binding_add_vif(mvm, vif);
4543	if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4544		return ret;
4545
4546	/* The station and queue allocation must be done only after the binding
4547	 * is done, as otherwise the FW might incorrectly configure its state.
4548	 */
4549	return iwl_mvm_add_p2p_bcast_sta(mvm, vif);
4550}
4551
4552static int iwl_mvm_roc(struct ieee80211_hw *hw,
4553		       struct ieee80211_vif *vif,
4554		       struct ieee80211_channel *channel,
4555		       int duration,
4556		       enum ieee80211_roc_type type)
4557{
4558	static const struct iwl_mvm_roc_ops ops = {
4559		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
4560		.link = iwl_mvm_roc_link,
4561	};
4562
4563	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4564}
4565
4566/* Execute the common part for MLD and non-MLD modes */
4567int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4568		       struct ieee80211_channel *channel, int duration,
4569		       enum ieee80211_roc_type type,
4570		       const struct iwl_mvm_roc_ops *ops)
4571{
4572	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4573	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4574	struct cfg80211_chan_def chandef;
4575	struct iwl_mvm_phy_ctxt *phy_ctxt;
4576	int ret, i;
4577	u32 lmac_id;
4578
4579	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4580			   duration, type);
4581
4582	/*
4583	 * Flush the done work, just in case it's still pending, so that
4584	 * the work it does can complete and we can accept new frames.
4585	 */
4586	flush_work(&mvm->roc_done_wk);
4587
4588	mutex_lock(&mvm->mutex);
4589
4590	switch (vif->type) {
4591	case NL80211_IFTYPE_STATION:
4592		lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
4593
4594		/* Use aux roc framework (HS20) */
4595		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4596		if (!ret)
4597			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
4598						       vif, duration);
4599		goto out_unlock;
4600	case NL80211_IFTYPE_P2P_DEVICE:
4601		/* handle below */
4602		break;
4603	default:
4604		IWL_ERR(mvm, "ROC: Invalid vif type=%u\n", vif->type);
4605		ret = -EINVAL;
4606		goto out_unlock;
4607	}
4608
4609	/* Try using a PHY context that is already in use */
4610	for (i = 0; i < NUM_PHY_CTX; i++) {
4611		phy_ctxt = &mvm->phy_ctxts[i];
4612		if (!phy_ctxt->ref || mvmvif->deflink.phy_ctxt == phy_ctxt)
4613			continue;
4614
4615		if (channel == phy_ctxt->channel) {
4616			if (mvmvif->deflink.phy_ctxt)
4617				iwl_mvm_phy_ctxt_unref(mvm,
4618						       mvmvif->deflink.phy_ctxt);
4619
4620			mvmvif->deflink.phy_ctxt = phy_ctxt;
4621			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4622			goto link_and_start_p2p_roc;
4623		}
4624	}
4625
4626	/* If the currently used PHY context is configured with a matching
4627	 * channel use it
4628	 */
4629	if (mvmvif->deflink.phy_ctxt) {
4630		if (channel == mvmvif->deflink.phy_ctxt->channel)
4631			goto link_and_start_p2p_roc;
4632	} else {
4633		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4634		if (!phy_ctxt) {
4635			ret = -ENOSPC;
4636			goto out_unlock;
4637		}
4638
4639		mvmvif->deflink.phy_ctxt = phy_ctxt;
4640		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4641	}
4642
4643	/* Configure the PHY context */
4644	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4645
4646	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
4647				       1, 1);
4648	if (ret) {
4649		IWL_ERR(mvm, "Failed to change PHY context\n");
4650		goto out_unlock;
4651	}
4652
4653link_and_start_p2p_roc:
4654	ret = ops->link(mvm, vif);
4655	if (ret)
4656		goto out_unlock;
4657
4658	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4659out_unlock:
4660	mutex_unlock(&mvm->mutex);
4661	IWL_DEBUG_MAC80211(mvm, "leave\n");
4662	return ret;
4663}
4664
4665int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
4666		       struct ieee80211_vif *vif)
4667{
4668	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4669
4670	IWL_DEBUG_MAC80211(mvm, "enter\n");
4671
4672	mutex_lock(&mvm->mutex);
4673	iwl_mvm_stop_roc(mvm, vif);
4674	mutex_unlock(&mvm->mutex);
4675
4676	IWL_DEBUG_MAC80211(mvm, "leave\n");
4677	return 0;
4678}
4679
4680struct iwl_mvm_ftm_responder_iter_data {
4681	bool responder;
4682	struct ieee80211_chanctx_conf *ctx;
4683};
4684
4685static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4686					       struct ieee80211_vif *vif)
4687{
4688	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4689
4690	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4691	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4692		data->responder = true;
4693}
4694
4695static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4696					     struct ieee80211_chanctx_conf *ctx)
4697{
4698	struct iwl_mvm_ftm_responder_iter_data data = {
4699		.responder = false,
4700		.ctx = ctx,
4701	};
4702
4703	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4704					IEEE80211_IFACE_ITER_NORMAL,
4705					iwl_mvm_ftm_responder_chanctx_iter,
4706					&data);
4707	return data.responder;
4708}
4709
4710static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4711				 struct ieee80211_chanctx_conf *ctx)
4712{
4713	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4714	struct iwl_mvm_phy_ctxt *phy_ctxt;
4715	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4716	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4717	int ret;
4718
4719	lockdep_assert_held(&mvm->mutex);
4720
4721	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4722
4723	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4724	if (!phy_ctxt) {
4725		ret = -ENOSPC;
4726		goto out;
4727	}
4728
4729	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4730				       ctx->rx_chains_static,
4731				       ctx->rx_chains_dynamic);
4732	if (ret) {
4733		IWL_ERR(mvm, "Failed to add PHY context\n");
4734		goto out;
4735	}
4736
4737	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4738	*phy_ctxt_id = phy_ctxt->id;
4739out:
4740	return ret;
4741}
4742
4743int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4744			struct ieee80211_chanctx_conf *ctx)
4745{
4746	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4747	int ret;
4748
4749	mutex_lock(&mvm->mutex);
4750	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4751	mutex_unlock(&mvm->mutex);
4752
4753	return ret;
4754}
4755
4756static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4757				     struct ieee80211_chanctx_conf *ctx)
4758{
4759	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4760	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4761
4762	lockdep_assert_held(&mvm->mutex);
4763
4764	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4765}
4766
4767void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4768			    struct ieee80211_chanctx_conf *ctx)
4769{
4770	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4771
4772	mutex_lock(&mvm->mutex);
4773	__iwl_mvm_remove_chanctx(mvm, ctx);
4774	mutex_unlock(&mvm->mutex);
4775}
4776
4777void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4778			    struct ieee80211_chanctx_conf *ctx, u32 changed)
4779{
4780	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4781	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4782	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4783	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4784	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4785
4786	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4787		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4788				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4789				   IEEE80211_CHANCTX_CHANGE_RADAR |
4790				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4791		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4792		      phy_ctxt->ref, changed))
4793		return;
4794
4795	mutex_lock(&mvm->mutex);
4796
4797	/* we are only changing the min_width, may be a noop */
4798	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4799		if (phy_ctxt->width == def->width)
4800			goto out_unlock;
4801
4802		/* we are just toggling between 20_NOHT and 20 */
4803		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4804		    def->width <= NL80211_CHAN_WIDTH_20)
4805			goto out_unlock;
4806	}
4807
4808	iwl_mvm_bt_coex_vif_change(mvm);
4809	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4810				 ctx->rx_chains_static,
4811				 ctx->rx_chains_dynamic);
4812
4813out_unlock:
4814	mutex_unlock(&mvm->mutex);
4815}
4816
4817/*
4818 * This function executes the common part for MLD and non-MLD modes.
4819 *
4820 * Returns true if we're done assigning the chanctx
4821 * (either on failure or success)
4822 */
4823static bool
4824__iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
4825				    struct ieee80211_vif *vif,
4826				    struct ieee80211_chanctx_conf *ctx,
4827				    bool switching_chanctx, int *ret)
4828{
4829	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4830	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4831	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4832
4833	lockdep_assert_held(&mvm->mutex);
4834
4835	mvmvif->deflink.phy_ctxt = phy_ctxt;
4836
4837	switch (vif->type) {
4838	case NL80211_IFTYPE_AP:
4839		/* only needed if we're switching chanctx (i.e. during CSA) */
4840		if (switching_chanctx) {
4841			mvmvif->ap_ibss_active = true;
4842			break;
4843		}
4844		fallthrough;
4845	case NL80211_IFTYPE_ADHOC:
4846		/*
4847		 * The AP binding flow is handled as part of the start_ap flow
4848		 * (in bss_info_changed), similarly for IBSS.
4849		 */
4850		*ret = 0;
4851		return true;
4852	case NL80211_IFTYPE_STATION:
4853		break;
4854	case NL80211_IFTYPE_MONITOR:
4855		/* always disable PS when a monitor interface is active */
4856		mvmvif->ps_disabled = true;
4857		break;
4858	default:
4859		*ret = -EINVAL;
4860		return true;
4861	}
4862	return false;
4863}
4864
4865static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4866					struct ieee80211_vif *vif,
4867					struct ieee80211_bss_conf *link_conf,
4868					struct ieee80211_chanctx_conf *ctx,
4869					bool switching_chanctx)
4870{
4871	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4872	int ret;
4873
4874	if (WARN_ON(!link_conf))
4875		return -EINVAL;
4876
4877	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
4878						switching_chanctx, &ret))
4879		goto out;
4880
4881	ret = iwl_mvm_binding_add_vif(mvm, vif);
4882	if (ret)
4883		goto out;
4884
4885	/*
4886	 * Power state must be updated before quotas,
4887	 * otherwise fw will complain.
4888	 */
4889	iwl_mvm_power_update_mac(mvm);
4890
4891	/* Setting the quota at this stage is only required for monitor
4892	 * interfaces. For the other types, the bss_info changed flow
4893	 * will handle quota settings.
4894	 */
4895	if (vif->type == NL80211_IFTYPE_MONITOR) {
4896		mvmvif->monitor_active = true;
4897		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4898		if (ret)
4899			goto out_remove_binding;
4900
4901		ret = iwl_mvm_add_snif_sta(mvm, vif);
4902		if (ret)
4903			goto out_remove_binding;
4904
4905	}
4906
4907	/* Handle binding during CSA */
4908	if (vif->type == NL80211_IFTYPE_AP) {
4909		iwl_mvm_update_quotas(mvm, false, NULL);
4910		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4911	}
4912
4913	if (vif->type == NL80211_IFTYPE_STATION) {
4914		if (!switching_chanctx) {
4915			mvmvif->csa_bcn_pending = false;
4916			goto out;
4917		}
4918
4919		mvmvif->csa_bcn_pending = true;
4920
4921		if (!fw_has_capa(&mvm->fw->ucode_capa,
4922				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
4923			u32 duration = 3 * vif->bss_conf.beacon_int;
4924
4925			/* Protect the session to make sure we hear the first
4926			 * beacon on the new channel.
4927			 */
4928			iwl_mvm_protect_session(mvm, vif, duration, duration,
4929						vif->bss_conf.beacon_int / 2,
4930						true);
4931		}
4932
4933		iwl_mvm_update_quotas(mvm, false, NULL);
4934	}
4935
4936	goto out;
4937
4938out_remove_binding:
4939	iwl_mvm_binding_remove_vif(mvm, vif);
4940	iwl_mvm_power_update_mac(mvm);
4941out:
4942	if (ret)
4943		mvmvif->deflink.phy_ctxt = NULL;
4944	return ret;
4945}
4946
4947static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4948				      struct ieee80211_vif *vif,
4949				      struct ieee80211_bss_conf *link_conf,
4950				      struct ieee80211_chanctx_conf *ctx)
4951{
4952	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4953	int ret;
4954
4955	mutex_lock(&mvm->mutex);
4956	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
4957	mutex_unlock(&mvm->mutex);
4958
4959	return ret;
4960}
4961
4962/*
4963 * This function executes the common part for MLD and non-MLD modes.
4964 *
4965 * Returns if chanctx unassign chanctx is done
4966 * (either on failure or success)
4967 */
4968static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
4969						  struct ieee80211_vif *vif,
4970						  bool switching_chanctx)
4971{
4972	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4973
4974	lockdep_assert_held(&mvm->mutex);
4975	iwl_mvm_remove_time_event(mvm, mvmvif,
4976				  &mvmvif->time_event_data);
4977
4978	switch (vif->type) {
4979	case NL80211_IFTYPE_ADHOC:
4980		return true;
4981	case NL80211_IFTYPE_MONITOR:
4982		mvmvif->monitor_active = false;
4983		mvmvif->ps_disabled = false;
4984		break;
4985	case NL80211_IFTYPE_AP:
4986		/* This part is triggered only during CSA */
4987		if (!switching_chanctx || !mvmvif->ap_ibss_active)
4988			return true;
4989
4990		mvmvif->csa_countdown = false;
4991
4992		/* Set CS bit on all the stations */
4993		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4994
4995		/* Save blocked iface, the timeout is set on the next beacon */
4996		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4997
4998		mvmvif->ap_ibss_active = false;
4999		break;
5000	default:
5001		break;
5002	}
5003	return false;
5004}
5005
5006static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5007					   struct ieee80211_vif *vif,
5008					   struct ieee80211_bss_conf *link_conf,
5009					   struct ieee80211_chanctx_conf *ctx,
5010					   bool switching_chanctx)
5011{
5012	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5013	struct ieee80211_vif *disabled_vif = NULL;
5014
5015	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5016		goto out;
5017
5018	if (vif->type == NL80211_IFTYPE_MONITOR)
5019		iwl_mvm_rm_snif_sta(mvm, vif);
5020
5021
5022	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5023		disabled_vif = vif;
5024		if (!fw_has_capa(&mvm->fw->ucode_capa,
5025				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5026			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5027	}
5028
5029	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5030	iwl_mvm_binding_remove_vif(mvm, vif);
5031
5032out:
5033	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5034	    switching_chanctx)
5035		return;
5036	mvmvif->deflink.phy_ctxt = NULL;
5037	iwl_mvm_power_update_mac(mvm);
5038}
5039
5040static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5041					 struct ieee80211_vif *vif,
5042					 struct ieee80211_bss_conf *link_conf,
5043					 struct ieee80211_chanctx_conf *ctx)
5044{
5045	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5046
5047	mutex_lock(&mvm->mutex);
5048	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5049	mutex_unlock(&mvm->mutex);
5050}
5051
5052static int
5053iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5054				struct ieee80211_vif_chanctx_switch *vifs,
5055				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5056{
5057	int ret;
5058
5059	mutex_lock(&mvm->mutex);
5060	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5061				    vifs[0].old_ctx, true);
5062	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5063
5064	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5065	if (ret) {
5066		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5067		goto out_reassign;
5068	}
5069
5070	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5071					vifs[0].new_ctx, true);
5072	if (ret) {
5073		IWL_ERR(mvm,
5074			"failed to assign new_ctx during channel switch\n");
5075		goto out_remove;
5076	}
5077
5078	/* we don't support TDLS during DCM - can be caused by channel switch */
5079	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5080		iwl_mvm_teardown_tdls_peers(mvm);
5081
5082	goto out;
5083
5084out_remove:
5085	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5086
5087out_reassign:
5088	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5089		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5090		goto out_restart;
5091	}
5092
5093	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5094				      vifs[0].old_ctx, true)) {
5095		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5096		goto out_restart;
5097	}
5098
5099	goto out;
5100
5101out_restart:
5102	/* things keep failing, better restart the hw */
5103	iwl_mvm_nic_restart(mvm, false);
5104
5105out:
5106	mutex_unlock(&mvm->mutex);
5107
5108	return ret;
5109}
5110
5111static int
5112iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5113				    struct ieee80211_vif_chanctx_switch *vifs,
5114				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5115{
5116	int ret;
5117
5118	mutex_lock(&mvm->mutex);
5119	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5120				    vifs[0].old_ctx, true);
5121
5122	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5123					vifs[0].new_ctx, true);
5124	if (ret) {
5125		IWL_ERR(mvm,
5126			"failed to assign new_ctx during channel switch\n");
5127		goto out_reassign;
5128	}
5129
5130	goto out;
5131
5132out_reassign:
5133	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5134				      vifs[0].old_ctx, true)) {
5135		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5136		goto out_restart;
5137	}
5138
5139	goto out;
5140
5141out_restart:
5142	/* things keep failing, better restart the hw */
5143	iwl_mvm_nic_restart(mvm, false);
5144
5145out:
5146	mutex_unlock(&mvm->mutex);
5147
5148	return ret;
5149}
5150
5151/* Execute the common part for both MLD and non-MLD modes */
5152int
5153iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5154				  struct ieee80211_vif_chanctx_switch *vifs,
5155				  int n_vifs,
5156				  enum ieee80211_chanctx_switch_mode mode,
5157				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5158{
5159	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5160	int ret;
5161
5162	/* we only support a single-vif right now */
5163	if (n_vifs > 1)
5164		return -EOPNOTSUPP;
5165
5166	switch (mode) {
5167	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5168		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5169		break;
5170	case CHANCTX_SWMODE_REASSIGN_VIF:
5171		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5172		break;
5173	default:
5174		ret = -EOPNOTSUPP;
5175		break;
5176	}
5177
5178	return ret;
5179}
5180
5181static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5182				      struct ieee80211_vif_chanctx_switch *vifs,
5183				      int n_vifs,
5184				      enum ieee80211_chanctx_switch_mode mode)
5185{
5186	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5187		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5188		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5189	};
5190
5191	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5192}
5193
5194int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
5195{
5196	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5197
5198	return mvm->ibss_manager;
5199}
5200
5201int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5202		    bool set)
5203{
5204	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5205	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5206
5207	if (!mvm_sta || !mvm_sta->vif) {
5208		IWL_ERR(mvm, "Station is not associated to a vif\n");
5209		return -EINVAL;
5210	}
5211
5212	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
5213					       &mvm_sta->vif->bss_conf);
5214}
5215
5216#ifdef CONFIG_NL80211_TESTMODE
5217static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5218	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5219	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5220	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5221};
5222
5223static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5224				      struct ieee80211_vif *vif,
5225				      void *data, int len)
5226{
5227	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5228	int err;
5229	u32 noa_duration;
5230
5231	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
5232				   iwl_mvm_tm_policy, NULL);
5233	if (err)
5234		return err;
5235
5236	if (!tb[IWL_MVM_TM_ATTR_CMD])
5237		return -EINVAL;
5238
5239	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5240	case IWL_MVM_TM_CMD_SET_NOA:
5241		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5242		    !vif->bss_conf.enable_beacon ||
5243		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5244			return -EINVAL;
5245
5246		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5247		if (noa_duration >= vif->bss_conf.beacon_int)
5248			return -EINVAL;
5249
5250		mvm->noa_duration = noa_duration;
5251		mvm->noa_vif = vif;
5252
5253		return iwl_mvm_update_quotas(mvm, true, NULL);
5254	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5255		/* must be associated client vif - ignore authorized */
5256		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5257		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5258		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5259			return -EINVAL;
5260
5261		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
5262			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
5263		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
5264	}
5265
5266	return -EOPNOTSUPP;
5267}
5268
5269int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5270			     struct ieee80211_vif *vif,
5271			     void *data, int len)
5272{
5273	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5274	int err;
5275
5276	mutex_lock(&mvm->mutex);
5277	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5278	mutex_unlock(&mvm->mutex);
5279
5280	return err;
5281}
5282#endif
5283
5284void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5285			    struct ieee80211_channel_switch *chsw)
5286{
5287	/* By implementing this operation, we prevent mac80211 from
5288	 * starting its own channel switch timer, so that we can call
5289	 * ieee80211_chswitch_done() ourselves at the right time
5290	 * (which is when the absence time event starts).
5291	 */
5292
5293	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5294			   "dummy channel switch op\n");
5295}
5296
5297static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
5298				       struct ieee80211_vif *vif,
5299				       struct ieee80211_channel_switch *chsw)
5300{
5301	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5302	struct iwl_chan_switch_te_cmd cmd = {
5303		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5304							  mvmvif->color)),
5305		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
5306		.tsf = cpu_to_le32(chsw->timestamp),
5307		.cs_count = chsw->count,
5308		.cs_mode = chsw->block_tx,
5309	};
5310
5311	lockdep_assert_held(&mvm->mutex);
5312
5313	if (chsw->delay)
5314		cmd.cs_delayed_bcn_count =
5315			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
5316
5317	return iwl_mvm_send_cmd_pdu(mvm,
5318				    WIDE_ID(MAC_CONF_GROUP,
5319					    CHANNEL_SWITCH_TIME_EVENT_CMD),
5320				    0, sizeof(cmd), &cmd);
5321}
5322
5323static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
5324				       struct ieee80211_vif *vif,
5325				       struct ieee80211_channel_switch *chsw)
5326{
5327	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5328	u32 apply_time;
5329
5330	/* Schedule the time event to a bit before beacon 1,
5331	 * to make sure we're in the new channel when the
5332	 * GO/AP arrives. In case count <= 1 immediately schedule the
5333	 * TE (this might result with some packet loss or connection
5334	 * loss).
5335	 */
5336	if (chsw->count <= 1)
5337		apply_time = 0;
5338	else
5339		apply_time = chsw->device_timestamp +
5340			((vif->bss_conf.beacon_int * (chsw->count - 1) -
5341			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
5342
5343	if (chsw->block_tx)
5344		iwl_mvm_csa_client_absent(mvm, vif);
5345
5346	if (mvmvif->bf_data.bf_enabled) {
5347		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
5348
5349		if (ret)
5350			return ret;
5351	}
5352
5353	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
5354				    apply_time);
5355
5356	return 0;
5357}
5358
5359#define IWL_MAX_CSA_BLOCK_TX 1500
5360int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
5361			       struct ieee80211_vif *vif,
5362			       struct ieee80211_channel_switch *chsw)
5363{
5364	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5365	struct ieee80211_vif *csa_vif;
5366	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5367	int ret;
5368
5369	mutex_lock(&mvm->mutex);
5370
5371	mvmvif->csa_failed = false;
5372
5373	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5374			   chsw->chandef.center_freq1);
5375
5376	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
5377				       ieee80211_vif_to_wdev(vif),
5378				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5379
5380	switch (vif->type) {
5381	case NL80211_IFTYPE_AP:
5382		csa_vif =
5383			rcu_dereference_protected(mvm->csa_vif,
5384						  lockdep_is_held(&mvm->mutex));
5385		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5386			      "Another CSA is already in progress")) {
5387			ret = -EBUSY;
5388			goto out_unlock;
5389		}
5390
5391		/* we still didn't unblock tx. prevent new CS meanwhile */
5392		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5393					      lockdep_is_held(&mvm->mutex))) {
5394			ret = -EBUSY;
5395			goto out_unlock;
5396		}
5397
5398		rcu_assign_pointer(mvm->csa_vif, vif);
5399
5400		if (WARN_ONCE(mvmvif->csa_countdown,
5401			      "Previous CSA countdown didn't complete")) {
5402			ret = -EBUSY;
5403			goto out_unlock;
5404		}
5405
5406		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5407
5408		break;
5409	case NL80211_IFTYPE_STATION:
5410		/*
5411		 * In the new flow FW is in charge of timing the switch so there
5412		 * is no need for all of this
5413		 */
5414		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5415					    CHANNEL_SWITCH_ERROR_NOTIF,
5416					    0))
5417			break;
5418
5419		/*
5420		 * We haven't configured the firmware to be associated yet since
5421		 * we don't know the dtim period. In this case, the firmware can't
5422		 * track the beacons.
5423		 */
5424		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period) {
5425			ret = -EBUSY;
5426			goto out_unlock;
5427		}
5428
5429		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX)
5430			schedule_delayed_work(&mvmvif->csa_work, 0);
5431
5432		if (chsw->block_tx) {
5433			/*
5434			 * In case of undetermined / long time with immediate
5435			 * quiet monitor status to gracefully disconnect
5436			 */
5437			if (!chsw->count ||
5438			    chsw->count * vif->bss_conf.beacon_int >
5439			    IWL_MAX_CSA_BLOCK_TX)
5440				schedule_delayed_work(&mvmvif->csa_work,
5441						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5442		}
5443
5444		if (!fw_has_capa(&mvm->fw->ucode_capa,
5445				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5446			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5447			if (ret)
5448				goto out_unlock;
5449		} else {
5450			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
5451		}
5452
5453		mvmvif->csa_count = chsw->count;
5454		mvmvif->csa_misbehave = false;
5455		break;
5456	default:
5457		break;
5458	}
5459
5460	mvmvif->ps_disabled = true;
5461
5462	ret = iwl_mvm_power_update_ps(mvm);
5463	if (ret)
5464		goto out_unlock;
5465
5466	/* we won't be on this channel any longer */
5467	iwl_mvm_teardown_tdls_peers(mvm);
5468
5469out_unlock:
5470	mutex_unlock(&mvm->mutex);
5471
5472	return ret;
5473}
5474
5475void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5476				      struct ieee80211_vif *vif,
5477				      struct ieee80211_channel_switch *chsw)
5478{
5479	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5480	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5481	struct iwl_chan_switch_te_cmd cmd = {
5482		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5483							  mvmvif->color)),
5484		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5485		.tsf = cpu_to_le32(chsw->timestamp),
5486		.cs_count = chsw->count,
5487		.cs_mode = chsw->block_tx,
5488	};
5489
5490	/*
5491	 * In the new flow FW is in charge of timing the switch so there is no
5492	 * need for all of this
5493	 */
5494	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5495				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5496		return;
5497
5498	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5499		return;
5500
5501	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5502			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5503
5504	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
5505		if (mvmvif->csa_misbehave) {
5506			/* Second time, give up on this AP*/
5507			iwl_mvm_abort_channel_switch(hw, vif);
5508			ieee80211_chswitch_done(vif, false);
5509			mvmvif->csa_misbehave = false;
5510			return;
5511		}
5512		mvmvif->csa_misbehave = true;
5513	}
5514	mvmvif->csa_count = chsw->count;
5515
5516	mutex_lock(&mvm->mutex);
5517	if (mvmvif->csa_failed)
5518		goto out_unlock;
5519
5520	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5521				     WIDE_ID(MAC_CONF_GROUP,
5522					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5523				     0, sizeof(cmd), &cmd));
5524out_unlock:
5525	mutex_unlock(&mvm->mutex);
5526}
5527
5528static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
5529{
5530	int i;
5531
5532	if (!iwl_mvm_has_new_tx_api(mvm)) {
5533		if (drop) {
5534			mutex_lock(&mvm->mutex);
5535			iwl_mvm_flush_tx_path(mvm,
5536				iwl_mvm_flushable_queues(mvm) & queues);
5537			mutex_unlock(&mvm->mutex);
5538		} else {
5539			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5540		}
5541		return;
5542	}
5543
5544	mutex_lock(&mvm->mutex);
5545	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5546		struct ieee80211_sta *sta;
5547
5548		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5549						lockdep_is_held(&mvm->mutex));
5550		if (IS_ERR_OR_NULL(sta))
5551			continue;
5552
5553		if (drop)
5554			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
5555		else
5556			iwl_mvm_wait_sta_queues_empty(mvm,
5557					iwl_mvm_sta_from_mac80211(sta));
5558	}
5559	mutex_unlock(&mvm->mutex);
5560}
5561
5562void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5563		       u32 queues, bool drop)
5564{
5565	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5566	struct iwl_mvm_vif *mvmvif;
5567	struct iwl_mvm_sta *mvmsta;
5568	struct ieee80211_sta *sta;
5569	bool ap_sta_done = false;
5570	int i;
5571	u32 msk = 0;
5572
5573	if (!vif) {
5574		iwl_mvm_flush_no_vif(mvm, queues, drop);
5575		return;
5576	}
5577
5578	/* Make sure we're done with the deferred traffic before flushing */
5579	flush_work(&mvm->add_stream_wk);
5580
5581	mutex_lock(&mvm->mutex);
5582	mvmvif = iwl_mvm_vif_from_mac80211(vif);
5583
5584	/* flush the AP-station and all TDLS peers */
5585	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5586		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5587						lockdep_is_held(&mvm->mutex));
5588		if (IS_ERR_OR_NULL(sta))
5589			continue;
5590
5591		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5592		if (mvmsta->vif != vif)
5593			continue;
5594
5595		if (sta == mvmvif->ap_sta) {
5596			if (ap_sta_done)
5597				continue;
5598			ap_sta_done = true;
5599		}
5600
5601		if (drop) {
5602			if (iwl_mvm_flush_sta(mvm, mvmsta->deflink.sta_id,
5603					      mvmsta->tfd_queue_msk))
5604				IWL_ERR(mvm, "flush request fail\n");
5605		} else {
5606			if (iwl_mvm_has_new_tx_api(mvm))
5607				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5608			else /* only used for !iwl_mvm_has_new_tx_api() below */
5609				msk |= mvmsta->tfd_queue_msk;
5610		}
5611	}
5612
5613	mutex_unlock(&mvm->mutex);
5614
5615	/* this can take a while, and we may need/want other operations
5616	 * to succeed while doing this, so do it without the mutex held
5617	 */
5618	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5619		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5620}
5621
5622void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5623			   struct ieee80211_sta *sta)
5624{
5625	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5626	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5627	struct iwl_mvm_link_sta *mvm_link_sta;
5628	struct ieee80211_link_sta *link_sta;
5629	int link_id;
5630
5631	mutex_lock(&mvm->mutex);
5632	for_each_sta_active_link(vif, sta, link_sta, link_id) {
5633		mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_id],
5634							 lockdep_is_held(&mvm->mutex));
5635		if (!mvm_link_sta)
5636			continue;
5637
5638		if (iwl_mvm_flush_sta(mvm, mvm_link_sta->sta_id,
5639				      mvmsta->tfd_queue_msk))
5640			IWL_ERR(mvm, "flush request fail\n");
5641	}
5642	mutex_unlock(&mvm->mutex);
5643}
5644
5645int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5646			   struct survey_info *survey)
5647{
5648	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5649	int ret;
5650
5651	memset(survey, 0, sizeof(*survey));
5652
5653	/* only support global statistics right now */
5654	if (idx != 0)
5655		return -ENOENT;
5656
5657	if (!fw_has_capa(&mvm->fw->ucode_capa,
5658			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
5659		return -ENOENT;
5660
5661	mutex_lock(&mvm->mutex);
5662
5663	if (iwl_mvm_firmware_running(mvm)) {
5664		ret = iwl_mvm_request_statistics(mvm, false);
5665		if (ret)
5666			goto out;
5667	}
5668
5669	survey->filled = SURVEY_INFO_TIME |
5670			 SURVEY_INFO_TIME_RX |
5671			 SURVEY_INFO_TIME_TX |
5672			 SURVEY_INFO_TIME_SCAN;
5673	survey->time = mvm->accu_radio_stats.on_time_rf +
5674		       mvm->radio_stats.on_time_rf;
5675	do_div(survey->time, USEC_PER_MSEC);
5676
5677	survey->time_rx = mvm->accu_radio_stats.rx_time +
5678			  mvm->radio_stats.rx_time;
5679	do_div(survey->time_rx, USEC_PER_MSEC);
5680
5681	survey->time_tx = mvm->accu_radio_stats.tx_time +
5682			  mvm->radio_stats.tx_time;
5683	do_div(survey->time_tx, USEC_PER_MSEC);
5684
5685	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
5686			    mvm->radio_stats.on_time_scan;
5687	do_div(survey->time_scan, USEC_PER_MSEC);
5688
5689	ret = 0;
5690 out:
5691	mutex_unlock(&mvm->mutex);
5692	return ret;
5693}
5694
5695static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
5696{
5697	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
5698	u32 gi_ltf;
5699
5700	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
5701	case RATE_MCS_CHAN_WIDTH_20:
5702		rinfo->bw = RATE_INFO_BW_20;
5703		break;
5704	case RATE_MCS_CHAN_WIDTH_40:
5705		rinfo->bw = RATE_INFO_BW_40;
5706		break;
5707	case RATE_MCS_CHAN_WIDTH_80:
5708		rinfo->bw = RATE_INFO_BW_80;
5709		break;
5710	case RATE_MCS_CHAN_WIDTH_160:
5711		rinfo->bw = RATE_INFO_BW_160;
5712		break;
5713	case RATE_MCS_CHAN_WIDTH_320:
5714		rinfo->bw = RATE_INFO_BW_320;
5715		break;
5716	}
5717
5718	if (format == RATE_MCS_CCK_MSK ||
5719	    format == RATE_MCS_LEGACY_OFDM_MSK) {
5720		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
5721
5722		/* add the offset needed to get to the legacy ofdm indices */
5723		if (format == RATE_MCS_LEGACY_OFDM_MSK)
5724			rate += IWL_FIRST_OFDM_RATE;
5725
5726		switch (rate) {
5727		case IWL_RATE_1M_INDEX:
5728			rinfo->legacy = 10;
5729			break;
5730		case IWL_RATE_2M_INDEX:
5731			rinfo->legacy = 20;
5732			break;
5733		case IWL_RATE_5M_INDEX:
5734			rinfo->legacy = 55;
5735			break;
5736		case IWL_RATE_11M_INDEX:
5737			rinfo->legacy = 110;
5738			break;
5739		case IWL_RATE_6M_INDEX:
5740			rinfo->legacy = 60;
5741			break;
5742		case IWL_RATE_9M_INDEX:
5743			rinfo->legacy = 90;
5744			break;
5745		case IWL_RATE_12M_INDEX:
5746			rinfo->legacy = 120;
5747			break;
5748		case IWL_RATE_18M_INDEX:
5749			rinfo->legacy = 180;
5750			break;
5751		case IWL_RATE_24M_INDEX:
5752			rinfo->legacy = 240;
5753			break;
5754		case IWL_RATE_36M_INDEX:
5755			rinfo->legacy = 360;
5756			break;
5757		case IWL_RATE_48M_INDEX:
5758			rinfo->legacy = 480;
5759			break;
5760		case IWL_RATE_54M_INDEX:
5761			rinfo->legacy = 540;
5762		}
5763		return;
5764	}
5765
5766	rinfo->nss = u32_get_bits(rate_n_flags,
5767				  RATE_MCS_NSS_MSK) + 1;
5768	rinfo->mcs = format == RATE_MCS_HT_MSK ?
5769		RATE_HT_MCS_INDEX(rate_n_flags) :
5770		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
5771
5772	if (rate_n_flags & RATE_MCS_SGI_MSK)
5773		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
5774
5775	switch (format) {
5776	case RATE_MCS_EHT_MSK:
5777		/* TODO: GI/LTF/RU. How does the firmware encode them? */
5778		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
5779		break;
5780	case RATE_MCS_HE_MSK:
5781		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
5782
5783		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
5784
5785		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
5786			rinfo->bw = RATE_INFO_BW_HE_RU;
5787			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
5788		}
5789
5790		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
5791		case RATE_MCS_HE_TYPE_SU:
5792		case RATE_MCS_HE_TYPE_EXT_SU:
5793			if (gi_ltf == 0 || gi_ltf == 1)
5794				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5795			else if (gi_ltf == 2)
5796				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5797			else if (gi_ltf == 3)
5798				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5799			else
5800				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5801			break;
5802		case RATE_MCS_HE_TYPE_MU:
5803			if (gi_ltf == 0 || gi_ltf == 1)
5804				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5805			else if (gi_ltf == 2)
5806				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5807			else
5808				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5809			break;
5810		case RATE_MCS_HE_TYPE_TRIG:
5811			if (gi_ltf == 0 || gi_ltf == 1)
5812				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5813			else
5814				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5815			break;
5816		}
5817
5818		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
5819			rinfo->he_dcm = 1;
5820		break;
5821	case RATE_MCS_HT_MSK:
5822		rinfo->flags |= RATE_INFO_FLAGS_MCS;
5823		break;
5824	case RATE_MCS_VHT_MSK:
5825		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
5826		break;
5827	}
5828}
5829
5830void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
5831				struct ieee80211_vif *vif,
5832				struct ieee80211_sta *sta,
5833				struct station_info *sinfo)
5834{
5835	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5836	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5837	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5838
5839	if (mvmsta->deflink.avg_energy) {
5840		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
5841		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
5842	}
5843
5844	if (iwl_mvm_has_tlc_offload(mvm)) {
5845		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
5846
5847		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5848		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5849	}
5850
5851	/* if beacon filtering isn't on mac80211 does it anyway */
5852	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5853		return;
5854
5855	if (!vif->cfg.assoc)
5856		return;
5857
5858	mutex_lock(&mvm->mutex);
5859
5860	if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
5861		goto unlock;
5862
5863	if (iwl_mvm_request_statistics(mvm, false))
5864		goto unlock;
5865
5866	sinfo->rx_beacon = mvmvif->deflink.beacon_stats.num_beacons +
5867			   mvmvif->deflink.beacon_stats.accu_num_beacons;
5868	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
5869	if (mvmvif->deflink.beacon_stats.avg_signal) {
5870		/* firmware only reports a value after RXing a few beacons */
5871		sinfo->rx_beacon_signal_avg =
5872			mvmvif->deflink.beacon_stats.avg_signal;
5873		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
5874	}
5875 unlock:
5876	mutex_unlock(&mvm->mutex);
5877}
5878
5879static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5880					    struct ieee80211_vif *vif,
5881					    const  struct ieee80211_mlme_event *mlme)
5882{
5883	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
5884	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
5885		iwl_dbg_tlv_time_point(&mvm->fwrt,
5886				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5887				       NULL);
5888		return;
5889	}
5890
5891	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5892		iwl_dbg_tlv_time_point(&mvm->fwrt,
5893				       IWL_FW_INI_TIME_POINT_DEASSOC,
5894				       NULL);
5895		return;
5896	}
5897}
5898
5899static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5900					struct ieee80211_vif *vif,
5901					const struct ieee80211_event *event)
5902{
5903#define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5904	do {								\
5905		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5906			break;						\
5907		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5908	} while (0)
5909
5910	struct iwl_fw_dbg_trigger_tlv *trig;
5911	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5912
5913	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5914		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5915		return;
5916	}
5917
5918	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
5919				     FW_DBG_TRIGGER_MLME);
5920	if (!trig)
5921		return;
5922
5923	trig_mlme = (void *)trig->data;
5924
5925	if (event->u.mlme.data == ASSOC_EVENT) {
5926		if (event->u.mlme.status == MLME_DENIED)
5927			CHECK_MLME_TRIGGER(stop_assoc_denied,
5928					   "DENIED ASSOC: reason %d",
5929					    event->u.mlme.reason);
5930		else if (event->u.mlme.status == MLME_TIMEOUT)
5931			CHECK_MLME_TRIGGER(stop_assoc_timeout,
5932					   "ASSOC TIMEOUT");
5933	} else if (event->u.mlme.data == AUTH_EVENT) {
5934		if (event->u.mlme.status == MLME_DENIED)
5935			CHECK_MLME_TRIGGER(stop_auth_denied,
5936					   "DENIED AUTH: reason %d",
5937					   event->u.mlme.reason);
5938		else if (event->u.mlme.status == MLME_TIMEOUT)
5939			CHECK_MLME_TRIGGER(stop_auth_timeout,
5940					   "AUTH TIMEOUT");
5941	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
5942		CHECK_MLME_TRIGGER(stop_rx_deauth,
5943				   "DEAUTH RX %d", event->u.mlme.reason);
5944	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
5945		CHECK_MLME_TRIGGER(stop_tx_deauth,
5946				   "DEAUTH TX %d", event->u.mlme.reason);
5947	}
5948#undef CHECK_MLME_TRIGGER
5949}
5950
5951static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5952					  struct ieee80211_vif *vif,
5953					  const struct ieee80211_event *event)
5954{
5955	struct iwl_fw_dbg_trigger_tlv *trig;
5956	struct iwl_fw_dbg_trigger_ba *ba_trig;
5957
5958	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
5959				     FW_DBG_TRIGGER_BA);
5960	if (!trig)
5961		return;
5962
5963	ba_trig = (void *)trig->data;
5964
5965	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5966		return;
5967
5968	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
5969				"BAR received from %pM, tid %d, ssn %d",
5970				event->u.ba.sta->addr, event->u.ba.tid,
5971				event->u.ba.ssn);
5972}
5973
5974void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
5975				struct ieee80211_vif *vif,
5976				const struct ieee80211_event *event)
5977{
5978	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5979
5980	switch (event->type) {
5981	case MLME_EVENT:
5982		iwl_mvm_event_mlme_callback(mvm, vif, event);
5983		break;
5984	case BAR_RX_EVENT:
5985		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
5986		break;
5987	case BA_FRAME_TIMEOUT:
5988		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
5989						     event->u.ba.tid);
5990		break;
5991	default:
5992		break;
5993	}
5994}
5995
5996void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
5997				     enum iwl_mvm_rxq_notif_type type,
5998				     bool sync,
5999				     const void *data, u32 size)
6000{
6001	struct {
6002		struct iwl_rxq_sync_cmd cmd;
6003		struct iwl_mvm_internal_rxq_notif notif;
6004	} __packed cmd = {
6005		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
6006		.cmd.count =
6007			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
6008				    size),
6009		.notif.type = type,
6010		.notif.sync = sync,
6011	};
6012	struct iwl_host_cmd hcmd = {
6013		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
6014		.data[0] = &cmd,
6015		.len[0] = sizeof(cmd),
6016		.data[1] = data,
6017		.len[1] = size,
6018		.flags = sync ? 0 : CMD_ASYNC,
6019	};
6020	int ret;
6021
6022	/* size must be a multiple of DWORD */
6023	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
6024		return;
6025
6026	if (!iwl_mvm_has_new_rx_api(mvm))
6027		return;
6028
6029	if (sync) {
6030		cmd.notif.cookie = mvm->queue_sync_cookie;
6031		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6032	}
6033
6034	ret = iwl_mvm_send_cmd(mvm, &hcmd);
6035	if (ret) {
6036		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
6037		goto out;
6038	}
6039
6040	if (sync) {
6041		lockdep_assert_held(&mvm->mutex);
6042		ret = wait_event_timeout(mvm->rx_sync_waitq,
6043					 READ_ONCE(mvm->queue_sync_state) == 0 ||
6044					 iwl_mvm_is_radio_killed(mvm),
6045					 HZ);
6046		WARN_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm),
6047			  "queue sync: failed to sync, state is 0x%lx\n",
6048			  mvm->queue_sync_state);
6049	}
6050
6051out:
6052	if (sync) {
6053		mvm->queue_sync_state = 0;
6054		mvm->queue_sync_cookie++;
6055	}
6056}
6057
6058void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
6059{
6060	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6061
6062	mutex_lock(&mvm->mutex);
6063	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
6064	mutex_unlock(&mvm->mutex);
6065}
6066
6067int
6068iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6069				    struct ieee80211_vif *vif,
6070				    struct cfg80211_ftm_responder_stats *stats)
6071{
6072	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6073	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6074
6075	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6076	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6077		return -EINVAL;
6078
6079	mutex_lock(&mvm->mutex);
6080	*stats = mvm->ftm_resp_stats;
6081	mutex_unlock(&mvm->mutex);
6082
6083	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6084			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6085			BIT(NL80211_FTM_STATS_FAILED_NUM) |
6086			BIT(NL80211_FTM_STATS_ASAP_NUM) |
6087			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6088			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6089			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6090			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6091			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6092
6093	return 0;
6094}
6095
6096int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6097		       struct cfg80211_pmsr_request *request)
6098{
6099	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6100	int ret;
6101
6102	mutex_lock(&mvm->mutex);
6103	ret = iwl_mvm_ftm_start(mvm, vif, request);
6104	mutex_unlock(&mvm->mutex);
6105
6106	return ret;
6107}
6108
6109void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6110			struct cfg80211_pmsr_request *request)
6111{
6112	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6113
6114	mutex_lock(&mvm->mutex);
6115	iwl_mvm_ftm_abort(mvm, request);
6116	mutex_unlock(&mvm->mutex);
6117}
6118
6119static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6120{
6121	u8 protocol = ip_hdr(skb)->protocol;
6122
6123	if (!IS_ENABLED(CONFIG_INET))
6124		return false;
6125
6126	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6127}
6128
6129static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6130				      struct sk_buff *head,
6131				      struct sk_buff *skb)
6132{
6133	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6134
6135	/* For now don't aggregate IPv6 in AMSDU */
6136	if (skb->protocol != htons(ETH_P_IP))
6137		return false;
6138
6139	if (!iwl_mvm_is_csum_supported(mvm))
6140		return true;
6141
6142	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6143}
6144
6145int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6146			     struct ieee80211_vif *vif,
6147			     struct cfg80211_set_hw_timestamp *hwts)
6148{
6149	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6150	u32 protocols = 0;
6151	int ret;
6152
6153	/* HW timestamping is only supported for a specific station */
6154	if (!hwts->macaddr)
6155		return -EOPNOTSUPP;
6156
6157	if (hwts->enable)
6158		protocols =
6159			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6160
6161	mutex_lock(&mvm->mutex);
6162	ret = iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6163	mutex_unlock(&mvm->mutex);
6164
6165	return ret;
6166}
6167
6168const struct ieee80211_ops iwl_mvm_hw_ops = {
6169	.tx = iwl_mvm_mac_tx,
6170	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6171	.ampdu_action = iwl_mvm_mac_ampdu_action,
6172	.get_antenna = iwl_mvm_op_get_antenna,
6173	.start = iwl_mvm_mac_start,
6174	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
6175	.stop = iwl_mvm_mac_stop,
6176	.add_interface = iwl_mvm_mac_add_interface,
6177	.remove_interface = iwl_mvm_mac_remove_interface,
6178	.config = iwl_mvm_mac_config,
6179	.prepare_multicast = iwl_mvm_prepare_multicast,
6180	.configure_filter = iwl_mvm_configure_filter,
6181	.config_iface_filter = iwl_mvm_config_iface_filter,
6182	.bss_info_changed = iwl_mvm_bss_info_changed,
6183	.hw_scan = iwl_mvm_mac_hw_scan,
6184	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6185	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6186	.sta_state = iwl_mvm_mac_sta_state,
6187	.sta_notify = iwl_mvm_mac_sta_notify,
6188	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6189	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6190	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6191	.sta_rc_update = iwl_mvm_sta_rc_update,
6192	.conf_tx = iwl_mvm_mac_conf_tx,
6193	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
6194	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6195	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6196	.flush = iwl_mvm_mac_flush,
6197	.flush_sta = iwl_mvm_mac_flush_sta,
6198	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
6199	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6200	.set_key = iwl_mvm_mac_set_key,
6201	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
6202	.remain_on_channel = iwl_mvm_roc,
6203	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
6204	.add_chanctx = iwl_mvm_add_chanctx,
6205	.remove_chanctx = iwl_mvm_remove_chanctx,
6206	.change_chanctx = iwl_mvm_change_chanctx,
6207	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6208	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6209	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6210
6211	.start_ap = iwl_mvm_start_ap,
6212	.stop_ap = iwl_mvm_stop_ap,
6213	.join_ibss = iwl_mvm_start_ibss,
6214	.leave_ibss = iwl_mvm_stop_ibss,
6215
6216	.tx_last_beacon = iwl_mvm_tx_last_beacon,
6217
6218	.set_tim = iwl_mvm_set_tim,
6219
6220	.channel_switch = iwl_mvm_channel_switch,
6221	.pre_channel_switch = iwl_mvm_pre_channel_switch,
6222	.post_channel_switch = iwl_mvm_post_channel_switch,
6223	.abort_channel_switch = iwl_mvm_abort_channel_switch,
6224	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6225
6226	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6227	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6228	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6229
6230	.event_callback = iwl_mvm_mac_event_callback,
6231
6232	.sync_rx_queues = iwl_mvm_sync_rx_queues,
6233
6234	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
6235
6236#ifdef CONFIG_PM_SLEEP
6237	/* look at d3.c */
6238	.suspend = iwl_mvm_suspend,
6239	.resume = iwl_mvm_resume,
6240	.set_wakeup = iwl_mvm_set_wakeup,
6241	.set_rekey_data = iwl_mvm_set_rekey_data,
6242#if IS_ENABLED(CONFIG_IPV6)
6243	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6244#endif
6245	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6246#endif
6247	.get_survey = iwl_mvm_mac_get_survey,
6248	.sta_statistics = iwl_mvm_mac_sta_statistics,
6249	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6250	.start_pmsr = iwl_mvm_start_pmsr,
6251	.abort_pmsr = iwl_mvm_abort_pmsr,
6252
6253	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6254#ifdef CONFIG_IWLWIFI_DEBUGFS
6255	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
6256#endif
6257	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6258};
6259