Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
   2/*
   3 * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
   4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
   5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
   6 */
   7#include <linux/etherdevice.h>
   8#include <linux/ip.h>
   9#include <linux/fs.h>
  10#include <net/cfg80211.h>
  11#include <net/ipv6.h>
  12#include <net/tcp.h>
  13#include <net/addrconf.h>
  14#include "iwl-modparams.h"
  15#include "fw-api.h"
  16#include "mvm.h"
  17#include "fw/img.h"
  18
  19void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
  20			    struct ieee80211_vif *vif,
  21			    struct cfg80211_gtk_rekey_data *data)
  22{
  23	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  24	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  25
  26	mutex_lock(&mvm->mutex);
  27
  28	mvmvif->rekey_data.kek_len = data->kek_len;
  29	mvmvif->rekey_data.kck_len = data->kck_len;
  30	memcpy(mvmvif->rekey_data.kek, data->kek, data->kek_len);
  31	memcpy(mvmvif->rekey_data.kck, data->kck, data->kck_len);
  32	mvmvif->rekey_data.akm = data->akm & 0xFF;
  33	mvmvif->rekey_data.replay_ctr =
  34		cpu_to_le64(be64_to_cpup((const __be64 *)data->replay_ctr));
  35	mvmvif->rekey_data.valid = true;
  36
  37	mutex_unlock(&mvm->mutex);
  38}
  39
  40#if IS_ENABLED(CONFIG_IPV6)
  41void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
  42			      struct ieee80211_vif *vif,
  43			      struct inet6_dev *idev)
  44{
  45	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  46	struct inet6_ifaddr *ifa;
  47	int idx = 0;
  48
  49	memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
  50
  51	read_lock_bh(&idev->lock);
  52	list_for_each_entry(ifa, &idev->addr_list, if_list) {
  53		mvmvif->target_ipv6_addrs[idx] = ifa->addr;
  54		if (ifa->flags & IFA_F_TENTATIVE)
  55			__set_bit(idx, mvmvif->tentative_addrs);
  56		idx++;
  57		if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
  58			break;
  59	}
  60	read_unlock_bh(&idev->lock);
  61
  62	mvmvif->num_target_ipv6_addrs = idx;
  63}
  64#endif
  65
  66void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
  67				     struct ieee80211_vif *vif, int idx)
  68{
  69	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  70
  71	mvmvif->tx_key_idx = idx;
  72}
  73
  74static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
  75{
  76	int i;
  77
  78	for (i = 0; i < IWL_P1K_SIZE; i++)
  79		out[i] = cpu_to_le16(p1k[i]);
  80}
  81
  82static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
  83				     struct iwl_mvm_key_pn *ptk_pn,
  84				     struct ieee80211_key_seq *seq,
  85				     int tid, int queues)
  86{
  87	const u8 *ret = seq->ccmp.pn;
  88	int i;
  89
  90	/* get the PN from mac80211, used on the default queue */
  91	ieee80211_get_key_rx_seq(key, tid, seq);
  92
  93	/* and use the internal data for the other queues */
  94	for (i = 1; i < queues; i++) {
  95		const u8 *tmp = ptk_pn->q[i].pn[tid];
  96
  97		if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
  98			ret = tmp;
  99	}
 100
 101	return ret;
 102}
 103
 104struct wowlan_key_reprogram_data {
 105	bool error;
 106	int wep_key_idx;
 107};
 108
 109static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
 110					struct ieee80211_vif *vif,
 111					struct ieee80211_sta *sta,
 112					struct ieee80211_key_conf *key,
 113					void *_data)
 114{
 115	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 116	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 117	struct wowlan_key_reprogram_data *data = _data;
 118	int ret;
 119
 120	switch (key->cipher) {
 121	case WLAN_CIPHER_SUITE_WEP40:
 122	case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
 123		struct {
 124			struct iwl_mvm_wep_key_cmd wep_key_cmd;
 125			struct iwl_mvm_wep_key wep_key;
 126		} __packed wkc = {
 127			.wep_key_cmd.mac_id_n_color =
 128				cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 129								mvmvif->color)),
 130			.wep_key_cmd.num_keys = 1,
 131			/* firmware sets STA_KEY_FLG_WEP_13BYTES */
 132			.wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
 133			.wep_key.key_index = key->keyidx,
 134			.wep_key.key_size = key->keylen,
 135		};
 136
 137		/*
 138		 * This will fail -- the key functions don't set support
 139		 * pairwise WEP keys. However, that's better than silently
 140		 * failing WoWLAN. Or maybe not?
 141		 */
 142		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
 143			break;
 144
 145		memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
 146		if (key->keyidx == mvmvif->tx_key_idx) {
 147			/* TX key must be at offset 0 */
 148			wkc.wep_key.key_offset = 0;
 149		} else {
 150			/* others start at 1 */
 151			data->wep_key_idx++;
 152			wkc.wep_key.key_offset = data->wep_key_idx;
 153		}
 154
 155		mutex_lock(&mvm->mutex);
 156		ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
 157		data->error = ret != 0;
 158
 159		mvm->ptk_ivlen = key->iv_len;
 160		mvm->ptk_icvlen = key->icv_len;
 161		mvm->gtk_ivlen = key->iv_len;
 162		mvm->gtk_icvlen = key->icv_len;
 163		mutex_unlock(&mvm->mutex);
 164
 165		/* don't upload key again */
 166		return;
 167	}
 168	default:
 169		data->error = true;
 170		return;
 171	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
 172	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 173		return;
 174	case WLAN_CIPHER_SUITE_AES_CMAC:
 175		/*
 176		 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
 177		 * but we also shouldn't abort suspend due to that. It does have
 178		 * support for the IGTK key renewal, but doesn't really use the
 179		 * IGTK for anything. This means we could spuriously wake up or
 180		 * be deauthenticated, but that was considered acceptable.
 181		 */
 182		return;
 183	case WLAN_CIPHER_SUITE_TKIP:
 184	case WLAN_CIPHER_SUITE_CCMP:
 185	case WLAN_CIPHER_SUITE_GCMP:
 186	case WLAN_CIPHER_SUITE_GCMP_256:
 187		break;
 188	}
 189
 190	mutex_lock(&mvm->mutex);
 191	/*
 192	 * The D3 firmware hardcodes the key offset 0 as the key it
 193	 * uses to transmit packets to the AP, i.e. the PTK.
 194	 */
 195	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
 196		mvm->ptk_ivlen = key->iv_len;
 197		mvm->ptk_icvlen = key->icv_len;
 198		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
 199	} else {
 200		/*
 201		 * firmware only supports TSC/RSC for a single key,
 202		 * so if there are multiple keep overwriting them
 203		 * with new ones -- this relies on mac80211 doing
 204		 * list_add_tail().
 205		 */
 206		mvm->gtk_ivlen = key->iv_len;
 207		mvm->gtk_icvlen = key->icv_len;
 208		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
 209	}
 210	mutex_unlock(&mvm->mutex);
 211	data->error = ret != 0;
 212}
 213
 214struct wowlan_key_rsc_tsc_data {
 215	struct iwl_wowlan_rsc_tsc_params_cmd_v4 *rsc_tsc;
 216	bool have_rsc_tsc;
 217};
 218
 219static void iwl_mvm_wowlan_get_rsc_tsc_data(struct ieee80211_hw *hw,
 220					    struct ieee80211_vif *vif,
 221					    struct ieee80211_sta *sta,
 222					    struct ieee80211_key_conf *key,
 223					    void *_data)
 224{
 225	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 226	struct wowlan_key_rsc_tsc_data *data = _data;
 227	struct aes_sc *aes_sc;
 228	struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
 229	struct ieee80211_key_seq seq;
 230	int i;
 231
 232	switch (key->cipher) {
 233	default:
 234		break;
 235	case WLAN_CIPHER_SUITE_TKIP:
 236		if (sta) {
 237			u64 pn64;
 238
 239			tkip_sc =
 240			   data->rsc_tsc->params.all_tsc_rsc.tkip.unicast_rsc;
 241			tkip_tx_sc =
 242				&data->rsc_tsc->params.all_tsc_rsc.tkip.tsc;
 243
 244			pn64 = atomic64_read(&key->tx_pn);
 245			tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
 246			tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
 247		} else {
 248			tkip_sc =
 249			  data->rsc_tsc->params.all_tsc_rsc.tkip.multicast_rsc;
 250		}
 251
 252		/*
 253		 * For non-QoS this relies on the fact that both the uCode and
 254		 * mac80211 use TID 0 (as they need to to avoid replay attacks)
 255		 * for checking the IV in the frames.
 256		 */
 257		for (i = 0; i < IWL_NUM_RSC; i++) {
 258			ieee80211_get_key_rx_seq(key, i, &seq);
 259			tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
 260			tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
 261		}
 262
 263		data->have_rsc_tsc = true;
 264		break;
 265	case WLAN_CIPHER_SUITE_CCMP:
 266	case WLAN_CIPHER_SUITE_GCMP:
 267	case WLAN_CIPHER_SUITE_GCMP_256:
 268		if (sta) {
 269			struct aes_sc *aes_tx_sc;
 270			u64 pn64;
 271
 272			aes_sc =
 273			   data->rsc_tsc->params.all_tsc_rsc.aes.unicast_rsc;
 274			aes_tx_sc =
 275				&data->rsc_tsc->params.all_tsc_rsc.aes.tsc;
 276
 277			pn64 = atomic64_read(&key->tx_pn);
 278			aes_tx_sc->pn = cpu_to_le64(pn64);
 279		} else {
 280			aes_sc =
 281			   data->rsc_tsc->params.all_tsc_rsc.aes.multicast_rsc;
 282		}
 283
 284		/*
 285		 * For non-QoS this relies on the fact that both the uCode and
 286		 * mac80211/our RX code use TID 0 for checking the PN.
 287		 */
 288		if (sta && iwl_mvm_has_new_rx_api(mvm)) {
 289			struct iwl_mvm_sta *mvmsta;
 290			struct iwl_mvm_key_pn *ptk_pn;
 291			const u8 *pn;
 292
 293			mvmsta = iwl_mvm_sta_from_mac80211(sta);
 294			rcu_read_lock();
 295			ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
 296			if (WARN_ON(!ptk_pn)) {
 297				rcu_read_unlock();
 298				break;
 299			}
 300
 301			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
 302				pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
 303						mvm->trans->num_rx_queues);
 304				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
 305							   ((u64)pn[4] << 8) |
 306							   ((u64)pn[3] << 16) |
 307							   ((u64)pn[2] << 24) |
 308							   ((u64)pn[1] << 32) |
 309							   ((u64)pn[0] << 40));
 310			}
 311
 312			rcu_read_unlock();
 313		} else {
 314			for (i = 0; i < IWL_NUM_RSC; i++) {
 315				u8 *pn = seq.ccmp.pn;
 316
 317				ieee80211_get_key_rx_seq(key, i, &seq);
 318				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
 319							   ((u64)pn[4] << 8) |
 320							   ((u64)pn[3] << 16) |
 321							   ((u64)pn[2] << 24) |
 322							   ((u64)pn[1] << 32) |
 323							   ((u64)pn[0] << 40));
 324			}
 325		}
 326		data->have_rsc_tsc = true;
 327		break;
 328	}
 329}
 330
 331struct wowlan_key_rsc_v5_data {
 332	struct iwl_wowlan_rsc_tsc_params_cmd *rsc;
 333	bool have_rsc;
 334	int gtks;
 335	int gtk_ids[4];
 336};
 337
 338static void iwl_mvm_wowlan_get_rsc_v5_data(struct ieee80211_hw *hw,
 339					   struct ieee80211_vif *vif,
 340					   struct ieee80211_sta *sta,
 341					   struct ieee80211_key_conf *key,
 342					   void *_data)
 343{
 344	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 345	struct wowlan_key_rsc_v5_data *data = _data;
 346	struct ieee80211_key_seq seq;
 347	__le64 *rsc;
 348	int i;
 349
 350	/* only for ciphers that can be PTK/GTK */
 351	switch (key->cipher) {
 352	default:
 353		return;
 354	case WLAN_CIPHER_SUITE_TKIP:
 355	case WLAN_CIPHER_SUITE_CCMP:
 356	case WLAN_CIPHER_SUITE_GCMP:
 357	case WLAN_CIPHER_SUITE_GCMP_256:
 358		break;
 359	}
 360
 361	if (sta) {
 362		rsc = data->rsc->ucast_rsc;
 363	} else {
 364		if (WARN_ON(data->gtks >= ARRAY_SIZE(data->gtk_ids)))
 365			return;
 366		data->gtk_ids[data->gtks] = key->keyidx;
 367		rsc = data->rsc->mcast_rsc[data->gtks % 2];
 368		if (WARN_ON(key->keyidx >=
 369				ARRAY_SIZE(data->rsc->mcast_key_id_map)))
 370			return;
 371		data->rsc->mcast_key_id_map[key->keyidx] = data->gtks % 2;
 372		if (data->gtks >= 2) {
 373			int prev = data->gtks - 2;
 374			int prev_idx = data->gtk_ids[prev];
 375
 376			data->rsc->mcast_key_id_map[prev_idx] =
 377				IWL_MCAST_KEY_MAP_INVALID;
 378		}
 379		data->gtks++;
 380	}
 381
 382	switch (key->cipher) {
 383	default:
 384		WARN_ON(1);
 385		break;
 386	case WLAN_CIPHER_SUITE_TKIP:
 387
 388		/*
 389		 * For non-QoS this relies on the fact that both the uCode and
 390		 * mac80211 use TID 0 (as they need to to avoid replay attacks)
 391		 * for checking the IV in the frames.
 392		 */
 393		for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
 394			ieee80211_get_key_rx_seq(key, i, &seq);
 395
 396			rsc[i] = cpu_to_le64(((u64)seq.tkip.iv32 << 16) |
 397					     seq.tkip.iv16);
 398		}
 399
 400		data->have_rsc = true;
 401		break;
 402	case WLAN_CIPHER_SUITE_CCMP:
 403	case WLAN_CIPHER_SUITE_GCMP:
 404	case WLAN_CIPHER_SUITE_GCMP_256:
 405		/*
 406		 * For non-QoS this relies on the fact that both the uCode and
 407		 * mac80211/our RX code use TID 0 for checking the PN.
 408		 */
 409		if (sta) {
 410			struct iwl_mvm_sta *mvmsta;
 411			struct iwl_mvm_key_pn *ptk_pn;
 412			const u8 *pn;
 413
 414			mvmsta = iwl_mvm_sta_from_mac80211(sta);
 415			rcu_read_lock();
 416			ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
 417			if (WARN_ON(!ptk_pn)) {
 418				rcu_read_unlock();
 419				break;
 420			}
 421
 422			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
 423				pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
 424						mvm->trans->num_rx_queues);
 425				rsc[i] = cpu_to_le64((u64)pn[5] |
 426						     ((u64)pn[4] << 8) |
 427						     ((u64)pn[3] << 16) |
 428						     ((u64)pn[2] << 24) |
 429						     ((u64)pn[1] << 32) |
 430						     ((u64)pn[0] << 40));
 431			}
 432
 433			rcu_read_unlock();
 434		} else {
 435			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
 436				u8 *pn = seq.ccmp.pn;
 437
 438				ieee80211_get_key_rx_seq(key, i, &seq);
 439				rsc[i] = cpu_to_le64((u64)pn[5] |
 440						     ((u64)pn[4] << 8) |
 441						     ((u64)pn[3] << 16) |
 442						     ((u64)pn[2] << 24) |
 443						     ((u64)pn[1] << 32) |
 444						     ((u64)pn[0] << 40));
 445			}
 446		}
 447		data->have_rsc = true;
 448		break;
 449	}
 450}
 451
 452static int iwl_mvm_wowlan_config_rsc_tsc(struct iwl_mvm *mvm,
 453					 struct ieee80211_vif *vif)
 454{
 455	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 456	int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TSC_RSC_PARAM,
 457					IWL_FW_CMD_VER_UNKNOWN);
 458	int ret;
 459
 460	if (ver == 5) {
 461		struct wowlan_key_rsc_v5_data data = {};
 462		int i;
 463
 464		data.rsc = kmalloc(sizeof(*data.rsc), GFP_KERNEL);
 465		if (!data.rsc)
 466			return -ENOMEM;
 467
 468		memset(data.rsc, 0xff, sizeof(*data.rsc));
 469
 470		for (i = 0; i < ARRAY_SIZE(data.rsc->mcast_key_id_map); i++)
 471			data.rsc->mcast_key_id_map[i] =
 472				IWL_MCAST_KEY_MAP_INVALID;
 473		data.rsc->sta_id = cpu_to_le32(mvmvif->deflink.ap_sta_id);
 474
 475		ieee80211_iter_keys(mvm->hw, vif,
 476				    iwl_mvm_wowlan_get_rsc_v5_data,
 477				    &data);
 478
 479		if (data.have_rsc)
 480			ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
 481						   CMD_ASYNC, sizeof(*data.rsc),
 482						   data.rsc);
 483		else
 484			ret = 0;
 485		kfree(data.rsc);
 486	} else if (ver == 4 || ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN) {
 487		struct wowlan_key_rsc_tsc_data data = {};
 488		int size;
 489
 490		data.rsc_tsc = kzalloc(sizeof(*data.rsc_tsc), GFP_KERNEL);
 491		if (!data.rsc_tsc)
 492			return -ENOMEM;
 493
 494		if (ver == 4) {
 495			size = sizeof(*data.rsc_tsc);
 496			data.rsc_tsc->sta_id =
 497				cpu_to_le32(mvmvif->deflink.ap_sta_id);
 498		} else {
 499			/* ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN */
 500			size = sizeof(data.rsc_tsc->params);
 501		}
 502
 503		ieee80211_iter_keys(mvm->hw, vif,
 504				    iwl_mvm_wowlan_get_rsc_tsc_data,
 505				    &data);
 506
 507		if (data.have_rsc_tsc)
 508			ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
 509						   CMD_ASYNC, size,
 510						   data.rsc_tsc);
 511		else
 512			ret = 0;
 513		kfree(data.rsc_tsc);
 514	} else {
 515		ret = 0;
 516		WARN_ON_ONCE(1);
 517	}
 518
 519	return ret;
 520}
 521
 522struct wowlan_key_tkip_data {
 523	struct iwl_wowlan_tkip_params_cmd tkip;
 524	bool have_tkip_keys;
 525};
 526
 527static void iwl_mvm_wowlan_get_tkip_data(struct ieee80211_hw *hw,
 528					 struct ieee80211_vif *vif,
 529					 struct ieee80211_sta *sta,
 530					 struct ieee80211_key_conf *key,
 531					 void *_data)
 532{
 533	struct wowlan_key_tkip_data *data = _data;
 534	struct iwl_p1k_cache *rx_p1ks;
 535	u8 *rx_mic_key;
 536	struct ieee80211_key_seq seq;
 537	u32 cur_rx_iv32 = 0;
 538	u16 p1k[IWL_P1K_SIZE];
 539	int i;
 540
 541	switch (key->cipher) {
 542	default:
 543		break;
 544	case WLAN_CIPHER_SUITE_TKIP:
 545		if (sta) {
 546			u64 pn64;
 547
 548			rx_p1ks = data->tkip.rx_uni;
 549
 550			pn64 = atomic64_read(&key->tx_pn);
 551
 552			ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
 553						  p1k);
 554			iwl_mvm_convert_p1k(p1k, data->tkip.tx.p1k);
 555
 556			memcpy(data->tkip.mic_keys.tx,
 557			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
 558			       IWL_MIC_KEY_SIZE);
 559
 560			rx_mic_key = data->tkip.mic_keys.rx_unicast;
 561		} else {
 562			rx_p1ks = data->tkip.rx_multi;
 563			rx_mic_key = data->tkip.mic_keys.rx_mcast;
 564		}
 565
 566		for (i = 0; i < IWL_NUM_RSC; i++) {
 567			ieee80211_get_key_rx_seq(key, i, &seq);
 568			/* wrapping isn't allowed, AP must rekey */
 569			if (seq.tkip.iv32 > cur_rx_iv32)
 570				cur_rx_iv32 = seq.tkip.iv32;
 571		}
 572
 573		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
 574					  cur_rx_iv32, p1k);
 575		iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
 576		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
 577					  cur_rx_iv32 + 1, p1k);
 578		iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
 579
 580		memcpy(rx_mic_key,
 581		       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
 582		       IWL_MIC_KEY_SIZE);
 583
 584		data->have_tkip_keys = true;
 585		break;
 586	}
 587}
 588
 589struct wowlan_key_gtk_type_iter {
 590	struct iwl_wowlan_kek_kck_material_cmd_v4 *kek_kck_cmd;
 591};
 592
 593static void iwl_mvm_wowlan_gtk_type_iter(struct ieee80211_hw *hw,
 594					 struct ieee80211_vif *vif,
 595					 struct ieee80211_sta *sta,
 596					 struct ieee80211_key_conf *key,
 597					 void *_data)
 598{
 599	struct wowlan_key_gtk_type_iter *data = _data;
 600
 601	switch (key->cipher) {
 602	default:
 603		return;
 604	case WLAN_CIPHER_SUITE_TKIP:
 605		if (!sta)
 606			data->kek_kck_cmd->gtk_cipher =
 607				cpu_to_le32(STA_KEY_FLG_TKIP);
 608		return;
 609	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
 610	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 611		data->kek_kck_cmd->igtk_cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
 612		return;
 613	case WLAN_CIPHER_SUITE_AES_CMAC:
 614		data->kek_kck_cmd->igtk_cipher = cpu_to_le32(STA_KEY_FLG_CCM);
 615		return;
 616	case WLAN_CIPHER_SUITE_CCMP:
 617		if (!sta)
 618			data->kek_kck_cmd->gtk_cipher =
 619				cpu_to_le32(STA_KEY_FLG_CCM);
 620		return;
 621	case WLAN_CIPHER_SUITE_GCMP:
 622	case WLAN_CIPHER_SUITE_GCMP_256:
 623		if (!sta)
 624			data->kek_kck_cmd->gtk_cipher =
 625				cpu_to_le32(STA_KEY_FLG_GCMP);
 626		return;
 627	}
 628}
 629
 630static int iwl_mvm_send_patterns_v1(struct iwl_mvm *mvm,
 631				    struct cfg80211_wowlan *wowlan)
 632{
 633	struct iwl_wowlan_patterns_cmd_v1 *pattern_cmd;
 634	struct iwl_host_cmd cmd = {
 635		.id = WOWLAN_PATTERNS,
 636		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
 637	};
 638	int i, err;
 639
 640	if (!wowlan->n_patterns)
 641		return 0;
 642
 643	cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns);
 644
 645	pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
 646	if (!pattern_cmd)
 647		return -ENOMEM;
 648
 649	pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
 650
 651	for (i = 0; i < wowlan->n_patterns; i++) {
 652		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
 653
 654		memcpy(&pattern_cmd->patterns[i].mask,
 655		       wowlan->patterns[i].mask, mask_len);
 656		memcpy(&pattern_cmd->patterns[i].pattern,
 657		       wowlan->patterns[i].pattern,
 658		       wowlan->patterns[i].pattern_len);
 659		pattern_cmd->patterns[i].mask_size = mask_len;
 660		pattern_cmd->patterns[i].pattern_size =
 661			wowlan->patterns[i].pattern_len;
 662	}
 663
 664	cmd.data[0] = pattern_cmd;
 665	err = iwl_mvm_send_cmd(mvm, &cmd);
 666	kfree(pattern_cmd);
 667	return err;
 668}
 669
 670static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
 671				 struct ieee80211_vif *vif,
 672				 struct cfg80211_wowlan *wowlan)
 673{
 674	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 675	struct iwl_wowlan_patterns_cmd *pattern_cmd;
 676	struct iwl_host_cmd cmd = {
 677		.id = WOWLAN_PATTERNS,
 678		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
 679	};
 680	int i, err;
 681	int ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
 682					IWL_FW_CMD_VER_UNKNOWN);
 683
 684	if (!wowlan->n_patterns)
 685		return 0;
 686
 687	cmd.len[0] = sizeof(*pattern_cmd) +
 688		wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v2);
 689
 690	pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL);
 691	if (!pattern_cmd)
 692		return -ENOMEM;
 693
 694	pattern_cmd->n_patterns = wowlan->n_patterns;
 695	if (ver >= 3)
 696		pattern_cmd->sta_id = mvmvif->deflink.ap_sta_id;
 697
 698	for (i = 0; i < wowlan->n_patterns; i++) {
 699		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
 700
 701		pattern_cmd->patterns[i].pattern_type =
 702			WOWLAN_PATTERN_TYPE_BITMASK;
 703
 704		memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
 705		       wowlan->patterns[i].mask, mask_len);
 706		memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
 707		       wowlan->patterns[i].pattern,
 708		       wowlan->patterns[i].pattern_len);
 709		pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
 710		pattern_cmd->patterns[i].u.bitmask.pattern_size =
 711			wowlan->patterns[i].pattern_len;
 712	}
 713
 714	cmd.data[0] = pattern_cmd;
 715	err = iwl_mvm_send_cmd(mvm, &cmd);
 716	kfree(pattern_cmd);
 717	return err;
 718}
 719
 720static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 721				struct ieee80211_sta *ap_sta)
 722{
 723	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 724	struct ieee80211_chanctx_conf *ctx;
 725	u8 chains_static, chains_dynamic;
 726	struct cfg80211_chan_def chandef;
 727	int ret, i;
 728	struct iwl_binding_cmd_v1 binding_cmd = {};
 729	struct iwl_time_quota_cmd quota_cmd = {};
 730	struct iwl_time_quota_data *quota;
 731	u32 status;
 732
 733	if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm)))
 734		return -EINVAL;
 735
 736	/* add back the PHY */
 737	if (WARN_ON(!mvmvif->deflink.phy_ctxt))
 738		return -EINVAL;
 739
 740	rcu_read_lock();
 741	ctx = rcu_dereference(vif->bss_conf.chanctx_conf);
 742	if (WARN_ON(!ctx)) {
 743		rcu_read_unlock();
 744		return -EINVAL;
 745	}
 746	chandef = ctx->def;
 747	chains_static = ctx->rx_chains_static;
 748	chains_dynamic = ctx->rx_chains_dynamic;
 749	rcu_read_unlock();
 750
 751	ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt, &chandef,
 752				   chains_static, chains_dynamic);
 753	if (ret)
 754		return ret;
 755
 756	/* add back the MAC */
 757	mvmvif->uploaded = false;
 758
 759	if (WARN_ON(!vif->cfg.assoc))
 760		return -EINVAL;
 761
 762	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
 763	if (ret)
 764		return ret;
 765
 766	/* add back binding - XXX refactor? */
 767	binding_cmd.id_and_color =
 768		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
 769						mvmvif->deflink.phy_ctxt->color));
 770	binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
 771	binding_cmd.phy =
 772		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
 773						mvmvif->deflink.phy_ctxt->color));
 774	binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 775							      mvmvif->color));
 776	for (i = 1; i < MAX_MACS_IN_BINDING; i++)
 777		binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
 778
 779	status = 0;
 780	ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
 781					  IWL_BINDING_CMD_SIZE_V1, &binding_cmd,
 782					  &status);
 783	if (ret) {
 784		IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
 785		return ret;
 786	}
 787
 788	if (status) {
 789		IWL_ERR(mvm, "Binding command failed: %u\n", status);
 790		return -EIO;
 791	}
 792
 793	ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
 794	if (ret)
 795		return ret;
 796	rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->deflink.ap_sta_id],
 797			   ap_sta);
 798
 799	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
 800	if (ret)
 801		return ret;
 802
 803	/* and some quota */
 804	quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, 0);
 805	quota->id_and_color =
 806		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
 807						mvmvif->deflink.phy_ctxt->color));
 808	quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
 809	quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
 810
 811	for (i = 1; i < MAX_BINDINGS; i++) {
 812		quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, i);
 813		quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
 814	}
 815
 816	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
 817				   iwl_mvm_quota_cmd_size(mvm), &quota_cmd);
 818	if (ret)
 819		IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
 820
 821	if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm, false))
 822		IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
 823
 824	return 0;
 825}
 826
 827static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
 828				       struct ieee80211_vif *vif)
 829{
 830	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 831	struct iwl_nonqos_seq_query_cmd query_cmd = {
 832		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
 833		.mac_id_n_color =
 834			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 835							mvmvif->color)),
 836	};
 837	struct iwl_host_cmd cmd = {
 838		.id = NON_QOS_TX_COUNTER_CMD,
 839		.flags = CMD_WANT_SKB,
 840	};
 841	int err;
 842	u32 size;
 843
 844	cmd.data[0] = &query_cmd;
 845	cmd.len[0] = sizeof(query_cmd);
 846
 847	err = iwl_mvm_send_cmd(mvm, &cmd);
 848	if (err)
 849		return err;
 850
 851	size = iwl_rx_packet_payload_len(cmd.resp_pkt);
 852	if (size < sizeof(__le16)) {
 853		err = -EINVAL;
 854	} else {
 855		err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
 856		/* firmware returns next, not last-used seqno */
 857		err = (u16) (err - 0x10);
 858	}
 859
 860	iwl_free_resp(&cmd);
 861	return err;
 862}
 863
 864void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
 865{
 866	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 867	struct iwl_nonqos_seq_query_cmd query_cmd = {
 868		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
 869		.mac_id_n_color =
 870			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 871							mvmvif->color)),
 872		.value = cpu_to_le16(mvmvif->seqno),
 873	};
 874
 875	/* return if called during restart, not resume from D3 */
 876	if (!mvmvif->seqno_valid)
 877		return;
 878
 879	mvmvif->seqno_valid = false;
 880
 881	if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
 882				 sizeof(query_cmd), &query_cmd))
 883		IWL_ERR(mvm, "failed to set non-QoS seqno\n");
 884}
 885
 886static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
 887{
 888	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
 889
 890	iwl_mvm_stop_device(mvm);
 891	/*
 892	 * Set the HW restart bit -- this is mostly true as we're
 893	 * going to load new firmware and reprogram that, though
 894	 * the reprogramming is going to be manual to avoid adding
 895	 * all the MACs that aren't support.
 896	 * We don't have to clear up everything though because the
 897	 * reprogramming is manual. When we resume, we'll actually
 898	 * go through a proper restart sequence again to switch
 899	 * back to the runtime firmware image.
 900	 */
 901	set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
 902
 903	/* the fw is reset, so all the keys are cleared */
 904	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
 905
 906	mvm->ptk_ivlen = 0;
 907	mvm->ptk_icvlen = 0;
 908	mvm->ptk_ivlen = 0;
 909	mvm->ptk_icvlen = 0;
 910
 911	return iwl_mvm_load_d3_fw(mvm);
 912}
 913
 914static int
 915iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
 916			  struct cfg80211_wowlan *wowlan,
 917			  struct iwl_wowlan_config_cmd *wowlan_config_cmd,
 918			  struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
 919			  struct ieee80211_sta *ap_sta)
 920{
 921	struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
 922
 923	/* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
 924
 925	wowlan_config_cmd->is_11n_connection =
 926					ap_sta->deflink.ht_cap.ht_supported;
 927	wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
 928		ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
 929
 930	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 6) {
 931		/* Query the last used seqno and set it */
 932		int ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
 933
 934		if (ret < 0)
 935			return ret;
 936
 937		wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
 938	}
 939
 940	iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
 941
 942	if (wowlan->disconnect)
 943		wowlan_config_cmd->wakeup_filter |=
 944			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
 945				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
 946	if (wowlan->magic_pkt)
 947		wowlan_config_cmd->wakeup_filter |=
 948			cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
 949	if (wowlan->gtk_rekey_failure)
 950		wowlan_config_cmd->wakeup_filter |=
 951			cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
 952	if (wowlan->eap_identity_req)
 953		wowlan_config_cmd->wakeup_filter |=
 954			cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
 955	if (wowlan->four_way_handshake)
 956		wowlan_config_cmd->wakeup_filter |=
 957			cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
 958	if (wowlan->n_patterns)
 959		wowlan_config_cmd->wakeup_filter |=
 960			cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
 961
 962	if (wowlan->rfkill_release)
 963		wowlan_config_cmd->wakeup_filter |=
 964			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
 965
 966	if (wowlan->tcp) {
 967		/*
 968		 * Set the "link change" (really "link lost") flag as well
 969		 * since that implies losing the TCP connection.
 970		 */
 971		wowlan_config_cmd->wakeup_filter |=
 972			cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
 973				    IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
 974				    IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
 975				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
 976	}
 977
 978	if (wowlan->any) {
 979		wowlan_config_cmd->wakeup_filter |=
 980			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
 981				    IWL_WOWLAN_WAKEUP_LINK_CHANGE |
 982				    IWL_WOWLAN_WAKEUP_RX_FRAME |
 983				    IWL_WOWLAN_WAKEUP_BCN_FILTERING);
 984	}
 985
 986	return 0;
 987}
 988
 989static int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
 990					    struct ieee80211_vif *vif)
 991{
 992	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
 993				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
 994	struct wowlan_key_reprogram_data key_data = {};
 995	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 996	int ret;
 997	u8 cmd_ver;
 998	size_t cmd_size;
 999
1000	if (!unified) {
1001		/*
1002		 * if we have to configure keys, call ieee80211_iter_keys(),
1003		 * as we need non-atomic context in order to take the
1004		 * required locks.
1005		 */
1006		/*
1007		 * Note that currently we don't use CMD_ASYNC in the iterator.
1008		 * In case of key_data.configure_keys, all the configured
1009		 * commands are SYNC, and iwl_mvm_wowlan_program_keys() will
1010		 * take care of locking/unlocking mvm->mutex.
1011		 */
1012		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_program_keys,
1013				    &key_data);
1014
1015		if (key_data.error)
1016			return -EIO;
1017	}
1018
1019	ret = iwl_mvm_wowlan_config_rsc_tsc(mvm, vif);
1020	if (ret)
1021		return ret;
1022
1023	if (!fw_has_api(&mvm->fw->ucode_capa,
1024			IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
1025		int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TKIP_PARAM,
1026						IWL_FW_CMD_VER_UNKNOWN);
1027		struct wowlan_key_tkip_data tkip_data = {};
1028		int size;
1029
1030		if (ver == 2) {
1031			size = sizeof(tkip_data.tkip);
1032			tkip_data.tkip.sta_id =
1033				cpu_to_le32(mvmvif->deflink.ap_sta_id);
1034		} else if (ver == 1 || ver == IWL_FW_CMD_VER_UNKNOWN) {
1035			size = sizeof(struct iwl_wowlan_tkip_params_cmd_ver_1);
1036		} else {
1037			WARN_ON_ONCE(1);
1038			return -EINVAL;
1039		}
1040
1041		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_get_tkip_data,
1042				    &tkip_data);
1043
1044		if (tkip_data.have_tkip_keys) {
1045			/* send relevant data according to CMD version */
1046			ret = iwl_mvm_send_cmd_pdu(mvm,
1047						   WOWLAN_TKIP_PARAM,
1048						   CMD_ASYNC, size,
1049						   &tkip_data.tkip);
1050			if (ret)
1051				return ret;
1052		}
1053	}
1054
1055	/* configure rekey data only if offloaded rekey is supported (d3) */
1056	if (mvmvif->rekey_data.valid) {
1057		struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {};
1058		struct iwl_wowlan_kek_kck_material_cmd_v4 *_kek_kck_cmd =
1059			&kek_kck_cmd;
1060		struct wowlan_key_gtk_type_iter gtk_type_data = {
1061			.kek_kck_cmd = _kek_kck_cmd,
1062		};
1063
1064		cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1065						WOWLAN_KEK_KCK_MATERIAL,
1066						IWL_FW_CMD_VER_UNKNOWN);
1067		if (WARN_ON(cmd_ver != 2 && cmd_ver != 3 && cmd_ver != 4 &&
1068			    cmd_ver != IWL_FW_CMD_VER_UNKNOWN))
1069			return -EINVAL;
1070
1071		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_gtk_type_iter,
1072				    &gtk_type_data);
1073
1074		memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1075		       mvmvif->rekey_data.kck_len);
1076		kek_kck_cmd.kck_len = cpu_to_le16(mvmvif->rekey_data.kck_len);
1077		memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1078		       mvmvif->rekey_data.kek_len);
1079		kek_kck_cmd.kek_len = cpu_to_le16(mvmvif->rekey_data.kek_len);
1080		kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1081		kek_kck_cmd.akm = cpu_to_le32(mvmvif->rekey_data.akm);
1082		kek_kck_cmd.sta_id = cpu_to_le32(mvmvif->deflink.ap_sta_id);
1083
1084		if (cmd_ver == 4) {
1085			cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v4);
1086		} else {
1087			if (cmd_ver == 3)
1088				cmd_size =
1089					sizeof(struct iwl_wowlan_kek_kck_material_cmd_v3);
1090			else
1091				cmd_size =
1092					sizeof(struct iwl_wowlan_kek_kck_material_cmd_v2);
1093			/* skip the sta_id at the beginning */
1094			_kek_kck_cmd = (void *)
1095				((u8 *)_kek_kck_cmd + sizeof(kek_kck_cmd.sta_id));
1096		}
1097
1098		IWL_DEBUG_WOWLAN(mvm, "setting akm %d\n",
1099				 mvmvif->rekey_data.akm);
1100
1101		ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_KEK_KCK_MATERIAL,
1102					   CMD_ASYNC, cmd_size, _kek_kck_cmd);
1103		if (ret)
1104			return ret;
1105	}
1106
1107	return 0;
1108}
1109
1110static int
1111iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1112		      struct cfg80211_wowlan *wowlan,
1113		      struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1114		      struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1115		      struct ieee80211_sta *ap_sta)
1116{
1117	int ret;
1118	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1119					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1120
1121	mvm->offload_tid = wowlan_config_cmd->offloading_tid;
1122
1123	if (!unified_image) {
1124		ret = iwl_mvm_switch_to_d3(mvm);
1125		if (ret)
1126			return ret;
1127
1128		ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1129		if (ret)
1130			return ret;
1131	}
1132
 
 
 
 
 
 
1133	ret = iwl_mvm_wowlan_config_key_params(mvm, vif);
 
1134	if (ret)
1135		return ret;
1136
1137	ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1138				   sizeof(*wowlan_config_cmd),
1139				   wowlan_config_cmd);
1140	if (ret)
1141		return ret;
1142
1143	if (fw_has_api(&mvm->fw->ucode_capa,
1144		       IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE))
1145		ret = iwl_mvm_send_patterns(mvm, vif, wowlan);
1146	else
1147		ret = iwl_mvm_send_patterns_v1(mvm, wowlan);
1148	if (ret)
1149		return ret;
1150
1151	return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
1152}
1153
1154static int
1155iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1156			 struct cfg80211_wowlan *wowlan,
1157			 struct cfg80211_sched_scan_request *nd_config,
1158			 struct ieee80211_vif *vif)
1159{
1160	int ret;
1161	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1162					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1163
1164	if (!unified_image) {
1165		ret = iwl_mvm_switch_to_d3(mvm);
1166		if (ret)
1167			return ret;
1168	} else {
1169		/* In theory, we wouldn't have to stop a running sched
1170		 * scan in order to start another one (for
1171		 * net-detect).  But in practice this doesn't seem to
1172		 * work properly, so stop any running sched_scan now.
1173		 */
1174		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1175		if (ret)
1176			return ret;
1177	}
1178
1179	ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1180				       IWL_MVM_SCAN_NETDETECT);
1181	if (ret)
1182		return ret;
1183
1184	if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1185		return -EBUSY;
1186
1187	/* save the sched scan matchsets... */
1188	if (nd_config->n_match_sets) {
1189		mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1190					     sizeof(*nd_config->match_sets) *
1191					     nd_config->n_match_sets,
1192					     GFP_KERNEL);
1193		if (mvm->nd_match_sets)
1194			mvm->n_nd_match_sets = nd_config->n_match_sets;
1195	}
1196
1197	/* ...and the sched scan channels for later reporting */
1198	mvm->nd_channels = kmemdup(nd_config->channels,
1199				   sizeof(*nd_config->channels) *
1200				   nd_config->n_channels,
1201				   GFP_KERNEL);
1202	if (mvm->nd_channels)
1203		mvm->n_nd_channels = nd_config->n_channels;
1204
1205	return 0;
1206}
1207
1208static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1209{
1210	kfree(mvm->nd_match_sets);
1211	mvm->nd_match_sets = NULL;
1212	mvm->n_nd_match_sets = 0;
1213	kfree(mvm->nd_channels);
1214	mvm->nd_channels = NULL;
1215	mvm->n_nd_channels = 0;
1216}
1217
1218static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1219			     struct cfg80211_wowlan *wowlan,
1220			     bool test)
1221{
1222	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1223	struct ieee80211_vif *vif = NULL;
1224	struct iwl_mvm_vif *mvmvif = NULL;
1225	struct ieee80211_sta *ap_sta = NULL;
1226	struct iwl_d3_manager_config d3_cfg_cmd_data = {
1227		/*
1228		 * Program the minimum sleep time to 10 seconds, as many
1229		 * platforms have issues processing a wakeup signal while
1230		 * still being in the process of suspending.
1231		 */
1232		.min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1233	};
1234	struct iwl_host_cmd d3_cfg_cmd = {
1235		.id = D3_CONFIG_CMD,
1236		.flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
1237		.data[0] = &d3_cfg_cmd_data,
1238		.len[0] = sizeof(d3_cfg_cmd_data),
1239	};
1240	int ret;
1241	int len __maybe_unused;
1242	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1243					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1244
1245	if (!wowlan) {
1246		/*
1247		 * mac80211 shouldn't get here, but for D3 test
1248		 * it doesn't warrant a warning
1249		 */
1250		WARN_ON(!test);
1251		return -EINVAL;
1252	}
1253
1254	mutex_lock(&mvm->mutex);
1255
1256	set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1257
1258	synchronize_net();
1259
1260	vif = iwl_mvm_get_bss_vif(mvm);
1261	if (IS_ERR_OR_NULL(vif)) {
1262		ret = 1;
1263		goto out_noreset;
1264	}
1265
1266	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1267
1268	if (mvmvif->deflink.ap_sta_id == IWL_MVM_INVALID_STA) {
1269		/* if we're not associated, this must be netdetect */
1270		if (!wowlan->nd_config) {
1271			ret = 1;
1272			goto out_noreset;
1273		}
1274
1275		ret = iwl_mvm_netdetect_config(
1276			mvm, wowlan, wowlan->nd_config, vif);
1277		if (ret)
1278			goto out;
1279
1280		mvm->net_detect = true;
1281	} else {
1282		struct iwl_wowlan_config_cmd wowlan_config_cmd = {
1283			.offloading_tid = 0,
1284		};
1285
1286		wowlan_config_cmd.sta_id = mvmvif->deflink.ap_sta_id;
1287
1288		ap_sta = rcu_dereference_protected(
1289			mvm->fw_id_to_mac_id[mvmvif->deflink.ap_sta_id],
1290			lockdep_is_held(&mvm->mutex));
1291		if (IS_ERR_OR_NULL(ap_sta)) {
1292			ret = -EINVAL;
1293			goto out_noreset;
1294		}
1295
1296		ret = iwl_mvm_sta_ensure_queue(
1297			mvm, ap_sta->txq[wowlan_config_cmd.offloading_tid]);
1298		if (ret)
1299			goto out_noreset;
1300
1301		ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1302						vif, mvmvif, ap_sta);
1303		if (ret)
1304			goto out_noreset;
1305		ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1306					    vif, mvmvif, ap_sta);
1307		if (ret)
1308			goto out;
1309
1310		mvm->net_detect = false;
1311	}
1312
1313	ret = iwl_mvm_power_update_device(mvm);
1314	if (ret)
1315		goto out;
1316
1317	ret = iwl_mvm_power_update_mac(mvm);
1318	if (ret)
1319		goto out;
1320
1321#ifdef CONFIG_IWLWIFI_DEBUGFS
1322	if (mvm->d3_wake_sysassert)
1323		d3_cfg_cmd_data.wakeup_flags |=
1324			cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1325#endif
1326
1327	/*
1328	 * Prior to 9000 device family the driver needs to stop the dbg
1329	 * recording before entering D3. In later devices the FW stops the
1330	 * recording automatically.
1331	 */
1332	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1333		iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
1334
1335	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1336
1337	/* must be last -- this switches firmware state */
1338	ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1339	if (ret)
1340		goto out;
1341#ifdef CONFIG_IWLWIFI_DEBUGFS
1342	len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1343	if (len >= sizeof(u32)) {
1344		mvm->d3_test_pme_ptr =
1345			le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1346	}
1347#endif
1348	iwl_free_resp(&d3_cfg_cmd);
1349
1350	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1351
1352	ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1353 out:
1354	if (ret < 0) {
1355		iwl_mvm_free_nd(mvm);
1356
1357		if (!unified_image) {
1358			if (mvm->fw_restart > 0) {
1359				mvm->fw_restart--;
1360				ieee80211_restart_hw(mvm->hw);
1361			}
1362		}
1363
1364		clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1365	}
1366 out_noreset:
1367	mutex_unlock(&mvm->mutex);
1368
1369	return ret;
1370}
1371
1372int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1373{
1374	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1375
1376	iwl_mvm_pause_tcm(mvm, true);
1377
1378	iwl_fw_runtime_suspend(&mvm->fwrt);
1379
1380	return __iwl_mvm_suspend(hw, wowlan, false);
1381}
1382
1383struct iwl_multicast_key_data {
1384	u8 key[WOWLAN_KEY_MAX_SIZE];
1385	u8 len;
1386	u8 flags;
1387	u8 id;
1388	u8 ipn[6];
1389};
1390
1391/* converted data from the different status responses */
1392struct iwl_wowlan_status_data {
1393	u64 replay_ctr;
1394	u32 num_of_gtk_rekeys;
1395	u32 received_beacons;
1396	u32 wakeup_reasons;
1397	u32 wake_packet_length;
1398	u32 wake_packet_bufsize;
1399	u16 pattern_number;
1400	u16 non_qos_seq_ctr;
1401	u16 qos_seq_ctr[8];
1402	u8 tid_tear_down;
1403
1404	struct {
1405		/* including RX MIC key for TKIP */
1406		u8 key[WOWLAN_KEY_MAX_SIZE];
1407		u8 len;
1408		u8 flags;
1409		u8 id;
1410	} gtk[WOWLAN_GTK_KEYS_NUM];
1411
1412	struct {
1413		/*
1414		 * We store both the TKIP and AES representations
1415		 * coming from the firmware because we decode the
1416		 * data from there before we iterate the keys and
1417		 * know which one we need.
1418		 */
1419		struct {
1420			struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1421		} tkip, aes;
1422
1423		/*
1424		 * We use -1 for when we have valid data but don't know
1425		 * the key ID from firmware, and thus it needs to be
1426		 * installed with the last key (depending on rekeying).
1427		 */
1428		s8 key_id;
1429		bool valid;
1430	} gtk_seq[2];
1431
1432	struct {
1433		/* Same as above */
1434		struct {
1435			struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1436			u64 tx_pn;
1437		} tkip, aes;
1438	} ptk;
1439
1440	struct iwl_multicast_key_data igtk;
1441	struct iwl_multicast_key_data bigtk[WOWLAN_BIGTK_KEYS_NUM];
 
 
 
 
1442
1443	u8 *wake_packet;
1444};
1445
1446static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1447					  struct ieee80211_vif *vif,
1448					  struct iwl_wowlan_status_data *status)
1449{
1450	struct sk_buff *pkt = NULL;
1451	struct cfg80211_wowlan_wakeup wakeup = {
1452		.pattern_idx = -1,
1453	};
1454	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1455	u32 reasons = status->wakeup_reasons;
1456
1457	if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1458		wakeup_report = NULL;
1459		goto report;
1460	}
1461
1462	pm_wakeup_event(mvm->dev, 0);
1463
1464	if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1465		wakeup.magic_pkt = true;
1466
1467	if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1468		wakeup.pattern_idx =
1469			status->pattern_number;
1470
1471	if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1472		       IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1473		wakeup.disconnect = true;
1474
1475	if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1476		wakeup.gtk_rekey_failure = true;
1477
1478	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1479		wakeup.rfkill_release = true;
1480
1481	if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1482		wakeup.eap_identity_req = true;
1483
1484	if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1485		wakeup.four_way_handshake = true;
1486
1487	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1488		wakeup.tcp_connlost = true;
1489
1490	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1491		wakeup.tcp_nomoretokens = true;
1492
1493	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1494		wakeup.tcp_match = true;
1495
1496	if (status->wake_packet) {
1497		int pktsize = status->wake_packet_bufsize;
1498		int pktlen = status->wake_packet_length;
1499		const u8 *pktdata = status->wake_packet;
1500		const struct ieee80211_hdr *hdr = (const void *)pktdata;
1501		int truncated = pktlen - pktsize;
1502
1503		/* this would be a firmware bug */
1504		if (WARN_ON_ONCE(truncated < 0))
1505			truncated = 0;
1506
1507		if (ieee80211_is_data(hdr->frame_control)) {
1508			int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1509			int ivlen = 0, icvlen = 4; /* also FCS */
1510
1511			pkt = alloc_skb(pktsize, GFP_KERNEL);
1512			if (!pkt)
1513				goto report;
1514
1515			skb_put_data(pkt, pktdata, hdrlen);
1516			pktdata += hdrlen;
1517			pktsize -= hdrlen;
1518
1519			if (ieee80211_has_protected(hdr->frame_control)) {
1520				/*
1521				 * This is unlocked and using gtk_i(c)vlen,
1522				 * but since everything is under RTNL still
1523				 * that's not really a problem - changing
1524				 * it would be difficult.
1525				 */
1526				if (is_multicast_ether_addr(hdr->addr1)) {
1527					ivlen = mvm->gtk_ivlen;
1528					icvlen += mvm->gtk_icvlen;
1529				} else {
1530					ivlen = mvm->ptk_ivlen;
1531					icvlen += mvm->ptk_icvlen;
1532				}
1533			}
1534
1535			/* if truncated, FCS/ICV is (partially) gone */
1536			if (truncated >= icvlen) {
1537				icvlen = 0;
1538				truncated -= icvlen;
1539			} else {
1540				icvlen -= truncated;
1541				truncated = 0;
1542			}
1543
1544			pktsize -= ivlen + icvlen;
1545			pktdata += ivlen;
1546
1547			skb_put_data(pkt, pktdata, pktsize);
1548
1549			if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1550				goto report;
1551			wakeup.packet = pkt->data;
1552			wakeup.packet_present_len = pkt->len;
1553			wakeup.packet_len = pkt->len - truncated;
1554			wakeup.packet_80211 = false;
1555		} else {
1556			int fcslen = 4;
1557
1558			if (truncated >= 4) {
1559				truncated -= 4;
1560				fcslen = 0;
1561			} else {
1562				fcslen -= truncated;
1563				truncated = 0;
1564			}
1565			pktsize -= fcslen;
1566			wakeup.packet = status->wake_packet;
1567			wakeup.packet_present_len = pktsize;
1568			wakeup.packet_len = pktlen - truncated;
1569			wakeup.packet_80211 = true;
1570		}
1571	}
1572
1573 report:
1574	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1575	kfree_skb(pkt);
1576}
1577
1578static void iwl_mvm_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1579{
1580	u64 pn = le64_to_cpu(le_pn);
1581
1582	seq->ccmp.pn[0] = pn >> 40;
1583	seq->ccmp.pn[1] = pn >> 32;
1584	seq->ccmp.pn[2] = pn >> 24;
1585	seq->ccmp.pn[3] = pn >> 16;
1586	seq->ccmp.pn[4] = pn >> 8;
1587	seq->ccmp.pn[5] = pn;
1588}
1589
1590static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1591				  struct ieee80211_key_seq *seq)
1592{
1593	iwl_mvm_le64_to_aes_seq(sc->pn, seq);
1594}
1595
1596static void iwl_mvm_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1597{
1598	u64 pn = le64_to_cpu(le_pn);
1599
1600	seq->tkip.iv16 = (u16)pn;
1601	seq->tkip.iv32 = (u32)(pn >> 16);
1602}
1603
1604static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1605				   struct ieee80211_key_seq *seq)
1606{
1607	seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1608	seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1609}
1610
1611static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key,
1612					struct ieee80211_key_seq *seq)
1613{
1614	int tid;
1615
1616	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1617		ieee80211_set_key_rx_seq(key, tid, &seq[tid]);
1618}
1619
1620static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm,
1621				       struct iwl_wowlan_status_data *status,
1622				       struct ieee80211_sta *sta,
1623				       struct ieee80211_key_conf *key)
1624{
1625	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1626	struct iwl_mvm_key_pn *ptk_pn;
1627	int tid;
1628
1629	iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq);
1630
1631	if (!iwl_mvm_has_new_rx_api(mvm))
1632		return;
1633
1634
1635	rcu_read_lock();
1636	ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
1637	if (WARN_ON(!ptk_pn)) {
1638		rcu_read_unlock();
1639		return;
1640	}
1641
1642	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1643		int i;
1644
1645		for (i = 1; i < mvm->trans->num_rx_queues; i++)
1646			memcpy(ptk_pn->q[i].pn[tid],
1647			       status->ptk.aes.seq[tid].ccmp.pn,
1648			       IEEE80211_CCMP_PN_LEN);
1649	}
1650	rcu_read_unlock();
1651}
1652
1653static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status,
1654					 union iwl_all_tsc_rsc *sc)
1655{
1656	int i;
1657
1658	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1659	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1660
1661	/* GTK RX counters */
1662	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1663		iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i],
1664				       &status->gtk_seq[0].tkip.seq[i]);
1665		iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i],
1666				      &status->gtk_seq[0].aes.seq[i]);
1667	}
1668	status->gtk_seq[0].valid = true;
1669	status->gtk_seq[0].key_id = -1;
1670
1671	/* PTK TX counter */
1672	status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) |
1673				 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16);
1674	status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn);
1675
1676	/* PTK RX counters */
1677	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1678		iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i],
1679				       &status->ptk.tkip.seq[i]);
1680		iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i],
1681				      &status->ptk.aes.seq[i]);
1682	}
1683}
1684
1685static void
1686iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data *status,
1687					struct iwl_wowlan_all_rsc_tsc_v5 *sc,
1688					unsigned int idx, unsigned int key_id)
1689{
1690	int tid;
1691
1692	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1693		iwl_mvm_le64_to_tkip_seq(sc->mcast_rsc[idx][tid],
1694					 &status->gtk_seq[idx].tkip.seq[tid]);
1695		iwl_mvm_le64_to_aes_seq(sc->mcast_rsc[idx][tid],
1696					&status->gtk_seq[idx].aes.seq[tid]);
1697	}
1698
1699	status->gtk_seq[idx].valid = true;
1700	status->gtk_seq[idx].key_id = key_id;
1701}
1702
1703static void
1704iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data *status,
1705				struct iwl_wowlan_all_rsc_tsc_v5 *sc)
1706{
1707	int i, tid;
1708
1709	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1710	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1711	BUILD_BUG_ON(ARRAY_SIZE(sc->mcast_rsc) != ARRAY_SIZE(status->gtk_seq));
1712
1713	/* GTK RX counters */
1714	for (i = 0; i < ARRAY_SIZE(sc->mcast_key_id_map); i++) {
1715		u8 entry = sc->mcast_key_id_map[i];
1716
1717		if (entry < ARRAY_SIZE(sc->mcast_rsc))
1718			iwl_mvm_convert_key_counters_v5_gtk_seq(status, sc,
1719								entry, i);
1720	}
1721
1722	/* PTK TX counters not needed, assigned in device */
1723
1724	/* PTK RX counters */
1725	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1726		iwl_mvm_le64_to_tkip_seq(sc->ucast_rsc[tid],
1727					 &status->ptk.tkip.seq[tid]);
1728		iwl_mvm_le64_to_aes_seq(sc->ucast_rsc[tid],
1729					&status->ptk.aes.seq[tid]);
1730	}
1731}
1732
1733static void iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf *key,
1734				       struct iwl_wowlan_status_data *status,
1735				       int idx)
1736{
1737	switch (key->cipher) {
1738	case WLAN_CIPHER_SUITE_CCMP:
1739	case WLAN_CIPHER_SUITE_GCMP:
1740	case WLAN_CIPHER_SUITE_GCMP_256:
1741		iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].aes.seq);
1742		break;
1743	case WLAN_CIPHER_SUITE_TKIP:
1744		iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].tkip.seq);
1745		break;
1746	default:
1747		WARN_ON(1);
1748	}
1749}
1750
1751static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1752				   struct iwl_wowlan_status_data *status,
1753				   bool installed)
1754{
1755	int i;
1756
1757	for (i = 0; i < ARRAY_SIZE(status->gtk_seq); i++) {
1758		if (!status->gtk_seq[i].valid)
1759			continue;
1760
1761		/* Handle the case where we know the key ID */
1762		if (status->gtk_seq[i].key_id == key->keyidx) {
1763			s8 new_key_id = -1;
1764
1765			if (status->num_of_gtk_rekeys)
1766				new_key_id = status->gtk[0].flags &
1767						IWL_WOWLAN_GTK_IDX_MASK;
1768
1769			/* Don't install a new key's value to an old key */
1770			if (new_key_id != key->keyidx)
1771				iwl_mvm_set_key_rx_seq_idx(key, status, i);
1772			continue;
1773		}
1774
1775		/* handle the case where we didn't, last key only */
1776		if (status->gtk_seq[i].key_id == -1 &&
1777		    (!status->num_of_gtk_rekeys || installed))
1778			iwl_mvm_set_key_rx_seq_idx(key, status, i);
1779	}
1780}
1781
1782struct iwl_mvm_d3_gtk_iter_data {
1783	struct iwl_mvm *mvm;
1784	struct iwl_wowlan_status_data *status;
1785	u32 gtk_cipher, igtk_cipher, bigtk_cipher;
1786	bool unhandled_cipher, igtk_support, bigtk_support;
 
1787	int num_keys;
1788};
1789
1790static void iwl_mvm_d3_find_last_keys(struct ieee80211_hw *hw,
1791				      struct ieee80211_vif *vif,
1792				      struct ieee80211_sta *sta,
1793				      struct ieee80211_key_conf *key,
1794				      void *_data)
1795{
1796	struct iwl_mvm_d3_gtk_iter_data *data = _data;
 
1797
1798	if (data->unhandled_cipher)
1799		return;
1800
1801	switch (key->cipher) {
1802	case WLAN_CIPHER_SUITE_WEP40:
1803	case WLAN_CIPHER_SUITE_WEP104:
1804		/* ignore WEP completely, nothing to do */
1805		return;
1806	case WLAN_CIPHER_SUITE_CCMP:
1807	case WLAN_CIPHER_SUITE_GCMP:
1808	case WLAN_CIPHER_SUITE_GCMP_256:
1809	case WLAN_CIPHER_SUITE_TKIP:
1810		/* we support these */
1811		data->gtk_cipher = key->cipher;
1812		break;
1813	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1814	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1815	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1816	case WLAN_CIPHER_SUITE_AES_CMAC:
1817		/* we support these */
1818		if (data->igtk_support &&
1819		    (key->keyidx == 4 || key->keyidx == 5)) {
1820			data->igtk_cipher = key->cipher;
1821		} else if (data->bigtk_support &&
1822			   (key->keyidx == 6 || key->keyidx == 7)) {
1823			data->bigtk_cipher = key->cipher;
1824		} else {
1825			data->unhandled_cipher = true;
1826			return;
1827		}
1828		break;
1829	default:
1830		/* everything else - disconnect from AP */
1831		data->unhandled_cipher = true;
1832		return;
1833	}
1834
1835	data->num_keys++;
1836}
1837
1838static void
1839iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data *key,
1840			      struct ieee80211_key_seq *seq, u32 cipher)
1841{
1842	switch (cipher) {
1843	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1844	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1845		BUILD_BUG_ON(sizeof(seq->aes_gmac.pn) != sizeof(key->ipn));
1846		memcpy(seq->aes_gmac.pn, key->ipn, sizeof(seq->aes_gmac.pn));
1847		break;
1848	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1849		BUILD_BUG_ON(sizeof(seq->aes_cmac.pn) != sizeof(key->ipn));
1850		memcpy(seq->aes_cmac.pn, key->ipn, sizeof(seq->aes_cmac.pn));
1851		break;
1852	}
1853}
1854
1855static void
1856iwl_mvm_d3_update_igtk_bigtk(struct iwl_wowlan_status_data *status,
1857			     struct ieee80211_key_conf *key,
1858			     struct iwl_multicast_key_data *key_data)
1859{
1860	if (status->num_of_gtk_rekeys && key_data->len) {
1861		/* remove rekeyed key */
1862		ieee80211_remove_key(key);
1863	} else {
1864		struct ieee80211_key_seq seq;
1865
1866		iwl_mvm_d3_set_igtk_bigtk_ipn(key_data,
1867					      &seq,
1868					      key->cipher);
1869		ieee80211_set_key_rx_seq(key, 0, &seq);
1870	}
1871}
1872
1873static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1874				   struct ieee80211_vif *vif,
1875				   struct ieee80211_sta *sta,
1876				   struct ieee80211_key_conf *key,
1877				   void *_data)
1878{
1879	struct iwl_mvm_d3_gtk_iter_data *data = _data;
1880	struct iwl_wowlan_status_data *status = data->status;
1881	s8 keyidx;
1882
1883	if (data->unhandled_cipher)
1884		return;
1885
1886	switch (key->cipher) {
1887	case WLAN_CIPHER_SUITE_WEP40:
1888	case WLAN_CIPHER_SUITE_WEP104:
1889		/* ignore WEP completely, nothing to do */
1890		return;
1891	case WLAN_CIPHER_SUITE_CCMP:
1892	case WLAN_CIPHER_SUITE_GCMP:
1893	case WLAN_CIPHER_SUITE_GCMP_256:
1894		if (sta) {
1895			atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn);
1896			iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key);
1897			return;
1898		}
1899		fallthrough;
1900	case WLAN_CIPHER_SUITE_TKIP:
1901		if (sta) {
1902			atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn);
1903			iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq);
1904			return;
1905		}
1906		keyidx = key->keyidx;
1907		/* The current key is always sent by the FW, even if it wasn't
1908		 * rekeyed during D3.
1909		 * We remove an existing key if it has the same index as
1910		 * a new key
1911		 */
1912		if (status->num_of_gtk_rekeys &&
1913		    ((status->gtk[0].len && keyidx == status->gtk[0].id) ||
1914		     (status->gtk[1].len && keyidx == status->gtk[1].id))) {
1915			ieee80211_remove_key(key);
1916		} else {
1917			iwl_mvm_set_key_rx_seq(key, data->status, false);
1918		}
1919		break;
1920	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1921	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1922	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1923	case WLAN_CIPHER_SUITE_AES_CMAC:
1924		if (key->keyidx == 4 || key->keyidx == 5) {
1925			iwl_mvm_d3_update_igtk_bigtk(status, key,
1926						     &status->igtk);
1927		}
1928		if (key->keyidx == 6 || key->keyidx == 7) {
1929			u8 idx = key->keyidx == status->bigtk[1].id;
1930
1931			iwl_mvm_d3_update_igtk_bigtk(status, key,
1932						     &status->bigtk[idx]);
1933		}
1934	}
1935}
1936
1937static bool iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data *status,
1938			      struct ieee80211_vif *vif,
1939			      struct iwl_mvm *mvm, u32 gtk_cipher)
1940{
1941	int i;
1942	struct ieee80211_key_conf *key;
1943	struct {
1944		struct ieee80211_key_conf conf;
1945		u8 key[32];
1946	} conf = {
1947		.conf.cipher = gtk_cipher,
1948	};
1949
1950	BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
1951	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP);
1952	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256);
1953	BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP);
1954	BUILD_BUG_ON(sizeof(conf.key) < sizeof(status->gtk[0].key));
1955
1956	switch (gtk_cipher) {
1957	case WLAN_CIPHER_SUITE_CCMP:
1958	case WLAN_CIPHER_SUITE_GCMP:
1959		conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1960		break;
1961	case WLAN_CIPHER_SUITE_GCMP_256:
1962		conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
1963		break;
1964	case WLAN_CIPHER_SUITE_TKIP:
1965		conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1966		break;
1967	default:
1968		WARN_ON(1);
1969	}
1970
1971	for (i = 0; i < ARRAY_SIZE(status->gtk); i++) {
1972		if (!status->gtk[i].len)
1973			continue;
1974
1975		conf.conf.keyidx = status->gtk[i].id;
1976		IWL_DEBUG_WOWLAN(mvm,
1977				 "Received from FW GTK cipher %d, key index %d\n",
1978				 conf.conf.cipher, conf.conf.keyidx);
1979		memcpy(conf.conf.key, status->gtk[i].key,
1980		       sizeof(status->gtk[i].key));
1981
1982		key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1983		if (IS_ERR(key))
1984			return false;
1985		iwl_mvm_set_key_rx_seq_idx(key, status, i);
1986	}
1987
1988	return true;
1989}
1990
1991static bool
1992iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data *status,
1993				struct ieee80211_vif *vif, u32 cipher,
1994				struct iwl_multicast_key_data *key_data)
1995{
1996	struct ieee80211_key_conf *key_config;
1997	struct {
1998		struct ieee80211_key_conf conf;
1999		u8 key[WOWLAN_KEY_MAX_SIZE];
2000	} conf = {
2001		.conf.cipher = cipher,
2002		.conf.keyidx = key_data->id,
2003	};
2004	struct ieee80211_key_seq seq;
2005
2006	if (!key_data->len)
2007		return true;
2008
2009	iwl_mvm_d3_set_igtk_bigtk_ipn(key_data, &seq, conf.conf.cipher);
2010
2011	switch (cipher) {
2012	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2013		conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2014		break;
2015	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2016		conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2017		break;
2018	case WLAN_CIPHER_SUITE_AES_CMAC:
2019		conf.conf.keylen = WLAN_KEY_LEN_AES_CMAC;
2020		break;
2021	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2022		conf.conf.keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2023		break;
2024	default:
2025		WARN_ON(1);
2026	}
2027	BUILD_BUG_ON(sizeof(conf.key) < sizeof(key_data->key));
2028	memcpy(conf.conf.key, key_data->key, conf.conf.keylen);
2029
2030	key_config = ieee80211_gtk_rekey_add(vif, &conf.conf);
2031	if (IS_ERR(key_config))
2032		return false;
2033	ieee80211_set_key_rx_seq(key_config, 0, &seq);
2034
2035	if (key_config->keyidx == 4 || key_config->keyidx == 5) {
2036		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2037		int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
2038		struct iwl_mvm_vif_link_info *mvm_link =
2039			mvmvif->link[link_id];
2040
2041		mvm_link->igtk = key_config;
 
 
 
2042	}
2043
2044	return true;
2045}
2046
2047static int iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm *mvm)
2048{
2049	u8 notif_ver;
2050
2051	if (!fw_has_api(&mvm->fw->ucode_capa,
2052			IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL))
2053		return 6;
2054
2055	/* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2056	notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2057					    WOWLAN_GET_STATUSES, 0);
2058	if (!notif_ver)
2059		notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
2060						    WOWLAN_GET_STATUSES, 7);
2061
2062	return notif_ver;
 
2063}
2064
2065static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
2066					  struct ieee80211_vif *vif,
2067					  struct iwl_wowlan_status_data *status)
2068{
2069	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2070	struct iwl_mvm_d3_gtk_iter_data gtkdata = {
2071		.mvm = mvm,
2072		.status = status,
2073	};
2074	int i;
2075
2076	u32 disconnection_reasons =
2077		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
2078		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
2079
2080	if (!status || !vif->bss_conf.bssid)
2081		return false;
2082
2083	if (status->wakeup_reasons & disconnection_reasons)
2084		return false;
2085
2086	if (iwl_mvm_lookup_wowlan_status_ver(mvm) > 6 ||
2087	    iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2088				    WOWLAN_INFO_NOTIFICATION,
2089				    0))
2090		gtkdata.igtk_support = true;
2091
2092	if (iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2093				    WOWLAN_INFO_NOTIFICATION,
2094				    0) >= 3)
2095		gtkdata.bigtk_support = true;
2096
2097	/* find last GTK that we used initially, if any */
 
2098	ieee80211_iter_keys(mvm->hw, vif,
2099			    iwl_mvm_d3_find_last_keys, &gtkdata);
2100	/* not trying to keep connections with MFP/unhandled ciphers */
2101	if (gtkdata.unhandled_cipher)
2102		return false;
2103	if (!gtkdata.num_keys)
2104		goto out;
 
 
2105
2106	/*
2107	 * invalidate all other GTKs that might still exist and update
2108	 * the one that we used
2109	 */
 
2110	ieee80211_iter_keys(mvm->hw, vif,
2111			    iwl_mvm_d3_update_keys, &gtkdata);
2112
 
 
2113	if (status->num_of_gtk_rekeys) {
2114		__be64 replay_ctr = cpu_to_be64(status->replay_ctr);
 
 
 
 
 
 
 
 
 
2115
2116		IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
2117				 status->num_of_gtk_rekeys);
 
2118
2119		if (!iwl_mvm_gtk_rekey(status, vif, mvm, gtkdata.gtk_cipher))
2120			return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2121
2122		if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2123						     gtkdata.igtk_cipher,
2124						     &status->igtk))
2125			return false;
 
