18c2ecf20Sopenharmony_ci/******************************************************************************
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
48c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation. All rights reserved.
98c2ecf20Sopenharmony_ci * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
108c2ecf20Sopenharmony_ci * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
138c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
148c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
178c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
188c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
198c2ecf20Sopenharmony_ci * General Public License for more details.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * The full GNU General Public License is included in this distribution
228c2ecf20Sopenharmony_ci * in the file called COPYING.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Contact Information:
258c2ecf20Sopenharmony_ci *  Intel Linux Wireless <linuxwifi@intel.com>
268c2ecf20Sopenharmony_ci * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * BSD LICENSE
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation. All rights reserved.
318c2ecf20Sopenharmony_ci * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
328c2ecf20Sopenharmony_ci * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
338c2ecf20Sopenharmony_ci * All rights reserved.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
368c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
378c2ecf20Sopenharmony_ci * are met:
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci *  * Redistributions of source code must retain the above copyright
408c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
418c2ecf20Sopenharmony_ci *  * Redistributions in binary form must reproduce the above copyright
428c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
438c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
448c2ecf20Sopenharmony_ci *    distribution.
458c2ecf20Sopenharmony_ci *  * Neither the name Intel Corporation nor the names of its
468c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived
478c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
508c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
518c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
528c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
538c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
548c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
558c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
568c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
578c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
588c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
598c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci *****************************************************************************/
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
648c2ecf20Sopenharmony_ci#include <linux/ip.h>
658c2ecf20Sopenharmony_ci#include <linux/fs.h>
668c2ecf20Sopenharmony_ci#include <net/cfg80211.h>
678c2ecf20Sopenharmony_ci#include <net/ipv6.h>
688c2ecf20Sopenharmony_ci#include <net/tcp.h>
698c2ecf20Sopenharmony_ci#include <net/addrconf.h>
708c2ecf20Sopenharmony_ci#include "iwl-modparams.h"
718c2ecf20Sopenharmony_ci#include "fw-api.h"
728c2ecf20Sopenharmony_ci#include "mvm.h"
738c2ecf20Sopenharmony_ci#include "fw/img.h"
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_civoid iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
768c2ecf20Sopenharmony_ci			    struct ieee80211_vif *vif,
778c2ecf20Sopenharmony_ci			    struct cfg80211_gtk_rekey_data *data)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
808c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	mvmvif->rekey_data.kek_len = data->kek_len;
858c2ecf20Sopenharmony_ci	mvmvif->rekey_data.kck_len = data->kck_len;
868c2ecf20Sopenharmony_ci	memcpy(mvmvif->rekey_data.kek, data->kek, data->kek_len);
878c2ecf20Sopenharmony_ci	memcpy(mvmvif->rekey_data.kck, data->kck, data->kck_len);
888c2ecf20Sopenharmony_ci	mvmvif->rekey_data.akm = data->akm & 0xFF;
898c2ecf20Sopenharmony_ci	mvmvif->rekey_data.replay_ctr =
908c2ecf20Sopenharmony_ci		cpu_to_le64(be64_to_cpup((__be64 *)data->replay_ctr));
918c2ecf20Sopenharmony_ci	mvmvif->rekey_data.valid = true;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
978c2ecf20Sopenharmony_civoid iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
988c2ecf20Sopenharmony_ci			      struct ieee80211_vif *vif,
998c2ecf20Sopenharmony_ci			      struct inet6_dev *idev)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1028c2ecf20Sopenharmony_ci	struct inet6_ifaddr *ifa;
1038c2ecf20Sopenharmony_ci	int idx = 0;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	read_lock_bh(&idev->lock);
1088c2ecf20Sopenharmony_ci	list_for_each_entry(ifa, &idev->addr_list, if_list) {
1098c2ecf20Sopenharmony_ci		mvmvif->target_ipv6_addrs[idx] = ifa->addr;
1108c2ecf20Sopenharmony_ci		if (ifa->flags & IFA_F_TENTATIVE)
1118c2ecf20Sopenharmony_ci			__set_bit(idx, mvmvif->tentative_addrs);
1128c2ecf20Sopenharmony_ci		idx++;
1138c2ecf20Sopenharmony_ci		if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
1148c2ecf20Sopenharmony_ci			break;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci	read_unlock_bh(&idev->lock);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	mvmvif->num_target_ipv6_addrs = idx;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci#endif
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_civoid iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
1238c2ecf20Sopenharmony_ci				     struct ieee80211_vif *vif, int idx)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	mvmvif->tx_key_idx = idx;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	int i;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	for (i = 0; i < IWL_P1K_SIZE; i++)
1358c2ecf20Sopenharmony_ci		out[i] = cpu_to_le16(p1k[i]);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
1398c2ecf20Sopenharmony_ci				     struct iwl_mvm_key_pn *ptk_pn,
1408c2ecf20Sopenharmony_ci				     struct ieee80211_key_seq *seq,
1418c2ecf20Sopenharmony_ci				     int tid, int queues)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	const u8 *ret = seq->ccmp.pn;
1448c2ecf20Sopenharmony_ci	int i;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	/* get the PN from mac80211, used on the default queue */
1478c2ecf20Sopenharmony_ci	ieee80211_get_key_rx_seq(key, tid, seq);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/* and use the internal data for the other queues */
1508c2ecf20Sopenharmony_ci	for (i = 1; i < queues; i++) {
1518c2ecf20Sopenharmony_ci		const u8 *tmp = ptk_pn->q[i].pn[tid];
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
1548c2ecf20Sopenharmony_ci			ret = tmp;
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return ret;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistruct wowlan_key_data {
1618c2ecf20Sopenharmony_ci	struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
1628c2ecf20Sopenharmony_ci	struct iwl_wowlan_tkip_params_cmd *tkip;
1638c2ecf20Sopenharmony_ci	struct iwl_wowlan_kek_kck_material_cmd_v3 *kek_kck_cmd;
1648c2ecf20Sopenharmony_ci	bool error, use_rsc_tsc, use_tkip, configure_keys;
1658c2ecf20Sopenharmony_ci	int wep_key_idx;
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
1698c2ecf20Sopenharmony_ci					struct ieee80211_vif *vif,
1708c2ecf20Sopenharmony_ci					struct ieee80211_sta *sta,
1718c2ecf20Sopenharmony_ci					struct ieee80211_key_conf *key,
1728c2ecf20Sopenharmony_ci					void *_data)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1758c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1768c2ecf20Sopenharmony_ci	struct wowlan_key_data *data = _data;
1778c2ecf20Sopenharmony_ci	struct aes_sc *aes_sc, *aes_tx_sc = NULL;
1788c2ecf20Sopenharmony_ci	struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
1798c2ecf20Sopenharmony_ci	struct iwl_p1k_cache *rx_p1ks;
1808c2ecf20Sopenharmony_ci	u8 *rx_mic_key;
1818c2ecf20Sopenharmony_ci	struct ieee80211_key_seq seq;
1828c2ecf20Sopenharmony_ci	u32 cur_rx_iv32 = 0;
1838c2ecf20Sopenharmony_ci	u16 p1k[IWL_P1K_SIZE];
1848c2ecf20Sopenharmony_ci	int ret, i;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	switch (key->cipher) {
1878c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_WEP40:
1888c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
1898c2ecf20Sopenharmony_ci		struct {
1908c2ecf20Sopenharmony_ci			struct iwl_mvm_wep_key_cmd wep_key_cmd;
1918c2ecf20Sopenharmony_ci			struct iwl_mvm_wep_key wep_key;
1928c2ecf20Sopenharmony_ci		} __packed wkc = {
1938c2ecf20Sopenharmony_ci			.wep_key_cmd.mac_id_n_color =
1948c2ecf20Sopenharmony_ci				cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1958c2ecf20Sopenharmony_ci								mvmvif->color)),
1968c2ecf20Sopenharmony_ci			.wep_key_cmd.num_keys = 1,
1978c2ecf20Sopenharmony_ci			/* firmware sets STA_KEY_FLG_WEP_13BYTES */
1988c2ecf20Sopenharmony_ci			.wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
1998c2ecf20Sopenharmony_ci			.wep_key.key_index = key->keyidx,
2008c2ecf20Sopenharmony_ci			.wep_key.key_size = key->keylen,
2018c2ecf20Sopenharmony_ci		};
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci		/*
2048c2ecf20Sopenharmony_ci		 * This will fail -- the key functions don't set support
2058c2ecf20Sopenharmony_ci		 * pairwise WEP keys. However, that's better than silently
2068c2ecf20Sopenharmony_ci		 * failing WoWLAN. Or maybe not?
2078c2ecf20Sopenharmony_ci		 */
2088c2ecf20Sopenharmony_ci		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2098c2ecf20Sopenharmony_ci			break;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci		memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
2128c2ecf20Sopenharmony_ci		if (key->keyidx == mvmvif->tx_key_idx) {
2138c2ecf20Sopenharmony_ci			/* TX key must be at offset 0 */
2148c2ecf20Sopenharmony_ci			wkc.wep_key.key_offset = 0;
2158c2ecf20Sopenharmony_ci		} else {
2168c2ecf20Sopenharmony_ci			/* others start at 1 */
2178c2ecf20Sopenharmony_ci			data->wep_key_idx++;
2188c2ecf20Sopenharmony_ci			wkc.wep_key.key_offset = data->wep_key_idx;
2198c2ecf20Sopenharmony_ci		}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci		if (data->configure_keys) {
2228c2ecf20Sopenharmony_ci			mutex_lock(&mvm->mutex);
2238c2ecf20Sopenharmony_ci			ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0,
2248c2ecf20Sopenharmony_ci						   sizeof(wkc), &wkc);
2258c2ecf20Sopenharmony_ci			data->error = ret != 0;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci			mvm->ptk_ivlen = key->iv_len;
2288c2ecf20Sopenharmony_ci			mvm->ptk_icvlen = key->icv_len;
2298c2ecf20Sopenharmony_ci			mvm->gtk_ivlen = key->iv_len;
2308c2ecf20Sopenharmony_ci			mvm->gtk_icvlen = key->icv_len;
2318c2ecf20Sopenharmony_ci			mutex_unlock(&mvm->mutex);
2328c2ecf20Sopenharmony_ci		}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci		/* don't upload key again */
2358c2ecf20Sopenharmony_ci		return;
2368c2ecf20Sopenharmony_ci	}
2378c2ecf20Sopenharmony_ci	default:
2388c2ecf20Sopenharmony_ci		data->error = true;
2398c2ecf20Sopenharmony_ci		return;
2408c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2418c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2428c2ecf20Sopenharmony_ci		data->kek_kck_cmd->igtk_cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
2438c2ecf20Sopenharmony_ci		return;
2448c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_AES_CMAC:
2458c2ecf20Sopenharmony_ci		data->kek_kck_cmd->igtk_cipher = cpu_to_le32(STA_KEY_FLG_CCM);
2468c2ecf20Sopenharmony_ci		/*
2478c2ecf20Sopenharmony_ci		 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
2488c2ecf20Sopenharmony_ci		 * but we also shouldn't abort suspend due to that. It does have
2498c2ecf20Sopenharmony_ci		 * support for the IGTK key renewal, but doesn't really use the
2508c2ecf20Sopenharmony_ci		 * IGTK for anything. This means we could spuriously wake up or
2518c2ecf20Sopenharmony_ci		 * be deauthenticated, but that was considered acceptable.
2528c2ecf20Sopenharmony_ci		 */
2538c2ecf20Sopenharmony_ci		return;
2548c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_TKIP:
2558c2ecf20Sopenharmony_ci		if (sta) {
2568c2ecf20Sopenharmony_ci			u64 pn64;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci			tkip_sc =
2598c2ecf20Sopenharmony_ci			   data->rsc_tsc->params.all_tsc_rsc.tkip.unicast_rsc;
2608c2ecf20Sopenharmony_ci			tkip_tx_sc =
2618c2ecf20Sopenharmony_ci				&data->rsc_tsc->params.all_tsc_rsc.tkip.tsc;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci			rx_p1ks = data->tkip->rx_uni;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci			pn64 = atomic64_read(&key->tx_pn);
2668c2ecf20Sopenharmony_ci			tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
2678c2ecf20Sopenharmony_ci			tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci			ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
2708c2ecf20Sopenharmony_ci						  p1k);
2718c2ecf20Sopenharmony_ci			iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci			memcpy(data->tkip->mic_keys.tx,
2748c2ecf20Sopenharmony_ci			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
2758c2ecf20Sopenharmony_ci			       IWL_MIC_KEY_SIZE);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci			rx_mic_key = data->tkip->mic_keys.rx_unicast;
2788c2ecf20Sopenharmony_ci		} else {
2798c2ecf20Sopenharmony_ci			tkip_sc =
2808c2ecf20Sopenharmony_ci			  data->rsc_tsc->params.all_tsc_rsc.tkip.multicast_rsc;
2818c2ecf20Sopenharmony_ci			rx_p1ks = data->tkip->rx_multi;
2828c2ecf20Sopenharmony_ci			rx_mic_key = data->tkip->mic_keys.rx_mcast;
2838c2ecf20Sopenharmony_ci			data->kek_kck_cmd->gtk_cipher =
2848c2ecf20Sopenharmony_ci				cpu_to_le32(STA_KEY_FLG_TKIP);
2858c2ecf20Sopenharmony_ci		}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci		/*
2888c2ecf20Sopenharmony_ci		 * For non-QoS this relies on the fact that both the uCode and
2898c2ecf20Sopenharmony_ci		 * mac80211 use TID 0 (as they need to to avoid replay attacks)
2908c2ecf20Sopenharmony_ci		 * for checking the IV in the frames.
2918c2ecf20Sopenharmony_ci		 */
2928c2ecf20Sopenharmony_ci		for (i = 0; i < IWL_NUM_RSC; i++) {
2938c2ecf20Sopenharmony_ci			ieee80211_get_key_rx_seq(key, i, &seq);
2948c2ecf20Sopenharmony_ci			tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
2958c2ecf20Sopenharmony_ci			tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
2968c2ecf20Sopenharmony_ci			/* wrapping isn't allowed, AP must rekey */
2978c2ecf20Sopenharmony_ci			if (seq.tkip.iv32 > cur_rx_iv32)
2988c2ecf20Sopenharmony_ci				cur_rx_iv32 = seq.tkip.iv32;
2998c2ecf20Sopenharmony_ci		}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
3028c2ecf20Sopenharmony_ci					  cur_rx_iv32, p1k);
3038c2ecf20Sopenharmony_ci		iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
3048c2ecf20Sopenharmony_ci		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
3058c2ecf20Sopenharmony_ci					  cur_rx_iv32 + 1, p1k);
3068c2ecf20Sopenharmony_ci		iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci		memcpy(rx_mic_key,
3098c2ecf20Sopenharmony_ci		       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3108c2ecf20Sopenharmony_ci		       IWL_MIC_KEY_SIZE);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci		data->use_tkip = true;
3138c2ecf20Sopenharmony_ci		data->use_rsc_tsc = true;
3148c2ecf20Sopenharmony_ci		break;
3158c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_CCMP:
3168c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_GCMP:
3178c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_GCMP_256:
3188c2ecf20Sopenharmony_ci		if (sta) {
3198c2ecf20Sopenharmony_ci			u64 pn64;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci			aes_sc =
3228c2ecf20Sopenharmony_ci			   data->rsc_tsc->params.all_tsc_rsc.aes.unicast_rsc;
3238c2ecf20Sopenharmony_ci			aes_tx_sc =
3248c2ecf20Sopenharmony_ci				&data->rsc_tsc->params.all_tsc_rsc.aes.tsc;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci			pn64 = atomic64_read(&key->tx_pn);
3278c2ecf20Sopenharmony_ci			aes_tx_sc->pn = cpu_to_le64(pn64);
3288c2ecf20Sopenharmony_ci		} else {
3298c2ecf20Sopenharmony_ci			aes_sc =
3308c2ecf20Sopenharmony_ci			   data->rsc_tsc->params.all_tsc_rsc.aes.multicast_rsc;
3318c2ecf20Sopenharmony_ci			data->kek_kck_cmd->gtk_cipher =
3328c2ecf20Sopenharmony_ci				key->cipher == WLAN_CIPHER_SUITE_CCMP ?
3338c2ecf20Sopenharmony_ci				cpu_to_le32(STA_KEY_FLG_CCM) :
3348c2ecf20Sopenharmony_ci				cpu_to_le32(STA_KEY_FLG_GCMP);
3358c2ecf20Sopenharmony_ci		}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci		/*
3388c2ecf20Sopenharmony_ci		 * For non-QoS this relies on the fact that both the uCode and
3398c2ecf20Sopenharmony_ci		 * mac80211/our RX code use TID 0 for checking the PN.
3408c2ecf20Sopenharmony_ci		 */
3418c2ecf20Sopenharmony_ci		if (sta && iwl_mvm_has_new_rx_api(mvm)) {
3428c2ecf20Sopenharmony_ci			struct iwl_mvm_sta *mvmsta;
3438c2ecf20Sopenharmony_ci			struct iwl_mvm_key_pn *ptk_pn;
3448c2ecf20Sopenharmony_ci			const u8 *pn;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3478c2ecf20Sopenharmony_ci			rcu_read_lock();
3488c2ecf20Sopenharmony_ci			ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
3498c2ecf20Sopenharmony_ci			if (WARN_ON(!ptk_pn)) {
3508c2ecf20Sopenharmony_ci				rcu_read_unlock();
3518c2ecf20Sopenharmony_ci				break;
3528c2ecf20Sopenharmony_ci			}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
3558c2ecf20Sopenharmony_ci				pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
3568c2ecf20Sopenharmony_ci						mvm->trans->num_rx_queues);
3578c2ecf20Sopenharmony_ci				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
3588c2ecf20Sopenharmony_ci							   ((u64)pn[4] << 8) |
3598c2ecf20Sopenharmony_ci							   ((u64)pn[3] << 16) |
3608c2ecf20Sopenharmony_ci							   ((u64)pn[2] << 24) |
3618c2ecf20Sopenharmony_ci							   ((u64)pn[1] << 32) |
3628c2ecf20Sopenharmony_ci							   ((u64)pn[0] << 40));
3638c2ecf20Sopenharmony_ci			}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci			rcu_read_unlock();
3668c2ecf20Sopenharmony_ci		} else {
3678c2ecf20Sopenharmony_ci			for (i = 0; i < IWL_NUM_RSC; i++) {
3688c2ecf20Sopenharmony_ci				u8 *pn = seq.ccmp.pn;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci				ieee80211_get_key_rx_seq(key, i, &seq);
3718c2ecf20Sopenharmony_ci				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
3728c2ecf20Sopenharmony_ci							   ((u64)pn[4] << 8) |
3738c2ecf20Sopenharmony_ci							   ((u64)pn[3] << 16) |
3748c2ecf20Sopenharmony_ci							   ((u64)pn[2] << 24) |
3758c2ecf20Sopenharmony_ci							   ((u64)pn[1] << 32) |
3768c2ecf20Sopenharmony_ci							   ((u64)pn[0] << 40));
3778c2ecf20Sopenharmony_ci			}
3788c2ecf20Sopenharmony_ci		}
3798c2ecf20Sopenharmony_ci		data->use_rsc_tsc = true;
3808c2ecf20Sopenharmony_ci		break;
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	IWL_DEBUG_WOWLAN(mvm, "GTK cipher %d\n", data->kek_kck_cmd->gtk_cipher);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	if (data->configure_keys) {
3868c2ecf20Sopenharmony_ci		mutex_lock(&mvm->mutex);
3878c2ecf20Sopenharmony_ci		/*
3888c2ecf20Sopenharmony_ci		 * The D3 firmware hardcodes the key offset 0 as the key it
3898c2ecf20Sopenharmony_ci		 * uses to transmit packets to the AP, i.e. the PTK.
3908c2ecf20Sopenharmony_ci		 */
3918c2ecf20Sopenharmony_ci		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3928c2ecf20Sopenharmony_ci			mvm->ptk_ivlen = key->iv_len;
3938c2ecf20Sopenharmony_ci			mvm->ptk_icvlen = key->icv_len;
3948c2ecf20Sopenharmony_ci			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
3958c2ecf20Sopenharmony_ci		} else {
3968c2ecf20Sopenharmony_ci			/*
3978c2ecf20Sopenharmony_ci			 * firmware only supports TSC/RSC for a single key,
3988c2ecf20Sopenharmony_ci			 * so if there are multiple keep overwriting them
3998c2ecf20Sopenharmony_ci			 * with new ones -- this relies on mac80211 doing
4008c2ecf20Sopenharmony_ci			 * list_add_tail().
4018c2ecf20Sopenharmony_ci			 */
4028c2ecf20Sopenharmony_ci			mvm->gtk_ivlen = key->iv_len;
4038c2ecf20Sopenharmony_ci			mvm->gtk_icvlen = key->icv_len;
4048c2ecf20Sopenharmony_ci			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
4058c2ecf20Sopenharmony_ci		}
4068c2ecf20Sopenharmony_ci		mutex_unlock(&mvm->mutex);
4078c2ecf20Sopenharmony_ci		data->error = ret != 0;
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic int iwl_mvm_send_patterns_v1(struct iwl_mvm *mvm,
4128c2ecf20Sopenharmony_ci				    struct cfg80211_wowlan *wowlan)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	struct iwl_wowlan_patterns_cmd_v1 *pattern_cmd;
4158c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
4168c2ecf20Sopenharmony_ci		.id = WOWLAN_PATTERNS,
4178c2ecf20Sopenharmony_ci		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
4188c2ecf20Sopenharmony_ci	};
4198c2ecf20Sopenharmony_ci	int i, err;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	if (!wowlan->n_patterns)
4228c2ecf20Sopenharmony_ci		return 0;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
4278c2ecf20Sopenharmony_ci	if (!pattern_cmd)
4288c2ecf20Sopenharmony_ci		return -ENOMEM;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	for (i = 0; i < wowlan->n_patterns; i++) {
4338c2ecf20Sopenharmony_ci		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci		memcpy(&pattern_cmd->patterns[i].mask,
4368c2ecf20Sopenharmony_ci		       wowlan->patterns[i].mask, mask_len);
4378c2ecf20Sopenharmony_ci		memcpy(&pattern_cmd->patterns[i].pattern,
4388c2ecf20Sopenharmony_ci		       wowlan->patterns[i].pattern,
4398c2ecf20Sopenharmony_ci		       wowlan->patterns[i].pattern_len);
4408c2ecf20Sopenharmony_ci		pattern_cmd->patterns[i].mask_size = mask_len;
4418c2ecf20Sopenharmony_ci		pattern_cmd->patterns[i].pattern_size =
4428c2ecf20Sopenharmony_ci			wowlan->patterns[i].pattern_len;
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	cmd.data[0] = pattern_cmd;
4468c2ecf20Sopenharmony_ci	err = iwl_mvm_send_cmd(mvm, &cmd);
4478c2ecf20Sopenharmony_ci	kfree(pattern_cmd);
4488c2ecf20Sopenharmony_ci	return err;
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
4528c2ecf20Sopenharmony_ci				 struct cfg80211_wowlan *wowlan)
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	struct iwl_wowlan_patterns_cmd *pattern_cmd;
4558c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
4568c2ecf20Sopenharmony_ci		.id = WOWLAN_PATTERNS,
4578c2ecf20Sopenharmony_ci		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
4588c2ecf20Sopenharmony_ci	};
4598c2ecf20Sopenharmony_ci	int i, err;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	if (!wowlan->n_patterns)
4628c2ecf20Sopenharmony_ci		return 0;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	cmd.len[0] = sizeof(*pattern_cmd) +
4658c2ecf20Sopenharmony_ci		wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v2);
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
4688c2ecf20Sopenharmony_ci	if (!pattern_cmd)
4698c2ecf20Sopenharmony_ci		return -ENOMEM;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	for (i = 0; i < wowlan->n_patterns; i++) {
4748c2ecf20Sopenharmony_ci		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci		pattern_cmd->patterns[i].pattern_type =
4778c2ecf20Sopenharmony_ci			WOWLAN_PATTERN_TYPE_BITMASK;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci		memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
4808c2ecf20Sopenharmony_ci		       wowlan->patterns[i].mask, mask_len);
4818c2ecf20Sopenharmony_ci		memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
4828c2ecf20Sopenharmony_ci		       wowlan->patterns[i].pattern,
4838c2ecf20Sopenharmony_ci		       wowlan->patterns[i].pattern_len);
4848c2ecf20Sopenharmony_ci		pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
4858c2ecf20Sopenharmony_ci		pattern_cmd->patterns[i].u.bitmask.pattern_size =
4868c2ecf20Sopenharmony_ci			wowlan->patterns[i].pattern_len;
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	cmd.data[0] = pattern_cmd;
4908c2ecf20Sopenharmony_ci	err = iwl_mvm_send_cmd(mvm, &cmd);
4918c2ecf20Sopenharmony_ci	kfree(pattern_cmd);
4928c2ecf20Sopenharmony_ci	return err;
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4968c2ecf20Sopenharmony_ci				struct ieee80211_sta *ap_sta)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4998c2ecf20Sopenharmony_ci	struct ieee80211_chanctx_conf *ctx;
5008c2ecf20Sopenharmony_ci	u8 chains_static, chains_dynamic;
5018c2ecf20Sopenharmony_ci	struct cfg80211_chan_def chandef;
5028c2ecf20Sopenharmony_ci	int ret, i;
5038c2ecf20Sopenharmony_ci	struct iwl_binding_cmd_v1 binding_cmd = {};
5048c2ecf20Sopenharmony_ci	struct iwl_time_quota_cmd quota_cmd = {};
5058c2ecf20Sopenharmony_ci	struct iwl_time_quota_data *quota;
5068c2ecf20Sopenharmony_ci	u32 status;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm)))
5098c2ecf20Sopenharmony_ci		return -EINVAL;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	/* add back the PHY */
5128c2ecf20Sopenharmony_ci	if (WARN_ON(!mvmvif->phy_ctxt))
5138c2ecf20Sopenharmony_ci		return -EINVAL;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	rcu_read_lock();
5168c2ecf20Sopenharmony_ci	ctx = rcu_dereference(vif->chanctx_conf);
5178c2ecf20Sopenharmony_ci	if (WARN_ON(!ctx)) {
5188c2ecf20Sopenharmony_ci		rcu_read_unlock();
5198c2ecf20Sopenharmony_ci		return -EINVAL;
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ci	chandef = ctx->def;
5228c2ecf20Sopenharmony_ci	chains_static = ctx->rx_chains_static;
5238c2ecf20Sopenharmony_ci	chains_dynamic = ctx->rx_chains_dynamic;
5248c2ecf20Sopenharmony_ci	rcu_read_unlock();
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
5278c2ecf20Sopenharmony_ci				   chains_static, chains_dynamic);
5288c2ecf20Sopenharmony_ci	if (ret)
5298c2ecf20Sopenharmony_ci		return ret;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	/* add back the MAC */
5328c2ecf20Sopenharmony_ci	mvmvif->uploaded = false;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	if (WARN_ON(!vif->bss_conf.assoc))
5358c2ecf20Sopenharmony_ci		return -EINVAL;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
5388c2ecf20Sopenharmony_ci	if (ret)
5398c2ecf20Sopenharmony_ci		return ret;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	/* add back binding - XXX refactor? */
5428c2ecf20Sopenharmony_ci	binding_cmd.id_and_color =
5438c2ecf20Sopenharmony_ci		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
5448c2ecf20Sopenharmony_ci						mvmvif->phy_ctxt->color));
5458c2ecf20Sopenharmony_ci	binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
5468c2ecf20Sopenharmony_ci	binding_cmd.phy =
5478c2ecf20Sopenharmony_ci		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
5488c2ecf20Sopenharmony_ci						mvmvif->phy_ctxt->color));
5498c2ecf20Sopenharmony_ci	binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5508c2ecf20Sopenharmony_ci							      mvmvif->color));
5518c2ecf20Sopenharmony_ci	for (i = 1; i < MAX_MACS_IN_BINDING; i++)
5528c2ecf20Sopenharmony_ci		binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	status = 0;
5558c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
5568c2ecf20Sopenharmony_ci					  IWL_BINDING_CMD_SIZE_V1, &binding_cmd,
5578c2ecf20Sopenharmony_ci					  &status);
5588c2ecf20Sopenharmony_ci	if (ret) {
5598c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
5608c2ecf20Sopenharmony_ci		return ret;
5618c2ecf20Sopenharmony_ci	}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	if (status) {
5648c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Binding command failed: %u\n", status);
5658c2ecf20Sopenharmony_ci		return -EIO;
5668c2ecf20Sopenharmony_ci	}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
5698c2ecf20Sopenharmony_ci	if (ret)
5708c2ecf20Sopenharmony_ci		return ret;
5718c2ecf20Sopenharmony_ci	rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
5748c2ecf20Sopenharmony_ci	if (ret)
5758c2ecf20Sopenharmony_ci		return ret;
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	/* and some quota */
5788c2ecf20Sopenharmony_ci	quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, 0);
5798c2ecf20Sopenharmony_ci	quota->id_and_color =
5808c2ecf20Sopenharmony_ci		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
5818c2ecf20Sopenharmony_ci						mvmvif->phy_ctxt->color));
5828c2ecf20Sopenharmony_ci	quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
5838c2ecf20Sopenharmony_ci	quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	for (i = 1; i < MAX_BINDINGS; i++) {
5868c2ecf20Sopenharmony_ci		quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, i);
5878c2ecf20Sopenharmony_ci		quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
5888c2ecf20Sopenharmony_ci	}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
5918c2ecf20Sopenharmony_ci				   iwl_mvm_quota_cmd_size(mvm), &quota_cmd);
5928c2ecf20Sopenharmony_ci	if (ret)
5938c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
5968c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	return 0;
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_cistatic int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
6028c2ecf20Sopenharmony_ci				       struct ieee80211_vif *vif)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6058c2ecf20Sopenharmony_ci	struct iwl_nonqos_seq_query_cmd query_cmd = {
6068c2ecf20Sopenharmony_ci		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
6078c2ecf20Sopenharmony_ci		.mac_id_n_color =
6088c2ecf20Sopenharmony_ci			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
6098c2ecf20Sopenharmony_ci							mvmvif->color)),
6108c2ecf20Sopenharmony_ci	};
6118c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
6128c2ecf20Sopenharmony_ci		.id = NON_QOS_TX_COUNTER_CMD,
6138c2ecf20Sopenharmony_ci		.flags = CMD_WANT_SKB,
6148c2ecf20Sopenharmony_ci	};
6158c2ecf20Sopenharmony_ci	int err;
6168c2ecf20Sopenharmony_ci	u32 size;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	cmd.data[0] = &query_cmd;
6198c2ecf20Sopenharmony_ci	cmd.len[0] = sizeof(query_cmd);
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	err = iwl_mvm_send_cmd(mvm, &cmd);
6228c2ecf20Sopenharmony_ci	if (err)
6238c2ecf20Sopenharmony_ci		return err;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	size = iwl_rx_packet_payload_len(cmd.resp_pkt);
6268c2ecf20Sopenharmony_ci	if (size < sizeof(__le16)) {
6278c2ecf20Sopenharmony_ci		err = -EINVAL;
6288c2ecf20Sopenharmony_ci	} else {
6298c2ecf20Sopenharmony_ci		err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
6308c2ecf20Sopenharmony_ci		/* firmware returns next, not last-used seqno */
6318c2ecf20Sopenharmony_ci		err = (u16) (err - 0x10);
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	iwl_free_resp(&cmd);
6358c2ecf20Sopenharmony_ci	return err;
6368c2ecf20Sopenharmony_ci}
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_civoid iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
6398c2ecf20Sopenharmony_ci{
6408c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6418c2ecf20Sopenharmony_ci	struct iwl_nonqos_seq_query_cmd query_cmd = {
6428c2ecf20Sopenharmony_ci		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
6438c2ecf20Sopenharmony_ci		.mac_id_n_color =
6448c2ecf20Sopenharmony_ci			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
6458c2ecf20Sopenharmony_ci							mvmvif->color)),
6468c2ecf20Sopenharmony_ci		.value = cpu_to_le16(mvmvif->seqno),
6478c2ecf20Sopenharmony_ci	};
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	/* return if called during restart, not resume from D3 */
6508c2ecf20Sopenharmony_ci	if (!mvmvif->seqno_valid)
6518c2ecf20Sopenharmony_ci		return;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	mvmvif->seqno_valid = false;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
6568c2ecf20Sopenharmony_ci				 sizeof(query_cmd), &query_cmd))
6578c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "failed to set non-QoS seqno\n");
6588c2ecf20Sopenharmony_ci}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_cistatic int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	iwl_mvm_stop_device(mvm);
6658c2ecf20Sopenharmony_ci	/*
6668c2ecf20Sopenharmony_ci	 * Set the HW restart bit -- this is mostly true as we're
6678c2ecf20Sopenharmony_ci	 * going to load new firmware and reprogram that, though
6688c2ecf20Sopenharmony_ci	 * the reprogramming is going to be manual to avoid adding
6698c2ecf20Sopenharmony_ci	 * all the MACs that aren't support.
6708c2ecf20Sopenharmony_ci	 * We don't have to clear up everything though because the
6718c2ecf20Sopenharmony_ci	 * reprogramming is manual. When we resume, we'll actually
6728c2ecf20Sopenharmony_ci	 * go through a proper restart sequence again to switch
6738c2ecf20Sopenharmony_ci	 * back to the runtime firmware image.
6748c2ecf20Sopenharmony_ci	 */
6758c2ecf20Sopenharmony_ci	set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	/* the fw is reset, so all the keys are cleared */
6788c2ecf20Sopenharmony_ci	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	mvm->ptk_ivlen = 0;
6818c2ecf20Sopenharmony_ci	mvm->ptk_icvlen = 0;
6828c2ecf20Sopenharmony_ci	mvm->ptk_ivlen = 0;
6838c2ecf20Sopenharmony_ci	mvm->ptk_icvlen = 0;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	return iwl_mvm_load_d3_fw(mvm);
6868c2ecf20Sopenharmony_ci}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_cistatic int
6898c2ecf20Sopenharmony_ciiwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
6908c2ecf20Sopenharmony_ci			  struct cfg80211_wowlan *wowlan,
6918c2ecf20Sopenharmony_ci			  struct iwl_wowlan_config_cmd *wowlan_config_cmd,
6928c2ecf20Sopenharmony_ci			  struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
6938c2ecf20Sopenharmony_ci			  struct ieee80211_sta *ap_sta)
6948c2ecf20Sopenharmony_ci{
6958c2ecf20Sopenharmony_ci	int ret;
6968c2ecf20Sopenharmony_ci	struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	/* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	wowlan_config_cmd->is_11n_connection =
7018c2ecf20Sopenharmony_ci					ap_sta->ht_cap.ht_supported;
7028c2ecf20Sopenharmony_ci	wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
7038c2ecf20Sopenharmony_ci		ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	/* Query the last used seqno and set it */
7068c2ecf20Sopenharmony_ci	ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
7078c2ecf20Sopenharmony_ci	if (ret < 0)
7088c2ecf20Sopenharmony_ci		return ret;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	if (wowlan->disconnect)
7158c2ecf20Sopenharmony_ci		wowlan_config_cmd->wakeup_filter |=
7168c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
7178c2ecf20Sopenharmony_ci				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
7188c2ecf20Sopenharmony_ci	if (wowlan->magic_pkt)
7198c2ecf20Sopenharmony_ci		wowlan_config_cmd->wakeup_filter |=
7208c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
7218c2ecf20Sopenharmony_ci	if (wowlan->gtk_rekey_failure)
7228c2ecf20Sopenharmony_ci		wowlan_config_cmd->wakeup_filter |=
7238c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
7248c2ecf20Sopenharmony_ci	if (wowlan->eap_identity_req)
7258c2ecf20Sopenharmony_ci		wowlan_config_cmd->wakeup_filter |=
7268c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
7278c2ecf20Sopenharmony_ci	if (wowlan->four_way_handshake)
7288c2ecf20Sopenharmony_ci		wowlan_config_cmd->wakeup_filter |=
7298c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
7308c2ecf20Sopenharmony_ci	if (wowlan->n_patterns)
7318c2ecf20Sopenharmony_ci		wowlan_config_cmd->wakeup_filter |=
7328c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	if (wowlan->rfkill_release)
7358c2ecf20Sopenharmony_ci		wowlan_config_cmd->wakeup_filter |=
7368c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	if (wowlan->tcp) {
7398c2ecf20Sopenharmony_ci		/*
7408c2ecf20Sopenharmony_ci		 * Set the "link change" (really "link lost") flag as well
7418c2ecf20Sopenharmony_ci		 * since that implies losing the TCP connection.
7428c2ecf20Sopenharmony_ci		 */
7438c2ecf20Sopenharmony_ci		wowlan_config_cmd->wakeup_filter |=
7448c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
7458c2ecf20Sopenharmony_ci				    IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
7468c2ecf20Sopenharmony_ci				    IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
7478c2ecf20Sopenharmony_ci				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
7488c2ecf20Sopenharmony_ci	}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	if (wowlan->any) {
7518c2ecf20Sopenharmony_ci		wowlan_config_cmd->wakeup_filter |=
7528c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
7538c2ecf20Sopenharmony_ci				    IWL_WOWLAN_WAKEUP_LINK_CHANGE |
7548c2ecf20Sopenharmony_ci				    IWL_WOWLAN_WAKEUP_RX_FRAME |
7558c2ecf20Sopenharmony_ci				    IWL_WOWLAN_WAKEUP_BCN_FILTERING);
7568c2ecf20Sopenharmony_ci	}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	return 0;
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_cistatic int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
7628c2ecf20Sopenharmony_ci					    struct ieee80211_vif *vif,
7638c2ecf20Sopenharmony_ci					    u32 cmd_flags)
7648c2ecf20Sopenharmony_ci{
7658c2ecf20Sopenharmony_ci	struct iwl_wowlan_kek_kck_material_cmd_v3 kek_kck_cmd = {};
7668c2ecf20Sopenharmony_ci	struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
7678c2ecf20Sopenharmony_ci	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
7688c2ecf20Sopenharmony_ci				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
7698c2ecf20Sopenharmony_ci	struct wowlan_key_data key_data = {
7708c2ecf20Sopenharmony_ci		.configure_keys = !unified,
7718c2ecf20Sopenharmony_ci		.use_rsc_tsc = false,
7728c2ecf20Sopenharmony_ci		.tkip = &tkip_cmd,
7738c2ecf20Sopenharmony_ci		.use_tkip = false,
7748c2ecf20Sopenharmony_ci		.kek_kck_cmd = &kek_kck_cmd,
7758c2ecf20Sopenharmony_ci	};
7768c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
7778c2ecf20Sopenharmony_ci	int ret;
7788c2ecf20Sopenharmony_ci	u8 cmd_ver;
7798c2ecf20Sopenharmony_ci	size_t cmd_size;
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
7828c2ecf20Sopenharmony_ci	if (!key_data.rsc_tsc)
7838c2ecf20Sopenharmony_ci		return -ENOMEM;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	/*
7868c2ecf20Sopenharmony_ci	 * if we have to configure keys, call ieee80211_iter_keys(),
7878c2ecf20Sopenharmony_ci	 * as we need non-atomic context in order to take the
7888c2ecf20Sopenharmony_ci	 * required locks.
7898c2ecf20Sopenharmony_ci	 */
7908c2ecf20Sopenharmony_ci	/*
7918c2ecf20Sopenharmony_ci	 * Note that currently we don't propagate cmd_flags
7928c2ecf20Sopenharmony_ci	 * to the iterator. In case of key_data.configure_keys,
7938c2ecf20Sopenharmony_ci	 * all the configured commands are SYNC, and
7948c2ecf20Sopenharmony_ci	 * iwl_mvm_wowlan_program_keys() will take care of
7958c2ecf20Sopenharmony_ci	 * locking/unlocking mvm->mutex.
7968c2ecf20Sopenharmony_ci	 */
7978c2ecf20Sopenharmony_ci	ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_program_keys,
7988c2ecf20Sopenharmony_ci			    &key_data);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	if (key_data.error) {
8018c2ecf20Sopenharmony_ci		ret = -EIO;
8028c2ecf20Sopenharmony_ci		goto out;
8038c2ecf20Sopenharmony_ci	}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	if (key_data.use_rsc_tsc) {
8068c2ecf20Sopenharmony_ci		int ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
8078c2ecf20Sopenharmony_ci						WOWLAN_TSC_RSC_PARAM,
8088c2ecf20Sopenharmony_ci						IWL_FW_CMD_VER_UNKNOWN);
8098c2ecf20Sopenharmony_ci		int size;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci		if (ver == 4) {
8128c2ecf20Sopenharmony_ci			size = sizeof(*key_data.rsc_tsc);
8138c2ecf20Sopenharmony_ci			key_data.rsc_tsc->sta_id =
8148c2ecf20Sopenharmony_ci				cpu_to_le32(mvmvif->ap_sta_id);
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci		} else if (ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN) {
8178c2ecf20Sopenharmony_ci			size = sizeof(key_data.rsc_tsc->params);
8188c2ecf20Sopenharmony_ci		} else {
8198c2ecf20Sopenharmony_ci			ret = 0;
8208c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
8218c2ecf20Sopenharmony_ci			goto out;
8228c2ecf20Sopenharmony_ci		}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci		ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
8258c2ecf20Sopenharmony_ci					   cmd_flags,
8268c2ecf20Sopenharmony_ci					   size,
8278c2ecf20Sopenharmony_ci					   key_data.rsc_tsc);
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci		if (ret)
8308c2ecf20Sopenharmony_ci			goto out;
8318c2ecf20Sopenharmony_ci	}
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	if (key_data.use_tkip &&
8348c2ecf20Sopenharmony_ci	    !fw_has_api(&mvm->fw->ucode_capa,
8358c2ecf20Sopenharmony_ci			IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
8368c2ecf20Sopenharmony_ci		int ver = iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
8378c2ecf20Sopenharmony_ci						WOWLAN_TKIP_PARAM,
8388c2ecf20Sopenharmony_ci						IWL_FW_CMD_VER_UNKNOWN);
8398c2ecf20Sopenharmony_ci		int size;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci		if (ver == 2) {
8428c2ecf20Sopenharmony_ci			size = sizeof(tkip_cmd);
8438c2ecf20Sopenharmony_ci			key_data.tkip->sta_id =
8448c2ecf20Sopenharmony_ci				cpu_to_le32(mvmvif->ap_sta_id);
8458c2ecf20Sopenharmony_ci		} else if (ver == 1 || ver == IWL_FW_CMD_VER_UNKNOWN) {
8468c2ecf20Sopenharmony_ci			size = sizeof(struct iwl_wowlan_tkip_params_cmd_ver_1);
8478c2ecf20Sopenharmony_ci		} else {
8488c2ecf20Sopenharmony_ci			ret =  -EINVAL;
8498c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
8508c2ecf20Sopenharmony_ci			goto out;
8518c2ecf20Sopenharmony_ci		}
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci		/* send relevant data according to CMD version */
8548c2ecf20Sopenharmony_ci		ret = iwl_mvm_send_cmd_pdu(mvm,
8558c2ecf20Sopenharmony_ci					   WOWLAN_TKIP_PARAM,
8568c2ecf20Sopenharmony_ci					   cmd_flags, size,
8578c2ecf20Sopenharmony_ci					   &tkip_cmd);
8588c2ecf20Sopenharmony_ci		if (ret)
8598c2ecf20Sopenharmony_ci			goto out;
8608c2ecf20Sopenharmony_ci	}
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	/* configure rekey data only if offloaded rekey is supported (d3) */
8638c2ecf20Sopenharmony_ci	if (mvmvif->rekey_data.valid) {
8648c2ecf20Sopenharmony_ci		cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
8658c2ecf20Sopenharmony_ci						IWL_ALWAYS_LONG_GROUP,
8668c2ecf20Sopenharmony_ci						WOWLAN_KEK_KCK_MATERIAL,
8678c2ecf20Sopenharmony_ci						IWL_FW_CMD_VER_UNKNOWN);
8688c2ecf20Sopenharmony_ci		if (WARN_ON(cmd_ver != 2 && cmd_ver != 3 &&
8698c2ecf20Sopenharmony_ci			    cmd_ver != IWL_FW_CMD_VER_UNKNOWN))
8708c2ecf20Sopenharmony_ci			return -EINVAL;
8718c2ecf20Sopenharmony_ci		if (cmd_ver == 3)
8728c2ecf20Sopenharmony_ci			cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v3);
8738c2ecf20Sopenharmony_ci		else
8748c2ecf20Sopenharmony_ci			cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v2);
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci		memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
8778c2ecf20Sopenharmony_ci		       mvmvif->rekey_data.kck_len);
8788c2ecf20Sopenharmony_ci		kek_kck_cmd.kck_len = cpu_to_le16(mvmvif->rekey_data.kck_len);
8798c2ecf20Sopenharmony_ci		memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
8808c2ecf20Sopenharmony_ci		       mvmvif->rekey_data.kek_len);
8818c2ecf20Sopenharmony_ci		kek_kck_cmd.kek_len = cpu_to_le16(mvmvif->rekey_data.kek_len);
8828c2ecf20Sopenharmony_ci		kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
8838c2ecf20Sopenharmony_ci		kek_kck_cmd.akm = cpu_to_le32(mvmvif->rekey_data.akm);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci		IWL_DEBUG_WOWLAN(mvm, "setting akm %d\n",
8868c2ecf20Sopenharmony_ci				 mvmvif->rekey_data.akm);
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci		ret = iwl_mvm_send_cmd_pdu(mvm,
8898c2ecf20Sopenharmony_ci					   WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
8908c2ecf20Sopenharmony_ci					   cmd_size,
8918c2ecf20Sopenharmony_ci					   &kek_kck_cmd);
8928c2ecf20Sopenharmony_ci		if (ret)
8938c2ecf20Sopenharmony_ci			goto out;
8948c2ecf20Sopenharmony_ci	}
8958c2ecf20Sopenharmony_ci	ret = 0;
8968c2ecf20Sopenharmony_ciout:
8978c2ecf20Sopenharmony_ci	kfree(key_data.rsc_tsc);
8988c2ecf20Sopenharmony_ci	return ret;
8998c2ecf20Sopenharmony_ci}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_cistatic int
9028c2ecf20Sopenharmony_ciiwl_mvm_wowlan_config(struct iwl_mvm *mvm,
9038c2ecf20Sopenharmony_ci		      struct cfg80211_wowlan *wowlan,
9048c2ecf20Sopenharmony_ci		      struct iwl_wowlan_config_cmd *wowlan_config_cmd,
9058c2ecf20Sopenharmony_ci		      struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
9068c2ecf20Sopenharmony_ci		      struct ieee80211_sta *ap_sta)
9078c2ecf20Sopenharmony_ci{
9088c2ecf20Sopenharmony_ci	int ret;
9098c2ecf20Sopenharmony_ci	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
9108c2ecf20Sopenharmony_ci					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	mvm->offload_tid = wowlan_config_cmd->offloading_tid;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	if (!unified_image) {
9158c2ecf20Sopenharmony_ci		ret = iwl_mvm_switch_to_d3(mvm);
9168c2ecf20Sopenharmony_ci		if (ret)
9178c2ecf20Sopenharmony_ci			return ret;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci		ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
9208c2ecf20Sopenharmony_ci		if (ret)
9218c2ecf20Sopenharmony_ci			return ret;
9228c2ecf20Sopenharmony_ci	}
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci	/*
9258c2ecf20Sopenharmony_ci	 * This needs to be unlocked due to lock ordering
9268c2ecf20Sopenharmony_ci	 * constraints. Since we're in the suspend path
9278c2ecf20Sopenharmony_ci	 * that isn't really a problem though.
9288c2ecf20Sopenharmony_ci	 */
9298c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
9308c2ecf20Sopenharmony_ci	ret = iwl_mvm_wowlan_config_key_params(mvm, vif, CMD_ASYNC);
9318c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
9328c2ecf20Sopenharmony_ci	if (ret)
9338c2ecf20Sopenharmony_ci		return ret;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
9368c2ecf20Sopenharmony_ci				   sizeof(*wowlan_config_cmd),
9378c2ecf20Sopenharmony_ci				   wowlan_config_cmd);
9388c2ecf20Sopenharmony_ci	if (ret)
9398c2ecf20Sopenharmony_ci		return ret;
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	if (fw_has_api(&mvm->fw->ucode_capa,
9428c2ecf20Sopenharmony_ci		       IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE))
9438c2ecf20Sopenharmony_ci		ret = iwl_mvm_send_patterns(mvm, wowlan);
9448c2ecf20Sopenharmony_ci	else
9458c2ecf20Sopenharmony_ci		ret = iwl_mvm_send_patterns_v1(mvm, wowlan);
9468c2ecf20Sopenharmony_ci	if (ret)
9478c2ecf20Sopenharmony_ci		return ret;
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
9508c2ecf20Sopenharmony_ci}
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_cistatic int
9538c2ecf20Sopenharmony_ciiwl_mvm_netdetect_config(struct iwl_mvm *mvm,
9548c2ecf20Sopenharmony_ci			 struct cfg80211_wowlan *wowlan,
9558c2ecf20Sopenharmony_ci			 struct cfg80211_sched_scan_request *nd_config,
9568c2ecf20Sopenharmony_ci			 struct ieee80211_vif *vif)
9578c2ecf20Sopenharmony_ci{
9588c2ecf20Sopenharmony_ci	struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
9598c2ecf20Sopenharmony_ci	int ret;
9608c2ecf20Sopenharmony_ci	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
9618c2ecf20Sopenharmony_ci					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	if (!unified_image) {
9648c2ecf20Sopenharmony_ci		ret = iwl_mvm_switch_to_d3(mvm);
9658c2ecf20Sopenharmony_ci		if (ret)
9668c2ecf20Sopenharmony_ci			return ret;
9678c2ecf20Sopenharmony_ci	} else {
9688c2ecf20Sopenharmony_ci		/* In theory, we wouldn't have to stop a running sched
9698c2ecf20Sopenharmony_ci		 * scan in order to start another one (for
9708c2ecf20Sopenharmony_ci		 * net-detect).  But in practice this doesn't seem to
9718c2ecf20Sopenharmony_ci		 * work properly, so stop any running sched_scan now.
9728c2ecf20Sopenharmony_ci		 */
9738c2ecf20Sopenharmony_ci		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
9748c2ecf20Sopenharmony_ci		if (ret)
9758c2ecf20Sopenharmony_ci			return ret;
9768c2ecf20Sopenharmony_ci	}
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	/* rfkill release can be either for wowlan or netdetect */
9798c2ecf20Sopenharmony_ci	if (wowlan->rfkill_release)
9808c2ecf20Sopenharmony_ci		wowlan_config_cmd.wakeup_filter |=
9818c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	wowlan_config_cmd.sta_id = mvm->aux_sta.sta_id;
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
9868c2ecf20Sopenharmony_ci				   sizeof(wowlan_config_cmd),
9878c2ecf20Sopenharmony_ci				   &wowlan_config_cmd);
9888c2ecf20Sopenharmony_ci	if (ret)
9898c2ecf20Sopenharmony_ci		return ret;
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
9928c2ecf20Sopenharmony_ci				       IWL_MVM_SCAN_NETDETECT);
9938c2ecf20Sopenharmony_ci	if (ret)
9948c2ecf20Sopenharmony_ci		return ret;
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
9978c2ecf20Sopenharmony_ci		return -EBUSY;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	/* save the sched scan matchsets... */
10008c2ecf20Sopenharmony_ci	if (nd_config->n_match_sets) {
10018c2ecf20Sopenharmony_ci		mvm->nd_match_sets = kmemdup(nd_config->match_sets,
10028c2ecf20Sopenharmony_ci					     sizeof(*nd_config->match_sets) *
10038c2ecf20Sopenharmony_ci					     nd_config->n_match_sets,
10048c2ecf20Sopenharmony_ci					     GFP_KERNEL);
10058c2ecf20Sopenharmony_ci		if (mvm->nd_match_sets)
10068c2ecf20Sopenharmony_ci			mvm->n_nd_match_sets = nd_config->n_match_sets;
10078c2ecf20Sopenharmony_ci	}
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	/* ...and the sched scan channels for later reporting */
10108c2ecf20Sopenharmony_ci	mvm->nd_channels = kmemdup(nd_config->channels,
10118c2ecf20Sopenharmony_ci				   sizeof(*nd_config->channels) *
10128c2ecf20Sopenharmony_ci				   nd_config->n_channels,
10138c2ecf20Sopenharmony_ci				   GFP_KERNEL);
10148c2ecf20Sopenharmony_ci	if (mvm->nd_channels)
10158c2ecf20Sopenharmony_ci		mvm->n_nd_channels = nd_config->n_channels;
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci	return 0;
10188c2ecf20Sopenharmony_ci}
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_cistatic void iwl_mvm_free_nd(struct iwl_mvm *mvm)
10218c2ecf20Sopenharmony_ci{
10228c2ecf20Sopenharmony_ci	kfree(mvm->nd_match_sets);
10238c2ecf20Sopenharmony_ci	mvm->nd_match_sets = NULL;
10248c2ecf20Sopenharmony_ci	mvm->n_nd_match_sets = 0;
10258c2ecf20Sopenharmony_ci	kfree(mvm->nd_channels);
10268c2ecf20Sopenharmony_ci	mvm->nd_channels = NULL;
10278c2ecf20Sopenharmony_ci	mvm->n_nd_channels = 0;
10288c2ecf20Sopenharmony_ci}
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_cistatic int __iwl_mvm_suspend(struct ieee80211_hw *hw,
10318c2ecf20Sopenharmony_ci			     struct cfg80211_wowlan *wowlan,
10328c2ecf20Sopenharmony_ci			     bool test)
10338c2ecf20Sopenharmony_ci{
10348c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
10358c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif = NULL;
10368c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = NULL;
10378c2ecf20Sopenharmony_ci	struct ieee80211_sta *ap_sta = NULL;
10388c2ecf20Sopenharmony_ci	struct iwl_d3_manager_config d3_cfg_cmd_data = {
10398c2ecf20Sopenharmony_ci		/*
10408c2ecf20Sopenharmony_ci		 * Program the minimum sleep time to 10 seconds, as many
10418c2ecf20Sopenharmony_ci		 * platforms have issues processing a wakeup signal while
10428c2ecf20Sopenharmony_ci		 * still being in the process of suspending.
10438c2ecf20Sopenharmony_ci		 */
10448c2ecf20Sopenharmony_ci		.min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
10458c2ecf20Sopenharmony_ci	};
10468c2ecf20Sopenharmony_ci	struct iwl_host_cmd d3_cfg_cmd = {
10478c2ecf20Sopenharmony_ci		.id = D3_CONFIG_CMD,
10488c2ecf20Sopenharmony_ci		.flags = CMD_WANT_SKB,
10498c2ecf20Sopenharmony_ci		.data[0] = &d3_cfg_cmd_data,
10508c2ecf20Sopenharmony_ci		.len[0] = sizeof(d3_cfg_cmd_data),
10518c2ecf20Sopenharmony_ci	};
10528c2ecf20Sopenharmony_ci	int ret;
10538c2ecf20Sopenharmony_ci	int len __maybe_unused;
10548c2ecf20Sopenharmony_ci	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
10558c2ecf20Sopenharmony_ci					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	if (!wowlan) {
10588c2ecf20Sopenharmony_ci		/*
10598c2ecf20Sopenharmony_ci		 * mac80211 shouldn't get here, but for D3 test
10608c2ecf20Sopenharmony_ci		 * it doesn't warrant a warning
10618c2ecf20Sopenharmony_ci		 */
10628c2ecf20Sopenharmony_ci		WARN_ON(!test);
10638c2ecf20Sopenharmony_ci		return -EINVAL;
10648c2ecf20Sopenharmony_ci	}
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	vif = iwl_mvm_get_bss_vif(mvm);
10718c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(vif)) {
10728c2ecf20Sopenharmony_ci		ret = 1;
10738c2ecf20Sopenharmony_ci		goto out_noreset;
10748c2ecf20Sopenharmony_ci	}
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	mvmvif = iwl_mvm_vif_from_mac80211(vif);
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA) {
10798c2ecf20Sopenharmony_ci		/* if we're not associated, this must be netdetect */
10808c2ecf20Sopenharmony_ci		if (!wowlan->nd_config) {
10818c2ecf20Sopenharmony_ci			ret = 1;
10828c2ecf20Sopenharmony_ci			goto out_noreset;
10838c2ecf20Sopenharmony_ci		}
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci		ret = iwl_mvm_netdetect_config(
10868c2ecf20Sopenharmony_ci			mvm, wowlan, wowlan->nd_config, vif);
10878c2ecf20Sopenharmony_ci		if (ret)
10888c2ecf20Sopenharmony_ci			goto out;
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci		mvm->net_detect = true;
10918c2ecf20Sopenharmony_ci	} else {
10928c2ecf20Sopenharmony_ci		struct iwl_wowlan_config_cmd wowlan_config_cmd = {
10938c2ecf20Sopenharmony_ci			.offloading_tid = 0,
10948c2ecf20Sopenharmony_ci		};
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci		wowlan_config_cmd.sta_id = mvmvif->ap_sta_id;
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci		ap_sta = rcu_dereference_protected(
10998c2ecf20Sopenharmony_ci			mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
11008c2ecf20Sopenharmony_ci			lockdep_is_held(&mvm->mutex));
11018c2ecf20Sopenharmony_ci		if (IS_ERR_OR_NULL(ap_sta)) {
11028c2ecf20Sopenharmony_ci			ret = -EINVAL;
11038c2ecf20Sopenharmony_ci			goto out_noreset;
11048c2ecf20Sopenharmony_ci		}
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci		ret = iwl_mvm_sta_ensure_queue(
11078c2ecf20Sopenharmony_ci			mvm, ap_sta->txq[wowlan_config_cmd.offloading_tid]);
11088c2ecf20Sopenharmony_ci		if (ret)
11098c2ecf20Sopenharmony_ci			goto out_noreset;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci		ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
11128c2ecf20Sopenharmony_ci						vif, mvmvif, ap_sta);
11138c2ecf20Sopenharmony_ci		if (ret)
11148c2ecf20Sopenharmony_ci			goto out_noreset;
11158c2ecf20Sopenharmony_ci		ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
11168c2ecf20Sopenharmony_ci					    vif, mvmvif, ap_sta);
11178c2ecf20Sopenharmony_ci		if (ret)
11188c2ecf20Sopenharmony_ci			goto out;
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci		mvm->net_detect = false;
11218c2ecf20Sopenharmony_ci	}
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	ret = iwl_mvm_power_update_device(mvm);
11248c2ecf20Sopenharmony_ci	if (ret)
11258c2ecf20Sopenharmony_ci		goto out;
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ci	ret = iwl_mvm_power_update_mac(mvm);
11288c2ecf20Sopenharmony_ci	if (ret)
11298c2ecf20Sopenharmony_ci		goto out;
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLWIFI_DEBUGFS
11328c2ecf20Sopenharmony_ci	if (mvm->d3_wake_sysassert)
11338c2ecf20Sopenharmony_ci		d3_cfg_cmd_data.wakeup_flags |=
11348c2ecf20Sopenharmony_ci			cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
11358c2ecf20Sopenharmony_ci#endif
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	/*
11388c2ecf20Sopenharmony_ci	 * Prior to 9000 device family the driver needs to stop the dbg
11398c2ecf20Sopenharmony_ci	 * recording before entering D3. In later devices the FW stops the
11408c2ecf20Sopenharmony_ci	 * recording automatically.
11418c2ecf20Sopenharmony_ci	 */
11428c2ecf20Sopenharmony_ci	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
11438c2ecf20Sopenharmony_ci		iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	/* must be last -- this switches firmware state */
11468c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
11478c2ecf20Sopenharmony_ci	if (ret)
11488c2ecf20Sopenharmony_ci		goto out;
11498c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLWIFI_DEBUGFS
11508c2ecf20Sopenharmony_ci	len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
11518c2ecf20Sopenharmony_ci	if (len >= sizeof(u32)) {
11528c2ecf20Sopenharmony_ci		mvm->d3_test_pme_ptr =
11538c2ecf20Sopenharmony_ci			le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
11548c2ecf20Sopenharmony_ci	}
11558c2ecf20Sopenharmony_ci#endif
11568c2ecf20Sopenharmony_ci	iwl_free_resp(&d3_cfg_cmd);
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci	ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
11618c2ecf20Sopenharmony_ci out:
11628c2ecf20Sopenharmony_ci	if (ret < 0) {
11638c2ecf20Sopenharmony_ci		iwl_mvm_free_nd(mvm);
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci		if (!unified_image) {
11668c2ecf20Sopenharmony_ci			if (mvm->fw_restart > 0) {
11678c2ecf20Sopenharmony_ci				mvm->fw_restart--;
11688c2ecf20Sopenharmony_ci				ieee80211_restart_hw(mvm->hw);
11698c2ecf20Sopenharmony_ci			}
11708c2ecf20Sopenharmony_ci		}
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci		clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
11738c2ecf20Sopenharmony_ci	}
11748c2ecf20Sopenharmony_ci out_noreset:
11758c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	return ret;
11788c2ecf20Sopenharmony_ci}
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ciint iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
11818c2ecf20Sopenharmony_ci{
11828c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
11838c2ecf20Sopenharmony_ci	struct iwl_trans *trans = mvm->trans;
11848c2ecf20Sopenharmony_ci	int ret;
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	iwl_mvm_pause_tcm(mvm, true);
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	iwl_fw_runtime_suspend(&mvm->fwrt);
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	ret = iwl_trans_suspend(trans);
11918c2ecf20Sopenharmony_ci	if (ret)
11928c2ecf20Sopenharmony_ci		return ret;
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	return __iwl_mvm_suspend(hw, wowlan, false);
11978c2ecf20Sopenharmony_ci}
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci/* converted data from the different status responses */
12008c2ecf20Sopenharmony_cistruct iwl_wowlan_status_data {
12018c2ecf20Sopenharmony_ci	u16 pattern_number;
12028c2ecf20Sopenharmony_ci	u16 qos_seq_ctr[8];
12038c2ecf20Sopenharmony_ci	u32 wakeup_reasons;
12048c2ecf20Sopenharmony_ci	u32 wake_packet_length;
12058c2ecf20Sopenharmony_ci	u32 wake_packet_bufsize;
12068c2ecf20Sopenharmony_ci	const u8 *wake_packet;
12078c2ecf20Sopenharmony_ci};
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_cistatic void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
12108c2ecf20Sopenharmony_ci					  struct ieee80211_vif *vif,
12118c2ecf20Sopenharmony_ci					  struct iwl_wowlan_status_data *status)
12128c2ecf20Sopenharmony_ci{
12138c2ecf20Sopenharmony_ci	struct sk_buff *pkt = NULL;
12148c2ecf20Sopenharmony_ci	struct cfg80211_wowlan_wakeup wakeup = {
12158c2ecf20Sopenharmony_ci		.pattern_idx = -1,
12168c2ecf20Sopenharmony_ci	};
12178c2ecf20Sopenharmony_ci	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
12188c2ecf20Sopenharmony_ci	u32 reasons = status->wakeup_reasons;
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
12218c2ecf20Sopenharmony_ci		wakeup_report = NULL;
12228c2ecf20Sopenharmony_ci		goto report;
12238c2ecf20Sopenharmony_ci	}
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	pm_wakeup_event(mvm->dev, 0);
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci	if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
12288c2ecf20Sopenharmony_ci		wakeup.magic_pkt = true;
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
12318c2ecf20Sopenharmony_ci		wakeup.pattern_idx =
12328c2ecf20Sopenharmony_ci			status->pattern_number;
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
12358c2ecf20Sopenharmony_ci		       IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
12368c2ecf20Sopenharmony_ci		wakeup.disconnect = true;
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
12398c2ecf20Sopenharmony_ci		wakeup.gtk_rekey_failure = true;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
12428c2ecf20Sopenharmony_ci		wakeup.rfkill_release = true;
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci	if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
12458c2ecf20Sopenharmony_ci		wakeup.eap_identity_req = true;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
12488c2ecf20Sopenharmony_ci		wakeup.four_way_handshake = true;
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
12518c2ecf20Sopenharmony_ci		wakeup.tcp_connlost = true;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
12548c2ecf20Sopenharmony_ci		wakeup.tcp_nomoretokens = true;
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
12578c2ecf20Sopenharmony_ci		wakeup.tcp_match = true;
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	if (status->wake_packet_bufsize) {
12608c2ecf20Sopenharmony_ci		int pktsize = status->wake_packet_bufsize;
12618c2ecf20Sopenharmony_ci		int pktlen = status->wake_packet_length;
12628c2ecf20Sopenharmony_ci		const u8 *pktdata = status->wake_packet;
12638c2ecf20Sopenharmony_ci		struct ieee80211_hdr *hdr = (void *)pktdata;
12648c2ecf20Sopenharmony_ci		int truncated = pktlen - pktsize;
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci		/* this would be a firmware bug */
12678c2ecf20Sopenharmony_ci		if (WARN_ON_ONCE(truncated < 0))
12688c2ecf20Sopenharmony_ci			truncated = 0;
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci		if (ieee80211_is_data(hdr->frame_control)) {
12718c2ecf20Sopenharmony_ci			int hdrlen = ieee80211_hdrlen(hdr->frame_control);
12728c2ecf20Sopenharmony_ci			int ivlen = 0, icvlen = 4; /* also FCS */
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci			pkt = alloc_skb(pktsize, GFP_KERNEL);
12758c2ecf20Sopenharmony_ci			if (!pkt)
12768c2ecf20Sopenharmony_ci				goto report;
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci			skb_put_data(pkt, pktdata, hdrlen);
12798c2ecf20Sopenharmony_ci			pktdata += hdrlen;
12808c2ecf20Sopenharmony_ci			pktsize -= hdrlen;
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci			if (ieee80211_has_protected(hdr->frame_control)) {
12838c2ecf20Sopenharmony_ci				/*
12848c2ecf20Sopenharmony_ci				 * This is unlocked and using gtk_i(c)vlen,
12858c2ecf20Sopenharmony_ci				 * but since everything is under RTNL still
12868c2ecf20Sopenharmony_ci				 * that's not really a problem - changing
12878c2ecf20Sopenharmony_ci				 * it would be difficult.
12888c2ecf20Sopenharmony_ci				 */
12898c2ecf20Sopenharmony_ci				if (is_multicast_ether_addr(hdr->addr1)) {
12908c2ecf20Sopenharmony_ci					ivlen = mvm->gtk_ivlen;
12918c2ecf20Sopenharmony_ci					icvlen += mvm->gtk_icvlen;
12928c2ecf20Sopenharmony_ci				} else {
12938c2ecf20Sopenharmony_ci					ivlen = mvm->ptk_ivlen;
12948c2ecf20Sopenharmony_ci					icvlen += mvm->ptk_icvlen;
12958c2ecf20Sopenharmony_ci				}
12968c2ecf20Sopenharmony_ci			}
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci			/* if truncated, FCS/ICV is (partially) gone */
12998c2ecf20Sopenharmony_ci			if (truncated >= icvlen) {
13008c2ecf20Sopenharmony_ci				icvlen = 0;
13018c2ecf20Sopenharmony_ci				truncated -= icvlen;
13028c2ecf20Sopenharmony_ci			} else {
13038c2ecf20Sopenharmony_ci				icvlen -= truncated;
13048c2ecf20Sopenharmony_ci				truncated = 0;
13058c2ecf20Sopenharmony_ci			}
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci			pktsize -= ivlen + icvlen;
13088c2ecf20Sopenharmony_ci			pktdata += ivlen;
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci			skb_put_data(pkt, pktdata, pktsize);
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_ci			if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
13138c2ecf20Sopenharmony_ci				goto report;
13148c2ecf20Sopenharmony_ci			wakeup.packet = pkt->data;
13158c2ecf20Sopenharmony_ci			wakeup.packet_present_len = pkt->len;
13168c2ecf20Sopenharmony_ci			wakeup.packet_len = pkt->len - truncated;
13178c2ecf20Sopenharmony_ci			wakeup.packet_80211 = false;
13188c2ecf20Sopenharmony_ci		} else {
13198c2ecf20Sopenharmony_ci			int fcslen = 4;
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci			if (truncated >= 4) {
13228c2ecf20Sopenharmony_ci				truncated -= 4;
13238c2ecf20Sopenharmony_ci				fcslen = 0;
13248c2ecf20Sopenharmony_ci			} else {
13258c2ecf20Sopenharmony_ci				fcslen -= truncated;
13268c2ecf20Sopenharmony_ci				truncated = 0;
13278c2ecf20Sopenharmony_ci			}
13288c2ecf20Sopenharmony_ci			pktsize -= fcslen;
13298c2ecf20Sopenharmony_ci			wakeup.packet = status->wake_packet;
13308c2ecf20Sopenharmony_ci			wakeup.packet_present_len = pktsize;
13318c2ecf20Sopenharmony_ci			wakeup.packet_len = pktlen - truncated;
13328c2ecf20Sopenharmony_ci			wakeup.packet_80211 = true;
13338c2ecf20Sopenharmony_ci		}
13348c2ecf20Sopenharmony_ci	}
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci report:
13378c2ecf20Sopenharmony_ci	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
13388c2ecf20Sopenharmony_ci	kfree_skb(pkt);
13398c2ecf20Sopenharmony_ci}
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_cistatic void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
13428c2ecf20Sopenharmony_ci				  struct ieee80211_key_seq *seq)
13438c2ecf20Sopenharmony_ci{
13448c2ecf20Sopenharmony_ci	u64 pn;
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci	pn = le64_to_cpu(sc->pn);
13478c2ecf20Sopenharmony_ci	seq->ccmp.pn[0] = pn >> 40;
13488c2ecf20Sopenharmony_ci	seq->ccmp.pn[1] = pn >> 32;
13498c2ecf20Sopenharmony_ci	seq->ccmp.pn[2] = pn >> 24;
13508c2ecf20Sopenharmony_ci	seq->ccmp.pn[3] = pn >> 16;
13518c2ecf20Sopenharmony_ci	seq->ccmp.pn[4] = pn >> 8;
13528c2ecf20Sopenharmony_ci	seq->ccmp.pn[5] = pn;
13538c2ecf20Sopenharmony_ci}
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_cistatic void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
13568c2ecf20Sopenharmony_ci				   struct ieee80211_key_seq *seq)
13578c2ecf20Sopenharmony_ci{
13588c2ecf20Sopenharmony_ci	seq->tkip.iv32 = le32_to_cpu(sc->iv32);
13598c2ecf20Sopenharmony_ci	seq->tkip.iv16 = le16_to_cpu(sc->iv16);
13608c2ecf20Sopenharmony_ci}
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_cistatic void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
13638c2ecf20Sopenharmony_ci				   struct ieee80211_sta *sta,
13648c2ecf20Sopenharmony_ci				   struct ieee80211_key_conf *key)
13658c2ecf20Sopenharmony_ci{
13668c2ecf20Sopenharmony_ci	int tid;
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	if (sta && iwl_mvm_has_new_rx_api(mvm)) {
13718c2ecf20Sopenharmony_ci		struct iwl_mvm_sta *mvmsta;
13728c2ecf20Sopenharmony_ci		struct iwl_mvm_key_pn *ptk_pn;
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci		mvmsta = iwl_mvm_sta_from_mac80211(sta);
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci		rcu_read_lock();
13778c2ecf20Sopenharmony_ci		ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
13788c2ecf20Sopenharmony_ci		if (WARN_ON(!ptk_pn)) {
13798c2ecf20Sopenharmony_ci			rcu_read_unlock();
13808c2ecf20Sopenharmony_ci			return;
13818c2ecf20Sopenharmony_ci		}
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci		for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
13848c2ecf20Sopenharmony_ci			struct ieee80211_key_seq seq = {};
13858c2ecf20Sopenharmony_ci			int i;
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci			iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
13888c2ecf20Sopenharmony_ci			ieee80211_set_key_rx_seq(key, tid, &seq);
13898c2ecf20Sopenharmony_ci			for (i = 1; i < mvm->trans->num_rx_queues; i++)
13908c2ecf20Sopenharmony_ci				memcpy(ptk_pn->q[i].pn[tid],
13918c2ecf20Sopenharmony_ci				       seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
13928c2ecf20Sopenharmony_ci		}
13938c2ecf20Sopenharmony_ci		rcu_read_unlock();
13948c2ecf20Sopenharmony_ci	} else {
13958c2ecf20Sopenharmony_ci		for (tid = 0; tid < IWL_NUM_RSC; tid++) {
13968c2ecf20Sopenharmony_ci			struct ieee80211_key_seq seq = {};
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci			iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
13998c2ecf20Sopenharmony_ci			ieee80211_set_key_rx_seq(key, tid, &seq);
14008c2ecf20Sopenharmony_ci		}
14018c2ecf20Sopenharmony_ci	}
14028c2ecf20Sopenharmony_ci}
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_cistatic void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
14058c2ecf20Sopenharmony_ci				    struct ieee80211_key_conf *key)
14068c2ecf20Sopenharmony_ci{
14078c2ecf20Sopenharmony_ci	int tid;
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_ci	BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci	for (tid = 0; tid < IWL_NUM_RSC; tid++) {
14128c2ecf20Sopenharmony_ci		struct ieee80211_key_seq seq = {};
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci		iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
14158c2ecf20Sopenharmony_ci		ieee80211_set_key_rx_seq(key, tid, &seq);
14168c2ecf20Sopenharmony_ci	}
14178c2ecf20Sopenharmony_ci}
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_cistatic void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
14208c2ecf20Sopenharmony_ci				   struct ieee80211_key_conf *key,
14218c2ecf20Sopenharmony_ci				   struct iwl_wowlan_status *status)
14228c2ecf20Sopenharmony_ci{
14238c2ecf20Sopenharmony_ci	union iwl_all_tsc_rsc *rsc = &status->gtk[0].rsc.all_tsc_rsc;
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci	switch (key->cipher) {
14268c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_CCMP:
14278c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_GCMP:
14288c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_GCMP_256:
14298c2ecf20Sopenharmony_ci		iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
14308c2ecf20Sopenharmony_ci		break;
14318c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_TKIP:
14328c2ecf20Sopenharmony_ci		iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
14338c2ecf20Sopenharmony_ci		break;
14348c2ecf20Sopenharmony_ci	default:
14358c2ecf20Sopenharmony_ci		WARN_ON(1);
14368c2ecf20Sopenharmony_ci	}
14378c2ecf20Sopenharmony_ci}
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_cistruct iwl_mvm_d3_gtk_iter_data {
14408c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm;
14418c2ecf20Sopenharmony_ci	struct iwl_wowlan_status *status;
14428c2ecf20Sopenharmony_ci	void *last_gtk;
14438c2ecf20Sopenharmony_ci	u32 cipher;
14448c2ecf20Sopenharmony_ci	bool find_phase, unhandled_cipher;
14458c2ecf20Sopenharmony_ci	int num_keys;
14468c2ecf20Sopenharmony_ci};
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_cistatic void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
14498c2ecf20Sopenharmony_ci				   struct ieee80211_vif *vif,
14508c2ecf20Sopenharmony_ci				   struct ieee80211_sta *sta,
14518c2ecf20Sopenharmony_ci				   struct ieee80211_key_conf *key,
14528c2ecf20Sopenharmony_ci				   void *_data)
14538c2ecf20Sopenharmony_ci{
14548c2ecf20Sopenharmony_ci	struct iwl_mvm_d3_gtk_iter_data *data = _data;
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci	if (data->unhandled_cipher)
14578c2ecf20Sopenharmony_ci		return;
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	switch (key->cipher) {
14608c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_WEP40:
14618c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_WEP104:
14628c2ecf20Sopenharmony_ci		/* ignore WEP completely, nothing to do */
14638c2ecf20Sopenharmony_ci		return;
14648c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_CCMP:
14658c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_GCMP:
14668c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_GCMP_256:
14678c2ecf20Sopenharmony_ci	case WLAN_CIPHER_SUITE_TKIP:
14688c2ecf20Sopenharmony_ci		/* we support these */
14698c2ecf20Sopenharmony_ci		break;
14708c2ecf20Sopenharmony_ci	default:
14718c2ecf20Sopenharmony_ci		/* everything else (even CMAC for MFP) - disconnect from AP */
14728c2ecf20Sopenharmony_ci		data->unhandled_cipher = true;
14738c2ecf20Sopenharmony_ci		return;
14748c2ecf20Sopenharmony_ci	}
14758c2ecf20Sopenharmony_ci
14768c2ecf20Sopenharmony_ci	data->num_keys++;
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci	/*
14798c2ecf20Sopenharmony_ci	 * pairwise key - update sequence counters only;
14808c2ecf20Sopenharmony_ci	 * note that this assumes no TDLS sessions are active
14818c2ecf20Sopenharmony_ci	 */
14828c2ecf20Sopenharmony_ci	if (sta) {
14838c2ecf20Sopenharmony_ci		struct ieee80211_key_seq seq = {};
14848c2ecf20Sopenharmony_ci		union iwl_all_tsc_rsc *sc =
14858c2ecf20Sopenharmony_ci			&data->status->gtk[0].rsc.all_tsc_rsc;
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci		if (data->find_phase)
14888c2ecf20Sopenharmony_ci			return;
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_ci		switch (key->cipher) {
14918c2ecf20Sopenharmony_ci		case WLAN_CIPHER_SUITE_CCMP:
14928c2ecf20Sopenharmony_ci		case WLAN_CIPHER_SUITE_GCMP:
14938c2ecf20Sopenharmony_ci		case WLAN_CIPHER_SUITE_GCMP_256:
14948c2ecf20Sopenharmony_ci			iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
14958c2ecf20Sopenharmony_ci					       sta, key);
14968c2ecf20Sopenharmony_ci			atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
14978c2ecf20Sopenharmony_ci			break;
14988c2ecf20Sopenharmony_ci		case WLAN_CIPHER_SUITE_TKIP:
14998c2ecf20Sopenharmony_ci			iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
15008c2ecf20Sopenharmony_ci			iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
15018c2ecf20Sopenharmony_ci			atomic64_set(&key->tx_pn,
15028c2ecf20Sopenharmony_ci				     (u64)seq.tkip.iv16 |
15038c2ecf20Sopenharmony_ci				     ((u64)seq.tkip.iv32 << 16));
15048c2ecf20Sopenharmony_ci			break;
15058c2ecf20Sopenharmony_ci		}
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_ci		/* that's it for this key */
15088c2ecf20Sopenharmony_ci		return;
15098c2ecf20Sopenharmony_ci	}
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci	if (data->find_phase) {
15128c2ecf20Sopenharmony_ci		data->last_gtk = key;
15138c2ecf20Sopenharmony_ci		data->cipher = key->cipher;
15148c2ecf20Sopenharmony_ci		return;
15158c2ecf20Sopenharmony_ci	}
15168c2ecf20Sopenharmony_ci
15178c2ecf20Sopenharmony_ci	if (data->status->num_of_gtk_rekeys)
15188c2ecf20Sopenharmony_ci		ieee80211_remove_key(key);
15198c2ecf20Sopenharmony_ci	else if (data->last_gtk == key)
15208c2ecf20Sopenharmony_ci		iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
15218c2ecf20Sopenharmony_ci}
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_cistatic bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
15248c2ecf20Sopenharmony_ci					  struct ieee80211_vif *vif,
15258c2ecf20Sopenharmony_ci					  struct iwl_wowlan_status *status)
15268c2ecf20Sopenharmony_ci{
15278c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
15288c2ecf20Sopenharmony_ci	struct iwl_mvm_d3_gtk_iter_data gtkdata = {
15298c2ecf20Sopenharmony_ci		.mvm = mvm,
15308c2ecf20Sopenharmony_ci		.status = status,
15318c2ecf20Sopenharmony_ci	};
15328c2ecf20Sopenharmony_ci	u32 disconnection_reasons =
15338c2ecf20Sopenharmony_ci		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
15348c2ecf20Sopenharmony_ci		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci	if (!status || !vif->bss_conf.bssid)
15378c2ecf20Sopenharmony_ci		return false;
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_ci	if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
15408c2ecf20Sopenharmony_ci		return false;
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	/* find last GTK that we used initially, if any */
15438c2ecf20Sopenharmony_ci	gtkdata.find_phase = true;
15448c2ecf20Sopenharmony_ci	ieee80211_iter_keys(mvm->hw, vif,
15458c2ecf20Sopenharmony_ci			    iwl_mvm_d3_update_keys, &gtkdata);
15468c2ecf20Sopenharmony_ci	/* not trying to keep connections with MFP/unhandled ciphers */
15478c2ecf20Sopenharmony_ci	if (gtkdata.unhandled_cipher)
15488c2ecf20Sopenharmony_ci		return false;
15498c2ecf20Sopenharmony_ci	if (!gtkdata.num_keys)
15508c2ecf20Sopenharmony_ci		goto out;
15518c2ecf20Sopenharmony_ci	if (!gtkdata.last_gtk)
15528c2ecf20Sopenharmony_ci		return false;
15538c2ecf20Sopenharmony_ci
15548c2ecf20Sopenharmony_ci	/*
15558c2ecf20Sopenharmony_ci	 * invalidate all other GTKs that might still exist and update
15568c2ecf20Sopenharmony_ci	 * the one that we used
15578c2ecf20Sopenharmony_ci	 */
15588c2ecf20Sopenharmony_ci	gtkdata.find_phase = false;
15598c2ecf20Sopenharmony_ci	ieee80211_iter_keys(mvm->hw, vif,
15608c2ecf20Sopenharmony_ci			    iwl_mvm_d3_update_keys, &gtkdata);
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_ci	IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
15638c2ecf20Sopenharmony_ci			 le32_to_cpu(status->num_of_gtk_rekeys));
15648c2ecf20Sopenharmony_ci	if (status->num_of_gtk_rekeys) {
15658c2ecf20Sopenharmony_ci		struct ieee80211_key_conf *key;
15668c2ecf20Sopenharmony_ci		struct {
15678c2ecf20Sopenharmony_ci			struct ieee80211_key_conf conf;
15688c2ecf20Sopenharmony_ci			u8 key[32];
15698c2ecf20Sopenharmony_ci		} conf = {
15708c2ecf20Sopenharmony_ci			.conf.cipher = gtkdata.cipher,
15718c2ecf20Sopenharmony_ci			.conf.keyidx =
15728c2ecf20Sopenharmony_ci				iwlmvm_wowlan_gtk_idx(&status->gtk[0]),
15738c2ecf20Sopenharmony_ci		};
15748c2ecf20Sopenharmony_ci		__be64 replay_ctr;
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_ci		IWL_DEBUG_WOWLAN(mvm,
15778c2ecf20Sopenharmony_ci				 "Received from FW GTK cipher %d, key index %d\n",
15788c2ecf20Sopenharmony_ci				 conf.conf.cipher, conf.conf.keyidx);
15798c2ecf20Sopenharmony_ci		switch (gtkdata.cipher) {
15808c2ecf20Sopenharmony_ci		case WLAN_CIPHER_SUITE_CCMP:
15818c2ecf20Sopenharmony_ci		case WLAN_CIPHER_SUITE_GCMP:
15828c2ecf20Sopenharmony_ci			BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
15838c2ecf20Sopenharmony_ci			BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP);
15848c2ecf20Sopenharmony_ci			conf.conf.keylen = WLAN_KEY_LEN_CCMP;
15858c2ecf20Sopenharmony_ci			memcpy(conf.conf.key, status->gtk[0].key,
15868c2ecf20Sopenharmony_ci			       WLAN_KEY_LEN_CCMP);
15878c2ecf20Sopenharmony_ci			break;
15888c2ecf20Sopenharmony_ci		case WLAN_CIPHER_SUITE_GCMP_256:
15898c2ecf20Sopenharmony_ci			BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256);
15908c2ecf20Sopenharmony_ci			conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
15918c2ecf20Sopenharmony_ci			memcpy(conf.conf.key, status->gtk[0].key,
15928c2ecf20Sopenharmony_ci			       WLAN_KEY_LEN_GCMP_256);
15938c2ecf20Sopenharmony_ci			break;
15948c2ecf20Sopenharmony_ci		case WLAN_CIPHER_SUITE_TKIP:
15958c2ecf20Sopenharmony_ci			BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP);
15968c2ecf20Sopenharmony_ci			conf.conf.keylen = WLAN_KEY_LEN_TKIP;
15978c2ecf20Sopenharmony_ci			memcpy(conf.conf.key, status->gtk[0].key, 16);
15988c2ecf20Sopenharmony_ci			/* leave TX MIC key zeroed, we don't use it anyway */
15998c2ecf20Sopenharmony_ci			memcpy(conf.conf.key +
16008c2ecf20Sopenharmony_ci			       NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
16018c2ecf20Sopenharmony_ci			       status->gtk[0].tkip_mic_key, 8);
16028c2ecf20Sopenharmony_ci			break;
16038c2ecf20Sopenharmony_ci		}
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci		key = ieee80211_gtk_rekey_add(vif, &conf.conf);
16068c2ecf20Sopenharmony_ci		if (IS_ERR(key))
16078c2ecf20Sopenharmony_ci			return false;
16088c2ecf20Sopenharmony_ci		iwl_mvm_set_key_rx_seq(mvm, key, status);
16098c2ecf20Sopenharmony_ci
16108c2ecf20Sopenharmony_ci		replay_ctr =
16118c2ecf20Sopenharmony_ci			cpu_to_be64(le64_to_cpu(status->replay_ctr));
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci		ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
16148c2ecf20Sopenharmony_ci					   (void *)&replay_ctr, GFP_KERNEL);
16158c2ecf20Sopenharmony_ci	}
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_ciout:
16188c2ecf20Sopenharmony_ci	mvmvif->seqno_valid = true;
16198c2ecf20Sopenharmony_ci	/* +0x10 because the set API expects next-to-use, not last-used */
16208c2ecf20Sopenharmony_ci	mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
16218c2ecf20Sopenharmony_ci
16228c2ecf20Sopenharmony_ci	return true;
16238c2ecf20Sopenharmony_ci}
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci/* Occasionally, templates would be nice. This is one of those times ... */
16268c2ecf20Sopenharmony_ci#define iwl_mvm_parse_wowlan_status_common(_ver)			\
16278c2ecf20Sopenharmony_cistatic struct iwl_wowlan_status *					\
16288c2ecf20Sopenharmony_ciiwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm,	\
16298c2ecf20Sopenharmony_ci					    void *_data, int len)	\
16308c2ecf20Sopenharmony_ci{									\
16318c2ecf20Sopenharmony_ci	struct iwl_wowlan_status *status;				\
16328c2ecf20Sopenharmony_ci	struct iwl_wowlan_status_ ##_ver *data = _data;			\
16338c2ecf20Sopenharmony_ci	int data_size;							\
16348c2ecf20Sopenharmony_ci									\
16358c2ecf20Sopenharmony_ci	if (len < sizeof(*data)) {					\
16368c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
16378c2ecf20Sopenharmony_ci		return ERR_PTR(-EIO);					\
16388c2ecf20Sopenharmony_ci	}								\
16398c2ecf20Sopenharmony_ci									\
16408c2ecf20Sopenharmony_ci	data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4);	\
16418c2ecf20Sopenharmony_ci	if (len != sizeof(*data) + data_size) {				\
16428c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
16438c2ecf20Sopenharmony_ci		return ERR_PTR(-EIO);					\
16448c2ecf20Sopenharmony_ci	}								\
16458c2ecf20Sopenharmony_ci									\
16468c2ecf20Sopenharmony_ci	status = kzalloc(sizeof(*status) + data_size, GFP_KERNEL);	\
16478c2ecf20Sopenharmony_ci	if (!status)							\
16488c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);				\
16498c2ecf20Sopenharmony_ci									\
16508c2ecf20Sopenharmony_ci	/* copy all the common fields */				\
16518c2ecf20Sopenharmony_ci	status->replay_ctr = data->replay_ctr;				\
16528c2ecf20Sopenharmony_ci	status->pattern_number = data->pattern_number;			\
16538c2ecf20Sopenharmony_ci	status->non_qos_seq_ctr = data->non_qos_seq_ctr;		\
16548c2ecf20Sopenharmony_ci	memcpy(status->qos_seq_ctr, data->qos_seq_ctr,			\
16558c2ecf20Sopenharmony_ci	       sizeof(status->qos_seq_ctr));				\
16568c2ecf20Sopenharmony_ci	status->wakeup_reasons = data->wakeup_reasons;			\
16578c2ecf20Sopenharmony_ci	status->num_of_gtk_rekeys = data->num_of_gtk_rekeys;		\
16588c2ecf20Sopenharmony_ci	status->received_beacons = data->received_beacons;		\
16598c2ecf20Sopenharmony_ci	status->wake_packet_length = data->wake_packet_length;		\
16608c2ecf20Sopenharmony_ci	status->wake_packet_bufsize = data->wake_packet_bufsize;	\
16618c2ecf20Sopenharmony_ci	memcpy(status->wake_packet, data->wake_packet,			\
16628c2ecf20Sopenharmony_ci	       le32_to_cpu(status->wake_packet_bufsize));		\
16638c2ecf20Sopenharmony_ci									\
16648c2ecf20Sopenharmony_ci	return status;							\
16658c2ecf20Sopenharmony_ci}
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ciiwl_mvm_parse_wowlan_status_common(v6)
16688c2ecf20Sopenharmony_ciiwl_mvm_parse_wowlan_status_common(v7)
16698c2ecf20Sopenharmony_ciiwl_mvm_parse_wowlan_status_common(v9)
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_cistruct iwl_wowlan_status *iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm)
16728c2ecf20Sopenharmony_ci{
16738c2ecf20Sopenharmony_ci	struct iwl_wowlan_status *status;
16748c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
16758c2ecf20Sopenharmony_ci		.id = WOWLAN_GET_STATUSES,
16768c2ecf20Sopenharmony_ci		.flags = CMD_WANT_SKB,
16778c2ecf20Sopenharmony_ci	};
16788c2ecf20Sopenharmony_ci	int ret, len;
16798c2ecf20Sopenharmony_ci	u8 notif_ver;
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd(mvm, &cmd);
16848c2ecf20Sopenharmony_ci	if (ret) {
16858c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
16868c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
16878c2ecf20Sopenharmony_ci	}
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_ci	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
16908c2ecf20Sopenharmony_ci
16918c2ecf20Sopenharmony_ci	/* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
16928c2ecf20Sopenharmony_ci	notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
16938c2ecf20Sopenharmony_ci					    WOWLAN_GET_STATUSES, 7);
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_ci	if (!fw_has_api(&mvm->fw->ucode_capa,
16968c2ecf20Sopenharmony_ci			IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL)) {
16978c2ecf20Sopenharmony_ci		struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
16988c2ecf20Sopenharmony_ci
16998c2ecf20Sopenharmony_ci		status = iwl_mvm_parse_wowlan_status_common_v6(mvm,
17008c2ecf20Sopenharmony_ci							       cmd.resp_pkt->data,
17018c2ecf20Sopenharmony_ci							       len);
17028c2ecf20Sopenharmony_ci		if (IS_ERR(status))
17038c2ecf20Sopenharmony_ci			goto out_free_resp;
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_ci		BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
17068c2ecf20Sopenharmony_ci			     sizeof(status->gtk[0].key));
17078c2ecf20Sopenharmony_ci		BUILD_BUG_ON(sizeof(v6->gtk.tkip_mic_key) >
17088c2ecf20Sopenharmony_ci			     sizeof(status->gtk[0].tkip_mic_key));
17098c2ecf20Sopenharmony_ci
17108c2ecf20Sopenharmony_ci		/* copy GTK info to the right place */
17118c2ecf20Sopenharmony_ci		memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
17128c2ecf20Sopenharmony_ci		       sizeof(v6->gtk.decrypt_key));
17138c2ecf20Sopenharmony_ci		memcpy(status->gtk[0].tkip_mic_key, v6->gtk.tkip_mic_key,
17148c2ecf20Sopenharmony_ci		       sizeof(v6->gtk.tkip_mic_key));
17158c2ecf20Sopenharmony_ci		memcpy(&status->gtk[0].rsc, &v6->gtk.rsc,
17168c2ecf20Sopenharmony_ci		       sizeof(status->gtk[0].rsc));
17178c2ecf20Sopenharmony_ci
17188c2ecf20Sopenharmony_ci		/* hardcode the key length to 16 since v6 only supports 16 */
17198c2ecf20Sopenharmony_ci		status->gtk[0].key_len = 16;
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci		/*
17228c2ecf20Sopenharmony_ci		 * The key index only uses 2 bits (values 0 to 3) and
17238c2ecf20Sopenharmony_ci		 * we always set bit 7 which means this is the
17248c2ecf20Sopenharmony_ci		 * currently used key.
17258c2ecf20Sopenharmony_ci		 */
17268c2ecf20Sopenharmony_ci		status->gtk[0].key_flags = v6->gtk.key_index | BIT(7);
17278c2ecf20Sopenharmony_ci	} else if (notif_ver == 7) {
17288c2ecf20Sopenharmony_ci		struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci		status = iwl_mvm_parse_wowlan_status_common_v7(mvm,
17318c2ecf20Sopenharmony_ci							       cmd.resp_pkt->data,
17328c2ecf20Sopenharmony_ci							       len);
17338c2ecf20Sopenharmony_ci		if (IS_ERR(status))
17348c2ecf20Sopenharmony_ci			goto out_free_resp;
17358c2ecf20Sopenharmony_ci
17368c2ecf20Sopenharmony_ci		status->gtk[0] = v7->gtk[0];
17378c2ecf20Sopenharmony_ci		status->igtk[0] = v7->igtk[0];
17388c2ecf20Sopenharmony_ci	} else if (notif_ver == 9) {
17398c2ecf20Sopenharmony_ci		struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data;
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci		status = iwl_mvm_parse_wowlan_status_common_v9(mvm,
17428c2ecf20Sopenharmony_ci							       cmd.resp_pkt->data,
17438c2ecf20Sopenharmony_ci							       len);
17448c2ecf20Sopenharmony_ci		if (IS_ERR(status))
17458c2ecf20Sopenharmony_ci			goto out_free_resp;
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci		status->gtk[0] = v9->gtk[0];
17488c2ecf20Sopenharmony_ci		status->igtk[0] = v9->igtk[0];
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_ci		status->tid_tear_down = v9->tid_tear_down;
17518c2ecf20Sopenharmony_ci	} else {
17528c2ecf20Sopenharmony_ci		IWL_ERR(mvm,
17538c2ecf20Sopenharmony_ci			"Firmware advertises unknown WoWLAN status response %d!\n",
17548c2ecf20Sopenharmony_ci			notif_ver);
17558c2ecf20Sopenharmony_ci		status = ERR_PTR(-EIO);
17568c2ecf20Sopenharmony_ci	}
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ciout_free_resp:
17598c2ecf20Sopenharmony_ci	iwl_free_resp(&cmd);
17608c2ecf20Sopenharmony_ci	return status;
17618c2ecf20Sopenharmony_ci}
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_cistatic struct iwl_wowlan_status *
17648c2ecf20Sopenharmony_ciiwl_mvm_get_wakeup_status(struct iwl_mvm *mvm)
17658c2ecf20Sopenharmony_ci{
17668c2ecf20Sopenharmony_ci	int ret;
17678c2ecf20Sopenharmony_ci
17688c2ecf20Sopenharmony_ci	/* only for tracing for now */
17698c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
17708c2ecf20Sopenharmony_ci	if (ret)
17718c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
17728c2ecf20Sopenharmony_ci
17738c2ecf20Sopenharmony_ci	return iwl_mvm_send_wowlan_get_status(mvm);
17748c2ecf20Sopenharmony_ci}
17758c2ecf20Sopenharmony_ci
17768c2ecf20Sopenharmony_ci/* releases the MVM mutex */
17778c2ecf20Sopenharmony_cistatic bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
17788c2ecf20Sopenharmony_ci					 struct ieee80211_vif *vif)
17798c2ecf20Sopenharmony_ci{
17808c2ecf20Sopenharmony_ci	struct iwl_wowlan_status_data status;
17818c2ecf20Sopenharmony_ci	struct iwl_wowlan_status *fw_status;
17828c2ecf20Sopenharmony_ci	int i;
17838c2ecf20Sopenharmony_ci	bool keep;
17848c2ecf20Sopenharmony_ci	struct iwl_mvm_sta *mvm_ap_sta;
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_ci	fw_status = iwl_mvm_get_wakeup_status(mvm);
17878c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(fw_status))
17888c2ecf20Sopenharmony_ci		goto out_unlock;
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci	IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
17918c2ecf20Sopenharmony_ci			 le32_to_cpu(fw_status->wakeup_reasons));
17928c2ecf20Sopenharmony_ci
17938c2ecf20Sopenharmony_ci	status.pattern_number = le16_to_cpu(fw_status->pattern_number);
17948c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++)
17958c2ecf20Sopenharmony_ci		status.qos_seq_ctr[i] =
17968c2ecf20Sopenharmony_ci			le16_to_cpu(fw_status->qos_seq_ctr[i]);
17978c2ecf20Sopenharmony_ci	status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
17988c2ecf20Sopenharmony_ci	status.wake_packet_length =
17998c2ecf20Sopenharmony_ci		le32_to_cpu(fw_status->wake_packet_length);
18008c2ecf20Sopenharmony_ci	status.wake_packet_bufsize =
18018c2ecf20Sopenharmony_ci		le32_to_cpu(fw_status->wake_packet_bufsize);
18028c2ecf20Sopenharmony_ci	status.wake_packet = fw_status->wake_packet;
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_ci	/* still at hard-coded place 0 for D3 image */
18058c2ecf20Sopenharmony_ci	mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
18068c2ecf20Sopenharmony_ci	if (!mvm_ap_sta)
18078c2ecf20Sopenharmony_ci		goto out_free;
18088c2ecf20Sopenharmony_ci
18098c2ecf20Sopenharmony_ci	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
18108c2ecf20Sopenharmony_ci		u16 seq = status.qos_seq_ctr[i];
18118c2ecf20Sopenharmony_ci		/* firmware stores last-used value, we store next value */
18128c2ecf20Sopenharmony_ci		seq += 0x10;
18138c2ecf20Sopenharmony_ci		mvm_ap_sta->tid_data[i].seq_number = seq;
18148c2ecf20Sopenharmony_ci	}
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
18178c2ecf20Sopenharmony_ci		i = mvm->offload_tid;
18188c2ecf20Sopenharmony_ci		iwl_trans_set_q_ptrs(mvm->trans,
18198c2ecf20Sopenharmony_ci				     mvm_ap_sta->tid_data[i].txq_id,
18208c2ecf20Sopenharmony_ci				     mvm_ap_sta->tid_data[i].seq_number >> 4);
18218c2ecf20Sopenharmony_ci	}
18228c2ecf20Sopenharmony_ci
18238c2ecf20Sopenharmony_ci	/* now we have all the data we need, unlock to avoid mac80211 issues */
18248c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
18258c2ecf20Sopenharmony_ci
18268c2ecf20Sopenharmony_ci	iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
18278c2ecf20Sopenharmony_ci
18288c2ecf20Sopenharmony_ci	keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	kfree(fw_status);
18318c2ecf20Sopenharmony_ci	return keep;
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_ciout_free:
18348c2ecf20Sopenharmony_ci	kfree(fw_status);
18358c2ecf20Sopenharmony_ciout_unlock:
18368c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
18378c2ecf20Sopenharmony_ci	return false;
18388c2ecf20Sopenharmony_ci}
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci#define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
18418c2ecf20Sopenharmony_ci			  IWL_SCAN_MAX_PROFILES)
18428c2ecf20Sopenharmony_ci
18438c2ecf20Sopenharmony_cistruct iwl_mvm_nd_query_results {
18448c2ecf20Sopenharmony_ci	u32 matched_profiles;
18458c2ecf20Sopenharmony_ci	u8 matches[ND_QUERY_BUF_LEN];
18468c2ecf20Sopenharmony_ci};
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_cistatic int
18498c2ecf20Sopenharmony_ciiwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
18508c2ecf20Sopenharmony_ci				struct iwl_mvm_nd_query_results *results)
18518c2ecf20Sopenharmony_ci{
18528c2ecf20Sopenharmony_ci	struct iwl_scan_offload_profiles_query *query;
18538c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
18548c2ecf20Sopenharmony_ci		.id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
18558c2ecf20Sopenharmony_ci		.flags = CMD_WANT_SKB,
18568c2ecf20Sopenharmony_ci	};
18578c2ecf20Sopenharmony_ci	int ret, len;
18588c2ecf20Sopenharmony_ci	size_t query_len, matches_len;
18598c2ecf20Sopenharmony_ci	int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
18608c2ecf20Sopenharmony_ci
18618c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd(mvm, &cmd);
18628c2ecf20Sopenharmony_ci	if (ret) {
18638c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
18648c2ecf20Sopenharmony_ci		return ret;
18658c2ecf20Sopenharmony_ci	}
18668c2ecf20Sopenharmony_ci
18678c2ecf20Sopenharmony_ci	if (fw_has_api(&mvm->fw->ucode_capa,
18688c2ecf20Sopenharmony_ci		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
18698c2ecf20Sopenharmony_ci		query_len = sizeof(struct iwl_scan_offload_profiles_query);
18708c2ecf20Sopenharmony_ci		matches_len = sizeof(struct iwl_scan_offload_profile_match) *
18718c2ecf20Sopenharmony_ci			max_profiles;
18728c2ecf20Sopenharmony_ci	} else {
18738c2ecf20Sopenharmony_ci		query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
18748c2ecf20Sopenharmony_ci		matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
18758c2ecf20Sopenharmony_ci			max_profiles;
18768c2ecf20Sopenharmony_ci	}
18778c2ecf20Sopenharmony_ci
18788c2ecf20Sopenharmony_ci	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
18798c2ecf20Sopenharmony_ci	if (len < query_len) {
18808c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
18818c2ecf20Sopenharmony_ci		ret = -EIO;
18828c2ecf20Sopenharmony_ci		goto out_free_resp;
18838c2ecf20Sopenharmony_ci	}
18848c2ecf20Sopenharmony_ci
18858c2ecf20Sopenharmony_ci	query = (void *)cmd.resp_pkt->data;
18868c2ecf20Sopenharmony_ci
18878c2ecf20Sopenharmony_ci	results->matched_profiles = le32_to_cpu(query->matched_profiles);
18888c2ecf20Sopenharmony_ci	memcpy(results->matches, query->matches, matches_len);
18898c2ecf20Sopenharmony_ci
18908c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLWIFI_DEBUGFS
18918c2ecf20Sopenharmony_ci	mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
18928c2ecf20Sopenharmony_ci#endif
18938c2ecf20Sopenharmony_ci
18948c2ecf20Sopenharmony_ciout_free_resp:
18958c2ecf20Sopenharmony_ci	iwl_free_resp(&cmd);
18968c2ecf20Sopenharmony_ci	return ret;
18978c2ecf20Sopenharmony_ci}
18988c2ecf20Sopenharmony_ci
18998c2ecf20Sopenharmony_cistatic int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
19008c2ecf20Sopenharmony_ci					 struct iwl_mvm_nd_query_results *query,
19018c2ecf20Sopenharmony_ci					 int idx)
19028c2ecf20Sopenharmony_ci{
19038c2ecf20Sopenharmony_ci	int n_chans = 0, i;
19048c2ecf20Sopenharmony_ci
19058c2ecf20Sopenharmony_ci	if (fw_has_api(&mvm->fw->ucode_capa,
19068c2ecf20Sopenharmony_ci		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
19078c2ecf20Sopenharmony_ci		struct iwl_scan_offload_profile_match *matches =
19088c2ecf20Sopenharmony_ci			(struct iwl_scan_offload_profile_match *)query->matches;
19098c2ecf20Sopenharmony_ci
19108c2ecf20Sopenharmony_ci		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
19118c2ecf20Sopenharmony_ci			n_chans += hweight8(matches[idx].matching_channels[i]);
19128c2ecf20Sopenharmony_ci	} else {
19138c2ecf20Sopenharmony_ci		struct iwl_scan_offload_profile_match_v1 *matches =
19148c2ecf20Sopenharmony_ci			(struct iwl_scan_offload_profile_match_v1 *)query->matches;
19158c2ecf20Sopenharmony_ci
19168c2ecf20Sopenharmony_ci		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
19178c2ecf20Sopenharmony_ci			n_chans += hweight8(matches[idx].matching_channels[i]);
19188c2ecf20Sopenharmony_ci	}
19198c2ecf20Sopenharmony_ci
19208c2ecf20Sopenharmony_ci	return n_chans;
19218c2ecf20Sopenharmony_ci}
19228c2ecf20Sopenharmony_ci
19238c2ecf20Sopenharmony_cistatic void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
19248c2ecf20Sopenharmony_ci				    struct iwl_mvm_nd_query_results *query,
19258c2ecf20Sopenharmony_ci				    struct cfg80211_wowlan_nd_match *match,
19268c2ecf20Sopenharmony_ci				    int idx)
19278c2ecf20Sopenharmony_ci{
19288c2ecf20Sopenharmony_ci	int i;
19298c2ecf20Sopenharmony_ci
19308c2ecf20Sopenharmony_ci	if (fw_has_api(&mvm->fw->ucode_capa,
19318c2ecf20Sopenharmony_ci		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
19328c2ecf20Sopenharmony_ci		struct iwl_scan_offload_profile_match *matches =
19338c2ecf20Sopenharmony_ci			(struct iwl_scan_offload_profile_match *)query->matches;
19348c2ecf20Sopenharmony_ci
19358c2ecf20Sopenharmony_ci		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
19368c2ecf20Sopenharmony_ci			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
19378c2ecf20Sopenharmony_ci				match->channels[match->n_channels++] =
19388c2ecf20Sopenharmony_ci					mvm->nd_channels[i]->center_freq;
19398c2ecf20Sopenharmony_ci	} else {
19408c2ecf20Sopenharmony_ci		struct iwl_scan_offload_profile_match_v1 *matches =
19418c2ecf20Sopenharmony_ci			(struct iwl_scan_offload_profile_match_v1 *)query->matches;
19428c2ecf20Sopenharmony_ci
19438c2ecf20Sopenharmony_ci		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
19448c2ecf20Sopenharmony_ci			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
19458c2ecf20Sopenharmony_ci				match->channels[match->n_channels++] =
19468c2ecf20Sopenharmony_ci					mvm->nd_channels[i]->center_freq;
19478c2ecf20Sopenharmony_ci	}
19488c2ecf20Sopenharmony_ci}
19498c2ecf20Sopenharmony_ci
19508c2ecf20Sopenharmony_cistatic void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
19518c2ecf20Sopenharmony_ci					    struct ieee80211_vif *vif)
19528c2ecf20Sopenharmony_ci{
19538c2ecf20Sopenharmony_ci	struct cfg80211_wowlan_nd_info *net_detect = NULL;
19548c2ecf20Sopenharmony_ci	struct cfg80211_wowlan_wakeup wakeup = {
19558c2ecf20Sopenharmony_ci		.pattern_idx = -1,
19568c2ecf20Sopenharmony_ci	};
19578c2ecf20Sopenharmony_ci	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
19588c2ecf20Sopenharmony_ci	struct iwl_mvm_nd_query_results query;
19598c2ecf20Sopenharmony_ci	struct iwl_wowlan_status *fw_status;
19608c2ecf20Sopenharmony_ci	unsigned long matched_profiles;
19618c2ecf20Sopenharmony_ci	u32 reasons = 0;
19628c2ecf20Sopenharmony_ci	int i, n_matches, ret;
19638c2ecf20Sopenharmony_ci
19648c2ecf20Sopenharmony_ci	fw_status = iwl_mvm_get_wakeup_status(mvm);
19658c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(fw_status)) {
19668c2ecf20Sopenharmony_ci		reasons = le32_to_cpu(fw_status->wakeup_reasons);
19678c2ecf20Sopenharmony_ci		kfree(fw_status);
19688c2ecf20Sopenharmony_ci	}
19698c2ecf20Sopenharmony_ci
19708c2ecf20Sopenharmony_ci	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
19718c2ecf20Sopenharmony_ci		wakeup.rfkill_release = true;
19728c2ecf20Sopenharmony_ci
19738c2ecf20Sopenharmony_ci	if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
19748c2ecf20Sopenharmony_ci		goto out;
19758c2ecf20Sopenharmony_ci
19768c2ecf20Sopenharmony_ci	ret = iwl_mvm_netdetect_query_results(mvm, &query);
19778c2ecf20Sopenharmony_ci	if (ret || !query.matched_profiles) {
19788c2ecf20Sopenharmony_ci		wakeup_report = NULL;
19798c2ecf20Sopenharmony_ci		goto out;
19808c2ecf20Sopenharmony_ci	}
19818c2ecf20Sopenharmony_ci
19828c2ecf20Sopenharmony_ci	matched_profiles = query.matched_profiles;
19838c2ecf20Sopenharmony_ci	if (mvm->n_nd_match_sets) {
19848c2ecf20Sopenharmony_ci		n_matches = hweight_long(matched_profiles);
19858c2ecf20Sopenharmony_ci	} else {
19868c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "no net detect match information available\n");
19878c2ecf20Sopenharmony_ci		n_matches = 0;
19888c2ecf20Sopenharmony_ci	}
19898c2ecf20Sopenharmony_ci
19908c2ecf20Sopenharmony_ci	net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
19918c2ecf20Sopenharmony_ci			     GFP_KERNEL);
19928c2ecf20Sopenharmony_ci	if (!net_detect || !n_matches)
19938c2ecf20Sopenharmony_ci		goto out_report_nd;
19948c2ecf20Sopenharmony_ci
19958c2ecf20Sopenharmony_ci	for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
19968c2ecf20Sopenharmony_ci		struct cfg80211_wowlan_nd_match *match;
19978c2ecf20Sopenharmony_ci		int idx, n_channels = 0;
19988c2ecf20Sopenharmony_ci
19998c2ecf20Sopenharmony_ci		n_channels = iwl_mvm_query_num_match_chans(mvm, &query, i);
20008c2ecf20Sopenharmony_ci
20018c2ecf20Sopenharmony_ci		match = kzalloc(struct_size(match, channels, n_channels),
20028c2ecf20Sopenharmony_ci				GFP_KERNEL);
20038c2ecf20Sopenharmony_ci		if (!match)
20048c2ecf20Sopenharmony_ci			goto out_report_nd;
20058c2ecf20Sopenharmony_ci
20068c2ecf20Sopenharmony_ci		net_detect->matches[net_detect->n_matches++] = match;
20078c2ecf20Sopenharmony_ci
20088c2ecf20Sopenharmony_ci		/* We inverted the order of the SSIDs in the scan
20098c2ecf20Sopenharmony_ci		 * request, so invert the index here.
20108c2ecf20Sopenharmony_ci		 */
20118c2ecf20Sopenharmony_ci		idx = mvm->n_nd_match_sets - i - 1;
20128c2ecf20Sopenharmony_ci		match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
20138c2ecf20Sopenharmony_ci		memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
20148c2ecf20Sopenharmony_ci		       match->ssid.ssid_len);
20158c2ecf20Sopenharmony_ci
20168c2ecf20Sopenharmony_ci		if (mvm->n_nd_channels < n_channels)
20178c2ecf20Sopenharmony_ci			continue;
20188c2ecf20Sopenharmony_ci
20198c2ecf20Sopenharmony_ci		iwl_mvm_query_set_freqs(mvm, &query, match, i);
20208c2ecf20Sopenharmony_ci	}
20218c2ecf20Sopenharmony_ci
20228c2ecf20Sopenharmony_ciout_report_nd:
20238c2ecf20Sopenharmony_ci	wakeup.net_detect = net_detect;
20248c2ecf20Sopenharmony_ciout:
20258c2ecf20Sopenharmony_ci	iwl_mvm_free_nd(mvm);
20268c2ecf20Sopenharmony_ci
20278c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
20288c2ecf20Sopenharmony_ci	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
20298c2ecf20Sopenharmony_ci
20308c2ecf20Sopenharmony_ci	if (net_detect) {
20318c2ecf20Sopenharmony_ci		for (i = 0; i < net_detect->n_matches; i++)
20328c2ecf20Sopenharmony_ci			kfree(net_detect->matches[i]);
20338c2ecf20Sopenharmony_ci		kfree(net_detect);
20348c2ecf20Sopenharmony_ci	}
20358c2ecf20Sopenharmony_ci}
20368c2ecf20Sopenharmony_ci
20378c2ecf20Sopenharmony_cistatic void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
20388c2ecf20Sopenharmony_ci				       struct ieee80211_vif *vif)
20398c2ecf20Sopenharmony_ci{
20408c2ecf20Sopenharmony_ci	/* skip the one we keep connection on */
20418c2ecf20Sopenharmony_ci	if (data == vif)
20428c2ecf20Sopenharmony_ci		return;
20438c2ecf20Sopenharmony_ci
20448c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_STATION)
20458c2ecf20Sopenharmony_ci		ieee80211_resume_disconnect(vif);
20468c2ecf20Sopenharmony_ci}
20478c2ecf20Sopenharmony_ci
20488c2ecf20Sopenharmony_cistatic bool iwl_mvm_rt_status(struct iwl_trans *trans, u32 base, u32 *err_id)
20498c2ecf20Sopenharmony_ci{
20508c2ecf20Sopenharmony_ci	struct error_table_start {
20518c2ecf20Sopenharmony_ci		/* cf. struct iwl_error_event_table */
20528c2ecf20Sopenharmony_ci		u32 valid;
20538c2ecf20Sopenharmony_ci		__le32 err_id;
20548c2ecf20Sopenharmony_ci	} err_info;
20558c2ecf20Sopenharmony_ci
20568c2ecf20Sopenharmony_ci	if (!base)
20578c2ecf20Sopenharmony_ci		return false;
20588c2ecf20Sopenharmony_ci
20598c2ecf20Sopenharmony_ci	iwl_trans_read_mem_bytes(trans, base,
20608c2ecf20Sopenharmony_ci				 &err_info, sizeof(err_info));
20618c2ecf20Sopenharmony_ci	if (err_info.valid && err_id)
20628c2ecf20Sopenharmony_ci		*err_id = le32_to_cpu(err_info.err_id);
20638c2ecf20Sopenharmony_ci
20648c2ecf20Sopenharmony_ci	return !!err_info.valid;
20658c2ecf20Sopenharmony_ci}
20668c2ecf20Sopenharmony_ci
20678c2ecf20Sopenharmony_cistatic bool iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
20688c2ecf20Sopenharmony_ci				   struct ieee80211_vif *vif)
20698c2ecf20Sopenharmony_ci{
20708c2ecf20Sopenharmony_ci	u32 err_id;
20718c2ecf20Sopenharmony_ci
20728c2ecf20Sopenharmony_ci	/* check for lmac1 error */
20738c2ecf20Sopenharmony_ci	if (iwl_mvm_rt_status(mvm->trans,
20748c2ecf20Sopenharmony_ci			      mvm->trans->dbg.lmac_error_event_table[0],
20758c2ecf20Sopenharmony_ci			      &err_id)) {
20768c2ecf20Sopenharmony_ci		if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
20778c2ecf20Sopenharmony_ci			struct cfg80211_wowlan_wakeup wakeup = {
20788c2ecf20Sopenharmony_ci				.rfkill_release = true,
20798c2ecf20Sopenharmony_ci			};
20808c2ecf20Sopenharmony_ci			ieee80211_report_wowlan_wakeup(vif, &wakeup,
20818c2ecf20Sopenharmony_ci						       GFP_KERNEL);
20828c2ecf20Sopenharmony_ci		}
20838c2ecf20Sopenharmony_ci		return true;
20848c2ecf20Sopenharmony_ci	}
20858c2ecf20Sopenharmony_ci
20868c2ecf20Sopenharmony_ci	/* check if we have lmac2 set and check for error */
20878c2ecf20Sopenharmony_ci	if (iwl_mvm_rt_status(mvm->trans,
20888c2ecf20Sopenharmony_ci			      mvm->trans->dbg.lmac_error_event_table[1], NULL))
20898c2ecf20Sopenharmony_ci		return true;
20908c2ecf20Sopenharmony_ci
20918c2ecf20Sopenharmony_ci	/* check for umac error */
20928c2ecf20Sopenharmony_ci	if (iwl_mvm_rt_status(mvm->trans,
20938c2ecf20Sopenharmony_ci			      mvm->trans->dbg.umac_error_event_table, NULL))
20948c2ecf20Sopenharmony_ci		return true;
20958c2ecf20Sopenharmony_ci
20968c2ecf20Sopenharmony_ci	return false;
20978c2ecf20Sopenharmony_ci}
20988c2ecf20Sopenharmony_ci
20998c2ecf20Sopenharmony_cistatic int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
21008c2ecf20Sopenharmony_ci{
21018c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif = NULL;
21028c2ecf20Sopenharmony_ci	int ret = 1;
21038c2ecf20Sopenharmony_ci	enum iwl_d3_status d3_status;
21048c2ecf20Sopenharmony_ci	bool keep = false;
21058c2ecf20Sopenharmony_ci	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
21068c2ecf20Sopenharmony_ci					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
21078c2ecf20Sopenharmony_ci	bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
21088c2ecf20Sopenharmony_ci				      IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
21098c2ecf20Sopenharmony_ci
21108c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
21118c2ecf20Sopenharmony_ci
21128c2ecf20Sopenharmony_ci	clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ci	/* get the BSS vif pointer again */
21158c2ecf20Sopenharmony_ci	vif = iwl_mvm_get_bss_vif(mvm);
21168c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(vif))
21178c2ecf20Sopenharmony_ci		goto err;
21188c2ecf20Sopenharmony_ci
21198c2ecf20Sopenharmony_ci	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
21208c2ecf20Sopenharmony_ci
21218c2ecf20Sopenharmony_ci	if (iwl_mvm_check_rt_status(mvm, vif)) {
21228c2ecf20Sopenharmony_ci		set_bit(STATUS_FW_ERROR, &mvm->trans->status);
21238c2ecf20Sopenharmony_ci		iwl_mvm_dump_nic_error_log(mvm);
21248c2ecf20Sopenharmony_ci		iwl_dbg_tlv_time_point(&mvm->fwrt,
21258c2ecf20Sopenharmony_ci				       IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL);
21268c2ecf20Sopenharmony_ci		iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
21278c2ecf20Sopenharmony_ci					false, 0);
21288c2ecf20Sopenharmony_ci		ret = 1;
21298c2ecf20Sopenharmony_ci		goto err;
21308c2ecf20Sopenharmony_ci	}
21318c2ecf20Sopenharmony_ci
21328c2ecf20Sopenharmony_ci	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_HOST_D3_END,
21338c2ecf20Sopenharmony_ci			       NULL);
21348c2ecf20Sopenharmony_ci
21358c2ecf20Sopenharmony_ci	ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
21368c2ecf20Sopenharmony_ci	if (ret)
21378c2ecf20Sopenharmony_ci		goto err;
21388c2ecf20Sopenharmony_ci
21398c2ecf20Sopenharmony_ci	if (d3_status != IWL_D3_STATUS_ALIVE) {
21408c2ecf20Sopenharmony_ci		IWL_INFO(mvm, "Device was reset during suspend\n");
21418c2ecf20Sopenharmony_ci		goto err;
21428c2ecf20Sopenharmony_ci	}
21438c2ecf20Sopenharmony_ci
21448c2ecf20Sopenharmony_ci	if (d0i3_first) {
21458c2ecf20Sopenharmony_ci		struct iwl_host_cmd cmd = {
21468c2ecf20Sopenharmony_ci			.id = D0I3_END_CMD,
21478c2ecf20Sopenharmony_ci			.flags = CMD_WANT_SKB,
21488c2ecf20Sopenharmony_ci		};
21498c2ecf20Sopenharmony_ci		int len;
21508c2ecf20Sopenharmony_ci
21518c2ecf20Sopenharmony_ci		ret = iwl_mvm_send_cmd(mvm, &cmd);
21528c2ecf20Sopenharmony_ci		if (ret < 0) {
21538c2ecf20Sopenharmony_ci			IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
21548c2ecf20Sopenharmony_ci				ret);
21558c2ecf20Sopenharmony_ci			goto err;
21568c2ecf20Sopenharmony_ci		}
21578c2ecf20Sopenharmony_ci		switch (mvm->cmd_ver.d0i3_resp) {
21588c2ecf20Sopenharmony_ci		case 0:
21598c2ecf20Sopenharmony_ci			break;
21608c2ecf20Sopenharmony_ci		case 1:
21618c2ecf20Sopenharmony_ci			len = iwl_rx_packet_payload_len(cmd.resp_pkt);
21628c2ecf20Sopenharmony_ci			if (len != sizeof(u32)) {
21638c2ecf20Sopenharmony_ci				IWL_ERR(mvm,
21648c2ecf20Sopenharmony_ci					"Error with D0I3_END_CMD response size (%d)\n",
21658c2ecf20Sopenharmony_ci					len);
21668c2ecf20Sopenharmony_ci				goto err;
21678c2ecf20Sopenharmony_ci			}
21688c2ecf20Sopenharmony_ci			if (IWL_D0I3_RESET_REQUIRE &
21698c2ecf20Sopenharmony_ci			    le32_to_cpu(*(__le32 *)cmd.resp_pkt->data)) {
21708c2ecf20Sopenharmony_ci				iwl_write32(mvm->trans, CSR_RESET,
21718c2ecf20Sopenharmony_ci					    CSR_RESET_REG_FLAG_FORCE_NMI);
21728c2ecf20Sopenharmony_ci				iwl_free_resp(&cmd);
21738c2ecf20Sopenharmony_ci			}
21748c2ecf20Sopenharmony_ci			break;
21758c2ecf20Sopenharmony_ci		default:
21768c2ecf20Sopenharmony_ci			WARN_ON(1);
21778c2ecf20Sopenharmony_ci		}
21788c2ecf20Sopenharmony_ci	}
21798c2ecf20Sopenharmony_ci
21808c2ecf20Sopenharmony_ci	/*
21818c2ecf20Sopenharmony_ci	 * Query the current location and source from the D3 firmware so we
21828c2ecf20Sopenharmony_ci	 * can play it back when we re-intiailize the D0 firmware
21838c2ecf20Sopenharmony_ci	 */
21848c2ecf20Sopenharmony_ci	iwl_mvm_update_changed_regdom(mvm);
21858c2ecf20Sopenharmony_ci
21868c2ecf20Sopenharmony_ci	/* Re-configure PPAG settings */
21878c2ecf20Sopenharmony_ci	iwl_mvm_ppag_send_cmd(mvm);
21888c2ecf20Sopenharmony_ci
21898c2ecf20Sopenharmony_ci	if (!unified_image)
21908c2ecf20Sopenharmony_ci		/*  Re-configure default SAR profile */
21918c2ecf20Sopenharmony_ci		iwl_mvm_sar_select_profile(mvm, 1, 1);
21928c2ecf20Sopenharmony_ci
21938c2ecf20Sopenharmony_ci	if (mvm->net_detect) {
21948c2ecf20Sopenharmony_ci		/* If this is a non-unified image, we restart the FW,
21958c2ecf20Sopenharmony_ci		 * so no need to stop the netdetect scan.  If that
21968c2ecf20Sopenharmony_ci		 * fails, continue and try to get the wake-up reasons,
21978c2ecf20Sopenharmony_ci		 * but trigger a HW restart by keeping a failure code
21988c2ecf20Sopenharmony_ci		 * in ret.
21998c2ecf20Sopenharmony_ci		 */
22008c2ecf20Sopenharmony_ci		if (unified_image)
22018c2ecf20Sopenharmony_ci			ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
22028c2ecf20Sopenharmony_ci						false);
22038c2ecf20Sopenharmony_ci
22048c2ecf20Sopenharmony_ci		iwl_mvm_query_netdetect_reasons(mvm, vif);
22058c2ecf20Sopenharmony_ci		/* has unlocked the mutex, so skip that */
22068c2ecf20Sopenharmony_ci		goto out;
22078c2ecf20Sopenharmony_ci	} else {
22088c2ecf20Sopenharmony_ci		keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
22098c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLWIFI_DEBUGFS
22108c2ecf20Sopenharmony_ci		if (keep)
22118c2ecf20Sopenharmony_ci			mvm->keep_vif = vif;
22128c2ecf20Sopenharmony_ci#endif
22138c2ecf20Sopenharmony_ci		/* has unlocked the mutex, so skip that */
22148c2ecf20Sopenharmony_ci		goto out_iterate;
22158c2ecf20Sopenharmony_ci	}
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_cierr:
22188c2ecf20Sopenharmony_ci	iwl_mvm_free_nd(mvm);
22198c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
22208c2ecf20Sopenharmony_ci
22218c2ecf20Sopenharmony_ciout_iterate:
22228c2ecf20Sopenharmony_ci	if (!test)
22238c2ecf20Sopenharmony_ci		ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
22248c2ecf20Sopenharmony_ci			IEEE80211_IFACE_ITER_NORMAL,
22258c2ecf20Sopenharmony_ci			iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
22268c2ecf20Sopenharmony_ci
22278c2ecf20Sopenharmony_ciout:
22288c2ecf20Sopenharmony_ci	/* no need to reset the device in unified images, if successful */
22298c2ecf20Sopenharmony_ci	if (unified_image && !ret) {
22308c2ecf20Sopenharmony_ci		/* nothing else to do if we already sent D0I3_END_CMD */
22318c2ecf20Sopenharmony_ci		if (d0i3_first)
22328c2ecf20Sopenharmony_ci			return 0;
22338c2ecf20Sopenharmony_ci
22348c2ecf20Sopenharmony_ci		ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
22358c2ecf20Sopenharmony_ci		if (!ret)
22368c2ecf20Sopenharmony_ci			return 0;
22378c2ecf20Sopenharmony_ci	}
22388c2ecf20Sopenharmony_ci
22398c2ecf20Sopenharmony_ci	/*
22408c2ecf20Sopenharmony_ci	 * Reconfigure the device in one of the following cases:
22418c2ecf20Sopenharmony_ci	 * 1. We are not using a unified image
22428c2ecf20Sopenharmony_ci	 * 2. We are using a unified image but had an error while exiting D3
22438c2ecf20Sopenharmony_ci	 */
22448c2ecf20Sopenharmony_ci	set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
22458c2ecf20Sopenharmony_ci
22468c2ecf20Sopenharmony_ci	return 1;
22478c2ecf20Sopenharmony_ci}
22488c2ecf20Sopenharmony_ci
22498c2ecf20Sopenharmony_cistatic int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
22508c2ecf20Sopenharmony_ci{
22518c2ecf20Sopenharmony_ci	iwl_trans_resume(mvm->trans);
22528c2ecf20Sopenharmony_ci
22538c2ecf20Sopenharmony_ci	return __iwl_mvm_resume(mvm, false);
22548c2ecf20Sopenharmony_ci}
22558c2ecf20Sopenharmony_ci
22568c2ecf20Sopenharmony_ciint iwl_mvm_resume(struct ieee80211_hw *hw)
22578c2ecf20Sopenharmony_ci{
22588c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
22598c2ecf20Sopenharmony_ci	int ret;
22608c2ecf20Sopenharmony_ci
22618c2ecf20Sopenharmony_ci	ret = iwl_mvm_resume_d3(mvm);
22628c2ecf20Sopenharmony_ci
22638c2ecf20Sopenharmony_ci	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
22648c2ecf20Sopenharmony_ci
22658c2ecf20Sopenharmony_ci	iwl_mvm_resume_tcm(mvm);
22668c2ecf20Sopenharmony_ci
22678c2ecf20Sopenharmony_ci	iwl_fw_runtime_resume(&mvm->fwrt);
22688c2ecf20Sopenharmony_ci
22698c2ecf20Sopenharmony_ci	return ret;
22708c2ecf20Sopenharmony_ci}
22718c2ecf20Sopenharmony_ci
22728c2ecf20Sopenharmony_civoid iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
22738c2ecf20Sopenharmony_ci{
22748c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
22758c2ecf20Sopenharmony_ci
22768c2ecf20Sopenharmony_ci	device_set_wakeup_enable(mvm->trans->dev, enabled);
22778c2ecf20Sopenharmony_ci}
22788c2ecf20Sopenharmony_ci
22798c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLWIFI_DEBUGFS
22808c2ecf20Sopenharmony_cistatic int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
22818c2ecf20Sopenharmony_ci{
22828c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = inode->i_private;
22838c2ecf20Sopenharmony_ci	int err;
22848c2ecf20Sopenharmony_ci
22858c2ecf20Sopenharmony_ci	if (mvm->d3_test_active)
22868c2ecf20Sopenharmony_ci		return -EBUSY;
22878c2ecf20Sopenharmony_ci
22888c2ecf20Sopenharmony_ci	file->private_data = inode->i_private;
22898c2ecf20Sopenharmony_ci
22908c2ecf20Sopenharmony_ci	synchronize_net();
22918c2ecf20Sopenharmony_ci
22928c2ecf20Sopenharmony_ci	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
22938c2ecf20Sopenharmony_ci
22948c2ecf20Sopenharmony_ci	iwl_mvm_pause_tcm(mvm, true);
22958c2ecf20Sopenharmony_ci
22968c2ecf20Sopenharmony_ci	iwl_fw_runtime_suspend(&mvm->fwrt);
22978c2ecf20Sopenharmony_ci
22988c2ecf20Sopenharmony_ci	/* start pseudo D3 */
22998c2ecf20Sopenharmony_ci	rtnl_lock();
23008c2ecf20Sopenharmony_ci	err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
23018c2ecf20Sopenharmony_ci	rtnl_unlock();
23028c2ecf20Sopenharmony_ci	if (err > 0)
23038c2ecf20Sopenharmony_ci		err = -EINVAL;
23048c2ecf20Sopenharmony_ci	if (err)
23058c2ecf20Sopenharmony_ci		return err;
23068c2ecf20Sopenharmony_ci
23078c2ecf20Sopenharmony_ci	mvm->d3_test_active = true;
23088c2ecf20Sopenharmony_ci	mvm->keep_vif = NULL;
23098c2ecf20Sopenharmony_ci	return 0;
23108c2ecf20Sopenharmony_ci}
23118c2ecf20Sopenharmony_ci
23128c2ecf20Sopenharmony_cistatic ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
23138c2ecf20Sopenharmony_ci				    size_t count, loff_t *ppos)
23148c2ecf20Sopenharmony_ci{
23158c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = file->private_data;
23168c2ecf20Sopenharmony_ci	u32 pme_asserted;
23178c2ecf20Sopenharmony_ci
23188c2ecf20Sopenharmony_ci	while (true) {
23198c2ecf20Sopenharmony_ci		/* read pme_ptr if available */
23208c2ecf20Sopenharmony_ci		if (mvm->d3_test_pme_ptr) {
23218c2ecf20Sopenharmony_ci			pme_asserted = iwl_trans_read_mem32(mvm->trans,
23228c2ecf20Sopenharmony_ci						mvm->d3_test_pme_ptr);
23238c2ecf20Sopenharmony_ci			if (pme_asserted)
23248c2ecf20Sopenharmony_ci				break;
23258c2ecf20Sopenharmony_ci		}
23268c2ecf20Sopenharmony_ci
23278c2ecf20Sopenharmony_ci		if (msleep_interruptible(100))
23288c2ecf20Sopenharmony_ci			break;
23298c2ecf20Sopenharmony_ci	}
23308c2ecf20Sopenharmony_ci
23318c2ecf20Sopenharmony_ci	return 0;
23328c2ecf20Sopenharmony_ci}
23338c2ecf20Sopenharmony_ci
23348c2ecf20Sopenharmony_cistatic void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
23358c2ecf20Sopenharmony_ci					      struct ieee80211_vif *vif)
23368c2ecf20Sopenharmony_ci{
23378c2ecf20Sopenharmony_ci	/* skip the one we keep connection on */
23388c2ecf20Sopenharmony_ci	if (_data == vif)
23398c2ecf20Sopenharmony_ci		return;
23408c2ecf20Sopenharmony_ci
23418c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_STATION)
23428c2ecf20Sopenharmony_ci		ieee80211_connection_loss(vif);
23438c2ecf20Sopenharmony_ci}
23448c2ecf20Sopenharmony_ci
23458c2ecf20Sopenharmony_cistatic int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
23468c2ecf20Sopenharmony_ci{
23478c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = inode->i_private;
23488c2ecf20Sopenharmony_ci	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
23498c2ecf20Sopenharmony_ci					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
23508c2ecf20Sopenharmony_ci
23518c2ecf20Sopenharmony_ci	mvm->d3_test_active = false;
23528c2ecf20Sopenharmony_ci
23538c2ecf20Sopenharmony_ci	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
23548c2ecf20Sopenharmony_ci
23558c2ecf20Sopenharmony_ci	rtnl_lock();
23568c2ecf20Sopenharmony_ci	__iwl_mvm_resume(mvm, true);
23578c2ecf20Sopenharmony_ci	rtnl_unlock();
23588c2ecf20Sopenharmony_ci
23598c2ecf20Sopenharmony_ci	iwl_mvm_resume_tcm(mvm);
23608c2ecf20Sopenharmony_ci
23618c2ecf20Sopenharmony_ci	iwl_fw_runtime_resume(&mvm->fwrt);
23628c2ecf20Sopenharmony_ci
23638c2ecf20Sopenharmony_ci	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
23648c2ecf20Sopenharmony_ci
23658c2ecf20Sopenharmony_ci	iwl_abort_notification_waits(&mvm->notif_wait);
23668c2ecf20Sopenharmony_ci	if (!unified_image) {
23678c2ecf20Sopenharmony_ci		int remaining_time = 10;
23688c2ecf20Sopenharmony_ci
23698c2ecf20Sopenharmony_ci		ieee80211_restart_hw(mvm->hw);
23708c2ecf20Sopenharmony_ci
23718c2ecf20Sopenharmony_ci		/* wait for restart and disconnect all interfaces */
23728c2ecf20Sopenharmony_ci		while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
23738c2ecf20Sopenharmony_ci		       remaining_time > 0) {
23748c2ecf20Sopenharmony_ci			remaining_time--;
23758c2ecf20Sopenharmony_ci			msleep(1000);
23768c2ecf20Sopenharmony_ci		}
23778c2ecf20Sopenharmony_ci
23788c2ecf20Sopenharmony_ci		if (remaining_time == 0)
23798c2ecf20Sopenharmony_ci			IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
23808c2ecf20Sopenharmony_ci	}
23818c2ecf20Sopenharmony_ci
23828c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(
23838c2ecf20Sopenharmony_ci		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
23848c2ecf20Sopenharmony_ci		iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
23858c2ecf20Sopenharmony_ci
23868c2ecf20Sopenharmony_ci	return 0;
23878c2ecf20Sopenharmony_ci}
23888c2ecf20Sopenharmony_ci
23898c2ecf20Sopenharmony_ciconst struct file_operations iwl_dbgfs_d3_test_ops = {
23908c2ecf20Sopenharmony_ci	.llseek = no_llseek,
23918c2ecf20Sopenharmony_ci	.open = iwl_mvm_d3_test_open,
23928c2ecf20Sopenharmony_ci	.read = iwl_mvm_d3_test_read,
23938c2ecf20Sopenharmony_ci	.release = iwl_mvm_d3_test_release,
23948c2ecf20Sopenharmony_ci};
23958c2ecf20Sopenharmony_ci#endif
2396