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