blob: 48c192d85c4c6a047d35392f5d637655b47664f8 [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
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300196 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100197 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 {
Johannes Berg5312e542013-07-22 18:26:56 +0200319 /*
320 * firmware only supports TSC/RSC for a single key,
321 * so if there are multiple keep overwriting them
322 * with new ones -- this relies on mac80211 doing
323 * list_add_tail().
324 */
325 key->hw_key_idx = 1;
Johannes Bergf444eb12013-02-26 12:04:18 +0100326 mvm->gtk_ivlen = key->iv_len;
327 mvm->gtk_icvlen = key->icv_len;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100328 }
329
330 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
331 data->error = ret != 0;
332out_unlock:
333 mutex_unlock(&mvm->mutex);
334}
335
336static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
337 struct cfg80211_wowlan *wowlan)
338{
339 struct iwl_wowlan_patterns_cmd *pattern_cmd;
340 struct iwl_host_cmd cmd = {
341 .id = WOWLAN_PATTERNS,
342 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100343 };
344 int i, err;
345
346 if (!wowlan->n_patterns)
347 return 0;
348
349 cmd.len[0] = sizeof(*pattern_cmd) +
350 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
351
352 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
353 if (!pattern_cmd)
354 return -ENOMEM;
355
356 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
357
358 for (i = 0; i < wowlan->n_patterns; i++) {
359 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
360
361 memcpy(&pattern_cmd->patterns[i].mask,
362 wowlan->patterns[i].mask, mask_len);
363 memcpy(&pattern_cmd->patterns[i].pattern,
364 wowlan->patterns[i].pattern,
365 wowlan->patterns[i].pattern_len);
366 pattern_cmd->patterns[i].mask_size = mask_len;
367 pattern_cmd->patterns[i].pattern_size =
368 wowlan->patterns[i].pattern_len;
369 }
370
371 cmd.data[0] = pattern_cmd;
372 err = iwl_mvm_send_cmd(mvm, &cmd);
373 kfree(pattern_cmd);
374 return err;
375}
376
Johannes Bergf0c26462013-01-22 20:41:58 +0100377enum iwl_mvm_tcp_packet_type {
378 MVM_TCP_TX_SYN,
379 MVM_TCP_RX_SYNACK,
380 MVM_TCP_TX_DATA,
381 MVM_TCP_RX_ACK,
382 MVM_TCP_RX_WAKE,
383 MVM_TCP_TX_FIN,
384};
385
386static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
387{
388 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
389 return cpu_to_le16(be16_to_cpu((__force __be16)check));
390}
391
Oren Givonb002c7e2013-06-12 20:37:03 +0300392static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
Johannes Bergf0c26462013-01-22 20:41:58 +0100393 struct cfg80211_wowlan_tcp *tcp,
394 void *_pkt, u8 *mask,
395 __le16 *pseudo_hdr_csum,
396 enum iwl_mvm_tcp_packet_type ptype)
397{
398 struct {
399 struct ethhdr eth;
400 struct iphdr ip;
401 struct tcphdr tcp;
402 u8 data[];
403 } __packed *pkt = _pkt;
404 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
405 int i;
406
407 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
408 pkt->ip.version = 4;
409 pkt->ip.ihl = 5;
410 pkt->ip.protocol = IPPROTO_TCP;
411
412 switch (ptype) {
413 case MVM_TCP_TX_SYN:
414 case MVM_TCP_TX_DATA:
415 case MVM_TCP_TX_FIN:
416 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
417 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
418 pkt->ip.ttl = 128;
419 pkt->ip.saddr = tcp->src;
420 pkt->ip.daddr = tcp->dst;
421 pkt->tcp.source = cpu_to_be16(tcp->src_port);
422 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
423 /* overwritten for TX SYN later */
424 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
425 pkt->tcp.window = cpu_to_be16(65000);
426 break;
427 case MVM_TCP_RX_SYNACK:
428 case MVM_TCP_RX_ACK:
429 case MVM_TCP_RX_WAKE:
430 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
431 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
432 pkt->ip.saddr = tcp->dst;
433 pkt->ip.daddr = tcp->src;
434 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
435 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
436 break;
437 default:
438 WARN_ON(1);
439 return;
440 }
441
442 switch (ptype) {
443 case MVM_TCP_TX_SYN:
444 /* firmware assumes 8 option bytes - 8 NOPs for now */
445 memset(pkt->data, 0x01, 8);
446 ip_tot_len += 8;
447 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
448 pkt->tcp.syn = 1;
449 break;
450 case MVM_TCP_TX_DATA:
451 ip_tot_len += tcp->payload_len;
452 memcpy(pkt->data, tcp->payload, tcp->payload_len);
453 pkt->tcp.psh = 1;
454 pkt->tcp.ack = 1;
455 break;
456 case MVM_TCP_TX_FIN:
457 pkt->tcp.fin = 1;
458 pkt->tcp.ack = 1;
459 break;
460 case MVM_TCP_RX_SYNACK:
461 pkt->tcp.syn = 1;
462 pkt->tcp.ack = 1;
463 break;
464 case MVM_TCP_RX_ACK:
465 pkt->tcp.ack = 1;
466 break;
467 case MVM_TCP_RX_WAKE:
468 ip_tot_len += tcp->wake_len;
469 pkt->tcp.psh = 1;
470 pkt->tcp.ack = 1;
471 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
472 break;
473 }
474
475 switch (ptype) {
476 case MVM_TCP_TX_SYN:
477 case MVM_TCP_TX_DATA:
478 case MVM_TCP_TX_FIN:
479 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
480 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
481 break;
482 case MVM_TCP_RX_WAKE:
483 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
484 u8 tmp = tcp->wake_mask[i];
485 mask[i + 6] |= tmp << 6;
486 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
487 mask[i + 7] = tmp >> 2;
488 }
489 /* fall through for ethernet/IP/TCP headers mask */
490 case MVM_TCP_RX_SYNACK:
491 case MVM_TCP_RX_ACK:
492 mask[0] = 0xff; /* match ethernet */
493 /*
494 * match ethernet, ip.version, ip.ihl
495 * the ip.ihl half byte is really masked out by firmware
496 */
497 mask[1] = 0x7f;
498 mask[2] = 0x80; /* match ip.protocol */
499 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
500 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
501 mask[5] = 0x80; /* match tcp flags */
502 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
503 break;
504 };
505
506 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
507 pkt->ip.saddr, pkt->ip.daddr);
508}
509
510static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
511 struct ieee80211_vif *vif,
512 struct cfg80211_wowlan_tcp *tcp)
513{
514 struct iwl_wowlan_remote_wake_config *cfg;
515 struct iwl_host_cmd cmd = {
516 .id = REMOTE_WAKE_CONFIG_CMD,
517 .len = { sizeof(*cfg), },
518 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
Johannes Bergf0c26462013-01-22 20:41:58 +0100519 };
520 int ret;
521
522 if (!tcp)
523 return 0;
524
525 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
526 if (!cfg)
527 return -ENOMEM;
528 cmd.data[0] = cfg;
529
530 cfg->max_syn_retries = 10;
531 cfg->max_data_retries = 10;
532 cfg->tcp_syn_ack_timeout = 1; /* seconds */
533 cfg->tcp_ack_timeout = 1; /* seconds */
534
535 /* SYN (TX) */
536 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300537 vif, tcp, cfg->syn_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100538 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
539 MVM_TCP_TX_SYN);
540 cfg->syn_tx.info.tcp_payload_length = 0;
541
542 /* SYN/ACK (RX) */
543 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300544 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
Johannes Bergf0c26462013-01-22 20:41:58 +0100545 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
546 MVM_TCP_RX_SYNACK);
547 cfg->synack_rx.info.tcp_payload_length = 0;
548
549 /* KEEPALIVE/ACK (TX) */
550 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300551 vif, tcp, cfg->keepalive_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100552 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
553 MVM_TCP_TX_DATA);
554 cfg->keepalive_tx.info.tcp_payload_length =
555 cpu_to_le16(tcp->payload_len);
556 cfg->sequence_number_offset = tcp->payload_seq.offset;
557 /* length must be 0..4, the field is little endian */
558 cfg->sequence_number_length = tcp->payload_seq.len;
559 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
560 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
561 if (tcp->payload_tok.len) {
562 cfg->token_offset = tcp->payload_tok.offset;
563 cfg->token_length = tcp->payload_tok.len;
564 cfg->num_tokens =
565 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
566 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
567 tcp->tokens_size);
568 } else {
569 /* set tokens to max value to almost never run out */
570 cfg->num_tokens = cpu_to_le16(65535);
571 }
572
573 /* ACK (RX) */
574 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300575 vif, tcp, cfg->keepalive_ack_rx.data,
Johannes Bergf0c26462013-01-22 20:41:58 +0100576 cfg->keepalive_ack_rx.rx_mask,
577 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
578 MVM_TCP_RX_ACK);
579 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
580
581 /* WAKEUP (RX) */
582 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300583 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
Johannes Bergf0c26462013-01-22 20:41:58 +0100584 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
585 MVM_TCP_RX_WAKE);
586 cfg->wake_rx.info.tcp_payload_length =
587 cpu_to_le16(tcp->wake_len);
588
589 /* FIN */
590 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300591 vif, tcp, cfg->fin_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100592 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
593 MVM_TCP_TX_FIN);
594 cfg->fin_tx.info.tcp_payload_length = 0;
595
596 ret = iwl_mvm_send_cmd(mvm, &cmd);
597 kfree(cfg);
598
599 return ret;
600}
601
Johannes Berg8ca151b2013-01-24 14:25:36 +0100602struct iwl_d3_iter_data {
603 struct iwl_mvm *mvm;
604 struct ieee80211_vif *vif;
605 bool error;
606};
607
608static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
609 struct ieee80211_vif *vif)
610{
611 struct iwl_d3_iter_data *data = _data;
612 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
613
614 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
615 return;
616
617 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
618 return;
619
620 if (data->vif) {
621 IWL_ERR(data->mvm, "More than one managed interface active!\n");
622 data->error = true;
623 return;
624 }
625
626 data->vif = vif;
627}
628
629static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
630 struct ieee80211_sta *ap_sta)
631{
632 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
633 struct ieee80211_chanctx_conf *ctx;
634 u8 chains_static, chains_dynamic;
635 struct cfg80211_chan_def chandef;
636 int ret, i;
637 struct iwl_binding_cmd binding_cmd = {};
638 struct iwl_time_quota_cmd quota_cmd = {};
639 u32 status;
640
641 /* add back the PHY */
642 if (WARN_ON(!mvmvif->phy_ctxt))
643 return -EINVAL;
644
645 rcu_read_lock();
646 ctx = rcu_dereference(vif->chanctx_conf);
647 if (WARN_ON(!ctx)) {
648 rcu_read_unlock();
649 return -EINVAL;
650 }
651 chandef = ctx->def;
652 chains_static = ctx->rx_chains_static;
653 chains_dynamic = ctx->rx_chains_dynamic;
654 rcu_read_unlock();
655
656 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
657 chains_static, chains_dynamic);
658 if (ret)
659 return ret;
660
661 /* add back the MAC */
662 mvmvif->uploaded = false;
663
664 if (WARN_ON(!vif->bss_conf.assoc))
665 return -EINVAL;
Luciano Coelhobca49d92014-05-13 17:33:38 +0300666
Johannes Berg8ca151b2013-01-24 14:25:36 +0100667 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100668 if (ret)
669 return ret;
670
671 /* add back binding - XXX refactor? */
672 binding_cmd.id_and_color =
673 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
674 mvmvif->phy_ctxt->color));
675 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
676 binding_cmd.phy =
677 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
678 mvmvif->phy_ctxt->color));
679 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
680 mvmvif->color));
681 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
682 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
683
684 status = 0;
685 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
686 sizeof(binding_cmd), &binding_cmd,
687 &status);
688 if (ret) {
689 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
690 return ret;
691 }
692
693 if (status) {
694 IWL_ERR(mvm, "Binding command failed: %u\n", status);
695 return -EIO;
696 }
697
Johannes Berg7a453972013-02-12 13:10:44 +0100698 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100699 if (ret)
700 return ret;
701 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
702
Luciano Coelhobca49d92014-05-13 17:33:38 +0300703 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100704 if (ret)
705 return ret;
706
707 /* and some quota */
708 quota_cmd.quotas[0].id_and_color =
709 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
710 mvmvif->phy_ctxt->color));
Emmanuel Grumbach9a3daf82014-03-05 10:00:44 +0200711 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
712 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100713
714 for (i = 1; i < MAX_BINDINGS; i++)
715 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
716
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300717 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100718 sizeof(quota_cmd), &quota_cmd);
719 if (ret)
720 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
721
722 return 0;
723}
724
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200725static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
726 struct ieee80211_vif *vif)
727{
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200728 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
729 struct iwl_nonqos_seq_query_cmd query_cmd = {
730 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
731 .mac_id_n_color =
732 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
733 mvmvif->color)),
734 };
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200735 struct iwl_host_cmd cmd = {
736 .id = NON_QOS_TX_COUNTER_CMD,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300737 .flags = CMD_WANT_SKB,
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200738 };
739 int err;
740 u32 size;
741
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300742 cmd.data[0] = &query_cmd;
743 cmd.len[0] = sizeof(query_cmd);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200744
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200745 err = iwl_mvm_send_cmd(mvm, &cmd);
746 if (err)
747 return err;
748
Johannes Berg65b30342014-01-08 13:16:33 +0100749 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200750 if (size < sizeof(__le16)) {
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200751 err = -EINVAL;
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200752 } else {
753 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300754 /* firmware returns next, not last-used seqno */
755 err = (u16) (err - 0x10);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200756 }
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200757
758 iwl_free_resp(&cmd);
759 return err;
760}
761
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200762void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
763{
764 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
765 struct iwl_nonqos_seq_query_cmd query_cmd = {
766 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
767 .mac_id_n_color =
768 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
769 mvmvif->color)),
770 .value = cpu_to_le16(mvmvif->seqno),
771 };
772
773 /* return if called during restart, not resume from D3 */
774 if (!mvmvif->seqno_valid)
775 return;
776
777 mvmvif->seqno_valid = false;
778
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300779 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200780 sizeof(query_cmd), &query_cmd))
781 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
782}
783
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200784static int
785iwl_mvm_send_wowlan_config_cmd(struct iwl_mvm *mvm,
786 const struct iwl_wowlan_config_cmd_v3 *cmd)
787{
788 /* start only with the v2 part of the command */
789 u16 cmd_len = sizeof(cmd->common);
790
791 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_WOWLAN_CONFIG_TID)
792 cmd_len = sizeof(*cmd);
793
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300794 return iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200795 cmd_len, cmd);
796}
797
Johannes Bergdebff612013-05-14 13:53:45 +0200798static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
799 struct cfg80211_wowlan *wowlan,
800 bool test)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100801{
802 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
803 struct iwl_d3_iter_data suspend_iter_data = {
804 .mvm = mvm,
805 };
806 struct ieee80211_vif *vif;
807 struct iwl_mvm_vif *mvmvif;
808 struct ieee80211_sta *ap_sta;
809 struct iwl_mvm_sta *mvm_ap_sta;
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200810 struct iwl_wowlan_config_cmd_v3 wowlan_config_cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100811 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
812 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
Johannes Bergdebff612013-05-14 13:53:45 +0200813 struct iwl_d3_manager_config d3_cfg_cmd_data = {
Johannes Bergc9204872013-02-15 11:00:58 +0100814 /*
815 * Program the minimum sleep time to 10 seconds, as many
816 * platforms have issues processing a wakeup signal while
817 * still being in the process of suspending.
818 */
819 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
820 };
Johannes Bergdebff612013-05-14 13:53:45 +0200821 struct iwl_host_cmd d3_cfg_cmd = {
822 .id = D3_CONFIG_CMD,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300823 .flags = CMD_WANT_SKB,
Johannes Bergdebff612013-05-14 13:53:45 +0200824 .data[0] = &d3_cfg_cmd_data,
825 .len[0] = sizeof(d3_cfg_cmd_data),
826 };
Johannes Berg8ca151b2013-01-24 14:25:36 +0100827 struct wowlan_key_data key_data = {
828 .use_rsc_tsc = false,
829 .tkip = &tkip_cmd,
830 .use_tkip = false,
831 };
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200832 int ret;
Johannes Bergdebff612013-05-14 13:53:45 +0200833 int len __maybe_unused;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100834
Johannes Bergdebff612013-05-14 13:53:45 +0200835 if (!wowlan) {
836 /*
837 * mac80211 shouldn't get here, but for D3 test
838 * it doesn't warrant a warning
839 */
840 WARN_ON(!test);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100841 return -EINVAL;
Johannes Bergdebff612013-05-14 13:53:45 +0200842 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100843
844 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
845 if (!key_data.rsc_tsc)
846 return -ENOMEM;
847
848 mutex_lock(&mvm->mutex);
849
Johannes Berg8ca151b2013-01-24 14:25:36 +0100850 /* see if there's only a single BSS vif and it's associated */
851 ieee80211_iterate_active_interfaces_atomic(
852 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
853 iwl_mvm_d3_iface_iterator, &suspend_iter_data);
854
855 if (suspend_iter_data.error || !suspend_iter_data.vif) {
856 ret = 1;
857 goto out_noreset;
858 }
859
860 vif = suspend_iter_data.vif;
861 mvmvif = iwl_mvm_vif_from_mac80211(vif);
862
863 ap_sta = rcu_dereference_protected(
864 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
865 lockdep_is_held(&mvm->mutex));
866 if (IS_ERR_OR_NULL(ap_sta)) {
867 ret = -EINVAL;
868 goto out_noreset;
869 }
870
871 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
872
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200873 /* TODO: wowlan_config_cmd.common.wowlan_ba_teardown_tids */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100874
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200875 wowlan_config_cmd.common.is_11n_connection =
876 ap_sta->ht_cap.ht_supported;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100877
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200878 /* Query the last used seqno and set it */
879 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
880 if (ret < 0)
881 goto out_noreset;
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200882 wowlan_config_cmd.common.non_qos_seq = cpu_to_le16(ret);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100883
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200884 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, &wowlan_config_cmd.common);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100885
886 if (wowlan->disconnect)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200887 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100888 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
889 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
890 if (wowlan->magic_pkt)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200891 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100892 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
893 if (wowlan->gtk_rekey_failure)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200894 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100895 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
896 if (wowlan->eap_identity_req)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200897 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100898 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
899 if (wowlan->four_way_handshake)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200900 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100901 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
902 if (wowlan->n_patterns)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200903 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100904 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
905
906 if (wowlan->rfkill_release)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200907 wowlan_config_cmd.common.wakeup_filter |=
Johannes Berg8ca151b2013-01-24 14:25:36 +0100908 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
909
Johannes Bergf0c26462013-01-22 20:41:58 +0100910 if (wowlan->tcp) {
911 /*
Johannes Berg6039f3e2013-03-20 10:40:05 +0100912 * Set the "link change" (really "link lost") flag as well
913 * since that implies losing the TCP connection.
Johannes Bergf0c26462013-01-22 20:41:58 +0100914 */
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200915 wowlan_config_cmd.common.wakeup_filter |=
Johannes Bergf0c26462013-01-22 20:41:58 +0100916 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
917 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
918 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
919 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
920 }
921
Johannes Berg8ca151b2013-01-24 14:25:36 +0100922 iwl_mvm_cancel_scan(mvm);
923
924 iwl_trans_stop_device(mvm->trans);
925
926 /*
927 * Set the HW restart bit -- this is mostly true as we're
928 * going to load new firmware and reprogram that, though
929 * the reprogramming is going to be manual to avoid adding
930 * all the MACs that aren't support.
931 * We don't have to clear up everything though because the
932 * reprogramming is manual. When we resume, we'll actually
933 * go through a proper restart sequence again to switch
934 * back to the runtime firmware image.
935 */
936 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
937
938 /* We reprogram keys and shouldn't allocate new key indices */
939 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
940
Johannes Bergf444eb12013-02-26 12:04:18 +0100941 mvm->ptk_ivlen = 0;
942 mvm->ptk_icvlen = 0;
943 mvm->ptk_ivlen = 0;
944 mvm->ptk_icvlen = 0;
945
Johannes Berg8ca151b2013-01-24 14:25:36 +0100946 ret = iwl_mvm_load_d3_fw(mvm);
947 if (ret)
948 goto out;
949
950 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
951 if (ret)
952 goto out;
953
954 if (!iwlwifi_mod_params.sw_crypto) {
955 /*
956 * This needs to be unlocked due to lock ordering
957 * constraints. Since we're in the suspend path
958 * that isn't really a problem though.
959 */
960 mutex_unlock(&mvm->mutex);
961 ieee80211_iter_keys(mvm->hw, vif,
962 iwl_mvm_wowlan_program_keys,
963 &key_data);
964 mutex_lock(&mvm->mutex);
965 if (key_data.error) {
966 ret = -EIO;
967 goto out;
968 }
969
970 if (key_data.use_rsc_tsc) {
971 struct iwl_host_cmd rsc_tsc_cmd = {
972 .id = WOWLAN_TSC_RSC_PARAM,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100973 .data[0] = key_data.rsc_tsc,
974 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
975 .len[0] = sizeof(*key_data.rsc_tsc),
976 };
977
978 ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
979 if (ret)
980 goto out;
981 }
982
983 if (key_data.use_tkip) {
984 ret = iwl_mvm_send_cmd_pdu(mvm,
985 WOWLAN_TKIP_PARAM,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300986 0, sizeof(tkip_cmd),
Johannes Berg8ca151b2013-01-24 14:25:36 +0100987 &tkip_cmd);
988 if (ret)
989 goto out;
990 }
991
992 if (mvmvif->rekey_data.valid) {
993 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
994 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
995 NL80211_KCK_LEN);
996 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
997 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
998 NL80211_KEK_LEN);
999 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1000 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1001
1002 ret = iwl_mvm_send_cmd_pdu(mvm,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001003 WOWLAN_KEK_KCK_MATERIAL, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001004 sizeof(kek_kck_cmd),
1005 &kek_kck_cmd);
1006 if (ret)
1007 goto out;
1008 }
1009 }
1010
Eliad Peller1a95c8d2013-11-21 19:19:52 +02001011 ret = iwl_mvm_send_wowlan_config_cmd(mvm, &wowlan_config_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001012 if (ret)
1013 goto out;
1014
1015 ret = iwl_mvm_send_patterns(mvm, wowlan);
1016 if (ret)
1017 goto out;
1018
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001019 ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001020 if (ret)
1021 goto out;
1022
Johannes Bergf0c26462013-01-22 20:41:58 +01001023 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1024 if (ret)
1025 goto out;
1026
Emmanuel Grumbachc1cb92f2014-01-28 10:17:18 +02001027 ret = iwl_mvm_power_update_device(mvm);
Alexander Bondar64b928c2013-09-03 14:18:03 +03001028 if (ret)
1029 goto out;
1030
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +02001031 ret = iwl_mvm_power_update_mac(mvm, vif);
Alexander Bondaree1e8422013-04-23 13:52:10 +03001032 if (ret)
1033 goto out;
1034
Johannes Bergb0114712013-06-12 14:55:40 +02001035#ifdef CONFIG_IWLWIFI_DEBUGFS
1036 if (mvm->d3_wake_sysassert)
1037 d3_cfg_cmd_data.wakeup_flags |=
1038 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1039#endif
1040
Johannes Berg8ca151b2013-01-24 14:25:36 +01001041 /* must be last -- this switches firmware state */
Johannes Bergdebff612013-05-14 13:53:45 +02001042 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001043 if (ret)
1044 goto out;
Johannes Bergdebff612013-05-14 13:53:45 +02001045#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg65b30342014-01-08 13:16:33 +01001046 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1047 if (len >= sizeof(u32)) {
Johannes Bergdebff612013-05-14 13:53:45 +02001048 mvm->d3_test_pme_ptr =
1049 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
Johannes Bergdebff612013-05-14 13:53:45 +02001050 }
1051#endif
1052 iwl_free_resp(&d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001053
1054 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1055
Johannes Bergdebff612013-05-14 13:53:45 +02001056 iwl_trans_d3_suspend(mvm->trans, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001057 out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001058 if (ret < 0)
1059 ieee80211_restart_hw(mvm->hw);
Luciano Coelhob70ab722013-11-19 22:53:41 +02001060 out_noreset:
1061 kfree(key_data.rsc_tsc);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001062
1063 mutex_unlock(&mvm->mutex);
1064
1065 return ret;
1066}
1067
Johannes Bergdebff612013-05-14 13:53:45 +02001068int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1069{
Eliad Pellerd15a7472014-03-27 18:53:12 +02001070 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1071
1072 if (iwl_mvm_is_d0i3_supported(mvm)) {
1073 mutex_lock(&mvm->d0i3_suspend_mutex);
1074 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1075 mutex_unlock(&mvm->d0i3_suspend_mutex);
1076 return 0;
1077 }
1078
Johannes Bergdebff612013-05-14 13:53:45 +02001079 return __iwl_mvm_suspend(hw, wowlan, false);
1080}
1081
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001082/* converted data from the different status responses */
1083struct iwl_wowlan_status_data {
1084 u16 pattern_number;
1085 u16 qos_seq_ctr[8];
1086 u32 wakeup_reasons;
1087 u32 wake_packet_length;
1088 u32 wake_packet_bufsize;
1089 const u8 *wake_packet;
1090};
1091
Johannes Berg7abc4632013-08-06 18:28:26 +02001092static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1093 struct ieee80211_vif *vif,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001094 struct iwl_wowlan_status_data *status)
Johannes Berg9b26b502013-01-22 13:02:09 +01001095{
Johannes Berg7abc4632013-08-06 18:28:26 +02001096 struct sk_buff *pkt = NULL;
Johannes Berg9b26b502013-01-22 13:02:09 +01001097 struct cfg80211_wowlan_wakeup wakeup = {
1098 .pattern_idx = -1,
1099 };
1100 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001101 u32 reasons = status->wakeup_reasons;
Johannes Berg9b26b502013-01-22 13:02:09 +01001102
1103 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1104 wakeup_report = NULL;
1105 goto report;
1106 }
1107
Johannes Bergf444eb12013-02-26 12:04:18 +01001108 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
Johannes Berg9b26b502013-01-22 13:02:09 +01001109 wakeup.magic_pkt = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001110
Johannes Bergf444eb12013-02-26 12:04:18 +01001111 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
Johannes Berg9b26b502013-01-22 13:02:09 +01001112 wakeup.pattern_idx =
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001113 status->pattern_number;
Johannes Berg9b26b502013-01-22 13:02:09 +01001114
1115 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1116 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1117 wakeup.disconnect = true;
1118
Johannes Bergf444eb12013-02-26 12:04:18 +01001119 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001120 wakeup.gtk_rekey_failure = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001121
Johannes Bergf444eb12013-02-26 12:04:18 +01001122 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
Johannes Berg9b26b502013-01-22 13:02:09 +01001123 wakeup.rfkill_release = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001124
Johannes Bergf444eb12013-02-26 12:04:18 +01001125 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
Johannes Berg9b26b502013-01-22 13:02:09 +01001126 wakeup.eap_identity_req = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001127
Johannes Bergf444eb12013-02-26 12:04:18 +01001128 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001129 wakeup.four_way_handshake = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001130
Johannes Bergf0c26462013-01-22 20:41:58 +01001131 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1132 wakeup.tcp_connlost = true;
1133
1134 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1135 wakeup.tcp_nomoretokens = true;
1136
1137 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1138 wakeup.tcp_match = true;
1139
Johannes Berg9b26b502013-01-22 13:02:09 +01001140 if (status->wake_packet_bufsize) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001141 int pktsize = status->wake_packet_bufsize;
1142 int pktlen = status->wake_packet_length;
Johannes Bergf444eb12013-02-26 12:04:18 +01001143 const u8 *pktdata = status->wake_packet;
1144 struct ieee80211_hdr *hdr = (void *)pktdata;
1145 int truncated = pktlen - pktsize;
Johannes Berg9b26b502013-01-22 13:02:09 +01001146
Johannes Bergf444eb12013-02-26 12:04:18 +01001147 /* this would be a firmware bug */
1148 if (WARN_ON_ONCE(truncated < 0))
1149 truncated = 0;
1150
1151 if (ieee80211_is_data(hdr->frame_control)) {
1152 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1153 int ivlen = 0, icvlen = 4; /* also FCS */
1154
Johannes Berg9b26b502013-01-22 13:02:09 +01001155 pkt = alloc_skb(pktsize, GFP_KERNEL);
1156 if (!pkt)
1157 goto report;
Johannes Bergf444eb12013-02-26 12:04:18 +01001158
1159 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1160 pktdata += hdrlen;
1161 pktsize -= hdrlen;
1162
1163 if (ieee80211_has_protected(hdr->frame_control)) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001164 /*
1165 * This is unlocked and using gtk_i(c)vlen,
1166 * but since everything is under RTNL still
1167 * that's not really a problem - changing
1168 * it would be difficult.
1169 */
Johannes Bergf444eb12013-02-26 12:04:18 +01001170 if (is_multicast_ether_addr(hdr->addr1)) {
1171 ivlen = mvm->gtk_ivlen;
1172 icvlen += mvm->gtk_icvlen;
1173 } else {
1174 ivlen = mvm->ptk_ivlen;
1175 icvlen += mvm->ptk_icvlen;
1176 }
1177 }
1178
1179 /* if truncated, FCS/ICV is (partially) gone */
1180 if (truncated >= icvlen) {
1181 icvlen = 0;
1182 truncated -= icvlen;
1183 } else {
1184 icvlen -= truncated;
1185 truncated = 0;
1186 }
1187
1188 pktsize -= ivlen + icvlen;
1189 pktdata += ivlen;
1190
1191 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1192
Johannes Berg9b26b502013-01-22 13:02:09 +01001193 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1194 goto report;
1195 wakeup.packet = pkt->data;
1196 wakeup.packet_present_len = pkt->len;
Johannes Bergf444eb12013-02-26 12:04:18 +01001197 wakeup.packet_len = pkt->len - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001198 wakeup.packet_80211 = false;
1199 } else {
Johannes Bergf444eb12013-02-26 12:04:18 +01001200 int fcslen = 4;
1201
1202 if (truncated >= 4) {
1203 truncated -= 4;
1204 fcslen = 0;
1205 } else {
1206 fcslen -= truncated;
1207 truncated = 0;
1208 }
1209 pktsize -= fcslen;
Johannes Berg9b26b502013-01-22 13:02:09 +01001210 wakeup.packet = status->wake_packet;
1211 wakeup.packet_present_len = pktsize;
Johannes Bergf444eb12013-02-26 12:04:18 +01001212 wakeup.packet_len = pktlen - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001213 wakeup.packet_80211 = true;
1214 }
1215 }
1216
1217 report:
1218 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1219 kfree_skb(pkt);
Johannes Berg7abc4632013-08-06 18:28:26 +02001220}
Johannes Berg9b26b502013-01-22 13:02:09 +01001221
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001222static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1223 struct ieee80211_key_seq *seq)
1224{
1225 u64 pn;
1226
1227 pn = le64_to_cpu(sc->pn);
1228 seq->ccmp.pn[0] = pn >> 40;
1229 seq->ccmp.pn[1] = pn >> 32;
1230 seq->ccmp.pn[2] = pn >> 24;
1231 seq->ccmp.pn[3] = pn >> 16;
1232 seq->ccmp.pn[4] = pn >> 8;
1233 seq->ccmp.pn[5] = pn;
1234}
1235
1236static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1237 struct ieee80211_key_seq *seq)
1238{
1239 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1240 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1241}
1242
1243static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1244 struct ieee80211_key_conf *key)
1245{
1246 int tid;
1247
1248 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1249
1250 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1251 struct ieee80211_key_seq seq = {};
1252
1253 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1254 ieee80211_set_key_rx_seq(key, tid, &seq);
1255 }
1256}
1257
1258static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1259 struct ieee80211_key_conf *key)
1260{
1261 int tid;
1262
1263 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1264
1265 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1266 struct ieee80211_key_seq seq = {};
1267
1268 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1269 ieee80211_set_key_rx_seq(key, tid, &seq);
1270 }
1271}
1272
1273static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001274 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001275{
1276 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1277
1278 switch (key->cipher) {
1279 case WLAN_CIPHER_SUITE_CCMP:
1280 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1281 break;
1282 case WLAN_CIPHER_SUITE_TKIP:
1283 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1284 break;
1285 default:
1286 WARN_ON(1);
1287 }
1288}
1289
1290struct iwl_mvm_d3_gtk_iter_data {
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001291 struct iwl_wowlan_status *status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001292 void *last_gtk;
1293 u32 cipher;
1294 bool find_phase, unhandled_cipher;
1295 int num_keys;
1296};
1297
1298static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
1299 struct ieee80211_vif *vif,
1300 struct ieee80211_sta *sta,
1301 struct ieee80211_key_conf *key,
1302 void *_data)
1303{
1304 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1305
1306 if (data->unhandled_cipher)
1307 return;
1308
1309 switch (key->cipher) {
1310 case WLAN_CIPHER_SUITE_WEP40:
1311 case WLAN_CIPHER_SUITE_WEP104:
1312 /* ignore WEP completely, nothing to do */
1313 return;
1314 case WLAN_CIPHER_SUITE_CCMP:
1315 case WLAN_CIPHER_SUITE_TKIP:
1316 /* we support these */
1317 break;
1318 default:
1319 /* everything else (even CMAC for MFP) - disconnect from AP */
1320 data->unhandled_cipher = true;
1321 return;
1322 }
1323
1324 data->num_keys++;
1325
1326 /*
1327 * pairwise key - update sequence counters only;
1328 * note that this assumes no TDLS sessions are active
1329 */
1330 if (sta) {
1331 struct ieee80211_key_seq seq = {};
1332 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1333
1334 if (data->find_phase)
1335 return;
1336
1337 switch (key->cipher) {
1338 case WLAN_CIPHER_SUITE_CCMP:
1339 iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
1340 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
1341 break;
1342 case WLAN_CIPHER_SUITE_TKIP:
1343 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1344 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1345 break;
1346 }
1347 ieee80211_set_key_tx_seq(key, &seq);
1348
1349 /* that's it for this key */
1350 return;
1351 }
1352
1353 if (data->find_phase) {
1354 data->last_gtk = key;
1355 data->cipher = key->cipher;
1356 return;
1357 }
1358
1359 if (data->status->num_of_gtk_rekeys)
1360 ieee80211_remove_key(key);
1361 else if (data->last_gtk == key)
1362 iwl_mvm_set_key_rx_seq(key, data->status);
1363}
1364
1365static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1366 struct ieee80211_vif *vif,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001367 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001368{
1369 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1370 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1371 .status = status,
1372 };
Eliad Peller431591c2013-11-07 14:12:09 +02001373 u32 disconnection_reasons =
1374 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1375 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001376
1377 if (!status || !vif->bss_conf.bssid)
1378 return false;
1379
Eliad Peller431591c2013-11-07 14:12:09 +02001380 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1381 return false;
1382
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001383 /* find last GTK that we used initially, if any */
1384 gtkdata.find_phase = true;
1385 ieee80211_iter_keys(mvm->hw, vif,
1386 iwl_mvm_d3_update_gtks, &gtkdata);
1387 /* not trying to keep connections with MFP/unhandled ciphers */
1388 if (gtkdata.unhandled_cipher)
1389 return false;
1390 if (!gtkdata.num_keys)
Luciano Coelhocfa88892013-11-19 15:38:55 +02001391 goto out;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001392 if (!gtkdata.last_gtk)
1393 return false;
1394
1395 /*
1396 * invalidate all other GTKs that might still exist and update
1397 * the one that we used
1398 */
1399 gtkdata.find_phase = false;
1400 ieee80211_iter_keys(mvm->hw, vif,
1401 iwl_mvm_d3_update_gtks, &gtkdata);
1402
1403 if (status->num_of_gtk_rekeys) {
1404 struct ieee80211_key_conf *key;
1405 struct {
1406 struct ieee80211_key_conf conf;
1407 u8 key[32];
1408 } conf = {
1409 .conf.cipher = gtkdata.cipher,
1410 .conf.keyidx = status->gtk.key_index,
1411 };
1412
1413 switch (gtkdata.cipher) {
1414 case WLAN_CIPHER_SUITE_CCMP:
1415 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1416 memcpy(conf.conf.key, status->gtk.decrypt_key,
1417 WLAN_KEY_LEN_CCMP);
1418 break;
1419 case WLAN_CIPHER_SUITE_TKIP:
1420 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1421 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1422 /* leave TX MIC key zeroed, we don't use it anyway */
1423 memcpy(conf.conf.key +
1424 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1425 status->gtk.tkip_mic_key, 8);
1426 break;
1427 }
1428
1429 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1430 if (IS_ERR(key))
1431 return false;
1432 iwl_mvm_set_key_rx_seq(key, status);
1433 }
1434
1435 if (status->num_of_gtk_rekeys) {
1436 __be64 replay_ctr =
1437 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1438 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1439 (void *)&replay_ctr, GFP_KERNEL);
1440 }
1441
Luciano Coelhocfa88892013-11-19 15:38:55 +02001442out:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001443 mvmvif->seqno_valid = true;
1444 /* +0x10 because the set API expects next-to-use, not last-used */
1445 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1446
1447 return true;
1448}
1449
Johannes Berg7abc4632013-08-06 18:28:26 +02001450/* releases the MVM mutex */
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001451static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
Johannes Berg7abc4632013-08-06 18:28:26 +02001452 struct ieee80211_vif *vif)
1453{
1454 u32 base = mvm->error_event_table;
1455 struct error_table_start {
1456 /* cf. struct iwl_error_event_table */
1457 u32 valid;
1458 u32 error_id;
1459 } err_info;
1460 struct iwl_host_cmd cmd = {
1461 .id = WOWLAN_GET_STATUSES,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001462 .flags = CMD_WANT_SKB,
Johannes Berg7abc4632013-08-06 18:28:26 +02001463 };
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001464 struct iwl_wowlan_status_data status;
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001465 struct iwl_wowlan_status *fw_status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001466 int ret, len, status_size, i;
1467 bool keep;
1468 struct ieee80211_sta *ap_sta;
1469 struct iwl_mvm_sta *mvm_ap_sta;
Johannes Berg7abc4632013-08-06 18:28:26 +02001470
1471 iwl_trans_read_mem_bytes(mvm->trans, base,
1472 &err_info, sizeof(err_info));
1473
1474 if (err_info.valid) {
1475 IWL_INFO(mvm, "error table is valid (%d)\n",
1476 err_info.valid);
1477 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1478 struct cfg80211_wowlan_wakeup wakeup = {
1479 .rfkill_release = true,
1480 };
1481 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1482 GFP_KERNEL);
1483 }
1484 goto out_unlock;
1485 }
1486
1487 /* only for tracing for now */
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001488 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
Johannes Berg7abc4632013-08-06 18:28:26 +02001489 if (ret)
1490 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1491
1492 ret = iwl_mvm_send_cmd(mvm, &cmd);
1493 if (ret) {
1494 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1495 goto out_unlock;
1496 }
1497
1498 /* RF-kill already asserted again... */
1499 if (!cmd.resp_pkt)
1500 goto out_unlock;
1501
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001502 status_size = sizeof(*fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001503
Johannes Berg65b30342014-01-08 13:16:33 +01001504 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1505 if (len < status_size) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001506 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1507 goto out_free_resp;
1508 }
1509
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001510 fw_status = (void *)cmd.resp_pkt->data;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001511
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001512 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1513 for (i = 0; i < 8; i++)
1514 status.qos_seq_ctr[i] =
1515 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1516 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1517 status.wake_packet_length =
1518 le32_to_cpu(fw_status->wake_packet_length);
1519 status.wake_packet_bufsize =
1520 le32_to_cpu(fw_status->wake_packet_bufsize);
1521 status.wake_packet = fw_status->wake_packet;
Johannes Berg7abc4632013-08-06 18:28:26 +02001522
Johannes Berg65b30342014-01-08 13:16:33 +01001523 if (len != status_size + ALIGN(status.wake_packet_bufsize, 4)) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001524 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1525 goto out_free_resp;
1526 }
1527
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001528 /* still at hard-coded place 0 for D3 image */
1529 ap_sta = rcu_dereference_protected(
1530 mvm->fw_id_to_mac_id[0],
1531 lockdep_is_held(&mvm->mutex));
1532 if (IS_ERR_OR_NULL(ap_sta))
1533 goto out_free_resp;
1534
1535 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
1536 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1537 u16 seq = status.qos_seq_ctr[i];
1538 /* firmware stores last-used value, we store next value */
1539 seq += 0x10;
1540 mvm_ap_sta->tid_data[i].seq_number = seq;
1541 }
1542
Johannes Berg7abc4632013-08-06 18:28:26 +02001543 /* now we have all the data we need, unlock to avoid mac80211 issues */
1544 mutex_unlock(&mvm->mutex);
1545
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001546 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1547
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001548 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001549
Johannes Berg9b26b502013-01-22 13:02:09 +01001550 iwl_free_resp(&cmd);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001551 return keep;
Johannes Berg7abc4632013-08-06 18:28:26 +02001552
1553 out_free_resp:
1554 iwl_free_resp(&cmd);
1555 out_unlock:
1556 mutex_unlock(&mvm->mutex);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001557 return false;
Johannes Berg9b26b502013-01-22 13:02:09 +01001558}
1559
Johannes Bergafc66bb2013-05-03 11:44:16 +02001560static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1561{
1562#ifdef CONFIG_IWLWIFI_DEBUGFS
1563 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1564 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1565 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1566
1567 if (!mvm->store_d3_resume_sram)
1568 return;
1569
1570 if (!mvm->d3_resume_sram) {
1571 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1572 if (!mvm->d3_resume_sram)
1573 return;
1574 }
1575
1576 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1577#endif
1578}
1579
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001580static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1581 struct ieee80211_vif *vif)
1582{
1583 /* skip the one we keep connection on */
1584 if (data == vif)
1585 return;
1586
1587 if (vif->type == NL80211_IFTYPE_STATION)
1588 ieee80211_resume_disconnect(vif);
1589}
1590
Johannes Bergdebff612013-05-14 13:53:45 +02001591static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001592{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001593 struct iwl_d3_iter_data resume_iter_data = {
1594 .mvm = mvm,
1595 };
1596 struct ieee80211_vif *vif = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001597 int ret;
1598 enum iwl_d3_status d3_status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001599 bool keep = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001600
1601 mutex_lock(&mvm->mutex);
1602
1603 /* get the BSS vif pointer again */
1604 ieee80211_iterate_active_interfaces_atomic(
1605 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1606 iwl_mvm_d3_iface_iterator, &resume_iter_data);
1607
1608 if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
1609 goto out_unlock;
1610
1611 vif = resume_iter_data.vif;
1612
Johannes Bergdebff612013-05-14 13:53:45 +02001613 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001614 if (ret)
1615 goto out_unlock;
1616
1617 if (d3_status != IWL_D3_STATUS_ALIVE) {
1618 IWL_INFO(mvm, "Device was reset during suspend\n");
1619 goto out_unlock;
1620 }
1621
Johannes Bergafc66bb2013-05-03 11:44:16 +02001622 /* query SRAM first in case we want event logging */
1623 iwl_mvm_read_d3_sram(mvm);
1624
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001625 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
Eliad Peller78c9df62013-11-07 14:13:30 +02001626#ifdef CONFIG_IWLWIFI_DEBUGFS
1627 if (keep)
1628 mvm->keep_vif = vif;
1629#endif
Johannes Berg7abc4632013-08-06 18:28:26 +02001630 /* has unlocked the mutex, so skip that */
1631 goto out;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001632
1633 out_unlock:
1634 mutex_unlock(&mvm->mutex);
1635
Johannes Berg7abc4632013-08-06 18:28:26 +02001636 out:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001637 if (!test)
1638 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1639 IEEE80211_IFACE_ITER_NORMAL,
1640 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001641
1642 /* return 1 to reconfigure the device */
1643 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1644 return 1;
1645}
1646
Johannes Bergdebff612013-05-14 13:53:45 +02001647int iwl_mvm_resume(struct ieee80211_hw *hw)
1648{
1649 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1650
Eliad Pellerd15a7472014-03-27 18:53:12 +02001651 if (iwl_mvm_is_d0i3_supported(mvm)) {
1652 bool exit_now;
1653
1654 mutex_lock(&mvm->d0i3_suspend_mutex);
1655 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1656 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1657 &mvm->d0i3_suspend_flags);
1658 mutex_unlock(&mvm->d0i3_suspend_mutex);
1659 if (exit_now)
1660 _iwl_mvm_exit_d0i3(mvm);
1661 return 0;
1662 }
1663
Johannes Bergdebff612013-05-14 13:53:45 +02001664 return __iwl_mvm_resume(mvm, false);
1665}
1666
Johannes Berg8ca151b2013-01-24 14:25:36 +01001667void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1668{
1669 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1670
1671 device_set_wakeup_enable(mvm->trans->dev, enabled);
1672}
Johannes Bergdebff612013-05-14 13:53:45 +02001673
1674#ifdef CONFIG_IWLWIFI_DEBUGFS
1675static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
1676{
1677 struct iwl_mvm *mvm = inode->i_private;
1678 int err;
1679
1680 if (mvm->d3_test_active)
1681 return -EBUSY;
1682
1683 file->private_data = inode->i_private;
1684
1685 ieee80211_stop_queues(mvm->hw);
1686 synchronize_net();
1687
1688 /* start pseudo D3 */
1689 rtnl_lock();
1690 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
1691 rtnl_unlock();
1692 if (err > 0)
1693 err = -EINVAL;
1694 if (err) {
1695 ieee80211_wake_queues(mvm->hw);
1696 return err;
1697 }
1698 mvm->d3_test_active = true;
Eliad Peller78c9df62013-11-07 14:13:30 +02001699 mvm->keep_vif = NULL;
Johannes Bergdebff612013-05-14 13:53:45 +02001700 return 0;
1701}
1702
1703static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
1704 size_t count, loff_t *ppos)
1705{
1706 struct iwl_mvm *mvm = file->private_data;
1707 u32 pme_asserted;
1708
1709 while (true) {
Eliad Peller4ed80a82013-11-07 13:23:21 +02001710 /* read pme_ptr if available */
1711 if (mvm->d3_test_pme_ptr) {
1712 pme_asserted = iwl_trans_read_mem32(mvm->trans,
1713 mvm->d3_test_pme_ptr);
1714 if (pme_asserted)
1715 break;
1716 }
1717
Johannes Bergdebff612013-05-14 13:53:45 +02001718 if (msleep_interruptible(100))
1719 break;
1720 }
1721
1722 return 0;
1723}
1724
1725static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
1726 struct ieee80211_vif *vif)
1727{
Eliad Peller78c9df62013-11-07 14:13:30 +02001728 /* skip the one we keep connection on */
1729 if (_data == vif)
1730 return;
1731
Johannes Bergdebff612013-05-14 13:53:45 +02001732 if (vif->type == NL80211_IFTYPE_STATION)
1733 ieee80211_connection_loss(vif);
1734}
1735
1736static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
1737{
1738 struct iwl_mvm *mvm = inode->i_private;
1739 int remaining_time = 10;
1740
1741 mvm->d3_test_active = false;
1742 __iwl_mvm_resume(mvm, true);
1743 iwl_abort_notification_waits(&mvm->notif_wait);
1744 ieee80211_restart_hw(mvm->hw);
1745
1746 /* wait for restart and disconnect all interfaces */
1747 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1748 remaining_time > 0) {
1749 remaining_time--;
1750 msleep(1000);
1751 }
1752
1753 if (remaining_time == 0)
1754 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
1755
1756 ieee80211_iterate_active_interfaces_atomic(
1757 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
Eliad Peller78c9df62013-11-07 14:13:30 +02001758 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
Johannes Bergdebff612013-05-14 13:53:45 +02001759
1760 ieee80211_wake_queues(mvm->hw);
1761
1762 return 0;
1763}
1764
1765const struct file_operations iwl_dbgfs_d3_test_ops = {
1766 .llseek = no_llseek,
1767 .open = iwl_mvm_d3_test_open,
1768 .read = iwl_mvm_d3_test_read,
1769 .release = iwl_mvm_d3_test_release,
1770};
1771#endif