1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2023 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 {
455 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
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(mvmvif->deflink.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(mvmvif->deflink.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 ieee80211_vif *vif,
679 				 struct cfg80211_wowlan *wowlan)
680 {
681 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
682 	struct iwl_wowlan_patterns_cmd *pattern_cmd;
683 	struct iwl_host_cmd cmd = {
684 		.id = WOWLAN_PATTERNS,
685 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
686 	};
687 	int i, err;
688 	int ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
689 					IWL_FW_CMD_VER_UNKNOWN);
690 
691 	if (!wowlan->n_patterns)
692 		return 0;
693 
694 	cmd.len[0] = sizeof(*pattern_cmd) +
695 		wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v2);
696 
697 	pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL);
698 	if (!pattern_cmd)
699 		return -ENOMEM;
700 
701 	pattern_cmd->n_patterns = wowlan->n_patterns;
702 	if (ver >= 3)
703 		pattern_cmd->sta_id = mvmvif->deflink.ap_sta_id;
704 
705 	for (i = 0; i < wowlan->n_patterns; i++) {
706 		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
707 
708 		pattern_cmd->patterns[i].pattern_type =
709 			WOWLAN_PATTERN_TYPE_BITMASK;
710 
711 		memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
712 		       wowlan->patterns[i].mask, mask_len);
713 		memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
714 		       wowlan->patterns[i].pattern,
715 		       wowlan->patterns[i].pattern_len);
716 		pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
717 		pattern_cmd->patterns[i].u.bitmask.pattern_size =
718 			wowlan->patterns[i].pattern_len;
719 	}
720 
721 	cmd.data[0] = pattern_cmd;
722 	err = iwl_mvm_send_cmd(mvm, &cmd);
723 	kfree(pattern_cmd);
724 	return err;
725 }
726 
727 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
728 				struct ieee80211_sta *ap_sta)
729 {
730 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
731 	struct ieee80211_chanctx_conf *ctx;
732 	u8 chains_static, chains_dynamic;
733 	struct cfg80211_chan_def chandef;
734 	int ret, i;
735 	struct iwl_binding_cmd_v1 binding_cmd = {};
736 	struct iwl_time_quota_cmd quota_cmd = {};
737 	struct iwl_time_quota_data *quota;
738 	u32 status;
739 
740 	if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm)))
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 	chains_static = ctx->rx_chains_static;
755 	chains_dynamic = ctx->rx_chains_dynamic;
756 	rcu_read_unlock();
757 
758 	ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt, &chandef,
759 				   chains_static, chains_dynamic);
760 	if (ret)
761 		return ret;
762 
763 	/* add back the MAC */
764 	mvmvif->uploaded = false;
765 
766 	if (WARN_ON(!vif->cfg.assoc))
767 		return -EINVAL;
768 
769 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
770 	if (ret)
771 		return ret;
772 
773 	/* add back binding - XXX refactor? */
774 	binding_cmd.id_and_color =
775 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
776 						mvmvif->deflink.phy_ctxt->color));
777 	binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
778 	binding_cmd.phy =
779 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
780 						mvmvif->deflink.phy_ctxt->color));
781 	binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
782 							      mvmvif->color));
783 	for (i = 1; i < MAX_MACS_IN_BINDING; i++)
784 		binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
785 
786 	status = 0;
787 	ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
788 					  IWL_BINDING_CMD_SIZE_V1, &binding_cmd,
789 					  &status);
790 	if (ret) {
791 		IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
792 		return ret;
793 	}
794 
795 	if (status) {
796 		IWL_ERR(mvm, "Binding command failed: %u\n", status);
797 		return -EIO;
798 	}
799 
800 	ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
801 	if (ret)
802 		return ret;
803 	rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->deflink.ap_sta_id],
804 			   ap_sta);
805 
806 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
807 	if (ret)
808 		return ret;
809 
810 	/* and some quota */
811 	quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, 0);
812 	quota->id_and_color =
813 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
814 						mvmvif->deflink.phy_ctxt->color));
815 	quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
816 	quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
817 
818 	for (i = 1; i < MAX_BINDINGS; i++) {
819 		quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, i);
820 		quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
821 	}
822 
823 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
824 				   iwl_mvm_quota_cmd_size(mvm), &quota_cmd);
825 	if (ret)
826 		IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
827 
828 	if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
829 		IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
830 
831 	return 0;
832 }
833 
834 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
835 				       struct ieee80211_vif *vif)
836 {
837 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
838 	struct iwl_nonqos_seq_query_cmd query_cmd = {
839 		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
840 		.mac_id_n_color =
841 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
842 							mvmvif->color)),
843 	};
844 	struct iwl_host_cmd cmd = {
845 		.id = NON_QOS_TX_COUNTER_CMD,
846 		.flags = CMD_WANT_SKB,
847 	};
848 	int err;
849 	u32 size;
850 
851 	cmd.data[0] = &query_cmd;
852 	cmd.len[0] = sizeof(query_cmd);
853 
854 	err = iwl_mvm_send_cmd(mvm, &cmd);
855 	if (err)
856 		return err;
857 
858 	size = iwl_rx_packet_payload_len(cmd.resp_pkt);
859 	if (size < sizeof(__le16)) {
860 		err = -EINVAL;
861 	} else {
862 		err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
863 		/* firmware returns next, not last-used seqno */
864 		err = (u16) (err - 0x10);
865 	}
866 
867 	iwl_free_resp(&cmd);
868 	return err;
869 }
870 
871 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
872 {
873 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
874 	struct iwl_nonqos_seq_query_cmd query_cmd = {
875 		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
876 		.mac_id_n_color =
877 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
878 							mvmvif->color)),
879 		.value = cpu_to_le16(mvmvif->seqno),
880 	};
881 
882 	/* return if called during restart, not resume from D3 */
883 	if (!mvmvif->seqno_valid)
884 		return;
885 
886 	mvmvif->seqno_valid = false;
887 
888 	if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
889 				 sizeof(query_cmd), &query_cmd))
890 		IWL_ERR(mvm, "failed to set non-QoS seqno\n");
891 }
892 
893 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
894 {
895 	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
896 
897 	iwl_mvm_stop_device(mvm);
898 	/*
899 	 * Set the HW restart bit -- this is mostly true as we're
900 	 * going to load new firmware and reprogram that, though
901 	 * the reprogramming is going to be manual to avoid adding
902 	 * all the MACs that aren't support.
903 	 * We don't have to clear up everything though because the
904 	 * reprogramming is manual. When we resume, we'll actually
905 	 * go through a proper restart sequence again to switch
906 	 * back to the runtime firmware image.
907 	 */
908 	set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
909 
910 	/* the fw is reset, so all the keys are cleared */
911 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
912 
913 	mvm->ptk_ivlen = 0;
914 	mvm->ptk_icvlen = 0;
915 	mvm->ptk_ivlen = 0;
916 	mvm->ptk_icvlen = 0;
917 
918 	return iwl_mvm_load_d3_fw(mvm);
919 }
920 
921 static int
922 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
923 			  struct cfg80211_wowlan *wowlan,
924 			  struct iwl_wowlan_config_cmd *wowlan_config_cmd,
925 			  struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
926 			  struct ieee80211_sta *ap_sta)
927 {
928 	struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
929 
930 	/* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
931 
932 	wowlan_config_cmd->is_11n_connection =
933 					ap_sta->deflink.ht_cap.ht_supported;
934 	wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
935 		ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
936 
937 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 6) {
938 		/* Query the last used seqno and set it */
939 		int ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
940 
941 		if (ret < 0)
942 			return ret;
943 
944 		wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
945 	}
946 
947 	iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
948 
949 	if (wowlan->disconnect)
950 		wowlan_config_cmd->wakeup_filter |=
951 			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
952 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
953 	if (wowlan->magic_pkt)
954 		wowlan_config_cmd->wakeup_filter |=
955 			cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
956 	if (wowlan->gtk_rekey_failure)
957 		wowlan_config_cmd->wakeup_filter |=
958 			cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
959 	if (wowlan->eap_identity_req)
960 		wowlan_config_cmd->wakeup_filter |=
961 			cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
962 	if (wowlan->four_way_handshake)
963 		wowlan_config_cmd->wakeup_filter |=
964 			cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
965 	if (wowlan->n_patterns)
966 		wowlan_config_cmd->wakeup_filter |=
967 			cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
968 
969 	if (wowlan->rfkill_release)
970 		wowlan_config_cmd->wakeup_filter |=
971 			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
972 
973 	if (wowlan->tcp) {
974 		/*
975 		 * Set the "link change" (really "link lost") flag as well
976 		 * since that implies losing the TCP connection.
977 		 */
978 		wowlan_config_cmd->wakeup_filter |=
979 			cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
980 				    IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
981 				    IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
982 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
983 	}
984 
985 	if (wowlan->any) {
986 		wowlan_config_cmd->wakeup_filter |=
987 			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
988 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE |
989 				    IWL_WOWLAN_WAKEUP_RX_FRAME |
990 				    IWL_WOWLAN_WAKEUP_BCN_FILTERING);
991 	}
992 
993 	return 0;
994 }
995 
996 static int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
997 					    struct ieee80211_vif *vif)
998 {
999 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
1000 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1001 	struct wowlan_key_reprogram_data key_data = {};
1002 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1003 	int ret;
1004 	u8 cmd_ver;
1005 	size_t cmd_size;
1006 
1007 	if (!unified) {
1008 		/*
1009 		 * if we have to configure keys, call ieee80211_iter_keys(),
1010 		 * as we need non-atomic context in order to take the
1011 		 * required locks.
1012 		 */
1013 		/*
1014 		 * Note that currently we don't use CMD_ASYNC in the iterator.
1015 		 * In case of key_data.configure_keys, all the configured
1016 		 * commands are SYNC, and iwl_mvm_wowlan_program_keys() will
1017 		 * take care of locking/unlocking mvm->mutex.
1018 		 */
1019 		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_program_keys,
1020 				    &key_data);
1021 
1022 		if (key_data.error)
1023 			return -EIO;
1024 	}
1025 
1026 	ret = iwl_mvm_wowlan_config_rsc_tsc(mvm, vif);
1027 	if (ret)
1028 		return ret;
1029 
1030 	if (!fw_has_api(&mvm->fw->ucode_capa,
1031 			IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
1032 		int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TKIP_PARAM,
1033 						IWL_FW_CMD_VER_UNKNOWN);
1034 		struct wowlan_key_tkip_data tkip_data = {};
1035 		int size;
1036 
1037 		if (ver == 2) {
1038 			size = sizeof(tkip_data.tkip);
1039 			tkip_data.tkip.sta_id =
1040 				cpu_to_le32(mvmvif->deflink.ap_sta_id);
1041 		} else if (ver == 1 || ver == IWL_FW_CMD_VER_UNKNOWN) {
1042 			size = sizeof(struct iwl_wowlan_tkip_params_cmd_ver_1);
1043 		} else {
1044 			WARN_ON_ONCE(1);
1045 			return -EINVAL;
1046 		}
1047 
1048 		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_get_tkip_data,
1049 				    &tkip_data);
1050 
1051 		if (tkip_data.have_tkip_keys) {
1052 			/* send relevant data according to CMD version */
1053 			ret = iwl_mvm_send_cmd_pdu(mvm,
1054 						   WOWLAN_TKIP_PARAM,
1055 						   CMD_ASYNC, size,
1056 						   &tkip_data.tkip);
1057 			if (ret)
1058 				return ret;
1059 		}
1060 	}
1061 
1062 	/* configure rekey data only if offloaded rekey is supported (d3) */
1063 	if (mvmvif->rekey_data.valid) {
1064 		struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {};
1065 		struct iwl_wowlan_kek_kck_material_cmd_v4 *_kek_kck_cmd =
1066 			&kek_kck_cmd;
1067 		struct wowlan_key_gtk_type_iter gtk_type_data = {
1068 			.kek_kck_cmd = _kek_kck_cmd,
1069 		};
1070 
1071 		cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1072 						WOWLAN_KEK_KCK_MATERIAL,
1073 						IWL_FW_CMD_VER_UNKNOWN);
1074 		if (WARN_ON(cmd_ver != 2 && cmd_ver != 3 && cmd_ver != 4 &&
1075 			    cmd_ver != IWL_FW_CMD_VER_UNKNOWN))
1076 			return -EINVAL;
1077 
1078 		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_gtk_type_iter,
1079 				    &gtk_type_data);
1080 
1081 		memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1082 		       mvmvif->rekey_data.kck_len);
1083 		kek_kck_cmd.kck_len = cpu_to_le16(mvmvif->rekey_data.kck_len);
1084 		memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1085 		       mvmvif->rekey_data.kek_len);
1086 		kek_kck_cmd.kek_len = cpu_to_le16(mvmvif->rekey_data.kek_len);
1087 		kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1088 		kek_kck_cmd.akm = cpu_to_le32(mvmvif->rekey_data.akm);
1089 		kek_kck_cmd.sta_id = cpu_to_le32(mvmvif->deflink.ap_sta_id);
1090 
1091 		if (cmd_ver == 4) {
1092 			cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v4);
1093 		} else {
1094 			if (cmd_ver == 3)
1095 				cmd_size =
1096 					sizeof(struct iwl_wowlan_kek_kck_material_cmd_v3);
1097 			else
1098 				cmd_size =
1099 					sizeof(struct iwl_wowlan_kek_kck_material_cmd_v2);
1100 			/* skip the sta_id at the beginning */
1101 			_kek_kck_cmd = (void *)
1102 				((u8 *)_kek_kck_cmd + sizeof(kek_kck_cmd.sta_id));
1103 		}
1104 
1105 		IWL_DEBUG_WOWLAN(mvm, "setting akm %d\n",
1106 				 mvmvif->rekey_data.akm);
1107 
1108 		ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_KEK_KCK_MATERIAL,
1109 					   CMD_ASYNC, cmd_size, _kek_kck_cmd);
1110 		if (ret)
1111 			return ret;
1112 	}
1113 
1114 	return 0;
1115 }
1116 
1117 static int
1118 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1119 		      struct cfg80211_wowlan *wowlan,
1120 		      struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1121 		      struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1122 		      struct ieee80211_sta *ap_sta)
1123 {
1124 	int ret;
1125 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1126 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1127 
1128 	mvm->offload_tid = wowlan_config_cmd->offloading_tid;
1129 
1130 	if (!unified_image) {
1131 		ret = iwl_mvm_switch_to_d3(mvm);
1132 		if (ret)
1133 			return ret;
1134 
1135 		ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1136 		if (ret)
1137 			return ret;
1138 	}
1139 
1140 	/*
1141 	 * This needs to be unlocked due to lock ordering
1142 	 * constraints. Since we're in the suspend path
1143 	 * that isn't really a problem though.
1144 	 */
1145 	mutex_unlock(&mvm->mutex);
1146 	ret = iwl_mvm_wowlan_config_key_params(mvm, vif);
1147 	mutex_lock(&mvm->mutex);
1148 	if (ret)
1149 		return ret;
1150 
1151 	ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1152 				   sizeof(*wowlan_config_cmd),
1153 				   wowlan_config_cmd);
1154 	if (ret)
1155 		return ret;
1156 
1157 	if (fw_has_api(&mvm->fw->ucode_capa,
1158 		       IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE))
1159 		ret = iwl_mvm_send_patterns(mvm, vif, wowlan);
1160 	else
1161 		ret = iwl_mvm_send_patterns_v1(mvm, wowlan);
1162 	if (ret)
1163 		return ret;
1164 
1165 	return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
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_d3_manager_config d3_cfg_cmd_data = {
1241 		/*
1242 		 * Program the minimum sleep time to 10 seconds, as many
1243 		 * platforms have issues processing a wakeup signal while
1244 		 * still being in the process of suspending.
1245 		 */
1246 		.min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1247 	};
1248 	struct iwl_host_cmd d3_cfg_cmd = {
1249 		.id = D3_CONFIG_CMD,
1250 		.flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
1251 		.data[0] = &d3_cfg_cmd_data,
1252 		.len[0] = sizeof(d3_cfg_cmd_data),
1253 	};
1254 	int ret;
1255 	int len __maybe_unused;
1256 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1257 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1258 
1259 	if (!wowlan) {
1260 		/*
1261 		 * mac80211 shouldn't get here, but for D3 test
1262 		 * it doesn't warrant a warning
1263 		 */
1264 		WARN_ON(!test);
1265 		return -EINVAL;
1266 	}
1267 
1268 	mutex_lock(&mvm->mutex);
1269 
1270 	set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1271 
1272 	synchronize_net();
1273 
1274 	vif = iwl_mvm_get_bss_vif(mvm);
1275 	if (IS_ERR_OR_NULL(vif)) {
1276 		ret = 1;
1277 		goto out_noreset;
1278 	}
1279 
1280 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1281 
1282 	if (mvmvif->deflink.ap_sta_id == IWL_MVM_INVALID_STA) {
1283 		/* if we're not associated, this must be netdetect */
1284 		if (!wowlan->nd_config) {
1285 			ret = 1;
1286 			goto out_noreset;
1287 		}
1288 
1289 		ret = iwl_mvm_netdetect_config(
1290 			mvm, wowlan, wowlan->nd_config, vif);
1291 		if (ret)
1292 			goto out;
1293 
1294 		mvm->net_detect = true;
1295 	} else {
1296 		struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1297 
1298 		wowlan_config_cmd.sta_id = mvmvif->deflink.ap_sta_id;
1299 
1300 		ap_sta = rcu_dereference_protected(
1301 			mvm->fw_id_to_mac_id[mvmvif->deflink.ap_sta_id],
1302 			lockdep_is_held(&mvm->mutex));
1303 		if (IS_ERR_OR_NULL(ap_sta)) {
1304 			ret = -EINVAL;
1305 			goto out_noreset;
1306 		}
1307 
1308 		ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1309 						vif, mvmvif, ap_sta);
1310 		if (ret)
1311 			goto out_noreset;
1312 		ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1313 					    vif, mvmvif, ap_sta);
1314 		if (ret)
1315 			goto out;
1316 
1317 		mvm->net_detect = false;
1318 	}
1319 
1320 	ret = iwl_mvm_power_update_device(mvm);
1321 	if (ret)
1322 		goto out;
1323 
1324 	ret = iwl_mvm_power_update_mac(mvm);
1325 	if (ret)
1326 		goto out;
1327 
1328 #ifdef CONFIG_IWLWIFI_DEBUGFS
1329 	if (mvm->d3_wake_sysassert)
1330 		d3_cfg_cmd_data.wakeup_flags |=
1331 			cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1332 #endif
1333 
1334 	/*
1335 	 * Prior to 9000 device family the driver needs to stop the dbg
1336 	 * recording before entering D3. In later devices the FW stops the
1337 	 * recording automatically.
1338 	 */
1339 	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1340 		iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
1341 
1342 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1343 
1344 	/* must be last -- this switches firmware state */
1345 	ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1346 	if (ret)
1347 		goto out;
1348 #ifdef CONFIG_IWLWIFI_DEBUGFS
1349 	len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1350 	if (len >= sizeof(u32)) {
1351 		mvm->d3_test_pme_ptr =
1352 			le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1353 	}
1354 #endif
1355 	iwl_free_resp(&d3_cfg_cmd);
1356 
1357 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1358 
1359 	ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1360  out:
1361 	if (ret < 0) {
1362 		iwl_mvm_free_nd(mvm);
1363 
1364 		if (!unified_image) {
1365 			if (mvm->fw_restart > 0) {
1366 				mvm->fw_restart--;
1367 				ieee80211_restart_hw(mvm->hw);
1368 			}
1369 		}
1370 
1371 		clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1372 	}
1373  out_noreset:
1374 	mutex_unlock(&mvm->mutex);
1375 
1376 	return ret;
1377 }
1378 
1379 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1380 {
1381 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1382 
1383 	iwl_mvm_pause_tcm(mvm, true);
1384 
1385 	iwl_fw_runtime_suspend(&mvm->fwrt);
1386 
1387 	return __iwl_mvm_suspend(hw, wowlan, false);
1388 }
1389 
1390 struct iwl_multicast_key_data {
1391 	u8 key[WOWLAN_KEY_MAX_SIZE];
1392 	u8 len;
1393 	u8 flags;
1394 	u8 id;
1395 	u8 ipn[6];
1396 };
1397 
1398 /* converted data from the different status responses */
1399 struct iwl_wowlan_status_data {
1400 	u64 replay_ctr;
1401 	u32 num_of_gtk_rekeys;
1402 	u32 received_beacons;
1403 	u32 wakeup_reasons;
1404 	u32 wake_packet_length;
1405 	u32 wake_packet_bufsize;
1406 	u16 pattern_number;
1407 	u16 non_qos_seq_ctr;
1408 	u16 qos_seq_ctr[8];
1409 	u8 tid_tear_down;
1410 
1411 	struct {
1412 		/* including RX MIC key for TKIP */
1413 		u8 key[WOWLAN_KEY_MAX_SIZE];
1414 		u8 len;
1415 		u8 flags;
1416 		u8 id;
1417 	} gtk[WOWLAN_GTK_KEYS_NUM];
1418 
1419 	struct {
1420 		/*
1421 		 * We store both the TKIP and AES representations
1422 		 * coming from the firmware because we decode the
1423 		 * data from there before we iterate the keys and
1424 		 * know which one we need.
1425 		 */
1426 		struct {
1427 			struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1428 		} tkip, aes;
1429 
1430 		/*
1431 		 * We use -1 for when we have valid data but don't know
1432 		 * the key ID from firmware, and thus it needs to be
1433 		 * installed with the last key (depending on rekeying).
1434 		 */
1435 		s8 key_id;
1436 		bool valid;
1437 	} gtk_seq[2];
1438 
1439 	struct {
1440 		/* Same as above */
1441 		struct {
1442 			struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1443 			u64 tx_pn;
1444 		} tkip, aes;
1445 	} ptk;
1446 
1447 	struct iwl_multicast_key_data igtk;
1448 
1449 	u8 *wake_packet;
1450 };
1451 
1452 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1453 					  struct ieee80211_vif *vif,
1454 					  struct iwl_wowlan_status_data *status)
1455 {
1456 	struct sk_buff *pkt = NULL;
1457 	struct cfg80211_wowlan_wakeup wakeup = {
1458 		.pattern_idx = -1,
1459 	};
1460 	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1461 	u32 reasons = status->wakeup_reasons;
1462 
1463 	if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1464 		wakeup_report = NULL;
1465 		goto report;
1466 	}
1467 
1468 	pm_wakeup_event(mvm->dev, 0);
1469 
1470 	if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1471 		wakeup.magic_pkt = true;
1472 
1473 	if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1474 		wakeup.pattern_idx =
1475 			status->pattern_number;
1476 
1477 	if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1478 		       IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1479 		wakeup.disconnect = true;
1480 
1481 	if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1482 		wakeup.gtk_rekey_failure = true;
1483 
1484 	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1485 		wakeup.rfkill_release = true;
1486 
1487 	if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1488 		wakeup.eap_identity_req = true;
1489 
1490 	if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1491 		wakeup.four_way_handshake = true;
1492 
1493 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1494 		wakeup.tcp_connlost = true;
1495 
1496 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1497 		wakeup.tcp_nomoretokens = true;
1498 
1499 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1500 		wakeup.tcp_match = true;
1501 
1502 	if (status->wake_packet) {
1503 		int pktsize = status->wake_packet_bufsize;
1504 		int pktlen = status->wake_packet_length;
1505 		const u8 *pktdata = status->wake_packet;
1506 		const struct ieee80211_hdr *hdr = (const void *)pktdata;
1507 		int truncated = pktlen - pktsize;
1508 
1509 		/* this would be a firmware bug */
1510 		if (WARN_ON_ONCE(truncated < 0))
1511 			truncated = 0;
1512 
1513 		if (ieee80211_is_data(hdr->frame_control)) {
1514 			int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1515 			int ivlen = 0, icvlen = 4; /* also FCS */
1516 
1517 			pkt = alloc_skb(pktsize, GFP_KERNEL);
1518 			if (!pkt)
1519 				goto report;
1520 
1521 			skb_put_data(pkt, pktdata, hdrlen);
1522 			pktdata += hdrlen;
1523 			pktsize -= hdrlen;
1524 
1525 			if (ieee80211_has_protected(hdr->frame_control)) {
1526 				/*
1527 				 * This is unlocked and using gtk_i(c)vlen,
1528 				 * but since everything is under RTNL still
1529 				 * that's not really a problem - changing
1530 				 * it would be difficult.
1531 				 */
1532 				if (is_multicast_ether_addr(hdr->addr1)) {
1533 					ivlen = mvm->gtk_ivlen;
1534 					icvlen += mvm->gtk_icvlen;
1535 				} else {
1536 					ivlen = mvm->ptk_ivlen;
1537 					icvlen += mvm->ptk_icvlen;
1538 				}
1539 			}
1540 
1541 			/* if truncated, FCS/ICV is (partially) gone */
1542 			if (truncated >= icvlen) {
1543 				icvlen = 0;
1544 				truncated -= icvlen;
1545 			} else {
1546 				icvlen -= truncated;
1547 				truncated = 0;
1548 			}
1549 
1550 			pktsize -= ivlen + icvlen;
1551 			pktdata += ivlen;
1552 
1553 			skb_put_data(pkt, pktdata, pktsize);
1554 
1555 			if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1556 				goto report;
1557 			wakeup.packet = pkt->data;
1558 			wakeup.packet_present_len = pkt->len;
1559 			wakeup.packet_len = pkt->len - truncated;
1560 			wakeup.packet_80211 = false;
1561 		} else {
1562 			int fcslen = 4;
1563 
1564 			if (truncated >= 4) {
1565 				truncated -= 4;
1566 				fcslen = 0;
1567 			} else {
1568 				fcslen -= truncated;
1569 				truncated = 0;
1570 			}
1571 			pktsize -= fcslen;
1572 			wakeup.packet = status->wake_packet;
1573 			wakeup.packet_present_len = pktsize;
1574 			wakeup.packet_len = pktlen - truncated;
1575 			wakeup.packet_80211 = true;
1576 		}
1577 	}
1578 
1579  report:
1580 	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1581 	kfree_skb(pkt);
1582 }
1583 
1584 static void iwl_mvm_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1585 {
1586 	u64 pn = le64_to_cpu(le_pn);
1587 
1588 	seq->ccmp.pn[0] = pn >> 40;
1589 	seq->ccmp.pn[1] = pn >> 32;
1590 	seq->ccmp.pn[2] = pn >> 24;
1591 	seq->ccmp.pn[3] = pn >> 16;
1592 	seq->ccmp.pn[4] = pn >> 8;
1593 	seq->ccmp.pn[5] = pn;
1594 }
1595 
1596 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1597 				  struct ieee80211_key_seq *seq)
1598 {
1599 	iwl_mvm_le64_to_aes_seq(sc->pn, seq);
1600 }
1601 
1602 static void iwl_mvm_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1603 {
1604 	u64 pn = le64_to_cpu(le_pn);
1605 
1606 	seq->tkip.iv16 = (u16)pn;
1607 	seq->tkip.iv32 = (u32)(pn >> 16);
1608 }
1609 
1610 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1611 				   struct ieee80211_key_seq *seq)
1612 {
1613 	seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1614 	seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1615 }
1616 
1617 static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key,
1618 					struct ieee80211_key_seq *seq)
1619 {
1620 	int tid;
1621 
1622 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1623 		ieee80211_set_key_rx_seq(key, tid, &seq[tid]);
1624 }
1625 
1626 static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm,
1627 				       struct iwl_wowlan_status_data *status,
1628 				       struct ieee80211_sta *sta,
1629 				       struct ieee80211_key_conf *key)
1630 {
1631 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1632 	struct iwl_mvm_key_pn *ptk_pn;
1633 	int tid;
1634 
1635 	iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq);
1636 
1637 	if (!iwl_mvm_has_new_rx_api(mvm))
1638 		return;
1639 
1640 
1641 	rcu_read_lock();
1642 	ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
1643 	if (WARN_ON(!ptk_pn)) {
1644 		rcu_read_unlock();
1645 		return;
1646 	}
1647 
1648 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1649 		int i;
1650 
1651 		for (i = 1; i < mvm->trans->num_rx_queues; i++)
1652 			memcpy(ptk_pn->q[i].pn[tid],
1653 			       status->ptk.aes.seq[tid].ccmp.pn,
1654 			       IEEE80211_CCMP_PN_LEN);
1655 	}
1656 	rcu_read_unlock();
1657 }
1658 
1659 static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status,
1660 					 union iwl_all_tsc_rsc *sc)
1661 {
1662 	int i;
1663 
1664 	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1665 	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1666 
1667 	/* GTK RX counters */
1668 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1669 		iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i],
1670 				       &status->gtk_seq[0].tkip.seq[i]);
1671 		iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i],
1672 				      &status->gtk_seq[0].aes.seq[i]);
1673 	}
1674 	status->gtk_seq[0].valid = true;
1675 	status->gtk_seq[0].key_id = -1;
1676 
1677 	/* PTK TX counter */
1678 	status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) |
1679 				 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16);
1680 	status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn);
1681 
1682 	/* PTK RX counters */
1683 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1684 		iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i],
1685 				       &status->ptk.tkip.seq[i]);
1686 		iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i],
1687 				      &status->ptk.aes.seq[i]);
1688 	}
1689 }
1690 
1691 static void
1692 iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data *status,
1693 					struct iwl_wowlan_all_rsc_tsc_v5 *sc,
1694 					unsigned int idx, unsigned int key_id)
1695 {
1696 	int tid;
1697 
1698 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1699 		iwl_mvm_le64_to_tkip_seq(sc->mcast_rsc[idx][tid],
1700 					 &status->gtk_seq[idx].tkip.seq[tid]);
1701 		iwl_mvm_le64_to_aes_seq(sc->mcast_rsc[idx][tid],
1702 					&status->gtk_seq[idx].aes.seq[tid]);
1703 	}
1704 
1705 	status->gtk_seq[idx].valid = true;
1706 	status->gtk_seq[idx].key_id = key_id;
1707 }
1708 
1709 static void
1710 iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data *status,
1711 				struct iwl_wowlan_all_rsc_tsc_v5 *sc)
1712 {
1713 	int i, tid;
1714 
1715 	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1716 	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1717 	BUILD_BUG_ON(ARRAY_SIZE(sc->mcast_rsc) != ARRAY_SIZE(status->gtk_seq));
1718 
1719 	/* GTK RX counters */
1720 	for (i = 0; i < ARRAY_SIZE(sc->mcast_key_id_map); i++) {
1721 		u8 entry = sc->mcast_key_id_map[i];
1722 
1723 		if (entry < ARRAY_SIZE(sc->mcast_rsc))
1724 			iwl_mvm_convert_key_counters_v5_gtk_seq(status, sc,
1725 								entry, i);
1726 	}
1727 
1728 	/* PTK TX counters not needed, assigned in device */
1729 
1730 	/* PTK RX counters */
1731 	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1732 		iwl_mvm_le64_to_tkip_seq(sc->ucast_rsc[tid],
1733 					 &status->ptk.tkip.seq[tid]);
1734 		iwl_mvm_le64_to_aes_seq(sc->ucast_rsc[tid],
1735 					&status->ptk.aes.seq[tid]);
1736 	}
1737 }
1738 
1739 static void iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf *key,
1740 				       struct iwl_wowlan_status_data *status,
1741 				       int idx)
1742 {
1743 	switch (key->cipher) {
1744 	case WLAN_CIPHER_SUITE_CCMP:
1745 	case WLAN_CIPHER_SUITE_GCMP:
1746 	case WLAN_CIPHER_SUITE_GCMP_256:
1747 		iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].aes.seq);
1748 		break;
1749 	case WLAN_CIPHER_SUITE_TKIP:
1750 		iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].tkip.seq);
1751 		break;
1752 	default:
1753 		WARN_ON(1);
1754 	}
1755 }
1756 
1757 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1758 				   struct iwl_wowlan_status_data *status,
1759 				   bool installed)
1760 {
1761 	int i;
1762 
1763 	for (i = 0; i < ARRAY_SIZE(status->gtk_seq); i++) {
1764 		if (!status->gtk_seq[i].valid)
1765 			continue;
1766 
1767 		/* Handle the case where we know the key ID */
1768 		if (status->gtk_seq[i].key_id == key->keyidx) {
1769 			s8 new_key_id = -1;
1770 
1771 			if (status->num_of_gtk_rekeys)
1772 				new_key_id = status->gtk[0].flags &
1773 						IWL_WOWLAN_GTK_IDX_MASK;
1774 
1775 			/* Don't install a new key's value to an old key */
1776 			if (new_key_id != key->keyidx)
1777 				iwl_mvm_set_key_rx_seq_idx(key, status, i);
1778 			continue;
1779 		}
1780 
1781 		/* handle the case where we didn't, last key only */
1782 		if (status->gtk_seq[i].key_id == -1 &&
1783 		    (!status->num_of_gtk_rekeys || installed))
1784 			iwl_mvm_set_key_rx_seq_idx(key, status, i);
1785 	}
1786 }
1787 
1788 struct iwl_mvm_d3_gtk_iter_data {
1789 	struct iwl_mvm *mvm;
1790 	struct iwl_wowlan_status_data *status;
1791 	u32 gtk_cipher, igtk_cipher;
1792 	bool unhandled_cipher, igtk_support;
1793 	int num_keys;
1794 };
1795 
1796 static void iwl_mvm_d3_find_last_keys(struct ieee80211_hw *hw,
1797 				      struct ieee80211_vif *vif,
1798 				      struct ieee80211_sta *sta,
1799 				      struct ieee80211_key_conf *key,
1800 				      void *_data)
1801 {
1802 	struct iwl_mvm_d3_gtk_iter_data *data = _data;
1803 
1804 	if (data->unhandled_cipher)
1805 		return;
1806 
1807 	switch (key->cipher) {
1808 	case WLAN_CIPHER_SUITE_WEP40:
1809 	case WLAN_CIPHER_SUITE_WEP104:
1810 		/* ignore WEP completely, nothing to do */
1811 		return;
1812 	case WLAN_CIPHER_SUITE_CCMP:
1813 	case WLAN_CIPHER_SUITE_GCMP:
1814 	case WLAN_CIPHER_SUITE_GCMP_256:
1815 	case WLAN_CIPHER_SUITE_TKIP:
1816 		/* we support these */
1817 		data->gtk_cipher = key->cipher;
1818 		break;
1819 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1820 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1821 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1822 	case WLAN_CIPHER_SUITE_AES_CMAC:
1823 		/* we support these */
1824 		if (data->igtk_support &&
1825 		    (key->keyidx == 4 || key->keyidx == 5)) {
1826 			data->igtk_cipher = key->cipher;
1827 		} else {
1828 			data->unhandled_cipher = true;
1829 			return;
1830 		}
1831 		break;
1832 	default:
1833 		/* everything else - disconnect from AP */
1834 		data->unhandled_cipher = true;
1835 		return;
1836 	}
1837 
1838 	data->num_keys++;
1839 }
1840 
1841 static void
1842 iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data *key,
1843 			      struct ieee80211_key_seq *seq, u32 cipher)
1844 {
1845 	switch (cipher) {
1846 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1847 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1848 		BUILD_BUG_ON(sizeof(seq->aes_gmac.pn) != sizeof(key->ipn));
1849 		memcpy(seq->aes_gmac.pn, key->ipn, sizeof(seq->aes_gmac.pn));
1850 		break;
1851 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1852 		BUILD_BUG_ON(sizeof(seq->aes_cmac.pn) != sizeof(key->ipn));
1853 		memcpy(seq->aes_cmac.pn, key->ipn, sizeof(seq->aes_cmac.pn));
1854 		break;
1855 	}
1856 }
1857 
1858 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1859 				   struct ieee80211_vif *vif,
1860 				   struct ieee80211_sta *sta,
1861 				   struct ieee80211_key_conf *key,
1862 				   void *_data)
1863 {
1864 	struct iwl_mvm_d3_gtk_iter_data *data = _data;
1865 	struct iwl_wowlan_status_data *status = data->status;
1866 	s8 keyidx;
1867 
1868 	if (data->unhandled_cipher)
1869 		return;
1870 
1871 	switch (key->cipher) {
1872 	case WLAN_CIPHER_SUITE_WEP40:
1873 	case WLAN_CIPHER_SUITE_WEP104:
1874 		/* ignore WEP completely, nothing to do */
1875 		return;
1876 	case WLAN_CIPHER_SUITE_CCMP:
1877 	case WLAN_CIPHER_SUITE_GCMP:
1878 	case WLAN_CIPHER_SUITE_GCMP_256:
1879 		if (sta) {
1880 			atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn);
1881 			iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key);
1882 			return;
1883 		}
1884 		fallthrough;
1885 	case WLAN_CIPHER_SUITE_TKIP:
1886 		if (sta) {
1887 			atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn);
1888 			iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq);
1889 			return;
1890 		}
1891 		keyidx = key->keyidx;
1892 		/* The current key is always sent by the FW, even if it wasn't
1893 		 * rekeyed during D3.
1894 		 * We remove an existing key if it has the same index as
1895 		 * a new key
1896 		 */
1897 		if (status->num_of_gtk_rekeys &&
1898 		    ((status->gtk[0].len && keyidx == status->gtk[0].id) ||
1899 		     (status->gtk[1].len && keyidx == status->gtk[1].id))) {
1900 			ieee80211_remove_key(key);
1901 		} else {
1902 			iwl_mvm_set_key_rx_seq(key, data->status, false);
1903 		}
1904 		break;
1905 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1906 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1907 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1908 	case WLAN_CIPHER_SUITE_AES_CMAC:
1909 		if (key->keyidx == 4 || key->keyidx == 5) {
1910 			/* remove rekeyed key */
1911 			if (status->num_of_gtk_rekeys) {
1912 				ieee80211_remove_key(key);
1913 			} else {
1914 				struct ieee80211_key_seq seq;
1915 
1916 				iwl_mvm_d3_set_igtk_bigtk_ipn(&status->igtk,
1917 							      &seq,
1918 							      key->cipher);
1919 				ieee80211_set_key_rx_seq(key, 0, &seq);
1920 			}
1921 		}
1922 	}
1923 }
1924 
1925 static bool iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data *status,
1926 			      struct ieee80211_vif *vif,
1927 			      struct iwl_mvm *mvm, u32 gtk_cipher)
1928 {
1929 	int i;
1930 	struct ieee80211_key_conf *key;
1931 	struct {
1932 		struct ieee80211_key_conf conf;
1933 		u8 key[32];
1934 	} conf = {
1935 		.conf.cipher = gtk_cipher,
1936 	};
1937 
1938 	BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
1939 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP);
1940 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256);
1941 	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP);
1942 	BUILD_BUG_ON(sizeof(conf.key) < sizeof(status->gtk[0].key));
1943 
1944 	switch (gtk_cipher) {
1945 	case WLAN_CIPHER_SUITE_CCMP:
1946 	case WLAN_CIPHER_SUITE_GCMP:
1947 		conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1948 		break;
1949 	case WLAN_CIPHER_SUITE_GCMP_256:
1950 		conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
1951 		break;
1952 	case WLAN_CIPHER_SUITE_TKIP:
1953 		conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1954 		break;
1955 	default:
1956 		WARN_ON(1);
1957 	}
1958 
1959 	for (i = 0; i < ARRAY_SIZE(status->gtk); i++) {
1960 		if (!status->gtk[i].len)
1961 			continue;
1962 
1963 		conf.conf.keyidx = status->gtk[i].id;
1964 		IWL_DEBUG_WOWLAN(mvm,
1965 				 "Received from FW GTK cipher %d, key index %d\n",
1966 				 conf.conf.cipher, conf.conf.keyidx);
1967 		memcpy(conf.conf.key, status->gtk[i].key,
1968 		       sizeof(status->gtk[i].key));
1969 
1970 		key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1971 		if (IS_ERR(key))
1972 			return false;
1973 		iwl_mvm_set_key_rx_seq_idx(key, status, i);
1974 	}
1975 
1976 	return true;
1977 }
1978 
1979 static bool
1980 iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data *status,
1981 				struct ieee80211_vif *vif, u32 cipher,
1982 				struct iwl_multicast_key_data *key_data)
1983 {
1984 	struct ieee80211_key_conf *key_config;
1985 	struct {
1986 		struct ieee80211_key_conf conf;
1987 		u8 key[WOWLAN_KEY_MAX_SIZE];
1988 	} conf = {
1989 		.conf.cipher = cipher,
1990 		.conf.keyidx = key_data->id,
1991 	};
1992 	struct ieee80211_key_seq seq;
1993 
1994 	if (!key_data->len)
1995 		return true;
1996 
1997 	iwl_mvm_d3_set_igtk_bigtk_ipn(key_data, &seq, conf.conf.cipher);
1998 
1999 	switch (cipher) {
2000 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2001 		conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2002 		break;
2003 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2004 		conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2005 		break;
2006 	case WLAN_CIPHER_SUITE_AES_CMAC:
2007 		conf.conf.keylen = WLAN_KEY_LEN_AES_CMAC;
2008 		break;
2009 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2010 		conf.conf.keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2011 		break;
2012 	default:
2013 		WARN_ON(1);
2014 	}
2015 	BUILD_BUG_ON(sizeof(conf.key) < sizeof(key_data->key));
2016 	memcpy(conf.conf.key, key_data->key, conf.conf.keylen);
2017 
2018 	key_config = ieee80211_gtk_rekey_add(vif, &conf.conf);
2019 	if (IS_ERR(key_config))
2020 		return false;
2021 	ieee80211_set_key_rx_seq(key_config, 0, &seq);
2022 
2023 	if (key_config->keyidx == 4 || key_config->keyidx == 5) {
2024 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2025 		int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
2026 		struct iwl_mvm_vif_link_info *mvm_link =
2027 			mvmvif->link[link_id];
2028 
2029 		mvm_link->igtk = key_config;
2030 	}
2031 
2032 	return true;
2033 }
2034 
2035 static int iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm *mvm)
2036 {
2037 	u8 notif_ver;
2038 
2039 	if (!fw_has_api(&mvm->fw->ucode_capa,
2040 			IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL))
2041 		return 6;
2042 
2043 	/* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2044 	notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2045 					    WOWLAN_GET_STATUSES, 0);
2046 	if (!notif_ver)
2047 		notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
2048 						    WOWLAN_GET_STATUSES, 7);
2049 
2050 	return notif_ver;
2051 }
2052 
2053 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
2054 					  struct ieee80211_vif *vif,
2055 					  struct iwl_wowlan_status_data *status)
2056 {
2057 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2058 	struct iwl_mvm_d3_gtk_iter_data gtkdata = {
2059 		.mvm = mvm,
2060 		.status = status,
2061 	};
2062 	u32 disconnection_reasons =
2063 		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
2064 		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
2065 
2066 	if (!status || !vif->bss_conf.bssid)
2067 		return false;
2068 
2069 	if (status->wakeup_reasons & disconnection_reasons)
2070 		return false;
2071 
2072 	if (iwl_mvm_lookup_wowlan_status_ver(mvm) > 6 ||
2073 	    iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2074 				    WOWLAN_INFO_NOTIFICATION,
2075 				    0))
2076 		gtkdata.igtk_support = true;
2077 
2078 	/* find last GTK that we used initially, if any */
2079 	ieee80211_iter_keys(mvm->hw, vif,
2080 			    iwl_mvm_d3_find_last_keys, &gtkdata);
2081 	/* not trying to keep connections with MFP/unhandled ciphers */
2082 	if (gtkdata.unhandled_cipher)
2083 		return false;
2084 	if (!gtkdata.num_keys)
2085 		goto out;
2086 
2087 	/*
2088 	 * invalidate all other GTKs that might still exist and update
2089 	 * the one that we used
2090 	 */
2091 	ieee80211_iter_keys(mvm->hw, vif,
2092 			    iwl_mvm_d3_update_keys, &gtkdata);
2093 
2094 	if (status->num_of_gtk_rekeys) {
2095 		__be64 replay_ctr = cpu_to_be64(status->replay_ctr);
2096 
2097 		IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
2098 				 status->num_of_gtk_rekeys);
2099 
2100 		if (!iwl_mvm_gtk_rekey(status, vif, mvm, gtkdata.gtk_cipher))
2101 			return false;
2102 
2103 		if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2104 						     gtkdata.igtk_cipher,
2105 						     &status->igtk))
2106 			return false;
2107 
2108 		ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
2109 					   (void *)&replay_ctr, GFP_KERNEL);
2110 	}
2111 
2112 out:
2113 	if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2114 				    WOWLAN_GET_STATUSES,
2115 				    IWL_FW_CMD_VER_UNKNOWN) < 10) {
2116 		mvmvif->seqno_valid = true;
2117 		/* +0x10 because the set API expects next-to-use, not last-used */
2118 		mvmvif->seqno = status->non_qos_seq_ctr + 0x10;
2119 	}
2120 
2121 	return true;
2122 }
2123 
2124 static void iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data *status,
2125 				   struct iwl_wowlan_gtk_status_v2 *data)
2126 {
2127 	BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data->key));
2128 	BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2129 		     sizeof(data->tkip_mic_key) >
2130 		     sizeof(status->gtk[0].key));
2131 
2132 	status->gtk[0].len = data->key_len;
2133 	status->gtk[0].flags = data->key_flags;
2134 
2135 	memcpy(status->gtk[0].key, data->key, sizeof(data->key));
2136 
2137 	/* if it's as long as the TKIP encryption key, copy MIC key */
2138 	if (status->gtk[0].len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2139 		memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2140 		       data->tkip_mic_key, sizeof(data->tkip_mic_key));
2141 }
2142 
2143 static void iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data *status,
2144 				   struct iwl_wowlan_gtk_status_v3 *data)
2145 {
2146 	int data_idx, status_idx = 0;
2147 
2148 	BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data[0].key));
2149 	BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2150 		     sizeof(data[0].tkip_mic_key) >
2151 		     sizeof(status->gtk[0].key));
2152 	BUILD_BUG_ON(ARRAY_SIZE(status->gtk) < WOWLAN_GTK_KEYS_NUM);
2153 	for (data_idx = 0; data_idx < ARRAY_SIZE(status->gtk); data_idx++) {
2154 		if (!(data[data_idx].key_len))
2155 			continue;
2156 		status->gtk[status_idx].len = data[data_idx].key_len;
2157 		status->gtk[status_idx].flags = data[data_idx].key_flags;
2158 		status->gtk[status_idx].id = status->gtk[status_idx].flags &
2159 				    IWL_WOWLAN_GTK_IDX_MASK;
2160 
2161 		memcpy(status->gtk[status_idx].key, data[data_idx].key,
2162 		       sizeof(data[data_idx].key));
2163 
2164 		/* if it's as long as the TKIP encryption key, copy MIC key */
2165 		if (status->gtk[status_idx].len ==
2166 		    NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2167 			memcpy(status->gtk[status_idx].key +
2168 			       NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2169 			       data[data_idx].tkip_mic_key,
2170 			       sizeof(data[data_idx].tkip_mic_key));
2171 		status_idx++;
2172 	}
2173 }
2174 
2175 static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status,
2176 				 struct iwl_wowlan_igtk_status *data)
2177 {
2178 	int i;
2179 
2180 	BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key));
2181 	BUILD_BUG_ON(sizeof(status->igtk.ipn) != sizeof(data->ipn));
2182 
2183 	if (!data->key_len)
2184 		return;
2185 
2186 	status->igtk.len = data->key_len;
2187 	status->igtk.flags = data->key_flags;
2188 	status->igtk.id = u32_get_bits(data->key_flags,
2189 				       IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2190 		+ WOWLAN_IGTK_MIN_INDEX;
2191 
2192 	memcpy(status->igtk.key, data->key, sizeof(data->key));
2193 
2194 	/* mac80211 expects big endian for memcmp() to work, convert */
2195 	for (i = 0; i < sizeof(data->ipn); i++)
2196 		status->igtk.ipn[i] = data->ipn[sizeof(data->ipn) - i - 1];
2197 }
2198 
2199 static void iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm *mvm,
2200 					    struct iwl_wowlan_info_notif *data,
2201 					    struct iwl_wowlan_status_data *status,
2202 					    u32 len)
2203 {
2204 	u32 i;
2205 
2206 	if (!data) {
2207 		IWL_ERR(mvm, "iwl_wowlan_info_notif data is NULL\n");
2208 		status = NULL;
2209 		return;
2210 	}
2211 
2212 	if (len < sizeof(*data)) {
2213 		IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2214 		status = NULL;
2215 		return;
2216 	}
2217 
2218 	iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2219 	iwl_mvm_convert_gtk_v3(status, data->gtk);
2220 	iwl_mvm_convert_igtk(status, &data->igtk[0]);
2221 
2222 	status->replay_ctr = le64_to_cpu(data->replay_ctr);
2223 	status->pattern_number = le16_to_cpu(data->pattern_number);
2224 	for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2225 		status->qos_seq_ctr[i] =
2226 			le16_to_cpu(data->qos_seq_ctr[i]);
2227 	status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2228 	status->num_of_gtk_rekeys =
2229 		le32_to_cpu(data->num_of_gtk_rekeys);
2230 	status->received_beacons = le32_to_cpu(data->received_beacons);
2231 	status->tid_tear_down = data->tid_tear_down;
2232 }
2233 
2234 /* Occasionally, templates would be nice. This is one of those times ... */
2235 #define iwl_mvm_parse_wowlan_status_common(_ver)			\
2236 static struct iwl_wowlan_status_data *					\
2237 iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm,	\
2238 					    struct iwl_wowlan_status_ ##_ver *data,\
2239 					    int len)			\
2240 {									\
2241 	struct iwl_wowlan_status_data *status;				\
2242 	int data_size, i;						\
2243 									\
2244 	if (len < sizeof(*data)) {					\
2245 		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
2246 		return NULL;						\
2247 	}								\
2248 									\
2249 	data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4);	\
2250 	if (len != sizeof(*data) + data_size) {				\
2251 		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
2252 		return NULL;						\
2253 	}								\
2254 									\
2255 	status = kzalloc(sizeof(*status), GFP_KERNEL);			\
2256 	if (!status)							\
2257 		return NULL;						\
2258 									\
2259 	/* copy all the common fields */				\
2260 	status->replay_ctr = le64_to_cpu(data->replay_ctr);		\
2261 	status->pattern_number = le16_to_cpu(data->pattern_number);	\
2262 	status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr);	\
2263 	for (i = 0; i < 8; i++)						\
2264 		status->qos_seq_ctr[i] =				\
2265 			le16_to_cpu(data->qos_seq_ctr[i]);		\
2266 	status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);	\
2267 	status->num_of_gtk_rekeys =					\
2268 		le32_to_cpu(data->num_of_gtk_rekeys);			\
2269 	status->received_beacons = le32_to_cpu(data->received_beacons);	\
2270 	status->wake_packet_length =					\
2271 		le32_to_cpu(data->wake_packet_length);			\
2272 	status->wake_packet_bufsize =					\
2273 		le32_to_cpu(data->wake_packet_bufsize);			\
2274 	if (status->wake_packet_bufsize) {				\
2275 		status->wake_packet =					\
2276 			kmemdup(data->wake_packet,			\
2277 				status->wake_packet_bufsize,		\
2278 				GFP_KERNEL);				\
2279 		if (!status->wake_packet) {				\
2280 			kfree(status);					\
2281 			return NULL;					\
2282 		}							\
2283 	} else {							\
2284 		status->wake_packet = NULL;				\
2285 	}								\
2286 									\
2287 	return status;							\
2288 }
2289 
2290 iwl_mvm_parse_wowlan_status_common(v6)
2291 iwl_mvm_parse_wowlan_status_common(v7)
2292 iwl_mvm_parse_wowlan_status_common(v9)
2293 iwl_mvm_parse_wowlan_status_common(v12)
2294 
2295 static struct iwl_wowlan_status_data *
2296 iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id)
2297 {
2298 	struct iwl_wowlan_status_data *status;
2299 	struct iwl_wowlan_get_status_cmd get_status_cmd = {
2300 		.sta_id = cpu_to_le32(sta_id),
2301 	};
2302 	struct iwl_host_cmd cmd = {
2303 		.id = WOWLAN_GET_STATUSES,
2304 		.flags = CMD_WANT_SKB,
2305 		.data = { &get_status_cmd, },
2306 		.len = { sizeof(get_status_cmd), },
2307 	};
2308 	int ret, len;
2309 	u8 notif_ver;
2310 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
2311 					   IWL_FW_CMD_VER_UNKNOWN);
2312 
2313 	if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN)
2314 		cmd.len[0] = 0;
2315 
2316 	lockdep_assert_held(&mvm->mutex);
2317 
2318 	ret = iwl_mvm_send_cmd(mvm, &cmd);
2319 	if (ret) {
2320 		IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
2321 		return ERR_PTR(ret);
2322 	}
2323 
2324 	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2325 
2326 	/* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2327 	notif_ver = iwl_mvm_lookup_wowlan_status_ver(mvm);
2328 
2329 	if (notif_ver < 7) {
2330 		struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
2331 
2332 		status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len);
2333 		if (!status)
2334 			goto out_free_resp;
2335 
2336 		BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
2337 			     sizeof(status->gtk[0].key));
2338 		BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2339 			     sizeof(v6->gtk.tkip_mic_key) >
2340 			     sizeof(status->gtk[0].key));
2341 
2342 		/* copy GTK info to the right place */
2343 		memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
2344 		       sizeof(v6->gtk.decrypt_key));
2345 		memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2346 		       v6->gtk.tkip_mic_key,
2347 		       sizeof(v6->gtk.tkip_mic_key));
2348 
2349 		iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc);
2350 
2351 		/* hardcode the key length to 16 since v6 only supports 16 */
2352 		status->gtk[0].len = 16;
2353 
2354 		/*
2355 		 * The key index only uses 2 bits (values 0 to 3) and
2356 		 * we always set bit 7 which means this is the
2357 		 * currently used key.
2358 		 */
2359 		status->gtk[0].flags = v6->gtk.key_index | BIT(7);
2360 	} else if (notif_ver == 7) {
2361 		struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
2362 
2363 		status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len);
2364 		if (!status)
2365 			goto out_free_resp;
2366 
2367 		iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc);
2368 		iwl_mvm_convert_gtk_v2(status, &v7->gtk[0]);
2369 		iwl_mvm_convert_igtk(status, &v7->igtk[0]);
2370 	} else if (notif_ver == 9 || notif_ver == 10 || notif_ver == 11) {
2371 		struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data;
2372 
2373 		/* these three command versions have same layout and size, the
2374 		 * difference is only in a few not used (reserved) fields.
2375 		 */
2376 		status = iwl_mvm_parse_wowlan_status_common_v9(mvm, v9, len);
2377 		if (!status)
2378 			goto out_free_resp;
2379 
2380 		iwl_mvm_convert_key_counters(status, &v9->gtk[0].rsc.all_tsc_rsc);
2381 		iwl_mvm_convert_gtk_v2(status, &v9->gtk[0]);
2382 		iwl_mvm_convert_igtk(status, &v9->igtk[0]);
2383 
2384 		status->tid_tear_down = v9->tid_tear_down;
2385 	} else if (notif_ver == 12) {
2386 		struct iwl_wowlan_status_v12 *v12 = (void *)cmd.resp_pkt->data;
2387 
2388 		status = iwl_mvm_parse_wowlan_status_common_v12(mvm, v12, len);
2389 		if (!status)
2390 			goto out_free_resp;
2391 
2392 		iwl_mvm_convert_key_counters_v5(status, &v12->gtk[0].sc);
2393 		iwl_mvm_convert_gtk_v3(status, v12->gtk);
2394 		iwl_mvm_convert_igtk(status, &v12->igtk[0]);
2395 
2396 		status->tid_tear_down = v12->tid_tear_down;
2397 	} else {
2398 		IWL_ERR(mvm,
2399 			"Firmware advertises unknown WoWLAN status response %d!\n",
2400 			notif_ver);
2401 		status = NULL;
2402 	}
2403 
2404 out_free_resp:
2405 	iwl_free_resp(&cmd);
2406 	return status;
2407 }
2408 
2409 /* releases the MVM mutex */
2410 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
2411 					 struct ieee80211_vif *vif,
2412 					 struct iwl_wowlan_status_data *status)
2413 {
2414 	int i;
2415 	bool keep;
2416 	struct iwl_mvm_sta *mvm_ap_sta;
2417 
2418 	if (!status)
2419 		goto out_unlock;
2420 
2421 	IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
2422 			 status->wakeup_reasons);
2423 
2424 	/* still at hard-coded place 0 for D3 image */
2425 	mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
2426 	if (!mvm_ap_sta)
2427 		goto out_unlock;
2428 
2429 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
2430 		u16 seq = status->qos_seq_ctr[i];
2431 		/* firmware stores last-used value, we store next value */
2432 		seq += 0x10;
2433 		mvm_ap_sta->tid_data[i].seq_number = seq;
2434 	}
2435 
2436 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
2437 		i = mvm->offload_tid;
2438 		iwl_trans_set_q_ptrs(mvm->trans,
2439 				     mvm_ap_sta->tid_data[i].txq_id,
2440 				     mvm_ap_sta->tid_data[i].seq_number >> 4);
2441 	}
2442 
2443 	/* now we have all the data we need, unlock to avoid mac80211 issues */
2444 	mutex_unlock(&mvm->mutex);
2445 
2446 	iwl_mvm_report_wakeup_reasons(mvm, vif, status);
2447 
2448 	keep = iwl_mvm_setup_connection_keep(mvm, vif, status);
2449 
2450 	return keep;
2451 
2452 out_unlock:
2453 	mutex_unlock(&mvm->mutex);
2454 	return false;
2455 }
2456 
2457 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
2458 			  IWL_SCAN_MAX_PROFILES)
2459 
2460 struct iwl_mvm_nd_results {
2461 	u32 matched_profiles;
2462 	u8 matches[ND_QUERY_BUF_LEN];
2463 };
2464 
2465 static int
2466 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
2467 				struct iwl_mvm_nd_results *results)
2468 {
2469 	struct iwl_scan_offload_match_info *query;
2470 	struct iwl_host_cmd cmd = {
2471 		.id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
2472 		.flags = CMD_WANT_SKB,
2473 	};
2474 	int ret, len;
2475 	size_t query_len, matches_len;
2476 	int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
2477 
2478 	ret = iwl_mvm_send_cmd(mvm, &cmd);
2479 	if (ret) {
2480 		IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
2481 		return ret;
2482 	}
2483 
2484 	if (fw_has_api(&mvm->fw->ucode_capa,
2485 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2486 		query_len = sizeof(struct iwl_scan_offload_match_info);
2487 		matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2488 			max_profiles;
2489 	} else {
2490 		query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
2491 		matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
2492 			max_profiles;
2493 	}
2494 
2495 	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2496 	if (len < query_len) {
2497 		IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
2498 		ret = -EIO;
2499 		goto out_free_resp;
2500 	}
2501 
2502 	query = (void *)cmd.resp_pkt->data;
2503 
2504 	results->matched_profiles = le32_to_cpu(query->matched_profiles);
2505 	memcpy(results->matches, query->matches, matches_len);
2506 
2507 #ifdef CONFIG_IWLWIFI_DEBUGFS
2508 	mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
2509 #endif
2510 
2511 out_free_resp:
2512 	iwl_free_resp(&cmd);
2513 	return ret;
2514 }
2515 
2516 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
2517 					 struct iwl_mvm_nd_results *results,
2518 					 int idx)
2519 {
2520 	int n_chans = 0, i;
2521 
2522 	if (fw_has_api(&mvm->fw->ucode_capa,
2523 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2524 		struct iwl_scan_offload_profile_match *matches =
2525 			(void *)results->matches;
2526 
2527 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
2528 			n_chans += hweight8(matches[idx].matching_channels[i]);
2529 	} else {
2530 		struct iwl_scan_offload_profile_match_v1 *matches =
2531 			(void *)results->matches;
2532 
2533 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
2534 			n_chans += hweight8(matches[idx].matching_channels[i]);
2535 	}
2536 
2537 	return n_chans;
2538 }
2539 
2540 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
2541 				    struct iwl_mvm_nd_results *results,
2542 				    struct cfg80211_wowlan_nd_match *match,
2543 				    int idx)
2544 {
2545 	int i;
2546 
2547 	if (fw_has_api(&mvm->fw->ucode_capa,
2548 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2549 		struct iwl_scan_offload_profile_match *matches =
2550 			 (void *)results->matches;
2551 
2552 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
2553 			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2554 				match->channels[match->n_channels++] =
2555 					mvm->nd_channels[i]->center_freq;
2556 	} else {
2557 		struct iwl_scan_offload_profile_match_v1 *matches =
2558 			 (void *)results->matches;
2559 
2560 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
2561 			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2562 				match->channels[match->n_channels++] =
2563 					mvm->nd_channels[i]->center_freq;
2564 	}
2565 }
2566 
2567 /**
2568  * enum iwl_d3_notif - d3 notifications
2569  * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF was received
2570  * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF was received
2571  * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF was received
2572  * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF was received
2573  * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF was received
2574  */
2575 enum iwl_d3_notif {
2576 	IWL_D3_NOTIF_WOWLAN_INFO =	BIT(0),
2577 	IWL_D3_NOTIF_WOWLAN_WAKE_PKT =	BIT(1),
2578 	IWL_D3_NOTIF_PROT_OFFLOAD =	BIT(2),
2579 	IWL_D3_ND_MATCH_INFO      =     BIT(3),
2580 	IWL_D3_NOTIF_D3_END_NOTIF =	BIT(4)
2581 };
2582 
2583 /* manage d3 resume data */
2584 struct iwl_d3_data {
2585 	struct iwl_wowlan_status_data *status;
2586 	bool test;
2587 	u32 d3_end_flags;
2588 	u32 notif_expected;	/* bitmap - see &enum iwl_d3_notif */
2589 	u32 notif_received;	/* bitmap - see &enum iwl_d3_notif */
2590 	struct iwl_mvm_nd_results *nd_results;
2591 	bool nd_results_valid;
2592 };
2593 
2594 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
2595 					    struct ieee80211_vif *vif,
2596 					    struct iwl_d3_data *d3_data)
2597 {
2598 	struct cfg80211_wowlan_nd_info *net_detect = NULL;
2599 	struct cfg80211_wowlan_wakeup wakeup = {
2600 		.pattern_idx = -1,
2601 	};
2602 	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
2603 	unsigned long matched_profiles;
2604 	u32 reasons = 0;
2605 	int i, n_matches, ret;
2606 
2607 	if (WARN_ON(!d3_data || !d3_data->status))
2608 		goto out;
2609 
2610 	reasons = d3_data->status->wakeup_reasons;
2611 
2612 	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
2613 		wakeup.rfkill_release = true;
2614 
2615 	if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
2616 		goto out;
2617 
2618 	if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2619 				     WOWLAN_INFO_NOTIFICATION, 0)) {
2620 		IWL_INFO(mvm, "Query FW for ND results\n");
2621 		ret = iwl_mvm_netdetect_query_results(mvm, d3_data->nd_results);
2622 
2623 	} else {
2624 		IWL_INFO(mvm, "Notification based ND results\n");
2625 		ret = d3_data->nd_results_valid ? 0 : -1;
2626 	}
2627 
2628 	if (ret || !d3_data->nd_results->matched_profiles) {
2629 		wakeup_report = NULL;
2630 		goto out;
2631 	}
2632 
2633 	matched_profiles = d3_data->nd_results->matched_profiles;
2634 	if (mvm->n_nd_match_sets) {
2635 		n_matches = hweight_long(matched_profiles);
2636 	} else {
2637 		IWL_ERR(mvm, "no net detect match information available\n");
2638 		n_matches = 0;
2639 	}
2640 
2641 	net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
2642 			     GFP_KERNEL);
2643 	if (!net_detect || !n_matches)
2644 		goto out_report_nd;
2645 
2646 	for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
2647 		struct cfg80211_wowlan_nd_match *match;
2648 		int idx, n_channels = 0;
2649 
2650 		n_channels = iwl_mvm_query_num_match_chans(mvm,
2651 							   d3_data->nd_results,
2652 							   i);
2653 
2654 		match = kzalloc(struct_size(match, channels, n_channels),
2655 				GFP_KERNEL);
2656 		if (!match)
2657 			goto out_report_nd;
2658 
2659 		net_detect->matches[net_detect->n_matches++] = match;
2660 
2661 		/* We inverted the order of the SSIDs in the scan
2662 		 * request, so invert the index here.
2663 		 */
2664 		idx = mvm->n_nd_match_sets - i - 1;
2665 		match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2666 		memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2667 		       match->ssid.ssid_len);
2668 
2669 		if (mvm->n_nd_channels < n_channels)
2670 			continue;
2671 
2672 		iwl_mvm_query_set_freqs(mvm, d3_data->nd_results, match, i);
2673 	}
2674 
2675 out_report_nd:
2676 	wakeup.net_detect = net_detect;
2677 out:
2678 	iwl_mvm_free_nd(mvm);
2679 
2680 	mutex_unlock(&mvm->mutex);
2681 	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2682 
2683 	if (net_detect) {
2684 		for (i = 0; i < net_detect->n_matches; i++)
2685 			kfree(net_detect->matches[i]);
2686 		kfree(net_detect);
2687 	}
2688 }
2689 
2690 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2691 				       struct ieee80211_vif *vif)
2692 {
2693 	/* skip the one we keep connection on */
2694 	if (data == vif)
2695 		return;
2696 
2697 	if (vif->type == NL80211_IFTYPE_STATION)
2698 		ieee80211_resume_disconnect(vif);
2699 }
2700 
2701 static bool iwl_mvm_rt_status(struct iwl_trans *trans, u32 base, u32 *err_id)
2702 {
2703 	struct error_table_start {
2704 		/* cf. struct iwl_error_event_table */
2705 		u32 valid;
2706 		__le32 err_id;
2707 	} err_info;
2708 
2709 	if (!base)
2710 		return false;
2711 
2712 	iwl_trans_read_mem_bytes(trans, base,
2713 				 &err_info, sizeof(err_info));
2714 	if (err_info.valid && err_id)
2715 		*err_id = le32_to_cpu(err_info.err_id);
2716 
2717 	return !!err_info.valid;
2718 }
2719 
2720 static bool iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
2721 				   struct ieee80211_vif *vif)
2722 {
2723 	u32 err_id;
2724 
2725 	/* check for lmac1 error */
2726 	if (iwl_mvm_rt_status(mvm->trans,
2727 			      mvm->trans->dbg.lmac_error_event_table[0],
2728 			      &err_id)) {
2729 		if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
2730 			struct cfg80211_wowlan_wakeup wakeup = {
2731 				.rfkill_release = true,
2732 			};
2733 			ieee80211_report_wowlan_wakeup(vif, &wakeup,
2734 						       GFP_KERNEL);
2735 		}
2736 		return true;
2737 	}
2738 
2739 	/* check if we have lmac2 set and check for error */
2740 	if (iwl_mvm_rt_status(mvm->trans,
2741 			      mvm->trans->dbg.lmac_error_event_table[1], NULL))
2742 		return true;
2743 
2744 	/* check for umac error */
2745 	if (iwl_mvm_rt_status(mvm->trans,
2746 			      mvm->trans->dbg.umac_error_event_table, NULL))
2747 		return true;
2748 
2749 	return false;
2750 }
2751 
2752 /*
2753  * This function assumes:
2754  *	1. The mutex is already held.
2755  *	2. The callee functions unlock the mutex.
2756  */
2757 static bool
2758 iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm *mvm,
2759 				    struct ieee80211_vif *vif,
2760 				    struct iwl_d3_data *d3_data)
2761 {
2762 	lockdep_assert_held(&mvm->mutex);
2763 
2764 	/* if FW uses status notification, status shouldn't be NULL here */
2765 	if (!d3_data->status) {
2766 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2767 		u8 sta_id = mvm->net_detect ? IWL_MVM_INVALID_STA :
2768 					      mvmvif->deflink.ap_sta_id;
2769 
2770 		d3_data->status = iwl_mvm_send_wowlan_get_status(mvm, sta_id);
2771 	}
2772 
2773 	if (mvm->net_detect) {
2774 		iwl_mvm_query_netdetect_reasons(mvm, vif, d3_data);
2775 	} else {
2776 		bool keep = iwl_mvm_query_wakeup_reasons(mvm, vif,
2777 							 d3_data->status);
2778 
2779 #ifdef CONFIG_IWLWIFI_DEBUGFS
2780 		if (keep)
2781 			mvm->keep_vif = vif;
2782 #endif
2783 
2784 		return keep;
2785 	}
2786 	return false;
2787 }
2788 
2789 #define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT (IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \
2790 						 IWL_WOWLAN_WAKEUP_BY_PATTERN | \
2791 						 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\
2792 						 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\
2793 						 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\
2794 						 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD)
2795 
2796 static int iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm *mvm,
2797 					 struct iwl_wowlan_wake_pkt_notif *notif,
2798 					 struct iwl_wowlan_status_data *status,
2799 					 u32 len)
2800 {
2801 	u32 data_size, packet_len = le32_to_cpu(notif->wake_packet_length);
2802 
2803 	if (len < sizeof(*notif)) {
2804 		IWL_ERR(mvm, "Invalid WoWLAN wake packet notification!\n");
2805 		return -EIO;
2806 	}
2807 
2808 	if (WARN_ON(!status)) {
2809 		IWL_ERR(mvm, "Got wake packet notification but wowlan status data is NULL\n");
2810 		return -EIO;
2811 	}
2812 
2813 	if (WARN_ON(!(status->wakeup_reasons &
2814 		      IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT))) {
2815 		IWL_ERR(mvm, "Got wakeup packet but wakeup reason is %x\n",
2816 			status->wakeup_reasons);
2817 		return -EIO;
2818 	}
2819 
2820 	data_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif, wake_packet);
2821 
2822 	/* data_size got the padding from the notification, remove it. */
2823 	if (packet_len < data_size)
2824 		data_size = packet_len;
2825 
2826 	status->wake_packet = kmemdup(notif->wake_packet, data_size,
2827 				      GFP_ATOMIC);
2828 
2829 	if (!status->wake_packet)
2830 		return -ENOMEM;
2831 
2832 	status->wake_packet_length = packet_len;
2833 	status->wake_packet_bufsize = data_size;
2834 
2835 	return 0;
2836 }
2837 
2838 static void iwl_mvm_nd_match_info_handler(struct iwl_mvm *mvm,
2839 					  struct iwl_d3_data *d3_data,
2840 					  struct iwl_scan_offload_match_info *notif,
2841 					  u32 len)
2842 {
2843 	struct iwl_wowlan_status_data *status = d3_data->status;
2844 	struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
2845 	struct iwl_mvm_nd_results *results = d3_data->nd_results;
2846 	size_t i, matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2847 		iwl_umac_scan_get_max_profiles(mvm->fw);
2848 
2849 	if (IS_ERR_OR_NULL(vif))
2850 		return;
2851 
2852 	if (len < sizeof(struct iwl_scan_offload_match_info)) {
2853 		IWL_ERR(mvm, "Invalid scan match info notification\n");
2854 		return;
2855 	}
2856 
2857 	if (!mvm->net_detect) {
2858 		IWL_ERR(mvm, "Unexpected scan match info notification\n");
2859 		return;
2860 	}
2861 
2862 	if (!status || status->wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
2863 		IWL_ERR(mvm,
2864 			"Ignore scan match info notification: no reason\n");
2865 		return;
2866 	}
2867 
2868 #ifdef CONFIG_IWLWIFI_DEBUGFS
2869 	mvm->last_netdetect_scans = le32_to_cpu(notif->n_scans_done);
2870 #endif
2871 
2872 	results->matched_profiles = le32_to_cpu(notif->matched_profiles);
2873 	IWL_INFO(mvm, "number of matched profiles=%u\n",
2874 		 results->matched_profiles);
2875 
2876 	if (results->matched_profiles) {
2877 		memcpy(results->matches, notif->matches, matches_len);
2878 		d3_data->nd_results_valid = TRUE;
2879 	}
2880 
2881 	/* no scan should be active at this point */
2882 	mvm->scan_status = 0;
2883 	for (i = 0; i < mvm->max_scans; i++)
2884 		mvm->scan_uid_status[i] = 0;
2885 }
2886 
2887 static bool iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data *notif_wait,
2888 				  struct iwl_rx_packet *pkt, void *data)
2889 {
2890 	struct iwl_mvm *mvm =
2891 		container_of(notif_wait, struct iwl_mvm, notif_wait);
2892 	struct iwl_d3_data *d3_data = data;
2893 	u32 len;
2894 	int ret;
2895 	int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
2896 						      PROT_OFFLOAD_GROUP,
2897 						      WOWLAN_INFO_NOTIFICATION,
2898 						      IWL_FW_CMD_VER_UNKNOWN);
2899 
2900 
2901 	switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
2902 	case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): {
2903 		struct iwl_wowlan_info_notif *notif;
2904 
2905 		if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_INFO) {
2906 			/* We might get two notifications due to dual bss */
2907 			IWL_DEBUG_WOWLAN(mvm,
2908 					 "Got additional wowlan info notification\n");
2909 			break;
2910 		}
2911 
2912 		if (wowlan_info_ver < 2) {
2913 			struct iwl_wowlan_info_notif_v1 *notif_v1 = (void *)pkt->data;
2914 
2915 			notif = kmemdup(notif_v1, sizeof(*notif), GFP_ATOMIC);
2916 			if (!notif)
2917 				return false;
2918 
2919 			notif->tid_tear_down = notif_v1->tid_tear_down;
2920 			notif->station_id = notif_v1->station_id;
2921 			memset_after(notif, 0, station_id);
2922 		} else {
2923 			notif = (void *)pkt->data;
2924 		}
2925 
2926 		d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_INFO;
2927 		len = iwl_rx_packet_payload_len(pkt);
2928 		iwl_mvm_parse_wowlan_info_notif(mvm, notif, d3_data->status,
2929 						len);
2930 
2931 		if (wowlan_info_ver < 2)
2932 			kfree(notif);
2933 
2934 		if (d3_data->status &&
2935 		    d3_data->status->wakeup_reasons & IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT)
2936 			/* We are supposed to get also wake packet notif */
2937 			d3_data->notif_expected |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
2938 
2939 		break;
2940 	}
2941 	case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): {
2942 		struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data;
2943 
2944 		if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_WAKE_PKT) {
2945 			/* We shouldn't get two wake packet notifications */
2946 			IWL_ERR(mvm,
2947 				"Got additional wowlan wake packet notification\n");
2948 		} else {
2949 			d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
2950 			len =  iwl_rx_packet_payload_len(pkt);
2951 			ret = iwl_mvm_wowlan_store_wake_pkt(mvm, notif,
2952 							    d3_data->status,
2953 							    len);
2954 			if (ret)
2955 				IWL_ERR(mvm,
2956 					"Can't parse WOWLAN_WAKE_PKT_NOTIFICATION\n");
2957 		}
2958 
2959 		break;
2960 	}
2961 	case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): {
2962 		struct iwl_scan_offload_match_info *notif = (void *)pkt->data;
2963 
2964 		if (d3_data->notif_received & IWL_D3_ND_MATCH_INFO) {
2965 			IWL_ERR(mvm,
2966 				"Got additional netdetect match info\n");
2967 			break;
2968 		}
2969 
2970 		d3_data->notif_received |= IWL_D3_ND_MATCH_INFO;
2971 
2972 		/* explicitly set this in the 'expected' as well */
2973 		d3_data->notif_expected |= IWL_D3_ND_MATCH_INFO;
2974 
2975 		len = iwl_rx_packet_payload_len(pkt);
2976 		iwl_mvm_nd_match_info_handler(mvm, d3_data, notif, len);
2977 		break;
2978 	}
2979 	case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): {
2980 		struct iwl_mvm_d3_end_notif *notif = (void *)pkt->data;
2981 
2982 		d3_data->d3_end_flags = __le32_to_cpu(notif->flags);
2983 		d3_data->notif_received |= IWL_D3_NOTIF_D3_END_NOTIF;
2984 
2985 		break;
2986 	}
2987 	default:
2988 		WARN_ON(1);
2989 	}
2990 
2991 	return d3_data->notif_received == d3_data->notif_expected;
2992 }
2993 
2994 static int iwl_mvm_resume_firmware(struct iwl_mvm *mvm, bool test)
2995 {
2996 	int ret;
2997 	enum iwl_d3_status d3_status;
2998 	struct iwl_host_cmd cmd = {
2999 			.id = D0I3_END_CMD,
3000 			.flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
3001 		};
3002 	bool reset = fw_has_capa(&mvm->fw->ucode_capa,
3003 				 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3004 
3005 	ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !reset);
3006 	if (ret)
3007 		return ret;
3008 
3009 	if (d3_status != IWL_D3_STATUS_ALIVE) {
3010 		IWL_INFO(mvm, "Device was reset during suspend\n");
3011 		return -ENOENT;
3012 	}
3013 
3014 	/*
3015 	 * We should trigger resume flow using command only for 22000 family
3016 	 * AX210 and above don't need the command since they have
3017 	 * the doorbell interrupt.
3018 	 */
3019 	if (mvm->trans->trans_cfg->device_family <= IWL_DEVICE_FAMILY_22000 &&
3020 	    fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_D0I3_END_FIRST)) {
3021 		ret = iwl_mvm_send_cmd(mvm, &cmd);
3022 		if (ret < 0)
3023 			IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
3024 				ret);
3025 	}
3026 
3027 	return ret;
3028 }
3029 
3030 #define IWL_MVM_D3_NOTIF_TIMEOUT (HZ / 5)
3031 
3032 static int iwl_mvm_d3_notif_wait(struct iwl_mvm *mvm,
3033 				 struct iwl_d3_data *d3_data)
3034 {
3035 	static const u16 d3_resume_notif[] = {
3036 		WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION),
3037 		WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION),
3038 		WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF),
3039 		WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3040 	};
3041 	struct iwl_notification_wait wait_d3_notif;
3042 	int ret;
3043 
3044 	iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3045 				   d3_resume_notif, ARRAY_SIZE(d3_resume_notif),
3046 				   iwl_mvm_wait_d3_notif, d3_data);
3047 
3048 	ret = iwl_mvm_resume_firmware(mvm, d3_data->test);
3049 	if (ret) {
3050 		iwl_remove_notification(&mvm->notif_wait, &wait_d3_notif);
3051 		return ret;
3052 	}
3053 
3054 	return iwl_wait_notification(&mvm->notif_wait, &wait_d3_notif,
3055 				     IWL_MVM_D3_NOTIF_TIMEOUT);
3056 }
3057 
3058 static inline bool iwl_mvm_d3_resume_notif_based(struct iwl_mvm *mvm)
3059 {
3060 	return iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3061 				       WOWLAN_INFO_NOTIFICATION, 0) &&
3062 		iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3063 					WOWLAN_WAKE_PKT_NOTIFICATION, 0) &&
3064 		iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3065 					D3_END_NOTIFICATION, 0);
3066 }
3067 
3068 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
3069 {
3070 	struct ieee80211_vif *vif = NULL;
3071 	int ret = 1;
3072 	struct iwl_mvm_nd_results results = {};
3073 	struct iwl_d3_data d3_data = {
3074 		.test = test,
3075 		.notif_expected =
3076 			IWL_D3_NOTIF_WOWLAN_INFO |
3077 			IWL_D3_NOTIF_D3_END_NOTIF,
3078 		.nd_results_valid = false,
3079 		.nd_results = &results,
3080 	};
3081 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3082 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3083 	bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
3084 				      IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
3085 	bool resume_notif_based = iwl_mvm_d3_resume_notif_based(mvm);
3086 	bool keep = false;
3087 
3088 	mutex_lock(&mvm->mutex);
3089 
3090 	mvm->last_reset_or_resume_time_jiffies = jiffies;
3091 
3092 	/* get the BSS vif pointer again */
3093 	vif = iwl_mvm_get_bss_vif(mvm);
3094 	if (IS_ERR_OR_NULL(vif))
3095 		goto err;
3096 
3097 	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3098 
3099 	if (iwl_mvm_check_rt_status(mvm, vif)) {
3100 		set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3101 		iwl_mvm_dump_nic_error_log(mvm);
3102 		iwl_dbg_tlv_time_point(&mvm->fwrt,
3103 				       IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL);
3104 		iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
3105 					false, 0);
3106 		ret = 1;
3107 		goto err;
3108 	}
3109 
3110 	if (resume_notif_based) {
3111 		d3_data.status = kzalloc(sizeof(*d3_data.status), GFP_KERNEL);
3112 		if (!d3_data.status) {
3113 			IWL_ERR(mvm, "Failed to allocate wowlan status\n");
3114 			ret = -ENOMEM;
3115 			goto err;
3116 		}
3117 
3118 		ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3119 		if (ret)
3120 			goto err;
3121 	} else {
3122 		ret = iwl_mvm_resume_firmware(mvm, test);
3123 		if (ret < 0)
3124 			goto err;
3125 	}
3126 
3127 	/* after the successful handshake, we're out of D3 */
3128 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3129 
3130 	/* when reset is required we can't send these following commands */
3131 	if (d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)
3132 		goto query_wakeup_reasons;
3133 
3134 	/*
3135 	 * Query the current location and source from the D3 firmware so we
3136 	 * can play it back when we re-intiailize the D0 firmware
3137 	 */
3138 	iwl_mvm_update_changed_regdom(mvm);
3139 
3140 	/* Re-configure PPAG settings */
3141 	iwl_mvm_ppag_send_cmd(mvm);
3142 
3143 	if (!unified_image)
3144 		/*  Re-configure default SAR profile */
3145 		iwl_mvm_sar_select_profile(mvm, 1, 1);
3146 
3147 	if (mvm->net_detect && unified_image) {
3148 		/* If this is a non-unified image, we restart the FW,
3149 		 * so no need to stop the netdetect scan.  If that
3150 		 * fails, continue and try to get the wake-up reasons,
3151 		 * but trigger a HW restart by keeping a failure code
3152 		 * in ret.
3153 		 */
3154 		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
3155 					false);
3156 	}
3157 
3158 query_wakeup_reasons:
3159 	keep = iwl_mvm_choose_query_wakeup_reasons(mvm, vif, &d3_data);
3160 	/* has unlocked the mutex, so skip that */
3161 	goto out;
3162 
3163 err:
3164 	mutex_unlock(&mvm->mutex);
3165 out:
3166 	if (d3_data.status)
3167 		kfree(d3_data.status->wake_packet);
3168 	kfree(d3_data.status);
3169 	iwl_mvm_free_nd(mvm);
3170 
3171 	if (!d3_data.test && !mvm->net_detect)
3172 		ieee80211_iterate_active_interfaces_mtx(mvm->hw,
3173 							IEEE80211_IFACE_ITER_NORMAL,
3174 							iwl_mvm_d3_disconnect_iter,
3175 							keep ? vif : NULL);
3176 
3177 	clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3178 
3179 	/* no need to reset the device in unified images, if successful */
3180 	if (unified_image && !ret) {
3181 		/* nothing else to do if we already sent D0I3_END_CMD */
3182 		if (d0i3_first)
3183 			return 0;
3184 
3185 		if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3186 					     D3_END_NOTIFICATION, 0)) {
3187 			ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
3188 			if (!ret)
3189 				return 0;
3190 		} else if (!(d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)) {
3191 			return 0;
3192 		}
3193 	}
3194 
3195 	/*
3196 	 * Reconfigure the device in one of the following cases:
3197 	 * 1. We are not using a unified image
3198 	 * 2. We are using a unified image but had an error while exiting D3
3199 	 */
3200 	set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
3201 
3202 	/* regardless of what happened, we're now out of D3 */
3203 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3204 
3205 	return 1;
3206 }
3207 
3208 int iwl_mvm_resume(struct ieee80211_hw *hw)
3209 {
3210 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3211 	int ret;
3212 
3213 	ret = __iwl_mvm_resume(mvm, false);
3214 
3215 	iwl_mvm_resume_tcm(mvm);
3216 
3217 	iwl_fw_runtime_resume(&mvm->fwrt);
3218 
3219 	return ret;
3220 }
3221 
3222 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
3223 {
3224 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3225 
3226 	device_set_wakeup_enable(mvm->trans->dev, enabled);
3227 }
3228 
3229 #ifdef CONFIG_IWLWIFI_DEBUGFS
3230 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
3231 {
3232 	struct iwl_mvm *mvm = inode->i_private;
3233 	int err;
3234 
3235 	if (mvm->d3_test_active)
3236 		return -EBUSY;
3237 
3238 	file->private_data = inode->i_private;
3239 
3240 	iwl_mvm_pause_tcm(mvm, true);
3241 
3242 	iwl_fw_runtime_suspend(&mvm->fwrt);
3243 
3244 	/* start pseudo D3 */
3245 	rtnl_lock();
3246 	wiphy_lock(mvm->hw->wiphy);
3247 	err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
3248 	wiphy_unlock(mvm->hw->wiphy);
3249 	rtnl_unlock();
3250 	if (err > 0)
3251 		err = -EINVAL;
3252 	if (err)
3253 		return err;
3254 
3255 	mvm->d3_test_active = true;
3256 	mvm->keep_vif = NULL;
3257 	return 0;
3258 }
3259 
3260 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
3261 				    size_t count, loff_t *ppos)
3262 {
3263 	struct iwl_mvm *mvm = file->private_data;
3264 	u32 pme_asserted;
3265 
3266 	while (true) {
3267 		/* read pme_ptr if available */
3268 		if (mvm->d3_test_pme_ptr) {
3269 			pme_asserted = iwl_trans_read_mem32(mvm->trans,
3270 						mvm->d3_test_pme_ptr);
3271 			if (pme_asserted)
3272 				break;
3273 		}
3274 
3275 		if (msleep_interruptible(100))
3276 			break;
3277 	}
3278 
3279 	return 0;
3280 }
3281 
3282 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
3283 					      struct ieee80211_vif *vif)
3284 {
3285 	/* skip the one we keep connection on */
3286 	if (_data == vif)
3287 		return;
3288 
3289 	if (vif->type == NL80211_IFTYPE_STATION)
3290 		ieee80211_connection_loss(vif);
3291 }
3292 
3293 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
3294 {
3295 	struct iwl_mvm *mvm = inode->i_private;
3296 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3297 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3298 
3299 	mvm->d3_test_active = false;
3300 
3301 	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3302 
3303 	rtnl_lock();
3304 	wiphy_lock(mvm->hw->wiphy);
3305 	__iwl_mvm_resume(mvm, true);
3306 	wiphy_unlock(mvm->hw->wiphy);
3307 	rtnl_unlock();
3308 
3309 	iwl_mvm_resume_tcm(mvm);
3310 
3311 	iwl_fw_runtime_resume(&mvm->fwrt);
3312 
3313 	iwl_abort_notification_waits(&mvm->notif_wait);
3314 	if (!unified_image) {
3315 		int remaining_time = 10;
3316 
3317 		ieee80211_restart_hw(mvm->hw);
3318 
3319 		/* wait for restart and disconnect all interfaces */
3320 		while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3321 		       remaining_time > 0) {
3322 			remaining_time--;
3323 			msleep(1000);
3324 		}
3325 
3326 		if (remaining_time == 0)
3327 			IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
3328 	}
3329 
3330 	ieee80211_iterate_active_interfaces_atomic(
3331 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
3332 		iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
3333 
3334 	return 0;
3335 }
3336 
3337 const struct file_operations iwl_dbgfs_d3_test_ops = {
3338 	.llseek = no_llseek,
3339 	.open = iwl_mvm_d3_test_open,
3340 	.read = iwl_mvm_d3_test_read,
3341 	.release = iwl_mvm_d3_test_release,
3342 };
3343 #endif
3344