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