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