blob: cf03ef5619d9fea602151cf573162848ffd12366 [file] [log] [blame]
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -07001/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02005 * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020022 * in the file called COPYING.
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070023 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#include <linux/kernel.h>
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070031
Wey-Yi Guy81b81762010-03-16 10:23:30 -070032#include "iwl-io.h"
Wey-Yi Guy19e6cda2010-03-16 17:41:23 -070033#include "iwl-agn-hw.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070034#include "iwl-trans.h"
Emmanuel Grumbachdda61a42011-08-25 23:11:11 -070035#include "iwl-fh.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020036#include "iwl-op-mode.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070037
Johannes Berg1023fdc2012-05-15 12:16:34 +020038#include "dev.h"
39#include "agn.h"
40#include "calib.h"
41
Don Fryde7f5f92011-11-10 06:55:10 -080042/******************************************************************************
43 *
44 * uCode download functions
45 *
46 ******************************************************************************/
47
Johannes Berg0692fe42012-03-06 13:30:37 -080048static inline const struct fw_img *
49iwl_get_ucode_image(struct iwl_priv *priv, enum iwl_ucode_type ucode_type)
Don Fry8929c242011-11-10 06:55:08 -080050{
David Spinadel6dfa8d02012-03-10 13:00:14 -080051 if (ucode_type >= IWL_UCODE_TYPE_MAX)
52 return NULL;
53
54 return &priv->fw->img[ucode_type];
Don Fry8929c242011-11-10 06:55:08 -080055}
56
Wey-Yi Guy741a6262010-03-16 12:37:24 -070057/*
58 * Calibration
59 */
Johannes Berge1991882012-03-06 13:30:36 -080060static int iwl_set_Xtal_calib(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -070061{
62 struct iwl_calib_xtal_freq_cmd cmd;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +020063 __le16 *xtal_calib = priv->nvm_data->xtal_calib;
Wey-Yi Guy741a6262010-03-16 12:37:24 -070064
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070065 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
Wey-Yi Guy741a6262010-03-16 12:37:24 -070066 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
67 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
Johannes Berge1991882012-03-06 13:30:36 -080068 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guy741a6262010-03-16 12:37:24 -070069}
70
Johannes Berge1991882012-03-06 13:30:36 -080071static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
Shanyu Zhaobf53f932010-09-21 16:54:01 -070072{
73 struct iwl_calib_temperature_offset_cmd cmd;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070074
75 memset(&cmd, 0, sizeof(cmd));
76 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Eytan Lifshitzb7998c82012-12-01 20:59:49 +020077 cmd.radio_sensor_offset = priv->nvm_data->raw_temperature;
Shanyu Zhaobf53f932010-09-21 16:54:01 -070078 if (!(cmd.radio_sensor_offset))
79 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070080
Johannes Berge1991882012-03-06 13:30:36 -080081 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
Wey-Yi Guy2e277992011-07-08 14:29:48 -070082 le16_to_cpu(cmd.radio_sensor_offset));
Johannes Berge1991882012-03-06 13:30:36 -080083 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Shanyu Zhaobf53f932010-09-21 16:54:01 -070084}
85
Johannes Berge1991882012-03-06 13:30:36 -080086static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070087{
88 struct iwl_calib_temperature_offset_v2_cmd cmd;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070089
90 memset(&cmd, 0, sizeof(cmd));
91 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Eytan Lifshitzb7998c82012-12-01 20:59:49 +020092 cmd.radio_sensor_offset_high = priv->nvm_data->kelvin_temperature;
93 cmd.radio_sensor_offset_low = priv->nvm_data->raw_temperature;
Johannes Berg26a7ca92012-05-21 11:55:54 +020094 if (!cmd.radio_sensor_offset_low) {
Johannes Berge1991882012-03-06 13:30:36 -080095 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070096 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
97 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
98 }
Eytan Lifshitzb7998c82012-12-01 20:59:49 +020099 cmd.burntVoltageRef = priv->nvm_data->calib_voltage;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700100
Johannes Berge1991882012-03-06 13:30:36 -0800101 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700102 le16_to_cpu(cmd.radio_sensor_offset_high));
Johannes Berge1991882012-03-06 13:30:36 -0800103 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700104 le16_to_cpu(cmd.radio_sensor_offset_low));
Johannes Berge1991882012-03-06 13:30:36 -0800105 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700106 le16_to_cpu(cmd.burntVoltageRef));
107
Johannes Berge1991882012-03-06 13:30:36 -0800108 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700109}
110
Johannes Berge1991882012-03-06 13:30:36 -0800111static int iwl_send_calib_cfg(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700112{
113 struct iwl_calib_cfg_cmd calib_cfg_cmd;
114 struct iwl_host_cmd cmd = {
115 .id = CALIBRATION_CFG_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700116 .len = { sizeof(struct iwl_calib_cfg_cmd), },
117 .data = { &calib_cfg_cmd, },
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700118 };
119
120 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
121 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
122 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
123 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
Wey-Yi Guydf2a4dc2011-07-08 14:29:45 -0700124 calib_cfg_cmd.ucd_calib_cfg.flags =
125 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700126
Johannes Berge10a0532012-03-06 13:30:39 -0800127 return iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700128}
129
Johannes Berge1991882012-03-06 13:30:36 -0800130int iwl_init_alive_start(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700131{
Johannes Bergca7966c2011-04-22 10:15:23 -0700132 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700133
Johannes Berg0d8877a2013-05-17 10:36:29 +0200134 if (priv->lib->bt_params &&
135 priv->lib->bt_params->advanced_bt_coexist) {
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700136 /*
137 * Tell uCode we are ready to perform calibration
138 * need to perform this before any calibration
139 * no need to close the envlope since we are going
140 * to load the runtime uCode later.
141 */
Johannes Berge1991882012-03-06 13:30:36 -0800142 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700143 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
Johannes Bergca7966c2011-04-22 10:15:23 -0700144 if (ret)
145 return ret;
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700146
147 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700148
Johannes Berge1991882012-03-06 13:30:36 -0800149 ret = iwl_send_calib_cfg(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700150 if (ret)
151 return ret;
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700152
153 /**
154 * temperature offset calibration is only needed for runtime ucode,
155 * so prepare the value now.
156 */
Johannes Berg0d8877a2013-05-17 10:36:29 +0200157 if (priv->lib->need_temp_offset_calib) {
158 if (priv->lib->temp_offset_v2)
Johannes Berge1991882012-03-06 13:30:36 -0800159 return iwl_set_temperature_offset_calib_v2(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700160 else
Johannes Berge1991882012-03-06 13:30:36 -0800161 return iwl_set_temperature_offset_calib(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700162 }
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700163
Johannes Bergca7966c2011-04-22 10:15:23 -0700164 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700165}
166
Johannes Bergaca86262012-05-14 09:08:50 +0200167static int iwl_send_wimax_coex(struct iwl_priv *priv)
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700168{
169 struct iwl_wimax_coex_cmd coex_cmd;
170
Johannes Berg85560af2012-03-15 13:26:48 -0700171 /* coexistence is disabled */
172 memset(&coex_cmd, 0, sizeof(coex_cmd));
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700173
Johannes Berge10a0532012-03-06 13:30:39 -0800174 return iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700175 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700176 sizeof(coex_cmd), &coex_cmd);
177}
178
Don Fry66128b12011-11-28 14:35:14 -0800179static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700180 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
181 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
182 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
183 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
184 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
185 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
186 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
187 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
188 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
189 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
190 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
191 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
192 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
193 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
194 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
195 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
196 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
197 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
198 0, 0, 0, 0, 0, 0, 0
199};
200
Johannes Berge1991882012-03-06 13:30:36 -0800201void iwl_send_prio_tbl(struct iwl_priv *priv)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700202{
203 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
204
Don Fry66128b12011-11-28 14:35:14 -0800205 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
206 sizeof(iwl_bt_prio_tbl));
Johannes Berge10a0532012-03-06 13:30:39 -0800207 if (iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700208 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700209 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
Johannes Berge1991882012-03-06 13:30:36 -0800210 IWL_ERR(priv, "failed to send BT prio tbl command\n");
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700211}
212
Johannes Berge1991882012-03-06 13:30:36 -0800213int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700214{
215 struct iwl_bt_coex_prot_env_cmd env_cmd;
Johannes Bergca7966c2011-04-22 10:15:23 -0700216 int ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700217
218 env_cmd.action = action;
219 env_cmd.type = type;
Johannes Berge10a0532012-03-06 13:30:39 -0800220 ret = iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700221 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
Johannes Bergca7966c2011-04-22 10:15:23 -0700222 sizeof(env_cmd), &env_cmd);
223 if (ret)
Johannes Berge1991882012-03-06 13:30:36 -0800224 IWL_ERR(priv, "failed to send BT env command\n");
Johannes Bergca7966c2011-04-22 10:15:23 -0700225 return ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700226}
227
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300228static const u8 iwlagn_default_queue_to_tx_fifo[] = {
229 IWL_TX_FIFO_VO,
230 IWL_TX_FIFO_VI,
231 IWL_TX_FIFO_BE,
232 IWL_TX_FIFO_BK,
233};
234
235static const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
236 IWL_TX_FIFO_VO,
237 IWL_TX_FIFO_VI,
238 IWL_TX_FIFO_BE,
239 IWL_TX_FIFO_BK,
240 IWL_TX_FIFO_BK_IPAN,
241 IWL_TX_FIFO_BE_IPAN,
242 IWL_TX_FIFO_VI_IPAN,
243 IWL_TX_FIFO_VO_IPAN,
244 IWL_TX_FIFO_BE_IPAN,
245 IWL_TX_FIFO_UNUSED,
246 IWL_TX_FIFO_AUX,
247};
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700248
Johannes Berge1991882012-03-06 13:30:36 -0800249static int iwl_alive_notify(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700250{
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300251 const u8 *queue_to_txf;
252 u8 n_queues;
Wey-Yi Guy74159522011-04-05 09:42:01 -0700253 int ret;
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300254 int i;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700255
Emmanuel Grumbachadca1232012-10-25 23:08:27 +0200256 iwl_trans_fw_alive(priv->trans, 0);
Johannes Berge755f882012-03-07 09:52:16 -0800257
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300258 if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN &&
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200259 priv->nvm_data->sku_cap_ipan_enable) {
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300260 n_queues = ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
261 queue_to_txf = iwlagn_ipan_queue_to_tx_fifo;
262 } else {
263 n_queues = ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
264 queue_to_txf = iwlagn_default_queue_to_tx_fifo;
265 }
266
267 for (i = 0; i < n_queues; i++)
268 if (queue_to_txf[i] != IWL_TX_FIFO_UNUSED)
269 iwl_trans_ac_txq_enable(priv->trans, i,
270 queue_to_txf[i]);
271
Johannes Berge755f882012-03-07 09:52:16 -0800272 priv->passive_no_rx = false;
273 priv->transport_queue_stop = 0;
Grumbach, Emmanuele7cad692010-11-18 03:47:38 -0800274
Johannes Berge1991882012-03-06 13:30:36 -0800275 ret = iwl_send_wimax_coex(priv);
Wey-Yi Guy74159522011-04-05 09:42:01 -0700276 if (ret)
277 return ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700278
Johannes Berg0d8877a2013-05-17 10:36:29 +0200279 if (!priv->lib->no_xtal_calib) {
Johannes Berge1991882012-03-06 13:30:36 -0800280 ret = iwl_set_Xtal_calib(priv);
Johannes Berg93b64102011-11-17 08:51:53 -0800281 if (ret)
282 return ret;
283 }
Wey-Yi Guy74159522011-04-05 09:42:01 -0700284
Johannes Berge1991882012-03-06 13:30:36 -0800285 return iwl_send_calib_results(priv);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700286}
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700287
Don Fry69a679b2011-12-07 08:50:46 -0800288struct iwl_alive_data {
Johannes Bergca7966c2011-04-22 10:15:23 -0700289 bool valid;
290 u8 subtype;
291};
292
Johannes Bergdb662d42012-03-15 13:26:44 -0700293static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
294 struct iwl_rx_packet *pkt, void *data)
Johannes Bergca7966c2011-04-22 10:15:23 -0700295{
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800296 struct iwl_priv *priv =
297 container_of(notif_wait, struct iwl_priv, notif_wait);
Don Fry69a679b2011-12-07 08:50:46 -0800298 struct iwl_alive_data *alive_data = data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700299 struct iwl_alive_resp *palive;
300
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800301 palive = (void *)pkt->data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700302
Johannes Berge1991882012-03-06 13:30:36 -0800303 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
Johannes Bergca7966c2011-04-22 10:15:23 -0700304 "0x%01X 0x%01X\n",
305 palive->is_valid, palive->ver_type,
306 palive->ver_subtype);
307
Meenakshi Venkataraman2fdfc472012-03-15 13:26:56 -0700308 priv->device_pointers.error_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700309 le32_to_cpu(palive->error_event_table_ptr);
Meenakshi Venkataraman2fdfc472012-03-15 13:26:56 -0700310 priv->device_pointers.log_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700311 le32_to_cpu(palive->log_event_table_ptr);
312
313 alive_data->subtype = palive->ver_subtype;
314 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
Johannes Bergdb662d42012-03-15 13:26:44 -0700315
316 return true;
Johannes Bergca7966c2011-04-22 10:15:23 -0700317}
318
319#define UCODE_ALIVE_TIMEOUT HZ
320#define UCODE_CALIB_TIMEOUT (2*HZ)
321
Johannes Berge1991882012-03-06 13:30:36 -0800322int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800323 enum iwl_ucode_type ucode_type)
Johannes Bergca7966c2011-04-22 10:15:23 -0700324{
325 struct iwl_notification_wait alive_wait;
Don Fry69a679b2011-12-07 08:50:46 -0800326 struct iwl_alive_data alive_data;
Johannes Berg0692fe42012-03-06 13:30:37 -0800327 const struct fw_img *fw;
Johannes Bergca7966c2011-04-22 10:15:23 -0700328 int ret;
Don Fryde7f5f92011-11-10 06:55:10 -0800329 enum iwl_ucode_type old_type;
Johannes Bergdb662d42012-03-15 13:26:44 -0700330 static const u8 alive_cmd[] = { REPLY_ALIVE };
Johannes Bergca7966c2011-04-22 10:15:23 -0700331
Johannes Bergbefe9b62013-10-25 12:32:51 +0200332 fw = iwl_get_ucode_image(priv, ucode_type);
333 if (WARN_ON(!fw))
334 return -EINVAL;
335
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700336 old_type = priv->cur_ucode;
337 priv->cur_ucode = ucode_type;
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800338 priv->ucode_loaded = false;
339
Johannes Bergdb662d42012-03-15 13:26:44 -0700340 iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
341 alive_cmd, ARRAY_SIZE(alive_cmd),
342 iwl_alive_fn, &alive_data);
Johannes Bergf4720892012-03-06 13:30:57 -0800343
Emmanuel Grumbach6ae02f32012-12-24 11:10:43 +0200344 ret = iwl_trans_start_fw(priv->trans, fw, false);
Johannes Bergca7966c2011-04-22 10:15:23 -0700345 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700346 priv->cur_ucode = old_type;
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800347 iwl_remove_notification(&priv->notif_wait, &alive_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700348 return ret;
349 }
350
Johannes Bergca7966c2011-04-22 10:15:23 -0700351 /*
352 * Some things may run in the background now, but we
353 * just wait for the ALIVE notification here.
354 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800355 ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800356 UCODE_ALIVE_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700357 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700358 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700359 return ret;
360 }
361
362 if (!alive_data.valid) {
Johannes Berge1991882012-03-06 13:30:36 -0800363 IWL_ERR(priv, "Loaded ucode is not valid!\n");
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700364 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700365 return -EIO;
366 }
367
Emmanuel Grumbach2d5d50e2013-01-31 15:03:55 +0200368 priv->ucode_loaded = true;
369
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700370 if (ucode_type != IWL_UCODE_WOWLAN) {
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700371 /* delay a bit to give rfkill time to run */
372 msleep(5);
373 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700374
Johannes Berge1991882012-03-06 13:30:36 -0800375 ret = iwl_alive_notify(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700376 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800377 IWL_WARN(priv,
Johannes Bergca7966c2011-04-22 10:15:23 -0700378 "Could not complete ALIVE transition: %d\n", ret);
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700379 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700380 return ret;
381 }
382
383 return 0;
384}
385
Johannes Berge1f0c502012-03-15 13:26:45 -0700386static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
387 struct iwl_rx_packet *pkt, void *data)
388{
389 struct iwl_priv *priv = data;
390 struct iwl_calib_hdr *hdr;
Johannes Berge1f0c502012-03-15 13:26:45 -0700391
392 if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
393 WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
394 return true;
395 }
396
397 hdr = (struct iwl_calib_hdr *)pkt->data;
Johannes Berge1f0c502012-03-15 13:26:45 -0700398
Johannes Berg65b30342014-01-08 13:16:33 +0100399 if (iwl_calib_set(priv, hdr, iwl_rx_packet_payload_len(pkt)))
Johannes Berge1f0c502012-03-15 13:26:45 -0700400 IWL_ERR(priv, "Failed to record calibration data %d\n",
401 hdr->op_code);
402
403 return false;
404}
405
Johannes Berge1991882012-03-06 13:30:36 -0800406int iwl_run_init_ucode(struct iwl_priv *priv)
Johannes Bergca7966c2011-04-22 10:15:23 -0700407{
408 struct iwl_notification_wait calib_wait;
Johannes Bergdb662d42012-03-15 13:26:44 -0700409 static const u8 calib_complete[] = {
Johannes Berge1f0c502012-03-15 13:26:45 -0700410 CALIBRATION_RES_NOTIFICATION,
Johannes Bergdb662d42012-03-15 13:26:44 -0700411 CALIBRATION_COMPLETE_NOTIFICATION
412 };
Johannes Bergca7966c2011-04-22 10:15:23 -0700413 int ret;
414
Johannes Bergb1eea292012-03-06 13:30:42 -0800415 lockdep_assert_held(&priv->mutex);
Johannes Bergca7966c2011-04-22 10:15:23 -0700416
417 /* No init ucode required? Curious, but maybe ok */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800418 if (!priv->fw->img[IWL_UCODE_INIT].sec[0].len)
Johannes Bergca7966c2011-04-22 10:15:23 -0700419 return 0;
420
David Spinadelb5ea1622012-03-10 13:00:11 -0800421 if (priv->init_ucode_run)
Johannes Bergca7966c2011-04-22 10:15:23 -0700422 return 0;
423
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800424 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
Johannes Bergdb662d42012-03-15 13:26:44 -0700425 calib_complete, ARRAY_SIZE(calib_complete),
Johannes Berge1f0c502012-03-15 13:26:45 -0700426 iwlagn_wait_calib, priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700427
428 /* Will also start the device */
Johannes Berge1991882012-03-06 13:30:36 -0800429 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700430 if (ret)
431 goto error;
432
Johannes Berge1991882012-03-06 13:30:36 -0800433 ret = iwl_init_alive_start(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700434 if (ret)
435 goto error;
436
437 /*
438 * Some things may run in the background now, but we
439 * just wait for the calibration complete notification.
440 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800441 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800442 UCODE_CALIB_TIMEOUT);
David Spinadelb5ea1622012-03-10 13:00:11 -0800443 if (!ret)
444 priv->init_ucode_run = true;
Johannes Bergca7966c2011-04-22 10:15:23 -0700445
446 goto out;
447
448 error:
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800449 iwl_remove_notification(&priv->notif_wait, &calib_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700450 out:
451 /* Whatever happened, stop the device */
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700452 iwl_trans_stop_device(priv->trans);
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800453 priv->ucode_loaded = false;
454
Johannes Bergca7966c2011-04-22 10:15:23 -0700455 return ret;
456}