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