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