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