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