d3.c 58.2 KB
Newer Older
J
Johannes Berg 已提交
1 2 3 4 5 6 7
/******************************************************************************
 *
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
8
 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9
 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10
 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11
 * Copyright(c) 2018        Intel Corporation
J
Johannes Berg 已提交
12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * The full GNU General Public License is included in this distribution
23
 * in the file called COPYING.
J
Johannes Berg 已提交
24 25
 *
 * Contact Information:
26
 *  Intel Linux Wireless <linuxwifi@intel.com>
J
Johannes Berg 已提交
27 28 29 30
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 * BSD LICENSE
 *
31
 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32
 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33
 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34
 * Copyright(c) 2018        Intel Corporation
J
Johannes Berg 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

65
#include <linux/etherdevice.h>
66
#include <linux/ip.h>
67
#include <linux/fs.h>
J
Johannes Berg 已提交
68 69
#include <net/cfg80211.h>
#include <net/ipv6.h>
70
#include <net/tcp.h>
71
#include <net/addrconf.h>
J
Johannes Berg 已提交
72 73 74 75 76 77 78 79 80 81 82
#include "iwl-modparams.h"
#include "fw-api.h"
#include "mvm.h"

void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
			    struct ieee80211_vif *vif,
			    struct cfg80211_gtk_rekey_data *data)
{
	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);

83
	if (iwlwifi_mod_params.swcrypto)
J
Johannes Berg 已提交
84 85 86 87 88 89 90
		return;

	mutex_lock(&mvm->mutex);

	memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
	memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
	mvmvif->rekey_data.replay_ctr =
91
		cpu_to_le64(be64_to_cpup((__be64 *)data->replay_ctr));
J
Johannes Berg 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105
	mvmvif->rekey_data.valid = true;

	mutex_unlock(&mvm->mutex);
}

#if IS_ENABLED(CONFIG_IPV6)
void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
			      struct ieee80211_vif *vif,
			      struct inet6_dev *idev)
{
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
	struct inet6_ifaddr *ifa;
	int idx = 0;

106 107
	memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));

108
	read_lock_bh(&idev->lock);
J
Johannes Berg 已提交
109 110
	list_for_each_entry(ifa, &idev->addr_list, if_list) {
		mvmvif->target_ipv6_addrs[idx] = ifa->addr;
111 112
		if (ifa->flags & IFA_F_TENTATIVE)
			__set_bit(idx, mvmvif->tentative_addrs);
J
Johannes Berg 已提交
113
		idx++;
114
		if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
J
Johannes Berg 已提交
115 116
			break;
	}
117
	read_unlock_bh(&idev->lock);
J
Johannes Berg 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

	mvmvif->num_target_ipv6_addrs = idx;
}
#endif

void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
				     struct ieee80211_vif *vif, int idx)
{
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);

	mvmvif->tx_key_idx = idx;
}

static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
{
	int i;

	for (i = 0; i < IWL_P1K_SIZE; i++)
		out[i] = cpu_to_le16(p1k[i]);
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
				     struct iwl_mvm_key_pn *ptk_pn,
				     struct ieee80211_key_seq *seq,
				     int tid, int queues)
{
	const u8 *ret = seq->ccmp.pn;
	int i;

	/* get the PN from mac80211, used on the default queue */
	ieee80211_get_key_rx_seq(key, tid, seq);

	/* and use the internal data for the other queues */
	for (i = 1; i < queues; i++) {
		const u8 *tmp = ptk_pn->q[i].pn[tid];

		if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
			ret = tmp;
	}

	return ret;
}

J
Johannes Berg 已提交
161 162 163
struct wowlan_key_data {
	struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
	struct iwl_wowlan_tkip_params_cmd *tkip;
164
	bool error, use_rsc_tsc, use_tkip, configure_keys;
165
	int wep_key_idx;
J
Johannes Berg 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
};

static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
					struct ieee80211_vif *vif,
					struct ieee80211_sta *sta,
					struct ieee80211_key_conf *key,
					void *_data)
{
	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
	struct wowlan_key_data *data = _data;
	struct aes_sc *aes_sc, *aes_tx_sc = NULL;
	struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
	struct iwl_p1k_cache *rx_p1ks;
	u8 *rx_mic_key;
	struct ieee80211_key_seq seq;
	u32 cur_rx_iv32 = 0;
	u16 p1k[IWL_P1K_SIZE];
	int ret, i;

	switch (key->cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
		struct {
			struct iwl_mvm_wep_key_cmd wep_key_cmd;
			struct iwl_mvm_wep_key wep_key;
		} __packed wkc = {
			.wep_key_cmd.mac_id_n_color =
				cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
								mvmvif->color)),
			.wep_key_cmd.num_keys = 1,
			/* firmware sets STA_KEY_FLG_WEP_13BYTES */
			.wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
			.wep_key.key_index = key->keyidx,
			.wep_key.key_size = key->keylen,
		};

		/*
		 * This will fail -- the key functions don't set support
		 * pairwise WEP keys. However, that's better than silently
		 * failing WoWLAN. Or maybe not?
		 */
		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
			break;

		memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
		if (key->keyidx == mvmvif->tx_key_idx) {
			/* TX key must be at offset 0 */
			wkc.wep_key.key_offset = 0;
		} else {
			/* others start at 1 */
217 218
			data->wep_key_idx++;
			wkc.wep_key.key_offset = data->wep_key_idx;
J
Johannes Berg 已提交
219 220
		}

221 222 223 224 225 226 227 228 229 230 231 232
		if (data->configure_keys) {
			mutex_lock(&mvm->mutex);
			ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0,
						   sizeof(wkc), &wkc);
			data->error = ret != 0;

			mvm->ptk_ivlen = key->iv_len;
			mvm->ptk_icvlen = key->icv_len;
			mvm->gtk_ivlen = key->iv_len;
			mvm->gtk_icvlen = key->icv_len;
			mutex_unlock(&mvm->mutex);
		}
233

J
Johannes Berg 已提交
234
		/* don't upload key again */
235
		return;
J
Johannes Berg 已提交
236 237 238
	}
	default:
		data->error = true;
239
		return;
J
Johannes Berg 已提交
240 241 242 243 244 245 246 247
	case WLAN_CIPHER_SUITE_AES_CMAC:
		/*
		 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
		 * but we also shouldn't abort suspend due to that. It does have
		 * support for the IGTK key renewal, but doesn't really use the
		 * IGTK for anything. This means we could spuriously wake up or
		 * be deauthenticated, but that was considered acceptable.
		 */
248
		return;
J
Johannes Berg 已提交
249 250
	case WLAN_CIPHER_SUITE_TKIP:
		if (sta) {
251 252
			u64 pn64;

J
Johannes Berg 已提交
253 254 255 256 257
			tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
			tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;

			rx_p1ks = data->tkip->rx_uni;

258 259 260
			pn64 = atomic64_read(&key->tx_pn);
			tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
			tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
J
Johannes Berg 已提交
261

262 263
			ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
						  p1k);
