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