xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/d3.c (revision a4128aad8503277614f2d214011ef60a19447b83)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/etherdevice.h>
8 #include <linux/ip.h>
9 #include <linux/fs.h>
10 #if defined(__FreeBSD__)
11 #include <linux/string.h>
12 #endif
13 #include <net/cfg80211.h>
14 #include <net/ipv6.h>
15 #include <net/tcp.h>
16 #include <net/addrconf.h>
17 #include "iwl-modparams.h"
18 #include "fw-api.h"
19 #include "mvm.h"
20 #include "fw/img.h"
21 
22 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
23 			    struct ieee80211_vif *vif,
24 			    struct cfg80211_gtk_rekey_data *data)
25 {
26 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
27 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
28 
29 	mutex_lock(&mvm->mutex);
30 
31 	mvmvif->rekey_data.kek_len = data->kek_len;
32 	mvmvif->rekey_data.kck_len = data->kck_len;
33 	memcpy(mvmvif->rekey_data.kek, data->kek, data->kek_len);
34 	memcpy(mvmvif->rekey_data.kck, data->kck, data->kck_len);
35 	mvmvif->rekey_data.akm = data->akm & 0xFF;
36 	mvmvif->rekey_data.replay_ctr =
37 		cpu_to_le64(be64_to_cpup((const __be64 *)data->replay_ctr));
38 	mvmvif->rekey_data.valid = true;
39 
40 	mutex_unlock(&mvm->mutex);
41 }
42 
43 #if IS_ENABLED(CONFIG_IPV6)
44 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
45 			      struct ieee80211_vif *vif,
46 			      struct inet6_dev *idev)
47 {
48 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
49 	struct inet6_ifaddr *ifa;
50 	int idx = 0;
51 
52 	memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
53 
54 	read_lock_bh(&idev->lock);
55 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
56 		mvmvif->target_ipv6_addrs[idx] = ifa->addr;
57 		if (ifa->flags & IFA_F_TENTATIVE)
58 			__set_bit(idx, mvmvif->tentative_addrs);
59 		idx++;
60 		if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
61 			break;
62 	}
63 	read_unlock_bh(&idev->lock);
64 
65 	mvmvif->num_target_ipv6_addrs = idx;
66 }
67 #endif
68 
69 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
70 				     struct ieee80211_vif *vif, int idx)
71 {
72 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
73 
74 	mvmvif->tx_key_idx = idx;
75 }
76 
77 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
78 {
79 	int i;
80 
81 	for (i = 0; i < IWL_P1K_SIZE; i++)
82 		out[i] = cpu_to_le16(p1k[i]);
83 }
84 
85 static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
86 				     struct iwl_mvm_key_pn *ptk_pn,
87 				     struct ieee80211_key_seq *seq,
88 				     int tid, int queues)
89 {
90 	const u8 *ret = seq->ccmp.pn;
91 	int i;
92 
93 	/* get the PN from mac80211, used on the default queue */
94 	ieee80211_get_key_rx_seq(key, tid, seq);
95 
96 	/* and use the internal data for the other queues */
97 	for (i = 1; i < queues; i++) {
98 		const u8 *tmp = ptk_pn->q[i].pn[tid];
99 
100 		if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
101 			ret = tmp;
102 	}
103 
104 	return ret;
105 }
106 
107 struct wowlan_key_reprogram_data {
108 	bool error;
109 	int wep_key_idx;
110 };
111 
112 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
113 					struct ieee80211_vif *vif,
114 					struct ieee80211_sta *sta,
115 					struct ieee80211_key_conf *key,
116 					void *_data)
117 {
118 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
119 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
120 	struct wowlan_key_reprogram_data *data = _data;
121 	int ret;
122 
123 	switch (key->cipher) {
124 	case WLAN_CIPHER_SUITE_WEP40:
125 	case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
126 		struct {
127 			struct iwl_mvm_wep_key_cmd wep_key_cmd;
128 			struct iwl_mvm_wep_key wep_key;
129 		} __packed wkc = {
130 			.wep_key_cmd.mac_id_n_color =
131 				cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
132 								mvmvif->color)),
133 			.wep_key_cmd.num_keys = 1,
134 			/* firmware sets STA_KEY_FLG_WEP_13BYTES */
135 			.wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
136 			.wep_key.key_index = key->keyidx,
137 			.wep_key.key_size = key->keylen,
138 		};
139 
140 		/*
141 		 * This will fail -- the key functions don't set support
142 		 * pairwise WEP keys. However, that's better than silently
143 		 * failing WoWLAN. Or maybe not?
144 		 */
145 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
146 			break;
147 
148 		memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
149 		if (key->keyidx == mvmvif->tx_key_idx) {
150 			/* TX key must be at offset 0 */
151 			wkc.wep_key.key_offset = 0;
152 		} else {
153 			/* others start at 1 */
154 			data->wep_key_idx++;
155 			wkc.wep_key.key_offset = data->wep_key_idx;
156 		}
157 
158 		mutex_lock(&mvm->mutex);
159 		ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
160 		data->error = ret != 0;
161 
162 		mvm->ptk_ivlen = key->iv_len;
163 		mvm->ptk_icvlen = key->icv_len;
164 		mvm->gtk_ivlen = key->iv_len;
165 		mvm->gtk_icvlen = key->icv_len;
166 		mutex_unlock(&mvm->mutex);
167 
168 		/* don't upload key again */
169 		return;
170 	}
171 	default:
172 		data->error = true;
173 		return;
174 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
175 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
176 		return;
177 	case WLAN_CIPHER_SUITE_AES_CMAC:
178 		/*
179 		 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
180 		 * but we also shouldn't abort suspend due to that. It does have
181 		 * support for the IGTK key renewal, but doesn't really use the
182 		 * IGTK for anything. This means we could spuriously wake up or
183 		 * be deauthenticated, but that was considered acceptable.
184 		 */
185 		return;
186 	case WLAN_CIPHER_SUITE_TKIP:
187 	case WLAN_CIPHER_SUITE_CCMP:
188 	case WLAN_CIPHER_SUITE_GCMP:
189 	case WLAN_CIPHER_SUITE_GCMP_256:
190 		break;
191 	}
192 
193 	mutex_lock(&mvm->mutex);
194 	/*
195 	 * The D3 firmware hardcodes the key offset 0 as the key it
196 	 * uses to transmit packets to the AP, i.e. the PTK.
197 	 */
198 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
199 		mvm->ptk_ivlen = key->iv_len;
200 		mvm->ptk_icvlen = key->icv_len;
201 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
202 	} else {
203 		/*
204 		 * firmware only supports TSC/RSC for a single key,
205 		 * so if there are multiple keep overwriting them
206 		 * with new ones -- this relies on mac80211 doing
207 		 * list_add_tail().
208 		 */
209 		mvm->gtk_ivlen = key->iv_len;
210 		mvm->gtk_icvlen = key->icv_len;
211 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
212 	}
213 	mutex_unlock(&mvm->mutex);
214 	data->error = ret != 0;
215 }
216 
217 struct wowlan_key_rsc_tsc_data {
218 	struct iwl_wowlan_rsc_tsc_params_cmd_v4 *rsc_tsc;
219 	bool have_rsc_tsc;
220 };
221 
222 static void iwl_mvm_wowlan_get_rsc_tsc_data(struct ieee80211_hw *hw,
223 					    struct ieee80211_vif *vif,
224 					    struct ieee80211_sta *sta,
225 					    struct ieee80211_key_conf *key,
226 					    void *_data)
227 {
228 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
229 	struct wowlan_key_rsc_tsc_data *data = _data;
230 	struct aes_sc *aes_sc;
231 	struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
232 	struct ieee80211_key_seq seq;
233 	int i;
234 
235 	switch (key->cipher) {
236 	default:
237 		break;
238 	case WLAN_CIPHER_SUITE_TKIP:
239 		if (sta) {
240 			u64 pn64;
241 
242 			tkip_sc =
243 			   data->rsc_tsc->params.all_tsc_rsc.tkip.unicast_rsc;
244 			tkip_tx_sc =
245 				&data->rsc_tsc->params.all_tsc_rsc.tkip.tsc;
246 
247 			pn64 = atomic64_read(&key->tx_pn);
248 			tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
249 			tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
250 		} else {
251 			tkip_sc =
252 			  data->rsc_tsc->params.all_tsc_rsc.tkip.multicast_rsc;
253 		}
254 
255 		/*
256 		 * For non-QoS this relies on the fact that both the uCode and
257 		 * mac80211 use TID 0 (as they need to to avoid replay attacks)
258 		 * for checking the IV in the frames.
259 		 */
260 		for (i = 0; i < IWL_NUM_RSC; i++) {
261 			ieee80211_get_key_rx_seq(key, i, &seq);
262 			tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
263 			tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
264 		}
265 
266 		data->have_rsc_tsc = true;
267 		break;
268 	case WLAN_CIPHER_SUITE_CCMP:
269 	case WLAN_CIPHER_SUITE_GCMP:
270 	case WLAN_CIPHER_SUITE_GCMP_256:
271 		if (sta) {
272 			struct aes_sc *aes_tx_sc;
273 			u64 pn64;
274 
275 			aes_sc =
276 			   data->rsc_tsc->params.all_tsc_rsc.aes.unicast_rsc;
277 			aes_tx_sc =
278 				&data->rsc_tsc->params.all_tsc_rsc.aes.tsc;
279 
280 			pn64 = atomic64_read(&key->tx_pn);
281 			aes_tx_sc->pn = cpu_to_le64(pn64);
282 		} else {
283 			aes_sc =
284 			   data->rsc_tsc->params.all_tsc_rsc.aes.multicast_rsc;
285 		}
286 
287 		/*
288 		 * For non-QoS this relies on the fact that both the uCode and
289 		 * mac80211/our RX code use TID 0 for checking the PN.
290 		 */
291 		if (sta && iwl_mvm_has_new_rx_api(mvm)) {
292 			struct iwl_mvm_sta *mvmsta;
293 			struct iwl_mvm_key_pn *ptk_pn;
294 			const u8 *pn;
295 
296 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
297 			rcu_read_lock();
298 			ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
299 			if (WARN_ON(!ptk_pn)) {
300 				rcu_read_unlock();
301 				break;
302 			}
303 
304 			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
305 				pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
306 						mvm->trans->num_rx_queues);
307 				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
308 							   ((u64)pn[4] << 8) |
309 							   ((u64)pn[3] << 16) |
310 							   ((u64)pn[2] << 24) |
311 							   ((u64)pn[1] << 32) |
312 							   ((u64)pn[0] << 40));
313 			}
314 
315 			rcu_read_unlock();
316 		} else {
317 			for (i = 0; i < IWL_NUM_RSC; i++) {
318 				u8 *pn = seq.ccmp.pn;
319 
320 				ieee80211_get_key_rx_seq(key, i, &seq);
321 				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
322 							   ((u64)pn[4] << 8) |
323 							   ((u64)pn[3] << 16) |
324 							   ((u64)pn[2] << 24) |
325 							   ((u64)pn[1] << 32) |
326 							   ((u64)pn[0] << 40));
327 			}
328 		}
329 		data->have_rsc_tsc = true;
330 		break;
331 	}
332 }
333 
334 struct wowlan_key_rsc_v5_data {
335 	struct iwl_wowlan_rsc_tsc_params_cmd *rsc;
336 	bool have_rsc;
337 	int gtks;
338 	int gtk_ids[4];
339 };
340 
341 static void iwl_mvm_wowlan_get_rsc_v5_data(struct ieee80211_hw *hw,
342 					   struct ieee80211_vif *vif,
343 					   struct ieee80211_sta *sta,
344 					   struct ieee80211_key_conf *key,
345 					   void *_data)
346 {
347 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
348 	struct wowlan_key_rsc_v5_data *data = _data;
349 	struct ieee80211_key_seq seq;
350 	__le64 *rsc;
351 	int i;
352 
353 	/* only for ciphers that can be PTK/GTK */
354 	switch (key->cipher) {
355 	default:
356 		return;
357 	case WLAN_CIPHER_SUITE_TKIP:
358 	case WLAN_CIPHER_SUITE_CCMP:
359 	case WLAN_CIPHER_SUITE_GCMP:
360 	case WLAN_CIPHER_SUITE_GCMP_256:
361 		break;
362 	}
363 
364 	if (sta) {
365 		rsc = data->rsc->ucast_rsc;
366 	} else {
367 		if (WARN_ON(data->gtks >= ARRAY_SIZE(data->gtk_ids)))
368 			return;
369 		data->gtk_ids[data->gtks] = key->keyidx;
370 		rsc = data->rsc->mcast_rsc[data->gtks % 2];
371 		if (WARN_ON(key->keyidx >=
372 				ARRAY_SIZE(data->rsc->mcast_key_id_map)))
373 			return;
374 		data->rsc->mcast_key_id_map[key->keyidx] = data->gtks % 2;
375 		if (data->gtks >= 2) {
376 			int prev = data->gtks - 2;
377 			int prev_idx = data->gtk_ids[prev];
378 
379 			data->rsc->mcast_key_id_map[prev_idx] =
380 				IWL_MCAST_KEY_MAP_INVALID;
381 		}
382 		data->gtks++;
383 	}
384 
385 	switch (key->cipher) {
386 	default:
387 		WARN_ON(1);
388 		break;
389 	case WLAN_CIPHER_SUITE_TKIP:
390 
391 		/*
392 		 * For non-QoS this relies on the fact that both the uCode and
393 		 * mac80211 use TID 0 (as they need to to avoid replay attacks)
394 		 * for checking the IV in the frames.
395 		 */
396 		for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
397 			ieee80211_get_key_rx_seq(key, i, &seq);
398 
399 			rsc[i] = cpu_to_le64(((u64)seq.tkip.iv32 << 16) |
400 					     seq.tkip.iv16);
401 		}
402 
403 		data->have_rsc = true;
404 		break;
405 	case WLAN_CIPHER_SUITE_CCMP:
406 	case WLAN_CIPHER_SUITE_GCMP:
407 	case WLAN_CIPHER_SUITE_GCMP_256:
408 		/*
409 		 * For non-QoS this relies on the fact that both the uCode and
410 		 * mac80211/our RX code use TID 0 for checking the PN.
411 		 */
412 		if (sta) {
413 			struct iwl_mvm_sta *mvmsta;
414 			struct iwl_mvm_key_pn *ptk_pn;
415 			const u8 *pn;
416 
417 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
418 			rcu_read_lock();
419 			ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
420 			if (WARN_ON(!ptk_pn)) {
421 				rcu_read_unlock();
422 				break;
423 			}
424 
425 			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
426 				pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
427 						mvm->trans->num_rx_queues);
428 				rsc[i] = cpu_to_le64((u64)pn[5] |
429 						     ((u64)pn[4] << 8) |
430 						     ((u64)pn[3] << 16) |
431 						     ((u64)pn[2] << 24) |
432 						     ((u64)pn[1] << 32) |
433 						     ((u64)pn[0] << 40));
434 			}
435 
436 			rcu_read_unlock();
437 		} else {
438 			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
439 				u8 *pn = seq.ccmp.pn;
440 
441 				ieee80211_get_key_rx_seq(key, i, &seq);
442 				rsc[i] = cpu_to_le64((u64)pn[5] |
443 						     ((u64)pn[4] << 8) |
444 						     ((u64)pn[3] << 16) |
445 						     ((u64)pn[2] << 24) |
446 						     ((u64)pn[1] << 32) |
447 						     ((u64)pn[0] << 40));
448 			}
449 		}
450 		data->have_rsc = true;
451 		break;
452 	}
453 }
454 
455 static int iwl_mvm_wowlan_config_rsc_tsc(struct iwl_mvm *mvm,
456 					 struct ieee80211_vif *vif,
457 					 struct iwl_mvm_vif_link_info *mvm_link)
458 {
459 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TSC_RSC_PARAM,
460 					IWL_FW_CMD_VER_UNKNOWN);
461 	int ret;
462 
463 	if (ver == 5) {
464 		struct wowlan_key_rsc_v5_data data = {};
465 		int i;
466 
467 		data.rsc = kzalloc(sizeof(*data.rsc), GFP_KERNEL);
468 		if (!data.rsc)
469 			return -ENOMEM;
470 
471 		for (i = 0; i < ARRAY_SIZE(data.rsc->mcast_key_id_map); i++)
472 			data.rsc->mcast_key_id_map[i] =
473 				IWL_MCAST_KEY_MAP_INVALID;
474 		data.rsc->sta_id = cpu_to_le32(mvm_link->ap_sta_id);
475 
476 		ieee80211_iter_keys(mvm->hw, vif,
477 				    iwl_mvm_wowlan_get_rsc_v5_data,
478 				    &data);
479 
480 		if (data.have_rsc)
481 			ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
482 						   CMD_ASYNC, sizeof(*data.rsc),
483 						   data.rsc);
484 		else
485 			ret = 0;
486 		kfree(data.rsc);
487 	} else if (ver == 4 || ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN) {
488 		struct wowlan_key_rsc_tsc_data data = {};
489 		int size;
490 
491 		data.rsc_tsc = kzalloc(sizeof(*data.rsc_tsc), GFP_KERNEL);
492 		if (!data.rsc_tsc)
493 			return -ENOMEM;
494 
495 		if (ver == 4) {
496 			size = sizeof(*data.rsc_tsc);
497 			data.rsc_tsc->sta_id =
498 				cpu_to_le32(mvm_link->ap_sta_id);
499 		} else {
500 			/* ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN */
501 			size = sizeof(data.rsc_tsc->params);
502 		}
503 
504 		ieee80211_iter_keys(mvm->hw, vif,
505 				    iwl_mvm_wowlan_get_rsc_tsc_data,
506 				    &data);
507 
508 		if (data.have_rsc_tsc)
509 			ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
510 						   CMD_ASYNC, size,
511 						   data.rsc_tsc);
512 		else
513 			ret = 0;
514 		kfree(data.rsc_tsc);
515 	} else {
516 		ret = 0;
517 		WARN_ON_ONCE(1);
518 	}
519 
520 	return ret;
521 }
522 
523 struct wowlan_key_tkip_data {
524 	struct iwl_wowlan_tkip_params_cmd tkip;
525 	bool have_tkip_keys;
526 };
527 
528 static void iwl_mvm_wowlan_get_tkip_data(struct ieee80211_hw *hw,
529 					 struct ieee80211_vif *vif,
530 					 struct ieee80211_sta *sta,
531 					 struct ieee80211_key_conf *key,
532 					 void *_data)
533 {
534 	struct wowlan_key_tkip_data *data = _data;
535 	struct iwl_p1k_cache *rx_p1ks;
536 	u8 *rx_mic_key;
537 	struct ieee80211_key_seq seq;
538 	u32 cur_rx_iv32 = 0;
539 	u16 p1k[IWL_P1K_SIZE];
540 	int i;
541 
542 	switch (key->cipher) {
543 	default:
544 		break;
545 	case WLAN_CIPHER_SUITE_TKIP:
546 		if (sta) {
547 			u64 pn64;
548 
549 			rx_p1ks = data->tkip.rx_uni;
550 
551 			pn64 = atomic64_read(&key->tx_pn);
552 
553 			ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
554 						  p1k);
555 			iwl_mvm_convert_p1k(p1k, data->tkip.tx.p1k);
556 
557 			memcpy(data->tkip.mic_keys.tx,
558 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
559 			       IWL_MIC_KEY_SIZE);
560 
561 			rx_mic_key = data->tkip.mic_keys.rx_unicast;
562 		} else {
563 			rx_p1ks = data->tkip.rx_multi;
564 			rx_mic_key = data->tkip.mic_keys.rx_mcast;
565 		}
566 
567 		for (i = 0; i < IWL_NUM_RSC; i++) {
568 			ieee80211_get_key_rx_seq(key, i, &seq);
569 			/* wrapping isn't allowed, AP must rekey */
570 			if (seq.tkip.iv32 > cur_rx_iv32)
571 				cur_rx_iv32 = seq.tkip.iv32;
572 		}
573 
574 		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
575 					  cur_rx_iv32, p1k);
576 		iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
577 		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
578 					  cur_rx_iv32 + 1, p1k);
579 		iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
580 
581 		memcpy(rx_mic_key,
582 		       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
583 		       IWL_MIC_KEY_SIZE);
584 
585 		data->have_tkip_keys = true;
586 		break;
587 	}
588 }
589 
590 struct wowlan_key_gtk_type_iter {
591 	struct iwl_wowlan_kek_kck_material_cmd_v4 *kek_kck_cmd;
592 };
593 
594 static void iwl_mvm_wowlan_gtk_type_iter(struct ieee80211_hw *hw,
595 					 struct ieee80211_vif *vif,
596 					 struct ieee80211_sta *sta,
597 					 struct ieee80211_key_conf *key,
598 					 void *_data)
599 {
600 	struct wowlan_key_gtk_type_iter *data = _data;
601 	__le32 *cipher = NULL;
602 
603 	if (key->keyidx == 4 || key->keyidx == 5)
604 		cipher = &data->kek_kck_cmd->igtk_cipher;
605 	if (key->keyidx == 6 || key->keyidx == 7)
606 		cipher = &data->kek_kck_cmd->bigtk_cipher;
607 
608 	switch (key->cipher) {
609 	default:
610 		return;
611 	case WLAN_CIPHER_SUITE_TKIP:
612 		if (!sta)
613 			data->kek_kck_cmd->gtk_cipher =
614 				cpu_to_le32(STA_KEY_FLG_TKIP);
615 		return;
616 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
617 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
618 		if (cipher)
619 			*cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
620 		return;
621 	case WLAN_CIPHER_SUITE_AES_CMAC:
622 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
623 		if (cipher)
624 			*cipher = cpu_to_le32(STA_KEY_FLG_CCM);
625 		return;
626 	case WLAN_CIPHER_SUITE_CCMP:
627 		if (!sta)
628 			data->kek_kck_cmd->gtk_cipher =
629 				cpu_to_le32(STA_KEY_FLG_CCM);
630 		return;
631 	case WLAN_CIPHER_SUITE_GCMP:
632 	case WLAN_CIPHER_SUITE_GCMP_256:
633 		if (!sta)
634 			data->kek_kck_cmd->gtk_cipher =
635 				cpu_to_le32(STA_KEY_FLG_GCMP);
636 		return;
637 	}
638 }
639 
640 static int iwl_mvm_send_patterns_v1(struct iwl_mvm *mvm,
641 				    struct cfg80211_wowlan *wowlan)
642 {
643 	struct iwl_wowlan_patterns_cmd_v1 *pattern_cmd;
644 	struct iwl_host_cmd cmd = {
645 		.id = WOWLAN_PATTERNS,
646 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
647 	};
648 	int i, err;
649 
650 	if (!wowlan->n_patterns)
651 		return 0;
652 
653 	cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns);
654 
655 	pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
656 	if (!pattern_cmd)
657 		return -ENOMEM;
658 
659 	pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
660 
661 	for (i = 0; i < wowlan->n_patterns; i++) {
662 		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
663 
664 		memcpy(&pattern_cmd->patterns[i].mask,
665 		       wowlan->patterns[i].mask, mask_len);
666 		memcpy(&pattern_cmd->patterns[i].pattern,
667 		       wowlan->patterns[i].pattern,
668 		       wowlan->patterns[i].pattern_len);
669 		pattern_cmd->patterns[i].mask_size = mask_len;
670 		pattern_cmd->patterns[i].pattern_size =
671 			wowlan->patterns[i].pattern_len;
672 	}
673 
674 	cmd.data[0] = pattern_cmd;
675 	err = iwl_mvm_send_cmd(mvm, &cmd);
676 	kfree(pattern_cmd);
677 	return err;
678 }
679 
680 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
681 				 struct iwl_mvm_vif_link_info *mvm_link,
682 				 struct cfg80211_wowlan *wowlan)
683 {
684 	struct iwl_wowlan_patterns_cmd *pattern_cmd;
685 	struct iwl_host_cmd cmd = {
686 		.id = WOWLAN_PATTERNS,
687 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
688 	};
689 	int i, err;
690 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
691 					IWL_FW_CMD_VER_UNKNOWN);
692 
693 	if (!wowlan->n_patterns)
694 		return 0;
695 
696 	cmd.len[0] = sizeof(*pattern_cmd) +
697 		wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v2);
698 
699 	pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL);
700 	if (!pattern_cmd)
701 		return -ENOMEM;
702 
703 	pattern_cmd->n_patterns = wowlan->n_patterns;
704 	if (ver >= 3)
705 		pattern_cmd->sta_id = mvm_link->ap_sta_id;
706 
707 	for (i = 0; i < wowlan->n_patterns; i++) {
708 		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
709 
710 		pattern_cmd->patterns[i].pattern_type =
711 			WOWLAN_PATTERN_TYPE_BITMASK;
712 
713 		memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
714 		       wowlan->patterns[i].mask, mask_len);
715 		memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
716 		       wowlan->patterns[i].pattern,
717 		       wowlan->patterns[i].pattern_len);
718 		pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
719 		pattern_cmd->patterns[i].u.bitmask.pattern_size =
720 			wowlan->patterns[i].pattern_len;
721 	}
722 
723 	cmd.data[0] = pattern_cmd;
724 	err = iwl_mvm_send_cmd(mvm, &cmd);
725 	kfree(pattern_cmd);
726 	return err;
727 }
728 
729 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
730 				struct ieee80211_sta *ap_sta)
731 {
732 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
733 	struct ieee80211_chanctx_conf *ctx;
734 	u8 chains_static, chains_dynamic;
735 	struct cfg80211_chan_def chandef, ap_def;
736 	int ret, i;
737 	struct iwl_binding_cmd_v1 binding_cmd = {};
738 	struct iwl_time_quota_cmd quota_cmd = {};
739 	struct iwl_time_quota_data *quota;
740 	u32 status;
741 
742 	if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm) ||
743 			 ieee80211_vif_is_mld(vif)))
744 		return -EINVAL;
745 
746 	/* add back the PHY */
747 	if (WARN_ON(!mvmvif->deflink.phy_ctxt))
748 		return -EINVAL;
749 
750 	rcu_read_lock();
751 	ctx = rcu_dereference(vif->bss_conf.chanctx_conf);
752 	if (WARN_ON(!ctx)) {
753 		rcu_read_unlock();
754 		return -EINVAL;
755 	}
756 	chandef = ctx->def;
757 	ap_def = ctx->ap;
758 	chains_static = ctx->rx_chains_static;
759 	chains_dynamic = ctx->rx_chains_dynamic;
760 	rcu_read_unlock();
761 
762 	ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt, &chandef,
763 				   &ap_def, chains_static, chains_dynamic);
764 	if (ret)
765 		return ret;
766 
767 	/* add back the MAC */
768 	mvmvif->uploaded = false;
769 
770 	if (WARN_ON(!vif->cfg.assoc))
771 		return -EINVAL;
772 
773 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
774 	if (ret)
775 		return ret;
776 
777 	/* add back binding - XXX refactor? */
778 	binding_cmd.id_and_color =
779 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
780 						mvmvif->deflink.phy_ctxt->color));
781 	binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
782 	binding_cmd.phy =
783 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
784 						mvmvif->deflink.phy_ctxt->color));
785 	binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
786 							      mvmvif->color));
787 	for (i = 1; i < MAX_MACS_IN_BINDING; i++)
788 		binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
789 
790 	status = 0;
791 	ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
792 					  IWL_BINDING_CMD_SIZE_V1, &binding_cmd,
793 					  &status);
794 	if (ret) {
795 		IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
796 		return ret;
797 	}
798 
799 	if (status) {
800 		IWL_ERR(mvm, "Binding command failed: %u\n", status);
801 		return -EIO;
802 	}
803 
804 	ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
805 	if (ret)
806 		return ret;
807 	rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->deflink.ap_sta_id],
808 			   ap_sta);
809 
810 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
811 	if (ret)
812 		return ret;
813 
814 	/* and some quota */
815 	quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, 0);
816 	quota->id_and_color =
817 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
818 						mvmvif->deflink.phy_ctxt->color));
819 	quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
820 	quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
821 
822 	for (i = 1; i < MAX_BINDINGS; i++) {
823 		quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, i);
824 		quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
825 	}
826 
827 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
828 				   iwl_mvm_quota_cmd_size(mvm), &quota_cmd);
829 	if (ret)
830 		IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
831 
832 	if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm, false))
833 		IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
834 
835 	return 0;
836 }
837 
838 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
839 				       struct ieee80211_vif *vif)
840 {
841 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
842 	struct iwl_nonqos_seq_query_cmd query_cmd = {
843 		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
844 		.mac_id_n_color =
845 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
846 							mvmvif->color)),
847 	};
848 	struct iwl_host_cmd cmd = {
849 		.id = NON_QOS_TX_COUNTER_CMD,
850 		.flags = CMD_WANT_SKB,
851 	};
852 	int err;
853 	u32 size;
854 
855 	cmd.data[0] = &query_cmd;
856 	cmd.len[0] = sizeof(query_cmd);
857 
858 	err = iwl_mvm_send_cmd(mvm, &cmd);
859 	if (err)
860 		return err;
861 
862 	size = iwl_rx_packet_payload_len(cmd.resp_pkt);
863 	if (size < sizeof(__le16)) {
864 		err = -EINVAL;
865 	} else {
866 		err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
867 		/* firmware returns next, not last-used seqno */
868 		err = (u16) (err - 0x10);
869 	}
870 
871 	iwl_free_resp(&cmd);
872 	return err;
873 }
874 
875 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
876 {
877 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
878 	struct iwl_nonqos_seq_query_cmd query_cmd = {
879 		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
880 		.mac_id_n_color =
881 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
882 							mvmvif->color)),
883 		.value = cpu_to_le16(mvmvif->seqno),
884 	};
885 
886 	/* return if called during restart, not resume from D3 */
887 	if (!mvmvif->seqno_valid)
888 		return;
889 
890 	mvmvif->seqno_valid = false;
891 
892 	if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
893 				 sizeof(query_cmd), &query_cmd))
894 		IWL_ERR(mvm, "failed to set non-QoS seqno\n");
895 }
896 
897 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
898 {
899 	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
900 
901 	iwl_mvm_stop_device(mvm);
902 	/*
903 	 * Set the HW restart bit -- this is mostly true as we're
904 	 * going to load new firmware and reprogram that, though
905 	 * the reprogramming is going to be manual to avoid adding
906 	 * all the MACs that aren't support.
907 	 * We don't have to clear up everything though because the
908 	 * reprogramming is manual. When we resume, we'll actually
909 	 * go through a proper restart sequence again to switch
910 	 * back to the runtime firmware image.
911 	 */
912 	set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
913 
914 	/* the fw is reset, so all the keys are cleared */
915 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
916 
917 	mvm->ptk_ivlen = 0;
918 	mvm->ptk_icvlen = 0;
919 	mvm->ptk_ivlen = 0;
920 	mvm->ptk_icvlen = 0;
921 
922 	return iwl_mvm_load_d3_fw(mvm);
923 }
924 
925 static int
926 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
927 			  struct cfg80211_wowlan *wowlan,
928 			  struct iwl_wowlan_config_cmd *wowlan_config_cmd,
929 			  struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
930 			  struct ieee80211_sta *ap_sta)
931 {
932 	struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
933 
934 	/* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
935 
936 	wowlan_config_cmd->is_11n_connection =
937 					ap_sta->deflink.ht_cap.ht_supported;
938 	wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
939 		ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
940 
941 	if (ap_sta->mfp)
942 		wowlan_config_cmd->flags |= IS_11W_ASSOC;
943 
944 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 6) {
945 		/* Query the last used seqno and set it */
946 		int ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
947 
948 		if (ret < 0)
949 			return ret;
950 
951 		wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
952 	}
953 
954 	iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
955 
956 	if (wowlan->disconnect)
957 		wowlan_config_cmd->wakeup_filter |=
958 			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
959 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
960 	if (wowlan->magic_pkt)
961 		wowlan_config_cmd->wakeup_filter |=
962 			cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
963 	if (wowlan->gtk_rekey_failure)
964 		wowlan_config_cmd->wakeup_filter |=
965 			cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
966 	if (wowlan->eap_identity_req)
967 		wowlan_config_cmd->wakeup_filter |=
968 			cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
969 	if (wowlan->four_way_handshake)
970 		wowlan_config_cmd->wakeup_filter |=
971 			cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
972 	if (wowlan->n_patterns)
973 		wowlan_config_cmd->wakeup_filter |=
974 			cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
975 
976 	if (wowlan->rfkill_release)
977 		wowlan_config_cmd->wakeup_filter |=
978 			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
979 
980 	if (wowlan->tcp) {
981 		/*
982 		 * Set the "link change" (really "link lost") flag as well
983 		 * since that implies losing the TCP connection.
984 		 */
985 		wowlan_config_cmd->wakeup_filter |=
986 			cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
987 				    IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
988 				    IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
989 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
990 	}
991 
992 	if (wowlan->any) {
993 		wowlan_config_cmd->wakeup_filter |=
994 			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
995 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE |
996 				    IWL_WOWLAN_WAKEUP_RX_FRAME |
997 				    IWL_WOWLAN_WAKEUP_BCN_FILTERING);
998 	}
999 
1000 	return 0;
1001 }
1002 
1003 static int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
1004 					    struct ieee80211_vif *vif,
1005 					    struct iwl_mvm_vif_link_info *mvm_link)
1006 {
1007 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
1008 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1009 	struct wowlan_key_reprogram_data key_data = {};
1010 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1011 	int ret;
1012 	u8 cmd_ver;
1013 	size_t cmd_size;
1014 
1015 	if (!unified) {
1016 		/*
1017 		 * if we have to configure keys, call ieee80211_iter_keys(),
1018 		 * as we need non-atomic context in order to take the
1019 		 * required locks.
1020 		 */
1021 		/*
1022 		 * Note that currently we don't use CMD_ASYNC in the iterator.
1023 		 * In case of key_data.configure_keys, all the configured
1024 		 * commands are SYNC, and iwl_mvm_wowlan_program_keys() will
1025 		 * take care of locking/unlocking mvm->mutex.
1026 		 */
1027 		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_program_keys,
1028 				    &key_data);
1029 
1030 		if (key_data.error)
1031 			return -EIO;
1032 	}
1033 
1034 	ret = iwl_mvm_wowlan_config_rsc_tsc(mvm, vif, mvm_link);
1035 	if (ret)
1036 		return ret;
1037 
1038 	if (!fw_has_api(&mvm->fw->ucode_capa,
1039 			IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
1040 		int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TKIP_PARAM,
1041 						IWL_FW_CMD_VER_UNKNOWN);
1042 		struct wowlan_key_tkip_data tkip_data = {};
1043 		int size;
1044 
1045 		if (ver == 2) {
1046 			size = sizeof(tkip_data.tkip);
1047 			tkip_data.tkip.sta_id =
1048 				cpu_to_le32(mvm_link->ap_sta_id);
1049 		} else if (ver == 1 || ver == IWL_FW_CMD_VER_UNKNOWN) {
1050 			size = sizeof(struct iwl_wowlan_tkip_params_cmd_ver_1);
1051 		} else {
1052 			WARN_ON_ONCE(1);
1053 			return -EINVAL;
1054 		}
1055 
1056 		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_get_tkip_data,
1057 				    &tkip_data);
1058 
1059 		if (tkip_data.have_tkip_keys) {
1060 			/* send relevant data according to CMD version */
1061 			ret = iwl_mvm_send_cmd_pdu(mvm,
1062 						   WOWLAN_TKIP_PARAM,
1063 						   CMD_ASYNC, size,
1064 						   &tkip_data.tkip);
1065 			if (ret)
1066 				return ret;
1067 		}
1068 	}
1069 
1070 	/* configure rekey data only if offloaded rekey is supported (d3) */
1071 	if (mvmvif->rekey_data.valid) {
1072 		struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {};
1073 		struct iwl_wowlan_kek_kck_material_cmd_v4 *_kek_kck_cmd =
1074 			&kek_kck_cmd;
1075 		struct wowlan_key_gtk_type_iter gtk_type_data = {
1076 			.kek_kck_cmd = _kek_kck_cmd,
1077 		};
1078 
1079 		cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1080 						WOWLAN_KEK_KCK_MATERIAL,
1081 						IWL_FW_CMD_VER_UNKNOWN);
1082 		if (WARN_ON(cmd_ver != 2 && cmd_ver != 3 && cmd_ver != 4 &&
1083 			    cmd_ver != IWL_FW_CMD_VER_UNKNOWN))
1084 			return -EINVAL;
1085 
1086 		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_gtk_type_iter,
1087 				    &gtk_type_data);
1088 
1089 		memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1090 		       mvmvif->rekey_data.kck_len);
1091 		kek_kck_cmd.kck_len = cpu_to_le16(mvmvif->rekey_data.kck_len);
1092 		memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1093 		       mvmvif->rekey_data.kek_len);
1094 		kek_kck_cmd.kek_len = cpu_to_le16(mvmvif->rekey_data.kek_len);
1095 		kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1096 		kek_kck_cmd.akm = cpu_to_le32(mvmvif->rekey_data.akm);
1097 		kek_kck_cmd.sta_id = cpu_to_le32(mvm_link->ap_sta_id);
1098 
1099 		if (cmd_ver == 4) {
1100 			cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v4);
1101 		} else {
1102 			if (cmd_ver == 3)
1103 				cmd_size =
1104 					sizeof(struct iwl_wowlan_kek_kck_material_cmd_v3);
1105 			else
1106 				cmd_size =
1107 					sizeof(struct iwl_wowlan_kek_kck_material_cmd_v2);
1108 			/* skip the sta_id at the beginning */
1109 			_kek_kck_cmd = (void *)
1110 				((u8 *)_kek_kck_cmd + sizeof(kek_kck_cmd.sta_id));
1111 		}
1112 
1113 		IWL_DEBUG_WOWLAN(mvm, "setting akm %d\n",
1114 				 mvmvif->rekey_data.akm);
1115 
1116 		ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_KEK_KCK_MATERIAL,
1117 					   CMD_ASYNC, cmd_size, _kek_kck_cmd);
1118 		if (ret)
1119 			return ret;
1120 	}
1121 
1122 	return 0;
1123 }
1124 
1125 static int
1126 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1127 		      struct cfg80211_wowlan *wowlan,
1128 		      struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1129 		      struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1130 		      struct iwl_mvm_vif_link_info *mvm_link,
1131 		      struct ieee80211_sta *ap_sta)
1132 {
1133 	int ret;
1134 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1135 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1136 
1137 	mvm->offload_tid = wowlan_config_cmd->offloading_tid;
1138 
1139 	if (!unified_image) {
1140 		ret = iwl_mvm_switch_to_d3(mvm);
1141 		if (ret)
1142 			return ret;
1143 
1144 		ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1145 		if (ret)
1146 			return ret;
1147 	}
1148 
1149 	ret = iwl_mvm_wowlan_config_key_params(mvm, vif, mvm_link);
1150 	if (ret)
1151 		return ret;
1152 
1153 	ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1154 				   sizeof(*wowlan_config_cmd),
1155 				   wowlan_config_cmd);
1156 	if (ret)
1157 		return ret;
1158 
1159 	if (fw_has_api(&mvm->fw->ucode_capa,
1160 		       IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE))
1161 		ret = iwl_mvm_send_patterns(mvm, mvm_link, wowlan);
1162 	else
1163 		ret = iwl_mvm_send_patterns_v1(mvm, wowlan);
1164 	if (ret)
1165 		return ret;
1166 
1167 	return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0,
1168 					  mvm_link->ap_sta_id);
1169 }
1170 
1171 static int
1172 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1173 			 struct cfg80211_wowlan *wowlan,
1174 			 struct cfg80211_sched_scan_request *nd_config,
1175 			 struct ieee80211_vif *vif)
1176 {
1177 	int ret;
1178 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1179 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1180 
1181 	if (!unified_image) {
1182 		ret = iwl_mvm_switch_to_d3(mvm);
1183 		if (ret)
1184 			return ret;
1185 	} else {
1186 		/* In theory, we wouldn't have to stop a running sched
1187 		 * scan in order to start another one (for
1188 		 * net-detect).  But in practice this doesn't seem to
1189 		 * work properly, so stop any running sched_scan now.
1190 		 */
1191 		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1192 		if (ret)
1193 			return ret;
1194 	}
1195 
1196 	ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1197 				       IWL_MVM_SCAN_NETDETECT);
1198 	if (ret)
1199 		return ret;
1200 
1201 	if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1202 		return -EBUSY;
1203 
1204 	/* save the sched scan matchsets... */
1205 	if (nd_config->n_match_sets) {
1206 		mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1207 					     sizeof(*nd_config->match_sets) *
1208 					     nd_config->n_match_sets,
1209 					     GFP_KERNEL);
1210 		if (mvm->nd_match_sets)
1211 			mvm->n_nd_match_sets = nd_config->n_match_sets;
1212 	}
1213 
1214 	/* ...and the sched scan channels for later reporting */
1215 	mvm->nd_channels = kmemdup(nd_config->channels,
1216 				   sizeof(*nd_config->channels) *
1217 				   nd_config->n_channels,
1218 				   GFP_KERNEL);
1219 	if (mvm->nd_channels)
1220 		mvm->n_nd_channels = nd_config->n_channels;
1221 
1222 	return 0;
1223 }
1224 
1225 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1226 {
1227 	kfree(mvm->nd_match_sets);
1228 	mvm->nd_match_sets = NULL;
1229 	mvm->n_nd_match_sets = 0;
1230 	kfree(mvm->nd_channels);
1231 	mvm->nd_channels = NULL;
1232 	mvm->n_nd_channels = 0;
1233 }
1234 
1235 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1236 			     struct cfg80211_wowlan *wowlan,
1237 			     bool test)
1238 {
1239 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1240 	struct ieee80211_vif *vif = NULL;
1241 	struct iwl_mvm_vif *mvmvif = NULL;
1242 	struct ieee80211_sta *ap_sta = NULL;
1243 	struct iwl_mvm_vif_link_info *mvm_link;
1244 	struct iwl_d3_manager_config d3_cfg_cmd_data = {
1245 		/*
1246 		 * Program the minimum sleep time to 10 seconds, as many
1247 		 * platforms have issues processing a wakeup signal while
1248 		 * still being in the process of suspending.
1249 		 */
1250 		.min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1251 	};
1252 	struct iwl_host_cmd d3_cfg_cmd = {
1253 		.id = D3_CONFIG_CMD,
1254 		.flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
1255 		.data[0] = &d3_cfg_cmd_data,
1256 		.len[0] = sizeof(d3_cfg_cmd_data),
1257 	};
1258 	int ret;
1259 	int len __maybe_unused;
1260 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1261 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1262 
1263 	if (!wowlan) {
1264 		/*
1265 		 * mac80211 shouldn't get here, but for D3 test
1266 		 * it doesn't warrant a warning
1267 		 */
1268 		WARN_ON(!test);
1269 		return -EINVAL;
1270 	}
1271 
1272 	vif = iwl_mvm_get_bss_vif(mvm);
1273 	if (IS_ERR_OR_NULL(vif))
1274 		return 1;
1275 
1276 	ret = iwl_mvm_block_esr_sync(mvm, vif, IWL_MVM_ESR_BLOCKED_WOWLAN);
1277 	if (ret)
1278 		return ret;
1279 
1280 	mutex_lock(&mvm->mutex);
1281 
1282 	set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1283 
1284 	synchronize_net();
1285 
1286 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1287 
1288 	mvm_link = mvmvif->link[iwl_mvm_get_primary_link(vif)];
1289 	if (WARN_ON_ONCE(!mvm_link)) {
1290 		ret = -EINVAL;
1291 		goto out_noreset;
1292 	}
1293 
1294 	if (mvm_link->ap_sta_id == IWL_MVM_INVALID_STA) {
1295 		/* if we're not associated, this must be netdetect */
1296 		if (!wowlan->nd_config) {
1297 			ret = 1;
1298 			goto out_noreset;
1299 		}
1300 
1301 		ret = iwl_mvm_netdetect_config(
1302 			mvm, wowlan, wowlan->nd_config, vif);
1303 		if (ret)
1304 			goto out;
1305 
1306 		mvm->net_detect = true;
1307 	} else {
1308 		struct iwl_wowlan_config_cmd wowlan_config_cmd = {
1309 			.offloading_tid = 0,
1310 		};
1311 
1312 		wowlan_config_cmd.sta_id = mvm_link->ap_sta_id;
1313 
1314 		ap_sta = rcu_dereference_protected(
1315 			mvm->fw_id_to_mac_id[mvm_link->ap_sta_id],
1316 			lockdep_is_held(&mvm->mutex));
1317 		if (IS_ERR_OR_NULL(ap_sta)) {
1318 			ret = -EINVAL;
1319 			goto out_noreset;
1320 		}
1321 
1322 		ret = iwl_mvm_sta_ensure_queue(
1323 			mvm, ap_sta->txq[wowlan_config_cmd.offloading_tid]);
1324 		if (ret)
1325 			goto out_noreset;
1326 
1327 		ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1328 						vif, mvmvif, ap_sta);
1329 		if (ret)
1330 			goto out_noreset;
1331 		ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1332 					    vif, mvmvif, mvm_link, ap_sta);
1333 		if (ret)
1334 			goto out;
1335 
1336 		mvm->net_detect = false;
1337 	}
1338 
1339 	ret = iwl_mvm_power_update_device(mvm);
1340 	if (ret)
1341 		goto out;
1342 
1343 	ret = iwl_mvm_power_update_mac(mvm);
1344 	if (ret)
1345 		goto out;
1346 
1347 #ifdef CONFIG_IWLWIFI_DEBUGFS
1348 	if (mvm->d3_wake_sysassert)
1349 		d3_cfg_cmd_data.wakeup_flags |=
1350 			cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1351 #endif
1352 
1353 	/*
1354 	 * Prior to 9000 device family the driver needs to stop the dbg
1355 	 * recording before entering D3. In later devices the FW stops the
1356 	 * recording automatically.
1357 	 */
1358 	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1359 		iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
1360 
1361 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1362 
1363 	/* must be last -- this switches firmware state */
1364 	ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1365 	if (ret)
1366 		goto out;
1367 #ifdef CONFIG_IWLWIFI_DEBUGFS
1368 	len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1369 	if (len >= sizeof(u32)) {
1370 		mvm->d3_test_pme_ptr =
1371 			le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1372 	}
1373 #endif
1374 	iwl_free_resp(&d3_cfg_cmd);
1375 
1376 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1377 
1378 	ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1379  out:
1380 	if (ret < 0) {
1381 		iwl_mvm_free_nd(mvm);
1382 
1383 		if (!unified_image) {
1384 			if (mvm->fw_restart > 0) {
1385 				mvm->fw_restart--;
1386 				ieee80211_restart_hw(mvm->hw);
1387 			}
1388 		}
1389 
1390 		clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1391 	}
1392  out_noreset:
1393 	mutex_unlock(&mvm->mutex);
1394 
1395 	return ret;
1396 }
1397 
1398 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1399 {
1400 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1401 
1402 	iwl_mvm_pause_tcm(mvm, true);
1403 
1404 	iwl_fw_runtime_suspend(&mvm->fwrt);
1405 
1406 	return __iwl_mvm_suspend(hw, wowlan, false);
1407 }
1408 
1409 struct iwl_multicast_key_data {
1410 	u8 key[WOWLAN_KEY_MAX_SIZE];
1411 	u8 len;
1412 	u8 flags;
1413 	u8 id;
1414 	u8 ipn[6];
1415 };
1416 
1417 /* converted data from the different status responses */
1418 struct iwl_wowlan_status_data {
1419 	u64 replay_ctr;
1420 	u32 num_of_gtk_rekeys;
1421 	u32 received_beacons;
1422 	u32 wakeup_reasons;
1423 	u32 wake_packet_length;
1424 	u32 wake_packet_bufsize;
1425 	u16 pattern_number;
1426 	u16 non_qos_seq_ctr;
1427 	u16 qos_seq_ctr[8];
1428 	u8 tid_tear_down;
1429 
1430 	struct {
1431 		/* including RX MIC key for TKIP */
1432 		u8 key[WOWLAN_KEY_MAX_SIZE];
1433 		u8 len;
1434 		u8 flags;
1435 		u8 id;
1436 	} gtk[WOWLAN_GTK_KEYS_NUM];
1437 
1438 	struct {
1439 		/*
1440 		 * We store both the TKIP and AES representations
1441 		 * coming from the firmware because we decode the
1442 		 * data from there before we iterate the keys and
1443 		 * know which one we need.
1444 		 */
1445 		struct {
1446 			struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1447 		} tkip, aes;
1448 
1449 		/*
1450 		 * We use -1 for when we have valid data but don't know
1451 		 * the key ID from firmware, and thus it needs to be
1452 		 * installed with the last key (depending on rekeying).
1453 		 */
1454 		s8 key_id;
1455 		bool valid;
1456 	} gtk_seq[2];
1457 
1458 	struct {
1459 		/* Same as above */
1460 		struct {
1461 			struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1462 			u64 tx_pn;
1463 		} tkip, aes;
1464 	} ptk;
1465 
1466 	struct iwl_multicast_key_data igtk;
1467 	struct iwl_multicast_key_data bigtk[WOWLAN_BIGTK_KEYS_NUM];
1468 
1469 	int num_mlo_keys;
1470 	struct iwl_wowlan_mlo_gtk mlo_keys[WOWLAN_MAX_MLO_KEYS];
1471 
1472 	u8 *wake_packet;
1473 };
1474 
1475 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1476 					  struct ieee80211_vif *vif,
1477 					  struct iwl_wowlan_status_data *status)
1478 {
1479 	struct sk_buff *pkt = NULL;
1480 	struct cfg80211_wowlan_wakeup wakeup = {
1481 		.pattern_idx = -1,
1482 	};
1483 	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1484 	u32 reasons = status->wakeup_reasons;
1485 
1486 	if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1487 		wakeup_report = NULL;
1488 		goto report;
1489 	}
1490 
1491 	pm_wakeup_event(mvm->dev, 0);
1492 
1493 	if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1494 		wakeup.magic_pkt = true;
1495 
1496 	if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1497 		wakeup.pattern_idx =
1498 			status->pattern_number;
1499 
1500 	if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1501 		       IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH |
1502 		       IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE))
1503 		wakeup.disconnect = true;
1504 
1505 	if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1506 		wakeup.gtk_rekey_failure = true;
1507 
1508 	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1509 		wakeup.rfkill_release = true;
1510 
1511 	if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1512 		wakeup.eap_identity_req = true;
1513 
1514 	if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1515 		wakeup.four_way_handshake = true;
1516 
1517 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1518 		wakeup.tcp_connlost = true;
1519 
1520 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1521 		wakeup.tcp_nomoretokens = true;
1522 
1523 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1524 		wakeup.tcp_match = true;
1525 
1526 	if (reasons & IWL_WAKEUP_BY_11W_UNPROTECTED_DEAUTH_OR_DISASSOC)
1527 		wakeup.unprot_deauth_disassoc = true;
1528 
1529 	if (status->wake_packet) {
1530 		int pktsize = status->wake_packet_bufsize;
1531 		int pktlen = status->wake_packet_length;
1532 		const u8 *pktdata = status->wake_packet;
1533 		const struct ieee80211_hdr *hdr = (const void *)pktdata;
1534 		int truncated = pktlen - pktsize;
1535 
1536 		/* this would be a firmware bug */
1537 		if (WARN_ON_ONCE(truncated < 0))
1538 			truncated = 0;
1539 
1540 		if (ieee80211_is_data(hdr->frame_control)) {
1541 			int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1542 			int ivlen = 0, icvlen = 4; /* also FCS */
1543 
1544 			pkt = alloc_skb(pktsize, GFP_KERNEL);
1545 			if (!pkt)
1546 				goto report;
1547 
1548 			skb_put_data(pkt, pktdata, hdrlen);
1549 			pktdata += hdrlen;
1550 			pktsize -= hdrlen;
1551 
1552 			if (ieee80211_has_protected(hdr->frame_control)) {
1553 				/*
1554 				 * This is unlocked and using gtk_i(c)vlen,
1555 				 * but since everything is under RTNL still
1556 				 * that's not really a problem - changing
1557 				 * it would be difficult.
1558 				 */
1559 				if (is_multicast_ether_addr(hdr->addr1)) {
1560 					ivlen = mvm->gtk_ivlen;
1561 					icvlen += mvm->gtk_icvlen;
1562 				} else {
1563 					ivlen = mvm->ptk_ivlen;
1564 					icvlen += mvm->ptk_icvlen;
1565 				}
1566 			}
1567 
1568 			/* if truncated, FCS/ICV is (partially) gone */
1569 			if (truncated >= icvlen) {
1570 				icvlen = 0;
1571 				truncated -= icvlen;
1572 			} else {
1573 				icvlen -= truncated;
1574 				truncated = 0;
1575 			}
1576 
1577 			pktsize -= ivlen + icvlen;
1578 			pktdata += ivlen;
1579 
1580 			skb_put_data(pkt, pktdata, pktsize);
1581 
1582 			if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1583 				goto report;
1584 			wakeup.packet = pkt->data;
1585 			wakeup.packet_present_len = pkt->len;
1586 			wakeup.packet_len = pkt->len - truncated;
1587 			wakeup.packet_80211 = false;
1588 		} else {
1589 			int fcslen = 4;
1590 
1591 			if (truncated >= 4) {
1592 				truncated -= 4;
1593 				fcslen = 0;
1594 			} else {
1595 				fcslen -= truncated;
1596 				truncated = 0;
1597 			}
1598 			pktsize -= fcslen;
1599 			wakeup.packet = status->wake_packet;
1600 			wakeup.packet_present_len = pktsize;
1601 			wakeup.packet_len = pktlen - truncated;
1602 			wakeup.packet_80211 = true;
1603 		}
1604 	}
1605 
1606  report:
1607 	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1608 	kfree_skb(pkt);
1609 }
1610 
1611 static void iwl_mvm_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1612 {
1613 	u64 pn = le64_to_cpu(le_pn);
1614 
1615 	seq->ccmp.pn[0] = pn >> 40;
1616 	seq->ccmp.pn[1] = pn >> 32;
1617 	seq->ccmp.pn[2] = pn >> 24;
1618 	seq->ccmp.pn[3] = pn >> 16;
1619 	seq->ccmp.pn[4] = pn >> 8;
1620 	seq->ccmp.pn[5] = pn;
1621 }
1622 
1623 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1624 				  struct ieee80211_key_seq *seq)
1625 {
1626 	iwl_mvm_le64_to_aes_seq(sc->pn, seq);
1627 }
1628 
1629 static void iwl_mvm_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1630 {
1631 	u64 pn = le64_to_cpu(le_pn);
1632 
1633 	seq->tkip.iv16 = (u16)pn;
1634 	seq->tkip.iv32 = (u32)(pn >> 16);
1635 }
1636 
1637 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1638 				   struct ieee80211_key_seq *seq)
1639 {
1640 	seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1641 	seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1642 }
1643 
1644 static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key,
1645 					struct ieee80211_key_seq *seq)
1646 {
1647 	int tid;
1648 
1649 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1650 		ieee80211_set_key_rx_seq(key, tid, &seq[tid]);
1651 }
1652 
1653 static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm,
1654 				       struct iwl_wowlan_status_data *status,
1655 				       struct ieee80211_sta *sta,
1656 				       struct ieee80211_key_conf *key)
1657 {
1658 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1659 	struct iwl_mvm_key_pn *ptk_pn;
1660 	int tid;
1661 
1662 	iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq);
1663 
1664 	if (!iwl_mvm_has_new_rx_api(mvm))
1665 		return;
1666 
1667 
1668 	rcu_read_lock();
1669 	ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
1670 	if (WARN_ON(!ptk_pn)) {
1671 		rcu_read_unlock();
1672 		return;
1673 	}
1674 
1675 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1676 		int i;
1677 
1678 		for (i = 1; i < mvm->trans->num_rx_queues; i++)
1679 			memcpy(ptk_pn->q[i].pn[tid],
1680 			       status->ptk.aes.seq[tid].ccmp.pn,
1681 			       IEEE80211_CCMP_PN_LEN);
1682 	}
1683 	rcu_read_unlock();
1684 }
1685 
1686 static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status,
1687 					 union iwl_all_tsc_rsc *sc)
1688 {
1689 	int i;
1690 
1691 	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1692 	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1693 
1694 	/* GTK RX counters */
1695 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1696 		iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i],
1697 				       &status->gtk_seq[0].tkip.seq[i]);
1698 		iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i],
1699 				      &status->gtk_seq[0].aes.seq[i]);
1700 	}
1701 	status->gtk_seq[0].valid = true;
1702 	status->gtk_seq[0].key_id = -1;
1703 
1704 	/* PTK TX counter */
1705 	status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) |
1706 				 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16);
1707 	status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn);
1708 
1709 	/* PTK RX counters */
1710 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1711 		iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i],
1712 				       &status->ptk.tkip.seq[i]);
1713 		iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i],
1714 				      &status->ptk.aes.seq[i]);
1715 	}
1716 }
1717 
1718 static void
1719 iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data *status,
1720 					struct iwl_wowlan_all_rsc_tsc_v5 *sc,
1721 					unsigned int idx, unsigned int key_id)
1722 {
1723 	int tid;
1724 
1725 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1726 		iwl_mvm_le64_to_tkip_seq(sc->mcast_rsc[idx][tid],
1727 					 &status->gtk_seq[idx].tkip.seq[tid]);
1728 		iwl_mvm_le64_to_aes_seq(sc->mcast_rsc[idx][tid],
1729 					&status->gtk_seq[idx].aes.seq[tid]);
1730 	}
1731 
1732 	status->gtk_seq[idx].valid = true;
1733 	status->gtk_seq[idx].key_id = key_id;
1734 }
1735 
1736 static void
1737 iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data *status,
1738 				struct iwl_wowlan_all_rsc_tsc_v5 *sc)
1739 {
1740 	int i, tid;
1741 
1742 	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1743 	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1744 	BUILD_BUG_ON(ARRAY_SIZE(sc->mcast_rsc) != ARRAY_SIZE(status->gtk_seq));
1745 
1746 	/* GTK RX counters */
1747 	for (i = 0; i < ARRAY_SIZE(sc->mcast_key_id_map); i++) {
1748 		u8 entry = sc->mcast_key_id_map[i];
1749 
1750 		if (entry < ARRAY_SIZE(sc->mcast_rsc))
1751 			iwl_mvm_convert_key_counters_v5_gtk_seq(status, sc,
1752 								entry, i);
1753 	}
1754 
1755 	/* PTK TX counters not needed, assigned in device */
1756 
1757 	/* PTK RX counters */
1758 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1759 		iwl_mvm_le64_to_tkip_seq(sc->ucast_rsc[tid],
1760 					 &status->ptk.tkip.seq[tid]);
1761 		iwl_mvm_le64_to_aes_seq(sc->ucast_rsc[tid],
1762 					&status->ptk.aes.seq[tid]);
1763 	}
1764 }
1765 
1766 static void iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf *key,
1767 				       struct iwl_wowlan_status_data *status,
1768 				       int idx)
1769 {
1770 	switch (key->cipher) {
1771 	case WLAN_CIPHER_SUITE_CCMP:
1772 	case WLAN_CIPHER_SUITE_GCMP:
1773 	case WLAN_CIPHER_SUITE_GCMP_256:
1774 		iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].aes.seq);
1775 		break;
1776 	case WLAN_CIPHER_SUITE_TKIP:
1777 		iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].tkip.seq);
1778 		break;
1779 	default:
1780 		WARN_ON(1);
1781 	}
1782 }
1783 
1784 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1785 				   struct iwl_wowlan_status_data *status,
1786 				   bool installed)
1787 {
1788 	int i;
1789 
1790 	for (i = 0; i < ARRAY_SIZE(status->gtk_seq); i++) {
1791 		if (!status->gtk_seq[i].valid)
1792 			continue;
1793 
1794 		/* Handle the case where we know the key ID */
1795 		if (status->gtk_seq[i].key_id == key->keyidx) {
1796 			s8 new_key_id = -1;
1797 
1798 			if (status->num_of_gtk_rekeys)
1799 				new_key_id = status->gtk[0].flags &
1800 						IWL_WOWLAN_GTK_IDX_MASK;
1801 
1802 			/* Don't install a new key's value to an old key */
1803 			if (new_key_id != key->keyidx)
1804 				iwl_mvm_set_key_rx_seq_idx(key, status, i);
1805 			continue;
1806 		}
1807 
1808 		/* handle the case where we didn't, last key only */
1809 		if (status->gtk_seq[i].key_id == -1 &&
1810 		    (!status->num_of_gtk_rekeys || installed))
1811 			iwl_mvm_set_key_rx_seq_idx(key, status, i);
1812 	}
1813 }
1814 
1815 struct iwl_mvm_d3_gtk_iter_data {
1816 	struct iwl_mvm *mvm;
1817 	struct iwl_wowlan_status_data *status;
1818 	u32 gtk_cipher, igtk_cipher, bigtk_cipher;
1819 	bool unhandled_cipher, igtk_support, bigtk_support;
1820 	int num_keys;
1821 };
1822 
1823 static void iwl_mvm_d3_find_last_keys(struct ieee80211_hw *hw,
1824 				      struct ieee80211_vif *vif,
1825 				      struct ieee80211_sta *sta,
1826 				      struct ieee80211_key_conf *key,
1827 				      void *_data)
1828 {
1829 	struct iwl_mvm_d3_gtk_iter_data *data = _data;
1830 	int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1831 
1832 	if (link_id >= 0 && key->link_id >= 0 && link_id != key->link_id)
1833 		return;
1834 
1835 	if (data->unhandled_cipher)
1836 		return;
1837 
1838 	switch (key->cipher) {
1839 	case WLAN_CIPHER_SUITE_WEP40:
1840 	case WLAN_CIPHER_SUITE_WEP104:
1841 		/* ignore WEP completely, nothing to do */
1842 		return;
1843 	case WLAN_CIPHER_SUITE_CCMP:
1844 	case WLAN_CIPHER_SUITE_GCMP:
1845 	case WLAN_CIPHER_SUITE_GCMP_256:
1846 	case WLAN_CIPHER_SUITE_TKIP:
1847 		/* we support these */
1848 		data->gtk_cipher = key->cipher;
1849 		break;
1850 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1851 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1852 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1853 	case WLAN_CIPHER_SUITE_AES_CMAC:
1854 		/* we support these */
1855 		if (data->igtk_support &&
1856 		    (key->keyidx == 4 || key->keyidx == 5)) {
1857 			data->igtk_cipher = key->cipher;
1858 		} else if (data->bigtk_support &&
1859 			   (key->keyidx == 6 || key->keyidx == 7)) {
1860 			data->bigtk_cipher = key->cipher;
1861 		} else {
1862 			data->unhandled_cipher = true;
1863 			return;
1864 		}
1865 		break;
1866 	default:
1867 		/* everything else - disconnect from AP */
1868 		data->unhandled_cipher = true;
1869 		return;
1870 	}
1871 
1872 	data->num_keys++;
1873 }
1874 
1875 static void
1876 iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data *key,
1877 			      struct ieee80211_key_seq *seq, u32 cipher)
1878 {
1879 	switch (cipher) {
1880 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1881 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1882 		BUILD_BUG_ON(sizeof(seq->aes_gmac.pn) != sizeof(key->ipn));
1883 		memcpy(seq->aes_gmac.pn, key->ipn, sizeof(seq->aes_gmac.pn));
1884 		break;
1885 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1886 	case WLAN_CIPHER_SUITE_AES_CMAC:
1887 		BUILD_BUG_ON(sizeof(seq->aes_cmac.pn) != sizeof(key->ipn));
1888 		memcpy(seq->aes_cmac.pn, key->ipn, sizeof(seq->aes_cmac.pn));
1889 		break;
1890 	default:
1891 		WARN_ON(1);
1892 	}
1893 }
1894 
1895 static void
1896 iwl_mvm_d3_update_igtk_bigtk(struct iwl_wowlan_status_data *status,
1897 			     struct ieee80211_key_conf *key,
1898 			     struct iwl_multicast_key_data *key_data)
1899 {
1900 	if (status->num_of_gtk_rekeys && key_data->len) {
1901 		/* remove rekeyed key */
1902 		ieee80211_remove_key(key);
1903 	} else {
1904 		struct ieee80211_key_seq seq;
1905 
1906 		iwl_mvm_d3_set_igtk_bigtk_ipn(key_data,
1907 					      &seq,
1908 					      key->cipher);
1909 		ieee80211_set_key_rx_seq(key, 0, &seq);
1910 	}
1911 }
1912 
1913 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1914 				   struct ieee80211_vif *vif,
1915 				   struct ieee80211_sta *sta,
1916 				   struct ieee80211_key_conf *key,
1917 				   void *_data)
1918 {
1919 	struct iwl_mvm_d3_gtk_iter_data *data = _data;
1920 	struct iwl_wowlan_status_data *status = data->status;
1921 	s8 keyidx;
1922 	int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1923 
1924 	if (link_id >= 0 && key->link_id >= 0 && link_id != key->link_id)
1925 		return;
1926 
1927 	if (data->unhandled_cipher)
1928 		return;
1929 
1930 	switch (key->cipher) {
1931 	case WLAN_CIPHER_SUITE_WEP40:
1932 	case WLAN_CIPHER_SUITE_WEP104:
1933 		/* ignore WEP completely, nothing to do */
1934 		return;
1935 	case WLAN_CIPHER_SUITE_CCMP:
1936 	case WLAN_CIPHER_SUITE_GCMP:
1937 	case WLAN_CIPHER_SUITE_GCMP_256:
1938 		if (sta) {
1939 			atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn);
1940 			iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key);
1941 			return;
1942 		}
1943 		fallthrough;
1944 	case WLAN_CIPHER_SUITE_TKIP:
1945 		if (sta) {
1946 			atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn);
1947 			iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq);
1948 			return;
1949 		}
1950 		keyidx = key->keyidx;
1951 		/* The current key is always sent by the FW, even if it wasn't
1952 		 * rekeyed during D3.
1953 		 * We remove an existing key if it has the same index as
1954 		 * a new key
1955 		 */
1956 		if (status->num_of_gtk_rekeys &&
1957 		    ((status->gtk[0].len && keyidx == status->gtk[0].id) ||
1958 		     (status->gtk[1].len && keyidx == status->gtk[1].id))) {
1959 			ieee80211_remove_key(key);
1960 		} else {
1961 			iwl_mvm_set_key_rx_seq(key, data->status, false);
1962 		}
1963 		break;
1964 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1965 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1966 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1967 	case WLAN_CIPHER_SUITE_AES_CMAC:
1968 		if (key->keyidx == 4 || key->keyidx == 5) {
1969 			iwl_mvm_d3_update_igtk_bigtk(status, key,
1970 						     &status->igtk);
1971 		}
1972 		if (key->keyidx == 6 || key->keyidx == 7) {
1973 			u8 idx = key->keyidx == status->bigtk[1].id;
1974 
1975 			iwl_mvm_d3_update_igtk_bigtk(status, key,
1976 						     &status->bigtk[idx]);
1977 		}
1978 	}
1979 }
1980 
1981 struct iwl_mvm_d3_mlo_old_keys {
1982 	u32 cipher[IEEE80211_MLD_MAX_NUM_LINKS][WOWLAN_MLO_GTK_KEY_NUM_TYPES];
1983 	struct ieee80211_key_conf *key[IEEE80211_MLD_MAX_NUM_LINKS][8];
1984 };
1985 
1986 static void iwl_mvm_mlo_key_ciphers(struct ieee80211_hw *hw,
1987 				    struct ieee80211_vif *vif,
1988 				    struct ieee80211_sta *sta,
1989 				    struct ieee80211_key_conf *key,
1990 				    void *data)
1991 {
1992 	struct iwl_mvm_d3_mlo_old_keys *old_keys = data;
1993 	enum iwl_wowlan_mlo_gtk_type key_type;
1994 
1995 	if (key->link_id < 0)
1996 		return;
1997 
1998 	if (WARN_ON(key->link_id >= IEEE80211_MLD_MAX_NUM_LINKS ||
1999 		    key->keyidx >= 8))
2000 		return;
2001 
2002 	if (WARN_ON(old_keys->key[key->link_id][key->keyidx]))
2003 		return;
2004 
2005 	switch (key->cipher) {
2006 	case WLAN_CIPHER_SUITE_CCMP:
2007 	case WLAN_CIPHER_SUITE_GCMP:
2008 	case WLAN_CIPHER_SUITE_GCMP_256:
2009 		key_type = WOWLAN_MLO_GTK_KEY_TYPE_GTK;
2010 		break;
2011 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2012 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2013 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2014 	case WLAN_CIPHER_SUITE_AES_CMAC:
2015 		if (key->keyidx == 4 || key->keyidx == 5) {
2016 			key_type = WOWLAN_MLO_GTK_KEY_TYPE_IGTK;
2017 			break;
2018 		} else if (key->keyidx == 6 || key->keyidx == 7) {
2019 			key_type = WOWLAN_MLO_GTK_KEY_TYPE_BIGTK;
2020 			break;
2021 		}
2022 		return;
2023 	default:
2024 		/* ignore WEP/TKIP or unknown ciphers */
2025 		return;
2026 	}
2027 
2028 	old_keys->cipher[key->link_id][key_type] = key->cipher;
2029 	old_keys->key[key->link_id][key->keyidx] = key;
2030 }
2031 
2032 static bool iwl_mvm_mlo_gtk_rekey(struct iwl_wowlan_status_data *status,
2033 				  struct ieee80211_vif *vif,
2034 				  struct iwl_mvm *mvm)
2035 {
2036 	int i;
2037 	struct iwl_mvm_d3_mlo_old_keys *old_keys;
2038 	bool ret = true;
2039 
2040 	IWL_DEBUG_WOWLAN(mvm, "Num of MLO Keys: %d\n", status->num_mlo_keys);
2041 	if (!status->num_mlo_keys)
2042 		return true;
2043 
2044 	old_keys = kzalloc(sizeof(*old_keys), GFP_KERNEL);
2045 	if (!old_keys)
2046 		return false;
2047 
2048 	/* find the cipher for each mlo key */
2049 	ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_mlo_key_ciphers, old_keys);
2050 
2051 	for (i = 0; i < status->num_mlo_keys; i++) {
2052 		struct iwl_wowlan_mlo_gtk *mlo_key = &status->mlo_keys[i];
2053 		struct ieee80211_key_conf *key, *old_key;
2054 		struct ieee80211_key_seq seq;
2055 		struct {
2056 			struct ieee80211_key_conf conf;
2057 			u8 key[32];
2058 		} conf = {};
2059 		u16 flags = le16_to_cpu(mlo_key->flags);
2060 		int j, link_id, key_id, key_type;
2061 
2062 		link_id = u16_get_bits(flags, WOWLAN_MLO_GTK_FLAG_LINK_ID_MSK);
2063 		key_id = u16_get_bits(flags, WOWLAN_MLO_GTK_FLAG_KEY_ID_MSK);
2064 		key_type = u16_get_bits(flags,
2065 					WOWLAN_MLO_GTK_FLAG_KEY_TYPE_MSK);
2066 
2067 		if (!(vif->valid_links & BIT(link_id)))
2068 			continue;
2069 
2070 		if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS ||
2071 			    key_id >= 8 ||
2072 			    key_type >= WOWLAN_MLO_GTK_KEY_NUM_TYPES))
2073 			continue;
2074 
2075 		conf.conf.cipher = old_keys->cipher[link_id][key_type];
2076 		/* WARN_ON? */
2077 		if (!conf.conf.cipher)
2078 			continue;
2079 
2080 		conf.conf.keylen = 0;
2081 		switch (conf.conf.cipher) {
2082 		case WLAN_CIPHER_SUITE_CCMP:
2083 		case WLAN_CIPHER_SUITE_GCMP:
2084 			conf.conf.keylen = WLAN_KEY_LEN_CCMP;
2085 			break;
2086 		case WLAN_CIPHER_SUITE_GCMP_256:
2087 			conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
2088 			break;
2089 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2090 			conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2091 			break;
2092 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2093 			conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2094 			break;
2095 		case WLAN_CIPHER_SUITE_AES_CMAC:
2096 			conf.conf.keylen = WLAN_KEY_LEN_AES_CMAC;
2097 			break;
2098 		case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2099 			conf.conf.keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2100 			break;
2101 		}
2102 
2103 		if (WARN_ON(!conf.conf.keylen ||
2104 			    conf.conf.keylen > sizeof(conf.key)))
2105 			continue;
2106 
2107 		memcpy(conf.conf.key, mlo_key->key, conf.conf.keylen);
2108 		conf.conf.keyidx = key_id;
2109 
2110 		old_key = old_keys->key[link_id][key_id];
2111 		if (old_key) {
2112 			IWL_DEBUG_WOWLAN(mvm,
2113 					 "Remove MLO key id %d, link id %d\n",
2114 					 key_id, link_id);
2115 			ieee80211_remove_key(old_key);
2116 		}
2117 
2118 		IWL_DEBUG_WOWLAN(mvm, "Add MLO key id %d, link id %d\n",
2119 				 key_id, link_id);
2120 		key = ieee80211_gtk_rekey_add(vif, &conf.conf, link_id);
2121 		if (WARN_ON(IS_ERR(key))) {
2122 			ret = false;
2123 			goto out;
2124 		}
2125 
2126 		/*
2127 		 * mac80211 expects the pn in big-endian
2128 		 * also note that seq is a union of all cipher types
2129 		 * (ccmp, gcmp, cmac, gmac), and they all have the same
2130 		 * pn field (of length 6) so just copy it to ccmp.pn.
2131 		 */
2132 		for (j = 5; j >= 0; j--)
2133 			seq.ccmp.pn[5 - j] = mlo_key->pn[j];
2134 
2135 		/* group keys are non-QoS and use TID 0 */
2136 		ieee80211_set_key_rx_seq(key, 0, &seq);
2137 	}
2138 
2139 out:
2140 	kfree(old_keys);
2141 	return ret;
2142 }
2143 
2144 static bool iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data *status,
2145 			      struct ieee80211_vif *vif,
2146 			      struct iwl_mvm *mvm, u32 gtk_cipher)
2147 {
2148 	int i, j;
2149 	struct ieee80211_key_conf *key;
2150 	struct {
2151 		struct ieee80211_key_conf conf;
2152 		u8 key[32];
2153 	} conf = {
2154 		.conf.cipher = gtk_cipher,
2155 	};
2156 	int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
2157 
2158 	BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
2159 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP);
2160 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256);
2161 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP);
2162 	BUILD_BUG_ON(sizeof(conf.key) < sizeof(status->gtk[0].key));
2163 
2164 	switch (gtk_cipher) {
2165 	case WLAN_CIPHER_SUITE_CCMP:
2166 	case WLAN_CIPHER_SUITE_GCMP:
2167 		conf.conf.keylen = WLAN_KEY_LEN_CCMP;
2168 		break;
2169 	case WLAN_CIPHER_SUITE_GCMP_256:
2170 		conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
2171 		break;
2172 	case WLAN_CIPHER_SUITE_TKIP:
2173 		conf.conf.keylen = WLAN_KEY_LEN_TKIP;
2174 		break;
2175 	default:
2176 		WARN_ON(1);
2177 	}
2178 
2179 	for (i = 0; i < ARRAY_SIZE(status->gtk); i++) {
2180 		if (!status->gtk[i].len)
2181 			continue;
2182 
2183 		conf.conf.keyidx = status->gtk[i].id;
2184 		IWL_DEBUG_WOWLAN(mvm,
2185 				 "Received from FW GTK cipher %d, key index %d\n",
2186 				 conf.conf.cipher, conf.conf.keyidx);
2187 		memcpy(conf.conf.key, status->gtk[i].key,
2188 		       sizeof(status->gtk[i].key));
2189 
2190 		key = ieee80211_gtk_rekey_add(vif, &conf.conf, link_id);
2191 		if (IS_ERR(key))
2192 			return false;
2193 
2194 		for (j = 0; j < ARRAY_SIZE(status->gtk_seq); j++) {
2195 			if (!status->gtk_seq[j].valid ||
2196 			    status->gtk_seq[j].key_id != key->keyidx)
2197 				continue;
2198 			iwl_mvm_set_key_rx_seq_idx(key, status, j);
2199 			break;
2200 		}
2201 		WARN_ON(j == ARRAY_SIZE(status->gtk_seq));
2202 	}
2203 
2204 	return true;
2205 }
2206 
2207 static bool
2208 iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data *status,
2209 				struct ieee80211_vif *vif, u32 cipher,
2210 				struct iwl_multicast_key_data *key_data)
2211 {
2212 	struct ieee80211_key_conf *key_config;
2213 	struct {
2214 		struct ieee80211_key_conf conf;
2215 		u8 key[WOWLAN_KEY_MAX_SIZE];
2216 	} conf = {
2217 		.conf.cipher = cipher,
2218 		.conf.keyidx = key_data->id,
2219 	};
2220 	struct ieee80211_key_seq seq;
2221 	int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
2222 
2223 	if (!key_data->len)
2224 		return true;
2225 
2226 	iwl_mvm_d3_set_igtk_bigtk_ipn(key_data, &seq, conf.conf.cipher);
2227 
2228 	switch (cipher) {
2229 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2230 		conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2231 		break;
2232 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2233 		conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2234 		break;
2235 	case WLAN_CIPHER_SUITE_AES_CMAC:
2236 		conf.conf.keylen = WLAN_KEY_LEN_AES_CMAC;
2237 		break;
2238 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2239 		conf.conf.keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2240 		break;
2241 	default:
2242 		WARN_ON(1);
2243 	}
2244 	BUILD_BUG_ON(sizeof(conf.key) < sizeof(key_data->key));
2245 	memcpy(conf.conf.key, key_data->key, conf.conf.keylen);
2246 
2247 	key_config = ieee80211_gtk_rekey_add(vif, &conf.conf, link_id);
2248 	if (IS_ERR(key_config))
2249 		return false;
2250 	ieee80211_set_key_rx_seq(key_config, 0, &seq);
2251 
2252 	if (key_config->keyidx == 4 || key_config->keyidx == 5) {
2253 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2254 		struct iwl_mvm_vif_link_info *mvm_link;
2255 
2256 		link_id = link_id < 0 ? 0 : link_id;
2257 		mvm_link = mvmvif->link[link_id];
2258 		mvm_link->igtk = key_config;
2259 	}
2260 
2261 	return true;
2262 }
2263 
2264 static int iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm *mvm)
2265 {
2266 	u8 notif_ver;
2267 
2268 	if (!fw_has_api(&mvm->fw->ucode_capa,
2269 			IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL))
2270 		return 6;
2271 
2272 	/* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2273 	notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2274 					    WOWLAN_GET_STATUSES, 0);
2275 	if (!notif_ver)
2276 		notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
2277 						    WOWLAN_GET_STATUSES, 7);
2278 
2279 	return notif_ver;
2280 }
2281 
2282 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
2283 					  struct ieee80211_vif *vif,
2284 					  struct iwl_wowlan_status_data *status)
2285 {
2286 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2287 	struct iwl_mvm_d3_gtk_iter_data gtkdata = {
2288 		.mvm = mvm,
2289 		.status = status,
2290 	};
2291 	int i;
2292 	u32 disconnection_reasons =
2293 		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
2294 		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
2295 
2296 	if (!status || !vif->bss_conf.bssid)
2297 		return false;
2298 
2299 	if (iwl_mvm_lookup_wowlan_status_ver(mvm) > 6 ||
2300 	    iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2301 				    WOWLAN_INFO_NOTIFICATION,
2302 				    0))
2303 		gtkdata.igtk_support = true;
2304 
2305 	if (iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2306 				    WOWLAN_INFO_NOTIFICATION,
2307 				    0) >= 3)
2308 		gtkdata.bigtk_support = true;
2309 
2310 	/* find last GTK that we used initially, if any */
2311 	ieee80211_iter_keys(mvm->hw, vif,
2312 			    iwl_mvm_d3_find_last_keys, &gtkdata);
2313 	/* not trying to keep connections with MFP/unhandled ciphers */
2314 	if (gtkdata.unhandled_cipher)
2315 		return false;
2316 	if (!gtkdata.num_keys)
2317 		goto out;
2318 
2319 	/*
2320 	 * invalidate all other GTKs that might still exist and update
2321 	 * the one that we used
2322 	 */
2323 	ieee80211_iter_keys(mvm->hw, vif,
2324 			    iwl_mvm_d3_update_keys, &gtkdata);
2325 
2326 	if (status->num_of_gtk_rekeys) {
2327 		__be64 replay_ctr = cpu_to_be64(status->replay_ctr);
2328 
2329 		IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
2330 				 status->num_of_gtk_rekeys);
2331 
2332 		if (!iwl_mvm_gtk_rekey(status, vif, mvm, gtkdata.gtk_cipher))
2333 			return false;
2334 
2335 		if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2336 						     gtkdata.igtk_cipher,
2337 						     &status->igtk))
2338 			return false;
2339 
2340 		for (i = 0; i < ARRAY_SIZE(status->bigtk); i++) {
2341 			if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2342 							     gtkdata.bigtk_cipher,
2343 							     &status->bigtk[i]))
2344 				return false;
2345 		}
2346 
2347 		if (!iwl_mvm_mlo_gtk_rekey(status, vif, mvm))
2348 			return false;
2349 
2350 		ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
2351 					   (void *)&replay_ctr, GFP_KERNEL);
2352 	}
2353 
2354 out:
2355 	if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2356 				    WOWLAN_GET_STATUSES,
2357 				    IWL_FW_CMD_VER_UNKNOWN) < 10) {
2358 		mvmvif->seqno_valid = true;
2359 		/* +0x10 because the set API expects next-to-use, not last-used */
2360 		mvmvif->seqno = status->non_qos_seq_ctr + 0x10;
2361 	}
2362 
2363 	if (status->wakeup_reasons & disconnection_reasons)
2364 		return false;
2365 
2366 	return true;
2367 }
2368 
2369 static void iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data *status,
2370 				   struct iwl_wowlan_gtk_status_v2 *data)
2371 {
2372 	BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data->key));
2373 	BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2374 		     sizeof(data->tkip_mic_key) >
2375 		     sizeof(status->gtk[0].key));
2376 
2377 	status->gtk[0].len = data->key_len;
2378 	status->gtk[0].flags = data->key_flags;
2379 
2380 	memcpy(status->gtk[0].key, data->key, sizeof(data->key));
2381 
2382 	/* if it's as long as the TKIP encryption key, copy MIC key */
2383 	if (status->gtk[0].len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2384 		memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2385 		       data->tkip_mic_key, sizeof(data->tkip_mic_key));
2386 }
2387 
2388 static void iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data *status,
2389 				   struct iwl_wowlan_gtk_status_v3 *data)
2390 {
2391 	int data_idx, status_idx = 0;
2392 
2393 	BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data[0].key));
2394 	BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2395 		     sizeof(data[0].tkip_mic_key) >
2396 		     sizeof(status->gtk[0].key));
2397 	BUILD_BUG_ON(ARRAY_SIZE(status->gtk) < WOWLAN_GTK_KEYS_NUM);
2398 	for (data_idx = 0; data_idx < ARRAY_SIZE(status->gtk); data_idx++) {
2399 		if (!(data[data_idx].key_len))
2400 			continue;
2401 		status->gtk[status_idx].len = data[data_idx].key_len;
2402 		status->gtk[status_idx].flags = data[data_idx].key_flags;
2403 		status->gtk[status_idx].id = status->gtk[status_idx].flags &
2404 				    IWL_WOWLAN_GTK_IDX_MASK;
2405 
2406 		memcpy(status->gtk[status_idx].key, data[data_idx].key,
2407 		       sizeof(data[data_idx].key));
2408 
2409 		/* if it's as long as the TKIP encryption key, copy MIC key */
2410 		if (status->gtk[status_idx].len ==
2411 		    NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2412 			memcpy(status->gtk[status_idx].key +
2413 			       NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2414 			       data[data_idx].tkip_mic_key,
2415 			       sizeof(data[data_idx].tkip_mic_key));
2416 		status_idx++;
2417 	}
2418 }
2419 
2420 static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status,
2421 				 struct iwl_wowlan_igtk_status *data)
2422 {
2423 	int i;
2424 
2425 	BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key));
2426 	BUILD_BUG_ON(sizeof(status->igtk.ipn) != sizeof(data->ipn));
2427 
2428 	if (!data->key_len)
2429 		return;
2430 
2431 	status->igtk.len = data->key_len;
2432 	status->igtk.flags = data->key_flags;
2433 	status->igtk.id = u32_get_bits(data->key_flags,
2434 				       IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2435 		+ WOWLAN_IGTK_MIN_INDEX;
2436 
2437 	memcpy(status->igtk.key, data->key, sizeof(data->key));
2438 
2439 	/* mac80211 expects big endian for memcmp() to work, convert */
2440 	for (i = 0; i < sizeof(data->ipn); i++)
2441 		status->igtk.ipn[i] = data->ipn[sizeof(data->ipn) - i - 1];
2442 }
2443 
2444 static void iwl_mvm_convert_bigtk(struct iwl_wowlan_status_data *status,
2445 				  const struct iwl_wowlan_igtk_status *data)
2446 {
2447 	int data_idx, status_idx = 0;
2448 
2449 	BUILD_BUG_ON(ARRAY_SIZE(status->bigtk) < WOWLAN_BIGTK_KEYS_NUM);
2450 
2451 	for (data_idx = 0; data_idx < WOWLAN_BIGTK_KEYS_NUM; data_idx++) {
2452 		if (!data[data_idx].key_len)
2453 			continue;
2454 
2455 		status->bigtk[status_idx].len = data[data_idx].key_len;
2456 		status->bigtk[status_idx].flags = data[data_idx].key_flags;
2457 		status->bigtk[status_idx].id =
2458 			u32_get_bits(data[data_idx].key_flags,
2459 				     IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2460 			+ WOWLAN_BIGTK_MIN_INDEX;
2461 
2462 		BUILD_BUG_ON(sizeof(status->bigtk[status_idx].key) <
2463 			     sizeof(data[data_idx].key));
2464 		BUILD_BUG_ON(sizeof(status->bigtk[status_idx].ipn) <
2465 			     sizeof(data[data_idx].ipn));
2466 
2467 		memcpy(status->bigtk[status_idx].key, data[data_idx].key,
2468 		       sizeof(data[data_idx].key));
2469 		memcpy(status->bigtk[status_idx].ipn, data[data_idx].ipn,
2470 		       sizeof(data[data_idx].ipn));
2471 		status_idx++;
2472 	}
2473 }
2474 
2475 static void iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm *mvm,
2476 					    struct iwl_wowlan_info_notif *data,
2477 					    struct iwl_wowlan_status_data *status,
2478 					    u32 len, bool has_mlo_keys)
2479 {
2480 	u32 i;
2481 	u32 expected_len = sizeof(*data);
2482 
2483 	if (!data) {
2484 		IWL_ERR(mvm, "iwl_wowlan_info_notif data is NULL\n");
2485 		status = NULL;
2486 		return;
2487 	}
2488 
2489 	if (has_mlo_keys)
2490 		expected_len += (data->num_mlo_link_keys *
2491 				 sizeof(status->mlo_keys[0]));
2492 
2493 	if (len < expected_len) {
2494 		IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2495 		status = NULL;
2496 		return;
2497 	}
2498 
2499 	if (mvm->fast_resume)
2500 		return;
2501 
2502 	iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2503 	iwl_mvm_convert_gtk_v3(status, data->gtk);
2504 	iwl_mvm_convert_igtk(status, &data->igtk[0]);
2505 	iwl_mvm_convert_bigtk(status, data->bigtk);
2506 	status->replay_ctr = le64_to_cpu(data->replay_ctr);
2507 	status->pattern_number = le16_to_cpu(data->pattern_number);
2508 	for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2509 		status->qos_seq_ctr[i] =
2510 			le16_to_cpu(data->qos_seq_ctr[i]);
2511 	status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2512 	status->num_of_gtk_rekeys =
2513 		le32_to_cpu(data->num_of_gtk_rekeys);
2514 	status->received_beacons = le32_to_cpu(data->received_beacons);
2515 	status->tid_tear_down = data->tid_tear_down;
2516 
2517 	if (has_mlo_keys && data->num_mlo_link_keys) {
2518 		status->num_mlo_keys = data->num_mlo_link_keys;
2519 		if (IWL_FW_CHECK(mvm,
2520 				 status->num_mlo_keys > WOWLAN_MAX_MLO_KEYS,
2521 				 "Too many mlo keys: %d, max %d\n",
2522 				 status->num_mlo_keys, WOWLAN_MAX_MLO_KEYS))
2523 			status->num_mlo_keys = WOWLAN_MAX_MLO_KEYS;
2524 		memcpy(status->mlo_keys, data->mlo_gtks,
2525 		       status->num_mlo_keys * sizeof(status->mlo_keys[0]));
2526 	}
2527 }
2528 
2529 static void
2530 iwl_mvm_parse_wowlan_info_notif_v2(struct iwl_mvm *mvm,
2531 				   struct iwl_wowlan_info_notif_v2 *data,
2532 				   struct iwl_wowlan_status_data *status,
2533 				   u32 len)
2534 {
2535 	u32 i;
2536 
2537 	if (!data) {
2538 		IWL_ERR(mvm, "iwl_wowlan_info_notif data is NULL\n");
2539 		status = NULL;
2540 		return;
2541 	}
2542 
2543 	if (len < sizeof(*data)) {
2544 		IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2545 		status = NULL;
2546 		return;
2547 	}
2548 
2549 	iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2550 	iwl_mvm_convert_gtk_v3(status, data->gtk);
2551 	iwl_mvm_convert_igtk(status, &data->igtk[0]);
2552 	status->replay_ctr = le64_to_cpu(data->replay_ctr);
2553 	status->pattern_number = le16_to_cpu(data->pattern_number);
2554 	for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2555 		status->qos_seq_ctr[i] =
2556 			le16_to_cpu(data->qos_seq_ctr[i]);
2557 	status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2558 	status->num_of_gtk_rekeys =
2559 		le32_to_cpu(data->num_of_gtk_rekeys);
2560 	status->received_beacons = le32_to_cpu(data->received_beacons);
2561 	status->tid_tear_down = data->tid_tear_down;
2562 }
2563 
2564 /* Occasionally, templates would be nice. This is one of those times ... */
2565 #define iwl_mvm_parse_wowlan_status_common(_ver)			\
2566 static struct iwl_wowlan_status_data *					\
2567 iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm,	\
2568 					    struct iwl_wowlan_status_ ##_ver *data,\
2569 					    int len)			\
2570 {									\
2571 	struct iwl_wowlan_status_data *status;				\
2572 	int data_size, i;						\
2573 									\
2574 	if (len < sizeof(*data)) {					\
2575 		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
2576 		return NULL;						\
2577 	}								\
2578 									\
2579 	data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4);	\
2580 	if (len != sizeof(*data) + data_size) {				\
2581 		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
2582 		return NULL;						\
2583 	}								\
2584 									\
2585 	status = kzalloc(sizeof(*status), GFP_KERNEL);			\
2586 	if (!status)							\
2587 		return NULL;						\
2588 									\
2589 	/* copy all the common fields */				\
2590 	status->replay_ctr = le64_to_cpu(data->replay_ctr);		\
2591 	status->pattern_number = le16_to_cpu(data->pattern_number);	\
2592 	status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr);	\
2593 	for (i = 0; i < 8; i++)						\
2594 		status->qos_seq_ctr[i] =				\
2595 			le16_to_cpu(data->qos_seq_ctr[i]);		\
2596 	status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);	\
2597 	status->num_of_gtk_rekeys =					\
2598 		le32_to_cpu(data->num_of_gtk_rekeys);			\
2599 	status->received_beacons = le32_to_cpu(data->received_beacons);	\
2600 	status->wake_packet_length =					\
2601 		le32_to_cpu(data->wake_packet_length);			\
2602 	status->wake_packet_bufsize =					\
2603 		le32_to_cpu(data->wake_packet_bufsize);			\
2604 	if (status->wake_packet_bufsize) {				\
2605 		status->wake_packet =					\
2606 			kmemdup(data->wake_packet,			\
2607 				status->wake_packet_bufsize,		\
2608 				GFP_KERNEL);				\
2609 		if (!status->wake_packet) {				\
2610 			kfree(status);					\
2611 			return NULL;					\
2612 		}							\
2613 	} else {							\
2614 		status->wake_packet = NULL;				\
2615 	}								\
2616 									\
2617 	return status;							\
2618 }
2619 
2620 iwl_mvm_parse_wowlan_status_common(v6)
2621 iwl_mvm_parse_wowlan_status_common(v7)
2622 iwl_mvm_parse_wowlan_status_common(v9)
2623 iwl_mvm_parse_wowlan_status_common(v12)
2624 
2625 static struct iwl_wowlan_status_data *
2626 iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id)
2627 {
2628 	struct iwl_wowlan_status_data *status;
2629 	struct iwl_wowlan_get_status_cmd get_status_cmd = {
2630 		.sta_id = cpu_to_le32(sta_id),
2631 	};
2632 	struct iwl_host_cmd cmd = {
2633 		.id = WOWLAN_GET_STATUSES,
2634 		.flags = CMD_WANT_SKB,
2635 		.data = { &get_status_cmd, },
2636 		.len = { sizeof(get_status_cmd), },
2637 	};
2638 	int ret, len;
2639 	u8 notif_ver;
2640 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
2641 					   IWL_FW_CMD_VER_UNKNOWN);
2642 
2643 	if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN)
2644 		cmd.len[0] = 0;
2645 
2646 	lockdep_assert_held(&mvm->mutex);
2647 
2648 	ret = iwl_mvm_send_cmd(mvm, &cmd);
2649 	if (ret) {
2650 		IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
2651 		return ERR_PTR(ret);
2652 	}
2653 
2654 	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2655 
2656 	/* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2657 	notif_ver = iwl_mvm_lookup_wowlan_status_ver(mvm);
2658 
2659 	if (notif_ver < 7) {
2660 		struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
2661 
2662 		status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len);
2663 		if (!status)
2664 			goto out_free_resp;
2665 
2666 		BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
2667 			     sizeof(status->gtk[0].key));
2668 		BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2669 			     sizeof(v6->gtk.tkip_mic_key) >
2670 			     sizeof(status->gtk[0].key));
2671 
2672 		/* copy GTK info to the right place */
2673 		memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
2674 		       sizeof(v6->gtk.decrypt_key));
2675 		memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2676 		       v6->gtk.tkip_mic_key,
2677 		       sizeof(v6->gtk.tkip_mic_key));
2678 
2679 		iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc);
2680 
2681 		/* hardcode the key length to 16 since v6 only supports 16 */
2682 		status->gtk[0].len = 16;
2683 
2684 		/*
2685 		 * The key index only uses 2 bits (values 0 to 3) and
2686 		 * we always set bit 7 which means this is the
2687 		 * currently used key.
2688 		 */
2689 		status->gtk[0].flags = v6->gtk.key_index | BIT(7);
2690 	} else if (notif_ver == 7) {
2691 		struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
2692 
2693 		status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len);
2694 		if (!status)
2695 			goto out_free_resp;
2696 
2697 		iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc);
2698 		iwl_mvm_convert_gtk_v2(status, &v7->gtk[0]);
2699 		iwl_mvm_convert_igtk(status, &v7->igtk[0]);
2700 	} else if (notif_ver == 9 || notif_ver == 10 || notif_ver == 11) {
2701 		struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data;
2702 
2703 		/* these three command versions have same layout and size, the
2704 		 * difference is only in a few not used (reserved) fields.
2705 		 */
2706 		status = iwl_mvm_parse_wowlan_status_common_v9(mvm, v9, len);
2707 		if (!status)
2708 			goto out_free_resp;
2709 
2710 		iwl_mvm_convert_key_counters(status, &v9->gtk[0].rsc.all_tsc_rsc);
2711 		iwl_mvm_convert_gtk_v2(status, &v9->gtk[0]);
2712 		iwl_mvm_convert_igtk(status, &v9->igtk[0]);
2713 
2714 		status->tid_tear_down = v9->tid_tear_down;
2715 	} else if (notif_ver == 12) {
2716 		struct iwl_wowlan_status_v12 *v12 = (void *)cmd.resp_pkt->data;
2717 
2718 		status = iwl_mvm_parse_wowlan_status_common_v12(mvm, v12, len);
2719 		if (!status)
2720 			goto out_free_resp;
2721 
2722 		iwl_mvm_convert_key_counters_v5(status, &v12->gtk[0].sc);
2723 		iwl_mvm_convert_gtk_v3(status, v12->gtk);
2724 		iwl_mvm_convert_igtk(status, &v12->igtk[0]);
2725 
2726 		status->tid_tear_down = v12->tid_tear_down;
2727 	} else {
2728 		IWL_ERR(mvm,
2729 			"Firmware advertises unknown WoWLAN status response %d!\n",
2730 			notif_ver);
2731 		status = NULL;
2732 	}
2733 
2734 out_free_resp:
2735 	iwl_free_resp(&cmd);
2736 	return status;
2737 }
2738 
2739 /* releases the MVM mutex */
2740 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
2741 					 struct ieee80211_vif *vif,
2742 					 struct iwl_wowlan_status_data *status)
2743 {
2744 	int i;
2745 	bool keep = false;
2746 	struct iwl_mvm_sta *mvm_ap_sta;
2747 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2748 	int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
2749 	struct iwl_mvm_vif_link_info *mvm_link = mvmvif->link[link_id];
2750 
2751 	if (WARN_ON(!mvm_link))
2752 		goto out_unlock;
2753 
2754 	if (!status)
2755 		goto out_unlock;
2756 
2757 	IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
2758 			 status->wakeup_reasons);
2759 
2760 	mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, mvm_link->ap_sta_id);
2761 	if (!mvm_ap_sta)
2762 		goto out_unlock;
2763 
2764 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
2765 		u16 seq = status->qos_seq_ctr[i];
2766 		/* firmware stores last-used value, we store next value */
2767 		seq += 0x10;
2768 		mvm_ap_sta->tid_data[i].seq_number = seq;
2769 	}
2770 
2771 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
2772 		i = mvm->offload_tid;
2773 		iwl_trans_set_q_ptrs(mvm->trans,
2774 				     mvm_ap_sta->tid_data[i].txq_id,
2775 				     mvm_ap_sta->tid_data[i].seq_number >> 4);
2776 	}
2777 
2778 	iwl_mvm_report_wakeup_reasons(mvm, vif, status);
2779 
2780 	keep = iwl_mvm_setup_connection_keep(mvm, vif, status);
2781 out_unlock:
2782 	mutex_unlock(&mvm->mutex);
2783 	return keep;
2784 }
2785 
2786 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
2787 			  IWL_SCAN_MAX_PROFILES)
2788 
2789 struct iwl_mvm_nd_results {
2790 	u32 matched_profiles;
2791 	u8 matches[ND_QUERY_BUF_LEN];
2792 };
2793 
2794 static int
2795 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
2796 				struct iwl_mvm_nd_results *results)
2797 {
2798 	struct iwl_scan_offload_match_info *query;
2799 	struct iwl_host_cmd cmd = {
2800 		.id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
2801 		.flags = CMD_WANT_SKB,
2802 	};
2803 	int ret, len;
2804 	size_t query_len, matches_len;
2805 	int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
2806 
2807 	ret = iwl_mvm_send_cmd(mvm, &cmd);
2808 	if (ret) {
2809 		IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
2810 		return ret;
2811 	}
2812 
2813 	if (fw_has_api(&mvm->fw->ucode_capa,
2814 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2815 		query_len = sizeof(struct iwl_scan_offload_match_info);
2816 		matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2817 			max_profiles;
2818 	} else {
2819 		query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
2820 		matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
2821 			max_profiles;
2822 	}
2823 
2824 	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2825 	if (len < query_len) {
2826 		IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
2827 		ret = -EIO;
2828 		goto out_free_resp;
2829 	}
2830 
2831 	query = (void *)cmd.resp_pkt->data;
2832 
2833 	results->matched_profiles = le32_to_cpu(query->matched_profiles);
2834 	memcpy(results->matches, query->matches, matches_len);
2835 
2836 #ifdef CONFIG_IWLWIFI_DEBUGFS
2837 	mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
2838 #endif
2839 
2840 out_free_resp:
2841 	iwl_free_resp(&cmd);
2842 	return ret;
2843 }
2844 
2845 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
2846 					 struct iwl_mvm_nd_results *results,
2847 					 int idx)
2848 {
2849 	int n_chans = 0, i;
2850 
2851 	if (fw_has_api(&mvm->fw->ucode_capa,
2852 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2853 		struct iwl_scan_offload_profile_match *matches =
2854 			(void *)results->matches;
2855 
2856 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
2857 			n_chans += hweight8(matches[idx].matching_channels[i]);
2858 	} else {
2859 		struct iwl_scan_offload_profile_match_v1 *matches =
2860 			(void *)results->matches;
2861 
2862 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
2863 			n_chans += hweight8(matches[idx].matching_channels[i]);
2864 	}
2865 
2866 	return n_chans;
2867 }
2868 
2869 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
2870 				    struct iwl_mvm_nd_results *results,
2871 				    struct cfg80211_wowlan_nd_match *match,
2872 				    int idx)
2873 {
2874 	int i;
2875 
2876 	if (fw_has_api(&mvm->fw->ucode_capa,
2877 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2878 		struct iwl_scan_offload_profile_match *matches =
2879 			 (void *)results->matches;
2880 
2881 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
2882 			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2883 				match->channels[match->n_channels++] =
2884 					mvm->nd_channels[i]->center_freq;
2885 	} else {
2886 		struct iwl_scan_offload_profile_match_v1 *matches =
2887 			 (void *)results->matches;
2888 
2889 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
2890 			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2891 				match->channels[match->n_channels++] =
2892 					mvm->nd_channels[i]->center_freq;
2893 	}
2894 }
2895 
2896 /**
2897  * enum iwl_d3_notif - d3 notifications
2898  * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF was received
2899  * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF was received
2900  * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF was received
2901  * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF was received
2902  * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF was received
2903  */
2904 enum iwl_d3_notif {
2905 	IWL_D3_NOTIF_WOWLAN_INFO =	BIT(0),
2906 	IWL_D3_NOTIF_WOWLAN_WAKE_PKT =	BIT(1),
2907 	IWL_D3_NOTIF_PROT_OFFLOAD =	BIT(2),
2908 	IWL_D3_ND_MATCH_INFO      =     BIT(3),
2909 	IWL_D3_NOTIF_D3_END_NOTIF =	BIT(4)
2910 };
2911 
2912 /* manage d3 resume data */
2913 struct iwl_d3_data {
2914 	struct iwl_wowlan_status_data *status;
2915 	bool test;
2916 	u32 d3_end_flags;
2917 	u32 notif_expected;	/* bitmap - see &enum iwl_d3_notif */
2918 	u32 notif_received;	/* bitmap - see &enum iwl_d3_notif */
2919 	struct iwl_mvm_nd_results *nd_results;
2920 	bool nd_results_valid;
2921 };
2922 
2923 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
2924 					    struct ieee80211_vif *vif,
2925 					    struct iwl_d3_data *d3_data)
2926 {
2927 	struct cfg80211_wowlan_nd_info *net_detect = NULL;
2928 	struct cfg80211_wowlan_wakeup wakeup = {
2929 		.pattern_idx = -1,
2930 	};
2931 	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
2932 	unsigned long matched_profiles;
2933 	u32 reasons = 0;
2934 	int i, n_matches, ret;
2935 
2936 	if (WARN_ON(!d3_data || !d3_data->status))
2937 		goto out;
2938 
2939 	reasons = d3_data->status->wakeup_reasons;
2940 
2941 	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
2942 		wakeup.rfkill_release = true;
2943 
2944 	if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
2945 		goto out;
2946 
2947 	if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2948 				     WOWLAN_INFO_NOTIFICATION, 0)) {
2949 		IWL_INFO(mvm, "Query FW for ND results\n");
2950 		ret = iwl_mvm_netdetect_query_results(mvm, d3_data->nd_results);
2951 
2952 	} else {
2953 		IWL_INFO(mvm, "Notification based ND results\n");
2954 		ret = d3_data->nd_results_valid ? 0 : -1;
2955 	}
2956 
2957 	if (ret || !d3_data->nd_results->matched_profiles) {
2958 		wakeup_report = NULL;
2959 		goto out;
2960 	}
2961 
2962 	matched_profiles = d3_data->nd_results->matched_profiles;
2963 	if (mvm->n_nd_match_sets) {
2964 		n_matches = hweight_long(matched_profiles);
2965 	} else {
2966 		IWL_ERR(mvm, "no net detect match information available\n");
2967 		n_matches = 0;
2968 	}
2969 
2970 	net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
2971 			     GFP_KERNEL);
2972 	if (!net_detect || !n_matches)
2973 		goto out_report_nd;
2974 
2975 	for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
2976 		struct cfg80211_wowlan_nd_match *match;
2977 		int idx, n_channels = 0;
2978 
2979 		n_channels = iwl_mvm_query_num_match_chans(mvm,
2980 							   d3_data->nd_results,
2981 							   i);
2982 
2983 		match = kzalloc(struct_size(match, channels, n_channels),
2984 				GFP_KERNEL);
2985 		if (!match)
2986 			goto out_report_nd;
2987 
2988 		net_detect->matches[net_detect->n_matches++] = match;
2989 
2990 		/* We inverted the order of the SSIDs in the scan
2991 		 * request, so invert the index here.
2992 		 */
2993 		idx = mvm->n_nd_match_sets - i - 1;
2994 		match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2995 		memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2996 		       match->ssid.ssid_len);
2997 
2998 		if (mvm->n_nd_channels < n_channels)
2999 			continue;
3000 
3001 		iwl_mvm_query_set_freqs(mvm, d3_data->nd_results, match, i);
3002 	}
3003 
3004 out_report_nd:
3005 	wakeup.net_detect = net_detect;
3006 out:
3007 	iwl_mvm_free_nd(mvm);
3008 
3009 	mutex_unlock(&mvm->mutex);
3010 	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
3011 
3012 	if (net_detect) {
3013 		for (i = 0; i < net_detect->n_matches; i++)
3014 			kfree(net_detect->matches[i]);
3015 		kfree(net_detect);
3016 	}
3017 }
3018 
3019 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
3020 				       struct ieee80211_vif *vif)
3021 {
3022 	/* skip the one we keep connection on */
3023 	if (data == vif)
3024 		return;
3025 
3026 	if (vif->type == NL80211_IFTYPE_STATION)
3027 		ieee80211_resume_disconnect(vif);
3028 }
3029 
3030 static bool iwl_mvm_rt_status(struct iwl_trans *trans, u32 base, u32 *err_id)
3031 {
3032 	struct error_table_start {
3033 		/* cf. struct iwl_error_event_table */
3034 		u32 valid;
3035 		__le32 err_id;
3036 	} err_info;
3037 
3038 	if (!base)
3039 		return false;
3040 
3041 	iwl_trans_read_mem_bytes(trans, base,
3042 				 &err_info, sizeof(err_info));
3043 	if (err_info.valid && err_id)
3044 		*err_id = le32_to_cpu(err_info.err_id);
3045 
3046 	return !!err_info.valid;
3047 }
3048 
3049 static bool iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
3050 				   struct ieee80211_vif *vif)
3051 {
3052 	u32 err_id;
3053 
3054 	/* check for lmac1 error */
3055 	if (iwl_mvm_rt_status(mvm->trans,
3056 			      mvm->trans->dbg.lmac_error_event_table[0],
3057 			      &err_id)) {
3058 		if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN && vif) {
3059 			struct cfg80211_wowlan_wakeup wakeup = {
3060 				.rfkill_release = true,
3061 			};
3062 			ieee80211_report_wowlan_wakeup(vif, &wakeup,
3063 						       GFP_KERNEL);
3064 		}
3065 		return true;
3066 	}
3067 
3068 	/* check if we have lmac2 set and check for error */
3069 	if (iwl_mvm_rt_status(mvm->trans,
3070 			      mvm->trans->dbg.lmac_error_event_table[1], NULL))
3071 		return true;
3072 
3073 	/* check for umac error */
3074 	if (iwl_mvm_rt_status(mvm->trans,
3075 			      mvm->trans->dbg.umac_error_event_table, NULL))
3076 		return true;
3077 
3078 	return false;
3079 }
3080 
3081 /*
3082  * This function assumes:
3083  *	1. The mutex is already held.
3084  *	2. The callee functions unlock the mutex.
3085  */
3086 static bool
3087 iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm *mvm,
3088 				    struct ieee80211_vif *vif,
3089 				    struct iwl_d3_data *d3_data)
3090 {
3091 	lockdep_assert_held(&mvm->mutex);
3092 
3093 	/* if FW uses status notification, status shouldn't be NULL here */
3094 	if (!d3_data->status) {
3095 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3096 		u8 sta_id = mvm->net_detect ? IWL_MVM_INVALID_STA :
3097 					      mvmvif->deflink.ap_sta_id;
3098 
3099 		/* bug - FW with MLO has status notification */
3100 		WARN_ON(ieee80211_vif_is_mld(vif));
3101 
3102 		d3_data->status = iwl_mvm_send_wowlan_get_status(mvm, sta_id);
3103 	}
3104 
3105 	if (mvm->net_detect) {
3106 		iwl_mvm_query_netdetect_reasons(mvm, vif, d3_data);
3107 	} else {
3108 		bool keep = iwl_mvm_query_wakeup_reasons(mvm, vif,
3109 							 d3_data->status);
3110 
3111 #ifdef CONFIG_IWLWIFI_DEBUGFS
3112 		if (keep)
3113 			mvm->keep_vif = vif;
3114 #endif
3115 
3116 		return keep;
3117 	}
3118 	return false;
3119 }
3120 
3121 #define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT (IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \
3122 						 IWL_WOWLAN_WAKEUP_BY_PATTERN | \
3123 						 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\
3124 						 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\
3125 						 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\
3126 						 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD)
3127 
3128 static int iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm *mvm,
3129 					 struct iwl_wowlan_wake_pkt_notif *notif,
3130 					 struct iwl_wowlan_status_data *status,
3131 					 u32 len)
3132 {
3133 	u32 data_size, packet_len = le32_to_cpu(notif->wake_packet_length);
3134 
3135 	if (len < sizeof(*notif)) {
3136 		IWL_ERR(mvm, "Invalid WoWLAN wake packet notification!\n");
3137 		return -EIO;
3138 	}
3139 
3140 	if (WARN_ON(!status)) {
3141 		IWL_ERR(mvm, "Got wake packet notification but wowlan status data is NULL\n");
3142 		return -EIO;
3143 	}
3144 
3145 	if (WARN_ON(!(status->wakeup_reasons &
3146 		      IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT))) {
3147 		IWL_ERR(mvm, "Got wakeup packet but wakeup reason is %x\n",
3148 			status->wakeup_reasons);
3149 		return -EIO;
3150 	}
3151 
3152 	data_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif, wake_packet);
3153 
3154 	/* data_size got the padding from the notification, remove it. */
3155 	if (packet_len < data_size)
3156 		data_size = packet_len;
3157 
3158 	status->wake_packet = kmemdup(notif->wake_packet, data_size,
3159 				      GFP_ATOMIC);
3160 
3161 	if (!status->wake_packet)
3162 		return -ENOMEM;
3163 
3164 	status->wake_packet_length = packet_len;
3165 	status->wake_packet_bufsize = data_size;
3166 
3167 	return 0;
3168 }
3169 
3170 static void iwl_mvm_nd_match_info_handler(struct iwl_mvm *mvm,
3171 					  struct iwl_d3_data *d3_data,
3172 					  struct iwl_scan_offload_match_info *notif,
3173 					  u32 len)
3174 {
3175 	struct iwl_wowlan_status_data *status = d3_data->status;
3176 	struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
3177 	struct iwl_mvm_nd_results *results = d3_data->nd_results;
3178 	size_t i, matches_len = sizeof(struct iwl_scan_offload_profile_match) *
3179 		iwl_umac_scan_get_max_profiles(mvm->fw);
3180 
3181 	if (IS_ERR_OR_NULL(vif))
3182 		return;
3183 
3184 	if (len < sizeof(struct iwl_scan_offload_match_info)) {
3185 		IWL_ERR(mvm, "Invalid scan match info notification\n");
3186 		return;
3187 	}
3188 
3189 	if (!mvm->net_detect) {
3190 		IWL_ERR(mvm, "Unexpected scan match info notification\n");
3191 		return;
3192 	}
3193 
3194 	if (!status || status->wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
3195 		IWL_ERR(mvm,
3196 			"Ignore scan match info notification: no reason\n");
3197 		return;
3198 	}
3199 
3200 #ifdef CONFIG_IWLWIFI_DEBUGFS
3201 	mvm->last_netdetect_scans = le32_to_cpu(notif->n_scans_done);
3202 #endif
3203 
3204 	results->matched_profiles = le32_to_cpu(notif->matched_profiles);
3205 	IWL_INFO(mvm, "number of matched profiles=%u\n",
3206 		 results->matched_profiles);
3207 
3208 	if (results->matched_profiles) {
3209 		memcpy(results->matches, notif->matches, matches_len);
3210 		d3_data->nd_results_valid = true;
3211 	}
3212 
3213 	/* no scan should be active at this point */
3214 	mvm->scan_status = 0;
3215 	for (i = 0; i < mvm->max_scans; i++)
3216 		mvm->scan_uid_status[i] = 0;
3217 }
3218 
3219 static bool iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data *notif_wait,
3220 				  struct iwl_rx_packet *pkt, void *data)
3221 {
3222 	struct iwl_mvm *mvm =
3223 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3224 	struct iwl_d3_data *d3_data = data;
3225 	u32 len = iwl_rx_packet_payload_len(pkt);
3226 	int ret;
3227 	int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
3228 						      PROT_OFFLOAD_GROUP,
3229 						      WOWLAN_INFO_NOTIFICATION,
3230 						      IWL_FW_CMD_VER_UNKNOWN);
3231 
3232 
3233 	switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
3234 	case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): {
3235 
3236 		if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_INFO) {
3237 			/* We might get two notifications due to dual bss */
3238 			IWL_DEBUG_WOWLAN(mvm,
3239 					 "Got additional wowlan info notification\n");
3240 			break;
3241 		}
3242 
3243 		if (wowlan_info_ver < 2) {
3244 			struct iwl_wowlan_info_notif_v1 *notif_v1 =
3245 				(void *)pkt->data;
3246 			struct iwl_wowlan_info_notif_v2 *notif_v2;
3247 
3248 			notif_v2 = kmemdup(notif_v1, sizeof(*notif_v2), GFP_ATOMIC);
3249 
3250 			if (!notif_v2)
3251 				return false;
3252 
3253 			notif_v2->tid_tear_down = notif_v1->tid_tear_down;
3254 			notif_v2->station_id = notif_v1->station_id;
3255 			memset_after(notif_v2, 0, station_id);
3256 			iwl_mvm_parse_wowlan_info_notif_v2(mvm, notif_v2,
3257 							   d3_data->status,
3258 							   len);
3259 			kfree(notif_v2);
3260 
3261 		} else if (wowlan_info_ver == 2) {
3262 			struct iwl_wowlan_info_notif_v2 *notif_v2 =
3263 				(void *)pkt->data;
3264 
3265 			iwl_mvm_parse_wowlan_info_notif_v2(mvm, notif_v2,
3266 							   d3_data->status,
3267 							   len);
3268 		} else {
3269 			struct iwl_wowlan_info_notif *notif =
3270 				(void *)pkt->data;
3271 
3272 			iwl_mvm_parse_wowlan_info_notif(mvm, notif,
3273 							d3_data->status, len,
3274 							wowlan_info_ver > 3);
3275 		}
3276 
3277 		d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_INFO;
3278 
3279 		if (d3_data->status &&
3280 		    d3_data->status->wakeup_reasons & IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT)
3281 			/* We are supposed to get also wake packet notif */
3282 			d3_data->notif_expected |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3283 
3284 		break;
3285 	}
3286 	case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): {
3287 		struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data;
3288 
3289 		if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_WAKE_PKT) {
3290 			/* We shouldn't get two wake packet notifications */
3291 			IWL_ERR(mvm,
3292 				"Got additional wowlan wake packet notification\n");
3293 		} else {
3294 			d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3295 			len =  iwl_rx_packet_payload_len(pkt);
3296 			ret = iwl_mvm_wowlan_store_wake_pkt(mvm, notif,
3297 							    d3_data->status,
3298 							    len);
3299 			if (ret)
3300 				IWL_ERR(mvm,
3301 					"Can't parse WOWLAN_WAKE_PKT_NOTIFICATION\n");
3302 		}
3303 
3304 		break;
3305 	}
3306 	case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): {
3307 		struct iwl_scan_offload_match_info *notif = (void *)pkt->data;
3308 
3309 		if (d3_data->notif_received & IWL_D3_ND_MATCH_INFO) {
3310 			IWL_ERR(mvm,
3311 				"Got additional netdetect match info\n");
3312 			break;
3313 		}
3314 
3315 		d3_data->notif_received |= IWL_D3_ND_MATCH_INFO;
3316 
3317 		/* explicitly set this in the 'expected' as well */
3318 		d3_data->notif_expected |= IWL_D3_ND_MATCH_INFO;
3319 
3320 		len = iwl_rx_packet_payload_len(pkt);
3321 		iwl_mvm_nd_match_info_handler(mvm, d3_data, notif, len);
3322 		break;
3323 	}
3324 	case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): {
3325 		struct iwl_mvm_d3_end_notif *notif = (void *)pkt->data;
3326 
3327 		d3_data->d3_end_flags = __le32_to_cpu(notif->flags);
3328 		d3_data->notif_received |= IWL_D3_NOTIF_D3_END_NOTIF;
3329 
3330 		break;
3331 	}
3332 	default:
3333 		WARN_ON(1);
3334 	}
3335 
3336 	return d3_data->notif_received == d3_data->notif_expected;
3337 }
3338 
3339 static int iwl_mvm_resume_firmware(struct iwl_mvm *mvm, bool test)
3340 {
3341 	int ret;
3342 	enum iwl_d3_status d3_status;
3343 	struct iwl_host_cmd cmd = {
3344 			.id = D0I3_END_CMD,
3345 			.flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
3346 		};
3347 	bool reset = fw_has_capa(&mvm->fw->ucode_capa,
3348 				 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3349 
3350 	ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !reset);
3351 	if (ret)
3352 		return ret;
3353 
3354 	if (d3_status != IWL_D3_STATUS_ALIVE) {
3355 		IWL_INFO(mvm, "Device was reset during suspend\n");
3356 		return -ENOENT;
3357 	}
3358 
3359 	/*
3360 	 * We should trigger resume flow using command only for 22000 family
3361 	 * AX210 and above don't need the command since they have
3362 	 * the doorbell interrupt.
3363 	 */
3364 	if (mvm->trans->trans_cfg->device_family <= IWL_DEVICE_FAMILY_22000 &&
3365 	    fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_D0I3_END_FIRST)) {
3366 		ret = iwl_mvm_send_cmd(mvm, &cmd);
3367 		if (ret < 0)
3368 			IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
3369 				ret);
3370 	}
3371 
3372 	return ret;
3373 }
3374 
3375 #define IWL_MVM_D3_NOTIF_TIMEOUT (HZ / 3)
3376 
3377 static int iwl_mvm_d3_notif_wait(struct iwl_mvm *mvm,
3378 				 struct iwl_d3_data *d3_data)
3379 {
3380 	static const u16 d3_resume_notif[] = {
3381 		WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION),
3382 		WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION),
3383 		WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF),
3384 		WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3385 	};
3386 	static const u16 d3_fast_resume_notif[] = {
3387 		WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3388 	};
3389 	struct iwl_notification_wait wait_d3_notif;
3390 	int ret;
3391 
3392 	if (mvm->fast_resume)
3393 		iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3394 					   d3_fast_resume_notif,
3395 					   ARRAY_SIZE(d3_fast_resume_notif),
3396 					   iwl_mvm_wait_d3_notif, d3_data);
3397 	else
3398 		iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3399 					   d3_resume_notif,
3400 					   ARRAY_SIZE(d3_resume_notif),
3401 					   iwl_mvm_wait_d3_notif, d3_data);
3402 
3403 	ret = iwl_mvm_resume_firmware(mvm, d3_data->test);
3404 	if (ret) {
3405 		iwl_remove_notification(&mvm->notif_wait, &wait_d3_notif);
3406 		return ret;
3407 	}
3408 
3409 	return iwl_wait_notification(&mvm->notif_wait, &wait_d3_notif,
3410 				     IWL_MVM_D3_NOTIF_TIMEOUT);
3411 }
3412 
3413 static inline bool iwl_mvm_d3_resume_notif_based(struct iwl_mvm *mvm)
3414 {
3415 	return iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3416 				       WOWLAN_INFO_NOTIFICATION, 0) &&
3417 		iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3418 					WOWLAN_WAKE_PKT_NOTIFICATION, 0) &&
3419 		iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3420 					D3_END_NOTIFICATION, 0);
3421 }
3422 
3423 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
3424 {
3425 	struct ieee80211_vif *vif = NULL;
3426 	int ret = 1;
3427 	struct iwl_mvm_nd_results results = {};
3428 	struct iwl_d3_data d3_data = {
3429 		.test = test,
3430 		.notif_expected =
3431 			IWL_D3_NOTIF_WOWLAN_INFO |
3432 			IWL_D3_NOTIF_D3_END_NOTIF,
3433 		.nd_results_valid = false,
3434 		.nd_results = &results,
3435 	};
3436 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3437 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3438 	bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
3439 				      IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
3440 	bool resume_notif_based = iwl_mvm_d3_resume_notif_based(mvm);
3441 	bool keep = false;
3442 
3443 	mutex_lock(&mvm->mutex);
3444 
3445 	/* Apparently, the device went away and device_powered_off() was called,
3446 	 * don't even try to read the rt_status, the device is currently
3447 	 * inaccessible.
3448 	 */
3449 	if (!test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status)) {
3450 		IWL_INFO(mvm,
3451 			 "Can't resume, device_powered_off() was called during wowlan\n");
3452 		goto err;
3453 	}
3454 
3455 	mvm->last_reset_or_resume_time_jiffies = jiffies;
3456 
3457 	/* get the BSS vif pointer again */
3458 	vif = iwl_mvm_get_bss_vif(mvm);
3459 	if (IS_ERR_OR_NULL(vif))
3460 		goto err;
3461 
3462 	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3463 
3464 	if (iwl_mvm_check_rt_status(mvm, vif)) {
3465 		set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3466 		iwl_mvm_dump_nic_error_log(mvm);
3467 		iwl_dbg_tlv_time_point(&mvm->fwrt,
3468 				       IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL);
3469 		iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
3470 					false, 0);
3471 		ret = 1;
3472 		goto err;
3473 	}
3474 
3475 	if (resume_notif_based) {
3476 		d3_data.status = kzalloc(sizeof(*d3_data.status), GFP_KERNEL);
3477 		if (!d3_data.status) {
3478 			IWL_ERR(mvm, "Failed to allocate wowlan status\n");
3479 			ret = -ENOMEM;
3480 			goto err;
3481 		}
3482 
3483 		ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3484 		if (ret)
3485 			goto err;
3486 	} else {
3487 		ret = iwl_mvm_resume_firmware(mvm, test);
3488 		if (ret < 0)
3489 			goto err;
3490 	}
3491 
3492 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_WOWLAN);
3493 
3494 	/* after the successful handshake, we're out of D3 */
3495 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3496 
3497 	/* when reset is required we can't send these following commands */
3498 	if (d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)
3499 		goto query_wakeup_reasons;
3500 
3501 	/*
3502 	 * Query the current location and source from the D3 firmware so we
3503 	 * can play it back when we re-intiailize the D0 firmware
3504 	 */
3505 	iwl_mvm_update_changed_regdom(mvm);
3506 
3507 	/* Re-configure PPAG settings */
3508 	iwl_mvm_ppag_send_cmd(mvm);
3509 
3510 	if (!unified_image)
3511 		/*  Re-configure default SAR profile */
3512 		iwl_mvm_sar_select_profile(mvm, 1, 1);
3513 
3514 	if (mvm->net_detect && unified_image) {
3515 		/* If this is a non-unified image, we restart the FW,
3516 		 * so no need to stop the netdetect scan.  If that
3517 		 * fails, continue and try to get the wake-up reasons,
3518 		 * but trigger a HW restart by keeping a failure code
3519 		 * in ret.
3520 		 */
3521 		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
3522 					false);
3523 	}
3524 
3525 query_wakeup_reasons:
3526 	keep = iwl_mvm_choose_query_wakeup_reasons(mvm, vif, &d3_data);
3527 	/* has unlocked the mutex, so skip that */
3528 	goto out;
3529 
3530 err:
3531 	mutex_unlock(&mvm->mutex);
3532 out:
3533 	if (d3_data.status)
3534 		kfree(d3_data.status->wake_packet);
3535 	kfree(d3_data.status);
3536 	iwl_mvm_free_nd(mvm);
3537 
3538 	if (!d3_data.test && !mvm->net_detect)
3539 		ieee80211_iterate_active_interfaces_mtx(mvm->hw,
3540 							IEEE80211_IFACE_ITER_NORMAL,
3541 							iwl_mvm_d3_disconnect_iter,
3542 							keep ? vif : NULL);
3543 
3544 	clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3545 
3546 	/* no need to reset the device in unified images, if successful */
3547 	if (unified_image && !ret) {
3548 		/* nothing else to do if we already sent D0I3_END_CMD */
3549 		if (d0i3_first)
3550 			return 0;
3551 
3552 		if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3553 					     D3_END_NOTIFICATION, 0)) {
3554 			ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
3555 			if (!ret)
3556 				return 0;
3557 		} else if (!(d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)) {
3558 			return 0;
3559 		}
3560 	}
3561 
3562 	/*
3563 	 * Reconfigure the device in one of the following cases:
3564 	 * 1. We are not using a unified image
3565 	 * 2. We are using a unified image but had an error while exiting D3
3566 	 */
3567 	set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
3568 
3569 	/* regardless of what happened, we're now out of D3 */
3570 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3571 
3572 	return 1;
3573 }
3574 
3575 int iwl_mvm_resume(struct ieee80211_hw *hw)
3576 {
3577 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3578 	int ret;
3579 
3580 	ret = __iwl_mvm_resume(mvm, false);
3581 
3582 	iwl_mvm_resume_tcm(mvm);
3583 
3584 	iwl_fw_runtime_resume(&mvm->fwrt);
3585 
3586 	return ret;
3587 }
3588 
3589 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
3590 {
3591 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3592 
3593 	device_set_wakeup_enable(mvm->trans->dev, enabled);
3594 }
3595 
3596 void iwl_mvm_fast_suspend(struct iwl_mvm *mvm)
3597 {
3598 	struct iwl_d3_manager_config d3_cfg_cmd_data = {};
3599 	int ret;
3600 
3601 	lockdep_assert_held(&mvm->mutex);
3602 
3603 	IWL_DEBUG_WOWLAN(mvm, "Starting fast suspend flow\n");
3604 
3605 	mvm->fast_resume = true;
3606 	set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3607 
3608 	WARN_ON(iwl_mvm_power_update_device(mvm));
3609 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
3610 	ret = iwl_mvm_send_cmd_pdu(mvm, D3_CONFIG_CMD, CMD_SEND_IN_D3,
3611 				   sizeof(d3_cfg_cmd_data), &d3_cfg_cmd_data);
3612 	if (ret)
3613 		IWL_ERR(mvm,
3614 			"fast suspend: couldn't send D3_CONFIG_CMD %d\n", ret);
3615 
3616 	WARN_ON(iwl_mvm_power_update_mac(mvm));
3617 
3618 	ret = iwl_trans_d3_suspend(mvm->trans, false, false);
3619 	if (ret)
3620 		IWL_ERR(mvm, "fast suspend: trans_d3_suspend failed %d\n", ret);
3621 }
3622 
3623 int iwl_mvm_fast_resume(struct iwl_mvm *mvm)
3624 {
3625 	struct iwl_d3_data d3_data = {
3626 		.notif_expected =
3627 			IWL_D3_NOTIF_D3_END_NOTIF,
3628 	};
3629 	int ret;
3630 
3631 	lockdep_assert_held(&mvm->mutex);
3632 
3633 	IWL_DEBUG_WOWLAN(mvm, "Starting the fast resume flow\n");
3634 
3635 	mvm->last_reset_or_resume_time_jiffies = jiffies;
3636 	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3637 
3638 	if (iwl_mvm_check_rt_status(mvm, NULL)) {
3639 		set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3640 		iwl_mvm_dump_nic_error_log(mvm);
3641 		iwl_dbg_tlv_time_point(&mvm->fwrt,
3642 				       IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL);
3643 		iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
3644 					false, 0);
3645 		return -ENODEV;
3646 	}
3647 	ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3648 	clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3649 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3650 	mvm->fast_resume = false;
3651 
3652 	if (ret)
3653 		IWL_ERR(mvm, "Couldn't get the d3 notif %d\n", ret);
3654 
3655 	return ret;
3656 }
3657 
3658 #ifdef CONFIG_IWLWIFI_DEBUGFS
3659 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
3660 {
3661 	struct iwl_mvm *mvm = inode->i_private;
3662 	int err;
3663 
3664 	if (mvm->d3_test_active)
3665 		return -EBUSY;
3666 
3667 	file->private_data = inode->i_private;
3668 
3669 	iwl_mvm_pause_tcm(mvm, true);
3670 
3671 	iwl_fw_runtime_suspend(&mvm->fwrt);
3672 
3673 	/* start pseudo D3 */
3674 	rtnl_lock();
3675 	wiphy_lock(mvm->hw->wiphy);
3676 	err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
3677 	wiphy_unlock(mvm->hw->wiphy);
3678 	rtnl_unlock();
3679 	if (err > 0)
3680 		err = -EINVAL;
3681 	if (err)
3682 		return err;
3683 
3684 	mvm->d3_test_active = true;
3685 	mvm->keep_vif = NULL;
3686 	return 0;
3687 }
3688 
3689 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
3690 				    size_t count, loff_t *ppos)
3691 {
3692 	struct iwl_mvm *mvm = file->private_data;
3693 	unsigned long end = jiffies + 60 * HZ;
3694 	u32 pme_asserted;
3695 
3696 	while (true) {
3697 		/* read pme_ptr if available */
3698 		if (mvm->d3_test_pme_ptr) {
3699 			pme_asserted = iwl_trans_read_mem32(mvm->trans,
3700 						mvm->d3_test_pme_ptr);
3701 			if (pme_asserted)
3702 				break;
3703 		}
3704 
3705 		if (msleep_interruptible(100))
3706 			break;
3707 
3708 		if (time_is_before_jiffies(end)) {
3709 			IWL_ERR(mvm,
3710 				"ending pseudo-D3 with timeout after ~60 seconds\n");
3711 			return -ETIMEDOUT;
3712 		}
3713 	}
3714 
3715 	return 0;
3716 }
3717 
3718 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
3719 					      struct ieee80211_vif *vif)
3720 {
3721 	/* skip the one we keep connection on */
3722 	if (_data == vif)
3723 		return;
3724 
3725 	if (vif->type == NL80211_IFTYPE_STATION)
3726 		ieee80211_connection_loss(vif);
3727 }
3728 
3729 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
3730 {
3731 	struct iwl_mvm *mvm = inode->i_private;
3732 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3733 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3734 
3735 	mvm->d3_test_active = false;
3736 
3737 	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3738 
3739 	rtnl_lock();
3740 	wiphy_lock(mvm->hw->wiphy);
3741 	__iwl_mvm_resume(mvm, true);
3742 	wiphy_unlock(mvm->hw->wiphy);
3743 	rtnl_unlock();
3744 
3745 	iwl_mvm_resume_tcm(mvm);
3746 
3747 	iwl_fw_runtime_resume(&mvm->fwrt);
3748 
3749 	iwl_abort_notification_waits(&mvm->notif_wait);
3750 	if (!unified_image) {
3751 		int remaining_time = 10;
3752 
3753 		ieee80211_restart_hw(mvm->hw);
3754 
3755 		/* wait for restart and disconnect all interfaces */
3756 		while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3757 		       remaining_time > 0) {
3758 			remaining_time--;
3759 			msleep(1000);
3760 		}
3761 
3762 		if (remaining_time == 0)
3763 			IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
3764 	}
3765 
3766 	ieee80211_iterate_active_interfaces_atomic(
3767 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
3768 		iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
3769 
3770 	return 0;
3771 }
3772 
3773 const struct file_operations iwl_dbgfs_d3_test_ops = {
3774 	.llseek = no_llseek,
3775 	.open = iwl_mvm_d3_test_open,
3776 	.read = iwl_mvm_d3_test_read,
3777 	.release = iwl_mvm_d3_test_release,
3778 };
3779 #endif
3780