]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/intel/iwlwifi/mvm/d3.c
Merge remote-tracking branch 'tty/tty-next'
[karo-tx-linux.git] / drivers / net / wireless / intel / iwlwifi / mvm / d3.c
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016   Intel Deutschland GmbH
11  *
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
27  * in the file called COPYING.
28  *
29  * Contact Information:
30  *  Intel Linux Wireless <linuxwifi@intel.com>
31  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32  *
33  * BSD LICENSE
34  *
35  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37  * Copyright(c) 2016   Intel Deutschland GmbH
38  * 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
68 #include <linux/etherdevice.h>
69 #include <linux/ip.h>
70 #include <linux/fs.h>
71 #include <net/cfg80211.h>
72 #include <net/ipv6.h>
73 #include <net/tcp.h>
74 #include <net/addrconf.h>
75 #include "iwl-modparams.h"
76 #include "fw-api.h"
77 #include "mvm.h"
78
79 void 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
86         if (iwlwifi_mod_params.sw_crypto)
87                 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 =
94                 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
95         mvmvif->rekey_data.valid = true;
96
97         mutex_unlock(&mvm->mutex);
98 }
99
100 #if IS_ENABLED(CONFIG_IPV6)
101 void 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
109         memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
110
111         read_lock_bh(&idev->lock);
112         list_for_each_entry(ifa, &idev->addr_list, if_list) {
113                 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
114                 if (ifa->flags & IFA_F_TENTATIVE)
115                         __set_bit(idx, mvmvif->tentative_addrs);
116                 idx++;
117                 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
118                         break;
119         }
120         read_unlock_bh(&idev->lock);
121
122         mvmvif->num_target_ipv6_addrs = idx;
123 }
124 #endif
125
126 void 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
134 static 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
142 static 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
164 struct wowlan_key_data {
165         struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
166         struct iwl_wowlan_tkip_params_cmd *tkip;
167         bool error, use_rsc_tsc, use_tkip, configure_keys;
168         int wep_key_idx;
169 };
170
171 static 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
189         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 */
220                         data->wep_key_idx++;
221                         wkc.wep_key.key_offset = data->wep_key_idx;
222                 }
223
224                 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;
229
230                         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                 }
236
237                 /* don't upload key again */
238                 return;
239         }
240         default:
241                 data->error = true;
242                 return;
243         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                  */
251                 return;
252         case WLAN_CIPHER_SUITE_TKIP:
253                 if (sta) {
254                         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
259                         ieee80211_get_key_tx_seq(key, &seq);
260                         tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
261                         tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
262
263                         ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
264                         iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
265
266                         memcpy(data->tkip->mic_keys.tx,
267                                &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
268                                IWL_MIC_KEY_SIZE);
269
270                         rx_mic_key = data->tkip->mic_keys.rx_unicast;
271                 } else {
272                         tkip_sc =
273                                 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
274                         rx_p1ks = data->tkip->rx_multi;
275                         rx_mic_key = data->tkip->mic_keys.rx_mcast;
276                 }
277
278                 /*
279                  * For non-QoS this relies on the fact that both the uCode and
280                  * mac80211 use TID 0 (as they need to to avoid replay attacks)
281                  * for checking the IV in the frames.
282                  */
283                 for (i = 0; i < IWL_NUM_RSC; i++) {
284                         ieee80211_get_key_rx_seq(key, i, &seq);
285                         tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
286                         tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
287                         /* wrapping isn't allowed, AP must rekey */
288                         if (seq.tkip.iv32 > cur_rx_iv32)
289                                 cur_rx_iv32 = seq.tkip.iv32;
290                 }
291
292                 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
293                                           cur_rx_iv32, p1k);
294                 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
295                 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
296                                           cur_rx_iv32 + 1, p1k);
297                 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
298
299                 memcpy(rx_mic_key,
300                        &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
301                        IWL_MIC_KEY_SIZE);
302
303                 data->use_tkip = true;
304                 data->use_rsc_tsc = true;
305                 break;
306         case WLAN_CIPHER_SUITE_CCMP:
307                 if (sta) {
308                         u64 pn64;
309
310                         aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
311                         aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
312
313                         pn64 = atomic64_read(&key->tx_pn);
314                         aes_tx_sc->pn = cpu_to_le64(pn64);
315                 } else {
316                         aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
317                 }
318
319                 /*
320                  * For non-QoS this relies on the fact that both the uCode and
321                  * mac80211/our RX code use TID 0 for checking the PN.
322                  */
323                 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
324                         struct iwl_mvm_sta *mvmsta;
325                         struct iwl_mvm_key_pn *ptk_pn;
326                         const u8 *pn;
327
328                         mvmsta = iwl_mvm_sta_from_mac80211(sta);
329                         ptk_pn = rcu_dereference_protected(
330                                                 mvmsta->ptk_pn[key->keyidx],
331                                                 lockdep_is_held(&mvm->mutex));
332                         if (WARN_ON(!ptk_pn))
333                                 break;
334
335                         for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
336                                 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
337                                                 mvm->trans->num_rx_queues);
338                                 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
339                                                            ((u64)pn[4] << 8) |
340                                                            ((u64)pn[3] << 16) |
341                                                            ((u64)pn[2] << 24) |
342                                                            ((u64)pn[1] << 32) |
343                                                            ((u64)pn[0] << 40));
344                         }
345                 } else {
346                         for (i = 0; i < IWL_NUM_RSC; i++) {
347                                 u8 *pn = seq.ccmp.pn;
348
349                                 ieee80211_get_key_rx_seq(key, i, &seq);
350                                 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
351                                                            ((u64)pn[4] << 8) |
352                                                            ((u64)pn[3] << 16) |
353                                                            ((u64)pn[2] << 24) |
354                                                            ((u64)pn[1] << 32) |
355                                                            ((u64)pn[0] << 40));
356                         }
357                 }
358                 data->use_rsc_tsc = true;
359                 break;
360         }
361
362         if (data->configure_keys) {
363                 mutex_lock(&mvm->mutex);
364                 /*
365                  * The D3 firmware hardcodes the key offset 0 as the key it
366                  * uses to transmit packets to the AP, i.e. the PTK.
367                  */
368                 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
369                         mvm->ptk_ivlen = key->iv_len;
370                         mvm->ptk_icvlen = key->icv_len;
371                         ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
372                 } else {
373                         /*
374                          * firmware only supports TSC/RSC for a single key,
375                          * so if there are multiple keep overwriting them
376                          * with new ones -- this relies on mac80211 doing
377                          * list_add_tail().
378                          */
379                         mvm->gtk_ivlen = key->iv_len;
380                         mvm->gtk_icvlen = key->icv_len;
381                         ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
382                 }
383                 mutex_unlock(&mvm->mutex);
384                 data->error = ret != 0;
385         }
386 }
387
388 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
389                                  struct cfg80211_wowlan *wowlan)
390 {
391         struct iwl_wowlan_patterns_cmd *pattern_cmd;
392         struct iwl_host_cmd cmd = {
393                 .id = WOWLAN_PATTERNS,
394                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
395         };
396         int i, err;
397
398         if (!wowlan->n_patterns)
399                 return 0;
400
401         cmd.len[0] = sizeof(*pattern_cmd) +
402                 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
403
404         pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
405         if (!pattern_cmd)
406                 return -ENOMEM;
407
408         pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
409
410         for (i = 0; i < wowlan->n_patterns; i++) {
411                 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
412
413                 memcpy(&pattern_cmd->patterns[i].mask,
414                        wowlan->patterns[i].mask, mask_len);
415                 memcpy(&pattern_cmd->patterns[i].pattern,
416                        wowlan->patterns[i].pattern,
417                        wowlan->patterns[i].pattern_len);
418                 pattern_cmd->patterns[i].mask_size = mask_len;
419                 pattern_cmd->patterns[i].pattern_size =
420                         wowlan->patterns[i].pattern_len;
421         }
422
423         cmd.data[0] = pattern_cmd;
424         err = iwl_mvm_send_cmd(mvm, &cmd);
425         kfree(pattern_cmd);
426         return err;
427 }
428
429 enum iwl_mvm_tcp_packet_type {
430         MVM_TCP_TX_SYN,
431         MVM_TCP_RX_SYNACK,
432         MVM_TCP_TX_DATA,
433         MVM_TCP_RX_ACK,
434         MVM_TCP_RX_WAKE,
435         MVM_TCP_TX_FIN,
436 };
437
438 static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
439 {
440         __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
441         return cpu_to_le16(be16_to_cpu((__force __be16)check));
442 }
443
444 static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
445                                      struct cfg80211_wowlan_tcp *tcp,
446                                      void *_pkt, u8 *mask,
447                                      __le16 *pseudo_hdr_csum,
448                                      enum iwl_mvm_tcp_packet_type ptype)
449 {
450         struct {
451                 struct ethhdr eth;
452                 struct iphdr ip;
453                 struct tcphdr tcp;
454                 u8 data[];
455         } __packed *pkt = _pkt;
456         u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
457         int i;
458
459         pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
460         pkt->ip.version = 4;
461         pkt->ip.ihl = 5;
462         pkt->ip.protocol = IPPROTO_TCP;
463
464         switch (ptype) {
465         case MVM_TCP_TX_SYN:
466         case MVM_TCP_TX_DATA:
467         case MVM_TCP_TX_FIN:
468                 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
469                 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
470                 pkt->ip.ttl = 128;
471                 pkt->ip.saddr = tcp->src;
472                 pkt->ip.daddr = tcp->dst;
473                 pkt->tcp.source = cpu_to_be16(tcp->src_port);
474                 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
475                 /* overwritten for TX SYN later */
476                 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
477                 pkt->tcp.window = cpu_to_be16(65000);
478                 break;
479         case MVM_TCP_RX_SYNACK:
480         case MVM_TCP_RX_ACK:
481         case MVM_TCP_RX_WAKE:
482                 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
483                 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
484                 pkt->ip.saddr = tcp->dst;
485                 pkt->ip.daddr = tcp->src;
486                 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
487                 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
488                 break;
489         default:
490                 WARN_ON(1);
491                 return;
492         }
493
494         switch (ptype) {
495         case MVM_TCP_TX_SYN:
496                 /* firmware assumes 8 option bytes - 8 NOPs for now */
497                 memset(pkt->data, 0x01, 8);
498                 ip_tot_len += 8;
499                 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
500                 pkt->tcp.syn = 1;
501                 break;
502         case MVM_TCP_TX_DATA:
503                 ip_tot_len += tcp->payload_len;
504                 memcpy(pkt->data, tcp->payload, tcp->payload_len);
505                 pkt->tcp.psh = 1;
506                 pkt->tcp.ack = 1;
507                 break;
508         case MVM_TCP_TX_FIN:
509                 pkt->tcp.fin = 1;
510                 pkt->tcp.ack = 1;
511                 break;
512         case MVM_TCP_RX_SYNACK:
513                 pkt->tcp.syn = 1;
514                 pkt->tcp.ack = 1;
515                 break;
516         case MVM_TCP_RX_ACK:
517                 pkt->tcp.ack = 1;
518                 break;
519         case MVM_TCP_RX_WAKE:
520                 ip_tot_len += tcp->wake_len;
521                 pkt->tcp.psh = 1;
522                 pkt->tcp.ack = 1;
523                 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
524                 break;
525         }
526
527         switch (ptype) {
528         case MVM_TCP_TX_SYN:
529         case MVM_TCP_TX_DATA:
530         case MVM_TCP_TX_FIN:
531                 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
532                 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
533                 break;
534         case MVM_TCP_RX_WAKE:
535                 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
536                         u8 tmp = tcp->wake_mask[i];
537                         mask[i + 6] |= tmp << 6;
538                         if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
539                                 mask[i + 7] = tmp >> 2;
540                 }
541                 /* fall through for ethernet/IP/TCP headers mask */
542         case MVM_TCP_RX_SYNACK:
543         case MVM_TCP_RX_ACK:
544                 mask[0] = 0xff; /* match ethernet */
545                 /*
546                  * match ethernet, ip.version, ip.ihl
547                  * the ip.ihl half byte is really masked out by firmware
548                  */
549                 mask[1] = 0x7f;
550                 mask[2] = 0x80; /* match ip.protocol */
551                 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
552                 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
553                 mask[5] = 0x80; /* match tcp flags */
554                 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
555                 break;
556         };
557
558         *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
559                                             pkt->ip.saddr, pkt->ip.daddr);
560 }
561
562 static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
563                                         struct ieee80211_vif *vif,
564                                         struct cfg80211_wowlan_tcp *tcp)
565 {
566         struct iwl_wowlan_remote_wake_config *cfg;
567         struct iwl_host_cmd cmd = {
568                 .id = REMOTE_WAKE_CONFIG_CMD,
569                 .len = { sizeof(*cfg), },
570                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
571         };
572         int ret;
573
574         if (!tcp)
575                 return 0;
576
577         cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
578         if (!cfg)
579                 return -ENOMEM;
580         cmd.data[0] = cfg;
581
582         cfg->max_syn_retries = 10;
583         cfg->max_data_retries = 10;
584         cfg->tcp_syn_ack_timeout = 1; /* seconds */
585         cfg->tcp_ack_timeout = 1; /* seconds */
586
587         /* SYN (TX) */
588         iwl_mvm_build_tcp_packet(
589                 vif, tcp, cfg->syn_tx.data, NULL,
590                 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
591                 MVM_TCP_TX_SYN);
592         cfg->syn_tx.info.tcp_payload_length = 0;
593
594         /* SYN/ACK (RX) */
595         iwl_mvm_build_tcp_packet(
596                 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
597                 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
598                 MVM_TCP_RX_SYNACK);
599         cfg->synack_rx.info.tcp_payload_length = 0;
600
601         /* KEEPALIVE/ACK (TX) */
602         iwl_mvm_build_tcp_packet(
603                 vif, tcp, cfg->keepalive_tx.data, NULL,
604                 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
605                 MVM_TCP_TX_DATA);
606         cfg->keepalive_tx.info.tcp_payload_length =
607                 cpu_to_le16(tcp->payload_len);
608         cfg->sequence_number_offset = tcp->payload_seq.offset;
609         /* length must be 0..4, the field is little endian */
610         cfg->sequence_number_length = tcp->payload_seq.len;
611         cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
612         cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
613         if (tcp->payload_tok.len) {
614                 cfg->token_offset = tcp->payload_tok.offset;
615                 cfg->token_length = tcp->payload_tok.len;
616                 cfg->num_tokens =
617                         cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
618                 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
619                        tcp->tokens_size);
620         } else {
621                 /* set tokens to max value to almost never run out */
622                 cfg->num_tokens = cpu_to_le16(65535);
623         }
624
625         /* ACK (RX) */
626         iwl_mvm_build_tcp_packet(
627                 vif, tcp, cfg->keepalive_ack_rx.data,
628                 cfg->keepalive_ack_rx.rx_mask,
629                 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
630                 MVM_TCP_RX_ACK);
631         cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
632
633         /* WAKEUP (RX) */
634         iwl_mvm_build_tcp_packet(
635                 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
636                 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
637                 MVM_TCP_RX_WAKE);
638         cfg->wake_rx.info.tcp_payload_length =
639                 cpu_to_le16(tcp->wake_len);
640
641         /* FIN */
642         iwl_mvm_build_tcp_packet(
643                 vif, tcp, cfg->fin_tx.data, NULL,
644                 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
645                 MVM_TCP_TX_FIN);
646         cfg->fin_tx.info.tcp_payload_length = 0;
647
648         ret = iwl_mvm_send_cmd(mvm, &cmd);
649         kfree(cfg);
650
651         return ret;
652 }
653
654 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
655                                 struct ieee80211_sta *ap_sta)
656 {
657         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
658         struct ieee80211_chanctx_conf *ctx;
659         u8 chains_static, chains_dynamic;
660         struct cfg80211_chan_def chandef;
661         int ret, i;
662         struct iwl_binding_cmd binding_cmd = {};
663         struct iwl_time_quota_cmd quota_cmd = {};
664         u32 status;
665
666         /* add back the PHY */
667         if (WARN_ON(!mvmvif->phy_ctxt))
668                 return -EINVAL;
669
670         rcu_read_lock();
671         ctx = rcu_dereference(vif->chanctx_conf);
672         if (WARN_ON(!ctx)) {
673                 rcu_read_unlock();
674                 return -EINVAL;
675         }
676         chandef = ctx->def;
677         chains_static = ctx->rx_chains_static;
678         chains_dynamic = ctx->rx_chains_dynamic;
679         rcu_read_unlock();
680
681         ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
682                                    chains_static, chains_dynamic);
683         if (ret)
684                 return ret;
685
686         /* add back the MAC */
687         mvmvif->uploaded = false;
688
689         if (WARN_ON(!vif->bss_conf.assoc))
690                 return -EINVAL;
691
692         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
693         if (ret)
694                 return ret;
695
696         /* add back binding - XXX refactor? */
697         binding_cmd.id_and_color =
698                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
699                                                 mvmvif->phy_ctxt->color));
700         binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
701         binding_cmd.phy =
702                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
703                                                 mvmvif->phy_ctxt->color));
704         binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
705                                                               mvmvif->color));
706         for (i = 1; i < MAX_MACS_IN_BINDING; i++)
707                 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
708
709         status = 0;
710         ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
711                                           sizeof(binding_cmd), &binding_cmd,
712                                           &status);
713         if (ret) {
714                 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
715                 return ret;
716         }
717
718         if (status) {
719                 IWL_ERR(mvm, "Binding command failed: %u\n", status);
720                 return -EIO;
721         }
722
723         ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
724         if (ret)
725                 return ret;
726         rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
727
728         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
729         if (ret)
730                 return ret;
731
732         /* and some quota */
733         quota_cmd.quotas[0].id_and_color =
734                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
735                                                 mvmvif->phy_ctxt->color));
736         quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
737         quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
738
739         for (i = 1; i < MAX_BINDINGS; i++)
740                 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
741
742         ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
743                                    sizeof(quota_cmd), &quota_cmd);
744         if (ret)
745                 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
746
747         if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
748                 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
749
750         return 0;
751 }
752
753 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
754                                        struct ieee80211_vif *vif)
755 {
756         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
757         struct iwl_nonqos_seq_query_cmd query_cmd = {
758                 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
759                 .mac_id_n_color =
760                         cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
761                                                         mvmvif->color)),
762         };
763         struct iwl_host_cmd cmd = {
764                 .id = NON_QOS_TX_COUNTER_CMD,
765                 .flags = CMD_WANT_SKB,
766         };
767         int err;
768         u32 size;
769
770         cmd.data[0] = &query_cmd;
771         cmd.len[0] = sizeof(query_cmd);
772
773         err = iwl_mvm_send_cmd(mvm, &cmd);
774         if (err)
775                 return err;
776
777         size = iwl_rx_packet_payload_len(cmd.resp_pkt);
778         if (size < sizeof(__le16)) {
779                 err = -EINVAL;
780         } else {
781                 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
782                 /* firmware returns next, not last-used seqno */
783                 err = (u16) (err - 0x10);
784         }
785
786         iwl_free_resp(&cmd);
787         return err;
788 }
789
790 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
791 {
792         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
793         struct iwl_nonqos_seq_query_cmd query_cmd = {
794                 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
795                 .mac_id_n_color =
796                         cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
797                                                         mvmvif->color)),
798                 .value = cpu_to_le16(mvmvif->seqno),
799         };
800
801         /* return if called during restart, not resume from D3 */
802         if (!mvmvif->seqno_valid)
803                 return;
804
805         mvmvif->seqno_valid = false;
806
807         if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
808                                  sizeof(query_cmd), &query_cmd))
809                 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
810 }
811
812 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
813 {
814         iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
815
816         iwl_trans_stop_device(mvm->trans);
817
818         /*
819          * Set the HW restart bit -- this is mostly true as we're
820          * going to load new firmware and reprogram that, though
821          * the reprogramming is going to be manual to avoid adding
822          * all the MACs that aren't support.
823          * We don't have to clear up everything though because the
824          * reprogramming is manual. When we resume, we'll actually
825          * go through a proper restart sequence again to switch
826          * back to the runtime firmware image.
827          */
828         set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
829
830         /* the fw is reset, so all the keys are cleared */
831         memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
832
833         mvm->ptk_ivlen = 0;
834         mvm->ptk_icvlen = 0;
835         mvm->ptk_ivlen = 0;
836         mvm->ptk_icvlen = 0;
837
838         return iwl_mvm_load_d3_fw(mvm);
839 }
840
841 static int
842 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
843                           struct cfg80211_wowlan *wowlan,
844                           struct iwl_wowlan_config_cmd *wowlan_config_cmd,
845                           struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
846                           struct ieee80211_sta *ap_sta)
847 {
848         int ret;
849         struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
850
851         /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
852
853         wowlan_config_cmd->is_11n_connection =
854                                         ap_sta->ht_cap.ht_supported;
855         wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
856                 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING |
857                 ENABLE_STORE_BEACON;
858
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
864         wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
865
866         iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
867
868         if (wowlan->disconnect)
869                 wowlan_config_cmd->wakeup_filter |=
870                         cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
871                                     IWL_WOWLAN_WAKEUP_LINK_CHANGE);
872         if (wowlan->magic_pkt)
873                 wowlan_config_cmd->wakeup_filter |=
874                         cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
875         if (wowlan->gtk_rekey_failure)
876                 wowlan_config_cmd->wakeup_filter |=
877                         cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
878         if (wowlan->eap_identity_req)
879                 wowlan_config_cmd->wakeup_filter |=
880                         cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
881         if (wowlan->four_way_handshake)
882                 wowlan_config_cmd->wakeup_filter |=
883                         cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
884         if (wowlan->n_patterns)
885                 wowlan_config_cmd->wakeup_filter |=
886                         cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
887
888         if (wowlan->rfkill_release)
889                 wowlan_config_cmd->wakeup_filter |=
890                         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                  */
897                 wowlan_config_cmd->wakeup_filter |=
898                         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
907 static void
908 iwl_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);
926 out:
927         rcu_read_unlock();
928 }
929
930 int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
931                                      struct ieee80211_vif *vif,
932                                      bool d0i3,
933                                      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 = {
938                 .configure_keys = !d0i3,
939                 .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         /*
951          * 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.
956          */
957         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         }
973
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
997         /* configure rekey data only if offloaded rekey is supported (d3) */
998         if (mvmvif->rekey_data.valid && !d0i3) {
999                 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         }
1015         ret = 0;
1016 out:
1017         kfree(key_data.rsc_tsc);
1018         return ret;
1019 }
1020
1021 static int
1022 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1023                       struct cfg80211_wowlan *wowlan,
1024                       struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1025                       struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1026                       struct ieee80211_sta *ap_sta)
1027 {
1028         int ret;
1029         bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1030                                          IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1031
1032         if (!unified_image) {
1033                 ret = iwl_mvm_switch_to_d3(mvm);
1034                 if (ret)
1035                         return ret;
1036
1037                 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1038                 if (ret)
1039                         return ret;
1040         }
1041
1042         if (!iwlwifi_mod_params.sw_crypto) {
1043                 /*
1044                  * This needs to be unlocked due to lock ordering
1045                  * constraints. Since we're in the suspend path
1046                  * that isn't really a problem though.
1047                  */
1048                 mutex_unlock(&mvm->mutex);
1049                 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
1050                                                        CMD_ASYNC);
1051                 mutex_lock(&mvm->mutex);
1052                 if (ret)
1053                         return ret;
1054         }
1055
1056         ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1057                                    sizeof(*wowlan_config_cmd),
1058                                    wowlan_config_cmd);
1059         if (ret)
1060                 return ret;
1061
1062         ret = iwl_mvm_send_patterns(mvm, wowlan);
1063         if (ret)
1064                 return ret;
1065
1066         ret = iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
1067         if (ret)
1068                 return ret;
1069
1070         ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1071         return ret;
1072 }
1073
1074 static int
1075 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1076                          struct cfg80211_wowlan *wowlan,
1077                          struct cfg80211_sched_scan_request *nd_config,
1078                          struct ieee80211_vif *vif)
1079 {
1080         struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1081         int ret;
1082         bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1083                                          IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1084
1085         if (!unified_image) {
1086                 ret = iwl_mvm_switch_to_d3(mvm);
1087                 if (ret)
1088                         return ret;
1089         }
1090
1091         /* rfkill release can be either for wowlan or netdetect */
1092         if (wowlan->rfkill_release)
1093                 wowlan_config_cmd.wakeup_filter |=
1094                         cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
1095
1096         ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1097                                    sizeof(wowlan_config_cmd),
1098                                    &wowlan_config_cmd);
1099         if (ret)
1100                 return ret;
1101
1102         ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1103                                        IWL_MVM_SCAN_NETDETECT);
1104         if (ret)
1105                 return ret;
1106
1107         if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1108                 return -EBUSY;
1109
1110         /* save the sched scan matchsets... */
1111         if (nd_config->n_match_sets) {
1112                 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1113                                              sizeof(*nd_config->match_sets) *
1114                                              nd_config->n_match_sets,
1115                                              GFP_KERNEL);
1116                 if (mvm->nd_match_sets)
1117                         mvm->n_nd_match_sets = nd_config->n_match_sets;
1118         }
1119
1120         /* ...and the sched scan channels for later reporting */
1121         mvm->nd_channels = kmemdup(nd_config->channels,
1122                                    sizeof(*nd_config->channels) *
1123                                    nd_config->n_channels,
1124                                    GFP_KERNEL);
1125         if (mvm->nd_channels)
1126                 mvm->n_nd_channels = nd_config->n_channels;
1127
1128         return 0;
1129 }
1130
1131 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1132 {
1133         kfree(mvm->nd_match_sets);
1134         mvm->nd_match_sets = NULL;
1135         mvm->n_nd_match_sets = 0;
1136         kfree(mvm->nd_channels);
1137         mvm->nd_channels = NULL;
1138         mvm->n_nd_channels = 0;
1139 }
1140
1141 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1142                              struct cfg80211_wowlan *wowlan,
1143                              bool test)
1144 {
1145         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1146         struct ieee80211_vif *vif = NULL;
1147         struct iwl_mvm_vif *mvmvif = NULL;
1148         struct ieee80211_sta *ap_sta = NULL;
1149         struct iwl_d3_manager_config d3_cfg_cmd_data = {
1150                 /*
1151                  * Program the minimum sleep time to 10 seconds, as many
1152                  * platforms have issues processing a wakeup signal while
1153                  * still being in the process of suspending.
1154                  */
1155                 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1156         };
1157         struct iwl_host_cmd d3_cfg_cmd = {
1158                 .id = D3_CONFIG_CMD,
1159                 .flags = CMD_WANT_SKB,
1160                 .data[0] = &d3_cfg_cmd_data,
1161                 .len[0] = sizeof(d3_cfg_cmd_data),
1162         };
1163         int ret;
1164         int len __maybe_unused;
1165         bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1166                                          IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1167
1168         if (!wowlan) {
1169                 /*
1170                  * mac80211 shouldn't get here, but for D3 test
1171                  * it doesn't warrant a warning
1172                  */
1173                 WARN_ON(!test);
1174                 return -EINVAL;
1175         }
1176
1177         mutex_lock(&mvm->mutex);
1178
1179         vif = iwl_mvm_get_bss_vif(mvm);
1180         if (IS_ERR_OR_NULL(vif)) {
1181                 ret = 1;
1182                 goto out_noreset;
1183         }
1184
1185         mvmvif = iwl_mvm_vif_from_mac80211(vif);
1186
1187         if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
1188                 /* if we're not associated, this must be netdetect */
1189                 if (!wowlan->nd_config) {
1190                         ret = 1;
1191                         goto out_noreset;
1192                 }
1193
1194                 ret = iwl_mvm_netdetect_config(
1195                         mvm, wowlan, wowlan->nd_config, vif);
1196                 if (ret)
1197                         goto out;
1198
1199                 mvm->net_detect = true;
1200         } else {
1201                 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1202
1203                 ap_sta = rcu_dereference_protected(
1204                         mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1205                         lockdep_is_held(&mvm->mutex));
1206                 if (IS_ERR_OR_NULL(ap_sta)) {
1207                         ret = -EINVAL;
1208                         goto out_noreset;
1209                 }
1210
1211                 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1212                                                 vif, mvmvif, ap_sta);
1213                 if (ret)
1214                         goto out_noreset;
1215                 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1216                                             vif, mvmvif, ap_sta);
1217                 if (ret)
1218                         goto out;
1219
1220                 mvm->net_detect = false;
1221         }
1222
1223         ret = iwl_mvm_power_update_device(mvm);
1224         if (ret)
1225                 goto out;
1226
1227         ret = iwl_mvm_power_update_mac(mvm);
1228         if (ret)
1229                 goto out;
1230
1231 #ifdef CONFIG_IWLWIFI_DEBUGFS
1232         if (mvm->d3_wake_sysassert)
1233                 d3_cfg_cmd_data.wakeup_flags |=
1234                         cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1235 #endif
1236
1237         /* must be last -- this switches firmware state */
1238         ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1239         if (ret)
1240                 goto out;
1241 #ifdef CONFIG_IWLWIFI_DEBUGFS
1242         len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1243         if (len >= sizeof(u32)) {
1244                 mvm->d3_test_pme_ptr =
1245                         le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1246         }
1247 #endif
1248         iwl_free_resp(&d3_cfg_cmd);
1249
1250         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1251
1252         iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1253  out:
1254         if (ret < 0) {
1255                 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1256                 ieee80211_restart_hw(mvm->hw);
1257                 iwl_mvm_free_nd(mvm);
1258         }
1259  out_noreset:
1260         mutex_unlock(&mvm->mutex);
1261
1262         return ret;
1263 }
1264
1265 static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
1266 {
1267         struct iwl_notification_wait wait_d3;
1268         static const u16 d3_notif[] = { D3_CONFIG_CMD };
1269         int ret;
1270
1271         iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
1272                                    d3_notif, ARRAY_SIZE(d3_notif),
1273                                    NULL, NULL);
1274
1275         ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
1276         if (ret)
1277                 goto remove_notif;
1278
1279         ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
1280         WARN_ON_ONCE(ret);
1281         return ret;
1282
1283 remove_notif:
1284         iwl_remove_notification(&mvm->notif_wait, &wait_d3);
1285         return ret;
1286 }
1287
1288 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1289 {
1290         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1291         struct iwl_trans *trans = mvm->trans;
1292         int ret;
1293
1294         /* make sure the d0i3 exit work is not pending */
1295         flush_work(&mvm->d0i3_exit_work);
1296
1297         ret = iwl_trans_suspend(trans);
1298         if (ret)
1299                 return ret;
1300
1301         if (wowlan->any) {
1302                 trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
1303
1304                 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
1305                         ret = iwl_mvm_enter_d0i3_sync(mvm);
1306
1307                         if (ret)
1308                                 return ret;
1309                 }
1310
1311                 mutex_lock(&mvm->d0i3_suspend_mutex);
1312                 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1313                 mutex_unlock(&mvm->d0i3_suspend_mutex);
1314
1315                 iwl_trans_d3_suspend(trans, false, false);
1316
1317                 return 0;
1318         }
1319
1320         trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1321
1322         return __iwl_mvm_suspend(hw, wowlan, false);
1323 }
1324
1325 /* converted data from the different status responses */
1326 struct iwl_wowlan_status_data {
1327         u16 pattern_number;
1328         u16 qos_seq_ctr[8];
1329         u32 wakeup_reasons;
1330         u32 wake_packet_length;
1331         u32 wake_packet_bufsize;
1332         const u8 *wake_packet;
1333 };
1334
1335 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1336                                           struct ieee80211_vif *vif,
1337                                           struct iwl_wowlan_status_data *status)
1338 {
1339         struct sk_buff *pkt = NULL;
1340         struct cfg80211_wowlan_wakeup wakeup = {
1341                 .pattern_idx = -1,
1342         };
1343         struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1344         u32 reasons = status->wakeup_reasons;
1345
1346         if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1347                 wakeup_report = NULL;
1348                 goto report;
1349         }
1350
1351         pm_wakeup_event(mvm->dev, 0);
1352
1353         if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1354                 wakeup.magic_pkt = true;
1355
1356         if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1357                 wakeup.pattern_idx =
1358                         status->pattern_number;
1359
1360         if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1361                        IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1362                 wakeup.disconnect = true;
1363
1364         if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1365                 wakeup.gtk_rekey_failure = true;
1366
1367         if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1368                 wakeup.rfkill_release = true;
1369
1370         if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1371                 wakeup.eap_identity_req = true;
1372
1373         if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1374                 wakeup.four_way_handshake = true;
1375
1376         if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1377                 wakeup.tcp_connlost = true;
1378
1379         if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1380                 wakeup.tcp_nomoretokens = true;
1381
1382         if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1383                 wakeup.tcp_match = true;
1384
1385         if (status->wake_packet_bufsize) {
1386                 int pktsize = status->wake_packet_bufsize;
1387                 int pktlen = status->wake_packet_length;
1388                 const u8 *pktdata = status->wake_packet;
1389                 struct ieee80211_hdr *hdr = (void *)pktdata;
1390                 int truncated = pktlen - pktsize;
1391
1392                 /* this would be a firmware bug */
1393                 if (WARN_ON_ONCE(truncated < 0))
1394                         truncated = 0;
1395
1396                 if (ieee80211_is_data(hdr->frame_control)) {
1397                         int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1398                         int ivlen = 0, icvlen = 4; /* also FCS */
1399
1400                         pkt = alloc_skb(pktsize, GFP_KERNEL);
1401                         if (!pkt)
1402                                 goto report;
1403
1404                         memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1405                         pktdata += hdrlen;
1406                         pktsize -= hdrlen;
1407
1408                         if (ieee80211_has_protected(hdr->frame_control)) {
1409                                 /*
1410                                  * This is unlocked and using gtk_i(c)vlen,
1411                                  * but since everything is under RTNL still
1412                                  * that's not really a problem - changing
1413                                  * it would be difficult.
1414                                  */
1415                                 if (is_multicast_ether_addr(hdr->addr1)) {
1416                                         ivlen = mvm->gtk_ivlen;
1417                                         icvlen += mvm->gtk_icvlen;
1418                                 } else {
1419                                         ivlen = mvm->ptk_ivlen;
1420                                         icvlen += mvm->ptk_icvlen;
1421                                 }
1422                         }
1423
1424                         /* if truncated, FCS/ICV is (partially) gone */
1425                         if (truncated >= icvlen) {
1426                                 icvlen = 0;
1427                                 truncated -= icvlen;
1428                         } else {
1429                                 icvlen -= truncated;
1430                                 truncated = 0;
1431                         }
1432
1433                         pktsize -= ivlen + icvlen;
1434                         pktdata += ivlen;
1435
1436                         memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1437
1438                         if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1439                                 goto report;
1440                         wakeup.packet = pkt->data;
1441                         wakeup.packet_present_len = pkt->len;
1442                         wakeup.packet_len = pkt->len - truncated;
1443                         wakeup.packet_80211 = false;
1444                 } else {
1445                         int fcslen = 4;
1446
1447                         if (truncated >= 4) {
1448                                 truncated -= 4;
1449                                 fcslen = 0;
1450                         } else {
1451                                 fcslen -= truncated;
1452                                 truncated = 0;
1453                         }
1454                         pktsize -= fcslen;
1455                         wakeup.packet = status->wake_packet;
1456                         wakeup.packet_present_len = pktsize;
1457                         wakeup.packet_len = pktlen - truncated;
1458                         wakeup.packet_80211 = true;
1459                 }
1460         }
1461
1462  report:
1463         ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1464         kfree_skb(pkt);
1465 }
1466
1467 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1468                                   struct ieee80211_key_seq *seq)
1469 {
1470         u64 pn;
1471
1472         pn = le64_to_cpu(sc->pn);
1473         seq->ccmp.pn[0] = pn >> 40;
1474         seq->ccmp.pn[1] = pn >> 32;
1475         seq->ccmp.pn[2] = pn >> 24;
1476         seq->ccmp.pn[3] = pn >> 16;
1477         seq->ccmp.pn[4] = pn >> 8;
1478         seq->ccmp.pn[5] = pn;
1479 }
1480
1481 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1482                                    struct ieee80211_key_seq *seq)
1483 {
1484         seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1485         seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1486 }
1487
1488 static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
1489                                    struct ieee80211_sta *sta,
1490                                    struct ieee80211_key_conf *key)
1491 {
1492         int tid;
1493
1494         BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1495
1496         if (sta && iwl_mvm_has_new_rx_api(mvm)) {
1497                 struct iwl_mvm_sta *mvmsta;
1498                 struct iwl_mvm_key_pn *ptk_pn;
1499
1500                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1501
1502                 ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
1503                                                    lockdep_is_held(&mvm->mutex));
1504                 if (WARN_ON(!ptk_pn))
1505                         return;
1506
1507                 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1508                         struct ieee80211_key_seq seq = {};
1509                         int i;
1510
1511                         iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1512                         ieee80211_set_key_rx_seq(key, tid, &seq);
1513                         for (i = 1; i < mvm->trans->num_rx_queues; i++)
1514                                 memcpy(ptk_pn->q[i].pn[tid],
1515                                        seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
1516                 }
1517         } else {
1518                 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1519                         struct ieee80211_key_seq seq = {};
1520
1521                         iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1522                         ieee80211_set_key_rx_seq(key, tid, &seq);
1523                 }
1524         }
1525 }
1526
1527 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1528                                     struct ieee80211_key_conf *key)
1529 {
1530         int tid;
1531
1532         BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1533
1534         for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1535                 struct ieee80211_key_seq seq = {};
1536
1537                 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1538                 ieee80211_set_key_rx_seq(key, tid, &seq);
1539         }
1540 }
1541
1542 static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
1543                                    struct ieee80211_key_conf *key,
1544                                    struct iwl_wowlan_status *status)
1545 {
1546         union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1547
1548         switch (key->cipher) {
1549         case WLAN_CIPHER_SUITE_CCMP:
1550                 iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
1551                 break;
1552         case WLAN_CIPHER_SUITE_TKIP:
1553                 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1554                 break;
1555         default:
1556                 WARN_ON(1);
1557         }
1558 }
1559
1560 struct iwl_mvm_d3_gtk_iter_data {
1561         struct iwl_mvm *mvm;
1562         struct iwl_wowlan_status *status;
1563         void *last_gtk;
1564         u32 cipher;
1565         bool find_phase, unhandled_cipher;
1566         int num_keys;
1567 };
1568
1569 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1570                                    struct ieee80211_vif *vif,
1571                                    struct ieee80211_sta *sta,
1572                                    struct ieee80211_key_conf *key,
1573                                    void *_data)
1574 {
1575         struct iwl_mvm_d3_gtk_iter_data *data = _data;
1576
1577         if (data->unhandled_cipher)
1578                 return;
1579
1580         switch (key->cipher) {
1581         case WLAN_CIPHER_SUITE_WEP40:
1582         case WLAN_CIPHER_SUITE_WEP104:
1583                 /* ignore WEP completely, nothing to do */
1584                 return;
1585         case WLAN_CIPHER_SUITE_CCMP:
1586         case WLAN_CIPHER_SUITE_TKIP:
1587                 /* we support these */
1588                 break;
1589         default:
1590                 /* everything else (even CMAC for MFP) - disconnect from AP */
1591                 data->unhandled_cipher = true;
1592                 return;
1593         }
1594
1595         data->num_keys++;
1596
1597         /*
1598          * pairwise key - update sequence counters only;
1599          * note that this assumes no TDLS sessions are active
1600          */
1601         if (sta) {
1602                 struct ieee80211_key_seq seq = {};
1603                 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1604
1605                 if (data->find_phase)
1606                         return;
1607
1608                 switch (key->cipher) {
1609                 case WLAN_CIPHER_SUITE_CCMP:
1610                         iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
1611                                                sta, key);
1612                         atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
1613                         break;
1614                 case WLAN_CIPHER_SUITE_TKIP:
1615                         iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1616                         iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1617                         ieee80211_set_key_tx_seq(key, &seq);
1618                         break;
1619                 }
1620
1621                 /* that's it for this key */
1622                 return;
1623         }
1624
1625         if (data->find_phase) {
1626                 data->last_gtk = key;
1627                 data->cipher = key->cipher;
1628                 return;
1629         }
1630
1631         if (data->status->num_of_gtk_rekeys)
1632                 ieee80211_remove_key(key);
1633         else if (data->last_gtk == key)
1634                 iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
1635 }
1636
1637 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1638                                           struct ieee80211_vif *vif,
1639                                           struct iwl_wowlan_status *status)
1640 {
1641         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1642         struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1643                 .mvm = mvm,
1644                 .status = status,
1645         };
1646         u32 disconnection_reasons =
1647                 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1648                 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1649
1650         if (!status || !vif->bss_conf.bssid)
1651                 return false;
1652
1653         if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1654                 return false;
1655
1656         /* find last GTK that we used initially, if any */
1657         gtkdata.find_phase = true;
1658         ieee80211_iter_keys(mvm->hw, vif,
1659                             iwl_mvm_d3_update_keys, &gtkdata);
1660         /* not trying to keep connections with MFP/unhandled ciphers */
1661         if (gtkdata.unhandled_cipher)
1662                 return false;
1663         if (!gtkdata.num_keys)
1664                 goto out;
1665         if (!gtkdata.last_gtk)
1666                 return false;
1667
1668         /*
1669          * invalidate all other GTKs that might still exist and update
1670          * the one that we used
1671          */
1672         gtkdata.find_phase = false;
1673         ieee80211_iter_keys(mvm->hw, vif,
1674                             iwl_mvm_d3_update_keys, &gtkdata);
1675
1676         if (status->num_of_gtk_rekeys) {
1677                 struct ieee80211_key_conf *key;
1678                 struct {
1679                         struct ieee80211_key_conf conf;
1680                         u8 key[32];
1681                 } conf = {
1682                         .conf.cipher = gtkdata.cipher,
1683                         .conf.keyidx = status->gtk.key_index,
1684                 };
1685
1686                 switch (gtkdata.cipher) {
1687                 case WLAN_CIPHER_SUITE_CCMP:
1688                         conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1689                         memcpy(conf.conf.key, status->gtk.decrypt_key,
1690                                WLAN_KEY_LEN_CCMP);
1691                         break;
1692                 case WLAN_CIPHER_SUITE_TKIP:
1693                         conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1694                         memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1695                         /* leave TX MIC key zeroed, we don't use it anyway */
1696                         memcpy(conf.conf.key +
1697                                NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1698                                status->gtk.tkip_mic_key, 8);
1699                         break;
1700                 }
1701
1702                 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1703                 if (IS_ERR(key))
1704                         return false;
1705                 iwl_mvm_set_key_rx_seq(mvm, key, status);
1706         }
1707
1708         if (status->num_of_gtk_rekeys) {
1709                 __be64 replay_ctr =
1710                         cpu_to_be64(le64_to_cpu(status->replay_ctr));
1711                 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1712                                            (void *)&replay_ctr, GFP_KERNEL);
1713         }
1714
1715 out:
1716         mvmvif->seqno_valid = true;
1717         /* +0x10 because the set API expects next-to-use, not last-used */
1718         mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1719
1720         return true;
1721 }
1722
1723 static struct iwl_wowlan_status *
1724 iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1725 {
1726         u32 base = mvm->error_event_table;
1727         struct error_table_start {
1728                 /* cf. struct iwl_error_event_table */
1729                 u32 valid;
1730                 u32 error_id;
1731         } err_info;
1732         struct iwl_host_cmd cmd = {
1733                 .id = WOWLAN_GET_STATUSES,
1734                 .flags = CMD_WANT_SKB,
1735         };
1736         struct iwl_wowlan_status *status, *fw_status;
1737         int ret, len, status_size;
1738
1739         iwl_trans_read_mem_bytes(mvm->trans, base,
1740                                  &err_info, sizeof(err_info));
1741
1742         if (err_info.valid) {
1743                 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1744                          err_info.valid, err_info.error_id);
1745                 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1746                         struct cfg80211_wowlan_wakeup wakeup = {
1747                                 .rfkill_release = true,
1748                         };
1749                         ieee80211_report_wowlan_wakeup(vif, &wakeup,
1750                                                        GFP_KERNEL);
1751                 }
1752                 return ERR_PTR(-EIO);
1753         }
1754
1755         /* only for tracing for now */
1756         ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1757         if (ret)
1758                 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1759
1760         ret = iwl_mvm_send_cmd(mvm, &cmd);
1761         if (ret) {
1762                 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1763                 return ERR_PTR(ret);
1764         }
1765
1766         /* RF-kill already asserted again... */
1767         if (!cmd.resp_pkt) {
1768                 fw_status = ERR_PTR(-ERFKILL);
1769                 goto out_free_resp;
1770         }
1771
1772         status_size = sizeof(*fw_status);
1773
1774         len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1775         if (len < status_size) {
1776                 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1777                 fw_status = ERR_PTR(-EIO);
1778                 goto out_free_resp;
1779         }
1780
1781         status = (void *)cmd.resp_pkt->data;
1782         if (len != (status_size +
1783                     ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1784                 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1785                 fw_status = ERR_PTR(-EIO);
1786                 goto out_free_resp;
1787         }
1788
1789         fw_status = kmemdup(status, len, GFP_KERNEL);
1790
1791 out_free_resp:
1792         iwl_free_resp(&cmd);
1793         return fw_status;
1794 }
1795
1796 /* releases the MVM mutex */
1797 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1798                                          struct ieee80211_vif *vif)
1799 {
1800         struct iwl_wowlan_status_data status;
1801         struct iwl_wowlan_status *fw_status;
1802         int i;
1803         bool keep;
1804         struct ieee80211_sta *ap_sta;
1805         struct iwl_mvm_sta *mvm_ap_sta;
1806
1807         fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1808         if (IS_ERR_OR_NULL(fw_status))
1809                 goto out_unlock;
1810
1811         status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1812         for (i = 0; i < 8; i++)
1813                 status.qos_seq_ctr[i] =
1814                         le16_to_cpu(fw_status->qos_seq_ctr[i]);
1815         status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1816         status.wake_packet_length =
1817                 le32_to_cpu(fw_status->wake_packet_length);
1818         status.wake_packet_bufsize =
1819                 le32_to_cpu(fw_status->wake_packet_bufsize);
1820         status.wake_packet = fw_status->wake_packet;
1821
1822         /* still at hard-coded place 0 for D3 image */
1823         ap_sta = rcu_dereference_protected(
1824                         mvm->fw_id_to_mac_id[0],
1825                         lockdep_is_held(&mvm->mutex));
1826         if (IS_ERR_OR_NULL(ap_sta))
1827                 goto out_free;
1828
1829         mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
1830         for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1831                 u16 seq = status.qos_seq_ctr[i];
1832                 /* firmware stores last-used value, we store next value */
1833                 seq += 0x10;
1834                 mvm_ap_sta->tid_data[i].seq_number = seq;
1835         }
1836
1837         /* now we have all the data we need, unlock to avoid mac80211 issues */
1838         mutex_unlock(&mvm->mutex);
1839
1840         iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1841
1842         keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1843
1844         kfree(fw_status);
1845         return keep;
1846
1847 out_free:
1848         kfree(fw_status);
1849 out_unlock:
1850         mutex_unlock(&mvm->mutex);
1851         return false;
1852 }
1853
1854 void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
1855                               struct ieee80211_vif *vif,
1856                               struct iwl_wowlan_status *status)
1857 {
1858         struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1859                 .mvm = mvm,
1860                 .status = status,
1861         };
1862
1863         /*
1864          * rekey handling requires taking locks that can't be taken now.
1865          * however, d0i3 doesn't offload rekey, so we're fine.
1866          */
1867         if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
1868                 return;
1869
1870         /* find last GTK that we used initially, if any */
1871         gtkdata.find_phase = true;
1872         iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
1873
1874         gtkdata.find_phase = false;
1875         iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
1876 }
1877
1878 struct iwl_mvm_nd_query_results {
1879         u32 matched_profiles;
1880         struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1881 };
1882
1883 static int
1884 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1885                                 struct iwl_mvm_nd_query_results *results)
1886 {
1887         struct iwl_scan_offload_profiles_query *query;
1888         struct iwl_host_cmd cmd = {
1889                 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1890                 .flags = CMD_WANT_SKB,
1891         };
1892         int ret, len;
1893
1894         ret = iwl_mvm_send_cmd(mvm, &cmd);
1895         if (ret) {
1896                 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
1897                 return ret;
1898         }
1899
1900         /* RF-kill already asserted again... */
1901         if (!cmd.resp_pkt) {
1902                 ret = -ERFKILL;
1903                 goto out_free_resp;
1904         }
1905
1906         len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1907         if (len < sizeof(*query)) {
1908                 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
1909                 ret = -EIO;
1910                 goto out_free_resp;
1911         }
1912
1913         query = (void *)cmd.resp_pkt->data;
1914
1915         results->matched_profiles = le32_to_cpu(query->matched_profiles);
1916         memcpy(results->matches, query->matches, sizeof(results->matches));
1917
1918 #ifdef CONFIG_IWLWIFI_DEBUGFS
1919         mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1920 #endif
1921
1922 out_free_resp:
1923         iwl_free_resp(&cmd);
1924         return ret;
1925 }
1926
1927 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1928                                             struct ieee80211_vif *vif)
1929 {
1930         struct cfg80211_wowlan_nd_info *net_detect = NULL;
1931         struct cfg80211_wowlan_wakeup wakeup = {
1932                 .pattern_idx = -1,
1933         };
1934         struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1935         struct iwl_mvm_nd_query_results query;
1936         struct iwl_wowlan_status *fw_status;
1937         unsigned long matched_profiles;
1938         u32 reasons = 0;
1939         int i, j, n_matches, ret;
1940
1941         fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1942         if (!IS_ERR_OR_NULL(fw_status)) {
1943                 reasons = le32_to_cpu(fw_status->wakeup_reasons);
1944                 kfree(fw_status);
1945         }
1946
1947         if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1948                 wakeup.rfkill_release = true;
1949
1950         if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1951                 goto out;
1952
1953         ret = iwl_mvm_netdetect_query_results(mvm, &query);
1954         if (ret || !query.matched_profiles) {
1955                 wakeup_report = NULL;
1956                 goto out;
1957         }
1958
1959         matched_profiles = query.matched_profiles;
1960         if (mvm->n_nd_match_sets) {
1961                 n_matches = hweight_long(matched_profiles);
1962         } else {
1963                 IWL_ERR(mvm, "no net detect match information available\n");
1964                 n_matches = 0;
1965         }
1966
1967         net_detect = kzalloc(sizeof(*net_detect) +
1968                              (n_matches * sizeof(net_detect->matches[0])),
1969                              GFP_KERNEL);
1970         if (!net_detect || !n_matches)
1971                 goto out_report_nd;
1972
1973         for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
1974                 struct iwl_scan_offload_profile_match *fw_match;
1975                 struct cfg80211_wowlan_nd_match *match;
1976                 int idx, n_channels = 0;
1977
1978                 fw_match = &query.matches[i];
1979
1980                 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1981                         n_channels += hweight8(fw_match->matching_channels[j]);
1982
1983                 match = kzalloc(sizeof(*match) +
1984                                 (n_channels * sizeof(*match->channels)),
1985                                 GFP_KERNEL);
1986                 if (!match)
1987                         goto out_report_nd;
1988
1989                 net_detect->matches[net_detect->n_matches++] = match;
1990
1991                 /* We inverted the order of the SSIDs in the scan
1992                  * request, so invert the index here.
1993                  */
1994                 idx = mvm->n_nd_match_sets - i - 1;
1995                 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
1996                 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
1997                        match->ssid.ssid_len);
1998
1999                 if (mvm->n_nd_channels < n_channels)
2000                         continue;
2001
2002                 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
2003                         if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
2004                                 match->channels[match->n_channels++] =
2005                                         mvm->nd_channels[j]->center_freq;
2006         }
2007
2008 out_report_nd:
2009         wakeup.net_detect = net_detect;
2010 out:
2011         iwl_mvm_free_nd(mvm);
2012
2013         mutex_unlock(&mvm->mutex);
2014         ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2015
2016         if (net_detect) {
2017                 for (i = 0; i < net_detect->n_matches; i++)
2018                         kfree(net_detect->matches[i]);
2019                 kfree(net_detect);
2020         }
2021 }
2022
2023 static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
2024 {
2025 #ifdef CONFIG_IWLWIFI_DEBUGFS
2026         const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
2027         u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
2028         u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
2029
2030         if (!mvm->store_d3_resume_sram)
2031                 return;
2032
2033         if (!mvm->d3_resume_sram) {
2034                 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
2035                 if (!mvm->d3_resume_sram)
2036                         return;
2037         }
2038
2039         iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
2040 #endif
2041 }
2042
2043 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2044                                        struct ieee80211_vif *vif)
2045 {
2046         /* skip the one we keep connection on */
2047         if (data == vif)
2048                 return;
2049
2050         if (vif->type == NL80211_IFTYPE_STATION)
2051                 ieee80211_resume_disconnect(vif);
2052 }
2053
2054 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
2055 {
2056         struct ieee80211_vif *vif = NULL;
2057         int ret = 1;
2058         enum iwl_d3_status d3_status;
2059         bool keep = false;
2060         bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2061                                          IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2062
2063         u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE |
2064                                     CMD_WAKE_UP_TRANS;
2065
2066         mutex_lock(&mvm->mutex);
2067
2068         /* get the BSS vif pointer again */
2069         vif = iwl_mvm_get_bss_vif(mvm);
2070         if (IS_ERR_OR_NULL(vif))
2071                 goto err;
2072
2073         ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
2074         if (ret)
2075                 goto err;
2076
2077         if (d3_status != IWL_D3_STATUS_ALIVE) {
2078                 IWL_INFO(mvm, "Device was reset during suspend\n");
2079                 goto err;
2080         }
2081
2082         /* query SRAM first in case we want event logging */
2083         iwl_mvm_read_d3_sram(mvm);
2084
2085         /*
2086          * Query the current location and source from the D3 firmware so we
2087          * can play it back when we re-intiailize the D0 firmware
2088          */
2089         iwl_mvm_update_changed_regdom(mvm);
2090
2091         if (mvm->net_detect) {
2092                 iwl_mvm_query_netdetect_reasons(mvm, vif);
2093                 /* has unlocked the mutex, so skip that */
2094                 goto out;
2095         } else {
2096                 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
2097 #ifdef CONFIG_IWLWIFI_DEBUGFS
2098                 if (keep)
2099                         mvm->keep_vif = vif;
2100 #endif
2101                 /* has unlocked the mutex, so skip that */
2102                 goto out_iterate;
2103         }
2104
2105 err:
2106         iwl_mvm_free_nd(mvm);
2107         mutex_unlock(&mvm->mutex);
2108
2109 out_iterate:
2110         if (!test)
2111                 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
2112                         IEEE80211_IFACE_ITER_NORMAL,
2113                         iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
2114
2115 out:
2116         if (unified_image && !ret) {
2117                 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, flags, 0, NULL);
2118                 if (!ret) /* D3 ended successfully - no need to reset device */
2119                         return 0;
2120         }
2121
2122         /*
2123          * Reconfigure the device in one of the following cases:
2124          * 1. We are not using a unified image
2125          * 2. We are using a unified image but had an error while exiting D3
2126          */
2127         set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
2128         set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
2129         /*
2130          * When switching images we return 1, which causes mac80211
2131          * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
2132          * This type of reconfig calls iwl_mvm_restart_complete(),
2133          * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
2134          * to take the reference here.
2135          */
2136         iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
2137
2138         return 1;
2139 }
2140
2141 static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
2142 {
2143         iwl_trans_resume(mvm->trans);
2144
2145         return __iwl_mvm_resume(mvm, false);
2146 }
2147
2148 static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
2149 {
2150         bool exit_now;
2151         enum iwl_d3_status d3_status;
2152         struct iwl_trans *trans = mvm->trans;
2153
2154         iwl_trans_d3_resume(trans, &d3_status, false, false);
2155
2156         /*
2157          * make sure to clear D0I3_DEFER_WAKEUP before
2158          * calling iwl_trans_resume(), which might wait
2159          * for d0i3 exit completion.
2160          */
2161         mutex_lock(&mvm->d0i3_suspend_mutex);
2162         __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
2163         exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
2164                                         &mvm->d0i3_suspend_flags);
2165         mutex_unlock(&mvm->d0i3_suspend_mutex);
2166         if (exit_now) {
2167                 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
2168                 _iwl_mvm_exit_d0i3(mvm);
2169         }
2170
2171         iwl_trans_resume(trans);
2172
2173         if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
2174                 int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
2175
2176                 if (ret)
2177                         return ret;
2178                 /*
2179                  * d0i3 exit will be deferred until reconfig_complete.
2180                  * make sure there we are out of d0i3.
2181                  */
2182         }
2183         return 0;
2184 }
2185
2186 int iwl_mvm_resume(struct ieee80211_hw *hw)
2187 {
2188         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2189         int ret;
2190
2191         if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
2192                 ret = iwl_mvm_resume_d0i3(mvm);
2193         else
2194                 ret = iwl_mvm_resume_d3(mvm);
2195
2196         mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2197
2198         return ret;
2199 }
2200
2201 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2202 {
2203         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2204
2205         device_set_wakeup_enable(mvm->trans->dev, enabled);
2206 }
2207
2208 #ifdef CONFIG_IWLWIFI_DEBUGFS
2209 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2210 {
2211         struct iwl_mvm *mvm = inode->i_private;
2212         int err;
2213
2214         if (mvm->d3_test_active)
2215                 return -EBUSY;
2216
2217         file->private_data = inode->i_private;
2218
2219         ieee80211_stop_queues(mvm->hw);
2220         synchronize_net();
2221
2222         mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
2223
2224         /* start pseudo D3 */
2225         rtnl_lock();
2226         err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2227         rtnl_unlock();
2228         if (err > 0)
2229                 err = -EINVAL;
2230         if (err) {
2231                 ieee80211_wake_queues(mvm->hw);
2232                 return err;
2233         }
2234         mvm->d3_test_active = true;
2235         mvm->keep_vif = NULL;
2236         return 0;
2237 }
2238
2239 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2240                                     size_t count, loff_t *ppos)
2241 {
2242         struct iwl_mvm *mvm = file->private_data;
2243         u32 pme_asserted;
2244
2245         while (true) {
2246                 /* read pme_ptr if available */
2247                 if (mvm->d3_test_pme_ptr) {
2248                         pme_asserted = iwl_trans_read_mem32(mvm->trans,
2249                                                 mvm->d3_test_pme_ptr);
2250                         if (pme_asserted)
2251                                 break;
2252                 }
2253
2254                 if (msleep_interruptible(100))
2255                         break;
2256         }
2257
2258         return 0;
2259 }
2260
2261 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2262                                               struct ieee80211_vif *vif)
2263 {
2264         /* skip the one we keep connection on */
2265         if (_data == vif)
2266                 return;
2267
2268         if (vif->type == NL80211_IFTYPE_STATION)
2269                 ieee80211_connection_loss(vif);
2270 }
2271
2272 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2273 {
2274         struct iwl_mvm *mvm = inode->i_private;
2275         int remaining_time = 10;
2276
2277         mvm->d3_test_active = false;
2278
2279         rtnl_lock();
2280         __iwl_mvm_resume(mvm, true);
2281         rtnl_unlock();
2282
2283         mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2284
2285         iwl_abort_notification_waits(&mvm->notif_wait);
2286         ieee80211_restart_hw(mvm->hw);
2287
2288         /* wait for restart and disconnect all interfaces */
2289         while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2290                remaining_time > 0) {
2291                 remaining_time--;
2292                 msleep(1000);
2293         }
2294
2295         if (remaining_time == 0)
2296                 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
2297
2298         ieee80211_iterate_active_interfaces_atomic(
2299                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2300                 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
2301
2302         ieee80211_wake_queues(mvm->hw);
2303
2304         return 0;
2305 }
2306
2307 const struct file_operations iwl_dbgfs_d3_test_ops = {
2308         .llseek = no_llseek,
2309         .open = iwl_mvm_d3_test_open,
2310         .read = iwl_mvm_d3_test_read,
2311         .release = iwl_mvm_d3_test_release,
2312 };
2313 #endif