Linux Audio

Check our new training course

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