162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/******************************************************************************
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
562306a36Sopenharmony_ci * Copyright(c) 2015 Intel Deutschland GmbH
662306a36Sopenharmony_ci *****************************************************************************/
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/kernel.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include "iwl-io.h"
1162306a36Sopenharmony_ci#include "iwl-agn-hw.h"
1262306a36Sopenharmony_ci#include "iwl-trans.h"
1362306a36Sopenharmony_ci#include "iwl-fh.h"
1462306a36Sopenharmony_ci#include "iwl-op-mode.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include "dev.h"
1762306a36Sopenharmony_ci#include "agn.h"
1862306a36Sopenharmony_ci#include "calib.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/******************************************************************************
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * uCode download functions
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci ******************************************************************************/
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/*
2762306a36Sopenharmony_ci *  Calibration
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_cistatic int iwl_set_Xtal_calib(struct iwl_priv *priv)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	struct iwl_calib_xtal_freq_cmd cmd;
3262306a36Sopenharmony_ci	__le16 *xtal_calib = priv->nvm_data->xtal_calib;
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
3562306a36Sopenharmony_ci	cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
3662306a36Sopenharmony_ci	cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
3762306a36Sopenharmony_ci	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
3862306a36Sopenharmony_ci}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	struct iwl_calib_temperature_offset_cmd cmd;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	memset(&cmd, 0, sizeof(cmd));
4562306a36Sopenharmony_ci	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
4662306a36Sopenharmony_ci	cmd.radio_sensor_offset = priv->nvm_data->raw_temperature;
4762306a36Sopenharmony_ci	if (!(cmd.radio_sensor_offset))
4862306a36Sopenharmony_ci		cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
5162306a36Sopenharmony_ci			le16_to_cpu(cmd.radio_sensor_offset));
5262306a36Sopenharmony_ci	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
5362306a36Sopenharmony_ci}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistatic int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	struct iwl_calib_temperature_offset_v2_cmd cmd;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	memset(&cmd, 0, sizeof(cmd));
6062306a36Sopenharmony_ci	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
6162306a36Sopenharmony_ci	cmd.radio_sensor_offset_high = priv->nvm_data->kelvin_temperature;
6262306a36Sopenharmony_ci	cmd.radio_sensor_offset_low = priv->nvm_data->raw_temperature;
6362306a36Sopenharmony_ci	if (!cmd.radio_sensor_offset_low) {
6462306a36Sopenharmony_ci		IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
6562306a36Sopenharmony_ci		cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
6662306a36Sopenharmony_ci		cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
6762306a36Sopenharmony_ci	}
6862306a36Sopenharmony_ci	cmd.burntVoltageRef = priv->nvm_data->calib_voltage;
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci	IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
7162306a36Sopenharmony_ci			le16_to_cpu(cmd.radio_sensor_offset_high));
7262306a36Sopenharmony_ci	IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
7362306a36Sopenharmony_ci			le16_to_cpu(cmd.radio_sensor_offset_low));
7462306a36Sopenharmony_ci	IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
7562306a36Sopenharmony_ci			le16_to_cpu(cmd.burntVoltageRef));
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistatic int iwl_send_calib_cfg(struct iwl_priv *priv)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	struct iwl_calib_cfg_cmd calib_cfg_cmd;
8362306a36Sopenharmony_ci	struct iwl_host_cmd cmd = {
8462306a36Sopenharmony_ci		.id = CALIBRATION_CFG_CMD,
8562306a36Sopenharmony_ci		.len = { sizeof(struct iwl_calib_cfg_cmd), },
8662306a36Sopenharmony_ci		.data = { &calib_cfg_cmd, },
8762306a36Sopenharmony_ci	};
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
9062306a36Sopenharmony_ci	calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
9162306a36Sopenharmony_ci	calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
9262306a36Sopenharmony_ci	calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
9362306a36Sopenharmony_ci	calib_cfg_cmd.ucd_calib_cfg.flags =
9462306a36Sopenharmony_ci		IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	return iwl_dvm_send_cmd(priv, &cmd);
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciint iwl_init_alive_start(struct iwl_priv *priv)
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci	int ret;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	if (priv->lib->bt_params &&
10462306a36Sopenharmony_ci	    priv->lib->bt_params->advanced_bt_coexist) {
10562306a36Sopenharmony_ci		/*
10662306a36Sopenharmony_ci		 * Tell uCode we are ready to perform calibration
10762306a36Sopenharmony_ci		 * need to perform this before any calibration
10862306a36Sopenharmony_ci		 * no need to close the envlope since we are going
10962306a36Sopenharmony_ci		 * to load the runtime uCode later.
11062306a36Sopenharmony_ci		 */
11162306a36Sopenharmony_ci		ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
11262306a36Sopenharmony_ci			BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
11362306a36Sopenharmony_ci		if (ret)
11462306a36Sopenharmony_ci			return ret;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	ret = iwl_send_calib_cfg(priv);
11962306a36Sopenharmony_ci	if (ret)
12062306a36Sopenharmony_ci		return ret;
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	/**
12362306a36Sopenharmony_ci	 * temperature offset calibration is only needed for runtime ucode,
12462306a36Sopenharmony_ci	 * so prepare the value now.
12562306a36Sopenharmony_ci	 */
12662306a36Sopenharmony_ci	if (priv->lib->need_temp_offset_calib) {
12762306a36Sopenharmony_ci		if (priv->lib->temp_offset_v2)
12862306a36Sopenharmony_ci			return iwl_set_temperature_offset_calib_v2(priv);
12962306a36Sopenharmony_ci		else
13062306a36Sopenharmony_ci			return iwl_set_temperature_offset_calib(priv);
13162306a36Sopenharmony_ci	}
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	return 0;
13462306a36Sopenharmony_ci}
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_cistatic int iwl_send_wimax_coex(struct iwl_priv *priv)
13762306a36Sopenharmony_ci{
13862306a36Sopenharmony_ci	struct iwl_wimax_coex_cmd coex_cmd;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	/* coexistence is disabled */
14162306a36Sopenharmony_ci	memset(&coex_cmd, 0, sizeof(coex_cmd));
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	return iwl_dvm_send_cmd_pdu(priv,
14462306a36Sopenharmony_ci				COEX_PRIORITY_TABLE_CMD, 0,
14562306a36Sopenharmony_ci				sizeof(coex_cmd), &coex_cmd);
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_cistatic const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
14962306a36Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
15062306a36Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
15162306a36Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
15262306a36Sopenharmony_ci		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
15362306a36Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
15462306a36Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
15562306a36Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
15662306a36Sopenharmony_ci		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
15762306a36Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
15862306a36Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
15962306a36Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
16062306a36Sopenharmony_ci		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
16162306a36Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
16262306a36Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
16362306a36Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
16462306a36Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
16562306a36Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
16662306a36Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
16762306a36Sopenharmony_ci	0, 0, 0, 0, 0, 0, 0
16862306a36Sopenharmony_ci};
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_civoid iwl_send_prio_tbl(struct iwl_priv *priv)
17162306a36Sopenharmony_ci{
17262306a36Sopenharmony_ci	struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
17562306a36Sopenharmony_ci		sizeof(iwl_bt_prio_tbl));
17662306a36Sopenharmony_ci	if (iwl_dvm_send_cmd_pdu(priv,
17762306a36Sopenharmony_ci				REPLY_BT_COEX_PRIO_TABLE, 0,
17862306a36Sopenharmony_ci				sizeof(prio_tbl_cmd), &prio_tbl_cmd))
17962306a36Sopenharmony_ci		IWL_ERR(priv, "failed to send BT prio tbl command\n");
18062306a36Sopenharmony_ci}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ciint iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	struct iwl_bt_coex_prot_env_cmd env_cmd;
18562306a36Sopenharmony_ci	int ret;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	env_cmd.action = action;
18862306a36Sopenharmony_ci	env_cmd.type = type;
18962306a36Sopenharmony_ci	ret = iwl_dvm_send_cmd_pdu(priv,
19062306a36Sopenharmony_ci			       REPLY_BT_COEX_PROT_ENV, 0,
19162306a36Sopenharmony_ci			       sizeof(env_cmd), &env_cmd);
19262306a36Sopenharmony_ci	if (ret)
19362306a36Sopenharmony_ci		IWL_ERR(priv, "failed to send BT env command\n");
19462306a36Sopenharmony_ci	return ret;
19562306a36Sopenharmony_ci}
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_cistatic const u8 iwlagn_default_queue_to_tx_fifo[] = {
19862306a36Sopenharmony_ci	IWL_TX_FIFO_VO,
19962306a36Sopenharmony_ci	IWL_TX_FIFO_VI,
20062306a36Sopenharmony_ci	IWL_TX_FIFO_BE,
20162306a36Sopenharmony_ci	IWL_TX_FIFO_BK,
20262306a36Sopenharmony_ci};
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistatic const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
20562306a36Sopenharmony_ci	IWL_TX_FIFO_VO,
20662306a36Sopenharmony_ci	IWL_TX_FIFO_VI,
20762306a36Sopenharmony_ci	IWL_TX_FIFO_BE,
20862306a36Sopenharmony_ci	IWL_TX_FIFO_BK,
20962306a36Sopenharmony_ci	IWL_TX_FIFO_BK_IPAN,
21062306a36Sopenharmony_ci	IWL_TX_FIFO_BE_IPAN,
21162306a36Sopenharmony_ci	IWL_TX_FIFO_VI_IPAN,
21262306a36Sopenharmony_ci	IWL_TX_FIFO_VO_IPAN,
21362306a36Sopenharmony_ci	IWL_TX_FIFO_BE_IPAN,
21462306a36Sopenharmony_ci	IWL_TX_FIFO_UNUSED,
21562306a36Sopenharmony_ci	IWL_TX_FIFO_AUX,
21662306a36Sopenharmony_ci};
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_cistatic int iwl_alive_notify(struct iwl_priv *priv)
21962306a36Sopenharmony_ci{
22062306a36Sopenharmony_ci	const u8 *queue_to_txf;
22162306a36Sopenharmony_ci	u8 n_queues;
22262306a36Sopenharmony_ci	int ret;
22362306a36Sopenharmony_ci	int i;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	iwl_trans_fw_alive(priv->trans, 0);
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN &&
22862306a36Sopenharmony_ci	    priv->nvm_data->sku_cap_ipan_enable) {
22962306a36Sopenharmony_ci		n_queues = ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
23062306a36Sopenharmony_ci		queue_to_txf = iwlagn_ipan_queue_to_tx_fifo;
23162306a36Sopenharmony_ci	} else {
23262306a36Sopenharmony_ci		n_queues = ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
23362306a36Sopenharmony_ci		queue_to_txf = iwlagn_default_queue_to_tx_fifo;
23462306a36Sopenharmony_ci	}
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	for (i = 0; i < n_queues; i++)
23762306a36Sopenharmony_ci		if (queue_to_txf[i] != IWL_TX_FIFO_UNUSED)
23862306a36Sopenharmony_ci			iwl_trans_ac_txq_enable(priv->trans, i,
23962306a36Sopenharmony_ci						queue_to_txf[i], 0);
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	priv->passive_no_rx = false;
24262306a36Sopenharmony_ci	priv->transport_queue_stop = 0;
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	ret = iwl_send_wimax_coex(priv);
24562306a36Sopenharmony_ci	if (ret)
24662306a36Sopenharmony_ci		return ret;
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	if (!priv->lib->no_xtal_calib) {
24962306a36Sopenharmony_ci		ret = iwl_set_Xtal_calib(priv);
25062306a36Sopenharmony_ci		if (ret)
25162306a36Sopenharmony_ci			return ret;
25262306a36Sopenharmony_ci	}
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	return iwl_send_calib_results(priv);
25562306a36Sopenharmony_ci}
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_cistruct iwl_alive_data {
25862306a36Sopenharmony_ci	bool valid;
25962306a36Sopenharmony_ci	u8 subtype;
26062306a36Sopenharmony_ci};
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_cistatic bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
26362306a36Sopenharmony_ci			 struct iwl_rx_packet *pkt, void *data)
26462306a36Sopenharmony_ci{
26562306a36Sopenharmony_ci	struct iwl_priv *priv =
26662306a36Sopenharmony_ci		container_of(notif_wait, struct iwl_priv, notif_wait);
26762306a36Sopenharmony_ci	struct iwl_alive_data *alive_data = data;
26862306a36Sopenharmony_ci	struct iwl_alive_resp *palive;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	palive = (void *)pkt->data;
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
27362306a36Sopenharmony_ci		       "0x%01X 0x%01X\n",
27462306a36Sopenharmony_ci		       palive->is_valid, palive->ver_type,
27562306a36Sopenharmony_ci		       palive->ver_subtype);
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci	priv->device_pointers.error_event_table =
27862306a36Sopenharmony_ci		le32_to_cpu(palive->error_event_table_ptr);
27962306a36Sopenharmony_ci	priv->device_pointers.log_event_table =
28062306a36Sopenharmony_ci		le32_to_cpu(palive->log_event_table_ptr);
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	alive_data->subtype = palive->ver_subtype;
28362306a36Sopenharmony_ci	alive_data->valid = palive->is_valid == UCODE_VALID_OK;
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci	return true;
28662306a36Sopenharmony_ci}
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci#define UCODE_ALIVE_TIMEOUT	HZ
28962306a36Sopenharmony_ci#define UCODE_CALIB_TIMEOUT	(2*HZ)
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ciint iwl_load_ucode_wait_alive(struct iwl_priv *priv,
29262306a36Sopenharmony_ci				 enum iwl_ucode_type ucode_type)
29362306a36Sopenharmony_ci{
29462306a36Sopenharmony_ci	struct iwl_notification_wait alive_wait;
29562306a36Sopenharmony_ci	struct iwl_alive_data alive_data;
29662306a36Sopenharmony_ci	const struct fw_img *fw;
29762306a36Sopenharmony_ci	int ret;
29862306a36Sopenharmony_ci	enum iwl_ucode_type old_type;
29962306a36Sopenharmony_ci	static const u16 alive_cmd[] = { REPLY_ALIVE };
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	fw = iwl_get_ucode_image(priv->fw, ucode_type);
30262306a36Sopenharmony_ci	if (WARN_ON(!fw))
30362306a36Sopenharmony_ci		return -EINVAL;
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci	old_type = priv->cur_ucode;
30662306a36Sopenharmony_ci	priv->cur_ucode = ucode_type;
30762306a36Sopenharmony_ci	priv->ucode_loaded = false;
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci	iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
31062306a36Sopenharmony_ci				   alive_cmd, ARRAY_SIZE(alive_cmd),
31162306a36Sopenharmony_ci				   iwl_alive_fn, &alive_data);
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	ret = iwl_trans_start_fw(priv->trans, fw, false);
31462306a36Sopenharmony_ci	if (ret) {
31562306a36Sopenharmony_ci		priv->cur_ucode = old_type;
31662306a36Sopenharmony_ci		iwl_remove_notification(&priv->notif_wait, &alive_wait);
31762306a36Sopenharmony_ci		return ret;
31862306a36Sopenharmony_ci	}
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	/*
32162306a36Sopenharmony_ci	 * Some things may run in the background now, but we
32262306a36Sopenharmony_ci	 * just wait for the ALIVE notification here.
32362306a36Sopenharmony_ci	 */
32462306a36Sopenharmony_ci	ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
32562306a36Sopenharmony_ci					UCODE_ALIVE_TIMEOUT);
32662306a36Sopenharmony_ci	if (ret) {
32762306a36Sopenharmony_ci		priv->cur_ucode = old_type;
32862306a36Sopenharmony_ci		return ret;
32962306a36Sopenharmony_ci	}
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	if (!alive_data.valid) {
33262306a36Sopenharmony_ci		IWL_ERR(priv, "Loaded ucode is not valid!\n");
33362306a36Sopenharmony_ci		priv->cur_ucode = old_type;
33462306a36Sopenharmony_ci		return -EIO;
33562306a36Sopenharmony_ci	}
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci	priv->ucode_loaded = true;
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	if (ucode_type != IWL_UCODE_WOWLAN) {
34062306a36Sopenharmony_ci		/* delay a bit to give rfkill time to run */
34162306a36Sopenharmony_ci		msleep(5);
34262306a36Sopenharmony_ci	}
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci	ret = iwl_alive_notify(priv);
34562306a36Sopenharmony_ci	if (ret) {
34662306a36Sopenharmony_ci		IWL_WARN(priv,
34762306a36Sopenharmony_ci			"Could not complete ALIVE transition: %d\n", ret);
34862306a36Sopenharmony_ci		priv->cur_ucode = old_type;
34962306a36Sopenharmony_ci		return ret;
35062306a36Sopenharmony_ci	}
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	return 0;
35362306a36Sopenharmony_ci}
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_cistatic bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
35662306a36Sopenharmony_ci			      struct iwl_rx_packet *pkt, void *data)
35762306a36Sopenharmony_ci{
35862306a36Sopenharmony_ci	struct iwl_priv *priv = data;
35962306a36Sopenharmony_ci	struct iwl_calib_cmd *cmd;
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci	if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
36262306a36Sopenharmony_ci		WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
36362306a36Sopenharmony_ci		return true;
36462306a36Sopenharmony_ci	}
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	cmd = (struct iwl_calib_cmd *)pkt->data;
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	if (iwl_calib_set(priv, cmd, iwl_rx_packet_payload_len(pkt)))
36962306a36Sopenharmony_ci		IWL_ERR(priv, "Failed to record calibration data %d\n",
37062306a36Sopenharmony_ci			cmd->hdr.op_code);
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	return false;
37362306a36Sopenharmony_ci}
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ciint iwl_run_init_ucode(struct iwl_priv *priv)
37662306a36Sopenharmony_ci{
37762306a36Sopenharmony_ci	struct iwl_notification_wait calib_wait;
37862306a36Sopenharmony_ci	static const u16 calib_complete[] = {
37962306a36Sopenharmony_ci		CALIBRATION_RES_NOTIFICATION,
38062306a36Sopenharmony_ci		CALIBRATION_COMPLETE_NOTIFICATION
38162306a36Sopenharmony_ci	};
38262306a36Sopenharmony_ci	int ret;
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	lockdep_assert_held(&priv->mutex);
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	/* No init ucode required? Curious, but maybe ok */
38762306a36Sopenharmony_ci	if (!priv->fw->img[IWL_UCODE_INIT].num_sec)
38862306a36Sopenharmony_ci		return 0;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
39162306a36Sopenharmony_ci				   calib_complete, ARRAY_SIZE(calib_complete),
39262306a36Sopenharmony_ci				   iwlagn_wait_calib, priv);
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	/* Will also start the device */
39562306a36Sopenharmony_ci	ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
39662306a36Sopenharmony_ci	if (ret)
39762306a36Sopenharmony_ci		goto error;
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	ret = iwl_init_alive_start(priv);
40062306a36Sopenharmony_ci	if (ret)
40162306a36Sopenharmony_ci		goto error;
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci	/*
40462306a36Sopenharmony_ci	 * Some things may run in the background now, but we
40562306a36Sopenharmony_ci	 * just wait for the calibration complete notification.
40662306a36Sopenharmony_ci	 */
40762306a36Sopenharmony_ci	ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
40862306a36Sopenharmony_ci					UCODE_CALIB_TIMEOUT);
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci	goto out;
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci error:
41362306a36Sopenharmony_ci	iwl_remove_notification(&priv->notif_wait, &calib_wait);
41462306a36Sopenharmony_ci out:
41562306a36Sopenharmony_ci	/* Whatever happened, stop the device */
41662306a36Sopenharmony_ci	iwl_trans_stop_device(priv->trans);
41762306a36Sopenharmony_ci	priv->ucode_loaded = false;
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	return ret;
42062306a36Sopenharmony_ci}
421