18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/******************************************************************************
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
58c2ecf20Sopenharmony_ci * Copyright(c) 2015 Intel Deutschland GmbH
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Contact Information:
88c2ecf20Sopenharmony_ci *  Intel Linux Wireless <linuxwifi@intel.com>
98c2ecf20Sopenharmony_ci * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *****************************************************************************/
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "iwl-io.h"
168c2ecf20Sopenharmony_ci#include "iwl-agn-hw.h"
178c2ecf20Sopenharmony_ci#include "iwl-trans.h"
188c2ecf20Sopenharmony_ci#include "iwl-fh.h"
198c2ecf20Sopenharmony_ci#include "iwl-op-mode.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "dev.h"
228c2ecf20Sopenharmony_ci#include "agn.h"
238c2ecf20Sopenharmony_ci#include "calib.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/******************************************************************************
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * uCode download functions
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci ******************************************************************************/
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/*
328c2ecf20Sopenharmony_ci *  Calibration
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_cistatic int iwl_set_Xtal_calib(struct iwl_priv *priv)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct iwl_calib_xtal_freq_cmd cmd;
378c2ecf20Sopenharmony_ci	__le16 *xtal_calib = priv->nvm_data->xtal_calib;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
408c2ecf20Sopenharmony_ci	cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
418c2ecf20Sopenharmony_ci	cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
428c2ecf20Sopenharmony_ci	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	struct iwl_calib_temperature_offset_cmd cmd;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	memset(&cmd, 0, sizeof(cmd));
508c2ecf20Sopenharmony_ci	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
518c2ecf20Sopenharmony_ci	cmd.radio_sensor_offset = priv->nvm_data->raw_temperature;
528c2ecf20Sopenharmony_ci	if (!(cmd.radio_sensor_offset))
538c2ecf20Sopenharmony_ci		cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
568c2ecf20Sopenharmony_ci			le16_to_cpu(cmd.radio_sensor_offset));
578c2ecf20Sopenharmony_ci	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct iwl_calib_temperature_offset_v2_cmd cmd;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	memset(&cmd, 0, sizeof(cmd));
658c2ecf20Sopenharmony_ci	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
668c2ecf20Sopenharmony_ci	cmd.radio_sensor_offset_high = priv->nvm_data->kelvin_temperature;
678c2ecf20Sopenharmony_ci	cmd.radio_sensor_offset_low = priv->nvm_data->raw_temperature;
688c2ecf20Sopenharmony_ci	if (!cmd.radio_sensor_offset_low) {
698c2ecf20Sopenharmony_ci		IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
708c2ecf20Sopenharmony_ci		cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
718c2ecf20Sopenharmony_ci		cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
728c2ecf20Sopenharmony_ci	}
738c2ecf20Sopenharmony_ci	cmd.burntVoltageRef = priv->nvm_data->calib_voltage;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
768c2ecf20Sopenharmony_ci			le16_to_cpu(cmd.radio_sensor_offset_high));
778c2ecf20Sopenharmony_ci	IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
788c2ecf20Sopenharmony_ci			le16_to_cpu(cmd.radio_sensor_offset_low));
798c2ecf20Sopenharmony_ci	IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
808c2ecf20Sopenharmony_ci			le16_to_cpu(cmd.burntVoltageRef));
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic int iwl_send_calib_cfg(struct iwl_priv *priv)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct iwl_calib_cfg_cmd calib_cfg_cmd;
888c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
898c2ecf20Sopenharmony_ci		.id = CALIBRATION_CFG_CMD,
908c2ecf20Sopenharmony_ci		.len = { sizeof(struct iwl_calib_cfg_cmd), },
918c2ecf20Sopenharmony_ci		.data = { &calib_cfg_cmd, },
928c2ecf20Sopenharmony_ci	};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
958c2ecf20Sopenharmony_ci	calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
968c2ecf20Sopenharmony_ci	calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
978c2ecf20Sopenharmony_ci	calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
988c2ecf20Sopenharmony_ci	calib_cfg_cmd.ucd_calib_cfg.flags =
998c2ecf20Sopenharmony_ci		IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return iwl_dvm_send_cmd(priv, &cmd);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciint iwl_init_alive_start(struct iwl_priv *priv)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	int ret;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	if (priv->lib->bt_params &&
1098c2ecf20Sopenharmony_ci	    priv->lib->bt_params->advanced_bt_coexist) {
1108c2ecf20Sopenharmony_ci		/*
1118c2ecf20Sopenharmony_ci		 * Tell uCode we are ready to perform calibration
1128c2ecf20Sopenharmony_ci		 * need to perform this before any calibration
1138c2ecf20Sopenharmony_ci		 * no need to close the envlope since we are going
1148c2ecf20Sopenharmony_ci		 * to load the runtime uCode later.
1158c2ecf20Sopenharmony_ci		 */
1168c2ecf20Sopenharmony_ci		ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
1178c2ecf20Sopenharmony_ci			BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
1188c2ecf20Sopenharmony_ci		if (ret)
1198c2ecf20Sopenharmony_ci			return ret;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	ret = iwl_send_calib_cfg(priv);
1248c2ecf20Sopenharmony_ci	if (ret)
1258c2ecf20Sopenharmony_ci		return ret;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/**
1288c2ecf20Sopenharmony_ci	 * temperature offset calibration is only needed for runtime ucode,
1298c2ecf20Sopenharmony_ci	 * so prepare the value now.
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	if (priv->lib->need_temp_offset_calib) {
1328c2ecf20Sopenharmony_ci		if (priv->lib->temp_offset_v2)
1338c2ecf20Sopenharmony_ci			return iwl_set_temperature_offset_calib_v2(priv);
1348c2ecf20Sopenharmony_ci		else
1358c2ecf20Sopenharmony_ci			return iwl_set_temperature_offset_calib(priv);
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	return 0;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic int iwl_send_wimax_coex(struct iwl_priv *priv)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	struct iwl_wimax_coex_cmd coex_cmd;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* coexistence is disabled */
1468c2ecf20Sopenharmony_ci	memset(&coex_cmd, 0, sizeof(coex_cmd));
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	return iwl_dvm_send_cmd_pdu(priv,
1498c2ecf20Sopenharmony_ci				COEX_PRIORITY_TABLE_CMD, 0,
1508c2ecf20Sopenharmony_ci				sizeof(coex_cmd), &coex_cmd);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
1548c2ecf20Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
1558c2ecf20Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
1568c2ecf20Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
1578c2ecf20Sopenharmony_ci		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
1588c2ecf20Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
1598c2ecf20Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
1608c2ecf20Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
1618c2ecf20Sopenharmony_ci		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
1628c2ecf20Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
1638c2ecf20Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
1648c2ecf20Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
1658c2ecf20Sopenharmony_ci		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
1668c2ecf20Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
1678c2ecf20Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
1688c2ecf20Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
1698c2ecf20Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
1708c2ecf20Sopenharmony_ci	((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
1718c2ecf20Sopenharmony_ci		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
1728c2ecf20Sopenharmony_ci	0, 0, 0, 0, 0, 0, 0
1738c2ecf20Sopenharmony_ci};
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_civoid iwl_send_prio_tbl(struct iwl_priv *priv)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
1808c2ecf20Sopenharmony_ci		sizeof(iwl_bt_prio_tbl));
1818c2ecf20Sopenharmony_ci	if (iwl_dvm_send_cmd_pdu(priv,
1828c2ecf20Sopenharmony_ci				REPLY_BT_COEX_PRIO_TABLE, 0,
1838c2ecf20Sopenharmony_ci				sizeof(prio_tbl_cmd), &prio_tbl_cmd))
1848c2ecf20Sopenharmony_ci		IWL_ERR(priv, "failed to send BT prio tbl command\n");
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ciint iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	struct iwl_bt_coex_prot_env_cmd env_cmd;
1908c2ecf20Sopenharmony_ci	int ret;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	env_cmd.action = action;
1938c2ecf20Sopenharmony_ci	env_cmd.type = type;
1948c2ecf20Sopenharmony_ci	ret = iwl_dvm_send_cmd_pdu(priv,
1958c2ecf20Sopenharmony_ci			       REPLY_BT_COEX_PROT_ENV, 0,
1968c2ecf20Sopenharmony_ci			       sizeof(env_cmd), &env_cmd);
1978c2ecf20Sopenharmony_ci	if (ret)
1988c2ecf20Sopenharmony_ci		IWL_ERR(priv, "failed to send BT env command\n");
1998c2ecf20Sopenharmony_ci	return ret;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic const u8 iwlagn_default_queue_to_tx_fifo[] = {
2038c2ecf20Sopenharmony_ci	IWL_TX_FIFO_VO,
2048c2ecf20Sopenharmony_ci	IWL_TX_FIFO_VI,
2058c2ecf20Sopenharmony_ci	IWL_TX_FIFO_BE,
2068c2ecf20Sopenharmony_ci	IWL_TX_FIFO_BK,
2078c2ecf20Sopenharmony_ci};
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
2108c2ecf20Sopenharmony_ci	IWL_TX_FIFO_VO,
2118c2ecf20Sopenharmony_ci	IWL_TX_FIFO_VI,
2128c2ecf20Sopenharmony_ci	IWL_TX_FIFO_BE,
2138c2ecf20Sopenharmony_ci	IWL_TX_FIFO_BK,
2148c2ecf20Sopenharmony_ci	IWL_TX_FIFO_BK_IPAN,
2158c2ecf20Sopenharmony_ci	IWL_TX_FIFO_BE_IPAN,
2168c2ecf20Sopenharmony_ci	IWL_TX_FIFO_VI_IPAN,
2178c2ecf20Sopenharmony_ci	IWL_TX_FIFO_VO_IPAN,
2188c2ecf20Sopenharmony_ci	IWL_TX_FIFO_BE_IPAN,
2198c2ecf20Sopenharmony_ci	IWL_TX_FIFO_UNUSED,
2208c2ecf20Sopenharmony_ci	IWL_TX_FIFO_AUX,
2218c2ecf20Sopenharmony_ci};
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic int iwl_alive_notify(struct iwl_priv *priv)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	const u8 *queue_to_txf;
2268c2ecf20Sopenharmony_ci	u8 n_queues;
2278c2ecf20Sopenharmony_ci	int ret;
2288c2ecf20Sopenharmony_ci	int i;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	iwl_trans_fw_alive(priv->trans, 0);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN &&
2338c2ecf20Sopenharmony_ci	    priv->nvm_data->sku_cap_ipan_enable) {
2348c2ecf20Sopenharmony_ci		n_queues = ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
2358c2ecf20Sopenharmony_ci		queue_to_txf = iwlagn_ipan_queue_to_tx_fifo;
2368c2ecf20Sopenharmony_ci	} else {
2378c2ecf20Sopenharmony_ci		n_queues = ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
2388c2ecf20Sopenharmony_ci		queue_to_txf = iwlagn_default_queue_to_tx_fifo;
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	for (i = 0; i < n_queues; i++)
2428c2ecf20Sopenharmony_ci		if (queue_to_txf[i] != IWL_TX_FIFO_UNUSED)
2438c2ecf20Sopenharmony_ci			iwl_trans_ac_txq_enable(priv->trans, i,
2448c2ecf20Sopenharmony_ci						queue_to_txf[i], 0);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	priv->passive_no_rx = false;
2478c2ecf20Sopenharmony_ci	priv->transport_queue_stop = 0;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	ret = iwl_send_wimax_coex(priv);
2508c2ecf20Sopenharmony_ci	if (ret)
2518c2ecf20Sopenharmony_ci		return ret;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (!priv->lib->no_xtal_calib) {
2548c2ecf20Sopenharmony_ci		ret = iwl_set_Xtal_calib(priv);
2558c2ecf20Sopenharmony_ci		if (ret)
2568c2ecf20Sopenharmony_ci			return ret;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	return iwl_send_calib_results(priv);
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistruct iwl_alive_data {
2638c2ecf20Sopenharmony_ci	bool valid;
2648c2ecf20Sopenharmony_ci	u8 subtype;
2658c2ecf20Sopenharmony_ci};
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
2688c2ecf20Sopenharmony_ci			 struct iwl_rx_packet *pkt, void *data)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	struct iwl_priv *priv =
2718c2ecf20Sopenharmony_ci		container_of(notif_wait, struct iwl_priv, notif_wait);
2728c2ecf20Sopenharmony_ci	struct iwl_alive_data *alive_data = data;
2738c2ecf20Sopenharmony_ci	struct iwl_alive_resp *palive;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	palive = (void *)pkt->data;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
2788c2ecf20Sopenharmony_ci		       "0x%01X 0x%01X\n",
2798c2ecf20Sopenharmony_ci		       palive->is_valid, palive->ver_type,
2808c2ecf20Sopenharmony_ci		       palive->ver_subtype);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	priv->device_pointers.error_event_table =
2838c2ecf20Sopenharmony_ci		le32_to_cpu(palive->error_event_table_ptr);
2848c2ecf20Sopenharmony_ci	priv->device_pointers.log_event_table =
2858c2ecf20Sopenharmony_ci		le32_to_cpu(palive->log_event_table_ptr);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	alive_data->subtype = palive->ver_subtype;
2888c2ecf20Sopenharmony_ci	alive_data->valid = palive->is_valid == UCODE_VALID_OK;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	return true;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci#define UCODE_ALIVE_TIMEOUT	HZ
2948c2ecf20Sopenharmony_ci#define UCODE_CALIB_TIMEOUT	(2*HZ)
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ciint iwl_load_ucode_wait_alive(struct iwl_priv *priv,
2978c2ecf20Sopenharmony_ci				 enum iwl_ucode_type ucode_type)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	struct iwl_notification_wait alive_wait;
3008c2ecf20Sopenharmony_ci	struct iwl_alive_data alive_data;
3018c2ecf20Sopenharmony_ci	const struct fw_img *fw;
3028c2ecf20Sopenharmony_ci	int ret;
3038c2ecf20Sopenharmony_ci	enum iwl_ucode_type old_type;
3048c2ecf20Sopenharmony_ci	static const u16 alive_cmd[] = { REPLY_ALIVE };
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	fw = iwl_get_ucode_image(priv->fw, ucode_type);
3078c2ecf20Sopenharmony_ci	if (WARN_ON(!fw))
3088c2ecf20Sopenharmony_ci		return -EINVAL;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	old_type = priv->cur_ucode;
3118c2ecf20Sopenharmony_ci	priv->cur_ucode = ucode_type;
3128c2ecf20Sopenharmony_ci	priv->ucode_loaded = false;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
3158c2ecf20Sopenharmony_ci				   alive_cmd, ARRAY_SIZE(alive_cmd),
3168c2ecf20Sopenharmony_ci				   iwl_alive_fn, &alive_data);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	ret = iwl_trans_start_fw(priv->trans, fw, false);
3198c2ecf20Sopenharmony_ci	if (ret) {
3208c2ecf20Sopenharmony_ci		priv->cur_ucode = old_type;
3218c2ecf20Sopenharmony_ci		iwl_remove_notification(&priv->notif_wait, &alive_wait);
3228c2ecf20Sopenharmony_ci		return ret;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	/*
3268c2ecf20Sopenharmony_ci	 * Some things may run in the background now, but we
3278c2ecf20Sopenharmony_ci	 * just wait for the ALIVE notification here.
3288c2ecf20Sopenharmony_ci	 */
3298c2ecf20Sopenharmony_ci	ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
3308c2ecf20Sopenharmony_ci					UCODE_ALIVE_TIMEOUT);
3318c2ecf20Sopenharmony_ci	if (ret) {
3328c2ecf20Sopenharmony_ci		priv->cur_ucode = old_type;
3338c2ecf20Sopenharmony_ci		return ret;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (!alive_data.valid) {
3378c2ecf20Sopenharmony_ci		IWL_ERR(priv, "Loaded ucode is not valid!\n");
3388c2ecf20Sopenharmony_ci		priv->cur_ucode = old_type;
3398c2ecf20Sopenharmony_ci		return -EIO;
3408c2ecf20Sopenharmony_ci	}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	priv->ucode_loaded = true;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	if (ucode_type != IWL_UCODE_WOWLAN) {
3458c2ecf20Sopenharmony_ci		/* delay a bit to give rfkill time to run */
3468c2ecf20Sopenharmony_ci		msleep(5);
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	ret = iwl_alive_notify(priv);
3508c2ecf20Sopenharmony_ci	if (ret) {
3518c2ecf20Sopenharmony_ci		IWL_WARN(priv,
3528c2ecf20Sopenharmony_ci			"Could not complete ALIVE transition: %d\n", ret);
3538c2ecf20Sopenharmony_ci		priv->cur_ucode = old_type;
3548c2ecf20Sopenharmony_ci		return ret;
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	return 0;
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
3618c2ecf20Sopenharmony_ci			      struct iwl_rx_packet *pkt, void *data)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	struct iwl_priv *priv = data;
3648c2ecf20Sopenharmony_ci	struct iwl_calib_hdr *hdr;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
3678c2ecf20Sopenharmony_ci		WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
3688c2ecf20Sopenharmony_ci		return true;
3698c2ecf20Sopenharmony_ci	}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	hdr = (struct iwl_calib_hdr *)pkt->data;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (iwl_calib_set(priv, hdr, iwl_rx_packet_payload_len(pkt)))
3748c2ecf20Sopenharmony_ci		IWL_ERR(priv, "Failed to record calibration data %d\n",
3758c2ecf20Sopenharmony_ci			hdr->op_code);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	return false;
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ciint iwl_run_init_ucode(struct iwl_priv *priv)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	struct iwl_notification_wait calib_wait;
3838c2ecf20Sopenharmony_ci	static const u16 calib_complete[] = {
3848c2ecf20Sopenharmony_ci		CALIBRATION_RES_NOTIFICATION,
3858c2ecf20Sopenharmony_ci		CALIBRATION_COMPLETE_NOTIFICATION
3868c2ecf20Sopenharmony_ci	};
3878c2ecf20Sopenharmony_ci	int ret;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	lockdep_assert_held(&priv->mutex);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	/* No init ucode required? Curious, but maybe ok */
3928c2ecf20Sopenharmony_ci	if (!priv->fw->img[IWL_UCODE_INIT].num_sec)
3938c2ecf20Sopenharmony_ci		return 0;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
3968c2ecf20Sopenharmony_ci				   calib_complete, ARRAY_SIZE(calib_complete),
3978c2ecf20Sopenharmony_ci				   iwlagn_wait_calib, priv);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/* Will also start the device */
4008c2ecf20Sopenharmony_ci	ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
4018c2ecf20Sopenharmony_ci	if (ret)
4028c2ecf20Sopenharmony_ci		goto error;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	ret = iwl_init_alive_start(priv);
4058c2ecf20Sopenharmony_ci	if (ret)
4068c2ecf20Sopenharmony_ci		goto error;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	/*
4098c2ecf20Sopenharmony_ci	 * Some things may run in the background now, but we
4108c2ecf20Sopenharmony_ci	 * just wait for the calibration complete notification.
4118c2ecf20Sopenharmony_ci	 */
4128c2ecf20Sopenharmony_ci	ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
4138c2ecf20Sopenharmony_ci					UCODE_CALIB_TIMEOUT);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	goto out;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci error:
4188c2ecf20Sopenharmony_ci	iwl_remove_notification(&priv->notif_wait, &calib_wait);
4198c2ecf20Sopenharmony_ci out:
4208c2ecf20Sopenharmony_ci	/* Whatever happened, stop the device */
4218c2ecf20Sopenharmony_ci	iwl_trans_stop_device(priv->trans);
4228c2ecf20Sopenharmony_ci	priv->ucode_loaded = false;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	return ret;
4258c2ecf20Sopenharmony_ci}
426