J
Johannes Berg 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
			iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);

			memcpy(data->tkip->mic_keys.tx,
			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
			       IWL_MIC_KEY_SIZE);

			rx_mic_key = data->tkip->mic_keys.rx_unicast;
		} else {
			tkip_sc =
				data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
			rx_p1ks = data->tkip->rx_multi;
			rx_mic_key = data->tkip->mic_keys.rx_mcast;
		}

		/*
		 * For non-QoS this relies on the fact that both the uCode and
		 * mac80211 use TID 0 (as they need to to avoid replay attacks)
		 * for checking the IV in the frames.
		 */
		for (i = 0; i < IWL_NUM_RSC; i++) {
			ieee80211_get_key_rx_seq(key, i, &seq);
			tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
			tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
			/* wrapping isn't allowed, AP must rekey */
			if (seq.tkip.iv32 > cur_rx_iv32)
				cur_rx_iv32 = seq.tkip.iv32;
		}

		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
					  cur_rx_iv32, p1k);
		iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
		ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
					  cur_rx_iv32 + 1, p1k);
		iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);

		memcpy(rx_mic_key,
		       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
		       IWL_MIC_KEY_SIZE);

		data->use_tkip = true;
		data->use_rsc_tsc = true;
		break;
	case WLAN_CIPHER_SUITE_CCMP:
		if (sta) {
308
			u64 pn64;
J
Johannes Berg 已提交
309 310 311 312

			aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
			aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;

313 314
			pn64 = atomic64_read(&key->tx_pn);
			aes_tx_sc->pn = cpu_to_le64(pn64);
J
Johannes Berg 已提交
315 316 317 318 319 320
		} else {
			aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
		}

		/*
		 * For non-QoS this relies on the fact that both the uCode and
321
		 * mac80211/our RX code use TID 0 for checking the PN.
J
Johannes Berg 已提交
322
		 */
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
		if (sta && iwl_mvm_has_new_rx_api(mvm)) {
			struct iwl_mvm_sta *mvmsta;
			struct iwl_mvm_key_pn *ptk_pn;
			const u8 *pn;

			mvmsta = iwl_mvm_sta_from_mac80211(sta);
			ptk_pn = rcu_dereference_protected(
						mvmsta->ptk_pn[key->keyidx],
						lockdep_is_held(&mvm->mutex));
			if (WARN_ON(!ptk_pn))
				break;

			for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
				pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
						mvm->trans->num_rx_queues);
				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
							   ((u64)pn[4] << 8) |
							   ((u64)pn[3] << 16) |
							   ((u64)pn[2] << 24) |
							   ((u64)pn[1] << 32) |
							   ((u64)pn[0] << 40));
			}
		} else {
			for (i = 0; i < IWL_NUM_RSC; i++) {
				u8 *pn = seq.ccmp.pn;

				ieee80211_get_key_rx_seq(key, i, &seq);
				aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
							   ((u64)pn[4] << 8) |
							   ((u64)pn[3] << 16) |
							   ((u64)pn[2] << 24) |
							   ((u64)pn[1] << 32) |
							   ((u64)pn[0] << 40));
			}
J
Johannes Berg 已提交
357 358 359 360 361
		}
		data->use_rsc_tsc = true;
		break;
	}

362 363
	if (data->configure_keys) {
		mutex_lock(&mvm->mutex);
364
		/*
365 366
		 * The D3 firmware hardcodes the key offset 0 as the key it
		 * uses to transmit packets to the AP, i.e. the PTK.
367
		 */
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
			mvm->ptk_ivlen = key->iv_len;
			mvm->ptk_icvlen = key->icv_len;
			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
		} else {
			/*
			 * firmware only supports TSC/RSC for a single key,
			 * so if there are multiple keep overwriting them
			 * with new ones -- this relies on mac80211 doing
			 * list_add_tail().
			 */
			mvm->gtk_ivlen = key->iv_len;
			mvm->gtk_icvlen = key->icv_len;
			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
		}
		mutex_unlock(&mvm->mutex);
		data->error = ret != 0;
J
Johannes Berg 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	}
}

static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
				 struct cfg80211_wowlan *wowlan)
{
	struct iwl_wowlan_patterns_cmd *pattern_cmd;
	struct iwl_host_cmd cmd = {
		.id = WOWLAN_PATTERNS,
		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
	};
	int i, err;

	if (!wowlan->n_patterns)
		return 0;

	cmd.len[0] = sizeof(*pattern_cmd) +
		wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);

	pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
	if (!pattern_cmd)
		return -ENOMEM;

	pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);

	for (i = 0; i < wowlan->n_patterns; i++) {
		int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);

		memcpy(&pattern_cmd->patterns[i].mask,
		       wowlan->patterns[i].mask, mask_len);
		memcpy(&pattern_cmd->patterns[i].pattern,
		       wowlan->patterns[i].pattern,
		       wowlan->patterns[i].pattern_len);
		pattern_cmd->patterns[i].mask_size = mask_len;
		pattern_cmd->patterns[i].pattern_size =
			wowlan->patterns[i].pattern_len;
	}

	cmd.data[0] = pattern_cmd;
	err = iwl_mvm_send_cmd(mvm, &cmd);
	kfree(pattern_cmd);
	return err;
}

static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
				struct ieee80211_sta *ap_sta)
{
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
	struct ieee80211_chanctx_conf *ctx;
	u8 chains_static, chains_dynamic;
	struct cfg80211_chan_def chandef;
	int ret, i;
437
	struct iwl_binding_cmd_v1 binding_cmd = {};
J
Johannes Berg 已提交
438
	struct iwl_time_quota_cmd quota_cmd = {};
439
	struct iwl_time_quota_data *quota;
J
Johannes Berg 已提交
440
	u32 status;
441 442 443

	if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm)))
		return -EINVAL;
J
Johannes Berg 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

	/* add back the PHY */
	if (WARN_ON(!mvmvif->phy_ctxt))
		return -EINVAL;

	rcu_read_lock();
	ctx = rcu_dereference(vif->chanctx_conf);
	if (WARN_ON(!ctx)) {
		rcu_read_unlock();
		return -EINVAL;
	}
	chandef = ctx->def;
	chains_static = ctx->rx_chains_static;
	chains_dynamic = ctx->rx_chains_dynamic;
	rcu_read_unlock();

	ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
				   chains_static, chains_dynamic);
	if (ret)
		return ret;

	/* add back the MAC */
	mvmvif->uploaded = false;

	if (WARN_ON(!vif->bss_conf.assoc))
		return -EINVAL;
470

J
Johannes Berg 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
	if (ret)
		return ret;

	/* add back binding - XXX refactor? */
	binding_cmd.id_and_color =
		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
						mvmvif->phy_ctxt->color));
	binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
	binding_cmd.phy =
		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
						mvmvif->phy_ctxt->color));
	binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
							      mvmvif->color));
	for (i = 1; i < MAX_MACS_IN_BINDING; i++)
		binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);

	status = 0;
	ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
490 491
					  IWL_BINDING_CMD_SIZE_V1, &binding_cmd,
					  &status);
J
Johannes Berg 已提交
492 493 494 495 496 497 498 499 500 501
	if (ret) {
		IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
		return ret;
	}

	if (status) {
		IWL_ERR(mvm, "Binding command failed: %u\n", status);
		return -EIO;
	}

502
	ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
J
Johannes Berg 已提交
503 504 505 506
	if (ret)
		return ret;
	rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);

507
	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
J
Johannes Berg 已提交
508 509 510 511
	if (ret)
		return ret;

	/* and some quota */
512 513
	quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, 0);
	quota->id_and_color =
J
Johannes Berg 已提交
514 515
		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
						mvmvif->phy_ctxt->color));
516 517
	quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
	quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
J
Johannes Berg 已提交
518

519 520 521 522
	for (i = 1; i < MAX_BINDINGS; i++) {
		quota = iwl_mvm_quota_cmd_get_quota(mvm, &quota_cmd, i);
		quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
	}
J
Johannes Berg 已提交
523

E
Emmanuel Grumbach 已提交
524
	ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
525
				   iwl_mvm_quota_cmd_size(mvm), &quota_cmd);
J
Johannes Berg 已提交
526 527 528
	if (ret)
		IWL_ERR(mvm, "Failed to send quota: %d\n", ret);

529 530 531
	if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
		IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");

J
Johannes Berg 已提交
532 533 534
	return 0;
}

535 536 537
static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
				       struct ieee80211_vif *vif)
{
538 539 540 541 542 543 544
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
	struct iwl_nonqos_seq_query_cmd query_cmd = {
		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
		.mac_id_n_color =
			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
							mvmvif->color)),
	};
545 546
	struct iwl_host_cmd cmd = {
		.id = NON_QOS_TX_COUNTER_CMD,
E
Emmanuel Grumbach 已提交
547
		.flags = CMD_WANT_SKB,
548 549 550 551
	};
	int err;
	u32 size;

552 553
	cmd.data[0] = &query_cmd;
	cmd.len[0] = sizeof(query_cmd);
554

555 556 557 558
	err = iwl_mvm_send_cmd(mvm, &cmd);
	if (err)
		return err;

559
	size = iwl_rx_packet_payload_len(cmd.resp_pkt);
560
	if (size < sizeof(__le16)) {
561
		err = -EINVAL;
562 563
	} else {
		err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
564 565
		/* firmware returns next, not last-used seqno */
		err = (u16) (err - 0x10);
566
	}
567 568 569 570 571

	iwl_free_resp(&cmd);
	return err;
}

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
{
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
	struct iwl_nonqos_seq_query_cmd query_cmd = {
		.get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
		.mac_id_n_color =
			cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
							mvmvif->color)),
		.value = cpu_to_le16(mvmvif->seqno),
	};

	/* return if called during restart, not resume from D3 */
	if (!mvmvif->seqno_valid)
		return;

	mvmvif->seqno_valid = false;

