iwlwifi: mvm: remove the vif parameter of iwl_mvm_configure_bcast_filter()
[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);
c43fe907
EP
1186 int ret;
1187
cf8c3cff
LC
1188 /* make sure the d0i3 exit work is not pending */
1189 flush_work(&mvm->d0i3_exit_work);
1190
c43fe907
EP
1191 ret = iwl_trans_suspend(mvm->trans);
1192 if (ret)
1193 return ret;
d15a747f 1194
54154618
EP
1195 mvm->trans->wowlan_d0i3 = wowlan->any;
1196 if (mvm->trans->wowlan_d0i3) {
91742449 1197 /* 'any' trigger means d0i3 usage */
6735943f 1198 if (mvm->trans->d0i3_mode == IWL_D0I3_MODE_ON_SUSPEND) {
c43fe907 1199 ret = iwl_mvm_enter_d0i3_sync(mvm);
6735943f
EP
1200
1201 if (ret)
1202 return ret;
1203 }
1204
d15a747f
EP
1205 mutex_lock(&mvm->d0i3_suspend_mutex);
1206 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1207 mutex_unlock(&mvm->d0i3_suspend_mutex);
6dfb36c8
EP
1208
1209 iwl_trans_d3_suspend(mvm->trans, false);
1210
d15a747f
EP
1211 return 0;
1212 }
1213
debff618
JB
1214 return __iwl_mvm_suspend(hw, wowlan, false);
1215}
1216
6d9d32b8
JB
1217/* converted data from the different status responses */
1218struct iwl_wowlan_status_data {
1219 u16 pattern_number;
1220 u16 qos_seq_ctr[8];
1221 u32 wakeup_reasons;
1222 u32 wake_packet_length;
1223 u32 wake_packet_bufsize;
1224 const u8 *wake_packet;
1225};
1226
7abc4632
JB
1227static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1228 struct ieee80211_vif *vif,
6d9d32b8 1229 struct iwl_wowlan_status_data *status)
9b26b500 1230{
7abc4632 1231 struct sk_buff *pkt = NULL;
9b26b500
JB
1232 struct cfg80211_wowlan_wakeup wakeup = {
1233 .pattern_idx = -1,
1234 };
1235 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
6d9d32b8 1236 u32 reasons = status->wakeup_reasons;
9b26b500
JB
1237
1238 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1239 wakeup_report = NULL;
1240 goto report;
1241 }
1242
06ae2ad4
DB
1243 pm_wakeup_event(mvm->dev, 0);
1244
f444eb10 1245 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
9b26b500 1246 wakeup.magic_pkt = true;
9b26b500 1247
f444eb10 1248 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
9b26b500 1249 wakeup.pattern_idx =
6d9d32b8 1250 status->pattern_number;
9b26b500
JB
1251
1252 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1253 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1254 wakeup.disconnect = true;
1255
f444eb10 1256 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
9b26b500 1257 wakeup.gtk_rekey_failure = true;
9b26b500 1258
f444eb10 1259 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
9b26b500 1260 wakeup.rfkill_release = true;
9b26b500 1261
f444eb10 1262 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
9b26b500 1263 wakeup.eap_identity_req = true;
9b26b500 1264
f444eb10 1265 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
9b26b500 1266 wakeup.four_way_handshake = true;
9b26b500 1267
f0c2646a
JB
1268 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1269 wakeup.tcp_connlost = true;
1270
1271 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1272 wakeup.tcp_nomoretokens = true;
1273
1274 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1275 wakeup.tcp_match = true;
1276
9b26b500 1277 if (status->wake_packet_bufsize) {
6d9d32b8
JB
1278 int pktsize = status->wake_packet_bufsize;
1279 int pktlen = status->wake_packet_length;
f444eb10
JB
1280 const u8 *pktdata = status->wake_packet;
1281 struct ieee80211_hdr *hdr = (void *)pktdata;
1282 int truncated = pktlen - pktsize;
1283
1284 /* this would be a firmware bug */
1285 if (WARN_ON_ONCE(truncated < 0))
1286 truncated = 0;
1287
1288 if (ieee80211_is_data(hdr->frame_control)) {
1289 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1290 int ivlen = 0, icvlen = 4; /* also FCS */
9b26b500 1291
9b26b500
JB
1292 pkt = alloc_skb(pktsize, GFP_KERNEL);
1293 if (!pkt)
1294 goto report;
f444eb10
JB
1295
1296 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1297 pktdata += hdrlen;
1298 pktsize -= hdrlen;
1299
1300 if (ieee80211_has_protected(hdr->frame_control)) {
7abc4632
JB
1301 /*
1302 * This is unlocked and using gtk_i(c)vlen,
1303 * but since everything is under RTNL still
1304 * that's not really a problem - changing
1305 * it would be difficult.
1306 */
f444eb10
JB
1307 if (is_multicast_ether_addr(hdr->addr1)) {
1308 ivlen = mvm->gtk_ivlen;
1309 icvlen += mvm->gtk_icvlen;
1310 } else {
1311 ivlen = mvm->ptk_ivlen;
1312 icvlen += mvm->ptk_icvlen;
1313 }
1314 }
1315
1316 /* if truncated, FCS/ICV is (partially) gone */
1317 if (truncated >= icvlen) {
1318 icvlen = 0;
1319 truncated -= icvlen;
1320 } else {
1321 icvlen -= truncated;
1322 truncated = 0;
1323 }
1324
1325 pktsize -= ivlen + icvlen;
1326 pktdata += ivlen;
1327
1328 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1329
9b26b500
JB
1330 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1331 goto report;
1332 wakeup.packet = pkt->data;
1333 wakeup.packet_present_len = pkt->len;
f444eb10 1334 wakeup.packet_len = pkt->len - truncated;
9b26b500
JB
1335 wakeup.packet_80211 = false;
1336 } else {
f444eb10
JB
1337 int fcslen = 4;
1338
1339 if (truncated >= 4) {
1340 truncated -= 4;
1341 fcslen = 0;
1342 } else {
1343 fcslen -= truncated;
1344 truncated = 0;
1345 }
1346 pktsize -= fcslen;
9b26b500
JB
1347 wakeup.packet = status->wake_packet;
1348 wakeup.packet_present_len = pktsize;
f444eb10 1349 wakeup.packet_len = pktlen - truncated;
9b26b500
JB
1350 wakeup.packet_80211 = true;
1351 }
1352 }
1353
1354 report:
1355 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1356 kfree_skb(pkt);
7abc4632 1357}
9b26b500 1358
6d9d32b8
JB
1359static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1360 struct ieee80211_key_seq *seq)
1361{
1362 u64 pn;
1363
1364 pn = le64_to_cpu(sc->pn);
1365 seq->ccmp.pn[0] = pn >> 40;
1366 seq->ccmp.pn[1] = pn >> 32;
1367 seq->ccmp.pn[2] = pn >> 24;
1368 seq->ccmp.pn[3] = pn >> 16;
1369 seq->ccmp.pn[4] = pn >> 8;
1370 seq->ccmp.pn[5] = pn;
1371}
1372
1373static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1374 struct ieee80211_key_seq *seq)
1375{
1376 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1377 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1378}
1379
1380static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1381 struct ieee80211_key_conf *key)
1382{
1383 int tid;
1384
1385 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1386
1387 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1388 struct ieee80211_key_seq seq = {};
1389
1390 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1391 ieee80211_set_key_rx_seq(key, tid, &seq);
1392 }
1393}
1394
1395static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1396 struct ieee80211_key_conf *key)
1397{
1398 int tid;
1399
1400 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1401
1402 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1403 struct ieee80211_key_seq seq = {};
1404
1405 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1406 ieee80211_set_key_rx_seq(key, tid, &seq);
1407 }
1408}
1409
1410static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
3afec639 1411 struct iwl_wowlan_status *status)
6d9d32b8
JB
1412{
1413 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1414
1415 switch (key->cipher) {
1416 case WLAN_CIPHER_SUITE_CCMP:
1417 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1418 break;
1419 case WLAN_CIPHER_SUITE_TKIP:
1420 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1421 break;
1422 default:
1423 WARN_ON(1);
1424 }
1425}
1426
1427struct iwl_mvm_d3_gtk_iter_data {
3afec639 1428 struct iwl_wowlan_status *status;
6d9d32b8
JB
1429 void *last_gtk;
1430 u32 cipher;
1431 bool find_phase, unhandled_cipher;
1432 int num_keys;
1433};
1434
59fd4bf6 1435static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
6d9d32b8
JB
1436 struct ieee80211_vif *vif,
1437 struct ieee80211_sta *sta,
1438 struct ieee80211_key_conf *key,
1439 void *_data)
1440{
1441 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1442
1443 if (data->unhandled_cipher)
1444 return;
1445
1446 switch (key->cipher) {
1447 case WLAN_CIPHER_SUITE_WEP40:
1448 case WLAN_CIPHER_SUITE_WEP104:
1449 /* ignore WEP completely, nothing to do */
1450 return;
1451 case WLAN_CIPHER_SUITE_CCMP:
1452 case WLAN_CIPHER_SUITE_TKIP:
1453 /* we support these */
1454 break;
1455 default:
1456 /* everything else (even CMAC for MFP) - disconnect from AP */
1457 data->unhandled_cipher = true;
1458 return;
1459 }
1460
1461 data->num_keys++;
1462
1463 /*
1464 * pairwise key - update sequence counters only;
1465 * note that this assumes no TDLS sessions are active
1466 */
1467 if (sta) {
1468 struct ieee80211_key_seq seq = {};
1469 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1470
1471 if (data->find_phase)
1472 return;
1473
1474 switch (key->cipher) {
1475 case WLAN_CIPHER_SUITE_CCMP:
6d9d32b8 1476 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
6645d5e4 1477 atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
6d9d32b8
JB
1478 break;
1479 case WLAN_CIPHER_SUITE_TKIP:
1480 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1481 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
6645d5e4 1482 ieee80211_set_key_tx_seq(key, &seq);
6d9d32b8
JB
1483 break;
1484 }
6d9d32b8
JB
1485
1486 /* that's it for this key */
1487 return;
1488 }
1489
1490 if (data->find_phase) {
1491 data->last_gtk = key;
1492 data->cipher = key->cipher;
1493 return;
1494 }
1495
1496 if (data->status->num_of_gtk_rekeys)
1497 ieee80211_remove_key(key);
1498 else if (data->last_gtk == key)
1499 iwl_mvm_set_key_rx_seq(key, data->status);
1500}
1501
1502static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1503 struct ieee80211_vif *vif,
3afec639 1504 struct iwl_wowlan_status *status)
6d9d32b8
JB
1505{
1506 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1507 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1508 .status = status,
1509 };
431591cc
EP
1510 u32 disconnection_reasons =
1511 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1512 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
6d9d32b8
JB
1513
1514 if (!status || !vif->bss_conf.bssid)
1515 return false;
1516
431591cc
EP
1517 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1518 return false;
1519
6d9d32b8
JB
1520 /* find last GTK that we used initially, if any */
1521 gtkdata.find_phase = true;
1522 ieee80211_iter_keys(mvm->hw, vif,
59fd4bf6 1523 iwl_mvm_d3_update_keys, &gtkdata);
6d9d32b8
JB
1524 /* not trying to keep connections with MFP/unhandled ciphers */
1525 if (gtkdata.unhandled_cipher)
1526 return false;
1527 if (!gtkdata.num_keys)
cfa88893 1528 goto out;
6d9d32b8
JB
1529 if (!gtkdata.last_gtk)
1530 return false;
1531
1532 /*
1533 * invalidate all other GTKs that might still exist and update
1534 * the one that we used
1535 */
1536 gtkdata.find_phase = false;
1537 ieee80211_iter_keys(mvm->hw, vif,
59fd4bf6 1538 iwl_mvm_d3_update_keys, &gtkdata);
6d9d32b8
JB
1539
1540 if (status->num_of_gtk_rekeys) {
1541 struct ieee80211_key_conf *key;
1542 struct {
1543 struct ieee80211_key_conf conf;
1544 u8 key[32];
1545 } conf = {
1546 .conf.cipher = gtkdata.cipher,
1547 .conf.keyidx = status->gtk.key_index,
1548 };
1549
1550 switch (gtkdata.cipher) {
1551 case WLAN_CIPHER_SUITE_CCMP:
1552 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1553 memcpy(conf.conf.key, status->gtk.decrypt_key,
1554 WLAN_KEY_LEN_CCMP);
1555 break;
1556 case WLAN_CIPHER_SUITE_TKIP:
1557 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1558 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1559 /* leave TX MIC key zeroed, we don't use it anyway */
1560 memcpy(conf.conf.key +
1561 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1562 status->gtk.tkip_mic_key, 8);
1563 break;
1564 }
1565
1566 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1567 if (IS_ERR(key))
1568 return false;
1569 iwl_mvm_set_key_rx_seq(key, status);
1570 }
1571
1572 if (status->num_of_gtk_rekeys) {
1573 __be64 replay_ctr =
1574 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1575 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1576 (void *)&replay_ctr, GFP_KERNEL);
1577 }
1578
cfa88893 1579out:
6d9d32b8
JB
1580 mvmvif->seqno_valid = true;
1581 /* +0x10 because the set API expects next-to-use, not last-used */
1582 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1583
1584 return true;
1585}
1586
ec12f457
LC
1587static struct iwl_wowlan_status *
1588iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
7abc4632
JB
1589{
1590 u32 base = mvm->error_event_table;
1591 struct error_table_start {
1592 /* cf. struct iwl_error_event_table */
1593 u32 valid;
1594 u32 error_id;
1595 } err_info;
1596 struct iwl_host_cmd cmd = {
1597 .id = WOWLAN_GET_STATUSES,
a1022927 1598 .flags = CMD_WANT_SKB,
7abc4632 1599 };
ec12f457
LC
1600 struct iwl_wowlan_status *status, *fw_status;
1601 int ret, len, status_size;
7abc4632
JB
1602
1603 iwl_trans_read_mem_bytes(mvm->trans, base,
1604 &err_info, sizeof(err_info));
1605
1606 if (err_info.valid) {
ec12f457
LC
1607 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1608 err_info.valid, err_info.error_id);
7abc4632
JB
1609 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1610 struct cfg80211_wowlan_wakeup wakeup = {
1611 .rfkill_release = true,
1612 };
1613 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1614 GFP_KERNEL);
1615 }
ec12f457 1616 return ERR_PTR(-EIO);
7abc4632
JB
1617 }
1618
1619 /* only for tracing for now */
a1022927 1620 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
7abc4632
JB
1621 if (ret)
1622 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1623
1624 ret = iwl_mvm_send_cmd(mvm, &cmd);
1625 if (ret) {
1626 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
ec12f457 1627 return ERR_PTR(ret);
7abc4632
JB
1628 }
1629
1630 /* RF-kill already asserted again... */
ec12f457 1631 if (!cmd.resp_pkt) {
e111e968 1632 fw_status = ERR_PTR(-ERFKILL);
ec12f457
LC
1633 goto out_free_resp;
1634 }
7abc4632 1635
3afec639 1636 status_size = sizeof(*fw_status);
6d9d32b8 1637
65b30348
JB
1638 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1639 if (len < status_size) {
7abc4632 1640 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
e111e968 1641 fw_status = ERR_PTR(-EIO);
ec12f457
LC
1642 goto out_free_resp;
1643 }
1644
1645 status = (void *)cmd.resp_pkt->data;
1646 if (len != (status_size +
1647 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1648 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
e111e968 1649 fw_status = ERR_PTR(-EIO);
7abc4632
JB
1650 goto out_free_resp;
1651 }
1652
ec12f457
LC
1653 fw_status = kmemdup(status, len, GFP_KERNEL);
1654
1655out_free_resp:
1656 iwl_free_resp(&cmd);
e111e968 1657 return fw_status;
ec12f457
LC
1658}
1659
1660/* releases the MVM mutex */
1661static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1662 struct ieee80211_vif *vif)
1663{
1664 struct iwl_wowlan_status_data status;
1665 struct iwl_wowlan_status *fw_status;
1666 int i;
1667 bool keep;
1668 struct ieee80211_sta *ap_sta;
1669 struct iwl_mvm_sta *mvm_ap_sta;
1670
1671 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1672 if (IS_ERR_OR_NULL(fw_status))
1673 goto out_unlock;
3afec639
EG
1674
1675 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1676 for (i = 0; i < 8; i++)
1677 status.qos_seq_ctr[i] =
1678 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1679 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1680 status.wake_packet_length =
1681 le32_to_cpu(fw_status->wake_packet_length);
1682 status.wake_packet_bufsize =
1683 le32_to_cpu(fw_status->wake_packet_bufsize);
1684 status.wake_packet = fw_status->wake_packet;
7abc4632 1685
6d9d32b8
JB
1686 /* still at hard-coded place 0 for D3 image */
1687 ap_sta = rcu_dereference_protected(
1688 mvm->fw_id_to_mac_id[0],
1689 lockdep_is_held(&mvm->mutex));
1690 if (IS_ERR_OR_NULL(ap_sta))
ec12f457 1691 goto out_free;
6d9d32b8 1692
9d8ce6af 1693 mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
6d9d32b8
JB
1694 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1695 u16 seq = status.qos_seq_ctr[i];
1696 /* firmware stores last-used value, we store next value */
1697 seq += 0x10;
1698 mvm_ap_sta->tid_data[i].seq_number = seq;
1699 }
1700
7abc4632
JB
1701 /* now we have all the data we need, unlock to avoid mac80211 issues */
1702 mutex_unlock(&mvm->mutex);
1703
6d9d32b8
JB
1704 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1705
3afec639 1706 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
6d9d32b8 1707
ec12f457 1708 kfree(fw_status);
6d9d32b8 1709 return keep;
7abc4632 1710
ec12f457
LC
1711out_free:
1712 kfree(fw_status);
1713out_unlock:
7abc4632 1714 mutex_unlock(&mvm->mutex);
6d9d32b8 1715 return false;
9b26b500
JB
1716}
1717
8ed4e659
LC
1718struct iwl_mvm_nd_query_results {
1719 u32 matched_profiles;
1720 struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1721};
1722
1723static int
1724iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1725 struct iwl_mvm_nd_query_results *results)
b04998f3
LC
1726{
1727 struct iwl_scan_offload_profiles_query *query;
1728 struct iwl_host_cmd cmd = {
1729 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1730 .flags = CMD_WANT_SKB,
1731 };
1732 int ret, len;
1733
1734 ret = iwl_mvm_send_cmd(mvm, &cmd);
1735 if (ret) {
1736 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
8ed4e659 1737 return ret;
b04998f3
LC
1738 }
1739
1740 /* RF-kill already asserted again... */
8ed4e659
LC
1741 if (!cmd.resp_pkt) {
1742 ret = -ERFKILL;
b04998f3 1743 goto out_free_resp;
8ed4e659 1744 }
b04998f3
LC
1745
1746 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1747 if (len < sizeof(*query)) {
1748 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
8ed4e659 1749 ret = -EIO;
b04998f3
LC
1750 goto out_free_resp;
1751 }
1752
1753 query = (void *)cmd.resp_pkt->data;
1754
8ed4e659
LC
1755 results->matched_profiles = le32_to_cpu(query->matched_profiles);
1756 memcpy(results->matches, query->matches, sizeof(results->matches));
b04998f3 1757
6bbd5521 1758#ifdef CONFIG_IWLWIFI_DEBUGFS
484b3d13
LC
1759 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1760#endif
1761
b04998f3
LC
1762out_free_resp:
1763 iwl_free_resp(&cmd);
1764 return ret;
1765}
1766
2021a89d
LC
1767static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1768 struct ieee80211_vif *vif)
1769{
d9718da8 1770 struct cfg80211_wowlan_nd_info *net_detect = NULL;
2021a89d
LC
1771 struct cfg80211_wowlan_wakeup wakeup = {
1772 .pattern_idx = -1,
1773 };
1774 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
8ed4e659 1775 struct iwl_mvm_nd_query_results query;
2021a89d 1776 struct iwl_wowlan_status *fw_status;
d9718da8 1777 unsigned long matched_profiles;
2021a89d 1778 u32 reasons = 0;
8ed4e659 1779 int i, j, n_matches, ret;
2021a89d
LC
1780
1781 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
2fc863a5 1782 if (!IS_ERR_OR_NULL(fw_status)) {
2021a89d 1783 reasons = le32_to_cpu(fw_status->wakeup_reasons);
2fc863a5
HD
1784 kfree(fw_status);
1785 }
2021a89d
LC
1786
1787 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1788 wakeup.rfkill_release = true;
1789
b04998f3
LC
1790 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1791 goto out;
1792
8ed4e659
LC
1793 ret = iwl_mvm_netdetect_query_results(mvm, &query);
1794 if (ret || !query.matched_profiles) {
2021a89d 1795 wakeup_report = NULL;
b04998f3 1796 goto out;
2021a89d
LC
1797 }
1798
8ed4e659 1799 matched_profiles = query.matched_profiles;
d9718da8
LC
1800 if (mvm->n_nd_match_sets) {
1801 n_matches = hweight_long(matched_profiles);
1802 } else {
1803 IWL_ERR(mvm, "no net detect match information available\n");
1804 n_matches = 0;
1805 }
1806
1807 net_detect = kzalloc(sizeof(*net_detect) +
1808 (n_matches * sizeof(net_detect->matches[0])),
1809 GFP_KERNEL);
1810 if (!net_detect || !n_matches)
1811 goto out_report_nd;
1812
1813 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
8ed4e659 1814 struct iwl_scan_offload_profile_match *fw_match;
d9718da8 1815 struct cfg80211_wowlan_nd_match *match;
ae90c2e5 1816 int idx, n_channels = 0;
8ed4e659
LC
1817
1818 fw_match = &query.matches[i];
1819
1820 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1821 n_channels += hweight8(fw_match->matching_channels[j]);
d9718da8 1822
8ed4e659
LC
1823 match = kzalloc(sizeof(*match) +
1824 (n_channels * sizeof(*match->channels)),
1825 GFP_KERNEL);
d9718da8
LC
1826 if (!match)
1827 goto out_report_nd;
1828
8ed4e659
LC
1829 net_detect->matches[net_detect->n_matches++] = match;
1830
ae90c2e5
LC
1831 /* We inverted the order of the SSIDs in the scan
1832 * request, so invert the index here.
1833 */
1834 idx = mvm->n_nd_match_sets - i - 1;
1835 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
1836 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
d9718da8
LC
1837 match->ssid.ssid_len);
1838
8ed4e659
LC
1839 if (mvm->n_nd_channels < n_channels)
1840 continue;
1841
1842 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
1843 if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
1844 match->channels[match->n_channels++] =
1845 mvm->nd_channels[j]->center_freq;
d9718da8
LC
1846 }
1847
1848out_report_nd:
1849 wakeup.net_detect = net_detect;
b04998f3 1850out:
d9718da8
LC
1851 iwl_mvm_free_nd(mvm);
1852
2021a89d
LC
1853 mutex_unlock(&mvm->mutex);
1854 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
d9718da8
LC
1855
1856 if (net_detect) {
1857 for (i = 0; i < net_detect->n_matches; i++)
1858 kfree(net_detect->matches[i]);
1859 kfree(net_detect);
1860 }
2021a89d
LC
1861}
1862
afc66bb7
JB
1863static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1864{
1865#ifdef CONFIG_IWLWIFI_DEBUGFS
1866 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1867 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1868 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1869
1870 if (!mvm->store_d3_resume_sram)
1871 return;
1872
1873 if (!mvm->d3_resume_sram) {
1874 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1875 if (!mvm->d3_resume_sram)
1876 return;
1877 }
1878
1879 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1880#endif
1881}
1882
6d9d32b8
JB
1883static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1884 struct ieee80211_vif *vif)
1885{
1886 /* skip the one we keep connection on */
1887 if (data == vif)
1888 return;
1889
1890 if (vif->type == NL80211_IFTYPE_STATION)
1891 ieee80211_resume_disconnect(vif);
1892}
1893
debff618 1894static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
8ca151b5 1895{
8ca151b5 1896 struct ieee80211_vif *vif = NULL;
8ca151b5
JB
1897 int ret;
1898 enum iwl_d3_status d3_status;
6d9d32b8 1899 bool keep = false;
8ca151b5
JB
1900
1901 mutex_lock(&mvm->mutex);
1902
1903 /* get the BSS vif pointer again */
7f549e2c
LC
1904 vif = iwl_mvm_get_bss_vif(mvm);
1905 if (IS_ERR_OR_NULL(vif))
a500e469 1906 goto err;
8ca151b5 1907
debff618 1908 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
8ca151b5 1909 if (ret)
a500e469 1910 goto err;
8ca151b5
JB
1911
1912 if (d3_status != IWL_D3_STATUS_ALIVE) {
1913 IWL_INFO(mvm, "Device was reset during suspend\n");
a500e469 1914 goto err;
8ca151b5
JB
1915 }
1916
afc66bb7
JB
1917 /* query SRAM first in case we want event logging */
1918 iwl_mvm_read_d3_sram(mvm);
1919
47c8b154
JD
1920 /*
1921 * Query the current location and source from the D3 firmware so we
1922 * can play it back when we re-intiailize the D0 firmware
1923 */
1924 iwl_mvm_update_changed_regdom(mvm);
1925
2021a89d
LC
1926 if (mvm->net_detect) {
1927 iwl_mvm_query_netdetect_reasons(mvm, vif);
e0ede177
LC
1928 /* has unlocked the mutex, so skip that */
1929 goto out;
2021a89d
LC
1930 } else {
1931 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
78c9df66 1932#ifdef CONFIG_IWLWIFI_DEBUGFS
2021a89d
LC
1933 if (keep)
1934 mvm->keep_vif = vif;
0c2ae049 1935#endif
e0ede177
LC
1936 /* has unlocked the mutex, so skip that */
1937 goto out_iterate;
2021a89d 1938 }
8ca151b5 1939
a500e469
LC
1940err:
1941 iwl_mvm_free_nd(mvm);
8ca151b5
JB
1942 mutex_unlock(&mvm->mutex);
1943
e0ede177 1944out_iterate:
6d9d32b8
JB
1945 if (!test)
1946 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1947 IEEE80211_IFACE_ITER_NORMAL,
1948 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
8ca151b5 1949
e0ede177 1950out:
8ca151b5
JB
1951 /* return 1 to reconfigure the device */
1952 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
58629d9d 1953 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
dcfc7fb1
LC
1954
1955 /* We always return 1, which causes mac80211 to do a reconfig
1956 * with IEEE80211_RECONFIG_TYPE_RESTART. This type of
1957 * reconfig calls iwl_mvm_restart_complete(), where we unref
1958 * the IWL_MVM_REF_UCODE_DOWN, so we need to take the
1959 * reference here.
1960 */
1961 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
8ca151b5
JB
1962 return 1;
1963}
1964
2d42801b 1965static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
debff618 1966{
2d42801b
EP
1967 iwl_trans_resume(mvm->trans);
1968
1969 return __iwl_mvm_resume(mvm, false);
1970}
1971
1972static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
1973{
1974 bool exit_now;
6dfb36c8
EP
1975 enum iwl_d3_status d3_status;
1976
1977 iwl_trans_d3_resume(mvm->trans, &d3_status, false);
2d42801b
EP
1978
1979 /*
1980 * make sure to clear D0I3_DEFER_WAKEUP before
1981 * calling iwl_trans_resume(), which might wait
1982 * for d0i3 exit completion.
1983 */
1984 mutex_lock(&mvm->d0i3_suspend_mutex);
1985 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1986 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1987 &mvm->d0i3_suspend_flags);
1988 mutex_unlock(&mvm->d0i3_suspend_mutex);
1989 if (exit_now) {
1990 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
1991 _iwl_mvm_exit_d0i3(mvm);
1992 }
debff618 1993
465921c8
EP
1994 iwl_trans_resume(mvm->trans);
1995
2d42801b
EP
1996 if (mvm->trans->d0i3_mode == IWL_D0I3_MODE_ON_SUSPEND) {
1997 int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
6735943f 1998
2d42801b
EP
1999 if (ret)
2000 return ret;
2001 /*
2002 * d0i3 exit will be deferred until reconfig_complete.
2003 * make sure there we are out of d0i3.
2004 */
91742449 2005 }
2d42801b
EP
2006 return 0;
2007}
d15a747f 2008
2d42801b
EP
2009int iwl_mvm_resume(struct ieee80211_hw *hw)
2010{
2011 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2012
2013 /* 'any' trigger means d0i3 was used */
2014 if (hw->wiphy->wowlan_config->any)
2015 return iwl_mvm_resume_d0i3(mvm);
2016 else
2017 return iwl_mvm_resume_d3(mvm);
debff618
JB
2018}
2019
8ca151b5
JB
2020void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2021{
2022 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2023
2024 device_set_wakeup_enable(mvm->trans->dev, enabled);
2025}
debff618
JB
2026
2027#ifdef CONFIG_IWLWIFI_DEBUGFS
2028static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2029{
2030 struct iwl_mvm *mvm = inode->i_private;
2031 int err;
2032
2033 if (mvm->d3_test_active)
2034 return -EBUSY;
2035
2036 file->private_data = inode->i_private;
2037
2038 ieee80211_stop_queues(mvm->hw);
2039 synchronize_net();
2040
2041 /* start pseudo D3 */
2042 rtnl_lock();
2043 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2044 rtnl_unlock();
2045 if (err > 0)
2046 err = -EINVAL;
2047 if (err) {
2048 ieee80211_wake_queues(mvm->hw);
2049 return err;
2050 }
2051 mvm->d3_test_active = true;
78c9df66 2052 mvm->keep_vif = NULL;
debff618
JB
2053 return 0;
2054}
2055
2056static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2057 size_t count, loff_t *ppos)
2058{
2059 struct iwl_mvm *mvm = file->private_data;
2060 u32 pme_asserted;
2061
2062 while (true) {
4ed80a86
EP
2063 /* read pme_ptr if available */
2064 if (mvm->d3_test_pme_ptr) {
2065 pme_asserted = iwl_trans_read_mem32(mvm->trans,
2066 mvm->d3_test_pme_ptr);
2067 if (pme_asserted)
2068 break;
2069 }
2070
debff618
JB
2071 if (msleep_interruptible(100))
2072 break;
2073 }
2074
2075 return 0;
2076}
2077
2078static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2079 struct ieee80211_vif *vif)
2080{
78c9df66
EP
2081 /* skip the one we keep connection on */
2082 if (_data == vif)
2083 return;
2084
debff618
JB
2085 if (vif->type == NL80211_IFTYPE_STATION)
2086 ieee80211_connection_loss(vif);
2087}
2088
2089static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2090{
2091 struct iwl_mvm *mvm = inode->i_private;
2092 int remaining_time = 10;
2093
2094 mvm->d3_test_active = false;
31ca09dd 2095 rtnl_lock();
debff618 2096 __iwl_mvm_resume(mvm, true);
31ca09dd 2097 rtnl_unlock();
debff618
JB
2098 iwl_abort_notification_waits(&mvm->notif_wait);
2099 ieee80211_restart_hw(mvm->hw);
2100
2101 /* wait for restart and disconnect all interfaces */
2102 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2103 remaining_time > 0) {
2104 remaining_time--;
2105 msleep(1000);
2106 }
2107
2108 if (remaining_time == 0)
2109 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
2110
2111 ieee80211_iterate_active_interfaces_atomic(
2112 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
78c9df66 2113 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
debff618
JB
2114
2115 ieee80211_wake_queues(mvm->hw);
2116
2117 return 0;
2118}
2119
2120const struct file_operations iwl_dbgfs_d3_test_ops = {
2121 .llseek = no_llseek,
2122 .open = iwl_mvm_d3_test_open,
2123 .read = iwl_mvm_d3_test_read,
2124 .release = iwl_mvm_d3_test_release,
2125};
2126#endif
This page took 0.322585 seconds and 5 git commands to generate.