2126
2127		for (i = 0; i < ARRAY_SIZE(status->bigtk); i++) {
2128			if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2129							     gtkdata.bigtk_cipher,
2130							     &status->bigtk[i]))
2131				return false;
2132		}
2133
2134		ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
2135					   (void *)&replay_ctr, GFP_KERNEL);
2136	}
2137
2138out:
2139	if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2140				    WOWLAN_GET_STATUSES, 0) < 10) {
2141		mvmvif->seqno_valid = true;
2142		/* +0x10 because the set API expects next-to-use, not last-used */
2143		mvmvif->seqno = status->non_qos_seq_ctr + 0x10;
2144	}
2145
2146	return true;
2147}
2148
2149static void iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data *status,
2150				   struct iwl_wowlan_gtk_status_v2 *data)
2151{
2152	BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data->key));
2153	BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2154		     sizeof(data->tkip_mic_key) >
2155		     sizeof(status->gtk[0].key));
2156
2157	status->gtk[0].len = data->key_len;
2158	status->gtk[0].flags = data->key_flags;
2159
2160	memcpy(status->gtk[0].key, data->key, sizeof(data->key));
2161
2162	/* if it's as long as the TKIP encryption key, copy MIC key */
2163	if (status->gtk[0].len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2164		memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2165		       data->tkip_mic_key, sizeof(data->tkip_mic_key));
2166}
2167
2168static void iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data *status,
2169				   struct iwl_wowlan_gtk_status_v3 *data)
2170{
2171	int data_idx, status_idx = 0;
 
 
 
 
 
 
 
 
 
 
 
 
2172
2173	BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data[0].key));
2174	BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2175		     sizeof(data[0].tkip_mic_key) >
2176		     sizeof(status->gtk[0].key));
2177	BUILD_BUG_ON(ARRAY_SIZE(status->gtk) < WOWLAN_GTK_KEYS_NUM);
2178	for (data_idx = 0; data_idx < ARRAY_SIZE(status->gtk); data_idx++) {
2179		if (!(data[data_idx].key_len))
2180			continue;
2181		status->gtk[status_idx].len = data[data_idx].key_len;
2182		status->gtk[status_idx].flags = data[data_idx].key_flags;
2183		status->gtk[status_idx].id = status->gtk[status_idx].flags &
2184				    IWL_WOWLAN_GTK_IDX_MASK;
2185
2186		memcpy(status->gtk[status_idx].key, data[data_idx].key,
2187		       sizeof(data[data_idx].key));
2188
2189		/* if it's as long as the TKIP encryption key, copy MIC key */
2190		if (status->gtk[status_idx].len ==
2191		    NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2192			memcpy(status->gtk[status_idx].key +
2193			       NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2194			       data[data_idx].tkip_mic_key,
2195			       sizeof(data[data_idx].tkip_mic_key));
2196		status_idx++;
2197	}
2198}
2199
2200static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status,
2201				 struct iwl_wowlan_igtk_status *data)
2202{
2203	BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key));
2204
2205	if (!data->key_len)
2206		return;
2207
2208	status->igtk.len = data->key_len;
2209	status->igtk.flags = data->key_flags;
2210	status->igtk.id = u32_get_bits(data->key_flags,
2211				       IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2212		+ WOWLAN_IGTK_MIN_INDEX;
2213
2214	memcpy(status->igtk.key, data->key, sizeof(data->key));
2215	memcpy(status->igtk.ipn, data->ipn, sizeof(data->ipn));
2216}
2217
2218static void iwl_mvm_convert_bigtk(struct iwl_wowlan_status_data *status,
2219				  const struct iwl_wowlan_igtk_status *data)
2220{
2221	int data_idx, status_idx = 0;
2222
2223	BUILD_BUG_ON(ARRAY_SIZE(status->bigtk) < WOWLAN_BIGTK_KEYS_NUM);
2224
2225	for (data_idx = 0; data_idx < WOWLAN_BIGTK_KEYS_NUM; data_idx++) {
2226		if (!data[data_idx].key_len)
2227			continue;
2228
2229		status->bigtk[status_idx].len = data[data_idx].key_len;
2230		status->bigtk[status_idx].flags = data[data_idx].key_flags;
2231		status->bigtk[status_idx].id =
2232			u32_get_bits(data[data_idx].key_flags,
2233				     IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2234			+ WOWLAN_BIGTK_MIN_INDEX;
2235
2236		BUILD_BUG_ON(sizeof(status->bigtk[status_idx].key) <
2237			     sizeof(data[data_idx].key));
2238		BUILD_BUG_ON(sizeof(status->bigtk[status_idx].ipn) <
2239			     sizeof(data[data_idx].ipn));
2240
2241		memcpy(status->bigtk[status_idx].key, data[data_idx].key,
2242		       sizeof(data[data_idx].key));
2243		memcpy(status->bigtk[status_idx].ipn, data[data_idx].ipn,
2244		       sizeof(data[data_idx].ipn));
2245		status_idx++;
2246	}
2247}
2248
2249static void iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm *mvm,
2250					    struct iwl_wowlan_info_notif *data,
2251					    struct iwl_wowlan_status_data *status,
2252					    u32 len)
2253{
2254	u32 i;
2255
2256	if (!data) {
2257		IWL_ERR(mvm, "iwl_wowlan_info_notif data is NULL\n");
2258		status = NULL;
2259		return;
2260	}
2261
2262	if (len < sizeof(*data)) {
2263		IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2264		status = NULL;
2265		return;
2266	}
2267
2268	iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2269	iwl_mvm_convert_gtk_v3(status, data->gtk);
2270	iwl_mvm_convert_igtk(status, &data->igtk[0]);
2271	iwl_mvm_convert_bigtk(status, data->bigtk);
2272	status->replay_ctr = le64_to_cpu(data->replay_ctr);
2273	status->pattern_number = le16_to_cpu(data->pattern_number);
2274	for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2275		status->qos_seq_ctr[i] =
2276			le16_to_cpu(data->qos_seq_ctr[i]);
2277	status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2278	status->num_of_gtk_rekeys =
2279		le32_to_cpu(data->num_of_gtk_rekeys);
2280	status->received_beacons = le32_to_cpu(data->received_beacons);
2281	status->tid_tear_down = data->tid_tear_down;
2282}
2283
2284static void
2285iwl_mvm_parse_wowlan_info_notif_v2(struct iwl_mvm *mvm,
2286				   struct iwl_wowlan_info_notif_v2 *data,
2287				   struct iwl_wowlan_status_data *status,
2288				   u32 len)
2289{
2290	u32 i;
2291
2292	if (!data) {
2293		IWL_ERR(mvm, "iwl_wowlan_info_notif data is NULL\n");
2294		status = NULL;
2295		return;
2296	}
2297
2298	if (len < sizeof(*data)) {
2299		IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2300		status = NULL;
2301		return;
2302	}
2303
2304	iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2305	iwl_mvm_convert_gtk_v3(status, data->gtk);
2306	iwl_mvm_convert_igtk(status, &data->igtk[0]);
2307	status->replay_ctr = le64_to_cpu(data->replay_ctr);
2308	status->pattern_number = le16_to_cpu(data->pattern_number);
2309	for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2310		status->qos_seq_ctr[i] =
2311			le16_to_cpu(data->qos_seq_ctr[i]);
2312	status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2313	status->num_of_gtk_rekeys =
2314		le32_to_cpu(data->num_of_gtk_rekeys);
2315	status->received_beacons = le32_to_cpu(data->received_beacons);
2316	status->tid_tear_down = data->tid_tear_down;
2317}
2318
2319/* Occasionally, templates would be nice. This is one of those times ... */
2320#define iwl_mvm_parse_wowlan_status_common(_ver)			\
2321static struct iwl_wowlan_status_data *					\
2322iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm,	\
2323					    struct iwl_wowlan_status_ ##_ver *data,\
2324					    int len)			\
2325{									\
2326	struct iwl_wowlan_status_data *status;				\
2327	int data_size, i;						\
2328									\
2329	if (len < sizeof(*data)) {					\
2330		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
2331		return NULL;						\
2332	}								\
2333									\
2334	data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4);	\
2335	if (len != sizeof(*data) + data_size) {				\
2336		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
2337		return NULL;						\
2338	}								\
2339									\
2340	status = kzalloc(sizeof(*status), GFP_KERNEL);			\
2341	if (!status)							\
2342		return NULL;						\
2343									\
2344	/* copy all the common fields */				\
2345	status->replay_ctr = le64_to_cpu(data->replay_ctr);		\
2346	status->pattern_number = le16_to_cpu(data->pattern_number);	\
2347	status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr);	\
2348	for (i = 0; i < 8; i++)						\
2349		status->qos_seq_ctr[i] =				\
2350			le16_to_cpu(data->qos_seq_ctr[i]);		\
2351	status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);	\
2352	status->num_of_gtk_rekeys =					\
2353		le32_to_cpu(data->num_of_gtk_rekeys);			\
2354	status->received_beacons = le32_to_cpu(data->received_beacons);	\
2355	status->wake_packet_length =					\
2356		le32_to_cpu(data->wake_packet_length);			\
2357	status->wake_packet_bufsize =					\
2358		le32_to_cpu(data->wake_packet_bufsize);			\
2359	if (status->wake_packet_bufsize) {				\
2360		status->wake_packet =					\
2361			kmemdup(data->wake_packet,			\
2362				status->wake_packet_bufsize,		\
2363				GFP_KERNEL);				\
2364		if (!status->wake_packet) {				\
2365			kfree(status);					\
2366			return NULL;					\
2367		}							\
2368	} else {							\
2369		status->wake_packet = NULL;				\
2370	}								\
2371									\
2372	return status;							\
2373}
2374
2375iwl_mvm_parse_wowlan_status_common(v6)
2376iwl_mvm_parse_wowlan_status_common(v7)
2377iwl_mvm_parse_wowlan_status_common(v9)
2378iwl_mvm_parse_wowlan_status_common(v12)
2379
2380static struct iwl_wowlan_status_data *
2381iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id)
2382{
2383	struct iwl_wowlan_status_data *status;
2384	struct iwl_wowlan_get_status_cmd get_status_cmd = {
2385		.sta_id = cpu_to_le32(sta_id),
2386	};
2387	struct iwl_host_cmd cmd = {
2388		.id = WOWLAN_GET_STATUSES,
2389		.flags = CMD_WANT_SKB,
2390		.data = { &get_status_cmd, },
2391		.len = { sizeof(get_status_cmd), },
2392	};
2393	int ret, len;
2394	u8 notif_ver;
2395	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
2396					   IWL_FW_CMD_VER_UNKNOWN);
2397
2398	if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN)
2399		cmd.len[0] = 0;
2400
2401	lockdep_assert_held(&mvm->mutex);
2402
2403	ret = iwl_mvm_send_cmd(mvm, &cmd);
2404	if (ret) {
2405		IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
2406		return ERR_PTR(ret);
2407	}
2408
2409	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2410
2411	/* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2412	notif_ver = iwl_mvm_lookup_wowlan_status_ver(mvm);
 
 
 
 
2413
2414	if (notif_ver < 7) {
 
2415		struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
2416
2417		status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len);
2418		if (!status)
2419			goto out_free_resp;
2420
2421		BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
2422			     sizeof(status->gtk[0].key));
2423		BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2424			     sizeof(v6->gtk.tkip_mic_key) >
2425			     sizeof(status->gtk[0].key));
2426
2427		/* copy GTK info to the right place */
2428		memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
2429		       sizeof(v6->gtk.decrypt_key));
2430		memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2431		       v6->gtk.tkip_mic_key,
2432		       sizeof(v6->gtk.tkip_mic_key));
2433
2434		iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc);
2435
2436		/* hardcode the key length to 16 since v6 only supports 16 */
2437		status->gtk[0].len = 16;
2438
2439		/*
2440		 * The key index only uses 2 bits (values 0 to 3) and
2441		 * we always set bit 7 which means this is the
2442		 * currently used key.
2443		 */
2444		status->gtk[0].flags = v6->gtk.key_index | BIT(7);
2445	} else if (notif_ver == 7) {
2446		struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
2447
2448		status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len);
2449		if (!status)
2450			goto out_free_resp;
2451
2452		iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc);
2453		iwl_mvm_convert_gtk_v2(status, &v7->gtk[0]);
2454		iwl_mvm_convert_igtk(status, &v7->igtk[0]);
2455	} else if (notif_ver == 9 || notif_ver == 10 || notif_ver == 11) {
2456		struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data;
2457
2458		/* these three command versions have same layout and size, the
2459		 * difference is only in a few not used (reserved) fields.
2460		 */
2461		status = iwl_mvm_parse_wowlan_status_common_v9(mvm, v9, len);
2462		if (!status)
2463			goto out_free_resp;
2464
2465		iwl_mvm_convert_key_counters(status, &v9->gtk[0].rsc.all_tsc_rsc);
2466		iwl_mvm_convert_gtk_v2(status, &v9->gtk[0]);
2467		iwl_mvm_convert_igtk(status, &v9->igtk[0]);
2468
2469		status->tid_tear_down = v9->tid_tear_down;
2470	} else if (notif_ver == 12) {
2471		struct iwl_wowlan_status_v12 *v12 = (void *)cmd.resp_pkt->data;
2472
2473		status = iwl_mvm_parse_wowlan_status_common_v12(mvm, v12, len);
2474		if (!status)
2475			goto out_free_resp;
2476
2477		iwl_mvm_convert_key_counters_v5(status, &v12->gtk[0].sc);
2478		iwl_mvm_convert_gtk_v3(status, v12->gtk);
2479		iwl_mvm_convert_igtk(status, &v12->igtk[0]);
2480
2481		status->tid_tear_down = v12->tid_tear_down;
2482	} else {
2483		IWL_ERR(mvm,
2484			"Firmware advertises unknown WoWLAN status response %d!\n",
2485			notif_ver);
2486		status = NULL;
2487	}
2488
2489out_free_resp:
2490	iwl_free_resp(&cmd);
2491	return status;
2492}
2493
2494/* releases the MVM mutex */
2495static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
2496					 struct ieee80211_vif *vif,
2497					 struct iwl_wowlan_status_data *status)
2498{
2499	int i;
2500	bool keep = false;
2501	struct iwl_mvm_sta *mvm_ap_sta;
2502
2503	if (!status)
2504		goto out_unlock;
2505
2506	IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
2507			 status->wakeup_reasons);
2508
2509	/* still at hard-coded place 0 for D3 image */
2510	mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
2511	if (!mvm_ap_sta)
2512		goto out_unlock;
2513
2514	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
2515		u16 seq = status->qos_seq_ctr[i];
2516		/* firmware stores last-used value, we store next value */
2517		seq += 0x10;
2518		mvm_ap_sta->tid_data[i].seq_number = seq;
2519	}
2520
2521	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
2522		i = mvm->offload_tid;
2523		iwl_trans_set_q_ptrs(mvm->trans,
2524				     mvm_ap_sta->tid_data[i].txq_id,
2525				     mvm_ap_sta->tid_data[i].seq_number >> 4);
2526	}
2527
 
 
 
