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