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