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