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