xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/d3.c (revision 44f57d78)
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11  * Copyright(c) 2018 - 2019 Intel Corporation
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of version 2 of the GNU General Public License as
15  * published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <linuxwifi@intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
31  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34  * Copyright(c) 2018 - 2019 Intel Corporation
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
41  *  * Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  *  * Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in
45  *    the documentation and/or other materials provided with the
46  *    distribution.
47  *  * Neither the name Intel Corporation nor the names of its
48  *    contributors may be used to endorse or promote products derived
49  *    from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *****************************************************************************/
64 
65 #include <linux/etherdevice.h>
66 #include <linux/ip.h>
67 #include <linux/fs.h>
68 #include <net/cfg80211.h>
69 #include <net/ipv6.h>
70 #include <net/tcp.h>
71 #include <net/addrconf.h>
72 #include "iwl-modparams.h"
73 #include "fw-api.h"
74 #include "mvm.h"
75 
76 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
77 			    struct ieee80211_vif *vif,
78 			    struct cfg80211_gtk_rekey_data *data)
79 {
80 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
81 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
82 
83 	if (iwlwifi_mod_params.swcrypto)
84 		return;
85 
86 	mutex_lock(&mvm->mutex);
87 
88 	memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
89 	memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
90 	mvmvif->rekey_data.replay_ctr =
91 		cpu_to_le64(be64_to_cpup((__be64 *)data->replay_ctr));
92 	mvmvif->rekey_data.valid = true;
93 
94 	mutex_unlock(&mvm->mutex);
95 }
96 
97 #if IS_ENABLED(CONFIG_IPV6)
98 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
99 			      struct ieee80211_vif *vif,
100 			      struct inet6_dev *idev)
101 {
102 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
103 	struct inet6_ifaddr *ifa;
104 	int idx = 0;
105 
106 	memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
107 
108 	read_lock_bh(&idev->lock);
109 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
110 		mvmvif->target_ipv6_addrs[idx] = ifa->addr;
111 		if (ifa->flags & IFA_F_TENTATIVE)
112 			__set_bit(idx, mvmvif->tentative_addrs);
113 		idx++;
114 		if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
115 			break;
116 	}
117 	read_unlock_bh(&idev->lock);
118 
119 	mvmvif->num_target_ipv6_addrs = idx;
120 }
121 #endif
122 
123 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
124 				     struct ieee80211_vif *vif, int idx)
125 {
126 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
127 
128 	mvmvif->tx_key_idx = idx;
129 }
130 
131 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
132 {
133 	int i;
134 
135 	for (i = 0; i < IWL_P1K_SIZE; i++)
136 		out[i] = cpu_to_le16(p1k[i]);
137 }
138 
139 static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
140 				     struct iwl_mvm_key_pn *ptk_pn,
141 				     struct ieee80211_key_seq *seq,
142 				     int tid, int queues)
143 {
144 	const u8 *ret = seq->ccmp.pn;
145 	int i;
146 
147 	/* get the PN from mac80211, used on the default queue */
148 	ieee80211_get_key_rx_seq(key, tid, seq);
149 
150 	/* and use the internal data for the other queues */
151 	for (i = 1; i < queues; i++) {
152 		const u8 *tmp = ptk_pn->q[i].pn[tid];
153 
154 		if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
155 			ret = tmp;
156 	}
157 
158 	return ret;
159 }
160 
161 struct wowlan_key_data {
162 	struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
163 	struct iwl_wowlan_tkip_params_cmd *tkip;
164 	bool error, use_rsc_tsc, use_tkip, configure_keys;
165 	int wep_key_idx;
166 };
167 
168 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
169 					struct ieee80211_vif *vif,
170 					struct ieee80211_sta *sta,
171 					struct ieee80211_key_conf *key,
172 					void *_data)
173 {
174 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
175 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
176 	struct wowlan_key_data *data = _data;
177 	struct aes_sc *aes_sc, *aes_tx_sc = NULL;
178 	struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
179 	struct iwl_p1k_cache *rx_p1ks;
180 	u8 *rx_mic_key;
181 	struct ieee80211_key_seq seq;
182 	u32 cur_rx_iv32 = 0;
183 	u16 p1k[IWL_P1K_SIZE];
184 	int ret, i;
185 
186 	switch (key->cipher) {
187 	case WLAN_CIPHER_SUITE_WEP40:
188 	case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
189 		struct {
190 			struct iwl_mvm_wep_key_cmd wep_key_cmd;
191 			struct iwl_mvm_wep_key wep_key;
192 		} __packed wkc = {
193 			.wep_key_cmd.mac_id_n_color =
194 				cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
195 								mvmvif->color)),
196 			.wep_key_cmd.num_keys = 1,
197 			/* firmware sets STA_KEY_FLG_WEP_13BYTES */
198 			.wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
199 			.wep_key.key_index = key->keyidx,
200 			.wep_key.key_size = key->keylen,
201 		};
202 
203 		/*
204 		 * This will fail -- the key functions don't set support
205 		 * pairwise WEP keys. However, that's better than silently
206 		 * failing WoWLAN. Or maybe not?
207 		 */
208 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
209 			break;
210 
211 		memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
212 		if (key->keyidx == mvmvif->tx_key_idx) {
213 			/* TX key must be at offset 0 */
214 			wkc.wep_key.key_offset = 0;
215 		} else {
216 			/* others start at 1 */
217 			data->wep_key_idx++;
218 			wkc.wep_key.key_offset = data->wep_key_idx;
219 		}
220 
221 		if (data->configure_keys) {
222 			mutex_lock(&mvm->mutex);
223 			ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0,
224 						   sizeof(wkc), &wkc);
225 			data->error = ret != 0;
226 
227 			mvm->ptk_ivlen = key->iv_len;
228 			mvm->ptk_icvlen = key->icv_len;
229 			mvm->gtk_ivlen = key->iv_len;
230 			mvm->gtk_icvlen = key->icv_len;
231 			mutex_unlock(&mvm->mutex);
232 		}
233 
234 		/* don't upload key again */
235 		return;
236 	}
237 	default:
238 		data->error = true;
239 		return;
240 	case WLAN_CIPHER_SUITE_AES_CMAC:
241 		/*
242 		 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
243 		 * but we also shouldn't abort suspend due to that. It does have
244 		 * support for the IGTK key renewal, but doesn't really use the
245 		 * IGTK for anything. This means we could spuriously wake up or
246 		 * be deauthenticated, but that was considered acceptable.
247 		 */
248 		return;
249 	case WLAN_CIPHER_SUITE_TKIP:
250 		if (sta) {
251 			u64 pn64;
252 
253 			tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
254 			tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
255 
256 			rx_p1ks = data->tkip->rx_uni;
257 
258 			pn64 = atomic64_read(&key->tx_pn);
259 			tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
260 			tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
261 
262 			ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
263 						  p1k);
264 			iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
265 
266 			memcpy(data->tkip->mic_keys.tx,
267 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
268 			       IWL_MIC_KEY_SIZE);
269 
270 			rx_mic_key = data->tkip->mic_keys.rx_unicast;
271 		} else {
272 			tkip_sc =
273 				data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
274 			rx_p1ks = data->tkip->rx_multi;
275 			rx_mic_key = data->tkip->mic_keys.rx_mcast;
276 		}
277 
278 		/*
279 		 * For non-QoS this relies on the fact that both the uCode and
280 		 * mac80211 use TID 0 (as they need to to avoid replay attacks)
281 		 * for checking the IV in the frames.
282 		 */
283 		for (i = 0; i < IWL_NUM_RSC; i++) {
284 			ieee80211_get_key_rx_seq(key, i, &seq);
285 			tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
286 			tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
287 			/* wrapping isn't allowed, AP must rekey */
288 			if (seq.tkip.iv32 > cur_rx_iv32)
289 				cur_rx_iv32 = seq.tkip.iv32;
290 		}
291 
292 		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
293 					  cur_rx_iv32, p1k);
294 		iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
295 		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
296 					  cur_rx_iv32 + 1, p1k);
297 		iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
298 
299 		memcpy(rx_mic_key,
300 		       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
301 		       IWL_MIC_KEY_SIZE);
302 
303 		data->use_tkip = true;
304 		data->use_rsc_tsc = true;
305 		break;
306 	case WLAN_CIPHER_SUITE_CCMP:
307 		if (sta) {
308 			u64 pn64;
309 
310 			aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
311 			aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
312 
313 			pn64 = atomic64_read(&key->tx_pn);
314 			aes_tx_sc->pn = cpu_to_le64(pn64);
315 		} else {
316 			aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
317 		}
318 
319 		/*
320 		 * For non-QoS this relies on the fact that both the uCode and
321 		 * mac80211/our RX code use TID 0 for checking the PN.
322 		 */
323 		if (sta && iwl_mvm_has_new_rx_api(mvm)) {
324 			struct iwl_mvm_sta *mvmsta;
325 			struct iwl_mvm_key_pn *ptk_pn;
326 			const u8 *pn;
327 
328 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
329 			ptk_pn = rcu_dereference_protected(
330 						mvmsta->ptk_pn[key->keyidx],
331 						lockdep_is_held(&mvm->mutex));
332 			if (WARN_ON(!ptk_pn))
333 				break;
334 
335 			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
336 				pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
337 						mvm->trans->num_rx_queues);
338 				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
339 							   ((u64)pn[4] << 8) |
340 							   ((u64)pn[3] << 16) |
341 							   ((u64)pn[2] << 24) |
342 							   ((u64)pn[1] << 32) |
343 							   ((u64)pn[0] << 40));
344 			}
345 		} else {
346 			for (i = 0; i < IWL_NUM_RSC; i++) {
347 				u8 *pn = seq.ccmp.pn;
348 
349 				ieee80211_get_key_rx_seq(key, i, &seq);
350 				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
351 							   ((u64)pn[4] << 8) |
352 							   ((u64)pn[3] << 16) |
353 							   ((u64)pn[2] << 24) |
354 							   ((u64)pn[1] << 32) |
355 							   ((u64)pn[0] << 40));
356 			}
357 		}
358 		data->use_rsc_tsc = true;
359 		break;
360 	}
361 
362 	if (data->configure_keys) {
363 		mutex_lock(&mvm->mutex);
364 		/*
365 		 * The D3 firmware hardcodes the key offset 0 as the key it
366 		 * uses to transmit packets to the AP, i.e. the PTK.
367 		 */
368 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
369 			mvm->ptk_ivlen = key->iv_len;
370 			mvm->ptk_icvlen = key->icv_len;
371 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
372 		} else {
373 			/*
374 			 * firmware only supports TSC/RSC for a single key,
375 			 * so if there are multiple keep overwriting them
376 			 * with new ones -- this relies on mac80211 doing
377 			 * list_add_tail().
378 			 */
379 			mvm->gtk_ivlen = key->iv_len;
380 			mvm->gtk_icvlen = key->icv_len;
381 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
382 		}
383 		mutex_unlock(&mvm->mutex);
384 		data->error = ret != 0;
385 	}
386 }
387 
388 static int iwl_mvm_send_patterns_v1(struct iwl_mvm *mvm,
389 				    struct cfg80211_wowlan *wowlan)
390 {
391 	struct iwl_wowlan_patterns_cmd_v1 *pattern_cmd;
392 	struct iwl_host_cmd cmd = {
393 		.id = WOWLAN_PATTERNS,
394 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
395 	};
396 	int i, err;
397 
398 	if (!wowlan->n_patterns)
399 		return 0;
400 
401 	cmd.len[0] = sizeof(*pattern_cmd) +
402 		wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v1);
403 
404 	pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
405 	if (!pattern_cmd)
406 		return -ENOMEM;
407 
408 	pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
409 
410 	for (i = 0; i < wowlan->n_patterns; i++) {
411 		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
412 
413 		memcpy(&pattern_cmd->patterns[i].mask,
414 		       wowlan->patterns[i].mask, mask_len);
415 		memcpy(&pattern_cmd->patterns[i].pattern,
416 		       wowlan->patterns[i].pattern,
417 		       wowlan->patterns[i].pattern_len);
418 		pattern_cmd->patterns[i].mask_size = mask_len;
419 		pattern_cmd->patterns[i].pattern_size =
420 			wowlan->patterns[i].pattern_len;
421 	}
422 
423 	cmd.data[0] = pattern_cmd;
424 	err = iwl_mvm_send_cmd(mvm, &cmd);
425 	kfree(pattern_cmd);
426 	return err;
427 }
428 
429 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
430 				 struct cfg80211_wowlan *wowlan)
431 {
432 	struct iwl_wowlan_patterns_cmd *pattern_cmd;
433 	struct iwl_host_cmd cmd = {
434 		.id = WOWLAN_PATTERNS,
435 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
436 	};
437 	int i, err;
438 
439 	if (!wowlan->n_patterns)
440 		return 0;
441 
442 	cmd.len[0] = sizeof(*pattern_cmd) +
443 		wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v2);
444 
445 	pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
446 	if (!pattern_cmd)
447 		return -ENOMEM;
448 
449 	pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
450 
451 	for (i = 0; i < wowlan->n_patterns; i++) {
452 		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
453 
454 		pattern_cmd->patterns[i].pattern_type =
455 			WOWLAN_PATTERN_TYPE_BITMASK;
456 
457 		memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
458 		       wowlan->patterns[i].mask, mask_len);
459 		memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
460 		       wowlan->patterns[i].pattern,
461 		       wowlan->patterns[i].pattern_len);
462 		pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
463 		pattern_cmd->patterns[i].u.bitmask.pattern_size =
464 			wowlan->patterns[i].pattern_len;
465 	}
466 
467 	cmd.data[0] = pattern_cmd;
468 	err = iwl_mvm_send_cmd(mvm, &cmd);
469 	kfree(pattern_cmd);
470 	return err;
471 }
472 
473 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
474 				struct ieee80211_sta *ap_sta)
475 {
476 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
477 	struct ieee80211_chanctx_conf *ctx;
478 	u8 chains_static, chains_dynamic;
479 	struct cfg80211_chan_def chandef;
480 	int ret, i;
481 	struct iwl_binding_cmd_v1 binding_cmd = {};
482 	struct iwl_time_quota_cmd quota_cmd = {};
483 	struct iwl_time_quota_data *quota;
484 	u32 status;
485 
486 	if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm)))
487 		return -EINVAL;
488 
489 	/* add back the PHY */
490 	if (WARN_ON(!mvmvif->phy_ctxt))
491 		return -EINVAL;
492 
493 	rcu_read_lock();
494 	ctx = rcu_dereference(vif->chanctx_conf);
495 	if (WARN_ON(!ctx)) {
496 		rcu_read_unlock();
497 		return -EINVAL;
498 	}
499 	chandef = ctx->def;
500 	chains_static = ctx->rx_chains_static;
501 	chains_dynamic = ctx->rx_chains_dynamic;
502 	rcu_read_unlock();
503 
504 	ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
505 				   chains_static, chains_dynamic);
506 	if (ret)
507 		return ret;
508 
509 	/* add back the MAC */
510 	mvmvif->uploaded = false;
511 
512 	if (WARN_ON(!vif->bss_conf.assoc))
513 		return -EINVAL;
514 
515 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
516 	if (ret)
517 		return ret;
518 
519 	/* add back binding - XXX refactor? */
520 	binding_cmd.id_and_color =
521 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
522 						mvmvif->phy_ctxt->color));
523 	binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
524 	binding_cmd.phy =
525 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
526 						mvmvif->phy_ctxt->color));
527 	binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
528 							      mvmvif->color));
529 	for (i = 1; i < MAX_MACS_IN_BINDING; i++)
530 		binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
531 
532 	status = 0;
533 	ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
534 					  IWL_BINDING_CMD_SIZE_V1, &binding_cmd,
535 					  &status);
536 	if (ret) {
537 		IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
538 		return ret;
539 	}
540 
541 	if (status) {
542 		IWL_ERR(mvm, "Binding command failed: %u\n", status);
543 		return -EIO;
544 	}
545 
546 	ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
547 	if (ret)
548 		return ret;
549 	rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
550 
551 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
552 	if (ret)
553 		return ret;
554 
555 	/* and some quota */
556 	quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, 0);
557 	quota->id_and_color =
558 		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
559 						mvmvif->phy_ctxt->color));
560 	quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
561 	quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
562 
563 	for (i = 1; i < MAX_BINDINGS; i++) {
564 		quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, i);
565 		quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
566 	}
567 
568 	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
569 				   iwl_mvm_quota_cmd_size(mvm), &quota_cmd);
570 	if (ret)
571 		IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
572 
573 	if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
574 		IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
575 
576 	return 0;
577 }
578 
579 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
580 				       struct ieee80211_vif *vif)
581 {
582 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
583 	struct iwl_nonqos_seq_query_cmd query_cmd = {
584 		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
585 		.mac_id_n_color =
586 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
587 							mvmvif->color)),
588 	};
589 	struct iwl_host_cmd cmd = {
590 		.id = NON_QOS_TX_COUNTER_CMD,
591 		.flags = CMD_WANT_SKB,
592 	};
593 	int err;
594 	u32 size;
595 
596 	cmd.data[0] = &query_cmd;
597 	cmd.len[0] = sizeof(query_cmd);
598 
599 	err = iwl_mvm_send_cmd(mvm, &cmd);
600 	if (err)
601 		return err;
602 
603 	size = iwl_rx_packet_payload_len(cmd.resp_pkt);
604 	if (size < sizeof(__le16)) {
605 		err = -EINVAL;
606 	} else {
607 		err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
608 		/* firmware returns next, not last-used seqno */
609 		err = (u16) (err - 0x10);
610 	}
611 
612 	iwl_free_resp(&cmd);
613 	return err;
614 }
615 
616 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
617 {
618 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
619 	struct iwl_nonqos_seq_query_cmd query_cmd = {
620 		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
621 		.mac_id_n_color =
622 			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
623 							mvmvif->color)),
624 		.value = cpu_to_le16(mvmvif->seqno),
625 	};
626 
627 	/* return if called during restart, not resume from D3 */
628 	if (!mvmvif->seqno_valid)
629 		return;
630 
631 	mvmvif->seqno_valid = false;
632 
633 	if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
634 				 sizeof(query_cmd), &query_cmd))
635 		IWL_ERR(mvm, "failed to set non-QoS seqno\n");
636 }
637 
638 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
639 {
640 	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
641 
642 	iwl_mvm_stop_device(mvm);
643 	/*
644 	 * Set the HW restart bit -- this is mostly true as we're
645 	 * going to load new firmware and reprogram that, though
646 	 * the reprogramming is going to be manual to avoid adding
647 	 * all the MACs that aren't support.
648 	 * We don't have to clear up everything though because the
649 	 * reprogramming is manual. When we resume, we'll actually
650 	 * go through a proper restart sequence again to switch
651 	 * back to the runtime firmware image.
652 	 */
653 	set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
654 
655 	/* the fw is reset, so all the keys are cleared */
656 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
657 
658 	mvm->ptk_ivlen = 0;
659 	mvm->ptk_icvlen = 0;
660 	mvm->ptk_ivlen = 0;
661 	mvm->ptk_icvlen = 0;
662 
663 	return iwl_mvm_load_d3_fw(mvm);
664 }
665 
666 static int
667 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
668 			  struct cfg80211_wowlan *wowlan,
669 			  struct iwl_wowlan_config_cmd *wowlan_config_cmd,
670 			  struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
671 			  struct ieee80211_sta *ap_sta)
672 {
673 	int ret;
674 	struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
675 
676 	/* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
677 
678 	wowlan_config_cmd->is_11n_connection =
679 					ap_sta->ht_cap.ht_supported;
680 	wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
681 		ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
682 
683 	/* Query the last used seqno and set it */
684 	ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
685 	if (ret < 0)
686 		return ret;
687 
688 	wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
689 
690 	iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
691 
692 	if (wowlan->disconnect)
693 		wowlan_config_cmd->wakeup_filter |=
694 			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
695 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
696 	if (wowlan->magic_pkt)
697 		wowlan_config_cmd->wakeup_filter |=
698 			cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
699 	if (wowlan->gtk_rekey_failure)
700 		wowlan_config_cmd->wakeup_filter |=
701 			cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
702 	if (wowlan->eap_identity_req)
703 		wowlan_config_cmd->wakeup_filter |=
704 			cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
705 	if (wowlan->four_way_handshake)
706 		wowlan_config_cmd->wakeup_filter |=
707 			cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
708 	if (wowlan->n_patterns)
709 		wowlan_config_cmd->wakeup_filter |=
710 			cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
711 
712 	if (wowlan->rfkill_release)
713 		wowlan_config_cmd->wakeup_filter |=
714 			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
715 
716 	if (wowlan->tcp) {
717 		/*
718 		 * Set the "link change" (really "link lost") flag as well
719 		 * since that implies losing the TCP connection.
720 		 */
721 		wowlan_config_cmd->wakeup_filter |=
722 			cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
723 				    IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
724 				    IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
725 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
726 	}
727 
728 	if (wowlan->any) {
729 		wowlan_config_cmd->wakeup_filter |=
730 			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
731 				    IWL_WOWLAN_WAKEUP_LINK_CHANGE |
732 				    IWL_WOWLAN_WAKEUP_RX_FRAME |
733 				    IWL_WOWLAN_WAKEUP_BCN_FILTERING);
734 	}
735 
736 	return 0;
737 }
738 
739 static void
740 iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
741 			  struct ieee80211_vif *vif,
742 			  void (*iter)(struct ieee80211_hw *hw,
743 				       struct ieee80211_vif *vif,
744 				       struct ieee80211_sta *sta,
745 				       struct ieee80211_key_conf *key,
746 				       void *data),
747 			  void *data)
748 {
749 	struct ieee80211_sta *ap_sta;
750 
751 	rcu_read_lock();
752 
753 	ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
754 	if (IS_ERR_OR_NULL(ap_sta))
755 		goto out;
756 
757 	ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
758 out:
759 	rcu_read_unlock();
760 }
761 
762 int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
763 				     struct ieee80211_vif *vif,
764 				     bool d0i3,
765 				     u32 cmd_flags)
766 {
767 	struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
768 	struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
769 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
770 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
771 	struct wowlan_key_data key_data = {
772 		.configure_keys = !d0i3 && !unified,
773 		.use_rsc_tsc = false,
774 		.tkip = &tkip_cmd,
775 		.use_tkip = false,
776 	};
777 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
778 	int ret;
779 
780 	key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
781 	if (!key_data.rsc_tsc)
782 		return -ENOMEM;
783 
784 	/*
785 	 * if we have to configure keys, call ieee80211_iter_keys(),
786 	 * as we need non-atomic context in order to take the
787 	 * required locks.
788 	 * for the d0i3 we can't use ieee80211_iter_keys(), as
789 	 * taking (almost) any mutex might result in deadlock.
790 	 */
791 	if (!d0i3) {
792 		/*
793 		 * Note that currently we don't propagate cmd_flags
794 		 * to the iterator. In case of key_data.configure_keys,
795 		 * all the configured commands are SYNC, and
796 		 * iwl_mvm_wowlan_program_keys() will take care of
797 		 * locking/unlocking mvm->mutex.
798 		 */
799 		ieee80211_iter_keys(mvm->hw, vif,
800 				    iwl_mvm_wowlan_program_keys,
801 				    &key_data);
802 	} else {
803 		iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
804 					  iwl_mvm_wowlan_program_keys,
805 					  &key_data);
806 	}
807 
808 	if (key_data.error) {
809 		ret = -EIO;
810 		goto out;
811 	}
812 
813 	if (key_data.use_rsc_tsc) {
814 		ret = iwl_mvm_send_cmd_pdu(mvm,
815 					   WOWLAN_TSC_RSC_PARAM, cmd_flags,
816 					   sizeof(*key_data.rsc_tsc),
817 					   key_data.rsc_tsc);
818 		if (ret)
819 			goto out;
820 	}
821 
822 	if (key_data.use_tkip &&
823 	    !fw_has_api(&mvm->fw->ucode_capa,
824 			IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
825 		ret = iwl_mvm_send_cmd_pdu(mvm,
826 					   WOWLAN_TKIP_PARAM,
827 					   cmd_flags, sizeof(tkip_cmd),
828 					   &tkip_cmd);
829 		if (ret)
830 			goto out;
831 	}
832 
833 	/* configure rekey data only if offloaded rekey is supported (d3) */
834 	if (mvmvif->rekey_data.valid && !d0i3) {
835 		memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
836 		memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
837 		       NL80211_KCK_LEN);
838 		kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
839 		memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
840 		       NL80211_KEK_LEN);
841 		kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
842 		kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
843 
844 		ret = iwl_mvm_send_cmd_pdu(mvm,
845 					   WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
846 					   sizeof(kek_kck_cmd),
847 					   &kek_kck_cmd);
848 		if (ret)
849 			goto out;
850 	}
851 	ret = 0;
852 out:
853 	kfree(key_data.rsc_tsc);
854 	return ret;
855 }
856 
857 static int
858 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
859 		      struct cfg80211_wowlan *wowlan,
860 		      struct iwl_wowlan_config_cmd *wowlan_config_cmd,
861 		      struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
862 		      struct ieee80211_sta *ap_sta)
863 {
864 	int ret;
865 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
866 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
867 
868 	if (!unified_image) {
869 		ret = iwl_mvm_switch_to_d3(mvm);
870 		if (ret)
871 			return ret;
872 
873 		ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
874 		if (ret)
875 			return ret;
876 	}
877 
878 	if (!iwlwifi_mod_params.swcrypto) {
879 		/*
880 		 * This needs to be unlocked due to lock ordering
881 		 * constraints. Since we're in the suspend path
882 		 * that isn't really a problem though.
883 		 */
884 		mutex_unlock(&mvm->mutex);
885 		ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
886 						       CMD_ASYNC);
887 		mutex_lock(&mvm->mutex);
888 		if (ret)
889 			return ret;
890 	}
891 
892 	ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
893 				   sizeof(*wowlan_config_cmd),
894 				   wowlan_config_cmd);
895 	if (ret)
896 		return ret;
897 
898 	if (fw_has_api(&mvm->fw->ucode_capa,
899 		       IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE))
900 		ret = iwl_mvm_send_patterns(mvm, wowlan);
901 	else
902 		ret = iwl_mvm_send_patterns_v1(mvm, wowlan);
903 	if (ret)
904 		return ret;
905 
906 	return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
907 }
908 
909 static int
910 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
911 			 struct cfg80211_wowlan *wowlan,
912 			 struct cfg80211_sched_scan_request *nd_config,
913 			 struct ieee80211_vif *vif)
914 {
915 	struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
916 	int ret;
917 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
918 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
919 
920 	if (!unified_image) {
921 		ret = iwl_mvm_switch_to_d3(mvm);
922 		if (ret)
923 			return ret;
924 	} else {
925 		/* In theory, we wouldn't have to stop a running sched
926 		 * scan in order to start another one (for
927 		 * net-detect).  But in practice this doesn't seem to
928 		 * work properly, so stop any running sched_scan now.
929 		 */
930 		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
931 		if (ret)
932 			return ret;
933 	}
934 
935 	/* rfkill release can be either for wowlan or netdetect */
936 	if (wowlan->rfkill_release)
937 		wowlan_config_cmd.wakeup_filter |=
938 			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
939 
940 	ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
941 				   sizeof(wowlan_config_cmd),
942 				   &wowlan_config_cmd);
943 	if (ret)
944 		return ret;
945 
946 	ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
947 				       IWL_MVM_SCAN_NETDETECT);
948 	if (ret)
949 		return ret;
950 
951 	if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
952 		return -EBUSY;
953 
954 	/* save the sched scan matchsets... */
955 	if (nd_config->n_match_sets) {
956 		mvm->nd_match_sets = kmemdup(nd_config->match_sets,
957 					     sizeof(*nd_config->match_sets) *
958 					     nd_config->n_match_sets,
959 					     GFP_KERNEL);
960 		if (mvm->nd_match_sets)
961 			mvm->n_nd_match_sets = nd_config->n_match_sets;
962 	}
963 
964 	/* ...and the sched scan channels for later reporting */
965 	mvm->nd_channels = kmemdup(nd_config->channels,
966 				   sizeof(*nd_config->channels) *
967 				   nd_config->n_channels,
968 				   GFP_KERNEL);
969 	if (mvm->nd_channels)
970 		mvm->n_nd_channels = nd_config->n_channels;
971 
972 	return 0;
973 }
974 
975 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
976 {
977 	kfree(mvm->nd_match_sets);
978 	mvm->nd_match_sets = NULL;
979 	mvm->n_nd_match_sets = 0;
980 	kfree(mvm->nd_channels);
981 	mvm->nd_channels = NULL;
982 	mvm->n_nd_channels = 0;
983 }
984 
985 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
986 			     struct cfg80211_wowlan *wowlan,
987 			     bool test)
988 {
989 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
990 	struct ieee80211_vif *vif = NULL;
991 	struct iwl_mvm_vif *mvmvif = NULL;
992 	struct ieee80211_sta *ap_sta = NULL;
993 	struct iwl_d3_manager_config d3_cfg_cmd_data = {
994 		/*
995 		 * Program the minimum sleep time to 10 seconds, as many
996 		 * platforms have issues processing a wakeup signal while
997 		 * still being in the process of suspending.
998 		 */
999 		.min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1000 	};
1001 	struct iwl_host_cmd d3_cfg_cmd = {
1002 		.id = D3_CONFIG_CMD,
1003 		.flags = CMD_WANT_SKB,
1004 		.data[0] = &d3_cfg_cmd_data,
1005 		.len[0] = sizeof(d3_cfg_cmd_data),
1006 	};
1007 	int ret;
1008 	int len __maybe_unused;
1009 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1010 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1011 
1012 	if (!wowlan) {
1013 		/*
1014 		 * mac80211 shouldn't get here, but for D3 test
1015 		 * it doesn't warrant a warning
1016 		 */
1017 		WARN_ON(!test);
1018 		return -EINVAL;
1019 	}
1020 
1021 	mutex_lock(&mvm->mutex);
1022 
1023 	vif = iwl_mvm_get_bss_vif(mvm);
1024 	if (IS_ERR_OR_NULL(vif)) {
1025 		ret = 1;
1026 		goto out_noreset;
1027 	}
1028 
1029 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1030 
1031 	if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA) {
1032 		/* if we're not associated, this must be netdetect */
1033 		if (!wowlan->nd_config) {
1034 			ret = 1;
1035 			goto out_noreset;
1036 		}
1037 
1038 		ret = iwl_mvm_netdetect_config(
1039 			mvm, wowlan, wowlan->nd_config, vif);
1040 		if (ret)
1041 			goto out;
1042 
1043 		mvm->net_detect = true;
1044 	} else {
1045 		struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1046 
1047 		ap_sta = rcu_dereference_protected(
1048 			mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1049 			lockdep_is_held(&mvm->mutex));
1050 		if (IS_ERR_OR_NULL(ap_sta)) {
1051 			ret = -EINVAL;
1052 			goto out_noreset;
1053 		}
1054 
1055 		ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1056 						vif, mvmvif, ap_sta);
1057 		if (ret)
1058 			goto out_noreset;
1059 		ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1060 					    vif, mvmvif, ap_sta);
1061 		if (ret)
1062 			goto out;
1063 
1064 		mvm->net_detect = false;
1065 	}
1066 
1067 	ret = iwl_mvm_power_update_device(mvm);
1068 	if (ret)
1069 		goto out;
1070 
1071 	ret = iwl_mvm_power_update_mac(mvm);
1072 	if (ret)
1073 		goto out;
1074 
1075 #ifdef CONFIG_IWLWIFI_DEBUGFS
1076 	if (mvm->d3_wake_sysassert)
1077 		d3_cfg_cmd_data.wakeup_flags |=
1078 			cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1079 #endif
1080 
1081 	/*
1082 	 * TODO: this is needed because the firmware is not stopping
1083 	 * the recording automatically before entering D3.  This can
1084 	 * be removed once the FW starts doing that.
1085 	 */
1086 	_iwl_fw_dbg_stop_recording(mvm->fwrt.trans, NULL);
1087 
1088 	/* must be last -- this switches firmware state */
1089 	ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1090 	if (ret)
1091 		goto out;
1092 #ifdef CONFIG_IWLWIFI_DEBUGFS
1093 	len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1094 	if (len >= sizeof(u32)) {
1095 		mvm->d3_test_pme_ptr =
1096 			le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1097 	}
1098 #endif
1099 	iwl_free_resp(&d3_cfg_cmd);
1100 
1101 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1102 
1103 	iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1104  out:
1105 	if (ret < 0) {
1106 		iwl_mvm_free_nd(mvm);
1107 
1108 		if (!unified_image) {
1109 			iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1110 			if (mvm->fw_restart > 0) {
1111 				mvm->fw_restart--;
1112 				ieee80211_restart_hw(mvm->hw);
1113 			}
1114 		}
1115 	}
1116  out_noreset:
1117 	mutex_unlock(&mvm->mutex);
1118 
1119 	return ret;
1120 }
1121 
1122 static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
1123 {
1124 	struct iwl_notification_wait wait_d3;
1125 	static const u16 d3_notif[] = { D3_CONFIG_CMD };
1126 	int ret;
1127 
1128 	iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
1129 				   d3_notif, ARRAY_SIZE(d3_notif),
1130 				   NULL, NULL);
1131 
1132 	ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
1133 	if (ret)
1134 		goto remove_notif;
1135 
1136 	ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
1137 	WARN_ON_ONCE(ret);
1138 	return ret;
1139 
1140 remove_notif:
1141 	iwl_remove_notification(&mvm->notif_wait, &wait_d3);
1142 	return ret;
1143 }
1144 
1145 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1146 {
1147 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1148 	struct iwl_trans *trans = mvm->trans;
1149 	int ret;
1150 
1151 	/* make sure the d0i3 exit work is not pending */
1152 	flush_work(&mvm->d0i3_exit_work);
1153 	iwl_mvm_pause_tcm(mvm, true);
1154 
1155 	iwl_fw_runtime_suspend(&mvm->fwrt);
1156 
1157 	ret = iwl_trans_suspend(trans);
1158 	if (ret)
1159 		return ret;
1160 
1161 	if (wowlan->any) {
1162 		trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
1163 
1164 		if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
1165 			ret = iwl_mvm_enter_d0i3_sync(mvm);
1166 
1167 			if (ret)
1168 				return ret;
1169 		}
1170 
1171 		mutex_lock(&mvm->d0i3_suspend_mutex);
1172 		__set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1173 		mutex_unlock(&mvm->d0i3_suspend_mutex);
1174 
1175 		iwl_trans_d3_suspend(trans, false, false);
1176 
1177 		return 0;
1178 	}
1179 
1180 	trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1181 
1182 	return __iwl_mvm_suspend(hw, wowlan, false);
1183 }
1184 
1185 /* converted data from the different status responses */
1186 struct iwl_wowlan_status_data {
1187 	u16 pattern_number;
1188 	u16 qos_seq_ctr[8];
1189 	u32 wakeup_reasons;
1190 	u32 wake_packet_length;
1191 	u32 wake_packet_bufsize;
1192 	const u8 *wake_packet;
1193 };
1194 
1195 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1196 					  struct ieee80211_vif *vif,
1197 					  struct iwl_wowlan_status_data *status)
1198 {
1199 	struct sk_buff *pkt = NULL;
1200 	struct cfg80211_wowlan_wakeup wakeup = {
1201 		.pattern_idx = -1,
1202 	};
1203 	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1204 	u32 reasons = status->wakeup_reasons;
1205 
1206 	if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1207 		wakeup_report = NULL;
1208 		goto report;
1209 	}
1210 
1211 	pm_wakeup_event(mvm->dev, 0);
1212 
1213 	if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1214 		wakeup.magic_pkt = true;
1215 
1216 	if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1217 		wakeup.pattern_idx =
1218 			status->pattern_number;
1219 
1220 	if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1221 		       IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1222 		wakeup.disconnect = true;
1223 
1224 	if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1225 		wakeup.gtk_rekey_failure = true;
1226 
1227 	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1228 		wakeup.rfkill_release = true;
1229 
1230 	if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1231 		wakeup.eap_identity_req = true;
1232 
1233 	if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1234 		wakeup.four_way_handshake = true;
1235 
1236 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1237 		wakeup.tcp_connlost = true;
1238 
1239 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1240 		wakeup.tcp_nomoretokens = true;
1241 
1242 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1243 		wakeup.tcp_match = true;
1244 
1245 	if (status->wake_packet_bufsize) {
1246 		int pktsize = status->wake_packet_bufsize;
1247 		int pktlen = status->wake_packet_length;
1248 		const u8 *pktdata = status->wake_packet;
1249 		struct ieee80211_hdr *hdr = (void *)pktdata;
1250 		int truncated = pktlen - pktsize;
1251 
1252 		/* this would be a firmware bug */
1253 		if (WARN_ON_ONCE(truncated < 0))
1254 			truncated = 0;
1255 
1256 		if (ieee80211_is_data(hdr->frame_control)) {
1257 			int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1258 			int ivlen = 0, icvlen = 4; /* also FCS */
1259 
1260 			pkt = alloc_skb(pktsize, GFP_KERNEL);
1261 			if (!pkt)
1262 				goto report;
1263 
1264 			skb_put_data(pkt, pktdata, hdrlen);
1265 			pktdata += hdrlen;
1266 			pktsize -= hdrlen;
1267 
1268 			if (ieee80211_has_protected(hdr->frame_control)) {
1269 				/*
1270 				 * This is unlocked and using gtk_i(c)vlen,
1271 				 * but since everything is under RTNL still
1272 				 * that's not really a problem - changing
1273 				 * it would be difficult.
1274 				 */
1275 				if (is_multicast_ether_addr(hdr->addr1)) {
1276 					ivlen = mvm->gtk_ivlen;
1277 					icvlen += mvm->gtk_icvlen;
1278 				} else {
1279 					ivlen = mvm->ptk_ivlen;
1280 					icvlen += mvm->ptk_icvlen;
1281 				}
1282 			}
1283 
1284 			/* if truncated, FCS/ICV is (partially) gone */
1285 			if (truncated >= icvlen) {
1286 				icvlen = 0;
1287 				truncated -= icvlen;
1288 			} else {
1289 				icvlen -= truncated;
1290 				truncated = 0;
1291 			}
1292 
1293 			pktsize -= ivlen + icvlen;
1294 			pktdata += ivlen;
1295 
1296 			skb_put_data(pkt, pktdata, pktsize);
1297 
1298 			if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1299 				goto report;
1300 			wakeup.packet = pkt->data;
1301 			wakeup.packet_present_len = pkt->len;
1302 			wakeup.packet_len = pkt->len - truncated;
1303 			wakeup.packet_80211 = false;
1304 		} else {
1305 			int fcslen = 4;
1306 
1307 			if (truncated >= 4) {
1308 				truncated -= 4;
1309 				fcslen = 0;
1310 			} else {
1311 				fcslen -= truncated;
1312 				truncated = 0;
1313 			}
1314 			pktsize -= fcslen;
1315 			wakeup.packet = status->wake_packet;
1316 			wakeup.packet_present_len = pktsize;
1317 			wakeup.packet_len = pktlen - truncated;
1318 			wakeup.packet_80211 = true;
1319 		}
1320 	}
1321 
1322  report:
1323 	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1324 	kfree_skb(pkt);
1325 }
1326 
1327 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1328 				  struct ieee80211_key_seq *seq)
1329 {
1330 	u64 pn;
1331 
1332 	pn = le64_to_cpu(sc->pn);
1333 	seq->ccmp.pn[0] = pn >> 40;
1334 	seq->ccmp.pn[1] = pn >> 32;
1335 	seq->ccmp.pn[2] = pn >> 24;
1336 	seq->ccmp.pn[3] = pn >> 16;
1337 	seq->ccmp.pn[4] = pn >> 8;
1338 	seq->ccmp.pn[5] = pn;
1339 }
1340 
1341 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1342 				   struct ieee80211_key_seq *seq)
1343 {
1344 	seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1345 	seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1346 }
1347 
1348 static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
1349 				   struct ieee80211_sta *sta,
1350 				   struct ieee80211_key_conf *key)
1351 {
1352 	int tid;
1353 
1354 	BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1355 
1356 	if (sta && iwl_mvm_has_new_rx_api(mvm)) {
1357 		struct iwl_mvm_sta *mvmsta;
1358 		struct iwl_mvm_key_pn *ptk_pn;
1359 
1360 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
1361 
1362 		ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
1363 						   lockdep_is_held(&mvm->mutex));
1364 		if (WARN_ON(!ptk_pn))
1365 			return;
1366 
1367 		for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1368 			struct ieee80211_key_seq seq = {};
1369 			int i;
1370 
1371 			iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1372 			ieee80211_set_key_rx_seq(key, tid, &seq);
1373 			for (i = 1; i < mvm->trans->num_rx_queues; i++)
1374 				memcpy(ptk_pn->q[i].pn[tid],
1375 				       seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
1376 		}
1377 	} else {
1378 		for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1379 			struct ieee80211_key_seq seq = {};
1380 
1381 			iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1382 			ieee80211_set_key_rx_seq(key, tid, &seq);
1383 		}
1384 	}
1385 }
1386 
1387 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1388 				    struct ieee80211_key_conf *key)
1389 {
1390 	int tid;
1391 
1392 	BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1393 
1394 	for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1395 		struct ieee80211_key_seq seq = {};
1396 
1397 		iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1398 		ieee80211_set_key_rx_seq(key, tid, &seq);
1399 	}
1400 }
1401 
1402 static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
1403 				   struct ieee80211_key_conf *key,
1404 				   struct iwl_wowlan_status *status)
1405 {
1406 	union iwl_all_tsc_rsc *rsc = &status->gtk[0].rsc.all_tsc_rsc;
1407 
1408 	switch (key->cipher) {
1409 	case WLAN_CIPHER_SUITE_CCMP:
1410 		iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
1411 		break;
1412 	case WLAN_CIPHER_SUITE_TKIP:
1413 		iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1414 		break;
1415 	default:
1416 		WARN_ON(1);
1417 	}
1418 }
1419 
1420 struct iwl_mvm_d3_gtk_iter_data {
1421 	struct iwl_mvm *mvm;
1422 	struct iwl_wowlan_status *status;
1423 	void *last_gtk;
1424 	u32 cipher;
1425 	bool find_phase, unhandled_cipher;
1426 	int num_keys;
1427 };
1428 
1429 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1430 				   struct ieee80211_vif *vif,
1431 				   struct ieee80211_sta *sta,
1432 				   struct ieee80211_key_conf *key,
1433 				   void *_data)
1434 {
1435 	struct iwl_mvm_d3_gtk_iter_data *data = _data;
1436 
1437 	if (data->unhandled_cipher)
1438 		return;
1439 
1440 	switch (key->cipher) {
1441 	case WLAN_CIPHER_SUITE_WEP40:
1442 	case WLAN_CIPHER_SUITE_WEP104:
1443 		/* ignore WEP completely, nothing to do */
1444 		return;
1445 	case WLAN_CIPHER_SUITE_CCMP:
1446 	case WLAN_CIPHER_SUITE_TKIP:
1447 		/* we support these */
1448 		break;
1449 	default:
1450 		/* everything else (even CMAC for MFP) - disconnect from AP */
1451 		data->unhandled_cipher = true;
1452 		return;
1453 	}
1454 
1455 	data->num_keys++;
1456 
1457 	/*
1458 	 * pairwise key - update sequence counters only;
1459 	 * note that this assumes no TDLS sessions are active
1460 	 */
1461 	if (sta) {
1462 		struct ieee80211_key_seq seq = {};
1463 		union iwl_all_tsc_rsc *sc =
1464 			&data->status->gtk[0].rsc.all_tsc_rsc;
1465 
1466 		if (data->find_phase)
1467 			return;
1468 
1469 		switch (key->cipher) {
1470 		case WLAN_CIPHER_SUITE_CCMP:
1471 			iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
1472 					       sta, key);
1473 			atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
1474 			break;
1475 		case WLAN_CIPHER_SUITE_TKIP:
1476 			iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1477 			iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1478 			atomic64_set(&key->tx_pn,
1479 				     (u64)seq.tkip.iv16 |
1480 				     ((u64)seq.tkip.iv32 << 16));
1481 			break;
1482 		}
1483 
1484 		/* that's it for this key */
1485 		return;
1486 	}
1487 
1488 	if (data->find_phase) {
1489 		data->last_gtk = key;
1490 		data->cipher = key->cipher;
1491 		return;
1492 	}
1493 
1494 	if (data->status->num_of_gtk_rekeys)
1495 		ieee80211_remove_key(key);
1496 	else if (data->last_gtk == key)
1497 		iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
1498 }
1499 
1500 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1501 					  struct ieee80211_vif *vif,
1502 					  struct iwl_wowlan_status *status)
1503 {
1504 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1505 	struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1506 		.mvm = mvm,
1507 		.status = status,
1508 	};
1509 	u32 disconnection_reasons =
1510 		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1511 		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1512 
1513 	if (!status || !vif->bss_conf.bssid)
1514 		return false;
1515 
1516 	if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1517 		return false;
1518 
1519 	/* find last GTK that we used initially, if any */
1520 	gtkdata.find_phase = true;
1521 	ieee80211_iter_keys(mvm->hw, vif,
1522 			    iwl_mvm_d3_update_keys, &gtkdata);
1523 	/* not trying to keep connections with MFP/unhandled ciphers */
1524 	if (gtkdata.unhandled_cipher)
1525 		return false;
1526 	if (!gtkdata.num_keys)
1527 		goto out;
1528 	if (!gtkdata.last_gtk)
1529 		return false;
1530 
1531 	/*
1532 	 * invalidate all other GTKs that might still exist and update
1533 	 * the one that we used
1534 	 */
1535 	gtkdata.find_phase = false;
1536 	ieee80211_iter_keys(mvm->hw, vif,
1537 			    iwl_mvm_d3_update_keys, &gtkdata);
1538 
1539 	if (status->num_of_gtk_rekeys) {
1540 		struct ieee80211_key_conf *key;
1541 		struct {
1542 			struct ieee80211_key_conf conf;
1543 			u8 key[32];
1544 		} conf = {
1545 			.conf.cipher = gtkdata.cipher,
1546 			.conf.keyidx =
1547 				iwlmvm_wowlan_gtk_idx(&status->gtk[0]),
1548 		};
1549 		__be64 replay_ctr;
1550 
1551 		switch (gtkdata.cipher) {
1552 		case WLAN_CIPHER_SUITE_CCMP:
1553 			conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1554 			memcpy(conf.conf.key, status->gtk[0].key,
1555 			       WLAN_KEY_LEN_CCMP);
1556 			break;
1557 		case WLAN_CIPHER_SUITE_TKIP:
1558 			conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1559 			memcpy(conf.conf.key, status->gtk[0].key, 16);
1560 			/* leave TX MIC key zeroed, we don't use it anyway */
1561 			memcpy(conf.conf.key +
1562 			       NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1563 			       status->gtk[0].tkip_mic_key, 8);
1564 			break;
1565 		}
1566 
1567 		key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1568 		if (IS_ERR(key))
1569 			return false;
1570 		iwl_mvm_set_key_rx_seq(mvm, key, status);
1571 
1572 		replay_ctr =
1573 			cpu_to_be64(le64_to_cpu(status->replay_ctr));
1574 
1575 		ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1576 					   (void *)&replay_ctr, GFP_KERNEL);
1577 	}
1578 
1579 out:
1580 	mvmvif->seqno_valid = true;
1581 	/* +0x10 because the set API expects next-to-use, not last-used */
1582 	mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1583 
1584 	return true;
1585 }
1586 
1587 struct iwl_wowlan_status *iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm)
1588 {
1589 	struct iwl_wowlan_status *v7, *status;
1590 	struct iwl_host_cmd cmd = {
1591 		.id = WOWLAN_GET_STATUSES,
1592 		.flags = CMD_WANT_SKB,
1593 	};
1594 	int ret, len, status_size;
1595 
1596 	lockdep_assert_held(&mvm->mutex);
1597 
1598 	ret = iwl_mvm_send_cmd(mvm, &cmd);
1599 	if (ret) {
1600 		IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
1601 		return ERR_PTR(ret);
1602 	}
1603 
1604 	if (!fw_has_api(&mvm->fw->ucode_capa,
1605 			IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL)) {
1606 		struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
1607 		int data_size;
1608 
1609 		status_size = sizeof(*v6);
1610 		len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1611 
1612 		if (len < status_size) {
1613 			IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1614 			status = ERR_PTR(-EIO);
1615 			goto out_free_resp;
1616 		}
1617 
1618 		data_size = ALIGN(le32_to_cpu(v6->wake_packet_bufsize), 4);
1619 
1620 		if (len != (status_size + data_size)) {
1621 			IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1622 			status = ERR_PTR(-EIO);
1623 			goto out_free_resp;
1624 		}
1625 
1626 		status = kzalloc(sizeof(*status) + data_size, GFP_KERNEL);
1627 		if (!status)
1628 			goto out_free_resp;
1629 
1630 		BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
1631 			     sizeof(status->gtk[0].key));
1632 		BUILD_BUG_ON(sizeof(v6->gtk.tkip_mic_key) >
1633 			     sizeof(status->gtk[0].tkip_mic_key));
1634 
1635 		/* copy GTK info to the right place */
1636 		memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
1637 		       sizeof(v6->gtk.decrypt_key));
1638 		memcpy(status->gtk[0].tkip_mic_key, v6->gtk.tkip_mic_key,
1639 		       sizeof(v6->gtk.tkip_mic_key));
1640 		memcpy(&status->gtk[0].rsc, &v6->gtk.rsc,
1641 		       sizeof(status->gtk[0].rsc));
1642 
1643 		/* hardcode the key length to 16 since v6 only supports 16 */
1644 		status->gtk[0].key_len = 16;
1645 
1646 		/*
1647 		 * The key index only uses 2 bits (values 0 to 3) and
1648 		 * we always set bit 7 which means this is the
1649 		 * currently used key.
1650 		 */
1651 		status->gtk[0].key_flags = v6->gtk.key_index | BIT(7);
1652 
1653 		status->replay_ctr = v6->replay_ctr;
1654 
1655 		/* everything starting from pattern_number is identical */
1656 		memcpy(&status->pattern_number, &v6->pattern_number,
1657 		       offsetof(struct iwl_wowlan_status, wake_packet) -
1658 		       offsetof(struct iwl_wowlan_status, pattern_number) +
1659 		       data_size);
1660 
1661 		goto out_free_resp;
1662 	}
1663 
1664 	v7 = (void *)cmd.resp_pkt->data;
1665 	status_size = sizeof(*v7);
1666 	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1667 
1668 	if (len < status_size) {
1669 		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1670 		status = ERR_PTR(-EIO);
1671 		goto out_free_resp;
1672 	}
1673 
1674 	if (len != (status_size +
1675 		    ALIGN(le32_to_cpu(v7->wake_packet_bufsize), 4))) {
1676 		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1677 		status = ERR_PTR(-EIO);
1678 		goto out_free_resp;
1679 	}
1680 
1681 	status = kmemdup(v7, len, GFP_KERNEL);
1682 
1683 out_free_resp:
1684 	iwl_free_resp(&cmd);
1685 	return status;
1686 }
1687 
1688 static struct iwl_wowlan_status *
1689 iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm)
1690 {
1691 	int ret;
1692 
1693 	/* only for tracing for now */
1694 	ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1695 	if (ret)
1696 		IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1697 
1698 	return iwl_mvm_send_wowlan_get_status(mvm);
1699 }
1700 
1701 /* releases the MVM mutex */
1702 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1703 					 struct ieee80211_vif *vif)
1704 {
1705 	struct iwl_wowlan_status_data status;
1706 	struct iwl_wowlan_status *fw_status;
1707 	int i;
1708 	bool keep;
1709 	struct iwl_mvm_sta *mvm_ap_sta;
1710 
1711 	fw_status = iwl_mvm_get_wakeup_status(mvm);
1712 	if (IS_ERR_OR_NULL(fw_status))
1713 		goto out_unlock;
1714 
1715 	status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1716 	for (i = 0; i < 8; i++)
1717 		status.qos_seq_ctr[i] =
1718 			le16_to_cpu(fw_status->qos_seq_ctr[i]);
1719 	status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1720 	status.wake_packet_length =
1721 		le32_to_cpu(fw_status->wake_packet_length);
1722 	status.wake_packet_bufsize =
1723 		le32_to_cpu(fw_status->wake_packet_bufsize);
1724 	status.wake_packet = fw_status->wake_packet;
1725 
1726 	/* still at hard-coded place 0 for D3 image */
1727 	mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
1728 	if (!mvm_ap_sta)
1729 		goto out_free;
1730 
1731 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1732 		u16 seq = status.qos_seq_ctr[i];
1733 		/* firmware stores last-used value, we store next value */
1734 		seq += 0x10;
1735 		mvm_ap_sta->tid_data[i].seq_number = seq;
1736 	}
1737 
1738 	/* now we have all the data we need, unlock to avoid mac80211 issues */
1739 	mutex_unlock(&mvm->mutex);
1740 
1741 	iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1742 
1743 	keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1744 
1745 	kfree(fw_status);
1746 	return keep;
1747 
1748 out_free:
1749 	kfree(fw_status);
1750 out_unlock:
1751 	mutex_unlock(&mvm->mutex);
1752 	return false;
1753 }
1754 
1755 void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
1756 			      struct ieee80211_vif *vif,
1757 			      struct iwl_wowlan_status *status)
1758 {
1759 	struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1760 		.mvm = mvm,
1761 		.status = status,
1762 	};
1763 
1764 	/*
1765 	 * rekey handling requires taking locks that can't be taken now.
1766 	 * however, d0i3 doesn't offload rekey, so we're fine.
1767 	 */
1768 	if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
1769 		return;
1770 
1771 	/* find last GTK that we used initially, if any */
1772 	gtkdata.find_phase = true;
1773 	iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
1774 
1775 	gtkdata.find_phase = false;
1776 	iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
1777 }
1778 
1779 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
1780 			  IWL_SCAN_MAX_PROFILES)
1781 
1782 struct iwl_mvm_nd_query_results {
1783 	u32 matched_profiles;
1784 	u8 matches[ND_QUERY_BUF_LEN];
1785 };
1786 
1787 static int
1788 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1789 				struct iwl_mvm_nd_query_results *results)
1790 {
1791 	struct iwl_scan_offload_profiles_query *query;
1792 	struct iwl_host_cmd cmd = {
1793 		.id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1794 		.flags = CMD_WANT_SKB,
1795 	};
1796 	int ret, len;
1797 	size_t query_len, matches_len;
1798 
1799 	ret = iwl_mvm_send_cmd(mvm, &cmd);
1800 	if (ret) {
1801 		IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
1802 		return ret;
1803 	}
1804 
1805 	if (fw_has_api(&mvm->fw->ucode_capa,
1806 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
1807 		query_len = sizeof(struct iwl_scan_offload_profiles_query);
1808 		matches_len = sizeof(struct iwl_scan_offload_profile_match) *
1809 			IWL_SCAN_MAX_PROFILES;
1810 	} else {
1811 		query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
1812 		matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
1813 			IWL_SCAN_MAX_PROFILES;
1814 	}
1815 
1816 	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1817 	if (len < query_len) {
1818 		IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
1819 		ret = -EIO;
1820 		goto out_free_resp;
1821 	}
1822 
1823 	query = (void *)cmd.resp_pkt->data;
1824 
1825 	results->matched_profiles = le32_to_cpu(query->matched_profiles);
1826 	memcpy(results->matches, query->matches, matches_len);
1827 
1828 #ifdef CONFIG_IWLWIFI_DEBUGFS
1829 	mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1830 #endif
1831 
1832 out_free_resp:
1833 	iwl_free_resp(&cmd);
1834 	return ret;
1835 }
1836 
1837 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
1838 					 struct iwl_mvm_nd_query_results *query,
1839 					 int idx)
1840 {
1841 	int n_chans = 0, i;
1842 
1843 	if (fw_has_api(&mvm->fw->ucode_capa,
1844 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
1845 		struct iwl_scan_offload_profile_match *matches =
1846 			(struct iwl_scan_offload_profile_match *)query->matches;
1847 
1848 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
1849 			n_chans += hweight8(matches[idx].matching_channels[i]);
1850 	} else {
1851 		struct iwl_scan_offload_profile_match_v1 *matches =
1852 			(struct iwl_scan_offload_profile_match_v1 *)query->matches;
1853 
1854 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
1855 			n_chans += hweight8(matches[idx].matching_channels[i]);
1856 	}
1857 
1858 	return n_chans;
1859 }
1860 
1861 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
1862 				    struct iwl_mvm_nd_query_results *query,
1863 				    struct cfg80211_wowlan_nd_match *match,
1864 				    int idx)
1865 {
1866 	int i;
1867 
1868 	if (fw_has_api(&mvm->fw->ucode_capa,
1869 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
1870 		struct iwl_scan_offload_profile_match *matches =
1871 			(struct iwl_scan_offload_profile_match *)query->matches;
1872 
1873 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
1874 			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
1875 				match->channels[match->n_channels++] =
1876 					mvm->nd_channels[i]->center_freq;
1877 	} else {
1878 		struct iwl_scan_offload_profile_match_v1 *matches =
1879 			(struct iwl_scan_offload_profile_match_v1 *)query->matches;
1880 
1881 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
1882 			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
1883 				match->channels[match->n_channels++] =
1884 					mvm->nd_channels[i]->center_freq;
1885 	}
1886 }
1887 
1888 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1889 					    struct ieee80211_vif *vif)
1890 {
1891 	struct cfg80211_wowlan_nd_info *net_detect = NULL;
1892 	struct cfg80211_wowlan_wakeup wakeup = {
1893 		.pattern_idx = -1,
1894 	};
1895 	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1896 	struct iwl_mvm_nd_query_results query;
1897 	struct iwl_wowlan_status *fw_status;
1898 	unsigned long matched_profiles;
1899 	u32 reasons = 0;
1900 	int i, n_matches, ret;
1901 
1902 	fw_status = iwl_mvm_get_wakeup_status(mvm);
1903 	if (!IS_ERR_OR_NULL(fw_status)) {
1904 		reasons = le32_to_cpu(fw_status->wakeup_reasons);
1905 		kfree(fw_status);
1906 	}
1907 
1908 	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1909 		wakeup.rfkill_release = true;
1910 
1911 	if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1912 		goto out;
1913 
1914 	ret = iwl_mvm_netdetect_query_results(mvm, &query);
1915 	if (ret || !query.matched_profiles) {
1916 		wakeup_report = NULL;
1917 		goto out;
1918 	}
1919 
1920 	matched_profiles = query.matched_profiles;
1921 	if (mvm->n_nd_match_sets) {
1922 		n_matches = hweight_long(matched_profiles);
1923 	} else {
1924 		IWL_ERR(mvm, "no net detect match information available\n");
1925 		n_matches = 0;
1926 	}
1927 
1928 	net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
1929 			     GFP_KERNEL);
1930 	if (!net_detect || !n_matches)
1931 		goto out_report_nd;
1932 
1933 	for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
1934 		struct cfg80211_wowlan_nd_match *match;
1935 		int idx, n_channels = 0;
1936 
1937 		n_channels = iwl_mvm_query_num_match_chans(mvm, &query, i);
1938 
1939 		match = kzalloc(struct_size(match, channels, n_channels),
1940 				GFP_KERNEL);
1941 		if (!match)
1942 			goto out_report_nd;
1943 
1944 		net_detect->matches[net_detect->n_matches++] = match;
1945 
1946 		/* We inverted the order of the SSIDs in the scan
1947 		 * request, so invert the index here.
1948 		 */
1949 		idx = mvm->n_nd_match_sets - i - 1;
1950 		match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
1951 		memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
1952 		       match->ssid.ssid_len);
1953 
1954 		if (mvm->n_nd_channels < n_channels)
1955 			continue;
1956 
1957 		iwl_mvm_query_set_freqs(mvm, &query, match, i);
1958 	}
1959 
1960 out_report_nd:
1961 	wakeup.net_detect = net_detect;
1962 out:
1963 	iwl_mvm_free_nd(mvm);
1964 
1965 	mutex_unlock(&mvm->mutex);
1966 	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1967 
1968 	if (net_detect) {
1969 		for (i = 0; i < net_detect->n_matches; i++)
1970 			kfree(net_detect->matches[i]);
1971 		kfree(net_detect);
1972 	}
1973 }
1974 
1975 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1976 				       struct ieee80211_vif *vif)
1977 {
1978 	/* skip the one we keep connection on */
1979 	if (data == vif)
1980 		return;
1981 
1982 	if (vif->type == NL80211_IFTYPE_STATION)
1983 		ieee80211_resume_disconnect(vif);
1984 }
1985 
1986 static int iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
1987 				   struct ieee80211_vif *vif)
1988 {
1989 	u32 base = mvm->trans->lmac_error_event_table[0];
1990 	struct error_table_start {
1991 		/* cf. struct iwl_error_event_table */
1992 		u32 valid;
1993 		u32 error_id;
1994 	} err_info;
1995 
1996 	iwl_trans_read_mem_bytes(mvm->trans, base,
1997 				 &err_info, sizeof(err_info));
1998 
1999 	if (err_info.valid &&
2000 	    err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
2001 		struct cfg80211_wowlan_wakeup wakeup = {
2002 			.rfkill_release = true,
2003 		};
2004 		ieee80211_report_wowlan_wakeup(vif, &wakeup, GFP_KERNEL);
2005 	}
2006 	return err_info.valid;
2007 }
2008 
2009 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
2010 {
2011 	struct ieee80211_vif *vif = NULL;
2012 	int ret = 1;
2013 	enum iwl_d3_status d3_status;
2014 	bool keep = false;
2015 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2016 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2017 	bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
2018 				      IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
2019 
2020 	mutex_lock(&mvm->mutex);
2021 
2022 	/* get the BSS vif pointer again */
2023 	vif = iwl_mvm_get_bss_vif(mvm);
2024 	if (IS_ERR_OR_NULL(vif))
2025 		goto err;
2026 
2027 	ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
2028 	if (ret)
2029 		goto err;
2030 
2031 	if (d3_status != IWL_D3_STATUS_ALIVE) {
2032 		IWL_INFO(mvm, "Device was reset during suspend\n");
2033 		goto err;
2034 	}
2035 
2036 	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
2037 
2038 	if (iwl_mvm_check_rt_status(mvm, vif)) {
2039 		set_bit(STATUS_FW_ERROR, &mvm->trans->status);
2040 		iwl_mvm_dump_nic_error_log(mvm);
2041 		iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
2042 					false, 0);
2043 		ret = 1;
2044 		goto err;
2045 	}
2046 
2047 	if (d0i3_first) {
2048 		ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
2049 		if (ret < 0) {
2050 			IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
2051 				ret);
2052 			goto err;
2053 		}
2054 	}
2055 
2056 	/*
2057 	 * Query the current location and source from the D3 firmware so we
2058 	 * can play it back when we re-intiailize the D0 firmware
2059 	 */
2060 	iwl_mvm_update_changed_regdom(mvm);
2061 
2062 	if (!unified_image)
2063 		/*  Re-configure default SAR profile */
2064 		iwl_mvm_sar_select_profile(mvm, 1, 1);
2065 
2066 	if (mvm->net_detect) {
2067 		/* If this is a non-unified image, we restart the FW,
2068 		 * so no need to stop the netdetect scan.  If that
2069 		 * fails, continue and try to get the wake-up reasons,
2070 		 * but trigger a HW restart by keeping a failure code
2071 		 * in ret.
2072 		 */
2073 		if (unified_image)
2074 			ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
2075 						false);
2076 
2077 		iwl_mvm_query_netdetect_reasons(mvm, vif);
2078 		/* has unlocked the mutex, so skip that */
2079 		goto out;
2080 	} else {
2081 		keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
2082 #ifdef CONFIG_IWLWIFI_DEBUGFS
2083 		if (keep)
2084 			mvm->keep_vif = vif;
2085 #endif
2086 		/* has unlocked the mutex, so skip that */
2087 		goto out_iterate;
2088 	}
2089 
2090 err:
2091 	iwl_mvm_free_nd(mvm);
2092 	mutex_unlock(&mvm->mutex);
2093 
2094 out_iterate:
2095 	if (!test)
2096 		ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
2097 			IEEE80211_IFACE_ITER_NORMAL,
2098 			iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
2099 
2100 out:
2101 	/* no need to reset the device in unified images, if successful */
2102 	if (unified_image && !ret) {
2103 		/* nothing else to do if we already sent D0I3_END_CMD */
2104 		if (d0i3_first)
2105 			return 0;
2106 
2107 		ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
2108 		if (!ret)
2109 			return 0;
2110 	}
2111 
2112 	/*
2113 	 * Reconfigure the device in one of the following cases:
2114 	 * 1. We are not using a unified image
2115 	 * 2. We are using a unified image but had an error while exiting D3
2116 	 */
2117 	set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
2118 	/*
2119 	 * When switching images we return 1, which causes mac80211
2120 	 * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
2121 	 * This type of reconfig calls iwl_mvm_restart_complete(),
2122 	 * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
2123 	 * to take the reference here.
2124 	 */
2125 	iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
2126 
2127 	return 1;
2128 }
2129 
2130 static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
2131 {
2132 	iwl_trans_resume(mvm->trans);
2133 
2134 	return __iwl_mvm_resume(mvm, false);
2135 }
2136 
2137 static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
2138 {
2139 	bool exit_now;
2140 	enum iwl_d3_status d3_status;
2141 	struct iwl_trans *trans = mvm->trans;
2142 
2143 	iwl_trans_d3_resume(trans, &d3_status, false, false);
2144 
2145 	/*
2146 	 * make sure to clear D0I3_DEFER_WAKEUP before
2147 	 * calling iwl_trans_resume(), which might wait
2148 	 * for d0i3 exit completion.
2149 	 */
2150 	mutex_lock(&mvm->d0i3_suspend_mutex);
2151 	__clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
2152 	exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
2153 					&mvm->d0i3_suspend_flags);
2154 	mutex_unlock(&mvm->d0i3_suspend_mutex);
2155 	if (exit_now) {
2156 		IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
2157 		_iwl_mvm_exit_d0i3(mvm);
2158 	}
2159 
2160 	iwl_trans_resume(trans);
2161 
2162 	if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
2163 		int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
2164 
2165 		if (ret)
2166 			return ret;
2167 		/*
2168 		 * d0i3 exit will be deferred until reconfig_complete.
2169 		 * make sure there we are out of d0i3.
2170 		 */
2171 	}
2172 	return 0;
2173 }
2174 
2175 int iwl_mvm_resume(struct ieee80211_hw *hw)
2176 {
2177 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2178 	int ret;
2179 
2180 	if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
2181 		ret = iwl_mvm_resume_d0i3(mvm);
2182 	else
2183 		ret = iwl_mvm_resume_d3(mvm);
2184 
2185 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2186 
2187 	iwl_mvm_resume_tcm(mvm);
2188 
2189 	iwl_fw_runtime_resume(&mvm->fwrt);
2190 
2191 	return ret;
2192 }
2193 
2194 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2195 {
2196 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2197 
2198 	device_set_wakeup_enable(mvm->trans->dev, enabled);
2199 }
2200 
2201 #ifdef CONFIG_IWLWIFI_DEBUGFS
2202 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2203 {
2204 	struct iwl_mvm *mvm = inode->i_private;
2205 	int err;
2206 
2207 	if (mvm->d3_test_active)
2208 		return -EBUSY;
2209 
2210 	file->private_data = inode->i_private;
2211 
2212 	synchronize_net();
2213 
2214 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
2215 
2216 	iwl_mvm_pause_tcm(mvm, true);
2217 
2218 	iwl_fw_runtime_suspend(&mvm->fwrt);
2219 
2220 	/* start pseudo D3 */
2221 	rtnl_lock();
2222 	err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2223 	rtnl_unlock();
2224 	if (err > 0)
2225 		err = -EINVAL;
2226 	if (err)
2227 		return err;
2228 
2229 	mvm->d3_test_active = true;
2230 	mvm->keep_vif = NULL;
2231 	return 0;
2232 }
2233 
2234 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2235 				    size_t count, loff_t *ppos)
2236 {
2237 	struct iwl_mvm *mvm = file->private_data;
2238 	u32 pme_asserted;
2239 
2240 	while (true) {
2241 		/* read pme_ptr if available */
2242 		if (mvm->d3_test_pme_ptr) {
2243 			pme_asserted = iwl_trans_read_mem32(mvm->trans,
2244 						mvm->d3_test_pme_ptr);
2245 			if (pme_asserted)
2246 				break;
2247 		}
2248 
2249 		if (msleep_interruptible(100))
2250 			break;
2251 	}
2252 
2253 	return 0;
2254 }
2255 
2256 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2257 					      struct ieee80211_vif *vif)
2258 {
2259 	/* skip the one we keep connection on */
2260 	if (_data == vif)
2261 		return;
2262 
2263 	if (vif->type == NL80211_IFTYPE_STATION)
2264 		ieee80211_connection_loss(vif);
2265 }
2266 
2267 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2268 {
2269 	struct iwl_mvm *mvm = inode->i_private;
2270 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2271 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2272 
2273 	mvm->d3_test_active = false;
2274 
2275 	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
2276 
2277 	rtnl_lock();
2278 	__iwl_mvm_resume(mvm, true);
2279 	rtnl_unlock();
2280 
2281 	iwl_mvm_resume_tcm(mvm);
2282 
2283 	iwl_fw_runtime_resume(&mvm->fwrt);
2284 
2285 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2286 
2287 	iwl_abort_notification_waits(&mvm->notif_wait);
2288 	if (!unified_image) {
2289 		int remaining_time = 10;
2290 
2291 		ieee80211_restart_hw(mvm->hw);
2292 
2293 		/* wait for restart and disconnect all interfaces */
2294 		while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2295 		       remaining_time > 0) {
2296 			remaining_time--;
2297 			msleep(1000);
2298 		}
2299 
2300 		if (remaining_time == 0)
2301 			IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
2302 	}
2303 
2304 	ieee80211_iterate_active_interfaces_atomic(
2305 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2306 		iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
2307 
2308 	return 0;
2309 }
2310 
2311 const struct file_operations iwl_dbgfs_d3_test_ops = {
2312 	.llseek = no_llseek,
2313 	.open = iwl_mvm_d3_test_open,
2314 	.read = iwl_mvm_d3_test_read,
2315 	.release = iwl_mvm_d3_test_release,
2316 };
2317 #endif
2318