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