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 - 2014 Intel Mobile Communications GmbH
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
25 * The full GNU General Public License is included in this distribution
26 * in the file called COPYING.
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 *****************************************************************************/
66 #include <linux/etherdevice.h>
69 #include <net/cfg80211.h>
72 #include <net/addrconf.h>
73 #include "iwl-modparams.h"
77 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
78 struct ieee80211_vif *vif,
79 struct cfg80211_gtk_rekey_data *data)
81 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
82 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
84 if (iwlwifi_mod_params.sw_crypto)
87 mutex_lock(&mvm->mutex);
89 memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
90 memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
91 mvmvif->rekey_data.replay_ctr =
92 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
93 mvmvif->rekey_data.valid = true;
95 mutex_unlock(&mvm->mutex);
98 #if IS_ENABLED(CONFIG_IPV6)
99 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
100 struct ieee80211_vif *vif,
101 struct inet6_dev *idev)
103 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
104 struct inet6_ifaddr *ifa;
107 read_lock_bh(&idev->lock);
108 list_for_each_entry(ifa, &idev->addr_list, if_list) {
109 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
111 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
114 read_unlock_bh(&idev->lock);
116 mvmvif->num_target_ipv6_addrs = idx;
120 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
121 struct ieee80211_vif *vif, int idx)
123 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
125 mvmvif->tx_key_idx = idx;
128 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
132 for (i = 0; i < IWL_P1K_SIZE; i++)
133 out[i] = cpu_to_le16(p1k[i]);
136 struct wowlan_key_data {
137 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
138 struct iwl_wowlan_tkip_params_cmd *tkip;
139 bool error, use_rsc_tsc, use_tkip;
143 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
144 struct ieee80211_vif *vif,
145 struct ieee80211_sta *sta,
146 struct ieee80211_key_conf *key,
149 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
150 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
151 struct wowlan_key_data *data = _data;
152 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
153 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
154 struct iwl_p1k_cache *rx_p1ks;
156 struct ieee80211_key_seq seq;
158 u16 p1k[IWL_P1K_SIZE];
161 mutex_lock(&mvm->mutex);
163 switch (key->cipher) {
164 case WLAN_CIPHER_SUITE_WEP40:
165 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
167 struct iwl_mvm_wep_key_cmd wep_key_cmd;
168 struct iwl_mvm_wep_key wep_key;
170 .wep_key_cmd.mac_id_n_color =
171 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
173 .wep_key_cmd.num_keys = 1,
174 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
175 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
176 .wep_key.key_index = key->keyidx,
177 .wep_key.key_size = key->keylen,
181 * This will fail -- the key functions don't set support
182 * pairwise WEP keys. However, that's better than silently
183 * failing WoWLAN. Or maybe not?
185 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
188 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
189 if (key->keyidx == mvmvif->tx_key_idx) {
190 /* TX key must be at offset 0 */
191 wkc.wep_key.key_offset = 0;
193 /* others start at 1 */
195 wkc.wep_key.key_offset = data->wep_key_idx;
198 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
199 data->error = ret != 0;
201 mvm->ptk_ivlen = key->iv_len;
202 mvm->ptk_icvlen = key->icv_len;
203 mvm->gtk_ivlen = key->iv_len;
204 mvm->gtk_icvlen = key->icv_len;
206 /* don't upload key again */
212 case WLAN_CIPHER_SUITE_AES_CMAC:
214 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
215 * but we also shouldn't abort suspend due to that. It does have
216 * support for the IGTK key renewal, but doesn't really use the
217 * IGTK for anything. This means we could spuriously wake up or
218 * be deauthenticated, but that was considered acceptable.
221 case WLAN_CIPHER_SUITE_TKIP:
223 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
224 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
226 rx_p1ks = data->tkip->rx_uni;
228 ieee80211_get_key_tx_seq(key, &seq);
229 tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
230 tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
232 ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
233 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
235 memcpy(data->tkip->mic_keys.tx,
236 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
239 rx_mic_key = data->tkip->mic_keys.rx_unicast;
242 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
243 rx_p1ks = data->tkip->rx_multi;
244 rx_mic_key = data->tkip->mic_keys.rx_mcast;
248 * For non-QoS this relies on the fact that both the uCode and
249 * mac80211 use TID 0 (as they need to to avoid replay attacks)
250 * for checking the IV in the frames.
252 for (i = 0; i < IWL_NUM_RSC; i++) {
253 ieee80211_get_key_rx_seq(key, i, &seq);
254 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
255 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
256 /* wrapping isn't allowed, AP must rekey */
257 if (seq.tkip.iv32 > cur_rx_iv32)
258 cur_rx_iv32 = seq.tkip.iv32;
261 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
263 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
264 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
265 cur_rx_iv32 + 1, p1k);
266 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
269 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
272 data->use_tkip = true;
273 data->use_rsc_tsc = true;
275 case WLAN_CIPHER_SUITE_CCMP:
277 u8 *pn = seq.ccmp.pn;
279 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
280 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
282 ieee80211_get_key_tx_seq(key, &seq);
283 aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
290 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
294 * For non-QoS this relies on the fact that both the uCode and
295 * mac80211 use TID 0 for checking the IV in the frames.
297 for (i = 0; i < IWL_NUM_RSC; i++) {
298 u8 *pn = seq.ccmp.pn;
300 ieee80211_get_key_rx_seq(key, i, &seq);
301 aes_sc->pn = cpu_to_le64((u64)pn[5] |
308 data->use_rsc_tsc = true;
313 * The D3 firmware hardcodes the key offset 0 as the key it uses
314 * to transmit packets to the AP, i.e. the PTK.
316 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
318 mvm->ptk_ivlen = key->iv_len;
319 mvm->ptk_icvlen = key->icv_len;
322 * firmware only supports TSC/RSC for a single key,
323 * so if there are multiple keep overwriting them
324 * with new ones -- this relies on mac80211 doing
328 mvm->gtk_ivlen = key->iv_len;
329 mvm->gtk_icvlen = key->icv_len;
332 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
333 data->error = ret != 0;
335 mutex_unlock(&mvm->mutex);
338 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
339 struct cfg80211_wowlan *wowlan)
341 struct iwl_wowlan_patterns_cmd *pattern_cmd;
342 struct iwl_host_cmd cmd = {
343 .id = WOWLAN_PATTERNS,
344 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
348 if (!wowlan->n_patterns)
351 cmd.len[0] = sizeof(*pattern_cmd) +
352 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
354 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
358 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
360 for (i = 0; i < wowlan->n_patterns; i++) {
361 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
363 memcpy(&pattern_cmd->patterns[i].mask,
364 wowlan->patterns[i].mask, mask_len);
365 memcpy(&pattern_cmd->patterns[i].pattern,
366 wowlan->patterns[i].pattern,
367 wowlan->patterns[i].pattern_len);
368 pattern_cmd->patterns[i].mask_size = mask_len;
369 pattern_cmd->patterns[i].pattern_size =
370 wowlan->patterns[i].pattern_len;
373 cmd.data[0] = pattern_cmd;
374 err = iwl_mvm_send_cmd(mvm, &cmd);
379 enum iwl_mvm_tcp_packet_type {
388 static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
390 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
391 return cpu_to_le16(be16_to_cpu((__force __be16)check));
394 static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
395 struct cfg80211_wowlan_tcp *tcp,
396 void *_pkt, u8 *mask,
397 __le16 *pseudo_hdr_csum,
398 enum iwl_mvm_tcp_packet_type ptype)
405 } __packed *pkt = _pkt;
406 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
409 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
412 pkt->ip.protocol = IPPROTO_TCP;
416 case MVM_TCP_TX_DATA:
418 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
419 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
421 pkt->ip.saddr = tcp->src;
422 pkt->ip.daddr = tcp->dst;
423 pkt->tcp.source = cpu_to_be16(tcp->src_port);
424 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
425 /* overwritten for TX SYN later */
426 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
427 pkt->tcp.window = cpu_to_be16(65000);
429 case MVM_TCP_RX_SYNACK:
431 case MVM_TCP_RX_WAKE:
432 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
433 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
434 pkt->ip.saddr = tcp->dst;
435 pkt->ip.daddr = tcp->src;
436 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
437 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
446 /* firmware assumes 8 option bytes - 8 NOPs for now */
447 memset(pkt->data, 0x01, 8);
449 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
452 case MVM_TCP_TX_DATA:
453 ip_tot_len += tcp->payload_len;
454 memcpy(pkt->data, tcp->payload, tcp->payload_len);
462 case MVM_TCP_RX_SYNACK:
469 case MVM_TCP_RX_WAKE:
470 ip_tot_len += tcp->wake_len;
473 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
479 case MVM_TCP_TX_DATA:
481 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
482 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
484 case MVM_TCP_RX_WAKE:
485 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
486 u8 tmp = tcp->wake_mask[i];
487 mask[i + 6] |= tmp << 6;
488 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
489 mask[i + 7] = tmp >> 2;
491 /* fall through for ethernet/IP/TCP headers mask */
492 case MVM_TCP_RX_SYNACK:
494 mask[0] = 0xff; /* match ethernet */
496 * match ethernet, ip.version, ip.ihl
497 * the ip.ihl half byte is really masked out by firmware
500 mask[2] = 0x80; /* match ip.protocol */
501 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
502 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
503 mask[5] = 0x80; /* match tcp flags */
504 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
508 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
509 pkt->ip.saddr, pkt->ip.daddr);
512 static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
513 struct ieee80211_vif *vif,
514 struct cfg80211_wowlan_tcp *tcp)
516 struct iwl_wowlan_remote_wake_config *cfg;
517 struct iwl_host_cmd cmd = {
518 .id = REMOTE_WAKE_CONFIG_CMD,
519 .len = { sizeof(*cfg), },
520 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
527 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
532 cfg->max_syn_retries = 10;
533 cfg->max_data_retries = 10;
534 cfg->tcp_syn_ack_timeout = 1; /* seconds */
535 cfg->tcp_ack_timeout = 1; /* seconds */
538 iwl_mvm_build_tcp_packet(
539 vif, tcp, cfg->syn_tx.data, NULL,
540 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
542 cfg->syn_tx.info.tcp_payload_length = 0;
545 iwl_mvm_build_tcp_packet(
546 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
547 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
549 cfg->synack_rx.info.tcp_payload_length = 0;
551 /* KEEPALIVE/ACK (TX) */
552 iwl_mvm_build_tcp_packet(
553 vif, tcp, cfg->keepalive_tx.data, NULL,
554 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
556 cfg->keepalive_tx.info.tcp_payload_length =
557 cpu_to_le16(tcp->payload_len);
558 cfg->sequence_number_offset = tcp->payload_seq.offset;
559 /* length must be 0..4, the field is little endian */
560 cfg->sequence_number_length = tcp->payload_seq.len;
561 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
562 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
563 if (tcp->payload_tok.len) {
564 cfg->token_offset = tcp->payload_tok.offset;
565 cfg->token_length = tcp->payload_tok.len;
567 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
568 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
571 /* set tokens to max value to almost never run out */
572 cfg->num_tokens = cpu_to_le16(65535);
576 iwl_mvm_build_tcp_packet(
577 vif, tcp, cfg->keepalive_ack_rx.data,
578 cfg->keepalive_ack_rx.rx_mask,
579 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
581 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
584 iwl_mvm_build_tcp_packet(
585 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
586 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
588 cfg->wake_rx.info.tcp_payload_length =
589 cpu_to_le16(tcp->wake_len);
592 iwl_mvm_build_tcp_packet(
593 vif, tcp, cfg->fin_tx.data, NULL,
594 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
596 cfg->fin_tx.info.tcp_payload_length = 0;
598 ret = iwl_mvm_send_cmd(mvm, &cmd);
604 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
605 struct ieee80211_sta *ap_sta)
607 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
608 struct ieee80211_chanctx_conf *ctx;
609 u8 chains_static, chains_dynamic;
610 struct cfg80211_chan_def chandef;
612 struct iwl_binding_cmd binding_cmd = {};
613 struct iwl_time_quota_cmd quota_cmd = {};
616 /* add back the PHY */
617 if (WARN_ON(!mvmvif->phy_ctxt))
621 ctx = rcu_dereference(vif->chanctx_conf);
627 chains_static = ctx->rx_chains_static;
628 chains_dynamic = ctx->rx_chains_dynamic;
631 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
632 chains_static, chains_dynamic);
636 /* add back the MAC */
637 mvmvif->uploaded = false;
639 if (WARN_ON(!vif->bss_conf.assoc))
642 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
646 /* add back binding - XXX refactor? */
647 binding_cmd.id_and_color =
648 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
649 mvmvif->phy_ctxt->color));
650 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
652 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
653 mvmvif->phy_ctxt->color));
654 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
656 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
657 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
660 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
661 sizeof(binding_cmd), &binding_cmd,
664 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
669 IWL_ERR(mvm, "Binding command failed: %u\n", status);
673 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
676 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
678 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
683 quota_cmd.quotas[0].id_and_color =
684 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
685 mvmvif->phy_ctxt->color));
686 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
687 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
689 for (i = 1; i < MAX_BINDINGS; i++)
690 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
692 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
693 sizeof(quota_cmd), "a_cmd);
695 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
700 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
701 struct ieee80211_vif *vif)
703 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
704 struct iwl_nonqos_seq_query_cmd query_cmd = {
705 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
707 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
710 struct iwl_host_cmd cmd = {
711 .id = NON_QOS_TX_COUNTER_CMD,
712 .flags = CMD_WANT_SKB,
717 cmd.data[0] = &query_cmd;
718 cmd.len[0] = sizeof(query_cmd);
720 err = iwl_mvm_send_cmd(mvm, &cmd);
724 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
725 if (size < sizeof(__le16)) {
728 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
729 /* firmware returns next, not last-used seqno */
730 err = (u16) (err - 0x10);
737 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
739 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
740 struct iwl_nonqos_seq_query_cmd query_cmd = {
741 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
743 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
745 .value = cpu_to_le16(mvmvif->seqno),
748 /* return if called during restart, not resume from D3 */
749 if (!mvmvif->seqno_valid)
752 mvmvif->seqno_valid = false;
754 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
755 sizeof(query_cmd), &query_cmd))
756 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
759 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
761 iwl_mvm_cancel_scan(mvm);
763 iwl_trans_stop_device(mvm->trans);
766 * Set the HW restart bit -- this is mostly true as we're
767 * going to load new firmware and reprogram that, though
768 * the reprogramming is going to be manual to avoid adding
769 * all the MACs that aren't support.
770 * We don't have to clear up everything though because the
771 * reprogramming is manual. When we resume, we'll actually
772 * go through a proper restart sequence again to switch
773 * back to the runtime firmware image.
775 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
777 /* We reprogram keys and shouldn't allocate new key indices */
778 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
785 return iwl_mvm_load_d3_fw(mvm);
789 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
790 struct cfg80211_wowlan *wowlan,
791 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
792 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
793 struct ieee80211_sta *ap_sta)
796 struct iwl_mvm_sta *mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
798 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
800 wowlan_config_cmd->is_11n_connection =
801 ap_sta->ht_cap.ht_supported;
803 /* Query the last used seqno and set it */
804 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
808 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
810 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
812 if (wowlan->disconnect)
813 wowlan_config_cmd->wakeup_filter |=
814 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
815 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
816 if (wowlan->magic_pkt)
817 wowlan_config_cmd->wakeup_filter |=
818 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
819 if (wowlan->gtk_rekey_failure)
820 wowlan_config_cmd->wakeup_filter |=
821 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
822 if (wowlan->eap_identity_req)
823 wowlan_config_cmd->wakeup_filter |=
824 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
825 if (wowlan->four_way_handshake)
826 wowlan_config_cmd->wakeup_filter |=
827 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
828 if (wowlan->n_patterns)
829 wowlan_config_cmd->wakeup_filter |=
830 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
832 if (wowlan->rfkill_release)
833 wowlan_config_cmd->wakeup_filter |=
834 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
838 * Set the "link change" (really "link lost") flag as well
839 * since that implies losing the TCP connection.
841 wowlan_config_cmd->wakeup_filter |=
842 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
843 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
844 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
845 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
852 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
853 struct cfg80211_wowlan *wowlan,
854 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
855 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
856 struct ieee80211_sta *ap_sta)
858 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
859 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
860 struct wowlan_key_data key_data = {
861 .use_rsc_tsc = false,
867 ret = iwl_mvm_switch_to_d3(mvm);
871 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
875 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
876 if (!key_data.rsc_tsc)
879 if (!iwlwifi_mod_params.sw_crypto) {
881 * This needs to be unlocked due to lock ordering
882 * constraints. Since we're in the suspend path
883 * that isn't really a problem though.
885 mutex_unlock(&mvm->mutex);
886 ieee80211_iter_keys(mvm->hw, vif,
887 iwl_mvm_wowlan_program_keys,
889 mutex_lock(&mvm->mutex);
890 if (key_data.error) {
895 if (key_data.use_rsc_tsc) {
896 struct iwl_host_cmd rsc_tsc_cmd = {
897 .id = WOWLAN_TSC_RSC_PARAM,
898 .data[0] = key_data.rsc_tsc,
899 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
900 .len[0] = sizeof(*key_data.rsc_tsc),
903 ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
908 if (key_data.use_tkip) {
909 ret = iwl_mvm_send_cmd_pdu(mvm,
917 if (mvmvif->rekey_data.valid) {
918 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
919 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
921 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
922 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
924 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
925 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
927 ret = iwl_mvm_send_cmd_pdu(mvm,
928 WOWLAN_KEK_KCK_MATERIAL, 0,
936 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
937 sizeof(*wowlan_config_cmd),
942 ret = iwl_mvm_send_patterns(mvm, wowlan);
946 ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
950 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
953 kfree(key_data.rsc_tsc);
958 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
959 struct cfg80211_wowlan *wowlan,
960 struct cfg80211_sched_scan_request *nd_config,
961 struct ieee80211_vif *vif)
963 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
966 ret = iwl_mvm_switch_to_d3(mvm);
970 /* rfkill release can be either for wowlan or netdetect */
971 if (wowlan->rfkill_release)
972 wowlan_config_cmd.wakeup_filter |=
973 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
975 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
976 sizeof(wowlan_config_cmd),
981 ret = iwl_mvm_scan_offload_start(mvm, vif, nd_config, &mvm->nd_ies);
985 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
988 /* save the sched scan matchsets... */
989 if (nd_config->n_match_sets) {
990 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
991 sizeof(*nd_config->match_sets) *
992 nd_config->n_match_sets,
994 if (mvm->nd_match_sets)
995 mvm->n_nd_match_sets = nd_config->n_match_sets;
998 /* ...and the sched scan channels for later reporting */
999 mvm->nd_channels = kmemdup(nd_config->channels,
1000 sizeof(*nd_config->channels) *
1001 nd_config->n_channels,
1003 if (mvm->nd_channels)
1004 mvm->n_nd_channels = nd_config->n_channels;
1009 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1011 kfree(mvm->nd_match_sets);
1012 mvm->nd_match_sets = NULL;
1013 mvm->n_nd_match_sets = 0;
1014 kfree(mvm->nd_channels);
1015 mvm->nd_channels = NULL;
1016 mvm->n_nd_channels = 0;
1019 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1020 struct cfg80211_wowlan *wowlan,
1023 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1024 struct ieee80211_vif *vif = NULL;
1025 struct iwl_mvm_vif *mvmvif = NULL;
1026 struct ieee80211_sta *ap_sta = NULL;
1027 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1029 * Program the minimum sleep time to 10 seconds, as many
1030 * platforms have issues processing a wakeup signal while
1031 * still being in the process of suspending.
1033 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1035 struct iwl_host_cmd d3_cfg_cmd = {
1036 .id = D3_CONFIG_CMD,
1037 .flags = CMD_WANT_SKB,
1038 .data[0] = &d3_cfg_cmd_data,
1039 .len[0] = sizeof(d3_cfg_cmd_data),
1042 int len __maybe_unused;
1046 * mac80211 shouldn't get here, but for D3 test
1047 * it doesn't warrant a warning
1053 mutex_lock(&mvm->mutex);
1055 vif = iwl_mvm_get_bss_vif(mvm);
1056 if (IS_ERR_OR_NULL(vif)) {
1061 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1063 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
1064 /* if we're not associated, this must be netdetect */
1065 if (!wowlan->nd_config && !mvm->nd_config) {
1070 ret = iwl_mvm_netdetect_config(
1071 mvm, wowlan, wowlan->nd_config ?: mvm->nd_config, vif);
1075 mvm->net_detect = true;
1077 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1079 ap_sta = rcu_dereference_protected(
1080 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1081 lockdep_is_held(&mvm->mutex));
1082 if (IS_ERR_OR_NULL(ap_sta)) {
1087 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1088 vif, mvmvif, ap_sta);
1091 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1092 vif, mvmvif, ap_sta);
1096 mvm->net_detect = false;
1099 ret = iwl_mvm_power_update_device(mvm);
1103 ret = iwl_mvm_power_update_mac(mvm);
1107 #ifdef CONFIG_IWLWIFI_DEBUGFS
1108 if (mvm->d3_wake_sysassert)
1109 d3_cfg_cmd_data.wakeup_flags |=
1110 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1113 /* must be last -- this switches firmware state */
1114 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1117 #ifdef CONFIG_IWLWIFI_DEBUGFS
1118 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1119 if (len >= sizeof(u32)) {
1120 mvm->d3_test_pme_ptr =
1121 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1124 iwl_free_resp(&d3_cfg_cmd);
1126 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1128 iwl_trans_d3_suspend(mvm->trans, test);
1131 ieee80211_restart_hw(mvm->hw);
1132 iwl_mvm_free_nd(mvm);
1135 mutex_unlock(&mvm->mutex);
1140 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1142 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1144 iwl_trans_suspend(mvm->trans);
1145 if (iwl_mvm_is_d0i3_supported(mvm)) {
1146 mutex_lock(&mvm->d0i3_suspend_mutex);
1147 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1148 mutex_unlock(&mvm->d0i3_suspend_mutex);
1152 return __iwl_mvm_suspend(hw, wowlan, false);
1155 /* converted data from the different status responses */
1156 struct iwl_wowlan_status_data {
1160 u32 wake_packet_length;
1161 u32 wake_packet_bufsize;
1162 const u8 *wake_packet;
1165 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1166 struct ieee80211_vif *vif,
1167 struct iwl_wowlan_status_data *status)
1169 struct sk_buff *pkt = NULL;
1170 struct cfg80211_wowlan_wakeup wakeup = {
1173 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1174 u32 reasons = status->wakeup_reasons;
1176 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1177 wakeup_report = NULL;
1181 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1182 wakeup.magic_pkt = true;
1184 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1185 wakeup.pattern_idx =
1186 status->pattern_number;
1188 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1189 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1190 wakeup.disconnect = true;
1192 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1193 wakeup.gtk_rekey_failure = true;
1195 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1196 wakeup.rfkill_release = true;
1198 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1199 wakeup.eap_identity_req = true;
1201 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1202 wakeup.four_way_handshake = true;
1204 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1205 wakeup.tcp_connlost = true;
1207 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1208 wakeup.tcp_nomoretokens = true;
1210 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1211 wakeup.tcp_match = true;
1213 if (status->wake_packet_bufsize) {
1214 int pktsize = status->wake_packet_bufsize;
1215 int pktlen = status->wake_packet_length;
1216 const u8 *pktdata = status->wake_packet;
1217 struct ieee80211_hdr *hdr = (void *)pktdata;
1218 int truncated = pktlen - pktsize;
1220 /* this would be a firmware bug */
1221 if (WARN_ON_ONCE(truncated < 0))
1224 if (ieee80211_is_data(hdr->frame_control)) {
1225 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1226 int ivlen = 0, icvlen = 4; /* also FCS */
1228 pkt = alloc_skb(pktsize, GFP_KERNEL);
1232 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1236 if (ieee80211_has_protected(hdr->frame_control)) {
1238 * This is unlocked and using gtk_i(c)vlen,
1239 * but since everything is under RTNL still
1240 * that's not really a problem - changing
1241 * it would be difficult.
1243 if (is_multicast_ether_addr(hdr->addr1)) {
1244 ivlen = mvm->gtk_ivlen;
1245 icvlen += mvm->gtk_icvlen;
1247 ivlen = mvm->ptk_ivlen;
1248 icvlen += mvm->ptk_icvlen;
1252 /* if truncated, FCS/ICV is (partially) gone */
1253 if (truncated >= icvlen) {
1255 truncated -= icvlen;
1257 icvlen -= truncated;
1261 pktsize -= ivlen + icvlen;
1264 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1266 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1268 wakeup.packet = pkt->data;
1269 wakeup.packet_present_len = pkt->len;
1270 wakeup.packet_len = pkt->len - truncated;
1271 wakeup.packet_80211 = false;
1275 if (truncated >= 4) {
1279 fcslen -= truncated;
1283 wakeup.packet = status->wake_packet;
1284 wakeup.packet_present_len = pktsize;
1285 wakeup.packet_len = pktlen - truncated;
1286 wakeup.packet_80211 = true;
1291 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1295 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1296 struct ieee80211_key_seq *seq)
1300 pn = le64_to_cpu(sc->pn);
1301 seq->ccmp.pn[0] = pn >> 40;
1302 seq->ccmp.pn[1] = pn >> 32;
1303 seq->ccmp.pn[2] = pn >> 24;
1304 seq->ccmp.pn[3] = pn >> 16;
1305 seq->ccmp.pn[4] = pn >> 8;
1306 seq->ccmp.pn[5] = pn;
1309 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1310 struct ieee80211_key_seq *seq)
1312 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1313 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1316 static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1317 struct ieee80211_key_conf *key)
1321 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1323 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1324 struct ieee80211_key_seq seq = {};
1326 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1327 ieee80211_set_key_rx_seq(key, tid, &seq);
1331 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1332 struct ieee80211_key_conf *key)
1336 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1338 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1339 struct ieee80211_key_seq seq = {};
1341 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1342 ieee80211_set_key_rx_seq(key, tid, &seq);
1346 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1347 struct iwl_wowlan_status *status)
1349 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1351 switch (key->cipher) {
1352 case WLAN_CIPHER_SUITE_CCMP:
1353 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1355 case WLAN_CIPHER_SUITE_TKIP:
1356 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1363 struct iwl_mvm_d3_gtk_iter_data {
1364 struct iwl_wowlan_status *status;
1367 bool find_phase, unhandled_cipher;
1371 static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
1372 struct ieee80211_vif *vif,
1373 struct ieee80211_sta *sta,
1374 struct ieee80211_key_conf *key,
1377 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1379 if (data->unhandled_cipher)
1382 switch (key->cipher) {
1383 case WLAN_CIPHER_SUITE_WEP40:
1384 case WLAN_CIPHER_SUITE_WEP104:
1385 /* ignore WEP completely, nothing to do */
1387 case WLAN_CIPHER_SUITE_CCMP:
1388 case WLAN_CIPHER_SUITE_TKIP:
1389 /* we support these */
1392 /* everything else (even CMAC for MFP) - disconnect from AP */
1393 data->unhandled_cipher = true;
1400 * pairwise key - update sequence counters only;
1401 * note that this assumes no TDLS sessions are active
1404 struct ieee80211_key_seq seq = {};
1405 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1407 if (data->find_phase)
1410 switch (key->cipher) {
1411 case WLAN_CIPHER_SUITE_CCMP:
1412 iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
1413 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
1415 case WLAN_CIPHER_SUITE_TKIP:
1416 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1417 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1420 ieee80211_set_key_tx_seq(key, &seq);
1422 /* that's it for this key */
1426 if (data->find_phase) {
1427 data->last_gtk = key;
1428 data->cipher = key->cipher;
1432 if (data->status->num_of_gtk_rekeys)
1433 ieee80211_remove_key(key);
1434 else if (data->last_gtk == key)
1435 iwl_mvm_set_key_rx_seq(key, data->status);
1438 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1439 struct ieee80211_vif *vif,
1440 struct iwl_wowlan_status *status)
1442 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1443 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1446 u32 disconnection_reasons =
1447 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1448 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1450 if (!status || !vif->bss_conf.bssid)
1453 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1456 /* find last GTK that we used initially, if any */
1457 gtkdata.find_phase = true;
1458 ieee80211_iter_keys(mvm->hw, vif,
1459 iwl_mvm_d3_update_gtks, >kdata);
1460 /* not trying to keep connections with MFP/unhandled ciphers */
1461 if (gtkdata.unhandled_cipher)
1463 if (!gtkdata.num_keys)
1465 if (!gtkdata.last_gtk)
1469 * invalidate all other GTKs that might still exist and update
1470 * the one that we used
1472 gtkdata.find_phase = false;
1473 ieee80211_iter_keys(mvm->hw, vif,
1474 iwl_mvm_d3_update_gtks, >kdata);
1476 if (status->num_of_gtk_rekeys) {
1477 struct ieee80211_key_conf *key;
1479 struct ieee80211_key_conf conf;
1482 .conf.cipher = gtkdata.cipher,
1483 .conf.keyidx = status->gtk.key_index,
1486 switch (gtkdata.cipher) {
1487 case WLAN_CIPHER_SUITE_CCMP:
1488 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1489 memcpy(conf.conf.key, status->gtk.decrypt_key,
1492 case WLAN_CIPHER_SUITE_TKIP:
1493 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1494 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1495 /* leave TX MIC key zeroed, we don't use it anyway */
1496 memcpy(conf.conf.key +
1497 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1498 status->gtk.tkip_mic_key, 8);
1502 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1505 iwl_mvm_set_key_rx_seq(key, status);
1508 if (status->num_of_gtk_rekeys) {
1510 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1511 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1512 (void *)&replay_ctr, GFP_KERNEL);
1516 mvmvif->seqno_valid = true;
1517 /* +0x10 because the set API expects next-to-use, not last-used */
1518 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1523 static struct iwl_wowlan_status *
1524 iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1526 u32 base = mvm->error_event_table;
1527 struct error_table_start {
1528 /* cf. struct iwl_error_event_table */
1532 struct iwl_host_cmd cmd = {
1533 .id = WOWLAN_GET_STATUSES,
1534 .flags = CMD_WANT_SKB,
1536 struct iwl_wowlan_status *status, *fw_status;
1537 int ret, len, status_size;
1539 iwl_trans_read_mem_bytes(mvm->trans, base,
1540 &err_info, sizeof(err_info));
1542 if (err_info.valid) {
1543 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1544 err_info.valid, err_info.error_id);
1545 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1546 struct cfg80211_wowlan_wakeup wakeup = {
1547 .rfkill_release = true,
1549 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1552 return ERR_PTR(-EIO);
1555 /* only for tracing for now */
1556 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1558 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1560 ret = iwl_mvm_send_cmd(mvm, &cmd);
1562 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1563 return ERR_PTR(ret);
1566 /* RF-kill already asserted again... */
1567 if (!cmd.resp_pkt) {
1572 status_size = sizeof(*fw_status);
1574 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1575 if (len < status_size) {
1576 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1581 status = (void *)cmd.resp_pkt->data;
1582 if (len != (status_size +
1583 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1584 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1589 fw_status = kmemdup(status, len, GFP_KERNEL);
1592 iwl_free_resp(&cmd);
1593 return ret ? ERR_PTR(ret) : fw_status;
1596 /* releases the MVM mutex */
1597 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1598 struct ieee80211_vif *vif)
1600 struct iwl_wowlan_status_data status;
1601 struct iwl_wowlan_status *fw_status;
1604 struct ieee80211_sta *ap_sta;
1605 struct iwl_mvm_sta *mvm_ap_sta;
1607 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1608 if (IS_ERR_OR_NULL(fw_status))
1611 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1612 for (i = 0; i < 8; i++)
1613 status.qos_seq_ctr[i] =
1614 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1615 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1616 status.wake_packet_length =
1617 le32_to_cpu(fw_status->wake_packet_length);
1618 status.wake_packet_bufsize =
1619 le32_to_cpu(fw_status->wake_packet_bufsize);
1620 status.wake_packet = fw_status->wake_packet;
1622 /* still at hard-coded place 0 for D3 image */
1623 ap_sta = rcu_dereference_protected(
1624 mvm->fw_id_to_mac_id[0],
1625 lockdep_is_held(&mvm->mutex));
1626 if (IS_ERR_OR_NULL(ap_sta))
1629 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
1630 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1631 u16 seq = status.qos_seq_ctr[i];
1632 /* firmware stores last-used value, we store next value */
1634 mvm_ap_sta->tid_data[i].seq_number = seq;
1637 /* now we have all the data we need, unlock to avoid mac80211 issues */
1638 mutex_unlock(&mvm->mutex);
1640 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1642 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1650 mutex_unlock(&mvm->mutex);
1654 struct iwl_mvm_nd_query_results {
1655 u32 matched_profiles;
1656 struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1660 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1661 struct iwl_mvm_nd_query_results *results)
1663 struct iwl_scan_offload_profiles_query *query;
1664 struct iwl_host_cmd cmd = {
1665 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1666 .flags = CMD_WANT_SKB,
1670 ret = iwl_mvm_send_cmd(mvm, &cmd);
1672 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
1676 /* RF-kill already asserted again... */
1677 if (!cmd.resp_pkt) {
1682 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1683 if (len < sizeof(*query)) {
1684 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
1689 query = (void *)cmd.resp_pkt->data;
1691 results->matched_profiles = le32_to_cpu(query->matched_profiles);
1692 memcpy(results->matches, query->matches, sizeof(results->matches));
1695 iwl_free_resp(&cmd);
1699 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1700 struct ieee80211_vif *vif)
1702 struct cfg80211_wowlan_nd_info *net_detect = NULL;
1703 struct cfg80211_wowlan_wakeup wakeup = {
1706 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1707 struct iwl_mvm_nd_query_results query;
1708 struct iwl_wowlan_status *fw_status;
1709 unsigned long matched_profiles;
1711 int i, j, n_matches, ret;
1713 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1714 if (!IS_ERR_OR_NULL(fw_status))
1715 reasons = le32_to_cpu(fw_status->wakeup_reasons);
1717 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1718 wakeup.rfkill_release = true;
1720 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1723 ret = iwl_mvm_netdetect_query_results(mvm, &query);
1724 if (ret || !query.matched_profiles) {
1725 wakeup_report = NULL;
1729 matched_profiles = query.matched_profiles;
1730 if (mvm->n_nd_match_sets) {
1731 n_matches = hweight_long(matched_profiles);
1733 IWL_ERR(mvm, "no net detect match information available\n");
1737 net_detect = kzalloc(sizeof(*net_detect) +
1738 (n_matches * sizeof(net_detect->matches[0])),
1740 if (!net_detect || !n_matches)
1743 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
1744 struct iwl_scan_offload_profile_match *fw_match;
1745 struct cfg80211_wowlan_nd_match *match;
1748 fw_match = &query.matches[i];
1750 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1751 n_channels += hweight8(fw_match->matching_channels[j]);
1753 match = kzalloc(sizeof(*match) +
1754 (n_channels * sizeof(*match->channels)),
1759 net_detect->matches[net_detect->n_matches++] = match;
1761 match->ssid.ssid_len = mvm->nd_match_sets[i].ssid.ssid_len;
1762 memcpy(match->ssid.ssid, mvm->nd_match_sets[i].ssid.ssid,
1763 match->ssid.ssid_len);
1765 if (mvm->n_nd_channels < n_channels)
1768 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
1769 if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
1770 match->channels[match->n_channels++] =
1771 mvm->nd_channels[j]->center_freq;
1775 wakeup.net_detect = net_detect;
1777 iwl_mvm_free_nd(mvm);
1779 mutex_unlock(&mvm->mutex);
1780 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1783 for (i = 0; i < net_detect->n_matches; i++)
1784 kfree(net_detect->matches[i]);
1789 static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1791 #ifdef CONFIG_IWLWIFI_DEBUGFS
1792 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1793 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1794 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1796 if (!mvm->store_d3_resume_sram)
1799 if (!mvm->d3_resume_sram) {
1800 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1801 if (!mvm->d3_resume_sram)
1805 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1809 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1810 struct ieee80211_vif *vif)
1812 /* skip the one we keep connection on */
1816 if (vif->type == NL80211_IFTYPE_STATION)
1817 ieee80211_resume_disconnect(vif);
1820 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
1822 struct ieee80211_vif *vif = NULL;
1824 enum iwl_d3_status d3_status;
1827 mutex_lock(&mvm->mutex);
1829 /* get the BSS vif pointer again */
1830 vif = iwl_mvm_get_bss_vif(mvm);
1831 if (IS_ERR_OR_NULL(vif))
1834 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
1838 if (d3_status != IWL_D3_STATUS_ALIVE) {
1839 IWL_INFO(mvm, "Device was reset during suspend\n");
1843 /* query SRAM first in case we want event logging */
1844 iwl_mvm_read_d3_sram(mvm);
1846 if (mvm->net_detect) {
1847 iwl_mvm_query_netdetect_reasons(mvm, vif);
1849 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
1850 #ifdef CONFIG_IWLWIFI_DEBUGFS
1852 mvm->keep_vif = vif;
1855 /* has unlocked the mutex, so skip that */
1859 mutex_unlock(&mvm->mutex);
1863 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1864 IEEE80211_IFACE_ITER_NORMAL,
1865 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
1867 /* return 1 to reconfigure the device */
1868 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1869 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
1873 int iwl_mvm_resume(struct ieee80211_hw *hw)
1875 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1877 iwl_trans_resume(mvm->trans);
1879 if (iwl_mvm_is_d0i3_supported(mvm))
1882 return __iwl_mvm_resume(mvm, false);
1885 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1887 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1889 device_set_wakeup_enable(mvm->trans->dev, enabled);
1892 #ifdef CONFIG_IWLWIFI_DEBUGFS
1893 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
1895 struct iwl_mvm *mvm = inode->i_private;
1898 if (mvm->d3_test_active)
1901 file->private_data = inode->i_private;
1903 ieee80211_stop_queues(mvm->hw);
1906 /* start pseudo D3 */
1908 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
1913 ieee80211_wake_queues(mvm->hw);
1916 mvm->d3_test_active = true;
1917 mvm->keep_vif = NULL;
1921 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
1922 size_t count, loff_t *ppos)
1924 struct iwl_mvm *mvm = file->private_data;
1928 /* read pme_ptr if available */
1929 if (mvm->d3_test_pme_ptr) {
1930 pme_asserted = iwl_trans_read_mem32(mvm->trans,
1931 mvm->d3_test_pme_ptr);
1936 if (msleep_interruptible(100))
1943 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
1944 struct ieee80211_vif *vif)
1946 /* skip the one we keep connection on */
1950 if (vif->type == NL80211_IFTYPE_STATION)
1951 ieee80211_connection_loss(vif);
1954 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
1956 struct iwl_mvm *mvm = inode->i_private;
1957 int remaining_time = 10;
1959 mvm->d3_test_active = false;
1961 __iwl_mvm_resume(mvm, true);
1963 iwl_abort_notification_waits(&mvm->notif_wait);
1964 ieee80211_restart_hw(mvm->hw);
1966 /* wait for restart and disconnect all interfaces */
1967 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1968 remaining_time > 0) {
1973 if (remaining_time == 0)
1974 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
1976 ieee80211_iterate_active_interfaces_atomic(
1977 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1978 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
1980 ieee80211_wake_queues(mvm->hw);
1985 const struct file_operations iwl_dbgfs_d3_test_ops = {
1986 .llseek = no_llseek,
1987 .open = iwl_mvm_d3_test_open,
1988 .read = iwl_mvm_d3_test_read,
1989 .release = iwl_mvm_d3_test_release,