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 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 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.sw_crypto)
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:
254 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
255 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
257 rx_p1ks = data->tkip->rx_uni;
259 ieee80211_get_key_tx_seq(key, &seq);
260 tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
261 tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
263 ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
264 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
266 memcpy(data->tkip->mic_keys.tx,
267 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
270 rx_mic_key = data->tkip->mic_keys.rx_unicast;
273 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
274 rx_p1ks = data->tkip->rx_multi;
275 rx_mic_key = data->tkip->mic_keys.rx_mcast;
279 * For non-QoS this relies on the fact that both the uCode and
280 * mac80211 use TID 0 (as they need to to avoid replay attacks)
281 * for checking the IV in the frames.
283 for (i = 0; i < IWL_NUM_RSC; i++) {
284 ieee80211_get_key_rx_seq(key, i, &seq);
285 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
286 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
287 /* wrapping isn't allowed, AP must rekey */
288 if (seq.tkip.iv32 > cur_rx_iv32)
289 cur_rx_iv32 = seq.tkip.iv32;
292 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
294 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
295 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
296 cur_rx_iv32 + 1, p1k);
297 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
300 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
303 data->use_tkip = true;
304 data->use_rsc_tsc = true;
306 case WLAN_CIPHER_SUITE_CCMP:
310 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
311 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
313 pn64 = atomic64_read(&key->tx_pn);
314 aes_tx_sc->pn = cpu_to_le64(pn64);
316 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
320 * For non-QoS this relies on the fact that both the uCode and
321 * mac80211/our RX code use TID 0 for checking the PN.
323 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
324 struct iwl_mvm_sta *mvmsta;
325 struct iwl_mvm_key_pn *ptk_pn;
328 mvmsta = iwl_mvm_sta_from_mac80211(sta);
329 ptk_pn = rcu_dereference_protected(
330 mvmsta->ptk_pn[key->keyidx],
331 lockdep_is_held(&mvm->mutex));
332 if (WARN_ON(!ptk_pn))
335 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
336 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
337 mvm->trans->num_rx_queues);
338 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
346 for (i = 0; i < IWL_NUM_RSC; i++) {
347 u8 *pn = seq.ccmp.pn;
349 ieee80211_get_key_rx_seq(key, i, &seq);
350 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
358 data->use_rsc_tsc = true;
362 if (data->configure_keys) {
363 mutex_lock(&mvm->mutex);
365 * The D3 firmware hardcodes the key offset 0 as the key it
366 * uses to transmit packets to the AP, i.e. the PTK.
368 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
369 mvm->ptk_ivlen = key->iv_len;
370 mvm->ptk_icvlen = key->icv_len;
371 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
374 * firmware only supports TSC/RSC for a single key,
375 * so if there are multiple keep overwriting them
376 * with new ones -- this relies on mac80211 doing
379 mvm->gtk_ivlen = key->iv_len;
380 mvm->gtk_icvlen = key->icv_len;
381 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
383 mutex_unlock(&mvm->mutex);
384 data->error = ret != 0;
388 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
389 struct cfg80211_wowlan *wowlan)
391 struct iwl_wowlan_patterns_cmd *pattern_cmd;
392 struct iwl_host_cmd cmd = {
393 .id = WOWLAN_PATTERNS,
394 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
398 if (!wowlan->n_patterns)
401 cmd.len[0] = sizeof(*pattern_cmd) +
402 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
404 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
408 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
410 for (i = 0; i < wowlan->n_patterns; i++) {
411 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
413 memcpy(&pattern_cmd->patterns[i].mask,
414 wowlan->patterns[i].mask, mask_len);
415 memcpy(&pattern_cmd->patterns[i].pattern,
416 wowlan->patterns[i].pattern,
417 wowlan->patterns[i].pattern_len);
418 pattern_cmd->patterns[i].mask_size = mask_len;
419 pattern_cmd->patterns[i].pattern_size =
420 wowlan->patterns[i].pattern_len;
423 cmd.data[0] = pattern_cmd;
424 err = iwl_mvm_send_cmd(mvm, &cmd);
429 enum iwl_mvm_tcp_packet_type {
438 static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
440 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
441 return cpu_to_le16(be16_to_cpu((__force __be16)check));
444 static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
445 struct cfg80211_wowlan_tcp *tcp,
446 void *_pkt, u8 *mask,
447 __le16 *pseudo_hdr_csum,
448 enum iwl_mvm_tcp_packet_type ptype)
455 } __packed *pkt = _pkt;
456 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
459 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
462 pkt->ip.protocol = IPPROTO_TCP;
466 case MVM_TCP_TX_DATA:
468 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
469 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
471 pkt->ip.saddr = tcp->src;
472 pkt->ip.daddr = tcp->dst;
473 pkt->tcp.source = cpu_to_be16(tcp->src_port);
474 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
475 /* overwritten for TX SYN later */
476 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
477 pkt->tcp.window = cpu_to_be16(65000);
479 case MVM_TCP_RX_SYNACK:
481 case MVM_TCP_RX_WAKE:
482 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
483 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
484 pkt->ip.saddr = tcp->dst;
485 pkt->ip.daddr = tcp->src;
486 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
487 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
496 /* firmware assumes 8 option bytes - 8 NOPs for now */
497 memset(pkt->data, 0x01, 8);
499 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
502 case MVM_TCP_TX_DATA:
503 ip_tot_len += tcp->payload_len;
504 memcpy(pkt->data, tcp->payload, tcp->payload_len);
512 case MVM_TCP_RX_SYNACK:
519 case MVM_TCP_RX_WAKE:
520 ip_tot_len += tcp->wake_len;
523 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
529 case MVM_TCP_TX_DATA:
531 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
532 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
534 case MVM_TCP_RX_WAKE:
535 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
536 u8 tmp = tcp->wake_mask[i];
537 mask[i + 6] |= tmp << 6;
538 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
539 mask[i + 7] = tmp >> 2;
541 /* fall through for ethernet/IP/TCP headers mask */
542 case MVM_TCP_RX_SYNACK:
544 mask[0] = 0xff; /* match ethernet */
546 * match ethernet, ip.version, ip.ihl
547 * the ip.ihl half byte is really masked out by firmware
550 mask[2] = 0x80; /* match ip.protocol */
551 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
552 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
553 mask[5] = 0x80; /* match tcp flags */
554 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
558 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
559 pkt->ip.saddr, pkt->ip.daddr);
562 static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
563 struct ieee80211_vif *vif,
564 struct cfg80211_wowlan_tcp *tcp)
566 struct iwl_wowlan_remote_wake_config *cfg;
567 struct iwl_host_cmd cmd = {
568 .id = REMOTE_WAKE_CONFIG_CMD,
569 .len = { sizeof(*cfg), },
570 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
577 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
582 cfg->max_syn_retries = 10;
583 cfg->max_data_retries = 10;
584 cfg->tcp_syn_ack_timeout = 1; /* seconds */
585 cfg->tcp_ack_timeout = 1; /* seconds */
588 iwl_mvm_build_tcp_packet(
589 vif, tcp, cfg->syn_tx.data, NULL,
590 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
592 cfg->syn_tx.info.tcp_payload_length = 0;
595 iwl_mvm_build_tcp_packet(
596 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
597 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
599 cfg->synack_rx.info.tcp_payload_length = 0;
601 /* KEEPALIVE/ACK (TX) */
602 iwl_mvm_build_tcp_packet(
603 vif, tcp, cfg->keepalive_tx.data, NULL,
604 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
606 cfg->keepalive_tx.info.tcp_payload_length =
607 cpu_to_le16(tcp->payload_len);
608 cfg->sequence_number_offset = tcp->payload_seq.offset;
609 /* length must be 0..4, the field is little endian */
610 cfg->sequence_number_length = tcp->payload_seq.len;
611 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
612 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
613 if (tcp->payload_tok.len) {
614 cfg->token_offset = tcp->payload_tok.offset;
615 cfg->token_length = tcp->payload_tok.len;
617 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
618 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
621 /* set tokens to max value to almost never run out */
622 cfg->num_tokens = cpu_to_le16(65535);
626 iwl_mvm_build_tcp_packet(
627 vif, tcp, cfg->keepalive_ack_rx.data,
628 cfg->keepalive_ack_rx.rx_mask,
629 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
631 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
634 iwl_mvm_build_tcp_packet(
635 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
636 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
638 cfg->wake_rx.info.tcp_payload_length =
639 cpu_to_le16(tcp->wake_len);
642 iwl_mvm_build_tcp_packet(
643 vif, tcp, cfg->fin_tx.data, NULL,
644 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
646 cfg->fin_tx.info.tcp_payload_length = 0;
648 ret = iwl_mvm_send_cmd(mvm, &cmd);
654 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
655 struct ieee80211_sta *ap_sta)
657 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
658 struct ieee80211_chanctx_conf *ctx;
659 u8 chains_static, chains_dynamic;
660 struct cfg80211_chan_def chandef;
662 struct iwl_binding_cmd binding_cmd = {};
663 struct iwl_time_quota_cmd quota_cmd = {};
666 /* add back the PHY */
667 if (WARN_ON(!mvmvif->phy_ctxt))
671 ctx = rcu_dereference(vif->chanctx_conf);
677 chains_static = ctx->rx_chains_static;
678 chains_dynamic = ctx->rx_chains_dynamic;
681 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
682 chains_static, chains_dynamic);
686 /* add back the MAC */
687 mvmvif->uploaded = false;
689 if (WARN_ON(!vif->bss_conf.assoc))
692 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
696 /* add back binding - XXX refactor? */
697 binding_cmd.id_and_color =
698 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
699 mvmvif->phy_ctxt->color));
700 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
702 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
703 mvmvif->phy_ctxt->color));
704 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
706 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
707 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
710 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
711 sizeof(binding_cmd), &binding_cmd,
714 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
719 IWL_ERR(mvm, "Binding command failed: %u\n", status);
723 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
726 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
728 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
733 quota_cmd.quotas[0].id_and_color =
734 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
735 mvmvif->phy_ctxt->color));
736 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
737 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
739 for (i = 1; i < MAX_BINDINGS; i++)
740 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
742 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
743 sizeof(quota_cmd), "a_cmd);
745 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
747 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
748 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
753 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
754 struct ieee80211_vif *vif)
756 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
757 struct iwl_nonqos_seq_query_cmd query_cmd = {
758 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
760 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
763 struct iwl_host_cmd cmd = {
764 .id = NON_QOS_TX_COUNTER_CMD,
765 .flags = CMD_WANT_SKB,
770 cmd.data[0] = &query_cmd;
771 cmd.len[0] = sizeof(query_cmd);
773 err = iwl_mvm_send_cmd(mvm, &cmd);
777 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
778 if (size < sizeof(__le16)) {
781 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
782 /* firmware returns next, not last-used seqno */
783 err = (u16) (err - 0x10);
790 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
792 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
793 struct iwl_nonqos_seq_query_cmd query_cmd = {
794 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
796 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
798 .value = cpu_to_le16(mvmvif->seqno),
801 /* return if called during restart, not resume from D3 */
802 if (!mvmvif->seqno_valid)
805 mvmvif->seqno_valid = false;
807 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
808 sizeof(query_cmd), &query_cmd))
809 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
812 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
814 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
816 iwl_trans_stop_device(mvm->trans);
819 * Set the HW restart bit -- this is mostly true as we're
820 * going to load new firmware and reprogram that, though
821 * the reprogramming is going to be manual to avoid adding
822 * all the MACs that aren't support.
823 * We don't have to clear up everything though because the
824 * reprogramming is manual. When we resume, we'll actually
825 * go through a proper restart sequence again to switch
826 * back to the runtime firmware image.
828 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
830 /* the fw is reset, so all the keys are cleared */
831 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
838 return iwl_mvm_load_d3_fw(mvm);
842 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
843 struct cfg80211_wowlan *wowlan,
844 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
845 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
846 struct ieee80211_sta *ap_sta)
849 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
851 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
853 wowlan_config_cmd->is_11n_connection =
854 ap_sta->ht_cap.ht_supported;
855 wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
856 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING |
859 /* Query the last used seqno and set it */
860 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
864 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
866 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
868 if (wowlan->disconnect)
869 wowlan_config_cmd->wakeup_filter |=
870 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
871 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
872 if (wowlan->magic_pkt)
873 wowlan_config_cmd->wakeup_filter |=
874 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
875 if (wowlan->gtk_rekey_failure)
876 wowlan_config_cmd->wakeup_filter |=
877 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
878 if (wowlan->eap_identity_req)
879 wowlan_config_cmd->wakeup_filter |=
880 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
881 if (wowlan->four_way_handshake)
882 wowlan_config_cmd->wakeup_filter |=
883 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
884 if (wowlan->n_patterns)
885 wowlan_config_cmd->wakeup_filter |=
886 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
888 if (wowlan->rfkill_release)
889 wowlan_config_cmd->wakeup_filter |=
890 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
894 * Set the "link change" (really "link lost") flag as well
895 * since that implies losing the TCP connection.
897 wowlan_config_cmd->wakeup_filter |=
898 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
899 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
900 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
901 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
908 iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
909 struct ieee80211_vif *vif,
910 void (*iter)(struct ieee80211_hw *hw,
911 struct ieee80211_vif *vif,
912 struct ieee80211_sta *sta,
913 struct ieee80211_key_conf *key,
917 struct ieee80211_sta *ap_sta;
921 ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
922 if (IS_ERR_OR_NULL(ap_sta))
925 ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
930 int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
931 struct ieee80211_vif *vif,
935 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
936 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
937 struct wowlan_key_data key_data = {
938 .configure_keys = !d0i3,
939 .use_rsc_tsc = false,
943 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
946 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
947 if (!key_data.rsc_tsc)
951 * if we have to configure keys, call ieee80211_iter_keys(),
952 * as we need non-atomic context in order to take the
954 * for the d0i3 we can't use ieee80211_iter_keys(), as
955 * taking (almost) any mutex might result in deadlock.
959 * Note that currently we don't propagate cmd_flags
960 * to the iterator. In case of key_data.configure_keys,
961 * all the configured commands are SYNC, and
962 * iwl_mvm_wowlan_program_keys() will take care of
963 * locking/unlocking mvm->mutex.
965 ieee80211_iter_keys(mvm->hw, vif,
966 iwl_mvm_wowlan_program_keys,
969 iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
970 iwl_mvm_wowlan_program_keys,
974 if (key_data.error) {
979 if (key_data.use_rsc_tsc) {
980 ret = iwl_mvm_send_cmd_pdu(mvm,
981 WOWLAN_TSC_RSC_PARAM, cmd_flags,
982 sizeof(*key_data.rsc_tsc),
988 if (key_data.use_tkip) {
989 ret = iwl_mvm_send_cmd_pdu(mvm,
991 cmd_flags, sizeof(tkip_cmd),
997 /* configure rekey data only if offloaded rekey is supported (d3) */
998 if (mvmvif->rekey_data.valid && !d0i3) {
999 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
1000 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1002 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
1003 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1005 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1006 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1008 ret = iwl_mvm_send_cmd_pdu(mvm,
1009 WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
1010 sizeof(kek_kck_cmd),
1017 kfree(key_data.rsc_tsc);
1022 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1023 struct cfg80211_wowlan *wowlan,
1024 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1025 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1026 struct ieee80211_sta *ap_sta)
1029 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1030 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1032 if (!unified_image) {
1033 ret = iwl_mvm_switch_to_d3(mvm);
1037 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1042 if (!iwlwifi_mod_params.sw_crypto) {
1044 * This needs to be unlocked due to lock ordering
1045 * constraints. Since we're in the suspend path
1046 * that isn't really a problem though.
1048 mutex_unlock(&mvm->mutex);
1049 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
1051 mutex_lock(&mvm->mutex);
1056 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1057 sizeof(*wowlan_config_cmd),
1062 ret = iwl_mvm_send_patterns(mvm, wowlan);
1066 ret = iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
1070 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1075 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1076 struct cfg80211_wowlan *wowlan,
1077 struct cfg80211_sched_scan_request *nd_config,
1078 struct ieee80211_vif *vif)
1080 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1082 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1083 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1085 if (!unified_image) {
1086 ret = iwl_mvm_switch_to_d3(mvm);
1091 /* rfkill release can be either for wowlan or netdetect */
1092 if (wowlan->rfkill_release)
1093 wowlan_config_cmd.wakeup_filter |=
1094 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
1096 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1097 sizeof(wowlan_config_cmd),
1098 &wowlan_config_cmd);
1102 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1103 IWL_MVM_SCAN_NETDETECT);
1107 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1110 /* save the sched scan matchsets... */
1111 if (nd_config->n_match_sets) {
1112 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1113 sizeof(*nd_config->match_sets) *
1114 nd_config->n_match_sets,
1116 if (mvm->nd_match_sets)
1117 mvm->n_nd_match_sets = nd_config->n_match_sets;
1120 /* ...and the sched scan channels for later reporting */
1121 mvm->nd_channels = kmemdup(nd_config->channels,
1122 sizeof(*nd_config->channels) *
1123 nd_config->n_channels,
1125 if (mvm->nd_channels)
1126 mvm->n_nd_channels = nd_config->n_channels;
1131 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1133 kfree(mvm->nd_match_sets);
1134 mvm->nd_match_sets = NULL;
1135 mvm->n_nd_match_sets = 0;
1136 kfree(mvm->nd_channels);
1137 mvm->nd_channels = NULL;
1138 mvm->n_nd_channels = 0;
1141 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1142 struct cfg80211_wowlan *wowlan,
1145 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1146 struct ieee80211_vif *vif = NULL;
1147 struct iwl_mvm_vif *mvmvif = NULL;
1148 struct ieee80211_sta *ap_sta = NULL;
1149 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1151 * Program the minimum sleep time to 10 seconds, as many
1152 * platforms have issues processing a wakeup signal while
1153 * still being in the process of suspending.
1155 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1157 struct iwl_host_cmd d3_cfg_cmd = {
1158 .id = D3_CONFIG_CMD,
1159 .flags = CMD_WANT_SKB,
1160 .data[0] = &d3_cfg_cmd_data,
1161 .len[0] = sizeof(d3_cfg_cmd_data),
1164 int len __maybe_unused;
1165 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1166 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1170 * mac80211 shouldn't get here, but for D3 test
1171 * it doesn't warrant a warning
1177 mutex_lock(&mvm->mutex);
1179 vif = iwl_mvm_get_bss_vif(mvm);
1180 if (IS_ERR_OR_NULL(vif)) {
1185 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1187 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
1188 /* if we're not associated, this must be netdetect */
1189 if (!wowlan->nd_config) {
1194 ret = iwl_mvm_netdetect_config(
1195 mvm, wowlan, wowlan->nd_config, vif);
1199 mvm->net_detect = true;
1201 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1203 ap_sta = rcu_dereference_protected(
1204 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1205 lockdep_is_held(&mvm->mutex));
1206 if (IS_ERR_OR_NULL(ap_sta)) {
1211 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1212 vif, mvmvif, ap_sta);
1215 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1216 vif, mvmvif, ap_sta);
1220 mvm->net_detect = false;
1223 ret = iwl_mvm_power_update_device(mvm);
1227 ret = iwl_mvm_power_update_mac(mvm);
1231 #ifdef CONFIG_IWLWIFI_DEBUGFS
1232 if (mvm->d3_wake_sysassert)
1233 d3_cfg_cmd_data.wakeup_flags |=
1234 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1237 /* must be last -- this switches firmware state */
1238 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1241 #ifdef CONFIG_IWLWIFI_DEBUGFS
1242 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1243 if (len >= sizeof(u32)) {
1244 mvm->d3_test_pme_ptr =
1245 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1248 iwl_free_resp(&d3_cfg_cmd);
1250 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1252 iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1255 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1256 ieee80211_restart_hw(mvm->hw);
1257 iwl_mvm_free_nd(mvm);
1260 mutex_unlock(&mvm->mutex);
1265 static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
1267 struct iwl_notification_wait wait_d3;
1268 static const u16 d3_notif[] = { D3_CONFIG_CMD };
1271 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
1272 d3_notif, ARRAY_SIZE(d3_notif),
1275 ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
1279 ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
1284 iwl_remove_notification(&mvm->notif_wait, &wait_d3);
1288 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1290 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1291 struct iwl_trans *trans = mvm->trans;
1294 /* make sure the d0i3 exit work is not pending */
1295 flush_work(&mvm->d0i3_exit_work);
1297 ret = iwl_trans_suspend(trans);
1302 trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
1304 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
1305 ret = iwl_mvm_enter_d0i3_sync(mvm);
1311 mutex_lock(&mvm->d0i3_suspend_mutex);
1312 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1313 mutex_unlock(&mvm->d0i3_suspend_mutex);
1315 iwl_trans_d3_suspend(trans, false, false);
1320 trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1322 return __iwl_mvm_suspend(hw, wowlan, false);
1325 /* converted data from the different status responses */
1326 struct iwl_wowlan_status_data {
1330 u32 wake_packet_length;
1331 u32 wake_packet_bufsize;
1332 const u8 *wake_packet;
1335 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1336 struct ieee80211_vif *vif,
1337 struct iwl_wowlan_status_data *status)
1339 struct sk_buff *pkt = NULL;
1340 struct cfg80211_wowlan_wakeup wakeup = {
1343 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1344 u32 reasons = status->wakeup_reasons;
1346 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1347 wakeup_report = NULL;
1351 pm_wakeup_event(mvm->dev, 0);
1353 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1354 wakeup.magic_pkt = true;
1356 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1357 wakeup.pattern_idx =
1358 status->pattern_number;
1360 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1361 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1362 wakeup.disconnect = true;
1364 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1365 wakeup.gtk_rekey_failure = true;
1367 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1368 wakeup.rfkill_release = true;
1370 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1371 wakeup.eap_identity_req = true;
1373 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1374 wakeup.four_way_handshake = true;
1376 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1377 wakeup.tcp_connlost = true;
1379 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1380 wakeup.tcp_nomoretokens = true;
1382 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1383 wakeup.tcp_match = true;
1385 if (status->wake_packet_bufsize) {
1386 int pktsize = status->wake_packet_bufsize;
1387 int pktlen = status->wake_packet_length;
1388 const u8 *pktdata = status->wake_packet;
1389 struct ieee80211_hdr *hdr = (void *)pktdata;
1390 int truncated = pktlen - pktsize;
1392 /* this would be a firmware bug */
1393 if (WARN_ON_ONCE(truncated < 0))
1396 if (ieee80211_is_data(hdr->frame_control)) {
1397 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1398 int ivlen = 0, icvlen = 4; /* also FCS */
1400 pkt = alloc_skb(pktsize, GFP_KERNEL);
1404 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1408 if (ieee80211_has_protected(hdr->frame_control)) {
1410 * This is unlocked and using gtk_i(c)vlen,
1411 * but since everything is under RTNL still
1412 * that's not really a problem - changing
1413 * it would be difficult.
1415 if (is_multicast_ether_addr(hdr->addr1)) {
1416 ivlen = mvm->gtk_ivlen;
1417 icvlen += mvm->gtk_icvlen;
1419 ivlen = mvm->ptk_ivlen;
1420 icvlen += mvm->ptk_icvlen;
1424 /* if truncated, FCS/ICV is (partially) gone */
1425 if (truncated >= icvlen) {
1427 truncated -= icvlen;
1429 icvlen -= truncated;
1433 pktsize -= ivlen + icvlen;
1436 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1438 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1440 wakeup.packet = pkt->data;
1441 wakeup.packet_present_len = pkt->len;
1442 wakeup.packet_len = pkt->len - truncated;
1443 wakeup.packet_80211 = false;
1447 if (truncated >= 4) {
1451 fcslen -= truncated;
1455 wakeup.packet = status->wake_packet;
1456 wakeup.packet_present_len = pktsize;
1457 wakeup.packet_len = pktlen - truncated;
1458 wakeup.packet_80211 = true;
1463 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1467 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1468 struct ieee80211_key_seq *seq)
1472 pn = le64_to_cpu(sc->pn);
1473 seq->ccmp.pn[0] = pn >> 40;
1474 seq->ccmp.pn[1] = pn >> 32;
1475 seq->ccmp.pn[2] = pn >> 24;
1476 seq->ccmp.pn[3] = pn >> 16;
1477 seq->ccmp.pn[4] = pn >> 8;
1478 seq->ccmp.pn[5] = pn;
1481 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1482 struct ieee80211_key_seq *seq)
1484 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1485 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1488 static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
1489 struct ieee80211_sta *sta,
1490 struct ieee80211_key_conf *key)
1494 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1496 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
1497 struct iwl_mvm_sta *mvmsta;
1498 struct iwl_mvm_key_pn *ptk_pn;
1500 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1502 ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
1503 lockdep_is_held(&mvm->mutex));
1504 if (WARN_ON(!ptk_pn))
1507 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1508 struct ieee80211_key_seq seq = {};
1511 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1512 ieee80211_set_key_rx_seq(key, tid, &seq);
1513 for (i = 1; i < mvm->trans->num_rx_queues; i++)
1514 memcpy(ptk_pn->q[i].pn[tid],
1515 seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
1518 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1519 struct ieee80211_key_seq seq = {};
1521 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1522 ieee80211_set_key_rx_seq(key, tid, &seq);
1527 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1528 struct ieee80211_key_conf *key)
1532 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1534 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1535 struct ieee80211_key_seq seq = {};
1537 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1538 ieee80211_set_key_rx_seq(key, tid, &seq);
1542 static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
1543 struct ieee80211_key_conf *key,
1544 struct iwl_wowlan_status *status)
1546 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1548 switch (key->cipher) {
1549 case WLAN_CIPHER_SUITE_CCMP:
1550 iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
1552 case WLAN_CIPHER_SUITE_TKIP:
1553 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1560 struct iwl_mvm_d3_gtk_iter_data {
1561 struct iwl_mvm *mvm;
1562 struct iwl_wowlan_status *status;
1565 bool find_phase, unhandled_cipher;
1569 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1570 struct ieee80211_vif *vif,
1571 struct ieee80211_sta *sta,
1572 struct ieee80211_key_conf *key,
1575 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1577 if (data->unhandled_cipher)
1580 switch (key->cipher) {
1581 case WLAN_CIPHER_SUITE_WEP40:
1582 case WLAN_CIPHER_SUITE_WEP104:
1583 /* ignore WEP completely, nothing to do */
1585 case WLAN_CIPHER_SUITE_CCMP:
1586 case WLAN_CIPHER_SUITE_TKIP:
1587 /* we support these */
1590 /* everything else (even CMAC for MFP) - disconnect from AP */
1591 data->unhandled_cipher = true;
1598 * pairwise key - update sequence counters only;
1599 * note that this assumes no TDLS sessions are active
1602 struct ieee80211_key_seq seq = {};
1603 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1605 if (data->find_phase)
1608 switch (key->cipher) {
1609 case WLAN_CIPHER_SUITE_CCMP:
1610 iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
1612 atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
1614 case WLAN_CIPHER_SUITE_TKIP:
1615 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1616 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1617 ieee80211_set_key_tx_seq(key, &seq);
1621 /* that's it for this key */
1625 if (data->find_phase) {
1626 data->last_gtk = key;
1627 data->cipher = key->cipher;
1631 if (data->status->num_of_gtk_rekeys)
1632 ieee80211_remove_key(key);
1633 else if (data->last_gtk == key)
1634 iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
1637 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1638 struct ieee80211_vif *vif,
1639 struct iwl_wowlan_status *status)
1641 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1642 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1646 u32 disconnection_reasons =
1647 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1648 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1650 if (!status || !vif->bss_conf.bssid)
1653 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1656 /* find last GTK that we used initially, if any */
1657 gtkdata.find_phase = true;
1658 ieee80211_iter_keys(mvm->hw, vif,
1659 iwl_mvm_d3_update_keys, >kdata);
1660 /* not trying to keep connections with MFP/unhandled ciphers */
1661 if (gtkdata.unhandled_cipher)
1663 if (!gtkdata.num_keys)
1665 if (!gtkdata.last_gtk)
1669 * invalidate all other GTKs that might still exist and update
1670 * the one that we used
1672 gtkdata.find_phase = false;
1673 ieee80211_iter_keys(mvm->hw, vif,
1674 iwl_mvm_d3_update_keys, >kdata);
1676 if (status->num_of_gtk_rekeys) {
1677 struct ieee80211_key_conf *key;
1679 struct ieee80211_key_conf conf;
1682 .conf.cipher = gtkdata.cipher,
1683 .conf.keyidx = status->gtk.key_index,
1686 switch (gtkdata.cipher) {
1687 case WLAN_CIPHER_SUITE_CCMP:
1688 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1689 memcpy(conf.conf.key, status->gtk.decrypt_key,
1692 case WLAN_CIPHER_SUITE_TKIP:
1693 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1694 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1695 /* leave TX MIC key zeroed, we don't use it anyway */
1696 memcpy(conf.conf.key +
1697 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1698 status->gtk.tkip_mic_key, 8);
1702 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1705 iwl_mvm_set_key_rx_seq(mvm, key, status);
1708 if (status->num_of_gtk_rekeys) {
1710 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1711 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1712 (void *)&replay_ctr, GFP_KERNEL);
1716 mvmvif->seqno_valid = true;
1717 /* +0x10 because the set API expects next-to-use, not last-used */
1718 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1723 static struct iwl_wowlan_status *
1724 iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1726 u32 base = mvm->error_event_table;
1727 struct error_table_start {
1728 /* cf. struct iwl_error_event_table */
1732 struct iwl_host_cmd cmd = {
1733 .id = WOWLAN_GET_STATUSES,
1734 .flags = CMD_WANT_SKB,
1736 struct iwl_wowlan_status *status, *fw_status;
1737 int ret, len, status_size;
1739 iwl_trans_read_mem_bytes(mvm->trans, base,
1740 &err_info, sizeof(err_info));
1742 if (err_info.valid) {
1743 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1744 err_info.valid, err_info.error_id);
1745 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1746 struct cfg80211_wowlan_wakeup wakeup = {
1747 .rfkill_release = true,
1749 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1752 return ERR_PTR(-EIO);
1755 /* only for tracing for now */
1756 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1758 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1760 ret = iwl_mvm_send_cmd(mvm, &cmd);
1762 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1763 return ERR_PTR(ret);
1766 /* RF-kill already asserted again... */
1767 if (!cmd.resp_pkt) {
1768 fw_status = ERR_PTR(-ERFKILL);
1772 status_size = sizeof(*fw_status);
1774 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1775 if (len < status_size) {
1776 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1777 fw_status = ERR_PTR(-EIO);
1781 status = (void *)cmd.resp_pkt->data;
1782 if (len != (status_size +
1783 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1784 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1785 fw_status = ERR_PTR(-EIO);
1789 fw_status = kmemdup(status, len, GFP_KERNEL);
1792 iwl_free_resp(&cmd);
1796 /* releases the MVM mutex */
1797 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1798 struct ieee80211_vif *vif)
1800 struct iwl_wowlan_status_data status;
1801 struct iwl_wowlan_status *fw_status;
1804 struct ieee80211_sta *ap_sta;
1805 struct iwl_mvm_sta *mvm_ap_sta;
1807 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1808 if (IS_ERR_OR_NULL(fw_status))
1811 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1812 for (i = 0; i < 8; i++)
1813 status.qos_seq_ctr[i] =
1814 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1815 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1816 status.wake_packet_length =
1817 le32_to_cpu(fw_status->wake_packet_length);
1818 status.wake_packet_bufsize =
1819 le32_to_cpu(fw_status->wake_packet_bufsize);
1820 status.wake_packet = fw_status->wake_packet;
1822 /* still at hard-coded place 0 for D3 image */
1823 ap_sta = rcu_dereference_protected(
1824 mvm->fw_id_to_mac_id[0],
1825 lockdep_is_held(&mvm->mutex));
1826 if (IS_ERR_OR_NULL(ap_sta))
1829 mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
1830 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1831 u16 seq = status.qos_seq_ctr[i];
1832 /* firmware stores last-used value, we store next value */
1834 mvm_ap_sta->tid_data[i].seq_number = seq;
1837 /* now we have all the data we need, unlock to avoid mac80211 issues */
1838 mutex_unlock(&mvm->mutex);
1840 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1842 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1850 mutex_unlock(&mvm->mutex);
1854 void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
1855 struct ieee80211_vif *vif,
1856 struct iwl_wowlan_status *status)
1858 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1864 * rekey handling requires taking locks that can't be taken now.
1865 * however, d0i3 doesn't offload rekey, so we're fine.
1867 if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
1870 /* find last GTK that we used initially, if any */
1871 gtkdata.find_phase = true;
1872 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, >kdata);
1874 gtkdata.find_phase = false;
1875 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, >kdata);
1878 struct iwl_mvm_nd_query_results {
1879 u32 matched_profiles;
1880 struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1884 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1885 struct iwl_mvm_nd_query_results *results)
1887 struct iwl_scan_offload_profiles_query *query;
1888 struct iwl_host_cmd cmd = {
1889 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1890 .flags = CMD_WANT_SKB,
1894 ret = iwl_mvm_send_cmd(mvm, &cmd);
1896 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
1900 /* RF-kill already asserted again... */
1901 if (!cmd.resp_pkt) {
1906 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1907 if (len < sizeof(*query)) {
1908 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
1913 query = (void *)cmd.resp_pkt->data;
1915 results->matched_profiles = le32_to_cpu(query->matched_profiles);
1916 memcpy(results->matches, query->matches, sizeof(results->matches));
1918 #ifdef CONFIG_IWLWIFI_DEBUGFS
1919 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1923 iwl_free_resp(&cmd);
1927 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1928 struct ieee80211_vif *vif)
1930 struct cfg80211_wowlan_nd_info *net_detect = NULL;
1931 struct cfg80211_wowlan_wakeup wakeup = {
1934 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1935 struct iwl_mvm_nd_query_results query;
1936 struct iwl_wowlan_status *fw_status;
1937 unsigned long matched_profiles;
1939 int i, j, n_matches, ret;
1941 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1942 if (!IS_ERR_OR_NULL(fw_status)) {
1943 reasons = le32_to_cpu(fw_status->wakeup_reasons);
1947 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1948 wakeup.rfkill_release = true;
1950 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1953 ret = iwl_mvm_netdetect_query_results(mvm, &query);
1954 if (ret || !query.matched_profiles) {
1955 wakeup_report = NULL;
1959 matched_profiles = query.matched_profiles;
1960 if (mvm->n_nd_match_sets) {
1961 n_matches = hweight_long(matched_profiles);
1963 IWL_ERR(mvm, "no net detect match information available\n");
1967 net_detect = kzalloc(sizeof(*net_detect) +
1968 (n_matches * sizeof(net_detect->matches[0])),
1970 if (!net_detect || !n_matches)
1973 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
1974 struct iwl_scan_offload_profile_match *fw_match;
1975 struct cfg80211_wowlan_nd_match *match;
1976 int idx, n_channels = 0;
1978 fw_match = &query.matches[i];
1980 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1981 n_channels += hweight8(fw_match->matching_channels[j]);
1983 match = kzalloc(sizeof(*match) +
1984 (n_channels * sizeof(*match->channels)),
1989 net_detect->matches[net_detect->n_matches++] = match;
1991 /* We inverted the order of the SSIDs in the scan
1992 * request, so invert the index here.
1994 idx = mvm->n_nd_match_sets - i - 1;
1995 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
1996 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
1997 match->ssid.ssid_len);
1999 if (mvm->n_nd_channels < n_channels)
2002 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
2003 if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
2004 match->channels[match->n_channels++] =
2005 mvm->nd_channels[j]->center_freq;
2009 wakeup.net_detect = net_detect;
2011 iwl_mvm_free_nd(mvm);
2013 mutex_unlock(&mvm->mutex);
2014 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2017 for (i = 0; i < net_detect->n_matches; i++)
2018 kfree(net_detect->matches[i]);
2023 static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
2025 #ifdef CONFIG_IWLWIFI_DEBUGFS
2026 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
2027 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
2028 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
2030 if (!mvm->store_d3_resume_sram)
2033 if (!mvm->d3_resume_sram) {
2034 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
2035 if (!mvm->d3_resume_sram)
2039 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
2043 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2044 struct ieee80211_vif *vif)
2046 /* skip the one we keep connection on */
2050 if (vif->type == NL80211_IFTYPE_STATION)
2051 ieee80211_resume_disconnect(vif);
2054 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
2056 struct ieee80211_vif *vif = NULL;
2058 enum iwl_d3_status d3_status;
2060 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2061 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2063 u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE |
2066 mutex_lock(&mvm->mutex);
2068 /* get the BSS vif pointer again */
2069 vif = iwl_mvm_get_bss_vif(mvm);
2070 if (IS_ERR_OR_NULL(vif))
2073 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
2077 if (d3_status != IWL_D3_STATUS_ALIVE) {
2078 IWL_INFO(mvm, "Device was reset during suspend\n");
2082 /* query SRAM first in case we want event logging */
2083 iwl_mvm_read_d3_sram(mvm);
2086 * Query the current location and source from the D3 firmware so we
2087 * can play it back when we re-intiailize the D0 firmware
2089 iwl_mvm_update_changed_regdom(mvm);
2091 if (mvm->net_detect) {
2092 iwl_mvm_query_netdetect_reasons(mvm, vif);
2093 /* has unlocked the mutex, so skip that */
2096 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
2097 #ifdef CONFIG_IWLWIFI_DEBUGFS
2099 mvm->keep_vif = vif;
2101 /* has unlocked the mutex, so skip that */
2106 iwl_mvm_free_nd(mvm);
2107 mutex_unlock(&mvm->mutex);
2111 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
2112 IEEE80211_IFACE_ITER_NORMAL,
2113 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
2116 if (unified_image && !ret) {
2117 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, flags, 0, NULL);
2118 if (!ret) /* D3 ended successfully - no need to reset device */
2123 * Reconfigure the device in one of the following cases:
2124 * 1. We are not using a unified image
2125 * 2. We are using a unified image but had an error while exiting D3
2127 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
2128 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
2130 * When switching images we return 1, which causes mac80211
2131 * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
2132 * This type of reconfig calls iwl_mvm_restart_complete(),
2133 * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
2134 * to take the reference here.
2136 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
2141 static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
2143 iwl_trans_resume(mvm->trans);
2145 return __iwl_mvm_resume(mvm, false);
2148 static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
2151 enum iwl_d3_status d3_status;
2152 struct iwl_trans *trans = mvm->trans;
2154 iwl_trans_d3_resume(trans, &d3_status, false, false);
2157 * make sure to clear D0I3_DEFER_WAKEUP before
2158 * calling iwl_trans_resume(), which might wait
2159 * for d0i3 exit completion.
2161 mutex_lock(&mvm->d0i3_suspend_mutex);
2162 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
2163 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
2164 &mvm->d0i3_suspend_flags);
2165 mutex_unlock(&mvm->d0i3_suspend_mutex);
2167 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
2168 _iwl_mvm_exit_d0i3(mvm);
2171 iwl_trans_resume(trans);
2173 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
2174 int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
2179 * d0i3 exit will be deferred until reconfig_complete.
2180 * make sure there we are out of d0i3.
2186 int iwl_mvm_resume(struct ieee80211_hw *hw)
2188 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2191 if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
2192 ret = iwl_mvm_resume_d0i3(mvm);
2194 ret = iwl_mvm_resume_d3(mvm);
2196 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2201 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2203 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2205 device_set_wakeup_enable(mvm->trans->dev, enabled);
2208 #ifdef CONFIG_IWLWIFI_DEBUGFS
2209 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2211 struct iwl_mvm *mvm = inode->i_private;
2214 if (mvm->d3_test_active)
2217 file->private_data = inode->i_private;
2219 ieee80211_stop_queues(mvm->hw);
2222 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
2224 /* start pseudo D3 */
2226 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2231 ieee80211_wake_queues(mvm->hw);
2234 mvm->d3_test_active = true;
2235 mvm->keep_vif = NULL;
2239 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2240 size_t count, loff_t *ppos)
2242 struct iwl_mvm *mvm = file->private_data;
2246 /* read pme_ptr if available */
2247 if (mvm->d3_test_pme_ptr) {
2248 pme_asserted = iwl_trans_read_mem32(mvm->trans,
2249 mvm->d3_test_pme_ptr);
2254 if (msleep_interruptible(100))
2261 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2262 struct ieee80211_vif *vif)
2264 /* skip the one we keep connection on */
2268 if (vif->type == NL80211_IFTYPE_STATION)
2269 ieee80211_connection_loss(vif);
2272 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2274 struct iwl_mvm *mvm = inode->i_private;
2275 int remaining_time = 10;
2277 mvm->d3_test_active = false;
2280 __iwl_mvm_resume(mvm, true);
2283 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2285 iwl_abort_notification_waits(&mvm->notif_wait);
2286 ieee80211_restart_hw(mvm->hw);
2288 /* wait for restart and disconnect all interfaces */
2289 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2290 remaining_time > 0) {
2295 if (remaining_time == 0)
2296 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
2298 ieee80211_iterate_active_interfaces_atomic(
2299 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2300 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
2302 ieee80211_wake_queues(mvm->hw);
2307 const struct file_operations iwl_dbgfs_d3_test_ops = {
2308 .llseek = no_llseek,
2309 .open = iwl_mvm_d3_test_open,
2310 .read = iwl_mvm_d3_test_read,
2311 .release = iwl_mvm_d3_test_release,