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, 0, sizeof(wkc), &wkc);
197 data->error = ret != 0;
199 mvm->ptk_ivlen = key->iv_len;
200 mvm->ptk_icvlen = key->icv_len;
201 mvm->gtk_ivlen = key->iv_len;
202 mvm->gtk_icvlen = key->icv_len;
204 /* don't upload key again */
210 case WLAN_CIPHER_SUITE_AES_CMAC:
212 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
213 * but we also shouldn't abort suspend due to that. It does have
214 * support for the IGTK key renewal, but doesn't really use the
215 * IGTK for anything. This means we could spuriously wake up or
216 * be deauthenticated, but that was considered acceptable.
219 case WLAN_CIPHER_SUITE_TKIP:
221 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
222 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
224 rx_p1ks = data->tkip->rx_uni;
226 ieee80211_get_key_tx_seq(key, &seq);
227 tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
228 tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
230 ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
231 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
233 memcpy(data->tkip->mic_keys.tx,
234 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
237 rx_mic_key = data->tkip->mic_keys.rx_unicast;
240 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
241 rx_p1ks = data->tkip->rx_multi;
242 rx_mic_key = data->tkip->mic_keys.rx_mcast;
246 * For non-QoS this relies on the fact that both the uCode and
247 * mac80211 use TID 0 (as they need to to avoid replay attacks)
248 * for checking the IV in the frames.
250 for (i = 0; i < IWL_NUM_RSC; i++) {
251 ieee80211_get_key_rx_seq(key, i, &seq);
252 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
253 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
254 /* wrapping isn't allowed, AP must rekey */
255 if (seq.tkip.iv32 > cur_rx_iv32)
256 cur_rx_iv32 = seq.tkip.iv32;
259 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
261 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
262 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
263 cur_rx_iv32 + 1, p1k);
264 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
267 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
270 data->use_tkip = true;
271 data->use_rsc_tsc = true;
273 case WLAN_CIPHER_SUITE_CCMP:
275 u8 *pn = seq.ccmp.pn;
277 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
278 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
280 ieee80211_get_key_tx_seq(key, &seq);
281 aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
288 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
292 * For non-QoS this relies on the fact that both the uCode and
293 * mac80211 use TID 0 for checking the IV in the frames.
295 for (i = 0; i < IWL_NUM_RSC; i++) {
296 u8 *pn = seq.ccmp.pn;
298 ieee80211_get_key_rx_seq(key, i, &seq);
299 aes_sc->pn = cpu_to_le64((u64)pn[5] |
306 data->use_rsc_tsc = true;
311 * The D3 firmware hardcodes the key offset 0 as the key it uses
312 * to transmit packets to the AP, i.e. the PTK.
314 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
316 mvm->ptk_ivlen = key->iv_len;
317 mvm->ptk_icvlen = key->icv_len;
320 * firmware only supports TSC/RSC for a single key,
321 * so if there are multiple keep overwriting them
322 * with new ones -- this relies on mac80211 doing
326 mvm->gtk_ivlen = key->iv_len;
327 mvm->gtk_icvlen = key->icv_len;
330 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
331 data->error = ret != 0;
333 mutex_unlock(&mvm->mutex);
336 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
337 struct cfg80211_wowlan *wowlan)
339 struct iwl_wowlan_patterns_cmd *pattern_cmd;
340 struct iwl_host_cmd cmd = {
341 .id = WOWLAN_PATTERNS,
342 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
346 if (!wowlan->n_patterns)
349 cmd.len[0] = sizeof(*pattern_cmd) +
350 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
352 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
356 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
358 for (i = 0; i < wowlan->n_patterns; i++) {
359 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
361 memcpy(&pattern_cmd->patterns[i].mask,
362 wowlan->patterns[i].mask, mask_len);
363 memcpy(&pattern_cmd->patterns[i].pattern,
364 wowlan->patterns[i].pattern,
365 wowlan->patterns[i].pattern_len);
366 pattern_cmd->patterns[i].mask_size = mask_len;
367 pattern_cmd->patterns[i].pattern_size =
368 wowlan->patterns[i].pattern_len;
371 cmd.data[0] = pattern_cmd;
372 err = iwl_mvm_send_cmd(mvm, &cmd);
377 enum iwl_mvm_tcp_packet_type {
386 static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
388 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
389 return cpu_to_le16(be16_to_cpu((__force __be16)check));
392 static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
393 struct cfg80211_wowlan_tcp *tcp,
394 void *_pkt, u8 *mask,
395 __le16 *pseudo_hdr_csum,
396 enum iwl_mvm_tcp_packet_type ptype)
403 } __packed *pkt = _pkt;
404 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
407 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
410 pkt->ip.protocol = IPPROTO_TCP;
414 case MVM_TCP_TX_DATA:
416 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
417 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
419 pkt->ip.saddr = tcp->src;
420 pkt->ip.daddr = tcp->dst;
421 pkt->tcp.source = cpu_to_be16(tcp->src_port);
422 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
423 /* overwritten for TX SYN later */
424 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
425 pkt->tcp.window = cpu_to_be16(65000);
427 case MVM_TCP_RX_SYNACK:
429 case MVM_TCP_RX_WAKE:
430 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
431 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
432 pkt->ip.saddr = tcp->dst;
433 pkt->ip.daddr = tcp->src;
434 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
435 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
444 /* firmware assumes 8 option bytes - 8 NOPs for now */
445 memset(pkt->data, 0x01, 8);
447 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
450 case MVM_TCP_TX_DATA:
451 ip_tot_len += tcp->payload_len;
452 memcpy(pkt->data, tcp->payload, tcp->payload_len);
460 case MVM_TCP_RX_SYNACK:
467 case MVM_TCP_RX_WAKE:
468 ip_tot_len += tcp->wake_len;
471 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
477 case MVM_TCP_TX_DATA:
479 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
480 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
482 case MVM_TCP_RX_WAKE:
483 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
484 u8 tmp = tcp->wake_mask[i];
485 mask[i + 6] |= tmp << 6;
486 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
487 mask[i + 7] = tmp >> 2;
489 /* fall through for ethernet/IP/TCP headers mask */
490 case MVM_TCP_RX_SYNACK:
492 mask[0] = 0xff; /* match ethernet */
494 * match ethernet, ip.version, ip.ihl
495 * the ip.ihl half byte is really masked out by firmware
498 mask[2] = 0x80; /* match ip.protocol */
499 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
500 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
501 mask[5] = 0x80; /* match tcp flags */
502 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
506 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
507 pkt->ip.saddr, pkt->ip.daddr);
510 static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
511 struct ieee80211_vif *vif,
512 struct cfg80211_wowlan_tcp *tcp)
514 struct iwl_wowlan_remote_wake_config *cfg;
515 struct iwl_host_cmd cmd = {
516 .id = REMOTE_WAKE_CONFIG_CMD,
517 .len = { sizeof(*cfg), },
518 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
525 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
530 cfg->max_syn_retries = 10;
531 cfg->max_data_retries = 10;
532 cfg->tcp_syn_ack_timeout = 1; /* seconds */
533 cfg->tcp_ack_timeout = 1; /* seconds */
536 iwl_mvm_build_tcp_packet(
537 vif, tcp, cfg->syn_tx.data, NULL,
538 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
540 cfg->syn_tx.info.tcp_payload_length = 0;
543 iwl_mvm_build_tcp_packet(
544 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
545 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
547 cfg->synack_rx.info.tcp_payload_length = 0;
549 /* KEEPALIVE/ACK (TX) */
550 iwl_mvm_build_tcp_packet(
551 vif, tcp, cfg->keepalive_tx.data, NULL,
552 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
554 cfg->keepalive_tx.info.tcp_payload_length =
555 cpu_to_le16(tcp->payload_len);
556 cfg->sequence_number_offset = tcp->payload_seq.offset;
557 /* length must be 0..4, the field is little endian */
558 cfg->sequence_number_length = tcp->payload_seq.len;
559 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
560 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
561 if (tcp->payload_tok.len) {
562 cfg->token_offset = tcp->payload_tok.offset;
563 cfg->token_length = tcp->payload_tok.len;
565 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
566 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
569 /* set tokens to max value to almost never run out */
570 cfg->num_tokens = cpu_to_le16(65535);
574 iwl_mvm_build_tcp_packet(
575 vif, tcp, cfg->keepalive_ack_rx.data,
576 cfg->keepalive_ack_rx.rx_mask,
577 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
579 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
582 iwl_mvm_build_tcp_packet(
583 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
584 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
586 cfg->wake_rx.info.tcp_payload_length =
587 cpu_to_le16(tcp->wake_len);
590 iwl_mvm_build_tcp_packet(
591 vif, tcp, cfg->fin_tx.data, NULL,
592 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
594 cfg->fin_tx.info.tcp_payload_length = 0;
596 ret = iwl_mvm_send_cmd(mvm, &cmd);
602 struct iwl_d3_iter_data {
604 struct ieee80211_vif *vif;
608 static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
609 struct ieee80211_vif *vif)
611 struct iwl_d3_iter_data *data = _data;
612 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
614 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
617 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
621 IWL_ERR(data->mvm, "More than one managed interface active!\n");
629 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
630 struct ieee80211_sta *ap_sta)
632 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
633 struct ieee80211_chanctx_conf *ctx;
634 u8 chains_static, chains_dynamic;
635 struct cfg80211_chan_def chandef;
637 struct iwl_binding_cmd binding_cmd = {};
638 struct iwl_time_quota_cmd quota_cmd = {};
641 /* add back the PHY */
642 if (WARN_ON(!mvmvif->phy_ctxt))
646 ctx = rcu_dereference(vif->chanctx_conf);
652 chains_static = ctx->rx_chains_static;
653 chains_dynamic = ctx->rx_chains_dynamic;
656 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
657 chains_static, chains_dynamic);
661 /* add back the MAC */
662 mvmvif->uploaded = false;
664 if (WARN_ON(!vif->bss_conf.assoc))
667 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
671 /* add back binding - XXX refactor? */
672 binding_cmd.id_and_color =
673 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
674 mvmvif->phy_ctxt->color));
675 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
677 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
678 mvmvif->phy_ctxt->color));
679 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
681 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
682 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
685 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
686 sizeof(binding_cmd), &binding_cmd,
689 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
694 IWL_ERR(mvm, "Binding command failed: %u\n", status);
698 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
701 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
703 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false);
708 quota_cmd.quotas[0].id_and_color =
709 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
710 mvmvif->phy_ctxt->color));
711 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
712 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
714 for (i = 1; i < MAX_BINDINGS; i++)
715 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
717 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
718 sizeof(quota_cmd), "a_cmd);
720 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
725 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
726 struct ieee80211_vif *vif)
728 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
729 struct iwl_nonqos_seq_query_cmd query_cmd = {
730 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
732 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
735 struct iwl_host_cmd cmd = {
736 .id = NON_QOS_TX_COUNTER_CMD,
737 .flags = CMD_WANT_SKB,
742 cmd.data[0] = &query_cmd;
743 cmd.len[0] = sizeof(query_cmd);
745 err = iwl_mvm_send_cmd(mvm, &cmd);
749 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
750 if (size < sizeof(__le16)) {
753 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
754 /* firmware returns next, not last-used seqno */
755 err = (u16) (err - 0x10);
762 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
764 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
765 struct iwl_nonqos_seq_query_cmd query_cmd = {
766 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
768 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
770 .value = cpu_to_le16(mvmvif->seqno),
773 /* return if called during restart, not resume from D3 */
774 if (!mvmvif->seqno_valid)
777 mvmvif->seqno_valid = false;
779 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
780 sizeof(query_cmd), &query_cmd))
781 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
785 iwl_mvm_send_wowlan_config_cmd(struct iwl_mvm *mvm,
786 const struct iwl_wowlan_config_cmd_v3 *cmd)
788 /* start only with the v2 part of the command */
789 u16 cmd_len = sizeof(cmd->common);
791 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_WOWLAN_CONFIG_TID)
792 cmd_len = sizeof(*cmd);
794 return iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
798 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
799 struct cfg80211_wowlan *wowlan,
802 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
803 struct iwl_d3_iter_data suspend_iter_data = {
806 struct ieee80211_vif *vif;
807 struct iwl_mvm_vif *mvmvif;
808 struct ieee80211_sta *ap_sta;
809 struct iwl_mvm_sta *mvm_ap_sta;
810 struct iwl_wowlan_config_cmd_v3 wowlan_config_cmd = {};
811 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
812 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
813 struct iwl_d3_manager_config d3_cfg_cmd_data = {
815 * Program the minimum sleep time to 10 seconds, as many
816 * platforms have issues processing a wakeup signal while
817 * still being in the process of suspending.
819 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
821 struct iwl_host_cmd d3_cfg_cmd = {
823 .flags = CMD_WANT_SKB,
824 .data[0] = &d3_cfg_cmd_data,
825 .len[0] = sizeof(d3_cfg_cmd_data),
827 struct wowlan_key_data key_data = {
828 .use_rsc_tsc = false,
833 int len __maybe_unused;
837 * mac80211 shouldn't get here, but for D3 test
838 * it doesn't warrant a warning
844 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
845 if (!key_data.rsc_tsc)
848 mutex_lock(&mvm->mutex);
850 /* see if there's only a single BSS vif and it's associated */
851 ieee80211_iterate_active_interfaces_atomic(
852 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
853 iwl_mvm_d3_iface_iterator, &suspend_iter_data);
855 if (suspend_iter_data.error || !suspend_iter_data.vif) {
860 vif = suspend_iter_data.vif;
861 mvmvif = iwl_mvm_vif_from_mac80211(vif);
863 ap_sta = rcu_dereference_protected(
864 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
865 lockdep_is_held(&mvm->mutex));
866 if (IS_ERR_OR_NULL(ap_sta)) {
871 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
873 /* TODO: wowlan_config_cmd.common.wowlan_ba_teardown_tids */
875 wowlan_config_cmd.common.is_11n_connection =
876 ap_sta->ht_cap.ht_supported;
878 /* Query the last used seqno and set it */
879 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
882 wowlan_config_cmd.common.non_qos_seq = cpu_to_le16(ret);
884 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, &wowlan_config_cmd.common);
886 if (wowlan->disconnect)
887 wowlan_config_cmd.common.wakeup_filter |=
888 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
889 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
890 if (wowlan->magic_pkt)
891 wowlan_config_cmd.common.wakeup_filter |=
892 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
893 if (wowlan->gtk_rekey_failure)
894 wowlan_config_cmd.common.wakeup_filter |=
895 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
896 if (wowlan->eap_identity_req)
897 wowlan_config_cmd.common.wakeup_filter |=
898 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
899 if (wowlan->four_way_handshake)
900 wowlan_config_cmd.common.wakeup_filter |=
901 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
902 if (wowlan->n_patterns)
903 wowlan_config_cmd.common.wakeup_filter |=
904 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
906 if (wowlan->rfkill_release)
907 wowlan_config_cmd.common.wakeup_filter |=
908 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
912 * Set the "link change" (really "link lost") flag as well
913 * since that implies losing the TCP connection.
915 wowlan_config_cmd.common.wakeup_filter |=
916 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
917 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
918 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
919 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
922 iwl_mvm_cancel_scan(mvm);
924 iwl_trans_stop_device(mvm->trans);
927 * Set the HW restart bit -- this is mostly true as we're
928 * going to load new firmware and reprogram that, though
929 * the reprogramming is going to be manual to avoid adding
930 * all the MACs that aren't support.
931 * We don't have to clear up everything though because the
932 * reprogramming is manual. When we resume, we'll actually
933 * go through a proper restart sequence again to switch
934 * back to the runtime firmware image.
936 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
938 /* We reprogram keys and shouldn't allocate new key indices */
939 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
946 ret = iwl_mvm_load_d3_fw(mvm);
950 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
954 if (!iwlwifi_mod_params.sw_crypto) {
956 * This needs to be unlocked due to lock ordering
957 * constraints. Since we're in the suspend path
958 * that isn't really a problem though.
960 mutex_unlock(&mvm->mutex);
961 ieee80211_iter_keys(mvm->hw, vif,
962 iwl_mvm_wowlan_program_keys,
964 mutex_lock(&mvm->mutex);
965 if (key_data.error) {
970 if (key_data.use_rsc_tsc) {
971 struct iwl_host_cmd rsc_tsc_cmd = {
972 .id = WOWLAN_TSC_RSC_PARAM,
973 .data[0] = key_data.rsc_tsc,
974 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
975 .len[0] = sizeof(*key_data.rsc_tsc),
978 ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
983 if (key_data.use_tkip) {
984 ret = iwl_mvm_send_cmd_pdu(mvm,
992 if (mvmvif->rekey_data.valid) {
993 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
994 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
996 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
997 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
999 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1000 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1002 ret = iwl_mvm_send_cmd_pdu(mvm,
1003 WOWLAN_KEK_KCK_MATERIAL, 0,
1004 sizeof(kek_kck_cmd),
1011 ret = iwl_mvm_send_wowlan_config_cmd(mvm, &wowlan_config_cmd);
1015 ret = iwl_mvm_send_patterns(mvm, wowlan);
1019 ret = iwl_mvm_send_proto_offload(mvm, vif, false, 0);
1023 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1027 ret = iwl_mvm_power_update_device(mvm);
1031 ret = iwl_mvm_power_update_mac(mvm);
1035 #ifdef CONFIG_IWLWIFI_DEBUGFS
1036 if (mvm->d3_wake_sysassert)
1037 d3_cfg_cmd_data.wakeup_flags |=
1038 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1041 /* must be last -- this switches firmware state */
1042 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1045 #ifdef CONFIG_IWLWIFI_DEBUGFS
1046 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1047 if (len >= sizeof(u32)) {
1048 mvm->d3_test_pme_ptr =
1049 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1052 iwl_free_resp(&d3_cfg_cmd);
1054 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1056 iwl_trans_d3_suspend(mvm->trans, test);
1059 ieee80211_restart_hw(mvm->hw);
1061 kfree(key_data.rsc_tsc);
1063 mutex_unlock(&mvm->mutex);
1068 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1070 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1072 if (iwl_mvm_is_d0i3_supported(mvm)) {
1073 mutex_lock(&mvm->d0i3_suspend_mutex);
1074 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1075 mutex_unlock(&mvm->d0i3_suspend_mutex);
1079 return __iwl_mvm_suspend(hw, wowlan, false);
1082 /* converted data from the different status responses */
1083 struct iwl_wowlan_status_data {
1087 u32 wake_packet_length;
1088 u32 wake_packet_bufsize;
1089 const u8 *wake_packet;
1092 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1093 struct ieee80211_vif *vif,
1094 struct iwl_wowlan_status_data *status)
1096 struct sk_buff *pkt = NULL;
1097 struct cfg80211_wowlan_wakeup wakeup = {
1100 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1101 u32 reasons = status->wakeup_reasons;
1103 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1104 wakeup_report = NULL;
1108 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1109 wakeup.magic_pkt = true;
1111 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1112 wakeup.pattern_idx =
1113 status->pattern_number;
1115 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1116 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1117 wakeup.disconnect = true;
1119 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1120 wakeup.gtk_rekey_failure = true;
1122 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1123 wakeup.rfkill_release = true;
1125 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1126 wakeup.eap_identity_req = true;
1128 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1129 wakeup.four_way_handshake = true;
1131 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1132 wakeup.tcp_connlost = true;
1134 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1135 wakeup.tcp_nomoretokens = true;
1137 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1138 wakeup.tcp_match = true;
1140 if (status->wake_packet_bufsize) {
1141 int pktsize = status->wake_packet_bufsize;
1142 int pktlen = status->wake_packet_length;
1143 const u8 *pktdata = status->wake_packet;
1144 struct ieee80211_hdr *hdr = (void *)pktdata;
1145 int truncated = pktlen - pktsize;
1147 /* this would be a firmware bug */
1148 if (WARN_ON_ONCE(truncated < 0))
1151 if (ieee80211_is_data(hdr->frame_control)) {
1152 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1153 int ivlen = 0, icvlen = 4; /* also FCS */
1155 pkt = alloc_skb(pktsize, GFP_KERNEL);
1159 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1163 if (ieee80211_has_protected(hdr->frame_control)) {
1165 * This is unlocked and using gtk_i(c)vlen,
1166 * but since everything is under RTNL still
1167 * that's not really a problem - changing
1168 * it would be difficult.
1170 if (is_multicast_ether_addr(hdr->addr1)) {
1171 ivlen = mvm->gtk_ivlen;
1172 icvlen += mvm->gtk_icvlen;
1174 ivlen = mvm->ptk_ivlen;
1175 icvlen += mvm->ptk_icvlen;
1179 /* if truncated, FCS/ICV is (partially) gone */
1180 if (truncated >= icvlen) {
1182 truncated -= icvlen;
1184 icvlen -= truncated;
1188 pktsize -= ivlen + icvlen;
1191 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1193 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1195 wakeup.packet = pkt->data;
1196 wakeup.packet_present_len = pkt->len;
1197 wakeup.packet_len = pkt->len - truncated;
1198 wakeup.packet_80211 = false;
1202 if (truncated >= 4) {
1206 fcslen -= truncated;
1210 wakeup.packet = status->wake_packet;
1211 wakeup.packet_present_len = pktsize;
1212 wakeup.packet_len = pktlen - truncated;
1213 wakeup.packet_80211 = true;
1218 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1222 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1223 struct ieee80211_key_seq *seq)
1227 pn = le64_to_cpu(sc->pn);
1228 seq->ccmp.pn[0] = pn >> 40;
1229 seq->ccmp.pn[1] = pn >> 32;
1230 seq->ccmp.pn[2] = pn >> 24;
1231 seq->ccmp.pn[3] = pn >> 16;
1232 seq->ccmp.pn[4] = pn >> 8;
1233 seq->ccmp.pn[5] = pn;
1236 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1237 struct ieee80211_key_seq *seq)
1239 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1240 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1243 static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
1244 struct ieee80211_key_conf *key)
1248 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1250 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1251 struct ieee80211_key_seq seq = {};
1253 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1254 ieee80211_set_key_rx_seq(key, tid, &seq);
1258 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1259 struct ieee80211_key_conf *key)
1263 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1265 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1266 struct ieee80211_key_seq seq = {};
1268 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1269 ieee80211_set_key_rx_seq(key, tid, &seq);
1273 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1274 struct iwl_wowlan_status *status)
1276 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1278 switch (key->cipher) {
1279 case WLAN_CIPHER_SUITE_CCMP:
1280 iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
1282 case WLAN_CIPHER_SUITE_TKIP:
1283 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1290 struct iwl_mvm_d3_gtk_iter_data {
1291 struct iwl_wowlan_status *status;
1294 bool find_phase, unhandled_cipher;
1298 static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
1299 struct ieee80211_vif *vif,
1300 struct ieee80211_sta *sta,
1301 struct ieee80211_key_conf *key,
1304 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1306 if (data->unhandled_cipher)
1309 switch (key->cipher) {
1310 case WLAN_CIPHER_SUITE_WEP40:
1311 case WLAN_CIPHER_SUITE_WEP104:
1312 /* ignore WEP completely, nothing to do */
1314 case WLAN_CIPHER_SUITE_CCMP:
1315 case WLAN_CIPHER_SUITE_TKIP:
1316 /* we support these */
1319 /* everything else (even CMAC for MFP) - disconnect from AP */
1320 data->unhandled_cipher = true;
1327 * pairwise key - update sequence counters only;
1328 * note that this assumes no TDLS sessions are active
1331 struct ieee80211_key_seq seq = {};
1332 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1334 if (data->find_phase)
1337 switch (key->cipher) {
1338 case WLAN_CIPHER_SUITE_CCMP:
1339 iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
1340 iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
1342 case WLAN_CIPHER_SUITE_TKIP:
1343 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1344 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1347 ieee80211_set_key_tx_seq(key, &seq);
1349 /* that's it for this key */
1353 if (data->find_phase) {
1354 data->last_gtk = key;
1355 data->cipher = key->cipher;
1359 if (data->status->num_of_gtk_rekeys)
1360 ieee80211_remove_key(key);
1361 else if (data->last_gtk == key)
1362 iwl_mvm_set_key_rx_seq(key, data->status);
1365 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1366 struct ieee80211_vif *vif,
1367 struct iwl_wowlan_status *status)
1369 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1370 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1373 u32 disconnection_reasons =
1374 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1375 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1377 if (!status || !vif->bss_conf.bssid)
1380 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1383 /* find last GTK that we used initially, if any */
1384 gtkdata.find_phase = true;
1385 ieee80211_iter_keys(mvm->hw, vif,
1386 iwl_mvm_d3_update_gtks, >kdata);
1387 /* not trying to keep connections with MFP/unhandled ciphers */
1388 if (gtkdata.unhandled_cipher)
1390 if (!gtkdata.num_keys)
1392 if (!gtkdata.last_gtk)
1396 * invalidate all other GTKs that might still exist and update
1397 * the one that we used
1399 gtkdata.find_phase = false;
1400 ieee80211_iter_keys(mvm->hw, vif,
1401 iwl_mvm_d3_update_gtks, >kdata);
1403 if (status->num_of_gtk_rekeys) {
1404 struct ieee80211_key_conf *key;
1406 struct ieee80211_key_conf conf;
1409 .conf.cipher = gtkdata.cipher,
1410 .conf.keyidx = status->gtk.key_index,
1413 switch (gtkdata.cipher) {
1414 case WLAN_CIPHER_SUITE_CCMP:
1415 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1416 memcpy(conf.conf.key, status->gtk.decrypt_key,
1419 case WLAN_CIPHER_SUITE_TKIP:
1420 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1421 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1422 /* leave TX MIC key zeroed, we don't use it anyway */
1423 memcpy(conf.conf.key +
1424 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1425 status->gtk.tkip_mic_key, 8);
1429 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1432 iwl_mvm_set_key_rx_seq(key, status);
1435 if (status->num_of_gtk_rekeys) {
1437 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1438 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1439 (void *)&replay_ctr, GFP_KERNEL);
1443 mvmvif->seqno_valid = true;
1444 /* +0x10 because the set API expects next-to-use, not last-used */
1445 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1450 /* releases the MVM mutex */
1451 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1452 struct ieee80211_vif *vif)
1454 u32 base = mvm->error_event_table;
1455 struct error_table_start {
1456 /* cf. struct iwl_error_event_table */
1460 struct iwl_host_cmd cmd = {
1461 .id = WOWLAN_GET_STATUSES,
1462 .flags = CMD_WANT_SKB,
1464 struct iwl_wowlan_status_data status;
1465 struct iwl_wowlan_status *fw_status;
1466 int ret, len, status_size, i;
1468 struct ieee80211_sta *ap_sta;
1469 struct iwl_mvm_sta *mvm_ap_sta;
1471 iwl_trans_read_mem_bytes(mvm->trans, base,
1472 &err_info, sizeof(err_info));
1474 if (err_info.valid) {
1475 IWL_INFO(mvm, "error table is valid (%d)\n",
1477 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1478 struct cfg80211_wowlan_wakeup wakeup = {
1479 .rfkill_release = true,
1481 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1487 /* only for tracing for now */
1488 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1490 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1492 ret = iwl_mvm_send_cmd(mvm, &cmd);
1494 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1498 /* RF-kill already asserted again... */
1502 status_size = sizeof(*fw_status);
1504 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1505 if (len < status_size) {
1506 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1510 fw_status = (void *)cmd.resp_pkt->data;
1512 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1513 for (i = 0; i < 8; i++)
1514 status.qos_seq_ctr[i] =
1515 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1516 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1517 status.wake_packet_length =
1518 le32_to_cpu(fw_status->wake_packet_length);
1519 status.wake_packet_bufsize =
1520 le32_to_cpu(fw_status->wake_packet_bufsize);
1521 status.wake_packet = fw_status->wake_packet;
1523 if (len != status_size + ALIGN(status.wake_packet_bufsize, 4)) {
1524 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1528 /* still at hard-coded place 0 for D3 image */
1529 ap_sta = rcu_dereference_protected(
1530 mvm->fw_id_to_mac_id[0],
1531 lockdep_is_held(&mvm->mutex));
1532 if (IS_ERR_OR_NULL(ap_sta))
1535 mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
1536 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1537 u16 seq = status.qos_seq_ctr[i];
1538 /* firmware stores last-used value, we store next value */
1540 mvm_ap_sta->tid_data[i].seq_number = seq;
1543 /* now we have all the data we need, unlock to avoid mac80211 issues */
1544 mutex_unlock(&mvm->mutex);
1546 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1548 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1550 iwl_free_resp(&cmd);
1554 iwl_free_resp(&cmd);
1556 mutex_unlock(&mvm->mutex);
1560 static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
1562 #ifdef CONFIG_IWLWIFI_DEBUGFS
1563 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1564 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
1565 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
1567 if (!mvm->store_d3_resume_sram)
1570 if (!mvm->d3_resume_sram) {
1571 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
1572 if (!mvm->d3_resume_sram)
1576 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
1580 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
1581 struct ieee80211_vif *vif)
1583 /* skip the one we keep connection on */
1587 if (vif->type == NL80211_IFTYPE_STATION)
1588 ieee80211_resume_disconnect(vif);
1591 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
1593 struct iwl_d3_iter_data resume_iter_data = {
1596 struct ieee80211_vif *vif = NULL;
1598 enum iwl_d3_status d3_status;
1601 mutex_lock(&mvm->mutex);
1603 /* get the BSS vif pointer again */
1604 ieee80211_iterate_active_interfaces_atomic(
1605 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1606 iwl_mvm_d3_iface_iterator, &resume_iter_data);
1608 if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
1611 vif = resume_iter_data.vif;
1613 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
1617 if (d3_status != IWL_D3_STATUS_ALIVE) {
1618 IWL_INFO(mvm, "Device was reset during suspend\n");
1622 /* query SRAM first in case we want event logging */
1623 iwl_mvm_read_d3_sram(mvm);
1625 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
1626 #ifdef CONFIG_IWLWIFI_DEBUGFS
1628 mvm->keep_vif = vif;
1630 /* has unlocked the mutex, so skip that */
1634 mutex_unlock(&mvm->mutex);
1638 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
1639 IEEE80211_IFACE_ITER_NORMAL,
1640 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
1642 /* return 1 to reconfigure the device */
1643 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1647 int iwl_mvm_resume(struct ieee80211_hw *hw)
1649 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1651 if (iwl_mvm_is_d0i3_supported(mvm)) {
1654 mutex_lock(&mvm->d0i3_suspend_mutex);
1655 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1656 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
1657 &mvm->d0i3_suspend_flags);
1658 mutex_unlock(&mvm->d0i3_suspend_mutex);
1660 _iwl_mvm_exit_d0i3(mvm);
1664 return __iwl_mvm_resume(mvm, false);
1667 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
1669 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1671 device_set_wakeup_enable(mvm->trans->dev, enabled);
1674 #ifdef CONFIG_IWLWIFI_DEBUGFS
1675 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
1677 struct iwl_mvm *mvm = inode->i_private;
1680 if (mvm->d3_test_active)
1683 file->private_data = inode->i_private;
1685 ieee80211_stop_queues(mvm->hw);
1688 /* start pseudo D3 */
1690 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
1695 ieee80211_wake_queues(mvm->hw);
1698 mvm->d3_test_active = true;
1699 mvm->keep_vif = NULL;
1703 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
1704 size_t count, loff_t *ppos)
1706 struct iwl_mvm *mvm = file->private_data;
1710 /* read pme_ptr if available */
1711 if (mvm->d3_test_pme_ptr) {
1712 pme_asserted = iwl_trans_read_mem32(mvm->trans,
1713 mvm->d3_test_pme_ptr);
1718 if (msleep_interruptible(100))
1725 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
1726 struct ieee80211_vif *vif)
1728 /* skip the one we keep connection on */
1732 if (vif->type == NL80211_IFTYPE_STATION)
1733 ieee80211_connection_loss(vif);
1736 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
1738 struct iwl_mvm *mvm = inode->i_private;
1739 int remaining_time = 10;
1741 mvm->d3_test_active = false;
1742 __iwl_mvm_resume(mvm, true);
1743 iwl_abort_notification_waits(&mvm->notif_wait);
1744 ieee80211_restart_hw(mvm->hw);
1746 /* wait for restart and disconnect all interfaces */
1747 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1748 remaining_time > 0) {
1753 if (remaining_time == 0)
1754 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
1756 ieee80211_iterate_active_interfaces_atomic(
1757 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1758 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
1760 ieee80211_wake_queues(mvm->hw);
1765 const struct file_operations iwl_dbgfs_d3_test_ops = {
1766 .llseek = no_llseek,
1767 .open = iwl_mvm_d3_test_open,
1768 .read = iwl_mvm_d3_test_read,
1769 .release = iwl_mvm_d3_test_release,