xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
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_cancel(mvm->hw->wiphy, &mvm->trig_link_selection_wk);
1354 	wiphy_work_flush(mvm->hw->wiphy, &mvm->async_handlers_wiphy_wk);
1355 	flush_work(&mvm->async_handlers_wk);
1356 	flush_work(&mvm->add_stream_wk);
1357 
1358 	/*
1359 	 * Lock and clear the firmware running bit here already, so that
1360 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1361 	 * be able to proceed. This is important here because one of those
1362 	 * debugfs files causes the firmware dump to be triggered, and if we
1363 	 * don't stop debugfs accesses before canceling that it could be
1364 	 * retriggered after we flush it but before we've cleared the bit.
1365 	 */
1366 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1367 
1368 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
1369 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1370 
1371 	/*
1372 	 * The work item could be running or queued if the
1373 	 * ROC time event stops just as we get here.
1374 	 */
1375 	flush_work(&mvm->roc_done_wk);
1376 
1377 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
1378 
1379 	mutex_lock(&mvm->mutex);
1380 	__iwl_mvm_mac_stop(mvm);
1381 	mutex_unlock(&mvm->mutex);
1382 
1383 	/*
1384 	 * The worker might have been waiting for the mutex, let it run and
1385 	 * discover that its list is now empty.
1386 	 */
1387 	cancel_work_sync(&mvm->async_handlers_wk);
1388 	wiphy_work_cancel(hw->wiphy, &mvm->async_handlers_wiphy_wk);
1389 }
1390 
1391 struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1392 {
1393 	u16 i;
1394 
1395 	lockdep_assert_held(&mvm->mutex);
1396 
1397 	for (i = 0; i < NUM_PHY_CTX; i++)
1398 		if (!mvm->phy_ctxts[i].ref)
1399 			return &mvm->phy_ctxts[i];
1400 
1401 	IWL_ERR(mvm, "No available PHY context\n");
1402 	return NULL;
1403 }
1404 
1405 int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1406 			 s16 tx_power)
1407 {
1408 	u32 cmd_id = REDUCE_TX_POWER_CMD;
1409 	int len;
1410 	struct iwl_dev_tx_power_cmd cmd = {
1411 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1412 		.common.mac_context_id =
1413 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1414 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1415 	};
1416 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1417 					   IWL_FW_CMD_VER_UNKNOWN);
1418 
1419 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1420 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1421 
1422 	if (cmd_ver == 8)
1423 		len = sizeof(cmd.v8);
1424 	else if (cmd_ver == 7)
1425 		len = sizeof(cmd.v7);
1426 	else if (cmd_ver == 6)
1427 		len = sizeof(cmd.v6);
1428 	else if (fw_has_api(&mvm->fw->ucode_capa,
1429 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
1430 		len = sizeof(cmd.v5);
1431 	else if (fw_has_capa(&mvm->fw->ucode_capa,
1432 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
1433 		len = sizeof(cmd.v4);
1434 	else
1435 		len = sizeof(cmd.v3);
1436 
1437 	/* all structs have the same common part, add it */
1438 	len += sizeof(cmd.common);
1439 
1440 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1441 }
1442 
1443 static void iwl_mvm_post_csa_tx(void *data, struct ieee80211_sta *sta)
1444 {
1445 	struct ieee80211_hw *hw = data;
1446 	int i;
1447 
1448 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1449 		struct iwl_mvm_txq *mvmtxq =
1450 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1451 
1452 		clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1453 		iwl_mvm_mac_itxq_xmit(hw, sta->txq[i]);
1454 	}
1455 }
1456 
1457 int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1458 				struct ieee80211_vif *vif,
1459 				struct ieee80211_bss_conf *link_conf)
1460 {
1461 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1462 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1463 	int ret;
1464 
1465 	mutex_lock(&mvm->mutex);
1466 
1467 	if (vif->type == NL80211_IFTYPE_STATION) {
1468 		struct iwl_mvm_sta *mvmsta;
1469 		unsigned int link_id = link_conf->link_id;
1470 		u8 ap_sta_id = mvmvif->link[link_id]->ap_sta_id;
1471 
1472 		mvmvif->csa_bcn_pending = false;
1473 		mvmvif->csa_blocks_tx = false;
1474 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm, ap_sta_id);
1475 
1476 		if (WARN_ON(!mvmsta)) {
1477 			ret = -EIO;
1478 			goto out_unlock;
1479 		}
1480 
1481 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1482 		if (mvm->mld_api_is_used)
1483 			iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
1484 		else
1485 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1486 
1487 		if (!fw_has_capa(&mvm->fw->ucode_capa,
1488 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1489 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
1490 			if (ret)
1491 				goto out_unlock;
1492 
1493 			iwl_mvm_stop_session_protection(mvm, vif);
1494 		}
1495 	} else if (vif->type == NL80211_IFTYPE_AP && mvmvif->csa_blocks_tx) {
1496 		struct iwl_mvm_txq *mvmtxq =
1497 			iwl_mvm_txq_from_mac80211(vif->txq);
1498 
1499 		clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1500 
1501 		local_bh_disable();
1502 		iwl_mvm_mac_itxq_xmit(hw, vif->txq);
1503 		ieee80211_iterate_stations_atomic(hw, iwl_mvm_post_csa_tx, hw);
1504 		local_bh_enable();
1505 
1506 		mvmvif->csa_blocks_tx = false;
1507 	}
1508 
1509 	mvmvif->ps_disabled = false;
1510 
1511 	ret = iwl_mvm_power_update_ps(mvm);
1512 
1513 out_unlock:
1514 	if (mvmvif->csa_failed)
1515 		ret = -EIO;
1516 	mutex_unlock(&mvm->mutex);
1517 
1518 	return ret;
1519 }
1520 
1521 void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1522 				  struct ieee80211_vif *vif,
1523 				  struct ieee80211_bss_conf *link_conf)
1524 {
1525 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1526 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1527 	struct iwl_chan_switch_te_cmd cmd = {
1528 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1529 							  mvmvif->color)),
1530 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1531 	};
1532 
1533 	/*
1534 	 * In the new flow since FW is in charge of the timing,
1535 	 * if driver has canceled the channel switch he will receive the
1536 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1537 	 */
1538 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1539 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1540 		return;
1541 
1542 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1543 
1544 	mutex_lock(&mvm->mutex);
1545 	if (!fw_has_capa(&mvm->fw->ucode_capa,
1546 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
1547 		iwl_mvm_remove_csa_period(mvm, vif);
1548 	else
1549 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1550 					     WIDE_ID(MAC_CONF_GROUP,
1551 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1552 					     0, sizeof(cmd), &cmd));
1553 	mvmvif->csa_failed = true;
1554 	mutex_unlock(&mvm->mutex);
1555 
1556 	/* If we're here, we can't support MLD */
1557 	iwl_mvm_post_channel_switch(hw, vif, &vif->bss_conf);
1558 }
1559 
1560 void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1561 {
1562 	struct iwl_mvm_vif *mvmvif;
1563 	struct ieee80211_vif *vif;
1564 
1565 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1566 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1567 
1568 	/* Trigger disconnect (should clear the CSA state) */
1569 	ieee80211_chswitch_done(vif, false, 0);
1570 }
1571 
1572 static u8
1573 iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
1574 {
1575 	int data_start;
1576 	int control_start;
1577 	int bw;
1578 
1579 	if (chandef->width == NL80211_CHAN_WIDTH_320)
1580 		bw = 320;
1581 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
1582 		bw = 160;
1583 	else
1584 		return 0;
1585 
1586 	/* data is bw wide so the start is half the width */
1587 	data_start = chandef->center_freq1 - bw / 2;
1588 	/* control is 20Mhz width */
1589 	control_start = chandef->chan->center_freq - 10;
1590 
1591 	return (control_start - data_start) / 80;
1592 }
1593 
1594 static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
1595 					 struct ieee80211_vif *vif)
1596 {
1597 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1598 	int ret;
1599 
1600 	lockdep_assert_held(&mvm->mutex);
1601 
1602 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1603 	if (ret) {
1604 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1605 		return ret;
1606 	}
1607 
1608 	/* Only queue for this station is the mcast queue,
1609 	 * which shouldn't be in TFD mask anyway
1610 	 */
1611 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
1612 					vif->type,
1613 					IWL_STA_MULTICAST);
1614 }
1615 
1616 static void iwl_mvm_prevent_esr_done_wk(struct wiphy *wiphy,
1617 					struct wiphy_work *wk)
1618 {
1619 	struct iwl_mvm_vif *mvmvif =
1620 		container_of(wk, struct iwl_mvm_vif, prevent_esr_done_wk.work);
1621 	struct iwl_mvm *mvm = mvmvif->mvm;
1622 	struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
1623 
1624 	mutex_lock(&mvm->mutex);
1625 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_PREVENTION);
1626 	mutex_unlock(&mvm->mutex);
1627 }
1628 
1629 static void iwl_mvm_mlo_int_scan_wk(struct wiphy *wiphy, struct wiphy_work *wk)
1630 {
1631 	struct iwl_mvm_vif *mvmvif = container_of(wk, struct iwl_mvm_vif,
1632 						  mlo_int_scan_wk.work);
1633 	struct ieee80211_vif *vif =
1634 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1635 
1636 	mutex_lock(&mvmvif->mvm->mutex);
1637 
1638 	iwl_mvm_int_mlo_scan(mvmvif->mvm, vif);
1639 
1640 	mutex_unlock(&mvmvif->mvm->mutex);
1641 }
1642 
1643 static void iwl_mvm_unblock_esr_tpt(struct wiphy *wiphy, struct wiphy_work *wk)
1644 {
1645 	struct iwl_mvm_vif *mvmvif =
1646 		container_of(wk, struct iwl_mvm_vif, unblock_esr_tpt_wk);
1647 	struct iwl_mvm *mvm = mvmvif->mvm;
1648 	struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
1649 
1650 	mutex_lock(&mvm->mutex);
1651 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT);
1652 	mutex_unlock(&mvm->mutex);
1653 }
1654 
1655 void iwl_mvm_mac_init_mvmvif(struct iwl_mvm *mvm, struct iwl_mvm_vif *mvmvif)
1656 {
1657 	lockdep_assert_held(&mvm->mutex);
1658 
1659 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1660 		return;
1661 
1662 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1663 			  iwl_mvm_channel_switch_disconnect_wk);
1664 
1665 	wiphy_delayed_work_init(&mvmvif->prevent_esr_done_wk,
1666 				iwl_mvm_prevent_esr_done_wk);
1667 
1668 	wiphy_delayed_work_init(&mvmvif->mlo_int_scan_wk,
1669 				iwl_mvm_mlo_int_scan_wk);
1670 
1671 	wiphy_work_init(&mvmvif->unblock_esr_tpt_wk,
1672 			iwl_mvm_unblock_esr_tpt);
1673 }
1674 
1675 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1676 				     struct ieee80211_vif *vif)
1677 {
1678 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1679 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1680 	int ret;
1681 	int i;
1682 
1683 	mutex_lock(&mvm->mutex);
1684 
1685 	iwl_mvm_mac_init_mvmvif(mvm, mvmvif);
1686 
1687 	mvmvif->mvm = mvm;
1688 
1689 	/* the first link always points to the default one */
1690 	mvmvif->deflink.fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1691 	mvmvif->deflink.active = 0;
1692 	mvmvif->link[0] = &mvmvif->deflink;
1693 
1694 	ret = iwl_mvm_set_link_mapping(mvm, vif, &vif->bss_conf);
1695 	if (ret)
1696 		goto out;
1697 
1698 	/*
1699 	 * Not much to do here. The stack will not allow interface
1700 	 * types or combinations that we didn't advertise, so we
1701 	 * don't really have to check the types.
1702 	 */
1703 
1704 	/* make sure that beacon statistics don't go backwards with FW reset */
1705 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1706 		for_each_mvm_vif_valid_link(mvmvif, i)
1707 			mvmvif->link[i]->beacon_stats.accu_num_beacons +=
1708 				mvmvif->link[i]->beacon_stats.num_beacons;
1709 
1710 	/* Allocate resources for the MAC context, and add it to the fw  */
1711 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1712 	if (ret)
1713 		goto out;
1714 
1715 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1716 
1717 	/* Currently not much to do for NAN */
1718 	if (vif->type == NL80211_IFTYPE_NAN) {
1719 		ret = 0;
1720 		goto out;
1721 	}
1722 
1723 	/*
1724 	 * The AP binding flow can be done only after the beacon
1725 	 * template is configured (which happens only in the mac80211
1726 	 * start_ap() flow), and adding the broadcast station can happen
1727 	 * only after the binding.
1728 	 * In addition, since modifying the MAC before adding a bcast
1729 	 * station is not allowed by the FW, delay the adding of MAC context to
1730 	 * the point where we can also add the bcast station.
1731 	 * In short: there's not much we can do at this point, other than
1732 	 * allocating resources :)
1733 	 */
1734 	if (vif->type == NL80211_IFTYPE_AP ||
1735 	    vif->type == NL80211_IFTYPE_ADHOC) {
1736 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1737 			iwl_mvm_vif_dbgfs_add_link(mvm, vif);
1738 		ret = 0;
1739 		goto out;
1740 	}
1741 
1742 	mvmvif->features |= hw->netdev_features;
1743 
1744 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1745 	if (ret)
1746 		goto out_unlock;
1747 
1748 	ret = iwl_mvm_power_update_mac(mvm);
1749 	if (ret)
1750 		goto out_remove_mac;
1751 
1752 	/* beacon filtering */
1753 	ret = iwl_mvm_disable_beacon_filter(mvm, vif);
1754 	if (ret)
1755 		goto out_remove_mac;
1756 
1757 	if (!mvm->bf_allowed_vif &&
1758 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1759 		mvm->bf_allowed_vif = mvmvif;
1760 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1761 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1762 	}
1763 
1764 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1765 		mvm->p2p_device_vif = vif;
1766 
1767 	iwl_mvm_tcm_add_vif(mvm, vif);
1768 
1769 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1770 		mvm->monitor_on = true;
1771 		mvm->monitor_p80 =
1772 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chanreq.oper);
1773 	}
1774 
1775 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1776 		iwl_mvm_vif_dbgfs_add_link(mvm, vif);
1777 
1778 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1779 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
1780 	    !mvm->csme_vif && mvm->mei_registered) {
1781 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
1782 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
1783 		mvm->csme_vif = vif;
1784 	}
1785 
1786 out:
1787 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
1788 		     vif->type == NL80211_IFTYPE_ADHOC))
1789 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
1790 
1791 	goto out_unlock;
1792 
1793  out_remove_mac:
1794 	mvmvif->deflink.phy_ctxt = NULL;
1795 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1796  out_unlock:
1797 	mutex_unlock(&mvm->mutex);
1798 
1799 	return ret;
1800 }
1801 
1802 void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1803 				 struct ieee80211_vif *vif)
1804 {
1805 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1806 
1807 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1808 		/*
1809 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1810 		 * We assume here that all the packets sent to the OFFCHANNEL
1811 		 * queue are sent in ROC session.
1812 		 */
1813 		flush_work(&mvm->roc_done_wk);
1814 	}
1815 
1816 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1817 				  &mvmvif->prevent_esr_done_wk);
1818 
1819 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1820 				  &mvmvif->mlo_int_scan_wk);
1821 
1822 	wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
1823 
1824 	cancel_delayed_work_sync(&mvmvif->csa_work);
1825 }
1826 
1827 /* This function is doing the common part of removing the interface for
1828  * both - MLD and non-MLD modes. Returns true if removing the interface
1829  * is done
1830  */
1831 static bool iwl_mvm_mac_remove_interface_common(struct ieee80211_hw *hw,
1832 						struct ieee80211_vif *vif)
1833 {
1834 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1835 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1836 	struct iwl_probe_resp_data *probe_data;
1837 
1838 	iwl_mvm_prepare_mac_removal(mvm, vif);
1839 
1840 	if (!(vif->type == NL80211_IFTYPE_AP ||
1841 	      vif->type == NL80211_IFTYPE_ADHOC))
1842 		iwl_mvm_tcm_rm_vif(mvm, vif);
1843 
1844 	mutex_lock(&mvm->mutex);
1845 
1846 	if (vif == mvm->csme_vif) {
1847 		iwl_mei_set_netdev(NULL);
1848 		mvm->csme_vif = NULL;
1849 	}
1850 
1851 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1852 					       lockdep_is_held(&mvm->mutex));
1853 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1854 	if (probe_data)
1855 		kfree_rcu(probe_data, rcu_head);
1856 
1857 	if (mvm->bf_allowed_vif == mvmvif) {
1858 		mvm->bf_allowed_vif = NULL;
1859 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1860 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1861 	}
1862 
1863 	if (vif->bss_conf.ftm_responder)
1864 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1865 
1866 	iwl_mvm_vif_dbgfs_rm_link(mvm, vif);
1867 
1868 	/*
1869 	 * For AP/GO interface, the tear down of the resources allocated to the
1870 	 * interface is be handled as part of the stop_ap flow.
1871 	 */
1872 	if (vif->type == NL80211_IFTYPE_AP ||
1873 	    vif->type == NL80211_IFTYPE_ADHOC) {
1874 #ifdef CONFIG_NL80211_TESTMODE
1875 		if (vif == mvm->noa_vif) {
1876 			mvm->noa_vif = NULL;
1877 			mvm->noa_duration = 0;
1878 		}
1879 #endif
1880 		return true;
1881 	}
1882 
1883 	iwl_mvm_power_update_mac(mvm);
1884 	return false;
1885 }
1886 
1887 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1888 					 struct ieee80211_vif *vif)
1889 {
1890 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1891 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1892 
1893 	if (iwl_mvm_mac_remove_interface_common(hw, vif))
1894 		goto out;
1895 
1896 	/* Before the interface removal, mac80211 would cancel the ROC, and the
1897 	 * ROC worker would be scheduled if needed. The worker would be flushed
1898 	 * in iwl_mvm_prepare_mac_removal() and thus at this point there is no
1899 	 * binding etc. so nothing needs to be done here.
1900 	 */
1901 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1902 		if (mvmvif->deflink.phy_ctxt) {
1903 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1904 			mvmvif->deflink.phy_ctxt = NULL;
1905 		}
1906 		mvm->p2p_device_vif = NULL;
1907 	}
1908 
1909 	iwl_mvm_unset_link_mapping(mvm, vif, &vif->bss_conf);
1910 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1911 
1912 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1913 
1914 	if (vif->type == NL80211_IFTYPE_MONITOR)
1915 		mvm->monitor_on = false;
1916 
1917 out:
1918 	if (vif->type == NL80211_IFTYPE_AP ||
1919 	    vif->type == NL80211_IFTYPE_ADHOC) {
1920 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
1921 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1922 	}
1923 
1924 	mutex_unlock(&mvm->mutex);
1925 }
1926 
1927 struct iwl_mvm_mc_iter_data {
1928 	struct iwl_mvm *mvm;
1929 	int port_id;
1930 };
1931 
1932 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1933 				      struct ieee80211_vif *vif)
1934 {
1935 	struct iwl_mvm_mc_iter_data *data = _data;
1936 	struct iwl_mvm *mvm = data->mvm;
1937 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1938 	struct iwl_host_cmd hcmd = {
1939 		.id = MCAST_FILTER_CMD,
1940 		.flags = CMD_ASYNC,
1941 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1942 	};
1943 	int ret, len;
1944 
1945 	/* if we don't have free ports, mcast frames will be dropped */
1946 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1947 		return;
1948 
1949 	if (vif->type != NL80211_IFTYPE_STATION ||
1950 	    !vif->cfg.assoc)
1951 		return;
1952 
1953 	cmd->port_id = data->port_id++;
1954 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1955 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1956 
1957 	hcmd.len[0] = len;
1958 	hcmd.data[0] = cmd;
1959 
1960 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1961 	if (ret)
1962 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1963 }
1964 
1965 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1966 {
1967 	struct iwl_mvm_mc_iter_data iter_data = {
1968 		.mvm = mvm,
1969 	};
1970 	int ret;
1971 
1972 	lockdep_assert_held(&mvm->mutex);
1973 
1974 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1975 		return;
1976 
1977 	ieee80211_iterate_active_interfaces_atomic(
1978 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1979 		iwl_mvm_mc_iface_iterator, &iter_data);
1980 
1981 	/*
1982 	 * Send a (synchronous) ech command so that we wait for the
1983 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
1984 	 * the interface iterator. Otherwise, we might get here over
1985 	 * and over again (by userspace just sending a lot of these)
1986 	 * and the CPU can send them faster than the firmware can
1987 	 * process them.
1988 	 * Note that the CPU is still faster - but with this we'll
1989 	 * actually send fewer commands overall because the CPU will
1990 	 * not schedule the work in mac80211 as frequently if it's
1991 	 * still running when rescheduled (possibly multiple times).
1992 	 */
1993 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1994 	if (ret)
1995 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
1996 }
1997 
1998 u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1999 			      struct netdev_hw_addr_list *mc_list)
2000 {
2001 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2002 	struct iwl_mcast_filter_cmd *cmd;
2003 	struct netdev_hw_addr *addr;
2004 	int addr_count;
2005 	bool pass_all;
2006 	int len;
2007 
2008 	addr_count = netdev_hw_addr_list_count(mc_list);
2009 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
2010 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
2011 	if (pass_all)
2012 		addr_count = 0;
2013 
2014 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
2015 	cmd = kzalloc(len, GFP_ATOMIC);
2016 	if (!cmd)
2017 		return 0;
2018 
2019 	if (pass_all) {
2020 		cmd->pass_all = 1;
2021 		return (u64)(unsigned long)cmd;
2022 	}
2023 
2024 	netdev_hw_addr_list_for_each(addr, mc_list) {
2025 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
2026 				   cmd->count, addr->addr);
2027 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
2028 		       addr->addr, ETH_ALEN);
2029 		cmd->count++;
2030 	}
2031 
2032 	return (u64)(unsigned long)cmd;
2033 }
2034 
2035 void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
2036 			      unsigned int changed_flags,
2037 			      unsigned int *total_flags, u64 multicast)
2038 {
2039 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2040 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
2041 
2042 	mutex_lock(&mvm->mutex);
2043 
2044 	/* replace previous configuration */
2045 	kfree(mvm->mcast_filter_cmd);
2046 	mvm->mcast_filter_cmd = cmd;
2047 
2048 	if (!cmd)
2049 		goto out;
2050 
2051 	if (changed_flags & FIF_ALLMULTI)
2052 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
2053 
2054 	if (cmd->pass_all)
2055 		cmd->count = 0;
2056 
2057 	iwl_mvm_recalc_multicast(mvm);
2058 out:
2059 	mutex_unlock(&mvm->mutex);
2060 	*total_flags = 0;
2061 }
2062 
2063 static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
2064 					struct ieee80211_vif *vif,
2065 					unsigned int filter_flags,
2066 					unsigned int changed_flags)
2067 {
2068 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2069 
2070 	/* We support only filter for probe requests */
2071 	if (!(changed_flags & FIF_PROBE_REQ))
2072 		return;
2073 
2074 	/* Supported only for p2p client interfaces */
2075 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
2076 	    !vif->p2p)
2077 		return;
2078 
2079 	mutex_lock(&mvm->mutex);
2080 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2081 	mutex_unlock(&mvm->mutex);
2082 }
2083 
2084 int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2085 {
2086 	struct iwl_mu_group_mgmt_cmd cmd = {};
2087 
2088 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
2089 	       WLAN_MEMBERSHIP_LEN);
2090 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
2091 	       WLAN_USER_POSITION_LEN);
2092 
2093 	return iwl_mvm_send_cmd_pdu(mvm,
2094 				    WIDE_ID(DATA_PATH_GROUP,
2095 					    UPDATE_MU_GROUPS_CMD),
2096 				    0, sizeof(cmd), &cmd);
2097 }
2098 
2099 static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
2100 					   struct ieee80211_vif *vif)
2101 {
2102 	if (vif->bss_conf.mu_mimo_owner) {
2103 		struct iwl_mu_group_mgmt_notif *notif = _data;
2104 
2105 		/*
2106 		 * MU-MIMO Group Id action frame is little endian. We treat
2107 		 * the data received from firmware as if it came from the
2108 		 * action frame, so no conversion is needed.
2109 		 */
2110 		ieee80211_update_mu_groups(vif, 0,
2111 					   (u8 *)&notif->membership_status,
2112 					   (u8 *)&notif->user_position);
2113 	}
2114 }
2115 
2116 void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
2117 			       struct iwl_rx_cmd_buffer *rxb)
2118 {
2119 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2120 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
2121 
2122 	ieee80211_iterate_active_interfaces_atomic(
2123 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2124 			iwl_mvm_mu_mimo_iface_iterator, notif);
2125 }
2126 
2127 static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2128 {
2129 	u8 byte_num = ppe_pos_bit / 8;
2130 	u8 bit_num = ppe_pos_bit % 8;
2131 	u8 residue_bits;
2132 	u8 res;
2133 
2134 	if (bit_num <= 5)
2135 		return (ppe[byte_num] >> bit_num) &
2136 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2137 
2138 	/*
2139 	 * If bit_num > 5, we have to combine bits with next byte.
2140 	 * Calculate how many bits we need to take from current byte (called
2141 	 * here "residue_bits"), and add them to bits from next byte.
2142 	 */
2143 
2144 	residue_bits = 8 - bit_num;
2145 
2146 	res = (ppe[byte_num + 1] &
2147 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2148 	      residue_bits;
2149 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2150 
2151 	return res;
2152 }
2153 
2154 static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
2155 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
2156 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
2157 			      bool inheritance)
2158 {
2159 	int i;
2160 
2161 	/*
2162 	* FW currently supports only nss == MAX_HE_SUPP_NSS
2163 	*
2164 	* If nss > MAX: we can ignore values we don't support
2165 	* If nss < MAX: we can set zeros in other streams
2166 	*/
2167 	if (nss > MAX_HE_SUPP_NSS) {
2168 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2169 			       MAX_HE_SUPP_NSS);
2170 		nss = MAX_HE_SUPP_NSS;
2171 	}
2172 
2173 	for (i = 0; i < nss; i++) {
2174 		u8 ru_index_tmp = ru_index_bitmap << 1;
2175 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2176 		u8 bw;
2177 
2178 		for (bw = 0;
2179 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2180 		     bw++) {
2181 			ru_index_tmp >>= 1;
2182 
2183 			/*
2184 			* According to the 11be spec, if for a specific BW the PPE Thresholds
2185 			* isn't present - it should inherit the thresholds from the last
2186 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2187 			* this inheritance - continue in this case
2188 			*/
2189 			if (!(ru_index_tmp & 1)) {
2190 				if (inheritance)
2191 					goto set_thresholds;
2192 				else
2193 					continue;
2194 			}
2195 
2196 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2197 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2198 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2199 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2200 
2201 set_thresholds:
2202 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2203 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2204 		}
2205 	}
2206 }
2207 
2208 static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
2209 					    struct ieee80211_link_sta *link_sta,
2210 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2211 					    bool inheritance)
2212 {
2213 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
2214 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2215 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2216 	u8 ru_index_bitmap =
2217 		u8_get_bits(*ppe,
2218 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2219 	/* Starting after PPE header */
2220 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2221 
2222 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2223 			  inheritance);
2224 }
2225 
2226 static int
2227 iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
2228 					 u8 nominal_padding)
2229 {
2230 	int low_th = -1;
2231 	int high_th = -1;
2232 	int i;
2233 
2234 	/* all the macros are the same for EHT and HE */
2235 	switch (nominal_padding) {
2236 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2237 		low_th = IWL_HE_PKT_EXT_NONE;
2238 		high_th = IWL_HE_PKT_EXT_NONE;
2239 		break;
2240 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2241 		low_th = IWL_HE_PKT_EXT_BPSK;
2242 		high_th = IWL_HE_PKT_EXT_NONE;
2243 		break;
2244 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2245 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2246 		low_th = IWL_HE_PKT_EXT_NONE;
2247 		high_th = IWL_HE_PKT_EXT_BPSK;
2248 		break;
2249 	}
2250 
2251 	if (low_th < 0 || high_th < 0)
2252 		return -EINVAL;
2253 
2254 	/* Set the PPE thresholds accordingly */
2255 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2256 		u8 bw;
2257 
2258 		for (bw = 0;
2259 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2260 			bw++) {
2261 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2262 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2263 		}
2264 	}
2265 
2266 	return 0;
2267 }
2268 
2269 static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2270 					 u8 nominal_padding)
2271 {
2272 	int i;
2273 
2274 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2275 		u8 bw;
2276 
2277 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2278 		     bw++) {
2279 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2280 
2281 			if (nominal_padding >
2282 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2283 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2284 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2285 			else if (nominal_padding ==
2286 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2287 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2288 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2289 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2290 		}
2291 	}
2292 }
2293 
2294 /* Set the pkt_ext field according to PPE Thresholds element */
2295 int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
2296 			    struct ieee80211_link_sta *link_sta,
2297 			    struct iwl_he_pkt_ext_v2 *pkt_ext)
2298 {
2299 	u8 nominal_padding;
2300 	int i, ret = 0;
2301 
2302 	if (WARN_ON(!link_sta))
2303 		return -EINVAL;
2304 
2305 	/* Initialize the PPE thresholds to "None" (7), as described in Table
2306 	 * 9-262ac of 80211.ax/D3.0.
2307 	 */
2308 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
2309 	       sizeof(struct iwl_he_pkt_ext_v2));
2310 
2311 	if (link_sta->eht_cap.has_eht) {
2312 		nominal_padding =
2313 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
2314 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
2315 
2316 		/* If PPE Thresholds exists, parse them into a FW-familiar
2317 		 * format.
2318 		 */
2319 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
2320 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
2321 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
2322 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
2323 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
2324 			u8 ru_index_bitmap =
2325 				u16_get_bits(*ppe,
2326 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2327 			 /* Starting after PPE header */
2328 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
2329 
2330 			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
2331 					  ppe, ppe_pos_bit, true);
2332 		/* EHT PPE Thresholds doesn't exist - set the API according to
2333 		 * HE PPE Tresholds
2334 		 */
2335 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2336 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2337 			/* Even though HE Capabilities IE doesn't contain PPE
2338 			 * Thresholds for BW 320Mhz, thresholds for this BW will
2339 			 * be filled in with the same values as 160Mhz, due to
2340 			 * the inheritance, as required.
2341 			 */
2342 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2343 							true);
2344 
2345 			/* According to the requirements, for MCSs 12-13 the
2346 			 * maximum value between HE PPE Threshold and Common
2347 			 * Nominal Packet Padding needs to be taken
2348 			 */
2349 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
2350 
2351 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
2352 		 * take the Thresholds from Common Nominal Packet Padding field
2353 		 */
2354 		} else {
2355 			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2356 								       nominal_padding);
2357 		}
2358 	} else if (link_sta->he_cap.has_he) {
2359 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2360 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2361 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2362 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2363 							false);
2364 		/* PPE Thresholds doesn't exist - set the API PPE values
2365 		 * according to Common Nominal Packet Padding field.
2366 		 */
2367 		} else {
2368 			nominal_padding =
2369 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
2370 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
2371 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
2372 				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2373 									       nominal_padding);
2374 		}
2375 	}
2376 
2377 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2378 		int bw;
2379 
2380 		for (bw = 0;
2381 		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
2382 		     bw++) {
2383 			u8 *qam_th =
2384 				&pkt_ext->pkt_ext_qam_th[i][bw][0];
2385 
2386 			IWL_DEBUG_HT(mvm,
2387 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
2388 				     i, bw, qam_th[0], qam_th[1]);
2389 		}
2390 	}
2391 	return ret;
2392 }
2393 
2394 /*
2395  * This function sets the MU EDCA parameters ans returns whether MU EDCA
2396  * is enabled or not
2397  */
2398 bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2399 				   const struct iwl_mvm_vif_link_info *link_info,
2400 				   struct iwl_he_backoff_conf *trig_based_txf)
2401 {
2402 	int i;
2403 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2404 	bool mu_edca_enabled = true;
2405 
2406 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2407 		const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2408 			&link_info->queue_params[i].mu_edca_param_rec;
2409 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2410 
2411 		if (!link_info->queue_params[i].mu_edca) {
2412 			mu_edca_enabled = false;
2413 			break;
2414 		}
2415 
2416 		trig_based_txf[ac].cwmin =
2417 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
2418 		trig_based_txf[ac].cwmax =
2419 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
2420 		trig_based_txf[ac].aifsn =
2421 			cpu_to_le16(mu_edca->aifsn & 0xf);
2422 		trig_based_txf[ac].mu_time =
2423 			cpu_to_le16(mu_edca->mu_edca_timer);
2424 	}
2425 
2426 	return mu_edca_enabled;
2427 }
2428 
2429 bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2430 {
2431 	const struct ieee80211_supported_band *sband;
2432 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
2433 
2434 	/* This capability is the same for all bands,
2435 	 * so take it from one of them.
2436 	 */
2437 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
2438 	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
2439 
2440 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
2441 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
2442 }
2443 
2444 __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
2445 				 struct ieee80211_link_sta *link_sta)
2446 {
2447 	u8 *mac_cap_info =
2448 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
2449 	__le32 htc_flags = 0;
2450 
2451 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
2452 		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2453 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2454 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2455 		u8 link_adap =
2456 			((mac_cap_info[2] &
2457 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2458 			 (mac_cap_info[1] &
2459 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2460 
2461 		if (link_adap == 2)
2462 			htc_flags |=
2463 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2464 		else if (link_adap == 3)
2465 			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2466 	}
2467 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2468 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2469 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2470 		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2471 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2472 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2473 
2474 	return htc_flags;
2475 }
2476 
2477 static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2478 			       struct ieee80211_vif *vif, u8 sta_id)
2479 {
2480 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2481 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2482 		.sta_id = sta_id,
2483 		.tid_limit = IWL_MAX_TID_COUNT,
2484 		.bss_color = vif->bss_conf.he_bss_color.color,
2485 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2486 		.frame_time_rts_th =
2487 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2488 	};
2489 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
2490 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
2491 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
2492 	int size;
2493 	struct ieee80211_sta *sta;
2494 	u32 flags;
2495 	int i;
2496 	void *cmd;
2497 
2498 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
2499 		ver = 1;
2500 
2501 	switch (ver) {
2502 	case 1:
2503 		/* same layout as v2 except some data at the end */
2504 		cmd = &sta_ctxt_cmd_v2;
2505 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
2506 		break;
2507 	case 2:
2508 		cmd = &sta_ctxt_cmd_v2;
2509 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
2510 		break;
2511 	case 3:
2512 		cmd = &sta_ctxt_cmd;
2513 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
2514 		break;
2515 	default:
2516 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
2517 		return;
2518 	}
2519 
2520 	rcu_read_lock();
2521 
2522 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2523 	if (IS_ERR_OR_NULL(sta)) {
2524 		rcu_read_unlock();
2525 		WARN(1, "Can't find STA to configure HE\n");
2526 		return;
2527 	}
2528 
2529 	if (!sta->deflink.he_cap.has_he) {
2530 		rcu_read_unlock();
2531 		return;
2532 	}
2533 
2534 	flags = 0;
2535 
2536 	/* Block 26-tone RU OFDMA transmissions */
2537 	if (mvmvif->deflink.he_ru_2mhz_block)
2538 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
2539 
2540 	/* HTC flags */
2541 	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2542 
2543 	/* PPE Thresholds */
2544 	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2545 		flags |= STA_CTXT_HE_PACKET_EXT;
2546 
2547 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2548 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
2549 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2550 
2551 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2552 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
2553 		flags |= STA_CTXT_HE_ACK_ENABLED;
2554 
2555 	rcu_read_unlock();
2556 
2557 	if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
2558 					  &sta_ctxt_cmd.trig_based_txf[0]))
2559 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2560 
2561 	if (vif->bss_conf.uora_exists) {
2562 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2563 
2564 		sta_ctxt_cmd.rand_alloc_ecwmin =
2565 			vif->bss_conf.uora_ocw_range & 0x7;
2566 		sta_ctxt_cmd.rand_alloc_ecwmax =
2567 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2568 	}
2569 
2570 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2571 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2572 
2573 	if (vif->bss_conf.nontransmitted) {
2574 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2575 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2576 				vif->bss_conf.transmitter_bssid);
2577 		sta_ctxt_cmd.max_bssid_indicator =
2578 			vif->bss_conf.bssid_indicator;
2579 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2580 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2581 		sta_ctxt_cmd.profile_periodicity =
2582 			vif->bss_conf.profile_periodicity;
2583 	}
2584 
2585 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2586 
2587 	if (ver < 3) {
2588 		/* fields before pkt_ext */
2589 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
2590 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
2591 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
2592 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
2593 
2594 		/* pkt_ext */
2595 		for (i = 0;
2596 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
2597 		     i++) {
2598 			u8 bw;
2599 
2600 			for (bw = 0;
2601 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
2602 			     bw++) {
2603 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
2604 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
2605 
2606 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
2607 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
2608 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
2609 			}
2610 		}
2611 
2612 		/* fields after pkt_ext */
2613 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
2614 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
2615 			     sizeof(sta_ctxt_cmd_v2) -
2616 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
2617 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
2618 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
2619 		       (u8 *)&sta_ctxt_cmd +
2620 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
2621 		       sizeof(sta_ctxt_cmd) -
2622 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
2623 		sta_ctxt_cmd_v2.reserved3 = 0;
2624 	}
2625 
2626 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2627 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2628 }
2629 
2630 void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2631 			   u32 duration_override, unsigned int link_id)
2632 {
2633 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2634 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2635 
2636 	if (duration_override > duration)
2637 		duration = duration_override;
2638 
2639 	/* Try really hard to protect the session and hear a beacon
2640 	 * The new session protection command allows us to protect the
2641 	 * session for a much longer time since the firmware will internally
2642 	 * create two events: a 300TU one with a very high priority that
2643 	 * won't be fragmented which should be enough for 99% of the cases,
2644 	 * and another one (which we configure here to be 900TU long) which
2645 	 * will have a slightly lower priority, but more importantly, can be
2646 	 * fragmented so that it'll allow other activities to run.
2647 	 */
2648 	if (fw_has_capa(&mvm->fw->ucode_capa,
2649 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2650 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2651 						    min_duration, false,
2652 						    link_id);
2653 	else
2654 		iwl_mvm_protect_session(mvm, vif, duration,
2655 					min_duration, 500, false);
2656 }
2657 
2658 /* Handle association common part to MLD and non-MLD modes */
2659 void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2660 					    struct ieee80211_vif *vif,
2661 					    u64 changes)
2662 {
2663 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2664 	int ret;
2665 	int link_id;
2666 
2667 	/* The firmware tracks the MU-MIMO group on its own.
2668 	 * However, on HW restart we should restore this data.
2669 	 */
2670 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2671 	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2672 		ret = iwl_mvm_update_mu_groups(mvm, vif);
2673 		if (ret)
2674 			IWL_ERR(mvm,
2675 				"failed to update VHT MU_MIMO groups\n");
2676 	}
2677 
2678 	iwl_mvm_recalc_multicast(mvm);
2679 
2680 	/* reset rssi values */
2681 	for_each_mvm_vif_valid_link(mvmvif, link_id)
2682 		mvmvif->link[link_id]->bf_data.ave_beacon_signal = 0;
2683 
2684 	iwl_mvm_bt_coex_vif_change(mvm);
2685 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2686 					    IEEE80211_SMPS_AUTOMATIC);
2687 	if (fw_has_capa(&mvm->fw->ucode_capa,
2688 			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2689 		iwl_mvm_config_scan(mvm);
2690 }
2691 
2692 /* Execute the common part for MLD and non-MLD modes */
2693 void
2694 iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2695 					struct ieee80211_vif *vif,
2696 					struct ieee80211_bss_conf *link_conf,
2697 					u64 changes)
2698 {
2699 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2700 	int ret;
2701 
2702 	if (changes & BSS_CHANGED_BEACON_INFO) {
2703 		/* We received a beacon from the associated AP so
2704 		 * remove the session protection.
2705 		 */
2706 		iwl_mvm_stop_session_protection(mvm, vif);
2707 
2708 		iwl_mvm_sf_update(mvm, vif, false);
2709 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
2710 	}
2711 
2712 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2713 		       /* Send power command on every beacon change,
2714 			* because we may have not enabled beacon abort yet.
2715 			*/
2716 		       BSS_CHANGED_BEACON_INFO)) {
2717 		ret = iwl_mvm_power_update_mac(mvm);
2718 		if (ret)
2719 			IWL_ERR(mvm, "failed to update power mode\n");
2720 	}
2721 
2722 	if (changes & BSS_CHANGED_CQM) {
2723 		struct iwl_mvm_vif_link_info *link_info =
2724 			mvmvif->link[link_conf->link_id];
2725 
2726 		IWL_DEBUG_MAC80211(mvm, "CQM info_changed\n");
2727 		if (link_info)
2728 			link_info->bf_data.last_cqm_event = 0;
2729 
2730 		if (mvmvif->bf_enabled) {
2731 			/* FIXME: need to update per link when FW API will
2732 			 * support it
2733 			 */
2734 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
2735 			if (ret)
2736 				IWL_ERR(mvm,
2737 					"failed to update CQM thresholds\n");
2738 		}
2739 	}
2740 
2741 	if (changes & BSS_CHANGED_BANDWIDTH)
2742 		iwl_mvm_update_link_smps(vif, link_conf);
2743 }
2744 
2745 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2746 					     struct ieee80211_vif *vif,
2747 					     struct ieee80211_bss_conf *bss_conf,
2748 					     u64 changes)
2749 {
2750 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2751 	int ret;
2752 	int i;
2753 
2754 	/*
2755 	 * Re-calculate the tsf id, as the leader-follower relations depend
2756 	 * on the beacon interval, which was not known when the station
2757 	 * interface was added.
2758 	 */
2759 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2760 		if ((vif->bss_conf.he_support &&
2761 		     !iwlwifi_mod_params.disable_11ax))
2762 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2763 
2764 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2765 	}
2766 
2767 	/* Update MU EDCA params */
2768 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2769 	    vif->cfg.assoc &&
2770 	    (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax))
2771 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2772 
2773 	/*
2774 	 * If we're not associated yet, take the (new) BSSID before associating
2775 	 * so the firmware knows. If we're already associated, then use the old
2776 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2777 	 * branch for disassociation below.
2778 	 */
2779 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2780 		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2781 
2782 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2783 	if (ret)
2784 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2785 
2786 	/* after sending it once, adopt mac80211 data */
2787 	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2788 	mvmvif->associated = vif->cfg.assoc;
2789 
2790 	if (changes & BSS_CHANGED_ASSOC) {
2791 		if (vif->cfg.assoc) {
2792 			/* clear statistics to get clean beacon counter */
2793 			iwl_mvm_request_statistics(mvm, true);
2794 			for_each_mvm_vif_valid_link(mvmvif, i)
2795 				memset(&mvmvif->link[i]->beacon_stats, 0,
2796 				       sizeof(mvmvif->link[i]->beacon_stats));
2797 
2798 			/* add quota for this interface */
2799 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2800 			if (ret) {
2801 				IWL_ERR(mvm, "failed to update quotas\n");
2802 				return;
2803 			}
2804 
2805 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2806 				     &mvm->status) &&
2807 			    !fw_has_capa(&mvm->fw->ucode_capa,
2808 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2809 				/*
2810 				 * If we're restarting then the firmware will
2811 				 * obviously have lost synchronisation with
2812 				 * the AP. It will attempt to synchronise by
2813 				 * itself, but we can make it more reliable by
2814 				 * scheduling a session protection time event.
2815 				 *
2816 				 * The firmware needs to receive a beacon to
2817 				 * catch up with synchronisation, use 110% of
2818 				 * the beacon interval.
2819 				 *
2820 				 * Set a large maximum delay to allow for more
2821 				 * than a single interface.
2822 				 *
2823 				 * For new firmware versions, rely on the
2824 				 * firmware. This is relevant for DCM scenarios
2825 				 * only anyway.
2826 				 */
2827 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2828 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2829 							5 * dur, false);
2830 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2831 					     &mvm->status) &&
2832 				   !vif->bss_conf.dtim_period) {
2833 				/*
2834 				 * If we're not restarting and still haven't
2835 				 * heard a beacon (dtim period unknown) then
2836 				 * make sure we still have enough minimum time
2837 				 * remaining in the time event, since the auth
2838 				 * might actually have taken quite a while
2839 				 * (especially for SAE) and so the remaining
2840 				 * time could be small without us having heard
2841 				 * a beacon yet.
2842 				 */
2843 				iwl_mvm_protect_assoc(mvm, vif, 0, 0);
2844 			}
2845 
2846 			iwl_mvm_sf_update(mvm, vif, false);
2847 			iwl_mvm_power_vif_assoc(mvm, vif);
2848 			if (vif->p2p) {
2849 				iwl_mvm_update_smps(mvm, vif,
2850 						    IWL_MVM_SMPS_REQ_PROT,
2851 						    IEEE80211_SMPS_DYNAMIC, 0);
2852 			}
2853 		} else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
2854 			iwl_mvm_mei_host_disassociated(mvm);
2855 			/*
2856 			 * If update fails - SF might be running in associated
2857 			 * mode while disassociated - which is forbidden.
2858 			 */
2859 			ret = iwl_mvm_sf_update(mvm, vif, false);
2860 			WARN_ONCE(ret &&
2861 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
2862 					    &mvm->status),
2863 				  "Failed to update SF upon disassociation\n");
2864 
2865 			/*
2866 			 * If we get an assert during the connection (after the
2867 			 * station has been added, but before the vif is set
2868 			 * to associated), mac80211 will re-add the station and
2869 			 * then configure the vif. Since the vif is not
2870 			 * associated, we would remove the station here and
2871 			 * this would fail the recovery.
2872 			 */
2873 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2874 				      &mvm->status)) {
2875 				/* first remove remaining keys */
2876 				iwl_mvm_sec_key_remove_ap(mvm, vif,
2877 							  &mvmvif->deflink, 0);
2878 
2879 				/*
2880 				 * Remove AP station now that
2881 				 * the MAC is unassoc
2882 				 */
2883 				ret = iwl_mvm_rm_sta_id(mvm, vif,
2884 							mvmvif->deflink.ap_sta_id);
2885 				if (ret)
2886 					IWL_ERR(mvm,
2887 						"failed to remove AP station\n");
2888 
2889 				mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
2890 			}
2891 
2892 			/* remove quota for this interface */
2893 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2894 			if (ret)
2895 				IWL_ERR(mvm, "failed to update quotas\n");
2896 
2897 			/* this will take the cleared BSSID from bss_conf */
2898 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2899 			if (ret)
2900 				IWL_ERR(mvm,
2901 					"failed to update MAC %pM (clear after unassoc)\n",
2902 					vif->addr);
2903 		}
2904 
2905 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
2906 	}
2907 
2908 	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
2909 						changes);
2910 }
2911 
2912 bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2913 				  struct ieee80211_vif *vif,
2914 				  int *ret)
2915 {
2916 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2917 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2918 	int i;
2919 
2920 	lockdep_assert_held(&mvm->mutex);
2921 
2922 	mvmvif->ap_assoc_sta_count = 0;
2923 
2924 	/* must be set before quota calculations */
2925 	mvmvif->ap_ibss_active = true;
2926 
2927 	/* send all the early keys to the device now */
2928 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2929 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2930 
2931 		if (!key)
2932 			continue;
2933 
2934 		mvmvif->ap_early_keys[i] = NULL;
2935 
2936 		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2937 		if (*ret)
2938 			return true;
2939 	}
2940 
2941 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2942 		iwl_mvm_vif_set_low_latency(mvmvif, true,
2943 					    LOW_LATENCY_VIF_TYPE);
2944 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2945 	}
2946 
2947 	/* power updated needs to be done before quotas */
2948 	iwl_mvm_power_update_mac(mvm);
2949 
2950 	return false;
2951 }
2952 
2953 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2954 				 struct ieee80211_vif *vif,
2955 				 struct ieee80211_bss_conf *link_conf)
2956 {
2957 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2958 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2959 	int ret;
2960 
2961 	mutex_lock(&mvm->mutex);
2962 
2963 	/*
2964 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2965 	 * the beacon interval, which was not known when the AP interface
2966 	 * was added.
2967 	 */
2968 	if (vif->type == NL80211_IFTYPE_AP)
2969 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2970 
2971 	/* For older devices need to send beacon template before adding mac
2972 	 * context. For the newer, the beacon is a resource that belongs to a
2973 	 * MAC, so need to send beacon template after adding the mac.
2974 	 */
2975 	if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
2976 		/* Add the mac context */
2977 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2978 		if (ret)
2979 			goto out_unlock;
2980 
2981 		/* Send the beacon template */
2982 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
2983 		if (ret)
2984 			goto out_unlock;
2985 	} else {
2986 		/* Send the beacon template */
2987 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
2988 		if (ret)
2989 			goto out_unlock;
2990 
2991 		/* Add the mac context */
2992 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2993 		if (ret)
2994 			goto out_unlock;
2995 	}
2996 
2997 	/* Perform the binding */
2998 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2999 	if (ret)
3000 		goto out_remove;
3001 
3002 	/*
3003 	 * This is not very nice, but the simplest:
3004 	 * For older FWs adding the mcast sta before the bcast station may
3005 	 * cause assert 0x2b00.
3006 	 * This is fixed in later FW so make the order of removal depend on
3007 	 * the TLV
3008 	 */
3009 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
3010 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
3011 		if (ret)
3012 			goto out_unbind;
3013 		/*
3014 		 * Send the bcast station. At this stage the TBTT and DTIM time
3015 		 * events are added and applied to the scheduler
3016 		 */
3017 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3018 		if (ret) {
3019 			iwl_mvm_rm_mcast_sta(mvm, vif);
3020 			goto out_unbind;
3021 		}
3022 	} else {
3023 		/*
3024 		 * Send the bcast station. At this stage the TBTT and DTIM time
3025 		 * events are added and applied to the scheduler
3026 		 */
3027 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3028 		if (ret)
3029 			goto out_unbind;
3030 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
3031 		if (ret) {
3032 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
3033 			goto out_unbind;
3034 		}
3035 	}
3036 
3037 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
3038 		goto out_failed;
3039 
3040 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
3041 	if (ret)
3042 		goto out_failed;
3043 
3044 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3045 	if (vif->p2p && mvm->p2p_device_vif)
3046 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3047 
3048 	iwl_mvm_bt_coex_vif_change(mvm);
3049 
3050 	/* we don't support TDLS during DCM */
3051 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3052 		iwl_mvm_teardown_tdls_peers(mvm);
3053 
3054 	iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
3055 
3056 	goto out_unlock;
3057 
3058 out_failed:
3059 	iwl_mvm_power_update_mac(mvm);
3060 	mvmvif->ap_ibss_active = false;
3061 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3062 	iwl_mvm_rm_mcast_sta(mvm, vif);
3063 out_unbind:
3064 	iwl_mvm_binding_remove_vif(mvm, vif);
3065 out_remove:
3066 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3067 out_unlock:
3068 	mutex_unlock(&mvm->mutex);
3069 	return ret;
3070 }
3071 
3072 static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
3073 			    struct ieee80211_vif *vif,
3074 			    struct ieee80211_bss_conf *link_conf)
3075 {
3076 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
3077 }
3078 
3079 static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
3080 			      struct ieee80211_vif *vif)
3081 {
3082 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
3083 }
3084 
3085 /* Common part for MLD and non-MLD ops */
3086 void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
3087 				 struct ieee80211_vif *vif)
3088 {
3089 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3090 
3091 	lockdep_assert_held(&mvm->mutex);
3092 
3093 	iwl_mvm_prepare_mac_removal(mvm, vif);
3094 
3095 	/* Handle AP stop while in CSA */
3096 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
3097 		iwl_mvm_remove_time_event(mvm, mvmvif,
3098 					  &mvmvif->time_event_data);
3099 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
3100 		mvmvif->csa_countdown = false;
3101 	}
3102 
3103 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
3104 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
3105 		mvm->csa_tx_block_bcn_timeout = 0;
3106 	}
3107 
3108 	mvmvif->ap_ibss_active = false;
3109 	mvm->ap_last_beacon_gp2 = 0;
3110 
3111 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
3112 		iwl_mvm_vif_set_low_latency(mvmvif, false,
3113 					    LOW_LATENCY_VIF_TYPE);
3114 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
3115 	}
3116 
3117 	iwl_mvm_bt_coex_vif_change(mvm);
3118 }
3119 
3120 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
3121 				 struct ieee80211_vif *vif,
3122 				 struct ieee80211_bss_conf *link_conf)
3123 {
3124 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3125 
3126 	mutex_lock(&mvm->mutex);
3127 
3128 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
3129 
3130 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3131 	if (vif->p2p && mvm->p2p_device_vif)
3132 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3133 
3134 	iwl_mvm_update_quotas(mvm, false, NULL);
3135 
3136 	iwl_mvm_ftm_responder_clear(mvm, vif);
3137 
3138 	/*
3139 	 * This is not very nice, but the simplest:
3140 	 * For older FWs removing the mcast sta before the bcast station may
3141 	 * cause assert 0x2b00.
3142 	 * This is fixed in later FW (which will stop beaconing when removing
3143 	 * bcast station).
3144 	 * So make the order of removal depend on the TLV
3145 	 */
3146 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3147 		iwl_mvm_rm_mcast_sta(mvm, vif);
3148 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3149 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3150 		iwl_mvm_rm_mcast_sta(mvm, vif);
3151 	iwl_mvm_binding_remove_vif(mvm, vif);
3152 
3153 	iwl_mvm_power_update_mac(mvm);
3154 
3155 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3156 
3157 	mutex_unlock(&mvm->mutex);
3158 }
3159 
3160 static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
3161 			    struct ieee80211_vif *vif,
3162 			    struct ieee80211_bss_conf *link_conf)
3163 {
3164 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
3165 }
3166 
3167 static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
3168 			      struct ieee80211_vif *vif)
3169 {
3170 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3171 }
3172 
3173 static void
3174 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3175 				 struct ieee80211_vif *vif,
3176 				 struct ieee80211_bss_conf *bss_conf,
3177 				 u64 changes)
3178 {
3179 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3180 
3181 	/* Changes will be applied when the AP/IBSS is started */
3182 	if (!mvmvif->ap_ibss_active)
3183 		return;
3184 
3185 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3186 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3187 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3188 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3189 
3190 	/* Need to send a new beacon template to the FW */
3191 	if (changes & BSS_CHANGED_BEACON &&
3192 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3193 		IWL_WARN(mvm, "Failed updating beacon data\n");
3194 
3195 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3196 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3197 
3198 		if (ret)
3199 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3200 				 ret);
3201 	}
3202 
3203 }
3204 
3205 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3206 				     struct ieee80211_vif *vif,
3207 				     struct ieee80211_bss_conf *bss_conf,
3208 				     u64 changes)
3209 {
3210 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3211 
3212 	mutex_lock(&mvm->mutex);
3213 
3214 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3215 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3216 
3217 	switch (vif->type) {
3218 	case NL80211_IFTYPE_STATION:
3219 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
3220 		break;
3221 	case NL80211_IFTYPE_AP:
3222 	case NL80211_IFTYPE_ADHOC:
3223 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
3224 		break;
3225 	case NL80211_IFTYPE_MONITOR:
3226 		if (changes & BSS_CHANGED_MU_GROUPS)
3227 			iwl_mvm_update_mu_groups(mvm, vif);
3228 		break;
3229 	default:
3230 		/* shouldn't happen */
3231 		WARN_ON_ONCE(1);
3232 	}
3233 
3234 	if (changes & BSS_CHANGED_TXPOWER) {
3235 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
3236 				bss_conf->txpower);
3237 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
3238 	}
3239 
3240 	mutex_unlock(&mvm->mutex);
3241 }
3242 
3243 int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3244 			struct ieee80211_scan_request *hw_req)
3245 {
3246 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3247 	int ret;
3248 
3249 	if (hw_req->req.n_channels == 0 ||
3250 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3251 		return -EINVAL;
3252 
3253 	mutex_lock(&mvm->mutex);
3254 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3255 	mutex_unlock(&mvm->mutex);
3256 
3257 	return ret;
3258 }
3259 
3260 void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3261 				struct ieee80211_vif *vif)
3262 {
3263 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3264 
3265 	mutex_lock(&mvm->mutex);
3266 
3267 	/* Due to a race condition, it's possible that mac80211 asks
3268 	 * us to stop a hw_scan when it's already stopped.  This can
3269 	 * happen, for instance, if we stopped the scan ourselves,
3270 	 * called ieee80211_scan_completed() and the userspace called
3271 	 * cancel scan scan before ieee80211_scan_work() could run.
3272 	 * To handle that, simply return if the scan is not running.
3273 	*/
3274 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3275 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3276 
3277 	mutex_unlock(&mvm->mutex);
3278 }
3279 
3280 void
3281 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3282 				  struct ieee80211_sta *sta, u16 tids,
3283 				  int num_frames,
3284 				  enum ieee80211_frame_release_type reason,
3285 				  bool more_data)
3286 {
3287 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3288 
3289 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3290 
3291 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3292 					  tids, more_data, false);
3293 }
3294 
3295 void
3296 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3297 				    struct ieee80211_sta *sta, u16 tids,
3298 				    int num_frames,
3299 				    enum ieee80211_frame_release_type reason,
3300 				    bool more_data)
3301 {
3302 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3303 
3304 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3305 
3306 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3307 					  tids, more_data, true);
3308 }
3309 
3310 static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3311 				     enum sta_notify_cmd cmd,
3312 				     struct ieee80211_sta *sta)
3313 {
3314 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3315 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3316 	unsigned long txqs = 0, tids = 0;
3317 	int tid;
3318 
3319 	/*
3320 	 * If we have TVQM then we get too high queue numbers - luckily
3321 	 * we really shouldn't get here with that because such hardware
3322 	 * should have firmware supporting buffer station offload.
3323 	 */
3324 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3325 		return;
3326 
3327 	spin_lock_bh(&mvmsta->lock);
3328 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3329 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3330 
3331 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
3332 			continue;
3333 
3334 		__set_bit(tid_data->txq_id, &txqs);
3335 
3336 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3337 			continue;
3338 
3339 		__set_bit(tid, &tids);
3340 	}
3341 
3342 	switch (cmd) {
3343 	case STA_NOTIFY_SLEEP:
3344 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3345 			ieee80211_sta_set_buffered(sta, tid, true);
3346 
3347 		if (txqs)
3348 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3349 		/*
3350 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3351 		 * queues to this station will not be transmitted. The fw will
3352 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3353 		 */
3354 		break;
3355 	case STA_NOTIFY_AWAKE:
3356 		if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
3357 			break;
3358 
3359 		if (txqs)
3360 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3361 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3362 		break;
3363 	default:
3364 		break;
3365 	}
3366 	spin_unlock_bh(&mvmsta->lock);
3367 }
3368 
3369 void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3370 			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3371 {
3372 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
3373 }
3374 
3375 void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
3376 {
3377 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3378 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
3379 	struct ieee80211_sta *sta;
3380 	struct iwl_mvm_sta *mvmsta;
3381 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
3382 
3383 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
3384 		return;
3385 
3386 	rcu_read_lock();
3387 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
3388 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
3389 		rcu_read_unlock();
3390 		return;
3391 	}
3392 
3393 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
3394 
3395 	if (!mvmsta->vif ||
3396 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
3397 		rcu_read_unlock();
3398 		return;
3399 	}
3400 
3401 	if (mvmsta->sleeping != sleeping) {
3402 		mvmsta->sleeping = sleeping;
3403 		__iwl_mvm_mac_sta_notify(mvm->hw,
3404 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
3405 			sta);
3406 		ieee80211_sta_ps_transition(sta, sleeping);
3407 	}
3408 
3409 	if (sleeping) {
3410 		switch (notif->type) {
3411 		case IWL_MVM_PM_EVENT_AWAKE:
3412 		case IWL_MVM_PM_EVENT_ASLEEP:
3413 			break;
3414 		case IWL_MVM_PM_EVENT_UAPSD:
3415 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
3416 			break;
3417 		case IWL_MVM_PM_EVENT_PS_POLL:
3418 			ieee80211_sta_pspoll(sta);
3419 			break;
3420 		default:
3421 			break;
3422 		}
3423 	}
3424 
3425 	rcu_read_unlock();
3426 }
3427 
3428 void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3429 				struct ieee80211_vif *vif,
3430 				struct ieee80211_sta *sta)
3431 {
3432 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3433 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3434 	unsigned int link_id;
3435 
3436 	/*
3437 	 * This is called before mac80211 does RCU synchronisation,
3438 	 * so here we already invalidate our internal RCU-protected
3439 	 * station pointer. The rest of the code will thus no longer
3440 	 * be able to find the station this way, and we don't rely
3441 	 * on further RCU synchronisation after the sta_state()
3442 	 * callback deleted the station.
3443 	 * Since there's mvm->mutex here, no need to have RCU lock for
3444 	 * mvm_sta->link access.
3445 	 */
3446 	mutex_lock(&mvm->mutex);
3447 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
3448 		struct iwl_mvm_link_sta *link_sta;
3449 		u32 sta_id;
3450 
3451 		if (!mvm_sta->link[link_id])
3452 			continue;
3453 
3454 		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
3455 						     lockdep_is_held(&mvm->mutex));
3456 		sta_id = link_sta->sta_id;
3457 		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
3458 			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
3459 					 ERR_PTR(-ENOENT));
3460 			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3461 		}
3462 	}
3463 	mutex_unlock(&mvm->mutex);
3464 }
3465 
3466 static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3467 				const u8 *bssid)
3468 {
3469 	int i;
3470 
3471 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3472 		struct iwl_mvm_tcm_mac *mdata;
3473 
3474 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3475 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3476 		mdata->opened_rx_ba_sessions = false;
3477 	}
3478 
3479 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3480 		return;
3481 
3482 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3483 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3484 		return;
3485 	}
3486 
3487 	if (!vif->p2p &&
3488 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3489 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3490 		return;
3491 	}
3492 
3493 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3494 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3495 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3496 			return;
3497 		}
3498 	}
3499 
3500 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3501 }
3502 
3503 static void
3504 iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
3505 			   struct ieee80211_vif *vif, u8 *peer_addr,
3506 			   enum nl80211_tdls_operation action)
3507 {
3508 	struct iwl_fw_dbg_trigger_tlv *trig;
3509 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
3510 
3511 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
3512 				     FW_DBG_TRIGGER_TDLS);
3513 	if (!trig)
3514 		return;
3515 
3516 	tdls_trig = (void *)trig->data;
3517 
3518 	if (!(tdls_trig->action_bitmap & BIT(action)))
3519 		return;
3520 
3521 	if (tdls_trig->peer_mode &&
3522 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
3523 		return;
3524 
3525 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
3526 				"TDLS event occurred, peer %pM, action %d",
3527 				peer_addr, action);
3528 }
3529 
3530 struct iwl_mvm_he_obss_narrow_bw_ru_data {
3531 	bool tolerated;
3532 };
3533 
3534 static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
3535 						    struct cfg80211_bss *bss,
3536 						    void *_data)
3537 {
3538 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
3539 	const struct cfg80211_bss_ies *ies;
3540 	const struct element *elem;
3541 
3542 	rcu_read_lock();
3543 	ies = rcu_dereference(bss->ies);
3544 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
3545 				  ies->len);
3546 
3547 	if (!elem || elem->datalen < 10 ||
3548 	    !(elem->data[10] &
3549 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
3550 		data->tolerated = false;
3551 	}
3552 	rcu_read_unlock();
3553 }
3554 
3555 static void
3556 iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
3557 				   struct ieee80211_vif *vif,
3558 				   unsigned int link_id,
3559 				   struct ieee80211_bss_conf *link_conf)
3560 {
3561 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3562 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
3563 		.tolerated = true,
3564 	};
3565 
3566 	if (WARN_ON_ONCE(!link_conf->chanreq.oper.chan ||
3567 			 !mvmvif->link[link_id]))
3568 		return;
3569 
3570 	if (!(link_conf->chanreq.oper.chan->flags & IEEE80211_CHAN_RADAR)) {
3571 		mvmvif->link[link_id]->he_ru_2mhz_block = false;
3572 		return;
3573 	}
3574 
3575 	cfg80211_bss_iter(hw->wiphy, &link_conf->chanreq.oper,
3576 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
3577 			  &iter_data);
3578 
3579 	/*
3580 	 * If there is at least one AP on radar channel that cannot
3581 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
3582 	 */
3583 	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
3584 }
3585 
3586 static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3587 					       struct ieee80211_vif *vif)
3588 {
3589 	struct ieee80211_supported_band *sband;
3590 	const struct ieee80211_sta_he_cap *he_cap;
3591 
3592 	if (vif->type != NL80211_IFTYPE_STATION)
3593 		return;
3594 
3595 	if (!mvm->cca_40mhz_workaround)
3596 		return;
3597 
3598 	/* decrement and check that we reached zero */
3599 	mvm->cca_40mhz_workaround--;
3600 	if (mvm->cca_40mhz_workaround)
3601 		return;
3602 
3603 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3604 
3605 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3606 
3607 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3608 
3609 	if (he_cap) {
3610 		/* we know that ours is writable */
3611 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3612 
3613 		he->he_cap_elem.phy_cap_info[0] |=
3614 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3615 	}
3616 }
3617 
3618 static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
3619 					struct ieee80211_vif *vif,
3620 					struct iwl_mvm_sta *mvm_sta)
3621 {
3622 #if IS_ENABLED(CONFIG_IWLMEI)
3623 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3624 	struct iwl_mei_conn_info conn_info = {
3625 		.ssid_len = vif->cfg.ssid_len,
3626 	};
3627 
3628 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3629 		return;
3630 
3631 	if (!mvm->mei_registered)
3632 		return;
3633 
3634 	/* FIXME: MEI needs to be updated for MLO */
3635 	if (!vif->bss_conf.chanreq.oper.chan)
3636 		return;
3637 
3638 	conn_info.channel = vif->bss_conf.chanreq.oper.chan->hw_value;
3639 
3640 	switch (mvm_sta->pairwise_cipher) {
3641 	case WLAN_CIPHER_SUITE_TKIP:
3642 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3643 		break;
3644 	case WLAN_CIPHER_SUITE_CCMP:
3645 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
3646 		break;
3647 	case WLAN_CIPHER_SUITE_GCMP:
3648 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
3649 		break;
3650 	case WLAN_CIPHER_SUITE_GCMP_256:
3651 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
3652 		break;
3653 	case 0:
3654 		/* open profile */
3655 		break;
3656 	default:
3657 		/* cipher not supported, don't send anything to iwlmei */
3658 		return;
3659 	}
3660 
3661 	switch (mvmvif->rekey_data.akm) {
3662 	case WLAN_AKM_SUITE_SAE & 0xff:
3663 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
3664 		break;
3665 	case WLAN_AKM_SUITE_PSK & 0xff:
3666 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
3667 		break;
3668 	case WLAN_AKM_SUITE_8021X & 0xff:
3669 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
3670 		break;
3671 	case 0:
3672 		/* open profile */
3673 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
3674 		break;
3675 	default:
3676 		/* auth method / AKM not supported */
3677 		/* TODO: All the FT vesions of these? */
3678 		return;
3679 	}
3680 
3681 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
3682 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
3683 
3684 	/* TODO: add support for collocated AP data */
3685 	iwl_mei_host_associated(&conn_info, NULL);
3686 #endif
3687 }
3688 
3689 static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
3690 					    struct ieee80211_vif *vif,
3691 					    bool force_assoc_off)
3692 {
3693 	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
3694 }
3695 
3696 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3697 				 struct ieee80211_vif *vif,
3698 				 struct ieee80211_sta *sta,
3699 				 enum ieee80211_sta_state old_state,
3700 				 enum ieee80211_sta_state new_state)
3701 {
3702 	static const struct iwl_mvm_sta_state_ops callbacks = {
3703 		.add_sta = iwl_mvm_add_sta,
3704 		.update_sta = iwl_mvm_update_sta,
3705 		.rm_sta = iwl_mvm_rm_sta,
3706 		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
3707 	};
3708 
3709 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
3710 					    &callbacks);
3711 }
3712 
3713 /* FIXME: temporary making two assumptions in all sta handling functions:
3714  *	(1) when setting sta state, the link exists and protected
3715  *	(2) if a link is valid in sta then it's valid in vif (can
3716  *	use same index in the link array)
3717  */
3718 static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3719 					   struct ieee80211_vif *vif,
3720 					   struct ieee80211_sta *sta)
3721 {
3722 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3723 	unsigned int link_id;
3724 
3725 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3726 		struct ieee80211_bss_conf *conf =
3727 			link_conf_dereference_check(vif, link_id);
3728 		struct ieee80211_link_sta *link_sta =
3729 			link_sta_dereference_check(sta, link_id);
3730 
3731 		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3732 			continue;
3733 
3734 		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
3735 				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3736 	}
3737 }
3738 
3739 #define IWL_MVM_MIN_BEACON_INTERVAL_TU 16
3740 
3741 static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
3742 				      struct ieee80211_vif *vif,
3743 				      struct ieee80211_sta *sta)
3744 {
3745 	struct ieee80211_link_sta *link_sta;
3746 	unsigned int link_id;
3747 
3748 	/* Beacon interval check - firmware will crash if the beacon
3749 	 * interval is less than 16. We can't avoid connecting at all,
3750 	 * so refuse the station state change, this will cause mac80211
3751 	 * to abandon attempts to connect to this AP, and eventually
3752 	 * wpa_s will blocklist the AP...
3753 	 */
3754 
3755 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
3756 		struct ieee80211_bss_conf *link_conf =
3757 			link_conf_dereference_protected(vif, link_id);
3758 
3759 		if (!link_conf)
3760 			continue;
3761 
3762 		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
3763 			IWL_ERR(mvm,
3764 				"Beacon interval %d for AP %pM is too small\n",
3765 				link_conf->beacon_int, link_sta->addr);
3766 			return false;
3767 		}
3768 
3769 		link_conf->he_support = link_sta->he_cap.has_he;
3770 	}
3771 
3772 	return true;
3773 }
3774 
3775 static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
3776 				       struct ieee80211_vif *vif,
3777 				       struct ieee80211_sta *sta,
3778 				       bool is_sta)
3779 {
3780 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3781 	struct ieee80211_link_sta *link_sta;
3782 	unsigned int link_id;
3783 
3784 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
3785 		struct ieee80211_bss_conf *link_conf =
3786 			link_conf_dereference_protected(vif, link_id);
3787 
3788 		if (!link_conf || !mvmvif->link[link_id])
3789 			continue;
3790 
3791 		link_conf->he_support = link_sta->he_cap.has_he;
3792 
3793 		if (is_sta) {
3794 			mvmvif->link[link_id]->he_ru_2mhz_block = false;
3795 			if (link_sta->he_cap.has_he)
3796 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3797 								   link_id,
3798 								   link_conf);
3799 		}
3800 	}
3801 }
3802 
3803 static int
3804 iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
3805 				   struct ieee80211_vif *vif,
3806 				   struct ieee80211_sta *sta,
3807 				   const struct iwl_mvm_sta_state_ops *callbacks)
3808 {
3809 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3810 	struct ieee80211_link_sta *link_sta;
3811 	unsigned int i;
3812 	int ret;
3813 
3814 	lockdep_assert_held(&mvm->mutex);
3815 
3816 	if (vif->type == NL80211_IFTYPE_STATION &&
3817 	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
3818 		return -EINVAL;
3819 
3820 	if (sta->tdls &&
3821 	    (vif->p2p ||
3822 	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
3823 	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3824 		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3825 		return -EBUSY;
3826 	}
3827 
3828 	ret = callbacks->add_sta(mvm, vif, sta);
3829 	if (sta->tdls && ret == 0) {
3830 		iwl_mvm_recalc_tdls_state(mvm, vif, true);
3831 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3832 					   NL80211_TDLS_SETUP);
3833 	}
3834 
3835 	if (ret)
3836 		return ret;
3837 
3838 	for_each_sta_active_link(vif, sta, link_sta, i)
3839 		link_sta->agg.max_rc_amsdu_len = 1;
3840 
3841 	ieee80211_sta_recalc_aggregates(sta);
3842 
3843 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3844 		mvmvif->ap_sta = sta;
3845 
3846 	/*
3847 	 * Initialize the rates here already - this really tells
3848 	 * the firmware only what the supported legacy rates are
3849 	 * (may be) since it's initialized already from what the
3850 	 * AP advertised in the beacon/probe response. This will
3851 	 * allow the firmware to send auth/assoc frames with one
3852 	 * of the supported rates already, rather than having to
3853 	 * use a mandatory rate.
3854 	 * If we're the AP, we'll just assume mandatory rates at
3855 	 * this point, but we know nothing about the STA anyway.
3856 	 */
3857 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3858 
3859 	return 0;
3860 }
3861 
3862 static int
3863 iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
3864 				struct iwl_mvm *mvm,
3865 				struct ieee80211_vif *vif,
3866 				struct ieee80211_sta *sta,
3867 				const struct iwl_mvm_sta_state_ops *callbacks)
3868 {
3869 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3870 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3871 	struct ieee80211_link_sta *link_sta;
3872 	unsigned int link_id;
3873 
3874 	lockdep_assert_held(&mvm->mutex);
3875 
3876 	if (vif->type == NL80211_IFTYPE_AP) {
3877 		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
3878 		mvmvif->ap_assoc_sta_count++;
3879 		callbacks->mac_ctxt_changed(mvm, vif, false);
3880 
3881 		/* since the below is not for MLD API, it's ok to use
3882 		 * the default bss_conf
3883 		 */
3884 		if (!mvm->mld_api_is_used &&
3885 		    (vif->bss_conf.he_support &&
3886 		     !iwlwifi_mod_params.disable_11ax))
3887 			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
3888 	} else if (vif->type == NL80211_IFTYPE_STATION) {
3889 		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
3890 
3891 		callbacks->mac_ctxt_changed(mvm, vif, false);
3892 
3893 		if (!mvm->mld_api_is_used)
3894 			goto out;
3895 
3896 		for_each_sta_active_link(vif, sta, link_sta, link_id) {
3897 			struct ieee80211_bss_conf *link_conf =
3898 				link_conf_dereference_protected(vif, link_id);
3899 
3900 			if (WARN_ON(!link_conf))
3901 				return -EINVAL;
3902 			if (!mvmvif->link[link_id])
3903 				continue;
3904 
3905 			iwl_mvm_link_changed(mvm, vif, link_conf,
3906 					     LINK_CONTEXT_MODIFY_ALL &
3907 					     ~LINK_CONTEXT_MODIFY_ACTIVE,
3908 					     true);
3909 		}
3910 	}
3911 
3912 out:
3913 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3914 
3915 	return callbacks->update_sta(mvm, vif, sta);
3916 }
3917 
3918 static int
3919 iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
3920 				      struct ieee80211_vif *vif,
3921 				      struct ieee80211_sta *sta,
3922 				      const struct iwl_mvm_sta_state_ops *callbacks)
3923 {
3924 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3925 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3926 
3927 	lockdep_assert_held(&mvm->mutex);
3928 
3929 	/* we don't support TDLS during DCM */
3930 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3931 		iwl_mvm_teardown_tdls_peers(mvm);
3932 
3933 	if (sta->tdls) {
3934 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3935 					   NL80211_TDLS_ENABLE_LINK);
3936 	} else {
3937 		/* enable beacon filtering */
3938 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
3939 
3940 		mvmvif->authorized = 1;
3941 
3942 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3943 			mvmvif->link_selection_res = vif->active_links;
3944 			mvmvif->link_selection_primary =
3945 				vif->active_links ? __ffs(vif->active_links) : 0;
3946 		}
3947 
3948 		callbacks->mac_ctxt_changed(mvm, vif, false);
3949 		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
3950 
3951 		memset(&mvmvif->last_esr_exit, 0,
3952 		       sizeof(mvmvif->last_esr_exit));
3953 
3954 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT, 0);
3955 
3956 		/* Block until FW notif will arrive */
3957 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_FW, 0);
3958 
3959 		/* when client is authorized (AP station marked as such),
3960 		 * try to enable the best link(s).
3961 		 */
3962 		if (vif->type == NL80211_IFTYPE_STATION &&
3963 		    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3964 			iwl_mvm_select_links(mvm, vif);
3965 	}
3966 
3967 	mvm_sta->authorized = true;
3968 
3969 	/* MFP is set by default before the station is authorized.
3970 	 * Clear it here in case it's not used.
3971 	 */
3972 	if (!sta->mfp) {
3973 		int ret = callbacks->update_sta(mvm, vif, sta);
3974 
3975 		if (ret)
3976 			return ret;
3977 	}
3978 
3979 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3980 
3981 	return 0;
3982 }
3983 
3984 static int
3985 iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
3986 				      struct ieee80211_vif *vif,
3987 				      struct ieee80211_sta *sta,
3988 				      const struct iwl_mvm_sta_state_ops *callbacks)
3989 {
3990 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3991 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3992 
3993 	lockdep_assert_held(&mvm->mutex);
3994 
3995 	mvmsta->authorized = false;
3996 
3997 	/* once we move into assoc state, need to update rate scale to
3998 	 * disable using wide bandwidth
3999 	 */
4000 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4001 
4002 	if (!sta->tdls) {
4003 		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
4004 		 * yet to avoid sending high prio again for a little
4005 		 * time.
4006 		 */
4007 		mvmvif->authorized = 0;
4008 		mvmvif->link_selection_res = 0;
4009 
4010 		/* disable beacon filtering */
4011 		iwl_mvm_disable_beacon_filter(mvm, vif);
4012 
4013 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4014 					  &mvmvif->prevent_esr_done_wk);
4015 
4016 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4017 					  &mvmvif->mlo_int_scan_wk);
4018 
4019 		wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
4020 
4021 		/* No need for the periodic statistics anymore */
4022 		if (ieee80211_vif_is_mld(vif) && mvmvif->esr_active)
4023 			iwl_mvm_request_periodic_system_statistics(mvm, false);
4024 	}
4025 
4026 	return 0;
4027 }
4028 
4029 /* Common part for MLD and non-MLD modes */
4030 int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
4031 				 struct ieee80211_vif *vif,
4032 				 struct ieee80211_sta *sta,
4033 				 enum ieee80211_sta_state old_state,
4034 				 enum ieee80211_sta_state new_state,
4035 				 const struct iwl_mvm_sta_state_ops *callbacks)
4036 {
4037 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4038 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4039 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4040 	struct ieee80211_link_sta *link_sta;
4041 	unsigned int link_id;
4042 	int ret;
4043 
4044 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
4045 			   sta->addr, old_state, new_state);
4046 
4047 	/*
4048 	 * If we are in a STA removal flow and in DQA mode:
4049 	 *
4050 	 * This is after the sync_rcu part, so the queues have already been
4051 	 * flushed. No more TXs on their way in mac80211's path, and no more in
4052 	 * the queues.
4053 	 * Also, we won't be getting any new TX frames for this station.
4054 	 * What we might have are deferred TX frames that need to be taken care
4055 	 * of.
4056 	 *
4057 	 * Drop any still-queued deferred-frame before removing the STA, and
4058 	 * make sure the worker is no longer handling frames for this STA.
4059 	 */
4060 	if (old_state == IEEE80211_STA_NONE &&
4061 	    new_state == IEEE80211_STA_NOTEXIST) {
4062 		flush_work(&mvm->add_stream_wk);
4063 
4064 		/*
4065 		 * No need to make sure deferred TX indication is off since the
4066 		 * worker will already remove it if it was on
4067 		 */
4068 
4069 		/*
4070 		 * Additionally, reset the 40 MHz capability if we disconnected
4071 		 * from the AP now.
4072 		 */
4073 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
4074 
4075 		/* Also free dup data just in case any assertions below fail */
4076 		kfree(mvm_sta->dup_data);
4077 	}
4078 
4079 	mutex_lock(&mvm->mutex);
4080 
4081 	/* this would be a mac80211 bug ... but don't crash, unless we had a
4082 	 * firmware crash while we were activating a link, in which case it is
4083 	 * legit to have phy_ctxt = NULL. Don't bother not to WARN if we are in
4084 	 * recovery flow since we spit tons of error messages anyway.
4085 	 */
4086 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
4087 		if (WARN_ON_ONCE(!mvmvif->link[link_id] ||
4088 				 !mvmvif->link[link_id]->phy_ctxt)) {
4089 			mutex_unlock(&mvm->mutex);
4090 			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
4091 					&mvm->status) ? 0 : -EINVAL;
4092 		}
4093 	}
4094 
4095 	/* track whether or not the station is associated */
4096 	mvm_sta->sta_state = new_state;
4097 
4098 	if (old_state == IEEE80211_STA_NOTEXIST &&
4099 	    new_state == IEEE80211_STA_NONE) {
4100 		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
4101 							 callbacks);
4102 		if (ret < 0)
4103 			goto out_unlock;
4104 	} else if (old_state == IEEE80211_STA_NONE &&
4105 		   new_state == IEEE80211_STA_AUTH) {
4106 		/*
4107 		 * EBS may be disabled due to previous failures reported by FW.
4108 		 * Reset EBS status here assuming environment has been changed.
4109 		 */
4110 		mvm->last_ebs_successful = true;
4111 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
4112 		ret = 0;
4113 	} else if (old_state == IEEE80211_STA_AUTH &&
4114 		   new_state == IEEE80211_STA_ASSOC) {
4115 		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
4116 						      callbacks);
4117 	} else if (old_state == IEEE80211_STA_ASSOC &&
4118 		   new_state == IEEE80211_STA_AUTHORIZED) {
4119 		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
4120 							    callbacks);
4121 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
4122 		   new_state == IEEE80211_STA_ASSOC) {
4123 		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
4124 							    callbacks);
4125 	} else if (old_state == IEEE80211_STA_ASSOC &&
4126 		   new_state == IEEE80211_STA_AUTH) {
4127 		if (vif->type == NL80211_IFTYPE_AP) {
4128 			mvmvif->ap_assoc_sta_count--;
4129 			callbacks->mac_ctxt_changed(mvm, vif, false);
4130 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
4131 			iwl_mvm_stop_session_protection(mvm, vif);
4132 		ret = 0;
4133 	} else if (old_state == IEEE80211_STA_AUTH &&
4134 		   new_state == IEEE80211_STA_NONE) {
4135 		ret = 0;
4136 	} else if (old_state == IEEE80211_STA_NONE &&
4137 		   new_state == IEEE80211_STA_NOTEXIST) {
4138 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
4139 			iwl_mvm_stop_session_protection(mvm, vif);
4140 			mvmvif->ap_sta = NULL;
4141 		}
4142 		ret = callbacks->rm_sta(mvm, vif, sta);
4143 		if (sta->tdls) {
4144 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
4145 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
4146 						   NL80211_TDLS_DISABLE_LINK);
4147 		}
4148 
4149 		if (unlikely(ret &&
4150 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
4151 				      &mvm->status)))
4152 			ret = 0;
4153 	} else {
4154 		ret = -EIO;
4155 	}
4156  out_unlock:
4157 	mutex_unlock(&mvm->mutex);
4158 
4159 	if (sta->tdls && ret == 0) {
4160 		if (old_state == IEEE80211_STA_NOTEXIST &&
4161 		    new_state == IEEE80211_STA_NONE)
4162 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4163 		else if (old_state == IEEE80211_STA_NONE &&
4164 			 new_state == IEEE80211_STA_NOTEXIST)
4165 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4166 	}
4167 
4168 	return ret;
4169 }
4170 
4171 int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4172 {
4173 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4174 
4175 	mvm->rts_threshold = value;
4176 
4177 	return 0;
4178 }
4179 
4180 void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4181 			   struct ieee80211_sta *sta, u32 changed)
4182 {
4183 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4184 
4185 	if (changed & (IEEE80211_RC_BW_CHANGED |
4186 		       IEEE80211_RC_SUPP_RATES_CHANGED |
4187 		       IEEE80211_RC_NSS_CHANGED))
4188 		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4189 
4190 	if (vif->type == NL80211_IFTYPE_STATION &&
4191 	    changed & IEEE80211_RC_NSS_CHANGED)
4192 		iwl_mvm_sf_update(mvm, vif, false);
4193 }
4194 
4195 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
4196 			       struct ieee80211_vif *vif,
4197 			       unsigned int link_id, u16 ac,
4198 			       const struct ieee80211_tx_queue_params *params)
4199 {
4200 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4201 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4202 
4203 	mvmvif->deflink.queue_params[ac] = *params;
4204 
4205 	/*
4206 	 * No need to update right away, we'll get BSS_CHANGED_QOS
4207 	 * The exception is P2P_DEVICE interface which needs immediate update.
4208 	 */
4209 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
4210 		int ret;
4211 
4212 		mutex_lock(&mvm->mutex);
4213 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4214 		mutex_unlock(&mvm->mutex);
4215 		return ret;
4216 	}
4217 	return 0;
4218 }
4219 
4220 void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4221 				struct ieee80211_vif *vif,
4222 				struct ieee80211_prep_tx_info *info)
4223 {
4224 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4225 
4226 	mutex_lock(&mvm->mutex);
4227 	iwl_mvm_protect_assoc(mvm, vif, info->duration, info->link_id);
4228 	mutex_unlock(&mvm->mutex);
4229 }
4230 
4231 void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
4232 				 struct ieee80211_vif *vif,
4233 				 struct ieee80211_prep_tx_info *info)
4234 {
4235 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4236 
4237 	/* for successful cases (auth/assoc), don't cancel session protection */
4238 	if (info->success)
4239 		return;
4240 
4241 	mutex_lock(&mvm->mutex);
4242 	iwl_mvm_stop_session_protection(mvm, vif);
4243 	mutex_unlock(&mvm->mutex);
4244 }
4245 
4246 int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4247 				 struct ieee80211_vif *vif,
4248 				 struct cfg80211_sched_scan_request *req,
4249 				 struct ieee80211_scan_ies *ies)
4250 {
4251 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4252 
4253 	int ret;
4254 
4255 	mutex_lock(&mvm->mutex);
4256 
4257 	if (!vif->cfg.idle) {
4258 		ret = -EBUSY;
4259 		goto out;
4260 	}
4261 
4262 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4263 
4264 out:
4265 	mutex_unlock(&mvm->mutex);
4266 	return ret;
4267 }
4268 
4269 int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4270 				struct ieee80211_vif *vif)
4271 {
4272 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4273 	int ret;
4274 
4275 	mutex_lock(&mvm->mutex);
4276 
4277 	/* Due to a race condition, it's possible that mac80211 asks
4278 	 * us to stop a sched_scan when it's already stopped.  This
4279 	 * can happen, for instance, if we stopped the scan ourselves,
4280 	 * called ieee80211_sched_scan_stopped() and the userspace called
4281 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4282 	 * could run.  To handle this, simply return if the scan is
4283 	 * not running.
4284 	*/
4285 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4286 		mutex_unlock(&mvm->mutex);
4287 		return 0;
4288 	}
4289 
4290 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4291 	mutex_unlock(&mvm->mutex);
4292 	iwl_mvm_wait_for_async_handlers(mvm);
4293 
4294 	return ret;
4295 }
4296 
4297 static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4298 				 enum set_key_cmd cmd,
4299 				 struct ieee80211_vif *vif,
4300 				 struct ieee80211_sta *sta,
4301 				 struct ieee80211_key_conf *key)
4302 {
4303 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4304 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4305 	struct iwl_mvm_sta *mvmsta = NULL;
4306 	struct iwl_mvm_key_pn *ptk_pn = NULL;
4307 	int keyidx = key->keyidx;
4308 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
4309 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4310 	int ret, i;
4311 	u8 key_offset;
4312 
4313 	if (sta)
4314 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4315 
4316 	switch (key->cipher) {
4317 	case WLAN_CIPHER_SUITE_TKIP:
4318 		if (!mvm->trans->trans_cfg->gen2) {
4319 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
4320 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4321 		} else if (vif->type == NL80211_IFTYPE_STATION) {
4322 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
4323 		} else {
4324 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
4325 			return -EOPNOTSUPP;
4326 		}
4327 		break;
4328 	case WLAN_CIPHER_SUITE_CCMP:
4329 	case WLAN_CIPHER_SUITE_GCMP:
4330 	case WLAN_CIPHER_SUITE_GCMP_256:
4331 		if (!iwl_mvm_has_new_tx_api(mvm))
4332 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4333 		break;
4334 	case WLAN_CIPHER_SUITE_AES_CMAC:
4335 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
4336 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4337 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4338 		break;
4339 	case WLAN_CIPHER_SUITE_WEP40:
4340 	case WLAN_CIPHER_SUITE_WEP104:
4341 		if (vif->type == NL80211_IFTYPE_STATION)
4342 			break;
4343 		if (iwl_mvm_has_new_tx_api(mvm))
4344 			return -EOPNOTSUPP;
4345 		/* support HW crypto on TX */
4346 		return 0;
4347 	default:
4348 		return -EOPNOTSUPP;
4349 	}
4350 
4351 	switch (cmd) {
4352 	case SET_KEY:
4353 		if (vif->type == NL80211_IFTYPE_STATION &&
4354 		    (keyidx == 6 || keyidx == 7))
4355 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4356 					   key);
4357 
4358 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4359 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4360 			/*
4361 			 * GTK on AP interface is a TX-only key, return 0;
4362 			 * on IBSS they're per-station and because we're lazy
4363 			 * we don't support them for RX, so do the same.
4364 			 * CMAC/GMAC in AP/IBSS modes must be done in software
4365 			 * on older NICs.
4366 			 *
4367 			 * Except, of course, beacon protection - it must be
4368 			 * offloaded since we just set a beacon template, and
4369 			 * then we must also offload the IGTK (not just BIGTK)
4370 			 * for firmware reasons.
4371 			 *
4372 			 * So just check for beacon protection - if we don't
4373 			 * have it we cannot get here with keyidx >= 6, and
4374 			 * if we do have it we need to send the key to FW in
4375 			 * all cases (CMAC/GMAC).
4376 			 */
4377 			if (!wiphy_ext_feature_isset(hw->wiphy,
4378 						     NL80211_EXT_FEATURE_BEACON_PROTECTION) &&
4379 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
4380 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4381 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
4382 				ret = -EOPNOTSUPP;
4383 				break;
4384 			}
4385 
4386 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
4387 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
4388 			    !iwl_mvm_has_new_tx_api(mvm)) {
4389 				key->hw_key_idx = STA_KEY_IDX_INVALID;
4390 				ret = 0;
4391 				break;
4392 			}
4393 
4394 			if (!mvmvif->ap_ibss_active) {
4395 				for (i = 0;
4396 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4397 				     i++) {
4398 					if (!mvmvif->ap_early_keys[i]) {
4399 						mvmvif->ap_early_keys[i] = key;
4400 						break;
4401 					}
4402 				}
4403 
4404 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4405 					ret = -ENOSPC;
4406 				else
4407 					ret = 0;
4408 
4409 				break;
4410 			}
4411 		}
4412 
4413 		/* During FW restart, in order to restore the state as it was,
4414 		 * don't try to reprogram keys we previously failed for.
4415 		 */
4416 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4417 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4418 			IWL_DEBUG_MAC80211(mvm,
4419 					   "skip invalid idx key programming during restart\n");
4420 			ret = 0;
4421 			break;
4422 		}
4423 
4424 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4425 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4426 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4427 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4428 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4429 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4430 			struct ieee80211_key_seq seq;
4431 			int tid, q;
4432 
4433 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4434 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4435 						     mvm->trans->num_rx_queues),
4436 					 GFP_KERNEL);
4437 			if (!ptk_pn) {
4438 				ret = -ENOMEM;
4439 				break;
4440 			}
4441 
4442 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4443 				ieee80211_get_key_rx_seq(key, tid, &seq);
4444 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
4445 					memcpy(ptk_pn->q[q].pn[tid],
4446 					       seq.ccmp.pn,
4447 					       IEEE80211_CCMP_PN_LEN);
4448 			}
4449 
4450 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4451 		}
4452 
4453 		/* in HW restart reuse the index, otherwise request a new one */
4454 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4455 			key_offset = key->hw_key_idx;
4456 		else
4457 			key_offset = STA_KEY_IDX_INVALID;
4458 
4459 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4460 			mvmsta->pairwise_cipher = key->cipher;
4461 
4462 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4463 				   sta ? sta->addr : NULL, key->keyidx);
4464 
4465 		if (sec_key_ver)
4466 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
4467 		else
4468 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
4469 
4470 		if (ret) {
4471 			IWL_WARN(mvm, "set key failed\n");
4472 			key->hw_key_idx = STA_KEY_IDX_INVALID;
4473 			if (ptk_pn) {
4474 				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4475 				kfree(ptk_pn);
4476 			}
4477 			/*
4478 			 * can't add key for RX, but we don't need it
4479 			 * in the device for TX so still return 0,
4480 			 * unless we have new TX API where we cannot
4481 			 * put key material into the TX_CMD
4482 			 */
4483 			if (iwl_mvm_has_new_tx_api(mvm))
4484 				ret = -EOPNOTSUPP;
4485 			else
4486 				ret = 0;
4487 		}
4488 
4489 		break;
4490 	case DISABLE_KEY:
4491 		if (vif->type == NL80211_IFTYPE_STATION &&
4492 		    (keyidx == 6 || keyidx == 7))
4493 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4494 					 NULL);
4495 
4496 		ret = -ENOENT;
4497 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4498 			if (mvmvif->ap_early_keys[i] == key) {
4499 				mvmvif->ap_early_keys[i] = NULL;
4500 				ret = 0;
4501 			}
4502 		}
4503 
4504 		/* found in pending list - don't do anything else */
4505 		if (ret == 0)
4506 			break;
4507 
4508 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4509 			ret = 0;
4510 			break;
4511 		}
4512 
4513 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4514 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4515 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4516 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4517 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4518 			ptk_pn = rcu_dereference_protected(
4519 						mvmsta->ptk_pn[keyidx],
4520 						lockdep_is_held(&mvm->mutex));
4521 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4522 			if (ptk_pn)
4523 				kfree_rcu(ptk_pn, rcu_head);
4524 		}
4525 
4526 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
4527 		if (sec_key_ver)
4528 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
4529 		else
4530 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4531 		break;
4532 	default:
4533 		ret = -EINVAL;
4534 	}
4535 
4536 	return ret;
4537 }
4538 
4539 int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4540 			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4541 			struct ieee80211_key_conf *key)
4542 {
4543 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4544 	int ret;
4545 
4546 	mutex_lock(&mvm->mutex);
4547 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4548 	mutex_unlock(&mvm->mutex);
4549 
4550 	return ret;
4551 }
4552 
4553 void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4554 				 struct ieee80211_vif *vif,
4555 				 struct ieee80211_key_conf *keyconf,
4556 				 struct ieee80211_sta *sta,
4557 				 u32 iv32, u16 *phase1key)
4558 {
4559 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4560 
4561 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4562 		return;
4563 
4564 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4565 }
4566 
4567 
4568 static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4569 			       struct iwl_rx_packet *pkt, void *data)
4570 {
4571 	struct iwl_mvm *mvm =
4572 		container_of(notif_wait, struct iwl_mvm, notif_wait);
4573 	struct iwl_hs20_roc_res *resp;
4574 	int resp_len = iwl_rx_packet_payload_len(pkt);
4575 	struct iwl_mvm_time_event_data *te_data = data;
4576 
4577 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4578 		return true;
4579 
4580 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4581 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4582 		return true;
4583 	}
4584 
4585 	resp = (void *)pkt->data;
4586 
4587 	IWL_DEBUG_TE(mvm,
4588 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4589 		     resp->status, resp->event_unique_id);
4590 
4591 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4592 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4593 		     te_data->uid);
4594 
4595 	spin_lock_bh(&mvm->time_event_lock);
4596 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4597 	spin_unlock_bh(&mvm->time_event_lock);
4598 
4599 	return true;
4600 }
4601 
4602 static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4603 				    struct ieee80211_channel *channel,
4604 				    struct ieee80211_vif *vif,
4605 				    int duration)
4606 {
4607 	int res;
4608 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4609 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4610 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4611 	struct iwl_notification_wait wait_time_event;
4612 	u32 req_dur, delay;
4613 	struct iwl_hs20_roc_req aux_roc_req = {
4614 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4615 		.id_and_color =
4616 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4617 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4618 	};
4619 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
4620 		&aux_roc_req.channel_info);
4621 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
4622 
4623 	/* Set the channel info data */
4624 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
4625 			      iwl_mvm_phy_band_from_nl80211(channel->band),
4626 			      IWL_PHY_CHANNEL_MODE20,
4627 			      0);
4628 
4629 	/* Set the time and duration */
4630 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4631 
4632 	iwl_mvm_roc_duration_and_delay(vif, duration, &req_dur, &delay);
4633 	tail->duration = cpu_to_le32(req_dur);
4634 	tail->apply_time_max_delay = cpu_to_le32(delay);
4635 
4636 	IWL_DEBUG_TE(mvm,
4637 		     "ROC: Requesting to remain on channel %u for %ums\n",
4638 		     channel->hw_value, req_dur);
4639 	IWL_DEBUG_TE(mvm,
4640 		     "\t(requested = %ums, max_delay = %ums)\n",
4641 		     duration, delay);
4642 
4643 	/* Set the node address */
4644 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4645 
4646 	lockdep_assert_held(&mvm->mutex);
4647 
4648 	spin_lock_bh(&mvm->time_event_lock);
4649 
4650 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4651 		spin_unlock_bh(&mvm->time_event_lock);
4652 		return -EIO;
4653 	}
4654 
4655 	te_data->vif = vif;
4656 	te_data->duration = duration;
4657 	te_data->id = HOT_SPOT_CMD;
4658 
4659 	spin_unlock_bh(&mvm->time_event_lock);
4660 
4661 	/*
4662 	 * Use a notification wait, which really just processes the
4663 	 * command response and doesn't wait for anything, in order
4664 	 * to be able to process the response and get the UID inside
4665 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4666 	 * stores the buffer and then wakes up this thread, by which
4667 	 * time another notification (that the time event started)
4668 	 * might already be processed unsuccessfully.
4669 	 */
4670 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4671 				   time_event_response,
4672 				   ARRAY_SIZE(time_event_response),
4673 				   iwl_mvm_rx_aux_roc, te_data);
4674 
4675 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4676 				   &aux_roc_req);
4677 
4678 	if (res) {
4679 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4680 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4681 		goto out_clear_te;
4682 	}
4683 
4684 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4685 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4686 	/* should never fail */
4687 	WARN_ON_ONCE(res);
4688 
4689 	if (res) {
4690  out_clear_te:
4691 		spin_lock_bh(&mvm->time_event_lock);
4692 		iwl_mvm_te_clear_data(mvm, te_data);
4693 		spin_unlock_bh(&mvm->time_event_lock);
4694 	}
4695 
4696 	return res;
4697 }
4698 
4699 static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4700 {
4701 	int ret = 0;
4702 
4703 	lockdep_assert_held(&mvm->mutex);
4704 
4705 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4706 			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4707 		IWL_ERR(mvm, "hotspot not supported\n");
4708 		return -EINVAL;
4709 	}
4710 
4711 	if (iwl_mvm_has_new_station_api(mvm->fw)) {
4712 		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4713 		WARN(ret, "Failed to allocate aux station");
4714 	}
4715 
4716 	return ret;
4717 }
4718 
4719 static int iwl_mvm_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4720 {
4721 	int ret;
4722 
4723 	lockdep_assert_held(&mvm->mutex);
4724 
4725 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4726 	if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4727 		return ret;
4728 
4729 	/* The station and queue allocation must be done only after the binding
4730 	 * is done, as otherwise the FW might incorrectly configure its state.
4731 	 */
4732 	return iwl_mvm_add_p2p_bcast_sta(mvm, vif);
4733 }
4734 
4735 static int iwl_mvm_roc(struct ieee80211_hw *hw,
4736 		       struct ieee80211_vif *vif,
4737 		       struct ieee80211_channel *channel,
4738 		       int duration,
4739 		       enum ieee80211_roc_type type)
4740 {
4741 	static const struct iwl_mvm_roc_ops ops = {
4742 		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
4743 		.link = iwl_mvm_roc_link,
4744 	};
4745 
4746 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4747 }
4748 
4749 static int iwl_mvm_roc_station(struct iwl_mvm *mvm,
4750 			       struct ieee80211_channel *channel,
4751 			       struct ieee80211_vif *vif,
4752 			       int duration)
4753 {
4754 	int ret;
4755 	u32 cmd_id = WIDE_ID(MAC_CONF_GROUP, ROC_CMD);
4756 	u8 fw_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
4757 					  IWL_FW_CMD_VER_UNKNOWN);
4758 
4759 	if (fw_ver == IWL_FW_CMD_VER_UNKNOWN) {
4760 		ret = iwl_mvm_send_aux_roc_cmd(mvm, channel, vif, duration);
4761 	} else if (fw_ver == 3) {
4762 		ret = iwl_mvm_roc_add_cmd(mvm, channel, vif, duration,
4763 					  ROC_ACTIVITY_HOTSPOT);
4764 	} else {
4765 		ret = -EOPNOTSUPP;
4766 		IWL_ERR(mvm, "ROC command version %d mismatch!\n", fw_ver);
4767 	}
4768 
4769 	return ret;
4770 }
4771 
4772 static int iwl_mvm_p2p_find_phy_ctxt(struct iwl_mvm *mvm,
4773 				     struct ieee80211_vif *vif,
4774 				     struct ieee80211_channel *channel)
4775 {
4776 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4777 	struct cfg80211_chan_def chandef;
4778 	int i;
4779 
4780 	lockdep_assert_held(&mvm->mutex);
4781 
4782 	if (mvmvif->deflink.phy_ctxt &&
4783 	    channel == mvmvif->deflink.phy_ctxt->channel)
4784 		return 0;
4785 
4786 	/* Try using a PHY context that is already in use */
4787 	for (i = 0; i < NUM_PHY_CTX; i++) {
4788 		struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[i];
4789 
4790 		if (!phy_ctxt->ref || mvmvif->deflink.phy_ctxt == phy_ctxt)
4791 			continue;
4792 
4793 		if (channel == phy_ctxt->channel) {
4794 			if (mvmvif->deflink.phy_ctxt)
4795 				iwl_mvm_phy_ctxt_unref(mvm,
4796 						       mvmvif->deflink.phy_ctxt);
4797 
4798 			mvmvif->deflink.phy_ctxt = phy_ctxt;
4799 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4800 			return 0;
4801 		}
4802 	}
4803 
4804 	/* We already have a phy_ctxt, but it's not on the right channel */
4805 	if (mvmvif->deflink.phy_ctxt)
4806 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
4807 
4808 	mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4809 	if (!mvmvif->deflink.phy_ctxt)
4810 		return -ENOSPC;
4811 
4812 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4813 
4814 	return iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt,
4815 				    &chandef, NULL, 1, 1);
4816 }
4817 
4818 /* Execute the common part for MLD and non-MLD modes */
4819 int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4820 		       struct ieee80211_channel *channel, int duration,
4821 		       enum ieee80211_roc_type type,
4822 		       const struct iwl_mvm_roc_ops *ops)
4823 {
4824 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4825 	u32 lmac_id;
4826 	int ret;
4827 
4828 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4829 			   duration, type);
4830 
4831 	/*
4832 	 * Flush the done work, just in case it's still pending, so that
4833 	 * the work it does can complete and we can accept new frames.
4834 	 */
4835 	flush_work(&mvm->roc_done_wk);
4836 
4837 	ret = iwl_mvm_esr_non_bss_link(mvm, vif, 0, true);
4838 	if (ret)
4839 		return ret;
4840 
4841 	mutex_lock(&mvm->mutex);
4842 
4843 	switch (vif->type) {
4844 	case NL80211_IFTYPE_STATION:
4845 		lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
4846 
4847 		/* Use aux roc framework (HS20) */
4848 		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4849 		if (!ret)
4850 			ret = iwl_mvm_roc_station(mvm, channel, vif, duration);
4851 		goto out_unlock;
4852 	case NL80211_IFTYPE_P2P_DEVICE:
4853 		/* handle below */
4854 		break;
4855 	default:
4856 		IWL_ERR(mvm, "ROC: Invalid vif type=%u\n", vif->type);
4857 		ret = -EINVAL;
4858 		goto out_unlock;
4859 	}
4860 
4861 
4862 	ret = iwl_mvm_p2p_find_phy_ctxt(mvm, vif, channel);
4863 	if (ret)
4864 		goto out_unlock;
4865 
4866 	ret = ops->link(mvm, vif);
4867 	if (ret)
4868 		goto out_unlock;
4869 
4870 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4871 out_unlock:
4872 	mutex_unlock(&mvm->mutex);
4873 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4874 	return ret;
4875 }
4876 
4877 int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
4878 		       struct ieee80211_vif *vif)
4879 {
4880 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4881 
4882 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4883 
4884 	iwl_mvm_stop_roc(mvm, vif);
4885 
4886 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4887 	return 0;
4888 }
4889 
4890 struct iwl_mvm_ftm_responder_iter_data {
4891 	bool responder;
4892 	struct ieee80211_chanctx_conf *ctx;
4893 };
4894 
4895 static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4896 					       struct ieee80211_vif *vif)
4897 {
4898 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4899 
4900 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4901 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4902 		data->responder = true;
4903 }
4904 
4905 bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4906 				      struct ieee80211_chanctx_conf *ctx)
4907 {
4908 	struct iwl_mvm_ftm_responder_iter_data data = {
4909 		.responder = false,
4910 		.ctx = ctx,
4911 	};
4912 
4913 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4914 					IEEE80211_IFACE_ITER_NORMAL,
4915 					iwl_mvm_ftm_responder_chanctx_iter,
4916 					&data);
4917 	return data.responder;
4918 }
4919 
4920 static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4921 				 struct ieee80211_chanctx_conf *ctx)
4922 {
4923 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4924 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4925 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
4926 	int ret;
4927 
4928 	lockdep_assert_held(&mvm->mutex);
4929 
4930 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4931 
4932 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4933 	if (!phy_ctxt) {
4934 		ret = -ENOSPC;
4935 		goto out;
4936 	}
4937 
4938 	ret = iwl_mvm_phy_ctxt_add(mvm, phy_ctxt, def, &ctx->ap,
4939 				   ctx->rx_chains_static,
4940 				   ctx->rx_chains_dynamic);
4941 	if (ret) {
4942 		IWL_ERR(mvm, "Failed to add PHY context\n");
4943 		goto out;
4944 	}
4945 
4946 	*phy_ctxt_id = phy_ctxt->id;
4947 out:
4948 	return ret;
4949 }
4950 
4951 int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4952 			struct ieee80211_chanctx_conf *ctx)
4953 {
4954 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4955 	int ret;
4956 
4957 	mutex_lock(&mvm->mutex);
4958 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4959 	mutex_unlock(&mvm->mutex);
4960 
4961 	return ret;
4962 }
4963 
4964 static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4965 				     struct ieee80211_chanctx_conf *ctx)
4966 {
4967 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4968 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4969 
4970 	lockdep_assert_held(&mvm->mutex);
4971 
4972 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4973 }
4974 
4975 void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4976 			    struct ieee80211_chanctx_conf *ctx)
4977 {
4978 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4979 
4980 	mutex_lock(&mvm->mutex);
4981 	__iwl_mvm_remove_chanctx(mvm, ctx);
4982 	mutex_unlock(&mvm->mutex);
4983 }
4984 
4985 void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4986 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
4987 {
4988 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4989 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4990 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4991 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
4992 
4993 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4994 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4995 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4996 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4997 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4998 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4999 		      phy_ctxt->ref, changed))
5000 		return;
5001 
5002 	mutex_lock(&mvm->mutex);
5003 
5004 	/* we are only changing the min_width, may be a noop */
5005 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
5006 		if (phy_ctxt->width == def->width)
5007 			goto out_unlock;
5008 
5009 		/* we are just toggling between 20_NOHT and 20 */
5010 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
5011 		    def->width <= NL80211_CHAN_WIDTH_20)
5012 			goto out_unlock;
5013 	}
5014 
5015 	iwl_mvm_bt_coex_vif_change(mvm);
5016 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def, &ctx->ap,
5017 				 ctx->rx_chains_static,
5018 				 ctx->rx_chains_dynamic);
5019 
5020 out_unlock:
5021 	mutex_unlock(&mvm->mutex);
5022 }
5023 
5024 /*
5025  * This function executes the common part for MLD and non-MLD modes.
5026  *
5027  * Returns true if we're done assigning the chanctx
5028  * (either on failure or success)
5029  */
5030 static bool
5031 __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
5032 				    struct ieee80211_vif *vif,
5033 				    struct ieee80211_chanctx_conf *ctx,
5034 				    bool switching_chanctx, int *ret)
5035 {
5036 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5037 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5038 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5039 
5040 	lockdep_assert_held(&mvm->mutex);
5041 
5042 	mvmvif->deflink.phy_ctxt = phy_ctxt;
5043 
5044 	switch (vif->type) {
5045 	case NL80211_IFTYPE_AP:
5046 		/* only needed if we're switching chanctx (i.e. during CSA) */
5047 		if (switching_chanctx) {
5048 			mvmvif->ap_ibss_active = true;
5049 			break;
5050 		}
5051 		fallthrough;
5052 	case NL80211_IFTYPE_ADHOC:
5053 		/*
5054 		 * The AP binding flow is handled as part of the start_ap flow
5055 		 * (in bss_info_changed), similarly for IBSS.
5056 		 */
5057 		*ret = 0;
5058 		return true;
5059 	case NL80211_IFTYPE_STATION:
5060 		break;
5061 	case NL80211_IFTYPE_MONITOR:
5062 		/* always disable PS when a monitor interface is active */
5063 		mvmvif->ps_disabled = true;
5064 		break;
5065 	default:
5066 		*ret = -EINVAL;
5067 		return true;
5068 	}
5069 	return false;
5070 }
5071 
5072 static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
5073 					struct ieee80211_vif *vif,
5074 					struct ieee80211_bss_conf *link_conf,
5075 					struct ieee80211_chanctx_conf *ctx,
5076 					bool switching_chanctx)
5077 {
5078 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5079 	int ret;
5080 
5081 	if (WARN_ON(!link_conf))
5082 		return -EINVAL;
5083 
5084 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
5085 						switching_chanctx, &ret))
5086 		goto out;
5087 
5088 	ret = iwl_mvm_binding_add_vif(mvm, vif);
5089 	if (ret)
5090 		goto out;
5091 
5092 	/*
5093 	 * Power state must be updated before quotas,
5094 	 * otherwise fw will complain.
5095 	 */
5096 	iwl_mvm_power_update_mac(mvm);
5097 
5098 	/* Setting the quota at this stage is only required for monitor
5099 	 * interfaces. For the other types, the bss_info changed flow
5100 	 * will handle quota settings.
5101 	 */
5102 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5103 		mvmvif->monitor_active = true;
5104 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
5105 		if (ret)
5106 			goto out_remove_binding;
5107 
5108 		ret = iwl_mvm_add_snif_sta(mvm, vif);
5109 		if (ret)
5110 			goto out_remove_binding;
5111 
5112 	}
5113 
5114 	/* Handle binding during CSA */
5115 	if (vif->type == NL80211_IFTYPE_AP) {
5116 		iwl_mvm_update_quotas(mvm, false, NULL);
5117 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
5118 	}
5119 
5120 	if (vif->type == NL80211_IFTYPE_STATION) {
5121 		if (!switching_chanctx) {
5122 			mvmvif->csa_bcn_pending = false;
5123 			goto out;
5124 		}
5125 
5126 		mvmvif->csa_bcn_pending = true;
5127 
5128 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5129 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5130 			u32 duration = 5 * vif->bss_conf.beacon_int;
5131 
5132 			/* Protect the session to make sure we hear the first
5133 			 * beacon on the new channel.
5134 			 */
5135 			iwl_mvm_protect_session(mvm, vif, duration, duration,
5136 						vif->bss_conf.beacon_int / 2,
5137 						true);
5138 		}
5139 
5140 		iwl_mvm_update_quotas(mvm, false, NULL);
5141 	}
5142 
5143 	goto out;
5144 
5145 out_remove_binding:
5146 	iwl_mvm_binding_remove_vif(mvm, vif);
5147 	iwl_mvm_power_update_mac(mvm);
5148 out:
5149 	if (ret)
5150 		mvmvif->deflink.phy_ctxt = NULL;
5151 	return ret;
5152 }
5153 
5154 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
5155 				      struct ieee80211_vif *vif,
5156 				      struct ieee80211_bss_conf *link_conf,
5157 				      struct ieee80211_chanctx_conf *ctx)
5158 {
5159 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5160 	int ret;
5161 
5162 	mutex_lock(&mvm->mutex);
5163 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5164 	mutex_unlock(&mvm->mutex);
5165 
5166 	return ret;
5167 }
5168 
5169 /*
5170  * This function executes the common part for MLD and non-MLD modes.
5171  *
5172  * Returns if chanctx unassign chanctx is done
5173  * (either on failure or success)
5174  */
5175 static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
5176 						  struct ieee80211_vif *vif,
5177 						  bool switching_chanctx)
5178 {
5179 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5180 
5181 	lockdep_assert_held(&mvm->mutex);
5182 	iwl_mvm_remove_time_event(mvm, mvmvif,
5183 				  &mvmvif->time_event_data);
5184 
5185 	switch (vif->type) {
5186 	case NL80211_IFTYPE_ADHOC:
5187 		return true;
5188 	case NL80211_IFTYPE_MONITOR:
5189 		mvmvif->monitor_active = false;
5190 		mvmvif->ps_disabled = false;
5191 		break;
5192 	case NL80211_IFTYPE_AP:
5193 		/* This part is triggered only during CSA */
5194 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
5195 			return true;
5196 
5197 		mvmvif->csa_countdown = false;
5198 
5199 		/* Set CS bit on all the stations */
5200 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5201 
5202 		/* Save blocked iface, the timeout is set on the next beacon */
5203 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5204 
5205 		mvmvif->ap_ibss_active = false;
5206 		break;
5207 	default:
5208 		break;
5209 	}
5210 	return false;
5211 }
5212 
5213 static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5214 					   struct ieee80211_vif *vif,
5215 					   struct ieee80211_bss_conf *link_conf,
5216 					   struct ieee80211_chanctx_conf *ctx,
5217 					   bool switching_chanctx)
5218 {
5219 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5220 	struct ieee80211_vif *disabled_vif = NULL;
5221 
5222 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5223 		goto out;
5224 
5225 	if (vif->type == NL80211_IFTYPE_MONITOR)
5226 		iwl_mvm_rm_snif_sta(mvm, vif);
5227 
5228 
5229 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5230 		disabled_vif = vif;
5231 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5232 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5233 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5234 	}
5235 
5236 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5237 	iwl_mvm_binding_remove_vif(mvm, vif);
5238 
5239 out:
5240 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5241 	    switching_chanctx)
5242 		return;
5243 	mvmvif->deflink.phy_ctxt = NULL;
5244 	iwl_mvm_power_update_mac(mvm);
5245 }
5246 
5247 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5248 					 struct ieee80211_vif *vif,
5249 					 struct ieee80211_bss_conf *link_conf,
5250 					 struct ieee80211_chanctx_conf *ctx)
5251 {
5252 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5253 
5254 	mutex_lock(&mvm->mutex);
5255 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5256 	mutex_unlock(&mvm->mutex);
5257 }
5258 
5259 static int
5260 iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5261 				struct ieee80211_vif_chanctx_switch *vifs,
5262 				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5263 {
5264 	int ret;
5265 
5266 	mutex_lock(&mvm->mutex);
5267 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5268 				    vifs[0].old_ctx, true);
5269 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5270 
5271 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5272 	if (ret) {
5273 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5274 		goto out_reassign;
5275 	}
5276 
5277 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5278 					vifs[0].new_ctx, true);
5279 	if (ret) {
5280 		IWL_ERR(mvm,
5281 			"failed to assign new_ctx during channel switch\n");
5282 		goto out_remove;
5283 	}
5284 
5285 	/* we don't support TDLS during DCM - can be caused by channel switch */
5286 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5287 		iwl_mvm_teardown_tdls_peers(mvm);
5288 
5289 	goto out;
5290 
5291 out_remove:
5292 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5293 
5294 out_reassign:
5295 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5296 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5297 		goto out_restart;
5298 	}
5299 
5300 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5301 				      vifs[0].old_ctx, true)) {
5302 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5303 		goto out_restart;
5304 	}
5305 
5306 	goto out;
5307 
5308 out_restart:
5309 	/* things keep failing, better restart the hw */
5310 	iwl_mvm_nic_restart(mvm, false);
5311 
5312 out:
5313 	mutex_unlock(&mvm->mutex);
5314 
5315 	return ret;
5316 }
5317 
5318 static int
5319 iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5320 				    struct ieee80211_vif_chanctx_switch *vifs,
5321 				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5322 {
5323 	int ret;
5324 
5325 	mutex_lock(&mvm->mutex);
5326 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5327 				    vifs[0].old_ctx, true);
5328 
5329 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5330 					vifs[0].new_ctx, true);
5331 	if (ret) {
5332 		IWL_ERR(mvm,
5333 			"failed to assign new_ctx during channel switch\n");
5334 		goto out_reassign;
5335 	}
5336 
5337 	goto out;
5338 
5339 out_reassign:
5340 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5341 				      vifs[0].old_ctx, true)) {
5342 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5343 		goto out_restart;
5344 	}
5345 
5346 	goto out;
5347 
5348 out_restart:
5349 	/* things keep failing, better restart the hw */
5350 	iwl_mvm_nic_restart(mvm, false);
5351 
5352 out:
5353 	mutex_unlock(&mvm->mutex);
5354 
5355 	return ret;
5356 }
5357 
5358 /* Execute the common part for both MLD and non-MLD modes */
5359 int
5360 iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5361 				  struct ieee80211_vif_chanctx_switch *vifs,
5362 				  int n_vifs,
5363 				  enum ieee80211_chanctx_switch_mode mode,
5364 				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5365 {
5366 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5367 	int ret;
5368 
5369 	/* we only support a single-vif right now */
5370 	if (n_vifs > 1)
5371 		return -EOPNOTSUPP;
5372 
5373 	switch (mode) {
5374 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5375 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5376 		break;
5377 	case CHANCTX_SWMODE_REASSIGN_VIF:
5378 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5379 		break;
5380 	default:
5381 		ret = -EOPNOTSUPP;
5382 		break;
5383 	}
5384 
5385 	return ret;
5386 }
5387 
5388 static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5389 				      struct ieee80211_vif_chanctx_switch *vifs,
5390 				      int n_vifs,
5391 				      enum ieee80211_chanctx_switch_mode mode)
5392 {
5393 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5394 		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5395 		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5396 	};
5397 
5398 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5399 }
5400 
5401 int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
5402 {
5403 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5404 
5405 	return mvm->ibss_manager;
5406 }
5407 
5408 static int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5409 			   bool set)
5410 {
5411 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5412 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5413 
5414 	if (!mvm_sta || !mvm_sta->vif) {
5415 		IWL_ERR(mvm, "Station is not associated to a vif\n");
5416 		return -EINVAL;
5417 	}
5418 
5419 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
5420 					       &mvm_sta->vif->bss_conf);
5421 }
5422 
5423 #ifdef CONFIG_NL80211_TESTMODE
5424 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5425 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5426 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5427 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5428 };
5429 
5430 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5431 				      struct ieee80211_vif *vif,
5432 				      void *data, int len)
5433 {
5434 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5435 	int err;
5436 	u32 noa_duration;
5437 
5438 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
5439 				   iwl_mvm_tm_policy, NULL);
5440 	if (err)
5441 		return err;
5442 
5443 	if (!tb[IWL_MVM_TM_ATTR_CMD])
5444 		return -EINVAL;
5445 
5446 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5447 	case IWL_MVM_TM_CMD_SET_NOA:
5448 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5449 		    !vif->bss_conf.enable_beacon ||
5450 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5451 			return -EINVAL;
5452 
5453 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5454 		if (noa_duration >= vif->bss_conf.beacon_int)
5455 			return -EINVAL;
5456 
5457 		mvm->noa_duration = noa_duration;
5458 		mvm->noa_vif = vif;
5459 
5460 		return iwl_mvm_update_quotas(mvm, true, NULL);
5461 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5462 		/* must be associated client vif - ignore authorized */
5463 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5464 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5465 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5466 			return -EINVAL;
5467 
5468 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
5469 			return iwl_mvm_enable_beacon_filter(mvm, vif);
5470 		return iwl_mvm_disable_beacon_filter(mvm, vif);
5471 	}
5472 
5473 	return -EOPNOTSUPP;
5474 }
5475 
5476 int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5477 			     struct ieee80211_vif *vif,
5478 			     void *data, int len)
5479 {
5480 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5481 	int err;
5482 
5483 	mutex_lock(&mvm->mutex);
5484 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5485 	mutex_unlock(&mvm->mutex);
5486 
5487 	return err;
5488 }
5489 #endif
5490 
5491 void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5492 			    struct ieee80211_channel_switch *chsw)
5493 {
5494 	/* By implementing this operation, we prevent mac80211 from
5495 	 * starting its own channel switch timer, so that we can call
5496 	 * ieee80211_chswitch_done() ourselves at the right time
5497 	 * (which is when the absence time event starts).
5498 	 */
5499 
5500 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5501 			   "dummy channel switch op\n");
5502 }
5503 
5504 static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
5505 				       struct ieee80211_vif *vif,
5506 				       struct ieee80211_channel_switch *chsw)
5507 {
5508 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5509 	struct iwl_chan_switch_te_cmd cmd = {
5510 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5511 							  mvmvif->color)),
5512 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
5513 		.tsf = cpu_to_le32(chsw->timestamp),
5514 		.cs_count = chsw->count,
5515 		.cs_mode = chsw->block_tx,
5516 	};
5517 
5518 	lockdep_assert_held(&mvm->mutex);
5519 
5520 	if (chsw->delay)
5521 		cmd.cs_delayed_bcn_count =
5522 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
5523 
5524 	return iwl_mvm_send_cmd_pdu(mvm,
5525 				    WIDE_ID(MAC_CONF_GROUP,
5526 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
5527 				    0, sizeof(cmd), &cmd);
5528 }
5529 
5530 static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
5531 				       struct ieee80211_vif *vif,
5532 				       struct ieee80211_channel_switch *chsw)
5533 {
5534 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5535 	u32 apply_time;
5536 
5537 	/* Schedule the time event to a bit before beacon 1,
5538 	 * to make sure we're in the new channel when the
5539 	 * GO/AP arrives. In case count <= 1 immediately schedule the
5540 	 * TE (this might result with some packet loss or connection
5541 	 * loss).
5542 	 */
5543 	if (chsw->count <= 1)
5544 		apply_time = 0;
5545 	else
5546 		apply_time = chsw->device_timestamp +
5547 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
5548 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
5549 
5550 	if (chsw->block_tx)
5551 		iwl_mvm_csa_client_absent(mvm, vif);
5552 
5553 	if (mvmvif->bf_enabled) {
5554 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif);
5555 
5556 		if (ret)
5557 			return ret;
5558 	}
5559 
5560 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
5561 				    apply_time);
5562 
5563 	return 0;
5564 }
5565 
5566 static void iwl_mvm_csa_block_txqs(void *data, struct ieee80211_sta *sta)
5567 {
5568 	int i;
5569 
5570 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
5571 		struct iwl_mvm_txq *mvmtxq =
5572 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
5573 
5574 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5575 	}
5576 }
5577 
5578 #define IWL_MAX_CSA_BLOCK_TX 1500
5579 int iwl_mvm_pre_channel_switch(struct iwl_mvm *mvm,
5580 			       struct ieee80211_vif *vif,
5581 			       struct ieee80211_channel_switch *chsw)
5582 {
5583 	struct ieee80211_vif *csa_vif;
5584 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5585 	struct iwl_mvm_txq *mvmtxq;
5586 	int ret;
5587 
5588 	lockdep_assert_held(&mvm->mutex);
5589 
5590 	mvmvif->csa_failed = false;
5591 	mvmvif->csa_blocks_tx = false;
5592 
5593 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5594 			   chsw->chandef.center_freq1);
5595 
5596 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
5597 				       ieee80211_vif_to_wdev(vif),
5598 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5599 
5600 	switch (vif->type) {
5601 	case NL80211_IFTYPE_AP:
5602 		csa_vif =
5603 			rcu_dereference_protected(mvm->csa_vif,
5604 						  lockdep_is_held(&mvm->mutex));
5605 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5606 			      "Another CSA is already in progress"))
5607 			return -EBUSY;
5608 
5609 		/* we still didn't unblock tx. prevent new CS meanwhile */
5610 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5611 					      lockdep_is_held(&mvm->mutex)))
5612 			return -EBUSY;
5613 
5614 		rcu_assign_pointer(mvm->csa_vif, vif);
5615 
5616 		if (WARN_ONCE(mvmvif->csa_countdown,
5617 			      "Previous CSA countdown didn't complete"))
5618 			return -EBUSY;
5619 
5620 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5621 
5622 		if (!chsw->block_tx)
5623 			break;
5624 		/* don't need blocking in driver otherwise - mac80211 will do */
5625 		if (!ieee80211_hw_check(mvm->hw, HANDLES_QUIET_CSA))
5626 			break;
5627 
5628 		mvmvif->csa_blocks_tx = true;
5629 		mvmtxq = iwl_mvm_txq_from_mac80211(vif->txq);
5630 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5631 		ieee80211_iterate_stations_atomic(mvm->hw,
5632 						  iwl_mvm_csa_block_txqs,
5633 						  NULL);
5634 		break;
5635 	case NL80211_IFTYPE_STATION:
5636 		mvmvif->csa_blocks_tx = chsw->block_tx;
5637 
5638 		/*
5639 		 * In the new flow FW is in charge of timing the switch so there
5640 		 * is no need for all of this
5641 		 */
5642 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5643 					    CHANNEL_SWITCH_ERROR_NOTIF,
5644 					    0))
5645 			break;
5646 
5647 		/*
5648 		 * We haven't configured the firmware to be associated yet since
5649 		 * we don't know the dtim period. In this case, the firmware can't
5650 		 * track the beacons.
5651 		 */
5652 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period)
5653 			return -EBUSY;
5654 
5655 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX &&
5656 		    hweight16(vif->valid_links) <= 1)
5657 			schedule_delayed_work(&mvmvif->csa_work, 0);
5658 
5659 		if (chsw->block_tx) {
5660 			/*
5661 			 * In case of undetermined / long time with immediate
5662 			 * quiet monitor status to gracefully disconnect
5663 			 */
5664 			if (!chsw->count ||
5665 			    chsw->count * vif->bss_conf.beacon_int >
5666 			    IWL_MAX_CSA_BLOCK_TX)
5667 				schedule_delayed_work(&mvmvif->csa_work,
5668 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5669 		}
5670 
5671 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5672 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5673 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5674 			if (ret)
5675 				return ret;
5676 		} else {
5677 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
5678 		}
5679 
5680 		mvmvif->csa_count = chsw->count;
5681 		mvmvif->csa_misbehave = false;
5682 		break;
5683 	default:
5684 		break;
5685 	}
5686 
5687 	mvmvif->ps_disabled = true;
5688 
5689 	ret = iwl_mvm_power_update_ps(mvm);
5690 	if (ret)
5691 		return ret;
5692 
5693 	/* we won't be on this channel any longer */
5694 	iwl_mvm_teardown_tdls_peers(mvm);
5695 
5696 	return ret;
5697 }
5698 
5699 static int iwl_mvm_mac_pre_channel_switch(struct ieee80211_hw *hw,
5700 					  struct ieee80211_vif *vif,
5701 					  struct ieee80211_channel_switch *chsw)
5702 {
5703 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5704 	int ret;
5705 
5706 	mutex_lock(&mvm->mutex);
5707 	ret = iwl_mvm_pre_channel_switch(mvm, vif, chsw);
5708 	mutex_unlock(&mvm->mutex);
5709 
5710 	return ret;
5711 }
5712 
5713 void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5714 				      struct ieee80211_vif *vif,
5715 				      struct ieee80211_channel_switch *chsw)
5716 {
5717 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5718 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5719 	struct iwl_chan_switch_te_cmd cmd = {
5720 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5721 							  mvmvif->color)),
5722 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5723 		.tsf = cpu_to_le32(chsw->timestamp),
5724 		.cs_count = chsw->count,
5725 		.cs_mode = chsw->block_tx,
5726 	};
5727 
5728 	/*
5729 	 * In the new flow FW is in charge of timing the switch so there is no
5730 	 * need for all of this
5731 	 */
5732 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5733 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5734 		return;
5735 
5736 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5737 		return;
5738 
5739 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5740 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5741 
5742 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
5743 		if (mvmvif->csa_misbehave) {
5744 			struct ieee80211_bss_conf *link_conf;
5745 
5746 			/* Second time, give up on this AP*/
5747 
5748 			link_conf = wiphy_dereference(hw->wiphy,
5749 						      vif->link_conf[chsw->link_id]);
5750 			if (WARN_ON(!link_conf))
5751 				return;
5752 
5753 			iwl_mvm_abort_channel_switch(hw, vif, link_conf);
5754 			ieee80211_chswitch_done(vif, false, 0);
5755 			mvmvif->csa_misbehave = false;
5756 			return;
5757 		}
5758 		mvmvif->csa_misbehave = true;
5759 	}
5760 	mvmvif->csa_count = chsw->count;
5761 
5762 	mutex_lock(&mvm->mutex);
5763 	if (mvmvif->csa_failed)
5764 		goto out_unlock;
5765 
5766 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5767 				     WIDE_ID(MAC_CONF_GROUP,
5768 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5769 				     0, sizeof(cmd), &cmd));
5770 out_unlock:
5771 	mutex_unlock(&mvm->mutex);
5772 }
5773 
5774 static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
5775 {
5776 	int i;
5777 
5778 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5779 		if (drop) {
5780 			mutex_lock(&mvm->mutex);
5781 			iwl_mvm_flush_tx_path(mvm,
5782 				iwl_mvm_flushable_queues(mvm) & queues);
5783 			mutex_unlock(&mvm->mutex);
5784 		} else {
5785 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5786 		}
5787 		return;
5788 	}
5789 
5790 	mutex_lock(&mvm->mutex);
5791 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5792 		struct ieee80211_sta *sta;
5793 
5794 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5795 						lockdep_is_held(&mvm->mutex));
5796 		if (IS_ERR_OR_NULL(sta))
5797 			continue;
5798 
5799 		if (drop)
5800 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
5801 		else
5802 			iwl_mvm_wait_sta_queues_empty(mvm,
5803 					iwl_mvm_sta_from_mac80211(sta));
5804 	}
5805 	mutex_unlock(&mvm->mutex);
5806 }
5807 
5808 void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5809 		       u32 queues, bool drop)
5810 {
5811 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5812 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5813 	struct iwl_mvm_sta *mvmsta;
5814 	struct ieee80211_sta *sta;
5815 	bool ap_sta_done = false;
5816 	int i;
5817 	u32 msk = 0;
5818 
5819 	if (!vif) {
5820 		iwl_mvm_flush_no_vif(mvm, queues, drop);
5821 		return;
5822 	}
5823 
5824 	if (!drop && hweight16(vif->active_links) <= 1) {
5825 		int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
5826 		struct ieee80211_bss_conf *link_conf;
5827 
5828 		link_conf = wiphy_dereference(hw->wiphy,
5829 					      vif->link_conf[link_id]);
5830 		if (WARN_ON(!link_conf))
5831 			return;
5832 		if (link_conf->csa_active && mvmvif->csa_blocks_tx)
5833 			drop = true;
5834 	}
5835 
5836 	/* Make sure we're done with the deferred traffic before flushing */
5837 	flush_work(&mvm->add_stream_wk);
5838 
5839 	mutex_lock(&mvm->mutex);
5840 
5841 	/* flush the AP-station and all TDLS peers */
5842 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5843 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5844 						lockdep_is_held(&mvm->mutex));
5845 		if (IS_ERR_OR_NULL(sta))
5846 			continue;
5847 
5848 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5849 		if (mvmsta->vif != vif)
5850 			continue;
5851 
5852 		if (sta == mvmvif->ap_sta) {
5853 			if (ap_sta_done)
5854 				continue;
5855 			ap_sta_done = true;
5856 		}
5857 
5858 		if (drop) {
5859 			if (iwl_mvm_flush_sta(mvm, mvmsta->deflink.sta_id,
5860 					      mvmsta->tfd_queue_msk))
5861 				IWL_ERR(mvm, "flush request fail\n");
5862 		} else {
5863 			if (iwl_mvm_has_new_tx_api(mvm))
5864 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5865 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5866 				msk |= mvmsta->tfd_queue_msk;
5867 		}
5868 	}
5869 
5870 	mutex_unlock(&mvm->mutex);
5871 
5872 	/* this can take a while, and we may need/want other operations
5873 	 * to succeed while doing this, so do it without the mutex held
5874 	 */
5875 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5876 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5877 }
5878 
5879 void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5880 			   struct ieee80211_sta *sta)
5881 {
5882 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5883 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5884 	struct iwl_mvm_link_sta *mvm_link_sta;
5885 	struct ieee80211_link_sta *link_sta;
5886 	int link_id;
5887 
5888 	mutex_lock(&mvm->mutex);
5889 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
5890 		mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_id],
5891 							 lockdep_is_held(&mvm->mutex));
5892 		if (!mvm_link_sta)
5893 			continue;
5894 
5895 		if (iwl_mvm_flush_sta(mvm, mvm_link_sta->sta_id,
5896 				      mvmsta->tfd_queue_msk))
5897 			IWL_ERR(mvm, "flush request fail\n");
5898 	}
5899 	mutex_unlock(&mvm->mutex);
5900 }
5901 
5902 static int iwl_mvm_mac_get_acs_survey(struct iwl_mvm *mvm, int idx,
5903 				      struct survey_info *survey)
5904 {
5905 	int chan_idx;
5906 	enum nl80211_band band;
5907 	int ret;
5908 
5909 	mutex_lock(&mvm->mutex);
5910 
5911 	if (!mvm->acs_survey) {
5912 		ret = -ENOENT;
5913 		goto out;
5914 	}
5915 
5916 	/* Find and return the next entry that has a non-zero active time */
5917 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5918 		struct ieee80211_supported_band *sband =
5919 			mvm->hw->wiphy->bands[band];
5920 
5921 		if (!sband)
5922 			continue;
5923 
5924 		for (chan_idx = 0; chan_idx < sband->n_channels; chan_idx++) {
5925 			struct iwl_mvm_acs_survey_channel *info =
5926 				&mvm->acs_survey->bands[band][chan_idx];
5927 
5928 			if (!info->time)
5929 				continue;
5930 
5931 			/* Found (the next) channel to report */
5932 			survey->channel = &sband->channels[chan_idx];
5933 			survey->filled = SURVEY_INFO_TIME |
5934 					 SURVEY_INFO_TIME_BUSY |
5935 					 SURVEY_INFO_TIME_RX |
5936 					 SURVEY_INFO_TIME_TX;
5937 			survey->time = info->time;
5938 			survey->time_busy = info->time_busy;
5939 			survey->time_rx = info->time_rx;
5940 			survey->time_tx = info->time_tx;
5941 			survey->noise = info->noise;
5942 			if (survey->noise < 0)
5943 				survey->filled |= SURVEY_INFO_NOISE_DBM;
5944 
5945 			/* Clear time so that channel is only reported once */
5946 			info->time = 0;
5947 
5948 			ret = 0;
5949 			goto out;
5950 		}
5951 	}
5952 
5953 	ret = -ENOENT;
5954 
5955 out:
5956 	mutex_unlock(&mvm->mutex);
5957 
5958 	return ret;
5959 }
5960 
5961 int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5962 			   struct survey_info *survey)
5963 {
5964 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5965 	int ret = 0;
5966 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
5967 					   WIDE_ID(SYSTEM_GROUP,
5968 						   SYSTEM_STATISTICS_CMD),
5969 					   IWL_FW_CMD_VER_UNKNOWN);
5970 
5971 	memset(survey, 0, sizeof(*survey));
5972 
5973 	if (!fw_has_capa(&mvm->fw->ucode_capa,
5974 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
5975 		return -ENOENT;
5976 
5977 	/*
5978 	 * Return the beacon stats at index zero and pass on following indices
5979 	 * to the function returning the full survey, most likely for ACS
5980 	 * (Automatic Channel Selection).
5981 	 */
5982 	if (idx > 0)
5983 		return iwl_mvm_mac_get_acs_survey(mvm, idx - 1, survey);
5984 
5985 	mutex_lock(&mvm->mutex);
5986 
5987 	if (iwl_mvm_firmware_running(mvm)) {
5988 		ret = iwl_mvm_request_statistics(mvm, false);
5989 		if (ret)
5990 			goto out;
5991 	}
5992 
5993 	survey->filled = SURVEY_INFO_TIME_RX |
5994 			 SURVEY_INFO_TIME_TX;
5995 
5996 	survey->time_rx = mvm->accu_radio_stats.rx_time +
5997 			  mvm->radio_stats.rx_time;
5998 	do_div(survey->time_rx, USEC_PER_MSEC);
5999 
6000 	survey->time_tx = mvm->accu_radio_stats.tx_time +
6001 			  mvm->radio_stats.tx_time;
6002 	do_div(survey->time_tx, USEC_PER_MSEC);
6003 
6004 	/* the new fw api doesn't support the following fields */
6005 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
6006 		goto out;
6007 
6008 	survey->filled |= SURVEY_INFO_TIME |
6009 			  SURVEY_INFO_TIME_SCAN;
6010 	survey->time = mvm->accu_radio_stats.on_time_rf +
6011 		       mvm->radio_stats.on_time_rf;
6012 	do_div(survey->time, USEC_PER_MSEC);
6013 
6014 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
6015 			    mvm->radio_stats.on_time_scan;
6016 	do_div(survey->time_scan, USEC_PER_MSEC);
6017 
6018  out:
6019 	mutex_unlock(&mvm->mutex);
6020 	return ret;
6021 }
6022 
6023 static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
6024 {
6025 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
6026 	u32 gi_ltf;
6027 
6028 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
6029 	case RATE_MCS_CHAN_WIDTH_20:
6030 		rinfo->bw = RATE_INFO_BW_20;
6031 		break;
6032 	case RATE_MCS_CHAN_WIDTH_40:
6033 		rinfo->bw = RATE_INFO_BW_40;
6034 		break;
6035 	case RATE_MCS_CHAN_WIDTH_80:
6036 		rinfo->bw = RATE_INFO_BW_80;
6037 		break;
6038 	case RATE_MCS_CHAN_WIDTH_160:
6039 		rinfo->bw = RATE_INFO_BW_160;
6040 		break;
6041 	case RATE_MCS_CHAN_WIDTH_320:
6042 		rinfo->bw = RATE_INFO_BW_320;
6043 		break;
6044 	}
6045 
6046 	if (format == RATE_MCS_CCK_MSK ||
6047 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
6048 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
6049 
6050 		/* add the offset needed to get to the legacy ofdm indices */
6051 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
6052 			rate += IWL_FIRST_OFDM_RATE;
6053 
6054 		switch (rate) {
6055 		case IWL_RATE_1M_INDEX:
6056 			rinfo->legacy = 10;
6057 			break;
6058 		case IWL_RATE_2M_INDEX:
6059 			rinfo->legacy = 20;
6060 			break;
6061 		case IWL_RATE_5M_INDEX:
6062 			rinfo->legacy = 55;
6063 			break;
6064 		case IWL_RATE_11M_INDEX:
6065 			rinfo->legacy = 110;
6066 			break;
6067 		case IWL_RATE_6M_INDEX:
6068 			rinfo->legacy = 60;
6069 			break;
6070 		case IWL_RATE_9M_INDEX:
6071 			rinfo->legacy = 90;
6072 			break;
6073 		case IWL_RATE_12M_INDEX:
6074 			rinfo->legacy = 120;
6075 			break;
6076 		case IWL_RATE_18M_INDEX:
6077 			rinfo->legacy = 180;
6078 			break;
6079 		case IWL_RATE_24M_INDEX:
6080 			rinfo->legacy = 240;
6081 			break;
6082 		case IWL_RATE_36M_INDEX:
6083 			rinfo->legacy = 360;
6084 			break;
6085 		case IWL_RATE_48M_INDEX:
6086 			rinfo->legacy = 480;
6087 			break;
6088 		case IWL_RATE_54M_INDEX:
6089 			rinfo->legacy = 540;
6090 		}
6091 		return;
6092 	}
6093 
6094 	rinfo->nss = u32_get_bits(rate_n_flags,
6095 				  RATE_MCS_NSS_MSK) + 1;
6096 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
6097 		RATE_HT_MCS_INDEX(rate_n_flags) :
6098 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
6099 
6100 	if (rate_n_flags & RATE_MCS_SGI_MSK)
6101 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
6102 
6103 	switch (format) {
6104 	case RATE_MCS_EHT_MSK:
6105 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
6106 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
6107 		break;
6108 	case RATE_MCS_HE_MSK:
6109 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
6110 
6111 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
6112 
6113 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
6114 			rinfo->bw = RATE_INFO_BW_HE_RU;
6115 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
6116 		}
6117 
6118 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
6119 		case RATE_MCS_HE_TYPE_SU:
6120 		case RATE_MCS_HE_TYPE_EXT_SU:
6121 			if (gi_ltf == 0 || gi_ltf == 1)
6122 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6123 			else if (gi_ltf == 2)
6124 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6125 			else if (gi_ltf == 3)
6126 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6127 			else
6128 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6129 			break;
6130 		case RATE_MCS_HE_TYPE_MU:
6131 			if (gi_ltf == 0 || gi_ltf == 1)
6132 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6133 			else if (gi_ltf == 2)
6134 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6135 			else
6136 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6137 			break;
6138 		case RATE_MCS_HE_TYPE_TRIG:
6139 			if (gi_ltf == 0 || gi_ltf == 1)
6140 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6141 			else
6142 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6143 			break;
6144 		}
6145 
6146 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
6147 			rinfo->he_dcm = 1;
6148 		break;
6149 	case RATE_MCS_HT_MSK:
6150 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
6151 		break;
6152 	case RATE_MCS_VHT_MSK:
6153 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
6154 		break;
6155 	}
6156 }
6157 
6158 void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
6159 				struct ieee80211_vif *vif,
6160 				struct ieee80211_sta *sta,
6161 				struct station_info *sinfo)
6162 {
6163 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6164 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6165 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
6166 	int i;
6167 
6168 	if (mvmsta->deflink.avg_energy) {
6169 		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
6170 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
6171 	}
6172 
6173 	if (iwl_mvm_has_tlc_offload(mvm)) {
6174 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
6175 
6176 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
6177 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6178 	}
6179 
6180 	/* if beacon filtering isn't on mac80211 does it anyway */
6181 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
6182 		return;
6183 
6184 	if (!vif->cfg.assoc)
6185 		return;
6186 
6187 	mutex_lock(&mvm->mutex);
6188 
6189 	if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
6190 		goto unlock;
6191 
6192 	if (iwl_mvm_request_statistics(mvm, false))
6193 		goto unlock;
6194 
6195 	sinfo->rx_beacon = 0;
6196 	for_each_mvm_vif_valid_link(mvmvif, i)
6197 		sinfo->rx_beacon += mvmvif->link[i]->beacon_stats.num_beacons +
6198 			mvmvif->link[i]->beacon_stats.accu_num_beacons;
6199 
6200 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
6201 	if (mvmvif->deflink.beacon_stats.avg_signal) {
6202 		/* firmware only reports a value after RXing a few beacons */
6203 		sinfo->rx_beacon_signal_avg =
6204 			mvmvif->deflink.beacon_stats.avg_signal;
6205 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
6206 	}
6207  unlock:
6208 	mutex_unlock(&mvm->mutex);
6209 }
6210 
6211 static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
6212 					    struct ieee80211_vif *vif,
6213 					    const  struct ieee80211_mlme_event *mlme)
6214 {
6215 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
6216 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
6217 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6218 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
6219 				       NULL);
6220 		return;
6221 	}
6222 
6223 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
6224 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6225 				       IWL_FW_INI_TIME_POINT_DEASSOC,
6226 				       NULL);
6227 		return;
6228 	}
6229 }
6230 
6231 static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
6232 					struct ieee80211_vif *vif,
6233 					const struct ieee80211_event *event)
6234 {
6235 #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
6236 	do {								\
6237 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
6238 			break;						\
6239 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
6240 	} while (0)
6241 
6242 	struct iwl_fw_dbg_trigger_tlv *trig;
6243 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
6244 
6245 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
6246 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
6247 		return;
6248 	}
6249 
6250 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
6251 				     FW_DBG_TRIGGER_MLME);
6252 	if (!trig)
6253 		return;
6254 
6255 	trig_mlme = (void *)trig->data;
6256 
6257 	if (event->u.mlme.data == ASSOC_EVENT) {
6258 		if (event->u.mlme.status == MLME_DENIED)
6259 			CHECK_MLME_TRIGGER(stop_assoc_denied,
6260 					   "DENIED ASSOC: reason %d",
6261 					    event->u.mlme.reason);
6262 		else if (event->u.mlme.status == MLME_TIMEOUT)
6263 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
6264 					   "ASSOC TIMEOUT");
6265 	} else if (event->u.mlme.data == AUTH_EVENT) {
6266 		if (event->u.mlme.status == MLME_DENIED)
6267 			CHECK_MLME_TRIGGER(stop_auth_denied,
6268 					   "DENIED AUTH: reason %d",
6269 					   event->u.mlme.reason);
6270 		else if (event->u.mlme.status == MLME_TIMEOUT)
6271 			CHECK_MLME_TRIGGER(stop_auth_timeout,
6272 					   "AUTH TIMEOUT");
6273 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
6274 		CHECK_MLME_TRIGGER(stop_rx_deauth,
6275 				   "DEAUTH RX %d", event->u.mlme.reason);
6276 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
6277 		CHECK_MLME_TRIGGER(stop_tx_deauth,
6278 				   "DEAUTH TX %d", event->u.mlme.reason);
6279 	}
6280 #undef CHECK_MLME_TRIGGER
6281 }
6282 
6283 static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
6284 					  struct ieee80211_vif *vif,
6285 					  const struct ieee80211_event *event)
6286 {
6287 	struct iwl_fw_dbg_trigger_tlv *trig;
6288 	struct iwl_fw_dbg_trigger_ba *ba_trig;
6289 
6290 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
6291 				     FW_DBG_TRIGGER_BA);
6292 	if (!trig)
6293 		return;
6294 
6295 	ba_trig = (void *)trig->data;
6296 
6297 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
6298 		return;
6299 
6300 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
6301 				"BAR received from %pM, tid %d, ssn %d",
6302 				event->u.ba.sta->addr, event->u.ba.tid,
6303 				event->u.ba.ssn);
6304 }
6305 
6306 void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
6307 				struct ieee80211_vif *vif,
6308 				const struct ieee80211_event *event)
6309 {
6310 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6311 
6312 	switch (event->type) {
6313 	case MLME_EVENT:
6314 		iwl_mvm_event_mlme_callback(mvm, vif, event);
6315 		break;
6316 	case BAR_RX_EVENT:
6317 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
6318 		break;
6319 	case BA_FRAME_TIMEOUT:
6320 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
6321 						     event->u.ba.tid);
6322 		break;
6323 	default:
6324 		break;
6325 	}
6326 }
6327 
6328 #define SYNC_RX_QUEUE_TIMEOUT (HZ)
6329 void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
6330 				     enum iwl_mvm_rxq_notif_type type,
6331 				     bool sync,
6332 				     const void *data, u32 size)
6333 {
6334 	struct {
6335 		struct iwl_rxq_sync_cmd cmd;
6336 		struct iwl_mvm_internal_rxq_notif notif;
6337 	} __packed cmd = {
6338 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
6339 		.cmd.count =
6340 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
6341 				    size),
6342 		.notif.type = type,
6343 		.notif.sync = sync,
6344 	};
6345 	struct iwl_host_cmd hcmd = {
6346 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
6347 		.data[0] = &cmd,
6348 		.len[0] = sizeof(cmd),
6349 		.data[1] = data,
6350 		.len[1] = size,
6351 		.flags = sync ? 0 : CMD_ASYNC,
6352 	};
6353 	int ret;
6354 
6355 	/* size must be a multiple of DWORD */
6356 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
6357 		return;
6358 
6359 	if (!iwl_mvm_has_new_rx_api(mvm))
6360 		return;
6361 
6362 	if (sync) {
6363 		cmd.notif.cookie = mvm->queue_sync_cookie;
6364 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6365 	}
6366 
6367 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
6368 	if (ret) {
6369 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
6370 		goto out;
6371 	}
6372 
6373 	if (sync) {
6374 		lockdep_assert_held(&mvm->mutex);
6375 		ret = wait_event_timeout(mvm->rx_sync_waitq,
6376 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
6377 					 iwl_mvm_is_radio_hw_killed(mvm),
6378 					 SYNC_RX_QUEUE_TIMEOUT);
6379 		WARN_ONCE(!ret && !iwl_mvm_is_radio_hw_killed(mvm),
6380 			  "queue sync: failed to sync, state is 0x%lx, cookie %d\n",
6381 			  mvm->queue_sync_state,
6382 			  mvm->queue_sync_cookie);
6383 	}
6384 
6385 out:
6386 	if (sync) {
6387 		mvm->queue_sync_state = 0;
6388 		mvm->queue_sync_cookie++;
6389 	}
6390 }
6391 
6392 void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
6393 {
6394 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6395 
6396 	mutex_lock(&mvm->mutex);
6397 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
6398 	mutex_unlock(&mvm->mutex);
6399 }
6400 
6401 int
6402 iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6403 				    struct ieee80211_vif *vif,
6404 				    struct cfg80211_ftm_responder_stats *stats)
6405 {
6406 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6407 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6408 
6409 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6410 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6411 		return -EINVAL;
6412 
6413 	mutex_lock(&mvm->mutex);
6414 	*stats = mvm->ftm_resp_stats;
6415 	mutex_unlock(&mvm->mutex);
6416 
6417 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6418 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6419 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
6420 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
6421 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6422 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6423 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6424 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6425 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6426 
6427 	return 0;
6428 }
6429 
6430 int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6431 		       struct cfg80211_pmsr_request *request)
6432 {
6433 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6434 	int ret;
6435 
6436 	mutex_lock(&mvm->mutex);
6437 	ret = iwl_mvm_ftm_start(mvm, vif, request);
6438 	mutex_unlock(&mvm->mutex);
6439 
6440 	return ret;
6441 }
6442 
6443 void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6444 			struct cfg80211_pmsr_request *request)
6445 {
6446 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6447 
6448 	mutex_lock(&mvm->mutex);
6449 	iwl_mvm_ftm_abort(mvm, request);
6450 	mutex_unlock(&mvm->mutex);
6451 }
6452 
6453 static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6454 {
6455 	u8 protocol = ip_hdr(skb)->protocol;
6456 
6457 	if (!IS_ENABLED(CONFIG_INET))
6458 		return false;
6459 
6460 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6461 }
6462 
6463 static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6464 				      struct sk_buff *head,
6465 				      struct sk_buff *skb)
6466 {
6467 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6468 
6469 	/* For now don't aggregate IPv6 in AMSDU */
6470 	if (skb->protocol != htons(ETH_P_IP))
6471 		return false;
6472 
6473 	if (!iwl_mvm_is_csum_supported(mvm))
6474 		return true;
6475 
6476 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6477 }
6478 
6479 int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6480 			     struct ieee80211_vif *vif,
6481 			     struct cfg80211_set_hw_timestamp *hwts)
6482 {
6483 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6484 	u32 protocols = 0;
6485 	int ret;
6486 
6487 	/* HW timestamping is only supported for a specific station */
6488 	if (!hwts->macaddr)
6489 		return -EOPNOTSUPP;
6490 
6491 	if (hwts->enable)
6492 		protocols =
6493 			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6494 
6495 	mutex_lock(&mvm->mutex);
6496 	ret = iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6497 	mutex_unlock(&mvm->mutex);
6498 
6499 	return ret;
6500 }
6501 
6502 const struct ieee80211_ops iwl_mvm_hw_ops = {
6503 	.tx = iwl_mvm_mac_tx,
6504 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6505 	.ampdu_action = iwl_mvm_mac_ampdu_action,
6506 	.get_antenna = iwl_mvm_op_get_antenna,
6507 	.set_antenna = iwl_mvm_op_set_antenna,
6508 	.start = iwl_mvm_mac_start,
6509 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
6510 	.stop = iwl_mvm_mac_stop,
6511 	.add_interface = iwl_mvm_mac_add_interface,
6512 	.remove_interface = iwl_mvm_mac_remove_interface,
6513 	.config = iwl_mvm_mac_config,
6514 	.prepare_multicast = iwl_mvm_prepare_multicast,
6515 	.configure_filter = iwl_mvm_configure_filter,
6516 	.config_iface_filter = iwl_mvm_config_iface_filter,
6517 	.bss_info_changed = iwl_mvm_bss_info_changed,
6518 	.hw_scan = iwl_mvm_mac_hw_scan,
6519 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6520 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6521 	.sta_state = iwl_mvm_mac_sta_state,
6522 	.sta_notify = iwl_mvm_mac_sta_notify,
6523 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6524 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6525 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6526 	.sta_rc_update = iwl_mvm_sta_rc_update,
6527 	.conf_tx = iwl_mvm_mac_conf_tx,
6528 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
6529 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6530 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6531 	.flush = iwl_mvm_mac_flush,
6532 	.flush_sta = iwl_mvm_mac_flush_sta,
6533 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
6534 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6535 	.set_key = iwl_mvm_mac_set_key,
6536 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
6537 	.remain_on_channel = iwl_mvm_roc,
6538 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
6539 	.add_chanctx = iwl_mvm_add_chanctx,
6540 	.remove_chanctx = iwl_mvm_remove_chanctx,
6541 	.change_chanctx = iwl_mvm_change_chanctx,
6542 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6543 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6544 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6545 
6546 	.start_ap = iwl_mvm_start_ap,
6547 	.stop_ap = iwl_mvm_stop_ap,
6548 	.join_ibss = iwl_mvm_start_ibss,
6549 	.leave_ibss = iwl_mvm_stop_ibss,
6550 
6551 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
6552 
6553 	.set_tim = iwl_mvm_set_tim,
6554 
6555 	.channel_switch = iwl_mvm_channel_switch,
6556 	.pre_channel_switch = iwl_mvm_mac_pre_channel_switch,
6557 	.post_channel_switch = iwl_mvm_post_channel_switch,
6558 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
6559 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6560 
6561 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6562 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6563 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6564 
6565 	.event_callback = iwl_mvm_mac_event_callback,
6566 
6567 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
6568 
6569 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
6570 
6571 #ifdef CONFIG_PM_SLEEP
6572 	/* look at d3.c */
6573 	.suspend = iwl_mvm_suspend,
6574 	.resume = iwl_mvm_resume,
6575 	.set_wakeup = iwl_mvm_set_wakeup,
6576 	.set_rekey_data = iwl_mvm_set_rekey_data,
6577 #if IS_ENABLED(CONFIG_IPV6)
6578 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6579 #endif
6580 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6581 #endif
6582 	.get_survey = iwl_mvm_mac_get_survey,
6583 	.sta_statistics = iwl_mvm_mac_sta_statistics,
6584 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6585 	.start_pmsr = iwl_mvm_start_pmsr,
6586 	.abort_pmsr = iwl_mvm_abort_pmsr,
6587 
6588 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6589 #ifdef CONFIG_IWLWIFI_DEBUGFS
6590 	.vif_add_debugfs = iwl_mvm_vif_add_debugfs,
6591 	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
6592 #endif
6593 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6594 };
6595