2528	iwl_mvm_report_wakeup_reasons(mvm, vif, status);
2529
2530	keep = iwl_mvm_setup_connection_keep(mvm, vif, status);
 
 
 
2531out_unlock:
2532	mutex_unlock(&mvm->mutex);
2533	return keep;
2534}
2535
2536#define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
2537			  IWL_SCAN_MAX_PROFILES)
2538
2539struct iwl_mvm_nd_results {
2540	u32 matched_profiles;
2541	u8 matches[ND_QUERY_BUF_LEN];
2542};
2543
2544static int
2545iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
2546				struct iwl_mvm_nd_results *results)
2547{
2548	struct iwl_scan_offload_match_info *query;
2549	struct iwl_host_cmd cmd = {
2550		.id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
2551		.flags = CMD_WANT_SKB,
2552	};
2553	int ret, len;
2554	size_t query_len, matches_len;
2555	int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
2556
2557	ret = iwl_mvm_send_cmd(mvm, &cmd);
2558	if (ret) {
2559		IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
2560		return ret;
2561	}
2562
2563	if (fw_has_api(&mvm->fw->ucode_capa,
2564		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2565		query_len = sizeof(struct iwl_scan_offload_match_info);
2566		matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2567			max_profiles;
2568	} else {
2569		query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
2570		matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
2571			max_profiles;
2572	}
2573
2574	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2575	if (len < query_len) {
2576		IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
2577		ret = -EIO;
2578		goto out_free_resp;
2579	}
2580
2581	query = (void *)cmd.resp_pkt->data;
2582
2583	results->matched_profiles = le32_to_cpu(query->matched_profiles);
2584	memcpy(results->matches, query->matches, matches_len);
2585
2586#ifdef CONFIG_IWLWIFI_DEBUGFS
2587	mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
2588#endif
2589
2590out_free_resp:
2591	iwl_free_resp(&cmd);
2592	return ret;
2593}
2594
2595static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
2596					 struct iwl_mvm_nd_results *results,
2597					 int idx)
2598{
2599	int n_chans = 0, i;
2600
2601	if (fw_has_api(&mvm->fw->ucode_capa,
2602		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2603		struct iwl_scan_offload_profile_match *matches =
2604			(void *)results->matches;
2605
2606		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
2607			n_chans += hweight8(matches[idx].matching_channels[i]);
2608	} else {
2609		struct iwl_scan_offload_profile_match_v1 *matches =
2610			(void *)results->matches;
2611
2612		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
2613			n_chans += hweight8(matches[idx].matching_channels[i]);
2614	}
2615
2616	return n_chans;
2617}
2618
2619static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
2620				    struct iwl_mvm_nd_results *results,
2621				    struct cfg80211_wowlan_nd_match *match,
2622				    int idx)
2623{
2624	int i;
2625
2626	if (fw_has_api(&mvm->fw->ucode_capa,
2627		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2628		struct iwl_scan_offload_profile_match *matches =
2629			 (void *)results->matches;
2630
2631		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
2632			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2633				match->channels[match->n_channels++] =
2634					mvm->nd_channels[i]->center_freq;
2635	} else {
2636		struct iwl_scan_offload_profile_match_v1 *matches =
2637			 (void *)results->matches;
2638
2639		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
2640			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2641				match->channels[match->n_channels++] =
2642					mvm->nd_channels[i]->center_freq;
2643	}
2644}
2645
2646/**
2647 * enum iwl_d3_notif - d3 notifications
2648 * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF was received
2649 * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF was received
2650 * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF was received
2651 * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF was received
2652 * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF was received
2653 */
2654enum iwl_d3_notif {
2655	IWL_D3_NOTIF_WOWLAN_INFO =	BIT(0),
2656	IWL_D3_NOTIF_WOWLAN_WAKE_PKT =	BIT(1),
2657	IWL_D3_NOTIF_PROT_OFFLOAD =	BIT(2),
2658	IWL_D3_ND_MATCH_INFO      =     BIT(3),
2659	IWL_D3_NOTIF_D3_END_NOTIF =	BIT(4)
2660};
2661
2662/* manage d3 resume data */
2663struct iwl_d3_data {
2664	struct iwl_wowlan_status_data *status;
2665	bool test;
2666	u32 d3_end_flags;
2667	u32 notif_expected;	/* bitmap - see &enum iwl_d3_notif */
2668	u32 notif_received;	/* bitmap - see &enum iwl_d3_notif */
2669	struct iwl_mvm_nd_results *nd_results;
2670	bool nd_results_valid;
2671};
2672
2673static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
2674					    struct ieee80211_vif *vif,
2675					    struct iwl_d3_data *d3_data)
2676{
2677	struct cfg80211_wowlan_nd_info *net_detect = NULL;
2678	struct cfg80211_wowlan_wakeup wakeup = {
2679		.pattern_idx = -1,
2680	};
2681	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
2682	unsigned long matched_profiles;
2683	u32 reasons = 0;
2684	int i, n_matches, ret;
2685
2686	if (WARN_ON(!d3_data || !d3_data->status))
2687		goto out;
2688
2689	reasons = d3_data->status->wakeup_reasons;
2690
2691	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
2692		wakeup.rfkill_release = true;
2693
2694	if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
2695		goto out;
2696
2697	if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2698				     WOWLAN_INFO_NOTIFICATION, 0)) {
2699		IWL_INFO(mvm, "Query FW for ND results\n");
2700		ret = iwl_mvm_netdetect_query_results(mvm, d3_data->nd_results);
2701
2702	} else {
2703		IWL_INFO(mvm, "Notification based ND results\n");
2704		ret = d3_data->nd_results_valid ? 0 : -1;
2705	}
2706
2707	if (ret || !d3_data->nd_results->matched_profiles) {
2708		wakeup_report = NULL;
2709		goto out;
2710	}
2711
2712	matched_profiles = d3_data->nd_results->matched_profiles;
2713	if (mvm->n_nd_match_sets) {
2714		n_matches = hweight_long(matched_profiles);
2715	} else {
2716		IWL_ERR(mvm, "no net detect match information available\n");
2717		n_matches = 0;
2718	}
2719
2720	net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
2721			     GFP_KERNEL);
2722	if (!net_detect || !n_matches)
2723		goto out_report_nd;
2724
2725	for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
2726		struct cfg80211_wowlan_nd_match *match;
2727		int idx, n_channels = 0;
2728
2729		n_channels = iwl_mvm_query_num_match_chans(mvm,
2730							   d3_data->nd_results,
2731							   i);
2732
2733		match = kzalloc(struct_size(match, channels, n_channels),
2734				GFP_KERNEL);
2735		if (!match)
2736			goto out_report_nd;
2737
2738		net_detect->matches[net_detect->n_matches++] = match;
2739
2740		/* We inverted the order of the SSIDs in the scan
2741		 * request, so invert the index here.
2742		 */
2743		idx = mvm->n_nd_match_sets - i - 1;
2744		match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2745		memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2746		       match->ssid.ssid_len);
2747
2748		if (mvm->n_nd_channels < n_channels)
2749			continue;
2750
2751		iwl_mvm_query_set_freqs(mvm, d3_data->nd_results, match, i);
2752	}
2753
2754out_report_nd:
2755	wakeup.net_detect = net_detect;
2756out:
2757	iwl_mvm_free_nd(mvm);
2758
2759	mutex_unlock(&mvm->mutex);
2760	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2761
2762	if (net_detect) {
2763		for (i = 0; i < net_detect->n_matches; i++)
2764			kfree(net_detect->matches[i]);
2765		kfree(net_detect);
2766	}
2767}
2768
2769static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2770				       struct ieee80211_vif *vif)
2771{
2772	/* skip the one we keep connection on */
2773	if (data == vif)
2774		return;
2775
2776	if (vif->type == NL80211_IFTYPE_STATION)
2777		ieee80211_resume_disconnect(vif);
2778}
2779
2780static bool iwl_mvm_rt_status(struct iwl_trans *trans, u32 base, u32 *err_id)
2781{
2782	struct error_table_start {
2783		/* cf. struct iwl_error_event_table */
2784		u32 valid;
2785		__le32 err_id;
2786	} err_info;
2787
2788	if (!base)
2789		return false;
2790
2791	iwl_trans_read_mem_bytes(trans, base,
2792				 &err_info, sizeof(err_info));
2793	if (err_info.valid && err_id)
2794		*err_id = le32_to_cpu(err_info.err_id);
2795
2796	return !!err_info.valid;
2797}
2798
2799static bool iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
2800				   struct ieee80211_vif *vif)
2801{
2802	u32 err_id;
2803
2804	/* check for lmac1 error */
2805	if (iwl_mvm_rt_status(mvm->trans,
2806			      mvm->trans->dbg.lmac_error_event_table[0],
2807			      &err_id)) {
2808		if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
2809			struct cfg80211_wowlan_wakeup wakeup = {
2810				.rfkill_release = true,
2811			};
2812			ieee80211_report_wowlan_wakeup(vif, &wakeup,
2813						       GFP_KERNEL);
2814		}
2815		return true;
2816	}
2817
2818	/* check if we have lmac2 set and check for error */
2819	if (iwl_mvm_rt_status(mvm->trans,
2820			      mvm->trans->dbg.lmac_error_event_table[1], NULL))
2821		return true;
2822
2823	/* check for umac error */
2824	if (iwl_mvm_rt_status(mvm->trans,
2825			      mvm->trans->dbg.umac_error_event_table, NULL))
2826		return true;
2827
2828	return false;
2829}
2830
2831/*
2832 * This function assumes:
2833 *	1. The mutex is already held.
2834 *	2. The callee functions unlock the mutex.
2835 */
2836static bool
2837iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm *mvm,
2838				    struct ieee80211_vif *vif,
2839				    struct iwl_d3_data *d3_data)
2840{
2841	lockdep_assert_held(&mvm->mutex);
2842
2843	/* if FW uses status notification, status shouldn't be NULL here */
2844	if (!d3_data->status) {
2845		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2846		u8 sta_id = mvm->net_detect ? IWL_MVM_INVALID_STA :
2847					      mvmvif->deflink.ap_sta_id;
2848
2849		d3_data->status = iwl_mvm_send_wowlan_get_status(mvm, sta_id);
2850	}
2851
2852	if (mvm->net_detect) {
2853		iwl_mvm_query_netdetect_reasons(mvm, vif, d3_data);
2854	} else {
2855		bool keep = iwl_mvm_query_wakeup_reasons(mvm, vif,
2856							 d3_data->status);
2857
2858#ifdef CONFIG_IWLWIFI_DEBUGFS
2859		if (keep)
2860			mvm->keep_vif = vif;
2861#endif
2862
2863		return keep;
2864	}
2865	return false;
2866}
2867
2868#define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT (IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \
2869						 IWL_WOWLAN_WAKEUP_BY_PATTERN | \
2870						 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\
2871						 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\
2872						 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\
2873						 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD)
2874
2875static int iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm *mvm,
2876					 struct iwl_wowlan_wake_pkt_notif *notif,
2877					 struct iwl_wowlan_status_data *status,
2878					 u32 len)
2879{
2880	u32 data_size, packet_len = le32_to_cpu(notif->wake_packet_length);
2881
2882	if (len < sizeof(*notif)) {
2883		IWL_ERR(mvm, "Invalid WoWLAN wake packet notification!\n");
2884		return -EIO;
2885	}
2886
2887	if (WARN_ON(!status)) {
2888		IWL_ERR(mvm, "Got wake packet notification but wowlan status data is NULL\n");
2889		return -EIO;
2890	}
2891
2892	if (WARN_ON(!(status->wakeup_reasons &
2893		      IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT))) {
2894		IWL_ERR(mvm, "Got wakeup packet but wakeup reason is %x\n",
2895			status->wakeup_reasons);
2896		return -EIO;
2897	}
2898
2899	data_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif, wake_packet);
2900
2901	/* data_size got the padding from the notification, remove it. */
2902	if (packet_len < data_size)
2903		data_size = packet_len;
2904
2905	status->wake_packet = kmemdup(notif->wake_packet, data_size,
2906				      GFP_ATOMIC);
2907
2908	if (!status->wake_packet)
2909		return -ENOMEM;
2910
2911	status->wake_packet_length = packet_len;
2912	status->wake_packet_bufsize = data_size;
2913
2914	return 0;
2915}
2916
2917static void iwl_mvm_nd_match_info_handler(struct iwl_mvm *mvm,
2918					  struct iwl_d3_data *d3_data,
2919					  struct iwl_scan_offload_match_info *notif,
2920					  u32 len)
2921{
2922	struct iwl_wowlan_status_data *status = d3_data->status;
2923	struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
2924	struct iwl_mvm_nd_results *results = d3_data->nd_results;
2925	size_t i, matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2926		iwl_umac_scan_get_max_profiles(mvm->fw);
2927
2928	if (IS_ERR_OR_NULL(vif))
2929		return;
2930
2931	if (len < sizeof(struct iwl_scan_offload_match_info)) {
2932		IWL_ERR(mvm, "Invalid scan match info notification\n");
2933		return;
2934	}
2935
2936	if (!mvm->net_detect) {
2937		IWL_ERR(mvm, "Unexpected scan match info notification\n");
2938		return;
2939	}
2940
2941	if (!status || status->wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
2942		IWL_ERR(mvm,
2943			"Ignore scan match info notification: no reason\n");
2944		return;
2945	}
2946
2947#ifdef CONFIG_IWLWIFI_DEBUGFS
2948	mvm->last_netdetect_scans = le32_to_cpu(notif->n_scans_done);
2949#endif
2950
2951	results->matched_profiles = le32_to_cpu(notif->matched_profiles);
2952	IWL_INFO(mvm, "number of matched profiles=%u\n",
2953		 results->matched_profiles);
2954
2955	if (results->matched_profiles) {
2956		memcpy(results->matches, notif->matches, matches_len);
2957		d3_data->nd_results_valid = TRUE;
2958	}
2959
2960	/* no scan should be active at this point */
2961	mvm->scan_status = 0;
2962	for (i = 0; i < mvm->max_scans; i++)
2963		mvm->scan_uid_status[i] = 0;
2964}
2965
2966static bool iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data *notif_wait,
2967				  struct iwl_rx_packet *pkt, void *data)
2968{
2969	struct iwl_mvm *mvm =
2970		container_of(notif_wait, struct iwl_mvm, notif_wait);
2971	struct iwl_d3_data *d3_data = data;
2972	u32 len = iwl_rx_packet_payload_len(pkt);
2973	int ret;
2974	int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
2975						      PROT_OFFLOAD_GROUP,
2976						      WOWLAN_INFO_NOTIFICATION,
2977						      IWL_FW_CMD_VER_UNKNOWN);
2978
2979
2980	switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
2981	case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): {
 
2982
2983		if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_INFO) {
2984			/* We might get two notifications due to dual bss */
2985			IWL_DEBUG_WOWLAN(mvm,
2986					 "Got additional wowlan info notification\n");
2987			break;
2988		}
2989
2990		if (wowlan_info_ver < 2) {
2991			struct iwl_wowlan_info_notif_v1 *notif_v1 =
2992				(void *)pkt->data;
2993			struct iwl_wowlan_info_notif_v2 *notif_v2;
2994
2995			notif_v2 = kmemdup(notif_v1, sizeof(*notif_v2), GFP_ATOMIC);
2996
2997			if (!notif_v2)
2998				return false;
2999
3000			notif_v2->tid_tear_down = notif_v1->tid_tear_down;
3001			notif_v2->station_id = notif_v1->station_id;
3002			memset_after(notif_v2, 0, station_id);
3003			iwl_mvm_parse_wowlan_info_notif_v2(mvm, notif_v2,
3004							   d3_data->status,
3005							   len);
3006			kfree(notif_v2);
3007
3008		} else if (wowlan_info_ver == 2) {
3009			struct iwl_wowlan_info_notif_v2 *notif_v2 =
3010				(void *)pkt->data;
3011
3012			iwl_mvm_parse_wowlan_info_notif_v2(mvm, notif_v2,
3013							   d3_data->status,
3014							   len);
3015		} else {
3016			struct iwl_wowlan_info_notif *notif =
3017				(void *)pkt->data;
3018
3019			iwl_mvm_parse_wowlan_info_notif(mvm, notif,
3020							d3_data->status, len);
3021		}
3022
3023		d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_INFO;
3024
 
 
3025		if (d3_data->status &&
3026		    d3_data->status->wakeup_reasons & IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT)
3027			/* We are supposed to get also wake packet notif */
3028			d3_data->notif_expected |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3029
3030		break;
3031	}
3032	case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): {
3033		struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data;
3034
3035		if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_WAKE_PKT) {
3036			/* We shouldn't get two wake packet notifications */
3037			IWL_ERR(mvm,
3038				"Got additional wowlan wake packet notification\n");
3039		} else {
3040			d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3041			len =  iwl_rx_packet_payload_len(pkt);
3042			ret = iwl_mvm_wowlan_store_wake_pkt(mvm, notif,
3043							    d3_data->status,
3044							    len);
3045			if (ret)
3046				IWL_ERR(mvm,
3047					"Can't parse WOWLAN_WAKE_PKT_NOTIFICATION\n");
3048		}
3049
3050		break;
3051	}
3052	case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): {
3053		struct iwl_scan_offload_match_info *notif = (void *)pkt->data;
3054
3055		if (d3_data->notif_received & IWL_D3_ND_MATCH_INFO) {
3056			IWL_ERR(mvm,
3057				"Got additional netdetect match info\n");
3058			break;
3059		}
3060
3061		d3_data->notif_received |= IWL_D3_ND_MATCH_INFO;
3062
3063		/* explicitly set this in the 'expected' as well */
3064		d3_data->notif_expected |= IWL_D3_ND_MATCH_INFO;
3065
3066		len = iwl_rx_packet_payload_len(pkt);
3067		iwl_mvm_nd_match_info_handler(mvm, d3_data, notif, len);
3068		break;
3069	}
3070	case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): {
3071		struct iwl_mvm_d3_end_notif *notif = (void *)pkt->data;
3072
3073		d3_data->d3_end_flags = __le32_to_cpu(notif->flags);
3074		d3_data->notif_received |= IWL_D3_NOTIF_D3_END_NOTIF;
3075
3076		break;
3077	}
3078	default:
3079		WARN_ON(1);
3080	}
3081
3082	return d3_data->notif_received == d3_data->notif_expected;
3083}
3084
3085static int iwl_mvm_resume_firmware(struct iwl_mvm *mvm, bool test)
3086{
3087	int ret;
3088	enum iwl_d3_status d3_status;
3089	struct iwl_host_cmd cmd = {
3090			.id = D0I3_END_CMD,
3091			.flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
3092		};
3093	bool reset = fw_has_capa(&mvm->fw->ucode_capa,
3094				 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3095
3096	ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !reset);
3097	if (ret)
3098		return ret;
3099
3100	if (d3_status != IWL_D3_STATUS_ALIVE) {
3101		IWL_INFO(mvm, "Device was reset during suspend\n");
3102		return -ENOENT;
3103	}
3104
3105	/*
3106	 * We should trigger resume flow using command only for 22000 family
3107	 * AX210 and above don't need the command since they have
3108	 * the doorbell interrupt.
3109	 */
3110	if (mvm->trans->trans_cfg->device_family <= IWL_DEVICE_FAMILY_22000 &&
3111	    fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_D0I3_END_FIRST)) {
3112		ret = iwl_mvm_send_cmd(mvm, &cmd);
3113		if (ret < 0)
3114			IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
3115				ret);
3116	}
3117
3118	return ret;
3119}
3120
3121#define IWL_MVM_D3_NOTIF_TIMEOUT (HZ / 5)
3122
3123static int iwl_mvm_d3_notif_wait(struct iwl_mvm *mvm,
3124				 struct iwl_d3_data *d3_data)
3125{
3126	static const u16 d3_resume_notif[] = {
3127		WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION),
3128		WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION),
3129		WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF),
3130		WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3131	};
3132	struct iwl_notification_wait wait_d3_notif;
3133	int ret;
3134
3135	iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3136				   d3_resume_notif, ARRAY_SIZE(d3_resume_notif),
3137				   iwl_mvm_wait_d3_notif, d3_data);
3138
3139	ret = iwl_mvm_resume_firmware(mvm, d3_data->test);
3140	if (ret) {
3141		iwl_remove_notification(&mvm->notif_wait, &wait_d3_notif);
3142		return ret;
3143	}
3144
3145	return iwl_wait_notification(&mvm->notif_wait, &wait_d3_notif,
3146				     IWL_MVM_D3_NOTIF_TIMEOUT);
3147}
3148
3149static inline bool iwl_mvm_d3_resume_notif_based(struct iwl_mvm *mvm)
3150{
3151	return iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3152				       WOWLAN_INFO_NOTIFICATION, 0) &&
3153		iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3154					WOWLAN_WAKE_PKT_NOTIFICATION, 0) &&
3155		iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3156					D3_END_NOTIFICATION, 0);
3157}
3158
3159static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
3160{
3161	struct ieee80211_vif *vif = NULL;
3162	int ret = 1;
3163	struct iwl_mvm_nd_results results = {};
3164	struct iwl_d3_data d3_data = {
3165		.test = test,
3166		.notif_expected =
3167			IWL_D3_NOTIF_WOWLAN_INFO |
3168			IWL_D3_NOTIF_D3_END_NOTIF,
3169		.nd_results_valid = false,
3170		.nd_results = &results,
3171	};
3172	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3173					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3174	bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
3175				      IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
3176	bool resume_notif_based = iwl_mvm_d3_resume_notif_based(mvm);
3177	bool keep = false;
3178
3179	mutex_lock(&mvm->mutex);
3180
3181	mvm->last_reset_or_resume_time_jiffies = jiffies;
3182
3183	/* get the BSS vif pointer again */
3184	vif = iwl_mvm_get_bss_vif(mvm);
3185	if (IS_ERR_OR_NULL(vif))
3186		goto err;
3187
3188	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3189
3190	if (iwl_mvm_check_rt_status(mvm, vif)) {
3191		set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3192		iwl_mvm_dump_nic_error_log(mvm);
3193		iwl_dbg_tlv_time_point(&mvm->fwrt,
3194				       IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL);
3195		iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
3196					false, 0);
3197		ret = 1;
3198		goto err;
3199	}
3200
3201	if (resume_notif_based) {
3202		d3_data.status = kzalloc(sizeof(*d3_data.status), GFP_KERNEL);
3203		if (!d3_data.status) {
3204			IWL_ERR(mvm, "Failed to allocate wowlan status\n");
3205			ret = -ENOMEM;
3206			goto err;
3207		}
3208
3209		ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3210		if (ret)
3211			goto err;
3212	} else {
3213		ret = iwl_mvm_resume_firmware(mvm, test);
3214		if (ret < 0)
3215			goto err;
3216	}
3217
3218	/* after the successful handshake, we're out of D3 */
3219	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3220
3221	/* when reset is required we can't send these following commands */
3222	if (d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)
3223		goto query_wakeup_reasons;
3224
3225	/*
3226	 * Query the current location and source from the D3 firmware so we
3227	 * can play it back when we re-intiailize the D0 firmware
3228	 */
3229	iwl_mvm_update_changed_regdom(mvm);
3230
3231	/* Re-configure PPAG settings */
3232	iwl_mvm_ppag_send_cmd(mvm);
3233
3234	if (!unified_image)
3235		/*  Re-configure default SAR profile */
3236		iwl_mvm_sar_select_profile(mvm, 1, 1);
3237
3238	if (mvm->net_detect && unified_image) {
3239		/* If this is a non-unified image, we restart the FW,
3240		 * so no need to stop the netdetect scan.  If that
3241		 * fails, continue and try to get the wake-up reasons,
3242		 * but trigger a HW restart by keeping a failure code
3243		 * in ret.
3244		 */
3245		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
3246					false);
3247	}
3248
3249query_wakeup_reasons:
3250	keep = iwl_mvm_choose_query_wakeup_reasons(mvm, vif, &d3_data);
3251	/* has unlocked the mutex, so skip that */
3252	goto out;
3253
3254err:
3255	mutex_unlock(&mvm->mutex);
3256out:
3257	if (d3_data.status)
3258		kfree(d3_data.status->wake_packet);
3259	kfree(d3_data.status);
3260	iwl_mvm_free_nd(mvm);
3261
3262	if (!d3_data.test && !mvm->net_detect)
3263		ieee80211_iterate_active_interfaces_mtx(mvm->hw,
3264							IEEE80211_IFACE_ITER_NORMAL,
3265							iwl_mvm_d3_disconnect_iter,
3266							keep ? vif : NULL);
3267
3268	clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3269
3270	/* no need to reset the device in unified images, if successful */
3271	if (unified_image && !ret) {
3272		/* nothing else to do if we already sent D0I3_END_CMD */
3273		if (d0i3_first)
3274			return 0;
3275
3276		if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3277					     D3_END_NOTIFICATION, 0)) {
3278			ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
3279			if (!ret)
3280				return 0;
3281		} else if (!(d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)) {
3282			return 0;
3283		}
3284	}
3285
3286	/*
3287	 * Reconfigure the device in one of the following cases:
3288	 * 1. We are not using a unified image
3289	 * 2. We are using a unified image but had an error while exiting D3
3290	 */
3291	set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
3292
3293	/* regardless of what happened, we're now out of D3 */
3294	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3295
3296	return 1;
3297}
3298
3299int iwl_mvm_resume(struct ieee80211_hw *hw)
3300{
3301	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3302	int ret;
3303
3304	ret = __iwl_mvm_resume(mvm, false);
3305
3306	iwl_mvm_resume_tcm(mvm);
3307
3308	iwl_fw_runtime_resume(&mvm->fwrt);
3309
3310	return ret;
3311}
3312
3313void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
3314{
3315	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3316
3317	device_set_wakeup_enable(mvm->trans->dev, enabled);
3318}
3319
3320#ifdef CONFIG_IWLWIFI_DEBUGFS
3321static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
3322{
3323	struct iwl_mvm *mvm = inode->i_private;
3324	int err;
3325
3326	if (mvm->d3_test_active)
3327		return -EBUSY;
3328
3329	file->private_data = inode->i_private;
3330
3331	iwl_mvm_pause_tcm(mvm, true);
3332
3333	iwl_fw_runtime_suspend(&mvm->fwrt);
3334
3335	/* start pseudo D3 */
3336	rtnl_lock();
3337	wiphy_lock(mvm->hw->wiphy);
3338	err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
3339	wiphy_unlock(mvm->hw->wiphy);
3340	rtnl_unlock();
3341	if (err > 0)
3342		err = -EINVAL;
3343	if (err)
3344		return err;
3345
3346	mvm->d3_test_active = true;
3347	mvm->keep_vif = NULL;
3348	return 0;
3349}
3350
3351static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
3352				    size_t count, loff_t *ppos)
3353{
3354	struct iwl_mvm *mvm = file->private_data;
3355	u32 pme_asserted;
3356
3357	while (true) {
3358		/* read pme_ptr if available */
3359		if (mvm->d3_test_pme_ptr) {
3360			pme_asserted = iwl_trans_read_mem32(mvm->trans,
3361						mvm->d3_test_pme_ptr);
3362			if (pme_asserted)
3363				break;
3364		}
3365
3366		if (msleep_interruptible(100))
3367			break;
3368	}
3369
3370	return 0;
3371}
3372
3373static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
3374					      struct ieee80211_vif *vif)
3375{
3376	/* skip the one we keep connection on */
3377	if (_data == vif)
3378		return;
3379
3380	if (vif->type == NL80211_IFTYPE_STATION)
3381		ieee80211_connection_loss(vif);
3382}
3383
3384static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
3385{
3386	struct iwl_mvm *mvm = inode->i_private;
3387	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3388					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3389
3390	mvm->d3_test_active = false;
3391
3392	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3393
3394	rtnl_lock();
3395	wiphy_lock(mvm->hw->wiphy);
3396	__iwl_mvm_resume(mvm, true);
3397	wiphy_unlock(mvm->hw->wiphy);
3398	rtnl_unlock();
3399
3400	iwl_mvm_resume_tcm(mvm);
3401
3402	iwl_fw_runtime_resume(&mvm->fwrt);
3403
3404	iwl_abort_notification_waits(&mvm->notif_wait);
3405	if (!unified_image) {
3406		int remaining_time = 10;
3407
3408		ieee80211_restart_hw(mvm->hw);
3409
3410		/* wait for restart and disconnect all interfaces */
3411		while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3412		       remaining_time > 0) {
3413			remaining_time--;
3414			msleep(1000);
3415		}
3416
3417		if (remaining_time == 0)
3418			IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
3419	}
3420
3421	ieee80211_iterate_active_interfaces_atomic(
3422		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
3423		iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
3424
3425	return 0;
3426}
3427
3428const struct file_operations iwl_dbgfs_d3_test_ops = {
3429	.llseek = no_llseek,
3430	.open = iwl_mvm_d3_test_open,
3431	.read = iwl_mvm_d3_test_read,
3432	.release = iwl_mvm_d3_test_release,
3433};
3434#endif
v6.2
   1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
   2/*
   3 * Copyright (C) 2012-2014, 2018-2022 Intel Corporation
   4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
   5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
   6 */
   7#include <linux/etherdevice.h>
   8#include <linux/ip.h>
   9#include <linux/fs.h>
  10#include <net/cfg80211.h>
  11#include <net/ipv6.h>
  12#include <net/tcp.h>
  13#include <net/addrconf.h>
  14#include "iwl-modparams.h"
  15#include "fw-api.h"
  16#include "mvm.h"
  17#include "fw/img.h"
  18
  19void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
  20			    struct ieee80211_vif *vif,
  21			    struct cfg80211_gtk_rekey_data *data)
  22{
  23	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  24	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  25
  26	mutex_lock(&mvm->mutex);
  27
  28	mvmvif->rekey_data.kek_len = data->kek_len;
  29	mvmvif->rekey_data.kck_len = data->kck_len;
  30	memcpy(mvmvif->rekey_data.kek, data->kek, data->kek_len);
  31	memcpy(mvmvif->rekey_data.kck, data->kck, data->kck_len);
  32	mvmvif->rekey_data.akm = data->akm & 0xFF;
  33	mvmvif->rekey_data.replay_ctr =
  34		cpu_to_le64(be64_to_cpup((const __be64 *)data->replay_ctr));
  35	mvmvif->rekey_data.valid = true;
  36
  37	mutex_unlock(&mvm->mutex);
  38}
  39
  40#if IS_ENABLED(CONFIG_IPV6)
  41void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
  42			      struct ieee80211_vif *vif,
  43			      struct inet6_dev *idev)
  44{
  45	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  46	struct inet6_ifaddr *ifa;
  47	int idx = 0;
  48
  49	memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
  50
  51	read_lock_bh(&idev->lock);
  52	list_for_each_entry(ifa, &idev->addr_list, if_list) {
  53		mvmvif->target_ipv6_addrs[idx] = ifa->addr;
  54		if (ifa->flags & IFA_F_TENTATIVE)
  55			__set_bit(idx, mvmvif->tentative_addrs);
  56		idx++;
  57		if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
  58			break;
  59	}
  60	read_unlock_bh(&idev->lock);
  61
  62	mvmvif->num_target_ipv6_addrs = idx;
  63}
  64#endif
  65
  66void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
  67				     struct ieee80211_vif *vif, int idx)
  68{
  69	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  70
  71	mvmvif->tx_key_idx = idx;
  72}
  73
  74static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
  75{
  76	int i;
  77
  78	for (i = 0; i < IWL_P1K_SIZE; i++)
  79		out[i] = cpu_to_le16(p1k[i]);
  80}
  81
  82static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
  83				     struct iwl_mvm_key_pn *ptk_pn,
  84				     struct ieee80211_key_seq *seq,
  85				     int tid, int queues)
  86{
  87	const u8 *ret = seq->ccmp.pn;
  88	int i;
  89
  90	/* get the PN from mac80211, used on the default queue */
  91	ieee80211_get_key_rx_seq(key, tid, seq);
  92
  93	/* and use the internal data for the other queues */
  94	for (i = 1; i < queues; i++) {
  95		const u8 *tmp = ptk_pn->q[i].pn[tid];
  96
  97		if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
  98			ret = tmp;
  99	}
 100
 101	return ret;
 102}
 103
 104struct wowlan_key_reprogram_data {
 105	bool error;
 106	int wep_key_idx;
 107};
 108
 109static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
 110					struct ieee80211_vif *vif,
 111					struct ieee80211_sta *sta,
 112					struct ieee80211_key_conf *key,
 113					void *_data)
 114{
 115	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 116	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 117	struct wowlan_key_reprogram_data *data = _data;
 118	int ret;
 119
 120	switch (key->cipher) {
 121	case WLAN_CIPHER_SUITE_WEP40:
 122	case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
 123		struct {
 124			struct iwl_mvm_wep_key_cmd wep_key_cmd;
 125			struct iwl_mvm_wep_key wep_key;
 126		} __packed wkc = {
 127			.wep_key_cmd.mac_id_n_color =
 128				cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 129								mvmvif->color)),
 130			.wep_key_cmd.num_keys = 1,
 131			/* firmware sets STA_KEY_FLG_WEP_13BYTES */
 132			.wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
 133			.wep_key.key_index = key->keyidx,
 134			.wep_key.key_size = key->keylen,
 135		};
 136
 137		/*
 138		 * This will fail -- the key functions don't set support
 139		 * pairwise WEP keys. However, that's better than silently
 140		 * failing WoWLAN. Or maybe not?
 141		 */
 142		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
 143			break;
 144
 145		memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
 146		if (key->keyidx == mvmvif->tx_key_idx) {
 147			/* TX key must be at offset 0 */
 148			wkc.wep_key.key_offset = 0;
 149		} else {
 150			/* others start at 1 */
 151			data->wep_key_idx++;
 152			wkc.wep_key.key_offset = data->wep_key_idx;
 153		}
 154
 155		mutex_lock(&mvm->mutex);
 156		ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
 157		data->error = ret != 0;
 158
 159		mvm->ptk_ivlen = key->iv_len;
 160		mvm->ptk_icvlen = key->icv_len;
 161		mvm->gtk_ivlen = key->iv_len;
 162		mvm->gtk_icvlen = key->icv_len;
 163		mutex_unlock(&mvm->mutex);
 164
 165		/* don't upload key again */
 166		return;
 167	}
 168	default:
 169		data->error = true;
 170		return;
 171	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
 172	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 173		return;
 174	case WLAN_CIPHER_SUITE_AES_CMAC:
 175		/*
 176		 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
 177		 * but we also shouldn't abort suspend due to that. It does have
 178		 * support for the IGTK key renewal, but doesn't really use the
 179		 * IGTK for anything. This means we could spuriously wake up or
 180		 * be deauthenticated, but that was considered acceptable.
 181		 */
 182		return;
 183	case WLAN_CIPHER_SUITE_TKIP:
 184	case WLAN_CIPHER_SUITE_CCMP:
 185	case WLAN_CIPHER_SUITE_GCMP:
 186	case WLAN_CIPHER_SUITE_GCMP_256:
 187		break;
 188	}
 189
 190	mutex_lock(&mvm->mutex);
 191	/*
 192	 * The D3 firmware hardcodes the key offset 0 as the key it
 193	 * uses to transmit packets to the AP, i.e. the PTK.
 194	 */
 195	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
 196		mvm->ptk_ivlen = key->iv_len;
 197		mvm->ptk_icvlen = key->icv_len;
 198		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
 199	} else {
 200		/*
 201		 * firmware only supports TSC/RSC for a single key,
 202		 * so if there are multiple keep overwriting them
 203		 * with new ones -- this relies on mac80211 doing
 204		 * list_add_tail().
 205		 */
 206		mvm->gtk_ivlen = key->iv_len;
 207		mvm->gtk_icvlen = key->icv_len;
 208		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
 209	}
 210	mutex_unlock(&mvm->mutex);
 211	data->error = ret != 0;
 212}
 213
 214struct wowlan_key_rsc_tsc_data {
 215	struct iwl_wowlan_rsc_tsc_params_cmd_v4 *rsc_tsc;
 216	bool have_rsc_tsc;
 217};
 218
 219static void iwl_mvm_wowlan_get_rsc_tsc_data(struct ieee80211_hw *hw,
 220					    struct ieee80211_vif *vif,
 221					    struct ieee80211_sta *sta,
 222					    struct ieee80211_key_conf *key,
 223					    void *_data)
 224{
 225	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 226	struct wowlan_key_rsc_tsc_data *data = _data;
 227	struct aes_sc *aes_sc;
 228	struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
 229	struct ieee80211_key_seq seq;
 230	int i;
 231
 232	switch (key->cipher) {
 233	default:
 234		break;
 235	case WLAN_CIPHER_SUITE_TKIP:
 236		if (sta) {
 237			u64 pn64;
 238
 239			tkip_sc =
 240			   data->rsc_tsc->params.all_tsc_rsc.tkip.unicast_rsc;
 241			tkip_tx_sc =
 242				&data->rsc_tsc->params.all_tsc_rsc.tkip.tsc;
 243
 244			pn64 = atomic64_read(&key->tx_pn);
 245			tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
 246			tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
 247		} else {
 248			tkip_sc =
 249			  data->rsc_tsc->params.all_tsc_rsc.tkip.multicast_rsc;
 250		}
 251
 252		/*
 253		 * For non-QoS this relies on the fact that both the uCode and
 254		 * mac80211 use TID 0 (as they need to to avoid replay attacks)
 255		 * for checking the IV in the frames.
 256		 */
 257		for (i = 0; i < IWL_NUM_RSC; i++) {
 258			ieee80211_get_key_rx_seq(key, i, &seq);
 259			tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
 260			tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
 261		}
 262
 263		data->have_rsc_tsc = true;
 264		break;
 265	case WLAN_CIPHER_SUITE_CCMP:
 266	case WLAN_CIPHER_SUITE_GCMP:
 267	case WLAN_CIPHER_SUITE_GCMP_256:
 268		if (sta) {
 269			struct aes_sc *aes_tx_sc;
 270			u64 pn64;
 271
 272			aes_sc =
 273			   data->rsc_tsc->params.all_tsc_rsc.aes.unicast_rsc;
 274			aes_tx_sc =
 275				&data->rsc_tsc->params.all_tsc_rsc.aes.tsc;
 276
 277			pn64 = atomic64_read(&key->tx_pn);
 278			aes_tx_sc->pn = cpu_to_le64(pn64);
 279		} else {
 280			aes_sc =
 281			   data->rsc_tsc->params.all_tsc_rsc.aes.multicast_rsc;
 282		}
 283
 284		/*
 285		 * For non-QoS this relies on the fact that both the uCode and
 286		 * mac80211/our RX code use TID 0 for checking the PN.
 287		 */
 288		if (sta && iwl_mvm_has_new_rx_api(mvm)) {
 289			struct iwl_mvm_sta *mvmsta;
 290			struct iwl_mvm_key_pn *ptk_pn;
 291			const u8 *pn;
 292
 293			mvmsta = iwl_mvm_sta_from_mac80211(sta);
 294			rcu_read_lock();
 295			ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
 296			if (WARN_ON(!ptk_pn)) {
 297				rcu_read_unlock();
 298				break;
 299			}
 300
 301			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
 302				pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
 303						mvm->trans->num_rx_queues);
 304				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
 305							   ((u64)pn[4] << 8) |
 306							   ((u64)pn[3] << 16) |
 307							   ((u64)pn[2] << 24) |
 308							   ((u64)pn[1] << 32) |
 309							   ((u64)pn[0] << 40));
 310			}
 311
 312			rcu_read_unlock();
 313		} else {
 314			for (i = 0; i < IWL_NUM_RSC; i++) {
 315				u8 *pn = seq.ccmp.pn;
 316
 317				ieee80211_get_key_rx_seq(key, i, &seq);
 318				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
 319							   ((u64)pn[4] << 8) |
 320							   ((u64)pn[3] << 16) |
 321							   ((u64)pn[2] << 24) |
 322							   ((u64)pn[1] << 32) |
 323							   ((u64)pn[0] << 40));
 324			}
 325		}
 326		data->have_rsc_tsc = true;
 327		break;
 328	}
 329}
 330
 331struct wowlan_key_rsc_v5_data {
 332	struct iwl_wowlan_rsc_tsc_params_cmd *rsc;
 333	bool have_rsc;
 334	int gtks;
 335	int gtk_ids[4];
 336};
 337
 338static void iwl_mvm_wowlan_get_rsc_v5_data(struct ieee80211_hw *hw,
 339					   struct ieee80211_vif *vif,
 340					   struct ieee80211_sta *sta,
 341					   struct ieee80211_key_conf *key,
 342					   void *_data)
 343{
 344	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 345	struct wowlan_key_rsc_v5_data *data = _data;
 346	struct ieee80211_key_seq seq;
 347	__le64 *rsc;
 348	int i;
 349
 350	/* only for ciphers that can be PTK/GTK */
 351	switch (key->cipher) {
 352	default:
 353		return;
 354	case WLAN_CIPHER_SUITE_TKIP:
 355	case WLAN_CIPHER_SUITE_CCMP:
 356	case WLAN_CIPHER_SUITE_GCMP:
 357	case WLAN_CIPHER_SUITE_GCMP_256:
 358		break;
 359	}
 360
 361	if (sta) {
 362		rsc = data->rsc->ucast_rsc;
 363	} else {
 364		if (WARN_ON(data->gtks >= ARRAY_SIZE(data->gtk_ids)))
 365			return;
 366		data->gtk_ids[data->gtks] = key->keyidx;
 367		rsc = data->rsc->mcast_rsc[data->gtks % 2];
 368		if (WARN_ON(key->keyidx >=
 369				ARRAY_SIZE(data->rsc->mcast_key_id_map)))
 370			return;
 371		data->rsc->mcast_key_id_map[key->keyidx] = data->gtks % 2;
 372		if (data->gtks >= 2) {
 373			int prev = data->gtks - 2;
 374			int prev_idx = data->gtk_ids[prev];
 375
 376			data->rsc->mcast_key_id_map[prev_idx] =
 377				IWL_MCAST_KEY_MAP_INVALID;
 378		}
 379		data->gtks++;
 380	}
 381
 382	switch (key->cipher) {
 383	default:
 384		WARN_ON(1);
 385		break;
 386	case WLAN_CIPHER_SUITE_TKIP:
 387
 388		/*
 389		 * For non-QoS this relies on the fact that both the uCode and
 390		 * mac80211 use TID 0 (as they need to to avoid replay attacks)
 391		 * for checking the IV in the frames.
 392		 */
 393		for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
 394			ieee80211_get_key_rx_seq(key, i, &seq);
 395
 396			rsc[i] = cpu_to_le64(((u64)seq.tkip.iv32 << 16) |
 397					     seq.tkip.iv16);
 398		}
 399
 400		data->have_rsc = true;
 401		break;
 402	case WLAN_CIPHER_SUITE_CCMP:
 403	case WLAN_CIPHER_SUITE_GCMP:
 404	case WLAN_CIPHER_SUITE_GCMP_256:
 405		/*
 406		 * For non-QoS this relies on the fact that both the uCode and
 407		 * mac80211/our RX code use TID 0 for checking the PN.
 408		 */
 409		if (sta) {
 410			struct iwl_mvm_sta *mvmsta;
 411			struct iwl_mvm_key_pn *ptk_pn;
 412			const u8 *pn;
 413
 414			mvmsta = iwl_mvm_sta_from_mac80211(sta);
 415			rcu_read_lock();
 416			ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
 417			if (WARN_ON(!ptk_pn)) {
 418				rcu_read_unlock();
 419				break;
 420			}
 421
 422			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
 423				pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
 424						mvm->trans->num_rx_queues);
 425				rsc[i] = cpu_to_le64((u64)pn[5] |
 426						     ((u64)pn[4] << 8) |
 427						     ((u64)pn[3] << 16) |
 428						     ((u64)pn[2] << 24) |
 429						     ((u64)pn[1] << 32) |
 430						     ((u64)pn[0] << 40));
 431			}
 432
 433			rcu_read_unlock();
 434		} else {
 435			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
 436				u8 *pn = seq.ccmp.pn;
 437
 438				ieee80211_get_key_rx_seq(key, i, &seq);
 439				rsc[i] = cpu_to_le64((u64)pn[5] |
 440						     ((u64)pn[4] << 8) |
 441						     ((u64)pn[3] << 16) |
 442						     ((u64)pn[2] << 24) |
 443						     ((u64)pn[1] << 32) |
 444						     ((u64)pn[0] << 40));
 445			}
 446		}
 447		data->have_rsc = true;
 448		break;
 449	}
 450}
 451
 452static int iwl_mvm_wowlan_config_rsc_tsc(struct iwl_mvm *mvm,
 453					 struct ieee80211_vif *vif)
 454{
 455	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 456	int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TSC_RSC_PARAM,
 457					IWL_FW_CMD_VER_UNKNOWN);
 458	int ret;
 459
 460	if (ver == 5) {
 461		struct wowlan_key_rsc_v5_data data = {};
 462		int i;
 463
 464		data.rsc = kmalloc(sizeof(*data.rsc), GFP_KERNEL);
 465		if (!data.rsc)
 466			return -ENOMEM;
 467
 468		memset(data.rsc, 0xff, sizeof(*data.rsc));
 469
 470		for (i = 0; i < ARRAY_SIZE(data.rsc->mcast_key_id_map); i++)
 471			data.rsc->mcast_key_id_map[i] =
 472				IWL_MCAST_KEY_MAP_INVALID;
 473		data.rsc->sta_id = cpu_to_le32(mvmvif->ap_sta_id);
 474
 475		ieee80211_iter_keys(mvm->hw, vif,
 476				    iwl_mvm_wowlan_get_rsc_v5_data,
 477				    &data);
 478
 479		if (data.have_rsc)
 480			ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
 481						   CMD_ASYNC, sizeof(*data.rsc),
 482						   data.rsc);
 483		else
 484			ret = 0;
 485		kfree(data.rsc);
 486	} else if (ver == 4 || ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN) {
 487		struct wowlan_key_rsc_tsc_data data = {};
 488		int size;
 489
 490		data.rsc_tsc = kzalloc(sizeof(*data.rsc_tsc), GFP_KERNEL);
 491		if (!data.rsc_tsc)
 492			return -ENOMEM;
 493
 494		if (ver == 4) {
 495			size = sizeof(*data.rsc_tsc);
 496			data.rsc_tsc->sta_id = cpu_to_le32(mvmvif->ap_sta_id);
 
 497		} else {
 498			/* ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN */
 499			size = sizeof(data.rsc_tsc->params);
 500		}
 501
 502		ieee80211_iter_keys(mvm->hw, vif,
 503				    iwl_mvm_wowlan_get_rsc_tsc_data,
 504				    &data);
 505
 506		if (data.have_rsc_tsc)
 507			ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
 508						   CMD_ASYNC, size,
 509						   data.rsc_tsc);
 510		else
 511			ret = 0;
 512		kfree(data.rsc_tsc);
 513	} else {
 514		ret = 0;
 515		WARN_ON_ONCE(1);
 516	}
 517
 518	return ret;
 519}
 520
 521struct wowlan_key_tkip_data {
 522	struct iwl_wowlan_tkip_params_cmd tkip;
 523	bool have_tkip_keys;
 524};
 525
 526static void iwl_mvm_wowlan_get_tkip_data(struct ieee80211_hw *hw,
 527					 struct ieee80211_vif *vif,
 528					 struct ieee80211_sta *sta,
 529					 struct ieee80211_key_conf *key,
 530					 void *_data)
 531{
 532	struct wowlan_key_tkip_data *data = _data;
 533	struct iwl_p1k_cache *rx_p1ks;
 534	u8 *rx_mic_key;
 535	struct ieee80211_key_seq seq;
 536	u32 cur_rx_iv32 = 0;
 537	u16 p1k[IWL_P1K_SIZE];
 538	int i;
 539
 540	switch (key->cipher) {
 541	default:
 542		break;
 543	case WLAN_CIPHER_SUITE_TKIP:
 544		if (sta) {
 545			u64 pn64;
 546
 547			rx_p1ks = data->tkip.rx_uni;
 548
 549			pn64 = atomic64_read(&key->tx_pn);
 550
 551			ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
 552						  p1k);
 553			iwl_mvm_convert_p1k(p1k, data->tkip.tx.p1k);
 554
 555			memcpy(data->tkip.mic_keys.tx,
 556			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
 557			       IWL_MIC_KEY_SIZE);
 558
 559			rx_mic_key = data->tkip.mic_keys.rx_unicast;
 560		} else {
 561			rx_p1ks = data->tkip.rx_multi;
 562			rx_mic_key = data->tkip.mic_keys.rx_mcast;
 563		}
 564
 565		for (i = 0; i < IWL_NUM_RSC; i++) {
 
 566			/* wrapping isn't allowed, AP must rekey */
 567			if (seq.tkip.iv32 > cur_rx_iv32)
 568				cur_rx_iv32 = seq.tkip.iv32;
 569		}
 570
 571		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
 572					  cur_rx_iv32, p1k);
 573		iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
 574		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
 575					  cur_rx_iv32 + 1, p1k);
 576		iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
 577
 578		memcpy(rx_mic_key,
 579		       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
 580		       IWL_MIC_KEY_SIZE);
 581
 582		data->have_tkip_keys = true;
 583		break;
 584	}
 585}
 586
 587struct wowlan_key_gtk_type_iter {
 588	struct iwl_wowlan_kek_kck_material_cmd_v4 *kek_kck_cmd;
 589};
 590
 591static void iwl_mvm_wowlan_gtk_type_iter(struct ieee80211_hw *hw,
 592					 struct ieee80211_vif *vif,
 593					 struct ieee80211_sta *sta,
 594					 struct ieee80211_key_conf *key,
 595					 void *_data)
 596{
 597	struct wowlan_key_gtk_type_iter *data = _data;
 598
 599	switch (key->cipher) {
 600	default:
 601		return;
 602	case WLAN_CIPHER_SUITE_TKIP:
 603		if (!sta)
 604			data->kek_kck_cmd->gtk_cipher =
 605				cpu_to_le32(STA_KEY_FLG_TKIP);
 606		return;
 607	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
 608	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 609		data->kek_kck_cmd->igtk_cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
 610		return;
 611	case WLAN_CIPHER_SUITE_AES_CMAC:
 612		data->kek_kck_cmd->igtk_cipher = cpu_to_le32(STA_KEY_FLG_CCM);
 613		return;
 614	case WLAN_CIPHER_SUITE_CCMP:
 615		if (!sta)
 616			data->kek_kck_cmd->gtk_cipher =
 617				cpu_to_le32(STA_KEY_FLG_CCM);
 618		return;
 619	case WLAN_CIPHER_SUITE_GCMP:
 620	case WLAN_CIPHER_SUITE_GCMP_256:
 621		if (!sta)
 622			data->kek_kck_cmd->gtk_cipher =
 623				cpu_to_le32(STA_KEY_FLG_GCMP);
 624		return;
 625	}
 626}
 627
 628static int iwl_mvm_send_patterns_v1(struct iwl_mvm *mvm,
 629				    struct cfg80211_wowlan *wowlan)
 630{
 631	struct iwl_wowlan_patterns_cmd_v1 *pattern_cmd;
 632	struct iwl_host_cmd cmd = {
 633		.id = WOWLAN_PATTERNS,
 634		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
 635	};
 636	int i, err;
 637
 638	if (!wowlan->n_patterns)
 639		return 0;
 640
 641	cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns);
 642
 643	pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
 644	if (!pattern_cmd)
 645		return -ENOMEM;
 646
 647	pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
 648
 649	for (i = 0; i < wowlan->n_patterns; i++) {
 650		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
 651
 652		memcpy(&pattern_cmd->patterns[i].mask,
 653		       wowlan->patterns[i].mask, mask_len);
 654		memcpy(&pattern_cmd->patterns[i].pattern,
 655		       wowlan->patterns[i].pattern,
 656		       wowlan->patterns[i].pattern_len);
 657		pattern_cmd->patterns[i].mask_size = mask_len;
 658		pattern_cmd->patterns[i].pattern_size =
 659			wowlan->patterns[i].pattern_len;
 660	}
 661
 662	cmd.data[0] = pattern_cmd;
 663	err = iwl_mvm_send_cmd(mvm, &cmd);
 664	kfree(pattern_cmd);
 665	return err;
 666}
 667
 668static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
 669				 struct ieee80211_vif *vif,
 670				 struct cfg80211_wowlan *wowlan)
 671{
 672	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 673	struct iwl_wowlan_patterns_cmd *pattern_cmd;
 674	struct iwl_host_cmd cmd = {
 675		.id = WOWLAN_PATTERNS,
 676		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
 677	};
 678	int i, err;
 679	int ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
 680					IWL_FW_CMD_VER_UNKNOWN);
 681
 682	if (!wowlan->n_patterns)
 683		return 0;
 684
 685	cmd.len[0] = sizeof(*pattern_cmd) +
 686		wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v2);
 687
 688	pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL);
 689	if (!pattern_cmd)
 690		return -ENOMEM;
 691
 692	pattern_cmd->n_patterns = wowlan->n_patterns;
 693	if (ver >= 3)
 694		pattern_cmd->sta_id = mvmvif->ap_sta_id;
 695
 696	for (i = 0; i < wowlan->n_patterns; i++) {
 697		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
 698
 699		pattern_cmd->patterns[i].pattern_type =
 700			WOWLAN_PATTERN_TYPE_BITMASK;
 701
 702		memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
 703		       wowlan->patterns[i].mask, mask_len);
 704		memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
 705		       wowlan->patterns[i].pattern,
 706		       wowlan->patterns[i].pattern_len);
 707		pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
 708		pattern_cmd->patterns[i].u.bitmask.pattern_size =
 709			wowlan->patterns[i].pattern_len;
 710	}
 711
 712	cmd.data[0] = pattern_cmd;
 713	err = iwl_mvm_send_cmd(mvm, &cmd);
 714	kfree(pattern_cmd);
 715	return err;
 716}
 717
 718static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 719				struct ieee80211_sta *ap_sta)
 720{
 721	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 722	struct ieee80211_chanctx_conf *ctx;
 723	u8 chains_static, chains_dynamic;
 724	struct cfg80211_chan_def chandef;
 725	int ret, i;
 726	struct iwl_binding_cmd_v1 binding_cmd = {};
 727	struct iwl_time_quota_cmd quota_cmd = {};
 728	struct iwl_time_quota_data *quota;
 729	u32 status;
 730
 731	if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm)))
 732		return -EINVAL;
 733
 734	/* add back the PHY */
 735	if (WARN_ON(!mvmvif->phy_ctxt))
 736		return -EINVAL;
 737
 738	rcu_read_lock();
 739	ctx = rcu_dereference(vif->bss_conf.chanctx_conf);
 740	if (WARN_ON(!ctx)) {
 741		rcu_read_unlock();
 742		return -EINVAL;
 743	}
 744	chandef = ctx->def;
 745	chains_static = ctx->rx_chains_static;
 746	chains_dynamic = ctx->rx_chains_dynamic;
 747	rcu_read_unlock();
 748
 749	ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
 750				   chains_static, chains_dynamic);
 751	if (ret)
 752		return ret;
 753
 754	/* add back the MAC */
 755	mvmvif->uploaded = false;
 756
 757	if (WARN_ON(!vif->cfg.assoc))
 758		return -EINVAL;
 759
 760	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
 761	if (ret)
 762		return ret;
 763
 764	/* add back binding - XXX refactor? */
 765	binding_cmd.id_and_color =
 766		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
 767						mvmvif->phy_ctxt->color));
 768	binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
 769	binding_cmd.phy =
 770		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
 771						mvmvif->phy_ctxt->color));
 772	binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 773							      mvmvif->color));
 774	for (i = 1; i < MAX_MACS_IN_BINDING; i++)
 775		binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
 776
 777	status = 0;
 778	ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
 779					  IWL_BINDING_CMD_SIZE_V1, &binding_cmd,
 780					  &status);
 781	if (ret) {
 782		IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
 783		return ret;
 784	}
 785
 786	if (status) {
 787		IWL_ERR(mvm, "Binding command failed: %u\n", status);
 788		return -EIO;
 789	}
 790
 791	ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
 792	if (ret)
 793		return ret;
 794	rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
 
 795
 796	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
 797	if (ret)
 798		return ret;
 799
 800	/* and some quota */
 801	quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, 0);
 802	quota->id_and_color =
 803		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
 804						mvmvif->phy_ctxt->color));
 805	quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
 806	quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
 807
 808	for (i = 1; i < MAX_BINDINGS; i++) {
 809		quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, i);
 810		quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
 811	}
 812
 813	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
 814				   iwl_mvm_quota_cmd_size(mvm), &quota_cmd);
 815	if (ret)
 816		IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
 817
 818	if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
 819		IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
 820
 821	return 0;
 822}
 823
 824static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
 825				       struct ieee80211_vif *vif)
 826{
 827	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 828	struct iwl_nonqos_seq_query_cmd query_cmd = {
 829		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
 830		.mac_id_n_color =
 831			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 832							mvmvif->color)),
 833	};
 834	struct iwl_host_cmd cmd = {
 835		.id = NON_QOS_TX_COUNTER_CMD,
 836		.flags = CMD_WANT_SKB,
 837	};
 838	int err;
 839	u32 size;
 840
 841	cmd.data[0] = &query_cmd;
 842	cmd.len[0] = sizeof(query_cmd);
 843
 844	err = iwl_mvm_send_cmd(mvm, &cmd);
 845	if (err)
 846		return err;
 847
 848	size = iwl_rx_packet_payload_len(cmd.resp_pkt);
 849	if (size < sizeof(__le16)) {
 850		err = -EINVAL;
 851	} else {
 852		err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
 853		/* firmware returns next, not last-used seqno */
 854		err = (u16) (err - 0x10);
 855	}
 856
 857	iwl_free_resp(&cmd);
 858	return err;
 859}
 860
 861void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
 862{
 863	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 864	struct iwl_nonqos_seq_query_cmd query_cmd = {
 865		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
 866		.mac_id_n_color =
 867			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
 868							mvmvif->color)),
 869		.value = cpu_to_le16(mvmvif->seqno),
 870	};
 871
 872	/* return if called during restart, not resume from D3 */
 873	if (!mvmvif->seqno_valid)
 874		return;
 875
 876	mvmvif->seqno_valid = false;
 877
 878	if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
 879				 sizeof(query_cmd), &query_cmd))
 880		IWL_ERR(mvm, "failed to set non-QoS seqno\n");
 881}
 882
 883static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
 884{
 885	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
 886
 887	iwl_mvm_stop_device(mvm);
 888	/*
 889	 * Set the HW restart bit -- this is mostly true as we're
 890	 * going to load new firmware and reprogram that, though
 891	 * the reprogramming is going to be manual to avoid adding
 892	 * all the MACs that aren't support.
 893	 * We don't have to clear up everything though because the
 894	 * reprogramming is manual. When we resume, we'll actually
 895	 * go through a proper restart sequence again to switch
 896	 * back to the runtime firmware image.
 897	 */
 898	set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
 899
 900	/* the fw is reset, so all the keys are cleared */
 901	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
 902
 903	mvm->ptk_ivlen = 0;
 904	mvm->ptk_icvlen = 0;
 905	mvm->ptk_ivlen = 0;
 906	mvm->ptk_icvlen = 0;
 907
 908	return iwl_mvm_load_d3_fw(mvm);
 909}
 910
 911static int
 912iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
 913			  struct cfg80211_wowlan *wowlan,
 914			  struct iwl_wowlan_config_cmd *wowlan_config_cmd,
 915			  struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
 916			  struct ieee80211_sta *ap_sta)
 917{
 918	struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
 919
 920	/* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
 921
 922	wowlan_config_cmd->is_11n_connection =
 923					ap_sta->deflink.ht_cap.ht_supported;
 924	wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
 925		ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
 926
 927	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 6) {
 928		/* Query the last used seqno and set it */
 929		int ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
 930
 931		if (ret < 0)
 932			return ret;
 933
 934		wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
 935	}
 936
 937	iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
 938
 939	if (wowlan->disconnect)
 940		wowlan_config_cmd->wakeup_filter |=
 941			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
 942				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
 943	if (wowlan->magic_pkt)
 944		wowlan_config_cmd->wakeup_filter |=
 945			cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
 946	if (wowlan->gtk_rekey_failure)
 947		wowlan_config_cmd->wakeup_filter |=
 948			cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
 949	if (wowlan->eap_identity_req)
 950		wowlan_config_cmd->wakeup_filter |=
 951			cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
 952	if (wowlan->four_way_handshake)
 953		wowlan_config_cmd->wakeup_filter |=
 954			cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
 955	if (wowlan->n_patterns)
 956		wowlan_config_cmd->wakeup_filter |=
 957			cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
 958
 959	if (wowlan->rfkill_release)
 960		wowlan_config_cmd->wakeup_filter |=
 961			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
 962
 963	if (wowlan->tcp) {
 964		/*
 965		 * Set the "link change" (really "link lost") flag as well
 966		 * since that implies losing the TCP connection.
 967		 */
 968		wowlan_config_cmd->wakeup_filter |=
 969			cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
 970				    IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
 971				    IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
 972				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
 973	}
 974
 975	if (wowlan->any) {
 976		wowlan_config_cmd->wakeup_filter |=
 977			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
 978				    IWL_WOWLAN_WAKEUP_LINK_CHANGE |
 979				    IWL_WOWLAN_WAKEUP_RX_FRAME |
 980				    IWL_WOWLAN_WAKEUP_BCN_FILTERING);
 981	}
 982
 983	return 0;
 984}
 985
 986static int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
 987					    struct ieee80211_vif *vif)
 988{
 989	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
 990				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
 991	struct wowlan_key_reprogram_data key_data = {};
 992	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 993	int ret;
 994	u8 cmd_ver;
 995	size_t cmd_size;
 996
 997	if (!unified) {
 998		/*
 999		 * if we have to configure keys, call ieee80211_iter_keys(),
1000		 * as we need non-atomic context in order to take the
1001		 * required locks.
1002		 */
1003		/*
1004		 * Note that currently we don't use CMD_ASYNC in the iterator.
1005		 * In case of key_data.configure_keys, all the configured
1006		 * commands are SYNC, and iwl_mvm_wowlan_program_keys() will
1007		 * take care of locking/unlocking mvm->mutex.
1008		 */
1009		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_program_keys,
1010				    &key_data);
1011
1012		if (key_data.error)
1013			return -EIO;
1014	}
1015
1016	ret = iwl_mvm_wowlan_config_rsc_tsc(mvm, vif);
1017	if (ret)
1018		return ret;
1019
1020	if (!fw_has_api(&mvm->fw->ucode_capa,
1021			IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
1022		int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TKIP_PARAM,
1023						IWL_FW_CMD_VER_UNKNOWN);
1024		struct wowlan_key_tkip_data tkip_data = {};
1025		int size;
1026
1027		if (ver == 2) {
1028			size = sizeof(tkip_data.tkip);
1029			tkip_data.tkip.sta_id =
1030				cpu_to_le32(mvmvif->ap_sta_id);
1031		} else if (ver == 1 || ver == IWL_FW_CMD_VER_UNKNOWN) {
1032			size = sizeof(struct iwl_wowlan_tkip_params_cmd_ver_1);
1033		} else {
1034			WARN_ON_ONCE(1);
1035			return -EINVAL;
1036		}
1037
1038		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_get_tkip_data,
1039				    &tkip_data);
1040
1041		if (tkip_data.have_tkip_keys) {
1042			/* send relevant data according to CMD version */
1043			ret = iwl_mvm_send_cmd_pdu(mvm,
1044						   WOWLAN_TKIP_PARAM,
1045						   CMD_ASYNC, size,
1046						   &tkip_data.tkip);
1047			if (ret)
1048				return ret;
1049		}
1050	}
1051
1052	/* configure rekey data only if offloaded rekey is supported (d3) */
1053	if (mvmvif->rekey_data.valid) {
1054		struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {};
1055		struct iwl_wowlan_kek_kck_material_cmd_v4 *_kek_kck_cmd =
1056			&kek_kck_cmd;
1057		struct wowlan_key_gtk_type_iter gtk_type_data = {
1058			.kek_kck_cmd = _kek_kck_cmd,
1059		};
1060
1061		cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1062						WOWLAN_KEK_KCK_MATERIAL,
1063						IWL_FW_CMD_VER_UNKNOWN);
1064		if (WARN_ON(cmd_ver != 2 && cmd_ver != 3 && cmd_ver != 4 &&
1065			    cmd_ver != IWL_FW_CMD_VER_UNKNOWN))
1066			return -EINVAL;
1067
1068		ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_gtk_type_iter,
1069				    &gtk_type_data);
1070
1071		memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1072		       mvmvif->rekey_data.kck_len);
1073		kek_kck_cmd.kck_len = cpu_to_le16(mvmvif->rekey_data.kck_len);
1074		memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1075		       mvmvif->rekey_data.kek_len);
1076		kek_kck_cmd.kek_len = cpu_to_le16(mvmvif->rekey_data.kek_len);
1077		kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1078		kek_kck_cmd.akm = cpu_to_le32(mvmvif->rekey_data.akm);
1079		kek_kck_cmd.sta_id = cpu_to_le32(mvmvif->ap_sta_id);
1080
1081		if (cmd_ver == 4) {
1082			cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v4);
1083		} else {
1084			if (cmd_ver == 3)
1085				cmd_size =
1086					sizeof(struct iwl_wowlan_kek_kck_material_cmd_v3);
1087			else
1088				cmd_size =
1089					sizeof(struct iwl_wowlan_kek_kck_material_cmd_v2);
1090			/* skip the sta_id at the beginning */
1091			_kek_kck_cmd = (void *)
1092				((u8 *)_kek_kck_cmd + sizeof(kek_kck_cmd.sta_id));
1093		}
1094
1095		IWL_DEBUG_WOWLAN(mvm, "setting akm %d\n",
1096				 mvmvif->rekey_data.akm);
1097
1098		ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_KEK_KCK_MATERIAL,
1099					   CMD_ASYNC, cmd_size, _kek_kck_cmd);
1100		if (ret)
1101			return ret;
1102	}
1103
1104	return 0;
1105}
1106
1107static int
1108iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1109		      struct cfg80211_wowlan *wowlan,
1110		      struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1111		      struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1112		      struct ieee80211_sta *ap_sta)
1113{
1114	int ret;
1115	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1116					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1117
1118	mvm->offload_tid = wowlan_config_cmd->offloading_tid;
1119
1120	if (!unified_image) {
1121		ret = iwl_mvm_switch_to_d3(mvm);
1122		if (ret)
1123			return ret;
1124
1125		ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1126		if (ret)
1127			return ret;
1128	}
1129
1130	/*
1131	 * This needs to be unlocked due to lock ordering
1132	 * constraints. Since we're in the suspend path
1133	 * that isn't really a problem though.
1134	 */
1135	mutex_unlock(&mvm->mutex);
1136	ret = iwl_mvm_wowlan_config_key_params(mvm, vif);
1137	mutex_lock(&mvm->mutex);
1138	if (ret)
1139		return ret;
1140
1141	ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1142				   sizeof(*wowlan_config_cmd),
1143				   wowlan_config_cmd);
1144	if (ret)
1145		return ret;
1146
1147	if (fw_has_api(&mvm->fw->ucode_capa,
1148		       IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE))
1149		ret = iwl_mvm_send_patterns(mvm, vif, wowlan);
1150	else
1151		ret = iwl_mvm_send_patterns_v1(mvm, wowlan);
1152	if (ret)
1153		return ret;
1154
1155	return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
1156}
1157
1158static int
1159iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1160			 struct cfg80211_wowlan *wowlan,
1161			 struct cfg80211_sched_scan_request *nd_config,
1162			 struct ieee80211_vif *vif)
1163{
1164	int ret;
1165	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1166					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1167
1168	if (!unified_image) {
1169		ret = iwl_mvm_switch_to_d3(mvm);
1170		if (ret)
1171			return ret;
1172	} else {
1173		/* In theory, we wouldn't have to stop a running sched
1174		 * scan in order to start another one (for
1175		 * net-detect).  But in practice this doesn't seem to
1176		 * work properly, so stop any running sched_scan now.
1177		 */
1178		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1179		if (ret)
1180			return ret;
1181	}
1182
1183	ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1184				       IWL_MVM_SCAN_NETDETECT);
1185	if (ret)
1186		return ret;
1187
1188	if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1189		return -EBUSY;
1190
1191	/* save the sched scan matchsets... */
1192	if (nd_config->n_match_sets) {
1193		mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1194					     sizeof(*nd_config->match_sets) *
1195					     nd_config->n_match_sets,
1196					     GFP_KERNEL);
1197		if (mvm->nd_match_sets)
1198			mvm->n_nd_match_sets = nd_config->n_match_sets;
1199	}
1200
1201	/* ...and the sched scan channels for later reporting */
1202	mvm->nd_channels = kmemdup(nd_config->channels,
1203				   sizeof(*nd_config->channels) *
1204				   nd_config->n_channels,
1205				   GFP_KERNEL);
1206	if (mvm->nd_channels)
1207		mvm->n_nd_channels = nd_config->n_channels;
1208
1209	return 0;
1210}
1211
1212static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1213{
1214	kfree(mvm->nd_match_sets);
1215	mvm->nd_match_sets = NULL;
1216	mvm->n_nd_match_sets = 0;
1217	kfree(mvm->nd_channels);
1218	mvm->nd_channels = NULL;
1219	mvm->n_nd_channels = 0;
1220}
1221
1222static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1223			     struct cfg80211_wowlan *wowlan,
1224			     bool test)
1225{
1226	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1227	struct ieee80211_vif *vif = NULL;
1228	struct iwl_mvm_vif *mvmvif = NULL;
1229	struct ieee80211_sta *ap_sta = NULL;
1230	struct iwl_d3_manager_config d3_cfg_cmd_data = {
1231		/*
1232		 * Program the minimum sleep time to 10 seconds, as many
1233		 * platforms have issues processing a wakeup signal while
1234		 * still being in the process of suspending.
1235		 */
1236		.min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1237	};
1238	struct iwl_host_cmd d3_cfg_cmd = {
1239		.id = D3_CONFIG_CMD,
1240		.flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
1241		.data[0] = &d3_cfg_cmd_data,
1242		.len[0] = sizeof(d3_cfg_cmd_data),
1243	};
1244	int ret;
1245	int len __maybe_unused;
1246	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1247					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1248
1249	if (!wowlan) {
1250		/*
1251		 * mac80211 shouldn't get here, but for D3 test
1252		 * it doesn't warrant a warning
1253		 */
1254		WARN_ON(!test);
1255		return -EINVAL;
1256	}
1257
1258	mutex_lock(&mvm->mutex);
1259
1260	set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1261
1262	synchronize_net();
1263
1264	vif = iwl_mvm_get_bss_vif(mvm);
1265	if (IS_ERR_OR_NULL(vif)) {
1266		ret = 1;
1267		goto out_noreset;
1268	}
1269
1270	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1271
1272	if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA) {
1273		/* if we're not associated, this must be netdetect */
1274		if (!wowlan->nd_config) {
1275			ret = 1;
1276			goto out_noreset;
1277		}
1278
1279		ret = iwl_mvm_netdetect_config(
1280			mvm, wowlan, wowlan->nd_config, vif);
1281		if (ret)
1282			goto out;
1283
1284		mvm->net_detect = true;
1285	} else {
1286		struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
 
 
1287
1288		wowlan_config_cmd.sta_id = mvmvif->ap_sta_id;
1289
1290		ap_sta = rcu_dereference_protected(
1291			mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1292			lockdep_is_held(&mvm->mutex));
1293		if (IS_ERR_OR_NULL(ap_sta)) {
1294			ret = -EINVAL;
1295			goto out_noreset;
1296		}
1297
 
 
 
 
 
1298		ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1299						vif, mvmvif, ap_sta);
1300		if (ret)
1301			goto out_noreset;
1302		ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1303					    vif, mvmvif, ap_sta);
1304		if (ret)
1305			goto out;
1306
1307		mvm->net_detect = false;
1308	}
1309
1310	ret = iwl_mvm_power_update_device(mvm);
1311	if (ret)
1312		goto out;
1313
1314	ret = iwl_mvm_power_update_mac(mvm);
1315	if (ret)
1316		goto out;
1317
1318#ifdef CONFIG_IWLWIFI_DEBUGFS
1319	if (mvm->d3_wake_sysassert)
1320		d3_cfg_cmd_data.wakeup_flags |=
1321			cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1322#endif
1323
1324	/*
1325	 * Prior to 9000 device family the driver needs to stop the dbg
1326	 * recording before entering D3. In later devices the FW stops the
1327	 * recording automatically.
1328	 */
1329	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1330		iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
1331
1332	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1333
1334	/* must be last -- this switches firmware state */
1335	ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1336	if (ret)
1337		goto out;
1338#ifdef CONFIG_IWLWIFI_DEBUGFS
1339	len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1340	if (len >= sizeof(u32)) {
1341		mvm->d3_test_pme_ptr =
1342			le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1343	}
1344#endif
1345	iwl_free_resp(&d3_cfg_cmd);
1346
1347	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1348
1349	ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1350 out:
1351	if (ret < 0) {
1352		iwl_mvm_free_nd(mvm);
1353
1354		if (!unified_image) {
1355			if (mvm->fw_restart > 0) {
1356				mvm->fw_restart--;
1357				ieee80211_restart_hw(mvm->hw);
1358			}
1359		}
1360
1361		clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1362	}
1363 out_noreset:
1364	mutex_unlock(&mvm->mutex);
1365
1366	return ret;
1367}
1368
1369int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1370{
1371	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1372
1373	iwl_mvm_pause_tcm(mvm, true);
1374
1375	iwl_fw_runtime_suspend(&mvm->fwrt);
1376
1377	return __iwl_mvm_suspend(hw, wowlan, false);
1378}
1379
 
 
 
 
 
 
 
 
1380/* converted data from the different status responses */
1381struct iwl_wowlan_status_data {
1382	u64 replay_ctr;
1383	u32 num_of_gtk_rekeys;
1384	u32 received_beacons;
1385	u32 wakeup_reasons;
1386	u32 wake_packet_length;
1387	u32 wake_packet_bufsize;
1388	u16 pattern_number;
1389	u16 non_qos_seq_ctr;
1390	u16 qos_seq_ctr[8];
1391	u8 tid_tear_down;
1392
1393	struct {
1394		/* including RX MIC key for TKIP */
1395		u8 key[WOWLAN_KEY_MAX_SIZE];
1396		u8 len;
1397		u8 flags;
1398	} gtk;
 
1399
1400	struct {
1401		/*
1402		 * We store both the TKIP and AES representations
1403		 * coming from the firmware because we decode the
1404		 * data from there before we iterate the keys and
1405		 * know which one we need.
1406		 */
1407		struct {
1408			struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1409		} tkip, aes;
1410
1411		/*
1412		 * We use -1 for when we have valid data but don't know
1413		 * the key ID from firmware, and thus it needs to be
1414		 * installed with the last key (depending on rekeying).
1415		 */
1416		s8 key_id;
1417		bool valid;
1418	} gtk_seq[2];
1419
1420	struct {
1421		/* Same as above */
1422		struct {
1423			struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1424			u64 tx_pn;
1425		} tkip, aes;
1426	} ptk;
1427
1428	struct {
1429		u64 ipn;
1430		u8 key[WOWLAN_KEY_MAX_SIZE];
1431		u8 len;
1432		u8 flags;
1433	} igtk;
1434
1435	u8 *wake_packet;
1436};
1437
1438static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1439					  struct ieee80211_vif *vif,
1440					  struct iwl_wowlan_status_data *status)
1441{
1442	struct sk_buff *pkt = NULL;
1443	struct cfg80211_wowlan_wakeup wakeup = {
1444		.pattern_idx = -1,
1445	};
1446	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1447	u32 reasons = status->wakeup_reasons;
1448
1449	if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1450		wakeup_report = NULL;
1451		goto report;
1452	}
1453
1454	pm_wakeup_event(mvm->dev, 0);
1455
1456	if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1457		wakeup.magic_pkt = true;
1458
1459	if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1460		wakeup.pattern_idx =
1461			status->pattern_number;
1462
1463	if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1464		       IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1465		wakeup.disconnect = true;
1466
1467	if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1468		wakeup.gtk_rekey_failure = true;
1469
1470	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1471		wakeup.rfkill_release = true;
1472
1473	if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1474		wakeup.eap_identity_req = true;
1475
1476	if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1477		wakeup.four_way_handshake = true;
1478
1479	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1480		wakeup.tcp_connlost = true;
1481
1482	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1483		wakeup.tcp_nomoretokens = true;
1484
1485	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1486		wakeup.tcp_match = true;
1487
1488	if (status->wake_packet) {
1489		int pktsize = status->wake_packet_bufsize;
1490		int pktlen = status->wake_packet_length;
1491		const u8 *pktdata = status->wake_packet;
1492		const struct ieee80211_hdr *hdr = (const void *)pktdata;
1493		int truncated = pktlen - pktsize;
1494
1495		/* this would be a firmware bug */
1496		if (WARN_ON_ONCE(truncated < 0))
1497			truncated = 0;
1498
1499		if (ieee80211_is_data(hdr->frame_control)) {
1500			int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1501			int ivlen = 0, icvlen = 4; /* also FCS */
1502
1503			pkt = alloc_skb(pktsize, GFP_KERNEL);
1504			if (!pkt)
1505				goto report;
1506
1507			skb_put_data(pkt, pktdata, hdrlen);
1508			pktdata += hdrlen;
1509			pktsize -= hdrlen;
1510
1511			if (ieee80211_has_protected(hdr->frame_control)) {
1512				/*
1513				 * This is unlocked and using gtk_i(c)vlen,
1514				 * but since everything is under RTNL still
1515				 * that's not really a problem - changing
1516				 * it would be difficult.
1517				 */
1518				if (is_multicast_ether_addr(hdr->addr1)) {
1519					ivlen = mvm->gtk_ivlen;
1520					icvlen += mvm->gtk_icvlen;
1521				} else {
1522					ivlen = mvm->ptk_ivlen;
1523					icvlen += mvm->ptk_icvlen;
1524				}
1525			}
1526
1527			/* if truncated, FCS/ICV is (partially) gone */
1528			if (truncated >= icvlen) {
1529				icvlen = 0;
1530				truncated -= icvlen;
1531			} else {
1532				icvlen -= truncated;
1533				truncated = 0;
1534			}
1535
1536			pktsize -= ivlen + icvlen;
1537			pktdata += ivlen;
1538
1539			skb_put_data(pkt, pktdata, pktsize);
1540
1541			if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1542				goto report;
1543			wakeup.packet = pkt->data;
1544			wakeup.packet_present_len = pkt->len;
1545			wakeup.packet_len = pkt->len - truncated;
1546			wakeup.packet_80211 = false;
1547		} else {
1548			int fcslen = 4;
1549
1550			if (truncated >= 4) {
1551				truncated -= 4;
1552				fcslen = 0;
1553			} else {
1554				fcslen -= truncated;
1555				truncated = 0;
1556			}
1557			pktsize -= fcslen;
1558			wakeup.packet = status->wake_packet;
1559			wakeup.packet_present_len = pktsize;
1560			wakeup.packet_len = pktlen - truncated;
1561			wakeup.packet_80211 = true;
1562		}
1563	}
1564
1565 report:
1566	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1567	kfree_skb(pkt);
1568}
1569
1570static void iwl_mvm_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1571{
1572	u64 pn = le64_to_cpu(le_pn);
1573
1574	seq->ccmp.pn[0] = pn >> 40;
1575	seq->ccmp.pn[1] = pn >> 32;
1576	seq->ccmp.pn[2] = pn >> 24;
1577	seq->ccmp.pn[3] = pn >> 16;
1578	seq->ccmp.pn[4] = pn >> 8;
1579	seq->ccmp.pn[5] = pn;
1580}
1581
1582static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1583				  struct ieee80211_key_seq *seq)
1584{
1585	iwl_mvm_le64_to_aes_seq(sc->pn, seq);
1586}
1587
1588static void iwl_mvm_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1589{
1590	u64 pn = le64_to_cpu(le_pn);
1591
1592	seq->tkip.iv16 = (u16)pn;
1593	seq->tkip.iv32 = (u32)(pn >> 16);
1594}
1595
1596static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1597				   struct ieee80211_key_seq *seq)
1598{
1599	seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1600	seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1601}
1602
1603static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key,
1604					struct ieee80211_key_seq *seq)
1605{
1606	int tid;
1607
1608	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1609		ieee80211_set_key_rx_seq(key, tid, &seq[tid]);
1610}
1611
1612static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm,
1613				       struct iwl_wowlan_status_data *status,
1614				       struct ieee80211_sta *sta,
1615				       struct ieee80211_key_conf *key)
1616{
1617	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1618	struct iwl_mvm_key_pn *ptk_pn;
1619	int tid;
1620
1621	iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq);
1622
1623	if (!iwl_mvm_has_new_rx_api(mvm))
1624		return;
1625
1626
1627	rcu_read_lock();
1628	ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
1629	if (WARN_ON(!ptk_pn)) {
1630		rcu_read_unlock();
1631		return;
1632	}
1633
1634	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1635		int i;
1636
1637		for (i = 1; i < mvm->trans->num_rx_queues; i++)
1638			memcpy(ptk_pn->q[i].pn[tid],
1639			       status->ptk.aes.seq[tid].ccmp.pn,
1640			       IEEE80211_CCMP_PN_LEN);
1641	}
1642	rcu_read_unlock();
1643}
1644
1645static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status,
1646					 union iwl_all_tsc_rsc *sc)
1647{
1648	int i;
1649
1650	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1651	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1652
1653	/* GTK RX counters */
1654	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1655		iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i],
1656				       &status->gtk_seq[0].tkip.seq[i]);
1657		iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i],
1658				      &status->gtk_seq[0].aes.seq[i]);
1659	}
1660	status->gtk_seq[0].valid = true;
1661	status->gtk_seq[0].key_id = -1;
1662
1663	/* PTK TX counter */
1664	status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) |
1665				 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16);
1666	status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn);
1667
1668	/* PTK RX counters */
1669	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1670		iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i],
1671				       &status->ptk.tkip.seq[i]);
1672		iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i],
1673				      &status->ptk.aes.seq[i]);
1674	}
1675}
1676
1677static void
1678iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data *status,
1679					struct iwl_wowlan_all_rsc_tsc_v5 *sc,
1680					unsigned int idx, unsigned int key_id)
1681{
1682	int tid;
1683
1684	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1685		iwl_mvm_le64_to_tkip_seq(sc->mcast_rsc[idx][tid],
1686					 &status->gtk_seq[idx].tkip.seq[tid]);
1687		iwl_mvm_le64_to_aes_seq(sc->mcast_rsc[idx][tid],
1688					&status->gtk_seq[idx].aes.seq[tid]);
1689	}
1690
1691	status->gtk_seq[idx].valid = true;
1692	status->gtk_seq[idx].key_id = key_id;
1693}
1694
1695static void
1696iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data *status,
1697				struct iwl_wowlan_all_rsc_tsc_v5 *sc)
1698{
1699	int i, tid;
1700
1701	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1702	BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1703	BUILD_BUG_ON(ARRAY_SIZE(sc->mcast_rsc) != ARRAY_SIZE(status->gtk_seq));
1704
1705	/* GTK RX counters */
1706	for (i = 0; i < ARRAY_SIZE(sc->mcast_key_id_map); i++) {
1707		u8 entry = sc->mcast_key_id_map[i];
1708
1709		if (entry < ARRAY_SIZE(sc->mcast_rsc))
1710			iwl_mvm_convert_key_counters_v5_gtk_seq(status, sc,
1711								entry, i);
1712	}
1713
1714	/* PTK TX counters not needed, assigned in device */
1715
1716	/* PTK RX counters */
1717	for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1718		iwl_mvm_le64_to_tkip_seq(sc->ucast_rsc[tid],
1719					 &status->ptk.tkip.seq[tid]);
1720		iwl_mvm_le64_to_aes_seq(sc->ucast_rsc[tid],
1721					&status->ptk.aes.seq[tid]);
1722	}
1723}
1724
1725static void iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf *key,
1726				       struct iwl_wowlan_status_data *status,
1727				       int idx)
1728{
1729	switch (key->cipher) {
1730	case WLAN_CIPHER_SUITE_CCMP:
1731	case WLAN_CIPHER_SUITE_GCMP:
1732	case WLAN_CIPHER_SUITE_GCMP_256:
1733		iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].aes.seq);
1734		break;
1735	case WLAN_CIPHER_SUITE_TKIP:
1736		iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].tkip.seq);
1737		break;
1738	default:
1739		WARN_ON(1);
1740	}
1741}
1742
1743static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1744				   struct iwl_wowlan_status_data *status,
1745				   bool installed)
1746{
1747	int i;
1748
1749	for (i = 0; i < ARRAY_SIZE(status->gtk_seq); i++) {
1750		if (!status->gtk_seq[i].valid)
1751			continue;
1752
1753		/* Handle the case where we know the key ID */
1754		if (status->gtk_seq[i].key_id == key->keyidx) {
1755			s8 new_key_id = -1;
1756
1757			if (status->num_of_gtk_rekeys)
1758				new_key_id = status->gtk.flags &
1759						IWL_WOWLAN_GTK_IDX_MASK;
1760
1761			/* Don't install a new key's value to an old key */
1762			if (new_key_id != key->keyidx)
1763				iwl_mvm_set_key_rx_seq_idx(key, status, i);
1764			continue;
1765		}
1766
1767		/* handle the case where we didn't, last key only */
1768		if (status->gtk_seq[i].key_id == -1 &&
1769		    (!status->num_of_gtk_rekeys || installed))
1770			iwl_mvm_set_key_rx_seq_idx(key, status, i);
1771	}
1772}
1773
1774struct iwl_mvm_d3_gtk_iter_data {
1775	struct iwl_mvm *mvm;
1776	struct iwl_wowlan_status_data *status;
1777	void *last_gtk;
1778	u32 cipher;
1779	bool find_phase, unhandled_cipher;
1780	int num_keys;
1781};
1782
1783static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1784				   struct ieee80211_vif *vif,
1785				   struct ieee80211_sta *sta,
1786				   struct ieee80211_key_conf *key,
1787				   void *_data)
1788{
1789	struct iwl_mvm_d3_gtk_iter_data *data = _data;
1790	struct iwl_wowlan_status_data *status = data->status;
1791
1792	if (data->unhandled_cipher)
1793		return;
1794
1795	switch (key->cipher) {
1796	case WLAN_CIPHER_SUITE_WEP40:
1797	case WLAN_CIPHER_SUITE_WEP104:
1798		/* ignore WEP completely, nothing to do */
1799		return;
1800	case WLAN_CIPHER_SUITE_CCMP:
1801	case WLAN_CIPHER_SUITE_GCMP:
1802	case WLAN_CIPHER_SUITE_GCMP_256:
1803	case WLAN_CIPHER_SUITE_TKIP:
1804		/* we support these */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1805		break;
1806	default:
1807		/* everything else (even CMAC for MFP) - disconnect from AP */
1808		data->unhandled_cipher = true;
1809		return;
1810	}
1811
1812	data->num_keys++;
 
1813
1814	/*
1815	 * pairwise key - update sequence counters only;
1816	 * note that this assumes no TDLS sessions are active
1817	 */
1818	if (sta) {
1819		if (data->find_phase)
1820			return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1821
1822		switch (key->cipher) {
1823		case WLAN_CIPHER_SUITE_CCMP:
1824		case WLAN_CIPHER_SUITE_GCMP:
1825		case WLAN_CIPHER_SUITE_GCMP_256:
 
 
 
 
 
1826			atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn);
1827			iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key);
1828			break;
1829		case WLAN_CIPHER_SUITE_TKIP:
 
 
 
1830			atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn);
1831			iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq);
1832			break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1833		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1834
1835		/* that's it for this key */
1836		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1837	}
 
 
 
 
 
 
 
 
 
 
 
 
 
