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