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