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.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * The full GNU General Public License is included in this distribution
25 * in the file called COPYING.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *****************************************************************************/
64 #include <linux/etherdevice.h>
67 #include <net/cfg80211.h>
70 #include <net/addrconf.h>
71 #include "iwl-modparams.h"
75 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
76 struct ieee80211_vif *vif,
77 struct cfg80211_gtk_rekey_data *data)
79 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
80 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
82 if (iwlwifi_mod_params.sw_crypto)
85 mutex_lock(&mvm->mutex);
87 memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
88 memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
89 mvmvif->rekey_data.replay_ctr =
90 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
91 mvmvif->rekey_data.valid = true;
93 mutex_unlock(&mvm->mutex);
96 #if IS_ENABLED(CONFIG_IPV6)
97 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
98 struct ieee80211_vif *vif,
99 struct inet6_dev *idev)
101 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
102 struct inet6_ifaddr *ifa;
105 read_lock_bh(&idev->lock);
106 list_for_each_entry(ifa, &idev->addr_list, if_list) {
107 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
109 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
112 read_unlock_bh(&idev->lock);
114 mvmvif->num_target_ipv6_addrs = idx;
118 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
119 struct ieee80211_vif *vif, int idx)
121 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
123 mvmvif->tx_key_idx = idx;
126 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
130 for (i = 0; i < IWL_P1K_SIZE; i++)
131 out[i] = cpu_to_le16(p1k[i]);
134 struct wowlan_key_data {
135 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
136 struct iwl_wowlan_tkip_params_cmd *tkip;
137 bool error, use_rsc_tsc, use_tkip;
141 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
142 struct ieee80211_vif *vif,
143 struct ieee80211_sta *sta,
144 struct ieee80211_key_conf *key,
147 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
148 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
149 struct wowlan_key_data *data = _data;
150 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
151 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
152 struct iwl_p1k_cache *rx_p1ks;
154 struct ieee80211_key_seq seq;
156 u16 p1k[IWL_P1K_SIZE];
159 mutex_lock(&mvm->mutex);
161 switch (key->cipher) {
162 case WLAN_CIPHER_SUITE_WEP40:
163 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
165 struct iwl_mvm_wep_key_cmd wep_key_cmd;
166 struct iwl_mvm_wep_key wep_key;
168 .wep_key_cmd.mac_id_n_color =
169 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
171 .wep_key_cmd.num_keys = 1,
172 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
173 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
174 .wep_key.key_index = key->keyidx,
175 .wep_key.key_size = key->keylen,
179 * This will fail -- the key functions don't set support
180 * pairwise WEP keys. However, that's better than silently
181 * failing WoWLAN. Or maybe not?
183 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
186 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
187 if (key->keyidx == mvmvif->tx_key_idx) {
188 /* TX key must be at offset 0 */
189 wkc.wep_key.key_offset = 0;
191 /* others start at 1 */
193 wkc.wep_key.key_offset = data->wep_key_idx;
196 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, CMD_SYNC,
198 data->error = ret != 0;
200 mvm->ptk_ivlen = key->iv_len;
201 mvm->ptk_icvlen = key->icv_len;
202 mvm->gtk_ivlen = key->iv_len;
203 mvm->gtk_icvlen = key->icv_len;
205 /* don't upload key again */
211 case WLAN_CIPHER_SUITE_AES_CMAC:
213 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
214 * but we also shouldn't abort suspend due to that. It does have
215 * support for the IGTK key renewal, but doesn't really use the
216 * IGTK for anything. This means we could spuriously wake up or
217 * be deauthenticated, but that was considered acceptable.
220 case WLAN_CIPHER_SUITE_TKIP:
222 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
223 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
225 rx_p1ks = data->tkip->rx_uni;
227 ieee80211_get_key_tx_seq(key, &seq);
228 tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
229 tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
231 ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
232 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
234 memcpy(data->tkip->mic_keys.tx,
235 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
238 rx_mic_key = data->tkip->mic_keys.rx_unicast;
241 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
242 rx_p1ks = data->tkip->rx_multi;
243 rx_mic_key = data->tkip->mic_keys.rx_mcast;
247 * For non-QoS this relies on the fact that both the uCode and
248 * mac80211 use TID 0 (as they need to to avoid replay attacks)
249 * for checking the IV in the frames.
251 for (i = 0; i < IWL_NUM_RSC; i++) {
252 ieee80211_get_key_rx_seq(key, i, &seq);
253 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
254 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
255 /* wrapping isn't allowed, AP must rekey */
256 if (seq.tkip.iv32 > cur_rx_iv32)
257 cur_rx_iv32 = seq.tkip.iv32;
260 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
262 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
263 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
264 cur_rx_iv32 + 1, p1k);
265 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
268 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
271 data->use_tkip = true;
272 data->use_rsc_tsc = true;
274 case WLAN_CIPHER_SUITE_CCMP:
276 u8 *pn = seq.ccmp.pn;
278 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
279 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
281 ieee80211_get_key_tx_seq(key, &seq);
282 aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
289 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
293 * For non-QoS this relies on the fact that both the uCode and
294 * mac80211 use TID 0 for checking the IV in the frames.
296 for (i = 0; i < IWL_NUM_RSC; i++) {
297 u8 *pn = seq.ccmp.pn;
299 ieee80211_get_key_rx_seq(key, i, &seq);
300 aes_sc->pn = cpu_to_le64((u64)pn[5] |
307 data->use_rsc_tsc = true;
312 * The D3 firmware hardcodes the key offset 0 as the key it uses
313 * to transmit packets to the AP, i.e. the PTK.
315 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
317 mvm->ptk_ivlen = key->iv_len;
318 mvm->ptk_icvlen = key->icv_len;
321 * firmware only supports TSC/RSC for a single key,
322 * so if there are multiple keep overwriting them
323 * with new ones -- this relies on mac80211 doing
327 mvm->gtk_ivlen = key->iv_len;
328 mvm->gtk_icvlen = key->icv_len;
331 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
332 data->error = ret != 0;
334 mutex_unlock(&mvm->mutex);
337 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
338 struct cfg80211_wowlan *wowlan)
340 struct iwl_wowlan_patterns_cmd *pattern_cmd;
341 struct iwl_host_cmd cmd = {
342 .id = WOWLAN_PATTERNS,
343 .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, },
528 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
533 cfg->max_syn_retries = 10;
534 cfg->max_data_retries = 10;
535 cfg->tcp_syn_ack_timeout = 1; /* seconds */
536 cfg->tcp_ack_timeout = 1; /* seconds */
539 iwl_mvm_build_tcp_packet(
540 vif, tcp, cfg->syn_tx.data, NULL,
541 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
543 cfg->syn_tx.info.tcp_payload_length = 0;
546 iwl_mvm_build_tcp_packet(
547 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
548 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
550 cfg->synack_rx.info.tcp_payload_length = 0;
552 /* KEEPALIVE/ACK (TX) */
553 iwl_mvm_build_tcp_packet(
554 vif, tcp, cfg->keepalive_tx.data, NULL,
555 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
557 cfg->keepalive_tx.info.tcp_payload_length =
558 cpu_to_le16(tcp->payload_len);
559 cfg->sequence_number_offset = tcp->payload_seq.offset;
560 /* length must be 0..4, the field is little endian */
561 cfg->sequence_number_length = tcp->payload_seq.len;
562 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
563 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
564 if (tcp->payload_tok.len) {
565 cfg->token_offset = tcp->payload_tok.offset;
566 cfg->token_length = tcp->payload_tok.len;
568 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
569 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
572 /* set tokens to max value to almost never run out */
573 cfg->num_tokens = cpu_to_le16(65535);
577 iwl_mvm_build_tcp_packet(
578 vif, tcp, cfg->keepalive_ack_rx.data,
579 cfg->keepalive_ack_rx.rx_mask,
580 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
582 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
585 iwl_mvm_build_tcp_packet(
586 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
587 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
589 cfg->wake_rx.info.tcp_payload_length =
590 cpu_to_le16(tcp->wake_len);
593 iwl_mvm_build_tcp_packet(
594 vif, tcp, cfg->fin_tx.data, NULL,
595 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
597 cfg->fin_tx.info.tcp_payload_length = 0;
599 ret = iwl_mvm_send_cmd(mvm, &cmd);
605 struct iwl_d3_iter_data {
607 struct ieee80211_vif *vif;
611 static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
612 struct ieee80211_vif *vif)
614 struct iwl_d3_iter_data *data = _data;
615 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
617 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
620 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
624 IWL_ERR(data->mvm, "More than one managed interface active!\n");
632 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
633 struct ieee80211_sta *ap_sta)
635 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
636 struct ieee80211_chanctx_conf *ctx;
637 u8 chains_static, chains_dynamic;
638 struct cfg80211_chan_def chandef;
640 struct iwl_binding_cmd binding_cmd = {};
641 struct iwl_time_quota_cmd quota_cmd = {};
644 /* add back the PHY */
645 if (WARN_ON(!mvmvif->phy_ctxt))
649 ctx = rcu_dereference(vif->chanctx_conf);
655 chains_static = ctx->rx_chains_static;
656 chains_dynamic = ctx->rx_chains_dynamic;
659 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
660 chains_static, chains_dynamic);
664 /* add back the MAC */
665 mvmvif->uploaded = false;
667 if (WARN_ON(!vif->bss_conf.assoc))
670 vif->bss_conf.assoc = false;
671 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
672 vif->bss_conf.assoc = true;
676 /* add back binding - XXX refactor? */
677 binding_cmd.id_and_color =
678 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
679 mvmvif->phy_ctxt->color));
680 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
682 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
683 mvmvif->phy_ctxt->color));
684 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
686 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
687 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
690 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
691 sizeof(binding_cmd), &binding_cmd,
694 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
699 IWL_ERR(mvm, "Binding command failed: %u\n", status);
703 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
706 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
708 ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
713 quota_cmd.quotas[0].id_and_color =
714 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
715 mvmvif->phy_ctxt->color));
716 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
717 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
719 for (i = 1; i < MAX_BINDINGS; i++)
720 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
722 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, CMD_SYNC,
723 sizeof(quota_cmd), "a_cmd);
725 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
730 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
731 struct ieee80211_vif *vif)
733 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
734 struct iwl_nonqos_seq_query_cmd query_cmd = {
735 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
737 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
740 struct iwl_host_cmd cmd = {
741 .id = NON_QOS_TX_COUNTER_CMD,
742 .flags = CMD_SYNC | CMD_WANT_SKB,
747 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_D3_CONTINUITY_API) {
748 cmd.data[0] = &query_cmd;
749 cmd.len[0] = sizeof(query_cmd);
752 err = iwl_mvm_send_cmd(mvm, &cmd);
756 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
757 if (size < sizeof(__le16)) {
760 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
761 /* new API returns next, not last-used seqno */
762 if (mvm->fw->ucode_capa.flags &
763 IWL_UCODE_TLV_FLAGS_D3_CONTINUITY_API)
764 err = (u16) (err - 0x10);
771 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
773 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
774 struct iwl_nonqos_seq_query_cmd query_cmd = {
775 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
777 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
779 .value = cpu_to_le16(mvmvif->seqno),
782 /* return if called during restart, not resume from D3 */
783 if (!mvmvif->seqno_valid)
786 mvmvif->seqno_valid = false;
788 if (!(mvm->fw->ucode_capa.flags &
789 IWL_UCODE_TLV_FLAGS_D3_CONTINUITY_API))
792 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, CMD_SYNC,
793 sizeof(query_cmd), &query_cmd))
794 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
798 iwl_mvm_send_wowlan_config_cmd(struct iwl_mvm *mvm,
799 const struct iwl_wowlan_config_cmd_v3 *cmd)
801 /* start only with the v2 part of the command */
802 u16 cmd_len = sizeof(cmd->common);
804 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_WOWLAN_CONFIG_TID)
805 cmd_len = sizeof(*cmd);
807 return iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, CMD_SYNC,
811 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
812 struct cfg80211_wowlan *wowlan,
815 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
816 struct iwl_d3_iter_data suspend_iter_data = {
819 struct ieee80211_vif *vif;
820 struct iwl_mvm_vif *mvmvif;
821 struct ieee80211_sta *ap_sta;
822 struct iwl_mvm_sta *mvm_ap_sta;
823 struct iwl_wowlan_config_cmd_v3 wowlan_config_cmd = {};
824 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
825 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
826 struct iwl_d3_manager_config d3_cfg_cmd_data = {
828 * Program the minimum sleep time to 10 seconds, as many
829 * platforms have issues processing a wakeup signal while
830 * still being in the process of suspending.
832 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
834 struct iwl_host_cmd d3_cfg_cmd = {
836 .flags = CMD_SYNC | CMD_WANT_SKB,
837 .data[0] = &d3_cfg_cmd_data,
838 .len[0] = sizeof(d3_cfg_cmd_data),
840 struct wowlan_key_data key_data = {
841 .use_rsc_tsc = false,
846 int len __maybe_unused;
850 * mac80211 shouldn't get here, but for D3 test
851 * it doesn't warrant a warning
857 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
858 if (!key_data.rsc_tsc)
861 mutex_lock(&mvm->mutex);
863 /* see if there's only a single BSS vif and it's associated */
864 ieee80211_iterate_active_interfaces_atomic(
865 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
866 iwl_mvm_d3_iface_iterator, &suspend_iter_data);
868 if (suspend_iter_data.error || !suspend_iter_data.vif) {
873 vif = suspend_iter_data.vif;
874 mvmvif = iwl_mvm_vif_from_mac80211(vif);
876 ap_sta = rcu_dereference_protected(
877 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
878 lockdep_is_held(&mvm->mutex));
879 if (IS_ERR_OR_NULL(ap_sta)) {
884 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
886 /* TODO: wowlan_config_cmd.common.wowlan_ba_teardown_tids */
888 wowlan_config_cmd.common.is_11n_connection =
889 ap_sta->ht_cap.ht_supported;
891 /* Query the last used seqno and set it */
892 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
895 wowlan_config_cmd.common.non_qos_seq = cpu_to_le16(ret);
897 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, &wowlan_config_cmd.common);
899 if (wowlan->disconnect)
900 wowlan_config_cmd.common.wakeup_filter |=
901 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
902 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
903 if (wowlan->magic_pkt)
904 wowlan_config_cmd.common.wakeup_filter |=
905 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
906 if (wowlan->gtk_rekey_failure)
907 wowlan_config_cmd.common.wakeup_filter |=
908 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
909 if (wowlan->eap_identity_req)
910 wowlan_config_cmd.common.wakeup_filter |=
911 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
912 if (wowlan->four_way_handshake)
913 wowlan_config_cmd.common.wakeup_filter |=
914 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
915 if (wowlan->n_patterns)
916 wowlan_config_cmd.common.wakeup_filter |=
917 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
919 if (wowlan->rfkill_release)
920 wowlan_config_cmd.common.wakeup_filter |=
921 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
925 * Set the "link change" (really "link lost") flag as well
926 * since that implies losing the TCP connection.
928 wowlan_config_cmd.common.wakeup_filter |=
929 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
930 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
931 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
932 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
935 iwl_mvm_cancel_scan(mvm);
937 iwl_trans_stop_device(mvm->trans);
940 * Set the HW restart bit -- this is mostly true as we're
941 * going to load new firmware and reprogram that, though
942 * the reprogramming is going to be manual to avoid adding
943 * all the MACs that aren't support.
944 * We don't have to clear up everything though because the
945 * reprogramming is manual. When we resume, we'll actually
946 * go through a proper restart sequence again to switch
947 * back to the runtime firmware image.
949 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
951 /* We reprogram keys and shouldn't allocate new key indices */
952 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
959 ret = iwl_mvm_load_d3_fw(mvm);
963 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
967 if (!iwlwifi_mod_params.sw_crypto) {
969 * This needs to be unlocked due to lock ordering
970 * constraints. Since we're in the suspend path
971 * that isn't really a problem though.
973 mutex_unlock(&mvm->mutex);
974 ieee80211_iter_keys(mvm->hw, vif,
975 iwl_mvm_wowlan_program_keys,
977 mutex_lock(&mvm->mutex);
978 if (key_data.error) {
983 if (key_data.use_rsc_tsc) {
984 struct iwl_host_cmd rsc_tsc_cmd = {
985 .id = WOWLAN_TSC_RSC_PARAM,
987 .data[0] = key_data.rsc_tsc,
988 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
989 .len[0] = sizeof(*key_data.rsc_tsc),
992 ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
997 if (key_data.use_tkip) {
998 ret = iwl_mvm_send_cmd_pdu(mvm,
1000 CMD_SYNC, sizeof(tkip_cmd),
1006 if (mvmvif->rekey_data.valid) {
1007 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
1008 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1010 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
1011 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1013 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1014 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1016 ret = iwl_mvm_send_cmd_pdu(mvm,
1017 WOWLAN_KEK_KCK_MATERIAL,
1019 sizeof(kek_kck_cmd),
1026 ret = iwl_mvm_send_wowlan_config_cmd(mvm, &wowlan_config_cmd);
1030 ret = iwl_mvm_send_patterns(mvm, wowlan);
1034 ret = iwl_mvm_send_proto_offload(mvm, vif, false, CMD_SYNC);
1038 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1042 ret = iwl_mvm_power_update_device(mvm);
1046 ret = iwl_mvm_power_update_mac(mvm, vif);
1050 #ifdef CONFIG_IWLWIFI_DEBUGFS
1051 if (mvm->d3_wake_sysassert)
1052 d3_cfg_cmd_data.wakeup_flags |=
1053 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1056 /* must be last -- this switches firmware state */
1057 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1060 #ifdef CONFIG_IWLWIFI_DEBUGFS
1061 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1062 if (len >= sizeof(u32)) {
1063 mvm->d3_test_pme_ptr =
1064 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1067 iwl_free_resp(&d3_cfg_cmd);
1069 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1071 iwl_trans_d3_suspend(mvm->trans, test);
1074 ieee80211_restart_hw(mvm->hw);
1076 kfree(key_data.rsc_tsc);
1078 mutex_unlock(&mvm->mutex);
1083 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1085 return __iwl_mvm_suspend(hw, wowlan, false);
1088 /* converted data from the different status responses */
1089 struct iwl_wowlan_status_data {
1093 u32 wake_packet_length;
1094 u32 wake_packet_bufsize;
1095 const u8 *wake_packet;
1098 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1099 struct ieee80211_vif *vif,
1100 struct iwl_wowlan_status_data *status)
1102 struct sk_buff *pkt = NULL;
1103 struct cfg80211_wowlan_wakeup wakeup = {
1106 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1107 u32 reasons = status->wakeup_reasons;
1109 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1110 wakeup_report = NULL;
1114 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1115 wakeup.magic_pkt = true;
1117 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1118 wakeup.pattern_idx =
1119 status->pattern_number;
1121 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1122 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1123 wakeup.disconnect = true;
1125 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1126 wakeup.gtk_rekey_failure = true;
1128 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1129 wakeup.rfkill_release = true;
1131 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1132 wakeup.eap_identity_req = true;
1134 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1135 wakeup.four_way_handshake = true;
1137 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1138 wakeup.tcp_connlost = true;
1140 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1141 wakeup.tcp_nomoretokens = true;
1143 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1144 wakeup.tcp_match = true;
1146 if (status->wake_packet_bufsize) {
1147 int pktsize = status->wake_packet_bufsize;
1148 int pktlen = status->wake_packet_length;
1149 const u8 *pktdata = status->wake_packet;
1150 struct ieee80211_hdr *hdr = (void *)pktdata;
1151 int truncated = pktlen - pktsize;
1153 /* this would be a firmware bug */
1154 if (WARN_ON_ONCE(truncated < 0))
1157 if (ieee80211_is_data(hdr->frame_control)) {
1158 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1159 int ivlen = 0, icvlen = 4; /* also FCS */
1161 pkt = alloc_skb(pktsize, GFP_KERNEL);
1165 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1169 if (ieee80211_has_protected(hdr->frame_control)) {
1171 * This is unlocked and using gtk_i(c)vlen,
1172 * but since everything is under RTNL still
1173 * that's not really a problem - changing
1174 * it would be difficult.
1176 if (is_multicast_ether_addr(hdr->addr1)) {
1177 ivlen = mvm->gtk_ivlen;
1178 icvlen += mvm->gtk_icvlen;
1180 ivlen = mvm->ptk_ivlen;
1181 icvlen += mvm->ptk_icvlen;
1185 /* if truncated, FCS/ICV is (partially) gone */
1186 if (truncated >= icvlen) {
1188 truncated -= icvlen;
1190 icvlen -= truncated;
1194 pktsize -= ivlen + icvlen;
1197 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1199 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1201 wakeup.packet = pkt->data;
1202 wakeup.packet_present_len = pkt->len;
1203 wakeup.packet_len = pkt->len - truncated;
1204 wakeup.packet_80211 = false;
1208 if (truncated >= 4) {
1212 fcslen -= truncated;
1216 wakeup.packet = status->wake_packet;
1217 wakeup.packet_present_len = pktsize;
1218 wakeup.packet_len = pktlen - truncated;
1219 wakeup.packet_80211 = true;
1224 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1228 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1229 struct ieee80211_key_seq *seq)
1233 pn = le64_to_cpu(sc->pn);
1234 seq->ccmp.pn[0] = pn >> 40;
1235 seq->ccmp.pn[1] = pn >> 32;
1236 seq->ccmp.pn[2] = pn >> 24;
1237 seq->ccmp.pn[3] = pn >> 16;
1238 seq->ccmp.pn[4] = pn >> 8;
1239 seq->ccmp.pn[5] = pn;
1242 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1243 struct ieee80211_key_seq *seq)
1245 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1246 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1249 static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1250 struct ieee80211_key_conf *key)
1254 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1256 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1257 struct ieee80211_key_seq seq = {};
1259 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1260 ieee80211_set_key_rx_seq(key, tid, &seq);
1264 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1265 struct ieee80211_key_conf *key)
1269 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1271 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1272 struct ieee80211_key_seq seq = {};
1274 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1275 ieee80211_set_key_rx_seq(key, tid, &seq);
1279 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1280 struct iwl_wowlan_status_v6 *status)
1282 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1284 switch (key->cipher) {
1285 case WLAN_CIPHER_SUITE_CCMP:
1286 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1288 case WLAN_CIPHER_SUITE_TKIP:
1289 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1296 struct iwl_mvm_d3_gtk_iter_data {
1297 struct iwl_wowlan_status_v6 *status;
1300 bool find_phase, unhandled_cipher;
1304 static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
1305 struct ieee80211_vif *vif,
1306 struct ieee80211_sta *sta,
1307 struct ieee80211_key_conf *key,
1310 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1312 if (data->unhandled_cipher)
1315 switch (key->cipher) {
1316 case WLAN_CIPHER_SUITE_WEP40:
1317 case WLAN_CIPHER_SUITE_WEP104:
1318 /* ignore WEP completely, nothing to do */
1320 case WLAN_CIPHER_SUITE_CCMP:
1321 case WLAN_CIPHER_SUITE_TKIP:
1322 /* we support these */
1325 /* everything else (even CMAC for MFP) - disconnect from AP */
1326 data->unhandled_cipher = true;
1333 * pairwise key - update sequence counters only;
1334 * note that this assumes no TDLS sessions are active
1337 struct ieee80211_key_seq seq = {};
1338 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1340 if (data->find_phase)
1343 switch (key->cipher) {
1344 case WLAN_CIPHER_SUITE_CCMP:
1345 iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
1346 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
1348 case WLAN_CIPHER_SUITE_TKIP:
1349 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1350 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1353 ieee80211_set_key_tx_seq(key, &seq);
1355 /* that's it for this key */
1359 if (data->find_phase) {
1360 data->last_gtk = key;
1361 data->cipher = key->cipher;
1365 if (data->status->num_of_gtk_rekeys)
1366 ieee80211_remove_key(key);
1367 else if (data->last_gtk == key)
1368 iwl_mvm_set_key_rx_seq(key, data->status);
1371 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1372 struct ieee80211_vif *vif,
1373 struct iwl_wowlan_status_v6 *status)
1375 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1376 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1379 u32 disconnection_reasons =
1380 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1381 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1383 if (!status || !vif->bss_conf.bssid)
1386 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1389 /* find last GTK that we used initially, if any */
1390 gtkdata.find_phase = true;
1391 ieee80211_iter_keys(mvm->hw, vif,
1392 iwl_mvm_d3_update_gtks, >kdata);
1393 /* not trying to keep connections with MFP/unhandled ciphers */
1394 if (gtkdata.unhandled_cipher)
1396 if (!gtkdata.num_keys)
1398 if (!gtkdata.last_gtk)
1402 * invalidate all other GTKs that might still exist and update
1403 * the one that we used
1405 gtkdata.find_phase = false;
1406 ieee80211_iter_keys(mvm->hw, vif,
1407 iwl_mvm_d3_update_gtks, >kdata);
1409 if (status->num_of_gtk_rekeys) {
1410 struct ieee80211_key_conf *key;
1412 struct ieee80211_key_conf conf;
1415 .conf.cipher = gtkdata.cipher,
1416 .conf.keyidx = status->gtk.key_index,
1419 switch (gtkdata.cipher) {
1420 case WLAN_CIPHER_SUITE_CCMP:
1421 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1422 memcpy(conf.conf.key, status->gtk.decrypt_key,
1425 case WLAN_CIPHER_SUITE_TKIP:
1426 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1427 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1428 /* leave TX MIC key zeroed, we don't use it anyway */
1429 memcpy(conf.conf.key +
1430 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1431 status->gtk.tkip_mic_key, 8);
1435 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1438 iwl_mvm_set_key_rx_seq(key, status);
1441 if (status->num_of_gtk_rekeys) {
1443 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1444 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1445 (void *)&replay_ctr, GFP_KERNEL);
1449 mvmvif->seqno_valid = true;
1450 /* +0x10 because the set API expects next-to-use, not last-used */
1451 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1456 /* releases the MVM mutex */
1457 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1458 struct ieee80211_vif *vif)
1460 u32 base = mvm->error_event_table;
1461 struct error_table_start {
1462 /* cf. struct iwl_error_event_table */
1466 struct iwl_host_cmd cmd = {
1467 .id = WOWLAN_GET_STATUSES,
1468 .flags = CMD_SYNC | CMD_WANT_SKB,
1470 struct iwl_wowlan_status_data status;
1471 struct iwl_wowlan_status_v6 *status_v6;
1472 int ret, len, status_size, i;
1474 struct ieee80211_sta *ap_sta;
1475 struct iwl_mvm_sta *mvm_ap_sta;
1477 iwl_trans_read_mem_bytes(mvm->trans, base,
1478 &err_info, sizeof(err_info));
1480 if (err_info.valid) {
1481 IWL_INFO(mvm, "error table is valid (%d)\n",
1483 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1484 struct cfg80211_wowlan_wakeup wakeup = {
1485 .rfkill_release = true,
1487 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1493 /* only for tracing for now */
1494 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, CMD_SYNC, 0, NULL);
1496 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1498 ret = iwl_mvm_send_cmd(mvm, &cmd);
1500 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1504 /* RF-kill already asserted again... */
1508 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_D3_CONTINUITY_API)
1509 status_size = sizeof(struct iwl_wowlan_status_v6);
1511 status_size = sizeof(struct iwl_wowlan_status_v4);
1513 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1514 if (len < status_size) {
1515 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1519 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_D3_CONTINUITY_API) {
1520 status_v6 = (void *)cmd.resp_pkt->data;
1522 status.pattern_number = le16_to_cpu(status_v6->pattern_number);
1523 for (i = 0; i < 8; i++)
1524 status.qos_seq_ctr[i] =
1525 le16_to_cpu(status_v6->qos_seq_ctr[i]);
1526 status.wakeup_reasons = le32_to_cpu(status_v6->wakeup_reasons);
1527 status.wake_packet_length =
1528 le32_to_cpu(status_v6->wake_packet_length);
1529 status.wake_packet_bufsize =
1530 le32_to_cpu(status_v6->wake_packet_bufsize);
1531 status.wake_packet = status_v6->wake_packet;
1533 struct iwl_wowlan_status_v4 *status_v4;
1535 status_v4 = (void *)cmd.resp_pkt->data;
1537 status.pattern_number = le16_to_cpu(status_v4->pattern_number);
1538 for (i = 0; i < 8; i++)
1539 status.qos_seq_ctr[i] =
1540 le16_to_cpu(status_v4->qos_seq_ctr[i]);
1541 status.wakeup_reasons = le32_to_cpu(status_v4->wakeup_reasons);
1542 status.wake_packet_length =
1543 le32_to_cpu(status_v4->wake_packet_length);
1544 status.wake_packet_bufsize =
1545 le32_to_cpu(status_v4->wake_packet_bufsize);
1546 status.wake_packet = status_v4->wake_packet;
1549 if (len != status_size + ALIGN(status.wake_packet_bufsize, 4)) {
1550 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1554 /* still at hard-coded place 0 for D3 image */
1555 ap_sta = rcu_dereference_protected(
1556 mvm->fw_id_to_mac_id[0],
1557 lockdep_is_held(&mvm->mutex));
1558 if (IS_ERR_OR_NULL(ap_sta))
1561 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
1562 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1563 u16 seq = status.qos_seq_ctr[i];
1564 /* firmware stores last-used value, we store next value */
1566 mvm_ap_sta->tid_data[i].seq_number = seq;
1569 /* now we have all the data we need, unlock to avoid mac80211 issues */
1570 mutex_unlock(&mvm->mutex);
1572 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1574 keep = iwl_mvm_setup_connection_keep(mvm, vif, status_v6);
1576 iwl_free_resp(&cmd);
1580 iwl_free_resp(&cmd);
1582 mutex_unlock(&mvm->mutex);
1586 static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1588 #ifdef CONFIG_IWLWIFI_DEBUGFS
1589 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1590 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1591 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1593 if (!mvm->store_d3_resume_sram)
1596 if (!mvm->d3_resume_sram) {
1597 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1598 if (!mvm->d3_resume_sram)
1602 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1606 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1607 struct ieee80211_vif *vif)
1609 /* skip the one we keep connection on */
1613 if (vif->type == NL80211_IFTYPE_STATION)
1614 ieee80211_resume_disconnect(vif);
1617 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
1619 struct iwl_d3_iter_data resume_iter_data = {
1622 struct ieee80211_vif *vif = NULL;
1624 enum iwl_d3_status d3_status;
1627 mutex_lock(&mvm->mutex);
1629 /* get the BSS vif pointer again */
1630 ieee80211_iterate_active_interfaces_atomic(
1631 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1632 iwl_mvm_d3_iface_iterator, &resume_iter_data);
1634 if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
1637 vif = resume_iter_data.vif;
1639 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
1643 if (d3_status != IWL_D3_STATUS_ALIVE) {
1644 IWL_INFO(mvm, "Device was reset during suspend\n");
1648 /* query SRAM first in case we want event logging */
1649 iwl_mvm_read_d3_sram(mvm);
1651 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
1652 #ifdef CONFIG_IWLWIFI_DEBUGFS
1654 mvm->keep_vif = vif;
1656 /* has unlocked the mutex, so skip that */
1660 mutex_unlock(&mvm->mutex);
1664 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1665 IEEE80211_IFACE_ITER_NORMAL,
1666 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
1668 /* return 1 to reconfigure the device */
1669 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1673 int iwl_mvm_resume(struct ieee80211_hw *hw)
1675 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1677 return __iwl_mvm_resume(mvm, false);
1680 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1682 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1684 device_set_wakeup_enable(mvm->trans->dev, enabled);
1687 #ifdef CONFIG_IWLWIFI_DEBUGFS
1688 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
1690 struct iwl_mvm *mvm = inode->i_private;
1693 if (mvm->d3_test_active)
1696 file->private_data = inode->i_private;
1698 ieee80211_stop_queues(mvm->hw);
1701 /* start pseudo D3 */
1703 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
1708 ieee80211_wake_queues(mvm->hw);
1711 mvm->d3_test_active = true;
1712 mvm->keep_vif = NULL;
1716 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
1717 size_t count, loff_t *ppos)
1719 struct iwl_mvm *mvm = file->private_data;
1723 /* read pme_ptr if available */
1724 if (mvm->d3_test_pme_ptr) {
1725 pme_asserted = iwl_trans_read_mem32(mvm->trans,
1726 mvm->d3_test_pme_ptr);
1731 if (msleep_interruptible(100))
1738 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
1739 struct ieee80211_vif *vif)
1741 /* skip the one we keep connection on */
1745 if (vif->type == NL80211_IFTYPE_STATION)
1746 ieee80211_connection_loss(vif);
1749 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
1751 struct iwl_mvm *mvm = inode->i_private;
1752 int remaining_time = 10;
1754 mvm->d3_test_active = false;
1755 __iwl_mvm_resume(mvm, true);
1756 iwl_abort_notification_waits(&mvm->notif_wait);
1757 ieee80211_restart_hw(mvm->hw);
1759 /* wait for restart and disconnect all interfaces */
1760 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1761 remaining_time > 0) {
1766 if (remaining_time == 0)
1767 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
1769 ieee80211_iterate_active_interfaces_atomic(
1770 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1771 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
1773 ieee80211_wake_queues(mvm->hw);
1778 const struct file_operations iwl_dbgfs_d3_test_ops = {
1779 .llseek = no_llseek,
1780 .open = iwl_mvm_d3_test_open,
1781 .read = iwl_mvm_d3_test_read,
1782 .release = iwl_mvm_d3_test_release,