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