xref: /linux/drivers/net/wireless/intel/iwlwifi/mld/d3.c (revision b2e4bccc55b138616cce9127c78e4eaded240abc)
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 	enum iwl_d3_status d3_status;
1199 	int ret;
1200 
1201 	if (with_wowlan)
1202 		iwl_init_notification_wait(&mld->notif_wait, &wait_d3_notif,
1203 					   wowlan_resume_notif,
1204 					   ARRAY_SIZE(wowlan_resume_notif),
1205 					   iwl_mld_handle_d3_notif,
1206 					   resume_data);
1207 	else
1208 		iwl_init_notification_wait(&mld->notif_wait, &wait_d3_notif,
1209 					   d3_resume_notif,
1210 					   ARRAY_SIZE(d3_resume_notif),
1211 					   iwl_mld_handle_d3_notif,
1212 					   resume_data);
1213 
1214 	ret = iwl_trans_d3_resume(mld->trans, &d3_status, false);
1215 	if (ret || d3_status != IWL_D3_STATUS_ALIVE) {
1216 		if (d3_status != IWL_D3_STATUS_ALIVE) {
1217 			IWL_INFO(mld, "Device was reset during suspend\n");
1218 			ret = -ENOENT;
1219 		} else {
1220 			IWL_ERR(mld, "Transport resume failed\n");
1221 		}
1222 		iwl_remove_notification(&mld->notif_wait, &wait_d3_notif);
1223 		return ret;
1224 	}
1225 
1226 	ret = iwl_wait_notification(&mld->notif_wait, &wait_d3_notif,
1227 				    IWL_MLD_D3_NOTIF_TIMEOUT);
1228 	if (ret)
1229 		IWL_ERR(mld, "Couldn't get the d3 notif %d\n", ret);
1230 
1231 	if (resume_data->notif_handling_err)
1232 		ret = -EIO;
1233 
1234 	return ret;
1235 }
1236 
1237 int iwl_mld_no_wowlan_suspend(struct iwl_mld *mld)
1238 {
1239 	struct iwl_d3_manager_config d3_cfg_cmd_data = {};
1240 	int ret;
1241 
1242 	if (mld->debug_max_sleep) {
1243 		d3_cfg_cmd_data.wakeup_host_timer =
1244 			cpu_to_le32(mld->debug_max_sleep);
1245 		d3_cfg_cmd_data.wakeup_flags =
1246 			cpu_to_le32(IWL_WAKEUP_D3_HOST_TIMER);
1247 	}
1248 
1249 	lockdep_assert_wiphy(mld->wiphy);
1250 
1251 	IWL_DEBUG_WOWLAN(mld, "Starting the no wowlan suspend flow\n");
1252 
1253 	iwl_mld_low_latency_stop(mld);
1254 
1255 	/* This will happen if iwl_mld_supsend failed with FW error */
1256 	if (mld->trans->state == IWL_TRANS_NO_FW &&
1257 	    test_bit(STATUS_FW_ERROR, &mld->trans->status))
1258 		return -ENODEV;
1259 
1260 	ret = iwl_mld_update_device_power(mld, true);
1261 	if (ret) {
1262 		IWL_ERR(mld,
1263 			"d3 suspend: couldn't send power_device %d\n", ret);
1264 		goto out;
1265 	}
1266 
1267 	ret = iwl_mld_send_cmd_pdu(mld, D3_CONFIG_CMD,
1268 				   &d3_cfg_cmd_data);
1269 	if (ret) {
1270 		IWL_ERR(mld,
1271 			"d3 suspend: couldn't send D3_CONFIG_CMD %d\n", ret);
1272 		goto out;
1273 	}
1274 
1275 	ret = iwl_trans_d3_suspend(mld->trans, false);
1276 	if (ret) {
1277 		IWL_ERR(mld, "d3 suspend: trans_d3_suspend failed %d\n", ret);
1278 	} else {
1279 		/* Async notification might send hcmds, which is not allowed in suspend */
1280 		iwl_mld_cancel_async_notifications(mld);
1281 		mld->fw_status.in_d3 = true;
1282 	}
1283 
1284  out:
1285 	if (ret) {
1286 		mld->trans->state = IWL_TRANS_NO_FW;
1287 		set_bit(STATUS_FW_ERROR, &mld->trans->status);
1288 	}
1289 
1290 	return ret;
1291 }
1292 
1293 int iwl_mld_no_wowlan_resume(struct iwl_mld *mld)
1294 {
1295 	struct iwl_mld_resume_data resume_data = {
1296 		.notifs_expected =
1297 			IWL_D3_NOTIF_D3_END_NOTIF,
1298 	};
1299 	int ret;
1300 
1301 	lockdep_assert_wiphy(mld->wiphy);
1302 
1303 	IWL_DEBUG_WOWLAN(mld, "Starting the no wowlan resume flow\n");
1304 
1305 	mld->fw_status.in_d3 = false;
1306 	iwl_fw_dbg_read_d3_debug_data(&mld->fwrt);
1307 
1308 	ret = iwl_mld_wait_d3_notif(mld, &resume_data, false);
1309 
1310 	if (!ret && (resume_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE))
1311 		return -ENODEV;
1312 
1313 	if (ret) {
1314 		mld->trans->state = IWL_TRANS_NO_FW;
1315 		set_bit(STATUS_FW_ERROR, &mld->trans->status);
1316 		return ret;
1317 	}
1318 	iwl_mld_low_latency_restart(mld);
1319 
1320 	return iwl_mld_update_device_power(mld, false);
1321 }
1322 
1323 static void
1324 iwl_mld_aes_seq_to_le64_pn(struct ieee80211_key_conf *key,
1325 			   __le64 *key_rsc)
1326 {
1327 	for (int i = 0; i < IWL_MAX_TID_COUNT; i++) {
1328 		struct ieee80211_key_seq seq;
1329 		u8 *pn = key->cipher == WLAN_CIPHER_SUITE_CCMP ? seq.ccmp.pn :
1330 			seq.gcmp.pn;
1331 
1332 		ieee80211_get_key_rx_seq(key, i, &seq);
1333 		key_rsc[i] = cpu_to_le64((u64)pn[5] |
1334 					 ((u64)pn[4] << 8) |
1335 					 ((u64)pn[3] << 16) |
1336 					 ((u64)pn[2] << 24) |
1337 					 ((u64)pn[1] << 32) |
1338 					 ((u64)pn[0] << 40));
1339 	}
1340 }
1341 
1342 static void
1343 iwl_mld_suspend_set_ucast_pn(struct iwl_mld *mld, struct ieee80211_sta *sta,
1344 			     struct ieee80211_key_conf *key, __le64 *key_rsc)
1345 {
1346 	struct iwl_mld_sta *mld_sta =
1347 		iwl_mld_sta_from_mac80211(sta);
1348 	struct iwl_mld_ptk_pn *mld_ptk_pn;
1349 
1350 	if (WARN_ON(key->keyidx >= ARRAY_SIZE(mld_sta->ptk_pn)))
1351 		return;
1352 
1353 	mld_ptk_pn = wiphy_dereference(mld->wiphy,
1354 				       mld_sta->ptk_pn[key->keyidx]);
1355 	if (WARN_ON(!mld_ptk_pn))
1356 		return;
1357 
1358 	for (int tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1359 		struct ieee80211_key_seq seq;
1360 		u8 *max_pn = seq.ccmp.pn;
1361 
1362 		/* get the PN from mac80211, used on the default queue */
1363 		ieee80211_get_key_rx_seq(key, tid, &seq);
1364 
1365 		/* and use the internal data for all queues */
1366 		for (int que = 1; que < mld->trans->info.num_rxqs; que++) {
1367 			u8 *cur_pn = mld_ptk_pn->q[que].pn[tid];
1368 
1369 			if (memcmp(max_pn, cur_pn, IEEE80211_CCMP_PN_LEN) < 0)
1370 				max_pn = cur_pn;
1371 		}
1372 		key_rsc[tid] = cpu_to_le64((u64)max_pn[5] |
1373 					   ((u64)max_pn[4] << 8) |
1374 					   ((u64)max_pn[3] << 16) |
1375 					   ((u64)max_pn[2] << 24) |
1376 					   ((u64)max_pn[1] << 32) |
1377 					   ((u64)max_pn[0] << 40));
1378 	}
1379 }
1380 
1381 static void
1382 iwl_mld_suspend_convert_tkip_ipn(struct ieee80211_key_conf *key,
1383 				 __le64 *rsc)
1384 {
1385 	struct ieee80211_key_seq seq;
1386 
1387 	for (int i = 0; i < IWL_MAX_TID_COUNT; i++) {
1388 		ieee80211_get_key_rx_seq(key, i, &seq);
1389 		rsc[i] =
1390 			cpu_to_le64(((u64)seq.tkip.iv32 << 16) |
1391 				    seq.tkip.iv16);
1392 	}
1393 }
1394 
1395 static void
1396 iwl_mld_suspend_key_data_iter(struct ieee80211_hw *hw,
1397 			      struct ieee80211_vif *vif,
1398 			      struct ieee80211_sta *sta,
1399 			      struct ieee80211_key_conf *key,
1400 			      void *_data)
1401 {
1402 	struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw);
1403 	struct iwl_mld_suspend_key_iter_data *data = _data;
1404 	__le64 *key_rsc;
1405 	__le32 cipher = 0;
1406 
1407 	switch (key->cipher) {
1408 	case WLAN_CIPHER_SUITE_CCMP:
1409 		cipher = cpu_to_le32(STA_KEY_FLG_CCM);
1410 		fallthrough;
1411 	case WLAN_CIPHER_SUITE_GCMP:
1412 	case WLAN_CIPHER_SUITE_GCMP_256:
1413 		if (!cipher)
1414 			cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
1415 		fallthrough;
1416 	case WLAN_CIPHER_SUITE_TKIP:
1417 		if (!cipher)
1418 			cipher = cpu_to_le32(STA_KEY_FLG_TKIP);
1419 		if (sta) {
1420 			key_rsc = data->rsc->ucast_rsc;
1421 			if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1422 				iwl_mld_suspend_convert_tkip_ipn(key, key_rsc);
1423 			else
1424 				iwl_mld_suspend_set_ucast_pn(mld, sta, key,
1425 							     key_rsc);
1426 
1427 			data->have_rsc = true;
1428 			return;
1429 		}
1430 		/* We're iterating from old to new, there're 4 possible
1431 		 * gtk ids, and only the last two keys matter
1432 		 */
1433 		if (WARN_ON(data->gtks >=
1434 				ARRAY_SIZE(data->found_gtk_idx)))
1435 			return;
1436 
1437 		if (WARN_ON(key->keyidx >=
1438 				ARRAY_SIZE(data->rsc->mcast_key_id_map)))
1439 			return;
1440 		data->gtk_cipher = cipher;
1441 		data->found_gtk_idx[data->gtks] = key->keyidx;
1442 		key_rsc = data->rsc->mcast_rsc[data->gtks % 2];
1443 		data->rsc->mcast_key_id_map[key->keyidx] =
1444 			data->gtks % 2;
1445 
1446 		if (data->gtks >= 2) {
1447 			int prev = data->gtks % 2;
1448 			int prev_idx = data->found_gtk_idx[prev];
1449 
1450 			data->rsc->mcast_key_id_map[prev_idx] =
1451 				IWL_MCAST_KEY_MAP_INVALID;
1452 		}
1453 
1454 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1455 			iwl_mld_suspend_convert_tkip_ipn(key, key_rsc);
1456 		else
1457 			iwl_mld_aes_seq_to_le64_pn(key, key_rsc);
1458 
1459 		data->gtks++;
1460 		data->have_rsc = true;
1461 		break;
1462 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1463 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1464 		cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
1465 		fallthrough;
1466 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1467 	case WLAN_CIPHER_SUITE_AES_CMAC:
1468 		if (!cipher)
1469 			cipher = cpu_to_le32(STA_KEY_FLG_CCM);
1470 		if (key->keyidx == 4 || key->keyidx == 5)
1471 			data->igtk_cipher = cipher;
1472 
1473 		if (key->keyidx == 6 || key->keyidx == 7)
1474 			data->bigtk_cipher = cipher;
1475 
1476 		break;
1477 	}
1478 }
1479 
1480 static int
1481 iwl_mld_send_kek_kck_cmd(struct iwl_mld *mld,
1482 			 struct iwl_mld_vif *mld_vif,
1483 			 struct iwl_mld_suspend_key_iter_data data,
1484 			 int ap_sta_id)
1485 {
1486 	struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {};
1487 	struct iwl_mld_rekey_data *rekey_data =
1488 		&mld_vif->wowlan_data.rekey_data;
1489 
1490 	memcpy(kek_kck_cmd.kck, rekey_data->kck,
1491 	       rekey_data->kck_len);
1492 	kek_kck_cmd.kck_len = cpu_to_le16(rekey_data->kck_len);
1493 	memcpy(kek_kck_cmd.kek, rekey_data->kek,
1494 	       rekey_data->kek_len);
1495 	kek_kck_cmd.kek_len = cpu_to_le16(rekey_data->kek_len);
1496 	kek_kck_cmd.replay_ctr = rekey_data->replay_ctr;
1497 	kek_kck_cmd.akm = cpu_to_le32(rekey_data->akm);
1498 	kek_kck_cmd.sta_id = cpu_to_le32(ap_sta_id);
1499 	kek_kck_cmd.gtk_cipher = data.gtk_cipher;
1500 	kek_kck_cmd.igtk_cipher = data.igtk_cipher;
1501 	kek_kck_cmd.bigtk_cipher = data.bigtk_cipher;
1502 
1503 	IWL_DEBUG_WOWLAN(mld, "setting akm %d\n",
1504 			 rekey_data->akm);
1505 
1506 	return iwl_mld_send_cmd_pdu(mld, WOWLAN_KEK_KCK_MATERIAL,
1507 				    &kek_kck_cmd);
1508 }
1509 
1510 static int
1511 iwl_mld_suspend_send_security_cmds(struct iwl_mld *mld,
1512 				   struct ieee80211_vif *vif,
1513 				   struct iwl_mld_vif *mld_vif,
1514 				   int ap_sta_id)
1515 {
1516 	struct iwl_mld_suspend_key_iter_data data = {};
1517 	int ret;
1518 
1519 	data.rsc = kzalloc(sizeof(*data.rsc), GFP_KERNEL);
1520 	if (!data.rsc)
1521 		return -ENOMEM;
1522 
1523 	memset(data.rsc->mcast_key_id_map, IWL_MCAST_KEY_MAP_INVALID,
1524 	       ARRAY_SIZE(data.rsc->mcast_key_id_map));
1525 
1526 	data.rsc->sta_id = cpu_to_le32(ap_sta_id);
1527 	ieee80211_iter_keys(mld->hw, vif,
1528 			    iwl_mld_suspend_key_data_iter,
1529 			    &data);
1530 
1531 	if (data.have_rsc)
1532 		ret = iwl_mld_send_cmd_pdu(mld, WOWLAN_TSC_RSC_PARAM,
1533 					   data.rsc);
1534 	else
1535 		ret = 0;
1536 
1537 	if (!ret && mld_vif->wowlan_data.rekey_data.valid)
1538 		ret = iwl_mld_send_kek_kck_cmd(mld, mld_vif, data, ap_sta_id);
1539 
1540 	kfree(data.rsc);
1541 
1542 	return ret;
1543 }
1544 
1545 static void
1546 iwl_mld_set_wowlan_config_cmd(struct iwl_mld *mld,
1547 			      struct cfg80211_wowlan *wowlan,
1548 			      struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1549 			      struct ieee80211_sta *ap_sta,
1550 			      struct ieee80211_bss_conf *link)
1551 {
1552 	wowlan_config_cmd->is_11n_connection =
1553 					ap_sta->deflink.ht_cap.ht_supported;
1554 	wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
1555 		ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
1556 
1557 	if (ap_sta->mfp)
1558 		wowlan_config_cmd->flags |= IS_11W_ASSOC;
1559 
1560 	if (iwl_mld_beacon_protection_enabled(mld, link))
1561 		wowlan_config_cmd->flags |= HAS_BEACON_PROTECTION;
1562 
1563 	if (wowlan->disconnect)
1564 		wowlan_config_cmd->wakeup_filter |=
1565 			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
1566 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
1567 	if (wowlan->magic_pkt)
1568 		wowlan_config_cmd->wakeup_filter |=
1569 			cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
1570 	if (wowlan->gtk_rekey_failure)
1571 		wowlan_config_cmd->wakeup_filter |=
1572 			cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
1573 	if (wowlan->eap_identity_req)
1574 		wowlan_config_cmd->wakeup_filter |=
1575 			cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
1576 	if (wowlan->four_way_handshake)
1577 		wowlan_config_cmd->wakeup_filter |=
1578 			cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
1579 	if (wowlan->n_patterns)
1580 		wowlan_config_cmd->wakeup_filter |=
1581 			cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
1582 
1583 	if (wowlan->rfkill_release)
1584 		wowlan_config_cmd->wakeup_filter |=
1585 			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
1586 
1587 	if (wowlan->any) {
1588 		wowlan_config_cmd->wakeup_filter |=
1589 			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
1590 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE |
1591 				    IWL_WOWLAN_WAKEUP_RX_FRAME |
1592 				    IWL_WOWLAN_WAKEUP_BCN_FILTERING);
1593 	}
1594 }
1595 
1596 static int iwl_mld_send_patterns(struct iwl_mld *mld,
1597 				 struct cfg80211_wowlan *wowlan,
1598 				 int ap_sta_id)
1599 {
1600 	struct iwl_wowlan_patterns_cmd *pattern_cmd;
1601 	struct iwl_host_cmd cmd = {
1602 		.id = WOWLAN_PATTERNS,
1603 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1604 	};
1605 	int ret;
1606 
1607 	if (!wowlan->n_patterns)
1608 		return 0;
1609 
1610 	cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns);
1611 
1612 	pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL);
1613 	if (!pattern_cmd)
1614 		return -ENOMEM;
1615 
1616 	pattern_cmd->n_patterns = wowlan->n_patterns;
1617 	pattern_cmd->sta_id = ap_sta_id;
1618 
1619 	for (int i = 0; i < wowlan->n_patterns; i++) {
1620 		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
1621 
1622 		pattern_cmd->patterns[i].pattern_type =
1623 			WOWLAN_PATTERN_TYPE_BITMASK;
1624 
1625 		memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
1626 		       wowlan->patterns[i].mask, mask_len);
1627 		memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
1628 		       wowlan->patterns[i].pattern,
1629 		       wowlan->patterns[i].pattern_len);
1630 		pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
1631 		pattern_cmd->patterns[i].u.bitmask.pattern_size =
1632 			wowlan->patterns[i].pattern_len;
1633 	}
1634 
1635 	cmd.data[0] = pattern_cmd;
1636 	ret = iwl_mld_send_cmd(mld, &cmd);
1637 	kfree(pattern_cmd);
1638 	return ret;
1639 }
1640 
1641 static int
1642 iwl_mld_send_proto_offload(struct iwl_mld *mld,
1643 			   struct ieee80211_vif *vif,
1644 			   u8 ap_sta_id)
1645 {
1646 	struct iwl_proto_offload_cmd_v4 *cmd __free(kfree);
1647 	struct iwl_host_cmd hcmd = {
1648 		.id = PROT_OFFLOAD_CONFIG_CMD,
1649 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1650 		.len[0] = sizeof(*cmd),
1651 	};
1652 	u32 enabled = 0;
1653 
1654 	cmd = kzalloc(hcmd.len[0], GFP_KERNEL);
1655 
1656 #if IS_ENABLED(CONFIG_IPV6)
1657 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1658 	struct iwl_mld_wowlan_data *wowlan_data = &mld_vif->wowlan_data;
1659 	struct iwl_ns_config *nsc;
1660 	struct iwl_targ_addr *addrs;
1661 	int n_nsc, n_addrs;
1662 	int i, c;
1663 	int num_skipped = 0;
1664 
1665 	nsc = cmd->ns_config;
1666 	n_nsc = IWL_PROTO_OFFLOAD_NUM_NS_CONFIG_V3L;
1667 	addrs = cmd->targ_addrs;
1668 	n_addrs = IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V3L;
1669 
1670 	/* For each address we have (and that will fit) fill a target
1671 	 * address struct and combine for NS offload structs with the
1672 	 * solicited node addresses.
1673 	 */
1674 	for (i = 0, c = 0;
1675 		i < wowlan_data->num_target_ipv6_addrs &&
1676 		i < n_addrs && c < n_nsc; i++) {
1677 		int j;
1678 		struct in6_addr solicited_addr;
1679 
1680 		/* Because ns is offloaded skip tentative address to avoid
1681 		 * violating RFC4862.
1682 		 */
1683 		if (test_bit(i, wowlan_data->tentative_addrs)) {
1684 			num_skipped++;
1685 			continue;
1686 		}
1687 
1688 		addrconf_addr_solict_mult(&wowlan_data->target_ipv6_addrs[i],
1689 					  &solicited_addr);
1690 		for (j = 0; j < n_nsc && j < c; j++)
1691 			if (ipv6_addr_cmp(&nsc[j].dest_ipv6_addr,
1692 					  &solicited_addr) == 0)
1693 				break;
1694 		if (j == c)
1695 			c++;
1696 		addrs[i].addr = wowlan_data->target_ipv6_addrs[i];
1697 		addrs[i].config_num = cpu_to_le32(j);
1698 		nsc[j].dest_ipv6_addr = solicited_addr;
1699 		memcpy(nsc[j].target_mac_addr, vif->addr, ETH_ALEN);
1700 	}
1701 
1702 	if (wowlan_data->num_target_ipv6_addrs - num_skipped)
1703 		enabled |= IWL_D3_PROTO_IPV6_VALID;
1704 
1705 	cmd->num_valid_ipv6_addrs = cpu_to_le32(i - num_skipped);
1706 	if (enabled & IWL_D3_PROTO_IPV6_VALID)
1707 		enabled |= IWL_D3_PROTO_OFFLOAD_NS;
1708 #endif
1709 
1710 	if (vif->cfg.arp_addr_cnt) {
1711 		enabled |= IWL_D3_PROTO_OFFLOAD_ARP | IWL_D3_PROTO_IPV4_VALID;
1712 		cmd->common.host_ipv4_addr = vif->cfg.arp_addr_list[0];
1713 		ether_addr_copy(cmd->common.arp_mac_addr, vif->addr);
1714 	}
1715 
1716 	enabled |= IWL_D3_PROTO_OFFLOAD_BTM;
1717 	cmd->common.enabled = cpu_to_le32(enabled);
1718 	cmd->sta_id = cpu_to_le32(ap_sta_id);
1719 	hcmd.data[0] = cmd;
1720 	return iwl_mld_send_cmd(mld, &hcmd);
1721 }
1722 
1723 static int
1724 iwl_mld_wowlan_config(struct iwl_mld *mld, struct ieee80211_vif *bss_vif,
1725 		      struct cfg80211_wowlan *wowlan)
1726 {
1727 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(bss_vif);
1728 	struct ieee80211_sta *ap_sta = mld_vif->ap_sta;
1729 	struct iwl_wowlan_config_cmd wowlan_config_cmd = {
1730 			.offloading_tid = IWL_WOWLAN_OFFLOAD_TID,
1731 	};
1732 	u32 sta_id_mask;
1733 	int ap_sta_id, ret;
1734 	int link_id = iwl_mld_get_primary_link(bss_vif);
1735 	struct ieee80211_bss_conf *link_conf;
1736 
1737 	ret = iwl_mld_block_emlsr_sync(mld, bss_vif,
1738 				       IWL_MLD_EMLSR_BLOCKED_WOWLAN, link_id);
1739 	if (ret)
1740 		return ret;
1741 
1742 	link_conf = link_conf_dereference_protected(bss_vif, link_id);
1743 
1744 	if (WARN_ON(!ap_sta || !link_conf))
1745 		return -EINVAL;
1746 
1747 	sta_id_mask = iwl_mld_fw_sta_id_mask(mld, ap_sta);
1748 	if (WARN_ON(hweight32(sta_id_mask) != 1))
1749 		return -EINVAL;
1750 
1751 	ap_sta_id = __ffs(sta_id_mask);
1752 	wowlan_config_cmd.sta_id = ap_sta_id;
1753 
1754 	ret = iwl_mld_ensure_queue(mld,
1755 				   ap_sta->txq[wowlan_config_cmd.offloading_tid]);
1756 	if (ret)
1757 		return ret;
1758 
1759 	iwl_mld_set_wowlan_config_cmd(mld, wowlan,
1760 				      &wowlan_config_cmd, ap_sta, link_conf);
1761 	ret = iwl_mld_send_cmd_pdu(mld, WOWLAN_CONFIGURATION,
1762 				   &wowlan_config_cmd);
1763 	if (ret)
1764 		return ret;
1765 
1766 	ret = iwl_mld_suspend_send_security_cmds(mld, bss_vif, mld_vif,
1767 						 ap_sta_id);
1768 	if (ret)
1769 		return ret;
1770 
1771 	ret = iwl_mld_send_patterns(mld, wowlan, ap_sta_id);
1772 	if (ret)
1773 		return ret;
1774 
1775 	ret = iwl_mld_send_proto_offload(mld, bss_vif, ap_sta_id);
1776 	if (ret)
1777 		return ret;
1778 
1779 	iwl_mld_enable_beacon_filter(mld, link_conf, true);
1780 	return iwl_mld_update_mac_power(mld, bss_vif, true);
1781 }
1782 
1783 int iwl_mld_wowlan_suspend(struct iwl_mld *mld, struct cfg80211_wowlan *wowlan)
1784 {
1785 	struct ieee80211_vif *bss_vif;
1786 
1787 	lockdep_assert_wiphy(mld->wiphy);
1788 
1789 	if (WARN_ON(!wowlan))
1790 		return 1;
1791 
1792 	IWL_DEBUG_WOWLAN(mld, "Starting the wowlan suspend flow\n");
1793 
1794 	bss_vif = iwl_mld_get_bss_vif(mld);
1795 	if (WARN_ON(!bss_vif))
1796 		return 1;
1797 
1798 	if (!bss_vif->cfg.assoc) {
1799 		int ret;
1800 		/* If we're not associated, this must be netdetect */
1801 		if (WARN_ON(!wowlan->nd_config))
1802 			return 1;
1803 
1804 		ret = iwl_mld_netdetect_config(mld, bss_vif, wowlan);
1805 		if (!ret)
1806 			mld->netdetect = true;
1807 
1808 		return ret;
1809 	}
1810 
1811 	return iwl_mld_wowlan_config(mld, bss_vif, wowlan);
1812 }
1813 
1814 /* Returns 0 on success, 1 if an error occurred in firmware during d3,
1815  * A negative value is expected only in unrecovreable cases.
1816  */
1817 int iwl_mld_wowlan_resume(struct iwl_mld *mld)
1818 {
1819 	struct ieee80211_vif *bss_vif;
1820 	struct ieee80211_bss_conf *link_conf;
1821 	struct iwl_mld_netdetect_res netdetect_res;
1822 	struct iwl_mld_resume_data resume_data = {
1823 		.notifs_expected =
1824 			IWL_D3_NOTIF_WOWLAN_INFO |
1825 			IWL_D3_NOTIF_D3_END_NOTIF,
1826 		.netdetect_res = &netdetect_res,
1827 	};
1828 	int link_id;
1829 	int ret;
1830 	bool fw_err = false;
1831 
1832 	lockdep_assert_wiphy(mld->wiphy);
1833 
1834 	IWL_DEBUG_WOWLAN(mld, "Starting the wowlan resume flow\n");
1835 
1836 	if (!mld->fw_status.in_d3) {
1837 		IWL_DEBUG_WOWLAN(mld,
1838 				 "Device_powered_off() was called during wowlan\n");
1839 		goto err;
1840 	}
1841 
1842 	mld->fw_status.resuming = true;
1843 	mld->fw_status.in_d3 = false;
1844 	mld->scan.last_start_time_jiffies = jiffies;
1845 
1846 	bss_vif = iwl_mld_get_bss_vif(mld);
1847 	if (WARN_ON(!bss_vif))
1848 		goto err;
1849 
1850 	/* We can't have several links upon wowlan entry,
1851 	 * this is enforced in the suspend flow.
1852 	 */
1853 	WARN_ON(hweight16(bss_vif->active_links) > 1);
1854 	link_id = bss_vif->active_links ? __ffs(bss_vif->active_links) : 0;
1855 	link_conf = link_conf_dereference_protected(bss_vif, link_id);
1856 
1857 	if (WARN_ON(!link_conf))
1858 		goto err;
1859 
1860 	iwl_fw_dbg_read_d3_debug_data(&mld->fwrt);
1861 
1862 	resume_data.wowlan_status = kzalloc(sizeof(*resume_data.wowlan_status),
1863 					    GFP_KERNEL);
1864 	if (!resume_data.wowlan_status)
1865 		return -ENOMEM;
1866 
1867 	if (mld->netdetect)
1868 		resume_data.notifs_expected |= IWL_D3_ND_MATCH_INFO;
1869 
1870 	ret = iwl_mld_wait_d3_notif(mld, &resume_data, true);
1871 	if (ret) {
1872 		IWL_ERR(mld, "Couldn't get the d3 notifs %d\n", ret);
1873 		fw_err = true;
1874 		goto err;
1875 	}
1876 
1877 	if (resume_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE) {
1878 		mld->fw_status.in_hw_restart = true;
1879 		goto process_wakeup_results;
1880 	}
1881 
1882 	iwl_mld_update_changed_regdomain(mld);
1883 	iwl_mld_update_mac_power(mld, bss_vif, false);
1884 	iwl_mld_enable_beacon_filter(mld, link_conf, false);
1885 	iwl_mld_update_device_power(mld, false);
1886 
1887 	if (mld->netdetect)
1888 		ret = iwl_mld_scan_stop(mld, IWL_MLD_SCAN_NETDETECT, false);
1889 
1890  process_wakeup_results:
1891 	if (mld->netdetect) {
1892 		iwl_mld_process_netdetect_res(mld, bss_vif, &resume_data);
1893 		mld->netdetect = false;
1894 	} else {
1895 		bool keep_connection =
1896 			iwl_mld_process_wowlan_status(mld, bss_vif,
1897 						      resume_data.wowlan_status);
1898 
1899 		/* EMLSR state will be cleared if the connection is not kept */
1900 		if (keep_connection)
1901 			iwl_mld_unblock_emlsr(mld, bss_vif,
1902 					      IWL_MLD_EMLSR_BLOCKED_WOWLAN);
1903 		else
1904 			ieee80211_resume_disconnect(bss_vif);
1905 	}
1906 
1907 	goto out;
1908 
1909  err:
1910 	if (fw_err) {
1911 		mld->trans->state = IWL_TRANS_NO_FW;
1912 		set_bit(STATUS_FW_ERROR, &mld->trans->status);
1913 	}
1914 
1915 	mld->fw_status.in_hw_restart = true;
1916 	ret = 1;
1917  out:
1918 	mld->fw_status.resuming = false;
1919 
1920 	if (resume_data.wowlan_status) {
1921 		kfree(resume_data.wowlan_status->wake_packet);
1922 		kfree(resume_data.wowlan_status);
1923 	}
1924 
1925 	return ret;
1926 }
1927