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