blob: 0e6cf39285f405d8c42e65ea2d0ef7d4e86179f0 [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
Luca Coelhofcea37b2017-04-12 16:33:41 +030010 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * USA
25 *
26 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020027 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010028 *
29 * Contact Information:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020030 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg8ca151b2013-01-24 14:25:36 +010031 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32 *
33 * BSD LICENSE
34 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020035 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Luciano Coelho65ff5562015-03-20 13:35:47 +020036 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Luca Coelhofcea37b2017-04-12 16:33:41 +030037 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010038 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 *
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
49 * distribution.
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 *****************************************************************************/
67
Johannes Bergf444eb12013-02-26 12:04:18 +010068#include <linux/etherdevice.h>
Johannes Bergf0c26462013-01-22 20:41:58 +010069#include <linux/ip.h>
Johannes Bergdebff612013-05-14 13:53:45 +020070#include <linux/fs.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010071#include <net/cfg80211.h>
72#include <net/ipv6.h>
Johannes Bergf0c26462013-01-22 20:41:58 +010073#include <net/tcp.h>
Johannes Bergf7fc5982013-08-20 13:04:10 +020074#include <net/addrconf.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010075#include "iwl-modparams.h"
76#include "fw-api.h"
77#include "mvm.h"
78
79void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
80 struct ieee80211_vif *vif,
81 struct cfg80211_gtk_rekey_data *data)
82{
83 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
84 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
85
Johannes Berg3b37f4c2017-05-30 16:45:31 +020086 if (iwlwifi_mod_params.swcrypto)
Johannes Berg8ca151b2013-01-24 14:25:36 +010087 return;
88
89 mutex_lock(&mvm->mutex);
90
91 memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
92 memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
93 mvmvif->rekey_data.replay_ctr =
Johannes Bergcba84572016-11-30 17:04:52 +010094 cpu_to_le64(be64_to_cpup((__be64 *)data->replay_ctr));
Johannes Berg8ca151b2013-01-24 14:25:36 +010095 mvmvif->rekey_data.valid = true;
96
97 mutex_unlock(&mvm->mutex);
98}
99
100#if IS_ENABLED(CONFIG_IPV6)
101void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
102 struct ieee80211_vif *vif,
103 struct inet6_dev *idev)
104{
105 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
106 struct inet6_ifaddr *ifa;
107 int idx = 0;
108
Sara Sharonc97dab42015-11-19 11:53:49 +0200109 memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
110
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200111 read_lock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100112 list_for_each_entry(ifa, &idev->addr_list, if_list) {
113 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
Sara Sharonc97dab42015-11-19 11:53:49 +0200114 if (ifa->flags & IFA_F_TENTATIVE)
115 __set_bit(idx, mvmvif->tentative_addrs);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100116 idx++;
Johannes Berg5369d6c2013-07-08 11:17:06 +0200117 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100118 break;
119 }
Emmanuel Grumbacha3777e02013-02-10 13:07:08 +0200120 read_unlock_bh(&idev->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100121
122 mvmvif->num_target_ipv6_addrs = idx;
123}
124#endif
125
126void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
127 struct ieee80211_vif *vif, int idx)
128{
129 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
130
131 mvmvif->tx_key_idx = idx;
132}
133
134static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
135{
136 int i;
137
138 for (i = 0; i < IWL_P1K_SIZE; i++)
139 out[i] = cpu_to_le16(p1k[i]);
140}
141
Johannes Bergf5e28ea2015-12-06 14:58:08 +0200142static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
143 struct iwl_mvm_key_pn *ptk_pn,
144 struct ieee80211_key_seq *seq,
145 int tid, int queues)
146{
147 const u8 *ret = seq->ccmp.pn;
148 int i;
149
150 /* get the PN from mac80211, used on the default queue */
151 ieee80211_get_key_rx_seq(key, tid, seq);
152
153 /* and use the internal data for the other queues */
154 for (i = 1; i < queues; i++) {
155 const u8 *tmp = ptk_pn->q[i].pn[tid];
156
157 if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
158 ret = tmp;
159 }
160
161 return ret;
162}
163
Johannes Berg8ca151b2013-01-24 14:25:36 +0100164struct wowlan_key_data {
165 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
166 struct iwl_wowlan_tkip_params_cmd *tkip;
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200167 bool error, use_rsc_tsc, use_tkip, configure_keys;
Johannes Berg5312e542013-07-22 18:26:56 +0200168 int wep_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100169};
170
171static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
172 struct ieee80211_vif *vif,
173 struct ieee80211_sta *sta,
174 struct ieee80211_key_conf *key,
175 void *_data)
176{
177 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
178 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
179 struct wowlan_key_data *data = _data;
180 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
181 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
182 struct iwl_p1k_cache *rx_p1ks;
183 u8 *rx_mic_key;
184 struct ieee80211_key_seq seq;
185 u32 cur_rx_iv32 = 0;
186 u16 p1k[IWL_P1K_SIZE];
187 int ret, i;
188
Johannes Berg8ca151b2013-01-24 14:25:36 +0100189 switch (key->cipher) {
190 case WLAN_CIPHER_SUITE_WEP40:
191 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
192 struct {
193 struct iwl_mvm_wep_key_cmd wep_key_cmd;
194 struct iwl_mvm_wep_key wep_key;
195 } __packed wkc = {
196 .wep_key_cmd.mac_id_n_color =
197 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
198 mvmvif->color)),
199 .wep_key_cmd.num_keys = 1,
200 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
201 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
202 .wep_key.key_index = key->keyidx,
203 .wep_key.key_size = key->keylen,
204 };
205
206 /*
207 * This will fail -- the key functions don't set support
208 * pairwise WEP keys. However, that's better than silently
209 * failing WoWLAN. Or maybe not?
210 */
211 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
212 break;
213
214 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
215 if (key->keyidx == mvmvif->tx_key_idx) {
216 /* TX key must be at offset 0 */
217 wkc.wep_key.key_offset = 0;
218 } else {
219 /* others start at 1 */
Johannes Berg5312e542013-07-22 18:26:56 +0200220 data->wep_key_idx++;
221 wkc.wep_key.key_offset = data->wep_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100222 }
223
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200224 if (data->configure_keys) {
225 mutex_lock(&mvm->mutex);
226 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0,
227 sizeof(wkc), &wkc);
228 data->error = ret != 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100229
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200230 mvm->ptk_ivlen = key->iv_len;
231 mvm->ptk_icvlen = key->icv_len;
232 mvm->gtk_ivlen = key->iv_len;
233 mvm->gtk_icvlen = key->icv_len;
234 mutex_unlock(&mvm->mutex);
235 }
Johannes Bergf444eb12013-02-26 12:04:18 +0100236
Johannes Berg8ca151b2013-01-24 14:25:36 +0100237 /* don't upload key again */
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200238 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100239 }
240 default:
241 data->error = true;
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200242 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100243 case WLAN_CIPHER_SUITE_AES_CMAC:
244 /*
245 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
246 * but we also shouldn't abort suspend due to that. It does have
247 * support for the IGTK key renewal, but doesn't really use the
248 * IGTK for anything. This means we could spuriously wake up or
249 * be deauthenticated, but that was considered acceptable.
250 */
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200251 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100252 case WLAN_CIPHER_SUITE_TKIP:
253 if (sta) {
Eliad Peller1ad4f632016-02-14 13:56:36 +0200254 u64 pn64;
255
Johannes Berg8ca151b2013-01-24 14:25:36 +0100256 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
257 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
258
259 rx_p1ks = data->tkip->rx_uni;
260
Eliad Peller1ad4f632016-02-14 13:56:36 +0200261 pn64 = atomic64_read(&key->tx_pn);
262 tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
263 tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100264
Eliad Peller1ad4f632016-02-14 13:56:36 +0200265 ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
266 p1k);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100267 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
268
269 memcpy(data->tkip->mic_keys.tx,
270 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
271 IWL_MIC_KEY_SIZE);
272
273 rx_mic_key = data->tkip->mic_keys.rx_unicast;
274 } else {
275 tkip_sc =
276 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
277 rx_p1ks = data->tkip->rx_multi;
278 rx_mic_key = data->tkip->mic_keys.rx_mcast;
279 }
280
281 /*
282 * For non-QoS this relies on the fact that both the uCode and
283 * mac80211 use TID 0 (as they need to to avoid replay attacks)
284 * for checking the IV in the frames.
285 */
286 for (i = 0; i < IWL_NUM_RSC; i++) {
287 ieee80211_get_key_rx_seq(key, i, &seq);
288 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
289 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
290 /* wrapping isn't allowed, AP must rekey */
291 if (seq.tkip.iv32 > cur_rx_iv32)
292 cur_rx_iv32 = seq.tkip.iv32;
293 }
294
295 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
296 cur_rx_iv32, p1k);
297 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
298 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
299 cur_rx_iv32 + 1, p1k);
300 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
301
302 memcpy(rx_mic_key,
303 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
304 IWL_MIC_KEY_SIZE);
305
306 data->use_tkip = true;
307 data->use_rsc_tsc = true;
308 break;
309 case WLAN_CIPHER_SUITE_CCMP:
310 if (sta) {
Johannes Berg6645d5e2015-09-15 09:58:53 +0200311 u64 pn64;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100312
313 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
314 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
315
Johannes Berg6645d5e2015-09-15 09:58:53 +0200316 pn64 = atomic64_read(&key->tx_pn);
317 aes_tx_sc->pn = cpu_to_le64(pn64);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100318 } else {
319 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
320 }
321
322 /*
323 * For non-QoS this relies on the fact that both the uCode and
Johannes Bergf5e28ea2015-12-06 14:58:08 +0200324 * mac80211/our RX code use TID 0 for checking the PN.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100325 */
Johannes Bergf5e28ea2015-12-06 14:58:08 +0200326 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
327 struct iwl_mvm_sta *mvmsta;
328 struct iwl_mvm_key_pn *ptk_pn;
329 const u8 *pn;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100330
Johannes Bergf5e28ea2015-12-06 14:58:08 +0200331 mvmsta = iwl_mvm_sta_from_mac80211(sta);
332 ptk_pn = rcu_dereference_protected(
333 mvmsta->ptk_pn[key->keyidx],
334 lockdep_is_held(&mvm->mutex));
335 if (WARN_ON(!ptk_pn))
336 break;
337
338 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
339 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
340 mvm->trans->num_rx_queues);
341 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
342 ((u64)pn[4] << 8) |
343 ((u64)pn[3] << 16) |
344 ((u64)pn[2] << 24) |
345 ((u64)pn[1] << 32) |
346 ((u64)pn[0] << 40));
347 }
348 } else {
349 for (i = 0; i < IWL_NUM_RSC; i++) {
350 u8 *pn = seq.ccmp.pn;
351
352 ieee80211_get_key_rx_seq(key, i, &seq);
353 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
354 ((u64)pn[4] << 8) |
355 ((u64)pn[3] << 16) |
356 ((u64)pn[2] << 24) |
357 ((u64)pn[1] << 32) |
358 ((u64)pn[0] << 40));
359 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100360 }
361 data->use_rsc_tsc = true;
362 break;
363 }
364
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200365 if (data->configure_keys) {
366 mutex_lock(&mvm->mutex);
Johannes Berg5312e542013-07-22 18:26:56 +0200367 /*
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200368 * The D3 firmware hardcodes the key offset 0 as the key it
369 * uses to transmit packets to the AP, i.e. the PTK.
Johannes Berg5312e542013-07-22 18:26:56 +0200370 */
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200371 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
372 mvm->ptk_ivlen = key->iv_len;
373 mvm->ptk_icvlen = key->icv_len;
374 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
375 } else {
376 /*
377 * firmware only supports TSC/RSC for a single key,
378 * so if there are multiple keep overwriting them
379 * with new ones -- this relies on mac80211 doing
380 * list_add_tail().
381 */
382 mvm->gtk_ivlen = key->iv_len;
383 mvm->gtk_icvlen = key->icv_len;
384 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
385 }
386 mutex_unlock(&mvm->mutex);
387 data->error = ret != 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100388 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100389}
390
391static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
392 struct cfg80211_wowlan *wowlan)
393{
394 struct iwl_wowlan_patterns_cmd *pattern_cmd;
395 struct iwl_host_cmd cmd = {
396 .id = WOWLAN_PATTERNS,
397 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100398 };
399 int i, err;
400
401 if (!wowlan->n_patterns)
402 return 0;
403
404 cmd.len[0] = sizeof(*pattern_cmd) +
405 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
406
407 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
408 if (!pattern_cmd)
409 return -ENOMEM;
410
411 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
412
413 for (i = 0; i < wowlan->n_patterns; i++) {
414 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
415
416 memcpy(&pattern_cmd->patterns[i].mask,
417 wowlan->patterns[i].mask, mask_len);
418 memcpy(&pattern_cmd->patterns[i].pattern,
419 wowlan->patterns[i].pattern,
420 wowlan->patterns[i].pattern_len);
421 pattern_cmd->patterns[i].mask_size = mask_len;
422 pattern_cmd->patterns[i].pattern_size =
423 wowlan->patterns[i].pattern_len;
424 }
425
426 cmd.data[0] = pattern_cmd;
427 err = iwl_mvm_send_cmd(mvm, &cmd);
428 kfree(pattern_cmd);
429 return err;
430}
431
Johannes Berg8ca151b2013-01-24 14:25:36 +0100432static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
433 struct ieee80211_sta *ap_sta)
434{
435 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
436 struct ieee80211_chanctx_conf *ctx;
437 u8 chains_static, chains_dynamic;
438 struct cfg80211_chan_def chandef;
439 int ret, i;
440 struct iwl_binding_cmd binding_cmd = {};
441 struct iwl_time_quota_cmd quota_cmd = {};
David Spinadel72cbb732017-08-30 16:23:14 +0300442 struct iwl_time_quota_data *quota;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100443 u32 status;
Sara Sharon9415af72016-11-17 13:57:32 +0200444 int size;
445
446 if (fw_has_capa(&mvm->fw->ucode_capa,
447 IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT)) {
448 size = sizeof(binding_cmd);
449 if (mvmvif->phy_ctxt->channel->band == NL80211_BAND_2GHZ ||
450 !iwl_mvm_is_cdb_supported(mvm))
451 binding_cmd.lmac_id = cpu_to_le32(IWL_LMAC_24G_INDEX);
452 else
453 binding_cmd.lmac_id = cpu_to_le32(IWL_LMAC_5G_INDEX);
454 } else {
455 size = IWL_BINDING_CMD_SIZE_V1;
456 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100457
458 /* add back the PHY */
459 if (WARN_ON(!mvmvif->phy_ctxt))
460 return -EINVAL;
461
462 rcu_read_lock();
463 ctx = rcu_dereference(vif->chanctx_conf);
464 if (WARN_ON(!ctx)) {
465 rcu_read_unlock();
466 return -EINVAL;
467 }
468 chandef = ctx->def;
469 chains_static = ctx->rx_chains_static;
470 chains_dynamic = ctx->rx_chains_dynamic;
471 rcu_read_unlock();
472
473 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
474 chains_static, chains_dynamic);
475 if (ret)
476 return ret;
477
478 /* add back the MAC */
479 mvmvif->uploaded = false;
480
481 if (WARN_ON(!vif->bss_conf.assoc))
482 return -EINVAL;
Luciano Coelhobca49d92014-05-13 17:33:38 +0300483
Johannes Berg8ca151b2013-01-24 14:25:36 +0100484 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100485 if (ret)
486 return ret;
487
488 /* add back binding - XXX refactor? */
489 binding_cmd.id_and_color =
490 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
491 mvmvif->phy_ctxt->color));
492 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
493 binding_cmd.phy =
494 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
495 mvmvif->phy_ctxt->color));
496 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
497 mvmvif->color));
498 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
499 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
500
501 status = 0;
502 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
Sara Sharon9415af72016-11-17 13:57:32 +0200503 size, &binding_cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100504 if (ret) {
505 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
506 return ret;
507 }
508
509 if (status) {
510 IWL_ERR(mvm, "Binding command failed: %u\n", status);
511 return -EIO;
512 }
513
Liad Kaufman24afba72015-07-28 18:56:08 +0300514 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100515 if (ret)
516 return ret;
517 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
518
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200519 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100520 if (ret)
521 return ret;
522
523 /* and some quota */
David Spinadel72cbb732017-08-30 16:23:14 +0300524 quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, 0);
525 quota->id_and_color =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100526 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
527 mvmvif->phy_ctxt->color));
David Spinadel72cbb732017-08-30 16:23:14 +0300528 quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
529 quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100530
David Spinadel72cbb732017-08-30 16:23:14 +0300531 for (i = 1; i < MAX_BINDINGS; i++) {
532 quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, i);
533 quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
534 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100535
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300536 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
David Spinadel72cbb732017-08-30 16:23:14 +0300537 iwl_mvm_quota_cmd_size(mvm), &quota_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100538 if (ret)
539 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
540
Jonathan Doron47c8b152014-11-27 16:55:25 +0200541 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
542 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
543
Johannes Berg8ca151b2013-01-24 14:25:36 +0100544 return 0;
545}
546
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200547static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
548 struct ieee80211_vif *vif)
549{
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200550 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
551 struct iwl_nonqos_seq_query_cmd query_cmd = {
552 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
553 .mac_id_n_color =
554 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
555 mvmvif->color)),
556 };
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200557 struct iwl_host_cmd cmd = {
558 .id = NON_QOS_TX_COUNTER_CMD,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300559 .flags = CMD_WANT_SKB,
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200560 };
561 int err;
562 u32 size;
563
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300564 cmd.data[0] = &query_cmd;
565 cmd.len[0] = sizeof(query_cmd);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200566
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200567 err = iwl_mvm_send_cmd(mvm, &cmd);
568 if (err)
569 return err;
570
Johannes Berg65b30342014-01-08 13:16:33 +0100571 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200572 if (size < sizeof(__le16)) {
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200573 err = -EINVAL;
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200574 } else {
575 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +0300576 /* firmware returns next, not last-used seqno */
577 err = (u16) (err - 0x10);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200578 }
Johannes Berg4ac6cb52013-08-08 09:30:13 +0200579
580 iwl_free_resp(&cmd);
581 return err;
582}
583
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200584void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
585{
586 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
587 struct iwl_nonqos_seq_query_cmd query_cmd = {
588 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
589 .mac_id_n_color =
590 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
591 mvmvif->color)),
592 .value = cpu_to_le16(mvmvif->seqno),
593 };
594
595 /* return if called during restart, not resume from D3 */
596 if (!mvmvif->seqno_valid)
597 return;
598
599 mvmvif->seqno_valid = false;
600
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300601 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200602 sizeof(query_cmd), &query_cmd))
603 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
604}
605
Luciano Coelho671b5822014-09-22 10:59:53 +0300606static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
Eliad Peller1a95c8d2013-11-21 19:19:52 +0200607{
Luciano Coelhoc7d42482015-05-07 16:00:26 +0300608 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100609
Chaya Rachel Ivgifcb6b922016-02-22 10:21:41 +0200610 iwl_mvm_stop_device(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100611 /*
612 * Set the HW restart bit -- this is mostly true as we're
613 * going to load new firmware and reprogram that, though
614 * the reprogramming is going to be manual to avoid adding
615 * all the MACs that aren't support.
616 * We don't have to clear up everything though because the
617 * reprogramming is manual. When we resume, we'll actually
618 * go through a proper restart sequence again to switch
619 * back to the runtime firmware image.
620 */
621 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
622
Luca Coelho9c3deeb2015-11-11 01:06:17 +0200623 /* the fw is reset, so all the keys are cleared */
624 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
625
Johannes Bergf444eb12013-02-26 12:04:18 +0100626 mvm->ptk_ivlen = 0;
627 mvm->ptk_icvlen = 0;
628 mvm->ptk_ivlen = 0;
629 mvm->ptk_icvlen = 0;
630
Luciano Coelho671b5822014-09-22 10:59:53 +0300631 return iwl_mvm_load_d3_fw(mvm);
632}
633
634static int
Luciano Coelho671b5822014-09-22 10:59:53 +0300635iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
636 struct cfg80211_wowlan *wowlan,
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200637 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
Luciano Coelho671b5822014-09-22 10:59:53 +0300638 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
639 struct ieee80211_sta *ap_sta)
640{
641 int ret;
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100642 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
Luciano Coelho671b5822014-09-22 10:59:53 +0300643
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200644 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
Luciano Coelho671b5822014-09-22 10:59:53 +0300645
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200646 wowlan_config_cmd->is_11n_connection =
Luciano Coelho671b5822014-09-22 10:59:53 +0300647 ap_sta->ht_cap.ht_supported;
Sara Sharon70b4c532015-11-19 13:12:15 +0200648 wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
Matti Gottlieb32795a82016-02-25 10:18:34 +0200649 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
Luciano Coelho671b5822014-09-22 10:59:53 +0300650
651 /* Query the last used seqno and set it */
652 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
653 if (ret < 0)
654 return ret;
655
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200656 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
Luciano Coelho671b5822014-09-22 10:59:53 +0300657
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200658 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
Luciano Coelho671b5822014-09-22 10:59:53 +0300659
660 if (wowlan->disconnect)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200661 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300662 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
663 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
664 if (wowlan->magic_pkt)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200665 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300666 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
667 if (wowlan->gtk_rekey_failure)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200668 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300669 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
670 if (wowlan->eap_identity_req)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200671 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300672 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
673 if (wowlan->four_way_handshake)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200674 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300675 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
676 if (wowlan->n_patterns)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200677 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300678 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
679
680 if (wowlan->rfkill_release)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200681 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300682 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
683
684 if (wowlan->tcp) {
685 /*
686 * Set the "link change" (really "link lost") flag as well
687 * since that implies losing the TCP connection.
688 */
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200689 wowlan_config_cmd->wakeup_filter |=
Luciano Coelho671b5822014-09-22 10:59:53 +0300690 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
691 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
692 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
693 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
694 }
695
696 return 0;
697}
698
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200699static void
700iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
701 struct ieee80211_vif *vif,
702 void (*iter)(struct ieee80211_hw *hw,
703 struct ieee80211_vif *vif,
704 struct ieee80211_sta *sta,
705 struct ieee80211_key_conf *key,
706 void *data),
707 void *data)
708{
709 struct ieee80211_sta *ap_sta;
710
711 rcu_read_lock();
712
713 ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
714 if (IS_ERR_OR_NULL(ap_sta))
715 goto out;
716
717 ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
718out:
719 rcu_read_unlock();
720}
721
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200722int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
723 struct ieee80211_vif *vif,
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200724 bool d0i3,
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200725 u32 cmd_flags)
726{
727 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
728 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
729 struct wowlan_key_data key_data = {
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200730 .configure_keys = !d0i3,
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200731 .use_rsc_tsc = false,
732 .tkip = &tkip_cmd,
733 .use_tkip = false,
734 };
735 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
736 int ret;
737
738 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
739 if (!key_data.rsc_tsc)
740 return -ENOMEM;
741
742 /*
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200743 * if we have to configure keys, call ieee80211_iter_keys(),
744 * as we need non-atomic context in order to take the
745 * required locks.
746 * for the d0i3 we can't use ieee80211_iter_keys(), as
747 * taking (almost) any mutex might result in deadlock.
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200748 */
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200749 if (!d0i3) {
750 /*
751 * Note that currently we don't propagate cmd_flags
752 * to the iterator. In case of key_data.configure_keys,
753 * all the configured commands are SYNC, and
754 * iwl_mvm_wowlan_program_keys() will take care of
755 * locking/unlocking mvm->mutex.
756 */
757 ieee80211_iter_keys(mvm->hw, vif,
758 iwl_mvm_wowlan_program_keys,
759 &key_data);
760 } else {
761 iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
762 iwl_mvm_wowlan_program_keys,
763 &key_data);
764 }
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200765
766 if (key_data.error) {
767 ret = -EIO;
768 goto out;
769 }
770
771 if (key_data.use_rsc_tsc) {
772 ret = iwl_mvm_send_cmd_pdu(mvm,
773 WOWLAN_TSC_RSC_PARAM, cmd_flags,
774 sizeof(*key_data.rsc_tsc),
775 key_data.rsc_tsc);
776 if (ret)
777 goto out;
778 }
779
Sara Sharon45c458b2016-11-09 15:43:26 +0200780 if (key_data.use_tkip &&
781 !fw_has_api(&mvm->fw->ucode_capa,
782 IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200783 ret = iwl_mvm_send_cmd_pdu(mvm,
784 WOWLAN_TKIP_PARAM,
785 cmd_flags, sizeof(tkip_cmd),
786 &tkip_cmd);
787 if (ret)
788 goto out;
789 }
790
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200791 /* configure rekey data only if offloaded rekey is supported (d3) */
792 if (mvmvif->rekey_data.valid && !d0i3) {
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200793 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
794 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
795 NL80211_KCK_LEN);
796 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
797 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
798 NL80211_KEK_LEN);
799 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
800 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
801
802 ret = iwl_mvm_send_cmd_pdu(mvm,
803 WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
804 sizeof(kek_kck_cmd),
805 &kek_kck_cmd);
806 if (ret)
807 goto out;
808 }
Eliad Peller3f50a692015-11-15 15:44:17 +0200809 ret = 0;
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200810out:
811 kfree(key_data.rsc_tsc);
812 return ret;
813}
814
Luciano Coelho671b5822014-09-22 10:59:53 +0300815static int
816iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
817 struct cfg80211_wowlan *wowlan,
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200818 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
Luciano Coelho671b5822014-09-22 10:59:53 +0300819 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
820 struct ieee80211_sta *ap_sta)
821{
Luciano Coelho671b5822014-09-22 10:59:53 +0300822 int ret;
Matti Gottlieb23ae6122015-12-31 18:18:02 +0200823 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
824 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100825
Matti Gottlieb23ae6122015-12-31 18:18:02 +0200826 if (!unified_image) {
827 ret = iwl_mvm_switch_to_d3(mvm);
828 if (ret)
829 return ret;
Luciano Coelho3acc9522014-11-19 22:35:37 +0200830
Matti Gottlieb23ae6122015-12-31 18:18:02 +0200831 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
832 if (ret)
833 return ret;
834 }
Luciano Coelho671b5822014-09-22 10:59:53 +0300835
Johannes Berg3b37f4c2017-05-30 16:45:31 +0200836 if (!iwlwifi_mod_params.swcrypto) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100837 /*
838 * This needs to be unlocked due to lock ordering
839 * constraints. Since we're in the suspend path
840 * that isn't really a problem though.
841 */
842 mutex_unlock(&mvm->mutex);
Eliad Pellera3f7ba52015-11-11 17:23:59 +0200843 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
Eliad Peller3f50a692015-11-15 15:44:17 +0200844 CMD_ASYNC);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100845 mutex_lock(&mvm->mutex);
Eliad Peller3f50a692015-11-15 15:44:17 +0200846 if (ret)
847 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100848 }
849
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200850 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
851 sizeof(*wowlan_config_cmd),
852 wowlan_config_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100853 if (ret)
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200854 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100855
856 ret = iwl_mvm_send_patterns(mvm, wowlan);
857 if (ret)
Eliad Pellerac8ef0c2015-11-12 10:49:38 +0200858 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100859
Johannes Bergdebdc232017-10-26 22:57:57 +0200860 return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
Luciano Coelho671b5822014-09-22 10:59:53 +0300861}
862
Luciano Coelho3acc9522014-11-19 22:35:37 +0200863static int
864iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
865 struct cfg80211_wowlan *wowlan,
866 struct cfg80211_sched_scan_request *nd_config,
867 struct ieee80211_vif *vif)
868{
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200869 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
Luciano Coelho3acc9522014-11-19 22:35:37 +0200870 int ret;
Matti Gottlieb23ae6122015-12-31 18:18:02 +0200871 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
872 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
Luciano Coelho3acc9522014-11-19 22:35:37 +0200873
Matti Gottlieb23ae6122015-12-31 18:18:02 +0200874 if (!unified_image) {
875 ret = iwl_mvm_switch_to_d3(mvm);
876 if (ret)
877 return ret;
Luca Coelho5a143db2016-10-05 09:28:53 +0300878 } else {
879 /* In theory, we wouldn't have to stop a running sched
880 * scan in order to start another one (for
881 * net-detect). But in practice this doesn't seem to
882 * work properly, so stop any running sched_scan now.
883 */
884 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
885 if (ret)
886 return ret;
Matti Gottlieb23ae6122015-12-31 18:18:02 +0200887 }
Luciano Coelho3acc9522014-11-19 22:35:37 +0200888
889 /* rfkill release can be either for wowlan or netdetect */
890 if (wowlan->rfkill_release)
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200891 wowlan_config_cmd.wakeup_filter |=
Luciano Coelho3acc9522014-11-19 22:35:37 +0200892 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
893
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200894 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
895 sizeof(wowlan_config_cmd),
896 &wowlan_config_cmd);
Luciano Coelho3acc9522014-11-19 22:35:37 +0200897 if (ret)
898 return ret;
899
Luciano Coelho19945df2015-03-20 16:11:28 +0200900 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
901 IWL_MVM_SCAN_NETDETECT);
Luciano Coelhod9718da2014-11-21 09:32:23 +0200902 if (ret)
903 return ret;
Luciano Coelho3acc9522014-11-19 22:35:37 +0200904
Luciano Coelho8ed4e652014-11-21 22:08:01 +0200905 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
Luciano Coelhod9718da2014-11-21 09:32:23 +0200906 return -EBUSY;
907
Luciano Coelho8ed4e652014-11-21 22:08:01 +0200908 /* save the sched scan matchsets... */
Luciano Coelhod9718da2014-11-21 09:32:23 +0200909 if (nd_config->n_match_sets) {
910 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
911 sizeof(*nd_config->match_sets) *
912 nd_config->n_match_sets,
913 GFP_KERNEL);
914 if (mvm->nd_match_sets)
915 mvm->n_nd_match_sets = nd_config->n_match_sets;
916 }
917
Luciano Coelho8ed4e652014-11-21 22:08:01 +0200918 /* ...and the sched scan channels for later reporting */
919 mvm->nd_channels = kmemdup(nd_config->channels,
920 sizeof(*nd_config->channels) *
921 nd_config->n_channels,
922 GFP_KERNEL);
923 if (mvm->nd_channels)
924 mvm->n_nd_channels = nd_config->n_channels;
925
Luciano Coelhod9718da2014-11-21 09:32:23 +0200926 return 0;
927}
928
929static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
930{
931 kfree(mvm->nd_match_sets);
932 mvm->nd_match_sets = NULL;
933 mvm->n_nd_match_sets = 0;
Luciano Coelho8ed4e652014-11-21 22:08:01 +0200934 kfree(mvm->nd_channels);
935 mvm->nd_channels = NULL;
936 mvm->n_nd_channels = 0;
Luciano Coelho3acc9522014-11-19 22:35:37 +0200937}
938
Luciano Coelho671b5822014-09-22 10:59:53 +0300939static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
940 struct cfg80211_wowlan *wowlan,
941 bool test)
942{
943 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Luciano Coelho671b5822014-09-22 10:59:53 +0300944 struct ieee80211_vif *vif = NULL;
945 struct iwl_mvm_vif *mvmvif = NULL;
946 struct ieee80211_sta *ap_sta = NULL;
Luciano Coelho671b5822014-09-22 10:59:53 +0300947 struct iwl_d3_manager_config d3_cfg_cmd_data = {
948 /*
949 * Program the minimum sleep time to 10 seconds, as many
950 * platforms have issues processing a wakeup signal while
951 * still being in the process of suspending.
952 */
953 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
954 };
955 struct iwl_host_cmd d3_cfg_cmd = {
956 .id = D3_CONFIG_CMD,
957 .flags = CMD_WANT_SKB,
958 .data[0] = &d3_cfg_cmd_data,
959 .len[0] = sizeof(d3_cfg_cmd_data),
960 };
961 int ret;
962 int len __maybe_unused;
Matti Gottlieb23ae6122015-12-31 18:18:02 +0200963 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
964 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
Luciano Coelho671b5822014-09-22 10:59:53 +0300965
966 if (!wowlan) {
967 /*
968 * mac80211 shouldn't get here, but for D3 test
969 * it doesn't warrant a warning
970 */
971 WARN_ON(!test);
972 return -EINVAL;
973 }
974
975 mutex_lock(&mvm->mutex);
976
Luciano Coelho7f549e22014-10-02 15:38:04 +0300977 vif = iwl_mvm_get_bss_vif(mvm);
978 if (IS_ERR_OR_NULL(vif)) {
Luciano Coelho671b5822014-09-22 10:59:53 +0300979 ret = 1;
980 goto out_noreset;
981 }
982
Luciano Coelho671b5822014-09-22 10:59:53 +0300983 mvmvif = iwl_mvm_vif_from_mac80211(vif);
984
Sara Sharon0ae98812017-01-04 14:53:58 +0200985 if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA) {
Luciano Coelho3acc9522014-11-19 22:35:37 +0200986 /* if we're not associated, this must be netdetect */
Luca Coelho566f1652015-10-25 22:55:32 +0200987 if (!wowlan->nd_config) {
Luciano Coelho3acc9522014-11-19 22:35:37 +0200988 ret = 1;
989 goto out_noreset;
990 }
991
992 ret = iwl_mvm_netdetect_config(
Luca Coelho566f1652015-10-25 22:55:32 +0200993 mvm, wowlan, wowlan->nd_config, vif);
Luciano Coelho3acc9522014-11-19 22:35:37 +0200994 if (ret)
995 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +0200996
997 mvm->net_detect = true;
Luciano Coelho3acc9522014-11-19 22:35:37 +0200998 } else {
Emmanuel Grumbachc8b06a92014-11-24 09:06:57 +0200999 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
Luciano Coelho3acc9522014-11-19 22:35:37 +02001000
Luciano Coelho1e4d19c2014-09-22 13:01:59 +03001001 ap_sta = rcu_dereference_protected(
1002 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1003 lockdep_is_held(&mvm->mutex));
1004 if (IS_ERR_OR_NULL(ap_sta)) {
1005 ret = -EINVAL;
1006 goto out_noreset;
1007 }
1008
1009 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1010 vif, mvmvif, ap_sta);
1011 if (ret)
1012 goto out_noreset;
Luciano Coelho1e4d19c2014-09-22 13:01:59 +03001013 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1014 vif, mvmvif, ap_sta);
1015 if (ret)
1016 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001017
1018 mvm->net_detect = false;
Luciano Coelho671b5822014-09-22 10:59:53 +03001019 }
1020
Emmanuel Grumbachc1cb92f2014-01-28 10:17:18 +02001021 ret = iwl_mvm_power_update_device(mvm);
Alexander Bondar64b928c2013-09-03 14:18:03 +03001022 if (ret)
1023 goto out;
1024
Arik Nemtsov999609f2014-05-15 17:31:51 +03001025 ret = iwl_mvm_power_update_mac(mvm);
Alexander Bondaree1e8422013-04-23 13:52:10 +03001026 if (ret)
1027 goto out;
1028
Johannes Bergb0114712013-06-12 14:55:40 +02001029#ifdef CONFIG_IWLWIFI_DEBUGFS
1030 if (mvm->d3_wake_sysassert)
1031 d3_cfg_cmd_data.wakeup_flags |=
1032 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1033#endif
1034
Johannes Berg8ca151b2013-01-24 14:25:36 +01001035 /* must be last -- this switches firmware state */
Johannes Bergdebff612013-05-14 13:53:45 +02001036 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001037 if (ret)
1038 goto out;
Johannes Bergdebff612013-05-14 13:53:45 +02001039#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg65b30342014-01-08 13:16:33 +01001040 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1041 if (len >= sizeof(u32)) {
Johannes Bergdebff612013-05-14 13:53:45 +02001042 mvm->d3_test_pme_ptr =
1043 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
Johannes Bergdebff612013-05-14 13:53:45 +02001044 }
1045#endif
1046 iwl_free_resp(&d3_cfg_cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001047
1048 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1049
Matti Gottlieb23ae6122015-12-31 18:18:02 +02001050 iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001051 out:
Luciano Coelhod9718da2014-11-21 09:32:23 +02001052 if (ret < 0) {
Luciano Coelhod9718da2014-11-21 09:32:23 +02001053 iwl_mvm_free_nd(mvm);
Luca Coelhobac453a2016-10-07 15:16:26 +03001054
1055 if (!unified_image) {
1056 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
Johannes Berg3b37f4c2017-05-30 16:45:31 +02001057 if (mvm->fw_restart > 0) {
1058 mvm->fw_restart--;
Luca Coelhobac453a2016-10-07 15:16:26 +03001059 ieee80211_restart_hw(mvm->hw);
1060 }
1061 }
Luciano Coelhod9718da2014-11-21 09:32:23 +02001062 }
Luciano Coelhob70ab722013-11-19 22:53:41 +02001063 out_noreset:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001064 mutex_unlock(&mvm->mutex);
1065
1066 return ret;
1067}
1068
Eliad Peller67359432014-12-09 15:23:54 +02001069static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
1070{
1071 struct iwl_notification_wait wait_d3;
Sara Sharon6eb031d2015-07-13 14:50:47 +03001072 static const u16 d3_notif[] = { D3_CONFIG_CMD };
Eliad Peller67359432014-12-09 15:23:54 +02001073 int ret;
1074
1075 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
1076 d3_notif, ARRAY_SIZE(d3_notif),
1077 NULL, NULL);
1078
1079 ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
1080 if (ret)
1081 goto remove_notif;
1082
1083 ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
1084 WARN_ON_ONCE(ret);
1085 return ret;
1086
1087remove_notif:
1088 iwl_remove_notification(&mvm->notif_wait, &wait_d3);
1089 return ret;
1090}
1091
Johannes Bergdebff612013-05-14 13:53:45 +02001092int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1093{
Eliad Pellerd15a7472014-03-27 18:53:12 +02001094 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Luca Coelhob7282642015-09-17 23:44:14 +03001095 struct iwl_trans *trans = mvm->trans;
Eliad Pellerc43fe902015-06-04 21:59:32 +03001096 int ret;
Eliad Pellerd15a7472014-03-27 18:53:12 +02001097
Luciano Coelhocf8c3cf2015-08-24 14:53:25 +03001098 /* make sure the d0i3 exit work is not pending */
1099 flush_work(&mvm->d0i3_exit_work);
1100
Luca Coelhob7282642015-09-17 23:44:14 +03001101 ret = iwl_trans_suspend(trans);
Eliad Pellerc43fe902015-06-04 21:59:32 +03001102 if (ret)
1103 return ret;
1104
Luca Coelhob7282642015-09-17 23:44:14 +03001105 if (wowlan->any) {
1106 trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
1107
1108 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
Eliad Pellerc43fe902015-06-04 21:59:32 +03001109 ret = iwl_mvm_enter_d0i3_sync(mvm);
Eliad Peller67359432014-12-09 15:23:54 +02001110
1111 if (ret)
1112 return ret;
1113 }
1114
Eliad Pellerd15a7472014-03-27 18:53:12 +02001115 mutex_lock(&mvm->d0i3_suspend_mutex);
1116 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1117 mutex_unlock(&mvm->d0i3_suspend_mutex);
Eliad Peller6dfb36c2015-07-09 14:17:24 +03001118
Matti Gottlieb23ae6122015-12-31 18:18:02 +02001119 iwl_trans_d3_suspend(trans, false, false);
Eliad Peller6dfb36c2015-07-09 14:17:24 +03001120
Eliad Pellerd15a7472014-03-27 18:53:12 +02001121 return 0;
1122 }
1123
Luca Coelhob7282642015-09-17 23:44:14 +03001124 trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1125
Johannes Bergdebff612013-05-14 13:53:45 +02001126 return __iwl_mvm_suspend(hw, wowlan, false);
1127}
1128
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001129/* converted data from the different status responses */
1130struct iwl_wowlan_status_data {
1131 u16 pattern_number;
1132 u16 qos_seq_ctr[8];
1133 u32 wakeup_reasons;
1134 u32 wake_packet_length;
1135 u32 wake_packet_bufsize;
1136 const u8 *wake_packet;
1137};
1138
Johannes Berg7abc4632013-08-06 18:28:26 +02001139static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1140 struct ieee80211_vif *vif,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001141 struct iwl_wowlan_status_data *status)
Johannes Berg9b26b502013-01-22 13:02:09 +01001142{
Johannes Berg7abc4632013-08-06 18:28:26 +02001143 struct sk_buff *pkt = NULL;
Johannes Berg9b26b502013-01-22 13:02:09 +01001144 struct cfg80211_wowlan_wakeup wakeup = {
1145 .pattern_idx = -1,
1146 };
1147 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001148 u32 reasons = status->wakeup_reasons;
Johannes Berg9b26b502013-01-22 13:02:09 +01001149
1150 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1151 wakeup_report = NULL;
1152 goto report;
1153 }
1154
Derek Basehore06ae2ad2015-11-04 17:37:52 -08001155 pm_wakeup_event(mvm->dev, 0);
1156
Johannes Bergf444eb12013-02-26 12:04:18 +01001157 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
Johannes Berg9b26b502013-01-22 13:02:09 +01001158 wakeup.magic_pkt = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001159
Johannes Bergf444eb12013-02-26 12:04:18 +01001160 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
Johannes Berg9b26b502013-01-22 13:02:09 +01001161 wakeup.pattern_idx =
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001162 status->pattern_number;
Johannes Berg9b26b502013-01-22 13:02:09 +01001163
1164 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1165 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1166 wakeup.disconnect = true;
1167
Johannes Bergf444eb12013-02-26 12:04:18 +01001168 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001169 wakeup.gtk_rekey_failure = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001170
Johannes Bergf444eb12013-02-26 12:04:18 +01001171 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
Johannes Berg9b26b502013-01-22 13:02:09 +01001172 wakeup.rfkill_release = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001173
Johannes Bergf444eb12013-02-26 12:04:18 +01001174 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
Johannes Berg9b26b502013-01-22 13:02:09 +01001175 wakeup.eap_identity_req = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001176
Johannes Bergf444eb12013-02-26 12:04:18 +01001177 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
Johannes Berg9b26b502013-01-22 13:02:09 +01001178 wakeup.four_way_handshake = true;
Johannes Berg9b26b502013-01-22 13:02:09 +01001179
Johannes Bergf0c26462013-01-22 20:41:58 +01001180 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1181 wakeup.tcp_connlost = true;
1182
1183 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1184 wakeup.tcp_nomoretokens = true;
1185
1186 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1187 wakeup.tcp_match = true;
1188
Johannes Berg9b26b502013-01-22 13:02:09 +01001189 if (status->wake_packet_bufsize) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001190 int pktsize = status->wake_packet_bufsize;
1191 int pktlen = status->wake_packet_length;
Johannes Bergf444eb12013-02-26 12:04:18 +01001192 const u8 *pktdata = status->wake_packet;
1193 struct ieee80211_hdr *hdr = (void *)pktdata;
1194 int truncated = pktlen - pktsize;
Johannes Berg9b26b502013-01-22 13:02:09 +01001195
Johannes Bergf444eb12013-02-26 12:04:18 +01001196 /* this would be a firmware bug */
1197 if (WARN_ON_ONCE(truncated < 0))
1198 truncated = 0;
1199
1200 if (ieee80211_is_data(hdr->frame_control)) {
1201 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1202 int ivlen = 0, icvlen = 4; /* also FCS */
1203
Johannes Berg9b26b502013-01-22 13:02:09 +01001204 pkt = alloc_skb(pktsize, GFP_KERNEL);
1205 if (!pkt)
1206 goto report;
Johannes Bergf444eb12013-02-26 12:04:18 +01001207
Johannes Berg59ae1d12017-06-16 14:29:20 +02001208 skb_put_data(pkt, pktdata, hdrlen);
Johannes Bergf444eb12013-02-26 12:04:18 +01001209 pktdata += hdrlen;
1210 pktsize -= hdrlen;
1211
1212 if (ieee80211_has_protected(hdr->frame_control)) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001213 /*
1214 * This is unlocked and using gtk_i(c)vlen,
1215 * but since everything is under RTNL still
1216 * that's not really a problem - changing
1217 * it would be difficult.
1218 */
Johannes Bergf444eb12013-02-26 12:04:18 +01001219 if (is_multicast_ether_addr(hdr->addr1)) {
1220 ivlen = mvm->gtk_ivlen;
1221 icvlen += mvm->gtk_icvlen;
1222 } else {
1223 ivlen = mvm->ptk_ivlen;
1224 icvlen += mvm->ptk_icvlen;
1225 }
1226 }
1227
1228 /* if truncated, FCS/ICV is (partially) gone */
1229 if (truncated >= icvlen) {
1230 icvlen = 0;
1231 truncated -= icvlen;
1232 } else {
1233 icvlen -= truncated;
1234 truncated = 0;
1235 }
1236
1237 pktsize -= ivlen + icvlen;
1238 pktdata += ivlen;
1239
Johannes Berg59ae1d12017-06-16 14:29:20 +02001240 skb_put_data(pkt, pktdata, pktsize);
Johannes Bergf444eb12013-02-26 12:04:18 +01001241
Johannes Berg9b26b502013-01-22 13:02:09 +01001242 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1243 goto report;
1244 wakeup.packet = pkt->data;
1245 wakeup.packet_present_len = pkt->len;
Johannes Bergf444eb12013-02-26 12:04:18 +01001246 wakeup.packet_len = pkt->len - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001247 wakeup.packet_80211 = false;
1248 } else {
Johannes Bergf444eb12013-02-26 12:04:18 +01001249 int fcslen = 4;
1250
1251 if (truncated >= 4) {
1252 truncated -= 4;
1253 fcslen = 0;
1254 } else {
1255 fcslen -= truncated;
1256 truncated = 0;
1257 }
1258 pktsize -= fcslen;
Johannes Berg9b26b502013-01-22 13:02:09 +01001259 wakeup.packet = status->wake_packet;
1260 wakeup.packet_present_len = pktsize;
Johannes Bergf444eb12013-02-26 12:04:18 +01001261 wakeup.packet_len = pktlen - truncated;
Johannes Berg9b26b502013-01-22 13:02:09 +01001262 wakeup.packet_80211 = true;
1263 }
1264 }
1265
1266 report:
1267 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1268 kfree_skb(pkt);
Johannes Berg7abc4632013-08-06 18:28:26 +02001269}
Johannes Berg9b26b502013-01-22 13:02:09 +01001270
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001271static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1272 struct ieee80211_key_seq *seq)
1273{
1274 u64 pn;
1275
1276 pn = le64_to_cpu(sc->pn);
1277 seq->ccmp.pn[0] = pn >> 40;
1278 seq->ccmp.pn[1] = pn >> 32;
1279 seq->ccmp.pn[2] = pn >> 24;
1280 seq->ccmp.pn[3] = pn >> 16;
1281 seq->ccmp.pn[4] = pn >> 8;
1282 seq->ccmp.pn[5] = pn;
1283}
1284
1285static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1286 struct ieee80211_key_seq *seq)
1287{
1288 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1289 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1290}
1291
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001292static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
1293 struct ieee80211_sta *sta,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001294 struct ieee80211_key_conf *key)
1295{
1296 int tid;
1297
1298 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1299
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001300 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
1301 struct iwl_mvm_sta *mvmsta;
1302 struct iwl_mvm_key_pn *ptk_pn;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001303
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001304 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1305
1306 ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
1307 lockdep_is_held(&mvm->mutex));
1308 if (WARN_ON(!ptk_pn))
1309 return;
1310
1311 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1312 struct ieee80211_key_seq seq = {};
1313 int i;
1314
1315 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1316 ieee80211_set_key_rx_seq(key, tid, &seq);
1317 for (i = 1; i < mvm->trans->num_rx_queues; i++)
1318 memcpy(ptk_pn->q[i].pn[tid],
1319 seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
1320 }
1321 } else {
1322 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1323 struct ieee80211_key_seq seq = {};
1324
1325 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1326 ieee80211_set_key_rx_seq(key, tid, &seq);
1327 }
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001328 }
1329}
1330
1331static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1332 struct ieee80211_key_conf *key)
1333{
1334 int tid;
1335
1336 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1337
1338 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1339 struct ieee80211_key_seq seq = {};
1340
1341 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1342 ieee80211_set_key_rx_seq(key, tid, &seq);
1343 }
1344}
1345
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001346static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
1347 struct ieee80211_key_conf *key,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001348 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001349{
1350 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1351
1352 switch (key->cipher) {
1353 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001354 iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001355 break;
1356 case WLAN_CIPHER_SUITE_TKIP:
1357 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1358 break;
1359 default:
1360 WARN_ON(1);
1361 }
1362}
1363
1364struct iwl_mvm_d3_gtk_iter_data {
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001365 struct iwl_mvm *mvm;
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001366 struct iwl_wowlan_status *status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001367 void *last_gtk;
1368 u32 cipher;
1369 bool find_phase, unhandled_cipher;
1370 int num_keys;
1371};
1372
Emmanuel Grumbach59fd4bf2015-11-01 13:06:37 +02001373static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001374 struct ieee80211_vif *vif,
1375 struct ieee80211_sta *sta,
1376 struct ieee80211_key_conf *key,
1377 void *_data)
1378{
1379 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1380
1381 if (data->unhandled_cipher)
1382 return;
1383
1384 switch (key->cipher) {
1385 case WLAN_CIPHER_SUITE_WEP40:
1386 case WLAN_CIPHER_SUITE_WEP104:
1387 /* ignore WEP completely, nothing to do */
1388 return;
1389 case WLAN_CIPHER_SUITE_CCMP:
1390 case WLAN_CIPHER_SUITE_TKIP:
1391 /* we support these */
1392 break;
1393 default:
1394 /* everything else (even CMAC for MFP) - disconnect from AP */
1395 data->unhandled_cipher = true;
1396 return;
1397 }
1398
1399 data->num_keys++;
1400
1401 /*
1402 * pairwise key - update sequence counters only;
1403 * note that this assumes no TDLS sessions are active
1404 */
1405 if (sta) {
1406 struct ieee80211_key_seq seq = {};
1407 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1408
1409 if (data->find_phase)
1410 return;
1411
1412 switch (key->cipher) {
1413 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001414 iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
1415 sta, key);
Johannes Berg6645d5e2015-09-15 09:58:53 +02001416 atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001417 break;
1418 case WLAN_CIPHER_SUITE_TKIP:
1419 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1420 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
Eliad Peller1ad4f632016-02-14 13:56:36 +02001421 atomic64_set(&key->tx_pn,
1422 (u64)seq.tkip.iv16 |
1423 ((u64)seq.tkip.iv32 << 16));
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001424 break;
1425 }
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001426
1427 /* that's it for this key */
1428 return;
1429 }
1430
1431 if (data->find_phase) {
1432 data->last_gtk = key;
1433 data->cipher = key->cipher;
1434 return;
1435 }
1436
1437 if (data->status->num_of_gtk_rekeys)
1438 ieee80211_remove_key(key);
1439 else if (data->last_gtk == key)
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001440 iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001441}
1442
1443static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1444 struct ieee80211_vif *vif,
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001445 struct iwl_wowlan_status *status)
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001446{
1447 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1448 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001449 .mvm = mvm,
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001450 .status = status,
1451 };
Eliad Peller431591c2013-11-07 14:12:09 +02001452 u32 disconnection_reasons =
1453 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1454 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001455
1456 if (!status || !vif->bss_conf.bssid)
1457 return false;
1458
Eliad Peller431591c2013-11-07 14:12:09 +02001459 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1460 return false;
1461
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001462 /* find last GTK that we used initially, if any */
1463 gtkdata.find_phase = true;
1464 ieee80211_iter_keys(mvm->hw, vif,
Emmanuel Grumbach59fd4bf2015-11-01 13:06:37 +02001465 iwl_mvm_d3_update_keys, &gtkdata);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001466 /* not trying to keep connections with MFP/unhandled ciphers */
1467 if (gtkdata.unhandled_cipher)
1468 return false;
1469 if (!gtkdata.num_keys)
Luciano Coelhocfa88892013-11-19 15:38:55 +02001470 goto out;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001471 if (!gtkdata.last_gtk)
1472 return false;
1473
1474 /*
1475 * invalidate all other GTKs that might still exist and update
1476 * the one that we used
1477 */
1478 gtkdata.find_phase = false;
1479 ieee80211_iter_keys(mvm->hw, vif,
Emmanuel Grumbach59fd4bf2015-11-01 13:06:37 +02001480 iwl_mvm_d3_update_keys, &gtkdata);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001481
1482 if (status->num_of_gtk_rekeys) {
1483 struct ieee80211_key_conf *key;
1484 struct {
1485 struct ieee80211_key_conf conf;
1486 u8 key[32];
1487 } conf = {
1488 .conf.cipher = gtkdata.cipher,
1489 .conf.keyidx = status->gtk.key_index,
1490 };
1491
1492 switch (gtkdata.cipher) {
1493 case WLAN_CIPHER_SUITE_CCMP:
1494 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1495 memcpy(conf.conf.key, status->gtk.decrypt_key,
1496 WLAN_KEY_LEN_CCMP);
1497 break;
1498 case WLAN_CIPHER_SUITE_TKIP:
1499 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1500 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1501 /* leave TX MIC key zeroed, we don't use it anyway */
1502 memcpy(conf.conf.key +
1503 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1504 status->gtk.tkip_mic_key, 8);
1505 break;
1506 }
1507
1508 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1509 if (IS_ERR(key))
1510 return false;
Johannes Bergf5e28ea2015-12-06 14:58:08 +02001511 iwl_mvm_set_key_rx_seq(mvm, key, status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001512 }
1513
1514 if (status->num_of_gtk_rekeys) {
1515 __be64 replay_ctr =
1516 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1517 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1518 (void *)&replay_ctr, GFP_KERNEL);
1519 }
1520
Luciano Coelhocfa88892013-11-19 15:38:55 +02001521out:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001522 mvmvif->seqno_valid = true;
1523 /* +0x10 because the set API expects next-to-use, not last-used */
1524 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1525
1526 return true;
1527}
1528
Luciano Coelhoec12f452014-11-20 10:14:44 +02001529static struct iwl_wowlan_status *
1530iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg7abc4632013-08-06 18:28:26 +02001531{
Sara Sharon5c228d62016-11-24 13:48:27 +02001532 u32 base = mvm->error_event_table[0];
Johannes Berg7abc4632013-08-06 18:28:26 +02001533 struct error_table_start {
1534 /* cf. struct iwl_error_event_table */
1535 u32 valid;
1536 u32 error_id;
1537 } err_info;
1538 struct iwl_host_cmd cmd = {
1539 .id = WOWLAN_GET_STATUSES,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001540 .flags = CMD_WANT_SKB,
Johannes Berg7abc4632013-08-06 18:28:26 +02001541 };
Luciano Coelhoec12f452014-11-20 10:14:44 +02001542 struct iwl_wowlan_status *status, *fw_status;
1543 int ret, len, status_size;
Johannes Berg7abc4632013-08-06 18:28:26 +02001544
1545 iwl_trans_read_mem_bytes(mvm->trans, base,
1546 &err_info, sizeof(err_info));
1547
1548 if (err_info.valid) {
Luciano Coelhoec12f452014-11-20 10:14:44 +02001549 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1550 err_info.valid, err_info.error_id);
Johannes Berg7abc4632013-08-06 18:28:26 +02001551 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1552 struct cfg80211_wowlan_wakeup wakeup = {
1553 .rfkill_release = true,
1554 };
1555 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1556 GFP_KERNEL);
1557 }
Luciano Coelhoec12f452014-11-20 10:14:44 +02001558 return ERR_PTR(-EIO);
Johannes Berg7abc4632013-08-06 18:28:26 +02001559 }
1560
1561 /* only for tracing for now */
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001562 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
Johannes Berg7abc4632013-08-06 18:28:26 +02001563 if (ret)
1564 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1565
1566 ret = iwl_mvm_send_cmd(mvm, &cmd);
1567 if (ret) {
1568 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
Luciano Coelhoec12f452014-11-20 10:14:44 +02001569 return ERR_PTR(ret);
Johannes Berg7abc4632013-08-06 18:28:26 +02001570 }
1571
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001572 status_size = sizeof(*fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001573
Johannes Berg65b30342014-01-08 13:16:33 +01001574 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1575 if (len < status_size) {
Johannes Berg7abc4632013-08-06 18:28:26 +02001576 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
Johannes Berge111e962015-03-03 20:23:47 +01001577 fw_status = ERR_PTR(-EIO);
Johannes Berg7abc4632013-08-06 18:28:26 +02001578 goto out_free_resp;
1579 }
1580
Luciano Coelhoec12f452014-11-20 10:14:44 +02001581 status = (void *)cmd.resp_pkt->data;
1582 if (len != (status_size +
1583 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1584 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
Johannes Berge111e962015-03-03 20:23:47 +01001585 fw_status = ERR_PTR(-EIO);
Luciano Coelhoec12f452014-11-20 10:14:44 +02001586 goto out_free_resp;
1587 }
1588
1589 fw_status = kmemdup(status, len, GFP_KERNEL);
1590
1591out_free_resp:
1592 iwl_free_resp(&cmd);
Johannes Berge111e962015-03-03 20:23:47 +01001593 return fw_status;
Luciano Coelhoec12f452014-11-20 10:14:44 +02001594}
1595
1596/* releases the MVM mutex */
1597static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1598 struct ieee80211_vif *vif)
1599{
1600 struct iwl_wowlan_status_data status;
1601 struct iwl_wowlan_status *fw_status;
1602 int i;
1603 bool keep;
Luciano Coelhoec12f452014-11-20 10:14:44 +02001604 struct iwl_mvm_sta *mvm_ap_sta;
1605
1606 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1607 if (IS_ERR_OR_NULL(fw_status))
1608 goto out_unlock;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001609
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001610 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1611 for (i = 0; i < 8; i++)
1612 status.qos_seq_ctr[i] =
1613 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1614 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1615 status.wake_packet_length =
1616 le32_to_cpu(fw_status->wake_packet_length);
1617 status.wake_packet_bufsize =
1618 le32_to_cpu(fw_status->wake_packet_bufsize);
1619 status.wake_packet = fw_status->wake_packet;
Johannes Berg7abc4632013-08-06 18:28:26 +02001620
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001621 /* still at hard-coded place 0 for D3 image */
Sara Sharon13303c02016-04-10 15:51:54 +03001622 mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
1623 if (!mvm_ap_sta)
Luciano Coelhoec12f452014-11-20 10:14:44 +02001624 goto out_free;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001625
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001626 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1627 u16 seq = status.qos_seq_ctr[i];
1628 /* firmware stores last-used value, we store next value */
1629 seq += 0x10;
1630 mvm_ap_sta->tid_data[i].seq_number = seq;
1631 }
1632
Johannes Berg7abc4632013-08-06 18:28:26 +02001633 /* now we have all the data we need, unlock to avoid mac80211 issues */
1634 mutex_unlock(&mvm->mutex);
1635
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001636 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1637
Emmanuel Grumbach3afec6392014-03-30 09:10:28 +03001638 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001639
Luciano Coelhoec12f452014-11-20 10:14:44 +02001640 kfree(fw_status);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001641 return keep;
Johannes Berg7abc4632013-08-06 18:28:26 +02001642
Luciano Coelhoec12f452014-11-20 10:14:44 +02001643out_free:
1644 kfree(fw_status);
1645out_unlock:
Johannes Berg7abc4632013-08-06 18:28:26 +02001646 mutex_unlock(&mvm->mutex);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001647 return false;
Johannes Berg9b26b502013-01-22 13:02:09 +01001648}
1649
Eliad Pellera3f7ba52015-11-11 17:23:59 +02001650void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
1651 struct ieee80211_vif *vif,
1652 struct iwl_wowlan_status *status)
1653{
1654 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
Eliad Peller0eb1c962015-12-31 15:19:32 +02001655 .mvm = mvm,
Eliad Pellera3f7ba52015-11-11 17:23:59 +02001656 .status = status,
1657 };
1658
1659 /*
1660 * rekey handling requires taking locks that can't be taken now.
1661 * however, d0i3 doesn't offload rekey, so we're fine.
1662 */
1663 if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
1664 return;
1665
1666 /* find last GTK that we used initially, if any */
1667 gtkdata.find_phase = true;
1668 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
1669
1670 gtkdata.find_phase = false;
1671 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
1672}
1673
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001674struct iwl_mvm_nd_query_results {
1675 u32 matched_profiles;
1676 struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1677};
1678
1679static int
1680iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1681 struct iwl_mvm_nd_query_results *results)
Luciano Coelhob04998f2014-11-20 15:58:34 +02001682{
1683 struct iwl_scan_offload_profiles_query *query;
1684 struct iwl_host_cmd cmd = {
1685 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1686 .flags = CMD_WANT_SKB,
1687 };
1688 int ret, len;
1689
1690 ret = iwl_mvm_send_cmd(mvm, &cmd);
1691 if (ret) {
1692 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001693 return ret;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001694 }
1695
Luciano Coelhob04998f2014-11-20 15:58:34 +02001696 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1697 if (len < sizeof(*query)) {
1698 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001699 ret = -EIO;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001700 goto out_free_resp;
1701 }
1702
1703 query = (void *)cmd.resp_pkt->data;
1704
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001705 results->matched_profiles = le32_to_cpu(query->matched_profiles);
1706 memcpy(results->matches, query->matches, sizeof(results->matches));
Luciano Coelhob04998f2014-11-20 15:58:34 +02001707
Emmanuel Grumbach6bbd5522015-04-28 15:00:23 +03001708#ifdef CONFIG_IWLWIFI_DEBUGFS
Luciano Coelho484b3d12015-03-30 20:46:32 +03001709 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1710#endif
1711
Luciano Coelhob04998f2014-11-20 15:58:34 +02001712out_free_resp:
1713 iwl_free_resp(&cmd);
1714 return ret;
1715}
1716
Luciano Coelho2021a892014-11-20 08:59:51 +02001717static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1718 struct ieee80211_vif *vif)
1719{
Luciano Coelhod9718da2014-11-21 09:32:23 +02001720 struct cfg80211_wowlan_nd_info *net_detect = NULL;
Luciano Coelho2021a892014-11-20 08:59:51 +02001721 struct cfg80211_wowlan_wakeup wakeup = {
1722 .pattern_idx = -1,
1723 };
1724 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001725 struct iwl_mvm_nd_query_results query;
Luciano Coelho2021a892014-11-20 08:59:51 +02001726 struct iwl_wowlan_status *fw_status;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001727 unsigned long matched_profiles;
Luciano Coelho2021a892014-11-20 08:59:51 +02001728 u32 reasons = 0;
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001729 int i, j, n_matches, ret;
Luciano Coelho2021a892014-11-20 08:59:51 +02001730
1731 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
Haim Dreyfuss2fc863a2015-05-20 08:10:43 +03001732 if (!IS_ERR_OR_NULL(fw_status)) {
Luciano Coelho2021a892014-11-20 08:59:51 +02001733 reasons = le32_to_cpu(fw_status->wakeup_reasons);
Haim Dreyfuss2fc863a2015-05-20 08:10:43 +03001734 kfree(fw_status);
1735 }
Luciano Coelho2021a892014-11-20 08:59:51 +02001736
1737 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1738 wakeup.rfkill_release = true;
1739
Luciano Coelhob04998f2014-11-20 15:58:34 +02001740 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1741 goto out;
1742
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001743 ret = iwl_mvm_netdetect_query_results(mvm, &query);
1744 if (ret || !query.matched_profiles) {
Luciano Coelho2021a892014-11-20 08:59:51 +02001745 wakeup_report = NULL;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001746 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001747 }
1748
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001749 matched_profiles = query.matched_profiles;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001750 if (mvm->n_nd_match_sets) {
1751 n_matches = hweight_long(matched_profiles);
1752 } else {
1753 IWL_ERR(mvm, "no net detect match information available\n");
1754 n_matches = 0;
1755 }
1756
1757 net_detect = kzalloc(sizeof(*net_detect) +
1758 (n_matches * sizeof(net_detect->matches[0])),
1759 GFP_KERNEL);
1760 if (!net_detect || !n_matches)
1761 goto out_report_nd;
1762
1763 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001764 struct iwl_scan_offload_profile_match *fw_match;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001765 struct cfg80211_wowlan_nd_match *match;
Luciano Coelhoae90c2e52015-05-06 10:45:42 +03001766 int idx, n_channels = 0;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001767
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001768 fw_match = &query.matches[i];
1769
1770 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1771 n_channels += hweight8(fw_match->matching_channels[j]);
1772
1773 match = kzalloc(sizeof(*match) +
1774 (n_channels * sizeof(*match->channels)),
1775 GFP_KERNEL);
Luciano Coelhod9718da2014-11-21 09:32:23 +02001776 if (!match)
1777 goto out_report_nd;
1778
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001779 net_detect->matches[net_detect->n_matches++] = match;
1780
Luciano Coelhoae90c2e52015-05-06 10:45:42 +03001781 /* We inverted the order of the SSIDs in the scan
1782 * request, so invert the index here.
1783 */
1784 idx = mvm->n_nd_match_sets - i - 1;
1785 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
1786 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
Luciano Coelhod9718da2014-11-21 09:32:23 +02001787 match->ssid.ssid_len);
1788
Luciano Coelho8ed4e652014-11-21 22:08:01 +02001789 if (mvm->n_nd_channels < n_channels)
1790 continue;
1791
1792 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
1793 if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
1794 match->channels[match->n_channels++] =
1795 mvm->nd_channels[j]->center_freq;
Luciano Coelhod9718da2014-11-21 09:32:23 +02001796 }
1797
1798out_report_nd:
1799 wakeup.net_detect = net_detect;
Luciano Coelhob04998f2014-11-20 15:58:34 +02001800out:
Luciano Coelhod9718da2014-11-21 09:32:23 +02001801 iwl_mvm_free_nd(mvm);
1802
Luciano Coelho2021a892014-11-20 08:59:51 +02001803 mutex_unlock(&mvm->mutex);
1804 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
Luciano Coelhod9718da2014-11-21 09:32:23 +02001805
1806 if (net_detect) {
1807 for (i = 0; i < net_detect->n_matches; i++)
1808 kfree(net_detect->matches[i]);
1809 kfree(net_detect);
1810 }
Luciano Coelho2021a892014-11-20 08:59:51 +02001811}
1812
Johannes Bergafc66bb2013-05-03 11:44:16 +02001813static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1814{
1815#ifdef CONFIG_IWLWIFI_DEBUGFS
1816 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1817 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1818 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1819
1820 if (!mvm->store_d3_resume_sram)
1821 return;
1822
1823 if (!mvm->d3_resume_sram) {
1824 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1825 if (!mvm->d3_resume_sram)
1826 return;
1827 }
1828
1829 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1830#endif
1831}
1832
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001833static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1834 struct ieee80211_vif *vif)
1835{
1836 /* skip the one we keep connection on */
1837 if (data == vif)
1838 return;
1839
1840 if (vif->type == NL80211_IFTYPE_STATION)
1841 ieee80211_resume_disconnect(vif);
1842}
1843
Johannes Bergdebff612013-05-14 13:53:45 +02001844static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001845{
Johannes Berg8ca151b2013-01-24 14:25:36 +01001846 struct ieee80211_vif *vif = NULL;
Matti Gottlieb23ae6122015-12-31 18:18:02 +02001847 int ret = 1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001848 enum iwl_d3_status d3_status;
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001849 bool keep = false;
Matti Gottlieb23ae6122015-12-31 18:18:02 +02001850 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1851 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
Luca Coelhofcea37b2017-04-12 16:33:41 +03001852 bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
1853 IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001854
1855 mutex_lock(&mvm->mutex);
1856
1857 /* get the BSS vif pointer again */
Luciano Coelho7f549e22014-10-02 15:38:04 +03001858 vif = iwl_mvm_get_bss_vif(mvm);
1859 if (IS_ERR_OR_NULL(vif))
Luciano Coelhoa500e462015-05-04 17:03:17 +03001860 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001861
Matti Gottlieb23ae6122015-12-31 18:18:02 +02001862 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001863 if (ret)
Luciano Coelhoa500e462015-05-04 17:03:17 +03001864 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001865
1866 if (d3_status != IWL_D3_STATUS_ALIVE) {
1867 IWL_INFO(mvm, "Device was reset during suspend\n");
Luciano Coelhoa500e462015-05-04 17:03:17 +03001868 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001869 }
1870
Johannes Bergafc66bb2013-05-03 11:44:16 +02001871 /* query SRAM first in case we want event logging */
1872 iwl_mvm_read_d3_sram(mvm);
1873
Luca Coelhofcea37b2017-04-12 16:33:41 +03001874 if (d0i3_first) {
1875 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
1876 if (ret < 0) {
1877 IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
1878 ret);
1879 goto err;
1880 }
1881 }
1882
Jonathan Doron47c8b152014-11-27 16:55:25 +02001883 /*
1884 * Query the current location and source from the D3 firmware so we
1885 * can play it back when we re-intiailize the D0 firmware
1886 */
1887 iwl_mvm_update_changed_regdom(mvm);
1888
Luca Coelho42ce76d2017-01-11 23:36:30 +02001889 if (!unified_image)
1890 /* Re-configure default SAR profile */
1891 iwl_mvm_sar_select_profile(mvm, 1, 1);
1892
Luciano Coelho2021a892014-11-20 08:59:51 +02001893 if (mvm->net_detect) {
Luca Coelho5a143db2016-10-05 09:28:53 +03001894 /* If this is a non-unified image, we restart the FW,
1895 * so no need to stop the netdetect scan. If that
1896 * fails, continue and try to get the wake-up reasons,
1897 * but trigger a HW restart by keeping a failure code
1898 * in ret.
1899 */
1900 if (unified_image)
1901 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
1902 false);
1903
Luciano Coelho2021a892014-11-20 08:59:51 +02001904 iwl_mvm_query_netdetect_reasons(mvm, vif);
Luciano Coelhoe0ede172015-02-13 21:37:09 +02001905 /* has unlocked the mutex, so skip that */
1906 goto out;
Luciano Coelho2021a892014-11-20 08:59:51 +02001907 } else {
1908 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
Eliad Peller78c9df62013-11-07 14:13:30 +02001909#ifdef CONFIG_IWLWIFI_DEBUGFS
Luciano Coelho2021a892014-11-20 08:59:51 +02001910 if (keep)
1911 mvm->keep_vif = vif;
Luciano Coelho0c2ae042015-03-12 09:25:15 +02001912#endif
Luciano Coelhoe0ede172015-02-13 21:37:09 +02001913 /* has unlocked the mutex, so skip that */
1914 goto out_iterate;
Luciano Coelho2021a892014-11-20 08:59:51 +02001915 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001916
Luciano Coelhoa500e462015-05-04 17:03:17 +03001917err:
1918 iwl_mvm_free_nd(mvm);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001919 mutex_unlock(&mvm->mutex);
1920
Luciano Coelhoe0ede172015-02-13 21:37:09 +02001921out_iterate:
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001922 if (!test)
1923 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1924 IEEE80211_IFACE_ITER_NORMAL,
1925 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001926
Luciano Coelhoe0ede172015-02-13 21:37:09 +02001927out:
Luca Coelhofcea37b2017-04-12 16:33:41 +03001928 /* no need to reset the device in unified images, if successful */
Matti Gottlieb23ae6122015-12-31 18:18:02 +02001929 if (unified_image && !ret) {
Luca Coelhofcea37b2017-04-12 16:33:41 +03001930 /* nothing else to do if we already sent D0I3_END_CMD */
1931 if (d0i3_first)
1932 return 0;
1933
Luca Coelhof28b9362017-04-12 12:42:12 +03001934 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
Luca Coelhofcea37b2017-04-12 16:33:41 +03001935 if (!ret)
Matti Gottlieb23ae6122015-12-31 18:18:02 +02001936 return 0;
1937 }
1938
1939 /*
1940 * Reconfigure the device in one of the following cases:
1941 * 1. We are not using a unified image
1942 * 2. We are using a unified image but had an error while exiting D3
1943 */
Matt Chen59cd8272017-08-28 14:57:54 +08001944 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
Johannes Berg58629d92014-11-06 09:40:50 +01001945 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
Matti Gottlieb23ae6122015-12-31 18:18:02 +02001946 /*
1947 * When switching images we return 1, which causes mac80211
1948 * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
1949 * This type of reconfig calls iwl_mvm_restart_complete(),
1950 * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
1951 * to take the reference here.
Luciano Coelhodcfc7fb2015-04-28 08:41:55 +03001952 */
1953 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
Matti Gottlieb23ae6122015-12-31 18:18:02 +02001954
Johannes Berg8ca151b2013-01-24 14:25:36 +01001955 return 1;
1956}
1957
Eliad Peller2d428012015-06-17 15:32:37 +03001958static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
1959{
1960 iwl_trans_resume(mvm->trans);
1961
1962 return __iwl_mvm_resume(mvm, false);
1963}
1964
1965static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
1966{
1967 bool exit_now;
Eliad Peller6dfb36c2015-07-09 14:17:24 +03001968 enum iwl_d3_status d3_status;
Luca Coelhob7282642015-09-17 23:44:14 +03001969 struct iwl_trans *trans = mvm->trans;
Eliad Peller6dfb36c2015-07-09 14:17:24 +03001970
Matti Gottlieb23ae6122015-12-31 18:18:02 +02001971 iwl_trans_d3_resume(trans, &d3_status, false, false);
Eliad Peller2d428012015-06-17 15:32:37 +03001972
1973 /*
1974 * make sure to clear D0I3_DEFER_WAKEUP before
1975 * calling iwl_trans_resume(), which might wait
1976 * for d0i3 exit completion.
1977 */
1978 mutex_lock(&mvm->d0i3_suspend_mutex);
1979 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1980 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1981 &mvm->d0i3_suspend_flags);
1982 mutex_unlock(&mvm->d0i3_suspend_mutex);
1983 if (exit_now) {
1984 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
1985 _iwl_mvm_exit_d0i3(mvm);
1986 }
1987
Luca Coelhob7282642015-09-17 23:44:14 +03001988 iwl_trans_resume(trans);
Eliad Peller2d428012015-06-17 15:32:37 +03001989
Luca Coelhob7282642015-09-17 23:44:14 +03001990 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
Eliad Peller2d428012015-06-17 15:32:37 +03001991 int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
1992
1993 if (ret)
1994 return ret;
1995 /*
1996 * d0i3 exit will be deferred until reconfig_complete.
1997 * make sure there we are out of d0i3.
1998 */
1999 }
2000 return 0;
2001}
2002
Johannes Bergdebff612013-05-14 13:53:45 +02002003int iwl_mvm_resume(struct ieee80211_hw *hw)
2004{
2005 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
Luca Coelhob7282642015-09-17 23:44:14 +03002006 int ret;
Johannes Bergdebff612013-05-14 13:53:45 +02002007
Luca Coelhob7282642015-09-17 23:44:14 +03002008 if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
2009 ret = iwl_mvm_resume_d0i3(mvm);
Eliad Peller2d428012015-06-17 15:32:37 +03002010 else
Luca Coelhob7282642015-09-17 23:44:14 +03002011 ret = iwl_mvm_resume_d3(mvm);
2012
2013 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2014
2015 return ret;
Johannes Bergdebff612013-05-14 13:53:45 +02002016}
2017
Johannes Berg8ca151b2013-01-24 14:25:36 +01002018void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2019{
2020 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2021
2022 device_set_wakeup_enable(mvm->trans->dev, enabled);
2023}
Johannes Bergdebff612013-05-14 13:53:45 +02002024
2025#ifdef CONFIG_IWLWIFI_DEBUGFS
2026static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2027{
2028 struct iwl_mvm *mvm = inode->i_private;
2029 int err;
2030
2031 if (mvm->d3_test_active)
2032 return -EBUSY;
2033
2034 file->private_data = inode->i_private;
2035
2036 ieee80211_stop_queues(mvm->hw);
2037 synchronize_net();
2038
Luca Coelhob7282642015-09-17 23:44:14 +03002039 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
2040
Johannes Bergdebff612013-05-14 13:53:45 +02002041 /* start pseudo D3 */
2042 rtnl_lock();
2043 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2044 rtnl_unlock();
2045 if (err > 0)
2046 err = -EINVAL;
2047 if (err) {
2048 ieee80211_wake_queues(mvm->hw);
2049 return err;
2050 }
2051 mvm->d3_test_active = true;
Eliad Peller78c9df62013-11-07 14:13:30 +02002052 mvm->keep_vif = NULL;
Johannes Bergdebff612013-05-14 13:53:45 +02002053 return 0;
2054}
2055
2056static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2057 size_t count, loff_t *ppos)
2058{
2059 struct iwl_mvm *mvm = file->private_data;
2060 u32 pme_asserted;
2061
2062 while (true) {
Eliad Peller4ed80a82013-11-07 13:23:21 +02002063 /* read pme_ptr if available */
2064 if (mvm->d3_test_pme_ptr) {
2065 pme_asserted = iwl_trans_read_mem32(mvm->trans,
2066 mvm->d3_test_pme_ptr);
2067 if (pme_asserted)
2068 break;
2069 }
2070
Johannes Bergdebff612013-05-14 13:53:45 +02002071 if (msleep_interruptible(100))
2072 break;
2073 }
2074
2075 return 0;
2076}
2077
2078static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2079 struct ieee80211_vif *vif)
2080{
Eliad Peller78c9df62013-11-07 14:13:30 +02002081 /* skip the one we keep connection on */
2082 if (_data == vif)
2083 return;
2084
Johannes Bergdebff612013-05-14 13:53:45 +02002085 if (vif->type == NL80211_IFTYPE_STATION)
2086 ieee80211_connection_loss(vif);
2087}
2088
2089static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2090{
2091 struct iwl_mvm *mvm = inode->i_private;
Luca Coelho85cd69b2016-10-05 11:24:12 +03002092 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2093 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
Johannes Bergdebff612013-05-14 13:53:45 +02002094
2095 mvm->d3_test_active = false;
Luca Coelhob7282642015-09-17 23:44:14 +03002096
Luciano Coelho31ca09d2014-09-22 13:22:51 +03002097 rtnl_lock();
Johannes Bergdebff612013-05-14 13:53:45 +02002098 __iwl_mvm_resume(mvm, true);
Luciano Coelho31ca09d2014-09-22 13:22:51 +03002099 rtnl_unlock();
Luca Coelhob7282642015-09-17 23:44:14 +03002100
2101 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2102
Johannes Bergdebff612013-05-14 13:53:45 +02002103 iwl_abort_notification_waits(&mvm->notif_wait);
Luca Coelho85cd69b2016-10-05 11:24:12 +03002104 if (!unified_image) {
2105 int remaining_time = 10;
Johannes Bergdebff612013-05-14 13:53:45 +02002106
Luca Coelho85cd69b2016-10-05 11:24:12 +03002107 ieee80211_restart_hw(mvm->hw);
2108
2109 /* wait for restart and disconnect all interfaces */
2110 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2111 remaining_time > 0) {
2112 remaining_time--;
2113 msleep(1000);
2114 }
2115
2116 if (remaining_time == 0)
2117 IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
Johannes Bergdebff612013-05-14 13:53:45 +02002118 }
2119
Johannes Bergdebff612013-05-14 13:53:45 +02002120 ieee80211_iterate_active_interfaces_atomic(
2121 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
Eliad Peller78c9df62013-11-07 14:13:30 +02002122 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
Johannes Bergdebff612013-05-14 13:53:45 +02002123
2124 ieee80211_wake_queues(mvm->hw);
2125
2126 return 0;
2127}
2128
2129const struct file_operations iwl_dbgfs_d3_test_ops = {
2130 .llseek = no_llseek,
2131 .open = iwl_mvm_d3_test_open,
2132 .read = iwl_mvm_d3_test_read,
2133 .release = iwl_mvm_d3_test_release,
2134};
2135#endif