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