E
Emmanuel Grumbach 已提交
589
	if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
590 591 592 593
				 sizeof(query_cmd), &query_cmd))
		IWL_ERR(mvm, "failed to set non-QoS seqno\n");
}

594 595
static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
{
596
	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
597

598
	iwl_mvm_stop_device(mvm);
599 600 601 602 603 604 605 606 607 608 609 610
	/*
	 * Set the HW restart bit -- this is mostly true as we're
	 * going to load new firmware and reprogram that, though
	 * the reprogramming is going to be manual to avoid adding
	 * all the MACs that aren't support.
	 * We don't have to clear up everything though because the
	 * reprogramming is manual. When we resume, we'll actually
	 * go through a proper restart sequence again to switch
	 * back to the runtime firmware image.
	 */
	set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);

611 612 613
	/* the fw is reset, so all the keys are cleared */
	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));

614 615 616 617 618 619 620 621 622 623 624
	mvm->ptk_ivlen = 0;
	mvm->ptk_icvlen = 0;
	mvm->ptk_ivlen = 0;
	mvm->ptk_icvlen = 0;

	return iwl_mvm_load_d3_fw(mvm);
}

static int
iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
			  struct cfg80211_wowlan *wowlan,
625
			  struct iwl_wowlan_config_cmd *wowlan_config_cmd,
626 627
			  struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
			  struct ieee80211_sta *ap_sta)
J
Johannes Berg 已提交
628
{
629
	int ret;
630
	struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
J
Johannes Berg 已提交
631

632
	/* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
J
Johannes Berg 已提交
633

634
	wowlan_config_cmd->is_11n_connection =
635
					ap_sta->ht_cap.ht_supported;
S
Sara Sharon 已提交
636
	wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
637
		ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
J
Johannes Berg 已提交
638

639 640 641
	/* Query the last used seqno and set it */
	ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
	if (ret < 0)
642
		return ret;
J
Johannes Berg 已提交
643

644
	wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
645

646
	iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
J
Johannes Berg 已提交
647 648

	if (wowlan->disconnect)
649
		wowlan_config_cmd->wakeup_filter |=
J
Johannes Berg 已提交
650 651 652
			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
	if (wowlan->magic_pkt)
653
		wowlan_config_cmd->wakeup_filter |=
J
Johannes Berg 已提交
654 655
			cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
	if (wowlan->gtk_rekey_failure)
656
		wowlan_config_cmd->wakeup_filter |=
J
Johannes Berg 已提交
657 658
			cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
	if (wowlan->eap_identity_req)
659
		wowlan_config_cmd->wakeup_filter |=
J
Johannes Berg 已提交
660 661
			cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
	if (wowlan->four_way_handshake)
662
		wowlan_config_cmd->wakeup_filter |=
J
Johannes Berg 已提交
663 664
			cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
	if (wowlan->n_patterns)
665
		wowlan_config_cmd->wakeup_filter |=
J
Johannes Berg 已提交
666 667 668
			cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);

	if (wowlan->rfkill_release)
669
		wowlan_config_cmd->wakeup_filter |=
J
Johannes Berg 已提交
670 671
			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);

672 673
	if (wowlan->tcp) {
		/*
674 675
		 * Set the "link change" (really "link lost") flag as well
		 * since that implies losing the TCP connection.
676
		 */
677
		wowlan_config_cmd->wakeup_filter |=
678 679 680 681 682 683
			cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
				    IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
				    IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
				    IWL_WOWLAN_WAKEUP_LINK_CHANGE);
	}

684 685 686 687 688 689 690 691
	if (wowlan->any) {
		wowlan_config_cmd->wakeup_filter |=
			cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
				    IWL_WOWLAN_WAKEUP_LINK_CHANGE |
				    IWL_WOWLAN_WAKEUP_RX_FRAME |
				    IWL_WOWLAN_WAKEUP_BCN_FILTERING);
	}

692 693
	return 0;
}
694

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
static void
iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
			  struct ieee80211_vif *vif,
			  void (*iter)(struct ieee80211_hw *hw,
				       struct ieee80211_vif *vif,
				       struct ieee80211_sta *sta,
				       struct ieee80211_key_conf *key,
				       void *data),
			  void *data)
{
	struct ieee80211_sta *ap_sta;

	rcu_read_lock();

	ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
	if (IS_ERR_OR_NULL(ap_sta))
		goto out;

	ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
out:
	rcu_read_unlock();
}

718 719
int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
				     struct ieee80211_vif *vif,
720
				     bool d0i3,
721
				     u32 cmd_flags)
722 723 724
{
	struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
	struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
725 726
	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
727
	struct wowlan_key_data key_data = {
728
		.configure_keys = !d0i3 && !unified,
729 730 731 732
		.use_rsc_tsc = false,
		.tkip = &tkip_cmd,
		.use_tkip = false,
	};
733 734 735 736 737 738 739 740
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
	int ret;

	key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
	if (!key_data.rsc_tsc)
		return -ENOMEM;

	/*
741 742 743 744 745
	 * if we have to configure keys, call ieee80211_iter_keys(),
	 * as we need non-atomic context in order to take the
	 * required locks.
	 * for the d0i3 we can't use ieee80211_iter_keys(), as
	 * taking (almost) any mutex might result in deadlock.
746
	 */
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
	if (!d0i3) {
		/*
		 * Note that currently we don't propagate cmd_flags
		 * to the iterator. In case of key_data.configure_keys,
		 * all the configured commands are SYNC, and
		 * iwl_mvm_wowlan_program_keys() will take care of
		 * locking/unlocking mvm->mutex.
		 */
		ieee80211_iter_keys(mvm->hw, vif,
				    iwl_mvm_wowlan_program_keys,
				    &key_data);
	} else {
		iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
					  iwl_mvm_wowlan_program_keys,
					  &key_data);
	}
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777

	if (key_data.error) {
		ret = -EIO;
		goto out;
	}

	if (key_data.use_rsc_tsc) {
		ret = iwl_mvm_send_cmd_pdu(mvm,
					   WOWLAN_TSC_RSC_PARAM, cmd_flags,
					   sizeof(*key_data.rsc_tsc),
					   key_data.rsc_tsc);
		if (ret)
			goto out;
	}

778 779 780
	if (key_data.use_tkip &&
	    !fw_has_api(&mvm->fw->ucode_capa,
			IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
781 782 783 784 785 786 787 788
		ret = iwl_mvm_send_cmd_pdu(mvm,
					   WOWLAN_TKIP_PARAM,
					   cmd_flags, sizeof(tkip_cmd),
					   &tkip_cmd);
		if (ret)
			goto out;
	}

789 790
	/* configure rekey data only if offloaded rekey is supported (d3) */
	if (mvmvif->rekey_data.valid && !d0i3) {
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
		memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
		memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
		       NL80211_KCK_LEN);
		kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
		memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
		       NL80211_KEK_LEN);
		kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
		kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;

		ret = iwl_mvm_send_cmd_pdu(mvm,
					   WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
					   sizeof(kek_kck_cmd),
					   &kek_kck_cmd);
		if (ret)
			goto out;
	}
807
	ret = 0;
808 809 810 811 812 813 814 815 816 817 818 819
out:
	kfree(key_data.rsc_tsc);
	return ret;
}

static int
iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
		      struct cfg80211_wowlan *wowlan,
		      struct iwl_wowlan_config_cmd *wowlan_config_cmd,
		      struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
		      struct ieee80211_sta *ap_sta)
{
820
	int ret;
821 822
	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
J
Johannes Berg 已提交
823

824 825 826 827
	if (!unified_image) {
		ret = iwl_mvm_switch_to_d3(mvm);
		if (ret)
			return ret;
828

829 830 831 832
		ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
		if (ret)
			return ret;
	}
833

834
	if (!iwlwifi_mod_params.swcrypto) {
J
Johannes Berg 已提交
835 836 837 838 839 840
		/*
		 * This needs to be unlocked due to lock ordering
		 * constraints. Since we're in the suspend path
		 * that isn't really a problem though.
		 */
		mutex_unlock(&mvm->mutex);
841
		ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
842
						       CMD_ASYNC);
J
Johannes Berg 已提交
843
		mutex_lock(&mvm->mutex);
844 845
		if (ret)
			return ret;
J
Johannes Berg 已提交
846 847
	}

