Merge tag 'scsi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb...
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / 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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63
64 #include <linux/etherdevice.h>
65 #include <linux/ip.h>
66 #include <linux/fs.h>
67 #include <net/cfg80211.h>
68 #include <net/ipv6.h>
69 #include <net/tcp.h>
70 #include <net/addrconf.h>
71 #include "iwl-modparams.h"
72 #include "fw-api.h"
73 #include "mvm.h"
74
75 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
76                             struct ieee80211_vif *vif,
77                             struct cfg80211_gtk_rekey_data *data)
78 {
79         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
80         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
81
82         if (iwlwifi_mod_params.sw_crypto)
83                 return;
84
85         mutex_lock(&mvm->mutex);
86
87         memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
88         memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
89         mvmvif->rekey_data.replay_ctr =
90                 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
91         mvmvif->rekey_data.valid = true;
92
93         mutex_unlock(&mvm->mutex);
94 }
95
96 #if IS_ENABLED(CONFIG_IPV6)
97 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
98                               struct ieee80211_vif *vif,
99                               struct inet6_dev *idev)
100 {
101         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
102         struct inet6_ifaddr *ifa;
103         int idx = 0;
104
105         read_lock_bh(&idev->lock);
106         list_for_each_entry(ifa, &idev->addr_list, if_list) {
107                 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
108                 idx++;
109                 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
110                         break;
111         }
112         read_unlock_bh(&idev->lock);
113
114         mvmvif->num_target_ipv6_addrs = idx;
115 }
116 #endif
117
118 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
119                                      struct ieee80211_vif *vif, int idx)
120 {
121         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
122
123         mvmvif->tx_key_idx = idx;
124 }
125
126 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
127 {
128         int i;
129
130         for (i = 0; i < IWL_P1K_SIZE; i++)
131                 out[i] = cpu_to_le16(p1k[i]);
132 }
133
134 struct wowlan_key_data {
135         struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
136         struct iwl_wowlan_tkip_params_cmd *tkip;
137         bool error, use_rsc_tsc, use_tkip;
138         int wep_key_idx;
139 };
140
141 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
142                                         struct ieee80211_vif *vif,
143                                         struct ieee80211_sta *sta,
144                                         struct ieee80211_key_conf *key,
145                                         void *_data)
146 {
147         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
148         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
149         struct wowlan_key_data *data = _data;
150         struct aes_sc *aes_sc, *aes_tx_sc = NULL;
151         struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
152         struct iwl_p1k_cache *rx_p1ks;
153         u8 *rx_mic_key;
154         struct ieee80211_key_seq seq;
155         u32 cur_rx_iv32 = 0;
156         u16 p1k[IWL_P1K_SIZE];
157         int ret, i;
158
159         mutex_lock(&mvm->mutex);
160
161         switch (key->cipher) {
162         case WLAN_CIPHER_SUITE_WEP40:
163         case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
164                 struct {
165                         struct iwl_mvm_wep_key_cmd wep_key_cmd;
166                         struct iwl_mvm_wep_key wep_key;
167                 } __packed wkc = {
168                         .wep_key_cmd.mac_id_n_color =
169                                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
170                                                                 mvmvif->color)),
171                         .wep_key_cmd.num_keys = 1,
172                         /* firmware sets STA_KEY_FLG_WEP_13BYTES */
173                         .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
174                         .wep_key.key_index = key->keyidx,
175                         .wep_key.key_size = key->keylen,
176                 };
177
178                 /*
179                  * This will fail -- the key functions don't set support
180                  * pairwise WEP keys. However, that's better than silently
181                  * failing WoWLAN. Or maybe not?
182                  */
183                 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
184                         break;
185
186                 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
187                 if (key->keyidx == mvmvif->tx_key_idx) {
188                         /* TX key must be at offset 0 */
189                         wkc.wep_key.key_offset = 0;
190                 } else {
191                         /* others start at 1 */
192                         data->wep_key_idx++;
193                         wkc.wep_key.key_offset = data->wep_key_idx;
194                 }
195
196                 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
197                 data->error = ret != 0;
198
199                 mvm->ptk_ivlen = key->iv_len;
200                 mvm->ptk_icvlen = key->icv_len;
201                 mvm->gtk_ivlen = key->iv_len;
202                 mvm->gtk_icvlen = key->icv_len;
203
204                 /* don't upload key again */
205                 goto out_unlock;
206         }
207         default:
208                 data->error = true;
209                 goto out_unlock;
210         case WLAN_CIPHER_SUITE_AES_CMAC:
211                 /*
212                  * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
213                  * but we also shouldn't abort suspend due to that. It does have
214                  * support for the IGTK key renewal, but doesn't really use the
215                  * IGTK for anything. This means we could spuriously wake up or
216                  * be deauthenticated, but that was considered acceptable.
217                  */
218                 goto out_unlock;
219         case WLAN_CIPHER_SUITE_TKIP:
220                 if (sta) {
221                         tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
222                         tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
223
224                         rx_p1ks = data->tkip->rx_uni;
225
226                         ieee80211_get_key_tx_seq(key, &seq);
227                         tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
228                         tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
229
230                         ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
231                         iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
232
233                         memcpy(data->tkip->mic_keys.tx,
234                                &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
235                                IWL_MIC_KEY_SIZE);
236
237                         rx_mic_key = data->tkip->mic_keys.rx_unicast;
238                 } else {
239                         tkip_sc =
240                                 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
241                         rx_p1ks = data->tkip->rx_multi;
242                         rx_mic_key = data->tkip->mic_keys.rx_mcast;
243                 }
244
245                 /*
246                  * For non-QoS this relies on the fact that both the uCode and
247                  * mac80211 use TID 0 (as they need to to avoid replay attacks)
248                  * for checking the IV in the frames.
249                  */
250                 for (i = 0; i < IWL_NUM_RSC; i++) {
251                         ieee80211_get_key_rx_seq(key, i, &seq);
252                         tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
253                         tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
254                         /* wrapping isn't allowed, AP must rekey */
255                         if (seq.tkip.iv32 > cur_rx_iv32)
256                                 cur_rx_iv32 = seq.tkip.iv32;
257                 }
258
259                 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
260                                           cur_rx_iv32, p1k);
261                 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
262                 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
263                                           cur_rx_iv32 + 1, p1k);
264                 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
265
266                 memcpy(rx_mic_key,
267                        &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
268                        IWL_MIC_KEY_SIZE);
269
270                 data->use_tkip = true;
271                 data->use_rsc_tsc = true;
272                 break;
273         case WLAN_CIPHER_SUITE_CCMP:
274                 if (sta) {
275                         u8 *pn = seq.ccmp.pn;
276
277                         aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
278                         aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
279
280                         ieee80211_get_key_tx_seq(key, &seq);
281                         aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
282                                                     ((u64)pn[4] << 8) |
283                                                     ((u64)pn[3] << 16) |
284                                                     ((u64)pn[2] << 24) |
285                                                     ((u64)pn[1] << 32) |
286                                                     ((u64)pn[0] << 40));
287                 } else {
288                         aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
289                 }
290
291                 /*
292                  * For non-QoS this relies on the fact that both the uCode and
293                  * mac80211 use TID 0 for checking the IV in the frames.
294                  */
295                 for (i = 0; i < IWL_NUM_RSC; i++) {
296                         u8 *pn = seq.ccmp.pn;
297
298                         ieee80211_get_key_rx_seq(key, i, &seq);
299                         aes_sc->pn = cpu_to_le64((u64)pn[5] |
300                                                  ((u64)pn[4] << 8) |
301                                                  ((u64)pn[3] << 16) |
302                                                  ((u64)pn[2] << 24) |
303                                                  ((u64)pn[1] << 32) |
304                                                  ((u64)pn[0] << 40));
305                 }
306                 data->use_rsc_tsc = true;
307                 break;
308         }
309
310         /*
311          * The D3 firmware hardcodes the key offset 0 as the key it uses
312          * to transmit packets to the AP, i.e. the PTK.
313          */
314         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
315                 key->hw_key_idx = 0;
316                 mvm->ptk_ivlen = key->iv_len;
317                 mvm->ptk_icvlen = key->icv_len;
318         } else {
319                 /*
320                  * firmware only supports TSC/RSC for a single key,
321                  * so if there are multiple keep overwriting them
322                  * with new ones -- this relies on mac80211 doing
323                  * list_add_tail().
324                  */
325                 key->hw_key_idx = 1;
326                 mvm->gtk_ivlen = key->iv_len;
327                 mvm->gtk_icvlen = key->icv_len;
328         }
329
330         ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
331         data->error = ret != 0;
332 out_unlock:
333         mutex_unlock(&mvm->mutex);
334 }
335
336 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
337                                  struct cfg80211_wowlan *wowlan)
338 {
339         struct iwl_wowlan_patterns_cmd *pattern_cmd;
340         struct iwl_host_cmd cmd = {
341                 .id = WOWLAN_PATTERNS,
342                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
343         };
344         int i, err;
345
346         if (!wowlan->n_patterns)
347                 return 0;
348
349         cmd.len[0] = sizeof(*pattern_cmd) +
350                 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
351
352         pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
353         if (!pattern_cmd)
354                 return -ENOMEM;
355
356         pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
357
358         for (i = 0; i < wowlan->n_patterns; i++) {
359                 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
360
361                 memcpy(&pattern_cmd->patterns[i].mask,
362                        wowlan->patterns[i].mask, mask_len);
363                 memcpy(&pattern_cmd->patterns[i].pattern,
364                        wowlan->patterns[i].pattern,
365                        wowlan->patterns[i].pattern_len);
366                 pattern_cmd->patterns[i].mask_size = mask_len;
367                 pattern_cmd->patterns[i].pattern_size =
368                         wowlan->patterns[i].pattern_len;
369         }
370
371         cmd.data[0] = pattern_cmd;
372         err = iwl_mvm_send_cmd(mvm, &cmd);
373         kfree(pattern_cmd);
374         return err;
375 }
376
377 enum iwl_mvm_tcp_packet_type {
378         MVM_TCP_TX_SYN,
379         MVM_TCP_RX_SYNACK,
380         MVM_TCP_TX_DATA,
381         MVM_TCP_RX_ACK,
382         MVM_TCP_RX_WAKE,
383         MVM_TCP_TX_FIN,
384 };
385
386 static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
387 {
388         __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
389         return cpu_to_le16(be16_to_cpu((__force __be16)check));
390 }
391
392 static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
393                                      struct cfg80211_wowlan_tcp *tcp,
394                                      void *_pkt, u8 *mask,
395                                      __le16 *pseudo_hdr_csum,
396                                      enum iwl_mvm_tcp_packet_type ptype)
397 {
398         struct {
399                 struct ethhdr eth;
400                 struct iphdr ip;
401                 struct tcphdr tcp;
402                 u8 data[];
403         } __packed *pkt = _pkt;
404         u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
405         int i;
406
407         pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
408         pkt->ip.version = 4;
409         pkt->ip.ihl = 5;
410         pkt->ip.protocol = IPPROTO_TCP;
411
412         switch (ptype) {
413         case MVM_TCP_TX_SYN:
414         case MVM_TCP_TX_DATA:
415         case MVM_TCP_TX_FIN:
416                 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
417                 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
418                 pkt->ip.ttl = 128;
419                 pkt->ip.saddr = tcp->src;
420                 pkt->ip.daddr = tcp->dst;
421                 pkt->tcp.source = cpu_to_be16(tcp->src_port);
422                 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
423                 /* overwritten for TX SYN later */
424                 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
425                 pkt->tcp.window = cpu_to_be16(65000);
426                 break;
427         case MVM_TCP_RX_SYNACK:
428         case MVM_TCP_RX_ACK:
429         case MVM_TCP_RX_WAKE:
430                 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
431                 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
432                 pkt->ip.saddr = tcp->dst;
433                 pkt->ip.daddr = tcp->src;
434                 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
435                 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
436                 break;
437         default:
438                 WARN_ON(1);
439                 return;
440         }
441
442         switch (ptype) {
443         case MVM_TCP_TX_SYN:
444                 /* firmware assumes 8 option bytes - 8 NOPs for now */
445                 memset(pkt->data, 0x01, 8);
446                 ip_tot_len += 8;
447                 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
448                 pkt->tcp.syn = 1;
449                 break;
450         case MVM_TCP_TX_DATA:
451                 ip_tot_len += tcp->payload_len;
452                 memcpy(pkt->data, tcp->payload, tcp->payload_len);
453                 pkt->tcp.psh = 1;
454                 pkt->tcp.ack = 1;
455                 break;
456         case MVM_TCP_TX_FIN:
457                 pkt->tcp.fin = 1;
458                 pkt->tcp.ack = 1;
459                 break;
460         case MVM_TCP_RX_SYNACK:
461                 pkt->tcp.syn = 1;
462                 pkt->tcp.ack = 1;
463                 break;
464         case MVM_TCP_RX_ACK:
465                 pkt->tcp.ack = 1;
466                 break;
467         case MVM_TCP_RX_WAKE:
468                 ip_tot_len += tcp->wake_len;
469                 pkt->tcp.psh = 1;
470                 pkt->tcp.ack = 1;
471                 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
472                 break;
473         }
474
475         switch (ptype) {
476         case MVM_TCP_TX_SYN:
477         case MVM_TCP_TX_DATA:
478         case MVM_TCP_TX_FIN:
479                 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
480                 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
481                 break;
482         case MVM_TCP_RX_WAKE:
483                 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
484                         u8 tmp = tcp->wake_mask[i];
485                         mask[i + 6] |= tmp << 6;
486                         if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
487                                 mask[i + 7] = tmp >> 2;
488                 }
489                 /* fall through for ethernet/IP/TCP headers mask */
490         case MVM_TCP_RX_SYNACK:
491         case MVM_TCP_RX_ACK:
492                 mask[0] = 0xff; /* match ethernet */
493                 /*
494                  * match ethernet, ip.version, ip.ihl
495                  * the ip.ihl half byte is really masked out by firmware
496                  */
497                 mask[1] = 0x7f;
498                 mask[2] = 0x80; /* match ip.protocol */
499                 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
500                 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
501                 mask[5] = 0x80; /* match tcp flags */
502                 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
503                 break;
504         };
505
506         *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
507                                             pkt->ip.saddr, pkt->ip.daddr);
508 }
509
510 static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
511                                         struct ieee80211_vif *vif,
512                                         struct cfg80211_wowlan_tcp *tcp)
513 {
514         struct iwl_wowlan_remote_wake_config *cfg;
515         struct iwl_host_cmd cmd = {
516                 .id = REMOTE_WAKE_CONFIG_CMD,
517                 .len = { sizeof(*cfg), },
518                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
519         };
520         int ret;
521
522         if (!tcp)
523                 return 0;
524
525         cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
526         if (!cfg)
527                 return -ENOMEM;
528         cmd.data[0] = cfg;
529
530         cfg->max_syn_retries = 10;
531         cfg->max_data_retries = 10;
532         cfg->tcp_syn_ack_timeout = 1; /* seconds */
533         cfg->tcp_ack_timeout = 1; /* seconds */
534
535         /* SYN (TX) */
536         iwl_mvm_build_tcp_packet(
537                 vif, tcp, cfg->syn_tx.data, NULL,
538                 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
539                 MVM_TCP_TX_SYN);
540         cfg->syn_tx.info.tcp_payload_length = 0;
541
542         /* SYN/ACK (RX) */
543         iwl_mvm_build_tcp_packet(
544                 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
545                 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
546                 MVM_TCP_RX_SYNACK);
547         cfg->synack_rx.info.tcp_payload_length = 0;
548
549         /* KEEPALIVE/ACK (TX) */
550         iwl_mvm_build_tcp_packet(
551                 vif, tcp, cfg->keepalive_tx.data, NULL,
552                 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
553                 MVM_TCP_TX_DATA);
554         cfg->keepalive_tx.info.tcp_payload_length =
555                 cpu_to_le16(tcp->payload_len);
556         cfg->sequence_number_offset = tcp->payload_seq.offset;
557         /* length must be 0..4, the field is little endian */
558         cfg->sequence_number_length = tcp->payload_seq.len;
559         cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
560         cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
561         if (tcp->payload_tok.len) {
562                 cfg->token_offset = tcp->payload_tok.offset;
563                 cfg->token_length = tcp->payload_tok.len;
564                 cfg->num_tokens =
565                         cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
566                 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
567                        tcp->tokens_size);
568         } else {
569                 /* set tokens to max value to almost never run out */
570                 cfg->num_tokens = cpu_to_le16(65535);
571         }
572
573         /* ACK (RX) */
574         iwl_mvm_build_tcp_packet(
575                 vif, tcp, cfg->keepalive_ack_rx.data,
576                 cfg->keepalive_ack_rx.rx_mask,
577                 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
578                 MVM_TCP_RX_ACK);
579         cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
580
581         /* WAKEUP (RX) */
582         iwl_mvm_build_tcp_packet(
583                 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
584                 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
585                 MVM_TCP_RX_WAKE);
586         cfg->wake_rx.info.tcp_payload_length =
587                 cpu_to_le16(tcp->wake_len);
588
589         /* FIN */
590         iwl_mvm_build_tcp_packet(
591                 vif, tcp, cfg->fin_tx.data, NULL,
592                 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
593                 MVM_TCP_TX_FIN);
594         cfg->fin_tx.info.tcp_payload_length = 0;
595
596         ret = iwl_mvm_send_cmd(mvm, &cmd);
597         kfree(cfg);
598
599         return ret;
600 }
601
602 struct iwl_d3_iter_data {
603         struct iwl_mvm *mvm;
604         struct ieee80211_vif *vif;
605         bool error;
606 };
607
608 static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
609                                       struct ieee80211_vif *vif)
610 {
611         struct iwl_d3_iter_data *data = _data;
612         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
613
614         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
615                 return;
616
617         if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
618                 return;
619
620         if (data->vif) {
621                 IWL_ERR(data->mvm, "More than one managed interface active!\n");
622                 data->error = true;
623                 return;
624         }
625
626         data->vif = vif;
627 }
628
629 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
630                                 struct ieee80211_sta *ap_sta)
631 {
632         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
633         struct ieee80211_chanctx_conf *ctx;
634         u8 chains_static, chains_dynamic;
635         struct cfg80211_chan_def chandef;
636         int ret, i;
637         struct iwl_binding_cmd binding_cmd = {};
638         struct iwl_time_quota_cmd quota_cmd = {};
639         u32 status;
640
641         /* add back the PHY */
642         if (WARN_ON(!mvmvif->phy_ctxt))
643                 return -EINVAL;
644
645         rcu_read_lock();
646         ctx = rcu_dereference(vif->chanctx_conf);
647         if (WARN_ON(!ctx)) {
648                 rcu_read_unlock();
649                 return -EINVAL;
650         }
651         chandef = ctx->def;
652         chains_static = ctx->rx_chains_static;
653         chains_dynamic = ctx->rx_chains_dynamic;
654         rcu_read_unlock();
655
656         ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
657                                    chains_static, chains_dynamic);
658         if (ret)
659                 return ret;
660
661         /* add back the MAC */
662         mvmvif->uploaded = false;
663
664         if (WARN_ON(!vif->bss_conf.assoc))
665                 return -EINVAL;
666
667         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
668         if (ret)
669                 return ret;
670
671         /* add back binding - XXX refactor? */
672         binding_cmd.id_and_color =
673                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
674                                                 mvmvif->phy_ctxt->color));
675         binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
676         binding_cmd.phy =
677                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
678                                                 mvmvif->phy_ctxt->color));
679         binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
680                                                               mvmvif->color));
681         for (i = 1; i < MAX_MACS_IN_BINDING; i++)
682                 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
683
684         status = 0;
685         ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
686                                           sizeof(binding_cmd), &binding_cmd,
687                                           &status);
688         if (ret) {
689                 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
690                 return ret;
691         }
692
693         if (status) {
694                 IWL_ERR(mvm, "Binding command failed: %u\n", status);
695                 return -EIO;
696         }
697
698         ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
699         if (ret)
700                 return ret;
701         rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
702
703         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false);
704         if (ret)
705                 return ret;
706
707         /* and some quota */
708         quota_cmd.quotas[0].id_and_color =
709                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
710                                                 mvmvif->phy_ctxt->color));
711         quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
712         quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
713
714         for (i = 1; i < MAX_BINDINGS; i++)
715                 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
716
717         ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
718                                    sizeof(quota_cmd), &quota_cmd);
719         if (ret)
720                 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
721
722         return 0;
723 }
724
725 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
726                                        struct ieee80211_vif *vif)
727 {
728         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
729         struct iwl_nonqos_seq_query_cmd query_cmd = {
730                 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
731                 .mac_id_n_color =
732                         cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
733                                                         mvmvif->color)),
734         };
735         struct iwl_host_cmd cmd = {
736                 .id = NON_QOS_TX_COUNTER_CMD,
737                 .flags = CMD_WANT_SKB,
738         };
739         int err;
740         u32 size;
741
742         cmd.data[0] = &query_cmd;
743         cmd.len[0] = sizeof(query_cmd);
744
745         err = iwl_mvm_send_cmd(mvm, &cmd);
746         if (err)
747                 return err;
748
749         size = iwl_rx_packet_payload_len(cmd.resp_pkt);
750         if (size < sizeof(__le16)) {
751                 err = -EINVAL;
752         } else {
753                 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
754                 /* firmware returns next, not last-used seqno */
755                 err = (u16) (err - 0x10);
756         }
757
758         iwl_free_resp(&cmd);
759         return err;
760 }
761
762 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
763 {
764         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
765         struct iwl_nonqos_seq_query_cmd query_cmd = {
766                 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
767                 .mac_id_n_color =
768                         cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
769                                                         mvmvif->color)),
770                 .value = cpu_to_le16(mvmvif->seqno),
771         };
772
773         /* return if called during restart, not resume from D3 */
774         if (!mvmvif->seqno_valid)
775                 return;
776
777         mvmvif->seqno_valid = false;
778
779         if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
780                                  sizeof(query_cmd), &query_cmd))
781                 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
782 }
783
784 static int
785 iwl_mvm_send_wowlan_config_cmd(struct iwl_mvm *mvm,
786                                const struct iwl_wowlan_config_cmd_v3 *cmd)
787 {
788         /* start only with the v2 part of the command */
789         u16 cmd_len = sizeof(cmd->common);
790
791         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_WOWLAN_CONFIG_TID)
792                 cmd_len = sizeof(*cmd);
793
794         return iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
795                                     cmd_len, cmd);
796 }
797
798 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
799                              struct cfg80211_wowlan *wowlan,
800                              bool test)
801 {
802         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
803         struct iwl_d3_iter_data suspend_iter_data = {
804                 .mvm = mvm,
805         };
806         struct ieee80211_vif *vif;
807         struct iwl_mvm_vif *mvmvif;
808         struct ieee80211_sta *ap_sta;
809         struct iwl_mvm_sta *mvm_ap_sta;
810         struct iwl_wowlan_config_cmd_v3 wowlan_config_cmd = {};
811         struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
812         struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
813         struct iwl_d3_manager_config d3_cfg_cmd_data = {
814                 /*
815                  * Program the minimum sleep time to 10 seconds, as many
816                  * platforms have issues processing a wakeup signal while
817                  * still being in the process of suspending.
818                  */
819                 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
820         };
821         struct iwl_host_cmd d3_cfg_cmd = {
822                 .id = D3_CONFIG_CMD,
823                 .flags = CMD_WANT_SKB,
824                 .data[0] = &d3_cfg_cmd_data,
825                 .len[0] = sizeof(d3_cfg_cmd_data),
826         };
827         struct wowlan_key_data key_data = {
828                 .use_rsc_tsc = false,
829                 .tkip = &tkip_cmd,
830                 .use_tkip = false,
831         };
832         int ret;
833         int len __maybe_unused;
834
835         if (!wowlan) {
836                 /*
837                  * mac80211 shouldn't get here, but for D3 test
838                  * it doesn't warrant a warning
839                  */
840                 WARN_ON(!test);
841                 return -EINVAL;
842         }
843
844         key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
845         if (!key_data.rsc_tsc)
846                 return -ENOMEM;
847
848         mutex_lock(&mvm->mutex);
849
850         /* see if there's only a single BSS vif and it's associated */
851         ieee80211_iterate_active_interfaces_atomic(
852                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
853                 iwl_mvm_d3_iface_iterator, &suspend_iter_data);
854
855         if (suspend_iter_data.error || !suspend_iter_data.vif) {
856                 ret = 1;
857                 goto out_noreset;
858         }
859
860         vif = suspend_iter_data.vif;
861         mvmvif = iwl_mvm_vif_from_mac80211(vif);
862
863         ap_sta = rcu_dereference_protected(
864                         mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
865                         lockdep_is_held(&mvm->mutex));
866         if (IS_ERR_OR_NULL(ap_sta)) {
867                 ret = -EINVAL;
868                 goto out_noreset;
869         }
870
871         mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
872
873         /* TODO: wowlan_config_cmd.common.wowlan_ba_teardown_tids */
874
875         wowlan_config_cmd.common.is_11n_connection =
876                                         ap_sta->ht_cap.ht_supported;
877
878         /* Query the last used seqno and set it */
879         ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
880         if (ret < 0)
881                 goto out_noreset;
882         wowlan_config_cmd.common.non_qos_seq = cpu_to_le16(ret);
883
884         iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, &wowlan_config_cmd.common);
885
886         if (wowlan->disconnect)
887                 wowlan_config_cmd.common.wakeup_filter |=
888                         cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
889                                     IWL_WOWLAN_WAKEUP_LINK_CHANGE);
890         if (wowlan->magic_pkt)
891                 wowlan_config_cmd.common.wakeup_filter |=
892                         cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
893         if (wowlan->gtk_rekey_failure)
894                 wowlan_config_cmd.common.wakeup_filter |=
895                         cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
896         if (wowlan->eap_identity_req)
897                 wowlan_config_cmd.common.wakeup_filter |=
898                         cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
899         if (wowlan->four_way_handshake)
900                 wowlan_config_cmd.common.wakeup_filter |=
901                         cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
902         if (wowlan->n_patterns)
903                 wowlan_config_cmd.common.wakeup_filter |=
904                         cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
905
906         if (wowlan->rfkill_release)
907                 wowlan_config_cmd.common.wakeup_filter |=
908                         cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
909
910         if (wowlan->tcp) {
911                 /*
912                  * Set the "link change" (really "link lost") flag as well
913                  * since that implies losing the TCP connection.
914                  */
915                 wowlan_config_cmd.common.wakeup_filter |=
916                         cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
917                                     IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
918                                     IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
919                                     IWL_WOWLAN_WAKEUP_LINK_CHANGE);
920         }
921
922         iwl_mvm_cancel_scan(mvm);
923
924         iwl_trans_stop_device(mvm->trans);
925
926         /*
927          * Set the HW restart bit -- this is mostly true as we're
928          * going to load new firmware and reprogram that, though
929          * the reprogramming is going to be manual to avoid adding
930          * all the MACs that aren't support.
931          * We don't have to clear up everything though because the
932          * reprogramming is manual. When we resume, we'll actually
933          * go through a proper restart sequence again to switch
934          * back to the runtime firmware image.
935          */
936         set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
937
938         /* We reprogram keys and shouldn't allocate new key indices */
939         memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
940
941         mvm->ptk_ivlen = 0;
942         mvm->ptk_icvlen = 0;
943         mvm->ptk_ivlen = 0;
944         mvm->ptk_icvlen = 0;
945
946         ret = iwl_mvm_load_d3_fw(mvm);
947         if (ret)
948                 goto out;
949
950         ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
951         if (ret)
952                 goto out;
953
954         if (!iwlwifi_mod_params.sw_crypto) {
955                 /*
956                  * This needs to be unlocked due to lock ordering
957                  * constraints. Since we're in the suspend path
958                  * that isn't really a problem though.
959                  */
960                 mutex_unlock(&mvm->mutex);
961                 ieee80211_iter_keys(mvm->hw, vif,
962                                     iwl_mvm_wowlan_program_keys,
963                                     &key_data);
964                 mutex_lock(&mvm->mutex);
965                 if (key_data.error) {
966                         ret = -EIO;
967                         goto out;
968                 }
969
970                 if (key_data.use_rsc_tsc) {
971                         struct iwl_host_cmd rsc_tsc_cmd = {
972                                 .id = WOWLAN_TSC_RSC_PARAM,
973                                 .data[0] = key_data.rsc_tsc,
974                                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
975                                 .len[0] = sizeof(*key_data.rsc_tsc),
976                         };
977
978                         ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
979                         if (ret)
980                                 goto out;
981                 }
982
983                 if (key_data.use_tkip) {
984                         ret = iwl_mvm_send_cmd_pdu(mvm,
985                                                    WOWLAN_TKIP_PARAM,
986                                                    0, sizeof(tkip_cmd),
987                                                    &tkip_cmd);
988                         if (ret)
989                                 goto out;
990                 }
991
992                 if (mvmvif->rekey_data.valid) {
993                         memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
994                         memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
995                                NL80211_KCK_LEN);
996                         kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
997                         memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
998                                NL80211_KEK_LEN);
999                         kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1000                         kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1001
1002                         ret = iwl_mvm_send_cmd_pdu(mvm,
1003                                                    WOWLAN_KEK_KCK_MATERIAL, 0,
1004                                                    sizeof(kek_kck_cmd),
1005                                                    &kek_kck_cmd);
1006                         if (ret)
1007                                 goto out;
1008                 }
1009         }
1010
1011         ret = iwl_mvm_send_wowlan_config_cmd(mvm, &wowlan_config_cmd);
1012         if (ret)
1013                 goto out;
1014
1015         ret = iwl_mvm_send_patterns(mvm, wowlan);
1016         if (ret)
1017                 goto out;
1018
1019         ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
1020         if (ret)
1021                 goto out;
1022
1023         ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1024         if (ret)
1025                 goto out;
1026
1027         ret = iwl_mvm_power_update_device(mvm);
1028         if (ret)
1029                 goto out;
1030
1031         ret = iwl_mvm_power_update_mac(mvm);
1032         if (ret)
1033                 goto out;
1034
1035 #ifdef CONFIG_IWLWIFI_DEBUGFS
1036         if (mvm->d3_wake_sysassert)
1037                 d3_cfg_cmd_data.wakeup_flags |=
1038                         cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1039 #endif
1040
1041         /* must be last -- this switches firmware state */
1042         ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1043         if (ret)
1044                 goto out;
1045 #ifdef CONFIG_IWLWIFI_DEBUGFS
1046         len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1047         if (len >= sizeof(u32)) {
1048                 mvm->d3_test_pme_ptr =
1049                         le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1050         }
1051 #endif
1052         iwl_free_resp(&d3_cfg_cmd);
1053
1054         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1055
1056         iwl_trans_d3_suspend(mvm->trans, test);
1057  out:
1058         if (ret < 0)
1059                 ieee80211_restart_hw(mvm->hw);
1060  out_noreset:
1061         kfree(key_data.rsc_tsc);
1062
1063         mutex_unlock(&mvm->mutex);
1064
1065         return ret;
1066 }
1067
1068 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1069 {
1070         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1071
1072         if (iwl_mvm_is_d0i3_supported(mvm)) {
1073                 mutex_lock(&mvm->d0i3_suspend_mutex);
1074                 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1075                 mutex_unlock(&mvm->d0i3_suspend_mutex);
1076                 return 0;
1077         }
1078
1079         return __iwl_mvm_suspend(hw, wowlan, false);
1080 }
1081
1082 /* converted data from the different status responses */
1083 struct iwl_wowlan_status_data {
1084         u16 pattern_number;
1085         u16 qos_seq_ctr[8];
1086         u32 wakeup_reasons;
1087         u32 wake_packet_length;
1088         u32 wake_packet_bufsize;
1089         const u8 *wake_packet;
1090 };
1091
1092 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1093                                           struct ieee80211_vif *vif,
1094                                           struct iwl_wowlan_status_data *status)
1095 {
1096         struct sk_buff *pkt = NULL;
1097         struct cfg80211_wowlan_wakeup wakeup = {
1098                 .pattern_idx = -1,
1099         };
1100         struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1101         u32 reasons = status->wakeup_reasons;
1102
1103         if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1104                 wakeup_report = NULL;
1105                 goto report;
1106         }
1107
1108         if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1109                 wakeup.magic_pkt = true;
1110
1111         if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1112                 wakeup.pattern_idx =
1113                         status->pattern_number;
1114
1115         if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1116                        IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1117                 wakeup.disconnect = true;
1118
1119         if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1120                 wakeup.gtk_rekey_failure = true;
1121
1122         if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1123                 wakeup.rfkill_release = true;
1124
1125         if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1126                 wakeup.eap_identity_req = true;
1127
1128         if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1129                 wakeup.four_way_handshake = true;
1130
1131         if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1132                 wakeup.tcp_connlost = true;
1133
1134         if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1135                 wakeup.tcp_nomoretokens = true;
1136
1137         if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1138                 wakeup.tcp_match = true;
1139
1140         if (status->wake_packet_bufsize) {
1141                 int pktsize = status->wake_packet_bufsize;
1142                 int pktlen = status->wake_packet_length;
1143                 const u8 *pktdata = status->wake_packet;
1144                 struct ieee80211_hdr *hdr = (void *)pktdata;
1145                 int truncated = pktlen - pktsize;
1146
1147                 /* this would be a firmware bug */
1148                 if (WARN_ON_ONCE(truncated < 0))
1149                         truncated = 0;
1150
1151                 if (ieee80211_is_data(hdr->frame_control)) {
1152                         int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1153                         int ivlen = 0, icvlen = 4; /* also FCS */
1154
1155                         pkt = alloc_skb(pktsize, GFP_KERNEL);
1156                         if (!pkt)
1157                                 goto report;
1158
1159                         memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1160                         pktdata += hdrlen;
1161                         pktsize -= hdrlen;
1162
1163                         if (ieee80211_has_protected(hdr->frame_control)) {
1164                                 /*
1165                                  * This is unlocked and using gtk_i(c)vlen,
1166                                  * but since everything is under RTNL still
1167                                  * that's not really a problem - changing
1168                                  * it would be difficult.
1169                                  */
1170                                 if (is_multicast_ether_addr(hdr->addr1)) {
1171                                         ivlen = mvm->gtk_ivlen;
1172                                         icvlen += mvm->gtk_icvlen;
1173                                 } else {
1174                                         ivlen = mvm->ptk_ivlen;
1175                                         icvlen += mvm->ptk_icvlen;
1176                                 }
1177                         }
1178
1179                         /* if truncated, FCS/ICV is (partially) gone */
1180                         if (truncated >= icvlen) {
1181                                 icvlen = 0;
1182                                 truncated -= icvlen;
1183                         } else {
1184                                 icvlen -= truncated;
1185                                 truncated = 0;
1186                         }
1187
1188                         pktsize -= ivlen + icvlen;
1189                         pktdata += ivlen;
1190
1191                         memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1192
1193                         if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1194                                 goto report;
1195                         wakeup.packet = pkt->data;
1196                         wakeup.packet_present_len = pkt->len;
1197                         wakeup.packet_len = pkt->len - truncated;
1198                         wakeup.packet_80211 = false;
1199                 } else {
1200                         int fcslen = 4;
1201
1202                         if (truncated >= 4) {
1203                                 truncated -= 4;
1204                                 fcslen = 0;
1205                         } else {
1206                                 fcslen -= truncated;
1207                                 truncated = 0;
1208                         }
1209                         pktsize -= fcslen;
1210                         wakeup.packet = status->wake_packet;
1211                         wakeup.packet_present_len = pktsize;
1212                         wakeup.packet_len = pktlen - truncated;
1213                         wakeup.packet_80211 = true;
1214                 }
1215         }
1216
1217  report:
1218         ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1219         kfree_skb(pkt);
1220 }
1221
1222 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1223                                   struct ieee80211_key_seq *seq)
1224 {
1225         u64 pn;
1226
1227         pn = le64_to_cpu(sc->pn);
1228         seq->ccmp.pn[0] = pn >> 40;
1229         seq->ccmp.pn[1] = pn >> 32;
1230         seq->ccmp.pn[2] = pn >> 24;
1231         seq->ccmp.pn[3] = pn >> 16;
1232         seq->ccmp.pn[4] = pn >> 8;
1233         seq->ccmp.pn[5] = pn;
1234 }
1235
1236 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1237                                    struct ieee80211_key_seq *seq)
1238 {
1239         seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1240         seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1241 }
1242
1243 static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1244                                    struct ieee80211_key_conf *key)
1245 {
1246         int tid;
1247
1248         BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1249
1250         for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1251                 struct ieee80211_key_seq seq = {};
1252
1253                 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1254                 ieee80211_set_key_rx_seq(key, tid, &seq);
1255         }
1256 }
1257
1258 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1259                                     struct ieee80211_key_conf *key)
1260 {
1261         int tid;
1262
1263         BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1264
1265         for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1266                 struct ieee80211_key_seq seq = {};
1267
1268                 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1269                 ieee80211_set_key_rx_seq(key, tid, &seq);
1270         }
1271 }
1272
1273 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1274                                    struct iwl_wowlan_status *status)
1275 {
1276         union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1277
1278         switch (key->cipher) {
1279         case WLAN_CIPHER_SUITE_CCMP:
1280                 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1281                 break;
1282         case WLAN_CIPHER_SUITE_TKIP:
1283                 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1284                 break;
1285         default:
1286                 WARN_ON(1);
1287         }
1288 }
1289
1290 struct iwl_mvm_d3_gtk_iter_data {
1291         struct iwl_wowlan_status *status;
1292         void *last_gtk;
1293         u32 cipher;
1294         bool find_phase, unhandled_cipher;
1295         int num_keys;
1296 };
1297
1298 static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
1299                                    struct ieee80211_vif *vif,
1300                                    struct ieee80211_sta *sta,
1301                                    struct ieee80211_key_conf *key,
1302                                    void *_data)
1303 {
1304         struct iwl_mvm_d3_gtk_iter_data *data = _data;
1305
1306         if (data->unhandled_cipher)
1307                 return;
1308
1309         switch (key->cipher) {
1310         case WLAN_CIPHER_SUITE_WEP40:
1311         case WLAN_CIPHER_SUITE_WEP104:
1312                 /* ignore WEP completely, nothing to do */
1313                 return;
1314         case WLAN_CIPHER_SUITE_CCMP:
1315         case WLAN_CIPHER_SUITE_TKIP:
1316                 /* we support these */
1317                 break;
1318         default:
1319                 /* everything else (even CMAC for MFP) - disconnect from AP */
1320                 data->unhandled_cipher = true;
1321                 return;
1322         }
1323
1324         data->num_keys++;
1325
1326         /*
1327          * pairwise key - update sequence counters only;
1328          * note that this assumes no TDLS sessions are active
1329          */
1330         if (sta) {
1331                 struct ieee80211_key_seq seq = {};
1332                 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1333
1334                 if (data->find_phase)
1335                         return;
1336
1337                 switch (key->cipher) {
1338                 case WLAN_CIPHER_SUITE_CCMP:
1339                         iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
1340                         iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
1341                         break;
1342                 case WLAN_CIPHER_SUITE_TKIP:
1343                         iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1344                         iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1345                         break;
1346                 }
1347                 ieee80211_set_key_tx_seq(key, &seq);
1348
1349                 /* that's it for this key */
1350                 return;
1351         }
1352
1353         if (data->find_phase) {
1354                 data->last_gtk = key;
1355                 data->cipher = key->cipher;
1356                 return;
1357         }
1358
1359         if (data->status->num_of_gtk_rekeys)
1360                 ieee80211_remove_key(key);
1361         else if (data->last_gtk == key)
1362                 iwl_mvm_set_key_rx_seq(key, data->status);
1363 }
1364
1365 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1366                                           struct ieee80211_vif *vif,
1367                                           struct iwl_wowlan_status *status)
1368 {
1369         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1370         struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1371                 .status = status,
1372         };
1373         u32 disconnection_reasons =
1374                 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1375                 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1376
1377         if (!status || !vif->bss_conf.bssid)
1378                 return false;
1379
1380         if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1381                 return false;
1382
1383         /* find last GTK that we used initially, if any */
1384         gtkdata.find_phase = true;
1385         ieee80211_iter_keys(mvm->hw, vif,
1386                             iwl_mvm_d3_update_gtks, &gtkdata);
1387         /* not trying to keep connections with MFP/unhandled ciphers */
1388         if (gtkdata.unhandled_cipher)
1389                 return false;
1390         if (!gtkdata.num_keys)
1391                 goto out;
1392         if (!gtkdata.last_gtk)
1393                 return false;
1394
1395         /*
1396          * invalidate all other GTKs that might still exist and update
1397          * the one that we used
1398          */
1399         gtkdata.find_phase = false;
1400         ieee80211_iter_keys(mvm->hw, vif,
1401                             iwl_mvm_d3_update_gtks, &gtkdata);
1402
1403         if (status->num_of_gtk_rekeys) {
1404                 struct ieee80211_key_conf *key;
1405                 struct {
1406                         struct ieee80211_key_conf conf;
1407                         u8 key[32];
1408                 } conf = {
1409                         .conf.cipher = gtkdata.cipher,
1410                         .conf.keyidx = status->gtk.key_index,
1411                 };
1412
1413                 switch (gtkdata.cipher) {
1414                 case WLAN_CIPHER_SUITE_CCMP:
1415                         conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1416                         memcpy(conf.conf.key, status->gtk.decrypt_key,
1417                                WLAN_KEY_LEN_CCMP);
1418                         break;
1419                 case WLAN_CIPHER_SUITE_TKIP:
1420                         conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1421                         memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1422                         /* leave TX MIC key zeroed, we don't use it anyway */
1423                         memcpy(conf.conf.key +
1424                                NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1425                                status->gtk.tkip_mic_key, 8);
1426                         break;
1427                 }
1428
1429                 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1430                 if (IS_ERR(key))
1431                         return false;
1432                 iwl_mvm_set_key_rx_seq(key, status);
1433         }
1434
1435         if (status->num_of_gtk_rekeys) {
1436                 __be64 replay_ctr =
1437                         cpu_to_be64(le64_to_cpu(status->replay_ctr));
1438                 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1439                                            (void *)&replay_ctr, GFP_KERNEL);
1440         }
1441
1442 out:
1443         mvmvif->seqno_valid = true;
1444         /* +0x10 because the set API expects next-to-use, not last-used */
1445         mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1446
1447         return true;
1448 }
1449
1450 /* releases the MVM mutex */
1451 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1452                                          struct ieee80211_vif *vif)
1453 {
1454         u32 base = mvm->error_event_table;
1455         struct error_table_start {
1456                 /* cf. struct iwl_error_event_table */
1457                 u32 valid;
1458                 u32 error_id;
1459         } err_info;
1460         struct iwl_host_cmd cmd = {
1461                 .id = WOWLAN_GET_STATUSES,
1462                 .flags = CMD_WANT_SKB,
1463         };
1464         struct iwl_wowlan_status_data status;
1465         struct iwl_wowlan_status *fw_status;
1466         int ret, len, status_size, i;
1467         bool keep;
1468         struct ieee80211_sta *ap_sta;
1469         struct iwl_mvm_sta *mvm_ap_sta;
1470
1471         iwl_trans_read_mem_bytes(mvm->trans, base,
1472                                  &err_info, sizeof(err_info));
1473
1474         if (err_info.valid) {
1475                 IWL_INFO(mvm, "error table is valid (%d)\n",
1476                          err_info.valid);
1477                 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1478                         struct cfg80211_wowlan_wakeup wakeup = {
1479                                 .rfkill_release = true,
1480                         };
1481                         ieee80211_report_wowlan_wakeup(vif, &wakeup,
1482                                                        GFP_KERNEL);
1483                 }
1484                 goto out_unlock;
1485         }
1486
1487         /* only for tracing for now */
1488         ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1489         if (ret)
1490                 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1491
1492         ret = iwl_mvm_send_cmd(mvm, &cmd);
1493         if (ret) {
1494                 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1495                 goto out_unlock;
1496         }
1497
1498         /* RF-kill already asserted again... */
1499         if (!cmd.resp_pkt)
1500                 goto out_unlock;
1501
1502         status_size = sizeof(*fw_status);
1503
1504         len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1505         if (len < status_size) {
1506                 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1507                 goto out_free_resp;
1508         }
1509
1510         fw_status = (void *)cmd.resp_pkt->data;
1511
1512         status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1513         for (i = 0; i < 8; i++)
1514                 status.qos_seq_ctr[i] =
1515                         le16_to_cpu(fw_status->qos_seq_ctr[i]);
1516         status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1517         status.wake_packet_length =
1518                 le32_to_cpu(fw_status->wake_packet_length);
1519         status.wake_packet_bufsize =
1520                 le32_to_cpu(fw_status->wake_packet_bufsize);
1521         status.wake_packet = fw_status->wake_packet;
1522
1523         if (len != status_size + ALIGN(status.wake_packet_bufsize, 4)) {
1524                 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1525                 goto out_free_resp;
1526         }
1527
1528         /* still at hard-coded place 0 for D3 image */
1529         ap_sta = rcu_dereference_protected(
1530                         mvm->fw_id_to_mac_id[0],
1531                         lockdep_is_held(&mvm->mutex));
1532         if (IS_ERR_OR_NULL(ap_sta))
1533                 goto out_free_resp;
1534
1535         mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
1536         for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1537                 u16 seq = status.qos_seq_ctr[i];
1538                 /* firmware stores last-used value, we store next value */
1539                 seq += 0x10;
1540                 mvm_ap_sta->tid_data[i].seq_number = seq;
1541         }
1542
1543         /* now we have all the data we need, unlock to avoid mac80211 issues */
1544         mutex_unlock(&mvm->mutex);
1545
1546         iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1547
1548         keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1549
1550         iwl_free_resp(&cmd);
1551         return keep;
1552
1553  out_free_resp:
1554         iwl_free_resp(&cmd);
1555  out_unlock:
1556         mutex_unlock(&mvm->mutex);
1557         return false;
1558 }
1559
1560 static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1561 {
1562 #ifdef CONFIG_IWLWIFI_DEBUGFS
1563         const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1564         u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1565         u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1566
1567         if (!mvm->store_d3_resume_sram)
1568                 return;
1569
1570         if (!mvm->d3_resume_sram) {
1571                 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1572                 if (!mvm->d3_resume_sram)
1573                         return;
1574         }
1575
1576         iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1577 #endif
1578 }
1579
1580 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1581                                        struct ieee80211_vif *vif)
1582 {
1583         /* skip the one we keep connection on */
1584         if (data == vif)
1585                 return;
1586
1587         if (vif->type == NL80211_IFTYPE_STATION)
1588                 ieee80211_resume_disconnect(vif);
1589 }
1590
1591 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
1592 {
1593         struct iwl_d3_iter_data resume_iter_data = {
1594                 .mvm = mvm,
1595         };
1596         struct ieee80211_vif *vif = NULL;
1597         int ret;
1598         enum iwl_d3_status d3_status;
1599         bool keep = false;
1600
1601         mutex_lock(&mvm->mutex);
1602
1603         /* get the BSS vif pointer again */
1604         ieee80211_iterate_active_interfaces_atomic(
1605                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1606                 iwl_mvm_d3_iface_iterator, &resume_iter_data);
1607
1608         if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
1609                 goto out_unlock;
1610
1611         vif = resume_iter_data.vif;
1612
1613         ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
1614         if (ret)
1615                 goto out_unlock;
1616
1617         if (d3_status != IWL_D3_STATUS_ALIVE) {
1618                 IWL_INFO(mvm, "Device was reset during suspend\n");
1619                 goto out_unlock;
1620         }
1621
1622         /* query SRAM first in case we want event logging */
1623         iwl_mvm_read_d3_sram(mvm);
1624
1625         keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
1626 #ifdef CONFIG_IWLWIFI_DEBUGFS
1627         if (keep)
1628                 mvm->keep_vif = vif;
1629 #endif
1630         /* has unlocked the mutex, so skip that */
1631         goto out;
1632
1633  out_unlock:
1634         mutex_unlock(&mvm->mutex);
1635
1636  out:
1637         if (!test)
1638                 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1639                         IEEE80211_IFACE_ITER_NORMAL,
1640                         iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
1641
1642         /* return 1 to reconfigure the device */
1643         set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1644         return 1;
1645 }
1646
1647 int iwl_mvm_resume(struct ieee80211_hw *hw)
1648 {
1649         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1650
1651         if (iwl_mvm_is_d0i3_supported(mvm)) {
1652                 bool exit_now;
1653
1654                 mutex_lock(&mvm->d0i3_suspend_mutex);
1655                 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1656                 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1657                                                 &mvm->d0i3_suspend_flags);
1658                 mutex_unlock(&mvm->d0i3_suspend_mutex);
1659                 if (exit_now)
1660                         _iwl_mvm_exit_d0i3(mvm);
1661                 return 0;
1662         }
1663
1664         return __iwl_mvm_resume(mvm, false);
1665 }
1666
1667 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1668 {
1669         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1670
1671         device_set_wakeup_enable(mvm->trans->dev, enabled);
1672 }
1673
1674 #ifdef CONFIG_IWLWIFI_DEBUGFS
1675 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
1676 {
1677         struct iwl_mvm *mvm = inode->i_private;
1678         int err;
1679
1680         if (mvm->d3_test_active)
1681                 return -EBUSY;
1682
1683         file->private_data = inode->i_private;
1684
1685         ieee80211_stop_queues(mvm->hw);
1686         synchronize_net();
1687
1688         /* start pseudo D3 */
1689         rtnl_lock();
1690         err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
1691         rtnl_unlock();
1692         if (err > 0)
1693                 err = -EINVAL;
1694         if (err) {
1695                 ieee80211_wake_queues(mvm->hw);
1696                 return err;
1697         }
1698         mvm->d3_test_active = true;
1699         mvm->keep_vif = NULL;
1700         return 0;
1701 }
1702
1703 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
1704                                     size_t count, loff_t *ppos)
1705 {
1706         struct iwl_mvm *mvm = file->private_data;
1707         u32 pme_asserted;
1708
1709         while (true) {
1710                 /* read pme_ptr if available */
1711                 if (mvm->d3_test_pme_ptr) {
1712                         pme_asserted = iwl_trans_read_mem32(mvm->trans,
1713                                                 mvm->d3_test_pme_ptr);
1714                         if (pme_asserted)
1715                                 break;
1716                 }
1717
1718                 if (msleep_interruptible(100))
1719                         break;
1720         }
1721
1722         return 0;
1723 }
1724
1725 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
1726                                               struct ieee80211_vif *vif)
1727 {
1728         /* skip the one we keep connection on */
1729         if (_data == vif)
1730                 return;
1731
1732         if (vif->type == NL80211_IFTYPE_STATION)
1733                 ieee80211_connection_loss(vif);
1734 }
1735
1736 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
1737 {
1738         struct iwl_mvm *mvm = inode->i_private;
1739         int remaining_time = 10;
1740
1741         mvm->d3_test_active = false;
1742         __iwl_mvm_resume(mvm, true);
1743         iwl_abort_notification_waits(&mvm->notif_wait);
1744         ieee80211_restart_hw(mvm->hw);
1745
1746         /* wait for restart and disconnect all interfaces */
1747         while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1748                remaining_time > 0) {
1749                 remaining_time--;
1750                 msleep(1000);
1751         }
1752
1753         if (remaining_time == 0)
1754                 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
1755
1756         ieee80211_iterate_active_interfaces_atomic(
1757                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1758                 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
1759
1760         ieee80211_wake_queues(mvm->hw);
1761
1762         return 0;
1763 }
1764
1765 const struct file_operations iwl_dbgfs_d3_test_ops = {
1766         .llseek = no_llseek,
1767         .open = iwl_mvm_d3_test_open,
1768         .read = iwl_mvm_d3_test_read,
1769         .release = iwl_mvm_d3_test_release,
1770 };
1771 #endif