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 
1094 		wowlan_config_cmd.sta_id = mvmvif->ap_sta_id;
1095 
1096 		ap_sta = rcu_dereference_protected(
1097 			mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1098 			lockdep_is_held(&mvm->mutex));
1099 		if (IS_ERR_OR_NULL(ap_sta)) {
1100 			ret = -EINVAL;
1101 			goto out_noreset;
1102 		}
1103 
1104 		ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1105 						vif, mvmvif, ap_sta);
1106 		if (ret)
1107 			goto out_noreset;
1108 		ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1109 					    vif, mvmvif, ap_sta);
1110 		if (ret)
1111 			goto out;
1112 
1113 		mvm->net_detect = false;
1114 	}
1115 
1116 	ret = iwl_mvm_power_update_device(mvm);
1117 	if (ret)
1118 		goto out;
1119 
1120 	ret = iwl_mvm_power_update_mac(mvm);
1121 	if (ret)
1122 		goto out;
1123 
1124 #ifdef CONFIG_IWLWIFI_DEBUGFS
1125 	if (mvm->d3_wake_sysassert)
1126 		d3_cfg_cmd_data.wakeup_flags |=
1127 			cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1128 #endif
1129 
1130 	/*
1131 	 * Prior to 9000 device family the driver needs to stop the dbg
1132 	 * recording before entering D3. In later devices the FW stops the
1133 	 * recording automatically.
1134 	 */
1135 	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1136 		iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
1137 
1138 	/* must be last -- this switches firmware state */
1139 	ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1140 	if (ret)
1141 		goto out;
1142 #ifdef CONFIG_IWLWIFI_DEBUGFS
1143 	len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1144 	if (len >= sizeof(u32)) {
1145 		mvm->d3_test_pme_ptr =
1146 			le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1147 	}
1148 #endif
1149 	iwl_free_resp(&d3_cfg_cmd);
1150 
1151 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1152 
1153 	ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1154  out:
1155 	if (ret < 0) {
1156 		iwl_mvm_free_nd(mvm);
1157 
1158 		if (!unified_image) {
1159 			if (mvm->fw_restart > 0) {
1160 				mvm->fw_restart--;
1161 				ieee80211_restart_hw(mvm->hw);
1162 			}
1163 		}
1164 
1165 		clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1166 	}
1167  out_noreset:
1168 	mutex_unlock(&mvm->mutex);
1169 
1170 	return ret;
1171 }
1172 
iwl_mvm_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1173 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1174 {
1175 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1176 	struct iwl_trans *trans = mvm->trans;
1177 	int ret;
1178 
1179 	iwl_mvm_pause_tcm(mvm, true);
1180 
1181 	iwl_fw_runtime_suspend(&mvm->fwrt);
1182 
1183 	ret = iwl_trans_suspend(trans);
1184 	if (ret)
1185 		return ret;
1186 
1187 	trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1188 
1189 	return __iwl_mvm_suspend(hw, wowlan, false);
1190 }
1191 
1192 /* converted data from the different status responses */
1193 struct iwl_wowlan_status_data {
1194 	u16 pattern_number;
1195 	u16 qos_seq_ctr[8];
1196 	u32 wakeup_reasons;
1197 	u32 wake_packet_length;
1198 	u32 wake_packet_bufsize;
1199 	const u8 *wake_packet;
1200 };
1201 
iwl_mvm_report_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)1202 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1203 					  struct ieee80211_vif *vif,
1204 					  struct iwl_wowlan_status_data *status)
1205 {
1206 	struct sk_buff *pkt = NULL;
1207 	struct cfg80211_wowlan_wakeup wakeup = {
1208 		.pattern_idx = -1,
1209 	};
1210 	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1211 	u32 reasons = status->wakeup_reasons;
1212 
1213 	if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1214 		wakeup_report = NULL;
1215 		goto report;
1216 	}
1217 
1218 	pm_wakeup_event(mvm->dev, 0);
1219 
1220 	if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1221 		wakeup.magic_pkt = true;
1222 
1223 	if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1224 		wakeup.pattern_idx =
1225 			status->pattern_number;
1226 
1227 	if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1228 		       IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1229 		wakeup.disconnect = true;
1230 
1231 	if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1232 		wakeup.gtk_rekey_failure = true;
1233 
1234 	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1235 		wakeup.rfkill_release = true;
1236 
1237 	if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1238 		wakeup.eap_identity_req = true;
1239 
1240 	if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1241 		wakeup.four_way_handshake = true;
1242 
1243 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1244 		wakeup.tcp_connlost = true;
1245 
1246 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1247 		wakeup.tcp_nomoretokens = true;
1248 
1249 	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1250 		wakeup.tcp_match = true;
1251 
1252 	if (status->wake_packet_bufsize) {
1253 		int pktsize = status->wake_packet_bufsize;
1254 		int pktlen = status->wake_packet_length;
1255 		const u8 *pktdata = status->wake_packet;
1256 		struct ieee80211_hdr *hdr = (void *)pktdata;
1257 		int truncated = pktlen - pktsize;
1258 
1259 		/* this would be a firmware bug */
1260 		if (WARN_ON_ONCE(truncated < 0))
1261 			truncated = 0;
1262 
1263 		if (ieee80211_is_data(hdr->frame_control)) {
1264 			int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1265 			int ivlen = 0, icvlen = 4; /* also FCS */
1266 
1267 			pkt = alloc_skb(pktsize, GFP_KERNEL);
1268 			if (!pkt)
1269 				goto report;
1270 
1271 			skb_put_data(pkt, pktdata, hdrlen);
1272 			pktdata += hdrlen;
1273 			pktsize -= hdrlen;
1274 
1275 			if (ieee80211_has_protected(hdr->frame_control)) {
1276 				/*
1277 				 * This is unlocked and using gtk_i(c)vlen,
1278 				 * but since everything is under RTNL still
1279 				 * that's not really a problem - changing
1280 				 * it would be difficult.
1281 				 */
1282 				if (is_multicast_ether_addr(hdr->addr1)) {
1283 					ivlen = mvm->gtk_ivlen;
1284 					icvlen += mvm->gtk_icvlen;
1285 				} else {
1286 					ivlen = mvm->ptk_ivlen;
1287 					icvlen += mvm->ptk_icvlen;
1288 				}
1289 			}
1290 
1291 			/* if truncated, FCS/ICV is (partially) gone */
1292 			if (truncated >= icvlen) {
1293 				icvlen = 0;
1294 				truncated -= icvlen;
1295 			} else {
1296 				icvlen -= truncated;
1297 				truncated = 0;
1298 			}
1299 
1300 			pktsize -= ivlen + icvlen;
1301 			pktdata += ivlen;
1302 
1303 			skb_put_data(pkt, pktdata, pktsize);
1304 
1305 			if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1306 				goto report;
1307 			wakeup.packet = pkt->data;
1308 			wakeup.packet_present_len = pkt->len;
1309 			wakeup.packet_len = pkt->len - truncated;
1310 			wakeup.packet_80211 = false;
1311 		} else {
1312 			int fcslen = 4;
1313 
1314 			if (truncated >= 4) {
1315 				truncated -= 4;
1316 				fcslen = 0;
1317 			} else {
1318 				fcslen -= truncated;
1319 				truncated = 0;
1320 			}
1321 			pktsize -= fcslen;
1322 			wakeup.packet = status->wake_packet;
1323 			wakeup.packet_present_len = pktsize;
1324 			wakeup.packet_len = pktlen - truncated;
1325 			wakeup.packet_80211 = true;
1326 		}
1327 	}
1328 
1329  report:
1330 	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1331 	kfree_skb(pkt);
1332 }
1333 
iwl_mvm_aes_sc_to_seq(struct aes_sc * sc,struct ieee80211_key_seq * seq)1334 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1335 				  struct ieee80211_key_seq *seq)
1336 {
1337 	u64 pn;
1338 
1339 	pn = le64_to_cpu(sc->pn);
1340 	seq->ccmp.pn[0] = pn >> 40;
1341 	seq->ccmp.pn[1] = pn >> 32;
1342 	seq->ccmp.pn[2] = pn >> 24;
1343 	seq->ccmp.pn[3] = pn >> 16;
1344 	seq->ccmp.pn[4] = pn >> 8;
1345 	seq->ccmp.pn[5] = pn;
1346 }
1347 
iwl_mvm_tkip_sc_to_seq(struct tkip_sc * sc,struct ieee80211_key_seq * seq)1348 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1349 				   struct ieee80211_key_seq *seq)
1350 {
1351 	seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1352 	seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1353 }
1354 
iwl_mvm_set_aes_rx_seq(struct iwl_mvm * mvm,struct aes_sc * scs,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)1355 static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
1356 				   struct ieee80211_sta *sta,
1357 				   struct ieee80211_key_conf *key)
1358 {
1359 	int tid;
1360 
1361 	BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1362 
1363 	if (sta && iwl_mvm_has_new_rx_api(mvm)) {
1364 		struct iwl_mvm_sta *mvmsta;
1365 		struct iwl_mvm_key_pn *ptk_pn;
1366 
1367 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
1368 
1369 		rcu_read_lock();
1370 		ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
1371 		if (WARN_ON(!ptk_pn)) {
1372 			rcu_read_unlock();
1373 			return;
1374 		}
1375 
1376 		for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1377 			struct ieee80211_key_seq seq = {};
1378 			int i;
1379 
1380 			iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1381 			ieee80211_set_key_rx_seq(key, tid, &seq);
1382 			for (i = 1; i < mvm->trans->num_rx_queues; i++)
1383 				memcpy(ptk_pn->q[i].pn[tid],
1384 				       seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
1385 		}
1386 		rcu_read_unlock();
1387 	} else {
1388 		for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1389 			struct ieee80211_key_seq seq = {};
1390 
1391 			iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1392 			ieee80211_set_key_rx_seq(key, tid, &seq);
1393 		}
1394 	}
1395 }
1396 
iwl_mvm_set_tkip_rx_seq(struct tkip_sc * scs,struct ieee80211_key_conf * key)1397 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1398 				    struct ieee80211_key_conf *key)
1399 {
1400 	int tid;
1401 
1402 	BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1403 
1404 	for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1405 		struct ieee80211_key_seq seq = {};
1406 
1407 		iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1408 		ieee80211_set_key_rx_seq(key, tid, &seq);
1409 	}
1410 }
1411 
iwl_mvm_set_key_rx_seq(struct iwl_mvm * mvm,struct ieee80211_key_conf * key,struct iwl_wowlan_status * status)1412 static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
1413 				   struct ieee80211_key_conf *key,
1414 				   struct iwl_wowlan_status *status)
1415 {
1416 	union iwl_all_tsc_rsc *rsc = &status->gtk[0].rsc.all_tsc_rsc;
1417 
1418 	switch (key->cipher) {
1419 	case WLAN_CIPHER_SUITE_CCMP:
1420 	case WLAN_CIPHER_SUITE_GCMP:
1421 	case WLAN_CIPHER_SUITE_GCMP_256:
1422 		iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
1423 		break;
1424 	case WLAN_CIPHER_SUITE_TKIP:
1425 		iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1426 		break;
1427 	default:
1428 		WARN_ON(1);
1429 	}
1430 }
1431 
1432 struct iwl_mvm_d3_gtk_iter_data {
1433 	struct iwl_mvm *mvm;
1434 	struct iwl_wowlan_status *status;
1435 	void *last_gtk;
1436 	u32 cipher;
1437 	bool find_phase, unhandled_cipher;
1438 	int num_keys;
1439 };
1440 
iwl_mvm_d3_update_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)1441 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1442 				   struct ieee80211_vif *vif,
1443 				   struct ieee80211_sta *sta,
1444 				   struct ieee80211_key_conf *key,
1445 				   void *_data)
1446 {
1447 	struct iwl_mvm_d3_gtk_iter_data *data = _data;
1448 
1449 	if (data->unhandled_cipher)
1450 		return;
1451 
1452 	switch (key->cipher) {
1453 	case WLAN_CIPHER_SUITE_WEP40:
1454 	case WLAN_CIPHER_SUITE_WEP104:
1455 		/* ignore WEP completely, nothing to do */
1456 		return;
1457 	case WLAN_CIPHER_SUITE_CCMP:
1458 	case WLAN_CIPHER_SUITE_GCMP:
1459 	case WLAN_CIPHER_SUITE_GCMP_256:
1460 	case WLAN_CIPHER_SUITE_TKIP:
1461 		/* we support these */
1462 		break;
1463 	default:
1464 		/* everything else (even CMAC for MFP) - disconnect from AP */
1465 		data->unhandled_cipher = true;
1466 		return;
1467 	}
1468 
1469 	data->num_keys++;
1470 
1471 	/*
1472 	 * pairwise key - update sequence counters only;
1473 	 * note that this assumes no TDLS sessions are active
1474 	 */
1475 	if (sta) {
1476 		struct ieee80211_key_seq seq = {};
1477 		union iwl_all_tsc_rsc *sc =
1478 			&data->status->gtk[0].rsc.all_tsc_rsc;
1479 
1480 		if (data->find_phase)
1481 			return;
1482 
1483 		switch (key->cipher) {
1484 		case WLAN_CIPHER_SUITE_CCMP:
1485 		case WLAN_CIPHER_SUITE_GCMP:
1486 		case WLAN_CIPHER_SUITE_GCMP_256:
1487 			iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
1488 					       sta, key);
1489 			atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
1490 			break;
1491 		case WLAN_CIPHER_SUITE_TKIP:
1492 			iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1493 			iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1494 			atomic64_set(&key->tx_pn,
1495 				     (u64)seq.tkip.iv16 |
1496 				     ((u64)seq.tkip.iv32 << 16));
1497 			break;
1498 		}
1499 
1500 		/* that's it for this key */
1501 		return;
1502 	}
1503 
1504 	if (data->find_phase) {
1505 		data->last_gtk = key;
1506 		data->cipher = key->cipher;
1507 		return;
1508 	}
1509 
1510 	if (data->status->num_of_gtk_rekeys)
1511 		ieee80211_remove_key(key);
1512 	else if (data->last_gtk == key)
1513 		iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
1514 }
1515 
iwl_mvm_setup_connection_keep(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status * status)1516 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1517 					  struct ieee80211_vif *vif,
1518 					  struct iwl_wowlan_status *status)
1519 {
1520 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1521 	struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1522 		.mvm = mvm,
1523 		.status = status,
1524 	};
1525 	u32 disconnection_reasons =
1526 		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1527 		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1528 
1529 	if (!status || !vif->bss_conf.bssid)
1530 		return false;
1531 
1532 	if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1533 		return false;
1534 
1535 	/* find last GTK that we used initially, if any */
1536 	gtkdata.find_phase = true;
1537 	ieee80211_iter_keys(mvm->hw, vif,
1538 			    iwl_mvm_d3_update_keys, &gtkdata);
1539 	/* not trying to keep connections with MFP/unhandled ciphers */
1540 	if (gtkdata.unhandled_cipher)
1541 		return false;
1542 	if (!gtkdata.num_keys)
1543 		goto out;
1544 	if (!gtkdata.last_gtk)
1545 		return false;
1546 
1547 	/*
1548 	 * invalidate all other GTKs that might still exist and update
1549 	 * the one that we used
1550 	 */
1551 	gtkdata.find_phase = false;
1552 	ieee80211_iter_keys(mvm->hw, vif,
1553 			    iwl_mvm_d3_update_keys, &gtkdata);
1554 
1555 	IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
1556 			 le32_to_cpu(status->num_of_gtk_rekeys));
1557 	if (status->num_of_gtk_rekeys) {
1558 		struct ieee80211_key_conf *key;
1559 		struct {
1560 			struct ieee80211_key_conf conf;
1561 			u8 key[32];
1562 		} conf = {
1563 			.conf.cipher = gtkdata.cipher,
1564 			.conf.keyidx =
1565 				iwlmvm_wowlan_gtk_idx(&status->gtk[0]),
1566 		};
1567 		__be64 replay_ctr;
1568 
1569 		IWL_DEBUG_WOWLAN(mvm,
1570 				 "Received from FW GTK cipher %d, key index %d\n",
1571 				 conf.conf.cipher, conf.conf.keyidx);
1572 		switch (gtkdata.cipher) {
1573 		case WLAN_CIPHER_SUITE_CCMP:
1574 		case WLAN_CIPHER_SUITE_GCMP:
1575 			BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
1576 			BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP);
1577 			conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1578 			memcpy(conf.conf.key, status->gtk[0].key,
1579 			       WLAN_KEY_LEN_CCMP);
1580 			break;
1581 		case WLAN_CIPHER_SUITE_GCMP_256:
1582 			BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256);
1583 			conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
1584 			memcpy(conf.conf.key, status->gtk[0].key,
1585 			       WLAN_KEY_LEN_GCMP_256);
1586 			break;
1587 		case WLAN_CIPHER_SUITE_TKIP:
1588 			BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP);
1589 			conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1590 			memcpy(conf.conf.key, status->gtk[0].key, 16);
1591 			/* leave TX MIC key zeroed, we don't use it anyway */
1592 			memcpy(conf.conf.key +
1593 			       NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1594 			       status->gtk[0].tkip_mic_key, 8);
1595 			break;
1596 		}
1597 
1598 		key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1599 		if (IS_ERR(key))
1600 			return false;
1601 		iwl_mvm_set_key_rx_seq(mvm, key, status);
1602 
1603 		replay_ctr =
1604 			cpu_to_be64(le64_to_cpu(status->replay_ctr));
1605 
1606 		ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1607 					   (void *)&replay_ctr, GFP_KERNEL);
1608 	}
1609 
1610 out:
1611 	mvmvif->seqno_valid = true;
1612 	/* +0x10 because the set API expects next-to-use, not last-used */
1613 	mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1614 
1615 	return true;
1616 }
1617 
1618 /* Occasionally, templates would be nice. This is one of those times ... */
1619 #define iwl_mvm_parse_wowlan_status_common(_ver)			\
1620 static struct iwl_wowlan_status *					\
1621 iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm,	\
1622 					    void *_data, int len)	\
1623 {									\
1624 	struct iwl_wowlan_status *status;				\
1625 	struct iwl_wowlan_status_ ##_ver *data = _data;			\
1626 	int data_size;							\
1627 									\
1628 	if (len < sizeof(*data)) {					\
1629 		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
1630 		return ERR_PTR(-EIO);					\
1631 	}								\
1632 									\
1633 	data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4);	\
1634 	if (len != sizeof(*data) + data_size) {				\
1635 		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
1636 		return ERR_PTR(-EIO);					\
1637 	}								\
1638 									\
1639 	status = kzalloc(sizeof(*status) + data_size, GFP_KERNEL);	\
1640 	if (!status)							\
1641 		return ERR_PTR(-ENOMEM);				\
1642 									\
1643 	/* copy all the common fields */				\
1644 	status->replay_ctr = data->replay_ctr;				\
1645 	status->pattern_number = data->pattern_number;			\
1646 	status->non_qos_seq_ctr = data->non_qos_seq_ctr;		\
1647 	memcpy(status->qos_seq_ctr, data->qos_seq_ctr,			\
1648 	       sizeof(status->qos_seq_ctr));				\
1649 	status->wakeup_reasons = data->wakeup_reasons;			\
1650 	status->num_of_gtk_rekeys = data->num_of_gtk_rekeys;		\
1651 	status->received_beacons = data->received_beacons;		\
1652 	status->wake_packet_length = data->wake_packet_length;		\
1653 	status->wake_packet_bufsize = data->wake_packet_bufsize;	\
1654 	memcpy(status->wake_packet, data->wake_packet,			\
1655 	       le32_to_cpu(status->wake_packet_bufsize));		\
1656 									\
1657 	return status;							\
1658 }
1659 
1660 iwl_mvm_parse_wowlan_status_common(v6)
iwl_mvm_parse_wowlan_status_common(v7)1661 iwl_mvm_parse_wowlan_status_common(v7)
1662 iwl_mvm_parse_wowlan_status_common(v9)
1663 
1664 struct iwl_wowlan_status *iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm)
1665 {
1666 	struct iwl_wowlan_status *status;
1667 	struct iwl_host_cmd cmd = {
1668 		.id = WOWLAN_GET_STATUSES,
1669 		.flags = CMD_WANT_SKB,
1670 	};
1671 	int ret, len;
1672 	u8 notif_ver;
1673 
1674 	lockdep_assert_held(&mvm->mutex);
1675 
1676 	ret = iwl_mvm_send_cmd(mvm, &cmd);
1677 	if (ret) {
1678 		IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
1679 		return ERR_PTR(ret);
1680 	}
1681 
1682 	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1683 
1684 	/* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
1685 	notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1686 					    WOWLAN_GET_STATUSES, 7);
1687 
1688 	if (!fw_has_api(&mvm->fw->ucode_capa,
1689 			IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL)) {
1690 		struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
1691 
1692 		status = iwl_mvm_parse_wowlan_status_common_v6(mvm,
1693 							       cmd.resp_pkt->data,
1694 							       len);
1695 		if (IS_ERR(status))
1696 			goto out_free_resp;
1697 
1698 		BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
1699 			     sizeof(status->gtk[0].key));
1700 		BUILD_BUG_ON(sizeof(v6->gtk.tkip_mic_key) >
1701 			     sizeof(status->gtk[0].tkip_mic_key));
1702 
1703 		/* copy GTK info to the right place */
1704 		memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
1705 		       sizeof(v6->gtk.decrypt_key));
1706 		memcpy(status->gtk[0].tkip_mic_key, v6->gtk.tkip_mic_key,
1707 		       sizeof(v6->gtk.tkip_mic_key));
1708 		memcpy(&status->gtk[0].rsc, &v6->gtk.rsc,
1709 		       sizeof(status->gtk[0].rsc));
1710 
1711 		/* hardcode the key length to 16 since v6 only supports 16 */
1712 		status->gtk[0].key_len = 16;
1713 
1714 		/*
1715 		 * The key index only uses 2 bits (values 0 to 3) and
1716 		 * we always set bit 7 which means this is the
1717 		 * currently used key.
1718 		 */
1719 		status->gtk[0].key_flags = v6->gtk.key_index | BIT(7);
1720 	} else if (notif_ver == 7) {
1721 		struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
1722 
1723 		status = iwl_mvm_parse_wowlan_status_common_v7(mvm,
1724 							       cmd.resp_pkt->data,
1725 							       len);
1726 		if (IS_ERR(status))
1727 			goto out_free_resp;
1728 
1729 		status->gtk[0] = v7->gtk[0];
1730 		status->igtk[0] = v7->igtk[0];
1731 	} else if (notif_ver == 9) {
1732 		struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data;
1733 
1734 		status = iwl_mvm_parse_wowlan_status_common_v9(mvm,
1735 							       cmd.resp_pkt->data,
1736 							       len);
1737 		if (IS_ERR(status))
1738 			goto out_free_resp;
1739 
1740 		status->gtk[0] = v9->gtk[0];
1741 		status->igtk[0] = v9->igtk[0];
1742 
1743 		status->tid_tear_down = v9->tid_tear_down;
1744 	} else {
1745 		IWL_ERR(mvm,
1746 			"Firmware advertises unknown WoWLAN status response %d!\n",
1747 			notif_ver);
1748 		status = ERR_PTR(-EIO);
1749 	}
1750 
1751 out_free_resp:
1752 	iwl_free_resp(&cmd);
1753 	return status;
1754 }
1755 
1756 static struct iwl_wowlan_status *
iwl_mvm_get_wakeup_status(struct iwl_mvm * mvm)1757 iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm)
1758 {
1759 	int ret;
1760 
1761 	/* only for tracing for now */
1762 	ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1763 	if (ret)
1764 		IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1765 
1766 	return iwl_mvm_send_wowlan_get_status(mvm);
1767 }
1768 
1769 /* releases the MVM mutex */
iwl_mvm_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1770 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1771 					 struct ieee80211_vif *vif)
1772 {
1773 	struct iwl_wowlan_status_data status;
1774 	struct iwl_wowlan_status *fw_status;
1775 	int i;
1776 	bool keep;
1777 	struct iwl_mvm_sta *mvm_ap_sta;
1778 
1779 	fw_status = iwl_mvm_get_wakeup_status(mvm);
1780 	if (IS_ERR_OR_NULL(fw_status))
1781 		goto out_unlock;
1782 
1783 	IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
1784 			 le32_to_cpu(fw_status->wakeup_reasons));
1785 
1786 	status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1787 	for (i = 0; i < 8; i++)
1788 		status.qos_seq_ctr[i] =
1789 			le16_to_cpu(fw_status->qos_seq_ctr[i]);
1790 	status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1791 	status.wake_packet_length =
1792 		le32_to_cpu(fw_status->wake_packet_length);
1793 	status.wake_packet_bufsize =
1794 		le32_to_cpu(fw_status->wake_packet_bufsize);
1795 	status.wake_packet = fw_status->wake_packet;
1796 
1797 	/* still at hard-coded place 0 for D3 image */
1798 	mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
1799 	if (!mvm_ap_sta)
1800 		goto out_free;
1801 
1802 	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1803 		u16 seq = status.qos_seq_ctr[i];
1804 		/* firmware stores last-used value, we store next value */
1805 		seq += 0x10;
1806 		mvm_ap_sta->tid_data[i].seq_number = seq;
1807 	}
1808 
1809 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
1810 		i = mvm->offload_tid;
1811 		iwl_trans_set_q_ptrs(mvm->trans,
1812 				     mvm_ap_sta->tid_data[i].txq_id,
1813 				     mvm_ap_sta->tid_data[i].seq_number >> 4);
1814 	}
1815 
1816 	/* now we have all the data we need, unlock to avoid mac80211 issues */
1817 	mutex_unlock(&mvm->mutex);
1818 
1819 	iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1820 
1821 	keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1822 
1823 	kfree(fw_status);
1824 	return keep;
1825 
1826 out_free:
1827 	kfree(fw_status);
1828 out_unlock:
1829 	mutex_unlock(&mvm->mutex);
1830 	return false;
1831 }
1832 
1833 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
1834 			  IWL_SCAN_MAX_PROFILES)
1835 
1836 struct iwl_mvm_nd_query_results {
1837 	u32 matched_profiles;
1838 	u8 matches[ND_QUERY_BUF_LEN];
1839 };
1840 
1841 static int
iwl_mvm_netdetect_query_results(struct iwl_mvm * mvm,struct iwl_mvm_nd_query_results * results)1842 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1843 				struct iwl_mvm_nd_query_results *results)
1844 {
1845 	struct iwl_scan_offload_profiles_query *query;
1846 	struct iwl_host_cmd cmd = {
1847 		.id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1848 		.flags = CMD_WANT_SKB,
1849 	};
1850 	int ret, len;
1851 	size_t query_len, matches_len;
1852 	int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
1853 
1854 	ret = iwl_mvm_send_cmd(mvm, &cmd);
1855 	if (ret) {
1856 		IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
1857 		return ret;
1858 	}
1859 
1860 	if (fw_has_api(&mvm->fw->ucode_capa,
1861 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
1862 		query_len = sizeof(struct iwl_scan_offload_profiles_query);
1863 		matches_len = sizeof(struct iwl_scan_offload_profile_match) *
1864 			max_profiles;
1865 	} else {
1866 		query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
1867 		matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
1868 			max_profiles;
1869 	}
1870 
1871 	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1872 	if (len < query_len) {
1873 		IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
1874 		ret = -EIO;
1875 		goto out_free_resp;
1876 	}
1877 
1878 	query = (void *)cmd.resp_pkt->data;
1879 
1880 	results->matched_profiles = le32_to_cpu(query->matched_profiles);
1881 	memcpy(results->matches, query->matches, matches_len);
1882 
1883 #ifdef CONFIG_IWLWIFI_DEBUGFS
1884 	mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1885 #endif
1886 
1887 out_free_resp:
1888 	iwl_free_resp(&cmd);
1889 	return ret;
1890 }
1891 
iwl_mvm_query_num_match_chans(struct iwl_mvm * mvm,struct iwl_mvm_nd_query_results * query,int idx)1892 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
1893 					 struct iwl_mvm_nd_query_results *query,
1894 					 int idx)
1895 {
1896 	int n_chans = 0, i;
1897 
1898 	if (fw_has_api(&mvm->fw->ucode_capa,
1899 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
1900 		struct iwl_scan_offload_profile_match *matches =
1901 			(struct iwl_scan_offload_profile_match *)query->matches;
1902 
1903 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
1904 			n_chans += hweight8(matches[idx].matching_channels[i]);
1905 	} else {
1906 		struct iwl_scan_offload_profile_match_v1 *matches =
1907 			(struct iwl_scan_offload_profile_match_v1 *)query->matches;
1908 
1909 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
1910 			n_chans += hweight8(matches[idx].matching_channels[i]);
1911 	}
1912 
1913 	return n_chans;
1914 }
1915 
iwl_mvm_query_set_freqs(struct iwl_mvm * mvm,struct iwl_mvm_nd_query_results * query,struct cfg80211_wowlan_nd_match * match,int idx)1916 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
1917 				    struct iwl_mvm_nd_query_results *query,
1918 				    struct cfg80211_wowlan_nd_match *match,
1919 				    int idx)
1920 {
1921 	int i;
1922 
1923 	if (fw_has_api(&mvm->fw->ucode_capa,
1924 		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
1925 		struct iwl_scan_offload_profile_match *matches =
1926 			(struct iwl_scan_offload_profile_match *)query->matches;
1927 
1928 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
1929 			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
1930 				match->channels[match->n_channels++] =
1931 					mvm->nd_channels[i]->center_freq;
1932 	} else {
1933 		struct iwl_scan_offload_profile_match_v1 *matches =
1934 			(struct iwl_scan_offload_profile_match_v1 *)query->matches;
1935 
1936 		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
1937 			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
1938 				match->channels[match->n_channels++] =
1939 					mvm->nd_channels[i]->center_freq;
1940 	}
1941 }
1942 
iwl_mvm_query_netdetect_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1943 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1944 					    struct ieee80211_vif *vif)
1945 {
1946 	struct cfg80211_wowlan_nd_info *net_detect = NULL;
1947 	struct cfg80211_wowlan_wakeup wakeup = {
1948 		.pattern_idx = -1,
1949 	};
1950 	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1951 	struct iwl_mvm_nd_query_results query;
1952 	struct iwl_wowlan_status *fw_status;
1953 	unsigned long matched_profiles;
1954 	u32 reasons = 0;
1955 	int i, n_matches, ret;
1956 
1957 	fw_status = iwl_mvm_get_wakeup_status(mvm);
1958 	if (!IS_ERR_OR_NULL(fw_status)) {
1959 		reasons = le32_to_cpu(fw_status->wakeup_reasons);
1960 		kfree(fw_status);
1961 	}
1962 
1963 	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1964 		wakeup.rfkill_release = true;
1965 
1966 	if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1967 		goto out;
1968 
1969 	ret = iwl_mvm_netdetect_query_results(mvm, &query);
1970 	if (ret || !query.matched_profiles) {
1971 		wakeup_report = NULL;
1972 		goto out;
1973 	}
1974 
1975 	matched_profiles = query.matched_profiles;
1976 	if (mvm->n_nd_match_sets) {
1977 		n_matches = hweight_long(matched_profiles);
1978 	} else {
1979 		IWL_ERR(mvm, "no net detect match information available\n");
1980 		n_matches = 0;
1981 	}
1982 
1983 	net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
1984 			     GFP_KERNEL);
1985 	if (!net_detect || !n_matches)
1986 		goto out_report_nd;
1987 
1988 	for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
1989 		struct cfg80211_wowlan_nd_match *match;
1990 		int idx, n_channels = 0;
1991 
1992 		n_channels = iwl_mvm_query_num_match_chans(mvm, &query, i);
1993 
1994 		match = kzalloc(struct_size(match, channels, n_channels),
1995 				GFP_KERNEL);
1996 		if (!match)
1997 			goto out_report_nd;
1998 
1999 		net_detect->matches[net_detect->n_matches++] = match;
2000 
2001 		/* We inverted the order of the SSIDs in the scan
2002 		 * request, so invert the index here.
2003 		 */
2004 		idx = mvm->n_nd_match_sets - i - 1;
2005 		match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2006 		memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2007 		       match->ssid.ssid_len);
2008 
2009 		if (mvm->n_nd_channels < n_channels)
2010 			continue;
2011 
2012 		iwl_mvm_query_set_freqs(mvm, &query, match, i);
2013 	}
2014 
2015 out_report_nd:
2016 	wakeup.net_detect = net_detect;
2017 out:
2018 	iwl_mvm_free_nd(mvm);
2019 
2020 	mutex_unlock(&mvm->mutex);
2021 	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2022 
2023 	if (net_detect) {
2024 		for (i = 0; i < net_detect->n_matches; i++)
2025 			kfree(net_detect->matches[i]);
2026 		kfree(net_detect);
2027 	}
2028 }
2029 
iwl_mvm_d3_disconnect_iter(void * data,u8 * mac,struct ieee80211_vif * vif)2030 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2031 				       struct ieee80211_vif *vif)
2032 {
2033 	/* skip the one we keep connection on */
2034 	if (data == vif)
2035 		return;
2036 
2037 	if (vif->type == NL80211_IFTYPE_STATION)
2038 		ieee80211_resume_disconnect(vif);
2039 }
2040 
iwl_mvm_rt_status(struct iwl_trans * trans,u32 base,u32 * err_id)2041 static bool iwl_mvm_rt_status(struct iwl_trans *trans, u32 base, u32 *err_id)
2042 {
2043 	struct error_table_start {
2044 		/* cf. struct iwl_error_event_table */
2045 		u32 valid;
2046 		__le32 err_id;
2047 	} err_info;
2048 
2049 	if (!base)
2050 		return false;
2051 
2052 	iwl_trans_read_mem_bytes(trans, base,
2053 				 &err_info, sizeof(err_info));
2054 	if (err_info.valid && err_id)
2055 		*err_id = le32_to_cpu(err_info.err_id);
2056 
2057 	return !!err_info.valid;
2058 }
2059 
iwl_mvm_check_rt_status(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2060 static bool iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
2061 				   struct ieee80211_vif *vif)
2062 {
2063 	u32 err_id;
2064 
2065 	/* check for lmac1 error */
2066 	if (iwl_mvm_rt_status(mvm->trans,
2067 			      mvm->trans->dbg.lmac_error_event_table[0],
2068 			      &err_id)) {
2069 		if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
2070 			struct cfg80211_wowlan_wakeup wakeup = {
2071 				.rfkill_release = true,
2072 			};
2073 			ieee80211_report_wowlan_wakeup(vif, &wakeup,
2074 						       GFP_KERNEL);
2075 		}
2076 		return true;
2077 	}
2078 
2079 	/* check if we have lmac2 set and check for error */
2080 	if (iwl_mvm_rt_status(mvm->trans,
2081 			      mvm->trans->dbg.lmac_error_event_table[1], NULL))
2082 		return true;
2083 
2084 	/* check for umac error */
2085 	if (iwl_mvm_rt_status(mvm->trans,
2086 			      mvm->trans->dbg.umac_error_event_table, NULL))
2087 		return true;
2088 
2089 	return false;
2090 }
2091 
__iwl_mvm_resume(struct iwl_mvm * mvm,bool test)2092 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
2093 {
2094 	struct ieee80211_vif *vif = NULL;
2095 	int ret = 1;
2096 	enum iwl_d3_status d3_status;
2097 	bool keep = false;
2098 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2099 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2100 	bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
2101 				      IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
2102 
2103 	mutex_lock(&mvm->mutex);
2104 
2105 	clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
2106 
2107 	/* get the BSS vif pointer again */
2108 	vif = iwl_mvm_get_bss_vif(mvm);
2109 	if (IS_ERR_OR_NULL(vif))
2110 		goto err;
2111 
2112 	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
2113 
2114 	if (iwl_mvm_check_rt_status(mvm, vif)) {
2115 		set_bit(STATUS_FW_ERROR, &mvm->trans->status);
2116 		iwl_mvm_dump_nic_error_log(mvm);
2117 		iwl_dbg_tlv_time_point(&mvm->fwrt,
2118 				       IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL);
2119 		iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
2120 					false, 0);
2121 		ret = 1;
2122 		goto err;
2123 	}
2124 
2125 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_HOST_D3_END,
2126 			       NULL);
2127 
2128 	ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
2129 	if (ret)
2130 		goto err;
2131 
2132 	if (d3_status != IWL_D3_STATUS_ALIVE) {
2133 		IWL_INFO(mvm, "Device was reset during suspend\n");
2134 		goto err;
2135 	}
2136 
2137 	if (d0i3_first) {
2138 		struct iwl_host_cmd cmd = {
2139 			.id = D0I3_END_CMD,
2140 			.flags = CMD_WANT_SKB,
2141 		};
2142 		int len;
2143 
2144 		ret = iwl_mvm_send_cmd(mvm, &cmd);
2145 		if (ret < 0) {
2146 			IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
2147 				ret);
2148 			goto err;
2149 		}
2150 		switch (mvm->cmd_ver.d0i3_resp) {
2151 		case 0:
2152 			break;
2153 		case 1:
2154 			len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2155 			if (len != sizeof(u32)) {
2156 				IWL_ERR(mvm,
2157 					"Error with D0I3_END_CMD response size (%d)\n",
2158 					len);
2159 				goto err;
2160 			}
2161 			if (IWL_D0I3_RESET_REQUIRE &
2162 			    le32_to_cpu(*(__le32 *)cmd.resp_pkt->data)) {
2163 				iwl_write32(mvm->trans, CSR_RESET,
2164 					    CSR_RESET_REG_FLAG_FORCE_NMI);
2165 				iwl_free_resp(&cmd);
2166 			}
2167 			break;
2168 		default:
2169 			WARN_ON(1);
2170 		}
2171 	}
2172 
2173 	/*
2174 	 * Query the current location and source from the D3 firmware so we
2175 	 * can play it back when we re-intiailize the D0 firmware
2176 	 */
2177 	iwl_mvm_update_changed_regdom(mvm);
2178 
2179 	/* Re-configure PPAG settings */
2180 	iwl_mvm_ppag_send_cmd(mvm);
2181 
2182 	if (!unified_image)
2183 		/*  Re-configure default SAR profile */
2184 		iwl_mvm_sar_select_profile(mvm, 1, 1);
2185 
2186 	if (mvm->net_detect) {
2187 		/* If this is a non-unified image, we restart the FW,
2188 		 * so no need to stop the netdetect scan.  If that
2189 		 * fails, continue and try to get the wake-up reasons,
2190 		 * but trigger a HW restart by keeping a failure code
2191 		 * in ret.
2192 		 */
2193 		if (unified_image)
2194 			ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
2195 						false);
2196 
2197 		iwl_mvm_query_netdetect_reasons(mvm, vif);
2198 		/* has unlocked the mutex, so skip that */
2199 		goto out;
2200 	} else {
2201 		keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
2202 #ifdef CONFIG_IWLWIFI_DEBUGFS
2203 		if (keep)
2204 			mvm->keep_vif = vif;
2205 #endif
2206 		/* has unlocked the mutex, so skip that */
2207 		goto out_iterate;
2208 	}
2209 
2210 err:
2211 	iwl_mvm_free_nd(mvm);
2212 	mutex_unlock(&mvm->mutex);
2213 
2214 out_iterate:
2215 	if (!test)
2216 		ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
2217 			IEEE80211_IFACE_ITER_NORMAL,
2218 			iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
2219 
2220 out:
2221 	/* no need to reset the device in unified images, if successful */
2222 	if (unified_image && !ret) {
2223 		/* nothing else to do if we already sent D0I3_END_CMD */
2224 		if (d0i3_first)
2225 			return 0;
2226 
2227 		ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
2228 		if (!ret)
2229 			return 0;
2230 	}
2231 
2232 	/*
2233 	 * Reconfigure the device in one of the following cases:
2234 	 * 1. We are not using a unified image
2235 	 * 2. We are using a unified image but had an error while exiting D3
2236 	 */
2237 	set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
2238 
2239 	return 1;
2240 }
2241 
iwl_mvm_resume_d3(struct iwl_mvm * mvm)2242 static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
2243 {
2244 	iwl_trans_resume(mvm->trans);
2245 
2246 	return __iwl_mvm_resume(mvm, false);
2247 }
2248 
iwl_mvm_resume(struct ieee80211_hw * hw)2249 int iwl_mvm_resume(struct ieee80211_hw *hw)
2250 {
2251 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2252 	int ret;
2253 
2254 	ret = iwl_mvm_resume_d3(mvm);
2255 
2256 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2257 
2258 	iwl_mvm_resume_tcm(mvm);
2259 
2260 	iwl_fw_runtime_resume(&mvm->fwrt);
2261 
2262 	return ret;
2263 }
2264 
iwl_mvm_set_wakeup(struct ieee80211_hw * hw,bool enabled)2265 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2266 {
2267 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2268 
2269 	device_set_wakeup_enable(mvm->trans->dev, enabled);
2270 }
2271 
2272 #ifdef CONFIG_IWLWIFI_DEBUGFS
iwl_mvm_d3_test_open(struct inode * inode,struct file * file)2273 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2274 {
2275 	struct iwl_mvm *mvm = inode->i_private;
2276 	int err;
2277 
2278 	if (mvm->d3_test_active)
2279 		return -EBUSY;
2280 
2281 	file->private_data = inode->i_private;
2282 
2283 	synchronize_net();
2284 
2285 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
2286 
2287 	iwl_mvm_pause_tcm(mvm, true);
2288 
2289 	iwl_fw_runtime_suspend(&mvm->fwrt);
2290 
2291 	/* start pseudo D3 */
2292 	rtnl_lock();
2293 	err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2294 	rtnl_unlock();
2295 	if (err > 0)
2296 		err = -EINVAL;
2297 	if (err)
2298 		return err;
2299 
2300 	mvm->d3_test_active = true;
2301 	mvm->keep_vif = NULL;
2302 	return 0;
2303 }
2304 
iwl_mvm_d3_test_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)2305 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2306 				    size_t count, loff_t *ppos)
2307 {
2308 	struct iwl_mvm *mvm = file->private_data;
2309 	u32 pme_asserted;
2310 
2311 	while (true) {
2312 		/* read pme_ptr if available */
2313 		if (mvm->d3_test_pme_ptr) {
2314 			pme_asserted = iwl_trans_read_mem32(mvm->trans,
2315 						mvm->d3_test_pme_ptr);
2316 			if (pme_asserted)
2317 				break;
2318 		}
2319 
2320 		if (msleep_interruptible(100))
2321 			break;
2322 	}
2323 
2324 	return 0;
2325 }
2326 
iwl_mvm_d3_test_disconn_work_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)2327 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2328 					      struct ieee80211_vif *vif)
2329 {
2330 	/* skip the one we keep connection on */
2331 	if (_data == vif)
2332 		return;
2333 
2334 	if (vif->type == NL80211_IFTYPE_STATION)
2335 		ieee80211_connection_loss(vif);
2336 }
2337 
iwl_mvm_d3_test_release(struct inode * inode,struct file * file)2338 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2339 {
2340 	struct iwl_mvm *mvm = inode->i_private;
2341 	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2342 					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2343 
2344 	mvm->d3_test_active = false;
2345 
2346 	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
2347 
2348 	rtnl_lock();
2349 	__iwl_mvm_resume(mvm, true);
2350 	rtnl_unlock();
2351 
2352 	iwl_mvm_resume_tcm(mvm);
2353 
2354 	iwl_fw_runtime_resume(&mvm->fwrt);
2355 
2356 	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2357 
2358 	iwl_abort_notification_waits(&mvm->notif_wait);
2359 	if (!unified_image) {
2360 		int remaining_time = 10;
2361 
2362 		ieee80211_restart_hw(mvm->hw);
2363 
2364 		/* wait for restart and disconnect all interfaces */
2365 		while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2366 		       remaining_time > 0) {
2367 			remaining_time--;
2368 			msleep(1000);
2369 		}
2370 
2371 		if (remaining_time == 0)
2372 			IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
2373 	}
2374 
2375 	ieee80211_iterate_active_interfaces_atomic(
2376 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2377 		iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
2378 
2379 	return 0;
2380 }
2381 
2382 const struct file_operations iwl_dbgfs_d3_test_ops = {
2383 	.llseek = no_llseek,
2384 	.open = iwl_mvm_d3_test_open,
2385 	.read = iwl_mvm_d3_test_read,
2386 	.release = iwl_mvm_d3_test_release,
2387 };
2388 #endif
2389