iwlegacy: move under intel directory
[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.
65ff556b 9 * Copyright(c) 2013 - 2015 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.
65ff556b 35 * Copyright(c) 2013 - 2015 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) {
6645d5e4 277 u64 pn64;
8ca151b5
JB
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
6645d5e4
JB
282 pn64 = atomic64_read(&key->tx_pn);
283 aes_tx_sc->pn = cpu_to_le64(pn64);
8ca151b5
JB
284 } else {
285 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
286 }
287
288 /*
289 * For non-QoS this relies on the fact that both the uCode and
290 * mac80211 use TID 0 for checking the IV in the frames.
291 */
292 for (i = 0; i < IWL_NUM_RSC; i++) {
293 u8 *pn = seq.ccmp.pn;
294
295 ieee80211_get_key_rx_seq(key, i, &seq);
2cf5eb3a
JB
296 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
297 ((u64)pn[4] << 8) |
298 ((u64)pn[3] << 16) |
299 ((u64)pn[2] << 24) |
300 ((u64)pn[1] << 32) |
301 ((u64)pn[0] << 40));
8ca151b5
JB
302 }
303 data->use_rsc_tsc = true;
304 break;
305 }
306
307 /*
308 * The D3 firmware hardcodes the key offset 0 as the key it uses
309 * to transmit packets to the AP, i.e. the PTK.
310 */
311 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
312 key->hw_key_idx = 0;
f444eb10
JB
313 mvm->ptk_ivlen = key->iv_len;
314 mvm->ptk_icvlen = key->icv_len;
8ca151b5 315 } else {
5312e54d
JB
316 /*
317 * firmware only supports TSC/RSC for a single key,
318 * so if there are multiple keep overwriting them
319 * with new ones -- this relies on mac80211 doing
320 * list_add_tail().
321 */
322 key->hw_key_idx = 1;
f444eb10
JB
323 mvm->gtk_ivlen = key->iv_len;
324 mvm->gtk_icvlen = key->icv_len;
8ca151b5
JB
325 }
326
327 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
328 data->error = ret != 0;
329out_unlock:
330 mutex_unlock(&mvm->mutex);
331}
332
333static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
334 struct cfg80211_wowlan *wowlan)
335{
336 struct iwl_wowlan_patterns_cmd *pattern_cmd;
337 struct iwl_host_cmd cmd = {
338 .id = WOWLAN_PATTERNS,
339 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
8ca151b5
JB
340 };
341 int i, err;
342
343 if (!wowlan->n_patterns)
344 return 0;
345
346 cmd.len[0] = sizeof(*pattern_cmd) +
347 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
348
349 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
350 if (!pattern_cmd)
351 return -ENOMEM;
352
353 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
354
355 for (i = 0; i < wowlan->n_patterns; i++) {
356 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
357
358 memcpy(&pattern_cmd->patterns[i].mask,
359 wowlan->patterns[i].mask, mask_len);
360 memcpy(&pattern_cmd->patterns[i].pattern,
361 wowlan->patterns[i].pattern,
362 wowlan->patterns[i].pattern_len);
363 pattern_cmd->patterns[i].mask_size = mask_len;
364 pattern_cmd->patterns[i].pattern_size =
365 wowlan->patterns[i].pattern_len;
366 }
367
368 cmd.data[0] = pattern_cmd;
369 err = iwl_mvm_send_cmd(mvm, &cmd);
370 kfree(pattern_cmd);
371 return err;
372}
373
f0c2646a
JB
374enum iwl_mvm_tcp_packet_type {
375 MVM_TCP_TX_SYN,
376 MVM_TCP_RX_SYNACK,
377 MVM_TCP_TX_DATA,
378 MVM_TCP_RX_ACK,
379 MVM_TCP_RX_WAKE,
380 MVM_TCP_TX_FIN,
381};
382
383static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
384{
385 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
386 return cpu_to_le16(be16_to_cpu((__force __be16)check));
387}
388
b002c7e1 389static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
f0c2646a
JB
390 struct cfg80211_wowlan_tcp *tcp,
391 void *_pkt, u8 *mask,
392 __le16 *pseudo_hdr_csum,
393 enum iwl_mvm_tcp_packet_type ptype)
394{
395 struct {
396 struct ethhdr eth;
397 struct iphdr ip;
398 struct tcphdr tcp;
399 u8 data[];
400 } __packed *pkt = _pkt;
401 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
402 int i;
403
404 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
405 pkt->ip.version = 4;
406 pkt->ip.ihl = 5;
407 pkt->ip.protocol = IPPROTO_TCP;
408
409 switch (ptype) {
410 case MVM_TCP_TX_SYN:
411 case MVM_TCP_TX_DATA:
412 case MVM_TCP_TX_FIN:
413 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
414 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
415 pkt->ip.ttl = 128;
416 pkt->ip.saddr = tcp->src;
417 pkt->ip.daddr = tcp->dst;
418 pkt->tcp.source = cpu_to_be16(tcp->src_port);
419 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
420 /* overwritten for TX SYN later */
421 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
422 pkt->tcp.window = cpu_to_be16(65000);
423 break;
424 case MVM_TCP_RX_SYNACK:
425 case MVM_TCP_RX_ACK:
426 case MVM_TCP_RX_WAKE:
427 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
428 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
429 pkt->ip.saddr = tcp->dst;
430 pkt->ip.daddr = tcp->src;
431 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
432 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
433 break;
434 default:
435 WARN_ON(1);
436 return;
437 }
438
439 switch (ptype) {
440 case MVM_TCP_TX_SYN:
441 /* firmware assumes 8 option bytes - 8 NOPs for now */
442 memset(pkt->data, 0x01, 8);
443 ip_tot_len += 8;
444 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
445 pkt->tcp.syn = 1;
446 break;
447 case MVM_TCP_TX_DATA:
448 ip_tot_len += tcp->payload_len;
449 memcpy(pkt->data, tcp->payload, tcp->payload_len);
450 pkt->tcp.psh = 1;
451 pkt->tcp.ack = 1;
452 break;
453 case MVM_TCP_TX_FIN:
454 pkt->tcp.fin = 1;
455 pkt->tcp.ack = 1;
456 break;
457 case MVM_TCP_RX_SYNACK:
458 pkt->tcp.syn = 1;
459 pkt->tcp.ack = 1;
460 break;
461 case MVM_TCP_RX_ACK:
462 pkt->tcp.ack = 1;
463 break;
464 case MVM_TCP_RX_WAKE:
465 ip_tot_len += tcp->wake_len;
466 pkt->tcp.psh = 1;
467 pkt->tcp.ack = 1;
468 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
469 break;
470 }
471
472 switch (ptype) {
473 case MVM_TCP_TX_SYN:
474 case MVM_TCP_TX_DATA:
475 case MVM_TCP_TX_FIN:
476 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
477 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
478 break;
479 case MVM_TCP_RX_WAKE:
480 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
481 u8 tmp = tcp->wake_mask[i];
482 mask[i + 6] |= tmp << 6;
483 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
484 mask[i + 7] = tmp >> 2;
485 }
486 /* fall through for ethernet/IP/TCP headers mask */
487 case MVM_TCP_RX_SYNACK:
488 case MVM_TCP_RX_ACK:
489 mask[0] = 0xff; /* match ethernet */
490 /*
491 * match ethernet, ip.version, ip.ihl
492 * the ip.ihl half byte is really masked out by firmware
493 */
494 mask[1] = 0x7f;
495 mask[2] = 0x80; /* match ip.protocol */
496 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
497 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
498 mask[5] = 0x80; /* match tcp flags */
499 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
500 break;
501 };
502
503 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
504 pkt->ip.saddr, pkt->ip.daddr);
505}
506
507static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
508 struct ieee80211_vif *vif,
509 struct cfg80211_wowlan_tcp *tcp)
510{
511 struct iwl_wowlan_remote_wake_config *cfg;
512 struct iwl_host_cmd cmd = {
513 .id = REMOTE_WAKE_CONFIG_CMD,
514 .len = { sizeof(*cfg), },
515 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
f0c2646a
JB
516 };
517 int ret;
518
519 if (!tcp)
520 return 0;
521
522 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
523 if (!cfg)
524 return -ENOMEM;
525 cmd.data[0] = cfg;
526
527 cfg->max_syn_retries = 10;
528 cfg->max_data_retries = 10;
529 cfg->tcp_syn_ack_timeout = 1; /* seconds */
530 cfg->tcp_ack_timeout = 1; /* seconds */
531
532 /* SYN (TX) */
533 iwl_mvm_build_tcp_packet(
b002c7e1 534 vif, tcp, cfg->syn_tx.data, NULL,
f0c2646a
JB
535 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
536 MVM_TCP_TX_SYN);
537 cfg->syn_tx.info.tcp_payload_length = 0;
538
539 /* SYN/ACK (RX) */
540 iwl_mvm_build_tcp_packet(
b002c7e1 541 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
f0c2646a
JB
542 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
543 MVM_TCP_RX_SYNACK);
544 cfg->synack_rx.info.tcp_payload_length = 0;
545
546 /* KEEPALIVE/ACK (TX) */
547 iwl_mvm_build_tcp_packet(
b002c7e1 548 vif, tcp, cfg->keepalive_tx.data, NULL,
f0c2646a
JB
549 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
550 MVM_TCP_TX_DATA);
551 cfg->keepalive_tx.info.tcp_payload_length =
552 cpu_to_le16(tcp->payload_len);
553 cfg->sequence_number_offset = tcp->payload_seq.offset;
554 /* length must be 0..4, the field is little endian */
555 cfg->sequence_number_length = tcp->payload_seq.len;
556 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
557 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
558 if (tcp->payload_tok.len) {
559 cfg->token_offset = tcp->payload_tok.offset;
560 cfg->token_length = tcp->payload_tok.len;
561 cfg->num_tokens =
562 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
563 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
564 tcp->tokens_size);
565 } else {
566 /* set tokens to max value to almost never run out */
567 cfg->num_tokens = cpu_to_le16(65535);
568 }
569
570 /* ACK (RX) */
571 iwl_mvm_build_tcp_packet(
b002c7e1 572 vif, tcp, cfg->keepalive_ack_rx.data,
f0c2646a
JB
573 cfg->keepalive_ack_rx.rx_mask,
574 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
575 MVM_TCP_RX_ACK);
576 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
577
578 /* WAKEUP (RX) */
579 iwl_mvm_build_tcp_packet(
b002c7e1 580 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
f0c2646a
JB
581 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
582 MVM_TCP_RX_WAKE);
583 cfg->wake_rx.info.tcp_payload_length =
584 cpu_to_le16(tcp->wake_len);
585
586 /* FIN */
587 iwl_mvm_build_tcp_packet(
b002c7e1 588 vif, tcp, cfg->fin_tx.data, NULL,
f0c2646a
JB
589 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
590 MVM_TCP_TX_FIN);
591 cfg->fin_tx.info.tcp_payload_length = 0;
592
593 ret = iwl_mvm_send_cmd(mvm, &cmd);
594 kfree(cfg);
595
596 return ret;
597}
598
8ca151b5
JB
599static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
600 struct ieee80211_sta *ap_sta)
601{
602 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
603 struct ieee80211_chanctx_conf *ctx;
604 u8 chains_static, chains_dynamic;
605 struct cfg80211_chan_def chandef;
606 int ret, i;
607 struct iwl_binding_cmd binding_cmd = {};
608 struct iwl_time_quota_cmd quota_cmd = {};
609 u32 status;
610
611 /* add back the PHY */
612 if (WARN_ON(!mvmvif->phy_ctxt))
613 return -EINVAL;
614
615 rcu_read_lock();
616 ctx = rcu_dereference(vif->chanctx_conf);
617 if (WARN_ON(!ctx)) {
618 rcu_read_unlock();
619 return -EINVAL;
620 }
621 chandef = ctx->def;
622 chains_static = ctx->rx_chains_static;
623 chains_dynamic = ctx->rx_chains_dynamic;
624 rcu_read_unlock();
625
626 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
627 chains_static, chains_dynamic);
628 if (ret)
629 return ret;
630
631 /* add back the MAC */
632 mvmvif->uploaded = false;
633
634 if (WARN_ON(!vif->bss_conf.assoc))
635 return -EINVAL;
bca49d9a 636
8ca151b5 637 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
8ca151b5
JB
638 if (ret)
639 return ret;
640
641 /* add back binding - XXX refactor? */
642 binding_cmd.id_and_color =
643 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
644 mvmvif->phy_ctxt->color));
645 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
646 binding_cmd.phy =
647 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
648 mvmvif->phy_ctxt->color));
649 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
650 mvmvif->color));
651 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
652 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
653
654 status = 0;
655 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
656 sizeof(binding_cmd), &binding_cmd,
657 &status);
658 if (ret) {
659 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
660 return ret;
661 }
662
663 if (status) {
664 IWL_ERR(mvm, "Binding command failed: %u\n", status);
665 return -EIO;
666 }
667
7a453973 668 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
8ca151b5
JB
669 if (ret)
670 return ret;
671 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
672
3dfd3a97 673 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
8ca151b5
JB
674 if (ret)
675 return ret;
676
677 /* and some quota */
678 quota_cmd.quotas[0].id_and_color =
679 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
680 mvmvif->phy_ctxt->color));
9a3daf82
EG
681 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
682 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
8ca151b5
JB
683
684 for (i = 1; i < MAX_BINDINGS; i++)
685 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
686
a1022927 687 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
8ca151b5
JB
688 sizeof(quota_cmd), &quota_cmd);
689 if (ret)
690 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
691
47c8b154
JD
692 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
693 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
694
8ca151b5
JB
695 return 0;
696}
697
4ac6cb59
JB
698static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
699 struct ieee80211_vif *vif)
700{
6d9d32b8
JB
701 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
702 struct iwl_nonqos_seq_query_cmd query_cmd = {
703 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
704 .mac_id_n_color =
705 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
706 mvmvif->color)),
707 };
4ac6cb59
JB
708 struct iwl_host_cmd cmd = {
709 .id = NON_QOS_TX_COUNTER_CMD,
a1022927 710 .flags = CMD_WANT_SKB,
4ac6cb59
JB
711 };
712 int err;
713 u32 size;
714
3afec639
EG
715 cmd.data[0] = &query_cmd;
716 cmd.len[0] = sizeof(query_cmd);
6d9d32b8 717
4ac6cb59
JB
718 err = iwl_mvm_send_cmd(mvm, &cmd);
719 if (err)
720 return err;
721
65b30348 722 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
6d9d32b8 723 if (size < sizeof(__le16)) {
4ac6cb59 724 err = -EINVAL;
6d9d32b8
JB
725 } else {
726 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
3afec639
EG
727 /* firmware returns next, not last-used seqno */
728 err = (u16) (err - 0x10);
6d9d32b8 729 }
4ac6cb59
JB
730
731 iwl_free_resp(&cmd);
732 return err;
733}
734
6d9d32b8
JB
735void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
736{
737 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
738 struct iwl_nonqos_seq_query_cmd query_cmd = {
739 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
740 .mac_id_n_color =
741 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
742 mvmvif->color)),
743 .value = cpu_to_le16(mvmvif->seqno),
744 };
745
746 /* return if called during restart, not resume from D3 */
747 if (!mvmvif->seqno_valid)
748 return;
749
750 mvmvif->seqno_valid = false;
751
a1022927 752 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
6d9d32b8
JB
753 sizeof(query_cmd), &query_cmd))
754 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
755}
756
671b5820
LC
757static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
758{
c7d42480 759 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
671b5820
LC
760
761 iwl_trans_stop_device(mvm->trans);
762
763 /*
764 * Set the HW restart bit -- this is mostly true as we're
765 * going to load new firmware and reprogram that, though
766 * the reprogramming is going to be manual to avoid adding
767 * all the MACs that aren't support.
768 * We don't have to clear up everything though because the
769 * reprogramming is manual. When we resume, we'll actually
770 * go through a proper restart sequence again to switch
771 * back to the runtime firmware image.
772 */
773 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
774
775 /* We reprogram keys and shouldn't allocate new key indices */
776 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
777
778 mvm->ptk_ivlen = 0;
779 mvm->ptk_icvlen = 0;
780 mvm->ptk_ivlen = 0;
781 mvm->ptk_icvlen = 0;
782
783 return iwl_mvm_load_d3_fw(mvm);
784}
785
671b5820
LC
786static int
787iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
788 struct cfg80211_wowlan *wowlan,
c8b06a99 789 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
671b5820
LC
790 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
791 struct ieee80211_sta *ap_sta)
8ca151b5 792{
1a95c8df 793 int ret;
9d8ce6af 794 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
8ca151b5 795
c8b06a99 796 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
8ca151b5 797
c8b06a99 798 wowlan_config_cmd->is_11n_connection =
1a95c8df 799 ap_sta->ht_cap.ht_supported;
8ca151b5 800
4ac6cb59
JB
801 /* Query the last used seqno and set it */
802 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
803 if (ret < 0)
671b5820 804 return ret;
8ca151b5 805
c8b06a99 806 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
671b5820 807
c8b06a99 808 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
8ca151b5
JB
809
810 if (wowlan->disconnect)
c8b06a99 811 wowlan_config_cmd->wakeup_filter |=
8ca151b5
JB
812 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
813 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
814 if (wowlan->magic_pkt)
c8b06a99 815 wowlan_config_cmd->wakeup_filter |=
8ca151b5
JB
816 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
817 if (wowlan->gtk_rekey_failure)
c8b06a99 818 wowlan_config_cmd->wakeup_filter |=
8ca151b5
JB
819 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
820 if (wowlan->eap_identity_req)
c8b06a99 821 wowlan_config_cmd->wakeup_filter |=
8ca151b5
JB
822 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
823 if (wowlan->four_way_handshake)
c8b06a99 824 wowlan_config_cmd->wakeup_filter |=
8ca151b5
JB
825 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
826 if (wowlan->n_patterns)
c8b06a99 827 wowlan_config_cmd->wakeup_filter |=
8ca151b5
JB
828 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
829
830 if (wowlan->rfkill_release)
c8b06a99 831 wowlan_config_cmd->wakeup_filter |=
8ca151b5
JB
832 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
833
f0c2646a
JB
834 if (wowlan->tcp) {
835 /*
6039f3e1
JB
836 * Set the "link change" (really "link lost") flag as well
837 * since that implies losing the TCP connection.
f0c2646a 838 */
c8b06a99 839 wowlan_config_cmd->wakeup_filter |=
f0c2646a
JB
840 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
841 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
842 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
843 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
844 }
845
671b5820
LC
846 return 0;
847}
f444eb10 848
671b5820
LC
849static int
850iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
851 struct cfg80211_wowlan *wowlan,
c8b06a99 852 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
671b5820
LC
853 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
854 struct ieee80211_sta *ap_sta)
855{
856 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
857 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
858 struct wowlan_key_data key_data = {
859 .use_rsc_tsc = false,
860 .tkip = &tkip_cmd,
861 .use_tkip = false,
862 };
863 int ret;
8ca151b5 864
3acc952b
LC
865 ret = iwl_mvm_switch_to_d3(mvm);
866 if (ret)
867 return ret;
868
8ca151b5
JB
869 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
870 if (ret)
671b5820
LC
871 return ret;
872
873 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
874 if (!key_data.rsc_tsc)
875 return -ENOMEM;
8ca151b5
JB
876
877 if (!iwlwifi_mod_params.sw_crypto) {
878 /*
879 * This needs to be unlocked due to lock ordering
880 * constraints. Since we're in the suspend path
881 * that isn't really a problem though.
882 */
883 mutex_unlock(&mvm->mutex);
884 ieee80211_iter_keys(mvm->hw, vif,
885 iwl_mvm_wowlan_program_keys,
886 &key_data);
887 mutex_lock(&mvm->mutex);
888 if (key_data.error) {
889 ret = -EIO;
890 goto out;
891 }
892
893 if (key_data.use_rsc_tsc) {
894 struct iwl_host_cmd rsc_tsc_cmd = {
895 .id = WOWLAN_TSC_RSC_PARAM,
8ca151b5
JB
896 .data[0] = key_data.rsc_tsc,
897 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
898 .len[0] = sizeof(*key_data.rsc_tsc),
899 };
900
901 ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
902 if (ret)
903 goto out;
904 }
905
906 if (key_data.use_tkip) {
907 ret = iwl_mvm_send_cmd_pdu(mvm,
908 WOWLAN_TKIP_PARAM,
a1022927 909 0, sizeof(tkip_cmd),
8ca151b5
JB
910 &tkip_cmd);
911 if (ret)
912 goto out;
913 }
914
915 if (mvmvif->rekey_data.valid) {
916 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
917 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
918 NL80211_KCK_LEN);
919 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
920 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
921 NL80211_KEK_LEN);
922 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
923 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
924
925 ret = iwl_mvm_send_cmd_pdu(mvm,
a1022927 926 WOWLAN_KEK_KCK_MATERIAL, 0,
8ca151b5
JB
927 sizeof(kek_kck_cmd),
928 &kek_kck_cmd);
929 if (ret)
930 goto out;
931 }
932 }
933
c8b06a99
EG
934 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
935 sizeof(*wowlan_config_cmd),
936 wowlan_config_cmd);
8ca151b5
JB
937 if (ret)
938 goto out;
939
940 ret = iwl_mvm_send_patterns(mvm, wowlan);
941 if (ret)
942 goto out;
943
a1022927 944 ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
8ca151b5
JB
945 if (ret)
946 goto out;
947
f0c2646a 948 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
671b5820
LC
949
950out:
951 kfree(key_data.rsc_tsc);
952 return ret;
953}
954
3acc952b
LC
955static int
956iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
957 struct cfg80211_wowlan *wowlan,
958 struct cfg80211_sched_scan_request *nd_config,
959 struct ieee80211_vif *vif)
960{
c8b06a99 961 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
3acc952b
LC
962 int ret;
963
964 ret = iwl_mvm_switch_to_d3(mvm);
965 if (ret)
966 return ret;
967
968 /* rfkill release can be either for wowlan or netdetect */
969 if (wowlan->rfkill_release)
c8b06a99 970 wowlan_config_cmd.wakeup_filter |=
3acc952b
LC
971 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
972
c8b06a99
EG
973 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
974 sizeof(wowlan_config_cmd),
975 &wowlan_config_cmd);
3acc952b
LC
976 if (ret)
977 return ret;
978
19945dfb
LC
979 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
980 IWL_MVM_SCAN_NETDETECT);
d9718da8
LC
981 if (ret)
982 return ret;
3acc952b 983
8ed4e659 984 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
d9718da8
LC
985 return -EBUSY;
986
8ed4e659 987 /* save the sched scan matchsets... */
d9718da8
LC
988 if (nd_config->n_match_sets) {
989 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
990 sizeof(*nd_config->match_sets) *
991 nd_config->n_match_sets,
992 GFP_KERNEL);
993 if (mvm->nd_match_sets)
994 mvm->n_nd_match_sets = nd_config->n_match_sets;
995 }
996
8ed4e659
LC
997 /* ...and the sched scan channels for later reporting */
998 mvm->nd_channels = kmemdup(nd_config->channels,
999 sizeof(*nd_config->channels) *
1000 nd_config->n_channels,
1001 GFP_KERNEL);
1002 if (mvm->nd_channels)
1003 mvm->n_nd_channels = nd_config->n_channels;
1004
d9718da8
LC
1005 return 0;
1006}
1007
1008static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1009{
1010 kfree(mvm->nd_match_sets);
1011 mvm->nd_match_sets = NULL;
1012 mvm->n_nd_match_sets = 0;
8ed4e659
LC
1013 kfree(mvm->nd_channels);
1014 mvm->nd_channels = NULL;
1015 mvm->n_nd_channels = 0;
3acc952b
LC
1016}
1017
671b5820
LC
1018static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1019 struct cfg80211_wowlan *wowlan,
1020 bool test)
1021{
1022 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
671b5820
LC
1023 struct ieee80211_vif *vif = NULL;
1024 struct iwl_mvm_vif *mvmvif = NULL;
1025 struct ieee80211_sta *ap_sta = NULL;
671b5820
LC
1026 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1027 /*
1028 * Program the minimum sleep time to 10 seconds, as many
1029 * platforms have issues processing a wakeup signal while
1030 * still being in the process of suspending.
1031 */
1032 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1033 };
1034 struct iwl_host_cmd d3_cfg_cmd = {
1035 .id = D3_CONFIG_CMD,
1036 .flags = CMD_WANT_SKB,
1037 .data[0] = &d3_cfg_cmd_data,
1038 .len[0] = sizeof(d3_cfg_cmd_data),
1039 };
1040 int ret;
1041 int len __maybe_unused;
1042
1043 if (!wowlan) {
1044 /*
1045 * mac80211 shouldn't get here, but for D3 test
1046 * it doesn't warrant a warning
1047 */
1048 WARN_ON(!test);
1049 return -EINVAL;
1050 }
1051
1052 mutex_lock(&mvm->mutex);
1053
7f549e2c
LC
1054 vif = iwl_mvm_get_bss_vif(mvm);
1055 if (IS_ERR_OR_NULL(vif)) {
671b5820
LC
1056 ret = 1;
1057 goto out_noreset;
1058 }
1059
671b5820
LC
1060 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1061
3acc952b
LC
1062 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
1063 /* if we're not associated, this must be netdetect */
1064 if (!wowlan->nd_config && !mvm->nd_config) {
1065 ret = 1;
1066 goto out_noreset;
1067 }
1068
1069 ret = iwl_mvm_netdetect_config(
1070 mvm, wowlan, wowlan->nd_config ?: mvm->nd_config, vif);
1071 if (ret)
1072 goto out;
2021a89d
LC
1073
1074 mvm->net_detect = true;
3acc952b 1075 } else {
c8b06a99 1076 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
3acc952b 1077
1e4d19ca
LC
1078 ap_sta = rcu_dereference_protected(
1079 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1080 lockdep_is_held(&mvm->mutex));
1081 if (IS_ERR_OR_NULL(ap_sta)) {
1082 ret = -EINVAL;
1083 goto out_noreset;
1084 }
671b5820 1085
1e4d19ca
LC
1086 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1087 vif, mvmvif, ap_sta);
1088 if (ret)
1089 goto out_noreset;
1e4d19ca
LC
1090 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1091 vif, mvmvif, ap_sta);
1092 if (ret)
3c2f3b20 1093 goto out;
2021a89d
LC
1094
1095 mvm->net_detect = false;
1e4d19ca 1096 }
f0c2646a 1097
c1cb92fc 1098 ret = iwl_mvm_power_update_device(mvm);
64b928c4
AB
1099 if (ret)
1100 goto out;
1101
999609f1 1102 ret = iwl_mvm_power_update_mac(mvm);
ee1e8422
AB
1103 if (ret)
1104 goto out;
1105
b0114714
JB
1106#ifdef CONFIG_IWLWIFI_DEBUGFS
1107 if (mvm->d3_wake_sysassert)
1108 d3_cfg_cmd_data.wakeup_flags |=
1109 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1110#endif
1111
8ca151b5 1112 /* must be last -- this switches firmware state */
debff618 1113 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
8ca151b5
JB
1114 if (ret)
1115 goto out;
debff618 1116#ifdef CONFIG_IWLWIFI_DEBUGFS
65b30348
JB
1117 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1118 if (len >= sizeof(u32)) {
debff618
JB
1119 mvm->d3_test_pme_ptr =
1120 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
debff618
JB
1121 }
1122#endif
1123 iwl_free_resp(&d3_cfg_cmd);
8ca151b5
JB
1124
1125 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1126
debff618 1127 iwl_trans_d3_suspend(mvm->trans, test);
8ca151b5 1128 out:
d9718da8 1129 if (ret < 0) {
48e67751 1130 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
8ca151b5 1131 ieee80211_restart_hw(mvm->hw);
d9718da8
LC
1132 iwl_mvm_free_nd(mvm);
1133 }
b70ab727 1134 out_noreset:
8ca151b5
JB
1135 mutex_unlock(&mvm->mutex);
1136
1137 return ret;
1138}
1139
6735943f
EP
1140static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
1141{
1142 struct iwl_notification_wait wait_d3;
6eb031d2 1143 static const u16 d3_notif[] = { D3_CONFIG_CMD };
6735943f
EP
1144 int ret;
1145
1146 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
1147 d3_notif, ARRAY_SIZE(d3_notif),
1148 NULL, NULL);
1149
1150 ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
1151 if (ret)
1152 goto remove_notif;
1153
1154 ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
1155 WARN_ON_ONCE(ret);
1156 return ret;
1157
1158remove_notif:
1159 iwl_remove_notification(&mvm->notif_wait, &wait_d3);
1160 return ret;
1161}
1162
debff618
JB
1163int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1164{
d15a747f 1165 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
c43fe907
EP
1166 int ret;
1167
cf8c3cff
LC
1168 /* make sure the d0i3 exit work is not pending */
1169 flush_work(&mvm->d0i3_exit_work);
1170
c43fe907
EP
1171 ret = iwl_trans_suspend(mvm->trans);
1172 if (ret)
1173 return ret;
d15a747f 1174
54154618
EP
1175 mvm->trans->wowlan_d0i3 = wowlan->any;
1176 if (mvm->trans->wowlan_d0i3) {
91742449 1177 /* 'any' trigger means d0i3 usage */
6735943f 1178 if (mvm->trans->d0i3_mode == IWL_D0I3_MODE_ON_SUSPEND) {
c43fe907 1179 ret = iwl_mvm_enter_d0i3_sync(mvm);
6735943f
EP
1180
1181 if (ret)
1182 return ret;
1183 }
1184
d15a747f
EP
1185 mutex_lock(&mvm->d0i3_suspend_mutex);
1186 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1187 mutex_unlock(&mvm->d0i3_suspend_mutex);
6dfb36c8
EP
1188
1189 iwl_trans_d3_suspend(mvm->trans, false);
1190
d15a747f
EP
1191 return 0;
1192 }
1193
debff618
JB
1194 return __iwl_mvm_suspend(hw, wowlan, false);
1195}
1196
6d9d32b8
JB
1197/* converted data from the different status responses */
1198struct iwl_wowlan_status_data {
1199 u16 pattern_number;
1200 u16 qos_seq_ctr[8];
1201 u32 wakeup_reasons;
1202 u32 wake_packet_length;
1203 u32 wake_packet_bufsize;
1204 const u8 *wake_packet;
1205};
1206
7abc4632
JB
1207static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1208 struct ieee80211_vif *vif,
6d9d32b8 1209 struct iwl_wowlan_status_data *status)
9b26b500 1210{
7abc4632 1211 struct sk_buff *pkt = NULL;
9b26b500
JB
1212 struct cfg80211_wowlan_wakeup wakeup = {
1213 .pattern_idx = -1,
1214 };
1215 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
6d9d32b8 1216 u32 reasons = status->wakeup_reasons;
9b26b500
JB
1217
1218 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1219 wakeup_report = NULL;
1220 goto report;
1221 }
1222
f444eb10 1223 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
9b26b500 1224 wakeup.magic_pkt = true;
9b26b500 1225
f444eb10 1226 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
9b26b500 1227 wakeup.pattern_idx =
6d9d32b8 1228 status->pattern_number;
9b26b500
JB
1229
1230 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1231 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1232 wakeup.disconnect = true;
1233
f444eb10 1234 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
9b26b500 1235 wakeup.gtk_rekey_failure = true;
9b26b500 1236
f444eb10 1237 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
9b26b500 1238 wakeup.rfkill_release = true;
9b26b500 1239
f444eb10 1240 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
9b26b500 1241 wakeup.eap_identity_req = true;
9b26b500 1242
f444eb10 1243 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
9b26b500 1244 wakeup.four_way_handshake = true;
9b26b500 1245
f0c2646a
JB
1246 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1247 wakeup.tcp_connlost = true;
1248
1249 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1250 wakeup.tcp_nomoretokens = true;
1251
1252 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1253 wakeup.tcp_match = true;
1254
9b26b500 1255 if (status->wake_packet_bufsize) {
6d9d32b8
JB
1256 int pktsize = status->wake_packet_bufsize;
1257 int pktlen = status->wake_packet_length;
f444eb10
JB
1258 const u8 *pktdata = status->wake_packet;
1259 struct ieee80211_hdr *hdr = (void *)pktdata;
1260 int truncated = pktlen - pktsize;
1261
1262 /* this would be a firmware bug */
1263 if (WARN_ON_ONCE(truncated < 0))
1264 truncated = 0;
1265
1266 if (ieee80211_is_data(hdr->frame_control)) {
1267 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1268 int ivlen = 0, icvlen = 4; /* also FCS */
9b26b500 1269
9b26b500
JB
1270 pkt = alloc_skb(pktsize, GFP_KERNEL);
1271 if (!pkt)
1272 goto report;
f444eb10
JB
1273
1274 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1275 pktdata += hdrlen;
1276 pktsize -= hdrlen;
1277
1278 if (ieee80211_has_protected(hdr->frame_control)) {
7abc4632
JB
1279 /*
1280 * This is unlocked and using gtk_i(c)vlen,
1281 * but since everything is under RTNL still
1282 * that's not really a problem - changing
1283 * it would be difficult.
1284 */
f444eb10
JB
1285 if (is_multicast_ether_addr(hdr->addr1)) {
1286 ivlen = mvm->gtk_ivlen;
1287 icvlen += mvm->gtk_icvlen;
1288 } else {
1289 ivlen = mvm->ptk_ivlen;
1290 icvlen += mvm->ptk_icvlen;
1291 }
1292 }
1293
1294 /* if truncated, FCS/ICV is (partially) gone */
1295 if (truncated >= icvlen) {
1296 icvlen = 0;
1297 truncated -= icvlen;
1298 } else {
1299 icvlen -= truncated;
1300 truncated = 0;
1301 }
1302
1303 pktsize -= ivlen + icvlen;
1304 pktdata += ivlen;
1305
1306 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1307
9b26b500
JB
1308 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1309 goto report;
1310 wakeup.packet = pkt->data;
1311 wakeup.packet_present_len = pkt->len;
f444eb10 1312 wakeup.packet_len = pkt->len - truncated;
9b26b500
JB
1313 wakeup.packet_80211 = false;
1314 } else {
f444eb10
JB
1315 int fcslen = 4;
1316
1317 if (truncated >= 4) {
1318 truncated -= 4;
1319 fcslen = 0;
1320 } else {
1321 fcslen -= truncated;
1322 truncated = 0;
1323 }
1324 pktsize -= fcslen;
9b26b500
JB
1325 wakeup.packet = status->wake_packet;
1326 wakeup.packet_present_len = pktsize;
f444eb10 1327 wakeup.packet_len = pktlen - truncated;
9b26b500
JB
1328 wakeup.packet_80211 = true;
1329 }
1330 }
1331
1332 report:
1333 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1334 kfree_skb(pkt);
7abc4632 1335}
9b26b500 1336
6d9d32b8
JB
1337static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1338 struct ieee80211_key_seq *seq)
1339{
1340 u64 pn;
1341
1342 pn = le64_to_cpu(sc->pn);
1343 seq->ccmp.pn[0] = pn >> 40;
1344 seq->ccmp.pn[1] = pn >> 32;
1345 seq->ccmp.pn[2] = pn >> 24;
1346 seq->ccmp.pn[3] = pn >> 16;
1347 seq->ccmp.pn[4] = pn >> 8;
1348 seq->ccmp.pn[5] = pn;
1349}
1350
1351static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1352 struct ieee80211_key_seq *seq)
1353{
1354 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1355 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1356}
1357
1358static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1359 struct ieee80211_key_conf *key)
1360{
1361 int tid;
1362
1363 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1364
1365 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1366 struct ieee80211_key_seq seq = {};
1367
1368 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1369 ieee80211_set_key_rx_seq(key, tid, &seq);
1370 }
1371}
1372
1373static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1374 struct ieee80211_key_conf *key)
1375{
1376 int tid;
1377
1378 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1379
1380 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1381 struct ieee80211_key_seq seq = {};
1382
1383 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1384 ieee80211_set_key_rx_seq(key, tid, &seq);
1385 }
1386}
1387
1388static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
3afec639 1389 struct iwl_wowlan_status *status)
6d9d32b8
JB
1390{
1391 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1392
1393 switch (key->cipher) {
1394 case WLAN_CIPHER_SUITE_CCMP:
1395 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1396 break;
1397 case WLAN_CIPHER_SUITE_TKIP:
1398 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1399 break;
1400 default:
1401 WARN_ON(1);
1402 }
1403}
1404
1405struct iwl_mvm_d3_gtk_iter_data {
3afec639 1406 struct iwl_wowlan_status *status;
6d9d32b8
JB
1407 void *last_gtk;
1408 u32 cipher;
1409 bool find_phase, unhandled_cipher;
1410 int num_keys;
1411};
1412
1413static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
1414 struct ieee80211_vif *vif,
1415 struct ieee80211_sta *sta,
1416 struct ieee80211_key_conf *key,
1417 void *_data)
1418{
1419 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1420
1421 if (data->unhandled_cipher)
1422 return;
1423
1424 switch (key->cipher) {
1425 case WLAN_CIPHER_SUITE_WEP40:
1426 case WLAN_CIPHER_SUITE_WEP104:
1427 /* ignore WEP completely, nothing to do */
1428 return;
1429 case WLAN_CIPHER_SUITE_CCMP:
1430 case WLAN_CIPHER_SUITE_TKIP:
1431 /* we support these */
1432 break;
1433 default:
1434 /* everything else (even CMAC for MFP) - disconnect from AP */
1435 data->unhandled_cipher = true;
1436 return;
1437 }
1438
1439 data->num_keys++;
1440
1441 /*
1442 * pairwise key - update sequence counters only;
1443 * note that this assumes no TDLS sessions are active
1444 */
1445 if (sta) {
1446 struct ieee80211_key_seq seq = {};
1447 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1448
1449 if (data->find_phase)
1450 return;
1451
1452 switch (key->cipher) {
1453 case WLAN_CIPHER_SUITE_CCMP:
6d9d32b8 1454 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
6645d5e4 1455 atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
6d9d32b8
JB
1456 break;
1457 case WLAN_CIPHER_SUITE_TKIP:
1458 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1459 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
6645d5e4 1460 ieee80211_set_key_tx_seq(key, &seq);
6d9d32b8
JB
1461 break;
1462 }
6d9d32b8
JB
1463
1464 /* that's it for this key */
1465 return;
1466 }
1467
1468 if (data->find_phase) {
1469 data->last_gtk = key;
1470 data->cipher = key->cipher;
1471 return;
1472 }
1473
1474 if (data->status->num_of_gtk_rekeys)
1475 ieee80211_remove_key(key);
1476 else if (data->last_gtk == key)
1477 iwl_mvm_set_key_rx_seq(key, data->status);
1478}
1479
1480static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1481 struct ieee80211_vif *vif,
3afec639 1482 struct iwl_wowlan_status *status)
6d9d32b8
JB
1483{
1484 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1485 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1486 .status = status,
1487 };
431591cc
EP
1488 u32 disconnection_reasons =
1489 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1490 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
6d9d32b8
JB
1491
1492 if (!status || !vif->bss_conf.bssid)
1493 return false;
1494
431591cc
EP
1495 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1496 return false;
1497
6d9d32b8
JB
1498 /* find last GTK that we used initially, if any */
1499 gtkdata.find_phase = true;
1500 ieee80211_iter_keys(mvm->hw, vif,
1501 iwl_mvm_d3_update_gtks, &gtkdata);
1502 /* not trying to keep connections with MFP/unhandled ciphers */
1503 if (gtkdata.unhandled_cipher)
1504 return false;
1505 if (!gtkdata.num_keys)
cfa88893 1506 goto out;
6d9d32b8
JB
1507 if (!gtkdata.last_gtk)
1508 return false;
1509
1510 /*
1511 * invalidate all other GTKs that might still exist and update
1512 * the one that we used
1513 */
1514 gtkdata.find_phase = false;
1515 ieee80211_iter_keys(mvm->hw, vif,
1516 iwl_mvm_d3_update_gtks, &gtkdata);
1517
1518 if (status->num_of_gtk_rekeys) {
1519 struct ieee80211_key_conf *key;
1520 struct {
1521 struct ieee80211_key_conf conf;
1522 u8 key[32];
1523 } conf = {
1524 .conf.cipher = gtkdata.cipher,
1525 .conf.keyidx = status->gtk.key_index,
1526 };
1527
1528 switch (gtkdata.cipher) {
1529 case WLAN_CIPHER_SUITE_CCMP:
1530 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1531 memcpy(conf.conf.key, status->gtk.decrypt_key,
1532 WLAN_KEY_LEN_CCMP);
1533 break;
1534 case WLAN_CIPHER_SUITE_TKIP:
1535 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1536 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1537 /* leave TX MIC key zeroed, we don't use it anyway */
1538 memcpy(conf.conf.key +
1539 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1540 status->gtk.tkip_mic_key, 8);
1541 break;
1542 }
1543
1544 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1545 if (IS_ERR(key))
1546 return false;
1547 iwl_mvm_set_key_rx_seq(key, status);
1548 }
1549
1550 if (status->num_of_gtk_rekeys) {
1551 __be64 replay_ctr =
1552 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1553 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1554 (void *)&replay_ctr, GFP_KERNEL);
1555 }
1556
cfa88893 1557out:
6d9d32b8
JB
1558 mvmvif->seqno_valid = true;
1559 /* +0x10 because the set API expects next-to-use, not last-used */
1560 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1561
1562 return true;
1563}
1564
ec12f457
LC
1565static struct iwl_wowlan_status *
1566iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
7abc4632
JB
1567{
1568 u32 base = mvm->error_event_table;
1569 struct error_table_start {
1570 /* cf. struct iwl_error_event_table */
1571 u32 valid;
1572 u32 error_id;
1573 } err_info;
1574 struct iwl_host_cmd cmd = {
1575 .id = WOWLAN_GET_STATUSES,
a1022927 1576 .flags = CMD_WANT_SKB,
7abc4632 1577 };
ec12f457
LC
1578 struct iwl_wowlan_status *status, *fw_status;
1579 int ret, len, status_size;
7abc4632
JB
1580
1581 iwl_trans_read_mem_bytes(mvm->trans, base,
1582 &err_info, sizeof(err_info));
1583
1584 if (err_info.valid) {
ec12f457
LC
1585 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1586 err_info.valid, err_info.error_id);
7abc4632
JB
1587 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1588 struct cfg80211_wowlan_wakeup wakeup = {
1589 .rfkill_release = true,
1590 };
1591 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1592 GFP_KERNEL);
1593 }
ec12f457 1594 return ERR_PTR(-EIO);
7abc4632
JB
1595 }
1596
1597 /* only for tracing for now */
a1022927 1598 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
7abc4632
JB
1599 if (ret)
1600 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1601
1602 ret = iwl_mvm_send_cmd(mvm, &cmd);
1603 if (ret) {
1604 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
ec12f457 1605 return ERR_PTR(ret);
7abc4632
JB
1606 }
1607
1608 /* RF-kill already asserted again... */
ec12f457 1609 if (!cmd.resp_pkt) {
e111e968 1610 fw_status = ERR_PTR(-ERFKILL);
ec12f457
LC
1611 goto out_free_resp;
1612 }
7abc4632 1613
3afec639 1614 status_size = sizeof(*fw_status);
6d9d32b8 1615
65b30348
JB
1616 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1617 if (len < status_size) {
7abc4632 1618 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
e111e968 1619 fw_status = ERR_PTR(-EIO);
ec12f457
LC
1620 goto out_free_resp;
1621 }
1622
1623 status = (void *)cmd.resp_pkt->data;
1624 if (len != (status_size +
1625 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1626 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
e111e968 1627 fw_status = ERR_PTR(-EIO);
7abc4632
JB
1628 goto out_free_resp;
1629 }
1630
ec12f457
LC
1631 fw_status = kmemdup(status, len, GFP_KERNEL);
1632
1633out_free_resp:
1634 iwl_free_resp(&cmd);
e111e968 1635 return fw_status;
ec12f457
LC
1636}
1637
1638/* releases the MVM mutex */
1639static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1640 struct ieee80211_vif *vif)
1641{
1642 struct iwl_wowlan_status_data status;
1643 struct iwl_wowlan_status *fw_status;
1644 int i;
1645 bool keep;
1646 struct ieee80211_sta *ap_sta;
1647 struct iwl_mvm_sta *mvm_ap_sta;
1648
1649 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1650 if (IS_ERR_OR_NULL(fw_status))
1651 goto out_unlock;
3afec639
EG
1652
1653 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1654 for (i = 0; i < 8; i++)
1655 status.qos_seq_ctr[i] =
1656 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1657 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1658 status.wake_packet_length =
1659 le32_to_cpu(fw_status->wake_packet_length);
1660 status.wake_packet_bufsize =
1661 le32_to_cpu(fw_status->wake_packet_bufsize);
1662 status.wake_packet = fw_status->wake_packet;
7abc4632 1663
6d9d32b8
JB
1664 /* still at hard-coded place 0 for D3 image */
1665 ap_sta = rcu_dereference_protected(
1666 mvm->fw_id_to_mac_id[0],
1667 lockdep_is_held(&mvm->mutex));
1668 if (IS_ERR_OR_NULL(ap_sta))
ec12f457 1669 goto out_free;
6d9d32b8 1670
9d8ce6af 1671 mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
6d9d32b8
JB
1672 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1673 u16 seq = status.qos_seq_ctr[i];
1674 /* firmware stores last-used value, we store next value */
1675 seq += 0x10;
1676 mvm_ap_sta->tid_data[i].seq_number = seq;
1677 }
1678
7abc4632
JB
1679 /* now we have all the data we need, unlock to avoid mac80211 issues */
1680 mutex_unlock(&mvm->mutex);
1681
6d9d32b8
JB
1682 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1683
3afec639 1684 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
6d9d32b8 1685
ec12f457 1686 kfree(fw_status);
6d9d32b8 1687 return keep;
7abc4632 1688
ec12f457
LC
1689out_free:
1690 kfree(fw_status);
1691out_unlock:
7abc4632 1692 mutex_unlock(&mvm->mutex);
6d9d32b8 1693 return false;
9b26b500
JB
1694}
1695
8ed4e659
LC
1696struct iwl_mvm_nd_query_results {
1697 u32 matched_profiles;
1698 struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1699};
1700
1701static int
1702iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1703 struct iwl_mvm_nd_query_results *results)
b04998f3
LC
1704{
1705 struct iwl_scan_offload_profiles_query *query;
1706 struct iwl_host_cmd cmd = {
1707 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1708 .flags = CMD_WANT_SKB,
1709 };
1710 int ret, len;
1711
1712 ret = iwl_mvm_send_cmd(mvm, &cmd);
1713 if (ret) {
1714 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
8ed4e659 1715 return ret;
b04998f3
LC
1716 }
1717
1718 /* RF-kill already asserted again... */
8ed4e659
LC
1719 if (!cmd.resp_pkt) {
1720 ret = -ERFKILL;
b04998f3 1721 goto out_free_resp;
8ed4e659 1722 }
b04998f3
LC
1723
1724 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1725 if (len < sizeof(*query)) {
1726 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
8ed4e659 1727 ret = -EIO;
b04998f3
LC
1728 goto out_free_resp;
1729 }
1730
1731 query = (void *)cmd.resp_pkt->data;
1732
8ed4e659
LC
1733 results->matched_profiles = le32_to_cpu(query->matched_profiles);
1734 memcpy(results->matches, query->matches, sizeof(results->matches));
b04998f3 1735
6bbd5521 1736#ifdef CONFIG_IWLWIFI_DEBUGFS
484b3d13
LC
1737 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1738#endif
1739
b04998f3
LC
1740out_free_resp:
1741 iwl_free_resp(&cmd);
1742 return ret;
1743}
1744
2021a89d
LC
1745static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1746 struct ieee80211_vif *vif)
1747{
d9718da8 1748 struct cfg80211_wowlan_nd_info *net_detect = NULL;
2021a89d
LC
1749 struct cfg80211_wowlan_wakeup wakeup = {
1750 .pattern_idx = -1,
1751 };
1752 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
8ed4e659 1753 struct iwl_mvm_nd_query_results query;
2021a89d 1754 struct iwl_wowlan_status *fw_status;
d9718da8 1755 unsigned long matched_profiles;
2021a89d 1756 u32 reasons = 0;
8ed4e659 1757 int i, j, n_matches, ret;
2021a89d
LC
1758
1759 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
2fc863a5 1760 if (!IS_ERR_OR_NULL(fw_status)) {
2021a89d 1761 reasons = le32_to_cpu(fw_status->wakeup_reasons);
2fc863a5
HD
1762 kfree(fw_status);
1763 }
2021a89d
LC
1764
1765 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1766 wakeup.rfkill_release = true;
1767
b04998f3
LC
1768 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1769 goto out;
1770
8ed4e659
LC
1771 ret = iwl_mvm_netdetect_query_results(mvm, &query);
1772 if (ret || !query.matched_profiles) {
2021a89d 1773 wakeup_report = NULL;
b04998f3 1774 goto out;
2021a89d
LC
1775 }
1776
8ed4e659 1777 matched_profiles = query.matched_profiles;
d9718da8
LC
1778 if (mvm->n_nd_match_sets) {
1779 n_matches = hweight_long(matched_profiles);
1780 } else {
1781 IWL_ERR(mvm, "no net detect match information available\n");
1782 n_matches = 0;
1783 }
1784
1785 net_detect = kzalloc(sizeof(*net_detect) +
1786 (n_matches * sizeof(net_detect->matches[0])),
1787 GFP_KERNEL);
1788 if (!net_detect || !n_matches)
1789 goto out_report_nd;
1790
1791 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
8ed4e659 1792 struct iwl_scan_offload_profile_match *fw_match;
d9718da8 1793 struct cfg80211_wowlan_nd_match *match;
ae90c2e5 1794 int idx, n_channels = 0;
8ed4e659
LC
1795
1796 fw_match = &query.matches[i];
1797
1798 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1799 n_channels += hweight8(fw_match->matching_channels[j]);
d9718da8 1800
8ed4e659
LC
1801 match = kzalloc(sizeof(*match) +
1802 (n_channels * sizeof(*match->channels)),
1803 GFP_KERNEL);
d9718da8
LC
1804 if (!match)
1805 goto out_report_nd;
1806
8ed4e659
LC
1807 net_detect->matches[net_detect->n_matches++] = match;
1808
ae90c2e5
LC
1809 /* We inverted the order of the SSIDs in the scan
1810 * request, so invert the index here.
1811 */
1812 idx = mvm->n_nd_match_sets - i - 1;
1813 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
1814 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
d9718da8
LC
1815 match->ssid.ssid_len);
1816
8ed4e659
LC
1817 if (mvm->n_nd_channels < n_channels)
1818 continue;
1819
1820 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
1821 if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
1822 match->channels[match->n_channels++] =
1823 mvm->nd_channels[j]->center_freq;
d9718da8
LC
1824 }
1825
1826out_report_nd:
1827 wakeup.net_detect = net_detect;
b04998f3 1828out:
d9718da8
LC
1829 iwl_mvm_free_nd(mvm);
1830
2021a89d
LC
1831 mutex_unlock(&mvm->mutex);
1832 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
d9718da8
LC
1833
1834 if (net_detect) {
1835 for (i = 0; i < net_detect->n_matches; i++)
1836 kfree(net_detect->matches[i]);
1837 kfree(net_detect);
1838 }
2021a89d
LC
1839}
1840
afc66bb7
JB
1841static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1842{
1843#ifdef CONFIG_IWLWIFI_DEBUGFS
1844 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1845 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1846 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1847
1848 if (!mvm->store_d3_resume_sram)
1849 return;
1850
1851 if (!mvm->d3_resume_sram) {
1852 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1853 if (!mvm->d3_resume_sram)
1854 return;
1855 }
1856
1857 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1858#endif
1859}
1860
6d9d32b8
JB
1861static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1862 struct ieee80211_vif *vif)
1863{
1864 /* skip the one we keep connection on */
1865 if (data == vif)
1866 return;
1867
1868 if (vif->type == NL80211_IFTYPE_STATION)
1869 ieee80211_resume_disconnect(vif);
1870}
1871
debff618 1872static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
8ca151b5 1873{
8ca151b5 1874 struct ieee80211_vif *vif = NULL;
8ca151b5
JB
1875 int ret;
1876 enum iwl_d3_status d3_status;
6d9d32b8 1877 bool keep = false;
8ca151b5
JB
1878
1879 mutex_lock(&mvm->mutex);
1880
1881 /* get the BSS vif pointer again */
7f549e2c
LC
1882 vif = iwl_mvm_get_bss_vif(mvm);
1883 if (IS_ERR_OR_NULL(vif))
a500e469 1884 goto err;
8ca151b5 1885
debff618 1886 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
8ca151b5 1887 if (ret)
a500e469 1888 goto err;
8ca151b5
JB
1889
1890 if (d3_status != IWL_D3_STATUS_ALIVE) {
1891 IWL_INFO(mvm, "Device was reset during suspend\n");
a500e469 1892 goto err;
8ca151b5
JB
1893 }
1894
afc66bb7
JB
1895 /* query SRAM first in case we want event logging */
1896 iwl_mvm_read_d3_sram(mvm);
1897
47c8b154
JD
1898 /*
1899 * Query the current location and source from the D3 firmware so we
1900 * can play it back when we re-intiailize the D0 firmware
1901 */
1902 iwl_mvm_update_changed_regdom(mvm);
1903
2021a89d
LC
1904 if (mvm->net_detect) {
1905 iwl_mvm_query_netdetect_reasons(mvm, vif);
e0ede177
LC
1906 /* has unlocked the mutex, so skip that */
1907 goto out;
2021a89d
LC
1908 } else {
1909 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
78c9df66 1910#ifdef CONFIG_IWLWIFI_DEBUGFS
2021a89d
LC
1911 if (keep)
1912 mvm->keep_vif = vif;
0c2ae049 1913#endif
e0ede177
LC
1914 /* has unlocked the mutex, so skip that */
1915 goto out_iterate;
2021a89d 1916 }
8ca151b5 1917
a500e469
LC
1918err:
1919 iwl_mvm_free_nd(mvm);
8ca151b5
JB
1920 mutex_unlock(&mvm->mutex);
1921
e0ede177 1922out_iterate:
6d9d32b8
JB
1923 if (!test)
1924 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1925 IEEE80211_IFACE_ITER_NORMAL,
1926 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
8ca151b5 1927
e0ede177 1928out:
8ca151b5
JB
1929 /* return 1 to reconfigure the device */
1930 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
58629d9d 1931 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
dcfc7fb1
LC
1932
1933 /* We always return 1, which causes mac80211 to do a reconfig
1934 * with IEEE80211_RECONFIG_TYPE_RESTART. This type of
1935 * reconfig calls iwl_mvm_restart_complete(), where we unref
1936 * the IWL_MVM_REF_UCODE_DOWN, so we need to take the
1937 * reference here.
1938 */
1939 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
8ca151b5
JB
1940 return 1;
1941}
1942
2d42801b 1943static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
debff618 1944{
2d42801b
EP
1945 iwl_trans_resume(mvm->trans);
1946
1947 return __iwl_mvm_resume(mvm, false);
1948}
1949
1950static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
1951{
1952 bool exit_now;
6dfb36c8
EP
1953 enum iwl_d3_status d3_status;
1954
1955 iwl_trans_d3_resume(mvm->trans, &d3_status, false);
2d42801b
EP
1956
1957 /*
1958 * make sure to clear D0I3_DEFER_WAKEUP before
1959 * calling iwl_trans_resume(), which might wait
1960 * for d0i3 exit completion.
1961 */
1962 mutex_lock(&mvm->d0i3_suspend_mutex);
1963 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1964 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1965 &mvm->d0i3_suspend_flags);
1966 mutex_unlock(&mvm->d0i3_suspend_mutex);
1967 if (exit_now) {
1968 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
1969 _iwl_mvm_exit_d0i3(mvm);
1970 }
debff618 1971
465921c8
EP
1972 iwl_trans_resume(mvm->trans);
1973
2d42801b
EP
1974 if (mvm->trans->d0i3_mode == IWL_D0I3_MODE_ON_SUSPEND) {
1975 int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
6735943f 1976
2d42801b
EP
1977 if (ret)
1978 return ret;
1979 /*
1980 * d0i3 exit will be deferred until reconfig_complete.
1981 * make sure there we are out of d0i3.
1982 */
91742449 1983 }
2d42801b
EP
1984 return 0;
1985}
d15a747f 1986
2d42801b
EP
1987int iwl_mvm_resume(struct ieee80211_hw *hw)
1988{
1989 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1990
1991 /* 'any' trigger means d0i3 was used */
1992 if (hw->wiphy->wowlan_config->any)
1993 return iwl_mvm_resume_d0i3(mvm);
1994 else
1995 return iwl_mvm_resume_d3(mvm);
debff618
JB
1996}
1997
8ca151b5
JB
1998void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1999{
2000 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2001
2002 device_set_wakeup_enable(mvm->trans->dev, enabled);
2003}
debff618
JB
2004
2005#ifdef CONFIG_IWLWIFI_DEBUGFS
2006static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2007{
2008 struct iwl_mvm *mvm = inode->i_private;
2009 int err;
2010
2011 if (mvm->d3_test_active)
2012 return -EBUSY;
2013
2014 file->private_data = inode->i_private;
2015
2016 ieee80211_stop_queues(mvm->hw);
2017 synchronize_net();
2018
2019 /* start pseudo D3 */
2020 rtnl_lock();
2021 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2022 rtnl_unlock();
2023 if (err > 0)
2024 err = -EINVAL;
2025 if (err) {
2026 ieee80211_wake_queues(mvm->hw);
2027 return err;
2028 }
2029 mvm->d3_test_active = true;
78c9df66 2030 mvm->keep_vif = NULL;
debff618
JB
2031 return 0;
2032}
2033
2034static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2035 size_t count, loff_t *ppos)
2036{
2037 struct iwl_mvm *mvm = file->private_data;
2038 u32 pme_asserted;
2039
2040 while (true) {
4ed80a86
EP
2041 /* read pme_ptr if available */
2042 if (mvm->d3_test_pme_ptr) {
2043 pme_asserted = iwl_trans_read_mem32(mvm->trans,
2044 mvm->d3_test_pme_ptr);
2045 if (pme_asserted)
2046 break;
2047 }
2048
debff618
JB
2049 if (msleep_interruptible(100))
2050 break;
2051 }
2052
2053 return 0;
2054}
2055
2056static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2057 struct ieee80211_vif *vif)
2058{
78c9df66
EP
2059 /* skip the one we keep connection on */
2060 if (_data == vif)
2061 return;
2062
debff618
JB
2063 if (vif->type == NL80211_IFTYPE_STATION)
2064 ieee80211_connection_loss(vif);
2065}
2066
2067static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2068{
2069 struct iwl_mvm *mvm = inode->i_private;
2070 int remaining_time = 10;
2071
2072 mvm->d3_test_active = false;
31ca09dd 2073 rtnl_lock();
debff618 2074 __iwl_mvm_resume(mvm, true);
31ca09dd 2075 rtnl_unlock();
debff618
JB
2076 iwl_abort_notification_waits(&mvm->notif_wait);
2077 ieee80211_restart_hw(mvm->hw);
2078
2079 /* wait for restart and disconnect all interfaces */
2080 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2081 remaining_time > 0) {
2082 remaining_time--;
2083 msleep(1000);
2084 }
2085
2086 if (remaining_time == 0)
2087 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
2088
2089 ieee80211_iterate_active_interfaces_atomic(
2090 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
78c9df66 2091 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
debff618
JB
2092
2093 ieee80211_wake_queues(mvm->hw);
2094
2095 return 0;
2096}
2097
2098const struct file_operations iwl_dbgfs_d3_test_ops = {
2099 .llseek = no_llseek,
2100 .open = iwl_mvm_d3_test_open,
2101 .read = iwl_mvm_d3_test_read,
2102 .release = iwl_mvm_d3_test_release,
2103};
2104#endif
This page took 0.317294 seconds and 5 git commands to generate.