848 849 850
	ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
				   sizeof(*wowlan_config_cmd),
				   wowlan_config_cmd);
J
Johannes Berg 已提交
851
	if (ret)
852
		return ret;
J
Johannes Berg 已提交
853 854 855

	ret = iwl_mvm_send_patterns(mvm, wowlan);
	if (ret)
856
		return ret;
J
Johannes Berg 已提交
857

858
	return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
859 860
}

861 862 863 864 865 866
static int
iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
			 struct cfg80211_wowlan *wowlan,
			 struct cfg80211_sched_scan_request *nd_config,
			 struct ieee80211_vif *vif)
{
867
	struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
868
	int ret;
869 870
	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
871

872 873 874 875
	if (!unified_image) {
		ret = iwl_mvm_switch_to_d3(mvm);
		if (ret)
			return ret;
876 877 878 879 880 881 882 883 884
	} else {
		/* In theory, we wouldn't have to stop a running sched
		 * scan in order to start another one (for
		 * net-detect).  But in practice this doesn't seem to
		 * work properly, so stop any running sched_scan now.
		 */
		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
		if (ret)
			return ret;
885
	}
886 887 888

	/* rfkill release can be either for wowlan or netdetect */
	if (wowlan->rfkill_release)
889
		wowlan_config_cmd.wakeup_filter |=
890 891
			cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);

892 893 894
	ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
				   sizeof(wowlan_config_cmd),
				   &wowlan_config_cmd);
895 896 897
	if (ret)
		return ret;

898 899
	ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
				       IWL_MVM_SCAN_NETDETECT);
900 901
	if (ret)
		return ret;
902

903
	if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
904 905
		return -EBUSY;

906
	/* save the sched scan matchsets... */
907 908 909 910 911 912 913 914 915
	if (nd_config->n_match_sets) {
		mvm->nd_match_sets = kmemdup(nd_config->match_sets,
					     sizeof(*nd_config->match_sets) *
					     nd_config->n_match_sets,
					     GFP_KERNEL);
		if (mvm->nd_match_sets)
			mvm->n_nd_match_sets = nd_config->n_match_sets;
	}

916 917 918 919 920 921 922 923
	/* ...and the sched scan channels for later reporting */
	mvm->nd_channels = kmemdup(nd_config->channels,
				   sizeof(*nd_config->channels) *
				   nd_config->n_channels,
				   GFP_KERNEL);
	if (mvm->nd_channels)
		mvm->n_nd_channels = nd_config->n_channels;

924 925 926 927 928 929 930 931
	return 0;
}

static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
{
	kfree(mvm->nd_match_sets);
	mvm->nd_match_sets = NULL;
	mvm->n_nd_match_sets = 0;
932 933 934
	kfree(mvm->nd_channels);
	mvm->nd_channels = NULL;
	mvm->n_nd_channels = 0;
935 936
}

937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
			     struct cfg80211_wowlan *wowlan,
			     bool test)
{
	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
	struct ieee80211_vif *vif = NULL;
	struct iwl_mvm_vif *mvmvif = NULL;
	struct ieee80211_sta *ap_sta = NULL;
	struct iwl_d3_manager_config d3_cfg_cmd_data = {
		/*
		 * Program the minimum sleep time to 10 seconds, as many
		 * platforms have issues processing a wakeup signal while
		 * still being in the process of suspending.
		 */
		.min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
	};
	struct iwl_host_cmd d3_cfg_cmd = {
		.id = D3_CONFIG_CMD,
		.flags = CMD_WANT_SKB,
		.data[0] = &d3_cfg_cmd_data,
		.len[0] = sizeof(d3_cfg_cmd_data),
	};
	int ret;
	int len __maybe_unused;
961 962
	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
963 964 965 966 967 968 969 970 971 972 973 974

	if (!wowlan) {
		/*
		 * mac80211 shouldn't get here, but for D3 test
		 * it doesn't warrant a warning
		 */
		WARN_ON(!test);
		return -EINVAL;
	}

	mutex_lock(&mvm->mutex);

975 976
	vif = iwl_mvm_get_bss_vif(mvm);
	if (IS_ERR_OR_NULL(vif)) {
977 978 979 980 981 982
		ret = 1;
		goto out_noreset;
	}

	mvmvif = iwl_mvm_vif_from_mac80211(vif);

983
	if (mvmvif->ap_sta_id == IWL_MVM_INVALID_STA) {
984
		/* if we're not associated, this must be netdetect */
985
		if (!wowlan->nd_config) {
986 987 988 989 990
			ret = 1;
			goto out_noreset;
		}

		ret = iwl_mvm_netdetect_config(
991
			mvm, wowlan, wowlan->nd_config, vif);
992 993
		if (ret)
			goto out;
994 995

		mvm->net_detect = true;
996
	} else {
997
		struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
998

999 1000 1001 1002 1003 1004 1005
		ap_sta = rcu_dereference_protected(
			mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
			lockdep_is_held(&mvm->mutex));
		if (IS_ERR_OR_NULL(ap_sta)) {
			ret = -EINVAL;
			goto out_noreset;
		}
1006

1007 1008 1009 1010 1011 1012 1013
		ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
						vif, mvmvif, ap_sta);
		if (ret)
			goto out_noreset;
		ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
					    vif, mvmvif, ap_sta);
		if (ret)
1014
			goto out;
1015 1016

		mvm->net_detect = false;
1017
	}
1018

1019
	ret = iwl_mvm_power_update_device(mvm);
1020 1021 1022
	if (ret)
		goto out;

1023
	ret = iwl_mvm_power_update_mac(mvm);
1024 1025 1026
	if (ret)
		goto out;

1027 1028 1029 1030 1031 1032
#ifdef CONFIG_IWLWIFI_DEBUGFS
	if (mvm->d3_wake_sysassert)
		d3_cfg_cmd_data.wakeup_flags |=
			cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
#endif

1033 1034 1035 1036 1037
	/*
	 * TODO: this is needed because the firmware is not stopping
	 * the recording automatically before entering D3.  This can
	 * be removed once the FW starts doing that.
	 */
1038
	_iwl_fw_dbg_stop_recording(mvm->fwrt.trans, NULL);
1039

J
Johannes Berg 已提交
1040
	/* must be last -- this switches firmware state */
1041
	ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
J
Johannes Berg 已提交
1042 1043
	if (ret)
		goto out;
1044
#ifdef CONFIG_IWLWIFI_DEBUGFS
1045 1046
	len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
	if (len >= sizeof(u32)) {
1047 1048 1049 1050 1051
		mvm->d3_test_pme_ptr =
			le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
	}
#endif
	iwl_free_resp(&d3_cfg_cmd);
J
Johannes Berg 已提交
1052 1053 1054

	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);

1055
	iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
J
Johannes Berg 已提交
1056
 out:
1057 1058
	if (ret < 0) {
		iwl_mvm_free_nd(mvm);
1059 1060 1061

		if (!unified_image) {
			iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1062 1063
			if (mvm->fw_restart > 0) {
				mvm->fw_restart--;
1064 1065 1066
				ieee80211_restart_hw(mvm->hw);
			}
		}
1067
	}
1068
 out_noreset:
J
Johannes Berg 已提交
1069 1070 1071 1072 1073
	mutex_unlock(&mvm->mutex);

	return ret;
}

1074 1075 1076
static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
{
	struct iwl_notification_wait wait_d3;
1077
	static const u16 d3_notif[] = { D3_CONFIG_CMD };
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	int ret;

	iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
				   d3_notif, ARRAY_SIZE(d3_notif),
				   NULL, NULL);

	ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
	if (ret)
		goto remove_notif;

	ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
	WARN_ON_ONCE(ret);
	return ret;

remove_notif:
	iwl_remove_notification(&mvm->notif_wait, &wait_d3);
	return ret;
}

1097 1098
int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
{
1099
	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1100
	struct iwl_trans *trans = mvm->trans;
1101 1102
	int ret;

1103 1104
	/* make sure the d0i3 exit work is not pending */
	flush_work(&mvm->d0i3_exit_work);
1105
	iwl_mvm_pause_tcm(mvm, true);
1106

1107 1108
	iwl_fw_runtime_suspend(&mvm->fwrt);

1109
	ret = iwl_trans_suspend(trans);
1110 1111
	if (ret)
		return ret;
1112

1113 1114 1115 1116
	if (wowlan->any) {
		trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;

		if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
1117
			ret = iwl_mvm_enter_d0i3_sync(mvm);
1118 1119 1120 1121 1122

			if (ret)
				return ret;
		}

1123 1124 1125
		mutex_lock(&mvm->d0i3_suspend_mutex);
		__set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
		mutex_unlock(&mvm->d0i3_suspend_mutex);
1126

1127
		iwl_trans_d3_suspend(trans, false, false);
1128

1129 1130 1131
		return 0;
	}

