blob: 9e51843764ca726ca8b23978afab84bbec6e79b0 [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.
Luciano Coelho65ff5562015-03-20 13:35:47 +02009 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020026 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010027 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Luciano Coelho65ff5562015-03-20 13:35:47 +020035 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010036 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65
Johannes Bergf444eb12013-02-26 12:04:18 +010066#include <linux/etherdevice.h>
Johannes Bergf0c26462013-01-22 20:41:58 +010067#include <linux/ip.h>
Johannes Bergdebff612013-05-14 13:53:45 +020068#include <linux/fs.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010069#include <net/cfg80211.h>
70#include <net/ipv6.h>
Johannes Bergf0c26462013-01-22 20:41:58 +010071#include <net/tcp.h>
Johannes Bergf7fc5982013-08-20 13:04:10 +020072#include <net/addrconf.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010073#include "iwl-modparams.h"
74#include "fw-api.h"
75#include "mvm.h"
76
77void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
78 struct ieee80211_vif *vif,
79 struct cfg80211_gtk_rekey_data *data)
80{
81 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
82 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
83
84 if (iwlwifi_mod_params.sw_crypto)
85 return;
86
87 mutex_lock(&mvm->mutex);
88
89 memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
90 memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
91 mvmvif->rekey_data.replay_ctr =
92 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
93 mvmvif->rekey_data.valid = true;
94
95 mutex_unlock(&mvm->mutex);
96}
97
98#if IS_ENABLED(CONFIG_IPV6)
99void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
100 struct ieee80211_vif *vif,
101 struct inet6_dev *idev)
102{
103 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
104 struct inet6_ifaddr *ifa;
105 int idx = 0;
106
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200107 read_lock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100108 list_for_each_entry(ifa, &idev->addr_list, if_list) {
109 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
110 idx++;
Johannes Berg5369d6c2013-07-08 11:17:06 +0200111 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100112 break;
113 }
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200114 read_unlock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100115
116 mvmvif->num_target_ipv6_addrs = idx;
117}
118#endif
119
120void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
121 struct ieee80211_vif *vif, int idx)
122{
123 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
124
125 mvmvif->tx_key_idx = idx;
126}
127
128static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
129{
130 int i;
131
132 for (i = 0; i < IWL_P1K_SIZE; i++)
133 out[i] = cpu_to_le16(p1k[i]);
134}
135
136struct wowlan_key_data {
137 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
138 struct iwl_wowlan_tkip_params_cmd *tkip;
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200139 bool error, use_rsc_tsc, use_tkip, configure_keys;
Johannes Berg5312e542013-07-22 18:26:56 +0200140 int wep_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100141};
142
143static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
144 struct ieee80211_vif *vif,
145 struct ieee80211_sta *sta,
146 struct ieee80211_key_conf *key,
147 void *_data)
148{
149 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
150 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
151 struct wowlan_key_data *data = _data;
152 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
153 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
154 struct iwl_p1k_cache *rx_p1ks;
155 u8 *rx_mic_key;
156 struct ieee80211_key_seq seq;
157 u32 cur_rx_iv32 = 0;
158 u16 p1k[IWL_P1K_SIZE];
159 int ret, i;
160
Johannes Berg8ca151b2013-01-24 14:25:36 +0100161 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
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200196 if (data->configure_keys) {
197 mutex_lock(&mvm->mutex);
198 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0,
199 sizeof(wkc), &wkc);
200 data->error = ret != 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100201
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200202 mvm->ptk_ivlen = key->iv_len;
203 mvm->ptk_icvlen = key->icv_len;
204 mvm->gtk_ivlen = key->iv_len;
205 mvm->gtk_icvlen = key->icv_len;
206 mutex_unlock(&mvm->mutex);
207 }
Johannes Bergf444eb12013-02-26 12:04:18 +0100208
Johannes Berg8ca151b2013-01-24 14:25:36 +0100209 /* don't upload key again */
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200210 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100211 }
212 default:
213 data->error = true;
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200214 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100215 case WLAN_CIPHER_SUITE_AES_CMAC:
216 /*
217 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
218 * but we also shouldn't abort suspend due to that. It does have
219 * support for the IGTK key renewal, but doesn't really use the
220 * IGTK for anything. This means we could spuriously wake up or
221 * be deauthenticated, but that was considered acceptable.
222 */
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200223 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100224 case WLAN_CIPHER_SUITE_TKIP:
225 if (sta) {
226 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
227 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
228
229 rx_p1ks = data->tkip->rx_uni;
230
231 ieee80211_get_key_tx_seq(key, &seq);
232 tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
233 tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
234
235 ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
236 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
237
238 memcpy(data->tkip->mic_keys.tx,
239 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
240 IWL_MIC_KEY_SIZE);
241
242 rx_mic_key = data->tkip->mic_keys.rx_unicast;
243 } else {
244 tkip_sc =
245 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
246 rx_p1ks = data->tkip->rx_multi;
247 rx_mic_key = data->tkip->mic_keys.rx_mcast;
248 }
249
250 /*
251 * For non-QoS this relies on the fact that both the uCode and
252 * mac80211 use TID 0 (as they need to to avoid replay attacks)
253 * for checking the IV in the frames.
254 */
255 for (i = 0; i < IWL_NUM_RSC; i++) {
256 ieee80211_get_key_rx_seq(key, i, &seq);
257 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
258 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
259 /* wrapping isn't allowed, AP must rekey */
260 if (seq.tkip.iv32 > cur_rx_iv32)
261 cur_rx_iv32 = seq.tkip.iv32;
262 }
263
264 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
265 cur_rx_iv32, p1k);
266 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
267 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
268 cur_rx_iv32 + 1, p1k);
269 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
270
271 memcpy(rx_mic_key,
272 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
273 IWL_MIC_KEY_SIZE);
274
275 data->use_tkip = true;
276 data->use_rsc_tsc = true;
277 break;
278 case WLAN_CIPHER_SUITE_CCMP:
279 if (sta) {
Johannes Berg6645d5e2015-09-15 09:58:53 +0200280 u64 pn64;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100281
282 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
283 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
284
Johannes Berg6645d5e2015-09-15 09:58:53 +0200285 pn64 = atomic64_read(&key->tx_pn);
286 aes_tx_sc->pn = cpu_to_le64(pn64);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100287 } 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);
Johannes Berg2cf5eb32015-09-15 14:36:09 +0200299 aes_sc[i].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));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100305 }
306 data->use_rsc_tsc = true;
307 break;
308 }
309
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200310 if (data->configure_keys) {
311 mutex_lock(&mvm->mutex);
Johannes Berg5312e542013-07-22 18:26:56 +0200312 /*
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200313 * The D3 firmware hardcodes the key offset 0 as the key it
314 * uses to transmit packets to the AP, i.e. the PTK.
Johannes Berg5312e542013-07-22 18:26:56 +0200315 */
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200316 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
317 mvm->ptk_ivlen = key->iv_len;
318 mvm->ptk_icvlen = key->icv_len;
319 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
320 } else {
321 /*
322 * firmware only supports TSC/RSC for a single key,
323 * so if there are multiple keep overwriting them
324 * with new ones -- this relies on mac80211 doing
325 * list_add_tail().
326 */
327 mvm->gtk_ivlen = key->iv_len;
328 mvm->gtk_icvlen = key->icv_len;
329 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
330 }
331 mutex_unlock(&mvm->mutex);
332 data->error = ret != 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100333 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100334}
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 +0100602static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
603 struct ieee80211_sta *ap_sta)
604{
605 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
606 struct ieee80211_chanctx_conf *ctx;
607 u8 chains_static, chains_dynamic;
608 struct cfg80211_chan_def chandef;
609 int ret, i;
610 struct iwl_binding_cmd binding_cmd = {};
611 struct iwl_time_quota_cmd quota_cmd = {};
612 u32 status;
613
614 /* add back the PHY */
615 if (WARN_ON(!mvmvif->phy_ctxt))
616 return -EINVAL;
617
618 rcu_read_lock();
619 ctx = rcu_dereference(vif->chanctx_conf);
620 if (WARN_ON(!ctx)) {
621 rcu_read_unlock();
622 return -EINVAL;
623 }
624 chandef = ctx->def;
625 chains_static = ctx->rx_chains_static;
626 chains_dynamic = ctx->rx_chains_dynamic;
627 rcu_read_unlock();
628
629 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
630 chains_static, chains_dynamic);
631 if (ret)
632 return ret;
633
634 /* add back the MAC */
635 mvmvif->uploaded = false;
636
637 if (WARN_ON(!vif->bss_conf.assoc))
638 return -EINVAL;
Luciano Coelhobca49d92014-05-13 17:33:38 +0300639
Johannes Berg8ca151b2013-01-24 14:25:36 +0100640 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100641 if (ret)
642 return ret;
643
644 /* add back binding - XXX refactor? */
645 binding_cmd.id_and_color =
646 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
647 mvmvif->phy_ctxt->color));
648 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
649 binding_cmd.phy =
650 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
651 mvmvif->phy_ctxt->color));
652 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
653 mvmvif->color));
654 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
655 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
656
657 status = 0;
658 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
659 sizeof(binding_cmd), &binding_cmd,
660 &status);
661 if (ret) {
662 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
663 return ret;
664 }
665
666 if (status) {
667 IWL_ERR(mvm, "Binding command failed: %u\n", status);
668 return -EIO;
669 }
670
Johannes Berg7a453972013-02-12 13:10:44 +0100671 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100672 if (ret)
673 return ret;
674 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
675
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200676 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100677 if (ret)
678 return ret;
679
680 /* and some quota */
681 quota_cmd.quotas[0].id_and_color =
682 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
683 mvmvif->phy_ctxt->color));
Emmanuel Grumbach9a3daf82014-03-05 10:00:44 +0200684 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
685 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100686
687 for (i = 1; i < MAX_BINDINGS; i++)
688 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
689
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300690 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100691 sizeof(quota_cmd), &quota_cmd);
692 if (ret)
693 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
694
Jonathan Doron47c8b152014-11-27 16:55:25 +0200695 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
696 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
697
Johannes Berg8ca151b2013-01-24 14:25:36 +0100698 return 0;
699}
700
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200701static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
702 struct ieee80211_vif *vif)
703{
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200704 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
705 struct iwl_nonqos_seq_query_cmd query_cmd = {
706 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
707 .mac_id_n_color =
708 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
709 mvmvif->color)),
710 };
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200711 struct iwl_host_cmd cmd = {
712 .id = NON_QOS_TX_COUNTER_CMD,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300713 .flags = CMD_WANT_SKB,
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200714 };
715 int err;
716 u32 size;
717
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300718 cmd.data[0] = &query_cmd;
719 cmd.len[0] = sizeof(query_cmd);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200720
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200721 err = iwl_mvm_send_cmd(mvm, &cmd);
722 if (err)
723 return err;
724
Johannes Berg65b30342014-01-08 13:16:33 +0100725 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200726 if (size < sizeof(__le16)) {
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200727 err = -EINVAL;
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200728 } else {
729 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300730 /* firmware returns next, not last-used seqno */
731 err = (u16) (err - 0x10);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200732 }
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200733
734 iwl_free_resp(&cmd);
735 return err;
736}
737
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200738void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
739{
740 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
741 struct iwl_nonqos_seq_query_cmd query_cmd = {
742 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
743 .mac_id_n_color =
744 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
745 mvmvif->color)),
746 .value = cpu_to_le16(mvmvif->seqno),
747 };
748
749 /* return if called during restart, not resume from D3 */
750 if (!mvmvif->seqno_valid)
751 return;
752
753 mvmvif->seqno_valid = false;
754
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300755 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200756 sizeof(query_cmd), &query_cmd))
757 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
758}
759
Luciano Coelho671b5822014-09-22 10:59:53 +0300760static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200761{
Luciano Coelhoc7d42482015-05-07 16:00:26 +0300762 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100763
764 iwl_trans_stop_device(mvm->trans);
765
766 /*
767 * Set the HW restart bit -- this is mostly true as we're
768 * going to load new firmware and reprogram that, though
769 * the reprogramming is going to be manual to avoid adding
770 * all the MACs that aren't support.
771 * We don't have to clear up everything though because the
772 * reprogramming is manual. When we resume, we'll actually
773 * go through a proper restart sequence again to switch
774 * back to the runtime firmware image.
775 */
776 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
777
Johannes Bergf444eb12013-02-26 12:04:18 +0100778 mvm->ptk_ivlen = 0;
779 mvm->ptk_icvlen = 0;
780 mvm->ptk_ivlen = 0;
781 mvm->ptk_icvlen = 0;
782
Luciano Coelho671b5822014-09-22 10:59:53 +0300783 return iwl_mvm_load_d3_fw(mvm);
784}
785
786static int
Luciano Coelho671b5822014-09-22 10:59:53 +0300787iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
788 struct cfg80211_wowlan *wowlan,
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200789 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
Luciano Coelho671b5822014-09-22 10:59:53 +0300790 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
791 struct ieee80211_sta *ap_sta)
792{
793 int ret;
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100794 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
Luciano Coelho671b5822014-09-22 10:59:53 +0300795
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200796 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
Luciano Coelho671b5822014-09-22 10:59:53 +0300797
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200798 wowlan_config_cmd->is_11n_connection =
Luciano Coelho671b5822014-09-22 10:59:53 +0300799 ap_sta->ht_cap.ht_supported;
800
801 /* Query the last used seqno and set it */
802 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
803 if (ret < 0)
804 return ret;
805
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200806 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
Luciano Coelho671b5822014-09-22 10:59:53 +0300807
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200808 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
Luciano Coelho671b5822014-09-22 10:59:53 +0300809
810 if (wowlan->disconnect)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200811 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300812 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
813 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
814 if (wowlan->magic_pkt)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200815 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300816 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
817 if (wowlan->gtk_rekey_failure)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200818 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300819 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
820 if (wowlan->eap_identity_req)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200821 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300822 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
823 if (wowlan->four_way_handshake)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200824 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300825 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
826 if (wowlan->n_patterns)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200827 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300828 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
829
830 if (wowlan->rfkill_release)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200831 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300832 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
833
834 if (wowlan->tcp) {
835 /*
836 * Set the "link change" (really "link lost") flag as well
837 * since that implies losing the TCP connection.
838 */
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200839 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300840 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
841 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
842 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
843 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
844 }
845
846 return 0;
847}
848
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200849int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
850 struct ieee80211_vif *vif,
851 bool configure_keys,
852 u32 cmd_flags)
853{
854 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
855 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
856 struct wowlan_key_data key_data = {
857 .configure_keys = configure_keys,
858 .use_rsc_tsc = false,
859 .tkip = &tkip_cmd,
860 .use_tkip = false,
861 };
862 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
863 int ret;
864
865 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
866 if (!key_data.rsc_tsc)
867 return -ENOMEM;
868
869 /*
870 * Note that currently we don't propagate cmd_flags
871 * to the iterator. In case of key_data.configure_keys,
872 * all the configured commands are SYNC, and
873 * iwl_mvm_wowlan_program_keys() will take care of
874 * locking/unlocking mvm->mutex.
875 */
876 ieee80211_iter_keys(mvm->hw, vif,
877 iwl_mvm_wowlan_program_keys,
878 &key_data);
879
880 if (key_data.error) {
881 ret = -EIO;
882 goto out;
883 }
884
885 if (key_data.use_rsc_tsc) {
886 ret = iwl_mvm_send_cmd_pdu(mvm,
887 WOWLAN_TSC_RSC_PARAM, cmd_flags,
888 sizeof(*key_data.rsc_tsc),
889 key_data.rsc_tsc);
890 if (ret)
891 goto out;
892 }
893
894 if (key_data.use_tkip) {
895 ret = iwl_mvm_send_cmd_pdu(mvm,
896 WOWLAN_TKIP_PARAM,
897 cmd_flags, sizeof(tkip_cmd),
898 &tkip_cmd);
899 if (ret)
900 goto out;
901 }
902
903 if (mvmvif->rekey_data.valid) {
904 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
905 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
906 NL80211_KCK_LEN);
907 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
908 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
909 NL80211_KEK_LEN);
910 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
911 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
912
913 ret = iwl_mvm_send_cmd_pdu(mvm,
914 WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
915 sizeof(kek_kck_cmd),
916 &kek_kck_cmd);
917 if (ret)
918 goto out;
919 }
920out:
921 kfree(key_data.rsc_tsc);
922 return ret;
923}
924
Luciano Coelho671b5822014-09-22 10:59:53 +0300925static int
926iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
927 struct cfg80211_wowlan *wowlan,
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200928 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
Luciano Coelho671b5822014-09-22 10:59:53 +0300929 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
930 struct ieee80211_sta *ap_sta)
931{
Luciano Coelho671b5822014-09-22 10:59:53 +0300932 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100933
Luciano Coelho3acc9522014-11-19 22:35:37 +0200934 ret = iwl_mvm_switch_to_d3(mvm);
935 if (ret)
936 return ret;
937
Johannes Berg8ca151b2013-01-24 14:25:36 +0100938 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
939 if (ret)
Luciano Coelho671b5822014-09-22 10:59:53 +0300940 return ret;
941
Johannes Berg8ca151b2013-01-24 14:25:36 +0100942 if (!iwlwifi_mod_params.sw_crypto) {
943 /*
944 * This needs to be unlocked due to lock ordering
945 * constraints. Since we're in the suspend path
946 * that isn't really a problem though.
947 */
948 mutex_unlock(&mvm->mutex);
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200949 iwl_mvm_wowlan_config_key_params(mvm, vif, true, CMD_ASYNC);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100950 mutex_lock(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100951 }
952
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200953 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
954 sizeof(*wowlan_config_cmd),
955 wowlan_config_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100956 if (ret)
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200957 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100958
959 ret = iwl_mvm_send_patterns(mvm, wowlan);
960 if (ret)
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200961 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100962
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300963 ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100964 if (ret)
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200965 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100966
Johannes Bergf0c26462013-01-22 20:41:58 +0100967 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
Luciano Coelho671b5822014-09-22 10:59:53 +0300968 return ret;
969}
970
Luciano Coelho3acc9522014-11-19 22:35:37 +0200971static int
972iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
973 struct cfg80211_wowlan *wowlan,
974 struct cfg80211_sched_scan_request *nd_config,
975 struct ieee80211_vif *vif)
976{
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200977 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
Luciano Coelho3acc9522014-11-19 22:35:37 +0200978 int ret;
979
980 ret = iwl_mvm_switch_to_d3(mvm);
981 if (ret)
982 return ret;
983
984 /* rfkill release can be either for wowlan or netdetect */
985 if (wowlan->rfkill_release)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200986 wowlan_config_cmd.wakeup_filter |=
Luciano Coelho3acc9522014-11-19 22:35:37 +0200987 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
988
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200989 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
990 sizeof(wowlan_config_cmd),
991 &wowlan_config_cmd);
Luciano Coelho3acc9522014-11-19 22:35:37 +0200992 if (ret)
993 return ret;
994
Luciano Coelho19945df2015-03-20 16:11:28 +0200995 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
996 IWL_MVM_SCAN_NETDETECT);
Luciano Coelhod9718da2014-11-21 09:32:23 +0200997 if (ret)
998 return ret;
Luciano Coelho3acc9522014-11-19 22:35:37 +0200999
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001000 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
Luciano Coelhod9718da2014-11-21 09:32:23 +02001001 return -EBUSY;
1002
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001003 /* save the sched scan matchsets... */
Luciano Coelhod9718da2014-11-21 09:32:23 +02001004 if (nd_config->n_match_sets) {
1005 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1006 sizeof(*nd_config->match_sets) *
1007 nd_config->n_match_sets,
1008 GFP_KERNEL);
1009 if (mvm->nd_match_sets)
1010 mvm->n_nd_match_sets = nd_config->n_match_sets;
1011 }
1012
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001013 /* ...and the sched scan channels for later reporting */
1014 mvm->nd_channels = kmemdup(nd_config->channels,
1015 sizeof(*nd_config->channels) *
1016 nd_config->n_channels,
1017 GFP_KERNEL);
1018 if (mvm->nd_channels)
1019 mvm->n_nd_channels = nd_config->n_channels;
1020
Luciano Coelhod9718da2014-11-21 09:32:23 +02001021 return 0;
1022}
1023
1024static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1025{
1026 kfree(mvm->nd_match_sets);
1027 mvm->nd_match_sets = NULL;
1028 mvm->n_nd_match_sets = 0;
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001029 kfree(mvm->nd_channels);
1030 mvm->nd_channels = NULL;
1031 mvm->n_nd_channels = 0;
Luciano Coelho3acc9522014-11-19 22:35:37 +02001032}
1033
Luciano Coelho671b5822014-09-22 10:59:53 +03001034static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1035 struct cfg80211_wowlan *wowlan,
1036 bool test)
1037{
1038 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Luciano Coelho671b5822014-09-22 10:59:53 +03001039 struct ieee80211_vif *vif = NULL;
1040 struct iwl_mvm_vif *mvmvif = NULL;
1041 struct ieee80211_sta *ap_sta = NULL;
Luciano Coelho671b5822014-09-22 10:59:53 +03001042 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1043 /*
1044 * Program the minimum sleep time to 10 seconds, as many
1045 * platforms have issues processing a wakeup signal while
1046 * still being in the process of suspending.
1047 */
1048 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1049 };
1050 struct iwl_host_cmd d3_cfg_cmd = {
1051 .id = D3_CONFIG_CMD,
1052 .flags = CMD_WANT_SKB,
1053 .data[0] = &d3_cfg_cmd_data,
1054 .len[0] = sizeof(d3_cfg_cmd_data),
1055 };
1056 int ret;
1057 int len __maybe_unused;
1058
1059 if (!wowlan) {
1060 /*
1061 * mac80211 shouldn't get here, but for D3 test
1062 * it doesn't warrant a warning
1063 */
1064 WARN_ON(!test);
1065 return -EINVAL;
1066 }
1067
1068 mutex_lock(&mvm->mutex);
1069
Luciano Coelho7f549e22014-10-02 15:38:04 +03001070 vif = iwl_mvm_get_bss_vif(mvm);
1071 if (IS_ERR_OR_NULL(vif)) {
Luciano Coelho671b5822014-09-22 10:59:53 +03001072 ret = 1;
1073 goto out_noreset;
1074 }
1075
Luciano Coelho671b5822014-09-22 10:59:53 +03001076 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1077
Luciano Coelho3acc9522014-11-19 22:35:37 +02001078 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
1079 /* if we're not associated, this must be netdetect */
Luca Coelho566f1652015-10-25 22:55:32 +02001080 if (!wowlan->nd_config) {
Luciano Coelho3acc9522014-11-19 22:35:37 +02001081 ret = 1;
1082 goto out_noreset;
1083 }
1084
1085 ret = iwl_mvm_netdetect_config(
Luca Coelho566f1652015-10-25 22:55:32 +02001086 mvm, wowlan, wowlan->nd_config, vif);
Luciano Coelho3acc9522014-11-19 22:35:37 +02001087 if (ret)
1088 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001089
1090 mvm->net_detect = true;
Luciano Coelho3acc9522014-11-19 22:35:37 +02001091 } else {
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +02001092 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
Luciano Coelho3acc9522014-11-19 22:35:37 +02001093
Luciano Coelho1e4d19c2014-09-22 13:01:59 +03001094 ap_sta = rcu_dereference_protected(
1095 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1096 lockdep_is_held(&mvm->mutex));
1097 if (IS_ERR_OR_NULL(ap_sta)) {
1098 ret = -EINVAL;
1099 goto out_noreset;
1100 }
1101
1102 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1103 vif, mvmvif, ap_sta);
1104 if (ret)
1105 goto out_noreset;
Luciano Coelho1e4d19c2014-09-22 13:01:59 +03001106 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1107 vif, mvmvif, ap_sta);
1108 if (ret)
1109 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001110
1111 mvm->net_detect = false;
Luciano Coelho671b5822014-09-22 10:59:53 +03001112 }
1113
Emmanuel Grumbachc1cb92f2014-01-28 10:17:18 +02001114 ret = iwl_mvm_power_update_device(mvm);
Alexander Bondar64b928c2013-09-03 14:18:03 +03001115 if (ret)
1116 goto out;
1117
Arik Nemtsov999609f2014-05-15 17:31:51 +03001118 ret = iwl_mvm_power_update_mac(mvm);
Alexander Bondaree1e8422013-04-23 13:52:10 +03001119 if (ret)
1120 goto out;
1121
Johannes Bergb0114712013-06-12 14:55:40 +02001122#ifdef CONFIG_IWLWIFI_DEBUGFS
1123 if (mvm->d3_wake_sysassert)
1124 d3_cfg_cmd_data.wakeup_flags |=
1125 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1126#endif
1127
Johannes Berg8ca151b2013-01-24 14:25:36 +01001128 /* must be last -- this switches firmware state */
Johannes Bergdebff612013-05-14 13:53:45 +02001129 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001130 if (ret)
1131 goto out;
Johannes Bergdebff612013-05-14 13:53:45 +02001132#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg65b30342014-01-08 13:16:33 +01001133 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1134 if (len >= sizeof(u32)) {
Johannes Bergdebff612013-05-14 13:53:45 +02001135 mvm->d3_test_pme_ptr =
1136 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
Johannes Bergdebff612013-05-14 13:53:45 +02001137 }
1138#endif
1139 iwl_free_resp(&d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001140
1141 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1142
Johannes Bergdebff612013-05-14 13:53:45 +02001143 iwl_trans_d3_suspend(mvm->trans, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001144 out:
Luciano Coelhod9718da2014-11-21 09:32:23 +02001145 if (ret < 0) {
Eliad Peller48e67752015-03-03 12:03:20 +02001146 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001147 ieee80211_restart_hw(mvm->hw);
Luciano Coelhod9718da2014-11-21 09:32:23 +02001148 iwl_mvm_free_nd(mvm);
1149 }
Luciano Coelhob70ab722013-11-19 22:53:41 +02001150 out_noreset:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001151 mutex_unlock(&mvm->mutex);
1152
1153 return ret;
1154}
1155
Eliad Peller67359432014-12-09 15:23:54 +02001156static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
1157{
1158 struct iwl_notification_wait wait_d3;
Sara Sharon6eb031d2015-07-13 14:50:47 +03001159 static const u16 d3_notif[] = { D3_CONFIG_CMD };
Eliad Peller67359432014-12-09 15:23:54 +02001160 int ret;
1161
1162 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
1163 d3_notif, ARRAY_SIZE(d3_notif),
1164 NULL, NULL);
1165
1166 ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
1167 if (ret)
1168 goto remove_notif;
1169
1170 ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
1171 WARN_ON_ONCE(ret);
1172 return ret;
1173
1174remove_notif:
1175 iwl_remove_notification(&mvm->notif_wait, &wait_d3);
1176 return ret;
1177}
1178
Johannes Bergdebff612013-05-14 13:53:45 +02001179int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1180{
Eliad Pellerd15a7472014-03-27 18:53:12 +02001181 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Eliad Pellerc43fe902015-06-04 21:59:32 +03001182 int ret;
Eliad Pellerd15a7472014-03-27 18:53:12 +02001183
Luciano Coelhocf8c3cf2015-08-24 14:53:25 +03001184 /* make sure the d0i3 exit work is not pending */
1185 flush_work(&mvm->d0i3_exit_work);
1186
Eliad Pellerc43fe902015-06-04 21:59:32 +03001187 ret = iwl_trans_suspend(mvm->trans);
1188 if (ret)
1189 return ret;
1190
Eliad Peller54154612015-05-28 16:50:12 +03001191 mvm->trans->wowlan_d0i3 = wowlan->any;
1192 if (mvm->trans->wowlan_d0i3) {
Eliad Peller91742442014-12-09 15:54:46 +02001193 /* 'any' trigger means d0i3 usage */
Eliad Peller67359432014-12-09 15:23:54 +02001194 if (mvm->trans->d0i3_mode == IWL_D0I3_MODE_ON_SUSPEND) {
Eliad Pellerc43fe902015-06-04 21:59:32 +03001195 ret = iwl_mvm_enter_d0i3_sync(mvm);
Eliad Peller67359432014-12-09 15:23:54 +02001196
1197 if (ret)
1198 return ret;
1199 }
1200
Eliad Pellerd15a7472014-03-27 18:53:12 +02001201 mutex_lock(&mvm->d0i3_suspend_mutex);
1202 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1203 mutex_unlock(&mvm->d0i3_suspend_mutex);
Eliad Peller6dfb36c2015-07-09 14:17:24 +03001204
1205 iwl_trans_d3_suspend(mvm->trans, false);
1206
Eliad Pellerd15a7472014-03-27 18:53:12 +02001207 return 0;
1208 }
1209
Johannes Bergdebff612013-05-14 13:53:45 +02001210 return __iwl_mvm_suspend(hw, wowlan, false);
1211}
1212
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001213/* converted data from the different status responses */
1214struct iwl_wowlan_status_data {
1215 u16 pattern_number;
1216 u16 qos_seq_ctr[8];
1217 u32 wakeup_reasons;
1218 u32 wake_packet_length;
1219 u32 wake_packet_bufsize;
1220 const u8 *wake_packet;
1221};
1222
Johannes Berg7abc4632013-08-06 18:28:26 +02001223static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1224 struct ieee80211_vif *vif,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001225 struct iwl_wowlan_status_data *status)
Johannes Berg9b26b502013-01-22 13:02:09 +01001226{
Johannes Berg7abc4632013-08-06 18:28:26 +02001227 struct sk_buff *pkt = NULL;
Johannes Berg9b26b502013-01-22 13:02:09 +01001228 struct cfg80211_wowlan_wakeup wakeup = {
1229 .pattern_idx = -1,
1230 };
1231 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001232 u32 reasons = status->wakeup_reasons;
Johannes Berg9b26b502013-01-22 13:02:09 +01001233
1234 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1235 wakeup_report = NULL;
1236 goto report;
1237 }
1238
Derek Basehore06ae2ad2015-11-04 17:37:52 -08001239 pm_wakeup_event(mvm->dev, 0);
1240
Johannes Bergf444eb12013-02-26 12:04:18 +01001241 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
Johannes Berg9b26b502013-01-22 13:02:09 +01001242 wakeup.magic_pkt = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001243
Johannes Bergf444eb12013-02-26 12:04:18 +01001244 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
Johannes Berg9b26b502013-01-22 13:02:09 +01001245 wakeup.pattern_idx =
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001246 status->pattern_number;
Johannes Berg9b26b502013-01-22 13:02:09 +01001247
1248 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1249 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1250 wakeup.disconnect = true;
1251
Johannes Bergf444eb12013-02-26 12:04:18 +01001252 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001253 wakeup.gtk_rekey_failure = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001254
Johannes Bergf444eb12013-02-26 12:04:18 +01001255 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
Johannes Berg9b26b502013-01-22 13:02:09 +01001256 wakeup.rfkill_release = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001257
Johannes Bergf444eb12013-02-26 12:04:18 +01001258 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
Johannes Berg9b26b502013-01-22 13:02:09 +01001259 wakeup.eap_identity_req = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001260
Johannes Bergf444eb12013-02-26 12:04:18 +01001261 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001262 wakeup.four_way_handshake = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001263
Johannes Bergf0c26462013-01-22 20:41:58 +01001264 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1265 wakeup.tcp_connlost = true;
1266
1267 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1268 wakeup.tcp_nomoretokens = true;
1269
1270 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1271 wakeup.tcp_match = true;
1272
Johannes Berg9b26b502013-01-22 13:02:09 +01001273 if (status->wake_packet_bufsize) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001274 int pktsize = status->wake_packet_bufsize;
1275 int pktlen = status->wake_packet_length;
Johannes Bergf444eb12013-02-26 12:04:18 +01001276 const u8 *pktdata = status->wake_packet;
1277 struct ieee80211_hdr *hdr = (void *)pktdata;
1278 int truncated = pktlen - pktsize;
Johannes Berg9b26b502013-01-22 13:02:09 +01001279
Johannes Bergf444eb12013-02-26 12:04:18 +01001280 /* this would be a firmware bug */
1281 if (WARN_ON_ONCE(truncated < 0))
1282 truncated = 0;
1283
1284 if (ieee80211_is_data(hdr->frame_control)) {
1285 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1286 int ivlen = 0, icvlen = 4; /* also FCS */
1287
Johannes Berg9b26b502013-01-22 13:02:09 +01001288 pkt = alloc_skb(pktsize, GFP_KERNEL);
1289 if (!pkt)
1290 goto report;
Johannes Bergf444eb12013-02-26 12:04:18 +01001291
1292 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1293 pktdata += hdrlen;
1294 pktsize -= hdrlen;
1295
1296 if (ieee80211_has_protected(hdr->frame_control)) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001297 /*
1298 * This is unlocked and using gtk_i(c)vlen,
1299 * but since everything is under RTNL still
1300 * that's not really a problem - changing
1301 * it would be difficult.
1302 */
Johannes Bergf444eb12013-02-26 12:04:18 +01001303 if (is_multicast_ether_addr(hdr->addr1)) {
1304 ivlen = mvm->gtk_ivlen;
1305 icvlen += mvm->gtk_icvlen;
1306 } else {
1307 ivlen = mvm->ptk_ivlen;
1308 icvlen += mvm->ptk_icvlen;
1309 }
1310 }
1311
1312 /* if truncated, FCS/ICV is (partially) gone */
1313 if (truncated >= icvlen) {
1314 icvlen = 0;
1315 truncated -= icvlen;
1316 } else {
1317 icvlen -= truncated;
1318 truncated = 0;
1319 }
1320
1321 pktsize -= ivlen + icvlen;
1322 pktdata += ivlen;
1323
1324 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1325
Johannes Berg9b26b502013-01-22 13:02:09 +01001326 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1327 goto report;
1328 wakeup.packet = pkt->data;
1329 wakeup.packet_present_len = pkt->len;
Johannes Bergf444eb12013-02-26 12:04:18 +01001330 wakeup.packet_len = pkt->len - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001331 wakeup.packet_80211 = false;
1332 } else {
Johannes Bergf444eb12013-02-26 12:04:18 +01001333 int fcslen = 4;
1334
1335 if (truncated >= 4) {
1336 truncated -= 4;
1337 fcslen = 0;
1338 } else {
1339 fcslen -= truncated;
1340 truncated = 0;
1341 }
1342 pktsize -= fcslen;
Johannes Berg9b26b502013-01-22 13:02:09 +01001343 wakeup.packet = status->wake_packet;
1344 wakeup.packet_present_len = pktsize;
Johannes Bergf444eb12013-02-26 12:04:18 +01001345 wakeup.packet_len = pktlen - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001346 wakeup.packet_80211 = true;
1347 }
1348 }
1349
1350 report:
1351 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1352 kfree_skb(pkt);
Johannes Berg7abc4632013-08-06 18:28:26 +02001353}
Johannes Berg9b26b502013-01-22 13:02:09 +01001354
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001355static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1356 struct ieee80211_key_seq *seq)
1357{
1358 u64 pn;
1359
1360 pn = le64_to_cpu(sc->pn);
1361 seq->ccmp.pn[0] = pn >> 40;
1362 seq->ccmp.pn[1] = pn >> 32;
1363 seq->ccmp.pn[2] = pn >> 24;
1364 seq->ccmp.pn[3] = pn >> 16;
1365 seq->ccmp.pn[4] = pn >> 8;
1366 seq->ccmp.pn[5] = pn;
1367}
1368
1369static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1370 struct ieee80211_key_seq *seq)
1371{
1372 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1373 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1374}
1375
1376static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1377 struct ieee80211_key_conf *key)
1378{
1379 int tid;
1380
1381 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1382
1383 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1384 struct ieee80211_key_seq seq = {};
1385
1386 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1387 ieee80211_set_key_rx_seq(key, tid, &seq);
1388 }
1389}
1390
1391static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1392 struct ieee80211_key_conf *key)
1393{
1394 int tid;
1395
1396 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1397
1398 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1399 struct ieee80211_key_seq seq = {};
1400
1401 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1402 ieee80211_set_key_rx_seq(key, tid, &seq);
1403 }
1404}
1405
1406static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001407 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001408{
1409 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1410
1411 switch (key->cipher) {
1412 case WLAN_CIPHER_SUITE_CCMP:
1413 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1414 break;
1415 case WLAN_CIPHER_SUITE_TKIP:
1416 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1417 break;
1418 default:
1419 WARN_ON(1);
1420 }
1421}
1422
1423struct iwl_mvm_d3_gtk_iter_data {
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001424 struct iwl_wowlan_status *status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001425 void *last_gtk;
1426 u32 cipher;
1427 bool find_phase, unhandled_cipher;
1428 int num_keys;
1429};
1430
Emmanuel Grumbach59fd4bf2015-11-01 13:06:37 +02001431static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001432 struct ieee80211_vif *vif,
1433 struct ieee80211_sta *sta,
1434 struct ieee80211_key_conf *key,
1435 void *_data)
1436{
1437 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1438
1439 if (data->unhandled_cipher)
1440 return;
1441
1442 switch (key->cipher) {
1443 case WLAN_CIPHER_SUITE_WEP40:
1444 case WLAN_CIPHER_SUITE_WEP104:
1445 /* ignore WEP completely, nothing to do */
1446 return;
1447 case WLAN_CIPHER_SUITE_CCMP:
1448 case WLAN_CIPHER_SUITE_TKIP:
1449 /* we support these */
1450 break;
1451 default:
1452 /* everything else (even CMAC for MFP) - disconnect from AP */
1453 data->unhandled_cipher = true;
1454 return;
1455 }
1456
1457 data->num_keys++;
1458
1459 /*
1460 * pairwise key - update sequence counters only;
1461 * note that this assumes no TDLS sessions are active
1462 */
1463 if (sta) {
1464 struct ieee80211_key_seq seq = {};
1465 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1466
1467 if (data->find_phase)
1468 return;
1469
1470 switch (key->cipher) {
1471 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001472 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
Johannes Berg6645d5e2015-09-15 09:58:53 +02001473 atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001474 break;
1475 case WLAN_CIPHER_SUITE_TKIP:
1476 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1477 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
Johannes Berg6645d5e2015-09-15 09:58:53 +02001478 ieee80211_set_key_tx_seq(key, &seq);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001479 break;
1480 }
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001481
1482 /* that's it for this key */
1483 return;
1484 }
1485
1486 if (data->find_phase) {
1487 data->last_gtk = key;
1488 data->cipher = key->cipher;
1489 return;
1490 }
1491
1492 if (data->status->num_of_gtk_rekeys)
1493 ieee80211_remove_key(key);
1494 else if (data->last_gtk == key)
1495 iwl_mvm_set_key_rx_seq(key, data->status);
1496}
1497
1498static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1499 struct ieee80211_vif *vif,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001500 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001501{
1502 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1503 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1504 .status = status,
1505 };
Eliad Peller431591c2013-11-07 14:12:09 +02001506 u32 disconnection_reasons =
1507 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1508 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001509
1510 if (!status || !vif->bss_conf.bssid)
1511 return false;
1512
Eliad Peller431591c2013-11-07 14:12:09 +02001513 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1514 return false;
1515
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001516 /* find last GTK that we used initially, if any */
1517 gtkdata.find_phase = true;
1518 ieee80211_iter_keys(mvm->hw, vif,
Emmanuel Grumbach59fd4bf2015-11-01 13:06:37 +02001519 iwl_mvm_d3_update_keys, &gtkdata);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001520 /* not trying to keep connections with MFP/unhandled ciphers */
1521 if (gtkdata.unhandled_cipher)
1522 return false;
1523 if (!gtkdata.num_keys)
Luciano Coelhocfa88892013-11-19 15:38:55 +02001524 goto out;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001525 if (!gtkdata.last_gtk)
1526 return false;
1527
1528 /*
1529 * invalidate all other GTKs that might still exist and update
1530 * the one that we used
1531 */
1532 gtkdata.find_phase = false;
1533 ieee80211_iter_keys(mvm->hw, vif,
Emmanuel Grumbach59fd4bf2015-11-01 13:06:37 +02001534 iwl_mvm_d3_update_keys, &gtkdata);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001535
1536 if (status->num_of_gtk_rekeys) {
1537 struct ieee80211_key_conf *key;
1538 struct {
1539 struct ieee80211_key_conf conf;
1540 u8 key[32];
1541 } conf = {
1542 .conf.cipher = gtkdata.cipher,
1543 .conf.keyidx = status->gtk.key_index,
1544 };
1545
1546 switch (gtkdata.cipher) {
1547 case WLAN_CIPHER_SUITE_CCMP:
1548 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1549 memcpy(conf.conf.key, status->gtk.decrypt_key,
1550 WLAN_KEY_LEN_CCMP);
1551 break;
1552 case WLAN_CIPHER_SUITE_TKIP:
1553 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1554 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1555 /* leave TX MIC key zeroed, we don't use it anyway */
1556 memcpy(conf.conf.key +
1557 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1558 status->gtk.tkip_mic_key, 8);
1559 break;
1560 }
1561
1562 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1563 if (IS_ERR(key))
1564 return false;
1565 iwl_mvm_set_key_rx_seq(key, status);
1566 }
1567
1568 if (status->num_of_gtk_rekeys) {
1569 __be64 replay_ctr =
1570 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1571 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1572 (void *)&replay_ctr, GFP_KERNEL);
1573 }
1574
Luciano Coelhocfa88892013-11-19 15:38:55 +02001575out:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001576 mvmvif->seqno_valid = true;
1577 /* +0x10 because the set API expects next-to-use, not last-used */
1578 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1579
1580 return true;
1581}
1582
Luciano Coelhoec12f452014-11-20 10:14:44 +02001583static struct iwl_wowlan_status *
1584iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg7abc4632013-08-06 18:28:26 +02001585{
1586 u32 base = mvm->error_event_table;
1587 struct error_table_start {
1588 /* cf. struct iwl_error_event_table */
1589 u32 valid;
1590 u32 error_id;
1591 } err_info;
1592 struct iwl_host_cmd cmd = {
1593 .id = WOWLAN_GET_STATUSES,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001594 .flags = CMD_WANT_SKB,
Johannes Berg7abc4632013-08-06 18:28:26 +02001595 };
Luciano Coelhoec12f452014-11-20 10:14:44 +02001596 struct iwl_wowlan_status *status, *fw_status;
1597 int ret, len, status_size;
Johannes Berg7abc4632013-08-06 18:28:26 +02001598
1599 iwl_trans_read_mem_bytes(mvm->trans, base,
1600 &err_info, sizeof(err_info));
1601
1602 if (err_info.valid) {
Luciano Coelhoec12f452014-11-20 10:14:44 +02001603 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1604 err_info.valid, err_info.error_id);
Johannes Berg7abc4632013-08-06 18:28:26 +02001605 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1606 struct cfg80211_wowlan_wakeup wakeup = {
1607 .rfkill_release = true,
1608 };
1609 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1610 GFP_KERNEL);
1611 }
Luciano Coelhoec12f452014-11-20 10:14:44 +02001612 return ERR_PTR(-EIO);
Johannes Berg7abc4632013-08-06 18:28:26 +02001613 }
1614
1615 /* only for tracing for now */
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001616 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
Johannes Berg7abc4632013-08-06 18:28:26 +02001617 if (ret)
1618 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1619
1620 ret = iwl_mvm_send_cmd(mvm, &cmd);
1621 if (ret) {
1622 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
Luciano Coelhoec12f452014-11-20 10:14:44 +02001623 return ERR_PTR(ret);
Johannes Berg7abc4632013-08-06 18:28:26 +02001624 }
1625
1626 /* RF-kill already asserted again... */
Luciano Coelhoec12f452014-11-20 10:14:44 +02001627 if (!cmd.resp_pkt) {
Johannes Berge111e962015-03-03 20:23:47 +01001628 fw_status = ERR_PTR(-ERFKILL);
Luciano Coelhoec12f452014-11-20 10:14:44 +02001629 goto out_free_resp;
1630 }
Johannes Berg7abc4632013-08-06 18:28:26 +02001631
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001632 status_size = sizeof(*fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001633
Johannes Berg65b30342014-01-08 13:16:33 +01001634 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1635 if (len < status_size) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001636 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
Johannes Berge111e962015-03-03 20:23:47 +01001637 fw_status = ERR_PTR(-EIO);
Johannes Berg7abc4632013-08-06 18:28:26 +02001638 goto out_free_resp;
1639 }
1640
Luciano Coelhoec12f452014-11-20 10:14:44 +02001641 status = (void *)cmd.resp_pkt->data;
1642 if (len != (status_size +
1643 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1644 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
Johannes Berge111e962015-03-03 20:23:47 +01001645 fw_status = ERR_PTR(-EIO);
Luciano Coelhoec12f452014-11-20 10:14:44 +02001646 goto out_free_resp;
1647 }
1648
1649 fw_status = kmemdup(status, len, GFP_KERNEL);
1650
1651out_free_resp:
1652 iwl_free_resp(&cmd);
Johannes Berge111e962015-03-03 20:23:47 +01001653 return fw_status;
Luciano Coelhoec12f452014-11-20 10:14:44 +02001654}
1655
1656/* releases the MVM mutex */
1657static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1658 struct ieee80211_vif *vif)
1659{
1660 struct iwl_wowlan_status_data status;
1661 struct iwl_wowlan_status *fw_status;
1662 int i;
1663 bool keep;
1664 struct ieee80211_sta *ap_sta;
1665 struct iwl_mvm_sta *mvm_ap_sta;
1666
1667 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1668 if (IS_ERR_OR_NULL(fw_status))
1669 goto out_unlock;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001670
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001671 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1672 for (i = 0; i < 8; i++)
1673 status.qos_seq_ctr[i] =
1674 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1675 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1676 status.wake_packet_length =
1677 le32_to_cpu(fw_status->wake_packet_length);
1678 status.wake_packet_bufsize =
1679 le32_to_cpu(fw_status->wake_packet_bufsize);
1680 status.wake_packet = fw_status->wake_packet;
Johannes Berg7abc4632013-08-06 18:28:26 +02001681
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001682 /* still at hard-coded place 0 for D3 image */
1683 ap_sta = rcu_dereference_protected(
1684 mvm->fw_id_to_mac_id[0],
1685 lockdep_is_held(&mvm->mutex));
1686 if (IS_ERR_OR_NULL(ap_sta))
Luciano Coelhoec12f452014-11-20 10:14:44 +02001687 goto out_free;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001688
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001689 mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001690 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1691 u16 seq = status.qos_seq_ctr[i];
1692 /* firmware stores last-used value, we store next value */
1693 seq += 0x10;
1694 mvm_ap_sta->tid_data[i].seq_number = seq;
1695 }
1696
Johannes Berg7abc4632013-08-06 18:28:26 +02001697 /* now we have all the data we need, unlock to avoid mac80211 issues */
1698 mutex_unlock(&mvm->mutex);
1699
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001700 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1701
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001702 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001703
Luciano Coelhoec12f452014-11-20 10:14:44 +02001704 kfree(fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001705 return keep;
Johannes Berg7abc4632013-08-06 18:28:26 +02001706
Luciano Coelhoec12f452014-11-20 10:14:44 +02001707out_free:
1708 kfree(fw_status);
1709out_unlock:
Johannes Berg7abc4632013-08-06 18:28:26 +02001710 mutex_unlock(&mvm->mutex);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001711 return false;
Johannes Berg9b26b502013-01-22 13:02:09 +01001712}
1713
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001714struct iwl_mvm_nd_query_results {
1715 u32 matched_profiles;
1716 struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1717};
1718
1719static int
1720iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1721 struct iwl_mvm_nd_query_results *results)
Luciano Coelhob04998f2014-11-20 15:58:34 +02001722{
1723 struct iwl_scan_offload_profiles_query *query;
1724 struct iwl_host_cmd cmd = {
1725 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1726 .flags = CMD_WANT_SKB,
1727 };
1728 int ret, len;
1729
1730 ret = iwl_mvm_send_cmd(mvm, &cmd);
1731 if (ret) {
1732 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001733 return ret;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001734 }
1735
1736 /* RF-kill already asserted again... */
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001737 if (!cmd.resp_pkt) {
1738 ret = -ERFKILL;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001739 goto out_free_resp;
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001740 }
Luciano Coelhob04998f2014-11-20 15:58:34 +02001741
1742 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1743 if (len < sizeof(*query)) {
1744 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001745 ret = -EIO;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001746 goto out_free_resp;
1747 }
1748
1749 query = (void *)cmd.resp_pkt->data;
1750
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001751 results->matched_profiles = le32_to_cpu(query->matched_profiles);
1752 memcpy(results->matches, query->matches, sizeof(results->matches));
Luciano Coelhob04998f2014-11-20 15:58:34 +02001753
Emmanuel Grumbach6bbd5522015-04-28 15:00:23 +03001754#ifdef CONFIG_IWLWIFI_DEBUGFS
Luciano Coelho484b3d12015-03-30 20:46:32 +03001755 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1756#endif
1757
Luciano Coelhob04998f2014-11-20 15:58:34 +02001758out_free_resp:
1759 iwl_free_resp(&cmd);
1760 return ret;
1761}
1762
Luciano Coelho2021a892014-11-20 08:59:51 +02001763static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1764 struct ieee80211_vif *vif)
1765{
Luciano Coelhod9718da2014-11-21 09:32:23 +02001766 struct cfg80211_wowlan_nd_info *net_detect = NULL;
Luciano Coelho2021a892014-11-20 08:59:51 +02001767 struct cfg80211_wowlan_wakeup wakeup = {
1768 .pattern_idx = -1,
1769 };
1770 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001771 struct iwl_mvm_nd_query_results query;
Luciano Coelho2021a892014-11-20 08:59:51 +02001772 struct iwl_wowlan_status *fw_status;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001773 unsigned long matched_profiles;
Luciano Coelho2021a892014-11-20 08:59:51 +02001774 u32 reasons = 0;
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001775 int i, j, n_matches, ret;
Luciano Coelho2021a892014-11-20 08:59:51 +02001776
1777 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
Haim Dreyfuss2fc863a2015-05-20 08:10:43 +03001778 if (!IS_ERR_OR_NULL(fw_status)) {
Luciano Coelho2021a892014-11-20 08:59:51 +02001779 reasons = le32_to_cpu(fw_status->wakeup_reasons);
Haim Dreyfuss2fc863a2015-05-20 08:10:43 +03001780 kfree(fw_status);
1781 }
Luciano Coelho2021a892014-11-20 08:59:51 +02001782
1783 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1784 wakeup.rfkill_release = true;
1785
Luciano Coelhob04998f2014-11-20 15:58:34 +02001786 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1787 goto out;
1788
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001789 ret = iwl_mvm_netdetect_query_results(mvm, &query);
1790 if (ret || !query.matched_profiles) {
Luciano Coelho2021a892014-11-20 08:59:51 +02001791 wakeup_report = NULL;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001792 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001793 }
1794
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001795 matched_profiles = query.matched_profiles;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001796 if (mvm->n_nd_match_sets) {
1797 n_matches = hweight_long(matched_profiles);
1798 } else {
1799 IWL_ERR(mvm, "no net detect match information available\n");
1800 n_matches = 0;
1801 }
1802
1803 net_detect = kzalloc(sizeof(*net_detect) +
1804 (n_matches * sizeof(net_detect->matches[0])),
1805 GFP_KERNEL);
1806 if (!net_detect || !n_matches)
1807 goto out_report_nd;
1808
1809 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001810 struct iwl_scan_offload_profile_match *fw_match;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001811 struct cfg80211_wowlan_nd_match *match;
Luciano Coelhoae90c2e52015-05-06 10:45:42 +03001812 int idx, n_channels = 0;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001813
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001814 fw_match = &query.matches[i];
1815
1816 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1817 n_channels += hweight8(fw_match->matching_channels[j]);
1818
1819 match = kzalloc(sizeof(*match) +
1820 (n_channels * sizeof(*match->channels)),
1821 GFP_KERNEL);
Luciano Coelhod9718da2014-11-21 09:32:23 +02001822 if (!match)
1823 goto out_report_nd;
1824
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001825 net_detect->matches[net_detect->n_matches++] = match;
1826
Luciano Coelhoae90c2e52015-05-06 10:45:42 +03001827 /* We inverted the order of the SSIDs in the scan
1828 * request, so invert the index here.
1829 */
1830 idx = mvm->n_nd_match_sets - i - 1;
1831 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
1832 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
Luciano Coelhod9718da2014-11-21 09:32:23 +02001833 match->ssid.ssid_len);
1834
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001835 if (mvm->n_nd_channels < n_channels)
1836 continue;
1837
1838 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
1839 if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
1840 match->channels[match->n_channels++] =
1841 mvm->nd_channels[j]->center_freq;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001842 }
1843
1844out_report_nd:
1845 wakeup.net_detect = net_detect;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001846out:
Luciano Coelhod9718da2014-11-21 09:32:23 +02001847 iwl_mvm_free_nd(mvm);
1848
Luciano Coelho2021a892014-11-20 08:59:51 +02001849 mutex_unlock(&mvm->mutex);
1850 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
Luciano Coelhod9718da2014-11-21 09:32:23 +02001851
1852 if (net_detect) {
1853 for (i = 0; i < net_detect->n_matches; i++)
1854 kfree(net_detect->matches[i]);
1855 kfree(net_detect);
1856 }
Luciano Coelho2021a892014-11-20 08:59:51 +02001857}
1858
Johannes Bergafc66bb2013-05-03 11:44:16 +02001859static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1860{
1861#ifdef CONFIG_IWLWIFI_DEBUGFS
1862 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1863 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1864 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1865
1866 if (!mvm->store_d3_resume_sram)
1867 return;
1868
1869 if (!mvm->d3_resume_sram) {
1870 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1871 if (!mvm->d3_resume_sram)
1872 return;
1873 }
1874
1875 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1876#endif
1877}
1878
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001879static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1880 struct ieee80211_vif *vif)
1881{
1882 /* skip the one we keep connection on */
1883 if (data == vif)
1884 return;
1885
1886 if (vif->type == NL80211_IFTYPE_STATION)
1887 ieee80211_resume_disconnect(vif);
1888}
1889
Johannes Bergdebff612013-05-14 13:53:45 +02001890static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001891{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001892 struct ieee80211_vif *vif = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001893 int ret;
1894 enum iwl_d3_status d3_status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001895 bool keep = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001896
1897 mutex_lock(&mvm->mutex);
1898
1899 /* get the BSS vif pointer again */
Luciano Coelho7f549e22014-10-02 15:38:04 +03001900 vif = iwl_mvm_get_bss_vif(mvm);
1901 if (IS_ERR_OR_NULL(vif))
Luciano Coelhoa500e462015-05-04 17:03:17 +03001902 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001903
Johannes Bergdebff612013-05-14 13:53:45 +02001904 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001905 if (ret)
Luciano Coelhoa500e462015-05-04 17:03:17 +03001906 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001907
1908 if (d3_status != IWL_D3_STATUS_ALIVE) {
1909 IWL_INFO(mvm, "Device was reset during suspend\n");
Luciano Coelhoa500e462015-05-04 17:03:17 +03001910 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001911 }
1912
Johannes Bergafc66bb2013-05-03 11:44:16 +02001913 /* query SRAM first in case we want event logging */
1914 iwl_mvm_read_d3_sram(mvm);
1915
Jonathan Doron47c8b152014-11-27 16:55:25 +02001916 /*
1917 * Query the current location and source from the D3 firmware so we
1918 * can play it back when we re-intiailize the D0 firmware
1919 */
1920 iwl_mvm_update_changed_regdom(mvm);
1921
Luciano Coelho2021a892014-11-20 08:59:51 +02001922 if (mvm->net_detect) {
1923 iwl_mvm_query_netdetect_reasons(mvm, vif);
Luciano Coelhoe0ede172015-02-13 21:37:09 +02001924 /* has unlocked the mutex, so skip that */
1925 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001926 } else {
1927 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
Eliad Peller78c9df62013-11-07 14:13:30 +02001928#ifdef CONFIG_IWLWIFI_DEBUGFS
Luciano Coelho2021a892014-11-20 08:59:51 +02001929 if (keep)
1930 mvm->keep_vif = vif;
Luciano Coelho0c2ae042015-03-12 09:25:15 +02001931#endif
Luciano Coelhoe0ede172015-02-13 21:37:09 +02001932 /* has unlocked the mutex, so skip that */
1933 goto out_iterate;
Luciano Coelho2021a892014-11-20 08:59:51 +02001934 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001935
Luciano Coelhoa500e462015-05-04 17:03:17 +03001936err:
1937 iwl_mvm_free_nd(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001938 mutex_unlock(&mvm->mutex);
1939
Luciano Coelhoe0ede172015-02-13 21:37:09 +02001940out_iterate:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001941 if (!test)
1942 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1943 IEEE80211_IFACE_ITER_NORMAL,
1944 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001945
Luciano Coelhoe0ede172015-02-13 21:37:09 +02001946out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001947 /* return 1 to reconfigure the device */
1948 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
Johannes Berg58629d92014-11-06 09:40:50 +01001949 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
Luciano Coelhodcfc7fb2015-04-28 08:41:55 +03001950
1951 /* We always return 1, which causes mac80211 to do a reconfig
1952 * with IEEE80211_RECONFIG_TYPE_RESTART. This type of
1953 * reconfig calls iwl_mvm_restart_complete(), where we unref
1954 * the IWL_MVM_REF_UCODE_DOWN, so we need to take the
1955 * reference here.
1956 */
1957 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001958 return 1;
1959}
1960
Eliad Peller2d428012015-06-17 15:32:37 +03001961static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
1962{
1963 iwl_trans_resume(mvm->trans);
1964
1965 return __iwl_mvm_resume(mvm, false);
1966}
1967
1968static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
1969{
1970 bool exit_now;
Eliad Peller6dfb36c2015-07-09 14:17:24 +03001971 enum iwl_d3_status d3_status;
1972
1973 iwl_trans_d3_resume(mvm->trans, &d3_status, false);
Eliad Peller2d428012015-06-17 15:32:37 +03001974
1975 /*
1976 * make sure to clear D0I3_DEFER_WAKEUP before
1977 * calling iwl_trans_resume(), which might wait
1978 * for d0i3 exit completion.
1979 */
1980 mutex_lock(&mvm->d0i3_suspend_mutex);
1981 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1982 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1983 &mvm->d0i3_suspend_flags);
1984 mutex_unlock(&mvm->d0i3_suspend_mutex);
1985 if (exit_now) {
1986 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
1987 _iwl_mvm_exit_d0i3(mvm);
1988 }
1989
1990 iwl_trans_resume(mvm->trans);
1991
1992 if (mvm->trans->d0i3_mode == IWL_D0I3_MODE_ON_SUSPEND) {
1993 int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
1994
1995 if (ret)
1996 return ret;
1997 /*
1998 * d0i3 exit will be deferred until reconfig_complete.
1999 * make sure there we are out of d0i3.
2000 */
2001 }
2002 return 0;
2003}
2004
Johannes Bergdebff612013-05-14 13:53:45 +02002005int iwl_mvm_resume(struct ieee80211_hw *hw)
2006{
2007 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2008
Eliad Peller2d428012015-06-17 15:32:37 +03002009 /* 'any' trigger means d0i3 was used */
2010 if (hw->wiphy->wowlan_config->any)
2011 return iwl_mvm_resume_d0i3(mvm);
2012 else
2013 return iwl_mvm_resume_d3(mvm);
Johannes Bergdebff612013-05-14 13:53:45 +02002014}
2015
Johannes Berg8ca151b2013-01-24 14:25:36 +01002016void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2017{
2018 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2019
2020 device_set_wakeup_enable(mvm->trans->dev, enabled);
2021}
Johannes Bergdebff612013-05-14 13:53:45 +02002022
2023#ifdef CONFIG_IWLWIFI_DEBUGFS
2024static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2025{
2026 struct iwl_mvm *mvm = inode->i_private;
2027 int err;
2028
2029 if (mvm->d3_test_active)
2030 return -EBUSY;
2031
2032 file->private_data = inode->i_private;
2033
2034 ieee80211_stop_queues(mvm->hw);
2035 synchronize_net();
2036
2037 /* start pseudo D3 */
2038 rtnl_lock();
2039 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2040 rtnl_unlock();
2041 if (err > 0)
2042 err = -EINVAL;
2043 if (err) {
2044 ieee80211_wake_queues(mvm->hw);
2045 return err;
2046 }
2047 mvm->d3_test_active = true;
Eliad Peller78c9df62013-11-07 14:13:30 +02002048 mvm->keep_vif = NULL;
Johannes Bergdebff612013-05-14 13:53:45 +02002049 return 0;
2050}
2051
2052static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2053 size_t count, loff_t *ppos)
2054{
2055 struct iwl_mvm *mvm = file->private_data;
2056 u32 pme_asserted;
2057
2058 while (true) {
Eliad Peller4ed80a82013-11-07 13:23:21 +02002059 /* read pme_ptr if available */
2060 if (mvm->d3_test_pme_ptr) {
2061 pme_asserted = iwl_trans_read_mem32(mvm->trans,
2062 mvm->d3_test_pme_ptr);
2063 if (pme_asserted)
2064 break;
2065 }
2066
Johannes Bergdebff612013-05-14 13:53:45 +02002067 if (msleep_interruptible(100))
2068 break;
2069 }
2070
2071 return 0;
2072}
2073
2074static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2075 struct ieee80211_vif *vif)
2076{
Eliad Peller78c9df62013-11-07 14:13:30 +02002077 /* skip the one we keep connection on */
2078 if (_data == vif)
2079 return;
2080
Johannes Bergdebff612013-05-14 13:53:45 +02002081 if (vif->type == NL80211_IFTYPE_STATION)
2082 ieee80211_connection_loss(vif);
2083}
2084
2085static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2086{
2087 struct iwl_mvm *mvm = inode->i_private;
2088 int remaining_time = 10;
2089
2090 mvm->d3_test_active = false;
Luciano Coelho31ca09d2014-09-22 13:22:51 +03002091 rtnl_lock();
Johannes Bergdebff612013-05-14 13:53:45 +02002092 __iwl_mvm_resume(mvm, true);
Luciano Coelho31ca09d2014-09-22 13:22:51 +03002093 rtnl_unlock();
Johannes Bergdebff612013-05-14 13:53:45 +02002094 iwl_abort_notification_waits(&mvm->notif_wait);
2095 ieee80211_restart_hw(mvm->hw);
2096
2097 /* wait for restart and disconnect all interfaces */
2098 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2099 remaining_time > 0) {
2100 remaining_time--;
2101 msleep(1000);
2102 }
2103
2104 if (remaining_time == 0)
2105 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
2106
2107 ieee80211_iterate_active_interfaces_atomic(
2108 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
Eliad Peller78c9df62013-11-07 14:13:30 +02002109 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
Johannes Bergdebff612013-05-14 13:53:45 +02002110
2111 ieee80211_wake_queues(mvm->hw);
2112
2113 return 0;
2114}
2115
2116const struct file_operations iwl_dbgfs_d3_test_ops = {
2117 .llseek = no_llseek,
2118 .open = iwl_mvm_d3_test_open,
2119 .read = iwl_mvm_d3_test_read,
2120 .release = iwl_mvm_d3_test_release,
2121};
2122#endif