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