1132 1133
	trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;

1134 1135 1136
	return __iwl_mvm_suspend(hw, wowlan, false);
}

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
/* converted data from the different status responses */
struct iwl_wowlan_status_data {
	u16 pattern_number;
	u16 qos_seq_ctr[8];
	u32 wakeup_reasons;
	u32 wake_packet_length;
	u32 wake_packet_bufsize;
	const u8 *wake_packet;
};

1147 1148
static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
					  struct ieee80211_vif *vif,
1149
					  struct iwl_wowlan_status_data *status)
1150
{
1151
	struct sk_buff *pkt = NULL;
1152 1153 1154 1155
	struct cfg80211_wowlan_wakeup wakeup = {
		.pattern_idx = -1,
	};
	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1156
	u32 reasons = status->wakeup_reasons;
1157 1158 1159 1160 1161 1162

	if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
		wakeup_report = NULL;
		goto report;
	}

1163 1164
	pm_wakeup_event(mvm->dev, 0);

1165
	if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1166 1167
		wakeup.magic_pkt = true;

1168
	if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1169
		wakeup.pattern_idx =
1170
			status->pattern_number;
1171 1172 1173 1174 1175

	if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
		       IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
		wakeup.disconnect = true;

1176
	if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1177 1178
		wakeup.gtk_rekey_failure = true;

1179
	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1180 1181
		wakeup.rfkill_release = true;

1182
	if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1183 1184
		wakeup.eap_identity_req = true;

1185
	if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1186 1187
		wakeup.four_way_handshake = true;

1188 1189 1190 1191 1192 1193 1194 1195 1196
	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
		wakeup.tcp_connlost = true;

	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
		wakeup.tcp_nomoretokens = true;

	if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
		wakeup.tcp_match = true;

1197
	if (status->wake_packet_bufsize) {
1198 1199
		int pktsize = status->wake_packet_bufsize;
		int pktlen = status->wake_packet_length;
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
		const u8 *pktdata = status->wake_packet;
		struct ieee80211_hdr *hdr = (void *)pktdata;
		int truncated = pktlen - pktsize;

		/* this would be a firmware bug */
		if (WARN_ON_ONCE(truncated < 0))
			truncated = 0;

		if (ieee80211_is_data(hdr->frame_control)) {
			int hdrlen = ieee80211_hdrlen(hdr->frame_control);
			int ivlen = 0, icvlen = 4; /* also FCS */
1211 1212 1213 1214

			pkt = alloc_skb(pktsize, GFP_KERNEL);
			if (!pkt)
				goto report;
1215

1216
			skb_put_data(pkt, pktdata, hdrlen);
1217 1218 1219 1220
			pktdata += hdrlen;
			pktsize -= hdrlen;

			if (ieee80211_has_protected(hdr->frame_control)) {
1221 1222 1223 1224 1225 1226
				/*
				 * This is unlocked and using gtk_i(c)vlen,
				 * but since everything is under RTNL still
				 * that's not really a problem - changing
				 * it would be difficult.
				 */
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
				if (is_multicast_ether_addr(hdr->addr1)) {
					ivlen = mvm->gtk_ivlen;
					icvlen += mvm->gtk_icvlen;
				} else {
					ivlen = mvm->ptk_ivlen;
					icvlen += mvm->ptk_icvlen;
				}
			}

			/* if truncated, FCS/ICV is (partially) gone */
			if (truncated >= icvlen) {
				icvlen = 0;
				truncated -= icvlen;
			} else {
				icvlen -= truncated;
				truncated = 0;
			}

			pktsize -= ivlen + icvlen;
			pktdata += ivlen;

1248
			skb_put_data(pkt, pktdata, pktsize);
1249

1250 1251 1252 1253
			if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
				goto report;
			wakeup.packet = pkt->data;
			wakeup.packet_present_len = pkt->len;
1254
			wakeup.packet_len = pkt->len - truncated;
1255 1256
			wakeup.packet_80211 = false;
		} else {
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
			int fcslen = 4;

			if (truncated >= 4) {
				truncated -= 4;
				fcslen = 0;
			} else {
				fcslen -= truncated;
				truncated = 0;
			}
			pktsize -= fcslen;
1267 1268
			wakeup.packet = status->wake_packet;
			wakeup.packet_present_len = pktsize;
1269
			wakeup.packet_len = pktlen - truncated;
1270 1271 1272 1273 1274 1275 1276
			wakeup.packet_80211 = true;
		}
	}

 report:
	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
	kfree_skb(pkt);
1277
}
1278

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
				  struct ieee80211_key_seq *seq)
{
	u64 pn;

	pn = le64_to_cpu(sc->pn);
	seq->ccmp.pn[0] = pn >> 40;
	seq->ccmp.pn[1] = pn >> 32;
	seq->ccmp.pn[2] = pn >> 24;
	seq->ccmp.pn[3] = pn >> 16;
	seq->ccmp.pn[4] = pn >> 8;
	seq->ccmp.pn[5] = pn;
}

static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
				   struct ieee80211_key_seq *seq)
{
	seq->tkip.iv32 = le32_to_cpu(sc->iv32);
	seq->tkip.iv16 = le16_to_cpu(sc->iv16);
}

1300 1301
static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
				   struct ieee80211_sta *sta,
1302 1303 1304 1305 1306 1307
				   struct ieee80211_key_conf *key)
{
	int tid;

	BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);

1308 1309 1310
	if (sta && iwl_mvm_has_new_rx_api(mvm)) {
		struct iwl_mvm_sta *mvmsta;
		struct iwl_mvm_key_pn *ptk_pn;
1311

1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
		mvmsta = iwl_mvm_sta_from_mac80211(sta);

		ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
						   lockdep_is_held(&mvm->mutex));
		if (WARN_ON(!ptk_pn))
			return;

		for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
			struct ieee80211_key_seq seq = {};
			int i;

			iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
			ieee80211_set_key_rx_seq(key, tid, &seq);
			for (i = 1; i < mvm->trans->num_rx_queues; i++)
				memcpy(ptk_pn->q[i].pn[tid],
				       seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
		}
	} else {
		for (tid = 0; tid < IWL_NUM_RSC; tid++) {
			struct ieee80211_key_seq seq = {};

			iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
			ieee80211_set_key_rx_seq(key, tid, &seq);
		}
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
	}
}

static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
				    struct ieee80211_key_conf *key)
{
	int tid;

	BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);

	for (tid = 0; tid < IWL_NUM_RSC; tid++) {
		struct ieee80211_key_seq seq = {};

		iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
		ieee80211_set_key_rx_seq(key, tid, &seq);
	}
}

1354 1355
static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
				   struct ieee80211_key_conf *key,
1356
				   struct iwl_wowlan_status *status)
1357
{
1358
	union iwl_all_tsc_rsc *rsc = &status->gtk[0].rsc.all_tsc_rsc;
1359 1360 1361

	switch (key->cipher) {
	case WLAN_CIPHER_SUITE_CCMP:
1362
		iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
		break;
	case WLAN_CIPHER_SUITE_TKIP:
		iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
		break;
	default:
		WARN_ON(1);
	}
}

struct iwl_mvm_d3_gtk_iter_data {
1373
	struct iwl_mvm *mvm;
1374
	struct iwl_wowlan_status *status;
1375 1376 1377 1378 1379 1380
	void *last_gtk;
	u32 cipher;
	bool find_phase, unhandled_cipher;
	int num_keys;
};

