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