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 enum iwl_mvm_tcp_packet_type {
441 static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
443 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
444 return cpu_to_le16(be16_to_cpu((__force __be16)check));
447 static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
448 struct cfg80211_wowlan_tcp *tcp,
449 void *_pkt, u8 *mask,
450 __le16 *pseudo_hdr_csum,
451 enum iwl_mvm_tcp_packet_type ptype)
458 } __packed *pkt = _pkt;
459 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
462 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
465 pkt->ip.protocol = IPPROTO_TCP;
469 case MVM_TCP_TX_DATA:
471 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
472 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
474 pkt->ip.saddr = tcp->src;
475 pkt->ip.daddr = tcp->dst;
476 pkt->tcp.source = cpu_to_be16(tcp->src_port);
477 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
478 /* overwritten for TX SYN later */
479 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
480 pkt->tcp.window = cpu_to_be16(65000);
482 case MVM_TCP_RX_SYNACK:
484 case MVM_TCP_RX_WAKE:
485 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
486 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
487 pkt->ip.saddr = tcp->dst;
488 pkt->ip.daddr = tcp->src;
489 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
490 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
499 /* firmware assumes 8 option bytes - 8 NOPs for now */
500 memset(pkt->data, 0x01, 8);
502 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
505 case MVM_TCP_TX_DATA:
506 ip_tot_len += tcp->payload_len;
507 memcpy(pkt->data, tcp->payload, tcp->payload_len);
515 case MVM_TCP_RX_SYNACK:
522 case MVM_TCP_RX_WAKE:
523 ip_tot_len += tcp->wake_len;
526 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
532 case MVM_TCP_TX_DATA:
534 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
535 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
537 case MVM_TCP_RX_WAKE:
538 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
539 u8 tmp = tcp->wake_mask[i];
540 mask[i + 6] |= tmp << 6;
541 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
542 mask[i + 7] = tmp >> 2;
544 /* fall through for ethernet/IP/TCP headers mask */
545 case MVM_TCP_RX_SYNACK:
547 mask[0] = 0xff; /* match ethernet */
549 * match ethernet, ip.version, ip.ihl
550 * the ip.ihl half byte is really masked out by firmware
553 mask[2] = 0x80; /* match ip.protocol */
554 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
555 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
556 mask[5] = 0x80; /* match tcp flags */
557 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
561 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
562 pkt->ip.saddr, pkt->ip.daddr);
565 static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
566 struct ieee80211_vif *vif,
567 struct cfg80211_wowlan_tcp *tcp)
569 struct iwl_wowlan_remote_wake_config *cfg;
570 struct iwl_host_cmd cmd = {
571 .id = REMOTE_WAKE_CONFIG_CMD,
572 .len = { sizeof(*cfg), },
573 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
580 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
585 cfg->max_syn_retries = 10;
586 cfg->max_data_retries = 10;
587 cfg->tcp_syn_ack_timeout = 1; /* seconds */
588 cfg->tcp_ack_timeout = 1; /* seconds */
591 iwl_mvm_build_tcp_packet(
592 vif, tcp, cfg->syn_tx.data, NULL,
593 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
595 cfg->syn_tx.info.tcp_payload_length = 0;
598 iwl_mvm_build_tcp_packet(
599 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
600 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
602 cfg->synack_rx.info.tcp_payload_length = 0;
604 /* KEEPALIVE/ACK (TX) */
605 iwl_mvm_build_tcp_packet(
606 vif, tcp, cfg->keepalive_tx.data, NULL,
607 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
609 cfg->keepalive_tx.info.tcp_payload_length =
610 cpu_to_le16(tcp->payload_len);
611 cfg->sequence_number_offset = tcp->payload_seq.offset;
612 /* length must be 0..4, the field is little endian */
613 cfg->sequence_number_length = tcp->payload_seq.len;
614 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
615 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
616 if (tcp->payload_tok.len) {
617 cfg->token_offset = tcp->payload_tok.offset;
618 cfg->token_length = tcp->payload_tok.len;
620 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
621 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
624 /* set tokens to max value to almost never run out */
625 cfg->num_tokens = cpu_to_le16(65535);
629 iwl_mvm_build_tcp_packet(
630 vif, tcp, cfg->keepalive_ack_rx.data,
631 cfg->keepalive_ack_rx.rx_mask,
632 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
634 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
637 iwl_mvm_build_tcp_packet(
638 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
639 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
641 cfg->wake_rx.info.tcp_payload_length =
642 cpu_to_le16(tcp->wake_len);
645 iwl_mvm_build_tcp_packet(
646 vif, tcp, cfg->fin_tx.data, NULL,
647 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
649 cfg->fin_tx.info.tcp_payload_length = 0;
651 ret = iwl_mvm_send_cmd(mvm, &cmd);
657 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
658 struct ieee80211_sta *ap_sta)
660 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
661 struct ieee80211_chanctx_conf *ctx;
662 u8 chains_static, chains_dynamic;
663 struct cfg80211_chan_def chandef;
665 struct iwl_binding_cmd binding_cmd = {};
666 struct iwl_time_quota_cmd quota_cmd = {};
670 if (fw_has_capa(&mvm->fw->ucode_capa,
671 IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT)) {
672 size = sizeof(binding_cmd);
673 if (mvmvif->phy_ctxt->channel->band == NL80211_BAND_2GHZ ||
674 !iwl_mvm_is_cdb_supported(mvm))
675 binding_cmd.lmac_id = cpu_to_le32(IWL_LMAC_24G_INDEX);
677 binding_cmd.lmac_id = cpu_to_le32(IWL_LMAC_5G_INDEX);
679 size = IWL_BINDING_CMD_SIZE_V1;
682 /* add back the PHY */
683 if (WARN_ON(!mvmvif->phy_ctxt))
687 ctx = rcu_dereference(vif->chanctx_conf);
693 chains_static = ctx->rx_chains_static;
694 chains_dynamic = ctx->rx_chains_dynamic;
697 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
698 chains_static, chains_dynamic);
702 /* add back the MAC */
703 mvmvif->uploaded = false;
705 if (WARN_ON(!vif->bss_conf.assoc))
708 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
712 /* add back binding - XXX refactor? */
713 binding_cmd.id_and_color =
714 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
715 mvmvif->phy_ctxt->color));
716 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
718 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
719 mvmvif->phy_ctxt->color));
720 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
722 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
723 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
726 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
727 size, &binding_cmd, &status);
729 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
734 IWL_ERR(mvm, "Binding command failed: %u\n", status);
738 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
741 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
743 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
748 quota_cmd.quotas[0].id_and_color =
749 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
750 mvmvif->phy_ctxt->color));
751 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
752 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
754 for (i = 1; i < MAX_BINDINGS; i++)
755 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
757 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
758 sizeof(quota_cmd), "a_cmd);
760 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
762 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
763 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
768 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
769 struct ieee80211_vif *vif)
771 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
772 struct iwl_nonqos_seq_query_cmd query_cmd = {
773 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
775 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
778 struct iwl_host_cmd cmd = {
779 .id = NON_QOS_TX_COUNTER_CMD,
780 .flags = CMD_WANT_SKB,
785 cmd.data[0] = &query_cmd;
786 cmd.len[0] = sizeof(query_cmd);
788 err = iwl_mvm_send_cmd(mvm, &cmd);
792 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
793 if (size < sizeof(__le16)) {
796 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
797 /* firmware returns next, not last-used seqno */
798 err = (u16) (err - 0x10);
805 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
807 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
808 struct iwl_nonqos_seq_query_cmd query_cmd = {
809 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
811 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
813 .value = cpu_to_le16(mvmvif->seqno),
816 /* return if called during restart, not resume from D3 */
817 if (!mvmvif->seqno_valid)
820 mvmvif->seqno_valid = false;
822 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
823 sizeof(query_cmd), &query_cmd))
824 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
827 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
829 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
831 iwl_mvm_stop_device(mvm);
833 * Set the HW restart bit -- this is mostly true as we're
834 * going to load new firmware and reprogram that, though
835 * the reprogramming is going to be manual to avoid adding
836 * all the MACs that aren't support.
837 * We don't have to clear up everything though because the
838 * reprogramming is manual. When we resume, we'll actually
839 * go through a proper restart sequence again to switch
840 * back to the runtime firmware image.
842 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
844 /* the fw is reset, so all the keys are cleared */
845 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
852 return iwl_mvm_load_d3_fw(mvm);
856 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
857 struct cfg80211_wowlan *wowlan,
858 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
859 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
860 struct ieee80211_sta *ap_sta)
863 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
865 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
867 wowlan_config_cmd->is_11n_connection =
868 ap_sta->ht_cap.ht_supported;
869 wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
870 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
872 /* Query the last used seqno and set it */
873 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
877 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
879 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
881 if (wowlan->disconnect)
882 wowlan_config_cmd->wakeup_filter |=
883 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
884 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
885 if (wowlan->magic_pkt)
886 wowlan_config_cmd->wakeup_filter |=
887 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
888 if (wowlan->gtk_rekey_failure)
889 wowlan_config_cmd->wakeup_filter |=
890 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
891 if (wowlan->eap_identity_req)
892 wowlan_config_cmd->wakeup_filter |=
893 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
894 if (wowlan->four_way_handshake)
895 wowlan_config_cmd->wakeup_filter |=
896 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
897 if (wowlan->n_patterns)
898 wowlan_config_cmd->wakeup_filter |=
899 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
901 if (wowlan->rfkill_release)
902 wowlan_config_cmd->wakeup_filter |=
903 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
907 * Set the "link change" (really "link lost") flag as well
908 * since that implies losing the TCP connection.
910 wowlan_config_cmd->wakeup_filter |=
911 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
912 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
913 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
914 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
921 iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
922 struct ieee80211_vif *vif,
923 void (*iter)(struct ieee80211_hw *hw,
924 struct ieee80211_vif *vif,
925 struct ieee80211_sta *sta,
926 struct ieee80211_key_conf *key,
930 struct ieee80211_sta *ap_sta;
934 ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
935 if (IS_ERR_OR_NULL(ap_sta))
938 ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
943 int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
944 struct ieee80211_vif *vif,
948 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
949 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
950 bool unified = fw_has_capa(&mvm->fw->ucode_capa,
951 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
952 struct wowlan_key_data key_data = {
953 .configure_keys = !d0i3 && !unified,
954 .use_rsc_tsc = false,
958 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
961 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
962 if (!key_data.rsc_tsc)
966 * if we have to configure keys, call ieee80211_iter_keys(),
967 * as we need non-atomic context in order to take the
969 * for the d0i3 we can't use ieee80211_iter_keys(), as
970 * taking (almost) any mutex might result in deadlock.
974 * Note that currently we don't propagate cmd_flags
975 * to the iterator. In case of key_data.configure_keys,
976 * all the configured commands are SYNC, and
977 * iwl_mvm_wowlan_program_keys() will take care of
978 * locking/unlocking mvm->mutex.
980 ieee80211_iter_keys(mvm->hw, vif,
981 iwl_mvm_wowlan_program_keys,
984 iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
985 iwl_mvm_wowlan_program_keys,
989 if (key_data.error) {
994 if (key_data.use_rsc_tsc) {
995 ret = iwl_mvm_send_cmd_pdu(mvm,
996 WOWLAN_TSC_RSC_PARAM, cmd_flags,
997 sizeof(*key_data.rsc_tsc),
1003 if (key_data.use_tkip &&
1004 !fw_has_api(&mvm->fw->ucode_capa,
1005 IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
1006 ret = iwl_mvm_send_cmd_pdu(mvm,
1008 cmd_flags, sizeof(tkip_cmd),
1014 /* configure rekey data only if offloaded rekey is supported (d3) */
1015 if (mvmvif->rekey_data.valid && !d0i3) {
1016 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
1017 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1019 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
1020 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1022 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1023 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1025 ret = iwl_mvm_send_cmd_pdu(mvm,
1026 WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
1027 sizeof(kek_kck_cmd),
1034 kfree(key_data.rsc_tsc);
1039 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1040 struct cfg80211_wowlan *wowlan,
1041 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1042 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1043 struct ieee80211_sta *ap_sta)
1046 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1047 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1049 if (!unified_image) {
1050 ret = iwl_mvm_switch_to_d3(mvm);
1054 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1059 if (!iwlwifi_mod_params.swcrypto) {
1061 * This needs to be unlocked due to lock ordering
1062 * constraints. Since we're in the suspend path
1063 * that isn't really a problem though.
1065 mutex_unlock(&mvm->mutex);
1066 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
1068 mutex_lock(&mvm->mutex);
1073 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1074 sizeof(*wowlan_config_cmd),
1079 ret = iwl_mvm_send_patterns(mvm, wowlan);
1083 ret = iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
1087 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1092 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1093 struct cfg80211_wowlan *wowlan,
1094 struct cfg80211_sched_scan_request *nd_config,
1095 struct ieee80211_vif *vif)
1097 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1099 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1100 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1102 if (!unified_image) {
1103 ret = iwl_mvm_switch_to_d3(mvm);
1107 /* In theory, we wouldn't have to stop a running sched
1108 * scan in order to start another one (for
1109 * net-detect). But in practice this doesn't seem to
1110 * work properly, so stop any running sched_scan now.
1112 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1117 /* rfkill release can be either for wowlan or netdetect */
1118 if (wowlan->rfkill_release)
1119 wowlan_config_cmd.wakeup_filter |=
1120 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
1122 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1123 sizeof(wowlan_config_cmd),
1124 &wowlan_config_cmd);
1128 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1129 IWL_MVM_SCAN_NETDETECT);
1133 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1136 /* save the sched scan matchsets... */
1137 if (nd_config->n_match_sets) {
1138 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1139 sizeof(*nd_config->match_sets) *
1140 nd_config->n_match_sets,
1142 if (mvm->nd_match_sets)
1143 mvm->n_nd_match_sets = nd_config->n_match_sets;
1146 /* ...and the sched scan channels for later reporting */
1147 mvm->nd_channels = kmemdup(nd_config->channels,
1148 sizeof(*nd_config->channels) *
1149 nd_config->n_channels,
1151 if (mvm->nd_channels)
1152 mvm->n_nd_channels = nd_config->n_channels;
1157 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1159 kfree(mvm->nd_match_sets);
1160 mvm->nd_match_sets = NULL;
1161 mvm->n_nd_match_sets = 0;
1162 kfree(mvm->nd_channels);
1163 mvm->nd_channels = NULL;
1164 mvm->n_nd_channels = 0;
1167 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1168 struct cfg80211_wowlan *wowlan,
1171 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1172 struct ieee80211_vif *vif = NULL;
1173 struct iwl_mvm_vif *mvmvif = NULL;
1174 struct ieee80211_sta *ap_sta = NULL;
1175 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1177 * Program the minimum sleep time to 10 seconds, as many
1178 * platforms have issues processing a wakeup signal while
1179 * still being in the process of suspending.
1181 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1183 struct iwl_host_cmd d3_cfg_cmd = {
1184 .id = D3_CONFIG_CMD,
1185 .flags = CMD_WANT_SKB,
1186 .data[0] = &d3_cfg_cmd_data,
1187 .len[0] = sizeof(d3_cfg_cmd_data),
1190 int len __maybe_unused;
1191 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1192 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1196 * mac80211 shouldn't get here, but for D3 test
1197 * it doesn't warrant a warning
1203 mutex_lock(&mvm->mutex);
1205 vif = iwl_mvm_get_bss_vif(mvm);
1206 if (IS_ERR_OR_NULL(vif)) {
1211 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1213 if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA) {
1214 /* if we're not associated, this must be netdetect */
1215 if (!wowlan->nd_config) {
1220 ret = iwl_mvm_netdetect_config(
1221 mvm, wowlan, wowlan->nd_config, vif);
1225 mvm->net_detect = true;
1227 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1229 ap_sta = rcu_dereference_protected(
1230 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1231 lockdep_is_held(&mvm->mutex));
1232 if (IS_ERR_OR_NULL(ap_sta)) {
1237 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1238 vif, mvmvif, ap_sta);
1241 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1242 vif, mvmvif, ap_sta);
1246 mvm->net_detect = false;
1249 ret = iwl_mvm_power_update_device(mvm);
1253 ret = iwl_mvm_power_update_mac(mvm);
1257 #ifdef CONFIG_IWLWIFI_DEBUGFS
1258 if (mvm->d3_wake_sysassert)
1259 d3_cfg_cmd_data.wakeup_flags |=
1260 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1263 /* must be last -- this switches firmware state */
1264 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1267 #ifdef CONFIG_IWLWIFI_DEBUGFS
1268 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1269 if (len >= sizeof(u32)) {
1270 mvm->d3_test_pme_ptr =
1271 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1274 iwl_free_resp(&d3_cfg_cmd);
1276 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1278 iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1281 iwl_mvm_free_nd(mvm);
1283 if (!unified_image) {
1284 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1285 if (mvm->fw_restart > 0) {
1287 ieee80211_restart_hw(mvm->hw);
1292 mutex_unlock(&mvm->mutex);
1297 static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
1299 struct iwl_notification_wait wait_d3;
1300 static const u16 d3_notif[] = { D3_CONFIG_CMD };
1303 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
1304 d3_notif, ARRAY_SIZE(d3_notif),
1307 ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
1311 ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
1316 iwl_remove_notification(&mvm->notif_wait, &wait_d3);
1320 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1322 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1323 struct iwl_trans *trans = mvm->trans;
1326 /* make sure the d0i3 exit work is not pending */
1327 flush_work(&mvm->d0i3_exit_work);
1329 ret = iwl_trans_suspend(trans);
1334 trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
1336 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
1337 ret = iwl_mvm_enter_d0i3_sync(mvm);
1343 mutex_lock(&mvm->d0i3_suspend_mutex);
1344 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1345 mutex_unlock(&mvm->d0i3_suspend_mutex);
1347 iwl_trans_d3_suspend(trans, false, false);
1352 trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1354 return __iwl_mvm_suspend(hw, wowlan, false);
1357 /* converted data from the different status responses */
1358 struct iwl_wowlan_status_data {
1362 u32 wake_packet_length;
1363 u32 wake_packet_bufsize;
1364 const u8 *wake_packet;
1367 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1368 struct ieee80211_vif *vif,
1369 struct iwl_wowlan_status_data *status)
1371 struct sk_buff *pkt = NULL;
1372 struct cfg80211_wowlan_wakeup wakeup = {
1375 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1376 u32 reasons = status->wakeup_reasons;
1378 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1379 wakeup_report = NULL;
1383 pm_wakeup_event(mvm->dev, 0);
1385 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1386 wakeup.magic_pkt = true;
1388 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1389 wakeup.pattern_idx =
1390 status->pattern_number;
1392 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1393 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1394 wakeup.disconnect = true;
1396 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1397 wakeup.gtk_rekey_failure = true;
1399 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1400 wakeup.rfkill_release = true;
1402 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1403 wakeup.eap_identity_req = true;
1405 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1406 wakeup.four_way_handshake = true;
1408 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1409 wakeup.tcp_connlost = true;
1411 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1412 wakeup.tcp_nomoretokens = true;
1414 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1415 wakeup.tcp_match = true;
1417 if (status->wake_packet_bufsize) {
1418 int pktsize = status->wake_packet_bufsize;
1419 int pktlen = status->wake_packet_length;
1420 const u8 *pktdata = status->wake_packet;
1421 struct ieee80211_hdr *hdr = (void *)pktdata;
1422 int truncated = pktlen - pktsize;
1424 /* this would be a firmware bug */
1425 if (WARN_ON_ONCE(truncated < 0))
1428 if (ieee80211_is_data(hdr->frame_control)) {
1429 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1430 int ivlen = 0, icvlen = 4; /* also FCS */
1432 pkt = alloc_skb(pktsize, GFP_KERNEL);
1436 skb_put_data(pkt, pktdata, hdrlen);
1440 if (ieee80211_has_protected(hdr->frame_control)) {
1442 * This is unlocked and using gtk_i(c)vlen,
1443 * but since everything is under RTNL still
1444 * that's not really a problem - changing
1445 * it would be difficult.
1447 if (is_multicast_ether_addr(hdr->addr1)) {
1448 ivlen = mvm->gtk_ivlen;
1449 icvlen += mvm->gtk_icvlen;
1451 ivlen = mvm->ptk_ivlen;
1452 icvlen += mvm->ptk_icvlen;
1456 /* if truncated, FCS/ICV is (partially) gone */
1457 if (truncated >= icvlen) {
1459 truncated -= icvlen;
1461 icvlen -= truncated;
1465 pktsize -= ivlen + icvlen;
1468 skb_put_data(pkt, pktdata, pktsize);
1470 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1472 wakeup.packet = pkt->data;
1473 wakeup.packet_present_len = pkt->len;
1474 wakeup.packet_len = pkt->len - truncated;
1475 wakeup.packet_80211 = false;
1479 if (truncated >= 4) {
1483 fcslen -= truncated;
1487 wakeup.packet = status->wake_packet;
1488 wakeup.packet_present_len = pktsize;
1489 wakeup.packet_len = pktlen - truncated;
1490 wakeup.packet_80211 = true;
1495 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1499 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1500 struct ieee80211_key_seq *seq)
1504 pn = le64_to_cpu(sc->pn);
1505 seq->ccmp.pn[0] = pn >> 40;
1506 seq->ccmp.pn[1] = pn >> 32;
1507 seq->ccmp.pn[2] = pn >> 24;
1508 seq->ccmp.pn[3] = pn >> 16;
1509 seq->ccmp.pn[4] = pn >> 8;
1510 seq->ccmp.pn[5] = pn;
1513 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1514 struct ieee80211_key_seq *seq)
1516 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1517 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1520 static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
1521 struct ieee80211_sta *sta,
1522 struct ieee80211_key_conf *key)
1526 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1528 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
1529 struct iwl_mvm_sta *mvmsta;
1530 struct iwl_mvm_key_pn *ptk_pn;
1532 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1534 ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
1535 lockdep_is_held(&mvm->mutex));
1536 if (WARN_ON(!ptk_pn))
1539 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1540 struct ieee80211_key_seq seq = {};
1543 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1544 ieee80211_set_key_rx_seq(key, tid, &seq);
1545 for (i = 1; i < mvm->trans->num_rx_queues; i++)
1546 memcpy(ptk_pn->q[i].pn[tid],
1547 seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
1550 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1551 struct ieee80211_key_seq seq = {};
1553 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1554 ieee80211_set_key_rx_seq(key, tid, &seq);
1559 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1560 struct ieee80211_key_conf *key)
1564 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1566 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1567 struct ieee80211_key_seq seq = {};
1569 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1570 ieee80211_set_key_rx_seq(key, tid, &seq);
1574 static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
1575 struct ieee80211_key_conf *key,
1576 struct iwl_wowlan_status *status)
1578 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1580 switch (key->cipher) {
1581 case WLAN_CIPHER_SUITE_CCMP:
1582 iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
1584 case WLAN_CIPHER_SUITE_TKIP:
1585 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1592 struct iwl_mvm_d3_gtk_iter_data {
1593 struct iwl_mvm *mvm;
1594 struct iwl_wowlan_status *status;
1597 bool find_phase, unhandled_cipher;
1601 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1602 struct ieee80211_vif *vif,
1603 struct ieee80211_sta *sta,
1604 struct ieee80211_key_conf *key,
1607 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1609 if (data->unhandled_cipher)
1612 switch (key->cipher) {
1613 case WLAN_CIPHER_SUITE_WEP40:
1614 case WLAN_CIPHER_SUITE_WEP104:
1615 /* ignore WEP completely, nothing to do */
1617 case WLAN_CIPHER_SUITE_CCMP:
1618 case WLAN_CIPHER_SUITE_TKIP:
1619 /* we support these */
1622 /* everything else (even CMAC for MFP) - disconnect from AP */
1623 data->unhandled_cipher = true;
1630 * pairwise key - update sequence counters only;
1631 * note that this assumes no TDLS sessions are active
1634 struct ieee80211_key_seq seq = {};
1635 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1637 if (data->find_phase)
1640 switch (key->cipher) {
1641 case WLAN_CIPHER_SUITE_CCMP:
1642 iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
1644 atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
1646 case WLAN_CIPHER_SUITE_TKIP:
1647 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1648 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1649 atomic64_set(&key->tx_pn,
1650 (u64)seq.tkip.iv16 |
1651 ((u64)seq.tkip.iv32 << 16));
1655 /* that's it for this key */
1659 if (data->find_phase) {
1660 data->last_gtk = key;
1661 data->cipher = key->cipher;
1665 if (data->status->num_of_gtk_rekeys)
1666 ieee80211_remove_key(key);
1667 else if (data->last_gtk == key)
1668 iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
1671 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1672 struct ieee80211_vif *vif,
1673 struct iwl_wowlan_status *status)
1675 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1676 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1680 u32 disconnection_reasons =
1681 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1682 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1684 if (!status || !vif->bss_conf.bssid)
1687 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1690 /* find last GTK that we used initially, if any */
1691 gtkdata.find_phase = true;
1692 ieee80211_iter_keys(mvm->hw, vif,
1693 iwl_mvm_d3_update_keys, >kdata);
1694 /* not trying to keep connections with MFP/unhandled ciphers */
1695 if (gtkdata.unhandled_cipher)
1697 if (!gtkdata.num_keys)
1699 if (!gtkdata.last_gtk)
1703 * invalidate all other GTKs that might still exist and update
1704 * the one that we used
1706 gtkdata.find_phase = false;
1707 ieee80211_iter_keys(mvm->hw, vif,
1708 iwl_mvm_d3_update_keys, >kdata);
1710 if (status->num_of_gtk_rekeys) {
1711 struct ieee80211_key_conf *key;
1713 struct ieee80211_key_conf conf;
1716 .conf.cipher = gtkdata.cipher,
1717 .conf.keyidx = status->gtk.key_index,
1720 switch (gtkdata.cipher) {
1721 case WLAN_CIPHER_SUITE_CCMP:
1722 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1723 memcpy(conf.conf.key, status->gtk.decrypt_key,
1726 case WLAN_CIPHER_SUITE_TKIP:
1727 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1728 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1729 /* leave TX MIC key zeroed, we don't use it anyway */
1730 memcpy(conf.conf.key +
1731 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1732 status->gtk.tkip_mic_key, 8);
1736 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1739 iwl_mvm_set_key_rx_seq(mvm, key, status);
1742 if (status->num_of_gtk_rekeys) {
1744 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1745 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1746 (void *)&replay_ctr, GFP_KERNEL);
1750 mvmvif->seqno_valid = true;
1751 /* +0x10 because the set API expects next-to-use, not last-used */
1752 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1757 static struct iwl_wowlan_status *
1758 iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1760 u32 base = mvm->error_event_table[0];
1761 struct error_table_start {
1762 /* cf. struct iwl_error_event_table */
1766 struct iwl_host_cmd cmd = {
1767 .id = WOWLAN_GET_STATUSES,
1768 .flags = CMD_WANT_SKB,
1770 struct iwl_wowlan_status *status, *fw_status;
1771 int ret, len, status_size;
1773 iwl_trans_read_mem_bytes(mvm->trans, base,
1774 &err_info, sizeof(err_info));
1776 if (err_info.valid) {
1777 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1778 err_info.valid, err_info.error_id);
1779 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1780 struct cfg80211_wowlan_wakeup wakeup = {
1781 .rfkill_release = true,
1783 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1786 return ERR_PTR(-EIO);
1789 /* only for tracing for now */
1790 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1792 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1794 ret = iwl_mvm_send_cmd(mvm, &cmd);
1796 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1797 return ERR_PTR(ret);
1800 status_size = sizeof(*fw_status);
1802 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1803 if (len < status_size) {
1804 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1805 fw_status = ERR_PTR(-EIO);
1809 status = (void *)cmd.resp_pkt->data;
1810 if (len != (status_size +
1811 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1812 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1813 fw_status = ERR_PTR(-EIO);
1817 fw_status = kmemdup(status, len, GFP_KERNEL);
1820 iwl_free_resp(&cmd);
1824 /* releases the MVM mutex */
1825 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1826 struct ieee80211_vif *vif)
1828 struct iwl_wowlan_status_data status;
1829 struct iwl_wowlan_status *fw_status;
1832 struct iwl_mvm_sta *mvm_ap_sta;
1834 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1835 if (IS_ERR_OR_NULL(fw_status))
1838 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1839 for (i = 0; i < 8; i++)
1840 status.qos_seq_ctr[i] =
1841 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1842 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1843 status.wake_packet_length =
1844 le32_to_cpu(fw_status->wake_packet_length);
1845 status.wake_packet_bufsize =
1846 le32_to_cpu(fw_status->wake_packet_bufsize);
1847 status.wake_packet = fw_status->wake_packet;
1849 /* still at hard-coded place 0 for D3 image */
1850 mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
1854 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1855 u16 seq = status.qos_seq_ctr[i];
1856 /* firmware stores last-used value, we store next value */
1858 mvm_ap_sta->tid_data[i].seq_number = seq;
1861 /* now we have all the data we need, unlock to avoid mac80211 issues */
1862 mutex_unlock(&mvm->mutex);
1864 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1866 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1874 mutex_unlock(&mvm->mutex);
1878 void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
1879 struct ieee80211_vif *vif,
1880 struct iwl_wowlan_status *status)
1882 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1888 * rekey handling requires taking locks that can't be taken now.
1889 * however, d0i3 doesn't offload rekey, so we're fine.
1891 if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
1894 /* find last GTK that we used initially, if any */
1895 gtkdata.find_phase = true;
1896 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, >kdata);
1898 gtkdata.find_phase = false;
1899 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, >kdata);
1902 struct iwl_mvm_nd_query_results {
1903 u32 matched_profiles;
1904 struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1908 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1909 struct iwl_mvm_nd_query_results *results)
1911 struct iwl_scan_offload_profiles_query *query;
1912 struct iwl_host_cmd cmd = {
1913 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1914 .flags = CMD_WANT_SKB,
1918 ret = iwl_mvm_send_cmd(mvm, &cmd);
1920 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
1924 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1925 if (len < sizeof(*query)) {
1926 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
1931 query = (void *)cmd.resp_pkt->data;
1933 results->matched_profiles = le32_to_cpu(query->matched_profiles);
1934 memcpy(results->matches, query->matches, sizeof(results->matches));
1936 #ifdef CONFIG_IWLWIFI_DEBUGFS
1937 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1941 iwl_free_resp(&cmd);
1945 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1946 struct ieee80211_vif *vif)
1948 struct cfg80211_wowlan_nd_info *net_detect = NULL;
1949 struct cfg80211_wowlan_wakeup wakeup = {
1952 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1953 struct iwl_mvm_nd_query_results query;
1954 struct iwl_wowlan_status *fw_status;
1955 unsigned long matched_profiles;
1957 int i, j, n_matches, ret;
1959 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1960 if (!IS_ERR_OR_NULL(fw_status)) {
1961 reasons = le32_to_cpu(fw_status->wakeup_reasons);
1965 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1966 wakeup.rfkill_release = true;
1968 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1971 ret = iwl_mvm_netdetect_query_results(mvm, &query);
1972 if (ret || !query.matched_profiles) {
1973 wakeup_report = NULL;
1977 matched_profiles = query.matched_profiles;
1978 if (mvm->n_nd_match_sets) {
1979 n_matches = hweight_long(matched_profiles);
1981 IWL_ERR(mvm, "no net detect match information available\n");
1985 net_detect = kzalloc(sizeof(*net_detect) +
1986 (n_matches * sizeof(net_detect->matches[0])),
1988 if (!net_detect || !n_matches)
1991 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
1992 struct iwl_scan_offload_profile_match *fw_match;
1993 struct cfg80211_wowlan_nd_match *match;
1994 int idx, n_channels = 0;
1996 fw_match = &query.matches[i];
1998 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1999 n_channels += hweight8(fw_match->matching_channels[j]);
2001 match = kzalloc(sizeof(*match) +
2002 (n_channels * sizeof(*match->channels)),
2007 net_detect->matches[net_detect->n_matches++] = match;
2009 /* We inverted the order of the SSIDs in the scan
2010 * request, so invert the index here.
2012 idx = mvm->n_nd_match_sets - i - 1;
2013 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2014 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2015 match->ssid.ssid_len);
2017 if (mvm->n_nd_channels < n_channels)
2020 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
2021 if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
2022 match->channels[match->n_channels++] =
2023 mvm->nd_channels[j]->center_freq;
2027 wakeup.net_detect = net_detect;
2029 iwl_mvm_free_nd(mvm);
2031 mutex_unlock(&mvm->mutex);
2032 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2035 for (i = 0; i < net_detect->n_matches; i++)
2036 kfree(net_detect->matches[i]);
2041 static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
2043 #ifdef CONFIG_IWLWIFI_DEBUGFS
2044 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
2045 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
2046 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
2048 if (!mvm->store_d3_resume_sram)
2051 if (!mvm->d3_resume_sram) {
2052 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
2053 if (!mvm->d3_resume_sram)
2057 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
2061 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2062 struct ieee80211_vif *vif)
2064 /* skip the one we keep connection on */
2068 if (vif->type == NL80211_IFTYPE_STATION)
2069 ieee80211_resume_disconnect(vif);
2072 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
2074 struct ieee80211_vif *vif = NULL;
2076 enum iwl_d3_status d3_status;
2078 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2079 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2080 bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
2081 IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
2083 mutex_lock(&mvm->mutex);
2085 /* get the BSS vif pointer again */
2086 vif = iwl_mvm_get_bss_vif(mvm);
2087 if (IS_ERR_OR_NULL(vif))
2090 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
2094 if (d3_status != IWL_D3_STATUS_ALIVE) {
2095 IWL_INFO(mvm, "Device was reset during suspend\n");
2099 /* query SRAM first in case we want event logging */
2100 iwl_mvm_read_d3_sram(mvm);
2103 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
2105 IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
2112 * Query the current location and source from the D3 firmware so we
2113 * can play it back when we re-intiailize the D0 firmware
2115 iwl_mvm_update_changed_regdom(mvm);
2118 /* Re-configure default SAR profile */
2119 iwl_mvm_sar_select_profile(mvm, 1, 1);
2121 if (mvm->net_detect) {
2122 /* If this is a non-unified image, we restart the FW,
2123 * so no need to stop the netdetect scan. If that
2124 * fails, continue and try to get the wake-up reasons,
2125 * but trigger a HW restart by keeping a failure code
2129 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
2132 iwl_mvm_query_netdetect_reasons(mvm, vif);
2133 /* has unlocked the mutex, so skip that */
2136 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
2137 #ifdef CONFIG_IWLWIFI_DEBUGFS
2139 mvm->keep_vif = vif;
2141 /* has unlocked the mutex, so skip that */
2146 iwl_mvm_free_nd(mvm);
2147 mutex_unlock(&mvm->mutex);
2151 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
2152 IEEE80211_IFACE_ITER_NORMAL,
2153 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
2156 /* no need to reset the device in unified images, if successful */
2157 if (unified_image && !ret) {
2158 /* nothing else to do if we already sent D0I3_END_CMD */
2162 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
2168 * Reconfigure the device in one of the following cases:
2169 * 1. We are not using a unified image
2170 * 2. We are using a unified image but had an error while exiting D3
2172 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
2173 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
2175 * When switching images we return 1, which causes mac80211
2176 * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
2177 * This type of reconfig calls iwl_mvm_restart_complete(),
2178 * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
2179 * to take the reference here.
2181 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
2186 static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
2188 iwl_trans_resume(mvm->trans);
2190 return __iwl_mvm_resume(mvm, false);
2193 static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
2196 enum iwl_d3_status d3_status;
2197 struct iwl_trans *trans = mvm->trans;
2199 iwl_trans_d3_resume(trans, &d3_status, false, false);
2202 * make sure to clear D0I3_DEFER_WAKEUP before
2203 * calling iwl_trans_resume(), which might wait
2204 * for d0i3 exit completion.
2206 mutex_lock(&mvm->d0i3_suspend_mutex);
2207 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
2208 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
2209 &mvm->d0i3_suspend_flags);
2210 mutex_unlock(&mvm->d0i3_suspend_mutex);
2212 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
2213 _iwl_mvm_exit_d0i3(mvm);
2216 iwl_trans_resume(trans);
2218 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
2219 int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
2224 * d0i3 exit will be deferred until reconfig_complete.
2225 * make sure there we are out of d0i3.
2231 int iwl_mvm_resume(struct ieee80211_hw *hw)
2233 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2236 if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
2237 ret = iwl_mvm_resume_d0i3(mvm);
2239 ret = iwl_mvm_resume_d3(mvm);
2241 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2246 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2248 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2250 device_set_wakeup_enable(mvm->trans->dev, enabled);
2253 #ifdef CONFIG_IWLWIFI_DEBUGFS
2254 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2256 struct iwl_mvm *mvm = inode->i_private;
2259 if (mvm->d3_test_active)
2262 file->private_data = inode->i_private;
2264 ieee80211_stop_queues(mvm->hw);
2267 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
2269 /* start pseudo D3 */
2271 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2276 ieee80211_wake_queues(mvm->hw);
2279 mvm->d3_test_active = true;
2280 mvm->keep_vif = NULL;
2284 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2285 size_t count, loff_t *ppos)
2287 struct iwl_mvm *mvm = file->private_data;
2291 /* read pme_ptr if available */
2292 if (mvm->d3_test_pme_ptr) {
2293 pme_asserted = iwl_trans_read_mem32(mvm->trans,
2294 mvm->d3_test_pme_ptr);
2299 if (msleep_interruptible(100))
2306 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2307 struct ieee80211_vif *vif)
2309 /* skip the one we keep connection on */
2313 if (vif->type == NL80211_IFTYPE_STATION)
2314 ieee80211_connection_loss(vif);
2317 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2319 struct iwl_mvm *mvm = inode->i_private;
2320 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2321 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2323 mvm->d3_test_active = false;
2326 __iwl_mvm_resume(mvm, true);
2329 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2331 iwl_abort_notification_waits(&mvm->notif_wait);
2332 if (!unified_image) {
2333 int remaining_time = 10;
2335 ieee80211_restart_hw(mvm->hw);
2337 /* wait for restart and disconnect all interfaces */
2338 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2339 remaining_time > 0) {
2344 if (remaining_time == 0)
2345 IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
2348 ieee80211_iterate_active_interfaces_atomic(
2349 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2350 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
2352 ieee80211_wake_queues(mvm->hw);
2357 const struct file_operations iwl_dbgfs_d3_test_ops = {
2358 .llseek = no_llseek,
2359 .open = iwl_mvm_d3_test_open,
2360 .read = iwl_mvm_d3_test_read,
2361 .release = iwl_mvm_d3_test_release,