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