blob: 7694472a303e0062b511ddb97404a11a1717c75d [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 Berg8ca151b2013-01-24 14:25:36 +01009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020025 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010026 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020033 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8ca151b2013-01-24 14:25:36 +010034 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63
Johannes Bergf444eb12013-02-26 12:04:18 +010064#include <linux/etherdevice.h>
Johannes Bergf0c26462013-01-22 20:41:58 +010065#include <linux/ip.h>
Johannes Bergdebff612013-05-14 13:53:45 +020066#include <linux/fs.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010067#include <net/cfg80211.h>
68#include <net/ipv6.h>
Johannes Bergf0c26462013-01-22 20:41:58 +010069#include <net/tcp.h>
Johannes Bergf7fc5982013-08-20 13:04:10 +020070#include <net/addrconf.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010071#include "iwl-modparams.h"
72#include "fw-api.h"
73#include "mvm.h"
74
75void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
76 struct ieee80211_vif *vif,
77 struct cfg80211_gtk_rekey_data *data)
78{
79 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
80 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
81
82 if (iwlwifi_mod_params.sw_crypto)
83 return;
84
85 mutex_lock(&mvm->mutex);
86
87 memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
88 memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
89 mvmvif->rekey_data.replay_ctr =
90 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
91 mvmvif->rekey_data.valid = true;
92
93 mutex_unlock(&mvm->mutex);
94}
95
96#if IS_ENABLED(CONFIG_IPV6)
97void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
98 struct ieee80211_vif *vif,
99 struct inet6_dev *idev)
100{
101 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
102 struct inet6_ifaddr *ifa;
103 int idx = 0;
104
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200105 read_lock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100106 list_for_each_entry(ifa, &idev->addr_list, if_list) {
107 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
108 idx++;
Johannes Berg5369d6c2013-07-08 11:17:06 +0200109 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100110 break;
111 }
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200112 read_unlock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100113
114 mvmvif->num_target_ipv6_addrs = idx;
115}
116#endif
117
118void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
119 struct ieee80211_vif *vif, int idx)
120{
121 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
122
123 mvmvif->tx_key_idx = idx;
124}
125
126static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
127{
128 int i;
129
130 for (i = 0; i < IWL_P1K_SIZE; i++)
131 out[i] = cpu_to_le16(p1k[i]);
132}
133
134struct wowlan_key_data {
135 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
136 struct iwl_wowlan_tkip_params_cmd *tkip;
137 bool error, use_rsc_tsc, use_tkip;
Johannes Berg5312e542013-07-22 18:26:56 +0200138 int wep_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100139};
140
141static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
142 struct ieee80211_vif *vif,
143 struct ieee80211_sta *sta,
144 struct ieee80211_key_conf *key,
145 void *_data)
146{
147 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
148 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
149 struct wowlan_key_data *data = _data;
150 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
151 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
152 struct iwl_p1k_cache *rx_p1ks;
153 u8 *rx_mic_key;
154 struct ieee80211_key_seq seq;
155 u32 cur_rx_iv32 = 0;
156 u16 p1k[IWL_P1K_SIZE];
157 int ret, i;
158
159 mutex_lock(&mvm->mutex);
160
161 switch (key->cipher) {
162 case WLAN_CIPHER_SUITE_WEP40:
163 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
164 struct {
165 struct iwl_mvm_wep_key_cmd wep_key_cmd;
166 struct iwl_mvm_wep_key wep_key;
167 } __packed wkc = {
168 .wep_key_cmd.mac_id_n_color =
169 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
170 mvmvif->color)),
171 .wep_key_cmd.num_keys = 1,
172 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
173 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
174 .wep_key.key_index = key->keyidx,
175 .wep_key.key_size = key->keylen,
176 };
177
178 /*
179 * This will fail -- the key functions don't set support
180 * pairwise WEP keys. However, that's better than silently
181 * failing WoWLAN. Or maybe not?
182 */
183 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
184 break;
185
186 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
187 if (key->keyidx == mvmvif->tx_key_idx) {
188 /* TX key must be at offset 0 */
189 wkc.wep_key.key_offset = 0;
190 } else {
191 /* others start at 1 */
Johannes Berg5312e542013-07-22 18:26:56 +0200192 data->wep_key_idx++;
193 wkc.wep_key.key_offset = data->wep_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100194 }
195
196 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, CMD_SYNC,
197 sizeof(wkc), &wkc);
198 data->error = ret != 0;
199
Johannes Bergf444eb12013-02-26 12:04:18 +0100200 mvm->ptk_ivlen = key->iv_len;
201 mvm->ptk_icvlen = key->icv_len;
202 mvm->gtk_ivlen = key->iv_len;
203 mvm->gtk_icvlen = key->icv_len;
204
Johannes Berg8ca151b2013-01-24 14:25:36 +0100205 /* don't upload key again */
206 goto out_unlock;
207 }
208 default:
209 data->error = true;
210 goto out_unlock;
211 case WLAN_CIPHER_SUITE_AES_CMAC:
212 /*
213 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
214 * but we also shouldn't abort suspend due to that. It does have
215 * support for the IGTK key renewal, but doesn't really use the
216 * IGTK for anything. This means we could spuriously wake up or
217 * be deauthenticated, but that was considered acceptable.
218 */
219 goto out_unlock;
220 case WLAN_CIPHER_SUITE_TKIP:
221 if (sta) {
222 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
223 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
224
225 rx_p1ks = data->tkip->rx_uni;
226
227 ieee80211_get_key_tx_seq(key, &seq);
228 tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
229 tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
230
231 ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
232 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
233
234 memcpy(data->tkip->mic_keys.tx,
235 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
236 IWL_MIC_KEY_SIZE);
237
238 rx_mic_key = data->tkip->mic_keys.rx_unicast;
239 } else {
240 tkip_sc =
241 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
242 rx_p1ks = data->tkip->rx_multi;
243 rx_mic_key = data->tkip->mic_keys.rx_mcast;
244 }
245
246 /*
247 * For non-QoS this relies on the fact that both the uCode and
248 * mac80211 use TID 0 (as they need to to avoid replay attacks)
249 * for checking the IV in the frames.
250 */
251 for (i = 0; i < IWL_NUM_RSC; i++) {
252 ieee80211_get_key_rx_seq(key, i, &seq);
253 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
254 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
255 /* wrapping isn't allowed, AP must rekey */
256 if (seq.tkip.iv32 > cur_rx_iv32)
257 cur_rx_iv32 = seq.tkip.iv32;
258 }
259
260 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
261 cur_rx_iv32, p1k);
262 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
263 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
264 cur_rx_iv32 + 1, p1k);
265 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
266
267 memcpy(rx_mic_key,
268 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
269 IWL_MIC_KEY_SIZE);
270
271 data->use_tkip = true;
272 data->use_rsc_tsc = true;
273 break;
274 case WLAN_CIPHER_SUITE_CCMP:
275 if (sta) {
276 u8 *pn = seq.ccmp.pn;
277
278 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
279 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
280
281 ieee80211_get_key_tx_seq(key, &seq);
282 aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
283 ((u64)pn[4] << 8) |
284 ((u64)pn[3] << 16) |
285 ((u64)pn[2] << 24) |
286 ((u64)pn[1] << 32) |
287 ((u64)pn[0] << 40));
288 } else {
289 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
290 }
291
292 /*
293 * For non-QoS this relies on the fact that both the uCode and
294 * mac80211 use TID 0 for checking the IV in the frames.
295 */
296 for (i = 0; i < IWL_NUM_RSC; i++) {
297 u8 *pn = seq.ccmp.pn;
298
299 ieee80211_get_key_rx_seq(key, i, &seq);
300 aes_sc->pn = cpu_to_le64((u64)pn[5] |
301 ((u64)pn[4] << 8) |
302 ((u64)pn[3] << 16) |
303 ((u64)pn[2] << 24) |
304 ((u64)pn[1] << 32) |
305 ((u64)pn[0] << 40));
306 }
307 data->use_rsc_tsc = true;
308 break;
309 }
310
311 /*
312 * The D3 firmware hardcodes the key offset 0 as the key it uses
313 * to transmit packets to the AP, i.e. the PTK.
314 */
315 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
316 key->hw_key_idx = 0;
Johannes Bergf444eb12013-02-26 12:04:18 +0100317 mvm->ptk_ivlen = key->iv_len;
318 mvm->ptk_icvlen = key->icv_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100319 } else {
Johannes Berg5312e542013-07-22 18:26:56 +0200320 /*
321 * firmware only supports TSC/RSC for a single key,
322 * so if there are multiple keep overwriting them
323 * with new ones -- this relies on mac80211 doing
324 * list_add_tail().
325 */
326 key->hw_key_idx = 1;
Johannes Bergf444eb12013-02-26 12:04:18 +0100327 mvm->gtk_ivlen = key->iv_len;
328 mvm->gtk_icvlen = key->icv_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100329 }
330
331 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
332 data->error = ret != 0;
333out_unlock:
334 mutex_unlock(&mvm->mutex);
335}
336
337static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
338 struct cfg80211_wowlan *wowlan)
339{
340 struct iwl_wowlan_patterns_cmd *pattern_cmd;
341 struct iwl_host_cmd cmd = {
342 .id = WOWLAN_PATTERNS,
343 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
344 .flags = CMD_SYNC,
345 };
346 int i, err;
347
348 if (!wowlan->n_patterns)
349 return 0;
350
351 cmd.len[0] = sizeof(*pattern_cmd) +
352 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
353
354 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
355 if (!pattern_cmd)
356 return -ENOMEM;
357
358 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
359
360 for (i = 0; i < wowlan->n_patterns; i++) {
361 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
362
363 memcpy(&pattern_cmd->patterns[i].mask,
364 wowlan->patterns[i].mask, mask_len);
365 memcpy(&pattern_cmd->patterns[i].pattern,
366 wowlan->patterns[i].pattern,
367 wowlan->patterns[i].pattern_len);
368 pattern_cmd->patterns[i].mask_size = mask_len;
369 pattern_cmd->patterns[i].pattern_size =
370 wowlan->patterns[i].pattern_len;
371 }
372
373 cmd.data[0] = pattern_cmd;
374 err = iwl_mvm_send_cmd(mvm, &cmd);
375 kfree(pattern_cmd);
376 return err;
377}
378
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, },
521 .flags = CMD_SYNC,
522 };
523 int ret;
524
525 if (!tcp)
526 return 0;
527
528 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
529 if (!cfg)
530 return -ENOMEM;
531 cmd.data[0] = cfg;
532
533 cfg->max_syn_retries = 10;
534 cfg->max_data_retries = 10;
535 cfg->tcp_syn_ack_timeout = 1; /* seconds */
536 cfg->tcp_ack_timeout = 1; /* seconds */
537
538 /* SYN (TX) */
539 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300540 vif, tcp, cfg->syn_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100541 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
542 MVM_TCP_TX_SYN);
543 cfg->syn_tx.info.tcp_payload_length = 0;
544
545 /* SYN/ACK (RX) */
546 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300547 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
Johannes Bergf0c26462013-01-22 20:41:58 +0100548 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
549 MVM_TCP_RX_SYNACK);
550 cfg->synack_rx.info.tcp_payload_length = 0;
551
552 /* KEEPALIVE/ACK (TX) */
553 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300554 vif, tcp, cfg->keepalive_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100555 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
556 MVM_TCP_TX_DATA);
557 cfg->keepalive_tx.info.tcp_payload_length =
558 cpu_to_le16(tcp->payload_len);
559 cfg->sequence_number_offset = tcp->payload_seq.offset;
560 /* length must be 0..4, the field is little endian */
561 cfg->sequence_number_length = tcp->payload_seq.len;
562 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
563 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
564 if (tcp->payload_tok.len) {
565 cfg->token_offset = tcp->payload_tok.offset;
566 cfg->token_length = tcp->payload_tok.len;
567 cfg->num_tokens =
568 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
569 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
570 tcp->tokens_size);
571 } else {
572 /* set tokens to max value to almost never run out */
573 cfg->num_tokens = cpu_to_le16(65535);
574 }
575
576 /* ACK (RX) */
577 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300578 vif, tcp, cfg->keepalive_ack_rx.data,
Johannes Bergf0c26462013-01-22 20:41:58 +0100579 cfg->keepalive_ack_rx.rx_mask,
580 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
581 MVM_TCP_RX_ACK);
582 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
583
584 /* WAKEUP (RX) */
585 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300586 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
Johannes Bergf0c26462013-01-22 20:41:58 +0100587 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
588 MVM_TCP_RX_WAKE);
589 cfg->wake_rx.info.tcp_payload_length =
590 cpu_to_le16(tcp->wake_len);
591
592 /* FIN */
593 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300594 vif, tcp, cfg->fin_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100595 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
596 MVM_TCP_TX_FIN);
597 cfg->fin_tx.info.tcp_payload_length = 0;
598
599 ret = iwl_mvm_send_cmd(mvm, &cmd);
600 kfree(cfg);
601
602 return ret;
603}
604
Johannes Berg8ca151b2013-01-24 14:25:36 +0100605struct iwl_d3_iter_data {
606 struct iwl_mvm *mvm;
607 struct ieee80211_vif *vif;
608 bool error;
609};
610
611static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
612 struct ieee80211_vif *vif)
613{
614 struct iwl_d3_iter_data *data = _data;
615 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
616
617 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
618 return;
619
620 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
621 return;
622
623 if (data->vif) {
624 IWL_ERR(data->mvm, "More than one managed interface active!\n");
625 data->error = true;
626 return;
627 }
628
629 data->vif = vif;
630}
631
632static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
633 struct ieee80211_sta *ap_sta)
634{
635 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
636 struct ieee80211_chanctx_conf *ctx;
637 u8 chains_static, chains_dynamic;
638 struct cfg80211_chan_def chandef;
639 int ret, i;
640 struct iwl_binding_cmd binding_cmd = {};
641 struct iwl_time_quota_cmd quota_cmd = {};
642 u32 status;
643
644 /* add back the PHY */
645 if (WARN_ON(!mvmvif->phy_ctxt))
646 return -EINVAL;
647
648 rcu_read_lock();
649 ctx = rcu_dereference(vif->chanctx_conf);
650 if (WARN_ON(!ctx)) {
651 rcu_read_unlock();
652 return -EINVAL;
653 }
654 chandef = ctx->def;
655 chains_static = ctx->rx_chains_static;
656 chains_dynamic = ctx->rx_chains_dynamic;
657 rcu_read_unlock();
658
659 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
660 chains_static, chains_dynamic);
661 if (ret)
662 return ret;
663
664 /* add back the MAC */
665 mvmvif->uploaded = false;
666
667 if (WARN_ON(!vif->bss_conf.assoc))
668 return -EINVAL;
669 /* hack */
670 vif->bss_conf.assoc = false;
671 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
672 vif->bss_conf.assoc = true;
673 if (ret)
674 return ret;
675
676 /* add back binding - XXX refactor? */
677 binding_cmd.id_and_color =
678 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
679 mvmvif->phy_ctxt->color));
680 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
681 binding_cmd.phy =
682 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
683 mvmvif->phy_ctxt->color));
684 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
685 mvmvif->color));
686 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
687 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
688
689 status = 0;
690 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
691 sizeof(binding_cmd), &binding_cmd,
692 &status);
693 if (ret) {
694 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
695 return ret;
696 }
697
698 if (status) {
699 IWL_ERR(mvm, "Binding command failed: %u\n", status);
700 return -EIO;
701 }
702
Johannes Berg7a453972013-02-12 13:10:44 +0100703 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100704 if (ret)
705 return ret;
706 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
707
708 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
709 if (ret)
710 return ret;
711
712 /* and some quota */
713 quota_cmd.quotas[0].id_and_color =
714 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
715 mvmvif->phy_ctxt->color));
Emmanuel Grumbach9a3daf82014-03-05 10:00:44 +0200716 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
717 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100718
719 for (i = 1; i < MAX_BINDINGS; i++)
720 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
721
722 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, CMD_SYNC,
723 sizeof(quota_cmd), &quota_cmd);
724 if (ret)
725 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
726
727 return 0;
728}
729
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200730static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
731 struct ieee80211_vif *vif)
732{
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200733 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
734 struct iwl_nonqos_seq_query_cmd query_cmd = {
735 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
736 .mac_id_n_color =
737 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
738 mvmvif->color)),
739 };
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200740 struct iwl_host_cmd cmd = {
741 .id = NON_QOS_TX_COUNTER_CMD,
742 .flags = CMD_SYNC | CMD_WANT_SKB,
743 };
744 int err;
745 u32 size;
746
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300747 cmd.data[0] = &query_cmd;
748 cmd.len[0] = sizeof(query_cmd);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200749
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200750 err = iwl_mvm_send_cmd(mvm, &cmd);
751 if (err)
752 return err;
753
Johannes Berg65b30342014-01-08 13:16:33 +0100754 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200755 if (size < sizeof(__le16)) {
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200756 err = -EINVAL;
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200757 } else {
758 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300759 /* firmware returns next, not last-used seqno */
760 err = (u16) (err - 0x10);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200761 }
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200762
763 iwl_free_resp(&cmd);
764 return err;
765}
766
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200767void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
768{
769 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
770 struct iwl_nonqos_seq_query_cmd query_cmd = {
771 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
772 .mac_id_n_color =
773 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
774 mvmvif->color)),
775 .value = cpu_to_le16(mvmvif->seqno),
776 };
777
778 /* return if called during restart, not resume from D3 */
779 if (!mvmvif->seqno_valid)
780 return;
781
782 mvmvif->seqno_valid = false;
783
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200784 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, CMD_SYNC,
785 sizeof(query_cmd), &query_cmd))
786 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
787}
788
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200789static int
790iwl_mvm_send_wowlan_config_cmd(struct iwl_mvm *mvm,
791 const struct iwl_wowlan_config_cmd_v3 *cmd)
792{
793 /* start only with the v2 part of the command */
794 u16 cmd_len = sizeof(cmd->common);
795
796 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_WOWLAN_CONFIG_TID)
797 cmd_len = sizeof(*cmd);
798
799 return iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, CMD_SYNC,
800 cmd_len, cmd);
801}
802
Johannes Bergdebff612013-05-14 13:53:45 +0200803static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
804 struct cfg80211_wowlan *wowlan,
805 bool test)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100806{
807 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
808 struct iwl_d3_iter_data suspend_iter_data = {
809 .mvm = mvm,
810 };
811 struct ieee80211_vif *vif;
812 struct iwl_mvm_vif *mvmvif;
813 struct ieee80211_sta *ap_sta;
814 struct iwl_mvm_sta *mvm_ap_sta;
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200815 struct iwl_wowlan_config_cmd_v3 wowlan_config_cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100816 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
817 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
Johannes Bergdebff612013-05-14 13:53:45 +0200818 struct iwl_d3_manager_config d3_cfg_cmd_data = {
Johannes Bergc9204872013-02-15 11:00:58 +0100819 /*
820 * Program the minimum sleep time to 10 seconds, as many
821 * platforms have issues processing a wakeup signal while
822 * still being in the process of suspending.
823 */
824 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
825 };
Johannes Bergdebff612013-05-14 13:53:45 +0200826 struct iwl_host_cmd d3_cfg_cmd = {
827 .id = D3_CONFIG_CMD,
828 .flags = CMD_SYNC | CMD_WANT_SKB,
829 .data[0] = &d3_cfg_cmd_data,
830 .len[0] = sizeof(d3_cfg_cmd_data),
831 };
Johannes Berg8ca151b2013-01-24 14:25:36 +0100832 struct wowlan_key_data key_data = {
833 .use_rsc_tsc = false,
834 .tkip = &tkip_cmd,
835 .use_tkip = false,
836 };
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200837 int ret;
Johannes Bergdebff612013-05-14 13:53:45 +0200838 int len __maybe_unused;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100839
Johannes Bergdebff612013-05-14 13:53:45 +0200840 if (!wowlan) {
841 /*
842 * mac80211 shouldn't get here, but for D3 test
843 * it doesn't warrant a warning
844 */
845 WARN_ON(!test);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100846 return -EINVAL;
Johannes Bergdebff612013-05-14 13:53:45 +0200847 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100848
849 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
850 if (!key_data.rsc_tsc)
851 return -ENOMEM;
852
853 mutex_lock(&mvm->mutex);
854
Johannes Berg8ca151b2013-01-24 14:25:36 +0100855 /* see if there's only a single BSS vif and it's associated */
856 ieee80211_iterate_active_interfaces_atomic(
857 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
858 iwl_mvm_d3_iface_iterator, &suspend_iter_data);
859
860 if (suspend_iter_data.error || !suspend_iter_data.vif) {
861 ret = 1;
862 goto out_noreset;
863 }
864
865 vif = suspend_iter_data.vif;
866 mvmvif = iwl_mvm_vif_from_mac80211(vif);
867
868 ap_sta = rcu_dereference_protected(
869 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
870 lockdep_is_held(&mvm->mutex));
871 if (IS_ERR_OR_NULL(ap_sta)) {
872 ret = -EINVAL;
873 goto out_noreset;
874 }
875
876 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
877
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200878 /* TODO: wowlan_config_cmd.common.wowlan_ba_teardown_tids */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100879
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200880 wowlan_config_cmd.common.is_11n_connection =
881 ap_sta->ht_cap.ht_supported;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100882
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200883 /* Query the last used seqno and set it */
884 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
885 if (ret < 0)
886 goto out_noreset;
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200887 wowlan_config_cmd.common.non_qos_seq = cpu_to_le16(ret);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100888
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200889 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, &wowlan_config_cmd.common);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100890
891 if (wowlan->disconnect)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200892 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100893 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
894 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
895 if (wowlan->magic_pkt)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200896 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100897 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
898 if (wowlan->gtk_rekey_failure)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200899 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100900 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
901 if (wowlan->eap_identity_req)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200902 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100903 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
904 if (wowlan->four_way_handshake)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200905 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100906 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
907 if (wowlan->n_patterns)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200908 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100909 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
910
911 if (wowlan->rfkill_release)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200912 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100913 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
914
Johannes Bergf0c26462013-01-22 20:41:58 +0100915 if (wowlan->tcp) {
916 /*
Johannes Berg6039f3e2013-03-20 10:40:05 +0100917 * Set the "link change" (really "link lost") flag as well
918 * since that implies losing the TCP connection.
Johannes Bergf0c26462013-01-22 20:41:58 +0100919 */
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200920 wowlan_config_cmd.common.wakeup_filter |=
Johannes Bergf0c26462013-01-22 20:41:58 +0100921 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
922 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
923 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
924 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
925 }
926
Johannes Berg8ca151b2013-01-24 14:25:36 +0100927 iwl_mvm_cancel_scan(mvm);
928
929 iwl_trans_stop_device(mvm->trans);
930
931 /*
932 * Set the HW restart bit -- this is mostly true as we're
933 * going to load new firmware and reprogram that, though
934 * the reprogramming is going to be manual to avoid adding
935 * all the MACs that aren't support.
936 * We don't have to clear up everything though because the
937 * reprogramming is manual. When we resume, we'll actually
938 * go through a proper restart sequence again to switch
939 * back to the runtime firmware image.
940 */
941 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
942
943 /* We reprogram keys and shouldn't allocate new key indices */
944 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
945
Johannes Bergf444eb12013-02-26 12:04:18 +0100946 mvm->ptk_ivlen = 0;
947 mvm->ptk_icvlen = 0;
948 mvm->ptk_ivlen = 0;
949 mvm->ptk_icvlen = 0;
950
Johannes Berg8ca151b2013-01-24 14:25:36 +0100951 ret = iwl_mvm_load_d3_fw(mvm);
952 if (ret)
953 goto out;
954
955 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
956 if (ret)
957 goto out;
958
959 if (!iwlwifi_mod_params.sw_crypto) {
960 /*
961 * This needs to be unlocked due to lock ordering
962 * constraints. Since we're in the suspend path
963 * that isn't really a problem though.
964 */
965 mutex_unlock(&mvm->mutex);
966 ieee80211_iter_keys(mvm->hw, vif,
967 iwl_mvm_wowlan_program_keys,
968 &key_data);
969 mutex_lock(&mvm->mutex);
970 if (key_data.error) {
971 ret = -EIO;
972 goto out;
973 }
974
975 if (key_data.use_rsc_tsc) {
976 struct iwl_host_cmd rsc_tsc_cmd = {
977 .id = WOWLAN_TSC_RSC_PARAM,
978 .flags = CMD_SYNC,
979 .data[0] = key_data.rsc_tsc,
980 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
981 .len[0] = sizeof(*key_data.rsc_tsc),
982 };
983
984 ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
985 if (ret)
986 goto out;
987 }
988
989 if (key_data.use_tkip) {
990 ret = iwl_mvm_send_cmd_pdu(mvm,
991 WOWLAN_TKIP_PARAM,
992 CMD_SYNC, sizeof(tkip_cmd),
993 &tkip_cmd);
994 if (ret)
995 goto out;
996 }
997
998 if (mvmvif->rekey_data.valid) {
999 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
1000 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1001 NL80211_KCK_LEN);
1002 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
1003 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1004 NL80211_KEK_LEN);
1005 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1006 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1007
1008 ret = iwl_mvm_send_cmd_pdu(mvm,
1009 WOWLAN_KEK_KCK_MATERIAL,
1010 CMD_SYNC,
1011 sizeof(kek_kck_cmd),
1012 &kek_kck_cmd);
1013 if (ret)
1014 goto out;
1015 }
1016 }
1017
Eliad Peller1a95c8d2013-11-21 19:19:52 +02001018 ret = iwl_mvm_send_wowlan_config_cmd(mvm, &wowlan_config_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001019 if (ret)
1020 goto out;
1021
1022 ret = iwl_mvm_send_patterns(mvm, wowlan);
1023 if (ret)
1024 goto out;
1025
Eliad Peller8bd22e72013-11-03 19:48:50 +02001026 ret = iwl_mvm_send_proto_offload(mvm, vif, false, CMD_SYNC);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001027 if (ret)
1028 goto out;
1029
Johannes Bergf0c26462013-01-22 20:41:58 +01001030 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1031 if (ret)
1032 goto out;
1033
Emmanuel Grumbachc1cb92f2014-01-28 10:17:18 +02001034 ret = iwl_mvm_power_update_device(mvm);
Alexander Bondar64b928c2013-09-03 14:18:03 +03001035 if (ret)
1036 goto out;
1037
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +02001038 ret = iwl_mvm_power_update_mac(mvm, vif);
Alexander Bondaree1e8422013-04-23 13:52:10 +03001039 if (ret)
1040 goto out;
1041
Johannes Bergb0114712013-06-12 14:55:40 +02001042#ifdef CONFIG_IWLWIFI_DEBUGFS
1043 if (mvm->d3_wake_sysassert)
1044 d3_cfg_cmd_data.wakeup_flags |=
1045 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1046#endif
1047
Johannes Berg8ca151b2013-01-24 14:25:36 +01001048 /* must be last -- this switches firmware state */
Johannes Bergdebff612013-05-14 13:53:45 +02001049 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001050 if (ret)
1051 goto out;
Johannes Bergdebff612013-05-14 13:53:45 +02001052#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg65b30342014-01-08 13:16:33 +01001053 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1054 if (len >= sizeof(u32)) {
Johannes Bergdebff612013-05-14 13:53:45 +02001055 mvm->d3_test_pme_ptr =
1056 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
Johannes Bergdebff612013-05-14 13:53:45 +02001057 }
1058#endif
1059 iwl_free_resp(&d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001060
1061 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1062
Johannes Bergdebff612013-05-14 13:53:45 +02001063 iwl_trans_d3_suspend(mvm->trans, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001064 out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001065 if (ret < 0)
1066 ieee80211_restart_hw(mvm->hw);
Luciano Coelhob70ab722013-11-19 22:53:41 +02001067 out_noreset:
1068 kfree(key_data.rsc_tsc);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001069
1070 mutex_unlock(&mvm->mutex);
1071
1072 return ret;
1073}
1074
Johannes Bergdebff612013-05-14 13:53:45 +02001075int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1076{
Eliad Pellerd15a7472014-03-27 18:53:12 +02001077 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1078
1079 if (iwl_mvm_is_d0i3_supported(mvm)) {
1080 mutex_lock(&mvm->d0i3_suspend_mutex);
1081 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1082 mutex_unlock(&mvm->d0i3_suspend_mutex);
1083 return 0;
1084 }
1085
Johannes Bergdebff612013-05-14 13:53:45 +02001086 return __iwl_mvm_suspend(hw, wowlan, false);
1087}
1088
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001089/* converted data from the different status responses */
1090struct iwl_wowlan_status_data {
1091 u16 pattern_number;
1092 u16 qos_seq_ctr[8];
1093 u32 wakeup_reasons;
1094 u32 wake_packet_length;
1095 u32 wake_packet_bufsize;
1096 const u8 *wake_packet;
1097};
1098
Johannes Berg7abc4632013-08-06 18:28:26 +02001099static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1100 struct ieee80211_vif *vif,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001101 struct iwl_wowlan_status_data *status)
Johannes Berg9b26b502013-01-22 13:02:09 +01001102{
Johannes Berg7abc4632013-08-06 18:28:26 +02001103 struct sk_buff *pkt = NULL;
Johannes Berg9b26b502013-01-22 13:02:09 +01001104 struct cfg80211_wowlan_wakeup wakeup = {
1105 .pattern_idx = -1,
1106 };
1107 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001108 u32 reasons = status->wakeup_reasons;
Johannes Berg9b26b502013-01-22 13:02:09 +01001109
1110 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1111 wakeup_report = NULL;
1112 goto report;
1113 }
1114
Johannes Bergf444eb12013-02-26 12:04:18 +01001115 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
Johannes Berg9b26b502013-01-22 13:02:09 +01001116 wakeup.magic_pkt = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001117
Johannes Bergf444eb12013-02-26 12:04:18 +01001118 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
Johannes Berg9b26b502013-01-22 13:02:09 +01001119 wakeup.pattern_idx =
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001120 status->pattern_number;
Johannes Berg9b26b502013-01-22 13:02:09 +01001121
1122 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1123 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1124 wakeup.disconnect = true;
1125
Johannes Bergf444eb12013-02-26 12:04:18 +01001126 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001127 wakeup.gtk_rekey_failure = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001128
Johannes Bergf444eb12013-02-26 12:04:18 +01001129 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
Johannes Berg9b26b502013-01-22 13:02:09 +01001130 wakeup.rfkill_release = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001131
Johannes Bergf444eb12013-02-26 12:04:18 +01001132 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
Johannes Berg9b26b502013-01-22 13:02:09 +01001133 wakeup.eap_identity_req = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001134
Johannes Bergf444eb12013-02-26 12:04:18 +01001135 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001136 wakeup.four_way_handshake = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001137
Johannes Bergf0c26462013-01-22 20:41:58 +01001138 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1139 wakeup.tcp_connlost = true;
1140
1141 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1142 wakeup.tcp_nomoretokens = true;
1143
1144 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1145 wakeup.tcp_match = true;
1146
Johannes Berg9b26b502013-01-22 13:02:09 +01001147 if (status->wake_packet_bufsize) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001148 int pktsize = status->wake_packet_bufsize;
1149 int pktlen = status->wake_packet_length;
Johannes Bergf444eb12013-02-26 12:04:18 +01001150 const u8 *pktdata = status->wake_packet;
1151 struct ieee80211_hdr *hdr = (void *)pktdata;
1152 int truncated = pktlen - pktsize;
Johannes Berg9b26b502013-01-22 13:02:09 +01001153
Johannes Bergf444eb12013-02-26 12:04:18 +01001154 /* this would be a firmware bug */
1155 if (WARN_ON_ONCE(truncated < 0))
1156 truncated = 0;
1157
1158 if (ieee80211_is_data(hdr->frame_control)) {
1159 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1160 int ivlen = 0, icvlen = 4; /* also FCS */
1161
Johannes Berg9b26b502013-01-22 13:02:09 +01001162 pkt = alloc_skb(pktsize, GFP_KERNEL);
1163 if (!pkt)
1164 goto report;
Johannes Bergf444eb12013-02-26 12:04:18 +01001165
1166 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1167 pktdata += hdrlen;
1168 pktsize -= hdrlen;
1169
1170 if (ieee80211_has_protected(hdr->frame_control)) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001171 /*
1172 * This is unlocked and using gtk_i(c)vlen,
1173 * but since everything is under RTNL still
1174 * that's not really a problem - changing
1175 * it would be difficult.
1176 */
Johannes Bergf444eb12013-02-26 12:04:18 +01001177 if (is_multicast_ether_addr(hdr->addr1)) {
1178 ivlen = mvm->gtk_ivlen;
1179 icvlen += mvm->gtk_icvlen;
1180 } else {
1181 ivlen = mvm->ptk_ivlen;
1182 icvlen += mvm->ptk_icvlen;
1183 }
1184 }
1185
1186 /* if truncated, FCS/ICV is (partially) gone */
1187 if (truncated >= icvlen) {
1188 icvlen = 0;
1189 truncated -= icvlen;
1190 } else {
1191 icvlen -= truncated;
1192 truncated = 0;
1193 }
1194
1195 pktsize -= ivlen + icvlen;
1196 pktdata += ivlen;
1197
1198 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1199
Johannes Berg9b26b502013-01-22 13:02:09 +01001200 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1201 goto report;
1202 wakeup.packet = pkt->data;
1203 wakeup.packet_present_len = pkt->len;
Johannes Bergf444eb12013-02-26 12:04:18 +01001204 wakeup.packet_len = pkt->len - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001205 wakeup.packet_80211 = false;
1206 } else {
Johannes Bergf444eb12013-02-26 12:04:18 +01001207 int fcslen = 4;
1208
1209 if (truncated >= 4) {
1210 truncated -= 4;
1211 fcslen = 0;
1212 } else {
1213 fcslen -= truncated;
1214 truncated = 0;
1215 }
1216 pktsize -= fcslen;
Johannes Berg9b26b502013-01-22 13:02:09 +01001217 wakeup.packet = status->wake_packet;
1218 wakeup.packet_present_len = pktsize;
Johannes Bergf444eb12013-02-26 12:04:18 +01001219 wakeup.packet_len = pktlen - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001220 wakeup.packet_80211 = true;
1221 }
1222 }
1223
1224 report:
1225 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1226 kfree_skb(pkt);
Johannes Berg7abc4632013-08-06 18:28:26 +02001227}
Johannes Berg9b26b502013-01-22 13:02:09 +01001228
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001229static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1230 struct ieee80211_key_seq *seq)
1231{
1232 u64 pn;
1233
1234 pn = le64_to_cpu(sc->pn);
1235 seq->ccmp.pn[0] = pn >> 40;
1236 seq->ccmp.pn[1] = pn >> 32;
1237 seq->ccmp.pn[2] = pn >> 24;
1238 seq->ccmp.pn[3] = pn >> 16;
1239 seq->ccmp.pn[4] = pn >> 8;
1240 seq->ccmp.pn[5] = pn;
1241}
1242
1243static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1244 struct ieee80211_key_seq *seq)
1245{
1246 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1247 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1248}
1249
1250static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1251 struct ieee80211_key_conf *key)
1252{
1253 int tid;
1254
1255 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1256
1257 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1258 struct ieee80211_key_seq seq = {};
1259
1260 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1261 ieee80211_set_key_rx_seq(key, tid, &seq);
1262 }
1263}
1264
1265static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1266 struct ieee80211_key_conf *key)
1267{
1268 int tid;
1269
1270 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1271
1272 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1273 struct ieee80211_key_seq seq = {};
1274
1275 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1276 ieee80211_set_key_rx_seq(key, tid, &seq);
1277 }
1278}
1279
1280static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001281 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001282{
1283 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1284
1285 switch (key->cipher) {
1286 case WLAN_CIPHER_SUITE_CCMP:
1287 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1288 break;
1289 case WLAN_CIPHER_SUITE_TKIP:
1290 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1291 break;
1292 default:
1293 WARN_ON(1);
1294 }
1295}
1296
1297struct iwl_mvm_d3_gtk_iter_data {
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001298 struct iwl_wowlan_status *status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001299 void *last_gtk;
1300 u32 cipher;
1301 bool find_phase, unhandled_cipher;
1302 int num_keys;
1303};
1304
1305static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
1306 struct ieee80211_vif *vif,
1307 struct ieee80211_sta *sta,
1308 struct ieee80211_key_conf *key,
1309 void *_data)
1310{
1311 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1312
1313 if (data->unhandled_cipher)
1314 return;
1315
1316 switch (key->cipher) {
1317 case WLAN_CIPHER_SUITE_WEP40:
1318 case WLAN_CIPHER_SUITE_WEP104:
1319 /* ignore WEP completely, nothing to do */
1320 return;
1321 case WLAN_CIPHER_SUITE_CCMP:
1322 case WLAN_CIPHER_SUITE_TKIP:
1323 /* we support these */
1324 break;
1325 default:
1326 /* everything else (even CMAC for MFP) - disconnect from AP */
1327 data->unhandled_cipher = true;
1328 return;
1329 }
1330
1331 data->num_keys++;
1332
1333 /*
1334 * pairwise key - update sequence counters only;
1335 * note that this assumes no TDLS sessions are active
1336 */
1337 if (sta) {
1338 struct ieee80211_key_seq seq = {};
1339 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1340
1341 if (data->find_phase)
1342 return;
1343
1344 switch (key->cipher) {
1345 case WLAN_CIPHER_SUITE_CCMP:
1346 iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
1347 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
1348 break;
1349 case WLAN_CIPHER_SUITE_TKIP:
1350 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1351 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1352 break;
1353 }
1354 ieee80211_set_key_tx_seq(key, &seq);
1355
1356 /* that's it for this key */
1357 return;
1358 }
1359
1360 if (data->find_phase) {
1361 data->last_gtk = key;
1362 data->cipher = key->cipher;
1363 return;
1364 }
1365
1366 if (data->status->num_of_gtk_rekeys)
1367 ieee80211_remove_key(key);
1368 else if (data->last_gtk == key)
1369 iwl_mvm_set_key_rx_seq(key, data->status);
1370}
1371
1372static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1373 struct ieee80211_vif *vif,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001374 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001375{
1376 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1377 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1378 .status = status,
1379 };
Eliad Peller431591c2013-11-07 14:12:09 +02001380 u32 disconnection_reasons =
1381 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1382 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001383
1384 if (!status || !vif->bss_conf.bssid)
1385 return false;
1386
Eliad Peller431591c2013-11-07 14:12:09 +02001387 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1388 return false;
1389
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001390 /* find last GTK that we used initially, if any */
1391 gtkdata.find_phase = true;
1392 ieee80211_iter_keys(mvm->hw, vif,
1393 iwl_mvm_d3_update_gtks, &gtkdata);
1394 /* not trying to keep connections with MFP/unhandled ciphers */
1395 if (gtkdata.unhandled_cipher)
1396 return false;
1397 if (!gtkdata.num_keys)
Luciano Coelhocfa88892013-11-19 15:38:55 +02001398 goto out;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001399 if (!gtkdata.last_gtk)
1400 return false;
1401
1402 /*
1403 * invalidate all other GTKs that might still exist and update
1404 * the one that we used
1405 */
1406 gtkdata.find_phase = false;
1407 ieee80211_iter_keys(mvm->hw, vif,
1408 iwl_mvm_d3_update_gtks, &gtkdata);
1409
1410 if (status->num_of_gtk_rekeys) {
1411 struct ieee80211_key_conf *key;
1412 struct {
1413 struct ieee80211_key_conf conf;
1414 u8 key[32];
1415 } conf = {
1416 .conf.cipher = gtkdata.cipher,
1417 .conf.keyidx = status->gtk.key_index,
1418 };
1419
1420 switch (gtkdata.cipher) {
1421 case WLAN_CIPHER_SUITE_CCMP:
1422 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1423 memcpy(conf.conf.key, status->gtk.decrypt_key,
1424 WLAN_KEY_LEN_CCMP);
1425 break;
1426 case WLAN_CIPHER_SUITE_TKIP:
1427 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1428 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1429 /* leave TX MIC key zeroed, we don't use it anyway */
1430 memcpy(conf.conf.key +
1431 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1432 status->gtk.tkip_mic_key, 8);
1433 break;
1434 }
1435
1436 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1437 if (IS_ERR(key))
1438 return false;
1439 iwl_mvm_set_key_rx_seq(key, status);
1440 }
1441
1442 if (status->num_of_gtk_rekeys) {
1443 __be64 replay_ctr =
1444 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1445 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1446 (void *)&replay_ctr, GFP_KERNEL);
1447 }
1448
Luciano Coelhocfa88892013-11-19 15:38:55 +02001449out:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001450 mvmvif->seqno_valid = true;
1451 /* +0x10 because the set API expects next-to-use, not last-used */
1452 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1453
1454 return true;
1455}
1456
Johannes Berg7abc4632013-08-06 18:28:26 +02001457/* releases the MVM mutex */
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001458static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
Johannes Berg7abc4632013-08-06 18:28:26 +02001459 struct ieee80211_vif *vif)
1460{
1461 u32 base = mvm->error_event_table;
1462 struct error_table_start {
1463 /* cf. struct iwl_error_event_table */
1464 u32 valid;
1465 u32 error_id;
1466 } err_info;
1467 struct iwl_host_cmd cmd = {
1468 .id = WOWLAN_GET_STATUSES,
1469 .flags = CMD_SYNC | CMD_WANT_SKB,
1470 };
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001471 struct iwl_wowlan_status_data status;
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001472 struct iwl_wowlan_status *fw_status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001473 int ret, len, status_size, i;
1474 bool keep;
1475 struct ieee80211_sta *ap_sta;
1476 struct iwl_mvm_sta *mvm_ap_sta;
Johannes Berg7abc4632013-08-06 18:28:26 +02001477
1478 iwl_trans_read_mem_bytes(mvm->trans, base,
1479 &err_info, sizeof(err_info));
1480
1481 if (err_info.valid) {
1482 IWL_INFO(mvm, "error table is valid (%d)\n",
1483 err_info.valid);
1484 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1485 struct cfg80211_wowlan_wakeup wakeup = {
1486 .rfkill_release = true,
1487 };
1488 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1489 GFP_KERNEL);
1490 }
1491 goto out_unlock;
1492 }
1493
1494 /* only for tracing for now */
1495 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, CMD_SYNC, 0, NULL);
1496 if (ret)
1497 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1498
1499 ret = iwl_mvm_send_cmd(mvm, &cmd);
1500 if (ret) {
1501 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1502 goto out_unlock;
1503 }
1504
1505 /* RF-kill already asserted again... */
1506 if (!cmd.resp_pkt)
1507 goto out_unlock;
1508
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001509 status_size = sizeof(*fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001510
Johannes Berg65b30342014-01-08 13:16:33 +01001511 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1512 if (len < status_size) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001513 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1514 goto out_free_resp;
1515 }
1516
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001517 fw_status = (void *)cmd.resp_pkt->data;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001518
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001519 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1520 for (i = 0; i < 8; i++)
1521 status.qos_seq_ctr[i] =
1522 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1523 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1524 status.wake_packet_length =
1525 le32_to_cpu(fw_status->wake_packet_length);
1526 status.wake_packet_bufsize =
1527 le32_to_cpu(fw_status->wake_packet_bufsize);
1528 status.wake_packet = fw_status->wake_packet;
Johannes Berg7abc4632013-08-06 18:28:26 +02001529
Johannes Berg65b30342014-01-08 13:16:33 +01001530 if (len != status_size + ALIGN(status.wake_packet_bufsize, 4)) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001531 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1532 goto out_free_resp;
1533 }
1534
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001535 /* still at hard-coded place 0 for D3 image */
1536 ap_sta = rcu_dereference_protected(
1537 mvm->fw_id_to_mac_id[0],
1538 lockdep_is_held(&mvm->mutex));
1539 if (IS_ERR_OR_NULL(ap_sta))
1540 goto out_free_resp;
1541
1542 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
1543 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1544 u16 seq = status.qos_seq_ctr[i];
1545 /* firmware stores last-used value, we store next value */
1546 seq += 0x10;
1547 mvm_ap_sta->tid_data[i].seq_number = seq;
1548 }
1549
Johannes Berg7abc4632013-08-06 18:28:26 +02001550 /* now we have all the data we need, unlock to avoid mac80211 issues */
1551 mutex_unlock(&mvm->mutex);
1552
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001553 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1554
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001555 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001556
Johannes Berg9b26b502013-01-22 13:02:09 +01001557 iwl_free_resp(&cmd);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001558 return keep;
Johannes Berg7abc4632013-08-06 18:28:26 +02001559
1560 out_free_resp:
1561 iwl_free_resp(&cmd);
1562 out_unlock:
1563 mutex_unlock(&mvm->mutex);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001564 return false;
Johannes Berg9b26b502013-01-22 13:02:09 +01001565}
1566
Johannes Bergafc66bb2013-05-03 11:44:16 +02001567static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1568{
1569#ifdef CONFIG_IWLWIFI_DEBUGFS
1570 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1571 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1572 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1573
1574 if (!mvm->store_d3_resume_sram)
1575 return;
1576
1577 if (!mvm->d3_resume_sram) {
1578 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1579 if (!mvm->d3_resume_sram)
1580 return;
1581 }
1582
1583 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1584#endif
1585}
1586
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001587static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1588 struct ieee80211_vif *vif)
1589{
1590 /* skip the one we keep connection on */
1591 if (data == vif)
1592 return;
1593
1594 if (vif->type == NL80211_IFTYPE_STATION)
1595 ieee80211_resume_disconnect(vif);
1596}
1597
Johannes Bergdebff612013-05-14 13:53:45 +02001598static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001599{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001600 struct iwl_d3_iter_data resume_iter_data = {
1601 .mvm = mvm,
1602 };
1603 struct ieee80211_vif *vif = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001604 int ret;
1605 enum iwl_d3_status d3_status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001606 bool keep = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001607
1608 mutex_lock(&mvm->mutex);
1609
1610 /* get the BSS vif pointer again */
1611 ieee80211_iterate_active_interfaces_atomic(
1612 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1613 iwl_mvm_d3_iface_iterator, &resume_iter_data);
1614
1615 if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
1616 goto out_unlock;
1617
1618 vif = resume_iter_data.vif;
1619
Johannes Bergdebff612013-05-14 13:53:45 +02001620 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001621 if (ret)
1622 goto out_unlock;
1623
1624 if (d3_status != IWL_D3_STATUS_ALIVE) {
1625 IWL_INFO(mvm, "Device was reset during suspend\n");
1626 goto out_unlock;
1627 }
1628
Johannes Bergafc66bb2013-05-03 11:44:16 +02001629 /* query SRAM first in case we want event logging */
1630 iwl_mvm_read_d3_sram(mvm);
1631
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001632 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
Eliad Peller78c9df62013-11-07 14:13:30 +02001633#ifdef CONFIG_IWLWIFI_DEBUGFS
1634 if (keep)
1635 mvm->keep_vif = vif;
1636#endif
Johannes Berg7abc4632013-08-06 18:28:26 +02001637 /* has unlocked the mutex, so skip that */
1638 goto out;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001639
1640 out_unlock:
1641 mutex_unlock(&mvm->mutex);
1642
Johannes Berg7abc4632013-08-06 18:28:26 +02001643 out:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001644 if (!test)
1645 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1646 IEEE80211_IFACE_ITER_NORMAL,
1647 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001648
1649 /* return 1 to reconfigure the device */
1650 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1651 return 1;
1652}
1653
Johannes Bergdebff612013-05-14 13:53:45 +02001654int iwl_mvm_resume(struct ieee80211_hw *hw)
1655{
1656 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1657
Eliad Pellerd15a7472014-03-27 18:53:12 +02001658 if (iwl_mvm_is_d0i3_supported(mvm)) {
1659 bool exit_now;
1660
1661 mutex_lock(&mvm->d0i3_suspend_mutex);
1662 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1663 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1664 &mvm->d0i3_suspend_flags);
1665 mutex_unlock(&mvm->d0i3_suspend_mutex);
1666 if (exit_now)
1667 _iwl_mvm_exit_d0i3(mvm);
1668 return 0;
1669 }
1670
Johannes Bergdebff612013-05-14 13:53:45 +02001671 return __iwl_mvm_resume(mvm, false);
1672}
1673
Johannes Berg8ca151b2013-01-24 14:25:36 +01001674void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1675{
1676 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1677
1678 device_set_wakeup_enable(mvm->trans->dev, enabled);
1679}
Johannes Bergdebff612013-05-14 13:53:45 +02001680
1681#ifdef CONFIG_IWLWIFI_DEBUGFS
1682static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
1683{
1684 struct iwl_mvm *mvm = inode->i_private;
1685 int err;
1686
1687 if (mvm->d3_test_active)
1688 return -EBUSY;
1689
1690 file->private_data = inode->i_private;
1691
1692 ieee80211_stop_queues(mvm->hw);
1693 synchronize_net();
1694
1695 /* start pseudo D3 */
1696 rtnl_lock();
1697 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
1698 rtnl_unlock();
1699 if (err > 0)
1700 err = -EINVAL;
1701 if (err) {
1702 ieee80211_wake_queues(mvm->hw);
1703 return err;
1704 }
1705 mvm->d3_test_active = true;
Eliad Peller78c9df62013-11-07 14:13:30 +02001706 mvm->keep_vif = NULL;
Johannes Bergdebff612013-05-14 13:53:45 +02001707 return 0;
1708}
1709
1710static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
1711 size_t count, loff_t *ppos)
1712{
1713 struct iwl_mvm *mvm = file->private_data;
1714 u32 pme_asserted;
1715
1716 while (true) {
Eliad Peller4ed80a82013-11-07 13:23:21 +02001717 /* read pme_ptr if available */
1718 if (mvm->d3_test_pme_ptr) {
1719 pme_asserted = iwl_trans_read_mem32(mvm->trans,
1720 mvm->d3_test_pme_ptr);
1721 if (pme_asserted)
1722 break;
1723 }
1724
Johannes Bergdebff612013-05-14 13:53:45 +02001725 if (msleep_interruptible(100))
1726 break;
1727 }
1728
1729 return 0;
1730}
1731
1732static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
1733 struct ieee80211_vif *vif)
1734{
Eliad Peller78c9df62013-11-07 14:13:30 +02001735 /* skip the one we keep connection on */
1736 if (_data == vif)
1737 return;
1738
Johannes Bergdebff612013-05-14 13:53:45 +02001739 if (vif->type == NL80211_IFTYPE_STATION)
1740 ieee80211_connection_loss(vif);
1741}
1742
1743static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
1744{
1745 struct iwl_mvm *mvm = inode->i_private;
1746 int remaining_time = 10;
1747
1748 mvm->d3_test_active = false;
1749 __iwl_mvm_resume(mvm, true);
1750 iwl_abort_notification_waits(&mvm->notif_wait);
1751 ieee80211_restart_hw(mvm->hw);
1752
1753 /* wait for restart and disconnect all interfaces */
1754 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1755 remaining_time > 0) {
1756 remaining_time--;
1757 msleep(1000);
1758 }
1759
1760 if (remaining_time == 0)
1761 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
1762
1763 ieee80211_iterate_active_interfaces_atomic(
1764 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
Eliad Peller78c9df62013-11-07 14:13:30 +02001765 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
Johannes Bergdebff612013-05-14 13:53:45 +02001766
1767 ieee80211_wake_queues(mvm->hw);
1768
1769 return 0;
1770}
1771
1772const struct file_operations iwl_dbgfs_d3_test_ops = {
1773 .llseek = no_llseek,
1774 .open = iwl_mvm_d3_test_open,
1775 .read = iwl_mvm_d3_test_read,
1776 .release = iwl_mvm_d3_test_release,
1777};
1778#endif