iwlwifi: mvm: set the TX disable bit when doing a chanctx switch
[deliverable/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
604struct iwl_d3_iter_data {
605 struct iwl_mvm *mvm;
606 struct ieee80211_vif *vif;
607 bool error;
608};
609
610static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
611 struct ieee80211_vif *vif)
612{
613 struct iwl_d3_iter_data *data = _data;
614 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
615
616 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
617 return;
618
619 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
620 return;
621
622 if (data->vif) {
623 IWL_ERR(data->mvm, "More than one managed interface active!\n");
624 data->error = true;
625 return;
626 }
627
628 data->vif = vif;
629}
630
631static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
632 struct ieee80211_sta *ap_sta)
633{
634 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
635 struct ieee80211_chanctx_conf *ctx;
636 u8 chains_static, chains_dynamic;
637 struct cfg80211_chan_def chandef;
638 int ret, i;
639 struct iwl_binding_cmd binding_cmd = {};
640 struct iwl_time_quota_cmd quota_cmd = {};
641 u32 status;
642
643 /* add back the PHY */
644 if (WARN_ON(!mvmvif->phy_ctxt))
645 return -EINVAL;
646
647 rcu_read_lock();
648 ctx = rcu_dereference(vif->chanctx_conf);
649 if (WARN_ON(!ctx)) {
650 rcu_read_unlock();
651 return -EINVAL;
652 }
653 chandef = ctx->def;
654 chains_static = ctx->rx_chains_static;
655 chains_dynamic = ctx->rx_chains_dynamic;
656 rcu_read_unlock();
657
658 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
659 chains_static, chains_dynamic);
660 if (ret)
661 return ret;
662
663 /* add back the MAC */
664 mvmvif->uploaded = false;
665
666 if (WARN_ON(!vif->bss_conf.assoc))
667 return -EINVAL;
bca49d9a 668
8ca151b5 669 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
8ca151b5
JB
670 if (ret)
671 return ret;
672
673 /* add back binding - XXX refactor? */
674 binding_cmd.id_and_color =
675 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
676 mvmvif->phy_ctxt->color));
677 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
678 binding_cmd.phy =
679 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
680 mvmvif->phy_ctxt->color));
681 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
682 mvmvif->color));
683 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
684 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
685
686 status = 0;
687 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
688 sizeof(binding_cmd), &binding_cmd,
689 &status);
690 if (ret) {
691 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
692 return ret;
693 }
694
695 if (status) {
696 IWL_ERR(mvm, "Binding command failed: %u\n", status);
697 return -EIO;
698 }
699
7a453973 700 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
8ca151b5
JB
701 if (ret)
702 return ret;
703 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
704
bca49d9a 705 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false);
8ca151b5
JB
706 if (ret)
707 return ret;
708
709 /* and some quota */
710 quota_cmd.quotas[0].id_and_color =
711 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
712 mvmvif->phy_ctxt->color));
9a3daf82
EG
713 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
714 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
8ca151b5
JB
715
716 for (i = 1; i < MAX_BINDINGS; i++)
717 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
718
a1022927 719 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
8ca151b5
JB
720 sizeof(quota_cmd), &quota_cmd);
721 if (ret)
722 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
723
724 return 0;
725}
726
4ac6cb59
JB
727static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
728 struct ieee80211_vif *vif)
729{
6d9d32b8
JB
730 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
731 struct iwl_nonqos_seq_query_cmd query_cmd = {
732 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
733 .mac_id_n_color =
734 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
735 mvmvif->color)),
736 };
4ac6cb59
JB
737 struct iwl_host_cmd cmd = {
738 .id = NON_QOS_TX_COUNTER_CMD,
a1022927 739 .flags = CMD_WANT_SKB,
4ac6cb59
JB
740 };
741 int err;
742 u32 size;
743
3afec639
EG
744 cmd.data[0] = &query_cmd;
745 cmd.len[0] = sizeof(query_cmd);
6d9d32b8 746
4ac6cb59
JB
747 err = iwl_mvm_send_cmd(mvm, &cmd);
748 if (err)
749 return err;
750
65b30348 751 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
6d9d32b8 752 if (size < sizeof(__le16)) {
4ac6cb59 753 err = -EINVAL;
6d9d32b8
JB
754 } else {
755 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
3afec639
EG
756 /* firmware returns next, not last-used seqno */
757 err = (u16) (err - 0x10);
6d9d32b8 758 }
4ac6cb59
JB
759
760 iwl_free_resp(&cmd);
761 return err;
762}
763
6d9d32b8
JB
764void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
765{
766 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
767 struct iwl_nonqos_seq_query_cmd query_cmd = {
768 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
769 .mac_id_n_color =
770 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
771 mvmvif->color)),
772 .value = cpu_to_le16(mvmvif->seqno),
773 };
774
775 /* return if called during restart, not resume from D3 */
776 if (!mvmvif->seqno_valid)
777 return;
778
779 mvmvif->seqno_valid = false;
780
a1022927 781 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
6d9d32b8
JB
782 sizeof(query_cmd), &query_cmd))
783 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
784}
785
1a95c8df
EP
786static int
787iwl_mvm_send_wowlan_config_cmd(struct iwl_mvm *mvm,
788 const struct iwl_wowlan_config_cmd_v3 *cmd)
789{
790 /* start only with the v2 part of the command */
791 u16 cmd_len = sizeof(cmd->common);
792
793 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_WOWLAN_CONFIG_TID)
794 cmd_len = sizeof(*cmd);
795
a1022927 796 return iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1a95c8df
EP
797 cmd_len, cmd);
798}
799
debff618
JB
800static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
801 struct cfg80211_wowlan *wowlan,
802 bool test)
8ca151b5
JB
803{
804 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
805 struct iwl_d3_iter_data suspend_iter_data = {
806 .mvm = mvm,
807 };
808 struct ieee80211_vif *vif;
809 struct iwl_mvm_vif *mvmvif;
810 struct ieee80211_sta *ap_sta;
811 struct iwl_mvm_sta *mvm_ap_sta;
1a95c8df 812 struct iwl_wowlan_config_cmd_v3 wowlan_config_cmd = {};
8ca151b5
JB
813 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
814 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
debff618 815 struct iwl_d3_manager_config d3_cfg_cmd_data = {
c920487d
JB
816 /*
817 * Program the minimum sleep time to 10 seconds, as many
818 * platforms have issues processing a wakeup signal while
819 * still being in the process of suspending.
820 */
821 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
822 };
debff618
JB
823 struct iwl_host_cmd d3_cfg_cmd = {
824 .id = D3_CONFIG_CMD,
a1022927 825 .flags = CMD_WANT_SKB,
debff618
JB
826 .data[0] = &d3_cfg_cmd_data,
827 .len[0] = sizeof(d3_cfg_cmd_data),
828 };
8ca151b5
JB
829 struct wowlan_key_data key_data = {
830 .use_rsc_tsc = false,
831 .tkip = &tkip_cmd,
832 .use_tkip = false,
833 };
1a95c8df 834 int ret;
debff618 835 int len __maybe_unused;
8ca151b5 836
debff618
JB
837 if (!wowlan) {
838 /*
839 * mac80211 shouldn't get here, but for D3 test
840 * it doesn't warrant a warning
841 */
842 WARN_ON(!test);
8ca151b5 843 return -EINVAL;
debff618 844 }
8ca151b5
JB
845
846 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
847 if (!key_data.rsc_tsc)
848 return -ENOMEM;
849
850 mutex_lock(&mvm->mutex);
851
8ca151b5
JB
852 /* see if there's only a single BSS vif and it's associated */
853 ieee80211_iterate_active_interfaces_atomic(
854 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
855 iwl_mvm_d3_iface_iterator, &suspend_iter_data);
856
857 if (suspend_iter_data.error || !suspend_iter_data.vif) {
858 ret = 1;
859 goto out_noreset;
860 }
861
862 vif = suspend_iter_data.vif;
863 mvmvif = iwl_mvm_vif_from_mac80211(vif);
864
865 ap_sta = rcu_dereference_protected(
866 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
867 lockdep_is_held(&mvm->mutex));
868 if (IS_ERR_OR_NULL(ap_sta)) {
869 ret = -EINVAL;
870 goto out_noreset;
871 }
872
873 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
874
1a95c8df 875 /* TODO: wowlan_config_cmd.common.wowlan_ba_teardown_tids */
8ca151b5 876
1a95c8df
EP
877 wowlan_config_cmd.common.is_11n_connection =
878 ap_sta->ht_cap.ht_supported;
8ca151b5 879
4ac6cb59
JB
880 /* Query the last used seqno and set it */
881 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
882 if (ret < 0)
883 goto out_noreset;
1a95c8df 884 wowlan_config_cmd.common.non_qos_seq = cpu_to_le16(ret);
8ca151b5 885
1a95c8df 886 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, &wowlan_config_cmd.common);
8ca151b5
JB
887
888 if (wowlan->disconnect)
1a95c8df 889 wowlan_config_cmd.common.wakeup_filter |=
8ca151b5
JB
890 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
891 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
892 if (wowlan->magic_pkt)
1a95c8df 893 wowlan_config_cmd.common.wakeup_filter |=
8ca151b5
JB
894 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
895 if (wowlan->gtk_rekey_failure)
1a95c8df 896 wowlan_config_cmd.common.wakeup_filter |=
8ca151b5
JB
897 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
898 if (wowlan->eap_identity_req)
1a95c8df 899 wowlan_config_cmd.common.wakeup_filter |=
8ca151b5
JB
900 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
901 if (wowlan->four_way_handshake)
1a95c8df 902 wowlan_config_cmd.common.wakeup_filter |=
8ca151b5
JB
903 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
904 if (wowlan->n_patterns)
1a95c8df 905 wowlan_config_cmd.common.wakeup_filter |=
8ca151b5
JB
906 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
907
908 if (wowlan->rfkill_release)
1a95c8df 909 wowlan_config_cmd.common.wakeup_filter |=
8ca151b5
JB
910 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
911
f0c2646a
JB
912 if (wowlan->tcp) {
913 /*
6039f3e1
JB
914 * Set the "link change" (really "link lost") flag as well
915 * since that implies losing the TCP connection.
f0c2646a 916 */
1a95c8df 917 wowlan_config_cmd.common.wakeup_filter |=
f0c2646a
JB
918 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
919 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
920 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
921 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
922 }
923
8ca151b5
JB
924 iwl_mvm_cancel_scan(mvm);
925
926 iwl_trans_stop_device(mvm->trans);
927
928 /*
929 * Set the HW restart bit -- this is mostly true as we're
930 * going to load new firmware and reprogram that, though
931 * the reprogramming is going to be manual to avoid adding
932 * all the MACs that aren't support.
933 * We don't have to clear up everything though because the
934 * reprogramming is manual. When we resume, we'll actually
935 * go through a proper restart sequence again to switch
936 * back to the runtime firmware image.
937 */
938 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
939
940 /* We reprogram keys and shouldn't allocate new key indices */
941 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
942
f444eb10
JB
943 mvm->ptk_ivlen = 0;
944 mvm->ptk_icvlen = 0;
945 mvm->ptk_ivlen = 0;
946 mvm->ptk_icvlen = 0;
947
8ca151b5
JB
948 ret = iwl_mvm_load_d3_fw(mvm);
949 if (ret)
950 goto out;
951
952 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
953 if (ret)
954 goto out;
955
956 if (!iwlwifi_mod_params.sw_crypto) {
957 /*
958 * This needs to be unlocked due to lock ordering
959 * constraints. Since we're in the suspend path
960 * that isn't really a problem though.
961 */
962 mutex_unlock(&mvm->mutex);
963 ieee80211_iter_keys(mvm->hw, vif,
964 iwl_mvm_wowlan_program_keys,
965 &key_data);
966 mutex_lock(&mvm->mutex);
967 if (key_data.error) {
968 ret = -EIO;
969 goto out;
970 }
971
972 if (key_data.use_rsc_tsc) {
973 struct iwl_host_cmd rsc_tsc_cmd = {
974 .id = WOWLAN_TSC_RSC_PARAM,
8ca151b5
JB
975 .data[0] = key_data.rsc_tsc,
976 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
977 .len[0] = sizeof(*key_data.rsc_tsc),
978 };
979
980 ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
981 if (ret)
982 goto out;
983 }
984
985 if (key_data.use_tkip) {
986 ret = iwl_mvm_send_cmd_pdu(mvm,
987 WOWLAN_TKIP_PARAM,
a1022927 988 0, sizeof(tkip_cmd),
8ca151b5
JB
989 &tkip_cmd);
990 if (ret)
991 goto out;
992 }
993
994 if (mvmvif->rekey_data.valid) {
995 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
996 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
997 NL80211_KCK_LEN);
998 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
999 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1000 NL80211_KEK_LEN);
1001 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1002 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1003
1004 ret = iwl_mvm_send_cmd_pdu(mvm,
a1022927 1005 WOWLAN_KEK_KCK_MATERIAL, 0,
8ca151b5
JB
1006 sizeof(kek_kck_cmd),
1007 &kek_kck_cmd);
1008 if (ret)
1009 goto out;
1010 }
1011 }
1012
1a95c8df 1013 ret = iwl_mvm_send_wowlan_config_cmd(mvm, &wowlan_config_cmd);
8ca151b5
JB
1014 if (ret)
1015 goto out;
1016
1017 ret = iwl_mvm_send_patterns(mvm, wowlan);
1018 if (ret)
1019 goto out;
1020
a1022927 1021 ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
8ca151b5
JB
1022 if (ret)
1023 goto out;
1024
f0c2646a
JB
1025 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1026 if (ret)
1027 goto out;
1028
c1cb92fc 1029 ret = iwl_mvm_power_update_device(mvm);
64b928c4
AB
1030 if (ret)
1031 goto out;
1032
999609f1 1033 ret = iwl_mvm_power_update_mac(mvm);
ee1e8422
AB
1034 if (ret)
1035 goto out;
1036
b0114714
JB
1037#ifdef CONFIG_IWLWIFI_DEBUGFS
1038 if (mvm->d3_wake_sysassert)
1039 d3_cfg_cmd_data.wakeup_flags |=
1040 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1041#endif
1042
8ca151b5 1043 /* must be last -- this switches firmware state */
debff618 1044 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
8ca151b5
JB
1045 if (ret)
1046 goto out;
debff618 1047#ifdef CONFIG_IWLWIFI_DEBUGFS
65b30348
JB
1048 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1049 if (len >= sizeof(u32)) {
debff618
JB
1050 mvm->d3_test_pme_ptr =
1051 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
debff618
JB
1052 }
1053#endif
1054 iwl_free_resp(&d3_cfg_cmd);
8ca151b5
JB
1055
1056 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1057
debff618 1058 iwl_trans_d3_suspend(mvm->trans, test);
8ca151b5 1059 out:
8ca151b5
JB
1060 if (ret < 0)
1061 ieee80211_restart_hw(mvm->hw);
b70ab727
LC
1062 out_noreset:
1063 kfree(key_data.rsc_tsc);
8ca151b5
JB
1064
1065 mutex_unlock(&mvm->mutex);
1066
1067 return ret;
1068}
1069
debff618
JB
1070int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1071{
d15a747f
EP
1072 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1073
1074 if (iwl_mvm_is_d0i3_supported(mvm)) {
1075 mutex_lock(&mvm->d0i3_suspend_mutex);
1076 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1077 mutex_unlock(&mvm->d0i3_suspend_mutex);
1078 return 0;
1079 }
1080
debff618
JB
1081 return __iwl_mvm_suspend(hw, wowlan, false);
1082}
1083
6d9d32b8
JB
1084/* converted data from the different status responses */
1085struct iwl_wowlan_status_data {
1086 u16 pattern_number;
1087 u16 qos_seq_ctr[8];
1088 u32 wakeup_reasons;
1089 u32 wake_packet_length;
1090 u32 wake_packet_bufsize;
1091 const u8 *wake_packet;
1092};
1093
7abc4632
JB
1094static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1095 struct ieee80211_vif *vif,
6d9d32b8 1096 struct iwl_wowlan_status_data *status)
9b26b500 1097{
7abc4632 1098 struct sk_buff *pkt = NULL;
9b26b500
JB
1099 struct cfg80211_wowlan_wakeup wakeup = {
1100 .pattern_idx = -1,
1101 };
1102 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
6d9d32b8 1103 u32 reasons = status->wakeup_reasons;
9b26b500
JB
1104
1105 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1106 wakeup_report = NULL;
1107 goto report;
1108 }
1109
f444eb10 1110 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
9b26b500 1111 wakeup.magic_pkt = true;
9b26b500 1112
f444eb10 1113 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
9b26b500 1114 wakeup.pattern_idx =
6d9d32b8 1115 status->pattern_number;
9b26b500
JB
1116
1117 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1118 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1119 wakeup.disconnect = true;
1120
f444eb10 1121 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
9b26b500 1122 wakeup.gtk_rekey_failure = true;
9b26b500 1123
f444eb10 1124 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
9b26b500 1125 wakeup.rfkill_release = true;
9b26b500 1126
f444eb10 1127 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
9b26b500 1128 wakeup.eap_identity_req = true;
9b26b500 1129
f444eb10 1130 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
9b26b500 1131 wakeup.four_way_handshake = true;
9b26b500 1132
f0c2646a
JB
1133 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1134 wakeup.tcp_connlost = true;
1135
1136 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1137 wakeup.tcp_nomoretokens = true;
1138
1139 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1140 wakeup.tcp_match = true;
1141
9b26b500 1142 if (status->wake_packet_bufsize) {
6d9d32b8
JB
1143 int pktsize = status->wake_packet_bufsize;
1144 int pktlen = status->wake_packet_length;
f444eb10
JB
1145 const u8 *pktdata = status->wake_packet;
1146 struct ieee80211_hdr *hdr = (void *)pktdata;
1147 int truncated = pktlen - pktsize;
1148
1149 /* this would be a firmware bug */
1150 if (WARN_ON_ONCE(truncated < 0))
1151 truncated = 0;
1152
1153 if (ieee80211_is_data(hdr->frame_control)) {
1154 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1155 int ivlen = 0, icvlen = 4; /* also FCS */
9b26b500 1156
9b26b500
JB
1157 pkt = alloc_skb(pktsize, GFP_KERNEL);
1158 if (!pkt)
1159 goto report;
f444eb10
JB
1160
1161 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1162 pktdata += hdrlen;
1163 pktsize -= hdrlen;
1164
1165 if (ieee80211_has_protected(hdr->frame_control)) {
7abc4632
JB
1166 /*
1167 * This is unlocked and using gtk_i(c)vlen,
1168 * but since everything is under RTNL still
1169 * that's not really a problem - changing
1170 * it would be difficult.
1171 */
f444eb10
JB
1172 if (is_multicast_ether_addr(hdr->addr1)) {
1173 ivlen = mvm->gtk_ivlen;
1174 icvlen += mvm->gtk_icvlen;
1175 } else {
1176 ivlen = mvm->ptk_ivlen;
1177 icvlen += mvm->ptk_icvlen;
1178 }
1179 }
1180
1181 /* if truncated, FCS/ICV is (partially) gone */
1182 if (truncated >= icvlen) {
1183 icvlen = 0;
1184 truncated -= icvlen;
1185 } else {
1186 icvlen -= truncated;
1187 truncated = 0;
1188 }
1189
1190 pktsize -= ivlen + icvlen;
1191 pktdata += ivlen;
1192
1193 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1194
9b26b500
JB
1195 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1196 goto report;
1197 wakeup.packet = pkt->data;
1198 wakeup.packet_present_len = pkt->len;
f444eb10 1199 wakeup.packet_len = pkt->len - truncated;
9b26b500
JB
1200 wakeup.packet_80211 = false;
1201 } else {
f444eb10
JB
1202 int fcslen = 4;
1203
1204 if (truncated >= 4) {
1205 truncated -= 4;
1206 fcslen = 0;
1207 } else {
1208 fcslen -= truncated;
1209 truncated = 0;
1210 }
1211 pktsize -= fcslen;
9b26b500
JB
1212 wakeup.packet = status->wake_packet;
1213 wakeup.packet_present_len = pktsize;
f444eb10 1214 wakeup.packet_len = pktlen - truncated;
9b26b500
JB
1215 wakeup.packet_80211 = true;
1216 }
1217 }
1218
1219 report:
1220 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1221 kfree_skb(pkt);
7abc4632 1222}
9b26b500 1223
6d9d32b8
JB
1224static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1225 struct ieee80211_key_seq *seq)
1226{
1227 u64 pn;
1228
1229 pn = le64_to_cpu(sc->pn);
1230 seq->ccmp.pn[0] = pn >> 40;
1231 seq->ccmp.pn[1] = pn >> 32;
1232 seq->ccmp.pn[2] = pn >> 24;
1233 seq->ccmp.pn[3] = pn >> 16;
1234 seq->ccmp.pn[4] = pn >> 8;
1235 seq->ccmp.pn[5] = pn;
1236}
1237
1238static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1239 struct ieee80211_key_seq *seq)
1240{
1241 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1242 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1243}
1244
1245static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1246 struct ieee80211_key_conf *key)
1247{
1248 int tid;
1249
1250 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1251
1252 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1253 struct ieee80211_key_seq seq = {};
1254
1255 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1256 ieee80211_set_key_rx_seq(key, tid, &seq);
1257 }
1258}
1259
1260static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1261 struct ieee80211_key_conf *key)
1262{
1263 int tid;
1264
1265 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1266
1267 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1268 struct ieee80211_key_seq seq = {};
1269
1270 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1271 ieee80211_set_key_rx_seq(key, tid, &seq);
1272 }
1273}
1274
1275static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
3afec639 1276 struct iwl_wowlan_status *status)
6d9d32b8
JB
1277{
1278 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1279
1280 switch (key->cipher) {
1281 case WLAN_CIPHER_SUITE_CCMP:
1282 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1283 break;
1284 case WLAN_CIPHER_SUITE_TKIP:
1285 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1286 break;
1287 default:
1288 WARN_ON(1);
1289 }
1290}
1291
1292struct iwl_mvm_d3_gtk_iter_data {
3afec639 1293 struct iwl_wowlan_status *status;
6d9d32b8
JB
1294 void *last_gtk;
1295 u32 cipher;
1296 bool find_phase, unhandled_cipher;
1297 int num_keys;
1298};
1299
1300static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
1301 struct ieee80211_vif *vif,
1302 struct ieee80211_sta *sta,
1303 struct ieee80211_key_conf *key,
1304 void *_data)
1305{
1306 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1307
1308 if (data->unhandled_cipher)
1309 return;
1310
1311 switch (key->cipher) {
1312 case WLAN_CIPHER_SUITE_WEP40:
1313 case WLAN_CIPHER_SUITE_WEP104:
1314 /* ignore WEP completely, nothing to do */
1315 return;
1316 case WLAN_CIPHER_SUITE_CCMP:
1317 case WLAN_CIPHER_SUITE_TKIP:
1318 /* we support these */
1319 break;
1320 default:
1321 /* everything else (even CMAC for MFP) - disconnect from AP */
1322 data->unhandled_cipher = true;
1323 return;
1324 }
1325
1326 data->num_keys++;
1327
1328 /*
1329 * pairwise key - update sequence counters only;
1330 * note that this assumes no TDLS sessions are active
1331 */
1332 if (sta) {
1333 struct ieee80211_key_seq seq = {};
1334 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1335
1336 if (data->find_phase)
1337 return;
1338
1339 switch (key->cipher) {
1340 case WLAN_CIPHER_SUITE_CCMP:
1341 iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
1342 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
1343 break;
1344 case WLAN_CIPHER_SUITE_TKIP:
1345 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1346 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1347 break;
1348 }
1349 ieee80211_set_key_tx_seq(key, &seq);
1350
1351 /* that's it for this key */
1352 return;
1353 }
1354
1355 if (data->find_phase) {
1356 data->last_gtk = key;
1357 data->cipher = key->cipher;
1358 return;
1359 }
1360
1361 if (data->status->num_of_gtk_rekeys)
1362 ieee80211_remove_key(key);
1363 else if (data->last_gtk == key)
1364 iwl_mvm_set_key_rx_seq(key, data->status);
1365}
1366
1367static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1368 struct ieee80211_vif *vif,
3afec639 1369 struct iwl_wowlan_status *status)
6d9d32b8
JB
1370{
1371 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1372 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1373 .status = status,
1374 };
431591cc
EP
1375 u32 disconnection_reasons =
1376 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1377 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
6d9d32b8
JB
1378
1379 if (!status || !vif->bss_conf.bssid)
1380 return false;
1381
431591cc
EP
1382 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1383 return false;
1384
6d9d32b8
JB
1385 /* find last GTK that we used initially, if any */
1386 gtkdata.find_phase = true;
1387 ieee80211_iter_keys(mvm->hw, vif,
1388 iwl_mvm_d3_update_gtks, &gtkdata);
1389 /* not trying to keep connections with MFP/unhandled ciphers */
1390 if (gtkdata.unhandled_cipher)
1391 return false;
1392 if (!gtkdata.num_keys)
cfa88893 1393 goto out;
6d9d32b8
JB
1394 if (!gtkdata.last_gtk)
1395 return false;
1396
1397 /*
1398 * invalidate all other GTKs that might still exist and update
1399 * the one that we used
1400 */
1401 gtkdata.find_phase = false;
1402 ieee80211_iter_keys(mvm->hw, vif,
1403 iwl_mvm_d3_update_gtks, &gtkdata);
1404
1405 if (status->num_of_gtk_rekeys) {
1406 struct ieee80211_key_conf *key;
1407 struct {
1408 struct ieee80211_key_conf conf;
1409 u8 key[32];
1410 } conf = {
1411 .conf.cipher = gtkdata.cipher,
1412 .conf.keyidx = status->gtk.key_index,
1413 };
1414
1415 switch (gtkdata.cipher) {
1416 case WLAN_CIPHER_SUITE_CCMP:
1417 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1418 memcpy(conf.conf.key, status->gtk.decrypt_key,
1419 WLAN_KEY_LEN_CCMP);
1420 break;
1421 case WLAN_CIPHER_SUITE_TKIP:
1422 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1423 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1424 /* leave TX MIC key zeroed, we don't use it anyway */
1425 memcpy(conf.conf.key +
1426 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1427 status->gtk.tkip_mic_key, 8);
1428 break;
1429 }
1430
1431 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1432 if (IS_ERR(key))
1433 return false;
1434 iwl_mvm_set_key_rx_seq(key, status);
1435 }
1436
1437 if (status->num_of_gtk_rekeys) {
1438 __be64 replay_ctr =
1439 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1440 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1441 (void *)&replay_ctr, GFP_KERNEL);
1442 }
1443
cfa88893 1444out:
6d9d32b8
JB
1445 mvmvif->seqno_valid = true;
1446 /* +0x10 because the set API expects next-to-use, not last-used */
1447 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1448
1449 return true;
1450}
1451
7abc4632 1452/* releases the MVM mutex */
6d9d32b8 1453static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
7abc4632
JB
1454 struct ieee80211_vif *vif)
1455{
1456 u32 base = mvm->error_event_table;
1457 struct error_table_start {
1458 /* cf. struct iwl_error_event_table */
1459 u32 valid;
1460 u32 error_id;
1461 } err_info;
1462 struct iwl_host_cmd cmd = {
1463 .id = WOWLAN_GET_STATUSES,
a1022927 1464 .flags = CMD_WANT_SKB,
7abc4632 1465 };
6d9d32b8 1466 struct iwl_wowlan_status_data status;
3afec639 1467 struct iwl_wowlan_status *fw_status;
6d9d32b8
JB
1468 int ret, len, status_size, i;
1469 bool keep;
1470 struct ieee80211_sta *ap_sta;
1471 struct iwl_mvm_sta *mvm_ap_sta;
7abc4632
JB
1472
1473 iwl_trans_read_mem_bytes(mvm->trans, base,
1474 &err_info, sizeof(err_info));
1475
1476 if (err_info.valid) {
1477 IWL_INFO(mvm, "error table is valid (%d)\n",
1478 err_info.valid);
1479 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1480 struct cfg80211_wowlan_wakeup wakeup = {
1481 .rfkill_release = true,
1482 };
1483 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1484 GFP_KERNEL);
1485 }
1486 goto out_unlock;
1487 }
1488
1489 /* only for tracing for now */
a1022927 1490 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
7abc4632
JB
1491 if (ret)
1492 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1493
1494 ret = iwl_mvm_send_cmd(mvm, &cmd);
1495 if (ret) {
1496 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1497 goto out_unlock;
1498 }
1499
1500 /* RF-kill already asserted again... */
1501 if (!cmd.resp_pkt)
1502 goto out_unlock;
1503
3afec639 1504 status_size = sizeof(*fw_status);
6d9d32b8 1505
65b30348
JB
1506 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1507 if (len < status_size) {
7abc4632
JB
1508 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1509 goto out_free_resp;
1510 }
1511
3afec639
EG
1512 fw_status = (void *)cmd.resp_pkt->data;
1513
1514 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1515 for (i = 0; i < 8; i++)
1516 status.qos_seq_ctr[i] =
1517 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1518 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1519 status.wake_packet_length =
1520 le32_to_cpu(fw_status->wake_packet_length);
1521 status.wake_packet_bufsize =
1522 le32_to_cpu(fw_status->wake_packet_bufsize);
1523 status.wake_packet = fw_status->wake_packet;
7abc4632 1524
65b30348 1525 if (len != status_size + ALIGN(status.wake_packet_bufsize, 4)) {
7abc4632
JB
1526 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1527 goto out_free_resp;
1528 }
1529
6d9d32b8
JB
1530 /* still at hard-coded place 0 for D3 image */
1531 ap_sta = rcu_dereference_protected(
1532 mvm->fw_id_to_mac_id[0],
1533 lockdep_is_held(&mvm->mutex));
1534 if (IS_ERR_OR_NULL(ap_sta))
1535 goto out_free_resp;
1536
1537 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
1538 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1539 u16 seq = status.qos_seq_ctr[i];
1540 /* firmware stores last-used value, we store next value */
1541 seq += 0x10;
1542 mvm_ap_sta->tid_data[i].seq_number = seq;
1543 }
1544
7abc4632
JB
1545 /* now we have all the data we need, unlock to avoid mac80211 issues */
1546 mutex_unlock(&mvm->mutex);
1547
6d9d32b8
JB
1548 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1549
3afec639 1550 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
6d9d32b8 1551
7abc4632 1552 iwl_free_resp(&cmd);
6d9d32b8 1553 return keep;
7abc4632
JB
1554
1555 out_free_resp:
9b26b500 1556 iwl_free_resp(&cmd);
7abc4632
JB
1557 out_unlock:
1558 mutex_unlock(&mvm->mutex);
6d9d32b8 1559 return false;
9b26b500
JB
1560}
1561
afc66bb7
JB
1562static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1563{
1564#ifdef CONFIG_IWLWIFI_DEBUGFS
1565 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1566 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1567 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1568
1569 if (!mvm->store_d3_resume_sram)
1570 return;
1571
1572 if (!mvm->d3_resume_sram) {
1573 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1574 if (!mvm->d3_resume_sram)
1575 return;
1576 }
1577
1578 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1579#endif
1580}
1581
6d9d32b8
JB
1582static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1583 struct ieee80211_vif *vif)
1584{
1585 /* skip the one we keep connection on */
1586 if (data == vif)
1587 return;
1588
1589 if (vif->type == NL80211_IFTYPE_STATION)
1590 ieee80211_resume_disconnect(vif);
1591}
1592
debff618 1593static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
8ca151b5 1594{
8ca151b5
JB
1595 struct iwl_d3_iter_data resume_iter_data = {
1596 .mvm = mvm,
1597 };
1598 struct ieee80211_vif *vif = NULL;
8ca151b5
JB
1599 int ret;
1600 enum iwl_d3_status d3_status;
6d9d32b8 1601 bool keep = false;
8ca151b5
JB
1602
1603 mutex_lock(&mvm->mutex);
1604
1605 /* get the BSS vif pointer again */
1606 ieee80211_iterate_active_interfaces_atomic(
1607 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1608 iwl_mvm_d3_iface_iterator, &resume_iter_data);
1609
1610 if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
1611 goto out_unlock;
1612
1613 vif = resume_iter_data.vif;
1614
debff618 1615 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
8ca151b5
JB
1616 if (ret)
1617 goto out_unlock;
1618
1619 if (d3_status != IWL_D3_STATUS_ALIVE) {
1620 IWL_INFO(mvm, "Device was reset during suspend\n");
1621 goto out_unlock;
1622 }
1623
afc66bb7
JB
1624 /* query SRAM first in case we want event logging */
1625 iwl_mvm_read_d3_sram(mvm);
1626
6d9d32b8 1627 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
78c9df66
EP
1628#ifdef CONFIG_IWLWIFI_DEBUGFS
1629 if (keep)
1630 mvm->keep_vif = vif;
1631#endif
7abc4632
JB
1632 /* has unlocked the mutex, so skip that */
1633 goto out;
8ca151b5
JB
1634
1635 out_unlock:
1636 mutex_unlock(&mvm->mutex);
1637
7abc4632 1638 out:
6d9d32b8
JB
1639 if (!test)
1640 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1641 IEEE80211_IFACE_ITER_NORMAL,
1642 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
8ca151b5
JB
1643
1644 /* return 1 to reconfigure the device */
1645 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1646 return 1;
1647}
1648
debff618
JB
1649int iwl_mvm_resume(struct ieee80211_hw *hw)
1650{
1651 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1652
d15a747f
EP
1653 if (iwl_mvm_is_d0i3_supported(mvm)) {
1654 bool exit_now;
1655
1656 mutex_lock(&mvm->d0i3_suspend_mutex);
1657 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1658 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1659 &mvm->d0i3_suspend_flags);
1660 mutex_unlock(&mvm->d0i3_suspend_mutex);
1661 if (exit_now)
1662 _iwl_mvm_exit_d0i3(mvm);
1663 return 0;
1664 }
1665
debff618
JB
1666 return __iwl_mvm_resume(mvm, false);
1667}
1668
8ca151b5
JB
1669void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1670{
1671 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1672
1673 device_set_wakeup_enable(mvm->trans->dev, enabled);
1674}
debff618
JB
1675
1676#ifdef CONFIG_IWLWIFI_DEBUGFS
1677static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
1678{
1679 struct iwl_mvm *mvm = inode->i_private;
1680 int err;
1681
1682 if (mvm->d3_test_active)
1683 return -EBUSY;
1684
1685 file->private_data = inode->i_private;
1686
1687 ieee80211_stop_queues(mvm->hw);
1688 synchronize_net();
1689
1690 /* start pseudo D3 */
1691 rtnl_lock();
1692 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
1693 rtnl_unlock();
1694 if (err > 0)
1695 err = -EINVAL;
1696 if (err) {
1697 ieee80211_wake_queues(mvm->hw);
1698 return err;
1699 }
1700 mvm->d3_test_active = true;
78c9df66 1701 mvm->keep_vif = NULL;
debff618
JB
1702 return 0;
1703}
1704
1705static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
1706 size_t count, loff_t *ppos)
1707{
1708 struct iwl_mvm *mvm = file->private_data;
1709 u32 pme_asserted;
1710
1711 while (true) {
4ed80a86
EP
1712 /* read pme_ptr if available */
1713 if (mvm->d3_test_pme_ptr) {
1714 pme_asserted = iwl_trans_read_mem32(mvm->trans,
1715 mvm->d3_test_pme_ptr);
1716 if (pme_asserted)
1717 break;
1718 }
1719
debff618
JB
1720 if (msleep_interruptible(100))
1721 break;
1722 }
1723
1724 return 0;
1725}
1726
1727static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
1728 struct ieee80211_vif *vif)
1729{
78c9df66
EP
1730 /* skip the one we keep connection on */
1731 if (_data == vif)
1732 return;
1733
debff618
JB
1734 if (vif->type == NL80211_IFTYPE_STATION)
1735 ieee80211_connection_loss(vif);
1736}
1737
1738static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
1739{
1740 struct iwl_mvm *mvm = inode->i_private;
1741 int remaining_time = 10;
1742
1743 mvm->d3_test_active = false;
1744 __iwl_mvm_resume(mvm, true);
1745 iwl_abort_notification_waits(&mvm->notif_wait);
1746 ieee80211_restart_hw(mvm->hw);
1747
1748 /* wait for restart and disconnect all interfaces */
1749 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1750 remaining_time > 0) {
1751 remaining_time--;
1752 msleep(1000);
1753 }
1754
1755 if (remaining_time == 0)
1756 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
1757
1758 ieee80211_iterate_active_interfaces_atomic(
1759 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
78c9df66 1760 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
debff618
JB
1761
1762 ieee80211_wake_queues(mvm->hw);
1763
1764 return 0;
1765}
1766
1767const struct file_operations iwl_dbgfs_d3_test_ops = {
1768 .llseek = no_llseek,
1769 .open = iwl_mvm_d3_test_open,
1770 .read = iwl_mvm_d3_test_read,
1771 .release = iwl_mvm_d3_test_release,
1772};
1773#endif
This page took 0.195997 seconds and 5 git commands to generate.