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