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