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