]> git.ipfire.org Git - people/ms/linux.git/blame - drivers/net/wireless/iwlwifi/mvm/d3.c
iwlwifi: trans: add suspend/resume ops
[people/ms/linux.git] / drivers / net / wireless / 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.
8b4139dc 9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
8ca151b5
JB
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
410dc5aa 26 * in the file called COPYING.
8ca151b5
JB
27 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
51368bf7 34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
8b4139dc 35 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
8ca151b5
JB
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65
f444eb10 66#include <linux/etherdevice.h>
f0c2646a 67#include <linux/ip.h>
debff618 68#include <linux/fs.h>
8ca151b5
JB
69#include <net/cfg80211.h>
70#include <net/ipv6.h>
f0c2646a 71#include <net/tcp.h>
f7fc5989 72#include <net/addrconf.h>
8ca151b5
JB
73#include "iwl-modparams.h"
74#include "fw-api.h"
75#include "mvm.h"
76
77void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
78 struct ieee80211_vif *vif,
79 struct cfg80211_gtk_rekey_data *data)
80{
81 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
82 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
83
84 if (iwlwifi_mod_params.sw_crypto)
85 return;
86
87 mutex_lock(&mvm->mutex);
88
89 memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
90 memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
91 mvmvif->rekey_data.replay_ctr =
92 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
93 mvmvif->rekey_data.valid = true;
94
95 mutex_unlock(&mvm->mutex);
96}
97
98#if IS_ENABLED(CONFIG_IPV6)
99void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
100 struct ieee80211_vif *vif,
101 struct inet6_dev *idev)
102{
103 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
104 struct inet6_ifaddr *ifa;
105 int idx = 0;
106
a3777e0f 107 read_lock_bh(&idev->lock);
8ca151b5
JB
108 list_for_each_entry(ifa, &idev->addr_list, if_list) {
109 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
110 idx++;
5369d6c1 111 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
8ca151b5
JB
112 break;
113 }
a3777e0f 114 read_unlock_bh(&idev->lock);
8ca151b5
JB
115
116 mvmvif->num_target_ipv6_addrs = idx;
117}
118#endif
119
120void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
121 struct ieee80211_vif *vif, int idx)
122{
123 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
124
125 mvmvif->tx_key_idx = idx;
126}
127
128static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
129{
130 int i;
131
132 for (i = 0; i < IWL_P1K_SIZE; i++)
133 out[i] = cpu_to_le16(p1k[i]);
134}
135
136struct wowlan_key_data {
137 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
138 struct iwl_wowlan_tkip_params_cmd *tkip;
139 bool error, use_rsc_tsc, use_tkip;
5312e54d 140 int wep_key_idx;
8ca151b5
JB
141};
142
143static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
144 struct ieee80211_vif *vif,
145 struct ieee80211_sta *sta,
146 struct ieee80211_key_conf *key,
147 void *_data)
148{
149 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
150 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
151 struct wowlan_key_data *data = _data;
152 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
153 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
154 struct iwl_p1k_cache *rx_p1ks;
155 u8 *rx_mic_key;
156 struct ieee80211_key_seq seq;
157 u32 cur_rx_iv32 = 0;
158 u16 p1k[IWL_P1K_SIZE];
159 int ret, i;
160
161 mutex_lock(&mvm->mutex);
162
163 switch (key->cipher) {
164 case WLAN_CIPHER_SUITE_WEP40:
165 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
166 struct {
167 struct iwl_mvm_wep_key_cmd wep_key_cmd;
168 struct iwl_mvm_wep_key wep_key;
169 } __packed wkc = {
170 .wep_key_cmd.mac_id_n_color =
171 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
172 mvmvif->color)),
173 .wep_key_cmd.num_keys = 1,
174 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
175 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
176 .wep_key.key_index = key->keyidx,
177 .wep_key.key_size = key->keylen,
178 };
179
180 /*
181 * This will fail -- the key functions don't set support
182 * pairwise WEP keys. However, that's better than silently
183 * failing WoWLAN. Or maybe not?
184 */
185 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
186 break;
187
188 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
189 if (key->keyidx == mvmvif->tx_key_idx) {
190 /* TX key must be at offset 0 */
191 wkc.wep_key.key_offset = 0;
192 } else {
193 /* others start at 1 */
5312e54d
JB
194 data->wep_key_idx++;
195 wkc.wep_key.key_offset = data->wep_key_idx;
8ca151b5
JB
196 }
197
a1022927 198 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
8ca151b5
JB
199 data->error = ret != 0;
200
f444eb10
JB
201 mvm->ptk_ivlen = key->iv_len;
202 mvm->ptk_icvlen = key->icv_len;
203 mvm->gtk_ivlen = key->iv_len;
204 mvm->gtk_icvlen = key->icv_len;
205
8ca151b5
JB
206 /* don't upload key again */
207 goto out_unlock;
208 }
209 default:
210 data->error = true;
211 goto out_unlock;
212 case WLAN_CIPHER_SUITE_AES_CMAC:
213 /*
214 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
215 * but we also shouldn't abort suspend due to that. It does have
216 * support for the IGTK key renewal, but doesn't really use the
217 * IGTK for anything. This means we could spuriously wake up or
218 * be deauthenticated, but that was considered acceptable.
219 */
220 goto out_unlock;
221 case WLAN_CIPHER_SUITE_TKIP:
222 if (sta) {
223 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
224 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
225
226 rx_p1ks = data->tkip->rx_uni;
227
228 ieee80211_get_key_tx_seq(key, &seq);
229 tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
230 tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
231
232 ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
233 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
234
235 memcpy(data->tkip->mic_keys.tx,
236 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
237 IWL_MIC_KEY_SIZE);
238
239 rx_mic_key = data->tkip->mic_keys.rx_unicast;
240 } else {
241 tkip_sc =
242 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
243 rx_p1ks = data->tkip->rx_multi;
244 rx_mic_key = data->tkip->mic_keys.rx_mcast;
245 }
246
247 /*
248 * For non-QoS this relies on the fact that both the uCode and
249 * mac80211 use TID 0 (as they need to to avoid replay attacks)
250 * for checking the IV in the frames.
251 */
252 for (i = 0; i < IWL_NUM_RSC; i++) {
253 ieee80211_get_key_rx_seq(key, i, &seq);
254 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
255 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
256 /* wrapping isn't allowed, AP must rekey */
257 if (seq.tkip.iv32 > cur_rx_iv32)
258 cur_rx_iv32 = seq.tkip.iv32;
259 }
260
261 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
262 cur_rx_iv32, p1k);
263 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
264 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
265 cur_rx_iv32 + 1, p1k);
266 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
267
268 memcpy(rx_mic_key,
269 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
270 IWL_MIC_KEY_SIZE);
271
272 data->use_tkip = true;
273 data->use_rsc_tsc = true;
274 break;
275 case WLAN_CIPHER_SUITE_CCMP:
276 if (sta) {
277 u8 *pn = seq.ccmp.pn;
278
279 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
280 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
281
282 ieee80211_get_key_tx_seq(key, &seq);
283 aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
284 ((u64)pn[4] << 8) |
285 ((u64)pn[3] << 16) |
286 ((u64)pn[2] << 24) |
287 ((u64)pn[1] << 32) |
288 ((u64)pn[0] << 40));
289 } else {
290 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
291 }
292
293 /*
294 * For non-QoS this relies on the fact that both the uCode and
295 * mac80211 use TID 0 for checking the IV in the frames.
296 */
297 for (i = 0; i < IWL_NUM_RSC; i++) {
298 u8 *pn = seq.ccmp.pn;
299
300 ieee80211_get_key_rx_seq(key, i, &seq);
301 aes_sc->pn = cpu_to_le64((u64)pn[5] |
302 ((u64)pn[4] << 8) |
303 ((u64)pn[3] << 16) |
304 ((u64)pn[2] << 24) |
305 ((u64)pn[1] << 32) |
306 ((u64)pn[0] << 40));
307 }
308 data->use_rsc_tsc = true;
309 break;
310 }
311
312 /*
313 * The D3 firmware hardcodes the key offset 0 as the key it uses
314 * to transmit packets to the AP, i.e. the PTK.
315 */
316 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
317 key->hw_key_idx = 0;
f444eb10
JB
318 mvm->ptk_ivlen = key->iv_len;
319 mvm->ptk_icvlen = key->icv_len;
8ca151b5 320 } else {
5312e54d
JB
321 /*
322 * firmware only supports TSC/RSC for a single key,
323 * so if there are multiple keep overwriting them
324 * with new ones -- this relies on mac80211 doing
325 * list_add_tail().
326 */
327 key->hw_key_idx = 1;
f444eb10
JB
328 mvm->gtk_ivlen = key->iv_len;
329 mvm->gtk_icvlen = key->icv_len;
8ca151b5
JB
330 }
331
332 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
333 data->error = ret != 0;
334out_unlock:
335 mutex_unlock(&mvm->mutex);
336}
337
338static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
339 struct cfg80211_wowlan *wowlan)
340{
341 struct iwl_wowlan_patterns_cmd *pattern_cmd;
342 struct iwl_host_cmd cmd = {
343 .id = WOWLAN_PATTERNS,
344 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
8ca151b5
JB
345 };
346 int i, err;
347
348 if (!wowlan->n_patterns)
349 return 0;
350
351 cmd.len[0] = sizeof(*pattern_cmd) +
352 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
353
354 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
355 if (!pattern_cmd)
356 return -ENOMEM;
357
358 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
359
360 for (i = 0; i < wowlan->n_patterns; i++) {
361 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
362
363 memcpy(&pattern_cmd->patterns[i].mask,
364 wowlan->patterns[i].mask, mask_len);
365 memcpy(&pattern_cmd->patterns[i].pattern,
366 wowlan->patterns[i].pattern,
367 wowlan->patterns[i].pattern_len);
368 pattern_cmd->patterns[i].mask_size = mask_len;
369 pattern_cmd->patterns[i].pattern_size =
370 wowlan->patterns[i].pattern_len;
371 }
372
373 cmd.data[0] = pattern_cmd;
374 err = iwl_mvm_send_cmd(mvm, &cmd);
375 kfree(pattern_cmd);
376 return err;
377}
378
f0c2646a
JB
379enum iwl_mvm_tcp_packet_type {
380 MVM_TCP_TX_SYN,
381 MVM_TCP_RX_SYNACK,
382 MVM_TCP_TX_DATA,
383 MVM_TCP_RX_ACK,
384 MVM_TCP_RX_WAKE,
385 MVM_TCP_TX_FIN,
386};
387
388static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
389{
390 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
391 return cpu_to_le16(be16_to_cpu((__force __be16)check));
392}
393
b002c7e1 394static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
f0c2646a
JB
395 struct cfg80211_wowlan_tcp *tcp,
396 void *_pkt, u8 *mask,
397 __le16 *pseudo_hdr_csum,
398 enum iwl_mvm_tcp_packet_type ptype)
399{
400 struct {
401 struct ethhdr eth;
402 struct iphdr ip;
403 struct tcphdr tcp;
404 u8 data[];
405 } __packed *pkt = _pkt;
406 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
407 int i;
408
409 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
410 pkt->ip.version = 4;
411 pkt->ip.ihl = 5;
412 pkt->ip.protocol = IPPROTO_TCP;
413
414 switch (ptype) {
415 case MVM_TCP_TX_SYN:
416 case MVM_TCP_TX_DATA:
417 case MVM_TCP_TX_FIN:
418 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
419 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
420 pkt->ip.ttl = 128;
421 pkt->ip.saddr = tcp->src;
422 pkt->ip.daddr = tcp->dst;
423 pkt->tcp.source = cpu_to_be16(tcp->src_port);
424 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
425 /* overwritten for TX SYN later */
426 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
427 pkt->tcp.window = cpu_to_be16(65000);
428 break;
429 case MVM_TCP_RX_SYNACK:
430 case MVM_TCP_RX_ACK:
431 case MVM_TCP_RX_WAKE:
432 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
433 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
434 pkt->ip.saddr = tcp->dst;
435 pkt->ip.daddr = tcp->src;
436 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
437 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
438 break;
439 default:
440 WARN_ON(1);
441 return;
442 }
443
444 switch (ptype) {
445 case MVM_TCP_TX_SYN:
446 /* firmware assumes 8 option bytes - 8 NOPs for now */
447 memset(pkt->data, 0x01, 8);
448 ip_tot_len += 8;
449 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
450 pkt->tcp.syn = 1;
451 break;
452 case MVM_TCP_TX_DATA:
453 ip_tot_len += tcp->payload_len;
454 memcpy(pkt->data, tcp->payload, tcp->payload_len);
455 pkt->tcp.psh = 1;
456 pkt->tcp.ack = 1;
457 break;
458 case MVM_TCP_TX_FIN:
459 pkt->tcp.fin = 1;
460 pkt->tcp.ack = 1;
461 break;
462 case MVM_TCP_RX_SYNACK:
463 pkt->tcp.syn = 1;
464 pkt->tcp.ack = 1;
465 break;
466 case MVM_TCP_RX_ACK:
467 pkt->tcp.ack = 1;
468 break;
469 case MVM_TCP_RX_WAKE:
470 ip_tot_len += tcp->wake_len;
471 pkt->tcp.psh = 1;
472 pkt->tcp.ack = 1;
473 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
474 break;
475 }
476
477 switch (ptype) {
478 case MVM_TCP_TX_SYN:
479 case MVM_TCP_TX_DATA:
480 case MVM_TCP_TX_FIN:
481 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
482 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
483 break;
484 case MVM_TCP_RX_WAKE:
485 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
486 u8 tmp = tcp->wake_mask[i];
487 mask[i + 6] |= tmp << 6;
488 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
489 mask[i + 7] = tmp >> 2;
490 }
491 /* fall through for ethernet/IP/TCP headers mask */
492 case MVM_TCP_RX_SYNACK:
493 case MVM_TCP_RX_ACK:
494 mask[0] = 0xff; /* match ethernet */
495 /*
496 * match ethernet, ip.version, ip.ihl
497 * the ip.ihl half byte is really masked out by firmware
498 */
499 mask[1] = 0x7f;
500 mask[2] = 0x80; /* match ip.protocol */
501 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
502 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
503 mask[5] = 0x80; /* match tcp flags */
504 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
505 break;
506 };
507
508 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
509 pkt->ip.saddr, pkt->ip.daddr);
510}
511
512static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
513 struct ieee80211_vif *vif,
514 struct cfg80211_wowlan_tcp *tcp)
515{
516 struct iwl_wowlan_remote_wake_config *cfg;
517 struct iwl_host_cmd cmd = {
518 .id = REMOTE_WAKE_CONFIG_CMD,
519 .len = { sizeof(*cfg), },
520 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
f0c2646a
JB
521 };
522 int ret;
523
524 if (!tcp)
525 return 0;
526
527 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
528 if (!cfg)
529 return -ENOMEM;
530 cmd.data[0] = cfg;
531
532 cfg->max_syn_retries = 10;
533 cfg->max_data_retries = 10;
534 cfg->tcp_syn_ack_timeout = 1; /* seconds */
535 cfg->tcp_ack_timeout = 1; /* seconds */
536
537 /* SYN (TX) */
538 iwl_mvm_build_tcp_packet(
b002c7e1 539 vif, tcp, cfg->syn_tx.data, NULL,
f0c2646a
JB
540 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
541 MVM_TCP_TX_SYN);
542 cfg->syn_tx.info.tcp_payload_length = 0;
543
544 /* SYN/ACK (RX) */
545 iwl_mvm_build_tcp_packet(
b002c7e1 546 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
f0c2646a
JB
547 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
548 MVM_TCP_RX_SYNACK);
549 cfg->synack_rx.info.tcp_payload_length = 0;
550
551 /* KEEPALIVE/ACK (TX) */
552 iwl_mvm_build_tcp_packet(
b002c7e1 553 vif, tcp, cfg->keepalive_tx.data, NULL,
f0c2646a
JB
554 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
555 MVM_TCP_TX_DATA);
556 cfg->keepalive_tx.info.tcp_payload_length =
557 cpu_to_le16(tcp->payload_len);
558 cfg->sequence_number_offset = tcp->payload_seq.offset;
559 /* length must be 0..4, the field is little endian */
560 cfg->sequence_number_length = tcp->payload_seq.len;
561 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
562 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
563 if (tcp->payload_tok.len) {
564 cfg->token_offset = tcp->payload_tok.offset;
565 cfg->token_length = tcp->payload_tok.len;
566 cfg->num_tokens =
567 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
568 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
569 tcp->tokens_size);
570 } else {
571 /* set tokens to max value to almost never run out */
572 cfg->num_tokens = cpu_to_le16(65535);
573 }
574
575 /* ACK (RX) */
576 iwl_mvm_build_tcp_packet(
b002c7e1 577 vif, tcp, cfg->keepalive_ack_rx.data,
f0c2646a
JB
578 cfg->keepalive_ack_rx.rx_mask,
579 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
580 MVM_TCP_RX_ACK);
581 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
582
583 /* WAKEUP (RX) */
584 iwl_mvm_build_tcp_packet(
b002c7e1 585 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
f0c2646a
JB
586 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
587 MVM_TCP_RX_WAKE);
588 cfg->wake_rx.info.tcp_payload_length =
589 cpu_to_le16(tcp->wake_len);
590
591 /* FIN */
592 iwl_mvm_build_tcp_packet(
b002c7e1 593 vif, tcp, cfg->fin_tx.data, NULL,
f0c2646a
JB
594 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
595 MVM_TCP_TX_FIN);
596 cfg->fin_tx.info.tcp_payload_length = 0;
597
598 ret = iwl_mvm_send_cmd(mvm, &cmd);
599 kfree(cfg);
600
601 return ret;
602}
603
8ca151b5
JB
604static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
605 struct ieee80211_sta *ap_sta)
606{
607 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
608 struct ieee80211_chanctx_conf *ctx;
609 u8 chains_static, chains_dynamic;
610 struct cfg80211_chan_def chandef;
611 int ret, i;
612 struct iwl_binding_cmd binding_cmd = {};
613 struct iwl_time_quota_cmd quota_cmd = {};
614 u32 status;
615
616 /* add back the PHY */
617 if (WARN_ON(!mvmvif->phy_ctxt))
618 return -EINVAL;
619
620 rcu_read_lock();
621 ctx = rcu_dereference(vif->chanctx_conf);
622 if (WARN_ON(!ctx)) {
623 rcu_read_unlock();
624 return -EINVAL;
625 }
626 chandef = ctx->def;
627 chains_static = ctx->rx_chains_static;
628 chains_dynamic = ctx->rx_chains_dynamic;
629 rcu_read_unlock();
630
631 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
632 chains_static, chains_dynamic);
633 if (ret)
634 return ret;
635
636 /* add back the MAC */
637 mvmvif->uploaded = false;
638
639 if (WARN_ON(!vif->bss_conf.assoc))
640 return -EINVAL;
bca49d9a 641
8ca151b5 642 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
8ca151b5
JB
643 if (ret)
644 return ret;
645
646 /* add back binding - XXX refactor? */
647 binding_cmd.id_and_color =
648 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
649 mvmvif->phy_ctxt->color));
650 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
651 binding_cmd.phy =
652 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
653 mvmvif->phy_ctxt->color));
654 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
655 mvmvif->color));
656 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
657 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
658
659 status = 0;
660 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
661 sizeof(binding_cmd), &binding_cmd,
662 &status);
663 if (ret) {
664 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
665 return ret;
666 }
667
668 if (status) {
669 IWL_ERR(mvm, "Binding command failed: %u\n", status);
670 return -EIO;
671 }
672
7a453973 673 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
8ca151b5
JB
674 if (ret)
675 return ret;
676 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
677
3dfd3a97 678 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
8ca151b5
JB
679 if (ret)
680 return ret;
681
682 /* and some quota */
683 quota_cmd.quotas[0].id_and_color =
684 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
685 mvmvif->phy_ctxt->color));
9a3daf82
EG
686 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
687 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
8ca151b5
JB
688
689 for (i = 1; i < MAX_BINDINGS; i++)
690 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
691
a1022927 692 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
8ca151b5
JB
693 sizeof(quota_cmd), &quota_cmd);
694 if (ret)
695 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
696
697 return 0;
698}
699
4ac6cb59
JB
700static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
701 struct ieee80211_vif *vif)
702{
6d9d32b8
JB
703 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
704 struct iwl_nonqos_seq_query_cmd query_cmd = {
705 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
706 .mac_id_n_color =
707 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
708 mvmvif->color)),
709 };
4ac6cb59
JB
710 struct iwl_host_cmd cmd = {
711 .id = NON_QOS_TX_COUNTER_CMD,
a1022927 712 .flags = CMD_WANT_SKB,
4ac6cb59
JB
713 };
714 int err;
715 u32 size;
716
3afec639
EG
717 cmd.data[0] = &query_cmd;
718 cmd.len[0] = sizeof(query_cmd);
6d9d32b8 719
4ac6cb59
JB
720 err = iwl_mvm_send_cmd(mvm, &cmd);
721 if (err)
722 return err;
723
65b30348 724 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
6d9d32b8 725 if (size < sizeof(__le16)) {
4ac6cb59 726 err = -EINVAL;
6d9d32b8
JB
727 } else {
728 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
3afec639
EG
729 /* firmware returns next, not last-used seqno */
730 err = (u16) (err - 0x10);
6d9d32b8 731 }
4ac6cb59
JB
732
733 iwl_free_resp(&cmd);
734 return err;
735}
736
6d9d32b8
JB
737void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
738{
739 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
740 struct iwl_nonqos_seq_query_cmd query_cmd = {
741 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
742 .mac_id_n_color =
743 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
744 mvmvif->color)),
745 .value = cpu_to_le16(mvmvif->seqno),
746 };
747
748 /* return if called during restart, not resume from D3 */
749 if (!mvmvif->seqno_valid)
750 return;
751
752 mvmvif->seqno_valid = false;
753
a1022927 754 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
6d9d32b8
JB
755 sizeof(query_cmd), &query_cmd))
756 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
757}
758
671b5820
LC
759static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
760{
761 iwl_mvm_cancel_scan(mvm);
762
763 iwl_trans_stop_device(mvm->trans);
764
765 /*
766 * Set the HW restart bit -- this is mostly true as we're
767 * going to load new firmware and reprogram that, though
768 * the reprogramming is going to be manual to avoid adding
769 * all the MACs that aren't support.
770 * We don't have to clear up everything though because the
771 * reprogramming is manual. When we resume, we'll actually
772 * go through a proper restart sequence again to switch
773 * back to the runtime firmware image.
774 */
775 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
776
777 /* We reprogram keys and shouldn't allocate new key indices */
778 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
779
780 mvm->ptk_ivlen = 0;
781 mvm->ptk_icvlen = 0;
782 mvm->ptk_ivlen = 0;
783 mvm->ptk_icvlen = 0;
784
785 return iwl_mvm_load_d3_fw(mvm);
786}
787
1a95c8df
EP
788static int
789iwl_mvm_send_wowlan_config_cmd(struct iwl_mvm *mvm,
790 const struct iwl_wowlan_config_cmd_v3 *cmd)
791{
792 /* start only with the v2 part of the command */
793 u16 cmd_len = sizeof(cmd->common);
794
795 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_WOWLAN_CONFIG_TID)
796 cmd_len = sizeof(*cmd);
797
a1022927 798 return iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1a95c8df
EP
799 cmd_len, cmd);
800}
801
671b5820
LC
802static int
803iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
804 struct cfg80211_wowlan *wowlan,
805 struct iwl_wowlan_config_cmd_v3 *wowlan_config_cmd,
806 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
807 struct ieee80211_sta *ap_sta)
8ca151b5 808{
1a95c8df 809 int ret;
671b5820 810 struct iwl_mvm_sta *mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
8ca151b5 811
671b5820 812 /* TODO: wowlan_config_cmd->common.wowlan_ba_teardown_tids */
8ca151b5 813
671b5820 814 wowlan_config_cmd->common.is_11n_connection =
1a95c8df 815 ap_sta->ht_cap.ht_supported;
8ca151b5 816
4ac6cb59
JB
817 /* Query the last used seqno and set it */
818 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
819 if (ret < 0)
671b5820 820 return ret;
8ca151b5 821
671b5820
LC
822 wowlan_config_cmd->common.non_qos_seq = cpu_to_le16(ret);
823
824 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, &wowlan_config_cmd->common);
8ca151b5
JB
825
826 if (wowlan->disconnect)
671b5820 827 wowlan_config_cmd->common.wakeup_filter |=
8ca151b5
JB
828 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
829 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
830 if (wowlan->magic_pkt)
671b5820 831 wowlan_config_cmd->common.wakeup_filter |=
8ca151b5
JB
832 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
833 if (wowlan->gtk_rekey_failure)
671b5820 834 wowlan_config_cmd->common.wakeup_filter |=
8ca151b5
JB
835 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
836 if (wowlan->eap_identity_req)
671b5820 837 wowlan_config_cmd->common.wakeup_filter |=
8ca151b5
JB
838 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
839 if (wowlan->four_way_handshake)
671b5820 840 wowlan_config_cmd->common.wakeup_filter |=
8ca151b5
JB
841 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
842 if (wowlan->n_patterns)
671b5820 843 wowlan_config_cmd->common.wakeup_filter |=
8ca151b5
JB
844 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
845
846 if (wowlan->rfkill_release)
671b5820 847 wowlan_config_cmd->common.wakeup_filter |=
8ca151b5
JB
848 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
849
f0c2646a
JB
850 if (wowlan->tcp) {
851 /*
6039f3e1
JB
852 * Set the "link change" (really "link lost") flag as well
853 * since that implies losing the TCP connection.
f0c2646a 854 */
671b5820 855 wowlan_config_cmd->common.wakeup_filter |=
f0c2646a
JB
856 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
857 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
858 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
859 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
860 }
861
671b5820
LC
862 return 0;
863}
f444eb10 864
671b5820
LC
865static int
866iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
867 struct cfg80211_wowlan *wowlan,
868 struct iwl_wowlan_config_cmd_v3 *wowlan_config_cmd,
869 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
870 struct ieee80211_sta *ap_sta)
871{
872 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
873 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
874 struct wowlan_key_data key_data = {
875 .use_rsc_tsc = false,
876 .tkip = &tkip_cmd,
877 .use_tkip = false,
878 };
879 int ret;
8ca151b5 880
3acc952b
LC
881 ret = iwl_mvm_switch_to_d3(mvm);
882 if (ret)
883 return ret;
884
8ca151b5
JB
885 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
886 if (ret)
671b5820
LC
887 return ret;
888
889 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
890 if (!key_data.rsc_tsc)
891 return -ENOMEM;
8ca151b5
JB
892
893 if (!iwlwifi_mod_params.sw_crypto) {
894 /*
895 * This needs to be unlocked due to lock ordering
896 * constraints. Since we're in the suspend path
897 * that isn't really a problem though.
898 */
899 mutex_unlock(&mvm->mutex);
900 ieee80211_iter_keys(mvm->hw, vif,
901 iwl_mvm_wowlan_program_keys,
902 &key_data);
903 mutex_lock(&mvm->mutex);
904 if (key_data.error) {
905 ret = -EIO;
906 goto out;
907 }
908
909 if (key_data.use_rsc_tsc) {
910 struct iwl_host_cmd rsc_tsc_cmd = {
911 .id = WOWLAN_TSC_RSC_PARAM,
8ca151b5
JB
912 .data[0] = key_data.rsc_tsc,
913 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
914 .len[0] = sizeof(*key_data.rsc_tsc),
915 };
916
917 ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
918 if (ret)
919 goto out;
920 }
921
922 if (key_data.use_tkip) {
923 ret = iwl_mvm_send_cmd_pdu(mvm,
924 WOWLAN_TKIP_PARAM,
a1022927 925 0, sizeof(tkip_cmd),
8ca151b5
JB
926 &tkip_cmd);
927 if (ret)
928 goto out;
929 }
930
931 if (mvmvif->rekey_data.valid) {
932 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
933 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
934 NL80211_KCK_LEN);
935 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
936 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
937 NL80211_KEK_LEN);
938 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
939 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
940
941 ret = iwl_mvm_send_cmd_pdu(mvm,
a1022927 942 WOWLAN_KEK_KCK_MATERIAL, 0,
8ca151b5
JB
943 sizeof(kek_kck_cmd),
944 &kek_kck_cmd);
945 if (ret)
946 goto out;
947 }
948 }
949
671b5820 950 ret = iwl_mvm_send_wowlan_config_cmd(mvm, wowlan_config_cmd);
8ca151b5
JB
951 if (ret)
952 goto out;
953
954 ret = iwl_mvm_send_patterns(mvm, wowlan);
955 if (ret)
956 goto out;
957
a1022927 958 ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
8ca151b5
JB
959 if (ret)
960 goto out;
961
f0c2646a 962 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
671b5820
LC
963
964out:
965 kfree(key_data.rsc_tsc);
966 return ret;
967}
968
3acc952b
LC
969static int
970iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
971 struct cfg80211_wowlan *wowlan,
972 struct cfg80211_sched_scan_request *nd_config,
973 struct ieee80211_vif *vif)
974{
975 struct iwl_wowlan_config_cmd_v3 wowlan_config_cmd = {};
976 int ret;
977
978 ret = iwl_mvm_switch_to_d3(mvm);
979 if (ret)
980 return ret;
981
982 /* rfkill release can be either for wowlan or netdetect */
983 if (wowlan->rfkill_release)
984 wowlan_config_cmd.common.wakeup_filter |=
985 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
986
987 ret = iwl_mvm_send_wowlan_config_cmd(mvm, &wowlan_config_cmd);
988 if (ret)
989 return ret;
990
991 ret = iwl_mvm_scan_offload_start(mvm, vif, nd_config, &mvm->nd_ies);
992
993 return ret;
994}
995
671b5820
LC
996static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
997 struct cfg80211_wowlan *wowlan,
998 bool test)
999{
1000 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
671b5820
LC
1001 struct ieee80211_vif *vif = NULL;
1002 struct iwl_mvm_vif *mvmvif = NULL;
1003 struct ieee80211_sta *ap_sta = NULL;
671b5820
LC
1004 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1005 /*
1006 * Program the minimum sleep time to 10 seconds, as many
1007 * platforms have issues processing a wakeup signal while
1008 * still being in the process of suspending.
1009 */
1010 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1011 };
1012 struct iwl_host_cmd d3_cfg_cmd = {
1013 .id = D3_CONFIG_CMD,
1014 .flags = CMD_WANT_SKB,
1015 .data[0] = &d3_cfg_cmd_data,
1016 .len[0] = sizeof(d3_cfg_cmd_data),
1017 };
1018 int ret;
1019 int len __maybe_unused;
1020
1021 if (!wowlan) {
1022 /*
1023 * mac80211 shouldn't get here, but for D3 test
1024 * it doesn't warrant a warning
1025 */
1026 WARN_ON(!test);
1027 return -EINVAL;
1028 }
1029
1030 mutex_lock(&mvm->mutex);
1031
7f549e2c
LC
1032 vif = iwl_mvm_get_bss_vif(mvm);
1033 if (IS_ERR_OR_NULL(vif)) {
671b5820
LC
1034 ret = 1;
1035 goto out_noreset;
1036 }
1037
671b5820
LC
1038 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1039
3acc952b
LC
1040 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
1041 /* if we're not associated, this must be netdetect */
1042 if (!wowlan->nd_config && !mvm->nd_config) {
1043 ret = 1;
1044 goto out_noreset;
1045 }
1046
1047 ret = iwl_mvm_netdetect_config(
1048 mvm, wowlan, wowlan->nd_config ?: mvm->nd_config, vif);
1049 if (ret)
1050 goto out;
2021a89d
LC
1051
1052 mvm->net_detect = true;
3acc952b
LC
1053 } else {
1054 struct iwl_wowlan_config_cmd_v3 wowlan_config_cmd = {};
1055
1e4d19ca
LC
1056 ap_sta = rcu_dereference_protected(
1057 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1058 lockdep_is_held(&mvm->mutex));
1059 if (IS_ERR_OR_NULL(ap_sta)) {
1060 ret = -EINVAL;
1061 goto out_noreset;
1062 }
671b5820 1063
1e4d19ca
LC
1064 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1065 vif, mvmvif, ap_sta);
1066 if (ret)
1067 goto out_noreset;
1e4d19ca
LC
1068 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1069 vif, mvmvif, ap_sta);
1070 if (ret)
3c2f3b20 1071 goto out;
2021a89d
LC
1072
1073 mvm->net_detect = false;
1e4d19ca 1074 }
f0c2646a 1075
c1cb92fc 1076 ret = iwl_mvm_power_update_device(mvm);
64b928c4
AB
1077 if (ret)
1078 goto out;
1079
999609f1 1080 ret = iwl_mvm_power_update_mac(mvm);
ee1e8422
AB
1081 if (ret)
1082 goto out;
1083
b0114714
JB
1084#ifdef CONFIG_IWLWIFI_DEBUGFS
1085 if (mvm->d3_wake_sysassert)
1086 d3_cfg_cmd_data.wakeup_flags |=
1087 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1088#endif
1089
8ca151b5 1090 /* must be last -- this switches firmware state */
debff618 1091 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
8ca151b5
JB
1092 if (ret)
1093 goto out;
debff618 1094#ifdef CONFIG_IWLWIFI_DEBUGFS
65b30348
JB
1095 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1096 if (len >= sizeof(u32)) {
debff618
JB
1097 mvm->d3_test_pme_ptr =
1098 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
debff618
JB
1099 }
1100#endif
1101 iwl_free_resp(&d3_cfg_cmd);
8ca151b5
JB
1102
1103 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1104
debff618 1105 iwl_trans_d3_suspend(mvm->trans, test);
8ca151b5 1106 out:
8ca151b5
JB
1107 if (ret < 0)
1108 ieee80211_restart_hw(mvm->hw);
b70ab727 1109 out_noreset:
8ca151b5
JB
1110 mutex_unlock(&mvm->mutex);
1111
1112 return ret;
1113}
1114
debff618
JB
1115int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1116{
d15a747f
EP
1117 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1118
1119 if (iwl_mvm_is_d0i3_supported(mvm)) {
1120 mutex_lock(&mvm->d0i3_suspend_mutex);
1121 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1122 mutex_unlock(&mvm->d0i3_suspend_mutex);
1123 return 0;
1124 }
1125
debff618
JB
1126 return __iwl_mvm_suspend(hw, wowlan, false);
1127}
1128
6d9d32b8
JB
1129/* converted data from the different status responses */
1130struct iwl_wowlan_status_data {
1131 u16 pattern_number;
1132 u16 qos_seq_ctr[8];
1133 u32 wakeup_reasons;
1134 u32 wake_packet_length;
1135 u32 wake_packet_bufsize;
1136 const u8 *wake_packet;
1137};
1138
7abc4632
JB
1139static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1140 struct ieee80211_vif *vif,
6d9d32b8 1141 struct iwl_wowlan_status_data *status)
9b26b500 1142{
7abc4632 1143 struct sk_buff *pkt = NULL;
9b26b500
JB
1144 struct cfg80211_wowlan_wakeup wakeup = {
1145 .pattern_idx = -1,
1146 };
1147 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
6d9d32b8 1148 u32 reasons = status->wakeup_reasons;
9b26b500
JB
1149
1150 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1151 wakeup_report = NULL;
1152 goto report;
1153 }
1154
f444eb10 1155 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
9b26b500 1156 wakeup.magic_pkt = true;
9b26b500 1157
f444eb10 1158 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
9b26b500 1159 wakeup.pattern_idx =
6d9d32b8 1160 status->pattern_number;
9b26b500
JB
1161
1162 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1163 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1164 wakeup.disconnect = true;
1165
f444eb10 1166 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
9b26b500 1167 wakeup.gtk_rekey_failure = true;
9b26b500 1168
f444eb10 1169 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
9b26b500 1170 wakeup.rfkill_release = true;
9b26b500 1171
f444eb10 1172 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
9b26b500 1173 wakeup.eap_identity_req = true;
9b26b500 1174
f444eb10 1175 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
9b26b500 1176 wakeup.four_way_handshake = true;
9b26b500 1177
f0c2646a
JB
1178 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1179 wakeup.tcp_connlost = true;
1180
1181 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1182 wakeup.tcp_nomoretokens = true;
1183
1184 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1185 wakeup.tcp_match = true;
1186
9b26b500 1187 if (status->wake_packet_bufsize) {
6d9d32b8
JB
1188 int pktsize = status->wake_packet_bufsize;
1189 int pktlen = status->wake_packet_length;
f444eb10
JB
1190 const u8 *pktdata = status->wake_packet;
1191 struct ieee80211_hdr *hdr = (void *)pktdata;
1192 int truncated = pktlen - pktsize;
1193
1194 /* this would be a firmware bug */
1195 if (WARN_ON_ONCE(truncated < 0))
1196 truncated = 0;
1197
1198 if (ieee80211_is_data(hdr->frame_control)) {
1199 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1200 int ivlen = 0, icvlen = 4; /* also FCS */
9b26b500 1201
9b26b500
JB
1202 pkt = alloc_skb(pktsize, GFP_KERNEL);
1203 if (!pkt)
1204 goto report;
f444eb10
JB
1205
1206 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1207 pktdata += hdrlen;
1208 pktsize -= hdrlen;
1209
1210 if (ieee80211_has_protected(hdr->frame_control)) {
7abc4632
JB
1211 /*
1212 * This is unlocked and using gtk_i(c)vlen,
1213 * but since everything is under RTNL still
1214 * that's not really a problem - changing
1215 * it would be difficult.
1216 */
f444eb10
JB
1217 if (is_multicast_ether_addr(hdr->addr1)) {
1218 ivlen = mvm->gtk_ivlen;
1219 icvlen += mvm->gtk_icvlen;
1220 } else {
1221 ivlen = mvm->ptk_ivlen;
1222 icvlen += mvm->ptk_icvlen;
1223 }
1224 }
1225
1226 /* if truncated, FCS/ICV is (partially) gone */
1227 if (truncated >= icvlen) {
1228 icvlen = 0;
1229 truncated -= icvlen;
1230 } else {
1231 icvlen -= truncated;
1232 truncated = 0;
1233 }
1234
1235 pktsize -= ivlen + icvlen;
1236 pktdata += ivlen;
1237
1238 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1239
9b26b500
JB
1240 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1241 goto report;
1242 wakeup.packet = pkt->data;
1243 wakeup.packet_present_len = pkt->len;
f444eb10 1244 wakeup.packet_len = pkt->len - truncated;
9b26b500
JB
1245 wakeup.packet_80211 = false;
1246 } else {
f444eb10
JB
1247 int fcslen = 4;
1248
1249 if (truncated >= 4) {
1250 truncated -= 4;
1251 fcslen = 0;
1252 } else {
1253 fcslen -= truncated;
1254 truncated = 0;
1255 }
1256 pktsize -= fcslen;
9b26b500
JB
1257 wakeup.packet = status->wake_packet;
1258 wakeup.packet_present_len = pktsize;
f444eb10 1259 wakeup.packet_len = pktlen - truncated;
9b26b500
JB
1260 wakeup.packet_80211 = true;
1261 }
1262 }
1263
1264 report:
1265 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1266 kfree_skb(pkt);
7abc4632 1267}
9b26b500 1268
6d9d32b8
JB
1269static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1270 struct ieee80211_key_seq *seq)
1271{
1272 u64 pn;
1273
1274 pn = le64_to_cpu(sc->pn);
1275 seq->ccmp.pn[0] = pn >> 40;
1276 seq->ccmp.pn[1] = pn >> 32;
1277 seq->ccmp.pn[2] = pn >> 24;
1278 seq->ccmp.pn[3] = pn >> 16;
1279 seq->ccmp.pn[4] = pn >> 8;
1280 seq->ccmp.pn[5] = pn;
1281}
1282
1283static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1284 struct ieee80211_key_seq *seq)
1285{
1286 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1287 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1288}
1289
1290static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1291 struct ieee80211_key_conf *key)
1292{
1293 int tid;
1294
1295 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1296
1297 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1298 struct ieee80211_key_seq seq = {};
1299
1300 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1301 ieee80211_set_key_rx_seq(key, tid, &seq);
1302 }
1303}
1304
1305static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1306 struct ieee80211_key_conf *key)
1307{
1308 int tid;
1309
1310 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1311
1312 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1313 struct ieee80211_key_seq seq = {};
1314
1315 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1316 ieee80211_set_key_rx_seq(key, tid, &seq);
1317 }
1318}
1319
1320static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
3afec639 1321 struct iwl_wowlan_status *status)
6d9d32b8
JB
1322{
1323 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1324
1325 switch (key->cipher) {
1326 case WLAN_CIPHER_SUITE_CCMP:
1327 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1328 break;
1329 case WLAN_CIPHER_SUITE_TKIP:
1330 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1331 break;
1332 default:
1333 WARN_ON(1);
1334 }
1335}
1336
1337struct iwl_mvm_d3_gtk_iter_data {
3afec639 1338 struct iwl_wowlan_status *status;
6d9d32b8
JB
1339 void *last_gtk;
1340 u32 cipher;
1341 bool find_phase, unhandled_cipher;
1342 int num_keys;
1343};
1344
1345static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
1346 struct ieee80211_vif *vif,
1347 struct ieee80211_sta *sta,
1348 struct ieee80211_key_conf *key,
1349 void *_data)
1350{
1351 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1352
1353 if (data->unhandled_cipher)
1354 return;
1355
1356 switch (key->cipher) {
1357 case WLAN_CIPHER_SUITE_WEP40:
1358 case WLAN_CIPHER_SUITE_WEP104:
1359 /* ignore WEP completely, nothing to do */
1360 return;
1361 case WLAN_CIPHER_SUITE_CCMP:
1362 case WLAN_CIPHER_SUITE_TKIP:
1363 /* we support these */
1364 break;
1365 default:
1366 /* everything else (even CMAC for MFP) - disconnect from AP */
1367 data->unhandled_cipher = true;
1368 return;
1369 }
1370
1371 data->num_keys++;
1372
1373 /*
1374 * pairwise key - update sequence counters only;
1375 * note that this assumes no TDLS sessions are active
1376 */
1377 if (sta) {
1378 struct ieee80211_key_seq seq = {};
1379 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1380
1381 if (data->find_phase)
1382 return;
1383
1384 switch (key->cipher) {
1385 case WLAN_CIPHER_SUITE_CCMP:
1386 iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
1387 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
1388 break;
1389 case WLAN_CIPHER_SUITE_TKIP:
1390 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1391 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1392 break;
1393 }
1394 ieee80211_set_key_tx_seq(key, &seq);
1395
1396 /* that's it for this key */
1397 return;
1398 }
1399
1400 if (data->find_phase) {
1401 data->last_gtk = key;
1402 data->cipher = key->cipher;
1403 return;
1404 }
1405
1406 if (data->status->num_of_gtk_rekeys)
1407 ieee80211_remove_key(key);
1408 else if (data->last_gtk == key)
1409 iwl_mvm_set_key_rx_seq(key, data->status);
1410}
1411
1412static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1413 struct ieee80211_vif *vif,
3afec639 1414 struct iwl_wowlan_status *status)
6d9d32b8
JB
1415{
1416 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1417 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1418 .status = status,
1419 };
431591cc
EP
1420 u32 disconnection_reasons =
1421 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1422 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
6d9d32b8
JB
1423
1424 if (!status || !vif->bss_conf.bssid)
1425 return false;
1426
431591cc
EP
1427 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1428 return false;
1429
6d9d32b8
JB
1430 /* find last GTK that we used initially, if any */
1431 gtkdata.find_phase = true;
1432 ieee80211_iter_keys(mvm->hw, vif,
1433 iwl_mvm_d3_update_gtks, &gtkdata);
1434 /* not trying to keep connections with MFP/unhandled ciphers */
1435 if (gtkdata.unhandled_cipher)
1436 return false;
1437 if (!gtkdata.num_keys)
cfa88893 1438 goto out;
6d9d32b8
JB
1439 if (!gtkdata.last_gtk)
1440 return false;
1441
1442 /*
1443 * invalidate all other GTKs that might still exist and update
1444 * the one that we used
1445 */
1446 gtkdata.find_phase = false;
1447 ieee80211_iter_keys(mvm->hw, vif,
1448 iwl_mvm_d3_update_gtks, &gtkdata);
1449
1450 if (status->num_of_gtk_rekeys) {
1451 struct ieee80211_key_conf *key;
1452 struct {
1453 struct ieee80211_key_conf conf;
1454 u8 key[32];
1455 } conf = {
1456 .conf.cipher = gtkdata.cipher,
1457 .conf.keyidx = status->gtk.key_index,
1458 };
1459
1460 switch (gtkdata.cipher) {
1461 case WLAN_CIPHER_SUITE_CCMP:
1462 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1463 memcpy(conf.conf.key, status->gtk.decrypt_key,
1464 WLAN_KEY_LEN_CCMP);
1465 break;
1466 case WLAN_CIPHER_SUITE_TKIP:
1467 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1468 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1469 /* leave TX MIC key zeroed, we don't use it anyway */
1470 memcpy(conf.conf.key +
1471 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1472 status->gtk.tkip_mic_key, 8);
1473 break;
1474 }
1475
1476 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1477 if (IS_ERR(key))
1478 return false;
1479 iwl_mvm_set_key_rx_seq(key, status);
1480 }
1481
1482 if (status->num_of_gtk_rekeys) {
1483 __be64 replay_ctr =
1484 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1485 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1486 (void *)&replay_ctr, GFP_KERNEL);
1487 }
1488
cfa88893 1489out:
6d9d32b8
JB
1490 mvmvif->seqno_valid = true;
1491 /* +0x10 because the set API expects next-to-use, not last-used */
1492 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1493
1494 return true;
1495}
1496
ec12f457
LC
1497static struct iwl_wowlan_status *
1498iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
7abc4632
JB
1499{
1500 u32 base = mvm->error_event_table;
1501 struct error_table_start {
1502 /* cf. struct iwl_error_event_table */
1503 u32 valid;
1504 u32 error_id;
1505 } err_info;
1506 struct iwl_host_cmd cmd = {
1507 .id = WOWLAN_GET_STATUSES,
a1022927 1508 .flags = CMD_WANT_SKB,
7abc4632 1509 };
ec12f457
LC
1510 struct iwl_wowlan_status *status, *fw_status;
1511 int ret, len, status_size;
7abc4632
JB
1512
1513 iwl_trans_read_mem_bytes(mvm->trans, base,
1514 &err_info, sizeof(err_info));
1515
1516 if (err_info.valid) {
ec12f457
LC
1517 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1518 err_info.valid, err_info.error_id);
7abc4632
JB
1519 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1520 struct cfg80211_wowlan_wakeup wakeup = {
1521 .rfkill_release = true,
1522 };
1523 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1524 GFP_KERNEL);
1525 }
ec12f457 1526 return ERR_PTR(-EIO);
7abc4632
JB
1527 }
1528
1529 /* only for tracing for now */
a1022927 1530 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
7abc4632
JB
1531 if (ret)
1532 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1533
1534 ret = iwl_mvm_send_cmd(mvm, &cmd);
1535 if (ret) {
1536 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
ec12f457 1537 return ERR_PTR(ret);
7abc4632
JB
1538 }
1539
1540 /* RF-kill already asserted again... */
ec12f457
LC
1541 if (!cmd.resp_pkt) {
1542 ret = -ERFKILL;
1543 goto out_free_resp;
1544 }
7abc4632 1545
3afec639 1546 status_size = sizeof(*fw_status);
6d9d32b8 1547
65b30348
JB
1548 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1549 if (len < status_size) {
7abc4632 1550 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
ec12f457
LC
1551 ret = -EIO;
1552 goto out_free_resp;
1553 }
1554
1555 status = (void *)cmd.resp_pkt->data;
1556 if (len != (status_size +
1557 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1558 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1559 ret = -EIO;
7abc4632
JB
1560 goto out_free_resp;
1561 }
1562
ec12f457
LC
1563 fw_status = kmemdup(status, len, GFP_KERNEL);
1564
1565out_free_resp:
1566 iwl_free_resp(&cmd);
1567 return ret ? ERR_PTR(ret) : fw_status;
1568}
1569
1570/* releases the MVM mutex */
1571static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1572 struct ieee80211_vif *vif)
1573{
1574 struct iwl_wowlan_status_data status;
1575 struct iwl_wowlan_status *fw_status;
1576 int i;
1577 bool keep;
1578 struct ieee80211_sta *ap_sta;
1579 struct iwl_mvm_sta *mvm_ap_sta;
1580
1581 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1582 if (IS_ERR_OR_NULL(fw_status))
1583 goto out_unlock;
3afec639
EG
1584
1585 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1586 for (i = 0; i < 8; i++)
1587 status.qos_seq_ctr[i] =
1588 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1589 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1590 status.wake_packet_length =
1591 le32_to_cpu(fw_status->wake_packet_length);
1592 status.wake_packet_bufsize =
1593 le32_to_cpu(fw_status->wake_packet_bufsize);
1594 status.wake_packet = fw_status->wake_packet;
7abc4632 1595
6d9d32b8
JB
1596 /* still at hard-coded place 0 for D3 image */
1597 ap_sta = rcu_dereference_protected(
1598 mvm->fw_id_to_mac_id[0],
1599 lockdep_is_held(&mvm->mutex));
1600 if (IS_ERR_OR_NULL(ap_sta))
ec12f457 1601 goto out_free;
6d9d32b8
JB
1602
1603 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
1604 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1605 u16 seq = status.qos_seq_ctr[i];
1606 /* firmware stores last-used value, we store next value */
1607 seq += 0x10;
1608 mvm_ap_sta->tid_data[i].seq_number = seq;
1609 }
1610
7abc4632
JB
1611 /* now we have all the data we need, unlock to avoid mac80211 issues */
1612 mutex_unlock(&mvm->mutex);
1613
6d9d32b8
JB
1614 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1615
3afec639 1616 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
6d9d32b8 1617
ec12f457 1618 kfree(fw_status);
6d9d32b8 1619 return keep;
7abc4632 1620
ec12f457
LC
1621out_free:
1622 kfree(fw_status);
1623out_unlock:
7abc4632 1624 mutex_unlock(&mvm->mutex);
6d9d32b8 1625 return false;
9b26b500
JB
1626}
1627
2021a89d
LC
1628static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1629 struct ieee80211_vif *vif)
1630{
1631 struct cfg80211_wowlan_wakeup wakeup = {
1632 .pattern_idx = -1,
1633 };
1634 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1635 struct iwl_wowlan_status *fw_status;
1636 u32 reasons = 0;
1637
1638 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1639 if (!IS_ERR_OR_NULL(fw_status))
1640 reasons = le32_to_cpu(fw_status->wakeup_reasons);
1641
1642 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1643 wakeup.rfkill_release = true;
1644
1645 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1646 /* TODO: read and check if it was netdetect */
1647 wakeup_report = NULL;
1648 }
1649
1650 mutex_unlock(&mvm->mutex);
1651 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1652}
1653
afc66bb7
JB
1654static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1655{
1656#ifdef CONFIG_IWLWIFI_DEBUGFS
1657 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1658 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1659 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1660
1661 if (!mvm->store_d3_resume_sram)
1662 return;
1663
1664 if (!mvm->d3_resume_sram) {
1665 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1666 if (!mvm->d3_resume_sram)
1667 return;
1668 }
1669
1670 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1671#endif
1672}
1673
6d9d32b8
JB
1674static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1675 struct ieee80211_vif *vif)
1676{
1677 /* skip the one we keep connection on */
1678 if (data == vif)
1679 return;
1680
1681 if (vif->type == NL80211_IFTYPE_STATION)
1682 ieee80211_resume_disconnect(vif);
1683}
1684
debff618 1685static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
8ca151b5 1686{
8ca151b5 1687 struct ieee80211_vif *vif = NULL;
8ca151b5
JB
1688 int ret;
1689 enum iwl_d3_status d3_status;
6d9d32b8 1690 bool keep = false;
8ca151b5
JB
1691
1692 mutex_lock(&mvm->mutex);
1693
1694 /* get the BSS vif pointer again */
7f549e2c
LC
1695 vif = iwl_mvm_get_bss_vif(mvm);
1696 if (IS_ERR_OR_NULL(vif))
8ca151b5
JB
1697 goto out_unlock;
1698
debff618 1699 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
8ca151b5
JB
1700 if (ret)
1701 goto out_unlock;
1702
1703 if (d3_status != IWL_D3_STATUS_ALIVE) {
1704 IWL_INFO(mvm, "Device was reset during suspend\n");
1705 goto out_unlock;
1706 }
1707
afc66bb7
JB
1708 /* query SRAM first in case we want event logging */
1709 iwl_mvm_read_d3_sram(mvm);
1710
2021a89d
LC
1711 if (mvm->net_detect) {
1712 iwl_mvm_query_netdetect_reasons(mvm, vif);
1713 } else {
1714 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
78c9df66 1715#ifdef CONFIG_IWLWIFI_DEBUGFS
2021a89d
LC
1716 if (keep)
1717 mvm->keep_vif = vif;
78c9df66 1718#endif
2021a89d 1719 }
7abc4632
JB
1720 /* has unlocked the mutex, so skip that */
1721 goto out;
8ca151b5
JB
1722
1723 out_unlock:
1724 mutex_unlock(&mvm->mutex);
1725
7abc4632 1726 out:
6d9d32b8
JB
1727 if (!test)
1728 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1729 IEEE80211_IFACE_ITER_NORMAL,
1730 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
8ca151b5
JB
1731
1732 /* return 1 to reconfigure the device */
1733 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
58629d9d 1734 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
8ca151b5
JB
1735 return 1;
1736}
1737
debff618
JB
1738int iwl_mvm_resume(struct ieee80211_hw *hw)
1739{
1740 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1741
088070a2 1742 if (iwl_mvm_is_d0i3_supported(mvm))
d15a747f 1743 return 0;
d15a747f 1744
debff618
JB
1745 return __iwl_mvm_resume(mvm, false);
1746}
1747
8ca151b5
JB
1748void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1749{
1750 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1751
1752 device_set_wakeup_enable(mvm->trans->dev, enabled);
1753}
debff618
JB
1754
1755#ifdef CONFIG_IWLWIFI_DEBUGFS
1756static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
1757{
1758 struct iwl_mvm *mvm = inode->i_private;
1759 int err;
1760
1761 if (mvm->d3_test_active)
1762 return -EBUSY;
1763
1764 file->private_data = inode->i_private;
1765
1766 ieee80211_stop_queues(mvm->hw);
1767 synchronize_net();
1768
1769 /* start pseudo D3 */
1770 rtnl_lock();
1771 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
1772 rtnl_unlock();
1773 if (err > 0)
1774 err = -EINVAL;
1775 if (err) {
1776 ieee80211_wake_queues(mvm->hw);
1777 return err;
1778 }
1779 mvm->d3_test_active = true;
78c9df66 1780 mvm->keep_vif = NULL;
debff618
JB
1781 return 0;
1782}
1783
1784static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
1785 size_t count, loff_t *ppos)
1786{
1787 struct iwl_mvm *mvm = file->private_data;
1788 u32 pme_asserted;
1789
1790 while (true) {
4ed80a86
EP
1791 /* read pme_ptr if available */
1792 if (mvm->d3_test_pme_ptr) {
1793 pme_asserted = iwl_trans_read_mem32(mvm->trans,
1794 mvm->d3_test_pme_ptr);
1795 if (pme_asserted)
1796 break;
1797 }
1798
debff618
JB
1799 if (msleep_interruptible(100))
1800 break;
1801 }
1802
1803 return 0;
1804}
1805
1806static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
1807 struct ieee80211_vif *vif)
1808{
78c9df66
EP
1809 /* skip the one we keep connection on */
1810 if (_data == vif)
1811 return;
1812
debff618
JB
1813 if (vif->type == NL80211_IFTYPE_STATION)
1814 ieee80211_connection_loss(vif);
1815}
1816
1817static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
1818{
1819 struct iwl_mvm *mvm = inode->i_private;
1820 int remaining_time = 10;
1821
1822 mvm->d3_test_active = false;
31ca09dd 1823 rtnl_lock();
debff618 1824 __iwl_mvm_resume(mvm, true);
31ca09dd 1825 rtnl_unlock();
debff618
JB
1826 iwl_abort_notification_waits(&mvm->notif_wait);
1827 ieee80211_restart_hw(mvm->hw);
1828
1829 /* wait for restart and disconnect all interfaces */
1830 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1831 remaining_time > 0) {
1832 remaining_time--;
1833 msleep(1000);
1834 }
1835
1836 if (remaining_time == 0)
1837 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
1838
1839 ieee80211_iterate_active_interfaces_atomic(
1840 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
78c9df66 1841 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
debff618
JB
1842
1843 ieee80211_wake_queues(mvm->hw);
1844
1845 return 0;
1846}
1847
1848const struct file_operations iwl_dbgfs_d3_test_ops = {
1849 .llseek = no_llseek,
1850 .open = iwl_mvm_d3_test_open,
1851 .read = iwl_mvm_d3_test_read,
1852 .release = iwl_mvm_d3_test_release,
1853};
1854#endif