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