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