1838
1839	if (data->find_phase) {
1840		data->last_gtk = key;
1841		data->cipher = key->cipher;
1842		return;
1843	}
1844
1845	if (data->status->num_of_gtk_rekeys)
1846		ieee80211_remove_key(key);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1847
1848	if (data->last_gtk == key)
1849		iwl_mvm_set_key_rx_seq(key, data->status, false);
1850}
1851
1852static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1853					  struct ieee80211_vif *vif,
1854					  struct iwl_wowlan_status_data *status)
1855{
1856	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1857	struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1858		.mvm = mvm,
1859		.status = status,
1860	};
 
 
1861	u32 disconnection_reasons =
1862		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1863		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1864
1865	if (!status || !vif->bss_conf.bssid)
1866		return false;
1867
1868	if (status->wakeup_reasons & disconnection_reasons)
1869		return false;
1870
 
 
 
 
 
 
 
 
 
 
 
1871	/* find last GTK that we used initially, if any */
1872	gtkdata.find_phase = true;
1873	ieee80211_iter_keys(mvm->hw, vif,
1874			    iwl_mvm_d3_update_keys, &gtkdata);
1875	/* not trying to keep connections with MFP/unhandled ciphers */
1876	if (gtkdata.unhandled_cipher)
1877		return false;
1878	if (!gtkdata.num_keys)
1879		goto out;
1880	if (!gtkdata.last_gtk)
1881		return false;
1882
1883	/*
1884	 * invalidate all other GTKs that might still exist and update
1885	 * the one that we used
1886	 */
1887	gtkdata.find_phase = false;
1888	ieee80211_iter_keys(mvm->hw, vif,
1889			    iwl_mvm_d3_update_keys, &gtkdata);
1890
1891	IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
1892			 status->num_of_gtk_rekeys);
1893	if (status->num_of_gtk_rekeys) {
1894		struct ieee80211_key_conf *key;
1895		struct {
1896			struct ieee80211_key_conf conf;
1897			u8 key[32];
1898		} conf = {
1899			.conf.cipher = gtkdata.cipher,
1900			.conf.keyidx =
1901				status->gtk.flags & IWL_WOWLAN_GTK_IDX_MASK,
1902		};
1903		__be64 replay_ctr;
1904
1905		IWL_DEBUG_WOWLAN(mvm,
1906				 "Received from FW GTK cipher %d, key index %d\n",
1907				 conf.conf.cipher, conf.conf.keyidx);
1908
1909		BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
1910		BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP);
1911		BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256);
1912		BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP);
1913		BUILD_BUG_ON(sizeof(conf.key) < sizeof(status->gtk.key));
1914
1915		memcpy(conf.conf.key, status->gtk.key, sizeof(status->gtk.key));
1916
1917		switch (gtkdata.cipher) {
1918		case WLAN_CIPHER_SUITE_CCMP:
1919		case WLAN_CIPHER_SUITE_GCMP:
1920			conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1921			break;
1922		case WLAN_CIPHER_SUITE_GCMP_256:
1923			conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
1924			break;
1925		case WLAN_CIPHER_SUITE_TKIP:
1926			conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1927			break;
1928		}
1929
1930		key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1931		if (IS_ERR(key))
 
