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