xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/intel/iwlwifi/dvm/ucode.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
5*4882a593Smuzhiyun  * Copyright(c) 2015 Intel Deutschland GmbH
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Contact Information:
8*4882a593Smuzhiyun  *  Intel Linux Wireless <linuxwifi@intel.com>
9*4882a593Smuzhiyun  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *****************************************************************************/
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include "iwl-io.h"
16*4882a593Smuzhiyun #include "iwl-agn-hw.h"
17*4882a593Smuzhiyun #include "iwl-trans.h"
18*4882a593Smuzhiyun #include "iwl-fh.h"
19*4882a593Smuzhiyun #include "iwl-op-mode.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #include "dev.h"
22*4882a593Smuzhiyun #include "agn.h"
23*4882a593Smuzhiyun #include "calib.h"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /******************************************************************************
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * uCode download functions
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  ******************************************************************************/
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun  *  Calibration
33*4882a593Smuzhiyun  */
iwl_set_Xtal_calib(struct iwl_priv * priv)34*4882a593Smuzhiyun static int iwl_set_Xtal_calib(struct iwl_priv *priv)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	struct iwl_calib_xtal_freq_cmd cmd;
37*4882a593Smuzhiyun 	__le16 *xtal_calib = priv->nvm_data->xtal_calib;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
40*4882a593Smuzhiyun 	cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
41*4882a593Smuzhiyun 	cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
42*4882a593Smuzhiyun 	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
iwl_set_temperature_offset_calib(struct iwl_priv * priv)45*4882a593Smuzhiyun static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	struct iwl_calib_temperature_offset_cmd cmd;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	memset(&cmd, 0, sizeof(cmd));
50*4882a593Smuzhiyun 	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
51*4882a593Smuzhiyun 	cmd.radio_sensor_offset = priv->nvm_data->raw_temperature;
52*4882a593Smuzhiyun 	if (!(cmd.radio_sensor_offset))
53*4882a593Smuzhiyun 		cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
56*4882a593Smuzhiyun 			le16_to_cpu(cmd.radio_sensor_offset));
57*4882a593Smuzhiyun 	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
iwl_set_temperature_offset_calib_v2(struct iwl_priv * priv)60*4882a593Smuzhiyun static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	struct iwl_calib_temperature_offset_v2_cmd cmd;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	memset(&cmd, 0, sizeof(cmd));
65*4882a593Smuzhiyun 	iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
66*4882a593Smuzhiyun 	cmd.radio_sensor_offset_high = priv->nvm_data->kelvin_temperature;
67*4882a593Smuzhiyun 	cmd.radio_sensor_offset_low = priv->nvm_data->raw_temperature;
68*4882a593Smuzhiyun 	if (!cmd.radio_sensor_offset_low) {
69*4882a593Smuzhiyun 		IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
70*4882a593Smuzhiyun 		cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
71*4882a593Smuzhiyun 		cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 	cmd.burntVoltageRef = priv->nvm_data->calib_voltage;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
76*4882a593Smuzhiyun 			le16_to_cpu(cmd.radio_sensor_offset_high));
77*4882a593Smuzhiyun 	IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
78*4882a593Smuzhiyun 			le16_to_cpu(cmd.radio_sensor_offset_low));
79*4882a593Smuzhiyun 	IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
80*4882a593Smuzhiyun 			le16_to_cpu(cmd.burntVoltageRef));
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
iwl_send_calib_cfg(struct iwl_priv * priv)85*4882a593Smuzhiyun static int iwl_send_calib_cfg(struct iwl_priv *priv)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	struct iwl_calib_cfg_cmd calib_cfg_cmd;
88*4882a593Smuzhiyun 	struct iwl_host_cmd cmd = {
89*4882a593Smuzhiyun 		.id = CALIBRATION_CFG_CMD,
90*4882a593Smuzhiyun 		.len = { sizeof(struct iwl_calib_cfg_cmd), },
91*4882a593Smuzhiyun 		.data = { &calib_cfg_cmd, },
92*4882a593Smuzhiyun 	};
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
95*4882a593Smuzhiyun 	calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
96*4882a593Smuzhiyun 	calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
97*4882a593Smuzhiyun 	calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
98*4882a593Smuzhiyun 	calib_cfg_cmd.ucd_calib_cfg.flags =
99*4882a593Smuzhiyun 		IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	return iwl_dvm_send_cmd(priv, &cmd);
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
iwl_init_alive_start(struct iwl_priv * priv)104*4882a593Smuzhiyun int iwl_init_alive_start(struct iwl_priv *priv)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	int ret;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	if (priv->lib->bt_params &&
109*4882a593Smuzhiyun 	    priv->lib->bt_params->advanced_bt_coexist) {
110*4882a593Smuzhiyun 		/*
111*4882a593Smuzhiyun 		 * Tell uCode we are ready to perform calibration
112*4882a593Smuzhiyun 		 * need to perform this before any calibration
113*4882a593Smuzhiyun 		 * no need to close the envlope since we are going
114*4882a593Smuzhiyun 		 * to load the runtime uCode later.
115*4882a593Smuzhiyun 		 */
116*4882a593Smuzhiyun 		ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
117*4882a593Smuzhiyun 			BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
118*4882a593Smuzhiyun 		if (ret)
119*4882a593Smuzhiyun 			return ret;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	}
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	ret = iwl_send_calib_cfg(priv);
124*4882a593Smuzhiyun 	if (ret)
125*4882a593Smuzhiyun 		return ret;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	/**
128*4882a593Smuzhiyun 	 * temperature offset calibration is only needed for runtime ucode,
129*4882a593Smuzhiyun 	 * so prepare the value now.
130*4882a593Smuzhiyun 	 */
131*4882a593Smuzhiyun 	if (priv->lib->need_temp_offset_calib) {
132*4882a593Smuzhiyun 		if (priv->lib->temp_offset_v2)
133*4882a593Smuzhiyun 			return iwl_set_temperature_offset_calib_v2(priv);
134*4882a593Smuzhiyun 		else
135*4882a593Smuzhiyun 			return iwl_set_temperature_offset_calib(priv);
136*4882a593Smuzhiyun 	}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	return 0;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
iwl_send_wimax_coex(struct iwl_priv * priv)141*4882a593Smuzhiyun static int iwl_send_wimax_coex(struct iwl_priv *priv)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	struct iwl_wimax_coex_cmd coex_cmd;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	/* coexistence is disabled */
146*4882a593Smuzhiyun 	memset(&coex_cmd, 0, sizeof(coex_cmd));
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	return iwl_dvm_send_cmd_pdu(priv,
149*4882a593Smuzhiyun 				COEX_PRIORITY_TABLE_CMD, 0,
150*4882a593Smuzhiyun 				sizeof(coex_cmd), &coex_cmd);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
154*4882a593Smuzhiyun 	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
155*4882a593Smuzhiyun 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
156*4882a593Smuzhiyun 	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
157*4882a593Smuzhiyun 		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
158*4882a593Smuzhiyun 	((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
159*4882a593Smuzhiyun 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
160*4882a593Smuzhiyun 	((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
161*4882a593Smuzhiyun 		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
162*4882a593Smuzhiyun 	((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
163*4882a593Smuzhiyun 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
164*4882a593Smuzhiyun 	((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
165*4882a593Smuzhiyun 		(1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
166*4882a593Smuzhiyun 	((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
167*4882a593Smuzhiyun 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
168*4882a593Smuzhiyun 	((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
169*4882a593Smuzhiyun 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
170*4882a593Smuzhiyun 	((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
171*4882a593Smuzhiyun 		(0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
172*4882a593Smuzhiyun 	0, 0, 0, 0, 0, 0, 0
173*4882a593Smuzhiyun };
174*4882a593Smuzhiyun 
iwl_send_prio_tbl(struct iwl_priv * priv)175*4882a593Smuzhiyun void iwl_send_prio_tbl(struct iwl_priv *priv)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
180*4882a593Smuzhiyun 		sizeof(iwl_bt_prio_tbl));
181*4882a593Smuzhiyun 	if (iwl_dvm_send_cmd_pdu(priv,
182*4882a593Smuzhiyun 				REPLY_BT_COEX_PRIO_TABLE, 0,
183*4882a593Smuzhiyun 				sizeof(prio_tbl_cmd), &prio_tbl_cmd))
184*4882a593Smuzhiyun 		IWL_ERR(priv, "failed to send BT prio tbl command\n");
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
iwl_send_bt_env(struct iwl_priv * priv,u8 action,u8 type)187*4882a593Smuzhiyun int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	struct iwl_bt_coex_prot_env_cmd env_cmd;
190*4882a593Smuzhiyun 	int ret;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	env_cmd.action = action;
193*4882a593Smuzhiyun 	env_cmd.type = type;
194*4882a593Smuzhiyun 	ret = iwl_dvm_send_cmd_pdu(priv,
195*4882a593Smuzhiyun 			       REPLY_BT_COEX_PROT_ENV, 0,
196*4882a593Smuzhiyun 			       sizeof(env_cmd), &env_cmd);
197*4882a593Smuzhiyun 	if (ret)
198*4882a593Smuzhiyun 		IWL_ERR(priv, "failed to send BT env command\n");
199*4882a593Smuzhiyun 	return ret;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun static const u8 iwlagn_default_queue_to_tx_fifo[] = {
203*4882a593Smuzhiyun 	IWL_TX_FIFO_VO,
204*4882a593Smuzhiyun 	IWL_TX_FIFO_VI,
205*4882a593Smuzhiyun 	IWL_TX_FIFO_BE,
206*4882a593Smuzhiyun 	IWL_TX_FIFO_BK,
207*4882a593Smuzhiyun };
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun static const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
210*4882a593Smuzhiyun 	IWL_TX_FIFO_VO,
211*4882a593Smuzhiyun 	IWL_TX_FIFO_VI,
212*4882a593Smuzhiyun 	IWL_TX_FIFO_BE,
213*4882a593Smuzhiyun 	IWL_TX_FIFO_BK,
214*4882a593Smuzhiyun 	IWL_TX_FIFO_BK_IPAN,
215*4882a593Smuzhiyun 	IWL_TX_FIFO_BE_IPAN,
216*4882a593Smuzhiyun 	IWL_TX_FIFO_VI_IPAN,
217*4882a593Smuzhiyun 	IWL_TX_FIFO_VO_IPAN,
218*4882a593Smuzhiyun 	IWL_TX_FIFO_BE_IPAN,
219*4882a593Smuzhiyun 	IWL_TX_FIFO_UNUSED,
220*4882a593Smuzhiyun 	IWL_TX_FIFO_AUX,
221*4882a593Smuzhiyun };
222*4882a593Smuzhiyun 
iwl_alive_notify(struct iwl_priv * priv)223*4882a593Smuzhiyun static int iwl_alive_notify(struct iwl_priv *priv)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun 	const u8 *queue_to_txf;
226*4882a593Smuzhiyun 	u8 n_queues;
227*4882a593Smuzhiyun 	int ret;
228*4882a593Smuzhiyun 	int i;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	iwl_trans_fw_alive(priv->trans, 0);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN &&
233*4882a593Smuzhiyun 	    priv->nvm_data->sku_cap_ipan_enable) {
234*4882a593Smuzhiyun 		n_queues = ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
235*4882a593Smuzhiyun 		queue_to_txf = iwlagn_ipan_queue_to_tx_fifo;
236*4882a593Smuzhiyun 	} else {
237*4882a593Smuzhiyun 		n_queues = ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
238*4882a593Smuzhiyun 		queue_to_txf = iwlagn_default_queue_to_tx_fifo;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	for (i = 0; i < n_queues; i++)
242*4882a593Smuzhiyun 		if (queue_to_txf[i] != IWL_TX_FIFO_UNUSED)
243*4882a593Smuzhiyun 			iwl_trans_ac_txq_enable(priv->trans, i,
244*4882a593Smuzhiyun 						queue_to_txf[i], 0);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	priv->passive_no_rx = false;
247*4882a593Smuzhiyun 	priv->transport_queue_stop = 0;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	ret = iwl_send_wimax_coex(priv);
250*4882a593Smuzhiyun 	if (ret)
251*4882a593Smuzhiyun 		return ret;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	if (!priv->lib->no_xtal_calib) {
254*4882a593Smuzhiyun 		ret = iwl_set_Xtal_calib(priv);
255*4882a593Smuzhiyun 		if (ret)
256*4882a593Smuzhiyun 			return ret;
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	return iwl_send_calib_results(priv);
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun struct iwl_alive_data {
263*4882a593Smuzhiyun 	bool valid;
264*4882a593Smuzhiyun 	u8 subtype;
265*4882a593Smuzhiyun };
266*4882a593Smuzhiyun 
iwl_alive_fn(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)267*4882a593Smuzhiyun static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
268*4882a593Smuzhiyun 			 struct iwl_rx_packet *pkt, void *data)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	struct iwl_priv *priv =
271*4882a593Smuzhiyun 		container_of(notif_wait, struct iwl_priv, notif_wait);
272*4882a593Smuzhiyun 	struct iwl_alive_data *alive_data = data;
273*4882a593Smuzhiyun 	struct iwl_alive_resp *palive;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	palive = (void *)pkt->data;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
278*4882a593Smuzhiyun 		       "0x%01X 0x%01X\n",
279*4882a593Smuzhiyun 		       palive->is_valid, palive->ver_type,
280*4882a593Smuzhiyun 		       palive->ver_subtype);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	priv->device_pointers.error_event_table =
283*4882a593Smuzhiyun 		le32_to_cpu(palive->error_event_table_ptr);
284*4882a593Smuzhiyun 	priv->device_pointers.log_event_table =
285*4882a593Smuzhiyun 		le32_to_cpu(palive->log_event_table_ptr);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	alive_data->subtype = palive->ver_subtype;
288*4882a593Smuzhiyun 	alive_data->valid = palive->is_valid == UCODE_VALID_OK;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	return true;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun #define UCODE_ALIVE_TIMEOUT	HZ
294*4882a593Smuzhiyun #define UCODE_CALIB_TIMEOUT	(2*HZ)
295*4882a593Smuzhiyun 
iwl_load_ucode_wait_alive(struct iwl_priv * priv,enum iwl_ucode_type ucode_type)296*4882a593Smuzhiyun int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
297*4882a593Smuzhiyun 				 enum iwl_ucode_type ucode_type)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun 	struct iwl_notification_wait alive_wait;
300*4882a593Smuzhiyun 	struct iwl_alive_data alive_data;
301*4882a593Smuzhiyun 	const struct fw_img *fw;
302*4882a593Smuzhiyun 	int ret;
303*4882a593Smuzhiyun 	enum iwl_ucode_type old_type;
304*4882a593Smuzhiyun 	static const u16 alive_cmd[] = { REPLY_ALIVE };
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	fw = iwl_get_ucode_image(priv->fw, ucode_type);
307*4882a593Smuzhiyun 	if (WARN_ON(!fw))
308*4882a593Smuzhiyun 		return -EINVAL;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	old_type = priv->cur_ucode;
311*4882a593Smuzhiyun 	priv->cur_ucode = ucode_type;
312*4882a593Smuzhiyun 	priv->ucode_loaded = false;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
315*4882a593Smuzhiyun 				   alive_cmd, ARRAY_SIZE(alive_cmd),
316*4882a593Smuzhiyun 				   iwl_alive_fn, &alive_data);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	ret = iwl_trans_start_fw(priv->trans, fw, false);
319*4882a593Smuzhiyun 	if (ret) {
320*4882a593Smuzhiyun 		priv->cur_ucode = old_type;
321*4882a593Smuzhiyun 		iwl_remove_notification(&priv->notif_wait, &alive_wait);
322*4882a593Smuzhiyun 		return ret;
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	/*
326*4882a593Smuzhiyun 	 * Some things may run in the background now, but we
327*4882a593Smuzhiyun 	 * just wait for the ALIVE notification here.
328*4882a593Smuzhiyun 	 */
329*4882a593Smuzhiyun 	ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
330*4882a593Smuzhiyun 					UCODE_ALIVE_TIMEOUT);
331*4882a593Smuzhiyun 	if (ret) {
332*4882a593Smuzhiyun 		priv->cur_ucode = old_type;
333*4882a593Smuzhiyun 		return ret;
334*4882a593Smuzhiyun 	}
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	if (!alive_data.valid) {
337*4882a593Smuzhiyun 		IWL_ERR(priv, "Loaded ucode is not valid!\n");
338*4882a593Smuzhiyun 		priv->cur_ucode = old_type;
339*4882a593Smuzhiyun 		return -EIO;
340*4882a593Smuzhiyun 	}
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	priv->ucode_loaded = true;
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	if (ucode_type != IWL_UCODE_WOWLAN) {
345*4882a593Smuzhiyun 		/* delay a bit to give rfkill time to run */
346*4882a593Smuzhiyun 		msleep(5);
347*4882a593Smuzhiyun 	}
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	ret = iwl_alive_notify(priv);
350*4882a593Smuzhiyun 	if (ret) {
351*4882a593Smuzhiyun 		IWL_WARN(priv,
352*4882a593Smuzhiyun 			"Could not complete ALIVE transition: %d\n", ret);
353*4882a593Smuzhiyun 		priv->cur_ucode = old_type;
354*4882a593Smuzhiyun 		return ret;
355*4882a593Smuzhiyun 	}
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	return 0;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
iwlagn_wait_calib(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)360*4882a593Smuzhiyun static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
361*4882a593Smuzhiyun 			      struct iwl_rx_packet *pkt, void *data)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun 	struct iwl_priv *priv = data;
364*4882a593Smuzhiyun 	struct iwl_calib_hdr *hdr;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
367*4882a593Smuzhiyun 		WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
368*4882a593Smuzhiyun 		return true;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	hdr = (struct iwl_calib_hdr *)pkt->data;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	if (iwl_calib_set(priv, hdr, iwl_rx_packet_payload_len(pkt)))
374*4882a593Smuzhiyun 		IWL_ERR(priv, "Failed to record calibration data %d\n",
375*4882a593Smuzhiyun 			hdr->op_code);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	return false;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
iwl_run_init_ucode(struct iwl_priv * priv)380*4882a593Smuzhiyun int iwl_run_init_ucode(struct iwl_priv *priv)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	struct iwl_notification_wait calib_wait;
383*4882a593Smuzhiyun 	static const u16 calib_complete[] = {
384*4882a593Smuzhiyun 		CALIBRATION_RES_NOTIFICATION,
385*4882a593Smuzhiyun 		CALIBRATION_COMPLETE_NOTIFICATION
386*4882a593Smuzhiyun 	};
387*4882a593Smuzhiyun 	int ret;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	lockdep_assert_held(&priv->mutex);
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	/* No init ucode required? Curious, but maybe ok */
392*4882a593Smuzhiyun 	if (!priv->fw->img[IWL_UCODE_INIT].num_sec)
393*4882a593Smuzhiyun 		return 0;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
396*4882a593Smuzhiyun 				   calib_complete, ARRAY_SIZE(calib_complete),
397*4882a593Smuzhiyun 				   iwlagn_wait_calib, priv);
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	/* Will also start the device */
400*4882a593Smuzhiyun 	ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
401*4882a593Smuzhiyun 	if (ret)
402*4882a593Smuzhiyun 		goto error;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	ret = iwl_init_alive_start(priv);
405*4882a593Smuzhiyun 	if (ret)
406*4882a593Smuzhiyun 		goto error;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	/*
409*4882a593Smuzhiyun 	 * Some things may run in the background now, but we
410*4882a593Smuzhiyun 	 * just wait for the calibration complete notification.
411*4882a593Smuzhiyun 	 */
412*4882a593Smuzhiyun 	ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
413*4882a593Smuzhiyun 					UCODE_CALIB_TIMEOUT);
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	goto out;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun  error:
418*4882a593Smuzhiyun 	iwl_remove_notification(&priv->notif_wait, &calib_wait);
419*4882a593Smuzhiyun  out:
420*4882a593Smuzhiyun 	/* Whatever happened, stop the device */
421*4882a593Smuzhiyun 	iwl_trans_stop_device(priv->trans);
422*4882a593Smuzhiyun 	priv->ucode_loaded = false;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	return ret;
425*4882a593Smuzhiyun }
426