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