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