xref: /linux/drivers/net/wireless/intel/iwlwifi/mld/d3.c (revision d7484babd2c4dcfa1ca02e7e303fab3fab529d75)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2024-2025 Intel Corporation
4  */
5 #include "mld.h"
6 
7 #include "d3.h"
8 #include "power.h"
9 #include "hcmd.h"
10 #include "iface.h"
11 #include "mcc.h"
12 #include "sta.h"
13 #include "mlo.h"
14 
15 #include "fw/api/d3.h"
16 #include "fw/api/offload.h"
17 #include "fw/api/sta.h"
18 #include "fw/dbg.h"
19 
20 #include <net/ipv6.h>
21 #include <net/addrconf.h>
22 #include <linux/bitops.h>
23 
24 /**
25  * enum iwl_mld_d3_notif - d3 notifications
26  * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF is expected/was received
27  * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF is expected/was received
28  * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF is expected/was received
29  * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF is expected/was received
30  * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF is expected/was received
31  */
32 enum iwl_mld_d3_notif {
33 	IWL_D3_NOTIF_WOWLAN_INFO =	BIT(0),
34 	IWL_D3_NOTIF_WOWLAN_WAKE_PKT =	BIT(1),
35 	IWL_D3_NOTIF_PROT_OFFLOAD =	BIT(2),
36 	IWL_D3_ND_MATCH_INFO      =     BIT(3),
37 	IWL_D3_NOTIF_D3_END_NOTIF =	BIT(4)
38 };
39 
40 struct iwl_mld_resume_key_iter_data {
41 	struct iwl_mld *mld;
42 	struct iwl_mld_wowlan_status *wowlan_status;
43 	u32 num_keys, gtk_cipher, igtk_cipher, bigtk_cipher;
44 	bool unhandled_cipher;
45 };
46 
47 struct iwl_mld_suspend_key_iter_data {
48 	struct iwl_wowlan_rsc_tsc_params_cmd *rsc;
49 	bool have_rsc;
50 	int gtks;
51 	int found_gtk_idx[4];
52 	__le32 gtk_cipher;
53 	__le32 igtk_cipher;
54 	__le32 bigtk_cipher;
55 };
56 
57 struct iwl_mld_mcast_key_data {
58 	u8 key[WOWLAN_KEY_MAX_SIZE];
59 	u8 len;
60 	u8 flags;
61 	u8 id;
62 	union {
63 		struct {
64 			struct ieee80211_key_seq aes_seq[IWL_MAX_TID_COUNT];
65 			struct ieee80211_key_seq tkip_seq[IWL_MAX_TID_COUNT];
66 		} gtk;
67 		struct {
68 			struct ieee80211_key_seq cmac_gmac_seq;
69 		} igtk_bigtk;
70 	};
71 
72 };
73 
74 /**
75  * struct iwl_mld_wowlan_status - contains wowlan status data from
76  * all wowlan notifications
77  * @wakeup_reasons: wakeup reasons, see &enum iwl_wowlan_wakeup_reason
78  * @replay_ctr: GTK rekey replay counter
79  * @pattern_number: number of the matched patterns on packets
80  * @last_qos_seq: QoS sequence counter of offloaded tid
81  * @num_of_gtk_rekeys: number of GTK rekeys during D3
82  * @tid_offloaded_tx: tid used by the firmware to transmit data packets
83  *	while in wowlan
84  * @wake_packet: wakeup packet received
85  * @wake_packet_length: wake packet length
86  * @wake_packet_bufsize: wake packet bufsize
87  * @gtk: data of the last two used gtk's by the FW upon resume
88  * @igtk: data of the last used igtk by the FW upon resume
89  * @bigtk: data of the last two used gtk's by the FW upon resume
90  * @ptk: last seq numbers per tid passed by the FW,
91  *	holds both in tkip and aes formats
92  */
93 struct iwl_mld_wowlan_status {
94 	u32 wakeup_reasons;
95 	u64 replay_ctr;
96 	u16 pattern_number;
97 	u16 last_qos_seq;
98 	u32 num_of_gtk_rekeys;
99 	u8 tid_offloaded_tx;
100 	u8 *wake_packet;
101 	u32 wake_packet_length;
102 	u32 wake_packet_bufsize;
103 	struct iwl_mld_mcast_key_data gtk[WOWLAN_GTK_KEYS_NUM];
104 	struct iwl_mld_mcast_key_data igtk;
105 	struct iwl_mld_mcast_key_data bigtk[WOWLAN_BIGTK_KEYS_NUM];
106 	struct {
107 		struct ieee80211_key_seq aes_seq[IWL_MAX_TID_COUNT];
108 		struct ieee80211_key_seq tkip_seq[IWL_MAX_TID_COUNT];
109 
110 	} ptk;
111 };
112 
113 #define NETDETECT_QUERY_BUF_LEN \
114 	(sizeof(struct iwl_scan_offload_profile_match) * \
115 	 IWL_SCAN_MAX_PROFILES_V2)
116 
117 /**
118  * struct iwl_mld_netdetect_res - contains netdetect results from
119  * match_info_notif
120  * @matched_profiles: bitmap of matched profiles, referencing the
121  *	matches passed in the scan offload request
122  * @matches: array of match information, one for each match
123  */
124 struct iwl_mld_netdetect_res {
125 	u32 matched_profiles;
126 	u8 matches[NETDETECT_QUERY_BUF_LEN];
127 };
128 
129 /**
130  * struct iwl_mld_resume_data - d3 resume flow data
131  * @notifs_expected: bitmap of expected notifications from fw,
132  *	see &enum iwl_mld_d3_notif
133  * @notifs_received: bitmap of received notifications from fw,
134  *	see &enum iwl_mld_d3_notif
135  * @d3_end_flags: bitmap of flags from d3_end_notif
136  * @notif_handling_err: error handling one of the resume notifications
137  * @wowlan_status: wowlan status data from all wowlan notifications
138  * @netdetect_res: contains netdetect results from match_info_notif
139  */
140 struct iwl_mld_resume_data {
141 	u32 notifs_expected;
142 	u32 notifs_received;
143 	u32 d3_end_flags;
144 	bool notif_handling_err;
145 	struct iwl_mld_wowlan_status *wowlan_status;
146 	struct iwl_mld_netdetect_res *netdetect_res;
147 };
148 
149 #define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT \
150 	(IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \
151 	IWL_WOWLAN_WAKEUP_BY_PATTERN | \
152 	IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\
153 	IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\
154 	IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\
155 	IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD)
156 
157 #define IWL_WOWLAN_OFFLOAD_TID 0
158 
159 void iwl_mld_set_rekey_data(struct ieee80211_hw *hw,
160 			    struct ieee80211_vif *vif,
161 			    struct cfg80211_gtk_rekey_data *data)
162 {
163 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
164 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
165 	struct iwl_mld_wowlan_data *wowlan_data = &mld_vif->wowlan_data;
166 
167 	lockdep_assert_wiphy(mld->wiphy);
168 
169 	wowlan_data->rekey_data.kek_len = data->kek_len;
170 	wowlan_data->rekey_data.kck_len = data->kck_len;
171 	memcpy(wowlan_data->rekey_data.kek, data->kek, data->kek_len);
172 	memcpy(wowlan_data->rekey_data.kck, data->kck, data->kck_len);
173 	wowlan_data->rekey_data.akm = data->akm & 0xFF;
174 	wowlan_data->rekey_data.replay_ctr =
175 		cpu_to_le64(be64_to_cpup((const __be64 *)data->replay_ctr));
176 	wowlan_data->rekey_data.valid = true;
177 }
178 
179 #if IS_ENABLED(CONFIG_IPV6)
180 void iwl_mld_ipv6_addr_change(struct ieee80211_hw *hw,
181 			      struct ieee80211_vif *vif,
182 			      struct inet6_dev *idev)
183 {
184 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
185 	struct iwl_mld_wowlan_data *wowlan_data = &mld_vif->wowlan_data;
186 	struct inet6_ifaddr *ifa;
187 	int idx = 0;
188 
189 	memset(wowlan_data->tentative_addrs, 0,
190 	       sizeof(wowlan_data->tentative_addrs));
191 
192 	read_lock_bh(&idev->lock);
193 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
194 		wowlan_data->target_ipv6_addrs[idx] = ifa->addr;
195 		if (ifa->flags & IFA_F_TENTATIVE)
196 			__set_bit(idx, wowlan_data->tentative_addrs);
197 		idx++;
198 		if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
199 			break;
200 	}
201 	read_unlock_bh(&idev->lock);
202 
203 	wowlan_data->num_target_ipv6_addrs = idx;
204 }
205 #endif
206 
207 enum rt_status {
208 	FW_ALIVE,
209 	FW_NEEDS_RESET,
210 	FW_ERROR,
211 };
212 
213 static enum rt_status iwl_mld_check_err_tables(struct iwl_mld *mld,
214 					       struct ieee80211_vif *vif)
215 {
216 	u32 err_id;
217 
218 	/* check for lmac1 error */
219 	if (iwl_fwrt_read_err_table(mld->trans,
220 				    mld->trans->dbg.lmac_error_event_table[0],
221 				    &err_id)) {
222 		if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN && vif) {
223 			struct cfg80211_wowlan_wakeup wakeup = {
224 				.rfkill_release = true,
225 			};
226 			ieee80211_report_wowlan_wakeup(vif, &wakeup,
227 						       GFP_KERNEL);
228 
229 			return FW_NEEDS_RESET;
230 		}
231 		return FW_ERROR;
232 	}
233 
234 	/* check if we have lmac2 set and check for error */
235 	if (iwl_fwrt_read_err_table(mld->trans,
236 				    mld->trans->dbg.lmac_error_event_table[1],
237 				    NULL))
238 		return FW_ERROR;
239 
240 	/* check for umac error */
241 	if (iwl_fwrt_read_err_table(mld->trans,
242 				    mld->trans->dbg.umac_error_event_table,
243 				    NULL))
244 		return FW_ERROR;
245 
246 	return FW_ALIVE;
247 }
248 
249 static bool iwl_mld_fw_needs_restart(struct iwl_mld *mld,
250 				     struct ieee80211_vif *vif)
251 {
252 	enum rt_status rt_status = iwl_mld_check_err_tables(mld, vif);
253 
254 	if (rt_status == FW_ALIVE)
255 		return false;
256 
257 	if (rt_status == FW_ERROR) {
258 		IWL_ERR(mld, "FW Error occurred during suspend\n");
259 		iwl_fwrt_dump_error_logs(&mld->fwrt);
260 		iwl_dbg_tlv_time_point(&mld->fwrt,
261 				       IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL);
262 	}
263 
264 	return true;
265 }
266 
267 static int
268 iwl_mld_netdetect_config(struct iwl_mld *mld,
269 			 struct ieee80211_vif *vif,
270 			 const struct cfg80211_wowlan *wowlan)
271 {
272 	int ret;
273 	struct cfg80211_sched_scan_request *netdetect_cfg =
274 		wowlan->nd_config;
275 	struct ieee80211_scan_ies ies = {};
276 
277 	ret = iwl_mld_scan_stop(mld, IWL_MLD_SCAN_SCHED, true);
278 	if (ret)
279 		return ret;
280 
281 	ret = iwl_mld_sched_scan_start(mld, vif, netdetect_cfg, &ies,
282 				       IWL_MLD_SCAN_NETDETECT);
283 	return ret;
284 }
285 
286 static void
287 iwl_mld_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
288 {
289 	u64 pn = le64_to_cpu(le_pn);
290 
291 	seq->tkip.iv16 = (u16)pn;
292 	seq->tkip.iv32 = (u32)(pn >> 16);
293 }
294 
295 static void
296 iwl_mld_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
297 {
298 	u64 pn = le64_to_cpu(le_pn);
299 
300 	seq->ccmp.pn[0] = pn >> 40;
301 	seq->ccmp.pn[1] = pn >> 32;
302 	seq->ccmp.pn[2] = pn >> 24;
303 	seq->ccmp.pn[3] = pn >> 16;
304 	seq->ccmp.pn[4] = pn >> 8;
305 	seq->ccmp.pn[5] = pn;
306 }
307 
308 static void
309 iwl_mld_convert_gtk_resume_seq(struct iwl_mld_mcast_key_data *gtk_data,
310 			       const struct iwl_wowlan_all_rsc_tsc_v5 *sc,
311 			       int rsc_idx)
312 {
313 	struct ieee80211_key_seq *aes_seq = gtk_data->gtk.aes_seq;
314 	struct ieee80211_key_seq *tkip_seq = gtk_data->gtk.tkip_seq;
315 
316 	if (rsc_idx >= ARRAY_SIZE(sc->mcast_rsc))
317 		return;
318 
319 	/* We store both the TKIP and AES representations coming from the
320 	 * FW because we decode the data from there before we iterate
321 	 * the keys and know which type is used.
322 	 */
323 	for (int tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
324 		iwl_mld_le64_to_tkip_seq(sc->mcast_rsc[rsc_idx][tid],
325 					 &tkip_seq[tid]);
326 		iwl_mld_le64_to_aes_seq(sc->mcast_rsc[rsc_idx][tid],
327 					&aes_seq[tid]);
328 	}
329 }
330 
331 static void
332 iwl_mld_convert_gtk_resume_data(struct iwl_mld *mld,
333 				struct iwl_mld_wowlan_status *wowlan_status,
334 				const struct iwl_wowlan_gtk_status_v3 *gtk_data,
335 				const struct iwl_wowlan_all_rsc_tsc_v5 *sc)
336 {
337 	int status_idx = 0;
338 
339 	BUILD_BUG_ON(sizeof(wowlan_status->gtk[0].key) <
340 		     sizeof(gtk_data[0].key));
341 	BUILD_BUG_ON(ARRAY_SIZE(wowlan_status->gtk) < WOWLAN_GTK_KEYS_NUM);
342 
343 	for (int notif_idx = 0; notif_idx < ARRAY_SIZE(wowlan_status->gtk);
344 	     notif_idx++) {
345 		int rsc_idx;
346 
347 		if (!(gtk_data[notif_idx].key_len))
348 			continue;
349 
350 		wowlan_status->gtk[status_idx].len =
351 			gtk_data[notif_idx].key_len;
352 		wowlan_status->gtk[status_idx].flags =
353 			gtk_data[notif_idx].key_flags;
354 		wowlan_status->gtk[status_idx].id =
355 			wowlan_status->gtk[status_idx].flags &
356 			IWL_WOWLAN_GTK_IDX_MASK;
357 		memcpy(wowlan_status->gtk[status_idx].key,
358 		       gtk_data[notif_idx].key,
359 		       sizeof(gtk_data[notif_idx].key));
360 
361 		/* The rsc for both gtk keys are stored in gtk[0]->sc->mcast_rsc
362 		 * The gtk ids can be any two numbers between 0 and 3,
363 		 * the id_map maps between the key id and the index in sc->mcast
364 		 */
365 		rsc_idx =
366 			sc->mcast_key_id_map[wowlan_status->gtk[status_idx].id];
367 		iwl_mld_convert_gtk_resume_seq(&wowlan_status->gtk[status_idx],
368 					       sc, rsc_idx);
369 
370 		/* if it's as long as the TKIP encryption key, copy MIC key */
371 		if (wowlan_status->gtk[status_idx].len ==
372 		    NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
373 			memcpy(wowlan_status->gtk[status_idx].key +
374 			       NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
375 			       gtk_data[notif_idx].tkip_mic_key,
376 			       sizeof(gtk_data[notif_idx].tkip_mic_key));
377 		status_idx++;
378 	}
379 }
380 
381 static void
382 iwl_mld_convert_ptk_resume_seq(struct iwl_mld *mld,
383 			       struct iwl_mld_wowlan_status *wowlan_status,
384 			       const struct iwl_wowlan_all_rsc_tsc_v5 *sc)
385 {
386 	struct ieee80211_key_seq *aes_seq = wowlan_status->ptk.aes_seq;
387 	struct ieee80211_key_seq *tkip_seq = wowlan_status->ptk.tkip_seq;
388 
389 	BUILD_BUG_ON(ARRAY_SIZE(sc->ucast_rsc) != IWL_MAX_TID_COUNT);
390 
391 	for (int tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
392 		iwl_mld_le64_to_aes_seq(sc->ucast_rsc[tid], &aes_seq[tid]);
393 		iwl_mld_le64_to_tkip_seq(sc->ucast_rsc[tid], &tkip_seq[tid]);
394 	}
395 }
396 
397 static void
398 iwl_mld_convert_mcast_ipn(struct iwl_mld_mcast_key_data *key_status,
399 			  const struct iwl_wowlan_igtk_status *key)
400 {
401 	struct ieee80211_key_seq *seq =
402 		&key_status->igtk_bigtk.cmac_gmac_seq;
403 	u8 ipn_len = ARRAY_SIZE(key->ipn);
404 
405 	BUILD_BUG_ON(ipn_len != ARRAY_SIZE(seq->aes_gmac.pn));
406 	BUILD_BUG_ON(ipn_len != ARRAY_SIZE(seq->aes_cmac.pn));
407 	BUILD_BUG_ON(offsetof(struct ieee80211_key_seq, aes_gmac) !=
408 		     offsetof(struct ieee80211_key_seq, aes_cmac));
409 
410 	/* mac80211 expects big endian for memcmp() to work, convert.
411 	 * We don't have the key cipher yet so copy to both to cmac and gmac
412 	 */
413 	for (int i = 0; i < ipn_len; i++) {
414 		seq->aes_gmac.pn[i] = key->ipn[ipn_len - i - 1];
415 		seq->aes_cmac.pn[i] = key->ipn[ipn_len - i - 1];
416 	}
417 }
418 
419 static void
420 iwl_mld_convert_igtk_resume_data(struct iwl_mld_wowlan_status *wowlan_status,
421 				 const struct iwl_wowlan_igtk_status *igtk)
422 {
423 	BUILD_BUG_ON(sizeof(wowlan_status->igtk.key) < sizeof(igtk->key));
424 
425 	if (!igtk->key_len)
426 		return;
427 
428 	wowlan_status->igtk.len = igtk->key_len;
429 	wowlan_status->igtk.flags = igtk->key_flags;
430 	wowlan_status->igtk.id =
431 		u32_get_bits(igtk->key_flags,
432 			     IWL_WOWLAN_IGTK_BIGTK_IDX_MASK) +
433 		WOWLAN_IGTK_MIN_INDEX;
434 
435 	memcpy(wowlan_status->igtk.key, igtk->key, sizeof(igtk->key));
436 	iwl_mld_convert_mcast_ipn(&wowlan_status->igtk, igtk);
437 }
438 
439 static void
440 iwl_mld_convert_bigtk_resume_data(struct iwl_mld_wowlan_status *wowlan_status,
441 				  const struct iwl_wowlan_igtk_status *bigtk)
442 {
443 	int status_idx = 0;
444 
445 	BUILD_BUG_ON(ARRAY_SIZE(wowlan_status->bigtk) < WOWLAN_BIGTK_KEYS_NUM);
446 
447 	for (int notif_idx = 0; notif_idx < WOWLAN_BIGTK_KEYS_NUM;
448 	     notif_idx++) {
449 		if (!bigtk[notif_idx].key_len)
450 			continue;
451 
452 		wowlan_status->bigtk[status_idx].len = bigtk[notif_idx].key_len;
453 		wowlan_status->bigtk[status_idx].flags =
454 			bigtk[notif_idx].key_flags;
455 		wowlan_status->bigtk[status_idx].id =
456 			u32_get_bits(bigtk[notif_idx].key_flags,
457 				     IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
458 			+ WOWLAN_BIGTK_MIN_INDEX;
459 
460 		BUILD_BUG_ON(sizeof(wowlan_status->bigtk[status_idx].key) <
461 			     sizeof(bigtk[notif_idx].key));
462 		memcpy(wowlan_status->bigtk[status_idx].key,
463 		       bigtk[notif_idx].key, sizeof(bigtk[notif_idx].key));
464 		iwl_mld_convert_mcast_ipn(&wowlan_status->bigtk[status_idx],
465 					  &bigtk[notif_idx]);
466 		status_idx++;
467 	}
468 }
469 
470 static bool
471 iwl_mld_handle_wowlan_info_notif(struct iwl_mld *mld,
472 				 struct iwl_mld_wowlan_status *wowlan_status,
473 				 struct iwl_rx_packet *pkt)
474 {
475 	const struct iwl_wowlan_info_notif *notif = (void *)pkt->data;
476 	u32 expected_len, len = iwl_rx_packet_payload_len(pkt);
477 
478 	expected_len = sizeof(*notif);
479 
480 	if (IWL_FW_CHECK(mld, len < expected_len,
481 			 "Invalid wowlan_info_notif (expected=%ud got=%ud)\n",
482 			 expected_len, len))
483 		return true;
484 
485 	if (IWL_FW_CHECK(mld, notif->tid_offloaded_tx != IWL_WOWLAN_OFFLOAD_TID,
486 			 "Invalid tid_offloaded_tx %d\n",
487 			 wowlan_status->tid_offloaded_tx))
488 		return true;
489 
490 	iwl_mld_convert_gtk_resume_data(mld, wowlan_status, notif->gtk,
491 					&notif->gtk[0].sc);
492 	iwl_mld_convert_ptk_resume_seq(mld, wowlan_status, &notif->gtk[0].sc);
493 	/* only one igtk is passed by FW */
494 	iwl_mld_convert_igtk_resume_data(wowlan_status, &notif->igtk[0]);
495 	iwl_mld_convert_bigtk_resume_data(wowlan_status, notif->bigtk);
496 
497 	wowlan_status->replay_ctr = le64_to_cpu(notif->replay_ctr);
498 	wowlan_status->pattern_number = le16_to_cpu(notif->pattern_number);
499 
500 	wowlan_status->tid_offloaded_tx = notif->tid_offloaded_tx;
501 	wowlan_status->last_qos_seq = le16_to_cpu(notif->qos_seq_ctr);
502 	wowlan_status->num_of_gtk_rekeys =
503 		le32_to_cpu(notif->num_of_gtk_rekeys);
504 	wowlan_status->wakeup_reasons = le32_to_cpu(notif->wakeup_reasons);
505 	return false;
506 	/* TODO: mlo_links (task=MLO)*/
507 }
508 
509 static bool
510 iwl_mld_handle_wake_pkt_notif(struct iwl_mld *mld,
511 			      struct iwl_mld_wowlan_status *wowlan_status,
512 			      struct iwl_rx_packet *pkt)
513 {
514 	const struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data;
515 	u32 actual_size, len = iwl_rx_packet_payload_len(pkt);
516 	u32 expected_size = le32_to_cpu(notif->wake_packet_length);
517 
518 	if (IWL_FW_CHECK(mld, len < sizeof(*notif),
519 			 "Invalid WoWLAN wake packet notification (expected size=%zu got=%u)\n",
520 			 sizeof(*notif), len))
521 		return true;
522 
523 	if (IWL_FW_CHECK(mld, !(wowlan_status->wakeup_reasons &
524 				IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT),
525 			 "Got wake packet but wakeup reason is %x\n",
526 			 wowlan_status->wakeup_reasons))
527 		return true;
528 
529 	actual_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif,
530 				     wake_packet);
531 
532 	/* actual_size got the padding from the notification, remove it. */
533 	if (expected_size < actual_size)
534 		actual_size = expected_size;
535 	wowlan_status->wake_packet = kmemdup(notif->wake_packet, actual_size,
536 					     GFP_ATOMIC);
537 	if (!wowlan_status->wake_packet)
538 		return true;
539 
540 	wowlan_status->wake_packet_length = expected_size;
541 	wowlan_status->wake_packet_bufsize = actual_size;
542 
543 	return false;
544 }
545 
546 static void
547 iwl_mld_set_wake_packet(struct iwl_mld *mld,
548 			struct ieee80211_vif *vif,
549 			const struct iwl_mld_wowlan_status *wowlan_status,
550 			struct cfg80211_wowlan_wakeup *wakeup,
551 			struct sk_buff **_pkt)
552 {
553 	int pkt_bufsize = wowlan_status->wake_packet_bufsize;
554 	int expected_pktlen = wowlan_status->wake_packet_length;
555 	const u8 *pktdata = wowlan_status->wake_packet;
556 	const struct ieee80211_hdr *hdr = (const void *)pktdata;
557 	int truncated = expected_pktlen - pkt_bufsize;
558 
559 	if (ieee80211_is_data(hdr->frame_control)) {
560 		int hdrlen = ieee80211_hdrlen(hdr->frame_control);
561 		int ivlen = 0, icvlen = 4; /* also FCS */
562 
563 		struct sk_buff *pkt = alloc_skb(pkt_bufsize, GFP_KERNEL);
564 		*_pkt = pkt;
565 		if (!pkt)
566 			return;
567 
568 		skb_put_data(pkt, pktdata, hdrlen);
569 		pktdata += hdrlen;
570 		pkt_bufsize -= hdrlen;
571 
572 		/* if truncated, FCS/ICV is (partially) gone */
573 		if (truncated >= icvlen) {
574 			truncated -= icvlen;
575 			icvlen = 0;
576 		} else {
577 			icvlen -= truncated;
578 			truncated = 0;
579 		}
580 
581 		pkt_bufsize -= ivlen + icvlen;
582 		pktdata += ivlen;
583 
584 		skb_put_data(pkt, pktdata, pkt_bufsize);
585 
586 		if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
587 			return;
588 		wakeup->packet = pkt->data;
589 		wakeup->packet_present_len = pkt->len;
590 		wakeup->packet_len = pkt->len - truncated;
591 		wakeup->packet_80211 = false;
592 	} else {
593 		int fcslen = 4;
594 
595 		if (truncated >= 4) {
596 			truncated -= 4;
597 			fcslen = 0;
598 		} else {
599 			fcslen -= truncated;
600 			truncated = 0;
601 		}
602 		pkt_bufsize -= fcslen;
603 		wakeup->packet = wowlan_status->wake_packet;
604 		wakeup->packet_present_len = pkt_bufsize;
605 		wakeup->packet_len = expected_pktlen - truncated;
606 		wakeup->packet_80211 = true;
607 	}
608 }
609 
610 static void
611 iwl_mld_report_wowlan_wakeup(struct iwl_mld *mld,
612 			     struct ieee80211_vif *vif,
613 			     struct iwl_mld_wowlan_status *wowlan_status)
614 {
615 	struct sk_buff *pkt = NULL;
616 	struct cfg80211_wowlan_wakeup wakeup = {
617 		.pattern_idx = -1,
618 	};
619 	u32 reasons = wowlan_status->wakeup_reasons;
620 
621 	if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
622 		ieee80211_report_wowlan_wakeup(vif, NULL, GFP_KERNEL);
623 		return;
624 	}
625 
626 	pm_wakeup_event(mld->dev, 0);
627 
628 	if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
629 		wakeup.magic_pkt = true;
630 
631 	if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
632 		wakeup.pattern_idx =
633 			wowlan_status->pattern_number;
634 
635 	if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
636 		       IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH |
637 		       IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE))
638 		wakeup.disconnect = true;
639 
640 	if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
641 		wakeup.gtk_rekey_failure = true;
642 
643 	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
644 		wakeup.rfkill_release = true;
645 
646 	if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
647 		wakeup.eap_identity_req = true;
648 
649 	if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
650 		wakeup.four_way_handshake = true;
651 
652 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
653 		wakeup.tcp_connlost = true;
654 
655 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
656 		wakeup.tcp_nomoretokens = true;
657 
658 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
659 		wakeup.tcp_match = true;
660 
661 	if (reasons & IWL_WAKEUP_BY_11W_UNPROTECTED_DEAUTH_OR_DISASSOC)
662 		wakeup.unprot_deauth_disassoc = true;
663 
664 	if (wowlan_status->wake_packet)
665 		iwl_mld_set_wake_packet(mld, vif, wowlan_status, &wakeup, &pkt);
666 
667 	ieee80211_report_wowlan_wakeup(vif, &wakeup, GFP_KERNEL);
668 	kfree_skb(pkt);
669 }
670 
671 static void
672 iwl_mld_set_key_rx_seq_tids(struct ieee80211_key_conf *key,
673 			    struct ieee80211_key_seq *seq)
674 {
675 	int tid;
676 
677 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
678 		ieee80211_set_key_rx_seq(key, tid, &seq[tid]);
679 }
680 
681 static void
682 iwl_mld_set_key_rx_seq(struct ieee80211_key_conf *key,
683 		       struct iwl_mld_mcast_key_data *key_data)
684 {
685 	switch (key->cipher) {
686 	case WLAN_CIPHER_SUITE_CCMP:
687 	case WLAN_CIPHER_SUITE_GCMP:
688 	case WLAN_CIPHER_SUITE_GCMP_256:
689 		iwl_mld_set_key_rx_seq_tids(key,
690 					    key_data->gtk.aes_seq);
691 		break;
692 	case WLAN_CIPHER_SUITE_TKIP:
693 		iwl_mld_set_key_rx_seq_tids(key,
694 					    key_data->gtk.tkip_seq);
695 		break;
696 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
697 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
698 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
699 	case WLAN_CIPHER_SUITE_AES_CMAC:
700 		/* igtk/bigtk ciphers*/
701 		ieee80211_set_key_rx_seq(key, 0,
702 					 &key_data->igtk_bigtk.cmac_gmac_seq);
703 		break;
704 	default:
705 		WARN_ON(1);
706 	}
707 }
708 
709 static void
710 iwl_mld_d3_update_mcast_key(struct iwl_mld *mld,
711 			    struct ieee80211_vif *vif,
712 			    struct iwl_mld_wowlan_status *wowlan_status,
713 			    struct ieee80211_key_conf *key,
714 			    struct iwl_mld_mcast_key_data *key_data)
715 {
716 	if (key->keyidx != key_data->id &&
717 	    (key->keyidx < 4 || key->keyidx > 5)) {
718 		IWL_ERR(mld,
719 			"Unexpected keyId mismatch. Old keyId:%d, New keyId:%d\n",
720 			key->keyidx, key_data->id);
721 		return;
722 	}
723 
724 	/* All installed keys are sent by the FW, even weren't
725 	 * rekeyed during D3.
726 	 * We remove an existing key if it has the same index as
727 	 * a new key and a rekey has occurred during d3
728 	 */
729 	if (wowlan_status->num_of_gtk_rekeys && key_data->len) {
730 		if (key->keyidx == 4 || key->keyidx == 5) {
731 			struct iwl_mld_vif *mld_vif =
732 				iwl_mld_vif_from_mac80211(vif);
733 			struct iwl_mld_link *mld_link;
734 			int link_id = vif->active_links ?
735 				__ffs(vif->active_links) : 0;
736 
737 			mld_link = iwl_mld_link_dereference_check(mld_vif,
738 								  link_id);
739 			if (WARN_ON(!mld_link))
740 				return;
741 
742 			if (mld_link->igtk == key)
743 				mld_link->igtk = NULL;
744 			mld->num_igtks--;
745 		}
746 
747 		ieee80211_remove_key(key);
748 		return;
749 	}
750 
751 	iwl_mld_set_key_rx_seq(key, key_data);
752 }
753 
754 static void
755 iwl_mld_update_ptk_rx_seq(struct iwl_mld *mld,
756 			  struct iwl_mld_wowlan_status *wowlan_status,
757 			  struct ieee80211_sta *sta,
758 			  struct ieee80211_key_conf *key,
759 			  bool is_tkip)
760 {
761 	struct iwl_mld_sta *mld_sta =
762 		iwl_mld_sta_from_mac80211(sta);
763 	struct iwl_mld_ptk_pn *mld_ptk_pn =
764 		wiphy_dereference(mld->wiphy,
765 				  mld_sta->ptk_pn[key->keyidx]);
766 
767 	iwl_mld_set_key_rx_seq_tids(key, is_tkip ?
768 				    wowlan_status->ptk.tkip_seq :
769 				    wowlan_status->ptk.aes_seq);
770 	if (is_tkip)
771 		return;
772 
773 	if (WARN_ON(!mld_ptk_pn))
774 		return;
775 
776 	for (int tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
777 		for (int i = 1; i < mld->trans->num_rx_queues; i++)
778 			memcpy(mld_ptk_pn->q[i].pn[tid],
779 			       wowlan_status->ptk.aes_seq[tid].ccmp.pn,
780 			       IEEE80211_CCMP_PN_LEN);
781 	}
782 }
783 
784 static void
785 iwl_mld_resume_keys_iter(struct ieee80211_hw *hw,
786 			 struct ieee80211_vif *vif,
787 			 struct ieee80211_sta *sta,
788 			 struct ieee80211_key_conf *key,
789 			 void *_data)
790 {
791 	struct iwl_mld_resume_key_iter_data *data = _data;
792 	struct iwl_mld_wowlan_status *wowlan_status = data->wowlan_status;
793 	u8 status_idx;
794 
795 	/* TODO: check key link id (task=MLO) */
796 	if (data->unhandled_cipher)
797 		return;
798 
799 	switch (key->cipher) {
800 	case WLAN_CIPHER_SUITE_WEP40:
801 	case WLAN_CIPHER_SUITE_WEP104:
802 		/* ignore WEP completely, nothing to do */
803 		return;
804 	case WLAN_CIPHER_SUITE_CCMP:
805 	case WLAN_CIPHER_SUITE_GCMP:
806 	case WLAN_CIPHER_SUITE_GCMP_256:
807 	case WLAN_CIPHER_SUITE_TKIP:
808 		if (sta) {
809 			iwl_mld_update_ptk_rx_seq(data->mld, wowlan_status,
810 						  sta, key,
811 						  key->cipher ==
812 						  WLAN_CIPHER_SUITE_TKIP);
813 			return;
814 		}
815 
816 		if (WARN_ON(data->gtk_cipher &&
817 			    data->gtk_cipher != key->cipher))
818 			return;
819 
820 		data->gtk_cipher = key->cipher;
821 		status_idx = key->keyidx == wowlan_status->gtk[1].id;
822 		iwl_mld_d3_update_mcast_key(data->mld, vif, wowlan_status, key,
823 					    &wowlan_status->gtk[status_idx]);
824 		break;
825 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
826 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
827 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
828 	case WLAN_CIPHER_SUITE_AES_CMAC:
829 		if (key->keyidx == 4 || key->keyidx == 5) {
830 			if (WARN_ON(data->igtk_cipher &&
831 				    data->igtk_cipher != key->cipher))
832 				return;
833 
834 			data->igtk_cipher = key->cipher;
835 			iwl_mld_d3_update_mcast_key(data->mld, vif,
836 						    wowlan_status,
837 						    key, &wowlan_status->igtk);
838 		}
839 		if (key->keyidx == 6 || key->keyidx == 7) {
840 			if (WARN_ON(data->bigtk_cipher &&
841 				    data->bigtk_cipher != key->cipher))
842 				return;
843 
844 			data->bigtk_cipher = key->cipher;
845 			status_idx = key->keyidx == wowlan_status->bigtk[1].id;
846 			iwl_mld_d3_update_mcast_key(data->mld, vif,
847 						    wowlan_status, key,
848 						    &wowlan_status->bigtk[status_idx]);
849 		}
850 		break;
851 	default:
852 		data->unhandled_cipher = true;
853 		return;
854 	}
855 	data->num_keys++;
856 }
857 
858 static bool
859 iwl_mld_add_mcast_rekey(struct ieee80211_vif *vif,
860 			struct iwl_mld *mld,
861 			struct iwl_mld_mcast_key_data *key_data,
862 			struct ieee80211_bss_conf *link_conf,
863 			u32 cipher)
864 {
865 	struct ieee80211_key_conf *key_config;
866 	struct {
867 		struct ieee80211_key_conf conf;
868 		u8 key[WOWLAN_KEY_MAX_SIZE];
869 	} conf = {
870 		.conf.cipher = cipher,
871 		.conf.keyidx = key_data->id,
872 	};
873 	int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
874 
875 	BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
876 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP);
877 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256);
878 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP);
879 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_BIP_GMAC_128);
880 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_BIP_GMAC_256);
881 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_AES_CMAC);
882 	BUILD_BUG_ON(sizeof(conf.key) < sizeof(key_data->key));
883 
884 	if (!key_data->len)
885 		return true;
886 
887 	switch (cipher) {
888 	case WLAN_CIPHER_SUITE_CCMP:
889 	case WLAN_CIPHER_SUITE_GCMP:
890 		conf.conf.keylen = WLAN_KEY_LEN_CCMP;
891 		break;
892 	case WLAN_CIPHER_SUITE_GCMP_256:
893 		conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
894 		break;
895 	case WLAN_CIPHER_SUITE_TKIP:
896 		conf.conf.keylen = WLAN_KEY_LEN_TKIP;
897 		break;
898 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
899 		conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_128;
900 		break;
901 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
902 		conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_256;
903 		break;
904 	case WLAN_CIPHER_SUITE_AES_CMAC:
905 		conf.conf.keylen = WLAN_KEY_LEN_AES_CMAC;
906 		break;
907 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
908 		conf.conf.keylen = WLAN_KEY_LEN_BIP_CMAC_256;
909 		break;
910 	default:
911 		WARN_ON(1);
912 	}
913 
914 	memcpy(conf.conf.key, key_data->key, conf.conf.keylen);
915 	key_config = ieee80211_gtk_rekey_add(vif, &conf.conf, link_id);
916 	if (IS_ERR(key_config))
917 		return false;
918 
919 	iwl_mld_set_key_rx_seq(key_config, key_data);
920 
921 	/* The FW holds only one igtk so we keep track of the valid one */
922 	if (key_config->keyidx == 4 || key_config->keyidx == 5) {
923 		struct iwl_mld_link *mld_link =
924 			iwl_mld_link_from_mac80211(link_conf);
925 		mld_link->igtk = key_config;
926 		mld->num_igtks++;
927 	}
928 	return true;
929 }
930 
931 static bool
932 iwl_mld_add_all_rekeys(struct ieee80211_vif *vif,
933 		       struct iwl_mld_wowlan_status *wowlan_status,
934 		       struct iwl_mld_resume_key_iter_data *key_iter_data,
935 		       struct ieee80211_bss_conf *link_conf)
936 {
937 	int i;
938 
939 	for (i = 0; i < ARRAY_SIZE(wowlan_status->gtk); i++)
940 		if (!iwl_mld_add_mcast_rekey(vif, key_iter_data->mld,
941 					     &wowlan_status->gtk[i],
942 					     link_conf,
943 					     key_iter_data->gtk_cipher))
944 			return false;
945 
946 	if (!iwl_mld_add_mcast_rekey(vif, key_iter_data->mld,
947 				     &wowlan_status->igtk,
948 				     link_conf, key_iter_data->igtk_cipher))
949 		return false;
950 
951 	for (i = 0; i < ARRAY_SIZE(wowlan_status->bigtk); i++)
952 		if (!iwl_mld_add_mcast_rekey(vif, key_iter_data->mld,
953 					     &wowlan_status->bigtk[i],
954 					     link_conf,
955 					     key_iter_data->bigtk_cipher))
956 			return false;
957 
958 	return true;
959 }
960 
961 static bool
962 iwl_mld_update_sec_keys(struct iwl_mld *mld,
963 			struct ieee80211_vif *vif,
964 			struct iwl_mld_wowlan_status *wowlan_status)
965 {
966 	int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
967 	struct ieee80211_bss_conf *link_conf =
968 		link_conf_dereference_protected(vif, link_id);
969 	__be64 replay_ctr = cpu_to_be64(wowlan_status->replay_ctr);
970 	struct iwl_mld_resume_key_iter_data key_iter_data = {
971 		.mld = mld,
972 		.wowlan_status = wowlan_status,
973 	};
974 
975 	if (WARN_ON(!link_conf))
976 		return false;
977 
978 	ieee80211_iter_keys(mld->hw, vif, iwl_mld_resume_keys_iter,
979 			    &key_iter_data);
980 
981 	if (key_iter_data.unhandled_cipher)
982 		return false;
983 
984 	IWL_DEBUG_WOWLAN(mld,
985 			 "Number of installed keys: %d, Number of rekeys: %d\n",
986 			 key_iter_data.num_keys,
987 			 wowlan_status->num_of_gtk_rekeys);
988 
989 	if (!key_iter_data.num_keys || !wowlan_status->num_of_gtk_rekeys)
990 		return true;
991 
992 	iwl_mld_add_all_rekeys(vif, wowlan_status, &key_iter_data,
993 			       link_conf);
994 
995 	ieee80211_gtk_rekey_notify(vif, link_conf->bssid,
996 				   (void *)&replay_ctr, GFP_KERNEL);
997 	/* TODO: MLO rekey (task=MLO) */
998 	return true;
999 }
1000 
1001 static bool
1002 iwl_mld_process_wowlan_status(struct iwl_mld *mld,
1003 			      struct ieee80211_vif *vif,
1004 			      struct iwl_mld_wowlan_status *wowlan_status)
1005 {
1006 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1007 	struct ieee80211_sta *ap_sta = mld_vif->ap_sta;
1008 	struct iwl_mld_txq *mld_txq;
1009 
1010 	iwl_mld_report_wowlan_wakeup(mld, vif, wowlan_status);
1011 
1012 	if (WARN_ON(!ap_sta))
1013 		return false;
1014 
1015 	mld_txq =
1016 		iwl_mld_txq_from_mac80211(ap_sta->txq[wowlan_status->tid_offloaded_tx]);
1017 
1018 	/* Update the pointers of the Tx queue that may have moved during
1019 	 * suspend if the firmware sent frames.
1020 	 * The firmware stores last-used value, we store next value.
1021 	 */
1022 	WARN_ON(!mld_txq->status.allocated);
1023 	iwl_trans_set_q_ptrs(mld->trans, mld_txq->fw_id,
1024 			     (wowlan_status->last_qos_seq +
1025 			     0x10) >> 4);
1026 
1027 	if (!iwl_mld_update_sec_keys(mld, vif, wowlan_status))
1028 		return false;
1029 
1030 	if (wowlan_status->wakeup_reasons &
1031 	    (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1032 	     IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH |
1033 	     IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE))
1034 		return false;
1035 
1036 	return true;
1037 }
1038 
1039 static bool
1040 iwl_mld_netdetect_match_info_handler(struct iwl_mld *mld,
1041 				     struct iwl_mld_resume_data *resume_data,
1042 				     struct iwl_rx_packet *pkt)
1043 {
1044 	struct iwl_mld_netdetect_res *results = resume_data->netdetect_res;
1045 	const struct iwl_scan_offload_match_info *notif = (void *)pkt->data;
1046 	u32 len = iwl_rx_packet_payload_len(pkt);
1047 
1048 	if (IWL_FW_CHECK(mld, !mld->netdetect,
1049 			 "Got scan match info notif when mld->netdetect==%d\n",
1050 			 mld->netdetect))
1051 		return true;
1052 
1053 	if (IWL_FW_CHECK(mld, len < sizeof(*notif),
1054 			 "Invalid scan offload match notif of length: %d\n",
1055 			 len))
1056 		return true;
1057 
1058 	if (IWL_FW_CHECK(mld, resume_data->wowlan_status->wakeup_reasons !=
1059 			 IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS,
1060 			 "Ignore scan match info: unexpected wakeup reason (expected=0x%x got=0x%x)\n",
1061 			 IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS,
1062 			 resume_data->wowlan_status->wakeup_reasons))
1063 		return true;
1064 
1065 	results->matched_profiles = le32_to_cpu(notif->matched_profiles);
1066 	IWL_DEBUG_WOWLAN(mld, "number of matched profiles=%u\n",
1067 			 results->matched_profiles);
1068 
1069 	if (results->matched_profiles)
1070 		memcpy(results->matches, notif->matches,
1071 		       NETDETECT_QUERY_BUF_LEN);
1072 
1073 	/* No scan should be active at this point */
1074 	mld->scan.status = 0;
1075 	memset(mld->scan.uid_status, 0, sizeof(mld->scan.uid_status));
1076 	return false;
1077 }
1078 
1079 static void
1080 iwl_mld_set_netdetect_info(struct iwl_mld *mld,
1081 			   const struct cfg80211_sched_scan_request *netdetect_cfg,
1082 			   struct cfg80211_wowlan_nd_info *netdetect_info,
1083 			   struct iwl_mld_netdetect_res *netdetect_res,
1084 			   unsigned long matched_profiles)
1085 {
1086 	int i;
1087 
1088 	for_each_set_bit(i, &matched_profiles, netdetect_cfg->n_match_sets) {
1089 		struct cfg80211_wowlan_nd_match *match;
1090 		int idx, j, n_channels = 0;
1091 		struct iwl_scan_offload_profile_match *matches =
1092 			(void *)netdetect_res->matches;
1093 
1094 		for (int k = 0; k < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; k++)
1095 			n_channels +=
1096 				hweight8(matches[i].matching_channels[k]);
1097 		match = kzalloc(struct_size(match, channels, n_channels),
1098 				GFP_KERNEL);
1099 		if (!match)
1100 			return;
1101 
1102 		netdetect_info->matches[netdetect_info->n_matches++] = match;
1103 
1104 		/* We inverted the order of the SSIDs in the scan
1105 		 * request, so invert the index here.
1106 		 */
1107 		idx = netdetect_cfg->n_match_sets - i - 1;
1108 		match->ssid.ssid_len =
1109 			netdetect_cfg->match_sets[idx].ssid.ssid_len;
1110 		memcpy(match->ssid.ssid,
1111 		       netdetect_cfg->match_sets[idx].ssid.ssid,
1112 		       match->ssid.ssid_len);
1113 
1114 		if (netdetect_cfg->n_channels < n_channels)
1115 			continue;
1116 
1117 		for_each_set_bit(j,
1118 				 (unsigned long *)&matches[i].matching_channels[0],
1119 				 sizeof(matches[i].matching_channels))
1120 			match->channels[match->n_channels++] =
1121 				netdetect_cfg->channels[j]->center_freq;
1122 	}
1123 }
1124 
1125 static void
1126 iwl_mld_process_netdetect_res(struct iwl_mld *mld,
1127 			      struct ieee80211_vif *vif,
1128 			      struct iwl_mld_resume_data *resume_data)
1129 {
1130 	struct cfg80211_wowlan_nd_info *netdetect_info = NULL;
1131 	const struct cfg80211_sched_scan_request *netdetect_cfg;
1132 	struct cfg80211_wowlan_wakeup wakeup = {
1133 		.pattern_idx = -1,
1134 	};
1135 	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1136 	unsigned long matched_profiles;
1137 	u32 wakeup_reasons;
1138 	int n_matches;
1139 
1140 	lockdep_assert_wiphy(mld->wiphy);
1141 
1142 	if (WARN_ON(!mld->wiphy->wowlan_config ||
1143 		    !mld->wiphy->wowlan_config->nd_config)) {
1144 		IWL_DEBUG_WOWLAN(mld,
1145 				 "Netdetect isn't configured on resume flow\n");
1146 		goto out;
1147 	}
1148 
1149 	netdetect_cfg = mld->wiphy->wowlan_config->nd_config;
1150 	wakeup_reasons = resume_data->wowlan_status->wakeup_reasons;
1151 
1152 	if (wakeup_reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1153 		wakeup.rfkill_release = true;
1154 
1155 	if (wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1156 		goto out;
1157 
1158 	if (!resume_data->netdetect_res->matched_profiles) {
1159 		IWL_DEBUG_WOWLAN(mld,
1160 				 "Netdetect results aren't valid\n");
1161 		wakeup_report = NULL;
1162 		goto out;
1163 	}
1164 
1165 	matched_profiles = resume_data->netdetect_res->matched_profiles;
1166 	if (!netdetect_cfg->n_match_sets) {
1167 		IWL_DEBUG_WOWLAN(mld,
1168 				 "No netdetect match sets are configured\n");
1169 		goto out;
1170 	}
1171 	n_matches = hweight_long(matched_profiles);
1172 	netdetect_info = kzalloc(struct_size(netdetect_info, matches,
1173 					     n_matches), GFP_KERNEL);
1174 	if (netdetect_info)
1175 		iwl_mld_set_netdetect_info(mld, netdetect_cfg, netdetect_info,
1176 					   resume_data->netdetect_res,
1177 					   matched_profiles);
1178 
1179 	wakeup.net_detect = netdetect_info;
1180  out:
1181 	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1182 	if (netdetect_info) {
1183 		for (int i = 0; i < netdetect_info->n_matches; i++)
1184 			kfree(netdetect_info->matches[i]);
1185 		kfree(netdetect_info);
1186 	}
1187 }
1188 
1189 static bool iwl_mld_handle_d3_notif(struct iwl_notif_wait_data *notif_wait,
1190 				    struct iwl_rx_packet *pkt, void *data)
1191 {
1192 	struct iwl_mld_resume_data *resume_data = data;
1193 	struct iwl_mld *mld =
1194 		container_of(notif_wait, struct iwl_mld, notif_wait);
1195 
1196 	switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
1197 	case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): {
1198 		if (resume_data->notifs_received & IWL_D3_NOTIF_WOWLAN_INFO) {
1199 			IWL_DEBUG_WOWLAN(mld,
1200 					 "got additional wowlan_info notif\n");
1201 			break;
1202 		}
1203 		resume_data->notif_handling_err =
1204 			iwl_mld_handle_wowlan_info_notif(mld,
1205 							 resume_data->wowlan_status,
1206 							 pkt);
1207 		resume_data->notifs_received |= IWL_D3_NOTIF_WOWLAN_INFO;
1208 
1209 		if (resume_data->wowlan_status->wakeup_reasons &
1210 		    IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT)
1211 			resume_data->notifs_expected |=
1212 				IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
1213 		break;
1214 	}
1215 	case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): {
1216 		if (resume_data->notifs_received &
1217 		    IWL_D3_NOTIF_WOWLAN_WAKE_PKT) {
1218 			/* We shouldn't get two wake packet notifications */
1219 			IWL_DEBUG_WOWLAN(mld,
1220 					 "Got additional wowlan wake packet notification\n");
1221 			break;
1222 		}
1223 		resume_data->notif_handling_err =
1224 			iwl_mld_handle_wake_pkt_notif(mld,
1225 						      resume_data->wowlan_status,
1226 						      pkt);
1227 		resume_data->notifs_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
1228 		break;
1229 	}
1230 	case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): {
1231 		if (resume_data->notifs_received & IWL_D3_ND_MATCH_INFO) {
1232 			IWL_ERR(mld,
1233 				"Got additional netdetect match info\n");
1234 			break;
1235 		}
1236 
1237 		resume_data->notif_handling_err =
1238 			iwl_mld_netdetect_match_info_handler(mld, resume_data,
1239 							     pkt);
1240 		resume_data->notifs_received |= IWL_D3_ND_MATCH_INFO;
1241 		break;
1242 	}
1243 	case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): {
1244 		struct iwl_d3_end_notif *notif = (void *)pkt->data;
1245 
1246 		resume_data->d3_end_flags = le32_to_cpu(notif->flags);
1247 		resume_data->notifs_received |= IWL_D3_NOTIF_D3_END_NOTIF;
1248 		break;
1249 	}
1250 	default:
1251 		WARN_ON(1);
1252 	}
1253 
1254 	return resume_data->notifs_received == resume_data->notifs_expected;
1255 }
1256 
1257 #define IWL_MLD_D3_NOTIF_TIMEOUT (HZ / 3)
1258 
1259 static int iwl_mld_wait_d3_notif(struct iwl_mld *mld,
1260 				 struct iwl_mld_resume_data *resume_data,
1261 				 bool with_wowlan)
1262 {
1263 	static const u16 wowlan_resume_notif[] = {
1264 		WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION),
1265 		WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION),
1266 		WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF),
1267 		WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
1268 	};
1269 	static const u16 d3_resume_notif[] = {
1270 		WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
1271 	};
1272 	struct iwl_notification_wait wait_d3_notif;
1273 	enum iwl_d3_status d3_status;
1274 	int ret;
1275 
1276 	if (with_wowlan)
1277 		iwl_init_notification_wait(&mld->notif_wait, &wait_d3_notif,
1278 					   wowlan_resume_notif,
1279 					   ARRAY_SIZE(wowlan_resume_notif),
1280 					   iwl_mld_handle_d3_notif,
1281 					   resume_data);
1282 	else
1283 		iwl_init_notification_wait(&mld->notif_wait, &wait_d3_notif,
1284 					   d3_resume_notif,
1285 					   ARRAY_SIZE(d3_resume_notif),
1286 					   iwl_mld_handle_d3_notif,
1287 					   resume_data);
1288 
1289 	ret = iwl_trans_d3_resume(mld->trans, &d3_status, false, false);
1290 	if (ret || d3_status != IWL_D3_STATUS_ALIVE) {
1291 		if (d3_status != IWL_D3_STATUS_ALIVE) {
1292 			IWL_INFO(mld, "Device was reset during suspend\n");
1293 			ret = -ENOENT;
1294 		} else {
1295 			IWL_ERR(mld, "Transport resume failed\n");
1296 		}
1297 		iwl_remove_notification(&mld->notif_wait, &wait_d3_notif);
1298 		return ret;
1299 	}
1300 
1301 	ret = iwl_wait_notification(&mld->notif_wait, &wait_d3_notif,
1302 				    IWL_MLD_D3_NOTIF_TIMEOUT);
1303 	if (ret)
1304 		IWL_ERR(mld, "Couldn't get the d3 notif %d\n", ret);
1305 
1306 	if (resume_data->notif_handling_err)
1307 		ret = -EIO;
1308 
1309 	return ret;
1310 }
1311 
1312 int iwl_mld_no_wowlan_suspend(struct iwl_mld *mld)
1313 {
1314 	struct iwl_d3_manager_config d3_cfg_cmd_data = {};
1315 	int ret;
1316 
1317 	lockdep_assert_wiphy(mld->wiphy);
1318 
1319 	IWL_DEBUG_WOWLAN(mld, "Starting the no wowlan suspend flow\n");
1320 
1321 	iwl_mld_low_latency_stop(mld);
1322 
1323 	/* This will happen if iwl_mld_supsend failed with FW error */
1324 	if (mld->trans->state == IWL_TRANS_NO_FW &&
1325 	    test_bit(STATUS_FW_ERROR, &mld->trans->status))
1326 		return -ENODEV;
1327 
1328 	ret = iwl_mld_update_device_power(mld, true);
1329 	if (ret) {
1330 		IWL_ERR(mld,
1331 			"d3 suspend: couldn't send power_device %d\n", ret);
1332 		goto out;
1333 	}
1334 
1335 	ret = iwl_mld_send_cmd_pdu(mld, D3_CONFIG_CMD,
1336 				   &d3_cfg_cmd_data);
1337 	if (ret) {
1338 		IWL_ERR(mld,
1339 			"d3 suspend: couldn't send D3_CONFIG_CMD %d\n", ret);
1340 		goto out;
1341 	}
1342 
1343 	ret = iwl_trans_d3_suspend(mld->trans, false, false);
1344 	if (ret) {
1345 		IWL_ERR(mld, "d3 suspend: trans_d3_suspend failed %d\n", ret);
1346 	} else {
1347 		mld->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1348 		mld->fw_status.in_d3 = true;
1349 	}
1350 
1351  out:
1352 	if (ret) {
1353 		mld->trans->state = IWL_TRANS_NO_FW;
1354 		set_bit(STATUS_FW_ERROR, &mld->trans->status);
1355 	}
1356 
1357 	return ret;
1358 }
1359 
1360 int iwl_mld_no_wowlan_resume(struct iwl_mld *mld)
1361 {
1362 	struct iwl_mld_resume_data resume_data = {
1363 		.notifs_expected =
1364 			IWL_D3_NOTIF_D3_END_NOTIF,
1365 	};
1366 	int ret;
1367 
1368 	lockdep_assert_wiphy(mld->wiphy);
1369 
1370 	IWL_DEBUG_WOWLAN(mld, "Starting the no wowlan resume flow\n");
1371 
1372 	mld->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
1373 	mld->fw_status.in_d3 = false;
1374 	iwl_fw_dbg_read_d3_debug_data(&mld->fwrt);
1375 
1376 	if (iwl_mld_fw_needs_restart(mld, NULL))
1377 		ret = -ENODEV;
1378 	else
1379 		ret = iwl_mld_wait_d3_notif(mld, &resume_data, false);
1380 
1381 	if (!ret && (resume_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE))
1382 		return -ENODEV;
1383 
1384 	if (ret) {
1385 		mld->trans->state = IWL_TRANS_NO_FW;
1386 		set_bit(STATUS_FW_ERROR, &mld->trans->status);
1387 		return ret;
1388 	}
1389 	iwl_mld_low_latency_restart(mld);
1390 
1391 	return iwl_mld_update_device_power(mld, false);
1392 }
1393 
1394 static void
1395 iwl_mld_aes_seq_to_le64_pn(struct ieee80211_key_conf *key,
1396 			   __le64 *key_rsc)
1397 {
1398 	for (int i = 0; i < IWL_MAX_TID_COUNT; i++) {
1399 		struct ieee80211_key_seq seq;
1400 		u8 *pn = key->cipher == WLAN_CIPHER_SUITE_CCMP ? seq.ccmp.pn :
1401 			seq.gcmp.pn;
1402 
1403 		ieee80211_get_key_rx_seq(key, i, &seq);
1404 		key_rsc[i] = cpu_to_le64((u64)pn[5] |
1405 					 ((u64)pn[4] << 8) |
1406 					 ((u64)pn[3] << 16) |
1407 					 ((u64)pn[2] << 24) |
1408 					 ((u64)pn[1] << 32) |
1409 					 ((u64)pn[0] << 40));
1410 	}
1411 }
1412 
1413 static void
1414 iwl_mld_suspend_set_ucast_pn(struct iwl_mld *mld, struct ieee80211_sta *sta,
1415 			     struct ieee80211_key_conf *key, __le64 *key_rsc)
1416 {
1417 	struct iwl_mld_sta *mld_sta =
1418 		iwl_mld_sta_from_mac80211(sta);
1419 	struct iwl_mld_ptk_pn *mld_ptk_pn;
1420 
1421 	if (WARN_ON(key->keyidx >= ARRAY_SIZE(mld_sta->ptk_pn)))
1422 		return;
1423 
1424 	mld_ptk_pn = wiphy_dereference(mld->wiphy,
1425 				       mld_sta->ptk_pn[key->keyidx]);
1426 	if (WARN_ON(!mld_ptk_pn))
1427 		return;
1428 
1429 	for (int tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1430 		struct ieee80211_key_seq seq;
1431 		u8 *max_pn = seq.ccmp.pn;
1432 
1433 		/* get the PN from mac80211, used on the default queue */
1434 		ieee80211_get_key_rx_seq(key, tid, &seq);
1435 
1436 		/* and use the internal data for all queues */
1437 		for (int que = 1; que < mld->trans->num_rx_queues; que++) {
1438 			u8 *cur_pn = mld_ptk_pn->q[que].pn[tid];
1439 
1440 			if (memcmp(max_pn, cur_pn, IEEE80211_CCMP_PN_LEN) < 0)
1441 				max_pn = cur_pn;
1442 		}
1443 		key_rsc[tid] = cpu_to_le64((u64)max_pn[5] |
1444 					   ((u64)max_pn[4] << 8) |
1445 					   ((u64)max_pn[3] << 16) |
1446 					   ((u64)max_pn[2] << 24) |
1447 					   ((u64)max_pn[1] << 32) |
1448 					   ((u64)max_pn[0] << 40));
1449 	}
1450 }
1451 
1452 static void
1453 iwl_mld_suspend_convert_tkip_ipn(struct ieee80211_key_conf *key,
1454 				 __le64 *rsc)
1455 {
1456 	struct ieee80211_key_seq seq;
1457 
1458 	for (int i = 0; i < IWL_MAX_TID_COUNT; i++) {
1459 		ieee80211_get_key_rx_seq(key, i, &seq);
1460 		rsc[i] =
1461 			cpu_to_le64(((u64)seq.tkip.iv32 << 16) |
1462 				    seq.tkip.iv16);
1463 	}
1464 }
1465 
1466 static void
1467 iwl_mld_suspend_key_data_iter(struct ieee80211_hw *hw,
1468 			      struct ieee80211_vif *vif,
1469 			      struct ieee80211_sta *sta,
1470 			      struct ieee80211_key_conf *key,
1471 			      void *_data)
1472 {
1473 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1474 	struct iwl_mld_suspend_key_iter_data *data = _data;
1475 	__le64 *key_rsc;
1476 	__le32 cipher = 0;
1477 
1478 	switch (key->cipher) {
1479 	case WLAN_CIPHER_SUITE_CCMP:
1480 		cipher = cpu_to_le32(STA_KEY_FLG_CCM);
1481 		fallthrough;
1482 	case WLAN_CIPHER_SUITE_GCMP:
1483 	case WLAN_CIPHER_SUITE_GCMP_256:
1484 		if (!cipher)
1485 			cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
1486 		fallthrough;
1487 	case WLAN_CIPHER_SUITE_TKIP:
1488 		if (!cipher)
1489 			cipher = cpu_to_le32(STA_KEY_FLG_TKIP);
1490 		if (sta) {
1491 			key_rsc = data->rsc->ucast_rsc;
1492 			if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1493 				iwl_mld_suspend_convert_tkip_ipn(key, key_rsc);
1494 			else
1495 				iwl_mld_suspend_set_ucast_pn(mld, sta, key,
1496 							     key_rsc);
1497 
1498 			data->have_rsc = true;
1499 			return;
1500 		}
1501 		/* We're iterating from old to new, there're 4 possible
1502 		 * gtk ids, and only the last two keys matter
1503 		 */
1504 		if (WARN_ON(data->gtks >=
1505 				ARRAY_SIZE(data->found_gtk_idx)))
1506 			return;
1507 
1508 		if (WARN_ON(key->keyidx >=
1509 				ARRAY_SIZE(data->rsc->mcast_key_id_map)))
1510 			return;
1511 		data->gtk_cipher = cipher;
1512 		data->found_gtk_idx[data->gtks] = key->keyidx;
1513 		key_rsc = data->rsc->mcast_rsc[data->gtks % 2];
1514 		data->rsc->mcast_key_id_map[key->keyidx] =
1515 			data->gtks % 2;
1516 
1517 		if (data->gtks >= 2) {
1518 			int prev = data->gtks % 2;
1519 			int prev_idx = data->found_gtk_idx[prev];
1520 
1521 			data->rsc->mcast_key_id_map[prev_idx] =
1522 				IWL_MCAST_KEY_MAP_INVALID;
1523 		}
1524 
1525 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1526 			iwl_mld_suspend_convert_tkip_ipn(key, key_rsc);
1527 		else
1528 			iwl_mld_aes_seq_to_le64_pn(key, key_rsc);
1529 
1530 		data->gtks++;
1531 		data->have_rsc = true;
1532 		break;
1533 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1534 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1535 		cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
1536 		fallthrough;
1537 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1538 	case WLAN_CIPHER_SUITE_AES_CMAC:
1539 		if (!cipher)
1540 			cipher = cpu_to_le32(STA_KEY_FLG_CCM);
1541 		if (key->keyidx == 4 || key->keyidx == 5)
1542 			data->igtk_cipher = cipher;
1543 
1544 		if (key->keyidx == 6 || key->keyidx == 7)
1545 			data->bigtk_cipher = cipher;
1546 
1547 		break;
1548 	}
1549 }
1550 
1551 static int
1552 iwl_mld_send_kek_kck_cmd(struct iwl_mld *mld,
1553 			 struct iwl_mld_vif *mld_vif,
1554 			 struct iwl_mld_suspend_key_iter_data data,
1555 			 int ap_sta_id)
1556 {
1557 	struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {};
1558 	struct iwl_mld_rekey_data *rekey_data =
1559 		&mld_vif->wowlan_data.rekey_data;
1560 
1561 	memcpy(kek_kck_cmd.kck, rekey_data->kck,
1562 	       rekey_data->kck_len);
1563 	kek_kck_cmd.kck_len = cpu_to_le16(rekey_data->kck_len);
1564 	memcpy(kek_kck_cmd.kek, rekey_data->kek,
1565 	       rekey_data->kek_len);
1566 	kek_kck_cmd.kek_len = cpu_to_le16(rekey_data->kek_len);
1567 	kek_kck_cmd.replay_ctr = rekey_data->replay_ctr;
1568 	kek_kck_cmd.akm = cpu_to_le32(rekey_data->akm);
1569 	kek_kck_cmd.sta_id = cpu_to_le32(ap_sta_id);
1570 	kek_kck_cmd.gtk_cipher = data.gtk_cipher;
1571 	kek_kck_cmd.igtk_cipher = data.igtk_cipher;
1572 	kek_kck_cmd.bigtk_cipher = data.bigtk_cipher;
1573 
1574 	IWL_DEBUG_WOWLAN(mld, "setting akm %d\n",
1575 			 rekey_data->akm);
1576 
1577 	return iwl_mld_send_cmd_pdu(mld, WOWLAN_KEK_KCK_MATERIAL,
1578 				    &kek_kck_cmd);
1579 }
1580 
1581 static int
1582 iwl_mld_suspend_send_security_cmds(struct iwl_mld *mld,
1583 				   struct ieee80211_vif *vif,
1584 				   struct iwl_mld_vif *mld_vif,
1585 				   int ap_sta_id)
1586 {
1587 	struct iwl_mld_suspend_key_iter_data data = {};
1588 	int ret;
1589 
1590 	data.rsc = kzalloc(sizeof(*data.rsc), GFP_KERNEL);
1591 	if (!data.rsc)
1592 		return -ENOMEM;
1593 
1594 	memset(data.rsc->mcast_key_id_map, IWL_MCAST_KEY_MAP_INVALID,
1595 	       ARRAY_SIZE(data.rsc->mcast_key_id_map));
1596 
1597 	data.rsc->sta_id = cpu_to_le32(ap_sta_id);
1598 	ieee80211_iter_keys(mld->hw, vif,
1599 			    iwl_mld_suspend_key_data_iter,
1600 			    &data);
1601 
1602 	if (data.have_rsc)
1603 		ret = iwl_mld_send_cmd_pdu(mld, WOWLAN_TSC_RSC_PARAM,
1604 					   data.rsc);
1605 	else
1606 		ret = 0;
1607 
1608 	if (!ret && mld_vif->wowlan_data.rekey_data.valid)
1609 		ret = iwl_mld_send_kek_kck_cmd(mld, mld_vif, data, ap_sta_id);
1610 
1611 	kfree(data.rsc);
1612 
1613 	return ret;
1614 }
1615 
1616 static void
1617 iwl_mld_set_wowlan_config_cmd(struct iwl_mld *mld,
1618 			      struct cfg80211_wowlan *wowlan,
1619 			      struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1620 			      struct ieee80211_sta *ap_sta)
1621 {
1622 	wowlan_config_cmd->is_11n_connection =
1623 					ap_sta->deflink.ht_cap.ht_supported;
1624 	wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
1625 		ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
1626 
1627 	if (ap_sta->mfp)
1628 		wowlan_config_cmd->flags |= IS_11W_ASSOC;
1629 
1630 	if (wowlan->disconnect)
1631 		wowlan_config_cmd->wakeup_filter |=
1632 			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
1633 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
1634 	if (wowlan->magic_pkt)
1635 		wowlan_config_cmd->wakeup_filter |=
1636 			cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
1637 	if (wowlan->gtk_rekey_failure)
1638 		wowlan_config_cmd->wakeup_filter |=
1639 			cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
1640 	if (wowlan->eap_identity_req)
1641 		wowlan_config_cmd->wakeup_filter |=
1642 			cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
1643 	if (wowlan->four_way_handshake)
1644 		wowlan_config_cmd->wakeup_filter |=
1645 			cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
1646 	if (wowlan->n_patterns)
1647 		wowlan_config_cmd->wakeup_filter |=
1648 			cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
1649 
1650 	if (wowlan->rfkill_release)
1651 		wowlan_config_cmd->wakeup_filter |=
1652 			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
1653 
1654 	if (wowlan->any) {
1655 		wowlan_config_cmd->wakeup_filter |=
1656 			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
1657 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE |
1658 				    IWL_WOWLAN_WAKEUP_RX_FRAME |
1659 				    IWL_WOWLAN_WAKEUP_BCN_FILTERING);
1660 	}
1661 }
1662 
1663 static int iwl_mld_send_patterns(struct iwl_mld *mld,
1664 				 struct cfg80211_wowlan *wowlan,
1665 				 int ap_sta_id)
1666 {
1667 	struct iwl_wowlan_patterns_cmd *pattern_cmd;
1668 	struct iwl_host_cmd cmd = {
1669 		.id = WOWLAN_PATTERNS,
1670 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1671 	};
1672 	int ret;
1673 
1674 	if (!wowlan->n_patterns)
1675 		return 0;
1676 
1677 	cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns);
1678 
1679 	pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL);
1680 	if (!pattern_cmd)
1681 		return -ENOMEM;
1682 
1683 	pattern_cmd->n_patterns = wowlan->n_patterns;
1684 	pattern_cmd->sta_id = ap_sta_id;
1685 
1686 	for (int i = 0; i < wowlan->n_patterns; i++) {
1687 		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
1688 
1689 		pattern_cmd->patterns[i].pattern_type =
1690 			WOWLAN_PATTERN_TYPE_BITMASK;
1691 
1692 		memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
1693 		       wowlan->patterns[i].mask, mask_len);
1694 		memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
1695 		       wowlan->patterns[i].pattern,
1696 		       wowlan->patterns[i].pattern_len);
1697 		pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
1698 		pattern_cmd->patterns[i].u.bitmask.pattern_size =
1699 			wowlan->patterns[i].pattern_len;
1700 	}
1701 
1702 	cmd.data[0] = pattern_cmd;
1703 	ret = iwl_mld_send_cmd(mld, &cmd);
1704 	kfree(pattern_cmd);
1705 	return ret;
1706 }
1707 
1708 static int
1709 iwl_mld_send_proto_offload(struct iwl_mld *mld,
1710 			   struct ieee80211_vif *vif,
1711 			   u8 ap_sta_id)
1712 {
1713 	struct iwl_proto_offload_cmd_v4 *cmd __free(kfree);
1714 	struct iwl_host_cmd hcmd = {
1715 		.id = PROT_OFFLOAD_CONFIG_CMD,
1716 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1717 		.len[0] = sizeof(*cmd),
1718 	};
1719 	u32 enabled = 0;
1720 
1721 	cmd = kzalloc(hcmd.len[0], GFP_KERNEL);
1722 
1723 #if IS_ENABLED(CONFIG_IPV6)
1724 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1725 	struct iwl_mld_wowlan_data *wowlan_data = &mld_vif->wowlan_data;
1726 	struct iwl_ns_config *nsc;
1727 	struct iwl_targ_addr *addrs;
1728 	int n_nsc, n_addrs;
1729 	int i, c;
1730 	int num_skipped = 0;
1731 
1732 	nsc = cmd->ns_config;
1733 	n_nsc = IWL_PROTO_OFFLOAD_NUM_NS_CONFIG_V3L;
1734 	addrs = cmd->targ_addrs;
1735 	n_addrs = IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V3L;
1736 
1737 	/* For each address we have (and that will fit) fill a target
1738 	 * address struct and combine for NS offload structs with the
1739 	 * solicited node addresses.
1740 	 */
1741 	for (i = 0, c = 0;
1742 		i < wowlan_data->num_target_ipv6_addrs &&
1743 		i < n_addrs && c < n_nsc; i++) {
1744 		int j;
1745 		struct in6_addr solicited_addr;
1746 
1747 		/* Because ns is offloaded skip tentative address to avoid
1748 		 * violating RFC4862.
1749 		 */
1750 		if (test_bit(i, wowlan_data->tentative_addrs)) {
1751 			num_skipped++;
1752 			continue;
1753 		}
1754 
1755 		addrconf_addr_solict_mult(&wowlan_data->target_ipv6_addrs[i],
1756 					  &solicited_addr);
1757 		for (j = 0; j < c; j++)
1758 			if (ipv6_addr_cmp(&nsc[j].dest_ipv6_addr,
1759 					  &solicited_addr) == 0)
1760 				break;
1761 		if (j == c)
1762 			c++;
1763 		addrs[i].addr = wowlan_data->target_ipv6_addrs[i];
1764 		addrs[i].config_num = cpu_to_le32(j);
1765 		nsc[j].dest_ipv6_addr = solicited_addr;
1766 		memcpy(nsc[j].target_mac_addr, vif->addr, ETH_ALEN);
1767 	}
1768 
1769 	if (wowlan_data->num_target_ipv6_addrs - num_skipped)
1770 		enabled |= IWL_D3_PROTO_IPV6_VALID;
1771 
1772 	cmd->num_valid_ipv6_addrs = cpu_to_le32(i - num_skipped);
1773 	if (enabled & IWL_D3_PROTO_IPV6_VALID)
1774 		enabled |= IWL_D3_PROTO_OFFLOAD_NS;
1775 #endif
1776 
1777 	if (vif->cfg.arp_addr_cnt) {
1778 		enabled |= IWL_D3_PROTO_OFFLOAD_ARP | IWL_D3_PROTO_IPV4_VALID;
1779 		cmd->common.host_ipv4_addr = vif->cfg.arp_addr_list[0];
1780 		ether_addr_copy(cmd->common.arp_mac_addr, vif->addr);
1781 	}
1782 
1783 	enabled |= IWL_D3_PROTO_OFFLOAD_BTM;
1784 	cmd->common.enabled = cpu_to_le32(enabled);
1785 	cmd->sta_id = cpu_to_le32(ap_sta_id);
1786 	hcmd.data[0] = cmd;
1787 	return iwl_mld_send_cmd(mld, &hcmd);
1788 }
1789 
1790 static int
1791 iwl_mld_wowlan_config(struct iwl_mld *mld, struct ieee80211_vif *bss_vif,
1792 		      struct cfg80211_wowlan *wowlan)
1793 {
1794 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(bss_vif);
1795 	struct ieee80211_sta *ap_sta = mld_vif->ap_sta;
1796 	struct iwl_wowlan_config_cmd wowlan_config_cmd = {
1797 			.offloading_tid = IWL_WOWLAN_OFFLOAD_TID,
1798 	};
1799 	u32 sta_id_mask;
1800 	int ap_sta_id, ret;
1801 	int link_id = iwl_mld_get_primary_link(bss_vif);
1802 	struct ieee80211_bss_conf *link_conf;
1803 
1804 	ret = iwl_mld_block_emlsr_sync(mld, bss_vif,
1805 				       IWL_MLD_EMLSR_BLOCKED_WOWLAN, link_id);
1806 	if (ret)
1807 		return ret;
1808 
1809 	link_conf = link_conf_dereference_protected(bss_vif, link_id);
1810 
1811 	if (WARN_ON(!ap_sta || !link_conf))
1812 		return -EINVAL;
1813 
1814 	sta_id_mask = iwl_mld_fw_sta_id_mask(mld, ap_sta);
1815 	if (WARN_ON(hweight32(sta_id_mask) != 1))
1816 		return -EINVAL;
1817 
1818 	ap_sta_id = __ffs(sta_id_mask);
1819 	wowlan_config_cmd.sta_id = ap_sta_id;
1820 
1821 	ret = iwl_mld_ensure_queue(mld,
1822 				   ap_sta->txq[wowlan_config_cmd.offloading_tid]);
1823 	if (ret)
1824 		return ret;
1825 
1826 	iwl_mld_set_wowlan_config_cmd(mld, wowlan,
1827 				      &wowlan_config_cmd, ap_sta);
1828 	ret = iwl_mld_send_cmd_pdu(mld, WOWLAN_CONFIGURATION,
1829 				   &wowlan_config_cmd);
1830 	if (ret)
1831 		return ret;
1832 
1833 	ret = iwl_mld_suspend_send_security_cmds(mld, bss_vif, mld_vif,
1834 						 ap_sta_id);
1835 	if (ret)
1836 		return ret;
1837 
1838 	ret = iwl_mld_send_patterns(mld, wowlan, ap_sta_id);
1839 	if (ret)
1840 		return ret;
1841 
1842 	ret = iwl_mld_send_proto_offload(mld, bss_vif, ap_sta_id);
1843 	if (ret)
1844 		return ret;
1845 
1846 	iwl_mld_enable_beacon_filter(mld, link_conf, true);
1847 	return iwl_mld_update_mac_power(mld, bss_vif, true);
1848 }
1849 
1850 int iwl_mld_wowlan_suspend(struct iwl_mld *mld, struct cfg80211_wowlan *wowlan)
1851 {
1852 	struct ieee80211_vif *bss_vif;
1853 
1854 	lockdep_assert_wiphy(mld->wiphy);
1855 
1856 	if (WARN_ON(!wowlan))
1857 		return 1;
1858 
1859 	IWL_DEBUG_WOWLAN(mld, "Starting the wowlan suspend flow\n");
1860 
1861 	bss_vif = iwl_mld_get_bss_vif(mld);
1862 	if (WARN_ON(!bss_vif))
1863 		return 1;
1864 
1865 	if (!bss_vif->cfg.assoc) {
1866 		int ret;
1867 		/* If we're not associated, this must be netdetect */
1868 		if (WARN_ON(!wowlan->nd_config))
1869 			return 1;
1870 
1871 		ret = iwl_mld_netdetect_config(mld, bss_vif, wowlan);
1872 		if (!ret)
1873 			mld->netdetect = true;
1874 
1875 		return ret;
1876 	}
1877 
1878 	return iwl_mld_wowlan_config(mld, bss_vif, wowlan);
1879 }
1880 
1881 /* Returns 0 on success, 1 if an error occurred in firmware during d3,
1882  * A negative value is expected only in unrecovreable cases.
1883  */
1884 int iwl_mld_wowlan_resume(struct iwl_mld *mld)
1885 {
1886 	struct ieee80211_vif *bss_vif;
1887 	struct ieee80211_bss_conf *link_conf;
1888 	struct iwl_mld_netdetect_res netdetect_res;
1889 	struct iwl_mld_resume_data resume_data = {
1890 		.notifs_expected =
1891 			IWL_D3_NOTIF_WOWLAN_INFO |
1892 			IWL_D3_NOTIF_D3_END_NOTIF,
1893 		.netdetect_res = &netdetect_res,
1894 	};
1895 	int link_id;
1896 	int ret;
1897 	bool fw_err = false;
1898 	bool keep_connection;
1899 
1900 	lockdep_assert_wiphy(mld->wiphy);
1901 
1902 	IWL_DEBUG_WOWLAN(mld, "Starting the wowlan resume flow\n");
1903 
1904 	mld->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
1905 	if (!mld->fw_status.in_d3) {
1906 		IWL_DEBUG_WOWLAN(mld,
1907 				 "Device_powered_off() was called during wowlan\n");
1908 		goto err;
1909 	}
1910 
1911 	mld->fw_status.in_d3 = false;
1912 	mld->scan.last_start_time_jiffies = jiffies;
1913 
1914 	bss_vif = iwl_mld_get_bss_vif(mld);
1915 	if (WARN_ON(!bss_vif))
1916 		goto err;
1917 
1918 	/* We can't have several links upon wowlan entry,
1919 	 * this is enforced in the suspend flow.
1920 	 */
1921 	WARN_ON(hweight16(bss_vif->active_links) > 1);
1922 	link_id = bss_vif->active_links ? __ffs(bss_vif->active_links) : 0;
1923 	link_conf = link_conf_dereference_protected(bss_vif, link_id);
1924 
1925 	if (WARN_ON(!link_conf))
1926 		goto err;
1927 
1928 	iwl_fw_dbg_read_d3_debug_data(&mld->fwrt);
1929 
1930 	if (iwl_mld_fw_needs_restart(mld, bss_vif)) {
1931 		fw_err = true;
1932 		goto err;
1933 	}
1934 
1935 	resume_data.wowlan_status = kzalloc(sizeof(*resume_data.wowlan_status),
1936 					    GFP_KERNEL);
1937 	if (!resume_data.wowlan_status)
1938 		return -1;
1939 
1940 	if (mld->netdetect)
1941 		resume_data.notifs_expected |= IWL_D3_ND_MATCH_INFO;
1942 
1943 	ret = iwl_mld_wait_d3_notif(mld, &resume_data, true);
1944 	if (ret) {
1945 		IWL_ERR(mld, "Couldn't get the d3 notifs %d\n", ret);
1946 		fw_err = true;
1947 		goto err;
1948 	}
1949 
1950 	if (resume_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE) {
1951 		mld->fw_status.in_hw_restart = true;
1952 		goto process_wakeup_results;
1953 	}
1954 
1955 	iwl_mld_update_changed_regdomain(mld);
1956 	iwl_mld_update_mac_power(mld, bss_vif, false);
1957 	iwl_mld_enable_beacon_filter(mld, link_conf, false);
1958 	iwl_mld_update_device_power(mld, false);
1959 
1960 	if (mld->netdetect)
1961 		ret = iwl_mld_scan_stop(mld, IWL_MLD_SCAN_NETDETECT, false);
1962 
1963  process_wakeup_results:
1964 	if (mld->netdetect) {
1965 		iwl_mld_process_netdetect_res(mld, bss_vif, &resume_data);
1966 		mld->netdetect = false;
1967 	} else {
1968 		keep_connection =
1969 			iwl_mld_process_wowlan_status(mld, bss_vif,
1970 						      resume_data.wowlan_status);
1971 
1972 		/* EMLSR state will be cleared if the connection is not kept */
1973 		if (keep_connection)
1974 			iwl_mld_unblock_emlsr(mld, bss_vif,
1975 					      IWL_MLD_EMLSR_BLOCKED_WOWLAN);
1976 	}
1977 
1978 	if (!mld->netdetect && !keep_connection)
1979 		ieee80211_resume_disconnect(bss_vif);
1980 
1981 	goto out;
1982 
1983  err:
1984 	if (fw_err) {
1985 		mld->trans->state = IWL_TRANS_NO_FW;
1986 		set_bit(STATUS_FW_ERROR, &mld->trans->status);
1987 	}
1988 
1989 	mld->fw_status.in_hw_restart = true;
1990 	ret = 1;
1991  out:
1992 	if (resume_data.wowlan_status) {
1993 		kfree(resume_data.wowlan_status->wake_packet);
1994 		kfree(resume_data.wowlan_status);
1995 	}
1996 
1997 	return ret;
1998 }
1999