1 /******************************************************************************
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.
8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
26 * The full GNU General Public License is included in this distribution
27 * in the file called COPYING.
29 * Contact Information:
30 * Intel Linux Wireless <linuxwifi@intel.com>
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *****************************************************************************/
68 #include <linux/etherdevice.h>
71 #include <net/cfg80211.h>
74 #include <net/addrconf.h>
75 #include "iwl-modparams.h"
79 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
80 struct ieee80211_vif *vif,
81 struct cfg80211_gtk_rekey_data *data)
83 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
84 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
86 if (iwlwifi_mod_params.swcrypto)
89 mutex_lock(&mvm->mutex);
91 memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
92 memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
93 mvmvif->rekey_data.replay_ctr =
94 cpu_to_le64(be64_to_cpup((__be64 *)data->replay_ctr));
95 mvmvif->rekey_data.valid = true;
97 mutex_unlock(&mvm->mutex);
100 #if IS_ENABLED(CONFIG_IPV6)
101 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
102 struct ieee80211_vif *vif,
103 struct inet6_dev *idev)
105 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
106 struct inet6_ifaddr *ifa;
109 memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
111 read_lock_bh(&idev->lock);
112 list_for_each_entry(ifa, &idev->addr_list, if_list) {
113 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
114 if (ifa->flags & IFA_F_TENTATIVE)
115 __set_bit(idx, mvmvif->tentative_addrs);
117 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
120 read_unlock_bh(&idev->lock);
122 mvmvif->num_target_ipv6_addrs = idx;
126 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
127 struct ieee80211_vif *vif, int idx)
129 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
131 mvmvif->tx_key_idx = idx;
134 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
138 for (i = 0; i < IWL_P1K_SIZE; i++)
139 out[i] = cpu_to_le16(p1k[i]);
142 static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
143 struct iwl_mvm_key_pn *ptk_pn,
144 struct ieee80211_key_seq *seq,
147 const u8 *ret = seq->ccmp.pn;
150 /* get the PN from mac80211, used on the default queue */
151 ieee80211_get_key_rx_seq(key, tid, seq);
153 /* and use the internal data for the other queues */
154 for (i = 1; i < queues; i++) {
155 const u8 *tmp = ptk_pn->q[i].pn[tid];
157 if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
164 struct wowlan_key_data {
165 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
166 struct iwl_wowlan_tkip_params_cmd *tkip;
167 bool error, use_rsc_tsc, use_tkip, configure_keys;
171 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
172 struct ieee80211_vif *vif,
173 struct ieee80211_sta *sta,
174 struct ieee80211_key_conf *key,
177 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
178 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
179 struct wowlan_key_data *data = _data;
180 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
181 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
182 struct iwl_p1k_cache *rx_p1ks;
184 struct ieee80211_key_seq seq;
186 u16 p1k[IWL_P1K_SIZE];
189 switch (key->cipher) {
190 case WLAN_CIPHER_SUITE_WEP40:
191 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
193 struct iwl_mvm_wep_key_cmd wep_key_cmd;
194 struct iwl_mvm_wep_key wep_key;
196 .wep_key_cmd.mac_id_n_color =
197 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
199 .wep_key_cmd.num_keys = 1,
200 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
201 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
202 .wep_key.key_index = key->keyidx,
203 .wep_key.key_size = key->keylen,
207 * This will fail -- the key functions don't set support
208 * pairwise WEP keys. However, that's better than silently
209 * failing WoWLAN. Or maybe not?
211 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
214 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
215 if (key->keyidx == mvmvif->tx_key_idx) {
216 /* TX key must be at offset 0 */
217 wkc.wep_key.key_offset = 0;
219 /* others start at 1 */
221 wkc.wep_key.key_offset = data->wep_key_idx;
224 if (data->configure_keys) {
225 mutex_lock(&mvm->mutex);
226 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0,
228 data->error = ret != 0;
230 mvm->ptk_ivlen = key->iv_len;
231 mvm->ptk_icvlen = key->icv_len;
232 mvm->gtk_ivlen = key->iv_len;
233 mvm->gtk_icvlen = key->icv_len;
234 mutex_unlock(&mvm->mutex);
237 /* don't upload key again */
243 case WLAN_CIPHER_SUITE_AES_CMAC:
245 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
246 * but we also shouldn't abort suspend due to that. It does have
247 * support for the IGTK key renewal, but doesn't really use the
248 * IGTK for anything. This means we could spuriously wake up or
249 * be deauthenticated, but that was considered acceptable.
252 case WLAN_CIPHER_SUITE_TKIP:
256 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
257 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
259 rx_p1ks = data->tkip->rx_uni;
261 pn64 = atomic64_read(&key->tx_pn);
262 tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
263 tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
265 ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
267 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
269 memcpy(data->tkip->mic_keys.tx,
270 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
273 rx_mic_key = data->tkip->mic_keys.rx_unicast;
276 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
277 rx_p1ks = data->tkip->rx_multi;
278 rx_mic_key = data->tkip->mic_keys.rx_mcast;
282 * For non-QoS this relies on the fact that both the uCode and
283 * mac80211 use TID 0 (as they need to to avoid replay attacks)
284 * for checking the IV in the frames.
286 for (i = 0; i < IWL_NUM_RSC; i++) {
287 ieee80211_get_key_rx_seq(key, i, &seq);
288 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
289 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
290 /* wrapping isn't allowed, AP must rekey */
291 if (seq.tkip.iv32 > cur_rx_iv32)
292 cur_rx_iv32 = seq.tkip.iv32;
295 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
297 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
298 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
299 cur_rx_iv32 + 1, p1k);
300 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
303 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
306 data->use_tkip = true;
307 data->use_rsc_tsc = true;
309 case WLAN_CIPHER_SUITE_CCMP:
313 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
314 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
316 pn64 = atomic64_read(&key->tx_pn);
317 aes_tx_sc->pn = cpu_to_le64(pn64);
319 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
323 * For non-QoS this relies on the fact that both the uCode and
324 * mac80211/our RX code use TID 0 for checking the PN.
326 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
327 struct iwl_mvm_sta *mvmsta;
328 struct iwl_mvm_key_pn *ptk_pn;
331 mvmsta = iwl_mvm_sta_from_mac80211(sta);
332 ptk_pn = rcu_dereference_protected(
333 mvmsta->ptk_pn[key->keyidx],
334 lockdep_is_held(&mvm->mutex));
335 if (WARN_ON(!ptk_pn))
338 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
339 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
340 mvm->trans->num_rx_queues);
341 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
349 for (i = 0; i < IWL_NUM_RSC; i++) {
350 u8 *pn = seq.ccmp.pn;
352 ieee80211_get_key_rx_seq(key, i, &seq);
353 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
361 data->use_rsc_tsc = true;
365 if (data->configure_keys) {
366 mutex_lock(&mvm->mutex);
368 * The D3 firmware hardcodes the key offset 0 as the key it
369 * uses to transmit packets to the AP, i.e. the PTK.
371 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
372 mvm->ptk_ivlen = key->iv_len;
373 mvm->ptk_icvlen = key->icv_len;
374 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
377 * firmware only supports TSC/RSC for a single key,
378 * so if there are multiple keep overwriting them
379 * with new ones -- this relies on mac80211 doing
382 mvm->gtk_ivlen = key->iv_len;
383 mvm->gtk_icvlen = key->icv_len;
384 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
386 mutex_unlock(&mvm->mutex);
387 data->error = ret != 0;
391 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
392 struct cfg80211_wowlan *wowlan)
394 struct iwl_wowlan_patterns_cmd *pattern_cmd;
395 struct iwl_host_cmd cmd = {
396 .id = WOWLAN_PATTERNS,
397 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
401 if (!wowlan->n_patterns)
404 cmd.len[0] = sizeof(*pattern_cmd) +
405 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
407 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
411 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
413 for (i = 0; i < wowlan->n_patterns; i++) {
414 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
416 memcpy(&pattern_cmd->patterns[i].mask,
417 wowlan->patterns[i].mask, mask_len);
418 memcpy(&pattern_cmd->patterns[i].pattern,
419 wowlan->patterns[i].pattern,
420 wowlan->patterns[i].pattern_len);
421 pattern_cmd->patterns[i].mask_size = mask_len;
422 pattern_cmd->patterns[i].pattern_size =
423 wowlan->patterns[i].pattern_len;
426 cmd.data[0] = pattern_cmd;
427 err = iwl_mvm_send_cmd(mvm, &cmd);
432 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
433 struct ieee80211_sta *ap_sta)
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;
440 struct iwl_binding_cmd binding_cmd = {};
441 struct iwl_time_quota_cmd quota_cmd = {};
442 struct iwl_time_quota_data *quota;
446 if (fw_has_capa(&mvm->fw->ucode_capa,
447 IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT)) {
448 size = sizeof(binding_cmd);
449 if (mvmvif->phy_ctxt->channel->band == NL80211_BAND_2GHZ ||
450 !iwl_mvm_is_cdb_supported(mvm))
451 binding_cmd.lmac_id = cpu_to_le32(IWL_LMAC_24G_INDEX);
453 binding_cmd.lmac_id = cpu_to_le32(IWL_LMAC_5G_INDEX);
455 size = IWL_BINDING_CMD_SIZE_V1;
458 /* add back the PHY */
459 if (WARN_ON(!mvmvif->phy_ctxt))
463 ctx = rcu_dereference(vif->chanctx_conf);
469 chains_static = ctx->rx_chains_static;
470 chains_dynamic = ctx->rx_chains_dynamic;
473 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
474 chains_static, chains_dynamic);
478 /* add back the MAC */
479 mvmvif->uploaded = false;
481 if (WARN_ON(!vif->bss_conf.assoc))
484 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
488 /* add back binding - XXX refactor? */
489 binding_cmd.id_and_color =
490 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
491 mvmvif->phy_ctxt->color));
492 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
494 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
495 mvmvif->phy_ctxt->color));
496 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
498 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
499 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
502 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
503 size, &binding_cmd, &status);
505 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
510 IWL_ERR(mvm, "Binding command failed: %u\n", status);
514 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
517 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
519 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
524 quota = iwl_mvm_quota_cmd_get_quota(mvm, "a_cmd, 0);
525 quota->id_and_color =
526 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
527 mvmvif->phy_ctxt->color));
528 quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
529 quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
531 for (i = 1; i < MAX_BINDINGS; i++) {
532 quota = iwl_mvm_quota_cmd_get_quota(mvm, "a_cmd, i);
533 quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
536 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
537 iwl_mvm_quota_cmd_size(mvm), "a_cmd);
539 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
541 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
542 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
547 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
548 struct ieee80211_vif *vif)
550 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
551 struct iwl_nonqos_seq_query_cmd query_cmd = {
552 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
554 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
557 struct iwl_host_cmd cmd = {
558 .id = NON_QOS_TX_COUNTER_CMD,
559 .flags = CMD_WANT_SKB,
564 cmd.data[0] = &query_cmd;
565 cmd.len[0] = sizeof(query_cmd);
567 err = iwl_mvm_send_cmd(mvm, &cmd);
571 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
572 if (size < sizeof(__le16)) {
575 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
576 /* firmware returns next, not last-used seqno */
577 err = (u16) (err - 0x10);
584 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
586 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
587 struct iwl_nonqos_seq_query_cmd query_cmd = {
588 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
590 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
592 .value = cpu_to_le16(mvmvif->seqno),
595 /* return if called during restart, not resume from D3 */
596 if (!mvmvif->seqno_valid)
599 mvmvif->seqno_valid = false;
601 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
602 sizeof(query_cmd), &query_cmd))
603 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
606 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
608 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
610 iwl_mvm_stop_device(mvm);
612 * Set the HW restart bit -- this is mostly true as we're
613 * going to load new firmware and reprogram that, though
614 * the reprogramming is going to be manual to avoid adding
615 * all the MACs that aren't support.
616 * We don't have to clear up everything though because the
617 * reprogramming is manual. When we resume, we'll actually
618 * go through a proper restart sequence again to switch
619 * back to the runtime firmware image.
621 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
623 /* the fw is reset, so all the keys are cleared */
624 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
631 return iwl_mvm_load_d3_fw(mvm);
635 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
636 struct cfg80211_wowlan *wowlan,
637 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
638 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
639 struct ieee80211_sta *ap_sta)
642 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
644 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
646 wowlan_config_cmd->is_11n_connection =
647 ap_sta->ht_cap.ht_supported;
648 wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
649 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
651 /* Query the last used seqno and set it */
652 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
656 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
658 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
660 if (wowlan->disconnect)
661 wowlan_config_cmd->wakeup_filter |=
662 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
663 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
664 if (wowlan->magic_pkt)
665 wowlan_config_cmd->wakeup_filter |=
666 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
667 if (wowlan->gtk_rekey_failure)
668 wowlan_config_cmd->wakeup_filter |=
669 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
670 if (wowlan->eap_identity_req)
671 wowlan_config_cmd->wakeup_filter |=
672 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
673 if (wowlan->four_way_handshake)
674 wowlan_config_cmd->wakeup_filter |=
675 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
676 if (wowlan->n_patterns)
677 wowlan_config_cmd->wakeup_filter |=
678 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
680 if (wowlan->rfkill_release)
681 wowlan_config_cmd->wakeup_filter |=
682 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
686 * Set the "link change" (really "link lost") flag as well
687 * since that implies losing the TCP connection.
689 wowlan_config_cmd->wakeup_filter |=
690 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
691 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
692 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
693 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
700 iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
701 struct ieee80211_vif *vif,
702 void (*iter)(struct ieee80211_hw *hw,
703 struct ieee80211_vif *vif,
704 struct ieee80211_sta *sta,
705 struct ieee80211_key_conf *key,
709 struct ieee80211_sta *ap_sta;
713 ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
714 if (IS_ERR_OR_NULL(ap_sta))
717 ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
722 int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
723 struct ieee80211_vif *vif,
727 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
728 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
729 struct wowlan_key_data key_data = {
730 .configure_keys = !d0i3,
731 .use_rsc_tsc = false,
735 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
738 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
739 if (!key_data.rsc_tsc)
743 * if we have to configure keys, call ieee80211_iter_keys(),
744 * as we need non-atomic context in order to take the
746 * for the d0i3 we can't use ieee80211_iter_keys(), as
747 * taking (almost) any mutex might result in deadlock.
751 * Note that currently we don't propagate cmd_flags
752 * to the iterator. In case of key_data.configure_keys,
753 * all the configured commands are SYNC, and
754 * iwl_mvm_wowlan_program_keys() will take care of
755 * locking/unlocking mvm->mutex.
757 ieee80211_iter_keys(mvm->hw, vif,
758 iwl_mvm_wowlan_program_keys,
761 iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
762 iwl_mvm_wowlan_program_keys,
766 if (key_data.error) {
771 if (key_data.use_rsc_tsc) {
772 ret = iwl_mvm_send_cmd_pdu(mvm,
773 WOWLAN_TSC_RSC_PARAM, cmd_flags,
774 sizeof(*key_data.rsc_tsc),
780 if (key_data.use_tkip &&
781 !fw_has_api(&mvm->fw->ucode_capa,
782 IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
783 ret = iwl_mvm_send_cmd_pdu(mvm,
785 cmd_flags, sizeof(tkip_cmd),
791 /* configure rekey data only if offloaded rekey is supported (d3) */
792 if (mvmvif->rekey_data.valid && !d0i3) {
793 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
794 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
796 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
797 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
799 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
800 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
802 ret = iwl_mvm_send_cmd_pdu(mvm,
803 WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
811 kfree(key_data.rsc_tsc);
816 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
817 struct cfg80211_wowlan *wowlan,
818 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
819 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
820 struct ieee80211_sta *ap_sta)
823 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
824 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
826 if (!unified_image) {
827 ret = iwl_mvm_switch_to_d3(mvm);
831 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
836 if (!iwlwifi_mod_params.swcrypto) {
838 * This needs to be unlocked due to lock ordering
839 * constraints. Since we're in the suspend path
840 * that isn't really a problem though.
842 mutex_unlock(&mvm->mutex);
843 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
845 mutex_lock(&mvm->mutex);
850 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
851 sizeof(*wowlan_config_cmd),
856 ret = iwl_mvm_send_patterns(mvm, wowlan);
860 return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
864 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
865 struct cfg80211_wowlan *wowlan,
866 struct cfg80211_sched_scan_request *nd_config,
867 struct ieee80211_vif *vif)
869 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
871 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
872 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
874 if (!unified_image) {
875 ret = iwl_mvm_switch_to_d3(mvm);
879 /* In theory, we wouldn't have to stop a running sched
880 * scan in order to start another one (for
881 * net-detect). But in practice this doesn't seem to
882 * work properly, so stop any running sched_scan now.
884 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
889 /* rfkill release can be either for wowlan or netdetect */
890 if (wowlan->rfkill_release)
891 wowlan_config_cmd.wakeup_filter |=
892 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
894 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
895 sizeof(wowlan_config_cmd),
900 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
901 IWL_MVM_SCAN_NETDETECT);
905 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
908 /* save the sched scan matchsets... */
909 if (nd_config->n_match_sets) {
910 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
911 sizeof(*nd_config->match_sets) *
912 nd_config->n_match_sets,
914 if (mvm->nd_match_sets)
915 mvm->n_nd_match_sets = nd_config->n_match_sets;
918 /* ...and the sched scan channels for later reporting */
919 mvm->nd_channels = kmemdup(nd_config->channels,
920 sizeof(*nd_config->channels) *
921 nd_config->n_channels,
923 if (mvm->nd_channels)
924 mvm->n_nd_channels = nd_config->n_channels;
929 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
931 kfree(mvm->nd_match_sets);
932 mvm->nd_match_sets = NULL;
933 mvm->n_nd_match_sets = 0;
934 kfree(mvm->nd_channels);
935 mvm->nd_channels = NULL;
936 mvm->n_nd_channels = 0;
939 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
940 struct cfg80211_wowlan *wowlan,
943 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
944 struct ieee80211_vif *vif = NULL;
945 struct iwl_mvm_vif *mvmvif = NULL;
946 struct ieee80211_sta *ap_sta = NULL;
947 struct iwl_d3_manager_config d3_cfg_cmd_data = {
949 * Program the minimum sleep time to 10 seconds, as many
950 * platforms have issues processing a wakeup signal while
951 * still being in the process of suspending.
953 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
955 struct iwl_host_cmd d3_cfg_cmd = {
957 .flags = CMD_WANT_SKB,
958 .data[0] = &d3_cfg_cmd_data,
959 .len[0] = sizeof(d3_cfg_cmd_data),
962 int len __maybe_unused;
963 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
964 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
968 * mac80211 shouldn't get here, but for D3 test
969 * it doesn't warrant a warning
975 mutex_lock(&mvm->mutex);
977 vif = iwl_mvm_get_bss_vif(mvm);
978 if (IS_ERR_OR_NULL(vif)) {
983 mvmvif = iwl_mvm_vif_from_mac80211(vif);
985 if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA) {
986 /* if we're not associated, this must be netdetect */
987 if (!wowlan->nd_config) {
992 ret = iwl_mvm_netdetect_config(
993 mvm, wowlan, wowlan->nd_config, vif);
997 mvm->net_detect = true;
999 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1001 ap_sta = rcu_dereference_protected(
1002 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1003 lockdep_is_held(&mvm->mutex));
1004 if (IS_ERR_OR_NULL(ap_sta)) {
1009 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1010 vif, mvmvif, ap_sta);
1013 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1014 vif, mvmvif, ap_sta);
1018 mvm->net_detect = false;
1021 ret = iwl_mvm_power_update_device(mvm);
1025 ret = iwl_mvm_power_update_mac(mvm);
1029 #ifdef CONFIG_IWLWIFI_DEBUGFS
1030 if (mvm->d3_wake_sysassert)
1031 d3_cfg_cmd_data.wakeup_flags |=
1032 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1035 /* must be last -- this switches firmware state */
1036 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1039 #ifdef CONFIG_IWLWIFI_DEBUGFS
1040 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1041 if (len >= sizeof(u32)) {
1042 mvm->d3_test_pme_ptr =
1043 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1046 iwl_free_resp(&d3_cfg_cmd);
1048 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1050 iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1053 iwl_mvm_free_nd(mvm);
1055 if (!unified_image) {
1056 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1057 if (mvm->fw_restart > 0) {
1059 ieee80211_restart_hw(mvm->hw);
1064 mutex_unlock(&mvm->mutex);
1069 static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
1071 struct iwl_notification_wait wait_d3;
1072 static const u16 d3_notif[] = { D3_CONFIG_CMD };
1075 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
1076 d3_notif, ARRAY_SIZE(d3_notif),
1079 ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
1083 ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
1088 iwl_remove_notification(&mvm->notif_wait, &wait_d3);
1092 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1094 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1095 struct iwl_trans *trans = mvm->trans;
1098 /* make sure the d0i3 exit work is not pending */
1099 flush_work(&mvm->d0i3_exit_work);
1101 iwl_fw_runtime_suspend(&mvm->fwrt);
1103 ret = iwl_trans_suspend(trans);
1108 trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
1110 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
1111 ret = iwl_mvm_enter_d0i3_sync(mvm);
1117 mutex_lock(&mvm->d0i3_suspend_mutex);
1118 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1119 mutex_unlock(&mvm->d0i3_suspend_mutex);
1121 iwl_trans_d3_suspend(trans, false, false);
1126 trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1128 return __iwl_mvm_suspend(hw, wowlan, false);
1131 /* converted data from the different status responses */
1132 struct iwl_wowlan_status_data {
1136 u32 wake_packet_length;
1137 u32 wake_packet_bufsize;
1138 const u8 *wake_packet;
1141 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1142 struct ieee80211_vif *vif,
1143 struct iwl_wowlan_status_data *status)
1145 struct sk_buff *pkt = NULL;
1146 struct cfg80211_wowlan_wakeup wakeup = {
1149 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1150 u32 reasons = status->wakeup_reasons;
1152 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1153 wakeup_report = NULL;
1157 pm_wakeup_event(mvm->dev, 0);
1159 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1160 wakeup.magic_pkt = true;
1162 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1163 wakeup.pattern_idx =
1164 status->pattern_number;
1166 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1167 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1168 wakeup.disconnect = true;
1170 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1171 wakeup.gtk_rekey_failure = true;
1173 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1174 wakeup.rfkill_release = true;
1176 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1177 wakeup.eap_identity_req = true;
1179 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1180 wakeup.four_way_handshake = true;
1182 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1183 wakeup.tcp_connlost = true;
1185 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1186 wakeup.tcp_nomoretokens = true;
1188 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1189 wakeup.tcp_match = true;
1191 if (status->wake_packet_bufsize) {
1192 int pktsize = status->wake_packet_bufsize;
1193 int pktlen = status->wake_packet_length;
1194 const u8 *pktdata = status->wake_packet;
1195 struct ieee80211_hdr *hdr = (void *)pktdata;
1196 int truncated = pktlen - pktsize;
1198 /* this would be a firmware bug */
1199 if (WARN_ON_ONCE(truncated < 0))
1202 if (ieee80211_is_data(hdr->frame_control)) {
1203 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1204 int ivlen = 0, icvlen = 4; /* also FCS */
1206 pkt = alloc_skb(pktsize, GFP_KERNEL);
1210 skb_put_data(pkt, pktdata, hdrlen);
1214 if (ieee80211_has_protected(hdr->frame_control)) {
1216 * This is unlocked and using gtk_i(c)vlen,
1217 * but since everything is under RTNL still
1218 * that's not really a problem - changing
1219 * it would be difficult.
1221 if (is_multicast_ether_addr(hdr->addr1)) {
1222 ivlen = mvm->gtk_ivlen;
1223 icvlen += mvm->gtk_icvlen;
1225 ivlen = mvm->ptk_ivlen;
1226 icvlen += mvm->ptk_icvlen;
1230 /* if truncated, FCS/ICV is (partially) gone */
1231 if (truncated >= icvlen) {
1233 truncated -= icvlen;
1235 icvlen -= truncated;
1239 pktsize -= ivlen + icvlen;
1242 skb_put_data(pkt, pktdata, pktsize);
1244 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1246 wakeup.packet = pkt->data;
1247 wakeup.packet_present_len = pkt->len;
1248 wakeup.packet_len = pkt->len - truncated;
1249 wakeup.packet_80211 = false;
1253 if (truncated >= 4) {
1257 fcslen -= truncated;
1261 wakeup.packet = status->wake_packet;
1262 wakeup.packet_present_len = pktsize;
1263 wakeup.packet_len = pktlen - truncated;
1264 wakeup.packet_80211 = true;
1269 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1273 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1274 struct ieee80211_key_seq *seq)
1278 pn = le64_to_cpu(sc->pn);
1279 seq->ccmp.pn[0] = pn >> 40;
1280 seq->ccmp.pn[1] = pn >> 32;
1281 seq->ccmp.pn[2] = pn >> 24;
1282 seq->ccmp.pn[3] = pn >> 16;
1283 seq->ccmp.pn[4] = pn >> 8;
1284 seq->ccmp.pn[5] = pn;
1287 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1288 struct ieee80211_key_seq *seq)
1290 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1291 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1294 static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
1295 struct ieee80211_sta *sta,
1296 struct ieee80211_key_conf *key)
1300 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1302 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
1303 struct iwl_mvm_sta *mvmsta;
1304 struct iwl_mvm_key_pn *ptk_pn;
1306 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1308 ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
1309 lockdep_is_held(&mvm->mutex));
1310 if (WARN_ON(!ptk_pn))
1313 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1314 struct ieee80211_key_seq seq = {};
1317 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1318 ieee80211_set_key_rx_seq(key, tid, &seq);
1319 for (i = 1; i < mvm->trans->num_rx_queues; i++)
1320 memcpy(ptk_pn->q[i].pn[tid],
1321 seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
1324 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1325 struct ieee80211_key_seq seq = {};
1327 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1328 ieee80211_set_key_rx_seq(key, tid, &seq);
1333 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1334 struct ieee80211_key_conf *key)
1338 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1340 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1341 struct ieee80211_key_seq seq = {};
1343 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1344 ieee80211_set_key_rx_seq(key, tid, &seq);
1348 static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
1349 struct ieee80211_key_conf *key,
1350 struct iwl_wowlan_status *status)
1352 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1354 switch (key->cipher) {
1355 case WLAN_CIPHER_SUITE_CCMP:
1356 iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
1358 case WLAN_CIPHER_SUITE_TKIP:
1359 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1366 struct iwl_mvm_d3_gtk_iter_data {
1367 struct iwl_mvm *mvm;
1368 struct iwl_wowlan_status *status;
1371 bool find_phase, unhandled_cipher;
1375 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1376 struct ieee80211_vif *vif,
1377 struct ieee80211_sta *sta,
1378 struct ieee80211_key_conf *key,
1381 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1383 if (data->unhandled_cipher)
1386 switch (key->cipher) {
1387 case WLAN_CIPHER_SUITE_WEP40:
1388 case WLAN_CIPHER_SUITE_WEP104:
1389 /* ignore WEP completely, nothing to do */
1391 case WLAN_CIPHER_SUITE_CCMP:
1392 case WLAN_CIPHER_SUITE_TKIP:
1393 /* we support these */
1396 /* everything else (even CMAC for MFP) - disconnect from AP */
1397 data->unhandled_cipher = true;
1404 * pairwise key - update sequence counters only;
1405 * note that this assumes no TDLS sessions are active
1408 struct ieee80211_key_seq seq = {};
1409 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1411 if (data->find_phase)
1414 switch (key->cipher) {
1415 case WLAN_CIPHER_SUITE_CCMP:
1416 iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
1418 atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
1420 case WLAN_CIPHER_SUITE_TKIP:
1421 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1422 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1423 atomic64_set(&key->tx_pn,
1424 (u64)seq.tkip.iv16 |
1425 ((u64)seq.tkip.iv32 << 16));
1429 /* that's it for this key */
1433 if (data->find_phase) {
1434 data->last_gtk = key;
1435 data->cipher = key->cipher;
1439 if (data->status->num_of_gtk_rekeys)
1440 ieee80211_remove_key(key);
1441 else if (data->last_gtk == key)
1442 iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
1445 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1446 struct ieee80211_vif *vif,
1447 struct iwl_wowlan_status *status)
1449 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1450 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1454 u32 disconnection_reasons =
1455 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1456 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1458 if (!status || !vif->bss_conf.bssid)
1461 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1464 /* find last GTK that we used initially, if any */
1465 gtkdata.find_phase = true;
1466 ieee80211_iter_keys(mvm->hw, vif,
1467 iwl_mvm_d3_update_keys, >kdata);
1468 /* not trying to keep connections with MFP/unhandled ciphers */
1469 if (gtkdata.unhandled_cipher)
1471 if (!gtkdata.num_keys)
1473 if (!gtkdata.last_gtk)
1477 * invalidate all other GTKs that might still exist and update
1478 * the one that we used
1480 gtkdata.find_phase = false;
1481 ieee80211_iter_keys(mvm->hw, vif,
1482 iwl_mvm_d3_update_keys, >kdata);
1484 if (status->num_of_gtk_rekeys) {
1485 struct ieee80211_key_conf *key;
1487 struct ieee80211_key_conf conf;
1490 .conf.cipher = gtkdata.cipher,
1491 .conf.keyidx = status->gtk.key_index,
1494 switch (gtkdata.cipher) {
1495 case WLAN_CIPHER_SUITE_CCMP:
1496 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1497 memcpy(conf.conf.key, status->gtk.decrypt_key,
1500 case WLAN_CIPHER_SUITE_TKIP:
1501 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1502 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1503 /* leave TX MIC key zeroed, we don't use it anyway */
1504 memcpy(conf.conf.key +
1505 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1506 status->gtk.tkip_mic_key, 8);
1510 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1513 iwl_mvm_set_key_rx_seq(mvm, key, status);
1516 if (status->num_of_gtk_rekeys) {
1518 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1519 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1520 (void *)&replay_ctr, GFP_KERNEL);
1524 mvmvif->seqno_valid = true;
1525 /* +0x10 because the set API expects next-to-use, not last-used */
1526 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1531 static struct iwl_wowlan_status *
1532 iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1534 u32 base = mvm->error_event_table[0];
1535 struct error_table_start {
1536 /* cf. struct iwl_error_event_table */
1540 struct iwl_host_cmd cmd = {
1541 .id = WOWLAN_GET_STATUSES,
1542 .flags = CMD_WANT_SKB,
1544 struct iwl_wowlan_status *status, *fw_status;
1545 int ret, len, status_size;
1547 iwl_trans_read_mem_bytes(mvm->trans, base,
1548 &err_info, sizeof(err_info));
1550 if (err_info.valid) {
1551 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1552 err_info.valid, err_info.error_id);
1553 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1554 struct cfg80211_wowlan_wakeup wakeup = {
1555 .rfkill_release = true,
1557 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1560 return ERR_PTR(-EIO);
1563 /* only for tracing for now */
1564 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1566 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1568 ret = iwl_mvm_send_cmd(mvm, &cmd);
1570 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1571 return ERR_PTR(ret);
1574 status_size = sizeof(*fw_status);
1576 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1577 if (len < status_size) {
1578 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1579 fw_status = ERR_PTR(-EIO);
1583 status = (void *)cmd.resp_pkt->data;
1584 if (len != (status_size +
1585 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1586 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1587 fw_status = ERR_PTR(-EIO);
1591 fw_status = kmemdup(status, len, GFP_KERNEL);
1594 iwl_free_resp(&cmd);
1598 /* releases the MVM mutex */
1599 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1600 struct ieee80211_vif *vif)
1602 struct iwl_wowlan_status_data status;
1603 struct iwl_wowlan_status *fw_status;
1606 struct iwl_mvm_sta *mvm_ap_sta;
1608 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1609 if (IS_ERR_OR_NULL(fw_status))
1612 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1613 for (i = 0; i < 8; i++)
1614 status.qos_seq_ctr[i] =
1615 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1616 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1617 status.wake_packet_length =
1618 le32_to_cpu(fw_status->wake_packet_length);
1619 status.wake_packet_bufsize =
1620 le32_to_cpu(fw_status->wake_packet_bufsize);
1621 status.wake_packet = fw_status->wake_packet;
1623 /* still at hard-coded place 0 for D3 image */
1624 mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
1628 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1629 u16 seq = status.qos_seq_ctr[i];
1630 /* firmware stores last-used value, we store next value */
1632 mvm_ap_sta->tid_data[i].seq_number = seq;
1635 /* now we have all the data we need, unlock to avoid mac80211 issues */
1636 mutex_unlock(&mvm->mutex);
1638 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1640 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1648 mutex_unlock(&mvm->mutex);
1652 void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
1653 struct ieee80211_vif *vif,
1654 struct iwl_wowlan_status *status)
1656 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1662 * rekey handling requires taking locks that can't be taken now.
1663 * however, d0i3 doesn't offload rekey, so we're fine.
1665 if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
1668 /* find last GTK that we used initially, if any */
1669 gtkdata.find_phase = true;
1670 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, >kdata);
1672 gtkdata.find_phase = false;
1673 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, >kdata);
1676 struct iwl_mvm_nd_query_results {
1677 u32 matched_profiles;
1678 struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1682 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1683 struct iwl_mvm_nd_query_results *results)
1685 struct iwl_scan_offload_profiles_query *query;
1686 struct iwl_host_cmd cmd = {
1687 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1688 .flags = CMD_WANT_SKB,
1692 ret = iwl_mvm_send_cmd(mvm, &cmd);
1694 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
1698 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1699 if (len < sizeof(*query)) {
1700 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
1705 query = (void *)cmd.resp_pkt->data;
1707 results->matched_profiles = le32_to_cpu(query->matched_profiles);
1708 memcpy(results->matches, query->matches, sizeof(results->matches));
1710 #ifdef CONFIG_IWLWIFI_DEBUGFS
1711 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1715 iwl_free_resp(&cmd);
1719 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1720 struct ieee80211_vif *vif)
1722 struct cfg80211_wowlan_nd_info *net_detect = NULL;
1723 struct cfg80211_wowlan_wakeup wakeup = {
1726 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1727 struct iwl_mvm_nd_query_results query;
1728 struct iwl_wowlan_status *fw_status;
1729 unsigned long matched_profiles;
1731 int i, j, n_matches, ret;
1733 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1734 if (!IS_ERR_OR_NULL(fw_status)) {
1735 reasons = le32_to_cpu(fw_status->wakeup_reasons);
1739 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1740 wakeup.rfkill_release = true;
1742 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1745 ret = iwl_mvm_netdetect_query_results(mvm, &query);
1746 if (ret || !query.matched_profiles) {
1747 wakeup_report = NULL;
1751 matched_profiles = query.matched_profiles;
1752 if (mvm->n_nd_match_sets) {
1753 n_matches = hweight_long(matched_profiles);
1755 IWL_ERR(mvm, "no net detect match information available\n");
1759 net_detect = kzalloc(sizeof(*net_detect) +
1760 (n_matches * sizeof(net_detect->matches[0])),
1762 if (!net_detect || !n_matches)
1765 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
1766 struct iwl_scan_offload_profile_match *fw_match;
1767 struct cfg80211_wowlan_nd_match *match;
1768 int idx, n_channels = 0;
1770 fw_match = &query.matches[i];
1772 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1773 n_channels += hweight8(fw_match->matching_channels[j]);
1775 match = kzalloc(sizeof(*match) +
1776 (n_channels * sizeof(*match->channels)),
1781 net_detect->matches[net_detect->n_matches++] = match;
1783 /* We inverted the order of the SSIDs in the scan
1784 * request, so invert the index here.
1786 idx = mvm->n_nd_match_sets - i - 1;
1787 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
1788 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
1789 match->ssid.ssid_len);
1791 if (mvm->n_nd_channels < n_channels)
1794 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
1795 if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
1796 match->channels[match->n_channels++] =
1797 mvm->nd_channels[j]->center_freq;
1801 wakeup.net_detect = net_detect;
1803 iwl_mvm_free_nd(mvm);
1805 mutex_unlock(&mvm->mutex);
1806 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1809 for (i = 0; i < net_detect->n_matches; i++)
1810 kfree(net_detect->matches[i]);
1815 static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1817 #ifdef CONFIG_IWLWIFI_DEBUGFS
1818 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1819 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1820 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1822 if (!mvm->store_d3_resume_sram)
1825 if (!mvm->d3_resume_sram) {
1826 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1827 if (!mvm->d3_resume_sram)
1831 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1835 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1836 struct ieee80211_vif *vif)
1838 /* skip the one we keep connection on */
1842 if (vif->type == NL80211_IFTYPE_STATION)
1843 ieee80211_resume_disconnect(vif);
1846 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
1848 struct ieee80211_vif *vif = NULL;
1850 enum iwl_d3_status d3_status;
1852 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1853 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1854 bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
1855 IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
1857 mutex_lock(&mvm->mutex);
1859 /* get the BSS vif pointer again */
1860 vif = iwl_mvm_get_bss_vif(mvm);
1861 if (IS_ERR_OR_NULL(vif))
1864 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
1868 if (d3_status != IWL_D3_STATUS_ALIVE) {
1869 IWL_INFO(mvm, "Device was reset during suspend\n");
1873 /* query SRAM first in case we want event logging */
1874 iwl_mvm_read_d3_sram(mvm);
1877 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
1879 IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
1886 * Query the current location and source from the D3 firmware so we
1887 * can play it back when we re-intiailize the D0 firmware
1889 iwl_mvm_update_changed_regdom(mvm);
1892 /* Re-configure default SAR profile */
1893 iwl_mvm_sar_select_profile(mvm, 1, 1);
1895 if (mvm->net_detect) {
1896 /* If this is a non-unified image, we restart the FW,
1897 * so no need to stop the netdetect scan. If that
1898 * fails, continue and try to get the wake-up reasons,
1899 * but trigger a HW restart by keeping a failure code
1903 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
1906 iwl_mvm_query_netdetect_reasons(mvm, vif);
1907 /* has unlocked the mutex, so skip that */
1910 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
1911 #ifdef CONFIG_IWLWIFI_DEBUGFS
1913 mvm->keep_vif = vif;
1915 /* has unlocked the mutex, so skip that */
1920 iwl_mvm_free_nd(mvm);
1921 mutex_unlock(&mvm->mutex);
1925 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1926 IEEE80211_IFACE_ITER_NORMAL,
1927 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
1930 /* no need to reset the device in unified images, if successful */
1931 if (unified_image && !ret) {
1932 /* nothing else to do if we already sent D0I3_END_CMD */
1936 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
1942 * Reconfigure the device in one of the following cases:
1943 * 1. We are not using a unified image
1944 * 2. We are using a unified image but had an error while exiting D3
1946 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1947 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
1949 * When switching images we return 1, which causes mac80211
1950 * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
1951 * This type of reconfig calls iwl_mvm_restart_complete(),
1952 * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
1953 * to take the reference here.
1955 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1960 static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
1962 iwl_trans_resume(mvm->trans);
1964 return __iwl_mvm_resume(mvm, false);
1967 static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
1970 enum iwl_d3_status d3_status;
1971 struct iwl_trans *trans = mvm->trans;
1973 iwl_trans_d3_resume(trans, &d3_status, false, false);
1976 * make sure to clear D0I3_DEFER_WAKEUP before
1977 * calling iwl_trans_resume(), which might wait
1978 * for d0i3 exit completion.
1980 mutex_lock(&mvm->d0i3_suspend_mutex);
1981 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1982 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1983 &mvm->d0i3_suspend_flags);
1984 mutex_unlock(&mvm->d0i3_suspend_mutex);
1986 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
1987 _iwl_mvm_exit_d0i3(mvm);
1990 iwl_trans_resume(trans);
1992 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
1993 int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
1998 * d0i3 exit will be deferred until reconfig_complete.
1999 * make sure there we are out of d0i3.
2005 int iwl_mvm_resume(struct ieee80211_hw *hw)
2007 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2010 if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
2011 ret = iwl_mvm_resume_d0i3(mvm);
2013 ret = iwl_mvm_resume_d3(mvm);
2015 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2017 iwl_fw_runtime_resume(&mvm->fwrt);
2022 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2024 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2026 device_set_wakeup_enable(mvm->trans->dev, enabled);
2029 #ifdef CONFIG_IWLWIFI_DEBUGFS
2030 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2032 struct iwl_mvm *mvm = inode->i_private;
2035 if (mvm->d3_test_active)
2038 file->private_data = inode->i_private;
2040 ieee80211_stop_queues(mvm->hw);
2043 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
2045 iwl_fw_runtime_suspend(&mvm->fwrt);
2047 /* start pseudo D3 */
2049 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2054 ieee80211_wake_queues(mvm->hw);
2057 mvm->d3_test_active = true;
2058 mvm->keep_vif = NULL;
2062 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2063 size_t count, loff_t *ppos)
2065 struct iwl_mvm *mvm = file->private_data;
2069 /* read pme_ptr if available */
2070 if (mvm->d3_test_pme_ptr) {
2071 pme_asserted = iwl_trans_read_mem32(mvm->trans,
2072 mvm->d3_test_pme_ptr);
2077 if (msleep_interruptible(100))
2084 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2085 struct ieee80211_vif *vif)
2087 /* skip the one we keep connection on */
2091 if (vif->type == NL80211_IFTYPE_STATION)
2092 ieee80211_connection_loss(vif);
2095 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2097 struct iwl_mvm *mvm = inode->i_private;
2098 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2099 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2101 mvm->d3_test_active = false;
2104 __iwl_mvm_resume(mvm, true);
2107 iwl_fw_runtime_resume(&mvm->fwrt);
2109 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2111 iwl_abort_notification_waits(&mvm->notif_wait);
2112 if (!unified_image) {
2113 int remaining_time = 10;
2115 ieee80211_restart_hw(mvm->hw);
2117 /* wait for restart and disconnect all interfaces */
2118 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2119 remaining_time > 0) {
2124 if (remaining_time == 0)
2125 IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
2128 ieee80211_iterate_active_interfaces_atomic(
2129 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2130 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
2132 ieee80211_wake_queues(mvm->hw);
2137 const struct file_operations iwl_dbgfs_d3_test_ops = {
2138 .llseek = no_llseek,
2139 .open = iwl_mvm_d3_test_open,
2140 .read = iwl_mvm_d3_test_read,
2141 .release = iwl_mvm_d3_test_release,