xref: /linux/drivers/net/wireless/intel/iwlwifi/mld/mac80211.c (revision 9bf1b409afc7c4a1f0340f3975846c4f3278643a)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2024-2025 Intel Corporation
4  */
5 
6 #include <net/mac80211.h>
7 #include <linux/fips.h>
8 #include <linux/ip.h>
9 
10 #include "mld.h"
11 #include "mac80211.h"
12 #include "phy.h"
13 #include "iface.h"
14 #include "power.h"
15 #include "sta.h"
16 #include "agg.h"
17 #include "scan.h"
18 #include "d3.h"
19 #include "tlc.h"
20 #include "key.h"
21 #include "ap.h"
22 #include "tx.h"
23 #include "roc.h"
24 #include "mlo.h"
25 #include "stats.h"
26 #include "iwl-nvm-parse.h"
27 #include "ftm-initiator.h"
28 #include "low_latency.h"
29 #include "fw/api/scan.h"
30 #include "fw/api/context.h"
31 #include "fw/api/filter.h"
32 #include "fw/api/sta.h"
33 #include "fw/api/tdls.h"
34 #ifdef CONFIG_PM_SLEEP
35 #include "fw/api/d3.h"
36 #endif /* CONFIG_PM_SLEEP */
37 #include "iwl-trans.h"
38 
39 #define IWL_MLD_LIMITS(ap)					\
40 	{							\
41 		.max = 2,					\
42 		.types = BIT(NL80211_IFTYPE_STATION),		\
43 	},							\
44 	{							\
45 		.max = 1,					\
46 		.types = ap |					\
47 			 BIT(NL80211_IFTYPE_P2P_CLIENT) |	\
48 			 BIT(NL80211_IFTYPE_P2P_GO),		\
49 	},							\
50 	{							\
51 		.max = 1,					\
52 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),	\
53 	},
54 
55 static const struct ieee80211_iface_limit iwl_mld_limits[] = {
56 	IWL_MLD_LIMITS(0)
57 };
58 
59 static const struct ieee80211_iface_limit iwl_mld_limits_ap[] = {
60 	IWL_MLD_LIMITS(BIT(NL80211_IFTYPE_AP))
61 };
62 
63 static const struct ieee80211_iface_limit iwl_mld_limits_nan[] = {
64 	{
65 		.max = 2,
66 		.types = BIT(NL80211_IFTYPE_STATION),
67 	},
68 	{
69 		.max = 1,
70 		.types = BIT(NL80211_IFTYPE_NAN),
71 	},
72 	/* Removed when two channels are permitted */
73 	{
74 		.max = 1,
75 		.types = BIT(NL80211_IFTYPE_AP),
76 	},
77 };
78 
79 static const struct ieee80211_iface_combination
80 iwl_mld_iface_combinations[] = {
81 	{
82 		.num_different_channels = 2,
83 		.max_interfaces = 4,
84 		.limits = iwl_mld_limits,
85 		.n_limits = ARRAY_SIZE(iwl_mld_limits),
86 	},
87 	{
88 		.num_different_channels = 1,
89 		.max_interfaces = 4,
90 		.limits = iwl_mld_limits_ap,
91 		.n_limits = ARRAY_SIZE(iwl_mld_limits_ap),
92 	},
93 	/* NAN combinations follow, these exclude P2P */
94 	{
95 		.num_different_channels = 2,
96 		.max_interfaces = 3,
97 		.limits = iwl_mld_limits_nan,
98 		.n_limits = ARRAY_SIZE(iwl_mld_limits_nan) - 1,
99 	},
100 	{
101 		.num_different_channels = 1,
102 		.max_interfaces = 4,
103 		.limits = iwl_mld_limits_nan,
104 		.n_limits = ARRAY_SIZE(iwl_mld_limits_nan),
105 	}
106 };
107 
108 static const u8 ext_capa_base[IWL_MLD_STA_EXT_CAPA_SIZE] = {
109 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
110 	[2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
111 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
112 	      WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
113 	[8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
114 };
115 
116 #define IWL_MLD_EMLSR_CAPA	(IEEE80211_EML_CAP_EMLSR_SUPP | \
117 				 IEEE80211_EML_CAP_EMLSR_PADDING_DELAY_32US << \
118 					__bf_shf(IEEE80211_EML_CAP_EMLSR_PADDING_DELAY) | \
119 				 IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_64US << \
120 					__bf_shf(IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY))
121 #define IWL_MLD_CAPA_OPS (FIELD_PREP_CONST( \
122 			IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP, \
123 			IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP_SAME) | \
124 			IEEE80211_MLD_CAP_OP_LINK_RECONF_SUPPORT)
125 
126 static void iwl_mld_hw_set_addresses(struct iwl_mld *mld)
127 {
128 	struct wiphy *wiphy = mld->wiphy;
129 	int num_addrs = 1;
130 
131 	/* Extract MAC address */
132 	memcpy(mld->addresses[0].addr, mld->nvm_data->hw_addr, ETH_ALEN);
133 	wiphy->addresses = mld->addresses;
134 	wiphy->n_addresses = 1;
135 
136 	/* Extract additional MAC addresses if available */
137 	if (mld->nvm_data->n_hw_addrs > 1)
138 		num_addrs = min(mld->nvm_data->n_hw_addrs,
139 				IWL_MLD_MAX_ADDRESSES);
140 
141 	for (int i = 1; i < num_addrs; i++) {
142 		memcpy(mld->addresses[i].addr,
143 		       mld->addresses[i - 1].addr,
144 		       ETH_ALEN);
145 		mld->addresses[i].addr[ETH_ALEN - 1]++;
146 		wiphy->n_addresses++;
147 	}
148 }
149 
150 static void iwl_mld_hw_set_channels(struct iwl_mld *mld)
151 {
152 	struct wiphy *wiphy = mld->wiphy;
153 	struct ieee80211_supported_band *bands = mld->nvm_data->bands;
154 
155 	wiphy->bands[NL80211_BAND_2GHZ] = &bands[NL80211_BAND_2GHZ];
156 	wiphy->bands[NL80211_BAND_5GHZ] = &bands[NL80211_BAND_5GHZ];
157 
158 	if (bands[NL80211_BAND_6GHZ].n_channels)
159 		wiphy->bands[NL80211_BAND_6GHZ] = &bands[NL80211_BAND_6GHZ];
160 }
161 
162 static void iwl_mld_hw_set_security(struct iwl_mld *mld)
163 {
164 	struct ieee80211_hw *hw = mld->hw;
165 	static const u32 mld_ciphers[] = {
166 		WLAN_CIPHER_SUITE_WEP40,
167 		WLAN_CIPHER_SUITE_WEP104,
168 		WLAN_CIPHER_SUITE_TKIP,
169 		WLAN_CIPHER_SUITE_CCMP,
170 		WLAN_CIPHER_SUITE_GCMP,
171 		WLAN_CIPHER_SUITE_GCMP_256,
172 		WLAN_CIPHER_SUITE_AES_CMAC,
173 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
174 		WLAN_CIPHER_SUITE_BIP_GMAC_256
175 	};
176 
177 	if (fips_enabled)
178 		return;
179 
180 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mld_ciphers);
181 	hw->wiphy->cipher_suites = mld_ciphers;
182 
183 	ieee80211_hw_set(hw, MFP_CAPABLE);
184 	wiphy_ext_feature_set(hw->wiphy,
185 			      NL80211_EXT_FEATURE_BEACON_PROTECTION);
186 }
187 
188 static void iwl_mld_hw_set_antennas(struct iwl_mld *mld)
189 {
190 	struct wiphy *wiphy = mld->wiphy;
191 
192 	wiphy->available_antennas_tx = iwl_mld_get_valid_tx_ant(mld);
193 	wiphy->available_antennas_rx = iwl_mld_get_valid_rx_ant(mld);
194 }
195 
196 static void iwl_mld_hw_set_pm(struct iwl_mld *mld)
197 {
198 #ifdef CONFIG_PM_SLEEP
199 	struct wiphy *wiphy = mld->wiphy;
200 
201 	if (!device_can_wakeup(mld->trans->dev))
202 		return;
203 
204 	if (fips_enabled)
205 		return;
206 
207 	mld->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
208 			     WIPHY_WOWLAN_DISCONNECT |
209 			     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
210 			     WIPHY_WOWLAN_RFKILL_RELEASE |
211 			     WIPHY_WOWLAN_NET_DETECT |
212 			     WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
213 			     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
214 			     WIPHY_WOWLAN_4WAY_HANDSHAKE;
215 
216 	mld->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
217 	mld->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
218 	mld->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
219 	mld->wowlan.max_nd_match_sets = IWL_SCAN_MAX_PROFILES_V2;
220 
221 	wiphy->wowlan = &mld->wowlan;
222 #endif /* CONFIG_PM_SLEEP */
223 }
224 
225 static void iwl_mac_hw_set_radiotap(struct iwl_mld *mld)
226 {
227 	struct ieee80211_hw *hw = mld->hw;
228 
229 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
230 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
231 
232 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
233 				    IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
234 
235 	hw->radiotap_timestamp.units_pos =
236 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
237 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
238 
239 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
240 	hw->radiotap_timestamp.accuracy = 22;
241 }
242 
243 static void iwl_mac_hw_set_flags(struct iwl_mld *mld)
244 {
245 	struct ieee80211_hw *hw = mld->hw;
246 
247 	ieee80211_hw_set(hw, USES_RSS);
248 	ieee80211_hw_set(hw, HANDLES_QUIET_CSA);
249 	ieee80211_hw_set(hw, AP_LINK_PS);
250 	ieee80211_hw_set(hw, SIGNAL_DBM);
251 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
252 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
253 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
254 	ieee80211_hw_set(hw, SUPPORTS_PS);
255 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
256 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
257 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
258 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
259 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
260 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
261 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
262 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
263 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
264 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
265 	ieee80211_hw_set(hw, TX_AMSDU);
266 	ieee80211_hw_set(hw, TX_FRAG_LIST);
267 	ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
268 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
269 	ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
270 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
271 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
272 	ieee80211_hw_set(hw, TDLS_WIDER_BW);
273 }
274 
275 static void iwl_mac_hw_set_wiphy(struct iwl_mld *mld)
276 {
277 	struct ieee80211_hw *hw = mld->hw;
278 	struct wiphy *wiphy = hw->wiphy;
279 	const struct iwl_ucode_capabilities *ucode_capa = &mld->fw->ucode_capa;
280 
281 	snprintf(wiphy->fw_version,
282 		 sizeof(wiphy->fw_version),
283 		 "%.31s", mld->fw->fw_version);
284 
285 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
286 				 BIT(NL80211_IFTYPE_P2P_CLIENT) |
287 				 BIT(NL80211_IFTYPE_AP) |
288 				 BIT(NL80211_IFTYPE_P2P_GO) |
289 				 BIT(NL80211_IFTYPE_P2P_DEVICE) |
290 				 BIT(NL80211_IFTYPE_ADHOC);
291 
292 	wiphy->features |= NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
293 			   NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
294 			   NL80211_FEATURE_ND_RANDOM_MAC_ADDR |
295 			   NL80211_FEATURE_HT_IBSS |
296 			   NL80211_FEATURE_P2P_GO_CTWIN |
297 			   NL80211_FEATURE_LOW_PRIORITY_SCAN |
298 			   NL80211_FEATURE_P2P_GO_OPPPS |
299 			   NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
300 			   NL80211_FEATURE_SUPPORTS_WMM_ADMISSION |
301 			   NL80211_FEATURE_TX_POWER_INSERTION |
302 			   NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
303 
304 	wiphy->flags |= WIPHY_FLAG_IBSS_RSN |
305 			WIPHY_FLAG_AP_UAPSD |
306 			WIPHY_FLAG_HAS_CHANNEL_SWITCH |
307 			WIPHY_FLAG_SPLIT_SCAN_6GHZ |
308 			WIPHY_FLAG_SUPPORTS_TDLS |
309 			WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
310 
311 	/* For fips_enabled, don't support WiFi7 due to WPA3/MFP requirements */
312 	if (mld->nvm_data->sku_cap_11be_enable &&
313 	    !iwlwifi_mod_params.disable_11ax &&
314 	    !iwlwifi_mod_params.disable_11be &&
315 	    !fips_enabled)
316 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
317 
318 	/* the firmware uses u8 for num of iterations, but 0xff is saved for
319 	 * infinite loop, so the maximum number of iterations is actually 254.
320 	 */
321 	wiphy->max_sched_scan_plan_iterations = 254;
322 	wiphy->max_sched_scan_ie_len = iwl_mld_scan_max_template_size();
323 	wiphy->max_scan_ie_len = iwl_mld_scan_max_template_size();
324 	wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
325 	wiphy->max_scan_ssids = PROBE_OPTION_MAX;
326 	wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
327 	wiphy->max_sched_scan_reqs = 1;
328 	wiphy->max_sched_scan_plan_interval = U16_MAX;
329 	wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES_V2;
330 
331 	wiphy->max_remain_on_channel_duration = 10000;
332 
333 	wiphy->hw_version = mld->trans->info.hw_id;
334 
335 	wiphy->hw_timestamp_max_peers = 1;
336 
337 	if (iwl_mld_nan_supported(mld)) {
338 		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_NAN);
339 		hw->wiphy->iface_combinations = iwl_mld_iface_combinations;
340 		hw->wiphy->n_iface_combinations =
341 			ARRAY_SIZE(iwl_mld_iface_combinations);
342 
343 		hw->wiphy->nan_supported_bands = BIT(NL80211_BAND_2GHZ);
344 		if (mld->nvm_data->bands[NL80211_BAND_5GHZ].n_channels)
345 			hw->wiphy->nan_supported_bands |=
346 				BIT(NL80211_BAND_5GHZ);
347 
348 		hw->wiphy->nan_capa.flags = WIPHY_NAN_FLAGS_CONFIGURABLE_SYNC |
349 					    WIPHY_NAN_FLAGS_USERSPACE_DE;
350 
351 		hw->wiphy->nan_capa.op_mode = NAN_OP_MODE_PHY_MODE_MASK |
352 					      NAN_OP_MODE_80P80MHZ |
353 					      NAN_OP_MODE_160MHZ;
354 
355 		/* Support 2 antenna's for Tx and Rx */
356 		hw->wiphy->nan_capa.n_antennas = 0x22;
357 
358 		/* Maximal channel switch time is 4 msec */
359 		hw->wiphy->nan_capa.max_channel_switch_time = 4;
360 		hw->wiphy->nan_capa.dev_capabilities =
361 			NAN_DEV_CAPA_EXT_KEY_ID_SUPPORTED |
362 			NAN_DEV_CAPA_NDPE_SUPPORTED;
363 	} else {
364 		wiphy->iface_combinations = iwl_mld_iface_combinations;
365 		/* Do not include NAN combinations */
366 		wiphy->n_iface_combinations =
367 			ARRAY_SIZE(iwl_mld_iface_combinations) - 2;
368 	}
369 
370 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
371 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_DFS_CONCURRENT);
372 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
373 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SCAN_START_TIME);
374 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_BSS_PARENT_TSF);
375 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
376 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
377 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
378 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
379 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
380 	wiphy_ext_feature_set(wiphy,
381 			      NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS);
382 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SPP_AMSDU_SUPPORT);
383 
384 	if (fw_has_capa(ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
385 		wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_PROTECTED_TWT);
386 
387 	if (iwlmld_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
388 		wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
389 	else
390 		wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
391 
392 	/* We are done for non-HE */
393 	if (iwlwifi_mod_params.disable_11ax)
394 		return;
395 
396 	ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
397 	ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
398 
399 	wiphy->iftype_ext_capab = mld->ext_capab;
400 	wiphy->num_iftype_ext_capab = ARRAY_SIZE(mld->ext_capab);
401 
402 	BUILD_BUG_ON(sizeof(mld->sta_ext_capab) < sizeof(ext_capa_base));
403 
404 	memcpy(mld->sta_ext_capab, ext_capa_base, sizeof(ext_capa_base));
405 
406 	mld->ext_capab[0].iftype = NL80211_IFTYPE_STATION;
407 	mld->ext_capab[0].extended_capabilities = mld->sta_ext_capab;
408 	mld->ext_capab[0].extended_capabilities_mask = mld->sta_ext_capab;
409 	mld->ext_capab[0].extended_capabilities_len = sizeof(mld->sta_ext_capab);
410 
411 	if (!mld->nvm_data->sku_cap_11be_enable ||
412 	    iwlwifi_mod_params.disable_11be)
413 		return;
414 
415 	mld->ext_capab[0].eml_capabilities = IWL_MLD_EMLSR_CAPA;
416 	mld->ext_capab[0].mld_capa_and_ops = IWL_MLD_CAPA_OPS;
417 
418 }
419 
420 static void iwl_mac_hw_set_misc(struct iwl_mld *mld)
421 {
422 	struct ieee80211_hw *hw = mld->hw;
423 
424 	hw->queues = IEEE80211_NUM_ACS;
425 
426 	hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
427 	hw->netdev_features |= mld->trans->mac_cfg->base->features;
428 
429 	hw->max_tx_fragments = mld->trans->info.max_skb_frags;
430 	hw->max_listen_interval = IWL_MLD_CONN_LISTEN_INTERVAL;
431 
432 	hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
433 	hw->uapsd_queues = IEEE80211_WMM_IE_STA_QOSINFO_AC_VO |
434 			   IEEE80211_WMM_IE_STA_QOSINFO_AC_VI |
435 			   IEEE80211_WMM_IE_STA_QOSINFO_AC_BK |
436 			   IEEE80211_WMM_IE_STA_QOSINFO_AC_BE;
437 
438 	hw->chanctx_data_size = sizeof(struct iwl_mld_phy);
439 	hw->vif_data_size = sizeof(struct iwl_mld_vif);
440 	hw->sta_data_size = sizeof(struct iwl_mld_sta);
441 	hw->txq_data_size = sizeof(struct iwl_mld_txq);
442 
443 	/* TODO: Remove this division when IEEE80211_MAX_AMPDU_BUF_EHT size
444 	 * is supported.
445 	 * Note: ensure that IWL_DEFAULT_QUEUE_SIZE_EHT is updated accordingly.
446 	 */
447 	hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF_EHT / 2;
448 }
449 
450 static int iwl_mld_hw_verify_preconditions(struct iwl_mld *mld)
451 {
452 	int ratecheck;
453 
454 	/* check for rates version 3 */
455 	ratecheck =
456 		(iwl_fw_lookup_cmd_ver(mld->fw, TX_CMD, 0) >= 11) +
457 		(iwl_fw_lookup_notif_ver(mld->fw, DATA_PATH_GROUP,
458 					 TLC_MNG_UPDATE_NOTIF, 0) >= 4) +
459 		(iwl_fw_lookup_notif_ver(mld->fw, LEGACY_GROUP,
460 					 REPLY_RX_MPDU_CMD, 0) >= 6) +
461 		(iwl_fw_lookup_notif_ver(mld->fw, LONG_GROUP, TX_CMD, 0) >= 9);
462 
463 	if (ratecheck != 0 && ratecheck != 4) {
464 		IWL_ERR(mld, "Firmware has inconsistent rates\n");
465 		return -EINVAL;
466 	}
467 
468 	/* 11ax is expected to be enabled for all supported devices */
469 	if (WARN_ON(!mld->nvm_data->sku_cap_11ax_enable))
470 		return -EINVAL;
471 
472 	/* LAR is expected to be enabled for all supported devices */
473 	if (WARN_ON(!mld->nvm_data->lar_enabled))
474 		return -EINVAL;
475 
476 	/* All supported devices are currently using version 3 of the cmd.
477 	 * Since version 3, IWL_SCAN_MAX_PROFILES_V2 shall be used where
478 	 * necessary.
479 	 */
480 	if (WARN_ON(iwl_fw_lookup_cmd_ver(mld->fw,
481 					  SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
482 					  IWL_FW_CMD_VER_UNKNOWN) != 3))
483 		return -EINVAL;
484 
485 	return 0;
486 }
487 
488 int iwl_mld_register_hw(struct iwl_mld *mld)
489 {
490 	/* verify once essential preconditions required for setting
491 	 * the hw capabilities
492 	 */
493 	if (iwl_mld_hw_verify_preconditions(mld))
494 		return -EINVAL;
495 
496 	iwl_mld_hw_set_addresses(mld);
497 	iwl_mld_hw_set_channels(mld);
498 	iwl_mld_hw_set_security(mld);
499 	iwl_mld_hw_set_pm(mld);
500 	iwl_mld_hw_set_antennas(mld);
501 	iwl_mac_hw_set_radiotap(mld);
502 	iwl_mac_hw_set_flags(mld);
503 	iwl_mac_hw_set_wiphy(mld);
504 	iwl_mac_hw_set_misc(mld);
505 
506 	SET_IEEE80211_DEV(mld->hw, mld->trans->dev);
507 
508 	return ieee80211_register_hw(mld->hw);
509 }
510 
511 static void
512 iwl_mld_mac80211_tx(struct ieee80211_hw *hw,
513 		    struct ieee80211_tx_control *control, struct sk_buff *skb)
514 {
515 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
516 	struct ieee80211_sta *sta = control->sta;
517 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
518 	struct ieee80211_hdr *hdr = (void *)skb->data;
519 	u32 link_id = u32_get_bits(info->control.flags,
520 				   IEEE80211_TX_CTRL_MLO_LINK);
521 
522 	/* In AP mode, mgmt frames are sent on the bcast station,
523 	 * so the FW can't translate the MLD addr to the link addr. Do it here
524 	 */
525 	if (ieee80211_is_mgmt(hdr->frame_control) && sta &&
526 	    link_id != IEEE80211_LINK_UNSPECIFIED &&
527 	    !ieee80211_is_probe_resp(hdr->frame_control)) {
528 		/* translate MLD addresses to LINK addresses */
529 		struct ieee80211_link_sta *link_sta =
530 			rcu_dereference(sta->link[link_id]);
531 		struct ieee80211_bss_conf *link_conf =
532 			rcu_dereference(info->control.vif->link_conf[link_id]);
533 		struct ieee80211_mgmt *mgmt;
534 
535 		if (WARN_ON(!link_sta || !link_conf)) {
536 			ieee80211_free_txskb(hw, skb);
537 			return;
538 		}
539 
540 		mgmt = (void *)hdr;
541 		memcpy(mgmt->da, link_sta->addr, ETH_ALEN);
542 		memcpy(mgmt->sa, link_conf->addr, ETH_ALEN);
543 		memcpy(mgmt->bssid, link_conf->bssid, ETH_ALEN);
544 	}
545 
546 	iwl_mld_tx_skb(mld, skb, NULL);
547 }
548 
549 static void
550 iwl_mld_restart_cleanup(struct iwl_mld *mld)
551 {
552 	iwl_cleanup_mld(mld);
553 
554 	ieee80211_iterate_interfaces(mld->hw, IEEE80211_IFACE_ITER_ACTIVE,
555 				     iwl_mld_cleanup_vif, NULL);
556 
557 	ieee80211_iterate_stations_atomic(mld->hw,
558 					  iwl_mld_cleanup_sta, NULL);
559 
560 	iwl_mld_ftm_restart_cleanup(mld);
561 }
562 
563 static
564 int iwl_mld_mac80211_start(struct ieee80211_hw *hw)
565 {
566 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
567 	bool in_d3 = false;
568 	int ret = 0;
569 
570 	lockdep_assert_wiphy(mld->wiphy);
571 
572 #ifdef CONFIG_PM_SLEEP
573 	/* Unless the host goes into hibernate the FW always stays on and
574 	 * the d3_resume flow is used. When wowlan is configured, mac80211
575 	 * would call it's resume callback and the wowlan_resume flow
576 	 * would be used.
577 	 */
578 
579 	in_d3 = mld->fw_status.in_d3;
580 	if (in_d3) {
581 		/* mac80211 already cleaned up the state, no need for cleanup */
582 		ret = iwl_mld_no_wowlan_resume(mld);
583 		if (ret) {
584 			iwl_mld_stop_fw(mld);
585 			/* We're not really restarting in the sense of
586 			 * in_hw_restart even if we got an error during
587 			 * this. We'll just start again below and have
588 			 * nothing to recover, mac80211 will do anyway.
589 			 */
590 			mld->fw_status.in_hw_restart = false;
591 		}
592 	}
593 #endif /* CONFIG_PM_SLEEP */
594 
595 	if (mld->fw_status.in_hw_restart) {
596 		iwl_mld_stop_fw(mld);
597 		iwl_mld_restart_cleanup(mld);
598 	}
599 
600 	if (!in_d3 || ret) {
601 		ret = iwl_mld_start_fw(mld);
602 		if (ret)
603 			goto error;
604 	}
605 
606 	mld->scan.last_start_time_jiffies = jiffies;
607 
608 	iwl_dbg_tlv_time_point(&mld->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
609 			       NULL);
610 	iwl_dbg_tlv_time_point(&mld->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
611 			       NULL);
612 
613 	return 0;
614 
615 error:
616 	/* If we failed to restart the hw, there is nothing useful
617 	 * we can do but indicate we are no longer in restart.
618 	 */
619 	mld->fw_status.in_hw_restart = false;
620 
621 	return ret;
622 }
623 
624 static
625 void iwl_mld_mac80211_stop(struct ieee80211_hw *hw, bool suspend)
626 {
627 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
628 
629 	lockdep_assert_wiphy(mld->wiphy);
630 
631 	wiphy_work_cancel(mld->wiphy, &mld->add_txqs_wk);
632 
633 	/* if the suspend flow fails the fw is in error. Stop it here, and it
634 	 * will be started upon wakeup
635 	 */
636 	if (!suspend ||
637 	    (IS_ENABLED(CONFIG_PM_SLEEP) && iwl_mld_no_wowlan_suspend(mld)))
638 		iwl_mld_stop_fw(mld);
639 
640 	/* Clear in_hw_restart flag when stopping the hw, as mac80211 won't
641 	 * execute the restart.
642 	 */
643 	mld->fw_status.in_hw_restart = false;
644 
645 	/* We shouldn't have any UIDs still set. Loop over all the UIDs to
646 	 * make sure there's nothing left there and warn if any is found.
647 	 */
648 	for (int i = 0; i < ARRAY_SIZE(mld->scan.uid_status); i++)
649 		if (WARN_ONCE(mld->scan.uid_status[i],
650 			      "UMAC scan UID %d status was not cleaned (0x%x 0x%x)\n",
651 			      i, mld->scan.uid_status[i], mld->scan.status))
652 			mld->scan.uid_status[i] = 0;
653 }
654 
655 static
656 int iwl_mld_mac80211_config(struct ieee80211_hw *hw, int radio_idx,
657 			    u32 changed)
658 {
659 	return 0;
660 }
661 
662 static
663 int iwl_mld_mac80211_add_interface(struct ieee80211_hw *hw,
664 				   struct ieee80211_vif *vif)
665 {
666 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
667 	int ret;
668 
669 	lockdep_assert_wiphy(mld->wiphy);
670 
671 	/* Construct mld_vif, add it to fw, and map its ID to ieee80211_vif */
672 	ret = iwl_mld_add_vif(mld, vif);
673 	if (ret)
674 		return ret;
675 
676 	/*
677 	 * Add the default link, but not if this is an MLD vif as that implies
678 	 * the HW is restarting and it will be configured by change_vif_links.
679 	 */
680 	if (vif->type != NL80211_IFTYPE_NAN && !ieee80211_vif_is_mld(vif)) {
681 		ret = iwl_mld_add_link(mld, &vif->bss_conf);
682 		if (ret)
683 			goto err;
684 	}
685 
686 	if (vif->type == NL80211_IFTYPE_STATION) {
687 		vif->driver_flags |= IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC;
688 		if (!vif->p2p)
689 			vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
690 					     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
691 	}
692 
693 	if (vif->p2p)
694 		vif->driver_flags |= IEEE80211_VIF_IGNORE_OFDMA_WIDER_BW;
695 
696 	/*
697 	 * For an MLD vif (in restart) we may not have a link; delay the call
698 	 * the initial change_vif_links.
699 	 */
700 	if (vif->type == NL80211_IFTYPE_STATION &&
701 	    !ieee80211_vif_is_mld(vif))
702 		iwl_mld_update_mac_power(mld, vif, false);
703 
704 	if (vif->type == NL80211_IFTYPE_MONITOR) {
705 		mld->monitor.on = true;
706 		ieee80211_hw_set(mld->hw, RX_INCLUDES_FCS);
707 	}
708 
709 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
710 		mld->p2p_device_vif = vif;
711 
712 	if (vif->type == NL80211_IFTYPE_NAN)
713 		mld->nan_device_vif = vif;
714 
715 	return 0;
716 
717 err:
718 	iwl_mld_rm_vif(mld, vif);
719 	return ret;
720 }
721 
722 static
723 void iwl_mld_mac80211_remove_interface(struct ieee80211_hw *hw,
724 				       struct ieee80211_vif *vif)
725 {
726 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
727 
728 	lockdep_assert_wiphy(mld->wiphy);
729 
730 	if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_STATION)
731 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
732 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
733 
734 	if (vif->type == NL80211_IFTYPE_MONITOR) {
735 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mld->hw->flags);
736 		mld->monitor.on = false;
737 	}
738 
739 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
740 		mld->p2p_device_vif = NULL;
741 
742 	if (vif->type == NL80211_IFTYPE_NAN)
743 		mld->nan_device_vif = NULL;
744 	else
745 		iwl_mld_remove_link(mld, &vif->bss_conf);
746 
747 #ifdef CONFIG_IWLWIFI_DEBUGFS
748 	debugfs_remove(iwl_mld_vif_from_mac80211(vif)->dbgfs_slink);
749 	iwl_mld_vif_from_mac80211(vif)->dbgfs_slink = NULL;
750 #endif
751 
752 	iwl_mld_rm_vif(mld, vif);
753 
754 	mld->monitor.phy.valid = false;
755 }
756 
757 static
758 int iwl_mld_mac80211_change_interface(struct ieee80211_hw *hw,
759 				      struct ieee80211_vif *vif,
760 				      enum nl80211_iftype new_type, bool p2p)
761 {
762 	enum nl80211_iftype old_type = vif->type;
763 	bool old_p2p = vif->p2p;
764 	int ret;
765 
766 	iwl_mld_mac80211_remove_interface(hw, vif);
767 
768 	/* set the new type for adding it cleanly */
769 	vif->type = new_type;
770 	vif->p2p = p2p;
771 
772 	ret = iwl_mld_mac80211_add_interface(hw, vif);
773 
774 	/* restore for mac80211, it will change it again */
775 	vif->type = old_type;
776 	vif->p2p = old_p2p;
777 
778 	return ret;
779 }
780 
781 struct iwl_mld_mc_iter_data {
782 	struct iwl_mld *mld;
783 	int port_id;
784 };
785 
786 static void iwl_mld_mc_iface_iterator(void *data, u8 *mac,
787 				      struct ieee80211_vif *vif)
788 {
789 	struct iwl_mld_mc_iter_data *mc_data = data;
790 	struct iwl_mld *mld = mc_data->mld;
791 	struct iwl_mcast_filter_cmd *cmd = mld->mcast_filter_cmd;
792 	struct iwl_host_cmd hcmd = {
793 		.id = MCAST_FILTER_CMD,
794 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
795 	};
796 	int ret, len;
797 
798 	/* If we don't have free ports, mcast frames will be dropped */
799 	if (WARN_ON_ONCE(mc_data->port_id >= MAX_PORT_ID_NUM))
800 		return;
801 
802 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc)
803 		return;
804 
805 	cmd->port_id = mc_data->port_id++;
806 	ether_addr_copy(cmd->bssid, vif->bss_conf.bssid);
807 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
808 
809 	hcmd.len[0] = len;
810 	hcmd.data[0] = cmd;
811 
812 	ret = iwl_mld_send_cmd(mld, &hcmd);
813 	if (ret)
814 		IWL_ERR(mld, "mcast filter cmd error. ret=%d\n", ret);
815 }
816 
817 void iwl_mld_recalc_multicast_filter(struct iwl_mld *mld)
818 {
819 	struct iwl_mld_mc_iter_data iter_data = {
820 		.mld = mld,
821 	};
822 
823 	if (WARN_ON_ONCE(!mld->mcast_filter_cmd))
824 		return;
825 
826 	ieee80211_iterate_active_interfaces(mld->hw,
827 					    IEEE80211_IFACE_ITER_NORMAL,
828 					    iwl_mld_mc_iface_iterator,
829 					    &iter_data);
830 }
831 
832 static u64
833 iwl_mld_mac80211_prepare_multicast(struct ieee80211_hw *hw,
834 				   struct netdev_hw_addr_list *mc_list)
835 {
836 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
837 	struct iwl_mcast_filter_cmd *cmd;
838 	struct netdev_hw_addr *addr;
839 	int addr_count = netdev_hw_addr_list_count(mc_list);
840 	bool pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES;
841 	int len;
842 
843 	if (pass_all)
844 		addr_count = 0;
845 
846 	/* len must be a multiple of 4 */
847 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
848 	cmd = kzalloc(len, GFP_ATOMIC);
849 	if (!cmd)
850 		return 0;
851 
852 	if (pass_all) {
853 		cmd->pass_all = 1;
854 		goto out;
855 	}
856 
857 	netdev_hw_addr_list_for_each(addr, mc_list) {
858 		IWL_DEBUG_MAC80211(mld, "mcast addr (%d): %pM\n",
859 				   cmd->count, addr->addr);
860 		ether_addr_copy(&cmd->addr_list[cmd->count * ETH_ALEN],
861 				addr->addr);
862 		cmd->count++;
863 	}
864 
865 out:
866 	return (u64)(unsigned long)cmd;
867 }
868 
869 static
870 void iwl_mld_mac80211_configure_filter(struct ieee80211_hw *hw,
871 				       unsigned int changed_flags,
872 				       unsigned int *total_flags,
873 				       u64 multicast)
874 {
875 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
876 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
877 
878 	/* Replace previous configuration */
879 	kfree(mld->mcast_filter_cmd);
880 	mld->mcast_filter_cmd = cmd;
881 
882 	if (!cmd)
883 		goto out;
884 
885 	if (changed_flags & FIF_ALLMULTI)
886 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
887 
888 	if (cmd->pass_all)
889 		cmd->count = 0;
890 
891 	iwl_mld_recalc_multicast_filter(mld);
892 out:
893 	*total_flags = 0;
894 }
895 
896 static
897 void iwl_mld_mac80211_wake_tx_queue(struct ieee80211_hw *hw,
898 				    struct ieee80211_txq *txq)
899 {
900 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
901 	struct iwl_mld_txq *mld_txq = iwl_mld_txq_from_mac80211(txq);
902 
903 	if (likely(mld_txq->status.allocated) || !txq->sta) {
904 		iwl_mld_tx_from_txq(mld, txq);
905 		return;
906 	}
907 
908 	/* We don't support TSPEC tids. %IEEE80211_NUM_TIDS is for mgmt */
909 	if (txq->tid != IEEE80211_NUM_TIDS && txq->tid >= IWL_MAX_TID_COUNT) {
910 		IWL_DEBUG_MAC80211(mld, "TID %d is not supported\n", txq->tid);
911 		return;
912 	}
913 
914 	/* The worker will handle any packets we leave on the txq now */
915 
916 	spin_lock_bh(&mld->add_txqs_lock);
917 	/* The list is being deleted only after the queue is fully allocated. */
918 	if (list_empty(&mld_txq->list) &&
919 	    /* recheck under lock, otherwise it can be added twice */
920 	    !mld_txq->status.allocated) {
921 		list_add_tail(&mld_txq->list, &mld->txqs_to_add);
922 		wiphy_work_queue(mld->wiphy, &mld->add_txqs_wk);
923 	}
924 	spin_unlock_bh(&mld->add_txqs_lock);
925 }
926 
927 static void iwl_mld_teardown_tdls_peers(struct iwl_mld *mld)
928 {
929 	lockdep_assert_wiphy(mld->wiphy);
930 
931 	for (int i = 0; i < mld->fw->ucode_capa.num_stations; i++) {
932 		struct ieee80211_link_sta *link_sta;
933 		struct iwl_mld_sta *mld_sta;
934 
935 		link_sta = wiphy_dereference(mld->wiphy,
936 					     mld->fw_id_to_link_sta[i]);
937 		if (IS_ERR_OR_NULL(link_sta))
938 			continue;
939 
940 		if (!link_sta->sta->tdls)
941 			continue;
942 
943 		mld_sta = iwl_mld_sta_from_mac80211(link_sta->sta);
944 
945 		ieee80211_tdls_oper_request(mld_sta->vif, link_sta->addr,
946 					    NL80211_TDLS_TEARDOWN,
947 					    WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED,
948 					    GFP_KERNEL);
949 	}
950 }
951 
952 static
953 int iwl_mld_add_chanctx(struct ieee80211_hw *hw,
954 			struct ieee80211_chanctx_conf *ctx)
955 {
956 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
957 	struct iwl_mld_phy *phy = iwl_mld_phy_from_mac80211(ctx);
958 	int fw_id = iwl_mld_allocate_fw_phy_id(mld);
959 	int ret;
960 
961 	if (fw_id < 0)
962 		return fw_id;
963 
964 	phy->mld = mld;
965 	phy->fw_id = fw_id;
966 	phy->chandef = *iwl_mld_get_chandef_from_chanctx(mld, ctx);
967 
968 	ret = iwl_mld_phy_fw_action(mld, ctx, FW_CTXT_ACTION_ADD);
969 	if (ret) {
970 		mld->used_phy_ids &= ~BIT(phy->fw_id);
971 		return ret;
972 	}
973 
974 	if (hweight8(mld->used_phy_ids) > 1)
975 		iwl_mld_teardown_tdls_peers(mld);
976 
977 	return 0;
978 }
979 
980 static
981 void iwl_mld_remove_chanctx(struct ieee80211_hw *hw,
982 			    struct ieee80211_chanctx_conf *ctx)
983 {
984 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
985 	struct iwl_mld_phy *phy = iwl_mld_phy_from_mac80211(ctx);
986 
987 	iwl_mld_phy_fw_action(mld, ctx, FW_CTXT_ACTION_REMOVE);
988 	mld->used_phy_ids &= ~BIT(phy->fw_id);
989 }
990 
991 static
992 void iwl_mld_change_chanctx(struct ieee80211_hw *hw,
993 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
994 {
995 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
996 	struct iwl_mld_phy *phy = iwl_mld_phy_from_mac80211(ctx);
997 	struct cfg80211_chan_def *chandef =
998 		iwl_mld_get_chandef_from_chanctx(mld, ctx);
999 
1000 	/* We don't care about these */
1001 	if (!(changed & ~(IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
1002 			  IEEE80211_CHANCTX_CHANGE_RADAR |
1003 			  IEEE80211_CHANCTX_CHANGE_CHANNEL)))
1004 		return;
1005 
1006 	/* Check if a FW update is required */
1007 
1008 	if (changed & IEEE80211_CHANCTX_CHANGE_AP)
1009 		goto update;
1010 
1011 	if (chandef->chan == phy->chandef.chan &&
1012 	    chandef->center_freq1 == phy->chandef.center_freq1 &&
1013 	    chandef->punctured == phy->chandef.punctured) {
1014 		/* Check if we are toggling between HT and non-HT, no-op */
1015 		if (phy->chandef.width == chandef->width ||
1016 		    (phy->chandef.width <= NL80211_CHAN_WIDTH_20 &&
1017 		     chandef->width <= NL80211_CHAN_WIDTH_20))
1018 			return;
1019 	}
1020 update:
1021 
1022 	iwl_mld_update_phy_chandef(mld, ctx);
1023 }
1024 
1025 static u8
1026 iwl_mld_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
1027 {
1028 	int data_start;
1029 	int control_start;
1030 	int bw;
1031 
1032 	if (chandef->width == NL80211_CHAN_WIDTH_320)
1033 		bw = 320;
1034 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
1035 		bw = 160;
1036 	else
1037 		return 0;
1038 
1039 	/* data is bw wide so the start is half the width */
1040 	data_start = chandef->center_freq1 - bw / 2;
1041 	/* control is 20Mhz width */
1042 	control_start = chandef->chan->center_freq - 10;
1043 
1044 	return (control_start - data_start) / 80;
1045 }
1046 
1047 static bool iwl_mld_can_activate_link(struct iwl_mld *mld,
1048 				      struct ieee80211_vif *vif,
1049 				      struct ieee80211_bss_conf *link)
1050 {
1051 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1052 	struct iwl_mld_sta *mld_sta;
1053 	struct iwl_mld_link_sta *link_sta;
1054 
1055 	/* In association, we activate the assoc link before adding the STA. */
1056 	if (!mld_vif->ap_sta || !vif->cfg.assoc)
1057 		return true;
1058 
1059 	mld_sta = iwl_mld_sta_from_mac80211(mld_vif->ap_sta);
1060 
1061 	/* When switching links, we need to wait with the activation until the
1062 	 * STA was added to the FW. It'll be activated in
1063 	 * iwl_mld_update_link_stas
1064 	 */
1065 	link_sta = wiphy_dereference(mld->wiphy, mld_sta->link[link->link_id]);
1066 
1067 	/* In restart we can have a link_sta that doesn't exist in FW yet */
1068 	return link_sta && link_sta->in_fw;
1069 }
1070 
1071 static
1072 int iwl_mld_assign_vif_chanctx(struct ieee80211_hw *hw,
1073 			       struct ieee80211_vif *vif,
1074 			       struct ieee80211_bss_conf *link,
1075 			       struct ieee80211_chanctx_conf *ctx)
1076 {
1077 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1078 	struct iwl_mld_link *mld_link = iwl_mld_link_from_mac80211(link);
1079 	struct iwl_mld_link *temp_mld_link;
1080 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1081 	u16 final_active_links = 0;
1082 	int ret;
1083 
1084 	lockdep_assert_wiphy(mld->wiphy);
1085 
1086 	if (WARN_ON(!mld_link))
1087 		return -EINVAL;
1088 
1089 	if (!rcu_access_pointer(mld_link->chan_ctx)) {
1090 		/* Track addition of non-BSS link */
1091 		if (ieee80211_vif_type_p2p(vif) != NL80211_IFTYPE_STATION) {
1092 			ret = iwl_mld_emlsr_check_non_bss_block(mld, 1);
1093 			if (ret)
1094 				return ret;
1095 		}
1096 	}
1097 
1098 	/* for AP, mac parameters such as HE support are updated at this stage. */
1099 	if (vif->type == NL80211_IFTYPE_AP) {
1100 		ret = iwl_mld_mac_fw_action(mld, vif, FW_CTXT_ACTION_MODIFY);
1101 
1102 		if (ret) {
1103 			IWL_ERR(mld, "failed to update MAC %pM\n", vif->addr);
1104 			return -EINVAL;
1105 		}
1106 	}
1107 
1108 	rcu_assign_pointer(mld_link->chan_ctx, ctx);
1109 
1110 	/* We cannot rely on vif->active_links at this stage as it contains
1111 	 * both the removed links and the newly added links.
1112 	 * Therefore, we create our own bitmap of the final active links,
1113 	 * which does not include the removed links.
1114 	 */
1115 	for_each_mld_vif_valid_link(mld_vif, temp_mld_link) {
1116 		if (rcu_access_pointer(temp_mld_link->chan_ctx))
1117 			final_active_links |= BIT(link_id);
1118 	}
1119 
1120 	if (hweight16(final_active_links) > 1) {
1121 		/* Indicate to mac80211 that EML is enabled */
1122 		vif->driver_flags |= IEEE80211_VIF_EML_ACTIVE;
1123 		mld_vif->emlsr.last_entry_ts = jiffies;
1124 
1125 		if (final_active_links == mld_vif->emlsr.selected_links)
1126 			mld_vif->emlsr.primary = mld_vif->emlsr.selected_primary;
1127 		else
1128 			mld_vif->emlsr.primary = __ffs(final_active_links);
1129 
1130 		iwl_dbg_tlv_time_point(&mld->fwrt, IWL_FW_INI_TIME_ESR_LINK_UP,
1131 				       NULL);
1132 	}
1133 
1134 	/* First send the link command with the phy context ID.
1135 	 * Now that we have the phy, we know the band so also the rates
1136 	 */
1137 	ret = iwl_mld_change_link_in_fw(mld, link,
1138 					LINK_CONTEXT_MODIFY_RATES_INFO);
1139 	if (ret)
1140 		goto err;
1141 
1142 	/* TODO: Initialize rate control for the AP station, since we might be
1143 	 * doing a link switch here - we cannot initialize it before since
1144 	 * this needs the phy context assigned (and in FW?), and we cannot
1145 	 * do it later because it needs to be initialized as soon as we're
1146 	 * able to TX on the link, i.e. when active. (task=link-switch)
1147 	 */
1148 
1149 	/* Now activate the link */
1150 	if (iwl_mld_can_activate_link(mld, vif, link)) {
1151 		iwl_mld_tlc_update_phy(mld, vif, link);
1152 
1153 		/* FW requires AP_TX_POWER_CONSTRAINTS_CMD before link
1154 		 * activation for AP and after link activation for STA,
1155 		 * for an unknown reason.
1156 		 */
1157 		if (vif->type == NL80211_IFTYPE_AP)
1158 			iwl_mld_send_ap_tx_power_constraint_cmd(mld, vif, link);
1159 
1160 		ret = iwl_mld_activate_link(mld, link);
1161 		if (ret)
1162 			goto err;
1163 	}
1164 
1165 	if (vif->type == NL80211_IFTYPE_STATION)
1166 		iwl_mld_send_ap_tx_power_constraint_cmd(mld, vif, link);
1167 
1168 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1169 		ret = iwl_mld_add_mon_sta(mld, vif, link);
1170 		if (ret)
1171 			goto deactivate_link;
1172 
1173 		mld->monitor.p80 =
1174 			iwl_mld_chandef_get_primary_80(&vif->bss_conf.chanreq.oper);
1175 	}
1176 
1177 	return 0;
1178 
1179 deactivate_link:
1180 	if (mld_link->active)
1181 		iwl_mld_deactivate_link(mld, link);
1182 err:
1183 	RCU_INIT_POINTER(mld_link->chan_ctx, NULL);
1184 	return ret;
1185 }
1186 
1187 static
1188 void iwl_mld_unassign_vif_chanctx(struct ieee80211_hw *hw,
1189 				  struct ieee80211_vif *vif,
1190 				  struct ieee80211_bss_conf *link,
1191 				  struct ieee80211_chanctx_conf *ctx)
1192 {
1193 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1194 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1195 	struct iwl_mld_link *mld_link = iwl_mld_link_from_mac80211(link);
1196 	unsigned int n_active = iwl_mld_count_active_links(mld, vif);
1197 
1198 	if (WARN_ON(!mld_link))
1199 		return;
1200 
1201 	/* Track removal of non-BSS link */
1202 	if (ieee80211_vif_type_p2p(vif) != NL80211_IFTYPE_STATION)
1203 		iwl_mld_emlsr_check_non_bss_block(mld, -1);
1204 
1205 	iwl_mld_deactivate_link(mld, link);
1206 
1207 	if (vif->type == NL80211_IFTYPE_MONITOR)
1208 		iwl_mld_remove_mon_sta(mld, vif, link);
1209 
1210 	if (n_active > 1) {
1211 		/* Indicate to mac80211 that EML is disabled */
1212 		vif->driver_flags &= ~IEEE80211_VIF_EML_ACTIVE;
1213 
1214 		iwl_dbg_tlv_time_point(&mld->fwrt,
1215 				       IWL_FW_INI_TIME_ESR_LINK_DOWN,
1216 				       NULL);
1217 	}
1218 
1219 	RCU_INIT_POINTER(mld_link->chan_ctx, NULL);
1220 
1221 	iwl_mld_tlc_update_phy(mld, vif, link);
1222 
1223 	/* in the non-MLO case, remove/re-add the link to clean up FW state.
1224 	 * In MLO, it'll be done in drv_change_vif_link
1225 	 */
1226 	if (!ieee80211_vif_is_mld(vif) && !mld_vif->ap_sta &&
1227 	    !WARN_ON_ONCE(vif->cfg.assoc) &&
1228 	    vif->type != NL80211_IFTYPE_AP && !mld->fw_status.in_hw_restart) {
1229 		iwl_mld_remove_link(mld, link);
1230 		iwl_mld_add_link(mld, link);
1231 	}
1232 }
1233 
1234 static
1235 int iwl_mld_mac80211_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
1236 				       u32 value)
1237 {
1238 	return 0;
1239 }
1240 
1241 static void
1242 iwl_mld_link_info_changed_ap_ibss(struct iwl_mld *mld,
1243 				  struct ieee80211_vif *vif,
1244 				  struct ieee80211_bss_conf *link,
1245 				  u64 changes)
1246 {
1247 	u32 link_changes = 0;
1248 
1249 	if (changes & BSS_CHANGED_ERP_SLOT)
1250 		link_changes |= LINK_CONTEXT_MODIFY_RATES_INFO;
1251 
1252 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT))
1253 		link_changes |= LINK_CONTEXT_MODIFY_PROTECT_FLAGS;
1254 
1255 	if (changes & (BSS_CHANGED_QOS | BSS_CHANGED_BANDWIDTH))
1256 		link_changes |= LINK_CONTEXT_MODIFY_QOS_PARAMS;
1257 
1258 	if (changes & BSS_CHANGED_HE_BSS_COLOR)
1259 		link_changes |= LINK_CONTEXT_MODIFY_HE_PARAMS;
1260 
1261 	if (link_changes)
1262 		iwl_mld_change_link_in_fw(mld, link, link_changes);
1263 
1264 	if (changes & BSS_CHANGED_BEACON)
1265 		iwl_mld_update_beacon_template(mld, vif, link);
1266 }
1267 
1268 static
1269 u32 iwl_mld_link_changed_mapping(struct iwl_mld *mld,
1270 				 struct ieee80211_vif *vif,
1271 				 struct ieee80211_bss_conf *link_conf,
1272 				 u64 changes)
1273 {
1274 	u32 link_changes = 0;
1275 	bool has_he, has_eht;
1276 
1277 	if (changes & BSS_CHANGED_QOS && vif->cfg.assoc && link_conf->qos)
1278 		link_changes |= LINK_CONTEXT_MODIFY_QOS_PARAMS;
1279 
1280 	if (changes & (BSS_CHANGED_ERP_PREAMBLE | BSS_CHANGED_BASIC_RATES |
1281 		       BSS_CHANGED_ERP_SLOT))
1282 		link_changes |= LINK_CONTEXT_MODIFY_RATES_INFO;
1283 
1284 	if (changes & (BSS_CHANGED_HT | BSS_CHANGED_ERP_CTS_PROT))
1285 		link_changes |= LINK_CONTEXT_MODIFY_PROTECT_FLAGS;
1286 
1287 	/* TODO: task=MLO check mac80211's HE flags and if command is needed
1288 	 * every time there's a link change. Currently used flags are
1289 	 * BSS_CHANGED_HE_OBSS_PD and BSS_CHANGED_HE_BSS_COLOR.
1290 	 */
1291 	has_he = link_conf->he_support && !iwlwifi_mod_params.disable_11ax;
1292 	has_eht = link_conf->eht_support && !iwlwifi_mod_params.disable_11be;
1293 
1294 	if (vif->cfg.assoc && (has_he || has_eht)) {
1295 		IWL_DEBUG_MAC80211(mld, "Associated in HE mode\n");
1296 		link_changes |= LINK_CONTEXT_MODIFY_HE_PARAMS;
1297 	}
1298 
1299 	return link_changes;
1300 }
1301 
1302 static void
1303 iwl_mld_mac80211_link_info_changed_sta(struct iwl_mld *mld,
1304 				       struct ieee80211_vif *vif,
1305 				       struct ieee80211_bss_conf *link_conf,
1306 				       u64 changes)
1307 {
1308 	u32 link_changes = iwl_mld_link_changed_mapping(mld, vif, link_conf,
1309 							changes);
1310 
1311 	if (link_changes)
1312 		iwl_mld_change_link_in_fw(mld, link_conf, link_changes);
1313 
1314 	if (changes & BSS_CHANGED_TPE)
1315 		iwl_mld_send_ap_tx_power_constraint_cmd(mld, vif, link_conf);
1316 
1317 	if (changes & BSS_CHANGED_BEACON_INFO)
1318 		iwl_mld_update_mac_power(mld, vif, false);
1319 
1320 	/* The firmware will wait quite a while after association before it
1321 	 * starts filtering the beacons. We can safely enable beacon filtering
1322 	 * upon CQM configuration, even if we didn't get a beacon yet.
1323 	 */
1324 	if (changes & (BSS_CHANGED_CQM | BSS_CHANGED_BEACON_INFO))
1325 		iwl_mld_enable_beacon_filter(mld, link_conf, false);
1326 
1327 	if (changes & BSS_CHANGED_BANDWIDTH)
1328 		iwl_mld_retry_emlsr(mld, vif);
1329 }
1330 
1331 static int iwl_mld_update_mu_groups(struct iwl_mld *mld,
1332 				    struct ieee80211_bss_conf *link_conf)
1333 {
1334 	struct iwl_mu_group_mgmt_cmd cmd = {};
1335 
1336 	BUILD_BUG_ON(sizeof(cmd.membership_status) !=
1337 		     sizeof(link_conf->mu_group.membership));
1338 	BUILD_BUG_ON(sizeof(cmd.user_position) !=
1339 		     sizeof(link_conf->mu_group.position));
1340 
1341 	memcpy(cmd.membership_status, link_conf->mu_group.membership,
1342 	       WLAN_MEMBERSHIP_LEN);
1343 	memcpy(cmd.user_position, link_conf->mu_group.position,
1344 	       WLAN_USER_POSITION_LEN);
1345 
1346 	return iwl_mld_send_cmd_pdu(mld,
1347 				    WIDE_ID(DATA_PATH_GROUP,
1348 					    UPDATE_MU_GROUPS_CMD),
1349 				    &cmd);
1350 }
1351 
1352 static void
1353 iwl_mld_mac80211_link_info_changed(struct ieee80211_hw *hw,
1354 				   struct ieee80211_vif *vif,
1355 				   struct ieee80211_bss_conf *link_conf,
1356 				   u64 changes)
1357 {
1358 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1359 
1360 	switch (vif->type) {
1361 	case NL80211_IFTYPE_STATION:
1362 		iwl_mld_mac80211_link_info_changed_sta(mld, vif, link_conf,
1363 						       changes);
1364 		break;
1365 	case NL80211_IFTYPE_AP:
1366 	case NL80211_IFTYPE_ADHOC:
1367 		iwl_mld_link_info_changed_ap_ibss(mld, vif, link_conf,
1368 						  changes);
1369 		break;
1370 	case NL80211_IFTYPE_MONITOR:
1371 		/* The firmware tracks this on its own in STATION mode, but
1372 		 * obviously not in sniffer mode.
1373 		 */
1374 		if (changes & BSS_CHANGED_MU_GROUPS)
1375 			iwl_mld_update_mu_groups(mld, link_conf);
1376 		break;
1377 	default:
1378 		/* shouldn't happen */
1379 		WARN_ON_ONCE(1);
1380 	}
1381 
1382 	/* We now know our BSSID, we can configure the MAC context with
1383 	 * eht_support if needed.
1384 	 */
1385 	if (changes & BSS_CHANGED_BSSID)
1386 		iwl_mld_mac_fw_action(mld, vif, FW_CTXT_ACTION_MODIFY);
1387 
1388 	if (changes & BSS_CHANGED_TXPOWER)
1389 		iwl_mld_set_tx_power(mld, link_conf, link_conf->txpower);
1390 }
1391 
1392 static void
1393 iwl_mld_smps_workaround(struct iwl_mld *mld, struct ieee80211_vif *vif, bool enable)
1394 {
1395 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1396 	bool workaround_required =
1397 		iwl_fw_lookup_cmd_ver(mld->fw, MAC_PM_POWER_TABLE, 0) < 2;
1398 
1399 	if (!workaround_required)
1400 		return;
1401 
1402 	/* Send the device-level power commands since the
1403 	 * firmware checks the POWER_TABLE_CMD's POWER_SAVE_EN bit to
1404 	 * determine SMPS mode.
1405 	 */
1406 	if (mld_vif->ps_disabled == !enable)
1407 		return;
1408 
1409 	mld_vif->ps_disabled = !enable;
1410 
1411 	iwl_mld_update_device_power(mld, false);
1412 }
1413 
1414 static
1415 void iwl_mld_mac80211_vif_cfg_changed(struct ieee80211_hw *hw,
1416 				      struct ieee80211_vif *vif,
1417 				      u64 changes)
1418 {
1419 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1420 	int ret;
1421 
1422 	lockdep_assert_wiphy(mld->wiphy);
1423 
1424 	if (vif->type != NL80211_IFTYPE_STATION)
1425 		return;
1426 
1427 	if (changes & BSS_CHANGED_ASSOC) {
1428 		ret = iwl_mld_mac_fw_action(mld, vif, FW_CTXT_ACTION_MODIFY);
1429 		if (ret)
1430 			IWL_ERR(mld, "failed to update context\n");
1431 
1432 		if (vif->cfg.assoc) {
1433 			/* Clear statistics to get clean beacon counter, and
1434 			 * ask for periodic statistics, as they are needed for
1435 			 * link selection and RX OMI decisions.
1436 			 */
1437 			iwl_mld_clear_stats_in_fw(mld);
1438 			iwl_mld_request_periodic_fw_stats(mld, true);
1439 
1440 			iwl_mld_set_vif_associated(mld, vif);
1441 		} else {
1442 			iwl_mld_request_periodic_fw_stats(mld, false);
1443 		}
1444 	}
1445 
1446 	if (changes & BSS_CHANGED_PS) {
1447 		iwl_mld_smps_workaround(mld, vif, vif->cfg.ps);
1448 		iwl_mld_update_mac_power(mld, vif, false);
1449 	}
1450 
1451 	/* TODO: task=MLO BSS_CHANGED_MLD_VALID_LINKS/CHANGED_MLD_TTLM */
1452 }
1453 
1454 static int
1455 iwl_mld_mac80211_hw_scan(struct ieee80211_hw *hw,
1456 			 struct ieee80211_vif *vif,
1457 			 struct ieee80211_scan_request *hw_req)
1458 {
1459 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1460 	int ret;
1461 
1462 	if (WARN_ON(!hw_req->req.n_channels ||
1463 		    hw_req->req.n_channels >
1464 		    mld->fw->ucode_capa.n_scan_channels))
1465 		return -EINVAL;
1466 
1467 	ret = iwl_mld_regular_scan_start(mld, vif, &hw_req->req, &hw_req->ies);
1468 	if (!ret) {
1469 		/* We will be busy with scanning, so the counters may not reflect the
1470 		 * reality. Stop checking the counters until the scan ends
1471 		 */
1472 		iwl_mld_start_ignoring_tpt_updates(mld);
1473 	}
1474 
1475 	return ret;
1476 }
1477 
1478 static void
1479 iwl_mld_mac80211_cancel_hw_scan(struct ieee80211_hw *hw,
1480 				struct ieee80211_vif *vif)
1481 {
1482 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1483 
1484 	/* Due to a race condition, it's possible that mac80211 asks
1485 	 * us to stop a hw_scan when it's already stopped. This can
1486 	 * happen, for instance, if we stopped the scan ourselves,
1487 	 * called ieee80211_scan_completed() and the userspace called
1488 	 * cancel scan before ieee80211_scan_work() could run.
1489 	 * To handle that, simply return if the scan is not running.
1490 	 */
1491 	if (mld->scan.status & IWL_MLD_SCAN_REGULAR) {
1492 		iwl_mld_scan_stop(mld, IWL_MLD_SCAN_REGULAR, true);
1493 		/* Scan is over, we can check again the tpt counters */
1494 		iwl_mld_stop_ignoring_tpt_updates(mld);
1495 	}
1496 }
1497 
1498 static int
1499 iwl_mld_mac80211_sched_scan_start(struct ieee80211_hw *hw,
1500 				  struct ieee80211_vif *vif,
1501 				  struct cfg80211_sched_scan_request *req,
1502 				  struct ieee80211_scan_ies *ies)
1503 {
1504 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1505 
1506 	return iwl_mld_sched_scan_start(mld, vif, req, ies, IWL_MLD_SCAN_SCHED);
1507 }
1508 
1509 static int
1510 iwl_mld_mac80211_sched_scan_stop(struct ieee80211_hw *hw,
1511 				 struct ieee80211_vif *vif)
1512 {
1513 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1514 
1515 	/* Due to a race condition, it's possible that mac80211 asks
1516 	 * us to stop a sched_scan when it's already stopped. This
1517 	 * can happen, for instance, if we stopped the scan ourselves,
1518 	 * called ieee80211_sched_scan_stopped() and the userspace called
1519 	 * stop sched scan before ieee80211_sched_scan_stopped_work()
1520 	 * could run. To handle this, simply return if the scan is
1521 	 * not running.
1522 	 */
1523 	if (!(mld->scan.status & IWL_MLD_SCAN_SCHED))
1524 		return 0;
1525 
1526 	return iwl_mld_scan_stop(mld, IWL_MLD_SCAN_SCHED, false);
1527 }
1528 
1529 static void
1530 iwl_mld_mac80211_reconfig_complete(struct ieee80211_hw *hw,
1531 				   enum ieee80211_reconfig_type reconfig_type)
1532 {
1533 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1534 
1535 	switch (reconfig_type) {
1536 	case IEEE80211_RECONFIG_TYPE_RESTART:
1537 		mld->fw_status.in_hw_restart = false;
1538 		iwl_mld_send_recovery_cmd(mld, ERROR_RECOVERY_END_OF_RECOVERY);
1539 		iwl_trans_finish_sw_reset(mld->trans);
1540 		/* no need to lock, adding in parallel would schedule too */
1541 		if (!list_empty(&mld->txqs_to_add))
1542 			wiphy_work_queue(mld->wiphy, &mld->add_txqs_wk);
1543 
1544 		IWL_INFO(mld, "restart completed\n");
1545 		break;
1546 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1547 		break;
1548 	}
1549 }
1550 
1551 static
1552 void iwl_mld_mac80211_mgd_prepare_tx(struct ieee80211_hw *hw,
1553 				     struct ieee80211_vif *vif,
1554 				     struct ieee80211_prep_tx_info *info)
1555 {
1556 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1557 	u32 duration = IWL_MLD_SESSION_PROTECTION_ASSOC_TIME_MS;
1558 
1559 	/* After a successful association the connection is established
1560 	 * and we can rely on the quota to send the disassociation frame.
1561 	 */
1562 	if (info->was_assoc)
1563 		return;
1564 
1565 	if (info->duration > duration)
1566 		duration = info->duration;
1567 
1568 	iwl_mld_schedule_session_protection(mld, vif, duration,
1569 					    IWL_MLD_SESSION_PROTECTION_MIN_TIME_MS,
1570 					    info->link_id);
1571 }
1572 
1573 static
1574 void iwl_mld_mac_mgd_complete_tx(struct ieee80211_hw *hw,
1575 				 struct ieee80211_vif *vif,
1576 				 struct ieee80211_prep_tx_info *info)
1577 {
1578 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1579 
1580 	/* Successful authentication is the only case that requires to let
1581 	 * the session protection go. We'll need it for the upcoming
1582 	 * association. For all the other cases, we need to cancel the session
1583 	 * protection.
1584 	 * After successful association the connection is established and
1585 	 * further mgd tx can rely on the quota.
1586 	 */
1587 	if (info->success && info->subtype == IEEE80211_STYPE_AUTH)
1588 		return;
1589 
1590 	/* The firmware will be on medium after we configure the vif as
1591 	 * associated. Removing the session protection allows the firmware
1592 	 * to stop being on medium. In order to ensure the continuity of our
1593 	 * presence on medium, we need first to configure the vif as associated
1594 	 * and only then, remove the session protection.
1595 	 * Currently, mac80211 calls vif_cfg_changed() first and then,
1596 	 * drv_mgd_complete_tx(). Ensure that this assumption stays true by
1597 	 * a warning.
1598 	 */
1599 	WARN_ON(info->success &&
1600 		(info->subtype == IEEE80211_STYPE_ASSOC_REQ ||
1601 		 info->subtype == IEEE80211_STYPE_REASSOC_REQ) &&
1602 		!vif->cfg.assoc);
1603 
1604 	iwl_mld_cancel_session_protection(mld, vif, info->link_id);
1605 }
1606 
1607 static int
1608 iwl_mld_mac80211_conf_tx(struct ieee80211_hw *hw,
1609 			 struct ieee80211_vif *vif,
1610 			 unsigned int link_id, u16 ac,
1611 			 const struct ieee80211_tx_queue_params *params)
1612 {
1613 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1614 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1615 	struct iwl_mld_link *link;
1616 
1617 	lockdep_assert_wiphy(mld->wiphy);
1618 
1619 	if (vif->type == NL80211_IFTYPE_NAN)
1620 		return 0;
1621 
1622 	link = iwl_mld_link_dereference_check(mld_vif, link_id);
1623 	if (!link)
1624 		return -EINVAL;
1625 
1626 	link->queue_params[ac] = *params;
1627 
1628 	/* No need to update right away, we'll get BSS_CHANGED_QOS
1629 	 * The exception is P2P_DEVICE interface which needs immediate update.
1630 	 */
1631 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1632 		iwl_mld_change_link_in_fw(mld, &vif->bss_conf,
1633 					  LINK_CONTEXT_MODIFY_QOS_PARAMS);
1634 
1635 	return 0;
1636 }
1637 
1638 static void iwl_mld_set_uapsd(struct iwl_mld *mld, struct ieee80211_vif *vif)
1639 {
1640 	vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
1641 
1642 	if (vif->type != NL80211_IFTYPE_STATION)
1643 		return;
1644 
1645 	if (vif->p2p &&
1646 	    !(iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_P2P_CLIENT))
1647 		vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
1648 
1649 	if (!vif->p2p &&
1650 	    !(iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS))
1651 		vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
1652 }
1653 
1654 int iwl_mld_tdls_sta_count(struct iwl_mld *mld)
1655 {
1656 	int count = 0;
1657 
1658 	lockdep_assert_wiphy(mld->wiphy);
1659 
1660 	for (int i = 0; i < mld->fw->ucode_capa.num_stations; i++) {
1661 		struct ieee80211_link_sta *link_sta;
1662 
1663 		link_sta = wiphy_dereference(mld->wiphy,
1664 					     mld->fw_id_to_link_sta[i]);
1665 		if (IS_ERR_OR_NULL(link_sta))
1666 			continue;
1667 
1668 		if (!link_sta->sta->tdls)
1669 			continue;
1670 
1671 		count++;
1672 	}
1673 
1674 	return count;
1675 }
1676 
1677 static void iwl_mld_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
1678 						    struct cfg80211_bss *bss,
1679 						    void *_data)
1680 {
1681 	bool *tolerated = _data;
1682 	const struct cfg80211_bss_ies *ies;
1683 	const struct element *elem;
1684 
1685 	rcu_read_lock();
1686 	ies = rcu_dereference(bss->ies);
1687 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
1688 				  ies->len);
1689 
1690 	if (!elem || elem->datalen < 10 ||
1691 	    !(elem->data[10] &
1692 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
1693 		*tolerated = false;
1694 	}
1695 	rcu_read_unlock();
1696 }
1697 
1698 static void
1699 iwl_mld_check_he_obss_narrow_bw_ru(struct iwl_mld *mld,
1700 				   struct iwl_mld_link *mld_link,
1701 				   struct ieee80211_bss_conf *link_conf)
1702 {
1703 	bool tolerated = true;
1704 
1705 	if (WARN_ON_ONCE(!link_conf->chanreq.oper.chan))
1706 		return;
1707 
1708 	if (!(link_conf->chanreq.oper.chan->flags & IEEE80211_CHAN_RADAR)) {
1709 		mld_link->he_ru_2mhz_block = false;
1710 		return;
1711 	}
1712 
1713 	cfg80211_bss_iter(mld->wiphy, &link_conf->chanreq.oper,
1714 			  iwl_mld_check_he_obss_narrow_bw_ru_iter, &tolerated);
1715 
1716 	/* If there is at least one AP on radar channel that cannot
1717 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
1718 	 */
1719 	mld_link->he_ru_2mhz_block = !tolerated;
1720 }
1721 
1722 static void iwl_mld_link_set_2mhz_block(struct iwl_mld *mld,
1723 					struct ieee80211_vif *vif,
1724 					struct ieee80211_sta *sta)
1725 {
1726 	struct ieee80211_link_sta *link_sta;
1727 	unsigned int link_id;
1728 
1729 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
1730 		struct ieee80211_bss_conf *link_conf =
1731 			link_conf_dereference_protected(vif, link_id);
1732 		struct iwl_mld_link *mld_link =
1733 			iwl_mld_link_from_mac80211(link_conf);
1734 
1735 		if (WARN_ON(!link_conf || !mld_link))
1736 			continue;
1737 
1738 		if (link_sta->he_cap.has_he)
1739 			iwl_mld_check_he_obss_narrow_bw_ru(mld, mld_link,
1740 							   link_conf);
1741 	}
1742 }
1743 
1744 static int iwl_mld_move_sta_state_up(struct iwl_mld *mld,
1745 				     struct ieee80211_vif *vif,
1746 				     struct ieee80211_sta *sta,
1747 				     enum ieee80211_sta_state old_state,
1748 				     enum ieee80211_sta_state new_state)
1749 {
1750 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1751 	int tdls_count = 0;
1752 	int ret;
1753 
1754 	if (old_state == IEEE80211_STA_NOTEXIST &&
1755 	    new_state == IEEE80211_STA_NONE) {
1756 		if (sta->tdls) {
1757 			if (vif->p2p || hweight8(mld->used_phy_ids) != 1)
1758 				return -EBUSY;
1759 
1760 			tdls_count = iwl_mld_tdls_sta_count(mld);
1761 			if (tdls_count >= IWL_TDLS_STA_COUNT)
1762 				return -EBUSY;
1763 		}
1764 
1765 		ret = iwl_mld_add_sta(mld, sta, vif);
1766 		if (ret)
1767 			return ret;
1768 
1769 		/* just added first TDLS STA, so disable PM and block EMLSR */
1770 		if (sta->tdls && tdls_count == 0) {
1771 			iwl_mld_update_mac_power(mld, vif, false);
1772 
1773 			/* TDLS requires single-link operation with
1774 			 * direct peer communication.
1775 			 * Block and exit EMLSR when TDLS is established.
1776 			 */
1777 			iwl_mld_block_emlsr(mld, vif,
1778 					    IWL_MLD_EMLSR_BLOCKED_TDLS,
1779 					    iwl_mld_get_primary_link(vif));
1780 		}
1781 
1782 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
1783 			mld_vif->ap_sta = sta;
1784 
1785 		/* Initialize TLC here already - this really tells
1786 		 * the firmware only what the supported legacy rates are
1787 		 * (may be) since it's initialized already from what the
1788 		 * AP advertised in the beacon/probe response. This will
1789 		 * allow the firmware to send auth/assoc frames with one
1790 		 * of the supported rates already, rather than having to
1791 		 * use a mandatory rate.
1792 		 * If we're the AP, we'll just assume mandatory rates at
1793 		 * this point, but we know nothing about the STA anyway.
1794 		 */
1795 		iwl_mld_config_tlc(mld, vif, sta);
1796 
1797 		return ret;
1798 	} else if (old_state == IEEE80211_STA_NONE &&
1799 		   new_state == IEEE80211_STA_AUTH) {
1800 		iwl_mld_set_uapsd(mld, vif);
1801 		return 0;
1802 	} else if (old_state == IEEE80211_STA_AUTH &&
1803 		   new_state == IEEE80211_STA_ASSOC) {
1804 		ret = iwl_mld_update_all_link_stations(mld, sta);
1805 
1806 		if (vif->type == NL80211_IFTYPE_STATION)
1807 			iwl_mld_link_set_2mhz_block(mld, vif, sta);
1808 
1809 		if (sta->tdls) {
1810 			/*
1811 			 * update MAC since wifi generation flags may change,
1812 			 * we also update MAC on association to the AP via the
1813 			 * vif assoc change
1814 			 */
1815 			iwl_mld_mac_fw_action(mld, vif, FW_CTXT_ACTION_MODIFY);
1816 		}
1817 
1818 		/* Now the link_sta's capabilities are set, update the FW */
1819 		iwl_mld_config_tlc(mld, vif, sta);
1820 
1821 		if (vif->type == NL80211_IFTYPE_AP) {
1822 			/* Update MAC_CFG_FILTER_ACCEPT_BEACON if at least
1823 			 * one sta is associated
1824 			 */
1825 			if (++mld_vif->num_associated_stas == 1)
1826 				ret = iwl_mld_mac_fw_action(mld, vif,
1827 							    FW_CTXT_ACTION_MODIFY);
1828 		}
1829 
1830 		return ret;
1831 	} else if (old_state == IEEE80211_STA_ASSOC &&
1832 		   new_state == IEEE80211_STA_AUTHORIZED) {
1833 		ret = 0;
1834 
1835 		if (!sta->tdls) {
1836 			mld_vif->authorized = true;
1837 
1838 			/* Ensure any block due to a non-BSS link is synced */
1839 			iwl_mld_emlsr_check_non_bss_block(mld, 0);
1840 
1841 			/* Ensure NAN block is synced */
1842 			iwl_mld_emlsr_check_nan_block(mld, vif);
1843 
1844 			/* Block EMLSR until a certain throughput it reached */
1845 			if (!mld->fw_status.in_hw_restart &&
1846 			    IWL_MLD_ENTER_EMLSR_TPT_THRESH > 0)
1847 				iwl_mld_block_emlsr(mld_vif->mld, vif,
1848 						    IWL_MLD_EMLSR_BLOCKED_TPT,
1849 						    0);
1850 
1851 			/* clear COEX_HIGH_PRIORITY_ENABLE */
1852 			ret = iwl_mld_mac_fw_action(mld, vif,
1853 						    FW_CTXT_ACTION_MODIFY);
1854 			if (ret)
1855 				return ret;
1856 			iwl_mld_smps_workaround(mld, vif, vif->cfg.ps);
1857 		}
1858 
1859 		/* MFP is set by default before the station is authorized.
1860 		 * Clear it here in case it's not used.
1861 		 */
1862 		if (!sta->mfp)
1863 			ret = iwl_mld_update_all_link_stations(mld, sta);
1864 
1865 		/* We can use wide bandwidth now, not only 20 MHz */
1866 		iwl_mld_config_tlc(mld, vif, sta);
1867 
1868 		return ret;
1869 	} else {
1870 		return -EINVAL;
1871 	}
1872 }
1873 
1874 static int iwl_mld_move_sta_state_down(struct iwl_mld *mld,
1875 				       struct ieee80211_vif *vif,
1876 				       struct ieee80211_sta *sta,
1877 				       enum ieee80211_sta_state old_state,
1878 				       enum ieee80211_sta_state new_state)
1879 {
1880 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1881 
1882 	if (old_state == IEEE80211_STA_AUTHORIZED &&
1883 	    new_state == IEEE80211_STA_ASSOC) {
1884 		if (!sta->tdls) {
1885 			mld_vif->authorized = false;
1886 
1887 			memset(&mld_vif->emlsr.zeroed_on_not_authorized, 0,
1888 			       sizeof(mld_vif->emlsr.zeroed_on_not_authorized));
1889 
1890 			wiphy_delayed_work_cancel(mld->wiphy,
1891 						  &mld_vif->emlsr.prevent_done_wk);
1892 			wiphy_delayed_work_cancel(mld->wiphy,
1893 						  &mld_vif->emlsr.tmp_non_bss_done_wk);
1894 			wiphy_work_cancel(mld->wiphy, &mld_vif->emlsr.unblock_tpt_wk);
1895 			wiphy_delayed_work_cancel(mld->wiphy,
1896 						  &mld_vif->emlsr.check_tpt_wk);
1897 			wiphy_delayed_work_cancel(mld->wiphy,
1898 						  &mld_vif->mlo_scan_start_wk);
1899 
1900 			iwl_mld_reset_cca_40mhz_workaround(mld, vif);
1901 			iwl_mld_smps_workaround(mld, vif, true);
1902 		}
1903 
1904 		/* once we move into assoc state, need to update the FW to
1905 		 * stop using wide bandwidth
1906 		 */
1907 		iwl_mld_config_tlc(mld, vif, sta);
1908 	} else if (old_state == IEEE80211_STA_ASSOC &&
1909 		   new_state == IEEE80211_STA_AUTH) {
1910 		if (vif->type == NL80211_IFTYPE_AP &&
1911 		    !WARN_ON(!mld_vif->num_associated_stas)) {
1912 			/* Update MAC_CFG_FILTER_ACCEPT_BEACON if the last sta
1913 			 * is disassociating
1914 			 */
1915 			if (--mld_vif->num_associated_stas == 0)
1916 				iwl_mld_mac_fw_action(mld, vif,
1917 						      FW_CTXT_ACTION_MODIFY);
1918 		}
1919 	} else if (old_state == IEEE80211_STA_AUTH &&
1920 		   new_state == IEEE80211_STA_NONE) {
1921 		/* nothing */
1922 	} else if (old_state == IEEE80211_STA_NONE &&
1923 		   new_state == IEEE80211_STA_NOTEXIST) {
1924 		iwl_mld_remove_sta(mld, sta);
1925 
1926 		if (sta->tdls && iwl_mld_tdls_sta_count(mld) == 0) {
1927 			/* just removed last TDLS STA, so enable PM
1928 			 * and unblock EMLSR
1929 			 */
1930 			iwl_mld_update_mac_power(mld, vif, false);
1931 
1932 			/* Unblock EMLSR when TDLS connection is torn down */
1933 			iwl_mld_unblock_emlsr(mld, vif,
1934 					      IWL_MLD_EMLSR_BLOCKED_TDLS);
1935 		}
1936 
1937 		if (sta->tdls) {
1938 			/*
1939 			 * update MAC since wifi generation flags may change,
1940 			 * we also update MAC on disassociation to the AP via
1941 			 * the vif assoc change
1942 			 */
1943 			iwl_mld_mac_fw_action(mld, vif, FW_CTXT_ACTION_MODIFY);
1944 		}
1945 	} else {
1946 		return -EINVAL;
1947 	}
1948 	return 0;
1949 }
1950 
1951 static int iwl_mld_mac80211_sta_state(struct ieee80211_hw *hw,
1952 				      struct ieee80211_vif *vif,
1953 				      struct ieee80211_sta *sta,
1954 				      enum ieee80211_sta_state old_state,
1955 				      enum ieee80211_sta_state new_state)
1956 {
1957 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1958 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
1959 
1960 	IWL_DEBUG_MAC80211(mld, "station %pM state change %d->%d\n",
1961 			   sta->addr, old_state, new_state);
1962 
1963 	mld_sta->sta_state = new_state;
1964 
1965 	if (old_state < new_state)
1966 		return iwl_mld_move_sta_state_up(mld, vif, sta, old_state,
1967 						 new_state);
1968 	else
1969 		return iwl_mld_move_sta_state_down(mld, vif, sta, old_state,
1970 						   new_state);
1971 }
1972 
1973 static void iwl_mld_mac80211_flush(struct ieee80211_hw *hw,
1974 				   struct ieee80211_vif *vif,
1975 				   u32 queues, bool drop)
1976 {
1977 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1978 
1979 	/* Make sure we're done with the deferred traffic before flushing */
1980 	iwl_mld_add_txq_list(mld);
1981 
1982 	for (int i = 0; i < mld->fw->ucode_capa.num_stations; i++) {
1983 		struct ieee80211_link_sta *link_sta =
1984 			wiphy_dereference(mld->wiphy,
1985 					  mld->fw_id_to_link_sta[i]);
1986 
1987 		if (IS_ERR_OR_NULL(link_sta))
1988 			continue;
1989 
1990 		/* Check that the sta belongs to the given vif */
1991 		if (vif && vif != iwl_mld_sta_from_mac80211(link_sta->sta)->vif)
1992 			continue;
1993 
1994 		if (drop)
1995 			iwl_mld_flush_sta_txqs(mld, link_sta->sta);
1996 		else
1997 			iwl_mld_wait_sta_txqs_empty(mld, link_sta->sta);
1998 	}
1999 }
2000 
2001 static void iwl_mld_mac80211_flush_sta(struct ieee80211_hw *hw,
2002 				       struct ieee80211_vif *vif,
2003 				       struct ieee80211_sta *sta)
2004 {
2005 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2006 
2007 	iwl_mld_flush_sta_txqs(mld, sta);
2008 }
2009 
2010 static int
2011 iwl_mld_mac80211_ampdu_action(struct ieee80211_hw *hw,
2012 			      struct ieee80211_vif *vif,
2013 			      struct ieee80211_ampdu_params *params)
2014 {
2015 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2016 	struct ieee80211_sta *sta = params->sta;
2017 	enum ieee80211_ampdu_mlme_action action = params->action;
2018 	u16 tid = params->tid;
2019 	u16 ssn = params->ssn;
2020 	u16 buf_size = params->buf_size;
2021 	u16 timeout = params->timeout;
2022 	int ret;
2023 
2024 	IWL_DEBUG_HT(mld, "A-MPDU action on addr %pM tid: %d action: %d\n",
2025 		     sta->addr, tid, action);
2026 
2027 	switch (action) {
2028 	case IEEE80211_AMPDU_RX_START:
2029 		if (!iwl_enable_rx_ampdu()) {
2030 			ret = -EINVAL;
2031 			break;
2032 		}
2033 		ret = iwl_mld_ampdu_rx_start(mld, sta, tid, ssn, buf_size,
2034 					     timeout);
2035 		break;
2036 	case IEEE80211_AMPDU_RX_STOP:
2037 		ret = iwl_mld_ampdu_rx_stop(mld, sta, tid);
2038 		break;
2039 	default:
2040 		/* The mac80211 TX_AMPDU_SETUP_IN_HW flag is set for all
2041 		 * devices, since all support TX A-MPDU offload in hardware.
2042 		 * Therefore, no TX action should be requested here.
2043 		 */
2044 		WARN_ON_ONCE(1);
2045 		return -EINVAL;
2046 	}
2047 
2048 	return ret;
2049 }
2050 
2051 static bool iwl_mld_can_hw_csum(struct sk_buff *skb)
2052 {
2053 	u8 protocol = ip_hdr(skb)->protocol;
2054 
2055 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
2056 }
2057 
2058 static bool iwl_mld_mac80211_can_aggregate(struct ieee80211_hw *hw,
2059 					   struct sk_buff *head,
2060 					   struct sk_buff *skb)
2061 {
2062 	if (!IS_ENABLED(CONFIG_INET))
2063 		return false;
2064 
2065 	/* For now don't aggregate IPv6 in AMSDU */
2066 	if (skb->protocol != htons(ETH_P_IP))
2067 		return false;
2068 
2069 	/* Allow aggregation only if both frames have the same HW csum offload
2070 	 * capability, ensuring consistent HW or SW csum handling in A-MSDU.
2071 	 */
2072 	return iwl_mld_can_hw_csum(skb) == iwl_mld_can_hw_csum(head);
2073 }
2074 
2075 static void iwl_mld_mac80211_sync_rx_queues(struct ieee80211_hw *hw)
2076 {
2077 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2078 
2079 	iwl_mld_sync_rx_queues(mld, IWL_MLD_RXQ_EMPTY, NULL, 0);
2080 }
2081 
2082 static void iwl_mld_sta_rc_update(struct ieee80211_hw *hw,
2083 				  struct ieee80211_vif *vif,
2084 				  struct ieee80211_link_sta *link_sta,
2085 				  u32 changed)
2086 {
2087 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2088 
2089 	if (changed & (IEEE80211_RC_BW_CHANGED |
2090 		       IEEE80211_RC_SUPP_RATES_CHANGED |
2091 		       IEEE80211_RC_NSS_CHANGED)) {
2092 		struct ieee80211_bss_conf *link =
2093 			link_conf_dereference_check(vif, link_sta->link_id);
2094 
2095 		if (WARN_ON(!link))
2096 			return;
2097 
2098 		iwl_mld_config_tlc_link(mld, vif, link, link_sta);
2099 	}
2100 }
2101 
2102 #ifdef CONFIG_PM_SLEEP
2103 static void iwl_mld_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2104 {
2105 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2106 
2107 	device_set_wakeup_enable(mld->trans->dev, enabled);
2108 }
2109 
2110 /* Returns 0 on success. 1 if failed to suspend with wowlan:
2111  * If the circumstances didn't satisfy the conditions for suspension
2112  * with wowlan, mac80211 would use the no_wowlan flow.
2113  * If an error had occurred we update the trans status and state here
2114  * and the result will be stopping the FW.
2115  */
2116 static int
2117 iwl_mld_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
2118 {
2119 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2120 	int ret;
2121 
2122 	iwl_fw_runtime_suspend(&mld->fwrt);
2123 
2124 	ret = iwl_mld_wowlan_suspend(mld, wowlan);
2125 	if (ret)
2126 		return 1;
2127 
2128 	if (iwl_mld_no_wowlan_suspend(mld))
2129 		return 1;
2130 
2131 	return 0;
2132 }
2133 
2134 static int iwl_mld_resume(struct ieee80211_hw *hw)
2135 {
2136 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2137 	int ret;
2138 
2139 	ret = iwl_mld_wowlan_resume(mld);
2140 	if (ret)
2141 		return ret;
2142 
2143 	iwl_fw_runtime_resume(&mld->fwrt);
2144 
2145 	iwl_mld_low_latency_restart(mld);
2146 
2147 	return 0;
2148 }
2149 #endif
2150 
2151 static int iwl_mld_alloc_ptk_pn(struct iwl_mld *mld,
2152 				struct iwl_mld_sta *mld_sta,
2153 				struct ieee80211_key_conf *key,
2154 				struct iwl_mld_ptk_pn **ptk_pn)
2155 {
2156 	u8 num_rx_queues = mld->trans->info.num_rxqs;
2157 	int keyidx = key->keyidx;
2158 	struct ieee80211_key_seq seq;
2159 
2160 	if (WARN_ON(keyidx >= ARRAY_SIZE(mld_sta->ptk_pn)))
2161 		return -EINVAL;
2162 
2163 	WARN_ON(rcu_access_pointer(mld_sta->ptk_pn[keyidx]));
2164 	*ptk_pn = kzalloc_flex(**ptk_pn, q, num_rx_queues);
2165 	if (!*ptk_pn)
2166 		return -ENOMEM;
2167 
2168 	for (u8 tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
2169 		ieee80211_get_key_rx_seq(key, tid, &seq);
2170 		for (u8 q = 0; q < num_rx_queues; q++)
2171 			memcpy((*ptk_pn)->q[q].pn[tid], seq.ccmp.pn,
2172 			       IEEE80211_CCMP_PN_LEN);
2173 	}
2174 
2175 	rcu_assign_pointer(mld_sta->ptk_pn[keyidx], *ptk_pn);
2176 
2177 	return 0;
2178 }
2179 
2180 static int iwl_mld_set_key_add(struct iwl_mld *mld,
2181 			       struct ieee80211_vif *vif,
2182 			       struct ieee80211_sta *sta,
2183 			       struct ieee80211_key_conf *key)
2184 {
2185 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
2186 	struct iwl_mld_sta *mld_sta =
2187 		sta ? iwl_mld_sta_from_mac80211(sta) : NULL;
2188 	struct iwl_mld_ptk_pn *ptk_pn = NULL;
2189 	int keyidx = key->keyidx;
2190 	int ret;
2191 
2192 	/* Will be set to 0 if added successfully */
2193 	key->hw_key_idx = STA_KEY_IDX_INVALID;
2194 
2195 	switch (key->cipher) {
2196 	case WLAN_CIPHER_SUITE_WEP40:
2197 	case WLAN_CIPHER_SUITE_WEP104:
2198 		IWL_DEBUG_MAC80211(mld, "Use SW encryption for WEP\n");
2199 		return -EOPNOTSUPP;
2200 	case WLAN_CIPHER_SUITE_TKIP:
2201 		if (vif->type == NL80211_IFTYPE_STATION) {
2202 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
2203 			break;
2204 		}
2205 		IWL_DEBUG_MAC80211(mld, "Use SW encryption for TKIP\n");
2206 		return -EOPNOTSUPP;
2207 	case WLAN_CIPHER_SUITE_CCMP:
2208 	case WLAN_CIPHER_SUITE_GCMP:
2209 	case WLAN_CIPHER_SUITE_GCMP_256:
2210 	case WLAN_CIPHER_SUITE_AES_CMAC:
2211 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2212 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2213 		break;
2214 	default:
2215 		return -EOPNOTSUPP;
2216 	}
2217 
2218 	if (keyidx == 6 || keyidx == 7)
2219 		iwl_mld_track_bigtk(mld, vif, key, true);
2220 
2221 	/* After exiting from RFKILL, hostapd configures GTK/ITGK before the
2222 	 * AP is started, but those keys can't be sent to the FW before the
2223 	 * MCAST/BCAST STAs are added to it (which happens upon AP start).
2224 	 * Store it here to be sent later when the AP is started.
2225 	 */
2226 	if ((vif->type == NL80211_IFTYPE_ADHOC ||
2227 	     vif->type == NL80211_IFTYPE_AP) && !sta &&
2228 	     !mld_vif->ap_ibss_active)
2229 		return iwl_mld_store_ap_early_key(mld, key, mld_vif);
2230 
2231 	if (!mld->fw_status.in_hw_restart && mld_sta &&
2232 	    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
2233 	    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
2234 	     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
2235 	     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
2236 		ret = iwl_mld_alloc_ptk_pn(mld, mld_sta, key, &ptk_pn);
2237 		if (ret)
2238 			return ret;
2239 	}
2240 
2241 	IWL_DEBUG_MAC80211(mld, "set hwcrypto key (sta:%pM, id:%d)\n",
2242 			   sta ? sta->addr : NULL, keyidx);
2243 
2244 	ret = iwl_mld_add_key(mld, vif, sta, key);
2245 	if (ret) {
2246 		IWL_WARN(mld, "set key failed (%d)\n", ret);
2247 		if (ptk_pn) {
2248 			RCU_INIT_POINTER(mld_sta->ptk_pn[keyidx], NULL);
2249 			kfree(ptk_pn);
2250 		}
2251 
2252 		return -EOPNOTSUPP;
2253 	}
2254 
2255 	return 0;
2256 }
2257 
2258 static void iwl_mld_set_key_remove(struct iwl_mld *mld,
2259 				   struct ieee80211_vif *vif,
2260 				   struct ieee80211_sta *sta,
2261 				   struct ieee80211_key_conf *key)
2262 {
2263 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
2264 	struct iwl_mld_sta *mld_sta =
2265 		sta ? iwl_mld_sta_from_mac80211(sta) : NULL;
2266 	int keyidx = key->keyidx;
2267 
2268 	if (keyidx == 6 || keyidx == 7)
2269 		iwl_mld_track_bigtk(mld, vif, key, false);
2270 
2271 	if (mld_sta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
2272 	    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
2273 	     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
2274 	     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
2275 		struct iwl_mld_ptk_pn *ptk_pn;
2276 
2277 		if (WARN_ON(keyidx >= ARRAY_SIZE(mld_sta->ptk_pn)))
2278 			return;
2279 
2280 		ptk_pn = wiphy_dereference(mld->wiphy,
2281 					   mld_sta->ptk_pn[keyidx]);
2282 		RCU_INIT_POINTER(mld_sta->ptk_pn[keyidx], NULL);
2283 		if (!WARN_ON(!ptk_pn))
2284 			kfree_rcu(ptk_pn, rcu_head);
2285 	}
2286 
2287 	/* if this key was stored to be added later to the FW - free it here */
2288 	if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
2289 		iwl_mld_free_ap_early_key(mld, key, mld_vif);
2290 
2291 	/* We already removed it */
2292 	if (key->hw_key_idx == STA_KEY_IDX_INVALID)
2293 		return;
2294 
2295 	IWL_DEBUG_MAC80211(mld, "disable hwcrypto key\n");
2296 
2297 	iwl_mld_remove_key(mld, vif, sta, key);
2298 }
2299 
2300 static int iwl_mld_mac80211_set_key(struct ieee80211_hw *hw,
2301 				    enum set_key_cmd cmd,
2302 				    struct ieee80211_vif *vif,
2303 				    struct ieee80211_sta *sta,
2304 				    struct ieee80211_key_conf *key)
2305 {
2306 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2307 	int ret;
2308 
2309 	switch (cmd) {
2310 	case SET_KEY:
2311 		ret = iwl_mld_set_key_add(mld, vif, sta, key);
2312 		if (ret)
2313 			ret = -EOPNOTSUPP;
2314 		break;
2315 	case DISABLE_KEY:
2316 		iwl_mld_set_key_remove(mld, vif, sta, key);
2317 		ret = 0;
2318 		break;
2319 	default:
2320 		ret = -EINVAL;
2321 		break;
2322 	}
2323 
2324 	return ret;
2325 }
2326 
2327 static int
2328 iwl_mld_pre_channel_switch(struct ieee80211_hw *hw,
2329 			   struct ieee80211_vif *vif,
2330 			   struct ieee80211_channel_switch *chsw)
2331 {
2332 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2333 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
2334 	struct iwl_mld_link *mld_link =
2335 		iwl_mld_link_dereference_check(mld_vif, chsw->link_id);
2336 	u8 primary;
2337 	int selected;
2338 
2339 	lockdep_assert_wiphy(mld->wiphy);
2340 
2341 	if (WARN_ON(!mld_link))
2342 		return -EINVAL;
2343 
2344 	IWL_DEBUG_MAC80211(mld, "pre CSA to freq %d\n",
2345 			   chsw->chandef.center_freq1);
2346 
2347 	if (!iwl_mld_emlsr_active(vif))
2348 		return 0;
2349 
2350 	primary = iwl_mld_get_primary_link(vif);
2351 
2352 	/* stay on the primary link unless it is undergoing a CSA with quiet */
2353 	if (chsw->link_id == primary && chsw->block_tx)
2354 		selected = iwl_mld_get_other_link(vif, primary);
2355 	else
2356 		selected = primary;
2357 
2358 	/* Remember to tell the firmware that this link can't tx
2359 	 * Note that this logic seems to be unrelated to emlsr, but it
2360 	 * really is needed only when emlsr is active. When we have a
2361 	 * single link, the firmware will handle all this on its own.
2362 	 * In multi-link scenarios, we can learn about the CSA from
2363 	 * another link and this logic is too complex for the firmware
2364 	 * to track.
2365 	 * Since we want to de-activate the link that got a CSA with mode=1,
2366 	 * we need to tell the firmware not to send any frame on that link
2367 	 * as the firmware may not be aware that link is under a CSA
2368 	 * with mode=1 (no Tx allowed).
2369 	 */
2370 	mld_link->silent_deactivation = chsw->block_tx;
2371 	iwl_mld_exit_emlsr(mld, vif, IWL_MLD_EMLSR_EXIT_CSA, selected);
2372 
2373 	return 0;
2374 }
2375 
2376 static void
2377 iwl_mld_channel_switch(struct ieee80211_hw *hw,
2378 		       struct ieee80211_vif *vif,
2379 		       struct ieee80211_channel_switch *chsw)
2380 {
2381 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2382 
2383 	/* By implementing this operation, we prevent mac80211 from
2384 	 * starting its own channel switch timer, so that we can call
2385 	 * ieee80211_chswitch_done() ourselves at the right time
2386 	 * (Upon receiving the channel_switch_start notification from the fw)
2387 	 */
2388 	IWL_DEBUG_MAC80211(mld,
2389 			   "dummy channel switch op\n");
2390 }
2391 
2392 static int
2393 iwl_mld_post_channel_switch(struct ieee80211_hw *hw,
2394 			    struct ieee80211_vif *vif,
2395 			    struct ieee80211_bss_conf *link_conf)
2396 {
2397 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2398 	struct iwl_mld_link *mld_link = iwl_mld_link_from_mac80211(link_conf);
2399 
2400 	lockdep_assert_wiphy(mld->wiphy);
2401 
2402 	WARN_ON(mld_link->silent_deactivation);
2403 
2404 	return 0;
2405 }
2406 
2407 static void
2408 iwl_mld_abort_channel_switch(struct ieee80211_hw *hw,
2409 			     struct ieee80211_vif *vif,
2410 			     struct ieee80211_bss_conf *link_conf)
2411 {
2412 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2413 	struct iwl_mld_link *mld_link = iwl_mld_link_from_mac80211(link_conf);
2414 
2415 	IWL_DEBUG_MAC80211(mld,
2416 			   "abort channel switch op\n");
2417 	mld_link->silent_deactivation = false;
2418 }
2419 
2420 static int
2421 iwl_mld_switch_vif_chanctx_swap(struct ieee80211_hw *hw,
2422 				struct ieee80211_vif_chanctx_switch *vifs)
2423 {
2424 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2425 	int ret;
2426 
2427 	iwl_mld_unassign_vif_chanctx(hw, vifs[0].vif, vifs[0].link_conf,
2428 				     vifs[0].old_ctx);
2429 	iwl_mld_remove_chanctx(hw, vifs[0].old_ctx);
2430 
2431 	ret = iwl_mld_add_chanctx(hw, vifs[0].new_ctx);
2432 	if (ret) {
2433 		IWL_ERR(mld, "failed to add new_ctx during channel switch\n");
2434 		goto out_reassign;
2435 	}
2436 
2437 	ret = iwl_mld_assign_vif_chanctx(hw, vifs[0].vif, vifs[0].link_conf,
2438 					 vifs[0].new_ctx);
2439 	if (ret) {
2440 		IWL_ERR(mld,
2441 			"failed to assign new_ctx during channel switch\n");
2442 		goto out_remove;
2443 	}
2444 
2445 	return 0;
2446 
2447  out_remove:
2448 	iwl_mld_remove_chanctx(hw, vifs[0].new_ctx);
2449  out_reassign:
2450 	if (iwl_mld_add_chanctx(hw, vifs[0].old_ctx)) {
2451 		IWL_ERR(mld, "failed to add old_ctx after failure\n");
2452 		return ret;
2453 	}
2454 
2455 	if (iwl_mld_assign_vif_chanctx(hw, vifs[0].vif, vifs[0].link_conf,
2456 				       vifs[0].old_ctx))
2457 		IWL_ERR(mld, "failed to reassign old_ctx after failure\n");
2458 
2459 	return ret;
2460 }
2461 
2462 static int
2463 iwl_mld_switch_vif_chanctx_reassign(struct ieee80211_hw *hw,
2464 				    struct ieee80211_vif_chanctx_switch *vifs)
2465 {
2466 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2467 	int ret;
2468 
2469 	iwl_mld_unassign_vif_chanctx(hw, vifs[0].vif, vifs[0].link_conf,
2470 				     vifs[0].old_ctx);
2471 	ret = iwl_mld_assign_vif_chanctx(hw, vifs[0].vif, vifs[0].link_conf,
2472 					 vifs[0].new_ctx);
2473 	if (ret) {
2474 		IWL_ERR(mld,
2475 			"failed to assign new_ctx during channel switch\n");
2476 		goto out_reassign;
2477 	}
2478 
2479 	return 0;
2480 
2481 out_reassign:
2482 	if (iwl_mld_assign_vif_chanctx(hw, vifs[0].vif, vifs[0].link_conf,
2483 				       vifs[0].old_ctx))
2484 		IWL_ERR(mld, "failed to reassign old_ctx after failure\n");
2485 
2486 	return ret;
2487 }
2488 
2489 static int
2490 iwl_mld_switch_vif_chanctx(struct ieee80211_hw *hw,
2491 			   struct ieee80211_vif_chanctx_switch *vifs,
2492 			   int n_vifs,
2493 			   enum ieee80211_chanctx_switch_mode mode)
2494 {
2495 	int ret;
2496 
2497 	/* we only support a single-vif right now */
2498 	if (n_vifs > 1)
2499 		return -EOPNOTSUPP;
2500 
2501 	switch (mode) {
2502 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
2503 		ret = iwl_mld_switch_vif_chanctx_swap(hw, vifs);
2504 		break;
2505 	case CHANCTX_SWMODE_REASSIGN_VIF:
2506 		ret = iwl_mld_switch_vif_chanctx_reassign(hw, vifs);
2507 		break;
2508 	default:
2509 		ret = -EOPNOTSUPP;
2510 		break;
2511 	}
2512 
2513 	return ret;
2514 }
2515 
2516 static void iwl_mld_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2517 				       struct ieee80211_vif *vif,
2518 				       struct ieee80211_sta *sta)
2519 {
2520 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2521 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
2522 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
2523 	struct iwl_mld_link_sta *mld_link_sta;
2524 	u8 link_id;
2525 
2526 	lockdep_assert_wiphy(mld->wiphy);
2527 
2528 	/* This is called before mac80211 does RCU synchronisation,
2529 	 * so here we already invalidate our internal RCU-protected
2530 	 * station pointer. The rest of the code will thus no longer
2531 	 * be able to find the station this way, and we don't rely
2532 	 * on further RCU synchronisation after the sta_state()
2533 	 * callback deleted the station.
2534 	 */
2535 	for_each_mld_link_sta(mld_sta, mld_link_sta, link_id)
2536 		RCU_INIT_POINTER(mld->fw_id_to_link_sta[mld_link_sta->fw_id],
2537 				 NULL);
2538 
2539 	if (sta == mld_vif->ap_sta)
2540 		mld_vif->ap_sta = NULL;
2541 }
2542 
2543 static void
2544 iwl_mld_mac80211_mgd_protect_tdls_discover(struct ieee80211_hw *hw,
2545 					   struct ieee80211_vif *vif,
2546 					   unsigned int link_id)
2547 {
2548 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2549 	struct ieee80211_bss_conf *link_conf;
2550 	u32 duration;
2551 	int ret;
2552 
2553 	link_conf = wiphy_dereference(hw->wiphy, vif->link_conf[link_id]);
2554 	if (WARN_ON_ONCE(!link_conf))
2555 		return;
2556 
2557 	/* Protect the session to hear the TDLS setup response on the channel */
2558 
2559 	duration = 2 * link_conf->dtim_period * link_conf->beacon_int;
2560 
2561 	ret = iwl_mld_start_session_protection(mld, vif, duration, duration,
2562 					       link_id, HZ / 5);
2563 	if (ret)
2564 		IWL_ERR(mld,
2565 			"Failed to start session protection for TDLS: %d\n",
2566 			ret);
2567 }
2568 
2569 static bool iwl_mld_can_activate_links(struct ieee80211_hw *hw,
2570 				       struct ieee80211_vif *vif,
2571 				       u16 desired_links)
2572 {
2573 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2574 	int n_links = hweight16(desired_links);
2575 
2576 	/* Check if HW supports the wanted number of links */
2577 	return n_links <= iwl_mld_max_active_links(mld, vif);
2578 }
2579 
2580 static int
2581 iwl_mld_change_vif_links(struct ieee80211_hw *hw,
2582 			 struct ieee80211_vif *vif,
2583 			 u16 old_links, u16 new_links,
2584 			 struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS])
2585 {
2586 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
2587 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2588 	struct ieee80211_bss_conf *link_conf;
2589 	u16 removed = old_links & ~new_links;
2590 	u16 added = new_links & ~old_links;
2591 	int err;
2592 
2593 	lockdep_assert_wiphy(mld->wiphy);
2594 
2595 	/*
2596 	 * No bits designate non-MLO mode. We can handle MLO exit/enter by
2597 	 * simply mapping that to link ID zero internally.
2598 	 * Note that mac80211 does such a non-MLO to MLO switch during restart
2599 	 * if it was in MLO before. In that case, we do not have a link to
2600 	 * remove.
2601 	 */
2602 	if (old_links == 0 && !mld->fw_status.in_hw_restart)
2603 		removed |= BIT(0);
2604 
2605 	if (new_links == 0)
2606 		added |= BIT(0);
2607 
2608 	for (int i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
2609 		if (removed & BIT(i) && !WARN_ON(!old[i]))
2610 			iwl_mld_remove_link(mld, old[i]);
2611 	}
2612 
2613 	for (int i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
2614 		if (added & BIT(i)) {
2615 			link_conf = link_conf_dereference_protected(vif, i);
2616 			if (!link_conf) {
2617 				err = -EINVAL;
2618 				goto remove_added_links;
2619 			}
2620 
2621 			err = iwl_mld_add_link(mld, link_conf);
2622 			if (err)
2623 				goto remove_added_links;
2624 		}
2625 	}
2626 
2627 	/*
2628 	 * Ensure we always have a valid primary_link. When using multiple
2629 	 * links the proper value is set in assign_vif_chanctx.
2630 	 */
2631 	mld_vif->emlsr.primary = new_links ? __ffs(new_links) : 0;
2632 
2633 	/*
2634 	 * Special MLO restart case. We did not have a link when the interface
2635 	 * was added, so do the power configuration now.
2636 	 */
2637 	if (old_links == 0 && mld->fw_status.in_hw_restart)
2638 		iwl_mld_update_mac_power(mld, vif, false);
2639 
2640 	return 0;
2641 
2642 remove_added_links:
2643 	for (int i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
2644 		if (!(added & BIT(i)))
2645 			continue;
2646 
2647 		link_conf = link_conf_dereference_protected(vif, i);
2648 		if (!link_conf || !iwl_mld_link_from_mac80211(link_conf))
2649 			continue;
2650 
2651 		iwl_mld_remove_link(mld, link_conf);
2652 	}
2653 
2654 	if (WARN_ON(!iwl_mld_error_before_recovery(mld)))
2655 		return err;
2656 
2657 	/* reconfig will fix us anyway */
2658 	return 0;
2659 }
2660 
2661 static int iwl_mld_change_sta_links(struct ieee80211_hw *hw,
2662 				    struct ieee80211_vif *vif,
2663 				    struct ieee80211_sta *sta,
2664 				    u16 old_links, u16 new_links)
2665 {
2666 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2667 
2668 	return iwl_mld_update_link_stas(mld, vif, sta, old_links, new_links);
2669 }
2670 
2671 static int iwl_mld_mac80211_join_ibss(struct ieee80211_hw *hw,
2672 				      struct ieee80211_vif *vif)
2673 {
2674 	return iwl_mld_start_ap_ibss(hw, vif, &vif->bss_conf);
2675 }
2676 
2677 static void iwl_mld_mac80211_leave_ibss(struct ieee80211_hw *hw,
2678 					struct ieee80211_vif *vif)
2679 {
2680 	return iwl_mld_stop_ap_ibss(hw, vif, &vif->bss_conf);
2681 }
2682 
2683 static int iwl_mld_mac80211_tx_last_beacon(struct ieee80211_hw *hw)
2684 {
2685 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2686 
2687 	return mld->ibss_manager;
2688 }
2689 
2690 static void iwl_mld_prep_add_interface(struct ieee80211_hw *hw,
2691 				       enum nl80211_iftype type)
2692 {
2693 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2694 
2695 	IWL_DEBUG_MAC80211(mld, "prep_add_interface: type=%u\n", type);
2696 
2697 	if (!(type == NL80211_IFTYPE_AP ||
2698 	      type == NL80211_IFTYPE_P2P_GO ||
2699 	      type == NL80211_IFTYPE_P2P_CLIENT))
2700 		return;
2701 
2702 	iwl_mld_emlsr_block_tmp_non_bss(mld);
2703 }
2704 
2705 static int iwl_mld_set_hw_timestamp(struct ieee80211_hw *hw,
2706 				    struct ieee80211_vif *vif,
2707 				    struct cfg80211_set_hw_timestamp *hwts)
2708 {
2709 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2710 	u32 protocols = 0;
2711 
2712 	/* HW timestamping is only supported for a specific station */
2713 	if (!hwts->macaddr)
2714 		return -EOPNOTSUPP;
2715 
2716 	if (hwts->enable)
2717 		protocols =
2718 			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
2719 
2720 	return iwl_mld_time_sync_config(mld, hwts->macaddr, protocols);
2721 }
2722 
2723 static int iwl_mld_start_pmsr(struct ieee80211_hw *hw,
2724 			      struct ieee80211_vif *vif,
2725 			      struct cfg80211_pmsr_request *request)
2726 {
2727 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2728 
2729 	return iwl_mld_ftm_start(mld, vif, request);
2730 }
2731 
2732 static enum ieee80211_neg_ttlm_res
2733 iwl_mld_can_neg_ttlm(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2734 		     struct ieee80211_neg_ttlm *neg_ttlm)
2735 {
2736 	u16 map;
2737 
2738 	/* Verify all TIDs are mapped to the same links set */
2739 	map = neg_ttlm->downlink[0];
2740 	for (int i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
2741 		if (neg_ttlm->downlink[i] != neg_ttlm->uplink[i] ||
2742 		    neg_ttlm->uplink[i] != map)
2743 			return NEG_TTLM_RES_REJECT;
2744 	}
2745 
2746 	return NEG_TTLM_RES_ACCEPT;
2747 }
2748 
2749 static int iwl_mld_get_antenna(struct ieee80211_hw *hw, int radio_idx,
2750 			       u32 *tx_ant, u32 *rx_ant)
2751 {
2752 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2753 
2754 	*tx_ant = iwl_mld_get_valid_tx_ant(mld);
2755 	*rx_ant = iwl_mld_get_valid_rx_ant(mld);
2756 
2757 	return 0;
2758 }
2759 
2760 static int iwl_mld_set_antenna(struct ieee80211_hw *hw, int radio_idx,
2761 			       u32 tx_ant, u32 rx_ant)
2762 {
2763 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
2764 
2765 	if (WARN_ON(!mld->nvm_data))
2766 		return -EBUSY;
2767 
2768 	/* mac80211 ensures the device is not started,
2769 	 * so the firmware cannot be running
2770 	 */
2771 
2772 	mld->set_tx_ant = tx_ant;
2773 	mld->set_rx_ant = rx_ant;
2774 
2775 	iwl_reinit_cab(mld->trans, mld->nvm_data, tx_ant, rx_ant, mld->fw);
2776 
2777 	return 0;
2778 }
2779 
2780 const struct ieee80211_ops iwl_mld_hw_ops = {
2781 	.tx = iwl_mld_mac80211_tx,
2782 	.start = iwl_mld_mac80211_start,
2783 	.stop = iwl_mld_mac80211_stop,
2784 	.config = iwl_mld_mac80211_config,
2785 	.get_antenna = iwl_mld_get_antenna,
2786 	.set_antenna = iwl_mld_set_antenna,
2787 	.add_interface = iwl_mld_mac80211_add_interface,
2788 	.change_interface = iwl_mld_mac80211_change_interface,
2789 	.remove_interface = iwl_mld_mac80211_remove_interface,
2790 	.conf_tx = iwl_mld_mac80211_conf_tx,
2791 	.prepare_multicast = iwl_mld_mac80211_prepare_multicast,
2792 	.configure_filter = iwl_mld_mac80211_configure_filter,
2793 	.reconfig_complete = iwl_mld_mac80211_reconfig_complete,
2794 	.wake_tx_queue = iwl_mld_mac80211_wake_tx_queue,
2795 	.add_chanctx = iwl_mld_add_chanctx,
2796 	.remove_chanctx = iwl_mld_remove_chanctx,
2797 	.change_chanctx = iwl_mld_change_chanctx,
2798 	.assign_vif_chanctx = iwl_mld_assign_vif_chanctx,
2799 	.unassign_vif_chanctx = iwl_mld_unassign_vif_chanctx,
2800 	.set_rts_threshold = iwl_mld_mac80211_set_rts_threshold,
2801 	.link_info_changed = iwl_mld_mac80211_link_info_changed,
2802 	.vif_cfg_changed = iwl_mld_mac80211_vif_cfg_changed,
2803 	.set_key = iwl_mld_mac80211_set_key,
2804 	.hw_scan = iwl_mld_mac80211_hw_scan,
2805 	.cancel_hw_scan = iwl_mld_mac80211_cancel_hw_scan,
2806 	.sched_scan_start = iwl_mld_mac80211_sched_scan_start,
2807 	.sched_scan_stop = iwl_mld_mac80211_sched_scan_stop,
2808 	.mgd_prepare_tx = iwl_mld_mac80211_mgd_prepare_tx,
2809 	.mgd_complete_tx = iwl_mld_mac_mgd_complete_tx,
2810 	.sta_state = iwl_mld_mac80211_sta_state,
2811 	.sta_statistics = iwl_mld_mac80211_sta_statistics,
2812 	.get_survey = iwl_mld_mac80211_get_survey,
2813 	.flush = iwl_mld_mac80211_flush,
2814 	.flush_sta = iwl_mld_mac80211_flush_sta,
2815 	.ampdu_action = iwl_mld_mac80211_ampdu_action,
2816 	.can_aggregate_in_amsdu = iwl_mld_mac80211_can_aggregate,
2817 	.sync_rx_queues = iwl_mld_mac80211_sync_rx_queues,
2818 	.link_sta_rc_update = iwl_mld_sta_rc_update,
2819 	.start_ap = iwl_mld_start_ap_ibss,
2820 	.stop_ap = iwl_mld_stop_ap_ibss,
2821 	.pre_channel_switch = iwl_mld_pre_channel_switch,
2822 	.channel_switch = iwl_mld_channel_switch,
2823 	.post_channel_switch = iwl_mld_post_channel_switch,
2824 	.abort_channel_switch = iwl_mld_abort_channel_switch,
2825 	.switch_vif_chanctx = iwl_mld_switch_vif_chanctx,
2826 	.sta_pre_rcu_remove = iwl_mld_sta_pre_rcu_remove,
2827 	.remain_on_channel = iwl_mld_start_roc,
2828 	.cancel_remain_on_channel = iwl_mld_cancel_roc,
2829 	.can_activate_links = iwl_mld_can_activate_links,
2830 	.change_vif_links = iwl_mld_change_vif_links,
2831 	.change_sta_links = iwl_mld_change_sta_links,
2832 #ifdef CONFIG_PM_SLEEP
2833 	.suspend = iwl_mld_suspend,
2834 	.resume = iwl_mld_resume,
2835 	.set_wakeup = iwl_mld_set_wakeup,
2836 	.set_rekey_data = iwl_mld_set_rekey_data,
2837 #if IS_ENABLED(CONFIG_IPV6)
2838 	.ipv6_addr_change = iwl_mld_ipv6_addr_change,
2839 #endif /* IS_ENABLED(CONFIG_IPV6) */
2840 #endif /* CONFIG_PM_SLEEP */
2841 #ifdef CONFIG_IWLWIFI_DEBUGFS
2842 	.vif_add_debugfs = iwl_mld_add_vif_debugfs,
2843 	.link_add_debugfs = iwl_mld_add_link_debugfs,
2844 	.link_sta_add_debugfs = iwl_mld_add_link_sta_debugfs,
2845 #endif
2846 	.mgd_protect_tdls_discover = iwl_mld_mac80211_mgd_protect_tdls_discover,
2847 	.join_ibss = iwl_mld_mac80211_join_ibss,
2848 	.leave_ibss = iwl_mld_mac80211_leave_ibss,
2849 	.tx_last_beacon = iwl_mld_mac80211_tx_last_beacon,
2850 	.prep_add_interface = iwl_mld_prep_add_interface,
2851 	.set_hw_timestamp = iwl_mld_set_hw_timestamp,
2852 	.start_pmsr = iwl_mld_start_pmsr,
2853 	.can_neg_ttlm = iwl_mld_can_neg_ttlm,
2854 	.start_nan = iwl_mld_start_nan,
2855 	.stop_nan = iwl_mld_stop_nan,
2856 	.nan_change_conf = iwl_mld_nan_change_config,
2857 };
2858