xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision 0759356bf5fadbd16794bec982a9af48351a02ff)
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 = iwl_mvm_get_bss_vif(mvm);
1702 
1703 	guard(mvm)(mvm);
1704 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_PREVENTION);
1705 }
1706 
1707 static void iwl_mvm_mlo_int_scan_wk(struct wiphy *wiphy, struct wiphy_work *wk)
1708 {
1709 	struct iwl_mvm_vif *mvmvif = container_of(wk, struct iwl_mvm_vif,
1710 						  mlo_int_scan_wk.work);
1711 	struct ieee80211_vif *vif =
1712 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1713 
1714 	guard(mvm)(mvmvif->mvm);
1715 	iwl_mvm_int_mlo_scan(mvmvif->mvm, vif);
1716 }
1717 
1718 static void iwl_mvm_unblock_esr_tpt(struct wiphy *wiphy, struct wiphy_work *wk)
1719 {
1720 	struct iwl_mvm_vif *mvmvif =
1721 		container_of(wk, struct iwl_mvm_vif, unblock_esr_tpt_wk);
1722 	struct iwl_mvm *mvm = mvmvif->mvm;
1723 	struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
1724 
1725 	guard(mvm)(mvm);
1726 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT);
1727 }
1728 
1729 void iwl_mvm_mac_init_mvmvif(struct iwl_mvm *mvm, struct iwl_mvm_vif *mvmvif)
1730 {
1731 	lockdep_assert_held(&mvm->mutex);
1732 
1733 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1734 		return;
1735 
1736 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1737 			  iwl_mvm_channel_switch_disconnect_wk);
1738 
1739 	wiphy_delayed_work_init(&mvmvif->prevent_esr_done_wk,
1740 				iwl_mvm_prevent_esr_done_wk);
1741 
1742 	wiphy_delayed_work_init(&mvmvif->mlo_int_scan_wk,
1743 				iwl_mvm_mlo_int_scan_wk);
1744 
1745 	wiphy_work_init(&mvmvif->unblock_esr_tpt_wk,
1746 			iwl_mvm_unblock_esr_tpt);
1747 }
1748 
1749 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1750 				     struct ieee80211_vif *vif)
1751 {
1752 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1753 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1754 	int ret;
1755 	int i;
1756 
1757 	mutex_lock(&mvm->mutex);
1758 
1759 	iwl_mvm_mac_init_mvmvif(mvm, mvmvif);
1760 
1761 	mvmvif->mvm = mvm;
1762 
1763 	/* the first link always points to the default one */
1764 	mvmvif->deflink.fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1765 	mvmvif->deflink.active = 0;
1766 	mvmvif->link[0] = &mvmvif->deflink;
1767 
1768 	ret = iwl_mvm_set_link_mapping(mvm, vif, &vif->bss_conf);
1769 	if (ret)
1770 		goto out;
1771 
1772 	/*
1773 	 * Not much to do here. The stack will not allow interface
1774 	 * types or combinations that we didn't advertise, so we
1775 	 * don't really have to check the types.
1776 	 */
1777 
1778 	/* make sure that beacon statistics don't go backwards with FW reset */
1779 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1780 		for_each_mvm_vif_valid_link(mvmvif, i)
1781 			mvmvif->link[i]->beacon_stats.accu_num_beacons +=
1782 				mvmvif->link[i]->beacon_stats.num_beacons;
1783 
1784 	/* Allocate resources for the MAC context, and add it to the fw  */
1785 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1786 	if (ret)
1787 		goto out;
1788 
1789 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1790 
1791 	/* Currently not much to do for NAN */
1792 	if (vif->type == NL80211_IFTYPE_NAN) {
1793 		ret = 0;
1794 		goto out;
1795 	}
1796 
1797 	/*
1798 	 * The AP binding flow can be done only after the beacon
1799 	 * template is configured (which happens only in the mac80211
1800 	 * start_ap() flow), and adding the broadcast station can happen
1801 	 * only after the binding.
1802 	 * In addition, since modifying the MAC before adding a bcast
1803 	 * station is not allowed by the FW, delay the adding of MAC context to
1804 	 * the point where we can also add the bcast station.
1805 	 * In short: there's not much we can do at this point, other than
1806 	 * allocating resources :)
1807 	 */
1808 	if (vif->type == NL80211_IFTYPE_AP ||
1809 	    vif->type == NL80211_IFTYPE_ADHOC) {
1810 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1811 			iwl_mvm_vif_dbgfs_add_link(mvm, vif);
1812 		ret = 0;
1813 		goto out;
1814 	}
1815 
1816 	mvmvif->features |= hw->netdev_features;
1817 
1818 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1819 	if (ret)
1820 		goto out_unlock;
1821 
1822 	ret = iwl_mvm_power_update_mac(mvm);
1823 	if (ret)
1824 		goto out_remove_mac;
1825 
1826 	/* beacon filtering */
1827 	ret = iwl_mvm_disable_beacon_filter(mvm, vif);
1828 	if (ret)
1829 		goto out_remove_mac;
1830 
1831 	if (!mvm->bf_allowed_vif &&
1832 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1833 		mvm->bf_allowed_vif = mvmvif;
1834 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1835 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1836 	}
1837 
1838 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1839 		mvm->p2p_device_vif = vif;
1840 
1841 	iwl_mvm_tcm_add_vif(mvm, vif);
1842 
1843 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1844 		mvm->monitor_on = true;
1845 		mvm->monitor_p80 =
1846 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chanreq.oper);
1847 	}
1848 
1849 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1850 		iwl_mvm_vif_dbgfs_add_link(mvm, vif);
1851 
1852 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1853 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
1854 	    !mvm->csme_vif && mvm->mei_registered) {
1855 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
1856 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
1857 		mvm->csme_vif = vif;
1858 	}
1859 
1860 out:
1861 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
1862 		     vif->type == NL80211_IFTYPE_ADHOC))
1863 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
1864 
1865 	goto out_unlock;
1866 
1867  out_remove_mac:
1868 	mvmvif->deflink.phy_ctxt = NULL;
1869 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1870  out_unlock:
1871 	mutex_unlock(&mvm->mutex);
1872 
1873 	return ret;
1874 }
1875 
1876 void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1877 				 struct ieee80211_vif *vif)
1878 {
1879 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1880 
1881 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1882 		/*
1883 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1884 		 * We assume here that all the packets sent to the OFFCHANNEL
1885 		 * queue are sent in ROC session.
1886 		 */
1887 		flush_work(&mvm->roc_done_wk);
1888 	}
1889 
1890 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1891 				  &mvmvif->prevent_esr_done_wk);
1892 
1893 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1894 				  &mvmvif->mlo_int_scan_wk);
1895 
1896 	wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
1897 
1898 	cancel_delayed_work_sync(&mvmvif->csa_work);
1899 }
1900 
1901 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1902 					 struct ieee80211_vif *vif)
1903 {
1904 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1905 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1906 	struct iwl_probe_resp_data *probe_data;
1907 
1908 	iwl_mvm_prepare_mac_removal(mvm, vif);
1909 
1910 	if (!(vif->type == NL80211_IFTYPE_AP ||
1911 	      vif->type == NL80211_IFTYPE_ADHOC))
1912 		iwl_mvm_tcm_rm_vif(mvm, vif);
1913 
1914 	mutex_lock(&mvm->mutex);
1915 
1916 	if (vif == mvm->csme_vif) {
1917 		iwl_mei_set_netdev(NULL);
1918 		mvm->csme_vif = NULL;
1919 	}
1920 
1921 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1922 					       lockdep_is_held(&mvm->mutex));
1923 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1924 	if (probe_data)
1925 		kfree_rcu(probe_data, rcu_head);
1926 
1927 	if (mvm->bf_allowed_vif == mvmvif) {
1928 		mvm->bf_allowed_vif = NULL;
1929 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1930 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1931 	}
1932 
1933 	if (vif->bss_conf.ftm_responder)
1934 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1935 
1936 	iwl_mvm_vif_dbgfs_rm_link(mvm, vif);
1937 
1938 	/*
1939 	 * For AP/GO interface, the tear down of the resources allocated to the
1940 	 * interface is be handled as part of the stop_ap flow.
1941 	 */
1942 	if (vif->type == NL80211_IFTYPE_AP ||
1943 	    vif->type == NL80211_IFTYPE_ADHOC) {
1944 #ifdef CONFIG_NL80211_TESTMODE
1945 		if (vif == mvm->noa_vif) {
1946 			mvm->noa_vif = NULL;
1947 			mvm->noa_duration = 0;
1948 		}
1949 #endif
1950 		goto out;
1951 	}
1952 
1953 	iwl_mvm_power_update_mac(mvm);
1954 
1955 	/* Before the interface removal, mac80211 would cancel the ROC, and the
1956 	 * ROC worker would be scheduled if needed. The worker would be flushed
1957 	 * in iwl_mvm_prepare_mac_removal() and thus at this point there is no
1958 	 * binding etc. so nothing needs to be done here.
1959 	 */
1960 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1961 		if (mvmvif->deflink.phy_ctxt) {
1962 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1963 			mvmvif->deflink.phy_ctxt = NULL;
1964 		}
1965 		mvm->p2p_device_vif = NULL;
1966 	}
1967 
1968 	iwl_mvm_unset_link_mapping(mvm, vif, &vif->bss_conf);
1969 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1970 
1971 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1972 
1973 	if (vif->type == NL80211_IFTYPE_MONITOR)
1974 		mvm->monitor_on = false;
1975 
1976 out:
1977 	if (vif->type == NL80211_IFTYPE_AP ||
1978 	    vif->type == NL80211_IFTYPE_ADHOC) {
1979 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
1980 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1981 	}
1982 
1983 	mutex_unlock(&mvm->mutex);
1984 }
1985 
1986 struct iwl_mvm_mc_iter_data {
1987 	struct iwl_mvm *mvm;
1988 	int port_id;
1989 };
1990 
1991 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1992 				      struct ieee80211_vif *vif)
1993 {
1994 	struct iwl_mvm_mc_iter_data *data = _data;
1995 	struct iwl_mvm *mvm = data->mvm;
1996 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1997 	struct iwl_host_cmd hcmd = {
1998 		.id = MCAST_FILTER_CMD,
1999 		.flags = CMD_ASYNC,
2000 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
2001 	};
2002 	int ret, len;
2003 
2004 	/* if we don't have free ports, mcast frames will be dropped */
2005 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
2006 		return;
2007 
2008 	if (vif->type != NL80211_IFTYPE_STATION ||
2009 	    !vif->cfg.assoc)
2010 		return;
2011 
2012 	cmd->port_id = data->port_id++;
2013 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
2014 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
2015 
2016 	hcmd.len[0] = len;
2017 	hcmd.data[0] = cmd;
2018 
2019 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2020 	if (ret)
2021 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
2022 }
2023 
2024 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
2025 {
2026 	struct iwl_mvm_mc_iter_data iter_data = {
2027 		.mvm = mvm,
2028 	};
2029 	int ret;
2030 
2031 	lockdep_assert_held(&mvm->mutex);
2032 
2033 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
2034 		return;
2035 
2036 	ieee80211_iterate_active_interfaces_atomic(
2037 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2038 		iwl_mvm_mc_iface_iterator, &iter_data);
2039 
2040 	/*
2041 	 * Send a (synchronous) ech command so that we wait for the
2042 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
2043 	 * the interface iterator. Otherwise, we might get here over
2044 	 * and over again (by userspace just sending a lot of these)
2045 	 * and the CPU can send them faster than the firmware can
2046 	 * process them.
2047 	 * Note that the CPU is still faster - but with this we'll
2048 	 * actually send fewer commands overall because the CPU will
2049 	 * not schedule the work in mac80211 as frequently if it's
2050 	 * still running when rescheduled (possibly multiple times).
2051 	 */
2052 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
2053 	if (ret)
2054 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
2055 }
2056 
2057 u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
2058 			      struct netdev_hw_addr_list *mc_list)
2059 {
2060 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2061 	struct iwl_mcast_filter_cmd *cmd;
2062 	struct netdev_hw_addr *addr;
2063 	int addr_count;
2064 	bool pass_all;
2065 	int len;
2066 
2067 	addr_count = netdev_hw_addr_list_count(mc_list);
2068 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
2069 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
2070 	if (pass_all)
2071 		addr_count = 0;
2072 
2073 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
2074 	cmd = kzalloc(len, GFP_ATOMIC);
2075 	if (!cmd)
2076 		return 0;
2077 
2078 	if (pass_all) {
2079 		cmd->pass_all = 1;
2080 		return (u64)(unsigned long)cmd;
2081 	}
2082 
2083 	netdev_hw_addr_list_for_each(addr, mc_list) {
2084 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
2085 				   cmd->count, addr->addr);
2086 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
2087 		       addr->addr, ETH_ALEN);
2088 		cmd->count++;
2089 	}
2090 
2091 	return (u64)(unsigned long)cmd;
2092 }
2093 
2094 void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
2095 			      unsigned int changed_flags,
2096 			      unsigned int *total_flags, u64 multicast)
2097 {
2098 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2099 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
2100 
2101 	guard(mvm)(mvm);
2102 
2103 	/* replace previous configuration */
2104 	kfree(mvm->mcast_filter_cmd);
2105 	mvm->mcast_filter_cmd = cmd;
2106 
2107 	if (!cmd)
2108 		goto out;
2109 
2110 	if (changed_flags & FIF_ALLMULTI)
2111 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
2112 
2113 	if (cmd->pass_all)
2114 		cmd->count = 0;
2115 
2116 	iwl_mvm_recalc_multicast(mvm);
2117 out:
2118 	*total_flags = 0;
2119 }
2120 
2121 static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
2122 					struct ieee80211_vif *vif,
2123 					unsigned int filter_flags,
2124 					unsigned int changed_flags)
2125 {
2126 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2127 
2128 	/* We support only filter for probe requests */
2129 	if (!(changed_flags & FIF_PROBE_REQ))
2130 		return;
2131 
2132 	/* Supported only for p2p client interfaces */
2133 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
2134 	    !vif->p2p)
2135 		return;
2136 
2137 	guard(mvm)(mvm);
2138 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2139 }
2140 
2141 int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2142 {
2143 	struct iwl_mu_group_mgmt_cmd cmd = {};
2144 
2145 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
2146 	       WLAN_MEMBERSHIP_LEN);
2147 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
2148 	       WLAN_USER_POSITION_LEN);
2149 
2150 	return iwl_mvm_send_cmd_pdu(mvm,
2151 				    WIDE_ID(DATA_PATH_GROUP,
2152 					    UPDATE_MU_GROUPS_CMD),
2153 				    0, sizeof(cmd), &cmd);
2154 }
2155 
2156 static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
2157 					   struct ieee80211_vif *vif)
2158 {
2159 	if (vif->bss_conf.mu_mimo_owner) {
2160 		struct iwl_mu_group_mgmt_notif *notif = _data;
2161 
2162 		/*
2163 		 * MU-MIMO Group Id action frame is little endian. We treat
2164 		 * the data received from firmware as if it came from the
2165 		 * action frame, so no conversion is needed.
2166 		 */
2167 		ieee80211_update_mu_groups(vif, 0,
2168 					   (u8 *)&notif->membership_status,
2169 					   (u8 *)&notif->user_position);
2170 	}
2171 }
2172 
2173 void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
2174 			       struct iwl_rx_cmd_buffer *rxb)
2175 {
2176 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2177 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
2178 
2179 	ieee80211_iterate_active_interfaces_atomic(
2180 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2181 			iwl_mvm_mu_mimo_iface_iterator, notif);
2182 }
2183 
2184 static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2185 {
2186 	u8 byte_num = ppe_pos_bit / 8;
2187 	u8 bit_num = ppe_pos_bit % 8;
2188 	u8 residue_bits;
2189 	u8 res;
2190 
2191 	if (bit_num <= 5)
2192 		return (ppe[byte_num] >> bit_num) &
2193 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2194 
2195 	/*
2196 	 * If bit_num > 5, we have to combine bits with next byte.
2197 	 * Calculate how many bits we need to take from current byte (called
2198 	 * here "residue_bits"), and add them to bits from next byte.
2199 	 */
2200 
2201 	residue_bits = 8 - bit_num;
2202 
2203 	res = (ppe[byte_num + 1] &
2204 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2205 	      residue_bits;
2206 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2207 
2208 	return res;
2209 }
2210 
2211 static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
2212 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
2213 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
2214 			      bool inheritance)
2215 {
2216 	int i;
2217 
2218 	/*
2219 	* FW currently supports only nss == MAX_HE_SUPP_NSS
2220 	*
2221 	* If nss > MAX: we can ignore values we don't support
2222 	* If nss < MAX: we can set zeros in other streams
2223 	*/
2224 	if (nss > MAX_HE_SUPP_NSS) {
2225 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2226 			       MAX_HE_SUPP_NSS);
2227 		nss = MAX_HE_SUPP_NSS;
2228 	}
2229 
2230 	for (i = 0; i < nss; i++) {
2231 		u8 ru_index_tmp = ru_index_bitmap << 1;
2232 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2233 		u8 bw;
2234 
2235 		for (bw = 0;
2236 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2237 		     bw++) {
2238 			ru_index_tmp >>= 1;
2239 
2240 			/*
2241 			* According to the 11be spec, if for a specific BW the PPE Thresholds
2242 			* isn't present - it should inherit the thresholds from the last
2243 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2244 			* this inheritance - continue in this case
2245 			*/
2246 			if (!(ru_index_tmp & 1)) {
2247 				if (inheritance)
2248 					goto set_thresholds;
2249 				else
2250 					continue;
2251 			}
2252 
2253 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2254 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2255 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2256 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2257 
2258 set_thresholds:
2259 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2260 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2261 		}
2262 	}
2263 }
2264 
2265 static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
2266 					    struct ieee80211_link_sta *link_sta,
2267 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2268 					    bool inheritance)
2269 {
2270 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
2271 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2272 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2273 	u8 ru_index_bitmap =
2274 		u8_get_bits(*ppe,
2275 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2276 	/* Starting after PPE header */
2277 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2278 
2279 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2280 			  inheritance);
2281 }
2282 
2283 static int
2284 iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
2285 					 u8 nominal_padding)
2286 {
2287 	int low_th = -1;
2288 	int high_th = -1;
2289 	int i;
2290 
2291 	/* all the macros are the same for EHT and HE */
2292 	switch (nominal_padding) {
2293 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2294 		low_th = IWL_HE_PKT_EXT_NONE;
2295 		high_th = IWL_HE_PKT_EXT_NONE;
2296 		break;
2297 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2298 		low_th = IWL_HE_PKT_EXT_BPSK;
2299 		high_th = IWL_HE_PKT_EXT_NONE;
2300 		break;
2301 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2302 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2303 		low_th = IWL_HE_PKT_EXT_NONE;
2304 		high_th = IWL_HE_PKT_EXT_BPSK;
2305 		break;
2306 	}
2307 
2308 	if (low_th < 0 || high_th < 0)
2309 		return -EINVAL;
2310 
2311 	/* Set the PPE thresholds accordingly */
2312 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2313 		u8 bw;
2314 
2315 		for (bw = 0;
2316 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2317 			bw++) {
2318 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2319 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2320 		}
2321 	}
2322 
2323 	return 0;
2324 }
2325 
2326 static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2327 					 u8 nominal_padding)
2328 {
2329 	int i;
2330 
2331 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2332 		u8 bw;
2333 
2334 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2335 		     bw++) {
2336 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2337 
2338 			if (nominal_padding >
2339 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2340 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2341 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2342 			else if (nominal_padding ==
2343 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2344 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2345 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2346 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2347 		}
2348 	}
2349 }
2350 
2351 /* Set the pkt_ext field according to PPE Thresholds element */
2352 int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
2353 			    struct ieee80211_link_sta *link_sta,
2354 			    struct iwl_he_pkt_ext_v2 *pkt_ext)
2355 {
2356 	u8 nominal_padding;
2357 	int i, ret = 0;
2358 
2359 	if (WARN_ON(!link_sta))
2360 		return -EINVAL;
2361 
2362 	/* Initialize the PPE thresholds to "None" (7), as described in Table
2363 	 * 9-262ac of 80211.ax/D3.0.
2364 	 */
2365 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
2366 	       sizeof(struct iwl_he_pkt_ext_v2));
2367 
2368 	if (link_sta->eht_cap.has_eht) {
2369 		nominal_padding =
2370 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
2371 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
2372 
2373 		/* If PPE Thresholds exists, parse them into a FW-familiar
2374 		 * format.
2375 		 */
2376 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
2377 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
2378 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
2379 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
2380 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
2381 			u8 ru_index_bitmap =
2382 				u16_get_bits(*ppe,
2383 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2384 			 /* Starting after PPE header */
2385 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
2386 
2387 			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
2388 					  ppe, ppe_pos_bit, true);
2389 		/* EHT PPE Thresholds doesn't exist - set the API according to
2390 		 * HE PPE Tresholds
2391 		 */
2392 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2393 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2394 			/* Even though HE Capabilities IE doesn't contain PPE
2395 			 * Thresholds for BW 320Mhz, thresholds for this BW will
2396 			 * be filled in with the same values as 160Mhz, due to
2397 			 * the inheritance, as required.
2398 			 */
2399 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2400 							true);
2401 
2402 			/* According to the requirements, for MCSs 12-13 the
2403 			 * maximum value between HE PPE Threshold and Common
2404 			 * Nominal Packet Padding needs to be taken
2405 			 */
2406 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
2407 
2408 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
2409 		 * take the Thresholds from Common Nominal Packet Padding field
2410 		 */
2411 		} else {
2412 			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2413 								       nominal_padding);
2414 		}
2415 	} else if (link_sta->he_cap.has_he) {
2416 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2417 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2418 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2419 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2420 							false);
2421 		/* PPE Thresholds doesn't exist - set the API PPE values
2422 		 * according to Common Nominal Packet Padding field.
2423 		 */
2424 		} else {
2425 			nominal_padding =
2426 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
2427 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
2428 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
2429 				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2430 									       nominal_padding);
2431 		}
2432 	}
2433 
2434 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2435 		int bw;
2436 
2437 		for (bw = 0;
2438 		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
2439 		     bw++) {
2440 			u8 *qam_th =
2441 				&pkt_ext->pkt_ext_qam_th[i][bw][0];
2442 
2443 			IWL_DEBUG_HT(mvm,
2444 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
2445 				     i, bw, qam_th[0], qam_th[1]);
2446 		}
2447 	}
2448 	return ret;
2449 }
2450 
2451 /*
2452  * This function sets the MU EDCA parameters ans returns whether MU EDCA
2453  * is enabled or not
2454  */
2455 bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2456 				   const struct iwl_mvm_vif_link_info *link_info,
2457 				   struct iwl_he_backoff_conf *trig_based_txf)
2458 {
2459 	int i;
2460 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2461 	bool mu_edca_enabled = true;
2462 
2463 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2464 		const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2465 			&link_info->queue_params[i].mu_edca_param_rec;
2466 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2467 
2468 		if (!link_info->queue_params[i].mu_edca) {
2469 			mu_edca_enabled = false;
2470 			break;
2471 		}
2472 
2473 		trig_based_txf[ac].cwmin =
2474 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
2475 		trig_based_txf[ac].cwmax =
2476 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
2477 		trig_based_txf[ac].aifsn =
2478 			cpu_to_le16(mu_edca->aifsn & 0xf);
2479 		trig_based_txf[ac].mu_time =
2480 			cpu_to_le16(mu_edca->mu_edca_timer);
2481 	}
2482 
2483 	return mu_edca_enabled;
2484 }
2485 
2486 bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2487 {
2488 	const struct ieee80211_supported_band *sband;
2489 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
2490 
2491 	/* This capability is the same for all bands,
2492 	 * so take it from one of them.
2493 	 */
2494 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
2495 	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
2496 
2497 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
2498 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
2499 }
2500 
2501 __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
2502 				 struct ieee80211_link_sta *link_sta)
2503 {
2504 	u8 *mac_cap_info =
2505 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
2506 	__le32 htc_flags = 0;
2507 
2508 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
2509 		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2510 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2511 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2512 		u8 link_adap =
2513 			((mac_cap_info[2] &
2514 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2515 			 (mac_cap_info[1] &
2516 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2517 
2518 		if (link_adap == 2)
2519 			htc_flags |=
2520 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2521 		else if (link_adap == 3)
2522 			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2523 	}
2524 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2525 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2526 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2527 		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2528 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2529 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2530 
2531 	return htc_flags;
2532 }
2533 
2534 static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2535 			       struct ieee80211_vif *vif, u8 sta_id)
2536 {
2537 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2538 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2539 		.sta_id = sta_id,
2540 		.tid_limit = IWL_MAX_TID_COUNT,
2541 		.bss_color = vif->bss_conf.he_bss_color.color,
2542 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2543 		.frame_time_rts_th =
2544 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2545 	};
2546 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
2547 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
2548 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
2549 	int size;
2550 	struct ieee80211_sta *sta;
2551 	u32 flags;
2552 	int i;
2553 	void *cmd;
2554 
2555 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
2556 		ver = 1;
2557 
2558 	switch (ver) {
2559 	case 1:
2560 		/* same layout as v2 except some data at the end */
2561 		cmd = &sta_ctxt_cmd_v2;
2562 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
2563 		break;
2564 	case 2:
2565 		cmd = &sta_ctxt_cmd_v2;
2566 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
2567 		break;
2568 	case 3:
2569 		cmd = &sta_ctxt_cmd;
2570 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
2571 		break;
2572 	default:
2573 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
2574 		return;
2575 	}
2576 
2577 	rcu_read_lock();
2578 
2579 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2580 	if (IS_ERR_OR_NULL(sta)) {
2581 		rcu_read_unlock();
2582 		WARN(1, "Can't find STA to configure HE\n");
2583 		return;
2584 	}
2585 
2586 	if (!sta->deflink.he_cap.has_he) {
2587 		rcu_read_unlock();
2588 		return;
2589 	}
2590 
2591 	flags = 0;
2592 
2593 	/* Block 26-tone RU OFDMA transmissions */
2594 	if (mvmvif->deflink.he_ru_2mhz_block)
2595 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
2596 
2597 	/* HTC flags */
2598 	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2599 
2600 	/* PPE Thresholds */
2601 	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2602 		flags |= STA_CTXT_HE_PACKET_EXT;
2603 
2604 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2605 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
2606 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2607 
2608 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2609 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
2610 		flags |= STA_CTXT_HE_ACK_ENABLED;
2611 
2612 	rcu_read_unlock();
2613 
2614 	if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
2615 					  &sta_ctxt_cmd.trig_based_txf[0]))
2616 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2617 
2618 	if (vif->bss_conf.uora_exists) {
2619 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2620 
2621 		sta_ctxt_cmd.rand_alloc_ecwmin =
2622 			vif->bss_conf.uora_ocw_range & 0x7;
2623 		sta_ctxt_cmd.rand_alloc_ecwmax =
2624 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2625 	}
2626 
2627 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2628 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2629 
2630 	if (vif->bss_conf.nontransmitted) {
2631 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2632 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2633 				vif->bss_conf.transmitter_bssid);
2634 		sta_ctxt_cmd.max_bssid_indicator =
2635 			vif->bss_conf.bssid_indicator;
2636 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2637 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2638 		sta_ctxt_cmd.profile_periodicity =
2639 			vif->bss_conf.profile_periodicity;
2640 	}
2641 
2642 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2643 
2644 	if (ver < 3) {
2645 		/* fields before pkt_ext */
2646 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
2647 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
2648 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
2649 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
2650 
2651 		/* pkt_ext */
2652 		for (i = 0;
2653 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
2654 		     i++) {
2655 			u8 bw;
2656 
2657 			for (bw = 0;
2658 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
2659 			     bw++) {
2660 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
2661 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
2662 
2663 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
2664 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
2665 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
2666 			}
2667 		}
2668 
2669 		/* fields after pkt_ext */
2670 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
2671 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
2672 			     sizeof(sta_ctxt_cmd_v2) -
2673 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
2674 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
2675 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
2676 		       (u8 *)&sta_ctxt_cmd +
2677 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
2678 		       sizeof(sta_ctxt_cmd) -
2679 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
2680 		sta_ctxt_cmd_v2.reserved3 = 0;
2681 	}
2682 
2683 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2684 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2685 }
2686 
2687 void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2688 			   u32 duration_override, unsigned int link_id)
2689 {
2690 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2691 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2692 
2693 	if (duration_override > duration)
2694 		duration = duration_override;
2695 
2696 	/* Try really hard to protect the session and hear a beacon
2697 	 * The new session protection command allows us to protect the
2698 	 * session for a much longer time since the firmware will internally
2699 	 * create two events: a 300TU one with a very high priority that
2700 	 * won't be fragmented which should be enough for 99% of the cases,
2701 	 * and another one (which we configure here to be 900TU long) which
2702 	 * will have a slightly lower priority, but more importantly, can be
2703 	 * fragmented so that it'll allow other activities to run.
2704 	 */
2705 	if (fw_has_capa(&mvm->fw->ucode_capa,
2706 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2707 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2708 						    min_duration, false,
2709 						    link_id);
2710 	else
2711 		iwl_mvm_protect_session(mvm, vif, duration,
2712 					min_duration, 500, false);
2713 }
2714 
2715 /* Handle association common part to MLD and non-MLD modes */
2716 void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2717 					    struct ieee80211_vif *vif,
2718 					    u64 changes)
2719 {
2720 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2721 	int ret;
2722 	int link_id;
2723 
2724 	/* The firmware tracks the MU-MIMO group on its own.
2725 	 * However, on HW restart we should restore this data.
2726 	 */
2727 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2728 	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2729 		ret = iwl_mvm_update_mu_groups(mvm, vif);
2730 		if (ret)
2731 			IWL_ERR(mvm,
2732 				"failed to update VHT MU_MIMO groups\n");
2733 	}
2734 
2735 	iwl_mvm_recalc_multicast(mvm);
2736 
2737 	/* reset rssi values */
2738 	for_each_mvm_vif_valid_link(mvmvif, link_id)
2739 		mvmvif->link[link_id]->bf_data.ave_beacon_signal = 0;
2740 
2741 	iwl_mvm_bt_coex_vif_change(mvm);
2742 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2743 					    IEEE80211_SMPS_AUTOMATIC);
2744 	if (fw_has_capa(&mvm->fw->ucode_capa,
2745 			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2746 		iwl_mvm_config_scan(mvm);
2747 }
2748 
2749 /* Execute the common part for MLD and non-MLD modes */
2750 void
2751 iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2752 					struct ieee80211_vif *vif,
2753 					struct ieee80211_bss_conf *link_conf,
2754 					u64 changes)
2755 {
2756 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2757 	int ret;
2758 
2759 	if (changes & BSS_CHANGED_BEACON_INFO) {
2760 		/* We received a beacon from the associated AP so
2761 		 * remove the session protection.
2762 		 */
2763 		iwl_mvm_stop_session_protection(mvm, vif);
2764 
2765 		iwl_mvm_sf_update(mvm, vif, false);
2766 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
2767 	}
2768 
2769 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2770 		       /* Send power command on every beacon change,
2771 			* because we may have not enabled beacon abort yet.
2772 			*/
2773 		       BSS_CHANGED_BEACON_INFO)) {
2774 		ret = iwl_mvm_power_update_mac(mvm);
2775 		if (ret)
2776 			IWL_ERR(mvm, "failed to update power mode\n");
2777 	}
2778 
2779 	if (changes & BSS_CHANGED_CQM) {
2780 		struct iwl_mvm_vif_link_info *link_info =
2781 			mvmvif->link[link_conf->link_id];
2782 
2783 		IWL_DEBUG_MAC80211(mvm, "CQM info_changed\n");
2784 		if (link_info)
2785 			link_info->bf_data.last_cqm_event = 0;
2786 
2787 		if (mvmvif->bf_enabled) {
2788 			/* FIXME: need to update per link when FW API will
2789 			 * support it
2790 			 */
2791 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
2792 			if (ret)
2793 				IWL_ERR(mvm,
2794 					"failed to update CQM thresholds\n");
2795 		}
2796 	}
2797 
2798 	if (changes & BSS_CHANGED_BANDWIDTH)
2799 		iwl_mvm_update_link_smps(vif, link_conf);
2800 
2801 	if (changes & BSS_CHANGED_TPE) {
2802 		IWL_DEBUG_CALIB(mvm, "Changing TPE\n");
2803 		iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif,
2804 							link_conf,
2805 							false);
2806 	}
2807 }
2808 
2809 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2810 					     struct ieee80211_vif *vif,
2811 					     struct ieee80211_bss_conf *bss_conf,
2812 					     u64 changes)
2813 {
2814 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2815 	int ret;
2816 	int i;
2817 
2818 	/*
2819 	 * Re-calculate the tsf id, as the leader-follower relations depend
2820 	 * on the beacon interval, which was not known when the station
2821 	 * interface was added.
2822 	 */
2823 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2824 		if ((vif->bss_conf.he_support &&
2825 		     !iwlwifi_mod_params.disable_11ax))
2826 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2827 
2828 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2829 	}
2830 
2831 	/* Update MU EDCA params */
2832 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2833 	    vif->cfg.assoc &&
2834 	    (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax))
2835 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2836 
2837 	/*
2838 	 * If we're not associated yet, take the (new) BSSID before associating
2839 	 * so the firmware knows. If we're already associated, then use the old
2840 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2841 	 * branch for disassociation below.
2842 	 */
2843 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2844 		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2845 
2846 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2847 	if (ret)
2848 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2849 
2850 	/* after sending it once, adopt mac80211 data */
2851 	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2852 	mvmvif->associated = vif->cfg.assoc;
2853 
2854 	if (changes & BSS_CHANGED_ASSOC) {
2855 		if (vif->cfg.assoc) {
2856 			/* clear statistics to get clean beacon counter */
2857 			iwl_mvm_request_statistics(mvm, true);
2858 			for_each_mvm_vif_valid_link(mvmvif, i)
2859 				memset(&mvmvif->link[i]->beacon_stats, 0,
2860 				       sizeof(mvmvif->link[i]->beacon_stats));
2861 
2862 			/* add quota for this interface */
2863 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2864 			if (ret) {
2865 				IWL_ERR(mvm, "failed to update quotas\n");
2866 				return;
2867 			}
2868 
2869 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2870 				     &mvm->status) &&
2871 			    !fw_has_capa(&mvm->fw->ucode_capa,
2872 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2873 				/*
2874 				 * If we're restarting then the firmware will
2875 				 * obviously have lost synchronisation with
2876 				 * the AP. It will attempt to synchronise by
2877 				 * itself, but we can make it more reliable by
2878 				 * scheduling a session protection time event.
2879 				 *
2880 				 * The firmware needs to receive a beacon to
2881 				 * catch up with synchronisation, use 110% of
2882 				 * the beacon interval.
2883 				 *
2884 				 * Set a large maximum delay to allow for more
2885 				 * than a single interface.
2886 				 *
2887 				 * For new firmware versions, rely on the
2888 				 * firmware. This is relevant for DCM scenarios
2889 				 * only anyway.
2890 				 */
2891 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2892 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2893 							5 * dur, false);
2894 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2895 					     &mvm->status) &&
2896 				   !vif->bss_conf.dtim_period) {
2897 				/*
2898 				 * If we're not restarting and still haven't
2899 				 * heard a beacon (dtim period unknown) then
2900 				 * make sure we still have enough minimum time
2901 				 * remaining in the time event, since the auth
2902 				 * might actually have taken quite a while
2903 				 * (especially for SAE) and so the remaining
2904 				 * time could be small without us having heard
2905 				 * a beacon yet.
2906 				 */
2907 				iwl_mvm_protect_assoc(mvm, vif, 0, 0);
2908 			}
2909 
2910 			iwl_mvm_sf_update(mvm, vif, false);
2911 			iwl_mvm_power_vif_assoc(mvm, vif);
2912 			if (vif->p2p) {
2913 				iwl_mvm_update_smps(mvm, vif,
2914 						    IWL_MVM_SMPS_REQ_PROT,
2915 						    IEEE80211_SMPS_DYNAMIC, 0);
2916 			}
2917 		} else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
2918 			iwl_mvm_mei_host_disassociated(mvm);
2919 			/*
2920 			 * If update fails - SF might be running in associated
2921 			 * mode while disassociated - which is forbidden.
2922 			 */
2923 			ret = iwl_mvm_sf_update(mvm, vif, false);
2924 			WARN_ONCE(ret &&
2925 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
2926 					    &mvm->status),
2927 				  "Failed to update SF upon disassociation\n");
2928 
2929 			/*
2930 			 * If we get an assert during the connection (after the
2931 			 * station has been added, but before the vif is set
2932 			 * to associated), mac80211 will re-add the station and
2933 			 * then configure the vif. Since the vif is not
2934 			 * associated, we would remove the station here and
2935 			 * this would fail the recovery.
2936 			 */
2937 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2938 				      &mvm->status)) {
2939 				/* first remove remaining keys */
2940 				iwl_mvm_sec_key_remove_ap(mvm, vif,
2941 							  &mvmvif->deflink, 0);
2942 
2943 				/*
2944 				 * Remove AP station now that
2945 				 * the MAC is unassoc
2946 				 */
2947 				ret = iwl_mvm_rm_sta_id(mvm, vif,
2948 							mvmvif->deflink.ap_sta_id);
2949 				if (ret)
2950 					IWL_ERR(mvm,
2951 						"failed to remove AP station\n");
2952 
2953 				mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
2954 			}
2955 
2956 			/* remove quota for this interface */
2957 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2958 			if (ret)
2959 				IWL_ERR(mvm, "failed to update quotas\n");
2960 
2961 			/* this will take the cleared BSSID from bss_conf */
2962 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2963 			if (ret)
2964 				IWL_ERR(mvm,
2965 					"failed to update MAC %pM (clear after unassoc)\n",
2966 					vif->addr);
2967 		}
2968 
2969 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
2970 	}
2971 
2972 	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
2973 						changes);
2974 }
2975 
2976 bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2977 				  struct ieee80211_vif *vif,
2978 				  int *ret)
2979 {
2980 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2981 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2982 	int i;
2983 
2984 	lockdep_assert_held(&mvm->mutex);
2985 
2986 	mvmvif->ap_assoc_sta_count = 0;
2987 
2988 	/* must be set before quota calculations */
2989 	mvmvif->ap_ibss_active = true;
2990 
2991 	/* send all the early keys to the device now */
2992 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2993 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2994 
2995 		if (!key)
2996 			continue;
2997 
2998 		mvmvif->ap_early_keys[i] = NULL;
2999 
3000 		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
3001 		if (*ret)
3002 			return true;
3003 	}
3004 
3005 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
3006 		iwl_mvm_vif_set_low_latency(mvmvif, true,
3007 					    LOW_LATENCY_VIF_TYPE);
3008 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
3009 	}
3010 
3011 	/* power updated needs to be done before quotas */
3012 	iwl_mvm_power_update_mac(mvm);
3013 
3014 	return false;
3015 }
3016 
3017 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
3018 				 struct ieee80211_vif *vif,
3019 				 struct ieee80211_bss_conf *link_conf)
3020 {
3021 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3022 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3023 	int ret;
3024 
3025 	mutex_lock(&mvm->mutex);
3026 
3027 	/*
3028 	 * Re-calculate the tsf id, as the leader-follower relations depend on
3029 	 * the beacon interval, which was not known when the AP interface
3030 	 * was added.
3031 	 */
3032 	if (vif->type == NL80211_IFTYPE_AP)
3033 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
3034 
3035 	/* For older devices need to send beacon template before adding mac
3036 	 * context. For the newer, the beacon is a resource that belongs to a
3037 	 * MAC, so need to send beacon template after adding the mac.
3038 	 */
3039 	if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
3040 		/* Add the mac context */
3041 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3042 		if (ret)
3043 			goto out_unlock;
3044 
3045 		/* Send the beacon template */
3046 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
3047 		if (ret)
3048 			goto out_unlock;
3049 	} else {
3050 		/* Send the beacon template */
3051 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
3052 		if (ret)
3053 			goto out_unlock;
3054 
3055 		/* Add the mac context */
3056 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3057 		if (ret)
3058 			goto out_unlock;
3059 	}
3060 
3061 	/* Perform the binding */
3062 	ret = iwl_mvm_binding_add_vif(mvm, vif);
3063 	if (ret)
3064 		goto out_remove;
3065 
3066 	/*
3067 	 * This is not very nice, but the simplest:
3068 	 * For older FWs adding the mcast sta before the bcast station may
3069 	 * cause assert 0x2b00.
3070 	 * This is fixed in later FW so make the order of removal depend on
3071 	 * the TLV
3072 	 */
3073 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
3074 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
3075 		if (ret)
3076 			goto out_unbind;
3077 		/*
3078 		 * Send the bcast station. At this stage the TBTT and DTIM time
3079 		 * events are added and applied to the scheduler
3080 		 */
3081 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3082 		if (ret) {
3083 			iwl_mvm_rm_mcast_sta(mvm, vif);
3084 			goto out_unbind;
3085 		}
3086 	} else {
3087 		/*
3088 		 * Send the bcast station. At this stage the TBTT and DTIM time
3089 		 * events are added and applied to the scheduler
3090 		 */
3091 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3092 		if (ret)
3093 			goto out_unbind;
3094 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
3095 		if (ret) {
3096 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
3097 			goto out_unbind;
3098 		}
3099 	}
3100 
3101 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
3102 		goto out_failed;
3103 
3104 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
3105 	if (ret)
3106 		goto out_failed;
3107 
3108 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3109 	if (vif->p2p && mvm->p2p_device_vif)
3110 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3111 
3112 	iwl_mvm_bt_coex_vif_change(mvm);
3113 
3114 	/* we don't support TDLS during DCM */
3115 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3116 		iwl_mvm_teardown_tdls_peers(mvm);
3117 
3118 	iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
3119 
3120 	goto out_unlock;
3121 
3122 out_failed:
3123 	iwl_mvm_power_update_mac(mvm);
3124 	mvmvif->ap_ibss_active = false;
3125 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3126 	iwl_mvm_rm_mcast_sta(mvm, vif);
3127 out_unbind:
3128 	iwl_mvm_binding_remove_vif(mvm, vif);
3129 out_remove:
3130 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3131 out_unlock:
3132 	mutex_unlock(&mvm->mutex);
3133 	return ret;
3134 }
3135 
3136 static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
3137 			    struct ieee80211_vif *vif,
3138 			    struct ieee80211_bss_conf *link_conf)
3139 {
3140 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
3141 }
3142 
3143 static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
3144 			      struct ieee80211_vif *vif)
3145 {
3146 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
3147 }
3148 
3149 /* Common part for MLD and non-MLD ops */
3150 void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
3151 				 struct ieee80211_vif *vif)
3152 {
3153 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3154 
3155 	lockdep_assert_held(&mvm->mutex);
3156 
3157 	iwl_mvm_prepare_mac_removal(mvm, vif);
3158 
3159 	/* Handle AP stop while in CSA */
3160 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
3161 		iwl_mvm_remove_time_event(mvm, mvmvif,
3162 					  &mvmvif->time_event_data);
3163 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
3164 		mvmvif->csa_countdown = false;
3165 	}
3166 
3167 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
3168 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
3169 		mvm->csa_tx_block_bcn_timeout = 0;
3170 	}
3171 
3172 	mvmvif->ap_ibss_active = false;
3173 	mvm->ap_last_beacon_gp2 = 0;
3174 
3175 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
3176 		iwl_mvm_vif_set_low_latency(mvmvif, false,
3177 					    LOW_LATENCY_VIF_TYPE);
3178 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
3179 	}
3180 
3181 	iwl_mvm_bt_coex_vif_change(mvm);
3182 }
3183 
3184 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
3185 				 struct ieee80211_vif *vif,
3186 				 struct ieee80211_bss_conf *link_conf)
3187 {
3188 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3189 
3190 	guard(mvm)(mvm);
3191 
3192 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
3193 
3194 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3195 	if (vif->p2p && mvm->p2p_device_vif)
3196 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3197 
3198 	iwl_mvm_update_quotas(mvm, false, NULL);
3199 
3200 	iwl_mvm_ftm_responder_clear(mvm, vif);
3201 
3202 	/*
3203 	 * This is not very nice, but the simplest:
3204 	 * For older FWs removing the mcast sta before the bcast station may
3205 	 * cause assert 0x2b00.
3206 	 * This is fixed in later FW (which will stop beaconing when removing
3207 	 * bcast station).
3208 	 * So make the order of removal depend on the TLV
3209 	 */
3210 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3211 		iwl_mvm_rm_mcast_sta(mvm, vif);
3212 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3213 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3214 		iwl_mvm_rm_mcast_sta(mvm, vif);
3215 	iwl_mvm_binding_remove_vif(mvm, vif);
3216 
3217 	iwl_mvm_power_update_mac(mvm);
3218 
3219 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3220 }
3221 
3222 static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
3223 			    struct ieee80211_vif *vif,
3224 			    struct ieee80211_bss_conf *link_conf)
3225 {
3226 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
3227 }
3228 
3229 static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
3230 			      struct ieee80211_vif *vif)
3231 {
3232 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3233 }
3234 
3235 static void
3236 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3237 				 struct ieee80211_vif *vif,
3238 				 struct ieee80211_bss_conf *bss_conf,
3239 				 u64 changes)
3240 {
3241 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3242 
3243 	/* Changes will be applied when the AP/IBSS is started */
3244 	if (!mvmvif->ap_ibss_active)
3245 		return;
3246 
3247 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3248 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3249 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3250 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3251 
3252 	/* Need to send a new beacon template to the FW */
3253 	if (changes & BSS_CHANGED_BEACON &&
3254 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3255 		IWL_WARN(mvm, "Failed updating beacon data\n");
3256 
3257 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3258 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3259 
3260 		if (ret)
3261 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3262 				 ret);
3263 	}
3264 
3265 }
3266 
3267 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3268 				     struct ieee80211_vif *vif,
3269 				     struct ieee80211_bss_conf *bss_conf,
3270 				     u64 changes)
3271 {
3272 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3273 
3274 	guard(mvm)(mvm);
3275 
3276 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3277 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3278 
3279 	switch (vif->type) {
3280 	case NL80211_IFTYPE_STATION:
3281 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
3282 		break;
3283 	case NL80211_IFTYPE_AP:
3284 	case NL80211_IFTYPE_ADHOC:
3285 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
3286 		break;
3287 	case NL80211_IFTYPE_MONITOR:
3288 		if (changes & BSS_CHANGED_MU_GROUPS)
3289 			iwl_mvm_update_mu_groups(mvm, vif);
3290 		break;
3291 	default:
3292 		/* shouldn't happen */
3293 		WARN_ON_ONCE(1);
3294 	}
3295 
3296 	if (changes & BSS_CHANGED_TXPOWER) {
3297 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
3298 				bss_conf->txpower);
3299 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
3300 	}
3301 }
3302 
3303 int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3304 			struct ieee80211_scan_request *hw_req)
3305 {
3306 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3307 
3308 	if (hw_req->req.n_channels == 0 ||
3309 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3310 		return -EINVAL;
3311 
3312 	guard(mvm)(mvm);
3313 	return iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3314 }
3315 
3316 void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3317 				struct ieee80211_vif *vif)
3318 {
3319 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3320 
3321 	guard(mvm)(mvm);
3322 
3323 	/* Due to a race condition, it's possible that mac80211 asks
3324 	 * us to stop a hw_scan when it's already stopped.  This can
3325 	 * happen, for instance, if we stopped the scan ourselves,
3326 	 * called ieee80211_scan_completed() and the userspace called
3327 	 * cancel scan scan before ieee80211_scan_work() could run.
3328 	 * To handle that, simply return if the scan is not running.
3329 	*/
3330 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3331 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3332 }
3333 
3334 void
3335 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3336 				  struct ieee80211_sta *sta, u16 tids,
3337 				  int num_frames,
3338 				  enum ieee80211_frame_release_type reason,
3339 				  bool more_data)
3340 {
3341 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3342 
3343 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3344 
3345 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3346 					  tids, more_data, false);
3347 }
3348 
3349 void
3350 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3351 				    struct ieee80211_sta *sta, u16 tids,
3352 				    int num_frames,
3353 				    enum ieee80211_frame_release_type reason,
3354 				    bool more_data)
3355 {
3356 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3357 
3358 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3359 
3360 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3361 					  tids, more_data, true);
3362 }
3363 
3364 static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3365 				     enum sta_notify_cmd cmd,
3366 				     struct ieee80211_sta *sta)
3367 {
3368 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3369 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3370 	unsigned long txqs = 0, tids = 0;
3371 	int tid;
3372 
3373 	/*
3374 	 * If we have TVQM then we get too high queue numbers - luckily
3375 	 * we really shouldn't get here with that because such hardware
3376 	 * should have firmware supporting buffer station offload.
3377 	 */
3378 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3379 		return;
3380 
3381 	spin_lock_bh(&mvmsta->lock);
3382 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3383 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3384 
3385 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
3386 			continue;
3387 
3388 		__set_bit(tid_data->txq_id, &txqs);
3389 
3390 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3391 			continue;
3392 
3393 		__set_bit(tid, &tids);
3394 	}
3395 
3396 	switch (cmd) {
3397 	case STA_NOTIFY_SLEEP:
3398 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3399 			ieee80211_sta_set_buffered(sta, tid, true);
3400 
3401 		if (txqs)
3402 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3403 		/*
3404 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3405 		 * queues to this station will not be transmitted. The fw will
3406 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3407 		 */
3408 		break;
3409 	case STA_NOTIFY_AWAKE:
3410 		if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
3411 			break;
3412 
3413 		if (txqs)
3414 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3415 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3416 		break;
3417 	default:
3418 		break;
3419 	}
3420 	spin_unlock_bh(&mvmsta->lock);
3421 }
3422 
3423 void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3424 			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3425 {
3426 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
3427 }
3428 
3429 void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
3430 {
3431 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3432 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
3433 	struct ieee80211_sta *sta;
3434 	struct iwl_mvm_sta *mvmsta;
3435 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
3436 
3437 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
3438 		return;
3439 
3440 	rcu_read_lock();
3441 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
3442 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
3443 		rcu_read_unlock();
3444 		return;
3445 	}
3446 
3447 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
3448 
3449 	if (!mvmsta->vif ||
3450 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
3451 		rcu_read_unlock();
3452 		return;
3453 	}
3454 
3455 	if (mvmsta->sleeping != sleeping) {
3456 		mvmsta->sleeping = sleeping;
3457 		__iwl_mvm_mac_sta_notify(mvm->hw,
3458 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
3459 			sta);
3460 		ieee80211_sta_ps_transition(sta, sleeping);
3461 	}
3462 
3463 	if (sleeping) {
3464 		switch (notif->type) {
3465 		case IWL_MVM_PM_EVENT_AWAKE:
3466 		case IWL_MVM_PM_EVENT_ASLEEP:
3467 			break;
3468 		case IWL_MVM_PM_EVENT_UAPSD:
3469 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
3470 			break;
3471 		case IWL_MVM_PM_EVENT_PS_POLL:
3472 			ieee80211_sta_pspoll(sta);
3473 			break;
3474 		default:
3475 			break;
3476 		}
3477 	}
3478 
3479 	rcu_read_unlock();
3480 }
3481 
3482 void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3483 				struct ieee80211_vif *vif,
3484 				struct ieee80211_sta *sta)
3485 {
3486 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3487 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3488 	unsigned int link_id;
3489 
3490 	/*
3491 	 * This is called before mac80211 does RCU synchronisation,
3492 	 * so here we already invalidate our internal RCU-protected
3493 	 * station pointer. The rest of the code will thus no longer
3494 	 * be able to find the station this way, and we don't rely
3495 	 * on further RCU synchronisation after the sta_state()
3496 	 * callback deleted the station.
3497 	 * Since there's mvm->mutex here, no need to have RCU lock for
3498 	 * mvm_sta->link access.
3499 	 */
3500 	guard(mvm)(mvm);
3501 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
3502 		struct iwl_mvm_link_sta *link_sta;
3503 		u32 sta_id;
3504 
3505 		if (!mvm_sta->link[link_id])
3506 			continue;
3507 
3508 		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
3509 						     lockdep_is_held(&mvm->mutex));
3510 		sta_id = link_sta->sta_id;
3511 		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
3512 			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
3513 					 ERR_PTR(-ENOENT));
3514 			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3515 		}
3516 	}
3517 }
3518 
3519 static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3520 				const u8 *bssid)
3521 {
3522 	int i;
3523 
3524 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3525 		struct iwl_mvm_tcm_mac *mdata;
3526 
3527 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3528 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3529 		mdata->opened_rx_ba_sessions = false;
3530 	}
3531 
3532 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3533 		return;
3534 
3535 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3536 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3537 		return;
3538 	}
3539 
3540 	if (!vif->p2p &&
3541 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3542 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3543 		return;
3544 	}
3545 
3546 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3547 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3548 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3549 			return;
3550 		}
3551 	}
3552 
3553 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3554 }
3555 
3556 static void
3557 iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
3558 			   struct ieee80211_vif *vif, u8 *peer_addr,
3559 			   enum nl80211_tdls_operation action)
3560 {
3561 	struct iwl_fw_dbg_trigger_tlv *trig;
3562 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
3563 
3564 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
3565 				     FW_DBG_TRIGGER_TDLS);
3566 	if (!trig)
3567 		return;
3568 
3569 	tdls_trig = (void *)trig->data;
3570 
3571 	if (!(tdls_trig->action_bitmap & BIT(action)))
3572 		return;
3573 
3574 	if (tdls_trig->peer_mode &&
3575 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
3576 		return;
3577 
3578 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
3579 				"TDLS event occurred, peer %pM, action %d",
3580 				peer_addr, action);
3581 }
3582 
3583 struct iwl_mvm_he_obss_narrow_bw_ru_data {
3584 	bool tolerated;
3585 };
3586 
3587 static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
3588 						    struct cfg80211_bss *bss,
3589 						    void *_data)
3590 {
3591 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
3592 	const struct cfg80211_bss_ies *ies;
3593 	const struct element *elem;
3594 
3595 	rcu_read_lock();
3596 	ies = rcu_dereference(bss->ies);
3597 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
3598 				  ies->len);
3599 
3600 	if (!elem || elem->datalen < 10 ||
3601 	    !(elem->data[10] &
3602 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
3603 		data->tolerated = false;
3604 	}
3605 	rcu_read_unlock();
3606 }
3607 
3608 static void
3609 iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
3610 				   struct ieee80211_vif *vif,
3611 				   unsigned int link_id,
3612 				   struct ieee80211_bss_conf *link_conf)
3613 {
3614 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3615 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
3616 		.tolerated = true,
3617 	};
3618 
3619 	if (WARN_ON_ONCE(!link_conf->chanreq.oper.chan ||
3620 			 !mvmvif->link[link_id]))
3621 		return;
3622 
3623 	if (!(link_conf->chanreq.oper.chan->flags & IEEE80211_CHAN_RADAR)) {
3624 		mvmvif->link[link_id]->he_ru_2mhz_block = false;
3625 		return;
3626 	}
3627 
3628 	cfg80211_bss_iter(hw->wiphy, &link_conf->chanreq.oper,
3629 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
3630 			  &iter_data);
3631 
3632 	/*
3633 	 * If there is at least one AP on radar channel that cannot
3634 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
3635 	 */
3636 	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
3637 }
3638 
3639 static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3640 					       struct ieee80211_vif *vif)
3641 {
3642 	struct ieee80211_supported_band *sband;
3643 	const struct ieee80211_sta_he_cap *he_cap;
3644 
3645 	if (vif->type != NL80211_IFTYPE_STATION)
3646 		return;
3647 
3648 	if (!mvm->cca_40mhz_workaround)
3649 		return;
3650 
3651 	/* decrement and check that we reached zero */
3652 	mvm->cca_40mhz_workaround--;
3653 	if (mvm->cca_40mhz_workaround)
3654 		return;
3655 
3656 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3657 
3658 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3659 
3660 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3661 
3662 	if (he_cap) {
3663 		/* we know that ours is writable */
3664 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3665 
3666 		he->he_cap_elem.phy_cap_info[0] |=
3667 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3668 	}
3669 }
3670 
3671 static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
3672 					struct ieee80211_vif *vif,
3673 					struct iwl_mvm_sta *mvm_sta)
3674 {
3675 #if IS_ENABLED(CONFIG_IWLMEI)
3676 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3677 	struct iwl_mei_conn_info conn_info = {
3678 		.ssid_len = vif->cfg.ssid_len,
3679 	};
3680 
3681 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3682 		return;
3683 
3684 	if (!mvm->mei_registered)
3685 		return;
3686 
3687 	/* FIXME: MEI needs to be updated for MLO */
3688 	if (!vif->bss_conf.chanreq.oper.chan)
3689 		return;
3690 
3691 	conn_info.channel = vif->bss_conf.chanreq.oper.chan->hw_value;
3692 
3693 	switch (mvm_sta->pairwise_cipher) {
3694 	case WLAN_CIPHER_SUITE_TKIP:
3695 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3696 		break;
3697 	case WLAN_CIPHER_SUITE_CCMP:
3698 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
3699 		break;
3700 	case WLAN_CIPHER_SUITE_GCMP:
3701 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
3702 		break;
3703 	case WLAN_CIPHER_SUITE_GCMP_256:
3704 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
3705 		break;
3706 	case 0:
3707 		/* open profile */
3708 		break;
3709 	default:
3710 		/* cipher not supported, don't send anything to iwlmei */
3711 		return;
3712 	}
3713 
3714 	switch (mvmvif->rekey_data.akm) {
3715 	case WLAN_AKM_SUITE_SAE & 0xff:
3716 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
3717 		break;
3718 	case WLAN_AKM_SUITE_PSK & 0xff:
3719 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
3720 		break;
3721 	case WLAN_AKM_SUITE_8021X & 0xff:
3722 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
3723 		break;
3724 	case 0:
3725 		/* open profile */
3726 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
3727 		break;
3728 	default:
3729 		/* auth method / AKM not supported */
3730 		/* TODO: All the FT vesions of these? */
3731 		return;
3732 	}
3733 
3734 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
3735 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
3736 
3737 	/* TODO: add support for collocated AP data */
3738 	iwl_mei_host_associated(&conn_info, NULL);
3739 #endif
3740 }
3741 
3742 static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
3743 					    struct ieee80211_vif *vif,
3744 					    bool force_assoc_off)
3745 {
3746 	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
3747 }
3748 
3749 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3750 				 struct ieee80211_vif *vif,
3751 				 struct ieee80211_sta *sta,
3752 				 enum ieee80211_sta_state old_state,
3753 				 enum ieee80211_sta_state new_state)
3754 {
3755 	static const struct iwl_mvm_sta_state_ops callbacks = {
3756 		.add_sta = iwl_mvm_add_sta,
3757 		.update_sta = iwl_mvm_update_sta,
3758 		.rm_sta = iwl_mvm_rm_sta,
3759 		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
3760 	};
3761 
3762 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
3763 					    &callbacks);
3764 }
3765 
3766 /* FIXME: temporary making two assumptions in all sta handling functions:
3767  *	(1) when setting sta state, the link exists and protected
3768  *	(2) if a link is valid in sta then it's valid in vif (can
3769  *	use same index in the link array)
3770  */
3771 static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3772 					   struct ieee80211_vif *vif,
3773 					   struct ieee80211_sta *sta)
3774 {
3775 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3776 	unsigned int link_id;
3777 
3778 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3779 		struct ieee80211_bss_conf *conf =
3780 			link_conf_dereference_check(vif, link_id);
3781 		struct ieee80211_link_sta *link_sta =
3782 			link_sta_dereference_check(sta, link_id);
3783 
3784 		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3785 			continue;
3786 
3787 		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
3788 				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3789 	}
3790 }
3791 
3792 static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
3793 				      struct ieee80211_vif *vif,
3794 				      struct ieee80211_sta *sta)
3795 {
3796 	struct ieee80211_link_sta *link_sta;
3797 	unsigned int link_id;
3798 
3799 	/* Beacon interval check - firmware will crash if the beacon
3800 	 * interval is less than 16. We can't avoid connecting at all,
3801 	 * so refuse the station state change, this will cause mac80211
3802 	 * to abandon attempts to connect to this AP, and eventually
3803 	 * wpa_s will blocklist the AP...
3804 	 */
3805 
3806 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
3807 		struct ieee80211_bss_conf *link_conf =
3808 			link_conf_dereference_protected(vif, link_id);
3809 
3810 		if (!link_conf)
3811 			continue;
3812 
3813 		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
3814 			IWL_ERR(mvm,
3815 				"Beacon interval %d for AP %pM is too small\n",
3816 				link_conf->beacon_int, link_sta->addr);
3817 			return false;
3818 		}
3819 
3820 		link_conf->he_support = link_sta->he_cap.has_he;
3821 	}
3822 
3823 	return true;
3824 }
3825 
3826 static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
3827 				       struct ieee80211_vif *vif,
3828 				       struct ieee80211_sta *sta,
3829 				       bool is_sta)
3830 {
3831 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3832 	struct ieee80211_link_sta *link_sta;
3833 	unsigned int link_id;
3834 
3835 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
3836 		struct ieee80211_bss_conf *link_conf =
3837 			link_conf_dereference_protected(vif, link_id);
3838 
3839 		if (!link_conf || !mvmvif->link[link_id])
3840 			continue;
3841 
3842 		link_conf->he_support = link_sta->he_cap.has_he;
3843 
3844 		if (is_sta) {
3845 			mvmvif->link[link_id]->he_ru_2mhz_block = false;
3846 			if (link_sta->he_cap.has_he)
3847 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3848 								   link_id,
3849 								   link_conf);
3850 		}
3851 	}
3852 }
3853 
3854 static int
3855 iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
3856 				   struct ieee80211_vif *vif,
3857 				   struct ieee80211_sta *sta,
3858 				   const struct iwl_mvm_sta_state_ops *callbacks)
3859 {
3860 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3861 	struct ieee80211_link_sta *link_sta;
3862 	unsigned int i;
3863 	int ret;
3864 
3865 	lockdep_assert_held(&mvm->mutex);
3866 
3867 	if (vif->type == NL80211_IFTYPE_STATION &&
3868 	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
3869 		return -EINVAL;
3870 
3871 	if (sta->tdls &&
3872 	    (vif->p2p ||
3873 	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
3874 	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3875 		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3876 		return -EBUSY;
3877 	}
3878 
3879 	ret = callbacks->add_sta(mvm, vif, sta);
3880 	if (sta->tdls && ret == 0) {
3881 		iwl_mvm_recalc_tdls_state(mvm, vif, true);
3882 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3883 					   NL80211_TDLS_SETUP);
3884 	}
3885 
3886 	if (ret)
3887 		return ret;
3888 
3889 	for_each_sta_active_link(vif, sta, link_sta, i)
3890 		link_sta->agg.max_rc_amsdu_len = 1;
3891 
3892 	ieee80211_sta_recalc_aggregates(sta);
3893 
3894 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3895 		mvmvif->ap_sta = sta;
3896 
3897 	/*
3898 	 * Initialize the rates here already - this really tells
3899 	 * the firmware only what the supported legacy rates are
3900 	 * (may be) since it's initialized already from what the
3901 	 * AP advertised in the beacon/probe response. This will
3902 	 * allow the firmware to send auth/assoc frames with one
3903 	 * of the supported rates already, rather than having to
3904 	 * use a mandatory rate.
3905 	 * If we're the AP, we'll just assume mandatory rates at
3906 	 * this point, but we know nothing about the STA anyway.
3907 	 */
3908 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3909 
3910 	return 0;
3911 }
3912 
3913 static int
3914 iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
3915 				struct iwl_mvm *mvm,
3916 				struct ieee80211_vif *vif,
3917 				struct ieee80211_sta *sta,
3918 				const struct iwl_mvm_sta_state_ops *callbacks)
3919 {
3920 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3921 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3922 	struct ieee80211_link_sta *link_sta;
3923 	unsigned int link_id;
3924 
3925 	lockdep_assert_held(&mvm->mutex);
3926 
3927 	if (vif->type == NL80211_IFTYPE_AP) {
3928 		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
3929 		mvmvif->ap_assoc_sta_count++;
3930 		callbacks->mac_ctxt_changed(mvm, vif, false);
3931 
3932 		/* since the below is not for MLD API, it's ok to use
3933 		 * the default bss_conf
3934 		 */
3935 		if (!mvm->mld_api_is_used &&
3936 		    (vif->bss_conf.he_support &&
3937 		     !iwlwifi_mod_params.disable_11ax))
3938 			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
3939 	} else if (vif->type == NL80211_IFTYPE_STATION) {
3940 		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
3941 
3942 		callbacks->mac_ctxt_changed(mvm, vif, false);
3943 
3944 		if (!mvm->mld_api_is_used)
3945 			goto out;
3946 
3947 		for_each_sta_active_link(vif, sta, link_sta, link_id) {
3948 			struct ieee80211_bss_conf *link_conf =
3949 				link_conf_dereference_protected(vif, link_id);
3950 
3951 			if (WARN_ON(!link_conf))
3952 				return -EINVAL;
3953 			if (!mvmvif->link[link_id])
3954 				continue;
3955 
3956 			iwl_mvm_link_changed(mvm, vif, link_conf,
3957 					     LINK_CONTEXT_MODIFY_ALL &
3958 					     ~LINK_CONTEXT_MODIFY_ACTIVE,
3959 					     true);
3960 		}
3961 	}
3962 
3963 out:
3964 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3965 
3966 	return callbacks->update_sta(mvm, vif, sta);
3967 }
3968 
3969 static int
3970 iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
3971 				      struct ieee80211_vif *vif,
3972 				      struct ieee80211_sta *sta,
3973 				      const struct iwl_mvm_sta_state_ops *callbacks)
3974 {
3975 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3976 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3977 
3978 	lockdep_assert_held(&mvm->mutex);
3979 
3980 	/* we don't support TDLS during DCM */
3981 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3982 		iwl_mvm_teardown_tdls_peers(mvm);
3983 
3984 	if (sta->tdls) {
3985 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3986 					   NL80211_TDLS_ENABLE_LINK);
3987 	} else {
3988 		/* enable beacon filtering */
3989 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
3990 
3991 		mvmvif->authorized = 1;
3992 
3993 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3994 			mvmvif->link_selection_res = vif->active_links;
3995 			mvmvif->link_selection_primary =
3996 				vif->active_links ? __ffs(vif->active_links) : 0;
3997 		}
3998 
3999 		callbacks->mac_ctxt_changed(mvm, vif, false);
4000 		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
4001 
4002 		memset(&mvmvif->last_esr_exit, 0,
4003 		       sizeof(mvmvif->last_esr_exit));
4004 
4005 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT, 0);
4006 
4007 		/* Block until FW notif will arrive */
4008 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_FW, 0);
4009 
4010 		/* when client is authorized (AP station marked as such),
4011 		 * try to enable the best link(s).
4012 		 */
4013 		if (vif->type == NL80211_IFTYPE_STATION &&
4014 		    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4015 			iwl_mvm_select_links(mvm, vif);
4016 	}
4017 
4018 	mvm_sta->authorized = true;
4019 
4020 	/* MFP is set by default before the station is authorized.
4021 	 * Clear it here in case it's not used.
4022 	 */
4023 	if (!sta->mfp) {
4024 		int ret = callbacks->update_sta(mvm, vif, sta);
4025 
4026 		if (ret)
4027 			return ret;
4028 	}
4029 
4030 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4031 
4032 	return 0;
4033 }
4034 
4035 static int
4036 iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
4037 				      struct ieee80211_vif *vif,
4038 				      struct ieee80211_sta *sta,
4039 				      const struct iwl_mvm_sta_state_ops *callbacks)
4040 {
4041 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4042 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4043 
4044 	lockdep_assert_held(&mvm->mutex);
4045 
4046 	mvmsta->authorized = false;
4047 
4048 	/* once we move into assoc state, need to update rate scale to
4049 	 * disable using wide bandwidth
4050 	 */
4051 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4052 
4053 	if (!sta->tdls) {
4054 		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
4055 		 * yet to avoid sending high prio again for a little
4056 		 * time.
4057 		 */
4058 		mvmvif->authorized = 0;
4059 		mvmvif->link_selection_res = 0;
4060 
4061 		/* disable beacon filtering */
4062 		iwl_mvm_disable_beacon_filter(mvm, vif);
4063 
4064 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4065 					  &mvmvif->prevent_esr_done_wk);
4066 
4067 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4068 					  &mvmvif->mlo_int_scan_wk);
4069 
4070 		wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
4071 
4072 		/* No need for the periodic statistics anymore */
4073 		if (ieee80211_vif_is_mld(vif) && mvmvif->esr_active)
4074 			iwl_mvm_request_periodic_system_statistics(mvm, false);
4075 	}
4076 
4077 	return 0;
4078 }
4079 
4080 /* Common part for MLD and non-MLD modes */
4081 int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
4082 				 struct ieee80211_vif *vif,
4083 				 struct ieee80211_sta *sta,
4084 				 enum ieee80211_sta_state old_state,
4085 				 enum ieee80211_sta_state new_state,
4086 				 const struct iwl_mvm_sta_state_ops *callbacks)
4087 {
4088 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4089 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4090 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4091 	struct ieee80211_link_sta *link_sta;
4092 	unsigned int link_id;
4093 	int ret;
4094 
4095 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
4096 			   sta->addr, old_state, new_state);
4097 
4098 	/*
4099 	 * If we are in a STA removal flow and in DQA mode:
4100 	 *
4101 	 * This is after the sync_rcu part, so the queues have already been
4102 	 * flushed. No more TXs on their way in mac80211's path, and no more in
4103 	 * the queues.
4104 	 * Also, we won't be getting any new TX frames for this station.
4105 	 * What we might have are deferred TX frames that need to be taken care
4106 	 * of.
4107 	 *
4108 	 * Drop any still-queued deferred-frame before removing the STA, and
4109 	 * make sure the worker is no longer handling frames for this STA.
4110 	 */
4111 	if (old_state == IEEE80211_STA_NONE &&
4112 	    new_state == IEEE80211_STA_NOTEXIST) {
4113 		flush_work(&mvm->add_stream_wk);
4114 
4115 		/*
4116 		 * No need to make sure deferred TX indication is off since the
4117 		 * worker will already remove it if it was on
4118 		 */
4119 
4120 		/*
4121 		 * Additionally, reset the 40 MHz capability if we disconnected
4122 		 * from the AP now.
4123 		 */
4124 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
4125 
4126 		/* Also free dup data just in case any assertions below fail */
4127 		kfree(mvm_sta->dup_data);
4128 	}
4129 
4130 	mutex_lock(&mvm->mutex);
4131 
4132 	/* this would be a mac80211 bug ... but don't crash, unless we had a
4133 	 * firmware crash while we were activating a link, in which case it is
4134 	 * legit to have phy_ctxt = NULL. Don't bother not to WARN if we are in
4135 	 * recovery flow since we spit tons of error messages anyway.
4136 	 */
4137 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
4138 		if (WARN_ON_ONCE(!mvmvif->link[link_id] ||
4139 				 !mvmvif->link[link_id]->phy_ctxt)) {
4140 			mutex_unlock(&mvm->mutex);
4141 			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
4142 					&mvm->status) ? 0 : -EINVAL;
4143 		}
4144 	}
4145 
4146 	/* track whether or not the station is associated */
4147 	mvm_sta->sta_state = new_state;
4148 
4149 	if (old_state == IEEE80211_STA_NOTEXIST &&
4150 	    new_state == IEEE80211_STA_NONE) {
4151 		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
4152 							 callbacks);
4153 		if (ret < 0)
4154 			goto out_unlock;
4155 	} else if (old_state == IEEE80211_STA_NONE &&
4156 		   new_state == IEEE80211_STA_AUTH) {
4157 		/*
4158 		 * EBS may be disabled due to previous failures reported by FW.
4159 		 * Reset EBS status here assuming environment has been changed.
4160 		 */
4161 		mvm->last_ebs_successful = true;
4162 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
4163 		ret = 0;
4164 	} else if (old_state == IEEE80211_STA_AUTH &&
4165 		   new_state == IEEE80211_STA_ASSOC) {
4166 		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
4167 						      callbacks);
4168 	} else if (old_state == IEEE80211_STA_ASSOC &&
4169 		   new_state == IEEE80211_STA_AUTHORIZED) {
4170 		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
4171 							    callbacks);
4172 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
4173 		   new_state == IEEE80211_STA_ASSOC) {
4174 		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
4175 							    callbacks);
4176 	} else if (old_state == IEEE80211_STA_ASSOC &&
4177 		   new_state == IEEE80211_STA_AUTH) {
4178 		if (vif->type == NL80211_IFTYPE_AP) {
4179 			mvmvif->ap_assoc_sta_count--;
4180 			callbacks->mac_ctxt_changed(mvm, vif, false);
4181 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
4182 			iwl_mvm_stop_session_protection(mvm, vif);
4183 		ret = 0;
4184 	} else if (old_state == IEEE80211_STA_AUTH &&
4185 		   new_state == IEEE80211_STA_NONE) {
4186 		ret = 0;
4187 	} else if (old_state == IEEE80211_STA_NONE &&
4188 		   new_state == IEEE80211_STA_NOTEXIST) {
4189 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
4190 			iwl_mvm_stop_session_protection(mvm, vif);
4191 			mvmvif->ap_sta = NULL;
4192 		}
4193 		ret = callbacks->rm_sta(mvm, vif, sta);
4194 		if (sta->tdls) {
4195 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
4196 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
4197 						   NL80211_TDLS_DISABLE_LINK);
4198 		}
4199 
4200 		if (unlikely(ret &&
4201 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
4202 				      &mvm->status)))
4203 			ret = 0;
4204 	} else {
4205 		ret = -EIO;
4206 	}
4207  out_unlock:
4208 	mutex_unlock(&mvm->mutex);
4209 
4210 	if (sta->tdls && ret == 0) {
4211 		if (old_state == IEEE80211_STA_NOTEXIST &&
4212 		    new_state == IEEE80211_STA_NONE)
4213 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4214 		else if (old_state == IEEE80211_STA_NONE &&
4215 			 new_state == IEEE80211_STA_NOTEXIST)
4216 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4217 	}
4218 
4219 	return ret;
4220 }
4221 
4222 int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4223 {
4224 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4225 
4226 	mvm->rts_threshold = value;
4227 
4228 	return 0;
4229 }
4230 
4231 void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4232 			   struct ieee80211_sta *sta, u32 changed)
4233 {
4234 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4235 
4236 	if (changed & (IEEE80211_RC_BW_CHANGED |
4237 		       IEEE80211_RC_SUPP_RATES_CHANGED |
4238 		       IEEE80211_RC_NSS_CHANGED))
4239 		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4240 
4241 	if (vif->type == NL80211_IFTYPE_STATION &&
4242 	    changed & IEEE80211_RC_NSS_CHANGED)
4243 		iwl_mvm_sf_update(mvm, vif, false);
4244 }
4245 
4246 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
4247 			       struct ieee80211_vif *vif,
4248 			       unsigned int link_id, u16 ac,
4249 			       const struct ieee80211_tx_queue_params *params)
4250 {
4251 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4252 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4253 
4254 	mvmvif->deflink.queue_params[ac] = *params;
4255 
4256 	/*
4257 	 * No need to update right away, we'll get BSS_CHANGED_QOS
4258 	 * The exception is P2P_DEVICE interface which needs immediate update.
4259 	 */
4260 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
4261 		guard(mvm)(mvm);
4262 		return iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4263 	}
4264 	return 0;
4265 }
4266 
4267 void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4268 				struct ieee80211_vif *vif,
4269 				struct ieee80211_prep_tx_info *info)
4270 {
4271 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4272 
4273 	guard(mvm)(mvm);
4274 	iwl_mvm_protect_assoc(mvm, vif, info->duration, info->link_id);
4275 }
4276 
4277 void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
4278 				 struct ieee80211_vif *vif,
4279 				 struct ieee80211_prep_tx_info *info)
4280 {
4281 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4282 
4283 	/* for successful cases (auth/assoc), don't cancel session protection */
4284 	if (info->success)
4285 		return;
4286 
4287 	guard(mvm)(mvm);
4288 	iwl_mvm_stop_session_protection(mvm, vif);
4289 }
4290 
4291 int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4292 				 struct ieee80211_vif *vif,
4293 				 struct cfg80211_sched_scan_request *req,
4294 				 struct ieee80211_scan_ies *ies)
4295 {
4296 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4297 
4298 	guard(mvm)(mvm);
4299 
4300 	if (!vif->cfg.idle)
4301 		return -EBUSY;
4302 
4303 	return iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4304 }
4305 
4306 int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4307 				struct ieee80211_vif *vif)
4308 {
4309 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4310 	int ret;
4311 
4312 	mutex_lock(&mvm->mutex);
4313 
4314 	/* Due to a race condition, it's possible that mac80211 asks
4315 	 * us to stop a sched_scan when it's already stopped.  This
4316 	 * can happen, for instance, if we stopped the scan ourselves,
4317 	 * called ieee80211_sched_scan_stopped() and the userspace called
4318 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4319 	 * could run.  To handle this, simply return if the scan is
4320 	 * not running.
4321 	*/
4322 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4323 		mutex_unlock(&mvm->mutex);
4324 		return 0;
4325 	}
4326 
4327 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4328 	mutex_unlock(&mvm->mutex);
4329 	iwl_mvm_wait_for_async_handlers(mvm);
4330 
4331 	return ret;
4332 }
4333 
4334 static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4335 				 enum set_key_cmd cmd,
4336 				 struct ieee80211_vif *vif,
4337 				 struct ieee80211_sta *sta,
4338 				 struct ieee80211_key_conf *key)
4339 {
4340 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4341 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4342 	struct iwl_mvm_sta *mvmsta = NULL;
4343 	struct iwl_mvm_key_pn *ptk_pn = NULL;
4344 	int keyidx = key->keyidx;
4345 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
4346 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4347 	int ret, i;
4348 	u8 key_offset;
4349 
4350 	if (sta)
4351 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4352 
4353 	switch (key->cipher) {
4354 	case WLAN_CIPHER_SUITE_TKIP:
4355 		if (!mvm->trans->trans_cfg->gen2) {
4356 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
4357 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4358 		} else if (vif->type == NL80211_IFTYPE_STATION) {
4359 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
4360 		} else {
4361 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
4362 			return -EOPNOTSUPP;
4363 		}
4364 		break;
4365 	case WLAN_CIPHER_SUITE_CCMP:
4366 	case WLAN_CIPHER_SUITE_GCMP:
4367 	case WLAN_CIPHER_SUITE_GCMP_256:
4368 		if (!iwl_mvm_has_new_tx_api(mvm))
4369 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4370 		break;
4371 	case WLAN_CIPHER_SUITE_AES_CMAC:
4372 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
4373 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4374 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4375 		break;
4376 	case WLAN_CIPHER_SUITE_WEP40:
4377 	case WLAN_CIPHER_SUITE_WEP104:
4378 		if (vif->type == NL80211_IFTYPE_STATION)
4379 			break;
4380 		if (iwl_mvm_has_new_tx_api(mvm))
4381 			return -EOPNOTSUPP;
4382 		/* support HW crypto on TX */
4383 		return 0;
4384 	default:
4385 		return -EOPNOTSUPP;
4386 	}
4387 
4388 	switch (cmd) {
4389 	case SET_KEY:
4390 		if (vif->type == NL80211_IFTYPE_STATION &&
4391 		    (keyidx == 6 || keyidx == 7))
4392 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4393 					   key);
4394 
4395 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4396 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4397 			/*
4398 			 * GTK on AP interface is a TX-only key, return 0;
4399 			 * on IBSS they're per-station and because we're lazy
4400 			 * we don't support them for RX, so do the same.
4401 			 * CMAC/GMAC in AP/IBSS modes must be done in software
4402 			 * on older NICs.
4403 			 *
4404 			 * Except, of course, beacon protection - it must be
4405 			 * offloaded since we just set a beacon template, and
4406 			 * then we must also offload the IGTK (not just BIGTK)
4407 			 * for firmware reasons.
4408 			 *
4409 			 * So just check for beacon protection - if we don't
4410 			 * have it we cannot get here with keyidx >= 6, and
4411 			 * if we do have it we need to send the key to FW in
4412 			 * all cases (CMAC/GMAC).
4413 			 */
4414 			if (!wiphy_ext_feature_isset(hw->wiphy,
4415 						     NL80211_EXT_FEATURE_BEACON_PROTECTION) &&
4416 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
4417 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4418 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
4419 				ret = -EOPNOTSUPP;
4420 				break;
4421 			}
4422 
4423 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
4424 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
4425 			    !iwl_mvm_has_new_tx_api(mvm)) {
4426 				key->hw_key_idx = STA_KEY_IDX_INVALID;
4427 				ret = 0;
4428 				break;
4429 			}
4430 
4431 			if (!mvmvif->ap_ibss_active) {
4432 				for (i = 0;
4433 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4434 				     i++) {
4435 					if (!mvmvif->ap_early_keys[i]) {
4436 						mvmvif->ap_early_keys[i] = key;
4437 						break;
4438 					}
4439 				}
4440 
4441 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4442 					ret = -ENOSPC;
4443 				else
4444 					ret = 0;
4445 
4446 				break;
4447 			}
4448 		}
4449 
4450 		/* During FW restart, in order to restore the state as it was,
4451 		 * don't try to reprogram keys we previously failed for.
4452 		 */
4453 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4454 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4455 			IWL_DEBUG_MAC80211(mvm,
4456 					   "skip invalid idx key programming during restart\n");
4457 			ret = 0;
4458 			break;
4459 		}
4460 
4461 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4462 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4463 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4464 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4465 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4466 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4467 			struct ieee80211_key_seq seq;
4468 			int tid, q;
4469 
4470 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4471 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4472 						     mvm->trans->num_rx_queues),
4473 					 GFP_KERNEL);
4474 			if (!ptk_pn) {
4475 				ret = -ENOMEM;
4476 				break;
4477 			}
4478 
4479 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4480 				ieee80211_get_key_rx_seq(key, tid, &seq);
4481 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
4482 					memcpy(ptk_pn->q[q].pn[tid],
4483 					       seq.ccmp.pn,
4484 					       IEEE80211_CCMP_PN_LEN);
4485 			}
4486 
4487 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4488 		}
4489 
4490 		/* in HW restart reuse the index, otherwise request a new one */
4491 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4492 			key_offset = key->hw_key_idx;
4493 		else
4494 			key_offset = STA_KEY_IDX_INVALID;
4495 
4496 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4497 			mvmsta->pairwise_cipher = key->cipher;
4498 
4499 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4500 				   sta ? sta->addr : NULL, key->keyidx);
4501 
4502 		if (sec_key_ver)
4503 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
4504 		else
4505 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
4506 
4507 		if (ret) {
4508 			IWL_WARN(mvm, "set key failed\n");
4509 			key->hw_key_idx = STA_KEY_IDX_INVALID;
4510 			if (ptk_pn) {
4511 				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4512 				kfree(ptk_pn);
4513 			}
4514 			/*
4515 			 * can't add key for RX, but we don't need it
4516 			 * in the device for TX so still return 0,
4517 			 * unless we have new TX API where we cannot
4518 			 * put key material into the TX_CMD
4519 			 */
4520 			if (iwl_mvm_has_new_tx_api(mvm))
4521 				ret = -EOPNOTSUPP;
4522 			else
4523 				ret = 0;
4524 		}
4525 
4526 		break;
4527 	case DISABLE_KEY:
4528 		if (vif->type == NL80211_IFTYPE_STATION &&
4529 		    (keyidx == 6 || keyidx == 7))
4530 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4531 					 NULL);
4532 
4533 		ret = -ENOENT;
4534 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4535 			if (mvmvif->ap_early_keys[i] == key) {
4536 				mvmvif->ap_early_keys[i] = NULL;
4537 				ret = 0;
4538 			}
4539 		}
4540 
4541 		/* found in pending list - don't do anything else */
4542 		if (ret == 0)
4543 			break;
4544 
4545 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4546 			ret = 0;
4547 			break;
4548 		}
4549 
4550 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4551 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4552 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4553 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4554 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4555 			ptk_pn = rcu_dereference_protected(
4556 						mvmsta->ptk_pn[keyidx],
4557 						lockdep_is_held(&mvm->mutex));
4558 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4559 			if (ptk_pn)
4560 				kfree_rcu(ptk_pn, rcu_head);
4561 		}
4562 
4563 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
4564 		if (sec_key_ver)
4565 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
4566 		else
4567 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4568 		break;
4569 	default:
4570 		ret = -EINVAL;
4571 	}
4572 
4573 	return ret;
4574 }
4575 
4576 int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4577 			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4578 			struct ieee80211_key_conf *key)
4579 {
4580 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4581 
4582 	guard(mvm)(mvm);
4583 	return __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4584 }
4585 
4586 void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4587 				 struct ieee80211_vif *vif,
4588 				 struct ieee80211_key_conf *keyconf,
4589 				 struct ieee80211_sta *sta,
4590 				 u32 iv32, u16 *phase1key)
4591 {
4592 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4593 
4594 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4595 		return;
4596 
4597 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4598 }
4599 
4600 
4601 static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4602 			       struct iwl_rx_packet *pkt, void *data)
4603 {
4604 	struct iwl_mvm *mvm =
4605 		container_of(notif_wait, struct iwl_mvm, notif_wait);
4606 	struct iwl_hs20_roc_res *resp;
4607 	int resp_len = iwl_rx_packet_payload_len(pkt);
4608 	struct iwl_mvm_time_event_data *te_data = data;
4609 
4610 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4611 		return true;
4612 
4613 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4614 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4615 		return true;
4616 	}
4617 
4618 	resp = (void *)pkt->data;
4619 
4620 	IWL_DEBUG_TE(mvm,
4621 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4622 		     resp->status, resp->event_unique_id);
4623 
4624 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4625 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4626 		     te_data->uid);
4627 
4628 	spin_lock_bh(&mvm->time_event_lock);
4629 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4630 	spin_unlock_bh(&mvm->time_event_lock);
4631 
4632 	return true;
4633 }
4634 
4635 static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4636 				    struct ieee80211_channel *channel,
4637 				    struct ieee80211_vif *vif,
4638 				    int duration)
4639 {
4640 	int res;
4641 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4642 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4643 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4644 	struct iwl_notification_wait wait_time_event;
4645 	u32 req_dur, delay;
4646 	struct iwl_hs20_roc_req aux_roc_req = {
4647 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4648 		.id_and_color =
4649 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4650 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4651 	};
4652 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
4653 		&aux_roc_req.channel_info);
4654 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
4655 
4656 	/* Set the channel info data */
4657 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
4658 			      iwl_mvm_phy_band_from_nl80211(channel->band),
4659 			      IWL_PHY_CHANNEL_MODE20,
4660 			      0);
4661 
4662 	/* Set the time and duration */
4663 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4664 
4665 	iwl_mvm_roc_duration_and_delay(vif, duration, &req_dur, &delay);
4666 	tail->duration = cpu_to_le32(req_dur);
4667 	tail->apply_time_max_delay = cpu_to_le32(delay);
4668 
4669 	IWL_DEBUG_TE(mvm,
4670 		     "ROC: Requesting to remain on channel %u for %ums\n",
4671 		     channel->hw_value, req_dur);
4672 	IWL_DEBUG_TE(mvm,
4673 		     "\t(requested = %ums, max_delay = %ums)\n",
4674 		     duration, delay);
4675 
4676 	/* Set the node address */
4677 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4678 
4679 	lockdep_assert_held(&mvm->mutex);
4680 
4681 	spin_lock_bh(&mvm->time_event_lock);
4682 
4683 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4684 		spin_unlock_bh(&mvm->time_event_lock);
4685 		return -EIO;
4686 	}
4687 
4688 	te_data->vif = vif;
4689 	te_data->duration = duration;
4690 	te_data->id = HOT_SPOT_CMD;
4691 
4692 	spin_unlock_bh(&mvm->time_event_lock);
4693 
4694 	/*
4695 	 * Use a notification wait, which really just processes the
4696 	 * command response and doesn't wait for anything, in order
4697 	 * to be able to process the response and get the UID inside
4698 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4699 	 * stores the buffer and then wakes up this thread, by which
4700 	 * time another notification (that the time event started)
4701 	 * might already be processed unsuccessfully.
4702 	 */
4703 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4704 				   time_event_response,
4705 				   ARRAY_SIZE(time_event_response),
4706 				   iwl_mvm_rx_aux_roc, te_data);
4707 
4708 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4709 				   &aux_roc_req);
4710 
4711 	if (res) {
4712 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4713 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4714 		goto out_clear_te;
4715 	}
4716 
4717 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4718 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4719 	/* should never fail */
4720 	WARN_ON_ONCE(res);
4721 
4722 	if (res) {
4723  out_clear_te:
4724 		spin_lock_bh(&mvm->time_event_lock);
4725 		iwl_mvm_te_clear_data(mvm, te_data);
4726 		spin_unlock_bh(&mvm->time_event_lock);
4727 	}
4728 
4729 	return res;
4730 }
4731 
4732 static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4733 {
4734 	int ret = 0;
4735 
4736 	lockdep_assert_held(&mvm->mutex);
4737 
4738 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4739 			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4740 		IWL_ERR(mvm, "hotspot not supported\n");
4741 		return -EINVAL;
4742 	}
4743 
4744 	if (iwl_mvm_has_new_station_api(mvm->fw)) {
4745 		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4746 		WARN(ret, "Failed to allocate aux station");
4747 	}
4748 
4749 	return ret;
4750 }
4751 
4752 static int iwl_mvm_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4753 {
4754 	int ret;
4755 
4756 	lockdep_assert_held(&mvm->mutex);
4757 
4758 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4759 	if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4760 		return ret;
4761 
4762 	/* The station and queue allocation must be done only after the binding
4763 	 * is done, as otherwise the FW might incorrectly configure its state.
4764 	 */
4765 	return iwl_mvm_add_p2p_bcast_sta(mvm, vif);
4766 }
4767 
4768 static int iwl_mvm_roc(struct ieee80211_hw *hw,
4769 		       struct ieee80211_vif *vif,
4770 		       struct ieee80211_channel *channel,
4771 		       int duration,
4772 		       enum ieee80211_roc_type type)
4773 {
4774 	static const struct iwl_mvm_roc_ops ops = {
4775 		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
4776 		.link = iwl_mvm_roc_link,
4777 	};
4778 
4779 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4780 }
4781 
4782 static int iwl_mvm_roc_station(struct iwl_mvm *mvm,
4783 			       struct ieee80211_channel *channel,
4784 			       struct ieee80211_vif *vif,
4785 			       int duration)
4786 {
4787 	int ret;
4788 	u32 cmd_id = WIDE_ID(MAC_CONF_GROUP, ROC_CMD);
4789 	u8 fw_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
4790 					  IWL_FW_CMD_VER_UNKNOWN);
4791 
4792 	if (fw_ver == IWL_FW_CMD_VER_UNKNOWN) {
4793 		ret = iwl_mvm_send_aux_roc_cmd(mvm, channel, vif, duration);
4794 	} else if (fw_ver >= 3) {
4795 		ret = iwl_mvm_roc_add_cmd(mvm, channel, vif, duration,
4796 					  ROC_ACTIVITY_HOTSPOT);
4797 	} else {
4798 		ret = -EOPNOTSUPP;
4799 		IWL_ERR(mvm, "ROC command version %d mismatch!\n", fw_ver);
4800 	}
4801 
4802 	return ret;
4803 }
4804 
4805 static int iwl_mvm_roc_p2p(struct iwl_mvm *mvm,
4806 			   struct ieee80211_channel *channel,
4807 			   struct ieee80211_vif *vif,
4808 			   int duration,
4809 			   enum ieee80211_roc_type type)
4810 {
4811 	enum iwl_roc_activity activity;
4812 	int ret;
4813 
4814 	lockdep_assert_held(&mvm->mutex);
4815 
4816 	switch (type) {
4817 	case IEEE80211_ROC_TYPE_NORMAL:
4818 		activity = ROC_ACTIVITY_P2P_DISC;
4819 		break;
4820 	case IEEE80211_ROC_TYPE_MGMT_TX:
4821 		activity = ROC_ACTIVITY_P2P_NEG;
4822 		break;
4823 	default:
4824 		WARN_ONCE(1, "Got an invalid P2P ROC type\n");
4825 		return -EINVAL;
4826 	}
4827 
4828 	ret = iwl_mvm_mld_add_aux_sta(mvm,
4829 				      iwl_mvm_get_lmac_id(mvm, channel->band));
4830 	if (ret)
4831 		return ret;
4832 
4833 	return iwl_mvm_roc_add_cmd(mvm, channel, vif, duration, activity);
4834 }
4835 
4836 static int iwl_mvm_p2p_find_phy_ctxt(struct iwl_mvm *mvm,
4837 				     struct ieee80211_vif *vif,
4838 				     struct ieee80211_channel *channel)
4839 {
4840 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4841 	struct cfg80211_chan_def chandef;
4842 	int i;
4843 
4844 	lockdep_assert_held(&mvm->mutex);
4845 
4846 	if (mvmvif->deflink.phy_ctxt &&
4847 	    channel == mvmvif->deflink.phy_ctxt->channel)
4848 		return 0;
4849 
4850 	/* Try using a PHY context that is already in use */
4851 	for (i = 0; i < NUM_PHY_CTX; i++) {
4852 		struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[i];
4853 
4854 		if (!phy_ctxt->ref || mvmvif->deflink.phy_ctxt == phy_ctxt)
4855 			continue;
4856 
4857 		if (channel == phy_ctxt->channel) {
4858 			if (mvmvif->deflink.phy_ctxt)
4859 				iwl_mvm_phy_ctxt_unref(mvm,
4860 						       mvmvif->deflink.phy_ctxt);
4861 
4862 			mvmvif->deflink.phy_ctxt = phy_ctxt;
4863 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4864 			return 0;
4865 		}
4866 	}
4867 
4868 	/* We already have a phy_ctxt, but it's not on the right channel */
4869 	if (mvmvif->deflink.phy_ctxt)
4870 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
4871 
4872 	mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4873 	if (!mvmvif->deflink.phy_ctxt)
4874 		return -ENOSPC;
4875 
4876 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4877 
4878 	return iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt,
4879 				    &chandef, NULL, 1, 1);
4880 }
4881 
4882 /* Execute the common part for MLD and non-MLD modes */
4883 int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4884 		       struct ieee80211_channel *channel, int duration,
4885 		       enum ieee80211_roc_type type,
4886 		       const struct iwl_mvm_roc_ops *ops)
4887 {
4888 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4889 	struct ieee80211_vif *bss_vif = iwl_mvm_get_bss_vif(mvm);
4890 	u32 lmac_id;
4891 	int ret;
4892 
4893 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4894 			   duration, type);
4895 
4896 	/*
4897 	 * Flush the done work, just in case it's still pending, so that
4898 	 * the work it does can complete and we can accept new frames.
4899 	 */
4900 	flush_work(&mvm->roc_done_wk);
4901 
4902 	if (!IS_ERR_OR_NULL(bss_vif)) {
4903 		ret = iwl_mvm_block_esr_sync(mvm, bss_vif,
4904 					     IWL_MVM_ESR_BLOCKED_ROC);
4905 		if (ret)
4906 			return ret;
4907 	}
4908 
4909 	guard(mvm)(mvm);
4910 
4911 	switch (vif->type) {
4912 	case NL80211_IFTYPE_STATION:
4913 		lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
4914 
4915 		/* Use aux roc framework (HS20) */
4916 		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4917 		if (!ret)
4918 			ret = iwl_mvm_roc_station(mvm, channel, vif, duration);
4919 		return ret;
4920 	case NL80211_IFTYPE_P2P_DEVICE:
4921 		/* handle below */
4922 		break;
4923 	default:
4924 		IWL_ERR(mvm, "ROC: Invalid vif type=%u\n", vif->type);
4925 		return -EINVAL;
4926 	}
4927 
4928 	if (iwl_mvm_has_p2p_over_aux(mvm)) {
4929 		ret = iwl_mvm_roc_p2p(mvm, channel, vif, duration, type);
4930 		return ret;
4931 	}
4932 
4933 	ret = iwl_mvm_p2p_find_phy_ctxt(mvm, vif, channel);
4934 	if (ret)
4935 		return ret;
4936 
4937 	ret = ops->link(mvm, vif);
4938 	if (ret)
4939 		return ret;
4940 
4941 	return iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4942 }
4943 
4944 int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
4945 		       struct ieee80211_vif *vif)
4946 {
4947 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4948 
4949 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4950 
4951 	iwl_mvm_stop_roc(mvm, vif);
4952 
4953 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4954 	return 0;
4955 }
4956 
4957 struct iwl_mvm_ftm_responder_iter_data {
4958 	bool responder;
4959 	struct ieee80211_chanctx_conf *ctx;
4960 };
4961 
4962 static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4963 					       struct ieee80211_vif *vif)
4964 {
4965 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4966 
4967 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4968 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4969 		data->responder = true;
4970 }
4971 
4972 bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4973 				      struct ieee80211_chanctx_conf *ctx)
4974 {
4975 	struct iwl_mvm_ftm_responder_iter_data data = {
4976 		.responder = false,
4977 		.ctx = ctx,
4978 	};
4979 
4980 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4981 					IEEE80211_IFACE_ITER_NORMAL,
4982 					iwl_mvm_ftm_responder_chanctx_iter,
4983 					&data);
4984 	return data.responder;
4985 }
4986 
4987 static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4988 				 struct ieee80211_chanctx_conf *ctx)
4989 {
4990 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4991 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4992 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
4993 	int ret;
4994 
4995 	lockdep_assert_held(&mvm->mutex);
4996 
4997 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4998 
4999 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
5000 	if (!phy_ctxt) {
5001 		ret = -ENOSPC;
5002 		goto out;
5003 	}
5004 
5005 	ret = iwl_mvm_phy_ctxt_add(mvm, phy_ctxt, def, &ctx->ap,
5006 				   ctx->rx_chains_static,
5007 				   ctx->rx_chains_dynamic);
5008 	if (ret) {
5009 		IWL_ERR(mvm, "Failed to add PHY context\n");
5010 		goto out;
5011 	}
5012 
5013 	*phy_ctxt_id = phy_ctxt->id;
5014 out:
5015 	return ret;
5016 }
5017 
5018 int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
5019 			struct ieee80211_chanctx_conf *ctx)
5020 {
5021 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5022 
5023 	guard(mvm)(mvm);
5024 	return __iwl_mvm_add_chanctx(mvm, ctx);
5025 }
5026 
5027 static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
5028 				     struct ieee80211_chanctx_conf *ctx)
5029 {
5030 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5031 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5032 
5033 	lockdep_assert_held(&mvm->mutex);
5034 
5035 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
5036 }
5037 
5038 void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
5039 			    struct ieee80211_chanctx_conf *ctx)
5040 {
5041 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5042 
5043 	guard(mvm)(mvm);
5044 	__iwl_mvm_remove_chanctx(mvm, ctx);
5045 }
5046 
5047 void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
5048 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
5049 {
5050 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5051 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5052 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5053 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
5054 
5055 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
5056 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
5057 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
5058 				   IEEE80211_CHANCTX_CHANGE_RADAR |
5059 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
5060 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
5061 		      phy_ctxt->ref, changed))
5062 		return;
5063 
5064 	guard(mvm)(mvm);
5065 
5066 	/* we are only changing the min_width, may be a noop */
5067 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
5068 		if (phy_ctxt->width == def->width)
5069 			return;
5070 
5071 		/* we are just toggling between 20_NOHT and 20 */
5072 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
5073 		    def->width <= NL80211_CHAN_WIDTH_20)
5074 			return;
5075 	}
5076 
5077 	iwl_mvm_bt_coex_vif_change(mvm);
5078 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def, &ctx->ap,
5079 				 ctx->rx_chains_static,
5080 				 ctx->rx_chains_dynamic);
5081 }
5082 
5083 /*
5084  * This function executes the common part for MLD and non-MLD modes.
5085  *
5086  * Returns true if we're done assigning the chanctx
5087  * (either on failure or success)
5088  */
5089 static bool
5090 __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
5091 				    struct ieee80211_vif *vif,
5092 				    struct ieee80211_chanctx_conf *ctx,
5093 				    bool switching_chanctx, int *ret)
5094 {
5095 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5096 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5097 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5098 
5099 	lockdep_assert_held(&mvm->mutex);
5100 
5101 	mvmvif->deflink.phy_ctxt = phy_ctxt;
5102 
5103 	switch (vif->type) {
5104 	case NL80211_IFTYPE_AP:
5105 		/* only needed if we're switching chanctx (i.e. during CSA) */
5106 		if (switching_chanctx) {
5107 			mvmvif->ap_ibss_active = true;
5108 			break;
5109 		}
5110 		fallthrough;
5111 	case NL80211_IFTYPE_ADHOC:
5112 		/*
5113 		 * The AP binding flow is handled as part of the start_ap flow
5114 		 * (in bss_info_changed), similarly for IBSS.
5115 		 */
5116 		*ret = 0;
5117 		return true;
5118 	case NL80211_IFTYPE_STATION:
5119 		break;
5120 	case NL80211_IFTYPE_MONITOR:
5121 		/* always disable PS when a monitor interface is active */
5122 		mvmvif->ps_disabled = true;
5123 		break;
5124 	default:
5125 		*ret = -EINVAL;
5126 		return true;
5127 	}
5128 	return false;
5129 }
5130 
5131 static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
5132 					struct ieee80211_vif *vif,
5133 					struct ieee80211_bss_conf *link_conf,
5134 					struct ieee80211_chanctx_conf *ctx,
5135 					bool switching_chanctx)
5136 {
5137 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5138 	int ret;
5139 
5140 	if (WARN_ON(!link_conf))
5141 		return -EINVAL;
5142 
5143 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
5144 						switching_chanctx, &ret))
5145 		goto out;
5146 
5147 	ret = iwl_mvm_binding_add_vif(mvm, vif);
5148 	if (ret)
5149 		goto out;
5150 
5151 	/*
5152 	 * Power state must be updated before quotas,
5153 	 * otherwise fw will complain.
5154 	 */
5155 	iwl_mvm_power_update_mac(mvm);
5156 
5157 	/* Setting the quota at this stage is only required for monitor
5158 	 * interfaces. For the other types, the bss_info changed flow
5159 	 * will handle quota settings.
5160 	 */
5161 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5162 		mvmvif->monitor_active = true;
5163 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
5164 		if (ret)
5165 			goto out_remove_binding;
5166 
5167 		ret = iwl_mvm_add_snif_sta(mvm, vif);
5168 		if (ret)
5169 			goto out_remove_binding;
5170 
5171 	}
5172 
5173 	/* Handle binding during CSA */
5174 	if (vif->type == NL80211_IFTYPE_AP) {
5175 		iwl_mvm_update_quotas(mvm, false, NULL);
5176 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
5177 	}
5178 
5179 	if (vif->type == NL80211_IFTYPE_STATION) {
5180 		if (!switching_chanctx) {
5181 			mvmvif->csa_bcn_pending = false;
5182 			goto out;
5183 		}
5184 
5185 		mvmvif->csa_bcn_pending = true;
5186 
5187 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5188 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5189 			u32 duration = 5 * vif->bss_conf.beacon_int;
5190 
5191 			/* Protect the session to make sure we hear the first
5192 			 * beacon on the new channel.
5193 			 */
5194 			iwl_mvm_protect_session(mvm, vif, duration, duration,
5195 						vif->bss_conf.beacon_int / 2,
5196 						true);
5197 		}
5198 
5199 		iwl_mvm_update_quotas(mvm, false, NULL);
5200 
5201 		iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif,
5202 							link_conf,
5203 							false);
5204 	}
5205 
5206 	goto out;
5207 
5208 out_remove_binding:
5209 	iwl_mvm_binding_remove_vif(mvm, vif);
5210 	iwl_mvm_power_update_mac(mvm);
5211 out:
5212 	if (ret)
5213 		mvmvif->deflink.phy_ctxt = NULL;
5214 	return ret;
5215 }
5216 
5217 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
5218 				      struct ieee80211_vif *vif,
5219 				      struct ieee80211_bss_conf *link_conf,
5220 				      struct ieee80211_chanctx_conf *ctx)
5221 {
5222 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5223 
5224 	guard(mvm)(mvm);
5225 	return __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5226 }
5227 
5228 /*
5229  * This function executes the common part for MLD and non-MLD modes.
5230  *
5231  * Returns if chanctx unassign chanctx is done
5232  * (either on failure or success)
5233  */
5234 static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
5235 						  struct ieee80211_vif *vif,
5236 						  bool switching_chanctx)
5237 {
5238 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5239 
5240 	lockdep_assert_held(&mvm->mutex);
5241 	iwl_mvm_remove_time_event(mvm, mvmvif,
5242 				  &mvmvif->time_event_data);
5243 
5244 	switch (vif->type) {
5245 	case NL80211_IFTYPE_ADHOC:
5246 		return true;
5247 	case NL80211_IFTYPE_MONITOR:
5248 		mvmvif->monitor_active = false;
5249 		mvmvif->ps_disabled = false;
5250 		break;
5251 	case NL80211_IFTYPE_AP:
5252 		/* This part is triggered only during CSA */
5253 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
5254 			return true;
5255 
5256 		mvmvif->csa_countdown = false;
5257 
5258 		/* Set CS bit on all the stations */
5259 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5260 
5261 		/* Save blocked iface, the timeout is set on the next beacon */
5262 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5263 
5264 		mvmvif->ap_ibss_active = false;
5265 		break;
5266 	default:
5267 		break;
5268 	}
5269 	return false;
5270 }
5271 
5272 static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5273 					   struct ieee80211_vif *vif,
5274 					   struct ieee80211_bss_conf *link_conf,
5275 					   struct ieee80211_chanctx_conf *ctx,
5276 					   bool switching_chanctx)
5277 {
5278 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5279 	struct ieee80211_vif *disabled_vif = NULL;
5280 
5281 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5282 		goto out;
5283 
5284 	if (vif->type == NL80211_IFTYPE_MONITOR)
5285 		iwl_mvm_rm_snif_sta(mvm, vif);
5286 
5287 
5288 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5289 		disabled_vif = vif;
5290 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5291 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5292 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5293 	}
5294 
5295 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5296 	iwl_mvm_binding_remove_vif(mvm, vif);
5297 
5298 out:
5299 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5300 	    switching_chanctx)
5301 		return;
5302 	mvmvif->deflink.phy_ctxt = NULL;
5303 	iwl_mvm_power_update_mac(mvm);
5304 }
5305 
5306 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5307 					 struct ieee80211_vif *vif,
5308 					 struct ieee80211_bss_conf *link_conf,
5309 					 struct ieee80211_chanctx_conf *ctx)
5310 {
5311 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5312 
5313 	guard(mvm)(mvm);
5314 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5315 }
5316 
5317 static int
5318 iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5319 				struct ieee80211_vif_chanctx_switch *vifs,
5320 				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5321 {
5322 	int ret;
5323 
5324 	guard(mvm)(mvm);
5325 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5326 				    vifs[0].old_ctx, true);
5327 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5328 
5329 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5330 	if (ret) {
5331 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5332 		goto out_reassign;
5333 	}
5334 
5335 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5336 					vifs[0].new_ctx, true);
5337 	if (ret) {
5338 		IWL_ERR(mvm,
5339 			"failed to assign new_ctx during channel switch\n");
5340 		goto out_remove;
5341 	}
5342 
5343 	/* we don't support TDLS during DCM - can be caused by channel switch */
5344 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5345 		iwl_mvm_teardown_tdls_peers(mvm);
5346 
5347 	return 0;
5348 
5349 out_remove:
5350 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5351 
5352 out_reassign:
5353 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5354 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5355 		goto out_restart;
5356 	}
5357 
5358 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5359 				      vifs[0].old_ctx, true)) {
5360 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5361 		goto out_restart;
5362 	}
5363 
5364 	return ret;
5365 
5366 out_restart:
5367 	/* things keep failing, better restart the hw */
5368 	iwl_mvm_nic_restart(mvm, false);
5369 	return ret;
5370 }
5371 
5372 static int
5373 iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5374 				    struct ieee80211_vif_chanctx_switch *vifs,
5375 				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5376 {
5377 	int ret;
5378 
5379 	guard(mvm)(mvm);
5380 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5381 				    vifs[0].old_ctx, true);
5382 
5383 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5384 					vifs[0].new_ctx, true);
5385 	if (ret) {
5386 		IWL_ERR(mvm,
5387 			"failed to assign new_ctx during channel switch\n");
5388 		goto out_reassign;
5389 	}
5390 
5391 	return 0;
5392 
5393 out_reassign:
5394 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5395 				      vifs[0].old_ctx, true)) {
5396 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5397 		goto out_restart;
5398 	}
5399 
5400 	return ret;
5401 
5402 out_restart:
5403 	/* things keep failing, better restart the hw */
5404 	iwl_mvm_nic_restart(mvm, false);
5405 	return ret;
5406 }
5407 
5408 /* Execute the common part for both MLD and non-MLD modes */
5409 int
5410 iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5411 				  struct ieee80211_vif_chanctx_switch *vifs,
5412 				  int n_vifs,
5413 				  enum ieee80211_chanctx_switch_mode mode,
5414 				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5415 {
5416 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5417 	int ret;
5418 
5419 	/* we only support a single-vif right now */
5420 	if (n_vifs > 1)
5421 		return -EOPNOTSUPP;
5422 
5423 	switch (mode) {
5424 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5425 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5426 		break;
5427 	case CHANCTX_SWMODE_REASSIGN_VIF:
5428 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5429 		break;
5430 	default:
5431 		ret = -EOPNOTSUPP;
5432 		break;
5433 	}
5434 
5435 	return ret;
5436 }
5437 
5438 static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5439 				      struct ieee80211_vif_chanctx_switch *vifs,
5440 				      int n_vifs,
5441 				      enum ieee80211_chanctx_switch_mode mode)
5442 {
5443 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5444 		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5445 		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5446 	};
5447 
5448 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5449 }
5450 
5451 int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
5452 {
5453 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5454 
5455 	return mvm->ibss_manager;
5456 }
5457 
5458 static int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5459 			   bool set)
5460 {
5461 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5462 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5463 
5464 	if (!mvm_sta || !mvm_sta->vif) {
5465 		IWL_ERR(mvm, "Station is not associated to a vif\n");
5466 		return -EINVAL;
5467 	}
5468 
5469 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
5470 					       &mvm_sta->vif->bss_conf);
5471 }
5472 
5473 #ifdef CONFIG_NL80211_TESTMODE
5474 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5475 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5476 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5477 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5478 };
5479 
5480 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5481 				      struct ieee80211_vif *vif,
5482 				      void *data, int len)
5483 {
5484 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5485 	int err;
5486 	u32 noa_duration;
5487 
5488 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
5489 				   iwl_mvm_tm_policy, NULL);
5490 	if (err)
5491 		return err;
5492 
5493 	if (!tb[IWL_MVM_TM_ATTR_CMD])
5494 		return -EINVAL;
5495 
5496 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5497 	case IWL_MVM_TM_CMD_SET_NOA:
5498 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5499 		    !vif->bss_conf.enable_beacon ||
5500 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5501 			return -EINVAL;
5502 
5503 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5504 		if (noa_duration >= vif->bss_conf.beacon_int)
5505 			return -EINVAL;
5506 
5507 		mvm->noa_duration = noa_duration;
5508 		mvm->noa_vif = vif;
5509 
5510 		return iwl_mvm_update_quotas(mvm, true, NULL);
5511 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5512 		/* must be associated client vif - ignore authorized */
5513 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5514 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5515 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5516 			return -EINVAL;
5517 
5518 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
5519 			return iwl_mvm_enable_beacon_filter(mvm, vif);
5520 		return iwl_mvm_disable_beacon_filter(mvm, vif);
5521 	}
5522 
5523 	return -EOPNOTSUPP;
5524 }
5525 
5526 int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5527 			     struct ieee80211_vif *vif,
5528 			     void *data, int len)
5529 {
5530 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5531 
5532 	guard(mvm)(mvm);
5533 	return __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5534 }
5535 #endif
5536 
5537 void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5538 			    struct ieee80211_channel_switch *chsw)
5539 {
5540 	/* By implementing this operation, we prevent mac80211 from
5541 	 * starting its own channel switch timer, so that we can call
5542 	 * ieee80211_chswitch_done() ourselves at the right time
5543 	 * (which is when the absence time event starts).
5544 	 */
5545 
5546 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5547 			   "dummy channel switch op\n");
5548 }
5549 
5550 static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
5551 				       struct ieee80211_vif *vif,
5552 				       struct ieee80211_channel_switch *chsw)
5553 {
5554 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5555 	struct iwl_chan_switch_te_cmd cmd = {
5556 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5557 							  mvmvif->color)),
5558 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
5559 		.tsf = cpu_to_le32(chsw->timestamp),
5560 		.cs_count = chsw->count,
5561 		.cs_mode = chsw->block_tx,
5562 	};
5563 
5564 	lockdep_assert_held(&mvm->mutex);
5565 
5566 	if (chsw->delay)
5567 		cmd.cs_delayed_bcn_count =
5568 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
5569 
5570 	return iwl_mvm_send_cmd_pdu(mvm,
5571 				    WIDE_ID(MAC_CONF_GROUP,
5572 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
5573 				    0, sizeof(cmd), &cmd);
5574 }
5575 
5576 static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
5577 				       struct ieee80211_vif *vif,
5578 				       struct ieee80211_channel_switch *chsw)
5579 {
5580 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5581 	u32 apply_time;
5582 
5583 	/* Schedule the time event to a bit before beacon 1,
5584 	 * to make sure we're in the new channel when the
5585 	 * GO/AP arrives. In case count <= 1 immediately schedule the
5586 	 * TE (this might result with some packet loss or connection
5587 	 * loss).
5588 	 */
5589 	if (chsw->count <= 1)
5590 		apply_time = 0;
5591 	else
5592 		apply_time = chsw->device_timestamp +
5593 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
5594 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
5595 
5596 	if (chsw->block_tx)
5597 		iwl_mvm_csa_client_absent(mvm, vif);
5598 
5599 	if (mvmvif->bf_enabled) {
5600 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif);
5601 
5602 		if (ret)
5603 			return ret;
5604 	}
5605 
5606 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
5607 				    apply_time);
5608 
5609 	return 0;
5610 }
5611 
5612 static void iwl_mvm_csa_block_txqs(void *data, struct ieee80211_sta *sta)
5613 {
5614 	int i;
5615 
5616 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
5617 		struct iwl_mvm_txq *mvmtxq =
5618 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
5619 
5620 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5621 	}
5622 }
5623 
5624 #define IWL_MAX_CSA_BLOCK_TX 1500
5625 int iwl_mvm_pre_channel_switch(struct iwl_mvm *mvm,
5626 			       struct ieee80211_vif *vif,
5627 			       struct ieee80211_channel_switch *chsw)
5628 {
5629 	struct ieee80211_vif *csa_vif;
5630 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5631 	struct iwl_mvm_txq *mvmtxq;
5632 	int ret;
5633 
5634 	lockdep_assert_held(&mvm->mutex);
5635 
5636 	mvmvif->csa_failed = false;
5637 	mvmvif->csa_blocks_tx = false;
5638 
5639 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5640 			   chsw->chandef.center_freq1);
5641 
5642 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
5643 				       ieee80211_vif_to_wdev(vif),
5644 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5645 
5646 	switch (vif->type) {
5647 	case NL80211_IFTYPE_AP:
5648 		csa_vif =
5649 			rcu_dereference_protected(mvm->csa_vif,
5650 						  lockdep_is_held(&mvm->mutex));
5651 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5652 			      "Another CSA is already in progress"))
5653 			return -EBUSY;
5654 
5655 		/* we still didn't unblock tx. prevent new CS meanwhile */
5656 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5657 					      lockdep_is_held(&mvm->mutex)))
5658 			return -EBUSY;
5659 
5660 		rcu_assign_pointer(mvm->csa_vif, vif);
5661 
5662 		if (WARN_ONCE(mvmvif->csa_countdown,
5663 			      "Previous CSA countdown didn't complete"))
5664 			return -EBUSY;
5665 
5666 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5667 
5668 		if (!chsw->block_tx)
5669 			break;
5670 		/* don't need blocking in driver otherwise - mac80211 will do */
5671 		if (!ieee80211_hw_check(mvm->hw, HANDLES_QUIET_CSA))
5672 			break;
5673 
5674 		mvmvif->csa_blocks_tx = true;
5675 		mvmtxq = iwl_mvm_txq_from_mac80211(vif->txq);
5676 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5677 		ieee80211_iterate_stations_atomic(mvm->hw,
5678 						  iwl_mvm_csa_block_txqs,
5679 						  NULL);
5680 		break;
5681 	case NL80211_IFTYPE_STATION:
5682 		mvmvif->csa_blocks_tx = chsw->block_tx;
5683 
5684 		/*
5685 		 * In the new flow FW is in charge of timing the switch so there
5686 		 * is no need for all of this
5687 		 */
5688 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5689 					    CHANNEL_SWITCH_ERROR_NOTIF,
5690 					    0))
5691 			break;
5692 
5693 		/*
5694 		 * We haven't configured the firmware to be associated yet since
5695 		 * we don't know the dtim period. In this case, the firmware can't
5696 		 * track the beacons.
5697 		 */
5698 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period)
5699 			return -EBUSY;
5700 
5701 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX &&
5702 		    hweight16(vif->valid_links) <= 1)
5703 			schedule_delayed_work(&mvmvif->csa_work, 0);
5704 
5705 		if (chsw->block_tx) {
5706 			/*
5707 			 * In case of undetermined / long time with immediate
5708 			 * quiet monitor status to gracefully disconnect
5709 			 */
5710 			if (!chsw->count ||
5711 			    chsw->count * vif->bss_conf.beacon_int >
5712 			    IWL_MAX_CSA_BLOCK_TX)
5713 				schedule_delayed_work(&mvmvif->csa_work,
5714 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5715 		}
5716 
5717 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5718 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5719 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5720 			if (ret)
5721 				return ret;
5722 		} else {
5723 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
5724 		}
5725 
5726 		mvmvif->csa_count = chsw->count;
5727 		mvmvif->csa_misbehave = false;
5728 		break;
5729 	default:
5730 		break;
5731 	}
5732 
5733 	mvmvif->ps_disabled = true;
5734 
5735 	ret = iwl_mvm_power_update_ps(mvm);
5736 	if (ret)
5737 		return ret;
5738 
5739 	/* we won't be on this channel any longer */
5740 	iwl_mvm_teardown_tdls_peers(mvm);
5741 
5742 	return ret;
5743 }
5744 
5745 static int iwl_mvm_mac_pre_channel_switch(struct ieee80211_hw *hw,
5746 					  struct ieee80211_vif *vif,
5747 					  struct ieee80211_channel_switch *chsw)
5748 {
5749 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5750 
5751 	guard(mvm)(mvm);
5752 	return iwl_mvm_pre_channel_switch(mvm, vif, chsw);
5753 }
5754 
5755 void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5756 				      struct ieee80211_vif *vif,
5757 				      struct ieee80211_channel_switch *chsw)
5758 {
5759 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5760 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5761 	struct iwl_chan_switch_te_cmd cmd = {
5762 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5763 							  mvmvif->color)),
5764 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5765 		.tsf = cpu_to_le32(chsw->timestamp),
5766 		.cs_count = chsw->count,
5767 		.cs_mode = chsw->block_tx,
5768 	};
5769 
5770 	/*
5771 	 * In the new flow FW is in charge of timing the switch so there is no
5772 	 * need for all of this
5773 	 */
5774 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5775 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5776 		return;
5777 
5778 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5779 		return;
5780 
5781 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5782 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5783 
5784 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
5785 		if (mvmvif->csa_misbehave) {
5786 			struct ieee80211_bss_conf *link_conf;
5787 
5788 			/* Second time, give up on this AP*/
5789 
5790 			link_conf = wiphy_dereference(hw->wiphy,
5791 						      vif->link_conf[chsw->link_id]);
5792 			if (WARN_ON(!link_conf))
5793 				return;
5794 
5795 			iwl_mvm_abort_channel_switch(hw, vif, link_conf);
5796 			ieee80211_chswitch_done(vif, false, 0);
5797 			mvmvif->csa_misbehave = false;
5798 			return;
5799 		}
5800 		mvmvif->csa_misbehave = true;
5801 	}
5802 	mvmvif->csa_count = chsw->count;
5803 
5804 	guard(mvm)(mvm);
5805 	if (mvmvif->csa_failed)
5806 		return;
5807 
5808 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5809 				     WIDE_ID(MAC_CONF_GROUP,
5810 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5811 				     0, sizeof(cmd), &cmd));
5812 }
5813 
5814 static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
5815 {
5816 	int i;
5817 
5818 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5819 		if (drop) {
5820 			guard(mvm)(mvm);
5821 			iwl_mvm_flush_tx_path(mvm,
5822 				iwl_mvm_flushable_queues(mvm) & queues);
5823 		} else {
5824 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5825 		}
5826 		return;
5827 	}
5828 
5829 	guard(mvm)(mvm);
5830 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5831 		struct ieee80211_sta *sta;
5832 
5833 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5834 						lockdep_is_held(&mvm->mutex));
5835 		if (IS_ERR_OR_NULL(sta))
5836 			continue;
5837 
5838 		if (drop)
5839 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
5840 		else
5841 			iwl_mvm_wait_sta_queues_empty(mvm,
5842 					iwl_mvm_sta_from_mac80211(sta));
5843 	}
5844 }
5845 
5846 void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5847 		       u32 queues, bool drop)
5848 {
5849 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5850 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5851 	struct iwl_mvm_sta *mvmsta;
5852 	struct ieee80211_sta *sta;
5853 	bool ap_sta_done = false;
5854 	int i;
5855 	u32 msk = 0;
5856 
5857 	if (!vif) {
5858 		iwl_mvm_flush_no_vif(mvm, queues, drop);
5859 		return;
5860 	}
5861 
5862 	if (!drop && hweight16(vif->active_links) <= 1) {
5863 		int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
5864 		struct ieee80211_bss_conf *link_conf;
5865 
5866 		link_conf = wiphy_dereference(hw->wiphy,
5867 					      vif->link_conf[link_id]);
5868 		if (WARN_ON(!link_conf))
5869 			return;
5870 		if (link_conf->csa_active && mvmvif->csa_blocks_tx)
5871 			drop = true;
5872 	}
5873 
5874 	/* Make sure we're done with the deferred traffic before flushing */
5875 	flush_work(&mvm->add_stream_wk);
5876 
5877 	mutex_lock(&mvm->mutex);
5878 
5879 	/* flush the AP-station and all TDLS peers */
5880 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5881 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5882 						lockdep_is_held(&mvm->mutex));
5883 		if (IS_ERR_OR_NULL(sta))
5884 			continue;
5885 
5886 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5887 		if (mvmsta->vif != vif)
5888 			continue;
5889 
5890 		if (sta == mvmvif->ap_sta) {
5891 			if (ap_sta_done)
5892 				continue;
5893 			ap_sta_done = true;
5894 		}
5895 
5896 		if (drop) {
5897 			if (iwl_mvm_flush_sta(mvm, mvmsta->deflink.sta_id,
5898 					      mvmsta->tfd_queue_msk))
5899 				IWL_ERR(mvm, "flush request fail\n");
5900 		} else {
5901 			if (iwl_mvm_has_new_tx_api(mvm))
5902 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5903 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5904 				msk |= mvmsta->tfd_queue_msk;
5905 		}
5906 	}
5907 
5908 	mutex_unlock(&mvm->mutex);
5909 
5910 	/* this can take a while, and we may need/want other operations
5911 	 * to succeed while doing this, so do it without the mutex held
5912 	 */
5913 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5914 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5915 }
5916 
5917 void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5918 			   struct ieee80211_sta *sta)
5919 {
5920 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5921 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5922 	struct iwl_mvm_link_sta *mvm_link_sta;
5923 	struct ieee80211_link_sta *link_sta;
5924 	int link_id;
5925 
5926 	guard(mvm)(mvm);
5927 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
5928 		mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_id],
5929 							 lockdep_is_held(&mvm->mutex));
5930 		if (!mvm_link_sta)
5931 			continue;
5932 
5933 		if (iwl_mvm_flush_sta(mvm, mvm_link_sta->sta_id,
5934 				      mvmsta->tfd_queue_msk))
5935 			IWL_ERR(mvm, "flush request fail\n");
5936 	}
5937 }
5938 
5939 static int iwl_mvm_mac_get_acs_survey(struct iwl_mvm *mvm, int idx,
5940 				      struct survey_info *survey)
5941 {
5942 	int chan_idx;
5943 	enum nl80211_band band;
5944 	int ret;
5945 
5946 	mutex_lock(&mvm->mutex);
5947 
5948 	if (!mvm->acs_survey) {
5949 		ret = -ENOENT;
5950 		goto out;
5951 	}
5952 
5953 	/* Find and return the next entry that has a non-zero active time */
5954 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5955 		struct ieee80211_supported_band *sband =
5956 			mvm->hw->wiphy->bands[band];
5957 
5958 		if (!sband)
5959 			continue;
5960 
5961 		for (chan_idx = 0; chan_idx < sband->n_channels; chan_idx++) {
5962 			struct iwl_mvm_acs_survey_channel *info =
5963 				&mvm->acs_survey->bands[band][chan_idx];
5964 
5965 			if (!info->time)
5966 				continue;
5967 
5968 			/* Found (the next) channel to report */
5969 			survey->channel = &sband->channels[chan_idx];
5970 			survey->filled = SURVEY_INFO_TIME |
5971 					 SURVEY_INFO_TIME_BUSY |
5972 					 SURVEY_INFO_TIME_RX |
5973 					 SURVEY_INFO_TIME_TX;
5974 			survey->time = info->time;
5975 			survey->time_busy = info->time_busy;
5976 			survey->time_rx = info->time_rx;
5977 			survey->time_tx = info->time_tx;
5978 			survey->noise = info->noise;
5979 			if (survey->noise < 0)
5980 				survey->filled |= SURVEY_INFO_NOISE_DBM;
5981 
5982 			/* Clear time so that channel is only reported once */
5983 			info->time = 0;
5984 
5985 			ret = 0;
5986 			goto out;
5987 		}
5988 	}
5989 
5990 	ret = -ENOENT;
5991 
5992 out:
5993 	mutex_unlock(&mvm->mutex);
5994 
5995 	return ret;
5996 }
5997 
5998 int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5999 			   struct survey_info *survey)
6000 {
6001 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6002 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
6003 					   WIDE_ID(SYSTEM_GROUP,
6004 						   SYSTEM_STATISTICS_CMD),
6005 					   IWL_FW_CMD_VER_UNKNOWN);
6006 
6007 	memset(survey, 0, sizeof(*survey));
6008 
6009 	if (!fw_has_capa(&mvm->fw->ucode_capa,
6010 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
6011 		return -ENOENT;
6012 
6013 	/*
6014 	 * Return the beacon stats at index zero and pass on following indices
6015 	 * to the function returning the full survey, most likely for ACS
6016 	 * (Automatic Channel Selection).
6017 	 */
6018 	if (idx > 0)
6019 		return iwl_mvm_mac_get_acs_survey(mvm, idx - 1, survey);
6020 
6021 	guard(mvm)(mvm);
6022 
6023 	if (iwl_mvm_firmware_running(mvm)) {
6024 		int ret = iwl_mvm_request_statistics(mvm, false);
6025 
6026 		if (ret)
6027 			return ret;
6028 	}
6029 
6030 	survey->filled = SURVEY_INFO_TIME_RX |
6031 			 SURVEY_INFO_TIME_TX;
6032 
6033 	survey->time_rx = mvm->accu_radio_stats.rx_time +
6034 			  mvm->radio_stats.rx_time;
6035 	do_div(survey->time_rx, USEC_PER_MSEC);
6036 
6037 	survey->time_tx = mvm->accu_radio_stats.tx_time +
6038 			  mvm->radio_stats.tx_time;
6039 	do_div(survey->time_tx, USEC_PER_MSEC);
6040 
6041 	/* the new fw api doesn't support the following fields */
6042 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
6043 		return 0;
6044 
6045 	survey->filled |= SURVEY_INFO_TIME |
6046 			  SURVEY_INFO_TIME_SCAN;
6047 	survey->time = mvm->accu_radio_stats.on_time_rf +
6048 		       mvm->radio_stats.on_time_rf;
6049 	do_div(survey->time, USEC_PER_MSEC);
6050 
6051 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
6052 			    mvm->radio_stats.on_time_scan;
6053 	do_div(survey->time_scan, USEC_PER_MSEC);
6054 
6055 	return 0;
6056 }
6057 
6058 static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
6059 {
6060 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
6061 	u32 gi_ltf;
6062 
6063 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
6064 	case RATE_MCS_CHAN_WIDTH_20:
6065 		rinfo->bw = RATE_INFO_BW_20;
6066 		break;
6067 	case RATE_MCS_CHAN_WIDTH_40:
6068 		rinfo->bw = RATE_INFO_BW_40;
6069 		break;
6070 	case RATE_MCS_CHAN_WIDTH_80:
6071 		rinfo->bw = RATE_INFO_BW_80;
6072 		break;
6073 	case RATE_MCS_CHAN_WIDTH_160:
6074 		rinfo->bw = RATE_INFO_BW_160;
6075 		break;
6076 	case RATE_MCS_CHAN_WIDTH_320:
6077 		rinfo->bw = RATE_INFO_BW_320;
6078 		break;
6079 	}
6080 
6081 	if (format == RATE_MCS_CCK_MSK ||
6082 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
6083 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
6084 
6085 		/* add the offset needed to get to the legacy ofdm indices */
6086 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
6087 			rate += IWL_FIRST_OFDM_RATE;
6088 
6089 		switch (rate) {
6090 		case IWL_RATE_1M_INDEX:
6091 			rinfo->legacy = 10;
6092 			break;
6093 		case IWL_RATE_2M_INDEX:
6094 			rinfo->legacy = 20;
6095 			break;
6096 		case IWL_RATE_5M_INDEX:
6097 			rinfo->legacy = 55;
6098 			break;
6099 		case IWL_RATE_11M_INDEX:
6100 			rinfo->legacy = 110;
6101 			break;
6102 		case IWL_RATE_6M_INDEX:
6103 			rinfo->legacy = 60;
6104 			break;
6105 		case IWL_RATE_9M_INDEX:
6106 			rinfo->legacy = 90;
6107 			break;
6108 		case IWL_RATE_12M_INDEX:
6109 			rinfo->legacy = 120;
6110 			break;
6111 		case IWL_RATE_18M_INDEX:
6112 			rinfo->legacy = 180;
6113 			break;
6114 		case IWL_RATE_24M_INDEX:
6115 			rinfo->legacy = 240;
6116 			break;
6117 		case IWL_RATE_36M_INDEX:
6118 			rinfo->legacy = 360;
6119 			break;
6120 		case IWL_RATE_48M_INDEX:
6121 			rinfo->legacy = 480;
6122 			break;
6123 		case IWL_RATE_54M_INDEX:
6124 			rinfo->legacy = 540;
6125 		}
6126 		return;
6127 	}
6128 
6129 	rinfo->nss = u32_get_bits(rate_n_flags,
6130 				  RATE_MCS_NSS_MSK) + 1;
6131 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
6132 		RATE_HT_MCS_INDEX(rate_n_flags) :
6133 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
6134 
6135 	if (rate_n_flags & RATE_MCS_SGI_MSK)
6136 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
6137 
6138 	switch (format) {
6139 	case RATE_MCS_EHT_MSK:
6140 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
6141 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
6142 		break;
6143 	case RATE_MCS_HE_MSK:
6144 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
6145 
6146 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
6147 
6148 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
6149 			rinfo->bw = RATE_INFO_BW_HE_RU;
6150 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
6151 		}
6152 
6153 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
6154 		case RATE_MCS_HE_TYPE_SU:
6155 		case RATE_MCS_HE_TYPE_EXT_SU:
6156 			if (gi_ltf == 0 || gi_ltf == 1)
6157 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6158 			else if (gi_ltf == 2)
6159 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6160 			else if (gi_ltf == 3)
6161 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6162 			else
6163 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6164 			break;
6165 		case RATE_MCS_HE_TYPE_MU:
6166 			if (gi_ltf == 0 || gi_ltf == 1)
6167 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6168 			else if (gi_ltf == 2)
6169 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6170 			else
6171 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6172 			break;
6173 		case RATE_MCS_HE_TYPE_TRIG:
6174 			if (gi_ltf == 0 || gi_ltf == 1)
6175 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6176 			else
6177 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6178 			break;
6179 		}
6180 
6181 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
6182 			rinfo->he_dcm = 1;
6183 		break;
6184 	case RATE_MCS_HT_MSK:
6185 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
6186 		break;
6187 	case RATE_MCS_VHT_MSK:
6188 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
6189 		break;
6190 	}
6191 }
6192 
6193 void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
6194 				struct ieee80211_vif *vif,
6195 				struct ieee80211_sta *sta,
6196 				struct station_info *sinfo)
6197 {
6198 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6199 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6200 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
6201 	int i;
6202 
6203 	if (mvmsta->deflink.avg_energy) {
6204 		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
6205 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
6206 	}
6207 
6208 	if (iwl_mvm_has_tlc_offload(mvm)) {
6209 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
6210 
6211 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
6212 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6213 	}
6214 
6215 	/* if beacon filtering isn't on mac80211 does it anyway */
6216 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
6217 		return;
6218 
6219 	if (!vif->cfg.assoc)
6220 		return;
6221 
6222 	guard(mvm)(mvm);
6223 
6224 	if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
6225 		return;
6226 
6227 	if (iwl_mvm_request_statistics(mvm, false))
6228 		return;
6229 
6230 	sinfo->rx_beacon = 0;
6231 	for_each_mvm_vif_valid_link(mvmvif, i)
6232 		sinfo->rx_beacon += mvmvif->link[i]->beacon_stats.num_beacons +
6233 			mvmvif->link[i]->beacon_stats.accu_num_beacons;
6234 
6235 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
6236 	if (mvmvif->deflink.beacon_stats.avg_signal) {
6237 		/* firmware only reports a value after RXing a few beacons */
6238 		sinfo->rx_beacon_signal_avg =
6239 			mvmvif->deflink.beacon_stats.avg_signal;
6240 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
6241 	}
6242 }
6243 
6244 static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
6245 					    struct ieee80211_vif *vif,
6246 					    const  struct ieee80211_mlme_event *mlme)
6247 {
6248 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
6249 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
6250 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6251 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
6252 				       NULL);
6253 		return;
6254 	}
6255 
6256 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
6257 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6258 				       IWL_FW_INI_TIME_POINT_DEASSOC,
6259 				       NULL);
6260 		return;
6261 	}
6262 }
6263 
6264 static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
6265 					struct ieee80211_vif *vif,
6266 					const struct ieee80211_event *event)
6267 {
6268 #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
6269 	do {								\
6270 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
6271 			break;						\
6272 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
6273 	} while (0)
6274 
6275 	struct iwl_fw_dbg_trigger_tlv *trig;
6276 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
6277 
6278 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
6279 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
6280 		return;
6281 	}
6282 
6283 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
6284 				     FW_DBG_TRIGGER_MLME);
6285 	if (!trig)
6286 		return;
6287 
6288 	trig_mlme = (void *)trig->data;
6289 
6290 	if (event->u.mlme.data == ASSOC_EVENT) {
6291 		if (event->u.mlme.status == MLME_DENIED)
6292 			CHECK_MLME_TRIGGER(stop_assoc_denied,
6293 					   "DENIED ASSOC: reason %d",
6294 					    event->u.mlme.reason);
6295 		else if (event->u.mlme.status == MLME_TIMEOUT)
6296 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
6297 					   "ASSOC TIMEOUT");
6298 	} else if (event->u.mlme.data == AUTH_EVENT) {
6299 		if (event->u.mlme.status == MLME_DENIED)
6300 			CHECK_MLME_TRIGGER(stop_auth_denied,
6301 					   "DENIED AUTH: reason %d",
6302 					   event->u.mlme.reason);
6303 		else if (event->u.mlme.status == MLME_TIMEOUT)
6304 			CHECK_MLME_TRIGGER(stop_auth_timeout,
6305 					   "AUTH TIMEOUT");
6306 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
6307 		CHECK_MLME_TRIGGER(stop_rx_deauth,
6308 				   "DEAUTH RX %d", event->u.mlme.reason);
6309 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
6310 		CHECK_MLME_TRIGGER(stop_tx_deauth,
6311 				   "DEAUTH TX %d", event->u.mlme.reason);
6312 	}
6313 #undef CHECK_MLME_TRIGGER
6314 }
6315 
6316 static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
6317 					  struct ieee80211_vif *vif,
6318 					  const struct ieee80211_event *event)
6319 {
6320 	struct iwl_fw_dbg_trigger_tlv *trig;
6321 	struct iwl_fw_dbg_trigger_ba *ba_trig;
6322 
6323 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
6324 				     FW_DBG_TRIGGER_BA);
6325 	if (!trig)
6326 		return;
6327 
6328 	ba_trig = (void *)trig->data;
6329 
6330 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
6331 		return;
6332 
6333 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
6334 				"BAR received from %pM, tid %d, ssn %d",
6335 				event->u.ba.sta->addr, event->u.ba.tid,
6336 				event->u.ba.ssn);
6337 }
6338 
6339 void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
6340 				struct ieee80211_vif *vif,
6341 				const struct ieee80211_event *event)
6342 {
6343 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6344 
6345 	switch (event->type) {
6346 	case MLME_EVENT:
6347 		iwl_mvm_event_mlme_callback(mvm, vif, event);
6348 		break;
6349 	case BAR_RX_EVENT:
6350 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
6351 		break;
6352 	case BA_FRAME_TIMEOUT:
6353 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
6354 						     event->u.ba.tid);
6355 		break;
6356 	default:
6357 		break;
6358 	}
6359 }
6360 
6361 #define SYNC_RX_QUEUE_TIMEOUT (HZ)
6362 void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
6363 				     enum iwl_mvm_rxq_notif_type type,
6364 				     bool sync,
6365 				     const void *data, u32 size)
6366 {
6367 	struct {
6368 		struct iwl_rxq_sync_cmd cmd;
6369 		struct iwl_mvm_internal_rxq_notif notif;
6370 	} __packed cmd = {
6371 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
6372 		.cmd.count =
6373 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
6374 				    size),
6375 		.notif.type = type,
6376 		.notif.sync = sync,
6377 	};
6378 	struct iwl_host_cmd hcmd = {
6379 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
6380 		.data[0] = &cmd,
6381 		.len[0] = sizeof(cmd),
6382 		.data[1] = data,
6383 		.len[1] = size,
6384 		.flags = CMD_SEND_IN_RFKILL | (sync ? 0 : CMD_ASYNC),
6385 	};
6386 	int ret;
6387 
6388 	/* size must be a multiple of DWORD */
6389 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
6390 		return;
6391 
6392 	if (!iwl_mvm_has_new_rx_api(mvm))
6393 		return;
6394 
6395 	if (sync) {
6396 		cmd.notif.cookie = mvm->queue_sync_cookie;
6397 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6398 	}
6399 
6400 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
6401 	if (ret) {
6402 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
6403 		goto out;
6404 	}
6405 
6406 	if (sync) {
6407 		lockdep_assert_held(&mvm->mutex);
6408 		ret = wait_event_timeout(mvm->rx_sync_waitq,
6409 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
6410 					 iwl_mvm_is_radio_hw_killed(mvm),
6411 					 SYNC_RX_QUEUE_TIMEOUT);
6412 		WARN_ONCE(!ret && !iwl_mvm_is_radio_hw_killed(mvm),
6413 			  "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