Linux 3.9-rc8
[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 - 2013 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 LICENSE.GPL.
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 - 2013 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 <net/cfg80211.h>
66 #include <net/ipv6.h>
67 #include "iwl-modparams.h"
68 #include "fw-api.h"
69 #include "mvm.h"
70
71 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
72                             struct ieee80211_vif *vif,
73                             struct cfg80211_gtk_rekey_data *data)
74 {
75         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
76         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
77
78         if (iwlwifi_mod_params.sw_crypto)
79                 return;
80
81         mutex_lock(&mvm->mutex);
82
83         memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
84         memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
85         mvmvif->rekey_data.replay_ctr =
86                 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
87         mvmvif->rekey_data.valid = true;
88
89         mutex_unlock(&mvm->mutex);
90 }
91
92 #if IS_ENABLED(CONFIG_IPV6)
93 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
94                               struct ieee80211_vif *vif,
95                               struct inet6_dev *idev)
96 {
97         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
98         struct inet6_ifaddr *ifa;
99         int idx = 0;
100
101         read_lock_bh(&idev->lock);
102         list_for_each_entry(ifa, &idev->addr_list, if_list) {
103                 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
104                 idx++;
105                 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS)
106                         break;
107         }
108         read_unlock_bh(&idev->lock);
109
110         mvmvif->num_target_ipv6_addrs = idx;
111 }
112 #endif
113
114 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
115                                      struct ieee80211_vif *vif, int idx)
116 {
117         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
118
119         mvmvif->tx_key_idx = idx;
120 }
121
122 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
123 {
124         int i;
125
126         for (i = 0; i < IWL_P1K_SIZE; i++)
127                 out[i] = cpu_to_le16(p1k[i]);
128 }
129
130 struct wowlan_key_data {
131         struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
132         struct iwl_wowlan_tkip_params_cmd *tkip;
133         bool error, use_rsc_tsc, use_tkip;
134         int gtk_key_idx;
135 };
136
137 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
138                                         struct ieee80211_vif *vif,
139                                         struct ieee80211_sta *sta,
140                                         struct ieee80211_key_conf *key,
141                                         void *_data)
142 {
143         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
144         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
145         struct wowlan_key_data *data = _data;
146         struct aes_sc *aes_sc, *aes_tx_sc = NULL;
147         struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
148         struct iwl_p1k_cache *rx_p1ks;
149         u8 *rx_mic_key;
150         struct ieee80211_key_seq seq;
151         u32 cur_rx_iv32 = 0;
152         u16 p1k[IWL_P1K_SIZE];
153         int ret, i;
154
155         mutex_lock(&mvm->mutex);
156
157         switch (key->cipher) {
158         case WLAN_CIPHER_SUITE_WEP40:
159         case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
160                 struct {
161                         struct iwl_mvm_wep_key_cmd wep_key_cmd;
162                         struct iwl_mvm_wep_key wep_key;
163                 } __packed wkc = {
164                         .wep_key_cmd.mac_id_n_color =
165                                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
166                                                                 mvmvif->color)),
167                         .wep_key_cmd.num_keys = 1,
168                         /* firmware sets STA_KEY_FLG_WEP_13BYTES */
169                         .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
170                         .wep_key.key_index = key->keyidx,
171                         .wep_key.key_size = key->keylen,
172                 };
173
174                 /*
175                  * This will fail -- the key functions don't set support
176                  * pairwise WEP keys. However, that's better than silently
177                  * failing WoWLAN. Or maybe not?
178                  */
179                 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
180                         break;
181
182                 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
183                 if (key->keyidx == mvmvif->tx_key_idx) {
184                         /* TX key must be at offset 0 */
185                         wkc.wep_key.key_offset = 0;
186                 } else {
187                         /* others start at 1 */
188                         data->gtk_key_idx++;
189                         wkc.wep_key.key_offset = data->gtk_key_idx;
190                 }
191
192                 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, CMD_SYNC,
193                                            sizeof(wkc), &wkc);
194                 data->error = ret != 0;
195
196                 mvm->ptk_ivlen = key->iv_len;
197                 mvm->ptk_icvlen = key->icv_len;
198                 mvm->gtk_ivlen = key->iv_len;
199                 mvm->gtk_icvlen = key->icv_len;
200
201                 /* don't upload key again */
202                 goto out_unlock;
203         }
204         default:
205                 data->error = true;
206                 goto out_unlock;
207         case WLAN_CIPHER_SUITE_AES_CMAC:
208                 /*
209                  * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
210                  * but we also shouldn't abort suspend due to that. It does have
211                  * support for the IGTK key renewal, but doesn't really use the
212                  * IGTK for anything. This means we could spuriously wake up or
213                  * be deauthenticated, but that was considered acceptable.
214                  */
215                 goto out_unlock;
216         case WLAN_CIPHER_SUITE_TKIP:
217                 if (sta) {
218                         tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
219                         tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
220
221                         rx_p1ks = data->tkip->rx_uni;
222
223                         ieee80211_get_key_tx_seq(key, &seq);
224                         tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
225                         tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
226
227                         ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
228                         iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
229
230                         memcpy(data->tkip->mic_keys.tx,
231                                &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
232                                IWL_MIC_KEY_SIZE);
233
234                         rx_mic_key = data->tkip->mic_keys.rx_unicast;
235                 } else {
236                         tkip_sc =
237                                 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
238                         rx_p1ks = data->tkip->rx_multi;
239                         rx_mic_key = data->tkip->mic_keys.rx_mcast;
240                 }
241
242                 /*
243                  * For non-QoS this relies on the fact that both the uCode and
244                  * mac80211 use TID 0 (as they need to to avoid replay attacks)
245                  * for checking the IV in the frames.
246                  */
247                 for (i = 0; i < IWL_NUM_RSC; i++) {
248                         ieee80211_get_key_rx_seq(key, i, &seq);
249                         tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
250                         tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
251                         /* wrapping isn't allowed, AP must rekey */
252                         if (seq.tkip.iv32 > cur_rx_iv32)
253                                 cur_rx_iv32 = seq.tkip.iv32;
254                 }
255
256                 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
257                                           cur_rx_iv32, p1k);
258                 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
259                 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
260                                           cur_rx_iv32 + 1, p1k);
261                 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
262
263                 memcpy(rx_mic_key,
264                        &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
265                        IWL_MIC_KEY_SIZE);
266
267                 data->use_tkip = true;
268                 data->use_rsc_tsc = true;
269                 break;
270         case WLAN_CIPHER_SUITE_CCMP:
271                 if (sta) {
272                         u8 *pn = seq.ccmp.pn;
273
274                         aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
275                         aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
276
277                         ieee80211_get_key_tx_seq(key, &seq);
278                         aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
279                                                     ((u64)pn[4] << 8) |
280                                                     ((u64)pn[3] << 16) |
281                                                     ((u64)pn[2] << 24) |
282                                                     ((u64)pn[1] << 32) |
283                                                     ((u64)pn[0] << 40));
284                 } else {
285                         aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
286                 }
287
288                 /*
289                  * For non-QoS this relies on the fact that both the uCode and
290                  * mac80211 use TID 0 for checking the IV in the frames.
291                  */
292                 for (i = 0; i < IWL_NUM_RSC; i++) {
293                         u8 *pn = seq.ccmp.pn;
294
295                         ieee80211_get_key_rx_seq(key, i, &seq);
296                         aes_sc->pn = cpu_to_le64((u64)pn[5] |
297                                                  ((u64)pn[4] << 8) |
298                                                  ((u64)pn[3] << 16) |
299                                                  ((u64)pn[2] << 24) |
300                                                  ((u64)pn[1] << 32) |
301                                                  ((u64)pn[0] << 40));
302                 }
303                 data->use_rsc_tsc = true;
304                 break;
305         }
306
307         /*
308          * The D3 firmware hardcodes the key offset 0 as the key it uses
309          * to transmit packets to the AP, i.e. the PTK.
310          */
311         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
312                 key->hw_key_idx = 0;
313                 mvm->ptk_ivlen = key->iv_len;
314                 mvm->ptk_icvlen = key->icv_len;
315         } else {
316                 data->gtk_key_idx++;
317                 key->hw_key_idx = data->gtk_key_idx;
318                 mvm->gtk_ivlen = key->iv_len;
319                 mvm->gtk_icvlen = key->icv_len;
320         }
321
322         ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
323         data->error = ret != 0;
324 out_unlock:
325         mutex_unlock(&mvm->mutex);
326 }
327
328 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
329                                  struct cfg80211_wowlan *wowlan)
330 {
331         struct iwl_wowlan_patterns_cmd *pattern_cmd;
332         struct iwl_host_cmd cmd = {
333                 .id = WOWLAN_PATTERNS,
334                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
335                 .flags = CMD_SYNC,
336         };
337         int i, err;
338
339         if (!wowlan->n_patterns)
340                 return 0;
341
342         cmd.len[0] = sizeof(*pattern_cmd) +
343                 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
344
345         pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
346         if (!pattern_cmd)
347                 return -ENOMEM;
348
349         pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
350
351         for (i = 0; i < wowlan->n_patterns; i++) {
352                 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
353
354                 memcpy(&pattern_cmd->patterns[i].mask,
355                        wowlan->patterns[i].mask, mask_len);
356                 memcpy(&pattern_cmd->patterns[i].pattern,
357                        wowlan->patterns[i].pattern,
358                        wowlan->patterns[i].pattern_len);
359                 pattern_cmd->patterns[i].mask_size = mask_len;
360                 pattern_cmd->patterns[i].pattern_size =
361                         wowlan->patterns[i].pattern_len;
362         }
363
364         cmd.data[0] = pattern_cmd;
365         err = iwl_mvm_send_cmd(mvm, &cmd);
366         kfree(pattern_cmd);
367         return err;
368 }
369
370 static int iwl_mvm_send_proto_offload(struct iwl_mvm *mvm,
371                                       struct ieee80211_vif *vif)
372 {
373         struct iwl_proto_offload_cmd cmd = {};
374 #if IS_ENABLED(CONFIG_IPV6)
375         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
376         int i;
377
378         if (mvmvif->num_target_ipv6_addrs) {
379                 cmd.enabled |= cpu_to_le32(IWL_D3_PROTO_OFFLOAD_NS);
380                 memcpy(cmd.ndp_mac_addr, vif->addr, ETH_ALEN);
381         }
382
383         BUILD_BUG_ON(sizeof(cmd.target_ipv6_addr[i]) !=
384                      sizeof(mvmvif->target_ipv6_addrs[i]));
385
386         for (i = 0; i < mvmvif->num_target_ipv6_addrs; i++)
387                 memcpy(cmd.target_ipv6_addr[i],
388                        &mvmvif->target_ipv6_addrs[i],
389                        sizeof(cmd.target_ipv6_addr[i]));
390 #endif
391
392         if (vif->bss_conf.arp_addr_cnt) {
393                 cmd.enabled |= cpu_to_le32(IWL_D3_PROTO_OFFLOAD_ARP);
394                 cmd.host_ipv4_addr = vif->bss_conf.arp_addr_list[0];
395                 memcpy(cmd.arp_mac_addr, vif->addr, ETH_ALEN);
396         }
397
398         if (!cmd.enabled)
399                 return 0;
400
401         return iwl_mvm_send_cmd_pdu(mvm, PROT_OFFLOAD_CONFIG_CMD, CMD_SYNC,
402                                     sizeof(cmd), &cmd);
403 }
404
405 struct iwl_d3_iter_data {
406         struct iwl_mvm *mvm;
407         struct ieee80211_vif *vif;
408         bool error;
409 };
410
411 static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
412                                       struct ieee80211_vif *vif)
413 {
414         struct iwl_d3_iter_data *data = _data;
415         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
416
417         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
418                 return;
419
420         if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
421                 return;
422
423         if (data->vif) {
424                 IWL_ERR(data->mvm, "More than one managed interface active!\n");
425                 data->error = true;
426                 return;
427         }
428
429         data->vif = vif;
430 }
431
432 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
433                                 struct ieee80211_sta *ap_sta)
434 {
435         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
436         struct ieee80211_chanctx_conf *ctx;
437         u8 chains_static, chains_dynamic;
438         struct cfg80211_chan_def chandef;
439         int ret, i;
440         struct iwl_binding_cmd binding_cmd = {};
441         struct iwl_time_quota_cmd quota_cmd = {};
442         u32 status;
443
444         /* add back the PHY */
445         if (WARN_ON(!mvmvif->phy_ctxt))
446                 return -EINVAL;
447
448         rcu_read_lock();
449         ctx = rcu_dereference(vif->chanctx_conf);
450         if (WARN_ON(!ctx)) {
451                 rcu_read_unlock();
452                 return -EINVAL;
453         }
454         chandef = ctx->def;
455         chains_static = ctx->rx_chains_static;
456         chains_dynamic = ctx->rx_chains_dynamic;
457         rcu_read_unlock();
458
459         ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
460                                    chains_static, chains_dynamic);
461         if (ret)
462                 return ret;
463
464         /* add back the MAC */
465         mvmvif->uploaded = false;
466
467         if (WARN_ON(!vif->bss_conf.assoc))
468                 return -EINVAL;
469         /* hack */
470         vif->bss_conf.assoc = false;
471         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
472         vif->bss_conf.assoc = true;
473         if (ret)
474                 return ret;
475
476         /* add back binding - XXX refactor? */
477         binding_cmd.id_and_color =
478                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
479                                                 mvmvif->phy_ctxt->color));
480         binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
481         binding_cmd.phy =
482                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
483                                                 mvmvif->phy_ctxt->color));
484         binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
485                                                               mvmvif->color));
486         for (i = 1; i < MAX_MACS_IN_BINDING; i++)
487                 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
488
489         status = 0;
490         ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
491                                           sizeof(binding_cmd), &binding_cmd,
492                                           &status);
493         if (ret) {
494                 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
495                 return ret;
496         }
497
498         if (status) {
499                 IWL_ERR(mvm, "Binding command failed: %u\n", status);
500                 return -EIO;
501         }
502
503         ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
504         if (ret)
505                 return ret;
506         rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
507
508         ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
509         if (ret)
510                 return ret;
511
512         /* and some quota */
513         quota_cmd.quotas[0].id_and_color =
514                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
515                                                 mvmvif->phy_ctxt->color));
516         quota_cmd.quotas[0].quota = cpu_to_le32(100);
517         quota_cmd.quotas[0].max_duration = cpu_to_le32(1000);
518
519         for (i = 1; i < MAX_BINDINGS; i++)
520                 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
521
522         ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, CMD_SYNC,
523                                    sizeof(quota_cmd), &quota_cmd);
524         if (ret)
525                 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
526
527         return 0;
528 }
529
530 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
531 {
532         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
533         struct iwl_d3_iter_data suspend_iter_data = {
534                 .mvm = mvm,
535         };
536         struct ieee80211_vif *vif;
537         struct iwl_mvm_vif *mvmvif;
538         struct ieee80211_sta *ap_sta;
539         struct iwl_mvm_sta *mvm_ap_sta;
540         struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
541         struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
542         struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
543         struct iwl_d3_manager_config d3_cfg_cmd = {};
544         struct wowlan_key_data key_data = {
545                 .use_rsc_tsc = false,
546                 .tkip = &tkip_cmd,
547                 .use_tkip = false,
548         };
549         int ret, i;
550         u16 seq;
551         u8 old_aux_sta_id, old_ap_sta_id = IWL_MVM_STATION_COUNT;
552
553         if (WARN_ON(!wowlan))
554                 return -EINVAL;
555
556         key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
557         if (!key_data.rsc_tsc)
558                 return -ENOMEM;
559
560         mutex_lock(&mvm->mutex);
561
562         old_aux_sta_id = mvm->aux_sta.sta_id;
563
564         /* see if there's only a single BSS vif and it's associated */
565         ieee80211_iterate_active_interfaces_atomic(
566                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
567                 iwl_mvm_d3_iface_iterator, &suspend_iter_data);
568
569         if (suspend_iter_data.error || !suspend_iter_data.vif) {
570                 ret = 1;
571                 goto out_noreset;
572         }
573
574         vif = suspend_iter_data.vif;
575         mvmvif = iwl_mvm_vif_from_mac80211(vif);
576
577         ap_sta = rcu_dereference_protected(
578                         mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
579                         lockdep_is_held(&mvm->mutex));
580         if (IS_ERR_OR_NULL(ap_sta)) {
581                 ret = -EINVAL;
582                 goto out_noreset;
583         }
584
585         mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
586
587         /*
588          * The D3 firmware still hardcodes the AP station ID for the
589          * BSS we're associated with as 0. Store the real STA ID here
590          * and assign 0. When we leave this function, we'll restore
591          * the original value for the resume code.
592          */
593         old_ap_sta_id = mvm_ap_sta->sta_id;
594         mvm_ap_sta->sta_id = 0;
595         mvmvif->ap_sta_id = 0;
596
597         /* TODO: wowlan_config_cmd.wowlan_ba_teardown_tids */
598
599         wowlan_config_cmd.is_11n_connection = ap_sta->ht_cap.ht_supported;
600
601         /*
602          * We know the last used seqno, and the uCode expects to know that
603          * one, it will increment before TX.
604          */
605         seq = mvm_ap_sta->last_seq_ctl & IEEE80211_SCTL_SEQ;
606         wowlan_config_cmd.non_qos_seq = cpu_to_le16(seq);
607
608         /*
609          * For QoS counters, we store the one to use next, so subtract 0x10
610          * since the uCode will add 0x10 *before* using the value while we
611          * increment after using the value (i.e. store the next value to use).
612          */
613         for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
614                 seq = mvm_ap_sta->tid_data[i].seq_number;
615                 seq -= 0x10;
616                 wowlan_config_cmd.qos_seq[i] = cpu_to_le16(seq);
617         }
618
619         if (wowlan->disconnect)
620                 wowlan_config_cmd.wakeup_filter |=
621                         cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
622                                     IWL_WOWLAN_WAKEUP_LINK_CHANGE);
623         if (wowlan->magic_pkt)
624                 wowlan_config_cmd.wakeup_filter |=
625                         cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
626         if (wowlan->gtk_rekey_failure)
627                 wowlan_config_cmd.wakeup_filter |=
628                         cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
629         if (wowlan->eap_identity_req)
630                 wowlan_config_cmd.wakeup_filter |=
631                         cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
632         if (wowlan->four_way_handshake)
633                 wowlan_config_cmd.wakeup_filter |=
634                         cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
635         if (wowlan->n_patterns)
636                 wowlan_config_cmd.wakeup_filter |=
637                         cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
638
639         if (wowlan->rfkill_release)
640                 d3_cfg_cmd.wakeup_flags |=
641                         cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
642
643         iwl_mvm_cancel_scan(mvm);
644
645         iwl_trans_stop_device(mvm->trans);
646
647         /*
648          * Set the HW restart bit -- this is mostly true as we're
649          * going to load new firmware and reprogram that, though
650          * the reprogramming is going to be manual to avoid adding
651          * all the MACs that aren't support.
652          * We don't have to clear up everything though because the
653          * reprogramming is manual. When we resume, we'll actually
654          * go through a proper restart sequence again to switch
655          * back to the runtime firmware image.
656          */
657         set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
658
659         /* We reprogram keys and shouldn't allocate new key indices */
660         memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
661
662         mvm->ptk_ivlen = 0;
663         mvm->ptk_icvlen = 0;
664         mvm->ptk_ivlen = 0;
665         mvm->ptk_icvlen = 0;
666
667         /*
668          * The D3 firmware still hardcodes the AP station ID for the
669          * BSS we're associated with as 0. As a result, we have to move
670          * the auxiliary station to ID 1 so the ID 0 remains free for
671          * the AP station for later.
672          * We set the sta_id to 1 here, and reset it to its previous
673          * value (that we stored above) later.
674          */
675         mvm->aux_sta.sta_id = 1;
676
677         ret = iwl_mvm_load_d3_fw(mvm);
678         if (ret)
679                 goto out;
680
681         ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
682         if (ret)
683                 goto out;
684
685         if (!iwlwifi_mod_params.sw_crypto) {
686                 /*
687                  * This needs to be unlocked due to lock ordering
688                  * constraints. Since we're in the suspend path
689                  * that isn't really a problem though.
690                  */
691                 mutex_unlock(&mvm->mutex);
692                 ieee80211_iter_keys(mvm->hw, vif,
693                                     iwl_mvm_wowlan_program_keys,
694                                     &key_data);
695                 mutex_lock(&mvm->mutex);
696                 if (key_data.error) {
697                         ret = -EIO;
698                         goto out;
699                 }
700
701                 if (key_data.use_rsc_tsc) {
702                         struct iwl_host_cmd rsc_tsc_cmd = {
703                                 .id = WOWLAN_TSC_RSC_PARAM,
704                                 .flags = CMD_SYNC,
705                                 .data[0] = key_data.rsc_tsc,
706                                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
707                                 .len[0] = sizeof(*key_data.rsc_tsc),
708                         };
709
710                         ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
711                         if (ret)
712                                 goto out;
713                 }
714
715                 if (key_data.use_tkip) {
716                         ret = iwl_mvm_send_cmd_pdu(mvm,
717                                                    WOWLAN_TKIP_PARAM,
718                                                    CMD_SYNC, sizeof(tkip_cmd),
719                                                    &tkip_cmd);
720                         if (ret)
721                                 goto out;
722                 }
723
724                 if (mvmvif->rekey_data.valid) {
725                         memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
726                         memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
727                                NL80211_KCK_LEN);
728                         kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
729                         memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
730                                NL80211_KEK_LEN);
731                         kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
732                         kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
733
734                         ret = iwl_mvm_send_cmd_pdu(mvm,
735                                                    WOWLAN_KEK_KCK_MATERIAL,
736                                                    CMD_SYNC,
737                                                    sizeof(kek_kck_cmd),
738                                                    &kek_kck_cmd);
739                         if (ret)
740                                 goto out;
741                 }
742         }
743
744         ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION,
745                                    CMD_SYNC, sizeof(wowlan_config_cmd),
746                                    &wowlan_config_cmd);
747         if (ret)
748                 goto out;
749
750         ret = iwl_mvm_send_patterns(mvm, wowlan);
751         if (ret)
752                 goto out;
753
754         ret = iwl_mvm_send_proto_offload(mvm, vif);
755         if (ret)
756                 goto out;
757
758         /* must be last -- this switches firmware state */
759         ret = iwl_mvm_send_cmd_pdu(mvm, D3_CONFIG_CMD, CMD_SYNC,
760                                    sizeof(d3_cfg_cmd), &d3_cfg_cmd);
761         if (ret)
762                 goto out;
763
764         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
765
766         iwl_trans_d3_suspend(mvm->trans);
767  out:
768         mvm->aux_sta.sta_id = old_aux_sta_id;
769         mvm_ap_sta->sta_id = old_ap_sta_id;
770         mvmvif->ap_sta_id = old_ap_sta_id;
771  out_noreset:
772         kfree(key_data.rsc_tsc);
773         if (ret < 0)
774                 ieee80211_restart_hw(mvm->hw);
775
776         mutex_unlock(&mvm->mutex);
777
778         return ret;
779 }
780
781 static void iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
782                                          struct ieee80211_vif *vif)
783 {
784         u32 base = mvm->error_event_table;
785         struct error_table_start {
786                 /* cf. struct iwl_error_event_table */
787                 u32 valid;
788                 u32 error_id;
789         } err_info;
790         struct cfg80211_wowlan_wakeup wakeup = {
791                 .pattern_idx = -1,
792         };
793         struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
794         struct iwl_host_cmd cmd = {
795                 .id = WOWLAN_GET_STATUSES,
796                 .flags = CMD_SYNC | CMD_WANT_SKB,
797         };
798         struct iwl_wowlan_status *status;
799         u32 reasons;
800         int ret, len;
801         struct sk_buff *pkt = NULL;
802
803         iwl_trans_read_mem_bytes(mvm->trans, base,
804                                  &err_info, sizeof(err_info));
805
806         if (err_info.valid) {
807                 IWL_INFO(mvm, "error table is valid (%d)\n",
808                          err_info.valid);
809                 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
810                         wakeup.rfkill_release = true;
811                         ieee80211_report_wowlan_wakeup(vif, &wakeup,
812                                                        GFP_KERNEL);
813                 }
814                 return;
815         }
816
817         /* only for tracing for now */
818         ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, CMD_SYNC, 0, NULL);
819         if (ret)
820                 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
821
822         ret = iwl_mvm_send_cmd(mvm, &cmd);
823         if (ret) {
824                 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
825                 return;
826         }
827
828         /* RF-kill already asserted again... */
829         if (!cmd.resp_pkt)
830                 return;
831
832         len = le32_to_cpu(cmd.resp_pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
833         if (len - sizeof(struct iwl_cmd_header) < sizeof(*status)) {
834                 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
835                 goto out;
836         }
837
838         status = (void *)cmd.resp_pkt->data;
839
840         if (len - sizeof(struct iwl_cmd_header) !=
841             sizeof(*status) +
842             ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4)) {
843                 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
844                 goto out;
845         }
846
847         reasons = le32_to_cpu(status->wakeup_reasons);
848
849         if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
850                 wakeup_report = NULL;
851                 goto report;
852         }
853
854         if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
855                 wakeup.magic_pkt = true;
856
857         if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
858                 wakeup.pattern_idx =
859                         le16_to_cpu(status->pattern_number);
860
861         if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
862                        IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
863                 wakeup.disconnect = true;
864
865         if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
866                 wakeup.gtk_rekey_failure = true;
867
868         if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
869                 wakeup.rfkill_release = true;
870
871         if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
872                 wakeup.eap_identity_req = true;
873
874         if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
875                 wakeup.four_way_handshake = true;
876
877         if (status->wake_packet_bufsize) {
878                 int pktsize = le32_to_cpu(status->wake_packet_bufsize);
879                 int pktlen = le32_to_cpu(status->wake_packet_length);
880                 const u8 *pktdata = status->wake_packet;
881                 struct ieee80211_hdr *hdr = (void *)pktdata;
882                 int truncated = pktlen - pktsize;
883
884                 /* this would be a firmware bug */
885                 if (WARN_ON_ONCE(truncated < 0))
886                         truncated = 0;
887
888                 if (ieee80211_is_data(hdr->frame_control)) {
889                         int hdrlen = ieee80211_hdrlen(hdr->frame_control);
890                         int ivlen = 0, icvlen = 4; /* also FCS */
891
892                         pkt = alloc_skb(pktsize, GFP_KERNEL);
893                         if (!pkt)
894                                 goto report;
895
896                         memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
897                         pktdata += hdrlen;
898                         pktsize -= hdrlen;
899
900                         if (ieee80211_has_protected(hdr->frame_control)) {
901                                 if (is_multicast_ether_addr(hdr->addr1)) {
902                                         ivlen = mvm->gtk_ivlen;
903                                         icvlen += mvm->gtk_icvlen;
904                                 } else {
905                                         ivlen = mvm->ptk_ivlen;
906                                         icvlen += mvm->ptk_icvlen;
907                                 }
908                         }
909
910                         /* if truncated, FCS/ICV is (partially) gone */
911                         if (truncated >= icvlen) {
912                                 icvlen = 0;
913                                 truncated -= icvlen;
914                         } else {
915                                 icvlen -= truncated;
916                                 truncated = 0;
917                         }
918
919                         pktsize -= ivlen + icvlen;
920                         pktdata += ivlen;
921
922                         memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
923
924                         if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
925                                 goto report;
926                         wakeup.packet = pkt->data;
927                         wakeup.packet_present_len = pkt->len;
928                         wakeup.packet_len = pkt->len - truncated;
929                         wakeup.packet_80211 = false;
930                 } else {
931                         int fcslen = 4;
932
933                         if (truncated >= 4) {
934                                 truncated -= 4;
935                                 fcslen = 0;
936                         } else {
937                                 fcslen -= truncated;
938                                 truncated = 0;
939                         }
940                         pktsize -= fcslen;
941                         wakeup.packet = status->wake_packet;
942                         wakeup.packet_present_len = pktsize;
943                         wakeup.packet_len = pktlen - truncated;
944                         wakeup.packet_80211 = true;
945                 }
946         }
947
948  report:
949         ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
950         kfree_skb(pkt);
951
952  out:
953         iwl_free_resp(&cmd);
954 }
955
956 int iwl_mvm_resume(struct ieee80211_hw *hw)
957 {
958         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
959         struct iwl_d3_iter_data resume_iter_data = {
960                 .mvm = mvm,
961         };
962         struct ieee80211_vif *vif = NULL;
963         int ret;
964         enum iwl_d3_status d3_status;
965
966         mutex_lock(&mvm->mutex);
967
968         /* get the BSS vif pointer again */
969         ieee80211_iterate_active_interfaces_atomic(
970                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
971                 iwl_mvm_d3_iface_iterator, &resume_iter_data);
972
973         if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
974                 goto out_unlock;
975
976         vif = resume_iter_data.vif;
977
978         ret = iwl_trans_d3_resume(mvm->trans, &d3_status);
979         if (ret)
980                 goto out_unlock;
981
982         if (d3_status != IWL_D3_STATUS_ALIVE) {
983                 IWL_INFO(mvm, "Device was reset during suspend\n");
984                 goto out_unlock;
985         }
986
987         iwl_mvm_query_wakeup_reasons(mvm, vif);
988
989  out_unlock:
990         mutex_unlock(&mvm->mutex);
991
992         if (vif)
993                 ieee80211_resume_disconnect(vif);
994
995         /* return 1 to reconfigure the device */
996         set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
997         return 1;
998 }
999
1000 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1001 {
1002         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1003
1004         device_set_wakeup_enable(mvm->trans->dev, enabled);
1005 }