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