1932			return false;
1933		iwl_mvm_set_key_rx_seq(key, status, true);
1934
1935		replay_ctr = cpu_to_be64(status->replay_ctr);
 
 
 
 
 
1936
1937		ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1938					   (void *)&replay_ctr, GFP_KERNEL);
1939	}
1940
1941out:
1942	if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
1943				    WOWLAN_GET_STATUSES, 0) < 10) {
1944		mvmvif->seqno_valid = true;
1945		/* +0x10 because the set API expects next-to-use, not last-used */
1946		mvmvif->seqno = status->non_qos_seq_ctr + 0x10;
1947	}
1948
1949	return true;
1950}
1951
1952static void iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data *status,
1953				   struct iwl_wowlan_gtk_status_v2 *data)
1954{
1955	BUILD_BUG_ON(sizeof(status->gtk.key) < sizeof(data->key));
1956	BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
1957		     sizeof(data->tkip_mic_key) >
1958		     sizeof(status->gtk.key));
1959
1960	status->gtk.len = data->key_len;
1961	status->gtk.flags = data->key_flags;
1962
1963	memcpy(status->gtk.key, data->key, sizeof(data->key));
1964
1965	/* if it's as long as the TKIP encryption key, copy MIC key */
1966	if (status->gtk.len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
1967		memcpy(status->gtk.key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1968		       data->tkip_mic_key, sizeof(data->tkip_mic_key));
1969}
1970
1971static void iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data *status,
1972				   struct iwl_wowlan_gtk_status_v3 *data)
1973{
1974	/* The parts we need are identical in v2 and v3 */
1975#define CHECK(_f) do {							\
1976	BUILD_BUG_ON(offsetof(struct iwl_wowlan_gtk_status_v2, _f) !=	\
1977		     offsetof(struct iwl_wowlan_gtk_status_v3, _f));	\
1978	BUILD_BUG_ON(offsetofend(struct iwl_wowlan_gtk_status_v2, _f) !=\
1979		     offsetofend(struct iwl_wowlan_gtk_status_v3, _f));	\
1980} while (0)
1981
1982	CHECK(key);
1983	CHECK(key_len);
1984	CHECK(key_flags);
1985	CHECK(tkip_mic_key);
1986#undef CHECK
1987
1988	iwl_mvm_convert_gtk_v2(status, (void *)data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1989}
1990
1991static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status,
1992				 struct iwl_wowlan_igtk_status *data)
1993{
1994	const u8 *ipn = data->ipn;
1995
1996	BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key));
 
