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