1381
static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
				   struct ieee80211_vif *vif,
				   struct ieee80211_sta *sta,
				   struct ieee80211_key_conf *key,
				   void *_data)
{
	struct iwl_mvm_d3_gtk_iter_data *data = _data;

	if (data->unhandled_cipher)
		return;

	switch (key->cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_WEP104:
		/* ignore WEP completely, nothing to do */
		return;
	case WLAN_CIPHER_SUITE_CCMP:
	case WLAN_CIPHER_SUITE_TKIP:
		/* we support these */
		break;
	default:
		/* everything else (even CMAC for MFP) - disconnect from AP */
		data->unhandled_cipher = true;
		return;
	}

	data->num_keys++;

	/*
	 * pairwise key - update sequence counters only;
	 * note that this assumes no TDLS sessions are active
	 */
	if (sta) {
		struct ieee80211_key_seq seq = {};
1415 1416
		union iwl_all_tsc_rsc *sc =
			&data->status->gtk[0].rsc.all_tsc_rsc;
1417 1418 1419 1420 1421 1422

		if (data->find_phase)
			return;

		switch (key->cipher) {
		case WLAN_CIPHER_SUITE_CCMP:
1423 1424
			iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
					       sta, key);
1425
			atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
1426 1427 1428 1429
			break;
		case WLAN_CIPHER_SUITE_TKIP:
			iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
			iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1430 1431 1432
			atomic64_set(&key->tx_pn,
				     (u64)seq.tkip.iv16 |
				     ((u64)seq.tkip.iv32 << 16));
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
			break;
		}

		/* that's it for this key */
		return;
	}

	if (data->find_phase) {
		data->last_gtk = key;
		data->cipher = key->cipher;
		return;
	}

	if (data->status->num_of_gtk_rekeys)
		ieee80211_remove_key(key);
	else if (data->last_gtk == key)
1449
		iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
1450 1451 1452 1453
}

static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
					  struct ieee80211_vif *vif,
1454
					  struct iwl_wowlan_status *status)
1455 1456 1457
{
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
	struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1458
		.mvm = mvm,
1459 1460
		.status = status,
	};
1461 1462 1463
	u32 disconnection_reasons =
		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
		IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1464 1465 1466 1467

	if (!status || !vif->bss_conf.bssid)
		return false;

1468 1469 1470
	if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
		return false;

1471 1472 1473
	/* find last GTK that we used initially, if any */
	gtkdata.find_phase = true;
	ieee80211_iter_keys(mvm->hw, vif,
1474
			    iwl_mvm_d3_update_keys, &gtkdata);
1475 1476 1477 1478
	/* not trying to keep connections with MFP/unhandled ciphers */
	if (gtkdata.unhandled_cipher)
		return false;
	if (!gtkdata.num_keys)
1479
		goto out;
1480 1481 1482 1483 1484 1485 1486 1487 1488
	if (!gtkdata.last_gtk)
		return false;

	/*
	 * invalidate all other GTKs that might still exist and update
	 * the one that we used
	 */
	gtkdata.find_phase = false;
	ieee80211_iter_keys(mvm->hw, vif,
1489
			    iwl_mvm_d3_update_keys, &gtkdata);
1490 1491 1492 1493 1494 1495 1496 1497

	if (status->num_of_gtk_rekeys) {
		struct ieee80211_key_conf *key;
		struct {
			struct ieee80211_key_conf conf;
			u8 key[32];
		} conf = {
			.conf.cipher = gtkdata.cipher,
1498 1499
			.conf.keyidx =
				iwlmvm_wowlan_gtk_idx(&status->gtk[0]),
1500
		};
1501
		__be64 replay_ctr;
1502 1503 1504 1505

		switch (gtkdata.cipher) {
		case WLAN_CIPHER_SUITE_CCMP:
			conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1506
			memcpy(conf.conf.key, status->gtk[0].key,
1507 1508 1509 1510
			       WLAN_KEY_LEN_CCMP);
			break;
		case WLAN_CIPHER_SUITE_TKIP:
			conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1511
			memcpy(conf.conf.key, status->gtk[0].key, 16);
1512 1513 1514
			/* leave TX MIC key zeroed, we don't use it anyway */
			memcpy(conf.conf.key +
			       NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1515
			       status->gtk[0].tkip_mic_key, 8);
1516 1517 1518 1519 1520 1521
			break;
		}

		key = ieee80211_gtk_rekey_add(vif, &conf.conf);
		if (IS_ERR(key))
			return false;
1522
		iwl_mvm_set_key_rx_seq(mvm, key, status);
1523

1524 1525
		replay_ctr =
			cpu_to_be64(le64_to_cpu(status->replay_ctr));
1526

1527 1528 1529 1530
		ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
					   (void *)&replay_ctr, GFP_KERNEL);
	}

1531
out:
1532 1533 1534 1535 1536 1537 1538
	mvmvif->seqno_valid = true;
	/* +0x10 because the set API expects next-to-use, not last-used */
	mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;

	return true;
}

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
struct iwl_wowlan_status *iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm)
{
	struct iwl_wowlan_status *v7, *status;
	struct iwl_host_cmd cmd = {
		.id = WOWLAN_GET_STATUSES,
		.flags = CMD_WANT_SKB,
	};
	int ret, len, status_size;

	lockdep_assert_held(&mvm->mutex);

	ret = iwl_mvm_send_cmd(mvm, &cmd);
	if (ret) {
		IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
		return ERR_PTR(ret);
	}

	if (!fw_has_api(&mvm->fw->ucode_capa,
			IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL)) {
		struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
		int data_size;

		status_size = sizeof(*v6);
		len = iwl_rx_packet_payload_len(cmd.resp_pkt);

		if (len < status_size) {
			IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
			status = ERR_PTR(-EIO);
			goto out_free_resp;
		}

		data_size = ALIGN(le32_to_cpu(v6->wake_packet_bufsize), 4);

		if (len != (status_size + data_size)) {
			IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
			status = ERR_PTR(-EIO);
			goto out_free_resp;
		}

		status = kzalloc(sizeof(*status) + data_size, GFP_KERNEL);
		if (!status)
			goto out_free_resp;

		BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
			     sizeof(status->gtk[0].key));
		BUILD_BUG_ON(sizeof(v6->gtk.tkip_mic_key) >
			     sizeof(status->gtk[0].tkip_mic_key));

		/* copy GTK info to the right place */
		memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
		       sizeof(v6->gtk.decrypt_key));
		memcpy(status->gtk[0].tkip_mic_key, v6->gtk.tkip_mic_key,
		       sizeof(v6->gtk.tkip_mic_key));
		memcpy(&status->gtk[0].rsc, &v6->gtk.rsc,
		       sizeof(status->gtk[0].rsc));

		/* hardcode the key length to 16 since v6 only supports 16 */
		status->gtk[0].key_len = 16;

		/*
		 * The key index only uses 2 bits (values 0 to 3) and
		 * we always set bit 7 which means this is the
		 * currently used key.
		 */
		status->gtk[0].key_flags = v6->gtk.key_index | BIT(7);

		status->replay_ctr = v6->replay_ctr;

		/* everything starting from pattern_number is identical */
		memcpy(&status->pattern_number, &v6->pattern_number,
		       offsetof(struct iwl_wowlan_status, wake_packet) -
		       offsetof(struct iwl_wowlan_status, pattern_number) +
		       data_size);

		goto out_free_resp;
	}

	v7 = (void *)cmd.resp_pkt->data;
	status_size = sizeof(*v7);
	len = iwl_rx_packet_payload_len(cmd.resp_pkt);

	if (len < status_size) {
		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
		status = ERR_PTR(-EIO);
		goto out_free_resp;
	}

	if (len != (status_size +
		    ALIGN(le32_to_cpu(v7->wake_packet_bufsize), 4))) {
		IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
		status = ERR_PTR(-EIO);
		goto out_free_resp;
	}

	status = kmemdup(v7, len, GFP_KERNEL);

out_free_resp:
	iwl_free_resp(&cmd);
	return status;
}

1640
static struct iwl_wowlan_status *
1641
iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm)
1642
{
1643
	int ret;
1644 1645

	/* only for tracing for now */
E
Emmanuel Grumbach 已提交
1646
	ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1647 1648 1649
	if (ret)
		IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);

1650
	return iwl_mvm_send_wowlan_get_status(mvm);
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
}

/* releases the MVM mutex */
static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
					 struct ieee80211_vif *vif)
{
	struct iwl_wowlan_status_data status;
	struct iwl_wowlan_status *fw_status;
	int i;
	bool keep;
	struct iwl_mvm_sta *mvm_ap_sta;

1663
	fw_status = iwl_mvm_get_wakeup_status(mvm);
1664 1665
	if (IS_ERR_OR_NULL(fw_status))
		goto out_unlock;
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676

