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