blob: 7a2ef3f013fdcf06a51e204bb356894016ba89cc [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 *
8 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
9 *
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 *
33 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
34 * 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 Berg8ca151b2013-01-24 14:25:36 +010070#include "iwl-modparams.h"
71#include "fw-api.h"
72#include "mvm.h"
73
74void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
75 struct ieee80211_vif *vif,
76 struct cfg80211_gtk_rekey_data *data)
77{
78 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
79 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
80
81 if (iwlwifi_mod_params.sw_crypto)
82 return;
83
84 mutex_lock(&mvm->mutex);
85
86 memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
87 memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
88 mvmvif->rekey_data.replay_ctr =
89 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
90 mvmvif->rekey_data.valid = true;
91
92 mutex_unlock(&mvm->mutex);
93}
94
95#if IS_ENABLED(CONFIG_IPV6)
96void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
97 struct ieee80211_vif *vif,
98 struct inet6_dev *idev)
99{
100 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
101 struct inet6_ifaddr *ifa;
102 int idx = 0;
103
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200104 read_lock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100105 list_for_each_entry(ifa, &idev->addr_list, if_list) {
106 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
107 idx++;
108 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS)
109 break;
110 }
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200111 read_unlock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100112
113 mvmvif->num_target_ipv6_addrs = idx;
114}
115#endif
116
117void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
118 struct ieee80211_vif *vif, int idx)
119{
120 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
121
122 mvmvif->tx_key_idx = idx;
123}
124
125static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
126{
127 int i;
128
129 for (i = 0; i < IWL_P1K_SIZE; i++)
130 out[i] = cpu_to_le16(p1k[i]);
131}
132
133struct wowlan_key_data {
134 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
135 struct iwl_wowlan_tkip_params_cmd *tkip;
136 bool error, use_rsc_tsc, use_tkip;
137 int gtk_key_idx;
138};
139
140static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
141 struct ieee80211_vif *vif,
142 struct ieee80211_sta *sta,
143 struct ieee80211_key_conf *key,
144 void *_data)
145{
146 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
147 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
148 struct wowlan_key_data *data = _data;
149 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
150 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
151 struct iwl_p1k_cache *rx_p1ks;
152 u8 *rx_mic_key;
153 struct ieee80211_key_seq seq;
154 u32 cur_rx_iv32 = 0;
155 u16 p1k[IWL_P1K_SIZE];
156 int ret, i;
157
158 mutex_lock(&mvm->mutex);
159
160 switch (key->cipher) {
161 case WLAN_CIPHER_SUITE_WEP40:
162 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
163 struct {
164 struct iwl_mvm_wep_key_cmd wep_key_cmd;
165 struct iwl_mvm_wep_key wep_key;
166 } __packed wkc = {
167 .wep_key_cmd.mac_id_n_color =
168 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
169 mvmvif->color)),
170 .wep_key_cmd.num_keys = 1,
171 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
172 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
173 .wep_key.key_index = key->keyidx,
174 .wep_key.key_size = key->keylen,
175 };
176
177 /*
178 * This will fail -- the key functions don't set support
179 * pairwise WEP keys. However, that's better than silently
180 * failing WoWLAN. Or maybe not?
181 */
182 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
183 break;
184
185 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
186 if (key->keyidx == mvmvif->tx_key_idx) {
187 /* TX key must be at offset 0 */
188 wkc.wep_key.key_offset = 0;
189 } else {
190 /* others start at 1 */
191 data->gtk_key_idx++;
192 wkc.wep_key.key_offset = data->gtk_key_idx;
193 }
194
195 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, CMD_SYNC,
196 sizeof(wkc), &wkc);
197 data->error = ret != 0;
198
Johannes Bergf444eb12013-02-26 12:04:18 +0100199 mvm->ptk_ivlen = key->iv_len;
200 mvm->ptk_icvlen = key->icv_len;
201 mvm->gtk_ivlen = key->iv_len;
202 mvm->gtk_icvlen = key->icv_len;
203
Johannes Berg8ca151b2013-01-24 14:25:36 +0100204 /* don't upload key again */
205 goto out_unlock;
206 }
207 default:
208 data->error = true;
209 goto out_unlock;
210 case WLAN_CIPHER_SUITE_AES_CMAC:
211 /*
212 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
213 * but we also shouldn't abort suspend due to that. It does have
214 * support for the IGTK key renewal, but doesn't really use the
215 * IGTK for anything. This means we could spuriously wake up or
216 * be deauthenticated, but that was considered acceptable.
217 */
218 goto out_unlock;
219 case WLAN_CIPHER_SUITE_TKIP:
220 if (sta) {
221 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
222 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
223
224 rx_p1ks = data->tkip->rx_uni;
225
226 ieee80211_get_key_tx_seq(key, &seq);
227 tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
228 tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
229
230 ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
231 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
232
233 memcpy(data->tkip->mic_keys.tx,
234 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
235 IWL_MIC_KEY_SIZE);
236
237 rx_mic_key = data->tkip->mic_keys.rx_unicast;
238 } else {
239 tkip_sc =
240 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
241 rx_p1ks = data->tkip->rx_multi;
242 rx_mic_key = data->tkip->mic_keys.rx_mcast;
243 }
244
245 /*
246 * For non-QoS this relies on the fact that both the uCode and
247 * mac80211 use TID 0 (as they need to to avoid replay attacks)
248 * for checking the IV in the frames.
249 */
250 for (i = 0; i < IWL_NUM_RSC; i++) {
251 ieee80211_get_key_rx_seq(key, i, &seq);
252 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
253 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
254 /* wrapping isn't allowed, AP must rekey */
255 if (seq.tkip.iv32 > cur_rx_iv32)
256 cur_rx_iv32 = seq.tkip.iv32;
257 }
258
259 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
260 cur_rx_iv32, p1k);
261 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
262 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
263 cur_rx_iv32 + 1, p1k);
264 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
265
266 memcpy(rx_mic_key,
267 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
268 IWL_MIC_KEY_SIZE);
269
270 data->use_tkip = true;
271 data->use_rsc_tsc = true;
272 break;
273 case WLAN_CIPHER_SUITE_CCMP:
274 if (sta) {
275 u8 *pn = seq.ccmp.pn;
276
277 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
278 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
279
280 ieee80211_get_key_tx_seq(key, &seq);
281 aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
282 ((u64)pn[4] << 8) |
283 ((u64)pn[3] << 16) |
284 ((u64)pn[2] << 24) |
285 ((u64)pn[1] << 32) |
286 ((u64)pn[0] << 40));
287 } else {
288 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
289 }
290
291 /*
292 * For non-QoS this relies on the fact that both the uCode and
293 * mac80211 use TID 0 for checking the IV in the frames.
294 */
295 for (i = 0; i < IWL_NUM_RSC; i++) {
296 u8 *pn = seq.ccmp.pn;
297
298 ieee80211_get_key_rx_seq(key, i, &seq);
299 aes_sc->pn = cpu_to_le64((u64)pn[5] |
300 ((u64)pn[4] << 8) |
301 ((u64)pn[3] << 16) |
302 ((u64)pn[2] << 24) |
303 ((u64)pn[1] << 32) |
304 ((u64)pn[0] << 40));
305 }
306 data->use_rsc_tsc = true;
307 break;
308 }
309
310 /*
311 * The D3 firmware hardcodes the key offset 0 as the key it uses
312 * to transmit packets to the AP, i.e. the PTK.
313 */
314 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
315 key->hw_key_idx = 0;
Johannes Bergf444eb12013-02-26 12:04:18 +0100316 mvm->ptk_ivlen = key->iv_len;
317 mvm->ptk_icvlen = key->icv_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100318 } else {
319 data->gtk_key_idx++;
320 key->hw_key_idx = data->gtk_key_idx;
Johannes Bergf444eb12013-02-26 12:04:18 +0100321 mvm->gtk_ivlen = key->iv_len;
322 mvm->gtk_icvlen = key->icv_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100323 }
324
325 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
326 data->error = ret != 0;
327out_unlock:
328 mutex_unlock(&mvm->mutex);
329}
330
331static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
332 struct cfg80211_wowlan *wowlan)
333{
334 struct iwl_wowlan_patterns_cmd *pattern_cmd;
335 struct iwl_host_cmd cmd = {
336 .id = WOWLAN_PATTERNS,
337 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
338 .flags = CMD_SYNC,
339 };
340 int i, err;
341
342 if (!wowlan->n_patterns)
343 return 0;
344
345 cmd.len[0] = sizeof(*pattern_cmd) +
346 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
347
348 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
349 if (!pattern_cmd)
350 return -ENOMEM;
351
352 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
353
354 for (i = 0; i < wowlan->n_patterns; i++) {
355 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
356
357 memcpy(&pattern_cmd->patterns[i].mask,
358 wowlan->patterns[i].mask, mask_len);
359 memcpy(&pattern_cmd->patterns[i].pattern,
360 wowlan->patterns[i].pattern,
361 wowlan->patterns[i].pattern_len);
362 pattern_cmd->patterns[i].mask_size = mask_len;
363 pattern_cmd->patterns[i].pattern_size =
364 wowlan->patterns[i].pattern_len;
365 }
366
367 cmd.data[0] = pattern_cmd;
368 err = iwl_mvm_send_cmd(mvm, &cmd);
369 kfree(pattern_cmd);
370 return err;
371}
372
373static int iwl_mvm_send_proto_offload(struct iwl_mvm *mvm,
374 struct ieee80211_vif *vif)
375{
376 struct iwl_proto_offload_cmd cmd = {};
377#if IS_ENABLED(CONFIG_IPV6)
378 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
379 int i;
380
381 if (mvmvif->num_target_ipv6_addrs) {
382 cmd.enabled |= cpu_to_le32(IWL_D3_PROTO_OFFLOAD_NS);
383 memcpy(cmd.ndp_mac_addr, vif->addr, ETH_ALEN);
384 }
385
386 BUILD_BUG_ON(sizeof(cmd.target_ipv6_addr[i]) !=
387 sizeof(mvmvif->target_ipv6_addrs[i]));
388
389 for (i = 0; i < mvmvif->num_target_ipv6_addrs; i++)
390 memcpy(cmd.target_ipv6_addr[i],
391 &mvmvif->target_ipv6_addrs[i],
392 sizeof(cmd.target_ipv6_addr[i]));
393#endif
394
395 if (vif->bss_conf.arp_addr_cnt) {
396 cmd.enabled |= cpu_to_le32(IWL_D3_PROTO_OFFLOAD_ARP);
397 cmd.host_ipv4_addr = vif->bss_conf.arp_addr_list[0];
398 memcpy(cmd.arp_mac_addr, vif->addr, ETH_ALEN);
399 }
400
401 if (!cmd.enabled)
402 return 0;
403
404 return iwl_mvm_send_cmd_pdu(mvm, PROT_OFFLOAD_CONFIG_CMD, CMD_SYNC,
405 sizeof(cmd), &cmd);
406}
407
Johannes Bergf0c26462013-01-22 20:41:58 +0100408enum iwl_mvm_tcp_packet_type {
409 MVM_TCP_TX_SYN,
410 MVM_TCP_RX_SYNACK,
411 MVM_TCP_TX_DATA,
412 MVM_TCP_RX_ACK,
413 MVM_TCP_RX_WAKE,
414 MVM_TCP_TX_FIN,
415};
416
417static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
418{
419 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
420 return cpu_to_le16(be16_to_cpu((__force __be16)check));
421}
422
423static void iwl_mvm_build_tcp_packet(struct iwl_mvm *mvm,
424 struct ieee80211_vif *vif,
425 struct cfg80211_wowlan_tcp *tcp,
426 void *_pkt, u8 *mask,
427 __le16 *pseudo_hdr_csum,
428 enum iwl_mvm_tcp_packet_type ptype)
429{
430 struct {
431 struct ethhdr eth;
432 struct iphdr ip;
433 struct tcphdr tcp;
434 u8 data[];
435 } __packed *pkt = _pkt;
436 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
437 int i;
438
439 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
440 pkt->ip.version = 4;
441 pkt->ip.ihl = 5;
442 pkt->ip.protocol = IPPROTO_TCP;
443
444 switch (ptype) {
445 case MVM_TCP_TX_SYN:
446 case MVM_TCP_TX_DATA:
447 case MVM_TCP_TX_FIN:
448 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
449 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
450 pkt->ip.ttl = 128;
451 pkt->ip.saddr = tcp->src;
452 pkt->ip.daddr = tcp->dst;
453 pkt->tcp.source = cpu_to_be16(tcp->src_port);
454 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
455 /* overwritten for TX SYN later */
456 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
457 pkt->tcp.window = cpu_to_be16(65000);
458 break;
459 case MVM_TCP_RX_SYNACK:
460 case MVM_TCP_RX_ACK:
461 case MVM_TCP_RX_WAKE:
462 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
463 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
464 pkt->ip.saddr = tcp->dst;
465 pkt->ip.daddr = tcp->src;
466 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
467 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
468 break;
469 default:
470 WARN_ON(1);
471 return;
472 }
473
474 switch (ptype) {
475 case MVM_TCP_TX_SYN:
476 /* firmware assumes 8 option bytes - 8 NOPs for now */
477 memset(pkt->data, 0x01, 8);
478 ip_tot_len += 8;
479 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
480 pkt->tcp.syn = 1;
481 break;
482 case MVM_TCP_TX_DATA:
483 ip_tot_len += tcp->payload_len;
484 memcpy(pkt->data, tcp->payload, tcp->payload_len);
485 pkt->tcp.psh = 1;
486 pkt->tcp.ack = 1;
487 break;
488 case MVM_TCP_TX_FIN:
489 pkt->tcp.fin = 1;
490 pkt->tcp.ack = 1;
491 break;
492 case MVM_TCP_RX_SYNACK:
493 pkt->tcp.syn = 1;
494 pkt->tcp.ack = 1;
495 break;
496 case MVM_TCP_RX_ACK:
497 pkt->tcp.ack = 1;
498 break;
499 case MVM_TCP_RX_WAKE:
500 ip_tot_len += tcp->wake_len;
501 pkt->tcp.psh = 1;
502 pkt->tcp.ack = 1;
503 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
504 break;
505 }
506
507 switch (ptype) {
508 case MVM_TCP_TX_SYN:
509 case MVM_TCP_TX_DATA:
510 case MVM_TCP_TX_FIN:
511 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
512 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
513 break;
514 case MVM_TCP_RX_WAKE:
515 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
516 u8 tmp = tcp->wake_mask[i];
517 mask[i + 6] |= tmp << 6;
518 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
519 mask[i + 7] = tmp >> 2;
520 }
521 /* fall through for ethernet/IP/TCP headers mask */
522 case MVM_TCP_RX_SYNACK:
523 case MVM_TCP_RX_ACK:
524 mask[0] = 0xff; /* match ethernet */
525 /*
526 * match ethernet, ip.version, ip.ihl
527 * the ip.ihl half byte is really masked out by firmware
528 */
529 mask[1] = 0x7f;
530 mask[2] = 0x80; /* match ip.protocol */
531 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
532 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
533 mask[5] = 0x80; /* match tcp flags */
534 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
535 break;
536 };
537
538 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
539 pkt->ip.saddr, pkt->ip.daddr);
540}
541
542static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
543 struct ieee80211_vif *vif,
544 struct cfg80211_wowlan_tcp *tcp)
545{
546 struct iwl_wowlan_remote_wake_config *cfg;
547 struct iwl_host_cmd cmd = {
548 .id = REMOTE_WAKE_CONFIG_CMD,
549 .len = { sizeof(*cfg), },
550 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
551 .flags = CMD_SYNC,
552 };
553 int ret;
554
555 if (!tcp)
556 return 0;
557
558 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
559 if (!cfg)
560 return -ENOMEM;
561 cmd.data[0] = cfg;
562
563 cfg->max_syn_retries = 10;
564 cfg->max_data_retries = 10;
565 cfg->tcp_syn_ack_timeout = 1; /* seconds */
566 cfg->tcp_ack_timeout = 1; /* seconds */
567
568 /* SYN (TX) */
569 iwl_mvm_build_tcp_packet(
570 mvm, vif, tcp, cfg->syn_tx.data, NULL,
571 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
572 MVM_TCP_TX_SYN);
573 cfg->syn_tx.info.tcp_payload_length = 0;
574
575 /* SYN/ACK (RX) */
576 iwl_mvm_build_tcp_packet(
577 mvm, vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
578 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
579 MVM_TCP_RX_SYNACK);
580 cfg->synack_rx.info.tcp_payload_length = 0;
581
582 /* KEEPALIVE/ACK (TX) */
583 iwl_mvm_build_tcp_packet(
584 mvm, vif, tcp, cfg->keepalive_tx.data, NULL,
585 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
586 MVM_TCP_TX_DATA);
587 cfg->keepalive_tx.info.tcp_payload_length =
588 cpu_to_le16(tcp->payload_len);
589 cfg->sequence_number_offset = tcp->payload_seq.offset;
590 /* length must be 0..4, the field is little endian */
591 cfg->sequence_number_length = tcp->payload_seq.len;
592 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
593 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
594 if (tcp->payload_tok.len) {
595 cfg->token_offset = tcp->payload_tok.offset;
596 cfg->token_length = tcp->payload_tok.len;
597 cfg->num_tokens =
598 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
599 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
600 tcp->tokens_size);
601 } else {
602 /* set tokens to max value to almost never run out */
603 cfg->num_tokens = cpu_to_le16(65535);
604 }
605
606 /* ACK (RX) */
607 iwl_mvm_build_tcp_packet(
608 mvm, vif, tcp, cfg->keepalive_ack_rx.data,
609 cfg->keepalive_ack_rx.rx_mask,
610 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
611 MVM_TCP_RX_ACK);
612 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
613
614 /* WAKEUP (RX) */
615 iwl_mvm_build_tcp_packet(
616 mvm, vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
617 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
618 MVM_TCP_RX_WAKE);
619 cfg->wake_rx.info.tcp_payload_length =
620 cpu_to_le16(tcp->wake_len);
621
622 /* FIN */
623 iwl_mvm_build_tcp_packet(
624 mvm, vif, tcp, cfg->fin_tx.data, NULL,
625 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
626 MVM_TCP_TX_FIN);
627 cfg->fin_tx.info.tcp_payload_length = 0;
628
629 ret = iwl_mvm_send_cmd(mvm, &cmd);
630 kfree(cfg);
631
632 return ret;
633}
634
Johannes Berg8ca151b2013-01-24 14:25:36 +0100635struct iwl_d3_iter_data {
636 struct iwl_mvm *mvm;
637 struct ieee80211_vif *vif;
638 bool error;
639};
640
641static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
642 struct ieee80211_vif *vif)
643{
644 struct iwl_d3_iter_data *data = _data;
645 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
646
647 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
648 return;
649
650 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
651 return;
652
653 if (data->vif) {
654 IWL_ERR(data->mvm, "More than one managed interface active!\n");
655 data->error = true;
656 return;
657 }
658
659 data->vif = vif;
660}
661
662static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
663 struct ieee80211_sta *ap_sta)
664{
665 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
666 struct ieee80211_chanctx_conf *ctx;
667 u8 chains_static, chains_dynamic;
668 struct cfg80211_chan_def chandef;
669 int ret, i;
670 struct iwl_binding_cmd binding_cmd = {};
671 struct iwl_time_quota_cmd quota_cmd = {};
672 u32 status;
673
674 /* add back the PHY */
675 if (WARN_ON(!mvmvif->phy_ctxt))
676 return -EINVAL;
677
678 rcu_read_lock();
679 ctx = rcu_dereference(vif->chanctx_conf);
680 if (WARN_ON(!ctx)) {
681 rcu_read_unlock();
682 return -EINVAL;
683 }
684 chandef = ctx->def;
685 chains_static = ctx->rx_chains_static;
686 chains_dynamic = ctx->rx_chains_dynamic;
687 rcu_read_unlock();
688
689 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
690 chains_static, chains_dynamic);
691 if (ret)
692 return ret;
693
694 /* add back the MAC */
695 mvmvif->uploaded = false;
696
697 if (WARN_ON(!vif->bss_conf.assoc))
698 return -EINVAL;
699 /* hack */
700 vif->bss_conf.assoc = false;
701 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
702 vif->bss_conf.assoc = true;
703 if (ret)
704 return ret;
705
706 /* add back binding - XXX refactor? */
707 binding_cmd.id_and_color =
708 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
709 mvmvif->phy_ctxt->color));
710 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
711 binding_cmd.phy =
712 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
713 mvmvif->phy_ctxt->color));
714 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
715 mvmvif->color));
716 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
717 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
718
719 status = 0;
720 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
721 sizeof(binding_cmd), &binding_cmd,
722 &status);
723 if (ret) {
724 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
725 return ret;
726 }
727
728 if (status) {
729 IWL_ERR(mvm, "Binding command failed: %u\n", status);
730 return -EIO;
731 }
732
Johannes Berg7a453972013-02-12 13:10:44 +0100733 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100734 if (ret)
735 return ret;
736 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
737
738 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
739 if (ret)
740 return ret;
741
742 /* and some quota */
743 quota_cmd.quotas[0].id_and_color =
744 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
745 mvmvif->phy_ctxt->color));
746 quota_cmd.quotas[0].quota = cpu_to_le32(100);
747 quota_cmd.quotas[0].max_duration = cpu_to_le32(1000);
748
749 for (i = 1; i < MAX_BINDINGS; i++)
750 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
751
752 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, CMD_SYNC,
753 sizeof(quota_cmd), &quota_cmd);
754 if (ret)
755 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
756
757 return 0;
758}
759
Johannes Bergdebff612013-05-14 13:53:45 +0200760static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
761 struct cfg80211_wowlan *wowlan,
762 bool test)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100763{
764 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
765 struct iwl_d3_iter_data suspend_iter_data = {
766 .mvm = mvm,
767 };
768 struct ieee80211_vif *vif;
769 struct iwl_mvm_vif *mvmvif;
770 struct ieee80211_sta *ap_sta;
771 struct iwl_mvm_sta *mvm_ap_sta;
772 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
773 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
774 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
Johannes Bergdebff612013-05-14 13:53:45 +0200775 struct iwl_d3_manager_config d3_cfg_cmd_data = {
Johannes Bergc9204872013-02-15 11:00:58 +0100776 /*
777 * Program the minimum sleep time to 10 seconds, as many
778 * platforms have issues processing a wakeup signal while
779 * still being in the process of suspending.
780 */
781 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
782 };
Johannes Bergdebff612013-05-14 13:53:45 +0200783 struct iwl_host_cmd d3_cfg_cmd = {
784 .id = D3_CONFIG_CMD,
785 .flags = CMD_SYNC | CMD_WANT_SKB,
786 .data[0] = &d3_cfg_cmd_data,
787 .len[0] = sizeof(d3_cfg_cmd_data),
788 };
Johannes Berg8ca151b2013-01-24 14:25:36 +0100789 struct wowlan_key_data key_data = {
790 .use_rsc_tsc = false,
791 .tkip = &tkip_cmd,
792 .use_tkip = false,
793 };
794 int ret, i;
Johannes Bergdebff612013-05-14 13:53:45 +0200795 int len __maybe_unused;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100796 u16 seq;
797 u8 old_aux_sta_id, old_ap_sta_id = IWL_MVM_STATION_COUNT;
798
Johannes Bergdebff612013-05-14 13:53:45 +0200799 if (!wowlan) {
800 /*
801 * mac80211 shouldn't get here, but for D3 test
802 * it doesn't warrant a warning
803 */
804 WARN_ON(!test);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100805 return -EINVAL;
Johannes Bergdebff612013-05-14 13:53:45 +0200806 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100807
808 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
809 if (!key_data.rsc_tsc)
810 return -ENOMEM;
811
812 mutex_lock(&mvm->mutex);
813
814 old_aux_sta_id = mvm->aux_sta.sta_id;
815
816 /* see if there's only a single BSS vif and it's associated */
817 ieee80211_iterate_active_interfaces_atomic(
818 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
819 iwl_mvm_d3_iface_iterator, &suspend_iter_data);
820
821 if (suspend_iter_data.error || !suspend_iter_data.vif) {
822 ret = 1;
823 goto out_noreset;
824 }
825
826 vif = suspend_iter_data.vif;
827 mvmvif = iwl_mvm_vif_from_mac80211(vif);
828
829 ap_sta = rcu_dereference_protected(
830 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
831 lockdep_is_held(&mvm->mutex));
832 if (IS_ERR_OR_NULL(ap_sta)) {
833 ret = -EINVAL;
834 goto out_noreset;
835 }
836
837 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
838
839 /*
840 * The D3 firmware still hardcodes the AP station ID for the
841 * BSS we're associated with as 0. Store the real STA ID here
842 * and assign 0. When we leave this function, we'll restore
843 * the original value for the resume code.
844 */
845 old_ap_sta_id = mvm_ap_sta->sta_id;
846 mvm_ap_sta->sta_id = 0;
847 mvmvif->ap_sta_id = 0;
848
849 /* TODO: wowlan_config_cmd.wowlan_ba_teardown_tids */
850
851 wowlan_config_cmd.is_11n_connection = ap_sta->ht_cap.ht_supported;
852
853 /*
854 * We know the last used seqno, and the uCode expects to know that
855 * one, it will increment before TX.
856 */
857 seq = mvm_ap_sta->last_seq_ctl & IEEE80211_SCTL_SEQ;
858 wowlan_config_cmd.non_qos_seq = cpu_to_le16(seq);
859
860 /*
861 * For QoS counters, we store the one to use next, so subtract 0x10
862 * since the uCode will add 0x10 *before* using the value while we
863 * increment after using the value (i.e. store the next value to use).
864 */
865 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
866 seq = mvm_ap_sta->tid_data[i].seq_number;
867 seq -= 0x10;
868 wowlan_config_cmd.qos_seq[i] = cpu_to_le16(seq);
869 }
870
871 if (wowlan->disconnect)
872 wowlan_config_cmd.wakeup_filter |=
873 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
874 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
875 if (wowlan->magic_pkt)
876 wowlan_config_cmd.wakeup_filter |=
877 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
878 if (wowlan->gtk_rekey_failure)
879 wowlan_config_cmd.wakeup_filter |=
880 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
881 if (wowlan->eap_identity_req)
882 wowlan_config_cmd.wakeup_filter |=
883 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
884 if (wowlan->four_way_handshake)
885 wowlan_config_cmd.wakeup_filter |=
886 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
887 if (wowlan->n_patterns)
888 wowlan_config_cmd.wakeup_filter |=
889 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
890
891 if (wowlan->rfkill_release)
Johannes Berg6039f3e2013-03-20 10:40:05 +0100892 wowlan_config_cmd.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100893 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
894
Johannes Bergf0c26462013-01-22 20:41:58 +0100895 if (wowlan->tcp) {
896 /*
Johannes Berg6039f3e2013-03-20 10:40:05 +0100897 * Set the "link change" (really "link lost") flag as well
898 * since that implies losing the TCP connection.
Johannes Bergf0c26462013-01-22 20:41:58 +0100899 */
900 wowlan_config_cmd.wakeup_filter |=
901 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
902 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
903 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
904 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
905 }
906
Johannes Berg8ca151b2013-01-24 14:25:36 +0100907 iwl_mvm_cancel_scan(mvm);
908
909 iwl_trans_stop_device(mvm->trans);
910
911 /*
912 * Set the HW restart bit -- this is mostly true as we're
913 * going to load new firmware and reprogram that, though
914 * the reprogramming is going to be manual to avoid adding
915 * all the MACs that aren't support.
916 * We don't have to clear up everything though because the
917 * reprogramming is manual. When we resume, we'll actually
918 * go through a proper restart sequence again to switch
919 * back to the runtime firmware image.
920 */
921 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
922
923 /* We reprogram keys and shouldn't allocate new key indices */
924 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
925
Johannes Bergf444eb12013-02-26 12:04:18 +0100926 mvm->ptk_ivlen = 0;
927 mvm->ptk_icvlen = 0;
928 mvm->ptk_ivlen = 0;
929 mvm->ptk_icvlen = 0;
930
Johannes Berg8ca151b2013-01-24 14:25:36 +0100931 /*
932 * The D3 firmware still hardcodes the AP station ID for the
933 * BSS we're associated with as 0. As a result, we have to move
934 * the auxiliary station to ID 1 so the ID 0 remains free for
935 * the AP station for later.
936 * We set the sta_id to 1 here, and reset it to its previous
937 * value (that we stored above) later.
938 */
939 mvm->aux_sta.sta_id = 1;
940
941 ret = iwl_mvm_load_d3_fw(mvm);
942 if (ret)
943 goto out;
944
945 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
946 if (ret)
947 goto out;
948
949 if (!iwlwifi_mod_params.sw_crypto) {
950 /*
951 * This needs to be unlocked due to lock ordering
952 * constraints. Since we're in the suspend path
953 * that isn't really a problem though.
954 */
955 mutex_unlock(&mvm->mutex);
956 ieee80211_iter_keys(mvm->hw, vif,
957 iwl_mvm_wowlan_program_keys,
958 &key_data);
959 mutex_lock(&mvm->mutex);
960 if (key_data.error) {
961 ret = -EIO;
962 goto out;
963 }
964
965 if (key_data.use_rsc_tsc) {
966 struct iwl_host_cmd rsc_tsc_cmd = {
967 .id = WOWLAN_TSC_RSC_PARAM,
968 .flags = CMD_SYNC,
969 .data[0] = key_data.rsc_tsc,
970 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
971 .len[0] = sizeof(*key_data.rsc_tsc),
972 };
973
974 ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
975 if (ret)
976 goto out;
977 }
978
979 if (key_data.use_tkip) {
980 ret = iwl_mvm_send_cmd_pdu(mvm,
981 WOWLAN_TKIP_PARAM,
982 CMD_SYNC, sizeof(tkip_cmd),
983 &tkip_cmd);
984 if (ret)
985 goto out;
986 }
987
988 if (mvmvif->rekey_data.valid) {
989 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
990 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
991 NL80211_KCK_LEN);
992 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
993 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
994 NL80211_KEK_LEN);
995 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
996 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
997
998 ret = iwl_mvm_send_cmd_pdu(mvm,
999 WOWLAN_KEK_KCK_MATERIAL,
1000 CMD_SYNC,
1001 sizeof(kek_kck_cmd),
1002 &kek_kck_cmd);
1003 if (ret)
1004 goto out;
1005 }
1006 }
1007
1008 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION,
1009 CMD_SYNC, sizeof(wowlan_config_cmd),
1010 &wowlan_config_cmd);
1011 if (ret)
1012 goto out;
1013
1014 ret = iwl_mvm_send_patterns(mvm, wowlan);
1015 if (ret)
1016 goto out;
1017
1018 ret = iwl_mvm_send_proto_offload(mvm, vif);
1019 if (ret)
1020 goto out;
1021
Johannes Bergf0c26462013-01-22 20:41:58 +01001022 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1023 if (ret)
1024 goto out;
1025
Alexander Bondaree1e8422013-04-23 13:52:10 +03001026 ret = iwl_mvm_power_update_mode(mvm, vif);
1027 if (ret)
1028 goto out;
1029
Johannes Berg8ca151b2013-01-24 14:25:36 +01001030 /* must be last -- this switches firmware state */
Johannes Bergdebff612013-05-14 13:53:45 +02001031 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001032 if (ret)
1033 goto out;
Johannes Bergdebff612013-05-14 13:53:45 +02001034#ifdef CONFIG_IWLWIFI_DEBUGFS
1035 len = le32_to_cpu(d3_cfg_cmd.resp_pkt->len_n_flags) &
1036 FH_RSCSR_FRAME_SIZE_MSK;
1037 if (len >= sizeof(u32) * 2) {
1038 mvm->d3_test_pme_ptr =
1039 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1040 } else if (test) {
1041 /* in test mode we require the pointer */
1042 ret = -EIO;
1043 goto out;
1044 }
1045#endif
1046 iwl_free_resp(&d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001047
1048 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1049
Johannes Bergdebff612013-05-14 13:53:45 +02001050 iwl_trans_d3_suspend(mvm->trans, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001051 out:
1052 mvm->aux_sta.sta_id = old_aux_sta_id;
1053 mvm_ap_sta->sta_id = old_ap_sta_id;
1054 mvmvif->ap_sta_id = old_ap_sta_id;
1055 out_noreset:
1056 kfree(key_data.rsc_tsc);
1057 if (ret < 0)
1058 ieee80211_restart_hw(mvm->hw);
1059
1060 mutex_unlock(&mvm->mutex);
1061
1062 return ret;
1063}
1064
Johannes Bergdebff612013-05-14 13:53:45 +02001065int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1066{
1067 return __iwl_mvm_suspend(hw, wowlan, false);
1068}
1069
Johannes Berg9b26b502013-01-22 13:02:09 +01001070static void iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1071 struct ieee80211_vif *vif)
1072{
1073 u32 base = mvm->error_event_table;
1074 struct error_table_start {
1075 /* cf. struct iwl_error_event_table */
1076 u32 valid;
1077 u32 error_id;
1078 } err_info;
1079 struct cfg80211_wowlan_wakeup wakeup = {
1080 .pattern_idx = -1,
1081 };
1082 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1083 struct iwl_host_cmd cmd = {
1084 .id = WOWLAN_GET_STATUSES,
1085 .flags = CMD_SYNC | CMD_WANT_SKB,
1086 };
1087 struct iwl_wowlan_status *status;
1088 u32 reasons;
1089 int ret, len;
Johannes Berg9b26b502013-01-22 13:02:09 +01001090 struct sk_buff *pkt = NULL;
1091
1092 iwl_trans_read_mem_bytes(mvm->trans, base,
1093 &err_info, sizeof(err_info));
1094
1095 if (err_info.valid) {
1096 IWL_INFO(mvm, "error table is valid (%d)\n",
1097 err_info.valid);
1098 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1099 wakeup.rfkill_release = true;
1100 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1101 GFP_KERNEL);
1102 }
1103 return;
1104 }
1105
1106 /* only for tracing for now */
1107 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, CMD_SYNC, 0, NULL);
1108 if (ret)
1109 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1110
1111 ret = iwl_mvm_send_cmd(mvm, &cmd);
1112 if (ret) {
1113 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1114 return;
1115 }
1116
1117 /* RF-kill already asserted again... */
1118 if (!cmd.resp_pkt)
1119 return;
1120
1121 len = le32_to_cpu(cmd.resp_pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1122 if (len - sizeof(struct iwl_cmd_header) < sizeof(*status)) {
1123 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1124 goto out;
1125 }
1126
1127 status = (void *)cmd.resp_pkt->data;
1128
1129 if (len - sizeof(struct iwl_cmd_header) !=
Johannes Bergf444eb12013-02-26 12:04:18 +01001130 sizeof(*status) +
1131 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4)) {
Johannes Berg9b26b502013-01-22 13:02:09 +01001132 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1133 goto out;
1134 }
1135
1136 reasons = le32_to_cpu(status->wakeup_reasons);
1137
1138 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1139 wakeup_report = NULL;
1140 goto report;
1141 }
1142
Johannes Bergf444eb12013-02-26 12:04:18 +01001143 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
Johannes Berg9b26b502013-01-22 13:02:09 +01001144 wakeup.magic_pkt = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001145
Johannes Bergf444eb12013-02-26 12:04:18 +01001146 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
Johannes Berg9b26b502013-01-22 13:02:09 +01001147 wakeup.pattern_idx =
1148 le16_to_cpu(status->pattern_number);
Johannes Berg9b26b502013-01-22 13:02:09 +01001149
1150 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1151 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1152 wakeup.disconnect = true;
1153
Johannes Bergf444eb12013-02-26 12:04:18 +01001154 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001155 wakeup.gtk_rekey_failure = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001156
Johannes Bergf444eb12013-02-26 12:04:18 +01001157 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
Johannes Berg9b26b502013-01-22 13:02:09 +01001158 wakeup.rfkill_release = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001159
Johannes Bergf444eb12013-02-26 12:04:18 +01001160 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
Johannes Berg9b26b502013-01-22 13:02:09 +01001161 wakeup.eap_identity_req = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001162
Johannes Bergf444eb12013-02-26 12:04:18 +01001163 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001164 wakeup.four_way_handshake = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001165
Johannes Bergf0c26462013-01-22 20:41:58 +01001166 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1167 wakeup.tcp_connlost = true;
1168
1169 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1170 wakeup.tcp_nomoretokens = true;
1171
1172 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1173 wakeup.tcp_match = true;
1174
Johannes Berg9b26b502013-01-22 13:02:09 +01001175 if (status->wake_packet_bufsize) {
Johannes Bergf444eb12013-02-26 12:04:18 +01001176 int pktsize = le32_to_cpu(status->wake_packet_bufsize);
1177 int pktlen = le32_to_cpu(status->wake_packet_length);
1178 const u8 *pktdata = status->wake_packet;
1179 struct ieee80211_hdr *hdr = (void *)pktdata;
1180 int truncated = pktlen - pktsize;
Johannes Berg9b26b502013-01-22 13:02:09 +01001181
Johannes Bergf444eb12013-02-26 12:04:18 +01001182 /* this would be a firmware bug */
1183 if (WARN_ON_ONCE(truncated < 0))
1184 truncated = 0;
1185
1186 if (ieee80211_is_data(hdr->frame_control)) {
1187 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1188 int ivlen = 0, icvlen = 4; /* also FCS */
1189
Johannes Berg9b26b502013-01-22 13:02:09 +01001190 pkt = alloc_skb(pktsize, GFP_KERNEL);
1191 if (!pkt)
1192 goto report;
Johannes Bergf444eb12013-02-26 12:04:18 +01001193
1194 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1195 pktdata += hdrlen;
1196 pktsize -= hdrlen;
1197
1198 if (ieee80211_has_protected(hdr->frame_control)) {
1199 if (is_multicast_ether_addr(hdr->addr1)) {
1200 ivlen = mvm->gtk_ivlen;
1201 icvlen += mvm->gtk_icvlen;
1202 } else {
1203 ivlen = mvm->ptk_ivlen;
1204 icvlen += mvm->ptk_icvlen;
1205 }
1206 }
1207
1208 /* if truncated, FCS/ICV is (partially) gone */
1209 if (truncated >= icvlen) {
1210 icvlen = 0;
1211 truncated -= icvlen;
1212 } else {
1213 icvlen -= truncated;
1214 truncated = 0;
1215 }
1216
1217 pktsize -= ivlen + icvlen;
1218 pktdata += ivlen;
1219
1220 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1221
Johannes Berg9b26b502013-01-22 13:02:09 +01001222 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1223 goto report;
1224 wakeup.packet = pkt->data;
1225 wakeup.packet_present_len = pkt->len;
Johannes Bergf444eb12013-02-26 12:04:18 +01001226 wakeup.packet_len = pkt->len - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001227 wakeup.packet_80211 = false;
1228 } else {
Johannes Bergf444eb12013-02-26 12:04:18 +01001229 int fcslen = 4;
1230
1231 if (truncated >= 4) {
1232 truncated -= 4;
1233 fcslen = 0;
1234 } else {
1235 fcslen -= truncated;
1236 truncated = 0;
1237 }
1238 pktsize -= fcslen;
Johannes Berg9b26b502013-01-22 13:02:09 +01001239 wakeup.packet = status->wake_packet;
1240 wakeup.packet_present_len = pktsize;
Johannes Bergf444eb12013-02-26 12:04:18 +01001241 wakeup.packet_len = pktlen - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001242 wakeup.packet_80211 = true;
1243 }
1244 }
1245
1246 report:
1247 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1248 kfree_skb(pkt);
1249
1250 out:
1251 iwl_free_resp(&cmd);
1252}
1253
Johannes Bergafc66bb2013-05-03 11:44:16 +02001254static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1255{
1256#ifdef CONFIG_IWLWIFI_DEBUGFS
1257 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1258 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1259 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1260
1261 if (!mvm->store_d3_resume_sram)
1262 return;
1263
1264 if (!mvm->d3_resume_sram) {
1265 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1266 if (!mvm->d3_resume_sram)
1267 return;
1268 }
1269
1270 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1271#endif
1272}
1273
Johannes Bergdebff612013-05-14 13:53:45 +02001274static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001275{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001276 struct iwl_d3_iter_data resume_iter_data = {
1277 .mvm = mvm,
1278 };
1279 struct ieee80211_vif *vif = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001280 int ret;
1281 enum iwl_d3_status d3_status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001282
1283 mutex_lock(&mvm->mutex);
1284
1285 /* get the BSS vif pointer again */
1286 ieee80211_iterate_active_interfaces_atomic(
1287 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1288 iwl_mvm_d3_iface_iterator, &resume_iter_data);
1289
1290 if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
1291 goto out_unlock;
1292
1293 vif = resume_iter_data.vif;
1294
Johannes Bergdebff612013-05-14 13:53:45 +02001295 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001296 if (ret)
1297 goto out_unlock;
1298
1299 if (d3_status != IWL_D3_STATUS_ALIVE) {
1300 IWL_INFO(mvm, "Device was reset during suspend\n");
1301 goto out_unlock;
1302 }
1303
Johannes Bergafc66bb2013-05-03 11:44:16 +02001304 /* query SRAM first in case we want event logging */
1305 iwl_mvm_read_d3_sram(mvm);
1306
Johannes Berg9b26b502013-01-22 13:02:09 +01001307 iwl_mvm_query_wakeup_reasons(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001308
1309 out_unlock:
1310 mutex_unlock(&mvm->mutex);
1311
Johannes Bergdebff612013-05-14 13:53:45 +02001312 if (!test && vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001313 ieee80211_resume_disconnect(vif);
1314
1315 /* return 1 to reconfigure the device */
1316 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1317 return 1;
1318}
1319
Johannes Bergdebff612013-05-14 13:53:45 +02001320int iwl_mvm_resume(struct ieee80211_hw *hw)
1321{
1322 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1323
1324 return __iwl_mvm_resume(mvm, false);
1325}
1326
Johannes Berg8ca151b2013-01-24 14:25:36 +01001327void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1328{
1329 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1330
1331 device_set_wakeup_enable(mvm->trans->dev, enabled);
1332}
Johannes Bergdebff612013-05-14 13:53:45 +02001333
1334#ifdef CONFIG_IWLWIFI_DEBUGFS
1335static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
1336{
1337 struct iwl_mvm *mvm = inode->i_private;
1338 int err;
1339
1340 if (mvm->d3_test_active)
1341 return -EBUSY;
1342
1343 file->private_data = inode->i_private;
1344
1345 ieee80211_stop_queues(mvm->hw);
1346 synchronize_net();
1347
1348 /* start pseudo D3 */
1349 rtnl_lock();
1350 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
1351 rtnl_unlock();
1352 if (err > 0)
1353 err = -EINVAL;
1354 if (err) {
1355 ieee80211_wake_queues(mvm->hw);
1356 return err;
1357 }
1358 mvm->d3_test_active = true;
1359 return 0;
1360}
1361
1362static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
1363 size_t count, loff_t *ppos)
1364{
1365 struct iwl_mvm *mvm = file->private_data;
1366 u32 pme_asserted;
1367
1368 while (true) {
1369 pme_asserted = iwl_trans_read_mem32(mvm->trans,
1370 mvm->d3_test_pme_ptr);
1371 if (pme_asserted)
1372 break;
1373 if (msleep_interruptible(100))
1374 break;
1375 }
1376
1377 return 0;
1378}
1379
1380static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
1381 struct ieee80211_vif *vif)
1382{
1383 if (vif->type == NL80211_IFTYPE_STATION)
1384 ieee80211_connection_loss(vif);
1385}
1386
1387static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
1388{
1389 struct iwl_mvm *mvm = inode->i_private;
1390 int remaining_time = 10;
1391
1392 mvm->d3_test_active = false;
1393 __iwl_mvm_resume(mvm, true);
1394 iwl_abort_notification_waits(&mvm->notif_wait);
1395 ieee80211_restart_hw(mvm->hw);
1396
1397 /* wait for restart and disconnect all interfaces */
1398 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1399 remaining_time > 0) {
1400 remaining_time--;
1401 msleep(1000);
1402 }
1403
1404 if (remaining_time == 0)
1405 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
1406
1407 ieee80211_iterate_active_interfaces_atomic(
1408 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1409 iwl_mvm_d3_test_disconn_work_iter, NULL);
1410
1411 ieee80211_wake_queues(mvm->hw);
1412
1413 return 0;
1414}
1415
1416const struct file_operations iwl_dbgfs_d3_test_ops = {
1417 .llseek = no_llseek,
1418 .open = iwl_mvm_d3_test_open,
1419 .read = iwl_mvm_d3_test_read,
1420 .release = iwl_mvm_d3_test_release,
1421};
1422#endif