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