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