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