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