blob: 93e49586121440a68ff2fa60691f4b5ce3a9eecc [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:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020029 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg8ca151b2013-01-24 14:25:36 +010030 * 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
Sara Sharonc97dab42015-11-19 11:53:49 +0200107 memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
108
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200109 read_lock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100110 list_for_each_entry(ifa, &idev->addr_list, if_list) {
111 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
Sara Sharonc97dab42015-11-19 11:53:49 +0200112 if (ifa->flags & IFA_F_TENTATIVE)
113 __set_bit(idx, mvmvif->tentative_addrs);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100114 idx++;
Johannes Berg5369d6c2013-07-08 11:17:06 +0200115 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100116 break;
117 }
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200118 read_unlock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100119
120 mvmvif->num_target_ipv6_addrs = idx;
121}
122#endif
123
124void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
125 struct ieee80211_vif *vif, int idx)
126{
127 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
128
129 mvmvif->tx_key_idx = idx;
130}
131
132static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
133{
134 int i;
135
136 for (i = 0; i < IWL_P1K_SIZE; i++)
137 out[i] = cpu_to_le16(p1k[i]);
138}
139
Johannes Bergf5e28ea2015-12-06 14:58:08 +0200140static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
141 struct iwl_mvm_key_pn *ptk_pn,
142 struct ieee80211_key_seq *seq,
143 int tid, int queues)
144{
145 const u8 *ret = seq->ccmp.pn;
146 int i;
147
148 /* get the PN from mac80211, used on the default queue */
149 ieee80211_get_key_rx_seq(key, tid, seq);
150
151 /* and use the internal data for the other queues */
152 for (i = 1; i < queues; i++) {
153 const u8 *tmp = ptk_pn->q[i].pn[tid];
154
155 if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
156 ret = tmp;
157 }
158
159 return ret;
160}
161
Johannes Berg8ca151b2013-01-24 14:25:36 +0100162struct wowlan_key_data {
163 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
164 struct iwl_wowlan_tkip_params_cmd *tkip;
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200165 bool error, use_rsc_tsc, use_tkip, configure_keys;
Johannes Berg5312e542013-07-22 18:26:56 +0200166 int wep_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100167};
168
169static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
170 struct ieee80211_vif *vif,
171 struct ieee80211_sta *sta,
172 struct ieee80211_key_conf *key,
173 void *_data)
174{
175 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
176 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
177 struct wowlan_key_data *data = _data;
178 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
179 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
180 struct iwl_p1k_cache *rx_p1ks;
181 u8 *rx_mic_key;
182 struct ieee80211_key_seq seq;
183 u32 cur_rx_iv32 = 0;
184 u16 p1k[IWL_P1K_SIZE];
185 int ret, i;
186
Johannes Berg8ca151b2013-01-24 14:25:36 +0100187 switch (key->cipher) {
188 case WLAN_CIPHER_SUITE_WEP40:
189 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
190 struct {
191 struct iwl_mvm_wep_key_cmd wep_key_cmd;
192 struct iwl_mvm_wep_key wep_key;
193 } __packed wkc = {
194 .wep_key_cmd.mac_id_n_color =
195 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
196 mvmvif->color)),
197 .wep_key_cmd.num_keys = 1,
198 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
199 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
200 .wep_key.key_index = key->keyidx,
201 .wep_key.key_size = key->keylen,
202 };
203
204 /*
205 * This will fail -- the key functions don't set support
206 * pairwise WEP keys. However, that's better than silently
207 * failing WoWLAN. Or maybe not?
208 */
209 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
210 break;
211
212 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
213 if (key->keyidx == mvmvif->tx_key_idx) {
214 /* TX key must be at offset 0 */
215 wkc.wep_key.key_offset = 0;
216 } else {
217 /* others start at 1 */
Johannes Berg5312e542013-07-22 18:26:56 +0200218 data->wep_key_idx++;
219 wkc.wep_key.key_offset = data->wep_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100220 }
221
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200222 if (data->configure_keys) {
223 mutex_lock(&mvm->mutex);
224 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0,
225 sizeof(wkc), &wkc);
226 data->error = ret != 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100227
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200228 mvm->ptk_ivlen = key->iv_len;
229 mvm->ptk_icvlen = key->icv_len;
230 mvm->gtk_ivlen = key->iv_len;
231 mvm->gtk_icvlen = key->icv_len;
232 mutex_unlock(&mvm->mutex);
233 }
Johannes Bergf444eb12013-02-26 12:04:18 +0100234
Johannes Berg8ca151b2013-01-24 14:25:36 +0100235 /* don't upload key again */
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200236 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100237 }
238 default:
239 data->error = true;
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200240 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100241 case WLAN_CIPHER_SUITE_AES_CMAC:
242 /*
243 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
244 * but we also shouldn't abort suspend due to that. It does have
245 * support for the IGTK key renewal, but doesn't really use the
246 * IGTK for anything. This means we could spuriously wake up or
247 * be deauthenticated, but that was considered acceptable.
248 */
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200249 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100250 case WLAN_CIPHER_SUITE_TKIP:
251 if (sta) {
Eliad Peller1ad4f632016-02-14 13:56:36 +0200252 u64 pn64;
253
Johannes Berg8ca151b2013-01-24 14:25:36 +0100254 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
255 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
256
257 rx_p1ks = data->tkip->rx_uni;
258
Eliad Peller1ad4f632016-02-14 13:56:36 +0200259 pn64 = atomic64_read(&key->tx_pn);
260 tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
261 tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100262
Eliad Peller1ad4f632016-02-14 13:56:36 +0200263 ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
264 p1k);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100265 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
266
267 memcpy(data->tkip->mic_keys.tx,
268 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
269 IWL_MIC_KEY_SIZE);
270
271 rx_mic_key = data->tkip->mic_keys.rx_unicast;
272 } else {
273 tkip_sc =
274 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
275 rx_p1ks = data->tkip->rx_multi;
276 rx_mic_key = data->tkip->mic_keys.rx_mcast;
277 }
278
279 /*
280 * For non-QoS this relies on the fact that both the uCode and
281 * mac80211 use TID 0 (as they need to to avoid replay attacks)
282 * for checking the IV in the frames.
283 */
284 for (i = 0; i < IWL_NUM_RSC; i++) {
285 ieee80211_get_key_rx_seq(key, i, &seq);
286 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
287 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
288 /* wrapping isn't allowed, AP must rekey */
289 if (seq.tkip.iv32 > cur_rx_iv32)
290 cur_rx_iv32 = seq.tkip.iv32;
291 }
292
293 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
294 cur_rx_iv32, p1k);
295 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
296 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
297 cur_rx_iv32 + 1, p1k);
298 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
299
300 memcpy(rx_mic_key,
301 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
302 IWL_MIC_KEY_SIZE);
303
304 data->use_tkip = true;
305 data->use_rsc_tsc = true;
306 break;
307 case WLAN_CIPHER_SUITE_CCMP:
308 if (sta) {
Johannes Berg6645d5e2015-09-15 09:58:53 +0200309 u64 pn64;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100310
311 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
312 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
313
Johannes Berg6645d5e2015-09-15 09:58:53 +0200314 pn64 = atomic64_read(&key->tx_pn);
315 aes_tx_sc->pn = cpu_to_le64(pn64);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100316 } else {
317 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
318 }
319
320 /*
321 * For non-QoS this relies on the fact that both the uCode and
Johannes Bergf5e28ea2015-12-06 14:58:08 +0200322 * mac80211/our RX code use TID 0 for checking the PN.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100323 */
Johannes Bergf5e28ea2015-12-06 14:58:08 +0200324 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
325 struct iwl_mvm_sta *mvmsta;
326 struct iwl_mvm_key_pn *ptk_pn;
327 const u8 *pn;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100328
Johannes Bergf5e28ea2015-12-06 14:58:08 +0200329 mvmsta = iwl_mvm_sta_from_mac80211(sta);
330 ptk_pn = rcu_dereference_protected(
331 mvmsta->ptk_pn[key->keyidx],
332 lockdep_is_held(&mvm->mutex));
333 if (WARN_ON(!ptk_pn))
334 break;
335
336 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
337 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
338 mvm->trans->num_rx_queues);
339 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
340 ((u64)pn[4] << 8) |
341 ((u64)pn[3] << 16) |
342 ((u64)pn[2] << 24) |
343 ((u64)pn[1] << 32) |
344 ((u64)pn[0] << 40));
345 }
346 } else {
347 for (i = 0; i < IWL_NUM_RSC; i++) {
348 u8 *pn = seq.ccmp.pn;
349
350 ieee80211_get_key_rx_seq(key, i, &seq);
351 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
352 ((u64)pn[4] << 8) |
353 ((u64)pn[3] << 16) |
354 ((u64)pn[2] << 24) |
355 ((u64)pn[1] << 32) |
356 ((u64)pn[0] << 40));
357 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100358 }
359 data->use_rsc_tsc = true;
360 break;
361 }
362
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200363 if (data->configure_keys) {
364 mutex_lock(&mvm->mutex);
Johannes Berg5312e542013-07-22 18:26:56 +0200365 /*
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200366 * The D3 firmware hardcodes the key offset 0 as the key it
367 * uses to transmit packets to the AP, i.e. the PTK.
Johannes Berg5312e542013-07-22 18:26:56 +0200368 */
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200369 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
370 mvm->ptk_ivlen = key->iv_len;
371 mvm->ptk_icvlen = key->icv_len;
372 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
373 } else {
374 /*
375 * firmware only supports TSC/RSC for a single key,
376 * so if there are multiple keep overwriting them
377 * with new ones -- this relies on mac80211 doing
378 * list_add_tail().
379 */
380 mvm->gtk_ivlen = key->iv_len;
381 mvm->gtk_icvlen = key->icv_len;
382 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
383 }
384 mutex_unlock(&mvm->mutex);
385 data->error = ret != 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100386 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100387}
388
389static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
390 struct cfg80211_wowlan *wowlan)
391{
392 struct iwl_wowlan_patterns_cmd *pattern_cmd;
393 struct iwl_host_cmd cmd = {
394 .id = WOWLAN_PATTERNS,
395 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100396 };
397 int i, err;
398
399 if (!wowlan->n_patterns)
400 return 0;
401
402 cmd.len[0] = sizeof(*pattern_cmd) +
403 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
404
405 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
406 if (!pattern_cmd)
407 return -ENOMEM;
408
409 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
410
411 for (i = 0; i < wowlan->n_patterns; i++) {
412 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
413
414 memcpy(&pattern_cmd->patterns[i].mask,
415 wowlan->patterns[i].mask, mask_len);
416 memcpy(&pattern_cmd->patterns[i].pattern,
417 wowlan->patterns[i].pattern,
418 wowlan->patterns[i].pattern_len);
419 pattern_cmd->patterns[i].mask_size = mask_len;
420 pattern_cmd->patterns[i].pattern_size =
421 wowlan->patterns[i].pattern_len;
422 }
423
424 cmd.data[0] = pattern_cmd;
425 err = iwl_mvm_send_cmd(mvm, &cmd);
426 kfree(pattern_cmd);
427 return err;
428}
429
Johannes Bergf0c26462013-01-22 20:41:58 +0100430enum iwl_mvm_tcp_packet_type {
431 MVM_TCP_TX_SYN,
432 MVM_TCP_RX_SYNACK,
433 MVM_TCP_TX_DATA,
434 MVM_TCP_RX_ACK,
435 MVM_TCP_RX_WAKE,
436 MVM_TCP_TX_FIN,
437};
438
439static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
440{
441 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
442 return cpu_to_le16(be16_to_cpu((__force __be16)check));
443}
444
Oren Givonb002c7e2013-06-12 20:37:03 +0300445static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
Johannes Bergf0c26462013-01-22 20:41:58 +0100446 struct cfg80211_wowlan_tcp *tcp,
447 void *_pkt, u8 *mask,
448 __le16 *pseudo_hdr_csum,
449 enum iwl_mvm_tcp_packet_type ptype)
450{
451 struct {
452 struct ethhdr eth;
453 struct iphdr ip;
454 struct tcphdr tcp;
455 u8 data[];
456 } __packed *pkt = _pkt;
457 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
458 int i;
459
460 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
461 pkt->ip.version = 4;
462 pkt->ip.ihl = 5;
463 pkt->ip.protocol = IPPROTO_TCP;
464
465 switch (ptype) {
466 case MVM_TCP_TX_SYN:
467 case MVM_TCP_TX_DATA:
468 case MVM_TCP_TX_FIN:
469 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
470 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
471 pkt->ip.ttl = 128;
472 pkt->ip.saddr = tcp->src;
473 pkt->ip.daddr = tcp->dst;
474 pkt->tcp.source = cpu_to_be16(tcp->src_port);
475 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
476 /* overwritten for TX SYN later */
477 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
478 pkt->tcp.window = cpu_to_be16(65000);
479 break;
480 case MVM_TCP_RX_SYNACK:
481 case MVM_TCP_RX_ACK:
482 case MVM_TCP_RX_WAKE:
483 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
484 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
485 pkt->ip.saddr = tcp->dst;
486 pkt->ip.daddr = tcp->src;
487 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
488 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
489 break;
490 default:
491 WARN_ON(1);
492 return;
493 }
494
495 switch (ptype) {
496 case MVM_TCP_TX_SYN:
497 /* firmware assumes 8 option bytes - 8 NOPs for now */
498 memset(pkt->data, 0x01, 8);
499 ip_tot_len += 8;
500 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
501 pkt->tcp.syn = 1;
502 break;
503 case MVM_TCP_TX_DATA:
504 ip_tot_len += tcp->payload_len;
505 memcpy(pkt->data, tcp->payload, tcp->payload_len);
506 pkt->tcp.psh = 1;
507 pkt->tcp.ack = 1;
508 break;
509 case MVM_TCP_TX_FIN:
510 pkt->tcp.fin = 1;
511 pkt->tcp.ack = 1;
512 break;
513 case MVM_TCP_RX_SYNACK:
514 pkt->tcp.syn = 1;
515 pkt->tcp.ack = 1;
516 break;
517 case MVM_TCP_RX_ACK:
518 pkt->tcp.ack = 1;
519 break;
520 case MVM_TCP_RX_WAKE:
521 ip_tot_len += tcp->wake_len;
522 pkt->tcp.psh = 1;
523 pkt->tcp.ack = 1;
524 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
525 break;
526 }
527
528 switch (ptype) {
529 case MVM_TCP_TX_SYN:
530 case MVM_TCP_TX_DATA:
531 case MVM_TCP_TX_FIN:
532 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
533 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
534 break;
535 case MVM_TCP_RX_WAKE:
536 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
537 u8 tmp = tcp->wake_mask[i];
538 mask[i + 6] |= tmp << 6;
539 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
540 mask[i + 7] = tmp >> 2;
541 }
542 /* fall through for ethernet/IP/TCP headers mask */
543 case MVM_TCP_RX_SYNACK:
544 case MVM_TCP_RX_ACK:
545 mask[0] = 0xff; /* match ethernet */
546 /*
547 * match ethernet, ip.version, ip.ihl
548 * the ip.ihl half byte is really masked out by firmware
549 */
550 mask[1] = 0x7f;
551 mask[2] = 0x80; /* match ip.protocol */
552 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
553 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
554 mask[5] = 0x80; /* match tcp flags */
555 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
556 break;
557 };
558
559 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
560 pkt->ip.saddr, pkt->ip.daddr);
561}
562
563static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
564 struct ieee80211_vif *vif,
565 struct cfg80211_wowlan_tcp *tcp)
566{
567 struct iwl_wowlan_remote_wake_config *cfg;
568 struct iwl_host_cmd cmd = {
569 .id = REMOTE_WAKE_CONFIG_CMD,
570 .len = { sizeof(*cfg), },
571 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
Johannes Bergf0c26462013-01-22 20:41:58 +0100572 };
573 int ret;
574
575 if (!tcp)
576 return 0;
577
578 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
579 if (!cfg)
580 return -ENOMEM;
581 cmd.data[0] = cfg;
582
583 cfg->max_syn_retries = 10;
584 cfg->max_data_retries = 10;
585 cfg->tcp_syn_ack_timeout = 1; /* seconds */
586 cfg->tcp_ack_timeout = 1; /* seconds */
587
588 /* SYN (TX) */
589 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300590 vif, tcp, cfg->syn_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100591 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
592 MVM_TCP_TX_SYN);
593 cfg->syn_tx.info.tcp_payload_length = 0;
594
595 /* SYN/ACK (RX) */
596 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300597 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
Johannes Bergf0c26462013-01-22 20:41:58 +0100598 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
599 MVM_TCP_RX_SYNACK);
600 cfg->synack_rx.info.tcp_payload_length = 0;
601
602 /* KEEPALIVE/ACK (TX) */
603 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300604 vif, tcp, cfg->keepalive_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100605 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
606 MVM_TCP_TX_DATA);
607 cfg->keepalive_tx.info.tcp_payload_length =
608 cpu_to_le16(tcp->payload_len);
609 cfg->sequence_number_offset = tcp->payload_seq.offset;
610 /* length must be 0..4, the field is little endian */
611 cfg->sequence_number_length = tcp->payload_seq.len;
612 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
613 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
614 if (tcp->payload_tok.len) {
615 cfg->token_offset = tcp->payload_tok.offset;
616 cfg->token_length = tcp->payload_tok.len;
617 cfg->num_tokens =
618 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
619 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
620 tcp->tokens_size);
621 } else {
622 /* set tokens to max value to almost never run out */
623 cfg->num_tokens = cpu_to_le16(65535);
624 }
625
626 /* ACK (RX) */
627 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300628 vif, tcp, cfg->keepalive_ack_rx.data,
Johannes Bergf0c26462013-01-22 20:41:58 +0100629 cfg->keepalive_ack_rx.rx_mask,
630 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
631 MVM_TCP_RX_ACK);
632 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
633
634 /* WAKEUP (RX) */
635 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300636 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
Johannes Bergf0c26462013-01-22 20:41:58 +0100637 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
638 MVM_TCP_RX_WAKE);
639 cfg->wake_rx.info.tcp_payload_length =
640 cpu_to_le16(tcp->wake_len);
641
642 /* FIN */
643 iwl_mvm_build_tcp_packet(
Oren Givonb002c7e2013-06-12 20:37:03 +0300644 vif, tcp, cfg->fin_tx.data, NULL,
Johannes Bergf0c26462013-01-22 20:41:58 +0100645 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
646 MVM_TCP_TX_FIN);
647 cfg->fin_tx.info.tcp_payload_length = 0;
648
649 ret = iwl_mvm_send_cmd(mvm, &cmd);
650 kfree(cfg);
651
652 return ret;
653}
654
Johannes Berg8ca151b2013-01-24 14:25:36 +0100655static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
656 struct ieee80211_sta *ap_sta)
657{
658 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
659 struct ieee80211_chanctx_conf *ctx;
660 u8 chains_static, chains_dynamic;
661 struct cfg80211_chan_def chandef;
662 int ret, i;
663 struct iwl_binding_cmd binding_cmd = {};
664 struct iwl_time_quota_cmd quota_cmd = {};
665 u32 status;
666
667 /* add back the PHY */
668 if (WARN_ON(!mvmvif->phy_ctxt))
669 return -EINVAL;
670
671 rcu_read_lock();
672 ctx = rcu_dereference(vif->chanctx_conf);
673 if (WARN_ON(!ctx)) {
674 rcu_read_unlock();
675 return -EINVAL;
676 }
677 chandef = ctx->def;
678 chains_static = ctx->rx_chains_static;
679 chains_dynamic = ctx->rx_chains_dynamic;
680 rcu_read_unlock();
681
682 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
683 chains_static, chains_dynamic);
684 if (ret)
685 return ret;
686
687 /* add back the MAC */
688 mvmvif->uploaded = false;
689
690 if (WARN_ON(!vif->bss_conf.assoc))
691 return -EINVAL;
Luciano Coelhobca49d92014-05-13 17:33:38 +0300692
Johannes Berg8ca151b2013-01-24 14:25:36 +0100693 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100694 if (ret)
695 return ret;
696
697 /* add back binding - XXX refactor? */
698 binding_cmd.id_and_color =
699 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
700 mvmvif->phy_ctxt->color));
701 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
702 binding_cmd.phy =
703 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
704 mvmvif->phy_ctxt->color));
705 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
706 mvmvif->color));
707 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
708 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
709
710 status = 0;
711 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
712 sizeof(binding_cmd), &binding_cmd,
713 &status);
714 if (ret) {
715 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
716 return ret;
717 }
718
719 if (status) {
720 IWL_ERR(mvm, "Binding command failed: %u\n", status);
721 return -EIO;
722 }
723
Johannes Berg7a453972013-02-12 13:10:44 +0100724 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100725 if (ret)
726 return ret;
727 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
728
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200729 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100730 if (ret)
731 return ret;
732
733 /* and some quota */
734 quota_cmd.quotas[0].id_and_color =
735 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
736 mvmvif->phy_ctxt->color));
Emmanuel Grumbach9a3daf82014-03-05 10:00:44 +0200737 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
738 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100739
740 for (i = 1; i < MAX_BINDINGS; i++)
741 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
742
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300743 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100744 sizeof(quota_cmd), &quota_cmd);
745 if (ret)
746 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
747
Jonathan Doron47c8b152014-11-27 16:55:25 +0200748 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
749 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
750
Johannes Berg8ca151b2013-01-24 14:25:36 +0100751 return 0;
752}
753
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200754static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
755 struct ieee80211_vif *vif)
756{
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200757 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
758 struct iwl_nonqos_seq_query_cmd query_cmd = {
759 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
760 .mac_id_n_color =
761 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
762 mvmvif->color)),
763 };
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200764 struct iwl_host_cmd cmd = {
765 .id = NON_QOS_TX_COUNTER_CMD,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300766 .flags = CMD_WANT_SKB,
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200767 };
768 int err;
769 u32 size;
770
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300771 cmd.data[0] = &query_cmd;
772 cmd.len[0] = sizeof(query_cmd);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200773
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200774 err = iwl_mvm_send_cmd(mvm, &cmd);
775 if (err)
776 return err;
777
Johannes Berg65b30342014-01-08 13:16:33 +0100778 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200779 if (size < sizeof(__le16)) {
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200780 err = -EINVAL;
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200781 } else {
782 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300783 /* firmware returns next, not last-used seqno */
784 err = (u16) (err - 0x10);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200785 }
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200786
787 iwl_free_resp(&cmd);
788 return err;
789}
790
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200791void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
792{
793 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
794 struct iwl_nonqos_seq_query_cmd query_cmd = {
795 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
796 .mac_id_n_color =
797 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
798 mvmvif->color)),
799 .value = cpu_to_le16(mvmvif->seqno),
800 };
801
802 /* return if called during restart, not resume from D3 */
803 if (!mvmvif->seqno_valid)
804 return;
805
806 mvmvif->seqno_valid = false;
807
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300808 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200809 sizeof(query_cmd), &query_cmd))
810 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
811}
812
Luciano Coelho671b5822014-09-22 10:59:53 +0300813static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200814{
Luciano Coelhoc7d42482015-05-07 16:00:26 +0300815 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100816
817 iwl_trans_stop_device(mvm->trans);
818
819 /*
820 * Set the HW restart bit -- this is mostly true as we're
821 * going to load new firmware and reprogram that, though
822 * the reprogramming is going to be manual to avoid adding
823 * all the MACs that aren't support.
824 * We don't have to clear up everything though because the
825 * reprogramming is manual. When we resume, we'll actually
826 * go through a proper restart sequence again to switch
827 * back to the runtime firmware image.
828 */
829 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
830
Luca Coelho9c3deeb2015-11-11 01:06:17 +0200831 /* the fw is reset, so all the keys are cleared */
832 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
833
Johannes Bergf444eb12013-02-26 12:04:18 +0100834 mvm->ptk_ivlen = 0;
835 mvm->ptk_icvlen = 0;
836 mvm->ptk_ivlen = 0;
837 mvm->ptk_icvlen = 0;
838
Luciano Coelho671b5822014-09-22 10:59:53 +0300839 return iwl_mvm_load_d3_fw(mvm);
840}
841
842static int
Luciano Coelho671b5822014-09-22 10:59:53 +0300843iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
844 struct cfg80211_wowlan *wowlan,
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200845 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
Luciano Coelho671b5822014-09-22 10:59:53 +0300846 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
847 struct ieee80211_sta *ap_sta)
848{
849 int ret;
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100850 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
Luciano Coelho671b5822014-09-22 10:59:53 +0300851
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200852 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
Luciano Coelho671b5822014-09-22 10:59:53 +0300853
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200854 wowlan_config_cmd->is_11n_connection =
Luciano Coelho671b5822014-09-22 10:59:53 +0300855 ap_sta->ht_cap.ht_supported;
Sara Sharon70b4c532015-11-19 13:12:15 +0200856 wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
857 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
Luciano Coelho671b5822014-09-22 10:59:53 +0300858
859 /* Query the last used seqno and set it */
860 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
861 if (ret < 0)
862 return ret;
863
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200864 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
Luciano Coelho671b5822014-09-22 10:59:53 +0300865
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200866 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
Luciano Coelho671b5822014-09-22 10:59:53 +0300867
868 if (wowlan->disconnect)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200869 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300870 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
871 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
872 if (wowlan->magic_pkt)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200873 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300874 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
875 if (wowlan->gtk_rekey_failure)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200876 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300877 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
878 if (wowlan->eap_identity_req)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200879 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300880 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
881 if (wowlan->four_way_handshake)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200882 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300883 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
884 if (wowlan->n_patterns)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200885 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300886 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
887
888 if (wowlan->rfkill_release)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200889 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300890 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
891
892 if (wowlan->tcp) {
893 /*
894 * Set the "link change" (really "link lost") flag as well
895 * since that implies losing the TCP connection.
896 */
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200897 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300898 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
899 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
900 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
901 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
902 }
903
904 return 0;
905}
906
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200907static void
908iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
909 struct ieee80211_vif *vif,
910 void (*iter)(struct ieee80211_hw *hw,
911 struct ieee80211_vif *vif,
912 struct ieee80211_sta *sta,
913 struct ieee80211_key_conf *key,
914 void *data),
915 void *data)
916{
917 struct ieee80211_sta *ap_sta;
918
919 rcu_read_lock();
920
921 ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
922 if (IS_ERR_OR_NULL(ap_sta))
923 goto out;
924
925 ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
926out:
927 rcu_read_unlock();
928}
929
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200930int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
931 struct ieee80211_vif *vif,
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200932 bool d0i3,
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200933 u32 cmd_flags)
934{
935 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
936 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
937 struct wowlan_key_data key_data = {
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200938 .configure_keys = !d0i3,
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200939 .use_rsc_tsc = false,
940 .tkip = &tkip_cmd,
941 .use_tkip = false,
942 };
943 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
944 int ret;
945
946 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
947 if (!key_data.rsc_tsc)
948 return -ENOMEM;
949
950 /*
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200951 * if we have to configure keys, call ieee80211_iter_keys(),
952 * as we need non-atomic context in order to take the
953 * required locks.
954 * for the d0i3 we can't use ieee80211_iter_keys(), as
955 * taking (almost) any mutex might result in deadlock.
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200956 */
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200957 if (!d0i3) {
958 /*
959 * Note that currently we don't propagate cmd_flags
960 * to the iterator. In case of key_data.configure_keys,
961 * all the configured commands are SYNC, and
962 * iwl_mvm_wowlan_program_keys() will take care of
963 * locking/unlocking mvm->mutex.
964 */
965 ieee80211_iter_keys(mvm->hw, vif,
966 iwl_mvm_wowlan_program_keys,
967 &key_data);
968 } else {
969 iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
970 iwl_mvm_wowlan_program_keys,
971 &key_data);
972 }
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200973
974 if (key_data.error) {
975 ret = -EIO;
976 goto out;
977 }
978
979 if (key_data.use_rsc_tsc) {
980 ret = iwl_mvm_send_cmd_pdu(mvm,
981 WOWLAN_TSC_RSC_PARAM, cmd_flags,
982 sizeof(*key_data.rsc_tsc),
983 key_data.rsc_tsc);
984 if (ret)
985 goto out;
986 }
987
988 if (key_data.use_tkip) {
989 ret = iwl_mvm_send_cmd_pdu(mvm,
990 WOWLAN_TKIP_PARAM,
991 cmd_flags, sizeof(tkip_cmd),
992 &tkip_cmd);
993 if (ret)
994 goto out;
995 }
996
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200997 /* configure rekey data only if offloaded rekey is supported (d3) */
998 if (mvmvif->rekey_data.valid && !d0i3) {
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200999 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
1000 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1001 NL80211_KCK_LEN);
1002 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
1003 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1004 NL80211_KEK_LEN);
1005 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1006 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1007
1008 ret = iwl_mvm_send_cmd_pdu(mvm,
1009 WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
1010 sizeof(kek_kck_cmd),
1011 &kek_kck_cmd);
1012 if (ret)
1013 goto out;
1014 }
Eliad Peller3f50a692015-11-15 15:44:17 +02001015 ret = 0;
Eliad Pellerac8ef0c2015-11-12 10:49:38 +02001016out:
1017 kfree(key_data.rsc_tsc);
1018 return ret;
1019}
1020
Luciano Coelho671b5822014-09-22 10:59:53 +03001021static int
1022iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1023 struct cfg80211_wowlan *wowlan,
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +02001024 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
Luciano Coelho671b5822014-09-22 10:59:53 +03001025 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1026 struct ieee80211_sta *ap_sta)
1027{
Luciano Coelho671b5822014-09-22 10:59:53 +03001028 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001029
Luciano Coelho3acc9522014-11-19 22:35:37 +02001030 ret = iwl_mvm_switch_to_d3(mvm);
1031 if (ret)
1032 return ret;
1033
Johannes Berg8ca151b2013-01-24 14:25:36 +01001034 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1035 if (ret)
Luciano Coelho671b5822014-09-22 10:59:53 +03001036 return ret;
1037
Johannes Berg8ca151b2013-01-24 14:25:36 +01001038 if (!iwlwifi_mod_params.sw_crypto) {
1039 /*
1040 * This needs to be unlocked due to lock ordering
1041 * constraints. Since we're in the suspend path
1042 * that isn't really a problem though.
1043 */
1044 mutex_unlock(&mvm->mutex);
Eliad Pellera3f7ba52015-11-11 17:23:59 +02001045 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
Eliad Peller3f50a692015-11-15 15:44:17 +02001046 CMD_ASYNC);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001047 mutex_lock(&mvm->mutex);
Eliad Peller3f50a692015-11-15 15:44:17 +02001048 if (ret)
1049 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001050 }
1051
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +02001052 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1053 sizeof(*wowlan_config_cmd),
1054 wowlan_config_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001055 if (ret)
Eliad Pellerac8ef0c2015-11-12 10:49:38 +02001056 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001057
1058 ret = iwl_mvm_send_patterns(mvm, wowlan);
1059 if (ret)
Eliad Pellerac8ef0c2015-11-12 10:49:38 +02001060 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001061
Sara Sharonc97dab42015-11-19 11:53:49 +02001062 ret = iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001063 if (ret)
Eliad Pellerac8ef0c2015-11-12 10:49:38 +02001064 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001065
Johannes Bergf0c26462013-01-22 20:41:58 +01001066 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
Luciano Coelho671b5822014-09-22 10:59:53 +03001067 return ret;
1068}
1069
Luciano Coelho3acc9522014-11-19 22:35:37 +02001070static int
1071iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1072 struct cfg80211_wowlan *wowlan,
1073 struct cfg80211_sched_scan_request *nd_config,
1074 struct ieee80211_vif *vif)
1075{
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +02001076 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
Luciano Coelho3acc9522014-11-19 22:35:37 +02001077 int ret;
1078
1079 ret = iwl_mvm_switch_to_d3(mvm);
1080 if (ret)
1081 return ret;
1082
1083 /* rfkill release can be either for wowlan or netdetect */
1084 if (wowlan->rfkill_release)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +02001085 wowlan_config_cmd.wakeup_filter |=
Luciano Coelho3acc9522014-11-19 22:35:37 +02001086 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
1087
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +02001088 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1089 sizeof(wowlan_config_cmd),
1090 &wowlan_config_cmd);
Luciano Coelho3acc9522014-11-19 22:35:37 +02001091 if (ret)
1092 return ret;
1093
Luciano Coelho19945df2015-03-20 16:11:28 +02001094 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1095 IWL_MVM_SCAN_NETDETECT);
Luciano Coelhod9718da2014-11-21 09:32:23 +02001096 if (ret)
1097 return ret;
Luciano Coelho3acc9522014-11-19 22:35:37 +02001098
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001099 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
Luciano Coelhod9718da2014-11-21 09:32:23 +02001100 return -EBUSY;
1101
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001102 /* save the sched scan matchsets... */
Luciano Coelhod9718da2014-11-21 09:32:23 +02001103 if (nd_config->n_match_sets) {
1104 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1105 sizeof(*nd_config->match_sets) *
1106 nd_config->n_match_sets,
1107 GFP_KERNEL);
1108 if (mvm->nd_match_sets)
1109 mvm->n_nd_match_sets = nd_config->n_match_sets;
1110 }
1111
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001112 /* ...and the sched scan channels for later reporting */
1113 mvm->nd_channels = kmemdup(nd_config->channels,
1114 sizeof(*nd_config->channels) *
1115 nd_config->n_channels,
1116 GFP_KERNEL);
1117 if (mvm->nd_channels)
1118 mvm->n_nd_channels = nd_config->n_channels;
1119
Luciano Coelhod9718da2014-11-21 09:32:23 +02001120 return 0;
1121}
1122
1123static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1124{
1125 kfree(mvm->nd_match_sets);
1126 mvm->nd_match_sets = NULL;
1127 mvm->n_nd_match_sets = 0;
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001128 kfree(mvm->nd_channels);
1129 mvm->nd_channels = NULL;
1130 mvm->n_nd_channels = 0;
Luciano Coelho3acc9522014-11-19 22:35:37 +02001131}
1132
Luciano Coelho671b5822014-09-22 10:59:53 +03001133static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1134 struct cfg80211_wowlan *wowlan,
1135 bool test)
1136{
1137 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Luciano Coelho671b5822014-09-22 10:59:53 +03001138 struct ieee80211_vif *vif = NULL;
1139 struct iwl_mvm_vif *mvmvif = NULL;
1140 struct ieee80211_sta *ap_sta = NULL;
Luciano Coelho671b5822014-09-22 10:59:53 +03001141 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1142 /*
1143 * Program the minimum sleep time to 10 seconds, as many
1144 * platforms have issues processing a wakeup signal while
1145 * still being in the process of suspending.
1146 */
1147 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1148 };
1149 struct iwl_host_cmd d3_cfg_cmd = {
1150 .id = D3_CONFIG_CMD,
1151 .flags = CMD_WANT_SKB,
1152 .data[0] = &d3_cfg_cmd_data,
1153 .len[0] = sizeof(d3_cfg_cmd_data),
1154 };
1155 int ret;
1156 int len __maybe_unused;
1157
1158 if (!wowlan) {
1159 /*
1160 * mac80211 shouldn't get here, but for D3 test
1161 * it doesn't warrant a warning
1162 */
1163 WARN_ON(!test);
1164 return -EINVAL;
1165 }
1166
1167 mutex_lock(&mvm->mutex);
1168
Luciano Coelho7f549e22014-10-02 15:38:04 +03001169 vif = iwl_mvm_get_bss_vif(mvm);
1170 if (IS_ERR_OR_NULL(vif)) {
Luciano Coelho671b5822014-09-22 10:59:53 +03001171 ret = 1;
1172 goto out_noreset;
1173 }
1174
Luciano Coelho671b5822014-09-22 10:59:53 +03001175 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1176
Luciano Coelho3acc9522014-11-19 22:35:37 +02001177 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
1178 /* if we're not associated, this must be netdetect */
Luca Coelho566f1652015-10-25 22:55:32 +02001179 if (!wowlan->nd_config) {
Luciano Coelho3acc9522014-11-19 22:35:37 +02001180 ret = 1;
1181 goto out_noreset;
1182 }
1183
1184 ret = iwl_mvm_netdetect_config(
Luca Coelho566f1652015-10-25 22:55:32 +02001185 mvm, wowlan, wowlan->nd_config, vif);
Luciano Coelho3acc9522014-11-19 22:35:37 +02001186 if (ret)
1187 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001188
1189 mvm->net_detect = true;
Luciano Coelho3acc9522014-11-19 22:35:37 +02001190 } else {
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +02001191 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
Luciano Coelho3acc9522014-11-19 22:35:37 +02001192
Luciano Coelho1e4d19c2014-09-22 13:01:59 +03001193 ap_sta = rcu_dereference_protected(
1194 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1195 lockdep_is_held(&mvm->mutex));
1196 if (IS_ERR_OR_NULL(ap_sta)) {
1197 ret = -EINVAL;
1198 goto out_noreset;
1199 }
1200
1201 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1202 vif, mvmvif, ap_sta);
1203 if (ret)
1204 goto out_noreset;
Luciano Coelho1e4d19c2014-09-22 13:01:59 +03001205 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1206 vif, mvmvif, ap_sta);
1207 if (ret)
1208 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001209
1210 mvm->net_detect = false;
Luciano Coelho671b5822014-09-22 10:59:53 +03001211 }
1212
Emmanuel Grumbachc1cb92f2014-01-28 10:17:18 +02001213 ret = iwl_mvm_power_update_device(mvm);
Alexander Bondar64b928c2013-09-03 14:18:03 +03001214 if (ret)
1215 goto out;
1216
Arik Nemtsov999609f2014-05-15 17:31:51 +03001217 ret = iwl_mvm_power_update_mac(mvm);
Alexander Bondaree1e8422013-04-23 13:52:10 +03001218 if (ret)
1219 goto out;
1220
Johannes Bergb0114712013-06-12 14:55:40 +02001221#ifdef CONFIG_IWLWIFI_DEBUGFS
1222 if (mvm->d3_wake_sysassert)
1223 d3_cfg_cmd_data.wakeup_flags |=
1224 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1225#endif
1226
Johannes Berg8ca151b2013-01-24 14:25:36 +01001227 /* must be last -- this switches firmware state */
Johannes Bergdebff612013-05-14 13:53:45 +02001228 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001229 if (ret)
1230 goto out;
Johannes Bergdebff612013-05-14 13:53:45 +02001231#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg65b30342014-01-08 13:16:33 +01001232 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1233 if (len >= sizeof(u32)) {
Johannes Bergdebff612013-05-14 13:53:45 +02001234 mvm->d3_test_pme_ptr =
1235 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
Johannes Bergdebff612013-05-14 13:53:45 +02001236 }
1237#endif
1238 iwl_free_resp(&d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001239
1240 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1241
Johannes Bergdebff612013-05-14 13:53:45 +02001242 iwl_trans_d3_suspend(mvm->trans, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001243 out:
Luciano Coelhod9718da2014-11-21 09:32:23 +02001244 if (ret < 0) {
Eliad Peller48e67752015-03-03 12:03:20 +02001245 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001246 ieee80211_restart_hw(mvm->hw);
Luciano Coelhod9718da2014-11-21 09:32:23 +02001247 iwl_mvm_free_nd(mvm);
1248 }
Luciano Coelhob70ab722013-11-19 22:53:41 +02001249 out_noreset:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001250 mutex_unlock(&mvm->mutex);
1251
1252 return ret;
1253}
1254
Eliad Peller67359432014-12-09 15:23:54 +02001255static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
1256{
1257 struct iwl_notification_wait wait_d3;
Sara Sharon6eb031d2015-07-13 14:50:47 +03001258 static const u16 d3_notif[] = { D3_CONFIG_CMD };
Eliad Peller67359432014-12-09 15:23:54 +02001259 int ret;
1260
1261 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
1262 d3_notif, ARRAY_SIZE(d3_notif),
1263 NULL, NULL);
1264
1265 ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
1266 if (ret)
1267 goto remove_notif;
1268
1269 ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
1270 WARN_ON_ONCE(ret);
1271 return ret;
1272
1273remove_notif:
1274 iwl_remove_notification(&mvm->notif_wait, &wait_d3);
1275 return ret;
1276}
1277
Johannes Bergdebff612013-05-14 13:53:45 +02001278int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1279{
Eliad Pellerd15a7472014-03-27 18:53:12 +02001280 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Luca Coelhob7282642015-09-17 23:44:14 +03001281 struct iwl_trans *trans = mvm->trans;
Eliad Pellerc43fe902015-06-04 21:59:32 +03001282 int ret;
Eliad Pellerd15a7472014-03-27 18:53:12 +02001283
Luciano Coelhocf8c3cf2015-08-24 14:53:25 +03001284 /* make sure the d0i3 exit work is not pending */
1285 flush_work(&mvm->d0i3_exit_work);
1286
Luca Coelhob7282642015-09-17 23:44:14 +03001287 ret = iwl_trans_suspend(trans);
Eliad Pellerc43fe902015-06-04 21:59:32 +03001288 if (ret)
1289 return ret;
1290
Luca Coelhob7282642015-09-17 23:44:14 +03001291 if (wowlan->any) {
1292 trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
1293
1294 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
Eliad Pellerc43fe902015-06-04 21:59:32 +03001295 ret = iwl_mvm_enter_d0i3_sync(mvm);
Eliad Peller67359432014-12-09 15:23:54 +02001296
1297 if (ret)
1298 return ret;
1299 }
1300
Eliad Pellerd15a7472014-03-27 18:53:12 +02001301 mutex_lock(&mvm->d0i3_suspend_mutex);
1302 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1303 mutex_unlock(&mvm->d0i3_suspend_mutex);
Eliad Peller6dfb36c2015-07-09 14:17:24 +03001304
Luca Coelhob7282642015-09-17 23:44:14 +03001305 iwl_trans_d3_suspend(trans, false);
Eliad Peller6dfb36c2015-07-09 14:17:24 +03001306
Eliad Pellerd15a7472014-03-27 18:53:12 +02001307 return 0;
1308 }
1309
Luca Coelhob7282642015-09-17 23:44:14 +03001310 trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1311
Johannes Bergdebff612013-05-14 13:53:45 +02001312 return __iwl_mvm_suspend(hw, wowlan, false);
1313}
1314
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001315/* converted data from the different status responses */
1316struct iwl_wowlan_status_data {
1317 u16 pattern_number;
1318 u16 qos_seq_ctr[8];
1319 u32 wakeup_reasons;
1320 u32 wake_packet_length;
1321 u32 wake_packet_bufsize;
1322 const u8 *wake_packet;
1323};
1324
Johannes Berg7abc4632013-08-06 18:28:26 +02001325static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1326 struct ieee80211_vif *vif,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001327 struct iwl_wowlan_status_data *status)
Johannes Berg9b26b502013-01-22 13:02:09 +01001328{
Johannes Berg7abc4632013-08-06 18:28:26 +02001329 struct sk_buff *pkt = NULL;
Johannes Berg9b26b502013-01-22 13:02:09 +01001330 struct cfg80211_wowlan_wakeup wakeup = {
1331 .pattern_idx = -1,
1332 };
1333 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001334 u32 reasons = status->wakeup_reasons;
Johannes Berg9b26b502013-01-22 13:02:09 +01001335
1336 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1337 wakeup_report = NULL;
1338 goto report;
1339 }
1340
Derek Basehore06ae2ad2015-11-04 17:37:52 -08001341 pm_wakeup_event(mvm->dev, 0);
1342
Johannes Bergf444eb12013-02-26 12:04:18 +01001343 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
Johannes Berg9b26b502013-01-22 13:02:09 +01001344 wakeup.magic_pkt = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001345
Johannes Bergf444eb12013-02-26 12:04:18 +01001346 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
Johannes Berg9b26b502013-01-22 13:02:09 +01001347 wakeup.pattern_idx =
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001348 status->pattern_number;
Johannes Berg9b26b502013-01-22 13:02:09 +01001349
1350 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1351 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1352 wakeup.disconnect = true;
1353
Johannes Bergf444eb12013-02-26 12:04:18 +01001354 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001355 wakeup.gtk_rekey_failure = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001356
Johannes Bergf444eb12013-02-26 12:04:18 +01001357 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
Johannes Berg9b26b502013-01-22 13:02:09 +01001358 wakeup.rfkill_release = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001359
Johannes Bergf444eb12013-02-26 12:04:18 +01001360 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
Johannes Berg9b26b502013-01-22 13:02:09 +01001361 wakeup.eap_identity_req = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001362
Johannes Bergf444eb12013-02-26 12:04:18 +01001363 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001364 wakeup.four_way_handshake = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001365
Johannes Bergf0c26462013-01-22 20:41:58 +01001366 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1367 wakeup.tcp_connlost = true;
1368
1369 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1370 wakeup.tcp_nomoretokens = true;
1371
1372 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1373 wakeup.tcp_match = true;
1374
Johannes Berg9b26b502013-01-22 13:02:09 +01001375 if (status->wake_packet_bufsize) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001376 int pktsize = status->wake_packet_bufsize;
1377 int pktlen = status->wake_packet_length;
Johannes Bergf444eb12013-02-26 12:04:18 +01001378 const u8 *pktdata = status->wake_packet;
1379 struct ieee80211_hdr *hdr = (void *)pktdata;
1380 int truncated = pktlen - pktsize;
Johannes Berg9b26b502013-01-22 13:02:09 +01001381
Johannes Bergf444eb12013-02-26 12:04:18 +01001382 /* this would be a firmware bug */
1383 if (WARN_ON_ONCE(truncated < 0))
1384 truncated = 0;
1385
1386 if (ieee80211_is_data(hdr->frame_control)) {
1387 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1388 int ivlen = 0, icvlen = 4; /* also FCS */
1389
Johannes Berg9b26b502013-01-22 13:02:09 +01001390 pkt = alloc_skb(pktsize, GFP_KERNEL);
1391 if (!pkt)
1392 goto report;
Johannes Bergf444eb12013-02-26 12:04:18 +01001393
1394 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1395 pktdata += hdrlen;
1396 pktsize -= hdrlen;
1397
1398 if (ieee80211_has_protected(hdr->frame_control)) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001399 /*
1400 * This is unlocked and using gtk_i(c)vlen,
1401 * but since everything is under RTNL still
1402 * that's not really a problem - changing
1403 * it would be difficult.
1404 */
Johannes Bergf444eb12013-02-26 12:04:18 +01001405 if (is_multicast_ether_addr(hdr->addr1)) {
1406 ivlen = mvm->gtk_ivlen;
1407 icvlen += mvm->gtk_icvlen;
1408 } else {
1409 ivlen = mvm->ptk_ivlen;
1410 icvlen += mvm->ptk_icvlen;
1411 }
1412 }
1413
1414 /* if truncated, FCS/ICV is (partially) gone */
1415 if (truncated >= icvlen) {
1416 icvlen = 0;
1417 truncated -= icvlen;
1418 } else {
1419 icvlen -= truncated;
1420 truncated = 0;
1421 }
1422
1423 pktsize -= ivlen + icvlen;
1424 pktdata += ivlen;
1425
1426 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1427
Johannes Berg9b26b502013-01-22 13:02:09 +01001428 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1429 goto report;
1430 wakeup.packet = pkt->data;
1431 wakeup.packet_present_len = pkt->len;
Johannes Bergf444eb12013-02-26 12:04:18 +01001432 wakeup.packet_len = pkt->len - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001433 wakeup.packet_80211 = false;
1434 } else {
Johannes Bergf444eb12013-02-26 12:04:18 +01001435 int fcslen = 4;
1436
1437 if (truncated >= 4) {
1438 truncated -= 4;
1439 fcslen = 0;
1440 } else {
1441 fcslen -= truncated;
1442 truncated = 0;
1443 }
1444 pktsize -= fcslen;
Johannes Berg9b26b502013-01-22 13:02:09 +01001445 wakeup.packet = status->wake_packet;
1446 wakeup.packet_present_len = pktsize;
Johannes Bergf444eb12013-02-26 12:04:18 +01001447 wakeup.packet_len = pktlen - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001448 wakeup.packet_80211 = true;
1449 }
1450 }
1451
1452 report:
1453 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1454 kfree_skb(pkt);
Johannes Berg7abc4632013-08-06 18:28:26 +02001455}
Johannes Berg9b26b502013-01-22 13:02:09 +01001456
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001457static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1458 struct ieee80211_key_seq *seq)
1459{
1460 u64 pn;
1461
1462 pn = le64_to_cpu(sc->pn);
1463 seq->ccmp.pn[0] = pn >> 40;
1464 seq->ccmp.pn[1] = pn >> 32;
1465 seq->ccmp.pn[2] = pn >> 24;
1466 seq->ccmp.pn[3] = pn >> 16;
1467 seq->ccmp.pn[4] = pn >> 8;
1468 seq->ccmp.pn[5] = pn;
1469}
1470
1471static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1472 struct ieee80211_key_seq *seq)
1473{
1474 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1475 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1476}
1477
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001478static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
1479 struct ieee80211_sta *sta,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001480 struct ieee80211_key_conf *key)
1481{
1482 int tid;
1483
1484 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1485
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001486 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
1487 struct iwl_mvm_sta *mvmsta;
1488 struct iwl_mvm_key_pn *ptk_pn;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001489
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001490 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1491
1492 ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
1493 lockdep_is_held(&mvm->mutex));
1494 if (WARN_ON(!ptk_pn))
1495 return;
1496
1497 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1498 struct ieee80211_key_seq seq = {};
1499 int i;
1500
1501 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1502 ieee80211_set_key_rx_seq(key, tid, &seq);
1503 for (i = 1; i < mvm->trans->num_rx_queues; i++)
1504 memcpy(ptk_pn->q[i].pn[tid],
1505 seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
1506 }
1507 } else {
1508 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1509 struct ieee80211_key_seq seq = {};
1510
1511 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1512 ieee80211_set_key_rx_seq(key, tid, &seq);
1513 }
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001514 }
1515}
1516
1517static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1518 struct ieee80211_key_conf *key)
1519{
1520 int tid;
1521
1522 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1523
1524 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1525 struct ieee80211_key_seq seq = {};
1526
1527 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1528 ieee80211_set_key_rx_seq(key, tid, &seq);
1529 }
1530}
1531
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001532static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
1533 struct ieee80211_key_conf *key,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001534 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001535{
1536 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1537
1538 switch (key->cipher) {
1539 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001540 iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001541 break;
1542 case WLAN_CIPHER_SUITE_TKIP:
1543 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1544 break;
1545 default:
1546 WARN_ON(1);
1547 }
1548}
1549
1550struct iwl_mvm_d3_gtk_iter_data {
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001551 struct iwl_mvm *mvm;
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001552 struct iwl_wowlan_status *status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001553 void *last_gtk;
1554 u32 cipher;
1555 bool find_phase, unhandled_cipher;
1556 int num_keys;
1557};
1558
Emmanuel Grumbach59fd4bf2015-11-01 13:06:37 +02001559static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001560 struct ieee80211_vif *vif,
1561 struct ieee80211_sta *sta,
1562 struct ieee80211_key_conf *key,
1563 void *_data)
1564{
1565 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1566
1567 if (data->unhandled_cipher)
1568 return;
1569
1570 switch (key->cipher) {
1571 case WLAN_CIPHER_SUITE_WEP40:
1572 case WLAN_CIPHER_SUITE_WEP104:
1573 /* ignore WEP completely, nothing to do */
1574 return;
1575 case WLAN_CIPHER_SUITE_CCMP:
1576 case WLAN_CIPHER_SUITE_TKIP:
1577 /* we support these */
1578 break;
1579 default:
1580 /* everything else (even CMAC for MFP) - disconnect from AP */
1581 data->unhandled_cipher = true;
1582 return;
1583 }
1584
1585 data->num_keys++;
1586
1587 /*
1588 * pairwise key - update sequence counters only;
1589 * note that this assumes no TDLS sessions are active
1590 */
1591 if (sta) {
1592 struct ieee80211_key_seq seq = {};
1593 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1594
1595 if (data->find_phase)
1596 return;
1597
1598 switch (key->cipher) {
1599 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001600 iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
1601 sta, key);
Johannes Berg6645d5e2015-09-15 09:58:53 +02001602 atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001603 break;
1604 case WLAN_CIPHER_SUITE_TKIP:
1605 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1606 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
Eliad Peller1ad4f632016-02-14 13:56:36 +02001607 atomic64_set(&key->tx_pn,
1608 (u64)seq.tkip.iv16 |
1609 ((u64)seq.tkip.iv32 << 16));
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001610 break;
1611 }
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001612
1613 /* that's it for this key */
1614 return;
1615 }
1616
1617 if (data->find_phase) {
1618 data->last_gtk = key;
1619 data->cipher = key->cipher;
1620 return;
1621 }
1622
1623 if (data->status->num_of_gtk_rekeys)
1624 ieee80211_remove_key(key);
1625 else if (data->last_gtk == key)
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001626 iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001627}
1628
1629static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1630 struct ieee80211_vif *vif,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001631 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001632{
1633 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1634 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001635 .mvm = mvm,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001636 .status = status,
1637 };
Eliad Peller431591c2013-11-07 14:12:09 +02001638 u32 disconnection_reasons =
1639 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1640 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001641
1642 if (!status || !vif->bss_conf.bssid)
1643 return false;
1644
Eliad Peller431591c2013-11-07 14:12:09 +02001645 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1646 return false;
1647
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001648 /* find last GTK that we used initially, if any */
1649 gtkdata.find_phase = true;
1650 ieee80211_iter_keys(mvm->hw, vif,
Emmanuel Grumbach59fd4bf2015-11-01 13:06:37 +02001651 iwl_mvm_d3_update_keys, &gtkdata);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001652 /* not trying to keep connections with MFP/unhandled ciphers */
1653 if (gtkdata.unhandled_cipher)
1654 return false;
1655 if (!gtkdata.num_keys)
Luciano Coelhocfa88892013-11-19 15:38:55 +02001656 goto out;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001657 if (!gtkdata.last_gtk)
1658 return false;
1659
1660 /*
1661 * invalidate all other GTKs that might still exist and update
1662 * the one that we used
1663 */
1664 gtkdata.find_phase = false;
1665 ieee80211_iter_keys(mvm->hw, vif,
Emmanuel Grumbach59fd4bf2015-11-01 13:06:37 +02001666 iwl_mvm_d3_update_keys, &gtkdata);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001667
1668 if (status->num_of_gtk_rekeys) {
1669 struct ieee80211_key_conf *key;
1670 struct {
1671 struct ieee80211_key_conf conf;
1672 u8 key[32];
1673 } conf = {
1674 .conf.cipher = gtkdata.cipher,
1675 .conf.keyidx = status->gtk.key_index,
1676 };
1677
1678 switch (gtkdata.cipher) {
1679 case WLAN_CIPHER_SUITE_CCMP:
1680 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1681 memcpy(conf.conf.key, status->gtk.decrypt_key,
1682 WLAN_KEY_LEN_CCMP);
1683 break;
1684 case WLAN_CIPHER_SUITE_TKIP:
1685 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1686 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1687 /* leave TX MIC key zeroed, we don't use it anyway */
1688 memcpy(conf.conf.key +
1689 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1690 status->gtk.tkip_mic_key, 8);
1691 break;
1692 }
1693
1694 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1695 if (IS_ERR(key))
1696 return false;
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001697 iwl_mvm_set_key_rx_seq(mvm, key, status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001698 }
1699
1700 if (status->num_of_gtk_rekeys) {
1701 __be64 replay_ctr =
1702 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1703 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1704 (void *)&replay_ctr, GFP_KERNEL);
1705 }
1706
Luciano Coelhocfa88892013-11-19 15:38:55 +02001707out:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001708 mvmvif->seqno_valid = true;
1709 /* +0x10 because the set API expects next-to-use, not last-used */
1710 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1711
1712 return true;
1713}
1714
Luciano Coelhoec12f452014-11-20 10:14:44 +02001715static struct iwl_wowlan_status *
1716iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg7abc4632013-08-06 18:28:26 +02001717{
1718 u32 base = mvm->error_event_table;
1719 struct error_table_start {
1720 /* cf. struct iwl_error_event_table */
1721 u32 valid;
1722 u32 error_id;
1723 } err_info;
1724 struct iwl_host_cmd cmd = {
1725 .id = WOWLAN_GET_STATUSES,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001726 .flags = CMD_WANT_SKB,
Johannes Berg7abc4632013-08-06 18:28:26 +02001727 };
Luciano Coelhoec12f452014-11-20 10:14:44 +02001728 struct iwl_wowlan_status *status, *fw_status;
1729 int ret, len, status_size;
Johannes Berg7abc4632013-08-06 18:28:26 +02001730
1731 iwl_trans_read_mem_bytes(mvm->trans, base,
1732 &err_info, sizeof(err_info));
1733
1734 if (err_info.valid) {
Luciano Coelhoec12f452014-11-20 10:14:44 +02001735 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1736 err_info.valid, err_info.error_id);
Johannes Berg7abc4632013-08-06 18:28:26 +02001737 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1738 struct cfg80211_wowlan_wakeup wakeup = {
1739 .rfkill_release = true,
1740 };
1741 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1742 GFP_KERNEL);
1743 }
Luciano Coelhoec12f452014-11-20 10:14:44 +02001744 return ERR_PTR(-EIO);
Johannes Berg7abc4632013-08-06 18:28:26 +02001745 }
1746
1747 /* only for tracing for now */
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001748 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
Johannes Berg7abc4632013-08-06 18:28:26 +02001749 if (ret)
1750 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1751
1752 ret = iwl_mvm_send_cmd(mvm, &cmd);
1753 if (ret) {
1754 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
Luciano Coelhoec12f452014-11-20 10:14:44 +02001755 return ERR_PTR(ret);
Johannes Berg7abc4632013-08-06 18:28:26 +02001756 }
1757
1758 /* RF-kill already asserted again... */
Luciano Coelhoec12f452014-11-20 10:14:44 +02001759 if (!cmd.resp_pkt) {
Johannes Berge111e962015-03-03 20:23:47 +01001760 fw_status = ERR_PTR(-ERFKILL);
Luciano Coelhoec12f452014-11-20 10:14:44 +02001761 goto out_free_resp;
1762 }
Johannes Berg7abc4632013-08-06 18:28:26 +02001763
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001764 status_size = sizeof(*fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001765
Johannes Berg65b30342014-01-08 13:16:33 +01001766 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1767 if (len < status_size) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001768 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
Johannes Berge111e962015-03-03 20:23:47 +01001769 fw_status = ERR_PTR(-EIO);
Johannes Berg7abc4632013-08-06 18:28:26 +02001770 goto out_free_resp;
1771 }
1772
Luciano Coelhoec12f452014-11-20 10:14:44 +02001773 status = (void *)cmd.resp_pkt->data;
1774 if (len != (status_size +
1775 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1776 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
Johannes Berge111e962015-03-03 20:23:47 +01001777 fw_status = ERR_PTR(-EIO);
Luciano Coelhoec12f452014-11-20 10:14:44 +02001778 goto out_free_resp;
1779 }
1780
1781 fw_status = kmemdup(status, len, GFP_KERNEL);
1782
1783out_free_resp:
1784 iwl_free_resp(&cmd);
Johannes Berge111e962015-03-03 20:23:47 +01001785 return fw_status;
Luciano Coelhoec12f452014-11-20 10:14:44 +02001786}
1787
1788/* releases the MVM mutex */
1789static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1790 struct ieee80211_vif *vif)
1791{
1792 struct iwl_wowlan_status_data status;
1793 struct iwl_wowlan_status *fw_status;
1794 int i;
1795 bool keep;
1796 struct ieee80211_sta *ap_sta;
1797 struct iwl_mvm_sta *mvm_ap_sta;
1798
1799 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1800 if (IS_ERR_OR_NULL(fw_status))
1801 goto out_unlock;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001802
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001803 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1804 for (i = 0; i < 8; i++)
1805 status.qos_seq_ctr[i] =
1806 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1807 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1808 status.wake_packet_length =
1809 le32_to_cpu(fw_status->wake_packet_length);
1810 status.wake_packet_bufsize =
1811 le32_to_cpu(fw_status->wake_packet_bufsize);
1812 status.wake_packet = fw_status->wake_packet;
Johannes Berg7abc4632013-08-06 18:28:26 +02001813
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001814 /* still at hard-coded place 0 for D3 image */
1815 ap_sta = rcu_dereference_protected(
1816 mvm->fw_id_to_mac_id[0],
1817 lockdep_is_held(&mvm->mutex));
1818 if (IS_ERR_OR_NULL(ap_sta))
Luciano Coelhoec12f452014-11-20 10:14:44 +02001819 goto out_free;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001820
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001821 mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001822 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1823 u16 seq = status.qos_seq_ctr[i];
1824 /* firmware stores last-used value, we store next value */
1825 seq += 0x10;
1826 mvm_ap_sta->tid_data[i].seq_number = seq;
1827 }
1828
Johannes Berg7abc4632013-08-06 18:28:26 +02001829 /* now we have all the data we need, unlock to avoid mac80211 issues */
1830 mutex_unlock(&mvm->mutex);
1831
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001832 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1833
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001834 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001835
Luciano Coelhoec12f452014-11-20 10:14:44 +02001836 kfree(fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001837 return keep;
Johannes Berg7abc4632013-08-06 18:28:26 +02001838
Luciano Coelhoec12f452014-11-20 10:14:44 +02001839out_free:
1840 kfree(fw_status);
1841out_unlock:
Johannes Berg7abc4632013-08-06 18:28:26 +02001842 mutex_unlock(&mvm->mutex);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001843 return false;
Johannes Berg9b26b502013-01-22 13:02:09 +01001844}
1845
Eliad Pellera3f7ba52015-11-11 17:23:59 +02001846void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
1847 struct ieee80211_vif *vif,
1848 struct iwl_wowlan_status *status)
1849{
1850 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
Eliad Peller0eb1c962015-12-31 15:19:32 +02001851 .mvm = mvm,
Eliad Pellera3f7ba52015-11-11 17:23:59 +02001852 .status = status,
1853 };
1854
1855 /*
1856 * rekey handling requires taking locks that can't be taken now.
1857 * however, d0i3 doesn't offload rekey, so we're fine.
1858 */
1859 if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
1860 return;
1861
1862 /* find last GTK that we used initially, if any */
1863 gtkdata.find_phase = true;
1864 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
1865
1866 gtkdata.find_phase = false;
1867 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
1868}
1869
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001870struct iwl_mvm_nd_query_results {
1871 u32 matched_profiles;
1872 struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1873};
1874
1875static int
1876iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1877 struct iwl_mvm_nd_query_results *results)
Luciano Coelhob04998f2014-11-20 15:58:34 +02001878{
1879 struct iwl_scan_offload_profiles_query *query;
1880 struct iwl_host_cmd cmd = {
1881 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1882 .flags = CMD_WANT_SKB,
1883 };
1884 int ret, len;
1885
1886 ret = iwl_mvm_send_cmd(mvm, &cmd);
1887 if (ret) {
1888 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001889 return ret;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001890 }
1891
1892 /* RF-kill already asserted again... */
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001893 if (!cmd.resp_pkt) {
1894 ret = -ERFKILL;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001895 goto out_free_resp;
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001896 }
Luciano Coelhob04998f2014-11-20 15:58:34 +02001897
1898 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1899 if (len < sizeof(*query)) {
1900 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001901 ret = -EIO;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001902 goto out_free_resp;
1903 }
1904
1905 query = (void *)cmd.resp_pkt->data;
1906
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001907 results->matched_profiles = le32_to_cpu(query->matched_profiles);
1908 memcpy(results->matches, query->matches, sizeof(results->matches));
Luciano Coelhob04998f2014-11-20 15:58:34 +02001909
Emmanuel Grumbach6bbd5522015-04-28 15:00:23 +03001910#ifdef CONFIG_IWLWIFI_DEBUGFS
Luciano Coelho484b3d12015-03-30 20:46:32 +03001911 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1912#endif
1913
Luciano Coelhob04998f2014-11-20 15:58:34 +02001914out_free_resp:
1915 iwl_free_resp(&cmd);
1916 return ret;
1917}
1918
Luciano Coelho2021a892014-11-20 08:59:51 +02001919static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1920 struct ieee80211_vif *vif)
1921{
Luciano Coelhod9718da2014-11-21 09:32:23 +02001922 struct cfg80211_wowlan_nd_info *net_detect = NULL;
Luciano Coelho2021a892014-11-20 08:59:51 +02001923 struct cfg80211_wowlan_wakeup wakeup = {
1924 .pattern_idx = -1,
1925 };
1926 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001927 struct iwl_mvm_nd_query_results query;
Luciano Coelho2021a892014-11-20 08:59:51 +02001928 struct iwl_wowlan_status *fw_status;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001929 unsigned long matched_profiles;
Luciano Coelho2021a892014-11-20 08:59:51 +02001930 u32 reasons = 0;
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001931 int i, j, n_matches, ret;
Luciano Coelho2021a892014-11-20 08:59:51 +02001932
1933 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
Haim Dreyfuss2fc863a2015-05-20 08:10:43 +03001934 if (!IS_ERR_OR_NULL(fw_status)) {
Luciano Coelho2021a892014-11-20 08:59:51 +02001935 reasons = le32_to_cpu(fw_status->wakeup_reasons);
Haim Dreyfuss2fc863a2015-05-20 08:10:43 +03001936 kfree(fw_status);
1937 }
Luciano Coelho2021a892014-11-20 08:59:51 +02001938
1939 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1940 wakeup.rfkill_release = true;
1941
Luciano Coelhob04998f2014-11-20 15:58:34 +02001942 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1943 goto out;
1944
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001945 ret = iwl_mvm_netdetect_query_results(mvm, &query);
1946 if (ret || !query.matched_profiles) {
Luciano Coelho2021a892014-11-20 08:59:51 +02001947 wakeup_report = NULL;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001948 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001949 }
1950
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001951 matched_profiles = query.matched_profiles;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001952 if (mvm->n_nd_match_sets) {
1953 n_matches = hweight_long(matched_profiles);
1954 } else {
1955 IWL_ERR(mvm, "no net detect match information available\n");
1956 n_matches = 0;
1957 }
1958
1959 net_detect = kzalloc(sizeof(*net_detect) +
1960 (n_matches * sizeof(net_detect->matches[0])),
1961 GFP_KERNEL);
1962 if (!net_detect || !n_matches)
1963 goto out_report_nd;
1964
1965 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001966 struct iwl_scan_offload_profile_match *fw_match;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001967 struct cfg80211_wowlan_nd_match *match;
Luciano Coelhoae90c2e52015-05-06 10:45:42 +03001968 int idx, n_channels = 0;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001969
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001970 fw_match = &query.matches[i];
1971
1972 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1973 n_channels += hweight8(fw_match->matching_channels[j]);
1974
1975 match = kzalloc(sizeof(*match) +
1976 (n_channels * sizeof(*match->channels)),
1977 GFP_KERNEL);
Luciano Coelhod9718da2014-11-21 09:32:23 +02001978 if (!match)
1979 goto out_report_nd;
1980
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001981 net_detect->matches[net_detect->n_matches++] = match;
1982
Luciano Coelhoae90c2e52015-05-06 10:45:42 +03001983 /* We inverted the order of the SSIDs in the scan
1984 * request, so invert the index here.
1985 */
1986 idx = mvm->n_nd_match_sets - i - 1;
1987 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
1988 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
Luciano Coelhod9718da2014-11-21 09:32:23 +02001989 match->ssid.ssid_len);
1990
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001991 if (mvm->n_nd_channels < n_channels)
1992 continue;
1993
1994 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
1995 if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
1996 match->channels[match->n_channels++] =
1997 mvm->nd_channels[j]->center_freq;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001998 }
1999
2000out_report_nd:
2001 wakeup.net_detect = net_detect;
Luciano Coelhob04998f2014-11-20 15:58:34 +02002002out:
Luciano Coelhod9718da2014-11-21 09:32:23 +02002003 iwl_mvm_free_nd(mvm);
2004
Luciano Coelho2021a892014-11-20 08:59:51 +02002005 mutex_unlock(&mvm->mutex);
2006 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
Luciano Coelhod9718da2014-11-21 09:32:23 +02002007
2008 if (net_detect) {
2009 for (i = 0; i < net_detect->n_matches; i++)
2010 kfree(net_detect->matches[i]);
2011 kfree(net_detect);
2012 }
Luciano Coelho2021a892014-11-20 08:59:51 +02002013}
2014
Johannes Bergafc66bb2013-05-03 11:44:16 +02002015static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
2016{
2017#ifdef CONFIG_IWLWIFI_DEBUGFS
2018 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
2019 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
2020 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
2021
2022 if (!mvm->store_d3_resume_sram)
2023 return;
2024
2025 if (!mvm->d3_resume_sram) {
2026 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
2027 if (!mvm->d3_resume_sram)
2028 return;
2029 }
2030
2031 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
2032#endif
2033}
2034
Johannes Berg6d9d32b2013-08-06 18:58:56 +02002035static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2036 struct ieee80211_vif *vif)
2037{
2038 /* skip the one we keep connection on */
2039 if (data == vif)
2040 return;
2041
2042 if (vif->type == NL80211_IFTYPE_STATION)
2043 ieee80211_resume_disconnect(vif);
2044}
2045
Johannes Bergdebff612013-05-14 13:53:45 +02002046static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002047{
Johannes Berg8ca151b2013-01-24 14:25:36 +01002048 struct ieee80211_vif *vif = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002049 int ret;
2050 enum iwl_d3_status d3_status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02002051 bool keep = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002052
2053 mutex_lock(&mvm->mutex);
2054
2055 /* get the BSS vif pointer again */
Luciano Coelho7f549e22014-10-02 15:38:04 +03002056 vif = iwl_mvm_get_bss_vif(mvm);
2057 if (IS_ERR_OR_NULL(vif))
Luciano Coelhoa500e462015-05-04 17:03:17 +03002058 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002059
Johannes Bergdebff612013-05-14 13:53:45 +02002060 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002061 if (ret)
Luciano Coelhoa500e462015-05-04 17:03:17 +03002062 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002063
2064 if (d3_status != IWL_D3_STATUS_ALIVE) {
2065 IWL_INFO(mvm, "Device was reset during suspend\n");
Luciano Coelhoa500e462015-05-04 17:03:17 +03002066 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002067 }
2068
Johannes Bergafc66bb2013-05-03 11:44:16 +02002069 /* query SRAM first in case we want event logging */
2070 iwl_mvm_read_d3_sram(mvm);
2071
Jonathan Doron47c8b152014-11-27 16:55:25 +02002072 /*
2073 * Query the current location and source from the D3 firmware so we
2074 * can play it back when we re-intiailize the D0 firmware
2075 */
2076 iwl_mvm_update_changed_regdom(mvm);
2077
Luciano Coelho2021a892014-11-20 08:59:51 +02002078 if (mvm->net_detect) {
2079 iwl_mvm_query_netdetect_reasons(mvm, vif);
Luciano Coelhoe0ede172015-02-13 21:37:09 +02002080 /* has unlocked the mutex, so skip that */
2081 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02002082 } else {
2083 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
Eliad Peller78c9df62013-11-07 14:13:30 +02002084#ifdef CONFIG_IWLWIFI_DEBUGFS
Luciano Coelho2021a892014-11-20 08:59:51 +02002085 if (keep)
2086 mvm->keep_vif = vif;
Luciano Coelho0c2ae042015-03-12 09:25:15 +02002087#endif
Luciano Coelhoe0ede172015-02-13 21:37:09 +02002088 /* has unlocked the mutex, so skip that */
2089 goto out_iterate;
Luciano Coelho2021a892014-11-20 08:59:51 +02002090 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002091
Luciano Coelhoa500e462015-05-04 17:03:17 +03002092err:
2093 iwl_mvm_free_nd(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002094 mutex_unlock(&mvm->mutex);
2095
Luciano Coelhoe0ede172015-02-13 21:37:09 +02002096out_iterate:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02002097 if (!test)
2098 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
2099 IEEE80211_IFACE_ITER_NORMAL,
2100 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002101
Luciano Coelhoe0ede172015-02-13 21:37:09 +02002102out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002103 /* return 1 to reconfigure the device */
2104 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
Johannes Berg58629d92014-11-06 09:40:50 +01002105 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
Luciano Coelhodcfc7fb2015-04-28 08:41:55 +03002106
2107 /* We always return 1, which causes mac80211 to do a reconfig
2108 * with IEEE80211_RECONFIG_TYPE_RESTART. This type of
2109 * reconfig calls iwl_mvm_restart_complete(), where we unref
2110 * the IWL_MVM_REF_UCODE_DOWN, so we need to take the
2111 * reference here.
2112 */
2113 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002114 return 1;
2115}
2116
Eliad Peller2d428012015-06-17 15:32:37 +03002117static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
2118{
2119 iwl_trans_resume(mvm->trans);
2120
2121 return __iwl_mvm_resume(mvm, false);
2122}
2123
2124static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
2125{
2126 bool exit_now;
Eliad Peller6dfb36c2015-07-09 14:17:24 +03002127 enum iwl_d3_status d3_status;
Luca Coelhob7282642015-09-17 23:44:14 +03002128 struct iwl_trans *trans = mvm->trans;
Eliad Peller6dfb36c2015-07-09 14:17:24 +03002129
Luca Coelhob7282642015-09-17 23:44:14 +03002130 iwl_trans_d3_resume(trans, &d3_status, false);
Eliad Peller2d428012015-06-17 15:32:37 +03002131
2132 /*
2133 * make sure to clear D0I3_DEFER_WAKEUP before
2134 * calling iwl_trans_resume(), which might wait
2135 * for d0i3 exit completion.
2136 */
2137 mutex_lock(&mvm->d0i3_suspend_mutex);
2138 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
2139 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
2140 &mvm->d0i3_suspend_flags);
2141 mutex_unlock(&mvm->d0i3_suspend_mutex);
2142 if (exit_now) {
2143 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
2144 _iwl_mvm_exit_d0i3(mvm);
2145 }
2146
Luca Coelhob7282642015-09-17 23:44:14 +03002147 iwl_trans_resume(trans);
Eliad Peller2d428012015-06-17 15:32:37 +03002148
Luca Coelhob7282642015-09-17 23:44:14 +03002149 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
Eliad Peller2d428012015-06-17 15:32:37 +03002150 int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
2151
2152 if (ret)
2153 return ret;
2154 /*
2155 * d0i3 exit will be deferred until reconfig_complete.
2156 * make sure there we are out of d0i3.
2157 */
2158 }
2159 return 0;
2160}
2161
Johannes Bergdebff612013-05-14 13:53:45 +02002162int iwl_mvm_resume(struct ieee80211_hw *hw)
2163{
2164 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Luca Coelhob7282642015-09-17 23:44:14 +03002165 int ret;
Johannes Bergdebff612013-05-14 13:53:45 +02002166
Luca Coelhob7282642015-09-17 23:44:14 +03002167 if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
2168 ret = iwl_mvm_resume_d0i3(mvm);
Eliad Peller2d428012015-06-17 15:32:37 +03002169 else
Luca Coelhob7282642015-09-17 23:44:14 +03002170 ret = iwl_mvm_resume_d3(mvm);
2171
2172 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2173
2174 return ret;
Johannes Bergdebff612013-05-14 13:53:45 +02002175}
2176
Johannes Berg8ca151b2013-01-24 14:25:36 +01002177void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2178{
2179 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2180
2181 device_set_wakeup_enable(mvm->trans->dev, enabled);
2182}
Johannes Bergdebff612013-05-14 13:53:45 +02002183
2184#ifdef CONFIG_IWLWIFI_DEBUGFS
2185static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2186{
2187 struct iwl_mvm *mvm = inode->i_private;
2188 int err;
2189
2190 if (mvm->d3_test_active)
2191 return -EBUSY;
2192
2193 file->private_data = inode->i_private;
2194
2195 ieee80211_stop_queues(mvm->hw);
2196 synchronize_net();
2197
Luca Coelhob7282642015-09-17 23:44:14 +03002198 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
2199
Johannes Bergdebff612013-05-14 13:53:45 +02002200 /* start pseudo D3 */
2201 rtnl_lock();
2202 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2203 rtnl_unlock();
2204 if (err > 0)
2205 err = -EINVAL;
2206 if (err) {
2207 ieee80211_wake_queues(mvm->hw);
2208 return err;
2209 }
2210 mvm->d3_test_active = true;
Eliad Peller78c9df62013-11-07 14:13:30 +02002211 mvm->keep_vif = NULL;
Johannes Bergdebff612013-05-14 13:53:45 +02002212 return 0;
2213}
2214
2215static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2216 size_t count, loff_t *ppos)
2217{
2218 struct iwl_mvm *mvm = file->private_data;
2219 u32 pme_asserted;
2220
2221 while (true) {
Eliad Peller4ed80a82013-11-07 13:23:21 +02002222 /* read pme_ptr if available */
2223 if (mvm->d3_test_pme_ptr) {
2224 pme_asserted = iwl_trans_read_mem32(mvm->trans,
2225 mvm->d3_test_pme_ptr);
2226 if (pme_asserted)
2227 break;
2228 }
2229
Johannes Bergdebff612013-05-14 13:53:45 +02002230 if (msleep_interruptible(100))
2231 break;
2232 }
2233
2234 return 0;
2235}
2236
2237static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2238 struct ieee80211_vif *vif)
2239{
Eliad Peller78c9df62013-11-07 14:13:30 +02002240 /* skip the one we keep connection on */
2241 if (_data == vif)
2242 return;
2243
Johannes Bergdebff612013-05-14 13:53:45 +02002244 if (vif->type == NL80211_IFTYPE_STATION)
2245 ieee80211_connection_loss(vif);
2246}
2247
2248static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2249{
2250 struct iwl_mvm *mvm = inode->i_private;
2251 int remaining_time = 10;
2252
2253 mvm->d3_test_active = false;
Luca Coelhob7282642015-09-17 23:44:14 +03002254
Luciano Coelho31ca09d2014-09-22 13:22:51 +03002255 rtnl_lock();
Johannes Bergdebff612013-05-14 13:53:45 +02002256 __iwl_mvm_resume(mvm, true);
Luciano Coelho31ca09d2014-09-22 13:22:51 +03002257 rtnl_unlock();
Luca Coelhob7282642015-09-17 23:44:14 +03002258
2259 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2260
Johannes Bergdebff612013-05-14 13:53:45 +02002261 iwl_abort_notification_waits(&mvm->notif_wait);
2262 ieee80211_restart_hw(mvm->hw);
2263
2264 /* wait for restart and disconnect all interfaces */
2265 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2266 remaining_time > 0) {
2267 remaining_time--;
2268 msleep(1000);
2269 }
2270
2271 if (remaining_time == 0)
2272 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
2273
2274 ieee80211_iterate_active_interfaces_atomic(
2275 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
Eliad Peller78c9df62013-11-07 14:13:30 +02002276 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
Johannes Bergdebff612013-05-14 13:53:45 +02002277
2278 ieee80211_wake_queues(mvm->hw);
2279
2280 return 0;
2281}
2282
2283const struct file_operations iwl_dbgfs_d3_test_ops = {
2284 .llseek = no_llseek,
2285 .open = iwl_mvm_d3_test_open,
2286 .read = iwl_mvm_d3_test_read,
2287 .release = iwl_mvm_d3_test_release,
2288};
2289#endif