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