1997
1998	status->igtk.len = data->key_len;
1999	status->igtk.flags = data->key_flags;
 
 
 
2000
2001	memcpy(status->igtk.key, data->key, sizeof(data->key));
 
 
 
 
 
 
 
 
 
 
 
 
 
2002
2003	status->igtk.ipn = ((u64)ipn[5] <<  0) |
2004			   ((u64)ipn[4] <<  8) |
2005			   ((u64)ipn[3] << 16) |
2006			   ((u64)ipn[2] << 24) |
2007			   ((u64)ipn[1] << 32) |
2008			   ((u64)ipn[0] << 40);
 
 
 
 
 
 
 
 
 
 
 
 
2009}
2010
2011static void iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm *mvm,
2012					    struct iwl_wowlan_info_notif *data,
2013					    struct iwl_wowlan_status_data *status,
2014					    u32 len)
2015{
2016	u32 i;
2017
 
 
 
 
 
 
2018	if (len < sizeof(*data)) {
2019		IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2020		status = NULL;
2021		return;
2022	}
2023
2024	iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2025	iwl_mvm_convert_gtk_v3(status, &data->gtk[0]);
2026	iwl_mvm_convert_igtk(status, &data->igtk[0]);
 
 
 
 
 
 
 
 
 
 
 
 
2027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2028	status->replay_ctr = le64_to_cpu(data->replay_ctr);
2029	status->pattern_number = le16_to_cpu(data->pattern_number);
2030	for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2031		status->qos_seq_ctr[i] =
2032			le16_to_cpu(data->qos_seq_ctr[i]);
2033	status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2034	status->num_of_gtk_rekeys =
2035		le32_to_cpu(data->num_of_gtk_rekeys);
2036	status->received_beacons = le32_to_cpu(data->received_beacons);
2037	status->tid_tear_down = data->tid_tear_down;
2038}
2039
2040/* Occasionally, templates would be nice. This is one of those times ... */
2041#define iwl_mvm_parse_wowlan_status_common(_ver)			\
2042static struct iwl_wowlan_status_data *					\
2043iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm,	\
2044					    struct iwl_wowlan_status_ ##_ver *data,\
2045					    int len)			\
2046{									\
2047	struct iwl_wowlan_status_data *status;				\
2048	int data_size, i;						\
2049									\
2050	if (len < sizeof(*data)) {					\
2051		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
2052		return NULL;						\
2053	}								\
2054									\
2055	data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4);	\
2056	if (len != sizeof(*data) + data_size) {				\
2057		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");	\
2058		return NULL;						\
2059	}								\
2060									\
2061	status = kzalloc(sizeof(*status), GFP_KERNEL);			\
2062	if (!status)							\
2063		return NULL;						\
2064									\
2065	/* copy all the common fields */				\
2066	status->replay_ctr = le64_to_cpu(data->replay_ctr);		\
2067	status->pattern_number = le16_to_cpu(data->pattern_number);	\
2068	status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr);	\
2069	for (i = 0; i < 8; i++)						\
2070		status->qos_seq_ctr[i] =				\
2071			le16_to_cpu(data->qos_seq_ctr[i]);		\
2072	status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);	\
2073	status->num_of_gtk_rekeys =					\
2074		le32_to_cpu(data->num_of_gtk_rekeys);			\
2075	status->received_beacons = le32_to_cpu(data->received_beacons);	\
2076	status->wake_packet_length =					\
2077		le32_to_cpu(data->wake_packet_length);			\
2078	status->wake_packet_bufsize =					\
2079		le32_to_cpu(data->wake_packet_bufsize);			\
2080	if (status->wake_packet_bufsize) {				\
2081		status->wake_packet =					\
2082			kmemdup(data->wake_packet,			\
2083				status->wake_packet_bufsize,		\
2084				GFP_KERNEL);				\
2085		if (!status->wake_packet) {				\
2086			kfree(status);					\
2087			return NULL;					\
2088		}							\
2089	} else {							\
2090		status->wake_packet = NULL;				\
2091	}								\
2092									\
2093	return status;							\
2094}
2095
2096iwl_mvm_parse_wowlan_status_common(v6)
2097iwl_mvm_parse_wowlan_status_common(v7)
2098iwl_mvm_parse_wowlan_status_common(v9)
2099iwl_mvm_parse_wowlan_status_common(v12)
2100
2101static struct iwl_wowlan_status_data *
2102iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id)
2103{
2104	struct iwl_wowlan_status_data *status;
2105	struct iwl_wowlan_get_status_cmd get_status_cmd = {
2106		.sta_id = cpu_to_le32(sta_id),
2107	};
2108	struct iwl_host_cmd cmd = {
2109		.id = WOWLAN_GET_STATUSES,
2110		.flags = CMD_WANT_SKB,
2111		.data = { &get_status_cmd, },
2112		.len = { sizeof(get_status_cmd), },
2113	};
2114	int ret, len;
2115	u8 notif_ver;
2116	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
2117					   IWL_FW_CMD_VER_UNKNOWN);
2118
2119	if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN)
2120		cmd.len[0] = 0;
2121
2122	lockdep_assert_held(&mvm->mutex);
2123
2124	ret = iwl_mvm_send_cmd(mvm, &cmd);
2125	if (ret) {
2126		IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
2127		return ERR_PTR(ret);
2128	}
2129
2130	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2131
2132	/* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2133	notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2134					    WOWLAN_GET_STATUSES, 0);
2135	if (!notif_ver)
2136		notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
2137						    WOWLAN_GET_STATUSES, 7);
2138
2139	if (!fw_has_api(&mvm->fw->ucode_capa,
2140			IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL)) {
2141		struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
2142
2143		status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len);
2144		if (!status)
2145			goto out_free_resp;
2146
2147		BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
2148			     sizeof(status->gtk.key));
2149		BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2150			     sizeof(v6->gtk.tkip_mic_key) >
2151			     sizeof(status->gtk.key));
2152
2153		/* copy GTK info to the right place */
2154		memcpy(status->gtk.key, v6->gtk.decrypt_key,
2155		       sizeof(v6->gtk.decrypt_key));
2156		memcpy(status->gtk.key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2157		       v6->gtk.tkip_mic_key,
2158		       sizeof(v6->gtk.tkip_mic_key));
2159
2160		iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc);
2161
2162		/* hardcode the key length to 16 since v6 only supports 16 */
2163		status->gtk.len = 16;
2164
2165		/*
2166		 * The key index only uses 2 bits (values 0 to 3) and
2167		 * we always set bit 7 which means this is the
2168		 * currently used key.
2169		 */
2170		status->gtk.flags = v6->gtk.key_index | BIT(7);
2171	} else if (notif_ver == 7) {
2172		struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
2173
2174		status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len);
2175		if (!status)
2176			goto out_free_resp;
2177
2178		iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc);
2179		iwl_mvm_convert_gtk_v2(status, &v7->gtk[0]);
2180		iwl_mvm_convert_igtk(status, &v7->igtk[0]);
2181	} else if (notif_ver == 9 || notif_ver == 10 || notif_ver == 11) {
2182		struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data;
2183
2184		/* these three command versions have same layout and size, the
2185		 * difference is only in a few not used (reserved) fields.
2186		 */
2187		status = iwl_mvm_parse_wowlan_status_common_v9(mvm, v9, len);
2188		if (!status)
2189			goto out_free_resp;
2190
2191		iwl_mvm_convert_key_counters(status, &v9->gtk[0].rsc.all_tsc_rsc);
2192		iwl_mvm_convert_gtk_v2(status, &v9->gtk[0]);
2193		iwl_mvm_convert_igtk(status, &v9->igtk[0]);
2194
2195		status->tid_tear_down = v9->tid_tear_down;
2196	} else if (notif_ver == 12) {
2197		struct iwl_wowlan_status_v12 *v12 = (void *)cmd.resp_pkt->data;
2198
2199		status = iwl_mvm_parse_wowlan_status_common_v12(mvm, v12, len);
2200		if (!status)
2201			goto out_free_resp;
2202
2203		iwl_mvm_convert_key_counters_v5(status, &v12->gtk[0].sc);
2204		iwl_mvm_convert_gtk_v3(status, &v12->gtk[0]);
2205		iwl_mvm_convert_igtk(status, &v12->igtk[0]);
2206
2207		status->tid_tear_down = v12->tid_tear_down;
2208	} else {
2209		IWL_ERR(mvm,
2210			"Firmware advertises unknown WoWLAN status response %d!\n",
2211			notif_ver);
2212		status = NULL;
2213	}
2214
2215out_free_resp:
2216	iwl_free_resp(&cmd);
2217	return status;
2218}
2219
2220/* releases the MVM mutex */
2221static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
2222					 struct ieee80211_vif *vif,
2223					 struct iwl_wowlan_status_data *status)
2224{
2225	int i;
2226	bool keep;
2227	struct iwl_mvm_sta *mvm_ap_sta;
2228
2229	if (!status)
2230		goto out_unlock;
2231
2232	IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
2233			 status->wakeup_reasons);
2234
2235	/* still at hard-coded place 0 for D3 image */
2236	mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
2237	if (!mvm_ap_sta)
2238		goto out_unlock;
2239
2240	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
2241		u16 seq = status->qos_seq_ctr[i];
2242		/* firmware stores last-used value, we store next value */
2243		seq += 0x10;
2244		mvm_ap_sta->tid_data[i].seq_number = seq;
2245	}
2246
2247	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
2248		i = mvm->offload_tid;
2249		iwl_trans_set_q_ptrs(mvm->trans,
2250				     mvm_ap_sta->tid_data[i].txq_id,
2251				     mvm_ap_sta->tid_data[i].seq_number >> 4);
2252	}
2253
2254	/* now we have all the data we need, unlock to avoid mac80211 issues */
2255	mutex_unlock(&mvm->mutex);
2256
2257	iwl_mvm_report_wakeup_reasons(mvm, vif, status);
2258
2259	keep = iwl_mvm_setup_connection_keep(mvm, vif, status);
2260
2261	return keep;
2262
2263out_unlock:
2264	mutex_unlock(&mvm->mutex);
2265	return false;
2266}
2267
2268#define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
2269			  IWL_SCAN_MAX_PROFILES)
2270
2271struct iwl_mvm_nd_results {
2272	u32 matched_profiles;
2273	u8 matches[ND_QUERY_BUF_LEN];
2274};
2275
2276static int
2277iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
2278				struct iwl_mvm_nd_results *results)
2279{
2280	struct iwl_scan_offload_match_info *query;
2281	struct iwl_host_cmd cmd = {
2282		.id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
2283		.flags = CMD_WANT_SKB,
2284	};
2285	int ret, len;
2286	size_t query_len, matches_len;
2287	int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
2288
2289	ret = iwl_mvm_send_cmd(mvm, &cmd);
2290	if (ret) {
2291		IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
2292		return ret;
2293	}
2294
2295	if (fw_has_api(&mvm->fw->ucode_capa,
2296		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2297		query_len = sizeof(struct iwl_scan_offload_match_info);
2298		matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2299			max_profiles;
2300	} else {
2301		query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
2302		matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
2303			max_profiles;
2304	}
2305
2306	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2307	if (len < query_len) {
2308		IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
2309		ret = -EIO;
2310		goto out_free_resp;
2311	}
2312
2313	query = (void *)cmd.resp_pkt->data;
2314
2315	results->matched_profiles = le32_to_cpu(query->matched_profiles);
2316	memcpy(results->matches, query->matches, matches_len);
2317
2318#ifdef CONFIG_IWLWIFI_DEBUGFS
2319	mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
2320#endif
2321
2322out_free_resp:
2323	iwl_free_resp(&cmd);
2324	return ret;
2325}
2326
2327static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
2328					 struct iwl_mvm_nd_results *results,
2329					 int idx)
2330{
2331	int n_chans = 0, i;
2332
2333	if (fw_has_api(&mvm->fw->ucode_capa,
2334		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2335		struct iwl_scan_offload_profile_match *matches =
2336			(void *)results->matches;
2337
2338		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
2339			n_chans += hweight8(matches[idx].matching_channels[i]);
2340	} else {
2341		struct iwl_scan_offload_profile_match_v1 *matches =
2342			(void *)results->matches;
2343
2344		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
2345			n_chans += hweight8(matches[idx].matching_channels[i]);
2346	}
2347
2348	return n_chans;
2349}
2350
2351static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
2352				    struct iwl_mvm_nd_results *results,
2353				    struct cfg80211_wowlan_nd_match *match,
2354				    int idx)
2355{
2356	int i;
2357
2358	if (fw_has_api(&mvm->fw->ucode_capa,
2359		       IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2360		struct iwl_scan_offload_profile_match *matches =
2361			 (void *)results->matches;
2362
2363		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
2364			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2365				match->channels[match->n_channels++] =
2366					mvm->nd_channels[i]->center_freq;
2367	} else {
2368		struct iwl_scan_offload_profile_match_v1 *matches =
2369			 (void *)results->matches;
2370
2371		for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
2372			if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2373				match->channels[match->n_channels++] =
2374					mvm->nd_channels[i]->center_freq;
2375	}
2376}
2377
2378/**
2379 * enum iwl_d3_notif - d3 notifications
2380 * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF was received
2381 * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF was received
2382 * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF was received
2383 * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF was received
2384 * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF was received
2385 */
2386enum iwl_d3_notif {
2387	IWL_D3_NOTIF_WOWLAN_INFO =	BIT(0),
2388	IWL_D3_NOTIF_WOWLAN_WAKE_PKT =	BIT(1),
2389	IWL_D3_NOTIF_PROT_OFFLOAD =	BIT(2),
2390	IWL_D3_ND_MATCH_INFO      =     BIT(3),
2391	IWL_D3_NOTIF_D3_END_NOTIF =	BIT(4)
2392};
2393
2394/* manage d3 resume data */
2395struct iwl_d3_data {
2396	struct iwl_wowlan_status_data *status;
2397	bool test;
2398	u32 d3_end_flags;
2399	u32 notif_expected;	/* bitmap - see &enum iwl_d3_notif */
2400	u32 notif_received;	/* bitmap - see &enum iwl_d3_notif */
2401	struct iwl_mvm_nd_results *nd_results;
2402	bool nd_results_valid;
2403};
2404
2405static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
2406					    struct ieee80211_vif *vif,
2407					    struct iwl_d3_data *d3_data)
2408{
2409	struct cfg80211_wowlan_nd_info *net_detect = NULL;
2410	struct cfg80211_wowlan_wakeup wakeup = {
2411		.pattern_idx = -1,
2412	};
2413	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
2414	unsigned long matched_profiles;
2415	u32 reasons = 0;
2416	int i, n_matches, ret;
2417
2418	if (WARN_ON(!d3_data || !d3_data->status))
2419		goto out;
2420
2421	reasons = d3_data->status->wakeup_reasons;
2422
2423	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
2424		wakeup.rfkill_release = true;
2425
2426	if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
2427		goto out;
2428
2429	if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2430				     WOWLAN_INFO_NOTIFICATION, 0)) {
2431		IWL_INFO(mvm, "Query FW for ND results\n");
2432		ret = iwl_mvm_netdetect_query_results(mvm, d3_data->nd_results);
2433
2434	} else {
2435		IWL_INFO(mvm, "Notification based ND results\n");
2436		ret = d3_data->nd_results_valid ? 0 : -1;
2437	}
2438
2439	if (ret || !d3_data->nd_results->matched_profiles) {
2440		wakeup_report = NULL;
2441		goto out;
2442	}
2443
2444	matched_profiles = d3_data->nd_results->matched_profiles;
2445	if (mvm->n_nd_match_sets) {
2446		n_matches = hweight_long(matched_profiles);
2447	} else {
2448		IWL_ERR(mvm, "no net detect match information available\n");
2449		n_matches = 0;
2450	}
2451
2452	net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
2453			     GFP_KERNEL);
2454	if (!net_detect || !n_matches)
2455		goto out_report_nd;
2456
2457	for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
2458		struct cfg80211_wowlan_nd_match *match;
2459		int idx, n_channels = 0;
2460
2461		n_channels = iwl_mvm_query_num_match_chans(mvm,
2462							   d3_data->nd_results,
2463							   i);
2464
2465		match = kzalloc(struct_size(match, channels, n_channels),
2466				GFP_KERNEL);
2467		if (!match)
2468			goto out_report_nd;
2469
2470		net_detect->matches[net_detect->n_matches++] = match;
2471
2472		/* We inverted the order of the SSIDs in the scan
2473		 * request, so invert the index here.
2474		 */
2475		idx = mvm->n_nd_match_sets - i - 1;
2476		match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2477		memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2478		       match->ssid.ssid_len);
2479
2480		if (mvm->n_nd_channels < n_channels)
2481			continue;
2482
2483		iwl_mvm_query_set_freqs(mvm, d3_data->nd_results, match, i);
2484	}
2485
2486out_report_nd:
2487	wakeup.net_detect = net_detect;
2488out:
2489	iwl_mvm_free_nd(mvm);
2490
2491	mutex_unlock(&mvm->mutex);
2492	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2493
2494	if (net_detect) {
2495		for (i = 0; i < net_detect->n_matches; i++)
2496			kfree(net_detect->matches[i]);
2497		kfree(net_detect);
2498	}
2499}
2500
2501static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2502				       struct ieee80211_vif *vif)
2503{
2504	/* skip the one we keep connection on */
2505	if (data == vif)
2506		return;
2507
2508	if (vif->type == NL80211_IFTYPE_STATION)
2509		ieee80211_resume_disconnect(vif);
2510}
2511
2512static bool iwl_mvm_rt_status(struct iwl_trans *trans, u32 base, u32 *err_id)
2513{
2514	struct error_table_start {
2515		/* cf. struct iwl_error_event_table */
2516		u32 valid;
2517		__le32 err_id;
2518	} err_info;
2519
2520	if (!base)
2521		return false;
2522
2523	iwl_trans_read_mem_bytes(trans, base,
2524				 &err_info, sizeof(err_info));
2525	if (err_info.valid && err_id)
2526		*err_id = le32_to_cpu(err_info.err_id);
2527
2528	return !!err_info.valid;
2529}
2530
2531static bool iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
2532				   struct ieee80211_vif *vif)
2533{
2534	u32 err_id;
2535
2536	/* check for lmac1 error */
2537	if (iwl_mvm_rt_status(mvm->trans,
2538			      mvm->trans->dbg.lmac_error_event_table[0],
2539			      &err_id)) {
2540		if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
2541			struct cfg80211_wowlan_wakeup wakeup = {
2542				.rfkill_release = true,
2543			};
2544			ieee80211_report_wowlan_wakeup(vif, &wakeup,
2545						       GFP_KERNEL);
2546		}
2547		return true;
2548	}
2549
2550	/* check if we have lmac2 set and check for error */
2551	if (iwl_mvm_rt_status(mvm->trans,
2552			      mvm->trans->dbg.lmac_error_event_table[1], NULL))
2553		return true;
2554
2555	/* check for umac error */
2556	if (iwl_mvm_rt_status(mvm->trans,
2557			      mvm->trans->dbg.umac_error_event_table, NULL))
2558		return true;
2559
2560	return false;
2561}
2562
2563/*
2564 * This function assumes:
2565 *	1. The mutex is already held.
2566 *	2. The callee functions unlock the mutex.
2567 */
2568static bool
2569iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm *mvm,
2570				    struct ieee80211_vif *vif,
2571				    struct iwl_d3_data *d3_data)
2572{
2573	lockdep_assert_held(&mvm->mutex);
2574
2575	/* if FW uses status notification, status shouldn't be NULL here */
2576	if (!d3_data->status) {
2577		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2578		u8 sta_id = mvm->net_detect ? IWL_MVM_INVALID_STA : mvmvif->ap_sta_id;
 
2579
2580		d3_data->status = iwl_mvm_send_wowlan_get_status(mvm, sta_id);
2581	}
2582
2583	if (mvm->net_detect) {
2584		iwl_mvm_query_netdetect_reasons(mvm, vif, d3_data);
2585	} else {
2586		bool keep = iwl_mvm_query_wakeup_reasons(mvm, vif,
2587							 d3_data->status);
2588
2589#ifdef CONFIG_IWLWIFI_DEBUGFS
2590		if (keep)
2591			mvm->keep_vif = vif;
2592#endif
2593
2594		return keep;
2595	}
2596	return false;
2597}
2598
2599#define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT (IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \
2600						 IWL_WOWLAN_WAKEUP_BY_PATTERN | \
2601						 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\
2602						 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\
2603						 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\
2604						 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD)
2605
2606static int iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm *mvm,
2607					 struct iwl_wowlan_wake_pkt_notif *notif,
2608					 struct iwl_wowlan_status_data *status,
2609					 u32 len)
2610{
2611	u32 data_size, packet_len = le32_to_cpu(notif->wake_packet_length);
2612
2613	if (len < sizeof(*notif)) {
2614		IWL_ERR(mvm, "Invalid WoWLAN wake packet notification!\n");
2615		return -EIO;
2616	}
2617
2618	if (WARN_ON(!status)) {
2619		IWL_ERR(mvm, "Got wake packet notification but wowlan status data is NULL\n");
2620		return -EIO;
2621	}
2622
2623	if (WARN_ON(!(status->wakeup_reasons &
2624		      IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT))) {
2625		IWL_ERR(mvm, "Got wakeup packet but wakeup reason is %x\n",
2626			status->wakeup_reasons);
2627		return -EIO;
2628	}
2629
2630	data_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif, wake_packet);
2631
2632	/* data_size got the padding from the notification, remove it. */
2633	if (packet_len < data_size)
2634		data_size = packet_len;
2635
2636	status->wake_packet = kmemdup(notif->wake_packet, data_size,
2637				      GFP_ATOMIC);
2638
2639	if (!status->wake_packet)
2640		return -ENOMEM;
2641
2642	status->wake_packet_length = packet_len;
2643	status->wake_packet_bufsize = data_size;
2644
2645	return 0;
2646}
2647
2648static void iwl_mvm_nd_match_info_handler(struct iwl_mvm *mvm,
2649					  struct iwl_d3_data *d3_data,
2650					  struct iwl_scan_offload_match_info *notif,
2651					  u32 len)
2652{
2653	struct iwl_wowlan_status_data *status = d3_data->status;
2654	struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
2655	struct iwl_mvm_nd_results *results = d3_data->nd_results;
2656	size_t i, matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2657		iwl_umac_scan_get_max_profiles(mvm->fw);
2658
2659	if (IS_ERR_OR_NULL(vif))
2660		return;
2661
2662	if (len < sizeof(struct iwl_scan_offload_match_info)) {
2663		IWL_ERR(mvm, "Invalid scan match info notification\n");
2664		return;
2665	}
2666
2667	if (!mvm->net_detect) {
2668		IWL_ERR(mvm, "Unexpected scan match info notification\n");
2669		return;
2670	}
2671
2672	if (!status || status->wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
2673		IWL_ERR(mvm,
2674			"Ignore scan match info notification: no reason\n");
2675		return;
2676	}
2677
2678#ifdef CONFIG_IWLWIFI_DEBUGFS
2679	mvm->last_netdetect_scans = le32_to_cpu(notif->n_scans_done);
2680#endif
2681
2682	results->matched_profiles = le32_to_cpu(notif->matched_profiles);
2683	IWL_INFO(mvm, "number of matched profiles=%u\n",
2684		 results->matched_profiles);
2685
2686	if (results->matched_profiles) {
2687		memcpy(results->matches, notif->matches, matches_len);
2688		d3_data->nd_results_valid = TRUE;
2689	}
2690
2691	/* no scan should be active at this point */
2692	mvm->scan_status = 0;
2693	for (i = 0; i < mvm->max_scans; i++)
2694		mvm->scan_uid_status[i] = 0;
2695}
2696
2697static bool iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data *notif_wait,
2698				  struct iwl_rx_packet *pkt, void *data)
2699{
2700	struct iwl_mvm *mvm =
2701		container_of(notif_wait, struct iwl_mvm, notif_wait);
2702	struct iwl_d3_data *d3_data = data;
2703	u32 len;
2704	int ret;
 
 
 
 
 
2705
2706	switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
2707	case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): {
2708		struct iwl_wowlan_info_notif *notif = (void *)pkt->data;
2709
2710		if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_INFO) {
2711			/* We might get two notifications due to dual bss */
2712			IWL_DEBUG_WOWLAN(mvm,
2713					 "Got additional wowlan info notification\n");
2714			break;
2715		}
2716
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2717		d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_INFO;
2718		len = iwl_rx_packet_payload_len(pkt);
2719		iwl_mvm_parse_wowlan_info_notif(mvm, notif, d3_data->status,
2720						len);
2721		if (d3_data->status &&
2722		    d3_data->status->wakeup_reasons & IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT)
2723			/* We are supposed to get also wake packet notif */
2724			d3_data->notif_expected |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
2725
2726		break;
2727	}
2728	case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): {
2729		struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data;
2730
2731		if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_WAKE_PKT) {
2732			/* We shouldn't get two wake packet notifications */
2733			IWL_ERR(mvm,
2734				"Got additional wowlan wake packet notification\n");
2735		} else {
2736			d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
2737			len =  iwl_rx_packet_payload_len(pkt);
2738			ret = iwl_mvm_wowlan_store_wake_pkt(mvm, notif,
2739							    d3_data->status,
2740							    len);
2741			if (ret)
2742				IWL_ERR(mvm,
2743					"Can't parse WOWLAN_WAKE_PKT_NOTIFICATION\n");
2744		}
2745
2746		break;
2747	}
2748	case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): {
2749		struct iwl_scan_offload_match_info *notif = (void *)pkt->data;
2750
2751		if (d3_data->notif_received & IWL_D3_ND_MATCH_INFO) {
2752			IWL_ERR(mvm,
2753				"Got additional netdetect match info\n");
2754			break;
2755		}
2756
2757		d3_data->notif_received |= IWL_D3_ND_MATCH_INFO;
2758
2759		/* explicitly set this in the 'expected' as well */
2760		d3_data->notif_expected |= IWL_D3_ND_MATCH_INFO;
2761
2762		len = iwl_rx_packet_payload_len(pkt);
2763		iwl_mvm_nd_match_info_handler(mvm, d3_data, notif, len);
2764		break;
2765	}
2766	case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): {
2767		struct iwl_mvm_d3_end_notif *notif = (void *)pkt->data;
2768
2769		d3_data->d3_end_flags = __le32_to_cpu(notif->flags);
2770		d3_data->notif_received |= IWL_D3_NOTIF_D3_END_NOTIF;
2771
2772		break;
2773	}
2774	default:
2775		WARN_ON(1);
2776	}
2777
2778	return d3_data->notif_received == d3_data->notif_expected;
2779}
2780
2781static int iwl_mvm_resume_firmware(struct iwl_mvm *mvm, bool test)
2782{
2783	int ret;
2784	enum iwl_d3_status d3_status;
2785	struct iwl_host_cmd cmd = {
2786			.id = D0I3_END_CMD,
2787			.flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
2788		};
2789	bool reset = fw_has_capa(&mvm->fw->ucode_capa,
2790				 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2791
2792	ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !reset);
2793	if (ret)
2794		return ret;
2795
2796	if (d3_status != IWL_D3_STATUS_ALIVE) {
2797		IWL_INFO(mvm, "Device was reset during suspend\n");
2798		return -ENOENT;
2799	}
2800
2801	/*
2802	 * We should trigger resume flow using command only for 22000 family
2803	 * AX210 and above don't need the command since they have
2804	 * the doorbell interrupt.
2805	 */
2806	if (mvm->trans->trans_cfg->device_family <= IWL_DEVICE_FAMILY_22000 &&
2807	    fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_D0I3_END_FIRST)) {
2808		ret = iwl_mvm_send_cmd(mvm, &cmd);
2809		if (ret < 0)
2810			IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
2811				ret);
2812	}
2813
2814	return ret;
2815}
2816
2817#define IWL_MVM_D3_NOTIF_TIMEOUT (HZ / 5)
2818
2819static int iwl_mvm_d3_notif_wait(struct iwl_mvm *mvm,
2820				 struct iwl_d3_data *d3_data)
2821{
2822	static const u16 d3_resume_notif[] = {
2823		WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION),
2824		WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION),
2825		WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF),
2826		WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
2827	};
2828	struct iwl_notification_wait wait_d3_notif;
2829	int ret;
2830
2831	iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
2832				   d3_resume_notif, ARRAY_SIZE(d3_resume_notif),
2833				   iwl_mvm_wait_d3_notif, d3_data);
2834
2835	ret = iwl_mvm_resume_firmware(mvm, d3_data->test);
2836	if (ret) {
2837		iwl_remove_notification(&mvm->notif_wait, &wait_d3_notif);
2838		return ret;
2839	}
2840
2841	return iwl_wait_notification(&mvm->notif_wait, &wait_d3_notif,
2842				     IWL_MVM_D3_NOTIF_TIMEOUT);
2843}
2844
2845static inline bool iwl_mvm_d3_resume_notif_based(struct iwl_mvm *mvm)
2846{
2847	return iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2848				       WOWLAN_INFO_NOTIFICATION, 0) &&
2849		iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2850					WOWLAN_WAKE_PKT_NOTIFICATION, 0) &&
2851		iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2852					D3_END_NOTIFICATION, 0);
2853}
2854
2855static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
2856{
2857	struct ieee80211_vif *vif = NULL;
2858	int ret = 1;
2859	struct iwl_mvm_nd_results results = {};
2860	struct iwl_d3_data d3_data = {
2861		.test = test,
2862		.notif_expected =
2863			IWL_D3_NOTIF_WOWLAN_INFO |
2864			IWL_D3_NOTIF_D3_END_NOTIF,
2865		.nd_results_valid = false,
2866		.nd_results = &results,
2867	};
2868	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2869					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2870	bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
2871				      IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
2872	bool resume_notif_based = iwl_mvm_d3_resume_notif_based(mvm);
2873	bool keep = false;
2874
2875	mutex_lock(&mvm->mutex);
2876
2877	mvm->last_reset_or_resume_time_jiffies = jiffies;
2878
2879	/* get the BSS vif pointer again */
2880	vif = iwl_mvm_get_bss_vif(mvm);
2881	if (IS_ERR_OR_NULL(vif))
2882		goto err;
2883
2884	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
2885
2886	if (iwl_mvm_check_rt_status(mvm, vif)) {
2887		set_bit(STATUS_FW_ERROR, &mvm->trans->status);
2888		iwl_mvm_dump_nic_error_log(mvm);
2889		iwl_dbg_tlv_time_point(&mvm->fwrt,
2890				       IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL);
2891		iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
2892					false, 0);
2893		ret = 1;
2894		goto err;
2895	}
2896
2897	if (resume_notif_based) {
2898		d3_data.status = kzalloc(sizeof(*d3_data.status), GFP_KERNEL);
2899		if (!d3_data.status) {
2900			IWL_ERR(mvm, "Failed to allocate wowlan status\n");
2901			ret = -ENOMEM;
2902			goto err;
2903		}
2904
2905		ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
2906		if (ret)
2907			goto err;
2908	} else {
2909		ret = iwl_mvm_resume_firmware(mvm, test);
2910		if (ret < 0)
2911			goto err;
2912	}
2913
2914	/* after the successful handshake, we're out of D3 */
2915	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2916
2917	/* when reset is required we can't send these following commands */
2918	if (d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)
2919		goto query_wakeup_reasons;
2920
2921	/*
2922	 * Query the current location and source from the D3 firmware so we
2923	 * can play it back when we re-intiailize the D0 firmware
2924	 */
2925	iwl_mvm_update_changed_regdom(mvm);
2926
2927	/* Re-configure PPAG settings */
2928	iwl_mvm_ppag_send_cmd(mvm);
2929
2930	if (!unified_image)
2931		/*  Re-configure default SAR profile */
2932		iwl_mvm_sar_select_profile(mvm, 1, 1);
2933
2934	if (mvm->net_detect && unified_image) {
2935		/* If this is a non-unified image, we restart the FW,
2936		 * so no need to stop the netdetect scan.  If that
2937		 * fails, continue and try to get the wake-up reasons,
2938		 * but trigger a HW restart by keeping a failure code
2939		 * in ret.
2940		 */
2941		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
2942					false);
2943	}
2944
2945query_wakeup_reasons:
2946	keep = iwl_mvm_choose_query_wakeup_reasons(mvm, vif, &d3_data);
2947	/* has unlocked the mutex, so skip that */
2948	goto out;
2949
2950err:
2951	mutex_unlock(&mvm->mutex);
2952out:
2953	if (d3_data.status)
2954		kfree(d3_data.status->wake_packet);
2955	kfree(d3_data.status);
2956	iwl_mvm_free_nd(mvm);
2957
2958	if (!d3_data.test && !mvm->net_detect)
2959		ieee80211_iterate_active_interfaces_mtx(mvm->hw,
2960							IEEE80211_IFACE_ITER_NORMAL,
2961							iwl_mvm_d3_disconnect_iter,
2962							keep ? vif : NULL);
2963
2964	clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
2965
2966	/* no need to reset the device in unified images, if successful */
2967	if (unified_image && !ret) {
2968		/* nothing else to do if we already sent D0I3_END_CMD */
2969		if (d0i3_first)
2970			return 0;
2971
2972		if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2973					     D3_END_NOTIFICATION, 0)) {
2974			ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
2975			if (!ret)
2976				return 0;
2977		} else if (!(d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)) {
2978			return 0;
2979		}
2980	}
2981
2982	/*
2983	 * Reconfigure the device in one of the following cases:
2984	 * 1. We are not using a unified image
2985	 * 2. We are using a unified image but had an error while exiting D3
2986	 */
2987	set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
2988
2989	/* regardless of what happened, we're now out of D3 */
2990	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2991
2992	return 1;
2993}
2994
2995int iwl_mvm_resume(struct ieee80211_hw *hw)
2996{
2997	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2998	int ret;
2999
3000	ret = __iwl_mvm_resume(mvm, false);
3001
3002	iwl_mvm_resume_tcm(mvm);
3003
3004	iwl_fw_runtime_resume(&mvm->fwrt);
3005
3006	return ret;
3007}
3008
3009void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
3010{
3011	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3012
3013	device_set_wakeup_enable(mvm->trans->dev, enabled);
3014}
3015
3016#ifdef CONFIG_IWLWIFI_DEBUGFS
3017static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
3018{
3019	struct iwl_mvm *mvm = inode->i_private;
3020	int err;
3021
3022	if (mvm->d3_test_active)
3023		return -EBUSY;
3024
3025	file->private_data = inode->i_private;
3026
3027	iwl_mvm_pause_tcm(mvm, true);
3028
3029	iwl_fw_runtime_suspend(&mvm->fwrt);
3030
3031	/* start pseudo D3 */
3032	rtnl_lock();
3033	wiphy_lock(mvm->hw->wiphy);
3034	err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
3035	wiphy_unlock(mvm->hw->wiphy);
3036	rtnl_unlock();
3037	if (err > 0)
3038		err = -EINVAL;
3039	if (err)
3040		return err;
3041
3042	mvm->d3_test_active = true;
3043	mvm->keep_vif = NULL;
3044	return 0;
3045}
3046
3047static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
3048				    size_t count, loff_t *ppos)
3049{
3050	struct iwl_mvm *mvm = file->private_data;
3051	u32 pme_asserted;
3052
3053	while (true) {
3054		/* read pme_ptr if available */
3055		if (mvm->d3_test_pme_ptr) {
3056			pme_asserted = iwl_trans_read_mem32(mvm->trans,
3057						mvm->d3_test_pme_ptr);
3058			if (pme_asserted)
3059				break;
3060		}
3061
3062		if (msleep_interruptible(100))
3063			break;
3064	}
3065
3066	return 0;
3067}
3068
3069static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
3070					      struct ieee80211_vif *vif)
3071{
3072	/* skip the one we keep connection on */
3073	if (_data == vif)
3074		return;
3075
3076	if (vif->type == NL80211_IFTYPE_STATION)
3077		ieee80211_connection_loss(vif);
3078}
3079
3080static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
3081{
3082	struct iwl_mvm *mvm = inode->i_private;
3083	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3084					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3085
3086	mvm->d3_test_active = false;
3087
3088	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3089
3090	rtnl_lock();
3091	wiphy_lock(mvm->hw->wiphy);
3092	__iwl_mvm_resume(mvm, true);
3093	wiphy_unlock(mvm->hw->wiphy);
3094	rtnl_unlock();
3095
3096	iwl_mvm_resume_tcm(mvm);
3097
3098	iwl_fw_runtime_resume(&mvm->fwrt);
3099
3100	iwl_abort_notification_waits(&mvm->notif_wait);
3101	if (!unified_image) {
3102		int remaining_time = 10;
3103
3104		ieee80211_restart_hw(mvm->hw);
3105
3106		/* wait for restart and disconnect all interfaces */
3107		while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3108		       remaining_time > 0) {
3109			remaining_time--;
3110			msleep(1000);
3111		}
3112
3113		if (remaining_time == 0)
3114			IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
3115	}
3116
3117	ieee80211_iterate_active_interfaces_atomic(
3118		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
3119		iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
3120
3121	return 0;
3122}
3123
3124const struct file_operations iwl_dbgfs_d3_test_ops = {
3125	.llseek = no_llseek,
3126	.open = iwl_mvm_d3_test_open,
3127	.read = iwl_mvm_d3_test_read,
3128	.release = iwl_mvm_d3_test_release,
3129};
3130#endif