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