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