blob: c7297c581488ccf651aaaf8c3e751f577f61c41a [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010010 *
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
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020026 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010027 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020035 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010036 * 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
Johannes Bergf444eb12013-02-26 12:04:18 +010066#include <linux/etherdevice.h>
Johannes Bergf0c26462013-01-22 20:41:58 +010067#include <linux/ip.h>
Johannes Bergdebff612013-05-14 13:53:45 +020068#include <linux/fs.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010069#include <net/cfg80211.h>
70#include <net/ipv6.h>
Johannes Bergf0c26462013-01-22 20:41:58 +010071#include <net/tcp.h>
Johannes Bergf7fc5982013-08-20 13:04:10 +020072#include <net/addrconf.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010073#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
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200107 read_lock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100108 list_for_each_entry(ifa, &idev->addr_list, if_list) {
109 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
110 idx++;
Johannes Berg5369d6c2013-07-08 11:17:06 +0200111 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100112 break;
113 }
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200114 read_unlock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100115
116 mvmvif->num_target_ipv6_addrs = idx;
117}
118#endif
119
120void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
121 struct ieee80211_vif *vif, int idx)
122{
123 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
124
125 mvmvif->tx_key_idx = idx;
126}
127
128static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
129{
130 int i;
131
132 for (i = 0; i < IWL_P1K_SIZE; i++)
133 out[i] = cpu_to_le16(p1k[i]);
134}
135
136struct wowlan_key_data {
137 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
138 struct iwl_wowlan_tkip_params_cmd *tkip;
139 bool error, use_rsc_tsc, use_tkip;
Johannes Berg5312e542013-07-22 18:26:56 +0200140 int wep_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100141};
142
143static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
144 struct ieee80211_vif *vif,
145 struct ieee80211_sta *sta,
146 struct ieee80211_key_conf *key,
147 void *_data)
148{
149 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
150 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
151 struct wowlan_key_data *data = _data;
152 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
153 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
154 struct iwl_p1k_cache *rx_p1ks;
155 u8 *rx_mic_key;
156 struct ieee80211_key_seq seq;
157 u32 cur_rx_iv32 = 0;
158 u16 p1k[IWL_P1K_SIZE];
159 int ret, i;
160
161 mutex_lock(&mvm->mutex);
162
163 switch (key->cipher) {
164 case WLAN_CIPHER_SUITE_WEP40:
165 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
166 struct {
167 struct iwl_mvm_wep_key_cmd wep_key_cmd;
168 struct iwl_mvm_wep_key wep_key;
169 } __packed wkc = {
170 .wep_key_cmd.mac_id_n_color =
171 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
172 mvmvif->color)),
173 .wep_key_cmd.num_keys = 1,
174 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
175 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
176 .wep_key.key_index = key->keyidx,
177 .wep_key.key_size = key->keylen,
178 };
179
180 /*
181 * This will fail -- the key functions don't set support
182 * pairwise WEP keys. However, that's better than silently
183 * failing WoWLAN. Or maybe not?
184 */
185 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
186 break;
187
188 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
189 if (key->keyidx == mvmvif->tx_key_idx) {
190 /* TX key must be at offset 0 */
191 wkc.wep_key.key_offset = 0;
192 } else {
193 /* others start at 1 */
Johannes Berg5312e542013-07-22 18:26:56 +0200194 data->wep_key_idx++;
195 wkc.wep_key.key_offset = data->wep_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100196 }
197
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300198 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100199 data->error = ret != 0;
200
Johannes Bergf444eb12013-02-26 12:04:18 +0100201 mvm->ptk_ivlen = key->iv_len;
202 mvm->ptk_icvlen = key->icv_len;
203 mvm->gtk_ivlen = key->iv_len;
204 mvm->gtk_icvlen = key->icv_len;
205
Johannes Berg8ca151b2013-01-24 14:25:36 +0100206 /* don't upload key again */
207 goto out_unlock;
208 }
209 default:
210 data->error = true;
211 goto out_unlock;
212 case WLAN_CIPHER_SUITE_AES_CMAC:
213 /*
214 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
215 * but we also shouldn't abort suspend due to that. It does have
216 * support for the IGTK key renewal, but doesn't really use the
217 * IGTK for anything. This means we could spuriously wake up or
218 * be deauthenticated, but that was considered acceptable.
219 */
220 goto out_unlock;
221 case WLAN_CIPHER_SUITE_TKIP:
222 if (sta) {
223 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
224 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
225
226 rx_p1ks = data->tkip->rx_uni;
227
228 ieee80211_get_key_tx_seq(key, &seq);
229 tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
230 tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
231
232 ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
233 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
234
235 memcpy(data->tkip->mic_keys.tx,
236 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
237 IWL_MIC_KEY_SIZE);
238
239 rx_mic_key = data->tkip->mic_keys.rx_unicast;
240 } else {
241 tkip_sc =
242 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
243 rx_p1ks = data->tkip->rx_multi;
244 rx_mic_key = data->tkip->mic_keys.rx_mcast;
245 }
246
247 /*
248 * For non-QoS this relies on the fact that both the uCode and
249 * mac80211 use TID 0 (as they need to to avoid replay attacks)
250 * for checking the IV in the frames.
251 */
252 for (i = 0; i < IWL_NUM_RSC; i++) {
253 ieee80211_get_key_rx_seq(key, i, &seq);
254 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
255 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
256 /* wrapping isn't allowed, AP must rekey */
257 if (seq.tkip.iv32 > cur_rx_iv32)
258 cur_rx_iv32 = seq.tkip.iv32;
259 }
260
261 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
262 cur_rx_iv32, p1k);
263 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
264 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
265 cur_rx_iv32 + 1, p1k);
266 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
267
268 memcpy(rx_mic_key,
269 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
270 IWL_MIC_KEY_SIZE);
271
272 data->use_tkip = true;
273 data->use_rsc_tsc = true;
274 break;
275 case WLAN_CIPHER_SUITE_CCMP:
276 if (sta) {
277 u8 *pn = seq.ccmp.pn;
278
279 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
280 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
281
282 ieee80211_get_key_tx_seq(key, &seq);
283 aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
284 ((u64)pn[4] << 8) |
285 ((u64)pn[3] << 16) |
286 ((u64)pn[2] << 24) |
287 ((u64)pn[1] << 32) |
288 ((u64)pn[0] << 40));
289 } else {
290 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
291 }
292
293 /*
294 * For non-QoS this relies on the fact that both the uCode and
295 * mac80211 use TID 0 for checking the IV in the frames.
296 */
297 for (i = 0; i < IWL_NUM_RSC; i++) {
298 u8 *pn = seq.ccmp.pn;
299
300 ieee80211_get_key_rx_seq(key, i, &seq);
301 aes_sc->pn = cpu_to_le64((u64)pn[5] |
302 ((u64)pn[4] << 8) |
303 ((u64)pn[3] << 16) |
304 ((u64)pn[2] << 24) |
305 ((u64)pn[1] << 32) |
306 ((u64)pn[0] << 40));
307 }
308 data->use_rsc_tsc = true;
309 break;
310 }
311
312 /*
313 * The D3 firmware hardcodes the key offset 0 as the key it uses
314 * to transmit packets to the AP, i.e. the PTK.
315 */
316 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
317 key->hw_key_idx = 0;
Johannes Bergf444eb12013-02-26 12:04:18 +0100318 mvm->ptk_ivlen = key->iv_len;
319 mvm->ptk_icvlen = key->icv_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100320 } else {
Johannes Berg5312e542013-07-22 18:26:56 +0200321 /*
322 * firmware only supports TSC/RSC for a single key,
323 * so if there are multiple keep overwriting them
324 * with new ones -- this relies on mac80211 doing
325 * list_add_tail().
326 */
327 key->hw_key_idx = 1;
Johannes Bergf444eb12013-02-26 12:04:18 +0100328 mvm->gtk_ivlen = key->iv_len;
329 mvm->gtk_icvlen = key->icv_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100330 }
331
332 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
333 data->error = ret != 0;
334out_unlock:
335 mutex_unlock(&mvm->mutex);
336}
337
338static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
339 struct cfg80211_wowlan *wowlan)
340{
341 struct iwl_wowlan_patterns_cmd *pattern_cmd;
342 struct iwl_host_cmd cmd = {
343 .id = WOWLAN_PATTERNS,
344 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100345 };
346 int i, err;
347
348 if (!wowlan->n_patterns)
349 return 0;
350
351 cmd.len[0] = sizeof(*pattern_cmd) +
352 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
353
354 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
355 if (!pattern_cmd)
356 return -ENOMEM;
357
358 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
359
360 for (i = 0; i < wowlan->n_patterns; i++) {
361 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
362
363 memcpy(&pattern_cmd->patterns[i].mask,
364 wowlan->patterns[i].mask, mask_len);
365 memcpy(&pattern_cmd->patterns[i].pattern,
366 wowlan->patterns[i].pattern,
367 wowlan->patterns[i].pattern_len);
368 pattern_cmd->patterns[i].mask_size = mask_len;
369 pattern_cmd->patterns[i].pattern_size =
370 wowlan->patterns[i].pattern_len;
371 }
372
373 cmd.data[0] = pattern_cmd;
374 err = iwl_mvm_send_cmd(mvm, &cmd);
375 kfree(pattern_cmd);
376 return err;
377}
378
Johannes Bergf0c26462013-01-22 20:41:58 +0100379enum iwl_mvm_tcp_packet_type {
380 MVM_TCP_TX_SYN,
381 MVM_TCP_RX_SYNACK,
382 MVM_TCP_TX_DATA,
383 MVM_TCP_RX_ACK,
384 MVM_TCP_RX_WAKE,
385 MVM_TCP_TX_FIN,
386};
387
388static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
389{
390 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
391 return cpu_to_le16(be16_to_cpu((__force __be16)check));
392}
393
Oren Givonb002c7e2013-06-12 20:37:03 +0300394static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
Johannes Bergf0c26462013-01-22 20:41:58 +0100395 struct cfg80211_wowlan_tcp *tcp,
396 void *_pkt, u8 *mask,
397 __le16 *pseudo_hdr_csum,
398 enum iwl_mvm_tcp_packet_type ptype)
399{
400 struct {
401 struct ethhdr eth;
402 struct iphdr ip;
403 struct tcphdr tcp;
404 u8 data[];
405 } __packed *pkt = _pkt;
406 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
407 int i;
408
409 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
410 pkt->ip.version = 4;
411 pkt->ip.ihl = 5;
412 pkt->ip.protocol = IPPROTO_TCP;
413
414 switch (ptype) {
415 case MVM_TCP_TX_SYN:
416 case MVM_TCP_TX_DATA:
417 case MVM_TCP_TX_FIN:
418 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
419 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
420 pkt->ip.ttl = 128;
421 pkt->ip.saddr = tcp->src;
422 pkt->ip.daddr = tcp->dst;
423 pkt->tcp.source = cpu_to_be16(tcp->src_port);
424 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
425 /* overwritten for TX SYN later */
426 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
427 pkt->tcp.window = cpu_to_be16(65000);
428 break;
429 case MVM_TCP_RX_SYNACK:
430 case MVM_TCP_RX_ACK:
431 case MVM_TCP_RX_WAKE:
432 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
433 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
434 pkt->ip.saddr = tcp->dst;
435 pkt->ip.daddr = tcp->src;
436 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
437 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
438 break;
439 default:
440 WARN_ON(1);
441 return;
442 }
443
444 switch (ptype) {
445 case MVM_TCP_TX_SYN:
446 /* firmware assumes 8 option bytes - 8 NOPs for now */
447 memset(pkt->data, 0x01, 8);
448 ip_tot_len += 8;
449 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
450 pkt->tcp.syn = 1;
451 break;
452 case MVM_TCP_TX_DATA:
453 ip_tot_len += tcp->payload_len;
454 memcpy(pkt->data, tcp->payload, tcp->payload_len);
455 pkt->tcp.psh = 1;
456 pkt->tcp.ack = 1;
457 break;
458 case MVM_TCP_TX_FIN:
459 pkt->tcp.fin = 1;
460 pkt->tcp.ack = 1;
461 break;
462 case MVM_TCP_RX_SYNACK:
463 pkt->tcp.syn = 1;
464 pkt->tcp.ack = 1;
465 break;
466 case MVM_TCP_RX_ACK:
467 pkt->tcp.ack = 1;
468 break;
469 case MVM_TCP_RX_WAKE:
470 ip_tot_len += tcp->wake_len;
471 pkt->tcp.psh = 1;
472 pkt->tcp.ack = 1;
473 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
474 break;
475 }
476
477 switch (ptype) {
478 case MVM_TCP_TX_SYN:
479 case MVM_TCP_TX_DATA:
480 case MVM_TCP_TX_FIN:
481 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
482 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
483 break;
484 case MVM_TCP_RX_WAKE:
485 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
486 u8 tmp = tcp->wake_mask[i];
487 mask[i + 6] |= tmp << 6;
488 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
489 mask[i + 7] = tmp >> 2;
490 }
491 /* fall through for ethernet/IP/TCP headers mask */
492 case MVM_TCP_RX_SYNACK:
493 case MVM_TCP_RX_ACK:
494 mask[0] = 0xff; /* match ethernet */
495 /*
496 * match ethernet, ip.version, ip.ihl
497 * the ip.ihl half byte is really masked out by firmware
498 */
499 mask[1] = 0x7f;
500 mask[2] = 0x80; /* match ip.protocol */
501 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
502 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
503 mask[5] = 0x80; /* match tcp flags */
504 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
505 break;
506 };
507
508 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
509 pkt->ip.saddr, pkt->ip.daddr);
510}
511
512static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
513 struct ieee80211_vif *vif,
514 struct cfg80211_wowlan_tcp *tcp)
515{
516 struct iwl_wowlan_remote_wake_config *cfg;
517 struct iwl_host_cmd cmd = {
518 .id = REMOTE_WAKE_CONFIG_CMD,
519 .len = { sizeof(*cfg), },
520 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
Johannes Bergf0c26462013-01-22 20:41:58 +0100521 };
522 int ret;
523
524 if (!tcp)
525 return 0;
526
527 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
528 if (!cfg)
529 return -ENOMEM;
530 cmd.data[0] = cfg;
531
532 cfg->max_syn_retries = 10;
533 cfg->max_data_retries = 10;
534 cfg->tcp_syn_ack_timeout = 1; /* seconds */
535 cfg->tcp_ack_timeout = 1; /* seconds */
536
537 /* SYN (TX) */
538 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300539 vif, tcp, cfg->syn_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100540 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
541 MVM_TCP_TX_SYN);
542 cfg->syn_tx.info.tcp_payload_length = 0;
543
544 /* SYN/ACK (RX) */
545 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300546 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
Johannes Bergf0c26462013-01-22 20:41:58 +0100547 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
548 MVM_TCP_RX_SYNACK);
549 cfg->synack_rx.info.tcp_payload_length = 0;
550
551 /* KEEPALIVE/ACK (TX) */
552 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300553 vif, tcp, cfg->keepalive_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100554 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
555 MVM_TCP_TX_DATA);
556 cfg->keepalive_tx.info.tcp_payload_length =
557 cpu_to_le16(tcp->payload_len);
558 cfg->sequence_number_offset = tcp->payload_seq.offset;
559 /* length must be 0..4, the field is little endian */
560 cfg->sequence_number_length = tcp->payload_seq.len;
561 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
562 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
563 if (tcp->payload_tok.len) {
564 cfg->token_offset = tcp->payload_tok.offset;
565 cfg->token_length = tcp->payload_tok.len;
566 cfg->num_tokens =
567 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
568 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
569 tcp->tokens_size);
570 } else {
571 /* set tokens to max value to almost never run out */
572 cfg->num_tokens = cpu_to_le16(65535);
573 }
574
575 /* ACK (RX) */
576 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300577 vif, tcp, cfg->keepalive_ack_rx.data,
Johannes Bergf0c26462013-01-22 20:41:58 +0100578 cfg->keepalive_ack_rx.rx_mask,
579 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
580 MVM_TCP_RX_ACK);
581 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
582
583 /* WAKEUP (RX) */
584 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300585 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
Johannes Bergf0c26462013-01-22 20:41:58 +0100586 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
587 MVM_TCP_RX_WAKE);
588 cfg->wake_rx.info.tcp_payload_length =
589 cpu_to_le16(tcp->wake_len);
590
591 /* FIN */
592 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300593 vif, tcp, cfg->fin_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100594 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
595 MVM_TCP_TX_FIN);
596 cfg->fin_tx.info.tcp_payload_length = 0;
597
598 ret = iwl_mvm_send_cmd(mvm, &cmd);
599 kfree(cfg);
600
601 return ret;
602}
603
Johannes Berg8ca151b2013-01-24 14:25:36 +0100604static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
605 struct ieee80211_sta *ap_sta)
606{
607 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
608 struct ieee80211_chanctx_conf *ctx;
609 u8 chains_static, chains_dynamic;
610 struct cfg80211_chan_def chandef;
611 int ret, i;
612 struct iwl_binding_cmd binding_cmd = {};
613 struct iwl_time_quota_cmd quota_cmd = {};
614 u32 status;
615
616 /* add back the PHY */
617 if (WARN_ON(!mvmvif->phy_ctxt))
618 return -EINVAL;
619
620 rcu_read_lock();
621 ctx = rcu_dereference(vif->chanctx_conf);
622 if (WARN_ON(!ctx)) {
623 rcu_read_unlock();
624 return -EINVAL;
625 }
626 chandef = ctx->def;
627 chains_static = ctx->rx_chains_static;
628 chains_dynamic = ctx->rx_chains_dynamic;
629 rcu_read_unlock();
630
631 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
632 chains_static, chains_dynamic);
633 if (ret)
634 return ret;
635
636 /* add back the MAC */
637 mvmvif->uploaded = false;
638
639 if (WARN_ON(!vif->bss_conf.assoc))
640 return -EINVAL;
Luciano Coelhobca49d92014-05-13 17:33:38 +0300641
Johannes Berg8ca151b2013-01-24 14:25:36 +0100642 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100643 if (ret)
644 return ret;
645
646 /* add back binding - XXX refactor? */
647 binding_cmd.id_and_color =
648 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
649 mvmvif->phy_ctxt->color));
650 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
651 binding_cmd.phy =
652 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
653 mvmvif->phy_ctxt->color));
654 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
655 mvmvif->color));
656 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
657 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
658
659 status = 0;
660 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
661 sizeof(binding_cmd), &binding_cmd,
662 &status);
663 if (ret) {
664 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
665 return ret;
666 }
667
668 if (status) {
669 IWL_ERR(mvm, "Binding command failed: %u\n", status);
670 return -EIO;
671 }
672
Johannes Berg7a453972013-02-12 13:10:44 +0100673 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100674 if (ret)
675 return ret;
676 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
677
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200678 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100679 if (ret)
680 return ret;
681
682 /* and some quota */
683 quota_cmd.quotas[0].id_and_color =
684 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
685 mvmvif->phy_ctxt->color));
Emmanuel Grumbach9a3daf82014-03-05 10:00:44 +0200686 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
687 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100688
689 for (i = 1; i < MAX_BINDINGS; i++)
690 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
691
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300692 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100693 sizeof(quota_cmd), &quota_cmd);
694 if (ret)
695 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
696
697 return 0;
698}
699
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200700static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
701 struct ieee80211_vif *vif)
702{
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200703 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
704 struct iwl_nonqos_seq_query_cmd query_cmd = {
705 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
706 .mac_id_n_color =
707 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
708 mvmvif->color)),
709 };
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200710 struct iwl_host_cmd cmd = {
711 .id = NON_QOS_TX_COUNTER_CMD,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300712 .flags = CMD_WANT_SKB,
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200713 };
714 int err;
715 u32 size;
716
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300717 cmd.data[0] = &query_cmd;
718 cmd.len[0] = sizeof(query_cmd);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200719
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200720 err = iwl_mvm_send_cmd(mvm, &cmd);
721 if (err)
722 return err;
723
Johannes Berg65b30342014-01-08 13:16:33 +0100724 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200725 if (size < sizeof(__le16)) {
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200726 err = -EINVAL;
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200727 } else {
728 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300729 /* firmware returns next, not last-used seqno */
730 err = (u16) (err - 0x10);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200731 }
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200732
733 iwl_free_resp(&cmd);
734 return err;
735}
736
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200737void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
738{
739 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
740 struct iwl_nonqos_seq_query_cmd query_cmd = {
741 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
742 .mac_id_n_color =
743 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
744 mvmvif->color)),
745 .value = cpu_to_le16(mvmvif->seqno),
746 };
747
748 /* return if called during restart, not resume from D3 */
749 if (!mvmvif->seqno_valid)
750 return;
751
752 mvmvif->seqno_valid = false;
753
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300754 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200755 sizeof(query_cmd), &query_cmd))
756 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
757}
758
Luciano Coelho671b5822014-09-22 10:59:53 +0300759static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200760{
Johannes Berg8ca151b2013-01-24 14:25:36 +0100761 iwl_mvm_cancel_scan(mvm);
762
763 iwl_trans_stop_device(mvm->trans);
764
765 /*
766 * Set the HW restart bit -- this is mostly true as we're
767 * going to load new firmware and reprogram that, though
768 * the reprogramming is going to be manual to avoid adding
769 * all the MACs that aren't support.
770 * We don't have to clear up everything though because the
771 * reprogramming is manual. When we resume, we'll actually
772 * go through a proper restart sequence again to switch
773 * back to the runtime firmware image.
774 */
775 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
776
777 /* We reprogram keys and shouldn't allocate new key indices */
778 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
779
Johannes Bergf444eb12013-02-26 12:04:18 +0100780 mvm->ptk_ivlen = 0;
781 mvm->ptk_icvlen = 0;
782 mvm->ptk_ivlen = 0;
783 mvm->ptk_icvlen = 0;
784
Luciano Coelho671b5822014-09-22 10:59:53 +0300785 return iwl_mvm_load_d3_fw(mvm);
786}
787
788static int
Luciano Coelho671b5822014-09-22 10:59:53 +0300789iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
790 struct cfg80211_wowlan *wowlan,
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200791 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
Luciano Coelho671b5822014-09-22 10:59:53 +0300792 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
793 struct ieee80211_sta *ap_sta)
794{
795 int ret;
796 struct iwl_mvm_sta *mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
797
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200798 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
Luciano Coelho671b5822014-09-22 10:59:53 +0300799
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200800 wowlan_config_cmd->is_11n_connection =
Luciano Coelho671b5822014-09-22 10:59:53 +0300801 ap_sta->ht_cap.ht_supported;
802
803 /* Query the last used seqno and set it */
804 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
805 if (ret < 0)
806 return ret;
807
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200808 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
Luciano Coelho671b5822014-09-22 10:59:53 +0300809
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200810 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
Luciano Coelho671b5822014-09-22 10:59:53 +0300811
812 if (wowlan->disconnect)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200813 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300814 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
815 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
816 if (wowlan->magic_pkt)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200817 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300818 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
819 if (wowlan->gtk_rekey_failure)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200820 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300821 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
822 if (wowlan->eap_identity_req)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200823 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300824 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
825 if (wowlan->four_way_handshake)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200826 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300827 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
828 if (wowlan->n_patterns)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200829 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300830 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
831
832 if (wowlan->rfkill_release)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200833 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300834 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
835
836 if (wowlan->tcp) {
837 /*
838 * Set the "link change" (really "link lost") flag as well
839 * since that implies losing the TCP connection.
840 */
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200841 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300842 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
843 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
844 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
845 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
846 }
847
848 return 0;
849}
850
851static int
852iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
853 struct cfg80211_wowlan *wowlan,
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200854 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
Luciano Coelho671b5822014-09-22 10:59:53 +0300855 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
856 struct ieee80211_sta *ap_sta)
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 = {
861 .use_rsc_tsc = false,
862 .tkip = &tkip_cmd,
863 .use_tkip = false,
864 };
865 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100866
Luciano Coelho3acc9522014-11-19 22:35:37 +0200867 ret = iwl_mvm_switch_to_d3(mvm);
868 if (ret)
869 return ret;
870
Johannes Berg8ca151b2013-01-24 14:25:36 +0100871 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
872 if (ret)
Luciano Coelho671b5822014-09-22 10:59:53 +0300873 return ret;
874
875 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
876 if (!key_data.rsc_tsc)
877 return -ENOMEM;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100878
879 if (!iwlwifi_mod_params.sw_crypto) {
880 /*
881 * This needs to be unlocked due to lock ordering
882 * constraints. Since we're in the suspend path
883 * that isn't really a problem though.
884 */
885 mutex_unlock(&mvm->mutex);
886 ieee80211_iter_keys(mvm->hw, vif,
887 iwl_mvm_wowlan_program_keys,
888 &key_data);
889 mutex_lock(&mvm->mutex);
890 if (key_data.error) {
891 ret = -EIO;
892 goto out;
893 }
894
895 if (key_data.use_rsc_tsc) {
896 struct iwl_host_cmd rsc_tsc_cmd = {
897 .id = WOWLAN_TSC_RSC_PARAM,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100898 .data[0] = key_data.rsc_tsc,
899 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
900 .len[0] = sizeof(*key_data.rsc_tsc),
901 };
902
903 ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
904 if (ret)
905 goto out;
906 }
907
908 if (key_data.use_tkip) {
909 ret = iwl_mvm_send_cmd_pdu(mvm,
910 WOWLAN_TKIP_PARAM,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300911 0, sizeof(tkip_cmd),
Johannes Berg8ca151b2013-01-24 14:25:36 +0100912 &tkip_cmd);
913 if (ret)
914 goto out;
915 }
916
917 if (mvmvif->rekey_data.valid) {
918 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
919 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
920 NL80211_KCK_LEN);
921 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
922 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
923 NL80211_KEK_LEN);
924 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
925 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
926
927 ret = iwl_mvm_send_cmd_pdu(mvm,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300928 WOWLAN_KEK_KCK_MATERIAL, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100929 sizeof(kek_kck_cmd),
930 &kek_kck_cmd);
931 if (ret)
932 goto out;
933 }
934 }
935
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200936 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
937 sizeof(*wowlan_config_cmd),
938 wowlan_config_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100939 if (ret)
940 goto out;
941
942 ret = iwl_mvm_send_patterns(mvm, wowlan);
943 if (ret)
944 goto out;
945
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300946 ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100947 if (ret)
948 goto out;
949
Johannes Bergf0c26462013-01-22 20:41:58 +0100950 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
Luciano Coelho671b5822014-09-22 10:59:53 +0300951
952out:
953 kfree(key_data.rsc_tsc);
954 return ret;
955}
956
Luciano Coelho3acc9522014-11-19 22:35:37 +0200957static int
958iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
959 struct cfg80211_wowlan *wowlan,
960 struct cfg80211_sched_scan_request *nd_config,
961 struct ieee80211_vif *vif)
962{
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200963 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
Luciano Coelho3acc9522014-11-19 22:35:37 +0200964 int ret;
965
966 ret = iwl_mvm_switch_to_d3(mvm);
967 if (ret)
968 return ret;
969
970 /* rfkill release can be either for wowlan or netdetect */
971 if (wowlan->rfkill_release)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200972 wowlan_config_cmd.wakeup_filter |=
Luciano Coelho3acc9522014-11-19 22:35:37 +0200973 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
974
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200975 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
976 sizeof(wowlan_config_cmd),
977 &wowlan_config_cmd);
Luciano Coelho3acc9522014-11-19 22:35:37 +0200978 if (ret)
979 return ret;
980
981 ret = iwl_mvm_scan_offload_start(mvm, vif, nd_config, &mvm->nd_ies);
982
983 return ret;
984}
985
Luciano Coelho671b5822014-09-22 10:59:53 +0300986static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
987 struct cfg80211_wowlan *wowlan,
988 bool test)
989{
990 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Luciano Coelho671b5822014-09-22 10:59:53 +0300991 struct ieee80211_vif *vif = NULL;
992 struct iwl_mvm_vif *mvmvif = NULL;
993 struct ieee80211_sta *ap_sta = NULL;
Luciano Coelho671b5822014-09-22 10:59:53 +0300994 struct iwl_d3_manager_config d3_cfg_cmd_data = {
995 /*
996 * Program the minimum sleep time to 10 seconds, as many
997 * platforms have issues processing a wakeup signal while
998 * still being in the process of suspending.
999 */
1000 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1001 };
1002 struct iwl_host_cmd d3_cfg_cmd = {
1003 .id = D3_CONFIG_CMD,
1004 .flags = CMD_WANT_SKB,
1005 .data[0] = &d3_cfg_cmd_data,
1006 .len[0] = sizeof(d3_cfg_cmd_data),
1007 };
1008 int ret;
1009 int len __maybe_unused;
1010
1011 if (!wowlan) {
1012 /*
1013 * mac80211 shouldn't get here, but for D3 test
1014 * it doesn't warrant a warning
1015 */
1016 WARN_ON(!test);
1017 return -EINVAL;
1018 }
1019
1020 mutex_lock(&mvm->mutex);
1021
Luciano Coelho7f549e22014-10-02 15:38:04 +03001022 vif = iwl_mvm_get_bss_vif(mvm);
1023 if (IS_ERR_OR_NULL(vif)) {
Luciano Coelho671b5822014-09-22 10:59:53 +03001024 ret = 1;
1025 goto out_noreset;
1026 }
1027
Luciano Coelho671b5822014-09-22 10:59:53 +03001028 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1029
Luciano Coelho3acc9522014-11-19 22:35:37 +02001030 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
1031 /* if we're not associated, this must be netdetect */
1032 if (!wowlan->nd_config && !mvm->nd_config) {
1033 ret = 1;
1034 goto out_noreset;
1035 }
1036
1037 ret = iwl_mvm_netdetect_config(
1038 mvm, wowlan, wowlan->nd_config ?: mvm->nd_config, vif);
1039 if (ret)
1040 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001041
1042 mvm->net_detect = true;
Luciano Coelho3acc9522014-11-19 22:35:37 +02001043 } else {
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +02001044 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
Luciano Coelho3acc9522014-11-19 22:35:37 +02001045
Luciano Coelho1e4d19c2014-09-22 13:01:59 +03001046 ap_sta = rcu_dereference_protected(
1047 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1048 lockdep_is_held(&mvm->mutex));
1049 if (IS_ERR_OR_NULL(ap_sta)) {
1050 ret = -EINVAL;
1051 goto out_noreset;
1052 }
1053
1054 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1055 vif, mvmvif, ap_sta);
1056 if (ret)
1057 goto out_noreset;
Luciano Coelho1e4d19c2014-09-22 13:01:59 +03001058 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1059 vif, mvmvif, ap_sta);
1060 if (ret)
1061 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001062
1063 mvm->net_detect = false;
Luciano Coelho671b5822014-09-22 10:59:53 +03001064 }
1065
Emmanuel Grumbachc1cb92f2014-01-28 10:17:18 +02001066 ret = iwl_mvm_power_update_device(mvm);
Alexander Bondar64b928c2013-09-03 14:18:03 +03001067 if (ret)
1068 goto out;
1069
Arik Nemtsov999609f2014-05-15 17:31:51 +03001070 ret = iwl_mvm_power_update_mac(mvm);
Alexander Bondaree1e8422013-04-23 13:52:10 +03001071 if (ret)
1072 goto out;
1073
Johannes Bergb0114712013-06-12 14:55:40 +02001074#ifdef CONFIG_IWLWIFI_DEBUGFS
1075 if (mvm->d3_wake_sysassert)
1076 d3_cfg_cmd_data.wakeup_flags |=
1077 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1078#endif
1079
Johannes Berg8ca151b2013-01-24 14:25:36 +01001080 /* must be last -- this switches firmware state */
Johannes Bergdebff612013-05-14 13:53:45 +02001081 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001082 if (ret)
1083 goto out;
Johannes Bergdebff612013-05-14 13:53:45 +02001084#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg65b30342014-01-08 13:16:33 +01001085 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1086 if (len >= sizeof(u32)) {
Johannes Bergdebff612013-05-14 13:53:45 +02001087 mvm->d3_test_pme_ptr =
1088 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
Johannes Bergdebff612013-05-14 13:53:45 +02001089 }
1090#endif
1091 iwl_free_resp(&d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001092
1093 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1094
Johannes Bergdebff612013-05-14 13:53:45 +02001095 iwl_trans_d3_suspend(mvm->trans, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001096 out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001097 if (ret < 0)
1098 ieee80211_restart_hw(mvm->hw);
Luciano Coelhob70ab722013-11-19 22:53:41 +02001099 out_noreset:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001100 mutex_unlock(&mvm->mutex);
1101
1102 return ret;
1103}
1104
Johannes Bergdebff612013-05-14 13:53:45 +02001105int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1106{
Eliad Pellerd15a7472014-03-27 18:53:12 +02001107 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1108
Eliad Peller465921c2014-09-30 10:02:13 +03001109 iwl_trans_suspend(mvm->trans);
Eliad Pellerd15a7472014-03-27 18:53:12 +02001110 if (iwl_mvm_is_d0i3_supported(mvm)) {
1111 mutex_lock(&mvm->d0i3_suspend_mutex);
1112 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1113 mutex_unlock(&mvm->d0i3_suspend_mutex);
1114 return 0;
1115 }
1116
Johannes Bergdebff612013-05-14 13:53:45 +02001117 return __iwl_mvm_suspend(hw, wowlan, false);
1118}
1119
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001120/* converted data from the different status responses */
1121struct iwl_wowlan_status_data {
1122 u16 pattern_number;
1123 u16 qos_seq_ctr[8];
1124 u32 wakeup_reasons;
1125 u32 wake_packet_length;
1126 u32 wake_packet_bufsize;
1127 const u8 *wake_packet;
1128};
1129
Johannes Berg7abc4632013-08-06 18:28:26 +02001130static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1131 struct ieee80211_vif *vif,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001132 struct iwl_wowlan_status_data *status)
Johannes Berg9b26b502013-01-22 13:02:09 +01001133{
Johannes Berg7abc4632013-08-06 18:28:26 +02001134 struct sk_buff *pkt = NULL;
Johannes Berg9b26b502013-01-22 13:02:09 +01001135 struct cfg80211_wowlan_wakeup wakeup = {
1136 .pattern_idx = -1,
1137 };
1138 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001139 u32 reasons = status->wakeup_reasons;
Johannes Berg9b26b502013-01-22 13:02:09 +01001140
1141 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1142 wakeup_report = NULL;
1143 goto report;
1144 }
1145
Johannes Bergf444eb12013-02-26 12:04:18 +01001146 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
Johannes Berg9b26b502013-01-22 13:02:09 +01001147 wakeup.magic_pkt = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001148
Johannes Bergf444eb12013-02-26 12:04:18 +01001149 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
Johannes Berg9b26b502013-01-22 13:02:09 +01001150 wakeup.pattern_idx =
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001151 status->pattern_number;
Johannes Berg9b26b502013-01-22 13:02:09 +01001152
1153 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1154 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1155 wakeup.disconnect = true;
1156
Johannes Bergf444eb12013-02-26 12:04:18 +01001157 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001158 wakeup.gtk_rekey_failure = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001159
Johannes Bergf444eb12013-02-26 12:04:18 +01001160 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
Johannes Berg9b26b502013-01-22 13:02:09 +01001161 wakeup.rfkill_release = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001162
Johannes Bergf444eb12013-02-26 12:04:18 +01001163 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
Johannes Berg9b26b502013-01-22 13:02:09 +01001164 wakeup.eap_identity_req = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001165
Johannes Bergf444eb12013-02-26 12:04:18 +01001166 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001167 wakeup.four_way_handshake = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001168
Johannes Bergf0c26462013-01-22 20:41:58 +01001169 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1170 wakeup.tcp_connlost = true;
1171
1172 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1173 wakeup.tcp_nomoretokens = true;
1174
1175 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1176 wakeup.tcp_match = true;
1177
Johannes Berg9b26b502013-01-22 13:02:09 +01001178 if (status->wake_packet_bufsize) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001179 int pktsize = status->wake_packet_bufsize;
1180 int pktlen = status->wake_packet_length;
Johannes Bergf444eb12013-02-26 12:04:18 +01001181 const u8 *pktdata = status->wake_packet;
1182 struct ieee80211_hdr *hdr = (void *)pktdata;
1183 int truncated = pktlen - pktsize;
Johannes Berg9b26b502013-01-22 13:02:09 +01001184
Johannes Bergf444eb12013-02-26 12:04:18 +01001185 /* this would be a firmware bug */
1186 if (WARN_ON_ONCE(truncated < 0))
1187 truncated = 0;
1188
1189 if (ieee80211_is_data(hdr->frame_control)) {
1190 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1191 int ivlen = 0, icvlen = 4; /* also FCS */
1192
Johannes Berg9b26b502013-01-22 13:02:09 +01001193 pkt = alloc_skb(pktsize, GFP_KERNEL);
1194 if (!pkt)
1195 goto report;
Johannes Bergf444eb12013-02-26 12:04:18 +01001196
1197 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1198 pktdata += hdrlen;
1199 pktsize -= hdrlen;
1200
1201 if (ieee80211_has_protected(hdr->frame_control)) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001202 /*
1203 * This is unlocked and using gtk_i(c)vlen,
1204 * but since everything is under RTNL still
1205 * that's not really a problem - changing
1206 * it would be difficult.
1207 */
Johannes Bergf444eb12013-02-26 12:04:18 +01001208 if (is_multicast_ether_addr(hdr->addr1)) {
1209 ivlen = mvm->gtk_ivlen;
1210 icvlen += mvm->gtk_icvlen;
1211 } else {
1212 ivlen = mvm->ptk_ivlen;
1213 icvlen += mvm->ptk_icvlen;
1214 }
1215 }
1216
1217 /* if truncated, FCS/ICV is (partially) gone */
1218 if (truncated >= icvlen) {
1219 icvlen = 0;
1220 truncated -= icvlen;
1221 } else {
1222 icvlen -= truncated;
1223 truncated = 0;
1224 }
1225
1226 pktsize -= ivlen + icvlen;
1227 pktdata += ivlen;
1228
1229 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1230
Johannes Berg9b26b502013-01-22 13:02:09 +01001231 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1232 goto report;
1233 wakeup.packet = pkt->data;
1234 wakeup.packet_present_len = pkt->len;
Johannes Bergf444eb12013-02-26 12:04:18 +01001235 wakeup.packet_len = pkt->len - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001236 wakeup.packet_80211 = false;
1237 } else {
Johannes Bergf444eb12013-02-26 12:04:18 +01001238 int fcslen = 4;
1239
1240 if (truncated >= 4) {
1241 truncated -= 4;
1242 fcslen = 0;
1243 } else {
1244 fcslen -= truncated;
1245 truncated = 0;
1246 }
1247 pktsize -= fcslen;
Johannes Berg9b26b502013-01-22 13:02:09 +01001248 wakeup.packet = status->wake_packet;
1249 wakeup.packet_present_len = pktsize;
Johannes Bergf444eb12013-02-26 12:04:18 +01001250 wakeup.packet_len = pktlen - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001251 wakeup.packet_80211 = true;
1252 }
1253 }
1254
1255 report:
1256 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1257 kfree_skb(pkt);
Johannes Berg7abc4632013-08-06 18:28:26 +02001258}
Johannes Berg9b26b502013-01-22 13:02:09 +01001259
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001260static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1261 struct ieee80211_key_seq *seq)
1262{
1263 u64 pn;
1264
1265 pn = le64_to_cpu(sc->pn);
1266 seq->ccmp.pn[0] = pn >> 40;
1267 seq->ccmp.pn[1] = pn >> 32;
1268 seq->ccmp.pn[2] = pn >> 24;
1269 seq->ccmp.pn[3] = pn >> 16;
1270 seq->ccmp.pn[4] = pn >> 8;
1271 seq->ccmp.pn[5] = pn;
1272}
1273
1274static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1275 struct ieee80211_key_seq *seq)
1276{
1277 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1278 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1279}
1280
1281static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1282 struct ieee80211_key_conf *key)
1283{
1284 int tid;
1285
1286 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1287
1288 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1289 struct ieee80211_key_seq seq = {};
1290
1291 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1292 ieee80211_set_key_rx_seq(key, tid, &seq);
1293 }
1294}
1295
1296static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1297 struct ieee80211_key_conf *key)
1298{
1299 int tid;
1300
1301 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1302
1303 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1304 struct ieee80211_key_seq seq = {};
1305
1306 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1307 ieee80211_set_key_rx_seq(key, tid, &seq);
1308 }
1309}
1310
1311static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001312 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001313{
1314 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1315
1316 switch (key->cipher) {
1317 case WLAN_CIPHER_SUITE_CCMP:
1318 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1319 break;
1320 case WLAN_CIPHER_SUITE_TKIP:
1321 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1322 break;
1323 default:
1324 WARN_ON(1);
1325 }
1326}
1327
1328struct iwl_mvm_d3_gtk_iter_data {
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001329 struct iwl_wowlan_status *status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001330 void *last_gtk;
1331 u32 cipher;
1332 bool find_phase, unhandled_cipher;
1333 int num_keys;
1334};
1335
1336static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
1337 struct ieee80211_vif *vif,
1338 struct ieee80211_sta *sta,
1339 struct ieee80211_key_conf *key,
1340 void *_data)
1341{
1342 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1343
1344 if (data->unhandled_cipher)
1345 return;
1346
1347 switch (key->cipher) {
1348 case WLAN_CIPHER_SUITE_WEP40:
1349 case WLAN_CIPHER_SUITE_WEP104:
1350 /* ignore WEP completely, nothing to do */
1351 return;
1352 case WLAN_CIPHER_SUITE_CCMP:
1353 case WLAN_CIPHER_SUITE_TKIP:
1354 /* we support these */
1355 break;
1356 default:
1357 /* everything else (even CMAC for MFP) - disconnect from AP */
1358 data->unhandled_cipher = true;
1359 return;
1360 }
1361
1362 data->num_keys++;
1363
1364 /*
1365 * pairwise key - update sequence counters only;
1366 * note that this assumes no TDLS sessions are active
1367 */
1368 if (sta) {
1369 struct ieee80211_key_seq seq = {};
1370 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1371
1372 if (data->find_phase)
1373 return;
1374
1375 switch (key->cipher) {
1376 case WLAN_CIPHER_SUITE_CCMP:
1377 iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
1378 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
1379 break;
1380 case WLAN_CIPHER_SUITE_TKIP:
1381 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1382 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1383 break;
1384 }
1385 ieee80211_set_key_tx_seq(key, &seq);
1386
1387 /* that's it for this key */
1388 return;
1389 }
1390
1391 if (data->find_phase) {
1392 data->last_gtk = key;
1393 data->cipher = key->cipher;
1394 return;
1395 }
1396
1397 if (data->status->num_of_gtk_rekeys)
1398 ieee80211_remove_key(key);
1399 else if (data->last_gtk == key)
1400 iwl_mvm_set_key_rx_seq(key, data->status);
1401}
1402
1403static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1404 struct ieee80211_vif *vif,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001405 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001406{
1407 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1408 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1409 .status = status,
1410 };
Eliad Peller431591c2013-11-07 14:12:09 +02001411 u32 disconnection_reasons =
1412 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1413 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001414
1415 if (!status || !vif->bss_conf.bssid)
1416 return false;
1417
Eliad Peller431591c2013-11-07 14:12:09 +02001418 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1419 return false;
1420
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001421 /* find last GTK that we used initially, if any */
1422 gtkdata.find_phase = true;
1423 ieee80211_iter_keys(mvm->hw, vif,
1424 iwl_mvm_d3_update_gtks, &gtkdata);
1425 /* not trying to keep connections with MFP/unhandled ciphers */
1426 if (gtkdata.unhandled_cipher)
1427 return false;
1428 if (!gtkdata.num_keys)
Luciano Coelhocfa88892013-11-19 15:38:55 +02001429 goto out;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001430 if (!gtkdata.last_gtk)
1431 return false;
1432
1433 /*
1434 * invalidate all other GTKs that might still exist and update
1435 * the one that we used
1436 */
1437 gtkdata.find_phase = false;
1438 ieee80211_iter_keys(mvm->hw, vif,
1439 iwl_mvm_d3_update_gtks, &gtkdata);
1440
1441 if (status->num_of_gtk_rekeys) {
1442 struct ieee80211_key_conf *key;
1443 struct {
1444 struct ieee80211_key_conf conf;
1445 u8 key[32];
1446 } conf = {
1447 .conf.cipher = gtkdata.cipher,
1448 .conf.keyidx = status->gtk.key_index,
1449 };
1450
1451 switch (gtkdata.cipher) {
1452 case WLAN_CIPHER_SUITE_CCMP:
1453 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1454 memcpy(conf.conf.key, status->gtk.decrypt_key,
1455 WLAN_KEY_LEN_CCMP);
1456 break;
1457 case WLAN_CIPHER_SUITE_TKIP:
1458 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1459 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1460 /* leave TX MIC key zeroed, we don't use it anyway */
1461 memcpy(conf.conf.key +
1462 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1463 status->gtk.tkip_mic_key, 8);
1464 break;
1465 }
1466
1467 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1468 if (IS_ERR(key))
1469 return false;
1470 iwl_mvm_set_key_rx_seq(key, status);
1471 }
1472
1473 if (status->num_of_gtk_rekeys) {
1474 __be64 replay_ctr =
1475 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1476 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1477 (void *)&replay_ctr, GFP_KERNEL);
1478 }
1479
Luciano Coelhocfa88892013-11-19 15:38:55 +02001480out:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001481 mvmvif->seqno_valid = true;
1482 /* +0x10 because the set API expects next-to-use, not last-used */
1483 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1484
1485 return true;
1486}
1487
Luciano Coelhoec12f452014-11-20 10:14:44 +02001488static struct iwl_wowlan_status *
1489iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg7abc4632013-08-06 18:28:26 +02001490{
1491 u32 base = mvm->error_event_table;
1492 struct error_table_start {
1493 /* cf. struct iwl_error_event_table */
1494 u32 valid;
1495 u32 error_id;
1496 } err_info;
1497 struct iwl_host_cmd cmd = {
1498 .id = WOWLAN_GET_STATUSES,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001499 .flags = CMD_WANT_SKB,
Johannes Berg7abc4632013-08-06 18:28:26 +02001500 };
Luciano Coelhoec12f452014-11-20 10:14:44 +02001501 struct iwl_wowlan_status *status, *fw_status;
1502 int ret, len, status_size;
Johannes Berg7abc4632013-08-06 18:28:26 +02001503
1504 iwl_trans_read_mem_bytes(mvm->trans, base,
1505 &err_info, sizeof(err_info));
1506
1507 if (err_info.valid) {
Luciano Coelhoec12f452014-11-20 10:14:44 +02001508 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1509 err_info.valid, err_info.error_id);
Johannes Berg7abc4632013-08-06 18:28:26 +02001510 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1511 struct cfg80211_wowlan_wakeup wakeup = {
1512 .rfkill_release = true,
1513 };
1514 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1515 GFP_KERNEL);
1516 }
Luciano Coelhoec12f452014-11-20 10:14:44 +02001517 return ERR_PTR(-EIO);
Johannes Berg7abc4632013-08-06 18:28:26 +02001518 }
1519
1520 /* only for tracing for now */
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001521 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
Johannes Berg7abc4632013-08-06 18:28:26 +02001522 if (ret)
1523 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1524
1525 ret = iwl_mvm_send_cmd(mvm, &cmd);
1526 if (ret) {
1527 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
Luciano Coelhoec12f452014-11-20 10:14:44 +02001528 return ERR_PTR(ret);
Johannes Berg7abc4632013-08-06 18:28:26 +02001529 }
1530
1531 /* RF-kill already asserted again... */
Luciano Coelhoec12f452014-11-20 10:14:44 +02001532 if (!cmd.resp_pkt) {
1533 ret = -ERFKILL;
1534 goto out_free_resp;
1535 }
Johannes Berg7abc4632013-08-06 18:28:26 +02001536
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001537 status_size = sizeof(*fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001538
Johannes Berg65b30342014-01-08 13:16:33 +01001539 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1540 if (len < status_size) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001541 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
Luciano Coelhoec12f452014-11-20 10:14:44 +02001542 ret = -EIO;
Johannes Berg7abc4632013-08-06 18:28:26 +02001543 goto out_free_resp;
1544 }
1545
Luciano Coelhoec12f452014-11-20 10:14:44 +02001546 status = (void *)cmd.resp_pkt->data;
1547 if (len != (status_size +
1548 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1549 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1550 ret = -EIO;
1551 goto out_free_resp;
1552 }
1553
1554 fw_status = kmemdup(status, len, GFP_KERNEL);
1555
1556out_free_resp:
1557 iwl_free_resp(&cmd);
1558 return ret ? ERR_PTR(ret) : fw_status;
1559}
1560
1561/* releases the MVM mutex */
1562static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1563 struct ieee80211_vif *vif)
1564{
1565 struct iwl_wowlan_status_data status;
1566 struct iwl_wowlan_status *fw_status;
1567 int i;
1568 bool keep;
1569 struct ieee80211_sta *ap_sta;
1570 struct iwl_mvm_sta *mvm_ap_sta;
1571
1572 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1573 if (IS_ERR_OR_NULL(fw_status))
1574 goto out_unlock;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001575
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001576 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1577 for (i = 0; i < 8; i++)
1578 status.qos_seq_ctr[i] =
1579 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1580 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1581 status.wake_packet_length =
1582 le32_to_cpu(fw_status->wake_packet_length);
1583 status.wake_packet_bufsize =
1584 le32_to_cpu(fw_status->wake_packet_bufsize);
1585 status.wake_packet = fw_status->wake_packet;
Johannes Berg7abc4632013-08-06 18:28:26 +02001586
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001587 /* still at hard-coded place 0 for D3 image */
1588 ap_sta = rcu_dereference_protected(
1589 mvm->fw_id_to_mac_id[0],
1590 lockdep_is_held(&mvm->mutex));
1591 if (IS_ERR_OR_NULL(ap_sta))
Luciano Coelhoec12f452014-11-20 10:14:44 +02001592 goto out_free;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001593
1594 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
1595 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1596 u16 seq = status.qos_seq_ctr[i];
1597 /* firmware stores last-used value, we store next value */
1598 seq += 0x10;
1599 mvm_ap_sta->tid_data[i].seq_number = seq;
1600 }
1601
Johannes Berg7abc4632013-08-06 18:28:26 +02001602 /* now we have all the data we need, unlock to avoid mac80211 issues */
1603 mutex_unlock(&mvm->mutex);
1604
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001605 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1606
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001607 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001608
Luciano Coelhoec12f452014-11-20 10:14:44 +02001609 kfree(fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001610 return keep;
Johannes Berg7abc4632013-08-06 18:28:26 +02001611
Luciano Coelhoec12f452014-11-20 10:14:44 +02001612out_free:
1613 kfree(fw_status);
1614out_unlock:
Johannes Berg7abc4632013-08-06 18:28:26 +02001615 mutex_unlock(&mvm->mutex);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001616 return false;
Johannes Berg9b26b502013-01-22 13:02:09 +01001617}
1618
Luciano Coelhob04998f2014-11-20 15:58:34 +02001619static u32 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm)
1620{
1621 struct iwl_scan_offload_profiles_query *query;
1622 struct iwl_host_cmd cmd = {
1623 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1624 .flags = CMD_WANT_SKB,
1625 };
1626 int ret, len;
1627
1628 ret = iwl_mvm_send_cmd(mvm, &cmd);
1629 if (ret) {
1630 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
1631 return 0;
1632 }
1633
1634 /* RF-kill already asserted again... */
1635 if (!cmd.resp_pkt)
1636 goto out_free_resp;
1637
1638 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1639 if (len < sizeof(*query)) {
1640 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
1641 goto out_free_resp;
1642 }
1643
1644 query = (void *)cmd.resp_pkt->data;
1645
1646 ret = le32_to_cpu(query->matched_profiles);
1647
1648out_free_resp:
1649 iwl_free_resp(&cmd);
1650 return ret;
1651}
1652
Luciano Coelho2021a892014-11-20 08:59:51 +02001653static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1654 struct ieee80211_vif *vif)
1655{
Luciano Coelhob04998f2014-11-20 15:58:34 +02001656 struct cfg80211_wowlan_nd_info net_detect = {};
Luciano Coelho2021a892014-11-20 08:59:51 +02001657 struct cfg80211_wowlan_wakeup wakeup = {
1658 .pattern_idx = -1,
1659 };
1660 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1661 struct iwl_wowlan_status *fw_status;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001662 u32 matched_profiles;
Luciano Coelho2021a892014-11-20 08:59:51 +02001663 u32 reasons = 0;
1664
1665 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1666 if (!IS_ERR_OR_NULL(fw_status))
1667 reasons = le32_to_cpu(fw_status->wakeup_reasons);
1668
1669 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1670 wakeup.rfkill_release = true;
1671
Luciano Coelhob04998f2014-11-20 15:58:34 +02001672 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1673 goto out;
1674
1675 matched_profiles = iwl_mvm_netdetect_query_results(mvm);
1676 if (!matched_profiles) {
Luciano Coelho2021a892014-11-20 08:59:51 +02001677 wakeup_report = NULL;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001678 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001679 }
1680
Luciano Coelhob04998f2014-11-20 15:58:34 +02001681 wakeup.net_detect = &net_detect;
1682out:
Luciano Coelho2021a892014-11-20 08:59:51 +02001683 mutex_unlock(&mvm->mutex);
1684 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1685}
1686
Johannes Bergafc66bb2013-05-03 11:44:16 +02001687static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1688{
1689#ifdef CONFIG_IWLWIFI_DEBUGFS
1690 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1691 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1692 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1693
1694 if (!mvm->store_d3_resume_sram)
1695 return;
1696
1697 if (!mvm->d3_resume_sram) {
1698 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1699 if (!mvm->d3_resume_sram)
1700 return;
1701 }
1702
1703 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1704#endif
1705}
1706
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001707static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1708 struct ieee80211_vif *vif)
1709{
1710 /* skip the one we keep connection on */
1711 if (data == vif)
1712 return;
1713
1714 if (vif->type == NL80211_IFTYPE_STATION)
1715 ieee80211_resume_disconnect(vif);
1716}
1717
Johannes Bergdebff612013-05-14 13:53:45 +02001718static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001719{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001720 struct ieee80211_vif *vif = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001721 int ret;
1722 enum iwl_d3_status d3_status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001723 bool keep = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001724
1725 mutex_lock(&mvm->mutex);
1726
1727 /* get the BSS vif pointer again */
Luciano Coelho7f549e22014-10-02 15:38:04 +03001728 vif = iwl_mvm_get_bss_vif(mvm);
1729 if (IS_ERR_OR_NULL(vif))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001730 goto out_unlock;
1731
Johannes Bergdebff612013-05-14 13:53:45 +02001732 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001733 if (ret)
1734 goto out_unlock;
1735
1736 if (d3_status != IWL_D3_STATUS_ALIVE) {
1737 IWL_INFO(mvm, "Device was reset during suspend\n");
1738 goto out_unlock;
1739 }
1740
Johannes Bergafc66bb2013-05-03 11:44:16 +02001741 /* query SRAM first in case we want event logging */
1742 iwl_mvm_read_d3_sram(mvm);
1743
Luciano Coelho2021a892014-11-20 08:59:51 +02001744 if (mvm->net_detect) {
1745 iwl_mvm_query_netdetect_reasons(mvm, vif);
1746 } else {
1747 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
Eliad Peller78c9df62013-11-07 14:13:30 +02001748#ifdef CONFIG_IWLWIFI_DEBUGFS
Luciano Coelho2021a892014-11-20 08:59:51 +02001749 if (keep)
1750 mvm->keep_vif = vif;
Eliad Peller78c9df62013-11-07 14:13:30 +02001751#endif
Luciano Coelho2021a892014-11-20 08:59:51 +02001752 }
Johannes Berg7abc4632013-08-06 18:28:26 +02001753 /* has unlocked the mutex, so skip that */
1754 goto out;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001755
1756 out_unlock:
1757 mutex_unlock(&mvm->mutex);
1758
Johannes Berg7abc4632013-08-06 18:28:26 +02001759 out:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001760 if (!test)
1761 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1762 IEEE80211_IFACE_ITER_NORMAL,
1763 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001764
1765 /* return 1 to reconfigure the device */
1766 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
Johannes Berg58629d92014-11-06 09:40:50 +01001767 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001768 return 1;
1769}
1770
Johannes Bergdebff612013-05-14 13:53:45 +02001771int iwl_mvm_resume(struct ieee80211_hw *hw)
1772{
1773 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1774
Eliad Peller465921c2014-09-30 10:02:13 +03001775 iwl_trans_resume(mvm->trans);
1776
Eliad Peller088070a2014-10-20 18:42:58 +03001777 if (iwl_mvm_is_d0i3_supported(mvm))
Eliad Pellerd15a7472014-03-27 18:53:12 +02001778 return 0;
Eliad Pellerd15a7472014-03-27 18:53:12 +02001779
Johannes Bergdebff612013-05-14 13:53:45 +02001780 return __iwl_mvm_resume(mvm, false);
1781}
1782
Johannes Berg8ca151b2013-01-24 14:25:36 +01001783void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1784{
1785 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1786
1787 device_set_wakeup_enable(mvm->trans->dev, enabled);
1788}
Johannes Bergdebff612013-05-14 13:53:45 +02001789
1790#ifdef CONFIG_IWLWIFI_DEBUGFS
1791static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
1792{
1793 struct iwl_mvm *mvm = inode->i_private;
1794 int err;
1795
1796 if (mvm->d3_test_active)
1797 return -EBUSY;
1798
1799 file->private_data = inode->i_private;
1800
1801 ieee80211_stop_queues(mvm->hw);
1802 synchronize_net();
1803
1804 /* start pseudo D3 */
1805 rtnl_lock();
1806 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
1807 rtnl_unlock();
1808 if (err > 0)
1809 err = -EINVAL;
1810 if (err) {
1811 ieee80211_wake_queues(mvm->hw);
1812 return err;
1813 }
1814 mvm->d3_test_active = true;
Eliad Peller78c9df62013-11-07 14:13:30 +02001815 mvm->keep_vif = NULL;
Johannes Bergdebff612013-05-14 13:53:45 +02001816 return 0;
1817}
1818
1819static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
1820 size_t count, loff_t *ppos)
1821{
1822 struct iwl_mvm *mvm = file->private_data;
1823 u32 pme_asserted;
1824
1825 while (true) {
Eliad Peller4ed80a82013-11-07 13:23:21 +02001826 /* read pme_ptr if available */
1827 if (mvm->d3_test_pme_ptr) {
1828 pme_asserted = iwl_trans_read_mem32(mvm->trans,
1829 mvm->d3_test_pme_ptr);
1830 if (pme_asserted)
1831 break;
1832 }
1833
Johannes Bergdebff612013-05-14 13:53:45 +02001834 if (msleep_interruptible(100))
1835 break;
1836 }
1837
1838 return 0;
1839}
1840
1841static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
1842 struct ieee80211_vif *vif)
1843{
Eliad Peller78c9df62013-11-07 14:13:30 +02001844 /* skip the one we keep connection on */
1845 if (_data == vif)
1846 return;
1847
Johannes Bergdebff612013-05-14 13:53:45 +02001848 if (vif->type == NL80211_IFTYPE_STATION)
1849 ieee80211_connection_loss(vif);
1850}
1851
1852static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
1853{
1854 struct iwl_mvm *mvm = inode->i_private;
1855 int remaining_time = 10;
1856
1857 mvm->d3_test_active = false;
Luciano Coelho31ca09d2014-09-22 13:22:51 +03001858 rtnl_lock();
Johannes Bergdebff612013-05-14 13:53:45 +02001859 __iwl_mvm_resume(mvm, true);
Luciano Coelho31ca09d2014-09-22 13:22:51 +03001860 rtnl_unlock();
Johannes Bergdebff612013-05-14 13:53:45 +02001861 iwl_abort_notification_waits(&mvm->notif_wait);
1862 ieee80211_restart_hw(mvm->hw);
1863
1864 /* wait for restart and disconnect all interfaces */
1865 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1866 remaining_time > 0) {
1867 remaining_time--;
1868 msleep(1000);
1869 }
1870
1871 if (remaining_time == 0)
1872 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
1873
1874 ieee80211_iterate_active_interfaces_atomic(
1875 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
Eliad Peller78c9df62013-11-07 14:13:30 +02001876 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
Johannes Bergdebff612013-05-14 13:53:45 +02001877
1878 ieee80211_wake_queues(mvm->hw);
1879
1880 return 0;
1881}
1882
1883const struct file_operations iwl_dbgfs_d3_test_ops = {
1884 .llseek = no_llseek,
1885 .open = iwl_mvm_d3_test_open,
1886 .read = iwl_mvm_d3_test_read,
1887 .release = iwl_mvm_d3_test_release,
1888};
1889#endif