	status.pattern_number = le16_to_cpu(fw_status->pattern_number);
	for (i = 0; i < 8; i++)
		status.qos_seq_ctr[i] =
			le16_to_cpu(fw_status->qos_seq_ctr[i]);
	status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
	status.wake_packet_length =
		le32_to_cpu(fw_status->wake_packet_length);
	status.wake_packet_bufsize =
		le32_to_cpu(fw_status->wake_packet_bufsize);
	status.wake_packet = fw_status->wake_packet;
1677

1678
	/* still at hard-coded place 0 for D3 image */
1679 1680
	mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, 0);
	if (!mvm_ap_sta)
1681
		goto out_free;
1682 1683 1684 1685 1686 1687 1688 1689

	for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
		u16 seq = status.qos_seq_ctr[i];
		/* firmware stores last-used value, we store next value */
		seq += 0x10;
		mvm_ap_sta->tid_data[i].seq_number = seq;
	}

1690 1691 1692
	/* now we have all the data we need, unlock to avoid mac80211 issues */
	mutex_unlock(&mvm->mutex);

1693 1694
	iwl_mvm_report_wakeup_reasons(mvm, vif, &status);

1695
	keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1696

1697
	kfree(fw_status);
1698
	return keep;
1699

1700 1701 1702
out_free:
	kfree(fw_status);
out_unlock:
1703
	mutex_unlock(&mvm->mutex);
1704
	return false;
1705 1706
}

1707 1708 1709 1710 1711
void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
			      struct ieee80211_vif *vif,
			      struct iwl_wowlan_status *status)
{
	struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1712
		.mvm = mvm,
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
		.status = status,
	};

	/*
	 * rekey handling requires taking locks that can't be taken now.
	 * however, d0i3 doesn't offload rekey, so we're fine.
	 */
	if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
		return;

	/* find last GTK that we used initially, if any */
	gtkdata.find_phase = true;
	iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);

	gtkdata.find_phase = false;
	iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
}

1731 1732 1733 1734 1735 1736 1737 1738
struct iwl_mvm_nd_query_results {
	u32 matched_profiles;
	struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
};

static int
iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
				struct iwl_mvm_nd_query_results *results)
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
{
	struct iwl_scan_offload_profiles_query *query;
	struct iwl_host_cmd cmd = {
		.id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
		.flags = CMD_WANT_SKB,
	};
	int ret, len;

	ret = iwl_mvm_send_cmd(mvm, &cmd);
	if (ret) {
		IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
1750
		return ret;
1751 1752 1753 1754 1755
	}

	len = iwl_rx_packet_payload_len(cmd.resp_pkt);
	if (len < sizeof(*query)) {
		IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
1756
		ret = -EIO;
1757 1758 1759 1760 1761
		goto out_free_resp;
	}

	query = (void *)cmd.resp_pkt->data;

1762 1763
	results->matched_profiles = le32_to_cpu(query->matched_profiles);
	memcpy(results->matches, query->matches, sizeof(results->matches));
1764

1765
#ifdef CONFIG_IWLWIFI_DEBUGFS
1766 1767 1768
	mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
#endif

1769 1770 1771 1772 1773
out_free_resp:
	iwl_free_resp(&cmd);
	return ret;
}

1774 1775 1776
static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
					    struct ieee80211_vif *vif)
{
1777
	struct cfg80211_wowlan_nd_info *net_detect = NULL;
1778 1779 1780 1781
	struct cfg80211_wowlan_wakeup wakeup = {
		.pattern_idx = -1,
	};
	struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1782
	struct iwl_mvm_nd_query_results query;
1783
	struct iwl_wowlan_status *fw_status;
1784
	unsigned long matched_profiles;
1785
	u32 reasons = 0;
1786
	int i, j, n_matches, ret;
1787

1788
	fw_status = iwl_mvm_get_wakeup_status(mvm);
1789
	if (!IS_ERR_OR_NULL(fw_status)) {
1790
		reasons = le32_to_cpu(fw_status->wakeup_reasons);
1791 1792
		kfree(fw_status);
	}
1793 1794 1795 1796

	if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
		wakeup.rfkill_release = true;

1797 1798 1799
	if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
		goto out;

1800 1801
	ret = iwl_mvm_netdetect_query_results(mvm, &query);
	if (ret || !query.matched_profiles) {
1802
		wakeup_report = NULL;
1803
		goto out;
1804 1805
	}

1806
	matched_profiles = query.matched_profiles;
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820
	if (mvm->n_nd_match_sets) {
		n_matches = hweight_long(matched_profiles);
	} else {
		IWL_ERR(mvm, "no net detect match information available\n");
		n_matches = 0;
	}

	net_detect = kzalloc(sizeof(*net_detect) +
			     (n_matches * sizeof(net_detect->matches[0])),
			     GFP_KERNEL);
	if (!net_detect || !n_matches)
		goto out_report_nd;

	for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
1821
		struct iwl_scan_offload_profile_match *fw_match;
1822
		struct cfg80211_wowlan_nd_match *match;
1823
		int idx, n_channels = 0;
1824 1825 1826 1827 1828

		fw_match = &query.matches[i];

		for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
			n_channels += hweight8(fw_match->matching_channels[j]);
1829

1830 1831 1832
		match = kzalloc(sizeof(*match) +
				(n_channels * sizeof(*match->channels)),
				GFP_KERNEL);
1833 1834 1835
		if (!match)
			goto out_report_nd;

1836 1837
		net_detect->matches[net_detect->n_matches++] = match;

1838 1839 1840 1841 1842 1843
		/* We inverted the order of the SSIDs in the scan
		 * request, so invert the index here.
		 */
		idx = mvm->n_nd_match_sets - i - 1;
		match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
		memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
1844 1845
		       match->ssid.ssid_len);

1846 1847 1848 1849 1850 1851 1852
		if (mvm->n_nd_channels < n_channels)
			continue;

		for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
			if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
				match->channels[match->n_channels++] =
					mvm->nd_channels[j]->center_freq;
1853 1854 1855 1856
	}

out_report_nd:
	wakeup.net_detect = net_detect;
1857
out:
1858 1859
	iwl_mvm_free_nd(mvm);

1860 1861
	mutex_unlock(&mvm->mutex);
	ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1862 1863 1864 1865 1866 1867

	if (net_detect) {
		for (i = 0; i < net_detect->n_matches; i++)
			kfree(net_detect->matches[i]);
		kfree(net_detect);
	}
1868 1869
}

1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
{
#ifdef CONFIG_IWLWIFI_DEBUGFS
	const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
	u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
	u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;

	if (!mvm->store_d3_resume_sram)
		return;

	if (!mvm->d3_resume_sram) {
		mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
		if (!mvm->d3_resume_sram)
			return;
	}

	iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
#endif
}

1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
				       struct ieee80211_vif *vif)
{
	/* skip the one we keep connection on */
	if (data == vif)
		return;

	if (vif->type == NL80211_IFTYPE_STATION)
		ieee80211_resume_disconnect(vif);
}

1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
static int iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
				   struct ieee80211_vif *vif)
{
	u32 base = mvm->error_event_table[0];
	struct error_table_start {
		/* cf. struct iwl_error_event_table */
		u32 valid;
		u32 error_id;
	} err_info;

	iwl_trans_read_mem_bytes(mvm->trans, base,
				 &err_info, sizeof(err_info));

	if (err_info.valid &&
	    err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
		struct cfg80211_wowlan_wakeup wakeup = {
			.rfkill_release = true,
		};
		ieee80211_report_wowlan_wakeup(vif, &wakeup, GFP_KERNEL);
	}
	return err_info.valid;
}

1924
static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
J
Johannes Berg 已提交
1925 1926
{
	struct ieee80211_vif *vif = NULL;
1927
	int ret = 1;
J
Johannes Berg 已提交
1928
	enum iwl_d3_status d3_status;
1929
	bool keep = false;
1930 1931
	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1932 1933
	bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
				      IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
J
Johannes Berg 已提交
1934 1935 1936 1937

	mutex_lock(&mvm->mutex);

	/* get the BSS vif pointer again */
1938 1939
	vif = iwl_mvm_get_bss_vif(mvm);
	if (IS_ERR_OR_NULL(vif))
1940
		goto err;
J
Johannes Berg 已提交
1941

1942
	ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
J
Johannes Berg 已提交
1943
	if (ret)
1944
		goto err;
J
Johannes Berg 已提交
1945 1946 1947

	if (d3_status != IWL_D3_STATUS_ALIVE) {
		IWL_INFO(mvm, "Device was reset during suspend\n");
1948
		goto err;
J
Johannes Berg 已提交
1949 1950
	}

1951
	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
1952 1953 1954
	/* query SRAM first in case we want event logging */
	iwl_mvm_read_d3_sram(mvm);

1955 1956 1957 1958
	if (iwl_mvm_check_rt_status(mvm, vif)) {
		set_bit(STATUS_FW_ERROR, &mvm->trans->status);
		iwl_mvm_dump_nic_error_log(mvm);
		iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
1959
					false, 0);
1960 1961 1962 1963
		ret = 1;
		goto err;
	}

