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