1964 1965 1966 1967 1968 1969 1970 1971 1972
	if (d0i3_first) {
		ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
		if (ret < 0) {
			IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
				ret);
			goto err;
		}
	}

1973 1974 1975 1976 1977 1978
	/*
	 * Query the current location and source from the D3 firmware so we
	 * can play it back when we re-intiailize the D0 firmware
	 */
	iwl_mvm_update_changed_regdom(mvm);

1979 1980 1981 1982
	if (!unified_image)
		/*  Re-configure default SAR profile */
		iwl_mvm_sar_select_profile(mvm, 1, 1);

1983
	if (mvm->net_detect) {
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
		/* If this is a non-unified image, we restart the FW,
		 * so no need to stop the netdetect scan.  If that
		 * fails, continue and try to get the wake-up reasons,
		 * but trigger a HW restart by keeping a failure code
		 * in ret.
		 */
		if (unified_image)
			ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
						false);

1994
		iwl_mvm_query_netdetect_reasons(mvm, vif);
1995 1996
		/* has unlocked the mutex, so skip that */
		goto out;
1997 1998
	} else {
		keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
1999
#ifdef CONFIG_IWLWIFI_DEBUGFS
2000 2001
		if (keep)
			mvm->keep_vif = vif;
2002
#endif
2003 2004
		/* has unlocked the mutex, so skip that */
		goto out_iterate;
2005
	}
J
Johannes Berg 已提交
2006

2007 2008
err:
	iwl_mvm_free_nd(mvm);
J
Johannes Berg 已提交
2009 2010
	mutex_unlock(&mvm->mutex);

2011
out_iterate:
2012 2013 2014 2015
	if (!test)
		ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
			IEEE80211_IFACE_ITER_NORMAL,
			iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
J
Johannes Berg 已提交
2016

2017
out:
2018
	/* no need to reset the device in unified images, if successful */
2019
	if (unified_image && !ret) {
2020 2021 2022 2023
		/* nothing else to do if we already sent D0I3_END_CMD */
		if (d0i3_first)
			return 0;

2024
		ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
2025
		if (!ret)
2026 2027 2028 2029 2030 2031 2032 2033
			return 0;
	}

	/*
	 * Reconfigure the device in one of the following cases:
	 * 1. We are not using a unified image
	 * 2. We are using a unified image but had an error while exiting D3
	 */
2034
	set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
2035
	set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
2036 2037 2038 2039 2040 2041
	/*
	 * When switching images we return 1, which causes mac80211
	 * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
	 * This type of reconfig calls iwl_mvm_restart_complete(),
	 * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
	 * to take the reference here.
2042 2043
	 */
	iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
2044

J
Johannes Berg 已提交
2045 2046 2047
	return 1;
}

2048
static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
2049
{
2050 2051 2052 2053 2054 2055 2056 2057
	iwl_trans_resume(mvm->trans);

	return __iwl_mvm_resume(mvm, false);
}

static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
{
	bool exit_now;
2058
	enum iwl_d3_status d3_status;
2059
	struct iwl_trans *trans = mvm->trans;
2060

2061
	iwl_trans_d3_resume(trans, &d3_status, false, false);
2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076

	/*
	 * make sure to clear D0I3_DEFER_WAKEUP before
	 * calling iwl_trans_resume(), which might wait
	 * for d0i3 exit completion.
	 */
	mutex_lock(&mvm->d0i3_suspend_mutex);
	__clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
	exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
					&mvm->d0i3_suspend_flags);
	mutex_unlock(&mvm->d0i3_suspend_mutex);
	if (exit_now) {
		IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
		_iwl_mvm_exit_d0i3(mvm);
	}
2077

2078
	iwl_trans_resume(trans);
2079

2080
	if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
2081
		int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
2082

2083 2084 2085 2086 2087 2088
		if (ret)
			return ret;
		/*
		 * d0i3 exit will be deferred until reconfig_complete.
		 * make sure there we are out of d0i3.
		 */
2089
	}
2090 2091
	return 0;
}
2092

2093 2094 2095
int iwl_mvm_resume(struct ieee80211_hw *hw)
{
	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2096
	int ret;
2097

2098 2099
	if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
		ret = iwl_mvm_resume_d0i3(mvm);
2100
	else
2101 2102 2103 2104
		ret = iwl_mvm_resume_d3(mvm);

	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;

2105 2106
	iwl_mvm_resume_tcm(mvm);

2107 2108
	iwl_fw_runtime_resume(&mvm->fwrt);

2109
	return ret;
2110 2111
}

J
Johannes Berg 已提交
2112 2113 2114 2115 2116 2117
void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
{
	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);

	device_set_wakeup_enable(mvm->trans->dev, enabled);
}
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

#ifdef CONFIG_IWLWIFI_DEBUGFS
static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
{
	struct iwl_mvm *mvm = inode->i_private;
	int err;

	if (mvm->d3_test_active)
		return -EBUSY;

	file->private_data = inode->i_private;

	ieee80211_stop_queues(mvm->hw);
	synchronize_net();

2133 2134
	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;

2135 2136
	iwl_mvm_pause_tcm(mvm, true);

2137 2138
	iwl_fw_runtime_suspend(&mvm->fwrt);

2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
	/* start pseudo D3 */
	rtnl_lock();
	err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
	rtnl_unlock();
	if (err > 0)
		err = -EINVAL;
	if (err) {
		ieee80211_wake_queues(mvm->hw);
		return err;
	}
	mvm->d3_test_active = true;
2150
	mvm->keep_vif = NULL;
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
	return 0;
}

static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
				    size_t count, loff_t *ppos)
{
	struct iwl_mvm *mvm = file->private_data;
	u32 pme_asserted;

	while (true) {
2161 2162 2163 2164 2165 2166 2167 2168
		/* read pme_ptr if available */
		if (mvm->d3_test_pme_ptr) {
			pme_asserted = iwl_trans_read_mem32(mvm->trans,
						mvm->d3_test_pme_ptr);
			if (pme_asserted)
				break;
		}

2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
		if (msleep_interruptible(100))
			break;
	}

	return 0;
}

static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
					      struct ieee80211_vif *vif)
{
2179 2180 2181 2182
	/* skip the one we keep connection on */
	if (_data == vif)
		return;

2183 2184 2185 2186 2187 2188 2189
	if (vif->type == NL80211_IFTYPE_STATION)
		ieee80211_connection_loss(vif);
}

static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
{
	struct iwl_mvm *mvm = inode->i_private;
2190 2191
	bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
					 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2192 2193

	mvm->d3_test_active = false;
2194

2195 2196
	iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);

2197
	rtnl_lock();
2198
	__iwl_mvm_resume(mvm, true);
2199
	rtnl_unlock();
2200

2201 2202
	iwl_mvm_resume_tcm(mvm);

2203 2204
	iwl_fw_runtime_resume(&mvm->fwrt);

2205 2206
	mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;

2207
	iwl_abort_notification_waits(&mvm->notif_wait);
2208 2209
	if (!unified_image) {
		int remaining_time = 10;
2210

2211 2212 2213 2214 2215 2216 2217 2218
		ieee80211_restart_hw(mvm->hw);

		/* wait for restart and disconnect all interfaces */
		while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
		       remaining_time > 0) {
			remaining_time--;
			msleep(1000);
		}
2219

2220 2221 2222
		if (remaining_time == 0)
			IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
	}
2223 2224 2225

	ieee80211_iterate_active_interfaces_atomic(
		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2226
		iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239

	ieee80211_wake_queues(mvm->hw);

	return 0;
}

const struct file_operations iwl_dbgfs_d3_test_ops = {
	.llseek = no_llseek,
	.open = iwl_mvm_d3_test_open,
	.read = iwl_mvm_d3_test_read,
	.release = iwl_mvm_d3_test_release,
};
#endif